xref: /openbmc/linux/drivers/md/dm-mpath.c (revision 5307e2ad69ab3b0e0622fdf8b254c1d4565eb924)
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 
937e0d574fSBart Van Assche 	enum dm_queue_mode 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);
11489bfce76SBart Van Assche static void activate_or_offline_path(struct pgpath *pgpath);
11589bfce76SBart Van Assche static void activate_path_work(struct work_struct *work);
11676e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work);
1171da177e4SLinus Torvalds 
118518257b1SMike Snitzer /*-----------------------------------------------
119518257b1SMike Snitzer  * Multipath state flags.
120518257b1SMike Snitzer  *-----------------------------------------------*/
121518257b1SMike Snitzer 
122518257b1SMike Snitzer #define MPATHF_QUEUE_IO 0			/* Must we queue all I/O? */
123518257b1SMike Snitzer #define MPATHF_QUEUE_IF_NO_PATH 1		/* Queue I/O if last path fails? */
124518257b1SMike Snitzer #define MPATHF_SAVED_QUEUE_IF_NO_PATH 2		/* Saved state during suspension */
125518257b1SMike Snitzer #define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3	/* If there's already a hw_handler present, don't change it. */
126518257b1SMike Snitzer #define MPATHF_PG_INIT_DISABLED 4		/* pg_init is not currently allowed */
127518257b1SMike Snitzer #define MPATHF_PG_INIT_REQUIRED 5		/* pg_init needs calling? */
128518257b1SMike Snitzer #define MPATHF_PG_INIT_DELAY_RETRY 6		/* Delay pg_init retry? */
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds /*-----------------------------------------------
1311da177e4SLinus Torvalds  * Allocation routines
1321da177e4SLinus Torvalds  *-----------------------------------------------*/
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds static struct pgpath *alloc_pgpath(void)
1351da177e4SLinus Torvalds {
136e69fae56SMicha³ Miros³aw 	struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
1371da177e4SLinus Torvalds 
138224cb3e9SMike Anderson 	if (pgpath) {
139be7d31ccSMike Snitzer 		pgpath->is_active = true;
14089bfce76SBart Van Assche 		INIT_DELAYED_WORK(&pgpath->activate_path, activate_path_work);
141224cb3e9SMike Anderson 	}
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	return pgpath;
1441da177e4SLinus Torvalds }
1451da177e4SLinus Torvalds 
146028867acSAlasdair G Kergon static void free_pgpath(struct pgpath *pgpath)
1471da177e4SLinus Torvalds {
1481da177e4SLinus Torvalds 	kfree(pgpath);
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds static struct priority_group *alloc_priority_group(void)
1521da177e4SLinus Torvalds {
1531da177e4SLinus Torvalds 	struct priority_group *pg;
1541da177e4SLinus Torvalds 
155e69fae56SMicha³ Miros³aw 	pg = kzalloc(sizeof(*pg), GFP_KERNEL);
1561da177e4SLinus Torvalds 
157e69fae56SMicha³ Miros³aw 	if (pg)
1581da177e4SLinus Torvalds 		INIT_LIST_HEAD(&pg->pgpaths);
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	return pg;
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
1641da177e4SLinus Torvalds {
1651da177e4SLinus Torvalds 	struct pgpath *pgpath, *tmp;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
1681da177e4SLinus Torvalds 		list_del(&pgpath->list);
1691da177e4SLinus Torvalds 		dm_put_device(ti, pgpath->path.dev);
1701da177e4SLinus Torvalds 		free_pgpath(pgpath);
1711da177e4SLinus Torvalds 	}
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds static void free_priority_group(struct priority_group *pg,
1751da177e4SLinus Torvalds 				struct dm_target *ti)
1761da177e4SLinus Torvalds {
1771da177e4SLinus Torvalds 	struct path_selector *ps = &pg->ps;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	if (ps->type) {
1801da177e4SLinus Torvalds 		ps->type->destroy(ps);
1811da177e4SLinus Torvalds 		dm_put_path_selector(ps->type);
1821da177e4SLinus Torvalds 	}
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	free_pgpaths(&pg->pgpaths, ti);
1851da177e4SLinus Torvalds 	kfree(pg);
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
188e83068a5SMike Snitzer static struct multipath *alloc_multipath(struct dm_target *ti)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds 	struct multipath *m;
1911da177e4SLinus Torvalds 
192e69fae56SMicha³ Miros³aw 	m = kzalloc(sizeof(*m), GFP_KERNEL);
1931da177e4SLinus Torvalds 	if (m) {
1941da177e4SLinus Torvalds 		INIT_LIST_HEAD(&m->priority_groups);
1951da177e4SLinus Torvalds 		spin_lock_init(&m->lock);
196518257b1SMike Snitzer 		set_bit(MPATHF_QUEUE_IO, &m->flags);
19791e968aaSMike Snitzer 		atomic_set(&m->nr_valid_paths, 0);
19891e968aaSMike Snitzer 		atomic_set(&m->pg_init_in_progress, 0);
19991e968aaSMike Snitzer 		atomic_set(&m->pg_init_count, 0);
2004e2d19e4SChandra Seetharaman 		m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
201c4028958SDavid Howells 		INIT_WORK(&m->trigger_event, trigger_event);
2022bded7bdSKiyoshi Ueda 		init_waitqueue_head(&m->pg_init_wait);
2036380f26fSMike Anderson 		mutex_init(&m->work_mutex);
2048637a6bfSMike Snitzer 
205e83068a5SMike Snitzer 		m->queue_mode = DM_TYPE_NONE;
206e83068a5SMike Snitzer 
207e83068a5SMike Snitzer 		m->ti = ti;
208e83068a5SMike Snitzer 		ti->private = m;
209e83068a5SMike Snitzer 	}
210e83068a5SMike Snitzer 
211e83068a5SMike Snitzer 	return m;
212e83068a5SMike Snitzer }
213e83068a5SMike Snitzer 
214e83068a5SMike Snitzer static int alloc_multipath_stage2(struct dm_target *ti, struct multipath *m)
215e83068a5SMike Snitzer {
216e83068a5SMike Snitzer 	if (m->queue_mode == DM_TYPE_NONE) {
217e83068a5SMike Snitzer 		/*
218e83068a5SMike Snitzer 		 * Default to request-based.
219e83068a5SMike Snitzer 		 */
220e83068a5SMike Snitzer 		if (dm_use_blk_mq(dm_table_get_md(ti->table)))
221e83068a5SMike Snitzer 			m->queue_mode = DM_TYPE_MQ_REQUEST_BASED;
222e83068a5SMike Snitzer 		else
223e83068a5SMike Snitzer 			m->queue_mode = DM_TYPE_REQUEST_BASED;
224eb8db831SChristoph Hellwig 	} else if (m->queue_mode == DM_TYPE_BIO_BASED) {
22576e33fe4SMike Snitzer 		INIT_WORK(&m->process_queued_bios, process_queued_bios);
22676e33fe4SMike Snitzer 		/*
22776e33fe4SMike Snitzer 		 * bio-based doesn't support any direct scsi_dh management;
22876e33fe4SMike Snitzer 		 * it just discovers if a scsi_dh is attached.
22976e33fe4SMike Snitzer 		 */
23076e33fe4SMike Snitzer 		set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
23176e33fe4SMike Snitzer 	}
23276e33fe4SMike Snitzer 
233e83068a5SMike Snitzer 	dm_table_set_type(ti->table, m->queue_mode);
2341da177e4SLinus Torvalds 
235e83068a5SMike Snitzer 	return 0;
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds static void free_multipath(struct multipath *m)
2391da177e4SLinus Torvalds {
2401da177e4SLinus Torvalds 	struct priority_group *pg, *tmp;
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
2431da177e4SLinus Torvalds 		list_del(&pg->list);
2441da177e4SLinus Torvalds 		free_priority_group(pg, m->ti);
2451da177e4SLinus Torvalds 	}
2461da177e4SLinus Torvalds 
247cfae5c9bSChandra Seetharaman 	kfree(m->hw_handler_name);
2482bfd2e13SChandra Seetharaman 	kfree(m->hw_handler_params);
2491da177e4SLinus Torvalds 	kfree(m);
2501da177e4SLinus Torvalds }
2511da177e4SLinus Torvalds 
2522eff1924SMike Snitzer static struct dm_mpath_io *get_mpio(union map_info *info)
2532eff1924SMike Snitzer {
2542eff1924SMike Snitzer 	return info->ptr;
2552eff1924SMike Snitzer }
2562eff1924SMike Snitzer 
257bf661be1SMike Snitzer static size_t multipath_per_bio_data_size(void)
25876e33fe4SMike Snitzer {
259bf661be1SMike Snitzer 	return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details);
26076e33fe4SMike Snitzer }
26176e33fe4SMike Snitzer 
262bf661be1SMike Snitzer static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio)
263bf661be1SMike Snitzer {
264bf661be1SMike Snitzer 	return dm_per_bio_data(bio, multipath_per_bio_data_size());
265bf661be1SMike Snitzer }
266bf661be1SMike Snitzer 
267bf661be1SMike Snitzer static struct dm_bio_details *get_bio_details_from_bio(struct bio *bio)
268bf661be1SMike Snitzer {
269bf661be1SMike Snitzer 	/* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */
270bf661be1SMike Snitzer 	struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
271bf661be1SMike Snitzer 	void *bio_details = mpio + 1;
272bf661be1SMike Snitzer 
273bf661be1SMike Snitzer 	return bio_details;
274bf661be1SMike Snitzer }
275bf661be1SMike Snitzer 
276bf661be1SMike Snitzer static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p,
277bf661be1SMike Snitzer 					struct dm_bio_details **bio_details_p)
27876e33fe4SMike Snitzer {
27976e33fe4SMike Snitzer 	struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
280bf661be1SMike Snitzer 	struct dm_bio_details *bio_details = get_bio_details_from_bio(bio);
28176e33fe4SMike Snitzer 
28276e33fe4SMike Snitzer 	memset(mpio, 0, sizeof(*mpio));
283bf661be1SMike Snitzer 	memset(bio_details, 0, sizeof(*bio_details));
284bf661be1SMike Snitzer 	dm_bio_record(bio_details, bio);
28576e33fe4SMike Snitzer 
286bf661be1SMike Snitzer 	if (mpio_p)
287bf661be1SMike Snitzer 		*mpio_p = mpio;
288bf661be1SMike Snitzer 	if (bio_details_p)
289bf661be1SMike Snitzer 		*bio_details_p = bio_details;
29076e33fe4SMike Snitzer }
29176e33fe4SMike Snitzer 
2921da177e4SLinus Torvalds /*-----------------------------------------------
2931da177e4SLinus Torvalds  * Path selection
2941da177e4SLinus Torvalds  *-----------------------------------------------*/
2951da177e4SLinus Torvalds 
2963e9f1be1SHannes Reinecke static int __pg_init_all_paths(struct multipath *m)
297fb612642SKiyoshi Ueda {
298fb612642SKiyoshi Ueda 	struct pgpath *pgpath;
2994e2d19e4SChandra Seetharaman 	unsigned long pg_init_delay = 0;
300fb612642SKiyoshi Ueda 
301b194679fSBart Van Assche 	lockdep_assert_held(&m->lock);
302b194679fSBart Van Assche 
30391e968aaSMike Snitzer 	if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
3043e9f1be1SHannes Reinecke 		return 0;
30517f4ff45SHannes Reinecke 
30691e968aaSMike Snitzer 	atomic_inc(&m->pg_init_count);
307518257b1SMike Snitzer 	clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
3083e9f1be1SHannes Reinecke 
3093e9f1be1SHannes Reinecke 	/* Check here to reset pg_init_required */
3103e9f1be1SHannes Reinecke 	if (!m->current_pg)
3113e9f1be1SHannes Reinecke 		return 0;
3123e9f1be1SHannes Reinecke 
313518257b1SMike Snitzer 	if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags))
3144e2d19e4SChandra Seetharaman 		pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
3154e2d19e4SChandra Seetharaman 						 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
316fb612642SKiyoshi Ueda 	list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
317fb612642SKiyoshi Ueda 		/* Skip failed paths */
318fb612642SKiyoshi Ueda 		if (!pgpath->is_active)
319fb612642SKiyoshi Ueda 			continue;
3204e2d19e4SChandra Seetharaman 		if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
3214e2d19e4SChandra Seetharaman 				       pg_init_delay))
32291e968aaSMike Snitzer 			atomic_inc(&m->pg_init_in_progress);
323fb612642SKiyoshi Ueda 	}
32491e968aaSMike Snitzer 	return atomic_read(&m->pg_init_in_progress);
325fb612642SKiyoshi Ueda }
326fb612642SKiyoshi Ueda 
327c1d7ecf7SBart Van Assche static int pg_init_all_paths(struct multipath *m)
3281da177e4SLinus Torvalds {
329c1d7ecf7SBart Van Assche 	int ret;
3302da1610aSMike Snitzer 	unsigned long flags;
3312da1610aSMike Snitzer 
3322da1610aSMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
333c1d7ecf7SBart Van Assche 	ret = __pg_init_all_paths(m);
3342da1610aSMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
335c1d7ecf7SBart Van Assche 
336c1d7ecf7SBart Van Assche 	return ret;
3372da1610aSMike Snitzer }
3382da1610aSMike Snitzer 
3392da1610aSMike Snitzer static void __switch_pg(struct multipath *m, struct priority_group *pg)
3402da1610aSMike Snitzer {
3412da1610aSMike Snitzer 	m->current_pg = pg;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	/* Must we initialise the PG first, and queue I/O till it's ready? */
344cfae5c9bSChandra Seetharaman 	if (m->hw_handler_name) {
345518257b1SMike Snitzer 		set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
346518257b1SMike Snitzer 		set_bit(MPATHF_QUEUE_IO, &m->flags);
3471da177e4SLinus Torvalds 	} else {
348518257b1SMike Snitzer 		clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
349518257b1SMike Snitzer 		clear_bit(MPATHF_QUEUE_IO, &m->flags);
3501da177e4SLinus Torvalds 	}
351c9e45581SDave Wysochanski 
35291e968aaSMike Snitzer 	atomic_set(&m->pg_init_count, 0);
3531da177e4SLinus Torvalds }
3541da177e4SLinus Torvalds 
3552da1610aSMike Snitzer static struct pgpath *choose_path_in_pg(struct multipath *m,
3562da1610aSMike Snitzer 					struct priority_group *pg,
35702ab823fSKiyoshi Ueda 					size_t nr_bytes)
3581da177e4SLinus Torvalds {
3592da1610aSMike Snitzer 	unsigned long flags;
360c922d5f7SJosef "Jeff" Sipek 	struct dm_path *path;
3612da1610aSMike Snitzer 	struct pgpath *pgpath;
3621da177e4SLinus Torvalds 
36390a4323cSMike Snitzer 	path = pg->ps.type->select_path(&pg->ps, nr_bytes);
3641da177e4SLinus Torvalds 	if (!path)
3652da1610aSMike Snitzer 		return ERR_PTR(-ENXIO);
3661da177e4SLinus Torvalds 
3672da1610aSMike Snitzer 	pgpath = path_to_pgpath(path);
3681da177e4SLinus Torvalds 
3692da1610aSMike Snitzer 	if (unlikely(lockless_dereference(m->current_pg) != pg)) {
3702da1610aSMike Snitzer 		/* Only update current_pgpath if pg changed */
3712da1610aSMike Snitzer 		spin_lock_irqsave(&m->lock, flags);
3722da1610aSMike Snitzer 		m->current_pgpath = pgpath;
3732da1610aSMike Snitzer 		__switch_pg(m, pg);
3742da1610aSMike Snitzer 		spin_unlock_irqrestore(&m->lock, flags);
3751da177e4SLinus Torvalds 	}
3761da177e4SLinus Torvalds 
3772da1610aSMike Snitzer 	return pgpath;
3782da1610aSMike Snitzer }
3792da1610aSMike Snitzer 
3802da1610aSMike Snitzer static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes)
3811da177e4SLinus Torvalds {
3822da1610aSMike Snitzer 	unsigned long flags;
3831da177e4SLinus Torvalds 	struct priority_group *pg;
3842da1610aSMike Snitzer 	struct pgpath *pgpath;
385d19a55ccSMike Snitzer 	unsigned bypassed = 1;
3861da177e4SLinus Torvalds 
38791e968aaSMike Snitzer 	if (!atomic_read(&m->nr_valid_paths)) {
388518257b1SMike Snitzer 		clear_bit(MPATHF_QUEUE_IO, &m->flags);
3891da177e4SLinus Torvalds 		goto failed;
3901f271972SBenjamin Marzinski 	}
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	/* Were we instructed to switch PG? */
3932da1610aSMike Snitzer 	if (lockless_dereference(m->next_pg)) {
3942da1610aSMike Snitzer 		spin_lock_irqsave(&m->lock, flags);
3951da177e4SLinus Torvalds 		pg = m->next_pg;
3962da1610aSMike Snitzer 		if (!pg) {
3972da1610aSMike Snitzer 			spin_unlock_irqrestore(&m->lock, flags);
3982da1610aSMike Snitzer 			goto check_current_pg;
3992da1610aSMike Snitzer 		}
4001da177e4SLinus Torvalds 		m->next_pg = NULL;
4012da1610aSMike Snitzer 		spin_unlock_irqrestore(&m->lock, flags);
4022da1610aSMike Snitzer 		pgpath = choose_path_in_pg(m, pg, nr_bytes);
4032da1610aSMike Snitzer 		if (!IS_ERR_OR_NULL(pgpath))
4042da1610aSMike Snitzer 			return pgpath;
4051da177e4SLinus Torvalds 	}
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	/* Don't change PG until it has no remaining paths */
4082da1610aSMike Snitzer check_current_pg:
4092da1610aSMike Snitzer 	pg = lockless_dereference(m->current_pg);
4102da1610aSMike Snitzer 	if (pg) {
4112da1610aSMike Snitzer 		pgpath = choose_path_in_pg(m, pg, nr_bytes);
4122da1610aSMike Snitzer 		if (!IS_ERR_OR_NULL(pgpath))
4132da1610aSMike Snitzer 			return pgpath;
4142da1610aSMike Snitzer 	}
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	/*
4171da177e4SLinus Torvalds 	 * Loop through priority groups until we find a valid path.
4181da177e4SLinus Torvalds 	 * First time we skip PGs marked 'bypassed'.
419f220fd4eSMike Christie 	 * Second time we only try the ones we skipped, but set
420f220fd4eSMike Christie 	 * pg_init_delay_retry so we do not hammer controllers.
4211da177e4SLinus Torvalds 	 */
4221da177e4SLinus Torvalds 	do {
4231da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
424d19a55ccSMike Snitzer 			if (pg->bypassed == !!bypassed)
4251da177e4SLinus Torvalds 				continue;
4262da1610aSMike Snitzer 			pgpath = choose_path_in_pg(m, pg, nr_bytes);
4272da1610aSMike Snitzer 			if (!IS_ERR_OR_NULL(pgpath)) {
428f220fd4eSMike Christie 				if (!bypassed)
429518257b1SMike Snitzer 					set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
4302da1610aSMike Snitzer 				return pgpath;
4311da177e4SLinus Torvalds 			}
432f220fd4eSMike Christie 		}
4331da177e4SLinus Torvalds 	} while (bypassed--);
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds failed:
4362da1610aSMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
4371da177e4SLinus Torvalds 	m->current_pgpath = NULL;
4381da177e4SLinus Torvalds 	m->current_pg = NULL;
4392da1610aSMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
4402da1610aSMike Snitzer 
4412da1610aSMike Snitzer 	return NULL;
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
44445e15720SKiyoshi Ueda /*
44586331f39SBart Van Assche  * dm_report_EIO() is a macro instead of a function to make pr_debug()
44686331f39SBart Van Assche  * report the function name and line number of the function from which
44786331f39SBart Van Assche  * it has been invoked.
44845e15720SKiyoshi Ueda  */
44986331f39SBart Van Assche #define dm_report_EIO(m)						\
45018a482f5SChristoph Hellwig do {									\
45186331f39SBart Van Assche 	struct mapped_device *md = dm_table_get_md((m)->ti->table);	\
45286331f39SBart Van Assche 									\
45386331f39SBart Van Assche 	pr_debug("%s: returning EIO; QIFNP = %d; SQIFNP = %d; DNFS = %d\n", \
45486331f39SBart Van Assche 		 dm_device_name(md),					\
45586331f39SBart Van Assche 		 test_bit(MPATHF_QUEUE_IF_NO_PATH, &(m)->flags),	\
45686331f39SBart Van Assche 		 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &(m)->flags),	\
45786331f39SBart Van Assche 		 dm_noflush_suspending((m)->ti));			\
45818a482f5SChristoph Hellwig } while (0)
45945e15720SKiyoshi Ueda 
46036fcffccSHannes Reinecke /*
46176e33fe4SMike Snitzer  * Map cloned requests (request-based multipath)
46236fcffccSHannes Reinecke  */
463eb8db831SChristoph Hellwig static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
464e5863d9aSMike Snitzer 				   union map_info *map_context,
465eb8db831SChristoph Hellwig 				   struct request **__clone)
4661da177e4SLinus Torvalds {
4677943bd6dSMike Snitzer 	struct multipath *m = ti->private;
468eb8db831SChristoph Hellwig 	size_t nr_bytes = blk_rq_bytes(rq);
4691da177e4SLinus Torvalds 	struct pgpath *pgpath;
470f40c67f0SKiyoshi Ueda 	struct block_device *bdev;
471eb8db831SChristoph Hellwig 	struct dm_mpath_io *mpio = get_mpio(map_context);
4727083abbbSBart Van Assche 	struct request_queue *q;
473eb8db831SChristoph Hellwig 	struct request *clone;
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	/* Do we need to select a new pgpath? */
4762da1610aSMike Snitzer 	pgpath = lockless_dereference(m->current_pgpath);
4772da1610aSMike Snitzer 	if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
4782da1610aSMike Snitzer 		pgpath = choose_pgpath(m, nr_bytes);
4791da177e4SLinus Torvalds 
4809bf59a61SMike Snitzer 	if (!pgpath) {
481ca5beb76SBart Van Assche 		if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
482b88efd43SMike Snitzer 			return DM_MAPIO_DELAY_REQUEUE;
48318a482f5SChristoph Hellwig 		dm_report_EIO(m);	/* Failed */
484f98e0eb6SChristoph Hellwig 		return DM_MAPIO_KILL;
485518257b1SMike Snitzer 	} else if (test_bit(MPATHF_QUEUE_IO, &m->flags) ||
486518257b1SMike Snitzer 		   test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
487c1d7ecf7SBart Van Assche 		if (pg_init_all_paths(m))
488c1d7ecf7SBart Van Assche 			return DM_MAPIO_DELAY_REQUEUE;
48906eb061fSBart Van Assche 		return DM_MAPIO_REQUEUE;
4909bf59a61SMike Snitzer 	}
4916afbc01dSMike Snitzer 
492eb8db831SChristoph Hellwig 	memset(mpio, 0, sizeof(*mpio));
493e8099177SHannes Reinecke 	mpio->pgpath = pgpath;
494e8099177SHannes Reinecke 	mpio->nr_bytes = nr_bytes;
4952eb6e1e3SKeith Busch 
4962eb6e1e3SKeith Busch 	bdev = pgpath->path.dev->bdev;
4977083abbbSBart Van Assche 	q = bdev_get_queue(bdev);
4987083abbbSBart Van Assche 	clone = blk_get_request(q, rq->cmd_flags | REQ_NOMERGE, GFP_ATOMIC);
4996599c84eSBart Van Assche 	if (IS_ERR(clone)) {
5006599c84eSBart Van Assche 		/* EBUSY, ENODEV or EWOULDBLOCK: requeue */
5017083abbbSBart Van Assche 		bool queue_dying = blk_queue_dying(q);
5027083abbbSBart Van Assche 		DMERR_LIMIT("blk_get_request() returned %ld%s - requeuing",
5037083abbbSBart Van Assche 			    PTR_ERR(clone), queue_dying ? " (path offline)" : "");
5047083abbbSBart Van Assche 		if (queue_dying) {
5057083abbbSBart Van Assche 			atomic_inc(&m->pg_init_in_progress);
5067083abbbSBart Van Assche 			activate_or_offline_path(pgpath);
5077083abbbSBart Van Assche 		}
50806eb061fSBart Van Assche 		return DM_MAPIO_DELAY_REQUEUE;
5094c6dd53dSMike Snitzer 	}
5106599c84eSBart Van Assche 	clone->bio = clone->biotail = NULL;
5116599c84eSBart Van Assche 	clone->rq_disk = bdev->bd_disk;
5126599c84eSBart Van Assche 	clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
5136599c84eSBart Van Assche 	*__clone = clone;
5142eb6e1e3SKeith Busch 
515e8099177SHannes Reinecke 	if (pgpath->pg->ps.type->start_io)
516e8099177SHannes Reinecke 		pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
517e8099177SHannes Reinecke 					      &pgpath->path,
518e8099177SHannes Reinecke 					      nr_bytes);
5192eb6e1e3SKeith Busch 	return DM_MAPIO_REMAPPED;
5201da177e4SLinus Torvalds }
5211da177e4SLinus Torvalds 
522e5863d9aSMike Snitzer static void multipath_release_clone(struct request *clone)
523e5863d9aSMike Snitzer {
524eb8db831SChristoph Hellwig 	blk_put_request(clone);
525e5863d9aSMike Snitzer }
526e5863d9aSMike Snitzer 
5271da177e4SLinus Torvalds /*
52876e33fe4SMike Snitzer  * Map cloned bios (bio-based multipath)
52976e33fe4SMike Snitzer  */
53076e33fe4SMike Snitzer static int __multipath_map_bio(struct multipath *m, struct bio *bio, struct dm_mpath_io *mpio)
53176e33fe4SMike Snitzer {
53276e33fe4SMike Snitzer 	size_t nr_bytes = bio->bi_iter.bi_size;
53376e33fe4SMike Snitzer 	struct pgpath *pgpath;
53476e33fe4SMike Snitzer 	unsigned long flags;
53576e33fe4SMike Snitzer 	bool queue_io;
53676e33fe4SMike Snitzer 
53776e33fe4SMike Snitzer 	/* Do we need to select a new pgpath? */
53876e33fe4SMike Snitzer 	pgpath = lockless_dereference(m->current_pgpath);
53976e33fe4SMike Snitzer 	queue_io = test_bit(MPATHF_QUEUE_IO, &m->flags);
54076e33fe4SMike Snitzer 	if (!pgpath || !queue_io)
54176e33fe4SMike Snitzer 		pgpath = choose_pgpath(m, nr_bytes);
54276e33fe4SMike Snitzer 
54376e33fe4SMike Snitzer 	if ((pgpath && queue_io) ||
54476e33fe4SMike Snitzer 	    (!pgpath && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) {
54576e33fe4SMike Snitzer 		/* Queue for the daemon to resubmit */
54676e33fe4SMike Snitzer 		spin_lock_irqsave(&m->lock, flags);
54776e33fe4SMike Snitzer 		bio_list_add(&m->queued_bios, bio);
54876e33fe4SMike Snitzer 		spin_unlock_irqrestore(&m->lock, flags);
54976e33fe4SMike Snitzer 		/* PG_INIT_REQUIRED cannot be set without QUEUE_IO */
55076e33fe4SMike Snitzer 		if (queue_io || test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
55176e33fe4SMike Snitzer 			pg_init_all_paths(m);
55276e33fe4SMike Snitzer 		else if (!queue_io)
55376e33fe4SMike Snitzer 			queue_work(kmultipathd, &m->process_queued_bios);
55476e33fe4SMike Snitzer 		return DM_MAPIO_SUBMITTED;
55576e33fe4SMike Snitzer 	}
55676e33fe4SMike Snitzer 
55776e33fe4SMike Snitzer 	if (!pgpath) {
558ca5beb76SBart Van Assche 		if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
55976e33fe4SMike Snitzer 			return DM_MAPIO_REQUEUE;
56018a482f5SChristoph Hellwig 		dm_report_EIO(m);
561846785e6SChristoph Hellwig 		return DM_MAPIO_KILL;
56276e33fe4SMike Snitzer 	}
56376e33fe4SMike Snitzer 
56476e33fe4SMike Snitzer 	mpio->pgpath = pgpath;
56576e33fe4SMike Snitzer 	mpio->nr_bytes = nr_bytes;
56676e33fe4SMike Snitzer 
5674e4cbee9SChristoph Hellwig 	bio->bi_status = 0;
56874d46992SChristoph Hellwig 	bio_set_dev(bio, pgpath->path.dev->bdev);
5691eff9d32SJens Axboe 	bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
57076e33fe4SMike Snitzer 
57176e33fe4SMike Snitzer 	if (pgpath->pg->ps.type->start_io)
57276e33fe4SMike Snitzer 		pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
57376e33fe4SMike Snitzer 					      &pgpath->path,
57476e33fe4SMike Snitzer 					      nr_bytes);
57576e33fe4SMike Snitzer 	return DM_MAPIO_REMAPPED;
57676e33fe4SMike Snitzer }
57776e33fe4SMike Snitzer 
57876e33fe4SMike Snitzer static int multipath_map_bio(struct dm_target *ti, struct bio *bio)
57976e33fe4SMike Snitzer {
58076e33fe4SMike Snitzer 	struct multipath *m = ti->private;
581bf661be1SMike Snitzer 	struct dm_mpath_io *mpio = NULL;
582bf661be1SMike Snitzer 
583bf661be1SMike Snitzer 	multipath_init_per_bio_data(bio, &mpio, NULL);
58476e33fe4SMike Snitzer 
58576e33fe4SMike Snitzer 	return __multipath_map_bio(m, bio, mpio);
58676e33fe4SMike Snitzer }
58776e33fe4SMike Snitzer 
5887e48c768SMike Snitzer static void process_queued_io_list(struct multipath *m)
58976e33fe4SMike Snitzer {
5907e48c768SMike Snitzer 	if (m->queue_mode == DM_TYPE_MQ_REQUEST_BASED)
5917e48c768SMike Snitzer 		dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table));
5927e48c768SMike Snitzer 	else if (m->queue_mode == DM_TYPE_BIO_BASED)
59376e33fe4SMike Snitzer 		queue_work(kmultipathd, &m->process_queued_bios);
59476e33fe4SMike Snitzer }
59576e33fe4SMike Snitzer 
59676e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work)
59776e33fe4SMike Snitzer {
59876e33fe4SMike Snitzer 	int r;
59976e33fe4SMike Snitzer 	unsigned long flags;
60076e33fe4SMike Snitzer 	struct bio *bio;
60176e33fe4SMike Snitzer 	struct bio_list bios;
60276e33fe4SMike Snitzer 	struct blk_plug plug;
60376e33fe4SMike Snitzer 	struct multipath *m =
60476e33fe4SMike Snitzer 		container_of(work, struct multipath, process_queued_bios);
60576e33fe4SMike Snitzer 
60676e33fe4SMike Snitzer 	bio_list_init(&bios);
60776e33fe4SMike Snitzer 
60876e33fe4SMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
60976e33fe4SMike Snitzer 
61076e33fe4SMike Snitzer 	if (bio_list_empty(&m->queued_bios)) {
61176e33fe4SMike Snitzer 		spin_unlock_irqrestore(&m->lock, flags);
61276e33fe4SMike Snitzer 		return;
61376e33fe4SMike Snitzer 	}
61476e33fe4SMike Snitzer 
61576e33fe4SMike Snitzer 	bio_list_merge(&bios, &m->queued_bios);
61676e33fe4SMike Snitzer 	bio_list_init(&m->queued_bios);
61776e33fe4SMike Snitzer 
61876e33fe4SMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
61976e33fe4SMike Snitzer 
62076e33fe4SMike Snitzer 	blk_start_plug(&plug);
62176e33fe4SMike Snitzer 	while ((bio = bio_list_pop(&bios))) {
62276e33fe4SMike Snitzer 		r = __multipath_map_bio(m, bio, get_mpio_from_bio(bio));
623846785e6SChristoph Hellwig 		switch (r) {
624846785e6SChristoph Hellwig 		case DM_MAPIO_KILL:
6254e4cbee9SChristoph Hellwig 			bio->bi_status = BLK_STS_IOERR;
6264e4cbee9SChristoph Hellwig 			bio_endio(bio);
627047385b3SDan Carpenter 			break;
628846785e6SChristoph Hellwig 		case DM_MAPIO_REQUEUE:
6294e4cbee9SChristoph Hellwig 			bio->bi_status = BLK_STS_DM_REQUEUE;
63076e33fe4SMike Snitzer 			bio_endio(bio);
631846785e6SChristoph Hellwig 			break;
632846785e6SChristoph Hellwig 		case DM_MAPIO_REMAPPED:
63376e33fe4SMike Snitzer 			generic_make_request(bio);
634846785e6SChristoph Hellwig 			break;
6359157c8d3SBart Van Assche 		case 0:
6369157c8d3SBart Van Assche 			break;
6379157c8d3SBart Van Assche 		default:
6389157c8d3SBart Van Assche 			WARN_ONCE(true, "__multipath_map_bio() returned %d\n", r);
639846785e6SChristoph Hellwig 		}
64076e33fe4SMike Snitzer 	}
64176e33fe4SMike Snitzer 	blk_finish_plug(&plug);
64276e33fe4SMike Snitzer }
64376e33fe4SMike Snitzer 
64476e33fe4SMike Snitzer /*
6451da177e4SLinus Torvalds  * If we run out of usable paths, should we queue I/O or error it?
6461da177e4SLinus Torvalds  */
647be7d31ccSMike Snitzer static int queue_if_no_path(struct multipath *m, bool queue_if_no_path,
648be7d31ccSMike Snitzer 			    bool save_old_value)
6491da177e4SLinus Torvalds {
6501da177e4SLinus Torvalds 	unsigned long flags;
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
653*5307e2adSLukas Wunner 	assign_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags,
654*5307e2adSLukas Wunner 		   (save_old_value && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) ||
655*5307e2adSLukas Wunner 		   (!save_old_value && queue_if_no_path));
656*5307e2adSLukas Wunner 	assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags,
657*5307e2adSLukas Wunner 		   queue_if_no_path || dm_noflush_suspending(m->ti));
6581da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
6591da177e4SLinus Torvalds 
66076e33fe4SMike Snitzer 	if (!queue_if_no_path) {
66163d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
6627e48c768SMike Snitzer 		process_queued_io_list(m);
66376e33fe4SMike Snitzer 	}
66463d832c3SHannes Reinecke 
6651da177e4SLinus Torvalds 	return 0;
6661da177e4SLinus Torvalds }
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds /*
6691da177e4SLinus Torvalds  * An event is triggered whenever a path is taken out of use.
6701da177e4SLinus Torvalds  * Includes path failure and PG bypass.
6711da177e4SLinus Torvalds  */
672c4028958SDavid Howells static void trigger_event(struct work_struct *work)
6731da177e4SLinus Torvalds {
674c4028958SDavid Howells 	struct multipath *m =
675c4028958SDavid Howells 		container_of(work, struct multipath, trigger_event);
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 	dm_table_event(m->ti->table);
6781da177e4SLinus Torvalds }
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds /*-----------------------------------------------------------------
6811da177e4SLinus Torvalds  * Constructor/argument parsing:
6821da177e4SLinus Torvalds  * <#multipath feature args> [<arg>]*
6831da177e4SLinus Torvalds  * <#hw_handler args> [hw_handler [<arg>]*]
6841da177e4SLinus Torvalds  * <#priority groups>
6851da177e4SLinus Torvalds  * <initial priority group>
6861da177e4SLinus Torvalds  *     [<selector> <#selector args> [<arg>]*
6871da177e4SLinus Torvalds  *      <#paths> <#per-path selector args>
6881da177e4SLinus Torvalds  *         [<path> [<arg>]* ]+ ]+
6891da177e4SLinus Torvalds  *---------------------------------------------------------------*/
690498f0103SMike Snitzer static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
6911da177e4SLinus Torvalds 			       struct dm_target *ti)
6921da177e4SLinus Torvalds {
6931da177e4SLinus Torvalds 	int r;
6941da177e4SLinus Torvalds 	struct path_selector_type *pst;
6951da177e4SLinus Torvalds 	unsigned ps_argc;
6961da177e4SLinus Torvalds 
6975916a22bSEric Biggers 	static const struct dm_arg _args[] = {
69872d94861SAlasdair G Kergon 		{0, 1024, "invalid number of path selector args"},
6991da177e4SLinus Torvalds 	};
7001da177e4SLinus Torvalds 
701498f0103SMike Snitzer 	pst = dm_get_path_selector(dm_shift_arg(as));
7021da177e4SLinus Torvalds 	if (!pst) {
70372d94861SAlasdair G Kergon 		ti->error = "unknown path selector type";
7041da177e4SLinus Torvalds 		return -EINVAL;
7051da177e4SLinus Torvalds 	}
7061da177e4SLinus Torvalds 
707498f0103SMike Snitzer 	r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
708371b2e34SMikulas Patocka 	if (r) {
709371b2e34SMikulas Patocka 		dm_put_path_selector(pst);
7101da177e4SLinus Torvalds 		return -EINVAL;
711371b2e34SMikulas Patocka 	}
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	r = pst->create(&pg->ps, ps_argc, as->argv);
7141da177e4SLinus Torvalds 	if (r) {
7151da177e4SLinus Torvalds 		dm_put_path_selector(pst);
71672d94861SAlasdair G Kergon 		ti->error = "path selector constructor failed";
7171da177e4SLinus Torvalds 		return r;
7181da177e4SLinus Torvalds 	}
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	pg->ps.type = pst;
721498f0103SMike Snitzer 	dm_consume_args(as, ps_argc);
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	return 0;
7241da177e4SLinus Torvalds }
7251da177e4SLinus Torvalds 
726498f0103SMike Snitzer static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
7271da177e4SLinus Torvalds 			       struct dm_target *ti)
7281da177e4SLinus Torvalds {
7291da177e4SLinus Torvalds 	int r;
7301da177e4SLinus Torvalds 	struct pgpath *p;
731ae11b1b3SHannes Reinecke 	struct multipath *m = ti->private;
732a58a935dSMike Snitzer 	struct request_queue *q = NULL;
733a58a935dSMike Snitzer 	const char *attached_handler_name;
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	/* we need at least a path arg */
7361da177e4SLinus Torvalds 	if (as->argc < 1) {
73772d94861SAlasdair G Kergon 		ti->error = "no device given";
73801460f35SBenjamin Marzinski 		return ERR_PTR(-EINVAL);
7391da177e4SLinus Torvalds 	}
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	p = alloc_pgpath();
7421da177e4SLinus Torvalds 	if (!p)
74301460f35SBenjamin Marzinski 		return ERR_PTR(-ENOMEM);
7441da177e4SLinus Torvalds 
745498f0103SMike Snitzer 	r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
7468215d6ecSNikanth Karthikesan 			  &p->path.dev);
7471da177e4SLinus Torvalds 	if (r) {
74872d94861SAlasdair G Kergon 		ti->error = "error getting device";
7491da177e4SLinus Torvalds 		goto bad;
7501da177e4SLinus Torvalds 	}
7511da177e4SLinus Torvalds 
752518257b1SMike Snitzer 	if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) || m->hw_handler_name)
753a58a935dSMike Snitzer 		q = bdev_get_queue(p->path.dev->bdev);
754a0cf7ea9SHannes Reinecke 
755518257b1SMike Snitzer 	if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) {
7561bab0de0SChristoph Hellwig retain:
757a58a935dSMike Snitzer 		attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
758a58a935dSMike Snitzer 		if (attached_handler_name) {
759a58a935dSMike Snitzer 			/*
76054cd640dStang.junhui 			 * Clear any hw_handler_params associated with a
76154cd640dStang.junhui 			 * handler that isn't already attached.
76254cd640dStang.junhui 			 */
76354cd640dStang.junhui 			if (m->hw_handler_name && strcmp(attached_handler_name, m->hw_handler_name)) {
76454cd640dStang.junhui 				kfree(m->hw_handler_params);
76554cd640dStang.junhui 				m->hw_handler_params = NULL;
76654cd640dStang.junhui 			}
76754cd640dStang.junhui 
76854cd640dStang.junhui 			/*
769a58a935dSMike Snitzer 			 * Reset hw_handler_name to match the attached handler
770a58a935dSMike Snitzer 			 *
771a58a935dSMike Snitzer 			 * NB. This modifies the table line to show the actual
772a58a935dSMike Snitzer 			 * handler instead of the original table passed in.
773a58a935dSMike Snitzer 			 */
774a58a935dSMike Snitzer 			kfree(m->hw_handler_name);
775a58a935dSMike Snitzer 			m->hw_handler_name = attached_handler_name;
776a58a935dSMike Snitzer 		}
777a58a935dSMike Snitzer 	}
778a58a935dSMike Snitzer 
779a58a935dSMike Snitzer 	if (m->hw_handler_name) {
780a0cf7ea9SHannes Reinecke 		r = scsi_dh_attach(q, m->hw_handler_name);
781a0cf7ea9SHannes Reinecke 		if (r == -EBUSY) {
7821bab0de0SChristoph Hellwig 			char b[BDEVNAME_SIZE];
783a0cf7ea9SHannes Reinecke 
7841bab0de0SChristoph Hellwig 			printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
7851bab0de0SChristoph Hellwig 				bdevname(p->path.dev->bdev, b));
7861bab0de0SChristoph Hellwig 			goto retain;
7871bab0de0SChristoph Hellwig 		}
788ae11b1b3SHannes Reinecke 		if (r < 0) {
789a0cf7ea9SHannes Reinecke 			ti->error = "error attaching hardware handler";
790ae11b1b3SHannes Reinecke 			dm_put_device(ti, p->path.dev);
791ae11b1b3SHannes Reinecke 			goto bad;
792ae11b1b3SHannes Reinecke 		}
7932bfd2e13SChandra Seetharaman 
7942bfd2e13SChandra Seetharaman 		if (m->hw_handler_params) {
7952bfd2e13SChandra Seetharaman 			r = scsi_dh_set_params(q, m->hw_handler_params);
7962bfd2e13SChandra Seetharaman 			if (r < 0) {
7972bfd2e13SChandra Seetharaman 				ti->error = "unable to set hardware "
7982bfd2e13SChandra Seetharaman 							"handler parameters";
7992bfd2e13SChandra Seetharaman 				dm_put_device(ti, p->path.dev);
8002bfd2e13SChandra Seetharaman 				goto bad;
8012bfd2e13SChandra Seetharaman 			}
8022bfd2e13SChandra Seetharaman 		}
803ae11b1b3SHannes Reinecke 	}
804ae11b1b3SHannes Reinecke 
8051da177e4SLinus Torvalds 	r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
8061da177e4SLinus Torvalds 	if (r) {
8071da177e4SLinus Torvalds 		dm_put_device(ti, p->path.dev);
8081da177e4SLinus Torvalds 		goto bad;
8091da177e4SLinus Torvalds 	}
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 	return p;
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds  bad:
8141da177e4SLinus Torvalds 	free_pgpath(p);
81501460f35SBenjamin Marzinski 	return ERR_PTR(r);
8161da177e4SLinus Torvalds }
8171da177e4SLinus Torvalds 
818498f0103SMike Snitzer static struct priority_group *parse_priority_group(struct dm_arg_set *as,
81928f16c20SMicha³ Miros³aw 						   struct multipath *m)
8201da177e4SLinus Torvalds {
8215916a22bSEric Biggers 	static const struct dm_arg _args[] = {
82272d94861SAlasdair G Kergon 		{1, 1024, "invalid number of paths"},
82372d94861SAlasdair G Kergon 		{0, 1024, "invalid number of selector args"}
8241da177e4SLinus Torvalds 	};
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	int r;
827498f0103SMike Snitzer 	unsigned i, nr_selector_args, nr_args;
8281da177e4SLinus Torvalds 	struct priority_group *pg;
82928f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	if (as->argc < 2) {
8321da177e4SLinus Torvalds 		as->argc = 0;
83301460f35SBenjamin Marzinski 		ti->error = "not enough priority group arguments";
83401460f35SBenjamin Marzinski 		return ERR_PTR(-EINVAL);
8351da177e4SLinus Torvalds 	}
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	pg = alloc_priority_group();
8381da177e4SLinus Torvalds 	if (!pg) {
83972d94861SAlasdair G Kergon 		ti->error = "couldn't allocate priority group";
84001460f35SBenjamin Marzinski 		return ERR_PTR(-ENOMEM);
8411da177e4SLinus Torvalds 	}
8421da177e4SLinus Torvalds 	pg->m = m;
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	r = parse_path_selector(as, pg, ti);
8451da177e4SLinus Torvalds 	if (r)
8461da177e4SLinus Torvalds 		goto bad;
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	/*
8491da177e4SLinus Torvalds 	 * read the paths
8501da177e4SLinus Torvalds 	 */
851498f0103SMike Snitzer 	r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
8521da177e4SLinus Torvalds 	if (r)
8531da177e4SLinus Torvalds 		goto bad;
8541da177e4SLinus Torvalds 
855498f0103SMike Snitzer 	r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
8561da177e4SLinus Torvalds 	if (r)
8571da177e4SLinus Torvalds 		goto bad;
8581da177e4SLinus Torvalds 
859498f0103SMike Snitzer 	nr_args = 1 + nr_selector_args;
8601da177e4SLinus Torvalds 	for (i = 0; i < pg->nr_pgpaths; i++) {
8611da177e4SLinus Torvalds 		struct pgpath *pgpath;
862498f0103SMike Snitzer 		struct dm_arg_set path_args;
8631da177e4SLinus Torvalds 
864498f0103SMike Snitzer 		if (as->argc < nr_args) {
865148acff6SMikulas Patocka 			ti->error = "not enough path parameters";
8666bbf79a1SAlasdair G Kergon 			r = -EINVAL;
8671da177e4SLinus Torvalds 			goto bad;
868148acff6SMikulas Patocka 		}
8691da177e4SLinus Torvalds 
870498f0103SMike Snitzer 		path_args.argc = nr_args;
8711da177e4SLinus Torvalds 		path_args.argv = as->argv;
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 		pgpath = parse_path(&path_args, &pg->ps, ti);
87401460f35SBenjamin Marzinski 		if (IS_ERR(pgpath)) {
87501460f35SBenjamin Marzinski 			r = PTR_ERR(pgpath);
8761da177e4SLinus Torvalds 			goto bad;
87701460f35SBenjamin Marzinski 		}
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 		pgpath->pg = pg;
8801da177e4SLinus Torvalds 		list_add_tail(&pgpath->list, &pg->pgpaths);
881498f0103SMike Snitzer 		dm_consume_args(as, nr_args);
8821da177e4SLinus Torvalds 	}
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	return pg;
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds  bad:
8871da177e4SLinus Torvalds 	free_priority_group(pg, ti);
88801460f35SBenjamin Marzinski 	return ERR_PTR(r);
8891da177e4SLinus Torvalds }
8901da177e4SLinus Torvalds 
891498f0103SMike Snitzer static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
8921da177e4SLinus Torvalds {
8931da177e4SLinus Torvalds 	unsigned hw_argc;
8942bfd2e13SChandra Seetharaman 	int ret;
89528f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
8961da177e4SLinus Torvalds 
8975916a22bSEric Biggers 	static const struct dm_arg _args[] = {
89872d94861SAlasdair G Kergon 		{0, 1024, "invalid number of hardware handler args"},
8991da177e4SLinus Torvalds 	};
9001da177e4SLinus Torvalds 
901498f0103SMike Snitzer 	if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
9021da177e4SLinus Torvalds 		return -EINVAL;
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 	if (!hw_argc)
9051da177e4SLinus Torvalds 		return 0;
9061da177e4SLinus Torvalds 
907e83068a5SMike Snitzer 	if (m->queue_mode == DM_TYPE_BIO_BASED) {
90876e33fe4SMike Snitzer 		dm_consume_args(as, hw_argc);
90976e33fe4SMike Snitzer 		DMERR("bio-based multipath doesn't allow hardware handler args");
91076e33fe4SMike Snitzer 		return 0;
91176e33fe4SMike Snitzer 	}
91276e33fe4SMike Snitzer 
913498f0103SMike Snitzer 	m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
914f97dc421Stang.junhui 	if (!m->hw_handler_name)
915f97dc421Stang.junhui 		return -EINVAL;
91614e98c5cSChandra Seetharaman 
9172bfd2e13SChandra Seetharaman 	if (hw_argc > 1) {
9182bfd2e13SChandra Seetharaman 		char *p;
9192bfd2e13SChandra Seetharaman 		int i, j, len = 4;
9202bfd2e13SChandra Seetharaman 
9212bfd2e13SChandra Seetharaman 		for (i = 0; i <= hw_argc - 2; i++)
9222bfd2e13SChandra Seetharaman 			len += strlen(as->argv[i]) + 1;
9232bfd2e13SChandra Seetharaman 		p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
9242bfd2e13SChandra Seetharaman 		if (!p) {
9252bfd2e13SChandra Seetharaman 			ti->error = "memory allocation failed";
9262bfd2e13SChandra Seetharaman 			ret = -ENOMEM;
9272bfd2e13SChandra Seetharaman 			goto fail;
9282bfd2e13SChandra Seetharaman 		}
9292bfd2e13SChandra Seetharaman 		j = sprintf(p, "%d", hw_argc - 1);
9302bfd2e13SChandra Seetharaman 		for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
9312bfd2e13SChandra Seetharaman 			j = sprintf(p, "%s", as->argv[i]);
9322bfd2e13SChandra Seetharaman 	}
933498f0103SMike Snitzer 	dm_consume_args(as, hw_argc - 1);
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	return 0;
9362bfd2e13SChandra Seetharaman fail:
9372bfd2e13SChandra Seetharaman 	kfree(m->hw_handler_name);
9382bfd2e13SChandra Seetharaman 	m->hw_handler_name = NULL;
9392bfd2e13SChandra Seetharaman 	return ret;
9401da177e4SLinus Torvalds }
9411da177e4SLinus Torvalds 
942498f0103SMike Snitzer static int parse_features(struct dm_arg_set *as, struct multipath *m)
9431da177e4SLinus Torvalds {
9441da177e4SLinus Torvalds 	int r;
9451da177e4SLinus Torvalds 	unsigned argc;
94628f16c20SMicha³ Miros³aw 	struct dm_target *ti = m->ti;
947498f0103SMike Snitzer 	const char *arg_name;
9481da177e4SLinus Torvalds 
9495916a22bSEric Biggers 	static const struct dm_arg _args[] = {
950e83068a5SMike Snitzer 		{0, 8, "invalid number of feature args"},
951c9e45581SDave Wysochanski 		{1, 50, "pg_init_retries must be between 1 and 50"},
9524e2d19e4SChandra Seetharaman 		{0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
9531da177e4SLinus Torvalds 	};
9541da177e4SLinus Torvalds 
955498f0103SMike Snitzer 	r = dm_read_arg_group(_args, as, &argc, &ti->error);
9561da177e4SLinus Torvalds 	if (r)
9571da177e4SLinus Torvalds 		return -EINVAL;
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 	if (!argc)
9601da177e4SLinus Torvalds 		return 0;
9611da177e4SLinus Torvalds 
962c9e45581SDave Wysochanski 	do {
963498f0103SMike Snitzer 		arg_name = dm_shift_arg(as);
964c9e45581SDave Wysochanski 		argc--;
965c9e45581SDave Wysochanski 
966498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "queue_if_no_path")) {
967be7d31ccSMike Snitzer 			r = queue_if_no_path(m, true, false);
968c9e45581SDave Wysochanski 			continue;
9691da177e4SLinus Torvalds 		}
970c9e45581SDave Wysochanski 
971a58a935dSMike Snitzer 		if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
972518257b1SMike Snitzer 			set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
973a58a935dSMike Snitzer 			continue;
974a58a935dSMike Snitzer 		}
975a58a935dSMike Snitzer 
976498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "pg_init_retries") &&
977c9e45581SDave Wysochanski 		    (argc >= 1)) {
978498f0103SMike Snitzer 			r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
979c9e45581SDave Wysochanski 			argc--;
980c9e45581SDave Wysochanski 			continue;
981c9e45581SDave Wysochanski 		}
982c9e45581SDave Wysochanski 
983498f0103SMike Snitzer 		if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
9844e2d19e4SChandra Seetharaman 		    (argc >= 1)) {
985498f0103SMike Snitzer 			r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
9864e2d19e4SChandra Seetharaman 			argc--;
9874e2d19e4SChandra Seetharaman 			continue;
9884e2d19e4SChandra Seetharaman 		}
9894e2d19e4SChandra Seetharaman 
990e83068a5SMike Snitzer 		if (!strcasecmp(arg_name, "queue_mode") &&
991e83068a5SMike Snitzer 		    (argc >= 1)) {
992e83068a5SMike Snitzer 			const char *queue_mode_name = dm_shift_arg(as);
993e83068a5SMike Snitzer 
994e83068a5SMike Snitzer 			if (!strcasecmp(queue_mode_name, "bio"))
995e83068a5SMike Snitzer 				m->queue_mode = DM_TYPE_BIO_BASED;
996e83068a5SMike Snitzer 			else if (!strcasecmp(queue_mode_name, "rq"))
997e83068a5SMike Snitzer 				m->queue_mode = DM_TYPE_REQUEST_BASED;
998e83068a5SMike Snitzer 			else if (!strcasecmp(queue_mode_name, "mq"))
999e83068a5SMike Snitzer 				m->queue_mode = DM_TYPE_MQ_REQUEST_BASED;
1000e83068a5SMike Snitzer 			else {
1001e83068a5SMike Snitzer 				ti->error = "Unknown 'queue_mode' requested";
1002e83068a5SMike Snitzer 				r = -EINVAL;
1003e83068a5SMike Snitzer 			}
1004e83068a5SMike Snitzer 			argc--;
1005e83068a5SMike Snitzer 			continue;
1006e83068a5SMike Snitzer 		}
1007e83068a5SMike Snitzer 
1008c9e45581SDave Wysochanski 		ti->error = "Unrecognised multipath feature request";
1009c9e45581SDave Wysochanski 		r = -EINVAL;
1010c9e45581SDave Wysochanski 	} while (argc && !r);
1011c9e45581SDave Wysochanski 
1012c9e45581SDave Wysochanski 	return r;
10131da177e4SLinus Torvalds }
10141da177e4SLinus Torvalds 
1015e83068a5SMike Snitzer static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv)
10161da177e4SLinus Torvalds {
1017498f0103SMike Snitzer 	/* target arguments */
10185916a22bSEric Biggers 	static const struct dm_arg _args[] = {
1019a490a07aSMike Snitzer 		{0, 1024, "invalid number of priority groups"},
1020a490a07aSMike Snitzer 		{0, 1024, "invalid initial priority group number"},
10211da177e4SLinus Torvalds 	};
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 	int r;
10241da177e4SLinus Torvalds 	struct multipath *m;
1025498f0103SMike Snitzer 	struct dm_arg_set as;
10261da177e4SLinus Torvalds 	unsigned pg_count = 0;
10271da177e4SLinus Torvalds 	unsigned next_pg_num;
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	as.argc = argc;
10301da177e4SLinus Torvalds 	as.argv = argv;
10311da177e4SLinus Torvalds 
1032e83068a5SMike Snitzer 	m = alloc_multipath(ti);
10331da177e4SLinus Torvalds 	if (!m) {
103472d94861SAlasdair G Kergon 		ti->error = "can't allocate multipath";
10351da177e4SLinus Torvalds 		return -EINVAL;
10361da177e4SLinus Torvalds 	}
10371da177e4SLinus Torvalds 
103828f16c20SMicha³ Miros³aw 	r = parse_features(&as, m);
10391da177e4SLinus Torvalds 	if (r)
10401da177e4SLinus Torvalds 		goto bad;
10411da177e4SLinus Torvalds 
1042e83068a5SMike Snitzer 	r = alloc_multipath_stage2(ti, m);
1043e83068a5SMike Snitzer 	if (r)
1044e83068a5SMike Snitzer 		goto bad;
1045e83068a5SMike Snitzer 
104628f16c20SMicha³ Miros³aw 	r = parse_hw_handler(&as, m);
10471da177e4SLinus Torvalds 	if (r)
10481da177e4SLinus Torvalds 		goto bad;
10491da177e4SLinus Torvalds 
1050498f0103SMike Snitzer 	r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
10511da177e4SLinus Torvalds 	if (r)
10521da177e4SLinus Torvalds 		goto bad;
10531da177e4SLinus Torvalds 
1054498f0103SMike Snitzer 	r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
10551da177e4SLinus Torvalds 	if (r)
10561da177e4SLinus Torvalds 		goto bad;
10571da177e4SLinus Torvalds 
1058a490a07aSMike Snitzer 	if ((!m->nr_priority_groups && next_pg_num) ||
1059a490a07aSMike Snitzer 	    (m->nr_priority_groups && !next_pg_num)) {
1060a490a07aSMike Snitzer 		ti->error = "invalid initial priority group";
1061a490a07aSMike Snitzer 		r = -EINVAL;
1062a490a07aSMike Snitzer 		goto bad;
1063a490a07aSMike Snitzer 	}
1064a490a07aSMike Snitzer 
10651da177e4SLinus Torvalds 	/* parse the priority groups */
10661da177e4SLinus Torvalds 	while (as.argc) {
10671da177e4SLinus Torvalds 		struct priority_group *pg;
106891e968aaSMike Snitzer 		unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths);
10691da177e4SLinus Torvalds 
107028f16c20SMicha³ Miros³aw 		pg = parse_priority_group(&as, m);
107101460f35SBenjamin Marzinski 		if (IS_ERR(pg)) {
107201460f35SBenjamin Marzinski 			r = PTR_ERR(pg);
10731da177e4SLinus Torvalds 			goto bad;
10741da177e4SLinus Torvalds 		}
10751da177e4SLinus Torvalds 
107691e968aaSMike Snitzer 		nr_valid_paths += pg->nr_pgpaths;
107791e968aaSMike Snitzer 		atomic_set(&m->nr_valid_paths, nr_valid_paths);
107891e968aaSMike Snitzer 
10791da177e4SLinus Torvalds 		list_add_tail(&pg->list, &m->priority_groups);
10801da177e4SLinus Torvalds 		pg_count++;
10811da177e4SLinus Torvalds 		pg->pg_num = pg_count;
10821da177e4SLinus Torvalds 		if (!--next_pg_num)
10831da177e4SLinus Torvalds 			m->next_pg = pg;
10841da177e4SLinus Torvalds 	}
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 	if (pg_count != m->nr_priority_groups) {
108772d94861SAlasdair G Kergon 		ti->error = "priority group count mismatch";
10881da177e4SLinus Torvalds 		r = -EINVAL;
10891da177e4SLinus Torvalds 		goto bad;
10901da177e4SLinus Torvalds 	}
10911da177e4SLinus Torvalds 
109255a62eefSAlasdair G Kergon 	ti->num_flush_bios = 1;
109355a62eefSAlasdair G Kergon 	ti->num_discard_bios = 1;
1094042bcef8SMike Snitzer 	ti->num_write_same_bios = 1;
1095ac62d620SChristoph Hellwig 	ti->num_write_zeroes_bios = 1;
1096e83068a5SMike Snitzer 	if (m->queue_mode == DM_TYPE_BIO_BASED)
1097bf661be1SMike Snitzer 		ti->per_io_data_size = multipath_per_bio_data_size();
1098eb8db831SChristoph Hellwig 	else
10998637a6bfSMike Snitzer 		ti->per_io_data_size = sizeof(struct dm_mpath_io);
11008627921fSMikulas Patocka 
11011da177e4SLinus Torvalds 	return 0;
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds  bad:
11041da177e4SLinus Torvalds 	free_multipath(m);
11051da177e4SLinus Torvalds 	return r;
11061da177e4SLinus Torvalds }
11071da177e4SLinus Torvalds 
11082bded7bdSKiyoshi Ueda static void multipath_wait_for_pg_init_completion(struct multipath *m)
11092bded7bdSKiyoshi Ueda {
11109f4c3f87SBart Van Assche 	DEFINE_WAIT(wait);
11112bded7bdSKiyoshi Ueda 
11122bded7bdSKiyoshi Ueda 	while (1) {
11139f4c3f87SBart Van Assche 		prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE);
11142bded7bdSKiyoshi Ueda 
111591e968aaSMike Snitzer 		if (!atomic_read(&m->pg_init_in_progress))
11162bded7bdSKiyoshi Ueda 			break;
11172bded7bdSKiyoshi Ueda 
11182bded7bdSKiyoshi Ueda 		io_schedule();
11192bded7bdSKiyoshi Ueda 	}
11209f4c3f87SBart Van Assche 	finish_wait(&m->pg_init_wait, &wait);
11212bded7bdSKiyoshi Ueda }
11222bded7bdSKiyoshi Ueda 
11232bded7bdSKiyoshi Ueda static void flush_multipath_work(struct multipath *m)
11241da177e4SLinus Torvalds {
1125518257b1SMike Snitzer 	set_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1126518257b1SMike Snitzer 	smp_mb__after_atomic();
1127954a73d5SShiva Krishna Merla 
1128bab7cfc7SChandra Seetharaman 	flush_workqueue(kmpath_handlerd);
11292bded7bdSKiyoshi Ueda 	multipath_wait_for_pg_init_completion(m);
1130a044d016SAlasdair G Kergon 	flush_workqueue(kmultipathd);
113143829731STejun Heo 	flush_work(&m->trigger_event);
1132954a73d5SShiva Krishna Merla 
1133518257b1SMike Snitzer 	clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1134518257b1SMike Snitzer 	smp_mb__after_atomic();
11356df400abSKiyoshi Ueda }
11366df400abSKiyoshi Ueda 
11376df400abSKiyoshi Ueda static void multipath_dtr(struct dm_target *ti)
11386df400abSKiyoshi Ueda {
11396df400abSKiyoshi Ueda 	struct multipath *m = ti->private;
11406df400abSKiyoshi Ueda 
11412bded7bdSKiyoshi Ueda 	flush_multipath_work(m);
11421da177e4SLinus Torvalds 	free_multipath(m);
11431da177e4SLinus Torvalds }
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds /*
11461da177e4SLinus Torvalds  * Take a path out of use.
11471da177e4SLinus Torvalds  */
11481da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath)
11491da177e4SLinus Torvalds {
11501da177e4SLinus Torvalds 	unsigned long flags;
11511da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
11541da177e4SLinus Torvalds 
11556680073dSKiyoshi Ueda 	if (!pgpath->is_active)
11561da177e4SLinus Torvalds 		goto out;
11571da177e4SLinus Torvalds 
115872d94861SAlasdair G Kergon 	DMWARN("Failing path %s.", pgpath->path.dev->name);
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
1161be7d31ccSMike Snitzer 	pgpath->is_active = false;
11621da177e4SLinus Torvalds 	pgpath->fail_count++;
11631da177e4SLinus Torvalds 
116491e968aaSMike Snitzer 	atomic_dec(&m->nr_valid_paths);
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds 	if (pgpath == m->current_pgpath)
11671da177e4SLinus Torvalds 		m->current_pgpath = NULL;
11681da177e4SLinus Torvalds 
1169b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
117091e968aaSMike Snitzer 		       pgpath->path.dev->name, atomic_read(&m->nr_valid_paths));
1171b15546f9SMike Anderson 
1172fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds out:
11751da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 	return 0;
11781da177e4SLinus Torvalds }
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds /*
11811da177e4SLinus Torvalds  * Reinstate a previously-failed path
11821da177e4SLinus Torvalds  */
11831da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath)
11841da177e4SLinus Torvalds {
118563d832c3SHannes Reinecke 	int r = 0, run_queue = 0;
11861da177e4SLinus Torvalds 	unsigned long flags;
11871da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
118891e968aaSMike Snitzer 	unsigned nr_valid_paths;
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
11911da177e4SLinus Torvalds 
11926680073dSKiyoshi Ueda 	if (pgpath->is_active)
11931da177e4SLinus Torvalds 		goto out;
11941da177e4SLinus Torvalds 
1195ec31f3f7SMike Snitzer 	DMWARN("Reinstating path %s.", pgpath->path.dev->name);
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 	r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
11981da177e4SLinus Torvalds 	if (r)
11991da177e4SLinus Torvalds 		goto out;
12001da177e4SLinus Torvalds 
1201be7d31ccSMike Snitzer 	pgpath->is_active = true;
12021da177e4SLinus Torvalds 
120391e968aaSMike Snitzer 	nr_valid_paths = atomic_inc_return(&m->nr_valid_paths);
120491e968aaSMike Snitzer 	if (nr_valid_paths == 1) {
12051da177e4SLinus Torvalds 		m->current_pgpath = NULL;
120663d832c3SHannes Reinecke 		run_queue = 1;
1207e54f77ddSChandra Seetharaman 	} else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
12084e2d19e4SChandra Seetharaman 		if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
120991e968aaSMike Snitzer 			atomic_inc(&m->pg_init_in_progress);
1210e54f77ddSChandra Seetharaman 	}
12111da177e4SLinus Torvalds 
1212b15546f9SMike Anderson 	dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
121391e968aaSMike Snitzer 		       pgpath->path.dev->name, nr_valid_paths);
1214b15546f9SMike Anderson 
1215fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds out:
12181da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
121976e33fe4SMike Snitzer 	if (run_queue) {
122063d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
12217e48c768SMike Snitzer 		process_queued_io_list(m);
122276e33fe4SMike Snitzer 	}
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 	return r;
12251da177e4SLinus Torvalds }
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds /*
12281da177e4SLinus Torvalds  * Fail or reinstate all paths that match the provided struct dm_dev.
12291da177e4SLinus Torvalds  */
12301da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev,
12311da177e4SLinus Torvalds 		      action_fn action)
12321da177e4SLinus Torvalds {
123319040c0bSMike Snitzer 	int r = -EINVAL;
12341da177e4SLinus Torvalds 	struct pgpath *pgpath;
12351da177e4SLinus Torvalds 	struct priority_group *pg;
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
12381da177e4SLinus Torvalds 		list_for_each_entry(pgpath, &pg->pgpaths, list) {
12391da177e4SLinus Torvalds 			if (pgpath->path.dev == dev)
12401da177e4SLinus Torvalds 				r = action(pgpath);
12411da177e4SLinus Torvalds 		}
12421da177e4SLinus Torvalds 	}
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds 	return r;
12451da177e4SLinus Torvalds }
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds /*
12481da177e4SLinus Torvalds  * Temporarily try to avoid having to use the specified PG
12491da177e4SLinus Torvalds  */
12501da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg,
1251be7d31ccSMike Snitzer 		      bool bypassed)
12521da177e4SLinus Torvalds {
12531da177e4SLinus Torvalds 	unsigned long flags;
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds 	pg->bypassed = bypassed;
12581da177e4SLinus Torvalds 	m->current_pgpath = NULL;
12591da177e4SLinus Torvalds 	m->current_pg = NULL;
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
12621da177e4SLinus Torvalds 
1263fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
12641da177e4SLinus Torvalds }
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds /*
12671da177e4SLinus Torvalds  * Switch to using the specified PG from the next I/O that gets mapped
12681da177e4SLinus Torvalds  */
12691da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr)
12701da177e4SLinus Torvalds {
12711da177e4SLinus Torvalds 	struct priority_group *pg;
12721da177e4SLinus Torvalds 	unsigned pgnum;
12731da177e4SLinus Torvalds 	unsigned long flags;
127431998ef1SMikulas Patocka 	char dummy;
12751da177e4SLinus Torvalds 
127631998ef1SMikulas Patocka 	if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1277cc5bd925Stang.junhui 	    !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
12781da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to switch_pg_num");
12791da177e4SLinus Torvalds 		return -EINVAL;
12801da177e4SLinus Torvalds 	}
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
12831da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
1284be7d31ccSMike Snitzer 		pg->bypassed = false;
12851da177e4SLinus Torvalds 		if (--pgnum)
12861da177e4SLinus Torvalds 			continue;
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 		m->current_pgpath = NULL;
12891da177e4SLinus Torvalds 		m->current_pg = NULL;
12901da177e4SLinus Torvalds 		m->next_pg = pg;
12911da177e4SLinus Torvalds 	}
12921da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
12931da177e4SLinus Torvalds 
1294fe9cf30eSAlasdair G Kergon 	schedule_work(&m->trigger_event);
12951da177e4SLinus Torvalds 	return 0;
12961da177e4SLinus Torvalds }
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds /*
12991da177e4SLinus Torvalds  * Set/clear bypassed status of a PG.
13001da177e4SLinus Torvalds  * PGs are numbered upwards from 1 in the order they were declared.
13011da177e4SLinus Torvalds  */
1302be7d31ccSMike Snitzer static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed)
13031da177e4SLinus Torvalds {
13041da177e4SLinus Torvalds 	struct priority_group *pg;
13051da177e4SLinus Torvalds 	unsigned pgnum;
130631998ef1SMikulas Patocka 	char dummy;
13071da177e4SLinus Torvalds 
130831998ef1SMikulas Patocka 	if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1309cc5bd925Stang.junhui 	    !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
13101da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to bypass_pg");
13111da177e4SLinus Torvalds 		return -EINVAL;
13121da177e4SLinus Torvalds 	}
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
13151da177e4SLinus Torvalds 		if (!--pgnum)
13161da177e4SLinus Torvalds 			break;
13171da177e4SLinus Torvalds 	}
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds 	bypass_pg(m, pg, bypassed);
13201da177e4SLinus Torvalds 	return 0;
13211da177e4SLinus Torvalds }
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds /*
1324c9e45581SDave Wysochanski  * Should we retry pg_init immediately?
1325c9e45581SDave Wysochanski  */
1326be7d31ccSMike Snitzer static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1327c9e45581SDave Wysochanski {
1328c9e45581SDave Wysochanski 	unsigned long flags;
1329be7d31ccSMike Snitzer 	bool limit_reached = false;
1330c9e45581SDave Wysochanski 
1331c9e45581SDave Wysochanski 	spin_lock_irqsave(&m->lock, flags);
1332c9e45581SDave Wysochanski 
133391e968aaSMike Snitzer 	if (atomic_read(&m->pg_init_count) <= m->pg_init_retries &&
133491e968aaSMike Snitzer 	    !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
1335518257b1SMike Snitzer 		set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
1336c9e45581SDave Wysochanski 	else
1337be7d31ccSMike Snitzer 		limit_reached = true;
1338c9e45581SDave Wysochanski 
1339c9e45581SDave Wysochanski 	spin_unlock_irqrestore(&m->lock, flags);
1340c9e45581SDave Wysochanski 
1341c9e45581SDave Wysochanski 	return limit_reached;
1342c9e45581SDave Wysochanski }
1343c9e45581SDave Wysochanski 
13443ae31f6aSChandra Seetharaman static void pg_init_done(void *data, int errors)
1345cfae5c9bSChandra Seetharaman {
134683c0d5d5SMoger, Babu 	struct pgpath *pgpath = data;
1347cfae5c9bSChandra Seetharaman 	struct priority_group *pg = pgpath->pg;
1348cfae5c9bSChandra Seetharaman 	struct multipath *m = pg->m;
1349cfae5c9bSChandra Seetharaman 	unsigned long flags;
1350be7d31ccSMike Snitzer 	bool delay_retry = false;
1351cfae5c9bSChandra Seetharaman 
1352cfae5c9bSChandra Seetharaman 	/* device or driver problems */
1353cfae5c9bSChandra Seetharaman 	switch (errors) {
1354cfae5c9bSChandra Seetharaman 	case SCSI_DH_OK:
1355cfae5c9bSChandra Seetharaman 		break;
1356cfae5c9bSChandra Seetharaman 	case SCSI_DH_NOSYS:
1357cfae5c9bSChandra Seetharaman 		if (!m->hw_handler_name) {
1358cfae5c9bSChandra Seetharaman 			errors = 0;
1359cfae5c9bSChandra Seetharaman 			break;
1360cfae5c9bSChandra Seetharaman 		}
1361f7b934c8SMoger, Babu 		DMERR("Could not failover the device: Handler scsi_dh_%s "
1362f7b934c8SMoger, Babu 		      "Error %d.", m->hw_handler_name, errors);
1363cfae5c9bSChandra Seetharaman 		/*
1364cfae5c9bSChandra Seetharaman 		 * Fail path for now, so we do not ping pong
1365cfae5c9bSChandra Seetharaman 		 */
1366cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1367cfae5c9bSChandra Seetharaman 		break;
1368cfae5c9bSChandra Seetharaman 	case SCSI_DH_DEV_TEMP_BUSY:
1369cfae5c9bSChandra Seetharaman 		/*
1370cfae5c9bSChandra Seetharaman 		 * Probably doing something like FW upgrade on the
1371cfae5c9bSChandra Seetharaman 		 * controller so try the other pg.
1372cfae5c9bSChandra Seetharaman 		 */
1373be7d31ccSMike Snitzer 		bypass_pg(m, pg, true);
1374cfae5c9bSChandra Seetharaman 		break;
1375cfae5c9bSChandra Seetharaman 	case SCSI_DH_RETRY:
13764e2d19e4SChandra Seetharaman 		/* Wait before retrying. */
13774e2d19e4SChandra Seetharaman 		delay_retry = 1;
13787b06e09aSBart Van Assche 		/* fall through */
1379cfae5c9bSChandra Seetharaman 	case SCSI_DH_IMM_RETRY:
1380cfae5c9bSChandra Seetharaman 	case SCSI_DH_RES_TEMP_UNAVAIL:
1381cfae5c9bSChandra Seetharaman 		if (pg_init_limit_reached(m, pgpath))
1382cfae5c9bSChandra Seetharaman 			fail_path(pgpath);
1383cfae5c9bSChandra Seetharaman 		errors = 0;
1384cfae5c9bSChandra Seetharaman 		break;
1385ec31f3f7SMike Snitzer 	case SCSI_DH_DEV_OFFLINED:
1386cfae5c9bSChandra Seetharaman 	default:
1387cfae5c9bSChandra Seetharaman 		/*
1388cfae5c9bSChandra Seetharaman 		 * We probably do not want to fail the path for a device
1389cfae5c9bSChandra Seetharaman 		 * error, but this is what the old dm did. In future
1390cfae5c9bSChandra Seetharaman 		 * patches we can do more advanced handling.
1391cfae5c9bSChandra Seetharaman 		 */
1392cfae5c9bSChandra Seetharaman 		fail_path(pgpath);
1393cfae5c9bSChandra Seetharaman 	}
1394cfae5c9bSChandra Seetharaman 
1395cfae5c9bSChandra Seetharaman 	spin_lock_irqsave(&m->lock, flags);
1396cfae5c9bSChandra Seetharaman 	if (errors) {
1397e54f77ddSChandra Seetharaman 		if (pgpath == m->current_pgpath) {
1398cfae5c9bSChandra Seetharaman 			DMERR("Could not failover device. Error %d.", errors);
1399cfae5c9bSChandra Seetharaman 			m->current_pgpath = NULL;
1400cfae5c9bSChandra Seetharaman 			m->current_pg = NULL;
1401e54f77ddSChandra Seetharaman 		}
1402518257b1SMike Snitzer 	} else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
1403be7d31ccSMike Snitzer 		pg->bypassed = false;
1404cfae5c9bSChandra Seetharaman 
140591e968aaSMike Snitzer 	if (atomic_dec_return(&m->pg_init_in_progress) > 0)
1406d0259bf0SKiyoshi Ueda 		/* Activations of other paths are still on going */
1407d0259bf0SKiyoshi Ueda 		goto out;
1408d0259bf0SKiyoshi Ueda 
1409518257b1SMike Snitzer 	if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
1410518257b1SMike Snitzer 		if (delay_retry)
1411518257b1SMike Snitzer 			set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1412518257b1SMike Snitzer 		else
1413518257b1SMike Snitzer 			clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1414518257b1SMike Snitzer 
14153e9f1be1SHannes Reinecke 		if (__pg_init_all_paths(m))
14163e9f1be1SHannes Reinecke 			goto out;
14173e9f1be1SHannes Reinecke 	}
1418518257b1SMike Snitzer 	clear_bit(MPATHF_QUEUE_IO, &m->flags);
1419d0259bf0SKiyoshi Ueda 
14207e48c768SMike Snitzer 	process_queued_io_list(m);
142176e33fe4SMike Snitzer 
14222bded7bdSKiyoshi Ueda 	/*
14232bded7bdSKiyoshi Ueda 	 * Wake up any thread waiting to suspend.
14242bded7bdSKiyoshi Ueda 	 */
14252bded7bdSKiyoshi Ueda 	wake_up(&m->pg_init_wait);
14262bded7bdSKiyoshi Ueda 
1427d0259bf0SKiyoshi Ueda out:
1428cfae5c9bSChandra Seetharaman 	spin_unlock_irqrestore(&m->lock, flags);
1429cfae5c9bSChandra Seetharaman }
1430cfae5c9bSChandra Seetharaman 
143189bfce76SBart Van Assche static void activate_or_offline_path(struct pgpath *pgpath)
1432bab7cfc7SChandra Seetharaman {
1433f10e06b7SMike Snitzer 	struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1434bab7cfc7SChandra Seetharaman 
1435f10e06b7SMike Snitzer 	if (pgpath->is_active && !blk_queue_dying(q))
1436f10e06b7SMike Snitzer 		scsi_dh_activate(q, pg_init_done, pgpath);
14373a017509SHannes Reinecke 	else
14383a017509SHannes Reinecke 		pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
1439bab7cfc7SChandra Seetharaman }
1440bab7cfc7SChandra Seetharaman 
144189bfce76SBart Van Assche static void activate_path_work(struct work_struct *work)
144289bfce76SBart Van Assche {
144389bfce76SBart Van Assche 	struct pgpath *pgpath =
144489bfce76SBart Van Assche 		container_of(work, struct pgpath, activate_path.work);
144589bfce76SBart Van Assche 
144689bfce76SBart Van Assche 	activate_or_offline_path(pgpath);
144789bfce76SBart Van Assche }
144889bfce76SBart Van Assche 
14492a842acaSChristoph Hellwig static int noretry_error(blk_status_t error)
14507e782af5SHannes Reinecke {
14517e782af5SHannes Reinecke 	switch (error) {
14522a842acaSChristoph Hellwig 	case BLK_STS_NOTSUPP:
14532a842acaSChristoph Hellwig 	case BLK_STS_NOSPC:
14542a842acaSChristoph Hellwig 	case BLK_STS_TARGET:
14552a842acaSChristoph Hellwig 	case BLK_STS_NEXUS:
14562a842acaSChristoph Hellwig 	case BLK_STS_MEDIUM:
14577e782af5SHannes Reinecke 		return 1;
14587e782af5SHannes Reinecke 	}
14597e782af5SHannes Reinecke 
14607e782af5SHannes Reinecke 	/* Anything else could be a path failure, so should be retried */
14617e782af5SHannes Reinecke 	return 0;
14627e782af5SHannes Reinecke }
14637e782af5SHannes Reinecke 
1464b79f10eeSChristoph Hellwig static int multipath_end_io(struct dm_target *ti, struct request *clone,
14652a842acaSChristoph Hellwig 			    blk_status_t error, union map_info *map_context)
14661da177e4SLinus Torvalds {
1467b79f10eeSChristoph Hellwig 	struct dm_mpath_io *mpio = get_mpio(map_context);
1468b79f10eeSChristoph Hellwig 	struct pgpath *pgpath = mpio->pgpath;
14697ed8578aSChristoph Hellwig 	int r = DM_ENDIO_DONE;
1470b79f10eeSChristoph Hellwig 
1471f40c67f0SKiyoshi Ueda 	/*
1472f40c67f0SKiyoshi Ueda 	 * We don't queue any clone request inside the multipath target
1473f40c67f0SKiyoshi Ueda 	 * during end I/O handling, since those clone requests don't have
1474f40c67f0SKiyoshi Ueda 	 * bio clones.  If we queue them inside the multipath target,
1475f40c67f0SKiyoshi Ueda 	 * we need to make bio clones, that requires memory allocation.
14764cc96131SMike Snitzer 	 * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests
1477f40c67f0SKiyoshi Ueda 	 *  don't have bio clones.)
1478f40c67f0SKiyoshi Ueda 	 * Instead of queueing the clone request here, we queue the original
1479f40c67f0SKiyoshi Ueda 	 * request into dm core, which will remake a clone request and
1480f40c67f0SKiyoshi Ueda 	 * clone bios for it and resubmit it later.
1481f40c67f0SKiyoshi Ueda 	 */
1482b79f10eeSChristoph Hellwig 	if (error && !noretry_error(error)) {
1483b79f10eeSChristoph Hellwig 		struct multipath *m = ti->private;
14841da177e4SLinus Torvalds 
14857ed8578aSChristoph Hellwig 		r = DM_ENDIO_REQUEUE;
14861da177e4SLinus Torvalds 
1487b79f10eeSChristoph Hellwig 		if (pgpath)
1488b79f10eeSChristoph Hellwig 			fail_path(pgpath);
14891da177e4SLinus Torvalds 
1490ca5beb76SBart Van Assche 		if (atomic_read(&m->nr_valid_paths) == 0 &&
14917ed8578aSChristoph Hellwig 		    !test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
14922a842acaSChristoph Hellwig 			if (error == BLK_STS_IOERR)
149318a482f5SChristoph Hellwig 				dm_report_EIO(m);
14947ed8578aSChristoph Hellwig 			/* complete with the original error */
14957ed8578aSChristoph Hellwig 			r = DM_ENDIO_DONE;
14967ed8578aSChristoph Hellwig 		}
14971da177e4SLinus Torvalds 	}
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds 	if (pgpath) {
1500b79f10eeSChristoph Hellwig 		struct path_selector *ps = &pgpath->pg->ps;
1501b79f10eeSChristoph Hellwig 
15021da177e4SLinus Torvalds 		if (ps->type->end_io)
150302ab823fSKiyoshi Ueda 			ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
15041da177e4SLinus Torvalds 	}
15051da177e4SLinus Torvalds 
15067ed8578aSChristoph Hellwig 	return r;
15071da177e4SLinus Torvalds }
15081da177e4SLinus Torvalds 
15094e4cbee9SChristoph Hellwig static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone,
15104e4cbee9SChristoph Hellwig 		blk_status_t *error)
151176e33fe4SMike Snitzer {
151214ef1e48SChristoph Hellwig 	struct multipath *m = ti->private;
151314ef1e48SChristoph Hellwig 	struct dm_mpath_io *mpio = get_mpio_from_bio(clone);
151414ef1e48SChristoph Hellwig 	struct pgpath *pgpath = mpio->pgpath;
151576e33fe4SMike Snitzer 	unsigned long flags;
15161be56909SChristoph Hellwig 	int r = DM_ENDIO_DONE;
151776e33fe4SMike Snitzer 
15184e4cbee9SChristoph Hellwig 	if (!*error || noretry_error(*error))
151914ef1e48SChristoph Hellwig 		goto done;
152076e33fe4SMike Snitzer 
152114ef1e48SChristoph Hellwig 	if (pgpath)
152214ef1e48SChristoph Hellwig 		fail_path(pgpath);
152376e33fe4SMike Snitzer 
1524ca5beb76SBart Van Assche 	if (atomic_read(&m->nr_valid_paths) == 0 &&
152518a482f5SChristoph Hellwig 	    !test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
152618a482f5SChristoph Hellwig 		dm_report_EIO(m);
15274e4cbee9SChristoph Hellwig 		*error = BLK_STS_IOERR;
152814ef1e48SChristoph Hellwig 		goto done;
152918a482f5SChristoph Hellwig 	}
153076e33fe4SMike Snitzer 
153176e33fe4SMike Snitzer 	/* Queue for the daemon to resubmit */
1532bf661be1SMike Snitzer 	dm_bio_restore(get_bio_details_from_bio(clone), clone);
153376e33fe4SMike Snitzer 
153476e33fe4SMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
153576e33fe4SMike Snitzer 	bio_list_add(&m->queued_bios, clone);
153676e33fe4SMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
153776e33fe4SMike Snitzer 	if (!test_bit(MPATHF_QUEUE_IO, &m->flags))
153876e33fe4SMike Snitzer 		queue_work(kmultipathd, &m->process_queued_bios);
153976e33fe4SMike Snitzer 
15401be56909SChristoph Hellwig 	r = DM_ENDIO_INCOMPLETE;
154114ef1e48SChristoph Hellwig done:
154276e33fe4SMike Snitzer 	if (pgpath) {
154314ef1e48SChristoph Hellwig 		struct path_selector *ps = &pgpath->pg->ps;
154414ef1e48SChristoph Hellwig 
154576e33fe4SMike Snitzer 		if (ps->type->end_io)
154676e33fe4SMike Snitzer 			ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
154776e33fe4SMike Snitzer 	}
154876e33fe4SMike Snitzer 
15491be56909SChristoph Hellwig 	return r;
155076e33fe4SMike Snitzer }
155176e33fe4SMike Snitzer 
15521da177e4SLinus Torvalds /*
15531da177e4SLinus Torvalds  * Suspend can't complete until all the I/O is processed so if
1554436d4108SAlasdair G Kergon  * the last path fails we must error any remaining I/O.
1555436d4108SAlasdair G Kergon  * Note that if the freeze_bdev fails while suspending, the
1556436d4108SAlasdair G Kergon  * queue_if_no_path state is lost - userspace should reset it.
15571da177e4SLinus Torvalds  */
15581da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti)
15591da177e4SLinus Torvalds {
15607943bd6dSMike Snitzer 	struct multipath *m = ti->private;
15611da177e4SLinus Torvalds 
1562be7d31ccSMike Snitzer 	queue_if_no_path(m, false, true);
15631da177e4SLinus Torvalds }
15641da177e4SLinus Torvalds 
15656df400abSKiyoshi Ueda static void multipath_postsuspend(struct dm_target *ti)
15666df400abSKiyoshi Ueda {
15676380f26fSMike Anderson 	struct multipath *m = ti->private;
15686380f26fSMike Anderson 
15696380f26fSMike Anderson 	mutex_lock(&m->work_mutex);
15702bded7bdSKiyoshi Ueda 	flush_multipath_work(m);
15716380f26fSMike Anderson 	mutex_unlock(&m->work_mutex);
15726df400abSKiyoshi Ueda }
15736df400abSKiyoshi Ueda 
1574436d4108SAlasdair G Kergon /*
1575436d4108SAlasdair G Kergon  * Restore the queue_if_no_path setting.
1576436d4108SAlasdair G Kergon  */
15771da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti)
15781da177e4SLinus Torvalds {
15797943bd6dSMike Snitzer 	struct multipath *m = ti->private;
15801814f2e3SMike Snitzer 	unsigned long flags;
15811da177e4SLinus Torvalds 
15821814f2e3SMike Snitzer 	spin_lock_irqsave(&m->lock, flags);
1583*5307e2adSLukas Wunner 	assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags,
1584*5307e2adSLukas Wunner 		   test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags));
15851814f2e3SMike Snitzer 	spin_unlock_irqrestore(&m->lock, flags);
15861da177e4SLinus Torvalds }
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds /*
15891da177e4SLinus Torvalds  * Info output has the following format:
15901da177e4SLinus Torvalds  * num_multipath_feature_args [multipath_feature_args]*
15911da177e4SLinus Torvalds  * num_handler_status_args [handler_status_args]*
15921da177e4SLinus Torvalds  * num_groups init_group_number
15931da177e4SLinus Torvalds  *            [A|D|E num_ps_status_args [ps_status_args]*
15941da177e4SLinus Torvalds  *             num_paths num_selector_args
15951da177e4SLinus Torvalds  *             [path_dev A|F fail_count [selector_args]* ]+ ]+
15961da177e4SLinus Torvalds  *
15971da177e4SLinus Torvalds  * Table output has the following format (identical to the constructor string):
15981da177e4SLinus Torvalds  * num_feature_args [features_args]*
15991da177e4SLinus Torvalds  * num_handler_args hw_handler [hw_handler_args]*
16001da177e4SLinus Torvalds  * num_groups init_group_number
16011da177e4SLinus Torvalds  *     [priority selector-name num_ps_args [ps_args]*
16021da177e4SLinus Torvalds  *      num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
16031da177e4SLinus Torvalds  */
1604fd7c092eSMikulas Patocka static void multipath_status(struct dm_target *ti, status_type_t type,
16051f4e0ff0SAlasdair G Kergon 			     unsigned status_flags, char *result, unsigned maxlen)
16061da177e4SLinus Torvalds {
16071da177e4SLinus Torvalds 	int sz = 0;
16081da177e4SLinus Torvalds 	unsigned long flags;
16097943bd6dSMike Snitzer 	struct multipath *m = ti->private;
16101da177e4SLinus Torvalds 	struct priority_group *pg;
16111da177e4SLinus Torvalds 	struct pgpath *p;
16121da177e4SLinus Torvalds 	unsigned pg_num;
16131da177e4SLinus Torvalds 	char state;
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds 	/* Features */
16181da177e4SLinus Torvalds 	if (type == STATUSTYPE_INFO)
161991e968aaSMike Snitzer 		DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags),
162091e968aaSMike Snitzer 		       atomic_read(&m->pg_init_count));
1621c9e45581SDave Wysochanski 	else {
1622518257b1SMike Snitzer 		DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) +
16234e2d19e4SChandra Seetharaman 			      (m->pg_init_retries > 0) * 2 +
1624a58a935dSMike Snitzer 			      (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
1625e83068a5SMike Snitzer 			      test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) +
1626e83068a5SMike Snitzer 			      (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2);
1627e83068a5SMike Snitzer 
1628518257b1SMike Snitzer 		if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
1629c9e45581SDave Wysochanski 			DMEMIT("queue_if_no_path ");
1630c9e45581SDave Wysochanski 		if (m->pg_init_retries)
1631c9e45581SDave Wysochanski 			DMEMIT("pg_init_retries %u ", m->pg_init_retries);
16324e2d19e4SChandra Seetharaman 		if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
16334e2d19e4SChandra Seetharaman 			DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
1634518257b1SMike Snitzer 		if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags))
1635a58a935dSMike Snitzer 			DMEMIT("retain_attached_hw_handler ");
1636e83068a5SMike Snitzer 		if (m->queue_mode != DM_TYPE_REQUEST_BASED) {
1637e83068a5SMike Snitzer 			switch(m->queue_mode) {
1638e83068a5SMike Snitzer 			case DM_TYPE_BIO_BASED:
1639e83068a5SMike Snitzer 				DMEMIT("queue_mode bio ");
1640e83068a5SMike Snitzer 				break;
1641e83068a5SMike Snitzer 			case DM_TYPE_MQ_REQUEST_BASED:
1642e83068a5SMike Snitzer 				DMEMIT("queue_mode mq ");
1643e83068a5SMike Snitzer 				break;
16447e0d574fSBart Van Assche 			default:
16457e0d574fSBart Van Assche 				WARN_ON_ONCE(true);
16467e0d574fSBart Van Assche 				break;
1647e83068a5SMike Snitzer 			}
1648e83068a5SMike Snitzer 		}
1649c9e45581SDave Wysochanski 	}
16501da177e4SLinus Torvalds 
1651cfae5c9bSChandra Seetharaman 	if (!m->hw_handler_name || type == STATUSTYPE_INFO)
16521da177e4SLinus Torvalds 		DMEMIT("0 ");
16531da177e4SLinus Torvalds 	else
1654cfae5c9bSChandra Seetharaman 		DMEMIT("1 %s ", m->hw_handler_name);
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds 	DMEMIT("%u ", m->nr_priority_groups);
16571da177e4SLinus Torvalds 
16581da177e4SLinus Torvalds 	if (m->next_pg)
16591da177e4SLinus Torvalds 		pg_num = m->next_pg->pg_num;
16601da177e4SLinus Torvalds 	else if (m->current_pg)
16611da177e4SLinus Torvalds 		pg_num = m->current_pg->pg_num;
16621da177e4SLinus Torvalds 	else
1663a490a07aSMike Snitzer 		pg_num = (m->nr_priority_groups ? 1 : 0);
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds 	DMEMIT("%u ", pg_num);
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 	switch (type) {
16681da177e4SLinus Torvalds 	case STATUSTYPE_INFO:
16691da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
16701da177e4SLinus Torvalds 			if (pg->bypassed)
16711da177e4SLinus Torvalds 				state = 'D';	/* Disabled */
16721da177e4SLinus Torvalds 			else if (pg == m->current_pg)
16731da177e4SLinus Torvalds 				state = 'A';	/* Currently Active */
16741da177e4SLinus Torvalds 			else
16751da177e4SLinus Torvalds 				state = 'E';	/* Enabled */
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds 			DMEMIT("%c ", state);
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds 			if (pg->ps.type->status)
16801da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
16811da177e4SLinus Torvalds 							  result + sz,
16821da177e4SLinus Torvalds 							  maxlen - sz);
16831da177e4SLinus Torvalds 			else
16841da177e4SLinus Torvalds 				DMEMIT("0 ");
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
16871da177e4SLinus Torvalds 			       pg->ps.type->info_args);
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
16901da177e4SLinus Torvalds 				DMEMIT("%s %s %u ", p->path.dev->name,
16916680073dSKiyoshi Ueda 				       p->is_active ? "A" : "F",
16921da177e4SLinus Torvalds 				       p->fail_count);
16931da177e4SLinus Torvalds 				if (pg->ps.type->status)
16941da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
16951da177e4SLinus Torvalds 					      &p->path, type, result + sz,
16961da177e4SLinus Torvalds 					      maxlen - sz);
16971da177e4SLinus Torvalds 			}
16981da177e4SLinus Torvalds 		}
16991da177e4SLinus Torvalds 		break;
17001da177e4SLinus Torvalds 
17011da177e4SLinus Torvalds 	case STATUSTYPE_TABLE:
17021da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
17031da177e4SLinus Torvalds 			DMEMIT("%s ", pg->ps.type->name);
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 			if (pg->ps.type->status)
17061da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
17071da177e4SLinus Torvalds 							  result + sz,
17081da177e4SLinus Torvalds 							  maxlen - sz);
17091da177e4SLinus Torvalds 			else
17101da177e4SLinus Torvalds 				DMEMIT("0 ");
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
17131da177e4SLinus Torvalds 			       pg->ps.type->table_args);
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
17161da177e4SLinus Torvalds 				DMEMIT("%s ", p->path.dev->name);
17171da177e4SLinus Torvalds 				if (pg->ps.type->status)
17181da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
17191da177e4SLinus Torvalds 					      &p->path, type, result + sz,
17201da177e4SLinus Torvalds 					      maxlen - sz);
17211da177e4SLinus Torvalds 			}
17221da177e4SLinus Torvalds 		}
17231da177e4SLinus Torvalds 		break;
17241da177e4SLinus Torvalds 	}
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
17271da177e4SLinus Torvalds }
17281da177e4SLinus Torvalds 
17291da177e4SLinus Torvalds static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
17301da177e4SLinus Torvalds {
17316380f26fSMike Anderson 	int r = -EINVAL;
17321da177e4SLinus Torvalds 	struct dm_dev *dev;
17337943bd6dSMike Snitzer 	struct multipath *m = ti->private;
17341da177e4SLinus Torvalds 	action_fn action;
17351da177e4SLinus Torvalds 
17366380f26fSMike Anderson 	mutex_lock(&m->work_mutex);
17376380f26fSMike Anderson 
1738c2f3d24bSKiyoshi Ueda 	if (dm_suspended(ti)) {
1739c2f3d24bSKiyoshi Ueda 		r = -EBUSY;
1740c2f3d24bSKiyoshi Ueda 		goto out;
1741c2f3d24bSKiyoshi Ueda 	}
1742c2f3d24bSKiyoshi Ueda 
17431da177e4SLinus Torvalds 	if (argc == 1) {
1744498f0103SMike Snitzer 		if (!strcasecmp(argv[0], "queue_if_no_path")) {
1745be7d31ccSMike Snitzer 			r = queue_if_no_path(m, true, false);
17466380f26fSMike Anderson 			goto out;
1747498f0103SMike Snitzer 		} else if (!strcasecmp(argv[0], "fail_if_no_path")) {
1748be7d31ccSMike Snitzer 			r = queue_if_no_path(m, false, false);
17496380f26fSMike Anderson 			goto out;
17506380f26fSMike Anderson 		}
17511da177e4SLinus Torvalds 	}
17521da177e4SLinus Torvalds 
17536380f26fSMike Anderson 	if (argc != 2) {
1754a356e426SJose Castillo 		DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
17556380f26fSMike Anderson 		goto out;
17566380f26fSMike Anderson 	}
17571da177e4SLinus Torvalds 
1758498f0103SMike Snitzer 	if (!strcasecmp(argv[0], "disable_group")) {
1759be7d31ccSMike Snitzer 		r = bypass_pg_num(m, argv[1], true);
17606380f26fSMike Anderson 		goto out;
1761498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "enable_group")) {
1762be7d31ccSMike Snitzer 		r = bypass_pg_num(m, argv[1], false);
17636380f26fSMike Anderson 		goto out;
1764498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "switch_group")) {
17656380f26fSMike Anderson 		r = switch_pg_num(m, argv[1]);
17666380f26fSMike Anderson 		goto out;
1767498f0103SMike Snitzer 	} else if (!strcasecmp(argv[0], "reinstate_path"))
17681da177e4SLinus Torvalds 		action = reinstate_path;
1769498f0103SMike Snitzer 	else if (!strcasecmp(argv[0], "fail_path"))
17701da177e4SLinus Torvalds 		action = fail_path;
17716380f26fSMike Anderson 	else {
1772a356e426SJose Castillo 		DMWARN("Unrecognised multipath message received: %s", argv[0]);
17736380f26fSMike Anderson 		goto out;
17746380f26fSMike Anderson 	}
17751da177e4SLinus Torvalds 
17768215d6ecSNikanth Karthikesan 	r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
17771da177e4SLinus Torvalds 	if (r) {
177872d94861SAlasdair G Kergon 		DMWARN("message: error getting device %s",
17791da177e4SLinus Torvalds 		       argv[1]);
17806380f26fSMike Anderson 		goto out;
17811da177e4SLinus Torvalds 	}
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 	r = action_dev(m, dev, action);
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds 	dm_put_device(ti, dev);
17861da177e4SLinus Torvalds 
17876380f26fSMike Anderson out:
17886380f26fSMike Anderson 	mutex_unlock(&m->work_mutex);
17891da177e4SLinus Torvalds 	return r;
17901da177e4SLinus Torvalds }
17911da177e4SLinus Torvalds 
1792e56f81e0SChristoph Hellwig static int multipath_prepare_ioctl(struct dm_target *ti,
1793e56f81e0SChristoph Hellwig 		struct block_device **bdev, fmode_t *mode)
17949af4aa30SMilan Broz {
179535991652SMikulas Patocka 	struct multipath *m = ti->private;
17962da1610aSMike Snitzer 	struct pgpath *current_pgpath;
179735991652SMikulas Patocka 	int r;
179835991652SMikulas Patocka 
17992da1610aSMike Snitzer 	current_pgpath = lockless_dereference(m->current_pgpath);
18002da1610aSMike Snitzer 	if (!current_pgpath)
18012da1610aSMike Snitzer 		current_pgpath = choose_pgpath(m, 0);
18029af4aa30SMilan Broz 
18032da1610aSMike Snitzer 	if (current_pgpath) {
1804518257b1SMike Snitzer 		if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) {
18052da1610aSMike Snitzer 			*bdev = current_pgpath->path.dev->bdev;
18062da1610aSMike Snitzer 			*mode = current_pgpath->path.dev->mode;
180743e43c9eSJunichi Nomura 			r = 0;
180843e43c9eSJunichi Nomura 		} else {
180943e43c9eSJunichi Nomura 			/* pg_init has not started or completed */
18106c182cd8SHannes Reinecke 			r = -ENOTCONN;
181143e43c9eSJunichi Nomura 		}
181243e43c9eSJunichi Nomura 	} else {
181343e43c9eSJunichi Nomura 		/* No path is available */
1814518257b1SMike Snitzer 		if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
181543e43c9eSJunichi Nomura 			r = -ENOTCONN;
181643e43c9eSJunichi Nomura 		else
18179af4aa30SMilan Broz 			r = -EIO;
181843e43c9eSJunichi Nomura 	}
18199af4aa30SMilan Broz 
18205bbbfdf6SJunichi Nomura 	if (r == -ENOTCONN) {
18212da1610aSMike Snitzer 		if (!lockless_dereference(m->current_pg)) {
18223e9f1be1SHannes Reinecke 			/* Path status changed, redo selection */
18232da1610aSMike Snitzer 			(void) choose_pgpath(m, 0);
18243e9f1be1SHannes Reinecke 		}
1825518257b1SMike Snitzer 		if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
18262da1610aSMike Snitzer 			pg_init_all_paths(m);
182763d832c3SHannes Reinecke 		dm_table_run_md_queue_async(m->ti->table);
18287e48c768SMike Snitzer 		process_queued_io_list(m);
18293e9f1be1SHannes Reinecke 	}
183035991652SMikulas Patocka 
1831e56f81e0SChristoph Hellwig 	/*
1832e56f81e0SChristoph Hellwig 	 * Only pass ioctls through if the device sizes match exactly.
1833e56f81e0SChristoph Hellwig 	 */
1834e56f81e0SChristoph Hellwig 	if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
1835e56f81e0SChristoph Hellwig 		return 1;
1836e56f81e0SChristoph Hellwig 	return r;
18379af4aa30SMilan Broz }
18389af4aa30SMilan Broz 
1839af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti,
1840af4874e0SMike Snitzer 				     iterate_devices_callout_fn fn, void *data)
1841af4874e0SMike Snitzer {
1842af4874e0SMike Snitzer 	struct multipath *m = ti->private;
1843af4874e0SMike Snitzer 	struct priority_group *pg;
1844af4874e0SMike Snitzer 	struct pgpath *p;
1845af4874e0SMike Snitzer 	int ret = 0;
1846af4874e0SMike Snitzer 
1847af4874e0SMike Snitzer 	list_for_each_entry(pg, &m->priority_groups, list) {
1848af4874e0SMike Snitzer 		list_for_each_entry(p, &pg->pgpaths, list) {
18495dea271bSMike Snitzer 			ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
1850af4874e0SMike Snitzer 			if (ret)
1851af4874e0SMike Snitzer 				goto out;
1852af4874e0SMike Snitzer 		}
1853af4874e0SMike Snitzer 	}
1854af4874e0SMike Snitzer 
1855af4874e0SMike Snitzer out:
1856af4874e0SMike Snitzer 	return ret;
1857af4874e0SMike Snitzer }
1858af4874e0SMike Snitzer 
18599f54cec5SMike Snitzer static int pgpath_busy(struct pgpath *pgpath)
1860f40c67f0SKiyoshi Ueda {
1861f40c67f0SKiyoshi Ueda 	struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1862f40c67f0SKiyoshi Ueda 
186352b09914SMike Snitzer 	return blk_lld_busy(q);
1864f40c67f0SKiyoshi Ueda }
1865f40c67f0SKiyoshi Ueda 
1866f40c67f0SKiyoshi Ueda /*
1867f40c67f0SKiyoshi Ueda  * We return "busy", only when we can map I/Os but underlying devices
1868f40c67f0SKiyoshi Ueda  * are busy (so even if we map I/Os now, the I/Os will wait on
1869f40c67f0SKiyoshi Ueda  * the underlying queue).
1870f40c67f0SKiyoshi Ueda  * In other words, if we want to kill I/Os or queue them inside us
1871f40c67f0SKiyoshi Ueda  * due to map unavailability, we don't return "busy".  Otherwise,
1872f40c67f0SKiyoshi Ueda  * dm core won't give us the I/Os and we can't do what we want.
1873f40c67f0SKiyoshi Ueda  */
1874f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti)
1875f40c67f0SKiyoshi Ueda {
1876be7d31ccSMike Snitzer 	bool busy = false, has_active = false;
1877f40c67f0SKiyoshi Ueda 	struct multipath *m = ti->private;
18782da1610aSMike Snitzer 	struct priority_group *pg, *next_pg;
1879f40c67f0SKiyoshi Ueda 	struct pgpath *pgpath;
1880f40c67f0SKiyoshi Ueda 
1881b88efd43SMike Snitzer 	/* pg_init in progress */
1882b88efd43SMike Snitzer 	if (atomic_read(&m->pg_init_in_progress))
18832da1610aSMike Snitzer 		return true;
18842da1610aSMike Snitzer 
1885b88efd43SMike Snitzer 	/* no paths available, for blk-mq: rely on IO mapping to delay requeue */
1886b88efd43SMike Snitzer 	if (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
1887b88efd43SMike Snitzer 		return (m->queue_mode != DM_TYPE_MQ_REQUEST_BASED);
1888b88efd43SMike Snitzer 
1889f40c67f0SKiyoshi Ueda 	/* Guess which priority_group will be used at next mapping time */
18902da1610aSMike Snitzer 	pg = lockless_dereference(m->current_pg);
18912da1610aSMike Snitzer 	next_pg = lockless_dereference(m->next_pg);
18922da1610aSMike Snitzer 	if (unlikely(!lockless_dereference(m->current_pgpath) && next_pg))
18932da1610aSMike Snitzer 		pg = next_pg;
18942da1610aSMike Snitzer 
18952da1610aSMike Snitzer 	if (!pg) {
1896f40c67f0SKiyoshi Ueda 		/*
1897f40c67f0SKiyoshi Ueda 		 * We don't know which pg will be used at next mapping time.
18982da1610aSMike Snitzer 		 * We don't call choose_pgpath() here to avoid to trigger
1899f40c67f0SKiyoshi Ueda 		 * pg_init just by busy checking.
1900f40c67f0SKiyoshi Ueda 		 * So we don't know whether underlying devices we will be using
1901f40c67f0SKiyoshi Ueda 		 * at next mapping time are busy or not. Just try mapping.
1902f40c67f0SKiyoshi Ueda 		 */
19032da1610aSMike Snitzer 		return busy;
19042da1610aSMike Snitzer 	}
1905f40c67f0SKiyoshi Ueda 
1906f40c67f0SKiyoshi Ueda 	/*
1907f40c67f0SKiyoshi Ueda 	 * If there is one non-busy active path at least, the path selector
1908f40c67f0SKiyoshi Ueda 	 * will be able to select it. So we consider such a pg as not busy.
1909f40c67f0SKiyoshi Ueda 	 */
1910be7d31ccSMike Snitzer 	busy = true;
19112da1610aSMike Snitzer 	list_for_each_entry(pgpath, &pg->pgpaths, list) {
1912f40c67f0SKiyoshi Ueda 		if (pgpath->is_active) {
1913be7d31ccSMike Snitzer 			has_active = true;
19149f54cec5SMike Snitzer 			if (!pgpath_busy(pgpath)) {
1915be7d31ccSMike Snitzer 				busy = false;
1916f40c67f0SKiyoshi Ueda 				break;
1917f40c67f0SKiyoshi Ueda 			}
1918f40c67f0SKiyoshi Ueda 		}
19192da1610aSMike Snitzer 	}
1920f40c67f0SKiyoshi Ueda 
19212da1610aSMike Snitzer 	if (!has_active) {
1922f40c67f0SKiyoshi Ueda 		/*
1923f40c67f0SKiyoshi Ueda 		 * No active path in this pg, so this pg won't be used and
1924f40c67f0SKiyoshi Ueda 		 * the current_pg will be changed at next mapping time.
1925f40c67f0SKiyoshi Ueda 		 * We need to try mapping to determine it.
1926f40c67f0SKiyoshi Ueda 		 */
1927be7d31ccSMike Snitzer 		busy = false;
19282da1610aSMike Snitzer 	}
1929f40c67f0SKiyoshi Ueda 
1930f40c67f0SKiyoshi Ueda 	return busy;
1931f40c67f0SKiyoshi Ueda }
1932f40c67f0SKiyoshi Ueda 
19331da177e4SLinus Torvalds /*-----------------------------------------------------------------
19341da177e4SLinus Torvalds  * Module setup
19351da177e4SLinus Torvalds  *---------------------------------------------------------------*/
19361da177e4SLinus Torvalds static struct target_type multipath_target = {
19371da177e4SLinus Torvalds 	.name = "multipath",
1938e83068a5SMike Snitzer 	.version = {1, 12, 0},
193916f12266SMike Snitzer 	.features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE,
19401da177e4SLinus Torvalds 	.module = THIS_MODULE,
19411da177e4SLinus Torvalds 	.ctr = multipath_ctr,
19421da177e4SLinus Torvalds 	.dtr = multipath_dtr,
1943e5863d9aSMike Snitzer 	.clone_and_map_rq = multipath_clone_and_map,
1944e5863d9aSMike Snitzer 	.release_clone_rq = multipath_release_clone,
1945f40c67f0SKiyoshi Ueda 	.rq_end_io = multipath_end_io,
194676e33fe4SMike Snitzer 	.map = multipath_map_bio,
194776e33fe4SMike Snitzer 	.end_io = multipath_end_io_bio,
194876e33fe4SMike Snitzer 	.presuspend = multipath_presuspend,
194976e33fe4SMike Snitzer 	.postsuspend = multipath_postsuspend,
195076e33fe4SMike Snitzer 	.resume = multipath_resume,
195176e33fe4SMike Snitzer 	.status = multipath_status,
195276e33fe4SMike Snitzer 	.message = multipath_message,
195376e33fe4SMike Snitzer 	.prepare_ioctl = multipath_prepare_ioctl,
195476e33fe4SMike Snitzer 	.iterate_devices = multipath_iterate_devices,
195576e33fe4SMike Snitzer 	.busy = multipath_busy,
195676e33fe4SMike Snitzer };
195776e33fe4SMike Snitzer 
19581da177e4SLinus Torvalds static int __init dm_multipath_init(void)
19591da177e4SLinus Torvalds {
19601da177e4SLinus Torvalds 	int r;
19611da177e4SLinus Torvalds 
19621da177e4SLinus Torvalds 	r = dm_register_target(&multipath_target);
19631da177e4SLinus Torvalds 	if (r < 0) {
196476e33fe4SMike Snitzer 		DMERR("request-based register failed %d", r);
1965ff658e9cSJohannes Thumshirn 		r = -EINVAL;
1966ff658e9cSJohannes Thumshirn 		goto bad_register_target;
19671da177e4SLinus Torvalds 	}
19681da177e4SLinus Torvalds 
19694d4d66abSTejun Heo 	kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
1970c557308eSAlasdair G Kergon 	if (!kmultipathd) {
19710cd33124SAlasdair G Kergon 		DMERR("failed to create workqueue kmpathd");
1972ff658e9cSJohannes Thumshirn 		r = -ENOMEM;
1973ff658e9cSJohannes Thumshirn 		goto bad_alloc_kmultipathd;
1974c557308eSAlasdair G Kergon 	}
1975c557308eSAlasdair G Kergon 
1976bab7cfc7SChandra Seetharaman 	/*
1977bab7cfc7SChandra Seetharaman 	 * A separate workqueue is used to handle the device handlers
1978bab7cfc7SChandra Seetharaman 	 * to avoid overloading existing workqueue. Overloading the
1979bab7cfc7SChandra Seetharaman 	 * old workqueue would also create a bottleneck in the
1980bab7cfc7SChandra Seetharaman 	 * path of the storage hardware device activation.
1981bab7cfc7SChandra Seetharaman 	 */
19824d4d66abSTejun Heo 	kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
19834d4d66abSTejun Heo 						  WQ_MEM_RECLAIM);
1984bab7cfc7SChandra Seetharaman 	if (!kmpath_handlerd) {
1985bab7cfc7SChandra Seetharaman 		DMERR("failed to create workqueue kmpath_handlerd");
1986ff658e9cSJohannes Thumshirn 		r = -ENOMEM;
1987ff658e9cSJohannes Thumshirn 		goto bad_alloc_kmpath_handlerd;
1988bab7cfc7SChandra Seetharaman 	}
1989bab7cfc7SChandra Seetharaman 
1990ff658e9cSJohannes Thumshirn 	return 0;
1991ff658e9cSJohannes Thumshirn 
1992ff658e9cSJohannes Thumshirn bad_alloc_kmpath_handlerd:
1993ff658e9cSJohannes Thumshirn 	destroy_workqueue(kmultipathd);
1994ff658e9cSJohannes Thumshirn bad_alloc_kmultipathd:
1995ff658e9cSJohannes Thumshirn 	dm_unregister_target(&multipath_target);
1996ff658e9cSJohannes Thumshirn bad_register_target:
19971da177e4SLinus Torvalds 	return r;
19981da177e4SLinus Torvalds }
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds static void __exit dm_multipath_exit(void)
20011da177e4SLinus Torvalds {
2002bab7cfc7SChandra Seetharaman 	destroy_workqueue(kmpath_handlerd);
2003c557308eSAlasdair G Kergon 	destroy_workqueue(kmultipathd);
2004c557308eSAlasdair G Kergon 
200510d3bd09SMikulas Patocka 	dm_unregister_target(&multipath_target);
20061da177e4SLinus Torvalds }
20071da177e4SLinus Torvalds 
20081da177e4SLinus Torvalds module_init(dm_multipath_init);
20091da177e4SLinus Torvalds module_exit(dm_multipath_exit);
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target");
20121da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
20131da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2014