xref: /openbmc/linux/drivers/md/dm-mpath.c (revision ac75b09fc62df441eee90fecfe9b2a6ca24976f2)
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 
104cc96131SMike Snitzer #include "dm-rq.h"
1176e33fe4SMike Snitzer #include "dm-bio-record.h"
121da177e4SLinus Torvalds #include "dm-path-selector.h"
13b15546f9SMike Anderson #include "dm-uevent.h"
141da177e4SLinus Torvalds 
15e5863d9aSMike Snitzer #include <linux/blkdev.h>
161da177e4SLinus Torvalds #include <linux/ctype.h>
171da177e4SLinus Torvalds #include <linux/init.h>
181da177e4SLinus Torvalds #include <linux/mempool.h>
191da177e4SLinus Torvalds #include <linux/module.h>
201da177e4SLinus Torvalds #include <linux/pagemap.h>
211da177e4SLinus Torvalds #include <linux/slab.h>
221da177e4SLinus Torvalds #include <linux/time.h>
23be240ff5SAnatol Pomazau #include <linux/timer.h>
241da177e4SLinus Torvalds #include <linux/workqueue.h>
2535991652SMikulas Patocka #include <linux/delay.h>
26cfae5c9bSChandra Seetharaman #include <scsi/scsi_dh.h>
2760063497SArun Sharma #include <linux/atomic.h>
2878ce23b5SMike Snitzer #include <linux/blk-mq.h>
291da177e4SLinus Torvalds 
3072d94861SAlasdair G Kergon #define DM_MSG_PREFIX "multipath"
314e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_MSECS 2000
324e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
33be240ff5SAnatol Pomazau #define QUEUE_IF_NO_PATH_TIMEOUT_DEFAULT 0
34be240ff5SAnatol Pomazau 
35be240ff5SAnatol Pomazau static unsigned long queue_if_no_path_timeout_secs = QUEUE_IF_NO_PATH_TIMEOUT_DEFAULT;
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds /* Path properties */
381da177e4SLinus Torvalds struct pgpath {
391da177e4SLinus Torvalds 	struct list_head list;
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds 	struct priority_group *pg;	/* Owning PG */
421da177e4SLinus Torvalds 	unsigned fail_count;		/* Cumulative failure count */
431da177e4SLinus Torvalds 
44c922d5f7SJosef "Jeff" Sipek 	struct dm_path path;
454e2d19e4SChandra Seetharaman 	struct delayed_work activate_path;
46be7d31ccSMike Snitzer 
47be7d31ccSMike Snitzer 	bool is_active:1;		/* Path status */
481da177e4SLinus Torvalds };
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds /*
531da177e4SLinus Torvalds  * Paths are grouped into Priority Groups and numbered from 1 upwards.
541da177e4SLinus Torvalds  * Each has a path selector which controls which path gets used.
551da177e4SLinus Torvalds  */
561da177e4SLinus Torvalds struct priority_group {
571da177e4SLinus Torvalds 	struct list_head list;
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds 	struct multipath *m;		/* Owning multipath instance */
601da177e4SLinus Torvalds 	struct path_selector ps;
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds 	unsigned pg_num;		/* Reference number */
631da177e4SLinus Torvalds 	unsigned nr_pgpaths;		/* Number of paths in PG */
641da177e4SLinus Torvalds 	struct list_head pgpaths;
65be7d31ccSMike Snitzer 
66be7d31ccSMike Snitzer 	bool bypassed:1;		/* Temporarily bypass this PG? */
671da177e4SLinus Torvalds };
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /* Multipath context */
701da177e4SLinus Torvalds struct multipath {
71848b8aefSMike Snitzer 	unsigned long flags;		/* Multipath state flags */
724e2d19e4SChandra Seetharaman 
731fbdd2b3SMike Snitzer 	spinlock_t lock;
74848b8aefSMike Snitzer 	enum dm_queue_mode queue_mode;
754e2d19e4SChandra Seetharaman 
761da177e4SLinus Torvalds 	struct pgpath *current_pgpath;
771da177e4SLinus Torvalds 	struct priority_group *current_pg;
781da177e4SLinus Torvalds 	struct priority_group *next_pg;	/* Switch to this PG if set */
791da177e4SLinus Torvalds 
80848b8aefSMike Snitzer 	atomic_t nr_valid_paths;	/* Total number of usable paths */
81848b8aefSMike Snitzer 	unsigned nr_priority_groups;
82848b8aefSMike Snitzer 	struct list_head priority_groups;
831fbdd2b3SMike Snitzer 
84848b8aefSMike Snitzer 	const char *hw_handler_name;
85848b8aefSMike Snitzer 	char *hw_handler_params;
86848b8aefSMike Snitzer 	wait_queue_head_t pg_init_wait;	/* Wait for pg_init completion */
87c9e45581SDave Wysochanski 	unsigned pg_init_retries;	/* Number of times to retry pg_init */
884e2d19e4SChandra Seetharaman 	unsigned pg_init_delay_msecs;	/* Number of msecs before pg_init retry */
8991e968aaSMike Snitzer 	atomic_t pg_init_in_progress;	/* Only one pg_init allowed at once */
9091e968aaSMike Snitzer 	atomic_t pg_init_count;		/* Number of times pg_init called */
9191e968aaSMike Snitzer 
926380f26fSMike Anderson 	struct mutex work_mutex;
9320800cb3SMike Snitzer 	struct work_struct trigger_event;
94848b8aefSMike Snitzer 	struct dm_target *ti;
9576e33fe4SMike Snitzer 
9676e33fe4SMike Snitzer 	struct work_struct process_queued_bios;
9776e33fe4SMike Snitzer 	struct bio_list queued_bios;
98be240ff5SAnatol Pomazau 
99be240ff5SAnatol Pomazau 	struct timer_list nopath_timer;	/* Timeout for queue_if_no_path */
1001da177e4SLinus Torvalds };
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds /*
10376e33fe4SMike Snitzer  * Context information attached to each io we process.
1041da177e4SLinus Torvalds  */
105028867acSAlasdair G Kergon struct dm_mpath_io {
1061da177e4SLinus Torvalds 	struct pgpath *pgpath;
10702ab823fSKiyoshi Ueda 	size_t nr_bytes;
1081da177e4SLinus Torvalds };
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds typedef int (*action_fn) (struct pgpath *pgpath);
1111da177e4SLinus Torvalds 
112bab7cfc7SChandra Seetharaman static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
113c4028958SDavid Howells static void trigger_event(struct work_struct *work);
11489bfce76SBart Van Assche static void activate_or_offline_path(struct pgpath *pgpath);
11589bfce76SBart Van Assche static void activate_path_work(struct work_struct *work);
11676e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work);
117be240ff5SAnatol Pomazau static void queue_if_no_path_timeout_work(struct timer_list *t);
1181da177e4SLinus Torvalds 
119518257b1SMike Snitzer /*-----------------------------------------------
120518257b1SMike Snitzer  * Multipath state flags.
121518257b1SMike Snitzer  *-----------------------------------------------*/
122518257b1SMike Snitzer 
123518257b1SMike Snitzer #define MPATHF_QUEUE_IO 0			/* Must we queue all I/O? */
124518257b1SMike Snitzer #define MPATHF_QUEUE_IF_NO_PATH 1		/* Queue I/O if last path fails? */
125518257b1SMike Snitzer #define MPATHF_SAVED_QUEUE_IF_NO_PATH 2		/* Saved state during suspension */
126518257b1SMike Snitzer #define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3	/* If there's already a hw_handler present, don't change it. */
127518257b1SMike Snitzer #define MPATHF_PG_INIT_DISABLED 4		/* pg_init is not currently allowed */
128518257b1SMike Snitzer #define MPATHF_PG_INIT_REQUIRED 5		/* pg_init needs calling? */
129518257b1SMike Snitzer #define MPATHF_PG_INIT_DELAY_RETRY 6		/* Delay pg_init retry? */
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds /*-----------------------------------------------
1321da177e4SLinus Torvalds  * Allocation routines
1331da177e4SLinus Torvalds  *-----------------------------------------------*/
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds static struct pgpath *alloc_pgpath(void)
1361da177e4SLinus Torvalds {
137e69fae56SMicha³ Miros³aw 	struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
1381da177e4SLinus Torvalds 
139848b8aefSMike Snitzer 	if (!pgpath)
140848b8aefSMike Snitzer 		return NULL;
141848b8aefSMike Snitzer 
142be7d31ccSMike Snitzer 	pgpath->is_active = true;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	return pgpath;
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds 
147028867acSAlasdair G Kergon static void free_pgpath(struct pgpath *pgpath)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	kfree(pgpath);
1501da177e4SLinus Torvalds }
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds static struct priority_group *alloc_priority_group(void)
1531da177e4SLinus Torvalds {
1541da177e4SLinus Torvalds 	struct priority_group *pg;
1551da177e4SLinus Torvalds 
156e69fae56SMicha³ Miros³aw 	pg = kzalloc(sizeof(*pg), GFP_KERNEL);
1571da177e4SLinus Torvalds 
158e69fae56SMicha³ Miros³aw 	if (pg)
1591da177e4SLinus Torvalds 		INIT_LIST_HEAD(&pg->pgpaths);
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	return pg;
1621da177e4SLinus Torvalds }
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
1651da177e4SLinus Torvalds {
1661da177e4SLinus Torvalds 	struct pgpath *pgpath, *tmp;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
1691da177e4SLinus Torvalds 		list_del(&pgpath->list);
1701da177e4SLinus Torvalds 		dm_put_device(ti, pgpath->path.dev);
1711da177e4SLinus Torvalds 		free_pgpath(pgpath);
1721da177e4SLinus Torvalds 	}
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds static void free_priority_group(struct priority_group *pg,
1761da177e4SLinus Torvalds 				struct dm_target *ti)
1771da177e4SLinus Torvalds {
1781da177e4SLinus Torvalds 	struct path_selector *ps = &pg->ps;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	if (ps->type) {
1811da177e4SLinus Torvalds 		ps->type->destroy(ps);
1821da177e4SLinus Torvalds 		dm_put_path_selector(ps->type);
1831da177e4SLinus Torvalds 	}
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	free_pgpaths(&pg->pgpaths, ti);
1861da177e4SLinus Torvalds 	kfree(pg);
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
189e83068a5SMike Snitzer 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);
1961da177e4SLinus Torvalds 		spin_lock_init(&m->lock);
19791e968aaSMike Snitzer 		atomic_set(&m->nr_valid_paths, 0);
198c4028958SDavid Howells 		INIT_WORK(&m->trigger_event, trigger_event);
1996380f26fSMike Anderson 		mutex_init(&m->work_mutex);
2008637a6bfSMike Snitzer 
201e83068a5SMike Snitzer 		m->queue_mode = DM_TYPE_NONE;
202e83068a5SMike Snitzer 
203e83068a5SMike Snitzer 		m->ti = ti;
204e83068a5SMike Snitzer 		ti->private = m;
205be240ff5SAnatol Pomazau 
206be240ff5SAnatol Pomazau 		timer_setup(&m->nopath_timer, queue_if_no_path_timeout_work, 0);
207e83068a5SMike Snitzer 	}
208e83068a5SMike Snitzer 
209e83068a5SMike Snitzer 	return m;
210e83068a5SMike Snitzer }
211e83068a5SMike Snitzer 
212e83068a5SMike Snitzer static int alloc_multipath_stage2(struct dm_target *ti, struct multipath *m)
213e83068a5SMike Snitzer {
214e83068a5SMike Snitzer 	if (m->queue_mode == DM_TYPE_NONE) {
215953923c0SMike Snitzer 		m->queue_mode = DM_TYPE_REQUEST_BASED;
2168d47e659SMike Snitzer 	} else if (m->queue_mode == DM_TYPE_BIO_BASED) {
21776e33fe4SMike Snitzer 		INIT_WORK(&m->process_queued_bios, process_queued_bios);
21876e33fe4SMike Snitzer 		/*
21976e33fe4SMike Snitzer 		 * bio-based doesn't support any direct scsi_dh management;
22076e33fe4SMike Snitzer 		 * it just discovers if a scsi_dh is attached.
22176e33fe4SMike Snitzer 		 */
22276e33fe4SMike Snitzer 		set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
22376e33fe4SMike Snitzer 	}
22476e33fe4SMike Snitzer 
225e83068a5SMike Snitzer 	dm_table_set_type(ti->table, m->queue_mode);
2261da177e4SLinus Torvalds 
227c3736674SMike Snitzer 	/*
228c3736674SMike Snitzer 	 * Init fields that are only used when a scsi_dh is attached
229c3736674SMike Snitzer 	 * - must do this unconditionally (really doesn't hurt non-SCSI uses)
230c3736674SMike Snitzer 	 */
231c3736674SMike Snitzer 	set_bit(MPATHF_QUEUE_IO, &m->flags);
232c3736674SMike Snitzer 	atomic_set(&m->pg_init_in_progress, 0);
233c3736674SMike Snitzer 	atomic_set(&m->pg_init_count, 0);
234c3736674SMike Snitzer 	m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
235c3736674SMike Snitzer 	init_waitqueue_head(&m->pg_init_wait);
236c3736674SMike Snitzer 
237e83068a5SMike Snitzer 	return 0;
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds static void free_multipath(struct multipath *m)
2411da177e4SLinus Torvalds {
2421da177e4SLinus Torvalds 	struct priority_group *pg, *tmp;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
2451da177e4SLinus Torvalds 		list_del(&pg->list);
2461da177e4SLinus Torvalds 		free_priority_group(pg, m->ti);
2471da177e4SLinus Torvalds 	}
2481da177e4SLinus Torvalds 
249cfae5c9bSChandra Seetharaman 	kfree(m->hw_handler_name);
2502bfd2e13SChandra Seetharaman 	kfree(m->hw_handler_params);
251d5ffebddSMike Snitzer 	mutex_destroy(&m->work_mutex);
2521da177e4SLinus Torvalds 	kfree(m);
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
2552eff1924SMike Snitzer static struct dm_mpath_io *get_mpio(union map_info *info)
2562eff1924SMike Snitzer {
2572eff1924SMike Snitzer 	return info->ptr;
2582eff1924SMike Snitzer }
2592eff1924SMike Snitzer 
260bf661be1SMike Snitzer static size_t multipath_per_bio_data_size(void)
26176e33fe4SMike Snitzer {
262bf661be1SMike Snitzer 	return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details);
26376e33fe4SMike Snitzer }
26476e33fe4SMike Snitzer 
265bf661be1SMike Snitzer static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio)
266bf661be1SMike Snitzer {
267bf661be1SMike Snitzer 	return dm_per_bio_data(bio, multipath_per_bio_data_size());
268bf661be1SMike Snitzer }
269bf661be1SMike Snitzer 
270d07a241dSMike Snitzer static struct dm_bio_details *get_bio_details_from_mpio(struct dm_mpath_io *mpio)
271bf661be1SMike Snitzer {
272bf661be1SMike Snitzer 	/* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */
273bf661be1SMike Snitzer 	void *bio_details = mpio + 1;
274bf661be1SMike Snitzer 	return bio_details;
275bf661be1SMike Snitzer }
276bf661be1SMike Snitzer 
27763f6e6fdSMike Snitzer static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p)
27876e33fe4SMike Snitzer {
27976e33fe4SMike Snitzer 	struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
280d07a241dSMike Snitzer 	struct dm_bio_details *bio_details = get_bio_details_from_mpio(mpio);
28176e33fe4SMike Snitzer 
282d0442f80SMike Snitzer 	mpio->nr_bytes = bio->bi_iter.bi_size;
283d0442f80SMike Snitzer 	mpio->pgpath = NULL;
284bf661be1SMike Snitzer 	*mpio_p = mpio;
285d0442f80SMike Snitzer 
286d0442f80SMike Snitzer 	dm_bio_record(bio_details, bio);
28776e33fe4SMike Snitzer }
28876e33fe4SMike Snitzer 
2891da177e4SLinus Torvalds /*-----------------------------------------------
2901da177e4SLinus Torvalds  * Path selection
2911da177e4SLinus Torvalds  *-----------------------------------------------*/
2921da177e4SLinus Torvalds 
2933e9f1be1SHannes Reinecke static int __pg_init_all_paths(struct multipath *m)
294fb612642SKiyoshi Ueda {
295fb612642SKiyoshi Ueda 	struct pgpath *pgpath;
2964e2d19e4SChandra Seetharaman 	unsigned long pg_init_delay = 0;
297fb612642SKiyoshi Ueda 
298b194679fSBart Van Assche 	lockdep_assert_held(&m->lock);
299b194679fSBart Van Assche 
30091e968aaSMike Snitzer 	if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
3013e9f1be1SHannes Reinecke 		return 0;
30217f4ff45SHannes Reinecke 
30391e968aaSMike Snitzer 	atomic_inc(&m->pg_init_count);
304518257b1SMike Snitzer 	clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
3053e9f1be1SHannes Reinecke 
3063e9f1be1SHannes Reinecke 	/* Check here to reset pg_init_required */
3073e9f1be1SHannes Reinecke 	if (!m->current_pg)
3083e9f1be1SHannes Reinecke 		return 0;
3093e9f1be1SHannes Reinecke 
310518257b1SMike Snitzer 	if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags))
3114e2d19e4SChandra Seetharaman 		pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
3124e2d19e4SChandra Seetharaman 						 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
313fb612642SKiyoshi Ueda 	list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
314fb612642SKiyoshi Ueda 		/* Skip failed paths */
315fb612642SKiyoshi Ueda 		if (!pgpath->is_active)
316fb612642SKiyoshi Ueda 			continue;
3174e2d19e4SChandra Seetharaman 		if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
3184e2d19e4SChandra Seetharaman 				       pg_init_delay))
31991e968aaSMike Snitzer 			atomic_inc(&m->pg_init_in_progress);
320fb612642SKiyoshi Ueda 	}
32191e968aaSMike Snitzer 	return atomic_read(&m->pg_init_in_progress);
322fb612642SKiyoshi Ueda }
323fb612642SKiyoshi Ueda 
324c1d7ecf7SBart Van Assche static int pg_init_all_paths(struct multipath *m)
3251da177e4SLinus Torvalds {
326c1d7ecf7SBart Van Assche 	int ret;
3272da1610aSMike Snitzer 	unsigned long flags;
3282da1610aSMike Snitzer 
3292da1610aSMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
330c1d7ecf7SBart Van Assche 	ret = __pg_init_all_paths(m);
3312da1610aSMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
332c1d7ecf7SBart Van Assche 
333c1d7ecf7SBart Van Assche 	return ret;
3342da1610aSMike Snitzer }
3352da1610aSMike Snitzer 
3362da1610aSMike Snitzer static void __switch_pg(struct multipath *m, struct priority_group *pg)
3372da1610aSMike Snitzer {
3382da1610aSMike Snitzer 	m->current_pg = pg;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	/* Must we initialise the PG first, and queue I/O till it's ready? */
341cfae5c9bSChandra Seetharaman 	if (m->hw_handler_name) {
342518257b1SMike Snitzer 		set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
343518257b1SMike Snitzer 		set_bit(MPATHF_QUEUE_IO, &m->flags);
3441da177e4SLinus Torvalds 	} else {
345518257b1SMike Snitzer 		clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
346518257b1SMike Snitzer 		clear_bit(MPATHF_QUEUE_IO, &m->flags);
3471da177e4SLinus Torvalds 	}
348c9e45581SDave Wysochanski 
34991e968aaSMike Snitzer 	atomic_set(&m->pg_init_count, 0);
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
3522da1610aSMike Snitzer static struct pgpath *choose_path_in_pg(struct multipath *m,
3532da1610aSMike Snitzer 					struct priority_group *pg,
35402ab823fSKiyoshi Ueda 					size_t nr_bytes)
3551da177e4SLinus Torvalds {
3562da1610aSMike Snitzer 	unsigned long flags;
357c922d5f7SJosef "Jeff" Sipek 	struct dm_path *path;
3582da1610aSMike Snitzer 	struct pgpath *pgpath;
3591da177e4SLinus Torvalds 
36090a4323cSMike Snitzer 	path = pg->ps.type->select_path(&pg->ps, nr_bytes);
3611da177e4SLinus Torvalds 	if (!path)
3622da1610aSMike Snitzer 		return ERR_PTR(-ENXIO);
3631da177e4SLinus Torvalds 
3642da1610aSMike Snitzer 	pgpath = path_to_pgpath(path);
3651da177e4SLinus Torvalds 
366506458efSWill Deacon 	if (unlikely(READ_ONCE(m->current_pg) != pg)) {
3672da1610aSMike Snitzer 		/* Only update current_pgpath if pg changed */
3682da1610aSMike Snitzer 		spin_lock_irqsave(&m->lock, flags);
3692da1610aSMike Snitzer 		m->current_pgpath = pgpath;
3702da1610aSMike Snitzer 		__switch_pg(m, pg);
3712da1610aSMike Snitzer 		spin_unlock_irqrestore(&m->lock, flags);
3721da177e4SLinus Torvalds 	}
3731da177e4SLinus Torvalds 
3742da1610aSMike Snitzer 	return pgpath;
3752da1610aSMike Snitzer }
3762da1610aSMike Snitzer 
3772da1610aSMike Snitzer static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes)
3781da177e4SLinus Torvalds {
3792da1610aSMike Snitzer 	unsigned long flags;
3801da177e4SLinus Torvalds 	struct priority_group *pg;
3812da1610aSMike Snitzer 	struct pgpath *pgpath;
382d19a55ccSMike Snitzer 	unsigned bypassed = 1;
3831da177e4SLinus Torvalds 
38491e968aaSMike Snitzer 	if (!atomic_read(&m->nr_valid_paths)) {
385518257b1SMike Snitzer 		clear_bit(MPATHF_QUEUE_IO, &m->flags);
3861da177e4SLinus Torvalds 		goto failed;
3871f271972SBenjamin Marzinski 	}
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	/* Were we instructed to switch PG? */
390506458efSWill Deacon 	if (READ_ONCE(m->next_pg)) {
3912da1610aSMike Snitzer 		spin_lock_irqsave(&m->lock, flags);
3921da177e4SLinus Torvalds 		pg = m->next_pg;
3932da1610aSMike Snitzer 		if (!pg) {
3942da1610aSMike Snitzer 			spin_unlock_irqrestore(&m->lock, flags);
3952da1610aSMike Snitzer 			goto check_current_pg;
3962da1610aSMike Snitzer 		}
3971da177e4SLinus Torvalds 		m->next_pg = NULL;
3982da1610aSMike Snitzer 		spin_unlock_irqrestore(&m->lock, flags);
3992da1610aSMike Snitzer 		pgpath = choose_path_in_pg(m, pg, nr_bytes);
4002da1610aSMike Snitzer 		if (!IS_ERR_OR_NULL(pgpath))
4012da1610aSMike Snitzer 			return pgpath;
4021da177e4SLinus Torvalds 	}
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	/* Don't change PG until it has no remaining paths */
4052da1610aSMike Snitzer check_current_pg:
406506458efSWill Deacon 	pg = READ_ONCE(m->current_pg);
4072da1610aSMike Snitzer 	if (pg) {
4082da1610aSMike Snitzer 		pgpath = choose_path_in_pg(m, pg, nr_bytes);
4092da1610aSMike Snitzer 		if (!IS_ERR_OR_NULL(pgpath))
4102da1610aSMike Snitzer 			return pgpath;
4112da1610aSMike Snitzer 	}
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	/*
4141da177e4SLinus Torvalds 	 * Loop through priority groups until we find a valid path.
4151da177e4SLinus Torvalds 	 * First time we skip PGs marked 'bypassed'.
416f220fd4eSMike Christie 	 * Second time we only try the ones we skipped, but set
417f220fd4eSMike Christie 	 * pg_init_delay_retry so we do not hammer controllers.
4181da177e4SLinus Torvalds 	 */
4191da177e4SLinus Torvalds 	do {
4201da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
421d19a55ccSMike Snitzer 			if (pg->bypassed == !!bypassed)
4221da177e4SLinus Torvalds 				continue;
4232da1610aSMike Snitzer 			pgpath = choose_path_in_pg(m, pg, nr_bytes);
4242da1610aSMike Snitzer 			if (!IS_ERR_OR_NULL(pgpath)) {
425f220fd4eSMike Christie 				if (!bypassed)
426518257b1SMike Snitzer 					set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
4272da1610aSMike Snitzer 				return pgpath;
4281da177e4SLinus Torvalds 			}
429f220fd4eSMike Christie 		}
4301da177e4SLinus Torvalds 	} while (bypassed--);
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds failed:
4332da1610aSMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
4341da177e4SLinus Torvalds 	m->current_pgpath = NULL;
4351da177e4SLinus Torvalds 	m->current_pg = NULL;
4362da1610aSMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
4372da1610aSMike Snitzer 
4382da1610aSMike Snitzer 	return NULL;
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
44145e15720SKiyoshi Ueda /*
442*ac75b09fSMike Snitzer  * dm_report_EIO() is a macro instead of a function to make pr_debug_ratelimited()
44386331f39SBart Van Assche  * report the function name and line number of the function from which
44486331f39SBart Van Assche  * it has been invoked.
44545e15720SKiyoshi Ueda  */
44686331f39SBart Van Assche #define dm_report_EIO(m)						\
44718a482f5SChristoph Hellwig do {									\
44886331f39SBart Van Assche 	struct mapped_device *md = dm_table_get_md((m)->ti->table);	\
44986331f39SBart Van Assche 									\
450*ac75b09fSMike Snitzer 	DMDEBUG_LIMIT("%s: returning EIO; QIFNP = %d; SQIFNP = %d; DNFS = %d", \
45186331f39SBart Van Assche 		      dm_device_name(md),				\
45286331f39SBart Van Assche 		      test_bit(MPATHF_QUEUE_IF_NO_PATH, &(m)->flags),	\
45386331f39SBart Van Assche 		      test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &(m)->flags), \
45486331f39SBart Van Assche 		      dm_noflush_suspending((m)->ti));			\
45518a482f5SChristoph Hellwig } while (0)
45645e15720SKiyoshi Ueda 
45736fcffccSHannes Reinecke /*
458c1fd0abeSMike Snitzer  * Check whether bios must be queued in the device-mapper core rather
459c1fd0abeSMike Snitzer  * than here in the target.
460c1fd0abeSMike Snitzer  *
461c1fd0abeSMike Snitzer  * If MPATHF_QUEUE_IF_NO_PATH and MPATHF_SAVED_QUEUE_IF_NO_PATH hold
462c1fd0abeSMike Snitzer  * the same value then we are not between multipath_presuspend()
463c1fd0abeSMike Snitzer  * and multipath_resume() calls and we have no need to check
464c1fd0abeSMike Snitzer  * for the DMF_NOFLUSH_SUSPENDING flag.
465c1fd0abeSMike Snitzer  */
466c1fd0abeSMike Snitzer static bool __must_push_back(struct multipath *m, unsigned long flags)
467c1fd0abeSMike Snitzer {
468c1fd0abeSMike Snitzer 	return ((test_bit(MPATHF_QUEUE_IF_NO_PATH, &flags) !=
469c1fd0abeSMike Snitzer 		 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &flags)) &&
470c1fd0abeSMike Snitzer 		dm_noflush_suspending(m->ti));
471c1fd0abeSMike Snitzer }
472c1fd0abeSMike Snitzer 
473c1fd0abeSMike Snitzer /*
474c1fd0abeSMike Snitzer  * Following functions use READ_ONCE to get atomic access to
475c1fd0abeSMike Snitzer  * all m->flags to avoid taking spinlock
476c1fd0abeSMike Snitzer  */
477c1fd0abeSMike Snitzer static bool must_push_back_rq(struct multipath *m)
478c1fd0abeSMike Snitzer {
479c1fd0abeSMike Snitzer 	unsigned long flags = READ_ONCE(m->flags);
480c1fd0abeSMike Snitzer 	return test_bit(MPATHF_QUEUE_IF_NO_PATH, &flags) || __must_push_back(m, flags);
481c1fd0abeSMike Snitzer }
482c1fd0abeSMike Snitzer 
483c1fd0abeSMike Snitzer static bool must_push_back_bio(struct multipath *m)
484c1fd0abeSMike Snitzer {
485c1fd0abeSMike Snitzer 	unsigned long flags = READ_ONCE(m->flags);
486c1fd0abeSMike Snitzer 	return __must_push_back(m, flags);
487c1fd0abeSMike Snitzer }
488c1fd0abeSMike Snitzer 
489c1fd0abeSMike Snitzer /*
49076e33fe4SMike Snitzer  * Map cloned requests (request-based multipath)
49136fcffccSHannes Reinecke  */
492eb8db831SChristoph Hellwig static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
493e5863d9aSMike Snitzer 				   union map_info *map_context,
494eb8db831SChristoph Hellwig 				   struct request **__clone)
4951da177e4SLinus Torvalds {
4967943bd6dSMike Snitzer 	struct multipath *m = ti->private;
497eb8db831SChristoph Hellwig 	size_t nr_bytes = blk_rq_bytes(rq);
4981da177e4SLinus Torvalds 	struct pgpath *pgpath;
499f40c67f0SKiyoshi Ueda 	struct block_device *bdev;
500eb8db831SChristoph Hellwig 	struct dm_mpath_io *mpio = get_mpio(map_context);
5017083abbbSBart Van Assche 	struct request_queue *q;
502eb8db831SChristoph Hellwig 	struct request *clone;
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	/* Do we need to select a new pgpath? */
505506458efSWill Deacon 	pgpath = READ_ONCE(m->current_pgpath);
5062da1610aSMike Snitzer 	if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
5072da1610aSMike Snitzer 		pgpath = choose_pgpath(m, nr_bytes);
5081da177e4SLinus Torvalds 
5099bf59a61SMike Snitzer 	if (!pgpath) {
510c1fd0abeSMike Snitzer 		if (must_push_back_rq(m))
511b88efd43SMike Snitzer 			return DM_MAPIO_DELAY_REQUEUE;
51218a482f5SChristoph Hellwig 		dm_report_EIO(m);	/* Failed */
513f98e0eb6SChristoph Hellwig 		return DM_MAPIO_KILL;
514518257b1SMike Snitzer 	} else if (test_bit(MPATHF_QUEUE_IO, &m->flags) ||
515518257b1SMike Snitzer 		   test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
516459b5401SMing Lei 		pg_init_all_paths(m);
517c1d7ecf7SBart Van Assche 		return DM_MAPIO_DELAY_REQUEUE;
5189bf59a61SMike Snitzer 	}
5196afbc01dSMike Snitzer 
520e8099177SHannes Reinecke 	mpio->pgpath = pgpath;
521e8099177SHannes Reinecke 	mpio->nr_bytes = nr_bytes;
5222eb6e1e3SKeith Busch 
5232eb6e1e3SKeith Busch 	bdev = pgpath->path.dev->bdev;
5247083abbbSBart Van Assche 	q = bdev_get_queue(bdev);
525ff005a06SChristoph Hellwig 	clone = blk_get_request(q, rq->cmd_flags | REQ_NOMERGE,
526ff005a06SChristoph Hellwig 			BLK_MQ_REQ_NOWAIT);
5276599c84eSBart Van Assche 	if (IS_ERR(clone)) {
5286599c84eSBart Van Assche 		/* EBUSY, ENODEV or EWOULDBLOCK: requeue */
529848b8aefSMike Snitzer 		if (blk_queue_dying(q)) {
5307083abbbSBart Van Assche 			atomic_inc(&m->pg_init_in_progress);
5317083abbbSBart Van Assche 			activate_or_offline_path(pgpath);
532050af08fSMing Lei 			return DM_MAPIO_DELAY_REQUEUE;
5337083abbbSBart Van Assche 		}
534050af08fSMing Lei 
535050af08fSMing Lei 		/*
536050af08fSMing Lei 		 * blk-mq's SCHED_RESTART can cover this requeue, so we
537050af08fSMing Lei 		 * needn't deal with it by DELAY_REQUEUE. More importantly,
538050af08fSMing Lei 		 * we have to return DM_MAPIO_REQUEUE so that blk-mq can
539050af08fSMing Lei 		 * get the queue busy feedback (via BLK_STS_RESOURCE),
540050af08fSMing Lei 		 * otherwise I/O merging can suffer.
541050af08fSMing Lei 		 */
542050af08fSMing Lei 		return DM_MAPIO_REQUEUE;
5434c6dd53dSMike Snitzer 	}
5446599c84eSBart Van Assche 	clone->bio = clone->biotail = NULL;
5456599c84eSBart Van Assche 	clone->rq_disk = bdev->bd_disk;
5466599c84eSBart Van Assche 	clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
5476599c84eSBart Van Assche 	*__clone = clone;
5482eb6e1e3SKeith Busch 
549e8099177SHannes Reinecke 	if (pgpath->pg->ps.type->start_io)
550e8099177SHannes Reinecke 		pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
551e8099177SHannes Reinecke 					      &pgpath->path,
552e8099177SHannes Reinecke 					      nr_bytes);
5532eb6e1e3SKeith Busch 	return DM_MAPIO_REMAPPED;
5541da177e4SLinus Torvalds }
5551da177e4SLinus Torvalds 
5565de719e3SYufen Yu static void multipath_release_clone(struct request *clone,
5575de719e3SYufen Yu 				    union map_info *map_context)
558e5863d9aSMike Snitzer {
5595de719e3SYufen Yu 	if (unlikely(map_context)) {
5605de719e3SYufen Yu 		/*
5615de719e3SYufen Yu 		 * non-NULL map_context means caller is still map
5625de719e3SYufen Yu 		 * method; must undo multipath_clone_and_map()
5635de719e3SYufen Yu 		 */
5645de719e3SYufen Yu 		struct dm_mpath_io *mpio = get_mpio(map_context);
5655de719e3SYufen Yu 		struct pgpath *pgpath = mpio->pgpath;
5665de719e3SYufen Yu 
5675de719e3SYufen Yu 		if (pgpath && pgpath->pg->ps.type->end_io)
5685de719e3SYufen Yu 			pgpath->pg->ps.type->end_io(&pgpath->pg->ps,
5695de719e3SYufen Yu 						    &pgpath->path,
570087615bfSGabriel Krisman Bertazi 						    mpio->nr_bytes,
571087615bfSGabriel Krisman Bertazi 						    clone->io_start_time_ns);
5725de719e3SYufen Yu 	}
5735de719e3SYufen Yu 
574eb8db831SChristoph Hellwig 	blk_put_request(clone);
575e5863d9aSMike Snitzer }
576e5863d9aSMike Snitzer 
5771da177e4SLinus Torvalds /*
57876e33fe4SMike Snitzer  * Map cloned bios (bio-based multipath)
57976e33fe4SMike Snitzer  */
5800001ec56SMike Snitzer 
5810001ec56SMike Snitzer static struct pgpath *__map_bio(struct multipath *m, struct bio *bio)
58276e33fe4SMike Snitzer {
58376e33fe4SMike Snitzer 	struct pgpath *pgpath;
58476e33fe4SMike Snitzer 	unsigned long flags;
58576e33fe4SMike Snitzer 	bool queue_io;
58676e33fe4SMike Snitzer 
58776e33fe4SMike Snitzer 	/* Do we need to select a new pgpath? */
588506458efSWill Deacon 	pgpath = READ_ONCE(m->current_pgpath);
5895686dee3SGabriel Krisman Bertazi 	if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
5900001ec56SMike Snitzer 		pgpath = choose_pgpath(m, bio->bi_iter.bi_size);
59176e33fe4SMike Snitzer 
5925686dee3SGabriel Krisman Bertazi 	/* MPATHF_QUEUE_IO might have been cleared by choose_pgpath. */
5935686dee3SGabriel Krisman Bertazi 	queue_io = test_bit(MPATHF_QUEUE_IO, &m->flags);
5945686dee3SGabriel Krisman Bertazi 
59576e33fe4SMike Snitzer 	if ((pgpath && queue_io) ||
59676e33fe4SMike Snitzer 	    (!pgpath && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) {
59776e33fe4SMike Snitzer 		/* Queue for the daemon to resubmit */
59876e33fe4SMike Snitzer 		spin_lock_irqsave(&m->lock, flags);
59976e33fe4SMike Snitzer 		bio_list_add(&m->queued_bios, bio);
60076e33fe4SMike Snitzer 		spin_unlock_irqrestore(&m->lock, flags);
601848b8aefSMike Snitzer 
60276e33fe4SMike Snitzer 		/* PG_INIT_REQUIRED cannot be set without QUEUE_IO */
60376e33fe4SMike Snitzer 		if (queue_io || test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
60476e33fe4SMike Snitzer 			pg_init_all_paths(m);
60576e33fe4SMike Snitzer 		else if (!queue_io)
60676e33fe4SMike Snitzer 			queue_work(kmultipathd, &m->process_queued_bios);
6070001ec56SMike Snitzer 
6080001ec56SMike Snitzer 		return ERR_PTR(-EAGAIN);
60976e33fe4SMike Snitzer 	}
61076e33fe4SMike Snitzer 
6110001ec56SMike Snitzer 	return pgpath;
61276e33fe4SMike Snitzer }
61376e33fe4SMike Snitzer 
6140001ec56SMike Snitzer static int __multipath_map_bio(struct multipath *m, struct bio *bio,
6150001ec56SMike Snitzer 			       struct dm_mpath_io *mpio)
6160001ec56SMike Snitzer {
617dbaf971cSMike Snitzer 	struct pgpath *pgpath = __map_bio(m, bio);
6180001ec56SMike Snitzer 
6190001ec56SMike Snitzer 	if (IS_ERR(pgpath))
6200001ec56SMike Snitzer 		return DM_MAPIO_SUBMITTED;
6210001ec56SMike Snitzer 
62276e33fe4SMike Snitzer 	if (!pgpath) {
623c1fd0abeSMike Snitzer 		if (must_push_back_bio(m))
62476e33fe4SMike Snitzer 			return DM_MAPIO_REQUEUE;
62518a482f5SChristoph Hellwig 		dm_report_EIO(m);
626846785e6SChristoph Hellwig 		return DM_MAPIO_KILL;
62776e33fe4SMike Snitzer 	}
62876e33fe4SMike Snitzer 
62976e33fe4SMike Snitzer 	mpio->pgpath = pgpath;
63076e33fe4SMike Snitzer 
6314e4cbee9SChristoph Hellwig 	bio->bi_status = 0;
63274d46992SChristoph Hellwig 	bio_set_dev(bio, pgpath->path.dev->bdev);
6331eff9d32SJens Axboe 	bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
63476e33fe4SMike Snitzer 
63576e33fe4SMike Snitzer 	if (pgpath->pg->ps.type->start_io)
63676e33fe4SMike Snitzer 		pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
63776e33fe4SMike Snitzer 					      &pgpath->path,
638d0442f80SMike Snitzer 					      mpio->nr_bytes);
63976e33fe4SMike Snitzer 	return DM_MAPIO_REMAPPED;
64076e33fe4SMike Snitzer }
64176e33fe4SMike Snitzer 
64276e33fe4SMike Snitzer static int multipath_map_bio(struct dm_target *ti, struct bio *bio)
64376e33fe4SMike Snitzer {
64476e33fe4SMike Snitzer 	struct multipath *m = ti->private;
645bf661be1SMike Snitzer 	struct dm_mpath_io *mpio = NULL;
646bf661be1SMike Snitzer 
64763f6e6fdSMike Snitzer 	multipath_init_per_bio_data(bio, &mpio);
64876e33fe4SMike Snitzer 	return __multipath_map_bio(m, bio, mpio);
64976e33fe4SMike Snitzer }
65076e33fe4SMike Snitzer 
6517e48c768SMike Snitzer static void process_queued_io_list(struct multipath *m)
65276e33fe4SMike Snitzer {
653953923c0SMike Snitzer 	if (m->queue_mode == DM_TYPE_REQUEST_BASED)
6547e48c768SMike Snitzer 		dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table));
6558d47e659SMike Snitzer 	else if (m->queue_mode == DM_TYPE_BIO_BASED)
65676e33fe4SMike Snitzer 		queue_work(kmultipathd, &m->process_queued_bios);
65776e33fe4SMike Snitzer }
65876e33fe4SMike Snitzer 
65976e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work)
66076e33fe4SMike Snitzer {
66176e33fe4SMike Snitzer 	int r;
66276e33fe4SMike Snitzer 	unsigned long flags;
66376e33fe4SMike Snitzer 	struct bio *bio;
66476e33fe4SMike Snitzer 	struct bio_list bios;
66576e33fe4SMike Snitzer 	struct blk_plug plug;
66676e33fe4SMike Snitzer 	struct multipath *m =
66776e33fe4SMike Snitzer 		container_of(work, struct multipath, process_queued_bios);
66876e33fe4SMike Snitzer 
66976e33fe4SMike Snitzer 	bio_list_init(&bios);
67076e33fe4SMike Snitzer 
67176e33fe4SMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
67276e33fe4SMike Snitzer 
67376e33fe4SMike Snitzer 	if (bio_list_empty(&m->queued_bios)) {
67476e33fe4SMike Snitzer 		spin_unlock_irqrestore(&m->lock, flags);
67576e33fe4SMike Snitzer 		return;
67676e33fe4SMike Snitzer 	}
67776e33fe4SMike Snitzer 
67876e33fe4SMike Snitzer 	bio_list_merge(&bios, &m->queued_bios);
67976e33fe4SMike Snitzer 	bio_list_init(&m->queued_bios);
68076e33fe4SMike Snitzer 
68176e33fe4SMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
68276e33fe4SMike Snitzer 
68376e33fe4SMike Snitzer 	blk_start_plug(&plug);
68476e33fe4SMike Snitzer 	while ((bio = bio_list_pop(&bios))) {
6851836df08SMike Snitzer 		struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
6861836df08SMike Snitzer 		dm_bio_restore(get_bio_details_from_mpio(mpio), bio);
6871836df08SMike Snitzer 		r = __multipath_map_bio(m, bio, mpio);
688846785e6SChristoph Hellwig 		switch (r) {
689846785e6SChristoph Hellwig 		case DM_MAPIO_KILL:
6904e4cbee9SChristoph Hellwig 			bio->bi_status = BLK_STS_IOERR;
6914e4cbee9SChristoph Hellwig 			bio_endio(bio);
692047385b3SDan Carpenter 			break;
693846785e6SChristoph Hellwig 		case DM_MAPIO_REQUEUE:
6944e4cbee9SChristoph Hellwig 			bio->bi_status = BLK_STS_DM_REQUEUE;
69576e33fe4SMike Snitzer 			bio_endio(bio);
696846785e6SChristoph Hellwig 			break;
697846785e6SChristoph Hellwig 		case DM_MAPIO_REMAPPED:
69876e33fe4SMike Snitzer 			generic_make_request(bio);
699846785e6SChristoph Hellwig 			break;
7008192a0cdSWang Sheng-Hui 		case DM_MAPIO_SUBMITTED:
7019157c8d3SBart Van Assche 			break;
7029157c8d3SBart Van Assche 		default:
7039157c8d3SBart Van Assche 			WARN_ONCE(true, "__multipath_map_bio() returned %d\n", r);
704846785e6SChristoph Hellwig 		}
70576e33fe4SMike Snitzer 	}
70676e33fe4SMike Snitzer 	blk_finish_plug(&plug);
70776e33fe4SMike Snitzer }
70876e33fe4SMike Snitzer 
70976e33fe4SMike Snitzer /*
7101da177e4SLinus Torvalds  * If we run out of usable paths, should we queue I/O or error it?
7111da177e4SLinus Torvalds  */
712be7d31ccSMike Snitzer static int queue_if_no_path(struct multipath *m, bool queue_if_no_path,
713be7d31ccSMike Snitzer 			    bool save_old_value)
7141da177e4SLinus Torvalds {
7151da177e4SLinus Torvalds 	unsigned long flags;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
7185307e2adSLukas Wunner 	assign_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags,
7195307e2adSLukas Wunner 		   (save_old_value && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) ||
7205307e2adSLukas Wunner 		   (!save_old_value && queue_if_no_path));
721c1fd0abeSMike Snitzer 	assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags, queue_if_no_path);
7221da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
7231da177e4SLinus Torvalds 
72476e33fe4SMike Snitzer 	if (!queue_if_no_path) {
72563d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
7267e48c768SMike Snitzer 		process_queued_io_list(m);
72776e33fe4SMike Snitzer 	}
72863d832c3SHannes Reinecke 
7291da177e4SLinus Torvalds 	return 0;
7301da177e4SLinus Torvalds }
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds /*
733be240ff5SAnatol Pomazau  * If the queue_if_no_path timeout fires, turn off queue_if_no_path and
734be240ff5SAnatol Pomazau  * process any queued I/O.
735be240ff5SAnatol Pomazau  */
736be240ff5SAnatol Pomazau static void queue_if_no_path_timeout_work(struct timer_list *t)
737be240ff5SAnatol Pomazau {
738be240ff5SAnatol Pomazau 	struct multipath *m = from_timer(m, t, nopath_timer);
739be240ff5SAnatol Pomazau 	struct mapped_device *md = dm_table_get_md(m->ti->table);
740be240ff5SAnatol Pomazau 
741be240ff5SAnatol Pomazau 	DMWARN("queue_if_no_path timeout on %s, failing queued IO", dm_device_name(md));
742be240ff5SAnatol Pomazau 	queue_if_no_path(m, false, false);
743be240ff5SAnatol Pomazau }
744be240ff5SAnatol Pomazau 
745be240ff5SAnatol Pomazau /*
746be240ff5SAnatol Pomazau  * Enable the queue_if_no_path timeout if necessary.
747be240ff5SAnatol Pomazau  * Called with m->lock held.
748be240ff5SAnatol Pomazau  */
749be240ff5SAnatol Pomazau static void enable_nopath_timeout(struct multipath *m)
750be240ff5SAnatol Pomazau {
751be240ff5SAnatol Pomazau 	unsigned long queue_if_no_path_timeout =
752be240ff5SAnatol Pomazau 		READ_ONCE(queue_if_no_path_timeout_secs) * HZ;
753be240ff5SAnatol Pomazau 
754be240ff5SAnatol Pomazau 	lockdep_assert_held(&m->lock);
755be240ff5SAnatol Pomazau 
756be240ff5SAnatol Pomazau 	if (queue_if_no_path_timeout > 0 &&
757be240ff5SAnatol Pomazau 	    atomic_read(&m->nr_valid_paths) == 0 &&
758be240ff5SAnatol Pomazau 	    test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
759be240ff5SAnatol Pomazau 		mod_timer(&m->nopath_timer,
760be240ff5SAnatol Pomazau 			  jiffies + queue_if_no_path_timeout);
761be240ff5SAnatol Pomazau 	}
762be240ff5SAnatol Pomazau }
763be240ff5SAnatol Pomazau 
764be240ff5SAnatol Pomazau static void disable_nopath_timeout(struct multipath *m)
765be240ff5SAnatol Pomazau {
766be240ff5SAnatol Pomazau 	del_timer_sync(&m->nopath_timer);
767be240ff5SAnatol Pomazau }
768be240ff5SAnatol Pomazau 
769be240ff5SAnatol Pomazau /*
7701da177e4SLinus Torvalds  * An event is triggered whenever a path is taken out of use.
7711da177e4SLinus Torvalds  * Includes path failure and PG bypass.
7721da177e4SLinus Torvalds  */
773c4028958SDavid Howells static void trigger_event(struct work_struct *work)
7741da177e4SLinus Torvalds {
775c4028958SDavid Howells 	struct multipath *m =
776c4028958SDavid Howells 		container_of(work, struct multipath, trigger_event);
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	dm_table_event(m->ti->table);
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds /*-----------------------------------------------------------------
7821da177e4SLinus Torvalds  * Constructor/argument parsing:
7831da177e4SLinus Torvalds  * <#multipath feature args> [<arg>]*
7841da177e4SLinus Torvalds  * <#hw_handler args> [hw_handler [<arg>]*]
7851da177e4SLinus Torvalds  * <#priority groups>
7861da177e4SLinus Torvalds  * <initial priority group>
7871da177e4SLinus Torvalds  *     [<selector> <#selector args> [<arg>]*
7881da177e4SLinus Torvalds  *      <#paths> <#per-path selector args>
7891da177e4SLinus Torvalds  *         [<path> [<arg>]* ]+ ]+
7901da177e4SLinus Torvalds  *---------------------------------------------------------------*/
791498f0103SMike Snitzer static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
7921da177e4SLinus Torvalds 			       struct dm_target *ti)
7931da177e4SLinus Torvalds {
7941da177e4SLinus Torvalds 	int r;
7951da177e4SLinus Torvalds 	struct path_selector_type *pst;
7961da177e4SLinus Torvalds 	unsigned ps_argc;
7971da177e4SLinus Torvalds 
7985916a22bSEric Biggers 	static const struct dm_arg _args[] = {
79972d94861SAlasdair G Kergon 		{0, 1024, "invalid number of path selector args"},
8001da177e4SLinus Torvalds 	};
8011da177e4SLinus Torvalds 
802498f0103SMike Snitzer 	pst = dm_get_path_selector(dm_shift_arg(as));
8031da177e4SLinus Torvalds 	if (!pst) {
80472d94861SAlasdair G Kergon 		ti->error = "unknown path selector type";
8051da177e4SLinus Torvalds 		return -EINVAL;
8061da177e4SLinus Torvalds 	}
8071da177e4SLinus Torvalds 
808498f0103SMike Snitzer 	r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
809371b2e34SMikulas Patocka 	if (r) {
810371b2e34SMikulas Patocka 		dm_put_path_selector(pst);
8111da177e4SLinus Torvalds 		return -EINVAL;
812371b2e34SMikulas Patocka 	}
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 	r = pst->create(&pg->ps, ps_argc, as->argv);
8151da177e4SLinus Torvalds 	if (r) {
8161da177e4SLinus Torvalds 		dm_put_path_selector(pst);
81772d94861SAlasdair G Kergon 		ti->error = "path selector constructor failed";
8181da177e4SLinus Torvalds 		return r;
8191da177e4SLinus Torvalds 	}
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	pg->ps.type = pst;
822498f0103SMike Snitzer 	dm_consume_args(as, ps_argc);
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	return 0;
8251da177e4SLinus Torvalds }
8261da177e4SLinus Torvalds 
827e8f74a0fSMike Snitzer static int setup_scsi_dh(struct block_device *bdev, struct multipath *m,
828b592211cSMike Snitzer 			 const char **attached_handler_name, char **error)
8291da177e4SLinus Torvalds {
830848b8aefSMike Snitzer 	struct request_queue *q = bdev_get_queue(bdev);
831848b8aefSMike Snitzer 	int r;
832a0cf7ea9SHannes Reinecke 
833518257b1SMike Snitzer 	if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) {
8341bab0de0SChristoph Hellwig retain:
835b592211cSMike Snitzer 		if (*attached_handler_name) {
836a58a935dSMike Snitzer 			/*
83754cd640dStang.junhui 			 * Clear any hw_handler_params associated with a
83854cd640dStang.junhui 			 * handler that isn't already attached.
83954cd640dStang.junhui 			 */
840b592211cSMike Snitzer 			if (m->hw_handler_name && strcmp(*attached_handler_name, m->hw_handler_name)) {
84154cd640dStang.junhui 				kfree(m->hw_handler_params);
84254cd640dStang.junhui 				m->hw_handler_params = NULL;
84354cd640dStang.junhui 			}
84454cd640dStang.junhui 
84554cd640dStang.junhui 			/*
846a58a935dSMike Snitzer 			 * Reset hw_handler_name to match the attached handler
847a58a935dSMike Snitzer 			 *
848a58a935dSMike Snitzer 			 * NB. This modifies the table line to show the actual
849a58a935dSMike Snitzer 			 * handler instead of the original table passed in.
850a58a935dSMike Snitzer 			 */
851a58a935dSMike Snitzer 			kfree(m->hw_handler_name);
852b592211cSMike Snitzer 			m->hw_handler_name = *attached_handler_name;
853b592211cSMike Snitzer 			*attached_handler_name = NULL;
854a58a935dSMike Snitzer 		}
855a58a935dSMike Snitzer 	}
856a58a935dSMike Snitzer 
857a58a935dSMike Snitzer 	if (m->hw_handler_name) {
858a0cf7ea9SHannes Reinecke 		r = scsi_dh_attach(q, m->hw_handler_name);
859a0cf7ea9SHannes Reinecke 		if (r == -EBUSY) {
8601bab0de0SChristoph Hellwig 			char b[BDEVNAME_SIZE];
861a0cf7ea9SHannes Reinecke 
8621bab0de0SChristoph Hellwig 			printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
863848b8aefSMike Snitzer 			       bdevname(bdev, b));
8641bab0de0SChristoph Hellwig 			goto retain;
8651bab0de0SChristoph Hellwig 		}
866ae11b1b3SHannes Reinecke 		if (r < 0) {
867848b8aefSMike Snitzer 			*error = "error attaching hardware handler";
868848b8aefSMike Snitzer 			return r;
869ae11b1b3SHannes Reinecke 		}
8702bfd2e13SChandra Seetharaman 
8712bfd2e13SChandra Seetharaman 		if (m->hw_handler_params) {
8722bfd2e13SChandra Seetharaman 			r = scsi_dh_set_params(q, m->hw_handler_params);
8732bfd2e13SChandra Seetharaman 			if (r < 0) {
874848b8aefSMike Snitzer 				*error = "unable to set hardware handler parameters";
875848b8aefSMike Snitzer 				return r;
876848b8aefSMike Snitzer 			}
877848b8aefSMike Snitzer 		}
878848b8aefSMike Snitzer 	}
879848b8aefSMike Snitzer 
880848b8aefSMike Snitzer 	return 0;
881848b8aefSMike Snitzer }
882848b8aefSMike Snitzer 
883848b8aefSMike Snitzer static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
884848b8aefSMike Snitzer 				 struct dm_target *ti)
885848b8aefSMike Snitzer {
886848b8aefSMike Snitzer 	int r;
887848b8aefSMike Snitzer 	struct pgpath *p;
888848b8aefSMike Snitzer 	struct multipath *m = ti->private;
889e8f74a0fSMike Snitzer 	struct request_queue *q;
890b592211cSMike Snitzer 	const char *attached_handler_name = NULL;
891848b8aefSMike Snitzer 
892848b8aefSMike Snitzer 	/* we need at least a path arg */
893848b8aefSMike Snitzer 	if (as->argc < 1) {
894848b8aefSMike Snitzer 		ti->error = "no device given";
895848b8aefSMike Snitzer 		return ERR_PTR(-EINVAL);
896848b8aefSMike Snitzer 	}
897848b8aefSMike Snitzer 
898848b8aefSMike Snitzer 	p = alloc_pgpath();
899848b8aefSMike Snitzer 	if (!p)
900848b8aefSMike Snitzer 		return ERR_PTR(-ENOMEM);
901848b8aefSMike Snitzer 
902848b8aefSMike Snitzer 	r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
903848b8aefSMike Snitzer 			  &p->path.dev);
904848b8aefSMike Snitzer 	if (r) {
905848b8aefSMike Snitzer 		ti->error = "error getting device";
9062bfd2e13SChandra Seetharaman 		goto bad;
9072bfd2e13SChandra Seetharaman 	}
908848b8aefSMike Snitzer 
909e8f74a0fSMike Snitzer 	q = bdev_get_queue(p->path.dev->bdev);
910e8f74a0fSMike Snitzer 	attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
911e457edf0SMike Snitzer 	if (attached_handler_name || m->hw_handler_name) {
912848b8aefSMike Snitzer 		INIT_DELAYED_WORK(&p->activate_path, activate_path_work);
913b592211cSMike Snitzer 		r = setup_scsi_dh(p->path.dev->bdev, m, &attached_handler_name, &ti->error);
914940bc471SMartin Wilck 		kfree(attached_handler_name);
915848b8aefSMike Snitzer 		if (r) {
916848b8aefSMike Snitzer 			dm_put_device(ti, p->path.dev);
917848b8aefSMike Snitzer 			goto bad;
9182bfd2e13SChandra Seetharaman 		}
919ae11b1b3SHannes Reinecke 	}
920ae11b1b3SHannes Reinecke 
9211da177e4SLinus Torvalds 	r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
9221da177e4SLinus Torvalds 	if (r) {
9231da177e4SLinus Torvalds 		dm_put_device(ti, p->path.dev);
9241da177e4SLinus Torvalds 		goto bad;
9251da177e4SLinus Torvalds 	}
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	return p;
9281da177e4SLinus Torvalds  bad:
9291da177e4SLinus Torvalds 	free_pgpath(p);
93001460f35SBenjamin Marzinski 	return ERR_PTR(r);
9311da177e4SLinus Torvalds }
9321da177e4SLinus Torvalds 
933498f0103SMike Snitzer static struct priority_group *parse_priority_group(struct dm_arg_set *as,
93428f16c20SMicha³ Miros³aw 						   struct multipath *m)
9351da177e4SLinus Torvalds {
9365916a22bSEric Biggers 	static const struct dm_arg _args[] = {
93772d94861SAlasdair G Kergon 		{1, 1024, "invalid number of paths"},
93872d94861SAlasdair G Kergon 		{0, 1024, "invalid number of selector args"}
9391da177e4SLinus Torvalds 	};
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 	int r;
942498f0103SMike Snitzer 	unsigned i, nr_selector_args, nr_args;
9431da177e4SLinus Torvalds 	struct priority_group *pg;
94428f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	if (as->argc < 2) {
9471da177e4SLinus Torvalds 		as->argc = 0;
94801460f35SBenjamin Marzinski 		ti->error = "not enough priority group arguments";
94901460f35SBenjamin Marzinski 		return ERR_PTR(-EINVAL);
9501da177e4SLinus Torvalds 	}
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	pg = alloc_priority_group();
9531da177e4SLinus Torvalds 	if (!pg) {
95472d94861SAlasdair G Kergon 		ti->error = "couldn't allocate priority group";
95501460f35SBenjamin Marzinski 		return ERR_PTR(-ENOMEM);
9561da177e4SLinus Torvalds 	}
9571da177e4SLinus Torvalds 	pg->m = m;
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 	r = parse_path_selector(as, pg, ti);
9601da177e4SLinus Torvalds 	if (r)
9611da177e4SLinus Torvalds 		goto bad;
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	/*
9641da177e4SLinus Torvalds 	 * read the paths
9651da177e4SLinus Torvalds 	 */
966498f0103SMike Snitzer 	r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
9671da177e4SLinus Torvalds 	if (r)
9681da177e4SLinus Torvalds 		goto bad;
9691da177e4SLinus Torvalds 
970498f0103SMike Snitzer 	r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
9711da177e4SLinus Torvalds 	if (r)
9721da177e4SLinus Torvalds 		goto bad;
9731da177e4SLinus Torvalds 
974498f0103SMike Snitzer 	nr_args = 1 + nr_selector_args;
9751da177e4SLinus Torvalds 	for (i = 0; i < pg->nr_pgpaths; i++) {
9761da177e4SLinus Torvalds 		struct pgpath *pgpath;
977498f0103SMike Snitzer 		struct dm_arg_set path_args;
9781da177e4SLinus Torvalds 
979498f0103SMike Snitzer 		if (as->argc < nr_args) {
980148acff6SMikulas Patocka 			ti->error = "not enough path parameters";
9816bbf79a1SAlasdair G Kergon 			r = -EINVAL;
9821da177e4SLinus Torvalds 			goto bad;
983148acff6SMikulas Patocka 		}
9841da177e4SLinus Torvalds 
985498f0103SMike Snitzer 		path_args.argc = nr_args;
9861da177e4SLinus Torvalds 		path_args.argv = as->argv;
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 		pgpath = parse_path(&path_args, &pg->ps, ti);
98901460f35SBenjamin Marzinski 		if (IS_ERR(pgpath)) {
99001460f35SBenjamin Marzinski 			r = PTR_ERR(pgpath);
9911da177e4SLinus Torvalds 			goto bad;
99201460f35SBenjamin Marzinski 		}
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 		pgpath->pg = pg;
9951da177e4SLinus Torvalds 		list_add_tail(&pgpath->list, &pg->pgpaths);
996498f0103SMike Snitzer 		dm_consume_args(as, nr_args);
9971da177e4SLinus Torvalds 	}
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 	return pg;
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds  bad:
10021da177e4SLinus Torvalds 	free_priority_group(pg, ti);
100301460f35SBenjamin Marzinski 	return ERR_PTR(r);
10041da177e4SLinus Torvalds }
10051da177e4SLinus Torvalds 
1006498f0103SMike Snitzer static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
10071da177e4SLinus Torvalds {
10081da177e4SLinus Torvalds 	unsigned hw_argc;
10092bfd2e13SChandra Seetharaman 	int ret;
101028f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
10111da177e4SLinus Torvalds 
10125916a22bSEric Biggers 	static const struct dm_arg _args[] = {
101372d94861SAlasdair G Kergon 		{0, 1024, "invalid number of hardware handler args"},
10141da177e4SLinus Torvalds 	};
10151da177e4SLinus Torvalds 
1016498f0103SMike Snitzer 	if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
10171da177e4SLinus Torvalds 		return -EINVAL;
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 	if (!hw_argc)
10201da177e4SLinus Torvalds 		return 0;
10211da177e4SLinus Torvalds 
10228d47e659SMike Snitzer 	if (m->queue_mode == DM_TYPE_BIO_BASED) {
102376e33fe4SMike Snitzer 		dm_consume_args(as, hw_argc);
102476e33fe4SMike Snitzer 		DMERR("bio-based multipath doesn't allow hardware handler args");
102576e33fe4SMike Snitzer 		return 0;
102676e33fe4SMike Snitzer 	}
102776e33fe4SMike Snitzer 
1028498f0103SMike Snitzer 	m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
1029f97dc421Stang.junhui 	if (!m->hw_handler_name)
1030f97dc421Stang.junhui 		return -EINVAL;
103114e98c5cSChandra Seetharaman 
10322bfd2e13SChandra Seetharaman 	if (hw_argc > 1) {
10332bfd2e13SChandra Seetharaman 		char *p;
10342bfd2e13SChandra Seetharaman 		int i, j, len = 4;
10352bfd2e13SChandra Seetharaman 
10362bfd2e13SChandra Seetharaman 		for (i = 0; i <= hw_argc - 2; i++)
10372bfd2e13SChandra Seetharaman 			len += strlen(as->argv[i]) + 1;
10382bfd2e13SChandra Seetharaman 		p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
10392bfd2e13SChandra Seetharaman 		if (!p) {
10402bfd2e13SChandra Seetharaman 			ti->error = "memory allocation failed";
10412bfd2e13SChandra Seetharaman 			ret = -ENOMEM;
10422bfd2e13SChandra Seetharaman 			goto fail;
10432bfd2e13SChandra Seetharaman 		}
10442bfd2e13SChandra Seetharaman 		j = sprintf(p, "%d", hw_argc - 1);
10452bfd2e13SChandra Seetharaman 		for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
10462bfd2e13SChandra Seetharaman 			j = sprintf(p, "%s", as->argv[i]);
10472bfd2e13SChandra Seetharaman 	}
1048498f0103SMike Snitzer 	dm_consume_args(as, hw_argc - 1);
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	return 0;
10512bfd2e13SChandra Seetharaman fail:
10522bfd2e13SChandra Seetharaman 	kfree(m->hw_handler_name);
10532bfd2e13SChandra Seetharaman 	m->hw_handler_name = NULL;
10542bfd2e13SChandra Seetharaman 	return ret;
10551da177e4SLinus Torvalds }
10561da177e4SLinus Torvalds 
1057498f0103SMike Snitzer static int parse_features(struct dm_arg_set *as, struct multipath *m)
10581da177e4SLinus Torvalds {
10591da177e4SLinus Torvalds 	int r;
10601da177e4SLinus Torvalds 	unsigned argc;
106128f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
1062498f0103SMike Snitzer 	const char *arg_name;
10631da177e4SLinus Torvalds 
10645916a22bSEric Biggers 	static const struct dm_arg _args[] = {
1065e83068a5SMike Snitzer 		{0, 8, "invalid number of feature args"},
1066c9e45581SDave Wysochanski 		{1, 50, "pg_init_retries must be between 1 and 50"},
10674e2d19e4SChandra Seetharaman 		{0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
10681da177e4SLinus Torvalds 	};
10691da177e4SLinus Torvalds 
1070498f0103SMike Snitzer 	r = dm_read_arg_group(_args, as, &argc, &ti->error);
10711da177e4SLinus Torvalds 	if (r)
10721da177e4SLinus Torvalds 		return -EINVAL;
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds 	if (!argc)
10751da177e4SLinus Torvalds 		return 0;
10761da177e4SLinus Torvalds 
1077c9e45581SDave Wysochanski 	do {
1078498f0103SMike Snitzer 		arg_name = dm_shift_arg(as);
1079c9e45581SDave Wysochanski 		argc--;
1080c9e45581SDave Wysochanski 
1081498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "queue_if_no_path")) {
1082be7d31ccSMike Snitzer 			r = queue_if_no_path(m, true, false);
1083c9e45581SDave Wysochanski 			continue;
10841da177e4SLinus Torvalds 		}
1085c9e45581SDave Wysochanski 
1086a58a935dSMike Snitzer 		if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
1087518257b1SMike Snitzer 			set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
1088a58a935dSMike Snitzer 			continue;
1089a58a935dSMike Snitzer 		}
1090a58a935dSMike Snitzer 
1091498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "pg_init_retries") &&
1092c9e45581SDave Wysochanski 		    (argc >= 1)) {
1093498f0103SMike Snitzer 			r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
1094c9e45581SDave Wysochanski 			argc--;
1095c9e45581SDave Wysochanski 			continue;
1096c9e45581SDave Wysochanski 		}
1097c9e45581SDave Wysochanski 
1098498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
10994e2d19e4SChandra Seetharaman 		    (argc >= 1)) {
1100498f0103SMike Snitzer 			r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
11014e2d19e4SChandra Seetharaman 			argc--;
11024e2d19e4SChandra Seetharaman 			continue;
11034e2d19e4SChandra Seetharaman 		}
11044e2d19e4SChandra Seetharaman 
1105e83068a5SMike Snitzer 		if (!strcasecmp(arg_name, "queue_mode") &&
1106e83068a5SMike Snitzer 		    (argc >= 1)) {
1107e83068a5SMike Snitzer 			const char *queue_mode_name = dm_shift_arg(as);
1108e83068a5SMike Snitzer 
1109e83068a5SMike Snitzer 			if (!strcasecmp(queue_mode_name, "bio"))
1110e83068a5SMike Snitzer 				m->queue_mode = DM_TYPE_BIO_BASED;
1111953923c0SMike Snitzer 			else if (!strcasecmp(queue_mode_name, "rq") ||
1112953923c0SMike Snitzer 				 !strcasecmp(queue_mode_name, "mq"))
1113e83068a5SMike Snitzer 				m->queue_mode = DM_TYPE_REQUEST_BASED;
1114e83068a5SMike Snitzer 			else {
1115e83068a5SMike Snitzer 				ti->error = "Unknown 'queue_mode' requested";
1116e83068a5SMike Snitzer 				r = -EINVAL;
1117e83068a5SMike Snitzer 			}
1118e83068a5SMike Snitzer 			argc--;
1119e83068a5SMike Snitzer 			continue;
1120e83068a5SMike Snitzer 		}
1121e83068a5SMike Snitzer 
1122c9e45581SDave Wysochanski 		ti->error = "Unrecognised multipath feature request";
1123c9e45581SDave Wysochanski 		r = -EINVAL;
1124c9e45581SDave Wysochanski 	} while (argc && !r);
1125c9e45581SDave Wysochanski 
1126c9e45581SDave Wysochanski 	return r;
11271da177e4SLinus Torvalds }
11281da177e4SLinus Torvalds 
1129e83068a5SMike Snitzer static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv)
11301da177e4SLinus Torvalds {
1131498f0103SMike Snitzer 	/* target arguments */
11325916a22bSEric Biggers 	static const struct dm_arg _args[] = {
1133a490a07aSMike Snitzer 		{0, 1024, "invalid number of priority groups"},
1134a490a07aSMike Snitzer 		{0, 1024, "invalid initial priority group number"},
11351da177e4SLinus Torvalds 	};
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 	int r;
11381da177e4SLinus Torvalds 	struct multipath *m;
1139498f0103SMike Snitzer 	struct dm_arg_set as;
11401da177e4SLinus Torvalds 	unsigned pg_count = 0;
11411da177e4SLinus Torvalds 	unsigned next_pg_num;
1142be240ff5SAnatol Pomazau 	unsigned long flags;
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	as.argc = argc;
11451da177e4SLinus Torvalds 	as.argv = argv;
11461da177e4SLinus Torvalds 
1147e83068a5SMike Snitzer 	m = alloc_multipath(ti);
11481da177e4SLinus Torvalds 	if (!m) {
114972d94861SAlasdair G Kergon 		ti->error = "can't allocate multipath";
11501da177e4SLinus Torvalds 		return -EINVAL;
11511da177e4SLinus Torvalds 	}
11521da177e4SLinus Torvalds 
115328f16c20SMicha³ Miros³aw 	r = parse_features(&as, m);
11541da177e4SLinus Torvalds 	if (r)
11551da177e4SLinus Torvalds 		goto bad;
11561da177e4SLinus Torvalds 
1157e83068a5SMike Snitzer 	r = alloc_multipath_stage2(ti, m);
1158e83068a5SMike Snitzer 	if (r)
1159e83068a5SMike Snitzer 		goto bad;
1160e83068a5SMike Snitzer 
116128f16c20SMicha³ Miros³aw 	r = parse_hw_handler(&as, m);
11621da177e4SLinus Torvalds 	if (r)
11631da177e4SLinus Torvalds 		goto bad;
11641da177e4SLinus Torvalds 
1165498f0103SMike Snitzer 	r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
11661da177e4SLinus Torvalds 	if (r)
11671da177e4SLinus Torvalds 		goto bad;
11681da177e4SLinus Torvalds 
1169498f0103SMike Snitzer 	r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
11701da177e4SLinus Torvalds 	if (r)
11711da177e4SLinus Torvalds 		goto bad;
11721da177e4SLinus Torvalds 
1173a490a07aSMike Snitzer 	if ((!m->nr_priority_groups && next_pg_num) ||
1174a490a07aSMike Snitzer 	    (m->nr_priority_groups && !next_pg_num)) {
1175a490a07aSMike Snitzer 		ti->error = "invalid initial priority group";
1176a490a07aSMike Snitzer 		r = -EINVAL;
1177a490a07aSMike Snitzer 		goto bad;
1178a490a07aSMike Snitzer 	}
1179a490a07aSMike Snitzer 
11801da177e4SLinus Torvalds 	/* parse the priority groups */
11811da177e4SLinus Torvalds 	while (as.argc) {
11821da177e4SLinus Torvalds 		struct priority_group *pg;
118391e968aaSMike Snitzer 		unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths);
11841da177e4SLinus Torvalds 
118528f16c20SMicha³ Miros³aw 		pg = parse_priority_group(&as, m);
118601460f35SBenjamin Marzinski 		if (IS_ERR(pg)) {
118701460f35SBenjamin Marzinski 			r = PTR_ERR(pg);
11881da177e4SLinus Torvalds 			goto bad;
11891da177e4SLinus Torvalds 		}
11901da177e4SLinus Torvalds 
119191e968aaSMike Snitzer 		nr_valid_paths += pg->nr_pgpaths;
119291e968aaSMike Snitzer 		atomic_set(&m->nr_valid_paths, nr_valid_paths);
119391e968aaSMike Snitzer 
11941da177e4SLinus Torvalds 		list_add_tail(&pg->list, &m->priority_groups);
11951da177e4SLinus Torvalds 		pg_count++;
11961da177e4SLinus Torvalds 		pg->pg_num = pg_count;
11971da177e4SLinus Torvalds 		if (!--next_pg_num)
11981da177e4SLinus Torvalds 			m->next_pg = pg;
11991da177e4SLinus Torvalds 	}
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 	if (pg_count != m->nr_priority_groups) {
120272d94861SAlasdair G Kergon 		ti->error = "priority group count mismatch";
12031da177e4SLinus Torvalds 		r = -EINVAL;
12041da177e4SLinus Torvalds 		goto bad;
12051da177e4SLinus Torvalds 	}
12061da177e4SLinus Torvalds 
1207be240ff5SAnatol Pomazau 	spin_lock_irqsave(&m->lock, flags);
1208be240ff5SAnatol Pomazau 	enable_nopath_timeout(m);
1209be240ff5SAnatol Pomazau 	spin_unlock_irqrestore(&m->lock, flags);
1210be240ff5SAnatol Pomazau 
121155a62eefSAlasdair G Kergon 	ti->num_flush_bios = 1;
121255a62eefSAlasdair G Kergon 	ti->num_discard_bios = 1;
1213042bcef8SMike Snitzer 	ti->num_write_same_bios = 1;
1214ac62d620SChristoph Hellwig 	ti->num_write_zeroes_bios = 1;
12158d47e659SMike Snitzer 	if (m->queue_mode == DM_TYPE_BIO_BASED)
1216bf661be1SMike Snitzer 		ti->per_io_data_size = multipath_per_bio_data_size();
1217eb8db831SChristoph Hellwig 	else
12188637a6bfSMike Snitzer 		ti->per_io_data_size = sizeof(struct dm_mpath_io);
12198627921fSMikulas Patocka 
12201da177e4SLinus Torvalds 	return 0;
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds  bad:
12231da177e4SLinus Torvalds 	free_multipath(m);
12241da177e4SLinus Torvalds 	return r;
12251da177e4SLinus Torvalds }
12261da177e4SLinus Torvalds 
12272bded7bdSKiyoshi Ueda static void multipath_wait_for_pg_init_completion(struct multipath *m)
12282bded7bdSKiyoshi Ueda {
12299f4c3f87SBart Van Assche 	DEFINE_WAIT(wait);
12302bded7bdSKiyoshi Ueda 
12312bded7bdSKiyoshi Ueda 	while (1) {
12329f4c3f87SBart Van Assche 		prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE);
12332bded7bdSKiyoshi Ueda 
123491e968aaSMike Snitzer 		if (!atomic_read(&m->pg_init_in_progress))
12352bded7bdSKiyoshi Ueda 			break;
12362bded7bdSKiyoshi Ueda 
12372bded7bdSKiyoshi Ueda 		io_schedule();
12382bded7bdSKiyoshi Ueda 	}
12399f4c3f87SBart Van Assche 	finish_wait(&m->pg_init_wait, &wait);
12402bded7bdSKiyoshi Ueda }
12412bded7bdSKiyoshi Ueda 
12422bded7bdSKiyoshi Ueda static void flush_multipath_work(struct multipath *m)
12431da177e4SLinus Torvalds {
1244848b8aefSMike Snitzer 	if (m->hw_handler_name) {
1245518257b1SMike Snitzer 		set_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1246518257b1SMike Snitzer 		smp_mb__after_atomic();
1247954a73d5SShiva Krishna Merla 
1248935fcc56Swuzhouhui 		if (atomic_read(&m->pg_init_in_progress))
1249bab7cfc7SChandra Seetharaman 			flush_workqueue(kmpath_handlerd);
12502bded7bdSKiyoshi Ueda 		multipath_wait_for_pg_init_completion(m);
1251954a73d5SShiva Krishna Merla 
1252518257b1SMike Snitzer 		clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1253518257b1SMike Snitzer 		smp_mb__after_atomic();
12546df400abSKiyoshi Ueda 	}
12556df400abSKiyoshi Ueda 
1256935fcc56Swuzhouhui 	if (m->queue_mode == DM_TYPE_BIO_BASED)
1257935fcc56Swuzhouhui 		flush_work(&m->process_queued_bios);
1258848b8aefSMike Snitzer 	flush_work(&m->trigger_event);
1259848b8aefSMike Snitzer }
1260848b8aefSMike Snitzer 
12616df400abSKiyoshi Ueda static void multipath_dtr(struct dm_target *ti)
12626df400abSKiyoshi Ueda {
12636df400abSKiyoshi Ueda 	struct multipath *m = ti->private;
12646df400abSKiyoshi Ueda 
1265be240ff5SAnatol Pomazau 	disable_nopath_timeout(m);
12662bded7bdSKiyoshi Ueda 	flush_multipath_work(m);
12671da177e4SLinus Torvalds 	free_multipath(m);
12681da177e4SLinus Torvalds }
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds /*
12711da177e4SLinus Torvalds  * Take a path out of use.
12721da177e4SLinus Torvalds  */
12731da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath)
12741da177e4SLinus Torvalds {
12751da177e4SLinus Torvalds 	unsigned long flags;
12761da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
12791da177e4SLinus Torvalds 
12806680073dSKiyoshi Ueda 	if (!pgpath->is_active)
12811da177e4SLinus Torvalds 		goto out;
12821da177e4SLinus Torvalds 
128372d94861SAlasdair G Kergon 	DMWARN("Failing path %s.", pgpath->path.dev->name);
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 	pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
1286be7d31ccSMike Snitzer 	pgpath->is_active = false;
12871da177e4SLinus Torvalds 	pgpath->fail_count++;
12881da177e4SLinus Torvalds 
128991e968aaSMike Snitzer 	atomic_dec(&m->nr_valid_paths);
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 	if (pgpath == m->current_pgpath)
12921da177e4SLinus Torvalds 		m->current_pgpath = NULL;
12931da177e4SLinus Torvalds 
1294b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
129591e968aaSMike Snitzer 		       pgpath->path.dev->name, atomic_read(&m->nr_valid_paths));
1296b15546f9SMike Anderson 
1297fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
12981da177e4SLinus Torvalds 
1299be240ff5SAnatol Pomazau 	enable_nopath_timeout(m);
1300be240ff5SAnatol Pomazau 
13011da177e4SLinus Torvalds out:
13021da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 	return 0;
13051da177e4SLinus Torvalds }
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds /*
13081da177e4SLinus Torvalds  * Reinstate a previously-failed path
13091da177e4SLinus Torvalds  */
13101da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath)
13111da177e4SLinus Torvalds {
131263d832c3SHannes Reinecke 	int r = 0, run_queue = 0;
13131da177e4SLinus Torvalds 	unsigned long flags;
13141da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
131591e968aaSMike Snitzer 	unsigned nr_valid_paths;
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
13181da177e4SLinus Torvalds 
13196680073dSKiyoshi Ueda 	if (pgpath->is_active)
13201da177e4SLinus Torvalds 		goto out;
13211da177e4SLinus Torvalds 
1322ec31f3f7SMike Snitzer 	DMWARN("Reinstating path %s.", pgpath->path.dev->name);
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 	r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
13251da177e4SLinus Torvalds 	if (r)
13261da177e4SLinus Torvalds 		goto out;
13271da177e4SLinus Torvalds 
1328be7d31ccSMike Snitzer 	pgpath->is_active = true;
13291da177e4SLinus Torvalds 
133091e968aaSMike Snitzer 	nr_valid_paths = atomic_inc_return(&m->nr_valid_paths);
133191e968aaSMike Snitzer 	if (nr_valid_paths == 1) {
13321da177e4SLinus Torvalds 		m->current_pgpath = NULL;
133363d832c3SHannes Reinecke 		run_queue = 1;
1334e54f77ddSChandra Seetharaman 	} else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
13354e2d19e4SChandra Seetharaman 		if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
133691e968aaSMike Snitzer 			atomic_inc(&m->pg_init_in_progress);
1337e54f77ddSChandra Seetharaman 	}
13381da177e4SLinus Torvalds 
1339b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
134091e968aaSMike Snitzer 		       pgpath->path.dev->name, nr_valid_paths);
1341b15546f9SMike Anderson 
1342fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds out:
13451da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
134676e33fe4SMike Snitzer 	if (run_queue) {
134763d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
13487e48c768SMike Snitzer 		process_queued_io_list(m);
134976e33fe4SMike Snitzer 	}
13501da177e4SLinus Torvalds 
1351be240ff5SAnatol Pomazau 	if (pgpath->is_active)
1352be240ff5SAnatol Pomazau 		disable_nopath_timeout(m);
1353be240ff5SAnatol Pomazau 
13541da177e4SLinus Torvalds 	return r;
13551da177e4SLinus Torvalds }
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds /*
13581da177e4SLinus Torvalds  * Fail or reinstate all paths that match the provided struct dm_dev.
13591da177e4SLinus Torvalds  */
13601da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev,
13611da177e4SLinus Torvalds 		      action_fn action)
13621da177e4SLinus Torvalds {
136319040c0bSMike Snitzer 	int r = -EINVAL;
13641da177e4SLinus Torvalds 	struct pgpath *pgpath;
13651da177e4SLinus Torvalds 	struct priority_group *pg;
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
13681da177e4SLinus Torvalds 		list_for_each_entry(pgpath, &pg->pgpaths, list) {
13691da177e4SLinus Torvalds 			if (pgpath->path.dev == dev)
13701da177e4SLinus Torvalds 				r = action(pgpath);
13711da177e4SLinus Torvalds 		}
13721da177e4SLinus Torvalds 	}
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds 	return r;
13751da177e4SLinus Torvalds }
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds /*
13781da177e4SLinus Torvalds  * Temporarily try to avoid having to use the specified PG
13791da177e4SLinus Torvalds  */
13801da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg,
1381be7d31ccSMike Snitzer 		      bool bypassed)
13821da177e4SLinus Torvalds {
13831da177e4SLinus Torvalds 	unsigned long flags;
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	pg->bypassed = bypassed;
13881da177e4SLinus Torvalds 	m->current_pgpath = NULL;
13891da177e4SLinus Torvalds 	m->current_pg = NULL;
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
13921da177e4SLinus Torvalds 
1393fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
13941da177e4SLinus Torvalds }
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds /*
13971da177e4SLinus Torvalds  * Switch to using the specified PG from the next I/O that gets mapped
13981da177e4SLinus Torvalds  */
13991da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr)
14001da177e4SLinus Torvalds {
14011da177e4SLinus Torvalds 	struct priority_group *pg;
14021da177e4SLinus Torvalds 	unsigned pgnum;
14031da177e4SLinus Torvalds 	unsigned long flags;
140431998ef1SMikulas Patocka 	char dummy;
14051da177e4SLinus Torvalds 
140631998ef1SMikulas Patocka 	if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1407cc5bd925Stang.junhui 	    !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
14081da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to switch_pg_num");
14091da177e4SLinus Torvalds 		return -EINVAL;
14101da177e4SLinus Torvalds 	}
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
14131da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
1414be7d31ccSMike Snitzer 		pg->bypassed = false;
14151da177e4SLinus Torvalds 		if (--pgnum)
14161da177e4SLinus Torvalds 			continue;
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds 		m->current_pgpath = NULL;
14191da177e4SLinus Torvalds 		m->current_pg = NULL;
14201da177e4SLinus Torvalds 		m->next_pg = pg;
14211da177e4SLinus Torvalds 	}
14221da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
14231da177e4SLinus Torvalds 
1424fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
14251da177e4SLinus Torvalds 	return 0;
14261da177e4SLinus Torvalds }
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds /*
14291da177e4SLinus Torvalds  * Set/clear bypassed status of a PG.
14301da177e4SLinus Torvalds  * PGs are numbered upwards from 1 in the order they were declared.
14311da177e4SLinus Torvalds  */
1432be7d31ccSMike Snitzer static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed)
14331da177e4SLinus Torvalds {
14341da177e4SLinus Torvalds 	struct priority_group *pg;
14351da177e4SLinus Torvalds 	unsigned pgnum;
143631998ef1SMikulas Patocka 	char dummy;
14371da177e4SLinus Torvalds 
143831998ef1SMikulas Patocka 	if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1439cc5bd925Stang.junhui 	    !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
14401da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to bypass_pg");
14411da177e4SLinus Torvalds 		return -EINVAL;
14421da177e4SLinus Torvalds 	}
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
14451da177e4SLinus Torvalds 		if (!--pgnum)
14461da177e4SLinus Torvalds 			break;
14471da177e4SLinus Torvalds 	}
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds 	bypass_pg(m, pg, bypassed);
14501da177e4SLinus Torvalds 	return 0;
14511da177e4SLinus Torvalds }
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds /*
1454c9e45581SDave Wysochanski  * Should we retry pg_init immediately?
1455c9e45581SDave Wysochanski  */
1456be7d31ccSMike Snitzer static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1457c9e45581SDave Wysochanski {
1458c9e45581SDave Wysochanski 	unsigned long flags;
1459be7d31ccSMike Snitzer 	bool limit_reached = false;
1460c9e45581SDave Wysochanski 
1461c9e45581SDave Wysochanski 	spin_lock_irqsave(&m->lock, flags);
1462c9e45581SDave Wysochanski 
146391e968aaSMike Snitzer 	if (atomic_read(&m->pg_init_count) <= m->pg_init_retries &&
146491e968aaSMike Snitzer 	    !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
1465518257b1SMike Snitzer 		set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
1466c9e45581SDave Wysochanski 	else
1467be7d31ccSMike Snitzer 		limit_reached = true;
1468c9e45581SDave Wysochanski 
1469c9e45581SDave Wysochanski 	spin_unlock_irqrestore(&m->lock, flags);
1470c9e45581SDave Wysochanski 
1471c9e45581SDave Wysochanski 	return limit_reached;
1472c9e45581SDave Wysochanski }
1473c9e45581SDave Wysochanski 
14743ae31f6aSChandra Seetharaman static void pg_init_done(void *data, int errors)
1475cfae5c9bSChandra Seetharaman {
147683c0d5d5SMoger, Babu 	struct pgpath *pgpath = data;
1477cfae5c9bSChandra Seetharaman 	struct priority_group *pg = pgpath->pg;
1478cfae5c9bSChandra Seetharaman 	struct multipath *m = pg->m;
1479cfae5c9bSChandra Seetharaman 	unsigned long flags;
1480be7d31ccSMike Snitzer 	bool delay_retry = false;
1481cfae5c9bSChandra Seetharaman 
1482cfae5c9bSChandra Seetharaman 	/* device or driver problems */
1483cfae5c9bSChandra Seetharaman 	switch (errors) {
1484cfae5c9bSChandra Seetharaman 	case SCSI_DH_OK:
1485cfae5c9bSChandra Seetharaman 		break;
1486cfae5c9bSChandra Seetharaman 	case SCSI_DH_NOSYS:
1487cfae5c9bSChandra Seetharaman 		if (!m->hw_handler_name) {
1488cfae5c9bSChandra Seetharaman 			errors = 0;
1489cfae5c9bSChandra Seetharaman 			break;
1490cfae5c9bSChandra Seetharaman 		}
1491f7b934c8SMoger, Babu 		DMERR("Could not failover the device: Handler scsi_dh_%s "
1492f7b934c8SMoger, Babu 		      "Error %d.", m->hw_handler_name, errors);
1493cfae5c9bSChandra Seetharaman 		/*
1494cfae5c9bSChandra Seetharaman 		 * Fail path for now, so we do not ping pong
1495cfae5c9bSChandra Seetharaman 		 */
1496cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1497cfae5c9bSChandra Seetharaman 		break;
1498cfae5c9bSChandra Seetharaman 	case SCSI_DH_DEV_TEMP_BUSY:
1499cfae5c9bSChandra Seetharaman 		/*
1500cfae5c9bSChandra Seetharaman 		 * Probably doing something like FW upgrade on the
1501cfae5c9bSChandra Seetharaman 		 * controller so try the other pg.
1502cfae5c9bSChandra Seetharaman 		 */
1503be7d31ccSMike Snitzer 		bypass_pg(m, pg, true);
1504cfae5c9bSChandra Seetharaman 		break;
1505cfae5c9bSChandra Seetharaman 	case SCSI_DH_RETRY:
15064e2d19e4SChandra Seetharaman 		/* Wait before retrying. */
15074ecc5081Szhengbin 		delay_retry = true;
15087b06e09aSBart Van Assche 		/* fall through */
1509cfae5c9bSChandra Seetharaman 	case SCSI_DH_IMM_RETRY:
1510cfae5c9bSChandra Seetharaman 	case SCSI_DH_RES_TEMP_UNAVAIL:
1511cfae5c9bSChandra Seetharaman 		if (pg_init_limit_reached(m, pgpath))
1512cfae5c9bSChandra Seetharaman 			fail_path(pgpath);
1513cfae5c9bSChandra Seetharaman 		errors = 0;
1514cfae5c9bSChandra Seetharaman 		break;
1515ec31f3f7SMike Snitzer 	case SCSI_DH_DEV_OFFLINED:
1516cfae5c9bSChandra Seetharaman 	default:
1517cfae5c9bSChandra Seetharaman 		/*
1518cfae5c9bSChandra Seetharaman 		 * We probably do not want to fail the path for a device
1519cfae5c9bSChandra Seetharaman 		 * error, but this is what the old dm did. In future
1520cfae5c9bSChandra Seetharaman 		 * patches we can do more advanced handling.
1521cfae5c9bSChandra Seetharaman 		 */
1522cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1523cfae5c9bSChandra Seetharaman 	}
1524cfae5c9bSChandra Seetharaman 
1525cfae5c9bSChandra Seetharaman 	spin_lock_irqsave(&m->lock, flags);
1526cfae5c9bSChandra Seetharaman 	if (errors) {
1527e54f77ddSChandra Seetharaman 		if (pgpath == m->current_pgpath) {
1528cfae5c9bSChandra Seetharaman 			DMERR("Could not failover device. Error %d.", errors);
1529cfae5c9bSChandra Seetharaman 			m->current_pgpath = NULL;
1530cfae5c9bSChandra Seetharaman 			m->current_pg = NULL;
1531e54f77ddSChandra Seetharaman 		}
1532518257b1SMike Snitzer 	} else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
1533be7d31ccSMike Snitzer 		pg->bypassed = false;
1534cfae5c9bSChandra Seetharaman 
153591e968aaSMike Snitzer 	if (atomic_dec_return(&m->pg_init_in_progress) > 0)
1536d0259bf0SKiyoshi Ueda 		/* Activations of other paths are still on going */
1537d0259bf0SKiyoshi Ueda 		goto out;
1538d0259bf0SKiyoshi Ueda 
1539518257b1SMike Snitzer 	if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
1540518257b1SMike Snitzer 		if (delay_retry)
1541518257b1SMike Snitzer 			set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1542518257b1SMike Snitzer 		else
1543518257b1SMike Snitzer 			clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1544518257b1SMike Snitzer 
15453e9f1be1SHannes Reinecke 		if (__pg_init_all_paths(m))
15463e9f1be1SHannes Reinecke 			goto out;
15473e9f1be1SHannes Reinecke 	}
1548518257b1SMike Snitzer 	clear_bit(MPATHF_QUEUE_IO, &m->flags);
1549d0259bf0SKiyoshi Ueda 
15507e48c768SMike Snitzer 	process_queued_io_list(m);
155176e33fe4SMike Snitzer 
15522bded7bdSKiyoshi Ueda 	/*
15532bded7bdSKiyoshi Ueda 	 * Wake up any thread waiting to suspend.
15542bded7bdSKiyoshi Ueda 	 */
15552bded7bdSKiyoshi Ueda 	wake_up(&m->pg_init_wait);
15562bded7bdSKiyoshi Ueda 
1557d0259bf0SKiyoshi Ueda out:
1558cfae5c9bSChandra Seetharaman 	spin_unlock_irqrestore(&m->lock, flags);
1559cfae5c9bSChandra Seetharaman }
1560cfae5c9bSChandra Seetharaman 
156189bfce76SBart Van Assche static void activate_or_offline_path(struct pgpath *pgpath)
1562bab7cfc7SChandra Seetharaman {
1563f10e06b7SMike Snitzer 	struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1564bab7cfc7SChandra Seetharaman 
1565f10e06b7SMike Snitzer 	if (pgpath->is_active && !blk_queue_dying(q))
1566f10e06b7SMike Snitzer 		scsi_dh_activate(q, pg_init_done, pgpath);
15673a017509SHannes Reinecke 	else
15683a017509SHannes Reinecke 		pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
1569bab7cfc7SChandra Seetharaman }
1570bab7cfc7SChandra Seetharaman 
157189bfce76SBart Van Assche static void activate_path_work(struct work_struct *work)
157289bfce76SBart Van Assche {
157389bfce76SBart Van Assche 	struct pgpath *pgpath =
157489bfce76SBart Van Assche 		container_of(work, struct pgpath, activate_path.work);
157589bfce76SBart Van Assche 
157689bfce76SBart Van Assche 	activate_or_offline_path(pgpath);
157789bfce76SBart Van Assche }
157889bfce76SBart Van Assche 
1579b79f10eeSChristoph Hellwig static int multipath_end_io(struct dm_target *ti, struct request *clone,
15802a842acaSChristoph Hellwig 			    blk_status_t error, union map_info *map_context)
15811da177e4SLinus Torvalds {
1582b79f10eeSChristoph Hellwig 	struct dm_mpath_io *mpio = get_mpio(map_context);
1583b79f10eeSChristoph Hellwig 	struct pgpath *pgpath = mpio->pgpath;
15847ed8578aSChristoph Hellwig 	int r = DM_ENDIO_DONE;
1585b79f10eeSChristoph Hellwig 
1586f40c67f0SKiyoshi Ueda 	/*
1587f40c67f0SKiyoshi Ueda 	 * We don't queue any clone request inside the multipath target
1588f40c67f0SKiyoshi Ueda 	 * during end I/O handling, since those clone requests don't have
1589f40c67f0SKiyoshi Ueda 	 * bio clones.  If we queue them inside the multipath target,
1590f40c67f0SKiyoshi Ueda 	 * we need to make bio clones, that requires memory allocation.
15914cc96131SMike Snitzer 	 * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests
1592f40c67f0SKiyoshi Ueda 	 *  don't have bio clones.)
1593f40c67f0SKiyoshi Ueda 	 * Instead of queueing the clone request here, we queue the original
1594f40c67f0SKiyoshi Ueda 	 * request into dm core, which will remake a clone request and
1595f40c67f0SKiyoshi Ueda 	 * clone bios for it and resubmit it later.
1596f40c67f0SKiyoshi Ueda 	 */
1597a1275677SKeith Busch 	if (error && blk_path_error(error)) {
1598b79f10eeSChristoph Hellwig 		struct multipath *m = ti->private;
15991da177e4SLinus Torvalds 
1600ac514ffcSMike Snitzer 		if (error == BLK_STS_RESOURCE)
1601ac514ffcSMike Snitzer 			r = DM_ENDIO_DELAY_REQUEUE;
1602ac514ffcSMike Snitzer 		else
16037ed8578aSChristoph Hellwig 			r = DM_ENDIO_REQUEUE;
16041da177e4SLinus Torvalds 
1605b79f10eeSChristoph Hellwig 		if (pgpath)
1606b79f10eeSChristoph Hellwig 			fail_path(pgpath);
16071da177e4SLinus Torvalds 
1608ca5beb76SBart Van Assche 		if (atomic_read(&m->nr_valid_paths) == 0 &&
1609c1fd0abeSMike Snitzer 		    !must_push_back_rq(m)) {
16102a842acaSChristoph Hellwig 			if (error == BLK_STS_IOERR)
161118a482f5SChristoph Hellwig 				dm_report_EIO(m);
16127ed8578aSChristoph Hellwig 			/* complete with the original error */
16137ed8578aSChristoph Hellwig 			r = DM_ENDIO_DONE;
16147ed8578aSChristoph Hellwig 		}
16151da177e4SLinus Torvalds 	}
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds 	if (pgpath) {
1618b79f10eeSChristoph Hellwig 		struct path_selector *ps = &pgpath->pg->ps;
1619b79f10eeSChristoph Hellwig 
16201da177e4SLinus Torvalds 		if (ps->type->end_io)
1621087615bfSGabriel Krisman Bertazi 			ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes,
1622087615bfSGabriel Krisman Bertazi 					 clone->io_start_time_ns);
16231da177e4SLinus Torvalds 	}
16241da177e4SLinus Torvalds 
16257ed8578aSChristoph Hellwig 	return r;
16261da177e4SLinus Torvalds }
16271da177e4SLinus Torvalds 
16284e4cbee9SChristoph Hellwig static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone,
16294e4cbee9SChristoph Hellwig 				blk_status_t *error)
163076e33fe4SMike Snitzer {
163114ef1e48SChristoph Hellwig 	struct multipath *m = ti->private;
163214ef1e48SChristoph Hellwig 	struct dm_mpath_io *mpio = get_mpio_from_bio(clone);
163314ef1e48SChristoph Hellwig 	struct pgpath *pgpath = mpio->pgpath;
163476e33fe4SMike Snitzer 	unsigned long flags;
16351be56909SChristoph Hellwig 	int r = DM_ENDIO_DONE;
163676e33fe4SMike Snitzer 
1637a1275677SKeith Busch 	if (!*error || !blk_path_error(*error))
163814ef1e48SChristoph Hellwig 		goto done;
163976e33fe4SMike Snitzer 
164014ef1e48SChristoph Hellwig 	if (pgpath)
164114ef1e48SChristoph Hellwig 		fail_path(pgpath);
164276e33fe4SMike Snitzer 
1643ca5beb76SBart Van Assche 	if (atomic_read(&m->nr_valid_paths) == 0 &&
164418a482f5SChristoph Hellwig 	    !test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
1645c1fd0abeSMike Snitzer 		if (must_push_back_bio(m)) {
1646c1fd0abeSMike Snitzer 			r = DM_ENDIO_REQUEUE;
1647c1fd0abeSMike Snitzer 		} else {
164818a482f5SChristoph Hellwig 			dm_report_EIO(m);
16494e4cbee9SChristoph Hellwig 			*error = BLK_STS_IOERR;
1650c1fd0abeSMike Snitzer 		}
165114ef1e48SChristoph Hellwig 		goto done;
165218a482f5SChristoph Hellwig 	}
165376e33fe4SMike Snitzer 
165476e33fe4SMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
165576e33fe4SMike Snitzer 	bio_list_add(&m->queued_bios, clone);
165676e33fe4SMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
165776e33fe4SMike Snitzer 	if (!test_bit(MPATHF_QUEUE_IO, &m->flags))
165876e33fe4SMike Snitzer 		queue_work(kmultipathd, &m->process_queued_bios);
165976e33fe4SMike Snitzer 
16601be56909SChristoph Hellwig 	r = DM_ENDIO_INCOMPLETE;
166114ef1e48SChristoph Hellwig done:
166276e33fe4SMike Snitzer 	if (pgpath) {
166314ef1e48SChristoph Hellwig 		struct path_selector *ps = &pgpath->pg->ps;
166414ef1e48SChristoph Hellwig 
166576e33fe4SMike Snitzer 		if (ps->type->end_io)
1666087615bfSGabriel Krisman Bertazi 			ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes,
1667087615bfSGabriel Krisman Bertazi 					 dm_start_time_ns_from_clone(clone));
166876e33fe4SMike Snitzer 	}
166976e33fe4SMike Snitzer 
16701be56909SChristoph Hellwig 	return r;
167176e33fe4SMike Snitzer }
167276e33fe4SMike Snitzer 
16731da177e4SLinus Torvalds /*
16741da177e4SLinus Torvalds  * Suspend can't complete until all the I/O is processed so if
1675436d4108SAlasdair G Kergon  * the last path fails we must error any remaining I/O.
1676436d4108SAlasdair G Kergon  * Note that if the freeze_bdev fails while suspending, the
1677436d4108SAlasdair G Kergon  * queue_if_no_path state is lost - userspace should reset it.
16781da177e4SLinus Torvalds  */
16791da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti)
16801da177e4SLinus Torvalds {
16817943bd6dSMike Snitzer 	struct multipath *m = ti->private;
16821da177e4SLinus Torvalds 
1683be7d31ccSMike Snitzer 	queue_if_no_path(m, false, true);
16841da177e4SLinus Torvalds }
16851da177e4SLinus Torvalds 
16866df400abSKiyoshi Ueda static void multipath_postsuspend(struct dm_target *ti)
16876df400abSKiyoshi Ueda {
16886380f26fSMike Anderson 	struct multipath *m = ti->private;
16896380f26fSMike Anderson 
16906380f26fSMike Anderson 	mutex_lock(&m->work_mutex);
16912bded7bdSKiyoshi Ueda 	flush_multipath_work(m);
16926380f26fSMike Anderson 	mutex_unlock(&m->work_mutex);
16936df400abSKiyoshi Ueda }
16946df400abSKiyoshi Ueda 
1695436d4108SAlasdair G Kergon /*
1696436d4108SAlasdair G Kergon  * Restore the queue_if_no_path setting.
1697436d4108SAlasdair G Kergon  */
16981da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti)
16991da177e4SLinus Torvalds {
17007943bd6dSMike Snitzer 	struct multipath *m = ti->private;
17011814f2e3SMike Snitzer 	unsigned long flags;
17021da177e4SLinus Torvalds 
17031814f2e3SMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
17045307e2adSLukas Wunner 	assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags,
17055307e2adSLukas Wunner 		   test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags));
17061814f2e3SMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
17071da177e4SLinus Torvalds }
17081da177e4SLinus Torvalds 
17091da177e4SLinus Torvalds /*
17101da177e4SLinus Torvalds  * Info output has the following format:
17111da177e4SLinus Torvalds  * num_multipath_feature_args [multipath_feature_args]*
17121da177e4SLinus Torvalds  * num_handler_status_args [handler_status_args]*
17131da177e4SLinus Torvalds  * num_groups init_group_number
17141da177e4SLinus Torvalds  *            [A|D|E num_ps_status_args [ps_status_args]*
17151da177e4SLinus Torvalds  *             num_paths num_selector_args
17161da177e4SLinus Torvalds  *             [path_dev A|F fail_count [selector_args]* ]+ ]+
17171da177e4SLinus Torvalds  *
17181da177e4SLinus Torvalds  * Table output has the following format (identical to the constructor string):
17191da177e4SLinus Torvalds  * num_feature_args [features_args]*
17201da177e4SLinus Torvalds  * num_handler_args hw_handler [hw_handler_args]*
17211da177e4SLinus Torvalds  * num_groups init_group_number
17221da177e4SLinus Torvalds  *     [priority selector-name num_ps_args [ps_args]*
17231da177e4SLinus Torvalds  *      num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
17241da177e4SLinus Torvalds  */
1725fd7c092eSMikulas Patocka static void multipath_status(struct dm_target *ti, status_type_t type,
17261f4e0ff0SAlasdair G Kergon 			     unsigned status_flags, char *result, unsigned maxlen)
17271da177e4SLinus Torvalds {
17281da177e4SLinus Torvalds 	int sz = 0;
17291da177e4SLinus Torvalds 	unsigned long flags;
17307943bd6dSMike Snitzer 	struct multipath *m = ti->private;
17311da177e4SLinus Torvalds 	struct priority_group *pg;
17321da177e4SLinus Torvalds 	struct pgpath *p;
17331da177e4SLinus Torvalds 	unsigned pg_num;
17341da177e4SLinus Torvalds 	char state;
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds 	/* Features */
17391da177e4SLinus Torvalds 	if (type == STATUSTYPE_INFO)
174091e968aaSMike Snitzer 		DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags),
174191e968aaSMike Snitzer 		       atomic_read(&m->pg_init_count));
1742c9e45581SDave Wysochanski 	else {
1743518257b1SMike Snitzer 		DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) +
17444e2d19e4SChandra Seetharaman 			      (m->pg_init_retries > 0) * 2 +
1745a58a935dSMike Snitzer 			      (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
1746e83068a5SMike Snitzer 			      test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) +
1747e83068a5SMike Snitzer 			      (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2);
1748e83068a5SMike Snitzer 
1749518257b1SMike Snitzer 		if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
1750c9e45581SDave Wysochanski 			DMEMIT("queue_if_no_path ");
1751c9e45581SDave Wysochanski 		if (m->pg_init_retries)
1752c9e45581SDave Wysochanski 			DMEMIT("pg_init_retries %u ", m->pg_init_retries);
17534e2d19e4SChandra Seetharaman 		if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
17544e2d19e4SChandra Seetharaman 			DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
1755518257b1SMike Snitzer 		if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags))
1756a58a935dSMike Snitzer 			DMEMIT("retain_attached_hw_handler ");
1757e83068a5SMike Snitzer 		if (m->queue_mode != DM_TYPE_REQUEST_BASED) {
1758e83068a5SMike Snitzer 			switch(m->queue_mode) {
1759e83068a5SMike Snitzer 			case DM_TYPE_BIO_BASED:
1760e83068a5SMike Snitzer 				DMEMIT("queue_mode bio ");
1761e83068a5SMike Snitzer 				break;
17627e0d574fSBart Van Assche 			default:
17637e0d574fSBart Van Assche 				WARN_ON_ONCE(true);
17647e0d574fSBart Van Assche 				break;
1765e83068a5SMike Snitzer 			}
1766e83068a5SMike Snitzer 		}
1767c9e45581SDave Wysochanski 	}
17681da177e4SLinus Torvalds 
1769cfae5c9bSChandra Seetharaman 	if (!m->hw_handler_name || type == STATUSTYPE_INFO)
17701da177e4SLinus Torvalds 		DMEMIT("0 ");
17711da177e4SLinus Torvalds 	else
1772cfae5c9bSChandra Seetharaman 		DMEMIT("1 %s ", m->hw_handler_name);
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds 	DMEMIT("%u ", m->nr_priority_groups);
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds 	if (m->next_pg)
17771da177e4SLinus Torvalds 		pg_num = m->next_pg->pg_num;
17781da177e4SLinus Torvalds 	else if (m->current_pg)
17791da177e4SLinus Torvalds 		pg_num = m->current_pg->pg_num;
17801da177e4SLinus Torvalds 	else
1781a490a07aSMike Snitzer 		pg_num = (m->nr_priority_groups ? 1 : 0);
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 	DMEMIT("%u ", pg_num);
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds 	switch (type) {
17861da177e4SLinus Torvalds 	case STATUSTYPE_INFO:
17871da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
17881da177e4SLinus Torvalds 			if (pg->bypassed)
17891da177e4SLinus Torvalds 				state = 'D';	/* Disabled */
17901da177e4SLinus Torvalds 			else if (pg == m->current_pg)
17911da177e4SLinus Torvalds 				state = 'A';	/* Currently Active */
17921da177e4SLinus Torvalds 			else
17931da177e4SLinus Torvalds 				state = 'E';	/* Enabled */
17941da177e4SLinus Torvalds 
17951da177e4SLinus Torvalds 			DMEMIT("%c ", state);
17961da177e4SLinus Torvalds 
17971da177e4SLinus Torvalds 			if (pg->ps.type->status)
17981da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
17991da177e4SLinus Torvalds 							  result + sz,
18001da177e4SLinus Torvalds 							  maxlen - sz);
18011da177e4SLinus Torvalds 			else
18021da177e4SLinus Torvalds 				DMEMIT("0 ");
18031da177e4SLinus Torvalds 
18041da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
18051da177e4SLinus Torvalds 			       pg->ps.type->info_args);
18061da177e4SLinus Torvalds 
18071da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
18081da177e4SLinus Torvalds 				DMEMIT("%s %s %u ", p->path.dev->name,
18096680073dSKiyoshi Ueda 				       p->is_active ? "A" : "F",
18101da177e4SLinus Torvalds 				       p->fail_count);
18111da177e4SLinus Torvalds 				if (pg->ps.type->status)
18121da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
18131da177e4SLinus Torvalds 					      &p->path, type, result + sz,
18141da177e4SLinus Torvalds 					      maxlen - sz);
18151da177e4SLinus Torvalds 			}
18161da177e4SLinus Torvalds 		}
18171da177e4SLinus Torvalds 		break;
18181da177e4SLinus Torvalds 
18191da177e4SLinus Torvalds 	case STATUSTYPE_TABLE:
18201da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
18211da177e4SLinus Torvalds 			DMEMIT("%s ", pg->ps.type->name);
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds 			if (pg->ps.type->status)
18241da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
18251da177e4SLinus Torvalds 							  result + sz,
18261da177e4SLinus Torvalds 							  maxlen - sz);
18271da177e4SLinus Torvalds 			else
18281da177e4SLinus Torvalds 				DMEMIT("0 ");
18291da177e4SLinus Torvalds 
18301da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
18311da177e4SLinus Torvalds 			       pg->ps.type->table_args);
18321da177e4SLinus Torvalds 
18331da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
18341da177e4SLinus Torvalds 				DMEMIT("%s ", p->path.dev->name);
18351da177e4SLinus Torvalds 				if (pg->ps.type->status)
18361da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
18371da177e4SLinus Torvalds 					      &p->path, type, result + sz,
18381da177e4SLinus Torvalds 					      maxlen - sz);
18391da177e4SLinus Torvalds 			}
18401da177e4SLinus Torvalds 		}
18411da177e4SLinus Torvalds 		break;
18421da177e4SLinus Torvalds 	}
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
18451da177e4SLinus Torvalds }
18461da177e4SLinus Torvalds 
18471eb5fa84SMike Snitzer static int multipath_message(struct dm_target *ti, unsigned argc, char **argv,
18481eb5fa84SMike Snitzer 			     char *result, unsigned maxlen)
18491da177e4SLinus Torvalds {
18506380f26fSMike Anderson 	int r = -EINVAL;
18511da177e4SLinus Torvalds 	struct dm_dev *dev;
18527943bd6dSMike Snitzer 	struct multipath *m = ti->private;
18531da177e4SLinus Torvalds 	action_fn action;
1854be240ff5SAnatol Pomazau 	unsigned long flags;
18551da177e4SLinus Torvalds 
18566380f26fSMike Anderson 	mutex_lock(&m->work_mutex);
18576380f26fSMike Anderson 
1858c2f3d24bSKiyoshi Ueda 	if (dm_suspended(ti)) {
1859c2f3d24bSKiyoshi Ueda 		r = -EBUSY;
1860c2f3d24bSKiyoshi Ueda 		goto out;
1861c2f3d24bSKiyoshi Ueda 	}
1862c2f3d24bSKiyoshi Ueda 
18631da177e4SLinus Torvalds 	if (argc == 1) {
1864498f0103SMike Snitzer 		if (!strcasecmp(argv[0], "queue_if_no_path")) {
1865be7d31ccSMike Snitzer 			r = queue_if_no_path(m, true, false);
1866be240ff5SAnatol Pomazau 			spin_lock_irqsave(&m->lock, flags);
1867be240ff5SAnatol Pomazau 			enable_nopath_timeout(m);
1868be240ff5SAnatol Pomazau 			spin_unlock_irqrestore(&m->lock, flags);
18696380f26fSMike Anderson 			goto out;
1870498f0103SMike Snitzer 		} else if (!strcasecmp(argv[0], "fail_if_no_path")) {
1871be7d31ccSMike Snitzer 			r = queue_if_no_path(m, false, false);
1872be240ff5SAnatol Pomazau 			disable_nopath_timeout(m);
18736380f26fSMike Anderson 			goto out;
18746380f26fSMike Anderson 		}
18751da177e4SLinus Torvalds 	}
18761da177e4SLinus Torvalds 
18776380f26fSMike Anderson 	if (argc != 2) {
1878a356e426SJose Castillo 		DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
18796380f26fSMike Anderson 		goto out;
18806380f26fSMike Anderson 	}
18811da177e4SLinus Torvalds 
1882498f0103SMike Snitzer 	if (!strcasecmp(argv[0], "disable_group")) {
1883be7d31ccSMike Snitzer 		r = bypass_pg_num(m, argv[1], true);
18846380f26fSMike Anderson 		goto out;
1885498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "enable_group")) {
1886be7d31ccSMike Snitzer 		r = bypass_pg_num(m, argv[1], false);
18876380f26fSMike Anderson 		goto out;
1888498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "switch_group")) {
18896380f26fSMike Anderson 		r = switch_pg_num(m, argv[1]);
18906380f26fSMike Anderson 		goto out;
1891498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "reinstate_path"))
18921da177e4SLinus Torvalds 		action = reinstate_path;
1893498f0103SMike Snitzer 	else if (!strcasecmp(argv[0], "fail_path"))
18941da177e4SLinus Torvalds 		action = fail_path;
18956380f26fSMike Anderson 	else {
1896a356e426SJose Castillo 		DMWARN("Unrecognised multipath message received: %s", argv[0]);
18976380f26fSMike Anderson 		goto out;
18986380f26fSMike Anderson 	}
18991da177e4SLinus Torvalds 
19008215d6ecSNikanth Karthikesan 	r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
19011da177e4SLinus Torvalds 	if (r) {
190272d94861SAlasdair G Kergon 		DMWARN("message: error getting device %s",
19031da177e4SLinus Torvalds 		       argv[1]);
19046380f26fSMike Anderson 		goto out;
19051da177e4SLinus Torvalds 	}
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds 	r = action_dev(m, dev, action);
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds 	dm_put_device(ti, dev);
19101da177e4SLinus Torvalds 
19116380f26fSMike Anderson out:
19126380f26fSMike Anderson 	mutex_unlock(&m->work_mutex);
19131da177e4SLinus Torvalds 	return r;
19141da177e4SLinus Torvalds }
19151da177e4SLinus Torvalds 
1916e56f81e0SChristoph Hellwig static int multipath_prepare_ioctl(struct dm_target *ti,
19175bd5e8d8SMike Snitzer 				   struct block_device **bdev)
19189af4aa30SMilan Broz {
191935991652SMikulas Patocka 	struct multipath *m = ti->private;
19202da1610aSMike Snitzer 	struct pgpath *current_pgpath;
192135991652SMikulas Patocka 	int r;
192235991652SMikulas Patocka 
1923506458efSWill Deacon 	current_pgpath = READ_ONCE(m->current_pgpath);
19242361ae59SMartin Wilck 	if (!current_pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
19252da1610aSMike Snitzer 		current_pgpath = choose_pgpath(m, 0);
19269af4aa30SMilan Broz 
19272da1610aSMike Snitzer 	if (current_pgpath) {
1928518257b1SMike Snitzer 		if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) {
19292da1610aSMike Snitzer 			*bdev = current_pgpath->path.dev->bdev;
193043e43c9eSJunichi Nomura 			r = 0;
193143e43c9eSJunichi Nomura 		} else {
193243e43c9eSJunichi Nomura 			/* pg_init has not started or completed */
19336c182cd8SHannes Reinecke 			r = -ENOTCONN;
193443e43c9eSJunichi Nomura 		}
193543e43c9eSJunichi Nomura 	} else {
193643e43c9eSJunichi Nomura 		/* No path is available */
1937518257b1SMike Snitzer 		if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
193843e43c9eSJunichi Nomura 			r = -ENOTCONN;
193943e43c9eSJunichi Nomura 		else
19409af4aa30SMilan Broz 			r = -EIO;
194143e43c9eSJunichi Nomura 	}
19429af4aa30SMilan Broz 
19435bbbfdf6SJunichi Nomura 	if (r == -ENOTCONN) {
1944506458efSWill Deacon 		if (!READ_ONCE(m->current_pg)) {
19453e9f1be1SHannes Reinecke 			/* Path status changed, redo selection */
19462da1610aSMike Snitzer 			(void) choose_pgpath(m, 0);
19473e9f1be1SHannes Reinecke 		}
1948518257b1SMike Snitzer 		if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
19492da1610aSMike Snitzer 			pg_init_all_paths(m);
195063d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
19517e48c768SMike Snitzer 		process_queued_io_list(m);
19523e9f1be1SHannes Reinecke 	}
195335991652SMikulas Patocka 
1954e56f81e0SChristoph Hellwig 	/*
1955e56f81e0SChristoph Hellwig 	 * Only pass ioctls through if the device sizes match exactly.
1956e56f81e0SChristoph Hellwig 	 */
1957e56f81e0SChristoph Hellwig 	if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
1958e56f81e0SChristoph Hellwig 		return 1;
1959e56f81e0SChristoph Hellwig 	return r;
19609af4aa30SMilan Broz }
19619af4aa30SMilan Broz 
1962af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti,
1963af4874e0SMike Snitzer 				     iterate_devices_callout_fn fn, void *data)
1964af4874e0SMike Snitzer {
1965af4874e0SMike Snitzer 	struct multipath *m = ti->private;
1966af4874e0SMike Snitzer 	struct priority_group *pg;
1967af4874e0SMike Snitzer 	struct pgpath *p;
1968af4874e0SMike Snitzer 	int ret = 0;
1969af4874e0SMike Snitzer 
1970af4874e0SMike Snitzer 	list_for_each_entry(pg, &m->priority_groups, list) {
1971af4874e0SMike Snitzer 		list_for_each_entry(p, &pg->pgpaths, list) {
19725dea271bSMike Snitzer 			ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1973af4874e0SMike Snitzer 			if (ret)
1974af4874e0SMike Snitzer 				goto out;
1975af4874e0SMike Snitzer 		}
1976af4874e0SMike Snitzer 	}
1977af4874e0SMike Snitzer 
1978af4874e0SMike Snitzer out:
1979af4874e0SMike Snitzer 	return ret;
1980af4874e0SMike Snitzer }
1981af4874e0SMike Snitzer 
19829f54cec5SMike Snitzer static int pgpath_busy(struct pgpath *pgpath)
1983f40c67f0SKiyoshi Ueda {
1984f40c67f0SKiyoshi Ueda 	struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1985f40c67f0SKiyoshi Ueda 
198652b09914SMike Snitzer 	return blk_lld_busy(q);
1987f40c67f0SKiyoshi Ueda }
1988f40c67f0SKiyoshi Ueda 
1989f40c67f0SKiyoshi Ueda /*
1990f40c67f0SKiyoshi Ueda  * We return "busy", only when we can map I/Os but underlying devices
1991f40c67f0SKiyoshi Ueda  * are busy (so even if we map I/Os now, the I/Os will wait on
1992f40c67f0SKiyoshi Ueda  * the underlying queue).
1993f40c67f0SKiyoshi Ueda  * In other words, if we want to kill I/Os or queue them inside us
1994f40c67f0SKiyoshi Ueda  * due to map unavailability, we don't return "busy".  Otherwise,
1995f40c67f0SKiyoshi Ueda  * dm core won't give us the I/Os and we can't do what we want.
1996f40c67f0SKiyoshi Ueda  */
1997f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti)
1998f40c67f0SKiyoshi Ueda {
1999be7d31ccSMike Snitzer 	bool busy = false, has_active = false;
2000f40c67f0SKiyoshi Ueda 	struct multipath *m = ti->private;
20012da1610aSMike Snitzer 	struct priority_group *pg, *next_pg;
2002f40c67f0SKiyoshi Ueda 	struct pgpath *pgpath;
2003f40c67f0SKiyoshi Ueda 
2004b88efd43SMike Snitzer 	/* pg_init in progress */
2005b88efd43SMike Snitzer 	if (atomic_read(&m->pg_init_in_progress))
20062da1610aSMike Snitzer 		return true;
20072da1610aSMike Snitzer 
2008b88efd43SMike Snitzer 	/* no paths available, for blk-mq: rely on IO mapping to delay requeue */
2009b88efd43SMike Snitzer 	if (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
2010953923c0SMike Snitzer 		return (m->queue_mode != DM_TYPE_REQUEST_BASED);
2011b88efd43SMike Snitzer 
2012f40c67f0SKiyoshi Ueda 	/* Guess which priority_group will be used at next mapping time */
2013506458efSWill Deacon 	pg = READ_ONCE(m->current_pg);
2014506458efSWill Deacon 	next_pg = READ_ONCE(m->next_pg);
2015506458efSWill Deacon 	if (unlikely(!READ_ONCE(m->current_pgpath) && next_pg))
20162da1610aSMike Snitzer 		pg = next_pg;
20172da1610aSMike Snitzer 
20182da1610aSMike Snitzer 	if (!pg) {
2019f40c67f0SKiyoshi Ueda 		/*
2020f40c67f0SKiyoshi Ueda 		 * We don't know which pg will be used at next mapping time.
20212da1610aSMike Snitzer 		 * We don't call choose_pgpath() here to avoid to trigger
2022f40c67f0SKiyoshi Ueda 		 * pg_init just by busy checking.
2023f40c67f0SKiyoshi Ueda 		 * So we don't know whether underlying devices we will be using
2024f40c67f0SKiyoshi Ueda 		 * at next mapping time are busy or not. Just try mapping.
2025f40c67f0SKiyoshi Ueda 		 */
20262da1610aSMike Snitzer 		return busy;
20272da1610aSMike Snitzer 	}
2028f40c67f0SKiyoshi Ueda 
2029f40c67f0SKiyoshi Ueda 	/*
2030f40c67f0SKiyoshi Ueda 	 * If there is one non-busy active path at least, the path selector
2031f40c67f0SKiyoshi Ueda 	 * will be able to select it. So we consider such a pg as not busy.
2032f40c67f0SKiyoshi Ueda 	 */
2033be7d31ccSMike Snitzer 	busy = true;
20342da1610aSMike Snitzer 	list_for_each_entry(pgpath, &pg->pgpaths, list) {
2035f40c67f0SKiyoshi Ueda 		if (pgpath->is_active) {
2036be7d31ccSMike Snitzer 			has_active = true;
20379f54cec5SMike Snitzer 			if (!pgpath_busy(pgpath)) {
2038be7d31ccSMike Snitzer 				busy = false;
2039f40c67f0SKiyoshi Ueda 				break;
2040f40c67f0SKiyoshi Ueda 			}
2041f40c67f0SKiyoshi Ueda 		}
20422da1610aSMike Snitzer 	}
2043f40c67f0SKiyoshi Ueda 
20442da1610aSMike Snitzer 	if (!has_active) {
2045f40c67f0SKiyoshi Ueda 		/*
2046f40c67f0SKiyoshi Ueda 		 * No active path in this pg, so this pg won't be used and
2047f40c67f0SKiyoshi Ueda 		 * the current_pg will be changed at next mapping time.
2048f40c67f0SKiyoshi Ueda 		 * We need to try mapping to determine it.
2049f40c67f0SKiyoshi Ueda 		 */
2050be7d31ccSMike Snitzer 		busy = false;
20512da1610aSMike Snitzer 	}
2052f40c67f0SKiyoshi Ueda 
2053f40c67f0SKiyoshi Ueda 	return busy;
2054f40c67f0SKiyoshi Ueda }
2055f40c67f0SKiyoshi Ueda 
20561da177e4SLinus Torvalds /*-----------------------------------------------------------------
20571da177e4SLinus Torvalds  * Module setup
20581da177e4SLinus Torvalds  *---------------------------------------------------------------*/
20591da177e4SLinus Torvalds static struct target_type multipath_target = {
20601da177e4SLinus Torvalds 	.name = "multipath",
2061636be424SMike Snitzer 	.version = {1, 14, 0},
20628c5c1473SSteffen Maier 	.features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE |
20638c5c1473SSteffen Maier 		    DM_TARGET_PASSES_INTEGRITY,
20641da177e4SLinus Torvalds 	.module = THIS_MODULE,
20651da177e4SLinus Torvalds 	.ctr = multipath_ctr,
20661da177e4SLinus Torvalds 	.dtr = multipath_dtr,
2067e5863d9aSMike Snitzer 	.clone_and_map_rq = multipath_clone_and_map,
2068e5863d9aSMike Snitzer 	.release_clone_rq = multipath_release_clone,
2069f40c67f0SKiyoshi Ueda 	.rq_end_io = multipath_end_io,
207076e33fe4SMike Snitzer 	.map = multipath_map_bio,
207176e33fe4SMike Snitzer 	.end_io = multipath_end_io_bio,
207276e33fe4SMike Snitzer 	.presuspend = multipath_presuspend,
207376e33fe4SMike Snitzer 	.postsuspend = multipath_postsuspend,
207476e33fe4SMike Snitzer 	.resume = multipath_resume,
207576e33fe4SMike Snitzer 	.status = multipath_status,
207676e33fe4SMike Snitzer 	.message = multipath_message,
207776e33fe4SMike Snitzer 	.prepare_ioctl = multipath_prepare_ioctl,
207876e33fe4SMike Snitzer 	.iterate_devices = multipath_iterate_devices,
207976e33fe4SMike Snitzer 	.busy = multipath_busy,
208076e33fe4SMike Snitzer };
208176e33fe4SMike Snitzer 
20821da177e4SLinus Torvalds static int __init dm_multipath_init(void)
20831da177e4SLinus Torvalds {
20841da177e4SLinus Torvalds 	int r;
20851da177e4SLinus Torvalds 
20864d4d66abSTejun Heo 	kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
2087c557308eSAlasdair G Kergon 	if (!kmultipathd) {
20880cd33124SAlasdair G Kergon 		DMERR("failed to create workqueue kmpathd");
2089ff658e9cSJohannes Thumshirn 		r = -ENOMEM;
2090ff658e9cSJohannes Thumshirn 		goto bad_alloc_kmultipathd;
2091c557308eSAlasdair G Kergon 	}
2092c557308eSAlasdair G Kergon 
2093bab7cfc7SChandra Seetharaman 	/*
2094bab7cfc7SChandra Seetharaman 	 * A separate workqueue is used to handle the device handlers
2095bab7cfc7SChandra Seetharaman 	 * to avoid overloading existing workqueue. Overloading the
2096bab7cfc7SChandra Seetharaman 	 * old workqueue would also create a bottleneck in the
2097bab7cfc7SChandra Seetharaman 	 * path of the storage hardware device activation.
2098bab7cfc7SChandra Seetharaman 	 */
20994d4d66abSTejun Heo 	kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
21004d4d66abSTejun Heo 						  WQ_MEM_RECLAIM);
2101bab7cfc7SChandra Seetharaman 	if (!kmpath_handlerd) {
2102bab7cfc7SChandra Seetharaman 		DMERR("failed to create workqueue kmpath_handlerd");
2103ff658e9cSJohannes Thumshirn 		r = -ENOMEM;
2104ff658e9cSJohannes Thumshirn 		goto bad_alloc_kmpath_handlerd;
2105bab7cfc7SChandra Seetharaman 	}
2106bab7cfc7SChandra Seetharaman 
21077e6358d2Smonty_pavel@sina.com 	r = dm_register_target(&multipath_target);
21087e6358d2Smonty_pavel@sina.com 	if (r < 0) {
21097e6358d2Smonty_pavel@sina.com 		DMERR("request-based register failed %d", r);
21107e6358d2Smonty_pavel@sina.com 		r = -EINVAL;
21117e6358d2Smonty_pavel@sina.com 		goto bad_register_target;
21127e6358d2Smonty_pavel@sina.com 	}
21137e6358d2Smonty_pavel@sina.com 
2114ff658e9cSJohannes Thumshirn 	return 0;
2115ff658e9cSJohannes Thumshirn 
21167e6358d2Smonty_pavel@sina.com bad_register_target:
21177e6358d2Smonty_pavel@sina.com 	destroy_workqueue(kmpath_handlerd);
2118ff658e9cSJohannes Thumshirn bad_alloc_kmpath_handlerd:
2119ff658e9cSJohannes Thumshirn 	destroy_workqueue(kmultipathd);
2120ff658e9cSJohannes Thumshirn bad_alloc_kmultipathd:
21211da177e4SLinus Torvalds 	return r;
21221da177e4SLinus Torvalds }
21231da177e4SLinus Torvalds 
21241da177e4SLinus Torvalds static void __exit dm_multipath_exit(void)
21251da177e4SLinus Torvalds {
2126bab7cfc7SChandra Seetharaman 	destroy_workqueue(kmpath_handlerd);
2127c557308eSAlasdair G Kergon 	destroy_workqueue(kmultipathd);
2128c557308eSAlasdair G Kergon 
212910d3bd09SMikulas Patocka 	dm_unregister_target(&multipath_target);
21301da177e4SLinus Torvalds }
21311da177e4SLinus Torvalds 
21321da177e4SLinus Torvalds module_init(dm_multipath_init);
21331da177e4SLinus Torvalds module_exit(dm_multipath_exit);
21341da177e4SLinus Torvalds 
2135be240ff5SAnatol Pomazau module_param_named(queue_if_no_path_timeout_secs,
2136be240ff5SAnatol Pomazau 		   queue_if_no_path_timeout_secs, ulong, S_IRUGO | S_IWUSR);
2137be240ff5SAnatol Pomazau MODULE_PARM_DESC(queue_if_no_path_timeout_secs, "No available paths queue IO timeout in seconds");
2138be240ff5SAnatol Pomazau 
21391da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target");
21401da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
21411da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2142