xref: /openbmc/linux/drivers/md/dm.c (revision 115485e83f497fdf9b4bf779038cfe4e141292a9)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3784aae73SMilan Broz  * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This file is released under the GPL.
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include "dm.h"
951e5b2bdSMike Anderson #include "dm-uevent.h"
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/init.h>
121da177e4SLinus Torvalds #include <linux/module.h>
1348c9c27bSArjan van de Ven #include <linux/mutex.h>
141da177e4SLinus Torvalds #include <linux/moduleparam.h>
151da177e4SLinus Torvalds #include <linux/blkpg.h>
161da177e4SLinus Torvalds #include <linux/bio.h>
171da177e4SLinus Torvalds #include <linux/mempool.h>
181da177e4SLinus Torvalds #include <linux/slab.h>
191da177e4SLinus Torvalds #include <linux/idr.h>
203ac51e74SDarrick J. Wong #include <linux/hdreg.h>
213f77316dSKiyoshi Ueda #include <linux/delay.h>
22ffcc3936SMike Snitzer #include <linux/wait.h>
232eb6e1e3SKeith Busch #include <linux/kthread.h>
240ce65797SMike Snitzer #include <linux/ktime.h>
25de3ec86dSMike Snitzer #include <linux/elevator.h> /* for rq_end_sector() */
26bfebd1cdSMike Snitzer #include <linux/blk-mq.h>
2771cdb697SChristoph Hellwig #include <linux/pr.h>
2855782138SLi Zefan 
2955782138SLi Zefan #include <trace/events/block.h>
301da177e4SLinus Torvalds 
3172d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
3272d94861SAlasdair G Kergon 
3371a16736SNamhyung Kim #ifdef CONFIG_PRINTK
3471a16736SNamhyung Kim /*
3571a16736SNamhyung Kim  * ratelimit state to be used in DMXXX_LIMIT().
3671a16736SNamhyung Kim  */
3771a16736SNamhyung Kim DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
3871a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_INTERVAL,
3971a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_BURST);
4071a16736SNamhyung Kim EXPORT_SYMBOL(dm_ratelimit_state);
4171a16736SNamhyung Kim #endif
4271a16736SNamhyung Kim 
4360935eb2SMilan Broz /*
4460935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
4560935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
4660935eb2SMilan Broz  */
4760935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
4860935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
4960935eb2SMilan Broz 
501da177e4SLinus Torvalds static const char *_name = DM_NAME;
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds static unsigned int major = 0;
531da177e4SLinus Torvalds static unsigned int _major = 0;
541da177e4SLinus Torvalds 
55d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr);
56d15b774cSAlasdair G Kergon 
57f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
582c140a24SMikulas Patocka 
592c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w);
602c140a24SMikulas Patocka 
612c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
622c140a24SMikulas Patocka 
63acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue;
64acfe0ad7SMikulas Patocka 
651da177e4SLinus Torvalds /*
668fbf26adSKiyoshi Ueda  * For bio-based dm.
671da177e4SLinus Torvalds  * One of these is allocated per bio.
681da177e4SLinus Torvalds  */
691da177e4SLinus Torvalds struct dm_io {
701da177e4SLinus Torvalds 	struct mapped_device *md;
711da177e4SLinus Torvalds 	int error;
721da177e4SLinus Torvalds 	atomic_t io_count;
736ae2fa67SRichard Kennedy 	struct bio *bio;
743eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
75f88fb981SKiyoshi Ueda 	spinlock_t endio_lock;
76fd2ed4d2SMikulas Patocka 	struct dm_stats_aux stats_aux;
771da177e4SLinus Torvalds };
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /*
808fbf26adSKiyoshi Ueda  * For request-based dm.
818fbf26adSKiyoshi Ueda  * One of these is allocated per request.
828fbf26adSKiyoshi Ueda  */
838fbf26adSKiyoshi Ueda struct dm_rq_target_io {
848fbf26adSKiyoshi Ueda 	struct mapped_device *md;
858fbf26adSKiyoshi Ueda 	struct dm_target *ti;
861ae49ea2SMike Snitzer 	struct request *orig, *clone;
872eb6e1e3SKeith Busch 	struct kthread_work work;
888fbf26adSKiyoshi Ueda 	int error;
898fbf26adSKiyoshi Ueda 	union map_info info;
90e262f347SMikulas Patocka 	struct dm_stats_aux stats_aux;
91e262f347SMikulas Patocka 	unsigned long duration_jiffies;
92e262f347SMikulas Patocka 	unsigned n_sectors;
938fbf26adSKiyoshi Ueda };
948fbf26adSKiyoshi Ueda 
958fbf26adSKiyoshi Ueda /*
9694818742SKent Overstreet  * For request-based dm - the bio clones we allocate are embedded in these
9794818742SKent Overstreet  * structs.
9894818742SKent Overstreet  *
9994818742SKent Overstreet  * We allocate these with bio_alloc_bioset, using the front_pad parameter when
10094818742SKent Overstreet  * the bioset is created - this means the bio has to come at the end of the
10194818742SKent Overstreet  * struct.
1028fbf26adSKiyoshi Ueda  */
1038fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info {
1048fbf26adSKiyoshi Ueda 	struct bio *orig;
105cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio;
10694818742SKent Overstreet 	struct bio clone;
1078fbf26adSKiyoshi Ueda };
1088fbf26adSKiyoshi Ueda 
109ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
110ba61fdd1SJeff Mahoney 
1111da177e4SLinus Torvalds /*
1121da177e4SLinus Torvalds  * Bits for the md->flags field.
1131da177e4SLinus Torvalds  */
1141eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
1151da177e4SLinus Torvalds #define DMF_SUSPENDED 1
116aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
117fba9f90eSJeff Mahoney #define DMF_FREEING 3
1185c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
1192e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
1208ae12666SKent Overstreet #define DMF_DEFERRED_REMOVE 6
1218ae12666SKent Overstreet #define DMF_SUSPENDED_INTERNALLY 7
1221da177e4SLinus Torvalds 
123304f3f6aSMilan Broz /*
12483d5e5b0SMikulas Patocka  * A dummy definition to make RCU happy.
12583d5e5b0SMikulas Patocka  * struct dm_table should never be dereferenced in this file.
12683d5e5b0SMikulas Patocka  */
12783d5e5b0SMikulas Patocka struct dm_table {
12883d5e5b0SMikulas Patocka 	int undefined__;
12983d5e5b0SMikulas Patocka };
13083d5e5b0SMikulas Patocka 
13183d5e5b0SMikulas Patocka /*
132304f3f6aSMilan Broz  * Work processed by per-device workqueue.
133304f3f6aSMilan Broz  */
1341da177e4SLinus Torvalds struct mapped_device {
13583d5e5b0SMikulas Patocka 	struct srcu_struct io_barrier;
136e61290a4SDaniel Walker 	struct mutex suspend_lock;
1371da177e4SLinus Torvalds 	atomic_t holders;
1385c6bd75dSAlasdair G Kergon 	atomic_t open_count;
1391da177e4SLinus Torvalds 
1402a7faeb1SMikulas Patocka 	/*
1412a7faeb1SMikulas Patocka 	 * The current mapping.
1422a7faeb1SMikulas Patocka 	 * Use dm_get_live_table{_fast} or take suspend_lock for
1432a7faeb1SMikulas Patocka 	 * dereference.
1442a7faeb1SMikulas Patocka 	 */
1456fa99520SPranith Kumar 	struct dm_table __rcu *map;
1462a7faeb1SMikulas Patocka 
14786f1152bSBenjamin Marzinski 	struct list_head table_devices;
14886f1152bSBenjamin Marzinski 	struct mutex table_devices_lock;
14986f1152bSBenjamin Marzinski 
1501da177e4SLinus Torvalds 	unsigned long flags;
1511da177e4SLinus Torvalds 
152165125e1SJens Axboe 	struct request_queue *queue;
153*115485e8SMike Snitzer 	int numa_node_id;
154*115485e8SMike Snitzer 
155a5664dadSMike Snitzer 	unsigned type;
1564a0b4ddfSMike Snitzer 	/* Protect queue and type against concurrent access. */
157a5664dadSMike Snitzer 	struct mutex type_lock;
158a5664dadSMike Snitzer 
15916f12266SMike Snitzer 	struct dm_target *immutable_target;
16036a0456fSAlasdair G Kergon 	struct target_type *immutable_target_type;
16136a0456fSAlasdair G Kergon 
1621da177e4SLinus Torvalds 	struct gendisk *disk;
1637e51f257SMike Anderson 	char name[16];
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	void *interface_ptr;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	/*
1681da177e4SLinus Torvalds 	 * A list of ios that arrived while we were suspended.
1691da177e4SLinus Torvalds 	 */
170316d315bSNikanth Karthikesan 	atomic_t pending[2];
1711da177e4SLinus Torvalds 	wait_queue_head_t wait;
17253d5914fSMikulas Patocka 	struct work_struct work;
1731da177e4SLinus Torvalds 	struct bio_list deferred;
174022c2611SMikulas Patocka 	spinlock_t deferred_lock;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	/*
17729e4013dSTejun Heo 	 * Processing queue (flush)
178304f3f6aSMilan Broz 	 */
179304f3f6aSMilan Broz 	struct workqueue_struct *wq;
180304f3f6aSMilan Broz 
181304f3f6aSMilan Broz 	/*
1821da177e4SLinus Torvalds 	 * io objects are allocated from here.
1831da177e4SLinus Torvalds 	 */
1841da177e4SLinus Torvalds 	mempool_t *io_pool;
1851ae49ea2SMike Snitzer 	mempool_t *rq_pool;
1861da177e4SLinus Torvalds 
1879faf400fSStefan Bader 	struct bio_set *bs;
1889faf400fSStefan Bader 
1891da177e4SLinus Torvalds 	/*
1901da177e4SLinus Torvalds 	 * Event handling.
1911da177e4SLinus Torvalds 	 */
1921da177e4SLinus Torvalds 	atomic_t event_nr;
1931da177e4SLinus Torvalds 	wait_queue_head_t eventq;
1947a8c3d3bSMike Anderson 	atomic_t uevent_seq;
1957a8c3d3bSMike Anderson 	struct list_head uevent_list;
1967a8c3d3bSMike Anderson 	spinlock_t uevent_lock; /* Protect access to uevent_list */
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	/*
1991da177e4SLinus Torvalds 	 * freeze/thaw support require holding onto a super block
2001da177e4SLinus Torvalds 	 */
2011da177e4SLinus Torvalds 	struct super_block *frozen_sb;
202db8fef4fSMikulas Patocka 	struct block_device *bdev;
2033ac51e74SDarrick J. Wong 
2043ac51e74SDarrick J. Wong 	/* forced geometry settings */
2053ac51e74SDarrick J. Wong 	struct hd_geometry geometry;
206784aae73SMilan Broz 
2072995fa78SMikulas Patocka 	/* kobject and completion */
2082995fa78SMikulas Patocka 	struct dm_kobject_holder kobj_holder;
209be35f486SMikulas Patocka 
210d87f4c14STejun Heo 	/* zero-length flush that will be cloned and submitted to targets */
211d87f4c14STejun Heo 	struct bio flush_bio;
212fd2ed4d2SMikulas Patocka 
21396b26c8cSMikulas Patocka 	/* the number of internal suspends */
21496b26c8cSMikulas Patocka 	unsigned internal_suspend_count;
21596b26c8cSMikulas Patocka 
216fd2ed4d2SMikulas Patocka 	struct dm_stats stats;
2172eb6e1e3SKeith Busch 
2182eb6e1e3SKeith Busch 	struct kthread_worker kworker;
2192eb6e1e3SKeith Busch 	struct task_struct *kworker_task;
220de3ec86dSMike Snitzer 
221de3ec86dSMike Snitzer 	/* for request-based merge heuristic in dm_request_fn() */
2220ce65797SMike Snitzer 	unsigned seq_rq_merge_deadline_usecs;
223de3ec86dSMike Snitzer 	int last_rq_rw;
2240ce65797SMike Snitzer 	sector_t last_rq_pos;
2250ce65797SMike Snitzer 	ktime_t last_rq_start_time;
226bfebd1cdSMike Snitzer 
227bfebd1cdSMike Snitzer 	/* for blk-mq request-based DM support */
2281c357a1eSMike Snitzer 	struct blk_mq_tag_set *tag_set;
229591ddcfcSMike Snitzer 	bool use_blk_mq:1;
230591ddcfcSMike Snitzer 	bool init_tio_pdu:1;
2311da177e4SLinus Torvalds };
2321da177e4SLinus Torvalds 
23317e149b8SMike Snitzer #ifdef CONFIG_DM_MQ_DEFAULT
23417e149b8SMike Snitzer static bool use_blk_mq = true;
23517e149b8SMike Snitzer #else
23617e149b8SMike Snitzer static bool use_blk_mq = false;
23717e149b8SMike Snitzer #endif
23817e149b8SMike Snitzer 
239faad87dfSMike Snitzer #define DM_MQ_NR_HW_QUEUES 1
240faad87dfSMike Snitzer #define DM_MQ_QUEUE_DEPTH 2048
241*115485e8SMike Snitzer #define DM_NUMA_NODE NUMA_NO_NODE
242faad87dfSMike Snitzer 
243faad87dfSMike Snitzer static unsigned dm_mq_nr_hw_queues = DM_MQ_NR_HW_QUEUES;
244faad87dfSMike Snitzer static unsigned dm_mq_queue_depth = DM_MQ_QUEUE_DEPTH;
245*115485e8SMike Snitzer static int dm_numa_node = DM_NUMA_NODE;
246faad87dfSMike Snitzer 
24717e149b8SMike Snitzer bool dm_use_blk_mq(struct mapped_device *md)
24817e149b8SMike Snitzer {
24917e149b8SMike Snitzer 	return md->use_blk_mq;
25017e149b8SMike Snitzer }
251591ddcfcSMike Snitzer EXPORT_SYMBOL_GPL(dm_use_blk_mq);
25217e149b8SMike Snitzer 
253e6ee8c0bSKiyoshi Ueda /*
254e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
255e6ee8c0bSKiyoshi Ueda  */
256e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
257e6ee8c0bSKiyoshi Ueda 	mempool_t *io_pool;
2581ae49ea2SMike Snitzer 	mempool_t *rq_pool;
259e6ee8c0bSKiyoshi Ueda 	struct bio_set *bs;
260e6ee8c0bSKiyoshi Ueda };
261e6ee8c0bSKiyoshi Ueda 
26286f1152bSBenjamin Marzinski struct table_device {
26386f1152bSBenjamin Marzinski 	struct list_head list;
26486f1152bSBenjamin Marzinski 	atomic_t count;
26586f1152bSBenjamin Marzinski 	struct dm_dev dm_dev;
26686f1152bSBenjamin Marzinski };
26786f1152bSBenjamin Marzinski 
2686cfa5857SMike Snitzer #define RESERVED_BIO_BASED_IOS		16
2696cfa5857SMike Snitzer #define RESERVED_REQUEST_BASED_IOS	256
270f4790826SMike Snitzer #define RESERVED_MAX_IOS		1024
271e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
2728fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
2731ae49ea2SMike Snitzer static struct kmem_cache *_rq_cache;
27494818742SKent Overstreet 
275f4790826SMike Snitzer /*
276e8603136SMike Snitzer  * Bio-based DM's mempools' reserved IOs set by the user.
277e8603136SMike Snitzer  */
278e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
279e8603136SMike Snitzer 
280e8603136SMike Snitzer /*
281f4790826SMike Snitzer  * Request-based DM's mempools' reserved IOs set by the user.
282f4790826SMike Snitzer  */
283f4790826SMike Snitzer static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
284f4790826SMike Snitzer 
285*115485e8SMike Snitzer static int __dm_get_module_param_int(int *module_param, int min, int max)
286*115485e8SMike Snitzer {
287*115485e8SMike Snitzer 	int param = ACCESS_ONCE(*module_param);
288*115485e8SMike Snitzer 	int modified_param = 0;
289*115485e8SMike Snitzer 	bool modified = true;
290*115485e8SMike Snitzer 
291*115485e8SMike Snitzer 	if (param < min)
292*115485e8SMike Snitzer 		modified_param = min;
293*115485e8SMike Snitzer 	else if (param > max)
294*115485e8SMike Snitzer 		modified_param = max;
295*115485e8SMike Snitzer 	else
296*115485e8SMike Snitzer 		modified = false;
297*115485e8SMike Snitzer 
298*115485e8SMike Snitzer 	if (modified) {
299*115485e8SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
300*115485e8SMike Snitzer 		param = modified_param;
301*115485e8SMike Snitzer 	}
302*115485e8SMike Snitzer 
303*115485e8SMike Snitzer 	return param;
304*115485e8SMike Snitzer }
305*115485e8SMike Snitzer 
30609c2d531SMike Snitzer static unsigned __dm_get_module_param(unsigned *module_param,
307f4790826SMike Snitzer 				      unsigned def, unsigned max)
308f4790826SMike Snitzer {
30909c2d531SMike Snitzer 	unsigned param = ACCESS_ONCE(*module_param);
31009c2d531SMike Snitzer 	unsigned modified_param = 0;
311f4790826SMike Snitzer 
31209c2d531SMike Snitzer 	if (!param)
31309c2d531SMike Snitzer 		modified_param = def;
31409c2d531SMike Snitzer 	else if (param > max)
31509c2d531SMike Snitzer 		modified_param = max;
316f4790826SMike Snitzer 
31709c2d531SMike Snitzer 	if (modified_param) {
31809c2d531SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
31909c2d531SMike Snitzer 		param = modified_param;
320f4790826SMike Snitzer 	}
321f4790826SMike Snitzer 
32209c2d531SMike Snitzer 	return param;
323f4790826SMike Snitzer }
324f4790826SMike Snitzer 
325e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void)
326e8603136SMike Snitzer {
32709c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_bio_based_ios,
328e8603136SMike Snitzer 				     RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
329e8603136SMike Snitzer }
330e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
331e8603136SMike Snitzer 
332f4790826SMike Snitzer unsigned dm_get_reserved_rq_based_ios(void)
333f4790826SMike Snitzer {
33409c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_rq_based_ios,
335f4790826SMike Snitzer 				     RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
336f4790826SMike Snitzer }
337f4790826SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
338f4790826SMike Snitzer 
339faad87dfSMike Snitzer static unsigned dm_get_blk_mq_nr_hw_queues(void)
340faad87dfSMike Snitzer {
341faad87dfSMike Snitzer 	return __dm_get_module_param(&dm_mq_nr_hw_queues, 1, 32);
342faad87dfSMike Snitzer }
343faad87dfSMike Snitzer 
344faad87dfSMike Snitzer static unsigned dm_get_blk_mq_queue_depth(void)
345faad87dfSMike Snitzer {
346faad87dfSMike Snitzer 	return __dm_get_module_param(&dm_mq_queue_depth,
347faad87dfSMike Snitzer 				     DM_MQ_QUEUE_DEPTH, BLK_MQ_MAX_DEPTH);
348faad87dfSMike Snitzer }
349faad87dfSMike Snitzer 
350*115485e8SMike Snitzer static unsigned dm_get_numa_node(void)
351*115485e8SMike Snitzer {
352*115485e8SMike Snitzer 	return __dm_get_module_param_int(&dm_numa_node,
353*115485e8SMike Snitzer 					 DM_NUMA_NODE, num_online_nodes() - 1);
354*115485e8SMike Snitzer }
355*115485e8SMike Snitzer 
3561da177e4SLinus Torvalds static int __init local_init(void)
3571da177e4SLinus Torvalds {
35851157b4aSKiyoshi Ueda 	int r = -ENOMEM;
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
361028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
3621da177e4SLinus Torvalds 	if (!_io_cache)
36351157b4aSKiyoshi Ueda 		return r;
3641da177e4SLinus Torvalds 
3658fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
3668fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
367dba14160SMikulas Patocka 		goto out_free_io_cache;
3688fbf26adSKiyoshi Ueda 
369eca7ee6dSMike Snitzer 	_rq_cache = kmem_cache_create("dm_old_clone_request", sizeof(struct request),
3701ae49ea2SMike Snitzer 				      __alignof__(struct request), 0, NULL);
3711ae49ea2SMike Snitzer 	if (!_rq_cache)
3721ae49ea2SMike Snitzer 		goto out_free_rq_tio_cache;
3731ae49ea2SMike Snitzer 
37451e5b2bdSMike Anderson 	r = dm_uevent_init();
37551157b4aSKiyoshi Ueda 	if (r)
3761ae49ea2SMike Snitzer 		goto out_free_rq_cache;
37751e5b2bdSMike Anderson 
378acfe0ad7SMikulas Patocka 	deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
379acfe0ad7SMikulas Patocka 	if (!deferred_remove_workqueue) {
380acfe0ad7SMikulas Patocka 		r = -ENOMEM;
381acfe0ad7SMikulas Patocka 		goto out_uevent_exit;
382acfe0ad7SMikulas Patocka 	}
383acfe0ad7SMikulas Patocka 
3841da177e4SLinus Torvalds 	_major = major;
3851da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
38651157b4aSKiyoshi Ueda 	if (r < 0)
387acfe0ad7SMikulas Patocka 		goto out_free_workqueue;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	if (!_major)
3901da177e4SLinus Torvalds 		_major = r;
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	return 0;
39351157b4aSKiyoshi Ueda 
394acfe0ad7SMikulas Patocka out_free_workqueue:
395acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
39651157b4aSKiyoshi Ueda out_uevent_exit:
39751157b4aSKiyoshi Ueda 	dm_uevent_exit();
3981ae49ea2SMike Snitzer out_free_rq_cache:
3991ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
4008fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
4018fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
40251157b4aSKiyoshi Ueda out_free_io_cache:
40351157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
40451157b4aSKiyoshi Ueda 
40551157b4aSKiyoshi Ueda 	return r;
4061da177e4SLinus Torvalds }
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds static void local_exit(void)
4091da177e4SLinus Torvalds {
4102c140a24SMikulas Patocka 	flush_scheduled_work();
411acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
4122c140a24SMikulas Patocka 
4131ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
4148fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
4151da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
41600d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
41751e5b2bdSMike Anderson 	dm_uevent_exit();
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	_major = 0;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	DMINFO("cleaned up");
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds 
424b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
4251da177e4SLinus Torvalds 	local_init,
4261da177e4SLinus Torvalds 	dm_target_init,
4271da177e4SLinus Torvalds 	dm_linear_init,
4281da177e4SLinus Torvalds 	dm_stripe_init,
429952b3557SMikulas Patocka 	dm_io_init,
430945fa4d2SMikulas Patocka 	dm_kcopyd_init,
4311da177e4SLinus Torvalds 	dm_interface_init,
432fd2ed4d2SMikulas Patocka 	dm_statistics_init,
4331da177e4SLinus Torvalds };
4341da177e4SLinus Torvalds 
435b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
4361da177e4SLinus Torvalds 	local_exit,
4371da177e4SLinus Torvalds 	dm_target_exit,
4381da177e4SLinus Torvalds 	dm_linear_exit,
4391da177e4SLinus Torvalds 	dm_stripe_exit,
440952b3557SMikulas Patocka 	dm_io_exit,
441945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
4421da177e4SLinus Torvalds 	dm_interface_exit,
443fd2ed4d2SMikulas Patocka 	dm_statistics_exit,
4441da177e4SLinus Torvalds };
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds static int __init dm_init(void)
4471da177e4SLinus Torvalds {
4481da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	int r, i;
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
4531da177e4SLinus Torvalds 		r = _inits[i]();
4541da177e4SLinus Torvalds 		if (r)
4551da177e4SLinus Torvalds 			goto bad;
4561da177e4SLinus Torvalds 	}
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	return 0;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds       bad:
4611da177e4SLinus Torvalds 	while (i--)
4621da177e4SLinus Torvalds 		_exits[i]();
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	return r;
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds static void __exit dm_exit(void)
4681da177e4SLinus Torvalds {
4691da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	while (i--)
4721da177e4SLinus Torvalds 		_exits[i]();
473d15b774cSAlasdair G Kergon 
474d15b774cSAlasdair G Kergon 	/*
475d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
476d15b774cSAlasdair G Kergon 	 */
477d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds /*
4811da177e4SLinus Torvalds  * Block device functions
4821da177e4SLinus Torvalds  */
483432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
484432a212cSMike Anderson {
485432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
486432a212cSMike Anderson }
487432a212cSMike Anderson 
488fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
4891da177e4SLinus Torvalds {
4901da177e4SLinus Torvalds 	struct mapped_device *md;
4911da177e4SLinus Torvalds 
492fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
493fba9f90eSJeff Mahoney 
494fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
495fba9f90eSJeff Mahoney 	if (!md)
496fba9f90eSJeff Mahoney 		goto out;
497fba9f90eSJeff Mahoney 
4985c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
499432a212cSMike Anderson 	    dm_deleting_md(md)) {
500fba9f90eSJeff Mahoney 		md = NULL;
501fba9f90eSJeff Mahoney 		goto out;
502fba9f90eSJeff Mahoney 	}
503fba9f90eSJeff Mahoney 
5041da177e4SLinus Torvalds 	dm_get(md);
5055c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
506fba9f90eSJeff Mahoney out:
507fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
508fba9f90eSJeff Mahoney 
509fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
512db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode)
5131da177e4SLinus Torvalds {
51463a4f065SMike Snitzer 	struct mapped_device *md;
5156e9624b8SArnd Bergmann 
5164a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
5174a1aeb98SMilan Broz 
51863a4f065SMike Snitzer 	md = disk->private_data;
51963a4f065SMike Snitzer 	if (WARN_ON(!md))
52063a4f065SMike Snitzer 		goto out;
52163a4f065SMike Snitzer 
5222c140a24SMikulas Patocka 	if (atomic_dec_and_test(&md->open_count) &&
5232c140a24SMikulas Patocka 	    (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
524acfe0ad7SMikulas Patocka 		queue_work(deferred_remove_workqueue, &deferred_remove_work);
5252c140a24SMikulas Patocka 
5261da177e4SLinus Torvalds 	dm_put(md);
52763a4f065SMike Snitzer out:
5284a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
5291da177e4SLinus Torvalds }
5301da177e4SLinus Torvalds 
5315c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
5325c6bd75dSAlasdair G Kergon {
5335c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
5345c6bd75dSAlasdair G Kergon }
5355c6bd75dSAlasdair G Kergon 
5365c6bd75dSAlasdair G Kergon /*
5375c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
5385c6bd75dSAlasdair G Kergon  */
5392c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
5405c6bd75dSAlasdair G Kergon {
5415c6bd75dSAlasdair G Kergon 	int r = 0;
5425c6bd75dSAlasdair G Kergon 
5435c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
5445c6bd75dSAlasdair G Kergon 
5452c140a24SMikulas Patocka 	if (dm_open_count(md)) {
5465c6bd75dSAlasdair G Kergon 		r = -EBUSY;
5472c140a24SMikulas Patocka 		if (mark_deferred)
5482c140a24SMikulas Patocka 			set_bit(DMF_DEFERRED_REMOVE, &md->flags);
5492c140a24SMikulas Patocka 	} else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
5502c140a24SMikulas Patocka 		r = -EEXIST;
5515c6bd75dSAlasdair G Kergon 	else
5525c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
5535c6bd75dSAlasdair G Kergon 
5545c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
5555c6bd75dSAlasdair G Kergon 
5565c6bd75dSAlasdair G Kergon 	return r;
5575c6bd75dSAlasdair G Kergon }
5585c6bd75dSAlasdair G Kergon 
5592c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md)
5602c140a24SMikulas Patocka {
5612c140a24SMikulas Patocka 	int r = 0;
5622c140a24SMikulas Patocka 
5632c140a24SMikulas Patocka 	spin_lock(&_minor_lock);
5642c140a24SMikulas Patocka 
5652c140a24SMikulas Patocka 	if (test_bit(DMF_DELETING, &md->flags))
5662c140a24SMikulas Patocka 		r = -EBUSY;
5672c140a24SMikulas Patocka 	else
5682c140a24SMikulas Patocka 		clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
5692c140a24SMikulas Patocka 
5702c140a24SMikulas Patocka 	spin_unlock(&_minor_lock);
5712c140a24SMikulas Patocka 
5722c140a24SMikulas Patocka 	return r;
5732c140a24SMikulas Patocka }
5742c140a24SMikulas Patocka 
5752c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w)
5762c140a24SMikulas Patocka {
5772c140a24SMikulas Patocka 	dm_deferred_remove();
5782c140a24SMikulas Patocka }
5792c140a24SMikulas Patocka 
580fd2ed4d2SMikulas Patocka sector_t dm_get_size(struct mapped_device *md)
581fd2ed4d2SMikulas Patocka {
582fd2ed4d2SMikulas Patocka 	return get_capacity(md->disk);
583fd2ed4d2SMikulas Patocka }
584fd2ed4d2SMikulas Patocka 
5859974fa2cSMike Snitzer struct request_queue *dm_get_md_queue(struct mapped_device *md)
5869974fa2cSMike Snitzer {
5879974fa2cSMike Snitzer 	return md->queue;
5889974fa2cSMike Snitzer }
5899974fa2cSMike Snitzer 
590fd2ed4d2SMikulas Patocka struct dm_stats *dm_get_stats(struct mapped_device *md)
591fd2ed4d2SMikulas Patocka {
592fd2ed4d2SMikulas Patocka 	return &md->stats;
593fd2ed4d2SMikulas Patocka }
594fd2ed4d2SMikulas Patocka 
5953ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5963ac51e74SDarrick J. Wong {
5973ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
5983ac51e74SDarrick J. Wong 
5993ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
6003ac51e74SDarrick J. Wong }
6013ac51e74SDarrick J. Wong 
602956a4025SMike Snitzer static int dm_grab_bdev_for_ioctl(struct mapped_device *md,
60366482026SMike Snitzer 				  struct block_device **bdev,
604956a4025SMike Snitzer 				  fmode_t *mode)
605aa129a22SMilan Broz {
60666482026SMike Snitzer 	struct dm_target *tgt;
6076c182cd8SHannes Reinecke 	struct dm_table *map;
608956a4025SMike Snitzer 	int srcu_idx, r;
609aa129a22SMilan Broz 
6106c182cd8SHannes Reinecke retry:
611e56f81e0SChristoph Hellwig 	r = -ENOTTY;
612956a4025SMike Snitzer 	map = dm_get_live_table(md, &srcu_idx);
613aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
614aa129a22SMilan Broz 		goto out;
615aa129a22SMilan Broz 
616aa129a22SMilan Broz 	/* We only support devices that have a single target */
617aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
618aa129a22SMilan Broz 		goto out;
619aa129a22SMilan Broz 
62066482026SMike Snitzer 	tgt = dm_table_get_target(map, 0);
62166482026SMike Snitzer 	if (!tgt->type->prepare_ioctl)
6224d341d82SMike Snitzer 		goto out;
623aa129a22SMilan Broz 
6244f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
625aa129a22SMilan Broz 		r = -EAGAIN;
626aa129a22SMilan Broz 		goto out;
627aa129a22SMilan Broz 	}
628aa129a22SMilan Broz 
62966482026SMike Snitzer 	r = tgt->type->prepare_ioctl(tgt, bdev, mode);
630e56f81e0SChristoph Hellwig 	if (r < 0)
631e56f81e0SChristoph Hellwig 		goto out;
632e56f81e0SChristoph Hellwig 
633956a4025SMike Snitzer 	bdgrab(*bdev);
634956a4025SMike Snitzer 	dm_put_live_table(md, srcu_idx);
635e56f81e0SChristoph Hellwig 	return r;
636aa129a22SMilan Broz 
637aa129a22SMilan Broz out:
638956a4025SMike Snitzer 	dm_put_live_table(md, srcu_idx);
6395bbbfdf6SJunichi Nomura 	if (r == -ENOTCONN && !fatal_signal_pending(current)) {
6406c182cd8SHannes Reinecke 		msleep(10);
6416c182cd8SHannes Reinecke 		goto retry;
6426c182cd8SHannes Reinecke 	}
643e56f81e0SChristoph Hellwig 	return r;
644e56f81e0SChristoph Hellwig }
6456c182cd8SHannes Reinecke 
646e56f81e0SChristoph Hellwig static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
647e56f81e0SChristoph Hellwig 			unsigned int cmd, unsigned long arg)
648e56f81e0SChristoph Hellwig {
649e56f81e0SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
650956a4025SMike Snitzer 	int r;
651e56f81e0SChristoph Hellwig 
652956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
653e56f81e0SChristoph Hellwig 	if (r < 0)
654e56f81e0SChristoph Hellwig 		return r;
655e56f81e0SChristoph Hellwig 
656e56f81e0SChristoph Hellwig 	if (r > 0) {
657e56f81e0SChristoph Hellwig 		/*
658e56f81e0SChristoph Hellwig 		 * Target determined this ioctl is being issued against
659e56f81e0SChristoph Hellwig 		 * a logical partition of the parent bdev; so extra
660e56f81e0SChristoph Hellwig 		 * validation is needed.
661e56f81e0SChristoph Hellwig 		 */
662e56f81e0SChristoph Hellwig 		r = scsi_verify_blk_ioctl(NULL, cmd);
663e56f81e0SChristoph Hellwig 		if (r)
664e56f81e0SChristoph Hellwig 			goto out;
665e56f81e0SChristoph Hellwig 	}
666e56f81e0SChristoph Hellwig 
66766482026SMike Snitzer 	r =  __blkdev_driver_ioctl(bdev, mode, cmd, arg);
668e56f81e0SChristoph Hellwig out:
669956a4025SMike Snitzer 	bdput(bdev);
670aa129a22SMilan Broz 	return r;
671aa129a22SMilan Broz }
672aa129a22SMilan Broz 
673028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
6741da177e4SLinus Torvalds {
6751da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
678028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
6791da177e4SLinus Torvalds {
6801da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds 
683028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
6841da177e4SLinus Torvalds {
685dba14160SMikulas Patocka 	bio_put(&tio->clone);
6861da177e4SLinus Torvalds }
6871da177e4SLinus Torvalds 
688eca7ee6dSMike Snitzer static struct dm_rq_target_io *alloc_old_rq_tio(struct mapped_device *md,
68908885643SKiyoshi Ueda 						gfp_t gfp_mask)
690cec47e3dSKiyoshi Ueda {
6915f015204SJun'ichi Nomura 	return mempool_alloc(md->io_pool, gfp_mask);
692cec47e3dSKiyoshi Ueda }
693cec47e3dSKiyoshi Ueda 
694eca7ee6dSMike Snitzer static void free_old_rq_tio(struct dm_rq_target_io *tio)
695cec47e3dSKiyoshi Ueda {
6965f015204SJun'ichi Nomura 	mempool_free(tio, tio->md->io_pool);
697cec47e3dSKiyoshi Ueda }
698cec47e3dSKiyoshi Ueda 
699eca7ee6dSMike Snitzer static struct request *alloc_old_clone_request(struct mapped_device *md,
7001ae49ea2SMike Snitzer 					       gfp_t gfp_mask)
7011ae49ea2SMike Snitzer {
7021ae49ea2SMike Snitzer 	return mempool_alloc(md->rq_pool, gfp_mask);
7031ae49ea2SMike Snitzer }
7041ae49ea2SMike Snitzer 
705eca7ee6dSMike Snitzer static void free_old_clone_request(struct mapped_device *md, struct request *rq)
7061ae49ea2SMike Snitzer {
7071ae49ea2SMike Snitzer 	mempool_free(rq, md->rq_pool);
7081ae49ea2SMike Snitzer }
7091ae49ea2SMike Snitzer 
71090abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md)
71190abb8c4SKiyoshi Ueda {
71290abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
71390abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
71490abb8c4SKiyoshi Ueda }
71590abb8c4SKiyoshi Ueda 
7163eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
7173eaf840eSJun'ichi "Nick" Nomura {
7183eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
719fd2ed4d2SMikulas Patocka 	struct bio *bio = io->bio;
720c9959059STejun Heo 	int cpu;
721fd2ed4d2SMikulas Patocka 	int rw = bio_data_dir(bio);
7223eaf840eSJun'ichi "Nick" Nomura 
7233eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
7243eaf840eSJun'ichi "Nick" Nomura 
725074a7acaSTejun Heo 	cpu = part_stat_lock();
726074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
727074a7acaSTejun Heo 	part_stat_unlock();
7281e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw],
7291e9bb880SShaohua Li 		atomic_inc_return(&md->pending[rw]));
730fd2ed4d2SMikulas Patocka 
731fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
7324f024f37SKent Overstreet 		dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
733fd2ed4d2SMikulas Patocka 				    bio_sectors(bio), false, 0, &io->stats_aux);
7343eaf840eSJun'ichi "Nick" Nomura }
7353eaf840eSJun'ichi "Nick" Nomura 
736d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
7373eaf840eSJun'ichi "Nick" Nomura {
7383eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
7393eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
7403eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
74118c0b223SGu Zheng 	int pending;
7423eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
7433eaf840eSJun'ichi "Nick" Nomura 
74418c0b223SGu Zheng 	generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time);
7453eaf840eSJun'ichi "Nick" Nomura 
746fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
7474f024f37SKent Overstreet 		dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
748fd2ed4d2SMikulas Patocka 				    bio_sectors(bio), true, duration, &io->stats_aux);
749fd2ed4d2SMikulas Patocka 
750af7e466aSMikulas Patocka 	/*
751af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
752d87f4c14STejun Heo 	 * a flush.
753af7e466aSMikulas Patocka 	 */
7541e9bb880SShaohua Li 	pending = atomic_dec_return(&md->pending[rw]);
7551e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
756316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
7573eaf840eSJun'ichi "Nick" Nomura 
758d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
759d221d2e7SMikulas Patocka 	if (!pending)
760d221d2e7SMikulas Patocka 		wake_up(&md->wait);
7613eaf840eSJun'ichi "Nick" Nomura }
7623eaf840eSJun'ichi "Nick" Nomura 
7631da177e4SLinus Torvalds /*
7641da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
7651da177e4SLinus Torvalds  */
76692c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
7671da177e4SLinus Torvalds {
76805447420SKiyoshi Ueda 	unsigned long flags;
7691da177e4SLinus Torvalds 
77005447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
7711da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
77205447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
77392c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
7741da177e4SLinus Torvalds }
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds /*
7771da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
7781da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
77983d5e5b0SMikulas Patocka  * dm_put_live_table() when finished.
7801da177e4SLinus Torvalds  */
78183d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
7821da177e4SLinus Torvalds {
78383d5e5b0SMikulas Patocka 	*srcu_idx = srcu_read_lock(&md->io_barrier);
7841da177e4SLinus Torvalds 
78583d5e5b0SMikulas Patocka 	return srcu_dereference(md->map, &md->io_barrier);
78683d5e5b0SMikulas Patocka }
7871da177e4SLinus Torvalds 
78883d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
78983d5e5b0SMikulas Patocka {
79083d5e5b0SMikulas Patocka 	srcu_read_unlock(&md->io_barrier, srcu_idx);
79183d5e5b0SMikulas Patocka }
79283d5e5b0SMikulas Patocka 
79383d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md)
79483d5e5b0SMikulas Patocka {
79583d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
79683d5e5b0SMikulas Patocka 	synchronize_rcu_expedited();
79783d5e5b0SMikulas Patocka }
79883d5e5b0SMikulas Patocka 
79983d5e5b0SMikulas Patocka /*
80083d5e5b0SMikulas Patocka  * A fast alternative to dm_get_live_table/dm_put_live_table.
80183d5e5b0SMikulas Patocka  * The caller must not block between these two functions.
80283d5e5b0SMikulas Patocka  */
80383d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
80483d5e5b0SMikulas Patocka {
80583d5e5b0SMikulas Patocka 	rcu_read_lock();
80683d5e5b0SMikulas Patocka 	return rcu_dereference(md->map);
80783d5e5b0SMikulas Patocka }
80883d5e5b0SMikulas Patocka 
80983d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
81083d5e5b0SMikulas Patocka {
81183d5e5b0SMikulas Patocka 	rcu_read_unlock();
8121da177e4SLinus Torvalds }
8131da177e4SLinus Torvalds 
8143ac51e74SDarrick J. Wong /*
81586f1152bSBenjamin Marzinski  * Open a table device so we can use it as a map destination.
81686f1152bSBenjamin Marzinski  */
81786f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev,
81886f1152bSBenjamin Marzinski 			     struct mapped_device *md)
81986f1152bSBenjamin Marzinski {
82086f1152bSBenjamin Marzinski 	static char *_claim_ptr = "I belong to device-mapper";
82186f1152bSBenjamin Marzinski 	struct block_device *bdev;
82286f1152bSBenjamin Marzinski 
82386f1152bSBenjamin Marzinski 	int r;
82486f1152bSBenjamin Marzinski 
82586f1152bSBenjamin Marzinski 	BUG_ON(td->dm_dev.bdev);
82686f1152bSBenjamin Marzinski 
82786f1152bSBenjamin Marzinski 	bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
82886f1152bSBenjamin Marzinski 	if (IS_ERR(bdev))
82986f1152bSBenjamin Marzinski 		return PTR_ERR(bdev);
83086f1152bSBenjamin Marzinski 
83186f1152bSBenjamin Marzinski 	r = bd_link_disk_holder(bdev, dm_disk(md));
83286f1152bSBenjamin Marzinski 	if (r) {
83386f1152bSBenjamin Marzinski 		blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
83486f1152bSBenjamin Marzinski 		return r;
83586f1152bSBenjamin Marzinski 	}
83686f1152bSBenjamin Marzinski 
83786f1152bSBenjamin Marzinski 	td->dm_dev.bdev = bdev;
83886f1152bSBenjamin Marzinski 	return 0;
83986f1152bSBenjamin Marzinski }
84086f1152bSBenjamin Marzinski 
84186f1152bSBenjamin Marzinski /*
84286f1152bSBenjamin Marzinski  * Close a table device that we've been using.
84386f1152bSBenjamin Marzinski  */
84486f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md)
84586f1152bSBenjamin Marzinski {
84686f1152bSBenjamin Marzinski 	if (!td->dm_dev.bdev)
84786f1152bSBenjamin Marzinski 		return;
84886f1152bSBenjamin Marzinski 
84986f1152bSBenjamin Marzinski 	bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
85086f1152bSBenjamin Marzinski 	blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
85186f1152bSBenjamin Marzinski 	td->dm_dev.bdev = NULL;
85286f1152bSBenjamin Marzinski }
85386f1152bSBenjamin Marzinski 
85486f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev,
85586f1152bSBenjamin Marzinski 					      fmode_t mode) {
85686f1152bSBenjamin Marzinski 	struct table_device *td;
85786f1152bSBenjamin Marzinski 
85886f1152bSBenjamin Marzinski 	list_for_each_entry(td, l, list)
85986f1152bSBenjamin Marzinski 		if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
86086f1152bSBenjamin Marzinski 			return td;
86186f1152bSBenjamin Marzinski 
86286f1152bSBenjamin Marzinski 	return NULL;
86386f1152bSBenjamin Marzinski }
86486f1152bSBenjamin Marzinski 
86586f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
86686f1152bSBenjamin Marzinski 			struct dm_dev **result) {
86786f1152bSBenjamin Marzinski 	int r;
86886f1152bSBenjamin Marzinski 	struct table_device *td;
86986f1152bSBenjamin Marzinski 
87086f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
87186f1152bSBenjamin Marzinski 	td = find_table_device(&md->table_devices, dev, mode);
87286f1152bSBenjamin Marzinski 	if (!td) {
873*115485e8SMike Snitzer 		td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
87486f1152bSBenjamin Marzinski 		if (!td) {
87586f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
87686f1152bSBenjamin Marzinski 			return -ENOMEM;
87786f1152bSBenjamin Marzinski 		}
87886f1152bSBenjamin Marzinski 
87986f1152bSBenjamin Marzinski 		td->dm_dev.mode = mode;
88086f1152bSBenjamin Marzinski 		td->dm_dev.bdev = NULL;
88186f1152bSBenjamin Marzinski 
88286f1152bSBenjamin Marzinski 		if ((r = open_table_device(td, dev, md))) {
88386f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
88486f1152bSBenjamin Marzinski 			kfree(td);
88586f1152bSBenjamin Marzinski 			return r;
88686f1152bSBenjamin Marzinski 		}
88786f1152bSBenjamin Marzinski 
88886f1152bSBenjamin Marzinski 		format_dev_t(td->dm_dev.name, dev);
88986f1152bSBenjamin Marzinski 
89086f1152bSBenjamin Marzinski 		atomic_set(&td->count, 0);
89186f1152bSBenjamin Marzinski 		list_add(&td->list, &md->table_devices);
89286f1152bSBenjamin Marzinski 	}
89386f1152bSBenjamin Marzinski 	atomic_inc(&td->count);
89486f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
89586f1152bSBenjamin Marzinski 
89686f1152bSBenjamin Marzinski 	*result = &td->dm_dev;
89786f1152bSBenjamin Marzinski 	return 0;
89886f1152bSBenjamin Marzinski }
89986f1152bSBenjamin Marzinski EXPORT_SYMBOL_GPL(dm_get_table_device);
90086f1152bSBenjamin Marzinski 
90186f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
90286f1152bSBenjamin Marzinski {
90386f1152bSBenjamin Marzinski 	struct table_device *td = container_of(d, struct table_device, dm_dev);
90486f1152bSBenjamin Marzinski 
90586f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
90686f1152bSBenjamin Marzinski 	if (atomic_dec_and_test(&td->count)) {
90786f1152bSBenjamin Marzinski 		close_table_device(td, md);
90886f1152bSBenjamin Marzinski 		list_del(&td->list);
90986f1152bSBenjamin Marzinski 		kfree(td);
91086f1152bSBenjamin Marzinski 	}
91186f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
91286f1152bSBenjamin Marzinski }
91386f1152bSBenjamin Marzinski EXPORT_SYMBOL(dm_put_table_device);
91486f1152bSBenjamin Marzinski 
91586f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices)
91686f1152bSBenjamin Marzinski {
91786f1152bSBenjamin Marzinski 	struct list_head *tmp, *next;
91886f1152bSBenjamin Marzinski 
91986f1152bSBenjamin Marzinski 	list_for_each_safe(tmp, next, devices) {
92086f1152bSBenjamin Marzinski 		struct table_device *td = list_entry(tmp, struct table_device, list);
92186f1152bSBenjamin Marzinski 
92286f1152bSBenjamin Marzinski 		DMWARN("dm_destroy: %s still exists with %d references",
92386f1152bSBenjamin Marzinski 		       td->dm_dev.name, atomic_read(&td->count));
92486f1152bSBenjamin Marzinski 		kfree(td);
92586f1152bSBenjamin Marzinski 	}
92686f1152bSBenjamin Marzinski }
92786f1152bSBenjamin Marzinski 
92886f1152bSBenjamin Marzinski /*
9293ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
9303ac51e74SDarrick J. Wong  */
9313ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
9323ac51e74SDarrick J. Wong {
9333ac51e74SDarrick J. Wong 	*geo = md->geometry;
9343ac51e74SDarrick J. Wong 
9353ac51e74SDarrick J. Wong 	return 0;
9363ac51e74SDarrick J. Wong }
9373ac51e74SDarrick J. Wong 
9383ac51e74SDarrick J. Wong /*
9393ac51e74SDarrick J. Wong  * Set the geometry of a device.
9403ac51e74SDarrick J. Wong  */
9413ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
9423ac51e74SDarrick J. Wong {
9433ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
9443ac51e74SDarrick J. Wong 
9453ac51e74SDarrick J. Wong 	if (geo->start > sz) {
9463ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
9473ac51e74SDarrick J. Wong 		return -EINVAL;
9483ac51e74SDarrick J. Wong 	}
9493ac51e74SDarrick J. Wong 
9503ac51e74SDarrick J. Wong 	md->geometry = *geo;
9513ac51e74SDarrick J. Wong 
9523ac51e74SDarrick J. Wong 	return 0;
9533ac51e74SDarrick J. Wong }
9543ac51e74SDarrick J. Wong 
9551da177e4SLinus Torvalds /*-----------------------------------------------------------------
9561da177e4SLinus Torvalds  * CRUD START:
9571da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
9581da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
9591da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
9601da177e4SLinus Torvalds  *   interests of getting something for people to use I give
9611da177e4SLinus Torvalds  *   you this clearly demarcated crap.
9621da177e4SLinus Torvalds  *---------------------------------------------------------------*/
9631da177e4SLinus Torvalds 
9642e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
9652e93ccc1SKiyoshi Ueda {
9662e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
9672e93ccc1SKiyoshi Ueda }
9682e93ccc1SKiyoshi Ueda 
9691da177e4SLinus Torvalds /*
9701da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
9711da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
9721da177e4SLinus Torvalds  */
973858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
9741da177e4SLinus Torvalds {
9752e93ccc1SKiyoshi Ueda 	unsigned long flags;
976b35f8caaSMilan Broz 	int io_error;
977b35f8caaSMilan Broz 	struct bio *bio;
978b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
9792e93ccc1SKiyoshi Ueda 
9802e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
981f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
982f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
983f88fb981SKiyoshi Ueda 		if (!(io->error > 0 && __noflush_suspending(md)))
9841da177e4SLinus Torvalds 			io->error = error;
985f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
986f88fb981SKiyoshi Ueda 	}
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
9892e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
9902e93ccc1SKiyoshi Ueda 			/*
9912e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
9922e93ccc1SKiyoshi Ueda 			 */
993022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
9946a8736d1STejun Heo 			if (__noflush_suspending(md))
9956a8736d1STejun Heo 				bio_list_add_head(&md->deferred, io->bio);
9966a8736d1STejun Heo 			else
9972e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
9982e93ccc1SKiyoshi Ueda 				io->error = -EIO;
999022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
10002e93ccc1SKiyoshi Ueda 		}
10012e93ccc1SKiyoshi Ueda 
1002b35f8caaSMilan Broz 		io_error = io->error;
1003b35f8caaSMilan Broz 		bio = io->bio;
1004af7e466aSMikulas Patocka 		end_io_acct(io);
1005a97f925aSMikulas Patocka 		free_io(md, io);
10061da177e4SLinus Torvalds 
10076a8736d1STejun Heo 		if (io_error == DM_ENDIO_REQUEUE)
10086a8736d1STejun Heo 			return;
10096a8736d1STejun Heo 
10104f024f37SKent Overstreet 		if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
10111da177e4SLinus Torvalds 			/*
10126a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
10136a8736d1STejun Heo 			 * without REQ_FLUSH.
10141da177e4SLinus Torvalds 			 */
10156a8736d1STejun Heo 			bio->bi_rw &= ~REQ_FLUSH;
10166a8736d1STejun Heo 			queue_io(md, bio);
10175f3ea37cSArnaldo Carvalho de Melo 		} else {
1018b372d360SMike Snitzer 			/* done with normal IO or empty flush */
10190a82a8d1SLinus Torvalds 			trace_block_bio_complete(md->queue, bio, io_error);
10204246a0b6SChristoph Hellwig 			bio->bi_error = io_error;
10214246a0b6SChristoph Hellwig 			bio_endio(bio);
10222e93ccc1SKiyoshi Ueda 		}
10231da177e4SLinus Torvalds 	}
1024af7e466aSMikulas Patocka }
10251da177e4SLinus Torvalds 
10267eee4ae2SMike Snitzer static void disable_write_same(struct mapped_device *md)
10277eee4ae2SMike Snitzer {
10287eee4ae2SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
10297eee4ae2SMike Snitzer 
10307eee4ae2SMike Snitzer 	/* device doesn't really support WRITE SAME, disable it */
10317eee4ae2SMike Snitzer 	limits->max_write_same_sectors = 0;
10327eee4ae2SMike Snitzer }
10337eee4ae2SMike Snitzer 
10344246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio)
10351da177e4SLinus Torvalds {
10364246a0b6SChristoph Hellwig 	int error = bio->bi_error;
10375164beceSzhendong chen 	int r = error;
1038bfc6d41cSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1039b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
10409faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
10411da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 	if (endio) {
10447de3ee57SMikulas Patocka 		r = endio(tio->ti, bio, error);
10452e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
10462e93ccc1SKiyoshi Ueda 			/*
10472e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
10482e93ccc1SKiyoshi Ueda 			 * in dec_pending().
10492e93ccc1SKiyoshi Ueda 			 */
10501da177e4SLinus Torvalds 			error = r;
105145cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
105245cbcd79SKiyoshi Ueda 			/* The target will handle the io */
10536712ecf8SNeilBrown 			return;
105445cbcd79SKiyoshi Ueda 		else if (r) {
105545cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
105645cbcd79SKiyoshi Ueda 			BUG();
105745cbcd79SKiyoshi Ueda 		}
10581da177e4SLinus Torvalds 	}
10591da177e4SLinus Torvalds 
10607eee4ae2SMike Snitzer 	if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) &&
10617eee4ae2SMike Snitzer 		     !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
10627eee4ae2SMike Snitzer 		disable_write_same(md);
10637eee4ae2SMike Snitzer 
10649faf400fSStefan Bader 	free_tio(md, tio);
1065b35f8caaSMilan Broz 	dec_pending(io, error);
10661da177e4SLinus Torvalds }
10671da177e4SLinus Torvalds 
106878d8e58aSMike Snitzer /*
106978d8e58aSMike Snitzer  * Partial completion handling for request-based dm
107078d8e58aSMike Snitzer  */
10714246a0b6SChristoph Hellwig static void end_clone_bio(struct bio *clone)
107278d8e58aSMike Snitzer {
107378d8e58aSMike Snitzer 	struct dm_rq_clone_bio_info *info =
107478d8e58aSMike Snitzer 		container_of(clone, struct dm_rq_clone_bio_info, clone);
107578d8e58aSMike Snitzer 	struct dm_rq_target_io *tio = info->tio;
107678d8e58aSMike Snitzer 	struct bio *bio = info->orig;
107778d8e58aSMike Snitzer 	unsigned int nr_bytes = info->orig->bi_iter.bi_size;
107850887bd1SJunichi Nomura 	int error = clone->bi_error;
107978d8e58aSMike Snitzer 
108078d8e58aSMike Snitzer 	bio_put(clone);
108178d8e58aSMike Snitzer 
108278d8e58aSMike Snitzer 	if (tio->error)
108378d8e58aSMike Snitzer 		/*
108478d8e58aSMike Snitzer 		 * An error has already been detected on the request.
108578d8e58aSMike Snitzer 		 * Once error occurred, just let clone->end_io() handle
108678d8e58aSMike Snitzer 		 * the remainder.
108778d8e58aSMike Snitzer 		 */
108878d8e58aSMike Snitzer 		return;
108950887bd1SJunichi Nomura 	else if (error) {
109078d8e58aSMike Snitzer 		/*
109178d8e58aSMike Snitzer 		 * Don't notice the error to the upper layer yet.
109278d8e58aSMike Snitzer 		 * The error handling decision is made by the target driver,
109378d8e58aSMike Snitzer 		 * when the request is completed.
109478d8e58aSMike Snitzer 		 */
109550887bd1SJunichi Nomura 		tio->error = error;
109678d8e58aSMike Snitzer 		return;
109778d8e58aSMike Snitzer 	}
109878d8e58aSMike Snitzer 
109978d8e58aSMike Snitzer 	/*
110078d8e58aSMike Snitzer 	 * I/O for the bio successfully completed.
110178d8e58aSMike Snitzer 	 * Notice the data completion to the upper layer.
110278d8e58aSMike Snitzer 	 */
110378d8e58aSMike Snitzer 
110478d8e58aSMike Snitzer 	/*
110578d8e58aSMike Snitzer 	 * bios are processed from the head of the list.
110678d8e58aSMike Snitzer 	 * So the completing bio should always be rq->bio.
110778d8e58aSMike Snitzer 	 * If it's not, something wrong is happening.
110878d8e58aSMike Snitzer 	 */
110978d8e58aSMike Snitzer 	if (tio->orig->bio != bio)
111078d8e58aSMike Snitzer 		DMERR("bio completion is going in the middle of the request");
111178d8e58aSMike Snitzer 
111278d8e58aSMike Snitzer 	/*
111378d8e58aSMike Snitzer 	 * Update the original request.
111478d8e58aSMike Snitzer 	 * Do not use blk_end_request() here, because it may complete
111578d8e58aSMike Snitzer 	 * the original request before the clone, and break the ordering.
111678d8e58aSMike Snitzer 	 */
111778d8e58aSMike Snitzer 	blk_update_request(tio->orig, 0, nr_bytes);
111878d8e58aSMike Snitzer }
111978d8e58aSMike Snitzer 
1120bfebd1cdSMike Snitzer static struct dm_rq_target_io *tio_from_request(struct request *rq)
1121bfebd1cdSMike Snitzer {
1122bfebd1cdSMike Snitzer 	return (rq->q->mq_ops ? blk_mq_rq_to_pdu(rq) : rq->special);
1123bfebd1cdSMike Snitzer }
1124bfebd1cdSMike Snitzer 
1125e262f347SMikulas Patocka static void rq_end_stats(struct mapped_device *md, struct request *orig)
1126e262f347SMikulas Patocka {
1127e262f347SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats))) {
1128e262f347SMikulas Patocka 		struct dm_rq_target_io *tio = tio_from_request(orig);
1129e262f347SMikulas Patocka 		tio->duration_jiffies = jiffies - tio->duration_jiffies;
1130e262f347SMikulas Patocka 		dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
1131e262f347SMikulas Patocka 				    tio->n_sectors, true, tio->duration_jiffies,
1132e262f347SMikulas Patocka 				    &tio->stats_aux);
1133e262f347SMikulas Patocka 	}
1134e262f347SMikulas Patocka }
1135e262f347SMikulas Patocka 
1136cec47e3dSKiyoshi Ueda /*
1137cec47e3dSKiyoshi Ueda  * Don't touch any member of the md after calling this function because
1138cec47e3dSKiyoshi Ueda  * the md may be freed in dm_put() at the end of this function.
1139cec47e3dSKiyoshi Ueda  * Or do dm_get() before calling this function and dm_put() later.
1140cec47e3dSKiyoshi Ueda  */
1141466d89a6SKeith Busch static void rq_completed(struct mapped_device *md, int rw, bool run_queue)
1142cec47e3dSKiyoshi Ueda {
1143b4324feeSKiyoshi Ueda 	atomic_dec(&md->pending[rw]);
1144cec47e3dSKiyoshi Ueda 
1145cec47e3dSKiyoshi Ueda 	/* nudge anyone waiting on suspend queue */
1146621739b0SMike Snitzer 	if (!md_in_flight(md))
1147cec47e3dSKiyoshi Ueda 		wake_up(&md->wait);
1148cec47e3dSKiyoshi Ueda 
1149a8c32a5cSJens Axboe 	/*
1150a8c32a5cSJens Axboe 	 * Run this off this callpath, as drivers could invoke end_io while
1151a8c32a5cSJens Axboe 	 * inside their request_fn (and holding the queue lock). Calling
1152a8c32a5cSJens Axboe 	 * back into ->request_fn() could deadlock attempting to grab the
1153a8c32a5cSJens Axboe 	 * queue lock again.
1154a8c32a5cSJens Axboe 	 */
11556acfe68bSMike Snitzer 	if (!md->queue->mq_ops && run_queue)
1156a8c32a5cSJens Axboe 		blk_run_queue_async(md->queue);
1157cec47e3dSKiyoshi Ueda 
1158cec47e3dSKiyoshi Ueda 	/*
1159cec47e3dSKiyoshi Ueda 	 * dm_put() must be at the end of this function. See the comment above
1160cec47e3dSKiyoshi Ueda 	 */
1161cec47e3dSKiyoshi Ueda 	dm_put(md);
1162cec47e3dSKiyoshi Ueda }
1163cec47e3dSKiyoshi Ueda 
1164e5d8de32SMike Snitzer static void free_rq_clone(struct request *clone)
1165a77e28c7SKiyoshi Ueda {
1166a77e28c7SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1167bfebd1cdSMike Snitzer 	struct mapped_device *md = tio->md;
1168a77e28c7SKiyoshi Ueda 
116978d8e58aSMike Snitzer 	blk_rq_unprep_clone(clone);
117078d8e58aSMike Snitzer 
1171aa6df8ddSMike Snitzer 	if (md->type == DM_TYPE_MQ_REQUEST_BASED)
1172aa6df8ddSMike Snitzer 		/* stacked on blk-mq queue(s) */
1173e5863d9aSMike Snitzer 		tio->ti->type->release_clone_rq(clone);
117402233342SMike Snitzer 	else if (!md->queue->mq_ops)
117502233342SMike Snitzer 		/* request_fn queue stacked on request_fn queue(s) */
1176eca7ee6dSMike Snitzer 		free_old_clone_request(md, clone);
1177bfebd1cdSMike Snitzer 
1178bfebd1cdSMike Snitzer 	if (!md->queue->mq_ops)
1179eca7ee6dSMike Snitzer 		free_old_rq_tio(tio);
1180a77e28c7SKiyoshi Ueda }
1181a77e28c7SKiyoshi Ueda 
1182980691e5SKiyoshi Ueda /*
1183980691e5SKiyoshi Ueda  * Complete the clone and the original request.
1184466d89a6SKeith Busch  * Must be called without clone's queue lock held,
1185466d89a6SKeith Busch  * see end_clone_request() for more details.
1186980691e5SKiyoshi Ueda  */
1187980691e5SKiyoshi Ueda static void dm_end_request(struct request *clone, int error)
1188980691e5SKiyoshi Ueda {
1189980691e5SKiyoshi Ueda 	int rw = rq_data_dir(clone);
1190980691e5SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1191980691e5SKiyoshi Ueda 	struct mapped_device *md = tio->md;
1192980691e5SKiyoshi Ueda 	struct request *rq = tio->orig;
1193980691e5SKiyoshi Ueda 
119429e4013dSTejun Heo 	if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
1195980691e5SKiyoshi Ueda 		rq->errors = clone->errors;
1196980691e5SKiyoshi Ueda 		rq->resid_len = clone->resid_len;
1197980691e5SKiyoshi Ueda 
1198980691e5SKiyoshi Ueda 		if (rq->sense)
1199980691e5SKiyoshi Ueda 			/*
1200980691e5SKiyoshi Ueda 			 * We are using the sense buffer of the original
1201980691e5SKiyoshi Ueda 			 * request.
1202980691e5SKiyoshi Ueda 			 * So setting the length of the sense data is enough.
1203980691e5SKiyoshi Ueda 			 */
1204980691e5SKiyoshi Ueda 			rq->sense_len = clone->sense_len;
1205980691e5SKiyoshi Ueda 	}
1206980691e5SKiyoshi Ueda 
1207e5d8de32SMike Snitzer 	free_rq_clone(clone);
1208e262f347SMikulas Patocka 	rq_end_stats(md, rq);
1209bfebd1cdSMike Snitzer 	if (!rq->q->mq_ops)
1210980691e5SKiyoshi Ueda 		blk_end_request_all(rq, error);
1211bfebd1cdSMike Snitzer 	else
1212bfebd1cdSMike Snitzer 		blk_mq_end_request(rq, error);
121329e4013dSTejun Heo 	rq_completed(md, rw, true);
1214980691e5SKiyoshi Ueda }
1215980691e5SKiyoshi Ueda 
1216cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq)
1217cec47e3dSKiyoshi Ueda {
1218bfebd1cdSMike Snitzer 	struct dm_rq_target_io *tio = tio_from_request(rq);
1219466d89a6SKeith Busch 	struct request *clone = tio->clone;
1220cec47e3dSKiyoshi Ueda 
1221bfebd1cdSMike Snitzer 	if (!rq->q->mq_ops) {
1222cec47e3dSKiyoshi Ueda 		rq->special = NULL;
1223cec47e3dSKiyoshi Ueda 		rq->cmd_flags &= ~REQ_DONTPREP;
1224bfebd1cdSMike Snitzer 	}
1225cec47e3dSKiyoshi Ueda 
1226e5863d9aSMike Snitzer 	if (clone)
1227e5d8de32SMike Snitzer 		free_rq_clone(clone);
12284328daa2SMike Snitzer 	else if (!tio->md->queue->mq_ops)
1229eca7ee6dSMike Snitzer 		free_old_rq_tio(tio);
1230cec47e3dSKiyoshi Ueda }
1231cec47e3dSKiyoshi Ueda 
1232cec47e3dSKiyoshi Ueda /*
1233cec47e3dSKiyoshi Ueda  * Requeue the original request of a clone.
1234cec47e3dSKiyoshi Ueda  */
1235eca7ee6dSMike Snitzer static void dm_old_requeue_request(struct request *rq)
1236cec47e3dSKiyoshi Ueda {
1237cec47e3dSKiyoshi Ueda 	struct request_queue *q = rq->q;
1238cec47e3dSKiyoshi Ueda 	unsigned long flags;
1239cec47e3dSKiyoshi Ueda 
1240cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
1241cec47e3dSKiyoshi Ueda 	blk_requeue_request(q, rq);
12424ae9944dSJunichi Nomura 	blk_run_queue_async(q);
1243cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
1244bfebd1cdSMike Snitzer }
1245bfebd1cdSMike Snitzer 
1246818c5f3bSMike Snitzer static void dm_mq_requeue_request(struct request *rq)
1247818c5f3bSMike Snitzer {
1248818c5f3bSMike Snitzer 	struct request_queue *q = rq->q;
1249818c5f3bSMike Snitzer 	unsigned long flags;
1250818c5f3bSMike Snitzer 
1251818c5f3bSMike Snitzer 	blk_mq_requeue_request(rq);
1252818c5f3bSMike Snitzer 	spin_lock_irqsave(q->queue_lock, flags);
1253818c5f3bSMike Snitzer 	if (!blk_queue_stopped(q))
1254818c5f3bSMike Snitzer 		blk_mq_kick_requeue_list(q);
1255818c5f3bSMike Snitzer 	spin_unlock_irqrestore(q->queue_lock, flags);
1256818c5f3bSMike Snitzer }
1257818c5f3bSMike Snitzer 
12582d76fff1SMike Snitzer static void dm_requeue_original_request(struct mapped_device *md,
1259bfebd1cdSMike Snitzer 					struct request *rq)
1260bfebd1cdSMike Snitzer {
1261bfebd1cdSMike Snitzer 	int rw = rq_data_dir(rq);
1262bfebd1cdSMike Snitzer 
1263bfebd1cdSMike Snitzer 	dm_unprep_request(rq);
1264bfebd1cdSMike Snitzer 
1265e262f347SMikulas Patocka 	rq_end_stats(md, rq);
1266bfebd1cdSMike Snitzer 	if (!rq->q->mq_ops)
1267eca7ee6dSMike Snitzer 		dm_old_requeue_request(rq);
1268818c5f3bSMike Snitzer 	else
1269818c5f3bSMike Snitzer 		dm_mq_requeue_request(rq);
1270cec47e3dSKiyoshi Ueda 
1271466d89a6SKeith Busch 	rq_completed(md, rw, false);
1272cec47e3dSKiyoshi Ueda }
1273466d89a6SKeith Busch 
1274eca7ee6dSMike Snitzer static void dm_old_stop_queue(struct request_queue *q)
1275cec47e3dSKiyoshi Ueda {
1276bfebd1cdSMike Snitzer 	unsigned long flags;
1277bfebd1cdSMike Snitzer 
1278bfebd1cdSMike Snitzer 	spin_lock_irqsave(q->queue_lock, flags);
1279818c5f3bSMike Snitzer 	if (blk_queue_stopped(q)) {
1280818c5f3bSMike Snitzer 		spin_unlock_irqrestore(q->queue_lock, flags);
1281818c5f3bSMike Snitzer 		return;
1282818c5f3bSMike Snitzer 	}
1283818c5f3bSMike Snitzer 
1284cec47e3dSKiyoshi Ueda 	blk_stop_queue(q);
1285bfebd1cdSMike Snitzer 	spin_unlock_irqrestore(q->queue_lock, flags);
1286cec47e3dSKiyoshi Ueda }
1287cec47e3dSKiyoshi Ueda 
1288eca7ee6dSMike Snitzer static void dm_stop_queue(struct request_queue *q)
1289cec47e3dSKiyoshi Ueda {
1290bfebd1cdSMike Snitzer 	if (!q->mq_ops)
1291eca7ee6dSMike Snitzer 		dm_old_stop_queue(q);
1292bfebd1cdSMike Snitzer 	else
1293bfebd1cdSMike Snitzer 		blk_mq_stop_hw_queues(q);
1294bfebd1cdSMike Snitzer }
1295bfebd1cdSMike Snitzer 
1296eca7ee6dSMike Snitzer static void dm_old_start_queue(struct request_queue *q)
1297bfebd1cdSMike Snitzer {
1298cec47e3dSKiyoshi Ueda 	unsigned long flags;
1299cec47e3dSKiyoshi Ueda 
1300cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
1301cec47e3dSKiyoshi Ueda 	if (blk_queue_stopped(q))
1302cec47e3dSKiyoshi Ueda 		blk_start_queue(q);
1303bfebd1cdSMike Snitzer 	spin_unlock_irqrestore(q->queue_lock, flags);
1304cec47e3dSKiyoshi Ueda }
1305cec47e3dSKiyoshi Ueda 
1306eca7ee6dSMike Snitzer static void dm_start_queue(struct request_queue *q)
1307cec47e3dSKiyoshi Ueda {
1308bfebd1cdSMike Snitzer 	if (!q->mq_ops)
1309eca7ee6dSMike Snitzer 		dm_old_start_queue(q);
1310818c5f3bSMike Snitzer 	else {
1311bfebd1cdSMike Snitzer 		blk_mq_start_stopped_hw_queues(q, true);
1312818c5f3bSMike Snitzer 		blk_mq_kick_requeue_list(q);
1313818c5f3bSMike Snitzer 	}
1314cec47e3dSKiyoshi Ueda }
1315cec47e3dSKiyoshi Ueda 
131611a68244SKiyoshi Ueda static void dm_done(struct request *clone, int error, bool mapped)
131711a68244SKiyoshi Ueda {
131811a68244SKiyoshi Ueda 	int r = error;
131911a68244SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1320ba1cbad9SMike Snitzer 	dm_request_endio_fn rq_end_io = NULL;
1321ba1cbad9SMike Snitzer 
1322ba1cbad9SMike Snitzer 	if (tio->ti) {
1323ba1cbad9SMike Snitzer 		rq_end_io = tio->ti->type->rq_end_io;
132411a68244SKiyoshi Ueda 
132511a68244SKiyoshi Ueda 		if (mapped && rq_end_io)
132611a68244SKiyoshi Ueda 			r = rq_end_io(tio->ti, clone, error, &tio->info);
1327ba1cbad9SMike Snitzer 	}
132811a68244SKiyoshi Ueda 
13297eee4ae2SMike Snitzer 	if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) &&
13307eee4ae2SMike Snitzer 		     !clone->q->limits.max_write_same_sectors))
13317eee4ae2SMike Snitzer 		disable_write_same(tio->md);
13327eee4ae2SMike Snitzer 
133311a68244SKiyoshi Ueda 	if (r <= 0)
133411a68244SKiyoshi Ueda 		/* The target wants to complete the I/O */
133511a68244SKiyoshi Ueda 		dm_end_request(clone, r);
133611a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_INCOMPLETE)
133711a68244SKiyoshi Ueda 		/* The target will handle the I/O */
133811a68244SKiyoshi Ueda 		return;
133911a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_REQUEUE)
134011a68244SKiyoshi Ueda 		/* The target wants to requeue the I/O */
13412d76fff1SMike Snitzer 		dm_requeue_original_request(tio->md, tio->orig);
134211a68244SKiyoshi Ueda 	else {
134311a68244SKiyoshi Ueda 		DMWARN("unimplemented target endio return value: %d", r);
134411a68244SKiyoshi Ueda 		BUG();
134511a68244SKiyoshi Ueda 	}
134611a68244SKiyoshi Ueda }
134711a68244SKiyoshi Ueda 
1348cec47e3dSKiyoshi Ueda /*
1349cec47e3dSKiyoshi Ueda  * Request completion handler for request-based dm
1350cec47e3dSKiyoshi Ueda  */
1351cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq)
1352cec47e3dSKiyoshi Ueda {
135311a68244SKiyoshi Ueda 	bool mapped = true;
1354bfebd1cdSMike Snitzer 	struct dm_rq_target_io *tio = tio_from_request(rq);
1355466d89a6SKeith Busch 	struct request *clone = tio->clone;
1356bfebd1cdSMike Snitzer 	int rw;
1357cec47e3dSKiyoshi Ueda 
1358e5863d9aSMike Snitzer 	if (!clone) {
1359e262f347SMikulas Patocka 		rq_end_stats(tio->md, rq);
1360bfebd1cdSMike Snitzer 		rw = rq_data_dir(rq);
1361bfebd1cdSMike Snitzer 		if (!rq->q->mq_ops) {
1362e5863d9aSMike Snitzer 			blk_end_request_all(rq, tio->error);
1363bfebd1cdSMike Snitzer 			rq_completed(tio->md, rw, false);
1364eca7ee6dSMike Snitzer 			free_old_rq_tio(tio);
1365bfebd1cdSMike Snitzer 		} else {
1366bfebd1cdSMike Snitzer 			blk_mq_end_request(rq, tio->error);
1367bfebd1cdSMike Snitzer 			rq_completed(tio->md, rw, false);
1368bfebd1cdSMike Snitzer 		}
1369e5863d9aSMike Snitzer 		return;
1370e5863d9aSMike Snitzer 	}
1371cec47e3dSKiyoshi Ueda 
137211a68244SKiyoshi Ueda 	if (rq->cmd_flags & REQ_FAILED)
137311a68244SKiyoshi Ueda 		mapped = false;
1374cec47e3dSKiyoshi Ueda 
137511a68244SKiyoshi Ueda 	dm_done(clone, tio->error, mapped);
1376cec47e3dSKiyoshi Ueda }
1377cec47e3dSKiyoshi Ueda 
1378cec47e3dSKiyoshi Ueda /*
1379cec47e3dSKiyoshi Ueda  * Complete the clone and the original request with the error status
1380cec47e3dSKiyoshi Ueda  * through softirq context.
1381cec47e3dSKiyoshi Ueda  */
1382466d89a6SKeith Busch static void dm_complete_request(struct request *rq, int error)
1383cec47e3dSKiyoshi Ueda {
1384bfebd1cdSMike Snitzer 	struct dm_rq_target_io *tio = tio_from_request(rq);
1385cec47e3dSKiyoshi Ueda 
1386cec47e3dSKiyoshi Ueda 	tio->error = error;
13876acfe68bSMike Snitzer 	if (!rq->q->mq_ops)
1388cec47e3dSKiyoshi Ueda 		blk_complete_request(rq);
13896acfe68bSMike Snitzer 	else
13906acfe68bSMike Snitzer 		blk_mq_complete_request(rq, error);
1391cec47e3dSKiyoshi Ueda }
1392cec47e3dSKiyoshi Ueda 
1393cec47e3dSKiyoshi Ueda /*
1394cec47e3dSKiyoshi Ueda  * Complete the not-mapped clone and the original request with the error status
1395cec47e3dSKiyoshi Ueda  * through softirq context.
1396cec47e3dSKiyoshi Ueda  * Target's rq_end_io() function isn't called.
1397e5863d9aSMike Snitzer  * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
1398cec47e3dSKiyoshi Ueda  */
1399466d89a6SKeith Busch static void dm_kill_unmapped_request(struct request *rq, int error)
1400cec47e3dSKiyoshi Ueda {
1401cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_FAILED;
1402466d89a6SKeith Busch 	dm_complete_request(rq, error);
1403cec47e3dSKiyoshi Ueda }
1404cec47e3dSKiyoshi Ueda 
1405cec47e3dSKiyoshi Ueda /*
1406eca7ee6dSMike Snitzer  * Called with the clone's queue lock held (in the case of .request_fn)
1407cec47e3dSKiyoshi Ueda  */
1408cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error)
1409cec47e3dSKiyoshi Ueda {
1410466d89a6SKeith Busch 	struct dm_rq_target_io *tio = clone->end_io_data;
1411466d89a6SKeith Busch 
1412e5863d9aSMike Snitzer 	if (!clone->q->mq_ops) {
1413cec47e3dSKiyoshi Ueda 		/*
1414cec47e3dSKiyoshi Ueda 		 * For just cleaning up the information of the queue in which
1415cec47e3dSKiyoshi Ueda 		 * the clone was dispatched.
1416e5863d9aSMike Snitzer 		 * The clone is *NOT* freed actually here because it is alloced
1417e5863d9aSMike Snitzer 		 * from dm own mempool (REQ_ALLOCED isn't set).
1418cec47e3dSKiyoshi Ueda 		 */
1419cec47e3dSKiyoshi Ueda 		__blk_put_request(clone->q, clone);
1420e5863d9aSMike Snitzer 	}
1421cec47e3dSKiyoshi Ueda 
1422cec47e3dSKiyoshi Ueda 	/*
1423cec47e3dSKiyoshi Ueda 	 * Actual request completion is done in a softirq context which doesn't
1424466d89a6SKeith Busch 	 * hold the clone's queue lock.  Otherwise, deadlock could occur because:
1425cec47e3dSKiyoshi Ueda 	 *     - another request may be submitted by the upper level driver
1426cec47e3dSKiyoshi Ueda 	 *       of the stacking during the completion
1427cec47e3dSKiyoshi Ueda 	 *     - the submission which requires queue lock may be done
1428466d89a6SKeith Busch 	 *       against this clone's queue
1429cec47e3dSKiyoshi Ueda 	 */
1430466d89a6SKeith Busch 	dm_complete_request(tio->orig, error);
1431cec47e3dSKiyoshi Ueda }
1432cec47e3dSKiyoshi Ueda 
143356a67df7SMike Snitzer /*
143456a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
143556a67df7SMike Snitzer  * target boundary.
143656a67df7SMike Snitzer  */
143756a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
14381da177e4SLinus Torvalds {
143956a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
144056a67df7SMike Snitzer 
144156a67df7SMike Snitzer 	return ti->len - target_offset;
144256a67df7SMike Snitzer }
144356a67df7SMike Snitzer 
144456a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
144556a67df7SMike Snitzer {
144656a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
1447542f9038SMike Snitzer 	sector_t offset, max_len;
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds 	/*
14501da177e4SLinus Torvalds 	 * Does the target need to split even further?
14511da177e4SLinus Torvalds 	 */
1452542f9038SMike Snitzer 	if (ti->max_io_len) {
1453542f9038SMike Snitzer 		offset = dm_target_offset(ti, sector);
1454542f9038SMike Snitzer 		if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1455542f9038SMike Snitzer 			max_len = sector_div(offset, ti->max_io_len);
1456542f9038SMike Snitzer 		else
1457542f9038SMike Snitzer 			max_len = offset & (ti->max_io_len - 1);
1458542f9038SMike Snitzer 		max_len = ti->max_io_len - max_len;
1459542f9038SMike Snitzer 
1460542f9038SMike Snitzer 		if (len > max_len)
1461542f9038SMike Snitzer 			len = max_len;
14621da177e4SLinus Torvalds 	}
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds 	return len;
14651da177e4SLinus Torvalds }
14661da177e4SLinus Torvalds 
1467542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1468542f9038SMike Snitzer {
1469542f9038SMike Snitzer 	if (len > UINT_MAX) {
1470542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1471542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
1472542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
1473542f9038SMike Snitzer 		return -EINVAL;
1474542f9038SMike Snitzer 	}
1475542f9038SMike Snitzer 
1476542f9038SMike Snitzer 	ti->max_io_len = (uint32_t) len;
1477542f9038SMike Snitzer 
1478542f9038SMike Snitzer 	return 0;
1479542f9038SMike Snitzer }
1480542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1481542f9038SMike Snitzer 
14821dd40c3eSMikulas Patocka /*
14831dd40c3eSMikulas Patocka  * A target may call dm_accept_partial_bio only from the map routine.  It is
14841dd40c3eSMikulas Patocka  * allowed for all bio types except REQ_FLUSH.
14851dd40c3eSMikulas Patocka  *
14861dd40c3eSMikulas Patocka  * dm_accept_partial_bio informs the dm that the target only wants to process
14871dd40c3eSMikulas Patocka  * additional n_sectors sectors of the bio and the rest of the data should be
14881dd40c3eSMikulas Patocka  * sent in a next bio.
14891dd40c3eSMikulas Patocka  *
14901dd40c3eSMikulas Patocka  * A diagram that explains the arithmetics:
14911dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
14921dd40c3eSMikulas Patocka  * |         1          |       2       |   3   |
14931dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
14941dd40c3eSMikulas Patocka  *
14951dd40c3eSMikulas Patocka  * <-------------- *tio->len_ptr --------------->
14961dd40c3eSMikulas Patocka  *                      <------- bi_size ------->
14971dd40c3eSMikulas Patocka  *                      <-- n_sectors -->
14981dd40c3eSMikulas Patocka  *
14991dd40c3eSMikulas Patocka  * Region 1 was already iterated over with bio_advance or similar function.
15001dd40c3eSMikulas Patocka  *	(it may be empty if the target doesn't use bio_advance)
15011dd40c3eSMikulas Patocka  * Region 2 is the remaining bio size that the target wants to process.
15021dd40c3eSMikulas Patocka  *	(it may be empty if region 1 is non-empty, although there is no reason
15031dd40c3eSMikulas Patocka  *	 to make it empty)
15041dd40c3eSMikulas Patocka  * The target requires that region 3 is to be sent in the next bio.
15051dd40c3eSMikulas Patocka  *
15061dd40c3eSMikulas Patocka  * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
15071dd40c3eSMikulas Patocka  * the partially processed part (the sum of regions 1+2) must be the same for all
15081dd40c3eSMikulas Patocka  * copies of the bio.
15091dd40c3eSMikulas Patocka  */
15101dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
15111dd40c3eSMikulas Patocka {
15121dd40c3eSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
15131dd40c3eSMikulas Patocka 	unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
15141dd40c3eSMikulas Patocka 	BUG_ON(bio->bi_rw & REQ_FLUSH);
15151dd40c3eSMikulas Patocka 	BUG_ON(bi_size > *tio->len_ptr);
15161dd40c3eSMikulas Patocka 	BUG_ON(n_sectors > bi_size);
15171dd40c3eSMikulas Patocka 	*tio->len_ptr -= bi_size - n_sectors;
15181dd40c3eSMikulas Patocka 	bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
15191dd40c3eSMikulas Patocka }
15201dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
15211dd40c3eSMikulas Patocka 
1522bd2a49b8SAlasdair G Kergon static void __map_bio(struct dm_target_io *tio)
15231da177e4SLinus Torvalds {
15241da177e4SLinus Torvalds 	int r;
15252056a782SJens Axboe 	sector_t sector;
15269faf400fSStefan Bader 	struct mapped_device *md;
1527dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
1528bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
15291da177e4SLinus Torvalds 
15301da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds 	/*
15331da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
15341da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
15351da177e4SLinus Torvalds 	 * this io.
15361da177e4SLinus Torvalds 	 */
15371da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
15384f024f37SKent Overstreet 	sector = clone->bi_iter.bi_sector;
15397de3ee57SMikulas Patocka 	r = ti->type->map(ti, clone);
154045cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
15411da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
15422056a782SJens Axboe 
1543d07335e5SMike Snitzer 		trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
154422a7c31aSAlan D. Brunelle 				      tio->io->bio->bi_bdev->bd_dev, sector);
15452056a782SJens Axboe 
15461da177e4SLinus Torvalds 		generic_make_request(clone);
15472e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
15482e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
15499faf400fSStefan Bader 		md = tio->io->md;
15509faf400fSStefan Bader 		dec_pending(tio->io, r);
15519faf400fSStefan Bader 		free_tio(md, tio);
1552ab37844dSMikulas Patocka 	} else if (r != DM_MAPIO_SUBMITTED) {
155345cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
155445cbcd79SKiyoshi Ueda 		BUG();
15551da177e4SLinus Torvalds 	}
15561da177e4SLinus Torvalds }
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds struct clone_info {
15591da177e4SLinus Torvalds 	struct mapped_device *md;
15601da177e4SLinus Torvalds 	struct dm_table *map;
15611da177e4SLinus Torvalds 	struct bio *bio;
15621da177e4SLinus Torvalds 	struct dm_io *io;
15631da177e4SLinus Torvalds 	sector_t sector;
1564e0d6609aSMikulas Patocka 	unsigned sector_count;
15651da177e4SLinus Torvalds };
15661da177e4SLinus Torvalds 
1567e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
1568bd2a49b8SAlasdair G Kergon {
15694f024f37SKent Overstreet 	bio->bi_iter.bi_sector = sector;
15704f024f37SKent Overstreet 	bio->bi_iter.bi_size = to_bytes(len);
15711da177e4SLinus Torvalds }
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds /*
15741da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
15751da177e4SLinus Torvalds  */
1576dba14160SMikulas Patocka static void clone_bio(struct dm_target_io *tio, struct bio *bio,
15771c3b13e6SKent Overstreet 		      sector_t sector, unsigned len)
15781da177e4SLinus Torvalds {
1579dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
15801da177e4SLinus Torvalds 
15811c3b13e6SKent Overstreet 	__bio_clone_fast(clone, bio);
15829c47008dSMartin K. Petersen 
15831c3b13e6SKent Overstreet 	if (bio_integrity(bio))
15841c3b13e6SKent Overstreet 		bio_integrity_clone(clone, bio, GFP_NOIO);
15851c3b13e6SKent Overstreet 
15861c3b13e6SKent Overstreet 	bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
15871c3b13e6SKent Overstreet 	clone->bi_iter.bi_size = to_bytes(len);
15881c3b13e6SKent Overstreet 
15891c3b13e6SKent Overstreet 	if (bio_integrity(bio))
15901c3b13e6SKent Overstreet 		bio_integrity_trim(clone, 0, len);
15911da177e4SLinus Torvalds }
15921da177e4SLinus Torvalds 
15939015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci,
159499778273SJunichi Nomura 				      struct dm_target *ti,
159555a62eefSAlasdair G Kergon 				      unsigned target_bio_nr)
1596f9ab94ceSMikulas Patocka {
1597dba14160SMikulas Patocka 	struct dm_target_io *tio;
1598dba14160SMikulas Patocka 	struct bio *clone;
1599dba14160SMikulas Patocka 
160099778273SJunichi Nomura 	clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1601dba14160SMikulas Patocka 	tio = container_of(clone, struct dm_target_io, clone);
1602f9ab94ceSMikulas Patocka 
1603f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1604f9ab94ceSMikulas Patocka 	tio->ti = ti;
160555a62eefSAlasdair G Kergon 	tio->target_bio_nr = target_bio_nr;
16069015df24SAlasdair G Kergon 
16079015df24SAlasdair G Kergon 	return tio;
16089015df24SAlasdair G Kergon }
16099015df24SAlasdair G Kergon 
161014fe594dSAlasdair G Kergon static void __clone_and_map_simple_bio(struct clone_info *ci,
161114fe594dSAlasdair G Kergon 				       struct dm_target *ti,
16121dd40c3eSMikulas Patocka 				       unsigned target_bio_nr, unsigned *len)
16139015df24SAlasdair G Kergon {
161499778273SJunichi Nomura 	struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
1615dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
16169015df24SAlasdair G Kergon 
16171dd40c3eSMikulas Patocka 	tio->len_ptr = len;
16181dd40c3eSMikulas Patocka 
16191c3b13e6SKent Overstreet 	__bio_clone_fast(clone, ci->bio);
1620bd2a49b8SAlasdair G Kergon 	if (len)
16211dd40c3eSMikulas Patocka 		bio_setup_sector(clone, ci->sector, *len);
1622f9ab94ceSMikulas Patocka 
1623bd2a49b8SAlasdair G Kergon 	__map_bio(tio);
1624f9ab94ceSMikulas Patocka }
1625f9ab94ceSMikulas Patocka 
162614fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
16271dd40c3eSMikulas Patocka 				  unsigned num_bios, unsigned *len)
162806a426ceSMike Snitzer {
162955a62eefSAlasdair G Kergon 	unsigned target_bio_nr;
163006a426ceSMike Snitzer 
163155a62eefSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
163214fe594dSAlasdair G Kergon 		__clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
163306a426ceSMike Snitzer }
163406a426ceSMike Snitzer 
163514fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1636f9ab94ceSMikulas Patocka {
163706a426ceSMike Snitzer 	unsigned target_nr = 0;
1638f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1639f9ab94ceSMikulas Patocka 
1640b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1641f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
16421dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
1643f9ab94ceSMikulas Patocka 
1644f9ab94ceSMikulas Patocka 	return 0;
1645f9ab94ceSMikulas Patocka }
1646f9ab94ceSMikulas Patocka 
1647e4c93811SAlasdair G Kergon static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
16481dd40c3eSMikulas Patocka 				     sector_t sector, unsigned *len)
16495ae89a87SMike Snitzer {
1650dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
16515ae89a87SMike Snitzer 	struct dm_target_io *tio;
1652b0d8ed4dSAlasdair G Kergon 	unsigned target_bio_nr;
1653b0d8ed4dSAlasdair G Kergon 	unsigned num_target_bios = 1;
16545ae89a87SMike Snitzer 
1655b0d8ed4dSAlasdair G Kergon 	/*
1656b0d8ed4dSAlasdair G Kergon 	 * Does the target want to receive duplicate copies of the bio?
1657b0d8ed4dSAlasdair G Kergon 	 */
1658b0d8ed4dSAlasdair G Kergon 	if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1659b0d8ed4dSAlasdair G Kergon 		num_target_bios = ti->num_write_bios(ti, bio);
1660e4c93811SAlasdair G Kergon 
1661b0d8ed4dSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
166299778273SJunichi Nomura 		tio = alloc_tio(ci, ti, target_bio_nr);
16631dd40c3eSMikulas Patocka 		tio->len_ptr = len;
16641dd40c3eSMikulas Patocka 		clone_bio(tio, bio, sector, *len);
1665bd2a49b8SAlasdair G Kergon 		__map_bio(tio);
16665ae89a87SMike Snitzer 	}
1667b0d8ed4dSAlasdair G Kergon }
16685ae89a87SMike Snitzer 
166955a62eefSAlasdair G Kergon typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
167023508a96SMike Snitzer 
167155a62eefSAlasdair G Kergon static unsigned get_num_discard_bios(struct dm_target *ti)
167223508a96SMike Snitzer {
167355a62eefSAlasdair G Kergon 	return ti->num_discard_bios;
167423508a96SMike Snitzer }
167523508a96SMike Snitzer 
167655a62eefSAlasdair G Kergon static unsigned get_num_write_same_bios(struct dm_target *ti)
167723508a96SMike Snitzer {
167855a62eefSAlasdair G Kergon 	return ti->num_write_same_bios;
167923508a96SMike Snitzer }
168023508a96SMike Snitzer 
168123508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti);
168223508a96SMike Snitzer 
168323508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti)
168423508a96SMike Snitzer {
168555a62eefSAlasdair G Kergon 	return ti->split_discard_bios;
168623508a96SMike Snitzer }
168723508a96SMike Snitzer 
168814fe594dSAlasdair G Kergon static int __send_changing_extent_only(struct clone_info *ci,
168955a62eefSAlasdair G Kergon 				       get_num_bios_fn get_num_bios,
169023508a96SMike Snitzer 				       is_split_required_fn is_split_required)
16915ae89a87SMike Snitzer {
16925ae89a87SMike Snitzer 	struct dm_target *ti;
1693e0d6609aSMikulas Patocka 	unsigned len;
169455a62eefSAlasdair G Kergon 	unsigned num_bios;
16955ae89a87SMike Snitzer 
1696a79245b3SMike Snitzer 	do {
16975ae89a87SMike Snitzer 		ti = dm_table_find_target(ci->map, ci->sector);
16985ae89a87SMike Snitzer 		if (!dm_target_is_valid(ti))
16995ae89a87SMike Snitzer 			return -EIO;
17005ae89a87SMike Snitzer 
17015ae89a87SMike Snitzer 		/*
170223508a96SMike Snitzer 		 * Even though the device advertised support for this type of
170323508a96SMike Snitzer 		 * request, that does not mean every target supports it, and
1704936688d7SMike Snitzer 		 * reconfiguration might also have changed that since the
17055ae89a87SMike Snitzer 		 * check was performed.
17065ae89a87SMike Snitzer 		 */
170755a62eefSAlasdair G Kergon 		num_bios = get_num_bios ? get_num_bios(ti) : 0;
170855a62eefSAlasdair G Kergon 		if (!num_bios)
17095ae89a87SMike Snitzer 			return -EOPNOTSUPP;
17105ae89a87SMike Snitzer 
171123508a96SMike Snitzer 		if (is_split_required && !is_split_required(ti))
1712e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
17137acf0277SMikulas Patocka 		else
1714e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
17155ae89a87SMike Snitzer 
17161dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, num_bios, &len);
17175ae89a87SMike Snitzer 
1718a79245b3SMike Snitzer 		ci->sector += len;
1719a79245b3SMike Snitzer 	} while (ci->sector_count -= len);
17205ae89a87SMike Snitzer 
17215ae89a87SMike Snitzer 	return 0;
17225ae89a87SMike Snitzer }
17235ae89a87SMike Snitzer 
172414fe594dSAlasdair G Kergon static int __send_discard(struct clone_info *ci)
172523508a96SMike Snitzer {
172614fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_discard_bios,
172723508a96SMike Snitzer 					   is_split_required_for_discard);
172823508a96SMike Snitzer }
172923508a96SMike Snitzer 
173014fe594dSAlasdair G Kergon static int __send_write_same(struct clone_info *ci)
173123508a96SMike Snitzer {
173214fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
173323508a96SMike Snitzer }
173423508a96SMike Snitzer 
1735e4c93811SAlasdair G Kergon /*
1736e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1737e4c93811SAlasdair G Kergon  */
1738e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci)
1739e4c93811SAlasdair G Kergon {
1740e4c93811SAlasdair G Kergon 	struct bio *bio = ci->bio;
1741e4c93811SAlasdair G Kergon 	struct dm_target *ti;
17421c3b13e6SKent Overstreet 	unsigned len;
1743e4c93811SAlasdair G Kergon 
1744e4c93811SAlasdair G Kergon 	if (unlikely(bio->bi_rw & REQ_DISCARD))
1745e4c93811SAlasdair G Kergon 		return __send_discard(ci);
1746e4c93811SAlasdair G Kergon 	else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
1747e4c93811SAlasdair G Kergon 		return __send_write_same(ci);
1748e4c93811SAlasdair G Kergon 
1749e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1750e4c93811SAlasdair G Kergon 	if (!dm_target_is_valid(ti))
1751e4c93811SAlasdair G Kergon 		return -EIO;
1752e4c93811SAlasdair G Kergon 
17531c3b13e6SKent Overstreet 	len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
1754e4c93811SAlasdair G Kergon 
17551dd40c3eSMikulas Patocka 	__clone_and_map_data_bio(ci, ti, ci->sector, &len);
1756e4c93811SAlasdair G Kergon 
1757e4c93811SAlasdair G Kergon 	ci->sector += len;
1758e4c93811SAlasdair G Kergon 	ci->sector_count -= len;
1759e4c93811SAlasdair G Kergon 
1760e4c93811SAlasdair G Kergon 	return 0;
1761e4c93811SAlasdair G Kergon }
1762e4c93811SAlasdair G Kergon 
1763e4c93811SAlasdair G Kergon /*
176414fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
17651da177e4SLinus Torvalds  */
176683d5e5b0SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md,
176783d5e5b0SMikulas Patocka 				    struct dm_table *map, struct bio *bio)
17681da177e4SLinus Torvalds {
17691da177e4SLinus Torvalds 	struct clone_info ci;
1770512875bdSJun'ichi Nomura 	int error = 0;
17711da177e4SLinus Torvalds 
177283d5e5b0SMikulas Patocka 	if (unlikely(!map)) {
1773f0b9a450SMikulas Patocka 		bio_io_error(bio);
1774f0b9a450SMikulas Patocka 		return;
1775f0b9a450SMikulas Patocka 	}
1776692d0eb9SMikulas Patocka 
177783d5e5b0SMikulas Patocka 	ci.map = map;
17781da177e4SLinus Torvalds 	ci.md = md;
17791da177e4SLinus Torvalds 	ci.io = alloc_io(md);
17801da177e4SLinus Torvalds 	ci.io->error = 0;
17811da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
17821da177e4SLinus Torvalds 	ci.io->bio = bio;
17831da177e4SLinus Torvalds 	ci.io->md = md;
1784f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
17854f024f37SKent Overstreet 	ci.sector = bio->bi_iter.bi_sector;
17861da177e4SLinus Torvalds 
17873eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1788bd2a49b8SAlasdair G Kergon 
1789b372d360SMike Snitzer 	if (bio->bi_rw & REQ_FLUSH) {
1790b372d360SMike Snitzer 		ci.bio = &ci.md->flush_bio;
1791b372d360SMike Snitzer 		ci.sector_count = 0;
179214fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1793b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1794b372d360SMike Snitzer 	} else {
17956a8736d1STejun Heo 		ci.bio = bio;
1796f6fccb12SMilan Broz 		ci.sector_count = bio_sectors(bio);
1797512875bdSJun'ichi Nomura 		while (ci.sector_count && !error)
179814fe594dSAlasdair G Kergon 			error = __split_and_process_non_flush(&ci);
1799d87f4c14STejun Heo 	}
18001da177e4SLinus Torvalds 
18011da177e4SLinus Torvalds 	/* drop the extra reference count */
1802512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
18039e4e5f87SMilan Broz }
18049e4e5f87SMilan Broz /*-----------------------------------------------------------------
18051da177e4SLinus Torvalds  * CRUD END
18061da177e4SLinus Torvalds  *---------------------------------------------------------------*/
18071da177e4SLinus Torvalds 
18081da177e4SLinus Torvalds /*
18091da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
18101da177e4SLinus Torvalds  * dm_merge_bvec.
18111da177e4SLinus Torvalds  */
1812dece1635SJens Axboe static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
18131da177e4SLinus Torvalds {
181412f03a49SKevin Corry 	int rw = bio_data_dir(bio);
18151da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
181683d5e5b0SMikulas Patocka 	int srcu_idx;
181783d5e5b0SMikulas Patocka 	struct dm_table *map;
18181da177e4SLinus Torvalds 
181983d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
18201da177e4SLinus Torvalds 
182118c0b223SGu Zheng 	generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
182212f03a49SKevin Corry 
18236a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
18246a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
182583d5e5b0SMikulas Patocka 		dm_put_live_table(md, srcu_idx);
18261da177e4SLinus Torvalds 
18276a8736d1STejun Heo 		if (bio_rw(bio) != READA)
182892c63902SMikulas Patocka 			queue_io(md, bio);
18296a8736d1STejun Heo 		else
18306a8736d1STejun Heo 			bio_io_error(bio);
1831dece1635SJens Axboe 		return BLK_QC_T_NONE;
18321da177e4SLinus Torvalds 	}
18331da177e4SLinus Torvalds 
183483d5e5b0SMikulas Patocka 	__split_and_process_bio(md, map, bio);
183583d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
1836dece1635SJens Axboe 	return BLK_QC_T_NONE;
1837cec47e3dSKiyoshi Ueda }
1838cec47e3dSKiyoshi Ueda 
1839fd2ed4d2SMikulas Patocka int dm_request_based(struct mapped_device *md)
1840cec47e3dSKiyoshi Ueda {
1841cec47e3dSKiyoshi Ueda 	return blk_queue_stackable(md->queue);
1842cec47e3dSKiyoshi Ueda }
1843cec47e3dSKiyoshi Ueda 
1844466d89a6SKeith Busch static void dm_dispatch_clone_request(struct request *clone, struct request *rq)
1845cec47e3dSKiyoshi Ueda {
1846cec47e3dSKiyoshi Ueda 	int r;
1847cec47e3dSKiyoshi Ueda 
1848466d89a6SKeith Busch 	if (blk_queue_io_stat(clone->q))
1849466d89a6SKeith Busch 		clone->cmd_flags |= REQ_IO_STAT;
1850cec47e3dSKiyoshi Ueda 
1851466d89a6SKeith Busch 	clone->start_time = jiffies;
1852466d89a6SKeith Busch 	r = blk_insert_cloned_request(clone->q, clone);
1853cec47e3dSKiyoshi Ueda 	if (r)
1854466d89a6SKeith Busch 		/* must complete clone in terms of original request */
1855cec47e3dSKiyoshi Ueda 		dm_complete_request(rq, r);
1856cec47e3dSKiyoshi Ueda }
1857cec47e3dSKiyoshi Ueda 
185878d8e58aSMike Snitzer static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
185978d8e58aSMike Snitzer 				 void *data)
1860cec47e3dSKiyoshi Ueda {
186178d8e58aSMike Snitzer 	struct dm_rq_target_io *tio = data;
186278d8e58aSMike Snitzer 	struct dm_rq_clone_bio_info *info =
186378d8e58aSMike Snitzer 		container_of(bio, struct dm_rq_clone_bio_info, clone);
186478d8e58aSMike Snitzer 
186578d8e58aSMike Snitzer 	info->orig = bio_orig;
186678d8e58aSMike Snitzer 	info->tio = tio;
186778d8e58aSMike Snitzer 	bio->bi_end_io = end_clone_bio;
186878d8e58aSMike Snitzer 
186978d8e58aSMike Snitzer 	return 0;
187078d8e58aSMike Snitzer }
187178d8e58aSMike Snitzer 
187278d8e58aSMike Snitzer static int setup_clone(struct request *clone, struct request *rq,
187378d8e58aSMike Snitzer 		       struct dm_rq_target_io *tio, gfp_t gfp_mask)
187478d8e58aSMike Snitzer {
187578d8e58aSMike Snitzer 	int r;
187678d8e58aSMike Snitzer 
187778d8e58aSMike Snitzer 	r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask,
187878d8e58aSMike Snitzer 			      dm_rq_bio_constructor, tio);
187978d8e58aSMike Snitzer 	if (r)
188078d8e58aSMike Snitzer 		return r;
188178d8e58aSMike Snitzer 
188278d8e58aSMike Snitzer 	clone->cmd = rq->cmd;
188378d8e58aSMike Snitzer 	clone->cmd_len = rq->cmd_len;
188478d8e58aSMike Snitzer 	clone->sense = rq->sense;
1885cec47e3dSKiyoshi Ueda 	clone->end_io = end_clone_request;
1886cec47e3dSKiyoshi Ueda 	clone->end_io_data = tio;
188778d8e58aSMike Snitzer 
18881ae49ea2SMike Snitzer 	tio->clone = clone;
188978d8e58aSMike Snitzer 
189078d8e58aSMike Snitzer 	return 0;
1891cec47e3dSKiyoshi Ueda }
1892cec47e3dSKiyoshi Ueda 
1893eca7ee6dSMike Snitzer static struct request *clone_old_rq(struct request *rq, struct mapped_device *md,
18941ae49ea2SMike Snitzer 				    struct dm_rq_target_io *tio, gfp_t gfp_mask)
18956facdaffSKiyoshi Ueda {
189602233342SMike Snitzer 	/*
1897c5248f79SMike Snitzer 	 * Create clone for use with .request_fn request_queue
189802233342SMike Snitzer 	 */
189902233342SMike Snitzer 	struct request *clone;
19001ae49ea2SMike Snitzer 
1901eca7ee6dSMike Snitzer 	clone = alloc_old_clone_request(md, gfp_mask);
19021ae49ea2SMike Snitzer 	if (!clone)
19031ae49ea2SMike Snitzer 		return NULL;
19041ae49ea2SMike Snitzer 
19051ae49ea2SMike Snitzer 	blk_rq_init(NULL, clone);
190678d8e58aSMike Snitzer 	if (setup_clone(clone, rq, tio, gfp_mask)) {
190778d8e58aSMike Snitzer 		/* -ENOMEM */
1908eca7ee6dSMike Snitzer 		free_old_clone_request(md, clone);
190978d8e58aSMike Snitzer 		return NULL;
191078d8e58aSMike Snitzer 	}
19111ae49ea2SMike Snitzer 
19121ae49ea2SMike Snitzer 	return clone;
19131ae49ea2SMike Snitzer }
19141ae49ea2SMike Snitzer 
19152eb6e1e3SKeith Busch static void map_tio_request(struct kthread_work *work);
19162eb6e1e3SKeith Busch 
1917bfebd1cdSMike Snitzer static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
1918bfebd1cdSMike Snitzer 		     struct mapped_device *md)
1919bfebd1cdSMike Snitzer {
1920bfebd1cdSMike Snitzer 	tio->md = md;
1921bfebd1cdSMike Snitzer 	tio->ti = NULL;
1922bfebd1cdSMike Snitzer 	tio->clone = NULL;
1923bfebd1cdSMike Snitzer 	tio->orig = rq;
1924bfebd1cdSMike Snitzer 	tio->error = 0;
1925591ddcfcSMike Snitzer 	/*
1926591ddcfcSMike Snitzer 	 * Avoid initializing info for blk-mq; it passes
1927591ddcfcSMike Snitzer 	 * target-specific data through info.ptr
1928591ddcfcSMike Snitzer 	 * (see: dm_mq_init_request)
1929591ddcfcSMike Snitzer 	 */
1930591ddcfcSMike Snitzer 	if (!md->init_tio_pdu)
1931bfebd1cdSMike Snitzer 		memset(&tio->info, 0, sizeof(tio->info));
193202233342SMike Snitzer 	if (md->kworker_task)
1933bfebd1cdSMike Snitzer 		init_kthread_work(&tio->work, map_tio_request);
1934bfebd1cdSMike Snitzer }
1935bfebd1cdSMike Snitzer 
1936eca7ee6dSMike Snitzer static struct dm_rq_target_io *dm_old_prep_tio(struct request *rq,
1937eca7ee6dSMike Snitzer 					       struct mapped_device *md,
1938eca7ee6dSMike Snitzer 					       gfp_t gfp_mask)
19396facdaffSKiyoshi Ueda {
19406facdaffSKiyoshi Ueda 	struct dm_rq_target_io *tio;
1941e5863d9aSMike Snitzer 	int srcu_idx;
1942e5863d9aSMike Snitzer 	struct dm_table *table;
19436facdaffSKiyoshi Ueda 
1944eca7ee6dSMike Snitzer 	tio = alloc_old_rq_tio(md, gfp_mask);
19456facdaffSKiyoshi Ueda 	if (!tio)
19466facdaffSKiyoshi Ueda 		return NULL;
19476facdaffSKiyoshi Ueda 
1948bfebd1cdSMike Snitzer 	init_tio(tio, rq, md);
19496facdaffSKiyoshi Ueda 
1950e5863d9aSMike Snitzer 	table = dm_get_live_table(md, &srcu_idx);
1951eca7ee6dSMike Snitzer 	/*
1952eca7ee6dSMike Snitzer 	 * Must clone a request if this .request_fn DM device
1953eca7ee6dSMike Snitzer 	 * is stacked on .request_fn device(s).
1954eca7ee6dSMike Snitzer 	 */
1955e5863d9aSMike Snitzer 	if (!dm_table_mq_request_based(table)) {
1956eca7ee6dSMike Snitzer 		if (!clone_old_rq(rq, md, tio, gfp_mask)) {
1957e5863d9aSMike Snitzer 			dm_put_live_table(md, srcu_idx);
1958eca7ee6dSMike Snitzer 			free_old_rq_tio(tio);
19596facdaffSKiyoshi Ueda 			return NULL;
19606facdaffSKiyoshi Ueda 		}
1961e5863d9aSMike Snitzer 	}
1962e5863d9aSMike Snitzer 	dm_put_live_table(md, srcu_idx);
19636facdaffSKiyoshi Ueda 
1964466d89a6SKeith Busch 	return tio;
19656facdaffSKiyoshi Ueda }
19666facdaffSKiyoshi Ueda 
1967cec47e3dSKiyoshi Ueda /*
1968cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1969cec47e3dSKiyoshi Ueda  */
1970eca7ee6dSMike Snitzer static int dm_old_prep_fn(struct request_queue *q, struct request *rq)
1971cec47e3dSKiyoshi Ueda {
1972cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1973466d89a6SKeith Busch 	struct dm_rq_target_io *tio;
1974cec47e3dSKiyoshi Ueda 
1975cec47e3dSKiyoshi Ueda 	if (unlikely(rq->special)) {
1976cec47e3dSKiyoshi Ueda 		DMWARN("Already has something in rq->special.");
1977cec47e3dSKiyoshi Ueda 		return BLKPREP_KILL;
1978cec47e3dSKiyoshi Ueda 	}
1979cec47e3dSKiyoshi Ueda 
1980eca7ee6dSMike Snitzer 	tio = dm_old_prep_tio(rq, md, GFP_ATOMIC);
1981466d89a6SKeith Busch 	if (!tio)
1982cec47e3dSKiyoshi Ueda 		return BLKPREP_DEFER;
1983cec47e3dSKiyoshi Ueda 
1984466d89a6SKeith Busch 	rq->special = tio;
1985cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_DONTPREP;
1986cec47e3dSKiyoshi Ueda 
1987cec47e3dSKiyoshi Ueda 	return BLKPREP_OK;
1988cec47e3dSKiyoshi Ueda }
1989cec47e3dSKiyoshi Ueda 
19909eef87daSKiyoshi Ueda /*
19919eef87daSKiyoshi Ueda  * Returns:
1992e5863d9aSMike Snitzer  * 0                : the request has been processed
1993e5863d9aSMike Snitzer  * DM_MAPIO_REQUEUE : the original request needs to be requeued
1994e5863d9aSMike Snitzer  * < 0              : the request was completed due to failure
19959eef87daSKiyoshi Ueda  */
1996bfebd1cdSMike Snitzer static int map_request(struct dm_rq_target_io *tio, struct request *rq,
1997cec47e3dSKiyoshi Ueda 		       struct mapped_device *md)
1998cec47e3dSKiyoshi Ueda {
1999e5863d9aSMike Snitzer 	int r;
2000bfebd1cdSMike Snitzer 	struct dm_target *ti = tio->ti;
2001e5863d9aSMike Snitzer 	struct request *clone = NULL;
2002cec47e3dSKiyoshi Ueda 
2003e5863d9aSMike Snitzer 	if (tio->clone) {
2004e5863d9aSMike Snitzer 		clone = tio->clone;
2005cec47e3dSKiyoshi Ueda 		r = ti->type->map_rq(ti, clone, &tio->info);
2006e5863d9aSMike Snitzer 	} else {
2007e5863d9aSMike Snitzer 		r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
2008e5863d9aSMike Snitzer 		if (r < 0) {
2009e5863d9aSMike Snitzer 			/* The target wants to complete the I/O */
2010e5863d9aSMike Snitzer 			dm_kill_unmapped_request(rq, r);
2011e5863d9aSMike Snitzer 			return r;
2012e5863d9aSMike Snitzer 		}
20133a140755SJunichi Nomura 		if (r != DM_MAPIO_REMAPPED)
20143a140755SJunichi Nomura 			return r;
201578d8e58aSMike Snitzer 		if (setup_clone(clone, rq, tio, GFP_ATOMIC)) {
201678d8e58aSMike Snitzer 			/* -ENOMEM */
201778d8e58aSMike Snitzer 			ti->type->release_clone_rq(clone);
201878d8e58aSMike Snitzer 			return DM_MAPIO_REQUEUE;
201978d8e58aSMike Snitzer 		}
2020e5863d9aSMike Snitzer 	}
2021e5863d9aSMike Snitzer 
2022cec47e3dSKiyoshi Ueda 	switch (r) {
2023cec47e3dSKiyoshi Ueda 	case DM_MAPIO_SUBMITTED:
2024cec47e3dSKiyoshi Ueda 		/* The target has taken the I/O to submit by itself later */
2025cec47e3dSKiyoshi Ueda 		break;
2026cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REMAPPED:
2027cec47e3dSKiyoshi Ueda 		/* The target has remapped the I/O so dispatch it */
20286db4ccd6SJun'ichi Nomura 		trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
2029466d89a6SKeith Busch 				     blk_rq_pos(rq));
2030466d89a6SKeith Busch 		dm_dispatch_clone_request(clone, rq);
2031cec47e3dSKiyoshi Ueda 		break;
2032cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REQUEUE:
2033cec47e3dSKiyoshi Ueda 		/* The target wants to requeue the I/O */
20342d76fff1SMike Snitzer 		dm_requeue_original_request(md, tio->orig);
2035cec47e3dSKiyoshi Ueda 		break;
2036cec47e3dSKiyoshi Ueda 	default:
2037cec47e3dSKiyoshi Ueda 		if (r > 0) {
2038cec47e3dSKiyoshi Ueda 			DMWARN("unimplemented target map return value: %d", r);
2039cec47e3dSKiyoshi Ueda 			BUG();
2040cec47e3dSKiyoshi Ueda 		}
2041cec47e3dSKiyoshi Ueda 
2042cec47e3dSKiyoshi Ueda 		/* The target wants to complete the I/O */
2043466d89a6SKeith Busch 		dm_kill_unmapped_request(rq, r);
2044e5863d9aSMike Snitzer 		return r;
2045cec47e3dSKiyoshi Ueda 	}
20469eef87daSKiyoshi Ueda 
2047e5863d9aSMike Snitzer 	return 0;
2048cec47e3dSKiyoshi Ueda }
2049cec47e3dSKiyoshi Ueda 
20502eb6e1e3SKeith Busch static void map_tio_request(struct kthread_work *work)
2051ba1cbad9SMike Snitzer {
20522eb6e1e3SKeith Busch 	struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work);
2053e5863d9aSMike Snitzer 	struct request *rq = tio->orig;
2054e5863d9aSMike Snitzer 	struct mapped_device *md = tio->md;
2055ba1cbad9SMike Snitzer 
2056bfebd1cdSMike Snitzer 	if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE)
20572d76fff1SMike Snitzer 		dm_requeue_original_request(md, rq);
20582eb6e1e3SKeith Busch }
20592eb6e1e3SKeith Busch 
2060466d89a6SKeith Busch static void dm_start_request(struct mapped_device *md, struct request *orig)
2061ba1cbad9SMike Snitzer {
2062bfebd1cdSMike Snitzer 	if (!orig->q->mq_ops)
2063ba1cbad9SMike Snitzer 		blk_start_request(orig);
2064bfebd1cdSMike Snitzer 	else
2065bfebd1cdSMike Snitzer 		blk_mq_start_request(orig);
2066466d89a6SKeith Busch 	atomic_inc(&md->pending[rq_data_dir(orig)]);
2067ba1cbad9SMike Snitzer 
20680ce65797SMike Snitzer 	if (md->seq_rq_merge_deadline_usecs) {
2069de3ec86dSMike Snitzer 		md->last_rq_pos = rq_end_sector(orig);
2070de3ec86dSMike Snitzer 		md->last_rq_rw = rq_data_dir(orig);
20710ce65797SMike Snitzer 		md->last_rq_start_time = ktime_get();
20720ce65797SMike Snitzer 	}
2073de3ec86dSMike Snitzer 
2074e262f347SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats))) {
2075e262f347SMikulas Patocka 		struct dm_rq_target_io *tio = tio_from_request(orig);
2076e262f347SMikulas Patocka 		tio->duration_jiffies = jiffies;
2077e262f347SMikulas Patocka 		tio->n_sectors = blk_rq_sectors(orig);
2078e262f347SMikulas Patocka 		dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
2079e262f347SMikulas Patocka 				    tio->n_sectors, false, 0, &tio->stats_aux);
2080e262f347SMikulas Patocka 	}
2081e262f347SMikulas Patocka 
2082ba1cbad9SMike Snitzer 	/*
2083ba1cbad9SMike Snitzer 	 * Hold the md reference here for the in-flight I/O.
2084ba1cbad9SMike Snitzer 	 * We can't rely on the reference count by device opener,
2085ba1cbad9SMike Snitzer 	 * because the device may be closed during the request completion
2086ba1cbad9SMike Snitzer 	 * when all bios are completed.
2087ba1cbad9SMike Snitzer 	 * See the comment in rq_completed() too.
2088ba1cbad9SMike Snitzer 	 */
2089ba1cbad9SMike Snitzer 	dm_get(md);
2090ba1cbad9SMike Snitzer }
2091ba1cbad9SMike Snitzer 
20920ce65797SMike Snitzer #define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000
20930ce65797SMike Snitzer 
20940ce65797SMike Snitzer ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
20950ce65797SMike Snitzer {
20960ce65797SMike Snitzer 	return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs);
20970ce65797SMike Snitzer }
20980ce65797SMike Snitzer 
20990ce65797SMike Snitzer ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
21000ce65797SMike Snitzer 						     const char *buf, size_t count)
21010ce65797SMike Snitzer {
21020ce65797SMike Snitzer 	unsigned deadline;
21030ce65797SMike Snitzer 
210417e149b8SMike Snitzer 	if (!dm_request_based(md) || md->use_blk_mq)
21050ce65797SMike Snitzer 		return count;
21060ce65797SMike Snitzer 
21070ce65797SMike Snitzer 	if (kstrtouint(buf, 10, &deadline))
21080ce65797SMike Snitzer 		return -EINVAL;
21090ce65797SMike Snitzer 
21100ce65797SMike Snitzer 	if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS)
21110ce65797SMike Snitzer 		deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS;
21120ce65797SMike Snitzer 
21130ce65797SMike Snitzer 	md->seq_rq_merge_deadline_usecs = deadline;
21140ce65797SMike Snitzer 
21150ce65797SMike Snitzer 	return count;
21160ce65797SMike Snitzer }
21170ce65797SMike Snitzer 
21180ce65797SMike Snitzer static bool dm_request_peeked_before_merge_deadline(struct mapped_device *md)
21190ce65797SMike Snitzer {
21200ce65797SMike Snitzer 	ktime_t kt_deadline;
21210ce65797SMike Snitzer 
21220ce65797SMike Snitzer 	if (!md->seq_rq_merge_deadline_usecs)
21230ce65797SMike Snitzer 		return false;
21240ce65797SMike Snitzer 
21250ce65797SMike Snitzer 	kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC);
21260ce65797SMike Snitzer 	kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline);
21270ce65797SMike Snitzer 
21280ce65797SMike Snitzer 	return !ktime_after(ktime_get(), kt_deadline);
21290ce65797SMike Snitzer }
21300ce65797SMike Snitzer 
2131cec47e3dSKiyoshi Ueda /*
2132cec47e3dSKiyoshi Ueda  * q->request_fn for request-based dm.
2133cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
2134cec47e3dSKiyoshi Ueda  */
2135cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q)
2136cec47e3dSKiyoshi Ueda {
2137cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
2138c91852ffSMike Snitzer 	struct dm_target *ti = md->immutable_target;
2139466d89a6SKeith Busch 	struct request *rq;
21402eb6e1e3SKeith Busch 	struct dm_rq_target_io *tio;
2141c91852ffSMike Snitzer 	sector_t pos = 0;
2142c91852ffSMike Snitzer 
2143c91852ffSMike Snitzer 	if (unlikely(!ti)) {
2144c91852ffSMike Snitzer 		int srcu_idx;
2145c91852ffSMike Snitzer 		struct dm_table *map = dm_get_live_table(md, &srcu_idx);
2146c91852ffSMike Snitzer 
2147c91852ffSMike Snitzer 		ti = dm_table_find_target(map, pos);
2148c91852ffSMike Snitzer 		dm_put_live_table(md, srcu_idx);
2149c91852ffSMike Snitzer 	}
2150cec47e3dSKiyoshi Ueda 
2151cec47e3dSKiyoshi Ueda 	/*
2152b4324feeSKiyoshi Ueda 	 * For suspend, check blk_queue_stopped() and increment
2153b4324feeSKiyoshi Ueda 	 * ->pending within a single queue_lock not to increment the
2154b4324feeSKiyoshi Ueda 	 * number of in-flight I/Os after the queue is stopped in
2155b4324feeSKiyoshi Ueda 	 * dm_suspend().
2156cec47e3dSKiyoshi Ueda 	 */
21577eaceaccSJens Axboe 	while (!blk_queue_stopped(q)) {
2158cec47e3dSKiyoshi Ueda 		rq = blk_peek_request(q);
2159cec47e3dSKiyoshi Ueda 		if (!rq)
2160c91852ffSMike Snitzer 			return;
2161cec47e3dSKiyoshi Ueda 
216229e4013dSTejun Heo 		/* always use block 0 to find the target for flushes for now */
216329e4013dSTejun Heo 		pos = 0;
216429e4013dSTejun Heo 		if (!(rq->cmd_flags & REQ_FLUSH))
216529e4013dSTejun Heo 			pos = blk_rq_pos(rq);
2166d0bcb878SKiyoshi Ueda 
2167c91852ffSMike Snitzer 		if ((dm_request_peeked_before_merge_deadline(md) &&
21680ce65797SMike Snitzer 		     md_in_flight(md) && rq->bio && rq->bio->bi_vcnt == 1 &&
2169c91852ffSMike Snitzer 		     md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq)) ||
2170c91852ffSMike Snitzer 		    (ti->type->busy && ti->type->busy(ti))) {
2171c91852ffSMike Snitzer 			blk_delay_queue(q, HZ / 100);
2172c91852ffSMike Snitzer 			return;
2173c91852ffSMike Snitzer 		}
2174cec47e3dSKiyoshi Ueda 
2175466d89a6SKeith Busch 		dm_start_request(md, rq);
2176b4324feeSKiyoshi Ueda 
2177bfebd1cdSMike Snitzer 		tio = tio_from_request(rq);
21782eb6e1e3SKeith Busch 		/* Establish tio->ti before queuing work (map_tio_request) */
21792eb6e1e3SKeith Busch 		tio->ti = ti;
21802eb6e1e3SKeith Busch 		queue_kthread_work(&md->kworker, &tio->work);
2181052189a2SKiyoshi Ueda 		BUG_ON(!irqs_disabled());
2182cec47e3dSKiyoshi Ueda 	}
2183cec47e3dSKiyoshi Ueda }
2184cec47e3dSKiyoshi Ueda 
21851da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
21861da177e4SLinus Torvalds {
21878a57dfc6SChandra Seetharaman 	int r = bdi_bits;
21888a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
21898a57dfc6SChandra Seetharaman 	struct dm_table *map;
21901da177e4SLinus Torvalds 
21911eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2192e522c039SMike Snitzer 		if (dm_request_based(md)) {
2193cec47e3dSKiyoshi Ueda 			/*
2194e522c039SMike Snitzer 			 * With request-based DM we only need to check the
2195e522c039SMike Snitzer 			 * top-level queue for congestion.
2196cec47e3dSKiyoshi Ueda 			 */
2197e522c039SMike Snitzer 			r = md->queue->backing_dev_info.wb.state & bdi_bits;
2198e522c039SMike Snitzer 		} else {
2199e522c039SMike Snitzer 			map = dm_get_live_table_fast(md);
2200e522c039SMike Snitzer 			if (map)
22011da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
220283d5e5b0SMikulas Patocka 			dm_put_live_table_fast(md);
22038a57dfc6SChandra Seetharaman 		}
2204e522c039SMike Snitzer 	}
22058a57dfc6SChandra Seetharaman 
22061da177e4SLinus Torvalds 	return r;
22071da177e4SLinus Torvalds }
22081da177e4SLinus Torvalds 
22091da177e4SLinus Torvalds /*-----------------------------------------------------------------
22101da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
22111da177e4SLinus Torvalds  *---------------------------------------------------------------*/
22122b06cfffSAlasdair G Kergon static void free_minor(int minor)
22131da177e4SLinus Torvalds {
2214f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
22151da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
2216f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
22171da177e4SLinus Torvalds }
22181da177e4SLinus Torvalds 
22191da177e4SLinus Torvalds /*
22201da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
22211da177e4SLinus Torvalds  */
2222cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
22231da177e4SLinus Torvalds {
2224c9d76be6STejun Heo 	int r;
22251da177e4SLinus Torvalds 
22261da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
22271da177e4SLinus Torvalds 		return -EINVAL;
22281da177e4SLinus Torvalds 
2229c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
2230f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
22311da177e4SLinus Torvalds 
2232c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
22331da177e4SLinus Torvalds 
2234f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2235c9d76be6STejun Heo 	idr_preload_end();
2236c9d76be6STejun Heo 	if (r < 0)
2237c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
2238c9d76be6STejun Heo 	return 0;
22391da177e4SLinus Torvalds }
22401da177e4SLinus Torvalds 
2241cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
22421da177e4SLinus Torvalds {
2243c9d76be6STejun Heo 	int r;
22441da177e4SLinus Torvalds 
2245c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
2246f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
22471da177e4SLinus Torvalds 
2248c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
22491da177e4SLinus Torvalds 
2250f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2251c9d76be6STejun Heo 	idr_preload_end();
2252c9d76be6STejun Heo 	if (r < 0)
22531da177e4SLinus Torvalds 		return r;
2254c9d76be6STejun Heo 	*minor = r;
2255c9d76be6STejun Heo 	return 0;
22561da177e4SLinus Torvalds }
22571da177e4SLinus Torvalds 
225883d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
22591da177e4SLinus Torvalds 
226053d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
226153d5914fSMikulas Patocka 
22624a0b4ddfSMike Snitzer static void dm_init_md_queue(struct mapped_device *md)
22634a0b4ddfSMike Snitzer {
22644a0b4ddfSMike Snitzer 	/*
22654a0b4ddfSMike Snitzer 	 * Request-based dm devices cannot be stacked on top of bio-based dm
2266bfebd1cdSMike Snitzer 	 * devices.  The type of this dm device may not have been decided yet.
22674a0b4ddfSMike Snitzer 	 * The type is decided at the first table loading time.
22684a0b4ddfSMike Snitzer 	 * To prevent problematic device stacking, clear the queue flag
22694a0b4ddfSMike Snitzer 	 * for request stacking support until then.
22704a0b4ddfSMike Snitzer 	 *
22714a0b4ddfSMike Snitzer 	 * This queue is new, so no concurrency on the queue_flags.
22724a0b4ddfSMike Snitzer 	 */
22734a0b4ddfSMike Snitzer 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
2274ad5f498fSMikulas Patocka 
2275ad5f498fSMikulas Patocka 	/*
2276ad5f498fSMikulas Patocka 	 * Initialize data that will only be used by a non-blk-mq DM queue
2277ad5f498fSMikulas Patocka 	 * - must do so here (in alloc_dev callchain) before queue is used
2278ad5f498fSMikulas Patocka 	 */
2279ad5f498fSMikulas Patocka 	md->queue->queuedata = md;
2280ad5f498fSMikulas Patocka 	md->queue->backing_dev_info.congested_data = md;
2281bfebd1cdSMike Snitzer }
22824a0b4ddfSMike Snitzer 
2283eca7ee6dSMike Snitzer static void dm_init_normal_md_queue(struct mapped_device *md)
2284bfebd1cdSMike Snitzer {
228517e149b8SMike Snitzer 	md->use_blk_mq = false;
2286bfebd1cdSMike Snitzer 	dm_init_md_queue(md);
2287bfebd1cdSMike Snitzer 
2288bfebd1cdSMike Snitzer 	/*
2289bfebd1cdSMike Snitzer 	 * Initialize aspects of queue that aren't relevant for blk-mq
2290bfebd1cdSMike Snitzer 	 */
22914a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
22924a0b4ddfSMike Snitzer 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
22934a0b4ddfSMike Snitzer }
22944a0b4ddfSMike Snitzer 
22950f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md)
22960f20972fSMike Snitzer {
22970f20972fSMike Snitzer 	if (md->wq)
22980f20972fSMike Snitzer 		destroy_workqueue(md->wq);
22990f20972fSMike Snitzer 	if (md->kworker_task)
23000f20972fSMike Snitzer 		kthread_stop(md->kworker_task);
23010f20972fSMike Snitzer 	mempool_destroy(md->io_pool);
23020f20972fSMike Snitzer 	mempool_destroy(md->rq_pool);
23030f20972fSMike Snitzer 	if (md->bs)
23040f20972fSMike Snitzer 		bioset_free(md->bs);
23050f20972fSMike Snitzer 
2306b06075a9SMikulas Patocka 	cleanup_srcu_struct(&md->io_barrier);
2307b06075a9SMikulas Patocka 
23080f20972fSMike Snitzer 	if (md->disk) {
23090f20972fSMike Snitzer 		spin_lock(&_minor_lock);
23100f20972fSMike Snitzer 		md->disk->private_data = NULL;
23110f20972fSMike Snitzer 		spin_unlock(&_minor_lock);
23120f20972fSMike Snitzer 		del_gendisk(md->disk);
23130f20972fSMike Snitzer 		put_disk(md->disk);
23140f20972fSMike Snitzer 	}
23150f20972fSMike Snitzer 
23160f20972fSMike Snitzer 	if (md->queue)
23170f20972fSMike Snitzer 		blk_cleanup_queue(md->queue);
23180f20972fSMike Snitzer 
23190f20972fSMike Snitzer 	if (md->bdev) {
23200f20972fSMike Snitzer 		bdput(md->bdev);
23210f20972fSMike Snitzer 		md->bdev = NULL;
23220f20972fSMike Snitzer 	}
23230f20972fSMike Snitzer }
23240f20972fSMike Snitzer 
23251da177e4SLinus Torvalds /*
23261da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
23271da177e4SLinus Torvalds  */
23282b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
23291da177e4SLinus Torvalds {
2330*115485e8SMike Snitzer 	int r, numa_node_id = dm_get_numa_node();
2331*115485e8SMike Snitzer 	struct mapped_device *md;
2332ba61fdd1SJeff Mahoney 	void *old_md;
23331da177e4SLinus Torvalds 
2334*115485e8SMike Snitzer 	md = kzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
23351da177e4SLinus Torvalds 	if (!md) {
23361da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
23371da177e4SLinus Torvalds 		return NULL;
23381da177e4SLinus Torvalds 	}
23391da177e4SLinus Torvalds 
234010da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
23416ed7ade8SMilan Broz 		goto bad_module_get;
234210da4f79SJeff Mahoney 
23431da177e4SLinus Torvalds 	/* get a minor number for the dev */
23442b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
2345cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
23462b06cfffSAlasdair G Kergon 	else
2347cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
23481da177e4SLinus Torvalds 	if (r < 0)
23496ed7ade8SMilan Broz 		goto bad_minor;
23501da177e4SLinus Torvalds 
235183d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
235283d5e5b0SMikulas Patocka 	if (r < 0)
235383d5e5b0SMikulas Patocka 		goto bad_io_barrier;
235483d5e5b0SMikulas Patocka 
2355*115485e8SMike Snitzer 	md->numa_node_id = numa_node_id;
235617e149b8SMike Snitzer 	md->use_blk_mq = use_blk_mq;
2357591ddcfcSMike Snitzer 	md->init_tio_pdu = false;
2358a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
2359e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
2360a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
236186f1152bSBenjamin Marzinski 	mutex_init(&md->table_devices_lock);
2362022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
23631da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
23645c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
23651da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
23667a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
23677a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
236886f1152bSBenjamin Marzinski 	INIT_LIST_HEAD(&md->table_devices);
23697a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
23701da177e4SLinus Torvalds 
2371*115485e8SMike Snitzer 	md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id);
23721da177e4SLinus Torvalds 	if (!md->queue)
23730f20972fSMike Snitzer 		goto bad;
23741da177e4SLinus Torvalds 
23754a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
23769faf400fSStefan Bader 
2377*115485e8SMike Snitzer 	md->disk = alloc_disk_node(1, numa_node_id);
23781da177e4SLinus Torvalds 	if (!md->disk)
23790f20972fSMike Snitzer 		goto bad;
23801da177e4SLinus Torvalds 
2381316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
2382316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
2383f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
238453d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
2385f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
23862995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
23872eb6e1e3SKeith Busch 	md->kworker_task = NULL;
2388f0b04115SJeff Mahoney 
23891da177e4SLinus Torvalds 	md->disk->major = _major;
23901da177e4SLinus Torvalds 	md->disk->first_minor = minor;
23911da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
23921da177e4SLinus Torvalds 	md->disk->queue = md->queue;
23931da177e4SLinus Torvalds 	md->disk->private_data = md;
23941da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
23951da177e4SLinus Torvalds 	add_disk(md->disk);
23967e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
23971da177e4SLinus Torvalds 
2398670368a8STejun Heo 	md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
2399304f3f6aSMilan Broz 	if (!md->wq)
24000f20972fSMike Snitzer 		goto bad;
2401304f3f6aSMilan Broz 
240232a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
240332a926daSMikulas Patocka 	if (!md->bdev)
24040f20972fSMike Snitzer 		goto bad;
240532a926daSMikulas Patocka 
24066a8736d1STejun Heo 	bio_init(&md->flush_bio);
24076a8736d1STejun Heo 	md->flush_bio.bi_bdev = md->bdev;
24086a8736d1STejun Heo 	md->flush_bio.bi_rw = WRITE_FLUSH;
24096a8736d1STejun Heo 
2410fd2ed4d2SMikulas Patocka 	dm_stats_init(&md->stats);
2411fd2ed4d2SMikulas Patocka 
2412ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
2413f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
2414ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
2415f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2416ba61fdd1SJeff Mahoney 
2417ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
2418ba61fdd1SJeff Mahoney 
24191da177e4SLinus Torvalds 	return md;
24201da177e4SLinus Torvalds 
24210f20972fSMike Snitzer bad:
24220f20972fSMike Snitzer 	cleanup_mapped_device(md);
242383d5e5b0SMikulas Patocka bad_io_barrier:
24241da177e4SLinus Torvalds 	free_minor(minor);
24256ed7ade8SMilan Broz bad_minor:
242610da4f79SJeff Mahoney 	module_put(THIS_MODULE);
24276ed7ade8SMilan Broz bad_module_get:
24281da177e4SLinus Torvalds 	kfree(md);
24291da177e4SLinus Torvalds 	return NULL;
24301da177e4SLinus Torvalds }
24311da177e4SLinus Torvalds 
2432ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
2433ae9da83fSJun'ichi Nomura 
24341da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
24351da177e4SLinus Torvalds {
2436f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
243763d94e48SJun'ichi Nomura 
2438ae9da83fSJun'ichi Nomura 	unlock_fs(md);
24392eb6e1e3SKeith Busch 
24400f20972fSMike Snitzer 	cleanup_mapped_device(md);
24411c357a1eSMike Snitzer 	if (md->tag_set) {
24421c357a1eSMike Snitzer 		blk_mq_free_tag_set(md->tag_set);
24431c357a1eSMike Snitzer 		kfree(md->tag_set);
24441c357a1eSMike Snitzer 	}
24450f20972fSMike Snitzer 
24460f20972fSMike Snitzer 	free_table_devices(&md->table_devices);
24470f20972fSMike Snitzer 	dm_stats_cleanup(&md->stats);
244863a4f065SMike Snitzer 	free_minor(minor);
244963a4f065SMike Snitzer 
245010da4f79SJeff Mahoney 	module_put(THIS_MODULE);
24511da177e4SLinus Torvalds 	kfree(md);
24521da177e4SLinus Torvalds }
24531da177e4SLinus Torvalds 
2454e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2455e6ee8c0bSKiyoshi Ueda {
2456c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
2457e6ee8c0bSKiyoshi Ueda 
24584e6e36c3SMike Snitzer 	if (md->bs) {
24594e6e36c3SMike Snitzer 		/* The md already has necessary mempools. */
24604e6e36c3SMike Snitzer 		if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
2461c0820cf5SMikulas Patocka 			/*
246216245bdcSJun'ichi Nomura 			 * Reload bioset because front_pad may have changed
246316245bdcSJun'ichi Nomura 			 * because a different table was loaded.
2464c0820cf5SMikulas Patocka 			 */
2465c0820cf5SMikulas Patocka 			bioset_free(md->bs);
2466c0820cf5SMikulas Patocka 			md->bs = p->bs;
2467c0820cf5SMikulas Patocka 			p->bs = NULL;
2468c0820cf5SMikulas Patocka 		}
2469cbc4e3c1SMike Snitzer 		/*
24704e6e36c3SMike Snitzer 		 * There's no need to reload with request-based dm
24714e6e36c3SMike Snitzer 		 * because the size of front_pad doesn't change.
24724e6e36c3SMike Snitzer 		 * Note for future: If you are to reload bioset,
24734e6e36c3SMike Snitzer 		 * prep-ed requests in the queue may refer
24744e6e36c3SMike Snitzer 		 * to bio from the old bioset, so you must walk
24754e6e36c3SMike Snitzer 		 * through the queue to unprep.
2476cbc4e3c1SMike Snitzer 		 */
2477cbc4e3c1SMike Snitzer 		goto out;
2478cbc4e3c1SMike Snitzer 	}
2479cbc4e3c1SMike Snitzer 
2480cbc4e3c1SMike Snitzer 	BUG_ON(!p || md->io_pool || md->rq_pool || md->bs);
2481e6ee8c0bSKiyoshi Ueda 
2482e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
2483e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
24841ae49ea2SMike Snitzer 	md->rq_pool = p->rq_pool;
24851ae49ea2SMike Snitzer 	p->rq_pool = NULL;
2486e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
2487e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
24884e6e36c3SMike Snitzer 
2489e6ee8c0bSKiyoshi Ueda out:
249002233342SMike Snitzer 	/* mempool bind completed, no longer need any mempools in the table */
2491e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
2492e6ee8c0bSKiyoshi Ueda }
2493e6ee8c0bSKiyoshi Ueda 
24941da177e4SLinus Torvalds /*
24951da177e4SLinus Torvalds  * Bind a table to the device.
24961da177e4SLinus Torvalds  */
24971da177e4SLinus Torvalds static void event_callback(void *context)
24981da177e4SLinus Torvalds {
24997a8c3d3bSMike Anderson 	unsigned long flags;
25007a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
25011da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
25021da177e4SLinus Torvalds 
25037a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
25047a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
25057a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
25067a8c3d3bSMike Anderson 
2507ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
25087a8c3d3bSMike Anderson 
25091da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
25101da177e4SLinus Torvalds 	wake_up(&md->eventq);
25111da177e4SLinus Torvalds }
25121da177e4SLinus Torvalds 
2513c217649bSMike Snitzer /*
2514c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
2515c217649bSMike Snitzer  */
25164e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
25171da177e4SLinus Torvalds {
25184e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
25191da177e4SLinus Torvalds 
2520db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
25211da177e4SLinus Torvalds }
25221da177e4SLinus Torvalds 
2523042d2a9bSAlasdair G Kergon /*
2524042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2525042d2a9bSAlasdair G Kergon  */
2526042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2527754c5fc7SMike Snitzer 			       struct queue_limits *limits)
25281da177e4SLinus Torvalds {
2529042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
2530165125e1SJens Axboe 	struct request_queue *q = md->queue;
25311da177e4SLinus Torvalds 	sector_t size;
25321da177e4SLinus Torvalds 
25331da177e4SLinus Torvalds 	size = dm_table_get_size(t);
25343ac51e74SDarrick J. Wong 
25353ac51e74SDarrick J. Wong 	/*
25363ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
25373ac51e74SDarrick J. Wong 	 */
2538fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
25393ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
25403ac51e74SDarrick J. Wong 
25414e90188bSAlasdair G Kergon 	__set_size(md, size);
25421da177e4SLinus Torvalds 
2543cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
25442ca3310eSAlasdair G Kergon 
2545e6ee8c0bSKiyoshi Ueda 	/*
2546e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
2547e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
2548e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
2549e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
2550e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
2551e6ee8c0bSKiyoshi Ueda 	 */
255216f12266SMike Snitzer 	if (dm_table_request_based(t)) {
2553eca7ee6dSMike Snitzer 		dm_stop_queue(q);
255416f12266SMike Snitzer 		/*
255516f12266SMike Snitzer 		 * Leverage the fact that request-based DM targets are
255616f12266SMike Snitzer 		 * immutable singletons and establish md->immutable_target
255716f12266SMike Snitzer 		 * - used to optimize both dm_request_fn and dm_mq_queue_rq
255816f12266SMike Snitzer 		 */
255916f12266SMike Snitzer 		md->immutable_target = dm_table_get_immutable_target(t);
256016f12266SMike Snitzer 	}
2561e6ee8c0bSKiyoshi Ueda 
2562e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
2563e6ee8c0bSKiyoshi Ueda 
2564a12f5d48SEric Dumazet 	old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
256583d5e5b0SMikulas Patocka 	rcu_assign_pointer(md->map, t);
256636a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
256736a0456fSAlasdair G Kergon 
2568754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
256941abc4e1SHannes Reinecke 	if (old_map)
257083d5e5b0SMikulas Patocka 		dm_sync_table(md);
25712ca3310eSAlasdair G Kergon 
2572042d2a9bSAlasdair G Kergon 	return old_map;
25731da177e4SLinus Torvalds }
25741da177e4SLinus Torvalds 
2575a7940155SAlasdair G Kergon /*
2576a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2577a7940155SAlasdair G Kergon  */
2578a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
25791da177e4SLinus Torvalds {
2580a12f5d48SEric Dumazet 	struct dm_table *map = rcu_dereference_protected(md->map, 1);
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds 	if (!map)
2583a7940155SAlasdair G Kergon 		return NULL;
25841da177e4SLinus Torvalds 
25851da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
25869cdb8520SMonam Agarwal 	RCU_INIT_POINTER(md->map, NULL);
258783d5e5b0SMikulas Patocka 	dm_sync_table(md);
2588a7940155SAlasdair G Kergon 
2589a7940155SAlasdair G Kergon 	return map;
25901da177e4SLinus Torvalds }
25911da177e4SLinus Torvalds 
25921da177e4SLinus Torvalds /*
25931da177e4SLinus Torvalds  * Constructor for a new device.
25941da177e4SLinus Torvalds  */
25952b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
25961da177e4SLinus Torvalds {
25971da177e4SLinus Torvalds 	struct mapped_device *md;
25981da177e4SLinus Torvalds 
25992b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
26001da177e4SLinus Torvalds 	if (!md)
26011da177e4SLinus Torvalds 		return -ENXIO;
26021da177e4SLinus Torvalds 
2603784aae73SMilan Broz 	dm_sysfs_init(md);
2604784aae73SMilan Broz 
26051da177e4SLinus Torvalds 	*result = md;
26061da177e4SLinus Torvalds 	return 0;
26071da177e4SLinus Torvalds }
26081da177e4SLinus Torvalds 
2609a5664dadSMike Snitzer /*
2610a5664dadSMike Snitzer  * Functions to manage md->type.
2611a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2612a5664dadSMike Snitzer  */
2613a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2614a5664dadSMike Snitzer {
2615a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2616a5664dadSMike Snitzer }
2617a5664dadSMike Snitzer 
2618a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2619a5664dadSMike Snitzer {
2620a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2621a5664dadSMike Snitzer }
2622a5664dadSMike Snitzer 
2623a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type)
2624a5664dadSMike Snitzer {
262500c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2626a5664dadSMike Snitzer 	md->type = type;
2627a5664dadSMike Snitzer }
2628a5664dadSMike Snitzer 
2629a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md)
2630a5664dadSMike Snitzer {
2631a5664dadSMike Snitzer 	return md->type;
2632a5664dadSMike Snitzer }
2633a5664dadSMike Snitzer 
263436a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
263536a0456fSAlasdair G Kergon {
263636a0456fSAlasdair G Kergon 	return md->immutable_target_type;
263736a0456fSAlasdair G Kergon }
263836a0456fSAlasdair G Kergon 
26394a0b4ddfSMike Snitzer /*
2640f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
2641f84cb8a4SMike Snitzer  * count on 'md'.
2642f84cb8a4SMike Snitzer  */
2643f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2644f84cb8a4SMike Snitzer {
2645f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
2646f84cb8a4SMike Snitzer 	return &md->queue->limits;
2647f84cb8a4SMike Snitzer }
2648f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2649f84cb8a4SMike Snitzer 
2650eca7ee6dSMike Snitzer static void dm_old_init_rq_based_worker_thread(struct mapped_device *md)
2651bfebd1cdSMike Snitzer {
2652bfebd1cdSMike Snitzer 	/* Initialize the request-based DM worker thread */
2653bfebd1cdSMike Snitzer 	init_kthread_worker(&md->kworker);
2654bfebd1cdSMike Snitzer 	md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker,
2655bfebd1cdSMike Snitzer 				       "kdmwork-%s", dm_device_name(md));
2656bfebd1cdSMike Snitzer }
2657bfebd1cdSMike Snitzer 
2658f84cb8a4SMike Snitzer /*
2659eca7ee6dSMike Snitzer  * Fully initialize a .request_fn request-based queue.
26604a0b4ddfSMike Snitzer  */
2661eca7ee6dSMike Snitzer static int dm_old_init_request_queue(struct mapped_device *md)
26624a0b4ddfSMike Snitzer {
26634a0b4ddfSMike Snitzer 	struct request_queue *q = NULL;
26644a0b4ddfSMike Snitzer 
26654a0b4ddfSMike Snitzer 	/* Fully initialize the queue */
26664a0b4ddfSMike Snitzer 	q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
26674a0b4ddfSMike Snitzer 	if (!q)
2668bfebd1cdSMike Snitzer 		return -EINVAL;
26694a0b4ddfSMike Snitzer 
26700ce65797SMike Snitzer 	/* disable dm_request_fn's merge heuristic by default */
26710ce65797SMike Snitzer 	md->seq_rq_merge_deadline_usecs = 0;
26720ce65797SMike Snitzer 
26734a0b4ddfSMike Snitzer 	md->queue = q;
2674eca7ee6dSMike Snitzer 	dm_init_normal_md_queue(md);
26754a0b4ddfSMike Snitzer 	blk_queue_softirq_done(md->queue, dm_softirq_done);
2676eca7ee6dSMike Snitzer 	blk_queue_prep_rq(md->queue, dm_old_prep_fn);
26774a0b4ddfSMike Snitzer 
2678eca7ee6dSMike Snitzer 	dm_old_init_rq_based_worker_thread(md);
26792eb6e1e3SKeith Busch 
26804a0b4ddfSMike Snitzer 	elv_register_queue(md->queue);
26814a0b4ddfSMike Snitzer 
2682bfebd1cdSMike Snitzer 	return 0;
2683bfebd1cdSMike Snitzer }
2684bfebd1cdSMike Snitzer 
2685bfebd1cdSMike Snitzer static int dm_mq_init_request(void *data, struct request *rq,
2686bfebd1cdSMike Snitzer 			      unsigned int hctx_idx, unsigned int request_idx,
2687bfebd1cdSMike Snitzer 			      unsigned int numa_node)
2688bfebd1cdSMike Snitzer {
2689bfebd1cdSMike Snitzer 	struct mapped_device *md = data;
2690bfebd1cdSMike Snitzer 	struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2691bfebd1cdSMike Snitzer 
2692bfebd1cdSMike Snitzer 	/*
2693bfebd1cdSMike Snitzer 	 * Must initialize md member of tio, otherwise it won't
2694bfebd1cdSMike Snitzer 	 * be available in dm_mq_queue_rq.
2695bfebd1cdSMike Snitzer 	 */
2696bfebd1cdSMike Snitzer 	tio->md = md;
2697bfebd1cdSMike Snitzer 
2698591ddcfcSMike Snitzer 	if (md->init_tio_pdu) {
2699591ddcfcSMike Snitzer 		/* target-specific per-io data is immediately after the tio */
2700591ddcfcSMike Snitzer 		tio->info.ptr = tio + 1;
2701591ddcfcSMike Snitzer 	}
2702591ddcfcSMike Snitzer 
2703bfebd1cdSMike Snitzer 	return 0;
2704bfebd1cdSMike Snitzer }
2705bfebd1cdSMike Snitzer 
2706bfebd1cdSMike Snitzer static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
2707bfebd1cdSMike Snitzer 			  const struct blk_mq_queue_data *bd)
2708bfebd1cdSMike Snitzer {
2709bfebd1cdSMike Snitzer 	struct request *rq = bd->rq;
2710bfebd1cdSMike Snitzer 	struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2711bfebd1cdSMike Snitzer 	struct mapped_device *md = tio->md;
271216f12266SMike Snitzer 	struct dm_target *ti = md->immutable_target;
271316f12266SMike Snitzer 
271416f12266SMike Snitzer 	if (unlikely(!ti)) {
2715bfebd1cdSMike Snitzer 		int srcu_idx;
2716bfebd1cdSMike Snitzer 		struct dm_table *map = dm_get_live_table(md, &srcu_idx);
2717bfebd1cdSMike Snitzer 
271816f12266SMike Snitzer 		ti = dm_table_find_target(map, 0);
2719bfebd1cdSMike Snitzer 		dm_put_live_table(md, srcu_idx);
2720bfebd1cdSMike Snitzer 	}
2721bfebd1cdSMike Snitzer 
2722bfebd1cdSMike Snitzer 	if (ti->type->busy && ti->type->busy(ti))
2723bfebd1cdSMike Snitzer 		return BLK_MQ_RQ_QUEUE_BUSY;
2724bfebd1cdSMike Snitzer 
2725bfebd1cdSMike Snitzer 	dm_start_request(md, rq);
2726bfebd1cdSMike Snitzer 
2727bfebd1cdSMike Snitzer 	/* Init tio using md established in .init_request */
2728bfebd1cdSMike Snitzer 	init_tio(tio, rq, md);
2729bfebd1cdSMike Snitzer 
273002233342SMike Snitzer 	/*
273102233342SMike Snitzer 	 * Establish tio->ti before queuing work (map_tio_request)
273202233342SMike Snitzer 	 * or making direct call to map_request().
273302233342SMike Snitzer 	 */
2734bfebd1cdSMike Snitzer 	tio->ti = ti;
273502233342SMike Snitzer 
273602233342SMike Snitzer 	/* Direct call is fine since .queue_rq allows allocations */
273745714fbeSMike Snitzer 	if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) {
273845714fbeSMike Snitzer 		/* Undo dm_start_request() before requeuing */
2739e262f347SMikulas Patocka 		rq_end_stats(md, rq);
274045714fbeSMike Snitzer 		rq_completed(md, rq_data_dir(rq), false);
274145714fbeSMike Snitzer 		return BLK_MQ_RQ_QUEUE_BUSY;
274245714fbeSMike Snitzer 	}
2743bfebd1cdSMike Snitzer 
2744bfebd1cdSMike Snitzer 	return BLK_MQ_RQ_QUEUE_OK;
2745bfebd1cdSMike Snitzer }
2746bfebd1cdSMike Snitzer 
2747bfebd1cdSMike Snitzer static struct blk_mq_ops dm_mq_ops = {
2748bfebd1cdSMike Snitzer 	.queue_rq = dm_mq_queue_rq,
2749bfebd1cdSMike Snitzer 	.map_queue = blk_mq_map_queue,
2750bfebd1cdSMike Snitzer 	.complete = dm_softirq_done,
2751bfebd1cdSMike Snitzer 	.init_request = dm_mq_init_request,
2752bfebd1cdSMike Snitzer };
2753bfebd1cdSMike Snitzer 
2754591ddcfcSMike Snitzer static int dm_mq_init_request_queue(struct mapped_device *md,
2755591ddcfcSMike Snitzer 				    struct dm_target *immutable_tgt)
2756bfebd1cdSMike Snitzer {
2757bfebd1cdSMike Snitzer 	struct request_queue *q;
2758bfebd1cdSMike Snitzer 	int err;
2759bfebd1cdSMike Snitzer 
2760c5248f79SMike Snitzer 	if (dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) {
2761c5248f79SMike Snitzer 		DMERR("request-based dm-mq may only be stacked on blk-mq device(s)");
2762c5248f79SMike Snitzer 		return -EINVAL;
2763c5248f79SMike Snitzer 	}
2764c5248f79SMike Snitzer 
2765*115485e8SMike Snitzer 	md->tag_set = kzalloc_node(sizeof(struct blk_mq_tag_set), GFP_KERNEL, md->numa_node_id);
27661c357a1eSMike Snitzer 	if (!md->tag_set)
27671c357a1eSMike Snitzer 		return -ENOMEM;
27681c357a1eSMike Snitzer 
27691c357a1eSMike Snitzer 	md->tag_set->ops = &dm_mq_ops;
27701c357a1eSMike Snitzer 	md->tag_set->queue_depth = dm_get_blk_mq_queue_depth();
2771*115485e8SMike Snitzer 	md->tag_set->numa_node = md->numa_node_id;
27721c357a1eSMike Snitzer 	md->tag_set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
27731c357a1eSMike Snitzer 	md->tag_set->nr_hw_queues = dm_get_blk_mq_nr_hw_queues();
27741c357a1eSMike Snitzer 	md->tag_set->driver_data = md;
27751c357a1eSMike Snitzer 
27761c357a1eSMike Snitzer 	md->tag_set->cmd_size = sizeof(struct dm_rq_target_io);
2777591ddcfcSMike Snitzer 	if (immutable_tgt && immutable_tgt->per_io_data_size) {
2778591ddcfcSMike Snitzer 		/* any target-specific per-io data is immediately after the tio */
2779591ddcfcSMike Snitzer 		md->tag_set->cmd_size += immutable_tgt->per_io_data_size;
2780591ddcfcSMike Snitzer 		md->init_tio_pdu = true;
2781591ddcfcSMike Snitzer 	}
2782bfebd1cdSMike Snitzer 
27831c357a1eSMike Snitzer 	err = blk_mq_alloc_tag_set(md->tag_set);
2784bfebd1cdSMike Snitzer 	if (err)
27851c357a1eSMike Snitzer 		goto out_kfree_tag_set;
2786bfebd1cdSMike Snitzer 
27871c357a1eSMike Snitzer 	q = blk_mq_init_allocated_queue(md->tag_set, md->queue);
2788bfebd1cdSMike Snitzer 	if (IS_ERR(q)) {
2789bfebd1cdSMike Snitzer 		err = PTR_ERR(q);
2790bfebd1cdSMike Snitzer 		goto out_tag_set;
2791bfebd1cdSMike Snitzer 	}
2792bfebd1cdSMike Snitzer 	md->queue = q;
2793bfebd1cdSMike Snitzer 	dm_init_md_queue(md);
2794bfebd1cdSMike Snitzer 
2795bfebd1cdSMike Snitzer 	/* backfill 'mq' sysfs registration normally done in blk_register_queue */
2796bfebd1cdSMike Snitzer 	blk_mq_register_disk(md->disk);
2797bfebd1cdSMike Snitzer 
2798bfebd1cdSMike Snitzer 	return 0;
2799bfebd1cdSMike Snitzer 
2800bfebd1cdSMike Snitzer out_tag_set:
28011c357a1eSMike Snitzer 	blk_mq_free_tag_set(md->tag_set);
28021c357a1eSMike Snitzer out_kfree_tag_set:
28031c357a1eSMike Snitzer 	kfree(md->tag_set);
28041c357a1eSMike Snitzer 
2805bfebd1cdSMike Snitzer 	return err;
28064a0b4ddfSMike Snitzer }
28074a0b4ddfSMike Snitzer 
28084e6e36c3SMike Snitzer static unsigned filter_md_type(unsigned type, struct mapped_device *md)
28094e6e36c3SMike Snitzer {
28104e6e36c3SMike Snitzer 	if (type == DM_TYPE_BIO_BASED)
28114e6e36c3SMike Snitzer 		return type;
28124e6e36c3SMike Snitzer 
28134e6e36c3SMike Snitzer 	return !md->use_blk_mq ? DM_TYPE_REQUEST_BASED : DM_TYPE_MQ_REQUEST_BASED;
28144e6e36c3SMike Snitzer }
28154e6e36c3SMike Snitzer 
28164a0b4ddfSMike Snitzer /*
28174a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
28184a0b4ddfSMike Snitzer  */
2819591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
28204a0b4ddfSMike Snitzer {
2821bfebd1cdSMike Snitzer 	int r;
282217e149b8SMike Snitzer 	unsigned md_type = filter_md_type(dm_get_md_type(md), md);
2823bfebd1cdSMike Snitzer 
2824bfebd1cdSMike Snitzer 	switch (md_type) {
2825bfebd1cdSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
2826eca7ee6dSMike Snitzer 		r = dm_old_init_request_queue(md);
2827bfebd1cdSMike Snitzer 		if (r) {
2828eca7ee6dSMike Snitzer 			DMERR("Cannot initialize queue for request-based mapped device");
2829bfebd1cdSMike Snitzer 			return r;
28304a0b4ddfSMike Snitzer 		}
2831bfebd1cdSMike Snitzer 		break;
2832bfebd1cdSMike Snitzer 	case DM_TYPE_MQ_REQUEST_BASED:
2833591ddcfcSMike Snitzer 		r = dm_mq_init_request_queue(md, dm_table_get_immutable_target(t));
2834bfebd1cdSMike Snitzer 		if (r) {
2835eca7ee6dSMike Snitzer 			DMERR("Cannot initialize queue for request-based dm-mq mapped device");
2836bfebd1cdSMike Snitzer 			return r;
2837bfebd1cdSMike Snitzer 		}
2838bfebd1cdSMike Snitzer 		break;
2839bfebd1cdSMike Snitzer 	case DM_TYPE_BIO_BASED:
2840eca7ee6dSMike Snitzer 		dm_init_normal_md_queue(md);
2841ff36ab34SMike Snitzer 		blk_queue_make_request(md->queue, dm_make_request);
2842dbba42d8SMikulas Patocka 		/*
2843dbba42d8SMikulas Patocka 		 * DM handles splitting bios as needed.  Free the bio_split bioset
2844dbba42d8SMikulas Patocka 		 * since it won't be used (saves 1 process per bio-based DM device).
2845dbba42d8SMikulas Patocka 		 */
2846dbba42d8SMikulas Patocka 		bioset_free(md->queue->bio_split);
2847dbba42d8SMikulas Patocka 		md->queue->bio_split = NULL;
2848bfebd1cdSMike Snitzer 		break;
2849ff36ab34SMike Snitzer 	}
28504a0b4ddfSMike Snitzer 
28514a0b4ddfSMike Snitzer 	return 0;
28524a0b4ddfSMike Snitzer }
28534a0b4ddfSMike Snitzer 
28542bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev)
28551da177e4SLinus Torvalds {
28561da177e4SLinus Torvalds 	struct mapped_device *md;
28571da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
28581da177e4SLinus Torvalds 
28591da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
28601da177e4SLinus Torvalds 		return NULL;
28611da177e4SLinus Torvalds 
2862f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
28631da177e4SLinus Torvalds 
28641da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
28652bec1f4aSMikulas Patocka 	if (md) {
28662bec1f4aSMikulas Patocka 		if ((md == MINOR_ALLOCED ||
2867f331c029STejun Heo 		     (MINOR(disk_devt(dm_disk(md))) != minor) ||
2868abdc568bSKiyoshi Ueda 		     dm_deleting_md(md) ||
2869fba9f90eSJeff Mahoney 		     test_bit(DMF_FREEING, &md->flags))) {
2870637842cfSDavid Teigland 			md = NULL;
2871fba9f90eSJeff Mahoney 			goto out;
2872fba9f90eSJeff Mahoney 		}
28732bec1f4aSMikulas Patocka 		dm_get(md);
28742bec1f4aSMikulas Patocka 	}
28751da177e4SLinus Torvalds 
2876fba9f90eSJeff Mahoney out:
2877f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
28781da177e4SLinus Torvalds 
2879637842cfSDavid Teigland 	return md;
2880637842cfSDavid Teigland }
28813cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2882d229a958SDavid Teigland 
28839ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2884637842cfSDavid Teigland {
28859ade92a9SAlasdair G Kergon 	return md->interface_ptr;
28861da177e4SLinus Torvalds }
28871da177e4SLinus Torvalds 
28881da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
28891da177e4SLinus Torvalds {
28901da177e4SLinus Torvalds 	md->interface_ptr = ptr;
28911da177e4SLinus Torvalds }
28921da177e4SLinus Torvalds 
28931da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
28941da177e4SLinus Torvalds {
28951da177e4SLinus Torvalds 	atomic_inc(&md->holders);
28963f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
28971da177e4SLinus Torvalds }
28981da177e4SLinus Torvalds 
289909ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md)
290009ee96b2SMikulas Patocka {
290109ee96b2SMikulas Patocka 	spin_lock(&_minor_lock);
290209ee96b2SMikulas Patocka 	if (test_bit(DMF_FREEING, &md->flags)) {
290309ee96b2SMikulas Patocka 		spin_unlock(&_minor_lock);
290409ee96b2SMikulas Patocka 		return -EBUSY;
290509ee96b2SMikulas Patocka 	}
290609ee96b2SMikulas Patocka 	dm_get(md);
290709ee96b2SMikulas Patocka 	spin_unlock(&_minor_lock);
290809ee96b2SMikulas Patocka 	return 0;
290909ee96b2SMikulas Patocka }
291009ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold);
291109ee96b2SMikulas Patocka 
291272d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
291372d94861SAlasdair G Kergon {
291472d94861SAlasdair G Kergon 	return md->name;
291572d94861SAlasdair G Kergon }
291672d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
291772d94861SAlasdair G Kergon 
29183f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
29191da177e4SLinus Torvalds {
29201134e5aeSMike Anderson 	struct dm_table *map;
292183d5e5b0SMikulas Patocka 	int srcu_idx;
29221da177e4SLinus Torvalds 
29233f77316dSKiyoshi Ueda 	might_sleep();
2924fba9f90eSJeff Mahoney 
292563a4f065SMike Snitzer 	spin_lock(&_minor_lock);
29263f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2927fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2928f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
29293f77316dSKiyoshi Ueda 
293002233342SMike Snitzer 	if (dm_request_based(md) && md->kworker_task)
29312eb6e1e3SKeith Busch 		flush_kthread_worker(&md->kworker);
29322eb6e1e3SKeith Busch 
2933ab7c7bb6SMikulas Patocka 	/*
2934ab7c7bb6SMikulas Patocka 	 * Take suspend_lock so that presuspend and postsuspend methods
2935ab7c7bb6SMikulas Patocka 	 * do not race with internal suspend.
2936ab7c7bb6SMikulas Patocka 	 */
2937ab7c7bb6SMikulas Patocka 	mutex_lock(&md->suspend_lock);
29382a708cffSJunichi Nomura 	map = dm_get_live_table(md, &srcu_idx);
29394f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
29401da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
29411da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
29421da177e4SLinus Torvalds 	}
294383d5e5b0SMikulas Patocka 	/* dm_put_live_table must be before msleep, otherwise deadlock is possible */
294483d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
29452a708cffSJunichi Nomura 	mutex_unlock(&md->suspend_lock);
294683d5e5b0SMikulas Patocka 
29473f77316dSKiyoshi Ueda 	/*
29483f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
29493f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
29503f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
29513f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
29523f77316dSKiyoshi Ueda 	 */
29533f77316dSKiyoshi Ueda 	if (wait)
29543f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
29553f77316dSKiyoshi Ueda 			msleep(1);
29563f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
29573f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
29583f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
29593f77316dSKiyoshi Ueda 
2960784aae73SMilan Broz 	dm_sysfs_exit(md);
2961a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
29621da177e4SLinus Torvalds 	free_dev(md);
29631da177e4SLinus Torvalds }
29643f77316dSKiyoshi Ueda 
29653f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
29663f77316dSKiyoshi Ueda {
29673f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
29683f77316dSKiyoshi Ueda }
29693f77316dSKiyoshi Ueda 
29703f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
29713f77316dSKiyoshi Ueda {
29723f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
29733f77316dSKiyoshi Ueda }
29743f77316dSKiyoshi Ueda 
29753f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
29763f77316dSKiyoshi Ueda {
29773f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
29781da177e4SLinus Torvalds }
297979eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
29801da177e4SLinus Torvalds 
2981401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
298246125c1cSMilan Broz {
298346125c1cSMilan Broz 	int r = 0;
2984b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
2985b44ebeb0SMikulas Patocka 
2986b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
298746125c1cSMilan Broz 
298846125c1cSMilan Broz 	while (1) {
2989401600dfSMikulas Patocka 		set_current_state(interruptible);
299046125c1cSMilan Broz 
2991b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
299246125c1cSMilan Broz 			break;
299346125c1cSMilan Broz 
2994401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
2995401600dfSMikulas Patocka 		    signal_pending(current)) {
299646125c1cSMilan Broz 			r = -EINTR;
299746125c1cSMilan Broz 			break;
299846125c1cSMilan Broz 		}
299946125c1cSMilan Broz 
300046125c1cSMilan Broz 		io_schedule();
300146125c1cSMilan Broz 	}
300246125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
300346125c1cSMilan Broz 
3004b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
3005b44ebeb0SMikulas Patocka 
300646125c1cSMilan Broz 	return r;
300746125c1cSMilan Broz }
300846125c1cSMilan Broz 
30091da177e4SLinus Torvalds /*
30101da177e4SLinus Torvalds  * Process the deferred bios
30111da177e4SLinus Torvalds  */
3012ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
30131da177e4SLinus Torvalds {
3014ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
3015ef208587SMikulas Patocka 						work);
30166d6f10dfSMilan Broz 	struct bio *c;
301783d5e5b0SMikulas Patocka 	int srcu_idx;
301883d5e5b0SMikulas Patocka 	struct dm_table *map;
30191da177e4SLinus Torvalds 
302083d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
3021ef208587SMikulas Patocka 
30223b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
3023022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
3024022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
3025022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
3026022c2611SMikulas Patocka 
30276a8736d1STejun Heo 		if (!c)
3028df12ee99SAlasdair G Kergon 			break;
302973d410c0SMilan Broz 
3030e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
3031e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
3032af7e466aSMikulas Patocka 		else
303383d5e5b0SMikulas Patocka 			__split_and_process_bio(md, map, c);
3034e6ee8c0bSKiyoshi Ueda 	}
30353b00b203SMikulas Patocka 
303683d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
30371da177e4SLinus Torvalds }
30381da177e4SLinus Torvalds 
30399a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
3040304f3f6aSMilan Broz {
30413b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
30424e857c58SPeter Zijlstra 	smp_mb__after_atomic();
304353d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
3044304f3f6aSMilan Broz }
3045304f3f6aSMilan Broz 
30461da177e4SLinus Torvalds /*
3047042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
30481da177e4SLinus Torvalds  */
3049042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
30501da177e4SLinus Torvalds {
305187eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
3052754c5fc7SMike Snitzer 	struct queue_limits limits;
3053042d2a9bSAlasdair G Kergon 	int r;
30541da177e4SLinus Torvalds 
3055e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
30561da177e4SLinus Torvalds 
30571da177e4SLinus Torvalds 	/* device must be suspended */
30584f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
305993c534aeSAlasdair G Kergon 		goto out;
30601da177e4SLinus Torvalds 
30613ae70656SMike Snitzer 	/*
30623ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
30633ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
30643ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
30653ae70656SMike Snitzer 	 * reappear.
30663ae70656SMike Snitzer 	 */
30673ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
306883d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
30693ae70656SMike Snitzer 		if (live_map)
30703ae70656SMike Snitzer 			limits = md->queue->limits;
307183d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
30723ae70656SMike Snitzer 	}
30733ae70656SMike Snitzer 
307487eb5b21SMike Christie 	if (!live_map) {
3075754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
3076042d2a9bSAlasdair G Kergon 		if (r) {
3077042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
3078754c5fc7SMike Snitzer 			goto out;
3079042d2a9bSAlasdair G Kergon 		}
308087eb5b21SMike Christie 	}
3081754c5fc7SMike Snitzer 
3082042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
30831da177e4SLinus Torvalds 
308493c534aeSAlasdair G Kergon out:
3085e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
3086042d2a9bSAlasdair G Kergon 	return map;
30871da177e4SLinus Torvalds }
30881da177e4SLinus Torvalds 
30891da177e4SLinus Torvalds /*
30901da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
30911da177e4SLinus Torvalds  * device.
30921da177e4SLinus Torvalds  */
30932ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
30941da177e4SLinus Torvalds {
3095e39e2e95SAlasdair G Kergon 	int r;
30961da177e4SLinus Torvalds 
30971da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
3098dfbe03f6SAlasdair G Kergon 
3099db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
3100dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
3101cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
3102e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
3103e39e2e95SAlasdair G Kergon 		return r;
3104dfbe03f6SAlasdair G Kergon 	}
3105dfbe03f6SAlasdair G Kergon 
3106aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
3107aa8d7c2fSAlasdair G Kergon 
31081da177e4SLinus Torvalds 	return 0;
31091da177e4SLinus Torvalds }
31101da177e4SLinus Torvalds 
31112ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
31121da177e4SLinus Torvalds {
3113aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
3114aa8d7c2fSAlasdair G Kergon 		return;
3115aa8d7c2fSAlasdair G Kergon 
3116db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
31171da177e4SLinus Torvalds 	md->frozen_sb = NULL;
3118aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
31191da177e4SLinus Torvalds }
31201da177e4SLinus Torvalds 
31211da177e4SLinus Torvalds /*
3122ffcc3936SMike Snitzer  * If __dm_suspend returns 0, the device is completely quiescent
3123ffcc3936SMike Snitzer  * now. There is no request-processing activity. All new requests
3124ffcc3936SMike Snitzer  * are being added to md->deferred list.
3125cec47e3dSKiyoshi Ueda  *
3126ffcc3936SMike Snitzer  * Caller must hold md->suspend_lock
3127cec47e3dSKiyoshi Ueda  */
3128ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
3129ffcc3936SMike Snitzer 			unsigned suspend_flags, int interruptible)
31301da177e4SLinus Torvalds {
3131ffcc3936SMike Snitzer 	bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
3132ffcc3936SMike Snitzer 	bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
3133ffcc3936SMike Snitzer 	int r;
3134cf222b37SAlasdair G Kergon 
31352e93ccc1SKiyoshi Ueda 	/*
31362e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
31372e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
31382e93ccc1SKiyoshi Ueda 	 */
31392e93ccc1SKiyoshi Ueda 	if (noflush)
31402e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
31412e93ccc1SKiyoshi Ueda 
3142d67ee213SMike Snitzer 	/*
3143d67ee213SMike Snitzer 	 * This gets reverted if there's an error later and the targets
3144d67ee213SMike Snitzer 	 * provide the .presuspend_undo hook.
3145d67ee213SMike Snitzer 	 */
31461da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
31471da177e4SLinus Torvalds 
31482e93ccc1SKiyoshi Ueda 	/*
31499f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
31509f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
31519f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
31529f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
31532e93ccc1SKiyoshi Ueda 	 */
315432a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
31552ca3310eSAlasdair G Kergon 		r = lock_fs(md);
3156d67ee213SMike Snitzer 		if (r) {
3157d67ee213SMike Snitzer 			dm_table_presuspend_undo_targets(map);
3158ffcc3936SMike Snitzer 			return r;
3159aa8d7c2fSAlasdair G Kergon 		}
3160d67ee213SMike Snitzer 	}
31611da177e4SLinus Torvalds 
31621da177e4SLinus Torvalds 	/*
31633b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
31643b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
31653b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
31663b00b203SMikulas Patocka 	 * dm_wq_work.
31673b00b203SMikulas Patocka 	 *
31683b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
31693b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
31706a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
31716a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
31726a8736d1STejun Heo 	 * flush_workqueue(md->wq).
31731da177e4SLinus Torvalds 	 */
31741eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
317541abc4e1SHannes Reinecke 	if (map)
317683d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
31771da177e4SLinus Torvalds 
3178d0bcb878SKiyoshi Ueda 	/*
317929e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
318029e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
3181d0bcb878SKiyoshi Ueda 	 */
31822eb6e1e3SKeith Busch 	if (dm_request_based(md)) {
3183eca7ee6dSMike Snitzer 		dm_stop_queue(md->queue);
318402233342SMike Snitzer 		if (md->kworker_task)
31852eb6e1e3SKeith Busch 			flush_kthread_worker(&md->kworker);
31862eb6e1e3SKeith Busch 	}
3187cec47e3dSKiyoshi Ueda 
3188d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
3189d0bcb878SKiyoshi Ueda 
31901da177e4SLinus Torvalds 	/*
31913b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
31923b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
31933b00b203SMikulas Patocka 	 * to finish.
31941da177e4SLinus Torvalds 	 */
3195ffcc3936SMike Snitzer 	r = dm_wait_for_completion(md, interruptible);
31961da177e4SLinus Torvalds 
31976d6f10dfSMilan Broz 	if (noflush)
3198022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
319941abc4e1SHannes Reinecke 	if (map)
320083d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
32012e93ccc1SKiyoshi Ueda 
32021da177e4SLinus Torvalds 	/* were we interrupted ? */
320346125c1cSMilan Broz 	if (r < 0) {
32049a1fb464SMikulas Patocka 		dm_queue_flush(md);
320573d410c0SMilan Broz 
3206cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
3207eca7ee6dSMike Snitzer 			dm_start_queue(md->queue);
3208cec47e3dSKiyoshi Ueda 
32092ca3310eSAlasdair G Kergon 		unlock_fs(md);
3210d67ee213SMike Snitzer 		dm_table_presuspend_undo_targets(map);
3211ffcc3936SMike Snitzer 		/* pushback list is already flushed, so skip flush */
3212ffcc3936SMike Snitzer 	}
3213ffcc3936SMike Snitzer 
3214ffcc3936SMike Snitzer 	return r;
32152ca3310eSAlasdair G Kergon }
32162ca3310eSAlasdair G Kergon 
32173b00b203SMikulas Patocka /*
3218ffcc3936SMike Snitzer  * We need to be able to change a mapping table under a mounted
3219ffcc3936SMike Snitzer  * filesystem.  For example we might want to move some data in
3220ffcc3936SMike Snitzer  * the background.  Before the table can be swapped with
3221ffcc3936SMike Snitzer  * dm_bind_table, dm_suspend must be called to flush any in
3222ffcc3936SMike Snitzer  * flight bios and ensure that any further io gets deferred.
32233b00b203SMikulas Patocka  */
3224ffcc3936SMike Snitzer /*
3225ffcc3936SMike Snitzer  * Suspend mechanism in request-based dm.
3226ffcc3936SMike Snitzer  *
3227ffcc3936SMike Snitzer  * 1. Flush all I/Os by lock_fs() if needed.
3228ffcc3936SMike Snitzer  * 2. Stop dispatching any I/O by stopping the request_queue.
3229ffcc3936SMike Snitzer  * 3. Wait for all in-flight I/Os to be completed or requeued.
3230ffcc3936SMike Snitzer  *
3231ffcc3936SMike Snitzer  * To abort suspend, start the request_queue.
3232ffcc3936SMike Snitzer  */
3233ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
3234ffcc3936SMike Snitzer {
3235ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
3236ffcc3936SMike Snitzer 	int r = 0;
3237ffcc3936SMike Snitzer 
3238ffcc3936SMike Snitzer retry:
3239ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3240ffcc3936SMike Snitzer 
3241ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
3242ffcc3936SMike Snitzer 		r = -EINVAL;
3243ffcc3936SMike Snitzer 		goto out_unlock;
3244ffcc3936SMike Snitzer 	}
3245ffcc3936SMike Snitzer 
3246ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
3247ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
3248ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
3249ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3250ffcc3936SMike Snitzer 		if (r)
3251ffcc3936SMike Snitzer 			return r;
3252ffcc3936SMike Snitzer 		goto retry;
3253ffcc3936SMike Snitzer 	}
3254ffcc3936SMike Snitzer 
3255a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
3256ffcc3936SMike Snitzer 
3257ffcc3936SMike Snitzer 	r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE);
3258ffcc3936SMike Snitzer 	if (r)
3259ffcc3936SMike Snitzer 		goto out_unlock;
32603b00b203SMikulas Patocka 
32611da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
32621da177e4SLinus Torvalds 
32634d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
32644d4471cbSKiyoshi Ueda 
3265d287483dSAlasdair G Kergon out_unlock:
3266e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
3267cf222b37SAlasdair G Kergon 	return r;
32681da177e4SLinus Torvalds }
32691da177e4SLinus Torvalds 
3270ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map)
32711da177e4SLinus Torvalds {
3272ffcc3936SMike Snitzer 	if (map) {
3273ffcc3936SMike Snitzer 		int r = dm_table_resume_targets(map);
32748757b776SMilan Broz 		if (r)
3275ffcc3936SMike Snitzer 			return r;
3276ffcc3936SMike Snitzer 	}
32772ca3310eSAlasdair G Kergon 
32789a1fb464SMikulas Patocka 	dm_queue_flush(md);
32792ca3310eSAlasdair G Kergon 
3280cec47e3dSKiyoshi Ueda 	/*
3281cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
3282cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
3283cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
3284cec47e3dSKiyoshi Ueda 	 */
3285cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
3286eca7ee6dSMike Snitzer 		dm_start_queue(md->queue);
3287cec47e3dSKiyoshi Ueda 
32882ca3310eSAlasdair G Kergon 	unlock_fs(md);
32892ca3310eSAlasdair G Kergon 
3290ffcc3936SMike Snitzer 	return 0;
3291ffcc3936SMike Snitzer }
3292ffcc3936SMike Snitzer 
3293ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md)
3294ffcc3936SMike Snitzer {
3295ffcc3936SMike Snitzer 	int r = -EINVAL;
3296ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
3297ffcc3936SMike Snitzer 
3298ffcc3936SMike Snitzer retry:
3299ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3300ffcc3936SMike Snitzer 
3301ffcc3936SMike Snitzer 	if (!dm_suspended_md(md))
3302ffcc3936SMike Snitzer 		goto out;
3303ffcc3936SMike Snitzer 
3304ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
3305ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
3306ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
3307ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3308ffcc3936SMike Snitzer 		if (r)
3309ffcc3936SMike Snitzer 			return r;
3310ffcc3936SMike Snitzer 		goto retry;
3311ffcc3936SMike Snitzer 	}
3312ffcc3936SMike Snitzer 
3313a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
3314ffcc3936SMike Snitzer 	if (!map || !dm_table_get_size(map))
3315ffcc3936SMike Snitzer 		goto out;
3316ffcc3936SMike Snitzer 
3317ffcc3936SMike Snitzer 	r = __dm_resume(md, map);
3318ffcc3936SMike Snitzer 	if (r)
3319ffcc3936SMike Snitzer 		goto out;
3320ffcc3936SMike Snitzer 
33212ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
33222ca3310eSAlasdair G Kergon 
3323cf222b37SAlasdair G Kergon 	r = 0;
3324cf222b37SAlasdair G Kergon out:
3325e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
33262ca3310eSAlasdair G Kergon 
3327cf222b37SAlasdair G Kergon 	return r;
33281da177e4SLinus Torvalds }
33291da177e4SLinus Torvalds 
3330fd2ed4d2SMikulas Patocka /*
3331fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
3332fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
3333fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
3334fd2ed4d2SMikulas Patocka  */
3335fd2ed4d2SMikulas Patocka 
3336ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
3337ffcc3936SMike Snitzer {
3338ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
3339ffcc3936SMike Snitzer 
334096b26c8cSMikulas Patocka 	if (md->internal_suspend_count++)
3341ffcc3936SMike Snitzer 		return; /* nested internal suspend */
3342ffcc3936SMike Snitzer 
3343ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
3344ffcc3936SMike Snitzer 		set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3345ffcc3936SMike Snitzer 		return; /* nest suspend */
3346ffcc3936SMike Snitzer 	}
3347ffcc3936SMike Snitzer 
3348a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
3349ffcc3936SMike Snitzer 
3350ffcc3936SMike Snitzer 	/*
3351ffcc3936SMike Snitzer 	 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
3352ffcc3936SMike Snitzer 	 * supported.  Properly supporting a TASK_INTERRUPTIBLE internal suspend
3353ffcc3936SMike Snitzer 	 * would require changing .presuspend to return an error -- avoid this
3354ffcc3936SMike Snitzer 	 * until there is a need for more elaborate variants of internal suspend.
3355ffcc3936SMike Snitzer 	 */
3356ffcc3936SMike Snitzer 	(void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE);
3357ffcc3936SMike Snitzer 
3358ffcc3936SMike Snitzer 	set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3359ffcc3936SMike Snitzer 
3360ffcc3936SMike Snitzer 	dm_table_postsuspend_targets(map);
3361ffcc3936SMike Snitzer }
3362ffcc3936SMike Snitzer 
3363ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md)
3364ffcc3936SMike Snitzer {
336596b26c8cSMikulas Patocka 	BUG_ON(!md->internal_suspend_count);
336696b26c8cSMikulas Patocka 
336796b26c8cSMikulas Patocka 	if (--md->internal_suspend_count)
3368ffcc3936SMike Snitzer 		return; /* resume from nested internal suspend */
3369ffcc3936SMike Snitzer 
3370ffcc3936SMike Snitzer 	if (dm_suspended_md(md))
3371ffcc3936SMike Snitzer 		goto done; /* resume from nested suspend */
3372ffcc3936SMike Snitzer 
3373ffcc3936SMike Snitzer 	/*
3374ffcc3936SMike Snitzer 	 * NOTE: existing callers don't need to call dm_table_resume_targets
3375ffcc3936SMike Snitzer 	 * (which may fail -- so best to avoid it for now by passing NULL map)
3376ffcc3936SMike Snitzer 	 */
3377ffcc3936SMike Snitzer 	(void) __dm_resume(md, NULL);
3378ffcc3936SMike Snitzer 
3379ffcc3936SMike Snitzer done:
3380ffcc3936SMike Snitzer 	clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3381ffcc3936SMike Snitzer 	smp_mb__after_atomic();
3382ffcc3936SMike Snitzer 	wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
3383ffcc3936SMike Snitzer }
3384ffcc3936SMike Snitzer 
3385ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md)
3386fd2ed4d2SMikulas Patocka {
3387fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
3388ffcc3936SMike Snitzer 	__dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
3389ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
3390ffcc3936SMike Snitzer }
3391ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
3392ffcc3936SMike Snitzer 
3393ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md)
3394ffcc3936SMike Snitzer {
3395ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
3396ffcc3936SMike Snitzer 	__dm_internal_resume(md);
3397ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
3398ffcc3936SMike Snitzer }
3399ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume);
3400ffcc3936SMike Snitzer 
3401ffcc3936SMike Snitzer /*
3402ffcc3936SMike Snitzer  * Fast variants of internal suspend/resume hold md->suspend_lock,
3403ffcc3936SMike Snitzer  * which prevents interaction with userspace-driven suspend.
3404ffcc3936SMike Snitzer  */
3405ffcc3936SMike Snitzer 
3406ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md)
3407ffcc3936SMike Snitzer {
3408ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
3409ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
3410fd2ed4d2SMikulas Patocka 		return;
3411fd2ed4d2SMikulas Patocka 
3412fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
3413fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
3414fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
3415fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
3416fd2ed4d2SMikulas Patocka }
3417b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
3418fd2ed4d2SMikulas Patocka 
3419ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md)
3420fd2ed4d2SMikulas Patocka {
3421ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
3422fd2ed4d2SMikulas Patocka 		goto done;
3423fd2ed4d2SMikulas Patocka 
3424fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
3425fd2ed4d2SMikulas Patocka 
3426fd2ed4d2SMikulas Patocka done:
3427fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
3428fd2ed4d2SMikulas Patocka }
3429b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
3430fd2ed4d2SMikulas Patocka 
34311da177e4SLinus Torvalds /*-----------------------------------------------------------------
34321da177e4SLinus Torvalds  * Event notification.
34331da177e4SLinus Torvalds  *---------------------------------------------------------------*/
34343abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
343560935eb2SMilan Broz 		       unsigned cookie)
343669267a30SAlasdair G Kergon {
343760935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
343860935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
343960935eb2SMilan Broz 
344060935eb2SMilan Broz 	if (!cookie)
34413abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
344260935eb2SMilan Broz 	else {
344360935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
344460935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
34453abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
34463abf85b5SPeter Rajnoha 					  action, envp);
344760935eb2SMilan Broz 	}
344869267a30SAlasdair G Kergon }
344969267a30SAlasdair G Kergon 
34507a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
34517a8c3d3bSMike Anderson {
34527a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
34537a8c3d3bSMike Anderson }
34547a8c3d3bSMike Anderson 
34551da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
34561da177e4SLinus Torvalds {
34571da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
34581da177e4SLinus Torvalds }
34591da177e4SLinus Torvalds 
34601da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
34611da177e4SLinus Torvalds {
34621da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
34631da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
34641da177e4SLinus Torvalds }
34651da177e4SLinus Torvalds 
34667a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
34677a8c3d3bSMike Anderson {
34687a8c3d3bSMike Anderson 	unsigned long flags;
34697a8c3d3bSMike Anderson 
34707a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
34717a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
34727a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
34737a8c3d3bSMike Anderson }
34747a8c3d3bSMike Anderson 
34751da177e4SLinus Torvalds /*
34761da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
34771da177e4SLinus Torvalds  * count on 'md'.
34781da177e4SLinus Torvalds  */
34791da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
34801da177e4SLinus Torvalds {
34811da177e4SLinus Torvalds 	return md->disk;
34821da177e4SLinus Torvalds }
348365ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk);
34841da177e4SLinus Torvalds 
3485784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
3486784aae73SMilan Broz {
34872995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
3488784aae73SMilan Broz }
3489784aae73SMilan Broz 
3490784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
3491784aae73SMilan Broz {
3492784aae73SMilan Broz 	struct mapped_device *md;
3493784aae73SMilan Broz 
34942995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
3495784aae73SMilan Broz 
34964d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
3497432a212cSMike Anderson 	    dm_deleting_md(md))
34984d89b7b4SMilan Broz 		return NULL;
34994d89b7b4SMilan Broz 
3500784aae73SMilan Broz 	dm_get(md);
3501784aae73SMilan Broz 	return md;
3502784aae73SMilan Broz }
3503784aae73SMilan Broz 
35044f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
35051da177e4SLinus Torvalds {
35061da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
35071da177e4SLinus Torvalds }
35081da177e4SLinus Torvalds 
3509ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md)
3510ffcc3936SMike Snitzer {
3511ffcc3936SMike Snitzer 	return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3512ffcc3936SMike Snitzer }
3513ffcc3936SMike Snitzer 
35142c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
35152c140a24SMikulas Patocka {
35162c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
35172c140a24SMikulas Patocka }
35182c140a24SMikulas Patocka 
351964dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
352064dbce58SKiyoshi Ueda {
3521ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
352264dbce58SKiyoshi Ueda }
352364dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
352464dbce58SKiyoshi Ueda 
35252e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
35262e93ccc1SKiyoshi Ueda {
3527ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
35282e93ccc1SKiyoshi Ueda }
35292e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
35302e93ccc1SKiyoshi Ueda 
353178d8e58aSMike Snitzer struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type,
353230187e1dSMike Snitzer 					    unsigned integrity, unsigned per_io_data_size)
3533e6ee8c0bSKiyoshi Ueda {
3534*115485e8SMike Snitzer 	struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
353578d8e58aSMike Snitzer 	struct kmem_cache *cachep = NULL;
353678d8e58aSMike Snitzer 	unsigned int pool_size = 0;
35375f015204SJun'ichi Nomura 	unsigned int front_pad;
3538e6ee8c0bSKiyoshi Ueda 
3539e6ee8c0bSKiyoshi Ueda 	if (!pools)
35404e6e36c3SMike Snitzer 		return NULL;
3541e6ee8c0bSKiyoshi Ueda 
354278d8e58aSMike Snitzer 	type = filter_md_type(type, md);
354317e149b8SMike Snitzer 
354478d8e58aSMike Snitzer 	switch (type) {
354578d8e58aSMike Snitzer 	case DM_TYPE_BIO_BASED:
354678d8e58aSMike Snitzer 		cachep = _io_cache;
354778d8e58aSMike Snitzer 		pool_size = dm_get_reserved_bio_based_ios();
354830187e1dSMike Snitzer 		front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
354978d8e58aSMike Snitzer 		break;
355078d8e58aSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
355178d8e58aSMike Snitzer 		cachep = _rq_tio_cache;
355278d8e58aSMike Snitzer 		pool_size = dm_get_reserved_rq_based_ios();
355378d8e58aSMike Snitzer 		pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache);
355478d8e58aSMike Snitzer 		if (!pools->rq_pool)
355578d8e58aSMike Snitzer 			goto out;
355678d8e58aSMike Snitzer 		/* fall through to setup remaining rq-based pools */
355778d8e58aSMike Snitzer 	case DM_TYPE_MQ_REQUEST_BASED:
355878d8e58aSMike Snitzer 		if (!pool_size)
355978d8e58aSMike Snitzer 			pool_size = dm_get_reserved_rq_based_ios();
356078d8e58aSMike Snitzer 		front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
3561591ddcfcSMike Snitzer 		/* per_io_data_size is used for blk-mq pdu at queue allocation */
356278d8e58aSMike Snitzer 		break;
356378d8e58aSMike Snitzer 	default:
356478d8e58aSMike Snitzer 		BUG();
356578d8e58aSMike Snitzer 	}
356678d8e58aSMike Snitzer 
356778d8e58aSMike Snitzer 	if (cachep) {
356878d8e58aSMike Snitzer 		pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
3569e6ee8c0bSKiyoshi Ueda 		if (!pools->io_pool)
35705f015204SJun'ichi Nomura 			goto out;
357178d8e58aSMike Snitzer 	}
3572e6ee8c0bSKiyoshi Ueda 
35733d8aab2dSJunichi Nomura 	pools->bs = bioset_create_nobvec(pool_size, front_pad);
3574e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
35755f015204SJun'ichi Nomura 		goto out;
3576e6ee8c0bSKiyoshi Ueda 
3577a91a2785SMartin K. Petersen 	if (integrity && bioset_integrity_create(pools->bs, pool_size))
35785f015204SJun'ichi Nomura 		goto out;
3579a91a2785SMartin K. Petersen 
3580e6ee8c0bSKiyoshi Ueda 	return pools;
358178d8e58aSMike Snitzer 
35825f015204SJun'ichi Nomura out:
35835f015204SJun'ichi Nomura 	dm_free_md_mempools(pools);
3584e6ee8c0bSKiyoshi Ueda 
35854e6e36c3SMike Snitzer 	return NULL;
3586e6ee8c0bSKiyoshi Ueda }
3587e6ee8c0bSKiyoshi Ueda 
3588e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
3589e6ee8c0bSKiyoshi Ueda {
3590e6ee8c0bSKiyoshi Ueda 	if (!pools)
3591e6ee8c0bSKiyoshi Ueda 		return;
3592e6ee8c0bSKiyoshi Ueda 
3593e6ee8c0bSKiyoshi Ueda 	mempool_destroy(pools->io_pool);
35941ae49ea2SMike Snitzer 	mempool_destroy(pools->rq_pool);
35951ae49ea2SMike Snitzer 
3596e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
3597e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
3598e6ee8c0bSKiyoshi Ueda 
3599e6ee8c0bSKiyoshi Ueda 	kfree(pools);
3600e6ee8c0bSKiyoshi Ueda }
3601e6ee8c0bSKiyoshi Ueda 
360271cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
360371cdb697SChristoph Hellwig 			  u32 flags)
360471cdb697SChristoph Hellwig {
360571cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
360671cdb697SChristoph Hellwig 	const struct pr_ops *ops;
360771cdb697SChristoph Hellwig 	fmode_t mode;
3608956a4025SMike Snitzer 	int r;
360971cdb697SChristoph Hellwig 
3610956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
361171cdb697SChristoph Hellwig 	if (r < 0)
361271cdb697SChristoph Hellwig 		return r;
361371cdb697SChristoph Hellwig 
361471cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
361571cdb697SChristoph Hellwig 	if (ops && ops->pr_register)
361671cdb697SChristoph Hellwig 		r = ops->pr_register(bdev, old_key, new_key, flags);
361771cdb697SChristoph Hellwig 	else
361871cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
361971cdb697SChristoph Hellwig 
3620956a4025SMike Snitzer 	bdput(bdev);
362171cdb697SChristoph Hellwig 	return r;
362271cdb697SChristoph Hellwig }
362371cdb697SChristoph Hellwig 
362471cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
362571cdb697SChristoph Hellwig 			 u32 flags)
362671cdb697SChristoph Hellwig {
362771cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
362871cdb697SChristoph Hellwig 	const struct pr_ops *ops;
362971cdb697SChristoph Hellwig 	fmode_t mode;
3630956a4025SMike Snitzer 	int r;
363171cdb697SChristoph Hellwig 
3632956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
363371cdb697SChristoph Hellwig 	if (r < 0)
363471cdb697SChristoph Hellwig 		return r;
363571cdb697SChristoph Hellwig 
363671cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
363771cdb697SChristoph Hellwig 	if (ops && ops->pr_reserve)
363871cdb697SChristoph Hellwig 		r = ops->pr_reserve(bdev, key, type, flags);
363971cdb697SChristoph Hellwig 	else
364071cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
364171cdb697SChristoph Hellwig 
3642956a4025SMike Snitzer 	bdput(bdev);
364371cdb697SChristoph Hellwig 	return r;
364471cdb697SChristoph Hellwig }
364571cdb697SChristoph Hellwig 
364671cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
364771cdb697SChristoph Hellwig {
364871cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
364971cdb697SChristoph Hellwig 	const struct pr_ops *ops;
365071cdb697SChristoph Hellwig 	fmode_t mode;
3651956a4025SMike Snitzer 	int r;
365271cdb697SChristoph Hellwig 
3653956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
365471cdb697SChristoph Hellwig 	if (r < 0)
365571cdb697SChristoph Hellwig 		return r;
365671cdb697SChristoph Hellwig 
365771cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
365871cdb697SChristoph Hellwig 	if (ops && ops->pr_release)
365971cdb697SChristoph Hellwig 		r = ops->pr_release(bdev, key, type);
366071cdb697SChristoph Hellwig 	else
366171cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
366271cdb697SChristoph Hellwig 
3663956a4025SMike Snitzer 	bdput(bdev);
366471cdb697SChristoph Hellwig 	return r;
366571cdb697SChristoph Hellwig }
366671cdb697SChristoph Hellwig 
366771cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
366871cdb697SChristoph Hellwig 			 enum pr_type type, bool abort)
366971cdb697SChristoph Hellwig {
367071cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
367171cdb697SChristoph Hellwig 	const struct pr_ops *ops;
367271cdb697SChristoph Hellwig 	fmode_t mode;
3673956a4025SMike Snitzer 	int r;
367471cdb697SChristoph Hellwig 
3675956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
367671cdb697SChristoph Hellwig 	if (r < 0)
367771cdb697SChristoph Hellwig 		return r;
367871cdb697SChristoph Hellwig 
367971cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
368071cdb697SChristoph Hellwig 	if (ops && ops->pr_preempt)
368171cdb697SChristoph Hellwig 		r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
368271cdb697SChristoph Hellwig 	else
368371cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
368471cdb697SChristoph Hellwig 
3685956a4025SMike Snitzer 	bdput(bdev);
368671cdb697SChristoph Hellwig 	return r;
368771cdb697SChristoph Hellwig }
368871cdb697SChristoph Hellwig 
368971cdb697SChristoph Hellwig static int dm_pr_clear(struct block_device *bdev, u64 key)
369071cdb697SChristoph Hellwig {
369171cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
369271cdb697SChristoph Hellwig 	const struct pr_ops *ops;
369371cdb697SChristoph Hellwig 	fmode_t mode;
3694956a4025SMike Snitzer 	int r;
369571cdb697SChristoph Hellwig 
3696956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
369771cdb697SChristoph Hellwig 	if (r < 0)
369871cdb697SChristoph Hellwig 		return r;
369971cdb697SChristoph Hellwig 
370071cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
370171cdb697SChristoph Hellwig 	if (ops && ops->pr_clear)
370271cdb697SChristoph Hellwig 		r = ops->pr_clear(bdev, key);
370371cdb697SChristoph Hellwig 	else
370471cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
370571cdb697SChristoph Hellwig 
3706956a4025SMike Snitzer 	bdput(bdev);
370771cdb697SChristoph Hellwig 	return r;
370871cdb697SChristoph Hellwig }
370971cdb697SChristoph Hellwig 
371071cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = {
371171cdb697SChristoph Hellwig 	.pr_register	= dm_pr_register,
371271cdb697SChristoph Hellwig 	.pr_reserve	= dm_pr_reserve,
371371cdb697SChristoph Hellwig 	.pr_release	= dm_pr_release,
371471cdb697SChristoph Hellwig 	.pr_preempt	= dm_pr_preempt,
371571cdb697SChristoph Hellwig 	.pr_clear	= dm_pr_clear,
371671cdb697SChristoph Hellwig };
371771cdb697SChristoph Hellwig 
371883d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
37191da177e4SLinus Torvalds 	.open = dm_blk_open,
37201da177e4SLinus Torvalds 	.release = dm_blk_close,
3721aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
37223ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
372371cdb697SChristoph Hellwig 	.pr_ops = &dm_pr_ops,
37241da177e4SLinus Torvalds 	.owner = THIS_MODULE
37251da177e4SLinus Torvalds };
37261da177e4SLinus Torvalds 
37271da177e4SLinus Torvalds /*
37281da177e4SLinus Torvalds  * module hooks
37291da177e4SLinus Torvalds  */
37301da177e4SLinus Torvalds module_init(dm_init);
37311da177e4SLinus Torvalds module_exit(dm_exit);
37321da177e4SLinus Torvalds 
37331da177e4SLinus Torvalds module_param(major, uint, 0);
37341da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
3735f4790826SMike Snitzer 
3736e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3737e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3738e8603136SMike Snitzer 
3739f4790826SMike Snitzer module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
3740f4790826SMike Snitzer MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
3741f4790826SMike Snitzer 
374217e149b8SMike Snitzer module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
374317e149b8SMike Snitzer MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
374417e149b8SMike Snitzer 
3745faad87dfSMike Snitzer module_param(dm_mq_nr_hw_queues, uint, S_IRUGO | S_IWUSR);
3746faad87dfSMike Snitzer MODULE_PARM_DESC(dm_mq_nr_hw_queues, "Number of hardware queues for request-based dm-mq devices");
3747faad87dfSMike Snitzer 
3748faad87dfSMike Snitzer module_param(dm_mq_queue_depth, uint, S_IRUGO | S_IWUSR);
3749faad87dfSMike Snitzer MODULE_PARM_DESC(dm_mq_queue_depth, "Queue depth for request-based dm-mq devices");
3750faad87dfSMike Snitzer 
3751*115485e8SMike Snitzer module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
3752*115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
3753*115485e8SMike Snitzer 
37541da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
37551da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
37561da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3757