xref: /openbmc/linux/drivers/md/dm.c (revision 09c2d53101da87f5ab4084643d2f8c718b3ab3cf)
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>
2455782138SLi Zefan 
2555782138SLi Zefan #include <trace/events/block.h>
261da177e4SLinus Torvalds 
2772d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
2872d94861SAlasdair G Kergon 
2971a16736SNamhyung Kim #ifdef CONFIG_PRINTK
3071a16736SNamhyung Kim /*
3171a16736SNamhyung Kim  * ratelimit state to be used in DMXXX_LIMIT().
3271a16736SNamhyung Kim  */
3371a16736SNamhyung Kim DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
3471a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_INTERVAL,
3571a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_BURST);
3671a16736SNamhyung Kim EXPORT_SYMBOL(dm_ratelimit_state);
3771a16736SNamhyung Kim #endif
3871a16736SNamhyung Kim 
3960935eb2SMilan Broz /*
4060935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
4160935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
4260935eb2SMilan Broz  */
4360935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
4460935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
4560935eb2SMilan Broz 
461da177e4SLinus Torvalds static const char *_name = DM_NAME;
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds static unsigned int major = 0;
491da177e4SLinus Torvalds static unsigned int _major = 0;
501da177e4SLinus Torvalds 
51d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr);
52d15b774cSAlasdair G Kergon 
53f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
542c140a24SMikulas Patocka 
552c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w);
562c140a24SMikulas Patocka 
572c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
582c140a24SMikulas Patocka 
59acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue;
60acfe0ad7SMikulas Patocka 
611da177e4SLinus Torvalds /*
628fbf26adSKiyoshi Ueda  * For bio-based dm.
631da177e4SLinus Torvalds  * One of these is allocated per bio.
641da177e4SLinus Torvalds  */
651da177e4SLinus Torvalds struct dm_io {
661da177e4SLinus Torvalds 	struct mapped_device *md;
671da177e4SLinus Torvalds 	int error;
681da177e4SLinus Torvalds 	atomic_t io_count;
696ae2fa67SRichard Kennedy 	struct bio *bio;
703eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
71f88fb981SKiyoshi Ueda 	spinlock_t endio_lock;
72fd2ed4d2SMikulas Patocka 	struct dm_stats_aux stats_aux;
731da177e4SLinus Torvalds };
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds /*
768fbf26adSKiyoshi Ueda  * For request-based dm.
778fbf26adSKiyoshi Ueda  * One of these is allocated per request.
788fbf26adSKiyoshi Ueda  */
798fbf26adSKiyoshi Ueda struct dm_rq_target_io {
808fbf26adSKiyoshi Ueda 	struct mapped_device *md;
818fbf26adSKiyoshi Ueda 	struct dm_target *ti;
821ae49ea2SMike Snitzer 	struct request *orig, *clone;
832eb6e1e3SKeith Busch 	struct kthread_work work;
848fbf26adSKiyoshi Ueda 	int error;
858fbf26adSKiyoshi Ueda 	union map_info info;
868fbf26adSKiyoshi Ueda };
878fbf26adSKiyoshi Ueda 
888fbf26adSKiyoshi Ueda /*
8994818742SKent Overstreet  * For request-based dm - the bio clones we allocate are embedded in these
9094818742SKent Overstreet  * structs.
9194818742SKent Overstreet  *
9294818742SKent Overstreet  * We allocate these with bio_alloc_bioset, using the front_pad parameter when
9394818742SKent Overstreet  * the bioset is created - this means the bio has to come at the end of the
9494818742SKent Overstreet  * struct.
958fbf26adSKiyoshi Ueda  */
968fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info {
978fbf26adSKiyoshi Ueda 	struct bio *orig;
98cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio;
9994818742SKent Overstreet 	struct bio clone;
1008fbf26adSKiyoshi Ueda };
1018fbf26adSKiyoshi Ueda 
102cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq)
103cec47e3dSKiyoshi Ueda {
104cec47e3dSKiyoshi Ueda 	if (rq && rq->end_io_data)
105cec47e3dSKiyoshi Ueda 		return &((struct dm_rq_target_io *)rq->end_io_data)->info;
106cec47e3dSKiyoshi Ueda 	return NULL;
107cec47e3dSKiyoshi Ueda }
108cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
109cec47e3dSKiyoshi Ueda 
110ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
111ba61fdd1SJeff Mahoney 
1121da177e4SLinus Torvalds /*
1131da177e4SLinus Torvalds  * Bits for the md->flags field.
1141da177e4SLinus Torvalds  */
1151eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
1161da177e4SLinus Torvalds #define DMF_SUSPENDED 1
117aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
118fba9f90eSJeff Mahoney #define DMF_FREEING 3
1195c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
1202e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
121d5b9dd04SMikulas Patocka #define DMF_MERGE_IS_OPTIONAL 6
1222c140a24SMikulas Patocka #define DMF_DEFERRED_REMOVE 7
123ffcc3936SMike Snitzer #define DMF_SUSPENDED_INTERNALLY 8
1241da177e4SLinus Torvalds 
125304f3f6aSMilan Broz /*
12683d5e5b0SMikulas Patocka  * A dummy definition to make RCU happy.
12783d5e5b0SMikulas Patocka  * struct dm_table should never be dereferenced in this file.
12883d5e5b0SMikulas Patocka  */
12983d5e5b0SMikulas Patocka struct dm_table {
13083d5e5b0SMikulas Patocka 	int undefined__;
13183d5e5b0SMikulas Patocka };
13283d5e5b0SMikulas Patocka 
13383d5e5b0SMikulas Patocka /*
134304f3f6aSMilan Broz  * Work processed by per-device workqueue.
135304f3f6aSMilan Broz  */
1361da177e4SLinus Torvalds struct mapped_device {
13783d5e5b0SMikulas Patocka 	struct srcu_struct io_barrier;
138e61290a4SDaniel Walker 	struct mutex suspend_lock;
1391da177e4SLinus Torvalds 	atomic_t holders;
1405c6bd75dSAlasdair G Kergon 	atomic_t open_count;
1411da177e4SLinus Torvalds 
1422a7faeb1SMikulas Patocka 	/*
1432a7faeb1SMikulas Patocka 	 * The current mapping.
1442a7faeb1SMikulas Patocka 	 * Use dm_get_live_table{_fast} or take suspend_lock for
1452a7faeb1SMikulas Patocka 	 * dereference.
1462a7faeb1SMikulas Patocka 	 */
1476fa99520SPranith Kumar 	struct dm_table __rcu *map;
1482a7faeb1SMikulas Patocka 
14986f1152bSBenjamin Marzinski 	struct list_head table_devices;
15086f1152bSBenjamin Marzinski 	struct mutex table_devices_lock;
15186f1152bSBenjamin Marzinski 
1521da177e4SLinus Torvalds 	unsigned long flags;
1531da177e4SLinus Torvalds 
154165125e1SJens Axboe 	struct request_queue *queue;
155a5664dadSMike Snitzer 	unsigned type;
1564a0b4ddfSMike Snitzer 	/* Protect queue and type against concurrent access. */
157a5664dadSMike Snitzer 	struct mutex type_lock;
158a5664dadSMike Snitzer 
15936a0456fSAlasdair G Kergon 	struct target_type *immutable_target_type;
16036a0456fSAlasdair G Kergon 
1611da177e4SLinus Torvalds 	struct gendisk *disk;
1627e51f257SMike Anderson 	char name[16];
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	void *interface_ptr;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	/*
1671da177e4SLinus Torvalds 	 * A list of ios that arrived while we were suspended.
1681da177e4SLinus Torvalds 	 */
169316d315bSNikanth Karthikesan 	atomic_t pending[2];
1701da177e4SLinus Torvalds 	wait_queue_head_t wait;
17153d5914fSMikulas Patocka 	struct work_struct work;
1721da177e4SLinus Torvalds 	struct bio_list deferred;
173022c2611SMikulas Patocka 	spinlock_t deferred_lock;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	/*
17629e4013dSTejun Heo 	 * Processing queue (flush)
177304f3f6aSMilan Broz 	 */
178304f3f6aSMilan Broz 	struct workqueue_struct *wq;
179304f3f6aSMilan Broz 
180304f3f6aSMilan Broz 	/*
1811da177e4SLinus Torvalds 	 * io objects are allocated from here.
1821da177e4SLinus Torvalds 	 */
1831da177e4SLinus Torvalds 	mempool_t *io_pool;
1841ae49ea2SMike Snitzer 	mempool_t *rq_pool;
1851da177e4SLinus Torvalds 
1869faf400fSStefan Bader 	struct bio_set *bs;
1879faf400fSStefan Bader 
1881da177e4SLinus Torvalds 	/*
1891da177e4SLinus Torvalds 	 * Event handling.
1901da177e4SLinus Torvalds 	 */
1911da177e4SLinus Torvalds 	atomic_t event_nr;
1921da177e4SLinus Torvalds 	wait_queue_head_t eventq;
1937a8c3d3bSMike Anderson 	atomic_t uevent_seq;
1947a8c3d3bSMike Anderson 	struct list_head uevent_list;
1957a8c3d3bSMike Anderson 	spinlock_t uevent_lock; /* Protect access to uevent_list */
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	/*
1981da177e4SLinus Torvalds 	 * freeze/thaw support require holding onto a super block
1991da177e4SLinus Torvalds 	 */
2001da177e4SLinus Torvalds 	struct super_block *frozen_sb;
201db8fef4fSMikulas Patocka 	struct block_device *bdev;
2023ac51e74SDarrick J. Wong 
2033ac51e74SDarrick J. Wong 	/* forced geometry settings */
2043ac51e74SDarrick J. Wong 	struct hd_geometry geometry;
205784aae73SMilan Broz 
2062995fa78SMikulas Patocka 	/* kobject and completion */
2072995fa78SMikulas Patocka 	struct dm_kobject_holder kobj_holder;
208be35f486SMikulas Patocka 
209d87f4c14STejun Heo 	/* zero-length flush that will be cloned and submitted to targets */
210d87f4c14STejun Heo 	struct bio flush_bio;
211fd2ed4d2SMikulas Patocka 
21296b26c8cSMikulas Patocka 	/* the number of internal suspends */
21396b26c8cSMikulas Patocka 	unsigned internal_suspend_count;
21496b26c8cSMikulas Patocka 
215fd2ed4d2SMikulas Patocka 	struct dm_stats stats;
2162eb6e1e3SKeith Busch 
2172eb6e1e3SKeith Busch 	struct kthread_worker kworker;
2182eb6e1e3SKeith Busch 	struct task_struct *kworker_task;
2191da177e4SLinus Torvalds };
2201da177e4SLinus Torvalds 
221e6ee8c0bSKiyoshi Ueda /*
222e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
223e6ee8c0bSKiyoshi Ueda  */
224e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
225e6ee8c0bSKiyoshi Ueda 	mempool_t *io_pool;
2261ae49ea2SMike Snitzer 	mempool_t *rq_pool;
227e6ee8c0bSKiyoshi Ueda 	struct bio_set *bs;
228e6ee8c0bSKiyoshi Ueda };
229e6ee8c0bSKiyoshi Ueda 
23086f1152bSBenjamin Marzinski struct table_device {
23186f1152bSBenjamin Marzinski 	struct list_head list;
23286f1152bSBenjamin Marzinski 	atomic_t count;
23386f1152bSBenjamin Marzinski 	struct dm_dev dm_dev;
23486f1152bSBenjamin Marzinski };
23586f1152bSBenjamin Marzinski 
2366cfa5857SMike Snitzer #define RESERVED_BIO_BASED_IOS		16
2376cfa5857SMike Snitzer #define RESERVED_REQUEST_BASED_IOS	256
238f4790826SMike Snitzer #define RESERVED_MAX_IOS		1024
239e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
2408fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
2411ae49ea2SMike Snitzer static struct kmem_cache *_rq_cache;
24294818742SKent Overstreet 
243f4790826SMike Snitzer /*
244e8603136SMike Snitzer  * Bio-based DM's mempools' reserved IOs set by the user.
245e8603136SMike Snitzer  */
246e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
247e8603136SMike Snitzer 
248e8603136SMike Snitzer /*
249f4790826SMike Snitzer  * Request-based DM's mempools' reserved IOs set by the user.
250f4790826SMike Snitzer  */
251f4790826SMike Snitzer static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
252f4790826SMike Snitzer 
253*09c2d531SMike Snitzer static unsigned __dm_get_module_param(unsigned *module_param,
254f4790826SMike Snitzer 				      unsigned def, unsigned max)
255f4790826SMike Snitzer {
256*09c2d531SMike Snitzer 	unsigned param = ACCESS_ONCE(*module_param);
257*09c2d531SMike Snitzer 	unsigned modified_param = 0;
258f4790826SMike Snitzer 
259*09c2d531SMike Snitzer 	if (!param)
260*09c2d531SMike Snitzer 		modified_param = def;
261*09c2d531SMike Snitzer 	else if (param > max)
262*09c2d531SMike Snitzer 		modified_param = max;
263f4790826SMike Snitzer 
264*09c2d531SMike Snitzer 	if (modified_param) {
265*09c2d531SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
266*09c2d531SMike Snitzer 		param = modified_param;
267f4790826SMike Snitzer 	}
268f4790826SMike Snitzer 
269*09c2d531SMike Snitzer 	return param;
270f4790826SMike Snitzer }
271f4790826SMike Snitzer 
272e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void)
273e8603136SMike Snitzer {
274*09c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_bio_based_ios,
275e8603136SMike Snitzer 				     RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
276e8603136SMike Snitzer }
277e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
278e8603136SMike Snitzer 
279f4790826SMike Snitzer unsigned dm_get_reserved_rq_based_ios(void)
280f4790826SMike Snitzer {
281*09c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_rq_based_ios,
282f4790826SMike Snitzer 				     RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
283f4790826SMike Snitzer }
284f4790826SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
285f4790826SMike Snitzer 
2861da177e4SLinus Torvalds static int __init local_init(void)
2871da177e4SLinus Torvalds {
28851157b4aSKiyoshi Ueda 	int r = -ENOMEM;
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
291028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
2921da177e4SLinus Torvalds 	if (!_io_cache)
29351157b4aSKiyoshi Ueda 		return r;
2941da177e4SLinus Torvalds 
2958fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
2968fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
297dba14160SMikulas Patocka 		goto out_free_io_cache;
2988fbf26adSKiyoshi Ueda 
2991ae49ea2SMike Snitzer 	_rq_cache = kmem_cache_create("dm_clone_request", sizeof(struct request),
3001ae49ea2SMike Snitzer 				      __alignof__(struct request), 0, NULL);
3011ae49ea2SMike Snitzer 	if (!_rq_cache)
3021ae49ea2SMike Snitzer 		goto out_free_rq_tio_cache;
3031ae49ea2SMike Snitzer 
30451e5b2bdSMike Anderson 	r = dm_uevent_init();
30551157b4aSKiyoshi Ueda 	if (r)
3061ae49ea2SMike Snitzer 		goto out_free_rq_cache;
30751e5b2bdSMike Anderson 
308acfe0ad7SMikulas Patocka 	deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
309acfe0ad7SMikulas Patocka 	if (!deferred_remove_workqueue) {
310acfe0ad7SMikulas Patocka 		r = -ENOMEM;
311acfe0ad7SMikulas Patocka 		goto out_uevent_exit;
312acfe0ad7SMikulas Patocka 	}
313acfe0ad7SMikulas Patocka 
3141da177e4SLinus Torvalds 	_major = major;
3151da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
31651157b4aSKiyoshi Ueda 	if (r < 0)
317acfe0ad7SMikulas Patocka 		goto out_free_workqueue;
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	if (!_major)
3201da177e4SLinus Torvalds 		_major = r;
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	return 0;
32351157b4aSKiyoshi Ueda 
324acfe0ad7SMikulas Patocka out_free_workqueue:
325acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
32651157b4aSKiyoshi Ueda out_uevent_exit:
32751157b4aSKiyoshi Ueda 	dm_uevent_exit();
3281ae49ea2SMike Snitzer out_free_rq_cache:
3291ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
3308fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
3318fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
33251157b4aSKiyoshi Ueda out_free_io_cache:
33351157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
33451157b4aSKiyoshi Ueda 
33551157b4aSKiyoshi Ueda 	return r;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds static void local_exit(void)
3391da177e4SLinus Torvalds {
3402c140a24SMikulas Patocka 	flush_scheduled_work();
341acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
3422c140a24SMikulas Patocka 
3431ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
3448fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
3451da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
34600d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
34751e5b2bdSMike Anderson 	dm_uevent_exit();
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	_major = 0;
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	DMINFO("cleaned up");
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
354b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
3551da177e4SLinus Torvalds 	local_init,
3561da177e4SLinus Torvalds 	dm_target_init,
3571da177e4SLinus Torvalds 	dm_linear_init,
3581da177e4SLinus Torvalds 	dm_stripe_init,
359952b3557SMikulas Patocka 	dm_io_init,
360945fa4d2SMikulas Patocka 	dm_kcopyd_init,
3611da177e4SLinus Torvalds 	dm_interface_init,
362fd2ed4d2SMikulas Patocka 	dm_statistics_init,
3631da177e4SLinus Torvalds };
3641da177e4SLinus Torvalds 
365b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
3661da177e4SLinus Torvalds 	local_exit,
3671da177e4SLinus Torvalds 	dm_target_exit,
3681da177e4SLinus Torvalds 	dm_linear_exit,
3691da177e4SLinus Torvalds 	dm_stripe_exit,
370952b3557SMikulas Patocka 	dm_io_exit,
371945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
3721da177e4SLinus Torvalds 	dm_interface_exit,
373fd2ed4d2SMikulas Patocka 	dm_statistics_exit,
3741da177e4SLinus Torvalds };
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds static int __init dm_init(void)
3771da177e4SLinus Torvalds {
3781da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	int r, i;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
3831da177e4SLinus Torvalds 		r = _inits[i]();
3841da177e4SLinus Torvalds 		if (r)
3851da177e4SLinus Torvalds 			goto bad;
3861da177e4SLinus Torvalds 	}
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	return 0;
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds       bad:
3911da177e4SLinus Torvalds 	while (i--)
3921da177e4SLinus Torvalds 		_exits[i]();
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	return r;
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds static void __exit dm_exit(void)
3981da177e4SLinus Torvalds {
3991da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	while (i--)
4021da177e4SLinus Torvalds 		_exits[i]();
403d15b774cSAlasdair G Kergon 
404d15b774cSAlasdair G Kergon 	/*
405d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
406d15b774cSAlasdair G Kergon 	 */
407d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds /*
4111da177e4SLinus Torvalds  * Block device functions
4121da177e4SLinus Torvalds  */
413432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
414432a212cSMike Anderson {
415432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
416432a212cSMike Anderson }
417432a212cSMike Anderson 
418fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
4191da177e4SLinus Torvalds {
4201da177e4SLinus Torvalds 	struct mapped_device *md;
4211da177e4SLinus Torvalds 
422fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
423fba9f90eSJeff Mahoney 
424fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
425fba9f90eSJeff Mahoney 	if (!md)
426fba9f90eSJeff Mahoney 		goto out;
427fba9f90eSJeff Mahoney 
4285c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
429432a212cSMike Anderson 	    dm_deleting_md(md)) {
430fba9f90eSJeff Mahoney 		md = NULL;
431fba9f90eSJeff Mahoney 		goto out;
432fba9f90eSJeff Mahoney 	}
433fba9f90eSJeff Mahoney 
4341da177e4SLinus Torvalds 	dm_get(md);
4355c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
436fba9f90eSJeff Mahoney out:
437fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
438fba9f90eSJeff Mahoney 
439fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
4401da177e4SLinus Torvalds }
4411da177e4SLinus Torvalds 
442db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode)
4431da177e4SLinus Torvalds {
44463a4f065SMike Snitzer 	struct mapped_device *md;
4456e9624b8SArnd Bergmann 
4464a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
4474a1aeb98SMilan Broz 
44863a4f065SMike Snitzer 	md = disk->private_data;
44963a4f065SMike Snitzer 	if (WARN_ON(!md))
45063a4f065SMike Snitzer 		goto out;
45163a4f065SMike Snitzer 
4522c140a24SMikulas Patocka 	if (atomic_dec_and_test(&md->open_count) &&
4532c140a24SMikulas Patocka 	    (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
454acfe0ad7SMikulas Patocka 		queue_work(deferred_remove_workqueue, &deferred_remove_work);
4552c140a24SMikulas Patocka 
4561da177e4SLinus Torvalds 	dm_put(md);
45763a4f065SMike Snitzer out:
4584a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
4591da177e4SLinus Torvalds }
4601da177e4SLinus Torvalds 
4615c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
4625c6bd75dSAlasdair G Kergon {
4635c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
4645c6bd75dSAlasdair G Kergon }
4655c6bd75dSAlasdair G Kergon 
4665c6bd75dSAlasdair G Kergon /*
4675c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
4685c6bd75dSAlasdair G Kergon  */
4692c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
4705c6bd75dSAlasdair G Kergon {
4715c6bd75dSAlasdair G Kergon 	int r = 0;
4725c6bd75dSAlasdair G Kergon 
4735c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
4745c6bd75dSAlasdair G Kergon 
4752c140a24SMikulas Patocka 	if (dm_open_count(md)) {
4765c6bd75dSAlasdair G Kergon 		r = -EBUSY;
4772c140a24SMikulas Patocka 		if (mark_deferred)
4782c140a24SMikulas Patocka 			set_bit(DMF_DEFERRED_REMOVE, &md->flags);
4792c140a24SMikulas Patocka 	} else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
4802c140a24SMikulas Patocka 		r = -EEXIST;
4815c6bd75dSAlasdair G Kergon 	else
4825c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
4835c6bd75dSAlasdair G Kergon 
4845c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
4855c6bd75dSAlasdair G Kergon 
4865c6bd75dSAlasdair G Kergon 	return r;
4875c6bd75dSAlasdair G Kergon }
4885c6bd75dSAlasdair G Kergon 
4892c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md)
4902c140a24SMikulas Patocka {
4912c140a24SMikulas Patocka 	int r = 0;
4922c140a24SMikulas Patocka 
4932c140a24SMikulas Patocka 	spin_lock(&_minor_lock);
4942c140a24SMikulas Patocka 
4952c140a24SMikulas Patocka 	if (test_bit(DMF_DELETING, &md->flags))
4962c140a24SMikulas Patocka 		r = -EBUSY;
4972c140a24SMikulas Patocka 	else
4982c140a24SMikulas Patocka 		clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
4992c140a24SMikulas Patocka 
5002c140a24SMikulas Patocka 	spin_unlock(&_minor_lock);
5012c140a24SMikulas Patocka 
5022c140a24SMikulas Patocka 	return r;
5032c140a24SMikulas Patocka }
5042c140a24SMikulas Patocka 
5052c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w)
5062c140a24SMikulas Patocka {
5072c140a24SMikulas Patocka 	dm_deferred_remove();
5082c140a24SMikulas Patocka }
5092c140a24SMikulas Patocka 
510fd2ed4d2SMikulas Patocka sector_t dm_get_size(struct mapped_device *md)
511fd2ed4d2SMikulas Patocka {
512fd2ed4d2SMikulas Patocka 	return get_capacity(md->disk);
513fd2ed4d2SMikulas Patocka }
514fd2ed4d2SMikulas Patocka 
5159974fa2cSMike Snitzer struct request_queue *dm_get_md_queue(struct mapped_device *md)
5169974fa2cSMike Snitzer {
5179974fa2cSMike Snitzer 	return md->queue;
5189974fa2cSMike Snitzer }
5199974fa2cSMike Snitzer 
520fd2ed4d2SMikulas Patocka struct dm_stats *dm_get_stats(struct mapped_device *md)
521fd2ed4d2SMikulas Patocka {
522fd2ed4d2SMikulas Patocka 	return &md->stats;
523fd2ed4d2SMikulas Patocka }
524fd2ed4d2SMikulas Patocka 
5253ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5263ac51e74SDarrick J. Wong {
5273ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
5283ac51e74SDarrick J. Wong 
5293ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
5303ac51e74SDarrick J. Wong }
5313ac51e74SDarrick J. Wong 
532fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
533aa129a22SMilan Broz 			unsigned int cmd, unsigned long arg)
534aa129a22SMilan Broz {
535fe5f9f2cSAl Viro 	struct mapped_device *md = bdev->bd_disk->private_data;
53683d5e5b0SMikulas Patocka 	int srcu_idx;
5376c182cd8SHannes Reinecke 	struct dm_table *map;
538aa129a22SMilan Broz 	struct dm_target *tgt;
539aa129a22SMilan Broz 	int r = -ENOTTY;
540aa129a22SMilan Broz 
5416c182cd8SHannes Reinecke retry:
54283d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
54383d5e5b0SMikulas Patocka 
544aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
545aa129a22SMilan Broz 		goto out;
546aa129a22SMilan Broz 
547aa129a22SMilan Broz 	/* We only support devices that have a single target */
548aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
549aa129a22SMilan Broz 		goto out;
550aa129a22SMilan Broz 
551aa129a22SMilan Broz 	tgt = dm_table_get_target(map, 0);
5524d341d82SMike Snitzer 	if (!tgt->type->ioctl)
5534d341d82SMike Snitzer 		goto out;
554aa129a22SMilan Broz 
5554f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
556aa129a22SMilan Broz 		r = -EAGAIN;
557aa129a22SMilan Broz 		goto out;
558aa129a22SMilan Broz 	}
559aa129a22SMilan Broz 
560647b3d00SAl Viro 	r = tgt->type->ioctl(tgt, cmd, arg);
561aa129a22SMilan Broz 
562aa129a22SMilan Broz out:
56383d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
564aa129a22SMilan Broz 
5656c182cd8SHannes Reinecke 	if (r == -ENOTCONN) {
5666c182cd8SHannes Reinecke 		msleep(10);
5676c182cd8SHannes Reinecke 		goto retry;
5686c182cd8SHannes Reinecke 	}
5696c182cd8SHannes Reinecke 
570aa129a22SMilan Broz 	return r;
571aa129a22SMilan Broz }
572aa129a22SMilan Broz 
573028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
5741da177e4SLinus Torvalds {
5751da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
5761da177e4SLinus Torvalds }
5771da177e4SLinus Torvalds 
578028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
5791da177e4SLinus Torvalds {
5801da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
5811da177e4SLinus Torvalds }
5821da177e4SLinus Torvalds 
583028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
5841da177e4SLinus Torvalds {
585dba14160SMikulas Patocka 	bio_put(&tio->clone);
5861da177e4SLinus Torvalds }
5871da177e4SLinus Torvalds 
58808885643SKiyoshi Ueda static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
58908885643SKiyoshi Ueda 					    gfp_t gfp_mask)
590cec47e3dSKiyoshi Ueda {
5915f015204SJun'ichi Nomura 	return mempool_alloc(md->io_pool, gfp_mask);
592cec47e3dSKiyoshi Ueda }
593cec47e3dSKiyoshi Ueda 
594cec47e3dSKiyoshi Ueda static void free_rq_tio(struct dm_rq_target_io *tio)
595cec47e3dSKiyoshi Ueda {
5965f015204SJun'ichi Nomura 	mempool_free(tio, tio->md->io_pool);
597cec47e3dSKiyoshi Ueda }
598cec47e3dSKiyoshi Ueda 
5991ae49ea2SMike Snitzer static struct request *alloc_clone_request(struct mapped_device *md,
6001ae49ea2SMike Snitzer 					   gfp_t gfp_mask)
6011ae49ea2SMike Snitzer {
6021ae49ea2SMike Snitzer 	return mempool_alloc(md->rq_pool, gfp_mask);
6031ae49ea2SMike Snitzer }
6041ae49ea2SMike Snitzer 
6051ae49ea2SMike Snitzer static void free_clone_request(struct mapped_device *md, struct request *rq)
6061ae49ea2SMike Snitzer {
6071ae49ea2SMike Snitzer 	mempool_free(rq, md->rq_pool);
6081ae49ea2SMike Snitzer }
6091ae49ea2SMike Snitzer 
61090abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md)
61190abb8c4SKiyoshi Ueda {
61290abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
61390abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
61490abb8c4SKiyoshi Ueda }
61590abb8c4SKiyoshi Ueda 
6163eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
6173eaf840eSJun'ichi "Nick" Nomura {
6183eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
619fd2ed4d2SMikulas Patocka 	struct bio *bio = io->bio;
620c9959059STejun Heo 	int cpu;
621fd2ed4d2SMikulas Patocka 	int rw = bio_data_dir(bio);
6223eaf840eSJun'ichi "Nick" Nomura 
6233eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
6243eaf840eSJun'ichi "Nick" Nomura 
625074a7acaSTejun Heo 	cpu = part_stat_lock();
626074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
627074a7acaSTejun Heo 	part_stat_unlock();
6281e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw],
6291e9bb880SShaohua Li 		atomic_inc_return(&md->pending[rw]));
630fd2ed4d2SMikulas Patocka 
631fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
6324f024f37SKent Overstreet 		dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
633fd2ed4d2SMikulas Patocka 				    bio_sectors(bio), false, 0, &io->stats_aux);
6343eaf840eSJun'ichi "Nick" Nomura }
6353eaf840eSJun'ichi "Nick" Nomura 
636d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
6373eaf840eSJun'ichi "Nick" Nomura {
6383eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
6393eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
6403eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
64118c0b223SGu Zheng 	int pending;
6423eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
6433eaf840eSJun'ichi "Nick" Nomura 
64418c0b223SGu Zheng 	generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time);
6453eaf840eSJun'ichi "Nick" Nomura 
646fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
6474f024f37SKent Overstreet 		dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
648fd2ed4d2SMikulas Patocka 				    bio_sectors(bio), true, duration, &io->stats_aux);
649fd2ed4d2SMikulas Patocka 
650af7e466aSMikulas Patocka 	/*
651af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
652d87f4c14STejun Heo 	 * a flush.
653af7e466aSMikulas Patocka 	 */
6541e9bb880SShaohua Li 	pending = atomic_dec_return(&md->pending[rw]);
6551e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
656316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
6573eaf840eSJun'ichi "Nick" Nomura 
658d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
659d221d2e7SMikulas Patocka 	if (!pending)
660d221d2e7SMikulas Patocka 		wake_up(&md->wait);
6613eaf840eSJun'ichi "Nick" Nomura }
6623eaf840eSJun'ichi "Nick" Nomura 
6631da177e4SLinus Torvalds /*
6641da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
6651da177e4SLinus Torvalds  */
66692c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
6671da177e4SLinus Torvalds {
66805447420SKiyoshi Ueda 	unsigned long flags;
6691da177e4SLinus Torvalds 
67005447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
6711da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
67205447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
67392c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
6741da177e4SLinus Torvalds }
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds /*
6771da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
6781da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
67983d5e5b0SMikulas Patocka  * dm_put_live_table() when finished.
6801da177e4SLinus Torvalds  */
68183d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
6821da177e4SLinus Torvalds {
68383d5e5b0SMikulas Patocka 	*srcu_idx = srcu_read_lock(&md->io_barrier);
6841da177e4SLinus Torvalds 
68583d5e5b0SMikulas Patocka 	return srcu_dereference(md->map, &md->io_barrier);
68683d5e5b0SMikulas Patocka }
6871da177e4SLinus Torvalds 
68883d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
68983d5e5b0SMikulas Patocka {
69083d5e5b0SMikulas Patocka 	srcu_read_unlock(&md->io_barrier, srcu_idx);
69183d5e5b0SMikulas Patocka }
69283d5e5b0SMikulas Patocka 
69383d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md)
69483d5e5b0SMikulas Patocka {
69583d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
69683d5e5b0SMikulas Patocka 	synchronize_rcu_expedited();
69783d5e5b0SMikulas Patocka }
69883d5e5b0SMikulas Patocka 
69983d5e5b0SMikulas Patocka /*
70083d5e5b0SMikulas Patocka  * A fast alternative to dm_get_live_table/dm_put_live_table.
70183d5e5b0SMikulas Patocka  * The caller must not block between these two functions.
70283d5e5b0SMikulas Patocka  */
70383d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
70483d5e5b0SMikulas Patocka {
70583d5e5b0SMikulas Patocka 	rcu_read_lock();
70683d5e5b0SMikulas Patocka 	return rcu_dereference(md->map);
70783d5e5b0SMikulas Patocka }
70883d5e5b0SMikulas Patocka 
70983d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
71083d5e5b0SMikulas Patocka {
71183d5e5b0SMikulas Patocka 	rcu_read_unlock();
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
7143ac51e74SDarrick J. Wong /*
71586f1152bSBenjamin Marzinski  * Open a table device so we can use it as a map destination.
71686f1152bSBenjamin Marzinski  */
71786f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev,
71886f1152bSBenjamin Marzinski 			     struct mapped_device *md)
71986f1152bSBenjamin Marzinski {
72086f1152bSBenjamin Marzinski 	static char *_claim_ptr = "I belong to device-mapper";
72186f1152bSBenjamin Marzinski 	struct block_device *bdev;
72286f1152bSBenjamin Marzinski 
72386f1152bSBenjamin Marzinski 	int r;
72486f1152bSBenjamin Marzinski 
72586f1152bSBenjamin Marzinski 	BUG_ON(td->dm_dev.bdev);
72686f1152bSBenjamin Marzinski 
72786f1152bSBenjamin Marzinski 	bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
72886f1152bSBenjamin Marzinski 	if (IS_ERR(bdev))
72986f1152bSBenjamin Marzinski 		return PTR_ERR(bdev);
73086f1152bSBenjamin Marzinski 
73186f1152bSBenjamin Marzinski 	r = bd_link_disk_holder(bdev, dm_disk(md));
73286f1152bSBenjamin Marzinski 	if (r) {
73386f1152bSBenjamin Marzinski 		blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
73486f1152bSBenjamin Marzinski 		return r;
73586f1152bSBenjamin Marzinski 	}
73686f1152bSBenjamin Marzinski 
73786f1152bSBenjamin Marzinski 	td->dm_dev.bdev = bdev;
73886f1152bSBenjamin Marzinski 	return 0;
73986f1152bSBenjamin Marzinski }
74086f1152bSBenjamin Marzinski 
74186f1152bSBenjamin Marzinski /*
74286f1152bSBenjamin Marzinski  * Close a table device that we've been using.
74386f1152bSBenjamin Marzinski  */
74486f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md)
74586f1152bSBenjamin Marzinski {
74686f1152bSBenjamin Marzinski 	if (!td->dm_dev.bdev)
74786f1152bSBenjamin Marzinski 		return;
74886f1152bSBenjamin Marzinski 
74986f1152bSBenjamin Marzinski 	bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
75086f1152bSBenjamin Marzinski 	blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
75186f1152bSBenjamin Marzinski 	td->dm_dev.bdev = NULL;
75286f1152bSBenjamin Marzinski }
75386f1152bSBenjamin Marzinski 
75486f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev,
75586f1152bSBenjamin Marzinski 					      fmode_t mode) {
75686f1152bSBenjamin Marzinski 	struct table_device *td;
75786f1152bSBenjamin Marzinski 
75886f1152bSBenjamin Marzinski 	list_for_each_entry(td, l, list)
75986f1152bSBenjamin Marzinski 		if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
76086f1152bSBenjamin Marzinski 			return td;
76186f1152bSBenjamin Marzinski 
76286f1152bSBenjamin Marzinski 	return NULL;
76386f1152bSBenjamin Marzinski }
76486f1152bSBenjamin Marzinski 
76586f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
76686f1152bSBenjamin Marzinski 			struct dm_dev **result) {
76786f1152bSBenjamin Marzinski 	int r;
76886f1152bSBenjamin Marzinski 	struct table_device *td;
76986f1152bSBenjamin Marzinski 
77086f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
77186f1152bSBenjamin Marzinski 	td = find_table_device(&md->table_devices, dev, mode);
77286f1152bSBenjamin Marzinski 	if (!td) {
77386f1152bSBenjamin Marzinski 		td = kmalloc(sizeof(*td), GFP_KERNEL);
77486f1152bSBenjamin Marzinski 		if (!td) {
77586f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
77686f1152bSBenjamin Marzinski 			return -ENOMEM;
77786f1152bSBenjamin Marzinski 		}
77886f1152bSBenjamin Marzinski 
77986f1152bSBenjamin Marzinski 		td->dm_dev.mode = mode;
78086f1152bSBenjamin Marzinski 		td->dm_dev.bdev = NULL;
78186f1152bSBenjamin Marzinski 
78286f1152bSBenjamin Marzinski 		if ((r = open_table_device(td, dev, md))) {
78386f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
78486f1152bSBenjamin Marzinski 			kfree(td);
78586f1152bSBenjamin Marzinski 			return r;
78686f1152bSBenjamin Marzinski 		}
78786f1152bSBenjamin Marzinski 
78886f1152bSBenjamin Marzinski 		format_dev_t(td->dm_dev.name, dev);
78986f1152bSBenjamin Marzinski 
79086f1152bSBenjamin Marzinski 		atomic_set(&td->count, 0);
79186f1152bSBenjamin Marzinski 		list_add(&td->list, &md->table_devices);
79286f1152bSBenjamin Marzinski 	}
79386f1152bSBenjamin Marzinski 	atomic_inc(&td->count);
79486f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
79586f1152bSBenjamin Marzinski 
79686f1152bSBenjamin Marzinski 	*result = &td->dm_dev;
79786f1152bSBenjamin Marzinski 	return 0;
79886f1152bSBenjamin Marzinski }
79986f1152bSBenjamin Marzinski EXPORT_SYMBOL_GPL(dm_get_table_device);
80086f1152bSBenjamin Marzinski 
80186f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
80286f1152bSBenjamin Marzinski {
80386f1152bSBenjamin Marzinski 	struct table_device *td = container_of(d, struct table_device, dm_dev);
80486f1152bSBenjamin Marzinski 
80586f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
80686f1152bSBenjamin Marzinski 	if (atomic_dec_and_test(&td->count)) {
80786f1152bSBenjamin Marzinski 		close_table_device(td, md);
80886f1152bSBenjamin Marzinski 		list_del(&td->list);
80986f1152bSBenjamin Marzinski 		kfree(td);
81086f1152bSBenjamin Marzinski 	}
81186f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
81286f1152bSBenjamin Marzinski }
81386f1152bSBenjamin Marzinski EXPORT_SYMBOL(dm_put_table_device);
81486f1152bSBenjamin Marzinski 
81586f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices)
81686f1152bSBenjamin Marzinski {
81786f1152bSBenjamin Marzinski 	struct list_head *tmp, *next;
81886f1152bSBenjamin Marzinski 
81986f1152bSBenjamin Marzinski 	list_for_each_safe(tmp, next, devices) {
82086f1152bSBenjamin Marzinski 		struct table_device *td = list_entry(tmp, struct table_device, list);
82186f1152bSBenjamin Marzinski 
82286f1152bSBenjamin Marzinski 		DMWARN("dm_destroy: %s still exists with %d references",
82386f1152bSBenjamin Marzinski 		       td->dm_dev.name, atomic_read(&td->count));
82486f1152bSBenjamin Marzinski 		kfree(td);
82586f1152bSBenjamin Marzinski 	}
82686f1152bSBenjamin Marzinski }
82786f1152bSBenjamin Marzinski 
82886f1152bSBenjamin Marzinski /*
8293ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
8303ac51e74SDarrick J. Wong  */
8313ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
8323ac51e74SDarrick J. Wong {
8333ac51e74SDarrick J. Wong 	*geo = md->geometry;
8343ac51e74SDarrick J. Wong 
8353ac51e74SDarrick J. Wong 	return 0;
8363ac51e74SDarrick J. Wong }
8373ac51e74SDarrick J. Wong 
8383ac51e74SDarrick J. Wong /*
8393ac51e74SDarrick J. Wong  * Set the geometry of a device.
8403ac51e74SDarrick J. Wong  */
8413ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
8423ac51e74SDarrick J. Wong {
8433ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
8443ac51e74SDarrick J. Wong 
8453ac51e74SDarrick J. Wong 	if (geo->start > sz) {
8463ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
8473ac51e74SDarrick J. Wong 		return -EINVAL;
8483ac51e74SDarrick J. Wong 	}
8493ac51e74SDarrick J. Wong 
8503ac51e74SDarrick J. Wong 	md->geometry = *geo;
8513ac51e74SDarrick J. Wong 
8523ac51e74SDarrick J. Wong 	return 0;
8533ac51e74SDarrick J. Wong }
8543ac51e74SDarrick J. Wong 
8551da177e4SLinus Torvalds /*-----------------------------------------------------------------
8561da177e4SLinus Torvalds  * CRUD START:
8571da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
8581da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
8591da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
8601da177e4SLinus Torvalds  *   interests of getting something for people to use I give
8611da177e4SLinus Torvalds  *   you this clearly demarcated crap.
8621da177e4SLinus Torvalds  *---------------------------------------------------------------*/
8631da177e4SLinus Torvalds 
8642e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
8652e93ccc1SKiyoshi Ueda {
8662e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
8672e93ccc1SKiyoshi Ueda }
8682e93ccc1SKiyoshi Ueda 
8691da177e4SLinus Torvalds /*
8701da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
8711da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
8721da177e4SLinus Torvalds  */
873858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
8741da177e4SLinus Torvalds {
8752e93ccc1SKiyoshi Ueda 	unsigned long flags;
876b35f8caaSMilan Broz 	int io_error;
877b35f8caaSMilan Broz 	struct bio *bio;
878b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
8792e93ccc1SKiyoshi Ueda 
8802e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
881f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
882f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
883f88fb981SKiyoshi Ueda 		if (!(io->error > 0 && __noflush_suspending(md)))
8841da177e4SLinus Torvalds 			io->error = error;
885f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
886f88fb981SKiyoshi Ueda 	}
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
8892e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
8902e93ccc1SKiyoshi Ueda 			/*
8912e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
8922e93ccc1SKiyoshi Ueda 			 */
893022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
8946a8736d1STejun Heo 			if (__noflush_suspending(md))
8956a8736d1STejun Heo 				bio_list_add_head(&md->deferred, io->bio);
8966a8736d1STejun Heo 			else
8972e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
8982e93ccc1SKiyoshi Ueda 				io->error = -EIO;
899022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
9002e93ccc1SKiyoshi Ueda 		}
9012e93ccc1SKiyoshi Ueda 
902b35f8caaSMilan Broz 		io_error = io->error;
903b35f8caaSMilan Broz 		bio = io->bio;
904af7e466aSMikulas Patocka 		end_io_acct(io);
905a97f925aSMikulas Patocka 		free_io(md, io);
9061da177e4SLinus Torvalds 
9076a8736d1STejun Heo 		if (io_error == DM_ENDIO_REQUEUE)
9086a8736d1STejun Heo 			return;
9096a8736d1STejun Heo 
9104f024f37SKent Overstreet 		if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
9111da177e4SLinus Torvalds 			/*
9126a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
9136a8736d1STejun Heo 			 * without REQ_FLUSH.
9141da177e4SLinus Torvalds 			 */
9156a8736d1STejun Heo 			bio->bi_rw &= ~REQ_FLUSH;
9166a8736d1STejun Heo 			queue_io(md, bio);
9175f3ea37cSArnaldo Carvalho de Melo 		} else {
918b372d360SMike Snitzer 			/* done with normal IO or empty flush */
9190a82a8d1SLinus Torvalds 			trace_block_bio_complete(md->queue, bio, io_error);
920b35f8caaSMilan Broz 			bio_endio(bio, io_error);
9212e93ccc1SKiyoshi Ueda 		}
9221da177e4SLinus Torvalds 	}
923af7e466aSMikulas Patocka }
9241da177e4SLinus Torvalds 
9257eee4ae2SMike Snitzer static void disable_write_same(struct mapped_device *md)
9267eee4ae2SMike Snitzer {
9277eee4ae2SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
9287eee4ae2SMike Snitzer 
9297eee4ae2SMike Snitzer 	/* device doesn't really support WRITE SAME, disable it */
9307eee4ae2SMike Snitzer 	limits->max_write_same_sectors = 0;
9317eee4ae2SMike Snitzer }
9327eee4ae2SMike Snitzer 
9336712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error)
9341da177e4SLinus Torvalds {
9355164beceSzhendong chen 	int r = error;
936bfc6d41cSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
937b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
9389faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
9391da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 	if (!bio_flagged(bio, BIO_UPTODATE) && !error)
9421da177e4SLinus Torvalds 		error = -EIO;
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds 	if (endio) {
9457de3ee57SMikulas Patocka 		r = endio(tio->ti, bio, error);
9462e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
9472e93ccc1SKiyoshi Ueda 			/*
9482e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
9492e93ccc1SKiyoshi Ueda 			 * in dec_pending().
9502e93ccc1SKiyoshi Ueda 			 */
9511da177e4SLinus Torvalds 			error = r;
95245cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
95345cbcd79SKiyoshi Ueda 			/* The target will handle the io */
9546712ecf8SNeilBrown 			return;
95545cbcd79SKiyoshi Ueda 		else if (r) {
95645cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
95745cbcd79SKiyoshi Ueda 			BUG();
95845cbcd79SKiyoshi Ueda 		}
9591da177e4SLinus Torvalds 	}
9601da177e4SLinus Torvalds 
9617eee4ae2SMike Snitzer 	if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) &&
9627eee4ae2SMike Snitzer 		     !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
9637eee4ae2SMike Snitzer 		disable_write_same(md);
9647eee4ae2SMike Snitzer 
9659faf400fSStefan Bader 	free_tio(md, tio);
966b35f8caaSMilan Broz 	dec_pending(io, error);
9671da177e4SLinus Torvalds }
9681da177e4SLinus Torvalds 
969cec47e3dSKiyoshi Ueda /*
970cec47e3dSKiyoshi Ueda  * Partial completion handling for request-based dm
971cec47e3dSKiyoshi Ueda  */
972cec47e3dSKiyoshi Ueda static void end_clone_bio(struct bio *clone, int error)
973cec47e3dSKiyoshi Ueda {
974bfc6d41cSMikulas Patocka 	struct dm_rq_clone_bio_info *info =
975bfc6d41cSMikulas Patocka 		container_of(clone, struct dm_rq_clone_bio_info, clone);
976cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = info->tio;
977cec47e3dSKiyoshi Ueda 	struct bio *bio = info->orig;
9784f024f37SKent Overstreet 	unsigned int nr_bytes = info->orig->bi_iter.bi_size;
979cec47e3dSKiyoshi Ueda 
980cec47e3dSKiyoshi Ueda 	bio_put(clone);
981cec47e3dSKiyoshi Ueda 
982cec47e3dSKiyoshi Ueda 	if (tio->error)
983cec47e3dSKiyoshi Ueda 		/*
984cec47e3dSKiyoshi Ueda 		 * An error has already been detected on the request.
985cec47e3dSKiyoshi Ueda 		 * Once error occurred, just let clone->end_io() handle
986cec47e3dSKiyoshi Ueda 		 * the remainder.
987cec47e3dSKiyoshi Ueda 		 */
988cec47e3dSKiyoshi Ueda 		return;
989cec47e3dSKiyoshi Ueda 	else if (error) {
990cec47e3dSKiyoshi Ueda 		/*
991cec47e3dSKiyoshi Ueda 		 * Don't notice the error to the upper layer yet.
992cec47e3dSKiyoshi Ueda 		 * The error handling decision is made by the target driver,
993cec47e3dSKiyoshi Ueda 		 * when the request is completed.
994cec47e3dSKiyoshi Ueda 		 */
995cec47e3dSKiyoshi Ueda 		tio->error = error;
996cec47e3dSKiyoshi Ueda 		return;
997cec47e3dSKiyoshi Ueda 	}
998cec47e3dSKiyoshi Ueda 
999cec47e3dSKiyoshi Ueda 	/*
1000cec47e3dSKiyoshi Ueda 	 * I/O for the bio successfully completed.
1001cec47e3dSKiyoshi Ueda 	 * Notice the data completion to the upper layer.
1002cec47e3dSKiyoshi Ueda 	 */
1003cec47e3dSKiyoshi Ueda 
1004cec47e3dSKiyoshi Ueda 	/*
1005cec47e3dSKiyoshi Ueda 	 * bios are processed from the head of the list.
1006cec47e3dSKiyoshi Ueda 	 * So the completing bio should always be rq->bio.
1007cec47e3dSKiyoshi Ueda 	 * If it's not, something wrong is happening.
1008cec47e3dSKiyoshi Ueda 	 */
1009cec47e3dSKiyoshi Ueda 	if (tio->orig->bio != bio)
1010cec47e3dSKiyoshi Ueda 		DMERR("bio completion is going in the middle of the request");
1011cec47e3dSKiyoshi Ueda 
1012cec47e3dSKiyoshi Ueda 	/*
1013cec47e3dSKiyoshi Ueda 	 * Update the original request.
1014cec47e3dSKiyoshi Ueda 	 * Do not use blk_end_request() here, because it may complete
1015cec47e3dSKiyoshi Ueda 	 * the original request before the clone, and break the ordering.
1016cec47e3dSKiyoshi Ueda 	 */
1017cec47e3dSKiyoshi Ueda 	blk_update_request(tio->orig, 0, nr_bytes);
1018cec47e3dSKiyoshi Ueda }
1019cec47e3dSKiyoshi Ueda 
1020cec47e3dSKiyoshi Ueda /*
1021cec47e3dSKiyoshi Ueda  * Don't touch any member of the md after calling this function because
1022cec47e3dSKiyoshi Ueda  * the md may be freed in dm_put() at the end of this function.
1023cec47e3dSKiyoshi Ueda  * Or do dm_get() before calling this function and dm_put() later.
1024cec47e3dSKiyoshi Ueda  */
1025466d89a6SKeith Busch static void rq_completed(struct mapped_device *md, int rw, bool run_queue)
1026cec47e3dSKiyoshi Ueda {
1027b4324feeSKiyoshi Ueda 	atomic_dec(&md->pending[rw]);
1028cec47e3dSKiyoshi Ueda 
1029cec47e3dSKiyoshi Ueda 	/* nudge anyone waiting on suspend queue */
1030b4324feeSKiyoshi Ueda 	if (!md_in_flight(md))
1031cec47e3dSKiyoshi Ueda 		wake_up(&md->wait);
1032cec47e3dSKiyoshi Ueda 
1033a8c32a5cSJens Axboe 	/*
1034a8c32a5cSJens Axboe 	 * Run this off this callpath, as drivers could invoke end_io while
1035a8c32a5cSJens Axboe 	 * inside their request_fn (and holding the queue lock). Calling
1036a8c32a5cSJens Axboe 	 * back into ->request_fn() could deadlock attempting to grab the
1037a8c32a5cSJens Axboe 	 * queue lock again.
1038a8c32a5cSJens Axboe 	 */
1039cec47e3dSKiyoshi Ueda 	if (run_queue)
1040a8c32a5cSJens Axboe 		blk_run_queue_async(md->queue);
1041cec47e3dSKiyoshi Ueda 
1042cec47e3dSKiyoshi Ueda 	/*
1043cec47e3dSKiyoshi Ueda 	 * dm_put() must be at the end of this function. See the comment above
1044cec47e3dSKiyoshi Ueda 	 */
1045cec47e3dSKiyoshi Ueda 	dm_put(md);
1046cec47e3dSKiyoshi Ueda }
1047cec47e3dSKiyoshi Ueda 
1048a77e28c7SKiyoshi Ueda static void free_rq_clone(struct request *clone)
1049a77e28c7SKiyoshi Ueda {
1050a77e28c7SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1051a77e28c7SKiyoshi Ueda 
1052a77e28c7SKiyoshi Ueda 	blk_rq_unprep_clone(clone);
1053e5863d9aSMike Snitzer 	if (clone->q && clone->q->mq_ops)
1054e5863d9aSMike Snitzer 		tio->ti->type->release_clone_rq(clone);
1055e5863d9aSMike Snitzer 	else
10561ae49ea2SMike Snitzer 		free_clone_request(tio->md, clone);
1057a77e28c7SKiyoshi Ueda 	free_rq_tio(tio);
1058a77e28c7SKiyoshi Ueda }
1059a77e28c7SKiyoshi Ueda 
1060980691e5SKiyoshi Ueda /*
1061980691e5SKiyoshi Ueda  * Complete the clone and the original request.
1062466d89a6SKeith Busch  * Must be called without clone's queue lock held,
1063466d89a6SKeith Busch  * see end_clone_request() for more details.
1064980691e5SKiyoshi Ueda  */
1065980691e5SKiyoshi Ueda static void dm_end_request(struct request *clone, int error)
1066980691e5SKiyoshi Ueda {
1067980691e5SKiyoshi Ueda 	int rw = rq_data_dir(clone);
1068980691e5SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1069980691e5SKiyoshi Ueda 	struct mapped_device *md = tio->md;
1070980691e5SKiyoshi Ueda 	struct request *rq = tio->orig;
1071980691e5SKiyoshi Ueda 
107229e4013dSTejun Heo 	if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
1073980691e5SKiyoshi Ueda 		rq->errors = clone->errors;
1074980691e5SKiyoshi Ueda 		rq->resid_len = clone->resid_len;
1075980691e5SKiyoshi Ueda 
1076980691e5SKiyoshi Ueda 		if (rq->sense)
1077980691e5SKiyoshi Ueda 			/*
1078980691e5SKiyoshi Ueda 			 * We are using the sense buffer of the original
1079980691e5SKiyoshi Ueda 			 * request.
1080980691e5SKiyoshi Ueda 			 * So setting the length of the sense data is enough.
1081980691e5SKiyoshi Ueda 			 */
1082980691e5SKiyoshi Ueda 			rq->sense_len = clone->sense_len;
1083980691e5SKiyoshi Ueda 	}
1084980691e5SKiyoshi Ueda 
1085980691e5SKiyoshi Ueda 	free_rq_clone(clone);
1086980691e5SKiyoshi Ueda 	blk_end_request_all(rq, error);
108729e4013dSTejun Heo 	rq_completed(md, rw, true);
1088980691e5SKiyoshi Ueda }
1089980691e5SKiyoshi Ueda 
1090cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq)
1091cec47e3dSKiyoshi Ueda {
1092466d89a6SKeith Busch 	struct dm_rq_target_io *tio = rq->special;
1093466d89a6SKeith Busch 	struct request *clone = tio->clone;
1094cec47e3dSKiyoshi Ueda 
1095cec47e3dSKiyoshi Ueda 	rq->special = NULL;
1096cec47e3dSKiyoshi Ueda 	rq->cmd_flags &= ~REQ_DONTPREP;
1097cec47e3dSKiyoshi Ueda 
1098e5863d9aSMike Snitzer 	if (clone)
1099a77e28c7SKiyoshi Ueda 		free_rq_clone(clone);
1100cec47e3dSKiyoshi Ueda }
1101cec47e3dSKiyoshi Ueda 
1102cec47e3dSKiyoshi Ueda /*
1103cec47e3dSKiyoshi Ueda  * Requeue the original request of a clone.
1104cec47e3dSKiyoshi Ueda  */
1105466d89a6SKeith Busch static void dm_requeue_unmapped_original_request(struct mapped_device *md,
1106466d89a6SKeith Busch 						 struct request *rq)
1107cec47e3dSKiyoshi Ueda {
1108466d89a6SKeith Busch 	int rw = rq_data_dir(rq);
1109cec47e3dSKiyoshi Ueda 	struct request_queue *q = rq->q;
1110cec47e3dSKiyoshi Ueda 	unsigned long flags;
1111cec47e3dSKiyoshi Ueda 
1112cec47e3dSKiyoshi Ueda 	dm_unprep_request(rq);
1113cec47e3dSKiyoshi Ueda 
1114cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
1115cec47e3dSKiyoshi Ueda 	blk_requeue_request(q, rq);
1116cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
1117cec47e3dSKiyoshi Ueda 
1118466d89a6SKeith Busch 	rq_completed(md, rw, false);
1119cec47e3dSKiyoshi Ueda }
1120466d89a6SKeith Busch 
1121466d89a6SKeith Busch static void dm_requeue_unmapped_request(struct request *clone)
1122466d89a6SKeith Busch {
1123466d89a6SKeith Busch 	struct dm_rq_target_io *tio = clone->end_io_data;
1124466d89a6SKeith Busch 
1125466d89a6SKeith Busch 	dm_requeue_unmapped_original_request(tio->md, tio->orig);
1126cec47e3dSKiyoshi Ueda }
1127cec47e3dSKiyoshi Ueda 
1128cec47e3dSKiyoshi Ueda static void __stop_queue(struct request_queue *q)
1129cec47e3dSKiyoshi Ueda {
1130cec47e3dSKiyoshi Ueda 	blk_stop_queue(q);
1131cec47e3dSKiyoshi Ueda }
1132cec47e3dSKiyoshi Ueda 
1133cec47e3dSKiyoshi Ueda static void stop_queue(struct request_queue *q)
1134cec47e3dSKiyoshi Ueda {
1135cec47e3dSKiyoshi Ueda 	unsigned long flags;
1136cec47e3dSKiyoshi Ueda 
1137cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
1138cec47e3dSKiyoshi Ueda 	__stop_queue(q);
1139cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
1140cec47e3dSKiyoshi Ueda }
1141cec47e3dSKiyoshi Ueda 
1142cec47e3dSKiyoshi Ueda static void __start_queue(struct request_queue *q)
1143cec47e3dSKiyoshi Ueda {
1144cec47e3dSKiyoshi Ueda 	if (blk_queue_stopped(q))
1145cec47e3dSKiyoshi Ueda 		blk_start_queue(q);
1146cec47e3dSKiyoshi Ueda }
1147cec47e3dSKiyoshi Ueda 
1148cec47e3dSKiyoshi Ueda static void start_queue(struct request_queue *q)
1149cec47e3dSKiyoshi Ueda {
1150cec47e3dSKiyoshi Ueda 	unsigned long flags;
1151cec47e3dSKiyoshi Ueda 
1152cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
1153cec47e3dSKiyoshi Ueda 	__start_queue(q);
1154cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
1155cec47e3dSKiyoshi Ueda }
1156cec47e3dSKiyoshi Ueda 
115711a68244SKiyoshi Ueda static void dm_done(struct request *clone, int error, bool mapped)
115811a68244SKiyoshi Ueda {
115911a68244SKiyoshi Ueda 	int r = error;
116011a68244SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1161ba1cbad9SMike Snitzer 	dm_request_endio_fn rq_end_io = NULL;
1162ba1cbad9SMike Snitzer 
1163ba1cbad9SMike Snitzer 	if (tio->ti) {
1164ba1cbad9SMike Snitzer 		rq_end_io = tio->ti->type->rq_end_io;
116511a68244SKiyoshi Ueda 
116611a68244SKiyoshi Ueda 		if (mapped && rq_end_io)
116711a68244SKiyoshi Ueda 			r = rq_end_io(tio->ti, clone, error, &tio->info);
1168ba1cbad9SMike Snitzer 	}
116911a68244SKiyoshi Ueda 
11707eee4ae2SMike Snitzer 	if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) &&
11717eee4ae2SMike Snitzer 		     !clone->q->limits.max_write_same_sectors))
11727eee4ae2SMike Snitzer 		disable_write_same(tio->md);
11737eee4ae2SMike Snitzer 
117411a68244SKiyoshi Ueda 	if (r <= 0)
117511a68244SKiyoshi Ueda 		/* The target wants to complete the I/O */
117611a68244SKiyoshi Ueda 		dm_end_request(clone, r);
117711a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_INCOMPLETE)
117811a68244SKiyoshi Ueda 		/* The target will handle the I/O */
117911a68244SKiyoshi Ueda 		return;
118011a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_REQUEUE)
118111a68244SKiyoshi Ueda 		/* The target wants to requeue the I/O */
118211a68244SKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
118311a68244SKiyoshi Ueda 	else {
118411a68244SKiyoshi Ueda 		DMWARN("unimplemented target endio return value: %d", r);
118511a68244SKiyoshi Ueda 		BUG();
118611a68244SKiyoshi Ueda 	}
118711a68244SKiyoshi Ueda }
118811a68244SKiyoshi Ueda 
1189cec47e3dSKiyoshi Ueda /*
1190cec47e3dSKiyoshi Ueda  * Request completion handler for request-based dm
1191cec47e3dSKiyoshi Ueda  */
1192cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq)
1193cec47e3dSKiyoshi Ueda {
119411a68244SKiyoshi Ueda 	bool mapped = true;
1195466d89a6SKeith Busch 	struct dm_rq_target_io *tio = rq->special;
1196466d89a6SKeith Busch 	struct request *clone = tio->clone;
1197cec47e3dSKiyoshi Ueda 
1198e5863d9aSMike Snitzer 	if (!clone) {
1199e5863d9aSMike Snitzer 		blk_end_request_all(rq, tio->error);
1200e5863d9aSMike Snitzer 		rq_completed(tio->md, rq_data_dir(rq), false);
1201e5863d9aSMike Snitzer 		free_rq_tio(tio);
1202e5863d9aSMike Snitzer 		return;
1203e5863d9aSMike Snitzer 	}
1204cec47e3dSKiyoshi Ueda 
120511a68244SKiyoshi Ueda 	if (rq->cmd_flags & REQ_FAILED)
120611a68244SKiyoshi Ueda 		mapped = false;
1207cec47e3dSKiyoshi Ueda 
120811a68244SKiyoshi Ueda 	dm_done(clone, tio->error, mapped);
1209cec47e3dSKiyoshi Ueda }
1210cec47e3dSKiyoshi Ueda 
1211cec47e3dSKiyoshi Ueda /*
1212cec47e3dSKiyoshi Ueda  * Complete the clone and the original request with the error status
1213cec47e3dSKiyoshi Ueda  * through softirq context.
1214cec47e3dSKiyoshi Ueda  */
1215466d89a6SKeith Busch static void dm_complete_request(struct request *rq, int error)
1216cec47e3dSKiyoshi Ueda {
1217466d89a6SKeith Busch 	struct dm_rq_target_io *tio = rq->special;
1218cec47e3dSKiyoshi Ueda 
1219cec47e3dSKiyoshi Ueda 	tio->error = error;
1220cec47e3dSKiyoshi Ueda 	blk_complete_request(rq);
1221cec47e3dSKiyoshi Ueda }
1222cec47e3dSKiyoshi Ueda 
1223cec47e3dSKiyoshi Ueda /*
1224cec47e3dSKiyoshi Ueda  * Complete the not-mapped clone and the original request with the error status
1225cec47e3dSKiyoshi Ueda  * through softirq context.
1226cec47e3dSKiyoshi Ueda  * Target's rq_end_io() function isn't called.
1227e5863d9aSMike Snitzer  * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
1228cec47e3dSKiyoshi Ueda  */
1229466d89a6SKeith Busch static void dm_kill_unmapped_request(struct request *rq, int error)
1230cec47e3dSKiyoshi Ueda {
1231cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_FAILED;
1232466d89a6SKeith Busch 	dm_complete_request(rq, error);
1233cec47e3dSKiyoshi Ueda }
1234cec47e3dSKiyoshi Ueda 
1235cec47e3dSKiyoshi Ueda /*
1236466d89a6SKeith Busch  * Called with the clone's queue lock held
1237cec47e3dSKiyoshi Ueda  */
1238cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error)
1239cec47e3dSKiyoshi Ueda {
1240466d89a6SKeith Busch 	struct dm_rq_target_io *tio = clone->end_io_data;
1241466d89a6SKeith Busch 
1242e5863d9aSMike Snitzer 	if (!clone->q->mq_ops) {
1243cec47e3dSKiyoshi Ueda 		/*
1244cec47e3dSKiyoshi Ueda 		 * For just cleaning up the information of the queue in which
1245cec47e3dSKiyoshi Ueda 		 * the clone was dispatched.
1246e5863d9aSMike Snitzer 		 * The clone is *NOT* freed actually here because it is alloced
1247e5863d9aSMike Snitzer 		 * from dm own mempool (REQ_ALLOCED isn't set).
1248cec47e3dSKiyoshi Ueda 		 */
1249cec47e3dSKiyoshi Ueda 		__blk_put_request(clone->q, clone);
1250e5863d9aSMike Snitzer 	}
1251cec47e3dSKiyoshi Ueda 
1252cec47e3dSKiyoshi Ueda 	/*
1253cec47e3dSKiyoshi Ueda 	 * Actual request completion is done in a softirq context which doesn't
1254466d89a6SKeith Busch 	 * hold the clone's queue lock.  Otherwise, deadlock could occur because:
1255cec47e3dSKiyoshi Ueda 	 *     - another request may be submitted by the upper level driver
1256cec47e3dSKiyoshi Ueda 	 *       of the stacking during the completion
1257cec47e3dSKiyoshi Ueda 	 *     - the submission which requires queue lock may be done
1258466d89a6SKeith Busch 	 *       against this clone's queue
1259cec47e3dSKiyoshi Ueda 	 */
1260466d89a6SKeith Busch 	dm_complete_request(tio->orig, error);
1261cec47e3dSKiyoshi Ueda }
1262cec47e3dSKiyoshi Ueda 
126356a67df7SMike Snitzer /*
126456a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
126556a67df7SMike Snitzer  * target boundary.
126656a67df7SMike Snitzer  */
126756a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
12681da177e4SLinus Torvalds {
126956a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
127056a67df7SMike Snitzer 
127156a67df7SMike Snitzer 	return ti->len - target_offset;
127256a67df7SMike Snitzer }
127356a67df7SMike Snitzer 
127456a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
127556a67df7SMike Snitzer {
127656a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
1277542f9038SMike Snitzer 	sector_t offset, max_len;
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 	/*
12801da177e4SLinus Torvalds 	 * Does the target need to split even further?
12811da177e4SLinus Torvalds 	 */
1282542f9038SMike Snitzer 	if (ti->max_io_len) {
1283542f9038SMike Snitzer 		offset = dm_target_offset(ti, sector);
1284542f9038SMike Snitzer 		if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1285542f9038SMike Snitzer 			max_len = sector_div(offset, ti->max_io_len);
1286542f9038SMike Snitzer 		else
1287542f9038SMike Snitzer 			max_len = offset & (ti->max_io_len - 1);
1288542f9038SMike Snitzer 		max_len = ti->max_io_len - max_len;
1289542f9038SMike Snitzer 
1290542f9038SMike Snitzer 		if (len > max_len)
1291542f9038SMike Snitzer 			len = max_len;
12921da177e4SLinus Torvalds 	}
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 	return len;
12951da177e4SLinus Torvalds }
12961da177e4SLinus Torvalds 
1297542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1298542f9038SMike Snitzer {
1299542f9038SMike Snitzer 	if (len > UINT_MAX) {
1300542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1301542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
1302542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
1303542f9038SMike Snitzer 		return -EINVAL;
1304542f9038SMike Snitzer 	}
1305542f9038SMike Snitzer 
1306542f9038SMike Snitzer 	ti->max_io_len = (uint32_t) len;
1307542f9038SMike Snitzer 
1308542f9038SMike Snitzer 	return 0;
1309542f9038SMike Snitzer }
1310542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1311542f9038SMike Snitzer 
13121dd40c3eSMikulas Patocka /*
13131dd40c3eSMikulas Patocka  * A target may call dm_accept_partial_bio only from the map routine.  It is
13141dd40c3eSMikulas Patocka  * allowed for all bio types except REQ_FLUSH.
13151dd40c3eSMikulas Patocka  *
13161dd40c3eSMikulas Patocka  * dm_accept_partial_bio informs the dm that the target only wants to process
13171dd40c3eSMikulas Patocka  * additional n_sectors sectors of the bio and the rest of the data should be
13181dd40c3eSMikulas Patocka  * sent in a next bio.
13191dd40c3eSMikulas Patocka  *
13201dd40c3eSMikulas Patocka  * A diagram that explains the arithmetics:
13211dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
13221dd40c3eSMikulas Patocka  * |         1          |       2       |   3   |
13231dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
13241dd40c3eSMikulas Patocka  *
13251dd40c3eSMikulas Patocka  * <-------------- *tio->len_ptr --------------->
13261dd40c3eSMikulas Patocka  *                      <------- bi_size ------->
13271dd40c3eSMikulas Patocka  *                      <-- n_sectors -->
13281dd40c3eSMikulas Patocka  *
13291dd40c3eSMikulas Patocka  * Region 1 was already iterated over with bio_advance or similar function.
13301dd40c3eSMikulas Patocka  *	(it may be empty if the target doesn't use bio_advance)
13311dd40c3eSMikulas Patocka  * Region 2 is the remaining bio size that the target wants to process.
13321dd40c3eSMikulas Patocka  *	(it may be empty if region 1 is non-empty, although there is no reason
13331dd40c3eSMikulas Patocka  *	 to make it empty)
13341dd40c3eSMikulas Patocka  * The target requires that region 3 is to be sent in the next bio.
13351dd40c3eSMikulas Patocka  *
13361dd40c3eSMikulas Patocka  * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
13371dd40c3eSMikulas Patocka  * the partially processed part (the sum of regions 1+2) must be the same for all
13381dd40c3eSMikulas Patocka  * copies of the bio.
13391dd40c3eSMikulas Patocka  */
13401dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
13411dd40c3eSMikulas Patocka {
13421dd40c3eSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
13431dd40c3eSMikulas Patocka 	unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
13441dd40c3eSMikulas Patocka 	BUG_ON(bio->bi_rw & REQ_FLUSH);
13451dd40c3eSMikulas Patocka 	BUG_ON(bi_size > *tio->len_ptr);
13461dd40c3eSMikulas Patocka 	BUG_ON(n_sectors > bi_size);
13471dd40c3eSMikulas Patocka 	*tio->len_ptr -= bi_size - n_sectors;
13481dd40c3eSMikulas Patocka 	bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
13491dd40c3eSMikulas Patocka }
13501dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
13511dd40c3eSMikulas Patocka 
1352bd2a49b8SAlasdair G Kergon static void __map_bio(struct dm_target_io *tio)
13531da177e4SLinus Torvalds {
13541da177e4SLinus Torvalds 	int r;
13552056a782SJens Axboe 	sector_t sector;
13569faf400fSStefan Bader 	struct mapped_device *md;
1357dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
1358bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
13611da177e4SLinus Torvalds 
13621da177e4SLinus Torvalds 	/*
13631da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
13641da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
13651da177e4SLinus Torvalds 	 * this io.
13661da177e4SLinus Torvalds 	 */
13671da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
13684f024f37SKent Overstreet 	sector = clone->bi_iter.bi_sector;
13697de3ee57SMikulas Patocka 	r = ti->type->map(ti, clone);
137045cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
13711da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
13722056a782SJens Axboe 
1373d07335e5SMike Snitzer 		trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
137422a7c31aSAlan D. Brunelle 				      tio->io->bio->bi_bdev->bd_dev, sector);
13752056a782SJens Axboe 
13761da177e4SLinus Torvalds 		generic_make_request(clone);
13772e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
13782e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
13799faf400fSStefan Bader 		md = tio->io->md;
13809faf400fSStefan Bader 		dec_pending(tio->io, r);
13819faf400fSStefan Bader 		free_tio(md, tio);
138245cbcd79SKiyoshi Ueda 	} else if (r) {
138345cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
138445cbcd79SKiyoshi Ueda 		BUG();
13851da177e4SLinus Torvalds 	}
13861da177e4SLinus Torvalds }
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds struct clone_info {
13891da177e4SLinus Torvalds 	struct mapped_device *md;
13901da177e4SLinus Torvalds 	struct dm_table *map;
13911da177e4SLinus Torvalds 	struct bio *bio;
13921da177e4SLinus Torvalds 	struct dm_io *io;
13931da177e4SLinus Torvalds 	sector_t sector;
1394e0d6609aSMikulas Patocka 	unsigned sector_count;
13951da177e4SLinus Torvalds };
13961da177e4SLinus Torvalds 
1397e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
1398bd2a49b8SAlasdair G Kergon {
13994f024f37SKent Overstreet 	bio->bi_iter.bi_sector = sector;
14004f024f37SKent Overstreet 	bio->bi_iter.bi_size = to_bytes(len);
14011da177e4SLinus Torvalds }
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds /*
14041da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
14051da177e4SLinus Torvalds  */
1406dba14160SMikulas Patocka static void clone_bio(struct dm_target_io *tio, struct bio *bio,
14071c3b13e6SKent Overstreet 		      sector_t sector, unsigned len)
14081da177e4SLinus Torvalds {
1409dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
14101da177e4SLinus Torvalds 
14111c3b13e6SKent Overstreet 	__bio_clone_fast(clone, bio);
14129c47008dSMartin K. Petersen 
14131c3b13e6SKent Overstreet 	if (bio_integrity(bio))
14141c3b13e6SKent Overstreet 		bio_integrity_clone(clone, bio, GFP_NOIO);
14151c3b13e6SKent Overstreet 
14161c3b13e6SKent Overstreet 	bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
14171c3b13e6SKent Overstreet 	clone->bi_iter.bi_size = to_bytes(len);
14181c3b13e6SKent Overstreet 
14191c3b13e6SKent Overstreet 	if (bio_integrity(bio))
14201c3b13e6SKent Overstreet 		bio_integrity_trim(clone, 0, len);
14211da177e4SLinus Torvalds }
14221da177e4SLinus Torvalds 
14239015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci,
142499778273SJunichi Nomura 				      struct dm_target *ti,
142555a62eefSAlasdair G Kergon 				      unsigned target_bio_nr)
1426f9ab94ceSMikulas Patocka {
1427dba14160SMikulas Patocka 	struct dm_target_io *tio;
1428dba14160SMikulas Patocka 	struct bio *clone;
1429dba14160SMikulas Patocka 
143099778273SJunichi Nomura 	clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1431dba14160SMikulas Patocka 	tio = container_of(clone, struct dm_target_io, clone);
1432f9ab94ceSMikulas Patocka 
1433f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1434f9ab94ceSMikulas Patocka 	tio->ti = ti;
143555a62eefSAlasdair G Kergon 	tio->target_bio_nr = target_bio_nr;
14369015df24SAlasdair G Kergon 
14379015df24SAlasdair G Kergon 	return tio;
14389015df24SAlasdair G Kergon }
14399015df24SAlasdair G Kergon 
144014fe594dSAlasdair G Kergon static void __clone_and_map_simple_bio(struct clone_info *ci,
144114fe594dSAlasdair G Kergon 				       struct dm_target *ti,
14421dd40c3eSMikulas Patocka 				       unsigned target_bio_nr, unsigned *len)
14439015df24SAlasdair G Kergon {
144499778273SJunichi Nomura 	struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
1445dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
14469015df24SAlasdair G Kergon 
14471dd40c3eSMikulas Patocka 	tio->len_ptr = len;
14481dd40c3eSMikulas Patocka 
14491c3b13e6SKent Overstreet 	__bio_clone_fast(clone, ci->bio);
1450bd2a49b8SAlasdair G Kergon 	if (len)
14511dd40c3eSMikulas Patocka 		bio_setup_sector(clone, ci->sector, *len);
1452f9ab94ceSMikulas Patocka 
1453bd2a49b8SAlasdair G Kergon 	__map_bio(tio);
1454f9ab94ceSMikulas Patocka }
1455f9ab94ceSMikulas Patocka 
145614fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
14571dd40c3eSMikulas Patocka 				  unsigned num_bios, unsigned *len)
145806a426ceSMike Snitzer {
145955a62eefSAlasdair G Kergon 	unsigned target_bio_nr;
146006a426ceSMike Snitzer 
146155a62eefSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
146214fe594dSAlasdair G Kergon 		__clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
146306a426ceSMike Snitzer }
146406a426ceSMike Snitzer 
146514fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1466f9ab94ceSMikulas Patocka {
146706a426ceSMike Snitzer 	unsigned target_nr = 0;
1468f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1469f9ab94ceSMikulas Patocka 
1470b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1471f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
14721dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
1473f9ab94ceSMikulas Patocka 
1474f9ab94ceSMikulas Patocka 	return 0;
1475f9ab94ceSMikulas Patocka }
1476f9ab94ceSMikulas Patocka 
1477e4c93811SAlasdair G Kergon static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
14781dd40c3eSMikulas Patocka 				     sector_t sector, unsigned *len)
14795ae89a87SMike Snitzer {
1480dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
14815ae89a87SMike Snitzer 	struct dm_target_io *tio;
1482b0d8ed4dSAlasdair G Kergon 	unsigned target_bio_nr;
1483b0d8ed4dSAlasdair G Kergon 	unsigned num_target_bios = 1;
14845ae89a87SMike Snitzer 
1485b0d8ed4dSAlasdair G Kergon 	/*
1486b0d8ed4dSAlasdair G Kergon 	 * Does the target want to receive duplicate copies of the bio?
1487b0d8ed4dSAlasdair G Kergon 	 */
1488b0d8ed4dSAlasdair G Kergon 	if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1489b0d8ed4dSAlasdair G Kergon 		num_target_bios = ti->num_write_bios(ti, bio);
1490e4c93811SAlasdair G Kergon 
1491b0d8ed4dSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
149299778273SJunichi Nomura 		tio = alloc_tio(ci, ti, target_bio_nr);
14931dd40c3eSMikulas Patocka 		tio->len_ptr = len;
14941dd40c3eSMikulas Patocka 		clone_bio(tio, bio, sector, *len);
1495bd2a49b8SAlasdair G Kergon 		__map_bio(tio);
14965ae89a87SMike Snitzer 	}
1497b0d8ed4dSAlasdair G Kergon }
14985ae89a87SMike Snitzer 
149955a62eefSAlasdair G Kergon typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
150023508a96SMike Snitzer 
150155a62eefSAlasdair G Kergon static unsigned get_num_discard_bios(struct dm_target *ti)
150223508a96SMike Snitzer {
150355a62eefSAlasdair G Kergon 	return ti->num_discard_bios;
150423508a96SMike Snitzer }
150523508a96SMike Snitzer 
150655a62eefSAlasdair G Kergon static unsigned get_num_write_same_bios(struct dm_target *ti)
150723508a96SMike Snitzer {
150855a62eefSAlasdair G Kergon 	return ti->num_write_same_bios;
150923508a96SMike Snitzer }
151023508a96SMike Snitzer 
151123508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti);
151223508a96SMike Snitzer 
151323508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti)
151423508a96SMike Snitzer {
151555a62eefSAlasdair G Kergon 	return ti->split_discard_bios;
151623508a96SMike Snitzer }
151723508a96SMike Snitzer 
151814fe594dSAlasdair G Kergon static int __send_changing_extent_only(struct clone_info *ci,
151955a62eefSAlasdair G Kergon 				       get_num_bios_fn get_num_bios,
152023508a96SMike Snitzer 				       is_split_required_fn is_split_required)
15215ae89a87SMike Snitzer {
15225ae89a87SMike Snitzer 	struct dm_target *ti;
1523e0d6609aSMikulas Patocka 	unsigned len;
152455a62eefSAlasdair G Kergon 	unsigned num_bios;
15255ae89a87SMike Snitzer 
1526a79245b3SMike Snitzer 	do {
15275ae89a87SMike Snitzer 		ti = dm_table_find_target(ci->map, ci->sector);
15285ae89a87SMike Snitzer 		if (!dm_target_is_valid(ti))
15295ae89a87SMike Snitzer 			return -EIO;
15305ae89a87SMike Snitzer 
15315ae89a87SMike Snitzer 		/*
153223508a96SMike Snitzer 		 * Even though the device advertised support for this type of
153323508a96SMike Snitzer 		 * request, that does not mean every target supports it, and
1534936688d7SMike Snitzer 		 * reconfiguration might also have changed that since the
15355ae89a87SMike Snitzer 		 * check was performed.
15365ae89a87SMike Snitzer 		 */
153755a62eefSAlasdair G Kergon 		num_bios = get_num_bios ? get_num_bios(ti) : 0;
153855a62eefSAlasdair G Kergon 		if (!num_bios)
15395ae89a87SMike Snitzer 			return -EOPNOTSUPP;
15405ae89a87SMike Snitzer 
154123508a96SMike Snitzer 		if (is_split_required && !is_split_required(ti))
1542e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
15437acf0277SMikulas Patocka 		else
1544e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
15455ae89a87SMike Snitzer 
15461dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, num_bios, &len);
15475ae89a87SMike Snitzer 
1548a79245b3SMike Snitzer 		ci->sector += len;
1549a79245b3SMike Snitzer 	} while (ci->sector_count -= len);
15505ae89a87SMike Snitzer 
15515ae89a87SMike Snitzer 	return 0;
15525ae89a87SMike Snitzer }
15535ae89a87SMike Snitzer 
155414fe594dSAlasdair G Kergon static int __send_discard(struct clone_info *ci)
155523508a96SMike Snitzer {
155614fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_discard_bios,
155723508a96SMike Snitzer 					   is_split_required_for_discard);
155823508a96SMike Snitzer }
155923508a96SMike Snitzer 
156014fe594dSAlasdair G Kergon static int __send_write_same(struct clone_info *ci)
156123508a96SMike Snitzer {
156214fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
156323508a96SMike Snitzer }
156423508a96SMike Snitzer 
1565e4c93811SAlasdair G Kergon /*
1566e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1567e4c93811SAlasdair G Kergon  */
1568e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci)
1569e4c93811SAlasdair G Kergon {
1570e4c93811SAlasdair G Kergon 	struct bio *bio = ci->bio;
1571e4c93811SAlasdair G Kergon 	struct dm_target *ti;
15721c3b13e6SKent Overstreet 	unsigned len;
1573e4c93811SAlasdair G Kergon 
1574e4c93811SAlasdair G Kergon 	if (unlikely(bio->bi_rw & REQ_DISCARD))
1575e4c93811SAlasdair G Kergon 		return __send_discard(ci);
1576e4c93811SAlasdair G Kergon 	else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
1577e4c93811SAlasdair G Kergon 		return __send_write_same(ci);
1578e4c93811SAlasdair G Kergon 
1579e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1580e4c93811SAlasdair G Kergon 	if (!dm_target_is_valid(ti))
1581e4c93811SAlasdair G Kergon 		return -EIO;
1582e4c93811SAlasdair G Kergon 
15831c3b13e6SKent Overstreet 	len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
1584e4c93811SAlasdair G Kergon 
15851dd40c3eSMikulas Patocka 	__clone_and_map_data_bio(ci, ti, ci->sector, &len);
1586e4c93811SAlasdair G Kergon 
1587e4c93811SAlasdair G Kergon 	ci->sector += len;
1588e4c93811SAlasdair G Kergon 	ci->sector_count -= len;
1589e4c93811SAlasdair G Kergon 
1590e4c93811SAlasdair G Kergon 	return 0;
1591e4c93811SAlasdair G Kergon }
1592e4c93811SAlasdair G Kergon 
1593e4c93811SAlasdair G Kergon /*
159414fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
15951da177e4SLinus Torvalds  */
159683d5e5b0SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md,
159783d5e5b0SMikulas Patocka 				    struct dm_table *map, struct bio *bio)
15981da177e4SLinus Torvalds {
15991da177e4SLinus Torvalds 	struct clone_info ci;
1600512875bdSJun'ichi Nomura 	int error = 0;
16011da177e4SLinus Torvalds 
160283d5e5b0SMikulas Patocka 	if (unlikely(!map)) {
1603f0b9a450SMikulas Patocka 		bio_io_error(bio);
1604f0b9a450SMikulas Patocka 		return;
1605f0b9a450SMikulas Patocka 	}
1606692d0eb9SMikulas Patocka 
160783d5e5b0SMikulas Patocka 	ci.map = map;
16081da177e4SLinus Torvalds 	ci.md = md;
16091da177e4SLinus Torvalds 	ci.io = alloc_io(md);
16101da177e4SLinus Torvalds 	ci.io->error = 0;
16111da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
16121da177e4SLinus Torvalds 	ci.io->bio = bio;
16131da177e4SLinus Torvalds 	ci.io->md = md;
1614f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
16154f024f37SKent Overstreet 	ci.sector = bio->bi_iter.bi_sector;
16161da177e4SLinus Torvalds 
16173eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1618bd2a49b8SAlasdair G Kergon 
1619b372d360SMike Snitzer 	if (bio->bi_rw & REQ_FLUSH) {
1620b372d360SMike Snitzer 		ci.bio = &ci.md->flush_bio;
1621b372d360SMike Snitzer 		ci.sector_count = 0;
162214fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1623b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1624b372d360SMike Snitzer 	} else {
16256a8736d1STejun Heo 		ci.bio = bio;
1626f6fccb12SMilan Broz 		ci.sector_count = bio_sectors(bio);
1627512875bdSJun'ichi Nomura 		while (ci.sector_count && !error)
162814fe594dSAlasdair G Kergon 			error = __split_and_process_non_flush(&ci);
1629d87f4c14STejun Heo 	}
16301da177e4SLinus Torvalds 
16311da177e4SLinus Torvalds 	/* drop the extra reference count */
1632512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
16339e4e5f87SMilan Broz }
16349e4e5f87SMilan Broz /*-----------------------------------------------------------------
16351da177e4SLinus Torvalds  * CRUD END
16361da177e4SLinus Torvalds  *---------------------------------------------------------------*/
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds static int dm_merge_bvec(struct request_queue *q,
16391da177e4SLinus Torvalds 			 struct bvec_merge_data *bvm,
1640f6fccb12SMilan Broz 			 struct bio_vec *biovec)
1641f6fccb12SMilan Broz {
1642f6fccb12SMilan Broz 	struct mapped_device *md = q->queuedata;
164383d5e5b0SMikulas Patocka 	struct dm_table *map = dm_get_live_table_fast(md);
1644f6fccb12SMilan Broz 	struct dm_target *ti;
1645f6fccb12SMilan Broz 	sector_t max_sectors;
1646f6fccb12SMilan Broz 	int max_size = 0;
1647f6fccb12SMilan Broz 
1648f6fccb12SMilan Broz 	if (unlikely(!map))
1649f6fccb12SMilan Broz 		goto out;
1650f6fccb12SMilan Broz 
1651f6fccb12SMilan Broz 	ti = dm_table_find_target(map, bvm->bi_sector);
1652f6fccb12SMilan Broz 	if (!dm_target_is_valid(ti))
165383d5e5b0SMikulas Patocka 		goto out;
1654f6fccb12SMilan Broz 
1655f6fccb12SMilan Broz 	/*
1656f6fccb12SMilan Broz 	 * Find maximum amount of I/O that won't need splitting
1657f6fccb12SMilan Broz 	 */
165856a67df7SMike Snitzer 	max_sectors = min(max_io_len(bvm->bi_sector, ti),
1659148e51baSMike Snitzer 			  (sector_t) queue_max_sectors(q));
1660f6fccb12SMilan Broz 	max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1661148e51baSMike Snitzer 	if (unlikely(max_size < 0)) /* this shouldn't _ever_ happen */
1662f6fccb12SMilan Broz 		max_size = 0;
1663f6fccb12SMilan Broz 
1664f6fccb12SMilan Broz 	/*
1665f6fccb12SMilan Broz 	 * merge_bvec_fn() returns number of bytes
1666f6fccb12SMilan Broz 	 * it can accept at this offset
1667f6fccb12SMilan Broz 	 * max is precomputed maximal io size
1668f6fccb12SMilan Broz 	 */
1669f6fccb12SMilan Broz 	if (max_size && ti->type->merge)
1670f6fccb12SMilan Broz 		max_size = ti->type->merge(ti, bvm, biovec, max_size);
16718cbeb67aSMikulas Patocka 	/*
16728cbeb67aSMikulas Patocka 	 * If the target doesn't support merge method and some of the devices
1673148e51baSMike Snitzer 	 * provided their merge_bvec method (we know this by looking for the
1674148e51baSMike Snitzer 	 * max_hw_sectors that dm_set_device_limits may set), then we can't
1675148e51baSMike Snitzer 	 * allow bios with multiple vector entries.  So always set max_size
1676148e51baSMike Snitzer 	 * to 0, and the code below allows just one page.
16778cbeb67aSMikulas Patocka 	 */
16788cbeb67aSMikulas Patocka 	else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
16798cbeb67aSMikulas Patocka 		max_size = 0;
1680f6fccb12SMilan Broz 
16815037108aSMikulas Patocka out:
168283d5e5b0SMikulas Patocka 	dm_put_live_table_fast(md);
1683f6fccb12SMilan Broz 	/*
1684f6fccb12SMilan Broz 	 * Always allow an entire first page
1685f6fccb12SMilan Broz 	 */
1686f6fccb12SMilan Broz 	if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1687f6fccb12SMilan Broz 		max_size = biovec->bv_len;
1688f6fccb12SMilan Broz 
1689f6fccb12SMilan Broz 	return max_size;
1690f6fccb12SMilan Broz }
1691f6fccb12SMilan Broz 
16921da177e4SLinus Torvalds /*
16931da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
16941da177e4SLinus Torvalds  * dm_merge_bvec.
16951da177e4SLinus Torvalds  */
16965a7bbad2SChristoph Hellwig static void _dm_request(struct request_queue *q, struct bio *bio)
16971da177e4SLinus Torvalds {
169812f03a49SKevin Corry 	int rw = bio_data_dir(bio);
16991da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
170083d5e5b0SMikulas Patocka 	int srcu_idx;
170183d5e5b0SMikulas Patocka 	struct dm_table *map;
17021da177e4SLinus Torvalds 
170383d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
17041da177e4SLinus Torvalds 
170518c0b223SGu Zheng 	generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
170612f03a49SKevin Corry 
17076a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
17086a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
170983d5e5b0SMikulas Patocka 		dm_put_live_table(md, srcu_idx);
17101da177e4SLinus Torvalds 
17116a8736d1STejun Heo 		if (bio_rw(bio) != READA)
171292c63902SMikulas Patocka 			queue_io(md, bio);
17136a8736d1STejun Heo 		else
17146a8736d1STejun Heo 			bio_io_error(bio);
17155a7bbad2SChristoph Hellwig 		return;
17161da177e4SLinus Torvalds 	}
17171da177e4SLinus Torvalds 
171883d5e5b0SMikulas Patocka 	__split_and_process_bio(md, map, bio);
171983d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
17205a7bbad2SChristoph Hellwig 	return;
1721cec47e3dSKiyoshi Ueda }
1722cec47e3dSKiyoshi Ueda 
1723fd2ed4d2SMikulas Patocka int dm_request_based(struct mapped_device *md)
1724cec47e3dSKiyoshi Ueda {
1725cec47e3dSKiyoshi Ueda 	return blk_queue_stackable(md->queue);
1726cec47e3dSKiyoshi Ueda }
1727cec47e3dSKiyoshi Ueda 
17285a7bbad2SChristoph Hellwig static void dm_request(struct request_queue *q, struct bio *bio)
1729cec47e3dSKiyoshi Ueda {
1730cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1731cec47e3dSKiyoshi Ueda 
1732cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
17335a7bbad2SChristoph Hellwig 		blk_queue_bio(q, bio);
17345a7bbad2SChristoph Hellwig 	else
17355a7bbad2SChristoph Hellwig 		_dm_request(q, bio);
1736cec47e3dSKiyoshi Ueda }
1737cec47e3dSKiyoshi Ueda 
1738466d89a6SKeith Busch static void dm_dispatch_clone_request(struct request *clone, struct request *rq)
1739cec47e3dSKiyoshi Ueda {
1740cec47e3dSKiyoshi Ueda 	int r;
1741cec47e3dSKiyoshi Ueda 
1742466d89a6SKeith Busch 	if (blk_queue_io_stat(clone->q))
1743466d89a6SKeith Busch 		clone->cmd_flags |= REQ_IO_STAT;
1744cec47e3dSKiyoshi Ueda 
1745466d89a6SKeith Busch 	clone->start_time = jiffies;
1746466d89a6SKeith Busch 	r = blk_insert_cloned_request(clone->q, clone);
1747cec47e3dSKiyoshi Ueda 	if (r)
1748466d89a6SKeith Busch 		/* must complete clone in terms of original request */
1749cec47e3dSKiyoshi Ueda 		dm_complete_request(rq, r);
1750cec47e3dSKiyoshi Ueda }
1751cec47e3dSKiyoshi Ueda 
1752cec47e3dSKiyoshi Ueda static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1753cec47e3dSKiyoshi Ueda 				 void *data)
1754cec47e3dSKiyoshi Ueda {
1755cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = data;
175694818742SKent Overstreet 	struct dm_rq_clone_bio_info *info =
175794818742SKent Overstreet 		container_of(bio, struct dm_rq_clone_bio_info, clone);
1758cec47e3dSKiyoshi Ueda 
1759cec47e3dSKiyoshi Ueda 	info->orig = bio_orig;
1760cec47e3dSKiyoshi Ueda 	info->tio = tio;
1761cec47e3dSKiyoshi Ueda 	bio->bi_end_io = end_clone_bio;
1762cec47e3dSKiyoshi Ueda 
1763cec47e3dSKiyoshi Ueda 	return 0;
1764cec47e3dSKiyoshi Ueda }
1765cec47e3dSKiyoshi Ueda 
1766cec47e3dSKiyoshi Ueda static int setup_clone(struct request *clone, struct request *rq,
17671ae49ea2SMike Snitzer 		       struct dm_rq_target_io *tio, gfp_t gfp_mask)
1768cec47e3dSKiyoshi Ueda {
1769d0bcb878SKiyoshi Ueda 	int r;
1770cec47e3dSKiyoshi Ueda 
17711ae49ea2SMike Snitzer 	r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask,
1772d0bcb878SKiyoshi Ueda 			      dm_rq_bio_constructor, tio);
1773cec47e3dSKiyoshi Ueda 	if (r)
1774cec47e3dSKiyoshi Ueda 		return r;
1775cec47e3dSKiyoshi Ueda 
1776cec47e3dSKiyoshi Ueda 	clone->cmd = rq->cmd;
1777cec47e3dSKiyoshi Ueda 	clone->cmd_len = rq->cmd_len;
1778cec47e3dSKiyoshi Ueda 	clone->sense = rq->sense;
1779cec47e3dSKiyoshi Ueda 	clone->end_io = end_clone_request;
1780cec47e3dSKiyoshi Ueda 	clone->end_io_data = tio;
1781cec47e3dSKiyoshi Ueda 
17821ae49ea2SMike Snitzer 	tio->clone = clone;
17831ae49ea2SMike Snitzer 
1784cec47e3dSKiyoshi Ueda 	return 0;
1785cec47e3dSKiyoshi Ueda }
1786cec47e3dSKiyoshi Ueda 
17876facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md,
17881ae49ea2SMike Snitzer 				struct dm_rq_target_io *tio, gfp_t gfp_mask)
17896facdaffSKiyoshi Ueda {
17901ae49ea2SMike Snitzer 	struct request *clone = alloc_clone_request(md, gfp_mask);
17911ae49ea2SMike Snitzer 
17921ae49ea2SMike Snitzer 	if (!clone)
17931ae49ea2SMike Snitzer 		return NULL;
17941ae49ea2SMike Snitzer 
17951ae49ea2SMike Snitzer 	blk_rq_init(NULL, clone);
17961ae49ea2SMike Snitzer 	if (setup_clone(clone, rq, tio, gfp_mask)) {
17971ae49ea2SMike Snitzer 		/* -ENOMEM */
17981ae49ea2SMike Snitzer 		free_clone_request(md, clone);
17991ae49ea2SMike Snitzer 		return NULL;
18001ae49ea2SMike Snitzer 	}
18011ae49ea2SMike Snitzer 
18021ae49ea2SMike Snitzer 	return clone;
18031ae49ea2SMike Snitzer }
18041ae49ea2SMike Snitzer 
18052eb6e1e3SKeith Busch static void map_tio_request(struct kthread_work *work);
18062eb6e1e3SKeith Busch 
1807466d89a6SKeith Busch static struct dm_rq_target_io *prep_tio(struct request *rq,
1808466d89a6SKeith Busch 					struct mapped_device *md, gfp_t gfp_mask)
18096facdaffSKiyoshi Ueda {
18106facdaffSKiyoshi Ueda 	struct dm_rq_target_io *tio;
1811e5863d9aSMike Snitzer 	int srcu_idx;
1812e5863d9aSMike Snitzer 	struct dm_table *table;
18136facdaffSKiyoshi Ueda 
18146facdaffSKiyoshi Ueda 	tio = alloc_rq_tio(md, gfp_mask);
18156facdaffSKiyoshi Ueda 	if (!tio)
18166facdaffSKiyoshi Ueda 		return NULL;
18176facdaffSKiyoshi Ueda 
18186facdaffSKiyoshi Ueda 	tio->md = md;
18196facdaffSKiyoshi Ueda 	tio->ti = NULL;
18201ae49ea2SMike Snitzer 	tio->clone = NULL;
18216facdaffSKiyoshi Ueda 	tio->orig = rq;
18226facdaffSKiyoshi Ueda 	tio->error = 0;
18236facdaffSKiyoshi Ueda 	memset(&tio->info, 0, sizeof(tio->info));
18242eb6e1e3SKeith Busch 	init_kthread_work(&tio->work, map_tio_request);
18256facdaffSKiyoshi Ueda 
1826e5863d9aSMike Snitzer 	table = dm_get_live_table(md, &srcu_idx);
1827e5863d9aSMike Snitzer 	if (!dm_table_mq_request_based(table)) {
1828466d89a6SKeith Busch 		if (!clone_rq(rq, md, tio, gfp_mask)) {
1829e5863d9aSMike Snitzer 			dm_put_live_table(md, srcu_idx);
18306facdaffSKiyoshi Ueda 			free_rq_tio(tio);
18316facdaffSKiyoshi Ueda 			return NULL;
18326facdaffSKiyoshi Ueda 		}
1833e5863d9aSMike Snitzer 	}
1834e5863d9aSMike Snitzer 	dm_put_live_table(md, srcu_idx);
18356facdaffSKiyoshi Ueda 
1836466d89a6SKeith Busch 	return tio;
18376facdaffSKiyoshi Ueda }
18386facdaffSKiyoshi Ueda 
1839cec47e3dSKiyoshi Ueda /*
1840cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1841cec47e3dSKiyoshi Ueda  */
1842cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq)
1843cec47e3dSKiyoshi Ueda {
1844cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1845466d89a6SKeith Busch 	struct dm_rq_target_io *tio;
1846cec47e3dSKiyoshi Ueda 
1847cec47e3dSKiyoshi Ueda 	if (unlikely(rq->special)) {
1848cec47e3dSKiyoshi Ueda 		DMWARN("Already has something in rq->special.");
1849cec47e3dSKiyoshi Ueda 		return BLKPREP_KILL;
1850cec47e3dSKiyoshi Ueda 	}
1851cec47e3dSKiyoshi Ueda 
1852466d89a6SKeith Busch 	tio = prep_tio(rq, md, GFP_ATOMIC);
1853466d89a6SKeith Busch 	if (!tio)
1854cec47e3dSKiyoshi Ueda 		return BLKPREP_DEFER;
1855cec47e3dSKiyoshi Ueda 
1856466d89a6SKeith Busch 	rq->special = tio;
1857cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_DONTPREP;
1858cec47e3dSKiyoshi Ueda 
1859cec47e3dSKiyoshi Ueda 	return BLKPREP_OK;
1860cec47e3dSKiyoshi Ueda }
1861cec47e3dSKiyoshi Ueda 
18629eef87daSKiyoshi Ueda /*
18639eef87daSKiyoshi Ueda  * Returns:
1864e5863d9aSMike Snitzer  * 0                : the request has been processed
1865e5863d9aSMike Snitzer  * DM_MAPIO_REQUEUE : the original request needs to be requeued
1866e5863d9aSMike Snitzer  * < 0              : the request was completed due to failure
18679eef87daSKiyoshi Ueda  */
1868466d89a6SKeith Busch static int map_request(struct dm_target *ti, struct request *rq,
1869cec47e3dSKiyoshi Ueda 		       struct mapped_device *md)
1870cec47e3dSKiyoshi Ueda {
1871e5863d9aSMike Snitzer 	int r;
1872466d89a6SKeith Busch 	struct dm_rq_target_io *tio = rq->special;
1873e5863d9aSMike Snitzer 	struct request *clone = NULL;
1874cec47e3dSKiyoshi Ueda 
1875e5863d9aSMike Snitzer 	if (tio->clone) {
1876e5863d9aSMike Snitzer 		clone = tio->clone;
1877cec47e3dSKiyoshi Ueda 		r = ti->type->map_rq(ti, clone, &tio->info);
1878e5863d9aSMike Snitzer 	} else {
1879e5863d9aSMike Snitzer 		r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
1880e5863d9aSMike Snitzer 		if (r < 0) {
1881e5863d9aSMike Snitzer 			/* The target wants to complete the I/O */
1882e5863d9aSMike Snitzer 			dm_kill_unmapped_request(rq, r);
1883e5863d9aSMike Snitzer 			return r;
1884e5863d9aSMike Snitzer 		}
1885e5863d9aSMike Snitzer 		if (IS_ERR(clone))
1886e5863d9aSMike Snitzer 			return DM_MAPIO_REQUEUE;
1887e5863d9aSMike Snitzer 		if (setup_clone(clone, rq, tio, GFP_KERNEL)) {
1888e5863d9aSMike Snitzer 			/* -ENOMEM */
1889e5863d9aSMike Snitzer 			ti->type->release_clone_rq(clone);
1890e5863d9aSMike Snitzer 			return DM_MAPIO_REQUEUE;
1891e5863d9aSMike Snitzer 		}
1892e5863d9aSMike Snitzer 	}
1893e5863d9aSMike Snitzer 
1894cec47e3dSKiyoshi Ueda 	switch (r) {
1895cec47e3dSKiyoshi Ueda 	case DM_MAPIO_SUBMITTED:
1896cec47e3dSKiyoshi Ueda 		/* The target has taken the I/O to submit by itself later */
1897cec47e3dSKiyoshi Ueda 		break;
1898cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REMAPPED:
1899cec47e3dSKiyoshi Ueda 		/* The target has remapped the I/O so dispatch it */
19006db4ccd6SJun'ichi Nomura 		trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1901466d89a6SKeith Busch 				     blk_rq_pos(rq));
1902466d89a6SKeith Busch 		dm_dispatch_clone_request(clone, rq);
1903cec47e3dSKiyoshi Ueda 		break;
1904cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REQUEUE:
1905cec47e3dSKiyoshi Ueda 		/* The target wants to requeue the I/O */
1906cec47e3dSKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
1907cec47e3dSKiyoshi Ueda 		break;
1908cec47e3dSKiyoshi Ueda 	default:
1909cec47e3dSKiyoshi Ueda 		if (r > 0) {
1910cec47e3dSKiyoshi Ueda 			DMWARN("unimplemented target map return value: %d", r);
1911cec47e3dSKiyoshi Ueda 			BUG();
1912cec47e3dSKiyoshi Ueda 		}
1913cec47e3dSKiyoshi Ueda 
1914cec47e3dSKiyoshi Ueda 		/* The target wants to complete the I/O */
1915466d89a6SKeith Busch 		dm_kill_unmapped_request(rq, r);
1916e5863d9aSMike Snitzer 		return r;
1917cec47e3dSKiyoshi Ueda 	}
19189eef87daSKiyoshi Ueda 
1919e5863d9aSMike Snitzer 	return 0;
1920cec47e3dSKiyoshi Ueda }
1921cec47e3dSKiyoshi Ueda 
19222eb6e1e3SKeith Busch static void map_tio_request(struct kthread_work *work)
1923ba1cbad9SMike Snitzer {
19242eb6e1e3SKeith Busch 	struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work);
1925e5863d9aSMike Snitzer 	struct request *rq = tio->orig;
1926e5863d9aSMike Snitzer 	struct mapped_device *md = tio->md;
1927ba1cbad9SMike Snitzer 
1928e5863d9aSMike Snitzer 	if (map_request(tio->ti, rq, md) == DM_MAPIO_REQUEUE)
1929e5863d9aSMike Snitzer 		dm_requeue_unmapped_original_request(md, rq);
19302eb6e1e3SKeith Busch }
19312eb6e1e3SKeith Busch 
1932466d89a6SKeith Busch static void dm_start_request(struct mapped_device *md, struct request *orig)
1933ba1cbad9SMike Snitzer {
1934ba1cbad9SMike Snitzer 	blk_start_request(orig);
1935466d89a6SKeith Busch 	atomic_inc(&md->pending[rq_data_dir(orig)]);
1936ba1cbad9SMike Snitzer 
1937ba1cbad9SMike Snitzer 	/*
1938ba1cbad9SMike Snitzer 	 * Hold the md reference here for the in-flight I/O.
1939ba1cbad9SMike Snitzer 	 * We can't rely on the reference count by device opener,
1940ba1cbad9SMike Snitzer 	 * because the device may be closed during the request completion
1941ba1cbad9SMike Snitzer 	 * when all bios are completed.
1942ba1cbad9SMike Snitzer 	 * See the comment in rq_completed() too.
1943ba1cbad9SMike Snitzer 	 */
1944ba1cbad9SMike Snitzer 	dm_get(md);
1945ba1cbad9SMike Snitzer }
1946ba1cbad9SMike Snitzer 
1947cec47e3dSKiyoshi Ueda /*
1948cec47e3dSKiyoshi Ueda  * q->request_fn for request-based dm.
1949cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1950cec47e3dSKiyoshi Ueda  */
1951cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q)
1952cec47e3dSKiyoshi Ueda {
1953cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
195483d5e5b0SMikulas Patocka 	int srcu_idx;
195583d5e5b0SMikulas Patocka 	struct dm_table *map = dm_get_live_table(md, &srcu_idx);
1956cec47e3dSKiyoshi Ueda 	struct dm_target *ti;
1957466d89a6SKeith Busch 	struct request *rq;
19582eb6e1e3SKeith Busch 	struct dm_rq_target_io *tio;
195929e4013dSTejun Heo 	sector_t pos;
1960cec47e3dSKiyoshi Ueda 
1961cec47e3dSKiyoshi Ueda 	/*
1962b4324feeSKiyoshi Ueda 	 * For suspend, check blk_queue_stopped() and increment
1963b4324feeSKiyoshi Ueda 	 * ->pending within a single queue_lock not to increment the
1964b4324feeSKiyoshi Ueda 	 * number of in-flight I/Os after the queue is stopped in
1965b4324feeSKiyoshi Ueda 	 * dm_suspend().
1966cec47e3dSKiyoshi Ueda 	 */
19677eaceaccSJens Axboe 	while (!blk_queue_stopped(q)) {
1968cec47e3dSKiyoshi Ueda 		rq = blk_peek_request(q);
1969cec47e3dSKiyoshi Ueda 		if (!rq)
19707eaceaccSJens Axboe 			goto delay_and_out;
1971cec47e3dSKiyoshi Ueda 
197229e4013dSTejun Heo 		/* always use block 0 to find the target for flushes for now */
197329e4013dSTejun Heo 		pos = 0;
197429e4013dSTejun Heo 		if (!(rq->cmd_flags & REQ_FLUSH))
197529e4013dSTejun Heo 			pos = blk_rq_pos(rq);
1976d0bcb878SKiyoshi Ueda 
197729e4013dSTejun Heo 		ti = dm_table_find_target(map, pos);
1978ba1cbad9SMike Snitzer 		if (!dm_target_is_valid(ti)) {
1979ba1cbad9SMike Snitzer 			/*
1980466d89a6SKeith Busch 			 * Must perform setup, that rq_completed() requires,
1981ba1cbad9SMike Snitzer 			 * before calling dm_kill_unmapped_request
1982ba1cbad9SMike Snitzer 			 */
1983ba1cbad9SMike Snitzer 			DMERR_LIMIT("request attempted access beyond the end of device");
1984466d89a6SKeith Busch 			dm_start_request(md, rq);
1985466d89a6SKeith Busch 			dm_kill_unmapped_request(rq, -EIO);
1986ba1cbad9SMike Snitzer 			continue;
1987ba1cbad9SMike Snitzer 		}
198829e4013dSTejun Heo 
1989cec47e3dSKiyoshi Ueda 		if (ti->type->busy && ti->type->busy(ti))
19907eaceaccSJens Axboe 			goto delay_and_out;
1991cec47e3dSKiyoshi Ueda 
1992466d89a6SKeith Busch 		dm_start_request(md, rq);
1993b4324feeSKiyoshi Ueda 
19942eb6e1e3SKeith Busch 		tio = rq->special;
19952eb6e1e3SKeith Busch 		/* Establish tio->ti before queuing work (map_tio_request) */
19962eb6e1e3SKeith Busch 		tio->ti = ti;
19972eb6e1e3SKeith Busch 		queue_kthread_work(&md->kworker, &tio->work);
1998052189a2SKiyoshi Ueda 		BUG_ON(!irqs_disabled());
1999cec47e3dSKiyoshi Ueda 	}
2000cec47e3dSKiyoshi Ueda 
2001cec47e3dSKiyoshi Ueda 	goto out;
2002cec47e3dSKiyoshi Ueda 
20037eaceaccSJens Axboe delay_and_out:
20047eaceaccSJens Axboe 	blk_delay_queue(q, HZ / 10);
2005cec47e3dSKiyoshi Ueda out:
200683d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
2007cec47e3dSKiyoshi Ueda }
2008cec47e3dSKiyoshi Ueda 
2009cec47e3dSKiyoshi Ueda int dm_underlying_device_busy(struct request_queue *q)
2010cec47e3dSKiyoshi Ueda {
2011cec47e3dSKiyoshi Ueda 	return blk_lld_busy(q);
2012cec47e3dSKiyoshi Ueda }
2013cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
2014cec47e3dSKiyoshi Ueda 
2015cec47e3dSKiyoshi Ueda static int dm_lld_busy(struct request_queue *q)
2016cec47e3dSKiyoshi Ueda {
2017cec47e3dSKiyoshi Ueda 	int r;
2018cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
201983d5e5b0SMikulas Patocka 	struct dm_table *map = dm_get_live_table_fast(md);
2020cec47e3dSKiyoshi Ueda 
2021cec47e3dSKiyoshi Ueda 	if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
2022cec47e3dSKiyoshi Ueda 		r = 1;
2023cec47e3dSKiyoshi Ueda 	else
2024cec47e3dSKiyoshi Ueda 		r = dm_table_any_busy_target(map);
2025cec47e3dSKiyoshi Ueda 
202683d5e5b0SMikulas Patocka 	dm_put_live_table_fast(md);
2027cec47e3dSKiyoshi Ueda 
2028cec47e3dSKiyoshi Ueda 	return r;
2029cec47e3dSKiyoshi Ueda }
2030cec47e3dSKiyoshi Ueda 
20311da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
20321da177e4SLinus Torvalds {
20338a57dfc6SChandra Seetharaman 	int r = bdi_bits;
20348a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
20358a57dfc6SChandra Seetharaman 	struct dm_table *map;
20361da177e4SLinus Torvalds 
20371eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
203883d5e5b0SMikulas Patocka 		map = dm_get_live_table_fast(md);
20398a57dfc6SChandra Seetharaman 		if (map) {
2040cec47e3dSKiyoshi Ueda 			/*
2041cec47e3dSKiyoshi Ueda 			 * Request-based dm cares about only own queue for
2042cec47e3dSKiyoshi Ueda 			 * the query about congestion status of request_queue
2043cec47e3dSKiyoshi Ueda 			 */
2044cec47e3dSKiyoshi Ueda 			if (dm_request_based(md))
2045cec47e3dSKiyoshi Ueda 				r = md->queue->backing_dev_info.state &
2046cec47e3dSKiyoshi Ueda 				    bdi_bits;
2047cec47e3dSKiyoshi Ueda 			else
20481da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
20498a57dfc6SChandra Seetharaman 		}
205083d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
20518a57dfc6SChandra Seetharaman 	}
20528a57dfc6SChandra Seetharaman 
20531da177e4SLinus Torvalds 	return r;
20541da177e4SLinus Torvalds }
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds /*-----------------------------------------------------------------
20571da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
20581da177e4SLinus Torvalds  *---------------------------------------------------------------*/
20592b06cfffSAlasdair G Kergon static void free_minor(int minor)
20601da177e4SLinus Torvalds {
2061f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
20621da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
2063f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
20641da177e4SLinus Torvalds }
20651da177e4SLinus Torvalds 
20661da177e4SLinus Torvalds /*
20671da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
20681da177e4SLinus Torvalds  */
2069cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
20701da177e4SLinus Torvalds {
2071c9d76be6STejun Heo 	int r;
20721da177e4SLinus Torvalds 
20731da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
20741da177e4SLinus Torvalds 		return -EINVAL;
20751da177e4SLinus Torvalds 
2076c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
2077f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
20781da177e4SLinus Torvalds 
2079c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
20801da177e4SLinus Torvalds 
2081f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2082c9d76be6STejun Heo 	idr_preload_end();
2083c9d76be6STejun Heo 	if (r < 0)
2084c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
2085c9d76be6STejun Heo 	return 0;
20861da177e4SLinus Torvalds }
20871da177e4SLinus Torvalds 
2088cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
20891da177e4SLinus Torvalds {
2090c9d76be6STejun Heo 	int r;
20911da177e4SLinus Torvalds 
2092c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
2093f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
20941da177e4SLinus Torvalds 
2095c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
20961da177e4SLinus Torvalds 
2097f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2098c9d76be6STejun Heo 	idr_preload_end();
2099c9d76be6STejun Heo 	if (r < 0)
21001da177e4SLinus Torvalds 		return r;
2101c9d76be6STejun Heo 	*minor = r;
2102c9d76be6STejun Heo 	return 0;
21031da177e4SLinus Torvalds }
21041da177e4SLinus Torvalds 
210583d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
21061da177e4SLinus Torvalds 
210753d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
210853d5914fSMikulas Patocka 
21094a0b4ddfSMike Snitzer static void dm_init_md_queue(struct mapped_device *md)
21104a0b4ddfSMike Snitzer {
21114a0b4ddfSMike Snitzer 	/*
21124a0b4ddfSMike Snitzer 	 * Request-based dm devices cannot be stacked on top of bio-based dm
21134a0b4ddfSMike Snitzer 	 * devices.  The type of this dm device has not been decided yet.
21144a0b4ddfSMike Snitzer 	 * The type is decided at the first table loading time.
21154a0b4ddfSMike Snitzer 	 * To prevent problematic device stacking, clear the queue flag
21164a0b4ddfSMike Snitzer 	 * for request stacking support until then.
21174a0b4ddfSMike Snitzer 	 *
21184a0b4ddfSMike Snitzer 	 * This queue is new, so no concurrency on the queue_flags.
21194a0b4ddfSMike Snitzer 	 */
21204a0b4ddfSMike Snitzer 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
21214a0b4ddfSMike Snitzer 
21224a0b4ddfSMike Snitzer 	md->queue->queuedata = md;
21234a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
21244a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_data = md;
21254a0b4ddfSMike Snitzer 	blk_queue_make_request(md->queue, dm_request);
21264a0b4ddfSMike Snitzer 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
21274a0b4ddfSMike Snitzer 	blk_queue_merge_bvec(md->queue, dm_merge_bvec);
21284a0b4ddfSMike Snitzer }
21294a0b4ddfSMike Snitzer 
21301da177e4SLinus Torvalds /*
21311da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
21321da177e4SLinus Torvalds  */
21332b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
21341da177e4SLinus Torvalds {
21351da177e4SLinus Torvalds 	int r;
2136cf13ab8eSFrederik Deweerdt 	struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
2137ba61fdd1SJeff Mahoney 	void *old_md;
21381da177e4SLinus Torvalds 
21391da177e4SLinus Torvalds 	if (!md) {
21401da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
21411da177e4SLinus Torvalds 		return NULL;
21421da177e4SLinus Torvalds 	}
21431da177e4SLinus Torvalds 
214410da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
21456ed7ade8SMilan Broz 		goto bad_module_get;
214610da4f79SJeff Mahoney 
21471da177e4SLinus Torvalds 	/* get a minor number for the dev */
21482b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
2149cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
21502b06cfffSAlasdair G Kergon 	else
2151cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
21521da177e4SLinus Torvalds 	if (r < 0)
21536ed7ade8SMilan Broz 		goto bad_minor;
21541da177e4SLinus Torvalds 
215583d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
215683d5e5b0SMikulas Patocka 	if (r < 0)
215783d5e5b0SMikulas Patocka 		goto bad_io_barrier;
215883d5e5b0SMikulas Patocka 
2159a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
2160e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
2161a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
216286f1152bSBenjamin Marzinski 	mutex_init(&md->table_devices_lock);
2163022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
21641da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
21655c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
21661da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
21677a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
21687a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
216986f1152bSBenjamin Marzinski 	INIT_LIST_HEAD(&md->table_devices);
21707a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
21711da177e4SLinus Torvalds 
21724a0b4ddfSMike Snitzer 	md->queue = blk_alloc_queue(GFP_KERNEL);
21731da177e4SLinus Torvalds 	if (!md->queue)
21746ed7ade8SMilan Broz 		goto bad_queue;
21751da177e4SLinus Torvalds 
21764a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
21779faf400fSStefan Bader 
21781da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
21791da177e4SLinus Torvalds 	if (!md->disk)
21806ed7ade8SMilan Broz 		goto bad_disk;
21811da177e4SLinus Torvalds 
2182316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
2183316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
2184f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
218553d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
2186f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
21872995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
21882eb6e1e3SKeith Busch 	md->kworker_task = NULL;
2189f0b04115SJeff Mahoney 
21901da177e4SLinus Torvalds 	md->disk->major = _major;
21911da177e4SLinus Torvalds 	md->disk->first_minor = minor;
21921da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
21931da177e4SLinus Torvalds 	md->disk->queue = md->queue;
21941da177e4SLinus Torvalds 	md->disk->private_data = md;
21951da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
21961da177e4SLinus Torvalds 	add_disk(md->disk);
21977e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
21981da177e4SLinus Torvalds 
2199670368a8STejun Heo 	md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
2200304f3f6aSMilan Broz 	if (!md->wq)
2201304f3f6aSMilan Broz 		goto bad_thread;
2202304f3f6aSMilan Broz 
220332a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
220432a926daSMikulas Patocka 	if (!md->bdev)
220532a926daSMikulas Patocka 		goto bad_bdev;
220632a926daSMikulas Patocka 
22076a8736d1STejun Heo 	bio_init(&md->flush_bio);
22086a8736d1STejun Heo 	md->flush_bio.bi_bdev = md->bdev;
22096a8736d1STejun Heo 	md->flush_bio.bi_rw = WRITE_FLUSH;
22106a8736d1STejun Heo 
2211fd2ed4d2SMikulas Patocka 	dm_stats_init(&md->stats);
2212fd2ed4d2SMikulas Patocka 
2213ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
2214f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
2215ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
2216f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2217ba61fdd1SJeff Mahoney 
2218ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
2219ba61fdd1SJeff Mahoney 
22201da177e4SLinus Torvalds 	return md;
22211da177e4SLinus Torvalds 
222232a926daSMikulas Patocka bad_bdev:
222332a926daSMikulas Patocka 	destroy_workqueue(md->wq);
2224304f3f6aSMilan Broz bad_thread:
222503022c54SZdenek Kabelac 	del_gendisk(md->disk);
2226304f3f6aSMilan Broz 	put_disk(md->disk);
22276ed7ade8SMilan Broz bad_disk:
22281312f40eSAl Viro 	blk_cleanup_queue(md->queue);
22296ed7ade8SMilan Broz bad_queue:
223083d5e5b0SMikulas Patocka 	cleanup_srcu_struct(&md->io_barrier);
223183d5e5b0SMikulas Patocka bad_io_barrier:
22321da177e4SLinus Torvalds 	free_minor(minor);
22336ed7ade8SMilan Broz bad_minor:
223410da4f79SJeff Mahoney 	module_put(THIS_MODULE);
22356ed7ade8SMilan Broz bad_module_get:
22361da177e4SLinus Torvalds 	kfree(md);
22371da177e4SLinus Torvalds 	return NULL;
22381da177e4SLinus Torvalds }
22391da177e4SLinus Torvalds 
2240ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
2241ae9da83fSJun'ichi Nomura 
22421da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
22431da177e4SLinus Torvalds {
2244f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
224563d94e48SJun'ichi Nomura 
2246ae9da83fSJun'ichi Nomura 	unlock_fs(md);
2247304f3f6aSMilan Broz 	destroy_workqueue(md->wq);
22482eb6e1e3SKeith Busch 
22492eb6e1e3SKeith Busch 	if (md->kworker_task)
22502eb6e1e3SKeith Busch 		kthread_stop(md->kworker_task);
2251e6ee8c0bSKiyoshi Ueda 	if (md->io_pool)
22521da177e4SLinus Torvalds 		mempool_destroy(md->io_pool);
22531ae49ea2SMike Snitzer 	if (md->rq_pool)
22541ae49ea2SMike Snitzer 		mempool_destroy(md->rq_pool);
2255e6ee8c0bSKiyoshi Ueda 	if (md->bs)
22569faf400fSStefan Bader 		bioset_free(md->bs);
225763a4f065SMike Snitzer 
225883d5e5b0SMikulas Patocka 	cleanup_srcu_struct(&md->io_barrier);
225986f1152bSBenjamin Marzinski 	free_table_devices(&md->table_devices);
226063a4f065SMike Snitzer 	dm_stats_cleanup(&md->stats);
2261fba9f90eSJeff Mahoney 
2262fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
2263fba9f90eSJeff Mahoney 	md->disk->private_data = NULL;
2264fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
226563a4f065SMike Snitzer 	if (blk_get_integrity(md->disk))
226663a4f065SMike Snitzer 		blk_integrity_unregister(md->disk);
226763a4f065SMike Snitzer 	del_gendisk(md->disk);
22681da177e4SLinus Torvalds 	put_disk(md->disk);
22691312f40eSAl Viro 	blk_cleanup_queue(md->queue);
227063a4f065SMike Snitzer 	bdput(md->bdev);
227163a4f065SMike Snitzer 	free_minor(minor);
227263a4f065SMike Snitzer 
227310da4f79SJeff Mahoney 	module_put(THIS_MODULE);
22741da177e4SLinus Torvalds 	kfree(md);
22751da177e4SLinus Torvalds }
22761da177e4SLinus Torvalds 
2277e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2278e6ee8c0bSKiyoshi Ueda {
2279c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
2280e6ee8c0bSKiyoshi Ueda 
22815f015204SJun'ichi Nomura 	if (md->io_pool && md->bs) {
228216245bdcSJun'ichi Nomura 		/* The md already has necessary mempools. */
228316245bdcSJun'ichi Nomura 		if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
2284c0820cf5SMikulas Patocka 			/*
228516245bdcSJun'ichi Nomura 			 * Reload bioset because front_pad may have changed
228616245bdcSJun'ichi Nomura 			 * because a different table was loaded.
2287c0820cf5SMikulas Patocka 			 */
2288c0820cf5SMikulas Patocka 			bioset_free(md->bs);
2289c0820cf5SMikulas Patocka 			md->bs = p->bs;
2290c0820cf5SMikulas Patocka 			p->bs = NULL;
2291466d89a6SKeith Busch 		}
229216245bdcSJun'ichi Nomura 		/*
229316245bdcSJun'ichi Nomura 		 * There's no need to reload with request-based dm
229416245bdcSJun'ichi Nomura 		 * because the size of front_pad doesn't change.
229516245bdcSJun'ichi Nomura 		 * Note for future: If you are to reload bioset,
229616245bdcSJun'ichi Nomura 		 * prep-ed requests in the queue may refer
229716245bdcSJun'ichi Nomura 		 * to bio from the old bioset, so you must walk
229816245bdcSJun'ichi Nomura 		 * through the queue to unprep.
229916245bdcSJun'ichi Nomura 		 */
2300e6ee8c0bSKiyoshi Ueda 		goto out;
2301c0820cf5SMikulas Patocka 	}
2302e6ee8c0bSKiyoshi Ueda 
23031ae49ea2SMike Snitzer 	BUG_ON(!p || md->io_pool || md->rq_pool || md->bs);
2304e6ee8c0bSKiyoshi Ueda 
2305e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
2306e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
23071ae49ea2SMike Snitzer 	md->rq_pool = p->rq_pool;
23081ae49ea2SMike Snitzer 	p->rq_pool = NULL;
2309e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
2310e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
2311e6ee8c0bSKiyoshi Ueda 
2312e6ee8c0bSKiyoshi Ueda out:
2313e6ee8c0bSKiyoshi Ueda 	/* mempool bind completed, now no need any mempools in the table */
2314e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
2315e6ee8c0bSKiyoshi Ueda }
2316e6ee8c0bSKiyoshi Ueda 
23171da177e4SLinus Torvalds /*
23181da177e4SLinus Torvalds  * Bind a table to the device.
23191da177e4SLinus Torvalds  */
23201da177e4SLinus Torvalds static void event_callback(void *context)
23211da177e4SLinus Torvalds {
23227a8c3d3bSMike Anderson 	unsigned long flags;
23237a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
23241da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
23251da177e4SLinus Torvalds 
23267a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
23277a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
23287a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
23297a8c3d3bSMike Anderson 
2330ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
23317a8c3d3bSMike Anderson 
23321da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
23331da177e4SLinus Torvalds 	wake_up(&md->eventq);
23341da177e4SLinus Torvalds }
23351da177e4SLinus Torvalds 
2336c217649bSMike Snitzer /*
2337c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
2338c217649bSMike Snitzer  */
23394e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
23401da177e4SLinus Torvalds {
23414e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
23421da177e4SLinus Torvalds 
2343db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
23441da177e4SLinus Torvalds }
23451da177e4SLinus Torvalds 
2346042d2a9bSAlasdair G Kergon /*
2347d5b9dd04SMikulas Patocka  * Return 1 if the queue has a compulsory merge_bvec_fn function.
2348d5b9dd04SMikulas Patocka  *
2349d5b9dd04SMikulas Patocka  * If this function returns 0, then the device is either a non-dm
2350d5b9dd04SMikulas Patocka  * device without a merge_bvec_fn, or it is a dm device that is
2351d5b9dd04SMikulas Patocka  * able to split any bios it receives that are too big.
2352d5b9dd04SMikulas Patocka  */
2353d5b9dd04SMikulas Patocka int dm_queue_merge_is_compulsory(struct request_queue *q)
2354d5b9dd04SMikulas Patocka {
2355d5b9dd04SMikulas Patocka 	struct mapped_device *dev_md;
2356d5b9dd04SMikulas Patocka 
2357d5b9dd04SMikulas Patocka 	if (!q->merge_bvec_fn)
2358d5b9dd04SMikulas Patocka 		return 0;
2359d5b9dd04SMikulas Patocka 
2360d5b9dd04SMikulas Patocka 	if (q->make_request_fn == dm_request) {
2361d5b9dd04SMikulas Patocka 		dev_md = q->queuedata;
2362d5b9dd04SMikulas Patocka 		if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2363d5b9dd04SMikulas Patocka 			return 0;
2364d5b9dd04SMikulas Patocka 	}
2365d5b9dd04SMikulas Patocka 
2366d5b9dd04SMikulas Patocka 	return 1;
2367d5b9dd04SMikulas Patocka }
2368d5b9dd04SMikulas Patocka 
2369d5b9dd04SMikulas Patocka static int dm_device_merge_is_compulsory(struct dm_target *ti,
2370d5b9dd04SMikulas Patocka 					 struct dm_dev *dev, sector_t start,
2371d5b9dd04SMikulas Patocka 					 sector_t len, void *data)
2372d5b9dd04SMikulas Patocka {
2373d5b9dd04SMikulas Patocka 	struct block_device *bdev = dev->bdev;
2374d5b9dd04SMikulas Patocka 	struct request_queue *q = bdev_get_queue(bdev);
2375d5b9dd04SMikulas Patocka 
2376d5b9dd04SMikulas Patocka 	return dm_queue_merge_is_compulsory(q);
2377d5b9dd04SMikulas Patocka }
2378d5b9dd04SMikulas Patocka 
2379d5b9dd04SMikulas Patocka /*
2380d5b9dd04SMikulas Patocka  * Return 1 if it is acceptable to ignore merge_bvec_fn based
2381d5b9dd04SMikulas Patocka  * on the properties of the underlying devices.
2382d5b9dd04SMikulas Patocka  */
2383d5b9dd04SMikulas Patocka static int dm_table_merge_is_optional(struct dm_table *table)
2384d5b9dd04SMikulas Patocka {
2385d5b9dd04SMikulas Patocka 	unsigned i = 0;
2386d5b9dd04SMikulas Patocka 	struct dm_target *ti;
2387d5b9dd04SMikulas Patocka 
2388d5b9dd04SMikulas Patocka 	while (i < dm_table_get_num_targets(table)) {
2389d5b9dd04SMikulas Patocka 		ti = dm_table_get_target(table, i++);
2390d5b9dd04SMikulas Patocka 
2391d5b9dd04SMikulas Patocka 		if (ti->type->iterate_devices &&
2392d5b9dd04SMikulas Patocka 		    ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2393d5b9dd04SMikulas Patocka 			return 0;
2394d5b9dd04SMikulas Patocka 	}
2395d5b9dd04SMikulas Patocka 
2396d5b9dd04SMikulas Patocka 	return 1;
2397d5b9dd04SMikulas Patocka }
2398d5b9dd04SMikulas Patocka 
2399d5b9dd04SMikulas Patocka /*
2400042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2401042d2a9bSAlasdair G Kergon  */
2402042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2403754c5fc7SMike Snitzer 			       struct queue_limits *limits)
24041da177e4SLinus Torvalds {
2405042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
2406165125e1SJens Axboe 	struct request_queue *q = md->queue;
24071da177e4SLinus Torvalds 	sector_t size;
2408d5b9dd04SMikulas Patocka 	int merge_is_optional;
24091da177e4SLinus Torvalds 
24101da177e4SLinus Torvalds 	size = dm_table_get_size(t);
24113ac51e74SDarrick J. Wong 
24123ac51e74SDarrick J. Wong 	/*
24133ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
24143ac51e74SDarrick J. Wong 	 */
2415fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
24163ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
24173ac51e74SDarrick J. Wong 
24184e90188bSAlasdair G Kergon 	__set_size(md, size);
24191da177e4SLinus Torvalds 
2420cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
24212ca3310eSAlasdair G Kergon 
2422e6ee8c0bSKiyoshi Ueda 	/*
2423e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
2424e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
2425e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
2426e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
2427e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
2428e6ee8c0bSKiyoshi Ueda 	 */
2429e6ee8c0bSKiyoshi Ueda 	if (dm_table_request_based(t) && !blk_queue_stopped(q))
2430e6ee8c0bSKiyoshi Ueda 		stop_queue(q);
2431e6ee8c0bSKiyoshi Ueda 
2432e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
2433e6ee8c0bSKiyoshi Ueda 
2434d5b9dd04SMikulas Patocka 	merge_is_optional = dm_table_merge_is_optional(t);
2435d5b9dd04SMikulas Patocka 
2436a12f5d48SEric Dumazet 	old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
243783d5e5b0SMikulas Patocka 	rcu_assign_pointer(md->map, t);
243836a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
243936a0456fSAlasdair G Kergon 
2440754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
2441d5b9dd04SMikulas Patocka 	if (merge_is_optional)
2442d5b9dd04SMikulas Patocka 		set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2443d5b9dd04SMikulas Patocka 	else
2444d5b9dd04SMikulas Patocka 		clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
244541abc4e1SHannes Reinecke 	if (old_map)
244683d5e5b0SMikulas Patocka 		dm_sync_table(md);
24472ca3310eSAlasdair G Kergon 
2448042d2a9bSAlasdair G Kergon 	return old_map;
24491da177e4SLinus Torvalds }
24501da177e4SLinus Torvalds 
2451a7940155SAlasdair G Kergon /*
2452a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2453a7940155SAlasdair G Kergon  */
2454a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
24551da177e4SLinus Torvalds {
2456a12f5d48SEric Dumazet 	struct dm_table *map = rcu_dereference_protected(md->map, 1);
24571da177e4SLinus Torvalds 
24581da177e4SLinus Torvalds 	if (!map)
2459a7940155SAlasdair G Kergon 		return NULL;
24601da177e4SLinus Torvalds 
24611da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
24629cdb8520SMonam Agarwal 	RCU_INIT_POINTER(md->map, NULL);
246383d5e5b0SMikulas Patocka 	dm_sync_table(md);
2464a7940155SAlasdair G Kergon 
2465a7940155SAlasdair G Kergon 	return map;
24661da177e4SLinus Torvalds }
24671da177e4SLinus Torvalds 
24681da177e4SLinus Torvalds /*
24691da177e4SLinus Torvalds  * Constructor for a new device.
24701da177e4SLinus Torvalds  */
24712b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
24721da177e4SLinus Torvalds {
24731da177e4SLinus Torvalds 	struct mapped_device *md;
24741da177e4SLinus Torvalds 
24752b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
24761da177e4SLinus Torvalds 	if (!md)
24771da177e4SLinus Torvalds 		return -ENXIO;
24781da177e4SLinus Torvalds 
2479784aae73SMilan Broz 	dm_sysfs_init(md);
2480784aae73SMilan Broz 
24811da177e4SLinus Torvalds 	*result = md;
24821da177e4SLinus Torvalds 	return 0;
24831da177e4SLinus Torvalds }
24841da177e4SLinus Torvalds 
2485a5664dadSMike Snitzer /*
2486a5664dadSMike Snitzer  * Functions to manage md->type.
2487a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2488a5664dadSMike Snitzer  */
2489a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2490a5664dadSMike Snitzer {
2491a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2492a5664dadSMike Snitzer }
2493a5664dadSMike Snitzer 
2494a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2495a5664dadSMike Snitzer {
2496a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2497a5664dadSMike Snitzer }
2498a5664dadSMike Snitzer 
2499a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type)
2500a5664dadSMike Snitzer {
250100c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2502a5664dadSMike Snitzer 	md->type = type;
2503a5664dadSMike Snitzer }
2504a5664dadSMike Snitzer 
2505a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md)
2506a5664dadSMike Snitzer {
250700c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2508a5664dadSMike Snitzer 	return md->type;
2509a5664dadSMike Snitzer }
2510a5664dadSMike Snitzer 
2511e5863d9aSMike Snitzer static bool dm_md_type_request_based(struct mapped_device *md)
2512e5863d9aSMike Snitzer {
2513e5863d9aSMike Snitzer 	unsigned table_type = dm_get_md_type(md);
2514e5863d9aSMike Snitzer 
2515e5863d9aSMike Snitzer 	return (table_type == DM_TYPE_REQUEST_BASED ||
2516e5863d9aSMike Snitzer 		table_type == DM_TYPE_MQ_REQUEST_BASED);
2517e5863d9aSMike Snitzer }
2518e5863d9aSMike Snitzer 
251936a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
252036a0456fSAlasdair G Kergon {
252136a0456fSAlasdair G Kergon 	return md->immutable_target_type;
252236a0456fSAlasdair G Kergon }
252336a0456fSAlasdair G Kergon 
25244a0b4ddfSMike Snitzer /*
2525f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
2526f84cb8a4SMike Snitzer  * count on 'md'.
2527f84cb8a4SMike Snitzer  */
2528f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2529f84cb8a4SMike Snitzer {
2530f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
2531f84cb8a4SMike Snitzer 	return &md->queue->limits;
2532f84cb8a4SMike Snitzer }
2533f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2534f84cb8a4SMike Snitzer 
2535f84cb8a4SMike Snitzer /*
25364a0b4ddfSMike Snitzer  * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
25374a0b4ddfSMike Snitzer  */
25384a0b4ddfSMike Snitzer static int dm_init_request_based_queue(struct mapped_device *md)
25394a0b4ddfSMike Snitzer {
25404a0b4ddfSMike Snitzer 	struct request_queue *q = NULL;
25414a0b4ddfSMike Snitzer 
25424a0b4ddfSMike Snitzer 	if (md->queue->elevator)
25434a0b4ddfSMike Snitzer 		return 1;
25444a0b4ddfSMike Snitzer 
25454a0b4ddfSMike Snitzer 	/* Fully initialize the queue */
25464a0b4ddfSMike Snitzer 	q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
25474a0b4ddfSMike Snitzer 	if (!q)
25484a0b4ddfSMike Snitzer 		return 0;
25494a0b4ddfSMike Snitzer 
25504a0b4ddfSMike Snitzer 	md->queue = q;
25514a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
25524a0b4ddfSMike Snitzer 	blk_queue_softirq_done(md->queue, dm_softirq_done);
25534a0b4ddfSMike Snitzer 	blk_queue_prep_rq(md->queue, dm_prep_fn);
25544a0b4ddfSMike Snitzer 	blk_queue_lld_busy(md->queue, dm_lld_busy);
25554a0b4ddfSMike Snitzer 
25562eb6e1e3SKeith Busch 	/* Also initialize the request-based DM worker thread */
25572eb6e1e3SKeith Busch 	init_kthread_worker(&md->kworker);
25582eb6e1e3SKeith Busch 	md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker,
25592eb6e1e3SKeith Busch 				       "kdmwork-%s", dm_device_name(md));
25602eb6e1e3SKeith Busch 
25614a0b4ddfSMike Snitzer 	elv_register_queue(md->queue);
25624a0b4ddfSMike Snitzer 
25634a0b4ddfSMike Snitzer 	return 1;
25644a0b4ddfSMike Snitzer }
25654a0b4ddfSMike Snitzer 
25664a0b4ddfSMike Snitzer /*
25674a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
25684a0b4ddfSMike Snitzer  */
25694a0b4ddfSMike Snitzer int dm_setup_md_queue(struct mapped_device *md)
25704a0b4ddfSMike Snitzer {
2571e5863d9aSMike Snitzer 	if (dm_md_type_request_based(md) && !dm_init_request_based_queue(md)) {
25724a0b4ddfSMike Snitzer 		DMWARN("Cannot initialize queue for request-based mapped device");
25734a0b4ddfSMike Snitzer 		return -EINVAL;
25744a0b4ddfSMike Snitzer 	}
25754a0b4ddfSMike Snitzer 
25764a0b4ddfSMike Snitzer 	return 0;
25774a0b4ddfSMike Snitzer }
25784a0b4ddfSMike Snitzer 
25792bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev)
25801da177e4SLinus Torvalds {
25811da177e4SLinus Torvalds 	struct mapped_device *md;
25821da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
25831da177e4SLinus Torvalds 
25841da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
25851da177e4SLinus Torvalds 		return NULL;
25861da177e4SLinus Torvalds 
2587f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
25881da177e4SLinus Torvalds 
25891da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
25902bec1f4aSMikulas Patocka 	if (md) {
25912bec1f4aSMikulas Patocka 		if ((md == MINOR_ALLOCED ||
2592f331c029STejun Heo 		     (MINOR(disk_devt(dm_disk(md))) != minor) ||
2593abdc568bSKiyoshi Ueda 		     dm_deleting_md(md) ||
2594fba9f90eSJeff Mahoney 		     test_bit(DMF_FREEING, &md->flags))) {
2595637842cfSDavid Teigland 			md = NULL;
2596fba9f90eSJeff Mahoney 			goto out;
2597fba9f90eSJeff Mahoney 		}
25982bec1f4aSMikulas Patocka 		dm_get(md);
25992bec1f4aSMikulas Patocka 	}
26001da177e4SLinus Torvalds 
2601fba9f90eSJeff Mahoney out:
2602f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
26031da177e4SLinus Torvalds 
2604637842cfSDavid Teigland 	return md;
2605637842cfSDavid Teigland }
26063cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2607d229a958SDavid Teigland 
26089ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2609637842cfSDavid Teigland {
26109ade92a9SAlasdair G Kergon 	return md->interface_ptr;
26111da177e4SLinus Torvalds }
26121da177e4SLinus Torvalds 
26131da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
26141da177e4SLinus Torvalds {
26151da177e4SLinus Torvalds 	md->interface_ptr = ptr;
26161da177e4SLinus Torvalds }
26171da177e4SLinus Torvalds 
26181da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
26191da177e4SLinus Torvalds {
26201da177e4SLinus Torvalds 	atomic_inc(&md->holders);
26213f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
26221da177e4SLinus Torvalds }
26231da177e4SLinus Torvalds 
262409ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md)
262509ee96b2SMikulas Patocka {
262609ee96b2SMikulas Patocka 	spin_lock(&_minor_lock);
262709ee96b2SMikulas Patocka 	if (test_bit(DMF_FREEING, &md->flags)) {
262809ee96b2SMikulas Patocka 		spin_unlock(&_minor_lock);
262909ee96b2SMikulas Patocka 		return -EBUSY;
263009ee96b2SMikulas Patocka 	}
263109ee96b2SMikulas Patocka 	dm_get(md);
263209ee96b2SMikulas Patocka 	spin_unlock(&_minor_lock);
263309ee96b2SMikulas Patocka 	return 0;
263409ee96b2SMikulas Patocka }
263509ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold);
263609ee96b2SMikulas Patocka 
263772d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
263872d94861SAlasdair G Kergon {
263972d94861SAlasdair G Kergon 	return md->name;
264072d94861SAlasdair G Kergon }
264172d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
264272d94861SAlasdair G Kergon 
26433f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
26441da177e4SLinus Torvalds {
26451134e5aeSMike Anderson 	struct dm_table *map;
264683d5e5b0SMikulas Patocka 	int srcu_idx;
26471da177e4SLinus Torvalds 
26483f77316dSKiyoshi Ueda 	might_sleep();
2649fba9f90eSJeff Mahoney 
265083d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
265163a4f065SMike Snitzer 
265263a4f065SMike Snitzer 	spin_lock(&_minor_lock);
26533f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2654fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2655f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
26563f77316dSKiyoshi Ueda 
26572eb6e1e3SKeith Busch 	if (dm_request_based(md))
26582eb6e1e3SKeith Busch 		flush_kthread_worker(&md->kworker);
26592eb6e1e3SKeith Busch 
2660ab7c7bb6SMikulas Patocka 	/*
2661ab7c7bb6SMikulas Patocka 	 * Take suspend_lock so that presuspend and postsuspend methods
2662ab7c7bb6SMikulas Patocka 	 * do not race with internal suspend.
2663ab7c7bb6SMikulas Patocka 	 */
2664ab7c7bb6SMikulas Patocka 	mutex_lock(&md->suspend_lock);
26654f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
26661da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
26671da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
26681da177e4SLinus Torvalds 	}
2669ab7c7bb6SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
26703f77316dSKiyoshi Ueda 
267183d5e5b0SMikulas Patocka 	/* dm_put_live_table must be before msleep, otherwise deadlock is possible */
267283d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
267383d5e5b0SMikulas Patocka 
26743f77316dSKiyoshi Ueda 	/*
26753f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
26763f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
26773f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
26783f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
26793f77316dSKiyoshi Ueda 	 */
26803f77316dSKiyoshi Ueda 	if (wait)
26813f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
26823f77316dSKiyoshi Ueda 			msleep(1);
26833f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
26843f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
26853f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
26863f77316dSKiyoshi Ueda 
2687784aae73SMilan Broz 	dm_sysfs_exit(md);
2688a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
26891da177e4SLinus Torvalds 	free_dev(md);
26901da177e4SLinus Torvalds }
26913f77316dSKiyoshi Ueda 
26923f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
26933f77316dSKiyoshi Ueda {
26943f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
26953f77316dSKiyoshi Ueda }
26963f77316dSKiyoshi Ueda 
26973f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
26983f77316dSKiyoshi Ueda {
26993f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
27003f77316dSKiyoshi Ueda }
27013f77316dSKiyoshi Ueda 
27023f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
27033f77316dSKiyoshi Ueda {
27043f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
27051da177e4SLinus Torvalds }
270679eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
27071da177e4SLinus Torvalds 
2708401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
270946125c1cSMilan Broz {
271046125c1cSMilan Broz 	int r = 0;
2711b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
2712b44ebeb0SMikulas Patocka 
2713b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
271446125c1cSMilan Broz 
271546125c1cSMilan Broz 	while (1) {
2716401600dfSMikulas Patocka 		set_current_state(interruptible);
271746125c1cSMilan Broz 
2718b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
271946125c1cSMilan Broz 			break;
272046125c1cSMilan Broz 
2721401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
2722401600dfSMikulas Patocka 		    signal_pending(current)) {
272346125c1cSMilan Broz 			r = -EINTR;
272446125c1cSMilan Broz 			break;
272546125c1cSMilan Broz 		}
272646125c1cSMilan Broz 
272746125c1cSMilan Broz 		io_schedule();
272846125c1cSMilan Broz 	}
272946125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
273046125c1cSMilan Broz 
2731b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
2732b44ebeb0SMikulas Patocka 
273346125c1cSMilan Broz 	return r;
273446125c1cSMilan Broz }
273546125c1cSMilan Broz 
27361da177e4SLinus Torvalds /*
27371da177e4SLinus Torvalds  * Process the deferred bios
27381da177e4SLinus Torvalds  */
2739ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
27401da177e4SLinus Torvalds {
2741ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2742ef208587SMikulas Patocka 						work);
27436d6f10dfSMilan Broz 	struct bio *c;
274483d5e5b0SMikulas Patocka 	int srcu_idx;
274583d5e5b0SMikulas Patocka 	struct dm_table *map;
27461da177e4SLinus Torvalds 
274783d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
2748ef208587SMikulas Patocka 
27493b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2750022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2751022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2752022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2753022c2611SMikulas Patocka 
27546a8736d1STejun Heo 		if (!c)
2755df12ee99SAlasdair G Kergon 			break;
275673d410c0SMilan Broz 
2757e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2758e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2759af7e466aSMikulas Patocka 		else
276083d5e5b0SMikulas Patocka 			__split_and_process_bio(md, map, c);
2761e6ee8c0bSKiyoshi Ueda 	}
27623b00b203SMikulas Patocka 
276383d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
27641da177e4SLinus Torvalds }
27651da177e4SLinus Torvalds 
27669a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2767304f3f6aSMilan Broz {
27683b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
27694e857c58SPeter Zijlstra 	smp_mb__after_atomic();
277053d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2771304f3f6aSMilan Broz }
2772304f3f6aSMilan Broz 
27731da177e4SLinus Torvalds /*
2774042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
27751da177e4SLinus Torvalds  */
2776042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
27771da177e4SLinus Torvalds {
277887eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2779754c5fc7SMike Snitzer 	struct queue_limits limits;
2780042d2a9bSAlasdair G Kergon 	int r;
27811da177e4SLinus Torvalds 
2782e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
27831da177e4SLinus Torvalds 
27841da177e4SLinus Torvalds 	/* device must be suspended */
27854f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
278693c534aeSAlasdair G Kergon 		goto out;
27871da177e4SLinus Torvalds 
27883ae70656SMike Snitzer 	/*
27893ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
27903ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
27913ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
27923ae70656SMike Snitzer 	 * reappear.
27933ae70656SMike Snitzer 	 */
27943ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
279583d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
27963ae70656SMike Snitzer 		if (live_map)
27973ae70656SMike Snitzer 			limits = md->queue->limits;
279883d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
27993ae70656SMike Snitzer 	}
28003ae70656SMike Snitzer 
280187eb5b21SMike Christie 	if (!live_map) {
2802754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2803042d2a9bSAlasdair G Kergon 		if (r) {
2804042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2805754c5fc7SMike Snitzer 			goto out;
2806042d2a9bSAlasdair G Kergon 		}
280787eb5b21SMike Christie 	}
2808754c5fc7SMike Snitzer 
2809042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
28101da177e4SLinus Torvalds 
281193c534aeSAlasdair G Kergon out:
2812e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2813042d2a9bSAlasdair G Kergon 	return map;
28141da177e4SLinus Torvalds }
28151da177e4SLinus Torvalds 
28161da177e4SLinus Torvalds /*
28171da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
28181da177e4SLinus Torvalds  * device.
28191da177e4SLinus Torvalds  */
28202ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
28211da177e4SLinus Torvalds {
2822e39e2e95SAlasdair G Kergon 	int r;
28231da177e4SLinus Torvalds 
28241da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2825dfbe03f6SAlasdair G Kergon 
2826db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2827dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2828cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2829e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2830e39e2e95SAlasdair G Kergon 		return r;
2831dfbe03f6SAlasdair G Kergon 	}
2832dfbe03f6SAlasdair G Kergon 
2833aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2834aa8d7c2fSAlasdair G Kergon 
28351da177e4SLinus Torvalds 	return 0;
28361da177e4SLinus Torvalds }
28371da177e4SLinus Torvalds 
28382ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
28391da177e4SLinus Torvalds {
2840aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2841aa8d7c2fSAlasdair G Kergon 		return;
2842aa8d7c2fSAlasdair G Kergon 
2843db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
28441da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2845aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
28461da177e4SLinus Torvalds }
28471da177e4SLinus Torvalds 
28481da177e4SLinus Torvalds /*
2849ffcc3936SMike Snitzer  * If __dm_suspend returns 0, the device is completely quiescent
2850ffcc3936SMike Snitzer  * now. There is no request-processing activity. All new requests
2851ffcc3936SMike Snitzer  * are being added to md->deferred list.
2852cec47e3dSKiyoshi Ueda  *
2853ffcc3936SMike Snitzer  * Caller must hold md->suspend_lock
2854cec47e3dSKiyoshi Ueda  */
2855ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
2856ffcc3936SMike Snitzer 			unsigned suspend_flags, int interruptible)
28571da177e4SLinus Torvalds {
2858ffcc3936SMike Snitzer 	bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2859ffcc3936SMike Snitzer 	bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2860ffcc3936SMike Snitzer 	int r;
2861cf222b37SAlasdair G Kergon 
28622e93ccc1SKiyoshi Ueda 	/*
28632e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
28642e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
28652e93ccc1SKiyoshi Ueda 	 */
28662e93ccc1SKiyoshi Ueda 	if (noflush)
28672e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
28682e93ccc1SKiyoshi Ueda 
2869d67ee213SMike Snitzer 	/*
2870d67ee213SMike Snitzer 	 * This gets reverted if there's an error later and the targets
2871d67ee213SMike Snitzer 	 * provide the .presuspend_undo hook.
2872d67ee213SMike Snitzer 	 */
28731da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
28741da177e4SLinus Torvalds 
28752e93ccc1SKiyoshi Ueda 	/*
28769f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
28779f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
28789f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
28799f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
28802e93ccc1SKiyoshi Ueda 	 */
288132a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
28822ca3310eSAlasdair G Kergon 		r = lock_fs(md);
2883d67ee213SMike Snitzer 		if (r) {
2884d67ee213SMike Snitzer 			dm_table_presuspend_undo_targets(map);
2885ffcc3936SMike Snitzer 			return r;
2886aa8d7c2fSAlasdair G Kergon 		}
2887d67ee213SMike Snitzer 	}
28881da177e4SLinus Torvalds 
28891da177e4SLinus Torvalds 	/*
28903b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
28913b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
28923b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
28933b00b203SMikulas Patocka 	 * dm_wq_work.
28943b00b203SMikulas Patocka 	 *
28953b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
28963b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
28976a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
28986a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
28996a8736d1STejun Heo 	 * flush_workqueue(md->wq).
29001da177e4SLinus Torvalds 	 */
29011eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
290241abc4e1SHannes Reinecke 	if (map)
290383d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
29041da177e4SLinus Torvalds 
2905d0bcb878SKiyoshi Ueda 	/*
290629e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
290729e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2908d0bcb878SKiyoshi Ueda 	 */
29092eb6e1e3SKeith Busch 	if (dm_request_based(md)) {
29109f518b27SKiyoshi Ueda 		stop_queue(md->queue);
29112eb6e1e3SKeith Busch 		flush_kthread_worker(&md->kworker);
29122eb6e1e3SKeith Busch 	}
2913cec47e3dSKiyoshi Ueda 
2914d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2915d0bcb878SKiyoshi Ueda 
29161da177e4SLinus Torvalds 	/*
29173b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
29183b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
29193b00b203SMikulas Patocka 	 * to finish.
29201da177e4SLinus Torvalds 	 */
2921ffcc3936SMike Snitzer 	r = dm_wait_for_completion(md, interruptible);
29221da177e4SLinus Torvalds 
29236d6f10dfSMilan Broz 	if (noflush)
2924022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
292541abc4e1SHannes Reinecke 	if (map)
292683d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
29272e93ccc1SKiyoshi Ueda 
29281da177e4SLinus Torvalds 	/* were we interrupted ? */
292946125c1cSMilan Broz 	if (r < 0) {
29309a1fb464SMikulas Patocka 		dm_queue_flush(md);
293173d410c0SMilan Broz 
2932cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
29339f518b27SKiyoshi Ueda 			start_queue(md->queue);
2934cec47e3dSKiyoshi Ueda 
29352ca3310eSAlasdair G Kergon 		unlock_fs(md);
2936d67ee213SMike Snitzer 		dm_table_presuspend_undo_targets(map);
2937ffcc3936SMike Snitzer 		/* pushback list is already flushed, so skip flush */
2938ffcc3936SMike Snitzer 	}
2939ffcc3936SMike Snitzer 
2940ffcc3936SMike Snitzer 	return r;
29412ca3310eSAlasdair G Kergon }
29422ca3310eSAlasdair G Kergon 
29433b00b203SMikulas Patocka /*
2944ffcc3936SMike Snitzer  * We need to be able to change a mapping table under a mounted
2945ffcc3936SMike Snitzer  * filesystem.  For example we might want to move some data in
2946ffcc3936SMike Snitzer  * the background.  Before the table can be swapped with
2947ffcc3936SMike Snitzer  * dm_bind_table, dm_suspend must be called to flush any in
2948ffcc3936SMike Snitzer  * flight bios and ensure that any further io gets deferred.
29493b00b203SMikulas Patocka  */
2950ffcc3936SMike Snitzer /*
2951ffcc3936SMike Snitzer  * Suspend mechanism in request-based dm.
2952ffcc3936SMike Snitzer  *
2953ffcc3936SMike Snitzer  * 1. Flush all I/Os by lock_fs() if needed.
2954ffcc3936SMike Snitzer  * 2. Stop dispatching any I/O by stopping the request_queue.
2955ffcc3936SMike Snitzer  * 3. Wait for all in-flight I/Os to be completed or requeued.
2956ffcc3936SMike Snitzer  *
2957ffcc3936SMike Snitzer  * To abort suspend, start the request_queue.
2958ffcc3936SMike Snitzer  */
2959ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2960ffcc3936SMike Snitzer {
2961ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2962ffcc3936SMike Snitzer 	int r = 0;
2963ffcc3936SMike Snitzer 
2964ffcc3936SMike Snitzer retry:
2965ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2966ffcc3936SMike Snitzer 
2967ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2968ffcc3936SMike Snitzer 		r = -EINVAL;
2969ffcc3936SMike Snitzer 		goto out_unlock;
2970ffcc3936SMike Snitzer 	}
2971ffcc3936SMike Snitzer 
2972ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2973ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2974ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2975ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2976ffcc3936SMike Snitzer 		if (r)
2977ffcc3936SMike Snitzer 			return r;
2978ffcc3936SMike Snitzer 		goto retry;
2979ffcc3936SMike Snitzer 	}
2980ffcc3936SMike Snitzer 
2981a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2982ffcc3936SMike Snitzer 
2983ffcc3936SMike Snitzer 	r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE);
2984ffcc3936SMike Snitzer 	if (r)
2985ffcc3936SMike Snitzer 		goto out_unlock;
29863b00b203SMikulas Patocka 
29871da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
29881da177e4SLinus Torvalds 
29894d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
29904d4471cbSKiyoshi Ueda 
2991d287483dSAlasdair G Kergon out_unlock:
2992e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2993cf222b37SAlasdair G Kergon 	return r;
29941da177e4SLinus Torvalds }
29951da177e4SLinus Torvalds 
2996ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map)
29971da177e4SLinus Torvalds {
2998ffcc3936SMike Snitzer 	if (map) {
2999ffcc3936SMike Snitzer 		int r = dm_table_resume_targets(map);
30008757b776SMilan Broz 		if (r)
3001ffcc3936SMike Snitzer 			return r;
3002ffcc3936SMike Snitzer 	}
30032ca3310eSAlasdair G Kergon 
30049a1fb464SMikulas Patocka 	dm_queue_flush(md);
30052ca3310eSAlasdair G Kergon 
3006cec47e3dSKiyoshi Ueda 	/*
3007cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
3008cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
3009cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
3010cec47e3dSKiyoshi Ueda 	 */
3011cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
3012cec47e3dSKiyoshi Ueda 		start_queue(md->queue);
3013cec47e3dSKiyoshi Ueda 
30142ca3310eSAlasdair G Kergon 	unlock_fs(md);
30152ca3310eSAlasdair G Kergon 
3016ffcc3936SMike Snitzer 	return 0;
3017ffcc3936SMike Snitzer }
3018ffcc3936SMike Snitzer 
3019ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md)
3020ffcc3936SMike Snitzer {
3021ffcc3936SMike Snitzer 	int r = -EINVAL;
3022ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
3023ffcc3936SMike Snitzer 
3024ffcc3936SMike Snitzer retry:
3025ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3026ffcc3936SMike Snitzer 
3027ffcc3936SMike Snitzer 	if (!dm_suspended_md(md))
3028ffcc3936SMike Snitzer 		goto out;
3029ffcc3936SMike Snitzer 
3030ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
3031ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
3032ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
3033ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3034ffcc3936SMike Snitzer 		if (r)
3035ffcc3936SMike Snitzer 			return r;
3036ffcc3936SMike Snitzer 		goto retry;
3037ffcc3936SMike Snitzer 	}
3038ffcc3936SMike Snitzer 
3039a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
3040ffcc3936SMike Snitzer 	if (!map || !dm_table_get_size(map))
3041ffcc3936SMike Snitzer 		goto out;
3042ffcc3936SMike Snitzer 
3043ffcc3936SMike Snitzer 	r = __dm_resume(md, map);
3044ffcc3936SMike Snitzer 	if (r)
3045ffcc3936SMike Snitzer 		goto out;
3046ffcc3936SMike Snitzer 
30472ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
30482ca3310eSAlasdair G Kergon 
3049cf222b37SAlasdair G Kergon 	r = 0;
3050cf222b37SAlasdair G Kergon out:
3051e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
30522ca3310eSAlasdair G Kergon 
3053cf222b37SAlasdair G Kergon 	return r;
30541da177e4SLinus Torvalds }
30551da177e4SLinus Torvalds 
3056fd2ed4d2SMikulas Patocka /*
3057fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
3058fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
3059fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
3060fd2ed4d2SMikulas Patocka  */
3061fd2ed4d2SMikulas Patocka 
3062ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
3063ffcc3936SMike Snitzer {
3064ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
3065ffcc3936SMike Snitzer 
306696b26c8cSMikulas Patocka 	if (md->internal_suspend_count++)
3067ffcc3936SMike Snitzer 		return; /* nested internal suspend */
3068ffcc3936SMike Snitzer 
3069ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
3070ffcc3936SMike Snitzer 		set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3071ffcc3936SMike Snitzer 		return; /* nest suspend */
3072ffcc3936SMike Snitzer 	}
3073ffcc3936SMike Snitzer 
3074a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
3075ffcc3936SMike Snitzer 
3076ffcc3936SMike Snitzer 	/*
3077ffcc3936SMike Snitzer 	 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
3078ffcc3936SMike Snitzer 	 * supported.  Properly supporting a TASK_INTERRUPTIBLE internal suspend
3079ffcc3936SMike Snitzer 	 * would require changing .presuspend to return an error -- avoid this
3080ffcc3936SMike Snitzer 	 * until there is a need for more elaborate variants of internal suspend.
3081ffcc3936SMike Snitzer 	 */
3082ffcc3936SMike Snitzer 	(void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE);
3083ffcc3936SMike Snitzer 
3084ffcc3936SMike Snitzer 	set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3085ffcc3936SMike Snitzer 
3086ffcc3936SMike Snitzer 	dm_table_postsuspend_targets(map);
3087ffcc3936SMike Snitzer }
3088ffcc3936SMike Snitzer 
3089ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md)
3090ffcc3936SMike Snitzer {
309196b26c8cSMikulas Patocka 	BUG_ON(!md->internal_suspend_count);
309296b26c8cSMikulas Patocka 
309396b26c8cSMikulas Patocka 	if (--md->internal_suspend_count)
3094ffcc3936SMike Snitzer 		return; /* resume from nested internal suspend */
3095ffcc3936SMike Snitzer 
3096ffcc3936SMike Snitzer 	if (dm_suspended_md(md))
3097ffcc3936SMike Snitzer 		goto done; /* resume from nested suspend */
3098ffcc3936SMike Snitzer 
3099ffcc3936SMike Snitzer 	/*
3100ffcc3936SMike Snitzer 	 * NOTE: existing callers don't need to call dm_table_resume_targets
3101ffcc3936SMike Snitzer 	 * (which may fail -- so best to avoid it for now by passing NULL map)
3102ffcc3936SMike Snitzer 	 */
3103ffcc3936SMike Snitzer 	(void) __dm_resume(md, NULL);
3104ffcc3936SMike Snitzer 
3105ffcc3936SMike Snitzer done:
3106ffcc3936SMike Snitzer 	clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3107ffcc3936SMike Snitzer 	smp_mb__after_atomic();
3108ffcc3936SMike Snitzer 	wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
3109ffcc3936SMike Snitzer }
3110ffcc3936SMike Snitzer 
3111ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md)
3112fd2ed4d2SMikulas Patocka {
3113fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
3114ffcc3936SMike Snitzer 	__dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
3115ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
3116ffcc3936SMike Snitzer }
3117ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
3118ffcc3936SMike Snitzer 
3119ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md)
3120ffcc3936SMike Snitzer {
3121ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
3122ffcc3936SMike Snitzer 	__dm_internal_resume(md);
3123ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
3124ffcc3936SMike Snitzer }
3125ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume);
3126ffcc3936SMike Snitzer 
3127ffcc3936SMike Snitzer /*
3128ffcc3936SMike Snitzer  * Fast variants of internal suspend/resume hold md->suspend_lock,
3129ffcc3936SMike Snitzer  * which prevents interaction with userspace-driven suspend.
3130ffcc3936SMike Snitzer  */
3131ffcc3936SMike Snitzer 
3132ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md)
3133ffcc3936SMike Snitzer {
3134ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
3135ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
3136fd2ed4d2SMikulas Patocka 		return;
3137fd2ed4d2SMikulas Patocka 
3138fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
3139fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
3140fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
3141fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
3142fd2ed4d2SMikulas Patocka }
3143b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
3144fd2ed4d2SMikulas Patocka 
3145ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md)
3146fd2ed4d2SMikulas Patocka {
3147ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
3148fd2ed4d2SMikulas Patocka 		goto done;
3149fd2ed4d2SMikulas Patocka 
3150fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
3151fd2ed4d2SMikulas Patocka 
3152fd2ed4d2SMikulas Patocka done:
3153fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
3154fd2ed4d2SMikulas Patocka }
3155b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
3156fd2ed4d2SMikulas Patocka 
31571da177e4SLinus Torvalds /*-----------------------------------------------------------------
31581da177e4SLinus Torvalds  * Event notification.
31591da177e4SLinus Torvalds  *---------------------------------------------------------------*/
31603abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
316160935eb2SMilan Broz 		       unsigned cookie)
316269267a30SAlasdair G Kergon {
316360935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
316460935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
316560935eb2SMilan Broz 
316660935eb2SMilan Broz 	if (!cookie)
31673abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
316860935eb2SMilan Broz 	else {
316960935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
317060935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
31713abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
31723abf85b5SPeter Rajnoha 					  action, envp);
317360935eb2SMilan Broz 	}
317469267a30SAlasdair G Kergon }
317569267a30SAlasdair G Kergon 
31767a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
31777a8c3d3bSMike Anderson {
31787a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
31797a8c3d3bSMike Anderson }
31807a8c3d3bSMike Anderson 
31811da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
31821da177e4SLinus Torvalds {
31831da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
31841da177e4SLinus Torvalds }
31851da177e4SLinus Torvalds 
31861da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
31871da177e4SLinus Torvalds {
31881da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
31891da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
31901da177e4SLinus Torvalds }
31911da177e4SLinus Torvalds 
31927a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
31937a8c3d3bSMike Anderson {
31947a8c3d3bSMike Anderson 	unsigned long flags;
31957a8c3d3bSMike Anderson 
31967a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
31977a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
31987a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
31997a8c3d3bSMike Anderson }
32007a8c3d3bSMike Anderson 
32011da177e4SLinus Torvalds /*
32021da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
32031da177e4SLinus Torvalds  * count on 'md'.
32041da177e4SLinus Torvalds  */
32051da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
32061da177e4SLinus Torvalds {
32071da177e4SLinus Torvalds 	return md->disk;
32081da177e4SLinus Torvalds }
32091da177e4SLinus Torvalds 
3210784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
3211784aae73SMilan Broz {
32122995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
3213784aae73SMilan Broz }
3214784aae73SMilan Broz 
3215784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
3216784aae73SMilan Broz {
3217784aae73SMilan Broz 	struct mapped_device *md;
3218784aae73SMilan Broz 
32192995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
3220784aae73SMilan Broz 
32214d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
3222432a212cSMike Anderson 	    dm_deleting_md(md))
32234d89b7b4SMilan Broz 		return NULL;
32244d89b7b4SMilan Broz 
3225784aae73SMilan Broz 	dm_get(md);
3226784aae73SMilan Broz 	return md;
3227784aae73SMilan Broz }
3228784aae73SMilan Broz 
32294f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
32301da177e4SLinus Torvalds {
32311da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
32321da177e4SLinus Torvalds }
32331da177e4SLinus Torvalds 
3234ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md)
3235ffcc3936SMike Snitzer {
3236ffcc3936SMike Snitzer 	return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3237ffcc3936SMike Snitzer }
3238ffcc3936SMike Snitzer 
32392c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
32402c140a24SMikulas Patocka {
32412c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
32422c140a24SMikulas Patocka }
32432c140a24SMikulas Patocka 
324464dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
324564dbce58SKiyoshi Ueda {
3246ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
324764dbce58SKiyoshi Ueda }
324864dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
324964dbce58SKiyoshi Ueda 
32502e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
32512e93ccc1SKiyoshi Ueda {
3252ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
32532e93ccc1SKiyoshi Ueda }
32542e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
32552e93ccc1SKiyoshi Ueda 
3256c0820cf5SMikulas Patocka struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity, unsigned per_bio_data_size)
3257e6ee8c0bSKiyoshi Ueda {
32585f015204SJun'ichi Nomura 	struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
32595f015204SJun'ichi Nomura 	struct kmem_cache *cachep;
3260e5863d9aSMike Snitzer 	unsigned int pool_size = 0;
32615f015204SJun'ichi Nomura 	unsigned int front_pad;
3262e6ee8c0bSKiyoshi Ueda 
3263e6ee8c0bSKiyoshi Ueda 	if (!pools)
3264e6ee8c0bSKiyoshi Ueda 		return NULL;
3265e6ee8c0bSKiyoshi Ueda 
3266e5863d9aSMike Snitzer 	switch (type) {
3267e5863d9aSMike Snitzer 	case DM_TYPE_BIO_BASED:
32685f015204SJun'ichi Nomura 		cachep = _io_cache;
3269e8603136SMike Snitzer 		pool_size = dm_get_reserved_bio_based_ios();
32705f015204SJun'ichi Nomura 		front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
3271e5863d9aSMike Snitzer 		break;
3272e5863d9aSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
3273f4790826SMike Snitzer 		pool_size = dm_get_reserved_rq_based_ios();
32741ae49ea2SMike Snitzer 		pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache);
32751ae49ea2SMike Snitzer 		if (!pools->rq_pool)
32761ae49ea2SMike Snitzer 			goto out;
3277e5863d9aSMike Snitzer 		/* fall through to setup remaining rq-based pools */
3278e5863d9aSMike Snitzer 	case DM_TYPE_MQ_REQUEST_BASED:
32795f015204SJun'ichi Nomura 		cachep = _rq_tio_cache;
3280e5863d9aSMike Snitzer 		if (!pool_size)
32816cfa5857SMike Snitzer 			pool_size = dm_get_reserved_rq_based_ios();
32825f015204SJun'ichi Nomura 		front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
32835f015204SJun'ichi Nomura 		/* per_bio_data_size is not used. See __bind_mempools(). */
32845f015204SJun'ichi Nomura 		WARN_ON(per_bio_data_size != 0);
3285e5863d9aSMike Snitzer 		break;
3286e5863d9aSMike Snitzer 	default:
32875f015204SJun'ichi Nomura 		goto out;
3288e5863d9aSMike Snitzer 	}
32895f015204SJun'ichi Nomura 
32906cfa5857SMike Snitzer 	pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
3291e6ee8c0bSKiyoshi Ueda 	if (!pools->io_pool)
32925f015204SJun'ichi Nomura 		goto out;
3293e6ee8c0bSKiyoshi Ueda 
32943d8aab2dSJunichi Nomura 	pools->bs = bioset_create_nobvec(pool_size, front_pad);
3295e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
32965f015204SJun'ichi Nomura 		goto out;
3297e6ee8c0bSKiyoshi Ueda 
3298a91a2785SMartin K. Petersen 	if (integrity && bioset_integrity_create(pools->bs, pool_size))
32995f015204SJun'ichi Nomura 		goto out;
3300a91a2785SMartin K. Petersen 
3301e6ee8c0bSKiyoshi Ueda 	return pools;
3302e6ee8c0bSKiyoshi Ueda 
33035f015204SJun'ichi Nomura out:
33045f015204SJun'ichi Nomura 	dm_free_md_mempools(pools);
3305e6ee8c0bSKiyoshi Ueda 
3306e6ee8c0bSKiyoshi Ueda 	return NULL;
3307e6ee8c0bSKiyoshi Ueda }
3308e6ee8c0bSKiyoshi Ueda 
3309e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
3310e6ee8c0bSKiyoshi Ueda {
3311e6ee8c0bSKiyoshi Ueda 	if (!pools)
3312e6ee8c0bSKiyoshi Ueda 		return;
3313e6ee8c0bSKiyoshi Ueda 
3314e6ee8c0bSKiyoshi Ueda 	if (pools->io_pool)
3315e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->io_pool);
3316e6ee8c0bSKiyoshi Ueda 
33171ae49ea2SMike Snitzer 	if (pools->rq_pool)
33181ae49ea2SMike Snitzer 		mempool_destroy(pools->rq_pool);
33191ae49ea2SMike Snitzer 
3320e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
3321e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
3322e6ee8c0bSKiyoshi Ueda 
3323e6ee8c0bSKiyoshi Ueda 	kfree(pools);
3324e6ee8c0bSKiyoshi Ueda }
3325e6ee8c0bSKiyoshi Ueda 
332683d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
33271da177e4SLinus Torvalds 	.open = dm_blk_open,
33281da177e4SLinus Torvalds 	.release = dm_blk_close,
3329aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
33303ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
33311da177e4SLinus Torvalds 	.owner = THIS_MODULE
33321da177e4SLinus Torvalds };
33331da177e4SLinus Torvalds 
33341da177e4SLinus Torvalds /*
33351da177e4SLinus Torvalds  * module hooks
33361da177e4SLinus Torvalds  */
33371da177e4SLinus Torvalds module_init(dm_init);
33381da177e4SLinus Torvalds module_exit(dm_exit);
33391da177e4SLinus Torvalds 
33401da177e4SLinus Torvalds module_param(major, uint, 0);
33411da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
3342f4790826SMike Snitzer 
3343e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3344e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3345e8603136SMike Snitzer 
3346f4790826SMike Snitzer module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
3347f4790826SMike Snitzer MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
3348f4790826SMike Snitzer 
33491da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
33501da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
33511da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3352