xref: /openbmc/linux/drivers/md/dm-mpath.c (revision 2eb6e1e3aa873f2bb62075bebe17fa108ee07374)
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 
141da177e4SLinus Torvalds #include <linux/ctype.h>
151da177e4SLinus Torvalds #include <linux/init.h>
161da177e4SLinus Torvalds #include <linux/mempool.h>
171da177e4SLinus Torvalds #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/pagemap.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/time.h>
211da177e4SLinus Torvalds #include <linux/workqueue.h>
2235991652SMikulas Patocka #include <linux/delay.h>
23cfae5c9bSChandra Seetharaman #include <scsi/scsi_dh.h>
2460063497SArun Sharma #include <linux/atomic.h>
251da177e4SLinus Torvalds 
2672d94861SAlasdair G Kergon #define DM_MSG_PREFIX "multipath"
274e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_MSECS 2000
284e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds /* Path properties */
311da177e4SLinus Torvalds struct pgpath {
321da177e4SLinus Torvalds 	struct list_head list;
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds 	struct priority_group *pg;	/* Owning PG */
356680073dSKiyoshi Ueda 	unsigned is_active;		/* Path status */
361da177e4SLinus Torvalds 	unsigned fail_count;		/* Cumulative failure count */
371da177e4SLinus Torvalds 
38c922d5f7SJosef "Jeff" Sipek 	struct dm_path path;
394e2d19e4SChandra Seetharaman 	struct delayed_work activate_path;
401da177e4SLinus Torvalds };
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds /*
451da177e4SLinus Torvalds  * Paths are grouped into Priority Groups and numbered from 1 upwards.
461da177e4SLinus Torvalds  * Each has a path selector which controls which path gets used.
471da177e4SLinus Torvalds  */
481da177e4SLinus Torvalds struct priority_group {
491da177e4SLinus Torvalds 	struct list_head list;
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds 	struct multipath *m;		/* Owning multipath instance */
521da177e4SLinus Torvalds 	struct path_selector ps;
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds 	unsigned pg_num;		/* Reference number */
551da177e4SLinus Torvalds 	unsigned bypassed;		/* Temporarily bypass this PG? */
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds 	unsigned nr_pgpaths;		/* Number of paths in PG */
581da177e4SLinus Torvalds 	struct list_head pgpaths;
591da177e4SLinus Torvalds };
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds /* Multipath context */
621da177e4SLinus Torvalds struct multipath {
631da177e4SLinus Torvalds 	struct list_head list;
641da177e4SLinus Torvalds 	struct dm_target *ti;
651da177e4SLinus Torvalds 
66cfae5c9bSChandra Seetharaman 	const char *hw_handler_name;
672bfd2e13SChandra Seetharaman 	char *hw_handler_params;
684e2d19e4SChandra Seetharaman 
691fbdd2b3SMike Snitzer 	spinlock_t lock;
701fbdd2b3SMike Snitzer 
711da177e4SLinus Torvalds 	unsigned nr_priority_groups;
721da177e4SLinus Torvalds 	struct list_head priority_groups;
734e2d19e4SChandra Seetharaman 
744e2d19e4SChandra Seetharaman 	wait_queue_head_t pg_init_wait;	/* Wait for pg_init completion */
754e2d19e4SChandra Seetharaman 
761da177e4SLinus Torvalds 	unsigned pg_init_required;	/* pg_init needs calling? */
77c3cd4f6bSAlasdair G Kergon 	unsigned pg_init_in_progress;	/* Only one pg_init allowed at once */
784e2d19e4SChandra Seetharaman 	unsigned pg_init_delay_retry;	/* Delay pg_init retry? */
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	unsigned nr_valid_paths;	/* Total number of usable paths */
811da177e4SLinus Torvalds 	struct pgpath *current_pgpath;
821da177e4SLinus Torvalds 	struct priority_group *current_pg;
831da177e4SLinus Torvalds 	struct priority_group *next_pg;	/* Switch to this PG if set */
841da177e4SLinus Torvalds 	unsigned repeat_count;		/* I/Os left before calling PS again */
851da177e4SLinus Torvalds 
861fbdd2b3SMike Snitzer 	unsigned queue_io:1;		/* Must we queue all I/O? */
871fbdd2b3SMike Snitzer 	unsigned queue_if_no_path:1;	/* Queue I/O if last path fails? */
881fbdd2b3SMike Snitzer 	unsigned saved_queue_if_no_path:1; /* Saved state during suspension */
89a58a935dSMike Snitzer 	unsigned retain_attached_hw_handler:1; /* If there's already a hw_handler present, don't change it. */
90954a73d5SShiva Krishna Merla 	unsigned pg_init_disabled:1;	/* pg_init is not currently allowed */
911fbdd2b3SMike Snitzer 
92c9e45581SDave Wysochanski 	unsigned pg_init_retries;	/* Number of times to retry pg_init */
93c9e45581SDave Wysochanski 	unsigned pg_init_count;		/* Number of times pg_init called */
944e2d19e4SChandra Seetharaman 	unsigned pg_init_delay_msecs;	/* Number of msecs before pg_init retry */
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	struct work_struct trigger_event;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	/*
99028867acSAlasdair G Kergon 	 * We must use a mempool of dm_mpath_io structs so that we
1001da177e4SLinus Torvalds 	 * can resubmit bios on error.
1011da177e4SLinus Torvalds 	 */
1021da177e4SLinus Torvalds 	mempool_t *mpio_pool;
1036380f26fSMike Anderson 
1046380f26fSMike Anderson 	struct mutex work_mutex;
1051da177e4SLinus Torvalds };
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds /*
1081da177e4SLinus Torvalds  * Context information attached to each bio we process.
1091da177e4SLinus Torvalds  */
110028867acSAlasdair G Kergon struct dm_mpath_io {
1111da177e4SLinus Torvalds 	struct pgpath *pgpath;
11202ab823fSKiyoshi Ueda 	size_t nr_bytes;
1131da177e4SLinus Torvalds };
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds typedef int (*action_fn) (struct pgpath *pgpath);
1161da177e4SLinus Torvalds 
117e18b890bSChristoph Lameter static struct kmem_cache *_mpio_cache;
1181da177e4SLinus Torvalds 
119bab7cfc7SChandra Seetharaman static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
120c4028958SDavid Howells static void trigger_event(struct work_struct *work);
121bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work);
122e8099177SHannes Reinecke static int __pgpath_busy(struct pgpath *pgpath);
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds /*-----------------------------------------------
1261da177e4SLinus Torvalds  * Allocation routines
1271da177e4SLinus Torvalds  *-----------------------------------------------*/
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds static struct pgpath *alloc_pgpath(void)
1301da177e4SLinus Torvalds {
131e69fae56SMicha³ Miros³aw 	struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
1321da177e4SLinus Torvalds 
133224cb3e9SMike Anderson 	if (pgpath) {
1346680073dSKiyoshi Ueda 		pgpath->is_active = 1;
1354e2d19e4SChandra Seetharaman 		INIT_DELAYED_WORK(&pgpath->activate_path, activate_path);
136224cb3e9SMike Anderson 	}
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	return pgpath;
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
141028867acSAlasdair G Kergon static void free_pgpath(struct pgpath *pgpath)
1421da177e4SLinus Torvalds {
1431da177e4SLinus Torvalds 	kfree(pgpath);
1441da177e4SLinus Torvalds }
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds static struct priority_group *alloc_priority_group(void)
1471da177e4SLinus Torvalds {
1481da177e4SLinus Torvalds 	struct priority_group *pg;
1491da177e4SLinus Torvalds 
150e69fae56SMicha³ Miros³aw 	pg = kzalloc(sizeof(*pg), GFP_KERNEL);
1511da177e4SLinus Torvalds 
152e69fae56SMicha³ Miros³aw 	if (pg)
1531da177e4SLinus Torvalds 		INIT_LIST_HEAD(&pg->pgpaths);
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	return pg;
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
1591da177e4SLinus Torvalds {
1601da177e4SLinus Torvalds 	struct pgpath *pgpath, *tmp;
161ae11b1b3SHannes Reinecke 	struct multipath *m = ti->private;
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
1641da177e4SLinus Torvalds 		list_del(&pgpath->list);
165ae11b1b3SHannes Reinecke 		if (m->hw_handler_name)
166ae11b1b3SHannes Reinecke 			scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
1671da177e4SLinus Torvalds 		dm_put_device(ti, pgpath->path.dev);
1681da177e4SLinus Torvalds 		free_pgpath(pgpath);
1691da177e4SLinus Torvalds 	}
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds static void free_priority_group(struct priority_group *pg,
1731da177e4SLinus Torvalds 				struct dm_target *ti)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	struct path_selector *ps = &pg->ps;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	if (ps->type) {
1781da177e4SLinus Torvalds 		ps->type->destroy(ps);
1791da177e4SLinus Torvalds 		dm_put_path_selector(ps->type);
1801da177e4SLinus Torvalds 	}
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	free_pgpaths(&pg->pgpaths, ti);
1831da177e4SLinus Torvalds 	kfree(pg);
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
18628f16c20SMicha³ Miros³aw static struct multipath *alloc_multipath(struct dm_target *ti)
1871da177e4SLinus Torvalds {
1881da177e4SLinus Torvalds 	struct multipath *m;
189f4790826SMike Snitzer 	unsigned min_ios = dm_get_reserved_rq_based_ios();
1901da177e4SLinus Torvalds 
191e69fae56SMicha³ Miros³aw 	m = kzalloc(sizeof(*m), GFP_KERNEL);
1921da177e4SLinus Torvalds 	if (m) {
1931da177e4SLinus Torvalds 		INIT_LIST_HEAD(&m->priority_groups);
1941da177e4SLinus Torvalds 		spin_lock_init(&m->lock);
1951da177e4SLinus Torvalds 		m->queue_io = 1;
1964e2d19e4SChandra Seetharaman 		m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
197c4028958SDavid Howells 		INIT_WORK(&m->trigger_event, trigger_event);
1982bded7bdSKiyoshi Ueda 		init_waitqueue_head(&m->pg_init_wait);
1996380f26fSMike Anderson 		mutex_init(&m->work_mutex);
200f4790826SMike Snitzer 		m->mpio_pool = mempool_create_slab_pool(min_ios, _mpio_cache);
2011da177e4SLinus Torvalds 		if (!m->mpio_pool) {
2021da177e4SLinus Torvalds 			kfree(m);
2031da177e4SLinus Torvalds 			return NULL;
2041da177e4SLinus Torvalds 		}
20528f16c20SMicha³ Miros³aw 		m->ti = ti;
20628f16c20SMicha³ Miros³aw 		ti->private = m;
2071da177e4SLinus Torvalds 	}
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	return m;
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds static void free_multipath(struct multipath *m)
2131da177e4SLinus Torvalds {
2141da177e4SLinus Torvalds 	struct priority_group *pg, *tmp;
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
2171da177e4SLinus Torvalds 		list_del(&pg->list);
2181da177e4SLinus Torvalds 		free_priority_group(pg, m->ti);
2191da177e4SLinus Torvalds 	}
2201da177e4SLinus Torvalds 
221cfae5c9bSChandra Seetharaman 	kfree(m->hw_handler_name);
2222bfd2e13SChandra Seetharaman 	kfree(m->hw_handler_params);
2231da177e4SLinus Torvalds 	mempool_destroy(m->mpio_pool);
2241da177e4SLinus Torvalds 	kfree(m);
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds 
227466891f9SJun'ichi Nomura static int set_mapinfo(struct multipath *m, union map_info *info)
228466891f9SJun'ichi Nomura {
229466891f9SJun'ichi Nomura 	struct dm_mpath_io *mpio;
230466891f9SJun'ichi Nomura 
231466891f9SJun'ichi Nomura 	mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
232466891f9SJun'ichi Nomura 	if (!mpio)
233466891f9SJun'ichi Nomura 		return -ENOMEM;
234466891f9SJun'ichi Nomura 
235466891f9SJun'ichi Nomura 	memset(mpio, 0, sizeof(*mpio));
236466891f9SJun'ichi Nomura 	info->ptr = mpio;
237466891f9SJun'ichi Nomura 
238466891f9SJun'ichi Nomura 	return 0;
239466891f9SJun'ichi Nomura }
240466891f9SJun'ichi Nomura 
241466891f9SJun'ichi Nomura static void clear_mapinfo(struct multipath *m, union map_info *info)
242466891f9SJun'ichi Nomura {
243466891f9SJun'ichi Nomura 	struct dm_mpath_io *mpio = info->ptr;
244466891f9SJun'ichi Nomura 
245466891f9SJun'ichi Nomura 	info->ptr = NULL;
246466891f9SJun'ichi Nomura 	mempool_free(mpio, m->mpio_pool);
247466891f9SJun'ichi Nomura }
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds /*-----------------------------------------------
2501da177e4SLinus Torvalds  * Path selection
2511da177e4SLinus Torvalds  *-----------------------------------------------*/
2521da177e4SLinus Torvalds 
2533e9f1be1SHannes Reinecke static int __pg_init_all_paths(struct multipath *m)
254fb612642SKiyoshi Ueda {
255fb612642SKiyoshi Ueda 	struct pgpath *pgpath;
2564e2d19e4SChandra Seetharaman 	unsigned long pg_init_delay = 0;
257fb612642SKiyoshi Ueda 
25817f4ff45SHannes Reinecke 	if (m->pg_init_in_progress || m->pg_init_disabled)
2593e9f1be1SHannes Reinecke 		return 0;
26017f4ff45SHannes Reinecke 
261fb612642SKiyoshi Ueda 	m->pg_init_count++;
262fb612642SKiyoshi Ueda 	m->pg_init_required = 0;
2633e9f1be1SHannes Reinecke 
2643e9f1be1SHannes Reinecke 	/* Check here to reset pg_init_required */
2653e9f1be1SHannes Reinecke 	if (!m->current_pg)
2663e9f1be1SHannes Reinecke 		return 0;
2673e9f1be1SHannes Reinecke 
2684e2d19e4SChandra Seetharaman 	if (m->pg_init_delay_retry)
2694e2d19e4SChandra Seetharaman 		pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
2704e2d19e4SChandra Seetharaman 						 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
271fb612642SKiyoshi Ueda 	list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
272fb612642SKiyoshi Ueda 		/* Skip failed paths */
273fb612642SKiyoshi Ueda 		if (!pgpath->is_active)
274fb612642SKiyoshi Ueda 			continue;
2754e2d19e4SChandra Seetharaman 		if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
2764e2d19e4SChandra Seetharaman 				       pg_init_delay))
277fb612642SKiyoshi Ueda 			m->pg_init_in_progress++;
278fb612642SKiyoshi Ueda 	}
2793e9f1be1SHannes Reinecke 	return m->pg_init_in_progress;
280fb612642SKiyoshi Ueda }
281fb612642SKiyoshi Ueda 
2821da177e4SLinus Torvalds static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
2831da177e4SLinus Torvalds {
2841da177e4SLinus Torvalds 	m->current_pg = pgpath->pg;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	/* Must we initialise the PG first, and queue I/O till it's ready? */
287cfae5c9bSChandra Seetharaman 	if (m->hw_handler_name) {
2881da177e4SLinus Torvalds 		m->pg_init_required = 1;
2891da177e4SLinus Torvalds 		m->queue_io = 1;
2901da177e4SLinus Torvalds 	} else {
2911da177e4SLinus Torvalds 		m->pg_init_required = 0;
2921da177e4SLinus Torvalds 		m->queue_io = 0;
2931da177e4SLinus Torvalds 	}
294c9e45581SDave Wysochanski 
295c9e45581SDave Wysochanski 	m->pg_init_count = 0;
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
29802ab823fSKiyoshi Ueda static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
29902ab823fSKiyoshi Ueda 			       size_t nr_bytes)
3001da177e4SLinus Torvalds {
301c922d5f7SJosef "Jeff" Sipek 	struct dm_path *path;
3021da177e4SLinus Torvalds 
30302ab823fSKiyoshi Ueda 	path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
3041da177e4SLinus Torvalds 	if (!path)
3051da177e4SLinus Torvalds 		return -ENXIO;
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	m->current_pgpath = path_to_pgpath(path);
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	if (m->current_pg != pg)
3101da177e4SLinus Torvalds 		__switch_pg(m, m->current_pgpath);
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	return 0;
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds 
31502ab823fSKiyoshi Ueda static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
3161da177e4SLinus Torvalds {
3171da177e4SLinus Torvalds 	struct priority_group *pg;
3181da177e4SLinus Torvalds 	unsigned bypassed = 1;
3191da177e4SLinus Torvalds 
3201f271972SBenjamin Marzinski 	if (!m->nr_valid_paths) {
3211f271972SBenjamin Marzinski 		m->queue_io = 0;
3221da177e4SLinus Torvalds 		goto failed;
3231f271972SBenjamin Marzinski 	}
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	/* Were we instructed to switch PG? */
3261da177e4SLinus Torvalds 	if (m->next_pg) {
3271da177e4SLinus Torvalds 		pg = m->next_pg;
3281da177e4SLinus Torvalds 		m->next_pg = NULL;
32902ab823fSKiyoshi Ueda 		if (!__choose_path_in_pg(m, pg, nr_bytes))
3301da177e4SLinus Torvalds 			return;
3311da177e4SLinus Torvalds 	}
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	/* Don't change PG until it has no remaining paths */
33402ab823fSKiyoshi Ueda 	if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
3351da177e4SLinus Torvalds 		return;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	/*
3381da177e4SLinus Torvalds 	 * Loop through priority groups until we find a valid path.
3391da177e4SLinus Torvalds 	 * First time we skip PGs marked 'bypassed'.
340f220fd4eSMike Christie 	 * Second time we only try the ones we skipped, but set
341f220fd4eSMike Christie 	 * pg_init_delay_retry so we do not hammer controllers.
3421da177e4SLinus Torvalds 	 */
3431da177e4SLinus Torvalds 	do {
3441da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
3451da177e4SLinus Torvalds 			if (pg->bypassed == bypassed)
3461da177e4SLinus Torvalds 				continue;
347f220fd4eSMike Christie 			if (!__choose_path_in_pg(m, pg, nr_bytes)) {
348f220fd4eSMike Christie 				if (!bypassed)
349f220fd4eSMike Christie 					m->pg_init_delay_retry = 1;
3501da177e4SLinus Torvalds 				return;
3511da177e4SLinus Torvalds 			}
352f220fd4eSMike Christie 		}
3531da177e4SLinus Torvalds 	} while (bypassed--);
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds failed:
3561da177e4SLinus Torvalds 	m->current_pgpath = NULL;
3571da177e4SLinus Torvalds 	m->current_pg = NULL;
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds 
36045e15720SKiyoshi Ueda /*
36145e15720SKiyoshi Ueda  * Check whether bios must be queued in the device-mapper core rather
36245e15720SKiyoshi Ueda  * than here in the target.
36345e15720SKiyoshi Ueda  *
36445e15720SKiyoshi Ueda  * m->lock must be held on entry.
36545e15720SKiyoshi Ueda  *
36645e15720SKiyoshi Ueda  * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
36745e15720SKiyoshi Ueda  * same value then we are not between multipath_presuspend()
36845e15720SKiyoshi Ueda  * and multipath_resume() calls and we have no need to check
36945e15720SKiyoshi Ueda  * for the DMF_NOFLUSH_SUSPENDING flag.
37045e15720SKiyoshi Ueda  */
37145e15720SKiyoshi Ueda static int __must_push_back(struct multipath *m)
37245e15720SKiyoshi Ueda {
373e8099177SHannes Reinecke 	return (m->queue_if_no_path ||
374e8099177SHannes Reinecke 		(m->queue_if_no_path != m->saved_queue_if_no_path &&
375e8099177SHannes Reinecke 		 dm_noflush_suspending(m->ti)));
37645e15720SKiyoshi Ueda }
37745e15720SKiyoshi Ueda 
37836fcffccSHannes Reinecke /*
37936fcffccSHannes Reinecke  * Map cloned requests
38036fcffccSHannes Reinecke  */
38136fcffccSHannes Reinecke static int multipath_map(struct dm_target *ti, struct request *clone,
382e8099177SHannes Reinecke 			 union map_info *map_context)
3831da177e4SLinus Torvalds {
38436fcffccSHannes Reinecke 	struct multipath *m = (struct multipath *) ti->private;
385e3bde04fSHannes Reinecke 	int r = DM_MAPIO_REQUEUE;
386f40c67f0SKiyoshi Ueda 	size_t nr_bytes = blk_rq_bytes(clone);
3871da177e4SLinus Torvalds 	struct pgpath *pgpath;
388f40c67f0SKiyoshi Ueda 	struct block_device *bdev;
389e3bde04fSHannes Reinecke 	struct dm_mpath_io *mpio;
3901da177e4SLinus Torvalds 
391*2eb6e1e3SKeith Busch 	spin_lock_irq(&m->lock);
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	/* Do we need to select a new pgpath? */
3941da177e4SLinus Torvalds 	if (!m->current_pgpath ||
3951da177e4SLinus Torvalds 	    (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
39602ab823fSKiyoshi Ueda 		__choose_pgpath(m, nr_bytes);
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	pgpath = m->current_pgpath;
3991da177e4SLinus Torvalds 
4009bf59a61SMike Snitzer 	if (!pgpath) {
4019bf59a61SMike Snitzer 		if (!__must_push_back(m))
4029bf59a61SMike Snitzer 			r = -EIO;	/* Failed */
4039bf59a61SMike Snitzer 		goto out_unlock;
4046afbc01dSMike Snitzer 	} else if (m->queue_io || m->pg_init_required) {
4059bf59a61SMike Snitzer 		__pg_init_all_paths(m);
4069bf59a61SMike Snitzer 		goto out_unlock;
4079bf59a61SMike Snitzer 	}
4086afbc01dSMike Snitzer 
409e3bde04fSHannes Reinecke 	if (set_mapinfo(m, map_context) < 0)
410e3bde04fSHannes Reinecke 		/* ENOMEM, requeue */
411e3bde04fSHannes Reinecke 		goto out_unlock;
412e3bde04fSHannes Reinecke 
413e3bde04fSHannes Reinecke 	mpio = map_context->ptr;
414e8099177SHannes Reinecke 	mpio->pgpath = pgpath;
415e8099177SHannes Reinecke 	mpio->nr_bytes = nr_bytes;
416*2eb6e1e3SKeith Busch 
417*2eb6e1e3SKeith Busch 	bdev = pgpath->path.dev->bdev;
418*2eb6e1e3SKeith Busch 
419*2eb6e1e3SKeith Busch 	clone->q = bdev_get_queue(bdev);
420*2eb6e1e3SKeith Busch 	clone->rq_disk = bdev->bd_disk;
421*2eb6e1e3SKeith Busch 	clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
422*2eb6e1e3SKeith Busch 
423*2eb6e1e3SKeith Busch 	spin_unlock_irq(&m->lock);
424*2eb6e1e3SKeith Busch 
425e8099177SHannes Reinecke 	if (pgpath->pg->ps.type->start_io)
426e8099177SHannes Reinecke 		pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
427e8099177SHannes Reinecke 					      &pgpath->path,
428e8099177SHannes Reinecke 					      nr_bytes);
429*2eb6e1e3SKeith Busch 	return DM_MAPIO_REMAPPED;
4301da177e4SLinus Torvalds 
431e3bde04fSHannes Reinecke out_unlock:
432*2eb6e1e3SKeith Busch 	spin_unlock_irq(&m->lock);
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	return r;
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds /*
4381da177e4SLinus Torvalds  * If we run out of usable paths, should we queue I/O or error it?
4391da177e4SLinus Torvalds  */
440485ef69eSAlasdair G Kergon static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
441485ef69eSAlasdair G Kergon 			    unsigned save_old_value)
4421da177e4SLinus Torvalds {
4431da177e4SLinus Torvalds 	unsigned long flags;
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
4461da177e4SLinus Torvalds 
447485ef69eSAlasdair G Kergon 	if (save_old_value)
448436d4108SAlasdair G Kergon 		m->saved_queue_if_no_path = m->queue_if_no_path;
449485ef69eSAlasdair G Kergon 	else
450485ef69eSAlasdair G Kergon 		m->saved_queue_if_no_path = queue_if_no_path;
4511da177e4SLinus Torvalds 	m->queue_if_no_path = queue_if_no_path;
4521da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
4531da177e4SLinus Torvalds 
45463d832c3SHannes Reinecke 	if (!queue_if_no_path)
45563d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
45663d832c3SHannes Reinecke 
4571da177e4SLinus Torvalds 	return 0;
4581da177e4SLinus Torvalds }
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds /*
4611da177e4SLinus Torvalds  * An event is triggered whenever a path is taken out of use.
4621da177e4SLinus Torvalds  * Includes path failure and PG bypass.
4631da177e4SLinus Torvalds  */
464c4028958SDavid Howells static void trigger_event(struct work_struct *work)
4651da177e4SLinus Torvalds {
466c4028958SDavid Howells 	struct multipath *m =
467c4028958SDavid Howells 		container_of(work, struct multipath, trigger_event);
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	dm_table_event(m->ti->table);
4701da177e4SLinus Torvalds }
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds /*-----------------------------------------------------------------
4731da177e4SLinus Torvalds  * Constructor/argument parsing:
4741da177e4SLinus Torvalds  * <#multipath feature args> [<arg>]*
4751da177e4SLinus Torvalds  * <#hw_handler args> [hw_handler [<arg>]*]
4761da177e4SLinus Torvalds  * <#priority groups>
4771da177e4SLinus Torvalds  * <initial priority group>
4781da177e4SLinus Torvalds  *     [<selector> <#selector args> [<arg>]*
4791da177e4SLinus Torvalds  *      <#paths> <#per-path selector args>
4801da177e4SLinus Torvalds  *         [<path> [<arg>]* ]+ ]+
4811da177e4SLinus Torvalds  *---------------------------------------------------------------*/
482498f0103SMike Snitzer static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
4831da177e4SLinus Torvalds 			       struct dm_target *ti)
4841da177e4SLinus Torvalds {
4851da177e4SLinus Torvalds 	int r;
4861da177e4SLinus Torvalds 	struct path_selector_type *pst;
4871da177e4SLinus Torvalds 	unsigned ps_argc;
4881da177e4SLinus Torvalds 
489498f0103SMike Snitzer 	static struct dm_arg _args[] = {
49072d94861SAlasdair G Kergon 		{0, 1024, "invalid number of path selector args"},
4911da177e4SLinus Torvalds 	};
4921da177e4SLinus Torvalds 
493498f0103SMike Snitzer 	pst = dm_get_path_selector(dm_shift_arg(as));
4941da177e4SLinus Torvalds 	if (!pst) {
49572d94861SAlasdair G Kergon 		ti->error = "unknown path selector type";
4961da177e4SLinus Torvalds 		return -EINVAL;
4971da177e4SLinus Torvalds 	}
4981da177e4SLinus Torvalds 
499498f0103SMike Snitzer 	r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
500371b2e34SMikulas Patocka 	if (r) {
501371b2e34SMikulas Patocka 		dm_put_path_selector(pst);
5021da177e4SLinus Torvalds 		return -EINVAL;
503371b2e34SMikulas Patocka 	}
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	r = pst->create(&pg->ps, ps_argc, as->argv);
5061da177e4SLinus Torvalds 	if (r) {
5071da177e4SLinus Torvalds 		dm_put_path_selector(pst);
50872d94861SAlasdair G Kergon 		ti->error = "path selector constructor failed";
5091da177e4SLinus Torvalds 		return r;
5101da177e4SLinus Torvalds 	}
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	pg->ps.type = pst;
513498f0103SMike Snitzer 	dm_consume_args(as, ps_argc);
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	return 0;
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
518498f0103SMike Snitzer static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
5191da177e4SLinus Torvalds 			       struct dm_target *ti)
5201da177e4SLinus Torvalds {
5211da177e4SLinus Torvalds 	int r;
5221da177e4SLinus Torvalds 	struct pgpath *p;
523ae11b1b3SHannes Reinecke 	struct multipath *m = ti->private;
524a58a935dSMike Snitzer 	struct request_queue *q = NULL;
525a58a935dSMike Snitzer 	const char *attached_handler_name;
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	/* we need at least a path arg */
5281da177e4SLinus Torvalds 	if (as->argc < 1) {
52972d94861SAlasdair G Kergon 		ti->error = "no device given";
53001460f35SBenjamin Marzinski 		return ERR_PTR(-EINVAL);
5311da177e4SLinus Torvalds 	}
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	p = alloc_pgpath();
5341da177e4SLinus Torvalds 	if (!p)
53501460f35SBenjamin Marzinski 		return ERR_PTR(-ENOMEM);
5361da177e4SLinus Torvalds 
537498f0103SMike Snitzer 	r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
5388215d6ecSNikanth Karthikesan 			  &p->path.dev);
5391da177e4SLinus Torvalds 	if (r) {
54072d94861SAlasdair G Kergon 		ti->error = "error getting device";
5411da177e4SLinus Torvalds 		goto bad;
5421da177e4SLinus Torvalds 	}
5431da177e4SLinus Torvalds 
544a58a935dSMike Snitzer 	if (m->retain_attached_hw_handler || m->hw_handler_name)
545a58a935dSMike Snitzer 		q = bdev_get_queue(p->path.dev->bdev);
546a0cf7ea9SHannes Reinecke 
547a58a935dSMike Snitzer 	if (m->retain_attached_hw_handler) {
548a58a935dSMike Snitzer 		attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
549a58a935dSMike Snitzer 		if (attached_handler_name) {
550a58a935dSMike Snitzer 			/*
551a58a935dSMike Snitzer 			 * Reset hw_handler_name to match the attached handler
552a58a935dSMike Snitzer 			 * and clear any hw_handler_params associated with the
553a58a935dSMike Snitzer 			 * ignored handler.
554a58a935dSMike Snitzer 			 *
555a58a935dSMike Snitzer 			 * NB. This modifies the table line to show the actual
556a58a935dSMike Snitzer 			 * handler instead of the original table passed in.
557a58a935dSMike Snitzer 			 */
558a58a935dSMike Snitzer 			kfree(m->hw_handler_name);
559a58a935dSMike Snitzer 			m->hw_handler_name = attached_handler_name;
560a58a935dSMike Snitzer 
561a58a935dSMike Snitzer 			kfree(m->hw_handler_params);
562a58a935dSMike Snitzer 			m->hw_handler_params = NULL;
563a58a935dSMike Snitzer 		}
564a58a935dSMike Snitzer 	}
565a58a935dSMike Snitzer 
566a58a935dSMike Snitzer 	if (m->hw_handler_name) {
567a58a935dSMike Snitzer 		/*
568a58a935dSMike Snitzer 		 * Increments scsi_dh reference, even when using an
569a58a935dSMike Snitzer 		 * already-attached handler.
570a58a935dSMike Snitzer 		 */
571a0cf7ea9SHannes Reinecke 		r = scsi_dh_attach(q, m->hw_handler_name);
572a0cf7ea9SHannes Reinecke 		if (r == -EBUSY) {
573a0cf7ea9SHannes Reinecke 			/*
574a58a935dSMike Snitzer 			 * Already attached to different hw_handler:
575a0cf7ea9SHannes Reinecke 			 * try to reattach with correct one.
576a0cf7ea9SHannes Reinecke 			 */
577a0cf7ea9SHannes Reinecke 			scsi_dh_detach(q);
578a0cf7ea9SHannes Reinecke 			r = scsi_dh_attach(q, m->hw_handler_name);
579a0cf7ea9SHannes Reinecke 		}
580a0cf7ea9SHannes Reinecke 
581ae11b1b3SHannes Reinecke 		if (r < 0) {
582a0cf7ea9SHannes Reinecke 			ti->error = "error attaching hardware handler";
583ae11b1b3SHannes Reinecke 			dm_put_device(ti, p->path.dev);
584ae11b1b3SHannes Reinecke 			goto bad;
585ae11b1b3SHannes Reinecke 		}
5862bfd2e13SChandra Seetharaman 
5872bfd2e13SChandra Seetharaman 		if (m->hw_handler_params) {
5882bfd2e13SChandra Seetharaman 			r = scsi_dh_set_params(q, m->hw_handler_params);
5892bfd2e13SChandra Seetharaman 			if (r < 0) {
5902bfd2e13SChandra Seetharaman 				ti->error = "unable to set hardware "
5912bfd2e13SChandra Seetharaman 							"handler parameters";
5922bfd2e13SChandra Seetharaman 				scsi_dh_detach(q);
5932bfd2e13SChandra Seetharaman 				dm_put_device(ti, p->path.dev);
5942bfd2e13SChandra Seetharaman 				goto bad;
5952bfd2e13SChandra Seetharaman 			}
5962bfd2e13SChandra Seetharaman 		}
597ae11b1b3SHannes Reinecke 	}
598ae11b1b3SHannes Reinecke 
5991da177e4SLinus Torvalds 	r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
6001da177e4SLinus Torvalds 	if (r) {
6011da177e4SLinus Torvalds 		dm_put_device(ti, p->path.dev);
6021da177e4SLinus Torvalds 		goto bad;
6031da177e4SLinus Torvalds 	}
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	return p;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds  bad:
6081da177e4SLinus Torvalds 	free_pgpath(p);
60901460f35SBenjamin Marzinski 	return ERR_PTR(r);
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds 
612498f0103SMike Snitzer static struct priority_group *parse_priority_group(struct dm_arg_set *as,
61328f16c20SMicha³ Miros³aw 						   struct multipath *m)
6141da177e4SLinus Torvalds {
615498f0103SMike Snitzer 	static struct dm_arg _args[] = {
61672d94861SAlasdair G Kergon 		{1, 1024, "invalid number of paths"},
61772d94861SAlasdair G Kergon 		{0, 1024, "invalid number of selector args"}
6181da177e4SLinus Torvalds 	};
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	int r;
621498f0103SMike Snitzer 	unsigned i, nr_selector_args, nr_args;
6221da177e4SLinus Torvalds 	struct priority_group *pg;
62328f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	if (as->argc < 2) {
6261da177e4SLinus Torvalds 		as->argc = 0;
62701460f35SBenjamin Marzinski 		ti->error = "not enough priority group arguments";
62801460f35SBenjamin Marzinski 		return ERR_PTR(-EINVAL);
6291da177e4SLinus Torvalds 	}
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	pg = alloc_priority_group();
6321da177e4SLinus Torvalds 	if (!pg) {
63372d94861SAlasdair G Kergon 		ti->error = "couldn't allocate priority group";
63401460f35SBenjamin Marzinski 		return ERR_PTR(-ENOMEM);
6351da177e4SLinus Torvalds 	}
6361da177e4SLinus Torvalds 	pg->m = m;
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	r = parse_path_selector(as, pg, ti);
6391da177e4SLinus Torvalds 	if (r)
6401da177e4SLinus Torvalds 		goto bad;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	/*
6431da177e4SLinus Torvalds 	 * read the paths
6441da177e4SLinus Torvalds 	 */
645498f0103SMike Snitzer 	r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
6461da177e4SLinus Torvalds 	if (r)
6471da177e4SLinus Torvalds 		goto bad;
6481da177e4SLinus Torvalds 
649498f0103SMike Snitzer 	r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
6501da177e4SLinus Torvalds 	if (r)
6511da177e4SLinus Torvalds 		goto bad;
6521da177e4SLinus Torvalds 
653498f0103SMike Snitzer 	nr_args = 1 + nr_selector_args;
6541da177e4SLinus Torvalds 	for (i = 0; i < pg->nr_pgpaths; i++) {
6551da177e4SLinus Torvalds 		struct pgpath *pgpath;
656498f0103SMike Snitzer 		struct dm_arg_set path_args;
6571da177e4SLinus Torvalds 
658498f0103SMike Snitzer 		if (as->argc < nr_args) {
659148acff6SMikulas Patocka 			ti->error = "not enough path parameters";
6606bbf79a1SAlasdair G Kergon 			r = -EINVAL;
6611da177e4SLinus Torvalds 			goto bad;
662148acff6SMikulas Patocka 		}
6631da177e4SLinus Torvalds 
664498f0103SMike Snitzer 		path_args.argc = nr_args;
6651da177e4SLinus Torvalds 		path_args.argv = as->argv;
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 		pgpath = parse_path(&path_args, &pg->ps, ti);
66801460f35SBenjamin Marzinski 		if (IS_ERR(pgpath)) {
66901460f35SBenjamin Marzinski 			r = PTR_ERR(pgpath);
6701da177e4SLinus Torvalds 			goto bad;
67101460f35SBenjamin Marzinski 		}
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 		pgpath->pg = pg;
6741da177e4SLinus Torvalds 		list_add_tail(&pgpath->list, &pg->pgpaths);
675498f0103SMike Snitzer 		dm_consume_args(as, nr_args);
6761da177e4SLinus Torvalds 	}
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	return pg;
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds  bad:
6811da177e4SLinus Torvalds 	free_priority_group(pg, ti);
68201460f35SBenjamin Marzinski 	return ERR_PTR(r);
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds 
685498f0103SMike Snitzer static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
6861da177e4SLinus Torvalds {
6871da177e4SLinus Torvalds 	unsigned hw_argc;
6882bfd2e13SChandra Seetharaman 	int ret;
68928f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
6901da177e4SLinus Torvalds 
691498f0103SMike Snitzer 	static struct dm_arg _args[] = {
69272d94861SAlasdair G Kergon 		{0, 1024, "invalid number of hardware handler args"},
6931da177e4SLinus Torvalds 	};
6941da177e4SLinus Torvalds 
695498f0103SMike Snitzer 	if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
6961da177e4SLinus Torvalds 		return -EINVAL;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	if (!hw_argc)
6991da177e4SLinus Torvalds 		return 0;
7001da177e4SLinus Torvalds 
701498f0103SMike Snitzer 	m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
702510193a2SMike Snitzer 	if (!try_then_request_module(scsi_dh_handler_exist(m->hw_handler_name),
703510193a2SMike Snitzer 				     "scsi_dh_%s", m->hw_handler_name)) {
70472d94861SAlasdair G Kergon 		ti->error = "unknown hardware handler type";
7052bfd2e13SChandra Seetharaman 		ret = -EINVAL;
7062bfd2e13SChandra Seetharaman 		goto fail;
7071da177e4SLinus Torvalds 	}
70814e98c5cSChandra Seetharaman 
7092bfd2e13SChandra Seetharaman 	if (hw_argc > 1) {
7102bfd2e13SChandra Seetharaman 		char *p;
7112bfd2e13SChandra Seetharaman 		int i, j, len = 4;
7122bfd2e13SChandra Seetharaman 
7132bfd2e13SChandra Seetharaman 		for (i = 0; i <= hw_argc - 2; i++)
7142bfd2e13SChandra Seetharaman 			len += strlen(as->argv[i]) + 1;
7152bfd2e13SChandra Seetharaman 		p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
7162bfd2e13SChandra Seetharaman 		if (!p) {
7172bfd2e13SChandra Seetharaman 			ti->error = "memory allocation failed";
7182bfd2e13SChandra Seetharaman 			ret = -ENOMEM;
7192bfd2e13SChandra Seetharaman 			goto fail;
7202bfd2e13SChandra Seetharaman 		}
7212bfd2e13SChandra Seetharaman 		j = sprintf(p, "%d", hw_argc - 1);
7222bfd2e13SChandra Seetharaman 		for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
7232bfd2e13SChandra Seetharaman 			j = sprintf(p, "%s", as->argv[i]);
7242bfd2e13SChandra Seetharaman 	}
725498f0103SMike Snitzer 	dm_consume_args(as, hw_argc - 1);
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 	return 0;
7282bfd2e13SChandra Seetharaman fail:
7292bfd2e13SChandra Seetharaman 	kfree(m->hw_handler_name);
7302bfd2e13SChandra Seetharaman 	m->hw_handler_name = NULL;
7312bfd2e13SChandra Seetharaman 	return ret;
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
734498f0103SMike Snitzer static int parse_features(struct dm_arg_set *as, struct multipath *m)
7351da177e4SLinus Torvalds {
7361da177e4SLinus Torvalds 	int r;
7371da177e4SLinus Torvalds 	unsigned argc;
73828f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
739498f0103SMike Snitzer 	const char *arg_name;
7401da177e4SLinus Torvalds 
741498f0103SMike Snitzer 	static struct dm_arg _args[] = {
742a58a935dSMike Snitzer 		{0, 6, "invalid number of feature args"},
743c9e45581SDave Wysochanski 		{1, 50, "pg_init_retries must be between 1 and 50"},
7444e2d19e4SChandra Seetharaman 		{0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
7451da177e4SLinus Torvalds 	};
7461da177e4SLinus Torvalds 
747498f0103SMike Snitzer 	r = dm_read_arg_group(_args, as, &argc, &ti->error);
7481da177e4SLinus Torvalds 	if (r)
7491da177e4SLinus Torvalds 		return -EINVAL;
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	if (!argc)
7521da177e4SLinus Torvalds 		return 0;
7531da177e4SLinus Torvalds 
754c9e45581SDave Wysochanski 	do {
755498f0103SMike Snitzer 		arg_name = dm_shift_arg(as);
756c9e45581SDave Wysochanski 		argc--;
757c9e45581SDave Wysochanski 
758498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "queue_if_no_path")) {
759c9e45581SDave Wysochanski 			r = queue_if_no_path(m, 1, 0);
760c9e45581SDave Wysochanski 			continue;
7611da177e4SLinus Torvalds 		}
762c9e45581SDave Wysochanski 
763a58a935dSMike Snitzer 		if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
764a58a935dSMike Snitzer 			m->retain_attached_hw_handler = 1;
765a58a935dSMike Snitzer 			continue;
766a58a935dSMike Snitzer 		}
767a58a935dSMike Snitzer 
768498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "pg_init_retries") &&
769c9e45581SDave Wysochanski 		    (argc >= 1)) {
770498f0103SMike Snitzer 			r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
771c9e45581SDave Wysochanski 			argc--;
772c9e45581SDave Wysochanski 			continue;
773c9e45581SDave Wysochanski 		}
774c9e45581SDave Wysochanski 
775498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
7764e2d19e4SChandra Seetharaman 		    (argc >= 1)) {
777498f0103SMike Snitzer 			r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
7784e2d19e4SChandra Seetharaman 			argc--;
7794e2d19e4SChandra Seetharaman 			continue;
7804e2d19e4SChandra Seetharaman 		}
7814e2d19e4SChandra Seetharaman 
782c9e45581SDave Wysochanski 		ti->error = "Unrecognised multipath feature request";
783c9e45581SDave Wysochanski 		r = -EINVAL;
784c9e45581SDave Wysochanski 	} while (argc && !r);
785c9e45581SDave Wysochanski 
786c9e45581SDave Wysochanski 	return r;
7871da177e4SLinus Torvalds }
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds static int multipath_ctr(struct dm_target *ti, unsigned int argc,
7901da177e4SLinus Torvalds 			 char **argv)
7911da177e4SLinus Torvalds {
792498f0103SMike Snitzer 	/* target arguments */
793498f0103SMike Snitzer 	static struct dm_arg _args[] = {
794a490a07aSMike Snitzer 		{0, 1024, "invalid number of priority groups"},
795a490a07aSMike Snitzer 		{0, 1024, "invalid initial priority group number"},
7961da177e4SLinus Torvalds 	};
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	int r;
7991da177e4SLinus Torvalds 	struct multipath *m;
800498f0103SMike Snitzer 	struct dm_arg_set as;
8011da177e4SLinus Torvalds 	unsigned pg_count = 0;
8021da177e4SLinus Torvalds 	unsigned next_pg_num;
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	as.argc = argc;
8051da177e4SLinus Torvalds 	as.argv = argv;
8061da177e4SLinus Torvalds 
80728f16c20SMicha³ Miros³aw 	m = alloc_multipath(ti);
8081da177e4SLinus Torvalds 	if (!m) {
80972d94861SAlasdair G Kergon 		ti->error = "can't allocate multipath";
8101da177e4SLinus Torvalds 		return -EINVAL;
8111da177e4SLinus Torvalds 	}
8121da177e4SLinus Torvalds 
81328f16c20SMicha³ Miros³aw 	r = parse_features(&as, m);
8141da177e4SLinus Torvalds 	if (r)
8151da177e4SLinus Torvalds 		goto bad;
8161da177e4SLinus Torvalds 
81728f16c20SMicha³ Miros³aw 	r = parse_hw_handler(&as, m);
8181da177e4SLinus Torvalds 	if (r)
8191da177e4SLinus Torvalds 		goto bad;
8201da177e4SLinus Torvalds 
821498f0103SMike Snitzer 	r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
8221da177e4SLinus Torvalds 	if (r)
8231da177e4SLinus Torvalds 		goto bad;
8241da177e4SLinus Torvalds 
825498f0103SMike Snitzer 	r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
8261da177e4SLinus Torvalds 	if (r)
8271da177e4SLinus Torvalds 		goto bad;
8281da177e4SLinus Torvalds 
829a490a07aSMike Snitzer 	if ((!m->nr_priority_groups && next_pg_num) ||
830a490a07aSMike Snitzer 	    (m->nr_priority_groups && !next_pg_num)) {
831a490a07aSMike Snitzer 		ti->error = "invalid initial priority group";
832a490a07aSMike Snitzer 		r = -EINVAL;
833a490a07aSMike Snitzer 		goto bad;
834a490a07aSMike Snitzer 	}
835a490a07aSMike Snitzer 
8361da177e4SLinus Torvalds 	/* parse the priority groups */
8371da177e4SLinus Torvalds 	while (as.argc) {
8381da177e4SLinus Torvalds 		struct priority_group *pg;
8391da177e4SLinus Torvalds 
84028f16c20SMicha³ Miros³aw 		pg = parse_priority_group(&as, m);
84101460f35SBenjamin Marzinski 		if (IS_ERR(pg)) {
84201460f35SBenjamin Marzinski 			r = PTR_ERR(pg);
8431da177e4SLinus Torvalds 			goto bad;
8441da177e4SLinus Torvalds 		}
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 		m->nr_valid_paths += pg->nr_pgpaths;
8471da177e4SLinus Torvalds 		list_add_tail(&pg->list, &m->priority_groups);
8481da177e4SLinus Torvalds 		pg_count++;
8491da177e4SLinus Torvalds 		pg->pg_num = pg_count;
8501da177e4SLinus Torvalds 		if (!--next_pg_num)
8511da177e4SLinus Torvalds 			m->next_pg = pg;
8521da177e4SLinus Torvalds 	}
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	if (pg_count != m->nr_priority_groups) {
85572d94861SAlasdair G Kergon 		ti->error = "priority group count mismatch";
8561da177e4SLinus Torvalds 		r = -EINVAL;
8571da177e4SLinus Torvalds 		goto bad;
8581da177e4SLinus Torvalds 	}
8591da177e4SLinus Torvalds 
86055a62eefSAlasdair G Kergon 	ti->num_flush_bios = 1;
86155a62eefSAlasdair G Kergon 	ti->num_discard_bios = 1;
862042bcef8SMike Snitzer 	ti->num_write_same_bios = 1;
8638627921fSMikulas Patocka 
8641da177e4SLinus Torvalds 	return 0;
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds  bad:
8671da177e4SLinus Torvalds 	free_multipath(m);
8681da177e4SLinus Torvalds 	return r;
8691da177e4SLinus Torvalds }
8701da177e4SLinus Torvalds 
8712bded7bdSKiyoshi Ueda static void multipath_wait_for_pg_init_completion(struct multipath *m)
8722bded7bdSKiyoshi Ueda {
8732bded7bdSKiyoshi Ueda 	DECLARE_WAITQUEUE(wait, current);
8742bded7bdSKiyoshi Ueda 	unsigned long flags;
8752bded7bdSKiyoshi Ueda 
8762bded7bdSKiyoshi Ueda 	add_wait_queue(&m->pg_init_wait, &wait);
8772bded7bdSKiyoshi Ueda 
8782bded7bdSKiyoshi Ueda 	while (1) {
8792bded7bdSKiyoshi Ueda 		set_current_state(TASK_UNINTERRUPTIBLE);
8802bded7bdSKiyoshi Ueda 
8812bded7bdSKiyoshi Ueda 		spin_lock_irqsave(&m->lock, flags);
8822bded7bdSKiyoshi Ueda 		if (!m->pg_init_in_progress) {
8832bded7bdSKiyoshi Ueda 			spin_unlock_irqrestore(&m->lock, flags);
8842bded7bdSKiyoshi Ueda 			break;
8852bded7bdSKiyoshi Ueda 		}
8862bded7bdSKiyoshi Ueda 		spin_unlock_irqrestore(&m->lock, flags);
8872bded7bdSKiyoshi Ueda 
8882bded7bdSKiyoshi Ueda 		io_schedule();
8892bded7bdSKiyoshi Ueda 	}
8902bded7bdSKiyoshi Ueda 	set_current_state(TASK_RUNNING);
8912bded7bdSKiyoshi Ueda 
8922bded7bdSKiyoshi Ueda 	remove_wait_queue(&m->pg_init_wait, &wait);
8932bded7bdSKiyoshi Ueda }
8942bded7bdSKiyoshi Ueda 
8952bded7bdSKiyoshi Ueda static void flush_multipath_work(struct multipath *m)
8961da177e4SLinus Torvalds {
897954a73d5SShiva Krishna Merla 	unsigned long flags;
898954a73d5SShiva Krishna Merla 
899954a73d5SShiva Krishna Merla 	spin_lock_irqsave(&m->lock, flags);
900954a73d5SShiva Krishna Merla 	m->pg_init_disabled = 1;
901954a73d5SShiva Krishna Merla 	spin_unlock_irqrestore(&m->lock, flags);
902954a73d5SShiva Krishna Merla 
903bab7cfc7SChandra Seetharaman 	flush_workqueue(kmpath_handlerd);
9042bded7bdSKiyoshi Ueda 	multipath_wait_for_pg_init_completion(m);
905a044d016SAlasdair G Kergon 	flush_workqueue(kmultipathd);
90643829731STejun Heo 	flush_work(&m->trigger_event);
907954a73d5SShiva Krishna Merla 
908954a73d5SShiva Krishna Merla 	spin_lock_irqsave(&m->lock, flags);
909954a73d5SShiva Krishna Merla 	m->pg_init_disabled = 0;
910954a73d5SShiva Krishna Merla 	spin_unlock_irqrestore(&m->lock, flags);
9116df400abSKiyoshi Ueda }
9126df400abSKiyoshi Ueda 
9136df400abSKiyoshi Ueda static void multipath_dtr(struct dm_target *ti)
9146df400abSKiyoshi Ueda {
9156df400abSKiyoshi Ueda 	struct multipath *m = ti->private;
9166df400abSKiyoshi Ueda 
9172bded7bdSKiyoshi Ueda 	flush_multipath_work(m);
9181da177e4SLinus Torvalds 	free_multipath(m);
9191da177e4SLinus Torvalds }
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds /*
9221da177e4SLinus Torvalds  * Take a path out of use.
9231da177e4SLinus Torvalds  */
9241da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath)
9251da177e4SLinus Torvalds {
9261da177e4SLinus Torvalds 	unsigned long flags;
9271da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
9301da177e4SLinus Torvalds 
9316680073dSKiyoshi Ueda 	if (!pgpath->is_active)
9321da177e4SLinus Torvalds 		goto out;
9331da177e4SLinus Torvalds 
93472d94861SAlasdair G Kergon 	DMWARN("Failing path %s.", pgpath->path.dev->name);
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
9376680073dSKiyoshi Ueda 	pgpath->is_active = 0;
9381da177e4SLinus Torvalds 	pgpath->fail_count++;
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 	m->nr_valid_paths--;
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 	if (pgpath == m->current_pgpath)
9431da177e4SLinus Torvalds 		m->current_pgpath = NULL;
9441da177e4SLinus Torvalds 
945b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
946b15546f9SMike Anderson 		      pgpath->path.dev->name, m->nr_valid_paths);
947b15546f9SMike Anderson 
948fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds out:
9511da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	return 0;
9541da177e4SLinus Torvalds }
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds /*
9571da177e4SLinus Torvalds  * Reinstate a previously-failed path
9581da177e4SLinus Torvalds  */
9591da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath)
9601da177e4SLinus Torvalds {
96163d832c3SHannes Reinecke 	int r = 0, run_queue = 0;
9621da177e4SLinus Torvalds 	unsigned long flags;
9631da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
9661da177e4SLinus Torvalds 
9676680073dSKiyoshi Ueda 	if (pgpath->is_active)
9681da177e4SLinus Torvalds 		goto out;
9691da177e4SLinus Torvalds 
970def052d2SAlasdair G Kergon 	if (!pgpath->pg->ps.type->reinstate_path) {
9711da177e4SLinus Torvalds 		DMWARN("Reinstate path not supported by path selector %s",
9721da177e4SLinus Torvalds 		       pgpath->pg->ps.type->name);
9731da177e4SLinus Torvalds 		r = -EINVAL;
9741da177e4SLinus Torvalds 		goto out;
9751da177e4SLinus Torvalds 	}
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
9781da177e4SLinus Torvalds 	if (r)
9791da177e4SLinus Torvalds 		goto out;
9801da177e4SLinus Torvalds 
9816680073dSKiyoshi Ueda 	pgpath->is_active = 1;
9821da177e4SLinus Torvalds 
983e8099177SHannes Reinecke 	if (!m->nr_valid_paths++) {
9841da177e4SLinus Torvalds 		m->current_pgpath = NULL;
98563d832c3SHannes Reinecke 		run_queue = 1;
986e54f77ddSChandra Seetharaman 	} else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
9874e2d19e4SChandra Seetharaman 		if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
988e54f77ddSChandra Seetharaman 			m->pg_init_in_progress++;
989e54f77ddSChandra Seetharaman 	}
9901da177e4SLinus Torvalds 
991b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
992b15546f9SMike Anderson 		      pgpath->path.dev->name, m->nr_valid_paths);
993b15546f9SMike Anderson 
994fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds out:
9971da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
99863d832c3SHannes Reinecke 	if (run_queue)
99963d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	return r;
10021da177e4SLinus Torvalds }
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds /*
10051da177e4SLinus Torvalds  * Fail or reinstate all paths that match the provided struct dm_dev.
10061da177e4SLinus Torvalds  */
10071da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev,
10081da177e4SLinus Torvalds 		      action_fn action)
10091da177e4SLinus Torvalds {
101019040c0bSMike Snitzer 	int r = -EINVAL;
10111da177e4SLinus Torvalds 	struct pgpath *pgpath;
10121da177e4SLinus Torvalds 	struct priority_group *pg;
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
10151da177e4SLinus Torvalds 		list_for_each_entry(pgpath, &pg->pgpaths, list) {
10161da177e4SLinus Torvalds 			if (pgpath->path.dev == dev)
10171da177e4SLinus Torvalds 				r = action(pgpath);
10181da177e4SLinus Torvalds 		}
10191da177e4SLinus Torvalds 	}
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	return r;
10221da177e4SLinus Torvalds }
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds /*
10251da177e4SLinus Torvalds  * Temporarily try to avoid having to use the specified PG
10261da177e4SLinus Torvalds  */
10271da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg,
10281da177e4SLinus Torvalds 		      int bypassed)
10291da177e4SLinus Torvalds {
10301da177e4SLinus Torvalds 	unsigned long flags;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds 	pg->bypassed = bypassed;
10351da177e4SLinus Torvalds 	m->current_pgpath = NULL;
10361da177e4SLinus Torvalds 	m->current_pg = NULL;
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
10391da177e4SLinus Torvalds 
1040fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
10411da177e4SLinus Torvalds }
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds /*
10441da177e4SLinus Torvalds  * Switch to using the specified PG from the next I/O that gets mapped
10451da177e4SLinus Torvalds  */
10461da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr)
10471da177e4SLinus Torvalds {
10481da177e4SLinus Torvalds 	struct priority_group *pg;
10491da177e4SLinus Torvalds 	unsigned pgnum;
10501da177e4SLinus Torvalds 	unsigned long flags;
105131998ef1SMikulas Patocka 	char dummy;
10521da177e4SLinus Torvalds 
105331998ef1SMikulas Patocka 	if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
10541da177e4SLinus Torvalds 	    (pgnum > m->nr_priority_groups)) {
10551da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to switch_pg_num");
10561da177e4SLinus Torvalds 		return -EINVAL;
10571da177e4SLinus Torvalds 	}
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
10601da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
10611da177e4SLinus Torvalds 		pg->bypassed = 0;
10621da177e4SLinus Torvalds 		if (--pgnum)
10631da177e4SLinus Torvalds 			continue;
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 		m->current_pgpath = NULL;
10661da177e4SLinus Torvalds 		m->current_pg = NULL;
10671da177e4SLinus Torvalds 		m->next_pg = pg;
10681da177e4SLinus Torvalds 	}
10691da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
10701da177e4SLinus Torvalds 
1071fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
10721da177e4SLinus Torvalds 	return 0;
10731da177e4SLinus Torvalds }
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds /*
10761da177e4SLinus Torvalds  * Set/clear bypassed status of a PG.
10771da177e4SLinus Torvalds  * PGs are numbered upwards from 1 in the order they were declared.
10781da177e4SLinus Torvalds  */
10791da177e4SLinus Torvalds static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
10801da177e4SLinus Torvalds {
10811da177e4SLinus Torvalds 	struct priority_group *pg;
10821da177e4SLinus Torvalds 	unsigned pgnum;
108331998ef1SMikulas Patocka 	char dummy;
10841da177e4SLinus Torvalds 
108531998ef1SMikulas Patocka 	if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
10861da177e4SLinus Torvalds 	    (pgnum > m->nr_priority_groups)) {
10871da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to bypass_pg");
10881da177e4SLinus Torvalds 		return -EINVAL;
10891da177e4SLinus Torvalds 	}
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
10921da177e4SLinus Torvalds 		if (!--pgnum)
10931da177e4SLinus Torvalds 			break;
10941da177e4SLinus Torvalds 	}
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 	bypass_pg(m, pg, bypassed);
10971da177e4SLinus Torvalds 	return 0;
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds /*
1101c9e45581SDave Wysochanski  * Should we retry pg_init immediately?
1102c9e45581SDave Wysochanski  */
1103c9e45581SDave Wysochanski static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1104c9e45581SDave Wysochanski {
1105c9e45581SDave Wysochanski 	unsigned long flags;
1106c9e45581SDave Wysochanski 	int limit_reached = 0;
1107c9e45581SDave Wysochanski 
1108c9e45581SDave Wysochanski 	spin_lock_irqsave(&m->lock, flags);
1109c9e45581SDave Wysochanski 
1110954a73d5SShiva Krishna Merla 	if (m->pg_init_count <= m->pg_init_retries && !m->pg_init_disabled)
1111c9e45581SDave Wysochanski 		m->pg_init_required = 1;
1112c9e45581SDave Wysochanski 	else
1113c9e45581SDave Wysochanski 		limit_reached = 1;
1114c9e45581SDave Wysochanski 
1115c9e45581SDave Wysochanski 	spin_unlock_irqrestore(&m->lock, flags);
1116c9e45581SDave Wysochanski 
1117c9e45581SDave Wysochanski 	return limit_reached;
1118c9e45581SDave Wysochanski }
1119c9e45581SDave Wysochanski 
11203ae31f6aSChandra Seetharaman static void pg_init_done(void *data, int errors)
1121cfae5c9bSChandra Seetharaman {
112283c0d5d5SMoger, Babu 	struct pgpath *pgpath = data;
1123cfae5c9bSChandra Seetharaman 	struct priority_group *pg = pgpath->pg;
1124cfae5c9bSChandra Seetharaman 	struct multipath *m = pg->m;
1125cfae5c9bSChandra Seetharaman 	unsigned long flags;
11264e2d19e4SChandra Seetharaman 	unsigned delay_retry = 0;
1127cfae5c9bSChandra Seetharaman 
1128cfae5c9bSChandra Seetharaman 	/* device or driver problems */
1129cfae5c9bSChandra Seetharaman 	switch (errors) {
1130cfae5c9bSChandra Seetharaman 	case SCSI_DH_OK:
1131cfae5c9bSChandra Seetharaman 		break;
1132cfae5c9bSChandra Seetharaman 	case SCSI_DH_NOSYS:
1133cfae5c9bSChandra Seetharaman 		if (!m->hw_handler_name) {
1134cfae5c9bSChandra Seetharaman 			errors = 0;
1135cfae5c9bSChandra Seetharaman 			break;
1136cfae5c9bSChandra Seetharaman 		}
1137f7b934c8SMoger, Babu 		DMERR("Could not failover the device: Handler scsi_dh_%s "
1138f7b934c8SMoger, Babu 		      "Error %d.", m->hw_handler_name, errors);
1139cfae5c9bSChandra Seetharaman 		/*
1140cfae5c9bSChandra Seetharaman 		 * Fail path for now, so we do not ping pong
1141cfae5c9bSChandra Seetharaman 		 */
1142cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1143cfae5c9bSChandra Seetharaman 		break;
1144cfae5c9bSChandra Seetharaman 	case SCSI_DH_DEV_TEMP_BUSY:
1145cfae5c9bSChandra Seetharaman 		/*
1146cfae5c9bSChandra Seetharaman 		 * Probably doing something like FW upgrade on the
1147cfae5c9bSChandra Seetharaman 		 * controller so try the other pg.
1148cfae5c9bSChandra Seetharaman 		 */
1149cfae5c9bSChandra Seetharaman 		bypass_pg(m, pg, 1);
1150cfae5c9bSChandra Seetharaman 		break;
1151cfae5c9bSChandra Seetharaman 	case SCSI_DH_RETRY:
11524e2d19e4SChandra Seetharaman 		/* Wait before retrying. */
11534e2d19e4SChandra Seetharaman 		delay_retry = 1;
1154cfae5c9bSChandra Seetharaman 	case SCSI_DH_IMM_RETRY:
1155cfae5c9bSChandra Seetharaman 	case SCSI_DH_RES_TEMP_UNAVAIL:
1156cfae5c9bSChandra Seetharaman 		if (pg_init_limit_reached(m, pgpath))
1157cfae5c9bSChandra Seetharaman 			fail_path(pgpath);
1158cfae5c9bSChandra Seetharaman 		errors = 0;
1159cfae5c9bSChandra Seetharaman 		break;
1160cfae5c9bSChandra Seetharaman 	default:
1161cfae5c9bSChandra Seetharaman 		/*
1162cfae5c9bSChandra Seetharaman 		 * We probably do not want to fail the path for a device
1163cfae5c9bSChandra Seetharaman 		 * error, but this is what the old dm did. In future
1164cfae5c9bSChandra Seetharaman 		 * patches we can do more advanced handling.
1165cfae5c9bSChandra Seetharaman 		 */
1166cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1167cfae5c9bSChandra Seetharaman 	}
1168cfae5c9bSChandra Seetharaman 
1169cfae5c9bSChandra Seetharaman 	spin_lock_irqsave(&m->lock, flags);
1170cfae5c9bSChandra Seetharaman 	if (errors) {
1171e54f77ddSChandra Seetharaman 		if (pgpath == m->current_pgpath) {
1172cfae5c9bSChandra Seetharaman 			DMERR("Could not failover device. Error %d.", errors);
1173cfae5c9bSChandra Seetharaman 			m->current_pgpath = NULL;
1174cfae5c9bSChandra Seetharaman 			m->current_pg = NULL;
1175e54f77ddSChandra Seetharaman 		}
1176d0259bf0SKiyoshi Ueda 	} else if (!m->pg_init_required)
1177cfae5c9bSChandra Seetharaman 		pg->bypassed = 0;
1178cfae5c9bSChandra Seetharaman 
1179d0259bf0SKiyoshi Ueda 	if (--m->pg_init_in_progress)
1180d0259bf0SKiyoshi Ueda 		/* Activations of other paths are still on going */
1181d0259bf0SKiyoshi Ueda 		goto out;
1182d0259bf0SKiyoshi Ueda 
11833e9f1be1SHannes Reinecke 	if (m->pg_init_required) {
11844e2d19e4SChandra Seetharaman 		m->pg_init_delay_retry = delay_retry;
11853e9f1be1SHannes Reinecke 		if (__pg_init_all_paths(m))
11863e9f1be1SHannes Reinecke 			goto out;
11873e9f1be1SHannes Reinecke 	}
11883e9f1be1SHannes Reinecke 	m->queue_io = 0;
1189d0259bf0SKiyoshi Ueda 
11902bded7bdSKiyoshi Ueda 	/*
11912bded7bdSKiyoshi Ueda 	 * Wake up any thread waiting to suspend.
11922bded7bdSKiyoshi Ueda 	 */
11932bded7bdSKiyoshi Ueda 	wake_up(&m->pg_init_wait);
11942bded7bdSKiyoshi Ueda 
1195d0259bf0SKiyoshi Ueda out:
1196cfae5c9bSChandra Seetharaman 	spin_unlock_irqrestore(&m->lock, flags);
1197cfae5c9bSChandra Seetharaman }
1198cfae5c9bSChandra Seetharaman 
1199bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work)
1200bab7cfc7SChandra Seetharaman {
1201e54f77ddSChandra Seetharaman 	struct pgpath *pgpath =
12024e2d19e4SChandra Seetharaman 		container_of(work, struct pgpath, activate_path.work);
1203bab7cfc7SChandra Seetharaman 
12043a017509SHannes Reinecke 	if (pgpath->is_active)
12053ae31f6aSChandra Seetharaman 		scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
120683c0d5d5SMoger, Babu 				 pg_init_done, pgpath);
12073a017509SHannes Reinecke 	else
12083a017509SHannes Reinecke 		pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
1209bab7cfc7SChandra Seetharaman }
1210bab7cfc7SChandra Seetharaman 
12117e782af5SHannes Reinecke static int noretry_error(int error)
12127e782af5SHannes Reinecke {
12137e782af5SHannes Reinecke 	switch (error) {
12147e782af5SHannes Reinecke 	case -EOPNOTSUPP:
12157e782af5SHannes Reinecke 	case -EREMOTEIO:
12167e782af5SHannes Reinecke 	case -EILSEQ:
12177e782af5SHannes Reinecke 	case -ENODATA:
1218cc9d3c38SJun'ichi Nomura 	case -ENOSPC:
12197e782af5SHannes Reinecke 		return 1;
12207e782af5SHannes Reinecke 	}
12217e782af5SHannes Reinecke 
12227e782af5SHannes Reinecke 	/* Anything else could be a path failure, so should be retried */
12237e782af5SHannes Reinecke 	return 0;
12247e782af5SHannes Reinecke }
12257e782af5SHannes Reinecke 
12261da177e4SLinus Torvalds /*
12271da177e4SLinus Torvalds  * end_io handling
12281da177e4SLinus Torvalds  */
1229f40c67f0SKiyoshi Ueda static int do_end_io(struct multipath *m, struct request *clone,
1230028867acSAlasdair G Kergon 		     int error, struct dm_mpath_io *mpio)
12311da177e4SLinus Torvalds {
1232f40c67f0SKiyoshi Ueda 	/*
1233f40c67f0SKiyoshi Ueda 	 * We don't queue any clone request inside the multipath target
1234f40c67f0SKiyoshi Ueda 	 * during end I/O handling, since those clone requests don't have
1235f40c67f0SKiyoshi Ueda 	 * bio clones.  If we queue them inside the multipath target,
1236f40c67f0SKiyoshi Ueda 	 * we need to make bio clones, that requires memory allocation.
1237f40c67f0SKiyoshi Ueda 	 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1238f40c67f0SKiyoshi Ueda 	 *  don't have bio clones.)
1239f40c67f0SKiyoshi Ueda 	 * Instead of queueing the clone request here, we queue the original
1240f40c67f0SKiyoshi Ueda 	 * request into dm core, which will remake a clone request and
1241f40c67f0SKiyoshi Ueda 	 * clone bios for it and resubmit it later.
1242f40c67f0SKiyoshi Ueda 	 */
1243f40c67f0SKiyoshi Ueda 	int r = DM_ENDIO_REQUEUE;
1244640eb3b0SStefan Bader 	unsigned long flags;
12451da177e4SLinus Torvalds 
1246f40c67f0SKiyoshi Ueda 	if (!error && !clone->errors)
12471da177e4SLinus Torvalds 		return 0;	/* I/O complete */
12481da177e4SLinus Torvalds 
12497eee4ae2SMike Snitzer 	if (noretry_error(error))
1250959eb4e5SMike Snitzer 		return error;
1251959eb4e5SMike Snitzer 
1252cfae5c9bSChandra Seetharaman 	if (mpio->pgpath)
12531da177e4SLinus Torvalds 		fail_path(mpio->pgpath);
12541da177e4SLinus Torvalds 
1255640eb3b0SStefan Bader 	spin_lock_irqsave(&m->lock, flags);
1256751b2a7dSHannes Reinecke 	if (!m->nr_valid_paths) {
1257751b2a7dSHannes Reinecke 		if (!m->queue_if_no_path) {
1258751b2a7dSHannes Reinecke 			if (!__must_push_back(m))
1259f40c67f0SKiyoshi Ueda 				r = -EIO;
1260751b2a7dSHannes Reinecke 		} else {
1261751b2a7dSHannes Reinecke 			if (error == -EBADE)
1262751b2a7dSHannes Reinecke 				r = error;
1263751b2a7dSHannes Reinecke 		}
1264751b2a7dSHannes Reinecke 	}
1265640eb3b0SStefan Bader 	spin_unlock_irqrestore(&m->lock, flags);
12661da177e4SLinus Torvalds 
1267f40c67f0SKiyoshi Ueda 	return r;
12681da177e4SLinus Torvalds }
12691da177e4SLinus Torvalds 
1270f40c67f0SKiyoshi Ueda static int multipath_end_io(struct dm_target *ti, struct request *clone,
12711da177e4SLinus Torvalds 			    int error, union map_info *map_context)
12721da177e4SLinus Torvalds {
1273028867acSAlasdair G Kergon 	struct multipath *m = ti->private;
1274028867acSAlasdair G Kergon 	struct dm_mpath_io *mpio = map_context->ptr;
1275a71a261fSWei Yongjun 	struct pgpath *pgpath;
12761da177e4SLinus Torvalds 	struct path_selector *ps;
12771da177e4SLinus Torvalds 	int r;
12781da177e4SLinus Torvalds 
1279466891f9SJun'ichi Nomura 	BUG_ON(!mpio);
1280466891f9SJun'ichi Nomura 
1281f40c67f0SKiyoshi Ueda 	r  = do_end_io(m, clone, error, mpio);
1282a71a261fSWei Yongjun 	pgpath = mpio->pgpath;
12831da177e4SLinus Torvalds 	if (pgpath) {
12841da177e4SLinus Torvalds 		ps = &pgpath->pg->ps;
12851da177e4SLinus Torvalds 		if (ps->type->end_io)
128602ab823fSKiyoshi Ueda 			ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
12871da177e4SLinus Torvalds 	}
1288466891f9SJun'ichi Nomura 	clear_mapinfo(m, map_context);
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 	return r;
12911da177e4SLinus Torvalds }
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds /*
12941da177e4SLinus Torvalds  * Suspend can't complete until all the I/O is processed so if
1295436d4108SAlasdair G Kergon  * the last path fails we must error any remaining I/O.
1296436d4108SAlasdair G Kergon  * Note that if the freeze_bdev fails while suspending, the
1297436d4108SAlasdair G Kergon  * queue_if_no_path state is lost - userspace should reset it.
12981da177e4SLinus Torvalds  */
12991da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti)
13001da177e4SLinus Torvalds {
13011da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
13021da177e4SLinus Torvalds 
1303485ef69eSAlasdair G Kergon 	queue_if_no_path(m, 0, 1);
13041da177e4SLinus Torvalds }
13051da177e4SLinus Torvalds 
13066df400abSKiyoshi Ueda static void multipath_postsuspend(struct dm_target *ti)
13076df400abSKiyoshi Ueda {
13086380f26fSMike Anderson 	struct multipath *m = ti->private;
13096380f26fSMike Anderson 
13106380f26fSMike Anderson 	mutex_lock(&m->work_mutex);
13112bded7bdSKiyoshi Ueda 	flush_multipath_work(m);
13126380f26fSMike Anderson 	mutex_unlock(&m->work_mutex);
13136df400abSKiyoshi Ueda }
13146df400abSKiyoshi Ueda 
1315436d4108SAlasdair G Kergon /*
1316436d4108SAlasdair G Kergon  * Restore the queue_if_no_path setting.
1317436d4108SAlasdair G Kergon  */
13181da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti)
13191da177e4SLinus Torvalds {
13201da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
13211da177e4SLinus Torvalds 	unsigned long flags;
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
1324436d4108SAlasdair G Kergon 	m->queue_if_no_path = m->saved_queue_if_no_path;
13251da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
13261da177e4SLinus Torvalds }
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds /*
13291da177e4SLinus Torvalds  * Info output has the following format:
13301da177e4SLinus Torvalds  * num_multipath_feature_args [multipath_feature_args]*
13311da177e4SLinus Torvalds  * num_handler_status_args [handler_status_args]*
13321da177e4SLinus Torvalds  * num_groups init_group_number
13331da177e4SLinus Torvalds  *            [A|D|E num_ps_status_args [ps_status_args]*
13341da177e4SLinus Torvalds  *             num_paths num_selector_args
13351da177e4SLinus Torvalds  *             [path_dev A|F fail_count [selector_args]* ]+ ]+
13361da177e4SLinus Torvalds  *
13371da177e4SLinus Torvalds  * Table output has the following format (identical to the constructor string):
13381da177e4SLinus Torvalds  * num_feature_args [features_args]*
13391da177e4SLinus Torvalds  * num_handler_args hw_handler [hw_handler_args]*
13401da177e4SLinus Torvalds  * num_groups init_group_number
13411da177e4SLinus Torvalds  *     [priority selector-name num_ps_args [ps_args]*
13421da177e4SLinus Torvalds  *      num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
13431da177e4SLinus Torvalds  */
1344fd7c092eSMikulas Patocka static void multipath_status(struct dm_target *ti, status_type_t type,
13451f4e0ff0SAlasdair G Kergon 			     unsigned status_flags, char *result, unsigned maxlen)
13461da177e4SLinus Torvalds {
13471da177e4SLinus Torvalds 	int sz = 0;
13481da177e4SLinus Torvalds 	unsigned long flags;
13491da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
13501da177e4SLinus Torvalds 	struct priority_group *pg;
13511da177e4SLinus Torvalds 	struct pgpath *p;
13521da177e4SLinus Torvalds 	unsigned pg_num;
13531da177e4SLinus Torvalds 	char state;
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 	/* Features */
13581da177e4SLinus Torvalds 	if (type == STATUSTYPE_INFO)
1359e8099177SHannes Reinecke 		DMEMIT("2 %u %u ", m->queue_io, m->pg_init_count);
1360c9e45581SDave Wysochanski 	else {
1361c9e45581SDave Wysochanski 		DMEMIT("%u ", m->queue_if_no_path +
13624e2d19e4SChandra Seetharaman 			      (m->pg_init_retries > 0) * 2 +
1363a58a935dSMike Snitzer 			      (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
1364a58a935dSMike Snitzer 			      m->retain_attached_hw_handler);
1365c9e45581SDave Wysochanski 		if (m->queue_if_no_path)
1366c9e45581SDave Wysochanski 			DMEMIT("queue_if_no_path ");
1367c9e45581SDave Wysochanski 		if (m->pg_init_retries)
1368c9e45581SDave Wysochanski 			DMEMIT("pg_init_retries %u ", m->pg_init_retries);
13694e2d19e4SChandra Seetharaman 		if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
13704e2d19e4SChandra Seetharaman 			DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
1371a58a935dSMike Snitzer 		if (m->retain_attached_hw_handler)
1372a58a935dSMike Snitzer 			DMEMIT("retain_attached_hw_handler ");
1373c9e45581SDave Wysochanski 	}
13741da177e4SLinus Torvalds 
1375cfae5c9bSChandra Seetharaman 	if (!m->hw_handler_name || type == STATUSTYPE_INFO)
13761da177e4SLinus Torvalds 		DMEMIT("0 ");
13771da177e4SLinus Torvalds 	else
1378cfae5c9bSChandra Seetharaman 		DMEMIT("1 %s ", m->hw_handler_name);
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	DMEMIT("%u ", m->nr_priority_groups);
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 	if (m->next_pg)
13831da177e4SLinus Torvalds 		pg_num = m->next_pg->pg_num;
13841da177e4SLinus Torvalds 	else if (m->current_pg)
13851da177e4SLinus Torvalds 		pg_num = m->current_pg->pg_num;
13861da177e4SLinus Torvalds 	else
1387a490a07aSMike Snitzer 		pg_num = (m->nr_priority_groups ? 1 : 0);
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds 	DMEMIT("%u ", pg_num);
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds 	switch (type) {
13921da177e4SLinus Torvalds 	case STATUSTYPE_INFO:
13931da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
13941da177e4SLinus Torvalds 			if (pg->bypassed)
13951da177e4SLinus Torvalds 				state = 'D';	/* Disabled */
13961da177e4SLinus Torvalds 			else if (pg == m->current_pg)
13971da177e4SLinus Torvalds 				state = 'A';	/* Currently Active */
13981da177e4SLinus Torvalds 			else
13991da177e4SLinus Torvalds 				state = 'E';	/* Enabled */
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 			DMEMIT("%c ", state);
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 			if (pg->ps.type->status)
14041da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
14051da177e4SLinus Torvalds 							  result + sz,
14061da177e4SLinus Torvalds 							  maxlen - sz);
14071da177e4SLinus Torvalds 			else
14081da177e4SLinus Torvalds 				DMEMIT("0 ");
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
14111da177e4SLinus Torvalds 			       pg->ps.type->info_args);
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
14141da177e4SLinus Torvalds 				DMEMIT("%s %s %u ", p->path.dev->name,
14156680073dSKiyoshi Ueda 				       p->is_active ? "A" : "F",
14161da177e4SLinus Torvalds 				       p->fail_count);
14171da177e4SLinus Torvalds 				if (pg->ps.type->status)
14181da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
14191da177e4SLinus Torvalds 					      &p->path, type, result + sz,
14201da177e4SLinus Torvalds 					      maxlen - sz);
14211da177e4SLinus Torvalds 			}
14221da177e4SLinus Torvalds 		}
14231da177e4SLinus Torvalds 		break;
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds 	case STATUSTYPE_TABLE:
14261da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
14271da177e4SLinus Torvalds 			DMEMIT("%s ", pg->ps.type->name);
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->table_args);
14381da177e4SLinus Torvalds 
14391da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
14401da177e4SLinus Torvalds 				DMEMIT("%s ", p->path.dev->name);
14411da177e4SLinus Torvalds 				if (pg->ps.type->status)
14421da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
14431da177e4SLinus Torvalds 					      &p->path, type, result + sz,
14441da177e4SLinus Torvalds 					      maxlen - sz);
14451da177e4SLinus Torvalds 			}
14461da177e4SLinus Torvalds 		}
14471da177e4SLinus Torvalds 		break;
14481da177e4SLinus Torvalds 	}
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
14511da177e4SLinus Torvalds }
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
14541da177e4SLinus Torvalds {
14556380f26fSMike Anderson 	int r = -EINVAL;
14561da177e4SLinus Torvalds 	struct dm_dev *dev;
14571da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
14581da177e4SLinus Torvalds 	action_fn action;
14591da177e4SLinus Torvalds 
14606380f26fSMike Anderson 	mutex_lock(&m->work_mutex);
14616380f26fSMike Anderson 
1462c2f3d24bSKiyoshi Ueda 	if (dm_suspended(ti)) {
1463c2f3d24bSKiyoshi Ueda 		r = -EBUSY;
1464c2f3d24bSKiyoshi Ueda 		goto out;
1465c2f3d24bSKiyoshi Ueda 	}
1466c2f3d24bSKiyoshi Ueda 
14671da177e4SLinus Torvalds 	if (argc == 1) {
1468498f0103SMike Snitzer 		if (!strcasecmp(argv[0], "queue_if_no_path")) {
14696380f26fSMike Anderson 			r = queue_if_no_path(m, 1, 0);
14706380f26fSMike Anderson 			goto out;
1471498f0103SMike Snitzer 		} else if (!strcasecmp(argv[0], "fail_if_no_path")) {
14726380f26fSMike Anderson 			r = queue_if_no_path(m, 0, 0);
14736380f26fSMike Anderson 			goto out;
14746380f26fSMike Anderson 		}
14751da177e4SLinus Torvalds 	}
14761da177e4SLinus Torvalds 
14776380f26fSMike Anderson 	if (argc != 2) {
1478a356e426SJose Castillo 		DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
14796380f26fSMike Anderson 		goto out;
14806380f26fSMike Anderson 	}
14811da177e4SLinus Torvalds 
1482498f0103SMike Snitzer 	if (!strcasecmp(argv[0], "disable_group")) {
14836380f26fSMike Anderson 		r = bypass_pg_num(m, argv[1], 1);
14846380f26fSMike Anderson 		goto out;
1485498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "enable_group")) {
14866380f26fSMike Anderson 		r = bypass_pg_num(m, argv[1], 0);
14876380f26fSMike Anderson 		goto out;
1488498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "switch_group")) {
14896380f26fSMike Anderson 		r = switch_pg_num(m, argv[1]);
14906380f26fSMike Anderson 		goto out;
1491498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "reinstate_path"))
14921da177e4SLinus Torvalds 		action = reinstate_path;
1493498f0103SMike Snitzer 	else if (!strcasecmp(argv[0], "fail_path"))
14941da177e4SLinus Torvalds 		action = fail_path;
14956380f26fSMike Anderson 	else {
1496a356e426SJose Castillo 		DMWARN("Unrecognised multipath message received: %s", argv[0]);
14976380f26fSMike Anderson 		goto out;
14986380f26fSMike Anderson 	}
14991da177e4SLinus Torvalds 
15008215d6ecSNikanth Karthikesan 	r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
15011da177e4SLinus Torvalds 	if (r) {
150272d94861SAlasdair G Kergon 		DMWARN("message: error getting device %s",
15031da177e4SLinus Torvalds 		       argv[1]);
15046380f26fSMike Anderson 		goto out;
15051da177e4SLinus Torvalds 	}
15061da177e4SLinus Torvalds 
15071da177e4SLinus Torvalds 	r = action_dev(m, dev, action);
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds 	dm_put_device(ti, dev);
15101da177e4SLinus Torvalds 
15116380f26fSMike Anderson out:
15126380f26fSMike Anderson 	mutex_unlock(&m->work_mutex);
15131da177e4SLinus Torvalds 	return r;
15141da177e4SLinus Torvalds }
15151da177e4SLinus Torvalds 
1516647b3d00SAl Viro static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
15179af4aa30SMilan Broz 			   unsigned long arg)
15189af4aa30SMilan Broz {
151935991652SMikulas Patocka 	struct multipath *m = ti->private;
15207ba10aa6SMike Snitzer 	struct pgpath *pgpath;
152135991652SMikulas Patocka 	struct block_device *bdev;
152235991652SMikulas Patocka 	fmode_t mode;
15239af4aa30SMilan Broz 	unsigned long flags;
152435991652SMikulas Patocka 	int r;
152535991652SMikulas Patocka 
152635991652SMikulas Patocka 	bdev = NULL;
152735991652SMikulas Patocka 	mode = 0;
152835991652SMikulas Patocka 	r = 0;
15299af4aa30SMilan Broz 
15309af4aa30SMilan Broz 	spin_lock_irqsave(&m->lock, flags);
15319af4aa30SMilan Broz 
15329af4aa30SMilan Broz 	if (!m->current_pgpath)
153302ab823fSKiyoshi Ueda 		__choose_pgpath(m, 0);
15349af4aa30SMilan Broz 
15357ba10aa6SMike Snitzer 	pgpath = m->current_pgpath;
15367ba10aa6SMike Snitzer 
15377ba10aa6SMike Snitzer 	if (pgpath) {
15387ba10aa6SMike Snitzer 		bdev = pgpath->path.dev->bdev;
15397ba10aa6SMike Snitzer 		mode = pgpath->path.dev->mode;
1540e90dae1fSMilan Broz 	}
15419af4aa30SMilan Broz 
15427ba10aa6SMike Snitzer 	if ((pgpath && m->queue_io) || (!pgpath && m->queue_if_no_path))
15436c182cd8SHannes Reinecke 		r = -ENOTCONN;
15449af4aa30SMilan Broz 	else if (!bdev)
15459af4aa30SMilan Broz 		r = -EIO;
15469af4aa30SMilan Broz 
15479af4aa30SMilan Broz 	spin_unlock_irqrestore(&m->lock, flags);
15489af4aa30SMilan Broz 
1549ec8013beSPaolo Bonzini 	/*
1550ec8013beSPaolo Bonzini 	 * Only pass ioctls through if the device sizes match exactly.
1551ec8013beSPaolo Bonzini 	 */
1552a1989b33SHannes Reinecke 	if (!bdev || ti->len != i_size_read(bdev->bd_inode) >> SECTOR_SHIFT) {
1553a1989b33SHannes Reinecke 		int err = scsi_verify_blk_ioctl(NULL, cmd);
1554a1989b33SHannes Reinecke 		if (err)
1555a1989b33SHannes Reinecke 			r = err;
1556a1989b33SHannes Reinecke 	}
1557ec8013beSPaolo Bonzini 
15583e9f1be1SHannes Reinecke 	if (r == -ENOTCONN && !fatal_signal_pending(current)) {
15593e9f1be1SHannes Reinecke 		spin_lock_irqsave(&m->lock, flags);
15603e9f1be1SHannes Reinecke 		if (!m->current_pg) {
15613e9f1be1SHannes Reinecke 			/* Path status changed, redo selection */
15623e9f1be1SHannes Reinecke 			__choose_pgpath(m, 0);
15633e9f1be1SHannes Reinecke 		}
15643e9f1be1SHannes Reinecke 		if (m->pg_init_required)
15653e9f1be1SHannes Reinecke 			__pg_init_all_paths(m);
15664cdd2ad7SMike Snitzer 		spin_unlock_irqrestore(&m->lock, flags);
156763d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
15683e9f1be1SHannes Reinecke 	}
156935991652SMikulas Patocka 
1570633a08b8SAl Viro 	return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
15719af4aa30SMilan Broz }
15729af4aa30SMilan Broz 
1573af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti,
1574af4874e0SMike Snitzer 				     iterate_devices_callout_fn fn, void *data)
1575af4874e0SMike Snitzer {
1576af4874e0SMike Snitzer 	struct multipath *m = ti->private;
1577af4874e0SMike Snitzer 	struct priority_group *pg;
1578af4874e0SMike Snitzer 	struct pgpath *p;
1579af4874e0SMike Snitzer 	int ret = 0;
1580af4874e0SMike Snitzer 
1581af4874e0SMike Snitzer 	list_for_each_entry(pg, &m->priority_groups, list) {
1582af4874e0SMike Snitzer 		list_for_each_entry(p, &pg->pgpaths, list) {
15835dea271bSMike Snitzer 			ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1584af4874e0SMike Snitzer 			if (ret)
1585af4874e0SMike Snitzer 				goto out;
1586af4874e0SMike Snitzer 		}
1587af4874e0SMike Snitzer 	}
1588af4874e0SMike Snitzer 
1589af4874e0SMike Snitzer out:
1590af4874e0SMike Snitzer 	return ret;
1591af4874e0SMike Snitzer }
1592af4874e0SMike Snitzer 
1593f40c67f0SKiyoshi Ueda static int __pgpath_busy(struct pgpath *pgpath)
1594f40c67f0SKiyoshi Ueda {
1595f40c67f0SKiyoshi Ueda 	struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1596f40c67f0SKiyoshi Ueda 
1597f40c67f0SKiyoshi Ueda 	return dm_underlying_device_busy(q);
1598f40c67f0SKiyoshi Ueda }
1599f40c67f0SKiyoshi Ueda 
1600f40c67f0SKiyoshi Ueda /*
1601f40c67f0SKiyoshi Ueda  * We return "busy", only when we can map I/Os but underlying devices
1602f40c67f0SKiyoshi Ueda  * are busy (so even if we map I/Os now, the I/Os will wait on
1603f40c67f0SKiyoshi Ueda  * the underlying queue).
1604f40c67f0SKiyoshi Ueda  * In other words, if we want to kill I/Os or queue them inside us
1605f40c67f0SKiyoshi Ueda  * due to map unavailability, we don't return "busy".  Otherwise,
1606f40c67f0SKiyoshi Ueda  * dm core won't give us the I/Os and we can't do what we want.
1607f40c67f0SKiyoshi Ueda  */
1608f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti)
1609f40c67f0SKiyoshi Ueda {
1610f40c67f0SKiyoshi Ueda 	int busy = 0, has_active = 0;
1611f40c67f0SKiyoshi Ueda 	struct multipath *m = ti->private;
1612f40c67f0SKiyoshi Ueda 	struct priority_group *pg;
1613f40c67f0SKiyoshi Ueda 	struct pgpath *pgpath;
1614f40c67f0SKiyoshi Ueda 	unsigned long flags;
1615f40c67f0SKiyoshi Ueda 
1616f40c67f0SKiyoshi Ueda 	spin_lock_irqsave(&m->lock, flags);
1617f40c67f0SKiyoshi Ueda 
16187a7a3b45SJun'ichi Nomura 	/* pg_init in progress or no paths available */
16197a7a3b45SJun'ichi Nomura 	if (m->pg_init_in_progress ||
16207a7a3b45SJun'ichi Nomura 	    (!m->nr_valid_paths && m->queue_if_no_path)) {
1621b63349a7SHannes Reinecke 		busy = 1;
1622b63349a7SHannes Reinecke 		goto out;
1623b63349a7SHannes Reinecke 	}
1624f40c67f0SKiyoshi Ueda 	/* Guess which priority_group will be used at next mapping time */
1625f40c67f0SKiyoshi Ueda 	if (unlikely(!m->current_pgpath && m->next_pg))
1626f40c67f0SKiyoshi Ueda 		pg = m->next_pg;
1627f40c67f0SKiyoshi Ueda 	else if (likely(m->current_pg))
1628f40c67f0SKiyoshi Ueda 		pg = m->current_pg;
1629f40c67f0SKiyoshi Ueda 	else
1630f40c67f0SKiyoshi Ueda 		/*
1631f40c67f0SKiyoshi Ueda 		 * We don't know which pg will be used at next mapping time.
1632f40c67f0SKiyoshi Ueda 		 * We don't call __choose_pgpath() here to avoid to trigger
1633f40c67f0SKiyoshi Ueda 		 * pg_init just by busy checking.
1634f40c67f0SKiyoshi Ueda 		 * So we don't know whether underlying devices we will be using
1635f40c67f0SKiyoshi Ueda 		 * at next mapping time are busy or not. Just try mapping.
1636f40c67f0SKiyoshi Ueda 		 */
1637f40c67f0SKiyoshi Ueda 		goto out;
1638f40c67f0SKiyoshi Ueda 
1639f40c67f0SKiyoshi Ueda 	/*
1640f40c67f0SKiyoshi Ueda 	 * If there is one non-busy active path at least, the path selector
1641f40c67f0SKiyoshi Ueda 	 * will be able to select it. So we consider such a pg as not busy.
1642f40c67f0SKiyoshi Ueda 	 */
1643f40c67f0SKiyoshi Ueda 	busy = 1;
1644f40c67f0SKiyoshi Ueda 	list_for_each_entry(pgpath, &pg->pgpaths, list)
1645f40c67f0SKiyoshi Ueda 		if (pgpath->is_active) {
1646f40c67f0SKiyoshi Ueda 			has_active = 1;
1647f40c67f0SKiyoshi Ueda 
1648f40c67f0SKiyoshi Ueda 			if (!__pgpath_busy(pgpath)) {
1649f40c67f0SKiyoshi Ueda 				busy = 0;
1650f40c67f0SKiyoshi Ueda 				break;
1651f40c67f0SKiyoshi Ueda 			}
1652f40c67f0SKiyoshi Ueda 		}
1653f40c67f0SKiyoshi Ueda 
1654f40c67f0SKiyoshi Ueda 	if (!has_active)
1655f40c67f0SKiyoshi Ueda 		/*
1656f40c67f0SKiyoshi Ueda 		 * No active path in this pg, so this pg won't be used and
1657f40c67f0SKiyoshi Ueda 		 * the current_pg will be changed at next mapping time.
1658f40c67f0SKiyoshi Ueda 		 * We need to try mapping to determine it.
1659f40c67f0SKiyoshi Ueda 		 */
1660f40c67f0SKiyoshi Ueda 		busy = 0;
1661f40c67f0SKiyoshi Ueda 
1662f40c67f0SKiyoshi Ueda out:
1663f40c67f0SKiyoshi Ueda 	spin_unlock_irqrestore(&m->lock, flags);
1664f40c67f0SKiyoshi Ueda 
1665f40c67f0SKiyoshi Ueda 	return busy;
1666f40c67f0SKiyoshi Ueda }
1667f40c67f0SKiyoshi Ueda 
16681da177e4SLinus Torvalds /*-----------------------------------------------------------------
16691da177e4SLinus Torvalds  * Module setup
16701da177e4SLinus Torvalds  *---------------------------------------------------------------*/
16711da177e4SLinus Torvalds static struct target_type multipath_target = {
16721da177e4SLinus Torvalds 	.name = "multipath",
1673e8099177SHannes Reinecke 	.version = {1, 7, 0},
16741da177e4SLinus Torvalds 	.module = THIS_MODULE,
16751da177e4SLinus Torvalds 	.ctr = multipath_ctr,
16761da177e4SLinus Torvalds 	.dtr = multipath_dtr,
1677f40c67f0SKiyoshi Ueda 	.map_rq = multipath_map,
1678f40c67f0SKiyoshi Ueda 	.rq_end_io = multipath_end_io,
16791da177e4SLinus Torvalds 	.presuspend = multipath_presuspend,
16806df400abSKiyoshi Ueda 	.postsuspend = multipath_postsuspend,
16811da177e4SLinus Torvalds 	.resume = multipath_resume,
16821da177e4SLinus Torvalds 	.status = multipath_status,
16831da177e4SLinus Torvalds 	.message = multipath_message,
16849af4aa30SMilan Broz 	.ioctl  = multipath_ioctl,
1685af4874e0SMike Snitzer 	.iterate_devices = multipath_iterate_devices,
1686f40c67f0SKiyoshi Ueda 	.busy = multipath_busy,
16871da177e4SLinus Torvalds };
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds static int __init dm_multipath_init(void)
16901da177e4SLinus Torvalds {
16911da177e4SLinus Torvalds 	int r;
16921da177e4SLinus Torvalds 
16931da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
1694028867acSAlasdair G Kergon 	_mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
16951da177e4SLinus Torvalds 	if (!_mpio_cache)
16961da177e4SLinus Torvalds 		return -ENOMEM;
16971da177e4SLinus Torvalds 
16981da177e4SLinus Torvalds 	r = dm_register_target(&multipath_target);
16991da177e4SLinus Torvalds 	if (r < 0) {
17000cd33124SAlasdair G Kergon 		DMERR("register failed %d", r);
17011da177e4SLinus Torvalds 		kmem_cache_destroy(_mpio_cache);
17021da177e4SLinus Torvalds 		return -EINVAL;
17031da177e4SLinus Torvalds 	}
17041da177e4SLinus Torvalds 
17054d4d66abSTejun Heo 	kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
1706c557308eSAlasdair G Kergon 	if (!kmultipathd) {
17070cd33124SAlasdair G Kergon 		DMERR("failed to create workqueue kmpathd");
1708c557308eSAlasdair G Kergon 		dm_unregister_target(&multipath_target);
1709c557308eSAlasdair G Kergon 		kmem_cache_destroy(_mpio_cache);
1710c557308eSAlasdair G Kergon 		return -ENOMEM;
1711c557308eSAlasdair G Kergon 	}
1712c557308eSAlasdair G Kergon 
1713bab7cfc7SChandra Seetharaman 	/*
1714bab7cfc7SChandra Seetharaman 	 * A separate workqueue is used to handle the device handlers
1715bab7cfc7SChandra Seetharaman 	 * to avoid overloading existing workqueue. Overloading the
1716bab7cfc7SChandra Seetharaman 	 * old workqueue would also create a bottleneck in the
1717bab7cfc7SChandra Seetharaman 	 * path of the storage hardware device activation.
1718bab7cfc7SChandra Seetharaman 	 */
17194d4d66abSTejun Heo 	kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
17204d4d66abSTejun Heo 						  WQ_MEM_RECLAIM);
1721bab7cfc7SChandra Seetharaman 	if (!kmpath_handlerd) {
1722bab7cfc7SChandra Seetharaman 		DMERR("failed to create workqueue kmpath_handlerd");
1723bab7cfc7SChandra Seetharaman 		destroy_workqueue(kmultipathd);
1724bab7cfc7SChandra Seetharaman 		dm_unregister_target(&multipath_target);
1725bab7cfc7SChandra Seetharaman 		kmem_cache_destroy(_mpio_cache);
1726bab7cfc7SChandra Seetharaman 		return -ENOMEM;
1727bab7cfc7SChandra Seetharaman 	}
1728bab7cfc7SChandra Seetharaman 
172972d94861SAlasdair G Kergon 	DMINFO("version %u.%u.%u loaded",
17301da177e4SLinus Torvalds 	       multipath_target.version[0], multipath_target.version[1],
17311da177e4SLinus Torvalds 	       multipath_target.version[2]);
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds 	return r;
17341da177e4SLinus Torvalds }
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds static void __exit dm_multipath_exit(void)
17371da177e4SLinus Torvalds {
1738bab7cfc7SChandra Seetharaman 	destroy_workqueue(kmpath_handlerd);
1739c557308eSAlasdair G Kergon 	destroy_workqueue(kmultipathd);
1740c557308eSAlasdair G Kergon 
174110d3bd09SMikulas Patocka 	dm_unregister_target(&multipath_target);
17421da177e4SLinus Torvalds 	kmem_cache_destroy(_mpio_cache);
17431da177e4SLinus Torvalds }
17441da177e4SLinus Torvalds 
17451da177e4SLinus Torvalds module_init(dm_multipath_init);
17461da177e4SLinus Torvalds module_exit(dm_multipath_exit);
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target");
17491da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
17501da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1751