xref: /openbmc/linux/drivers/md/dm.c (revision 591ddcfc4bfad28e096787b1159942124d49cd1e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3784aae73SMilan Broz  * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This file is released under the GPL.
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include "dm.h"
951e5b2bdSMike Anderson #include "dm-uevent.h"
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/init.h>
121da177e4SLinus Torvalds #include <linux/module.h>
1348c9c27bSArjan van de Ven #include <linux/mutex.h>
141da177e4SLinus Torvalds #include <linux/moduleparam.h>
151da177e4SLinus Torvalds #include <linux/blkpg.h>
161da177e4SLinus Torvalds #include <linux/bio.h>
171da177e4SLinus Torvalds #include <linux/mempool.h>
181da177e4SLinus Torvalds #include <linux/slab.h>
191da177e4SLinus Torvalds #include <linux/idr.h>
203ac51e74SDarrick J. Wong #include <linux/hdreg.h>
213f77316dSKiyoshi Ueda #include <linux/delay.h>
22ffcc3936SMike Snitzer #include <linux/wait.h>
232eb6e1e3SKeith Busch #include <linux/kthread.h>
240ce65797SMike Snitzer #include <linux/ktime.h>
25de3ec86dSMike Snitzer #include <linux/elevator.h> /* for rq_end_sector() */
26bfebd1cdSMike Snitzer #include <linux/blk-mq.h>
2771cdb697SChristoph Hellwig #include <linux/pr.h>
2855782138SLi Zefan 
2955782138SLi Zefan #include <trace/events/block.h>
301da177e4SLinus Torvalds 
3172d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
3272d94861SAlasdair G Kergon 
3371a16736SNamhyung Kim #ifdef CONFIG_PRINTK
3471a16736SNamhyung Kim /*
3571a16736SNamhyung Kim  * ratelimit state to be used in DMXXX_LIMIT().
3671a16736SNamhyung Kim  */
3771a16736SNamhyung Kim DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
3871a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_INTERVAL,
3971a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_BURST);
4071a16736SNamhyung Kim EXPORT_SYMBOL(dm_ratelimit_state);
4171a16736SNamhyung Kim #endif
4271a16736SNamhyung Kim 
4360935eb2SMilan Broz /*
4460935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
4560935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
4660935eb2SMilan Broz  */
4760935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
4860935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
4960935eb2SMilan Broz 
501da177e4SLinus Torvalds static const char *_name = DM_NAME;
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds static unsigned int major = 0;
531da177e4SLinus Torvalds static unsigned int _major = 0;
541da177e4SLinus Torvalds 
55d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr);
56d15b774cSAlasdair G Kergon 
57f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
582c140a24SMikulas Patocka 
592c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w);
602c140a24SMikulas Patocka 
612c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
622c140a24SMikulas Patocka 
63acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue;
64acfe0ad7SMikulas Patocka 
651da177e4SLinus Torvalds /*
668fbf26adSKiyoshi Ueda  * For bio-based dm.
671da177e4SLinus Torvalds  * One of these is allocated per bio.
681da177e4SLinus Torvalds  */
691da177e4SLinus Torvalds struct dm_io {
701da177e4SLinus Torvalds 	struct mapped_device *md;
711da177e4SLinus Torvalds 	int error;
721da177e4SLinus Torvalds 	atomic_t io_count;
736ae2fa67SRichard Kennedy 	struct bio *bio;
743eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
75f88fb981SKiyoshi Ueda 	spinlock_t endio_lock;
76fd2ed4d2SMikulas Patocka 	struct dm_stats_aux stats_aux;
771da177e4SLinus Torvalds };
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /*
808fbf26adSKiyoshi Ueda  * For request-based dm.
818fbf26adSKiyoshi Ueda  * One of these is allocated per request.
828fbf26adSKiyoshi Ueda  */
838fbf26adSKiyoshi Ueda struct dm_rq_target_io {
848fbf26adSKiyoshi Ueda 	struct mapped_device *md;
858fbf26adSKiyoshi Ueda 	struct dm_target *ti;
861ae49ea2SMike Snitzer 	struct request *orig, *clone;
872eb6e1e3SKeith Busch 	struct kthread_work work;
888fbf26adSKiyoshi Ueda 	int error;
898fbf26adSKiyoshi Ueda 	union map_info info;
90e262f347SMikulas Patocka 	struct dm_stats_aux stats_aux;
91e262f347SMikulas Patocka 	unsigned long duration_jiffies;
92e262f347SMikulas Patocka 	unsigned n_sectors;
938fbf26adSKiyoshi Ueda };
948fbf26adSKiyoshi Ueda 
958fbf26adSKiyoshi Ueda /*
9694818742SKent Overstreet  * For request-based dm - the bio clones we allocate are embedded in these
9794818742SKent Overstreet  * structs.
9894818742SKent Overstreet  *
9994818742SKent Overstreet  * We allocate these with bio_alloc_bioset, using the front_pad parameter when
10094818742SKent Overstreet  * the bioset is created - this means the bio has to come at the end of the
10194818742SKent Overstreet  * struct.
1028fbf26adSKiyoshi Ueda  */
1038fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info {
1048fbf26adSKiyoshi Ueda 	struct bio *orig;
105cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio;
10694818742SKent Overstreet 	struct bio clone;
1078fbf26adSKiyoshi Ueda };
1088fbf26adSKiyoshi Ueda 
109ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
110ba61fdd1SJeff Mahoney 
1111da177e4SLinus Torvalds /*
1121da177e4SLinus Torvalds  * Bits for the md->flags field.
1131da177e4SLinus Torvalds  */
1141eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
1151da177e4SLinus Torvalds #define DMF_SUSPENDED 1
116aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
117fba9f90eSJeff Mahoney #define DMF_FREEING 3
1185c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
1192e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
1208ae12666SKent Overstreet #define DMF_DEFERRED_REMOVE 6
1218ae12666SKent Overstreet #define DMF_SUSPENDED_INTERNALLY 7
1221da177e4SLinus Torvalds 
123304f3f6aSMilan Broz /*
12483d5e5b0SMikulas Patocka  * A dummy definition to make RCU happy.
12583d5e5b0SMikulas Patocka  * struct dm_table should never be dereferenced in this file.
12683d5e5b0SMikulas Patocka  */
12783d5e5b0SMikulas Patocka struct dm_table {
12883d5e5b0SMikulas Patocka 	int undefined__;
12983d5e5b0SMikulas Patocka };
13083d5e5b0SMikulas Patocka 
13183d5e5b0SMikulas Patocka /*
132304f3f6aSMilan Broz  * Work processed by per-device workqueue.
133304f3f6aSMilan Broz  */
1341da177e4SLinus Torvalds struct mapped_device {
13583d5e5b0SMikulas Patocka 	struct srcu_struct io_barrier;
136e61290a4SDaniel Walker 	struct mutex suspend_lock;
1371da177e4SLinus Torvalds 	atomic_t holders;
1385c6bd75dSAlasdair G Kergon 	atomic_t open_count;
1391da177e4SLinus Torvalds 
1402a7faeb1SMikulas Patocka 	/*
1412a7faeb1SMikulas Patocka 	 * The current mapping.
1422a7faeb1SMikulas Patocka 	 * Use dm_get_live_table{_fast} or take suspend_lock for
1432a7faeb1SMikulas Patocka 	 * dereference.
1442a7faeb1SMikulas Patocka 	 */
1456fa99520SPranith Kumar 	struct dm_table __rcu *map;
1462a7faeb1SMikulas Patocka 
14786f1152bSBenjamin Marzinski 	struct list_head table_devices;
14886f1152bSBenjamin Marzinski 	struct mutex table_devices_lock;
14986f1152bSBenjamin Marzinski 
1501da177e4SLinus Torvalds 	unsigned long flags;
1511da177e4SLinus Torvalds 
152165125e1SJens Axboe 	struct request_queue *queue;
153a5664dadSMike Snitzer 	unsigned type;
1544a0b4ddfSMike Snitzer 	/* Protect queue and type against concurrent access. */
155a5664dadSMike Snitzer 	struct mutex type_lock;
156a5664dadSMike Snitzer 
15716f12266SMike Snitzer 	struct dm_target *immutable_target;
15836a0456fSAlasdair G Kergon 	struct target_type *immutable_target_type;
15936a0456fSAlasdair G Kergon 
1601da177e4SLinus Torvalds 	struct gendisk *disk;
1617e51f257SMike Anderson 	char name[16];
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	void *interface_ptr;
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	/*
1661da177e4SLinus Torvalds 	 * A list of ios that arrived while we were suspended.
1671da177e4SLinus Torvalds 	 */
168316d315bSNikanth Karthikesan 	atomic_t pending[2];
1691da177e4SLinus Torvalds 	wait_queue_head_t wait;
17053d5914fSMikulas Patocka 	struct work_struct work;
1711da177e4SLinus Torvalds 	struct bio_list deferred;
172022c2611SMikulas Patocka 	spinlock_t deferred_lock;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	/*
17529e4013dSTejun Heo 	 * Processing queue (flush)
176304f3f6aSMilan Broz 	 */
177304f3f6aSMilan Broz 	struct workqueue_struct *wq;
178304f3f6aSMilan Broz 
179304f3f6aSMilan Broz 	/*
1801da177e4SLinus Torvalds 	 * io objects are allocated from here.
1811da177e4SLinus Torvalds 	 */
1821da177e4SLinus Torvalds 	mempool_t *io_pool;
1831ae49ea2SMike Snitzer 	mempool_t *rq_pool;
1841da177e4SLinus Torvalds 
1859faf400fSStefan Bader 	struct bio_set *bs;
1869faf400fSStefan Bader 
1871da177e4SLinus Torvalds 	/*
1881da177e4SLinus Torvalds 	 * Event handling.
1891da177e4SLinus Torvalds 	 */
1901da177e4SLinus Torvalds 	atomic_t event_nr;
1911da177e4SLinus Torvalds 	wait_queue_head_t eventq;
1927a8c3d3bSMike Anderson 	atomic_t uevent_seq;
1937a8c3d3bSMike Anderson 	struct list_head uevent_list;
1947a8c3d3bSMike Anderson 	spinlock_t uevent_lock; /* Protect access to uevent_list */
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	/*
1971da177e4SLinus Torvalds 	 * freeze/thaw support require holding onto a super block
1981da177e4SLinus Torvalds 	 */
1991da177e4SLinus Torvalds 	struct super_block *frozen_sb;
200db8fef4fSMikulas Patocka 	struct block_device *bdev;
2013ac51e74SDarrick J. Wong 
2023ac51e74SDarrick J. Wong 	/* forced geometry settings */
2033ac51e74SDarrick J. Wong 	struct hd_geometry geometry;
204784aae73SMilan Broz 
2052995fa78SMikulas Patocka 	/* kobject and completion */
2062995fa78SMikulas Patocka 	struct dm_kobject_holder kobj_holder;
207be35f486SMikulas Patocka 
208d87f4c14STejun Heo 	/* zero-length flush that will be cloned and submitted to targets */
209d87f4c14STejun Heo 	struct bio flush_bio;
210fd2ed4d2SMikulas Patocka 
21196b26c8cSMikulas Patocka 	/* the number of internal suspends */
21296b26c8cSMikulas Patocka 	unsigned internal_suspend_count;
21396b26c8cSMikulas Patocka 
214fd2ed4d2SMikulas Patocka 	struct dm_stats stats;
2152eb6e1e3SKeith Busch 
2162eb6e1e3SKeith Busch 	struct kthread_worker kworker;
2172eb6e1e3SKeith Busch 	struct task_struct *kworker_task;
218de3ec86dSMike Snitzer 
219de3ec86dSMike Snitzer 	/* for request-based merge heuristic in dm_request_fn() */
2200ce65797SMike Snitzer 	unsigned seq_rq_merge_deadline_usecs;
221de3ec86dSMike Snitzer 	int last_rq_rw;
2220ce65797SMike Snitzer 	sector_t last_rq_pos;
2230ce65797SMike Snitzer 	ktime_t last_rq_start_time;
224bfebd1cdSMike Snitzer 
225bfebd1cdSMike Snitzer 	/* for blk-mq request-based DM support */
2261c357a1eSMike Snitzer 	struct blk_mq_tag_set *tag_set;
227*591ddcfcSMike Snitzer 	bool use_blk_mq:1;
228*591ddcfcSMike Snitzer 	bool init_tio_pdu:1;
2291da177e4SLinus Torvalds };
2301da177e4SLinus Torvalds 
23117e149b8SMike Snitzer #ifdef CONFIG_DM_MQ_DEFAULT
23217e149b8SMike Snitzer static bool use_blk_mq = true;
23317e149b8SMike Snitzer #else
23417e149b8SMike Snitzer static bool use_blk_mq = false;
23517e149b8SMike Snitzer #endif
23617e149b8SMike Snitzer 
237faad87dfSMike Snitzer #define DM_MQ_NR_HW_QUEUES 1
238faad87dfSMike Snitzer #define DM_MQ_QUEUE_DEPTH 2048
239faad87dfSMike Snitzer 
240faad87dfSMike Snitzer static unsigned dm_mq_nr_hw_queues = DM_MQ_NR_HW_QUEUES;
241faad87dfSMike Snitzer static unsigned dm_mq_queue_depth = DM_MQ_QUEUE_DEPTH;
242faad87dfSMike Snitzer 
24317e149b8SMike Snitzer bool dm_use_blk_mq(struct mapped_device *md)
24417e149b8SMike Snitzer {
24517e149b8SMike Snitzer 	return md->use_blk_mq;
24617e149b8SMike Snitzer }
247*591ddcfcSMike Snitzer EXPORT_SYMBOL_GPL(dm_use_blk_mq);
24817e149b8SMike Snitzer 
249e6ee8c0bSKiyoshi Ueda /*
250e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
251e6ee8c0bSKiyoshi Ueda  */
252e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
253e6ee8c0bSKiyoshi Ueda 	mempool_t *io_pool;
2541ae49ea2SMike Snitzer 	mempool_t *rq_pool;
255e6ee8c0bSKiyoshi Ueda 	struct bio_set *bs;
256e6ee8c0bSKiyoshi Ueda };
257e6ee8c0bSKiyoshi Ueda 
25886f1152bSBenjamin Marzinski struct table_device {
25986f1152bSBenjamin Marzinski 	struct list_head list;
26086f1152bSBenjamin Marzinski 	atomic_t count;
26186f1152bSBenjamin Marzinski 	struct dm_dev dm_dev;
26286f1152bSBenjamin Marzinski };
26386f1152bSBenjamin Marzinski 
2646cfa5857SMike Snitzer #define RESERVED_BIO_BASED_IOS		16
2656cfa5857SMike Snitzer #define RESERVED_REQUEST_BASED_IOS	256
266f4790826SMike Snitzer #define RESERVED_MAX_IOS		1024
267e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
2688fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
2691ae49ea2SMike Snitzer static struct kmem_cache *_rq_cache;
27094818742SKent Overstreet 
271f4790826SMike Snitzer /*
272e8603136SMike Snitzer  * Bio-based DM's mempools' reserved IOs set by the user.
273e8603136SMike Snitzer  */
274e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
275e8603136SMike Snitzer 
276e8603136SMike Snitzer /*
277f4790826SMike Snitzer  * Request-based DM's mempools' reserved IOs set by the user.
278f4790826SMike Snitzer  */
279f4790826SMike Snitzer static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
280f4790826SMike Snitzer 
28109c2d531SMike Snitzer static unsigned __dm_get_module_param(unsigned *module_param,
282f4790826SMike Snitzer 				      unsigned def, unsigned max)
283f4790826SMike Snitzer {
28409c2d531SMike Snitzer 	unsigned param = ACCESS_ONCE(*module_param);
28509c2d531SMike Snitzer 	unsigned modified_param = 0;
286f4790826SMike Snitzer 
28709c2d531SMike Snitzer 	if (!param)
28809c2d531SMike Snitzer 		modified_param = def;
28909c2d531SMike Snitzer 	else if (param > max)
29009c2d531SMike Snitzer 		modified_param = max;
291f4790826SMike Snitzer 
29209c2d531SMike Snitzer 	if (modified_param) {
29309c2d531SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
29409c2d531SMike Snitzer 		param = modified_param;
295f4790826SMike Snitzer 	}
296f4790826SMike Snitzer 
29709c2d531SMike Snitzer 	return param;
298f4790826SMike Snitzer }
299f4790826SMike Snitzer 
300e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void)
301e8603136SMike Snitzer {
30209c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_bio_based_ios,
303e8603136SMike Snitzer 				     RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
304e8603136SMike Snitzer }
305e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
306e8603136SMike Snitzer 
307f4790826SMike Snitzer unsigned dm_get_reserved_rq_based_ios(void)
308f4790826SMike Snitzer {
30909c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_rq_based_ios,
310f4790826SMike Snitzer 				     RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
311f4790826SMike Snitzer }
312f4790826SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
313f4790826SMike Snitzer 
314faad87dfSMike Snitzer static unsigned dm_get_blk_mq_nr_hw_queues(void)
315faad87dfSMike Snitzer {
316faad87dfSMike Snitzer 	return __dm_get_module_param(&dm_mq_nr_hw_queues, 1, 32);
317faad87dfSMike Snitzer }
318faad87dfSMike Snitzer 
319faad87dfSMike Snitzer static unsigned dm_get_blk_mq_queue_depth(void)
320faad87dfSMike Snitzer {
321faad87dfSMike Snitzer 	return __dm_get_module_param(&dm_mq_queue_depth,
322faad87dfSMike Snitzer 				     DM_MQ_QUEUE_DEPTH, BLK_MQ_MAX_DEPTH);
323faad87dfSMike Snitzer }
324faad87dfSMike Snitzer 
3251da177e4SLinus Torvalds static int __init local_init(void)
3261da177e4SLinus Torvalds {
32751157b4aSKiyoshi Ueda 	int r = -ENOMEM;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
330028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
3311da177e4SLinus Torvalds 	if (!_io_cache)
33251157b4aSKiyoshi Ueda 		return r;
3331da177e4SLinus Torvalds 
3348fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
3358fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
336dba14160SMikulas Patocka 		goto out_free_io_cache;
3378fbf26adSKiyoshi Ueda 
338eca7ee6dSMike Snitzer 	_rq_cache = kmem_cache_create("dm_old_clone_request", sizeof(struct request),
3391ae49ea2SMike Snitzer 				      __alignof__(struct request), 0, NULL);
3401ae49ea2SMike Snitzer 	if (!_rq_cache)
3411ae49ea2SMike Snitzer 		goto out_free_rq_tio_cache;
3421ae49ea2SMike Snitzer 
34351e5b2bdSMike Anderson 	r = dm_uevent_init();
34451157b4aSKiyoshi Ueda 	if (r)
3451ae49ea2SMike Snitzer 		goto out_free_rq_cache;
34651e5b2bdSMike Anderson 
347acfe0ad7SMikulas Patocka 	deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
348acfe0ad7SMikulas Patocka 	if (!deferred_remove_workqueue) {
349acfe0ad7SMikulas Patocka 		r = -ENOMEM;
350acfe0ad7SMikulas Patocka 		goto out_uevent_exit;
351acfe0ad7SMikulas Patocka 	}
352acfe0ad7SMikulas Patocka 
3531da177e4SLinus Torvalds 	_major = major;
3541da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
35551157b4aSKiyoshi Ueda 	if (r < 0)
356acfe0ad7SMikulas Patocka 		goto out_free_workqueue;
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	if (!_major)
3591da177e4SLinus Torvalds 		_major = r;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	return 0;
36251157b4aSKiyoshi Ueda 
363acfe0ad7SMikulas Patocka out_free_workqueue:
364acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
36551157b4aSKiyoshi Ueda out_uevent_exit:
36651157b4aSKiyoshi Ueda 	dm_uevent_exit();
3671ae49ea2SMike Snitzer out_free_rq_cache:
3681ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
3698fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
3708fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
37151157b4aSKiyoshi Ueda out_free_io_cache:
37251157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
37351157b4aSKiyoshi Ueda 
37451157b4aSKiyoshi Ueda 	return r;
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds static void local_exit(void)
3781da177e4SLinus Torvalds {
3792c140a24SMikulas Patocka 	flush_scheduled_work();
380acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
3812c140a24SMikulas Patocka 
3821ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
3838fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
3841da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
38500d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
38651e5b2bdSMike Anderson 	dm_uevent_exit();
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	_major = 0;
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	DMINFO("cleaned up");
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds 
393b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
3941da177e4SLinus Torvalds 	local_init,
3951da177e4SLinus Torvalds 	dm_target_init,
3961da177e4SLinus Torvalds 	dm_linear_init,
3971da177e4SLinus Torvalds 	dm_stripe_init,
398952b3557SMikulas Patocka 	dm_io_init,
399945fa4d2SMikulas Patocka 	dm_kcopyd_init,
4001da177e4SLinus Torvalds 	dm_interface_init,
401fd2ed4d2SMikulas Patocka 	dm_statistics_init,
4021da177e4SLinus Torvalds };
4031da177e4SLinus Torvalds 
404b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
4051da177e4SLinus Torvalds 	local_exit,
4061da177e4SLinus Torvalds 	dm_target_exit,
4071da177e4SLinus Torvalds 	dm_linear_exit,
4081da177e4SLinus Torvalds 	dm_stripe_exit,
409952b3557SMikulas Patocka 	dm_io_exit,
410945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
4111da177e4SLinus Torvalds 	dm_interface_exit,
412fd2ed4d2SMikulas Patocka 	dm_statistics_exit,
4131da177e4SLinus Torvalds };
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds static int __init dm_init(void)
4161da177e4SLinus Torvalds {
4171da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	int r, i;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
4221da177e4SLinus Torvalds 		r = _inits[i]();
4231da177e4SLinus Torvalds 		if (r)
4241da177e4SLinus Torvalds 			goto bad;
4251da177e4SLinus Torvalds 	}
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	return 0;
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds       bad:
4301da177e4SLinus Torvalds 	while (i--)
4311da177e4SLinus Torvalds 		_exits[i]();
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	return r;
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds static void __exit dm_exit(void)
4371da177e4SLinus Torvalds {
4381da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	while (i--)
4411da177e4SLinus Torvalds 		_exits[i]();
442d15b774cSAlasdair G Kergon 
443d15b774cSAlasdair G Kergon 	/*
444d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
445d15b774cSAlasdair G Kergon 	 */
446d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds /*
4501da177e4SLinus Torvalds  * Block device functions
4511da177e4SLinus Torvalds  */
452432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
453432a212cSMike Anderson {
454432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
455432a212cSMike Anderson }
456432a212cSMike Anderson 
457fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
4581da177e4SLinus Torvalds {
4591da177e4SLinus Torvalds 	struct mapped_device *md;
4601da177e4SLinus Torvalds 
461fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
462fba9f90eSJeff Mahoney 
463fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
464fba9f90eSJeff Mahoney 	if (!md)
465fba9f90eSJeff Mahoney 		goto out;
466fba9f90eSJeff Mahoney 
4675c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
468432a212cSMike Anderson 	    dm_deleting_md(md)) {
469fba9f90eSJeff Mahoney 		md = NULL;
470fba9f90eSJeff Mahoney 		goto out;
471fba9f90eSJeff Mahoney 	}
472fba9f90eSJeff Mahoney 
4731da177e4SLinus Torvalds 	dm_get(md);
4745c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
475fba9f90eSJeff Mahoney out:
476fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
477fba9f90eSJeff Mahoney 
478fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
4791da177e4SLinus Torvalds }
4801da177e4SLinus Torvalds 
481db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode)
4821da177e4SLinus Torvalds {
48363a4f065SMike Snitzer 	struct mapped_device *md;
4846e9624b8SArnd Bergmann 
4854a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
4864a1aeb98SMilan Broz 
48763a4f065SMike Snitzer 	md = disk->private_data;
48863a4f065SMike Snitzer 	if (WARN_ON(!md))
48963a4f065SMike Snitzer 		goto out;
49063a4f065SMike Snitzer 
4912c140a24SMikulas Patocka 	if (atomic_dec_and_test(&md->open_count) &&
4922c140a24SMikulas Patocka 	    (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
493acfe0ad7SMikulas Patocka 		queue_work(deferred_remove_workqueue, &deferred_remove_work);
4942c140a24SMikulas Patocka 
4951da177e4SLinus Torvalds 	dm_put(md);
49663a4f065SMike Snitzer out:
4974a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
4981da177e4SLinus Torvalds }
4991da177e4SLinus Torvalds 
5005c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
5015c6bd75dSAlasdair G Kergon {
5025c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
5035c6bd75dSAlasdair G Kergon }
5045c6bd75dSAlasdair G Kergon 
5055c6bd75dSAlasdair G Kergon /*
5065c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
5075c6bd75dSAlasdair G Kergon  */
5082c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
5095c6bd75dSAlasdair G Kergon {
5105c6bd75dSAlasdair G Kergon 	int r = 0;
5115c6bd75dSAlasdair G Kergon 
5125c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
5135c6bd75dSAlasdair G Kergon 
5142c140a24SMikulas Patocka 	if (dm_open_count(md)) {
5155c6bd75dSAlasdair G Kergon 		r = -EBUSY;
5162c140a24SMikulas Patocka 		if (mark_deferred)
5172c140a24SMikulas Patocka 			set_bit(DMF_DEFERRED_REMOVE, &md->flags);
5182c140a24SMikulas Patocka 	} else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
5192c140a24SMikulas Patocka 		r = -EEXIST;
5205c6bd75dSAlasdair G Kergon 	else
5215c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
5225c6bd75dSAlasdair G Kergon 
5235c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
5245c6bd75dSAlasdair G Kergon 
5255c6bd75dSAlasdair G Kergon 	return r;
5265c6bd75dSAlasdair G Kergon }
5275c6bd75dSAlasdair G Kergon 
5282c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md)
5292c140a24SMikulas Patocka {
5302c140a24SMikulas Patocka 	int r = 0;
5312c140a24SMikulas Patocka 
5322c140a24SMikulas Patocka 	spin_lock(&_minor_lock);
5332c140a24SMikulas Patocka 
5342c140a24SMikulas Patocka 	if (test_bit(DMF_DELETING, &md->flags))
5352c140a24SMikulas Patocka 		r = -EBUSY;
5362c140a24SMikulas Patocka 	else
5372c140a24SMikulas Patocka 		clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
5382c140a24SMikulas Patocka 
5392c140a24SMikulas Patocka 	spin_unlock(&_minor_lock);
5402c140a24SMikulas Patocka 
5412c140a24SMikulas Patocka 	return r;
5422c140a24SMikulas Patocka }
5432c140a24SMikulas Patocka 
5442c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w)
5452c140a24SMikulas Patocka {
5462c140a24SMikulas Patocka 	dm_deferred_remove();
5472c140a24SMikulas Patocka }
5482c140a24SMikulas Patocka 
549fd2ed4d2SMikulas Patocka sector_t dm_get_size(struct mapped_device *md)
550fd2ed4d2SMikulas Patocka {
551fd2ed4d2SMikulas Patocka 	return get_capacity(md->disk);
552fd2ed4d2SMikulas Patocka }
553fd2ed4d2SMikulas Patocka 
5549974fa2cSMike Snitzer struct request_queue *dm_get_md_queue(struct mapped_device *md)
5559974fa2cSMike Snitzer {
5569974fa2cSMike Snitzer 	return md->queue;
5579974fa2cSMike Snitzer }
5589974fa2cSMike Snitzer 
559fd2ed4d2SMikulas Patocka struct dm_stats *dm_get_stats(struct mapped_device *md)
560fd2ed4d2SMikulas Patocka {
561fd2ed4d2SMikulas Patocka 	return &md->stats;
562fd2ed4d2SMikulas Patocka }
563fd2ed4d2SMikulas Patocka 
5643ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5653ac51e74SDarrick J. Wong {
5663ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
5673ac51e74SDarrick J. Wong 
5683ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
5693ac51e74SDarrick J. Wong }
5703ac51e74SDarrick J. Wong 
571956a4025SMike Snitzer static int dm_grab_bdev_for_ioctl(struct mapped_device *md,
57266482026SMike Snitzer 				  struct block_device **bdev,
573956a4025SMike Snitzer 				  fmode_t *mode)
574aa129a22SMilan Broz {
57566482026SMike Snitzer 	struct dm_target *tgt;
5766c182cd8SHannes Reinecke 	struct dm_table *map;
577956a4025SMike Snitzer 	int srcu_idx, r;
578aa129a22SMilan Broz 
5796c182cd8SHannes Reinecke retry:
580e56f81e0SChristoph Hellwig 	r = -ENOTTY;
581956a4025SMike Snitzer 	map = dm_get_live_table(md, &srcu_idx);
582aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
583aa129a22SMilan Broz 		goto out;
584aa129a22SMilan Broz 
585aa129a22SMilan Broz 	/* We only support devices that have a single target */
586aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
587aa129a22SMilan Broz 		goto out;
588aa129a22SMilan Broz 
58966482026SMike Snitzer 	tgt = dm_table_get_target(map, 0);
59066482026SMike Snitzer 	if (!tgt->type->prepare_ioctl)
5914d341d82SMike Snitzer 		goto out;
592aa129a22SMilan Broz 
5934f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
594aa129a22SMilan Broz 		r = -EAGAIN;
595aa129a22SMilan Broz 		goto out;
596aa129a22SMilan Broz 	}
597aa129a22SMilan Broz 
59866482026SMike Snitzer 	r = tgt->type->prepare_ioctl(tgt, bdev, mode);
599e56f81e0SChristoph Hellwig 	if (r < 0)
600e56f81e0SChristoph Hellwig 		goto out;
601e56f81e0SChristoph Hellwig 
602956a4025SMike Snitzer 	bdgrab(*bdev);
603956a4025SMike Snitzer 	dm_put_live_table(md, srcu_idx);
604e56f81e0SChristoph Hellwig 	return r;
605aa129a22SMilan Broz 
606aa129a22SMilan Broz out:
607956a4025SMike Snitzer 	dm_put_live_table(md, srcu_idx);
6085bbbfdf6SJunichi Nomura 	if (r == -ENOTCONN && !fatal_signal_pending(current)) {
6096c182cd8SHannes Reinecke 		msleep(10);
6106c182cd8SHannes Reinecke 		goto retry;
6116c182cd8SHannes Reinecke 	}
612e56f81e0SChristoph Hellwig 	return r;
613e56f81e0SChristoph Hellwig }
6146c182cd8SHannes Reinecke 
615e56f81e0SChristoph Hellwig static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
616e56f81e0SChristoph Hellwig 			unsigned int cmd, unsigned long arg)
617e56f81e0SChristoph Hellwig {
618e56f81e0SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
619956a4025SMike Snitzer 	int r;
620e56f81e0SChristoph Hellwig 
621956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
622e56f81e0SChristoph Hellwig 	if (r < 0)
623e56f81e0SChristoph Hellwig 		return r;
624e56f81e0SChristoph Hellwig 
625e56f81e0SChristoph Hellwig 	if (r > 0) {
626e56f81e0SChristoph Hellwig 		/*
627e56f81e0SChristoph Hellwig 		 * Target determined this ioctl is being issued against
628e56f81e0SChristoph Hellwig 		 * a logical partition of the parent bdev; so extra
629e56f81e0SChristoph Hellwig 		 * validation is needed.
630e56f81e0SChristoph Hellwig 		 */
631e56f81e0SChristoph Hellwig 		r = scsi_verify_blk_ioctl(NULL, cmd);
632e56f81e0SChristoph Hellwig 		if (r)
633e56f81e0SChristoph Hellwig 			goto out;
634e56f81e0SChristoph Hellwig 	}
635e56f81e0SChristoph Hellwig 
63666482026SMike Snitzer 	r =  __blkdev_driver_ioctl(bdev, mode, cmd, arg);
637e56f81e0SChristoph Hellwig out:
638956a4025SMike Snitzer 	bdput(bdev);
639aa129a22SMilan Broz 	return r;
640aa129a22SMilan Broz }
641aa129a22SMilan Broz 
642028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
6431da177e4SLinus Torvalds {
6441da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
6451da177e4SLinus Torvalds }
6461da177e4SLinus Torvalds 
647028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
6481da177e4SLinus Torvalds {
6491da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
6501da177e4SLinus Torvalds }
6511da177e4SLinus Torvalds 
652028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
6531da177e4SLinus Torvalds {
654dba14160SMikulas Patocka 	bio_put(&tio->clone);
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds 
657eca7ee6dSMike Snitzer static struct dm_rq_target_io *alloc_old_rq_tio(struct mapped_device *md,
65808885643SKiyoshi Ueda 						gfp_t gfp_mask)
659cec47e3dSKiyoshi Ueda {
6605f015204SJun'ichi Nomura 	return mempool_alloc(md->io_pool, gfp_mask);
661cec47e3dSKiyoshi Ueda }
662cec47e3dSKiyoshi Ueda 
663eca7ee6dSMike Snitzer static void free_old_rq_tio(struct dm_rq_target_io *tio)
664cec47e3dSKiyoshi Ueda {
6655f015204SJun'ichi Nomura 	mempool_free(tio, tio->md->io_pool);
666cec47e3dSKiyoshi Ueda }
667cec47e3dSKiyoshi Ueda 
668eca7ee6dSMike Snitzer static struct request *alloc_old_clone_request(struct mapped_device *md,
6691ae49ea2SMike Snitzer 					       gfp_t gfp_mask)
6701ae49ea2SMike Snitzer {
6711ae49ea2SMike Snitzer 	return mempool_alloc(md->rq_pool, gfp_mask);
6721ae49ea2SMike Snitzer }
6731ae49ea2SMike Snitzer 
674eca7ee6dSMike Snitzer static void free_old_clone_request(struct mapped_device *md, struct request *rq)
6751ae49ea2SMike Snitzer {
6761ae49ea2SMike Snitzer 	mempool_free(rq, md->rq_pool);
6771ae49ea2SMike Snitzer }
6781ae49ea2SMike Snitzer 
67990abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md)
68090abb8c4SKiyoshi Ueda {
68190abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
68290abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
68390abb8c4SKiyoshi Ueda }
68490abb8c4SKiyoshi Ueda 
6853eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
6863eaf840eSJun'ichi "Nick" Nomura {
6873eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
688fd2ed4d2SMikulas Patocka 	struct bio *bio = io->bio;
689c9959059STejun Heo 	int cpu;
690fd2ed4d2SMikulas Patocka 	int rw = bio_data_dir(bio);
6913eaf840eSJun'ichi "Nick" Nomura 
6923eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
6933eaf840eSJun'ichi "Nick" Nomura 
694074a7acaSTejun Heo 	cpu = part_stat_lock();
695074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
696074a7acaSTejun Heo 	part_stat_unlock();
6971e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw],
6981e9bb880SShaohua Li 		atomic_inc_return(&md->pending[rw]));
699fd2ed4d2SMikulas Patocka 
700fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
7014f024f37SKent Overstreet 		dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
702fd2ed4d2SMikulas Patocka 				    bio_sectors(bio), false, 0, &io->stats_aux);
7033eaf840eSJun'ichi "Nick" Nomura }
7043eaf840eSJun'ichi "Nick" Nomura 
705d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
7063eaf840eSJun'ichi "Nick" Nomura {
7073eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
7083eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
7093eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
71018c0b223SGu Zheng 	int pending;
7113eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
7123eaf840eSJun'ichi "Nick" Nomura 
71318c0b223SGu Zheng 	generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time);
7143eaf840eSJun'ichi "Nick" Nomura 
715fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
7164f024f37SKent Overstreet 		dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
717fd2ed4d2SMikulas Patocka 				    bio_sectors(bio), true, duration, &io->stats_aux);
718fd2ed4d2SMikulas Patocka 
719af7e466aSMikulas Patocka 	/*
720af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
721d87f4c14STejun Heo 	 * a flush.
722af7e466aSMikulas Patocka 	 */
7231e9bb880SShaohua Li 	pending = atomic_dec_return(&md->pending[rw]);
7241e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
725316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
7263eaf840eSJun'ichi "Nick" Nomura 
727d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
728d221d2e7SMikulas Patocka 	if (!pending)
729d221d2e7SMikulas Patocka 		wake_up(&md->wait);
7303eaf840eSJun'ichi "Nick" Nomura }
7313eaf840eSJun'ichi "Nick" Nomura 
7321da177e4SLinus Torvalds /*
7331da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
7341da177e4SLinus Torvalds  */
73592c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
7361da177e4SLinus Torvalds {
73705447420SKiyoshi Ueda 	unsigned long flags;
7381da177e4SLinus Torvalds 
73905447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
7401da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
74105447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
74292c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
7431da177e4SLinus Torvalds }
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds /*
7461da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
7471da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
74883d5e5b0SMikulas Patocka  * dm_put_live_table() when finished.
7491da177e4SLinus Torvalds  */
75083d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
7511da177e4SLinus Torvalds {
75283d5e5b0SMikulas Patocka 	*srcu_idx = srcu_read_lock(&md->io_barrier);
7531da177e4SLinus Torvalds 
75483d5e5b0SMikulas Patocka 	return srcu_dereference(md->map, &md->io_barrier);
75583d5e5b0SMikulas Patocka }
7561da177e4SLinus Torvalds 
75783d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
75883d5e5b0SMikulas Patocka {
75983d5e5b0SMikulas Patocka 	srcu_read_unlock(&md->io_barrier, srcu_idx);
76083d5e5b0SMikulas Patocka }
76183d5e5b0SMikulas Patocka 
76283d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md)
76383d5e5b0SMikulas Patocka {
76483d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
76583d5e5b0SMikulas Patocka 	synchronize_rcu_expedited();
76683d5e5b0SMikulas Patocka }
76783d5e5b0SMikulas Patocka 
76883d5e5b0SMikulas Patocka /*
76983d5e5b0SMikulas Patocka  * A fast alternative to dm_get_live_table/dm_put_live_table.
77083d5e5b0SMikulas Patocka  * The caller must not block between these two functions.
77183d5e5b0SMikulas Patocka  */
77283d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
77383d5e5b0SMikulas Patocka {
77483d5e5b0SMikulas Patocka 	rcu_read_lock();
77583d5e5b0SMikulas Patocka 	return rcu_dereference(md->map);
77683d5e5b0SMikulas Patocka }
77783d5e5b0SMikulas Patocka 
77883d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
77983d5e5b0SMikulas Patocka {
78083d5e5b0SMikulas Patocka 	rcu_read_unlock();
7811da177e4SLinus Torvalds }
7821da177e4SLinus Torvalds 
7833ac51e74SDarrick J. Wong /*
78486f1152bSBenjamin Marzinski  * Open a table device so we can use it as a map destination.
78586f1152bSBenjamin Marzinski  */
78686f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev,
78786f1152bSBenjamin Marzinski 			     struct mapped_device *md)
78886f1152bSBenjamin Marzinski {
78986f1152bSBenjamin Marzinski 	static char *_claim_ptr = "I belong to device-mapper";
79086f1152bSBenjamin Marzinski 	struct block_device *bdev;
79186f1152bSBenjamin Marzinski 
79286f1152bSBenjamin Marzinski 	int r;
79386f1152bSBenjamin Marzinski 
79486f1152bSBenjamin Marzinski 	BUG_ON(td->dm_dev.bdev);
79586f1152bSBenjamin Marzinski 
79686f1152bSBenjamin Marzinski 	bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
79786f1152bSBenjamin Marzinski 	if (IS_ERR(bdev))
79886f1152bSBenjamin Marzinski 		return PTR_ERR(bdev);
79986f1152bSBenjamin Marzinski 
80086f1152bSBenjamin Marzinski 	r = bd_link_disk_holder(bdev, dm_disk(md));
80186f1152bSBenjamin Marzinski 	if (r) {
80286f1152bSBenjamin Marzinski 		blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
80386f1152bSBenjamin Marzinski 		return r;
80486f1152bSBenjamin Marzinski 	}
80586f1152bSBenjamin Marzinski 
80686f1152bSBenjamin Marzinski 	td->dm_dev.bdev = bdev;
80786f1152bSBenjamin Marzinski 	return 0;
80886f1152bSBenjamin Marzinski }
80986f1152bSBenjamin Marzinski 
81086f1152bSBenjamin Marzinski /*
81186f1152bSBenjamin Marzinski  * Close a table device that we've been using.
81286f1152bSBenjamin Marzinski  */
81386f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md)
81486f1152bSBenjamin Marzinski {
81586f1152bSBenjamin Marzinski 	if (!td->dm_dev.bdev)
81686f1152bSBenjamin Marzinski 		return;
81786f1152bSBenjamin Marzinski 
81886f1152bSBenjamin Marzinski 	bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
81986f1152bSBenjamin Marzinski 	blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
82086f1152bSBenjamin Marzinski 	td->dm_dev.bdev = NULL;
82186f1152bSBenjamin Marzinski }
82286f1152bSBenjamin Marzinski 
82386f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev,
82486f1152bSBenjamin Marzinski 					      fmode_t mode) {
82586f1152bSBenjamin Marzinski 	struct table_device *td;
82686f1152bSBenjamin Marzinski 
82786f1152bSBenjamin Marzinski 	list_for_each_entry(td, l, list)
82886f1152bSBenjamin Marzinski 		if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
82986f1152bSBenjamin Marzinski 			return td;
83086f1152bSBenjamin Marzinski 
83186f1152bSBenjamin Marzinski 	return NULL;
83286f1152bSBenjamin Marzinski }
83386f1152bSBenjamin Marzinski 
83486f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
83586f1152bSBenjamin Marzinski 			struct dm_dev **result) {
83686f1152bSBenjamin Marzinski 	int r;
83786f1152bSBenjamin Marzinski 	struct table_device *td;
83886f1152bSBenjamin Marzinski 
83986f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
84086f1152bSBenjamin Marzinski 	td = find_table_device(&md->table_devices, dev, mode);
84186f1152bSBenjamin Marzinski 	if (!td) {
84286f1152bSBenjamin Marzinski 		td = kmalloc(sizeof(*td), GFP_KERNEL);
84386f1152bSBenjamin Marzinski 		if (!td) {
84486f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
84586f1152bSBenjamin Marzinski 			return -ENOMEM;
84686f1152bSBenjamin Marzinski 		}
84786f1152bSBenjamin Marzinski 
84886f1152bSBenjamin Marzinski 		td->dm_dev.mode = mode;
84986f1152bSBenjamin Marzinski 		td->dm_dev.bdev = NULL;
85086f1152bSBenjamin Marzinski 
85186f1152bSBenjamin Marzinski 		if ((r = open_table_device(td, dev, md))) {
85286f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
85386f1152bSBenjamin Marzinski 			kfree(td);
85486f1152bSBenjamin Marzinski 			return r;
85586f1152bSBenjamin Marzinski 		}
85686f1152bSBenjamin Marzinski 
85786f1152bSBenjamin Marzinski 		format_dev_t(td->dm_dev.name, dev);
85886f1152bSBenjamin Marzinski 
85986f1152bSBenjamin Marzinski 		atomic_set(&td->count, 0);
86086f1152bSBenjamin Marzinski 		list_add(&td->list, &md->table_devices);
86186f1152bSBenjamin Marzinski 	}
86286f1152bSBenjamin Marzinski 	atomic_inc(&td->count);
86386f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
86486f1152bSBenjamin Marzinski 
86586f1152bSBenjamin Marzinski 	*result = &td->dm_dev;
86686f1152bSBenjamin Marzinski 	return 0;
86786f1152bSBenjamin Marzinski }
86886f1152bSBenjamin Marzinski EXPORT_SYMBOL_GPL(dm_get_table_device);
86986f1152bSBenjamin Marzinski 
87086f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
87186f1152bSBenjamin Marzinski {
87286f1152bSBenjamin Marzinski 	struct table_device *td = container_of(d, struct table_device, dm_dev);
87386f1152bSBenjamin Marzinski 
87486f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
87586f1152bSBenjamin Marzinski 	if (atomic_dec_and_test(&td->count)) {
87686f1152bSBenjamin Marzinski 		close_table_device(td, md);
87786f1152bSBenjamin Marzinski 		list_del(&td->list);
87886f1152bSBenjamin Marzinski 		kfree(td);
87986f1152bSBenjamin Marzinski 	}
88086f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
88186f1152bSBenjamin Marzinski }
88286f1152bSBenjamin Marzinski EXPORT_SYMBOL(dm_put_table_device);
88386f1152bSBenjamin Marzinski 
88486f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices)
88586f1152bSBenjamin Marzinski {
88686f1152bSBenjamin Marzinski 	struct list_head *tmp, *next;
88786f1152bSBenjamin Marzinski 
88886f1152bSBenjamin Marzinski 	list_for_each_safe(tmp, next, devices) {
88986f1152bSBenjamin Marzinski 		struct table_device *td = list_entry(tmp, struct table_device, list);
89086f1152bSBenjamin Marzinski 
89186f1152bSBenjamin Marzinski 		DMWARN("dm_destroy: %s still exists with %d references",
89286f1152bSBenjamin Marzinski 		       td->dm_dev.name, atomic_read(&td->count));
89386f1152bSBenjamin Marzinski 		kfree(td);
89486f1152bSBenjamin Marzinski 	}
89586f1152bSBenjamin Marzinski }
89686f1152bSBenjamin Marzinski 
89786f1152bSBenjamin Marzinski /*
8983ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
8993ac51e74SDarrick J. Wong  */
9003ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
9013ac51e74SDarrick J. Wong {
9023ac51e74SDarrick J. Wong 	*geo = md->geometry;
9033ac51e74SDarrick J. Wong 
9043ac51e74SDarrick J. Wong 	return 0;
9053ac51e74SDarrick J. Wong }
9063ac51e74SDarrick J. Wong 
9073ac51e74SDarrick J. Wong /*
9083ac51e74SDarrick J. Wong  * Set the geometry of a device.
9093ac51e74SDarrick J. Wong  */
9103ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
9113ac51e74SDarrick J. Wong {
9123ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
9133ac51e74SDarrick J. Wong 
9143ac51e74SDarrick J. Wong 	if (geo->start > sz) {
9153ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
9163ac51e74SDarrick J. Wong 		return -EINVAL;
9173ac51e74SDarrick J. Wong 	}
9183ac51e74SDarrick J. Wong 
9193ac51e74SDarrick J. Wong 	md->geometry = *geo;
9203ac51e74SDarrick J. Wong 
9213ac51e74SDarrick J. Wong 	return 0;
9223ac51e74SDarrick J. Wong }
9233ac51e74SDarrick J. Wong 
9241da177e4SLinus Torvalds /*-----------------------------------------------------------------
9251da177e4SLinus Torvalds  * CRUD START:
9261da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
9271da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
9281da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
9291da177e4SLinus Torvalds  *   interests of getting something for people to use I give
9301da177e4SLinus Torvalds  *   you this clearly demarcated crap.
9311da177e4SLinus Torvalds  *---------------------------------------------------------------*/
9321da177e4SLinus Torvalds 
9332e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
9342e93ccc1SKiyoshi Ueda {
9352e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
9362e93ccc1SKiyoshi Ueda }
9372e93ccc1SKiyoshi Ueda 
9381da177e4SLinus Torvalds /*
9391da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
9401da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
9411da177e4SLinus Torvalds  */
942858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
9431da177e4SLinus Torvalds {
9442e93ccc1SKiyoshi Ueda 	unsigned long flags;
945b35f8caaSMilan Broz 	int io_error;
946b35f8caaSMilan Broz 	struct bio *bio;
947b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
9482e93ccc1SKiyoshi Ueda 
9492e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
950f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
951f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
952f88fb981SKiyoshi Ueda 		if (!(io->error > 0 && __noflush_suspending(md)))
9531da177e4SLinus Torvalds 			io->error = error;
954f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
955f88fb981SKiyoshi Ueda 	}
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
9582e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
9592e93ccc1SKiyoshi Ueda 			/*
9602e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
9612e93ccc1SKiyoshi Ueda 			 */
962022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
9636a8736d1STejun Heo 			if (__noflush_suspending(md))
9646a8736d1STejun Heo 				bio_list_add_head(&md->deferred, io->bio);
9656a8736d1STejun Heo 			else
9662e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
9672e93ccc1SKiyoshi Ueda 				io->error = -EIO;
968022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
9692e93ccc1SKiyoshi Ueda 		}
9702e93ccc1SKiyoshi Ueda 
971b35f8caaSMilan Broz 		io_error = io->error;
972b35f8caaSMilan Broz 		bio = io->bio;
973af7e466aSMikulas Patocka 		end_io_acct(io);
974a97f925aSMikulas Patocka 		free_io(md, io);
9751da177e4SLinus Torvalds 
9766a8736d1STejun Heo 		if (io_error == DM_ENDIO_REQUEUE)
9776a8736d1STejun Heo 			return;
9786a8736d1STejun Heo 
9794f024f37SKent Overstreet 		if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
9801da177e4SLinus Torvalds 			/*
9816a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
9826a8736d1STejun Heo 			 * without REQ_FLUSH.
9831da177e4SLinus Torvalds 			 */
9846a8736d1STejun Heo 			bio->bi_rw &= ~REQ_FLUSH;
9856a8736d1STejun Heo 			queue_io(md, bio);
9865f3ea37cSArnaldo Carvalho de Melo 		} else {
987b372d360SMike Snitzer 			/* done with normal IO or empty flush */
9880a82a8d1SLinus Torvalds 			trace_block_bio_complete(md->queue, bio, io_error);
9894246a0b6SChristoph Hellwig 			bio->bi_error = io_error;
9904246a0b6SChristoph Hellwig 			bio_endio(bio);
9912e93ccc1SKiyoshi Ueda 		}
9921da177e4SLinus Torvalds 	}
993af7e466aSMikulas Patocka }
9941da177e4SLinus Torvalds 
9957eee4ae2SMike Snitzer static void disable_write_same(struct mapped_device *md)
9967eee4ae2SMike Snitzer {
9977eee4ae2SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
9987eee4ae2SMike Snitzer 
9997eee4ae2SMike Snitzer 	/* device doesn't really support WRITE SAME, disable it */
10007eee4ae2SMike Snitzer 	limits->max_write_same_sectors = 0;
10017eee4ae2SMike Snitzer }
10027eee4ae2SMike Snitzer 
10034246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio)
10041da177e4SLinus Torvalds {
10054246a0b6SChristoph Hellwig 	int error = bio->bi_error;
10065164beceSzhendong chen 	int r = error;
1007bfc6d41cSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1008b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
10099faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
10101da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 	if (endio) {
10137de3ee57SMikulas Patocka 		r = endio(tio->ti, bio, error);
10142e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
10152e93ccc1SKiyoshi Ueda 			/*
10162e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
10172e93ccc1SKiyoshi Ueda 			 * in dec_pending().
10182e93ccc1SKiyoshi Ueda 			 */
10191da177e4SLinus Torvalds 			error = r;
102045cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
102145cbcd79SKiyoshi Ueda 			/* The target will handle the io */
10226712ecf8SNeilBrown 			return;
102345cbcd79SKiyoshi Ueda 		else if (r) {
102445cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
102545cbcd79SKiyoshi Ueda 			BUG();
102645cbcd79SKiyoshi Ueda 		}
10271da177e4SLinus Torvalds 	}
10281da177e4SLinus Torvalds 
10297eee4ae2SMike Snitzer 	if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) &&
10307eee4ae2SMike Snitzer 		     !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
10317eee4ae2SMike Snitzer 		disable_write_same(md);
10327eee4ae2SMike Snitzer 
10339faf400fSStefan Bader 	free_tio(md, tio);
1034b35f8caaSMilan Broz 	dec_pending(io, error);
10351da177e4SLinus Torvalds }
10361da177e4SLinus Torvalds 
103778d8e58aSMike Snitzer /*
103878d8e58aSMike Snitzer  * Partial completion handling for request-based dm
103978d8e58aSMike Snitzer  */
10404246a0b6SChristoph Hellwig static void end_clone_bio(struct bio *clone)
104178d8e58aSMike Snitzer {
104278d8e58aSMike Snitzer 	struct dm_rq_clone_bio_info *info =
104378d8e58aSMike Snitzer 		container_of(clone, struct dm_rq_clone_bio_info, clone);
104478d8e58aSMike Snitzer 	struct dm_rq_target_io *tio = info->tio;
104578d8e58aSMike Snitzer 	struct bio *bio = info->orig;
104678d8e58aSMike Snitzer 	unsigned int nr_bytes = info->orig->bi_iter.bi_size;
104750887bd1SJunichi Nomura 	int error = clone->bi_error;
104878d8e58aSMike Snitzer 
104978d8e58aSMike Snitzer 	bio_put(clone);
105078d8e58aSMike Snitzer 
105178d8e58aSMike Snitzer 	if (tio->error)
105278d8e58aSMike Snitzer 		/*
105378d8e58aSMike Snitzer 		 * An error has already been detected on the request.
105478d8e58aSMike Snitzer 		 * Once error occurred, just let clone->end_io() handle
105578d8e58aSMike Snitzer 		 * the remainder.
105678d8e58aSMike Snitzer 		 */
105778d8e58aSMike Snitzer 		return;
105850887bd1SJunichi Nomura 	else if (error) {
105978d8e58aSMike Snitzer 		/*
106078d8e58aSMike Snitzer 		 * Don't notice the error to the upper layer yet.
106178d8e58aSMike Snitzer 		 * The error handling decision is made by the target driver,
106278d8e58aSMike Snitzer 		 * when the request is completed.
106378d8e58aSMike Snitzer 		 */
106450887bd1SJunichi Nomura 		tio->error = error;
106578d8e58aSMike Snitzer 		return;
106678d8e58aSMike Snitzer 	}
106778d8e58aSMike Snitzer 
106878d8e58aSMike Snitzer 	/*
106978d8e58aSMike Snitzer 	 * I/O for the bio successfully completed.
107078d8e58aSMike Snitzer 	 * Notice the data completion to the upper layer.
107178d8e58aSMike Snitzer 	 */
107278d8e58aSMike Snitzer 
107378d8e58aSMike Snitzer 	/*
107478d8e58aSMike Snitzer 	 * bios are processed from the head of the list.
107578d8e58aSMike Snitzer 	 * So the completing bio should always be rq->bio.
107678d8e58aSMike Snitzer 	 * If it's not, something wrong is happening.
107778d8e58aSMike Snitzer 	 */
107878d8e58aSMike Snitzer 	if (tio->orig->bio != bio)
107978d8e58aSMike Snitzer 		DMERR("bio completion is going in the middle of the request");
108078d8e58aSMike Snitzer 
108178d8e58aSMike Snitzer 	/*
108278d8e58aSMike Snitzer 	 * Update the original request.
108378d8e58aSMike Snitzer 	 * Do not use blk_end_request() here, because it may complete
108478d8e58aSMike Snitzer 	 * the original request before the clone, and break the ordering.
108578d8e58aSMike Snitzer 	 */
108678d8e58aSMike Snitzer 	blk_update_request(tio->orig, 0, nr_bytes);
108778d8e58aSMike Snitzer }
108878d8e58aSMike Snitzer 
1089bfebd1cdSMike Snitzer static struct dm_rq_target_io *tio_from_request(struct request *rq)
1090bfebd1cdSMike Snitzer {
1091bfebd1cdSMike Snitzer 	return (rq->q->mq_ops ? blk_mq_rq_to_pdu(rq) : rq->special);
1092bfebd1cdSMike Snitzer }
1093bfebd1cdSMike Snitzer 
1094e262f347SMikulas Patocka static void rq_end_stats(struct mapped_device *md, struct request *orig)
1095e262f347SMikulas Patocka {
1096e262f347SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats))) {
1097e262f347SMikulas Patocka 		struct dm_rq_target_io *tio = tio_from_request(orig);
1098e262f347SMikulas Patocka 		tio->duration_jiffies = jiffies - tio->duration_jiffies;
1099e262f347SMikulas Patocka 		dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
1100e262f347SMikulas Patocka 				    tio->n_sectors, true, tio->duration_jiffies,
1101e262f347SMikulas Patocka 				    &tio->stats_aux);
1102e262f347SMikulas Patocka 	}
1103e262f347SMikulas Patocka }
1104e262f347SMikulas Patocka 
1105cec47e3dSKiyoshi Ueda /*
1106cec47e3dSKiyoshi Ueda  * Don't touch any member of the md after calling this function because
1107cec47e3dSKiyoshi Ueda  * the md may be freed in dm_put() at the end of this function.
1108cec47e3dSKiyoshi Ueda  * Or do dm_get() before calling this function and dm_put() later.
1109cec47e3dSKiyoshi Ueda  */
1110466d89a6SKeith Busch static void rq_completed(struct mapped_device *md, int rw, bool run_queue)
1111cec47e3dSKiyoshi Ueda {
1112b4324feeSKiyoshi Ueda 	atomic_dec(&md->pending[rw]);
1113cec47e3dSKiyoshi Ueda 
1114cec47e3dSKiyoshi Ueda 	/* nudge anyone waiting on suspend queue */
1115621739b0SMike Snitzer 	if (!md_in_flight(md))
1116cec47e3dSKiyoshi Ueda 		wake_up(&md->wait);
1117cec47e3dSKiyoshi Ueda 
1118a8c32a5cSJens Axboe 	/*
1119a8c32a5cSJens Axboe 	 * Run this off this callpath, as drivers could invoke end_io while
1120a8c32a5cSJens Axboe 	 * inside their request_fn (and holding the queue lock). Calling
1121a8c32a5cSJens Axboe 	 * back into ->request_fn() could deadlock attempting to grab the
1122a8c32a5cSJens Axboe 	 * queue lock again.
1123a8c32a5cSJens Axboe 	 */
11246acfe68bSMike Snitzer 	if (!md->queue->mq_ops && run_queue)
1125a8c32a5cSJens Axboe 		blk_run_queue_async(md->queue);
1126cec47e3dSKiyoshi Ueda 
1127cec47e3dSKiyoshi Ueda 	/*
1128cec47e3dSKiyoshi Ueda 	 * dm_put() must be at the end of this function. See the comment above
1129cec47e3dSKiyoshi Ueda 	 */
1130cec47e3dSKiyoshi Ueda 	dm_put(md);
1131cec47e3dSKiyoshi Ueda }
1132cec47e3dSKiyoshi Ueda 
1133e5d8de32SMike Snitzer static void free_rq_clone(struct request *clone)
1134a77e28c7SKiyoshi Ueda {
1135a77e28c7SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1136bfebd1cdSMike Snitzer 	struct mapped_device *md = tio->md;
1137a77e28c7SKiyoshi Ueda 
113878d8e58aSMike Snitzer 	blk_rq_unprep_clone(clone);
113978d8e58aSMike Snitzer 
1140aa6df8ddSMike Snitzer 	if (md->type == DM_TYPE_MQ_REQUEST_BASED)
1141aa6df8ddSMike Snitzer 		/* stacked on blk-mq queue(s) */
1142e5863d9aSMike Snitzer 		tio->ti->type->release_clone_rq(clone);
114302233342SMike Snitzer 	else if (!md->queue->mq_ops)
114402233342SMike Snitzer 		/* request_fn queue stacked on request_fn queue(s) */
1145eca7ee6dSMike Snitzer 		free_old_clone_request(md, clone);
1146bfebd1cdSMike Snitzer 
1147bfebd1cdSMike Snitzer 	if (!md->queue->mq_ops)
1148eca7ee6dSMike Snitzer 		free_old_rq_tio(tio);
1149a77e28c7SKiyoshi Ueda }
1150a77e28c7SKiyoshi Ueda 
1151980691e5SKiyoshi Ueda /*
1152980691e5SKiyoshi Ueda  * Complete the clone and the original request.
1153466d89a6SKeith Busch  * Must be called without clone's queue lock held,
1154466d89a6SKeith Busch  * see end_clone_request() for more details.
1155980691e5SKiyoshi Ueda  */
1156980691e5SKiyoshi Ueda static void dm_end_request(struct request *clone, int error)
1157980691e5SKiyoshi Ueda {
1158980691e5SKiyoshi Ueda 	int rw = rq_data_dir(clone);
1159980691e5SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1160980691e5SKiyoshi Ueda 	struct mapped_device *md = tio->md;
1161980691e5SKiyoshi Ueda 	struct request *rq = tio->orig;
1162980691e5SKiyoshi Ueda 
116329e4013dSTejun Heo 	if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
1164980691e5SKiyoshi Ueda 		rq->errors = clone->errors;
1165980691e5SKiyoshi Ueda 		rq->resid_len = clone->resid_len;
1166980691e5SKiyoshi Ueda 
1167980691e5SKiyoshi Ueda 		if (rq->sense)
1168980691e5SKiyoshi Ueda 			/*
1169980691e5SKiyoshi Ueda 			 * We are using the sense buffer of the original
1170980691e5SKiyoshi Ueda 			 * request.
1171980691e5SKiyoshi Ueda 			 * So setting the length of the sense data is enough.
1172980691e5SKiyoshi Ueda 			 */
1173980691e5SKiyoshi Ueda 			rq->sense_len = clone->sense_len;
1174980691e5SKiyoshi Ueda 	}
1175980691e5SKiyoshi Ueda 
1176e5d8de32SMike Snitzer 	free_rq_clone(clone);
1177e262f347SMikulas Patocka 	rq_end_stats(md, rq);
1178bfebd1cdSMike Snitzer 	if (!rq->q->mq_ops)
1179980691e5SKiyoshi Ueda 		blk_end_request_all(rq, error);
1180bfebd1cdSMike Snitzer 	else
1181bfebd1cdSMike Snitzer 		blk_mq_end_request(rq, error);
118229e4013dSTejun Heo 	rq_completed(md, rw, true);
1183980691e5SKiyoshi Ueda }
1184980691e5SKiyoshi Ueda 
1185cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq)
1186cec47e3dSKiyoshi Ueda {
1187bfebd1cdSMike Snitzer 	struct dm_rq_target_io *tio = tio_from_request(rq);
1188466d89a6SKeith Busch 	struct request *clone = tio->clone;
1189cec47e3dSKiyoshi Ueda 
1190bfebd1cdSMike Snitzer 	if (!rq->q->mq_ops) {
1191cec47e3dSKiyoshi Ueda 		rq->special = NULL;
1192cec47e3dSKiyoshi Ueda 		rq->cmd_flags &= ~REQ_DONTPREP;
1193bfebd1cdSMike Snitzer 	}
1194cec47e3dSKiyoshi Ueda 
1195e5863d9aSMike Snitzer 	if (clone)
1196e5d8de32SMike Snitzer 		free_rq_clone(clone);
11974328daa2SMike Snitzer 	else if (!tio->md->queue->mq_ops)
1198eca7ee6dSMike Snitzer 		free_old_rq_tio(tio);
1199cec47e3dSKiyoshi Ueda }
1200cec47e3dSKiyoshi Ueda 
1201cec47e3dSKiyoshi Ueda /*
1202cec47e3dSKiyoshi Ueda  * Requeue the original request of a clone.
1203cec47e3dSKiyoshi Ueda  */
1204eca7ee6dSMike Snitzer static void dm_old_requeue_request(struct request *rq)
1205cec47e3dSKiyoshi Ueda {
1206cec47e3dSKiyoshi Ueda 	struct request_queue *q = rq->q;
1207cec47e3dSKiyoshi Ueda 	unsigned long flags;
1208cec47e3dSKiyoshi Ueda 
1209cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
1210cec47e3dSKiyoshi Ueda 	blk_requeue_request(q, rq);
12114ae9944dSJunichi Nomura 	blk_run_queue_async(q);
1212cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
1213bfebd1cdSMike Snitzer }
1214bfebd1cdSMike Snitzer 
1215818c5f3bSMike Snitzer static void dm_mq_requeue_request(struct request *rq)
1216818c5f3bSMike Snitzer {
1217818c5f3bSMike Snitzer 	struct request_queue *q = rq->q;
1218818c5f3bSMike Snitzer 	unsigned long flags;
1219818c5f3bSMike Snitzer 
1220818c5f3bSMike Snitzer 	blk_mq_requeue_request(rq);
1221818c5f3bSMike Snitzer 	spin_lock_irqsave(q->queue_lock, flags);
1222818c5f3bSMike Snitzer 	if (!blk_queue_stopped(q))
1223818c5f3bSMike Snitzer 		blk_mq_kick_requeue_list(q);
1224818c5f3bSMike Snitzer 	spin_unlock_irqrestore(q->queue_lock, flags);
1225818c5f3bSMike Snitzer }
1226818c5f3bSMike Snitzer 
12272d76fff1SMike Snitzer static void dm_requeue_original_request(struct mapped_device *md,
1228bfebd1cdSMike Snitzer 					struct request *rq)
1229bfebd1cdSMike Snitzer {
1230bfebd1cdSMike Snitzer 	int rw = rq_data_dir(rq);
1231bfebd1cdSMike Snitzer 
1232bfebd1cdSMike Snitzer 	dm_unprep_request(rq);
1233bfebd1cdSMike Snitzer 
1234e262f347SMikulas Patocka 	rq_end_stats(md, rq);
1235bfebd1cdSMike Snitzer 	if (!rq->q->mq_ops)
1236eca7ee6dSMike Snitzer 		dm_old_requeue_request(rq);
1237818c5f3bSMike Snitzer 	else
1238818c5f3bSMike Snitzer 		dm_mq_requeue_request(rq);
1239cec47e3dSKiyoshi Ueda 
1240466d89a6SKeith Busch 	rq_completed(md, rw, false);
1241cec47e3dSKiyoshi Ueda }
1242466d89a6SKeith Busch 
1243eca7ee6dSMike Snitzer static void dm_old_stop_queue(struct request_queue *q)
1244cec47e3dSKiyoshi Ueda {
1245bfebd1cdSMike Snitzer 	unsigned long flags;
1246bfebd1cdSMike Snitzer 
1247bfebd1cdSMike Snitzer 	spin_lock_irqsave(q->queue_lock, flags);
1248818c5f3bSMike Snitzer 	if (blk_queue_stopped(q)) {
1249818c5f3bSMike Snitzer 		spin_unlock_irqrestore(q->queue_lock, flags);
1250818c5f3bSMike Snitzer 		return;
1251818c5f3bSMike Snitzer 	}
1252818c5f3bSMike Snitzer 
1253cec47e3dSKiyoshi Ueda 	blk_stop_queue(q);
1254bfebd1cdSMike Snitzer 	spin_unlock_irqrestore(q->queue_lock, flags);
1255cec47e3dSKiyoshi Ueda }
1256cec47e3dSKiyoshi Ueda 
1257eca7ee6dSMike Snitzer static void dm_stop_queue(struct request_queue *q)
1258cec47e3dSKiyoshi Ueda {
1259bfebd1cdSMike Snitzer 	if (!q->mq_ops)
1260eca7ee6dSMike Snitzer 		dm_old_stop_queue(q);
1261bfebd1cdSMike Snitzer 	else
1262bfebd1cdSMike Snitzer 		blk_mq_stop_hw_queues(q);
1263bfebd1cdSMike Snitzer }
1264bfebd1cdSMike Snitzer 
1265eca7ee6dSMike Snitzer static void dm_old_start_queue(struct request_queue *q)
1266bfebd1cdSMike Snitzer {
1267cec47e3dSKiyoshi Ueda 	unsigned long flags;
1268cec47e3dSKiyoshi Ueda 
1269cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
1270cec47e3dSKiyoshi Ueda 	if (blk_queue_stopped(q))
1271cec47e3dSKiyoshi Ueda 		blk_start_queue(q);
1272bfebd1cdSMike Snitzer 	spin_unlock_irqrestore(q->queue_lock, flags);
1273cec47e3dSKiyoshi Ueda }
1274cec47e3dSKiyoshi Ueda 
1275eca7ee6dSMike Snitzer static void dm_start_queue(struct request_queue *q)
1276cec47e3dSKiyoshi Ueda {
1277bfebd1cdSMike Snitzer 	if (!q->mq_ops)
1278eca7ee6dSMike Snitzer 		dm_old_start_queue(q);
1279818c5f3bSMike Snitzer 	else {
1280bfebd1cdSMike Snitzer 		blk_mq_start_stopped_hw_queues(q, true);
1281818c5f3bSMike Snitzer 		blk_mq_kick_requeue_list(q);
1282818c5f3bSMike Snitzer 	}
1283cec47e3dSKiyoshi Ueda }
1284cec47e3dSKiyoshi Ueda 
128511a68244SKiyoshi Ueda static void dm_done(struct request *clone, int error, bool mapped)
128611a68244SKiyoshi Ueda {
128711a68244SKiyoshi Ueda 	int r = error;
128811a68244SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1289ba1cbad9SMike Snitzer 	dm_request_endio_fn rq_end_io = NULL;
1290ba1cbad9SMike Snitzer 
1291ba1cbad9SMike Snitzer 	if (tio->ti) {
1292ba1cbad9SMike Snitzer 		rq_end_io = tio->ti->type->rq_end_io;
129311a68244SKiyoshi Ueda 
129411a68244SKiyoshi Ueda 		if (mapped && rq_end_io)
129511a68244SKiyoshi Ueda 			r = rq_end_io(tio->ti, clone, error, &tio->info);
1296ba1cbad9SMike Snitzer 	}
129711a68244SKiyoshi Ueda 
12987eee4ae2SMike Snitzer 	if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) &&
12997eee4ae2SMike Snitzer 		     !clone->q->limits.max_write_same_sectors))
13007eee4ae2SMike Snitzer 		disable_write_same(tio->md);
13017eee4ae2SMike Snitzer 
130211a68244SKiyoshi Ueda 	if (r <= 0)
130311a68244SKiyoshi Ueda 		/* The target wants to complete the I/O */
130411a68244SKiyoshi Ueda 		dm_end_request(clone, r);
130511a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_INCOMPLETE)
130611a68244SKiyoshi Ueda 		/* The target will handle the I/O */
130711a68244SKiyoshi Ueda 		return;
130811a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_REQUEUE)
130911a68244SKiyoshi Ueda 		/* The target wants to requeue the I/O */
13102d76fff1SMike Snitzer 		dm_requeue_original_request(tio->md, tio->orig);
131111a68244SKiyoshi Ueda 	else {
131211a68244SKiyoshi Ueda 		DMWARN("unimplemented target endio return value: %d", r);
131311a68244SKiyoshi Ueda 		BUG();
131411a68244SKiyoshi Ueda 	}
131511a68244SKiyoshi Ueda }
131611a68244SKiyoshi Ueda 
1317cec47e3dSKiyoshi Ueda /*
1318cec47e3dSKiyoshi Ueda  * Request completion handler for request-based dm
1319cec47e3dSKiyoshi Ueda  */
1320cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq)
1321cec47e3dSKiyoshi Ueda {
132211a68244SKiyoshi Ueda 	bool mapped = true;
1323bfebd1cdSMike Snitzer 	struct dm_rq_target_io *tio = tio_from_request(rq);
1324466d89a6SKeith Busch 	struct request *clone = tio->clone;
1325bfebd1cdSMike Snitzer 	int rw;
1326cec47e3dSKiyoshi Ueda 
1327e5863d9aSMike Snitzer 	if (!clone) {
1328e262f347SMikulas Patocka 		rq_end_stats(tio->md, rq);
1329bfebd1cdSMike Snitzer 		rw = rq_data_dir(rq);
1330bfebd1cdSMike Snitzer 		if (!rq->q->mq_ops) {
1331e5863d9aSMike Snitzer 			blk_end_request_all(rq, tio->error);
1332bfebd1cdSMike Snitzer 			rq_completed(tio->md, rw, false);
1333eca7ee6dSMike Snitzer 			free_old_rq_tio(tio);
1334bfebd1cdSMike Snitzer 		} else {
1335bfebd1cdSMike Snitzer 			blk_mq_end_request(rq, tio->error);
1336bfebd1cdSMike Snitzer 			rq_completed(tio->md, rw, false);
1337bfebd1cdSMike Snitzer 		}
1338e5863d9aSMike Snitzer 		return;
1339e5863d9aSMike Snitzer 	}
1340cec47e3dSKiyoshi Ueda 
134111a68244SKiyoshi Ueda 	if (rq->cmd_flags & REQ_FAILED)
134211a68244SKiyoshi Ueda 		mapped = false;
1343cec47e3dSKiyoshi Ueda 
134411a68244SKiyoshi Ueda 	dm_done(clone, tio->error, mapped);
1345cec47e3dSKiyoshi Ueda }
1346cec47e3dSKiyoshi Ueda 
1347cec47e3dSKiyoshi Ueda /*
1348cec47e3dSKiyoshi Ueda  * Complete the clone and the original request with the error status
1349cec47e3dSKiyoshi Ueda  * through softirq context.
1350cec47e3dSKiyoshi Ueda  */
1351466d89a6SKeith Busch static void dm_complete_request(struct request *rq, int error)
1352cec47e3dSKiyoshi Ueda {
1353bfebd1cdSMike Snitzer 	struct dm_rq_target_io *tio = tio_from_request(rq);
1354cec47e3dSKiyoshi Ueda 
1355cec47e3dSKiyoshi Ueda 	tio->error = error;
13566acfe68bSMike Snitzer 	if (!rq->q->mq_ops)
1357cec47e3dSKiyoshi Ueda 		blk_complete_request(rq);
13586acfe68bSMike Snitzer 	else
13596acfe68bSMike Snitzer 		blk_mq_complete_request(rq, error);
1360cec47e3dSKiyoshi Ueda }
1361cec47e3dSKiyoshi Ueda 
1362cec47e3dSKiyoshi Ueda /*
1363cec47e3dSKiyoshi Ueda  * Complete the not-mapped clone and the original request with the error status
1364cec47e3dSKiyoshi Ueda  * through softirq context.
1365cec47e3dSKiyoshi Ueda  * Target's rq_end_io() function isn't called.
1366e5863d9aSMike Snitzer  * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
1367cec47e3dSKiyoshi Ueda  */
1368466d89a6SKeith Busch static void dm_kill_unmapped_request(struct request *rq, int error)
1369cec47e3dSKiyoshi Ueda {
1370cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_FAILED;
1371466d89a6SKeith Busch 	dm_complete_request(rq, error);
1372cec47e3dSKiyoshi Ueda }
1373cec47e3dSKiyoshi Ueda 
1374cec47e3dSKiyoshi Ueda /*
1375eca7ee6dSMike Snitzer  * Called with the clone's queue lock held (in the case of .request_fn)
1376cec47e3dSKiyoshi Ueda  */
1377cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error)
1378cec47e3dSKiyoshi Ueda {
1379466d89a6SKeith Busch 	struct dm_rq_target_io *tio = clone->end_io_data;
1380466d89a6SKeith Busch 
1381e5863d9aSMike Snitzer 	if (!clone->q->mq_ops) {
1382cec47e3dSKiyoshi Ueda 		/*
1383cec47e3dSKiyoshi Ueda 		 * For just cleaning up the information of the queue in which
1384cec47e3dSKiyoshi Ueda 		 * the clone was dispatched.
1385e5863d9aSMike Snitzer 		 * The clone is *NOT* freed actually here because it is alloced
1386e5863d9aSMike Snitzer 		 * from dm own mempool (REQ_ALLOCED isn't set).
1387cec47e3dSKiyoshi Ueda 		 */
1388cec47e3dSKiyoshi Ueda 		__blk_put_request(clone->q, clone);
1389e5863d9aSMike Snitzer 	}
1390cec47e3dSKiyoshi Ueda 
1391cec47e3dSKiyoshi Ueda 	/*
1392cec47e3dSKiyoshi Ueda 	 * Actual request completion is done in a softirq context which doesn't
1393466d89a6SKeith Busch 	 * hold the clone's queue lock.  Otherwise, deadlock could occur because:
1394cec47e3dSKiyoshi Ueda 	 *     - another request may be submitted by the upper level driver
1395cec47e3dSKiyoshi Ueda 	 *       of the stacking during the completion
1396cec47e3dSKiyoshi Ueda 	 *     - the submission which requires queue lock may be done
1397466d89a6SKeith Busch 	 *       against this clone's queue
1398cec47e3dSKiyoshi Ueda 	 */
1399466d89a6SKeith Busch 	dm_complete_request(tio->orig, error);
1400cec47e3dSKiyoshi Ueda }
1401cec47e3dSKiyoshi Ueda 
140256a67df7SMike Snitzer /*
140356a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
140456a67df7SMike Snitzer  * target boundary.
140556a67df7SMike Snitzer  */
140656a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
14071da177e4SLinus Torvalds {
140856a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
140956a67df7SMike Snitzer 
141056a67df7SMike Snitzer 	return ti->len - target_offset;
141156a67df7SMike Snitzer }
141256a67df7SMike Snitzer 
141356a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
141456a67df7SMike Snitzer {
141556a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
1416542f9038SMike Snitzer 	sector_t offset, max_len;
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds 	/*
14191da177e4SLinus Torvalds 	 * Does the target need to split even further?
14201da177e4SLinus Torvalds 	 */
1421542f9038SMike Snitzer 	if (ti->max_io_len) {
1422542f9038SMike Snitzer 		offset = dm_target_offset(ti, sector);
1423542f9038SMike Snitzer 		if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1424542f9038SMike Snitzer 			max_len = sector_div(offset, ti->max_io_len);
1425542f9038SMike Snitzer 		else
1426542f9038SMike Snitzer 			max_len = offset & (ti->max_io_len - 1);
1427542f9038SMike Snitzer 		max_len = ti->max_io_len - max_len;
1428542f9038SMike Snitzer 
1429542f9038SMike Snitzer 		if (len > max_len)
1430542f9038SMike Snitzer 			len = max_len;
14311da177e4SLinus Torvalds 	}
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 	return len;
14341da177e4SLinus Torvalds }
14351da177e4SLinus Torvalds 
1436542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1437542f9038SMike Snitzer {
1438542f9038SMike Snitzer 	if (len > UINT_MAX) {
1439542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1440542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
1441542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
1442542f9038SMike Snitzer 		return -EINVAL;
1443542f9038SMike Snitzer 	}
1444542f9038SMike Snitzer 
1445542f9038SMike Snitzer 	ti->max_io_len = (uint32_t) len;
1446542f9038SMike Snitzer 
1447542f9038SMike Snitzer 	return 0;
1448542f9038SMike Snitzer }
1449542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1450542f9038SMike Snitzer 
14511dd40c3eSMikulas Patocka /*
14521dd40c3eSMikulas Patocka  * A target may call dm_accept_partial_bio only from the map routine.  It is
14531dd40c3eSMikulas Patocka  * allowed for all bio types except REQ_FLUSH.
14541dd40c3eSMikulas Patocka  *
14551dd40c3eSMikulas Patocka  * dm_accept_partial_bio informs the dm that the target only wants to process
14561dd40c3eSMikulas Patocka  * additional n_sectors sectors of the bio and the rest of the data should be
14571dd40c3eSMikulas Patocka  * sent in a next bio.
14581dd40c3eSMikulas Patocka  *
14591dd40c3eSMikulas Patocka  * A diagram that explains the arithmetics:
14601dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
14611dd40c3eSMikulas Patocka  * |         1          |       2       |   3   |
14621dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
14631dd40c3eSMikulas Patocka  *
14641dd40c3eSMikulas Patocka  * <-------------- *tio->len_ptr --------------->
14651dd40c3eSMikulas Patocka  *                      <------- bi_size ------->
14661dd40c3eSMikulas Patocka  *                      <-- n_sectors -->
14671dd40c3eSMikulas Patocka  *
14681dd40c3eSMikulas Patocka  * Region 1 was already iterated over with bio_advance or similar function.
14691dd40c3eSMikulas Patocka  *	(it may be empty if the target doesn't use bio_advance)
14701dd40c3eSMikulas Patocka  * Region 2 is the remaining bio size that the target wants to process.
14711dd40c3eSMikulas Patocka  *	(it may be empty if region 1 is non-empty, although there is no reason
14721dd40c3eSMikulas Patocka  *	 to make it empty)
14731dd40c3eSMikulas Patocka  * The target requires that region 3 is to be sent in the next bio.
14741dd40c3eSMikulas Patocka  *
14751dd40c3eSMikulas Patocka  * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
14761dd40c3eSMikulas Patocka  * the partially processed part (the sum of regions 1+2) must be the same for all
14771dd40c3eSMikulas Patocka  * copies of the bio.
14781dd40c3eSMikulas Patocka  */
14791dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
14801dd40c3eSMikulas Patocka {
14811dd40c3eSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
14821dd40c3eSMikulas Patocka 	unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
14831dd40c3eSMikulas Patocka 	BUG_ON(bio->bi_rw & REQ_FLUSH);
14841dd40c3eSMikulas Patocka 	BUG_ON(bi_size > *tio->len_ptr);
14851dd40c3eSMikulas Patocka 	BUG_ON(n_sectors > bi_size);
14861dd40c3eSMikulas Patocka 	*tio->len_ptr -= bi_size - n_sectors;
14871dd40c3eSMikulas Patocka 	bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
14881dd40c3eSMikulas Patocka }
14891dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
14901dd40c3eSMikulas Patocka 
1491bd2a49b8SAlasdair G Kergon static void __map_bio(struct dm_target_io *tio)
14921da177e4SLinus Torvalds {
14931da177e4SLinus Torvalds 	int r;
14942056a782SJens Axboe 	sector_t sector;
14959faf400fSStefan Bader 	struct mapped_device *md;
1496dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
1497bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds 	/*
15021da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
15031da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
15041da177e4SLinus Torvalds 	 * this io.
15051da177e4SLinus Torvalds 	 */
15061da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
15074f024f37SKent Overstreet 	sector = clone->bi_iter.bi_sector;
15087de3ee57SMikulas Patocka 	r = ti->type->map(ti, clone);
150945cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
15101da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
15112056a782SJens Axboe 
1512d07335e5SMike Snitzer 		trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
151322a7c31aSAlan D. Brunelle 				      tio->io->bio->bi_bdev->bd_dev, sector);
15142056a782SJens Axboe 
15151da177e4SLinus Torvalds 		generic_make_request(clone);
15162e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
15172e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
15189faf400fSStefan Bader 		md = tio->io->md;
15199faf400fSStefan Bader 		dec_pending(tio->io, r);
15209faf400fSStefan Bader 		free_tio(md, tio);
1521ab37844dSMikulas Patocka 	} else if (r != DM_MAPIO_SUBMITTED) {
152245cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
152345cbcd79SKiyoshi Ueda 		BUG();
15241da177e4SLinus Torvalds 	}
15251da177e4SLinus Torvalds }
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds struct clone_info {
15281da177e4SLinus Torvalds 	struct mapped_device *md;
15291da177e4SLinus Torvalds 	struct dm_table *map;
15301da177e4SLinus Torvalds 	struct bio *bio;
15311da177e4SLinus Torvalds 	struct dm_io *io;
15321da177e4SLinus Torvalds 	sector_t sector;
1533e0d6609aSMikulas Patocka 	unsigned sector_count;
15341da177e4SLinus Torvalds };
15351da177e4SLinus Torvalds 
1536e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
1537bd2a49b8SAlasdair G Kergon {
15384f024f37SKent Overstreet 	bio->bi_iter.bi_sector = sector;
15394f024f37SKent Overstreet 	bio->bi_iter.bi_size = to_bytes(len);
15401da177e4SLinus Torvalds }
15411da177e4SLinus Torvalds 
15421da177e4SLinus Torvalds /*
15431da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
15441da177e4SLinus Torvalds  */
1545dba14160SMikulas Patocka static void clone_bio(struct dm_target_io *tio, struct bio *bio,
15461c3b13e6SKent Overstreet 		      sector_t sector, unsigned len)
15471da177e4SLinus Torvalds {
1548dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
15491da177e4SLinus Torvalds 
15501c3b13e6SKent Overstreet 	__bio_clone_fast(clone, bio);
15519c47008dSMartin K. Petersen 
15521c3b13e6SKent Overstreet 	if (bio_integrity(bio))
15531c3b13e6SKent Overstreet 		bio_integrity_clone(clone, bio, GFP_NOIO);
15541c3b13e6SKent Overstreet 
15551c3b13e6SKent Overstreet 	bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
15561c3b13e6SKent Overstreet 	clone->bi_iter.bi_size = to_bytes(len);
15571c3b13e6SKent Overstreet 
15581c3b13e6SKent Overstreet 	if (bio_integrity(bio))
15591c3b13e6SKent Overstreet 		bio_integrity_trim(clone, 0, len);
15601da177e4SLinus Torvalds }
15611da177e4SLinus Torvalds 
15629015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci,
156399778273SJunichi Nomura 				      struct dm_target *ti,
156455a62eefSAlasdair G Kergon 				      unsigned target_bio_nr)
1565f9ab94ceSMikulas Patocka {
1566dba14160SMikulas Patocka 	struct dm_target_io *tio;
1567dba14160SMikulas Patocka 	struct bio *clone;
1568dba14160SMikulas Patocka 
156999778273SJunichi Nomura 	clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1570dba14160SMikulas Patocka 	tio = container_of(clone, struct dm_target_io, clone);
1571f9ab94ceSMikulas Patocka 
1572f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1573f9ab94ceSMikulas Patocka 	tio->ti = ti;
157455a62eefSAlasdair G Kergon 	tio->target_bio_nr = target_bio_nr;
15759015df24SAlasdair G Kergon 
15769015df24SAlasdair G Kergon 	return tio;
15779015df24SAlasdair G Kergon }
15789015df24SAlasdair G Kergon 
157914fe594dSAlasdair G Kergon static void __clone_and_map_simple_bio(struct clone_info *ci,
158014fe594dSAlasdair G Kergon 				       struct dm_target *ti,
15811dd40c3eSMikulas Patocka 				       unsigned target_bio_nr, unsigned *len)
15829015df24SAlasdair G Kergon {
158399778273SJunichi Nomura 	struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
1584dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
15859015df24SAlasdair G Kergon 
15861dd40c3eSMikulas Patocka 	tio->len_ptr = len;
15871dd40c3eSMikulas Patocka 
15881c3b13e6SKent Overstreet 	__bio_clone_fast(clone, ci->bio);
1589bd2a49b8SAlasdair G Kergon 	if (len)
15901dd40c3eSMikulas Patocka 		bio_setup_sector(clone, ci->sector, *len);
1591f9ab94ceSMikulas Patocka 
1592bd2a49b8SAlasdair G Kergon 	__map_bio(tio);
1593f9ab94ceSMikulas Patocka }
1594f9ab94ceSMikulas Patocka 
159514fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
15961dd40c3eSMikulas Patocka 				  unsigned num_bios, unsigned *len)
159706a426ceSMike Snitzer {
159855a62eefSAlasdair G Kergon 	unsigned target_bio_nr;
159906a426ceSMike Snitzer 
160055a62eefSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
160114fe594dSAlasdair G Kergon 		__clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
160206a426ceSMike Snitzer }
160306a426ceSMike Snitzer 
160414fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1605f9ab94ceSMikulas Patocka {
160606a426ceSMike Snitzer 	unsigned target_nr = 0;
1607f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1608f9ab94ceSMikulas Patocka 
1609b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1610f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
16111dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
1612f9ab94ceSMikulas Patocka 
1613f9ab94ceSMikulas Patocka 	return 0;
1614f9ab94ceSMikulas Patocka }
1615f9ab94ceSMikulas Patocka 
1616e4c93811SAlasdair G Kergon static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
16171dd40c3eSMikulas Patocka 				     sector_t sector, unsigned *len)
16185ae89a87SMike Snitzer {
1619dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
16205ae89a87SMike Snitzer 	struct dm_target_io *tio;
1621b0d8ed4dSAlasdair G Kergon 	unsigned target_bio_nr;
1622b0d8ed4dSAlasdair G Kergon 	unsigned num_target_bios = 1;
16235ae89a87SMike Snitzer 
1624b0d8ed4dSAlasdair G Kergon 	/*
1625b0d8ed4dSAlasdair G Kergon 	 * Does the target want to receive duplicate copies of the bio?
1626b0d8ed4dSAlasdair G Kergon 	 */
1627b0d8ed4dSAlasdair G Kergon 	if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1628b0d8ed4dSAlasdair G Kergon 		num_target_bios = ti->num_write_bios(ti, bio);
1629e4c93811SAlasdair G Kergon 
1630b0d8ed4dSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
163199778273SJunichi Nomura 		tio = alloc_tio(ci, ti, target_bio_nr);
16321dd40c3eSMikulas Patocka 		tio->len_ptr = len;
16331dd40c3eSMikulas Patocka 		clone_bio(tio, bio, sector, *len);
1634bd2a49b8SAlasdair G Kergon 		__map_bio(tio);
16355ae89a87SMike Snitzer 	}
1636b0d8ed4dSAlasdair G Kergon }
16375ae89a87SMike Snitzer 
163855a62eefSAlasdair G Kergon typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
163923508a96SMike Snitzer 
164055a62eefSAlasdair G Kergon static unsigned get_num_discard_bios(struct dm_target *ti)
164123508a96SMike Snitzer {
164255a62eefSAlasdair G Kergon 	return ti->num_discard_bios;
164323508a96SMike Snitzer }
164423508a96SMike Snitzer 
164555a62eefSAlasdair G Kergon static unsigned get_num_write_same_bios(struct dm_target *ti)
164623508a96SMike Snitzer {
164755a62eefSAlasdair G Kergon 	return ti->num_write_same_bios;
164823508a96SMike Snitzer }
164923508a96SMike Snitzer 
165023508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti);
165123508a96SMike Snitzer 
165223508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti)
165323508a96SMike Snitzer {
165455a62eefSAlasdair G Kergon 	return ti->split_discard_bios;
165523508a96SMike Snitzer }
165623508a96SMike Snitzer 
165714fe594dSAlasdair G Kergon static int __send_changing_extent_only(struct clone_info *ci,
165855a62eefSAlasdair G Kergon 				       get_num_bios_fn get_num_bios,
165923508a96SMike Snitzer 				       is_split_required_fn is_split_required)
16605ae89a87SMike Snitzer {
16615ae89a87SMike Snitzer 	struct dm_target *ti;
1662e0d6609aSMikulas Patocka 	unsigned len;
166355a62eefSAlasdair G Kergon 	unsigned num_bios;
16645ae89a87SMike Snitzer 
1665a79245b3SMike Snitzer 	do {
16665ae89a87SMike Snitzer 		ti = dm_table_find_target(ci->map, ci->sector);
16675ae89a87SMike Snitzer 		if (!dm_target_is_valid(ti))
16685ae89a87SMike Snitzer 			return -EIO;
16695ae89a87SMike Snitzer 
16705ae89a87SMike Snitzer 		/*
167123508a96SMike Snitzer 		 * Even though the device advertised support for this type of
167223508a96SMike Snitzer 		 * request, that does not mean every target supports it, and
1673936688d7SMike Snitzer 		 * reconfiguration might also have changed that since the
16745ae89a87SMike Snitzer 		 * check was performed.
16755ae89a87SMike Snitzer 		 */
167655a62eefSAlasdair G Kergon 		num_bios = get_num_bios ? get_num_bios(ti) : 0;
167755a62eefSAlasdair G Kergon 		if (!num_bios)
16785ae89a87SMike Snitzer 			return -EOPNOTSUPP;
16795ae89a87SMike Snitzer 
168023508a96SMike Snitzer 		if (is_split_required && !is_split_required(ti))
1681e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
16827acf0277SMikulas Patocka 		else
1683e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
16845ae89a87SMike Snitzer 
16851dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, num_bios, &len);
16865ae89a87SMike Snitzer 
1687a79245b3SMike Snitzer 		ci->sector += len;
1688a79245b3SMike Snitzer 	} while (ci->sector_count -= len);
16895ae89a87SMike Snitzer 
16905ae89a87SMike Snitzer 	return 0;
16915ae89a87SMike Snitzer }
16925ae89a87SMike Snitzer 
169314fe594dSAlasdair G Kergon static int __send_discard(struct clone_info *ci)
169423508a96SMike Snitzer {
169514fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_discard_bios,
169623508a96SMike Snitzer 					   is_split_required_for_discard);
169723508a96SMike Snitzer }
169823508a96SMike Snitzer 
169914fe594dSAlasdair G Kergon static int __send_write_same(struct clone_info *ci)
170023508a96SMike Snitzer {
170114fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
170223508a96SMike Snitzer }
170323508a96SMike Snitzer 
1704e4c93811SAlasdair G Kergon /*
1705e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1706e4c93811SAlasdair G Kergon  */
1707e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci)
1708e4c93811SAlasdair G Kergon {
1709e4c93811SAlasdair G Kergon 	struct bio *bio = ci->bio;
1710e4c93811SAlasdair G Kergon 	struct dm_target *ti;
17111c3b13e6SKent Overstreet 	unsigned len;
1712e4c93811SAlasdair G Kergon 
1713e4c93811SAlasdair G Kergon 	if (unlikely(bio->bi_rw & REQ_DISCARD))
1714e4c93811SAlasdair G Kergon 		return __send_discard(ci);
1715e4c93811SAlasdair G Kergon 	else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
1716e4c93811SAlasdair G Kergon 		return __send_write_same(ci);
1717e4c93811SAlasdair G Kergon 
1718e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1719e4c93811SAlasdair G Kergon 	if (!dm_target_is_valid(ti))
1720e4c93811SAlasdair G Kergon 		return -EIO;
1721e4c93811SAlasdair G Kergon 
17221c3b13e6SKent Overstreet 	len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
1723e4c93811SAlasdair G Kergon 
17241dd40c3eSMikulas Patocka 	__clone_and_map_data_bio(ci, ti, ci->sector, &len);
1725e4c93811SAlasdair G Kergon 
1726e4c93811SAlasdair G Kergon 	ci->sector += len;
1727e4c93811SAlasdair G Kergon 	ci->sector_count -= len;
1728e4c93811SAlasdair G Kergon 
1729e4c93811SAlasdair G Kergon 	return 0;
1730e4c93811SAlasdair G Kergon }
1731e4c93811SAlasdair G Kergon 
1732e4c93811SAlasdair G Kergon /*
173314fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
17341da177e4SLinus Torvalds  */
173583d5e5b0SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md,
173683d5e5b0SMikulas Patocka 				    struct dm_table *map, struct bio *bio)
17371da177e4SLinus Torvalds {
17381da177e4SLinus Torvalds 	struct clone_info ci;
1739512875bdSJun'ichi Nomura 	int error = 0;
17401da177e4SLinus Torvalds 
174183d5e5b0SMikulas Patocka 	if (unlikely(!map)) {
1742f0b9a450SMikulas Patocka 		bio_io_error(bio);
1743f0b9a450SMikulas Patocka 		return;
1744f0b9a450SMikulas Patocka 	}
1745692d0eb9SMikulas Patocka 
174683d5e5b0SMikulas Patocka 	ci.map = map;
17471da177e4SLinus Torvalds 	ci.md = md;
17481da177e4SLinus Torvalds 	ci.io = alloc_io(md);
17491da177e4SLinus Torvalds 	ci.io->error = 0;
17501da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
17511da177e4SLinus Torvalds 	ci.io->bio = bio;
17521da177e4SLinus Torvalds 	ci.io->md = md;
1753f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
17544f024f37SKent Overstreet 	ci.sector = bio->bi_iter.bi_sector;
17551da177e4SLinus Torvalds 
17563eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1757bd2a49b8SAlasdair G Kergon 
1758b372d360SMike Snitzer 	if (bio->bi_rw & REQ_FLUSH) {
1759b372d360SMike Snitzer 		ci.bio = &ci.md->flush_bio;
1760b372d360SMike Snitzer 		ci.sector_count = 0;
176114fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1762b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1763b372d360SMike Snitzer 	} else {
17646a8736d1STejun Heo 		ci.bio = bio;
1765f6fccb12SMilan Broz 		ci.sector_count = bio_sectors(bio);
1766512875bdSJun'ichi Nomura 		while (ci.sector_count && !error)
176714fe594dSAlasdair G Kergon 			error = __split_and_process_non_flush(&ci);
1768d87f4c14STejun Heo 	}
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds 	/* drop the extra reference count */
1771512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
17729e4e5f87SMilan Broz }
17739e4e5f87SMilan Broz /*-----------------------------------------------------------------
17741da177e4SLinus Torvalds  * CRUD END
17751da177e4SLinus Torvalds  *---------------------------------------------------------------*/
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds /*
17781da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
17791da177e4SLinus Torvalds  * dm_merge_bvec.
17801da177e4SLinus Torvalds  */
1781dece1635SJens Axboe static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
17821da177e4SLinus Torvalds {
178312f03a49SKevin Corry 	int rw = bio_data_dir(bio);
17841da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
178583d5e5b0SMikulas Patocka 	int srcu_idx;
178683d5e5b0SMikulas Patocka 	struct dm_table *map;
17871da177e4SLinus Torvalds 
178883d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
17891da177e4SLinus Torvalds 
179018c0b223SGu Zheng 	generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
179112f03a49SKevin Corry 
17926a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
17936a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
179483d5e5b0SMikulas Patocka 		dm_put_live_table(md, srcu_idx);
17951da177e4SLinus Torvalds 
17966a8736d1STejun Heo 		if (bio_rw(bio) != READA)
179792c63902SMikulas Patocka 			queue_io(md, bio);
17986a8736d1STejun Heo 		else
17996a8736d1STejun Heo 			bio_io_error(bio);
1800dece1635SJens Axboe 		return BLK_QC_T_NONE;
18011da177e4SLinus Torvalds 	}
18021da177e4SLinus Torvalds 
180383d5e5b0SMikulas Patocka 	__split_and_process_bio(md, map, bio);
180483d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
1805dece1635SJens Axboe 	return BLK_QC_T_NONE;
1806cec47e3dSKiyoshi Ueda }
1807cec47e3dSKiyoshi Ueda 
1808fd2ed4d2SMikulas Patocka int dm_request_based(struct mapped_device *md)
1809cec47e3dSKiyoshi Ueda {
1810cec47e3dSKiyoshi Ueda 	return blk_queue_stackable(md->queue);
1811cec47e3dSKiyoshi Ueda }
1812cec47e3dSKiyoshi Ueda 
1813466d89a6SKeith Busch static void dm_dispatch_clone_request(struct request *clone, struct request *rq)
1814cec47e3dSKiyoshi Ueda {
1815cec47e3dSKiyoshi Ueda 	int r;
1816cec47e3dSKiyoshi Ueda 
1817466d89a6SKeith Busch 	if (blk_queue_io_stat(clone->q))
1818466d89a6SKeith Busch 		clone->cmd_flags |= REQ_IO_STAT;
1819cec47e3dSKiyoshi Ueda 
1820466d89a6SKeith Busch 	clone->start_time = jiffies;
1821466d89a6SKeith Busch 	r = blk_insert_cloned_request(clone->q, clone);
1822cec47e3dSKiyoshi Ueda 	if (r)
1823466d89a6SKeith Busch 		/* must complete clone in terms of original request */
1824cec47e3dSKiyoshi Ueda 		dm_complete_request(rq, r);
1825cec47e3dSKiyoshi Ueda }
1826cec47e3dSKiyoshi Ueda 
182778d8e58aSMike Snitzer static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
182878d8e58aSMike Snitzer 				 void *data)
1829cec47e3dSKiyoshi Ueda {
183078d8e58aSMike Snitzer 	struct dm_rq_target_io *tio = data;
183178d8e58aSMike Snitzer 	struct dm_rq_clone_bio_info *info =
183278d8e58aSMike Snitzer 		container_of(bio, struct dm_rq_clone_bio_info, clone);
183378d8e58aSMike Snitzer 
183478d8e58aSMike Snitzer 	info->orig = bio_orig;
183578d8e58aSMike Snitzer 	info->tio = tio;
183678d8e58aSMike Snitzer 	bio->bi_end_io = end_clone_bio;
183778d8e58aSMike Snitzer 
183878d8e58aSMike Snitzer 	return 0;
183978d8e58aSMike Snitzer }
184078d8e58aSMike Snitzer 
184178d8e58aSMike Snitzer static int setup_clone(struct request *clone, struct request *rq,
184278d8e58aSMike Snitzer 		       struct dm_rq_target_io *tio, gfp_t gfp_mask)
184378d8e58aSMike Snitzer {
184478d8e58aSMike Snitzer 	int r;
184578d8e58aSMike Snitzer 
184678d8e58aSMike Snitzer 	r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask,
184778d8e58aSMike Snitzer 			      dm_rq_bio_constructor, tio);
184878d8e58aSMike Snitzer 	if (r)
184978d8e58aSMike Snitzer 		return r;
185078d8e58aSMike Snitzer 
185178d8e58aSMike Snitzer 	clone->cmd = rq->cmd;
185278d8e58aSMike Snitzer 	clone->cmd_len = rq->cmd_len;
185378d8e58aSMike Snitzer 	clone->sense = rq->sense;
1854cec47e3dSKiyoshi Ueda 	clone->end_io = end_clone_request;
1855cec47e3dSKiyoshi Ueda 	clone->end_io_data = tio;
185678d8e58aSMike Snitzer 
18571ae49ea2SMike Snitzer 	tio->clone = clone;
185878d8e58aSMike Snitzer 
185978d8e58aSMike Snitzer 	return 0;
1860cec47e3dSKiyoshi Ueda }
1861cec47e3dSKiyoshi Ueda 
1862eca7ee6dSMike Snitzer static struct request *clone_old_rq(struct request *rq, struct mapped_device *md,
18631ae49ea2SMike Snitzer 				    struct dm_rq_target_io *tio, gfp_t gfp_mask)
18646facdaffSKiyoshi Ueda {
186502233342SMike Snitzer 	/*
1866c5248f79SMike Snitzer 	 * Create clone for use with .request_fn request_queue
186702233342SMike Snitzer 	 */
186802233342SMike Snitzer 	struct request *clone;
18691ae49ea2SMike Snitzer 
1870eca7ee6dSMike Snitzer 	clone = alloc_old_clone_request(md, gfp_mask);
18711ae49ea2SMike Snitzer 	if (!clone)
18721ae49ea2SMike Snitzer 		return NULL;
18731ae49ea2SMike Snitzer 
18741ae49ea2SMike Snitzer 	blk_rq_init(NULL, clone);
187578d8e58aSMike Snitzer 	if (setup_clone(clone, rq, tio, gfp_mask)) {
187678d8e58aSMike Snitzer 		/* -ENOMEM */
1877eca7ee6dSMike Snitzer 		free_old_clone_request(md, clone);
187878d8e58aSMike Snitzer 		return NULL;
187978d8e58aSMike Snitzer 	}
18801ae49ea2SMike Snitzer 
18811ae49ea2SMike Snitzer 	return clone;
18821ae49ea2SMike Snitzer }
18831ae49ea2SMike Snitzer 
18842eb6e1e3SKeith Busch static void map_tio_request(struct kthread_work *work);
18852eb6e1e3SKeith Busch 
1886bfebd1cdSMike Snitzer static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
1887bfebd1cdSMike Snitzer 		     struct mapped_device *md)
1888bfebd1cdSMike Snitzer {
1889bfebd1cdSMike Snitzer 	tio->md = md;
1890bfebd1cdSMike Snitzer 	tio->ti = NULL;
1891bfebd1cdSMike Snitzer 	tio->clone = NULL;
1892bfebd1cdSMike Snitzer 	tio->orig = rq;
1893bfebd1cdSMike Snitzer 	tio->error = 0;
1894*591ddcfcSMike Snitzer 	/*
1895*591ddcfcSMike Snitzer 	 * Avoid initializing info for blk-mq; it passes
1896*591ddcfcSMike Snitzer 	 * target-specific data through info.ptr
1897*591ddcfcSMike Snitzer 	 * (see: dm_mq_init_request)
1898*591ddcfcSMike Snitzer 	 */
1899*591ddcfcSMike Snitzer 	if (!md->init_tio_pdu)
1900bfebd1cdSMike Snitzer 		memset(&tio->info, 0, sizeof(tio->info));
190102233342SMike Snitzer 	if (md->kworker_task)
1902bfebd1cdSMike Snitzer 		init_kthread_work(&tio->work, map_tio_request);
1903bfebd1cdSMike Snitzer }
1904bfebd1cdSMike Snitzer 
1905eca7ee6dSMike Snitzer static struct dm_rq_target_io *dm_old_prep_tio(struct request *rq,
1906eca7ee6dSMike Snitzer 					       struct mapped_device *md,
1907eca7ee6dSMike Snitzer 					       gfp_t gfp_mask)
19086facdaffSKiyoshi Ueda {
19096facdaffSKiyoshi Ueda 	struct dm_rq_target_io *tio;
1910e5863d9aSMike Snitzer 	int srcu_idx;
1911e5863d9aSMike Snitzer 	struct dm_table *table;
19126facdaffSKiyoshi Ueda 
1913eca7ee6dSMike Snitzer 	tio = alloc_old_rq_tio(md, gfp_mask);
19146facdaffSKiyoshi Ueda 	if (!tio)
19156facdaffSKiyoshi Ueda 		return NULL;
19166facdaffSKiyoshi Ueda 
1917bfebd1cdSMike Snitzer 	init_tio(tio, rq, md);
19186facdaffSKiyoshi Ueda 
1919e5863d9aSMike Snitzer 	table = dm_get_live_table(md, &srcu_idx);
1920eca7ee6dSMike Snitzer 	/*
1921eca7ee6dSMike Snitzer 	 * Must clone a request if this .request_fn DM device
1922eca7ee6dSMike Snitzer 	 * is stacked on .request_fn device(s).
1923eca7ee6dSMike Snitzer 	 */
1924e5863d9aSMike Snitzer 	if (!dm_table_mq_request_based(table)) {
1925eca7ee6dSMike Snitzer 		if (!clone_old_rq(rq, md, tio, gfp_mask)) {
1926e5863d9aSMike Snitzer 			dm_put_live_table(md, srcu_idx);
1927eca7ee6dSMike Snitzer 			free_old_rq_tio(tio);
19286facdaffSKiyoshi Ueda 			return NULL;
19296facdaffSKiyoshi Ueda 		}
1930e5863d9aSMike Snitzer 	}
1931e5863d9aSMike Snitzer 	dm_put_live_table(md, srcu_idx);
19326facdaffSKiyoshi Ueda 
1933466d89a6SKeith Busch 	return tio;
19346facdaffSKiyoshi Ueda }
19356facdaffSKiyoshi Ueda 
1936cec47e3dSKiyoshi Ueda /*
1937cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1938cec47e3dSKiyoshi Ueda  */
1939eca7ee6dSMike Snitzer static int dm_old_prep_fn(struct request_queue *q, struct request *rq)
1940cec47e3dSKiyoshi Ueda {
1941cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1942466d89a6SKeith Busch 	struct dm_rq_target_io *tio;
1943cec47e3dSKiyoshi Ueda 
1944cec47e3dSKiyoshi Ueda 	if (unlikely(rq->special)) {
1945cec47e3dSKiyoshi Ueda 		DMWARN("Already has something in rq->special.");
1946cec47e3dSKiyoshi Ueda 		return BLKPREP_KILL;
1947cec47e3dSKiyoshi Ueda 	}
1948cec47e3dSKiyoshi Ueda 
1949eca7ee6dSMike Snitzer 	tio = dm_old_prep_tio(rq, md, GFP_ATOMIC);
1950466d89a6SKeith Busch 	if (!tio)
1951cec47e3dSKiyoshi Ueda 		return BLKPREP_DEFER;
1952cec47e3dSKiyoshi Ueda 
1953466d89a6SKeith Busch 	rq->special = tio;
1954cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_DONTPREP;
1955cec47e3dSKiyoshi Ueda 
1956cec47e3dSKiyoshi Ueda 	return BLKPREP_OK;
1957cec47e3dSKiyoshi Ueda }
1958cec47e3dSKiyoshi Ueda 
19599eef87daSKiyoshi Ueda /*
19609eef87daSKiyoshi Ueda  * Returns:
1961e5863d9aSMike Snitzer  * 0                : the request has been processed
1962e5863d9aSMike Snitzer  * DM_MAPIO_REQUEUE : the original request needs to be requeued
1963e5863d9aSMike Snitzer  * < 0              : the request was completed due to failure
19649eef87daSKiyoshi Ueda  */
1965bfebd1cdSMike Snitzer static int map_request(struct dm_rq_target_io *tio, struct request *rq,
1966cec47e3dSKiyoshi Ueda 		       struct mapped_device *md)
1967cec47e3dSKiyoshi Ueda {
1968e5863d9aSMike Snitzer 	int r;
1969bfebd1cdSMike Snitzer 	struct dm_target *ti = tio->ti;
1970e5863d9aSMike Snitzer 	struct request *clone = NULL;
1971cec47e3dSKiyoshi Ueda 
1972e5863d9aSMike Snitzer 	if (tio->clone) {
1973e5863d9aSMike Snitzer 		clone = tio->clone;
1974cec47e3dSKiyoshi Ueda 		r = ti->type->map_rq(ti, clone, &tio->info);
1975e5863d9aSMike Snitzer 	} else {
1976e5863d9aSMike Snitzer 		r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
1977e5863d9aSMike Snitzer 		if (r < 0) {
1978e5863d9aSMike Snitzer 			/* The target wants to complete the I/O */
1979e5863d9aSMike Snitzer 			dm_kill_unmapped_request(rq, r);
1980e5863d9aSMike Snitzer 			return r;
1981e5863d9aSMike Snitzer 		}
19823a140755SJunichi Nomura 		if (r != DM_MAPIO_REMAPPED)
19833a140755SJunichi Nomura 			return r;
198478d8e58aSMike Snitzer 		if (setup_clone(clone, rq, tio, GFP_ATOMIC)) {
198578d8e58aSMike Snitzer 			/* -ENOMEM */
198678d8e58aSMike Snitzer 			ti->type->release_clone_rq(clone);
198778d8e58aSMike Snitzer 			return DM_MAPIO_REQUEUE;
198878d8e58aSMike Snitzer 		}
1989e5863d9aSMike Snitzer 	}
1990e5863d9aSMike Snitzer 
1991cec47e3dSKiyoshi Ueda 	switch (r) {
1992cec47e3dSKiyoshi Ueda 	case DM_MAPIO_SUBMITTED:
1993cec47e3dSKiyoshi Ueda 		/* The target has taken the I/O to submit by itself later */
1994cec47e3dSKiyoshi Ueda 		break;
1995cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REMAPPED:
1996cec47e3dSKiyoshi Ueda 		/* The target has remapped the I/O so dispatch it */
19976db4ccd6SJun'ichi Nomura 		trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1998466d89a6SKeith Busch 				     blk_rq_pos(rq));
1999466d89a6SKeith Busch 		dm_dispatch_clone_request(clone, rq);
2000cec47e3dSKiyoshi Ueda 		break;
2001cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REQUEUE:
2002cec47e3dSKiyoshi Ueda 		/* The target wants to requeue the I/O */
20032d76fff1SMike Snitzer 		dm_requeue_original_request(md, tio->orig);
2004cec47e3dSKiyoshi Ueda 		break;
2005cec47e3dSKiyoshi Ueda 	default:
2006cec47e3dSKiyoshi Ueda 		if (r > 0) {
2007cec47e3dSKiyoshi Ueda 			DMWARN("unimplemented target map return value: %d", r);
2008cec47e3dSKiyoshi Ueda 			BUG();
2009cec47e3dSKiyoshi Ueda 		}
2010cec47e3dSKiyoshi Ueda 
2011cec47e3dSKiyoshi Ueda 		/* The target wants to complete the I/O */
2012466d89a6SKeith Busch 		dm_kill_unmapped_request(rq, r);
2013e5863d9aSMike Snitzer 		return r;
2014cec47e3dSKiyoshi Ueda 	}
20159eef87daSKiyoshi Ueda 
2016e5863d9aSMike Snitzer 	return 0;
2017cec47e3dSKiyoshi Ueda }
2018cec47e3dSKiyoshi Ueda 
20192eb6e1e3SKeith Busch static void map_tio_request(struct kthread_work *work)
2020ba1cbad9SMike Snitzer {
20212eb6e1e3SKeith Busch 	struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work);
2022e5863d9aSMike Snitzer 	struct request *rq = tio->orig;
2023e5863d9aSMike Snitzer 	struct mapped_device *md = tio->md;
2024ba1cbad9SMike Snitzer 
2025bfebd1cdSMike Snitzer 	if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE)
20262d76fff1SMike Snitzer 		dm_requeue_original_request(md, rq);
20272eb6e1e3SKeith Busch }
20282eb6e1e3SKeith Busch 
2029466d89a6SKeith Busch static void dm_start_request(struct mapped_device *md, struct request *orig)
2030ba1cbad9SMike Snitzer {
2031bfebd1cdSMike Snitzer 	if (!orig->q->mq_ops)
2032ba1cbad9SMike Snitzer 		blk_start_request(orig);
2033bfebd1cdSMike Snitzer 	else
2034bfebd1cdSMike Snitzer 		blk_mq_start_request(orig);
2035466d89a6SKeith Busch 	atomic_inc(&md->pending[rq_data_dir(orig)]);
2036ba1cbad9SMike Snitzer 
20370ce65797SMike Snitzer 	if (md->seq_rq_merge_deadline_usecs) {
2038de3ec86dSMike Snitzer 		md->last_rq_pos = rq_end_sector(orig);
2039de3ec86dSMike Snitzer 		md->last_rq_rw = rq_data_dir(orig);
20400ce65797SMike Snitzer 		md->last_rq_start_time = ktime_get();
20410ce65797SMike Snitzer 	}
2042de3ec86dSMike Snitzer 
2043e262f347SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats))) {
2044e262f347SMikulas Patocka 		struct dm_rq_target_io *tio = tio_from_request(orig);
2045e262f347SMikulas Patocka 		tio->duration_jiffies = jiffies;
2046e262f347SMikulas Patocka 		tio->n_sectors = blk_rq_sectors(orig);
2047e262f347SMikulas Patocka 		dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
2048e262f347SMikulas Patocka 				    tio->n_sectors, false, 0, &tio->stats_aux);
2049e262f347SMikulas Patocka 	}
2050e262f347SMikulas Patocka 
2051ba1cbad9SMike Snitzer 	/*
2052ba1cbad9SMike Snitzer 	 * Hold the md reference here for the in-flight I/O.
2053ba1cbad9SMike Snitzer 	 * We can't rely on the reference count by device opener,
2054ba1cbad9SMike Snitzer 	 * because the device may be closed during the request completion
2055ba1cbad9SMike Snitzer 	 * when all bios are completed.
2056ba1cbad9SMike Snitzer 	 * See the comment in rq_completed() too.
2057ba1cbad9SMike Snitzer 	 */
2058ba1cbad9SMike Snitzer 	dm_get(md);
2059ba1cbad9SMike Snitzer }
2060ba1cbad9SMike Snitzer 
20610ce65797SMike Snitzer #define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000
20620ce65797SMike Snitzer 
20630ce65797SMike Snitzer ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
20640ce65797SMike Snitzer {
20650ce65797SMike Snitzer 	return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs);
20660ce65797SMike Snitzer }
20670ce65797SMike Snitzer 
20680ce65797SMike Snitzer ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
20690ce65797SMike Snitzer 						     const char *buf, size_t count)
20700ce65797SMike Snitzer {
20710ce65797SMike Snitzer 	unsigned deadline;
20720ce65797SMike Snitzer 
207317e149b8SMike Snitzer 	if (!dm_request_based(md) || md->use_blk_mq)
20740ce65797SMike Snitzer 		return count;
20750ce65797SMike Snitzer 
20760ce65797SMike Snitzer 	if (kstrtouint(buf, 10, &deadline))
20770ce65797SMike Snitzer 		return -EINVAL;
20780ce65797SMike Snitzer 
20790ce65797SMike Snitzer 	if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS)
20800ce65797SMike Snitzer 		deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS;
20810ce65797SMike Snitzer 
20820ce65797SMike Snitzer 	md->seq_rq_merge_deadline_usecs = deadline;
20830ce65797SMike Snitzer 
20840ce65797SMike Snitzer 	return count;
20850ce65797SMike Snitzer }
20860ce65797SMike Snitzer 
20870ce65797SMike Snitzer static bool dm_request_peeked_before_merge_deadline(struct mapped_device *md)
20880ce65797SMike Snitzer {
20890ce65797SMike Snitzer 	ktime_t kt_deadline;
20900ce65797SMike Snitzer 
20910ce65797SMike Snitzer 	if (!md->seq_rq_merge_deadline_usecs)
20920ce65797SMike Snitzer 		return false;
20930ce65797SMike Snitzer 
20940ce65797SMike Snitzer 	kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC);
20950ce65797SMike Snitzer 	kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline);
20960ce65797SMike Snitzer 
20970ce65797SMike Snitzer 	return !ktime_after(ktime_get(), kt_deadline);
20980ce65797SMike Snitzer }
20990ce65797SMike Snitzer 
2100cec47e3dSKiyoshi Ueda /*
2101cec47e3dSKiyoshi Ueda  * q->request_fn for request-based dm.
2102cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
2103cec47e3dSKiyoshi Ueda  */
2104cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q)
2105cec47e3dSKiyoshi Ueda {
2106cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
2107c91852ffSMike Snitzer 	struct dm_target *ti = md->immutable_target;
2108466d89a6SKeith Busch 	struct request *rq;
21092eb6e1e3SKeith Busch 	struct dm_rq_target_io *tio;
2110c91852ffSMike Snitzer 	sector_t pos = 0;
2111c91852ffSMike Snitzer 
2112c91852ffSMike Snitzer 	if (unlikely(!ti)) {
2113c91852ffSMike Snitzer 		int srcu_idx;
2114c91852ffSMike Snitzer 		struct dm_table *map = dm_get_live_table(md, &srcu_idx);
2115c91852ffSMike Snitzer 
2116c91852ffSMike Snitzer 		ti = dm_table_find_target(map, pos);
2117c91852ffSMike Snitzer 		dm_put_live_table(md, srcu_idx);
2118c91852ffSMike Snitzer 	}
2119cec47e3dSKiyoshi Ueda 
2120cec47e3dSKiyoshi Ueda 	/*
2121b4324feeSKiyoshi Ueda 	 * For suspend, check blk_queue_stopped() and increment
2122b4324feeSKiyoshi Ueda 	 * ->pending within a single queue_lock not to increment the
2123b4324feeSKiyoshi Ueda 	 * number of in-flight I/Os after the queue is stopped in
2124b4324feeSKiyoshi Ueda 	 * dm_suspend().
2125cec47e3dSKiyoshi Ueda 	 */
21267eaceaccSJens Axboe 	while (!blk_queue_stopped(q)) {
2127cec47e3dSKiyoshi Ueda 		rq = blk_peek_request(q);
2128cec47e3dSKiyoshi Ueda 		if (!rq)
2129c91852ffSMike Snitzer 			return;
2130cec47e3dSKiyoshi Ueda 
213129e4013dSTejun Heo 		/* always use block 0 to find the target for flushes for now */
213229e4013dSTejun Heo 		pos = 0;
213329e4013dSTejun Heo 		if (!(rq->cmd_flags & REQ_FLUSH))
213429e4013dSTejun Heo 			pos = blk_rq_pos(rq);
2135d0bcb878SKiyoshi Ueda 
2136c91852ffSMike Snitzer 		if ((dm_request_peeked_before_merge_deadline(md) &&
21370ce65797SMike Snitzer 		     md_in_flight(md) && rq->bio && rq->bio->bi_vcnt == 1 &&
2138c91852ffSMike Snitzer 		     md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq)) ||
2139c91852ffSMike Snitzer 		    (ti->type->busy && ti->type->busy(ti))) {
2140c91852ffSMike Snitzer 			blk_delay_queue(q, HZ / 100);
2141c91852ffSMike Snitzer 			return;
2142c91852ffSMike Snitzer 		}
2143cec47e3dSKiyoshi Ueda 
2144466d89a6SKeith Busch 		dm_start_request(md, rq);
2145b4324feeSKiyoshi Ueda 
2146bfebd1cdSMike Snitzer 		tio = tio_from_request(rq);
21472eb6e1e3SKeith Busch 		/* Establish tio->ti before queuing work (map_tio_request) */
21482eb6e1e3SKeith Busch 		tio->ti = ti;
21492eb6e1e3SKeith Busch 		queue_kthread_work(&md->kworker, &tio->work);
2150052189a2SKiyoshi Ueda 		BUG_ON(!irqs_disabled());
2151cec47e3dSKiyoshi Ueda 	}
2152cec47e3dSKiyoshi Ueda }
2153cec47e3dSKiyoshi Ueda 
21541da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
21551da177e4SLinus Torvalds {
21568a57dfc6SChandra Seetharaman 	int r = bdi_bits;
21578a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
21588a57dfc6SChandra Seetharaman 	struct dm_table *map;
21591da177e4SLinus Torvalds 
21601eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2161e522c039SMike Snitzer 		if (dm_request_based(md)) {
2162cec47e3dSKiyoshi Ueda 			/*
2163e522c039SMike Snitzer 			 * With request-based DM we only need to check the
2164e522c039SMike Snitzer 			 * top-level queue for congestion.
2165cec47e3dSKiyoshi Ueda 			 */
2166e522c039SMike Snitzer 			r = md->queue->backing_dev_info.wb.state & bdi_bits;
2167e522c039SMike Snitzer 		} else {
2168e522c039SMike Snitzer 			map = dm_get_live_table_fast(md);
2169e522c039SMike Snitzer 			if (map)
21701da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
217183d5e5b0SMikulas Patocka 			dm_put_live_table_fast(md);
21728a57dfc6SChandra Seetharaman 		}
2173e522c039SMike Snitzer 	}
21748a57dfc6SChandra Seetharaman 
21751da177e4SLinus Torvalds 	return r;
21761da177e4SLinus Torvalds }
21771da177e4SLinus Torvalds 
21781da177e4SLinus Torvalds /*-----------------------------------------------------------------
21791da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
21801da177e4SLinus Torvalds  *---------------------------------------------------------------*/
21812b06cfffSAlasdair G Kergon static void free_minor(int minor)
21821da177e4SLinus Torvalds {
2183f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
21841da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
2185f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
21861da177e4SLinus Torvalds }
21871da177e4SLinus Torvalds 
21881da177e4SLinus Torvalds /*
21891da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
21901da177e4SLinus Torvalds  */
2191cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
21921da177e4SLinus Torvalds {
2193c9d76be6STejun Heo 	int r;
21941da177e4SLinus Torvalds 
21951da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
21961da177e4SLinus Torvalds 		return -EINVAL;
21971da177e4SLinus Torvalds 
2198c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
2199f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
22001da177e4SLinus Torvalds 
2201c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
22021da177e4SLinus Torvalds 
2203f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2204c9d76be6STejun Heo 	idr_preload_end();
2205c9d76be6STejun Heo 	if (r < 0)
2206c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
2207c9d76be6STejun Heo 	return 0;
22081da177e4SLinus Torvalds }
22091da177e4SLinus Torvalds 
2210cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
22111da177e4SLinus Torvalds {
2212c9d76be6STejun Heo 	int r;
22131da177e4SLinus Torvalds 
2214c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
2215f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
22161da177e4SLinus Torvalds 
2217c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
22181da177e4SLinus Torvalds 
2219f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2220c9d76be6STejun Heo 	idr_preload_end();
2221c9d76be6STejun Heo 	if (r < 0)
22221da177e4SLinus Torvalds 		return r;
2223c9d76be6STejun Heo 	*minor = r;
2224c9d76be6STejun Heo 	return 0;
22251da177e4SLinus Torvalds }
22261da177e4SLinus Torvalds 
222783d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
22281da177e4SLinus Torvalds 
222953d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
223053d5914fSMikulas Patocka 
22314a0b4ddfSMike Snitzer static void dm_init_md_queue(struct mapped_device *md)
22324a0b4ddfSMike Snitzer {
22334a0b4ddfSMike Snitzer 	/*
22344a0b4ddfSMike Snitzer 	 * Request-based dm devices cannot be stacked on top of bio-based dm
2235bfebd1cdSMike Snitzer 	 * devices.  The type of this dm device may not have been decided yet.
22364a0b4ddfSMike Snitzer 	 * The type is decided at the first table loading time.
22374a0b4ddfSMike Snitzer 	 * To prevent problematic device stacking, clear the queue flag
22384a0b4ddfSMike Snitzer 	 * for request stacking support until then.
22394a0b4ddfSMike Snitzer 	 *
22404a0b4ddfSMike Snitzer 	 * This queue is new, so no concurrency on the queue_flags.
22414a0b4ddfSMike Snitzer 	 */
22424a0b4ddfSMike Snitzer 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
2243ad5f498fSMikulas Patocka 
2244ad5f498fSMikulas Patocka 	/*
2245ad5f498fSMikulas Patocka 	 * Initialize data that will only be used by a non-blk-mq DM queue
2246ad5f498fSMikulas Patocka 	 * - must do so here (in alloc_dev callchain) before queue is used
2247ad5f498fSMikulas Patocka 	 */
2248ad5f498fSMikulas Patocka 	md->queue->queuedata = md;
2249ad5f498fSMikulas Patocka 	md->queue->backing_dev_info.congested_data = md;
2250bfebd1cdSMike Snitzer }
22514a0b4ddfSMike Snitzer 
2252eca7ee6dSMike Snitzer static void dm_init_normal_md_queue(struct mapped_device *md)
2253bfebd1cdSMike Snitzer {
225417e149b8SMike Snitzer 	md->use_blk_mq = false;
2255bfebd1cdSMike Snitzer 	dm_init_md_queue(md);
2256bfebd1cdSMike Snitzer 
2257bfebd1cdSMike Snitzer 	/*
2258bfebd1cdSMike Snitzer 	 * Initialize aspects of queue that aren't relevant for blk-mq
2259bfebd1cdSMike Snitzer 	 */
22604a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
22614a0b4ddfSMike Snitzer 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
22624a0b4ddfSMike Snitzer }
22634a0b4ddfSMike Snitzer 
22640f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md)
22650f20972fSMike Snitzer {
22660f20972fSMike Snitzer 	if (md->wq)
22670f20972fSMike Snitzer 		destroy_workqueue(md->wq);
22680f20972fSMike Snitzer 	if (md->kworker_task)
22690f20972fSMike Snitzer 		kthread_stop(md->kworker_task);
22700f20972fSMike Snitzer 	mempool_destroy(md->io_pool);
22710f20972fSMike Snitzer 	mempool_destroy(md->rq_pool);
22720f20972fSMike Snitzer 	if (md->bs)
22730f20972fSMike Snitzer 		bioset_free(md->bs);
22740f20972fSMike Snitzer 
2275b06075a9SMikulas Patocka 	cleanup_srcu_struct(&md->io_barrier);
2276b06075a9SMikulas Patocka 
22770f20972fSMike Snitzer 	if (md->disk) {
22780f20972fSMike Snitzer 		spin_lock(&_minor_lock);
22790f20972fSMike Snitzer 		md->disk->private_data = NULL;
22800f20972fSMike Snitzer 		spin_unlock(&_minor_lock);
22810f20972fSMike Snitzer 		del_gendisk(md->disk);
22820f20972fSMike Snitzer 		put_disk(md->disk);
22830f20972fSMike Snitzer 	}
22840f20972fSMike Snitzer 
22850f20972fSMike Snitzer 	if (md->queue)
22860f20972fSMike Snitzer 		blk_cleanup_queue(md->queue);
22870f20972fSMike Snitzer 
22880f20972fSMike Snitzer 	if (md->bdev) {
22890f20972fSMike Snitzer 		bdput(md->bdev);
22900f20972fSMike Snitzer 		md->bdev = NULL;
22910f20972fSMike Snitzer 	}
22920f20972fSMike Snitzer }
22930f20972fSMike Snitzer 
22941da177e4SLinus Torvalds /*
22951da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
22961da177e4SLinus Torvalds  */
22972b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
22981da177e4SLinus Torvalds {
22991da177e4SLinus Torvalds 	int r;
2300cf13ab8eSFrederik Deweerdt 	struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
2301ba61fdd1SJeff Mahoney 	void *old_md;
23021da177e4SLinus Torvalds 
23031da177e4SLinus Torvalds 	if (!md) {
23041da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
23051da177e4SLinus Torvalds 		return NULL;
23061da177e4SLinus Torvalds 	}
23071da177e4SLinus Torvalds 
230810da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
23096ed7ade8SMilan Broz 		goto bad_module_get;
231010da4f79SJeff Mahoney 
23111da177e4SLinus Torvalds 	/* get a minor number for the dev */
23122b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
2313cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
23142b06cfffSAlasdair G Kergon 	else
2315cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
23161da177e4SLinus Torvalds 	if (r < 0)
23176ed7ade8SMilan Broz 		goto bad_minor;
23181da177e4SLinus Torvalds 
231983d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
232083d5e5b0SMikulas Patocka 	if (r < 0)
232183d5e5b0SMikulas Patocka 		goto bad_io_barrier;
232283d5e5b0SMikulas Patocka 
232317e149b8SMike Snitzer 	md->use_blk_mq = use_blk_mq;
2324*591ddcfcSMike Snitzer 	md->init_tio_pdu = false;
2325a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
2326e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
2327a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
232886f1152bSBenjamin Marzinski 	mutex_init(&md->table_devices_lock);
2329022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
23301da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
23315c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
23321da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
23337a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
23347a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
233586f1152bSBenjamin Marzinski 	INIT_LIST_HEAD(&md->table_devices);
23367a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
23371da177e4SLinus Torvalds 
23384a0b4ddfSMike Snitzer 	md->queue = blk_alloc_queue(GFP_KERNEL);
23391da177e4SLinus Torvalds 	if (!md->queue)
23400f20972fSMike Snitzer 		goto bad;
23411da177e4SLinus Torvalds 
23424a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
23439faf400fSStefan Bader 
23441da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
23451da177e4SLinus Torvalds 	if (!md->disk)
23460f20972fSMike Snitzer 		goto bad;
23471da177e4SLinus Torvalds 
2348316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
2349316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
2350f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
235153d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
2352f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
23532995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
23542eb6e1e3SKeith Busch 	md->kworker_task = NULL;
2355f0b04115SJeff Mahoney 
23561da177e4SLinus Torvalds 	md->disk->major = _major;
23571da177e4SLinus Torvalds 	md->disk->first_minor = minor;
23581da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
23591da177e4SLinus Torvalds 	md->disk->queue = md->queue;
23601da177e4SLinus Torvalds 	md->disk->private_data = md;
23611da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
23621da177e4SLinus Torvalds 	add_disk(md->disk);
23637e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
23641da177e4SLinus Torvalds 
2365670368a8STejun Heo 	md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
2366304f3f6aSMilan Broz 	if (!md->wq)
23670f20972fSMike Snitzer 		goto bad;
2368304f3f6aSMilan Broz 
236932a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
237032a926daSMikulas Patocka 	if (!md->bdev)
23710f20972fSMike Snitzer 		goto bad;
237232a926daSMikulas Patocka 
23736a8736d1STejun Heo 	bio_init(&md->flush_bio);
23746a8736d1STejun Heo 	md->flush_bio.bi_bdev = md->bdev;
23756a8736d1STejun Heo 	md->flush_bio.bi_rw = WRITE_FLUSH;
23766a8736d1STejun Heo 
2377fd2ed4d2SMikulas Patocka 	dm_stats_init(&md->stats);
2378fd2ed4d2SMikulas Patocka 
2379ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
2380f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
2381ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
2382f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2383ba61fdd1SJeff Mahoney 
2384ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
2385ba61fdd1SJeff Mahoney 
23861da177e4SLinus Torvalds 	return md;
23871da177e4SLinus Torvalds 
23880f20972fSMike Snitzer bad:
23890f20972fSMike Snitzer 	cleanup_mapped_device(md);
239083d5e5b0SMikulas Patocka bad_io_barrier:
23911da177e4SLinus Torvalds 	free_minor(minor);
23926ed7ade8SMilan Broz bad_minor:
239310da4f79SJeff Mahoney 	module_put(THIS_MODULE);
23946ed7ade8SMilan Broz bad_module_get:
23951da177e4SLinus Torvalds 	kfree(md);
23961da177e4SLinus Torvalds 	return NULL;
23971da177e4SLinus Torvalds }
23981da177e4SLinus Torvalds 
2399ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
2400ae9da83fSJun'ichi Nomura 
24011da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
24021da177e4SLinus Torvalds {
2403f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
240463d94e48SJun'ichi Nomura 
2405ae9da83fSJun'ichi Nomura 	unlock_fs(md);
24062eb6e1e3SKeith Busch 
24070f20972fSMike Snitzer 	cleanup_mapped_device(md);
24081c357a1eSMike Snitzer 	if (md->tag_set) {
24091c357a1eSMike Snitzer 		blk_mq_free_tag_set(md->tag_set);
24101c357a1eSMike Snitzer 		kfree(md->tag_set);
24111c357a1eSMike Snitzer 	}
24120f20972fSMike Snitzer 
24130f20972fSMike Snitzer 	free_table_devices(&md->table_devices);
24140f20972fSMike Snitzer 	dm_stats_cleanup(&md->stats);
241563a4f065SMike Snitzer 	free_minor(minor);
241663a4f065SMike Snitzer 
241710da4f79SJeff Mahoney 	module_put(THIS_MODULE);
24181da177e4SLinus Torvalds 	kfree(md);
24191da177e4SLinus Torvalds }
24201da177e4SLinus Torvalds 
2421e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2422e6ee8c0bSKiyoshi Ueda {
2423c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
2424e6ee8c0bSKiyoshi Ueda 
24254e6e36c3SMike Snitzer 	if (md->bs) {
24264e6e36c3SMike Snitzer 		/* The md already has necessary mempools. */
24274e6e36c3SMike Snitzer 		if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
2428c0820cf5SMikulas Patocka 			/*
242916245bdcSJun'ichi Nomura 			 * Reload bioset because front_pad may have changed
243016245bdcSJun'ichi Nomura 			 * because a different table was loaded.
2431c0820cf5SMikulas Patocka 			 */
2432c0820cf5SMikulas Patocka 			bioset_free(md->bs);
2433c0820cf5SMikulas Patocka 			md->bs = p->bs;
2434c0820cf5SMikulas Patocka 			p->bs = NULL;
2435c0820cf5SMikulas Patocka 		}
2436cbc4e3c1SMike Snitzer 		/*
24374e6e36c3SMike Snitzer 		 * There's no need to reload with request-based dm
24384e6e36c3SMike Snitzer 		 * because the size of front_pad doesn't change.
24394e6e36c3SMike Snitzer 		 * Note for future: If you are to reload bioset,
24404e6e36c3SMike Snitzer 		 * prep-ed requests in the queue may refer
24414e6e36c3SMike Snitzer 		 * to bio from the old bioset, so you must walk
24424e6e36c3SMike Snitzer 		 * through the queue to unprep.
2443cbc4e3c1SMike Snitzer 		 */
2444cbc4e3c1SMike Snitzer 		goto out;
2445cbc4e3c1SMike Snitzer 	}
2446cbc4e3c1SMike Snitzer 
2447cbc4e3c1SMike Snitzer 	BUG_ON(!p || md->io_pool || md->rq_pool || md->bs);
2448e6ee8c0bSKiyoshi Ueda 
2449e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
2450e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
24511ae49ea2SMike Snitzer 	md->rq_pool = p->rq_pool;
24521ae49ea2SMike Snitzer 	p->rq_pool = NULL;
2453e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
2454e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
24554e6e36c3SMike Snitzer 
2456e6ee8c0bSKiyoshi Ueda out:
245702233342SMike Snitzer 	/* mempool bind completed, no longer need any mempools in the table */
2458e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
2459e6ee8c0bSKiyoshi Ueda }
2460e6ee8c0bSKiyoshi Ueda 
24611da177e4SLinus Torvalds /*
24621da177e4SLinus Torvalds  * Bind a table to the device.
24631da177e4SLinus Torvalds  */
24641da177e4SLinus Torvalds static void event_callback(void *context)
24651da177e4SLinus Torvalds {
24667a8c3d3bSMike Anderson 	unsigned long flags;
24677a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
24681da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
24691da177e4SLinus Torvalds 
24707a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
24717a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
24727a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
24737a8c3d3bSMike Anderson 
2474ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
24757a8c3d3bSMike Anderson 
24761da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
24771da177e4SLinus Torvalds 	wake_up(&md->eventq);
24781da177e4SLinus Torvalds }
24791da177e4SLinus Torvalds 
2480c217649bSMike Snitzer /*
2481c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
2482c217649bSMike Snitzer  */
24834e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
24841da177e4SLinus Torvalds {
24854e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
24861da177e4SLinus Torvalds 
2487db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
24881da177e4SLinus Torvalds }
24891da177e4SLinus Torvalds 
2490042d2a9bSAlasdair G Kergon /*
2491042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2492042d2a9bSAlasdair G Kergon  */
2493042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2494754c5fc7SMike Snitzer 			       struct queue_limits *limits)
24951da177e4SLinus Torvalds {
2496042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
2497165125e1SJens Axboe 	struct request_queue *q = md->queue;
24981da177e4SLinus Torvalds 	sector_t size;
24991da177e4SLinus Torvalds 
25001da177e4SLinus Torvalds 	size = dm_table_get_size(t);
25013ac51e74SDarrick J. Wong 
25023ac51e74SDarrick J. Wong 	/*
25033ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
25043ac51e74SDarrick J. Wong 	 */
2505fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
25063ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
25073ac51e74SDarrick J. Wong 
25084e90188bSAlasdair G Kergon 	__set_size(md, size);
25091da177e4SLinus Torvalds 
2510cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
25112ca3310eSAlasdair G Kergon 
2512e6ee8c0bSKiyoshi Ueda 	/*
2513e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
2514e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
2515e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
2516e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
2517e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
2518e6ee8c0bSKiyoshi Ueda 	 */
251916f12266SMike Snitzer 	if (dm_table_request_based(t)) {
2520eca7ee6dSMike Snitzer 		dm_stop_queue(q);
252116f12266SMike Snitzer 		/*
252216f12266SMike Snitzer 		 * Leverage the fact that request-based DM targets are
252316f12266SMike Snitzer 		 * immutable singletons and establish md->immutable_target
252416f12266SMike Snitzer 		 * - used to optimize both dm_request_fn and dm_mq_queue_rq
252516f12266SMike Snitzer 		 */
252616f12266SMike Snitzer 		md->immutable_target = dm_table_get_immutable_target(t);
252716f12266SMike Snitzer 	}
2528e6ee8c0bSKiyoshi Ueda 
2529e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
2530e6ee8c0bSKiyoshi Ueda 
2531a12f5d48SEric Dumazet 	old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
253283d5e5b0SMikulas Patocka 	rcu_assign_pointer(md->map, t);
253336a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
253436a0456fSAlasdair G Kergon 
2535754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
253641abc4e1SHannes Reinecke 	if (old_map)
253783d5e5b0SMikulas Patocka 		dm_sync_table(md);
25382ca3310eSAlasdair G Kergon 
2539042d2a9bSAlasdair G Kergon 	return old_map;
25401da177e4SLinus Torvalds }
25411da177e4SLinus Torvalds 
2542a7940155SAlasdair G Kergon /*
2543a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2544a7940155SAlasdair G Kergon  */
2545a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
25461da177e4SLinus Torvalds {
2547a12f5d48SEric Dumazet 	struct dm_table *map = rcu_dereference_protected(md->map, 1);
25481da177e4SLinus Torvalds 
25491da177e4SLinus Torvalds 	if (!map)
2550a7940155SAlasdair G Kergon 		return NULL;
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
25539cdb8520SMonam Agarwal 	RCU_INIT_POINTER(md->map, NULL);
255483d5e5b0SMikulas Patocka 	dm_sync_table(md);
2555a7940155SAlasdair G Kergon 
2556a7940155SAlasdair G Kergon 	return map;
25571da177e4SLinus Torvalds }
25581da177e4SLinus Torvalds 
25591da177e4SLinus Torvalds /*
25601da177e4SLinus Torvalds  * Constructor for a new device.
25611da177e4SLinus Torvalds  */
25622b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
25631da177e4SLinus Torvalds {
25641da177e4SLinus Torvalds 	struct mapped_device *md;
25651da177e4SLinus Torvalds 
25662b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
25671da177e4SLinus Torvalds 	if (!md)
25681da177e4SLinus Torvalds 		return -ENXIO;
25691da177e4SLinus Torvalds 
2570784aae73SMilan Broz 	dm_sysfs_init(md);
2571784aae73SMilan Broz 
25721da177e4SLinus Torvalds 	*result = md;
25731da177e4SLinus Torvalds 	return 0;
25741da177e4SLinus Torvalds }
25751da177e4SLinus Torvalds 
2576a5664dadSMike Snitzer /*
2577a5664dadSMike Snitzer  * Functions to manage md->type.
2578a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2579a5664dadSMike Snitzer  */
2580a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2581a5664dadSMike Snitzer {
2582a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2583a5664dadSMike Snitzer }
2584a5664dadSMike Snitzer 
2585a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2586a5664dadSMike Snitzer {
2587a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2588a5664dadSMike Snitzer }
2589a5664dadSMike Snitzer 
2590a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type)
2591a5664dadSMike Snitzer {
259200c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2593a5664dadSMike Snitzer 	md->type = type;
2594a5664dadSMike Snitzer }
2595a5664dadSMike Snitzer 
2596a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md)
2597a5664dadSMike Snitzer {
2598a5664dadSMike Snitzer 	return md->type;
2599a5664dadSMike Snitzer }
2600a5664dadSMike Snitzer 
260136a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
260236a0456fSAlasdair G Kergon {
260336a0456fSAlasdair G Kergon 	return md->immutable_target_type;
260436a0456fSAlasdair G Kergon }
260536a0456fSAlasdair G Kergon 
26064a0b4ddfSMike Snitzer /*
2607f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
2608f84cb8a4SMike Snitzer  * count on 'md'.
2609f84cb8a4SMike Snitzer  */
2610f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2611f84cb8a4SMike Snitzer {
2612f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
2613f84cb8a4SMike Snitzer 	return &md->queue->limits;
2614f84cb8a4SMike Snitzer }
2615f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2616f84cb8a4SMike Snitzer 
2617eca7ee6dSMike Snitzer static void dm_old_init_rq_based_worker_thread(struct mapped_device *md)
2618bfebd1cdSMike Snitzer {
2619bfebd1cdSMike Snitzer 	/* Initialize the request-based DM worker thread */
2620bfebd1cdSMike Snitzer 	init_kthread_worker(&md->kworker);
2621bfebd1cdSMike Snitzer 	md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker,
2622bfebd1cdSMike Snitzer 				       "kdmwork-%s", dm_device_name(md));
2623bfebd1cdSMike Snitzer }
2624bfebd1cdSMike Snitzer 
2625f84cb8a4SMike Snitzer /*
2626eca7ee6dSMike Snitzer  * Fully initialize a .request_fn request-based queue.
26274a0b4ddfSMike Snitzer  */
2628eca7ee6dSMike Snitzer static int dm_old_init_request_queue(struct mapped_device *md)
26294a0b4ddfSMike Snitzer {
26304a0b4ddfSMike Snitzer 	struct request_queue *q = NULL;
26314a0b4ddfSMike Snitzer 
26324a0b4ddfSMike Snitzer 	/* Fully initialize the queue */
26334a0b4ddfSMike Snitzer 	q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
26344a0b4ddfSMike Snitzer 	if (!q)
2635bfebd1cdSMike Snitzer 		return -EINVAL;
26364a0b4ddfSMike Snitzer 
26370ce65797SMike Snitzer 	/* disable dm_request_fn's merge heuristic by default */
26380ce65797SMike Snitzer 	md->seq_rq_merge_deadline_usecs = 0;
26390ce65797SMike Snitzer 
26404a0b4ddfSMike Snitzer 	md->queue = q;
2641eca7ee6dSMike Snitzer 	dm_init_normal_md_queue(md);
26424a0b4ddfSMike Snitzer 	blk_queue_softirq_done(md->queue, dm_softirq_done);
2643eca7ee6dSMike Snitzer 	blk_queue_prep_rq(md->queue, dm_old_prep_fn);
26444a0b4ddfSMike Snitzer 
2645eca7ee6dSMike Snitzer 	dm_old_init_rq_based_worker_thread(md);
26462eb6e1e3SKeith Busch 
26474a0b4ddfSMike Snitzer 	elv_register_queue(md->queue);
26484a0b4ddfSMike Snitzer 
2649bfebd1cdSMike Snitzer 	return 0;
2650bfebd1cdSMike Snitzer }
2651bfebd1cdSMike Snitzer 
2652bfebd1cdSMike Snitzer static int dm_mq_init_request(void *data, struct request *rq,
2653bfebd1cdSMike Snitzer 			      unsigned int hctx_idx, unsigned int request_idx,
2654bfebd1cdSMike Snitzer 			      unsigned int numa_node)
2655bfebd1cdSMike Snitzer {
2656bfebd1cdSMike Snitzer 	struct mapped_device *md = data;
2657bfebd1cdSMike Snitzer 	struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2658bfebd1cdSMike Snitzer 
2659bfebd1cdSMike Snitzer 	/*
2660bfebd1cdSMike Snitzer 	 * Must initialize md member of tio, otherwise it won't
2661bfebd1cdSMike Snitzer 	 * be available in dm_mq_queue_rq.
2662bfebd1cdSMike Snitzer 	 */
2663bfebd1cdSMike Snitzer 	tio->md = md;
2664bfebd1cdSMike Snitzer 
2665*591ddcfcSMike Snitzer 	if (md->init_tio_pdu) {
2666*591ddcfcSMike Snitzer 		/* target-specific per-io data is immediately after the tio */
2667*591ddcfcSMike Snitzer 		tio->info.ptr = tio + 1;
2668*591ddcfcSMike Snitzer 	}
2669*591ddcfcSMike Snitzer 
2670bfebd1cdSMike Snitzer 	return 0;
2671bfebd1cdSMike Snitzer }
2672bfebd1cdSMike Snitzer 
2673bfebd1cdSMike Snitzer static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
2674bfebd1cdSMike Snitzer 			  const struct blk_mq_queue_data *bd)
2675bfebd1cdSMike Snitzer {
2676bfebd1cdSMike Snitzer 	struct request *rq = bd->rq;
2677bfebd1cdSMike Snitzer 	struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2678bfebd1cdSMike Snitzer 	struct mapped_device *md = tio->md;
267916f12266SMike Snitzer 	struct dm_target *ti = md->immutable_target;
268016f12266SMike Snitzer 
268116f12266SMike Snitzer 	if (unlikely(!ti)) {
2682bfebd1cdSMike Snitzer 		int srcu_idx;
2683bfebd1cdSMike Snitzer 		struct dm_table *map = dm_get_live_table(md, &srcu_idx);
2684bfebd1cdSMike Snitzer 
268516f12266SMike Snitzer 		ti = dm_table_find_target(map, 0);
2686bfebd1cdSMike Snitzer 		dm_put_live_table(md, srcu_idx);
2687bfebd1cdSMike Snitzer 	}
2688bfebd1cdSMike Snitzer 
2689bfebd1cdSMike Snitzer 	if (ti->type->busy && ti->type->busy(ti))
2690bfebd1cdSMike Snitzer 		return BLK_MQ_RQ_QUEUE_BUSY;
2691bfebd1cdSMike Snitzer 
2692bfebd1cdSMike Snitzer 	dm_start_request(md, rq);
2693bfebd1cdSMike Snitzer 
2694bfebd1cdSMike Snitzer 	/* Init tio using md established in .init_request */
2695bfebd1cdSMike Snitzer 	init_tio(tio, rq, md);
2696bfebd1cdSMike Snitzer 
269702233342SMike Snitzer 	/*
269802233342SMike Snitzer 	 * Establish tio->ti before queuing work (map_tio_request)
269902233342SMike Snitzer 	 * or making direct call to map_request().
270002233342SMike Snitzer 	 */
2701bfebd1cdSMike Snitzer 	tio->ti = ti;
270202233342SMike Snitzer 
270302233342SMike Snitzer 	/* Direct call is fine since .queue_rq allows allocations */
270445714fbeSMike Snitzer 	if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) {
270545714fbeSMike Snitzer 		/* Undo dm_start_request() before requeuing */
2706e262f347SMikulas Patocka 		rq_end_stats(md, rq);
270745714fbeSMike Snitzer 		rq_completed(md, rq_data_dir(rq), false);
270845714fbeSMike Snitzer 		return BLK_MQ_RQ_QUEUE_BUSY;
270945714fbeSMike Snitzer 	}
2710bfebd1cdSMike Snitzer 
2711bfebd1cdSMike Snitzer 	return BLK_MQ_RQ_QUEUE_OK;
2712bfebd1cdSMike Snitzer }
2713bfebd1cdSMike Snitzer 
2714bfebd1cdSMike Snitzer static struct blk_mq_ops dm_mq_ops = {
2715bfebd1cdSMike Snitzer 	.queue_rq = dm_mq_queue_rq,
2716bfebd1cdSMike Snitzer 	.map_queue = blk_mq_map_queue,
2717bfebd1cdSMike Snitzer 	.complete = dm_softirq_done,
2718bfebd1cdSMike Snitzer 	.init_request = dm_mq_init_request,
2719bfebd1cdSMike Snitzer };
2720bfebd1cdSMike Snitzer 
2721*591ddcfcSMike Snitzer static int dm_mq_init_request_queue(struct mapped_device *md,
2722*591ddcfcSMike Snitzer 				    struct dm_target *immutable_tgt)
2723bfebd1cdSMike Snitzer {
2724bfebd1cdSMike Snitzer 	struct request_queue *q;
2725bfebd1cdSMike Snitzer 	int err;
2726bfebd1cdSMike Snitzer 
2727c5248f79SMike Snitzer 	if (dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) {
2728c5248f79SMike Snitzer 		DMERR("request-based dm-mq may only be stacked on blk-mq device(s)");
2729c5248f79SMike Snitzer 		return -EINVAL;
2730c5248f79SMike Snitzer 	}
2731c5248f79SMike Snitzer 
27321c357a1eSMike Snitzer 	md->tag_set = kzalloc(sizeof(struct blk_mq_tag_set), GFP_KERNEL);
27331c357a1eSMike Snitzer 	if (!md->tag_set)
27341c357a1eSMike Snitzer 		return -ENOMEM;
27351c357a1eSMike Snitzer 
27361c357a1eSMike Snitzer 	md->tag_set->ops = &dm_mq_ops;
27371c357a1eSMike Snitzer 	md->tag_set->queue_depth = dm_get_blk_mq_queue_depth();
27381c357a1eSMike Snitzer 	md->tag_set->numa_node = NUMA_NO_NODE;
27391c357a1eSMike Snitzer 	md->tag_set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
27401c357a1eSMike Snitzer 	md->tag_set->nr_hw_queues = dm_get_blk_mq_nr_hw_queues();
27411c357a1eSMike Snitzer 	md->tag_set->driver_data = md;
27421c357a1eSMike Snitzer 
27431c357a1eSMike Snitzer 	md->tag_set->cmd_size = sizeof(struct dm_rq_target_io);
2744*591ddcfcSMike Snitzer 	if (immutable_tgt && immutable_tgt->per_io_data_size) {
2745*591ddcfcSMike Snitzer 		/* any target-specific per-io data is immediately after the tio */
2746*591ddcfcSMike Snitzer 		md->tag_set->cmd_size += immutable_tgt->per_io_data_size;
2747*591ddcfcSMike Snitzer 		md->init_tio_pdu = true;
2748*591ddcfcSMike Snitzer 	}
2749bfebd1cdSMike Snitzer 
27501c357a1eSMike Snitzer 	err = blk_mq_alloc_tag_set(md->tag_set);
2751bfebd1cdSMike Snitzer 	if (err)
27521c357a1eSMike Snitzer 		goto out_kfree_tag_set;
2753bfebd1cdSMike Snitzer 
27541c357a1eSMike Snitzer 	q = blk_mq_init_allocated_queue(md->tag_set, md->queue);
2755bfebd1cdSMike Snitzer 	if (IS_ERR(q)) {
2756bfebd1cdSMike Snitzer 		err = PTR_ERR(q);
2757bfebd1cdSMike Snitzer 		goto out_tag_set;
2758bfebd1cdSMike Snitzer 	}
2759bfebd1cdSMike Snitzer 	md->queue = q;
2760bfebd1cdSMike Snitzer 	dm_init_md_queue(md);
2761bfebd1cdSMike Snitzer 
2762bfebd1cdSMike Snitzer 	/* backfill 'mq' sysfs registration normally done in blk_register_queue */
2763bfebd1cdSMike Snitzer 	blk_mq_register_disk(md->disk);
2764bfebd1cdSMike Snitzer 
2765bfebd1cdSMike Snitzer 	return 0;
2766bfebd1cdSMike Snitzer 
2767bfebd1cdSMike Snitzer out_tag_set:
27681c357a1eSMike Snitzer 	blk_mq_free_tag_set(md->tag_set);
27691c357a1eSMike Snitzer out_kfree_tag_set:
27701c357a1eSMike Snitzer 	kfree(md->tag_set);
27711c357a1eSMike Snitzer 
2772bfebd1cdSMike Snitzer 	return err;
27734a0b4ddfSMike Snitzer }
27744a0b4ddfSMike Snitzer 
27754e6e36c3SMike Snitzer static unsigned filter_md_type(unsigned type, struct mapped_device *md)
27764e6e36c3SMike Snitzer {
27774e6e36c3SMike Snitzer 	if (type == DM_TYPE_BIO_BASED)
27784e6e36c3SMike Snitzer 		return type;
27794e6e36c3SMike Snitzer 
27804e6e36c3SMike Snitzer 	return !md->use_blk_mq ? DM_TYPE_REQUEST_BASED : DM_TYPE_MQ_REQUEST_BASED;
27814e6e36c3SMike Snitzer }
27824e6e36c3SMike Snitzer 
27834a0b4ddfSMike Snitzer /*
27844a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
27854a0b4ddfSMike Snitzer  */
2786*591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
27874a0b4ddfSMike Snitzer {
2788bfebd1cdSMike Snitzer 	int r;
278917e149b8SMike Snitzer 	unsigned md_type = filter_md_type(dm_get_md_type(md), md);
2790bfebd1cdSMike Snitzer 
2791bfebd1cdSMike Snitzer 	switch (md_type) {
2792bfebd1cdSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
2793eca7ee6dSMike Snitzer 		r = dm_old_init_request_queue(md);
2794bfebd1cdSMike Snitzer 		if (r) {
2795eca7ee6dSMike Snitzer 			DMERR("Cannot initialize queue for request-based mapped device");
2796bfebd1cdSMike Snitzer 			return r;
27974a0b4ddfSMike Snitzer 		}
2798bfebd1cdSMike Snitzer 		break;
2799bfebd1cdSMike Snitzer 	case DM_TYPE_MQ_REQUEST_BASED:
2800*591ddcfcSMike Snitzer 		r = dm_mq_init_request_queue(md, dm_table_get_immutable_target(t));
2801bfebd1cdSMike Snitzer 		if (r) {
2802eca7ee6dSMike Snitzer 			DMERR("Cannot initialize queue for request-based dm-mq mapped device");
2803bfebd1cdSMike Snitzer 			return r;
2804bfebd1cdSMike Snitzer 		}
2805bfebd1cdSMike Snitzer 		break;
2806bfebd1cdSMike Snitzer 	case DM_TYPE_BIO_BASED:
2807eca7ee6dSMike Snitzer 		dm_init_normal_md_queue(md);
2808ff36ab34SMike Snitzer 		blk_queue_make_request(md->queue, dm_make_request);
2809dbba42d8SMikulas Patocka 		/*
2810dbba42d8SMikulas Patocka 		 * DM handles splitting bios as needed.  Free the bio_split bioset
2811dbba42d8SMikulas Patocka 		 * since it won't be used (saves 1 process per bio-based DM device).
2812dbba42d8SMikulas Patocka 		 */
2813dbba42d8SMikulas Patocka 		bioset_free(md->queue->bio_split);
2814dbba42d8SMikulas Patocka 		md->queue->bio_split = NULL;
2815bfebd1cdSMike Snitzer 		break;
2816ff36ab34SMike Snitzer 	}
28174a0b4ddfSMike Snitzer 
28184a0b4ddfSMike Snitzer 	return 0;
28194a0b4ddfSMike Snitzer }
28204a0b4ddfSMike Snitzer 
28212bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev)
28221da177e4SLinus Torvalds {
28231da177e4SLinus Torvalds 	struct mapped_device *md;
28241da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
28251da177e4SLinus Torvalds 
28261da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
28271da177e4SLinus Torvalds 		return NULL;
28281da177e4SLinus Torvalds 
2829f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
28301da177e4SLinus Torvalds 
28311da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
28322bec1f4aSMikulas Patocka 	if (md) {
28332bec1f4aSMikulas Patocka 		if ((md == MINOR_ALLOCED ||
2834f331c029STejun Heo 		     (MINOR(disk_devt(dm_disk(md))) != minor) ||
2835abdc568bSKiyoshi Ueda 		     dm_deleting_md(md) ||
2836fba9f90eSJeff Mahoney 		     test_bit(DMF_FREEING, &md->flags))) {
2837637842cfSDavid Teigland 			md = NULL;
2838fba9f90eSJeff Mahoney 			goto out;
2839fba9f90eSJeff Mahoney 		}
28402bec1f4aSMikulas Patocka 		dm_get(md);
28412bec1f4aSMikulas Patocka 	}
28421da177e4SLinus Torvalds 
2843fba9f90eSJeff Mahoney out:
2844f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
28451da177e4SLinus Torvalds 
2846637842cfSDavid Teigland 	return md;
2847637842cfSDavid Teigland }
28483cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2849d229a958SDavid Teigland 
28509ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2851637842cfSDavid Teigland {
28529ade92a9SAlasdair G Kergon 	return md->interface_ptr;
28531da177e4SLinus Torvalds }
28541da177e4SLinus Torvalds 
28551da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
28561da177e4SLinus Torvalds {
28571da177e4SLinus Torvalds 	md->interface_ptr = ptr;
28581da177e4SLinus Torvalds }
28591da177e4SLinus Torvalds 
28601da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
28611da177e4SLinus Torvalds {
28621da177e4SLinus Torvalds 	atomic_inc(&md->holders);
28633f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
28641da177e4SLinus Torvalds }
28651da177e4SLinus Torvalds 
286609ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md)
286709ee96b2SMikulas Patocka {
286809ee96b2SMikulas Patocka 	spin_lock(&_minor_lock);
286909ee96b2SMikulas Patocka 	if (test_bit(DMF_FREEING, &md->flags)) {
287009ee96b2SMikulas Patocka 		spin_unlock(&_minor_lock);
287109ee96b2SMikulas Patocka 		return -EBUSY;
287209ee96b2SMikulas Patocka 	}
287309ee96b2SMikulas Patocka 	dm_get(md);
287409ee96b2SMikulas Patocka 	spin_unlock(&_minor_lock);
287509ee96b2SMikulas Patocka 	return 0;
287609ee96b2SMikulas Patocka }
287709ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold);
287809ee96b2SMikulas Patocka 
287972d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
288072d94861SAlasdair G Kergon {
288172d94861SAlasdair G Kergon 	return md->name;
288272d94861SAlasdair G Kergon }
288372d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
288472d94861SAlasdair G Kergon 
28853f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
28861da177e4SLinus Torvalds {
28871134e5aeSMike Anderson 	struct dm_table *map;
288883d5e5b0SMikulas Patocka 	int srcu_idx;
28891da177e4SLinus Torvalds 
28903f77316dSKiyoshi Ueda 	might_sleep();
2891fba9f90eSJeff Mahoney 
289263a4f065SMike Snitzer 	spin_lock(&_minor_lock);
28933f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2894fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2895f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
28963f77316dSKiyoshi Ueda 
289702233342SMike Snitzer 	if (dm_request_based(md) && md->kworker_task)
28982eb6e1e3SKeith Busch 		flush_kthread_worker(&md->kworker);
28992eb6e1e3SKeith Busch 
2900ab7c7bb6SMikulas Patocka 	/*
2901ab7c7bb6SMikulas Patocka 	 * Take suspend_lock so that presuspend and postsuspend methods
2902ab7c7bb6SMikulas Patocka 	 * do not race with internal suspend.
2903ab7c7bb6SMikulas Patocka 	 */
2904ab7c7bb6SMikulas Patocka 	mutex_lock(&md->suspend_lock);
29052a708cffSJunichi Nomura 	map = dm_get_live_table(md, &srcu_idx);
29064f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
29071da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
29081da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
29091da177e4SLinus Torvalds 	}
291083d5e5b0SMikulas Patocka 	/* dm_put_live_table must be before msleep, otherwise deadlock is possible */
291183d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
29122a708cffSJunichi Nomura 	mutex_unlock(&md->suspend_lock);
291383d5e5b0SMikulas Patocka 
29143f77316dSKiyoshi Ueda 	/*
29153f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
29163f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
29173f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
29183f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
29193f77316dSKiyoshi Ueda 	 */
29203f77316dSKiyoshi Ueda 	if (wait)
29213f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
29223f77316dSKiyoshi Ueda 			msleep(1);
29233f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
29243f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
29253f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
29263f77316dSKiyoshi Ueda 
2927784aae73SMilan Broz 	dm_sysfs_exit(md);
2928a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
29291da177e4SLinus Torvalds 	free_dev(md);
29301da177e4SLinus Torvalds }
29313f77316dSKiyoshi Ueda 
29323f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
29333f77316dSKiyoshi Ueda {
29343f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
29353f77316dSKiyoshi Ueda }
29363f77316dSKiyoshi Ueda 
29373f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
29383f77316dSKiyoshi Ueda {
29393f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
29403f77316dSKiyoshi Ueda }
29413f77316dSKiyoshi Ueda 
29423f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
29433f77316dSKiyoshi Ueda {
29443f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
29451da177e4SLinus Torvalds }
294679eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
29471da177e4SLinus Torvalds 
2948401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
294946125c1cSMilan Broz {
295046125c1cSMilan Broz 	int r = 0;
2951b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
2952b44ebeb0SMikulas Patocka 
2953b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
295446125c1cSMilan Broz 
295546125c1cSMilan Broz 	while (1) {
2956401600dfSMikulas Patocka 		set_current_state(interruptible);
295746125c1cSMilan Broz 
2958b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
295946125c1cSMilan Broz 			break;
296046125c1cSMilan Broz 
2961401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
2962401600dfSMikulas Patocka 		    signal_pending(current)) {
296346125c1cSMilan Broz 			r = -EINTR;
296446125c1cSMilan Broz 			break;
296546125c1cSMilan Broz 		}
296646125c1cSMilan Broz 
296746125c1cSMilan Broz 		io_schedule();
296846125c1cSMilan Broz 	}
296946125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
297046125c1cSMilan Broz 
2971b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
2972b44ebeb0SMikulas Patocka 
297346125c1cSMilan Broz 	return r;
297446125c1cSMilan Broz }
297546125c1cSMilan Broz 
29761da177e4SLinus Torvalds /*
29771da177e4SLinus Torvalds  * Process the deferred bios
29781da177e4SLinus Torvalds  */
2979ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
29801da177e4SLinus Torvalds {
2981ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2982ef208587SMikulas Patocka 						work);
29836d6f10dfSMilan Broz 	struct bio *c;
298483d5e5b0SMikulas Patocka 	int srcu_idx;
298583d5e5b0SMikulas Patocka 	struct dm_table *map;
29861da177e4SLinus Torvalds 
298783d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
2988ef208587SMikulas Patocka 
29893b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2990022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2991022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2992022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2993022c2611SMikulas Patocka 
29946a8736d1STejun Heo 		if (!c)
2995df12ee99SAlasdair G Kergon 			break;
299673d410c0SMilan Broz 
2997e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2998e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2999af7e466aSMikulas Patocka 		else
300083d5e5b0SMikulas Patocka 			__split_and_process_bio(md, map, c);
3001e6ee8c0bSKiyoshi Ueda 	}
30023b00b203SMikulas Patocka 
300383d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
30041da177e4SLinus Torvalds }
30051da177e4SLinus Torvalds 
30069a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
3007304f3f6aSMilan Broz {
30083b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
30094e857c58SPeter Zijlstra 	smp_mb__after_atomic();
301053d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
3011304f3f6aSMilan Broz }
3012304f3f6aSMilan Broz 
30131da177e4SLinus Torvalds /*
3014042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
30151da177e4SLinus Torvalds  */
3016042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
30171da177e4SLinus Torvalds {
301887eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
3019754c5fc7SMike Snitzer 	struct queue_limits limits;
3020042d2a9bSAlasdair G Kergon 	int r;
30211da177e4SLinus Torvalds 
3022e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
30231da177e4SLinus Torvalds 
30241da177e4SLinus Torvalds 	/* device must be suspended */
30254f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
302693c534aeSAlasdair G Kergon 		goto out;
30271da177e4SLinus Torvalds 
30283ae70656SMike Snitzer 	/*
30293ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
30303ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
30313ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
30323ae70656SMike Snitzer 	 * reappear.
30333ae70656SMike Snitzer 	 */
30343ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
303583d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
30363ae70656SMike Snitzer 		if (live_map)
30373ae70656SMike Snitzer 			limits = md->queue->limits;
303883d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
30393ae70656SMike Snitzer 	}
30403ae70656SMike Snitzer 
304187eb5b21SMike Christie 	if (!live_map) {
3042754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
3043042d2a9bSAlasdair G Kergon 		if (r) {
3044042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
3045754c5fc7SMike Snitzer 			goto out;
3046042d2a9bSAlasdair G Kergon 		}
304787eb5b21SMike Christie 	}
3048754c5fc7SMike Snitzer 
3049042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
30501da177e4SLinus Torvalds 
305193c534aeSAlasdair G Kergon out:
3052e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
3053042d2a9bSAlasdair G Kergon 	return map;
30541da177e4SLinus Torvalds }
30551da177e4SLinus Torvalds 
30561da177e4SLinus Torvalds /*
30571da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
30581da177e4SLinus Torvalds  * device.
30591da177e4SLinus Torvalds  */
30602ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
30611da177e4SLinus Torvalds {
3062e39e2e95SAlasdair G Kergon 	int r;
30631da177e4SLinus Torvalds 
30641da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
3065dfbe03f6SAlasdair G Kergon 
3066db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
3067dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
3068cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
3069e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
3070e39e2e95SAlasdair G Kergon 		return r;
3071dfbe03f6SAlasdair G Kergon 	}
3072dfbe03f6SAlasdair G Kergon 
3073aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
3074aa8d7c2fSAlasdair G Kergon 
30751da177e4SLinus Torvalds 	return 0;
30761da177e4SLinus Torvalds }
30771da177e4SLinus Torvalds 
30782ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
30791da177e4SLinus Torvalds {
3080aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
3081aa8d7c2fSAlasdair G Kergon 		return;
3082aa8d7c2fSAlasdair G Kergon 
3083db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
30841da177e4SLinus Torvalds 	md->frozen_sb = NULL;
3085aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
30861da177e4SLinus Torvalds }
30871da177e4SLinus Torvalds 
30881da177e4SLinus Torvalds /*
3089ffcc3936SMike Snitzer  * If __dm_suspend returns 0, the device is completely quiescent
3090ffcc3936SMike Snitzer  * now. There is no request-processing activity. All new requests
3091ffcc3936SMike Snitzer  * are being added to md->deferred list.
3092cec47e3dSKiyoshi Ueda  *
3093ffcc3936SMike Snitzer  * Caller must hold md->suspend_lock
3094cec47e3dSKiyoshi Ueda  */
3095ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
3096ffcc3936SMike Snitzer 			unsigned suspend_flags, int interruptible)
30971da177e4SLinus Torvalds {
3098ffcc3936SMike Snitzer 	bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
3099ffcc3936SMike Snitzer 	bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
3100ffcc3936SMike Snitzer 	int r;
3101cf222b37SAlasdair G Kergon 
31022e93ccc1SKiyoshi Ueda 	/*
31032e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
31042e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
31052e93ccc1SKiyoshi Ueda 	 */
31062e93ccc1SKiyoshi Ueda 	if (noflush)
31072e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
31082e93ccc1SKiyoshi Ueda 
3109d67ee213SMike Snitzer 	/*
3110d67ee213SMike Snitzer 	 * This gets reverted if there's an error later and the targets
3111d67ee213SMike Snitzer 	 * provide the .presuspend_undo hook.
3112d67ee213SMike Snitzer 	 */
31131da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
31141da177e4SLinus Torvalds 
31152e93ccc1SKiyoshi Ueda 	/*
31169f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
31179f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
31189f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
31199f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
31202e93ccc1SKiyoshi Ueda 	 */
312132a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
31222ca3310eSAlasdair G Kergon 		r = lock_fs(md);
3123d67ee213SMike Snitzer 		if (r) {
3124d67ee213SMike Snitzer 			dm_table_presuspend_undo_targets(map);
3125ffcc3936SMike Snitzer 			return r;
3126aa8d7c2fSAlasdair G Kergon 		}
3127d67ee213SMike Snitzer 	}
31281da177e4SLinus Torvalds 
31291da177e4SLinus Torvalds 	/*
31303b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
31313b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
31323b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
31333b00b203SMikulas Patocka 	 * dm_wq_work.
31343b00b203SMikulas Patocka 	 *
31353b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
31363b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
31376a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
31386a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
31396a8736d1STejun Heo 	 * flush_workqueue(md->wq).
31401da177e4SLinus Torvalds 	 */
31411eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
314241abc4e1SHannes Reinecke 	if (map)
314383d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
31441da177e4SLinus Torvalds 
3145d0bcb878SKiyoshi Ueda 	/*
314629e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
314729e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
3148d0bcb878SKiyoshi Ueda 	 */
31492eb6e1e3SKeith Busch 	if (dm_request_based(md)) {
3150eca7ee6dSMike Snitzer 		dm_stop_queue(md->queue);
315102233342SMike Snitzer 		if (md->kworker_task)
31522eb6e1e3SKeith Busch 			flush_kthread_worker(&md->kworker);
31532eb6e1e3SKeith Busch 	}
3154cec47e3dSKiyoshi Ueda 
3155d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
3156d0bcb878SKiyoshi Ueda 
31571da177e4SLinus Torvalds 	/*
31583b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
31593b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
31603b00b203SMikulas Patocka 	 * to finish.
31611da177e4SLinus Torvalds 	 */
3162ffcc3936SMike Snitzer 	r = dm_wait_for_completion(md, interruptible);
31631da177e4SLinus Torvalds 
31646d6f10dfSMilan Broz 	if (noflush)
3165022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
316641abc4e1SHannes Reinecke 	if (map)
316783d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
31682e93ccc1SKiyoshi Ueda 
31691da177e4SLinus Torvalds 	/* were we interrupted ? */
317046125c1cSMilan Broz 	if (r < 0) {
31719a1fb464SMikulas Patocka 		dm_queue_flush(md);
317273d410c0SMilan Broz 
3173cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
3174eca7ee6dSMike Snitzer 			dm_start_queue(md->queue);
3175cec47e3dSKiyoshi Ueda 
31762ca3310eSAlasdair G Kergon 		unlock_fs(md);
3177d67ee213SMike Snitzer 		dm_table_presuspend_undo_targets(map);
3178ffcc3936SMike Snitzer 		/* pushback list is already flushed, so skip flush */
3179ffcc3936SMike Snitzer 	}
3180ffcc3936SMike Snitzer 
3181ffcc3936SMike Snitzer 	return r;
31822ca3310eSAlasdair G Kergon }
31832ca3310eSAlasdair G Kergon 
31843b00b203SMikulas Patocka /*
3185ffcc3936SMike Snitzer  * We need to be able to change a mapping table under a mounted
3186ffcc3936SMike Snitzer  * filesystem.  For example we might want to move some data in
3187ffcc3936SMike Snitzer  * the background.  Before the table can be swapped with
3188ffcc3936SMike Snitzer  * dm_bind_table, dm_suspend must be called to flush any in
3189ffcc3936SMike Snitzer  * flight bios and ensure that any further io gets deferred.
31903b00b203SMikulas Patocka  */
3191ffcc3936SMike Snitzer /*
3192ffcc3936SMike Snitzer  * Suspend mechanism in request-based dm.
3193ffcc3936SMike Snitzer  *
3194ffcc3936SMike Snitzer  * 1. Flush all I/Os by lock_fs() if needed.
3195ffcc3936SMike Snitzer  * 2. Stop dispatching any I/O by stopping the request_queue.
3196ffcc3936SMike Snitzer  * 3. Wait for all in-flight I/Os to be completed or requeued.
3197ffcc3936SMike Snitzer  *
3198ffcc3936SMike Snitzer  * To abort suspend, start the request_queue.
3199ffcc3936SMike Snitzer  */
3200ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
3201ffcc3936SMike Snitzer {
3202ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
3203ffcc3936SMike Snitzer 	int r = 0;
3204ffcc3936SMike Snitzer 
3205ffcc3936SMike Snitzer retry:
3206ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3207ffcc3936SMike Snitzer 
3208ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
3209ffcc3936SMike Snitzer 		r = -EINVAL;
3210ffcc3936SMike Snitzer 		goto out_unlock;
3211ffcc3936SMike Snitzer 	}
3212ffcc3936SMike Snitzer 
3213ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
3214ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
3215ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
3216ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3217ffcc3936SMike Snitzer 		if (r)
3218ffcc3936SMike Snitzer 			return r;
3219ffcc3936SMike Snitzer 		goto retry;
3220ffcc3936SMike Snitzer 	}
3221ffcc3936SMike Snitzer 
3222a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
3223ffcc3936SMike Snitzer 
3224ffcc3936SMike Snitzer 	r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE);
3225ffcc3936SMike Snitzer 	if (r)
3226ffcc3936SMike Snitzer 		goto out_unlock;
32273b00b203SMikulas Patocka 
32281da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
32291da177e4SLinus Torvalds 
32304d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
32314d4471cbSKiyoshi Ueda 
3232d287483dSAlasdair G Kergon out_unlock:
3233e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
3234cf222b37SAlasdair G Kergon 	return r;
32351da177e4SLinus Torvalds }
32361da177e4SLinus Torvalds 
3237ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map)
32381da177e4SLinus Torvalds {
3239ffcc3936SMike Snitzer 	if (map) {
3240ffcc3936SMike Snitzer 		int r = dm_table_resume_targets(map);
32418757b776SMilan Broz 		if (r)
3242ffcc3936SMike Snitzer 			return r;
3243ffcc3936SMike Snitzer 	}
32442ca3310eSAlasdair G Kergon 
32459a1fb464SMikulas Patocka 	dm_queue_flush(md);
32462ca3310eSAlasdair G Kergon 
3247cec47e3dSKiyoshi Ueda 	/*
3248cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
3249cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
3250cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
3251cec47e3dSKiyoshi Ueda 	 */
3252cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
3253eca7ee6dSMike Snitzer 		dm_start_queue(md->queue);
3254cec47e3dSKiyoshi Ueda 
32552ca3310eSAlasdair G Kergon 	unlock_fs(md);
32562ca3310eSAlasdair G Kergon 
3257ffcc3936SMike Snitzer 	return 0;
3258ffcc3936SMike Snitzer }
3259ffcc3936SMike Snitzer 
3260ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md)
3261ffcc3936SMike Snitzer {
3262ffcc3936SMike Snitzer 	int r = -EINVAL;
3263ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
3264ffcc3936SMike Snitzer 
3265ffcc3936SMike Snitzer retry:
3266ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3267ffcc3936SMike Snitzer 
3268ffcc3936SMike Snitzer 	if (!dm_suspended_md(md))
3269ffcc3936SMike Snitzer 		goto out;
3270ffcc3936SMike Snitzer 
3271ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
3272ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
3273ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
3274ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3275ffcc3936SMike Snitzer 		if (r)
3276ffcc3936SMike Snitzer 			return r;
3277ffcc3936SMike Snitzer 		goto retry;
3278ffcc3936SMike Snitzer 	}
3279ffcc3936SMike Snitzer 
3280a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
3281ffcc3936SMike Snitzer 	if (!map || !dm_table_get_size(map))
3282ffcc3936SMike Snitzer 		goto out;
3283ffcc3936SMike Snitzer 
3284ffcc3936SMike Snitzer 	r = __dm_resume(md, map);
3285ffcc3936SMike Snitzer 	if (r)
3286ffcc3936SMike Snitzer 		goto out;
3287ffcc3936SMike Snitzer 
32882ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
32892ca3310eSAlasdair G Kergon 
3290cf222b37SAlasdair G Kergon 	r = 0;
3291cf222b37SAlasdair G Kergon out:
3292e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
32932ca3310eSAlasdair G Kergon 
3294cf222b37SAlasdair G Kergon 	return r;
32951da177e4SLinus Torvalds }
32961da177e4SLinus Torvalds 
3297fd2ed4d2SMikulas Patocka /*
3298fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
3299fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
3300fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
3301fd2ed4d2SMikulas Patocka  */
3302fd2ed4d2SMikulas Patocka 
3303ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
3304ffcc3936SMike Snitzer {
3305ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
3306ffcc3936SMike Snitzer 
330796b26c8cSMikulas Patocka 	if (md->internal_suspend_count++)
3308ffcc3936SMike Snitzer 		return; /* nested internal suspend */
3309ffcc3936SMike Snitzer 
3310ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
3311ffcc3936SMike Snitzer 		set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3312ffcc3936SMike Snitzer 		return; /* nest suspend */
3313ffcc3936SMike Snitzer 	}
3314ffcc3936SMike Snitzer 
3315a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
3316ffcc3936SMike Snitzer 
3317ffcc3936SMike Snitzer 	/*
3318ffcc3936SMike Snitzer 	 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
3319ffcc3936SMike Snitzer 	 * supported.  Properly supporting a TASK_INTERRUPTIBLE internal suspend
3320ffcc3936SMike Snitzer 	 * would require changing .presuspend to return an error -- avoid this
3321ffcc3936SMike Snitzer 	 * until there is a need for more elaborate variants of internal suspend.
3322ffcc3936SMike Snitzer 	 */
3323ffcc3936SMike Snitzer 	(void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE);
3324ffcc3936SMike Snitzer 
3325ffcc3936SMike Snitzer 	set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3326ffcc3936SMike Snitzer 
3327ffcc3936SMike Snitzer 	dm_table_postsuspend_targets(map);
3328ffcc3936SMike Snitzer }
3329ffcc3936SMike Snitzer 
3330ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md)
3331ffcc3936SMike Snitzer {
333296b26c8cSMikulas Patocka 	BUG_ON(!md->internal_suspend_count);
333396b26c8cSMikulas Patocka 
333496b26c8cSMikulas Patocka 	if (--md->internal_suspend_count)
3335ffcc3936SMike Snitzer 		return; /* resume from nested internal suspend */
3336ffcc3936SMike Snitzer 
3337ffcc3936SMike Snitzer 	if (dm_suspended_md(md))
3338ffcc3936SMike Snitzer 		goto done; /* resume from nested suspend */
3339ffcc3936SMike Snitzer 
3340ffcc3936SMike Snitzer 	/*
3341ffcc3936SMike Snitzer 	 * NOTE: existing callers don't need to call dm_table_resume_targets
3342ffcc3936SMike Snitzer 	 * (which may fail -- so best to avoid it for now by passing NULL map)
3343ffcc3936SMike Snitzer 	 */
3344ffcc3936SMike Snitzer 	(void) __dm_resume(md, NULL);
3345ffcc3936SMike Snitzer 
3346ffcc3936SMike Snitzer done:
3347ffcc3936SMike Snitzer 	clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3348ffcc3936SMike Snitzer 	smp_mb__after_atomic();
3349ffcc3936SMike Snitzer 	wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
3350ffcc3936SMike Snitzer }
3351ffcc3936SMike Snitzer 
3352ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md)
3353fd2ed4d2SMikulas Patocka {
3354fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
3355ffcc3936SMike Snitzer 	__dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
3356ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
3357ffcc3936SMike Snitzer }
3358ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
3359ffcc3936SMike Snitzer 
3360ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md)
3361ffcc3936SMike Snitzer {
3362ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
3363ffcc3936SMike Snitzer 	__dm_internal_resume(md);
3364ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
3365ffcc3936SMike Snitzer }
3366ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume);
3367ffcc3936SMike Snitzer 
3368ffcc3936SMike Snitzer /*
3369ffcc3936SMike Snitzer  * Fast variants of internal suspend/resume hold md->suspend_lock,
3370ffcc3936SMike Snitzer  * which prevents interaction with userspace-driven suspend.
3371ffcc3936SMike Snitzer  */
3372ffcc3936SMike Snitzer 
3373ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md)
3374ffcc3936SMike Snitzer {
3375ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
3376ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
3377fd2ed4d2SMikulas Patocka 		return;
3378fd2ed4d2SMikulas Patocka 
3379fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
3380fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
3381fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
3382fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
3383fd2ed4d2SMikulas Patocka }
3384b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
3385fd2ed4d2SMikulas Patocka 
3386ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md)
3387fd2ed4d2SMikulas Patocka {
3388ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
3389fd2ed4d2SMikulas Patocka 		goto done;
3390fd2ed4d2SMikulas Patocka 
3391fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
3392fd2ed4d2SMikulas Patocka 
3393fd2ed4d2SMikulas Patocka done:
3394fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
3395fd2ed4d2SMikulas Patocka }
3396b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
3397fd2ed4d2SMikulas Patocka 
33981da177e4SLinus Torvalds /*-----------------------------------------------------------------
33991da177e4SLinus Torvalds  * Event notification.
34001da177e4SLinus Torvalds  *---------------------------------------------------------------*/
34013abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
340260935eb2SMilan Broz 		       unsigned cookie)
340369267a30SAlasdair G Kergon {
340460935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
340560935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
340660935eb2SMilan Broz 
340760935eb2SMilan Broz 	if (!cookie)
34083abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
340960935eb2SMilan Broz 	else {
341060935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
341160935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
34123abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
34133abf85b5SPeter Rajnoha 					  action, envp);
341460935eb2SMilan Broz 	}
341569267a30SAlasdair G Kergon }
341669267a30SAlasdair G Kergon 
34177a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
34187a8c3d3bSMike Anderson {
34197a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
34207a8c3d3bSMike Anderson }
34217a8c3d3bSMike Anderson 
34221da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
34231da177e4SLinus Torvalds {
34241da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
34251da177e4SLinus Torvalds }
34261da177e4SLinus Torvalds 
34271da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
34281da177e4SLinus Torvalds {
34291da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
34301da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
34311da177e4SLinus Torvalds }
34321da177e4SLinus Torvalds 
34337a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
34347a8c3d3bSMike Anderson {
34357a8c3d3bSMike Anderson 	unsigned long flags;
34367a8c3d3bSMike Anderson 
34377a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
34387a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
34397a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
34407a8c3d3bSMike Anderson }
34417a8c3d3bSMike Anderson 
34421da177e4SLinus Torvalds /*
34431da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
34441da177e4SLinus Torvalds  * count on 'md'.
34451da177e4SLinus Torvalds  */
34461da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
34471da177e4SLinus Torvalds {
34481da177e4SLinus Torvalds 	return md->disk;
34491da177e4SLinus Torvalds }
345065ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk);
34511da177e4SLinus Torvalds 
3452784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
3453784aae73SMilan Broz {
34542995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
3455784aae73SMilan Broz }
3456784aae73SMilan Broz 
3457784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
3458784aae73SMilan Broz {
3459784aae73SMilan Broz 	struct mapped_device *md;
3460784aae73SMilan Broz 
34612995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
3462784aae73SMilan Broz 
34634d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
3464432a212cSMike Anderson 	    dm_deleting_md(md))
34654d89b7b4SMilan Broz 		return NULL;
34664d89b7b4SMilan Broz 
3467784aae73SMilan Broz 	dm_get(md);
3468784aae73SMilan Broz 	return md;
3469784aae73SMilan Broz }
3470784aae73SMilan Broz 
34714f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
34721da177e4SLinus Torvalds {
34731da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
34741da177e4SLinus Torvalds }
34751da177e4SLinus Torvalds 
3476ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md)
3477ffcc3936SMike Snitzer {
3478ffcc3936SMike Snitzer 	return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3479ffcc3936SMike Snitzer }
3480ffcc3936SMike Snitzer 
34812c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
34822c140a24SMikulas Patocka {
34832c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
34842c140a24SMikulas Patocka }
34852c140a24SMikulas Patocka 
348664dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
348764dbce58SKiyoshi Ueda {
3488ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
348964dbce58SKiyoshi Ueda }
349064dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
349164dbce58SKiyoshi Ueda 
34922e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
34932e93ccc1SKiyoshi Ueda {
3494ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
34952e93ccc1SKiyoshi Ueda }
34962e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
34972e93ccc1SKiyoshi Ueda 
349878d8e58aSMike Snitzer struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type,
349930187e1dSMike Snitzer 					    unsigned integrity, unsigned per_io_data_size)
3500e6ee8c0bSKiyoshi Ueda {
350178d8e58aSMike Snitzer 	struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
350278d8e58aSMike Snitzer 	struct kmem_cache *cachep = NULL;
350378d8e58aSMike Snitzer 	unsigned int pool_size = 0;
35045f015204SJun'ichi Nomura 	unsigned int front_pad;
3505e6ee8c0bSKiyoshi Ueda 
3506e6ee8c0bSKiyoshi Ueda 	if (!pools)
35074e6e36c3SMike Snitzer 		return NULL;
3508e6ee8c0bSKiyoshi Ueda 
350978d8e58aSMike Snitzer 	type = filter_md_type(type, md);
351017e149b8SMike Snitzer 
351178d8e58aSMike Snitzer 	switch (type) {
351278d8e58aSMike Snitzer 	case DM_TYPE_BIO_BASED:
351378d8e58aSMike Snitzer 		cachep = _io_cache;
351478d8e58aSMike Snitzer 		pool_size = dm_get_reserved_bio_based_ios();
351530187e1dSMike Snitzer 		front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
351678d8e58aSMike Snitzer 		break;
351778d8e58aSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
351878d8e58aSMike Snitzer 		cachep = _rq_tio_cache;
351978d8e58aSMike Snitzer 		pool_size = dm_get_reserved_rq_based_ios();
352078d8e58aSMike Snitzer 		pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache);
352178d8e58aSMike Snitzer 		if (!pools->rq_pool)
352278d8e58aSMike Snitzer 			goto out;
352378d8e58aSMike Snitzer 		/* fall through to setup remaining rq-based pools */
352478d8e58aSMike Snitzer 	case DM_TYPE_MQ_REQUEST_BASED:
352578d8e58aSMike Snitzer 		if (!pool_size)
352678d8e58aSMike Snitzer 			pool_size = dm_get_reserved_rq_based_ios();
352778d8e58aSMike Snitzer 		front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
3528*591ddcfcSMike Snitzer 		/* per_io_data_size is used for blk-mq pdu at queue allocation */
352978d8e58aSMike Snitzer 		break;
353078d8e58aSMike Snitzer 	default:
353178d8e58aSMike Snitzer 		BUG();
353278d8e58aSMike Snitzer 	}
353378d8e58aSMike Snitzer 
353478d8e58aSMike Snitzer 	if (cachep) {
353578d8e58aSMike Snitzer 		pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
3536e6ee8c0bSKiyoshi Ueda 		if (!pools->io_pool)
35375f015204SJun'ichi Nomura 			goto out;
353878d8e58aSMike Snitzer 	}
3539e6ee8c0bSKiyoshi Ueda 
35403d8aab2dSJunichi Nomura 	pools->bs = bioset_create_nobvec(pool_size, front_pad);
3541e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
35425f015204SJun'ichi Nomura 		goto out;
3543e6ee8c0bSKiyoshi Ueda 
3544a91a2785SMartin K. Petersen 	if (integrity && bioset_integrity_create(pools->bs, pool_size))
35455f015204SJun'ichi Nomura 		goto out;
3546a91a2785SMartin K. Petersen 
3547e6ee8c0bSKiyoshi Ueda 	return pools;
354878d8e58aSMike Snitzer 
35495f015204SJun'ichi Nomura out:
35505f015204SJun'ichi Nomura 	dm_free_md_mempools(pools);
3551e6ee8c0bSKiyoshi Ueda 
35524e6e36c3SMike Snitzer 	return NULL;
3553e6ee8c0bSKiyoshi Ueda }
3554e6ee8c0bSKiyoshi Ueda 
3555e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
3556e6ee8c0bSKiyoshi Ueda {
3557e6ee8c0bSKiyoshi Ueda 	if (!pools)
3558e6ee8c0bSKiyoshi Ueda 		return;
3559e6ee8c0bSKiyoshi Ueda 
3560e6ee8c0bSKiyoshi Ueda 	mempool_destroy(pools->io_pool);
35611ae49ea2SMike Snitzer 	mempool_destroy(pools->rq_pool);
35621ae49ea2SMike Snitzer 
3563e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
3564e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
3565e6ee8c0bSKiyoshi Ueda 
3566e6ee8c0bSKiyoshi Ueda 	kfree(pools);
3567e6ee8c0bSKiyoshi Ueda }
3568e6ee8c0bSKiyoshi Ueda 
356971cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
357071cdb697SChristoph Hellwig 			  u32 flags)
357171cdb697SChristoph Hellwig {
357271cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
357371cdb697SChristoph Hellwig 	const struct pr_ops *ops;
357471cdb697SChristoph Hellwig 	fmode_t mode;
3575956a4025SMike Snitzer 	int r;
357671cdb697SChristoph Hellwig 
3577956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
357871cdb697SChristoph Hellwig 	if (r < 0)
357971cdb697SChristoph Hellwig 		return r;
358071cdb697SChristoph Hellwig 
358171cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
358271cdb697SChristoph Hellwig 	if (ops && ops->pr_register)
358371cdb697SChristoph Hellwig 		r = ops->pr_register(bdev, old_key, new_key, flags);
358471cdb697SChristoph Hellwig 	else
358571cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
358671cdb697SChristoph Hellwig 
3587956a4025SMike Snitzer 	bdput(bdev);
358871cdb697SChristoph Hellwig 	return r;
358971cdb697SChristoph Hellwig }
359071cdb697SChristoph Hellwig 
359171cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
359271cdb697SChristoph Hellwig 			 u32 flags)
359371cdb697SChristoph Hellwig {
359471cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
359571cdb697SChristoph Hellwig 	const struct pr_ops *ops;
359671cdb697SChristoph Hellwig 	fmode_t mode;
3597956a4025SMike Snitzer 	int r;
359871cdb697SChristoph Hellwig 
3599956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
360071cdb697SChristoph Hellwig 	if (r < 0)
360171cdb697SChristoph Hellwig 		return r;
360271cdb697SChristoph Hellwig 
360371cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
360471cdb697SChristoph Hellwig 	if (ops && ops->pr_reserve)
360571cdb697SChristoph Hellwig 		r = ops->pr_reserve(bdev, key, type, flags);
360671cdb697SChristoph Hellwig 	else
360771cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
360871cdb697SChristoph Hellwig 
3609956a4025SMike Snitzer 	bdput(bdev);
361071cdb697SChristoph Hellwig 	return r;
361171cdb697SChristoph Hellwig }
361271cdb697SChristoph Hellwig 
361371cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
361471cdb697SChristoph Hellwig {
361571cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
361671cdb697SChristoph Hellwig 	const struct pr_ops *ops;
361771cdb697SChristoph Hellwig 	fmode_t mode;
3618956a4025SMike Snitzer 	int r;
361971cdb697SChristoph Hellwig 
3620956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
362171cdb697SChristoph Hellwig 	if (r < 0)
362271cdb697SChristoph Hellwig 		return r;
362371cdb697SChristoph Hellwig 
362471cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
362571cdb697SChristoph Hellwig 	if (ops && ops->pr_release)
362671cdb697SChristoph Hellwig 		r = ops->pr_release(bdev, key, type);
362771cdb697SChristoph Hellwig 	else
362871cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
362971cdb697SChristoph Hellwig 
3630956a4025SMike Snitzer 	bdput(bdev);
363171cdb697SChristoph Hellwig 	return r;
363271cdb697SChristoph Hellwig }
363371cdb697SChristoph Hellwig 
363471cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
363571cdb697SChristoph Hellwig 			 enum pr_type type, bool abort)
363671cdb697SChristoph Hellwig {
363771cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
363871cdb697SChristoph Hellwig 	const struct pr_ops *ops;
363971cdb697SChristoph Hellwig 	fmode_t mode;
3640956a4025SMike Snitzer 	int r;
364171cdb697SChristoph Hellwig 
3642956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
364371cdb697SChristoph Hellwig 	if (r < 0)
364471cdb697SChristoph Hellwig 		return r;
364571cdb697SChristoph Hellwig 
364671cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
364771cdb697SChristoph Hellwig 	if (ops && ops->pr_preempt)
364871cdb697SChristoph Hellwig 		r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
364971cdb697SChristoph Hellwig 	else
365071cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
365171cdb697SChristoph Hellwig 
3652956a4025SMike Snitzer 	bdput(bdev);
365371cdb697SChristoph Hellwig 	return r;
365471cdb697SChristoph Hellwig }
365571cdb697SChristoph Hellwig 
365671cdb697SChristoph Hellwig static int dm_pr_clear(struct block_device *bdev, u64 key)
365771cdb697SChristoph Hellwig {
365871cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
365971cdb697SChristoph Hellwig 	const struct pr_ops *ops;
366071cdb697SChristoph Hellwig 	fmode_t mode;
3661956a4025SMike Snitzer 	int r;
366271cdb697SChristoph Hellwig 
3663956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
366471cdb697SChristoph Hellwig 	if (r < 0)
366571cdb697SChristoph Hellwig 		return r;
366671cdb697SChristoph Hellwig 
366771cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
366871cdb697SChristoph Hellwig 	if (ops && ops->pr_clear)
366971cdb697SChristoph Hellwig 		r = ops->pr_clear(bdev, key);
367071cdb697SChristoph Hellwig 	else
367171cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
367271cdb697SChristoph Hellwig 
3673956a4025SMike Snitzer 	bdput(bdev);
367471cdb697SChristoph Hellwig 	return r;
367571cdb697SChristoph Hellwig }
367671cdb697SChristoph Hellwig 
367771cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = {
367871cdb697SChristoph Hellwig 	.pr_register	= dm_pr_register,
367971cdb697SChristoph Hellwig 	.pr_reserve	= dm_pr_reserve,
368071cdb697SChristoph Hellwig 	.pr_release	= dm_pr_release,
368171cdb697SChristoph Hellwig 	.pr_preempt	= dm_pr_preempt,
368271cdb697SChristoph Hellwig 	.pr_clear	= dm_pr_clear,
368371cdb697SChristoph Hellwig };
368471cdb697SChristoph Hellwig 
368583d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
36861da177e4SLinus Torvalds 	.open = dm_blk_open,
36871da177e4SLinus Torvalds 	.release = dm_blk_close,
3688aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
36893ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
369071cdb697SChristoph Hellwig 	.pr_ops = &dm_pr_ops,
36911da177e4SLinus Torvalds 	.owner = THIS_MODULE
36921da177e4SLinus Torvalds };
36931da177e4SLinus Torvalds 
36941da177e4SLinus Torvalds /*
36951da177e4SLinus Torvalds  * module hooks
36961da177e4SLinus Torvalds  */
36971da177e4SLinus Torvalds module_init(dm_init);
36981da177e4SLinus Torvalds module_exit(dm_exit);
36991da177e4SLinus Torvalds 
37001da177e4SLinus Torvalds module_param(major, uint, 0);
37011da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
3702f4790826SMike Snitzer 
3703e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3704e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3705e8603136SMike Snitzer 
3706f4790826SMike Snitzer module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
3707f4790826SMike Snitzer MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
3708f4790826SMike Snitzer 
370917e149b8SMike Snitzer module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
371017e149b8SMike Snitzer MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
371117e149b8SMike Snitzer 
3712faad87dfSMike Snitzer module_param(dm_mq_nr_hw_queues, uint, S_IRUGO | S_IWUSR);
3713faad87dfSMike Snitzer MODULE_PARM_DESC(dm_mq_nr_hw_queues, "Number of hardware queues for request-based dm-mq devices");
3714faad87dfSMike Snitzer 
3715faad87dfSMike Snitzer module_param(dm_mq_queue_depth, uint, S_IRUGO | S_IWUSR);
3716faad87dfSMike Snitzer MODULE_PARM_DESC(dm_mq_queue_depth, "Queue depth for request-based dm-mq devices");
3717faad87dfSMike Snitzer 
37181da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
37191da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
37201da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3721