xref: /openbmc/linux/drivers/md/dm.c (revision 0f20972f7bf6922df49ef7ce7a6df802347d2c52)
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>
2755782138SLi Zefan 
2855782138SLi Zefan #include <trace/events/block.h>
291da177e4SLinus Torvalds 
3072d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
3172d94861SAlasdair G Kergon 
3271a16736SNamhyung Kim #ifdef CONFIG_PRINTK
3371a16736SNamhyung Kim /*
3471a16736SNamhyung Kim  * ratelimit state to be used in DMXXX_LIMIT().
3571a16736SNamhyung Kim  */
3671a16736SNamhyung Kim DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
3771a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_INTERVAL,
3871a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_BURST);
3971a16736SNamhyung Kim EXPORT_SYMBOL(dm_ratelimit_state);
4071a16736SNamhyung Kim #endif
4171a16736SNamhyung Kim 
4260935eb2SMilan Broz /*
4360935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
4460935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
4560935eb2SMilan Broz  */
4660935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
4760935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
4860935eb2SMilan Broz 
491da177e4SLinus Torvalds static const char *_name = DM_NAME;
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds static unsigned int major = 0;
521da177e4SLinus Torvalds static unsigned int _major = 0;
531da177e4SLinus Torvalds 
54d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr);
55d15b774cSAlasdair G Kergon 
56f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
572c140a24SMikulas Patocka 
582c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w);
592c140a24SMikulas Patocka 
602c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
612c140a24SMikulas Patocka 
62acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue;
63acfe0ad7SMikulas Patocka 
641da177e4SLinus Torvalds /*
658fbf26adSKiyoshi Ueda  * For bio-based dm.
661da177e4SLinus Torvalds  * One of these is allocated per bio.
671da177e4SLinus Torvalds  */
681da177e4SLinus Torvalds struct dm_io {
691da177e4SLinus Torvalds 	struct mapped_device *md;
701da177e4SLinus Torvalds 	int error;
711da177e4SLinus Torvalds 	atomic_t io_count;
726ae2fa67SRichard Kennedy 	struct bio *bio;
733eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
74f88fb981SKiyoshi Ueda 	spinlock_t endio_lock;
75fd2ed4d2SMikulas Patocka 	struct dm_stats_aux stats_aux;
761da177e4SLinus Torvalds };
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds /*
798fbf26adSKiyoshi Ueda  * For request-based dm.
808fbf26adSKiyoshi Ueda  * One of these is allocated per request.
818fbf26adSKiyoshi Ueda  */
828fbf26adSKiyoshi Ueda struct dm_rq_target_io {
838fbf26adSKiyoshi Ueda 	struct mapped_device *md;
848fbf26adSKiyoshi Ueda 	struct dm_target *ti;
851ae49ea2SMike Snitzer 	struct request *orig, *clone;
862eb6e1e3SKeith Busch 	struct kthread_work work;
878fbf26adSKiyoshi Ueda 	int error;
888fbf26adSKiyoshi Ueda 	union map_info info;
898fbf26adSKiyoshi Ueda };
908fbf26adSKiyoshi Ueda 
918fbf26adSKiyoshi Ueda /*
9294818742SKent Overstreet  * For request-based dm - the bio clones we allocate are embedded in these
9394818742SKent Overstreet  * structs.
9494818742SKent Overstreet  *
9594818742SKent Overstreet  * We allocate these with bio_alloc_bioset, using the front_pad parameter when
9694818742SKent Overstreet  * the bioset is created - this means the bio has to come at the end of the
9794818742SKent Overstreet  * struct.
988fbf26adSKiyoshi Ueda  */
998fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info {
1008fbf26adSKiyoshi Ueda 	struct bio *orig;
101cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio;
10294818742SKent Overstreet 	struct bio clone;
1038fbf26adSKiyoshi Ueda };
1048fbf26adSKiyoshi Ueda 
105cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq)
106cec47e3dSKiyoshi Ueda {
107cec47e3dSKiyoshi Ueda 	if (rq && rq->end_io_data)
108cec47e3dSKiyoshi Ueda 		return &((struct dm_rq_target_io *)rq->end_io_data)->info;
109cec47e3dSKiyoshi Ueda 	return NULL;
110cec47e3dSKiyoshi Ueda }
111cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
112cec47e3dSKiyoshi Ueda 
113ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
114ba61fdd1SJeff Mahoney 
1151da177e4SLinus Torvalds /*
1161da177e4SLinus Torvalds  * Bits for the md->flags field.
1171da177e4SLinus Torvalds  */
1181eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
1191da177e4SLinus Torvalds #define DMF_SUSPENDED 1
120aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
121fba9f90eSJeff Mahoney #define DMF_FREEING 3
1225c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
1232e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
124d5b9dd04SMikulas Patocka #define DMF_MERGE_IS_OPTIONAL 6
1252c140a24SMikulas Patocka #define DMF_DEFERRED_REMOVE 7
126ffcc3936SMike Snitzer #define DMF_SUSPENDED_INTERNALLY 8
1271da177e4SLinus Torvalds 
128304f3f6aSMilan Broz /*
12983d5e5b0SMikulas Patocka  * A dummy definition to make RCU happy.
13083d5e5b0SMikulas Patocka  * struct dm_table should never be dereferenced in this file.
13183d5e5b0SMikulas Patocka  */
13283d5e5b0SMikulas Patocka struct dm_table {
13383d5e5b0SMikulas Patocka 	int undefined__;
13483d5e5b0SMikulas Patocka };
13583d5e5b0SMikulas Patocka 
13683d5e5b0SMikulas Patocka /*
137304f3f6aSMilan Broz  * Work processed by per-device workqueue.
138304f3f6aSMilan Broz  */
1391da177e4SLinus Torvalds struct mapped_device {
14083d5e5b0SMikulas Patocka 	struct srcu_struct io_barrier;
141e61290a4SDaniel Walker 	struct mutex suspend_lock;
1421da177e4SLinus Torvalds 	atomic_t holders;
1435c6bd75dSAlasdair G Kergon 	atomic_t open_count;
1441da177e4SLinus Torvalds 
1452a7faeb1SMikulas Patocka 	/*
1462a7faeb1SMikulas Patocka 	 * The current mapping.
1472a7faeb1SMikulas Patocka 	 * Use dm_get_live_table{_fast} or take suspend_lock for
1482a7faeb1SMikulas Patocka 	 * dereference.
1492a7faeb1SMikulas Patocka 	 */
1506fa99520SPranith Kumar 	struct dm_table __rcu *map;
1512a7faeb1SMikulas Patocka 
15286f1152bSBenjamin Marzinski 	struct list_head table_devices;
15386f1152bSBenjamin Marzinski 	struct mutex table_devices_lock;
15486f1152bSBenjamin Marzinski 
1551da177e4SLinus Torvalds 	unsigned long flags;
1561da177e4SLinus Torvalds 
157165125e1SJens Axboe 	struct request_queue *queue;
158a5664dadSMike Snitzer 	unsigned type;
1594a0b4ddfSMike Snitzer 	/* Protect queue and type against concurrent access. */
160a5664dadSMike Snitzer 	struct mutex type_lock;
161a5664dadSMike Snitzer 
16236a0456fSAlasdair G Kergon 	struct target_type *immutable_target_type;
16336a0456fSAlasdair G Kergon 
1641da177e4SLinus Torvalds 	struct gendisk *disk;
1657e51f257SMike Anderson 	char name[16];
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	void *interface_ptr;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	/*
1701da177e4SLinus Torvalds 	 * A list of ios that arrived while we were suspended.
1711da177e4SLinus Torvalds 	 */
172316d315bSNikanth Karthikesan 	atomic_t pending[2];
1731da177e4SLinus Torvalds 	wait_queue_head_t wait;
17453d5914fSMikulas Patocka 	struct work_struct work;
1751da177e4SLinus Torvalds 	struct bio_list deferred;
176022c2611SMikulas Patocka 	spinlock_t deferred_lock;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	/*
17929e4013dSTejun Heo 	 * Processing queue (flush)
180304f3f6aSMilan Broz 	 */
181304f3f6aSMilan Broz 	struct workqueue_struct *wq;
182304f3f6aSMilan Broz 
183304f3f6aSMilan Broz 	/*
1841da177e4SLinus Torvalds 	 * io objects are allocated from here.
1851da177e4SLinus Torvalds 	 */
1861da177e4SLinus Torvalds 	mempool_t *io_pool;
1871ae49ea2SMike Snitzer 	mempool_t *rq_pool;
1881da177e4SLinus Torvalds 
1899faf400fSStefan Bader 	struct bio_set *bs;
1909faf400fSStefan Bader 
1911da177e4SLinus Torvalds 	/*
1921da177e4SLinus Torvalds 	 * Event handling.
1931da177e4SLinus Torvalds 	 */
1941da177e4SLinus Torvalds 	atomic_t event_nr;
1951da177e4SLinus Torvalds 	wait_queue_head_t eventq;
1967a8c3d3bSMike Anderson 	atomic_t uevent_seq;
1977a8c3d3bSMike Anderson 	struct list_head uevent_list;
1987a8c3d3bSMike Anderson 	spinlock_t uevent_lock; /* Protect access to uevent_list */
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	/*
2011da177e4SLinus Torvalds 	 * freeze/thaw support require holding onto a super block
2021da177e4SLinus Torvalds 	 */
2031da177e4SLinus Torvalds 	struct super_block *frozen_sb;
204db8fef4fSMikulas Patocka 	struct block_device *bdev;
2053ac51e74SDarrick J. Wong 
2063ac51e74SDarrick J. Wong 	/* forced geometry settings */
2073ac51e74SDarrick J. Wong 	struct hd_geometry geometry;
208784aae73SMilan Broz 
2092995fa78SMikulas Patocka 	/* kobject and completion */
2102995fa78SMikulas Patocka 	struct dm_kobject_holder kobj_holder;
211be35f486SMikulas Patocka 
212d87f4c14STejun Heo 	/* zero-length flush that will be cloned and submitted to targets */
213d87f4c14STejun Heo 	struct bio flush_bio;
214fd2ed4d2SMikulas Patocka 
21596b26c8cSMikulas Patocka 	/* the number of internal suspends */
21696b26c8cSMikulas Patocka 	unsigned internal_suspend_count;
21796b26c8cSMikulas Patocka 
218fd2ed4d2SMikulas Patocka 	struct dm_stats stats;
2192eb6e1e3SKeith Busch 
2202eb6e1e3SKeith Busch 	struct kthread_worker kworker;
2212eb6e1e3SKeith Busch 	struct task_struct *kworker_task;
222de3ec86dSMike Snitzer 
223de3ec86dSMike Snitzer 	/* for request-based merge heuristic in dm_request_fn() */
2240ce65797SMike Snitzer 	unsigned seq_rq_merge_deadline_usecs;
225de3ec86dSMike Snitzer 	int last_rq_rw;
2260ce65797SMike Snitzer 	sector_t last_rq_pos;
2270ce65797SMike Snitzer 	ktime_t last_rq_start_time;
228bfebd1cdSMike Snitzer 
229bfebd1cdSMike Snitzer 	/* for blk-mq request-based DM support */
230bfebd1cdSMike Snitzer 	struct blk_mq_tag_set tag_set;
23117e149b8SMike Snitzer 	bool use_blk_mq;
2321da177e4SLinus Torvalds };
2331da177e4SLinus Torvalds 
23417e149b8SMike Snitzer #ifdef CONFIG_DM_MQ_DEFAULT
23517e149b8SMike Snitzer static bool use_blk_mq = true;
23617e149b8SMike Snitzer #else
23717e149b8SMike Snitzer static bool use_blk_mq = false;
23817e149b8SMike Snitzer #endif
23917e149b8SMike Snitzer 
24017e149b8SMike Snitzer bool dm_use_blk_mq(struct mapped_device *md)
24117e149b8SMike Snitzer {
24217e149b8SMike Snitzer 	return md->use_blk_mq;
24317e149b8SMike Snitzer }
24417e149b8SMike Snitzer 
245e6ee8c0bSKiyoshi Ueda /*
246e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
247e6ee8c0bSKiyoshi Ueda  */
248e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
249e6ee8c0bSKiyoshi Ueda 	mempool_t *io_pool;
2501ae49ea2SMike Snitzer 	mempool_t *rq_pool;
251e6ee8c0bSKiyoshi Ueda 	struct bio_set *bs;
252e6ee8c0bSKiyoshi Ueda };
253e6ee8c0bSKiyoshi Ueda 
25486f1152bSBenjamin Marzinski struct table_device {
25586f1152bSBenjamin Marzinski 	struct list_head list;
25686f1152bSBenjamin Marzinski 	atomic_t count;
25786f1152bSBenjamin Marzinski 	struct dm_dev dm_dev;
25886f1152bSBenjamin Marzinski };
25986f1152bSBenjamin Marzinski 
2606cfa5857SMike Snitzer #define RESERVED_BIO_BASED_IOS		16
2616cfa5857SMike Snitzer #define RESERVED_REQUEST_BASED_IOS	256
262f4790826SMike Snitzer #define RESERVED_MAX_IOS		1024
263e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
2648fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
2651ae49ea2SMike Snitzer static struct kmem_cache *_rq_cache;
26694818742SKent Overstreet 
267f4790826SMike Snitzer /*
268e8603136SMike Snitzer  * Bio-based DM's mempools' reserved IOs set by the user.
269e8603136SMike Snitzer  */
270e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
271e8603136SMike Snitzer 
272e8603136SMike Snitzer /*
273f4790826SMike Snitzer  * Request-based DM's mempools' reserved IOs set by the user.
274f4790826SMike Snitzer  */
275f4790826SMike Snitzer static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
276f4790826SMike Snitzer 
27709c2d531SMike Snitzer static unsigned __dm_get_module_param(unsigned *module_param,
278f4790826SMike Snitzer 				      unsigned def, unsigned max)
279f4790826SMike Snitzer {
28009c2d531SMike Snitzer 	unsigned param = ACCESS_ONCE(*module_param);
28109c2d531SMike Snitzer 	unsigned modified_param = 0;
282f4790826SMike Snitzer 
28309c2d531SMike Snitzer 	if (!param)
28409c2d531SMike Snitzer 		modified_param = def;
28509c2d531SMike Snitzer 	else if (param > max)
28609c2d531SMike Snitzer 		modified_param = max;
287f4790826SMike Snitzer 
28809c2d531SMike Snitzer 	if (modified_param) {
28909c2d531SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
29009c2d531SMike Snitzer 		param = modified_param;
291f4790826SMike Snitzer 	}
292f4790826SMike Snitzer 
29309c2d531SMike Snitzer 	return param;
294f4790826SMike Snitzer }
295f4790826SMike Snitzer 
296e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void)
297e8603136SMike Snitzer {
29809c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_bio_based_ios,
299e8603136SMike Snitzer 				     RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
300e8603136SMike Snitzer }
301e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
302e8603136SMike Snitzer 
303f4790826SMike Snitzer unsigned dm_get_reserved_rq_based_ios(void)
304f4790826SMike Snitzer {
30509c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_rq_based_ios,
306f4790826SMike Snitzer 				     RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
307f4790826SMike Snitzer }
308f4790826SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
309f4790826SMike Snitzer 
3101da177e4SLinus Torvalds static int __init local_init(void)
3111da177e4SLinus Torvalds {
31251157b4aSKiyoshi Ueda 	int r = -ENOMEM;
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
315028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
3161da177e4SLinus Torvalds 	if (!_io_cache)
31751157b4aSKiyoshi Ueda 		return r;
3181da177e4SLinus Torvalds 
3198fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
3208fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
321dba14160SMikulas Patocka 		goto out_free_io_cache;
3228fbf26adSKiyoshi Ueda 
3231ae49ea2SMike Snitzer 	_rq_cache = kmem_cache_create("dm_clone_request", sizeof(struct request),
3241ae49ea2SMike Snitzer 				      __alignof__(struct request), 0, NULL);
3251ae49ea2SMike Snitzer 	if (!_rq_cache)
3261ae49ea2SMike Snitzer 		goto out_free_rq_tio_cache;
3271ae49ea2SMike Snitzer 
32851e5b2bdSMike Anderson 	r = dm_uevent_init();
32951157b4aSKiyoshi Ueda 	if (r)
3301ae49ea2SMike Snitzer 		goto out_free_rq_cache;
33151e5b2bdSMike Anderson 
332acfe0ad7SMikulas Patocka 	deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
333acfe0ad7SMikulas Patocka 	if (!deferred_remove_workqueue) {
334acfe0ad7SMikulas Patocka 		r = -ENOMEM;
335acfe0ad7SMikulas Patocka 		goto out_uevent_exit;
336acfe0ad7SMikulas Patocka 	}
337acfe0ad7SMikulas Patocka 
3381da177e4SLinus Torvalds 	_major = major;
3391da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
34051157b4aSKiyoshi Ueda 	if (r < 0)
341acfe0ad7SMikulas Patocka 		goto out_free_workqueue;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	if (!_major)
3441da177e4SLinus Torvalds 		_major = r;
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	return 0;
34751157b4aSKiyoshi Ueda 
348acfe0ad7SMikulas Patocka out_free_workqueue:
349acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
35051157b4aSKiyoshi Ueda out_uevent_exit:
35151157b4aSKiyoshi Ueda 	dm_uevent_exit();
3521ae49ea2SMike Snitzer out_free_rq_cache:
3531ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
3548fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
3558fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
35651157b4aSKiyoshi Ueda out_free_io_cache:
35751157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
35851157b4aSKiyoshi Ueda 
35951157b4aSKiyoshi Ueda 	return r;
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds static void local_exit(void)
3631da177e4SLinus Torvalds {
3642c140a24SMikulas Patocka 	flush_scheduled_work();
365acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
3662c140a24SMikulas Patocka 
3671ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
3688fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
3691da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
37000d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
37151e5b2bdSMike Anderson 	dm_uevent_exit();
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	_major = 0;
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	DMINFO("cleaned up");
3761da177e4SLinus Torvalds }
3771da177e4SLinus Torvalds 
378b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
3791da177e4SLinus Torvalds 	local_init,
3801da177e4SLinus Torvalds 	dm_target_init,
3811da177e4SLinus Torvalds 	dm_linear_init,
3821da177e4SLinus Torvalds 	dm_stripe_init,
383952b3557SMikulas Patocka 	dm_io_init,
384945fa4d2SMikulas Patocka 	dm_kcopyd_init,
3851da177e4SLinus Torvalds 	dm_interface_init,
386fd2ed4d2SMikulas Patocka 	dm_statistics_init,
3871da177e4SLinus Torvalds };
3881da177e4SLinus Torvalds 
389b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
3901da177e4SLinus Torvalds 	local_exit,
3911da177e4SLinus Torvalds 	dm_target_exit,
3921da177e4SLinus Torvalds 	dm_linear_exit,
3931da177e4SLinus Torvalds 	dm_stripe_exit,
394952b3557SMikulas Patocka 	dm_io_exit,
395945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
3961da177e4SLinus Torvalds 	dm_interface_exit,
397fd2ed4d2SMikulas Patocka 	dm_statistics_exit,
3981da177e4SLinus Torvalds };
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds static int __init dm_init(void)
4011da177e4SLinus Torvalds {
4021da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	int r, i;
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
4071da177e4SLinus Torvalds 		r = _inits[i]();
4081da177e4SLinus Torvalds 		if (r)
4091da177e4SLinus Torvalds 			goto bad;
4101da177e4SLinus Torvalds 	}
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	return 0;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds       bad:
4151da177e4SLinus Torvalds 	while (i--)
4161da177e4SLinus Torvalds 		_exits[i]();
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	return r;
4191da177e4SLinus Torvalds }
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds static void __exit dm_exit(void)
4221da177e4SLinus Torvalds {
4231da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	while (i--)
4261da177e4SLinus Torvalds 		_exits[i]();
427d15b774cSAlasdair G Kergon 
428d15b774cSAlasdair G Kergon 	/*
429d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
430d15b774cSAlasdair G Kergon 	 */
431d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds /*
4351da177e4SLinus Torvalds  * Block device functions
4361da177e4SLinus Torvalds  */
437432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
438432a212cSMike Anderson {
439432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
440432a212cSMike Anderson }
441432a212cSMike Anderson 
442fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
4431da177e4SLinus Torvalds {
4441da177e4SLinus Torvalds 	struct mapped_device *md;
4451da177e4SLinus Torvalds 
446fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
447fba9f90eSJeff Mahoney 
448fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
449fba9f90eSJeff Mahoney 	if (!md)
450fba9f90eSJeff Mahoney 		goto out;
451fba9f90eSJeff Mahoney 
4525c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
453432a212cSMike Anderson 	    dm_deleting_md(md)) {
454fba9f90eSJeff Mahoney 		md = NULL;
455fba9f90eSJeff Mahoney 		goto out;
456fba9f90eSJeff Mahoney 	}
457fba9f90eSJeff Mahoney 
4581da177e4SLinus Torvalds 	dm_get(md);
4595c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
460fba9f90eSJeff Mahoney out:
461fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
462fba9f90eSJeff Mahoney 
463fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
466db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode)
4671da177e4SLinus Torvalds {
46863a4f065SMike Snitzer 	struct mapped_device *md;
4696e9624b8SArnd Bergmann 
4704a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
4714a1aeb98SMilan Broz 
47263a4f065SMike Snitzer 	md = disk->private_data;
47363a4f065SMike Snitzer 	if (WARN_ON(!md))
47463a4f065SMike Snitzer 		goto out;
47563a4f065SMike Snitzer 
4762c140a24SMikulas Patocka 	if (atomic_dec_and_test(&md->open_count) &&
4772c140a24SMikulas Patocka 	    (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
478acfe0ad7SMikulas Patocka 		queue_work(deferred_remove_workqueue, &deferred_remove_work);
4792c140a24SMikulas Patocka 
4801da177e4SLinus Torvalds 	dm_put(md);
48163a4f065SMike Snitzer out:
4824a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds 
4855c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
4865c6bd75dSAlasdair G Kergon {
4875c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
4885c6bd75dSAlasdair G Kergon }
4895c6bd75dSAlasdair G Kergon 
4905c6bd75dSAlasdair G Kergon /*
4915c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
4925c6bd75dSAlasdair G Kergon  */
4932c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
4945c6bd75dSAlasdair G Kergon {
4955c6bd75dSAlasdair G Kergon 	int r = 0;
4965c6bd75dSAlasdair G Kergon 
4975c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
4985c6bd75dSAlasdair G Kergon 
4992c140a24SMikulas Patocka 	if (dm_open_count(md)) {
5005c6bd75dSAlasdair G Kergon 		r = -EBUSY;
5012c140a24SMikulas Patocka 		if (mark_deferred)
5022c140a24SMikulas Patocka 			set_bit(DMF_DEFERRED_REMOVE, &md->flags);
5032c140a24SMikulas Patocka 	} else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
5042c140a24SMikulas Patocka 		r = -EEXIST;
5055c6bd75dSAlasdair G Kergon 	else
5065c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
5075c6bd75dSAlasdair G Kergon 
5085c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
5095c6bd75dSAlasdair G Kergon 
5105c6bd75dSAlasdair G Kergon 	return r;
5115c6bd75dSAlasdair G Kergon }
5125c6bd75dSAlasdair G Kergon 
5132c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md)
5142c140a24SMikulas Patocka {
5152c140a24SMikulas Patocka 	int r = 0;
5162c140a24SMikulas Patocka 
5172c140a24SMikulas Patocka 	spin_lock(&_minor_lock);
5182c140a24SMikulas Patocka 
5192c140a24SMikulas Patocka 	if (test_bit(DMF_DELETING, &md->flags))
5202c140a24SMikulas Patocka 		r = -EBUSY;
5212c140a24SMikulas Patocka 	else
5222c140a24SMikulas Patocka 		clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
5232c140a24SMikulas Patocka 
5242c140a24SMikulas Patocka 	spin_unlock(&_minor_lock);
5252c140a24SMikulas Patocka 
5262c140a24SMikulas Patocka 	return r;
5272c140a24SMikulas Patocka }
5282c140a24SMikulas Patocka 
5292c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w)
5302c140a24SMikulas Patocka {
5312c140a24SMikulas Patocka 	dm_deferred_remove();
5322c140a24SMikulas Patocka }
5332c140a24SMikulas Patocka 
534fd2ed4d2SMikulas Patocka sector_t dm_get_size(struct mapped_device *md)
535fd2ed4d2SMikulas Patocka {
536fd2ed4d2SMikulas Patocka 	return get_capacity(md->disk);
537fd2ed4d2SMikulas Patocka }
538fd2ed4d2SMikulas Patocka 
5399974fa2cSMike Snitzer struct request_queue *dm_get_md_queue(struct mapped_device *md)
5409974fa2cSMike Snitzer {
5419974fa2cSMike Snitzer 	return md->queue;
5429974fa2cSMike Snitzer }
5439974fa2cSMike Snitzer 
544fd2ed4d2SMikulas Patocka struct dm_stats *dm_get_stats(struct mapped_device *md)
545fd2ed4d2SMikulas Patocka {
546fd2ed4d2SMikulas Patocka 	return &md->stats;
547fd2ed4d2SMikulas Patocka }
548fd2ed4d2SMikulas Patocka 
5493ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5503ac51e74SDarrick J. Wong {
5513ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
5523ac51e74SDarrick J. Wong 
5533ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
5543ac51e74SDarrick J. Wong }
5553ac51e74SDarrick J. Wong 
556fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
557aa129a22SMilan Broz 			unsigned int cmd, unsigned long arg)
558aa129a22SMilan Broz {
559fe5f9f2cSAl Viro 	struct mapped_device *md = bdev->bd_disk->private_data;
56083d5e5b0SMikulas Patocka 	int srcu_idx;
5616c182cd8SHannes Reinecke 	struct dm_table *map;
562aa129a22SMilan Broz 	struct dm_target *tgt;
563aa129a22SMilan Broz 	int r = -ENOTTY;
564aa129a22SMilan Broz 
5656c182cd8SHannes Reinecke retry:
56683d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
56783d5e5b0SMikulas Patocka 
568aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
569aa129a22SMilan Broz 		goto out;
570aa129a22SMilan Broz 
571aa129a22SMilan Broz 	/* We only support devices that have a single target */
572aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
573aa129a22SMilan Broz 		goto out;
574aa129a22SMilan Broz 
575aa129a22SMilan Broz 	tgt = dm_table_get_target(map, 0);
5764d341d82SMike Snitzer 	if (!tgt->type->ioctl)
5774d341d82SMike Snitzer 		goto out;
578aa129a22SMilan Broz 
5794f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
580aa129a22SMilan Broz 		r = -EAGAIN;
581aa129a22SMilan Broz 		goto out;
582aa129a22SMilan Broz 	}
583aa129a22SMilan Broz 
584647b3d00SAl Viro 	r = tgt->type->ioctl(tgt, cmd, arg);
585aa129a22SMilan Broz 
586aa129a22SMilan Broz out:
58783d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
588aa129a22SMilan Broz 
5896c182cd8SHannes Reinecke 	if (r == -ENOTCONN) {
5906c182cd8SHannes Reinecke 		msleep(10);
5916c182cd8SHannes Reinecke 		goto retry;
5926c182cd8SHannes Reinecke 	}
5936c182cd8SHannes Reinecke 
594aa129a22SMilan Broz 	return r;
595aa129a22SMilan Broz }
596aa129a22SMilan Broz 
597028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
5981da177e4SLinus Torvalds {
5991da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds 
602028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
6031da177e4SLinus Torvalds {
6041da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
6051da177e4SLinus Torvalds }
6061da177e4SLinus Torvalds 
607028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
6081da177e4SLinus Torvalds {
609dba14160SMikulas Patocka 	bio_put(&tio->clone);
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds 
61208885643SKiyoshi Ueda static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
61308885643SKiyoshi Ueda 					    gfp_t gfp_mask)
614cec47e3dSKiyoshi Ueda {
6155f015204SJun'ichi Nomura 	return mempool_alloc(md->io_pool, gfp_mask);
616cec47e3dSKiyoshi Ueda }
617cec47e3dSKiyoshi Ueda 
618cec47e3dSKiyoshi Ueda static void free_rq_tio(struct dm_rq_target_io *tio)
619cec47e3dSKiyoshi Ueda {
6205f015204SJun'ichi Nomura 	mempool_free(tio, tio->md->io_pool);
621cec47e3dSKiyoshi Ueda }
622cec47e3dSKiyoshi Ueda 
6231ae49ea2SMike Snitzer static struct request *alloc_clone_request(struct mapped_device *md,
6241ae49ea2SMike Snitzer 					   gfp_t gfp_mask)
6251ae49ea2SMike Snitzer {
6261ae49ea2SMike Snitzer 	return mempool_alloc(md->rq_pool, gfp_mask);
6271ae49ea2SMike Snitzer }
6281ae49ea2SMike Snitzer 
6291ae49ea2SMike Snitzer static void free_clone_request(struct mapped_device *md, struct request *rq)
6301ae49ea2SMike Snitzer {
6311ae49ea2SMike Snitzer 	mempool_free(rq, md->rq_pool);
6321ae49ea2SMike Snitzer }
6331ae49ea2SMike Snitzer 
63490abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md)
63590abb8c4SKiyoshi Ueda {
63690abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
63790abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
63890abb8c4SKiyoshi Ueda }
63990abb8c4SKiyoshi Ueda 
6403eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
6413eaf840eSJun'ichi "Nick" Nomura {
6423eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
643fd2ed4d2SMikulas Patocka 	struct bio *bio = io->bio;
644c9959059STejun Heo 	int cpu;
645fd2ed4d2SMikulas Patocka 	int rw = bio_data_dir(bio);
6463eaf840eSJun'ichi "Nick" Nomura 
6473eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
6483eaf840eSJun'ichi "Nick" Nomura 
649074a7acaSTejun Heo 	cpu = part_stat_lock();
650074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
651074a7acaSTejun Heo 	part_stat_unlock();
6521e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw],
6531e9bb880SShaohua Li 		atomic_inc_return(&md->pending[rw]));
654fd2ed4d2SMikulas Patocka 
655fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
6564f024f37SKent Overstreet 		dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
657fd2ed4d2SMikulas Patocka 				    bio_sectors(bio), false, 0, &io->stats_aux);
6583eaf840eSJun'ichi "Nick" Nomura }
6593eaf840eSJun'ichi "Nick" Nomura 
660d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
6613eaf840eSJun'ichi "Nick" Nomura {
6623eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
6633eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
6643eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
66518c0b223SGu Zheng 	int pending;
6663eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
6673eaf840eSJun'ichi "Nick" Nomura 
66818c0b223SGu Zheng 	generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time);
6693eaf840eSJun'ichi "Nick" Nomura 
670fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
6714f024f37SKent Overstreet 		dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
672fd2ed4d2SMikulas Patocka 				    bio_sectors(bio), true, duration, &io->stats_aux);
673fd2ed4d2SMikulas Patocka 
674af7e466aSMikulas Patocka 	/*
675af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
676d87f4c14STejun Heo 	 * a flush.
677af7e466aSMikulas Patocka 	 */
6781e9bb880SShaohua Li 	pending = atomic_dec_return(&md->pending[rw]);
6791e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
680316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
6813eaf840eSJun'ichi "Nick" Nomura 
682d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
683d221d2e7SMikulas Patocka 	if (!pending)
684d221d2e7SMikulas Patocka 		wake_up(&md->wait);
6853eaf840eSJun'ichi "Nick" Nomura }
6863eaf840eSJun'ichi "Nick" Nomura 
6871da177e4SLinus Torvalds /*
6881da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
6891da177e4SLinus Torvalds  */
69092c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
6911da177e4SLinus Torvalds {
69205447420SKiyoshi Ueda 	unsigned long flags;
6931da177e4SLinus Torvalds 
69405447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
6951da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
69605447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
69792c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
6981da177e4SLinus Torvalds }
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds /*
7011da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
7021da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
70383d5e5b0SMikulas Patocka  * dm_put_live_table() when finished.
7041da177e4SLinus Torvalds  */
70583d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
7061da177e4SLinus Torvalds {
70783d5e5b0SMikulas Patocka 	*srcu_idx = srcu_read_lock(&md->io_barrier);
7081da177e4SLinus Torvalds 
70983d5e5b0SMikulas Patocka 	return srcu_dereference(md->map, &md->io_barrier);
71083d5e5b0SMikulas Patocka }
7111da177e4SLinus Torvalds 
71283d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
71383d5e5b0SMikulas Patocka {
71483d5e5b0SMikulas Patocka 	srcu_read_unlock(&md->io_barrier, srcu_idx);
71583d5e5b0SMikulas Patocka }
71683d5e5b0SMikulas Patocka 
71783d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md)
71883d5e5b0SMikulas Patocka {
71983d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
72083d5e5b0SMikulas Patocka 	synchronize_rcu_expedited();
72183d5e5b0SMikulas Patocka }
72283d5e5b0SMikulas Patocka 
72383d5e5b0SMikulas Patocka /*
72483d5e5b0SMikulas Patocka  * A fast alternative to dm_get_live_table/dm_put_live_table.
72583d5e5b0SMikulas Patocka  * The caller must not block between these two functions.
72683d5e5b0SMikulas Patocka  */
72783d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
72883d5e5b0SMikulas Patocka {
72983d5e5b0SMikulas Patocka 	rcu_read_lock();
73083d5e5b0SMikulas Patocka 	return rcu_dereference(md->map);
73183d5e5b0SMikulas Patocka }
73283d5e5b0SMikulas Patocka 
73383d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
73483d5e5b0SMikulas Patocka {
73583d5e5b0SMikulas Patocka 	rcu_read_unlock();
7361da177e4SLinus Torvalds }
7371da177e4SLinus Torvalds 
7383ac51e74SDarrick J. Wong /*
73986f1152bSBenjamin Marzinski  * Open a table device so we can use it as a map destination.
74086f1152bSBenjamin Marzinski  */
74186f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev,
74286f1152bSBenjamin Marzinski 			     struct mapped_device *md)
74386f1152bSBenjamin Marzinski {
74486f1152bSBenjamin Marzinski 	static char *_claim_ptr = "I belong to device-mapper";
74586f1152bSBenjamin Marzinski 	struct block_device *bdev;
74686f1152bSBenjamin Marzinski 
74786f1152bSBenjamin Marzinski 	int r;
74886f1152bSBenjamin Marzinski 
74986f1152bSBenjamin Marzinski 	BUG_ON(td->dm_dev.bdev);
75086f1152bSBenjamin Marzinski 
75186f1152bSBenjamin Marzinski 	bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
75286f1152bSBenjamin Marzinski 	if (IS_ERR(bdev))
75386f1152bSBenjamin Marzinski 		return PTR_ERR(bdev);
75486f1152bSBenjamin Marzinski 
75586f1152bSBenjamin Marzinski 	r = bd_link_disk_holder(bdev, dm_disk(md));
75686f1152bSBenjamin Marzinski 	if (r) {
75786f1152bSBenjamin Marzinski 		blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
75886f1152bSBenjamin Marzinski 		return r;
75986f1152bSBenjamin Marzinski 	}
76086f1152bSBenjamin Marzinski 
76186f1152bSBenjamin Marzinski 	td->dm_dev.bdev = bdev;
76286f1152bSBenjamin Marzinski 	return 0;
76386f1152bSBenjamin Marzinski }
76486f1152bSBenjamin Marzinski 
76586f1152bSBenjamin Marzinski /*
76686f1152bSBenjamin Marzinski  * Close a table device that we've been using.
76786f1152bSBenjamin Marzinski  */
76886f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md)
76986f1152bSBenjamin Marzinski {
77086f1152bSBenjamin Marzinski 	if (!td->dm_dev.bdev)
77186f1152bSBenjamin Marzinski 		return;
77286f1152bSBenjamin Marzinski 
77386f1152bSBenjamin Marzinski 	bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
77486f1152bSBenjamin Marzinski 	blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
77586f1152bSBenjamin Marzinski 	td->dm_dev.bdev = NULL;
77686f1152bSBenjamin Marzinski }
77786f1152bSBenjamin Marzinski 
77886f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev,
77986f1152bSBenjamin Marzinski 					      fmode_t mode) {
78086f1152bSBenjamin Marzinski 	struct table_device *td;
78186f1152bSBenjamin Marzinski 
78286f1152bSBenjamin Marzinski 	list_for_each_entry(td, l, list)
78386f1152bSBenjamin Marzinski 		if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
78486f1152bSBenjamin Marzinski 			return td;
78586f1152bSBenjamin Marzinski 
78686f1152bSBenjamin Marzinski 	return NULL;
78786f1152bSBenjamin Marzinski }
78886f1152bSBenjamin Marzinski 
78986f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
79086f1152bSBenjamin Marzinski 			struct dm_dev **result) {
79186f1152bSBenjamin Marzinski 	int r;
79286f1152bSBenjamin Marzinski 	struct table_device *td;
79386f1152bSBenjamin Marzinski 
79486f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
79586f1152bSBenjamin Marzinski 	td = find_table_device(&md->table_devices, dev, mode);
79686f1152bSBenjamin Marzinski 	if (!td) {
79786f1152bSBenjamin Marzinski 		td = kmalloc(sizeof(*td), GFP_KERNEL);
79886f1152bSBenjamin Marzinski 		if (!td) {
79986f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
80086f1152bSBenjamin Marzinski 			return -ENOMEM;
80186f1152bSBenjamin Marzinski 		}
80286f1152bSBenjamin Marzinski 
80386f1152bSBenjamin Marzinski 		td->dm_dev.mode = mode;
80486f1152bSBenjamin Marzinski 		td->dm_dev.bdev = NULL;
80586f1152bSBenjamin Marzinski 
80686f1152bSBenjamin Marzinski 		if ((r = open_table_device(td, dev, md))) {
80786f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
80886f1152bSBenjamin Marzinski 			kfree(td);
80986f1152bSBenjamin Marzinski 			return r;
81086f1152bSBenjamin Marzinski 		}
81186f1152bSBenjamin Marzinski 
81286f1152bSBenjamin Marzinski 		format_dev_t(td->dm_dev.name, dev);
81386f1152bSBenjamin Marzinski 
81486f1152bSBenjamin Marzinski 		atomic_set(&td->count, 0);
81586f1152bSBenjamin Marzinski 		list_add(&td->list, &md->table_devices);
81686f1152bSBenjamin Marzinski 	}
81786f1152bSBenjamin Marzinski 	atomic_inc(&td->count);
81886f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
81986f1152bSBenjamin Marzinski 
82086f1152bSBenjamin Marzinski 	*result = &td->dm_dev;
82186f1152bSBenjamin Marzinski 	return 0;
82286f1152bSBenjamin Marzinski }
82386f1152bSBenjamin Marzinski EXPORT_SYMBOL_GPL(dm_get_table_device);
82486f1152bSBenjamin Marzinski 
82586f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
82686f1152bSBenjamin Marzinski {
82786f1152bSBenjamin Marzinski 	struct table_device *td = container_of(d, struct table_device, dm_dev);
82886f1152bSBenjamin Marzinski 
82986f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
83086f1152bSBenjamin Marzinski 	if (atomic_dec_and_test(&td->count)) {
83186f1152bSBenjamin Marzinski 		close_table_device(td, md);
83286f1152bSBenjamin Marzinski 		list_del(&td->list);
83386f1152bSBenjamin Marzinski 		kfree(td);
83486f1152bSBenjamin Marzinski 	}
83586f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
83686f1152bSBenjamin Marzinski }
83786f1152bSBenjamin Marzinski EXPORT_SYMBOL(dm_put_table_device);
83886f1152bSBenjamin Marzinski 
83986f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices)
84086f1152bSBenjamin Marzinski {
84186f1152bSBenjamin Marzinski 	struct list_head *tmp, *next;
84286f1152bSBenjamin Marzinski 
84386f1152bSBenjamin Marzinski 	list_for_each_safe(tmp, next, devices) {
84486f1152bSBenjamin Marzinski 		struct table_device *td = list_entry(tmp, struct table_device, list);
84586f1152bSBenjamin Marzinski 
84686f1152bSBenjamin Marzinski 		DMWARN("dm_destroy: %s still exists with %d references",
84786f1152bSBenjamin Marzinski 		       td->dm_dev.name, atomic_read(&td->count));
84886f1152bSBenjamin Marzinski 		kfree(td);
84986f1152bSBenjamin Marzinski 	}
85086f1152bSBenjamin Marzinski }
85186f1152bSBenjamin Marzinski 
85286f1152bSBenjamin Marzinski /*
8533ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
8543ac51e74SDarrick J. Wong  */
8553ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
8563ac51e74SDarrick J. Wong {
8573ac51e74SDarrick J. Wong 	*geo = md->geometry;
8583ac51e74SDarrick J. Wong 
8593ac51e74SDarrick J. Wong 	return 0;
8603ac51e74SDarrick J. Wong }
8613ac51e74SDarrick J. Wong 
8623ac51e74SDarrick J. Wong /*
8633ac51e74SDarrick J. Wong  * Set the geometry of a device.
8643ac51e74SDarrick J. Wong  */
8653ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
8663ac51e74SDarrick J. Wong {
8673ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
8683ac51e74SDarrick J. Wong 
8693ac51e74SDarrick J. Wong 	if (geo->start > sz) {
8703ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
8713ac51e74SDarrick J. Wong 		return -EINVAL;
8723ac51e74SDarrick J. Wong 	}
8733ac51e74SDarrick J. Wong 
8743ac51e74SDarrick J. Wong 	md->geometry = *geo;
8753ac51e74SDarrick J. Wong 
8763ac51e74SDarrick J. Wong 	return 0;
8773ac51e74SDarrick J. Wong }
8783ac51e74SDarrick J. Wong 
8791da177e4SLinus Torvalds /*-----------------------------------------------------------------
8801da177e4SLinus Torvalds  * CRUD START:
8811da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
8821da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
8831da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
8841da177e4SLinus Torvalds  *   interests of getting something for people to use I give
8851da177e4SLinus Torvalds  *   you this clearly demarcated crap.
8861da177e4SLinus Torvalds  *---------------------------------------------------------------*/
8871da177e4SLinus Torvalds 
8882e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
8892e93ccc1SKiyoshi Ueda {
8902e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
8912e93ccc1SKiyoshi Ueda }
8922e93ccc1SKiyoshi Ueda 
8931da177e4SLinus Torvalds /*
8941da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
8951da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
8961da177e4SLinus Torvalds  */
897858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
8981da177e4SLinus Torvalds {
8992e93ccc1SKiyoshi Ueda 	unsigned long flags;
900b35f8caaSMilan Broz 	int io_error;
901b35f8caaSMilan Broz 	struct bio *bio;
902b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
9032e93ccc1SKiyoshi Ueda 
9042e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
905f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
906f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
907f88fb981SKiyoshi Ueda 		if (!(io->error > 0 && __noflush_suspending(md)))
9081da177e4SLinus Torvalds 			io->error = error;
909f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
910f88fb981SKiyoshi Ueda 	}
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
9132e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
9142e93ccc1SKiyoshi Ueda 			/*
9152e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
9162e93ccc1SKiyoshi Ueda 			 */
917022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
9186a8736d1STejun Heo 			if (__noflush_suspending(md))
9196a8736d1STejun Heo 				bio_list_add_head(&md->deferred, io->bio);
9206a8736d1STejun Heo 			else
9212e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
9222e93ccc1SKiyoshi Ueda 				io->error = -EIO;
923022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
9242e93ccc1SKiyoshi Ueda 		}
9252e93ccc1SKiyoshi Ueda 
926b35f8caaSMilan Broz 		io_error = io->error;
927b35f8caaSMilan Broz 		bio = io->bio;
928af7e466aSMikulas Patocka 		end_io_acct(io);
929a97f925aSMikulas Patocka 		free_io(md, io);
9301da177e4SLinus Torvalds 
9316a8736d1STejun Heo 		if (io_error == DM_ENDIO_REQUEUE)
9326a8736d1STejun Heo 			return;
9336a8736d1STejun Heo 
9344f024f37SKent Overstreet 		if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
9351da177e4SLinus Torvalds 			/*
9366a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
9376a8736d1STejun Heo 			 * without REQ_FLUSH.
9381da177e4SLinus Torvalds 			 */
9396a8736d1STejun Heo 			bio->bi_rw &= ~REQ_FLUSH;
9406a8736d1STejun Heo 			queue_io(md, bio);
9415f3ea37cSArnaldo Carvalho de Melo 		} else {
942b372d360SMike Snitzer 			/* done with normal IO or empty flush */
9430a82a8d1SLinus Torvalds 			trace_block_bio_complete(md->queue, bio, io_error);
944b35f8caaSMilan Broz 			bio_endio(bio, io_error);
9452e93ccc1SKiyoshi Ueda 		}
9461da177e4SLinus Torvalds 	}
947af7e466aSMikulas Patocka }
9481da177e4SLinus Torvalds 
9497eee4ae2SMike Snitzer static void disable_write_same(struct mapped_device *md)
9507eee4ae2SMike Snitzer {
9517eee4ae2SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
9527eee4ae2SMike Snitzer 
9537eee4ae2SMike Snitzer 	/* device doesn't really support WRITE SAME, disable it */
9547eee4ae2SMike Snitzer 	limits->max_write_same_sectors = 0;
9557eee4ae2SMike Snitzer }
9567eee4ae2SMike Snitzer 
9576712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error)
9581da177e4SLinus Torvalds {
9595164beceSzhendong chen 	int r = error;
960bfc6d41cSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
961b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
9629faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
9631da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	if (!bio_flagged(bio, BIO_UPTODATE) && !error)
9661da177e4SLinus Torvalds 		error = -EIO;
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	if (endio) {
9697de3ee57SMikulas Patocka 		r = endio(tio->ti, bio, error);
9702e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
9712e93ccc1SKiyoshi Ueda 			/*
9722e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
9732e93ccc1SKiyoshi Ueda 			 * in dec_pending().
9742e93ccc1SKiyoshi Ueda 			 */
9751da177e4SLinus Torvalds 			error = r;
97645cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
97745cbcd79SKiyoshi Ueda 			/* The target will handle the io */
9786712ecf8SNeilBrown 			return;
97945cbcd79SKiyoshi Ueda 		else if (r) {
98045cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
98145cbcd79SKiyoshi Ueda 			BUG();
98245cbcd79SKiyoshi Ueda 		}
9831da177e4SLinus Torvalds 	}
9841da177e4SLinus Torvalds 
9857eee4ae2SMike Snitzer 	if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) &&
9867eee4ae2SMike Snitzer 		     !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
9877eee4ae2SMike Snitzer 		disable_write_same(md);
9887eee4ae2SMike Snitzer 
9899faf400fSStefan Bader 	free_tio(md, tio);
990b35f8caaSMilan Broz 	dec_pending(io, error);
9911da177e4SLinus Torvalds }
9921da177e4SLinus Torvalds 
993bfebd1cdSMike Snitzer static struct dm_rq_target_io *tio_from_request(struct request *rq)
994bfebd1cdSMike Snitzer {
995bfebd1cdSMike Snitzer 	return (rq->q->mq_ops ? blk_mq_rq_to_pdu(rq) : rq->special);
996bfebd1cdSMike Snitzer }
997bfebd1cdSMike Snitzer 
998cec47e3dSKiyoshi Ueda /*
999cec47e3dSKiyoshi Ueda  * Don't touch any member of the md after calling this function because
1000cec47e3dSKiyoshi Ueda  * the md may be freed in dm_put() at the end of this function.
1001cec47e3dSKiyoshi Ueda  * Or do dm_get() before calling this function and dm_put() later.
1002cec47e3dSKiyoshi Ueda  */
1003466d89a6SKeith Busch static void rq_completed(struct mapped_device *md, int rw, bool run_queue)
1004cec47e3dSKiyoshi Ueda {
10059a0e609eSMike Snitzer 	int nr_requests_pending;
10069a0e609eSMike Snitzer 
1007b4324feeSKiyoshi Ueda 	atomic_dec(&md->pending[rw]);
1008cec47e3dSKiyoshi Ueda 
1009cec47e3dSKiyoshi Ueda 	/* nudge anyone waiting on suspend queue */
10109a0e609eSMike Snitzer 	nr_requests_pending = md_in_flight(md);
10119a0e609eSMike Snitzer 	if (!nr_requests_pending)
1012cec47e3dSKiyoshi Ueda 		wake_up(&md->wait);
1013cec47e3dSKiyoshi Ueda 
1014a8c32a5cSJens Axboe 	/*
1015a8c32a5cSJens Axboe 	 * Run this off this callpath, as drivers could invoke end_io while
1016a8c32a5cSJens Axboe 	 * inside their request_fn (and holding the queue lock). Calling
1017a8c32a5cSJens Axboe 	 * back into ->request_fn() could deadlock attempting to grab the
1018a8c32a5cSJens Axboe 	 * queue lock again.
1019a8c32a5cSJens Axboe 	 */
10209a0e609eSMike Snitzer 	if (run_queue) {
1021bfebd1cdSMike Snitzer 		if (md->queue->mq_ops)
1022bfebd1cdSMike Snitzer 			blk_mq_run_hw_queues(md->queue, true);
1023bfebd1cdSMike Snitzer 		else if (!nr_requests_pending ||
10249a0e609eSMike Snitzer 			 (nr_requests_pending >= md->queue->nr_congestion_on))
1025a8c32a5cSJens Axboe 			blk_run_queue_async(md->queue);
10269a0e609eSMike Snitzer 	}
1027cec47e3dSKiyoshi Ueda 
1028cec47e3dSKiyoshi Ueda 	/*
1029cec47e3dSKiyoshi Ueda 	 * dm_put() must be at the end of this function. See the comment above
1030cec47e3dSKiyoshi Ueda 	 */
1031cec47e3dSKiyoshi Ueda 	dm_put(md);
1032cec47e3dSKiyoshi Ueda }
1033cec47e3dSKiyoshi Ueda 
1034e5d8de32SMike Snitzer static void free_rq_clone(struct request *clone)
1035a77e28c7SKiyoshi Ueda {
1036a77e28c7SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1037bfebd1cdSMike Snitzer 	struct mapped_device *md = tio->md;
1038a77e28c7SKiyoshi Ueda 
1039aa6df8ddSMike Snitzer 	if (md->type == DM_TYPE_MQ_REQUEST_BASED)
1040aa6df8ddSMike Snitzer 		/* stacked on blk-mq queue(s) */
1041e5863d9aSMike Snitzer 		tio->ti->type->release_clone_rq(clone);
104202233342SMike Snitzer 	else if (!md->queue->mq_ops)
104302233342SMike Snitzer 		/* request_fn queue stacked on request_fn queue(s) */
1044bfebd1cdSMike Snitzer 		free_clone_request(md, clone);
1045aa6df8ddSMike Snitzer 	/*
1046aa6df8ddSMike Snitzer 	 * NOTE: for the blk-mq queue stacked on request_fn queue(s) case:
1047aa6df8ddSMike Snitzer 	 * no need to call free_clone_request() because we leverage blk-mq by
1048aa6df8ddSMike Snitzer 	 * allocating the clone at the end of the blk-mq pdu (see: clone_rq)
1049aa6df8ddSMike Snitzer 	 */
1050bfebd1cdSMike Snitzer 
1051bfebd1cdSMike Snitzer 	if (!md->queue->mq_ops)
1052a77e28c7SKiyoshi Ueda 		free_rq_tio(tio);
1053a77e28c7SKiyoshi Ueda }
1054a77e28c7SKiyoshi Ueda 
1055980691e5SKiyoshi Ueda /*
1056980691e5SKiyoshi Ueda  * Complete the clone and the original request.
1057466d89a6SKeith Busch  * Must be called without clone's queue lock held,
1058466d89a6SKeith Busch  * see end_clone_request() for more details.
1059980691e5SKiyoshi Ueda  */
1060980691e5SKiyoshi Ueda static void dm_end_request(struct request *clone, int error)
1061980691e5SKiyoshi Ueda {
1062980691e5SKiyoshi Ueda 	int rw = rq_data_dir(clone);
1063980691e5SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1064980691e5SKiyoshi Ueda 	struct mapped_device *md = tio->md;
1065980691e5SKiyoshi Ueda 	struct request *rq = tio->orig;
1066980691e5SKiyoshi Ueda 
106729e4013dSTejun Heo 	if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
1068980691e5SKiyoshi Ueda 		rq->errors = clone->errors;
1069980691e5SKiyoshi Ueda 		rq->resid_len = clone->resid_len;
1070980691e5SKiyoshi Ueda 
1071980691e5SKiyoshi Ueda 		if (rq->sense)
1072980691e5SKiyoshi Ueda 			/*
1073980691e5SKiyoshi Ueda 			 * We are using the sense buffer of the original
1074980691e5SKiyoshi Ueda 			 * request.
1075980691e5SKiyoshi Ueda 			 * So setting the length of the sense data is enough.
1076980691e5SKiyoshi Ueda 			 */
1077980691e5SKiyoshi Ueda 			rq->sense_len = clone->sense_len;
1078980691e5SKiyoshi Ueda 	}
1079980691e5SKiyoshi Ueda 
1080e5d8de32SMike Snitzer 	free_rq_clone(clone);
1081bfebd1cdSMike Snitzer 	if (!rq->q->mq_ops)
1082980691e5SKiyoshi Ueda 		blk_end_request_all(rq, error);
1083bfebd1cdSMike Snitzer 	else
1084bfebd1cdSMike Snitzer 		blk_mq_end_request(rq, error);
108529e4013dSTejun Heo 	rq_completed(md, rw, true);
1086980691e5SKiyoshi Ueda }
1087980691e5SKiyoshi Ueda 
1088cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq)
1089cec47e3dSKiyoshi Ueda {
1090bfebd1cdSMike Snitzer 	struct dm_rq_target_io *tio = tio_from_request(rq);
1091466d89a6SKeith Busch 	struct request *clone = tio->clone;
1092cec47e3dSKiyoshi Ueda 
1093bfebd1cdSMike Snitzer 	if (!rq->q->mq_ops) {
1094cec47e3dSKiyoshi Ueda 		rq->special = NULL;
1095cec47e3dSKiyoshi Ueda 		rq->cmd_flags &= ~REQ_DONTPREP;
1096bfebd1cdSMike Snitzer 	}
1097cec47e3dSKiyoshi Ueda 
1098e5863d9aSMike Snitzer 	if (clone)
1099e5d8de32SMike Snitzer 		free_rq_clone(clone);
1100cec47e3dSKiyoshi Ueda }
1101cec47e3dSKiyoshi Ueda 
1102cec47e3dSKiyoshi Ueda /*
1103cec47e3dSKiyoshi Ueda  * Requeue the original request of a clone.
1104cec47e3dSKiyoshi Ueda  */
1105bfebd1cdSMike Snitzer static void old_requeue_request(struct request *rq)
1106cec47e3dSKiyoshi Ueda {
1107cec47e3dSKiyoshi Ueda 	struct request_queue *q = rq->q;
1108cec47e3dSKiyoshi Ueda 	unsigned long flags;
1109cec47e3dSKiyoshi Ueda 
1110cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
1111cec47e3dSKiyoshi Ueda 	blk_requeue_request(q, rq);
11124ae9944dSJunichi Nomura 	blk_run_queue_async(q);
1113cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
1114bfebd1cdSMike Snitzer }
1115bfebd1cdSMike Snitzer 
11162d76fff1SMike Snitzer static void dm_requeue_original_request(struct mapped_device *md,
1117bfebd1cdSMike Snitzer 					struct request *rq)
1118bfebd1cdSMike Snitzer {
1119bfebd1cdSMike Snitzer 	int rw = rq_data_dir(rq);
1120bfebd1cdSMike Snitzer 
1121bfebd1cdSMike Snitzer 	dm_unprep_request(rq);
1122bfebd1cdSMike Snitzer 
1123bfebd1cdSMike Snitzer 	if (!rq->q->mq_ops)
1124bfebd1cdSMike Snitzer 		old_requeue_request(rq);
1125bfebd1cdSMike Snitzer 	else {
1126bfebd1cdSMike Snitzer 		blk_mq_requeue_request(rq);
1127bfebd1cdSMike Snitzer 		blk_mq_kick_requeue_list(rq->q);
1128bfebd1cdSMike Snitzer 	}
1129cec47e3dSKiyoshi Ueda 
1130466d89a6SKeith Busch 	rq_completed(md, rw, false);
1131cec47e3dSKiyoshi Ueda }
1132466d89a6SKeith Busch 
1133bfebd1cdSMike Snitzer static void old_stop_queue(struct request_queue *q)
1134cec47e3dSKiyoshi Ueda {
1135bfebd1cdSMike Snitzer 	unsigned long flags;
1136bfebd1cdSMike Snitzer 
1137bfebd1cdSMike Snitzer 	if (blk_queue_stopped(q))
1138bfebd1cdSMike Snitzer 		return;
1139bfebd1cdSMike Snitzer 
1140bfebd1cdSMike Snitzer 	spin_lock_irqsave(q->queue_lock, flags);
1141cec47e3dSKiyoshi Ueda 	blk_stop_queue(q);
1142bfebd1cdSMike Snitzer 	spin_unlock_irqrestore(q->queue_lock, flags);
1143cec47e3dSKiyoshi Ueda }
1144cec47e3dSKiyoshi Ueda 
1145cec47e3dSKiyoshi Ueda static void stop_queue(struct request_queue *q)
1146cec47e3dSKiyoshi Ueda {
1147bfebd1cdSMike Snitzer 	if (!q->mq_ops)
1148bfebd1cdSMike Snitzer 		old_stop_queue(q);
1149bfebd1cdSMike Snitzer 	else
1150bfebd1cdSMike Snitzer 		blk_mq_stop_hw_queues(q);
1151bfebd1cdSMike Snitzer }
1152bfebd1cdSMike Snitzer 
1153bfebd1cdSMike Snitzer static void old_start_queue(struct request_queue *q)
1154bfebd1cdSMike Snitzer {
1155cec47e3dSKiyoshi Ueda 	unsigned long flags;
1156cec47e3dSKiyoshi Ueda 
1157cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
1158cec47e3dSKiyoshi Ueda 	if (blk_queue_stopped(q))
1159cec47e3dSKiyoshi Ueda 		blk_start_queue(q);
1160bfebd1cdSMike Snitzer 	spin_unlock_irqrestore(q->queue_lock, flags);
1161cec47e3dSKiyoshi Ueda }
1162cec47e3dSKiyoshi Ueda 
1163cec47e3dSKiyoshi Ueda static void start_queue(struct request_queue *q)
1164cec47e3dSKiyoshi Ueda {
1165bfebd1cdSMike Snitzer 	if (!q->mq_ops)
1166bfebd1cdSMike Snitzer 		old_start_queue(q);
1167bfebd1cdSMike Snitzer 	else
1168bfebd1cdSMike Snitzer 		blk_mq_start_stopped_hw_queues(q, true);
1169cec47e3dSKiyoshi Ueda }
1170cec47e3dSKiyoshi Ueda 
117111a68244SKiyoshi Ueda static void dm_done(struct request *clone, int error, bool mapped)
117211a68244SKiyoshi Ueda {
117311a68244SKiyoshi Ueda 	int r = error;
117411a68244SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1175ba1cbad9SMike Snitzer 	dm_request_endio_fn rq_end_io = NULL;
1176ba1cbad9SMike Snitzer 
1177ba1cbad9SMike Snitzer 	if (tio->ti) {
1178ba1cbad9SMike Snitzer 		rq_end_io = tio->ti->type->rq_end_io;
117911a68244SKiyoshi Ueda 
118011a68244SKiyoshi Ueda 		if (mapped && rq_end_io)
118111a68244SKiyoshi Ueda 			r = rq_end_io(tio->ti, clone, error, &tio->info);
1182ba1cbad9SMike Snitzer 	}
118311a68244SKiyoshi Ueda 
11847eee4ae2SMike Snitzer 	if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) &&
11857eee4ae2SMike Snitzer 		     !clone->q->limits.max_write_same_sectors))
11867eee4ae2SMike Snitzer 		disable_write_same(tio->md);
11877eee4ae2SMike Snitzer 
118811a68244SKiyoshi Ueda 	if (r <= 0)
118911a68244SKiyoshi Ueda 		/* The target wants to complete the I/O */
119011a68244SKiyoshi Ueda 		dm_end_request(clone, r);
119111a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_INCOMPLETE)
119211a68244SKiyoshi Ueda 		/* The target will handle the I/O */
119311a68244SKiyoshi Ueda 		return;
119411a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_REQUEUE)
119511a68244SKiyoshi Ueda 		/* The target wants to requeue the I/O */
11962d76fff1SMike Snitzer 		dm_requeue_original_request(tio->md, tio->orig);
119711a68244SKiyoshi Ueda 	else {
119811a68244SKiyoshi Ueda 		DMWARN("unimplemented target endio return value: %d", r);
119911a68244SKiyoshi Ueda 		BUG();
120011a68244SKiyoshi Ueda 	}
120111a68244SKiyoshi Ueda }
120211a68244SKiyoshi Ueda 
1203cec47e3dSKiyoshi Ueda /*
1204cec47e3dSKiyoshi Ueda  * Request completion handler for request-based dm
1205cec47e3dSKiyoshi Ueda  */
1206cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq)
1207cec47e3dSKiyoshi Ueda {
120811a68244SKiyoshi Ueda 	bool mapped = true;
1209bfebd1cdSMike Snitzer 	struct dm_rq_target_io *tio = tio_from_request(rq);
1210466d89a6SKeith Busch 	struct request *clone = tio->clone;
1211bfebd1cdSMike Snitzer 	int rw;
1212cec47e3dSKiyoshi Ueda 
1213e5863d9aSMike Snitzer 	if (!clone) {
1214bfebd1cdSMike Snitzer 		rw = rq_data_dir(rq);
1215bfebd1cdSMike Snitzer 		if (!rq->q->mq_ops) {
1216e5863d9aSMike Snitzer 			blk_end_request_all(rq, tio->error);
1217bfebd1cdSMike Snitzer 			rq_completed(tio->md, rw, false);
1218e5863d9aSMike Snitzer 			free_rq_tio(tio);
1219bfebd1cdSMike Snitzer 		} else {
1220bfebd1cdSMike Snitzer 			blk_mq_end_request(rq, tio->error);
1221bfebd1cdSMike Snitzer 			rq_completed(tio->md, rw, false);
1222bfebd1cdSMike Snitzer 		}
1223e5863d9aSMike Snitzer 		return;
1224e5863d9aSMike Snitzer 	}
1225cec47e3dSKiyoshi Ueda 
122611a68244SKiyoshi Ueda 	if (rq->cmd_flags & REQ_FAILED)
122711a68244SKiyoshi Ueda 		mapped = false;
1228cec47e3dSKiyoshi Ueda 
122911a68244SKiyoshi Ueda 	dm_done(clone, tio->error, mapped);
1230cec47e3dSKiyoshi Ueda }
1231cec47e3dSKiyoshi Ueda 
1232cec47e3dSKiyoshi Ueda /*
1233cec47e3dSKiyoshi Ueda  * Complete the clone and the original request with the error status
1234cec47e3dSKiyoshi Ueda  * through softirq context.
1235cec47e3dSKiyoshi Ueda  */
1236466d89a6SKeith Busch static void dm_complete_request(struct request *rq, int error)
1237cec47e3dSKiyoshi Ueda {
1238bfebd1cdSMike Snitzer 	struct dm_rq_target_io *tio = tio_from_request(rq);
1239cec47e3dSKiyoshi Ueda 
1240cec47e3dSKiyoshi Ueda 	tio->error = error;
1241cec47e3dSKiyoshi Ueda 	blk_complete_request(rq);
1242cec47e3dSKiyoshi Ueda }
1243cec47e3dSKiyoshi Ueda 
1244cec47e3dSKiyoshi Ueda /*
1245cec47e3dSKiyoshi Ueda  * Complete the not-mapped clone and the original request with the error status
1246cec47e3dSKiyoshi Ueda  * through softirq context.
1247cec47e3dSKiyoshi Ueda  * Target's rq_end_io() function isn't called.
1248e5863d9aSMike Snitzer  * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
1249cec47e3dSKiyoshi Ueda  */
1250466d89a6SKeith Busch static void dm_kill_unmapped_request(struct request *rq, int error)
1251cec47e3dSKiyoshi Ueda {
1252cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_FAILED;
1253466d89a6SKeith Busch 	dm_complete_request(rq, error);
1254cec47e3dSKiyoshi Ueda }
1255cec47e3dSKiyoshi Ueda 
1256cec47e3dSKiyoshi Ueda /*
1257bfebd1cdSMike Snitzer  * Called with the clone's queue lock held (for non-blk-mq)
1258cec47e3dSKiyoshi Ueda  */
1259cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error)
1260cec47e3dSKiyoshi Ueda {
1261466d89a6SKeith Busch 	struct dm_rq_target_io *tio = clone->end_io_data;
1262466d89a6SKeith Busch 
1263e5863d9aSMike Snitzer 	if (!clone->q->mq_ops) {
1264cec47e3dSKiyoshi Ueda 		/*
1265cec47e3dSKiyoshi Ueda 		 * For just cleaning up the information of the queue in which
1266cec47e3dSKiyoshi Ueda 		 * the clone was dispatched.
1267e5863d9aSMike Snitzer 		 * The clone is *NOT* freed actually here because it is alloced
1268e5863d9aSMike Snitzer 		 * from dm own mempool (REQ_ALLOCED isn't set).
1269cec47e3dSKiyoshi Ueda 		 */
1270cec47e3dSKiyoshi Ueda 		__blk_put_request(clone->q, clone);
1271e5863d9aSMike Snitzer 	}
1272cec47e3dSKiyoshi Ueda 
1273cec47e3dSKiyoshi Ueda 	/*
1274cec47e3dSKiyoshi Ueda 	 * Actual request completion is done in a softirq context which doesn't
1275466d89a6SKeith Busch 	 * hold the clone's queue lock.  Otherwise, deadlock could occur because:
1276cec47e3dSKiyoshi Ueda 	 *     - another request may be submitted by the upper level driver
1277cec47e3dSKiyoshi Ueda 	 *       of the stacking during the completion
1278cec47e3dSKiyoshi Ueda 	 *     - the submission which requires queue lock may be done
1279466d89a6SKeith Busch 	 *       against this clone's queue
1280cec47e3dSKiyoshi Ueda 	 */
1281466d89a6SKeith Busch 	dm_complete_request(tio->orig, error);
1282cec47e3dSKiyoshi Ueda }
1283cec47e3dSKiyoshi Ueda 
128456a67df7SMike Snitzer /*
128556a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
128656a67df7SMike Snitzer  * target boundary.
128756a67df7SMike Snitzer  */
128856a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
12891da177e4SLinus Torvalds {
129056a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
129156a67df7SMike Snitzer 
129256a67df7SMike Snitzer 	return ti->len - target_offset;
129356a67df7SMike Snitzer }
129456a67df7SMike Snitzer 
129556a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
129656a67df7SMike Snitzer {
129756a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
1298542f9038SMike Snitzer 	sector_t offset, max_len;
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 	/*
13011da177e4SLinus Torvalds 	 * Does the target need to split even further?
13021da177e4SLinus Torvalds 	 */
1303542f9038SMike Snitzer 	if (ti->max_io_len) {
1304542f9038SMike Snitzer 		offset = dm_target_offset(ti, sector);
1305542f9038SMike Snitzer 		if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1306542f9038SMike Snitzer 			max_len = sector_div(offset, ti->max_io_len);
1307542f9038SMike Snitzer 		else
1308542f9038SMike Snitzer 			max_len = offset & (ti->max_io_len - 1);
1309542f9038SMike Snitzer 		max_len = ti->max_io_len - max_len;
1310542f9038SMike Snitzer 
1311542f9038SMike Snitzer 		if (len > max_len)
1312542f9038SMike Snitzer 			len = max_len;
13131da177e4SLinus Torvalds 	}
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 	return len;
13161da177e4SLinus Torvalds }
13171da177e4SLinus Torvalds 
1318542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1319542f9038SMike Snitzer {
1320542f9038SMike Snitzer 	if (len > UINT_MAX) {
1321542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1322542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
1323542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
1324542f9038SMike Snitzer 		return -EINVAL;
1325542f9038SMike Snitzer 	}
1326542f9038SMike Snitzer 
1327542f9038SMike Snitzer 	ti->max_io_len = (uint32_t) len;
1328542f9038SMike Snitzer 
1329542f9038SMike Snitzer 	return 0;
1330542f9038SMike Snitzer }
1331542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1332542f9038SMike Snitzer 
13331dd40c3eSMikulas Patocka /*
13341dd40c3eSMikulas Patocka  * A target may call dm_accept_partial_bio only from the map routine.  It is
13351dd40c3eSMikulas Patocka  * allowed for all bio types except REQ_FLUSH.
13361dd40c3eSMikulas Patocka  *
13371dd40c3eSMikulas Patocka  * dm_accept_partial_bio informs the dm that the target only wants to process
13381dd40c3eSMikulas Patocka  * additional n_sectors sectors of the bio and the rest of the data should be
13391dd40c3eSMikulas Patocka  * sent in a next bio.
13401dd40c3eSMikulas Patocka  *
13411dd40c3eSMikulas Patocka  * A diagram that explains the arithmetics:
13421dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
13431dd40c3eSMikulas Patocka  * |         1          |       2       |   3   |
13441dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
13451dd40c3eSMikulas Patocka  *
13461dd40c3eSMikulas Patocka  * <-------------- *tio->len_ptr --------------->
13471dd40c3eSMikulas Patocka  *                      <------- bi_size ------->
13481dd40c3eSMikulas Patocka  *                      <-- n_sectors -->
13491dd40c3eSMikulas Patocka  *
13501dd40c3eSMikulas Patocka  * Region 1 was already iterated over with bio_advance or similar function.
13511dd40c3eSMikulas Patocka  *	(it may be empty if the target doesn't use bio_advance)
13521dd40c3eSMikulas Patocka  * Region 2 is the remaining bio size that the target wants to process.
13531dd40c3eSMikulas Patocka  *	(it may be empty if region 1 is non-empty, although there is no reason
13541dd40c3eSMikulas Patocka  *	 to make it empty)
13551dd40c3eSMikulas Patocka  * The target requires that region 3 is to be sent in the next bio.
13561dd40c3eSMikulas Patocka  *
13571dd40c3eSMikulas Patocka  * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
13581dd40c3eSMikulas Patocka  * the partially processed part (the sum of regions 1+2) must be the same for all
13591dd40c3eSMikulas Patocka  * copies of the bio.
13601dd40c3eSMikulas Patocka  */
13611dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
13621dd40c3eSMikulas Patocka {
13631dd40c3eSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
13641dd40c3eSMikulas Patocka 	unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
13651dd40c3eSMikulas Patocka 	BUG_ON(bio->bi_rw & REQ_FLUSH);
13661dd40c3eSMikulas Patocka 	BUG_ON(bi_size > *tio->len_ptr);
13671dd40c3eSMikulas Patocka 	BUG_ON(n_sectors > bi_size);
13681dd40c3eSMikulas Patocka 	*tio->len_ptr -= bi_size - n_sectors;
13691dd40c3eSMikulas Patocka 	bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
13701dd40c3eSMikulas Patocka }
13711dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
13721dd40c3eSMikulas Patocka 
1373bd2a49b8SAlasdair G Kergon static void __map_bio(struct dm_target_io *tio)
13741da177e4SLinus Torvalds {
13751da177e4SLinus Torvalds 	int r;
13762056a782SJens Axboe 	sector_t sector;
13779faf400fSStefan Bader 	struct mapped_device *md;
1378dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
1379bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 	/*
13841da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
13851da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
13861da177e4SLinus Torvalds 	 * this io.
13871da177e4SLinus Torvalds 	 */
13881da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
13894f024f37SKent Overstreet 	sector = clone->bi_iter.bi_sector;
13907de3ee57SMikulas Patocka 	r = ti->type->map(ti, clone);
139145cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
13921da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
13932056a782SJens Axboe 
1394d07335e5SMike Snitzer 		trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
139522a7c31aSAlan D. Brunelle 				      tio->io->bio->bi_bdev->bd_dev, sector);
13962056a782SJens Axboe 
13971da177e4SLinus Torvalds 		generic_make_request(clone);
13982e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
13992e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
14009faf400fSStefan Bader 		md = tio->io->md;
14019faf400fSStefan Bader 		dec_pending(tio->io, r);
14029faf400fSStefan Bader 		free_tio(md, tio);
140345cbcd79SKiyoshi Ueda 	} else if (r) {
140445cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
140545cbcd79SKiyoshi Ueda 		BUG();
14061da177e4SLinus Torvalds 	}
14071da177e4SLinus Torvalds }
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds struct clone_info {
14101da177e4SLinus Torvalds 	struct mapped_device *md;
14111da177e4SLinus Torvalds 	struct dm_table *map;
14121da177e4SLinus Torvalds 	struct bio *bio;
14131da177e4SLinus Torvalds 	struct dm_io *io;
14141da177e4SLinus Torvalds 	sector_t sector;
1415e0d6609aSMikulas Patocka 	unsigned sector_count;
14161da177e4SLinus Torvalds };
14171da177e4SLinus Torvalds 
1418e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
1419bd2a49b8SAlasdair G Kergon {
14204f024f37SKent Overstreet 	bio->bi_iter.bi_sector = sector;
14214f024f37SKent Overstreet 	bio->bi_iter.bi_size = to_bytes(len);
14221da177e4SLinus Torvalds }
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds /*
14251da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
14261da177e4SLinus Torvalds  */
1427dba14160SMikulas Patocka static void clone_bio(struct dm_target_io *tio, struct bio *bio,
14281c3b13e6SKent Overstreet 		      sector_t sector, unsigned len)
14291da177e4SLinus Torvalds {
1430dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
14311da177e4SLinus Torvalds 
14321c3b13e6SKent Overstreet 	__bio_clone_fast(clone, bio);
14339c47008dSMartin K. Petersen 
14341c3b13e6SKent Overstreet 	if (bio_integrity(bio))
14351c3b13e6SKent Overstreet 		bio_integrity_clone(clone, bio, GFP_NOIO);
14361c3b13e6SKent Overstreet 
14371c3b13e6SKent Overstreet 	bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
14381c3b13e6SKent Overstreet 	clone->bi_iter.bi_size = to_bytes(len);
14391c3b13e6SKent Overstreet 
14401c3b13e6SKent Overstreet 	if (bio_integrity(bio))
14411c3b13e6SKent Overstreet 		bio_integrity_trim(clone, 0, len);
14421da177e4SLinus Torvalds }
14431da177e4SLinus Torvalds 
14449015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci,
144599778273SJunichi Nomura 				      struct dm_target *ti,
144655a62eefSAlasdair G Kergon 				      unsigned target_bio_nr)
1447f9ab94ceSMikulas Patocka {
1448dba14160SMikulas Patocka 	struct dm_target_io *tio;
1449dba14160SMikulas Patocka 	struct bio *clone;
1450dba14160SMikulas Patocka 
145199778273SJunichi Nomura 	clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1452dba14160SMikulas Patocka 	tio = container_of(clone, struct dm_target_io, clone);
1453f9ab94ceSMikulas Patocka 
1454f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1455f9ab94ceSMikulas Patocka 	tio->ti = ti;
145655a62eefSAlasdair G Kergon 	tio->target_bio_nr = target_bio_nr;
14579015df24SAlasdair G Kergon 
14589015df24SAlasdair G Kergon 	return tio;
14599015df24SAlasdair G Kergon }
14609015df24SAlasdair G Kergon 
146114fe594dSAlasdair G Kergon static void __clone_and_map_simple_bio(struct clone_info *ci,
146214fe594dSAlasdair G Kergon 				       struct dm_target *ti,
14631dd40c3eSMikulas Patocka 				       unsigned target_bio_nr, unsigned *len)
14649015df24SAlasdair G Kergon {
146599778273SJunichi Nomura 	struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
1466dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
14679015df24SAlasdair G Kergon 
14681dd40c3eSMikulas Patocka 	tio->len_ptr = len;
14691dd40c3eSMikulas Patocka 
14701c3b13e6SKent Overstreet 	__bio_clone_fast(clone, ci->bio);
1471bd2a49b8SAlasdair G Kergon 	if (len)
14721dd40c3eSMikulas Patocka 		bio_setup_sector(clone, ci->sector, *len);
1473f9ab94ceSMikulas Patocka 
1474bd2a49b8SAlasdair G Kergon 	__map_bio(tio);
1475f9ab94ceSMikulas Patocka }
1476f9ab94ceSMikulas Patocka 
147714fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
14781dd40c3eSMikulas Patocka 				  unsigned num_bios, unsigned *len)
147906a426ceSMike Snitzer {
148055a62eefSAlasdair G Kergon 	unsigned target_bio_nr;
148106a426ceSMike Snitzer 
148255a62eefSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
148314fe594dSAlasdair G Kergon 		__clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
148406a426ceSMike Snitzer }
148506a426ceSMike Snitzer 
148614fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1487f9ab94ceSMikulas Patocka {
148806a426ceSMike Snitzer 	unsigned target_nr = 0;
1489f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1490f9ab94ceSMikulas Patocka 
1491b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1492f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
14931dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
1494f9ab94ceSMikulas Patocka 
1495f9ab94ceSMikulas Patocka 	return 0;
1496f9ab94ceSMikulas Patocka }
1497f9ab94ceSMikulas Patocka 
1498e4c93811SAlasdair G Kergon static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
14991dd40c3eSMikulas Patocka 				     sector_t sector, unsigned *len)
15005ae89a87SMike Snitzer {
1501dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
15025ae89a87SMike Snitzer 	struct dm_target_io *tio;
1503b0d8ed4dSAlasdair G Kergon 	unsigned target_bio_nr;
1504b0d8ed4dSAlasdair G Kergon 	unsigned num_target_bios = 1;
15055ae89a87SMike Snitzer 
1506b0d8ed4dSAlasdair G Kergon 	/*
1507b0d8ed4dSAlasdair G Kergon 	 * Does the target want to receive duplicate copies of the bio?
1508b0d8ed4dSAlasdair G Kergon 	 */
1509b0d8ed4dSAlasdair G Kergon 	if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1510b0d8ed4dSAlasdair G Kergon 		num_target_bios = ti->num_write_bios(ti, bio);
1511e4c93811SAlasdair G Kergon 
1512b0d8ed4dSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
151399778273SJunichi Nomura 		tio = alloc_tio(ci, ti, target_bio_nr);
15141dd40c3eSMikulas Patocka 		tio->len_ptr = len;
15151dd40c3eSMikulas Patocka 		clone_bio(tio, bio, sector, *len);
1516bd2a49b8SAlasdair G Kergon 		__map_bio(tio);
15175ae89a87SMike Snitzer 	}
1518b0d8ed4dSAlasdair G Kergon }
15195ae89a87SMike Snitzer 
152055a62eefSAlasdair G Kergon typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
152123508a96SMike Snitzer 
152255a62eefSAlasdair G Kergon static unsigned get_num_discard_bios(struct dm_target *ti)
152323508a96SMike Snitzer {
152455a62eefSAlasdair G Kergon 	return ti->num_discard_bios;
152523508a96SMike Snitzer }
152623508a96SMike Snitzer 
152755a62eefSAlasdair G Kergon static unsigned get_num_write_same_bios(struct dm_target *ti)
152823508a96SMike Snitzer {
152955a62eefSAlasdair G Kergon 	return ti->num_write_same_bios;
153023508a96SMike Snitzer }
153123508a96SMike Snitzer 
153223508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti);
153323508a96SMike Snitzer 
153423508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti)
153523508a96SMike Snitzer {
153655a62eefSAlasdair G Kergon 	return ti->split_discard_bios;
153723508a96SMike Snitzer }
153823508a96SMike Snitzer 
153914fe594dSAlasdair G Kergon static int __send_changing_extent_only(struct clone_info *ci,
154055a62eefSAlasdair G Kergon 				       get_num_bios_fn get_num_bios,
154123508a96SMike Snitzer 				       is_split_required_fn is_split_required)
15425ae89a87SMike Snitzer {
15435ae89a87SMike Snitzer 	struct dm_target *ti;
1544e0d6609aSMikulas Patocka 	unsigned len;
154555a62eefSAlasdair G Kergon 	unsigned num_bios;
15465ae89a87SMike Snitzer 
1547a79245b3SMike Snitzer 	do {
15485ae89a87SMike Snitzer 		ti = dm_table_find_target(ci->map, ci->sector);
15495ae89a87SMike Snitzer 		if (!dm_target_is_valid(ti))
15505ae89a87SMike Snitzer 			return -EIO;
15515ae89a87SMike Snitzer 
15525ae89a87SMike Snitzer 		/*
155323508a96SMike Snitzer 		 * Even though the device advertised support for this type of
155423508a96SMike Snitzer 		 * request, that does not mean every target supports it, and
1555936688d7SMike Snitzer 		 * reconfiguration might also have changed that since the
15565ae89a87SMike Snitzer 		 * check was performed.
15575ae89a87SMike Snitzer 		 */
155855a62eefSAlasdair G Kergon 		num_bios = get_num_bios ? get_num_bios(ti) : 0;
155955a62eefSAlasdair G Kergon 		if (!num_bios)
15605ae89a87SMike Snitzer 			return -EOPNOTSUPP;
15615ae89a87SMike Snitzer 
156223508a96SMike Snitzer 		if (is_split_required && !is_split_required(ti))
1563e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
15647acf0277SMikulas Patocka 		else
1565e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
15665ae89a87SMike Snitzer 
15671dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, num_bios, &len);
15685ae89a87SMike Snitzer 
1569a79245b3SMike Snitzer 		ci->sector += len;
1570a79245b3SMike Snitzer 	} while (ci->sector_count -= len);
15715ae89a87SMike Snitzer 
15725ae89a87SMike Snitzer 	return 0;
15735ae89a87SMike Snitzer }
15745ae89a87SMike Snitzer 
157514fe594dSAlasdair G Kergon static int __send_discard(struct clone_info *ci)
157623508a96SMike Snitzer {
157714fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_discard_bios,
157823508a96SMike Snitzer 					   is_split_required_for_discard);
157923508a96SMike Snitzer }
158023508a96SMike Snitzer 
158114fe594dSAlasdair G Kergon static int __send_write_same(struct clone_info *ci)
158223508a96SMike Snitzer {
158314fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
158423508a96SMike Snitzer }
158523508a96SMike Snitzer 
1586e4c93811SAlasdair G Kergon /*
1587e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1588e4c93811SAlasdair G Kergon  */
1589e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci)
1590e4c93811SAlasdair G Kergon {
1591e4c93811SAlasdair G Kergon 	struct bio *bio = ci->bio;
1592e4c93811SAlasdair G Kergon 	struct dm_target *ti;
15931c3b13e6SKent Overstreet 	unsigned len;
1594e4c93811SAlasdair G Kergon 
1595e4c93811SAlasdair G Kergon 	if (unlikely(bio->bi_rw & REQ_DISCARD))
1596e4c93811SAlasdair G Kergon 		return __send_discard(ci);
1597e4c93811SAlasdair G Kergon 	else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
1598e4c93811SAlasdair G Kergon 		return __send_write_same(ci);
1599e4c93811SAlasdair G Kergon 
1600e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1601e4c93811SAlasdair G Kergon 	if (!dm_target_is_valid(ti))
1602e4c93811SAlasdair G Kergon 		return -EIO;
1603e4c93811SAlasdair G Kergon 
16041c3b13e6SKent Overstreet 	len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
1605e4c93811SAlasdair G Kergon 
16061dd40c3eSMikulas Patocka 	__clone_and_map_data_bio(ci, ti, ci->sector, &len);
1607e4c93811SAlasdair G Kergon 
1608e4c93811SAlasdair G Kergon 	ci->sector += len;
1609e4c93811SAlasdair G Kergon 	ci->sector_count -= len;
1610e4c93811SAlasdair G Kergon 
1611e4c93811SAlasdair G Kergon 	return 0;
1612e4c93811SAlasdair G Kergon }
1613e4c93811SAlasdair G Kergon 
1614e4c93811SAlasdair G Kergon /*
161514fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
16161da177e4SLinus Torvalds  */
161783d5e5b0SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md,
161883d5e5b0SMikulas Patocka 				    struct dm_table *map, struct bio *bio)
16191da177e4SLinus Torvalds {
16201da177e4SLinus Torvalds 	struct clone_info ci;
1621512875bdSJun'ichi Nomura 	int error = 0;
16221da177e4SLinus Torvalds 
162383d5e5b0SMikulas Patocka 	if (unlikely(!map)) {
1624f0b9a450SMikulas Patocka 		bio_io_error(bio);
1625f0b9a450SMikulas Patocka 		return;
1626f0b9a450SMikulas Patocka 	}
1627692d0eb9SMikulas Patocka 
162883d5e5b0SMikulas Patocka 	ci.map = map;
16291da177e4SLinus Torvalds 	ci.md = md;
16301da177e4SLinus Torvalds 	ci.io = alloc_io(md);
16311da177e4SLinus Torvalds 	ci.io->error = 0;
16321da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
16331da177e4SLinus Torvalds 	ci.io->bio = bio;
16341da177e4SLinus Torvalds 	ci.io->md = md;
1635f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
16364f024f37SKent Overstreet 	ci.sector = bio->bi_iter.bi_sector;
16371da177e4SLinus Torvalds 
16383eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1639bd2a49b8SAlasdair G Kergon 
1640b372d360SMike Snitzer 	if (bio->bi_rw & REQ_FLUSH) {
1641b372d360SMike Snitzer 		ci.bio = &ci.md->flush_bio;
1642b372d360SMike Snitzer 		ci.sector_count = 0;
164314fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1644b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1645b372d360SMike Snitzer 	} else {
16466a8736d1STejun Heo 		ci.bio = bio;
1647f6fccb12SMilan Broz 		ci.sector_count = bio_sectors(bio);
1648512875bdSJun'ichi Nomura 		while (ci.sector_count && !error)
164914fe594dSAlasdair G Kergon 			error = __split_and_process_non_flush(&ci);
1650d87f4c14STejun Heo 	}
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 	/* drop the extra reference count */
1653512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
16549e4e5f87SMilan Broz }
16559e4e5f87SMilan Broz /*-----------------------------------------------------------------
16561da177e4SLinus Torvalds  * CRUD END
16571da177e4SLinus Torvalds  *---------------------------------------------------------------*/
16581da177e4SLinus Torvalds 
16591da177e4SLinus Torvalds static int dm_merge_bvec(struct request_queue *q,
16601da177e4SLinus Torvalds 			 struct bvec_merge_data *bvm,
1661f6fccb12SMilan Broz 			 struct bio_vec *biovec)
1662f6fccb12SMilan Broz {
1663f6fccb12SMilan Broz 	struct mapped_device *md = q->queuedata;
166483d5e5b0SMikulas Patocka 	struct dm_table *map = dm_get_live_table_fast(md);
1665f6fccb12SMilan Broz 	struct dm_target *ti;
16661c220c69SJoe Thornber 	sector_t max_sectors, max_size = 0;
1667f6fccb12SMilan Broz 
1668f6fccb12SMilan Broz 	if (unlikely(!map))
1669f6fccb12SMilan Broz 		goto out;
1670f6fccb12SMilan Broz 
1671f6fccb12SMilan Broz 	ti = dm_table_find_target(map, bvm->bi_sector);
1672f6fccb12SMilan Broz 	if (!dm_target_is_valid(ti))
167383d5e5b0SMikulas Patocka 		goto out;
1674f6fccb12SMilan Broz 
1675f6fccb12SMilan Broz 	/*
1676f6fccb12SMilan Broz 	 * Find maximum amount of I/O that won't need splitting
1677f6fccb12SMilan Broz 	 */
167856a67df7SMike Snitzer 	max_sectors = min(max_io_len(bvm->bi_sector, ti),
1679148e51baSMike Snitzer 			  (sector_t) queue_max_sectors(q));
1680f6fccb12SMilan Broz 	max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
16811c220c69SJoe Thornber 
16821c220c69SJoe Thornber 	/*
16831c220c69SJoe Thornber 	 * FIXME: this stop-gap fix _must_ be cleaned up (by passing a sector_t
16841c220c69SJoe Thornber 	 * to the targets' merge function since it holds sectors not bytes).
16851c220c69SJoe Thornber 	 * Just doing this as an interim fix for stable@ because the more
16861c220c69SJoe Thornber 	 * comprehensive cleanup of switching to sector_t will impact every
16871c220c69SJoe Thornber 	 * DM target that implements a ->merge hook.
16881c220c69SJoe Thornber 	 */
16891c220c69SJoe Thornber 	if (max_size > INT_MAX)
16901c220c69SJoe Thornber 		max_size = INT_MAX;
1691f6fccb12SMilan Broz 
1692f6fccb12SMilan Broz 	/*
1693f6fccb12SMilan Broz 	 * merge_bvec_fn() returns number of bytes
1694f6fccb12SMilan Broz 	 * it can accept at this offset
1695f6fccb12SMilan Broz 	 * max is precomputed maximal io size
1696f6fccb12SMilan Broz 	 */
1697f6fccb12SMilan Broz 	if (max_size && ti->type->merge)
16981c220c69SJoe Thornber 		max_size = ti->type->merge(ti, bvm, biovec, (int) max_size);
16998cbeb67aSMikulas Patocka 	/*
17008cbeb67aSMikulas Patocka 	 * If the target doesn't support merge method and some of the devices
1701148e51baSMike Snitzer 	 * provided their merge_bvec method (we know this by looking for the
1702148e51baSMike Snitzer 	 * max_hw_sectors that dm_set_device_limits may set), then we can't
1703148e51baSMike Snitzer 	 * allow bios with multiple vector entries.  So always set max_size
1704148e51baSMike Snitzer 	 * to 0, and the code below allows just one page.
17058cbeb67aSMikulas Patocka 	 */
17068cbeb67aSMikulas Patocka 	else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
17078cbeb67aSMikulas Patocka 		max_size = 0;
1708f6fccb12SMilan Broz 
17095037108aSMikulas Patocka out:
171083d5e5b0SMikulas Patocka 	dm_put_live_table_fast(md);
1711f6fccb12SMilan Broz 	/*
1712f6fccb12SMilan Broz 	 * Always allow an entire first page
1713f6fccb12SMilan Broz 	 */
1714f6fccb12SMilan Broz 	if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1715f6fccb12SMilan Broz 		max_size = biovec->bv_len;
1716f6fccb12SMilan Broz 
1717f6fccb12SMilan Broz 	return max_size;
1718f6fccb12SMilan Broz }
1719f6fccb12SMilan Broz 
17201da177e4SLinus Torvalds /*
17211da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
17221da177e4SLinus Torvalds  * dm_merge_bvec.
17231da177e4SLinus Torvalds  */
1724ff36ab34SMike Snitzer static void dm_make_request(struct request_queue *q, struct bio *bio)
17251da177e4SLinus Torvalds {
172612f03a49SKevin Corry 	int rw = bio_data_dir(bio);
17271da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
172883d5e5b0SMikulas Patocka 	int srcu_idx;
172983d5e5b0SMikulas Patocka 	struct dm_table *map;
17301da177e4SLinus Torvalds 
173183d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
17321da177e4SLinus Torvalds 
173318c0b223SGu Zheng 	generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
173412f03a49SKevin Corry 
17356a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
17366a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
173783d5e5b0SMikulas Patocka 		dm_put_live_table(md, srcu_idx);
17381da177e4SLinus Torvalds 
17396a8736d1STejun Heo 		if (bio_rw(bio) != READA)
174092c63902SMikulas Patocka 			queue_io(md, bio);
17416a8736d1STejun Heo 		else
17426a8736d1STejun Heo 			bio_io_error(bio);
17435a7bbad2SChristoph Hellwig 		return;
17441da177e4SLinus Torvalds 	}
17451da177e4SLinus Torvalds 
174683d5e5b0SMikulas Patocka 	__split_and_process_bio(md, map, bio);
174783d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
17485a7bbad2SChristoph Hellwig 	return;
1749cec47e3dSKiyoshi Ueda }
1750cec47e3dSKiyoshi Ueda 
1751fd2ed4d2SMikulas Patocka int dm_request_based(struct mapped_device *md)
1752cec47e3dSKiyoshi Ueda {
1753cec47e3dSKiyoshi Ueda 	return blk_queue_stackable(md->queue);
1754cec47e3dSKiyoshi Ueda }
1755cec47e3dSKiyoshi Ueda 
1756466d89a6SKeith Busch static void dm_dispatch_clone_request(struct request *clone, struct request *rq)
1757cec47e3dSKiyoshi Ueda {
1758cec47e3dSKiyoshi Ueda 	int r;
1759cec47e3dSKiyoshi Ueda 
1760466d89a6SKeith Busch 	if (blk_queue_io_stat(clone->q))
1761466d89a6SKeith Busch 		clone->cmd_flags |= REQ_IO_STAT;
1762cec47e3dSKiyoshi Ueda 
1763466d89a6SKeith Busch 	clone->start_time = jiffies;
1764466d89a6SKeith Busch 	r = blk_insert_cloned_request(clone->q, clone);
1765cec47e3dSKiyoshi Ueda 	if (r)
1766466d89a6SKeith Busch 		/* must complete clone in terms of original request */
1767cec47e3dSKiyoshi Ueda 		dm_complete_request(rq, r);
1768cec47e3dSKiyoshi Ueda }
1769cec47e3dSKiyoshi Ueda 
17705f1b670dSChristoph Hellwig static void setup_clone(struct request *clone, struct request *rq,
17715f1b670dSChristoph Hellwig 		        struct dm_rq_target_io *tio)
1772cec47e3dSKiyoshi Ueda {
17735f1b670dSChristoph Hellwig 	blk_rq_prep_clone(clone, rq);
1774cec47e3dSKiyoshi Ueda 	clone->end_io = end_clone_request;
1775cec47e3dSKiyoshi Ueda 	clone->end_io_data = tio;
17761ae49ea2SMike Snitzer 	tio->clone = clone;
1777cec47e3dSKiyoshi Ueda }
1778cec47e3dSKiyoshi Ueda 
17796facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md,
17801ae49ea2SMike Snitzer 				struct dm_rq_target_io *tio, gfp_t gfp_mask)
17816facdaffSKiyoshi Ueda {
178202233342SMike Snitzer 	/*
178302233342SMike Snitzer 	 * Do not allocate a clone if tio->clone was already set
178402233342SMike Snitzer 	 * (see: dm_mq_queue_rq).
178502233342SMike Snitzer 	 */
178602233342SMike Snitzer 	bool alloc_clone = !tio->clone;
178702233342SMike Snitzer 	struct request *clone;
17881ae49ea2SMike Snitzer 
178902233342SMike Snitzer 	if (alloc_clone) {
179002233342SMike Snitzer 		clone = alloc_clone_request(md, gfp_mask);
17911ae49ea2SMike Snitzer 		if (!clone)
17921ae49ea2SMike Snitzer 			return NULL;
179302233342SMike Snitzer 	} else
179402233342SMike Snitzer 		clone = tio->clone;
17951ae49ea2SMike Snitzer 
17961ae49ea2SMike Snitzer 	blk_rq_init(NULL, clone);
17975f1b670dSChristoph Hellwig 	setup_clone(clone, rq, tio);
17981ae49ea2SMike Snitzer 
17991ae49ea2SMike Snitzer 	return clone;
18001ae49ea2SMike Snitzer }
18011ae49ea2SMike Snitzer 
18022eb6e1e3SKeith Busch static void map_tio_request(struct kthread_work *work);
18032eb6e1e3SKeith Busch 
1804bfebd1cdSMike Snitzer static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
1805bfebd1cdSMike Snitzer 		     struct mapped_device *md)
1806bfebd1cdSMike Snitzer {
1807bfebd1cdSMike Snitzer 	tio->md = md;
1808bfebd1cdSMike Snitzer 	tio->ti = NULL;
1809bfebd1cdSMike Snitzer 	tio->clone = NULL;
1810bfebd1cdSMike Snitzer 	tio->orig = rq;
1811bfebd1cdSMike Snitzer 	tio->error = 0;
1812bfebd1cdSMike Snitzer 	memset(&tio->info, 0, sizeof(tio->info));
181302233342SMike Snitzer 	if (md->kworker_task)
1814bfebd1cdSMike Snitzer 		init_kthread_work(&tio->work, map_tio_request);
1815bfebd1cdSMike Snitzer }
1816bfebd1cdSMike Snitzer 
1817466d89a6SKeith Busch static struct dm_rq_target_io *prep_tio(struct request *rq,
1818466d89a6SKeith Busch 					struct mapped_device *md, gfp_t gfp_mask)
18196facdaffSKiyoshi Ueda {
18206facdaffSKiyoshi Ueda 	struct dm_rq_target_io *tio;
1821e5863d9aSMike Snitzer 	int srcu_idx;
1822e5863d9aSMike Snitzer 	struct dm_table *table;
18236facdaffSKiyoshi Ueda 
18246facdaffSKiyoshi Ueda 	tio = alloc_rq_tio(md, gfp_mask);
18256facdaffSKiyoshi Ueda 	if (!tio)
18266facdaffSKiyoshi Ueda 		return NULL;
18276facdaffSKiyoshi Ueda 
1828bfebd1cdSMike Snitzer 	init_tio(tio, rq, md);
18296facdaffSKiyoshi Ueda 
1830e5863d9aSMike Snitzer 	table = dm_get_live_table(md, &srcu_idx);
1831e5863d9aSMike Snitzer 	if (!dm_table_mq_request_based(table)) {
1832466d89a6SKeith Busch 		if (!clone_rq(rq, md, tio, gfp_mask)) {
1833e5863d9aSMike Snitzer 			dm_put_live_table(md, srcu_idx);
18346facdaffSKiyoshi Ueda 			free_rq_tio(tio);
18356facdaffSKiyoshi Ueda 			return NULL;
18366facdaffSKiyoshi Ueda 		}
1837e5863d9aSMike Snitzer 	}
1838e5863d9aSMike Snitzer 	dm_put_live_table(md, srcu_idx);
18396facdaffSKiyoshi Ueda 
1840466d89a6SKeith Busch 	return tio;
18416facdaffSKiyoshi Ueda }
18426facdaffSKiyoshi Ueda 
1843cec47e3dSKiyoshi Ueda /*
1844cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1845cec47e3dSKiyoshi Ueda  */
1846cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq)
1847cec47e3dSKiyoshi Ueda {
1848cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1849466d89a6SKeith Busch 	struct dm_rq_target_io *tio;
1850cec47e3dSKiyoshi Ueda 
1851cec47e3dSKiyoshi Ueda 	if (unlikely(rq->special)) {
1852cec47e3dSKiyoshi Ueda 		DMWARN("Already has something in rq->special.");
1853cec47e3dSKiyoshi Ueda 		return BLKPREP_KILL;
1854cec47e3dSKiyoshi Ueda 	}
1855cec47e3dSKiyoshi Ueda 
1856466d89a6SKeith Busch 	tio = prep_tio(rq, md, GFP_ATOMIC);
1857466d89a6SKeith Busch 	if (!tio)
1858cec47e3dSKiyoshi Ueda 		return BLKPREP_DEFER;
1859cec47e3dSKiyoshi Ueda 
1860466d89a6SKeith Busch 	rq->special = tio;
1861cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_DONTPREP;
1862cec47e3dSKiyoshi Ueda 
1863cec47e3dSKiyoshi Ueda 	return BLKPREP_OK;
1864cec47e3dSKiyoshi Ueda }
1865cec47e3dSKiyoshi Ueda 
18669eef87daSKiyoshi Ueda /*
18679eef87daSKiyoshi Ueda  * Returns:
1868e5863d9aSMike Snitzer  * 0                : the request has been processed
1869e5863d9aSMike Snitzer  * DM_MAPIO_REQUEUE : the original request needs to be requeued
1870e5863d9aSMike Snitzer  * < 0              : the request was completed due to failure
18719eef87daSKiyoshi Ueda  */
1872bfebd1cdSMike Snitzer static int map_request(struct dm_rq_target_io *tio, struct request *rq,
1873cec47e3dSKiyoshi Ueda 		       struct mapped_device *md)
1874cec47e3dSKiyoshi Ueda {
1875e5863d9aSMike Snitzer 	int r;
1876bfebd1cdSMike Snitzer 	struct dm_target *ti = tio->ti;
1877e5863d9aSMike Snitzer 	struct request *clone = NULL;
1878cec47e3dSKiyoshi Ueda 
1879e5863d9aSMike Snitzer 	if (tio->clone) {
1880e5863d9aSMike Snitzer 		clone = tio->clone;
1881cec47e3dSKiyoshi Ueda 		r = ti->type->map_rq(ti, clone, &tio->info);
1882e5863d9aSMike Snitzer 	} else {
1883e5863d9aSMike Snitzer 		r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
1884e5863d9aSMike Snitzer 		if (r < 0) {
1885e5863d9aSMike Snitzer 			/* The target wants to complete the I/O */
1886e5863d9aSMike Snitzer 			dm_kill_unmapped_request(rq, r);
1887e5863d9aSMike Snitzer 			return r;
1888e5863d9aSMike Snitzer 		}
18893a140755SJunichi Nomura 		if (r != DM_MAPIO_REMAPPED)
18903a140755SJunichi Nomura 			return r;
18915f1b670dSChristoph Hellwig 		setup_clone(clone, rq, tio);
1892e5863d9aSMike Snitzer 	}
1893e5863d9aSMike Snitzer 
1894cec47e3dSKiyoshi Ueda 	switch (r) {
1895cec47e3dSKiyoshi Ueda 	case DM_MAPIO_SUBMITTED:
1896cec47e3dSKiyoshi Ueda 		/* The target has taken the I/O to submit by itself later */
1897cec47e3dSKiyoshi Ueda 		break;
1898cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REMAPPED:
1899cec47e3dSKiyoshi Ueda 		/* The target has remapped the I/O so dispatch it */
19006db4ccd6SJun'ichi Nomura 		trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1901466d89a6SKeith Busch 				     blk_rq_pos(rq));
1902466d89a6SKeith Busch 		dm_dispatch_clone_request(clone, rq);
1903cec47e3dSKiyoshi Ueda 		break;
1904cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REQUEUE:
1905cec47e3dSKiyoshi Ueda 		/* The target wants to requeue the I/O */
19062d76fff1SMike Snitzer 		dm_requeue_original_request(md, tio->orig);
1907cec47e3dSKiyoshi Ueda 		break;
1908cec47e3dSKiyoshi Ueda 	default:
1909cec47e3dSKiyoshi Ueda 		if (r > 0) {
1910cec47e3dSKiyoshi Ueda 			DMWARN("unimplemented target map return value: %d", r);
1911cec47e3dSKiyoshi Ueda 			BUG();
1912cec47e3dSKiyoshi Ueda 		}
1913cec47e3dSKiyoshi Ueda 
1914cec47e3dSKiyoshi Ueda 		/* The target wants to complete the I/O */
1915466d89a6SKeith Busch 		dm_kill_unmapped_request(rq, r);
1916e5863d9aSMike Snitzer 		return r;
1917cec47e3dSKiyoshi Ueda 	}
19189eef87daSKiyoshi Ueda 
1919e5863d9aSMike Snitzer 	return 0;
1920cec47e3dSKiyoshi Ueda }
1921cec47e3dSKiyoshi Ueda 
19222eb6e1e3SKeith Busch static void map_tio_request(struct kthread_work *work)
1923ba1cbad9SMike Snitzer {
19242eb6e1e3SKeith Busch 	struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work);
1925e5863d9aSMike Snitzer 	struct request *rq = tio->orig;
1926e5863d9aSMike Snitzer 	struct mapped_device *md = tio->md;
1927ba1cbad9SMike Snitzer 
1928bfebd1cdSMike Snitzer 	if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE)
19292d76fff1SMike Snitzer 		dm_requeue_original_request(md, rq);
19302eb6e1e3SKeith Busch }
19312eb6e1e3SKeith Busch 
1932466d89a6SKeith Busch static void dm_start_request(struct mapped_device *md, struct request *orig)
1933ba1cbad9SMike Snitzer {
1934bfebd1cdSMike Snitzer 	if (!orig->q->mq_ops)
1935ba1cbad9SMike Snitzer 		blk_start_request(orig);
1936bfebd1cdSMike Snitzer 	else
1937bfebd1cdSMike Snitzer 		blk_mq_start_request(orig);
1938466d89a6SKeith Busch 	atomic_inc(&md->pending[rq_data_dir(orig)]);
1939ba1cbad9SMike Snitzer 
19400ce65797SMike Snitzer 	if (md->seq_rq_merge_deadline_usecs) {
1941de3ec86dSMike Snitzer 		md->last_rq_pos = rq_end_sector(orig);
1942de3ec86dSMike Snitzer 		md->last_rq_rw = rq_data_dir(orig);
19430ce65797SMike Snitzer 		md->last_rq_start_time = ktime_get();
19440ce65797SMike Snitzer 	}
1945de3ec86dSMike Snitzer 
1946ba1cbad9SMike Snitzer 	/*
1947ba1cbad9SMike Snitzer 	 * Hold the md reference here for the in-flight I/O.
1948ba1cbad9SMike Snitzer 	 * We can't rely on the reference count by device opener,
1949ba1cbad9SMike Snitzer 	 * because the device may be closed during the request completion
1950ba1cbad9SMike Snitzer 	 * when all bios are completed.
1951ba1cbad9SMike Snitzer 	 * See the comment in rq_completed() too.
1952ba1cbad9SMike Snitzer 	 */
1953ba1cbad9SMike Snitzer 	dm_get(md);
1954ba1cbad9SMike Snitzer }
1955ba1cbad9SMike Snitzer 
19560ce65797SMike Snitzer #define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000
19570ce65797SMike Snitzer 
19580ce65797SMike Snitzer ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
19590ce65797SMike Snitzer {
19600ce65797SMike Snitzer 	return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs);
19610ce65797SMike Snitzer }
19620ce65797SMike Snitzer 
19630ce65797SMike Snitzer ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
19640ce65797SMike Snitzer 						     const char *buf, size_t count)
19650ce65797SMike Snitzer {
19660ce65797SMike Snitzer 	unsigned deadline;
19670ce65797SMike Snitzer 
196817e149b8SMike Snitzer 	if (!dm_request_based(md) || md->use_blk_mq)
19690ce65797SMike Snitzer 		return count;
19700ce65797SMike Snitzer 
19710ce65797SMike Snitzer 	if (kstrtouint(buf, 10, &deadline))
19720ce65797SMike Snitzer 		return -EINVAL;
19730ce65797SMike Snitzer 
19740ce65797SMike Snitzer 	if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS)
19750ce65797SMike Snitzer 		deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS;
19760ce65797SMike Snitzer 
19770ce65797SMike Snitzer 	md->seq_rq_merge_deadline_usecs = deadline;
19780ce65797SMike Snitzer 
19790ce65797SMike Snitzer 	return count;
19800ce65797SMike Snitzer }
19810ce65797SMike Snitzer 
19820ce65797SMike Snitzer static bool dm_request_peeked_before_merge_deadline(struct mapped_device *md)
19830ce65797SMike Snitzer {
19840ce65797SMike Snitzer 	ktime_t kt_deadline;
19850ce65797SMike Snitzer 
19860ce65797SMike Snitzer 	if (!md->seq_rq_merge_deadline_usecs)
19870ce65797SMike Snitzer 		return false;
19880ce65797SMike Snitzer 
19890ce65797SMike Snitzer 	kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC);
19900ce65797SMike Snitzer 	kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline);
19910ce65797SMike Snitzer 
19920ce65797SMike Snitzer 	return !ktime_after(ktime_get(), kt_deadline);
19930ce65797SMike Snitzer }
19940ce65797SMike Snitzer 
1995cec47e3dSKiyoshi Ueda /*
1996cec47e3dSKiyoshi Ueda  * q->request_fn for request-based dm.
1997cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1998cec47e3dSKiyoshi Ueda  */
1999cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q)
2000cec47e3dSKiyoshi Ueda {
2001cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
200283d5e5b0SMikulas Patocka 	int srcu_idx;
200383d5e5b0SMikulas Patocka 	struct dm_table *map = dm_get_live_table(md, &srcu_idx);
2004cec47e3dSKiyoshi Ueda 	struct dm_target *ti;
2005466d89a6SKeith Busch 	struct request *rq;
20062eb6e1e3SKeith Busch 	struct dm_rq_target_io *tio;
200729e4013dSTejun Heo 	sector_t pos;
2008cec47e3dSKiyoshi Ueda 
2009cec47e3dSKiyoshi Ueda 	/*
2010b4324feeSKiyoshi Ueda 	 * For suspend, check blk_queue_stopped() and increment
2011b4324feeSKiyoshi Ueda 	 * ->pending within a single queue_lock not to increment the
2012b4324feeSKiyoshi Ueda 	 * number of in-flight I/Os after the queue is stopped in
2013b4324feeSKiyoshi Ueda 	 * dm_suspend().
2014cec47e3dSKiyoshi Ueda 	 */
20157eaceaccSJens Axboe 	while (!blk_queue_stopped(q)) {
2016cec47e3dSKiyoshi Ueda 		rq = blk_peek_request(q);
2017cec47e3dSKiyoshi Ueda 		if (!rq)
20189d1deb83SMike Snitzer 			goto out;
2019cec47e3dSKiyoshi Ueda 
202029e4013dSTejun Heo 		/* always use block 0 to find the target for flushes for now */
202129e4013dSTejun Heo 		pos = 0;
202229e4013dSTejun Heo 		if (!(rq->cmd_flags & REQ_FLUSH))
202329e4013dSTejun Heo 			pos = blk_rq_pos(rq);
2024d0bcb878SKiyoshi Ueda 
202529e4013dSTejun Heo 		ti = dm_table_find_target(map, pos);
2026ba1cbad9SMike Snitzer 		if (!dm_target_is_valid(ti)) {
2027ba1cbad9SMike Snitzer 			/*
2028466d89a6SKeith Busch 			 * Must perform setup, that rq_completed() requires,
2029ba1cbad9SMike Snitzer 			 * before calling dm_kill_unmapped_request
2030ba1cbad9SMike Snitzer 			 */
2031ba1cbad9SMike Snitzer 			DMERR_LIMIT("request attempted access beyond the end of device");
2032466d89a6SKeith Busch 			dm_start_request(md, rq);
2033466d89a6SKeith Busch 			dm_kill_unmapped_request(rq, -EIO);
2034ba1cbad9SMike Snitzer 			continue;
2035ba1cbad9SMike Snitzer 		}
203629e4013dSTejun Heo 
20370ce65797SMike Snitzer 		if (dm_request_peeked_before_merge_deadline(md) &&
20380ce65797SMike Snitzer 		    md_in_flight(md) && rq->bio && rq->bio->bi_vcnt == 1 &&
2039de3ec86dSMike Snitzer 		    md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq))
2040de3ec86dSMike Snitzer 			goto delay_and_out;
2041de3ec86dSMike Snitzer 
2042cec47e3dSKiyoshi Ueda 		if (ti->type->busy && ti->type->busy(ti))
20437eaceaccSJens Axboe 			goto delay_and_out;
2044cec47e3dSKiyoshi Ueda 
2045466d89a6SKeith Busch 		dm_start_request(md, rq);
2046b4324feeSKiyoshi Ueda 
2047bfebd1cdSMike Snitzer 		tio = tio_from_request(rq);
20482eb6e1e3SKeith Busch 		/* Establish tio->ti before queuing work (map_tio_request) */
20492eb6e1e3SKeith Busch 		tio->ti = ti;
20502eb6e1e3SKeith Busch 		queue_kthread_work(&md->kworker, &tio->work);
2051052189a2SKiyoshi Ueda 		BUG_ON(!irqs_disabled());
2052cec47e3dSKiyoshi Ueda 	}
2053cec47e3dSKiyoshi Ueda 
2054cec47e3dSKiyoshi Ueda 	goto out;
2055cec47e3dSKiyoshi Ueda 
20567eaceaccSJens Axboe delay_and_out:
2057d548b34bSMike Snitzer 	blk_delay_queue(q, HZ / 100);
2058cec47e3dSKiyoshi Ueda out:
205983d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
2060cec47e3dSKiyoshi Ueda }
2061cec47e3dSKiyoshi Ueda 
20621da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
20631da177e4SLinus Torvalds {
20648a57dfc6SChandra Seetharaman 	int r = bdi_bits;
20658a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
20668a57dfc6SChandra Seetharaman 	struct dm_table *map;
20671da177e4SLinus Torvalds 
20681eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
206983d5e5b0SMikulas Patocka 		map = dm_get_live_table_fast(md);
20708a57dfc6SChandra Seetharaman 		if (map) {
2071cec47e3dSKiyoshi Ueda 			/*
2072cec47e3dSKiyoshi Ueda 			 * Request-based dm cares about only own queue for
2073cec47e3dSKiyoshi Ueda 			 * the query about congestion status of request_queue
2074cec47e3dSKiyoshi Ueda 			 */
2075cec47e3dSKiyoshi Ueda 			if (dm_request_based(md))
2076cec47e3dSKiyoshi Ueda 				r = md->queue->backing_dev_info.state &
2077cec47e3dSKiyoshi Ueda 				    bdi_bits;
2078cec47e3dSKiyoshi Ueda 			else
20791da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
20808a57dfc6SChandra Seetharaman 		}
208183d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
20828a57dfc6SChandra Seetharaman 	}
20838a57dfc6SChandra Seetharaman 
20841da177e4SLinus Torvalds 	return r;
20851da177e4SLinus Torvalds }
20861da177e4SLinus Torvalds 
20871da177e4SLinus Torvalds /*-----------------------------------------------------------------
20881da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
20891da177e4SLinus Torvalds  *---------------------------------------------------------------*/
20902b06cfffSAlasdair G Kergon static void free_minor(int minor)
20911da177e4SLinus Torvalds {
2092f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
20931da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
2094f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
20951da177e4SLinus Torvalds }
20961da177e4SLinus Torvalds 
20971da177e4SLinus Torvalds /*
20981da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
20991da177e4SLinus Torvalds  */
2100cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
21011da177e4SLinus Torvalds {
2102c9d76be6STejun Heo 	int r;
21031da177e4SLinus Torvalds 
21041da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
21051da177e4SLinus Torvalds 		return -EINVAL;
21061da177e4SLinus Torvalds 
2107c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
2108f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
21091da177e4SLinus Torvalds 
2110c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
21111da177e4SLinus Torvalds 
2112f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2113c9d76be6STejun Heo 	idr_preload_end();
2114c9d76be6STejun Heo 	if (r < 0)
2115c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
2116c9d76be6STejun Heo 	return 0;
21171da177e4SLinus Torvalds }
21181da177e4SLinus Torvalds 
2119cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
21201da177e4SLinus Torvalds {
2121c9d76be6STejun Heo 	int r;
21221da177e4SLinus Torvalds 
2123c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
2124f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
21251da177e4SLinus Torvalds 
2126c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
21271da177e4SLinus Torvalds 
2128f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2129c9d76be6STejun Heo 	idr_preload_end();
2130c9d76be6STejun Heo 	if (r < 0)
21311da177e4SLinus Torvalds 		return r;
2132c9d76be6STejun Heo 	*minor = r;
2133c9d76be6STejun Heo 	return 0;
21341da177e4SLinus Torvalds }
21351da177e4SLinus Torvalds 
213683d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
21371da177e4SLinus Torvalds 
213853d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
213953d5914fSMikulas Patocka 
21404a0b4ddfSMike Snitzer static void dm_init_md_queue(struct mapped_device *md)
21414a0b4ddfSMike Snitzer {
21424a0b4ddfSMike Snitzer 	/*
21434a0b4ddfSMike Snitzer 	 * Request-based dm devices cannot be stacked on top of bio-based dm
2144bfebd1cdSMike Snitzer 	 * devices.  The type of this dm device may not have been decided yet.
21454a0b4ddfSMike Snitzer 	 * The type is decided at the first table loading time.
21464a0b4ddfSMike Snitzer 	 * To prevent problematic device stacking, clear the queue flag
21474a0b4ddfSMike Snitzer 	 * for request stacking support until then.
21484a0b4ddfSMike Snitzer 	 *
21494a0b4ddfSMike Snitzer 	 * This queue is new, so no concurrency on the queue_flags.
21504a0b4ddfSMike Snitzer 	 */
21514a0b4ddfSMike Snitzer 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
2152bfebd1cdSMike Snitzer }
21534a0b4ddfSMike Snitzer 
2154bfebd1cdSMike Snitzer static void dm_init_old_md_queue(struct mapped_device *md)
2155bfebd1cdSMike Snitzer {
215617e149b8SMike Snitzer 	md->use_blk_mq = false;
2157bfebd1cdSMike Snitzer 	dm_init_md_queue(md);
2158bfebd1cdSMike Snitzer 
2159bfebd1cdSMike Snitzer 	/*
2160bfebd1cdSMike Snitzer 	 * Initialize aspects of queue that aren't relevant for blk-mq
2161bfebd1cdSMike Snitzer 	 */
21624a0b4ddfSMike Snitzer 	md->queue->queuedata = md;
21634a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
21644a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_data = md;
2165ff36ab34SMike Snitzer 
21664a0b4ddfSMike Snitzer 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
21674a0b4ddfSMike Snitzer }
21684a0b4ddfSMike Snitzer 
2169*0f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md)
2170*0f20972fSMike Snitzer {
2171*0f20972fSMike Snitzer 	cleanup_srcu_struct(&md->io_barrier);
2172*0f20972fSMike Snitzer 
2173*0f20972fSMike Snitzer 	if (md->wq)
2174*0f20972fSMike Snitzer 		destroy_workqueue(md->wq);
2175*0f20972fSMike Snitzer 	if (md->kworker_task)
2176*0f20972fSMike Snitzer 		kthread_stop(md->kworker_task);
2177*0f20972fSMike Snitzer 	if (md->io_pool)
2178*0f20972fSMike Snitzer 		mempool_destroy(md->io_pool);
2179*0f20972fSMike Snitzer 	if (md->rq_pool)
2180*0f20972fSMike Snitzer 		mempool_destroy(md->rq_pool);
2181*0f20972fSMike Snitzer 	if (md->bs)
2182*0f20972fSMike Snitzer 		bioset_free(md->bs);
2183*0f20972fSMike Snitzer 
2184*0f20972fSMike Snitzer 	if (md->disk) {
2185*0f20972fSMike Snitzer 		spin_lock(&_minor_lock);
2186*0f20972fSMike Snitzer 		md->disk->private_data = NULL;
2187*0f20972fSMike Snitzer 		spin_unlock(&_minor_lock);
2188*0f20972fSMike Snitzer 		if (blk_get_integrity(md->disk))
2189*0f20972fSMike Snitzer 			blk_integrity_unregister(md->disk);
2190*0f20972fSMike Snitzer 		del_gendisk(md->disk);
2191*0f20972fSMike Snitzer 		put_disk(md->disk);
2192*0f20972fSMike Snitzer 	}
2193*0f20972fSMike Snitzer 
2194*0f20972fSMike Snitzer 	if (md->queue)
2195*0f20972fSMike Snitzer 		blk_cleanup_queue(md->queue);
2196*0f20972fSMike Snitzer 
2197*0f20972fSMike Snitzer 	if (md->bdev) {
2198*0f20972fSMike Snitzer 		bdput(md->bdev);
2199*0f20972fSMike Snitzer 		md->bdev = NULL;
2200*0f20972fSMike Snitzer 	}
2201*0f20972fSMike Snitzer }
2202*0f20972fSMike Snitzer 
22031da177e4SLinus Torvalds /*
22041da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
22051da177e4SLinus Torvalds  */
22062b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
22071da177e4SLinus Torvalds {
22081da177e4SLinus Torvalds 	int r;
2209cf13ab8eSFrederik Deweerdt 	struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
2210ba61fdd1SJeff Mahoney 	void *old_md;
22111da177e4SLinus Torvalds 
22121da177e4SLinus Torvalds 	if (!md) {
22131da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
22141da177e4SLinus Torvalds 		return NULL;
22151da177e4SLinus Torvalds 	}
22161da177e4SLinus Torvalds 
221710da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
22186ed7ade8SMilan Broz 		goto bad_module_get;
221910da4f79SJeff Mahoney 
22201da177e4SLinus Torvalds 	/* get a minor number for the dev */
22212b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
2222cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
22232b06cfffSAlasdair G Kergon 	else
2224cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
22251da177e4SLinus Torvalds 	if (r < 0)
22266ed7ade8SMilan Broz 		goto bad_minor;
22271da177e4SLinus Torvalds 
222883d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
222983d5e5b0SMikulas Patocka 	if (r < 0)
223083d5e5b0SMikulas Patocka 		goto bad_io_barrier;
223183d5e5b0SMikulas Patocka 
223217e149b8SMike Snitzer 	md->use_blk_mq = use_blk_mq;
2233a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
2234e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
2235a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
223686f1152bSBenjamin Marzinski 	mutex_init(&md->table_devices_lock);
2237022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
22381da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
22395c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
22401da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
22417a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
22427a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
224386f1152bSBenjamin Marzinski 	INIT_LIST_HEAD(&md->table_devices);
22447a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
22451da177e4SLinus Torvalds 
22464a0b4ddfSMike Snitzer 	md->queue = blk_alloc_queue(GFP_KERNEL);
22471da177e4SLinus Torvalds 	if (!md->queue)
2248*0f20972fSMike Snitzer 		goto bad;
22491da177e4SLinus Torvalds 
22504a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
22519faf400fSStefan Bader 
22521da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
22531da177e4SLinus Torvalds 	if (!md->disk)
2254*0f20972fSMike Snitzer 		goto bad;
22551da177e4SLinus Torvalds 
2256316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
2257316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
2258f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
225953d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
2260f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
22612995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
22622eb6e1e3SKeith Busch 	md->kworker_task = NULL;
2263f0b04115SJeff Mahoney 
22641da177e4SLinus Torvalds 	md->disk->major = _major;
22651da177e4SLinus Torvalds 	md->disk->first_minor = minor;
22661da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
22671da177e4SLinus Torvalds 	md->disk->queue = md->queue;
22681da177e4SLinus Torvalds 	md->disk->private_data = md;
22691da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
22701da177e4SLinus Torvalds 	add_disk(md->disk);
22717e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
22721da177e4SLinus Torvalds 
2273670368a8STejun Heo 	md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
2274304f3f6aSMilan Broz 	if (!md->wq)
2275*0f20972fSMike Snitzer 		goto bad;
2276304f3f6aSMilan Broz 
227732a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
227832a926daSMikulas Patocka 	if (!md->bdev)
2279*0f20972fSMike Snitzer 		goto bad;
228032a926daSMikulas Patocka 
22816a8736d1STejun Heo 	bio_init(&md->flush_bio);
22826a8736d1STejun Heo 	md->flush_bio.bi_bdev = md->bdev;
22836a8736d1STejun Heo 	md->flush_bio.bi_rw = WRITE_FLUSH;
22846a8736d1STejun Heo 
2285fd2ed4d2SMikulas Patocka 	dm_stats_init(&md->stats);
2286fd2ed4d2SMikulas Patocka 
2287ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
2288f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
2289ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
2290f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2291ba61fdd1SJeff Mahoney 
2292ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
2293ba61fdd1SJeff Mahoney 
22941da177e4SLinus Torvalds 	return md;
22951da177e4SLinus Torvalds 
2296*0f20972fSMike Snitzer bad:
2297*0f20972fSMike Snitzer 	cleanup_mapped_device(md);
229883d5e5b0SMikulas Patocka bad_io_barrier:
22991da177e4SLinus Torvalds 	free_minor(minor);
23006ed7ade8SMilan Broz bad_minor:
230110da4f79SJeff Mahoney 	module_put(THIS_MODULE);
23026ed7ade8SMilan Broz bad_module_get:
23031da177e4SLinus Torvalds 	kfree(md);
23041da177e4SLinus Torvalds 	return NULL;
23051da177e4SLinus Torvalds }
23061da177e4SLinus Torvalds 
2307ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
2308ae9da83fSJun'ichi Nomura 
23091da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
23101da177e4SLinus Torvalds {
2311f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
231263d94e48SJun'ichi Nomura 
2313ae9da83fSJun'ichi Nomura 	unlock_fs(md);
23142eb6e1e3SKeith Busch 
2315*0f20972fSMike Snitzer 	cleanup_mapped_device(md);
231617e149b8SMike Snitzer 	if (md->use_blk_mq)
2317bfebd1cdSMike Snitzer 		blk_mq_free_tag_set(&md->tag_set);
2318*0f20972fSMike Snitzer 
2319*0f20972fSMike Snitzer 	free_table_devices(&md->table_devices);
2320*0f20972fSMike Snitzer 	dm_stats_cleanup(&md->stats);
232163a4f065SMike Snitzer 	free_minor(minor);
232263a4f065SMike Snitzer 
232310da4f79SJeff Mahoney 	module_put(THIS_MODULE);
23241da177e4SLinus Torvalds 	kfree(md);
23251da177e4SLinus Torvalds }
23261da177e4SLinus Torvalds 
2327cbc4e3c1SMike Snitzer static unsigned filter_md_type(unsigned type, struct mapped_device *md)
2328cbc4e3c1SMike Snitzer {
2329cbc4e3c1SMike Snitzer 	if (type == DM_TYPE_BIO_BASED)
2330cbc4e3c1SMike Snitzer 		return type;
2331cbc4e3c1SMike Snitzer 
2332cbc4e3c1SMike Snitzer 	return !md->use_blk_mq ? DM_TYPE_REQUEST_BASED : DM_TYPE_MQ_REQUEST_BASED;
2333cbc4e3c1SMike Snitzer }
2334cbc4e3c1SMike Snitzer 
2335e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2336e6ee8c0bSKiyoshi Ueda {
2337c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
2338e6ee8c0bSKiyoshi Ueda 
2339cbc4e3c1SMike Snitzer 	switch (filter_md_type(dm_table_get_type(t), md)) {
2340cbc4e3c1SMike Snitzer 	case DM_TYPE_BIO_BASED:
2341cbc4e3c1SMike Snitzer 		if (md->bs && md->io_pool) {
2342c0820cf5SMikulas Patocka 			/*
2343cbc4e3c1SMike Snitzer 			 * This bio-based md already has necessary mempools.
234416245bdcSJun'ichi Nomura 			 * Reload bioset because front_pad may have changed
234516245bdcSJun'ichi Nomura 			 * because a different table was loaded.
2346c0820cf5SMikulas Patocka 			 */
2347c0820cf5SMikulas Patocka 			bioset_free(md->bs);
2348c0820cf5SMikulas Patocka 			md->bs = p->bs;
2349c0820cf5SMikulas Patocka 			p->bs = NULL;
2350e6ee8c0bSKiyoshi Ueda 			goto out;
2351c0820cf5SMikulas Patocka 		}
2352cbc4e3c1SMike Snitzer 		break;
2353cbc4e3c1SMike Snitzer 	case DM_TYPE_REQUEST_BASED:
2354cbc4e3c1SMike Snitzer 		if (md->rq_pool && md->io_pool)
2355cbc4e3c1SMike Snitzer 			/*
2356cbc4e3c1SMike Snitzer 			 * This request-based md already has necessary mempools.
2357cbc4e3c1SMike Snitzer 			 */
2358cbc4e3c1SMike Snitzer 			goto out;
2359cbc4e3c1SMike Snitzer 		break;
2360cbc4e3c1SMike Snitzer 	case DM_TYPE_MQ_REQUEST_BASED:
2361cbc4e3c1SMike Snitzer 		BUG_ON(p); /* No mempools needed */
2362cbc4e3c1SMike Snitzer 		return;
2363cbc4e3c1SMike Snitzer 	}
2364cbc4e3c1SMike Snitzer 
2365cbc4e3c1SMike Snitzer 	BUG_ON(!p || md->io_pool || md->rq_pool || md->bs);
2366e6ee8c0bSKiyoshi Ueda 
2367e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
2368e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
23691ae49ea2SMike Snitzer 	md->rq_pool = p->rq_pool;
23701ae49ea2SMike Snitzer 	p->rq_pool = NULL;
2371e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
2372e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
2373e6ee8c0bSKiyoshi Ueda out:
237402233342SMike Snitzer 	/* mempool bind completed, no longer need any mempools in the table */
2375e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
2376e6ee8c0bSKiyoshi Ueda }
2377e6ee8c0bSKiyoshi Ueda 
23781da177e4SLinus Torvalds /*
23791da177e4SLinus Torvalds  * Bind a table to the device.
23801da177e4SLinus Torvalds  */
23811da177e4SLinus Torvalds static void event_callback(void *context)
23821da177e4SLinus Torvalds {
23837a8c3d3bSMike Anderson 	unsigned long flags;
23847a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
23851da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
23861da177e4SLinus Torvalds 
23877a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
23887a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
23897a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
23907a8c3d3bSMike Anderson 
2391ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
23927a8c3d3bSMike Anderson 
23931da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
23941da177e4SLinus Torvalds 	wake_up(&md->eventq);
23951da177e4SLinus Torvalds }
23961da177e4SLinus Torvalds 
2397c217649bSMike Snitzer /*
2398c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
2399c217649bSMike Snitzer  */
24004e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
24011da177e4SLinus Torvalds {
24024e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
24031da177e4SLinus Torvalds 
2404db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
24051da177e4SLinus Torvalds }
24061da177e4SLinus Torvalds 
2407042d2a9bSAlasdair G Kergon /*
2408d5b9dd04SMikulas Patocka  * Return 1 if the queue has a compulsory merge_bvec_fn function.
2409d5b9dd04SMikulas Patocka  *
2410d5b9dd04SMikulas Patocka  * If this function returns 0, then the device is either a non-dm
2411d5b9dd04SMikulas Patocka  * device without a merge_bvec_fn, or it is a dm device that is
2412d5b9dd04SMikulas Patocka  * able to split any bios it receives that are too big.
2413d5b9dd04SMikulas Patocka  */
2414d5b9dd04SMikulas Patocka int dm_queue_merge_is_compulsory(struct request_queue *q)
2415d5b9dd04SMikulas Patocka {
2416d5b9dd04SMikulas Patocka 	struct mapped_device *dev_md;
2417d5b9dd04SMikulas Patocka 
2418d5b9dd04SMikulas Patocka 	if (!q->merge_bvec_fn)
2419d5b9dd04SMikulas Patocka 		return 0;
2420d5b9dd04SMikulas Patocka 
2421ff36ab34SMike Snitzer 	if (q->make_request_fn == dm_make_request) {
2422d5b9dd04SMikulas Patocka 		dev_md = q->queuedata;
2423d5b9dd04SMikulas Patocka 		if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2424d5b9dd04SMikulas Patocka 			return 0;
2425d5b9dd04SMikulas Patocka 	}
2426d5b9dd04SMikulas Patocka 
2427d5b9dd04SMikulas Patocka 	return 1;
2428d5b9dd04SMikulas Patocka }
2429d5b9dd04SMikulas Patocka 
2430d5b9dd04SMikulas Patocka static int dm_device_merge_is_compulsory(struct dm_target *ti,
2431d5b9dd04SMikulas Patocka 					 struct dm_dev *dev, sector_t start,
2432d5b9dd04SMikulas Patocka 					 sector_t len, void *data)
2433d5b9dd04SMikulas Patocka {
2434d5b9dd04SMikulas Patocka 	struct block_device *bdev = dev->bdev;
2435d5b9dd04SMikulas Patocka 	struct request_queue *q = bdev_get_queue(bdev);
2436d5b9dd04SMikulas Patocka 
2437d5b9dd04SMikulas Patocka 	return dm_queue_merge_is_compulsory(q);
2438d5b9dd04SMikulas Patocka }
2439d5b9dd04SMikulas Patocka 
2440d5b9dd04SMikulas Patocka /*
2441d5b9dd04SMikulas Patocka  * Return 1 if it is acceptable to ignore merge_bvec_fn based
2442d5b9dd04SMikulas Patocka  * on the properties of the underlying devices.
2443d5b9dd04SMikulas Patocka  */
2444d5b9dd04SMikulas Patocka static int dm_table_merge_is_optional(struct dm_table *table)
2445d5b9dd04SMikulas Patocka {
2446d5b9dd04SMikulas Patocka 	unsigned i = 0;
2447d5b9dd04SMikulas Patocka 	struct dm_target *ti;
2448d5b9dd04SMikulas Patocka 
2449d5b9dd04SMikulas Patocka 	while (i < dm_table_get_num_targets(table)) {
2450d5b9dd04SMikulas Patocka 		ti = dm_table_get_target(table, i++);
2451d5b9dd04SMikulas Patocka 
2452d5b9dd04SMikulas Patocka 		if (ti->type->iterate_devices &&
2453d5b9dd04SMikulas Patocka 		    ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2454d5b9dd04SMikulas Patocka 			return 0;
2455d5b9dd04SMikulas Patocka 	}
2456d5b9dd04SMikulas Patocka 
2457d5b9dd04SMikulas Patocka 	return 1;
2458d5b9dd04SMikulas Patocka }
2459d5b9dd04SMikulas Patocka 
2460d5b9dd04SMikulas Patocka /*
2461042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2462042d2a9bSAlasdair G Kergon  */
2463042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2464754c5fc7SMike Snitzer 			       struct queue_limits *limits)
24651da177e4SLinus Torvalds {
2466042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
2467165125e1SJens Axboe 	struct request_queue *q = md->queue;
24681da177e4SLinus Torvalds 	sector_t size;
2469d5b9dd04SMikulas Patocka 	int merge_is_optional;
24701da177e4SLinus Torvalds 
24711da177e4SLinus Torvalds 	size = dm_table_get_size(t);
24723ac51e74SDarrick J. Wong 
24733ac51e74SDarrick J. Wong 	/*
24743ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
24753ac51e74SDarrick J. Wong 	 */
2476fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
24773ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
24783ac51e74SDarrick J. Wong 
24794e90188bSAlasdair G Kergon 	__set_size(md, size);
24801da177e4SLinus Torvalds 
2481cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
24822ca3310eSAlasdair G Kergon 
2483e6ee8c0bSKiyoshi Ueda 	/*
2484e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
2485e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
2486e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
2487e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
2488e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
2489e6ee8c0bSKiyoshi Ueda 	 */
2490bfebd1cdSMike Snitzer 	if (dm_table_request_based(t))
2491e6ee8c0bSKiyoshi Ueda 		stop_queue(q);
2492e6ee8c0bSKiyoshi Ueda 
2493e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
2494e6ee8c0bSKiyoshi Ueda 
2495d5b9dd04SMikulas Patocka 	merge_is_optional = dm_table_merge_is_optional(t);
2496d5b9dd04SMikulas Patocka 
2497a12f5d48SEric Dumazet 	old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
249883d5e5b0SMikulas Patocka 	rcu_assign_pointer(md->map, t);
249936a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
250036a0456fSAlasdair G Kergon 
2501754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
2502d5b9dd04SMikulas Patocka 	if (merge_is_optional)
2503d5b9dd04SMikulas Patocka 		set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2504d5b9dd04SMikulas Patocka 	else
2505d5b9dd04SMikulas Patocka 		clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
250641abc4e1SHannes Reinecke 	if (old_map)
250783d5e5b0SMikulas Patocka 		dm_sync_table(md);
25082ca3310eSAlasdair G Kergon 
2509042d2a9bSAlasdair G Kergon 	return old_map;
25101da177e4SLinus Torvalds }
25111da177e4SLinus Torvalds 
2512a7940155SAlasdair G Kergon /*
2513a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2514a7940155SAlasdair G Kergon  */
2515a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
25161da177e4SLinus Torvalds {
2517a12f5d48SEric Dumazet 	struct dm_table *map = rcu_dereference_protected(md->map, 1);
25181da177e4SLinus Torvalds 
25191da177e4SLinus Torvalds 	if (!map)
2520a7940155SAlasdair G Kergon 		return NULL;
25211da177e4SLinus Torvalds 
25221da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
25239cdb8520SMonam Agarwal 	RCU_INIT_POINTER(md->map, NULL);
252483d5e5b0SMikulas Patocka 	dm_sync_table(md);
2525a7940155SAlasdair G Kergon 
2526a7940155SAlasdair G Kergon 	return map;
25271da177e4SLinus Torvalds }
25281da177e4SLinus Torvalds 
25291da177e4SLinus Torvalds /*
25301da177e4SLinus Torvalds  * Constructor for a new device.
25311da177e4SLinus Torvalds  */
25322b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
25331da177e4SLinus Torvalds {
25341da177e4SLinus Torvalds 	struct mapped_device *md;
25351da177e4SLinus Torvalds 
25362b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
25371da177e4SLinus Torvalds 	if (!md)
25381da177e4SLinus Torvalds 		return -ENXIO;
25391da177e4SLinus Torvalds 
2540784aae73SMilan Broz 	dm_sysfs_init(md);
2541784aae73SMilan Broz 
25421da177e4SLinus Torvalds 	*result = md;
25431da177e4SLinus Torvalds 	return 0;
25441da177e4SLinus Torvalds }
25451da177e4SLinus Torvalds 
2546a5664dadSMike Snitzer /*
2547a5664dadSMike Snitzer  * Functions to manage md->type.
2548a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2549a5664dadSMike Snitzer  */
2550a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2551a5664dadSMike Snitzer {
2552a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2553a5664dadSMike Snitzer }
2554a5664dadSMike Snitzer 
2555a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2556a5664dadSMike Snitzer {
2557a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2558a5664dadSMike Snitzer }
2559a5664dadSMike Snitzer 
2560a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type)
2561a5664dadSMike Snitzer {
256200c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2563a5664dadSMike Snitzer 	md->type = type;
2564a5664dadSMike Snitzer }
2565a5664dadSMike Snitzer 
2566a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md)
2567a5664dadSMike Snitzer {
256800c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2569a5664dadSMike Snitzer 	return md->type;
2570a5664dadSMike Snitzer }
2571a5664dadSMike Snitzer 
257236a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
257336a0456fSAlasdair G Kergon {
257436a0456fSAlasdair G Kergon 	return md->immutable_target_type;
257536a0456fSAlasdair G Kergon }
257636a0456fSAlasdair G Kergon 
25774a0b4ddfSMike Snitzer /*
2578f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
2579f84cb8a4SMike Snitzer  * count on 'md'.
2580f84cb8a4SMike Snitzer  */
2581f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2582f84cb8a4SMike Snitzer {
2583f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
2584f84cb8a4SMike Snitzer 	return &md->queue->limits;
2585f84cb8a4SMike Snitzer }
2586f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2587f84cb8a4SMike Snitzer 
2588bfebd1cdSMike Snitzer static void init_rq_based_worker_thread(struct mapped_device *md)
2589bfebd1cdSMike Snitzer {
2590bfebd1cdSMike Snitzer 	/* Initialize the request-based DM worker thread */
2591bfebd1cdSMike Snitzer 	init_kthread_worker(&md->kworker);
2592bfebd1cdSMike Snitzer 	md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker,
2593bfebd1cdSMike Snitzer 				       "kdmwork-%s", dm_device_name(md));
2594bfebd1cdSMike Snitzer }
2595bfebd1cdSMike Snitzer 
2596f84cb8a4SMike Snitzer /*
25974a0b4ddfSMike Snitzer  * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
25984a0b4ddfSMike Snitzer  */
25994a0b4ddfSMike Snitzer static int dm_init_request_based_queue(struct mapped_device *md)
26004a0b4ddfSMike Snitzer {
26014a0b4ddfSMike Snitzer 	struct request_queue *q = NULL;
26024a0b4ddfSMike Snitzer 
26034a0b4ddfSMike Snitzer 	/* Fully initialize the queue */
26044a0b4ddfSMike Snitzer 	q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
26054a0b4ddfSMike Snitzer 	if (!q)
2606bfebd1cdSMike Snitzer 		return -EINVAL;
26074a0b4ddfSMike Snitzer 
26080ce65797SMike Snitzer 	/* disable dm_request_fn's merge heuristic by default */
26090ce65797SMike Snitzer 	md->seq_rq_merge_deadline_usecs = 0;
26100ce65797SMike Snitzer 
26114a0b4ddfSMike Snitzer 	md->queue = q;
2612bfebd1cdSMike Snitzer 	dm_init_old_md_queue(md);
26134a0b4ddfSMike Snitzer 	blk_queue_softirq_done(md->queue, dm_softirq_done);
26144a0b4ddfSMike Snitzer 	blk_queue_prep_rq(md->queue, dm_prep_fn);
26154a0b4ddfSMike Snitzer 
2616bfebd1cdSMike Snitzer 	init_rq_based_worker_thread(md);
26172eb6e1e3SKeith Busch 
26184a0b4ddfSMike Snitzer 	elv_register_queue(md->queue);
26194a0b4ddfSMike Snitzer 
2620bfebd1cdSMike Snitzer 	return 0;
2621bfebd1cdSMike Snitzer }
2622bfebd1cdSMike Snitzer 
2623bfebd1cdSMike Snitzer static int dm_mq_init_request(void *data, struct request *rq,
2624bfebd1cdSMike Snitzer 			      unsigned int hctx_idx, unsigned int request_idx,
2625bfebd1cdSMike Snitzer 			      unsigned int numa_node)
2626bfebd1cdSMike Snitzer {
2627bfebd1cdSMike Snitzer 	struct mapped_device *md = data;
2628bfebd1cdSMike Snitzer 	struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2629bfebd1cdSMike Snitzer 
2630bfebd1cdSMike Snitzer 	/*
2631bfebd1cdSMike Snitzer 	 * Must initialize md member of tio, otherwise it won't
2632bfebd1cdSMike Snitzer 	 * be available in dm_mq_queue_rq.
2633bfebd1cdSMike Snitzer 	 */
2634bfebd1cdSMike Snitzer 	tio->md = md;
2635bfebd1cdSMike Snitzer 
2636bfebd1cdSMike Snitzer 	return 0;
2637bfebd1cdSMike Snitzer }
2638bfebd1cdSMike Snitzer 
2639bfebd1cdSMike Snitzer static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
2640bfebd1cdSMike Snitzer 			  const struct blk_mq_queue_data *bd)
2641bfebd1cdSMike Snitzer {
2642bfebd1cdSMike Snitzer 	struct request *rq = bd->rq;
2643bfebd1cdSMike Snitzer 	struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2644bfebd1cdSMike Snitzer 	struct mapped_device *md = tio->md;
2645bfebd1cdSMike Snitzer 	int srcu_idx;
2646bfebd1cdSMike Snitzer 	struct dm_table *map = dm_get_live_table(md, &srcu_idx);
2647bfebd1cdSMike Snitzer 	struct dm_target *ti;
2648bfebd1cdSMike Snitzer 	sector_t pos;
2649bfebd1cdSMike Snitzer 
2650bfebd1cdSMike Snitzer 	/* always use block 0 to find the target for flushes for now */
2651bfebd1cdSMike Snitzer 	pos = 0;
2652bfebd1cdSMike Snitzer 	if (!(rq->cmd_flags & REQ_FLUSH))
2653bfebd1cdSMike Snitzer 		pos = blk_rq_pos(rq);
2654bfebd1cdSMike Snitzer 
2655bfebd1cdSMike Snitzer 	ti = dm_table_find_target(map, pos);
2656bfebd1cdSMike Snitzer 	if (!dm_target_is_valid(ti)) {
2657bfebd1cdSMike Snitzer 		dm_put_live_table(md, srcu_idx);
2658bfebd1cdSMike Snitzer 		DMERR_LIMIT("request attempted access beyond the end of device");
2659bfebd1cdSMike Snitzer 		/*
2660bfebd1cdSMike Snitzer 		 * Must perform setup, that rq_completed() requires,
2661bfebd1cdSMike Snitzer 		 * before returning BLK_MQ_RQ_QUEUE_ERROR
2662bfebd1cdSMike Snitzer 		 */
2663bfebd1cdSMike Snitzer 		dm_start_request(md, rq);
2664bfebd1cdSMike Snitzer 		return BLK_MQ_RQ_QUEUE_ERROR;
2665bfebd1cdSMike Snitzer 	}
2666bfebd1cdSMike Snitzer 	dm_put_live_table(md, srcu_idx);
2667bfebd1cdSMike Snitzer 
2668bfebd1cdSMike Snitzer 	if (ti->type->busy && ti->type->busy(ti))
2669bfebd1cdSMike Snitzer 		return BLK_MQ_RQ_QUEUE_BUSY;
2670bfebd1cdSMike Snitzer 
2671bfebd1cdSMike Snitzer 	dm_start_request(md, rq);
2672bfebd1cdSMike Snitzer 
2673bfebd1cdSMike Snitzer 	/* Init tio using md established in .init_request */
2674bfebd1cdSMike Snitzer 	init_tio(tio, rq, md);
2675bfebd1cdSMike Snitzer 
267602233342SMike Snitzer 	/*
267702233342SMike Snitzer 	 * Establish tio->ti before queuing work (map_tio_request)
267802233342SMike Snitzer 	 * or making direct call to map_request().
267902233342SMike Snitzer 	 */
2680bfebd1cdSMike Snitzer 	tio->ti = ti;
268102233342SMike Snitzer 
268202233342SMike Snitzer 	/* Clone the request if underlying devices aren't blk-mq */
268302233342SMike Snitzer 	if (dm_table_get_type(map) == DM_TYPE_REQUEST_BASED) {
268402233342SMike Snitzer 		/* clone request is allocated at the end of the pdu */
268502233342SMike Snitzer 		tio->clone = (void *)blk_mq_rq_to_pdu(rq) + sizeof(struct dm_rq_target_io);
268645714fbeSMike Snitzer 		(void) clone_rq(rq, md, tio, GFP_ATOMIC);
2687bfebd1cdSMike Snitzer 		queue_kthread_work(&md->kworker, &tio->work);
268802233342SMike Snitzer 	} else {
268902233342SMike Snitzer 		/* Direct call is fine since .queue_rq allows allocations */
269045714fbeSMike Snitzer 		if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) {
269145714fbeSMike Snitzer 			/* Undo dm_start_request() before requeuing */
269245714fbeSMike Snitzer 			rq_completed(md, rq_data_dir(rq), false);
269345714fbeSMike Snitzer 			return BLK_MQ_RQ_QUEUE_BUSY;
269445714fbeSMike Snitzer 		}
269502233342SMike Snitzer 	}
2696bfebd1cdSMike Snitzer 
2697bfebd1cdSMike Snitzer 	return BLK_MQ_RQ_QUEUE_OK;
2698bfebd1cdSMike Snitzer }
2699bfebd1cdSMike Snitzer 
2700bfebd1cdSMike Snitzer static struct blk_mq_ops dm_mq_ops = {
2701bfebd1cdSMike Snitzer 	.queue_rq = dm_mq_queue_rq,
2702bfebd1cdSMike Snitzer 	.map_queue = blk_mq_map_queue,
2703bfebd1cdSMike Snitzer 	.complete = dm_softirq_done,
2704bfebd1cdSMike Snitzer 	.init_request = dm_mq_init_request,
2705bfebd1cdSMike Snitzer };
2706bfebd1cdSMike Snitzer 
2707bfebd1cdSMike Snitzer static int dm_init_request_based_blk_mq_queue(struct mapped_device *md)
2708bfebd1cdSMike Snitzer {
270902233342SMike Snitzer 	unsigned md_type = dm_get_md_type(md);
2710bfebd1cdSMike Snitzer 	struct request_queue *q;
2711bfebd1cdSMike Snitzer 	int err;
2712bfebd1cdSMike Snitzer 
2713bfebd1cdSMike Snitzer 	memset(&md->tag_set, 0, sizeof(md->tag_set));
2714bfebd1cdSMike Snitzer 	md->tag_set.ops = &dm_mq_ops;
2715bfebd1cdSMike Snitzer 	md->tag_set.queue_depth = BLKDEV_MAX_RQ;
2716bfebd1cdSMike Snitzer 	md->tag_set.numa_node = NUMA_NO_NODE;
2717bfebd1cdSMike Snitzer 	md->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
2718bfebd1cdSMike Snitzer 	md->tag_set.nr_hw_queues = 1;
271902233342SMike Snitzer 	if (md_type == DM_TYPE_REQUEST_BASED) {
272002233342SMike Snitzer 		/* make the memory for non-blk-mq clone part of the pdu */
272102233342SMike Snitzer 		md->tag_set.cmd_size = sizeof(struct dm_rq_target_io) + sizeof(struct request);
272202233342SMike Snitzer 	} else
2723bfebd1cdSMike Snitzer 		md->tag_set.cmd_size = sizeof(struct dm_rq_target_io);
2724bfebd1cdSMike Snitzer 	md->tag_set.driver_data = md;
2725bfebd1cdSMike Snitzer 
2726bfebd1cdSMike Snitzer 	err = blk_mq_alloc_tag_set(&md->tag_set);
2727bfebd1cdSMike Snitzer 	if (err)
2728bfebd1cdSMike Snitzer 		return err;
2729bfebd1cdSMike Snitzer 
2730bfebd1cdSMike Snitzer 	q = blk_mq_init_allocated_queue(&md->tag_set, md->queue);
2731bfebd1cdSMike Snitzer 	if (IS_ERR(q)) {
2732bfebd1cdSMike Snitzer 		err = PTR_ERR(q);
2733bfebd1cdSMike Snitzer 		goto out_tag_set;
2734bfebd1cdSMike Snitzer 	}
2735bfebd1cdSMike Snitzer 	md->queue = q;
2736bfebd1cdSMike Snitzer 	dm_init_md_queue(md);
2737bfebd1cdSMike Snitzer 
2738bfebd1cdSMike Snitzer 	/* backfill 'mq' sysfs registration normally done in blk_register_queue */
2739bfebd1cdSMike Snitzer 	blk_mq_register_disk(md->disk);
2740bfebd1cdSMike Snitzer 
274102233342SMike Snitzer 	if (md_type == DM_TYPE_REQUEST_BASED)
2742bfebd1cdSMike Snitzer 		init_rq_based_worker_thread(md);
2743bfebd1cdSMike Snitzer 
2744bfebd1cdSMike Snitzer 	return 0;
2745bfebd1cdSMike Snitzer 
2746bfebd1cdSMike Snitzer out_tag_set:
2747bfebd1cdSMike Snitzer 	blk_mq_free_tag_set(&md->tag_set);
2748bfebd1cdSMike Snitzer 	return err;
27494a0b4ddfSMike Snitzer }
27504a0b4ddfSMike Snitzer 
27514a0b4ddfSMike Snitzer /*
27524a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
27534a0b4ddfSMike Snitzer  */
27544a0b4ddfSMike Snitzer int dm_setup_md_queue(struct mapped_device *md)
27554a0b4ddfSMike Snitzer {
2756bfebd1cdSMike Snitzer 	int r;
275717e149b8SMike Snitzer 	unsigned md_type = filter_md_type(dm_get_md_type(md), md);
2758bfebd1cdSMike Snitzer 
2759bfebd1cdSMike Snitzer 	switch (md_type) {
2760bfebd1cdSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
2761bfebd1cdSMike Snitzer 		r = dm_init_request_based_queue(md);
2762bfebd1cdSMike Snitzer 		if (r) {
27634a0b4ddfSMike Snitzer 			DMWARN("Cannot initialize queue for request-based mapped device");
2764bfebd1cdSMike Snitzer 			return r;
27654a0b4ddfSMike Snitzer 		}
2766bfebd1cdSMike Snitzer 		break;
2767bfebd1cdSMike Snitzer 	case DM_TYPE_MQ_REQUEST_BASED:
2768bfebd1cdSMike Snitzer 		r = dm_init_request_based_blk_mq_queue(md);
2769bfebd1cdSMike Snitzer 		if (r) {
2770bfebd1cdSMike Snitzer 			DMWARN("Cannot initialize queue for request-based blk-mq mapped device");
2771bfebd1cdSMike Snitzer 			return r;
2772bfebd1cdSMike Snitzer 		}
2773bfebd1cdSMike Snitzer 		break;
2774bfebd1cdSMike Snitzer 	case DM_TYPE_BIO_BASED:
2775bfebd1cdSMike Snitzer 		dm_init_old_md_queue(md);
2776ff36ab34SMike Snitzer 		blk_queue_make_request(md->queue, dm_make_request);
2777ff36ab34SMike Snitzer 		blk_queue_merge_bvec(md->queue, dm_merge_bvec);
2778bfebd1cdSMike Snitzer 		break;
2779ff36ab34SMike Snitzer 	}
27804a0b4ddfSMike Snitzer 
27814a0b4ddfSMike Snitzer 	return 0;
27824a0b4ddfSMike Snitzer }
27834a0b4ddfSMike Snitzer 
27842bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev)
27851da177e4SLinus Torvalds {
27861da177e4SLinus Torvalds 	struct mapped_device *md;
27871da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
27881da177e4SLinus Torvalds 
27891da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
27901da177e4SLinus Torvalds 		return NULL;
27911da177e4SLinus Torvalds 
2792f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
27931da177e4SLinus Torvalds 
27941da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
27952bec1f4aSMikulas Patocka 	if (md) {
27962bec1f4aSMikulas Patocka 		if ((md == MINOR_ALLOCED ||
2797f331c029STejun Heo 		     (MINOR(disk_devt(dm_disk(md))) != minor) ||
2798abdc568bSKiyoshi Ueda 		     dm_deleting_md(md) ||
2799fba9f90eSJeff Mahoney 		     test_bit(DMF_FREEING, &md->flags))) {
2800637842cfSDavid Teigland 			md = NULL;
2801fba9f90eSJeff Mahoney 			goto out;
2802fba9f90eSJeff Mahoney 		}
28032bec1f4aSMikulas Patocka 		dm_get(md);
28042bec1f4aSMikulas Patocka 	}
28051da177e4SLinus Torvalds 
2806fba9f90eSJeff Mahoney out:
2807f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
28081da177e4SLinus Torvalds 
2809637842cfSDavid Teigland 	return md;
2810637842cfSDavid Teigland }
28113cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2812d229a958SDavid Teigland 
28139ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2814637842cfSDavid Teigland {
28159ade92a9SAlasdair G Kergon 	return md->interface_ptr;
28161da177e4SLinus Torvalds }
28171da177e4SLinus Torvalds 
28181da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
28191da177e4SLinus Torvalds {
28201da177e4SLinus Torvalds 	md->interface_ptr = ptr;
28211da177e4SLinus Torvalds }
28221da177e4SLinus Torvalds 
28231da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
28241da177e4SLinus Torvalds {
28251da177e4SLinus Torvalds 	atomic_inc(&md->holders);
28263f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
28271da177e4SLinus Torvalds }
28281da177e4SLinus Torvalds 
282909ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md)
283009ee96b2SMikulas Patocka {
283109ee96b2SMikulas Patocka 	spin_lock(&_minor_lock);
283209ee96b2SMikulas Patocka 	if (test_bit(DMF_FREEING, &md->flags)) {
283309ee96b2SMikulas Patocka 		spin_unlock(&_minor_lock);
283409ee96b2SMikulas Patocka 		return -EBUSY;
283509ee96b2SMikulas Patocka 	}
283609ee96b2SMikulas Patocka 	dm_get(md);
283709ee96b2SMikulas Patocka 	spin_unlock(&_minor_lock);
283809ee96b2SMikulas Patocka 	return 0;
283909ee96b2SMikulas Patocka }
284009ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold);
284109ee96b2SMikulas Patocka 
284272d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
284372d94861SAlasdair G Kergon {
284472d94861SAlasdair G Kergon 	return md->name;
284572d94861SAlasdair G Kergon }
284672d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
284772d94861SAlasdair G Kergon 
28483f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
28491da177e4SLinus Torvalds {
28501134e5aeSMike Anderson 	struct dm_table *map;
285183d5e5b0SMikulas Patocka 	int srcu_idx;
28521da177e4SLinus Torvalds 
28533f77316dSKiyoshi Ueda 	might_sleep();
2854fba9f90eSJeff Mahoney 
285583d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
285663a4f065SMike Snitzer 
285763a4f065SMike Snitzer 	spin_lock(&_minor_lock);
28583f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2859fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2860f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
28613f77316dSKiyoshi Ueda 
286202233342SMike Snitzer 	if (dm_request_based(md) && md->kworker_task)
28632eb6e1e3SKeith Busch 		flush_kthread_worker(&md->kworker);
28642eb6e1e3SKeith Busch 
2865ab7c7bb6SMikulas Patocka 	/*
2866ab7c7bb6SMikulas Patocka 	 * Take suspend_lock so that presuspend and postsuspend methods
2867ab7c7bb6SMikulas Patocka 	 * do not race with internal suspend.
2868ab7c7bb6SMikulas Patocka 	 */
2869ab7c7bb6SMikulas Patocka 	mutex_lock(&md->suspend_lock);
28704f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
28711da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
28721da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
28731da177e4SLinus Torvalds 	}
2874ab7c7bb6SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
28753f77316dSKiyoshi Ueda 
287683d5e5b0SMikulas Patocka 	/* dm_put_live_table must be before msleep, otherwise deadlock is possible */
287783d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
287883d5e5b0SMikulas Patocka 
28793f77316dSKiyoshi Ueda 	/*
28803f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
28813f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
28823f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
28833f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
28843f77316dSKiyoshi Ueda 	 */
28853f77316dSKiyoshi Ueda 	if (wait)
28863f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
28873f77316dSKiyoshi Ueda 			msleep(1);
28883f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
28893f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
28903f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
28913f77316dSKiyoshi Ueda 
2892784aae73SMilan Broz 	dm_sysfs_exit(md);
2893a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
28941da177e4SLinus Torvalds 	free_dev(md);
28951da177e4SLinus Torvalds }
28963f77316dSKiyoshi Ueda 
28973f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
28983f77316dSKiyoshi Ueda {
28993f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
29003f77316dSKiyoshi Ueda }
29013f77316dSKiyoshi Ueda 
29023f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
29033f77316dSKiyoshi Ueda {
29043f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
29053f77316dSKiyoshi Ueda }
29063f77316dSKiyoshi Ueda 
29073f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
29083f77316dSKiyoshi Ueda {
29093f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
29101da177e4SLinus Torvalds }
291179eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
29121da177e4SLinus Torvalds 
2913401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
291446125c1cSMilan Broz {
291546125c1cSMilan Broz 	int r = 0;
2916b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
2917b44ebeb0SMikulas Patocka 
2918b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
291946125c1cSMilan Broz 
292046125c1cSMilan Broz 	while (1) {
2921401600dfSMikulas Patocka 		set_current_state(interruptible);
292246125c1cSMilan Broz 
2923b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
292446125c1cSMilan Broz 			break;
292546125c1cSMilan Broz 
2926401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
2927401600dfSMikulas Patocka 		    signal_pending(current)) {
292846125c1cSMilan Broz 			r = -EINTR;
292946125c1cSMilan Broz 			break;
293046125c1cSMilan Broz 		}
293146125c1cSMilan Broz 
293246125c1cSMilan Broz 		io_schedule();
293346125c1cSMilan Broz 	}
293446125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
293546125c1cSMilan Broz 
2936b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
2937b44ebeb0SMikulas Patocka 
293846125c1cSMilan Broz 	return r;
293946125c1cSMilan Broz }
294046125c1cSMilan Broz 
29411da177e4SLinus Torvalds /*
29421da177e4SLinus Torvalds  * Process the deferred bios
29431da177e4SLinus Torvalds  */
2944ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
29451da177e4SLinus Torvalds {
2946ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2947ef208587SMikulas Patocka 						work);
29486d6f10dfSMilan Broz 	struct bio *c;
294983d5e5b0SMikulas Patocka 	int srcu_idx;
295083d5e5b0SMikulas Patocka 	struct dm_table *map;
29511da177e4SLinus Torvalds 
295283d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
2953ef208587SMikulas Patocka 
29543b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2955022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2956022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2957022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2958022c2611SMikulas Patocka 
29596a8736d1STejun Heo 		if (!c)
2960df12ee99SAlasdair G Kergon 			break;
296173d410c0SMilan Broz 
2962e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2963e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2964af7e466aSMikulas Patocka 		else
296583d5e5b0SMikulas Patocka 			__split_and_process_bio(md, map, c);
2966e6ee8c0bSKiyoshi Ueda 	}
29673b00b203SMikulas Patocka 
296883d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
29691da177e4SLinus Torvalds }
29701da177e4SLinus Torvalds 
29719a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2972304f3f6aSMilan Broz {
29733b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
29744e857c58SPeter Zijlstra 	smp_mb__after_atomic();
297553d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2976304f3f6aSMilan Broz }
2977304f3f6aSMilan Broz 
29781da177e4SLinus Torvalds /*
2979042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
29801da177e4SLinus Torvalds  */
2981042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
29821da177e4SLinus Torvalds {
298387eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2984754c5fc7SMike Snitzer 	struct queue_limits limits;
2985042d2a9bSAlasdair G Kergon 	int r;
29861da177e4SLinus Torvalds 
2987e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
29881da177e4SLinus Torvalds 
29891da177e4SLinus Torvalds 	/* device must be suspended */
29904f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
299193c534aeSAlasdair G Kergon 		goto out;
29921da177e4SLinus Torvalds 
29933ae70656SMike Snitzer 	/*
29943ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
29953ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
29963ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
29973ae70656SMike Snitzer 	 * reappear.
29983ae70656SMike Snitzer 	 */
29993ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
300083d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
30013ae70656SMike Snitzer 		if (live_map)
30023ae70656SMike Snitzer 			limits = md->queue->limits;
300383d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
30043ae70656SMike Snitzer 	}
30053ae70656SMike Snitzer 
300687eb5b21SMike Christie 	if (!live_map) {
3007754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
3008042d2a9bSAlasdair G Kergon 		if (r) {
3009042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
3010754c5fc7SMike Snitzer 			goto out;
3011042d2a9bSAlasdair G Kergon 		}
301287eb5b21SMike Christie 	}
3013754c5fc7SMike Snitzer 
3014042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
30151da177e4SLinus Torvalds 
301693c534aeSAlasdair G Kergon out:
3017e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
3018042d2a9bSAlasdair G Kergon 	return map;
30191da177e4SLinus Torvalds }
30201da177e4SLinus Torvalds 
30211da177e4SLinus Torvalds /*
30221da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
30231da177e4SLinus Torvalds  * device.
30241da177e4SLinus Torvalds  */
30252ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
30261da177e4SLinus Torvalds {
3027e39e2e95SAlasdair G Kergon 	int r;
30281da177e4SLinus Torvalds 
30291da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
3030dfbe03f6SAlasdair G Kergon 
3031db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
3032dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
3033cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
3034e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
3035e39e2e95SAlasdair G Kergon 		return r;
3036dfbe03f6SAlasdair G Kergon 	}
3037dfbe03f6SAlasdair G Kergon 
3038aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
3039aa8d7c2fSAlasdair G Kergon 
30401da177e4SLinus Torvalds 	return 0;
30411da177e4SLinus Torvalds }
30421da177e4SLinus Torvalds 
30432ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
30441da177e4SLinus Torvalds {
3045aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
3046aa8d7c2fSAlasdair G Kergon 		return;
3047aa8d7c2fSAlasdair G Kergon 
3048db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
30491da177e4SLinus Torvalds 	md->frozen_sb = NULL;
3050aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
30511da177e4SLinus Torvalds }
30521da177e4SLinus Torvalds 
30531da177e4SLinus Torvalds /*
3054ffcc3936SMike Snitzer  * If __dm_suspend returns 0, the device is completely quiescent
3055ffcc3936SMike Snitzer  * now. There is no request-processing activity. All new requests
3056ffcc3936SMike Snitzer  * are being added to md->deferred list.
3057cec47e3dSKiyoshi Ueda  *
3058ffcc3936SMike Snitzer  * Caller must hold md->suspend_lock
3059cec47e3dSKiyoshi Ueda  */
3060ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
3061ffcc3936SMike Snitzer 			unsigned suspend_flags, int interruptible)
30621da177e4SLinus Torvalds {
3063ffcc3936SMike Snitzer 	bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
3064ffcc3936SMike Snitzer 	bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
3065ffcc3936SMike Snitzer 	int r;
3066cf222b37SAlasdair G Kergon 
30672e93ccc1SKiyoshi Ueda 	/*
30682e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
30692e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
30702e93ccc1SKiyoshi Ueda 	 */
30712e93ccc1SKiyoshi Ueda 	if (noflush)
30722e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
30732e93ccc1SKiyoshi Ueda 
3074d67ee213SMike Snitzer 	/*
3075d67ee213SMike Snitzer 	 * This gets reverted if there's an error later and the targets
3076d67ee213SMike Snitzer 	 * provide the .presuspend_undo hook.
3077d67ee213SMike Snitzer 	 */
30781da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
30791da177e4SLinus Torvalds 
30802e93ccc1SKiyoshi Ueda 	/*
30819f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
30829f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
30839f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
30849f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
30852e93ccc1SKiyoshi Ueda 	 */
308632a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
30872ca3310eSAlasdair G Kergon 		r = lock_fs(md);
3088d67ee213SMike Snitzer 		if (r) {
3089d67ee213SMike Snitzer 			dm_table_presuspend_undo_targets(map);
3090ffcc3936SMike Snitzer 			return r;
3091aa8d7c2fSAlasdair G Kergon 		}
3092d67ee213SMike Snitzer 	}
30931da177e4SLinus Torvalds 
30941da177e4SLinus Torvalds 	/*
30953b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
30963b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
30973b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
30983b00b203SMikulas Patocka 	 * dm_wq_work.
30993b00b203SMikulas Patocka 	 *
31003b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
31013b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
31026a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
31036a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
31046a8736d1STejun Heo 	 * flush_workqueue(md->wq).
31051da177e4SLinus Torvalds 	 */
31061eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
310741abc4e1SHannes Reinecke 	if (map)
310883d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
31091da177e4SLinus Torvalds 
3110d0bcb878SKiyoshi Ueda 	/*
311129e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
311229e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
3113d0bcb878SKiyoshi Ueda 	 */
31142eb6e1e3SKeith Busch 	if (dm_request_based(md)) {
31159f518b27SKiyoshi Ueda 		stop_queue(md->queue);
311602233342SMike Snitzer 		if (md->kworker_task)
31172eb6e1e3SKeith Busch 			flush_kthread_worker(&md->kworker);
31182eb6e1e3SKeith Busch 	}
3119cec47e3dSKiyoshi Ueda 
3120d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
3121d0bcb878SKiyoshi Ueda 
31221da177e4SLinus Torvalds 	/*
31233b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
31243b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
31253b00b203SMikulas Patocka 	 * to finish.
31261da177e4SLinus Torvalds 	 */
3127ffcc3936SMike Snitzer 	r = dm_wait_for_completion(md, interruptible);
31281da177e4SLinus Torvalds 
31296d6f10dfSMilan Broz 	if (noflush)
3130022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
313141abc4e1SHannes Reinecke 	if (map)
313283d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
31332e93ccc1SKiyoshi Ueda 
31341da177e4SLinus Torvalds 	/* were we interrupted ? */
313546125c1cSMilan Broz 	if (r < 0) {
31369a1fb464SMikulas Patocka 		dm_queue_flush(md);
313773d410c0SMilan Broz 
3138cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
31399f518b27SKiyoshi Ueda 			start_queue(md->queue);
3140cec47e3dSKiyoshi Ueda 
31412ca3310eSAlasdair G Kergon 		unlock_fs(md);
3142d67ee213SMike Snitzer 		dm_table_presuspend_undo_targets(map);
3143ffcc3936SMike Snitzer 		/* pushback list is already flushed, so skip flush */
3144ffcc3936SMike Snitzer 	}
3145ffcc3936SMike Snitzer 
3146ffcc3936SMike Snitzer 	return r;
31472ca3310eSAlasdair G Kergon }
31482ca3310eSAlasdair G Kergon 
31493b00b203SMikulas Patocka /*
3150ffcc3936SMike Snitzer  * We need to be able to change a mapping table under a mounted
3151ffcc3936SMike Snitzer  * filesystem.  For example we might want to move some data in
3152ffcc3936SMike Snitzer  * the background.  Before the table can be swapped with
3153ffcc3936SMike Snitzer  * dm_bind_table, dm_suspend must be called to flush any in
3154ffcc3936SMike Snitzer  * flight bios and ensure that any further io gets deferred.
31553b00b203SMikulas Patocka  */
3156ffcc3936SMike Snitzer /*
3157ffcc3936SMike Snitzer  * Suspend mechanism in request-based dm.
3158ffcc3936SMike Snitzer  *
3159ffcc3936SMike Snitzer  * 1. Flush all I/Os by lock_fs() if needed.
3160ffcc3936SMike Snitzer  * 2. Stop dispatching any I/O by stopping the request_queue.
3161ffcc3936SMike Snitzer  * 3. Wait for all in-flight I/Os to be completed or requeued.
3162ffcc3936SMike Snitzer  *
3163ffcc3936SMike Snitzer  * To abort suspend, start the request_queue.
3164ffcc3936SMike Snitzer  */
3165ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
3166ffcc3936SMike Snitzer {
3167ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
3168ffcc3936SMike Snitzer 	int r = 0;
3169ffcc3936SMike Snitzer 
3170ffcc3936SMike Snitzer retry:
3171ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3172ffcc3936SMike Snitzer 
3173ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
3174ffcc3936SMike Snitzer 		r = -EINVAL;
3175ffcc3936SMike Snitzer 		goto out_unlock;
3176ffcc3936SMike Snitzer 	}
3177ffcc3936SMike Snitzer 
3178ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
3179ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
3180ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
3181ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3182ffcc3936SMike Snitzer 		if (r)
3183ffcc3936SMike Snitzer 			return r;
3184ffcc3936SMike Snitzer 		goto retry;
3185ffcc3936SMike Snitzer 	}
3186ffcc3936SMike Snitzer 
3187a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
3188ffcc3936SMike Snitzer 
3189ffcc3936SMike Snitzer 	r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE);
3190ffcc3936SMike Snitzer 	if (r)
3191ffcc3936SMike Snitzer 		goto out_unlock;
31923b00b203SMikulas Patocka 
31931da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
31941da177e4SLinus Torvalds 
31954d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
31964d4471cbSKiyoshi Ueda 
3197d287483dSAlasdair G Kergon out_unlock:
3198e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
3199cf222b37SAlasdair G Kergon 	return r;
32001da177e4SLinus Torvalds }
32011da177e4SLinus Torvalds 
3202ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map)
32031da177e4SLinus Torvalds {
3204ffcc3936SMike Snitzer 	if (map) {
3205ffcc3936SMike Snitzer 		int r = dm_table_resume_targets(map);
32068757b776SMilan Broz 		if (r)
3207ffcc3936SMike Snitzer 			return r;
3208ffcc3936SMike Snitzer 	}
32092ca3310eSAlasdair G Kergon 
32109a1fb464SMikulas Patocka 	dm_queue_flush(md);
32112ca3310eSAlasdair G Kergon 
3212cec47e3dSKiyoshi Ueda 	/*
3213cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
3214cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
3215cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
3216cec47e3dSKiyoshi Ueda 	 */
3217cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
3218cec47e3dSKiyoshi Ueda 		start_queue(md->queue);
3219cec47e3dSKiyoshi Ueda 
32202ca3310eSAlasdair G Kergon 	unlock_fs(md);
32212ca3310eSAlasdair G Kergon 
3222ffcc3936SMike Snitzer 	return 0;
3223ffcc3936SMike Snitzer }
3224ffcc3936SMike Snitzer 
3225ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md)
3226ffcc3936SMike Snitzer {
3227ffcc3936SMike Snitzer 	int r = -EINVAL;
3228ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
3229ffcc3936SMike Snitzer 
3230ffcc3936SMike Snitzer retry:
3231ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3232ffcc3936SMike Snitzer 
3233ffcc3936SMike Snitzer 	if (!dm_suspended_md(md))
3234ffcc3936SMike Snitzer 		goto out;
3235ffcc3936SMike Snitzer 
3236ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
3237ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
3238ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
3239ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3240ffcc3936SMike Snitzer 		if (r)
3241ffcc3936SMike Snitzer 			return r;
3242ffcc3936SMike Snitzer 		goto retry;
3243ffcc3936SMike Snitzer 	}
3244ffcc3936SMike Snitzer 
3245a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
3246ffcc3936SMike Snitzer 	if (!map || !dm_table_get_size(map))
3247ffcc3936SMike Snitzer 		goto out;
3248ffcc3936SMike Snitzer 
3249ffcc3936SMike Snitzer 	r = __dm_resume(md, map);
3250ffcc3936SMike Snitzer 	if (r)
3251ffcc3936SMike Snitzer 		goto out;
3252ffcc3936SMike Snitzer 
32532ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
32542ca3310eSAlasdair G Kergon 
3255cf222b37SAlasdair G Kergon 	r = 0;
3256cf222b37SAlasdair G Kergon out:
3257e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
32582ca3310eSAlasdair G Kergon 
3259cf222b37SAlasdair G Kergon 	return r;
32601da177e4SLinus Torvalds }
32611da177e4SLinus Torvalds 
3262fd2ed4d2SMikulas Patocka /*
3263fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
3264fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
3265fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
3266fd2ed4d2SMikulas Patocka  */
3267fd2ed4d2SMikulas Patocka 
3268ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
3269ffcc3936SMike Snitzer {
3270ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
3271ffcc3936SMike Snitzer 
327296b26c8cSMikulas Patocka 	if (md->internal_suspend_count++)
3273ffcc3936SMike Snitzer 		return; /* nested internal suspend */
3274ffcc3936SMike Snitzer 
3275ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
3276ffcc3936SMike Snitzer 		set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3277ffcc3936SMike Snitzer 		return; /* nest suspend */
3278ffcc3936SMike Snitzer 	}
3279ffcc3936SMike Snitzer 
3280a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
3281ffcc3936SMike Snitzer 
3282ffcc3936SMike Snitzer 	/*
3283ffcc3936SMike Snitzer 	 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
3284ffcc3936SMike Snitzer 	 * supported.  Properly supporting a TASK_INTERRUPTIBLE internal suspend
3285ffcc3936SMike Snitzer 	 * would require changing .presuspend to return an error -- avoid this
3286ffcc3936SMike Snitzer 	 * until there is a need for more elaborate variants of internal suspend.
3287ffcc3936SMike Snitzer 	 */
3288ffcc3936SMike Snitzer 	(void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE);
3289ffcc3936SMike Snitzer 
3290ffcc3936SMike Snitzer 	set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3291ffcc3936SMike Snitzer 
3292ffcc3936SMike Snitzer 	dm_table_postsuspend_targets(map);
3293ffcc3936SMike Snitzer }
3294ffcc3936SMike Snitzer 
3295ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md)
3296ffcc3936SMike Snitzer {
329796b26c8cSMikulas Patocka 	BUG_ON(!md->internal_suspend_count);
329896b26c8cSMikulas Patocka 
329996b26c8cSMikulas Patocka 	if (--md->internal_suspend_count)
3300ffcc3936SMike Snitzer 		return; /* resume from nested internal suspend */
3301ffcc3936SMike Snitzer 
3302ffcc3936SMike Snitzer 	if (dm_suspended_md(md))
3303ffcc3936SMike Snitzer 		goto done; /* resume from nested suspend */
3304ffcc3936SMike Snitzer 
3305ffcc3936SMike Snitzer 	/*
3306ffcc3936SMike Snitzer 	 * NOTE: existing callers don't need to call dm_table_resume_targets
3307ffcc3936SMike Snitzer 	 * (which may fail -- so best to avoid it for now by passing NULL map)
3308ffcc3936SMike Snitzer 	 */
3309ffcc3936SMike Snitzer 	(void) __dm_resume(md, NULL);
3310ffcc3936SMike Snitzer 
3311ffcc3936SMike Snitzer done:
3312ffcc3936SMike Snitzer 	clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3313ffcc3936SMike Snitzer 	smp_mb__after_atomic();
3314ffcc3936SMike Snitzer 	wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
3315ffcc3936SMike Snitzer }
3316ffcc3936SMike Snitzer 
3317ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md)
3318fd2ed4d2SMikulas Patocka {
3319fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
3320ffcc3936SMike Snitzer 	__dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
3321ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
3322ffcc3936SMike Snitzer }
3323ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
3324ffcc3936SMike Snitzer 
3325ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md)
3326ffcc3936SMike Snitzer {
3327ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
3328ffcc3936SMike Snitzer 	__dm_internal_resume(md);
3329ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
3330ffcc3936SMike Snitzer }
3331ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume);
3332ffcc3936SMike Snitzer 
3333ffcc3936SMike Snitzer /*
3334ffcc3936SMike Snitzer  * Fast variants of internal suspend/resume hold md->suspend_lock,
3335ffcc3936SMike Snitzer  * which prevents interaction with userspace-driven suspend.
3336ffcc3936SMike Snitzer  */
3337ffcc3936SMike Snitzer 
3338ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md)
3339ffcc3936SMike Snitzer {
3340ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
3341ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
3342fd2ed4d2SMikulas Patocka 		return;
3343fd2ed4d2SMikulas Patocka 
3344fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
3345fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
3346fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
3347fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
3348fd2ed4d2SMikulas Patocka }
3349b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
3350fd2ed4d2SMikulas Patocka 
3351ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md)
3352fd2ed4d2SMikulas Patocka {
3353ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
3354fd2ed4d2SMikulas Patocka 		goto done;
3355fd2ed4d2SMikulas Patocka 
3356fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
3357fd2ed4d2SMikulas Patocka 
3358fd2ed4d2SMikulas Patocka done:
3359fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
3360fd2ed4d2SMikulas Patocka }
3361b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
3362fd2ed4d2SMikulas Patocka 
33631da177e4SLinus Torvalds /*-----------------------------------------------------------------
33641da177e4SLinus Torvalds  * Event notification.
33651da177e4SLinus Torvalds  *---------------------------------------------------------------*/
33663abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
336760935eb2SMilan Broz 		       unsigned cookie)
336869267a30SAlasdair G Kergon {
336960935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
337060935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
337160935eb2SMilan Broz 
337260935eb2SMilan Broz 	if (!cookie)
33733abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
337460935eb2SMilan Broz 	else {
337560935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
337660935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
33773abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
33783abf85b5SPeter Rajnoha 					  action, envp);
337960935eb2SMilan Broz 	}
338069267a30SAlasdair G Kergon }
338169267a30SAlasdair G Kergon 
33827a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
33837a8c3d3bSMike Anderson {
33847a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
33857a8c3d3bSMike Anderson }
33867a8c3d3bSMike Anderson 
33871da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
33881da177e4SLinus Torvalds {
33891da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
33901da177e4SLinus Torvalds }
33911da177e4SLinus Torvalds 
33921da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
33931da177e4SLinus Torvalds {
33941da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
33951da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
33961da177e4SLinus Torvalds }
33971da177e4SLinus Torvalds 
33987a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
33997a8c3d3bSMike Anderson {
34007a8c3d3bSMike Anderson 	unsigned long flags;
34017a8c3d3bSMike Anderson 
34027a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
34037a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
34047a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
34057a8c3d3bSMike Anderson }
34067a8c3d3bSMike Anderson 
34071da177e4SLinus Torvalds /*
34081da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
34091da177e4SLinus Torvalds  * count on 'md'.
34101da177e4SLinus Torvalds  */
34111da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
34121da177e4SLinus Torvalds {
34131da177e4SLinus Torvalds 	return md->disk;
34141da177e4SLinus Torvalds }
341565ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk);
34161da177e4SLinus Torvalds 
3417784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
3418784aae73SMilan Broz {
34192995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
3420784aae73SMilan Broz }
3421784aae73SMilan Broz 
3422784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
3423784aae73SMilan Broz {
3424784aae73SMilan Broz 	struct mapped_device *md;
3425784aae73SMilan Broz 
34262995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
3427784aae73SMilan Broz 
34284d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
3429432a212cSMike Anderson 	    dm_deleting_md(md))
34304d89b7b4SMilan Broz 		return NULL;
34314d89b7b4SMilan Broz 
3432784aae73SMilan Broz 	dm_get(md);
3433784aae73SMilan Broz 	return md;
3434784aae73SMilan Broz }
3435784aae73SMilan Broz 
34364f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
34371da177e4SLinus Torvalds {
34381da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
34391da177e4SLinus Torvalds }
34401da177e4SLinus Torvalds 
3441ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md)
3442ffcc3936SMike Snitzer {
3443ffcc3936SMike Snitzer 	return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3444ffcc3936SMike Snitzer }
3445ffcc3936SMike Snitzer 
34462c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
34472c140a24SMikulas Patocka {
34482c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
34492c140a24SMikulas Patocka }
34502c140a24SMikulas Patocka 
345164dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
345264dbce58SKiyoshi Ueda {
3453ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
345464dbce58SKiyoshi Ueda }
345564dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
345664dbce58SKiyoshi Ueda 
34572e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
34582e93ccc1SKiyoshi Ueda {
3459ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
34602e93ccc1SKiyoshi Ueda }
34612e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
34622e93ccc1SKiyoshi Ueda 
34635f1b670dSChristoph Hellwig struct dm_md_mempools *dm_alloc_bio_mempools(unsigned integrity,
34645f1b670dSChristoph Hellwig 					     unsigned per_bio_data_size)
3465e6ee8c0bSKiyoshi Ueda {
34665f1b670dSChristoph Hellwig 	struct dm_md_mempools *pools;
34675f1b670dSChristoph Hellwig 	unsigned int pool_size = dm_get_reserved_bio_based_ios();
34685f015204SJun'ichi Nomura 	unsigned int front_pad;
3469e6ee8c0bSKiyoshi Ueda 
34705f1b670dSChristoph Hellwig 	pools = kzalloc(sizeof(*pools), GFP_KERNEL);
3471e6ee8c0bSKiyoshi Ueda 	if (!pools)
3472cbc4e3c1SMike Snitzer 		return ERR_PTR(-ENOMEM);
3473e6ee8c0bSKiyoshi Ueda 
34745f1b670dSChristoph Hellwig 	front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) +
34755f1b670dSChristoph Hellwig 		offsetof(struct dm_target_io, clone);
347617e149b8SMike Snitzer 
34775f1b670dSChristoph Hellwig 	pools->io_pool = mempool_create_slab_pool(pool_size, _io_cache);
3478e6ee8c0bSKiyoshi Ueda 	if (!pools->io_pool)
34795f015204SJun'ichi Nomura 		goto out;
3480e6ee8c0bSKiyoshi Ueda 
34813d8aab2dSJunichi Nomura 	pools->bs = bioset_create_nobvec(pool_size, front_pad);
3482e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
34835f015204SJun'ichi Nomura 		goto out;
3484e6ee8c0bSKiyoshi Ueda 
3485a91a2785SMartin K. Petersen 	if (integrity && bioset_integrity_create(pools->bs, pool_size))
34865f015204SJun'ichi Nomura 		goto out;
3487a91a2785SMartin K. Petersen 
3488e6ee8c0bSKiyoshi Ueda 	return pools;
34895f015204SJun'ichi Nomura out:
34905f015204SJun'ichi Nomura 	dm_free_md_mempools(pools);
3491cbc4e3c1SMike Snitzer 	return ERR_PTR(-ENOMEM);
34925f1b670dSChristoph Hellwig }
3493e6ee8c0bSKiyoshi Ueda 
34945f1b670dSChristoph Hellwig struct dm_md_mempools *dm_alloc_rq_mempools(struct mapped_device *md,
34955f1b670dSChristoph Hellwig 					    unsigned type)
34965f1b670dSChristoph Hellwig {
3497cbc4e3c1SMike Snitzer 	unsigned int pool_size;
34985f1b670dSChristoph Hellwig 	struct dm_md_mempools *pools;
34995f1b670dSChristoph Hellwig 
3500cbc4e3c1SMike Snitzer 	if (filter_md_type(type, md) == DM_TYPE_MQ_REQUEST_BASED)
3501cbc4e3c1SMike Snitzer 		return NULL; /* No mempools needed */
3502cbc4e3c1SMike Snitzer 
3503cbc4e3c1SMike Snitzer 	pool_size = dm_get_reserved_rq_based_ios();
35045f1b670dSChristoph Hellwig 	pools = kzalloc(sizeof(*pools), GFP_KERNEL);
35055f1b670dSChristoph Hellwig 	if (!pools)
3506cbc4e3c1SMike Snitzer 		return ERR_PTR(-ENOMEM);
35075f1b670dSChristoph Hellwig 
35085f1b670dSChristoph Hellwig 	pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache);
35095f1b670dSChristoph Hellwig 	if (!pools->rq_pool)
35105f1b670dSChristoph Hellwig 		goto out;
35115f1b670dSChristoph Hellwig 
35125f1b670dSChristoph Hellwig 	pools->io_pool = mempool_create_slab_pool(pool_size, _rq_tio_cache);
35135f1b670dSChristoph Hellwig 	if (!pools->io_pool)
35145f1b670dSChristoph Hellwig 		goto out;
35155f1b670dSChristoph Hellwig 
35165f1b670dSChristoph Hellwig 	return pools;
35175f1b670dSChristoph Hellwig out:
35185f1b670dSChristoph Hellwig 	dm_free_md_mempools(pools);
3519cbc4e3c1SMike Snitzer 	return ERR_PTR(-ENOMEM);
3520e6ee8c0bSKiyoshi Ueda }
3521e6ee8c0bSKiyoshi Ueda 
3522e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
3523e6ee8c0bSKiyoshi Ueda {
3524e6ee8c0bSKiyoshi Ueda 	if (!pools)
3525e6ee8c0bSKiyoshi Ueda 		return;
3526e6ee8c0bSKiyoshi Ueda 
3527e6ee8c0bSKiyoshi Ueda 	if (pools->io_pool)
3528e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->io_pool);
3529e6ee8c0bSKiyoshi Ueda 
35301ae49ea2SMike Snitzer 	if (pools->rq_pool)
35311ae49ea2SMike Snitzer 		mempool_destroy(pools->rq_pool);
35321ae49ea2SMike Snitzer 
3533e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
3534e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
3535e6ee8c0bSKiyoshi Ueda 
3536e6ee8c0bSKiyoshi Ueda 	kfree(pools);
3537e6ee8c0bSKiyoshi Ueda }
3538e6ee8c0bSKiyoshi Ueda 
353983d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
35401da177e4SLinus Torvalds 	.open = dm_blk_open,
35411da177e4SLinus Torvalds 	.release = dm_blk_close,
3542aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
35433ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
35441da177e4SLinus Torvalds 	.owner = THIS_MODULE
35451da177e4SLinus Torvalds };
35461da177e4SLinus Torvalds 
35471da177e4SLinus Torvalds /*
35481da177e4SLinus Torvalds  * module hooks
35491da177e4SLinus Torvalds  */
35501da177e4SLinus Torvalds module_init(dm_init);
35511da177e4SLinus Torvalds module_exit(dm_exit);
35521da177e4SLinus Torvalds 
35531da177e4SLinus Torvalds module_param(major, uint, 0);
35541da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
3555f4790826SMike Snitzer 
3556e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3557e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3558e8603136SMike Snitzer 
3559f4790826SMike Snitzer module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
3560f4790826SMike Snitzer MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
3561f4790826SMike Snitzer 
356217e149b8SMike Snitzer module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
356317e149b8SMike Snitzer MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
356417e149b8SMike Snitzer 
35651da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
35661da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
35671da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3568