xref: /openbmc/linux/drivers/md/dm.c (revision 1ae49ea2cf3ef097d4496981261a400f1f988b84)
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>
2355782138SLi Zefan 
2455782138SLi Zefan #include <trace/events/block.h>
251da177e4SLinus Torvalds 
2672d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
2772d94861SAlasdair G Kergon 
2871a16736SNamhyung Kim #ifdef CONFIG_PRINTK
2971a16736SNamhyung Kim /*
3071a16736SNamhyung Kim  * ratelimit state to be used in DMXXX_LIMIT().
3171a16736SNamhyung Kim  */
3271a16736SNamhyung Kim DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
3371a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_INTERVAL,
3471a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_BURST);
3571a16736SNamhyung Kim EXPORT_SYMBOL(dm_ratelimit_state);
3671a16736SNamhyung Kim #endif
3771a16736SNamhyung Kim 
3860935eb2SMilan Broz /*
3960935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
4060935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
4160935eb2SMilan Broz  */
4260935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
4360935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
4460935eb2SMilan Broz 
451da177e4SLinus Torvalds static const char *_name = DM_NAME;
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds static unsigned int major = 0;
481da177e4SLinus Torvalds static unsigned int _major = 0;
491da177e4SLinus Torvalds 
50d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr);
51d15b774cSAlasdair G Kergon 
52f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
532c140a24SMikulas Patocka 
542c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w);
552c140a24SMikulas Patocka 
562c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
572c140a24SMikulas Patocka 
58acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue;
59acfe0ad7SMikulas Patocka 
601da177e4SLinus Torvalds /*
618fbf26adSKiyoshi Ueda  * For bio-based dm.
621da177e4SLinus Torvalds  * One of these is allocated per bio.
631da177e4SLinus Torvalds  */
641da177e4SLinus Torvalds struct dm_io {
651da177e4SLinus Torvalds 	struct mapped_device *md;
661da177e4SLinus Torvalds 	int error;
671da177e4SLinus Torvalds 	atomic_t io_count;
686ae2fa67SRichard Kennedy 	struct bio *bio;
693eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
70f88fb981SKiyoshi Ueda 	spinlock_t endio_lock;
71fd2ed4d2SMikulas Patocka 	struct dm_stats_aux stats_aux;
721da177e4SLinus Torvalds };
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /*
758fbf26adSKiyoshi Ueda  * For request-based dm.
768fbf26adSKiyoshi Ueda  * One of these is allocated per request.
778fbf26adSKiyoshi Ueda  */
788fbf26adSKiyoshi Ueda struct dm_rq_target_io {
798fbf26adSKiyoshi Ueda 	struct mapped_device *md;
808fbf26adSKiyoshi Ueda 	struct dm_target *ti;
81*1ae49ea2SMike Snitzer 	struct request *orig, *clone;
828fbf26adSKiyoshi Ueda 	int error;
838fbf26adSKiyoshi Ueda 	union map_info info;
848fbf26adSKiyoshi Ueda };
858fbf26adSKiyoshi Ueda 
868fbf26adSKiyoshi Ueda /*
8794818742SKent Overstreet  * For request-based dm - the bio clones we allocate are embedded in these
8894818742SKent Overstreet  * structs.
8994818742SKent Overstreet  *
9094818742SKent Overstreet  * We allocate these with bio_alloc_bioset, using the front_pad parameter when
9194818742SKent Overstreet  * the bioset is created - this means the bio has to come at the end of the
9294818742SKent Overstreet  * struct.
938fbf26adSKiyoshi Ueda  */
948fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info {
958fbf26adSKiyoshi Ueda 	struct bio *orig;
96cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio;
9794818742SKent Overstreet 	struct bio clone;
988fbf26adSKiyoshi Ueda };
998fbf26adSKiyoshi Ueda 
100cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq)
101cec47e3dSKiyoshi Ueda {
102cec47e3dSKiyoshi Ueda 	if (rq && rq->end_io_data)
103cec47e3dSKiyoshi Ueda 		return &((struct dm_rq_target_io *)rq->end_io_data)->info;
104cec47e3dSKiyoshi Ueda 	return NULL;
105cec47e3dSKiyoshi Ueda }
106cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
107cec47e3dSKiyoshi Ueda 
108ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
109ba61fdd1SJeff Mahoney 
1101da177e4SLinus Torvalds /*
1111da177e4SLinus Torvalds  * Bits for the md->flags field.
1121da177e4SLinus Torvalds  */
1131eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
1141da177e4SLinus Torvalds #define DMF_SUSPENDED 1
115aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
116fba9f90eSJeff Mahoney #define DMF_FREEING 3
1175c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
1182e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
119d5b9dd04SMikulas Patocka #define DMF_MERGE_IS_OPTIONAL 6
1202c140a24SMikulas Patocka #define DMF_DEFERRED_REMOVE 7
121ffcc3936SMike Snitzer #define DMF_SUSPENDED_INTERNALLY 8
1221da177e4SLinus Torvalds 
123304f3f6aSMilan Broz /*
12483d5e5b0SMikulas Patocka  * A dummy definition to make RCU happy.
12583d5e5b0SMikulas Patocka  * struct dm_table should never be dereferenced in this file.
12683d5e5b0SMikulas Patocka  */
12783d5e5b0SMikulas Patocka struct dm_table {
12883d5e5b0SMikulas Patocka 	int undefined__;
12983d5e5b0SMikulas Patocka };
13083d5e5b0SMikulas Patocka 
13183d5e5b0SMikulas Patocka /*
132304f3f6aSMilan Broz  * Work processed by per-device workqueue.
133304f3f6aSMilan Broz  */
1341da177e4SLinus Torvalds struct mapped_device {
13583d5e5b0SMikulas Patocka 	struct srcu_struct io_barrier;
136e61290a4SDaniel Walker 	struct mutex suspend_lock;
1371da177e4SLinus Torvalds 	atomic_t holders;
1385c6bd75dSAlasdair G Kergon 	atomic_t open_count;
1391da177e4SLinus Torvalds 
1402a7faeb1SMikulas Patocka 	/*
1412a7faeb1SMikulas Patocka 	 * The current mapping.
1422a7faeb1SMikulas Patocka 	 * Use dm_get_live_table{_fast} or take suspend_lock for
1432a7faeb1SMikulas Patocka 	 * dereference.
1442a7faeb1SMikulas Patocka 	 */
1456fa99520SPranith Kumar 	struct dm_table __rcu *map;
1462a7faeb1SMikulas Patocka 
14786f1152bSBenjamin Marzinski 	struct list_head table_devices;
14886f1152bSBenjamin Marzinski 	struct mutex table_devices_lock;
14986f1152bSBenjamin Marzinski 
1501da177e4SLinus Torvalds 	unsigned long flags;
1511da177e4SLinus Torvalds 
152165125e1SJens Axboe 	struct request_queue *queue;
153a5664dadSMike Snitzer 	unsigned type;
1544a0b4ddfSMike Snitzer 	/* Protect queue and type against concurrent access. */
155a5664dadSMike Snitzer 	struct mutex type_lock;
156a5664dadSMike Snitzer 
15736a0456fSAlasdair G Kergon 	struct target_type *immutable_target_type;
15836a0456fSAlasdair G Kergon 
1591da177e4SLinus Torvalds 	struct gendisk *disk;
1607e51f257SMike Anderson 	char name[16];
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	void *interface_ptr;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	/*
1651da177e4SLinus Torvalds 	 * A list of ios that arrived while we were suspended.
1661da177e4SLinus Torvalds 	 */
167316d315bSNikanth Karthikesan 	atomic_t pending[2];
1681da177e4SLinus Torvalds 	wait_queue_head_t wait;
16953d5914fSMikulas Patocka 	struct work_struct work;
1701da177e4SLinus Torvalds 	struct bio_list deferred;
171022c2611SMikulas Patocka 	spinlock_t deferred_lock;
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	/*
17429e4013dSTejun Heo 	 * Processing queue (flush)
175304f3f6aSMilan Broz 	 */
176304f3f6aSMilan Broz 	struct workqueue_struct *wq;
177304f3f6aSMilan Broz 
178304f3f6aSMilan Broz 	/*
1791da177e4SLinus Torvalds 	 * io objects are allocated from here.
1801da177e4SLinus Torvalds 	 */
1811da177e4SLinus Torvalds 	mempool_t *io_pool;
182*1ae49ea2SMike Snitzer 	mempool_t *rq_pool;
1831da177e4SLinus Torvalds 
1849faf400fSStefan Bader 	struct bio_set *bs;
1859faf400fSStefan Bader 
1861da177e4SLinus Torvalds 	/*
1871da177e4SLinus Torvalds 	 * Event handling.
1881da177e4SLinus Torvalds 	 */
1891da177e4SLinus Torvalds 	atomic_t event_nr;
1901da177e4SLinus Torvalds 	wait_queue_head_t eventq;
1917a8c3d3bSMike Anderson 	atomic_t uevent_seq;
1927a8c3d3bSMike Anderson 	struct list_head uevent_list;
1937a8c3d3bSMike Anderson 	spinlock_t uevent_lock; /* Protect access to uevent_list */
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	/*
1961da177e4SLinus Torvalds 	 * freeze/thaw support require holding onto a super block
1971da177e4SLinus Torvalds 	 */
1981da177e4SLinus Torvalds 	struct super_block *frozen_sb;
199db8fef4fSMikulas Patocka 	struct block_device *bdev;
2003ac51e74SDarrick J. Wong 
2013ac51e74SDarrick J. Wong 	/* forced geometry settings */
2023ac51e74SDarrick J. Wong 	struct hd_geometry geometry;
203784aae73SMilan Broz 
2042995fa78SMikulas Patocka 	/* kobject and completion */
2052995fa78SMikulas Patocka 	struct dm_kobject_holder kobj_holder;
206be35f486SMikulas Patocka 
207d87f4c14STejun Heo 	/* zero-length flush that will be cloned and submitted to targets */
208d87f4c14STejun Heo 	struct bio flush_bio;
209fd2ed4d2SMikulas Patocka 
210fd2ed4d2SMikulas Patocka 	struct dm_stats stats;
2111da177e4SLinus Torvalds };
2121da177e4SLinus Torvalds 
213e6ee8c0bSKiyoshi Ueda /*
214e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
215e6ee8c0bSKiyoshi Ueda  */
216e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
217e6ee8c0bSKiyoshi Ueda 	mempool_t *io_pool;
218*1ae49ea2SMike Snitzer 	mempool_t *rq_pool;
219e6ee8c0bSKiyoshi Ueda 	struct bio_set *bs;
220e6ee8c0bSKiyoshi Ueda };
221e6ee8c0bSKiyoshi Ueda 
22286f1152bSBenjamin Marzinski struct table_device {
22386f1152bSBenjamin Marzinski 	struct list_head list;
22486f1152bSBenjamin Marzinski 	atomic_t count;
22586f1152bSBenjamin Marzinski 	struct dm_dev dm_dev;
22686f1152bSBenjamin Marzinski };
22786f1152bSBenjamin Marzinski 
2286cfa5857SMike Snitzer #define RESERVED_BIO_BASED_IOS		16
2296cfa5857SMike Snitzer #define RESERVED_REQUEST_BASED_IOS	256
230f4790826SMike Snitzer #define RESERVED_MAX_IOS		1024
231e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
2328fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
233*1ae49ea2SMike Snitzer static struct kmem_cache *_rq_cache;
23494818742SKent Overstreet 
235f4790826SMike Snitzer /*
236e8603136SMike Snitzer  * Bio-based DM's mempools' reserved IOs set by the user.
237e8603136SMike Snitzer  */
238e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
239e8603136SMike Snitzer 
240e8603136SMike Snitzer /*
241f4790826SMike Snitzer  * Request-based DM's mempools' reserved IOs set by the user.
242f4790826SMike Snitzer  */
243f4790826SMike Snitzer static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
244f4790826SMike Snitzer 
245f4790826SMike Snitzer static unsigned __dm_get_reserved_ios(unsigned *reserved_ios,
246f4790826SMike Snitzer 				      unsigned def, unsigned max)
247f4790826SMike Snitzer {
248f4790826SMike Snitzer 	unsigned ios = ACCESS_ONCE(*reserved_ios);
249f4790826SMike Snitzer 	unsigned modified_ios = 0;
250f4790826SMike Snitzer 
251f4790826SMike Snitzer 	if (!ios)
252f4790826SMike Snitzer 		modified_ios = def;
253f4790826SMike Snitzer 	else if (ios > max)
254f4790826SMike Snitzer 		modified_ios = max;
255f4790826SMike Snitzer 
256f4790826SMike Snitzer 	if (modified_ios) {
257f4790826SMike Snitzer 		(void)cmpxchg(reserved_ios, ios, modified_ios);
258f4790826SMike Snitzer 		ios = modified_ios;
259f4790826SMike Snitzer 	}
260f4790826SMike Snitzer 
261f4790826SMike Snitzer 	return ios;
262f4790826SMike Snitzer }
263f4790826SMike Snitzer 
264e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void)
265e8603136SMike Snitzer {
266e8603136SMike Snitzer 	return __dm_get_reserved_ios(&reserved_bio_based_ios,
267e8603136SMike Snitzer 				     RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
268e8603136SMike Snitzer }
269e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
270e8603136SMike Snitzer 
271f4790826SMike Snitzer unsigned dm_get_reserved_rq_based_ios(void)
272f4790826SMike Snitzer {
273f4790826SMike Snitzer 	return __dm_get_reserved_ios(&reserved_rq_based_ios,
274f4790826SMike Snitzer 				     RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
275f4790826SMike Snitzer }
276f4790826SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
277f4790826SMike Snitzer 
2781da177e4SLinus Torvalds static int __init local_init(void)
2791da177e4SLinus Torvalds {
28051157b4aSKiyoshi Ueda 	int r = -ENOMEM;
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
283028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
2841da177e4SLinus Torvalds 	if (!_io_cache)
28551157b4aSKiyoshi Ueda 		return r;
2861da177e4SLinus Torvalds 
2878fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
2888fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
289dba14160SMikulas Patocka 		goto out_free_io_cache;
2908fbf26adSKiyoshi Ueda 
291*1ae49ea2SMike Snitzer 	_rq_cache = kmem_cache_create("dm_clone_request", sizeof(struct request),
292*1ae49ea2SMike Snitzer 				      __alignof__(struct request), 0, NULL);
293*1ae49ea2SMike Snitzer 	if (!_rq_cache)
294*1ae49ea2SMike Snitzer 		goto out_free_rq_tio_cache;
295*1ae49ea2SMike Snitzer 
29651e5b2bdSMike Anderson 	r = dm_uevent_init();
29751157b4aSKiyoshi Ueda 	if (r)
298*1ae49ea2SMike Snitzer 		goto out_free_rq_cache;
29951e5b2bdSMike Anderson 
300acfe0ad7SMikulas Patocka 	deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
301acfe0ad7SMikulas Patocka 	if (!deferred_remove_workqueue) {
302acfe0ad7SMikulas Patocka 		r = -ENOMEM;
303acfe0ad7SMikulas Patocka 		goto out_uevent_exit;
304acfe0ad7SMikulas Patocka 	}
305acfe0ad7SMikulas Patocka 
3061da177e4SLinus Torvalds 	_major = major;
3071da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
30851157b4aSKiyoshi Ueda 	if (r < 0)
309acfe0ad7SMikulas Patocka 		goto out_free_workqueue;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	if (!_major)
3121da177e4SLinus Torvalds 		_major = r;
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	return 0;
31551157b4aSKiyoshi Ueda 
316acfe0ad7SMikulas Patocka out_free_workqueue:
317acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
31851157b4aSKiyoshi Ueda out_uevent_exit:
31951157b4aSKiyoshi Ueda 	dm_uevent_exit();
320*1ae49ea2SMike Snitzer out_free_rq_cache:
321*1ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
3228fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
3238fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
32451157b4aSKiyoshi Ueda out_free_io_cache:
32551157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
32651157b4aSKiyoshi Ueda 
32751157b4aSKiyoshi Ueda 	return r;
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds static void local_exit(void)
3311da177e4SLinus Torvalds {
3322c140a24SMikulas Patocka 	flush_scheduled_work();
333acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
3342c140a24SMikulas Patocka 
335*1ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
3368fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
3371da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
33800d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
33951e5b2bdSMike Anderson 	dm_uevent_exit();
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	_major = 0;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	DMINFO("cleaned up");
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
346b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
3471da177e4SLinus Torvalds 	local_init,
3481da177e4SLinus Torvalds 	dm_target_init,
3491da177e4SLinus Torvalds 	dm_linear_init,
3501da177e4SLinus Torvalds 	dm_stripe_init,
351952b3557SMikulas Patocka 	dm_io_init,
352945fa4d2SMikulas Patocka 	dm_kcopyd_init,
3531da177e4SLinus Torvalds 	dm_interface_init,
354fd2ed4d2SMikulas Patocka 	dm_statistics_init,
3551da177e4SLinus Torvalds };
3561da177e4SLinus Torvalds 
357b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
3581da177e4SLinus Torvalds 	local_exit,
3591da177e4SLinus Torvalds 	dm_target_exit,
3601da177e4SLinus Torvalds 	dm_linear_exit,
3611da177e4SLinus Torvalds 	dm_stripe_exit,
362952b3557SMikulas Patocka 	dm_io_exit,
363945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
3641da177e4SLinus Torvalds 	dm_interface_exit,
365fd2ed4d2SMikulas Patocka 	dm_statistics_exit,
3661da177e4SLinus Torvalds };
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds static int __init dm_init(void)
3691da177e4SLinus Torvalds {
3701da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	int r, i;
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
3751da177e4SLinus Torvalds 		r = _inits[i]();
3761da177e4SLinus Torvalds 		if (r)
3771da177e4SLinus Torvalds 			goto bad;
3781da177e4SLinus Torvalds 	}
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	return 0;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds       bad:
3831da177e4SLinus Torvalds 	while (i--)
3841da177e4SLinus Torvalds 		_exits[i]();
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	return r;
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds static void __exit dm_exit(void)
3901da177e4SLinus Torvalds {
3911da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	while (i--)
3941da177e4SLinus Torvalds 		_exits[i]();
395d15b774cSAlasdair G Kergon 
396d15b774cSAlasdair G Kergon 	/*
397d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
398d15b774cSAlasdair G Kergon 	 */
399d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
4001da177e4SLinus Torvalds }
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds /*
4031da177e4SLinus Torvalds  * Block device functions
4041da177e4SLinus Torvalds  */
405432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
406432a212cSMike Anderson {
407432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
408432a212cSMike Anderson }
409432a212cSMike Anderson 
410fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
4111da177e4SLinus Torvalds {
4121da177e4SLinus Torvalds 	struct mapped_device *md;
4131da177e4SLinus Torvalds 
414fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
415fba9f90eSJeff Mahoney 
416fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
417fba9f90eSJeff Mahoney 	if (!md)
418fba9f90eSJeff Mahoney 		goto out;
419fba9f90eSJeff Mahoney 
4205c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
421432a212cSMike Anderson 	    dm_deleting_md(md)) {
422fba9f90eSJeff Mahoney 		md = NULL;
423fba9f90eSJeff Mahoney 		goto out;
424fba9f90eSJeff Mahoney 	}
425fba9f90eSJeff Mahoney 
4261da177e4SLinus Torvalds 	dm_get(md);
4275c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
428fba9f90eSJeff Mahoney 
429fba9f90eSJeff Mahoney out:
430fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
431fba9f90eSJeff Mahoney 
432fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
435db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode)
4361da177e4SLinus Torvalds {
437fe5f9f2cSAl Viro 	struct mapped_device *md = disk->private_data;
4386e9624b8SArnd Bergmann 
4394a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
4404a1aeb98SMilan Broz 
4412c140a24SMikulas Patocka 	if (atomic_dec_and_test(&md->open_count) &&
4422c140a24SMikulas Patocka 	    (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
443acfe0ad7SMikulas Patocka 		queue_work(deferred_remove_workqueue, &deferred_remove_work);
4442c140a24SMikulas Patocka 
4451da177e4SLinus Torvalds 	dm_put(md);
4464a1aeb98SMilan Broz 
4474a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds 
4505c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
4515c6bd75dSAlasdair G Kergon {
4525c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
4535c6bd75dSAlasdair G Kergon }
4545c6bd75dSAlasdair G Kergon 
4555c6bd75dSAlasdair G Kergon /*
4565c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
4575c6bd75dSAlasdair G Kergon  */
4582c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
4595c6bd75dSAlasdair G Kergon {
4605c6bd75dSAlasdair G Kergon 	int r = 0;
4615c6bd75dSAlasdair G Kergon 
4625c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
4635c6bd75dSAlasdair G Kergon 
4642c140a24SMikulas Patocka 	if (dm_open_count(md)) {
4655c6bd75dSAlasdair G Kergon 		r = -EBUSY;
4662c140a24SMikulas Patocka 		if (mark_deferred)
4672c140a24SMikulas Patocka 			set_bit(DMF_DEFERRED_REMOVE, &md->flags);
4682c140a24SMikulas Patocka 	} else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
4692c140a24SMikulas Patocka 		r = -EEXIST;
4705c6bd75dSAlasdair G Kergon 	else
4715c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
4725c6bd75dSAlasdair G Kergon 
4735c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
4745c6bd75dSAlasdair G Kergon 
4755c6bd75dSAlasdair G Kergon 	return r;
4765c6bd75dSAlasdair G Kergon }
4775c6bd75dSAlasdair G Kergon 
4782c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md)
4792c140a24SMikulas Patocka {
4802c140a24SMikulas Patocka 	int r = 0;
4812c140a24SMikulas Patocka 
4822c140a24SMikulas Patocka 	spin_lock(&_minor_lock);
4832c140a24SMikulas Patocka 
4842c140a24SMikulas Patocka 	if (test_bit(DMF_DELETING, &md->flags))
4852c140a24SMikulas Patocka 		r = -EBUSY;
4862c140a24SMikulas Patocka 	else
4872c140a24SMikulas Patocka 		clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
4882c140a24SMikulas Patocka 
4892c140a24SMikulas Patocka 	spin_unlock(&_minor_lock);
4902c140a24SMikulas Patocka 
4912c140a24SMikulas Patocka 	return r;
4922c140a24SMikulas Patocka }
4932c140a24SMikulas Patocka 
4942c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w)
4952c140a24SMikulas Patocka {
4962c140a24SMikulas Patocka 	dm_deferred_remove();
4972c140a24SMikulas Patocka }
4982c140a24SMikulas Patocka 
499fd2ed4d2SMikulas Patocka sector_t dm_get_size(struct mapped_device *md)
500fd2ed4d2SMikulas Patocka {
501fd2ed4d2SMikulas Patocka 	return get_capacity(md->disk);
502fd2ed4d2SMikulas Patocka }
503fd2ed4d2SMikulas Patocka 
5049974fa2cSMike Snitzer struct request_queue *dm_get_md_queue(struct mapped_device *md)
5059974fa2cSMike Snitzer {
5069974fa2cSMike Snitzer 	return md->queue;
5079974fa2cSMike Snitzer }
5089974fa2cSMike Snitzer 
509fd2ed4d2SMikulas Patocka struct dm_stats *dm_get_stats(struct mapped_device *md)
510fd2ed4d2SMikulas Patocka {
511fd2ed4d2SMikulas Patocka 	return &md->stats;
512fd2ed4d2SMikulas Patocka }
513fd2ed4d2SMikulas Patocka 
5143ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5153ac51e74SDarrick J. Wong {
5163ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
5173ac51e74SDarrick J. Wong 
5183ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
5193ac51e74SDarrick J. Wong }
5203ac51e74SDarrick J. Wong 
521fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
522aa129a22SMilan Broz 			unsigned int cmd, unsigned long arg)
523aa129a22SMilan Broz {
524fe5f9f2cSAl Viro 	struct mapped_device *md = bdev->bd_disk->private_data;
52583d5e5b0SMikulas Patocka 	int srcu_idx;
5266c182cd8SHannes Reinecke 	struct dm_table *map;
527aa129a22SMilan Broz 	struct dm_target *tgt;
528aa129a22SMilan Broz 	int r = -ENOTTY;
529aa129a22SMilan Broz 
5306c182cd8SHannes Reinecke retry:
53183d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
53283d5e5b0SMikulas Patocka 
533aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
534aa129a22SMilan Broz 		goto out;
535aa129a22SMilan Broz 
536aa129a22SMilan Broz 	/* We only support devices that have a single target */
537aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
538aa129a22SMilan Broz 		goto out;
539aa129a22SMilan Broz 
540aa129a22SMilan Broz 	tgt = dm_table_get_target(map, 0);
5414d341d82SMike Snitzer 	if (!tgt->type->ioctl)
5424d341d82SMike Snitzer 		goto out;
543aa129a22SMilan Broz 
5444f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
545aa129a22SMilan Broz 		r = -EAGAIN;
546aa129a22SMilan Broz 		goto out;
547aa129a22SMilan Broz 	}
548aa129a22SMilan Broz 
549647b3d00SAl Viro 	r = tgt->type->ioctl(tgt, cmd, arg);
550aa129a22SMilan Broz 
551aa129a22SMilan Broz out:
55283d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
553aa129a22SMilan Broz 
5546c182cd8SHannes Reinecke 	if (r == -ENOTCONN) {
5556c182cd8SHannes Reinecke 		msleep(10);
5566c182cd8SHannes Reinecke 		goto retry;
5576c182cd8SHannes Reinecke 	}
5586c182cd8SHannes Reinecke 
559aa129a22SMilan Broz 	return r;
560aa129a22SMilan Broz }
561aa129a22SMilan Broz 
562028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
5631da177e4SLinus Torvalds {
5641da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
5651da177e4SLinus Torvalds }
5661da177e4SLinus Torvalds 
567028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
5681da177e4SLinus Torvalds {
5691da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds 
572028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
5731da177e4SLinus Torvalds {
574dba14160SMikulas Patocka 	bio_put(&tio->clone);
5751da177e4SLinus Torvalds }
5761da177e4SLinus Torvalds 
57708885643SKiyoshi Ueda static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
57808885643SKiyoshi Ueda 					    gfp_t gfp_mask)
579cec47e3dSKiyoshi Ueda {
5805f015204SJun'ichi Nomura 	return mempool_alloc(md->io_pool, gfp_mask);
581cec47e3dSKiyoshi Ueda }
582cec47e3dSKiyoshi Ueda 
583cec47e3dSKiyoshi Ueda static void free_rq_tio(struct dm_rq_target_io *tio)
584cec47e3dSKiyoshi Ueda {
5855f015204SJun'ichi Nomura 	mempool_free(tio, tio->md->io_pool);
586cec47e3dSKiyoshi Ueda }
587cec47e3dSKiyoshi Ueda 
588*1ae49ea2SMike Snitzer static struct request *alloc_clone_request(struct mapped_device *md,
589*1ae49ea2SMike Snitzer 					   gfp_t gfp_mask)
590*1ae49ea2SMike Snitzer {
591*1ae49ea2SMike Snitzer 	return mempool_alloc(md->rq_pool, gfp_mask);
592*1ae49ea2SMike Snitzer }
593*1ae49ea2SMike Snitzer 
594*1ae49ea2SMike Snitzer static void free_clone_request(struct mapped_device *md, struct request *rq)
595*1ae49ea2SMike Snitzer {
596*1ae49ea2SMike Snitzer 	mempool_free(rq, md->rq_pool);
597*1ae49ea2SMike Snitzer }
598*1ae49ea2SMike Snitzer 
59990abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md)
60090abb8c4SKiyoshi Ueda {
60190abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
60290abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
60390abb8c4SKiyoshi Ueda }
60490abb8c4SKiyoshi Ueda 
6053eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
6063eaf840eSJun'ichi "Nick" Nomura {
6073eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
608fd2ed4d2SMikulas Patocka 	struct bio *bio = io->bio;
609c9959059STejun Heo 	int cpu;
610fd2ed4d2SMikulas Patocka 	int rw = bio_data_dir(bio);
6113eaf840eSJun'ichi "Nick" Nomura 
6123eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
6133eaf840eSJun'ichi "Nick" Nomura 
614074a7acaSTejun Heo 	cpu = part_stat_lock();
615074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
616074a7acaSTejun Heo 	part_stat_unlock();
6171e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw],
6181e9bb880SShaohua Li 		atomic_inc_return(&md->pending[rw]));
619fd2ed4d2SMikulas Patocka 
620fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
6214f024f37SKent Overstreet 		dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
622fd2ed4d2SMikulas Patocka 				    bio_sectors(bio), false, 0, &io->stats_aux);
6233eaf840eSJun'ichi "Nick" Nomura }
6243eaf840eSJun'ichi "Nick" Nomura 
625d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
6263eaf840eSJun'ichi "Nick" Nomura {
6273eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
6283eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
6293eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
63018c0b223SGu Zheng 	int pending;
6313eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
6323eaf840eSJun'ichi "Nick" Nomura 
63318c0b223SGu Zheng 	generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time);
6343eaf840eSJun'ichi "Nick" Nomura 
635fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
6364f024f37SKent Overstreet 		dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
637fd2ed4d2SMikulas Patocka 				    bio_sectors(bio), true, duration, &io->stats_aux);
638fd2ed4d2SMikulas Patocka 
639af7e466aSMikulas Patocka 	/*
640af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
641d87f4c14STejun Heo 	 * a flush.
642af7e466aSMikulas Patocka 	 */
6431e9bb880SShaohua Li 	pending = atomic_dec_return(&md->pending[rw]);
6441e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
645316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
6463eaf840eSJun'ichi "Nick" Nomura 
647d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
648d221d2e7SMikulas Patocka 	if (!pending)
649d221d2e7SMikulas Patocka 		wake_up(&md->wait);
6503eaf840eSJun'ichi "Nick" Nomura }
6513eaf840eSJun'ichi "Nick" Nomura 
6521da177e4SLinus Torvalds /*
6531da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
6541da177e4SLinus Torvalds  */
65592c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
6561da177e4SLinus Torvalds {
65705447420SKiyoshi Ueda 	unsigned long flags;
6581da177e4SLinus Torvalds 
65905447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
6601da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
66105447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
66292c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
6631da177e4SLinus Torvalds }
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds /*
6661da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
6671da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
66883d5e5b0SMikulas Patocka  * dm_put_live_table() when finished.
6691da177e4SLinus Torvalds  */
67083d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
6711da177e4SLinus Torvalds {
67283d5e5b0SMikulas Patocka 	*srcu_idx = srcu_read_lock(&md->io_barrier);
6731da177e4SLinus Torvalds 
67483d5e5b0SMikulas Patocka 	return srcu_dereference(md->map, &md->io_barrier);
67583d5e5b0SMikulas Patocka }
6761da177e4SLinus Torvalds 
67783d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
67883d5e5b0SMikulas Patocka {
67983d5e5b0SMikulas Patocka 	srcu_read_unlock(&md->io_barrier, srcu_idx);
68083d5e5b0SMikulas Patocka }
68183d5e5b0SMikulas Patocka 
68283d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md)
68383d5e5b0SMikulas Patocka {
68483d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
68583d5e5b0SMikulas Patocka 	synchronize_rcu_expedited();
68683d5e5b0SMikulas Patocka }
68783d5e5b0SMikulas Patocka 
68883d5e5b0SMikulas Patocka /*
68983d5e5b0SMikulas Patocka  * A fast alternative to dm_get_live_table/dm_put_live_table.
69083d5e5b0SMikulas Patocka  * The caller must not block between these two functions.
69183d5e5b0SMikulas Patocka  */
69283d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
69383d5e5b0SMikulas Patocka {
69483d5e5b0SMikulas Patocka 	rcu_read_lock();
69583d5e5b0SMikulas Patocka 	return rcu_dereference(md->map);
69683d5e5b0SMikulas Patocka }
69783d5e5b0SMikulas Patocka 
69883d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
69983d5e5b0SMikulas Patocka {
70083d5e5b0SMikulas Patocka 	rcu_read_unlock();
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds 
7033ac51e74SDarrick J. Wong /*
70486f1152bSBenjamin Marzinski  * Open a table device so we can use it as a map destination.
70586f1152bSBenjamin Marzinski  */
70686f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev,
70786f1152bSBenjamin Marzinski 			     struct mapped_device *md)
70886f1152bSBenjamin Marzinski {
70986f1152bSBenjamin Marzinski 	static char *_claim_ptr = "I belong to device-mapper";
71086f1152bSBenjamin Marzinski 	struct block_device *bdev;
71186f1152bSBenjamin Marzinski 
71286f1152bSBenjamin Marzinski 	int r;
71386f1152bSBenjamin Marzinski 
71486f1152bSBenjamin Marzinski 	BUG_ON(td->dm_dev.bdev);
71586f1152bSBenjamin Marzinski 
71686f1152bSBenjamin Marzinski 	bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
71786f1152bSBenjamin Marzinski 	if (IS_ERR(bdev))
71886f1152bSBenjamin Marzinski 		return PTR_ERR(bdev);
71986f1152bSBenjamin Marzinski 
72086f1152bSBenjamin Marzinski 	r = bd_link_disk_holder(bdev, dm_disk(md));
72186f1152bSBenjamin Marzinski 	if (r) {
72286f1152bSBenjamin Marzinski 		blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
72386f1152bSBenjamin Marzinski 		return r;
72486f1152bSBenjamin Marzinski 	}
72586f1152bSBenjamin Marzinski 
72686f1152bSBenjamin Marzinski 	td->dm_dev.bdev = bdev;
72786f1152bSBenjamin Marzinski 	return 0;
72886f1152bSBenjamin Marzinski }
72986f1152bSBenjamin Marzinski 
73086f1152bSBenjamin Marzinski /*
73186f1152bSBenjamin Marzinski  * Close a table device that we've been using.
73286f1152bSBenjamin Marzinski  */
73386f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md)
73486f1152bSBenjamin Marzinski {
73586f1152bSBenjamin Marzinski 	if (!td->dm_dev.bdev)
73686f1152bSBenjamin Marzinski 		return;
73786f1152bSBenjamin Marzinski 
73886f1152bSBenjamin Marzinski 	bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
73986f1152bSBenjamin Marzinski 	blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
74086f1152bSBenjamin Marzinski 	td->dm_dev.bdev = NULL;
74186f1152bSBenjamin Marzinski }
74286f1152bSBenjamin Marzinski 
74386f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev,
74486f1152bSBenjamin Marzinski 					      fmode_t mode) {
74586f1152bSBenjamin Marzinski 	struct table_device *td;
74686f1152bSBenjamin Marzinski 
74786f1152bSBenjamin Marzinski 	list_for_each_entry(td, l, list)
74886f1152bSBenjamin Marzinski 		if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
74986f1152bSBenjamin Marzinski 			return td;
75086f1152bSBenjamin Marzinski 
75186f1152bSBenjamin Marzinski 	return NULL;
75286f1152bSBenjamin Marzinski }
75386f1152bSBenjamin Marzinski 
75486f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
75586f1152bSBenjamin Marzinski 			struct dm_dev **result) {
75686f1152bSBenjamin Marzinski 	int r;
75786f1152bSBenjamin Marzinski 	struct table_device *td;
75886f1152bSBenjamin Marzinski 
75986f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
76086f1152bSBenjamin Marzinski 	td = find_table_device(&md->table_devices, dev, mode);
76186f1152bSBenjamin Marzinski 	if (!td) {
76286f1152bSBenjamin Marzinski 		td = kmalloc(sizeof(*td), GFP_KERNEL);
76386f1152bSBenjamin Marzinski 		if (!td) {
76486f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
76586f1152bSBenjamin Marzinski 			return -ENOMEM;
76686f1152bSBenjamin Marzinski 		}
76786f1152bSBenjamin Marzinski 
76886f1152bSBenjamin Marzinski 		td->dm_dev.mode = mode;
76986f1152bSBenjamin Marzinski 		td->dm_dev.bdev = NULL;
77086f1152bSBenjamin Marzinski 
77186f1152bSBenjamin Marzinski 		if ((r = open_table_device(td, dev, md))) {
77286f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
77386f1152bSBenjamin Marzinski 			kfree(td);
77486f1152bSBenjamin Marzinski 			return r;
77586f1152bSBenjamin Marzinski 		}
77686f1152bSBenjamin Marzinski 
77786f1152bSBenjamin Marzinski 		format_dev_t(td->dm_dev.name, dev);
77886f1152bSBenjamin Marzinski 
77986f1152bSBenjamin Marzinski 		atomic_set(&td->count, 0);
78086f1152bSBenjamin Marzinski 		list_add(&td->list, &md->table_devices);
78186f1152bSBenjamin Marzinski 	}
78286f1152bSBenjamin Marzinski 	atomic_inc(&td->count);
78386f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
78486f1152bSBenjamin Marzinski 
78586f1152bSBenjamin Marzinski 	*result = &td->dm_dev;
78686f1152bSBenjamin Marzinski 	return 0;
78786f1152bSBenjamin Marzinski }
78886f1152bSBenjamin Marzinski EXPORT_SYMBOL_GPL(dm_get_table_device);
78986f1152bSBenjamin Marzinski 
79086f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
79186f1152bSBenjamin Marzinski {
79286f1152bSBenjamin Marzinski 	struct table_device *td = container_of(d, struct table_device, dm_dev);
79386f1152bSBenjamin Marzinski 
79486f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
79586f1152bSBenjamin Marzinski 	if (atomic_dec_and_test(&td->count)) {
79686f1152bSBenjamin Marzinski 		close_table_device(td, md);
79786f1152bSBenjamin Marzinski 		list_del(&td->list);
79886f1152bSBenjamin Marzinski 		kfree(td);
79986f1152bSBenjamin Marzinski 	}
80086f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
80186f1152bSBenjamin Marzinski }
80286f1152bSBenjamin Marzinski EXPORT_SYMBOL(dm_put_table_device);
80386f1152bSBenjamin Marzinski 
80486f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices)
80586f1152bSBenjamin Marzinski {
80686f1152bSBenjamin Marzinski 	struct list_head *tmp, *next;
80786f1152bSBenjamin Marzinski 
80886f1152bSBenjamin Marzinski 	list_for_each_safe(tmp, next, devices) {
80986f1152bSBenjamin Marzinski 		struct table_device *td = list_entry(tmp, struct table_device, list);
81086f1152bSBenjamin Marzinski 
81186f1152bSBenjamin Marzinski 		DMWARN("dm_destroy: %s still exists with %d references",
81286f1152bSBenjamin Marzinski 		       td->dm_dev.name, atomic_read(&td->count));
81386f1152bSBenjamin Marzinski 		kfree(td);
81486f1152bSBenjamin Marzinski 	}
81586f1152bSBenjamin Marzinski }
81686f1152bSBenjamin Marzinski 
81786f1152bSBenjamin Marzinski /*
8183ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
8193ac51e74SDarrick J. Wong  */
8203ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
8213ac51e74SDarrick J. Wong {
8223ac51e74SDarrick J. Wong 	*geo = md->geometry;
8233ac51e74SDarrick J. Wong 
8243ac51e74SDarrick J. Wong 	return 0;
8253ac51e74SDarrick J. Wong }
8263ac51e74SDarrick J. Wong 
8273ac51e74SDarrick J. Wong /*
8283ac51e74SDarrick J. Wong  * Set the geometry of a device.
8293ac51e74SDarrick J. Wong  */
8303ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
8313ac51e74SDarrick J. Wong {
8323ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
8333ac51e74SDarrick J. Wong 
8343ac51e74SDarrick J. Wong 	if (geo->start > sz) {
8353ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
8363ac51e74SDarrick J. Wong 		return -EINVAL;
8373ac51e74SDarrick J. Wong 	}
8383ac51e74SDarrick J. Wong 
8393ac51e74SDarrick J. Wong 	md->geometry = *geo;
8403ac51e74SDarrick J. Wong 
8413ac51e74SDarrick J. Wong 	return 0;
8423ac51e74SDarrick J. Wong }
8433ac51e74SDarrick J. Wong 
8441da177e4SLinus Torvalds /*-----------------------------------------------------------------
8451da177e4SLinus Torvalds  * CRUD START:
8461da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
8471da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
8481da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
8491da177e4SLinus Torvalds  *   interests of getting something for people to use I give
8501da177e4SLinus Torvalds  *   you this clearly demarcated crap.
8511da177e4SLinus Torvalds  *---------------------------------------------------------------*/
8521da177e4SLinus Torvalds 
8532e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
8542e93ccc1SKiyoshi Ueda {
8552e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
8562e93ccc1SKiyoshi Ueda }
8572e93ccc1SKiyoshi Ueda 
8581da177e4SLinus Torvalds /*
8591da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
8601da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
8611da177e4SLinus Torvalds  */
862858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
8631da177e4SLinus Torvalds {
8642e93ccc1SKiyoshi Ueda 	unsigned long flags;
865b35f8caaSMilan Broz 	int io_error;
866b35f8caaSMilan Broz 	struct bio *bio;
867b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
8682e93ccc1SKiyoshi Ueda 
8692e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
870f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
871f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
872f88fb981SKiyoshi Ueda 		if (!(io->error > 0 && __noflush_suspending(md)))
8731da177e4SLinus Torvalds 			io->error = error;
874f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
875f88fb981SKiyoshi Ueda 	}
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
8782e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
8792e93ccc1SKiyoshi Ueda 			/*
8802e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
8812e93ccc1SKiyoshi Ueda 			 */
882022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
8836a8736d1STejun Heo 			if (__noflush_suspending(md))
8846a8736d1STejun Heo 				bio_list_add_head(&md->deferred, io->bio);
8856a8736d1STejun Heo 			else
8862e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
8872e93ccc1SKiyoshi Ueda 				io->error = -EIO;
888022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
8892e93ccc1SKiyoshi Ueda 		}
8902e93ccc1SKiyoshi Ueda 
891b35f8caaSMilan Broz 		io_error = io->error;
892b35f8caaSMilan Broz 		bio = io->bio;
893af7e466aSMikulas Patocka 		end_io_acct(io);
894a97f925aSMikulas Patocka 		free_io(md, io);
8951da177e4SLinus Torvalds 
8966a8736d1STejun Heo 		if (io_error == DM_ENDIO_REQUEUE)
8976a8736d1STejun Heo 			return;
8986a8736d1STejun Heo 
8994f024f37SKent Overstreet 		if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
9001da177e4SLinus Torvalds 			/*
9016a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
9026a8736d1STejun Heo 			 * without REQ_FLUSH.
9031da177e4SLinus Torvalds 			 */
9046a8736d1STejun Heo 			bio->bi_rw &= ~REQ_FLUSH;
9056a8736d1STejun Heo 			queue_io(md, bio);
9065f3ea37cSArnaldo Carvalho de Melo 		} else {
907b372d360SMike Snitzer 			/* done with normal IO or empty flush */
9080a82a8d1SLinus Torvalds 			trace_block_bio_complete(md->queue, bio, io_error);
909b35f8caaSMilan Broz 			bio_endio(bio, io_error);
9102e93ccc1SKiyoshi Ueda 		}
9111da177e4SLinus Torvalds 	}
912af7e466aSMikulas Patocka }
9131da177e4SLinus Torvalds 
9147eee4ae2SMike Snitzer static void disable_write_same(struct mapped_device *md)
9157eee4ae2SMike Snitzer {
9167eee4ae2SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
9177eee4ae2SMike Snitzer 
9187eee4ae2SMike Snitzer 	/* device doesn't really support WRITE SAME, disable it */
9197eee4ae2SMike Snitzer 	limits->max_write_same_sectors = 0;
9207eee4ae2SMike Snitzer }
9217eee4ae2SMike Snitzer 
9226712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error)
9231da177e4SLinus Torvalds {
9245164beceSzhendong chen 	int r = error;
925bfc6d41cSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
926b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
9279faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
9281da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	if (!bio_flagged(bio, BIO_UPTODATE) && !error)
9311da177e4SLinus Torvalds 		error = -EIO;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	if (endio) {
9347de3ee57SMikulas Patocka 		r = endio(tio->ti, bio, error);
9352e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
9362e93ccc1SKiyoshi Ueda 			/*
9372e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
9382e93ccc1SKiyoshi Ueda 			 * in dec_pending().
9392e93ccc1SKiyoshi Ueda 			 */
9401da177e4SLinus Torvalds 			error = r;
94145cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
94245cbcd79SKiyoshi Ueda 			/* The target will handle the io */
9436712ecf8SNeilBrown 			return;
94445cbcd79SKiyoshi Ueda 		else if (r) {
94545cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
94645cbcd79SKiyoshi Ueda 			BUG();
94745cbcd79SKiyoshi Ueda 		}
9481da177e4SLinus Torvalds 	}
9491da177e4SLinus Torvalds 
9507eee4ae2SMike Snitzer 	if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) &&
9517eee4ae2SMike Snitzer 		     !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
9527eee4ae2SMike Snitzer 		disable_write_same(md);
9537eee4ae2SMike Snitzer 
9549faf400fSStefan Bader 	free_tio(md, tio);
955b35f8caaSMilan Broz 	dec_pending(io, error);
9561da177e4SLinus Torvalds }
9571da177e4SLinus Torvalds 
958cec47e3dSKiyoshi Ueda /*
959cec47e3dSKiyoshi Ueda  * Partial completion handling for request-based dm
960cec47e3dSKiyoshi Ueda  */
961cec47e3dSKiyoshi Ueda static void end_clone_bio(struct bio *clone, int error)
962cec47e3dSKiyoshi Ueda {
963bfc6d41cSMikulas Patocka 	struct dm_rq_clone_bio_info *info =
964bfc6d41cSMikulas Patocka 		container_of(clone, struct dm_rq_clone_bio_info, clone);
965cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = info->tio;
966cec47e3dSKiyoshi Ueda 	struct bio *bio = info->orig;
9674f024f37SKent Overstreet 	unsigned int nr_bytes = info->orig->bi_iter.bi_size;
968cec47e3dSKiyoshi Ueda 
969cec47e3dSKiyoshi Ueda 	bio_put(clone);
970cec47e3dSKiyoshi Ueda 
971cec47e3dSKiyoshi Ueda 	if (tio->error)
972cec47e3dSKiyoshi Ueda 		/*
973cec47e3dSKiyoshi Ueda 		 * An error has already been detected on the request.
974cec47e3dSKiyoshi Ueda 		 * Once error occurred, just let clone->end_io() handle
975cec47e3dSKiyoshi Ueda 		 * the remainder.
976cec47e3dSKiyoshi Ueda 		 */
977cec47e3dSKiyoshi Ueda 		return;
978cec47e3dSKiyoshi Ueda 	else if (error) {
979cec47e3dSKiyoshi Ueda 		/*
980cec47e3dSKiyoshi Ueda 		 * Don't notice the error to the upper layer yet.
981cec47e3dSKiyoshi Ueda 		 * The error handling decision is made by the target driver,
982cec47e3dSKiyoshi Ueda 		 * when the request is completed.
983cec47e3dSKiyoshi Ueda 		 */
984cec47e3dSKiyoshi Ueda 		tio->error = error;
985cec47e3dSKiyoshi Ueda 		return;
986cec47e3dSKiyoshi Ueda 	}
987cec47e3dSKiyoshi Ueda 
988cec47e3dSKiyoshi Ueda 	/*
989cec47e3dSKiyoshi Ueda 	 * I/O for the bio successfully completed.
990cec47e3dSKiyoshi Ueda 	 * Notice the data completion to the upper layer.
991cec47e3dSKiyoshi Ueda 	 */
992cec47e3dSKiyoshi Ueda 
993cec47e3dSKiyoshi Ueda 	/*
994cec47e3dSKiyoshi Ueda 	 * bios are processed from the head of the list.
995cec47e3dSKiyoshi Ueda 	 * So the completing bio should always be rq->bio.
996cec47e3dSKiyoshi Ueda 	 * If it's not, something wrong is happening.
997cec47e3dSKiyoshi Ueda 	 */
998cec47e3dSKiyoshi Ueda 	if (tio->orig->bio != bio)
999cec47e3dSKiyoshi Ueda 		DMERR("bio completion is going in the middle of the request");
1000cec47e3dSKiyoshi Ueda 
1001cec47e3dSKiyoshi Ueda 	/*
1002cec47e3dSKiyoshi Ueda 	 * Update the original request.
1003cec47e3dSKiyoshi Ueda 	 * Do not use blk_end_request() here, because it may complete
1004cec47e3dSKiyoshi Ueda 	 * the original request before the clone, and break the ordering.
1005cec47e3dSKiyoshi Ueda 	 */
1006cec47e3dSKiyoshi Ueda 	blk_update_request(tio->orig, 0, nr_bytes);
1007cec47e3dSKiyoshi Ueda }
1008cec47e3dSKiyoshi Ueda 
1009cec47e3dSKiyoshi Ueda /*
1010cec47e3dSKiyoshi Ueda  * Don't touch any member of the md after calling this function because
1011cec47e3dSKiyoshi Ueda  * the md may be freed in dm_put() at the end of this function.
1012cec47e3dSKiyoshi Ueda  * Or do dm_get() before calling this function and dm_put() later.
1013cec47e3dSKiyoshi Ueda  */
1014b4324feeSKiyoshi Ueda static void rq_completed(struct mapped_device *md, int rw, int run_queue)
1015cec47e3dSKiyoshi Ueda {
1016b4324feeSKiyoshi Ueda 	atomic_dec(&md->pending[rw]);
1017cec47e3dSKiyoshi Ueda 
1018cec47e3dSKiyoshi Ueda 	/* nudge anyone waiting on suspend queue */
1019b4324feeSKiyoshi Ueda 	if (!md_in_flight(md))
1020cec47e3dSKiyoshi Ueda 		wake_up(&md->wait);
1021cec47e3dSKiyoshi Ueda 
1022a8c32a5cSJens Axboe 	/*
1023a8c32a5cSJens Axboe 	 * Run this off this callpath, as drivers could invoke end_io while
1024a8c32a5cSJens Axboe 	 * inside their request_fn (and holding the queue lock). Calling
1025a8c32a5cSJens Axboe 	 * back into ->request_fn() could deadlock attempting to grab the
1026a8c32a5cSJens Axboe 	 * queue lock again.
1027a8c32a5cSJens Axboe 	 */
1028cec47e3dSKiyoshi Ueda 	if (run_queue)
1029a8c32a5cSJens Axboe 		blk_run_queue_async(md->queue);
1030cec47e3dSKiyoshi Ueda 
1031cec47e3dSKiyoshi Ueda 	/*
1032cec47e3dSKiyoshi Ueda 	 * dm_put() must be at the end of this function. See the comment above
1033cec47e3dSKiyoshi Ueda 	 */
1034cec47e3dSKiyoshi Ueda 	dm_put(md);
1035cec47e3dSKiyoshi Ueda }
1036cec47e3dSKiyoshi Ueda 
1037a77e28c7SKiyoshi Ueda static void free_rq_clone(struct request *clone)
1038a77e28c7SKiyoshi Ueda {
1039a77e28c7SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1040a77e28c7SKiyoshi Ueda 
1041a77e28c7SKiyoshi Ueda 	blk_rq_unprep_clone(clone);
1042*1ae49ea2SMike Snitzer 	free_clone_request(tio->md, clone);
1043a77e28c7SKiyoshi Ueda 	free_rq_tio(tio);
1044a77e28c7SKiyoshi Ueda }
1045a77e28c7SKiyoshi Ueda 
1046980691e5SKiyoshi Ueda /*
1047980691e5SKiyoshi Ueda  * Complete the clone and the original request.
1048980691e5SKiyoshi Ueda  * Must be called without queue lock.
1049980691e5SKiyoshi Ueda  */
1050980691e5SKiyoshi Ueda static void dm_end_request(struct request *clone, int error)
1051980691e5SKiyoshi Ueda {
1052980691e5SKiyoshi Ueda 	int rw = rq_data_dir(clone);
1053980691e5SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1054980691e5SKiyoshi Ueda 	struct mapped_device *md = tio->md;
1055980691e5SKiyoshi Ueda 	struct request *rq = tio->orig;
1056980691e5SKiyoshi Ueda 
105729e4013dSTejun Heo 	if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
1058980691e5SKiyoshi Ueda 		rq->errors = clone->errors;
1059980691e5SKiyoshi Ueda 		rq->resid_len = clone->resid_len;
1060980691e5SKiyoshi Ueda 
1061980691e5SKiyoshi Ueda 		if (rq->sense)
1062980691e5SKiyoshi Ueda 			/*
1063980691e5SKiyoshi Ueda 			 * We are using the sense buffer of the original
1064980691e5SKiyoshi Ueda 			 * request.
1065980691e5SKiyoshi Ueda 			 * So setting the length of the sense data is enough.
1066980691e5SKiyoshi Ueda 			 */
1067980691e5SKiyoshi Ueda 			rq->sense_len = clone->sense_len;
1068980691e5SKiyoshi Ueda 	}
1069980691e5SKiyoshi Ueda 
1070980691e5SKiyoshi Ueda 	free_rq_clone(clone);
1071980691e5SKiyoshi Ueda 	blk_end_request_all(rq, error);
107229e4013dSTejun Heo 	rq_completed(md, rw, true);
1073980691e5SKiyoshi Ueda }
1074980691e5SKiyoshi Ueda 
1075cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq)
1076cec47e3dSKiyoshi Ueda {
1077cec47e3dSKiyoshi Ueda 	struct request *clone = rq->special;
1078cec47e3dSKiyoshi Ueda 
1079cec47e3dSKiyoshi Ueda 	rq->special = NULL;
1080cec47e3dSKiyoshi Ueda 	rq->cmd_flags &= ~REQ_DONTPREP;
1081cec47e3dSKiyoshi Ueda 
1082a77e28c7SKiyoshi Ueda 	free_rq_clone(clone);
1083cec47e3dSKiyoshi Ueda }
1084cec47e3dSKiyoshi Ueda 
1085cec47e3dSKiyoshi Ueda /*
1086cec47e3dSKiyoshi Ueda  * Requeue the original request of a clone.
1087cec47e3dSKiyoshi Ueda  */
1088dbf9782cSMike Snitzer static void dm_requeue_unmapped_request(struct request *clone)
1089cec47e3dSKiyoshi Ueda {
1090b4324feeSKiyoshi Ueda 	int rw = rq_data_dir(clone);
1091cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1092cec47e3dSKiyoshi Ueda 	struct mapped_device *md = tio->md;
1093cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
1094cec47e3dSKiyoshi Ueda 	struct request_queue *q = rq->q;
1095cec47e3dSKiyoshi Ueda 	unsigned long flags;
1096cec47e3dSKiyoshi Ueda 
1097cec47e3dSKiyoshi Ueda 	dm_unprep_request(rq);
1098cec47e3dSKiyoshi Ueda 
1099cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
1100cec47e3dSKiyoshi Ueda 	blk_requeue_request(q, rq);
1101cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
1102cec47e3dSKiyoshi Ueda 
1103b4324feeSKiyoshi Ueda 	rq_completed(md, rw, 0);
1104cec47e3dSKiyoshi Ueda }
1105cec47e3dSKiyoshi Ueda 
1106cec47e3dSKiyoshi Ueda static void __stop_queue(struct request_queue *q)
1107cec47e3dSKiyoshi Ueda {
1108cec47e3dSKiyoshi Ueda 	blk_stop_queue(q);
1109cec47e3dSKiyoshi Ueda }
1110cec47e3dSKiyoshi Ueda 
1111cec47e3dSKiyoshi Ueda static void stop_queue(struct request_queue *q)
1112cec47e3dSKiyoshi Ueda {
1113cec47e3dSKiyoshi Ueda 	unsigned long flags;
1114cec47e3dSKiyoshi Ueda 
1115cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
1116cec47e3dSKiyoshi Ueda 	__stop_queue(q);
1117cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
1118cec47e3dSKiyoshi Ueda }
1119cec47e3dSKiyoshi Ueda 
1120cec47e3dSKiyoshi Ueda static void __start_queue(struct request_queue *q)
1121cec47e3dSKiyoshi Ueda {
1122cec47e3dSKiyoshi Ueda 	if (blk_queue_stopped(q))
1123cec47e3dSKiyoshi Ueda 		blk_start_queue(q);
1124cec47e3dSKiyoshi Ueda }
1125cec47e3dSKiyoshi Ueda 
1126cec47e3dSKiyoshi Ueda static void start_queue(struct request_queue *q)
1127cec47e3dSKiyoshi Ueda {
1128cec47e3dSKiyoshi Ueda 	unsigned long flags;
1129cec47e3dSKiyoshi Ueda 
1130cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
1131cec47e3dSKiyoshi Ueda 	__start_queue(q);
1132cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
1133cec47e3dSKiyoshi Ueda }
1134cec47e3dSKiyoshi Ueda 
113511a68244SKiyoshi Ueda static void dm_done(struct request *clone, int error, bool mapped)
113611a68244SKiyoshi Ueda {
113711a68244SKiyoshi Ueda 	int r = error;
113811a68244SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1139ba1cbad9SMike Snitzer 	dm_request_endio_fn rq_end_io = NULL;
1140ba1cbad9SMike Snitzer 
1141ba1cbad9SMike Snitzer 	if (tio->ti) {
1142ba1cbad9SMike Snitzer 		rq_end_io = tio->ti->type->rq_end_io;
114311a68244SKiyoshi Ueda 
114411a68244SKiyoshi Ueda 		if (mapped && rq_end_io)
114511a68244SKiyoshi Ueda 			r = rq_end_io(tio->ti, clone, error, &tio->info);
1146ba1cbad9SMike Snitzer 	}
114711a68244SKiyoshi Ueda 
11487eee4ae2SMike Snitzer 	if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) &&
11497eee4ae2SMike Snitzer 		     !clone->q->limits.max_write_same_sectors))
11507eee4ae2SMike Snitzer 		disable_write_same(tio->md);
11517eee4ae2SMike Snitzer 
115211a68244SKiyoshi Ueda 	if (r <= 0)
115311a68244SKiyoshi Ueda 		/* The target wants to complete the I/O */
115411a68244SKiyoshi Ueda 		dm_end_request(clone, r);
115511a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_INCOMPLETE)
115611a68244SKiyoshi Ueda 		/* The target will handle the I/O */
115711a68244SKiyoshi Ueda 		return;
115811a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_REQUEUE)
115911a68244SKiyoshi Ueda 		/* The target wants to requeue the I/O */
116011a68244SKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
116111a68244SKiyoshi Ueda 	else {
116211a68244SKiyoshi Ueda 		DMWARN("unimplemented target endio return value: %d", r);
116311a68244SKiyoshi Ueda 		BUG();
116411a68244SKiyoshi Ueda 	}
116511a68244SKiyoshi Ueda }
116611a68244SKiyoshi Ueda 
1167cec47e3dSKiyoshi Ueda /*
1168cec47e3dSKiyoshi Ueda  * Request completion handler for request-based dm
1169cec47e3dSKiyoshi Ueda  */
1170cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq)
1171cec47e3dSKiyoshi Ueda {
117211a68244SKiyoshi Ueda 	bool mapped = true;
1173cec47e3dSKiyoshi Ueda 	struct request *clone = rq->completion_data;
1174cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1175cec47e3dSKiyoshi Ueda 
117611a68244SKiyoshi Ueda 	if (rq->cmd_flags & REQ_FAILED)
117711a68244SKiyoshi Ueda 		mapped = false;
1178cec47e3dSKiyoshi Ueda 
117911a68244SKiyoshi Ueda 	dm_done(clone, tio->error, mapped);
1180cec47e3dSKiyoshi Ueda }
1181cec47e3dSKiyoshi Ueda 
1182cec47e3dSKiyoshi Ueda /*
1183cec47e3dSKiyoshi Ueda  * Complete the clone and the original request with the error status
1184cec47e3dSKiyoshi Ueda  * through softirq context.
1185cec47e3dSKiyoshi Ueda  */
1186cec47e3dSKiyoshi Ueda static void dm_complete_request(struct request *clone, int error)
1187cec47e3dSKiyoshi Ueda {
1188cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1189cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
1190cec47e3dSKiyoshi Ueda 
1191cec47e3dSKiyoshi Ueda 	tio->error = error;
1192cec47e3dSKiyoshi Ueda 	rq->completion_data = clone;
1193cec47e3dSKiyoshi Ueda 	blk_complete_request(rq);
1194cec47e3dSKiyoshi Ueda }
1195cec47e3dSKiyoshi Ueda 
1196cec47e3dSKiyoshi Ueda /*
1197cec47e3dSKiyoshi Ueda  * Complete the not-mapped clone and the original request with the error status
1198cec47e3dSKiyoshi Ueda  * through softirq context.
1199cec47e3dSKiyoshi Ueda  * Target's rq_end_io() function isn't called.
1200cec47e3dSKiyoshi Ueda  * This may be used when the target's map_rq() function fails.
1201cec47e3dSKiyoshi Ueda  */
1202dbf9782cSMike Snitzer static void dm_kill_unmapped_request(struct request *clone, int error)
1203cec47e3dSKiyoshi Ueda {
1204cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1205cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
1206cec47e3dSKiyoshi Ueda 
1207cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_FAILED;
1208cec47e3dSKiyoshi Ueda 	dm_complete_request(clone, error);
1209cec47e3dSKiyoshi Ueda }
1210cec47e3dSKiyoshi Ueda 
1211cec47e3dSKiyoshi Ueda /*
1212cec47e3dSKiyoshi Ueda  * Called with the queue lock held
1213cec47e3dSKiyoshi Ueda  */
1214cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error)
1215cec47e3dSKiyoshi Ueda {
1216cec47e3dSKiyoshi Ueda 	/*
1217cec47e3dSKiyoshi Ueda 	 * For just cleaning up the information of the queue in which
1218cec47e3dSKiyoshi Ueda 	 * the clone was dispatched.
1219cec47e3dSKiyoshi Ueda 	 * The clone is *NOT* freed actually here because it is alloced from
1220cec47e3dSKiyoshi Ueda 	 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
1221cec47e3dSKiyoshi Ueda 	 */
1222cec47e3dSKiyoshi Ueda 	__blk_put_request(clone->q, clone);
1223cec47e3dSKiyoshi Ueda 
1224cec47e3dSKiyoshi Ueda 	/*
1225cec47e3dSKiyoshi Ueda 	 * Actual request completion is done in a softirq context which doesn't
1226cec47e3dSKiyoshi Ueda 	 * hold the queue lock.  Otherwise, deadlock could occur because:
1227cec47e3dSKiyoshi Ueda 	 *     - another request may be submitted by the upper level driver
1228cec47e3dSKiyoshi Ueda 	 *       of the stacking during the completion
1229cec47e3dSKiyoshi Ueda 	 *     - the submission which requires queue lock may be done
1230cec47e3dSKiyoshi Ueda 	 *       against this queue
1231cec47e3dSKiyoshi Ueda 	 */
1232cec47e3dSKiyoshi Ueda 	dm_complete_request(clone, error);
1233cec47e3dSKiyoshi Ueda }
1234cec47e3dSKiyoshi Ueda 
123556a67df7SMike Snitzer /*
123656a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
123756a67df7SMike Snitzer  * target boundary.
123856a67df7SMike Snitzer  */
123956a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
12401da177e4SLinus Torvalds {
124156a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
124256a67df7SMike Snitzer 
124356a67df7SMike Snitzer 	return ti->len - target_offset;
124456a67df7SMike Snitzer }
124556a67df7SMike Snitzer 
124656a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
124756a67df7SMike Snitzer {
124856a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
1249542f9038SMike Snitzer 	sector_t offset, max_len;
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds 	/*
12521da177e4SLinus Torvalds 	 * Does the target need to split even further?
12531da177e4SLinus Torvalds 	 */
1254542f9038SMike Snitzer 	if (ti->max_io_len) {
1255542f9038SMike Snitzer 		offset = dm_target_offset(ti, sector);
1256542f9038SMike Snitzer 		if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1257542f9038SMike Snitzer 			max_len = sector_div(offset, ti->max_io_len);
1258542f9038SMike Snitzer 		else
1259542f9038SMike Snitzer 			max_len = offset & (ti->max_io_len - 1);
1260542f9038SMike Snitzer 		max_len = ti->max_io_len - max_len;
1261542f9038SMike Snitzer 
1262542f9038SMike Snitzer 		if (len > max_len)
1263542f9038SMike Snitzer 			len = max_len;
12641da177e4SLinus Torvalds 	}
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 	return len;
12671da177e4SLinus Torvalds }
12681da177e4SLinus Torvalds 
1269542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1270542f9038SMike Snitzer {
1271542f9038SMike Snitzer 	if (len > UINT_MAX) {
1272542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1273542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
1274542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
1275542f9038SMike Snitzer 		return -EINVAL;
1276542f9038SMike Snitzer 	}
1277542f9038SMike Snitzer 
1278542f9038SMike Snitzer 	ti->max_io_len = (uint32_t) len;
1279542f9038SMike Snitzer 
1280542f9038SMike Snitzer 	return 0;
1281542f9038SMike Snitzer }
1282542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1283542f9038SMike Snitzer 
12841dd40c3eSMikulas Patocka /*
12851dd40c3eSMikulas Patocka  * A target may call dm_accept_partial_bio only from the map routine.  It is
12861dd40c3eSMikulas Patocka  * allowed for all bio types except REQ_FLUSH.
12871dd40c3eSMikulas Patocka  *
12881dd40c3eSMikulas Patocka  * dm_accept_partial_bio informs the dm that the target only wants to process
12891dd40c3eSMikulas Patocka  * additional n_sectors sectors of the bio and the rest of the data should be
12901dd40c3eSMikulas Patocka  * sent in a next bio.
12911dd40c3eSMikulas Patocka  *
12921dd40c3eSMikulas Patocka  * A diagram that explains the arithmetics:
12931dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
12941dd40c3eSMikulas Patocka  * |         1          |       2       |   3   |
12951dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
12961dd40c3eSMikulas Patocka  *
12971dd40c3eSMikulas Patocka  * <-------------- *tio->len_ptr --------------->
12981dd40c3eSMikulas Patocka  *                      <------- bi_size ------->
12991dd40c3eSMikulas Patocka  *                      <-- n_sectors -->
13001dd40c3eSMikulas Patocka  *
13011dd40c3eSMikulas Patocka  * Region 1 was already iterated over with bio_advance or similar function.
13021dd40c3eSMikulas Patocka  *	(it may be empty if the target doesn't use bio_advance)
13031dd40c3eSMikulas Patocka  * Region 2 is the remaining bio size that the target wants to process.
13041dd40c3eSMikulas Patocka  *	(it may be empty if region 1 is non-empty, although there is no reason
13051dd40c3eSMikulas Patocka  *	 to make it empty)
13061dd40c3eSMikulas Patocka  * The target requires that region 3 is to be sent in the next bio.
13071dd40c3eSMikulas Patocka  *
13081dd40c3eSMikulas Patocka  * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
13091dd40c3eSMikulas Patocka  * the partially processed part (the sum of regions 1+2) must be the same for all
13101dd40c3eSMikulas Patocka  * copies of the bio.
13111dd40c3eSMikulas Patocka  */
13121dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
13131dd40c3eSMikulas Patocka {
13141dd40c3eSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
13151dd40c3eSMikulas Patocka 	unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
13161dd40c3eSMikulas Patocka 	BUG_ON(bio->bi_rw & REQ_FLUSH);
13171dd40c3eSMikulas Patocka 	BUG_ON(bi_size > *tio->len_ptr);
13181dd40c3eSMikulas Patocka 	BUG_ON(n_sectors > bi_size);
13191dd40c3eSMikulas Patocka 	*tio->len_ptr -= bi_size - n_sectors;
13201dd40c3eSMikulas Patocka 	bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
13211dd40c3eSMikulas Patocka }
13221dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
13231dd40c3eSMikulas Patocka 
1324bd2a49b8SAlasdair G Kergon static void __map_bio(struct dm_target_io *tio)
13251da177e4SLinus Torvalds {
13261da177e4SLinus Torvalds 	int r;
13272056a782SJens Axboe 	sector_t sector;
13289faf400fSStefan Bader 	struct mapped_device *md;
1329dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
1330bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 	/*
13351da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
13361da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
13371da177e4SLinus Torvalds 	 * this io.
13381da177e4SLinus Torvalds 	 */
13391da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
13404f024f37SKent Overstreet 	sector = clone->bi_iter.bi_sector;
13417de3ee57SMikulas Patocka 	r = ti->type->map(ti, clone);
134245cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
13431da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
13442056a782SJens Axboe 
1345d07335e5SMike Snitzer 		trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
134622a7c31aSAlan D. Brunelle 				      tio->io->bio->bi_bdev->bd_dev, sector);
13472056a782SJens Axboe 
13481da177e4SLinus Torvalds 		generic_make_request(clone);
13492e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
13502e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
13519faf400fSStefan Bader 		md = tio->io->md;
13529faf400fSStefan Bader 		dec_pending(tio->io, r);
13539faf400fSStefan Bader 		free_tio(md, tio);
135445cbcd79SKiyoshi Ueda 	} else if (r) {
135545cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
135645cbcd79SKiyoshi Ueda 		BUG();
13571da177e4SLinus Torvalds 	}
13581da177e4SLinus Torvalds }
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds struct clone_info {
13611da177e4SLinus Torvalds 	struct mapped_device *md;
13621da177e4SLinus Torvalds 	struct dm_table *map;
13631da177e4SLinus Torvalds 	struct bio *bio;
13641da177e4SLinus Torvalds 	struct dm_io *io;
13651da177e4SLinus Torvalds 	sector_t sector;
1366e0d6609aSMikulas Patocka 	unsigned sector_count;
13671da177e4SLinus Torvalds };
13681da177e4SLinus Torvalds 
1369e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
1370bd2a49b8SAlasdair G Kergon {
13714f024f37SKent Overstreet 	bio->bi_iter.bi_sector = sector;
13724f024f37SKent Overstreet 	bio->bi_iter.bi_size = to_bytes(len);
13731da177e4SLinus Torvalds }
13741da177e4SLinus Torvalds 
13751da177e4SLinus Torvalds /*
13761da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
13771da177e4SLinus Torvalds  */
1378dba14160SMikulas Patocka static void clone_bio(struct dm_target_io *tio, struct bio *bio,
13791c3b13e6SKent Overstreet 		      sector_t sector, unsigned len)
13801da177e4SLinus Torvalds {
1381dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
13821da177e4SLinus Torvalds 
13831c3b13e6SKent Overstreet 	__bio_clone_fast(clone, bio);
13849c47008dSMartin K. Petersen 
13851c3b13e6SKent Overstreet 	if (bio_integrity(bio))
13861c3b13e6SKent Overstreet 		bio_integrity_clone(clone, bio, GFP_NOIO);
13871c3b13e6SKent Overstreet 
13881c3b13e6SKent Overstreet 	bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
13891c3b13e6SKent Overstreet 	clone->bi_iter.bi_size = to_bytes(len);
13901c3b13e6SKent Overstreet 
13911c3b13e6SKent Overstreet 	if (bio_integrity(bio))
13921c3b13e6SKent Overstreet 		bio_integrity_trim(clone, 0, len);
13931da177e4SLinus Torvalds }
13941da177e4SLinus Torvalds 
13959015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci,
139699778273SJunichi Nomura 				      struct dm_target *ti,
139755a62eefSAlasdair G Kergon 				      unsigned target_bio_nr)
1398f9ab94ceSMikulas Patocka {
1399dba14160SMikulas Patocka 	struct dm_target_io *tio;
1400dba14160SMikulas Patocka 	struct bio *clone;
1401dba14160SMikulas Patocka 
140299778273SJunichi Nomura 	clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1403dba14160SMikulas Patocka 	tio = container_of(clone, struct dm_target_io, clone);
1404f9ab94ceSMikulas Patocka 
1405f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1406f9ab94ceSMikulas Patocka 	tio->ti = ti;
140755a62eefSAlasdair G Kergon 	tio->target_bio_nr = target_bio_nr;
14089015df24SAlasdair G Kergon 
14099015df24SAlasdair G Kergon 	return tio;
14109015df24SAlasdair G Kergon }
14119015df24SAlasdair G Kergon 
141214fe594dSAlasdair G Kergon static void __clone_and_map_simple_bio(struct clone_info *ci,
141314fe594dSAlasdair G Kergon 				       struct dm_target *ti,
14141dd40c3eSMikulas Patocka 				       unsigned target_bio_nr, unsigned *len)
14159015df24SAlasdair G Kergon {
141699778273SJunichi Nomura 	struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
1417dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
14189015df24SAlasdair G Kergon 
14191dd40c3eSMikulas Patocka 	tio->len_ptr = len;
14201dd40c3eSMikulas Patocka 
14211c3b13e6SKent Overstreet 	__bio_clone_fast(clone, ci->bio);
1422bd2a49b8SAlasdair G Kergon 	if (len)
14231dd40c3eSMikulas Patocka 		bio_setup_sector(clone, ci->sector, *len);
1424f9ab94ceSMikulas Patocka 
1425bd2a49b8SAlasdair G Kergon 	__map_bio(tio);
1426f9ab94ceSMikulas Patocka }
1427f9ab94ceSMikulas Patocka 
142814fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
14291dd40c3eSMikulas Patocka 				  unsigned num_bios, unsigned *len)
143006a426ceSMike Snitzer {
143155a62eefSAlasdair G Kergon 	unsigned target_bio_nr;
143206a426ceSMike Snitzer 
143355a62eefSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
143414fe594dSAlasdair G Kergon 		__clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
143506a426ceSMike Snitzer }
143606a426ceSMike Snitzer 
143714fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1438f9ab94ceSMikulas Patocka {
143906a426ceSMike Snitzer 	unsigned target_nr = 0;
1440f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1441f9ab94ceSMikulas Patocka 
1442b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1443f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
14441dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
1445f9ab94ceSMikulas Patocka 
1446f9ab94ceSMikulas Patocka 	return 0;
1447f9ab94ceSMikulas Patocka }
1448f9ab94ceSMikulas Patocka 
1449e4c93811SAlasdair G Kergon static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
14501dd40c3eSMikulas Patocka 				     sector_t sector, unsigned *len)
14515ae89a87SMike Snitzer {
1452dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
14535ae89a87SMike Snitzer 	struct dm_target_io *tio;
1454b0d8ed4dSAlasdair G Kergon 	unsigned target_bio_nr;
1455b0d8ed4dSAlasdair G Kergon 	unsigned num_target_bios = 1;
14565ae89a87SMike Snitzer 
1457b0d8ed4dSAlasdair G Kergon 	/*
1458b0d8ed4dSAlasdair G Kergon 	 * Does the target want to receive duplicate copies of the bio?
1459b0d8ed4dSAlasdair G Kergon 	 */
1460b0d8ed4dSAlasdair G Kergon 	if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1461b0d8ed4dSAlasdair G Kergon 		num_target_bios = ti->num_write_bios(ti, bio);
1462e4c93811SAlasdair G Kergon 
1463b0d8ed4dSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
146499778273SJunichi Nomura 		tio = alloc_tio(ci, ti, target_bio_nr);
14651dd40c3eSMikulas Patocka 		tio->len_ptr = len;
14661dd40c3eSMikulas Patocka 		clone_bio(tio, bio, sector, *len);
1467bd2a49b8SAlasdair G Kergon 		__map_bio(tio);
14685ae89a87SMike Snitzer 	}
1469b0d8ed4dSAlasdair G Kergon }
14705ae89a87SMike Snitzer 
147155a62eefSAlasdair G Kergon typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
147223508a96SMike Snitzer 
147355a62eefSAlasdair G Kergon static unsigned get_num_discard_bios(struct dm_target *ti)
147423508a96SMike Snitzer {
147555a62eefSAlasdair G Kergon 	return ti->num_discard_bios;
147623508a96SMike Snitzer }
147723508a96SMike Snitzer 
147855a62eefSAlasdair G Kergon static unsigned get_num_write_same_bios(struct dm_target *ti)
147923508a96SMike Snitzer {
148055a62eefSAlasdair G Kergon 	return ti->num_write_same_bios;
148123508a96SMike Snitzer }
148223508a96SMike Snitzer 
148323508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti);
148423508a96SMike Snitzer 
148523508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti)
148623508a96SMike Snitzer {
148755a62eefSAlasdair G Kergon 	return ti->split_discard_bios;
148823508a96SMike Snitzer }
148923508a96SMike Snitzer 
149014fe594dSAlasdair G Kergon static int __send_changing_extent_only(struct clone_info *ci,
149155a62eefSAlasdair G Kergon 				       get_num_bios_fn get_num_bios,
149223508a96SMike Snitzer 				       is_split_required_fn is_split_required)
14935ae89a87SMike Snitzer {
14945ae89a87SMike Snitzer 	struct dm_target *ti;
1495e0d6609aSMikulas Patocka 	unsigned len;
149655a62eefSAlasdair G Kergon 	unsigned num_bios;
14975ae89a87SMike Snitzer 
1498a79245b3SMike Snitzer 	do {
14995ae89a87SMike Snitzer 		ti = dm_table_find_target(ci->map, ci->sector);
15005ae89a87SMike Snitzer 		if (!dm_target_is_valid(ti))
15015ae89a87SMike Snitzer 			return -EIO;
15025ae89a87SMike Snitzer 
15035ae89a87SMike Snitzer 		/*
150423508a96SMike Snitzer 		 * Even though the device advertised support for this type of
150523508a96SMike Snitzer 		 * request, that does not mean every target supports it, and
1506936688d7SMike Snitzer 		 * reconfiguration might also have changed that since the
15075ae89a87SMike Snitzer 		 * check was performed.
15085ae89a87SMike Snitzer 		 */
150955a62eefSAlasdair G Kergon 		num_bios = get_num_bios ? get_num_bios(ti) : 0;
151055a62eefSAlasdair G Kergon 		if (!num_bios)
15115ae89a87SMike Snitzer 			return -EOPNOTSUPP;
15125ae89a87SMike Snitzer 
151323508a96SMike Snitzer 		if (is_split_required && !is_split_required(ti))
1514e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
15157acf0277SMikulas Patocka 		else
1516e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
15175ae89a87SMike Snitzer 
15181dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, num_bios, &len);
15195ae89a87SMike Snitzer 
1520a79245b3SMike Snitzer 		ci->sector += len;
1521a79245b3SMike Snitzer 	} while (ci->sector_count -= len);
15225ae89a87SMike Snitzer 
15235ae89a87SMike Snitzer 	return 0;
15245ae89a87SMike Snitzer }
15255ae89a87SMike Snitzer 
152614fe594dSAlasdair G Kergon static int __send_discard(struct clone_info *ci)
152723508a96SMike Snitzer {
152814fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_discard_bios,
152923508a96SMike Snitzer 					   is_split_required_for_discard);
153023508a96SMike Snitzer }
153123508a96SMike Snitzer 
153214fe594dSAlasdair G Kergon static int __send_write_same(struct clone_info *ci)
153323508a96SMike Snitzer {
153414fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
153523508a96SMike Snitzer }
153623508a96SMike Snitzer 
1537e4c93811SAlasdair G Kergon /*
1538e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1539e4c93811SAlasdair G Kergon  */
1540e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci)
1541e4c93811SAlasdair G Kergon {
1542e4c93811SAlasdair G Kergon 	struct bio *bio = ci->bio;
1543e4c93811SAlasdair G Kergon 	struct dm_target *ti;
15441c3b13e6SKent Overstreet 	unsigned len;
1545e4c93811SAlasdair G Kergon 
1546e4c93811SAlasdair G Kergon 	if (unlikely(bio->bi_rw & REQ_DISCARD))
1547e4c93811SAlasdair G Kergon 		return __send_discard(ci);
1548e4c93811SAlasdair G Kergon 	else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
1549e4c93811SAlasdair G Kergon 		return __send_write_same(ci);
1550e4c93811SAlasdair G Kergon 
1551e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1552e4c93811SAlasdair G Kergon 	if (!dm_target_is_valid(ti))
1553e4c93811SAlasdair G Kergon 		return -EIO;
1554e4c93811SAlasdair G Kergon 
15551c3b13e6SKent Overstreet 	len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
1556e4c93811SAlasdair G Kergon 
15571dd40c3eSMikulas Patocka 	__clone_and_map_data_bio(ci, ti, ci->sector, &len);
1558e4c93811SAlasdair G Kergon 
1559e4c93811SAlasdair G Kergon 	ci->sector += len;
1560e4c93811SAlasdair G Kergon 	ci->sector_count -= len;
1561e4c93811SAlasdair G Kergon 
1562e4c93811SAlasdair G Kergon 	return 0;
1563e4c93811SAlasdair G Kergon }
1564e4c93811SAlasdair G Kergon 
1565e4c93811SAlasdair G Kergon /*
156614fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
15671da177e4SLinus Torvalds  */
156883d5e5b0SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md,
156983d5e5b0SMikulas Patocka 				    struct dm_table *map, struct bio *bio)
15701da177e4SLinus Torvalds {
15711da177e4SLinus Torvalds 	struct clone_info ci;
1572512875bdSJun'ichi Nomura 	int error = 0;
15731da177e4SLinus Torvalds 
157483d5e5b0SMikulas Patocka 	if (unlikely(!map)) {
1575f0b9a450SMikulas Patocka 		bio_io_error(bio);
1576f0b9a450SMikulas Patocka 		return;
1577f0b9a450SMikulas Patocka 	}
1578692d0eb9SMikulas Patocka 
157983d5e5b0SMikulas Patocka 	ci.map = map;
15801da177e4SLinus Torvalds 	ci.md = md;
15811da177e4SLinus Torvalds 	ci.io = alloc_io(md);
15821da177e4SLinus Torvalds 	ci.io->error = 0;
15831da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
15841da177e4SLinus Torvalds 	ci.io->bio = bio;
15851da177e4SLinus Torvalds 	ci.io->md = md;
1586f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
15874f024f37SKent Overstreet 	ci.sector = bio->bi_iter.bi_sector;
15881da177e4SLinus Torvalds 
15893eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1590bd2a49b8SAlasdair G Kergon 
1591b372d360SMike Snitzer 	if (bio->bi_rw & REQ_FLUSH) {
1592b372d360SMike Snitzer 		ci.bio = &ci.md->flush_bio;
1593b372d360SMike Snitzer 		ci.sector_count = 0;
159414fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1595b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1596b372d360SMike Snitzer 	} else {
15976a8736d1STejun Heo 		ci.bio = bio;
1598f6fccb12SMilan Broz 		ci.sector_count = bio_sectors(bio);
1599512875bdSJun'ichi Nomura 		while (ci.sector_count && !error)
160014fe594dSAlasdair G Kergon 			error = __split_and_process_non_flush(&ci);
1601d87f4c14STejun Heo 	}
16021da177e4SLinus Torvalds 
16031da177e4SLinus Torvalds 	/* drop the extra reference count */
1604512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
16059e4e5f87SMilan Broz }
16069e4e5f87SMilan Broz /*-----------------------------------------------------------------
16071da177e4SLinus Torvalds  * CRUD END
16081da177e4SLinus Torvalds  *---------------------------------------------------------------*/
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds static int dm_merge_bvec(struct request_queue *q,
16111da177e4SLinus Torvalds 			 struct bvec_merge_data *bvm,
1612f6fccb12SMilan Broz 			 struct bio_vec *biovec)
1613f6fccb12SMilan Broz {
1614f6fccb12SMilan Broz 	struct mapped_device *md = q->queuedata;
161583d5e5b0SMikulas Patocka 	struct dm_table *map = dm_get_live_table_fast(md);
1616f6fccb12SMilan Broz 	struct dm_target *ti;
1617f6fccb12SMilan Broz 	sector_t max_sectors;
1618f6fccb12SMilan Broz 	int max_size = 0;
1619f6fccb12SMilan Broz 
1620f6fccb12SMilan Broz 	if (unlikely(!map))
1621f6fccb12SMilan Broz 		goto out;
1622f6fccb12SMilan Broz 
1623f6fccb12SMilan Broz 	ti = dm_table_find_target(map, bvm->bi_sector);
1624f6fccb12SMilan Broz 	if (!dm_target_is_valid(ti))
162583d5e5b0SMikulas Patocka 		goto out;
1626f6fccb12SMilan Broz 
1627f6fccb12SMilan Broz 	/*
1628f6fccb12SMilan Broz 	 * Find maximum amount of I/O that won't need splitting
1629f6fccb12SMilan Broz 	 */
163056a67df7SMike Snitzer 	max_sectors = min(max_io_len(bvm->bi_sector, ti),
1631148e51baSMike Snitzer 			  (sector_t) queue_max_sectors(q));
1632f6fccb12SMilan Broz 	max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1633148e51baSMike Snitzer 	if (unlikely(max_size < 0)) /* this shouldn't _ever_ happen */
1634f6fccb12SMilan Broz 		max_size = 0;
1635f6fccb12SMilan Broz 
1636f6fccb12SMilan Broz 	/*
1637f6fccb12SMilan Broz 	 * merge_bvec_fn() returns number of bytes
1638f6fccb12SMilan Broz 	 * it can accept at this offset
1639f6fccb12SMilan Broz 	 * max is precomputed maximal io size
1640f6fccb12SMilan Broz 	 */
1641f6fccb12SMilan Broz 	if (max_size && ti->type->merge)
1642f6fccb12SMilan Broz 		max_size = ti->type->merge(ti, bvm, biovec, max_size);
16438cbeb67aSMikulas Patocka 	/*
16448cbeb67aSMikulas Patocka 	 * If the target doesn't support merge method and some of the devices
1645148e51baSMike Snitzer 	 * provided their merge_bvec method (we know this by looking for the
1646148e51baSMike Snitzer 	 * max_hw_sectors that dm_set_device_limits may set), then we can't
1647148e51baSMike Snitzer 	 * allow bios with multiple vector entries.  So always set max_size
1648148e51baSMike Snitzer 	 * to 0, and the code below allows just one page.
16498cbeb67aSMikulas Patocka 	 */
16508cbeb67aSMikulas Patocka 	else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
16518cbeb67aSMikulas Patocka 		max_size = 0;
1652f6fccb12SMilan Broz 
16535037108aSMikulas Patocka out:
165483d5e5b0SMikulas Patocka 	dm_put_live_table_fast(md);
1655f6fccb12SMilan Broz 	/*
1656f6fccb12SMilan Broz 	 * Always allow an entire first page
1657f6fccb12SMilan Broz 	 */
1658f6fccb12SMilan Broz 	if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1659f6fccb12SMilan Broz 		max_size = biovec->bv_len;
1660f6fccb12SMilan Broz 
1661f6fccb12SMilan Broz 	return max_size;
1662f6fccb12SMilan Broz }
1663f6fccb12SMilan Broz 
16641da177e4SLinus Torvalds /*
16651da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
16661da177e4SLinus Torvalds  * dm_merge_bvec.
16671da177e4SLinus Torvalds  */
16685a7bbad2SChristoph Hellwig static void _dm_request(struct request_queue *q, struct bio *bio)
16691da177e4SLinus Torvalds {
167012f03a49SKevin Corry 	int rw = bio_data_dir(bio);
16711da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
167283d5e5b0SMikulas Patocka 	int srcu_idx;
167383d5e5b0SMikulas Patocka 	struct dm_table *map;
16741da177e4SLinus Torvalds 
167583d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
16761da177e4SLinus Torvalds 
167718c0b223SGu Zheng 	generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
167812f03a49SKevin Corry 
16796a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
16806a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
168183d5e5b0SMikulas Patocka 		dm_put_live_table(md, srcu_idx);
16821da177e4SLinus Torvalds 
16836a8736d1STejun Heo 		if (bio_rw(bio) != READA)
168492c63902SMikulas Patocka 			queue_io(md, bio);
16856a8736d1STejun Heo 		else
16866a8736d1STejun Heo 			bio_io_error(bio);
16875a7bbad2SChristoph Hellwig 		return;
16881da177e4SLinus Torvalds 	}
16891da177e4SLinus Torvalds 
169083d5e5b0SMikulas Patocka 	__split_and_process_bio(md, map, bio);
169183d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
16925a7bbad2SChristoph Hellwig 	return;
1693cec47e3dSKiyoshi Ueda }
1694cec47e3dSKiyoshi Ueda 
1695fd2ed4d2SMikulas Patocka int dm_request_based(struct mapped_device *md)
1696cec47e3dSKiyoshi Ueda {
1697cec47e3dSKiyoshi Ueda 	return blk_queue_stackable(md->queue);
1698cec47e3dSKiyoshi Ueda }
1699cec47e3dSKiyoshi Ueda 
17005a7bbad2SChristoph Hellwig static void dm_request(struct request_queue *q, struct bio *bio)
1701cec47e3dSKiyoshi Ueda {
1702cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1703cec47e3dSKiyoshi Ueda 
1704cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
17055a7bbad2SChristoph Hellwig 		blk_queue_bio(q, bio);
17065a7bbad2SChristoph Hellwig 	else
17075a7bbad2SChristoph Hellwig 		_dm_request(q, bio);
1708cec47e3dSKiyoshi Ueda }
1709cec47e3dSKiyoshi Ueda 
1710dbf9782cSMike Snitzer static void dm_dispatch_request(struct request *rq)
1711cec47e3dSKiyoshi Ueda {
1712cec47e3dSKiyoshi Ueda 	int r;
1713cec47e3dSKiyoshi Ueda 
1714cec47e3dSKiyoshi Ueda 	if (blk_queue_io_stat(rq->q))
1715cec47e3dSKiyoshi Ueda 		rq->cmd_flags |= REQ_IO_STAT;
1716cec47e3dSKiyoshi Ueda 
1717cec47e3dSKiyoshi Ueda 	rq->start_time = jiffies;
1718cec47e3dSKiyoshi Ueda 	r = blk_insert_cloned_request(rq->q, rq);
1719cec47e3dSKiyoshi Ueda 	if (r)
1720cec47e3dSKiyoshi Ueda 		dm_complete_request(rq, r);
1721cec47e3dSKiyoshi Ueda }
1722cec47e3dSKiyoshi Ueda 
1723cec47e3dSKiyoshi Ueda static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1724cec47e3dSKiyoshi Ueda 				 void *data)
1725cec47e3dSKiyoshi Ueda {
1726cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = data;
172794818742SKent Overstreet 	struct dm_rq_clone_bio_info *info =
172894818742SKent Overstreet 		container_of(bio, struct dm_rq_clone_bio_info, clone);
1729cec47e3dSKiyoshi Ueda 
1730cec47e3dSKiyoshi Ueda 	info->orig = bio_orig;
1731cec47e3dSKiyoshi Ueda 	info->tio = tio;
1732cec47e3dSKiyoshi Ueda 	bio->bi_end_io = end_clone_bio;
1733cec47e3dSKiyoshi Ueda 
1734cec47e3dSKiyoshi Ueda 	return 0;
1735cec47e3dSKiyoshi Ueda }
1736cec47e3dSKiyoshi Ueda 
1737cec47e3dSKiyoshi Ueda static int setup_clone(struct request *clone, struct request *rq,
1738*1ae49ea2SMike Snitzer 		       struct dm_rq_target_io *tio, gfp_t gfp_mask)
1739cec47e3dSKiyoshi Ueda {
1740d0bcb878SKiyoshi Ueda 	int r;
1741cec47e3dSKiyoshi Ueda 
1742*1ae49ea2SMike Snitzer 	r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask,
1743d0bcb878SKiyoshi Ueda 			      dm_rq_bio_constructor, tio);
1744cec47e3dSKiyoshi Ueda 	if (r)
1745cec47e3dSKiyoshi Ueda 		return r;
1746cec47e3dSKiyoshi Ueda 
1747cec47e3dSKiyoshi Ueda 	clone->cmd = rq->cmd;
1748cec47e3dSKiyoshi Ueda 	clone->cmd_len = rq->cmd_len;
1749cec47e3dSKiyoshi Ueda 	clone->sense = rq->sense;
1750cec47e3dSKiyoshi Ueda 	clone->end_io = end_clone_request;
1751cec47e3dSKiyoshi Ueda 	clone->end_io_data = tio;
1752cec47e3dSKiyoshi Ueda 
1753*1ae49ea2SMike Snitzer 	tio->clone = clone;
1754*1ae49ea2SMike Snitzer 
1755cec47e3dSKiyoshi Ueda 	return 0;
1756cec47e3dSKiyoshi Ueda }
1757cec47e3dSKiyoshi Ueda 
1758*1ae49ea2SMike Snitzer static struct request *__clone_rq(struct request *rq, struct mapped_device *md,
1759*1ae49ea2SMike Snitzer 				  struct dm_rq_target_io *tio, gfp_t gfp_mask)
1760*1ae49ea2SMike Snitzer {
1761*1ae49ea2SMike Snitzer 	struct request *clone = alloc_clone_request(md, gfp_mask);
1762*1ae49ea2SMike Snitzer 
1763*1ae49ea2SMike Snitzer 	if (!clone)
1764*1ae49ea2SMike Snitzer 		return NULL;
1765*1ae49ea2SMike Snitzer 
1766*1ae49ea2SMike Snitzer 	blk_rq_init(NULL, clone);
1767*1ae49ea2SMike Snitzer 	if (setup_clone(clone, rq, tio, gfp_mask)) {
1768*1ae49ea2SMike Snitzer 		/* -ENOMEM */
1769*1ae49ea2SMike Snitzer 		free_clone_request(md, clone);
1770*1ae49ea2SMike Snitzer 		return NULL;
1771*1ae49ea2SMike Snitzer 	}
1772*1ae49ea2SMike Snitzer 
1773*1ae49ea2SMike Snitzer 	return clone;
1774*1ae49ea2SMike Snitzer }
1775*1ae49ea2SMike Snitzer 
17766facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md,
17776facdaffSKiyoshi Ueda 				gfp_t gfp_mask)
17786facdaffSKiyoshi Ueda {
17796facdaffSKiyoshi Ueda 	struct request *clone;
17806facdaffSKiyoshi Ueda 	struct dm_rq_target_io *tio;
17816facdaffSKiyoshi Ueda 
17826facdaffSKiyoshi Ueda 	tio = alloc_rq_tio(md, gfp_mask);
17836facdaffSKiyoshi Ueda 	if (!tio)
17846facdaffSKiyoshi Ueda 		return NULL;
17856facdaffSKiyoshi Ueda 
17866facdaffSKiyoshi Ueda 	tio->md = md;
17876facdaffSKiyoshi Ueda 	tio->ti = NULL;
1788*1ae49ea2SMike Snitzer 	tio->clone = NULL;
17896facdaffSKiyoshi Ueda 	tio->orig = rq;
17906facdaffSKiyoshi Ueda 	tio->error = 0;
17916facdaffSKiyoshi Ueda 	memset(&tio->info, 0, sizeof(tio->info));
17926facdaffSKiyoshi Ueda 
1793*1ae49ea2SMike Snitzer 	clone = __clone_rq(rq, md, tio, GFP_ATOMIC);
1794*1ae49ea2SMike Snitzer 	if (!clone) {
17956facdaffSKiyoshi Ueda 		free_rq_tio(tio);
17966facdaffSKiyoshi Ueda 		return NULL;
17976facdaffSKiyoshi Ueda 	}
17986facdaffSKiyoshi Ueda 
17996facdaffSKiyoshi Ueda 	return clone;
18006facdaffSKiyoshi Ueda }
18016facdaffSKiyoshi Ueda 
1802cec47e3dSKiyoshi Ueda /*
1803cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1804cec47e3dSKiyoshi Ueda  */
1805cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq)
1806cec47e3dSKiyoshi Ueda {
1807cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1808cec47e3dSKiyoshi Ueda 	struct request *clone;
1809cec47e3dSKiyoshi Ueda 
1810cec47e3dSKiyoshi Ueda 	if (unlikely(rq->special)) {
1811cec47e3dSKiyoshi Ueda 		DMWARN("Already has something in rq->special.");
1812cec47e3dSKiyoshi Ueda 		return BLKPREP_KILL;
1813cec47e3dSKiyoshi Ueda 	}
1814cec47e3dSKiyoshi Ueda 
18156facdaffSKiyoshi Ueda 	clone = clone_rq(rq, md, GFP_ATOMIC);
18166facdaffSKiyoshi Ueda 	if (!clone)
1817cec47e3dSKiyoshi Ueda 		return BLKPREP_DEFER;
1818cec47e3dSKiyoshi Ueda 
1819cec47e3dSKiyoshi Ueda 	rq->special = clone;
1820cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_DONTPREP;
1821cec47e3dSKiyoshi Ueda 
1822cec47e3dSKiyoshi Ueda 	return BLKPREP_OK;
1823cec47e3dSKiyoshi Ueda }
1824cec47e3dSKiyoshi Ueda 
18259eef87daSKiyoshi Ueda /*
18269eef87daSKiyoshi Ueda  * Returns:
18279eef87daSKiyoshi Ueda  * 0  : the request has been processed (not requeued)
18289eef87daSKiyoshi Ueda  * !0 : the request has been requeued
18299eef87daSKiyoshi Ueda  */
18309eef87daSKiyoshi Ueda static int map_request(struct dm_target *ti, struct request *clone,
1831cec47e3dSKiyoshi Ueda 		       struct mapped_device *md)
1832cec47e3dSKiyoshi Ueda {
18339eef87daSKiyoshi Ueda 	int r, requeued = 0;
1834cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1835cec47e3dSKiyoshi Ueda 
1836cec47e3dSKiyoshi Ueda 	tio->ti = ti;
1837cec47e3dSKiyoshi Ueda 	r = ti->type->map_rq(ti, clone, &tio->info);
1838cec47e3dSKiyoshi Ueda 	switch (r) {
1839cec47e3dSKiyoshi Ueda 	case DM_MAPIO_SUBMITTED:
1840cec47e3dSKiyoshi Ueda 		/* The target has taken the I/O to submit by itself later */
1841cec47e3dSKiyoshi Ueda 		break;
1842cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REMAPPED:
1843cec47e3dSKiyoshi Ueda 		/* The target has remapped the I/O so dispatch it */
18446db4ccd6SJun'ichi Nomura 		trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
18456db4ccd6SJun'ichi Nomura 				     blk_rq_pos(tio->orig));
1846cec47e3dSKiyoshi Ueda 		dm_dispatch_request(clone);
1847cec47e3dSKiyoshi Ueda 		break;
1848cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REQUEUE:
1849cec47e3dSKiyoshi Ueda 		/* The target wants to requeue the I/O */
1850cec47e3dSKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
18519eef87daSKiyoshi Ueda 		requeued = 1;
1852cec47e3dSKiyoshi Ueda 		break;
1853cec47e3dSKiyoshi Ueda 	default:
1854cec47e3dSKiyoshi Ueda 		if (r > 0) {
1855cec47e3dSKiyoshi Ueda 			DMWARN("unimplemented target map return value: %d", r);
1856cec47e3dSKiyoshi Ueda 			BUG();
1857cec47e3dSKiyoshi Ueda 		}
1858cec47e3dSKiyoshi Ueda 
1859cec47e3dSKiyoshi Ueda 		/* The target wants to complete the I/O */
1860cec47e3dSKiyoshi Ueda 		dm_kill_unmapped_request(clone, r);
1861cec47e3dSKiyoshi Ueda 		break;
1862cec47e3dSKiyoshi Ueda 	}
18639eef87daSKiyoshi Ueda 
18649eef87daSKiyoshi Ueda 	return requeued;
1865cec47e3dSKiyoshi Ueda }
1866cec47e3dSKiyoshi Ueda 
1867ba1cbad9SMike Snitzer static struct request *dm_start_request(struct mapped_device *md, struct request *orig)
1868ba1cbad9SMike Snitzer {
1869ba1cbad9SMike Snitzer 	struct request *clone;
1870ba1cbad9SMike Snitzer 
1871ba1cbad9SMike Snitzer 	blk_start_request(orig);
1872ba1cbad9SMike Snitzer 	clone = orig->special;
1873ba1cbad9SMike Snitzer 	atomic_inc(&md->pending[rq_data_dir(clone)]);
1874ba1cbad9SMike Snitzer 
1875ba1cbad9SMike Snitzer 	/*
1876ba1cbad9SMike Snitzer 	 * Hold the md reference here for the in-flight I/O.
1877ba1cbad9SMike Snitzer 	 * We can't rely on the reference count by device opener,
1878ba1cbad9SMike Snitzer 	 * because the device may be closed during the request completion
1879ba1cbad9SMike Snitzer 	 * when all bios are completed.
1880ba1cbad9SMike Snitzer 	 * See the comment in rq_completed() too.
1881ba1cbad9SMike Snitzer 	 */
1882ba1cbad9SMike Snitzer 	dm_get(md);
1883ba1cbad9SMike Snitzer 
1884ba1cbad9SMike Snitzer 	return clone;
1885ba1cbad9SMike Snitzer }
1886ba1cbad9SMike Snitzer 
1887cec47e3dSKiyoshi Ueda /*
1888cec47e3dSKiyoshi Ueda  * q->request_fn for request-based dm.
1889cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1890cec47e3dSKiyoshi Ueda  */
1891cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q)
1892cec47e3dSKiyoshi Ueda {
1893cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
189483d5e5b0SMikulas Patocka 	int srcu_idx;
189583d5e5b0SMikulas Patocka 	struct dm_table *map = dm_get_live_table(md, &srcu_idx);
1896cec47e3dSKiyoshi Ueda 	struct dm_target *ti;
1897b4324feeSKiyoshi Ueda 	struct request *rq, *clone;
189829e4013dSTejun Heo 	sector_t pos;
1899cec47e3dSKiyoshi Ueda 
1900cec47e3dSKiyoshi Ueda 	/*
1901b4324feeSKiyoshi Ueda 	 * For suspend, check blk_queue_stopped() and increment
1902b4324feeSKiyoshi Ueda 	 * ->pending within a single queue_lock not to increment the
1903b4324feeSKiyoshi Ueda 	 * number of in-flight I/Os after the queue is stopped in
1904b4324feeSKiyoshi Ueda 	 * dm_suspend().
1905cec47e3dSKiyoshi Ueda 	 */
19067eaceaccSJens Axboe 	while (!blk_queue_stopped(q)) {
1907cec47e3dSKiyoshi Ueda 		rq = blk_peek_request(q);
1908cec47e3dSKiyoshi Ueda 		if (!rq)
19097eaceaccSJens Axboe 			goto delay_and_out;
1910cec47e3dSKiyoshi Ueda 
191129e4013dSTejun Heo 		/* always use block 0 to find the target for flushes for now */
191229e4013dSTejun Heo 		pos = 0;
191329e4013dSTejun Heo 		if (!(rq->cmd_flags & REQ_FLUSH))
191429e4013dSTejun Heo 			pos = blk_rq_pos(rq);
1915d0bcb878SKiyoshi Ueda 
191629e4013dSTejun Heo 		ti = dm_table_find_target(map, pos);
1917ba1cbad9SMike Snitzer 		if (!dm_target_is_valid(ti)) {
1918ba1cbad9SMike Snitzer 			/*
1919ba1cbad9SMike Snitzer 			 * Must perform setup, that dm_done() requires,
1920ba1cbad9SMike Snitzer 			 * before calling dm_kill_unmapped_request
1921ba1cbad9SMike Snitzer 			 */
1922ba1cbad9SMike Snitzer 			DMERR_LIMIT("request attempted access beyond the end of device");
1923ba1cbad9SMike Snitzer 			clone = dm_start_request(md, rq);
1924ba1cbad9SMike Snitzer 			dm_kill_unmapped_request(clone, -EIO);
1925ba1cbad9SMike Snitzer 			continue;
1926ba1cbad9SMike Snitzer 		}
192729e4013dSTejun Heo 
1928cec47e3dSKiyoshi Ueda 		if (ti->type->busy && ti->type->busy(ti))
19297eaceaccSJens Axboe 			goto delay_and_out;
1930cec47e3dSKiyoshi Ueda 
1931ba1cbad9SMike Snitzer 		clone = dm_start_request(md, rq);
1932b4324feeSKiyoshi Ueda 
1933cec47e3dSKiyoshi Ueda 		spin_unlock(q->queue_lock);
19349eef87daSKiyoshi Ueda 		if (map_request(ti, clone, md))
19359eef87daSKiyoshi Ueda 			goto requeued;
19369eef87daSKiyoshi Ueda 
1937052189a2SKiyoshi Ueda 		BUG_ON(!irqs_disabled());
1938052189a2SKiyoshi Ueda 		spin_lock(q->queue_lock);
1939cec47e3dSKiyoshi Ueda 	}
1940cec47e3dSKiyoshi Ueda 
1941cec47e3dSKiyoshi Ueda 	goto out;
1942cec47e3dSKiyoshi Ueda 
19439eef87daSKiyoshi Ueda requeued:
1944052189a2SKiyoshi Ueda 	BUG_ON(!irqs_disabled());
1945052189a2SKiyoshi Ueda 	spin_lock(q->queue_lock);
19469eef87daSKiyoshi Ueda 
19477eaceaccSJens Axboe delay_and_out:
19487eaceaccSJens Axboe 	blk_delay_queue(q, HZ / 10);
1949cec47e3dSKiyoshi Ueda out:
195083d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
1951cec47e3dSKiyoshi Ueda }
1952cec47e3dSKiyoshi Ueda 
1953cec47e3dSKiyoshi Ueda int dm_underlying_device_busy(struct request_queue *q)
1954cec47e3dSKiyoshi Ueda {
1955cec47e3dSKiyoshi Ueda 	return blk_lld_busy(q);
1956cec47e3dSKiyoshi Ueda }
1957cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1958cec47e3dSKiyoshi Ueda 
1959cec47e3dSKiyoshi Ueda static int dm_lld_busy(struct request_queue *q)
1960cec47e3dSKiyoshi Ueda {
1961cec47e3dSKiyoshi Ueda 	int r;
1962cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
196383d5e5b0SMikulas Patocka 	struct dm_table *map = dm_get_live_table_fast(md);
1964cec47e3dSKiyoshi Ueda 
1965cec47e3dSKiyoshi Ueda 	if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1966cec47e3dSKiyoshi Ueda 		r = 1;
1967cec47e3dSKiyoshi Ueda 	else
1968cec47e3dSKiyoshi Ueda 		r = dm_table_any_busy_target(map);
1969cec47e3dSKiyoshi Ueda 
197083d5e5b0SMikulas Patocka 	dm_put_live_table_fast(md);
1971cec47e3dSKiyoshi Ueda 
1972cec47e3dSKiyoshi Ueda 	return r;
1973cec47e3dSKiyoshi Ueda }
1974cec47e3dSKiyoshi Ueda 
19751da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
19761da177e4SLinus Torvalds {
19778a57dfc6SChandra Seetharaman 	int r = bdi_bits;
19788a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
19798a57dfc6SChandra Seetharaman 	struct dm_table *map;
19801da177e4SLinus Torvalds 
19811eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
198283d5e5b0SMikulas Patocka 		map = dm_get_live_table_fast(md);
19838a57dfc6SChandra Seetharaman 		if (map) {
1984cec47e3dSKiyoshi Ueda 			/*
1985cec47e3dSKiyoshi Ueda 			 * Request-based dm cares about only own queue for
1986cec47e3dSKiyoshi Ueda 			 * the query about congestion status of request_queue
1987cec47e3dSKiyoshi Ueda 			 */
1988cec47e3dSKiyoshi Ueda 			if (dm_request_based(md))
1989cec47e3dSKiyoshi Ueda 				r = md->queue->backing_dev_info.state &
1990cec47e3dSKiyoshi Ueda 				    bdi_bits;
1991cec47e3dSKiyoshi Ueda 			else
19921da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
19938a57dfc6SChandra Seetharaman 		}
199483d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
19958a57dfc6SChandra Seetharaman 	}
19968a57dfc6SChandra Seetharaman 
19971da177e4SLinus Torvalds 	return r;
19981da177e4SLinus Torvalds }
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds /*-----------------------------------------------------------------
20011da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
20021da177e4SLinus Torvalds  *---------------------------------------------------------------*/
20032b06cfffSAlasdair G Kergon static void free_minor(int minor)
20041da177e4SLinus Torvalds {
2005f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
20061da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
2007f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
20081da177e4SLinus Torvalds }
20091da177e4SLinus Torvalds 
20101da177e4SLinus Torvalds /*
20111da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
20121da177e4SLinus Torvalds  */
2013cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
20141da177e4SLinus Torvalds {
2015c9d76be6STejun Heo 	int r;
20161da177e4SLinus Torvalds 
20171da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
20181da177e4SLinus Torvalds 		return -EINVAL;
20191da177e4SLinus Torvalds 
2020c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
2021f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
20221da177e4SLinus Torvalds 
2023c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
20241da177e4SLinus Torvalds 
2025f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2026c9d76be6STejun Heo 	idr_preload_end();
2027c9d76be6STejun Heo 	if (r < 0)
2028c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
2029c9d76be6STejun Heo 	return 0;
20301da177e4SLinus Torvalds }
20311da177e4SLinus Torvalds 
2032cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
20331da177e4SLinus Torvalds {
2034c9d76be6STejun Heo 	int r;
20351da177e4SLinus Torvalds 
2036c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
2037f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
20381da177e4SLinus Torvalds 
2039c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
20401da177e4SLinus Torvalds 
2041f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2042c9d76be6STejun Heo 	idr_preload_end();
2043c9d76be6STejun Heo 	if (r < 0)
20441da177e4SLinus Torvalds 		return r;
2045c9d76be6STejun Heo 	*minor = r;
2046c9d76be6STejun Heo 	return 0;
20471da177e4SLinus Torvalds }
20481da177e4SLinus Torvalds 
204983d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
20501da177e4SLinus Torvalds 
205153d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
205253d5914fSMikulas Patocka 
20534a0b4ddfSMike Snitzer static void dm_init_md_queue(struct mapped_device *md)
20544a0b4ddfSMike Snitzer {
20554a0b4ddfSMike Snitzer 	/*
20564a0b4ddfSMike Snitzer 	 * Request-based dm devices cannot be stacked on top of bio-based dm
20574a0b4ddfSMike Snitzer 	 * devices.  The type of this dm device has not been decided yet.
20584a0b4ddfSMike Snitzer 	 * The type is decided at the first table loading time.
20594a0b4ddfSMike Snitzer 	 * To prevent problematic device stacking, clear the queue flag
20604a0b4ddfSMike Snitzer 	 * for request stacking support until then.
20614a0b4ddfSMike Snitzer 	 *
20624a0b4ddfSMike Snitzer 	 * This queue is new, so no concurrency on the queue_flags.
20634a0b4ddfSMike Snitzer 	 */
20644a0b4ddfSMike Snitzer 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
20654a0b4ddfSMike Snitzer 
20664a0b4ddfSMike Snitzer 	md->queue->queuedata = md;
20674a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
20684a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_data = md;
20694a0b4ddfSMike Snitzer 	blk_queue_make_request(md->queue, dm_request);
20704a0b4ddfSMike Snitzer 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
20714a0b4ddfSMike Snitzer 	blk_queue_merge_bvec(md->queue, dm_merge_bvec);
20724a0b4ddfSMike Snitzer }
20734a0b4ddfSMike Snitzer 
20741da177e4SLinus Torvalds /*
20751da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
20761da177e4SLinus Torvalds  */
20772b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
20781da177e4SLinus Torvalds {
20791da177e4SLinus Torvalds 	int r;
2080cf13ab8eSFrederik Deweerdt 	struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
2081ba61fdd1SJeff Mahoney 	void *old_md;
20821da177e4SLinus Torvalds 
20831da177e4SLinus Torvalds 	if (!md) {
20841da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
20851da177e4SLinus Torvalds 		return NULL;
20861da177e4SLinus Torvalds 	}
20871da177e4SLinus Torvalds 
208810da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
20896ed7ade8SMilan Broz 		goto bad_module_get;
209010da4f79SJeff Mahoney 
20911da177e4SLinus Torvalds 	/* get a minor number for the dev */
20922b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
2093cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
20942b06cfffSAlasdair G Kergon 	else
2095cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
20961da177e4SLinus Torvalds 	if (r < 0)
20976ed7ade8SMilan Broz 		goto bad_minor;
20981da177e4SLinus Torvalds 
209983d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
210083d5e5b0SMikulas Patocka 	if (r < 0)
210183d5e5b0SMikulas Patocka 		goto bad_io_barrier;
210283d5e5b0SMikulas Patocka 
2103a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
2104e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
2105a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
210686f1152bSBenjamin Marzinski 	mutex_init(&md->table_devices_lock);
2107022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
21081da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
21095c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
21101da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
21117a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
21127a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
211386f1152bSBenjamin Marzinski 	INIT_LIST_HEAD(&md->table_devices);
21147a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
21151da177e4SLinus Torvalds 
21164a0b4ddfSMike Snitzer 	md->queue = blk_alloc_queue(GFP_KERNEL);
21171da177e4SLinus Torvalds 	if (!md->queue)
21186ed7ade8SMilan Broz 		goto bad_queue;
21191da177e4SLinus Torvalds 
21204a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
21219faf400fSStefan Bader 
21221da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
21231da177e4SLinus Torvalds 	if (!md->disk)
21246ed7ade8SMilan Broz 		goto bad_disk;
21251da177e4SLinus Torvalds 
2126316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
2127316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
2128f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
212953d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
2130f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
21312995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
2132f0b04115SJeff Mahoney 
21331da177e4SLinus Torvalds 	md->disk->major = _major;
21341da177e4SLinus Torvalds 	md->disk->first_minor = minor;
21351da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
21361da177e4SLinus Torvalds 	md->disk->queue = md->queue;
21371da177e4SLinus Torvalds 	md->disk->private_data = md;
21381da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
21391da177e4SLinus Torvalds 	add_disk(md->disk);
21407e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
21411da177e4SLinus Torvalds 
2142670368a8STejun Heo 	md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
2143304f3f6aSMilan Broz 	if (!md->wq)
2144304f3f6aSMilan Broz 		goto bad_thread;
2145304f3f6aSMilan Broz 
214632a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
214732a926daSMikulas Patocka 	if (!md->bdev)
214832a926daSMikulas Patocka 		goto bad_bdev;
214932a926daSMikulas Patocka 
21506a8736d1STejun Heo 	bio_init(&md->flush_bio);
21516a8736d1STejun Heo 	md->flush_bio.bi_bdev = md->bdev;
21526a8736d1STejun Heo 	md->flush_bio.bi_rw = WRITE_FLUSH;
21536a8736d1STejun Heo 
2154fd2ed4d2SMikulas Patocka 	dm_stats_init(&md->stats);
2155fd2ed4d2SMikulas Patocka 
2156ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
2157f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
2158ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
2159f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2160ba61fdd1SJeff Mahoney 
2161ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
2162ba61fdd1SJeff Mahoney 
21631da177e4SLinus Torvalds 	return md;
21641da177e4SLinus Torvalds 
216532a926daSMikulas Patocka bad_bdev:
216632a926daSMikulas Patocka 	destroy_workqueue(md->wq);
2167304f3f6aSMilan Broz bad_thread:
216803022c54SZdenek Kabelac 	del_gendisk(md->disk);
2169304f3f6aSMilan Broz 	put_disk(md->disk);
21706ed7ade8SMilan Broz bad_disk:
21711312f40eSAl Viro 	blk_cleanup_queue(md->queue);
21726ed7ade8SMilan Broz bad_queue:
217383d5e5b0SMikulas Patocka 	cleanup_srcu_struct(&md->io_barrier);
217483d5e5b0SMikulas Patocka bad_io_barrier:
21751da177e4SLinus Torvalds 	free_minor(minor);
21766ed7ade8SMilan Broz bad_minor:
217710da4f79SJeff Mahoney 	module_put(THIS_MODULE);
21786ed7ade8SMilan Broz bad_module_get:
21791da177e4SLinus Torvalds 	kfree(md);
21801da177e4SLinus Torvalds 	return NULL;
21811da177e4SLinus Torvalds }
21821da177e4SLinus Torvalds 
2183ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
2184ae9da83fSJun'ichi Nomura 
21851da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
21861da177e4SLinus Torvalds {
2187f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
218863d94e48SJun'ichi Nomura 
2189ae9da83fSJun'ichi Nomura 	unlock_fs(md);
2190db8fef4fSMikulas Patocka 	bdput(md->bdev);
2191304f3f6aSMilan Broz 	destroy_workqueue(md->wq);
2192e6ee8c0bSKiyoshi Ueda 	if (md->io_pool)
21931da177e4SLinus Torvalds 		mempool_destroy(md->io_pool);
2194*1ae49ea2SMike Snitzer 	if (md->rq_pool)
2195*1ae49ea2SMike Snitzer 		mempool_destroy(md->rq_pool);
2196e6ee8c0bSKiyoshi Ueda 	if (md->bs)
21979faf400fSStefan Bader 		bioset_free(md->bs);
21989c47008dSMartin K. Petersen 	blk_integrity_unregister(md->disk);
21991da177e4SLinus Torvalds 	del_gendisk(md->disk);
220083d5e5b0SMikulas Patocka 	cleanup_srcu_struct(&md->io_barrier);
220186f1152bSBenjamin Marzinski 	free_table_devices(&md->table_devices);
220263d94e48SJun'ichi Nomura 	free_minor(minor);
2203fba9f90eSJeff Mahoney 
2204fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
2205fba9f90eSJeff Mahoney 	md->disk->private_data = NULL;
2206fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
2207fba9f90eSJeff Mahoney 
22081da177e4SLinus Torvalds 	put_disk(md->disk);
22091312f40eSAl Viro 	blk_cleanup_queue(md->queue);
2210fd2ed4d2SMikulas Patocka 	dm_stats_cleanup(&md->stats);
221110da4f79SJeff Mahoney 	module_put(THIS_MODULE);
22121da177e4SLinus Torvalds 	kfree(md);
22131da177e4SLinus Torvalds }
22141da177e4SLinus Torvalds 
2215e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2216e6ee8c0bSKiyoshi Ueda {
2217c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
2218e6ee8c0bSKiyoshi Ueda 
22195f015204SJun'ichi Nomura 	if (md->io_pool && md->bs) {
222016245bdcSJun'ichi Nomura 		/* The md already has necessary mempools. */
222116245bdcSJun'ichi Nomura 		if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
2222c0820cf5SMikulas Patocka 			/*
222316245bdcSJun'ichi Nomura 			 * Reload bioset because front_pad may have changed
222416245bdcSJun'ichi Nomura 			 * because a different table was loaded.
2225c0820cf5SMikulas Patocka 			 */
2226c0820cf5SMikulas Patocka 			bioset_free(md->bs);
2227c0820cf5SMikulas Patocka 			md->bs = p->bs;
2228c0820cf5SMikulas Patocka 			p->bs = NULL;
222916245bdcSJun'ichi Nomura 		} else if (dm_table_get_type(t) == DM_TYPE_REQUEST_BASED) {
223016245bdcSJun'ichi Nomura 			/*
223116245bdcSJun'ichi Nomura 			 * There's no need to reload with request-based dm
223216245bdcSJun'ichi Nomura 			 * because the size of front_pad doesn't change.
223316245bdcSJun'ichi Nomura 			 * Note for future: If you are to reload bioset,
223416245bdcSJun'ichi Nomura 			 * prep-ed requests in the queue may refer
223516245bdcSJun'ichi Nomura 			 * to bio from the old bioset, so you must walk
223616245bdcSJun'ichi Nomura 			 * through the queue to unprep.
223716245bdcSJun'ichi Nomura 			 */
223816245bdcSJun'ichi Nomura 		}
2239e6ee8c0bSKiyoshi Ueda 		goto out;
2240c0820cf5SMikulas Patocka 	}
2241e6ee8c0bSKiyoshi Ueda 
2242*1ae49ea2SMike Snitzer 	BUG_ON(!p || md->io_pool || md->rq_pool || md->bs);
2243e6ee8c0bSKiyoshi Ueda 
2244e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
2245e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
2246*1ae49ea2SMike Snitzer 	md->rq_pool = p->rq_pool;
2247*1ae49ea2SMike Snitzer 	p->rq_pool = NULL;
2248e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
2249e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
2250e6ee8c0bSKiyoshi Ueda 
2251e6ee8c0bSKiyoshi Ueda out:
2252e6ee8c0bSKiyoshi Ueda 	/* mempool bind completed, now no need any mempools in the table */
2253e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
2254e6ee8c0bSKiyoshi Ueda }
2255e6ee8c0bSKiyoshi Ueda 
22561da177e4SLinus Torvalds /*
22571da177e4SLinus Torvalds  * Bind a table to the device.
22581da177e4SLinus Torvalds  */
22591da177e4SLinus Torvalds static void event_callback(void *context)
22601da177e4SLinus Torvalds {
22617a8c3d3bSMike Anderson 	unsigned long flags;
22627a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
22631da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
22641da177e4SLinus Torvalds 
22657a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
22667a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
22677a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
22687a8c3d3bSMike Anderson 
2269ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
22707a8c3d3bSMike Anderson 
22711da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
22721da177e4SLinus Torvalds 	wake_up(&md->eventq);
22731da177e4SLinus Torvalds }
22741da177e4SLinus Torvalds 
2275c217649bSMike Snitzer /*
2276c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
2277c217649bSMike Snitzer  */
22784e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
22791da177e4SLinus Torvalds {
22804e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
22811da177e4SLinus Torvalds 
2282db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
22831da177e4SLinus Torvalds }
22841da177e4SLinus Torvalds 
2285042d2a9bSAlasdair G Kergon /*
2286d5b9dd04SMikulas Patocka  * Return 1 if the queue has a compulsory merge_bvec_fn function.
2287d5b9dd04SMikulas Patocka  *
2288d5b9dd04SMikulas Patocka  * If this function returns 0, then the device is either a non-dm
2289d5b9dd04SMikulas Patocka  * device without a merge_bvec_fn, or it is a dm device that is
2290d5b9dd04SMikulas Patocka  * able to split any bios it receives that are too big.
2291d5b9dd04SMikulas Patocka  */
2292d5b9dd04SMikulas Patocka int dm_queue_merge_is_compulsory(struct request_queue *q)
2293d5b9dd04SMikulas Patocka {
2294d5b9dd04SMikulas Patocka 	struct mapped_device *dev_md;
2295d5b9dd04SMikulas Patocka 
2296d5b9dd04SMikulas Patocka 	if (!q->merge_bvec_fn)
2297d5b9dd04SMikulas Patocka 		return 0;
2298d5b9dd04SMikulas Patocka 
2299d5b9dd04SMikulas Patocka 	if (q->make_request_fn == dm_request) {
2300d5b9dd04SMikulas Patocka 		dev_md = q->queuedata;
2301d5b9dd04SMikulas Patocka 		if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2302d5b9dd04SMikulas Patocka 			return 0;
2303d5b9dd04SMikulas Patocka 	}
2304d5b9dd04SMikulas Patocka 
2305d5b9dd04SMikulas Patocka 	return 1;
2306d5b9dd04SMikulas Patocka }
2307d5b9dd04SMikulas Patocka 
2308d5b9dd04SMikulas Patocka static int dm_device_merge_is_compulsory(struct dm_target *ti,
2309d5b9dd04SMikulas Patocka 					 struct dm_dev *dev, sector_t start,
2310d5b9dd04SMikulas Patocka 					 sector_t len, void *data)
2311d5b9dd04SMikulas Patocka {
2312d5b9dd04SMikulas Patocka 	struct block_device *bdev = dev->bdev;
2313d5b9dd04SMikulas Patocka 	struct request_queue *q = bdev_get_queue(bdev);
2314d5b9dd04SMikulas Patocka 
2315d5b9dd04SMikulas Patocka 	return dm_queue_merge_is_compulsory(q);
2316d5b9dd04SMikulas Patocka }
2317d5b9dd04SMikulas Patocka 
2318d5b9dd04SMikulas Patocka /*
2319d5b9dd04SMikulas Patocka  * Return 1 if it is acceptable to ignore merge_bvec_fn based
2320d5b9dd04SMikulas Patocka  * on the properties of the underlying devices.
2321d5b9dd04SMikulas Patocka  */
2322d5b9dd04SMikulas Patocka static int dm_table_merge_is_optional(struct dm_table *table)
2323d5b9dd04SMikulas Patocka {
2324d5b9dd04SMikulas Patocka 	unsigned i = 0;
2325d5b9dd04SMikulas Patocka 	struct dm_target *ti;
2326d5b9dd04SMikulas Patocka 
2327d5b9dd04SMikulas Patocka 	while (i < dm_table_get_num_targets(table)) {
2328d5b9dd04SMikulas Patocka 		ti = dm_table_get_target(table, i++);
2329d5b9dd04SMikulas Patocka 
2330d5b9dd04SMikulas Patocka 		if (ti->type->iterate_devices &&
2331d5b9dd04SMikulas Patocka 		    ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2332d5b9dd04SMikulas Patocka 			return 0;
2333d5b9dd04SMikulas Patocka 	}
2334d5b9dd04SMikulas Patocka 
2335d5b9dd04SMikulas Patocka 	return 1;
2336d5b9dd04SMikulas Patocka }
2337d5b9dd04SMikulas Patocka 
2338d5b9dd04SMikulas Patocka /*
2339042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2340042d2a9bSAlasdair G Kergon  */
2341042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2342754c5fc7SMike Snitzer 			       struct queue_limits *limits)
23431da177e4SLinus Torvalds {
2344042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
2345165125e1SJens Axboe 	struct request_queue *q = md->queue;
23461da177e4SLinus Torvalds 	sector_t size;
2347d5b9dd04SMikulas Patocka 	int merge_is_optional;
23481da177e4SLinus Torvalds 
23491da177e4SLinus Torvalds 	size = dm_table_get_size(t);
23503ac51e74SDarrick J. Wong 
23513ac51e74SDarrick J. Wong 	/*
23523ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
23533ac51e74SDarrick J. Wong 	 */
2354fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
23553ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
23563ac51e74SDarrick J. Wong 
23574e90188bSAlasdair G Kergon 	__set_size(md, size);
23581da177e4SLinus Torvalds 
2359cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
23602ca3310eSAlasdair G Kergon 
2361e6ee8c0bSKiyoshi Ueda 	/*
2362e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
2363e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
2364e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
2365e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
2366e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
2367e6ee8c0bSKiyoshi Ueda 	 */
2368e6ee8c0bSKiyoshi Ueda 	if (dm_table_request_based(t) && !blk_queue_stopped(q))
2369e6ee8c0bSKiyoshi Ueda 		stop_queue(q);
2370e6ee8c0bSKiyoshi Ueda 
2371e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
2372e6ee8c0bSKiyoshi Ueda 
2373d5b9dd04SMikulas Patocka 	merge_is_optional = dm_table_merge_is_optional(t);
2374d5b9dd04SMikulas Patocka 
2375a12f5d48SEric Dumazet 	old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
237683d5e5b0SMikulas Patocka 	rcu_assign_pointer(md->map, t);
237736a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
237836a0456fSAlasdair G Kergon 
2379754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
2380d5b9dd04SMikulas Patocka 	if (merge_is_optional)
2381d5b9dd04SMikulas Patocka 		set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2382d5b9dd04SMikulas Patocka 	else
2383d5b9dd04SMikulas Patocka 		clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
238441abc4e1SHannes Reinecke 	if (old_map)
238583d5e5b0SMikulas Patocka 		dm_sync_table(md);
23862ca3310eSAlasdair G Kergon 
2387042d2a9bSAlasdair G Kergon 	return old_map;
23881da177e4SLinus Torvalds }
23891da177e4SLinus Torvalds 
2390a7940155SAlasdair G Kergon /*
2391a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2392a7940155SAlasdair G Kergon  */
2393a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
23941da177e4SLinus Torvalds {
2395a12f5d48SEric Dumazet 	struct dm_table *map = rcu_dereference_protected(md->map, 1);
23961da177e4SLinus Torvalds 
23971da177e4SLinus Torvalds 	if (!map)
2398a7940155SAlasdair G Kergon 		return NULL;
23991da177e4SLinus Torvalds 
24001da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
24019cdb8520SMonam Agarwal 	RCU_INIT_POINTER(md->map, NULL);
240283d5e5b0SMikulas Patocka 	dm_sync_table(md);
2403a7940155SAlasdair G Kergon 
2404a7940155SAlasdair G Kergon 	return map;
24051da177e4SLinus Torvalds }
24061da177e4SLinus Torvalds 
24071da177e4SLinus Torvalds /*
24081da177e4SLinus Torvalds  * Constructor for a new device.
24091da177e4SLinus Torvalds  */
24102b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
24111da177e4SLinus Torvalds {
24121da177e4SLinus Torvalds 	struct mapped_device *md;
24131da177e4SLinus Torvalds 
24142b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
24151da177e4SLinus Torvalds 	if (!md)
24161da177e4SLinus Torvalds 		return -ENXIO;
24171da177e4SLinus Torvalds 
2418784aae73SMilan Broz 	dm_sysfs_init(md);
2419784aae73SMilan Broz 
24201da177e4SLinus Torvalds 	*result = md;
24211da177e4SLinus Torvalds 	return 0;
24221da177e4SLinus Torvalds }
24231da177e4SLinus Torvalds 
2424a5664dadSMike Snitzer /*
2425a5664dadSMike Snitzer  * Functions to manage md->type.
2426a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2427a5664dadSMike Snitzer  */
2428a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2429a5664dadSMike Snitzer {
2430a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2431a5664dadSMike Snitzer }
2432a5664dadSMike Snitzer 
2433a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2434a5664dadSMike Snitzer {
2435a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2436a5664dadSMike Snitzer }
2437a5664dadSMike Snitzer 
2438a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type)
2439a5664dadSMike Snitzer {
244000c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2441a5664dadSMike Snitzer 	md->type = type;
2442a5664dadSMike Snitzer }
2443a5664dadSMike Snitzer 
2444a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md)
2445a5664dadSMike Snitzer {
244600c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2447a5664dadSMike Snitzer 	return md->type;
2448a5664dadSMike Snitzer }
2449a5664dadSMike Snitzer 
245036a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
245136a0456fSAlasdair G Kergon {
245236a0456fSAlasdair G Kergon 	return md->immutable_target_type;
245336a0456fSAlasdair G Kergon }
245436a0456fSAlasdair G Kergon 
24554a0b4ddfSMike Snitzer /*
2456f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
2457f84cb8a4SMike Snitzer  * count on 'md'.
2458f84cb8a4SMike Snitzer  */
2459f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2460f84cb8a4SMike Snitzer {
2461f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
2462f84cb8a4SMike Snitzer 	return &md->queue->limits;
2463f84cb8a4SMike Snitzer }
2464f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2465f84cb8a4SMike Snitzer 
2466f84cb8a4SMike Snitzer /*
24674a0b4ddfSMike Snitzer  * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
24684a0b4ddfSMike Snitzer  */
24694a0b4ddfSMike Snitzer static int dm_init_request_based_queue(struct mapped_device *md)
24704a0b4ddfSMike Snitzer {
24714a0b4ddfSMike Snitzer 	struct request_queue *q = NULL;
24724a0b4ddfSMike Snitzer 
24734a0b4ddfSMike Snitzer 	if (md->queue->elevator)
24744a0b4ddfSMike Snitzer 		return 1;
24754a0b4ddfSMike Snitzer 
24764a0b4ddfSMike Snitzer 	/* Fully initialize the queue */
24774a0b4ddfSMike Snitzer 	q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
24784a0b4ddfSMike Snitzer 	if (!q)
24794a0b4ddfSMike Snitzer 		return 0;
24804a0b4ddfSMike Snitzer 
24814a0b4ddfSMike Snitzer 	md->queue = q;
24824a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
24834a0b4ddfSMike Snitzer 	blk_queue_softirq_done(md->queue, dm_softirq_done);
24844a0b4ddfSMike Snitzer 	blk_queue_prep_rq(md->queue, dm_prep_fn);
24854a0b4ddfSMike Snitzer 	blk_queue_lld_busy(md->queue, dm_lld_busy);
24864a0b4ddfSMike Snitzer 
24874a0b4ddfSMike Snitzer 	elv_register_queue(md->queue);
24884a0b4ddfSMike Snitzer 
24894a0b4ddfSMike Snitzer 	return 1;
24904a0b4ddfSMike Snitzer }
24914a0b4ddfSMike Snitzer 
24924a0b4ddfSMike Snitzer /*
24934a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
24944a0b4ddfSMike Snitzer  */
24954a0b4ddfSMike Snitzer int dm_setup_md_queue(struct mapped_device *md)
24964a0b4ddfSMike Snitzer {
24974a0b4ddfSMike Snitzer 	if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
24984a0b4ddfSMike Snitzer 	    !dm_init_request_based_queue(md)) {
24994a0b4ddfSMike Snitzer 		DMWARN("Cannot initialize queue for request-based mapped device");
25004a0b4ddfSMike Snitzer 		return -EINVAL;
25014a0b4ddfSMike Snitzer 	}
25024a0b4ddfSMike Snitzer 
25034a0b4ddfSMike Snitzer 	return 0;
25044a0b4ddfSMike Snitzer }
25054a0b4ddfSMike Snitzer 
2506637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev)
25071da177e4SLinus Torvalds {
25081da177e4SLinus Torvalds 	struct mapped_device *md;
25091da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
25101da177e4SLinus Torvalds 
25111da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
25121da177e4SLinus Torvalds 		return NULL;
25131da177e4SLinus Torvalds 
2514f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
25151da177e4SLinus Torvalds 
25161da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
2517fba9f90eSJeff Mahoney 	if (md && (md == MINOR_ALLOCED ||
2518f331c029STejun Heo 		   (MINOR(disk_devt(dm_disk(md))) != minor) ||
2519abdc568bSKiyoshi Ueda 		   dm_deleting_md(md) ||
2520fba9f90eSJeff Mahoney 		   test_bit(DMF_FREEING, &md->flags))) {
2521637842cfSDavid Teigland 		md = NULL;
2522fba9f90eSJeff Mahoney 		goto out;
2523fba9f90eSJeff Mahoney 	}
25241da177e4SLinus Torvalds 
2525fba9f90eSJeff Mahoney out:
2526f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
25271da177e4SLinus Torvalds 
2528637842cfSDavid Teigland 	return md;
2529637842cfSDavid Teigland }
2530637842cfSDavid Teigland 
2531d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev)
2532d229a958SDavid Teigland {
2533d229a958SDavid Teigland 	struct mapped_device *md = dm_find_md(dev);
2534d229a958SDavid Teigland 
2535d229a958SDavid Teigland 	if (md)
2536d229a958SDavid Teigland 		dm_get(md);
2537d229a958SDavid Teigland 
2538d229a958SDavid Teigland 	return md;
2539d229a958SDavid Teigland }
25403cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2541d229a958SDavid Teigland 
25429ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2543637842cfSDavid Teigland {
25449ade92a9SAlasdair G Kergon 	return md->interface_ptr;
25451da177e4SLinus Torvalds }
25461da177e4SLinus Torvalds 
25471da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
25481da177e4SLinus Torvalds {
25491da177e4SLinus Torvalds 	md->interface_ptr = ptr;
25501da177e4SLinus Torvalds }
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
25531da177e4SLinus Torvalds {
25541da177e4SLinus Torvalds 	atomic_inc(&md->holders);
25553f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
25561da177e4SLinus Torvalds }
25571da177e4SLinus Torvalds 
255872d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
255972d94861SAlasdair G Kergon {
256072d94861SAlasdair G Kergon 	return md->name;
256172d94861SAlasdair G Kergon }
256272d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
256372d94861SAlasdair G Kergon 
25643f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
25651da177e4SLinus Torvalds {
25661134e5aeSMike Anderson 	struct dm_table *map;
256783d5e5b0SMikulas Patocka 	int srcu_idx;
25681da177e4SLinus Torvalds 
25693f77316dSKiyoshi Ueda 	might_sleep();
2570fba9f90eSJeff Mahoney 
25713f77316dSKiyoshi Ueda 	spin_lock(&_minor_lock);
257283d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
25733f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2574fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2575f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
25763f77316dSKiyoshi Ueda 
25774f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
25781da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
25791da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
25801da177e4SLinus Torvalds 	}
25813f77316dSKiyoshi Ueda 
258283d5e5b0SMikulas Patocka 	/* dm_put_live_table must be before msleep, otherwise deadlock is possible */
258383d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
258483d5e5b0SMikulas Patocka 
25853f77316dSKiyoshi Ueda 	/*
25863f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
25873f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
25883f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
25893f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
25903f77316dSKiyoshi Ueda 	 */
25913f77316dSKiyoshi Ueda 	if (wait)
25923f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
25933f77316dSKiyoshi Ueda 			msleep(1);
25943f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
25953f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
25963f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
25973f77316dSKiyoshi Ueda 
2598784aae73SMilan Broz 	dm_sysfs_exit(md);
2599a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
26001da177e4SLinus Torvalds 	free_dev(md);
26011da177e4SLinus Torvalds }
26023f77316dSKiyoshi Ueda 
26033f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
26043f77316dSKiyoshi Ueda {
26053f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
26063f77316dSKiyoshi Ueda }
26073f77316dSKiyoshi Ueda 
26083f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
26093f77316dSKiyoshi Ueda {
26103f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
26113f77316dSKiyoshi Ueda }
26123f77316dSKiyoshi Ueda 
26133f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
26143f77316dSKiyoshi Ueda {
26153f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
26161da177e4SLinus Torvalds }
261779eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
26181da177e4SLinus Torvalds 
2619401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
262046125c1cSMilan Broz {
262146125c1cSMilan Broz 	int r = 0;
2622b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
2623b44ebeb0SMikulas Patocka 
2624b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
262546125c1cSMilan Broz 
262646125c1cSMilan Broz 	while (1) {
2627401600dfSMikulas Patocka 		set_current_state(interruptible);
262846125c1cSMilan Broz 
2629b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
263046125c1cSMilan Broz 			break;
263146125c1cSMilan Broz 
2632401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
2633401600dfSMikulas Patocka 		    signal_pending(current)) {
263446125c1cSMilan Broz 			r = -EINTR;
263546125c1cSMilan Broz 			break;
263646125c1cSMilan Broz 		}
263746125c1cSMilan Broz 
263846125c1cSMilan Broz 		io_schedule();
263946125c1cSMilan Broz 	}
264046125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
264146125c1cSMilan Broz 
2642b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
2643b44ebeb0SMikulas Patocka 
264446125c1cSMilan Broz 	return r;
264546125c1cSMilan Broz }
264646125c1cSMilan Broz 
26471da177e4SLinus Torvalds /*
26481da177e4SLinus Torvalds  * Process the deferred bios
26491da177e4SLinus Torvalds  */
2650ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
26511da177e4SLinus Torvalds {
2652ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2653ef208587SMikulas Patocka 						work);
26546d6f10dfSMilan Broz 	struct bio *c;
265583d5e5b0SMikulas Patocka 	int srcu_idx;
265683d5e5b0SMikulas Patocka 	struct dm_table *map;
26571da177e4SLinus Torvalds 
265883d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
2659ef208587SMikulas Patocka 
26603b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2661022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2662022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2663022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2664022c2611SMikulas Patocka 
26656a8736d1STejun Heo 		if (!c)
2666df12ee99SAlasdair G Kergon 			break;
266773d410c0SMilan Broz 
2668e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2669e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2670af7e466aSMikulas Patocka 		else
267183d5e5b0SMikulas Patocka 			__split_and_process_bio(md, map, c);
2672e6ee8c0bSKiyoshi Ueda 	}
26733b00b203SMikulas Patocka 
267483d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
26751da177e4SLinus Torvalds }
26761da177e4SLinus Torvalds 
26779a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2678304f3f6aSMilan Broz {
26793b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
26804e857c58SPeter Zijlstra 	smp_mb__after_atomic();
268153d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2682304f3f6aSMilan Broz }
2683304f3f6aSMilan Broz 
26841da177e4SLinus Torvalds /*
2685042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
26861da177e4SLinus Torvalds  */
2687042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
26881da177e4SLinus Torvalds {
268987eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2690754c5fc7SMike Snitzer 	struct queue_limits limits;
2691042d2a9bSAlasdair G Kergon 	int r;
26921da177e4SLinus Torvalds 
2693e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
26941da177e4SLinus Torvalds 
26951da177e4SLinus Torvalds 	/* device must be suspended */
26964f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
269793c534aeSAlasdair G Kergon 		goto out;
26981da177e4SLinus Torvalds 
26993ae70656SMike Snitzer 	/*
27003ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
27013ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
27023ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
27033ae70656SMike Snitzer 	 * reappear.
27043ae70656SMike Snitzer 	 */
27053ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
270683d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
27073ae70656SMike Snitzer 		if (live_map)
27083ae70656SMike Snitzer 			limits = md->queue->limits;
270983d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
27103ae70656SMike Snitzer 	}
27113ae70656SMike Snitzer 
271287eb5b21SMike Christie 	if (!live_map) {
2713754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2714042d2a9bSAlasdair G Kergon 		if (r) {
2715042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2716754c5fc7SMike Snitzer 			goto out;
2717042d2a9bSAlasdair G Kergon 		}
271887eb5b21SMike Christie 	}
2719754c5fc7SMike Snitzer 
2720042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
27211da177e4SLinus Torvalds 
272293c534aeSAlasdair G Kergon out:
2723e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2724042d2a9bSAlasdair G Kergon 	return map;
27251da177e4SLinus Torvalds }
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds /*
27281da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
27291da177e4SLinus Torvalds  * device.
27301da177e4SLinus Torvalds  */
27312ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
27321da177e4SLinus Torvalds {
2733e39e2e95SAlasdair G Kergon 	int r;
27341da177e4SLinus Torvalds 
27351da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2736dfbe03f6SAlasdair G Kergon 
2737db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2738dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2739cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2740e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2741e39e2e95SAlasdair G Kergon 		return r;
2742dfbe03f6SAlasdair G Kergon 	}
2743dfbe03f6SAlasdair G Kergon 
2744aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2745aa8d7c2fSAlasdair G Kergon 
27461da177e4SLinus Torvalds 	return 0;
27471da177e4SLinus Torvalds }
27481da177e4SLinus Torvalds 
27492ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
27501da177e4SLinus Torvalds {
2751aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2752aa8d7c2fSAlasdair G Kergon 		return;
2753aa8d7c2fSAlasdair G Kergon 
2754db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
27551da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2756aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
27571da177e4SLinus Torvalds }
27581da177e4SLinus Torvalds 
27591da177e4SLinus Torvalds /*
2760ffcc3936SMike Snitzer  * If __dm_suspend returns 0, the device is completely quiescent
2761ffcc3936SMike Snitzer  * now. There is no request-processing activity. All new requests
2762ffcc3936SMike Snitzer  * are being added to md->deferred list.
2763cec47e3dSKiyoshi Ueda  *
2764ffcc3936SMike Snitzer  * Caller must hold md->suspend_lock
2765cec47e3dSKiyoshi Ueda  */
2766ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
2767ffcc3936SMike Snitzer 			unsigned suspend_flags, int interruptible)
27681da177e4SLinus Torvalds {
2769ffcc3936SMike Snitzer 	bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2770ffcc3936SMike Snitzer 	bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2771ffcc3936SMike Snitzer 	int r;
2772cf222b37SAlasdair G Kergon 
27732e93ccc1SKiyoshi Ueda 	/*
27742e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
27752e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
27762e93ccc1SKiyoshi Ueda 	 */
27772e93ccc1SKiyoshi Ueda 	if (noflush)
27782e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
27792e93ccc1SKiyoshi Ueda 
2780d67ee213SMike Snitzer 	/*
2781d67ee213SMike Snitzer 	 * This gets reverted if there's an error later and the targets
2782d67ee213SMike Snitzer 	 * provide the .presuspend_undo hook.
2783d67ee213SMike Snitzer 	 */
27841da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
27851da177e4SLinus Torvalds 
27862e93ccc1SKiyoshi Ueda 	/*
27879f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
27889f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
27899f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
27909f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
27912e93ccc1SKiyoshi Ueda 	 */
279232a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
27932ca3310eSAlasdair G Kergon 		r = lock_fs(md);
2794d67ee213SMike Snitzer 		if (r) {
2795d67ee213SMike Snitzer 			dm_table_presuspend_undo_targets(map);
2796ffcc3936SMike Snitzer 			return r;
2797aa8d7c2fSAlasdair G Kergon 		}
2798d67ee213SMike Snitzer 	}
27991da177e4SLinus Torvalds 
28001da177e4SLinus Torvalds 	/*
28013b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
28023b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
28033b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
28043b00b203SMikulas Patocka 	 * dm_wq_work.
28053b00b203SMikulas Patocka 	 *
28063b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
28073b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
28086a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
28096a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
28106a8736d1STejun Heo 	 * flush_workqueue(md->wq).
28111da177e4SLinus Torvalds 	 */
28121eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
281341abc4e1SHannes Reinecke 	if (map)
281483d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
28151da177e4SLinus Torvalds 
2816d0bcb878SKiyoshi Ueda 	/*
281729e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
281829e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2819d0bcb878SKiyoshi Ueda 	 */
2820cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
28219f518b27SKiyoshi Ueda 		stop_queue(md->queue);
2822cec47e3dSKiyoshi Ueda 
2823d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2824d0bcb878SKiyoshi Ueda 
28251da177e4SLinus Torvalds 	/*
28263b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
28273b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
28283b00b203SMikulas Patocka 	 * to finish.
28291da177e4SLinus Torvalds 	 */
2830ffcc3936SMike Snitzer 	r = dm_wait_for_completion(md, interruptible);
28311da177e4SLinus Torvalds 
28326d6f10dfSMilan Broz 	if (noflush)
2833022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
283441abc4e1SHannes Reinecke 	if (map)
283583d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
28362e93ccc1SKiyoshi Ueda 
28371da177e4SLinus Torvalds 	/* were we interrupted ? */
283846125c1cSMilan Broz 	if (r < 0) {
28399a1fb464SMikulas Patocka 		dm_queue_flush(md);
284073d410c0SMilan Broz 
2841cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
28429f518b27SKiyoshi Ueda 			start_queue(md->queue);
2843cec47e3dSKiyoshi Ueda 
28442ca3310eSAlasdair G Kergon 		unlock_fs(md);
2845d67ee213SMike Snitzer 		dm_table_presuspend_undo_targets(map);
2846ffcc3936SMike Snitzer 		/* pushback list is already flushed, so skip flush */
2847ffcc3936SMike Snitzer 	}
2848ffcc3936SMike Snitzer 
2849ffcc3936SMike Snitzer 	return r;
28502ca3310eSAlasdair G Kergon }
28512ca3310eSAlasdair G Kergon 
28523b00b203SMikulas Patocka /*
2853ffcc3936SMike Snitzer  * We need to be able to change a mapping table under a mounted
2854ffcc3936SMike Snitzer  * filesystem.  For example we might want to move some data in
2855ffcc3936SMike Snitzer  * the background.  Before the table can be swapped with
2856ffcc3936SMike Snitzer  * dm_bind_table, dm_suspend must be called to flush any in
2857ffcc3936SMike Snitzer  * flight bios and ensure that any further io gets deferred.
28583b00b203SMikulas Patocka  */
2859ffcc3936SMike Snitzer /*
2860ffcc3936SMike Snitzer  * Suspend mechanism in request-based dm.
2861ffcc3936SMike Snitzer  *
2862ffcc3936SMike Snitzer  * 1. Flush all I/Os by lock_fs() if needed.
2863ffcc3936SMike Snitzer  * 2. Stop dispatching any I/O by stopping the request_queue.
2864ffcc3936SMike Snitzer  * 3. Wait for all in-flight I/Os to be completed or requeued.
2865ffcc3936SMike Snitzer  *
2866ffcc3936SMike Snitzer  * To abort suspend, start the request_queue.
2867ffcc3936SMike Snitzer  */
2868ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2869ffcc3936SMike Snitzer {
2870ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2871ffcc3936SMike Snitzer 	int r = 0;
2872ffcc3936SMike Snitzer 
2873ffcc3936SMike Snitzer retry:
2874ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2875ffcc3936SMike Snitzer 
2876ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2877ffcc3936SMike Snitzer 		r = -EINVAL;
2878ffcc3936SMike Snitzer 		goto out_unlock;
2879ffcc3936SMike Snitzer 	}
2880ffcc3936SMike Snitzer 
2881ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2882ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2883ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2884ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2885ffcc3936SMike Snitzer 		if (r)
2886ffcc3936SMike Snitzer 			return r;
2887ffcc3936SMike Snitzer 		goto retry;
2888ffcc3936SMike Snitzer 	}
2889ffcc3936SMike Snitzer 
2890a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2891ffcc3936SMike Snitzer 
2892ffcc3936SMike Snitzer 	r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE);
2893ffcc3936SMike Snitzer 	if (r)
2894ffcc3936SMike Snitzer 		goto out_unlock;
28953b00b203SMikulas Patocka 
28961da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
28971da177e4SLinus Torvalds 
28984d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
28994d4471cbSKiyoshi Ueda 
2900d287483dSAlasdair G Kergon out_unlock:
2901e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2902cf222b37SAlasdair G Kergon 	return r;
29031da177e4SLinus Torvalds }
29041da177e4SLinus Torvalds 
2905ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map)
29061da177e4SLinus Torvalds {
2907ffcc3936SMike Snitzer 	if (map) {
2908ffcc3936SMike Snitzer 		int r = dm_table_resume_targets(map);
29098757b776SMilan Broz 		if (r)
2910ffcc3936SMike Snitzer 			return r;
2911ffcc3936SMike Snitzer 	}
29122ca3310eSAlasdair G Kergon 
29139a1fb464SMikulas Patocka 	dm_queue_flush(md);
29142ca3310eSAlasdair G Kergon 
2915cec47e3dSKiyoshi Ueda 	/*
2916cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2917cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2918cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2919cec47e3dSKiyoshi Ueda 	 */
2920cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2921cec47e3dSKiyoshi Ueda 		start_queue(md->queue);
2922cec47e3dSKiyoshi Ueda 
29232ca3310eSAlasdair G Kergon 	unlock_fs(md);
29242ca3310eSAlasdair G Kergon 
2925ffcc3936SMike Snitzer 	return 0;
2926ffcc3936SMike Snitzer }
2927ffcc3936SMike Snitzer 
2928ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md)
2929ffcc3936SMike Snitzer {
2930ffcc3936SMike Snitzer 	int r = -EINVAL;
2931ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2932ffcc3936SMike Snitzer 
2933ffcc3936SMike Snitzer retry:
2934ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2935ffcc3936SMike Snitzer 
2936ffcc3936SMike Snitzer 	if (!dm_suspended_md(md))
2937ffcc3936SMike Snitzer 		goto out;
2938ffcc3936SMike Snitzer 
2939ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2940ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2941ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2942ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2943ffcc3936SMike Snitzer 		if (r)
2944ffcc3936SMike Snitzer 			return r;
2945ffcc3936SMike Snitzer 		goto retry;
2946ffcc3936SMike Snitzer 	}
2947ffcc3936SMike Snitzer 
2948a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2949ffcc3936SMike Snitzer 	if (!map || !dm_table_get_size(map))
2950ffcc3936SMike Snitzer 		goto out;
2951ffcc3936SMike Snitzer 
2952ffcc3936SMike Snitzer 	r = __dm_resume(md, map);
2953ffcc3936SMike Snitzer 	if (r)
2954ffcc3936SMike Snitzer 		goto out;
2955ffcc3936SMike Snitzer 
29562ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
29572ca3310eSAlasdair G Kergon 
2958cf222b37SAlasdair G Kergon 	r = 0;
2959cf222b37SAlasdair G Kergon out:
2960e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
29612ca3310eSAlasdair G Kergon 
2962cf222b37SAlasdair G Kergon 	return r;
29631da177e4SLinus Torvalds }
29641da177e4SLinus Torvalds 
2965fd2ed4d2SMikulas Patocka /*
2966fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
2967fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
2968fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
2969fd2ed4d2SMikulas Patocka  */
2970fd2ed4d2SMikulas Patocka 
2971ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
2972ffcc3936SMike Snitzer {
2973ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2974ffcc3936SMike Snitzer 
2975ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md))
2976ffcc3936SMike Snitzer 		return; /* nested internal suspend */
2977ffcc3936SMike Snitzer 
2978ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2979ffcc3936SMike Snitzer 		set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2980ffcc3936SMike Snitzer 		return; /* nest suspend */
2981ffcc3936SMike Snitzer 	}
2982ffcc3936SMike Snitzer 
2983a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2984ffcc3936SMike Snitzer 
2985ffcc3936SMike Snitzer 	/*
2986ffcc3936SMike Snitzer 	 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2987ffcc3936SMike Snitzer 	 * supported.  Properly supporting a TASK_INTERRUPTIBLE internal suspend
2988ffcc3936SMike Snitzer 	 * would require changing .presuspend to return an error -- avoid this
2989ffcc3936SMike Snitzer 	 * until there is a need for more elaborate variants of internal suspend.
2990ffcc3936SMike Snitzer 	 */
2991ffcc3936SMike Snitzer 	(void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE);
2992ffcc3936SMike Snitzer 
2993ffcc3936SMike Snitzer 	set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2994ffcc3936SMike Snitzer 
2995ffcc3936SMike Snitzer 	dm_table_postsuspend_targets(map);
2996ffcc3936SMike Snitzer }
2997ffcc3936SMike Snitzer 
2998ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md)
2999ffcc3936SMike Snitzer {
3000ffcc3936SMike Snitzer 	if (!dm_suspended_internally_md(md))
3001ffcc3936SMike Snitzer 		return; /* resume from nested internal suspend */
3002ffcc3936SMike Snitzer 
3003ffcc3936SMike Snitzer 	if (dm_suspended_md(md))
3004ffcc3936SMike Snitzer 		goto done; /* resume from nested suspend */
3005ffcc3936SMike Snitzer 
3006ffcc3936SMike Snitzer 	/*
3007ffcc3936SMike Snitzer 	 * NOTE: existing callers don't need to call dm_table_resume_targets
3008ffcc3936SMike Snitzer 	 * (which may fail -- so best to avoid it for now by passing NULL map)
3009ffcc3936SMike Snitzer 	 */
3010ffcc3936SMike Snitzer 	(void) __dm_resume(md, NULL);
3011ffcc3936SMike Snitzer 
3012ffcc3936SMike Snitzer done:
3013ffcc3936SMike Snitzer 	clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3014ffcc3936SMike Snitzer 	smp_mb__after_atomic();
3015ffcc3936SMike Snitzer 	wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
3016ffcc3936SMike Snitzer }
3017ffcc3936SMike Snitzer 
3018ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md)
3019fd2ed4d2SMikulas Patocka {
3020fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
3021ffcc3936SMike Snitzer 	__dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
3022ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
3023ffcc3936SMike Snitzer }
3024ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
3025ffcc3936SMike Snitzer 
3026ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md)
3027ffcc3936SMike Snitzer {
3028ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
3029ffcc3936SMike Snitzer 	__dm_internal_resume(md);
3030ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
3031ffcc3936SMike Snitzer }
3032ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume);
3033ffcc3936SMike Snitzer 
3034ffcc3936SMike Snitzer /*
3035ffcc3936SMike Snitzer  * Fast variants of internal suspend/resume hold md->suspend_lock,
3036ffcc3936SMike Snitzer  * which prevents interaction with userspace-driven suspend.
3037ffcc3936SMike Snitzer  */
3038ffcc3936SMike Snitzer 
3039ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md)
3040ffcc3936SMike Snitzer {
3041ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
3042ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
3043fd2ed4d2SMikulas Patocka 		return;
3044fd2ed4d2SMikulas Patocka 
3045fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
3046fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
3047fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
3048fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
3049fd2ed4d2SMikulas Patocka }
3050fd2ed4d2SMikulas Patocka 
3051ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md)
3052fd2ed4d2SMikulas Patocka {
3053ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
3054fd2ed4d2SMikulas Patocka 		goto done;
3055fd2ed4d2SMikulas Patocka 
3056fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
3057fd2ed4d2SMikulas Patocka 
3058fd2ed4d2SMikulas Patocka done:
3059fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
3060fd2ed4d2SMikulas Patocka }
3061fd2ed4d2SMikulas Patocka 
30621da177e4SLinus Torvalds /*-----------------------------------------------------------------
30631da177e4SLinus Torvalds  * Event notification.
30641da177e4SLinus Torvalds  *---------------------------------------------------------------*/
30653abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
306660935eb2SMilan Broz 		       unsigned cookie)
306769267a30SAlasdair G Kergon {
306860935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
306960935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
307060935eb2SMilan Broz 
307160935eb2SMilan Broz 	if (!cookie)
30723abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
307360935eb2SMilan Broz 	else {
307460935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
307560935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
30763abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
30773abf85b5SPeter Rajnoha 					  action, envp);
307860935eb2SMilan Broz 	}
307969267a30SAlasdair G Kergon }
308069267a30SAlasdair G Kergon 
30817a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
30827a8c3d3bSMike Anderson {
30837a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
30847a8c3d3bSMike Anderson }
30857a8c3d3bSMike Anderson 
30861da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
30871da177e4SLinus Torvalds {
30881da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
30891da177e4SLinus Torvalds }
30901da177e4SLinus Torvalds 
30911da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
30921da177e4SLinus Torvalds {
30931da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
30941da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
30951da177e4SLinus Torvalds }
30961da177e4SLinus Torvalds 
30977a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
30987a8c3d3bSMike Anderson {
30997a8c3d3bSMike Anderson 	unsigned long flags;
31007a8c3d3bSMike Anderson 
31017a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
31027a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
31037a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
31047a8c3d3bSMike Anderson }
31057a8c3d3bSMike Anderson 
31061da177e4SLinus Torvalds /*
31071da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
31081da177e4SLinus Torvalds  * count on 'md'.
31091da177e4SLinus Torvalds  */
31101da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
31111da177e4SLinus Torvalds {
31121da177e4SLinus Torvalds 	return md->disk;
31131da177e4SLinus Torvalds }
31141da177e4SLinus Torvalds 
3115784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
3116784aae73SMilan Broz {
31172995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
3118784aae73SMilan Broz }
3119784aae73SMilan Broz 
3120784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
3121784aae73SMilan Broz {
3122784aae73SMilan Broz 	struct mapped_device *md;
3123784aae73SMilan Broz 
31242995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
3125784aae73SMilan Broz 
31264d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
3127432a212cSMike Anderson 	    dm_deleting_md(md))
31284d89b7b4SMilan Broz 		return NULL;
31294d89b7b4SMilan Broz 
3130784aae73SMilan Broz 	dm_get(md);
3131784aae73SMilan Broz 	return md;
3132784aae73SMilan Broz }
3133784aae73SMilan Broz 
31344f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
31351da177e4SLinus Torvalds {
31361da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
31371da177e4SLinus Torvalds }
31381da177e4SLinus Torvalds 
3139ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md)
3140ffcc3936SMike Snitzer {
3141ffcc3936SMike Snitzer 	return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3142ffcc3936SMike Snitzer }
3143ffcc3936SMike Snitzer 
31442c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
31452c140a24SMikulas Patocka {
31462c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
31472c140a24SMikulas Patocka }
31482c140a24SMikulas Patocka 
314964dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
315064dbce58SKiyoshi Ueda {
3151ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
315264dbce58SKiyoshi Ueda }
315364dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
315464dbce58SKiyoshi Ueda 
31552e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
31562e93ccc1SKiyoshi Ueda {
3157ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
31582e93ccc1SKiyoshi Ueda }
31592e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
31602e93ccc1SKiyoshi Ueda 
3161c0820cf5SMikulas Patocka struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity, unsigned per_bio_data_size)
3162e6ee8c0bSKiyoshi Ueda {
31635f015204SJun'ichi Nomura 	struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
31645f015204SJun'ichi Nomura 	struct kmem_cache *cachep;
31655f015204SJun'ichi Nomura 	unsigned int pool_size;
31665f015204SJun'ichi Nomura 	unsigned int front_pad;
3167e6ee8c0bSKiyoshi Ueda 
3168e6ee8c0bSKiyoshi Ueda 	if (!pools)
3169e6ee8c0bSKiyoshi Ueda 		return NULL;
3170e6ee8c0bSKiyoshi Ueda 
317123e5083bSJun'ichi Nomura 	if (type == DM_TYPE_BIO_BASED) {
31725f015204SJun'ichi Nomura 		cachep = _io_cache;
3173e8603136SMike Snitzer 		pool_size = dm_get_reserved_bio_based_ios();
31745f015204SJun'ichi Nomura 		front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
31755f015204SJun'ichi Nomura 	} else if (type == DM_TYPE_REQUEST_BASED) {
31765f015204SJun'ichi Nomura 		cachep = _rq_tio_cache;
3177f4790826SMike Snitzer 		pool_size = dm_get_reserved_rq_based_ios();
3178*1ae49ea2SMike Snitzer 		pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache);
3179*1ae49ea2SMike Snitzer 		if (!pools->rq_pool)
3180*1ae49ea2SMike Snitzer 			goto out;
31815f015204SJun'ichi Nomura 		front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
31825f015204SJun'ichi Nomura 		/* per_bio_data_size is not used. See __bind_mempools(). */
31835f015204SJun'ichi Nomura 		WARN_ON(per_bio_data_size != 0);
31845f015204SJun'ichi Nomura 	} else
31855f015204SJun'ichi Nomura 		goto out;
31865f015204SJun'ichi Nomura 
31876cfa5857SMike Snitzer 	pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
3188e6ee8c0bSKiyoshi Ueda 	if (!pools->io_pool)
31895f015204SJun'ichi Nomura 		goto out;
3190e6ee8c0bSKiyoshi Ueda 
31913d8aab2dSJunichi Nomura 	pools->bs = bioset_create_nobvec(pool_size, front_pad);
3192e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
31935f015204SJun'ichi Nomura 		goto out;
3194e6ee8c0bSKiyoshi Ueda 
3195a91a2785SMartin K. Petersen 	if (integrity && bioset_integrity_create(pools->bs, pool_size))
31965f015204SJun'ichi Nomura 		goto out;
3197a91a2785SMartin K. Petersen 
3198e6ee8c0bSKiyoshi Ueda 	return pools;
3199e6ee8c0bSKiyoshi Ueda 
32005f015204SJun'ichi Nomura out:
32015f015204SJun'ichi Nomura 	dm_free_md_mempools(pools);
3202e6ee8c0bSKiyoshi Ueda 
3203e6ee8c0bSKiyoshi Ueda 	return NULL;
3204e6ee8c0bSKiyoshi Ueda }
3205e6ee8c0bSKiyoshi Ueda 
3206e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
3207e6ee8c0bSKiyoshi Ueda {
3208e6ee8c0bSKiyoshi Ueda 	if (!pools)
3209e6ee8c0bSKiyoshi Ueda 		return;
3210e6ee8c0bSKiyoshi Ueda 
3211e6ee8c0bSKiyoshi Ueda 	if (pools->io_pool)
3212e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->io_pool);
3213e6ee8c0bSKiyoshi Ueda 
3214*1ae49ea2SMike Snitzer 	if (pools->rq_pool)
3215*1ae49ea2SMike Snitzer 		mempool_destroy(pools->rq_pool);
3216*1ae49ea2SMike Snitzer 
3217e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
3218e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
3219e6ee8c0bSKiyoshi Ueda 
3220e6ee8c0bSKiyoshi Ueda 	kfree(pools);
3221e6ee8c0bSKiyoshi Ueda }
3222e6ee8c0bSKiyoshi Ueda 
322383d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
32241da177e4SLinus Torvalds 	.open = dm_blk_open,
32251da177e4SLinus Torvalds 	.release = dm_blk_close,
3226aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
32273ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
32281da177e4SLinus Torvalds 	.owner = THIS_MODULE
32291da177e4SLinus Torvalds };
32301da177e4SLinus Torvalds 
32311da177e4SLinus Torvalds /*
32321da177e4SLinus Torvalds  * module hooks
32331da177e4SLinus Torvalds  */
32341da177e4SLinus Torvalds module_init(dm_init);
32351da177e4SLinus Torvalds module_exit(dm_exit);
32361da177e4SLinus Torvalds 
32371da177e4SLinus Torvalds module_param(major, uint, 0);
32381da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
3239f4790826SMike Snitzer 
3240e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3241e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3242e8603136SMike Snitzer 
3243f4790826SMike Snitzer module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
3244f4790826SMike Snitzer MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
3245f4790826SMike Snitzer 
32461da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
32471da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
32481da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3249