xref: /openbmc/linux/drivers/md/dm-mpath.c (revision 848b8aefd44df99b3e38a872acb8d54d3530bebf)
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>
231da177e4SLinus Torvalds #include <linux/workqueue.h>
2435991652SMikulas Patocka #include <linux/delay.h>
25cfae5c9bSChandra Seetharaman #include <scsi/scsi_dh.h>
2660063497SArun Sharma #include <linux/atomic.h>
2778ce23b5SMike Snitzer #include <linux/blk-mq.h>
281da177e4SLinus Torvalds 
2972d94861SAlasdair G Kergon #define DM_MSG_PREFIX "multipath"
304e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_MSECS 2000
314e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds /* Path properties */
341da177e4SLinus Torvalds struct pgpath {
351da177e4SLinus Torvalds 	struct list_head list;
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds 	struct priority_group *pg;	/* Owning PG */
381da177e4SLinus Torvalds 	unsigned fail_count;		/* Cumulative failure count */
391da177e4SLinus Torvalds 
40c922d5f7SJosef "Jeff" Sipek 	struct dm_path path;
414e2d19e4SChandra Seetharaman 	struct delayed_work activate_path;
42be7d31ccSMike Snitzer 
43be7d31ccSMike Snitzer 	bool is_active:1;		/* Path status */
441da177e4SLinus Torvalds };
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /*
491da177e4SLinus Torvalds  * Paths are grouped into Priority Groups and numbered from 1 upwards.
501da177e4SLinus Torvalds  * Each has a path selector which controls which path gets used.
511da177e4SLinus Torvalds  */
521da177e4SLinus Torvalds struct priority_group {
531da177e4SLinus Torvalds 	struct list_head list;
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	struct multipath *m;		/* Owning multipath instance */
561da177e4SLinus Torvalds 	struct path_selector ps;
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 	unsigned pg_num;		/* Reference number */
591da177e4SLinus Torvalds 	unsigned nr_pgpaths;		/* Number of paths in PG */
601da177e4SLinus Torvalds 	struct list_head pgpaths;
61be7d31ccSMike Snitzer 
62be7d31ccSMike Snitzer 	bool bypassed:1;		/* Temporarily bypass this PG? */
631da177e4SLinus Torvalds };
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /* Multipath context */
661da177e4SLinus Torvalds struct multipath {
67*848b8aefSMike Snitzer 	unsigned long flags;		/* Multipath state flags */
684e2d19e4SChandra Seetharaman 
691fbdd2b3SMike Snitzer 	spinlock_t lock;
70*848b8aefSMike Snitzer 	enum dm_queue_mode queue_mode;
714e2d19e4SChandra Seetharaman 
721da177e4SLinus Torvalds 	struct pgpath *current_pgpath;
731da177e4SLinus Torvalds 	struct priority_group *current_pg;
741da177e4SLinus Torvalds 	struct priority_group *next_pg;	/* Switch to this PG if set */
751da177e4SLinus Torvalds 
76*848b8aefSMike Snitzer 	atomic_t nr_valid_paths;	/* Total number of usable paths */
77*848b8aefSMike Snitzer 	unsigned nr_priority_groups;
78*848b8aefSMike Snitzer 	struct list_head priority_groups;
791fbdd2b3SMike Snitzer 
80*848b8aefSMike Snitzer 	const char *hw_handler_name;
81*848b8aefSMike Snitzer 	char *hw_handler_params;
82*848b8aefSMike Snitzer 	wait_queue_head_t pg_init_wait;	/* Wait for pg_init completion */
83c9e45581SDave Wysochanski 	unsigned pg_init_retries;	/* Number of times to retry pg_init */
844e2d19e4SChandra Seetharaman 	unsigned pg_init_delay_msecs;	/* Number of msecs before pg_init retry */
8591e968aaSMike Snitzer 	atomic_t pg_init_in_progress;	/* Only one pg_init allowed at once */
8691e968aaSMike Snitzer 	atomic_t pg_init_count;		/* Number of times pg_init called */
8791e968aaSMike Snitzer 
886380f26fSMike Anderson 	struct mutex work_mutex;
8920800cb3SMike Snitzer 	struct work_struct trigger_event;
90*848b8aefSMike Snitzer 	struct dm_target *ti;
9176e33fe4SMike Snitzer 
9276e33fe4SMike Snitzer 	struct work_struct process_queued_bios;
9376e33fe4SMike Snitzer 	struct bio_list queued_bios;
941da177e4SLinus Torvalds };
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds /*
9776e33fe4SMike Snitzer  * Context information attached to each io we process.
981da177e4SLinus Torvalds  */
99028867acSAlasdair G Kergon struct dm_mpath_io {
1001da177e4SLinus Torvalds 	struct pgpath *pgpath;
10102ab823fSKiyoshi Ueda 	size_t nr_bytes;
1021da177e4SLinus Torvalds };
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds typedef int (*action_fn) (struct pgpath *pgpath);
1051da177e4SLinus Torvalds 
106bab7cfc7SChandra Seetharaman static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
107c4028958SDavid Howells static void trigger_event(struct work_struct *work);
10889bfce76SBart Van Assche static void activate_or_offline_path(struct pgpath *pgpath);
10989bfce76SBart Van Assche static void activate_path_work(struct work_struct *work);
11076e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work);
1111da177e4SLinus Torvalds 
112518257b1SMike Snitzer /*-----------------------------------------------
113518257b1SMike Snitzer  * Multipath state flags.
114518257b1SMike Snitzer  *-----------------------------------------------*/
115518257b1SMike Snitzer 
116518257b1SMike Snitzer #define MPATHF_QUEUE_IO 0			/* Must we queue all I/O? */
117518257b1SMike Snitzer #define MPATHF_QUEUE_IF_NO_PATH 1		/* Queue I/O if last path fails? */
118518257b1SMike Snitzer #define MPATHF_SAVED_QUEUE_IF_NO_PATH 2		/* Saved state during suspension */
119518257b1SMike Snitzer #define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3	/* If there's already a hw_handler present, don't change it. */
120518257b1SMike Snitzer #define MPATHF_PG_INIT_DISABLED 4		/* pg_init is not currently allowed */
121518257b1SMike Snitzer #define MPATHF_PG_INIT_REQUIRED 5		/* pg_init needs calling? */
122518257b1SMike Snitzer #define MPATHF_PG_INIT_DELAY_RETRY 6		/* Delay pg_init retry? */
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds /*-----------------------------------------------
1251da177e4SLinus Torvalds  * Allocation routines
1261da177e4SLinus Torvalds  *-----------------------------------------------*/
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds static struct pgpath *alloc_pgpath(void)
1291da177e4SLinus Torvalds {
130e69fae56SMicha³ Miros³aw 	struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
1311da177e4SLinus Torvalds 
132*848b8aefSMike Snitzer 	if (!pgpath)
133*848b8aefSMike Snitzer 		return NULL;
134*848b8aefSMike Snitzer 
135be7d31ccSMike Snitzer 	pgpath->is_active = true;
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	return pgpath;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
140028867acSAlasdair G Kergon static void free_pgpath(struct pgpath *pgpath)
1411da177e4SLinus Torvalds {
1421da177e4SLinus Torvalds 	kfree(pgpath);
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds static struct priority_group *alloc_priority_group(void)
1461da177e4SLinus Torvalds {
1471da177e4SLinus Torvalds 	struct priority_group *pg;
1481da177e4SLinus Torvalds 
149e69fae56SMicha³ Miros³aw 	pg = kzalloc(sizeof(*pg), GFP_KERNEL);
1501da177e4SLinus Torvalds 
151e69fae56SMicha³ Miros³aw 	if (pg)
1521da177e4SLinus Torvalds 		INIT_LIST_HEAD(&pg->pgpaths);
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	return pg;
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
1581da177e4SLinus Torvalds {
1591da177e4SLinus Torvalds 	struct pgpath *pgpath, *tmp;
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
1621da177e4SLinus Torvalds 		list_del(&pgpath->list);
1631da177e4SLinus Torvalds 		dm_put_device(ti, pgpath->path.dev);
1641da177e4SLinus Torvalds 		free_pgpath(pgpath);
1651da177e4SLinus Torvalds 	}
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds static void free_priority_group(struct priority_group *pg,
1691da177e4SLinus Torvalds 				struct dm_target *ti)
1701da177e4SLinus Torvalds {
1711da177e4SLinus Torvalds 	struct path_selector *ps = &pg->ps;
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	if (ps->type) {
1741da177e4SLinus Torvalds 		ps->type->destroy(ps);
1751da177e4SLinus Torvalds 		dm_put_path_selector(ps->type);
1761da177e4SLinus Torvalds 	}
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	free_pgpaths(&pg->pgpaths, ti);
1791da177e4SLinus Torvalds 	kfree(pg);
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
182e83068a5SMike Snitzer static struct multipath *alloc_multipath(struct dm_target *ti)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	struct multipath *m;
1851da177e4SLinus Torvalds 
186e69fae56SMicha³ Miros³aw 	m = kzalloc(sizeof(*m), GFP_KERNEL);
1871da177e4SLinus Torvalds 	if (m) {
1881da177e4SLinus Torvalds 		INIT_LIST_HEAD(&m->priority_groups);
1891da177e4SLinus Torvalds 		spin_lock_init(&m->lock);
19091e968aaSMike Snitzer 		atomic_set(&m->nr_valid_paths, 0);
191c4028958SDavid Howells 		INIT_WORK(&m->trigger_event, trigger_event);
1926380f26fSMike Anderson 		mutex_init(&m->work_mutex);
1938637a6bfSMike Snitzer 
194e83068a5SMike Snitzer 		m->queue_mode = DM_TYPE_NONE;
195e83068a5SMike Snitzer 
196e83068a5SMike Snitzer 		m->ti = ti;
197e83068a5SMike Snitzer 		ti->private = m;
198e83068a5SMike Snitzer 	}
199e83068a5SMike Snitzer 
200e83068a5SMike Snitzer 	return m;
201e83068a5SMike Snitzer }
202e83068a5SMike Snitzer 
203e83068a5SMike Snitzer static int alloc_multipath_stage2(struct dm_target *ti, struct multipath *m)
204e83068a5SMike Snitzer {
205e83068a5SMike Snitzer 	if (m->queue_mode == DM_TYPE_NONE) {
206e83068a5SMike Snitzer 		/*
207e83068a5SMike Snitzer 		 * Default to request-based.
208e83068a5SMike Snitzer 		 */
209e83068a5SMike Snitzer 		if (dm_use_blk_mq(dm_table_get_md(ti->table)))
210e83068a5SMike Snitzer 			m->queue_mode = DM_TYPE_MQ_REQUEST_BASED;
211e83068a5SMike Snitzer 		else
212e83068a5SMike Snitzer 			m->queue_mode = DM_TYPE_REQUEST_BASED;
213cd025384SMike Snitzer 
214cd025384SMike Snitzer 	} else if (m->queue_mode == DM_TYPE_BIO_BASED ||
215cd025384SMike Snitzer 		   m->queue_mode == DM_TYPE_NVME_BIO_BASED) {
21676e33fe4SMike Snitzer 		INIT_WORK(&m->process_queued_bios, process_queued_bios);
217cd025384SMike Snitzer 
218cd025384SMike Snitzer 		if (m->queue_mode == DM_TYPE_BIO_BASED) {
21976e33fe4SMike Snitzer 			/*
22076e33fe4SMike Snitzer 			 * bio-based doesn't support any direct scsi_dh management;
22176e33fe4SMike Snitzer 			 * it just discovers if a scsi_dh is attached.
22276e33fe4SMike Snitzer 			 */
22376e33fe4SMike Snitzer 			set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
22476e33fe4SMike Snitzer 		}
225cd025384SMike Snitzer 	}
22676e33fe4SMike Snitzer 
227*848b8aefSMike Snitzer 	if (m->queue_mode != DM_TYPE_NVME_BIO_BASED) {
228*848b8aefSMike Snitzer 		set_bit(MPATHF_QUEUE_IO, &m->flags);
229*848b8aefSMike Snitzer 		atomic_set(&m->pg_init_in_progress, 0);
230*848b8aefSMike Snitzer 		atomic_set(&m->pg_init_count, 0);
231*848b8aefSMike Snitzer 		m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
232*848b8aefSMike Snitzer 		init_waitqueue_head(&m->pg_init_wait);
233*848b8aefSMike Snitzer 	}
234*848b8aefSMike Snitzer 
235e83068a5SMike Snitzer 	dm_table_set_type(ti->table, m->queue_mode);
2361da177e4SLinus Torvalds 
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);
2511da177e4SLinus Torvalds 	kfree(m);
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds 
2542eff1924SMike Snitzer static struct dm_mpath_io *get_mpio(union map_info *info)
2552eff1924SMike Snitzer {
2562eff1924SMike Snitzer 	return info->ptr;
2572eff1924SMike Snitzer }
2582eff1924SMike Snitzer 
259bf661be1SMike Snitzer static size_t multipath_per_bio_data_size(void)
26076e33fe4SMike Snitzer {
261bf661be1SMike Snitzer 	return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details);
26276e33fe4SMike Snitzer }
26376e33fe4SMike Snitzer 
264bf661be1SMike Snitzer static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio)
265bf661be1SMike Snitzer {
266bf661be1SMike Snitzer 	return dm_per_bio_data(bio, multipath_per_bio_data_size());
267bf661be1SMike Snitzer }
268bf661be1SMike Snitzer 
269d07a241dSMike Snitzer static struct dm_bio_details *get_bio_details_from_mpio(struct dm_mpath_io *mpio)
270bf661be1SMike Snitzer {
271bf661be1SMike Snitzer 	/* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */
272bf661be1SMike Snitzer 	void *bio_details = mpio + 1;
273bf661be1SMike Snitzer 	return bio_details;
274bf661be1SMike Snitzer }
275bf661be1SMike Snitzer 
27663f6e6fdSMike Snitzer static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p)
27776e33fe4SMike Snitzer {
27876e33fe4SMike Snitzer 	struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
279d07a241dSMike Snitzer 	struct dm_bio_details *bio_details = get_bio_details_from_mpio(mpio);
28076e33fe4SMike Snitzer 
281d0442f80SMike Snitzer 	mpio->nr_bytes = bio->bi_iter.bi_size;
282d0442f80SMike Snitzer 	mpio->pgpath = NULL;
283bf661be1SMike Snitzer 	*mpio_p = mpio;
284d0442f80SMike Snitzer 
285d0442f80SMike Snitzer 	dm_bio_record(bio_details, bio);
28676e33fe4SMike Snitzer }
28776e33fe4SMike Snitzer 
2881da177e4SLinus Torvalds /*-----------------------------------------------
2891da177e4SLinus Torvalds  * Path selection
2901da177e4SLinus Torvalds  *-----------------------------------------------*/
2911da177e4SLinus Torvalds 
2923e9f1be1SHannes Reinecke static int __pg_init_all_paths(struct multipath *m)
293fb612642SKiyoshi Ueda {
294fb612642SKiyoshi Ueda 	struct pgpath *pgpath;
2954e2d19e4SChandra Seetharaman 	unsigned long pg_init_delay = 0;
296fb612642SKiyoshi Ueda 
297b194679fSBart Van Assche 	lockdep_assert_held(&m->lock);
298b194679fSBart Van Assche 
29991e968aaSMike Snitzer 	if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
3003e9f1be1SHannes Reinecke 		return 0;
30117f4ff45SHannes Reinecke 
30291e968aaSMike Snitzer 	atomic_inc(&m->pg_init_count);
303518257b1SMike Snitzer 	clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
3043e9f1be1SHannes Reinecke 
3053e9f1be1SHannes Reinecke 	/* Check here to reset pg_init_required */
3063e9f1be1SHannes Reinecke 	if (!m->current_pg)
3073e9f1be1SHannes Reinecke 		return 0;
3083e9f1be1SHannes Reinecke 
309518257b1SMike Snitzer 	if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags))
3104e2d19e4SChandra Seetharaman 		pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
3114e2d19e4SChandra Seetharaman 						 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
312fb612642SKiyoshi Ueda 	list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
313fb612642SKiyoshi Ueda 		/* Skip failed paths */
314fb612642SKiyoshi Ueda 		if (!pgpath->is_active)
315fb612642SKiyoshi Ueda 			continue;
3164e2d19e4SChandra Seetharaman 		if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
3174e2d19e4SChandra Seetharaman 				       pg_init_delay))
31891e968aaSMike Snitzer 			atomic_inc(&m->pg_init_in_progress);
319fb612642SKiyoshi Ueda 	}
32091e968aaSMike Snitzer 	return atomic_read(&m->pg_init_in_progress);
321fb612642SKiyoshi Ueda }
322fb612642SKiyoshi Ueda 
323c1d7ecf7SBart Van Assche static int pg_init_all_paths(struct multipath *m)
3241da177e4SLinus Torvalds {
325c1d7ecf7SBart Van Assche 	int ret;
3262da1610aSMike Snitzer 	unsigned long flags;
3272da1610aSMike Snitzer 
3282da1610aSMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
329c1d7ecf7SBart Van Assche 	ret = __pg_init_all_paths(m);
3302da1610aSMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
331c1d7ecf7SBart Van Assche 
332c1d7ecf7SBart Van Assche 	return ret;
3332da1610aSMike Snitzer }
3342da1610aSMike Snitzer 
3352da1610aSMike Snitzer static void __switch_pg(struct multipath *m, struct priority_group *pg)
3362da1610aSMike Snitzer {
3372da1610aSMike Snitzer 	m->current_pg = pg;
3381da177e4SLinus Torvalds 
339*848b8aefSMike Snitzer 	if (m->queue_mode == DM_TYPE_NVME_BIO_BASED)
340*848b8aefSMike Snitzer 		return;
341*848b8aefSMike Snitzer 
3421da177e4SLinus Torvalds 	/* Must we initialise the PG first, and queue I/O till it's ready? */
343cfae5c9bSChandra Seetharaman 	if (m->hw_handler_name) {
344518257b1SMike Snitzer 		set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
345518257b1SMike Snitzer 		set_bit(MPATHF_QUEUE_IO, &m->flags);
3461da177e4SLinus Torvalds 	} else {
347518257b1SMike Snitzer 		clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
348518257b1SMike Snitzer 		clear_bit(MPATHF_QUEUE_IO, &m->flags);
3491da177e4SLinus Torvalds 	}
350c9e45581SDave Wysochanski 
35191e968aaSMike Snitzer 	atomic_set(&m->pg_init_count, 0);
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
3542da1610aSMike Snitzer static struct pgpath *choose_path_in_pg(struct multipath *m,
3552da1610aSMike Snitzer 					struct priority_group *pg,
35602ab823fSKiyoshi Ueda 					size_t nr_bytes)
3571da177e4SLinus Torvalds {
3582da1610aSMike Snitzer 	unsigned long flags;
359c922d5f7SJosef "Jeff" Sipek 	struct dm_path *path;
3602da1610aSMike Snitzer 	struct pgpath *pgpath;
3611da177e4SLinus Torvalds 
36290a4323cSMike Snitzer 	path = pg->ps.type->select_path(&pg->ps, nr_bytes);
3631da177e4SLinus Torvalds 	if (!path)
3642da1610aSMike Snitzer 		return ERR_PTR(-ENXIO);
3651da177e4SLinus Torvalds 
3662da1610aSMike Snitzer 	pgpath = path_to_pgpath(path);
3671da177e4SLinus Torvalds 
368506458efSWill Deacon 	if (unlikely(READ_ONCE(m->current_pg) != pg)) {
3692da1610aSMike Snitzer 		/* Only update current_pgpath if pg changed */
3702da1610aSMike Snitzer 		spin_lock_irqsave(&m->lock, flags);
3712da1610aSMike Snitzer 		m->current_pgpath = pgpath;
3722da1610aSMike Snitzer 		__switch_pg(m, pg);
3732da1610aSMike Snitzer 		spin_unlock_irqrestore(&m->lock, flags);
3741da177e4SLinus Torvalds 	}
3751da177e4SLinus Torvalds 
3762da1610aSMike Snitzer 	return pgpath;
3772da1610aSMike Snitzer }
3782da1610aSMike Snitzer 
3792da1610aSMike Snitzer static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes)
3801da177e4SLinus Torvalds {
3812da1610aSMike Snitzer 	unsigned long flags;
3821da177e4SLinus Torvalds 	struct priority_group *pg;
3832da1610aSMike Snitzer 	struct pgpath *pgpath;
384d19a55ccSMike Snitzer 	unsigned bypassed = 1;
3851da177e4SLinus Torvalds 
38691e968aaSMike Snitzer 	if (!atomic_read(&m->nr_valid_paths)) {
387*848b8aefSMike Snitzer 		if (m->queue_mode != DM_TYPE_NVME_BIO_BASED)
388518257b1SMike Snitzer 			clear_bit(MPATHF_QUEUE_IO, &m->flags);
3891da177e4SLinus Torvalds 		goto failed;
3901f271972SBenjamin Marzinski 	}
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	/* Were we instructed to switch PG? */
393506458efSWill Deacon 	if (READ_ONCE(m->next_pg)) {
3942da1610aSMike Snitzer 		spin_lock_irqsave(&m->lock, flags);
3951da177e4SLinus Torvalds 		pg = m->next_pg;
3962da1610aSMike Snitzer 		if (!pg) {
3972da1610aSMike Snitzer 			spin_unlock_irqrestore(&m->lock, flags);
3982da1610aSMike Snitzer 			goto check_current_pg;
3992da1610aSMike Snitzer 		}
4001da177e4SLinus Torvalds 		m->next_pg = NULL;
4012da1610aSMike Snitzer 		spin_unlock_irqrestore(&m->lock, flags);
4022da1610aSMike Snitzer 		pgpath = choose_path_in_pg(m, pg, nr_bytes);
4032da1610aSMike Snitzer 		if (!IS_ERR_OR_NULL(pgpath))
4042da1610aSMike Snitzer 			return pgpath;
4051da177e4SLinus Torvalds 	}
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	/* Don't change PG until it has no remaining paths */
4082da1610aSMike Snitzer check_current_pg:
409506458efSWill Deacon 	pg = READ_ONCE(m->current_pg);
4102da1610aSMike Snitzer 	if (pg) {
4112da1610aSMike Snitzer 		pgpath = choose_path_in_pg(m, pg, nr_bytes);
4122da1610aSMike Snitzer 		if (!IS_ERR_OR_NULL(pgpath))
4132da1610aSMike Snitzer 			return pgpath;
4142da1610aSMike Snitzer 	}
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	/*
4171da177e4SLinus Torvalds 	 * Loop through priority groups until we find a valid path.
4181da177e4SLinus Torvalds 	 * First time we skip PGs marked 'bypassed'.
419f220fd4eSMike Christie 	 * Second time we only try the ones we skipped, but set
420f220fd4eSMike Christie 	 * pg_init_delay_retry so we do not hammer controllers.
4211da177e4SLinus Torvalds 	 */
4221da177e4SLinus Torvalds 	do {
4231da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
424d19a55ccSMike Snitzer 			if (pg->bypassed == !!bypassed)
4251da177e4SLinus Torvalds 				continue;
4262da1610aSMike Snitzer 			pgpath = choose_path_in_pg(m, pg, nr_bytes);
4272da1610aSMike Snitzer 			if (!IS_ERR_OR_NULL(pgpath)) {
428f220fd4eSMike Christie 				if (!bypassed)
429518257b1SMike Snitzer 					set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
4302da1610aSMike Snitzer 				return pgpath;
4311da177e4SLinus Torvalds 			}
432f220fd4eSMike Christie 		}
4331da177e4SLinus Torvalds 	} while (bypassed--);
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds failed:
4362da1610aSMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
4371da177e4SLinus Torvalds 	m->current_pgpath = NULL;
4381da177e4SLinus Torvalds 	m->current_pg = NULL;
4392da1610aSMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
4402da1610aSMike Snitzer 
4412da1610aSMike Snitzer 	return NULL;
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
44445e15720SKiyoshi Ueda /*
44586331f39SBart Van Assche  * dm_report_EIO() is a macro instead of a function to make pr_debug()
44686331f39SBart Van Assche  * report the function name and line number of the function from which
44786331f39SBart Van Assche  * it has been invoked.
44845e15720SKiyoshi Ueda  */
44986331f39SBart Van Assche #define dm_report_EIO(m)						\
45018a482f5SChristoph Hellwig do {									\
45186331f39SBart Van Assche 	struct mapped_device *md = dm_table_get_md((m)->ti->table);	\
45286331f39SBart Van Assche 									\
45386331f39SBart Van Assche 	pr_debug("%s: returning EIO; QIFNP = %d; SQIFNP = %d; DNFS = %d\n", \
45486331f39SBart Van Assche 		 dm_device_name(md),					\
45586331f39SBart Van Assche 		 test_bit(MPATHF_QUEUE_IF_NO_PATH, &(m)->flags),	\
45686331f39SBart Van Assche 		 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &(m)->flags),	\
45786331f39SBart Van Assche 		 dm_noflush_suspending((m)->ti));			\
45818a482f5SChristoph Hellwig } while (0)
45945e15720SKiyoshi Ueda 
46036fcffccSHannes Reinecke /*
461c1fd0abeSMike Snitzer  * Check whether bios must be queued in the device-mapper core rather
462c1fd0abeSMike Snitzer  * than here in the target.
463c1fd0abeSMike Snitzer  *
464c1fd0abeSMike Snitzer  * If MPATHF_QUEUE_IF_NO_PATH and MPATHF_SAVED_QUEUE_IF_NO_PATH hold
465c1fd0abeSMike Snitzer  * the same value then we are not between multipath_presuspend()
466c1fd0abeSMike Snitzer  * and multipath_resume() calls and we have no need to check
467c1fd0abeSMike Snitzer  * for the DMF_NOFLUSH_SUSPENDING flag.
468c1fd0abeSMike Snitzer  */
469c1fd0abeSMike Snitzer static bool __must_push_back(struct multipath *m, unsigned long flags)
470c1fd0abeSMike Snitzer {
471c1fd0abeSMike Snitzer 	return ((test_bit(MPATHF_QUEUE_IF_NO_PATH, &flags) !=
472c1fd0abeSMike Snitzer 		 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &flags)) &&
473c1fd0abeSMike Snitzer 		dm_noflush_suspending(m->ti));
474c1fd0abeSMike Snitzer }
475c1fd0abeSMike Snitzer 
476c1fd0abeSMike Snitzer /*
477c1fd0abeSMike Snitzer  * Following functions use READ_ONCE to get atomic access to
478c1fd0abeSMike Snitzer  * all m->flags to avoid taking spinlock
479c1fd0abeSMike Snitzer  */
480c1fd0abeSMike Snitzer static bool must_push_back_rq(struct multipath *m)
481c1fd0abeSMike Snitzer {
482c1fd0abeSMike Snitzer 	unsigned long flags = READ_ONCE(m->flags);
483c1fd0abeSMike Snitzer 	return test_bit(MPATHF_QUEUE_IF_NO_PATH, &flags) || __must_push_back(m, flags);
484c1fd0abeSMike Snitzer }
485c1fd0abeSMike Snitzer 
486c1fd0abeSMike Snitzer static bool must_push_back_bio(struct multipath *m)
487c1fd0abeSMike Snitzer {
488c1fd0abeSMike Snitzer 	unsigned long flags = READ_ONCE(m->flags);
489c1fd0abeSMike Snitzer 	return __must_push_back(m, flags);
490c1fd0abeSMike Snitzer }
491c1fd0abeSMike Snitzer 
492c1fd0abeSMike Snitzer /*
49376e33fe4SMike Snitzer  * Map cloned requests (request-based multipath)
49436fcffccSHannes Reinecke  */
495eb8db831SChristoph Hellwig static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
496e5863d9aSMike Snitzer 				   union map_info *map_context,
497eb8db831SChristoph Hellwig 				   struct request **__clone)
4981da177e4SLinus Torvalds {
4997943bd6dSMike Snitzer 	struct multipath *m = ti->private;
500eb8db831SChristoph Hellwig 	size_t nr_bytes = blk_rq_bytes(rq);
5011da177e4SLinus Torvalds 	struct pgpath *pgpath;
502f40c67f0SKiyoshi Ueda 	struct block_device *bdev;
503eb8db831SChristoph Hellwig 	struct dm_mpath_io *mpio = get_mpio(map_context);
5047083abbbSBart Van Assche 	struct request_queue *q;
505eb8db831SChristoph Hellwig 	struct request *clone;
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	/* Do we need to select a new pgpath? */
508506458efSWill Deacon 	pgpath = READ_ONCE(m->current_pgpath);
5092da1610aSMike Snitzer 	if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
5102da1610aSMike Snitzer 		pgpath = choose_pgpath(m, nr_bytes);
5111da177e4SLinus Torvalds 
5129bf59a61SMike Snitzer 	if (!pgpath) {
513c1fd0abeSMike Snitzer 		if (must_push_back_rq(m))
514b88efd43SMike Snitzer 			return DM_MAPIO_DELAY_REQUEUE;
51518a482f5SChristoph Hellwig 		dm_report_EIO(m);	/* Failed */
516f98e0eb6SChristoph Hellwig 		return DM_MAPIO_KILL;
517518257b1SMike Snitzer 	} else if (test_bit(MPATHF_QUEUE_IO, &m->flags) ||
518518257b1SMike Snitzer 		   test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
519c1d7ecf7SBart Van Assche 		if (pg_init_all_paths(m))
520c1d7ecf7SBart Van Assche 			return DM_MAPIO_DELAY_REQUEUE;
52106eb061fSBart Van Assche 		return DM_MAPIO_REQUEUE;
5229bf59a61SMike Snitzer 	}
5236afbc01dSMike Snitzer 
524e8099177SHannes Reinecke 	mpio->pgpath = pgpath;
525e8099177SHannes Reinecke 	mpio->nr_bytes = nr_bytes;
5262eb6e1e3SKeith Busch 
5272eb6e1e3SKeith Busch 	bdev = pgpath->path.dev->bdev;
5287083abbbSBart Van Assche 	q = bdev_get_queue(bdev);
5297083abbbSBart Van Assche 	clone = blk_get_request(q, rq->cmd_flags | REQ_NOMERGE, GFP_ATOMIC);
5306599c84eSBart Van Assche 	if (IS_ERR(clone)) {
5316599c84eSBart Van Assche 		/* EBUSY, ENODEV or EWOULDBLOCK: requeue */
532*848b8aefSMike Snitzer 		if (blk_queue_dying(q)) {
5337083abbbSBart Van Assche 			atomic_inc(&m->pg_init_in_progress);
5347083abbbSBart Van Assche 			activate_or_offline_path(pgpath);
5357083abbbSBart Van Assche 		}
53606eb061fSBart Van Assche 		return DM_MAPIO_DELAY_REQUEUE;
5374c6dd53dSMike Snitzer 	}
5386599c84eSBart Van Assche 	clone->bio = clone->biotail = NULL;
5396599c84eSBart Van Assche 	clone->rq_disk = bdev->bd_disk;
5406599c84eSBart Van Assche 	clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
5416599c84eSBart Van Assche 	*__clone = clone;
5422eb6e1e3SKeith Busch 
543e8099177SHannes Reinecke 	if (pgpath->pg->ps.type->start_io)
544e8099177SHannes Reinecke 		pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
545e8099177SHannes Reinecke 					      &pgpath->path,
546e8099177SHannes Reinecke 					      nr_bytes);
5472eb6e1e3SKeith Busch 	return DM_MAPIO_REMAPPED;
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
550e5863d9aSMike Snitzer static void multipath_release_clone(struct request *clone)
551e5863d9aSMike Snitzer {
552eb8db831SChristoph Hellwig 	blk_put_request(clone);
553e5863d9aSMike Snitzer }
554e5863d9aSMike Snitzer 
5551da177e4SLinus Torvalds /*
55676e33fe4SMike Snitzer  * Map cloned bios (bio-based multipath)
55776e33fe4SMike Snitzer  */
55876e33fe4SMike Snitzer static int __multipath_map_bio(struct multipath *m, struct bio *bio, struct dm_mpath_io *mpio)
55976e33fe4SMike Snitzer {
56076e33fe4SMike Snitzer 	struct pgpath *pgpath;
56176e33fe4SMike Snitzer 	unsigned long flags;
56276e33fe4SMike Snitzer 	bool queue_io;
56376e33fe4SMike Snitzer 
56476e33fe4SMike Snitzer 	/* Do we need to select a new pgpath? */
565506458efSWill Deacon 	pgpath = READ_ONCE(m->current_pgpath);
566*848b8aefSMike Snitzer 	/* MPATHF_QUEUE_IO will never be set for NVMe */
56776e33fe4SMike Snitzer 	queue_io = test_bit(MPATHF_QUEUE_IO, &m->flags);
56876e33fe4SMike Snitzer 	if (!pgpath || !queue_io)
569d0442f80SMike Snitzer 		pgpath = choose_pgpath(m, mpio->nr_bytes);
57076e33fe4SMike Snitzer 
571*848b8aefSMike Snitzer 	if ((!pgpath && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) ||
572*848b8aefSMike Snitzer 	    (pgpath && queue_io)) {
57376e33fe4SMike Snitzer 		/* Queue for the daemon to resubmit */
57476e33fe4SMike Snitzer 		spin_lock_irqsave(&m->lock, flags);
57576e33fe4SMike Snitzer 		bio_list_add(&m->queued_bios, bio);
57676e33fe4SMike Snitzer 		spin_unlock_irqrestore(&m->lock, flags);
577*848b8aefSMike Snitzer 
578*848b8aefSMike Snitzer 		if (m->queue_mode == DM_TYPE_NVME_BIO_BASED) {
579*848b8aefSMike Snitzer 			queue_work(kmultipathd, &m->process_queued_bios);
580*848b8aefSMike Snitzer 		} else {
58176e33fe4SMike Snitzer 			/* PG_INIT_REQUIRED cannot be set without QUEUE_IO */
58276e33fe4SMike Snitzer 			if (queue_io || test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
58376e33fe4SMike Snitzer 				pg_init_all_paths(m);
58476e33fe4SMike Snitzer 			else if (!queue_io)
58576e33fe4SMike Snitzer 				queue_work(kmultipathd, &m->process_queued_bios);
586*848b8aefSMike Snitzer 		}
587*848b8aefSMike Snitzer 
58876e33fe4SMike Snitzer 		return DM_MAPIO_SUBMITTED;
58976e33fe4SMike Snitzer 	}
59076e33fe4SMike Snitzer 
59176e33fe4SMike Snitzer 	if (!pgpath) {
592c1fd0abeSMike Snitzer 		if (must_push_back_bio(m))
59376e33fe4SMike Snitzer 			return DM_MAPIO_REQUEUE;
59418a482f5SChristoph Hellwig 		dm_report_EIO(m);
595846785e6SChristoph Hellwig 		return DM_MAPIO_KILL;
59676e33fe4SMike Snitzer 	}
59776e33fe4SMike Snitzer 
59876e33fe4SMike Snitzer 	mpio->pgpath = pgpath;
59976e33fe4SMike Snitzer 
6004e4cbee9SChristoph Hellwig 	bio->bi_status = 0;
60174d46992SChristoph Hellwig 	bio_set_dev(bio, pgpath->path.dev->bdev);
6021eff9d32SJens Axboe 	bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
60376e33fe4SMike Snitzer 
60476e33fe4SMike Snitzer 	if (pgpath->pg->ps.type->start_io)
60576e33fe4SMike Snitzer 		pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
60676e33fe4SMike Snitzer 					      &pgpath->path,
607d0442f80SMike Snitzer 					      mpio->nr_bytes);
60876e33fe4SMike Snitzer 	return DM_MAPIO_REMAPPED;
60976e33fe4SMike Snitzer }
61076e33fe4SMike Snitzer 
61176e33fe4SMike Snitzer static int multipath_map_bio(struct dm_target *ti, struct bio *bio)
61276e33fe4SMike Snitzer {
61376e33fe4SMike Snitzer 	struct multipath *m = ti->private;
614bf661be1SMike Snitzer 	struct dm_mpath_io *mpio = NULL;
615bf661be1SMike Snitzer 
61663f6e6fdSMike Snitzer 	multipath_init_per_bio_data(bio, &mpio);
61776e33fe4SMike Snitzer 	return __multipath_map_bio(m, bio, mpio);
61876e33fe4SMike Snitzer }
61976e33fe4SMike Snitzer 
6207e48c768SMike Snitzer static void process_queued_io_list(struct multipath *m)
62176e33fe4SMike Snitzer {
6227e48c768SMike Snitzer 	if (m->queue_mode == DM_TYPE_MQ_REQUEST_BASED)
6237e48c768SMike Snitzer 		dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table));
624cd025384SMike Snitzer 	else if (m->queue_mode == DM_TYPE_BIO_BASED ||
625cd025384SMike Snitzer 		 m->queue_mode == DM_TYPE_NVME_BIO_BASED)
62676e33fe4SMike Snitzer 		queue_work(kmultipathd, &m->process_queued_bios);
62776e33fe4SMike Snitzer }
62876e33fe4SMike Snitzer 
62976e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work)
63076e33fe4SMike Snitzer {
63176e33fe4SMike Snitzer 	int r;
63276e33fe4SMike Snitzer 	unsigned long flags;
63376e33fe4SMike Snitzer 	struct bio *bio;
63476e33fe4SMike Snitzer 	struct bio_list bios;
63576e33fe4SMike Snitzer 	struct blk_plug plug;
63676e33fe4SMike Snitzer 	struct multipath *m =
63776e33fe4SMike Snitzer 		container_of(work, struct multipath, process_queued_bios);
63876e33fe4SMike Snitzer 
63976e33fe4SMike Snitzer 	bio_list_init(&bios);
64076e33fe4SMike Snitzer 
64176e33fe4SMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
64276e33fe4SMike Snitzer 
64376e33fe4SMike Snitzer 	if (bio_list_empty(&m->queued_bios)) {
64476e33fe4SMike Snitzer 		spin_unlock_irqrestore(&m->lock, flags);
64576e33fe4SMike Snitzer 		return;
64676e33fe4SMike Snitzer 	}
64776e33fe4SMike Snitzer 
64876e33fe4SMike Snitzer 	bio_list_merge(&bios, &m->queued_bios);
64976e33fe4SMike Snitzer 	bio_list_init(&m->queued_bios);
65076e33fe4SMike Snitzer 
65176e33fe4SMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
65276e33fe4SMike Snitzer 
65376e33fe4SMike Snitzer 	blk_start_plug(&plug);
65476e33fe4SMike Snitzer 	while ((bio = bio_list_pop(&bios))) {
6551836df08SMike Snitzer 		struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
6561836df08SMike Snitzer 		dm_bio_restore(get_bio_details_from_mpio(mpio), bio);
6571836df08SMike Snitzer 		r = __multipath_map_bio(m, bio, mpio);
658846785e6SChristoph Hellwig 		switch (r) {
659846785e6SChristoph Hellwig 		case DM_MAPIO_KILL:
6604e4cbee9SChristoph Hellwig 			bio->bi_status = BLK_STS_IOERR;
6614e4cbee9SChristoph Hellwig 			bio_endio(bio);
662047385b3SDan Carpenter 			break;
663846785e6SChristoph Hellwig 		case DM_MAPIO_REQUEUE:
6644e4cbee9SChristoph Hellwig 			bio->bi_status = BLK_STS_DM_REQUEUE;
66576e33fe4SMike Snitzer 			bio_endio(bio);
666846785e6SChristoph Hellwig 			break;
667846785e6SChristoph Hellwig 		case DM_MAPIO_REMAPPED:
66876e33fe4SMike Snitzer 			generic_make_request(bio);
669846785e6SChristoph Hellwig 			break;
6709157c8d3SBart Van Assche 		case 0:
6719157c8d3SBart Van Assche 			break;
6729157c8d3SBart Van Assche 		default:
6739157c8d3SBart Van Assche 			WARN_ONCE(true, "__multipath_map_bio() returned %d\n", r);
674846785e6SChristoph Hellwig 		}
67576e33fe4SMike Snitzer 	}
67676e33fe4SMike Snitzer 	blk_finish_plug(&plug);
67776e33fe4SMike Snitzer }
67876e33fe4SMike Snitzer 
67976e33fe4SMike Snitzer /*
6801da177e4SLinus Torvalds  * If we run out of usable paths, should we queue I/O or error it?
6811da177e4SLinus Torvalds  */
682be7d31ccSMike Snitzer static int queue_if_no_path(struct multipath *m, bool queue_if_no_path,
683be7d31ccSMike Snitzer 			    bool save_old_value)
6841da177e4SLinus Torvalds {
6851da177e4SLinus Torvalds 	unsigned long flags;
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
6885307e2adSLukas Wunner 	assign_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags,
6895307e2adSLukas Wunner 		   (save_old_value && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) ||
6905307e2adSLukas Wunner 		   (!save_old_value && queue_if_no_path));
691c1fd0abeSMike Snitzer 	assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags, queue_if_no_path);
6921da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
6931da177e4SLinus Torvalds 
69476e33fe4SMike Snitzer 	if (!queue_if_no_path) {
69563d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
6967e48c768SMike Snitzer 		process_queued_io_list(m);
69776e33fe4SMike Snitzer 	}
69863d832c3SHannes Reinecke 
6991da177e4SLinus Torvalds 	return 0;
7001da177e4SLinus Torvalds }
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds /*
7031da177e4SLinus Torvalds  * An event is triggered whenever a path is taken out of use.
7041da177e4SLinus Torvalds  * Includes path failure and PG bypass.
7051da177e4SLinus Torvalds  */
706c4028958SDavid Howells static void trigger_event(struct work_struct *work)
7071da177e4SLinus Torvalds {
708c4028958SDavid Howells 	struct multipath *m =
709c4028958SDavid Howells 		container_of(work, struct multipath, trigger_event);
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	dm_table_event(m->ti->table);
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds /*-----------------------------------------------------------------
7151da177e4SLinus Torvalds  * Constructor/argument parsing:
7161da177e4SLinus Torvalds  * <#multipath feature args> [<arg>]*
7171da177e4SLinus Torvalds  * <#hw_handler args> [hw_handler [<arg>]*]
7181da177e4SLinus Torvalds  * <#priority groups>
7191da177e4SLinus Torvalds  * <initial priority group>
7201da177e4SLinus Torvalds  *     [<selector> <#selector args> [<arg>]*
7211da177e4SLinus Torvalds  *      <#paths> <#per-path selector args>
7221da177e4SLinus Torvalds  *         [<path> [<arg>]* ]+ ]+
7231da177e4SLinus Torvalds  *---------------------------------------------------------------*/
724498f0103SMike Snitzer static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
7251da177e4SLinus Torvalds 			       struct dm_target *ti)
7261da177e4SLinus Torvalds {
7271da177e4SLinus Torvalds 	int r;
7281da177e4SLinus Torvalds 	struct path_selector_type *pst;
7291da177e4SLinus Torvalds 	unsigned ps_argc;
7301da177e4SLinus Torvalds 
7315916a22bSEric Biggers 	static const struct dm_arg _args[] = {
73272d94861SAlasdair G Kergon 		{0, 1024, "invalid number of path selector args"},
7331da177e4SLinus Torvalds 	};
7341da177e4SLinus Torvalds 
735498f0103SMike Snitzer 	pst = dm_get_path_selector(dm_shift_arg(as));
7361da177e4SLinus Torvalds 	if (!pst) {
73772d94861SAlasdair G Kergon 		ti->error = "unknown path selector type";
7381da177e4SLinus Torvalds 		return -EINVAL;
7391da177e4SLinus Torvalds 	}
7401da177e4SLinus Torvalds 
741498f0103SMike Snitzer 	r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
742371b2e34SMikulas Patocka 	if (r) {
743371b2e34SMikulas Patocka 		dm_put_path_selector(pst);
7441da177e4SLinus Torvalds 		return -EINVAL;
745371b2e34SMikulas Patocka 	}
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	r = pst->create(&pg->ps, ps_argc, as->argv);
7481da177e4SLinus Torvalds 	if (r) {
7491da177e4SLinus Torvalds 		dm_put_path_selector(pst);
75072d94861SAlasdair G Kergon 		ti->error = "path selector constructor failed";
7511da177e4SLinus Torvalds 		return r;
7521da177e4SLinus Torvalds 	}
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	pg->ps.type = pst;
755498f0103SMike Snitzer 	dm_consume_args(as, ps_argc);
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	return 0;
7581da177e4SLinus Torvalds }
7591da177e4SLinus Torvalds 
760*848b8aefSMike Snitzer static int setup_scsi_dh(struct block_device *bdev, struct multipath *m, char **error)
7611da177e4SLinus Torvalds {
762*848b8aefSMike Snitzer 	struct request_queue *q = bdev_get_queue(bdev);
763a58a935dSMike Snitzer 	const char *attached_handler_name;
764*848b8aefSMike Snitzer 	int r;
765a0cf7ea9SHannes Reinecke 
766518257b1SMike Snitzer 	if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) {
7671bab0de0SChristoph Hellwig retain:
768a58a935dSMike Snitzer 		attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
769a58a935dSMike Snitzer 		if (attached_handler_name) {
770a58a935dSMike Snitzer 			/*
77154cd640dStang.junhui 			 * Clear any hw_handler_params associated with a
77254cd640dStang.junhui 			 * handler that isn't already attached.
77354cd640dStang.junhui 			 */
77454cd640dStang.junhui 			if (m->hw_handler_name && strcmp(attached_handler_name, m->hw_handler_name)) {
77554cd640dStang.junhui 				kfree(m->hw_handler_params);
77654cd640dStang.junhui 				m->hw_handler_params = NULL;
77754cd640dStang.junhui 			}
77854cd640dStang.junhui 
77954cd640dStang.junhui 			/*
780a58a935dSMike Snitzer 			 * Reset hw_handler_name to match the attached handler
781a58a935dSMike Snitzer 			 *
782a58a935dSMike Snitzer 			 * NB. This modifies the table line to show the actual
783a58a935dSMike Snitzer 			 * handler instead of the original table passed in.
784a58a935dSMike Snitzer 			 */
785a58a935dSMike Snitzer 			kfree(m->hw_handler_name);
786a58a935dSMike Snitzer 			m->hw_handler_name = attached_handler_name;
787a58a935dSMike Snitzer 		}
788a58a935dSMike Snitzer 	}
789a58a935dSMike Snitzer 
790a58a935dSMike Snitzer 	if (m->hw_handler_name) {
791a0cf7ea9SHannes Reinecke 		r = scsi_dh_attach(q, m->hw_handler_name);
792a0cf7ea9SHannes Reinecke 		if (r == -EBUSY) {
7931bab0de0SChristoph Hellwig 			char b[BDEVNAME_SIZE];
794a0cf7ea9SHannes Reinecke 
7951bab0de0SChristoph Hellwig 			printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
796*848b8aefSMike Snitzer 			       bdevname(bdev, b));
7971bab0de0SChristoph Hellwig 			goto retain;
7981bab0de0SChristoph Hellwig 		}
799ae11b1b3SHannes Reinecke 		if (r < 0) {
800*848b8aefSMike Snitzer 			*error = "error attaching hardware handler";
801*848b8aefSMike Snitzer 			return r;
802ae11b1b3SHannes Reinecke 		}
8032bfd2e13SChandra Seetharaman 
8042bfd2e13SChandra Seetharaman 		if (m->hw_handler_params) {
8052bfd2e13SChandra Seetharaman 			r = scsi_dh_set_params(q, m->hw_handler_params);
8062bfd2e13SChandra Seetharaman 			if (r < 0) {
807*848b8aefSMike Snitzer 				*error = "unable to set hardware handler parameters";
808*848b8aefSMike Snitzer 				return r;
809*848b8aefSMike Snitzer 			}
810*848b8aefSMike Snitzer 		}
811*848b8aefSMike Snitzer 	}
812*848b8aefSMike Snitzer 
813*848b8aefSMike Snitzer 	return 0;
814*848b8aefSMike Snitzer }
815*848b8aefSMike Snitzer 
816*848b8aefSMike Snitzer static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
817*848b8aefSMike Snitzer 				 struct dm_target *ti)
818*848b8aefSMike Snitzer {
819*848b8aefSMike Snitzer 	int r;
820*848b8aefSMike Snitzer 	struct pgpath *p;
821*848b8aefSMike Snitzer 	struct multipath *m = ti->private;
822*848b8aefSMike Snitzer 
823*848b8aefSMike Snitzer 	/* we need at least a path arg */
824*848b8aefSMike Snitzer 	if (as->argc < 1) {
825*848b8aefSMike Snitzer 		ti->error = "no device given";
826*848b8aefSMike Snitzer 		return ERR_PTR(-EINVAL);
827*848b8aefSMike Snitzer 	}
828*848b8aefSMike Snitzer 
829*848b8aefSMike Snitzer 	p = alloc_pgpath();
830*848b8aefSMike Snitzer 	if (!p)
831*848b8aefSMike Snitzer 		return ERR_PTR(-ENOMEM);
832*848b8aefSMike Snitzer 
833*848b8aefSMike Snitzer 	r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
834*848b8aefSMike Snitzer 			  &p->path.dev);
835*848b8aefSMike Snitzer 	if (r) {
836*848b8aefSMike Snitzer 		ti->error = "error getting device";
8372bfd2e13SChandra Seetharaman 		goto bad;
8382bfd2e13SChandra Seetharaman 	}
839*848b8aefSMike Snitzer 
840*848b8aefSMike Snitzer 	if (m->queue_mode != DM_TYPE_NVME_BIO_BASED) {
841*848b8aefSMike Snitzer 		INIT_DELAYED_WORK(&p->activate_path, activate_path_work);
842*848b8aefSMike Snitzer 		r = setup_scsi_dh(p->path.dev->bdev, m, &ti->error);
843*848b8aefSMike Snitzer 		if (r) {
844*848b8aefSMike Snitzer 			dm_put_device(ti, p->path.dev);
845*848b8aefSMike Snitzer 			goto bad;
8462bfd2e13SChandra Seetharaman 		}
847ae11b1b3SHannes Reinecke 	}
848ae11b1b3SHannes Reinecke 
8491da177e4SLinus Torvalds 	r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
8501da177e4SLinus Torvalds 	if (r) {
8511da177e4SLinus Torvalds 		dm_put_device(ti, p->path.dev);
8521da177e4SLinus Torvalds 		goto bad;
8531da177e4SLinus Torvalds 	}
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	return p;
8561da177e4SLinus Torvalds  bad:
8571da177e4SLinus Torvalds 	free_pgpath(p);
85801460f35SBenjamin Marzinski 	return ERR_PTR(r);
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds 
861498f0103SMike Snitzer static struct priority_group *parse_priority_group(struct dm_arg_set *as,
86228f16c20SMicha³ Miros³aw 						   struct multipath *m)
8631da177e4SLinus Torvalds {
8645916a22bSEric Biggers 	static const struct dm_arg _args[] = {
86572d94861SAlasdair G Kergon 		{1, 1024, "invalid number of paths"},
86672d94861SAlasdair G Kergon 		{0, 1024, "invalid number of selector args"}
8671da177e4SLinus Torvalds 	};
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	int r;
870498f0103SMike Snitzer 	unsigned i, nr_selector_args, nr_args;
8711da177e4SLinus Torvalds 	struct priority_group *pg;
87228f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	if (as->argc < 2) {
8751da177e4SLinus Torvalds 		as->argc = 0;
87601460f35SBenjamin Marzinski 		ti->error = "not enough priority group arguments";
87701460f35SBenjamin Marzinski 		return ERR_PTR(-EINVAL);
8781da177e4SLinus Torvalds 	}
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	pg = alloc_priority_group();
8811da177e4SLinus Torvalds 	if (!pg) {
88272d94861SAlasdair G Kergon 		ti->error = "couldn't allocate priority group";
88301460f35SBenjamin Marzinski 		return ERR_PTR(-ENOMEM);
8841da177e4SLinus Torvalds 	}
8851da177e4SLinus Torvalds 	pg->m = m;
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	r = parse_path_selector(as, pg, ti);
8881da177e4SLinus Torvalds 	if (r)
8891da177e4SLinus Torvalds 		goto bad;
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	/*
8921da177e4SLinus Torvalds 	 * read the paths
8931da177e4SLinus Torvalds 	 */
894498f0103SMike Snitzer 	r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
8951da177e4SLinus Torvalds 	if (r)
8961da177e4SLinus Torvalds 		goto bad;
8971da177e4SLinus Torvalds 
898498f0103SMike Snitzer 	r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
8991da177e4SLinus Torvalds 	if (r)
9001da177e4SLinus Torvalds 		goto bad;
9011da177e4SLinus Torvalds 
902498f0103SMike Snitzer 	nr_args = 1 + nr_selector_args;
9031da177e4SLinus Torvalds 	for (i = 0; i < pg->nr_pgpaths; i++) {
9041da177e4SLinus Torvalds 		struct pgpath *pgpath;
905498f0103SMike Snitzer 		struct dm_arg_set path_args;
9061da177e4SLinus Torvalds 
907498f0103SMike Snitzer 		if (as->argc < nr_args) {
908148acff6SMikulas Patocka 			ti->error = "not enough path parameters";
9096bbf79a1SAlasdair G Kergon 			r = -EINVAL;
9101da177e4SLinus Torvalds 			goto bad;
911148acff6SMikulas Patocka 		}
9121da177e4SLinus Torvalds 
913498f0103SMike Snitzer 		path_args.argc = nr_args;
9141da177e4SLinus Torvalds 		path_args.argv = as->argv;
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 		pgpath = parse_path(&path_args, &pg->ps, ti);
91701460f35SBenjamin Marzinski 		if (IS_ERR(pgpath)) {
91801460f35SBenjamin Marzinski 			r = PTR_ERR(pgpath);
9191da177e4SLinus Torvalds 			goto bad;
92001460f35SBenjamin Marzinski 		}
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 		pgpath->pg = pg;
9231da177e4SLinus Torvalds 		list_add_tail(&pgpath->list, &pg->pgpaths);
924498f0103SMike Snitzer 		dm_consume_args(as, nr_args);
9251da177e4SLinus Torvalds 	}
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	return pg;
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds  bad:
9301da177e4SLinus Torvalds 	free_priority_group(pg, ti);
93101460f35SBenjamin Marzinski 	return ERR_PTR(r);
9321da177e4SLinus Torvalds }
9331da177e4SLinus Torvalds 
934498f0103SMike Snitzer static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
9351da177e4SLinus Torvalds {
9361da177e4SLinus Torvalds 	unsigned hw_argc;
9372bfd2e13SChandra Seetharaman 	int ret;
93828f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
9391da177e4SLinus Torvalds 
9405916a22bSEric Biggers 	static const struct dm_arg _args[] = {
94172d94861SAlasdair G Kergon 		{0, 1024, "invalid number of hardware handler args"},
9421da177e4SLinus Torvalds 	};
9431da177e4SLinus Torvalds 
944498f0103SMike Snitzer 	if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
9451da177e4SLinus Torvalds 		return -EINVAL;
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	if (!hw_argc)
9481da177e4SLinus Torvalds 		return 0;
9491da177e4SLinus Torvalds 
950cd025384SMike Snitzer 	if (m->queue_mode == DM_TYPE_BIO_BASED ||
951cd025384SMike Snitzer 	    m->queue_mode == DM_TYPE_NVME_BIO_BASED) {
95276e33fe4SMike Snitzer 		dm_consume_args(as, hw_argc);
95376e33fe4SMike Snitzer 		DMERR("bio-based multipath doesn't allow hardware handler args");
95476e33fe4SMike Snitzer 		return 0;
95576e33fe4SMike Snitzer 	}
95676e33fe4SMike Snitzer 
957498f0103SMike Snitzer 	m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
958f97dc421Stang.junhui 	if (!m->hw_handler_name)
959f97dc421Stang.junhui 		return -EINVAL;
96014e98c5cSChandra Seetharaman 
9612bfd2e13SChandra Seetharaman 	if (hw_argc > 1) {
9622bfd2e13SChandra Seetharaman 		char *p;
9632bfd2e13SChandra Seetharaman 		int i, j, len = 4;
9642bfd2e13SChandra Seetharaman 
9652bfd2e13SChandra Seetharaman 		for (i = 0; i <= hw_argc - 2; i++)
9662bfd2e13SChandra Seetharaman 			len += strlen(as->argv[i]) + 1;
9672bfd2e13SChandra Seetharaman 		p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
9682bfd2e13SChandra Seetharaman 		if (!p) {
9692bfd2e13SChandra Seetharaman 			ti->error = "memory allocation failed";
9702bfd2e13SChandra Seetharaman 			ret = -ENOMEM;
9712bfd2e13SChandra Seetharaman 			goto fail;
9722bfd2e13SChandra Seetharaman 		}
9732bfd2e13SChandra Seetharaman 		j = sprintf(p, "%d", hw_argc - 1);
9742bfd2e13SChandra Seetharaman 		for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
9752bfd2e13SChandra Seetharaman 			j = sprintf(p, "%s", as->argv[i]);
9762bfd2e13SChandra Seetharaman 	}
977498f0103SMike Snitzer 	dm_consume_args(as, hw_argc - 1);
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	return 0;
9802bfd2e13SChandra Seetharaman fail:
9812bfd2e13SChandra Seetharaman 	kfree(m->hw_handler_name);
9822bfd2e13SChandra Seetharaman 	m->hw_handler_name = NULL;
9832bfd2e13SChandra Seetharaman 	return ret;
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds 
986498f0103SMike Snitzer static int parse_features(struct dm_arg_set *as, struct multipath *m)
9871da177e4SLinus Torvalds {
9881da177e4SLinus Torvalds 	int r;
9891da177e4SLinus Torvalds 	unsigned argc;
99028f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
991498f0103SMike Snitzer 	const char *arg_name;
9921da177e4SLinus Torvalds 
9935916a22bSEric Biggers 	static const struct dm_arg _args[] = {
994e83068a5SMike Snitzer 		{0, 8, "invalid number of feature args"},
995c9e45581SDave Wysochanski 		{1, 50, "pg_init_retries must be between 1 and 50"},
9964e2d19e4SChandra Seetharaman 		{0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
9971da177e4SLinus Torvalds 	};
9981da177e4SLinus Torvalds 
999498f0103SMike Snitzer 	r = dm_read_arg_group(_args, as, &argc, &ti->error);
10001da177e4SLinus Torvalds 	if (r)
10011da177e4SLinus Torvalds 		return -EINVAL;
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 	if (!argc)
10041da177e4SLinus Torvalds 		return 0;
10051da177e4SLinus Torvalds 
1006c9e45581SDave Wysochanski 	do {
1007498f0103SMike Snitzer 		arg_name = dm_shift_arg(as);
1008c9e45581SDave Wysochanski 		argc--;
1009c9e45581SDave Wysochanski 
1010498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "queue_if_no_path")) {
1011be7d31ccSMike Snitzer 			r = queue_if_no_path(m, true, false);
1012c9e45581SDave Wysochanski 			continue;
10131da177e4SLinus Torvalds 		}
1014c9e45581SDave Wysochanski 
1015a58a935dSMike Snitzer 		if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
1016518257b1SMike Snitzer 			set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
1017a58a935dSMike Snitzer 			continue;
1018a58a935dSMike Snitzer 		}
1019a58a935dSMike Snitzer 
1020498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "pg_init_retries") &&
1021c9e45581SDave Wysochanski 		    (argc >= 1)) {
1022498f0103SMike Snitzer 			r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
1023c9e45581SDave Wysochanski 			argc--;
1024c9e45581SDave Wysochanski 			continue;
1025c9e45581SDave Wysochanski 		}
1026c9e45581SDave Wysochanski 
1027498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
10284e2d19e4SChandra Seetharaman 		    (argc >= 1)) {
1029498f0103SMike Snitzer 			r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
10304e2d19e4SChandra Seetharaman 			argc--;
10314e2d19e4SChandra Seetharaman 			continue;
10324e2d19e4SChandra Seetharaman 		}
10334e2d19e4SChandra Seetharaman 
1034e83068a5SMike Snitzer 		if (!strcasecmp(arg_name, "queue_mode") &&
1035e83068a5SMike Snitzer 		    (argc >= 1)) {
1036e83068a5SMike Snitzer 			const char *queue_mode_name = dm_shift_arg(as);
1037e83068a5SMike Snitzer 
1038e83068a5SMike Snitzer 			if (!strcasecmp(queue_mode_name, "bio"))
1039e83068a5SMike Snitzer 				m->queue_mode = DM_TYPE_BIO_BASED;
1040cd025384SMike Snitzer 			else if (!strcasecmp(queue_mode_name, "nvme"))
1041cd025384SMike Snitzer 				m->queue_mode = DM_TYPE_NVME_BIO_BASED;
1042e83068a5SMike Snitzer 			else if (!strcasecmp(queue_mode_name, "rq"))
1043e83068a5SMike Snitzer 				m->queue_mode = DM_TYPE_REQUEST_BASED;
1044e83068a5SMike Snitzer 			else if (!strcasecmp(queue_mode_name, "mq"))
1045e83068a5SMike Snitzer 				m->queue_mode = DM_TYPE_MQ_REQUEST_BASED;
1046e83068a5SMike Snitzer 			else {
1047e83068a5SMike Snitzer 				ti->error = "Unknown 'queue_mode' requested";
1048e83068a5SMike Snitzer 				r = -EINVAL;
1049e83068a5SMike Snitzer 			}
1050e83068a5SMike Snitzer 			argc--;
1051e83068a5SMike Snitzer 			continue;
1052e83068a5SMike Snitzer 		}
1053e83068a5SMike Snitzer 
1054c9e45581SDave Wysochanski 		ti->error = "Unrecognised multipath feature request";
1055c9e45581SDave Wysochanski 		r = -EINVAL;
1056c9e45581SDave Wysochanski 	} while (argc && !r);
1057c9e45581SDave Wysochanski 
1058c9e45581SDave Wysochanski 	return r;
10591da177e4SLinus Torvalds }
10601da177e4SLinus Torvalds 
1061e83068a5SMike Snitzer static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv)
10621da177e4SLinus Torvalds {
1063498f0103SMike Snitzer 	/* target arguments */
10645916a22bSEric Biggers 	static const struct dm_arg _args[] = {
1065a490a07aSMike Snitzer 		{0, 1024, "invalid number of priority groups"},
1066a490a07aSMike Snitzer 		{0, 1024, "invalid initial priority group number"},
10671da177e4SLinus Torvalds 	};
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 	int r;
10701da177e4SLinus Torvalds 	struct multipath *m;
1071498f0103SMike Snitzer 	struct dm_arg_set as;
10721da177e4SLinus Torvalds 	unsigned pg_count = 0;
10731da177e4SLinus Torvalds 	unsigned next_pg_num;
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	as.argc = argc;
10761da177e4SLinus Torvalds 	as.argv = argv;
10771da177e4SLinus Torvalds 
1078e83068a5SMike Snitzer 	m = alloc_multipath(ti);
10791da177e4SLinus Torvalds 	if (!m) {
108072d94861SAlasdair G Kergon 		ti->error = "can't allocate multipath";
10811da177e4SLinus Torvalds 		return -EINVAL;
10821da177e4SLinus Torvalds 	}
10831da177e4SLinus Torvalds 
108428f16c20SMicha³ Miros³aw 	r = parse_features(&as, m);
10851da177e4SLinus Torvalds 	if (r)
10861da177e4SLinus Torvalds 		goto bad;
10871da177e4SLinus Torvalds 
1088e83068a5SMike Snitzer 	r = alloc_multipath_stage2(ti, m);
1089e83068a5SMike Snitzer 	if (r)
1090e83068a5SMike Snitzer 		goto bad;
1091e83068a5SMike Snitzer 
109228f16c20SMicha³ Miros³aw 	r = parse_hw_handler(&as, m);
10931da177e4SLinus Torvalds 	if (r)
10941da177e4SLinus Torvalds 		goto bad;
10951da177e4SLinus Torvalds 
1096498f0103SMike Snitzer 	r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
10971da177e4SLinus Torvalds 	if (r)
10981da177e4SLinus Torvalds 		goto bad;
10991da177e4SLinus Torvalds 
1100498f0103SMike Snitzer 	r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
11011da177e4SLinus Torvalds 	if (r)
11021da177e4SLinus Torvalds 		goto bad;
11031da177e4SLinus Torvalds 
1104a490a07aSMike Snitzer 	if ((!m->nr_priority_groups && next_pg_num) ||
1105a490a07aSMike Snitzer 	    (m->nr_priority_groups && !next_pg_num)) {
1106a490a07aSMike Snitzer 		ti->error = "invalid initial priority group";
1107a490a07aSMike Snitzer 		r = -EINVAL;
1108a490a07aSMike Snitzer 		goto bad;
1109a490a07aSMike Snitzer 	}
1110a490a07aSMike Snitzer 
11111da177e4SLinus Torvalds 	/* parse the priority groups */
11121da177e4SLinus Torvalds 	while (as.argc) {
11131da177e4SLinus Torvalds 		struct priority_group *pg;
111491e968aaSMike Snitzer 		unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths);
11151da177e4SLinus Torvalds 
111628f16c20SMicha³ Miros³aw 		pg = parse_priority_group(&as, m);
111701460f35SBenjamin Marzinski 		if (IS_ERR(pg)) {
111801460f35SBenjamin Marzinski 			r = PTR_ERR(pg);
11191da177e4SLinus Torvalds 			goto bad;
11201da177e4SLinus Torvalds 		}
11211da177e4SLinus Torvalds 
112291e968aaSMike Snitzer 		nr_valid_paths += pg->nr_pgpaths;
112391e968aaSMike Snitzer 		atomic_set(&m->nr_valid_paths, nr_valid_paths);
112491e968aaSMike Snitzer 
11251da177e4SLinus Torvalds 		list_add_tail(&pg->list, &m->priority_groups);
11261da177e4SLinus Torvalds 		pg_count++;
11271da177e4SLinus Torvalds 		pg->pg_num = pg_count;
11281da177e4SLinus Torvalds 		if (!--next_pg_num)
11291da177e4SLinus Torvalds 			m->next_pg = pg;
11301da177e4SLinus Torvalds 	}
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 	if (pg_count != m->nr_priority_groups) {
113372d94861SAlasdair G Kergon 		ti->error = "priority group count mismatch";
11341da177e4SLinus Torvalds 		r = -EINVAL;
11351da177e4SLinus Torvalds 		goto bad;
11361da177e4SLinus Torvalds 	}
11371da177e4SLinus Torvalds 
113855a62eefSAlasdair G Kergon 	ti->num_flush_bios = 1;
113955a62eefSAlasdair G Kergon 	ti->num_discard_bios = 1;
1140042bcef8SMike Snitzer 	ti->num_write_same_bios = 1;
1141ac62d620SChristoph Hellwig 	ti->num_write_zeroes_bios = 1;
1142cd025384SMike Snitzer 	if (m->queue_mode == DM_TYPE_BIO_BASED || m->queue_mode == DM_TYPE_NVME_BIO_BASED)
1143bf661be1SMike Snitzer 		ti->per_io_data_size = multipath_per_bio_data_size();
1144eb8db831SChristoph Hellwig 	else
11458637a6bfSMike Snitzer 		ti->per_io_data_size = sizeof(struct dm_mpath_io);
11468627921fSMikulas Patocka 
11471da177e4SLinus Torvalds 	return 0;
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds  bad:
11501da177e4SLinus Torvalds 	free_multipath(m);
11511da177e4SLinus Torvalds 	return r;
11521da177e4SLinus Torvalds }
11531da177e4SLinus Torvalds 
11542bded7bdSKiyoshi Ueda static void multipath_wait_for_pg_init_completion(struct multipath *m)
11552bded7bdSKiyoshi Ueda {
11569f4c3f87SBart Van Assche 	DEFINE_WAIT(wait);
11572bded7bdSKiyoshi Ueda 
11582bded7bdSKiyoshi Ueda 	while (1) {
11599f4c3f87SBart Van Assche 		prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE);
11602bded7bdSKiyoshi Ueda 
116191e968aaSMike Snitzer 		if (!atomic_read(&m->pg_init_in_progress))
11622bded7bdSKiyoshi Ueda 			break;
11632bded7bdSKiyoshi Ueda 
11642bded7bdSKiyoshi Ueda 		io_schedule();
11652bded7bdSKiyoshi Ueda 	}
11669f4c3f87SBart Van Assche 	finish_wait(&m->pg_init_wait, &wait);
11672bded7bdSKiyoshi Ueda }
11682bded7bdSKiyoshi Ueda 
11692bded7bdSKiyoshi Ueda static void flush_multipath_work(struct multipath *m)
11701da177e4SLinus Torvalds {
1171*848b8aefSMike Snitzer 	if (m->hw_handler_name) {
1172518257b1SMike Snitzer 		set_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1173518257b1SMike Snitzer 		smp_mb__after_atomic();
1174954a73d5SShiva Krishna Merla 
1175bab7cfc7SChandra Seetharaman 		flush_workqueue(kmpath_handlerd);
11762bded7bdSKiyoshi Ueda 		multipath_wait_for_pg_init_completion(m);
1177954a73d5SShiva Krishna Merla 
1178518257b1SMike Snitzer 		clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1179518257b1SMike Snitzer 		smp_mb__after_atomic();
11806df400abSKiyoshi Ueda 	}
11816df400abSKiyoshi Ueda 
1182*848b8aefSMike Snitzer 	flush_workqueue(kmultipathd);
1183*848b8aefSMike Snitzer 	flush_work(&m->trigger_event);
1184*848b8aefSMike Snitzer }
1185*848b8aefSMike Snitzer 
11866df400abSKiyoshi Ueda static void multipath_dtr(struct dm_target *ti)
11876df400abSKiyoshi Ueda {
11886df400abSKiyoshi Ueda 	struct multipath *m = ti->private;
11896df400abSKiyoshi Ueda 
11902bded7bdSKiyoshi Ueda 	flush_multipath_work(m);
11911da177e4SLinus Torvalds 	free_multipath(m);
11921da177e4SLinus Torvalds }
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds /*
11951da177e4SLinus Torvalds  * Take a path out of use.
11961da177e4SLinus Torvalds  */
11971da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath)
11981da177e4SLinus Torvalds {
11991da177e4SLinus Torvalds 	unsigned long flags;
12001da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
12031da177e4SLinus Torvalds 
12046680073dSKiyoshi Ueda 	if (!pgpath->is_active)
12051da177e4SLinus Torvalds 		goto out;
12061da177e4SLinus Torvalds 
120772d94861SAlasdair G Kergon 	DMWARN("Failing path %s.", pgpath->path.dev->name);
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 	pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
1210be7d31ccSMike Snitzer 	pgpath->is_active = false;
12111da177e4SLinus Torvalds 	pgpath->fail_count++;
12121da177e4SLinus Torvalds 
121391e968aaSMike Snitzer 	atomic_dec(&m->nr_valid_paths);
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds 	if (pgpath == m->current_pgpath)
12161da177e4SLinus Torvalds 		m->current_pgpath = NULL;
12171da177e4SLinus Torvalds 
1218b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
121991e968aaSMike Snitzer 		       pgpath->path.dev->name, atomic_read(&m->nr_valid_paths));
1220b15546f9SMike Anderson 
1221fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds out:
12241da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 	return 0;
12271da177e4SLinus Torvalds }
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds /*
12301da177e4SLinus Torvalds  * Reinstate a previously-failed path
12311da177e4SLinus Torvalds  */
12321da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath)
12331da177e4SLinus Torvalds {
123463d832c3SHannes Reinecke 	int r = 0, run_queue = 0;
12351da177e4SLinus Torvalds 	unsigned long flags;
12361da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
123791e968aaSMike Snitzer 	unsigned nr_valid_paths;
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
12401da177e4SLinus Torvalds 
12416680073dSKiyoshi Ueda 	if (pgpath->is_active)
12421da177e4SLinus Torvalds 		goto out;
12431da177e4SLinus Torvalds 
1244ec31f3f7SMike Snitzer 	DMWARN("Reinstating path %s.", pgpath->path.dev->name);
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
12471da177e4SLinus Torvalds 	if (r)
12481da177e4SLinus Torvalds 		goto out;
12491da177e4SLinus Torvalds 
1250be7d31ccSMike Snitzer 	pgpath->is_active = true;
12511da177e4SLinus Torvalds 
125291e968aaSMike Snitzer 	nr_valid_paths = atomic_inc_return(&m->nr_valid_paths);
125391e968aaSMike Snitzer 	if (nr_valid_paths == 1) {
12541da177e4SLinus Torvalds 		m->current_pgpath = NULL;
125563d832c3SHannes Reinecke 		run_queue = 1;
1256e54f77ddSChandra Seetharaman 	} else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
12574e2d19e4SChandra Seetharaman 		if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
125891e968aaSMike Snitzer 			atomic_inc(&m->pg_init_in_progress);
1259e54f77ddSChandra Seetharaman 	}
12601da177e4SLinus Torvalds 
1261b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
126291e968aaSMike Snitzer 		       pgpath->path.dev->name, nr_valid_paths);
1263b15546f9SMike Anderson 
1264fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds out:
12671da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
126876e33fe4SMike Snitzer 	if (run_queue) {
126963d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
12707e48c768SMike Snitzer 		process_queued_io_list(m);
127176e33fe4SMike Snitzer 	}
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds 	return r;
12741da177e4SLinus Torvalds }
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds /*
12771da177e4SLinus Torvalds  * Fail or reinstate all paths that match the provided struct dm_dev.
12781da177e4SLinus Torvalds  */
12791da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev,
12801da177e4SLinus Torvalds 		      action_fn action)
12811da177e4SLinus Torvalds {
128219040c0bSMike Snitzer 	int r = -EINVAL;
12831da177e4SLinus Torvalds 	struct pgpath *pgpath;
12841da177e4SLinus Torvalds 	struct priority_group *pg;
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
12871da177e4SLinus Torvalds 		list_for_each_entry(pgpath, &pg->pgpaths, list) {
12881da177e4SLinus Torvalds 			if (pgpath->path.dev == dev)
12891da177e4SLinus Torvalds 				r = action(pgpath);
12901da177e4SLinus Torvalds 		}
12911da177e4SLinus Torvalds 	}
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 	return r;
12941da177e4SLinus Torvalds }
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds /*
12971da177e4SLinus Torvalds  * Temporarily try to avoid having to use the specified PG
12981da177e4SLinus Torvalds  */
12991da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg,
1300be7d31ccSMike Snitzer 		      bool bypassed)
13011da177e4SLinus Torvalds {
13021da177e4SLinus Torvalds 	unsigned long flags;
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 	pg->bypassed = bypassed;
13071da177e4SLinus Torvalds 	m->current_pgpath = NULL;
13081da177e4SLinus Torvalds 	m->current_pg = NULL;
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
13111da177e4SLinus Torvalds 
1312fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
13131da177e4SLinus Torvalds }
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds /*
13161da177e4SLinus Torvalds  * Switch to using the specified PG from the next I/O that gets mapped
13171da177e4SLinus Torvalds  */
13181da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr)
13191da177e4SLinus Torvalds {
13201da177e4SLinus Torvalds 	struct priority_group *pg;
13211da177e4SLinus Torvalds 	unsigned pgnum;
13221da177e4SLinus Torvalds 	unsigned long flags;
132331998ef1SMikulas Patocka 	char dummy;
13241da177e4SLinus Torvalds 
132531998ef1SMikulas Patocka 	if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1326cc5bd925Stang.junhui 	    !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
13271da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to switch_pg_num");
13281da177e4SLinus Torvalds 		return -EINVAL;
13291da177e4SLinus Torvalds 	}
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
13321da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
1333be7d31ccSMike Snitzer 		pg->bypassed = false;
13341da177e4SLinus Torvalds 		if (--pgnum)
13351da177e4SLinus Torvalds 			continue;
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds 		m->current_pgpath = NULL;
13381da177e4SLinus Torvalds 		m->current_pg = NULL;
13391da177e4SLinus Torvalds 		m->next_pg = pg;
13401da177e4SLinus Torvalds 	}
13411da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
13421da177e4SLinus Torvalds 
1343fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
13441da177e4SLinus Torvalds 	return 0;
13451da177e4SLinus Torvalds }
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds /*
13481da177e4SLinus Torvalds  * Set/clear bypassed status of a PG.
13491da177e4SLinus Torvalds  * PGs are numbered upwards from 1 in the order they were declared.
13501da177e4SLinus Torvalds  */
1351be7d31ccSMike Snitzer static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed)
13521da177e4SLinus Torvalds {
13531da177e4SLinus Torvalds 	struct priority_group *pg;
13541da177e4SLinus Torvalds 	unsigned pgnum;
135531998ef1SMikulas Patocka 	char dummy;
13561da177e4SLinus Torvalds 
135731998ef1SMikulas Patocka 	if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1358cc5bd925Stang.junhui 	    !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
13591da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to bypass_pg");
13601da177e4SLinus Torvalds 		return -EINVAL;
13611da177e4SLinus Torvalds 	}
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
13641da177e4SLinus Torvalds 		if (!--pgnum)
13651da177e4SLinus Torvalds 			break;
13661da177e4SLinus Torvalds 	}
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	bypass_pg(m, pg, bypassed);
13691da177e4SLinus Torvalds 	return 0;
13701da177e4SLinus Torvalds }
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds /*
1373c9e45581SDave Wysochanski  * Should we retry pg_init immediately?
1374c9e45581SDave Wysochanski  */
1375be7d31ccSMike Snitzer static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1376c9e45581SDave Wysochanski {
1377c9e45581SDave Wysochanski 	unsigned long flags;
1378be7d31ccSMike Snitzer 	bool limit_reached = false;
1379c9e45581SDave Wysochanski 
1380c9e45581SDave Wysochanski 	spin_lock_irqsave(&m->lock, flags);
1381c9e45581SDave Wysochanski 
138291e968aaSMike Snitzer 	if (atomic_read(&m->pg_init_count) <= m->pg_init_retries &&
138391e968aaSMike Snitzer 	    !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
1384518257b1SMike Snitzer 		set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
1385c9e45581SDave Wysochanski 	else
1386be7d31ccSMike Snitzer 		limit_reached = true;
1387c9e45581SDave Wysochanski 
1388c9e45581SDave Wysochanski 	spin_unlock_irqrestore(&m->lock, flags);
1389c9e45581SDave Wysochanski 
1390c9e45581SDave Wysochanski 	return limit_reached;
1391c9e45581SDave Wysochanski }
1392c9e45581SDave Wysochanski 
13933ae31f6aSChandra Seetharaman static void pg_init_done(void *data, int errors)
1394cfae5c9bSChandra Seetharaman {
139583c0d5d5SMoger, Babu 	struct pgpath *pgpath = data;
1396cfae5c9bSChandra Seetharaman 	struct priority_group *pg = pgpath->pg;
1397cfae5c9bSChandra Seetharaman 	struct multipath *m = pg->m;
1398cfae5c9bSChandra Seetharaman 	unsigned long flags;
1399be7d31ccSMike Snitzer 	bool delay_retry = false;
1400cfae5c9bSChandra Seetharaman 
1401cfae5c9bSChandra Seetharaman 	/* device or driver problems */
1402cfae5c9bSChandra Seetharaman 	switch (errors) {
1403cfae5c9bSChandra Seetharaman 	case SCSI_DH_OK:
1404cfae5c9bSChandra Seetharaman 		break;
1405cfae5c9bSChandra Seetharaman 	case SCSI_DH_NOSYS:
1406cfae5c9bSChandra Seetharaman 		if (!m->hw_handler_name) {
1407cfae5c9bSChandra Seetharaman 			errors = 0;
1408cfae5c9bSChandra Seetharaman 			break;
1409cfae5c9bSChandra Seetharaman 		}
1410f7b934c8SMoger, Babu 		DMERR("Could not failover the device: Handler scsi_dh_%s "
1411f7b934c8SMoger, Babu 		      "Error %d.", m->hw_handler_name, errors);
1412cfae5c9bSChandra Seetharaman 		/*
1413cfae5c9bSChandra Seetharaman 		 * Fail path for now, so we do not ping pong
1414cfae5c9bSChandra Seetharaman 		 */
1415cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1416cfae5c9bSChandra Seetharaman 		break;
1417cfae5c9bSChandra Seetharaman 	case SCSI_DH_DEV_TEMP_BUSY:
1418cfae5c9bSChandra Seetharaman 		/*
1419cfae5c9bSChandra Seetharaman 		 * Probably doing something like FW upgrade on the
1420cfae5c9bSChandra Seetharaman 		 * controller so try the other pg.
1421cfae5c9bSChandra Seetharaman 		 */
1422be7d31ccSMike Snitzer 		bypass_pg(m, pg, true);
1423cfae5c9bSChandra Seetharaman 		break;
1424cfae5c9bSChandra Seetharaman 	case SCSI_DH_RETRY:
14254e2d19e4SChandra Seetharaman 		/* Wait before retrying. */
14264e2d19e4SChandra Seetharaman 		delay_retry = 1;
14277b06e09aSBart Van Assche 		/* fall through */
1428cfae5c9bSChandra Seetharaman 	case SCSI_DH_IMM_RETRY:
1429cfae5c9bSChandra Seetharaman 	case SCSI_DH_RES_TEMP_UNAVAIL:
1430cfae5c9bSChandra Seetharaman 		if (pg_init_limit_reached(m, pgpath))
1431cfae5c9bSChandra Seetharaman 			fail_path(pgpath);
1432cfae5c9bSChandra Seetharaman 		errors = 0;
1433cfae5c9bSChandra Seetharaman 		break;
1434ec31f3f7SMike Snitzer 	case SCSI_DH_DEV_OFFLINED:
1435cfae5c9bSChandra Seetharaman 	default:
1436cfae5c9bSChandra Seetharaman 		/*
1437cfae5c9bSChandra Seetharaman 		 * We probably do not want to fail the path for a device
1438cfae5c9bSChandra Seetharaman 		 * error, but this is what the old dm did. In future
1439cfae5c9bSChandra Seetharaman 		 * patches we can do more advanced handling.
1440cfae5c9bSChandra Seetharaman 		 */
1441cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1442cfae5c9bSChandra Seetharaman 	}
1443cfae5c9bSChandra Seetharaman 
1444cfae5c9bSChandra Seetharaman 	spin_lock_irqsave(&m->lock, flags);
1445cfae5c9bSChandra Seetharaman 	if (errors) {
1446e54f77ddSChandra Seetharaman 		if (pgpath == m->current_pgpath) {
1447cfae5c9bSChandra Seetharaman 			DMERR("Could not failover device. Error %d.", errors);
1448cfae5c9bSChandra Seetharaman 			m->current_pgpath = NULL;
1449cfae5c9bSChandra Seetharaman 			m->current_pg = NULL;
1450e54f77ddSChandra Seetharaman 		}
1451518257b1SMike Snitzer 	} else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
1452be7d31ccSMike Snitzer 		pg->bypassed = false;
1453cfae5c9bSChandra Seetharaman 
145491e968aaSMike Snitzer 	if (atomic_dec_return(&m->pg_init_in_progress) > 0)
1455d0259bf0SKiyoshi Ueda 		/* Activations of other paths are still on going */
1456d0259bf0SKiyoshi Ueda 		goto out;
1457d0259bf0SKiyoshi Ueda 
1458518257b1SMike Snitzer 	if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
1459518257b1SMike Snitzer 		if (delay_retry)
1460518257b1SMike Snitzer 			set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1461518257b1SMike Snitzer 		else
1462518257b1SMike Snitzer 			clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1463518257b1SMike Snitzer 
14643e9f1be1SHannes Reinecke 		if (__pg_init_all_paths(m))
14653e9f1be1SHannes Reinecke 			goto out;
14663e9f1be1SHannes Reinecke 	}
1467518257b1SMike Snitzer 	clear_bit(MPATHF_QUEUE_IO, &m->flags);
1468d0259bf0SKiyoshi Ueda 
14697e48c768SMike Snitzer 	process_queued_io_list(m);
147076e33fe4SMike Snitzer 
14712bded7bdSKiyoshi Ueda 	/*
14722bded7bdSKiyoshi Ueda 	 * Wake up any thread waiting to suspend.
14732bded7bdSKiyoshi Ueda 	 */
14742bded7bdSKiyoshi Ueda 	wake_up(&m->pg_init_wait);
14752bded7bdSKiyoshi Ueda 
1476d0259bf0SKiyoshi Ueda out:
1477cfae5c9bSChandra Seetharaman 	spin_unlock_irqrestore(&m->lock, flags);
1478cfae5c9bSChandra Seetharaman }
1479cfae5c9bSChandra Seetharaman 
148089bfce76SBart Van Assche static void activate_or_offline_path(struct pgpath *pgpath)
1481bab7cfc7SChandra Seetharaman {
1482f10e06b7SMike Snitzer 	struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1483bab7cfc7SChandra Seetharaman 
1484f10e06b7SMike Snitzer 	if (pgpath->is_active && !blk_queue_dying(q))
1485f10e06b7SMike Snitzer 		scsi_dh_activate(q, pg_init_done, pgpath);
14863a017509SHannes Reinecke 	else
14873a017509SHannes Reinecke 		pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
1488bab7cfc7SChandra Seetharaman }
1489bab7cfc7SChandra Seetharaman 
149089bfce76SBart Van Assche static void activate_path_work(struct work_struct *work)
149189bfce76SBart Van Assche {
149289bfce76SBart Van Assche 	struct pgpath *pgpath =
149389bfce76SBart Van Assche 		container_of(work, struct pgpath, activate_path.work);
149489bfce76SBart Van Assche 
149589bfce76SBart Van Assche 	activate_or_offline_path(pgpath);
149689bfce76SBart Van Assche }
149789bfce76SBart Van Assche 
14982a842acaSChristoph Hellwig static int noretry_error(blk_status_t error)
14997e782af5SHannes Reinecke {
15007e782af5SHannes Reinecke 	switch (error) {
15012a842acaSChristoph Hellwig 	case BLK_STS_NOTSUPP:
15022a842acaSChristoph Hellwig 	case BLK_STS_NOSPC:
15032a842acaSChristoph Hellwig 	case BLK_STS_TARGET:
15042a842acaSChristoph Hellwig 	case BLK_STS_NEXUS:
15052a842acaSChristoph Hellwig 	case BLK_STS_MEDIUM:
15067e782af5SHannes Reinecke 		return 1;
15077e782af5SHannes Reinecke 	}
15087e782af5SHannes Reinecke 
15097e782af5SHannes Reinecke 	/* Anything else could be a path failure, so should be retried */
15107e782af5SHannes Reinecke 	return 0;
15117e782af5SHannes Reinecke }
15127e782af5SHannes Reinecke 
1513b79f10eeSChristoph Hellwig static int multipath_end_io(struct dm_target *ti, struct request *clone,
15142a842acaSChristoph Hellwig 			    blk_status_t error, union map_info *map_context)
15151da177e4SLinus Torvalds {
1516b79f10eeSChristoph Hellwig 	struct dm_mpath_io *mpio = get_mpio(map_context);
1517b79f10eeSChristoph Hellwig 	struct pgpath *pgpath = mpio->pgpath;
15187ed8578aSChristoph Hellwig 	int r = DM_ENDIO_DONE;
1519b79f10eeSChristoph Hellwig 
1520f40c67f0SKiyoshi Ueda 	/*
1521f40c67f0SKiyoshi Ueda 	 * We don't queue any clone request inside the multipath target
1522f40c67f0SKiyoshi Ueda 	 * during end I/O handling, since those clone requests don't have
1523f40c67f0SKiyoshi Ueda 	 * bio clones.  If we queue them inside the multipath target,
1524f40c67f0SKiyoshi Ueda 	 * we need to make bio clones, that requires memory allocation.
15254cc96131SMike Snitzer 	 * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests
1526f40c67f0SKiyoshi Ueda 	 *  don't have bio clones.)
1527f40c67f0SKiyoshi Ueda 	 * Instead of queueing the clone request here, we queue the original
1528f40c67f0SKiyoshi Ueda 	 * request into dm core, which will remake a clone request and
1529f40c67f0SKiyoshi Ueda 	 * clone bios for it and resubmit it later.
1530f40c67f0SKiyoshi Ueda 	 */
1531b79f10eeSChristoph Hellwig 	if (error && !noretry_error(error)) {
1532b79f10eeSChristoph Hellwig 		struct multipath *m = ti->private;
15331da177e4SLinus Torvalds 
15347ed8578aSChristoph Hellwig 		r = DM_ENDIO_REQUEUE;
15351da177e4SLinus Torvalds 
1536b79f10eeSChristoph Hellwig 		if (pgpath)
1537b79f10eeSChristoph Hellwig 			fail_path(pgpath);
15381da177e4SLinus Torvalds 
1539ca5beb76SBart Van Assche 		if (atomic_read(&m->nr_valid_paths) == 0 &&
1540c1fd0abeSMike Snitzer 		    !must_push_back_rq(m)) {
15412a842acaSChristoph Hellwig 			if (error == BLK_STS_IOERR)
154218a482f5SChristoph Hellwig 				dm_report_EIO(m);
15437ed8578aSChristoph Hellwig 			/* complete with the original error */
15447ed8578aSChristoph Hellwig 			r = DM_ENDIO_DONE;
15457ed8578aSChristoph Hellwig 		}
15461da177e4SLinus Torvalds 	}
15471da177e4SLinus Torvalds 
15481da177e4SLinus Torvalds 	if (pgpath) {
1549b79f10eeSChristoph Hellwig 		struct path_selector *ps = &pgpath->pg->ps;
1550b79f10eeSChristoph Hellwig 
15511da177e4SLinus Torvalds 		if (ps->type->end_io)
155202ab823fSKiyoshi Ueda 			ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
15531da177e4SLinus Torvalds 	}
15541da177e4SLinus Torvalds 
15557ed8578aSChristoph Hellwig 	return r;
15561da177e4SLinus Torvalds }
15571da177e4SLinus Torvalds 
15584e4cbee9SChristoph Hellwig static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone,
15594e4cbee9SChristoph Hellwig 				blk_status_t *error)
156076e33fe4SMike Snitzer {
156114ef1e48SChristoph Hellwig 	struct multipath *m = ti->private;
156214ef1e48SChristoph Hellwig 	struct dm_mpath_io *mpio = get_mpio_from_bio(clone);
156314ef1e48SChristoph Hellwig 	struct pgpath *pgpath = mpio->pgpath;
156476e33fe4SMike Snitzer 	unsigned long flags;
15651be56909SChristoph Hellwig 	int r = DM_ENDIO_DONE;
156676e33fe4SMike Snitzer 
15674e4cbee9SChristoph Hellwig 	if (!*error || noretry_error(*error))
156814ef1e48SChristoph Hellwig 		goto done;
156976e33fe4SMike Snitzer 
157014ef1e48SChristoph Hellwig 	if (pgpath)
157114ef1e48SChristoph Hellwig 		fail_path(pgpath);
157276e33fe4SMike Snitzer 
1573ca5beb76SBart Van Assche 	if (atomic_read(&m->nr_valid_paths) == 0 &&
157418a482f5SChristoph Hellwig 	    !test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
1575c1fd0abeSMike Snitzer 		if (must_push_back_bio(m)) {
1576c1fd0abeSMike Snitzer 			r = DM_ENDIO_REQUEUE;
1577c1fd0abeSMike Snitzer 		} else {
157818a482f5SChristoph Hellwig 			dm_report_EIO(m);
15794e4cbee9SChristoph Hellwig 			*error = BLK_STS_IOERR;
1580c1fd0abeSMike Snitzer 		}
158114ef1e48SChristoph Hellwig 		goto done;
158218a482f5SChristoph Hellwig 	}
158376e33fe4SMike Snitzer 
158476e33fe4SMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
158576e33fe4SMike Snitzer 	bio_list_add(&m->queued_bios, clone);
158676e33fe4SMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
158776e33fe4SMike Snitzer 	if (!test_bit(MPATHF_QUEUE_IO, &m->flags))
158876e33fe4SMike Snitzer 		queue_work(kmultipathd, &m->process_queued_bios);
158976e33fe4SMike Snitzer 
15901be56909SChristoph Hellwig 	r = DM_ENDIO_INCOMPLETE;
159114ef1e48SChristoph Hellwig done:
159276e33fe4SMike Snitzer 	if (pgpath) {
159314ef1e48SChristoph Hellwig 		struct path_selector *ps = &pgpath->pg->ps;
159414ef1e48SChristoph Hellwig 
159576e33fe4SMike Snitzer 		if (ps->type->end_io)
159676e33fe4SMike Snitzer 			ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
159776e33fe4SMike Snitzer 	}
159876e33fe4SMike Snitzer 
15991be56909SChristoph Hellwig 	return r;
160076e33fe4SMike Snitzer }
160176e33fe4SMike Snitzer 
16021da177e4SLinus Torvalds /*
16031da177e4SLinus Torvalds  * Suspend can't complete until all the I/O is processed so if
1604436d4108SAlasdair G Kergon  * the last path fails we must error any remaining I/O.
1605436d4108SAlasdair G Kergon  * Note that if the freeze_bdev fails while suspending, the
1606436d4108SAlasdair G Kergon  * queue_if_no_path state is lost - userspace should reset it.
16071da177e4SLinus Torvalds  */
16081da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti)
16091da177e4SLinus Torvalds {
16107943bd6dSMike Snitzer 	struct multipath *m = ti->private;
16111da177e4SLinus Torvalds 
1612be7d31ccSMike Snitzer 	queue_if_no_path(m, false, true);
16131da177e4SLinus Torvalds }
16141da177e4SLinus Torvalds 
16156df400abSKiyoshi Ueda static void multipath_postsuspend(struct dm_target *ti)
16166df400abSKiyoshi Ueda {
16176380f26fSMike Anderson 	struct multipath *m = ti->private;
16186380f26fSMike Anderson 
16196380f26fSMike Anderson 	mutex_lock(&m->work_mutex);
16202bded7bdSKiyoshi Ueda 	flush_multipath_work(m);
16216380f26fSMike Anderson 	mutex_unlock(&m->work_mutex);
16226df400abSKiyoshi Ueda }
16236df400abSKiyoshi Ueda 
1624436d4108SAlasdair G Kergon /*
1625436d4108SAlasdair G Kergon  * Restore the queue_if_no_path setting.
1626436d4108SAlasdair G Kergon  */
16271da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti)
16281da177e4SLinus Torvalds {
16297943bd6dSMike Snitzer 	struct multipath *m = ti->private;
16301814f2e3SMike Snitzer 	unsigned long flags;
16311da177e4SLinus Torvalds 
16321814f2e3SMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
16335307e2adSLukas Wunner 	assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags,
16345307e2adSLukas Wunner 		   test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags));
16351814f2e3SMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
16361da177e4SLinus Torvalds }
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds /*
16391da177e4SLinus Torvalds  * Info output has the following format:
16401da177e4SLinus Torvalds  * num_multipath_feature_args [multipath_feature_args]*
16411da177e4SLinus Torvalds  * num_handler_status_args [handler_status_args]*
16421da177e4SLinus Torvalds  * num_groups init_group_number
16431da177e4SLinus Torvalds  *            [A|D|E num_ps_status_args [ps_status_args]*
16441da177e4SLinus Torvalds  *             num_paths num_selector_args
16451da177e4SLinus Torvalds  *             [path_dev A|F fail_count [selector_args]* ]+ ]+
16461da177e4SLinus Torvalds  *
16471da177e4SLinus Torvalds  * Table output has the following format (identical to the constructor string):
16481da177e4SLinus Torvalds  * num_feature_args [features_args]*
16491da177e4SLinus Torvalds  * num_handler_args hw_handler [hw_handler_args]*
16501da177e4SLinus Torvalds  * num_groups init_group_number
16511da177e4SLinus Torvalds  *     [priority selector-name num_ps_args [ps_args]*
16521da177e4SLinus Torvalds  *      num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
16531da177e4SLinus Torvalds  */
1654fd7c092eSMikulas Patocka static void multipath_status(struct dm_target *ti, status_type_t type,
16551f4e0ff0SAlasdair G Kergon 			     unsigned status_flags, char *result, unsigned maxlen)
16561da177e4SLinus Torvalds {
16571da177e4SLinus Torvalds 	int sz = 0;
16581da177e4SLinus Torvalds 	unsigned long flags;
16597943bd6dSMike Snitzer 	struct multipath *m = ti->private;
16601da177e4SLinus Torvalds 	struct priority_group *pg;
16611da177e4SLinus Torvalds 	struct pgpath *p;
16621da177e4SLinus Torvalds 	unsigned pg_num;
16631da177e4SLinus Torvalds 	char state;
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 	/* Features */
16681da177e4SLinus Torvalds 	if (type == STATUSTYPE_INFO)
166991e968aaSMike Snitzer 		DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags),
167091e968aaSMike Snitzer 		       atomic_read(&m->pg_init_count));
1671c9e45581SDave Wysochanski 	else {
1672518257b1SMike Snitzer 		DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) +
16734e2d19e4SChandra Seetharaman 			      (m->pg_init_retries > 0) * 2 +
1674a58a935dSMike Snitzer 			      (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
1675e83068a5SMike Snitzer 			      test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) +
1676e83068a5SMike Snitzer 			      (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2);
1677e83068a5SMike Snitzer 
1678518257b1SMike Snitzer 		if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
1679c9e45581SDave Wysochanski 			DMEMIT("queue_if_no_path ");
1680c9e45581SDave Wysochanski 		if (m->pg_init_retries)
1681c9e45581SDave Wysochanski 			DMEMIT("pg_init_retries %u ", m->pg_init_retries);
16824e2d19e4SChandra Seetharaman 		if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
16834e2d19e4SChandra Seetharaman 			DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
1684518257b1SMike Snitzer 		if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags))
1685a58a935dSMike Snitzer 			DMEMIT("retain_attached_hw_handler ");
1686e83068a5SMike Snitzer 		if (m->queue_mode != DM_TYPE_REQUEST_BASED) {
1687e83068a5SMike Snitzer 			switch(m->queue_mode) {
1688e83068a5SMike Snitzer 			case DM_TYPE_BIO_BASED:
1689e83068a5SMike Snitzer 				DMEMIT("queue_mode bio ");
1690e83068a5SMike Snitzer 				break;
1691cd025384SMike Snitzer 			case DM_TYPE_NVME_BIO_BASED:
1692cd025384SMike Snitzer 				DMEMIT("queue_mode nvme ");
1693cd025384SMike Snitzer 				break;
1694e83068a5SMike Snitzer 			case DM_TYPE_MQ_REQUEST_BASED:
1695e83068a5SMike Snitzer 				DMEMIT("queue_mode mq ");
1696e83068a5SMike Snitzer 				break;
16977e0d574fSBart Van Assche 			default:
16987e0d574fSBart Van Assche 				WARN_ON_ONCE(true);
16997e0d574fSBart Van Assche 				break;
1700e83068a5SMike Snitzer 			}
1701e83068a5SMike Snitzer 		}
1702c9e45581SDave Wysochanski 	}
17031da177e4SLinus Torvalds 
1704cfae5c9bSChandra Seetharaman 	if (!m->hw_handler_name || type == STATUSTYPE_INFO)
17051da177e4SLinus Torvalds 		DMEMIT("0 ");
17061da177e4SLinus Torvalds 	else
1707cfae5c9bSChandra Seetharaman 		DMEMIT("1 %s ", m->hw_handler_name);
17081da177e4SLinus Torvalds 
17091da177e4SLinus Torvalds 	DMEMIT("%u ", m->nr_priority_groups);
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds 	if (m->next_pg)
17121da177e4SLinus Torvalds 		pg_num = m->next_pg->pg_num;
17131da177e4SLinus Torvalds 	else if (m->current_pg)
17141da177e4SLinus Torvalds 		pg_num = m->current_pg->pg_num;
17151da177e4SLinus Torvalds 	else
1716a490a07aSMike Snitzer 		pg_num = (m->nr_priority_groups ? 1 : 0);
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds 	DMEMIT("%u ", pg_num);
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds 	switch (type) {
17211da177e4SLinus Torvalds 	case STATUSTYPE_INFO:
17221da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
17231da177e4SLinus Torvalds 			if (pg->bypassed)
17241da177e4SLinus Torvalds 				state = 'D';	/* Disabled */
17251da177e4SLinus Torvalds 			else if (pg == m->current_pg)
17261da177e4SLinus Torvalds 				state = 'A';	/* Currently Active */
17271da177e4SLinus Torvalds 			else
17281da177e4SLinus Torvalds 				state = 'E';	/* Enabled */
17291da177e4SLinus Torvalds 
17301da177e4SLinus Torvalds 			DMEMIT("%c ", state);
17311da177e4SLinus Torvalds 
17321da177e4SLinus Torvalds 			if (pg->ps.type->status)
17331da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
17341da177e4SLinus Torvalds 							  result + sz,
17351da177e4SLinus Torvalds 							  maxlen - sz);
17361da177e4SLinus Torvalds 			else
17371da177e4SLinus Torvalds 				DMEMIT("0 ");
17381da177e4SLinus Torvalds 
17391da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
17401da177e4SLinus Torvalds 			       pg->ps.type->info_args);
17411da177e4SLinus Torvalds 
17421da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
17431da177e4SLinus Torvalds 				DMEMIT("%s %s %u ", p->path.dev->name,
17446680073dSKiyoshi Ueda 				       p->is_active ? "A" : "F",
17451da177e4SLinus Torvalds 				       p->fail_count);
17461da177e4SLinus Torvalds 				if (pg->ps.type->status)
17471da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
17481da177e4SLinus Torvalds 					      &p->path, type, result + sz,
17491da177e4SLinus Torvalds 					      maxlen - sz);
17501da177e4SLinus Torvalds 			}
17511da177e4SLinus Torvalds 		}
17521da177e4SLinus Torvalds 		break;
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds 	case STATUSTYPE_TABLE:
17551da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
17561da177e4SLinus Torvalds 			DMEMIT("%s ", pg->ps.type->name);
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds 			if (pg->ps.type->status)
17591da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
17601da177e4SLinus Torvalds 							  result + sz,
17611da177e4SLinus Torvalds 							  maxlen - sz);
17621da177e4SLinus Torvalds 			else
17631da177e4SLinus Torvalds 				DMEMIT("0 ");
17641da177e4SLinus Torvalds 
17651da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
17661da177e4SLinus Torvalds 			       pg->ps.type->table_args);
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
17691da177e4SLinus Torvalds 				DMEMIT("%s ", p->path.dev->name);
17701da177e4SLinus Torvalds 				if (pg->ps.type->status)
17711da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
17721da177e4SLinus Torvalds 					      &p->path, type, result + sz,
17731da177e4SLinus Torvalds 					      maxlen - sz);
17741da177e4SLinus Torvalds 			}
17751da177e4SLinus Torvalds 		}
17761da177e4SLinus Torvalds 		break;
17771da177e4SLinus Torvalds 	}
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
17801da177e4SLinus Torvalds }
17811da177e4SLinus Torvalds 
17821da177e4SLinus Torvalds static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
17831da177e4SLinus Torvalds {
17846380f26fSMike Anderson 	int r = -EINVAL;
17851da177e4SLinus Torvalds 	struct dm_dev *dev;
17867943bd6dSMike Snitzer 	struct multipath *m = ti->private;
17871da177e4SLinus Torvalds 	action_fn action;
17881da177e4SLinus Torvalds 
17896380f26fSMike Anderson 	mutex_lock(&m->work_mutex);
17906380f26fSMike Anderson 
1791c2f3d24bSKiyoshi Ueda 	if (dm_suspended(ti)) {
1792c2f3d24bSKiyoshi Ueda 		r = -EBUSY;
1793c2f3d24bSKiyoshi Ueda 		goto out;
1794c2f3d24bSKiyoshi Ueda 	}
1795c2f3d24bSKiyoshi Ueda 
17961da177e4SLinus Torvalds 	if (argc == 1) {
1797498f0103SMike Snitzer 		if (!strcasecmp(argv[0], "queue_if_no_path")) {
1798be7d31ccSMike Snitzer 			r = queue_if_no_path(m, true, false);
17996380f26fSMike Anderson 			goto out;
1800498f0103SMike Snitzer 		} else if (!strcasecmp(argv[0], "fail_if_no_path")) {
1801be7d31ccSMike Snitzer 			r = queue_if_no_path(m, false, false);
18026380f26fSMike Anderson 			goto out;
18036380f26fSMike Anderson 		}
18041da177e4SLinus Torvalds 	}
18051da177e4SLinus Torvalds 
18066380f26fSMike Anderson 	if (argc != 2) {
1807a356e426SJose Castillo 		DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
18086380f26fSMike Anderson 		goto out;
18096380f26fSMike Anderson 	}
18101da177e4SLinus Torvalds 
1811498f0103SMike Snitzer 	if (!strcasecmp(argv[0], "disable_group")) {
1812be7d31ccSMike Snitzer 		r = bypass_pg_num(m, argv[1], true);
18136380f26fSMike Anderson 		goto out;
1814498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "enable_group")) {
1815be7d31ccSMike Snitzer 		r = bypass_pg_num(m, argv[1], false);
18166380f26fSMike Anderson 		goto out;
1817498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "switch_group")) {
18186380f26fSMike Anderson 		r = switch_pg_num(m, argv[1]);
18196380f26fSMike Anderson 		goto out;
1820498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "reinstate_path"))
18211da177e4SLinus Torvalds 		action = reinstate_path;
1822498f0103SMike Snitzer 	else if (!strcasecmp(argv[0], "fail_path"))
18231da177e4SLinus Torvalds 		action = fail_path;
18246380f26fSMike Anderson 	else {
1825a356e426SJose Castillo 		DMWARN("Unrecognised multipath message received: %s", argv[0]);
18266380f26fSMike Anderson 		goto out;
18276380f26fSMike Anderson 	}
18281da177e4SLinus Torvalds 
18298215d6ecSNikanth Karthikesan 	r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
18301da177e4SLinus Torvalds 	if (r) {
183172d94861SAlasdair G Kergon 		DMWARN("message: error getting device %s",
18321da177e4SLinus Torvalds 		       argv[1]);
18336380f26fSMike Anderson 		goto out;
18341da177e4SLinus Torvalds 	}
18351da177e4SLinus Torvalds 
18361da177e4SLinus Torvalds 	r = action_dev(m, dev, action);
18371da177e4SLinus Torvalds 
18381da177e4SLinus Torvalds 	dm_put_device(ti, dev);
18391da177e4SLinus Torvalds 
18406380f26fSMike Anderson out:
18416380f26fSMike Anderson 	mutex_unlock(&m->work_mutex);
18421da177e4SLinus Torvalds 	return r;
18431da177e4SLinus Torvalds }
18441da177e4SLinus Torvalds 
1845e56f81e0SChristoph Hellwig static int multipath_prepare_ioctl(struct dm_target *ti,
1846e56f81e0SChristoph Hellwig 		struct block_device **bdev, fmode_t *mode)
18479af4aa30SMilan Broz {
184835991652SMikulas Patocka 	struct multipath *m = ti->private;
18492da1610aSMike Snitzer 	struct pgpath *current_pgpath;
185035991652SMikulas Patocka 	int r;
185135991652SMikulas Patocka 
1852506458efSWill Deacon 	current_pgpath = READ_ONCE(m->current_pgpath);
18532da1610aSMike Snitzer 	if (!current_pgpath)
18542da1610aSMike Snitzer 		current_pgpath = choose_pgpath(m, 0);
18559af4aa30SMilan Broz 
18562da1610aSMike Snitzer 	if (current_pgpath) {
1857518257b1SMike Snitzer 		if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) {
18582da1610aSMike Snitzer 			*bdev = current_pgpath->path.dev->bdev;
18592da1610aSMike Snitzer 			*mode = current_pgpath->path.dev->mode;
186043e43c9eSJunichi Nomura 			r = 0;
186143e43c9eSJunichi Nomura 		} else {
186243e43c9eSJunichi Nomura 			/* pg_init has not started or completed */
18636c182cd8SHannes Reinecke 			r = -ENOTCONN;
186443e43c9eSJunichi Nomura 		}
186543e43c9eSJunichi Nomura 	} else {
186643e43c9eSJunichi Nomura 		/* No path is available */
1867518257b1SMike Snitzer 		if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
186843e43c9eSJunichi Nomura 			r = -ENOTCONN;
186943e43c9eSJunichi Nomura 		else
18709af4aa30SMilan Broz 			r = -EIO;
187143e43c9eSJunichi Nomura 	}
18729af4aa30SMilan Broz 
18735bbbfdf6SJunichi Nomura 	if (r == -ENOTCONN) {
1874506458efSWill Deacon 		if (!READ_ONCE(m->current_pg)) {
18753e9f1be1SHannes Reinecke 			/* Path status changed, redo selection */
18762da1610aSMike Snitzer 			(void) choose_pgpath(m, 0);
18773e9f1be1SHannes Reinecke 		}
1878518257b1SMike Snitzer 		if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
18792da1610aSMike Snitzer 			pg_init_all_paths(m);
188063d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
18817e48c768SMike Snitzer 		process_queued_io_list(m);
18823e9f1be1SHannes Reinecke 	}
188335991652SMikulas Patocka 
1884e56f81e0SChristoph Hellwig 	/*
1885e56f81e0SChristoph Hellwig 	 * Only pass ioctls through if the device sizes match exactly.
1886e56f81e0SChristoph Hellwig 	 */
1887e56f81e0SChristoph Hellwig 	if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
1888e56f81e0SChristoph Hellwig 		return 1;
1889e56f81e0SChristoph Hellwig 	return r;
18909af4aa30SMilan Broz }
18919af4aa30SMilan Broz 
1892af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti,
1893af4874e0SMike Snitzer 				     iterate_devices_callout_fn fn, void *data)
1894af4874e0SMike Snitzer {
1895af4874e0SMike Snitzer 	struct multipath *m = ti->private;
1896af4874e0SMike Snitzer 	struct priority_group *pg;
1897af4874e0SMike Snitzer 	struct pgpath *p;
1898af4874e0SMike Snitzer 	int ret = 0;
1899af4874e0SMike Snitzer 
1900af4874e0SMike Snitzer 	list_for_each_entry(pg, &m->priority_groups, list) {
1901af4874e0SMike Snitzer 		list_for_each_entry(p, &pg->pgpaths, list) {
19025dea271bSMike Snitzer 			ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1903af4874e0SMike Snitzer 			if (ret)
1904af4874e0SMike Snitzer 				goto out;
1905af4874e0SMike Snitzer 		}
1906af4874e0SMike Snitzer 	}
1907af4874e0SMike Snitzer 
1908af4874e0SMike Snitzer out:
1909af4874e0SMike Snitzer 	return ret;
1910af4874e0SMike Snitzer }
1911af4874e0SMike Snitzer 
19129f54cec5SMike Snitzer static int pgpath_busy(struct pgpath *pgpath)
1913f40c67f0SKiyoshi Ueda {
1914f40c67f0SKiyoshi Ueda 	struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1915f40c67f0SKiyoshi Ueda 
191652b09914SMike Snitzer 	return blk_lld_busy(q);
1917f40c67f0SKiyoshi Ueda }
1918f40c67f0SKiyoshi Ueda 
1919f40c67f0SKiyoshi Ueda /*
1920f40c67f0SKiyoshi Ueda  * We return "busy", only when we can map I/Os but underlying devices
1921f40c67f0SKiyoshi Ueda  * are busy (so even if we map I/Os now, the I/Os will wait on
1922f40c67f0SKiyoshi Ueda  * the underlying queue).
1923f40c67f0SKiyoshi Ueda  * In other words, if we want to kill I/Os or queue them inside us
1924f40c67f0SKiyoshi Ueda  * due to map unavailability, we don't return "busy".  Otherwise,
1925f40c67f0SKiyoshi Ueda  * dm core won't give us the I/Os and we can't do what we want.
1926f40c67f0SKiyoshi Ueda  */
1927f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti)
1928f40c67f0SKiyoshi Ueda {
1929be7d31ccSMike Snitzer 	bool busy = false, has_active = false;
1930f40c67f0SKiyoshi Ueda 	struct multipath *m = ti->private;
19312da1610aSMike Snitzer 	struct priority_group *pg, *next_pg;
1932f40c67f0SKiyoshi Ueda 	struct pgpath *pgpath;
1933f40c67f0SKiyoshi Ueda 
1934b88efd43SMike Snitzer 	/* pg_init in progress */
1935b88efd43SMike Snitzer 	if (atomic_read(&m->pg_init_in_progress))
19362da1610aSMike Snitzer 		return true;
19372da1610aSMike Snitzer 
1938b88efd43SMike Snitzer 	/* no paths available, for blk-mq: rely on IO mapping to delay requeue */
1939b88efd43SMike Snitzer 	if (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
1940b88efd43SMike Snitzer 		return (m->queue_mode != DM_TYPE_MQ_REQUEST_BASED);
1941b88efd43SMike Snitzer 
1942f40c67f0SKiyoshi Ueda 	/* Guess which priority_group will be used at next mapping time */
1943506458efSWill Deacon 	pg = READ_ONCE(m->current_pg);
1944506458efSWill Deacon 	next_pg = READ_ONCE(m->next_pg);
1945506458efSWill Deacon 	if (unlikely(!READ_ONCE(m->current_pgpath) && next_pg))
19462da1610aSMike Snitzer 		pg = next_pg;
19472da1610aSMike Snitzer 
19482da1610aSMike Snitzer 	if (!pg) {
1949f40c67f0SKiyoshi Ueda 		/*
1950f40c67f0SKiyoshi Ueda 		 * We don't know which pg will be used at next mapping time.
19512da1610aSMike Snitzer 		 * We don't call choose_pgpath() here to avoid to trigger
1952f40c67f0SKiyoshi Ueda 		 * pg_init just by busy checking.
1953f40c67f0SKiyoshi Ueda 		 * So we don't know whether underlying devices we will be using
1954f40c67f0SKiyoshi Ueda 		 * at next mapping time are busy or not. Just try mapping.
1955f40c67f0SKiyoshi Ueda 		 */
19562da1610aSMike Snitzer 		return busy;
19572da1610aSMike Snitzer 	}
1958f40c67f0SKiyoshi Ueda 
1959f40c67f0SKiyoshi Ueda 	/*
1960f40c67f0SKiyoshi Ueda 	 * If there is one non-busy active path at least, the path selector
1961f40c67f0SKiyoshi Ueda 	 * will be able to select it. So we consider such a pg as not busy.
1962f40c67f0SKiyoshi Ueda 	 */
1963be7d31ccSMike Snitzer 	busy = true;
19642da1610aSMike Snitzer 	list_for_each_entry(pgpath, &pg->pgpaths, list) {
1965f40c67f0SKiyoshi Ueda 		if (pgpath->is_active) {
1966be7d31ccSMike Snitzer 			has_active = true;
19679f54cec5SMike Snitzer 			if (!pgpath_busy(pgpath)) {
1968be7d31ccSMike Snitzer 				busy = false;
1969f40c67f0SKiyoshi Ueda 				break;
1970f40c67f0SKiyoshi Ueda 			}
1971f40c67f0SKiyoshi Ueda 		}
19722da1610aSMike Snitzer 	}
1973f40c67f0SKiyoshi Ueda 
19742da1610aSMike Snitzer 	if (!has_active) {
1975f40c67f0SKiyoshi Ueda 		/*
1976f40c67f0SKiyoshi Ueda 		 * No active path in this pg, so this pg won't be used and
1977f40c67f0SKiyoshi Ueda 		 * the current_pg will be changed at next mapping time.
1978f40c67f0SKiyoshi Ueda 		 * We need to try mapping to determine it.
1979f40c67f0SKiyoshi Ueda 		 */
1980be7d31ccSMike Snitzer 		busy = false;
19812da1610aSMike Snitzer 	}
1982f40c67f0SKiyoshi Ueda 
1983f40c67f0SKiyoshi Ueda 	return busy;
1984f40c67f0SKiyoshi Ueda }
1985f40c67f0SKiyoshi Ueda 
19861da177e4SLinus Torvalds /*-----------------------------------------------------------------
19871da177e4SLinus Torvalds  * Module setup
19881da177e4SLinus Torvalds  *---------------------------------------------------------------*/
19891da177e4SLinus Torvalds static struct target_type multipath_target = {
19901da177e4SLinus Torvalds 	.name = "multipath",
1991e83068a5SMike Snitzer 	.version = {1, 12, 0},
199216f12266SMike Snitzer 	.features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE,
19931da177e4SLinus Torvalds 	.module = THIS_MODULE,
19941da177e4SLinus Torvalds 	.ctr = multipath_ctr,
19951da177e4SLinus Torvalds 	.dtr = multipath_dtr,
1996e5863d9aSMike Snitzer 	.clone_and_map_rq = multipath_clone_and_map,
1997e5863d9aSMike Snitzer 	.release_clone_rq = multipath_release_clone,
1998f40c67f0SKiyoshi Ueda 	.rq_end_io = multipath_end_io,
199976e33fe4SMike Snitzer 	.map = multipath_map_bio,
200076e33fe4SMike Snitzer 	.end_io = multipath_end_io_bio,
200176e33fe4SMike Snitzer 	.presuspend = multipath_presuspend,
200276e33fe4SMike Snitzer 	.postsuspend = multipath_postsuspend,
200376e33fe4SMike Snitzer 	.resume = multipath_resume,
200476e33fe4SMike Snitzer 	.status = multipath_status,
200576e33fe4SMike Snitzer 	.message = multipath_message,
200676e33fe4SMike Snitzer 	.prepare_ioctl = multipath_prepare_ioctl,
200776e33fe4SMike Snitzer 	.iterate_devices = multipath_iterate_devices,
200876e33fe4SMike Snitzer 	.busy = multipath_busy,
200976e33fe4SMike Snitzer };
201076e33fe4SMike Snitzer 
20111da177e4SLinus Torvalds static int __init dm_multipath_init(void)
20121da177e4SLinus Torvalds {
20131da177e4SLinus Torvalds 	int r;
20141da177e4SLinus Torvalds 
20154d4d66abSTejun Heo 	kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
2016c557308eSAlasdair G Kergon 	if (!kmultipathd) {
20170cd33124SAlasdair G Kergon 		DMERR("failed to create workqueue kmpathd");
2018ff658e9cSJohannes Thumshirn 		r = -ENOMEM;
2019ff658e9cSJohannes Thumshirn 		goto bad_alloc_kmultipathd;
2020c557308eSAlasdair G Kergon 	}
2021c557308eSAlasdair G Kergon 
2022bab7cfc7SChandra Seetharaman 	/*
2023bab7cfc7SChandra Seetharaman 	 * A separate workqueue is used to handle the device handlers
2024bab7cfc7SChandra Seetharaman 	 * to avoid overloading existing workqueue. Overloading the
2025bab7cfc7SChandra Seetharaman 	 * old workqueue would also create a bottleneck in the
2026bab7cfc7SChandra Seetharaman 	 * path of the storage hardware device activation.
2027bab7cfc7SChandra Seetharaman 	 */
20284d4d66abSTejun Heo 	kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
20294d4d66abSTejun Heo 						  WQ_MEM_RECLAIM);
2030bab7cfc7SChandra Seetharaman 	if (!kmpath_handlerd) {
2031bab7cfc7SChandra Seetharaman 		DMERR("failed to create workqueue kmpath_handlerd");
2032ff658e9cSJohannes Thumshirn 		r = -ENOMEM;
2033ff658e9cSJohannes Thumshirn 		goto bad_alloc_kmpath_handlerd;
2034bab7cfc7SChandra Seetharaman 	}
2035bab7cfc7SChandra Seetharaman 
20367e6358d2Smonty_pavel@sina.com 	r = dm_register_target(&multipath_target);
20377e6358d2Smonty_pavel@sina.com 	if (r < 0) {
20387e6358d2Smonty_pavel@sina.com 		DMERR("request-based register failed %d", r);
20397e6358d2Smonty_pavel@sina.com 		r = -EINVAL;
20407e6358d2Smonty_pavel@sina.com 		goto bad_register_target;
20417e6358d2Smonty_pavel@sina.com 	}
20427e6358d2Smonty_pavel@sina.com 
2043ff658e9cSJohannes Thumshirn 	return 0;
2044ff658e9cSJohannes Thumshirn 
20457e6358d2Smonty_pavel@sina.com bad_register_target:
20467e6358d2Smonty_pavel@sina.com 	destroy_workqueue(kmpath_handlerd);
2047ff658e9cSJohannes Thumshirn bad_alloc_kmpath_handlerd:
2048ff658e9cSJohannes Thumshirn 	destroy_workqueue(kmultipathd);
2049ff658e9cSJohannes Thumshirn bad_alloc_kmultipathd:
20501da177e4SLinus Torvalds 	return r;
20511da177e4SLinus Torvalds }
20521da177e4SLinus Torvalds 
20531da177e4SLinus Torvalds static void __exit dm_multipath_exit(void)
20541da177e4SLinus Torvalds {
2055bab7cfc7SChandra Seetharaman 	destroy_workqueue(kmpath_handlerd);
2056c557308eSAlasdair G Kergon 	destroy_workqueue(kmultipathd);
2057c557308eSAlasdair G Kergon 
205810d3bd09SMikulas Patocka 	dm_unregister_target(&multipath_target);
20591da177e4SLinus Torvalds }
20601da177e4SLinus Torvalds 
20611da177e4SLinus Torvalds module_init(dm_multipath_init);
20621da177e4SLinus Torvalds module_exit(dm_multipath_exit);
20631da177e4SLinus Torvalds 
20641da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target");
20651da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
20661da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2067