xref: /openbmc/linux/drivers/md/dm-mpath.c (revision 2bfd2e1337f0d8bb6ff45ce12934c45b83d70ee0)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2003 Sistina Software Limited.
31da177e4SLinus Torvalds  * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This file is released under the GPL.
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
8586e80e6SMikulas Patocka #include <linux/device-mapper.h>
9586e80e6SMikulas Patocka 
101da177e4SLinus Torvalds #include "dm-path-selector.h"
11b15546f9SMike Anderson #include "dm-uevent.h"
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include <linux/ctype.h>
141da177e4SLinus Torvalds #include <linux/init.h>
151da177e4SLinus Torvalds #include <linux/mempool.h>
161da177e4SLinus Torvalds #include <linux/module.h>
171da177e4SLinus Torvalds #include <linux/pagemap.h>
181da177e4SLinus Torvalds #include <linux/slab.h>
191da177e4SLinus Torvalds #include <linux/time.h>
201da177e4SLinus Torvalds #include <linux/workqueue.h>
21cfae5c9bSChandra Seetharaman #include <scsi/scsi_dh.h>
221da177e4SLinus Torvalds #include <asm/atomic.h>
231da177e4SLinus Torvalds 
2472d94861SAlasdair G Kergon #define DM_MSG_PREFIX "multipath"
251da177e4SLinus Torvalds #define MESG_STR(x) x, sizeof(x)
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds /* Path properties */
281da177e4SLinus Torvalds struct pgpath {
291da177e4SLinus Torvalds 	struct list_head list;
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds 	struct priority_group *pg;	/* Owning PG */
326680073dSKiyoshi Ueda 	unsigned is_active;		/* Path status */
331da177e4SLinus Torvalds 	unsigned fail_count;		/* Cumulative failure count */
341da177e4SLinus Torvalds 
35c922d5f7SJosef "Jeff" Sipek 	struct dm_path path;
36224cb3e9SMike Anderson 	struct work_struct deactivate_path;
37e54f77ddSChandra Seetharaman 	struct work_struct activate_path;
381da177e4SLinus Torvalds };
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds /*
431da177e4SLinus Torvalds  * Paths are grouped into Priority Groups and numbered from 1 upwards.
441da177e4SLinus Torvalds  * Each has a path selector which controls which path gets used.
451da177e4SLinus Torvalds  */
461da177e4SLinus Torvalds struct priority_group {
471da177e4SLinus Torvalds 	struct list_head list;
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds 	struct multipath *m;		/* Owning multipath instance */
501da177e4SLinus Torvalds 	struct path_selector ps;
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 	unsigned pg_num;		/* Reference number */
531da177e4SLinus Torvalds 	unsigned bypassed;		/* Temporarily bypass this PG? */
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	unsigned nr_pgpaths;		/* Number of paths in PG */
561da177e4SLinus Torvalds 	struct list_head pgpaths;
571da177e4SLinus Torvalds };
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /* Multipath context */
601da177e4SLinus Torvalds struct multipath {
611da177e4SLinus Torvalds 	struct list_head list;
621da177e4SLinus Torvalds 	struct dm_target *ti;
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 	spinlock_t lock;
651da177e4SLinus Torvalds 
66cfae5c9bSChandra Seetharaman 	const char *hw_handler_name;
67*2bfd2e13SChandra Seetharaman 	char *hw_handler_params;
681da177e4SLinus Torvalds 	unsigned nr_priority_groups;
691da177e4SLinus Torvalds 	struct list_head priority_groups;
701da177e4SLinus Torvalds 	unsigned pg_init_required;	/* pg_init needs calling? */
71c3cd4f6bSAlasdair G Kergon 	unsigned pg_init_in_progress;	/* Only one pg_init allowed at once */
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 	unsigned nr_valid_paths;	/* Total number of usable paths */
741da177e4SLinus Torvalds 	struct pgpath *current_pgpath;
751da177e4SLinus Torvalds 	struct priority_group *current_pg;
761da177e4SLinus Torvalds 	struct priority_group *next_pg;	/* Switch to this PG if set */
771da177e4SLinus Torvalds 	unsigned repeat_count;		/* I/Os left before calling PS again */
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	unsigned queue_io;		/* Must we queue all I/O? */
801da177e4SLinus Torvalds 	unsigned queue_if_no_path;	/* Queue I/O if last path fails? */
81436d4108SAlasdair G Kergon 	unsigned saved_queue_if_no_path;/* Saved state during suspension */
82c9e45581SDave Wysochanski 	unsigned pg_init_retries;	/* Number of times to retry pg_init */
83c9e45581SDave Wysochanski 	unsigned pg_init_count;		/* Number of times pg_init called */
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	struct work_struct process_queued_ios;
86f40c67f0SKiyoshi Ueda 	struct list_head queued_ios;
871da177e4SLinus Torvalds 	unsigned queue_size;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	struct work_struct trigger_event;
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	/*
92028867acSAlasdair G Kergon 	 * We must use a mempool of dm_mpath_io structs so that we
931da177e4SLinus Torvalds 	 * can resubmit bios on error.
941da177e4SLinus Torvalds 	 */
951da177e4SLinus Torvalds 	mempool_t *mpio_pool;
961da177e4SLinus Torvalds };
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds /*
991da177e4SLinus Torvalds  * Context information attached to each bio we process.
1001da177e4SLinus Torvalds  */
101028867acSAlasdair G Kergon struct dm_mpath_io {
1021da177e4SLinus Torvalds 	struct pgpath *pgpath;
10302ab823fSKiyoshi Ueda 	size_t nr_bytes;
1041da177e4SLinus Torvalds };
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds typedef int (*action_fn) (struct pgpath *pgpath);
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds #define MIN_IOS 256	/* Mempool size */
1091da177e4SLinus Torvalds 
110e18b890bSChristoph Lameter static struct kmem_cache *_mpio_cache;
1111da177e4SLinus Torvalds 
112bab7cfc7SChandra Seetharaman static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
113c4028958SDavid Howells static void process_queued_ios(struct work_struct *work);
114c4028958SDavid Howells static void trigger_event(struct work_struct *work);
115bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work);
116224cb3e9SMike Anderson static void deactivate_path(struct work_struct *work);
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds /*-----------------------------------------------
1201da177e4SLinus Torvalds  * Allocation routines
1211da177e4SLinus Torvalds  *-----------------------------------------------*/
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds static struct pgpath *alloc_pgpath(void)
1241da177e4SLinus Torvalds {
125e69fae56SMicha³ Miros³aw 	struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
1261da177e4SLinus Torvalds 
127224cb3e9SMike Anderson 	if (pgpath) {
1286680073dSKiyoshi Ueda 		pgpath->is_active = 1;
129224cb3e9SMike Anderson 		INIT_WORK(&pgpath->deactivate_path, deactivate_path);
130e54f77ddSChandra Seetharaman 		INIT_WORK(&pgpath->activate_path, activate_path);
131224cb3e9SMike Anderson 	}
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	return pgpath;
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds 
136028867acSAlasdair G Kergon static void free_pgpath(struct pgpath *pgpath)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds 	kfree(pgpath);
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
141224cb3e9SMike Anderson static void deactivate_path(struct work_struct *work)
142224cb3e9SMike Anderson {
143224cb3e9SMike Anderson 	struct pgpath *pgpath =
144224cb3e9SMike Anderson 		container_of(work, struct pgpath, deactivate_path);
145224cb3e9SMike Anderson 
146224cb3e9SMike Anderson 	blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
147224cb3e9SMike Anderson }
148224cb3e9SMike Anderson 
1491da177e4SLinus Torvalds static struct priority_group *alloc_priority_group(void)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	struct priority_group *pg;
1521da177e4SLinus Torvalds 
153e69fae56SMicha³ Miros³aw 	pg = kzalloc(sizeof(*pg), GFP_KERNEL);
1541da177e4SLinus Torvalds 
155e69fae56SMicha³ Miros³aw 	if (pg)
1561da177e4SLinus Torvalds 		INIT_LIST_HEAD(&pg->pgpaths);
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	return pg;
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
1621da177e4SLinus Torvalds {
1631da177e4SLinus Torvalds 	struct pgpath *pgpath, *tmp;
164ae11b1b3SHannes Reinecke 	struct multipath *m = ti->private;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
1671da177e4SLinus Torvalds 		list_del(&pgpath->list);
168ae11b1b3SHannes Reinecke 		if (m->hw_handler_name)
169ae11b1b3SHannes Reinecke 			scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
1701da177e4SLinus Torvalds 		dm_put_device(ti, pgpath->path.dev);
1711da177e4SLinus Torvalds 		free_pgpath(pgpath);
1721da177e4SLinus Torvalds 	}
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds static void free_priority_group(struct priority_group *pg,
1761da177e4SLinus Torvalds 				struct dm_target *ti)
1771da177e4SLinus Torvalds {
1781da177e4SLinus Torvalds 	struct path_selector *ps = &pg->ps;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	if (ps->type) {
1811da177e4SLinus Torvalds 		ps->type->destroy(ps);
1821da177e4SLinus Torvalds 		dm_put_path_selector(ps->type);
1831da177e4SLinus Torvalds 	}
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	free_pgpaths(&pg->pgpaths, ti);
1861da177e4SLinus Torvalds 	kfree(pg);
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
18928f16c20SMicha³ Miros³aw static struct multipath *alloc_multipath(struct dm_target *ti)
1901da177e4SLinus Torvalds {
1911da177e4SLinus Torvalds 	struct multipath *m;
1921da177e4SLinus Torvalds 
193e69fae56SMicha³ Miros³aw 	m = kzalloc(sizeof(*m), GFP_KERNEL);
1941da177e4SLinus Torvalds 	if (m) {
1951da177e4SLinus Torvalds 		INIT_LIST_HEAD(&m->priority_groups);
196f40c67f0SKiyoshi Ueda 		INIT_LIST_HEAD(&m->queued_ios);
1971da177e4SLinus Torvalds 		spin_lock_init(&m->lock);
1981da177e4SLinus Torvalds 		m->queue_io = 1;
199c4028958SDavid Howells 		INIT_WORK(&m->process_queued_ios, process_queued_ios);
200c4028958SDavid Howells 		INIT_WORK(&m->trigger_event, trigger_event);
20193d2341cSMatthew Dobson 		m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
2021da177e4SLinus Torvalds 		if (!m->mpio_pool) {
2031da177e4SLinus Torvalds 			kfree(m);
2041da177e4SLinus Torvalds 			return NULL;
2051da177e4SLinus Torvalds 		}
20628f16c20SMicha³ Miros³aw 		m->ti = ti;
20728f16c20SMicha³ Miros³aw 		ti->private = m;
2081da177e4SLinus Torvalds 	}
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	return m;
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds static void free_multipath(struct multipath *m)
2141da177e4SLinus Torvalds {
2151da177e4SLinus Torvalds 	struct priority_group *pg, *tmp;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
2181da177e4SLinus Torvalds 		list_del(&pg->list);
2191da177e4SLinus Torvalds 		free_priority_group(pg, m->ti);
2201da177e4SLinus Torvalds 	}
2211da177e4SLinus Torvalds 
222cfae5c9bSChandra Seetharaman 	kfree(m->hw_handler_name);
223*2bfd2e13SChandra Seetharaman 	kfree(m->hw_handler_params);
2241da177e4SLinus Torvalds 	mempool_destroy(m->mpio_pool);
2251da177e4SLinus Torvalds 	kfree(m);
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds /*-----------------------------------------------
2301da177e4SLinus Torvalds  * Path selection
2311da177e4SLinus Torvalds  *-----------------------------------------------*/
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
2341da177e4SLinus Torvalds {
2351da177e4SLinus Torvalds 	m->current_pg = pgpath->pg;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	/* Must we initialise the PG first, and queue I/O till it's ready? */
238cfae5c9bSChandra Seetharaman 	if (m->hw_handler_name) {
2391da177e4SLinus Torvalds 		m->pg_init_required = 1;
2401da177e4SLinus Torvalds 		m->queue_io = 1;
2411da177e4SLinus Torvalds 	} else {
2421da177e4SLinus Torvalds 		m->pg_init_required = 0;
2431da177e4SLinus Torvalds 		m->queue_io = 0;
2441da177e4SLinus Torvalds 	}
245c9e45581SDave Wysochanski 
246c9e45581SDave Wysochanski 	m->pg_init_count = 0;
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
24902ab823fSKiyoshi Ueda static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
25002ab823fSKiyoshi Ueda 			       size_t nr_bytes)
2511da177e4SLinus Torvalds {
252c922d5f7SJosef "Jeff" Sipek 	struct dm_path *path;
2531da177e4SLinus Torvalds 
25402ab823fSKiyoshi Ueda 	path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
2551da177e4SLinus Torvalds 	if (!path)
2561da177e4SLinus Torvalds 		return -ENXIO;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	m->current_pgpath = path_to_pgpath(path);
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	if (m->current_pg != pg)
2611da177e4SLinus Torvalds 		__switch_pg(m, m->current_pgpath);
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	return 0;
2641da177e4SLinus Torvalds }
2651da177e4SLinus Torvalds 
26602ab823fSKiyoshi Ueda static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
2671da177e4SLinus Torvalds {
2681da177e4SLinus Torvalds 	struct priority_group *pg;
2691da177e4SLinus Torvalds 	unsigned bypassed = 1;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	if (!m->nr_valid_paths)
2721da177e4SLinus Torvalds 		goto failed;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	/* Were we instructed to switch PG? */
2751da177e4SLinus Torvalds 	if (m->next_pg) {
2761da177e4SLinus Torvalds 		pg = m->next_pg;
2771da177e4SLinus Torvalds 		m->next_pg = NULL;
27802ab823fSKiyoshi Ueda 		if (!__choose_path_in_pg(m, pg, nr_bytes))
2791da177e4SLinus Torvalds 			return;
2801da177e4SLinus Torvalds 	}
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	/* Don't change PG until it has no remaining paths */
28302ab823fSKiyoshi Ueda 	if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
2841da177e4SLinus Torvalds 		return;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	/*
2871da177e4SLinus Torvalds 	 * Loop through priority groups until we find a valid path.
2881da177e4SLinus Torvalds 	 * First time we skip PGs marked 'bypassed'.
2891da177e4SLinus Torvalds 	 * Second time we only try the ones we skipped.
2901da177e4SLinus Torvalds 	 */
2911da177e4SLinus Torvalds 	do {
2921da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
2931da177e4SLinus Torvalds 			if (pg->bypassed == bypassed)
2941da177e4SLinus Torvalds 				continue;
29502ab823fSKiyoshi Ueda 			if (!__choose_path_in_pg(m, pg, nr_bytes))
2961da177e4SLinus Torvalds 				return;
2971da177e4SLinus Torvalds 		}
2981da177e4SLinus Torvalds 	} while (bypassed--);
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds failed:
3011da177e4SLinus Torvalds 	m->current_pgpath = NULL;
3021da177e4SLinus Torvalds 	m->current_pg = NULL;
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
30545e15720SKiyoshi Ueda /*
30645e15720SKiyoshi Ueda  * Check whether bios must be queued in the device-mapper core rather
30745e15720SKiyoshi Ueda  * than here in the target.
30845e15720SKiyoshi Ueda  *
30945e15720SKiyoshi Ueda  * m->lock must be held on entry.
31045e15720SKiyoshi Ueda  *
31145e15720SKiyoshi Ueda  * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
31245e15720SKiyoshi Ueda  * same value then we are not between multipath_presuspend()
31345e15720SKiyoshi Ueda  * and multipath_resume() calls and we have no need to check
31445e15720SKiyoshi Ueda  * for the DMF_NOFLUSH_SUSPENDING flag.
31545e15720SKiyoshi Ueda  */
31645e15720SKiyoshi Ueda static int __must_push_back(struct multipath *m)
31745e15720SKiyoshi Ueda {
31845e15720SKiyoshi Ueda 	return (m->queue_if_no_path != m->saved_queue_if_no_path &&
31945e15720SKiyoshi Ueda 		dm_noflush_suspending(m->ti));
32045e15720SKiyoshi Ueda }
32145e15720SKiyoshi Ueda 
322f40c67f0SKiyoshi Ueda static int map_io(struct multipath *m, struct request *clone,
323028867acSAlasdair G Kergon 		  struct dm_mpath_io *mpio, unsigned was_queued)
3241da177e4SLinus Torvalds {
325d2a7ad29SKiyoshi Ueda 	int r = DM_MAPIO_REMAPPED;
326f40c67f0SKiyoshi Ueda 	size_t nr_bytes = blk_rq_bytes(clone);
3271da177e4SLinus Torvalds 	unsigned long flags;
3281da177e4SLinus Torvalds 	struct pgpath *pgpath;
329f40c67f0SKiyoshi Ueda 	struct block_device *bdev;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	/* Do we need to select a new pgpath? */
3341da177e4SLinus Torvalds 	if (!m->current_pgpath ||
3351da177e4SLinus Torvalds 	    (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
33602ab823fSKiyoshi Ueda 		__choose_pgpath(m, nr_bytes);
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	pgpath = m->current_pgpath;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	if (was_queued)
3411da177e4SLinus Torvalds 		m->queue_size--;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	if ((pgpath && m->queue_io) ||
344436d4108SAlasdair G Kergon 	    (!pgpath && m->queue_if_no_path)) {
3451da177e4SLinus Torvalds 		/* Queue for the daemon to resubmit */
346f40c67f0SKiyoshi Ueda 		list_add_tail(&clone->queuelist, &m->queued_ios);
3471da177e4SLinus Torvalds 		m->queue_size++;
348c3cd4f6bSAlasdair G Kergon 		if ((m->pg_init_required && !m->pg_init_in_progress) ||
349c3cd4f6bSAlasdair G Kergon 		    !m->queue_io)
350c557308eSAlasdair G Kergon 			queue_work(kmultipathd, &m->process_queued_ios);
3511da177e4SLinus Torvalds 		pgpath = NULL;
352d2a7ad29SKiyoshi Ueda 		r = DM_MAPIO_SUBMITTED;
353f40c67f0SKiyoshi Ueda 	} else if (pgpath) {
354f40c67f0SKiyoshi Ueda 		bdev = pgpath->path.dev->bdev;
355f40c67f0SKiyoshi Ueda 		clone->q = bdev_get_queue(bdev);
356f40c67f0SKiyoshi Ueda 		clone->rq_disk = bdev->bd_disk;
357f40c67f0SKiyoshi Ueda 	} else if (__must_push_back(m))
35845e15720SKiyoshi Ueda 		r = DM_MAPIO_REQUEUE;
35945e15720SKiyoshi Ueda 	else
36045e15720SKiyoshi Ueda 		r = -EIO;	/* Failed */
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	mpio->pgpath = pgpath;
36302ab823fSKiyoshi Ueda 	mpio->nr_bytes = nr_bytes;
36402ab823fSKiyoshi Ueda 
36502ab823fSKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io)
36602ab823fSKiyoshi Ueda 		pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path,
36702ab823fSKiyoshi Ueda 					      nr_bytes);
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	return r;
3721da177e4SLinus Torvalds }
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds /*
3751da177e4SLinus Torvalds  * If we run out of usable paths, should we queue I/O or error it?
3761da177e4SLinus Torvalds  */
377485ef69eSAlasdair G Kergon static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
378485ef69eSAlasdair G Kergon 			    unsigned save_old_value)
3791da177e4SLinus Torvalds {
3801da177e4SLinus Torvalds 	unsigned long flags;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
3831da177e4SLinus Torvalds 
384485ef69eSAlasdair G Kergon 	if (save_old_value)
385436d4108SAlasdair G Kergon 		m->saved_queue_if_no_path = m->queue_if_no_path;
386485ef69eSAlasdair G Kergon 	else
387485ef69eSAlasdair G Kergon 		m->saved_queue_if_no_path = queue_if_no_path;
3881da177e4SLinus Torvalds 	m->queue_if_no_path = queue_if_no_path;
389c3cd4f6bSAlasdair G Kergon 	if (!m->queue_if_no_path && m->queue_size)
390c557308eSAlasdair G Kergon 		queue_work(kmultipathd, &m->process_queued_ios);
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	return 0;
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds /*-----------------------------------------------------------------
3981da177e4SLinus Torvalds  * The multipath daemon is responsible for resubmitting queued ios.
3991da177e4SLinus Torvalds  *---------------------------------------------------------------*/
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds static void dispatch_queued_ios(struct multipath *m)
4021da177e4SLinus Torvalds {
4031da177e4SLinus Torvalds 	int r;
4041da177e4SLinus Torvalds 	unsigned long flags;
405028867acSAlasdair G Kergon 	struct dm_mpath_io *mpio;
4061da177e4SLinus Torvalds 	union map_info *info;
407f40c67f0SKiyoshi Ueda 	struct request *clone, *n;
408f40c67f0SKiyoshi Ueda 	LIST_HEAD(cl);
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
411f40c67f0SKiyoshi Ueda 	list_splice_init(&m->queued_ios, &cl);
4121da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
4131da177e4SLinus Torvalds 
414f40c67f0SKiyoshi Ueda 	list_for_each_entry_safe(clone, n, &cl, queuelist) {
415f40c67f0SKiyoshi Ueda 		list_del_init(&clone->queuelist);
4161da177e4SLinus Torvalds 
417f40c67f0SKiyoshi Ueda 		info = dm_get_rq_mapinfo(clone);
4181da177e4SLinus Torvalds 		mpio = info->ptr;
4191da177e4SLinus Torvalds 
420f40c67f0SKiyoshi Ueda 		r = map_io(m, clone, mpio, 1);
421f40c67f0SKiyoshi Ueda 		if (r < 0) {
422f40c67f0SKiyoshi Ueda 			mempool_free(mpio, m->mpio_pool);
423f40c67f0SKiyoshi Ueda 			dm_kill_unmapped_request(clone, r);
424f40c67f0SKiyoshi Ueda 		} else if (r == DM_MAPIO_REMAPPED)
425f40c67f0SKiyoshi Ueda 			dm_dispatch_request(clone);
426f40c67f0SKiyoshi Ueda 		else if (r == DM_MAPIO_REQUEUE) {
427f40c67f0SKiyoshi Ueda 			mempool_free(mpio, m->mpio_pool);
428f40c67f0SKiyoshi Ueda 			dm_requeue_unmapped_request(clone);
429f40c67f0SKiyoshi Ueda 		}
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds 
433c4028958SDavid Howells static void process_queued_ios(struct work_struct *work)
4341da177e4SLinus Torvalds {
435c4028958SDavid Howells 	struct multipath *m =
436c4028958SDavid Howells 		container_of(work, struct multipath, process_queued_ios);
437e54f77ddSChandra Seetharaman 	struct pgpath *pgpath = NULL, *tmp;
438e54f77ddSChandra Seetharaman 	unsigned must_queue = 1;
4391da177e4SLinus Torvalds 	unsigned long flags;
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
4421da177e4SLinus Torvalds 
443c3cd4f6bSAlasdair G Kergon 	if (!m->queue_size)
444c3cd4f6bSAlasdair G Kergon 		goto out;
445c3cd4f6bSAlasdair G Kergon 
4461da177e4SLinus Torvalds 	if (!m->current_pgpath)
44702ab823fSKiyoshi Ueda 		__choose_pgpath(m, 0);
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	pgpath = m->current_pgpath;
4501da177e4SLinus Torvalds 
451c3cd4f6bSAlasdair G Kergon 	if ((pgpath && !m->queue_io) ||
452c3cd4f6bSAlasdair G Kergon 	    (!pgpath && !m->queue_if_no_path))
453c3cd4f6bSAlasdair G Kergon 		must_queue = 0;
4541da177e4SLinus Torvalds 
455b81aa1c7SChandra Seetharaman 	if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
456c9e45581SDave Wysochanski 		m->pg_init_count++;
4571da177e4SLinus Torvalds 		m->pg_init_required = 0;
458e54f77ddSChandra Seetharaman 		list_for_each_entry(tmp, &pgpath->pg->pgpaths, list) {
459e54f77ddSChandra Seetharaman 			if (queue_work(kmpath_handlerd, &tmp->activate_path))
460e54f77ddSChandra Seetharaman 				m->pg_init_in_progress++;
461c3cd4f6bSAlasdair G Kergon 		}
462e54f77ddSChandra Seetharaman 	}
463c3cd4f6bSAlasdair G Kergon out:
4641da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
4651da177e4SLinus Torvalds 	if (!must_queue)
4661da177e4SLinus Torvalds 		dispatch_queued_ios(m);
4671da177e4SLinus Torvalds }
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds /*
4701da177e4SLinus Torvalds  * An event is triggered whenever a path is taken out of use.
4711da177e4SLinus Torvalds  * Includes path failure and PG bypass.
4721da177e4SLinus Torvalds  */
473c4028958SDavid Howells static void trigger_event(struct work_struct *work)
4741da177e4SLinus Torvalds {
475c4028958SDavid Howells 	struct multipath *m =
476c4028958SDavid Howells 		container_of(work, struct multipath, trigger_event);
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	dm_table_event(m->ti->table);
4791da177e4SLinus Torvalds }
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds /*-----------------------------------------------------------------
4821da177e4SLinus Torvalds  * Constructor/argument parsing:
4831da177e4SLinus Torvalds  * <#multipath feature args> [<arg>]*
4841da177e4SLinus Torvalds  * <#hw_handler args> [hw_handler [<arg>]*]
4851da177e4SLinus Torvalds  * <#priority groups>
4861da177e4SLinus Torvalds  * <initial priority group>
4871da177e4SLinus Torvalds  *     [<selector> <#selector args> [<arg>]*
4881da177e4SLinus Torvalds  *      <#paths> <#per-path selector args>
4891da177e4SLinus Torvalds  *         [<path> [<arg>]* ]+ ]+
4901da177e4SLinus Torvalds  *---------------------------------------------------------------*/
4911da177e4SLinus Torvalds struct param {
4921da177e4SLinus Torvalds 	unsigned min;
4931da177e4SLinus Torvalds 	unsigned max;
4941da177e4SLinus Torvalds 	char *error;
4951da177e4SLinus Torvalds };
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds static int read_param(struct param *param, char *str, unsigned *v, char **error)
4981da177e4SLinus Torvalds {
4991da177e4SLinus Torvalds 	if (!str ||
5001da177e4SLinus Torvalds 	    (sscanf(str, "%u", v) != 1) ||
5011da177e4SLinus Torvalds 	    (*v < param->min) ||
5021da177e4SLinus Torvalds 	    (*v > param->max)) {
5031da177e4SLinus Torvalds 		*error = param->error;
5041da177e4SLinus Torvalds 		return -EINVAL;
5051da177e4SLinus Torvalds 	}
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	return 0;
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds struct arg_set {
5111da177e4SLinus Torvalds 	unsigned argc;
5121da177e4SLinus Torvalds 	char **argv;
5131da177e4SLinus Torvalds };
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds static char *shift(struct arg_set *as)
5161da177e4SLinus Torvalds {
5171da177e4SLinus Torvalds 	char *r;
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	if (as->argc) {
5201da177e4SLinus Torvalds 		as->argc--;
5211da177e4SLinus Torvalds 		r = *as->argv;
5221da177e4SLinus Torvalds 		as->argv++;
5231da177e4SLinus Torvalds 		return r;
5241da177e4SLinus Torvalds 	}
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	return NULL;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds static void consume(struct arg_set *as, unsigned n)
5301da177e4SLinus Torvalds {
5311da177e4SLinus Torvalds 	BUG_ON (as->argc < n);
5321da177e4SLinus Torvalds 	as->argc -= n;
5331da177e4SLinus Torvalds 	as->argv += n;
5341da177e4SLinus Torvalds }
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
5371da177e4SLinus Torvalds 			       struct dm_target *ti)
5381da177e4SLinus Torvalds {
5391da177e4SLinus Torvalds 	int r;
5401da177e4SLinus Torvalds 	struct path_selector_type *pst;
5411da177e4SLinus Torvalds 	unsigned ps_argc;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	static struct param _params[] = {
54472d94861SAlasdair G Kergon 		{0, 1024, "invalid number of path selector args"},
5451da177e4SLinus Torvalds 	};
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	pst = dm_get_path_selector(shift(as));
5481da177e4SLinus Torvalds 	if (!pst) {
54972d94861SAlasdair G Kergon 		ti->error = "unknown path selector type";
5501da177e4SLinus Torvalds 		return -EINVAL;
5511da177e4SLinus Torvalds 	}
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	r = read_param(_params, shift(as), &ps_argc, &ti->error);
554371b2e34SMikulas Patocka 	if (r) {
555371b2e34SMikulas Patocka 		dm_put_path_selector(pst);
5561da177e4SLinus Torvalds 		return -EINVAL;
557371b2e34SMikulas Patocka 	}
5581da177e4SLinus Torvalds 
5590e0497c0SMikulas Patocka 	if (ps_argc > as->argc) {
5600e0497c0SMikulas Patocka 		dm_put_path_selector(pst);
5610e0497c0SMikulas Patocka 		ti->error = "not enough arguments for path selector";
5620e0497c0SMikulas Patocka 		return -EINVAL;
5630e0497c0SMikulas Patocka 	}
5640e0497c0SMikulas Patocka 
5651da177e4SLinus Torvalds 	r = pst->create(&pg->ps, ps_argc, as->argv);
5661da177e4SLinus Torvalds 	if (r) {
5671da177e4SLinus Torvalds 		dm_put_path_selector(pst);
56872d94861SAlasdair G Kergon 		ti->error = "path selector constructor failed";
5691da177e4SLinus Torvalds 		return r;
5701da177e4SLinus Torvalds 	}
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	pg->ps.type = pst;
5731da177e4SLinus Torvalds 	consume(as, ps_argc);
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	return 0;
5761da177e4SLinus Torvalds }
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
5791da177e4SLinus Torvalds 			       struct dm_target *ti)
5801da177e4SLinus Torvalds {
5811da177e4SLinus Torvalds 	int r;
5821da177e4SLinus Torvalds 	struct pgpath *p;
583ae11b1b3SHannes Reinecke 	struct multipath *m = ti->private;
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	/* we need at least a path arg */
5861da177e4SLinus Torvalds 	if (as->argc < 1) {
58772d94861SAlasdair G Kergon 		ti->error = "no device given";
58801460f35SBenjamin Marzinski 		return ERR_PTR(-EINVAL);
5891da177e4SLinus Torvalds 	}
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	p = alloc_pgpath();
5921da177e4SLinus Torvalds 	if (!p)
59301460f35SBenjamin Marzinski 		return ERR_PTR(-ENOMEM);
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	r = dm_get_device(ti, shift(as), ti->begin, ti->len,
5961da177e4SLinus Torvalds 			  dm_table_get_mode(ti->table), &p->path.dev);
5971da177e4SLinus Torvalds 	if (r) {
59872d94861SAlasdair G Kergon 		ti->error = "error getting device";
5991da177e4SLinus Torvalds 		goto bad;
6001da177e4SLinus Torvalds 	}
6011da177e4SLinus Torvalds 
602ae11b1b3SHannes Reinecke 	if (m->hw_handler_name) {
603a0cf7ea9SHannes Reinecke 		struct request_queue *q = bdev_get_queue(p->path.dev->bdev);
604a0cf7ea9SHannes Reinecke 
605a0cf7ea9SHannes Reinecke 		r = scsi_dh_attach(q, m->hw_handler_name);
606a0cf7ea9SHannes Reinecke 		if (r == -EBUSY) {
607a0cf7ea9SHannes Reinecke 			/*
608a0cf7ea9SHannes Reinecke 			 * Already attached to different hw_handler,
609a0cf7ea9SHannes Reinecke 			 * try to reattach with correct one.
610a0cf7ea9SHannes Reinecke 			 */
611a0cf7ea9SHannes Reinecke 			scsi_dh_detach(q);
612a0cf7ea9SHannes Reinecke 			r = scsi_dh_attach(q, m->hw_handler_name);
613a0cf7ea9SHannes Reinecke 		}
614a0cf7ea9SHannes Reinecke 
615ae11b1b3SHannes Reinecke 		if (r < 0) {
616a0cf7ea9SHannes Reinecke 			ti->error = "error attaching hardware handler";
617ae11b1b3SHannes Reinecke 			dm_put_device(ti, p->path.dev);
618ae11b1b3SHannes Reinecke 			goto bad;
619ae11b1b3SHannes Reinecke 		}
620*2bfd2e13SChandra Seetharaman 
621*2bfd2e13SChandra Seetharaman 		if (m->hw_handler_params) {
622*2bfd2e13SChandra Seetharaman 			r = scsi_dh_set_params(q, m->hw_handler_params);
623*2bfd2e13SChandra Seetharaman 			if (r < 0) {
624*2bfd2e13SChandra Seetharaman 				ti->error = "unable to set hardware "
625*2bfd2e13SChandra Seetharaman 							"handler parameters";
626*2bfd2e13SChandra Seetharaman 				scsi_dh_detach(q);
627*2bfd2e13SChandra Seetharaman 				dm_put_device(ti, p->path.dev);
628*2bfd2e13SChandra Seetharaman 				goto bad;
629*2bfd2e13SChandra Seetharaman 			}
630*2bfd2e13SChandra Seetharaman 		}
631ae11b1b3SHannes Reinecke 	}
632ae11b1b3SHannes Reinecke 
6331da177e4SLinus Torvalds 	r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
6341da177e4SLinus Torvalds 	if (r) {
6351da177e4SLinus Torvalds 		dm_put_device(ti, p->path.dev);
6361da177e4SLinus Torvalds 		goto bad;
6371da177e4SLinus Torvalds 	}
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	return p;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds  bad:
6421da177e4SLinus Torvalds 	free_pgpath(p);
64301460f35SBenjamin Marzinski 	return ERR_PTR(r);
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds static struct priority_group *parse_priority_group(struct arg_set *as,
64728f16c20SMicha³ Miros³aw 						   struct multipath *m)
6481da177e4SLinus Torvalds {
6491da177e4SLinus Torvalds 	static struct param _params[] = {
65072d94861SAlasdair G Kergon 		{1, 1024, "invalid number of paths"},
65172d94861SAlasdair G Kergon 		{0, 1024, "invalid number of selector args"}
6521da177e4SLinus Torvalds 	};
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	int r;
6551da177e4SLinus Torvalds 	unsigned i, nr_selector_args, nr_params;
6561da177e4SLinus Torvalds 	struct priority_group *pg;
65728f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	if (as->argc < 2) {
6601da177e4SLinus Torvalds 		as->argc = 0;
66101460f35SBenjamin Marzinski 		ti->error = "not enough priority group arguments";
66201460f35SBenjamin Marzinski 		return ERR_PTR(-EINVAL);
6631da177e4SLinus Torvalds 	}
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 	pg = alloc_priority_group();
6661da177e4SLinus Torvalds 	if (!pg) {
66772d94861SAlasdair G Kergon 		ti->error = "couldn't allocate priority group";
66801460f35SBenjamin Marzinski 		return ERR_PTR(-ENOMEM);
6691da177e4SLinus Torvalds 	}
6701da177e4SLinus Torvalds 	pg->m = m;
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	r = parse_path_selector(as, pg, ti);
6731da177e4SLinus Torvalds 	if (r)
6741da177e4SLinus Torvalds 		goto bad;
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	/*
6771da177e4SLinus Torvalds 	 * read the paths
6781da177e4SLinus Torvalds 	 */
6791da177e4SLinus Torvalds 	r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
6801da177e4SLinus Torvalds 	if (r)
6811da177e4SLinus Torvalds 		goto bad;
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
6841da177e4SLinus Torvalds 	if (r)
6851da177e4SLinus Torvalds 		goto bad;
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	nr_params = 1 + nr_selector_args;
6881da177e4SLinus Torvalds 	for (i = 0; i < pg->nr_pgpaths; i++) {
6891da177e4SLinus Torvalds 		struct pgpath *pgpath;
6901da177e4SLinus Torvalds 		struct arg_set path_args;
6911da177e4SLinus Torvalds 
692148acff6SMikulas Patocka 		if (as->argc < nr_params) {
693148acff6SMikulas Patocka 			ti->error = "not enough path parameters";
6941da177e4SLinus Torvalds 			goto bad;
695148acff6SMikulas Patocka 		}
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 		path_args.argc = nr_params;
6981da177e4SLinus Torvalds 		path_args.argv = as->argv;
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 		pgpath = parse_path(&path_args, &pg->ps, ti);
70101460f35SBenjamin Marzinski 		if (IS_ERR(pgpath)) {
70201460f35SBenjamin Marzinski 			r = PTR_ERR(pgpath);
7031da177e4SLinus Torvalds 			goto bad;
70401460f35SBenjamin Marzinski 		}
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 		pgpath->pg = pg;
7071da177e4SLinus Torvalds 		list_add_tail(&pgpath->list, &pg->pgpaths);
7081da177e4SLinus Torvalds 		consume(as, nr_params);
7091da177e4SLinus Torvalds 	}
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	return pg;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds  bad:
7141da177e4SLinus Torvalds 	free_priority_group(pg, ti);
71501460f35SBenjamin Marzinski 	return ERR_PTR(r);
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
71828f16c20SMicha³ Miros³aw static int parse_hw_handler(struct arg_set *as, struct multipath *m)
7191da177e4SLinus Torvalds {
7201da177e4SLinus Torvalds 	unsigned hw_argc;
721*2bfd2e13SChandra Seetharaman 	int ret;
72228f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	static struct param _params[] = {
72572d94861SAlasdair G Kergon 		{0, 1024, "invalid number of hardware handler args"},
7261da177e4SLinus Torvalds 	};
7271da177e4SLinus Torvalds 
728cfae5c9bSChandra Seetharaman 	if (read_param(_params, shift(as), &hw_argc, &ti->error))
7291da177e4SLinus Torvalds 		return -EINVAL;
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	if (!hw_argc)
7321da177e4SLinus Torvalds 		return 0;
7331da177e4SLinus Torvalds 
734e094f4f1SMikulas Patocka 	if (hw_argc > as->argc) {
735e094f4f1SMikulas Patocka 		ti->error = "not enough arguments for hardware handler";
736e094f4f1SMikulas Patocka 		return -EINVAL;
737e094f4f1SMikulas Patocka 	}
738e094f4f1SMikulas Patocka 
739cfae5c9bSChandra Seetharaman 	m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
740cfae5c9bSChandra Seetharaman 	request_module("scsi_dh_%s", m->hw_handler_name);
741cfae5c9bSChandra Seetharaman 	if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
74272d94861SAlasdair G Kergon 		ti->error = "unknown hardware handler type";
743*2bfd2e13SChandra Seetharaman 		ret = -EINVAL;
744*2bfd2e13SChandra Seetharaman 		goto fail;
7451da177e4SLinus Torvalds 	}
74614e98c5cSChandra Seetharaman 
747*2bfd2e13SChandra Seetharaman 	if (hw_argc > 1) {
748*2bfd2e13SChandra Seetharaman 		char *p;
749*2bfd2e13SChandra Seetharaman 		int i, j, len = 4;
750*2bfd2e13SChandra Seetharaman 
751*2bfd2e13SChandra Seetharaman 		for (i = 0; i <= hw_argc - 2; i++)
752*2bfd2e13SChandra Seetharaman 			len += strlen(as->argv[i]) + 1;
753*2bfd2e13SChandra Seetharaman 		p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
754*2bfd2e13SChandra Seetharaman 		if (!p) {
755*2bfd2e13SChandra Seetharaman 			ti->error = "memory allocation failed";
756*2bfd2e13SChandra Seetharaman 			ret = -ENOMEM;
757*2bfd2e13SChandra Seetharaman 			goto fail;
758*2bfd2e13SChandra Seetharaman 		}
759*2bfd2e13SChandra Seetharaman 		j = sprintf(p, "%d", hw_argc - 1);
760*2bfd2e13SChandra Seetharaman 		for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
761*2bfd2e13SChandra Seetharaman 			j = sprintf(p, "%s", as->argv[i]);
762*2bfd2e13SChandra Seetharaman 	}
7631da177e4SLinus Torvalds 	consume(as, hw_argc - 1);
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	return 0;
766*2bfd2e13SChandra Seetharaman fail:
767*2bfd2e13SChandra Seetharaman 	kfree(m->hw_handler_name);
768*2bfd2e13SChandra Seetharaman 	m->hw_handler_name = NULL;
769*2bfd2e13SChandra Seetharaman 	return ret;
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds 
77228f16c20SMicha³ Miros³aw static int parse_features(struct arg_set *as, struct multipath *m)
7731da177e4SLinus Torvalds {
7741da177e4SLinus Torvalds 	int r;
7751da177e4SLinus Torvalds 	unsigned argc;
77628f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
777c9e45581SDave Wysochanski 	const char *param_name;
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	static struct param _params[] = {
780c9e45581SDave Wysochanski 		{0, 3, "invalid number of feature args"},
781c9e45581SDave Wysochanski 		{1, 50, "pg_init_retries must be between 1 and 50"},
7821da177e4SLinus Torvalds 	};
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	r = read_param(_params, shift(as), &argc, &ti->error);
7851da177e4SLinus Torvalds 	if (r)
7861da177e4SLinus Torvalds 		return -EINVAL;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	if (!argc)
7891da177e4SLinus Torvalds 		return 0;
7901da177e4SLinus Torvalds 
791c9e45581SDave Wysochanski 	do {
792c9e45581SDave Wysochanski 		param_name = shift(as);
793c9e45581SDave Wysochanski 		argc--;
794c9e45581SDave Wysochanski 
795c9e45581SDave Wysochanski 		if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
796c9e45581SDave Wysochanski 			r = queue_if_no_path(m, 1, 0);
797c9e45581SDave Wysochanski 			continue;
7981da177e4SLinus Torvalds 		}
799c9e45581SDave Wysochanski 
800c9e45581SDave Wysochanski 		if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
801c9e45581SDave Wysochanski 		    (argc >= 1)) {
802c9e45581SDave Wysochanski 			r = read_param(_params + 1, shift(as),
803c9e45581SDave Wysochanski 				       &m->pg_init_retries, &ti->error);
804c9e45581SDave Wysochanski 			argc--;
805c9e45581SDave Wysochanski 			continue;
806c9e45581SDave Wysochanski 		}
807c9e45581SDave Wysochanski 
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 {
8181da177e4SLinus Torvalds 	/* target parameters */
8191da177e4SLinus Torvalds 	static struct param _params[] = {
82072d94861SAlasdair G Kergon 		{1, 1024, "invalid number of priority groups"},
82172d94861SAlasdair G Kergon 		{1, 1024, "invalid initial priority group number"},
8221da177e4SLinus Torvalds 	};
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	int r;
8251da177e4SLinus Torvalds 	struct multipath *m;
8261da177e4SLinus Torvalds 	struct 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 
8471da177e4SLinus Torvalds 	r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
8481da177e4SLinus Torvalds 	if (r)
8491da177e4SLinus Torvalds 		goto bad;
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
8521da177e4SLinus Torvalds 	if (r)
8531da177e4SLinus Torvalds 		goto bad;
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	/* parse the priority groups */
8561da177e4SLinus Torvalds 	while (as.argc) {
8571da177e4SLinus Torvalds 		struct priority_group *pg;
8581da177e4SLinus Torvalds 
85928f16c20SMicha³ Miros³aw 		pg = parse_priority_group(&as, m);
86001460f35SBenjamin Marzinski 		if (IS_ERR(pg)) {
86101460f35SBenjamin Marzinski 			r = PTR_ERR(pg);
8621da177e4SLinus Torvalds 			goto bad;
8631da177e4SLinus Torvalds 		}
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 		m->nr_valid_paths += pg->nr_pgpaths;
8661da177e4SLinus Torvalds 		list_add_tail(&pg->list, &m->priority_groups);
8671da177e4SLinus Torvalds 		pg_count++;
8681da177e4SLinus Torvalds 		pg->pg_num = pg_count;
8691da177e4SLinus Torvalds 		if (!--next_pg_num)
8701da177e4SLinus Torvalds 			m->next_pg = pg;
8711da177e4SLinus Torvalds 	}
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	if (pg_count != m->nr_priority_groups) {
87472d94861SAlasdair G Kergon 		ti->error = "priority group count mismatch";
8751da177e4SLinus Torvalds 		r = -EINVAL;
8761da177e4SLinus Torvalds 		goto bad;
8771da177e4SLinus Torvalds 	}
8781da177e4SLinus Torvalds 
8798627921fSMikulas Patocka 	ti->num_flush_requests = 1;
8808627921fSMikulas Patocka 
8811da177e4SLinus Torvalds 	return 0;
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds  bad:
8841da177e4SLinus Torvalds 	free_multipath(m);
8851da177e4SLinus Torvalds 	return r;
8861da177e4SLinus Torvalds }
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds static void multipath_dtr(struct dm_target *ti)
8891da177e4SLinus Torvalds {
8901da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
891a044d016SAlasdair G Kergon 
892bab7cfc7SChandra Seetharaman 	flush_workqueue(kmpath_handlerd);
893a044d016SAlasdair G Kergon 	flush_workqueue(kmultipathd);
89453b351f9SMikulas Patocka 	flush_scheduled_work();
8951da177e4SLinus Torvalds 	free_multipath(m);
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds /*
899f40c67f0SKiyoshi Ueda  * Map cloned requests
9001da177e4SLinus Torvalds  */
901f40c67f0SKiyoshi Ueda static int multipath_map(struct dm_target *ti, struct request *clone,
9021da177e4SLinus Torvalds 			 union map_info *map_context)
9031da177e4SLinus Torvalds {
9041da177e4SLinus Torvalds 	int r;
905028867acSAlasdair G Kergon 	struct dm_mpath_io *mpio;
9061da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
9071da177e4SLinus Torvalds 
908f40c67f0SKiyoshi Ueda 	mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
909f40c67f0SKiyoshi Ueda 	if (!mpio)
910f40c67f0SKiyoshi Ueda 		/* ENOMEM, requeue */
911f40c67f0SKiyoshi Ueda 		return DM_MAPIO_REQUEUE;
912f40c67f0SKiyoshi Ueda 	memset(mpio, 0, sizeof(*mpio));
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	map_context->ptr = mpio;
915f40c67f0SKiyoshi Ueda 	clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
916f40c67f0SKiyoshi Ueda 	r = map_io(m, clone, mpio, 0);
91745e15720SKiyoshi Ueda 	if (r < 0 || r == DM_MAPIO_REQUEUE)
9181da177e4SLinus Torvalds 		mempool_free(mpio, m->mpio_pool);
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 	return r;
9211da177e4SLinus Torvalds }
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds /*
9241da177e4SLinus Torvalds  * Take a path out of use.
9251da177e4SLinus Torvalds  */
9261da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath)
9271da177e4SLinus Torvalds {
9281da177e4SLinus Torvalds 	unsigned long flags;
9291da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
9321da177e4SLinus Torvalds 
9336680073dSKiyoshi Ueda 	if (!pgpath->is_active)
9341da177e4SLinus Torvalds 		goto out;
9351da177e4SLinus Torvalds 
93672d94861SAlasdair G Kergon 	DMWARN("Failing path %s.", pgpath->path.dev->name);
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 	pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
9396680073dSKiyoshi Ueda 	pgpath->is_active = 0;
9401da177e4SLinus Torvalds 	pgpath->fail_count++;
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 	m->nr_valid_paths--;
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds 	if (pgpath == m->current_pgpath)
9451da177e4SLinus Torvalds 		m->current_pgpath = NULL;
9461da177e4SLinus Torvalds 
947b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
948b15546f9SMike Anderson 		      pgpath->path.dev->name, m->nr_valid_paths);
949b15546f9SMike Anderson 
950fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
951224cb3e9SMike Anderson 	queue_work(kmultipathd, &pgpath->deactivate_path);
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds out:
9541da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	return 0;
9571da177e4SLinus Torvalds }
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds /*
9601da177e4SLinus Torvalds  * Reinstate a previously-failed path
9611da177e4SLinus Torvalds  */
9621da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath)
9631da177e4SLinus Torvalds {
9641da177e4SLinus Torvalds 	int r = 0;
9651da177e4SLinus Torvalds 	unsigned long flags;
9661da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
9691da177e4SLinus Torvalds 
9706680073dSKiyoshi Ueda 	if (pgpath->is_active)
9711da177e4SLinus Torvalds 		goto out;
9721da177e4SLinus Torvalds 
973def052d2SAlasdair G Kergon 	if (!pgpath->pg->ps.type->reinstate_path) {
9741da177e4SLinus Torvalds 		DMWARN("Reinstate path not supported by path selector %s",
9751da177e4SLinus Torvalds 		       pgpath->pg->ps.type->name);
9761da177e4SLinus Torvalds 		r = -EINVAL;
9771da177e4SLinus Torvalds 		goto out;
9781da177e4SLinus Torvalds 	}
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
9811da177e4SLinus Torvalds 	if (r)
9821da177e4SLinus Torvalds 		goto out;
9831da177e4SLinus Torvalds 
9846680073dSKiyoshi Ueda 	pgpath->is_active = 1;
9851da177e4SLinus Torvalds 
986e54f77ddSChandra Seetharaman 	if (!m->nr_valid_paths++ && m->queue_size) {
9871da177e4SLinus Torvalds 		m->current_pgpath = NULL;
988c557308eSAlasdair G Kergon 		queue_work(kmultipathd, &m->process_queued_ios);
989e54f77ddSChandra Seetharaman 	} else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
990e54f77ddSChandra Seetharaman 		if (queue_work(kmpath_handlerd, &pgpath->activate_path))
991e54f77ddSChandra Seetharaman 			m->pg_init_in_progress++;
992e54f77ddSChandra Seetharaman 	}
9931da177e4SLinus Torvalds 
994b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
995b15546f9SMike Anderson 		      pgpath->path.dev->name, m->nr_valid_paths);
996b15546f9SMike Anderson 
997fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds out:
10001da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	return r;
10031da177e4SLinus Torvalds }
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds /*
10061da177e4SLinus Torvalds  * Fail or reinstate all paths that match the provided struct dm_dev.
10071da177e4SLinus Torvalds  */
10081da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev,
10091da177e4SLinus Torvalds 		      action_fn action)
10101da177e4SLinus Torvalds {
10111da177e4SLinus Torvalds 	int r = 0;
10121da177e4SLinus Torvalds 	struct pgpath *pgpath;
10131da177e4SLinus Torvalds 	struct priority_group *pg;
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
10161da177e4SLinus Torvalds 		list_for_each_entry(pgpath, &pg->pgpaths, list) {
10171da177e4SLinus Torvalds 			if (pgpath->path.dev == dev)
10181da177e4SLinus Torvalds 				r = action(pgpath);
10191da177e4SLinus Torvalds 		}
10201da177e4SLinus Torvalds 	}
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	return r;
10231da177e4SLinus Torvalds }
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds /*
10261da177e4SLinus Torvalds  * Temporarily try to avoid having to use the specified PG
10271da177e4SLinus Torvalds  */
10281da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg,
10291da177e4SLinus Torvalds 		      int bypassed)
10301da177e4SLinus Torvalds {
10311da177e4SLinus Torvalds 	unsigned long flags;
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	pg->bypassed = bypassed;
10361da177e4SLinus Torvalds 	m->current_pgpath = NULL;
10371da177e4SLinus Torvalds 	m->current_pg = NULL;
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
10401da177e4SLinus Torvalds 
1041fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
10421da177e4SLinus Torvalds }
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds /*
10451da177e4SLinus Torvalds  * Switch to using the specified PG from the next I/O that gets mapped
10461da177e4SLinus Torvalds  */
10471da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr)
10481da177e4SLinus Torvalds {
10491da177e4SLinus Torvalds 	struct priority_group *pg;
10501da177e4SLinus Torvalds 	unsigned pgnum;
10511da177e4SLinus Torvalds 	unsigned long flags;
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 	if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 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;
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
10851da177e4SLinus Torvalds 	    (pgnum > m->nr_priority_groups)) {
10861da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to bypass_pg");
10871da177e4SLinus Torvalds 		return -EINVAL;
10881da177e4SLinus Torvalds 	}
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
10911da177e4SLinus Torvalds 		if (!--pgnum)
10921da177e4SLinus Torvalds 			break;
10931da177e4SLinus Torvalds 	}
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 	bypass_pg(m, pg, bypassed);
10961da177e4SLinus Torvalds 	return 0;
10971da177e4SLinus Torvalds }
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds /*
1100c9e45581SDave Wysochanski  * Should we retry pg_init immediately?
1101c9e45581SDave Wysochanski  */
1102c9e45581SDave Wysochanski static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1103c9e45581SDave Wysochanski {
1104c9e45581SDave Wysochanski 	unsigned long flags;
1105c9e45581SDave Wysochanski 	int limit_reached = 0;
1106c9e45581SDave Wysochanski 
1107c9e45581SDave Wysochanski 	spin_lock_irqsave(&m->lock, flags);
1108c9e45581SDave Wysochanski 
1109c9e45581SDave Wysochanski 	if (m->pg_init_count <= m->pg_init_retries)
1110c9e45581SDave Wysochanski 		m->pg_init_required = 1;
1111c9e45581SDave Wysochanski 	else
1112c9e45581SDave Wysochanski 		limit_reached = 1;
1113c9e45581SDave Wysochanski 
1114c9e45581SDave Wysochanski 	spin_unlock_irqrestore(&m->lock, flags);
1115c9e45581SDave Wysochanski 
1116c9e45581SDave Wysochanski 	return limit_reached;
1117c9e45581SDave Wysochanski }
1118c9e45581SDave Wysochanski 
1119cfae5c9bSChandra Seetharaman static void pg_init_done(struct dm_path *path, int errors)
1120cfae5c9bSChandra Seetharaman {
1121cfae5c9bSChandra Seetharaman 	struct pgpath *pgpath = path_to_pgpath(path);
1122cfae5c9bSChandra Seetharaman 	struct priority_group *pg = pgpath->pg;
1123cfae5c9bSChandra Seetharaman 	struct multipath *m = pg->m;
1124cfae5c9bSChandra Seetharaman 	unsigned long flags;
1125cfae5c9bSChandra Seetharaman 
1126cfae5c9bSChandra Seetharaman 	/* device or driver problems */
1127cfae5c9bSChandra Seetharaman 	switch (errors) {
1128cfae5c9bSChandra Seetharaman 	case SCSI_DH_OK:
1129cfae5c9bSChandra Seetharaman 		break;
1130cfae5c9bSChandra Seetharaman 	case SCSI_DH_NOSYS:
1131cfae5c9bSChandra Seetharaman 		if (!m->hw_handler_name) {
1132cfae5c9bSChandra Seetharaman 			errors = 0;
1133cfae5c9bSChandra Seetharaman 			break;
1134cfae5c9bSChandra Seetharaman 		}
1135cfae5c9bSChandra Seetharaman 		DMERR("Cannot failover device because scsi_dh_%s was not "
1136cfae5c9bSChandra Seetharaman 		      "loaded.", m->hw_handler_name);
1137cfae5c9bSChandra Seetharaman 		/*
1138cfae5c9bSChandra Seetharaman 		 * Fail path for now, so we do not ping pong
1139cfae5c9bSChandra Seetharaman 		 */
1140cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1141cfae5c9bSChandra Seetharaman 		break;
1142cfae5c9bSChandra Seetharaman 	case SCSI_DH_DEV_TEMP_BUSY:
1143cfae5c9bSChandra Seetharaman 		/*
1144cfae5c9bSChandra Seetharaman 		 * Probably doing something like FW upgrade on the
1145cfae5c9bSChandra Seetharaman 		 * controller so try the other pg.
1146cfae5c9bSChandra Seetharaman 		 */
1147cfae5c9bSChandra Seetharaman 		bypass_pg(m, pg, 1);
1148cfae5c9bSChandra Seetharaman 		break;
1149cfae5c9bSChandra Seetharaman 	/* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1150cfae5c9bSChandra Seetharaman 	case SCSI_DH_RETRY:
1151cfae5c9bSChandra Seetharaman 	case SCSI_DH_IMM_RETRY:
1152cfae5c9bSChandra Seetharaman 	case SCSI_DH_RES_TEMP_UNAVAIL:
1153cfae5c9bSChandra Seetharaman 		if (pg_init_limit_reached(m, pgpath))
1154cfae5c9bSChandra Seetharaman 			fail_path(pgpath);
1155cfae5c9bSChandra Seetharaman 		errors = 0;
1156cfae5c9bSChandra Seetharaman 		break;
1157cfae5c9bSChandra Seetharaman 	default:
1158cfae5c9bSChandra Seetharaman 		/*
1159cfae5c9bSChandra Seetharaman 		 * We probably do not want to fail the path for a device
1160cfae5c9bSChandra Seetharaman 		 * error, but this is what the old dm did. In future
1161cfae5c9bSChandra Seetharaman 		 * patches we can do more advanced handling.
1162cfae5c9bSChandra Seetharaman 		 */
1163cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1164cfae5c9bSChandra Seetharaman 	}
1165cfae5c9bSChandra Seetharaman 
1166cfae5c9bSChandra Seetharaman 	spin_lock_irqsave(&m->lock, flags);
1167cfae5c9bSChandra Seetharaman 	if (errors) {
1168e54f77ddSChandra Seetharaman 		if (pgpath == m->current_pgpath) {
1169cfae5c9bSChandra Seetharaman 			DMERR("Could not failover device. Error %d.", errors);
1170cfae5c9bSChandra Seetharaman 			m->current_pgpath = NULL;
1171cfae5c9bSChandra Seetharaman 			m->current_pg = NULL;
1172e54f77ddSChandra Seetharaman 		}
1173cfae5c9bSChandra Seetharaman 	} else if (!m->pg_init_required) {
1174cfae5c9bSChandra Seetharaman 		m->queue_io = 0;
1175cfae5c9bSChandra Seetharaman 		pg->bypassed = 0;
1176cfae5c9bSChandra Seetharaman 	}
1177cfae5c9bSChandra Seetharaman 
1178e54f77ddSChandra Seetharaman 	m->pg_init_in_progress--;
1179e54f77ddSChandra Seetharaman 	if (!m->pg_init_in_progress)
1180cfae5c9bSChandra Seetharaman 		queue_work(kmultipathd, &m->process_queued_ios);
1181cfae5c9bSChandra Seetharaman 	spin_unlock_irqrestore(&m->lock, flags);
1182cfae5c9bSChandra Seetharaman }
1183cfae5c9bSChandra Seetharaman 
1184bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work)
1185bab7cfc7SChandra Seetharaman {
1186bab7cfc7SChandra Seetharaman 	int ret;
1187e54f77ddSChandra Seetharaman 	struct pgpath *pgpath =
1188e54f77ddSChandra Seetharaman 		container_of(work, struct pgpath, activate_path);
1189bab7cfc7SChandra Seetharaman 
1190e54f77ddSChandra Seetharaman 	ret = scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev));
1191e54f77ddSChandra Seetharaman 	pg_init_done(&pgpath->path, ret);
1192bab7cfc7SChandra Seetharaman }
1193bab7cfc7SChandra Seetharaman 
11941da177e4SLinus Torvalds /*
11951da177e4SLinus Torvalds  * end_io handling
11961da177e4SLinus Torvalds  */
1197f40c67f0SKiyoshi Ueda static int do_end_io(struct multipath *m, struct request *clone,
1198028867acSAlasdair G Kergon 		     int error, struct dm_mpath_io *mpio)
11991da177e4SLinus Torvalds {
1200f40c67f0SKiyoshi Ueda 	/*
1201f40c67f0SKiyoshi Ueda 	 * We don't queue any clone request inside the multipath target
1202f40c67f0SKiyoshi Ueda 	 * during end I/O handling, since those clone requests don't have
1203f40c67f0SKiyoshi Ueda 	 * bio clones.  If we queue them inside the multipath target,
1204f40c67f0SKiyoshi Ueda 	 * we need to make bio clones, that requires memory allocation.
1205f40c67f0SKiyoshi Ueda 	 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1206f40c67f0SKiyoshi Ueda 	 *  don't have bio clones.)
1207f40c67f0SKiyoshi Ueda 	 * Instead of queueing the clone request here, we queue the original
1208f40c67f0SKiyoshi Ueda 	 * request into dm core, which will remake a clone request and
1209f40c67f0SKiyoshi Ueda 	 * clone bios for it and resubmit it later.
1210f40c67f0SKiyoshi Ueda 	 */
1211f40c67f0SKiyoshi Ueda 	int r = DM_ENDIO_REQUEUE;
1212640eb3b0SStefan Bader 	unsigned long flags;
12131da177e4SLinus Torvalds 
1214f40c67f0SKiyoshi Ueda 	if (!error && !clone->errors)
12151da177e4SLinus Torvalds 		return 0;	/* I/O complete */
12161da177e4SLinus Torvalds 
1217f6a80ea8SAlasdair G Kergon 	if (error == -EOPNOTSUPP)
1218f6a80ea8SAlasdair G Kergon 		return error;
1219f6a80ea8SAlasdair G Kergon 
1220cfae5c9bSChandra Seetharaman 	if (mpio->pgpath)
12211da177e4SLinus Torvalds 		fail_path(mpio->pgpath);
12221da177e4SLinus Torvalds 
1223640eb3b0SStefan Bader 	spin_lock_irqsave(&m->lock, flags);
1224f40c67f0SKiyoshi Ueda 	if (!m->nr_valid_paths && !m->queue_if_no_path && !__must_push_back(m))
1225f40c67f0SKiyoshi Ueda 		r = -EIO;
1226640eb3b0SStefan Bader 	spin_unlock_irqrestore(&m->lock, flags);
12271da177e4SLinus Torvalds 
1228f40c67f0SKiyoshi Ueda 	return r;
12291da177e4SLinus Torvalds }
12301da177e4SLinus Torvalds 
1231f40c67f0SKiyoshi Ueda static int multipath_end_io(struct dm_target *ti, struct request *clone,
12321da177e4SLinus Torvalds 			    int error, union map_info *map_context)
12331da177e4SLinus Torvalds {
1234028867acSAlasdair G Kergon 	struct multipath *m = ti->private;
1235028867acSAlasdair G Kergon 	struct dm_mpath_io *mpio = map_context->ptr;
12361da177e4SLinus Torvalds 	struct pgpath *pgpath = mpio->pgpath;
12371da177e4SLinus Torvalds 	struct path_selector *ps;
12381da177e4SLinus Torvalds 	int r;
12391da177e4SLinus Torvalds 
1240f40c67f0SKiyoshi Ueda 	r  = do_end_io(m, clone, error, mpio);
12411da177e4SLinus Torvalds 	if (pgpath) {
12421da177e4SLinus Torvalds 		ps = &pgpath->pg->ps;
12431da177e4SLinus Torvalds 		if (ps->type->end_io)
124402ab823fSKiyoshi Ueda 			ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
12451da177e4SLinus Torvalds 	}
12461da177e4SLinus Torvalds 	mempool_free(mpio, m->mpio_pool);
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds 	return r;
12491da177e4SLinus Torvalds }
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds /*
12521da177e4SLinus Torvalds  * Suspend can't complete until all the I/O is processed so if
1253436d4108SAlasdair G Kergon  * the last path fails we must error any remaining I/O.
1254436d4108SAlasdair G Kergon  * Note that if the freeze_bdev fails while suspending, the
1255436d4108SAlasdair G Kergon  * queue_if_no_path state is lost - userspace should reset it.
12561da177e4SLinus Torvalds  */
12571da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti)
12581da177e4SLinus Torvalds {
12591da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
12601da177e4SLinus Torvalds 
1261485ef69eSAlasdair G Kergon 	queue_if_no_path(m, 0, 1);
12621da177e4SLinus Torvalds }
12631da177e4SLinus Torvalds 
1264436d4108SAlasdair G Kergon /*
1265436d4108SAlasdair G Kergon  * Restore the queue_if_no_path setting.
1266436d4108SAlasdair G Kergon  */
12671da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti)
12681da177e4SLinus Torvalds {
12691da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
12701da177e4SLinus Torvalds 	unsigned long flags;
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
1273436d4108SAlasdair G Kergon 	m->queue_if_no_path = m->saved_queue_if_no_path;
12741da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
12751da177e4SLinus Torvalds }
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds /*
12781da177e4SLinus Torvalds  * Info output has the following format:
12791da177e4SLinus Torvalds  * num_multipath_feature_args [multipath_feature_args]*
12801da177e4SLinus Torvalds  * num_handler_status_args [handler_status_args]*
12811da177e4SLinus Torvalds  * num_groups init_group_number
12821da177e4SLinus Torvalds  *            [A|D|E num_ps_status_args [ps_status_args]*
12831da177e4SLinus Torvalds  *             num_paths num_selector_args
12841da177e4SLinus Torvalds  *             [path_dev A|F fail_count [selector_args]* ]+ ]+
12851da177e4SLinus Torvalds  *
12861da177e4SLinus Torvalds  * Table output has the following format (identical to the constructor string):
12871da177e4SLinus Torvalds  * num_feature_args [features_args]*
12881da177e4SLinus Torvalds  * num_handler_args hw_handler [hw_handler_args]*
12891da177e4SLinus Torvalds  * num_groups init_group_number
12901da177e4SLinus Torvalds  *     [priority selector-name num_ps_args [ps_args]*
12911da177e4SLinus Torvalds  *      num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
12921da177e4SLinus Torvalds  */
12931da177e4SLinus Torvalds static int multipath_status(struct dm_target *ti, status_type_t type,
12941da177e4SLinus Torvalds 			    char *result, unsigned int maxlen)
12951da177e4SLinus Torvalds {
12961da177e4SLinus Torvalds 	int sz = 0;
12971da177e4SLinus Torvalds 	unsigned long flags;
12981da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
12991da177e4SLinus Torvalds 	struct priority_group *pg;
13001da177e4SLinus Torvalds 	struct pgpath *p;
13011da177e4SLinus Torvalds 	unsigned pg_num;
13021da177e4SLinus Torvalds 	char state;
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 	/* Features */
13071da177e4SLinus Torvalds 	if (type == STATUSTYPE_INFO)
1308c9e45581SDave Wysochanski 		DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1309c9e45581SDave Wysochanski 	else {
1310c9e45581SDave Wysochanski 		DMEMIT("%u ", m->queue_if_no_path +
1311c9e45581SDave Wysochanski 			      (m->pg_init_retries > 0) * 2);
1312c9e45581SDave Wysochanski 		if (m->queue_if_no_path)
1313c9e45581SDave Wysochanski 			DMEMIT("queue_if_no_path ");
1314c9e45581SDave Wysochanski 		if (m->pg_init_retries)
1315c9e45581SDave Wysochanski 			DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1316c9e45581SDave Wysochanski 	}
13171da177e4SLinus Torvalds 
1318cfae5c9bSChandra Seetharaman 	if (!m->hw_handler_name || type == STATUSTYPE_INFO)
13191da177e4SLinus Torvalds 		DMEMIT("0 ");
13201da177e4SLinus Torvalds 	else
1321cfae5c9bSChandra Seetharaman 		DMEMIT("1 %s ", m->hw_handler_name);
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 	DMEMIT("%u ", m->nr_priority_groups);
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 	if (m->next_pg)
13261da177e4SLinus Torvalds 		pg_num = m->next_pg->pg_num;
13271da177e4SLinus Torvalds 	else if (m->current_pg)
13281da177e4SLinus Torvalds 		pg_num = m->current_pg->pg_num;
13291da177e4SLinus Torvalds 	else
13301da177e4SLinus Torvalds 			pg_num = 1;
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds 	DMEMIT("%u ", pg_num);
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 	switch (type) {
13351da177e4SLinus Torvalds 	case STATUSTYPE_INFO:
13361da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
13371da177e4SLinus Torvalds 			if (pg->bypassed)
13381da177e4SLinus Torvalds 				state = 'D';	/* Disabled */
13391da177e4SLinus Torvalds 			else if (pg == m->current_pg)
13401da177e4SLinus Torvalds 				state = 'A';	/* Currently Active */
13411da177e4SLinus Torvalds 			else
13421da177e4SLinus Torvalds 				state = 'E';	/* Enabled */
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds 			DMEMIT("%c ", state);
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 			if (pg->ps.type->status)
13471da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
13481da177e4SLinus Torvalds 							  result + sz,
13491da177e4SLinus Torvalds 							  maxlen - sz);
13501da177e4SLinus Torvalds 			else
13511da177e4SLinus Torvalds 				DMEMIT("0 ");
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
13541da177e4SLinus Torvalds 			       pg->ps.type->info_args);
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
13571da177e4SLinus Torvalds 				DMEMIT("%s %s %u ", p->path.dev->name,
13586680073dSKiyoshi Ueda 				       p->is_active ? "A" : "F",
13591da177e4SLinus Torvalds 				       p->fail_count);
13601da177e4SLinus Torvalds 				if (pg->ps.type->status)
13611da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
13621da177e4SLinus Torvalds 					      &p->path, type, result + sz,
13631da177e4SLinus Torvalds 					      maxlen - sz);
13641da177e4SLinus Torvalds 			}
13651da177e4SLinus Torvalds 		}
13661da177e4SLinus Torvalds 		break;
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	case STATUSTYPE_TABLE:
13691da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
13701da177e4SLinus Torvalds 			DMEMIT("%s ", pg->ps.type->name);
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds 			if (pg->ps.type->status)
13731da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
13741da177e4SLinus Torvalds 							  result + sz,
13751da177e4SLinus Torvalds 							  maxlen - sz);
13761da177e4SLinus Torvalds 			else
13771da177e4SLinus Torvalds 				DMEMIT("0 ");
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
13801da177e4SLinus Torvalds 			       pg->ps.type->table_args);
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
13831da177e4SLinus Torvalds 				DMEMIT("%s ", p->path.dev->name);
13841da177e4SLinus Torvalds 				if (pg->ps.type->status)
13851da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
13861da177e4SLinus Torvalds 					      &p->path, type, result + sz,
13871da177e4SLinus Torvalds 					      maxlen - sz);
13881da177e4SLinus Torvalds 			}
13891da177e4SLinus Torvalds 		}
13901da177e4SLinus Torvalds 		break;
13911da177e4SLinus Torvalds 	}
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds 	return 0;
13961da177e4SLinus Torvalds }
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
13991da177e4SLinus Torvalds {
14001da177e4SLinus Torvalds 	int r;
14011da177e4SLinus Torvalds 	struct dm_dev *dev;
14021da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
14031da177e4SLinus Torvalds 	action_fn action;
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	if (argc == 1) {
14061da177e4SLinus Torvalds 		if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
1407485ef69eSAlasdair G Kergon 			return queue_if_no_path(m, 1, 0);
14081da177e4SLinus Torvalds 		else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
1409485ef69eSAlasdair G Kergon 			return queue_if_no_path(m, 0, 0);
14101da177e4SLinus Torvalds 	}
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds 	if (argc != 2)
14131da177e4SLinus Torvalds 		goto error;
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 	if (!strnicmp(argv[0], MESG_STR("disable_group")))
14161da177e4SLinus Torvalds 		return bypass_pg_num(m, argv[1], 1);
14171da177e4SLinus Torvalds 	else if (!strnicmp(argv[0], MESG_STR("enable_group")))
14181da177e4SLinus Torvalds 		return bypass_pg_num(m, argv[1], 0);
14191da177e4SLinus Torvalds 	else if (!strnicmp(argv[0], MESG_STR("switch_group")))
14201da177e4SLinus Torvalds 		return switch_pg_num(m, argv[1]);
14211da177e4SLinus Torvalds 	else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
14221da177e4SLinus Torvalds 		action = reinstate_path;
14231da177e4SLinus Torvalds 	else if (!strnicmp(argv[0], MESG_STR("fail_path")))
14241da177e4SLinus Torvalds 		action = fail_path;
14251da177e4SLinus Torvalds 	else
14261da177e4SLinus Torvalds 		goto error;
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 	r = dm_get_device(ti, argv[1], ti->begin, ti->len,
14291da177e4SLinus Torvalds 			  dm_table_get_mode(ti->table), &dev);
14301da177e4SLinus Torvalds 	if (r) {
143172d94861SAlasdair G Kergon 		DMWARN("message: error getting device %s",
14321da177e4SLinus Torvalds 		       argv[1]);
14331da177e4SLinus Torvalds 		return -EINVAL;
14341da177e4SLinus Torvalds 	}
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds 	r = action_dev(m, dev, action);
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 	dm_put_device(ti, dev);
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds 	return r;
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds error:
14431da177e4SLinus Torvalds 	DMWARN("Unrecognised multipath message received.");
14441da177e4SLinus Torvalds 	return -EINVAL;
14451da177e4SLinus Torvalds }
14461da177e4SLinus Torvalds 
1447647b3d00SAl Viro static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
14489af4aa30SMilan Broz 			   unsigned long arg)
14499af4aa30SMilan Broz {
14509af4aa30SMilan Broz 	struct multipath *m = (struct multipath *) ti->private;
14519af4aa30SMilan Broz 	struct block_device *bdev = NULL;
1452633a08b8SAl Viro 	fmode_t mode = 0;
14539af4aa30SMilan Broz 	unsigned long flags;
14549af4aa30SMilan Broz 	int r = 0;
14559af4aa30SMilan Broz 
14569af4aa30SMilan Broz 	spin_lock_irqsave(&m->lock, flags);
14579af4aa30SMilan Broz 
14589af4aa30SMilan Broz 	if (!m->current_pgpath)
145902ab823fSKiyoshi Ueda 		__choose_pgpath(m, 0);
14609af4aa30SMilan Broz 
1461e90dae1fSMilan Broz 	if (m->current_pgpath) {
14629af4aa30SMilan Broz 		bdev = m->current_pgpath->path.dev->bdev;
1463633a08b8SAl Viro 		mode = m->current_pgpath->path.dev->mode;
1464e90dae1fSMilan Broz 	}
14659af4aa30SMilan Broz 
14669af4aa30SMilan Broz 	if (m->queue_io)
14679af4aa30SMilan Broz 		r = -EAGAIN;
14689af4aa30SMilan Broz 	else if (!bdev)
14699af4aa30SMilan Broz 		r = -EIO;
14709af4aa30SMilan Broz 
14719af4aa30SMilan Broz 	spin_unlock_irqrestore(&m->lock, flags);
14729af4aa30SMilan Broz 
1473633a08b8SAl Viro 	return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
14749af4aa30SMilan Broz }
14759af4aa30SMilan Broz 
1476af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti,
1477af4874e0SMike Snitzer 				     iterate_devices_callout_fn fn, void *data)
1478af4874e0SMike Snitzer {
1479af4874e0SMike Snitzer 	struct multipath *m = ti->private;
1480af4874e0SMike Snitzer 	struct priority_group *pg;
1481af4874e0SMike Snitzer 	struct pgpath *p;
1482af4874e0SMike Snitzer 	int ret = 0;
1483af4874e0SMike Snitzer 
1484af4874e0SMike Snitzer 	list_for_each_entry(pg, &m->priority_groups, list) {
1485af4874e0SMike Snitzer 		list_for_each_entry(p, &pg->pgpaths, list) {
14865dea271bSMike Snitzer 			ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1487af4874e0SMike Snitzer 			if (ret)
1488af4874e0SMike Snitzer 				goto out;
1489af4874e0SMike Snitzer 		}
1490af4874e0SMike Snitzer 	}
1491af4874e0SMike Snitzer 
1492af4874e0SMike Snitzer out:
1493af4874e0SMike Snitzer 	return ret;
1494af4874e0SMike Snitzer }
1495af4874e0SMike Snitzer 
1496f40c67f0SKiyoshi Ueda static int __pgpath_busy(struct pgpath *pgpath)
1497f40c67f0SKiyoshi Ueda {
1498f40c67f0SKiyoshi Ueda 	struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1499f40c67f0SKiyoshi Ueda 
1500f40c67f0SKiyoshi Ueda 	return dm_underlying_device_busy(q);
1501f40c67f0SKiyoshi Ueda }
1502f40c67f0SKiyoshi Ueda 
1503f40c67f0SKiyoshi Ueda /*
1504f40c67f0SKiyoshi Ueda  * We return "busy", only when we can map I/Os but underlying devices
1505f40c67f0SKiyoshi Ueda  * are busy (so even if we map I/Os now, the I/Os will wait on
1506f40c67f0SKiyoshi Ueda  * the underlying queue).
1507f40c67f0SKiyoshi Ueda  * In other words, if we want to kill I/Os or queue them inside us
1508f40c67f0SKiyoshi Ueda  * due to map unavailability, we don't return "busy".  Otherwise,
1509f40c67f0SKiyoshi Ueda  * dm core won't give us the I/Os and we can't do what we want.
1510f40c67f0SKiyoshi Ueda  */
1511f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti)
1512f40c67f0SKiyoshi Ueda {
1513f40c67f0SKiyoshi Ueda 	int busy = 0, has_active = 0;
1514f40c67f0SKiyoshi Ueda 	struct multipath *m = ti->private;
1515f40c67f0SKiyoshi Ueda 	struct priority_group *pg;
1516f40c67f0SKiyoshi Ueda 	struct pgpath *pgpath;
1517f40c67f0SKiyoshi Ueda 	unsigned long flags;
1518f40c67f0SKiyoshi Ueda 
1519f40c67f0SKiyoshi Ueda 	spin_lock_irqsave(&m->lock, flags);
1520f40c67f0SKiyoshi Ueda 
1521f40c67f0SKiyoshi Ueda 	/* Guess which priority_group will be used at next mapping time */
1522f40c67f0SKiyoshi Ueda 	if (unlikely(!m->current_pgpath && m->next_pg))
1523f40c67f0SKiyoshi Ueda 		pg = m->next_pg;
1524f40c67f0SKiyoshi Ueda 	else if (likely(m->current_pg))
1525f40c67f0SKiyoshi Ueda 		pg = m->current_pg;
1526f40c67f0SKiyoshi Ueda 	else
1527f40c67f0SKiyoshi Ueda 		/*
1528f40c67f0SKiyoshi Ueda 		 * We don't know which pg will be used at next mapping time.
1529f40c67f0SKiyoshi Ueda 		 * We don't call __choose_pgpath() here to avoid to trigger
1530f40c67f0SKiyoshi Ueda 		 * pg_init just by busy checking.
1531f40c67f0SKiyoshi Ueda 		 * So we don't know whether underlying devices we will be using
1532f40c67f0SKiyoshi Ueda 		 * at next mapping time are busy or not. Just try mapping.
1533f40c67f0SKiyoshi Ueda 		 */
1534f40c67f0SKiyoshi Ueda 		goto out;
1535f40c67f0SKiyoshi Ueda 
1536f40c67f0SKiyoshi Ueda 	/*
1537f40c67f0SKiyoshi Ueda 	 * If there is one non-busy active path at least, the path selector
1538f40c67f0SKiyoshi Ueda 	 * will be able to select it. So we consider such a pg as not busy.
1539f40c67f0SKiyoshi Ueda 	 */
1540f40c67f0SKiyoshi Ueda 	busy = 1;
1541f40c67f0SKiyoshi Ueda 	list_for_each_entry(pgpath, &pg->pgpaths, list)
1542f40c67f0SKiyoshi Ueda 		if (pgpath->is_active) {
1543f40c67f0SKiyoshi Ueda 			has_active = 1;
1544f40c67f0SKiyoshi Ueda 
1545f40c67f0SKiyoshi Ueda 			if (!__pgpath_busy(pgpath)) {
1546f40c67f0SKiyoshi Ueda 				busy = 0;
1547f40c67f0SKiyoshi Ueda 				break;
1548f40c67f0SKiyoshi Ueda 			}
1549f40c67f0SKiyoshi Ueda 		}
1550f40c67f0SKiyoshi Ueda 
1551f40c67f0SKiyoshi Ueda 	if (!has_active)
1552f40c67f0SKiyoshi Ueda 		/*
1553f40c67f0SKiyoshi Ueda 		 * No active path in this pg, so this pg won't be used and
1554f40c67f0SKiyoshi Ueda 		 * the current_pg will be changed at next mapping time.
1555f40c67f0SKiyoshi Ueda 		 * We need to try mapping to determine it.
1556f40c67f0SKiyoshi Ueda 		 */
1557f40c67f0SKiyoshi Ueda 		busy = 0;
1558f40c67f0SKiyoshi Ueda 
1559f40c67f0SKiyoshi Ueda out:
1560f40c67f0SKiyoshi Ueda 	spin_unlock_irqrestore(&m->lock, flags);
1561f40c67f0SKiyoshi Ueda 
1562f40c67f0SKiyoshi Ueda 	return busy;
1563f40c67f0SKiyoshi Ueda }
1564f40c67f0SKiyoshi Ueda 
15651da177e4SLinus Torvalds /*-----------------------------------------------------------------
15661da177e4SLinus Torvalds  * Module setup
15671da177e4SLinus Torvalds  *---------------------------------------------------------------*/
15681da177e4SLinus Torvalds static struct target_type multipath_target = {
15691da177e4SLinus Torvalds 	.name = "multipath",
1570af4874e0SMike Snitzer 	.version = {1, 1, 0},
15711da177e4SLinus Torvalds 	.module = THIS_MODULE,
15721da177e4SLinus Torvalds 	.ctr = multipath_ctr,
15731da177e4SLinus Torvalds 	.dtr = multipath_dtr,
1574f40c67f0SKiyoshi Ueda 	.map_rq = multipath_map,
1575f40c67f0SKiyoshi Ueda 	.rq_end_io = multipath_end_io,
15761da177e4SLinus Torvalds 	.presuspend = multipath_presuspend,
15771da177e4SLinus Torvalds 	.resume = multipath_resume,
15781da177e4SLinus Torvalds 	.status = multipath_status,
15791da177e4SLinus Torvalds 	.message = multipath_message,
15809af4aa30SMilan Broz 	.ioctl  = multipath_ioctl,
1581af4874e0SMike Snitzer 	.iterate_devices = multipath_iterate_devices,
1582f40c67f0SKiyoshi Ueda 	.busy = multipath_busy,
15831da177e4SLinus Torvalds };
15841da177e4SLinus Torvalds 
15851da177e4SLinus Torvalds static int __init dm_multipath_init(void)
15861da177e4SLinus Torvalds {
15871da177e4SLinus Torvalds 	int r;
15881da177e4SLinus Torvalds 
15891da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
1590028867acSAlasdair G Kergon 	_mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
15911da177e4SLinus Torvalds 	if (!_mpio_cache)
15921da177e4SLinus Torvalds 		return -ENOMEM;
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds 	r = dm_register_target(&multipath_target);
15951da177e4SLinus Torvalds 	if (r < 0) {
15960cd33124SAlasdair G Kergon 		DMERR("register failed %d", r);
15971da177e4SLinus Torvalds 		kmem_cache_destroy(_mpio_cache);
15981da177e4SLinus Torvalds 		return -EINVAL;
15991da177e4SLinus Torvalds 	}
16001da177e4SLinus Torvalds 
1601c557308eSAlasdair G Kergon 	kmultipathd = create_workqueue("kmpathd");
1602c557308eSAlasdair G Kergon 	if (!kmultipathd) {
16030cd33124SAlasdair G Kergon 		DMERR("failed to create workqueue kmpathd");
1604c557308eSAlasdair G Kergon 		dm_unregister_target(&multipath_target);
1605c557308eSAlasdair G Kergon 		kmem_cache_destroy(_mpio_cache);
1606c557308eSAlasdair G Kergon 		return -ENOMEM;
1607c557308eSAlasdair G Kergon 	}
1608c557308eSAlasdair G Kergon 
1609bab7cfc7SChandra Seetharaman 	/*
1610bab7cfc7SChandra Seetharaman 	 * A separate workqueue is used to handle the device handlers
1611bab7cfc7SChandra Seetharaman 	 * to avoid overloading existing workqueue. Overloading the
1612bab7cfc7SChandra Seetharaman 	 * old workqueue would also create a bottleneck in the
1613bab7cfc7SChandra Seetharaman 	 * path of the storage hardware device activation.
1614bab7cfc7SChandra Seetharaman 	 */
1615bab7cfc7SChandra Seetharaman 	kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1616bab7cfc7SChandra Seetharaman 	if (!kmpath_handlerd) {
1617bab7cfc7SChandra Seetharaman 		DMERR("failed to create workqueue kmpath_handlerd");
1618bab7cfc7SChandra Seetharaman 		destroy_workqueue(kmultipathd);
1619bab7cfc7SChandra Seetharaman 		dm_unregister_target(&multipath_target);
1620bab7cfc7SChandra Seetharaman 		kmem_cache_destroy(_mpio_cache);
1621bab7cfc7SChandra Seetharaman 		return -ENOMEM;
1622bab7cfc7SChandra Seetharaman 	}
1623bab7cfc7SChandra Seetharaman 
162472d94861SAlasdair G Kergon 	DMINFO("version %u.%u.%u loaded",
16251da177e4SLinus Torvalds 	       multipath_target.version[0], multipath_target.version[1],
16261da177e4SLinus Torvalds 	       multipath_target.version[2]);
16271da177e4SLinus Torvalds 
16281da177e4SLinus Torvalds 	return r;
16291da177e4SLinus Torvalds }
16301da177e4SLinus Torvalds 
16311da177e4SLinus Torvalds static void __exit dm_multipath_exit(void)
16321da177e4SLinus Torvalds {
1633bab7cfc7SChandra Seetharaman 	destroy_workqueue(kmpath_handlerd);
1634c557308eSAlasdair G Kergon 	destroy_workqueue(kmultipathd);
1635c557308eSAlasdair G Kergon 
163610d3bd09SMikulas Patocka 	dm_unregister_target(&multipath_target);
16371da177e4SLinus Torvalds 	kmem_cache_destroy(_mpio_cache);
16381da177e4SLinus Torvalds }
16391da177e4SLinus Torvalds 
16401da177e4SLinus Torvalds module_init(dm_multipath_init);
16411da177e4SLinus Torvalds module_exit(dm_multipath_exit);
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target");
16441da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
16451da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1646