xref: /openbmc/linux/drivers/md/dm-mpath.c (revision 06eb061f48594aa369f6e852b352410298b317a8)
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 {
671da177e4SLinus Torvalds 	struct list_head list;
681da177e4SLinus Torvalds 	struct dm_target *ti;
691da177e4SLinus Torvalds 
70cfae5c9bSChandra Seetharaman 	const char *hw_handler_name;
712bfd2e13SChandra Seetharaman 	char *hw_handler_params;
724e2d19e4SChandra Seetharaman 
731fbdd2b3SMike Snitzer 	spinlock_t lock;
741fbdd2b3SMike Snitzer 
751da177e4SLinus Torvalds 	unsigned nr_priority_groups;
761da177e4SLinus Torvalds 	struct list_head priority_groups;
774e2d19e4SChandra Seetharaman 
784e2d19e4SChandra Seetharaman 	wait_queue_head_t pg_init_wait;	/* Wait for pg_init completion */
794e2d19e4SChandra Seetharaman 
801da177e4SLinus Torvalds 	struct pgpath *current_pgpath;
811da177e4SLinus Torvalds 	struct priority_group *current_pg;
821da177e4SLinus Torvalds 	struct priority_group *next_pg;	/* Switch to this PG if set */
831da177e4SLinus Torvalds 
84518257b1SMike Snitzer 	unsigned long flags;		/* Multipath state flags */
851fbdd2b3SMike Snitzer 
86c9e45581SDave Wysochanski 	unsigned pg_init_retries;	/* Number of times to retry pg_init */
874e2d19e4SChandra Seetharaman 	unsigned pg_init_delay_msecs;	/* Number of msecs before pg_init retry */
881da177e4SLinus Torvalds 
8991e968aaSMike Snitzer 	atomic_t nr_valid_paths;	/* Total number of usable paths */
9091e968aaSMike Snitzer 	atomic_t pg_init_in_progress;	/* Only one pg_init allowed at once */
9191e968aaSMike Snitzer 	atomic_t pg_init_count;		/* Number of times pg_init called */
9291e968aaSMike Snitzer 
93e83068a5SMike Snitzer 	unsigned queue_mode;
94e83068a5SMike Snitzer 
956380f26fSMike Anderson 	struct mutex work_mutex;
9620800cb3SMike Snitzer 	struct work_struct trigger_event;
9776e33fe4SMike Snitzer 
9876e33fe4SMike Snitzer 	struct work_struct process_queued_bios;
9976e33fe4SMike Snitzer 	struct bio_list queued_bios;
1001da177e4SLinus Torvalds };
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds /*
10376e33fe4SMike Snitzer  * Context information attached to each io we process.
1041da177e4SLinus Torvalds  */
105028867acSAlasdair G Kergon struct dm_mpath_io {
1061da177e4SLinus Torvalds 	struct pgpath *pgpath;
10702ab823fSKiyoshi Ueda 	size_t nr_bytes;
1081da177e4SLinus Torvalds };
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds typedef int (*action_fn) (struct pgpath *pgpath);
1111da177e4SLinus Torvalds 
112bab7cfc7SChandra Seetharaman static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
113c4028958SDavid Howells static void trigger_event(struct work_struct *work);
114bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work);
11576e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work);
1161da177e4SLinus Torvalds 
117518257b1SMike Snitzer /*-----------------------------------------------
118518257b1SMike Snitzer  * Multipath state flags.
119518257b1SMike Snitzer  *-----------------------------------------------*/
120518257b1SMike Snitzer 
121518257b1SMike Snitzer #define MPATHF_QUEUE_IO 0			/* Must we queue all I/O? */
122518257b1SMike Snitzer #define MPATHF_QUEUE_IF_NO_PATH 1		/* Queue I/O if last path fails? */
123518257b1SMike Snitzer #define MPATHF_SAVED_QUEUE_IF_NO_PATH 2		/* Saved state during suspension */
124518257b1SMike Snitzer #define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3	/* If there's already a hw_handler present, don't change it. */
125518257b1SMike Snitzer #define MPATHF_PG_INIT_DISABLED 4		/* pg_init is not currently allowed */
126518257b1SMike Snitzer #define MPATHF_PG_INIT_REQUIRED 5		/* pg_init needs calling? */
127518257b1SMike Snitzer #define MPATHF_PG_INIT_DELAY_RETRY 6		/* Delay pg_init retry? */
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds /*-----------------------------------------------
1301da177e4SLinus Torvalds  * Allocation routines
1311da177e4SLinus Torvalds  *-----------------------------------------------*/
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds static struct pgpath *alloc_pgpath(void)
1341da177e4SLinus Torvalds {
135e69fae56SMicha³ Miros³aw 	struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
1361da177e4SLinus Torvalds 
137224cb3e9SMike Anderson 	if (pgpath) {
138be7d31ccSMike Snitzer 		pgpath->is_active = true;
1394e2d19e4SChandra Seetharaman 		INIT_DELAYED_WORK(&pgpath->activate_path, activate_path);
140224cb3e9SMike Anderson 	}
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	return pgpath;
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
145028867acSAlasdair G Kergon static void free_pgpath(struct pgpath *pgpath)
1461da177e4SLinus Torvalds {
1471da177e4SLinus Torvalds 	kfree(pgpath);
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds static struct priority_group *alloc_priority_group(void)
1511da177e4SLinus Torvalds {
1521da177e4SLinus Torvalds 	struct priority_group *pg;
1531da177e4SLinus Torvalds 
154e69fae56SMicha³ Miros³aw 	pg = kzalloc(sizeof(*pg), GFP_KERNEL);
1551da177e4SLinus Torvalds 
156e69fae56SMicha³ Miros³aw 	if (pg)
1571da177e4SLinus Torvalds 		INIT_LIST_HEAD(&pg->pgpaths);
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	return pg;
1601da177e4SLinus Torvalds }
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
1631da177e4SLinus Torvalds {
1641da177e4SLinus Torvalds 	struct pgpath *pgpath, *tmp;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
1671da177e4SLinus Torvalds 		list_del(&pgpath->list);
1681da177e4SLinus Torvalds 		dm_put_device(ti, pgpath->path.dev);
1691da177e4SLinus Torvalds 		free_pgpath(pgpath);
1701da177e4SLinus Torvalds 	}
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds static void free_priority_group(struct priority_group *pg,
1741da177e4SLinus Torvalds 				struct dm_target *ti)
1751da177e4SLinus Torvalds {
1761da177e4SLinus Torvalds 	struct path_selector *ps = &pg->ps;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	if (ps->type) {
1791da177e4SLinus Torvalds 		ps->type->destroy(ps);
1801da177e4SLinus Torvalds 		dm_put_path_selector(ps->type);
1811da177e4SLinus Torvalds 	}
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	free_pgpaths(&pg->pgpaths, ti);
1841da177e4SLinus Torvalds 	kfree(pg);
1851da177e4SLinus Torvalds }
1861da177e4SLinus Torvalds 
187e83068a5SMike Snitzer static struct multipath *alloc_multipath(struct dm_target *ti)
1881da177e4SLinus Torvalds {
1891da177e4SLinus Torvalds 	struct multipath *m;
1901da177e4SLinus Torvalds 
191e69fae56SMicha³ Miros³aw 	m = kzalloc(sizeof(*m), GFP_KERNEL);
1921da177e4SLinus Torvalds 	if (m) {
1931da177e4SLinus Torvalds 		INIT_LIST_HEAD(&m->priority_groups);
1941da177e4SLinus Torvalds 		spin_lock_init(&m->lock);
195518257b1SMike Snitzer 		set_bit(MPATHF_QUEUE_IO, &m->flags);
19691e968aaSMike Snitzer 		atomic_set(&m->nr_valid_paths, 0);
19791e968aaSMike Snitzer 		atomic_set(&m->pg_init_in_progress, 0);
19891e968aaSMike Snitzer 		atomic_set(&m->pg_init_count, 0);
1994e2d19e4SChandra Seetharaman 		m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
200c4028958SDavid Howells 		INIT_WORK(&m->trigger_event, trigger_event);
2012bded7bdSKiyoshi Ueda 		init_waitqueue_head(&m->pg_init_wait);
2026380f26fSMike Anderson 		mutex_init(&m->work_mutex);
2038637a6bfSMike Snitzer 
204e83068a5SMike Snitzer 		m->queue_mode = DM_TYPE_NONE;
205e83068a5SMike Snitzer 
206e83068a5SMike Snitzer 		m->ti = ti;
207e83068a5SMike Snitzer 		ti->private = m;
208e83068a5SMike Snitzer 	}
209e83068a5SMike Snitzer 
210e83068a5SMike Snitzer 	return m;
211e83068a5SMike Snitzer }
212e83068a5SMike Snitzer 
213e83068a5SMike Snitzer static int alloc_multipath_stage2(struct dm_target *ti, struct multipath *m)
214e83068a5SMike Snitzer {
215e83068a5SMike Snitzer 	if (m->queue_mode == DM_TYPE_NONE) {
216e83068a5SMike Snitzer 		/*
217e83068a5SMike Snitzer 		 * Default to request-based.
218e83068a5SMike Snitzer 		 */
219e83068a5SMike Snitzer 		if (dm_use_blk_mq(dm_table_get_md(ti->table)))
220e83068a5SMike Snitzer 			m->queue_mode = DM_TYPE_MQ_REQUEST_BASED;
221e83068a5SMike Snitzer 		else
222e83068a5SMike Snitzer 			m->queue_mode = DM_TYPE_REQUEST_BASED;
223eb8db831SChristoph Hellwig 	} else if (m->queue_mode == DM_TYPE_BIO_BASED) {
22476e33fe4SMike Snitzer 		INIT_WORK(&m->process_queued_bios, process_queued_bios);
22576e33fe4SMike Snitzer 		/*
22676e33fe4SMike Snitzer 		 * bio-based doesn't support any direct scsi_dh management;
22776e33fe4SMike Snitzer 		 * it just discovers if a scsi_dh is attached.
22876e33fe4SMike Snitzer 		 */
22976e33fe4SMike Snitzer 		set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
23076e33fe4SMike Snitzer 	}
23176e33fe4SMike Snitzer 
232e83068a5SMike Snitzer 	dm_table_set_type(ti->table, m->queue_mode);
2331da177e4SLinus Torvalds 
234e83068a5SMike Snitzer 	return 0;
2351da177e4SLinus Torvalds }
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds static void free_multipath(struct multipath *m)
2381da177e4SLinus Torvalds {
2391da177e4SLinus Torvalds 	struct priority_group *pg, *tmp;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
2421da177e4SLinus Torvalds 		list_del(&pg->list);
2431da177e4SLinus Torvalds 		free_priority_group(pg, m->ti);
2441da177e4SLinus Torvalds 	}
2451da177e4SLinus Torvalds 
246cfae5c9bSChandra Seetharaman 	kfree(m->hw_handler_name);
2472bfd2e13SChandra Seetharaman 	kfree(m->hw_handler_params);
2481da177e4SLinus Torvalds 	kfree(m);
2491da177e4SLinus Torvalds }
2501da177e4SLinus Torvalds 
2512eff1924SMike Snitzer static struct dm_mpath_io *get_mpio(union map_info *info)
2522eff1924SMike Snitzer {
2532eff1924SMike Snitzer 	return info->ptr;
2542eff1924SMike Snitzer }
2552eff1924SMike Snitzer 
256bf661be1SMike Snitzer static size_t multipath_per_bio_data_size(void)
25776e33fe4SMike Snitzer {
258bf661be1SMike Snitzer 	return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details);
25976e33fe4SMike Snitzer }
26076e33fe4SMike Snitzer 
261bf661be1SMike Snitzer static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio)
262bf661be1SMike Snitzer {
263bf661be1SMike Snitzer 	return dm_per_bio_data(bio, multipath_per_bio_data_size());
264bf661be1SMike Snitzer }
265bf661be1SMike Snitzer 
266bf661be1SMike Snitzer static struct dm_bio_details *get_bio_details_from_bio(struct bio *bio)
267bf661be1SMike Snitzer {
268bf661be1SMike Snitzer 	/* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */
269bf661be1SMike Snitzer 	struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
270bf661be1SMike Snitzer 	void *bio_details = mpio + 1;
271bf661be1SMike Snitzer 
272bf661be1SMike Snitzer 	return bio_details;
273bf661be1SMike Snitzer }
274bf661be1SMike Snitzer 
275bf661be1SMike Snitzer static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p,
276bf661be1SMike Snitzer 					struct dm_bio_details **bio_details_p)
27776e33fe4SMike Snitzer {
27876e33fe4SMike Snitzer 	struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
279bf661be1SMike Snitzer 	struct dm_bio_details *bio_details = get_bio_details_from_bio(bio);
28076e33fe4SMike Snitzer 
28176e33fe4SMike Snitzer 	memset(mpio, 0, sizeof(*mpio));
282bf661be1SMike Snitzer 	memset(bio_details, 0, sizeof(*bio_details));
283bf661be1SMike Snitzer 	dm_bio_record(bio_details, bio);
28476e33fe4SMike Snitzer 
285bf661be1SMike Snitzer 	if (mpio_p)
286bf661be1SMike Snitzer 		*mpio_p = mpio;
287bf661be1SMike Snitzer 	if (bio_details_p)
288bf661be1SMike Snitzer 		*bio_details_p = bio_details;
28976e33fe4SMike Snitzer }
29076e33fe4SMike Snitzer 
2911da177e4SLinus Torvalds /*-----------------------------------------------
2921da177e4SLinus Torvalds  * Path selection
2931da177e4SLinus Torvalds  *-----------------------------------------------*/
2941da177e4SLinus Torvalds 
2953e9f1be1SHannes Reinecke static int __pg_init_all_paths(struct multipath *m)
296fb612642SKiyoshi Ueda {
297fb612642SKiyoshi Ueda 	struct pgpath *pgpath;
2984e2d19e4SChandra Seetharaman 	unsigned long pg_init_delay = 0;
299fb612642SKiyoshi Ueda 
30091e968aaSMike Snitzer 	if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
3013e9f1be1SHannes Reinecke 		return 0;
30217f4ff45SHannes Reinecke 
30391e968aaSMike Snitzer 	atomic_inc(&m->pg_init_count);
304518257b1SMike Snitzer 	clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
3053e9f1be1SHannes Reinecke 
3063e9f1be1SHannes Reinecke 	/* Check here to reset pg_init_required */
3073e9f1be1SHannes Reinecke 	if (!m->current_pg)
3083e9f1be1SHannes Reinecke 		return 0;
3093e9f1be1SHannes Reinecke 
310518257b1SMike Snitzer 	if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags))
3114e2d19e4SChandra Seetharaman 		pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
3124e2d19e4SChandra Seetharaman 						 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
313fb612642SKiyoshi Ueda 	list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
314fb612642SKiyoshi Ueda 		/* Skip failed paths */
315fb612642SKiyoshi Ueda 		if (!pgpath->is_active)
316fb612642SKiyoshi Ueda 			continue;
3174e2d19e4SChandra Seetharaman 		if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
3184e2d19e4SChandra Seetharaman 				       pg_init_delay))
31991e968aaSMike Snitzer 			atomic_inc(&m->pg_init_in_progress);
320fb612642SKiyoshi Ueda 	}
32191e968aaSMike Snitzer 	return atomic_read(&m->pg_init_in_progress);
322fb612642SKiyoshi Ueda }
323fb612642SKiyoshi Ueda 
3244813577fSBart Van Assche static void pg_init_all_paths(struct multipath *m)
3251da177e4SLinus Torvalds {
3262da1610aSMike Snitzer 	unsigned long flags;
3272da1610aSMike Snitzer 
3282da1610aSMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
3294813577fSBart Van Assche 	__pg_init_all_paths(m);
3302da1610aSMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
3312da1610aSMike Snitzer }
3322da1610aSMike Snitzer 
3332da1610aSMike Snitzer static void __switch_pg(struct multipath *m, struct priority_group *pg)
3342da1610aSMike Snitzer {
3352da1610aSMike Snitzer 	m->current_pg = pg;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	/* Must we initialise the PG first, and queue I/O till it's ready? */
338cfae5c9bSChandra Seetharaman 	if (m->hw_handler_name) {
339518257b1SMike Snitzer 		set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
340518257b1SMike Snitzer 		set_bit(MPATHF_QUEUE_IO, &m->flags);
3411da177e4SLinus Torvalds 	} else {
342518257b1SMike Snitzer 		clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
343518257b1SMike Snitzer 		clear_bit(MPATHF_QUEUE_IO, &m->flags);
3441da177e4SLinus Torvalds 	}
345c9e45581SDave Wysochanski 
34691e968aaSMike Snitzer 	atomic_set(&m->pg_init_count, 0);
3471da177e4SLinus Torvalds }
3481da177e4SLinus Torvalds 
3492da1610aSMike Snitzer static struct pgpath *choose_path_in_pg(struct multipath *m,
3502da1610aSMike Snitzer 					struct priority_group *pg,
35102ab823fSKiyoshi Ueda 					size_t nr_bytes)
3521da177e4SLinus Torvalds {
3532da1610aSMike Snitzer 	unsigned long flags;
354c922d5f7SJosef "Jeff" Sipek 	struct dm_path *path;
3552da1610aSMike Snitzer 	struct pgpath *pgpath;
3561da177e4SLinus Torvalds 
35790a4323cSMike Snitzer 	path = pg->ps.type->select_path(&pg->ps, nr_bytes);
3581da177e4SLinus Torvalds 	if (!path)
3592da1610aSMike Snitzer 		return ERR_PTR(-ENXIO);
3601da177e4SLinus Torvalds 
3612da1610aSMike Snitzer 	pgpath = path_to_pgpath(path);
3621da177e4SLinus Torvalds 
3632da1610aSMike Snitzer 	if (unlikely(lockless_dereference(m->current_pg) != pg)) {
3642da1610aSMike Snitzer 		/* Only update current_pgpath if pg changed */
3652da1610aSMike Snitzer 		spin_lock_irqsave(&m->lock, flags);
3662da1610aSMike Snitzer 		m->current_pgpath = pgpath;
3672da1610aSMike Snitzer 		__switch_pg(m, pg);
3682da1610aSMike Snitzer 		spin_unlock_irqrestore(&m->lock, flags);
3691da177e4SLinus Torvalds 	}
3701da177e4SLinus Torvalds 
3712da1610aSMike Snitzer 	return pgpath;
3722da1610aSMike Snitzer }
3732da1610aSMike Snitzer 
3742da1610aSMike Snitzer static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes)
3751da177e4SLinus Torvalds {
3762da1610aSMike Snitzer 	unsigned long flags;
3771da177e4SLinus Torvalds 	struct priority_group *pg;
3782da1610aSMike Snitzer 	struct pgpath *pgpath;
379d19a55ccSMike Snitzer 	unsigned bypassed = 1;
3801da177e4SLinus Torvalds 
38191e968aaSMike Snitzer 	if (!atomic_read(&m->nr_valid_paths)) {
382518257b1SMike Snitzer 		clear_bit(MPATHF_QUEUE_IO, &m->flags);
3831da177e4SLinus Torvalds 		goto failed;
3841f271972SBenjamin Marzinski 	}
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	/* Were we instructed to switch PG? */
3872da1610aSMike Snitzer 	if (lockless_dereference(m->next_pg)) {
3882da1610aSMike Snitzer 		spin_lock_irqsave(&m->lock, flags);
3891da177e4SLinus Torvalds 		pg = m->next_pg;
3902da1610aSMike Snitzer 		if (!pg) {
3912da1610aSMike Snitzer 			spin_unlock_irqrestore(&m->lock, flags);
3922da1610aSMike Snitzer 			goto check_current_pg;
3932da1610aSMike Snitzer 		}
3941da177e4SLinus Torvalds 		m->next_pg = NULL;
3952da1610aSMike Snitzer 		spin_unlock_irqrestore(&m->lock, flags);
3962da1610aSMike Snitzer 		pgpath = choose_path_in_pg(m, pg, nr_bytes);
3972da1610aSMike Snitzer 		if (!IS_ERR_OR_NULL(pgpath))
3982da1610aSMike Snitzer 			return pgpath;
3991da177e4SLinus Torvalds 	}
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	/* Don't change PG until it has no remaining paths */
4022da1610aSMike Snitzer check_current_pg:
4032da1610aSMike Snitzer 	pg = lockless_dereference(m->current_pg);
4042da1610aSMike Snitzer 	if (pg) {
4052da1610aSMike Snitzer 		pgpath = choose_path_in_pg(m, pg, nr_bytes);
4062da1610aSMike Snitzer 		if (!IS_ERR_OR_NULL(pgpath))
4072da1610aSMike Snitzer 			return pgpath;
4082da1610aSMike Snitzer 	}
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	/*
4111da177e4SLinus Torvalds 	 * Loop through priority groups until we find a valid path.
4121da177e4SLinus Torvalds 	 * First time we skip PGs marked 'bypassed'.
413f220fd4eSMike Christie 	 * Second time we only try the ones we skipped, but set
414f220fd4eSMike Christie 	 * pg_init_delay_retry so we do not hammer controllers.
4151da177e4SLinus Torvalds 	 */
4161da177e4SLinus Torvalds 	do {
4171da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
418d19a55ccSMike Snitzer 			if (pg->bypassed == !!bypassed)
4191da177e4SLinus Torvalds 				continue;
4202da1610aSMike Snitzer 			pgpath = choose_path_in_pg(m, pg, nr_bytes);
4212da1610aSMike Snitzer 			if (!IS_ERR_OR_NULL(pgpath)) {
422f220fd4eSMike Christie 				if (!bypassed)
423518257b1SMike Snitzer 					set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
4242da1610aSMike Snitzer 				return pgpath;
4251da177e4SLinus Torvalds 			}
426f220fd4eSMike Christie 		}
4271da177e4SLinus Torvalds 	} while (bypassed--);
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds failed:
4302da1610aSMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
4311da177e4SLinus Torvalds 	m->current_pgpath = NULL;
4321da177e4SLinus Torvalds 	m->current_pg = NULL;
4332da1610aSMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
4342da1610aSMike Snitzer 
4352da1610aSMike Snitzer 	return NULL;
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
43845e15720SKiyoshi Ueda /*
43945e15720SKiyoshi Ueda  * Check whether bios must be queued in the device-mapper core rather
44045e15720SKiyoshi Ueda  * than here in the target.
44145e15720SKiyoshi Ueda  *
44245e15720SKiyoshi Ueda  * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
44345e15720SKiyoshi Ueda  * same value then we are not between multipath_presuspend()
44445e15720SKiyoshi Ueda  * and multipath_resume() calls and we have no need to check
44545e15720SKiyoshi Ueda  * for the DMF_NOFLUSH_SUSPENDING flag.
44645e15720SKiyoshi Ueda  */
44776e33fe4SMike Snitzer static bool __must_push_back(struct multipath *m)
44876e33fe4SMike Snitzer {
44976e33fe4SMike Snitzer 	return ((test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) !=
45076e33fe4SMike Snitzer 		 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)) &&
45176e33fe4SMike Snitzer 		dm_noflush_suspending(m->ti));
45276e33fe4SMike Snitzer }
45376e33fe4SMike Snitzer 
45476e33fe4SMike Snitzer static bool must_push_back_rq(struct multipath *m)
45545e15720SKiyoshi Ueda {
4561814f2e3SMike Snitzer 	bool r;
4571814f2e3SMike Snitzer 	unsigned long flags;
4581814f2e3SMike Snitzer 
4591814f2e3SMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
4601814f2e3SMike Snitzer 	r = (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) ||
46176e33fe4SMike Snitzer 	     __must_push_back(m));
4621814f2e3SMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
4631814f2e3SMike Snitzer 
4641814f2e3SMike Snitzer 	return r;
46576e33fe4SMike Snitzer }
46676e33fe4SMike Snitzer 
46776e33fe4SMike Snitzer static bool must_push_back_bio(struct multipath *m)
46876e33fe4SMike Snitzer {
4691814f2e3SMike Snitzer 	bool r;
4701814f2e3SMike Snitzer 	unsigned long flags;
4711814f2e3SMike Snitzer 
4721814f2e3SMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
4731814f2e3SMike Snitzer 	r = __must_push_back(m);
4741814f2e3SMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
4751814f2e3SMike Snitzer 
4761814f2e3SMike Snitzer 	return r;
47745e15720SKiyoshi Ueda }
47845e15720SKiyoshi Ueda 
47936fcffccSHannes Reinecke /*
48076e33fe4SMike Snitzer  * Map cloned requests (request-based multipath)
48136fcffccSHannes Reinecke  */
482eb8db831SChristoph Hellwig static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
483e5863d9aSMike Snitzer 				   union map_info *map_context,
484eb8db831SChristoph Hellwig 				   struct request **__clone)
4851da177e4SLinus Torvalds {
4867943bd6dSMike Snitzer 	struct multipath *m = ti->private;
487eb8db831SChristoph Hellwig 	size_t nr_bytes = blk_rq_bytes(rq);
4881da177e4SLinus Torvalds 	struct pgpath *pgpath;
489f40c67f0SKiyoshi Ueda 	struct block_device *bdev;
490eb8db831SChristoph Hellwig 	struct dm_mpath_io *mpio = get_mpio(map_context);
491eb8db831SChristoph Hellwig 	struct request *clone;
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	/* Do we need to select a new pgpath? */
4942da1610aSMike Snitzer 	pgpath = lockless_dereference(m->current_pgpath);
4952da1610aSMike Snitzer 	if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
4962da1610aSMike Snitzer 		pgpath = choose_pgpath(m, nr_bytes);
4971da177e4SLinus Torvalds 
4989bf59a61SMike Snitzer 	if (!pgpath) {
499b88efd43SMike Snitzer 		if (must_push_back_rq(m))
500b88efd43SMike Snitzer 			return DM_MAPIO_DELAY_REQUEUE;
501b88efd43SMike Snitzer 		return -EIO;	/* Failed */
502518257b1SMike Snitzer 	} else if (test_bit(MPATHF_QUEUE_IO, &m->flags) ||
503518257b1SMike Snitzer 		   test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
5042da1610aSMike Snitzer 		pg_init_all_paths(m);
505*06eb061fSBart Van Assche 		return DM_MAPIO_REQUEUE;
5069bf59a61SMike Snitzer 	}
5076afbc01dSMike Snitzer 
508eb8db831SChristoph Hellwig 	memset(mpio, 0, sizeof(*mpio));
509e8099177SHannes Reinecke 	mpio->pgpath = pgpath;
510e8099177SHannes Reinecke 	mpio->nr_bytes = nr_bytes;
5112eb6e1e3SKeith Busch 
5122eb6e1e3SKeith Busch 	bdev = pgpath->path.dev->bdev;
5132eb6e1e3SKeith Busch 
514eb8db831SChristoph Hellwig 	clone = blk_get_request(bdev_get_queue(bdev),
515eb8db831SChristoph Hellwig 			rq->cmd_flags | REQ_NOMERGE,
516eb8db831SChristoph Hellwig 			GFP_ATOMIC);
5176599c84eSBart Van Assche 	if (IS_ERR(clone)) {
5186599c84eSBart Van Assche 		/* EBUSY, ENODEV or EWOULDBLOCK: requeue */
519*06eb061fSBart Van Assche 		return DM_MAPIO_DELAY_REQUEUE;
5204c6dd53dSMike Snitzer 	}
5216599c84eSBart Van Assche 	clone->bio = clone->biotail = NULL;
5226599c84eSBart Van Assche 	clone->rq_disk = bdev->bd_disk;
5236599c84eSBart Van Assche 	clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
5246599c84eSBart Van Assche 	*__clone = clone;
5252eb6e1e3SKeith Busch 
526e8099177SHannes Reinecke 	if (pgpath->pg->ps.type->start_io)
527e8099177SHannes Reinecke 		pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
528e8099177SHannes Reinecke 					      &pgpath->path,
529e8099177SHannes Reinecke 					      nr_bytes);
5302eb6e1e3SKeith Busch 	return DM_MAPIO_REMAPPED;
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
533e5863d9aSMike Snitzer static void multipath_release_clone(struct request *clone)
534e5863d9aSMike Snitzer {
535eb8db831SChristoph Hellwig 	blk_put_request(clone);
536e5863d9aSMike Snitzer }
537e5863d9aSMike Snitzer 
5381da177e4SLinus Torvalds /*
53976e33fe4SMike Snitzer  * Map cloned bios (bio-based multipath)
54076e33fe4SMike Snitzer  */
54176e33fe4SMike Snitzer static int __multipath_map_bio(struct multipath *m, struct bio *bio, struct dm_mpath_io *mpio)
54276e33fe4SMike Snitzer {
54376e33fe4SMike Snitzer 	size_t nr_bytes = bio->bi_iter.bi_size;
54476e33fe4SMike Snitzer 	struct pgpath *pgpath;
54576e33fe4SMike Snitzer 	unsigned long flags;
54676e33fe4SMike Snitzer 	bool queue_io;
54776e33fe4SMike Snitzer 
54876e33fe4SMike Snitzer 	/* Do we need to select a new pgpath? */
54976e33fe4SMike Snitzer 	pgpath = lockless_dereference(m->current_pgpath);
55076e33fe4SMike Snitzer 	queue_io = test_bit(MPATHF_QUEUE_IO, &m->flags);
55176e33fe4SMike Snitzer 	if (!pgpath || !queue_io)
55276e33fe4SMike Snitzer 		pgpath = choose_pgpath(m, nr_bytes);
55376e33fe4SMike Snitzer 
55476e33fe4SMike Snitzer 	if ((pgpath && queue_io) ||
55576e33fe4SMike Snitzer 	    (!pgpath && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) {
55676e33fe4SMike Snitzer 		/* Queue for the daemon to resubmit */
55776e33fe4SMike Snitzer 		spin_lock_irqsave(&m->lock, flags);
55876e33fe4SMike Snitzer 		bio_list_add(&m->queued_bios, bio);
55976e33fe4SMike Snitzer 		spin_unlock_irqrestore(&m->lock, flags);
56076e33fe4SMike Snitzer 		/* PG_INIT_REQUIRED cannot be set without QUEUE_IO */
56176e33fe4SMike Snitzer 		if (queue_io || test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
56276e33fe4SMike Snitzer 			pg_init_all_paths(m);
56376e33fe4SMike Snitzer 		else if (!queue_io)
56476e33fe4SMike Snitzer 			queue_work(kmultipathd, &m->process_queued_bios);
56576e33fe4SMike Snitzer 		return DM_MAPIO_SUBMITTED;
56676e33fe4SMike Snitzer 	}
56776e33fe4SMike Snitzer 
56876e33fe4SMike Snitzer 	if (!pgpath) {
56976e33fe4SMike Snitzer 		if (!must_push_back_bio(m))
57076e33fe4SMike Snitzer 			return -EIO;
57176e33fe4SMike Snitzer 		return DM_MAPIO_REQUEUE;
57276e33fe4SMike Snitzer 	}
57376e33fe4SMike Snitzer 
57476e33fe4SMike Snitzer 	mpio->pgpath = pgpath;
57576e33fe4SMike Snitzer 	mpio->nr_bytes = nr_bytes;
57676e33fe4SMike Snitzer 
57776e33fe4SMike Snitzer 	bio->bi_error = 0;
57876e33fe4SMike Snitzer 	bio->bi_bdev = pgpath->path.dev->bdev;
5791eff9d32SJens Axboe 	bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
58076e33fe4SMike Snitzer 
58176e33fe4SMike Snitzer 	if (pgpath->pg->ps.type->start_io)
58276e33fe4SMike Snitzer 		pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
58376e33fe4SMike Snitzer 					      &pgpath->path,
58476e33fe4SMike Snitzer 					      nr_bytes);
58576e33fe4SMike Snitzer 	return DM_MAPIO_REMAPPED;
58676e33fe4SMike Snitzer }
58776e33fe4SMike Snitzer 
58876e33fe4SMike Snitzer static int multipath_map_bio(struct dm_target *ti, struct bio *bio)
58976e33fe4SMike Snitzer {
59076e33fe4SMike Snitzer 	struct multipath *m = ti->private;
591bf661be1SMike Snitzer 	struct dm_mpath_io *mpio = NULL;
592bf661be1SMike Snitzer 
593bf661be1SMike Snitzer 	multipath_init_per_bio_data(bio, &mpio, NULL);
59476e33fe4SMike Snitzer 
59576e33fe4SMike Snitzer 	return __multipath_map_bio(m, bio, mpio);
59676e33fe4SMike Snitzer }
59776e33fe4SMike Snitzer 
5987e48c768SMike Snitzer static void process_queued_io_list(struct multipath *m)
59976e33fe4SMike Snitzer {
6007e48c768SMike Snitzer 	if (m->queue_mode == DM_TYPE_MQ_REQUEST_BASED)
6017e48c768SMike Snitzer 		dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table));
6027e48c768SMike Snitzer 	else if (m->queue_mode == DM_TYPE_BIO_BASED)
60376e33fe4SMike Snitzer 		queue_work(kmultipathd, &m->process_queued_bios);
60476e33fe4SMike Snitzer }
60576e33fe4SMike Snitzer 
60676e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work)
60776e33fe4SMike Snitzer {
60876e33fe4SMike Snitzer 	int r;
60976e33fe4SMike Snitzer 	unsigned long flags;
61076e33fe4SMike Snitzer 	struct bio *bio;
61176e33fe4SMike Snitzer 	struct bio_list bios;
61276e33fe4SMike Snitzer 	struct blk_plug plug;
61376e33fe4SMike Snitzer 	struct multipath *m =
61476e33fe4SMike Snitzer 		container_of(work, struct multipath, process_queued_bios);
61576e33fe4SMike Snitzer 
61676e33fe4SMike Snitzer 	bio_list_init(&bios);
61776e33fe4SMike Snitzer 
61876e33fe4SMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
61976e33fe4SMike Snitzer 
62076e33fe4SMike Snitzer 	if (bio_list_empty(&m->queued_bios)) {
62176e33fe4SMike Snitzer 		spin_unlock_irqrestore(&m->lock, flags);
62276e33fe4SMike Snitzer 		return;
62376e33fe4SMike Snitzer 	}
62476e33fe4SMike Snitzer 
62576e33fe4SMike Snitzer 	bio_list_merge(&bios, &m->queued_bios);
62676e33fe4SMike Snitzer 	bio_list_init(&m->queued_bios);
62776e33fe4SMike Snitzer 
62876e33fe4SMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
62976e33fe4SMike Snitzer 
63076e33fe4SMike Snitzer 	blk_start_plug(&plug);
63176e33fe4SMike Snitzer 	while ((bio = bio_list_pop(&bios))) {
63276e33fe4SMike Snitzer 		r = __multipath_map_bio(m, bio, get_mpio_from_bio(bio));
63376e33fe4SMike Snitzer 		if (r < 0 || r == DM_MAPIO_REQUEUE) {
63476e33fe4SMike Snitzer 			bio->bi_error = r;
63576e33fe4SMike Snitzer 			bio_endio(bio);
63676e33fe4SMike Snitzer 		} else if (r == DM_MAPIO_REMAPPED)
63776e33fe4SMike Snitzer 			generic_make_request(bio);
63876e33fe4SMike Snitzer 	}
63976e33fe4SMike Snitzer 	blk_finish_plug(&plug);
64076e33fe4SMike Snitzer }
64176e33fe4SMike Snitzer 
64276e33fe4SMike Snitzer /*
6431da177e4SLinus Torvalds  * If we run out of usable paths, should we queue I/O or error it?
6441da177e4SLinus Torvalds  */
645be7d31ccSMike Snitzer static int queue_if_no_path(struct multipath *m, bool queue_if_no_path,
646be7d31ccSMike Snitzer 			    bool save_old_value)
6471da177e4SLinus Torvalds {
6481da177e4SLinus Torvalds 	unsigned long flags;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
6511da177e4SLinus Torvalds 
652518257b1SMike Snitzer 	if (save_old_value) {
653518257b1SMike Snitzer 		if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
654518257b1SMike Snitzer 			set_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
655485ef69eSAlasdair G Kergon 		else
656518257b1SMike Snitzer 			clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
657518257b1SMike Snitzer 	} else {
658518257b1SMike Snitzer 		if (queue_if_no_path)
659518257b1SMike Snitzer 			set_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
660518257b1SMike Snitzer 		else
661518257b1SMike Snitzer 			clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
662518257b1SMike Snitzer 	}
663518257b1SMike Snitzer 	if (queue_if_no_path)
664518257b1SMike Snitzer 		set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
665518257b1SMike Snitzer 	else
666518257b1SMike Snitzer 		clear_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
667518257b1SMike Snitzer 
6681da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
6691da177e4SLinus Torvalds 
67076e33fe4SMike Snitzer 	if (!queue_if_no_path) {
67163d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
6727e48c768SMike Snitzer 		process_queued_io_list(m);
67376e33fe4SMike Snitzer 	}
67463d832c3SHannes Reinecke 
6751da177e4SLinus Torvalds 	return 0;
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds /*
6791da177e4SLinus Torvalds  * An event is triggered whenever a path is taken out of use.
6801da177e4SLinus Torvalds  * Includes path failure and PG bypass.
6811da177e4SLinus Torvalds  */
682c4028958SDavid Howells static void trigger_event(struct work_struct *work)
6831da177e4SLinus Torvalds {
684c4028958SDavid Howells 	struct multipath *m =
685c4028958SDavid Howells 		container_of(work, struct multipath, trigger_event);
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	dm_table_event(m->ti->table);
6881da177e4SLinus Torvalds }
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds /*-----------------------------------------------------------------
6911da177e4SLinus Torvalds  * Constructor/argument parsing:
6921da177e4SLinus Torvalds  * <#multipath feature args> [<arg>]*
6931da177e4SLinus Torvalds  * <#hw_handler args> [hw_handler [<arg>]*]
6941da177e4SLinus Torvalds  * <#priority groups>
6951da177e4SLinus Torvalds  * <initial priority group>
6961da177e4SLinus Torvalds  *     [<selector> <#selector args> [<arg>]*
6971da177e4SLinus Torvalds  *      <#paths> <#per-path selector args>
6981da177e4SLinus Torvalds  *         [<path> [<arg>]* ]+ ]+
6991da177e4SLinus Torvalds  *---------------------------------------------------------------*/
700498f0103SMike Snitzer static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
7011da177e4SLinus Torvalds 			       struct dm_target *ti)
7021da177e4SLinus Torvalds {
7031da177e4SLinus Torvalds 	int r;
7041da177e4SLinus Torvalds 	struct path_selector_type *pst;
7051da177e4SLinus Torvalds 	unsigned ps_argc;
7061da177e4SLinus Torvalds 
707498f0103SMike Snitzer 	static struct dm_arg _args[] = {
70872d94861SAlasdair G Kergon 		{0, 1024, "invalid number of path selector args"},
7091da177e4SLinus Torvalds 	};
7101da177e4SLinus Torvalds 
711498f0103SMike Snitzer 	pst = dm_get_path_selector(dm_shift_arg(as));
7121da177e4SLinus Torvalds 	if (!pst) {
71372d94861SAlasdair G Kergon 		ti->error = "unknown path selector type";
7141da177e4SLinus Torvalds 		return -EINVAL;
7151da177e4SLinus Torvalds 	}
7161da177e4SLinus Torvalds 
717498f0103SMike Snitzer 	r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
718371b2e34SMikulas Patocka 	if (r) {
719371b2e34SMikulas Patocka 		dm_put_path_selector(pst);
7201da177e4SLinus Torvalds 		return -EINVAL;
721371b2e34SMikulas Patocka 	}
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	r = pst->create(&pg->ps, ps_argc, as->argv);
7241da177e4SLinus Torvalds 	if (r) {
7251da177e4SLinus Torvalds 		dm_put_path_selector(pst);
72672d94861SAlasdair G Kergon 		ti->error = "path selector constructor failed";
7271da177e4SLinus Torvalds 		return r;
7281da177e4SLinus Torvalds 	}
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	pg->ps.type = pst;
731498f0103SMike Snitzer 	dm_consume_args(as, ps_argc);
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	return 0;
7341da177e4SLinus Torvalds }
7351da177e4SLinus Torvalds 
736498f0103SMike Snitzer static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
7371da177e4SLinus Torvalds 			       struct dm_target *ti)
7381da177e4SLinus Torvalds {
7391da177e4SLinus Torvalds 	int r;
7401da177e4SLinus Torvalds 	struct pgpath *p;
741ae11b1b3SHannes Reinecke 	struct multipath *m = ti->private;
742a58a935dSMike Snitzer 	struct request_queue *q = NULL;
743a58a935dSMike Snitzer 	const char *attached_handler_name;
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 	/* we need at least a path arg */
7461da177e4SLinus Torvalds 	if (as->argc < 1) {
74772d94861SAlasdair G Kergon 		ti->error = "no device given";
74801460f35SBenjamin Marzinski 		return ERR_PTR(-EINVAL);
7491da177e4SLinus Torvalds 	}
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	p = alloc_pgpath();
7521da177e4SLinus Torvalds 	if (!p)
75301460f35SBenjamin Marzinski 		return ERR_PTR(-ENOMEM);
7541da177e4SLinus Torvalds 
755498f0103SMike Snitzer 	r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
7568215d6ecSNikanth Karthikesan 			  &p->path.dev);
7571da177e4SLinus Torvalds 	if (r) {
75872d94861SAlasdair G Kergon 		ti->error = "error getting device";
7591da177e4SLinus Torvalds 		goto bad;
7601da177e4SLinus Torvalds 	}
7611da177e4SLinus Torvalds 
762518257b1SMike Snitzer 	if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) || m->hw_handler_name)
763a58a935dSMike Snitzer 		q = bdev_get_queue(p->path.dev->bdev);
764a0cf7ea9SHannes Reinecke 
765518257b1SMike Snitzer 	if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) {
7661bab0de0SChristoph Hellwig retain:
767a58a935dSMike Snitzer 		attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
768a58a935dSMike Snitzer 		if (attached_handler_name) {
769a58a935dSMike Snitzer 			/*
77054cd640dStang.junhui 			 * Clear any hw_handler_params associated with a
77154cd640dStang.junhui 			 * handler that isn't already attached.
77254cd640dStang.junhui 			 */
77354cd640dStang.junhui 			if (m->hw_handler_name && strcmp(attached_handler_name, m->hw_handler_name)) {
77454cd640dStang.junhui 				kfree(m->hw_handler_params);
77554cd640dStang.junhui 				m->hw_handler_params = NULL;
77654cd640dStang.junhui 			}
77754cd640dStang.junhui 
77854cd640dStang.junhui 			/*
779a58a935dSMike Snitzer 			 * Reset hw_handler_name to match the attached handler
780a58a935dSMike Snitzer 			 *
781a58a935dSMike Snitzer 			 * NB. This modifies the table line to show the actual
782a58a935dSMike Snitzer 			 * handler instead of the original table passed in.
783a58a935dSMike Snitzer 			 */
784a58a935dSMike Snitzer 			kfree(m->hw_handler_name);
785a58a935dSMike Snitzer 			m->hw_handler_name = attached_handler_name;
786a58a935dSMike Snitzer 		}
787a58a935dSMike Snitzer 	}
788a58a935dSMike Snitzer 
789a58a935dSMike Snitzer 	if (m->hw_handler_name) {
790a0cf7ea9SHannes Reinecke 		r = scsi_dh_attach(q, m->hw_handler_name);
791a0cf7ea9SHannes Reinecke 		if (r == -EBUSY) {
7921bab0de0SChristoph Hellwig 			char b[BDEVNAME_SIZE];
793a0cf7ea9SHannes Reinecke 
7941bab0de0SChristoph Hellwig 			printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
7951bab0de0SChristoph Hellwig 				bdevname(p->path.dev->bdev, b));
7961bab0de0SChristoph Hellwig 			goto retain;
7971bab0de0SChristoph Hellwig 		}
798ae11b1b3SHannes Reinecke 		if (r < 0) {
799a0cf7ea9SHannes Reinecke 			ti->error = "error attaching hardware handler";
800ae11b1b3SHannes Reinecke 			dm_put_device(ti, p->path.dev);
801ae11b1b3SHannes Reinecke 			goto bad;
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) {
8072bfd2e13SChandra Seetharaman 				ti->error = "unable to set hardware "
8082bfd2e13SChandra Seetharaman 							"handler parameters";
8092bfd2e13SChandra Seetharaman 				dm_put_device(ti, p->path.dev);
8102bfd2e13SChandra Seetharaman 				goto bad;
8112bfd2e13SChandra Seetharaman 			}
8122bfd2e13SChandra Seetharaman 		}
813ae11b1b3SHannes Reinecke 	}
814ae11b1b3SHannes Reinecke 
8151da177e4SLinus Torvalds 	r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
8161da177e4SLinus Torvalds 	if (r) {
8171da177e4SLinus Torvalds 		dm_put_device(ti, p->path.dev);
8181da177e4SLinus Torvalds 		goto bad;
8191da177e4SLinus Torvalds 	}
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	return p;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds  bad:
8241da177e4SLinus Torvalds 	free_pgpath(p);
82501460f35SBenjamin Marzinski 	return ERR_PTR(r);
8261da177e4SLinus Torvalds }
8271da177e4SLinus Torvalds 
828498f0103SMike Snitzer static struct priority_group *parse_priority_group(struct dm_arg_set *as,
82928f16c20SMicha³ Miros³aw 						   struct multipath *m)
8301da177e4SLinus Torvalds {
831498f0103SMike Snitzer 	static struct dm_arg _args[] = {
83272d94861SAlasdair G Kergon 		{1, 1024, "invalid number of paths"},
83372d94861SAlasdair G Kergon 		{0, 1024, "invalid number of selector args"}
8341da177e4SLinus Torvalds 	};
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	int r;
837498f0103SMike Snitzer 	unsigned i, nr_selector_args, nr_args;
8381da177e4SLinus Torvalds 	struct priority_group *pg;
83928f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	if (as->argc < 2) {
8421da177e4SLinus Torvalds 		as->argc = 0;
84301460f35SBenjamin Marzinski 		ti->error = "not enough priority group arguments";
84401460f35SBenjamin Marzinski 		return ERR_PTR(-EINVAL);
8451da177e4SLinus Torvalds 	}
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	pg = alloc_priority_group();
8481da177e4SLinus Torvalds 	if (!pg) {
84972d94861SAlasdair G Kergon 		ti->error = "couldn't allocate priority group";
85001460f35SBenjamin Marzinski 		return ERR_PTR(-ENOMEM);
8511da177e4SLinus Torvalds 	}
8521da177e4SLinus Torvalds 	pg->m = m;
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	r = parse_path_selector(as, pg, ti);
8551da177e4SLinus Torvalds 	if (r)
8561da177e4SLinus Torvalds 		goto bad;
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 	/*
8591da177e4SLinus Torvalds 	 * read the paths
8601da177e4SLinus Torvalds 	 */
861498f0103SMike Snitzer 	r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
8621da177e4SLinus Torvalds 	if (r)
8631da177e4SLinus Torvalds 		goto bad;
8641da177e4SLinus Torvalds 
865498f0103SMike Snitzer 	r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
8661da177e4SLinus Torvalds 	if (r)
8671da177e4SLinus Torvalds 		goto bad;
8681da177e4SLinus Torvalds 
869498f0103SMike Snitzer 	nr_args = 1 + nr_selector_args;
8701da177e4SLinus Torvalds 	for (i = 0; i < pg->nr_pgpaths; i++) {
8711da177e4SLinus Torvalds 		struct pgpath *pgpath;
872498f0103SMike Snitzer 		struct dm_arg_set path_args;
8731da177e4SLinus Torvalds 
874498f0103SMike Snitzer 		if (as->argc < nr_args) {
875148acff6SMikulas Patocka 			ti->error = "not enough path parameters";
8766bbf79a1SAlasdair G Kergon 			r = -EINVAL;
8771da177e4SLinus Torvalds 			goto bad;
878148acff6SMikulas Patocka 		}
8791da177e4SLinus Torvalds 
880498f0103SMike Snitzer 		path_args.argc = nr_args;
8811da177e4SLinus Torvalds 		path_args.argv = as->argv;
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 		pgpath = parse_path(&path_args, &pg->ps, ti);
88401460f35SBenjamin Marzinski 		if (IS_ERR(pgpath)) {
88501460f35SBenjamin Marzinski 			r = PTR_ERR(pgpath);
8861da177e4SLinus Torvalds 			goto bad;
88701460f35SBenjamin Marzinski 		}
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 		pgpath->pg = pg;
8901da177e4SLinus Torvalds 		list_add_tail(&pgpath->list, &pg->pgpaths);
891498f0103SMike Snitzer 		dm_consume_args(as, nr_args);
8921da177e4SLinus Torvalds 	}
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	return pg;
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds  bad:
8971da177e4SLinus Torvalds 	free_priority_group(pg, ti);
89801460f35SBenjamin Marzinski 	return ERR_PTR(r);
8991da177e4SLinus Torvalds }
9001da177e4SLinus Torvalds 
901498f0103SMike Snitzer static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
9021da177e4SLinus Torvalds {
9031da177e4SLinus Torvalds 	unsigned hw_argc;
9042bfd2e13SChandra Seetharaman 	int ret;
90528f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
9061da177e4SLinus Torvalds 
907498f0103SMike Snitzer 	static struct dm_arg _args[] = {
90872d94861SAlasdair G Kergon 		{0, 1024, "invalid number of hardware handler args"},
9091da177e4SLinus Torvalds 	};
9101da177e4SLinus Torvalds 
911498f0103SMike Snitzer 	if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
9121da177e4SLinus Torvalds 		return -EINVAL;
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	if (!hw_argc)
9151da177e4SLinus Torvalds 		return 0;
9161da177e4SLinus Torvalds 
917e83068a5SMike Snitzer 	if (m->queue_mode == DM_TYPE_BIO_BASED) {
91876e33fe4SMike Snitzer 		dm_consume_args(as, hw_argc);
91976e33fe4SMike Snitzer 		DMERR("bio-based multipath doesn't allow hardware handler args");
92076e33fe4SMike Snitzer 		return 0;
92176e33fe4SMike Snitzer 	}
92276e33fe4SMike Snitzer 
923498f0103SMike Snitzer 	m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
924f97dc421Stang.junhui 	if (!m->hw_handler_name)
925f97dc421Stang.junhui 		return -EINVAL;
92614e98c5cSChandra Seetharaman 
9272bfd2e13SChandra Seetharaman 	if (hw_argc > 1) {
9282bfd2e13SChandra Seetharaman 		char *p;
9292bfd2e13SChandra Seetharaman 		int i, j, len = 4;
9302bfd2e13SChandra Seetharaman 
9312bfd2e13SChandra Seetharaman 		for (i = 0; i <= hw_argc - 2; i++)
9322bfd2e13SChandra Seetharaman 			len += strlen(as->argv[i]) + 1;
9332bfd2e13SChandra Seetharaman 		p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
9342bfd2e13SChandra Seetharaman 		if (!p) {
9352bfd2e13SChandra Seetharaman 			ti->error = "memory allocation failed";
9362bfd2e13SChandra Seetharaman 			ret = -ENOMEM;
9372bfd2e13SChandra Seetharaman 			goto fail;
9382bfd2e13SChandra Seetharaman 		}
9392bfd2e13SChandra Seetharaman 		j = sprintf(p, "%d", hw_argc - 1);
9402bfd2e13SChandra Seetharaman 		for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
9412bfd2e13SChandra Seetharaman 			j = sprintf(p, "%s", as->argv[i]);
9422bfd2e13SChandra Seetharaman 	}
943498f0103SMike Snitzer 	dm_consume_args(as, hw_argc - 1);
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	return 0;
9462bfd2e13SChandra Seetharaman fail:
9472bfd2e13SChandra Seetharaman 	kfree(m->hw_handler_name);
9482bfd2e13SChandra Seetharaman 	m->hw_handler_name = NULL;
9492bfd2e13SChandra Seetharaman 	return ret;
9501da177e4SLinus Torvalds }
9511da177e4SLinus Torvalds 
952498f0103SMike Snitzer static int parse_features(struct dm_arg_set *as, struct multipath *m)
9531da177e4SLinus Torvalds {
9541da177e4SLinus Torvalds 	int r;
9551da177e4SLinus Torvalds 	unsigned argc;
95628f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
957498f0103SMike Snitzer 	const char *arg_name;
9581da177e4SLinus Torvalds 
959498f0103SMike Snitzer 	static struct dm_arg _args[] = {
960e83068a5SMike Snitzer 		{0, 8, "invalid number of feature args"},
961c9e45581SDave Wysochanski 		{1, 50, "pg_init_retries must be between 1 and 50"},
9624e2d19e4SChandra Seetharaman 		{0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
9631da177e4SLinus Torvalds 	};
9641da177e4SLinus Torvalds 
965498f0103SMike Snitzer 	r = dm_read_arg_group(_args, as, &argc, &ti->error);
9661da177e4SLinus Torvalds 	if (r)
9671da177e4SLinus Torvalds 		return -EINVAL;
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	if (!argc)
9701da177e4SLinus Torvalds 		return 0;
9711da177e4SLinus Torvalds 
972c9e45581SDave Wysochanski 	do {
973498f0103SMike Snitzer 		arg_name = dm_shift_arg(as);
974c9e45581SDave Wysochanski 		argc--;
975c9e45581SDave Wysochanski 
976498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "queue_if_no_path")) {
977be7d31ccSMike Snitzer 			r = queue_if_no_path(m, true, false);
978c9e45581SDave Wysochanski 			continue;
9791da177e4SLinus Torvalds 		}
980c9e45581SDave Wysochanski 
981a58a935dSMike Snitzer 		if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
982518257b1SMike Snitzer 			set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
983a58a935dSMike Snitzer 			continue;
984a58a935dSMike Snitzer 		}
985a58a935dSMike Snitzer 
986498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "pg_init_retries") &&
987c9e45581SDave Wysochanski 		    (argc >= 1)) {
988498f0103SMike Snitzer 			r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
989c9e45581SDave Wysochanski 			argc--;
990c9e45581SDave Wysochanski 			continue;
991c9e45581SDave Wysochanski 		}
992c9e45581SDave Wysochanski 
993498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
9944e2d19e4SChandra Seetharaman 		    (argc >= 1)) {
995498f0103SMike Snitzer 			r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
9964e2d19e4SChandra Seetharaman 			argc--;
9974e2d19e4SChandra Seetharaman 			continue;
9984e2d19e4SChandra Seetharaman 		}
9994e2d19e4SChandra Seetharaman 
1000e83068a5SMike Snitzer 		if (!strcasecmp(arg_name, "queue_mode") &&
1001e83068a5SMike Snitzer 		    (argc >= 1)) {
1002e83068a5SMike Snitzer 			const char *queue_mode_name = dm_shift_arg(as);
1003e83068a5SMike Snitzer 
1004e83068a5SMike Snitzer 			if (!strcasecmp(queue_mode_name, "bio"))
1005e83068a5SMike Snitzer 				m->queue_mode = DM_TYPE_BIO_BASED;
1006e83068a5SMike Snitzer 			else if (!strcasecmp(queue_mode_name, "rq"))
1007e83068a5SMike Snitzer 				m->queue_mode = DM_TYPE_REQUEST_BASED;
1008e83068a5SMike Snitzer 			else if (!strcasecmp(queue_mode_name, "mq"))
1009e83068a5SMike Snitzer 				m->queue_mode = DM_TYPE_MQ_REQUEST_BASED;
1010e83068a5SMike Snitzer 			else {
1011e83068a5SMike Snitzer 				ti->error = "Unknown 'queue_mode' requested";
1012e83068a5SMike Snitzer 				r = -EINVAL;
1013e83068a5SMike Snitzer 			}
1014e83068a5SMike Snitzer 			argc--;
1015e83068a5SMike Snitzer 			continue;
1016e83068a5SMike Snitzer 		}
1017e83068a5SMike Snitzer 
1018c9e45581SDave Wysochanski 		ti->error = "Unrecognised multipath feature request";
1019c9e45581SDave Wysochanski 		r = -EINVAL;
1020c9e45581SDave Wysochanski 	} while (argc && !r);
1021c9e45581SDave Wysochanski 
1022c9e45581SDave Wysochanski 	return r;
10231da177e4SLinus Torvalds }
10241da177e4SLinus Torvalds 
1025e83068a5SMike Snitzer static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv)
10261da177e4SLinus Torvalds {
1027498f0103SMike Snitzer 	/* target arguments */
1028498f0103SMike Snitzer 	static struct dm_arg _args[] = {
1029a490a07aSMike Snitzer 		{0, 1024, "invalid number of priority groups"},
1030a490a07aSMike Snitzer 		{0, 1024, "invalid initial priority group number"},
10311da177e4SLinus Torvalds 	};
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 	int r;
10341da177e4SLinus Torvalds 	struct multipath *m;
1035498f0103SMike Snitzer 	struct dm_arg_set as;
10361da177e4SLinus Torvalds 	unsigned pg_count = 0;
10371da177e4SLinus Torvalds 	unsigned next_pg_num;
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	as.argc = argc;
10401da177e4SLinus Torvalds 	as.argv = argv;
10411da177e4SLinus Torvalds 
1042e83068a5SMike Snitzer 	m = alloc_multipath(ti);
10431da177e4SLinus Torvalds 	if (!m) {
104472d94861SAlasdair G Kergon 		ti->error = "can't allocate multipath";
10451da177e4SLinus Torvalds 		return -EINVAL;
10461da177e4SLinus Torvalds 	}
10471da177e4SLinus Torvalds 
104828f16c20SMicha³ Miros³aw 	r = parse_features(&as, m);
10491da177e4SLinus Torvalds 	if (r)
10501da177e4SLinus Torvalds 		goto bad;
10511da177e4SLinus Torvalds 
1052e83068a5SMike Snitzer 	r = alloc_multipath_stage2(ti, m);
1053e83068a5SMike Snitzer 	if (r)
1054e83068a5SMike Snitzer 		goto bad;
1055e83068a5SMike Snitzer 
105628f16c20SMicha³ Miros³aw 	r = parse_hw_handler(&as, m);
10571da177e4SLinus Torvalds 	if (r)
10581da177e4SLinus Torvalds 		goto bad;
10591da177e4SLinus Torvalds 
1060498f0103SMike Snitzer 	r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
10611da177e4SLinus Torvalds 	if (r)
10621da177e4SLinus Torvalds 		goto bad;
10631da177e4SLinus Torvalds 
1064498f0103SMike Snitzer 	r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
10651da177e4SLinus Torvalds 	if (r)
10661da177e4SLinus Torvalds 		goto bad;
10671da177e4SLinus Torvalds 
1068a490a07aSMike Snitzer 	if ((!m->nr_priority_groups && next_pg_num) ||
1069a490a07aSMike Snitzer 	    (m->nr_priority_groups && !next_pg_num)) {
1070a490a07aSMike Snitzer 		ti->error = "invalid initial priority group";
1071a490a07aSMike Snitzer 		r = -EINVAL;
1072a490a07aSMike Snitzer 		goto bad;
1073a490a07aSMike Snitzer 	}
1074a490a07aSMike Snitzer 
10751da177e4SLinus Torvalds 	/* parse the priority groups */
10761da177e4SLinus Torvalds 	while (as.argc) {
10771da177e4SLinus Torvalds 		struct priority_group *pg;
107891e968aaSMike Snitzer 		unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths);
10791da177e4SLinus Torvalds 
108028f16c20SMicha³ Miros³aw 		pg = parse_priority_group(&as, m);
108101460f35SBenjamin Marzinski 		if (IS_ERR(pg)) {
108201460f35SBenjamin Marzinski 			r = PTR_ERR(pg);
10831da177e4SLinus Torvalds 			goto bad;
10841da177e4SLinus Torvalds 		}
10851da177e4SLinus Torvalds 
108691e968aaSMike Snitzer 		nr_valid_paths += pg->nr_pgpaths;
108791e968aaSMike Snitzer 		atomic_set(&m->nr_valid_paths, nr_valid_paths);
108891e968aaSMike Snitzer 
10891da177e4SLinus Torvalds 		list_add_tail(&pg->list, &m->priority_groups);
10901da177e4SLinus Torvalds 		pg_count++;
10911da177e4SLinus Torvalds 		pg->pg_num = pg_count;
10921da177e4SLinus Torvalds 		if (!--next_pg_num)
10931da177e4SLinus Torvalds 			m->next_pg = pg;
10941da177e4SLinus Torvalds 	}
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 	if (pg_count != m->nr_priority_groups) {
109772d94861SAlasdair G Kergon 		ti->error = "priority group count mismatch";
10981da177e4SLinus Torvalds 		r = -EINVAL;
10991da177e4SLinus Torvalds 		goto bad;
11001da177e4SLinus Torvalds 	}
11011da177e4SLinus Torvalds 
110255a62eefSAlasdair G Kergon 	ti->num_flush_bios = 1;
110355a62eefSAlasdair G Kergon 	ti->num_discard_bios = 1;
1104042bcef8SMike Snitzer 	ti->num_write_same_bios = 1;
1105e83068a5SMike Snitzer 	if (m->queue_mode == DM_TYPE_BIO_BASED)
1106bf661be1SMike Snitzer 		ti->per_io_data_size = multipath_per_bio_data_size();
1107eb8db831SChristoph Hellwig 	else
11088637a6bfSMike Snitzer 		ti->per_io_data_size = sizeof(struct dm_mpath_io);
11098627921fSMikulas Patocka 
11101da177e4SLinus Torvalds 	return 0;
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds  bad:
11131da177e4SLinus Torvalds 	free_multipath(m);
11141da177e4SLinus Torvalds 	return r;
11151da177e4SLinus Torvalds }
11161da177e4SLinus Torvalds 
11172bded7bdSKiyoshi Ueda static void multipath_wait_for_pg_init_completion(struct multipath *m)
11182bded7bdSKiyoshi Ueda {
11199f4c3f87SBart Van Assche 	DEFINE_WAIT(wait);
11202bded7bdSKiyoshi Ueda 
11212bded7bdSKiyoshi Ueda 	while (1) {
11229f4c3f87SBart Van Assche 		prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE);
11232bded7bdSKiyoshi Ueda 
112491e968aaSMike Snitzer 		if (!atomic_read(&m->pg_init_in_progress))
11252bded7bdSKiyoshi Ueda 			break;
11262bded7bdSKiyoshi Ueda 
11272bded7bdSKiyoshi Ueda 		io_schedule();
11282bded7bdSKiyoshi Ueda 	}
11299f4c3f87SBart Van Assche 	finish_wait(&m->pg_init_wait, &wait);
11302bded7bdSKiyoshi Ueda }
11312bded7bdSKiyoshi Ueda 
11322bded7bdSKiyoshi Ueda static void flush_multipath_work(struct multipath *m)
11331da177e4SLinus Torvalds {
1134518257b1SMike Snitzer 	set_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1135518257b1SMike Snitzer 	smp_mb__after_atomic();
1136954a73d5SShiva Krishna Merla 
1137bab7cfc7SChandra Seetharaman 	flush_workqueue(kmpath_handlerd);
11382bded7bdSKiyoshi Ueda 	multipath_wait_for_pg_init_completion(m);
1139a044d016SAlasdair G Kergon 	flush_workqueue(kmultipathd);
114043829731STejun Heo 	flush_work(&m->trigger_event);
1141954a73d5SShiva Krishna Merla 
1142518257b1SMike Snitzer 	clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1143518257b1SMike Snitzer 	smp_mb__after_atomic();
11446df400abSKiyoshi Ueda }
11456df400abSKiyoshi Ueda 
11466df400abSKiyoshi Ueda static void multipath_dtr(struct dm_target *ti)
11476df400abSKiyoshi Ueda {
11486df400abSKiyoshi Ueda 	struct multipath *m = ti->private;
11496df400abSKiyoshi Ueda 
11502bded7bdSKiyoshi Ueda 	flush_multipath_work(m);
11511da177e4SLinus Torvalds 	free_multipath(m);
11521da177e4SLinus Torvalds }
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds /*
11551da177e4SLinus Torvalds  * Take a path out of use.
11561da177e4SLinus Torvalds  */
11571da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath)
11581da177e4SLinus Torvalds {
11591da177e4SLinus Torvalds 	unsigned long flags;
11601da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
11631da177e4SLinus Torvalds 
11646680073dSKiyoshi Ueda 	if (!pgpath->is_active)
11651da177e4SLinus Torvalds 		goto out;
11661da177e4SLinus Torvalds 
116772d94861SAlasdair G Kergon 	DMWARN("Failing path %s.", pgpath->path.dev->name);
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
1170be7d31ccSMike Snitzer 	pgpath->is_active = false;
11711da177e4SLinus Torvalds 	pgpath->fail_count++;
11721da177e4SLinus Torvalds 
117391e968aaSMike Snitzer 	atomic_dec(&m->nr_valid_paths);
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	if (pgpath == m->current_pgpath)
11761da177e4SLinus Torvalds 		m->current_pgpath = NULL;
11771da177e4SLinus Torvalds 
1178b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
117991e968aaSMike Snitzer 		       pgpath->path.dev->name, atomic_read(&m->nr_valid_paths));
1180b15546f9SMike Anderson 
1181fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds out:
11841da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds 	return 0;
11871da177e4SLinus Torvalds }
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds /*
11901da177e4SLinus Torvalds  * Reinstate a previously-failed path
11911da177e4SLinus Torvalds  */
11921da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath)
11931da177e4SLinus Torvalds {
119463d832c3SHannes Reinecke 	int r = 0, run_queue = 0;
11951da177e4SLinus Torvalds 	unsigned long flags;
11961da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
119791e968aaSMike Snitzer 	unsigned nr_valid_paths;
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
12001da177e4SLinus Torvalds 
12016680073dSKiyoshi Ueda 	if (pgpath->is_active)
12021da177e4SLinus Torvalds 		goto out;
12031da177e4SLinus Torvalds 
1204ec31f3f7SMike Snitzer 	DMWARN("Reinstating path %s.", pgpath->path.dev->name);
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 	r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
12071da177e4SLinus Torvalds 	if (r)
12081da177e4SLinus Torvalds 		goto out;
12091da177e4SLinus Torvalds 
1210be7d31ccSMike Snitzer 	pgpath->is_active = true;
12111da177e4SLinus Torvalds 
121291e968aaSMike Snitzer 	nr_valid_paths = atomic_inc_return(&m->nr_valid_paths);
121391e968aaSMike Snitzer 	if (nr_valid_paths == 1) {
12141da177e4SLinus Torvalds 		m->current_pgpath = NULL;
121563d832c3SHannes Reinecke 		run_queue = 1;
1216e54f77ddSChandra Seetharaman 	} else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
12174e2d19e4SChandra Seetharaman 		if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
121891e968aaSMike Snitzer 			atomic_inc(&m->pg_init_in_progress);
1219e54f77ddSChandra Seetharaman 	}
12201da177e4SLinus Torvalds 
1221b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
122291e968aaSMike Snitzer 		       pgpath->path.dev->name, nr_valid_paths);
1223b15546f9SMike Anderson 
1224fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds out:
12271da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
122876e33fe4SMike Snitzer 	if (run_queue) {
122963d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
12307e48c768SMike Snitzer 		process_queued_io_list(m);
123176e33fe4SMike Snitzer 	}
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 	return r;
12341da177e4SLinus Torvalds }
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds /*
12371da177e4SLinus Torvalds  * Fail or reinstate all paths that match the provided struct dm_dev.
12381da177e4SLinus Torvalds  */
12391da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev,
12401da177e4SLinus Torvalds 		      action_fn action)
12411da177e4SLinus Torvalds {
124219040c0bSMike Snitzer 	int r = -EINVAL;
12431da177e4SLinus Torvalds 	struct pgpath *pgpath;
12441da177e4SLinus Torvalds 	struct priority_group *pg;
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
12471da177e4SLinus Torvalds 		list_for_each_entry(pgpath, &pg->pgpaths, list) {
12481da177e4SLinus Torvalds 			if (pgpath->path.dev == dev)
12491da177e4SLinus Torvalds 				r = action(pgpath);
12501da177e4SLinus Torvalds 		}
12511da177e4SLinus Torvalds 	}
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 	return r;
12541da177e4SLinus Torvalds }
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds /*
12571da177e4SLinus Torvalds  * Temporarily try to avoid having to use the specified PG
12581da177e4SLinus Torvalds  */
12591da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg,
1260be7d31ccSMike Snitzer 		      bool bypassed)
12611da177e4SLinus Torvalds {
12621da177e4SLinus Torvalds 	unsigned long flags;
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 	pg->bypassed = bypassed;
12671da177e4SLinus Torvalds 	m->current_pgpath = NULL;
12681da177e4SLinus Torvalds 	m->current_pg = NULL;
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
12711da177e4SLinus Torvalds 
1272fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
12731da177e4SLinus Torvalds }
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds /*
12761da177e4SLinus Torvalds  * Switch to using the specified PG from the next I/O that gets mapped
12771da177e4SLinus Torvalds  */
12781da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr)
12791da177e4SLinus Torvalds {
12801da177e4SLinus Torvalds 	struct priority_group *pg;
12811da177e4SLinus Torvalds 	unsigned pgnum;
12821da177e4SLinus Torvalds 	unsigned long flags;
128331998ef1SMikulas Patocka 	char dummy;
12841da177e4SLinus Torvalds 
128531998ef1SMikulas Patocka 	if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1286cc5bd925Stang.junhui 	    !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
12871da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to switch_pg_num");
12881da177e4SLinus Torvalds 		return -EINVAL;
12891da177e4SLinus Torvalds 	}
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
12921da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
1293be7d31ccSMike Snitzer 		pg->bypassed = false;
12941da177e4SLinus Torvalds 		if (--pgnum)
12951da177e4SLinus Torvalds 			continue;
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds 		m->current_pgpath = NULL;
12981da177e4SLinus Torvalds 		m->current_pg = NULL;
12991da177e4SLinus Torvalds 		m->next_pg = pg;
13001da177e4SLinus Torvalds 	}
13011da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
13021da177e4SLinus Torvalds 
1303fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
13041da177e4SLinus Torvalds 	return 0;
13051da177e4SLinus Torvalds }
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds /*
13081da177e4SLinus Torvalds  * Set/clear bypassed status of a PG.
13091da177e4SLinus Torvalds  * PGs are numbered upwards from 1 in the order they were declared.
13101da177e4SLinus Torvalds  */
1311be7d31ccSMike Snitzer static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed)
13121da177e4SLinus Torvalds {
13131da177e4SLinus Torvalds 	struct priority_group *pg;
13141da177e4SLinus Torvalds 	unsigned pgnum;
131531998ef1SMikulas Patocka 	char dummy;
13161da177e4SLinus Torvalds 
131731998ef1SMikulas Patocka 	if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1318cc5bd925Stang.junhui 	    !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
13191da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to bypass_pg");
13201da177e4SLinus Torvalds 		return -EINVAL;
13211da177e4SLinus Torvalds 	}
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
13241da177e4SLinus Torvalds 		if (!--pgnum)
13251da177e4SLinus Torvalds 			break;
13261da177e4SLinus Torvalds 	}
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds 	bypass_pg(m, pg, bypassed);
13291da177e4SLinus Torvalds 	return 0;
13301da177e4SLinus Torvalds }
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds /*
1333c9e45581SDave Wysochanski  * Should we retry pg_init immediately?
1334c9e45581SDave Wysochanski  */
1335be7d31ccSMike Snitzer static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1336c9e45581SDave Wysochanski {
1337c9e45581SDave Wysochanski 	unsigned long flags;
1338be7d31ccSMike Snitzer 	bool limit_reached = false;
1339c9e45581SDave Wysochanski 
1340c9e45581SDave Wysochanski 	spin_lock_irqsave(&m->lock, flags);
1341c9e45581SDave Wysochanski 
134291e968aaSMike Snitzer 	if (atomic_read(&m->pg_init_count) <= m->pg_init_retries &&
134391e968aaSMike Snitzer 	    !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
1344518257b1SMike Snitzer 		set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
1345c9e45581SDave Wysochanski 	else
1346be7d31ccSMike Snitzer 		limit_reached = true;
1347c9e45581SDave Wysochanski 
1348c9e45581SDave Wysochanski 	spin_unlock_irqrestore(&m->lock, flags);
1349c9e45581SDave Wysochanski 
1350c9e45581SDave Wysochanski 	return limit_reached;
1351c9e45581SDave Wysochanski }
1352c9e45581SDave Wysochanski 
13533ae31f6aSChandra Seetharaman static void pg_init_done(void *data, int errors)
1354cfae5c9bSChandra Seetharaman {
135583c0d5d5SMoger, Babu 	struct pgpath *pgpath = data;
1356cfae5c9bSChandra Seetharaman 	struct priority_group *pg = pgpath->pg;
1357cfae5c9bSChandra Seetharaman 	struct multipath *m = pg->m;
1358cfae5c9bSChandra Seetharaman 	unsigned long flags;
1359be7d31ccSMike Snitzer 	bool delay_retry = false;
1360cfae5c9bSChandra Seetharaman 
1361cfae5c9bSChandra Seetharaman 	/* device or driver problems */
1362cfae5c9bSChandra Seetharaman 	switch (errors) {
1363cfae5c9bSChandra Seetharaman 	case SCSI_DH_OK:
1364cfae5c9bSChandra Seetharaman 		break;
1365cfae5c9bSChandra Seetharaman 	case SCSI_DH_NOSYS:
1366cfae5c9bSChandra Seetharaman 		if (!m->hw_handler_name) {
1367cfae5c9bSChandra Seetharaman 			errors = 0;
1368cfae5c9bSChandra Seetharaman 			break;
1369cfae5c9bSChandra Seetharaman 		}
1370f7b934c8SMoger, Babu 		DMERR("Could not failover the device: Handler scsi_dh_%s "
1371f7b934c8SMoger, Babu 		      "Error %d.", m->hw_handler_name, errors);
1372cfae5c9bSChandra Seetharaman 		/*
1373cfae5c9bSChandra Seetharaman 		 * Fail path for now, so we do not ping pong
1374cfae5c9bSChandra Seetharaman 		 */
1375cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1376cfae5c9bSChandra Seetharaman 		break;
1377cfae5c9bSChandra Seetharaman 	case SCSI_DH_DEV_TEMP_BUSY:
1378cfae5c9bSChandra Seetharaman 		/*
1379cfae5c9bSChandra Seetharaman 		 * Probably doing something like FW upgrade on the
1380cfae5c9bSChandra Seetharaman 		 * controller so try the other pg.
1381cfae5c9bSChandra Seetharaman 		 */
1382be7d31ccSMike Snitzer 		bypass_pg(m, pg, true);
1383cfae5c9bSChandra Seetharaman 		break;
1384cfae5c9bSChandra Seetharaman 	case SCSI_DH_RETRY:
13854e2d19e4SChandra Seetharaman 		/* Wait before retrying. */
13864e2d19e4SChandra Seetharaman 		delay_retry = 1;
1387cfae5c9bSChandra Seetharaman 	case SCSI_DH_IMM_RETRY:
1388cfae5c9bSChandra Seetharaman 	case SCSI_DH_RES_TEMP_UNAVAIL:
1389cfae5c9bSChandra Seetharaman 		if (pg_init_limit_reached(m, pgpath))
1390cfae5c9bSChandra Seetharaman 			fail_path(pgpath);
1391cfae5c9bSChandra Seetharaman 		errors = 0;
1392cfae5c9bSChandra Seetharaman 		break;
1393ec31f3f7SMike Snitzer 	case SCSI_DH_DEV_OFFLINED:
1394cfae5c9bSChandra Seetharaman 	default:
1395cfae5c9bSChandra Seetharaman 		/*
1396cfae5c9bSChandra Seetharaman 		 * We probably do not want to fail the path for a device
1397cfae5c9bSChandra Seetharaman 		 * error, but this is what the old dm did. In future
1398cfae5c9bSChandra Seetharaman 		 * patches we can do more advanced handling.
1399cfae5c9bSChandra Seetharaman 		 */
1400cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1401cfae5c9bSChandra Seetharaman 	}
1402cfae5c9bSChandra Seetharaman 
1403cfae5c9bSChandra Seetharaman 	spin_lock_irqsave(&m->lock, flags);
1404cfae5c9bSChandra Seetharaman 	if (errors) {
1405e54f77ddSChandra Seetharaman 		if (pgpath == m->current_pgpath) {
1406cfae5c9bSChandra Seetharaman 			DMERR("Could not failover device. Error %d.", errors);
1407cfae5c9bSChandra Seetharaman 			m->current_pgpath = NULL;
1408cfae5c9bSChandra Seetharaman 			m->current_pg = NULL;
1409e54f77ddSChandra Seetharaman 		}
1410518257b1SMike Snitzer 	} else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
1411be7d31ccSMike Snitzer 		pg->bypassed = false;
1412cfae5c9bSChandra Seetharaman 
141391e968aaSMike Snitzer 	if (atomic_dec_return(&m->pg_init_in_progress) > 0)
1414d0259bf0SKiyoshi Ueda 		/* Activations of other paths are still on going */
1415d0259bf0SKiyoshi Ueda 		goto out;
1416d0259bf0SKiyoshi Ueda 
1417518257b1SMike Snitzer 	if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
1418518257b1SMike Snitzer 		if (delay_retry)
1419518257b1SMike Snitzer 			set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1420518257b1SMike Snitzer 		else
1421518257b1SMike Snitzer 			clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1422518257b1SMike Snitzer 
14233e9f1be1SHannes Reinecke 		if (__pg_init_all_paths(m))
14243e9f1be1SHannes Reinecke 			goto out;
14253e9f1be1SHannes Reinecke 	}
1426518257b1SMike Snitzer 	clear_bit(MPATHF_QUEUE_IO, &m->flags);
1427d0259bf0SKiyoshi Ueda 
14287e48c768SMike Snitzer 	process_queued_io_list(m);
142976e33fe4SMike Snitzer 
14302bded7bdSKiyoshi Ueda 	/*
14312bded7bdSKiyoshi Ueda 	 * Wake up any thread waiting to suspend.
14322bded7bdSKiyoshi Ueda 	 */
14332bded7bdSKiyoshi Ueda 	wake_up(&m->pg_init_wait);
14342bded7bdSKiyoshi Ueda 
1435d0259bf0SKiyoshi Ueda out:
1436cfae5c9bSChandra Seetharaman 	spin_unlock_irqrestore(&m->lock, flags);
1437cfae5c9bSChandra Seetharaman }
1438cfae5c9bSChandra Seetharaman 
1439bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work)
1440bab7cfc7SChandra Seetharaman {
1441e54f77ddSChandra Seetharaman 	struct pgpath *pgpath =
14424e2d19e4SChandra Seetharaman 		container_of(work, struct pgpath, activate_path.work);
1443f10e06b7SMike Snitzer 	struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1444bab7cfc7SChandra Seetharaman 
1445f10e06b7SMike Snitzer 	if (pgpath->is_active && !blk_queue_dying(q))
1446f10e06b7SMike Snitzer 		scsi_dh_activate(q, pg_init_done, pgpath);
14473a017509SHannes Reinecke 	else
14483a017509SHannes Reinecke 		pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
1449bab7cfc7SChandra Seetharaman }
1450bab7cfc7SChandra Seetharaman 
14517e782af5SHannes Reinecke static int noretry_error(int error)
14527e782af5SHannes Reinecke {
14537e782af5SHannes Reinecke 	switch (error) {
14548ff232c1SHannes Reinecke 	case -EBADE:
14558ff232c1SHannes Reinecke 		/*
14568ff232c1SHannes Reinecke 		 * EBADE signals an reservation conflict.
14578ff232c1SHannes Reinecke 		 * We shouldn't fail the path here as we can communicate with
14588ff232c1SHannes Reinecke 		 * the target.  We should failover to the next path, but in
14598ff232c1SHannes Reinecke 		 * doing so we might be causing a ping-pong between paths.
14608ff232c1SHannes Reinecke 		 * So just return the reservation conflict error.
14618ff232c1SHannes Reinecke 		 */
14627e782af5SHannes Reinecke 	case -EOPNOTSUPP:
14637e782af5SHannes Reinecke 	case -EREMOTEIO:
14647e782af5SHannes Reinecke 	case -EILSEQ:
14657e782af5SHannes Reinecke 	case -ENODATA:
1466cc9d3c38SJun'ichi Nomura 	case -ENOSPC:
14677e782af5SHannes Reinecke 		return 1;
14687e782af5SHannes Reinecke 	}
14697e782af5SHannes Reinecke 
14707e782af5SHannes Reinecke 	/* Anything else could be a path failure, so should be retried */
14717e782af5SHannes Reinecke 	return 0;
14727e782af5SHannes Reinecke }
14737e782af5SHannes Reinecke 
14741da177e4SLinus Torvalds /*
14751da177e4SLinus Torvalds  * end_io handling
14761da177e4SLinus Torvalds  */
1477f40c67f0SKiyoshi Ueda static int do_end_io(struct multipath *m, struct request *clone,
1478028867acSAlasdair G Kergon 		     int error, struct dm_mpath_io *mpio)
14791da177e4SLinus Torvalds {
1480f40c67f0SKiyoshi Ueda 	/*
1481f40c67f0SKiyoshi Ueda 	 * We don't queue any clone request inside the multipath target
1482f40c67f0SKiyoshi Ueda 	 * during end I/O handling, since those clone requests don't have
1483f40c67f0SKiyoshi Ueda 	 * bio clones.  If we queue them inside the multipath target,
1484f40c67f0SKiyoshi Ueda 	 * we need to make bio clones, that requires memory allocation.
14854cc96131SMike Snitzer 	 * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests
1486f40c67f0SKiyoshi Ueda 	 *  don't have bio clones.)
1487f40c67f0SKiyoshi Ueda 	 * Instead of queueing the clone request here, we queue the original
1488f40c67f0SKiyoshi Ueda 	 * request into dm core, which will remake a clone request and
1489f40c67f0SKiyoshi Ueda 	 * clone bios for it and resubmit it later.
1490f40c67f0SKiyoshi Ueda 	 */
1491f40c67f0SKiyoshi Ueda 	int r = DM_ENDIO_REQUEUE;
14921da177e4SLinus Torvalds 
1493f40c67f0SKiyoshi Ueda 	if (!error && !clone->errors)
14941da177e4SLinus Torvalds 		return 0;	/* I/O complete */
14951da177e4SLinus Torvalds 
14967eee4ae2SMike Snitzer 	if (noretry_error(error))
1497959eb4e5SMike Snitzer 		return error;
1498959eb4e5SMike Snitzer 
1499cfae5c9bSChandra Seetharaman 	if (mpio->pgpath)
15001da177e4SLinus Torvalds 		fail_path(mpio->pgpath);
15011da177e4SLinus Torvalds 
150291e968aaSMike Snitzer 	if (!atomic_read(&m->nr_valid_paths)) {
1503518257b1SMike Snitzer 		if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
150476e33fe4SMike Snitzer 			if (!must_push_back_rq(m))
1505f40c67f0SKiyoshi Ueda 				r = -EIO;
1506751b2a7dSHannes Reinecke 		}
1507751b2a7dSHannes Reinecke 	}
15081da177e4SLinus Torvalds 
1509f40c67f0SKiyoshi Ueda 	return r;
15101da177e4SLinus Torvalds }
15111da177e4SLinus Torvalds 
1512f40c67f0SKiyoshi Ueda static int multipath_end_io(struct dm_target *ti, struct request *clone,
15131da177e4SLinus Torvalds 			    int error, union map_info *map_context)
15141da177e4SLinus Torvalds {
1515028867acSAlasdair G Kergon 	struct multipath *m = ti->private;
15162eff1924SMike Snitzer 	struct dm_mpath_io *mpio = get_mpio(map_context);
1517a71a261fSWei Yongjun 	struct pgpath *pgpath;
15181da177e4SLinus Torvalds 	struct path_selector *ps;
15191da177e4SLinus Torvalds 	int r;
15201da177e4SLinus Torvalds 
1521466891f9SJun'ichi Nomura 	BUG_ON(!mpio);
1522466891f9SJun'ichi Nomura 
1523f40c67f0SKiyoshi Ueda 	r = do_end_io(m, clone, error, mpio);
1524a71a261fSWei Yongjun 	pgpath = mpio->pgpath;
15251da177e4SLinus Torvalds 	if (pgpath) {
15261da177e4SLinus Torvalds 		ps = &pgpath->pg->ps;
15271da177e4SLinus Torvalds 		if (ps->type->end_io)
152802ab823fSKiyoshi Ueda 			ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
15291da177e4SLinus Torvalds 	}
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 	return r;
15321da177e4SLinus Torvalds }
15331da177e4SLinus Torvalds 
153476e33fe4SMike Snitzer static int do_end_io_bio(struct multipath *m, struct bio *clone,
153576e33fe4SMike Snitzer 			 int error, struct dm_mpath_io *mpio)
153676e33fe4SMike Snitzer {
153776e33fe4SMike Snitzer 	unsigned long flags;
153876e33fe4SMike Snitzer 
153976e33fe4SMike Snitzer 	if (!error)
154076e33fe4SMike Snitzer 		return 0;	/* I/O complete */
154176e33fe4SMike Snitzer 
154276e33fe4SMike Snitzer 	if (noretry_error(error))
154376e33fe4SMike Snitzer 		return error;
154476e33fe4SMike Snitzer 
154576e33fe4SMike Snitzer 	if (mpio->pgpath)
154676e33fe4SMike Snitzer 		fail_path(mpio->pgpath);
154776e33fe4SMike Snitzer 
154876e33fe4SMike Snitzer 	if (!atomic_read(&m->nr_valid_paths)) {
154976e33fe4SMike Snitzer 		if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
155076e33fe4SMike Snitzer 			if (!must_push_back_bio(m))
155176e33fe4SMike Snitzer 				return -EIO;
155276e33fe4SMike Snitzer 			return DM_ENDIO_REQUEUE;
155376e33fe4SMike Snitzer 		}
155476e33fe4SMike Snitzer 	}
155576e33fe4SMike Snitzer 
155676e33fe4SMike Snitzer 	/* Queue for the daemon to resubmit */
1557bf661be1SMike Snitzer 	dm_bio_restore(get_bio_details_from_bio(clone), clone);
155876e33fe4SMike Snitzer 
155976e33fe4SMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
156076e33fe4SMike Snitzer 	bio_list_add(&m->queued_bios, clone);
156176e33fe4SMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
156276e33fe4SMike Snitzer 	if (!test_bit(MPATHF_QUEUE_IO, &m->flags))
156376e33fe4SMike Snitzer 		queue_work(kmultipathd, &m->process_queued_bios);
156476e33fe4SMike Snitzer 
156576e33fe4SMike Snitzer 	return DM_ENDIO_INCOMPLETE;
156676e33fe4SMike Snitzer }
156776e33fe4SMike Snitzer 
156876e33fe4SMike Snitzer static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone, int error)
156976e33fe4SMike Snitzer {
157076e33fe4SMike Snitzer 	struct multipath *m = ti->private;
157176e33fe4SMike Snitzer 	struct dm_mpath_io *mpio = get_mpio_from_bio(clone);
157276e33fe4SMike Snitzer 	struct pgpath *pgpath;
157376e33fe4SMike Snitzer 	struct path_selector *ps;
157476e33fe4SMike Snitzer 	int r;
157576e33fe4SMike Snitzer 
157676e33fe4SMike Snitzer 	BUG_ON(!mpio);
157776e33fe4SMike Snitzer 
157876e33fe4SMike Snitzer 	r = do_end_io_bio(m, clone, error, mpio);
157976e33fe4SMike Snitzer 	pgpath = mpio->pgpath;
158076e33fe4SMike Snitzer 	if (pgpath) {
158176e33fe4SMike Snitzer 		ps = &pgpath->pg->ps;
158276e33fe4SMike Snitzer 		if (ps->type->end_io)
158376e33fe4SMike Snitzer 			ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
158476e33fe4SMike Snitzer 	}
158576e33fe4SMike Snitzer 
158676e33fe4SMike Snitzer 	return r;
158776e33fe4SMike Snitzer }
158876e33fe4SMike Snitzer 
15891da177e4SLinus Torvalds /*
15901da177e4SLinus Torvalds  * Suspend can't complete until all the I/O is processed so if
1591436d4108SAlasdair G Kergon  * the last path fails we must error any remaining I/O.
1592436d4108SAlasdair G Kergon  * Note that if the freeze_bdev fails while suspending, the
1593436d4108SAlasdair G Kergon  * queue_if_no_path state is lost - userspace should reset it.
15941da177e4SLinus Torvalds  */
15951da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti)
15961da177e4SLinus Torvalds {
15977943bd6dSMike Snitzer 	struct multipath *m = ti->private;
15981da177e4SLinus Torvalds 
1599be7d31ccSMike Snitzer 	queue_if_no_path(m, false, true);
16001da177e4SLinus Torvalds }
16011da177e4SLinus Torvalds 
16026df400abSKiyoshi Ueda static void multipath_postsuspend(struct dm_target *ti)
16036df400abSKiyoshi Ueda {
16046380f26fSMike Anderson 	struct multipath *m = ti->private;
16056380f26fSMike Anderson 
16066380f26fSMike Anderson 	mutex_lock(&m->work_mutex);
16072bded7bdSKiyoshi Ueda 	flush_multipath_work(m);
16086380f26fSMike Anderson 	mutex_unlock(&m->work_mutex);
16096df400abSKiyoshi Ueda }
16106df400abSKiyoshi Ueda 
1611436d4108SAlasdair G Kergon /*
1612436d4108SAlasdair G Kergon  * Restore the queue_if_no_path setting.
1613436d4108SAlasdair G Kergon  */
16141da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti)
16151da177e4SLinus Torvalds {
16167943bd6dSMike Snitzer 	struct multipath *m = ti->private;
16171814f2e3SMike Snitzer 	unsigned long flags;
16181da177e4SLinus Torvalds 
16191814f2e3SMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
1620518257b1SMike Snitzer 	if (test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags))
1621518257b1SMike Snitzer 		set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
1622518257b1SMike Snitzer 	else
1623518257b1SMike Snitzer 		clear_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
16241814f2e3SMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
16251da177e4SLinus Torvalds }
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds /*
16281da177e4SLinus Torvalds  * Info output has the following format:
16291da177e4SLinus Torvalds  * num_multipath_feature_args [multipath_feature_args]*
16301da177e4SLinus Torvalds  * num_handler_status_args [handler_status_args]*
16311da177e4SLinus Torvalds  * num_groups init_group_number
16321da177e4SLinus Torvalds  *            [A|D|E num_ps_status_args [ps_status_args]*
16331da177e4SLinus Torvalds  *             num_paths num_selector_args
16341da177e4SLinus Torvalds  *             [path_dev A|F fail_count [selector_args]* ]+ ]+
16351da177e4SLinus Torvalds  *
16361da177e4SLinus Torvalds  * Table output has the following format (identical to the constructor string):
16371da177e4SLinus Torvalds  * num_feature_args [features_args]*
16381da177e4SLinus Torvalds  * num_handler_args hw_handler [hw_handler_args]*
16391da177e4SLinus Torvalds  * num_groups init_group_number
16401da177e4SLinus Torvalds  *     [priority selector-name num_ps_args [ps_args]*
16411da177e4SLinus Torvalds  *      num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
16421da177e4SLinus Torvalds  */
1643fd7c092eSMikulas Patocka static void multipath_status(struct dm_target *ti, status_type_t type,
16441f4e0ff0SAlasdair G Kergon 			     unsigned status_flags, char *result, unsigned maxlen)
16451da177e4SLinus Torvalds {
16461da177e4SLinus Torvalds 	int sz = 0;
16471da177e4SLinus Torvalds 	unsigned long flags;
16487943bd6dSMike Snitzer 	struct multipath *m = ti->private;
16491da177e4SLinus Torvalds 	struct priority_group *pg;
16501da177e4SLinus Torvalds 	struct pgpath *p;
16511da177e4SLinus Torvalds 	unsigned pg_num;
16521da177e4SLinus Torvalds 	char state;
16531da177e4SLinus Torvalds 
16541da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds 	/* Features */
16571da177e4SLinus Torvalds 	if (type == STATUSTYPE_INFO)
165891e968aaSMike Snitzer 		DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags),
165991e968aaSMike Snitzer 		       atomic_read(&m->pg_init_count));
1660c9e45581SDave Wysochanski 	else {
1661518257b1SMike Snitzer 		DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) +
16624e2d19e4SChandra Seetharaman 			      (m->pg_init_retries > 0) * 2 +
1663a58a935dSMike Snitzer 			      (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
1664e83068a5SMike Snitzer 			      test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) +
1665e83068a5SMike Snitzer 			      (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2);
1666e83068a5SMike Snitzer 
1667518257b1SMike Snitzer 		if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
1668c9e45581SDave Wysochanski 			DMEMIT("queue_if_no_path ");
1669c9e45581SDave Wysochanski 		if (m->pg_init_retries)
1670c9e45581SDave Wysochanski 			DMEMIT("pg_init_retries %u ", m->pg_init_retries);
16714e2d19e4SChandra Seetharaman 		if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
16724e2d19e4SChandra Seetharaman 			DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
1673518257b1SMike Snitzer 		if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags))
1674a58a935dSMike Snitzer 			DMEMIT("retain_attached_hw_handler ");
1675e83068a5SMike Snitzer 		if (m->queue_mode != DM_TYPE_REQUEST_BASED) {
1676e83068a5SMike Snitzer 			switch(m->queue_mode) {
1677e83068a5SMike Snitzer 			case DM_TYPE_BIO_BASED:
1678e83068a5SMike Snitzer 				DMEMIT("queue_mode bio ");
1679e83068a5SMike Snitzer 				break;
1680e83068a5SMike Snitzer 			case DM_TYPE_MQ_REQUEST_BASED:
1681e83068a5SMike Snitzer 				DMEMIT("queue_mode mq ");
1682e83068a5SMike Snitzer 				break;
1683e83068a5SMike Snitzer 			}
1684e83068a5SMike Snitzer 		}
1685c9e45581SDave Wysochanski 	}
16861da177e4SLinus Torvalds 
1687cfae5c9bSChandra Seetharaman 	if (!m->hw_handler_name || type == STATUSTYPE_INFO)
16881da177e4SLinus Torvalds 		DMEMIT("0 ");
16891da177e4SLinus Torvalds 	else
1690cfae5c9bSChandra Seetharaman 		DMEMIT("1 %s ", m->hw_handler_name);
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds 	DMEMIT("%u ", m->nr_priority_groups);
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds 	if (m->next_pg)
16951da177e4SLinus Torvalds 		pg_num = m->next_pg->pg_num;
16961da177e4SLinus Torvalds 	else if (m->current_pg)
16971da177e4SLinus Torvalds 		pg_num = m->current_pg->pg_num;
16981da177e4SLinus Torvalds 	else
1699a490a07aSMike Snitzer 		pg_num = (m->nr_priority_groups ? 1 : 0);
17001da177e4SLinus Torvalds 
17011da177e4SLinus Torvalds 	DMEMIT("%u ", pg_num);
17021da177e4SLinus Torvalds 
17031da177e4SLinus Torvalds 	switch (type) {
17041da177e4SLinus Torvalds 	case STATUSTYPE_INFO:
17051da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
17061da177e4SLinus Torvalds 			if (pg->bypassed)
17071da177e4SLinus Torvalds 				state = 'D';	/* Disabled */
17081da177e4SLinus Torvalds 			else if (pg == m->current_pg)
17091da177e4SLinus Torvalds 				state = 'A';	/* Currently Active */
17101da177e4SLinus Torvalds 			else
17111da177e4SLinus Torvalds 				state = 'E';	/* Enabled */
17121da177e4SLinus Torvalds 
17131da177e4SLinus Torvalds 			DMEMIT("%c ", state);
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds 			if (pg->ps.type->status)
17161da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
17171da177e4SLinus Torvalds 							  result + sz,
17181da177e4SLinus Torvalds 							  maxlen - sz);
17191da177e4SLinus Torvalds 			else
17201da177e4SLinus Torvalds 				DMEMIT("0 ");
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
17231da177e4SLinus Torvalds 			       pg->ps.type->info_args);
17241da177e4SLinus Torvalds 
17251da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
17261da177e4SLinus Torvalds 				DMEMIT("%s %s %u ", p->path.dev->name,
17276680073dSKiyoshi Ueda 				       p->is_active ? "A" : "F",
17281da177e4SLinus Torvalds 				       p->fail_count);
17291da177e4SLinus Torvalds 				if (pg->ps.type->status)
17301da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
17311da177e4SLinus Torvalds 					      &p->path, type, result + sz,
17321da177e4SLinus Torvalds 					      maxlen - sz);
17331da177e4SLinus Torvalds 			}
17341da177e4SLinus Torvalds 		}
17351da177e4SLinus Torvalds 		break;
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds 	case STATUSTYPE_TABLE:
17381da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
17391da177e4SLinus Torvalds 			DMEMIT("%s ", pg->ps.type->name);
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds 			if (pg->ps.type->status)
17421da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
17431da177e4SLinus Torvalds 							  result + sz,
17441da177e4SLinus Torvalds 							  maxlen - sz);
17451da177e4SLinus Torvalds 			else
17461da177e4SLinus Torvalds 				DMEMIT("0 ");
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
17491da177e4SLinus Torvalds 			       pg->ps.type->table_args);
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
17521da177e4SLinus Torvalds 				DMEMIT("%s ", p->path.dev->name);
17531da177e4SLinus Torvalds 				if (pg->ps.type->status)
17541da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
17551da177e4SLinus Torvalds 					      &p->path, type, result + sz,
17561da177e4SLinus Torvalds 					      maxlen - sz);
17571da177e4SLinus Torvalds 			}
17581da177e4SLinus Torvalds 		}
17591da177e4SLinus Torvalds 		break;
17601da177e4SLinus Torvalds 	}
17611da177e4SLinus Torvalds 
17621da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
17631da177e4SLinus Torvalds }
17641da177e4SLinus Torvalds 
17651da177e4SLinus Torvalds static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
17661da177e4SLinus Torvalds {
17676380f26fSMike Anderson 	int r = -EINVAL;
17681da177e4SLinus Torvalds 	struct dm_dev *dev;
17697943bd6dSMike Snitzer 	struct multipath *m = ti->private;
17701da177e4SLinus Torvalds 	action_fn action;
17711da177e4SLinus Torvalds 
17726380f26fSMike Anderson 	mutex_lock(&m->work_mutex);
17736380f26fSMike Anderson 
1774c2f3d24bSKiyoshi Ueda 	if (dm_suspended(ti)) {
1775c2f3d24bSKiyoshi Ueda 		r = -EBUSY;
1776c2f3d24bSKiyoshi Ueda 		goto out;
1777c2f3d24bSKiyoshi Ueda 	}
1778c2f3d24bSKiyoshi Ueda 
17791da177e4SLinus Torvalds 	if (argc == 1) {
1780498f0103SMike Snitzer 		if (!strcasecmp(argv[0], "queue_if_no_path")) {
1781be7d31ccSMike Snitzer 			r = queue_if_no_path(m, true, false);
17826380f26fSMike Anderson 			goto out;
1783498f0103SMike Snitzer 		} else if (!strcasecmp(argv[0], "fail_if_no_path")) {
1784be7d31ccSMike Snitzer 			r = queue_if_no_path(m, false, false);
17856380f26fSMike Anderson 			goto out;
17866380f26fSMike Anderson 		}
17871da177e4SLinus Torvalds 	}
17881da177e4SLinus Torvalds 
17896380f26fSMike Anderson 	if (argc != 2) {
1790a356e426SJose Castillo 		DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
17916380f26fSMike Anderson 		goto out;
17926380f26fSMike Anderson 	}
17931da177e4SLinus Torvalds 
1794498f0103SMike Snitzer 	if (!strcasecmp(argv[0], "disable_group")) {
1795be7d31ccSMike Snitzer 		r = bypass_pg_num(m, argv[1], true);
17966380f26fSMike Anderson 		goto out;
1797498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "enable_group")) {
1798be7d31ccSMike Snitzer 		r = bypass_pg_num(m, argv[1], false);
17996380f26fSMike Anderson 		goto out;
1800498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "switch_group")) {
18016380f26fSMike Anderson 		r = switch_pg_num(m, argv[1]);
18026380f26fSMike Anderson 		goto out;
1803498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "reinstate_path"))
18041da177e4SLinus Torvalds 		action = reinstate_path;
1805498f0103SMike Snitzer 	else if (!strcasecmp(argv[0], "fail_path"))
18061da177e4SLinus Torvalds 		action = fail_path;
18076380f26fSMike Anderson 	else {
1808a356e426SJose Castillo 		DMWARN("Unrecognised multipath message received: %s", argv[0]);
18096380f26fSMike Anderson 		goto out;
18106380f26fSMike Anderson 	}
18111da177e4SLinus Torvalds 
18128215d6ecSNikanth Karthikesan 	r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
18131da177e4SLinus Torvalds 	if (r) {
181472d94861SAlasdair G Kergon 		DMWARN("message: error getting device %s",
18151da177e4SLinus Torvalds 		       argv[1]);
18166380f26fSMike Anderson 		goto out;
18171da177e4SLinus Torvalds 	}
18181da177e4SLinus Torvalds 
18191da177e4SLinus Torvalds 	r = action_dev(m, dev, action);
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds 	dm_put_device(ti, dev);
18221da177e4SLinus Torvalds 
18236380f26fSMike Anderson out:
18246380f26fSMike Anderson 	mutex_unlock(&m->work_mutex);
18251da177e4SLinus Torvalds 	return r;
18261da177e4SLinus Torvalds }
18271da177e4SLinus Torvalds 
1828e56f81e0SChristoph Hellwig static int multipath_prepare_ioctl(struct dm_target *ti,
1829e56f81e0SChristoph Hellwig 		struct block_device **bdev, fmode_t *mode)
18309af4aa30SMilan Broz {
183135991652SMikulas Patocka 	struct multipath *m = ti->private;
18322da1610aSMike Snitzer 	struct pgpath *current_pgpath;
183335991652SMikulas Patocka 	int r;
183435991652SMikulas Patocka 
18352da1610aSMike Snitzer 	current_pgpath = lockless_dereference(m->current_pgpath);
18362da1610aSMike Snitzer 	if (!current_pgpath)
18372da1610aSMike Snitzer 		current_pgpath = choose_pgpath(m, 0);
18389af4aa30SMilan Broz 
18392da1610aSMike Snitzer 	if (current_pgpath) {
1840518257b1SMike Snitzer 		if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) {
18412da1610aSMike Snitzer 			*bdev = current_pgpath->path.dev->bdev;
18422da1610aSMike Snitzer 			*mode = current_pgpath->path.dev->mode;
184343e43c9eSJunichi Nomura 			r = 0;
184443e43c9eSJunichi Nomura 		} else {
184543e43c9eSJunichi Nomura 			/* pg_init has not started or completed */
18466c182cd8SHannes Reinecke 			r = -ENOTCONN;
184743e43c9eSJunichi Nomura 		}
184843e43c9eSJunichi Nomura 	} else {
184943e43c9eSJunichi Nomura 		/* No path is available */
1850518257b1SMike Snitzer 		if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
185143e43c9eSJunichi Nomura 			r = -ENOTCONN;
185243e43c9eSJunichi Nomura 		else
18539af4aa30SMilan Broz 			r = -EIO;
185443e43c9eSJunichi Nomura 	}
18559af4aa30SMilan Broz 
18565bbbfdf6SJunichi Nomura 	if (r == -ENOTCONN) {
18572da1610aSMike Snitzer 		if (!lockless_dereference(m->current_pg)) {
18583e9f1be1SHannes Reinecke 			/* Path status changed, redo selection */
18592da1610aSMike Snitzer 			(void) choose_pgpath(m, 0);
18603e9f1be1SHannes Reinecke 		}
1861518257b1SMike Snitzer 		if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
18622da1610aSMike Snitzer 			pg_init_all_paths(m);
186363d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
18647e48c768SMike Snitzer 		process_queued_io_list(m);
18653e9f1be1SHannes Reinecke 	}
186635991652SMikulas Patocka 
1867e56f81e0SChristoph Hellwig 	/*
1868e56f81e0SChristoph Hellwig 	 * Only pass ioctls through if the device sizes match exactly.
1869e56f81e0SChristoph Hellwig 	 */
1870e56f81e0SChristoph Hellwig 	if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
1871e56f81e0SChristoph Hellwig 		return 1;
1872e56f81e0SChristoph Hellwig 	return r;
18739af4aa30SMilan Broz }
18749af4aa30SMilan Broz 
1875af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti,
1876af4874e0SMike Snitzer 				     iterate_devices_callout_fn fn, void *data)
1877af4874e0SMike Snitzer {
1878af4874e0SMike Snitzer 	struct multipath *m = ti->private;
1879af4874e0SMike Snitzer 	struct priority_group *pg;
1880af4874e0SMike Snitzer 	struct pgpath *p;
1881af4874e0SMike Snitzer 	int ret = 0;
1882af4874e0SMike Snitzer 
1883af4874e0SMike Snitzer 	list_for_each_entry(pg, &m->priority_groups, list) {
1884af4874e0SMike Snitzer 		list_for_each_entry(p, &pg->pgpaths, list) {
18855dea271bSMike Snitzer 			ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1886af4874e0SMike Snitzer 			if (ret)
1887af4874e0SMike Snitzer 				goto out;
1888af4874e0SMike Snitzer 		}
1889af4874e0SMike Snitzer 	}
1890af4874e0SMike Snitzer 
1891af4874e0SMike Snitzer out:
1892af4874e0SMike Snitzer 	return ret;
1893af4874e0SMike Snitzer }
1894af4874e0SMike Snitzer 
18959f54cec5SMike Snitzer static int pgpath_busy(struct pgpath *pgpath)
1896f40c67f0SKiyoshi Ueda {
1897f40c67f0SKiyoshi Ueda 	struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1898f40c67f0SKiyoshi Ueda 
189952b09914SMike Snitzer 	return blk_lld_busy(q);
1900f40c67f0SKiyoshi Ueda }
1901f40c67f0SKiyoshi Ueda 
1902f40c67f0SKiyoshi Ueda /*
1903f40c67f0SKiyoshi Ueda  * We return "busy", only when we can map I/Os but underlying devices
1904f40c67f0SKiyoshi Ueda  * are busy (so even if we map I/Os now, the I/Os will wait on
1905f40c67f0SKiyoshi Ueda  * the underlying queue).
1906f40c67f0SKiyoshi Ueda  * In other words, if we want to kill I/Os or queue them inside us
1907f40c67f0SKiyoshi Ueda  * due to map unavailability, we don't return "busy".  Otherwise,
1908f40c67f0SKiyoshi Ueda  * dm core won't give us the I/Os and we can't do what we want.
1909f40c67f0SKiyoshi Ueda  */
1910f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti)
1911f40c67f0SKiyoshi Ueda {
1912be7d31ccSMike Snitzer 	bool busy = false, has_active = false;
1913f40c67f0SKiyoshi Ueda 	struct multipath *m = ti->private;
19142da1610aSMike Snitzer 	struct priority_group *pg, *next_pg;
1915f40c67f0SKiyoshi Ueda 	struct pgpath *pgpath;
1916f40c67f0SKiyoshi Ueda 
1917b88efd43SMike Snitzer 	/* pg_init in progress */
1918b88efd43SMike Snitzer 	if (atomic_read(&m->pg_init_in_progress))
19192da1610aSMike Snitzer 		return true;
19202da1610aSMike Snitzer 
1921b88efd43SMike Snitzer 	/* no paths available, for blk-mq: rely on IO mapping to delay requeue */
1922b88efd43SMike Snitzer 	if (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
1923b88efd43SMike Snitzer 		return (m->queue_mode != DM_TYPE_MQ_REQUEST_BASED);
1924b88efd43SMike Snitzer 
1925f40c67f0SKiyoshi Ueda 	/* Guess which priority_group will be used at next mapping time */
19262da1610aSMike Snitzer 	pg = lockless_dereference(m->current_pg);
19272da1610aSMike Snitzer 	next_pg = lockless_dereference(m->next_pg);
19282da1610aSMike Snitzer 	if (unlikely(!lockless_dereference(m->current_pgpath) && next_pg))
19292da1610aSMike Snitzer 		pg = next_pg;
19302da1610aSMike Snitzer 
19312da1610aSMike Snitzer 	if (!pg) {
1932f40c67f0SKiyoshi Ueda 		/*
1933f40c67f0SKiyoshi Ueda 		 * We don't know which pg will be used at next mapping time.
19342da1610aSMike Snitzer 		 * We don't call choose_pgpath() here to avoid to trigger
1935f40c67f0SKiyoshi Ueda 		 * pg_init just by busy checking.
1936f40c67f0SKiyoshi Ueda 		 * So we don't know whether underlying devices we will be using
1937f40c67f0SKiyoshi Ueda 		 * at next mapping time are busy or not. Just try mapping.
1938f40c67f0SKiyoshi Ueda 		 */
19392da1610aSMike Snitzer 		return busy;
19402da1610aSMike Snitzer 	}
1941f40c67f0SKiyoshi Ueda 
1942f40c67f0SKiyoshi Ueda 	/*
1943f40c67f0SKiyoshi Ueda 	 * If there is one non-busy active path at least, the path selector
1944f40c67f0SKiyoshi Ueda 	 * will be able to select it. So we consider such a pg as not busy.
1945f40c67f0SKiyoshi Ueda 	 */
1946be7d31ccSMike Snitzer 	busy = true;
19472da1610aSMike Snitzer 	list_for_each_entry(pgpath, &pg->pgpaths, list) {
1948f40c67f0SKiyoshi Ueda 		if (pgpath->is_active) {
1949be7d31ccSMike Snitzer 			has_active = true;
19509f54cec5SMike Snitzer 			if (!pgpath_busy(pgpath)) {
1951be7d31ccSMike Snitzer 				busy = false;
1952f40c67f0SKiyoshi Ueda 				break;
1953f40c67f0SKiyoshi Ueda 			}
1954f40c67f0SKiyoshi Ueda 		}
19552da1610aSMike Snitzer 	}
1956f40c67f0SKiyoshi Ueda 
19572da1610aSMike Snitzer 	if (!has_active) {
1958f40c67f0SKiyoshi Ueda 		/*
1959f40c67f0SKiyoshi Ueda 		 * No active path in this pg, so this pg won't be used and
1960f40c67f0SKiyoshi Ueda 		 * the current_pg will be changed at next mapping time.
1961f40c67f0SKiyoshi Ueda 		 * We need to try mapping to determine it.
1962f40c67f0SKiyoshi Ueda 		 */
1963be7d31ccSMike Snitzer 		busy = false;
19642da1610aSMike Snitzer 	}
1965f40c67f0SKiyoshi Ueda 
1966f40c67f0SKiyoshi Ueda 	return busy;
1967f40c67f0SKiyoshi Ueda }
1968f40c67f0SKiyoshi Ueda 
19691da177e4SLinus Torvalds /*-----------------------------------------------------------------
19701da177e4SLinus Torvalds  * Module setup
19711da177e4SLinus Torvalds  *---------------------------------------------------------------*/
19721da177e4SLinus Torvalds static struct target_type multipath_target = {
19731da177e4SLinus Torvalds 	.name = "multipath",
1974e83068a5SMike Snitzer 	.version = {1, 12, 0},
197516f12266SMike Snitzer 	.features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE,
19761da177e4SLinus Torvalds 	.module = THIS_MODULE,
19771da177e4SLinus Torvalds 	.ctr = multipath_ctr,
19781da177e4SLinus Torvalds 	.dtr = multipath_dtr,
1979e5863d9aSMike Snitzer 	.clone_and_map_rq = multipath_clone_and_map,
1980e5863d9aSMike Snitzer 	.release_clone_rq = multipath_release_clone,
1981f40c67f0SKiyoshi Ueda 	.rq_end_io = multipath_end_io,
198276e33fe4SMike Snitzer 	.map = multipath_map_bio,
198376e33fe4SMike Snitzer 	.end_io = multipath_end_io_bio,
198476e33fe4SMike Snitzer 	.presuspend = multipath_presuspend,
198576e33fe4SMike Snitzer 	.postsuspend = multipath_postsuspend,
198676e33fe4SMike Snitzer 	.resume = multipath_resume,
198776e33fe4SMike Snitzer 	.status = multipath_status,
198876e33fe4SMike Snitzer 	.message = multipath_message,
198976e33fe4SMike Snitzer 	.prepare_ioctl = multipath_prepare_ioctl,
199076e33fe4SMike Snitzer 	.iterate_devices = multipath_iterate_devices,
199176e33fe4SMike Snitzer 	.busy = multipath_busy,
199276e33fe4SMike Snitzer };
199376e33fe4SMike Snitzer 
19941da177e4SLinus Torvalds static int __init dm_multipath_init(void)
19951da177e4SLinus Torvalds {
19961da177e4SLinus Torvalds 	int r;
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds 	r = dm_register_target(&multipath_target);
19991da177e4SLinus Torvalds 	if (r < 0) {
200076e33fe4SMike Snitzer 		DMERR("request-based register failed %d", r);
2001ff658e9cSJohannes Thumshirn 		r = -EINVAL;
2002ff658e9cSJohannes Thumshirn 		goto bad_register_target;
20031da177e4SLinus Torvalds 	}
20041da177e4SLinus Torvalds 
20054d4d66abSTejun Heo 	kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
2006c557308eSAlasdair G Kergon 	if (!kmultipathd) {
20070cd33124SAlasdair G Kergon 		DMERR("failed to create workqueue kmpathd");
2008ff658e9cSJohannes Thumshirn 		r = -ENOMEM;
2009ff658e9cSJohannes Thumshirn 		goto bad_alloc_kmultipathd;
2010c557308eSAlasdair G Kergon 	}
2011c557308eSAlasdair G Kergon 
2012bab7cfc7SChandra Seetharaman 	/*
2013bab7cfc7SChandra Seetharaman 	 * A separate workqueue is used to handle the device handlers
2014bab7cfc7SChandra Seetharaman 	 * to avoid overloading existing workqueue. Overloading the
2015bab7cfc7SChandra Seetharaman 	 * old workqueue would also create a bottleneck in the
2016bab7cfc7SChandra Seetharaman 	 * path of the storage hardware device activation.
2017bab7cfc7SChandra Seetharaman 	 */
20184d4d66abSTejun Heo 	kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
20194d4d66abSTejun Heo 						  WQ_MEM_RECLAIM);
2020bab7cfc7SChandra Seetharaman 	if (!kmpath_handlerd) {
2021bab7cfc7SChandra Seetharaman 		DMERR("failed to create workqueue kmpath_handlerd");
2022ff658e9cSJohannes Thumshirn 		r = -ENOMEM;
2023ff658e9cSJohannes Thumshirn 		goto bad_alloc_kmpath_handlerd;
2024bab7cfc7SChandra Seetharaman 	}
2025bab7cfc7SChandra Seetharaman 
2026ff658e9cSJohannes Thumshirn 	return 0;
2027ff658e9cSJohannes Thumshirn 
2028ff658e9cSJohannes Thumshirn bad_alloc_kmpath_handlerd:
2029ff658e9cSJohannes Thumshirn 	destroy_workqueue(kmultipathd);
2030ff658e9cSJohannes Thumshirn bad_alloc_kmultipathd:
2031ff658e9cSJohannes Thumshirn 	dm_unregister_target(&multipath_target);
2032ff658e9cSJohannes Thumshirn bad_register_target:
20331da177e4SLinus Torvalds 	return r;
20341da177e4SLinus Torvalds }
20351da177e4SLinus Torvalds 
20361da177e4SLinus Torvalds static void __exit dm_multipath_exit(void)
20371da177e4SLinus Torvalds {
2038bab7cfc7SChandra Seetharaman 	destroy_workqueue(kmpath_handlerd);
2039c557308eSAlasdair G Kergon 	destroy_workqueue(kmultipathd);
2040c557308eSAlasdair G Kergon 
204110d3bd09SMikulas Patocka 	dm_unregister_target(&multipath_target);
20421da177e4SLinus Torvalds }
20431da177e4SLinus Torvalds 
20441da177e4SLinus Torvalds module_init(dm_multipath_init);
20451da177e4SLinus Torvalds module_exit(dm_multipath_exit);
20461da177e4SLinus Torvalds 
20471da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target");
20481da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
20491da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2050