xref: /openbmc/linux/drivers/md/dm.c (revision 86f1152b117a404229fd6f08ec3faca779f37b92)
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>
2255782138SLi Zefan 
2355782138SLi Zefan #include <trace/events/block.h>
241da177e4SLinus Torvalds 
2572d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
2672d94861SAlasdair G Kergon 
2771a16736SNamhyung Kim #ifdef CONFIG_PRINTK
2871a16736SNamhyung Kim /*
2971a16736SNamhyung Kim  * ratelimit state to be used in DMXXX_LIMIT().
3071a16736SNamhyung Kim  */
3171a16736SNamhyung Kim DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
3271a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_INTERVAL,
3371a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_BURST);
3471a16736SNamhyung Kim EXPORT_SYMBOL(dm_ratelimit_state);
3571a16736SNamhyung Kim #endif
3671a16736SNamhyung Kim 
3760935eb2SMilan Broz /*
3860935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
3960935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
4060935eb2SMilan Broz  */
4160935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
4260935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
4360935eb2SMilan Broz 
441da177e4SLinus Torvalds static const char *_name = DM_NAME;
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds static unsigned int major = 0;
471da177e4SLinus Torvalds static unsigned int _major = 0;
481da177e4SLinus Torvalds 
49d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr);
50d15b774cSAlasdair G Kergon 
51f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
522c140a24SMikulas Patocka 
532c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w);
542c140a24SMikulas Patocka 
552c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
562c140a24SMikulas Patocka 
57acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue;
58acfe0ad7SMikulas Patocka 
591da177e4SLinus Torvalds /*
608fbf26adSKiyoshi Ueda  * For bio-based dm.
611da177e4SLinus Torvalds  * One of these is allocated per bio.
621da177e4SLinus Torvalds  */
631da177e4SLinus Torvalds struct dm_io {
641da177e4SLinus Torvalds 	struct mapped_device *md;
651da177e4SLinus Torvalds 	int error;
661da177e4SLinus Torvalds 	atomic_t io_count;
676ae2fa67SRichard Kennedy 	struct bio *bio;
683eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
69f88fb981SKiyoshi Ueda 	spinlock_t endio_lock;
70fd2ed4d2SMikulas Patocka 	struct dm_stats_aux stats_aux;
711da177e4SLinus Torvalds };
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /*
748fbf26adSKiyoshi Ueda  * For request-based dm.
758fbf26adSKiyoshi Ueda  * One of these is allocated per request.
768fbf26adSKiyoshi Ueda  */
778fbf26adSKiyoshi Ueda struct dm_rq_target_io {
788fbf26adSKiyoshi Ueda 	struct mapped_device *md;
798fbf26adSKiyoshi Ueda 	struct dm_target *ti;
808fbf26adSKiyoshi Ueda 	struct request *orig, clone;
818fbf26adSKiyoshi Ueda 	int error;
828fbf26adSKiyoshi Ueda 	union map_info info;
838fbf26adSKiyoshi Ueda };
848fbf26adSKiyoshi Ueda 
858fbf26adSKiyoshi Ueda /*
8694818742SKent Overstreet  * For request-based dm - the bio clones we allocate are embedded in these
8794818742SKent Overstreet  * structs.
8894818742SKent Overstreet  *
8994818742SKent Overstreet  * We allocate these with bio_alloc_bioset, using the front_pad parameter when
9094818742SKent Overstreet  * the bioset is created - this means the bio has to come at the end of the
9194818742SKent Overstreet  * struct.
928fbf26adSKiyoshi Ueda  */
938fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info {
948fbf26adSKiyoshi Ueda 	struct bio *orig;
95cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio;
9694818742SKent Overstreet 	struct bio clone;
978fbf26adSKiyoshi Ueda };
988fbf26adSKiyoshi Ueda 
99cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq)
100cec47e3dSKiyoshi Ueda {
101cec47e3dSKiyoshi Ueda 	if (rq && rq->end_io_data)
102cec47e3dSKiyoshi Ueda 		return &((struct dm_rq_target_io *)rq->end_io_data)->info;
103cec47e3dSKiyoshi Ueda 	return NULL;
104cec47e3dSKiyoshi Ueda }
105cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
106cec47e3dSKiyoshi Ueda 
107ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
108ba61fdd1SJeff Mahoney 
1091da177e4SLinus Torvalds /*
1101da177e4SLinus Torvalds  * Bits for the md->flags field.
1111da177e4SLinus Torvalds  */
1121eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
1131da177e4SLinus Torvalds #define DMF_SUSPENDED 1
114aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
115fba9f90eSJeff Mahoney #define DMF_FREEING 3
1165c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
1172e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
118d5b9dd04SMikulas Patocka #define DMF_MERGE_IS_OPTIONAL 6
1192c140a24SMikulas Patocka #define DMF_DEFERRED_REMOVE 7
1201da177e4SLinus Torvalds 
121304f3f6aSMilan Broz /*
12283d5e5b0SMikulas Patocka  * A dummy definition to make RCU happy.
12383d5e5b0SMikulas Patocka  * struct dm_table should never be dereferenced in this file.
12483d5e5b0SMikulas Patocka  */
12583d5e5b0SMikulas Patocka struct dm_table {
12683d5e5b0SMikulas Patocka 	int undefined__;
12783d5e5b0SMikulas Patocka };
12883d5e5b0SMikulas Patocka 
12983d5e5b0SMikulas Patocka /*
130304f3f6aSMilan Broz  * Work processed by per-device workqueue.
131304f3f6aSMilan Broz  */
1321da177e4SLinus Torvalds struct mapped_device {
13383d5e5b0SMikulas Patocka 	struct srcu_struct io_barrier;
134e61290a4SDaniel Walker 	struct mutex suspend_lock;
1351da177e4SLinus Torvalds 	atomic_t holders;
1365c6bd75dSAlasdair G Kergon 	atomic_t open_count;
1371da177e4SLinus Torvalds 
1382a7faeb1SMikulas Patocka 	/*
1392a7faeb1SMikulas Patocka 	 * The current mapping.
1402a7faeb1SMikulas Patocka 	 * Use dm_get_live_table{_fast} or take suspend_lock for
1412a7faeb1SMikulas Patocka 	 * dereference.
1422a7faeb1SMikulas Patocka 	 */
1432a7faeb1SMikulas Patocka 	struct dm_table *map;
1442a7faeb1SMikulas Patocka 
145*86f1152bSBenjamin Marzinski 	struct list_head table_devices;
146*86f1152bSBenjamin Marzinski 	struct mutex table_devices_lock;
147*86f1152bSBenjamin Marzinski 
1481da177e4SLinus Torvalds 	unsigned long flags;
1491da177e4SLinus Torvalds 
150165125e1SJens Axboe 	struct request_queue *queue;
151a5664dadSMike Snitzer 	unsigned type;
1524a0b4ddfSMike Snitzer 	/* Protect queue and type against concurrent access. */
153a5664dadSMike Snitzer 	struct mutex type_lock;
154a5664dadSMike Snitzer 
15536a0456fSAlasdair G Kergon 	struct target_type *immutable_target_type;
15636a0456fSAlasdair G Kergon 
1571da177e4SLinus Torvalds 	struct gendisk *disk;
1587e51f257SMike Anderson 	char name[16];
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	void *interface_ptr;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	/*
1631da177e4SLinus Torvalds 	 * A list of ios that arrived while we were suspended.
1641da177e4SLinus Torvalds 	 */
165316d315bSNikanth Karthikesan 	atomic_t pending[2];
1661da177e4SLinus Torvalds 	wait_queue_head_t wait;
16753d5914fSMikulas Patocka 	struct work_struct work;
1681da177e4SLinus Torvalds 	struct bio_list deferred;
169022c2611SMikulas Patocka 	spinlock_t deferred_lock;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	/*
17229e4013dSTejun Heo 	 * Processing queue (flush)
173304f3f6aSMilan Broz 	 */
174304f3f6aSMilan Broz 	struct workqueue_struct *wq;
175304f3f6aSMilan Broz 
176304f3f6aSMilan Broz 	/*
1771da177e4SLinus Torvalds 	 * io objects are allocated from here.
1781da177e4SLinus Torvalds 	 */
1791da177e4SLinus Torvalds 	mempool_t *io_pool;
1801da177e4SLinus Torvalds 
1819faf400fSStefan Bader 	struct bio_set *bs;
1829faf400fSStefan Bader 
1831da177e4SLinus Torvalds 	/*
1841da177e4SLinus Torvalds 	 * Event handling.
1851da177e4SLinus Torvalds 	 */
1861da177e4SLinus Torvalds 	atomic_t event_nr;
1871da177e4SLinus Torvalds 	wait_queue_head_t eventq;
1887a8c3d3bSMike Anderson 	atomic_t uevent_seq;
1897a8c3d3bSMike Anderson 	struct list_head uevent_list;
1907a8c3d3bSMike Anderson 	spinlock_t uevent_lock; /* Protect access to uevent_list */
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	/*
1931da177e4SLinus Torvalds 	 * freeze/thaw support require holding onto a super block
1941da177e4SLinus Torvalds 	 */
1951da177e4SLinus Torvalds 	struct super_block *frozen_sb;
196db8fef4fSMikulas Patocka 	struct block_device *bdev;
1973ac51e74SDarrick J. Wong 
1983ac51e74SDarrick J. Wong 	/* forced geometry settings */
1993ac51e74SDarrick J. Wong 	struct hd_geometry geometry;
200784aae73SMilan Broz 
2012995fa78SMikulas Patocka 	/* kobject and completion */
2022995fa78SMikulas Patocka 	struct dm_kobject_holder kobj_holder;
203be35f486SMikulas Patocka 
204d87f4c14STejun Heo 	/* zero-length flush that will be cloned and submitted to targets */
205d87f4c14STejun Heo 	struct bio flush_bio;
206fd2ed4d2SMikulas Patocka 
207fd2ed4d2SMikulas Patocka 	struct dm_stats stats;
2081da177e4SLinus Torvalds };
2091da177e4SLinus Torvalds 
210e6ee8c0bSKiyoshi Ueda /*
211e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
212e6ee8c0bSKiyoshi Ueda  */
213e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
214e6ee8c0bSKiyoshi Ueda 	mempool_t *io_pool;
215e6ee8c0bSKiyoshi Ueda 	struct bio_set *bs;
216e6ee8c0bSKiyoshi Ueda };
217e6ee8c0bSKiyoshi Ueda 
218*86f1152bSBenjamin Marzinski struct table_device {
219*86f1152bSBenjamin Marzinski 	struct list_head list;
220*86f1152bSBenjamin Marzinski 	atomic_t count;
221*86f1152bSBenjamin Marzinski 	struct dm_dev dm_dev;
222*86f1152bSBenjamin Marzinski };
223*86f1152bSBenjamin Marzinski 
2246cfa5857SMike Snitzer #define RESERVED_BIO_BASED_IOS		16
2256cfa5857SMike Snitzer #define RESERVED_REQUEST_BASED_IOS	256
226f4790826SMike Snitzer #define RESERVED_MAX_IOS		1024
227e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
2288fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
22994818742SKent Overstreet 
230f4790826SMike Snitzer /*
231e8603136SMike Snitzer  * Bio-based DM's mempools' reserved IOs set by the user.
232e8603136SMike Snitzer  */
233e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
234e8603136SMike Snitzer 
235e8603136SMike Snitzer /*
236f4790826SMike Snitzer  * Request-based DM's mempools' reserved IOs set by the user.
237f4790826SMike Snitzer  */
238f4790826SMike Snitzer static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
239f4790826SMike Snitzer 
240f4790826SMike Snitzer static unsigned __dm_get_reserved_ios(unsigned *reserved_ios,
241f4790826SMike Snitzer 				      unsigned def, unsigned max)
242f4790826SMike Snitzer {
243f4790826SMike Snitzer 	unsigned ios = ACCESS_ONCE(*reserved_ios);
244f4790826SMike Snitzer 	unsigned modified_ios = 0;
245f4790826SMike Snitzer 
246f4790826SMike Snitzer 	if (!ios)
247f4790826SMike Snitzer 		modified_ios = def;
248f4790826SMike Snitzer 	else if (ios > max)
249f4790826SMike Snitzer 		modified_ios = max;
250f4790826SMike Snitzer 
251f4790826SMike Snitzer 	if (modified_ios) {
252f4790826SMike Snitzer 		(void)cmpxchg(reserved_ios, ios, modified_ios);
253f4790826SMike Snitzer 		ios = modified_ios;
254f4790826SMike Snitzer 	}
255f4790826SMike Snitzer 
256f4790826SMike Snitzer 	return ios;
257f4790826SMike Snitzer }
258f4790826SMike Snitzer 
259e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void)
260e8603136SMike Snitzer {
261e8603136SMike Snitzer 	return __dm_get_reserved_ios(&reserved_bio_based_ios,
262e8603136SMike Snitzer 				     RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
263e8603136SMike Snitzer }
264e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
265e8603136SMike Snitzer 
266f4790826SMike Snitzer unsigned dm_get_reserved_rq_based_ios(void)
267f4790826SMike Snitzer {
268f4790826SMike Snitzer 	return __dm_get_reserved_ios(&reserved_rq_based_ios,
269f4790826SMike Snitzer 				     RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
270f4790826SMike Snitzer }
271f4790826SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
272f4790826SMike Snitzer 
2731da177e4SLinus Torvalds static int __init local_init(void)
2741da177e4SLinus Torvalds {
27551157b4aSKiyoshi Ueda 	int r = -ENOMEM;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
278028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
2791da177e4SLinus Torvalds 	if (!_io_cache)
28051157b4aSKiyoshi Ueda 		return r;
2811da177e4SLinus Torvalds 
2828fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
2838fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
284dba14160SMikulas Patocka 		goto out_free_io_cache;
2858fbf26adSKiyoshi Ueda 
28651e5b2bdSMike Anderson 	r = dm_uevent_init();
28751157b4aSKiyoshi Ueda 	if (r)
28823e5083bSJun'ichi Nomura 		goto out_free_rq_tio_cache;
28951e5b2bdSMike Anderson 
290acfe0ad7SMikulas Patocka 	deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
291acfe0ad7SMikulas Patocka 	if (!deferred_remove_workqueue) {
292acfe0ad7SMikulas Patocka 		r = -ENOMEM;
293acfe0ad7SMikulas Patocka 		goto out_uevent_exit;
294acfe0ad7SMikulas Patocka 	}
295acfe0ad7SMikulas Patocka 
2961da177e4SLinus Torvalds 	_major = major;
2971da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
29851157b4aSKiyoshi Ueda 	if (r < 0)
299acfe0ad7SMikulas Patocka 		goto out_free_workqueue;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	if (!_major)
3021da177e4SLinus Torvalds 		_major = r;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	return 0;
30551157b4aSKiyoshi Ueda 
306acfe0ad7SMikulas Patocka out_free_workqueue:
307acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
30851157b4aSKiyoshi Ueda out_uevent_exit:
30951157b4aSKiyoshi Ueda 	dm_uevent_exit();
3108fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
3118fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
31251157b4aSKiyoshi Ueda out_free_io_cache:
31351157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
31451157b4aSKiyoshi Ueda 
31551157b4aSKiyoshi Ueda 	return r;
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds static void local_exit(void)
3191da177e4SLinus Torvalds {
3202c140a24SMikulas Patocka 	flush_scheduled_work();
321acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
3222c140a24SMikulas Patocka 
3238fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
3241da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
32500d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
32651e5b2bdSMike Anderson 	dm_uevent_exit();
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	_major = 0;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	DMINFO("cleaned up");
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds 
333b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
3341da177e4SLinus Torvalds 	local_init,
3351da177e4SLinus Torvalds 	dm_target_init,
3361da177e4SLinus Torvalds 	dm_linear_init,
3371da177e4SLinus Torvalds 	dm_stripe_init,
338952b3557SMikulas Patocka 	dm_io_init,
339945fa4d2SMikulas Patocka 	dm_kcopyd_init,
3401da177e4SLinus Torvalds 	dm_interface_init,
341fd2ed4d2SMikulas Patocka 	dm_statistics_init,
3421da177e4SLinus Torvalds };
3431da177e4SLinus Torvalds 
344b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
3451da177e4SLinus Torvalds 	local_exit,
3461da177e4SLinus Torvalds 	dm_target_exit,
3471da177e4SLinus Torvalds 	dm_linear_exit,
3481da177e4SLinus Torvalds 	dm_stripe_exit,
349952b3557SMikulas Patocka 	dm_io_exit,
350945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
3511da177e4SLinus Torvalds 	dm_interface_exit,
352fd2ed4d2SMikulas Patocka 	dm_statistics_exit,
3531da177e4SLinus Torvalds };
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds static int __init dm_init(void)
3561da177e4SLinus Torvalds {
3571da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	int r, i;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
3621da177e4SLinus Torvalds 		r = _inits[i]();
3631da177e4SLinus Torvalds 		if (r)
3641da177e4SLinus Torvalds 			goto bad;
3651da177e4SLinus Torvalds 	}
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	return 0;
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds       bad:
3701da177e4SLinus Torvalds 	while (i--)
3711da177e4SLinus Torvalds 		_exits[i]();
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	return r;
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds static void __exit dm_exit(void)
3771da177e4SLinus Torvalds {
3781da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	while (i--)
3811da177e4SLinus Torvalds 		_exits[i]();
382d15b774cSAlasdair G Kergon 
383d15b774cSAlasdair G Kergon 	/*
384d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
385d15b774cSAlasdair G Kergon 	 */
386d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds /*
3901da177e4SLinus Torvalds  * Block device functions
3911da177e4SLinus Torvalds  */
392432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
393432a212cSMike Anderson {
394432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
395432a212cSMike Anderson }
396432a212cSMike Anderson 
397fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3981da177e4SLinus Torvalds {
3991da177e4SLinus Torvalds 	struct mapped_device *md;
4001da177e4SLinus Torvalds 
401fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
402fba9f90eSJeff Mahoney 
403fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
404fba9f90eSJeff Mahoney 	if (!md)
405fba9f90eSJeff Mahoney 		goto out;
406fba9f90eSJeff Mahoney 
4075c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
408432a212cSMike Anderson 	    dm_deleting_md(md)) {
409fba9f90eSJeff Mahoney 		md = NULL;
410fba9f90eSJeff Mahoney 		goto out;
411fba9f90eSJeff Mahoney 	}
412fba9f90eSJeff Mahoney 
4131da177e4SLinus Torvalds 	dm_get(md);
4145c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
415fba9f90eSJeff Mahoney 
416fba9f90eSJeff Mahoney out:
417fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
418fba9f90eSJeff Mahoney 
419fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
4201da177e4SLinus Torvalds }
4211da177e4SLinus Torvalds 
422db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode)
4231da177e4SLinus Torvalds {
424fe5f9f2cSAl Viro 	struct mapped_device *md = disk->private_data;
4256e9624b8SArnd Bergmann 
4264a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
4274a1aeb98SMilan Broz 
4282c140a24SMikulas Patocka 	if (atomic_dec_and_test(&md->open_count) &&
4292c140a24SMikulas Patocka 	    (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
430acfe0ad7SMikulas Patocka 		queue_work(deferred_remove_workqueue, &deferred_remove_work);
4312c140a24SMikulas Patocka 
4321da177e4SLinus Torvalds 	dm_put(md);
4334a1aeb98SMilan Broz 
4344a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
4375c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
4385c6bd75dSAlasdair G Kergon {
4395c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
4405c6bd75dSAlasdair G Kergon }
4415c6bd75dSAlasdair G Kergon 
4425c6bd75dSAlasdair G Kergon /*
4435c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
4445c6bd75dSAlasdair G Kergon  */
4452c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
4465c6bd75dSAlasdair G Kergon {
4475c6bd75dSAlasdair G Kergon 	int r = 0;
4485c6bd75dSAlasdair G Kergon 
4495c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
4505c6bd75dSAlasdair G Kergon 
4512c140a24SMikulas Patocka 	if (dm_open_count(md)) {
4525c6bd75dSAlasdair G Kergon 		r = -EBUSY;
4532c140a24SMikulas Patocka 		if (mark_deferred)
4542c140a24SMikulas Patocka 			set_bit(DMF_DEFERRED_REMOVE, &md->flags);
4552c140a24SMikulas Patocka 	} else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
4562c140a24SMikulas Patocka 		r = -EEXIST;
4575c6bd75dSAlasdair G Kergon 	else
4585c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
4595c6bd75dSAlasdair G Kergon 
4605c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
4615c6bd75dSAlasdair G Kergon 
4625c6bd75dSAlasdair G Kergon 	return r;
4635c6bd75dSAlasdair G Kergon }
4645c6bd75dSAlasdair G Kergon 
4652c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md)
4662c140a24SMikulas Patocka {
4672c140a24SMikulas Patocka 	int r = 0;
4682c140a24SMikulas Patocka 
4692c140a24SMikulas Patocka 	spin_lock(&_minor_lock);
4702c140a24SMikulas Patocka 
4712c140a24SMikulas Patocka 	if (test_bit(DMF_DELETING, &md->flags))
4722c140a24SMikulas Patocka 		r = -EBUSY;
4732c140a24SMikulas Patocka 	else
4742c140a24SMikulas Patocka 		clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
4752c140a24SMikulas Patocka 
4762c140a24SMikulas Patocka 	spin_unlock(&_minor_lock);
4772c140a24SMikulas Patocka 
4782c140a24SMikulas Patocka 	return r;
4792c140a24SMikulas Patocka }
4802c140a24SMikulas Patocka 
4812c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w)
4822c140a24SMikulas Patocka {
4832c140a24SMikulas Patocka 	dm_deferred_remove();
4842c140a24SMikulas Patocka }
4852c140a24SMikulas Patocka 
486fd2ed4d2SMikulas Patocka sector_t dm_get_size(struct mapped_device *md)
487fd2ed4d2SMikulas Patocka {
488fd2ed4d2SMikulas Patocka 	return get_capacity(md->disk);
489fd2ed4d2SMikulas Patocka }
490fd2ed4d2SMikulas Patocka 
4919974fa2cSMike Snitzer struct request_queue *dm_get_md_queue(struct mapped_device *md)
4929974fa2cSMike Snitzer {
4939974fa2cSMike Snitzer 	return md->queue;
4949974fa2cSMike Snitzer }
4959974fa2cSMike Snitzer 
496fd2ed4d2SMikulas Patocka struct dm_stats *dm_get_stats(struct mapped_device *md)
497fd2ed4d2SMikulas Patocka {
498fd2ed4d2SMikulas Patocka 	return &md->stats;
499fd2ed4d2SMikulas Patocka }
500fd2ed4d2SMikulas Patocka 
5013ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5023ac51e74SDarrick J. Wong {
5033ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
5043ac51e74SDarrick J. Wong 
5053ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
5063ac51e74SDarrick J. Wong }
5073ac51e74SDarrick J. Wong 
508fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
509aa129a22SMilan Broz 			unsigned int cmd, unsigned long arg)
510aa129a22SMilan Broz {
511fe5f9f2cSAl Viro 	struct mapped_device *md = bdev->bd_disk->private_data;
51283d5e5b0SMikulas Patocka 	int srcu_idx;
5136c182cd8SHannes Reinecke 	struct dm_table *map;
514aa129a22SMilan Broz 	struct dm_target *tgt;
515aa129a22SMilan Broz 	int r = -ENOTTY;
516aa129a22SMilan Broz 
5176c182cd8SHannes Reinecke retry:
51883d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
51983d5e5b0SMikulas Patocka 
520aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
521aa129a22SMilan Broz 		goto out;
522aa129a22SMilan Broz 
523aa129a22SMilan Broz 	/* We only support devices that have a single target */
524aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
525aa129a22SMilan Broz 		goto out;
526aa129a22SMilan Broz 
527aa129a22SMilan Broz 	tgt = dm_table_get_target(map, 0);
528aa129a22SMilan Broz 
5294f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
530aa129a22SMilan Broz 		r = -EAGAIN;
531aa129a22SMilan Broz 		goto out;
532aa129a22SMilan Broz 	}
533aa129a22SMilan Broz 
534aa129a22SMilan Broz 	if (tgt->type->ioctl)
535647b3d00SAl Viro 		r = tgt->type->ioctl(tgt, cmd, arg);
536aa129a22SMilan Broz 
537aa129a22SMilan Broz out:
53883d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
539aa129a22SMilan Broz 
5406c182cd8SHannes Reinecke 	if (r == -ENOTCONN) {
5416c182cd8SHannes Reinecke 		msleep(10);
5426c182cd8SHannes Reinecke 		goto retry;
5436c182cd8SHannes Reinecke 	}
5446c182cd8SHannes Reinecke 
545aa129a22SMilan Broz 	return r;
546aa129a22SMilan Broz }
547aa129a22SMilan Broz 
548028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
5491da177e4SLinus Torvalds {
5501da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
553028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
5541da177e4SLinus Torvalds {
5551da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
5561da177e4SLinus Torvalds }
5571da177e4SLinus Torvalds 
558028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
5591da177e4SLinus Torvalds {
560dba14160SMikulas Patocka 	bio_put(&tio->clone);
5611da177e4SLinus Torvalds }
5621da177e4SLinus Torvalds 
56308885643SKiyoshi Ueda static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
56408885643SKiyoshi Ueda 					    gfp_t gfp_mask)
565cec47e3dSKiyoshi Ueda {
5665f015204SJun'ichi Nomura 	return mempool_alloc(md->io_pool, gfp_mask);
567cec47e3dSKiyoshi Ueda }
568cec47e3dSKiyoshi Ueda 
569cec47e3dSKiyoshi Ueda static void free_rq_tio(struct dm_rq_target_io *tio)
570cec47e3dSKiyoshi Ueda {
5715f015204SJun'ichi Nomura 	mempool_free(tio, tio->md->io_pool);
572cec47e3dSKiyoshi Ueda }
573cec47e3dSKiyoshi Ueda 
57490abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md)
57590abb8c4SKiyoshi Ueda {
57690abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
57790abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
57890abb8c4SKiyoshi Ueda }
57990abb8c4SKiyoshi Ueda 
5803eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
5813eaf840eSJun'ichi "Nick" Nomura {
5823eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
583fd2ed4d2SMikulas Patocka 	struct bio *bio = io->bio;
584c9959059STejun Heo 	int cpu;
585fd2ed4d2SMikulas Patocka 	int rw = bio_data_dir(bio);
5863eaf840eSJun'ichi "Nick" Nomura 
5873eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
5883eaf840eSJun'ichi "Nick" Nomura 
589074a7acaSTejun Heo 	cpu = part_stat_lock();
590074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
591074a7acaSTejun Heo 	part_stat_unlock();
5921e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw],
5931e9bb880SShaohua Li 		atomic_inc_return(&md->pending[rw]));
594fd2ed4d2SMikulas Patocka 
595fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
5964f024f37SKent Overstreet 		dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
597fd2ed4d2SMikulas Patocka 				    bio_sectors(bio), false, 0, &io->stats_aux);
5983eaf840eSJun'ichi "Nick" Nomura }
5993eaf840eSJun'ichi "Nick" Nomura 
600d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
6013eaf840eSJun'ichi "Nick" Nomura {
6023eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
6033eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
6043eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
605c9959059STejun Heo 	int pending, cpu;
6063eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
6073eaf840eSJun'ichi "Nick" Nomura 
608074a7acaSTejun Heo 	cpu = part_stat_lock();
609074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
610074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
611074a7acaSTejun Heo 	part_stat_unlock();
6123eaf840eSJun'ichi "Nick" Nomura 
613fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
6144f024f37SKent Overstreet 		dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
615fd2ed4d2SMikulas Patocka 				    bio_sectors(bio), true, duration, &io->stats_aux);
616fd2ed4d2SMikulas Patocka 
617af7e466aSMikulas Patocka 	/*
618af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
619d87f4c14STejun Heo 	 * a flush.
620af7e466aSMikulas Patocka 	 */
6211e9bb880SShaohua Li 	pending = atomic_dec_return(&md->pending[rw]);
6221e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
623316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
6243eaf840eSJun'ichi "Nick" Nomura 
625d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
626d221d2e7SMikulas Patocka 	if (!pending)
627d221d2e7SMikulas Patocka 		wake_up(&md->wait);
6283eaf840eSJun'ichi "Nick" Nomura }
6293eaf840eSJun'ichi "Nick" Nomura 
6301da177e4SLinus Torvalds /*
6311da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
6321da177e4SLinus Torvalds  */
63392c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
6341da177e4SLinus Torvalds {
63505447420SKiyoshi Ueda 	unsigned long flags;
6361da177e4SLinus Torvalds 
63705447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
6381da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
63905447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
64092c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
6411da177e4SLinus Torvalds }
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds /*
6441da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
6451da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
64683d5e5b0SMikulas Patocka  * dm_put_live_table() when finished.
6471da177e4SLinus Torvalds  */
64883d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
6491da177e4SLinus Torvalds {
65083d5e5b0SMikulas Patocka 	*srcu_idx = srcu_read_lock(&md->io_barrier);
6511da177e4SLinus Torvalds 
65283d5e5b0SMikulas Patocka 	return srcu_dereference(md->map, &md->io_barrier);
65383d5e5b0SMikulas Patocka }
6541da177e4SLinus Torvalds 
65583d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
65683d5e5b0SMikulas Patocka {
65783d5e5b0SMikulas Patocka 	srcu_read_unlock(&md->io_barrier, srcu_idx);
65883d5e5b0SMikulas Patocka }
65983d5e5b0SMikulas Patocka 
66083d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md)
66183d5e5b0SMikulas Patocka {
66283d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
66383d5e5b0SMikulas Patocka 	synchronize_rcu_expedited();
66483d5e5b0SMikulas Patocka }
66583d5e5b0SMikulas Patocka 
66683d5e5b0SMikulas Patocka /*
66783d5e5b0SMikulas Patocka  * A fast alternative to dm_get_live_table/dm_put_live_table.
66883d5e5b0SMikulas Patocka  * The caller must not block between these two functions.
66983d5e5b0SMikulas Patocka  */
67083d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
67183d5e5b0SMikulas Patocka {
67283d5e5b0SMikulas Patocka 	rcu_read_lock();
67383d5e5b0SMikulas Patocka 	return rcu_dereference(md->map);
67483d5e5b0SMikulas Patocka }
67583d5e5b0SMikulas Patocka 
67683d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
67783d5e5b0SMikulas Patocka {
67883d5e5b0SMikulas Patocka 	rcu_read_unlock();
6791da177e4SLinus Torvalds }
6801da177e4SLinus Torvalds 
6813ac51e74SDarrick J. Wong /*
682*86f1152bSBenjamin Marzinski  * Open a table device so we can use it as a map destination.
683*86f1152bSBenjamin Marzinski  */
684*86f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev,
685*86f1152bSBenjamin Marzinski 			     struct mapped_device *md)
686*86f1152bSBenjamin Marzinski {
687*86f1152bSBenjamin Marzinski 	static char *_claim_ptr = "I belong to device-mapper";
688*86f1152bSBenjamin Marzinski 	struct block_device *bdev;
689*86f1152bSBenjamin Marzinski 
690*86f1152bSBenjamin Marzinski 	int r;
691*86f1152bSBenjamin Marzinski 
692*86f1152bSBenjamin Marzinski 	BUG_ON(td->dm_dev.bdev);
693*86f1152bSBenjamin Marzinski 
694*86f1152bSBenjamin Marzinski 	bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
695*86f1152bSBenjamin Marzinski 	if (IS_ERR(bdev))
696*86f1152bSBenjamin Marzinski 		return PTR_ERR(bdev);
697*86f1152bSBenjamin Marzinski 
698*86f1152bSBenjamin Marzinski 	r = bd_link_disk_holder(bdev, dm_disk(md));
699*86f1152bSBenjamin Marzinski 	if (r) {
700*86f1152bSBenjamin Marzinski 		blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
701*86f1152bSBenjamin Marzinski 		return r;
702*86f1152bSBenjamin Marzinski 	}
703*86f1152bSBenjamin Marzinski 
704*86f1152bSBenjamin Marzinski 	td->dm_dev.bdev = bdev;
705*86f1152bSBenjamin Marzinski 	return 0;
706*86f1152bSBenjamin Marzinski }
707*86f1152bSBenjamin Marzinski 
708*86f1152bSBenjamin Marzinski /*
709*86f1152bSBenjamin Marzinski  * Close a table device that we've been using.
710*86f1152bSBenjamin Marzinski  */
711*86f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md)
712*86f1152bSBenjamin Marzinski {
713*86f1152bSBenjamin Marzinski 	if (!td->dm_dev.bdev)
714*86f1152bSBenjamin Marzinski 		return;
715*86f1152bSBenjamin Marzinski 
716*86f1152bSBenjamin Marzinski 	bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
717*86f1152bSBenjamin Marzinski 	blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
718*86f1152bSBenjamin Marzinski 	td->dm_dev.bdev = NULL;
719*86f1152bSBenjamin Marzinski }
720*86f1152bSBenjamin Marzinski 
721*86f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev,
722*86f1152bSBenjamin Marzinski 					      fmode_t mode) {
723*86f1152bSBenjamin Marzinski 	struct table_device *td;
724*86f1152bSBenjamin Marzinski 
725*86f1152bSBenjamin Marzinski 	list_for_each_entry(td, l, list)
726*86f1152bSBenjamin Marzinski 		if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
727*86f1152bSBenjamin Marzinski 			return td;
728*86f1152bSBenjamin Marzinski 
729*86f1152bSBenjamin Marzinski 	return NULL;
730*86f1152bSBenjamin Marzinski }
731*86f1152bSBenjamin Marzinski 
732*86f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
733*86f1152bSBenjamin Marzinski 			struct dm_dev **result) {
734*86f1152bSBenjamin Marzinski 	int r;
735*86f1152bSBenjamin Marzinski 	struct table_device *td;
736*86f1152bSBenjamin Marzinski 
737*86f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
738*86f1152bSBenjamin Marzinski 	td = find_table_device(&md->table_devices, dev, mode);
739*86f1152bSBenjamin Marzinski 	if (!td) {
740*86f1152bSBenjamin Marzinski 		td = kmalloc(sizeof(*td), GFP_KERNEL);
741*86f1152bSBenjamin Marzinski 		if (!td) {
742*86f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
743*86f1152bSBenjamin Marzinski 			return -ENOMEM;
744*86f1152bSBenjamin Marzinski 		}
745*86f1152bSBenjamin Marzinski 
746*86f1152bSBenjamin Marzinski 		td->dm_dev.mode = mode;
747*86f1152bSBenjamin Marzinski 		td->dm_dev.bdev = NULL;
748*86f1152bSBenjamin Marzinski 
749*86f1152bSBenjamin Marzinski 		if ((r = open_table_device(td, dev, md))) {
750*86f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
751*86f1152bSBenjamin Marzinski 			kfree(td);
752*86f1152bSBenjamin Marzinski 			return r;
753*86f1152bSBenjamin Marzinski 		}
754*86f1152bSBenjamin Marzinski 
755*86f1152bSBenjamin Marzinski 		format_dev_t(td->dm_dev.name, dev);
756*86f1152bSBenjamin Marzinski 
757*86f1152bSBenjamin Marzinski 		atomic_set(&td->count, 0);
758*86f1152bSBenjamin Marzinski 		list_add(&td->list, &md->table_devices);
759*86f1152bSBenjamin Marzinski 	}
760*86f1152bSBenjamin Marzinski 	atomic_inc(&td->count);
761*86f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
762*86f1152bSBenjamin Marzinski 
763*86f1152bSBenjamin Marzinski 	*result = &td->dm_dev;
764*86f1152bSBenjamin Marzinski 	return 0;
765*86f1152bSBenjamin Marzinski }
766*86f1152bSBenjamin Marzinski EXPORT_SYMBOL_GPL(dm_get_table_device);
767*86f1152bSBenjamin Marzinski 
768*86f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
769*86f1152bSBenjamin Marzinski {
770*86f1152bSBenjamin Marzinski 	struct table_device *td = container_of(d, struct table_device, dm_dev);
771*86f1152bSBenjamin Marzinski 
772*86f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
773*86f1152bSBenjamin Marzinski 	if (atomic_dec_and_test(&td->count)) {
774*86f1152bSBenjamin Marzinski 		close_table_device(td, md);
775*86f1152bSBenjamin Marzinski 		list_del(&td->list);
776*86f1152bSBenjamin Marzinski 		kfree(td);
777*86f1152bSBenjamin Marzinski 	}
778*86f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
779*86f1152bSBenjamin Marzinski }
780*86f1152bSBenjamin Marzinski EXPORT_SYMBOL(dm_put_table_device);
781*86f1152bSBenjamin Marzinski 
782*86f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices)
783*86f1152bSBenjamin Marzinski {
784*86f1152bSBenjamin Marzinski 	struct list_head *tmp, *next;
785*86f1152bSBenjamin Marzinski 
786*86f1152bSBenjamin Marzinski 	list_for_each_safe(tmp, next, devices) {
787*86f1152bSBenjamin Marzinski 		struct table_device *td = list_entry(tmp, struct table_device, list);
788*86f1152bSBenjamin Marzinski 
789*86f1152bSBenjamin Marzinski 		DMWARN("dm_destroy: %s still exists with %d references",
790*86f1152bSBenjamin Marzinski 		       td->dm_dev.name, atomic_read(&td->count));
791*86f1152bSBenjamin Marzinski 		kfree(td);
792*86f1152bSBenjamin Marzinski 	}
793*86f1152bSBenjamin Marzinski }
794*86f1152bSBenjamin Marzinski 
795*86f1152bSBenjamin Marzinski /*
7963ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
7973ac51e74SDarrick J. Wong  */
7983ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
7993ac51e74SDarrick J. Wong {
8003ac51e74SDarrick J. Wong 	*geo = md->geometry;
8013ac51e74SDarrick J. Wong 
8023ac51e74SDarrick J. Wong 	return 0;
8033ac51e74SDarrick J. Wong }
8043ac51e74SDarrick J. Wong 
8053ac51e74SDarrick J. Wong /*
8063ac51e74SDarrick J. Wong  * Set the geometry of a device.
8073ac51e74SDarrick J. Wong  */
8083ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
8093ac51e74SDarrick J. Wong {
8103ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
8113ac51e74SDarrick J. Wong 
8123ac51e74SDarrick J. Wong 	if (geo->start > sz) {
8133ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
8143ac51e74SDarrick J. Wong 		return -EINVAL;
8153ac51e74SDarrick J. Wong 	}
8163ac51e74SDarrick J. Wong 
8173ac51e74SDarrick J. Wong 	md->geometry = *geo;
8183ac51e74SDarrick J. Wong 
8193ac51e74SDarrick J. Wong 	return 0;
8203ac51e74SDarrick J. Wong }
8213ac51e74SDarrick J. Wong 
8221da177e4SLinus Torvalds /*-----------------------------------------------------------------
8231da177e4SLinus Torvalds  * CRUD START:
8241da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
8251da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
8261da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
8271da177e4SLinus Torvalds  *   interests of getting something for people to use I give
8281da177e4SLinus Torvalds  *   you this clearly demarcated crap.
8291da177e4SLinus Torvalds  *---------------------------------------------------------------*/
8301da177e4SLinus Torvalds 
8312e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
8322e93ccc1SKiyoshi Ueda {
8332e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
8342e93ccc1SKiyoshi Ueda }
8352e93ccc1SKiyoshi Ueda 
8361da177e4SLinus Torvalds /*
8371da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
8381da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
8391da177e4SLinus Torvalds  */
840858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
8411da177e4SLinus Torvalds {
8422e93ccc1SKiyoshi Ueda 	unsigned long flags;
843b35f8caaSMilan Broz 	int io_error;
844b35f8caaSMilan Broz 	struct bio *bio;
845b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
8462e93ccc1SKiyoshi Ueda 
8472e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
848f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
849f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
850f88fb981SKiyoshi Ueda 		if (!(io->error > 0 && __noflush_suspending(md)))
8511da177e4SLinus Torvalds 			io->error = error;
852f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
853f88fb981SKiyoshi Ueda 	}
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
8562e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
8572e93ccc1SKiyoshi Ueda 			/*
8582e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
8592e93ccc1SKiyoshi Ueda 			 */
860022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
8616a8736d1STejun Heo 			if (__noflush_suspending(md))
8626a8736d1STejun Heo 				bio_list_add_head(&md->deferred, io->bio);
8636a8736d1STejun Heo 			else
8642e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
8652e93ccc1SKiyoshi Ueda 				io->error = -EIO;
866022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
8672e93ccc1SKiyoshi Ueda 		}
8682e93ccc1SKiyoshi Ueda 
869b35f8caaSMilan Broz 		io_error = io->error;
870b35f8caaSMilan Broz 		bio = io->bio;
871af7e466aSMikulas Patocka 		end_io_acct(io);
872a97f925aSMikulas Patocka 		free_io(md, io);
8731da177e4SLinus Torvalds 
8746a8736d1STejun Heo 		if (io_error == DM_ENDIO_REQUEUE)
8756a8736d1STejun Heo 			return;
8766a8736d1STejun Heo 
8774f024f37SKent Overstreet 		if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
8781da177e4SLinus Torvalds 			/*
8796a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
8806a8736d1STejun Heo 			 * without REQ_FLUSH.
8811da177e4SLinus Torvalds 			 */
8826a8736d1STejun Heo 			bio->bi_rw &= ~REQ_FLUSH;
8836a8736d1STejun Heo 			queue_io(md, bio);
8845f3ea37cSArnaldo Carvalho de Melo 		} else {
885b372d360SMike Snitzer 			/* done with normal IO or empty flush */
8860a82a8d1SLinus Torvalds 			trace_block_bio_complete(md->queue, bio, io_error);
887b35f8caaSMilan Broz 			bio_endio(bio, io_error);
8882e93ccc1SKiyoshi Ueda 		}
8891da177e4SLinus Torvalds 	}
890af7e466aSMikulas Patocka }
8911da177e4SLinus Torvalds 
8927eee4ae2SMike Snitzer static void disable_write_same(struct mapped_device *md)
8937eee4ae2SMike Snitzer {
8947eee4ae2SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
8957eee4ae2SMike Snitzer 
8967eee4ae2SMike Snitzer 	/* device doesn't really support WRITE SAME, disable it */
8977eee4ae2SMike Snitzer 	limits->max_write_same_sectors = 0;
8987eee4ae2SMike Snitzer }
8997eee4ae2SMike Snitzer 
9006712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error)
9011da177e4SLinus Torvalds {
9021da177e4SLinus Torvalds 	int r = 0;
903bfc6d41cSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
904b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
9059faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
9061da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	if (!bio_flagged(bio, BIO_UPTODATE) && !error)
9091da177e4SLinus Torvalds 		error = -EIO;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	if (endio) {
9127de3ee57SMikulas Patocka 		r = endio(tio->ti, bio, error);
9132e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
9142e93ccc1SKiyoshi Ueda 			/*
9152e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
9162e93ccc1SKiyoshi Ueda 			 * in dec_pending().
9172e93ccc1SKiyoshi Ueda 			 */
9181da177e4SLinus Torvalds 			error = r;
91945cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
92045cbcd79SKiyoshi Ueda 			/* The target will handle the io */
9216712ecf8SNeilBrown 			return;
92245cbcd79SKiyoshi Ueda 		else if (r) {
92345cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
92445cbcd79SKiyoshi Ueda 			BUG();
92545cbcd79SKiyoshi Ueda 		}
9261da177e4SLinus Torvalds 	}
9271da177e4SLinus Torvalds 
9287eee4ae2SMike Snitzer 	if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) &&
9297eee4ae2SMike Snitzer 		     !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
9307eee4ae2SMike Snitzer 		disable_write_same(md);
9317eee4ae2SMike Snitzer 
9329faf400fSStefan Bader 	free_tio(md, tio);
933b35f8caaSMilan Broz 	dec_pending(io, error);
9341da177e4SLinus Torvalds }
9351da177e4SLinus Torvalds 
936cec47e3dSKiyoshi Ueda /*
937cec47e3dSKiyoshi Ueda  * Partial completion handling for request-based dm
938cec47e3dSKiyoshi Ueda  */
939cec47e3dSKiyoshi Ueda static void end_clone_bio(struct bio *clone, int error)
940cec47e3dSKiyoshi Ueda {
941bfc6d41cSMikulas Patocka 	struct dm_rq_clone_bio_info *info =
942bfc6d41cSMikulas Patocka 		container_of(clone, struct dm_rq_clone_bio_info, clone);
943cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = info->tio;
944cec47e3dSKiyoshi Ueda 	struct bio *bio = info->orig;
9454f024f37SKent Overstreet 	unsigned int nr_bytes = info->orig->bi_iter.bi_size;
946cec47e3dSKiyoshi Ueda 
947cec47e3dSKiyoshi Ueda 	bio_put(clone);
948cec47e3dSKiyoshi Ueda 
949cec47e3dSKiyoshi Ueda 	if (tio->error)
950cec47e3dSKiyoshi Ueda 		/*
951cec47e3dSKiyoshi Ueda 		 * An error has already been detected on the request.
952cec47e3dSKiyoshi Ueda 		 * Once error occurred, just let clone->end_io() handle
953cec47e3dSKiyoshi Ueda 		 * the remainder.
954cec47e3dSKiyoshi Ueda 		 */
955cec47e3dSKiyoshi Ueda 		return;
956cec47e3dSKiyoshi Ueda 	else if (error) {
957cec47e3dSKiyoshi Ueda 		/*
958cec47e3dSKiyoshi Ueda 		 * Don't notice the error to the upper layer yet.
959cec47e3dSKiyoshi Ueda 		 * The error handling decision is made by the target driver,
960cec47e3dSKiyoshi Ueda 		 * when the request is completed.
961cec47e3dSKiyoshi Ueda 		 */
962cec47e3dSKiyoshi Ueda 		tio->error = error;
963cec47e3dSKiyoshi Ueda 		return;
964cec47e3dSKiyoshi Ueda 	}
965cec47e3dSKiyoshi Ueda 
966cec47e3dSKiyoshi Ueda 	/*
967cec47e3dSKiyoshi Ueda 	 * I/O for the bio successfully completed.
968cec47e3dSKiyoshi Ueda 	 * Notice the data completion to the upper layer.
969cec47e3dSKiyoshi Ueda 	 */
970cec47e3dSKiyoshi Ueda 
971cec47e3dSKiyoshi Ueda 	/*
972cec47e3dSKiyoshi Ueda 	 * bios are processed from the head of the list.
973cec47e3dSKiyoshi Ueda 	 * So the completing bio should always be rq->bio.
974cec47e3dSKiyoshi Ueda 	 * If it's not, something wrong is happening.
975cec47e3dSKiyoshi Ueda 	 */
976cec47e3dSKiyoshi Ueda 	if (tio->orig->bio != bio)
977cec47e3dSKiyoshi Ueda 		DMERR("bio completion is going in the middle of the request");
978cec47e3dSKiyoshi Ueda 
979cec47e3dSKiyoshi Ueda 	/*
980cec47e3dSKiyoshi Ueda 	 * Update the original request.
981cec47e3dSKiyoshi Ueda 	 * Do not use blk_end_request() here, because it may complete
982cec47e3dSKiyoshi Ueda 	 * the original request before the clone, and break the ordering.
983cec47e3dSKiyoshi Ueda 	 */
984cec47e3dSKiyoshi Ueda 	blk_update_request(tio->orig, 0, nr_bytes);
985cec47e3dSKiyoshi Ueda }
986cec47e3dSKiyoshi Ueda 
987cec47e3dSKiyoshi Ueda /*
988cec47e3dSKiyoshi Ueda  * Don't touch any member of the md after calling this function because
989cec47e3dSKiyoshi Ueda  * the md may be freed in dm_put() at the end of this function.
990cec47e3dSKiyoshi Ueda  * Or do dm_get() before calling this function and dm_put() later.
991cec47e3dSKiyoshi Ueda  */
992b4324feeSKiyoshi Ueda static void rq_completed(struct mapped_device *md, int rw, int run_queue)
993cec47e3dSKiyoshi Ueda {
994b4324feeSKiyoshi Ueda 	atomic_dec(&md->pending[rw]);
995cec47e3dSKiyoshi Ueda 
996cec47e3dSKiyoshi Ueda 	/* nudge anyone waiting on suspend queue */
997b4324feeSKiyoshi Ueda 	if (!md_in_flight(md))
998cec47e3dSKiyoshi Ueda 		wake_up(&md->wait);
999cec47e3dSKiyoshi Ueda 
1000a8c32a5cSJens Axboe 	/*
1001a8c32a5cSJens Axboe 	 * Run this off this callpath, as drivers could invoke end_io while
1002a8c32a5cSJens Axboe 	 * inside their request_fn (and holding the queue lock). Calling
1003a8c32a5cSJens Axboe 	 * back into ->request_fn() could deadlock attempting to grab the
1004a8c32a5cSJens Axboe 	 * queue lock again.
1005a8c32a5cSJens Axboe 	 */
1006cec47e3dSKiyoshi Ueda 	if (run_queue)
1007a8c32a5cSJens Axboe 		blk_run_queue_async(md->queue);
1008cec47e3dSKiyoshi Ueda 
1009cec47e3dSKiyoshi Ueda 	/*
1010cec47e3dSKiyoshi Ueda 	 * dm_put() must be at the end of this function. See the comment above
1011cec47e3dSKiyoshi Ueda 	 */
1012cec47e3dSKiyoshi Ueda 	dm_put(md);
1013cec47e3dSKiyoshi Ueda }
1014cec47e3dSKiyoshi Ueda 
1015a77e28c7SKiyoshi Ueda static void free_rq_clone(struct request *clone)
1016a77e28c7SKiyoshi Ueda {
1017a77e28c7SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1018a77e28c7SKiyoshi Ueda 
1019a77e28c7SKiyoshi Ueda 	blk_rq_unprep_clone(clone);
1020a77e28c7SKiyoshi Ueda 	free_rq_tio(tio);
1021a77e28c7SKiyoshi Ueda }
1022a77e28c7SKiyoshi Ueda 
1023980691e5SKiyoshi Ueda /*
1024980691e5SKiyoshi Ueda  * Complete the clone and the original request.
1025980691e5SKiyoshi Ueda  * Must be called without queue lock.
1026980691e5SKiyoshi Ueda  */
1027980691e5SKiyoshi Ueda static void dm_end_request(struct request *clone, int error)
1028980691e5SKiyoshi Ueda {
1029980691e5SKiyoshi Ueda 	int rw = rq_data_dir(clone);
1030980691e5SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1031980691e5SKiyoshi Ueda 	struct mapped_device *md = tio->md;
1032980691e5SKiyoshi Ueda 	struct request *rq = tio->orig;
1033980691e5SKiyoshi Ueda 
103429e4013dSTejun Heo 	if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
1035980691e5SKiyoshi Ueda 		rq->errors = clone->errors;
1036980691e5SKiyoshi Ueda 		rq->resid_len = clone->resid_len;
1037980691e5SKiyoshi Ueda 
1038980691e5SKiyoshi Ueda 		if (rq->sense)
1039980691e5SKiyoshi Ueda 			/*
1040980691e5SKiyoshi Ueda 			 * We are using the sense buffer of the original
1041980691e5SKiyoshi Ueda 			 * request.
1042980691e5SKiyoshi Ueda 			 * So setting the length of the sense data is enough.
1043980691e5SKiyoshi Ueda 			 */
1044980691e5SKiyoshi Ueda 			rq->sense_len = clone->sense_len;
1045980691e5SKiyoshi Ueda 	}
1046980691e5SKiyoshi Ueda 
1047980691e5SKiyoshi Ueda 	free_rq_clone(clone);
1048980691e5SKiyoshi Ueda 	blk_end_request_all(rq, error);
104929e4013dSTejun Heo 	rq_completed(md, rw, true);
1050980691e5SKiyoshi Ueda }
1051980691e5SKiyoshi Ueda 
1052cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq)
1053cec47e3dSKiyoshi Ueda {
1054cec47e3dSKiyoshi Ueda 	struct request *clone = rq->special;
1055cec47e3dSKiyoshi Ueda 
1056cec47e3dSKiyoshi Ueda 	rq->special = NULL;
1057cec47e3dSKiyoshi Ueda 	rq->cmd_flags &= ~REQ_DONTPREP;
1058cec47e3dSKiyoshi Ueda 
1059a77e28c7SKiyoshi Ueda 	free_rq_clone(clone);
1060cec47e3dSKiyoshi Ueda }
1061cec47e3dSKiyoshi Ueda 
1062cec47e3dSKiyoshi Ueda /*
1063cec47e3dSKiyoshi Ueda  * Requeue the original request of a clone.
1064cec47e3dSKiyoshi Ueda  */
1065cec47e3dSKiyoshi Ueda void dm_requeue_unmapped_request(struct request *clone)
1066cec47e3dSKiyoshi Ueda {
1067b4324feeSKiyoshi Ueda 	int rw = rq_data_dir(clone);
1068cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1069cec47e3dSKiyoshi Ueda 	struct mapped_device *md = tio->md;
1070cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
1071cec47e3dSKiyoshi Ueda 	struct request_queue *q = rq->q;
1072cec47e3dSKiyoshi Ueda 	unsigned long flags;
1073cec47e3dSKiyoshi Ueda 
1074cec47e3dSKiyoshi Ueda 	dm_unprep_request(rq);
1075cec47e3dSKiyoshi Ueda 
1076cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
1077cec47e3dSKiyoshi Ueda 	blk_requeue_request(q, rq);
1078cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
1079cec47e3dSKiyoshi Ueda 
1080b4324feeSKiyoshi Ueda 	rq_completed(md, rw, 0);
1081cec47e3dSKiyoshi Ueda }
1082cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
1083cec47e3dSKiyoshi Ueda 
1084cec47e3dSKiyoshi Ueda static void __stop_queue(struct request_queue *q)
1085cec47e3dSKiyoshi Ueda {
1086cec47e3dSKiyoshi Ueda 	blk_stop_queue(q);
1087cec47e3dSKiyoshi Ueda }
1088cec47e3dSKiyoshi Ueda 
1089cec47e3dSKiyoshi Ueda static void stop_queue(struct request_queue *q)
1090cec47e3dSKiyoshi Ueda {
1091cec47e3dSKiyoshi Ueda 	unsigned long flags;
1092cec47e3dSKiyoshi Ueda 
1093cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
1094cec47e3dSKiyoshi Ueda 	__stop_queue(q);
1095cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
1096cec47e3dSKiyoshi Ueda }
1097cec47e3dSKiyoshi Ueda 
1098cec47e3dSKiyoshi Ueda static void __start_queue(struct request_queue *q)
1099cec47e3dSKiyoshi Ueda {
1100cec47e3dSKiyoshi Ueda 	if (blk_queue_stopped(q))
1101cec47e3dSKiyoshi Ueda 		blk_start_queue(q);
1102cec47e3dSKiyoshi Ueda }
1103cec47e3dSKiyoshi Ueda 
1104cec47e3dSKiyoshi Ueda static void start_queue(struct request_queue *q)
1105cec47e3dSKiyoshi Ueda {
1106cec47e3dSKiyoshi Ueda 	unsigned long flags;
1107cec47e3dSKiyoshi Ueda 
1108cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
1109cec47e3dSKiyoshi Ueda 	__start_queue(q);
1110cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
1111cec47e3dSKiyoshi Ueda }
1112cec47e3dSKiyoshi Ueda 
111311a68244SKiyoshi Ueda static void dm_done(struct request *clone, int error, bool mapped)
111411a68244SKiyoshi Ueda {
111511a68244SKiyoshi Ueda 	int r = error;
111611a68244SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1117ba1cbad9SMike Snitzer 	dm_request_endio_fn rq_end_io = NULL;
1118ba1cbad9SMike Snitzer 
1119ba1cbad9SMike Snitzer 	if (tio->ti) {
1120ba1cbad9SMike Snitzer 		rq_end_io = tio->ti->type->rq_end_io;
112111a68244SKiyoshi Ueda 
112211a68244SKiyoshi Ueda 		if (mapped && rq_end_io)
112311a68244SKiyoshi Ueda 			r = rq_end_io(tio->ti, clone, error, &tio->info);
1124ba1cbad9SMike Snitzer 	}
112511a68244SKiyoshi Ueda 
11267eee4ae2SMike Snitzer 	if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) &&
11277eee4ae2SMike Snitzer 		     !clone->q->limits.max_write_same_sectors))
11287eee4ae2SMike Snitzer 		disable_write_same(tio->md);
11297eee4ae2SMike Snitzer 
113011a68244SKiyoshi Ueda 	if (r <= 0)
113111a68244SKiyoshi Ueda 		/* The target wants to complete the I/O */
113211a68244SKiyoshi Ueda 		dm_end_request(clone, r);
113311a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_INCOMPLETE)
113411a68244SKiyoshi Ueda 		/* The target will handle the I/O */
113511a68244SKiyoshi Ueda 		return;
113611a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_REQUEUE)
113711a68244SKiyoshi Ueda 		/* The target wants to requeue the I/O */
113811a68244SKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
113911a68244SKiyoshi Ueda 	else {
114011a68244SKiyoshi Ueda 		DMWARN("unimplemented target endio return value: %d", r);
114111a68244SKiyoshi Ueda 		BUG();
114211a68244SKiyoshi Ueda 	}
114311a68244SKiyoshi Ueda }
114411a68244SKiyoshi Ueda 
1145cec47e3dSKiyoshi Ueda /*
1146cec47e3dSKiyoshi Ueda  * Request completion handler for request-based dm
1147cec47e3dSKiyoshi Ueda  */
1148cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq)
1149cec47e3dSKiyoshi Ueda {
115011a68244SKiyoshi Ueda 	bool mapped = true;
1151cec47e3dSKiyoshi Ueda 	struct request *clone = rq->completion_data;
1152cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1153cec47e3dSKiyoshi Ueda 
115411a68244SKiyoshi Ueda 	if (rq->cmd_flags & REQ_FAILED)
115511a68244SKiyoshi Ueda 		mapped = false;
1156cec47e3dSKiyoshi Ueda 
115711a68244SKiyoshi Ueda 	dm_done(clone, tio->error, mapped);
1158cec47e3dSKiyoshi Ueda }
1159cec47e3dSKiyoshi Ueda 
1160cec47e3dSKiyoshi Ueda /*
1161cec47e3dSKiyoshi Ueda  * Complete the clone and the original request with the error status
1162cec47e3dSKiyoshi Ueda  * through softirq context.
1163cec47e3dSKiyoshi Ueda  */
1164cec47e3dSKiyoshi Ueda static void dm_complete_request(struct request *clone, int error)
1165cec47e3dSKiyoshi Ueda {
1166cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1167cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
1168cec47e3dSKiyoshi Ueda 
1169cec47e3dSKiyoshi Ueda 	tio->error = error;
1170cec47e3dSKiyoshi Ueda 	rq->completion_data = clone;
1171cec47e3dSKiyoshi Ueda 	blk_complete_request(rq);
1172cec47e3dSKiyoshi Ueda }
1173cec47e3dSKiyoshi Ueda 
1174cec47e3dSKiyoshi Ueda /*
1175cec47e3dSKiyoshi Ueda  * Complete the not-mapped clone and the original request with the error status
1176cec47e3dSKiyoshi Ueda  * through softirq context.
1177cec47e3dSKiyoshi Ueda  * Target's rq_end_io() function isn't called.
1178cec47e3dSKiyoshi Ueda  * This may be used when the target's map_rq() function fails.
1179cec47e3dSKiyoshi Ueda  */
1180cec47e3dSKiyoshi Ueda void dm_kill_unmapped_request(struct request *clone, int error)
1181cec47e3dSKiyoshi Ueda {
1182cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1183cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
1184cec47e3dSKiyoshi Ueda 
1185cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_FAILED;
1186cec47e3dSKiyoshi Ueda 	dm_complete_request(clone, error);
1187cec47e3dSKiyoshi Ueda }
1188cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
1189cec47e3dSKiyoshi Ueda 
1190cec47e3dSKiyoshi Ueda /*
1191cec47e3dSKiyoshi Ueda  * Called with the queue lock held
1192cec47e3dSKiyoshi Ueda  */
1193cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error)
1194cec47e3dSKiyoshi Ueda {
1195cec47e3dSKiyoshi Ueda 	/*
1196cec47e3dSKiyoshi Ueda 	 * For just cleaning up the information of the queue in which
1197cec47e3dSKiyoshi Ueda 	 * the clone was dispatched.
1198cec47e3dSKiyoshi Ueda 	 * The clone is *NOT* freed actually here because it is alloced from
1199cec47e3dSKiyoshi Ueda 	 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
1200cec47e3dSKiyoshi Ueda 	 */
1201cec47e3dSKiyoshi Ueda 	__blk_put_request(clone->q, clone);
1202cec47e3dSKiyoshi Ueda 
1203cec47e3dSKiyoshi Ueda 	/*
1204cec47e3dSKiyoshi Ueda 	 * Actual request completion is done in a softirq context which doesn't
1205cec47e3dSKiyoshi Ueda 	 * hold the queue lock.  Otherwise, deadlock could occur because:
1206cec47e3dSKiyoshi Ueda 	 *     - another request may be submitted by the upper level driver
1207cec47e3dSKiyoshi Ueda 	 *       of the stacking during the completion
1208cec47e3dSKiyoshi Ueda 	 *     - the submission which requires queue lock may be done
1209cec47e3dSKiyoshi Ueda 	 *       against this queue
1210cec47e3dSKiyoshi Ueda 	 */
1211cec47e3dSKiyoshi Ueda 	dm_complete_request(clone, error);
1212cec47e3dSKiyoshi Ueda }
1213cec47e3dSKiyoshi Ueda 
121456a67df7SMike Snitzer /*
121556a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
121656a67df7SMike Snitzer  * target boundary.
121756a67df7SMike Snitzer  */
121856a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
12191da177e4SLinus Torvalds {
122056a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
122156a67df7SMike Snitzer 
122256a67df7SMike Snitzer 	return ti->len - target_offset;
122356a67df7SMike Snitzer }
122456a67df7SMike Snitzer 
122556a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
122656a67df7SMike Snitzer {
122756a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
1228542f9038SMike Snitzer 	sector_t offset, max_len;
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 	/*
12311da177e4SLinus Torvalds 	 * Does the target need to split even further?
12321da177e4SLinus Torvalds 	 */
1233542f9038SMike Snitzer 	if (ti->max_io_len) {
1234542f9038SMike Snitzer 		offset = dm_target_offset(ti, sector);
1235542f9038SMike Snitzer 		if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1236542f9038SMike Snitzer 			max_len = sector_div(offset, ti->max_io_len);
1237542f9038SMike Snitzer 		else
1238542f9038SMike Snitzer 			max_len = offset & (ti->max_io_len - 1);
1239542f9038SMike Snitzer 		max_len = ti->max_io_len - max_len;
1240542f9038SMike Snitzer 
1241542f9038SMike Snitzer 		if (len > max_len)
1242542f9038SMike Snitzer 			len = max_len;
12431da177e4SLinus Torvalds 	}
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 	return len;
12461da177e4SLinus Torvalds }
12471da177e4SLinus Torvalds 
1248542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1249542f9038SMike Snitzer {
1250542f9038SMike Snitzer 	if (len > UINT_MAX) {
1251542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1252542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
1253542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
1254542f9038SMike Snitzer 		return -EINVAL;
1255542f9038SMike Snitzer 	}
1256542f9038SMike Snitzer 
1257542f9038SMike Snitzer 	ti->max_io_len = (uint32_t) len;
1258542f9038SMike Snitzer 
1259542f9038SMike Snitzer 	return 0;
1260542f9038SMike Snitzer }
1261542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1262542f9038SMike Snitzer 
12631dd40c3eSMikulas Patocka /*
12641dd40c3eSMikulas Patocka  * A target may call dm_accept_partial_bio only from the map routine.  It is
12651dd40c3eSMikulas Patocka  * allowed for all bio types except REQ_FLUSH.
12661dd40c3eSMikulas Patocka  *
12671dd40c3eSMikulas Patocka  * dm_accept_partial_bio informs the dm that the target only wants to process
12681dd40c3eSMikulas Patocka  * additional n_sectors sectors of the bio and the rest of the data should be
12691dd40c3eSMikulas Patocka  * sent in a next bio.
12701dd40c3eSMikulas Patocka  *
12711dd40c3eSMikulas Patocka  * A diagram that explains the arithmetics:
12721dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
12731dd40c3eSMikulas Patocka  * |         1          |       2       |   3   |
12741dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
12751dd40c3eSMikulas Patocka  *
12761dd40c3eSMikulas Patocka  * <-------------- *tio->len_ptr --------------->
12771dd40c3eSMikulas Patocka  *                      <------- bi_size ------->
12781dd40c3eSMikulas Patocka  *                      <-- n_sectors -->
12791dd40c3eSMikulas Patocka  *
12801dd40c3eSMikulas Patocka  * Region 1 was already iterated over with bio_advance or similar function.
12811dd40c3eSMikulas Patocka  *	(it may be empty if the target doesn't use bio_advance)
12821dd40c3eSMikulas Patocka  * Region 2 is the remaining bio size that the target wants to process.
12831dd40c3eSMikulas Patocka  *	(it may be empty if region 1 is non-empty, although there is no reason
12841dd40c3eSMikulas Patocka  *	 to make it empty)
12851dd40c3eSMikulas Patocka  * The target requires that region 3 is to be sent in the next bio.
12861dd40c3eSMikulas Patocka  *
12871dd40c3eSMikulas Patocka  * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
12881dd40c3eSMikulas Patocka  * the partially processed part (the sum of regions 1+2) must be the same for all
12891dd40c3eSMikulas Patocka  * copies of the bio.
12901dd40c3eSMikulas Patocka  */
12911dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
12921dd40c3eSMikulas Patocka {
12931dd40c3eSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
12941dd40c3eSMikulas Patocka 	unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
12951dd40c3eSMikulas Patocka 	BUG_ON(bio->bi_rw & REQ_FLUSH);
12961dd40c3eSMikulas Patocka 	BUG_ON(bi_size > *tio->len_ptr);
12971dd40c3eSMikulas Patocka 	BUG_ON(n_sectors > bi_size);
12981dd40c3eSMikulas Patocka 	*tio->len_ptr -= bi_size - n_sectors;
12991dd40c3eSMikulas Patocka 	bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
13001dd40c3eSMikulas Patocka }
13011dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
13021dd40c3eSMikulas Patocka 
1303bd2a49b8SAlasdair G Kergon static void __map_bio(struct dm_target_io *tio)
13041da177e4SLinus Torvalds {
13051da177e4SLinus Torvalds 	int r;
13062056a782SJens Axboe 	sector_t sector;
13079faf400fSStefan Bader 	struct mapped_device *md;
1308dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
1309bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds 	/*
13141da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
13151da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
13161da177e4SLinus Torvalds 	 * this io.
13171da177e4SLinus Torvalds 	 */
13181da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
13194f024f37SKent Overstreet 	sector = clone->bi_iter.bi_sector;
13207de3ee57SMikulas Patocka 	r = ti->type->map(ti, clone);
132145cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
13221da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
13232056a782SJens Axboe 
1324d07335e5SMike Snitzer 		trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
132522a7c31aSAlan D. Brunelle 				      tio->io->bio->bi_bdev->bd_dev, sector);
13262056a782SJens Axboe 
13271da177e4SLinus Torvalds 		generic_make_request(clone);
13282e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
13292e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
13309faf400fSStefan Bader 		md = tio->io->md;
13319faf400fSStefan Bader 		dec_pending(tio->io, r);
13329faf400fSStefan Bader 		free_tio(md, tio);
133345cbcd79SKiyoshi Ueda 	} else if (r) {
133445cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
133545cbcd79SKiyoshi Ueda 		BUG();
13361da177e4SLinus Torvalds 	}
13371da177e4SLinus Torvalds }
13381da177e4SLinus Torvalds 
13391da177e4SLinus Torvalds struct clone_info {
13401da177e4SLinus Torvalds 	struct mapped_device *md;
13411da177e4SLinus Torvalds 	struct dm_table *map;
13421da177e4SLinus Torvalds 	struct bio *bio;
13431da177e4SLinus Torvalds 	struct dm_io *io;
13441da177e4SLinus Torvalds 	sector_t sector;
1345e0d6609aSMikulas Patocka 	unsigned sector_count;
13461da177e4SLinus Torvalds };
13471da177e4SLinus Torvalds 
1348e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
1349bd2a49b8SAlasdair G Kergon {
13504f024f37SKent Overstreet 	bio->bi_iter.bi_sector = sector;
13514f024f37SKent Overstreet 	bio->bi_iter.bi_size = to_bytes(len);
13521da177e4SLinus Torvalds }
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds /*
13551da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
13561da177e4SLinus Torvalds  */
1357dba14160SMikulas Patocka static void clone_bio(struct dm_target_io *tio, struct bio *bio,
13581c3b13e6SKent Overstreet 		      sector_t sector, unsigned len)
13591da177e4SLinus Torvalds {
1360dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
13611da177e4SLinus Torvalds 
13621c3b13e6SKent Overstreet 	__bio_clone_fast(clone, bio);
13639c47008dSMartin K. Petersen 
13641c3b13e6SKent Overstreet 	if (bio_integrity(bio))
13651c3b13e6SKent Overstreet 		bio_integrity_clone(clone, bio, GFP_NOIO);
13661c3b13e6SKent Overstreet 
13671c3b13e6SKent Overstreet 	bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
13681c3b13e6SKent Overstreet 	clone->bi_iter.bi_size = to_bytes(len);
13691c3b13e6SKent Overstreet 
13701c3b13e6SKent Overstreet 	if (bio_integrity(bio))
13711c3b13e6SKent Overstreet 		bio_integrity_trim(clone, 0, len);
13721da177e4SLinus Torvalds }
13731da177e4SLinus Torvalds 
13749015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci,
137599778273SJunichi Nomura 				      struct dm_target *ti,
137655a62eefSAlasdair G Kergon 				      unsigned target_bio_nr)
1377f9ab94ceSMikulas Patocka {
1378dba14160SMikulas Patocka 	struct dm_target_io *tio;
1379dba14160SMikulas Patocka 	struct bio *clone;
1380dba14160SMikulas Patocka 
138199778273SJunichi Nomura 	clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1382dba14160SMikulas Patocka 	tio = container_of(clone, struct dm_target_io, clone);
1383f9ab94ceSMikulas Patocka 
1384f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1385f9ab94ceSMikulas Patocka 	tio->ti = ti;
138655a62eefSAlasdair G Kergon 	tio->target_bio_nr = target_bio_nr;
13879015df24SAlasdair G Kergon 
13889015df24SAlasdair G Kergon 	return tio;
13899015df24SAlasdair G Kergon }
13909015df24SAlasdair G Kergon 
139114fe594dSAlasdair G Kergon static void __clone_and_map_simple_bio(struct clone_info *ci,
139214fe594dSAlasdair G Kergon 				       struct dm_target *ti,
13931dd40c3eSMikulas Patocka 				       unsigned target_bio_nr, unsigned *len)
13949015df24SAlasdair G Kergon {
139599778273SJunichi Nomura 	struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
1396dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
13979015df24SAlasdair G Kergon 
13981dd40c3eSMikulas Patocka 	tio->len_ptr = len;
13991dd40c3eSMikulas Patocka 
14001c3b13e6SKent Overstreet 	__bio_clone_fast(clone, ci->bio);
1401bd2a49b8SAlasdair G Kergon 	if (len)
14021dd40c3eSMikulas Patocka 		bio_setup_sector(clone, ci->sector, *len);
1403f9ab94ceSMikulas Patocka 
1404bd2a49b8SAlasdair G Kergon 	__map_bio(tio);
1405f9ab94ceSMikulas Patocka }
1406f9ab94ceSMikulas Patocka 
140714fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
14081dd40c3eSMikulas Patocka 				  unsigned num_bios, unsigned *len)
140906a426ceSMike Snitzer {
141055a62eefSAlasdair G Kergon 	unsigned target_bio_nr;
141106a426ceSMike Snitzer 
141255a62eefSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
141314fe594dSAlasdair G Kergon 		__clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
141406a426ceSMike Snitzer }
141506a426ceSMike Snitzer 
141614fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1417f9ab94ceSMikulas Patocka {
141806a426ceSMike Snitzer 	unsigned target_nr = 0;
1419f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1420f9ab94ceSMikulas Patocka 
1421b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1422f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
14231dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
1424f9ab94ceSMikulas Patocka 
1425f9ab94ceSMikulas Patocka 	return 0;
1426f9ab94ceSMikulas Patocka }
1427f9ab94ceSMikulas Patocka 
1428e4c93811SAlasdair G Kergon static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
14291dd40c3eSMikulas Patocka 				     sector_t sector, unsigned *len)
14305ae89a87SMike Snitzer {
1431dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
14325ae89a87SMike Snitzer 	struct dm_target_io *tio;
1433b0d8ed4dSAlasdair G Kergon 	unsigned target_bio_nr;
1434b0d8ed4dSAlasdair G Kergon 	unsigned num_target_bios = 1;
14355ae89a87SMike Snitzer 
1436b0d8ed4dSAlasdair G Kergon 	/*
1437b0d8ed4dSAlasdair G Kergon 	 * Does the target want to receive duplicate copies of the bio?
1438b0d8ed4dSAlasdair G Kergon 	 */
1439b0d8ed4dSAlasdair G Kergon 	if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1440b0d8ed4dSAlasdair G Kergon 		num_target_bios = ti->num_write_bios(ti, bio);
1441e4c93811SAlasdair G Kergon 
1442b0d8ed4dSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
144399778273SJunichi Nomura 		tio = alloc_tio(ci, ti, target_bio_nr);
14441dd40c3eSMikulas Patocka 		tio->len_ptr = len;
14451dd40c3eSMikulas Patocka 		clone_bio(tio, bio, sector, *len);
1446bd2a49b8SAlasdair G Kergon 		__map_bio(tio);
14475ae89a87SMike Snitzer 	}
1448b0d8ed4dSAlasdair G Kergon }
14495ae89a87SMike Snitzer 
145055a62eefSAlasdair G Kergon typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
145123508a96SMike Snitzer 
145255a62eefSAlasdair G Kergon static unsigned get_num_discard_bios(struct dm_target *ti)
145323508a96SMike Snitzer {
145455a62eefSAlasdair G Kergon 	return ti->num_discard_bios;
145523508a96SMike Snitzer }
145623508a96SMike Snitzer 
145755a62eefSAlasdair G Kergon static unsigned get_num_write_same_bios(struct dm_target *ti)
145823508a96SMike Snitzer {
145955a62eefSAlasdair G Kergon 	return ti->num_write_same_bios;
146023508a96SMike Snitzer }
146123508a96SMike Snitzer 
146223508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti);
146323508a96SMike Snitzer 
146423508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti)
146523508a96SMike Snitzer {
146655a62eefSAlasdair G Kergon 	return ti->split_discard_bios;
146723508a96SMike Snitzer }
146823508a96SMike Snitzer 
146914fe594dSAlasdair G Kergon static int __send_changing_extent_only(struct clone_info *ci,
147055a62eefSAlasdair G Kergon 				       get_num_bios_fn get_num_bios,
147123508a96SMike Snitzer 				       is_split_required_fn is_split_required)
14725ae89a87SMike Snitzer {
14735ae89a87SMike Snitzer 	struct dm_target *ti;
1474e0d6609aSMikulas Patocka 	unsigned len;
147555a62eefSAlasdair G Kergon 	unsigned num_bios;
14765ae89a87SMike Snitzer 
1477a79245b3SMike Snitzer 	do {
14785ae89a87SMike Snitzer 		ti = dm_table_find_target(ci->map, ci->sector);
14795ae89a87SMike Snitzer 		if (!dm_target_is_valid(ti))
14805ae89a87SMike Snitzer 			return -EIO;
14815ae89a87SMike Snitzer 
14825ae89a87SMike Snitzer 		/*
148323508a96SMike Snitzer 		 * Even though the device advertised support for this type of
148423508a96SMike Snitzer 		 * request, that does not mean every target supports it, and
1485936688d7SMike Snitzer 		 * reconfiguration might also have changed that since the
14865ae89a87SMike Snitzer 		 * check was performed.
14875ae89a87SMike Snitzer 		 */
148855a62eefSAlasdair G Kergon 		num_bios = get_num_bios ? get_num_bios(ti) : 0;
148955a62eefSAlasdair G Kergon 		if (!num_bios)
14905ae89a87SMike Snitzer 			return -EOPNOTSUPP;
14915ae89a87SMike Snitzer 
149223508a96SMike Snitzer 		if (is_split_required && !is_split_required(ti))
1493e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
14947acf0277SMikulas Patocka 		else
1495e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
14965ae89a87SMike Snitzer 
14971dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, num_bios, &len);
14985ae89a87SMike Snitzer 
1499a79245b3SMike Snitzer 		ci->sector += len;
1500a79245b3SMike Snitzer 	} while (ci->sector_count -= len);
15015ae89a87SMike Snitzer 
15025ae89a87SMike Snitzer 	return 0;
15035ae89a87SMike Snitzer }
15045ae89a87SMike Snitzer 
150514fe594dSAlasdair G Kergon static int __send_discard(struct clone_info *ci)
150623508a96SMike Snitzer {
150714fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_discard_bios,
150823508a96SMike Snitzer 					   is_split_required_for_discard);
150923508a96SMike Snitzer }
151023508a96SMike Snitzer 
151114fe594dSAlasdair G Kergon static int __send_write_same(struct clone_info *ci)
151223508a96SMike Snitzer {
151314fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
151423508a96SMike Snitzer }
151523508a96SMike Snitzer 
1516e4c93811SAlasdair G Kergon /*
1517e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1518e4c93811SAlasdair G Kergon  */
1519e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci)
1520e4c93811SAlasdair G Kergon {
1521e4c93811SAlasdair G Kergon 	struct bio *bio = ci->bio;
1522e4c93811SAlasdair G Kergon 	struct dm_target *ti;
15231c3b13e6SKent Overstreet 	unsigned len;
1524e4c93811SAlasdair G Kergon 
1525e4c93811SAlasdair G Kergon 	if (unlikely(bio->bi_rw & REQ_DISCARD))
1526e4c93811SAlasdair G Kergon 		return __send_discard(ci);
1527e4c93811SAlasdair G Kergon 	else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
1528e4c93811SAlasdair G Kergon 		return __send_write_same(ci);
1529e4c93811SAlasdair G Kergon 
1530e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1531e4c93811SAlasdair G Kergon 	if (!dm_target_is_valid(ti))
1532e4c93811SAlasdair G Kergon 		return -EIO;
1533e4c93811SAlasdair G Kergon 
15341c3b13e6SKent Overstreet 	len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
1535e4c93811SAlasdair G Kergon 
15361dd40c3eSMikulas Patocka 	__clone_and_map_data_bio(ci, ti, ci->sector, &len);
1537e4c93811SAlasdair G Kergon 
1538e4c93811SAlasdair G Kergon 	ci->sector += len;
1539e4c93811SAlasdair G Kergon 	ci->sector_count -= len;
1540e4c93811SAlasdair G Kergon 
1541e4c93811SAlasdair G Kergon 	return 0;
1542e4c93811SAlasdair G Kergon }
1543e4c93811SAlasdair G Kergon 
1544e4c93811SAlasdair G Kergon /*
154514fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
15461da177e4SLinus Torvalds  */
154783d5e5b0SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md,
154883d5e5b0SMikulas Patocka 				    struct dm_table *map, struct bio *bio)
15491da177e4SLinus Torvalds {
15501da177e4SLinus Torvalds 	struct clone_info ci;
1551512875bdSJun'ichi Nomura 	int error = 0;
15521da177e4SLinus Torvalds 
155383d5e5b0SMikulas Patocka 	if (unlikely(!map)) {
1554f0b9a450SMikulas Patocka 		bio_io_error(bio);
1555f0b9a450SMikulas Patocka 		return;
1556f0b9a450SMikulas Patocka 	}
1557692d0eb9SMikulas Patocka 
155883d5e5b0SMikulas Patocka 	ci.map = map;
15591da177e4SLinus Torvalds 	ci.md = md;
15601da177e4SLinus Torvalds 	ci.io = alloc_io(md);
15611da177e4SLinus Torvalds 	ci.io->error = 0;
15621da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
15631da177e4SLinus Torvalds 	ci.io->bio = bio;
15641da177e4SLinus Torvalds 	ci.io->md = md;
1565f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
15664f024f37SKent Overstreet 	ci.sector = bio->bi_iter.bi_sector;
15671da177e4SLinus Torvalds 
15683eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1569bd2a49b8SAlasdair G Kergon 
1570b372d360SMike Snitzer 	if (bio->bi_rw & REQ_FLUSH) {
1571b372d360SMike Snitzer 		ci.bio = &ci.md->flush_bio;
1572b372d360SMike Snitzer 		ci.sector_count = 0;
157314fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1574b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1575b372d360SMike Snitzer 	} else {
15766a8736d1STejun Heo 		ci.bio = bio;
1577f6fccb12SMilan Broz 		ci.sector_count = bio_sectors(bio);
1578512875bdSJun'ichi Nomura 		while (ci.sector_count && !error)
157914fe594dSAlasdair G Kergon 			error = __split_and_process_non_flush(&ci);
1580d87f4c14STejun Heo 	}
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 	/* drop the extra reference count */
1583512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
15849e4e5f87SMilan Broz }
15859e4e5f87SMilan Broz /*-----------------------------------------------------------------
15861da177e4SLinus Torvalds  * CRUD END
15871da177e4SLinus Torvalds  *---------------------------------------------------------------*/
15881da177e4SLinus Torvalds 
15891da177e4SLinus Torvalds static int dm_merge_bvec(struct request_queue *q,
15901da177e4SLinus Torvalds 			 struct bvec_merge_data *bvm,
1591f6fccb12SMilan Broz 			 struct bio_vec *biovec)
1592f6fccb12SMilan Broz {
1593f6fccb12SMilan Broz 	struct mapped_device *md = q->queuedata;
159483d5e5b0SMikulas Patocka 	struct dm_table *map = dm_get_live_table_fast(md);
1595f6fccb12SMilan Broz 	struct dm_target *ti;
1596f6fccb12SMilan Broz 	sector_t max_sectors;
1597f6fccb12SMilan Broz 	int max_size = 0;
1598f6fccb12SMilan Broz 
1599f6fccb12SMilan Broz 	if (unlikely(!map))
1600f6fccb12SMilan Broz 		goto out;
1601f6fccb12SMilan Broz 
1602f6fccb12SMilan Broz 	ti = dm_table_find_target(map, bvm->bi_sector);
1603f6fccb12SMilan Broz 	if (!dm_target_is_valid(ti))
160483d5e5b0SMikulas Patocka 		goto out;
1605f6fccb12SMilan Broz 
1606f6fccb12SMilan Broz 	/*
1607f6fccb12SMilan Broz 	 * Find maximum amount of I/O that won't need splitting
1608f6fccb12SMilan Broz 	 */
160956a67df7SMike Snitzer 	max_sectors = min(max_io_len(bvm->bi_sector, ti),
1610f6fccb12SMilan Broz 			  (sector_t) BIO_MAX_SECTORS);
1611f6fccb12SMilan Broz 	max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1612f6fccb12SMilan Broz 	if (max_size < 0)
1613f6fccb12SMilan Broz 		max_size = 0;
1614f6fccb12SMilan Broz 
1615f6fccb12SMilan Broz 	/*
1616f6fccb12SMilan Broz 	 * merge_bvec_fn() returns number of bytes
1617f6fccb12SMilan Broz 	 * it can accept at this offset
1618f6fccb12SMilan Broz 	 * max is precomputed maximal io size
1619f6fccb12SMilan Broz 	 */
1620f6fccb12SMilan Broz 	if (max_size && ti->type->merge)
1621f6fccb12SMilan Broz 		max_size = ti->type->merge(ti, bvm, biovec, max_size);
16228cbeb67aSMikulas Patocka 	/*
16238cbeb67aSMikulas Patocka 	 * If the target doesn't support merge method and some of the devices
16248cbeb67aSMikulas Patocka 	 * provided their merge_bvec method (we know this by looking at
16258cbeb67aSMikulas Patocka 	 * queue_max_hw_sectors), then we can't allow bios with multiple vector
16268cbeb67aSMikulas Patocka 	 * entries.  So always set max_size to 0, and the code below allows
16278cbeb67aSMikulas Patocka 	 * just one page.
16288cbeb67aSMikulas Patocka 	 */
16298cbeb67aSMikulas Patocka 	else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
16308cbeb67aSMikulas Patocka 		max_size = 0;
1631f6fccb12SMilan Broz 
16325037108aSMikulas Patocka out:
163383d5e5b0SMikulas Patocka 	dm_put_live_table_fast(md);
1634f6fccb12SMilan Broz 	/*
1635f6fccb12SMilan Broz 	 * Always allow an entire first page
1636f6fccb12SMilan Broz 	 */
1637f6fccb12SMilan Broz 	if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1638f6fccb12SMilan Broz 		max_size = biovec->bv_len;
1639f6fccb12SMilan Broz 
1640f6fccb12SMilan Broz 	return max_size;
1641f6fccb12SMilan Broz }
1642f6fccb12SMilan Broz 
16431da177e4SLinus Torvalds /*
16441da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
16451da177e4SLinus Torvalds  * dm_merge_bvec.
16461da177e4SLinus Torvalds  */
16475a7bbad2SChristoph Hellwig static void _dm_request(struct request_queue *q, struct bio *bio)
16481da177e4SLinus Torvalds {
164912f03a49SKevin Corry 	int rw = bio_data_dir(bio);
16501da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
1651c9959059STejun Heo 	int cpu;
165283d5e5b0SMikulas Patocka 	int srcu_idx;
165383d5e5b0SMikulas Patocka 	struct dm_table *map;
16541da177e4SLinus Torvalds 
165583d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
16561da177e4SLinus Torvalds 
1657074a7acaSTejun Heo 	cpu = part_stat_lock();
1658074a7acaSTejun Heo 	part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1659074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1660074a7acaSTejun Heo 	part_stat_unlock();
166112f03a49SKevin Corry 
16626a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
16636a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
166483d5e5b0SMikulas Patocka 		dm_put_live_table(md, srcu_idx);
16651da177e4SLinus Torvalds 
16666a8736d1STejun Heo 		if (bio_rw(bio) != READA)
166792c63902SMikulas Patocka 			queue_io(md, bio);
16686a8736d1STejun Heo 		else
16696a8736d1STejun Heo 			bio_io_error(bio);
16705a7bbad2SChristoph Hellwig 		return;
16711da177e4SLinus Torvalds 	}
16721da177e4SLinus Torvalds 
167383d5e5b0SMikulas Patocka 	__split_and_process_bio(md, map, bio);
167483d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
16755a7bbad2SChristoph Hellwig 	return;
1676cec47e3dSKiyoshi Ueda }
1677cec47e3dSKiyoshi Ueda 
1678fd2ed4d2SMikulas Patocka int dm_request_based(struct mapped_device *md)
1679cec47e3dSKiyoshi Ueda {
1680cec47e3dSKiyoshi Ueda 	return blk_queue_stackable(md->queue);
1681cec47e3dSKiyoshi Ueda }
1682cec47e3dSKiyoshi Ueda 
16835a7bbad2SChristoph Hellwig static void dm_request(struct request_queue *q, struct bio *bio)
1684cec47e3dSKiyoshi Ueda {
1685cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1686cec47e3dSKiyoshi Ueda 
1687cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
16885a7bbad2SChristoph Hellwig 		blk_queue_bio(q, bio);
16895a7bbad2SChristoph Hellwig 	else
16905a7bbad2SChristoph Hellwig 		_dm_request(q, bio);
1691cec47e3dSKiyoshi Ueda }
1692cec47e3dSKiyoshi Ueda 
1693cec47e3dSKiyoshi Ueda void dm_dispatch_request(struct request *rq)
1694cec47e3dSKiyoshi Ueda {
1695cec47e3dSKiyoshi Ueda 	int r;
1696cec47e3dSKiyoshi Ueda 
1697cec47e3dSKiyoshi Ueda 	if (blk_queue_io_stat(rq->q))
1698cec47e3dSKiyoshi Ueda 		rq->cmd_flags |= REQ_IO_STAT;
1699cec47e3dSKiyoshi Ueda 
1700cec47e3dSKiyoshi Ueda 	rq->start_time = jiffies;
1701cec47e3dSKiyoshi Ueda 	r = blk_insert_cloned_request(rq->q, rq);
1702cec47e3dSKiyoshi Ueda 	if (r)
1703cec47e3dSKiyoshi Ueda 		dm_complete_request(rq, r);
1704cec47e3dSKiyoshi Ueda }
1705cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_dispatch_request);
1706cec47e3dSKiyoshi Ueda 
1707cec47e3dSKiyoshi Ueda static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1708cec47e3dSKiyoshi Ueda 				 void *data)
1709cec47e3dSKiyoshi Ueda {
1710cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = data;
171194818742SKent Overstreet 	struct dm_rq_clone_bio_info *info =
171294818742SKent Overstreet 		container_of(bio, struct dm_rq_clone_bio_info, clone);
1713cec47e3dSKiyoshi Ueda 
1714cec47e3dSKiyoshi Ueda 	info->orig = bio_orig;
1715cec47e3dSKiyoshi Ueda 	info->tio = tio;
1716cec47e3dSKiyoshi Ueda 	bio->bi_end_io = end_clone_bio;
1717cec47e3dSKiyoshi Ueda 
1718cec47e3dSKiyoshi Ueda 	return 0;
1719cec47e3dSKiyoshi Ueda }
1720cec47e3dSKiyoshi Ueda 
1721cec47e3dSKiyoshi Ueda static int setup_clone(struct request *clone, struct request *rq,
1722cec47e3dSKiyoshi Ueda 		       struct dm_rq_target_io *tio)
1723cec47e3dSKiyoshi Ueda {
1724d0bcb878SKiyoshi Ueda 	int r;
1725cec47e3dSKiyoshi Ueda 
1726d0bcb878SKiyoshi Ueda 	r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1727d0bcb878SKiyoshi Ueda 			      dm_rq_bio_constructor, tio);
1728cec47e3dSKiyoshi Ueda 	if (r)
1729cec47e3dSKiyoshi Ueda 		return r;
1730cec47e3dSKiyoshi Ueda 
1731cec47e3dSKiyoshi Ueda 	clone->cmd = rq->cmd;
1732cec47e3dSKiyoshi Ueda 	clone->cmd_len = rq->cmd_len;
1733cec47e3dSKiyoshi Ueda 	clone->sense = rq->sense;
1734cec47e3dSKiyoshi Ueda 	clone->end_io = end_clone_request;
1735cec47e3dSKiyoshi Ueda 	clone->end_io_data = tio;
1736cec47e3dSKiyoshi Ueda 
1737cec47e3dSKiyoshi Ueda 	return 0;
1738cec47e3dSKiyoshi Ueda }
1739cec47e3dSKiyoshi Ueda 
17406facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md,
17416facdaffSKiyoshi Ueda 				gfp_t gfp_mask)
17426facdaffSKiyoshi Ueda {
17436facdaffSKiyoshi Ueda 	struct request *clone;
17446facdaffSKiyoshi Ueda 	struct dm_rq_target_io *tio;
17456facdaffSKiyoshi Ueda 
17466facdaffSKiyoshi Ueda 	tio = alloc_rq_tio(md, gfp_mask);
17476facdaffSKiyoshi Ueda 	if (!tio)
17486facdaffSKiyoshi Ueda 		return NULL;
17496facdaffSKiyoshi Ueda 
17506facdaffSKiyoshi Ueda 	tio->md = md;
17516facdaffSKiyoshi Ueda 	tio->ti = NULL;
17526facdaffSKiyoshi Ueda 	tio->orig = rq;
17536facdaffSKiyoshi Ueda 	tio->error = 0;
17546facdaffSKiyoshi Ueda 	memset(&tio->info, 0, sizeof(tio->info));
17556facdaffSKiyoshi Ueda 
17566facdaffSKiyoshi Ueda 	clone = &tio->clone;
17576facdaffSKiyoshi Ueda 	if (setup_clone(clone, rq, tio)) {
17586facdaffSKiyoshi Ueda 		/* -ENOMEM */
17596facdaffSKiyoshi Ueda 		free_rq_tio(tio);
17606facdaffSKiyoshi Ueda 		return NULL;
17616facdaffSKiyoshi Ueda 	}
17626facdaffSKiyoshi Ueda 
17636facdaffSKiyoshi Ueda 	return clone;
17646facdaffSKiyoshi Ueda }
17656facdaffSKiyoshi Ueda 
1766cec47e3dSKiyoshi Ueda /*
1767cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1768cec47e3dSKiyoshi Ueda  */
1769cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq)
1770cec47e3dSKiyoshi Ueda {
1771cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1772cec47e3dSKiyoshi Ueda 	struct request *clone;
1773cec47e3dSKiyoshi Ueda 
1774cec47e3dSKiyoshi Ueda 	if (unlikely(rq->special)) {
1775cec47e3dSKiyoshi Ueda 		DMWARN("Already has something in rq->special.");
1776cec47e3dSKiyoshi Ueda 		return BLKPREP_KILL;
1777cec47e3dSKiyoshi Ueda 	}
1778cec47e3dSKiyoshi Ueda 
17796facdaffSKiyoshi Ueda 	clone = clone_rq(rq, md, GFP_ATOMIC);
17806facdaffSKiyoshi Ueda 	if (!clone)
1781cec47e3dSKiyoshi Ueda 		return BLKPREP_DEFER;
1782cec47e3dSKiyoshi Ueda 
1783cec47e3dSKiyoshi Ueda 	rq->special = clone;
1784cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_DONTPREP;
1785cec47e3dSKiyoshi Ueda 
1786cec47e3dSKiyoshi Ueda 	return BLKPREP_OK;
1787cec47e3dSKiyoshi Ueda }
1788cec47e3dSKiyoshi Ueda 
17899eef87daSKiyoshi Ueda /*
17909eef87daSKiyoshi Ueda  * Returns:
17919eef87daSKiyoshi Ueda  * 0  : the request has been processed (not requeued)
17929eef87daSKiyoshi Ueda  * !0 : the request has been requeued
17939eef87daSKiyoshi Ueda  */
17949eef87daSKiyoshi Ueda static int map_request(struct dm_target *ti, struct request *clone,
1795cec47e3dSKiyoshi Ueda 		       struct mapped_device *md)
1796cec47e3dSKiyoshi Ueda {
17979eef87daSKiyoshi Ueda 	int r, requeued = 0;
1798cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1799cec47e3dSKiyoshi Ueda 
1800cec47e3dSKiyoshi Ueda 	tio->ti = ti;
1801cec47e3dSKiyoshi Ueda 	r = ti->type->map_rq(ti, clone, &tio->info);
1802cec47e3dSKiyoshi Ueda 	switch (r) {
1803cec47e3dSKiyoshi Ueda 	case DM_MAPIO_SUBMITTED:
1804cec47e3dSKiyoshi Ueda 		/* The target has taken the I/O to submit by itself later */
1805cec47e3dSKiyoshi Ueda 		break;
1806cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REMAPPED:
1807cec47e3dSKiyoshi Ueda 		/* The target has remapped the I/O so dispatch it */
18086db4ccd6SJun'ichi Nomura 		trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
18096db4ccd6SJun'ichi Nomura 				     blk_rq_pos(tio->orig));
1810cec47e3dSKiyoshi Ueda 		dm_dispatch_request(clone);
1811cec47e3dSKiyoshi Ueda 		break;
1812cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REQUEUE:
1813cec47e3dSKiyoshi Ueda 		/* The target wants to requeue the I/O */
1814cec47e3dSKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
18159eef87daSKiyoshi Ueda 		requeued = 1;
1816cec47e3dSKiyoshi Ueda 		break;
1817cec47e3dSKiyoshi Ueda 	default:
1818cec47e3dSKiyoshi Ueda 		if (r > 0) {
1819cec47e3dSKiyoshi Ueda 			DMWARN("unimplemented target map return value: %d", r);
1820cec47e3dSKiyoshi Ueda 			BUG();
1821cec47e3dSKiyoshi Ueda 		}
1822cec47e3dSKiyoshi Ueda 
1823cec47e3dSKiyoshi Ueda 		/* The target wants to complete the I/O */
1824cec47e3dSKiyoshi Ueda 		dm_kill_unmapped_request(clone, r);
1825cec47e3dSKiyoshi Ueda 		break;
1826cec47e3dSKiyoshi Ueda 	}
18279eef87daSKiyoshi Ueda 
18289eef87daSKiyoshi Ueda 	return requeued;
1829cec47e3dSKiyoshi Ueda }
1830cec47e3dSKiyoshi Ueda 
1831ba1cbad9SMike Snitzer static struct request *dm_start_request(struct mapped_device *md, struct request *orig)
1832ba1cbad9SMike Snitzer {
1833ba1cbad9SMike Snitzer 	struct request *clone;
1834ba1cbad9SMike Snitzer 
1835ba1cbad9SMike Snitzer 	blk_start_request(orig);
1836ba1cbad9SMike Snitzer 	clone = orig->special;
1837ba1cbad9SMike Snitzer 	atomic_inc(&md->pending[rq_data_dir(clone)]);
1838ba1cbad9SMike Snitzer 
1839ba1cbad9SMike Snitzer 	/*
1840ba1cbad9SMike Snitzer 	 * Hold the md reference here for the in-flight I/O.
1841ba1cbad9SMike Snitzer 	 * We can't rely on the reference count by device opener,
1842ba1cbad9SMike Snitzer 	 * because the device may be closed during the request completion
1843ba1cbad9SMike Snitzer 	 * when all bios are completed.
1844ba1cbad9SMike Snitzer 	 * See the comment in rq_completed() too.
1845ba1cbad9SMike Snitzer 	 */
1846ba1cbad9SMike Snitzer 	dm_get(md);
1847ba1cbad9SMike Snitzer 
1848ba1cbad9SMike Snitzer 	return clone;
1849ba1cbad9SMike Snitzer }
1850ba1cbad9SMike Snitzer 
1851cec47e3dSKiyoshi Ueda /*
1852cec47e3dSKiyoshi Ueda  * q->request_fn for request-based dm.
1853cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1854cec47e3dSKiyoshi Ueda  */
1855cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q)
1856cec47e3dSKiyoshi Ueda {
1857cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
185883d5e5b0SMikulas Patocka 	int srcu_idx;
185983d5e5b0SMikulas Patocka 	struct dm_table *map = dm_get_live_table(md, &srcu_idx);
1860cec47e3dSKiyoshi Ueda 	struct dm_target *ti;
1861b4324feeSKiyoshi Ueda 	struct request *rq, *clone;
186229e4013dSTejun Heo 	sector_t pos;
1863cec47e3dSKiyoshi Ueda 
1864cec47e3dSKiyoshi Ueda 	/*
1865b4324feeSKiyoshi Ueda 	 * For suspend, check blk_queue_stopped() and increment
1866b4324feeSKiyoshi Ueda 	 * ->pending within a single queue_lock not to increment the
1867b4324feeSKiyoshi Ueda 	 * number of in-flight I/Os after the queue is stopped in
1868b4324feeSKiyoshi Ueda 	 * dm_suspend().
1869cec47e3dSKiyoshi Ueda 	 */
18707eaceaccSJens Axboe 	while (!blk_queue_stopped(q)) {
1871cec47e3dSKiyoshi Ueda 		rq = blk_peek_request(q);
1872cec47e3dSKiyoshi Ueda 		if (!rq)
18737eaceaccSJens Axboe 			goto delay_and_out;
1874cec47e3dSKiyoshi Ueda 
187529e4013dSTejun Heo 		/* always use block 0 to find the target for flushes for now */
187629e4013dSTejun Heo 		pos = 0;
187729e4013dSTejun Heo 		if (!(rq->cmd_flags & REQ_FLUSH))
187829e4013dSTejun Heo 			pos = blk_rq_pos(rq);
1879d0bcb878SKiyoshi Ueda 
188029e4013dSTejun Heo 		ti = dm_table_find_target(map, pos);
1881ba1cbad9SMike Snitzer 		if (!dm_target_is_valid(ti)) {
1882ba1cbad9SMike Snitzer 			/*
1883ba1cbad9SMike Snitzer 			 * Must perform setup, that dm_done() requires,
1884ba1cbad9SMike Snitzer 			 * before calling dm_kill_unmapped_request
1885ba1cbad9SMike Snitzer 			 */
1886ba1cbad9SMike Snitzer 			DMERR_LIMIT("request attempted access beyond the end of device");
1887ba1cbad9SMike Snitzer 			clone = dm_start_request(md, rq);
1888ba1cbad9SMike Snitzer 			dm_kill_unmapped_request(clone, -EIO);
1889ba1cbad9SMike Snitzer 			continue;
1890ba1cbad9SMike Snitzer 		}
189129e4013dSTejun Heo 
1892cec47e3dSKiyoshi Ueda 		if (ti->type->busy && ti->type->busy(ti))
18937eaceaccSJens Axboe 			goto delay_and_out;
1894cec47e3dSKiyoshi Ueda 
1895ba1cbad9SMike Snitzer 		clone = dm_start_request(md, rq);
1896b4324feeSKiyoshi Ueda 
1897cec47e3dSKiyoshi Ueda 		spin_unlock(q->queue_lock);
18989eef87daSKiyoshi Ueda 		if (map_request(ti, clone, md))
18999eef87daSKiyoshi Ueda 			goto requeued;
19009eef87daSKiyoshi Ueda 
1901052189a2SKiyoshi Ueda 		BUG_ON(!irqs_disabled());
1902052189a2SKiyoshi Ueda 		spin_lock(q->queue_lock);
1903cec47e3dSKiyoshi Ueda 	}
1904cec47e3dSKiyoshi Ueda 
1905cec47e3dSKiyoshi Ueda 	goto out;
1906cec47e3dSKiyoshi Ueda 
19079eef87daSKiyoshi Ueda requeued:
1908052189a2SKiyoshi Ueda 	BUG_ON(!irqs_disabled());
1909052189a2SKiyoshi Ueda 	spin_lock(q->queue_lock);
19109eef87daSKiyoshi Ueda 
19117eaceaccSJens Axboe delay_and_out:
19127eaceaccSJens Axboe 	blk_delay_queue(q, HZ / 10);
1913cec47e3dSKiyoshi Ueda out:
191483d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
1915cec47e3dSKiyoshi Ueda }
1916cec47e3dSKiyoshi Ueda 
1917cec47e3dSKiyoshi Ueda int dm_underlying_device_busy(struct request_queue *q)
1918cec47e3dSKiyoshi Ueda {
1919cec47e3dSKiyoshi Ueda 	return blk_lld_busy(q);
1920cec47e3dSKiyoshi Ueda }
1921cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1922cec47e3dSKiyoshi Ueda 
1923cec47e3dSKiyoshi Ueda static int dm_lld_busy(struct request_queue *q)
1924cec47e3dSKiyoshi Ueda {
1925cec47e3dSKiyoshi Ueda 	int r;
1926cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
192783d5e5b0SMikulas Patocka 	struct dm_table *map = dm_get_live_table_fast(md);
1928cec47e3dSKiyoshi Ueda 
1929cec47e3dSKiyoshi Ueda 	if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1930cec47e3dSKiyoshi Ueda 		r = 1;
1931cec47e3dSKiyoshi Ueda 	else
1932cec47e3dSKiyoshi Ueda 		r = dm_table_any_busy_target(map);
1933cec47e3dSKiyoshi Ueda 
193483d5e5b0SMikulas Patocka 	dm_put_live_table_fast(md);
1935cec47e3dSKiyoshi Ueda 
1936cec47e3dSKiyoshi Ueda 	return r;
1937cec47e3dSKiyoshi Ueda }
1938cec47e3dSKiyoshi Ueda 
19391da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
19401da177e4SLinus Torvalds {
19418a57dfc6SChandra Seetharaman 	int r = bdi_bits;
19428a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
19438a57dfc6SChandra Seetharaman 	struct dm_table *map;
19441da177e4SLinus Torvalds 
19451eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
194683d5e5b0SMikulas Patocka 		map = dm_get_live_table_fast(md);
19478a57dfc6SChandra Seetharaman 		if (map) {
1948cec47e3dSKiyoshi Ueda 			/*
1949cec47e3dSKiyoshi Ueda 			 * Request-based dm cares about only own queue for
1950cec47e3dSKiyoshi Ueda 			 * the query about congestion status of request_queue
1951cec47e3dSKiyoshi Ueda 			 */
1952cec47e3dSKiyoshi Ueda 			if (dm_request_based(md))
1953cec47e3dSKiyoshi Ueda 				r = md->queue->backing_dev_info.state &
1954cec47e3dSKiyoshi Ueda 				    bdi_bits;
1955cec47e3dSKiyoshi Ueda 			else
19561da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
19578a57dfc6SChandra Seetharaman 		}
195883d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
19598a57dfc6SChandra Seetharaman 	}
19608a57dfc6SChandra Seetharaman 
19611da177e4SLinus Torvalds 	return r;
19621da177e4SLinus Torvalds }
19631da177e4SLinus Torvalds 
19641da177e4SLinus Torvalds /*-----------------------------------------------------------------
19651da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
19661da177e4SLinus Torvalds  *---------------------------------------------------------------*/
19672b06cfffSAlasdair G Kergon static void free_minor(int minor)
19681da177e4SLinus Torvalds {
1969f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
19701da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1971f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
19721da177e4SLinus Torvalds }
19731da177e4SLinus Torvalds 
19741da177e4SLinus Torvalds /*
19751da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
19761da177e4SLinus Torvalds  */
1977cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
19781da177e4SLinus Torvalds {
1979c9d76be6STejun Heo 	int r;
19801da177e4SLinus Torvalds 
19811da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
19821da177e4SLinus Torvalds 		return -EINVAL;
19831da177e4SLinus Torvalds 
1984c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1985f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
19861da177e4SLinus Torvalds 
1987c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
19881da177e4SLinus Torvalds 
1989f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1990c9d76be6STejun Heo 	idr_preload_end();
1991c9d76be6STejun Heo 	if (r < 0)
1992c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
1993c9d76be6STejun Heo 	return 0;
19941da177e4SLinus Torvalds }
19951da177e4SLinus Torvalds 
1996cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
19971da177e4SLinus Torvalds {
1998c9d76be6STejun Heo 	int r;
19991da177e4SLinus Torvalds 
2000c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
2001f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
20021da177e4SLinus Torvalds 
2003c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
20041da177e4SLinus Torvalds 
2005f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2006c9d76be6STejun Heo 	idr_preload_end();
2007c9d76be6STejun Heo 	if (r < 0)
20081da177e4SLinus Torvalds 		return r;
2009c9d76be6STejun Heo 	*minor = r;
2010c9d76be6STejun Heo 	return 0;
20111da177e4SLinus Torvalds }
20121da177e4SLinus Torvalds 
201383d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
20141da177e4SLinus Torvalds 
201553d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
201653d5914fSMikulas Patocka 
20174a0b4ddfSMike Snitzer static void dm_init_md_queue(struct mapped_device *md)
20184a0b4ddfSMike Snitzer {
20194a0b4ddfSMike Snitzer 	/*
20204a0b4ddfSMike Snitzer 	 * Request-based dm devices cannot be stacked on top of bio-based dm
20214a0b4ddfSMike Snitzer 	 * devices.  The type of this dm device has not been decided yet.
20224a0b4ddfSMike Snitzer 	 * The type is decided at the first table loading time.
20234a0b4ddfSMike Snitzer 	 * To prevent problematic device stacking, clear the queue flag
20244a0b4ddfSMike Snitzer 	 * for request stacking support until then.
20254a0b4ddfSMike Snitzer 	 *
20264a0b4ddfSMike Snitzer 	 * This queue is new, so no concurrency on the queue_flags.
20274a0b4ddfSMike Snitzer 	 */
20284a0b4ddfSMike Snitzer 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
20294a0b4ddfSMike Snitzer 
20304a0b4ddfSMike Snitzer 	md->queue->queuedata = md;
20314a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
20324a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_data = md;
20334a0b4ddfSMike Snitzer 	blk_queue_make_request(md->queue, dm_request);
20344a0b4ddfSMike Snitzer 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
20354a0b4ddfSMike Snitzer 	blk_queue_merge_bvec(md->queue, dm_merge_bvec);
20364a0b4ddfSMike Snitzer }
20374a0b4ddfSMike Snitzer 
20381da177e4SLinus Torvalds /*
20391da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
20401da177e4SLinus Torvalds  */
20412b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
20421da177e4SLinus Torvalds {
20431da177e4SLinus Torvalds 	int r;
2044cf13ab8eSFrederik Deweerdt 	struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
2045ba61fdd1SJeff Mahoney 	void *old_md;
20461da177e4SLinus Torvalds 
20471da177e4SLinus Torvalds 	if (!md) {
20481da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
20491da177e4SLinus Torvalds 		return NULL;
20501da177e4SLinus Torvalds 	}
20511da177e4SLinus Torvalds 
205210da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
20536ed7ade8SMilan Broz 		goto bad_module_get;
205410da4f79SJeff Mahoney 
20551da177e4SLinus Torvalds 	/* get a minor number for the dev */
20562b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
2057cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
20582b06cfffSAlasdair G Kergon 	else
2059cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
20601da177e4SLinus Torvalds 	if (r < 0)
20616ed7ade8SMilan Broz 		goto bad_minor;
20621da177e4SLinus Torvalds 
206383d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
206483d5e5b0SMikulas Patocka 	if (r < 0)
206583d5e5b0SMikulas Patocka 		goto bad_io_barrier;
206683d5e5b0SMikulas Patocka 
2067a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
2068e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
2069a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
2070*86f1152bSBenjamin Marzinski 	mutex_init(&md->table_devices_lock);
2071022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
20721da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
20735c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
20741da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
20757a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
20767a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
2077*86f1152bSBenjamin Marzinski 	INIT_LIST_HEAD(&md->table_devices);
20787a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
20791da177e4SLinus Torvalds 
20804a0b4ddfSMike Snitzer 	md->queue = blk_alloc_queue(GFP_KERNEL);
20811da177e4SLinus Torvalds 	if (!md->queue)
20826ed7ade8SMilan Broz 		goto bad_queue;
20831da177e4SLinus Torvalds 
20844a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
20859faf400fSStefan Bader 
20861da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
20871da177e4SLinus Torvalds 	if (!md->disk)
20886ed7ade8SMilan Broz 		goto bad_disk;
20891da177e4SLinus Torvalds 
2090316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
2091316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
2092f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
209353d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
2094f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
20952995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
2096f0b04115SJeff Mahoney 
20971da177e4SLinus Torvalds 	md->disk->major = _major;
20981da177e4SLinus Torvalds 	md->disk->first_minor = minor;
20991da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
21001da177e4SLinus Torvalds 	md->disk->queue = md->queue;
21011da177e4SLinus Torvalds 	md->disk->private_data = md;
21021da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
21031da177e4SLinus Torvalds 	add_disk(md->disk);
21047e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
21051da177e4SLinus Torvalds 
2106670368a8STejun Heo 	md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
2107304f3f6aSMilan Broz 	if (!md->wq)
2108304f3f6aSMilan Broz 		goto bad_thread;
2109304f3f6aSMilan Broz 
211032a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
211132a926daSMikulas Patocka 	if (!md->bdev)
211232a926daSMikulas Patocka 		goto bad_bdev;
211332a926daSMikulas Patocka 
21146a8736d1STejun Heo 	bio_init(&md->flush_bio);
21156a8736d1STejun Heo 	md->flush_bio.bi_bdev = md->bdev;
21166a8736d1STejun Heo 	md->flush_bio.bi_rw = WRITE_FLUSH;
21176a8736d1STejun Heo 
2118fd2ed4d2SMikulas Patocka 	dm_stats_init(&md->stats);
2119fd2ed4d2SMikulas Patocka 
2120ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
2121f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
2122ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
2123f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2124ba61fdd1SJeff Mahoney 
2125ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
2126ba61fdd1SJeff Mahoney 
21271da177e4SLinus Torvalds 	return md;
21281da177e4SLinus Torvalds 
212932a926daSMikulas Patocka bad_bdev:
213032a926daSMikulas Patocka 	destroy_workqueue(md->wq);
2131304f3f6aSMilan Broz bad_thread:
213203022c54SZdenek Kabelac 	del_gendisk(md->disk);
2133304f3f6aSMilan Broz 	put_disk(md->disk);
21346ed7ade8SMilan Broz bad_disk:
21351312f40eSAl Viro 	blk_cleanup_queue(md->queue);
21366ed7ade8SMilan Broz bad_queue:
213783d5e5b0SMikulas Patocka 	cleanup_srcu_struct(&md->io_barrier);
213883d5e5b0SMikulas Patocka bad_io_barrier:
21391da177e4SLinus Torvalds 	free_minor(minor);
21406ed7ade8SMilan Broz bad_minor:
214110da4f79SJeff Mahoney 	module_put(THIS_MODULE);
21426ed7ade8SMilan Broz bad_module_get:
21431da177e4SLinus Torvalds 	kfree(md);
21441da177e4SLinus Torvalds 	return NULL;
21451da177e4SLinus Torvalds }
21461da177e4SLinus Torvalds 
2147ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
2148ae9da83fSJun'ichi Nomura 
21491da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
21501da177e4SLinus Torvalds {
2151f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
215263d94e48SJun'ichi Nomura 
2153ae9da83fSJun'ichi Nomura 	unlock_fs(md);
2154db8fef4fSMikulas Patocka 	bdput(md->bdev);
2155304f3f6aSMilan Broz 	destroy_workqueue(md->wq);
2156e6ee8c0bSKiyoshi Ueda 	if (md->io_pool)
21571da177e4SLinus Torvalds 		mempool_destroy(md->io_pool);
2158e6ee8c0bSKiyoshi Ueda 	if (md->bs)
21599faf400fSStefan Bader 		bioset_free(md->bs);
21609c47008dSMartin K. Petersen 	blk_integrity_unregister(md->disk);
21611da177e4SLinus Torvalds 	del_gendisk(md->disk);
216283d5e5b0SMikulas Patocka 	cleanup_srcu_struct(&md->io_barrier);
2163*86f1152bSBenjamin Marzinski 	free_table_devices(&md->table_devices);
216463d94e48SJun'ichi Nomura 	free_minor(minor);
2165fba9f90eSJeff Mahoney 
2166fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
2167fba9f90eSJeff Mahoney 	md->disk->private_data = NULL;
2168fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
2169fba9f90eSJeff Mahoney 
21701da177e4SLinus Torvalds 	put_disk(md->disk);
21711312f40eSAl Viro 	blk_cleanup_queue(md->queue);
2172fd2ed4d2SMikulas Patocka 	dm_stats_cleanup(&md->stats);
217310da4f79SJeff Mahoney 	module_put(THIS_MODULE);
21741da177e4SLinus Torvalds 	kfree(md);
21751da177e4SLinus Torvalds }
21761da177e4SLinus Torvalds 
2177e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2178e6ee8c0bSKiyoshi Ueda {
2179c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
2180e6ee8c0bSKiyoshi Ueda 
21815f015204SJun'ichi Nomura 	if (md->io_pool && md->bs) {
218216245bdcSJun'ichi Nomura 		/* The md already has necessary mempools. */
218316245bdcSJun'ichi Nomura 		if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
2184c0820cf5SMikulas Patocka 			/*
218516245bdcSJun'ichi Nomura 			 * Reload bioset because front_pad may have changed
218616245bdcSJun'ichi Nomura 			 * because a different table was loaded.
2187c0820cf5SMikulas Patocka 			 */
2188c0820cf5SMikulas Patocka 			bioset_free(md->bs);
2189c0820cf5SMikulas Patocka 			md->bs = p->bs;
2190c0820cf5SMikulas Patocka 			p->bs = NULL;
219116245bdcSJun'ichi Nomura 		} else if (dm_table_get_type(t) == DM_TYPE_REQUEST_BASED) {
219216245bdcSJun'ichi Nomura 			/*
219316245bdcSJun'ichi Nomura 			 * There's no need to reload with request-based dm
219416245bdcSJun'ichi Nomura 			 * because the size of front_pad doesn't change.
219516245bdcSJun'ichi Nomura 			 * Note for future: If you are to reload bioset,
219616245bdcSJun'ichi Nomura 			 * prep-ed requests in the queue may refer
219716245bdcSJun'ichi Nomura 			 * to bio from the old bioset, so you must walk
219816245bdcSJun'ichi Nomura 			 * through the queue to unprep.
219916245bdcSJun'ichi Nomura 			 */
220016245bdcSJun'ichi Nomura 		}
2201e6ee8c0bSKiyoshi Ueda 		goto out;
2202c0820cf5SMikulas Patocka 	}
2203e6ee8c0bSKiyoshi Ueda 
22045f015204SJun'ichi Nomura 	BUG_ON(!p || md->io_pool || md->bs);
2205e6ee8c0bSKiyoshi Ueda 
2206e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
2207e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
2208e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
2209e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
2210e6ee8c0bSKiyoshi Ueda 
2211e6ee8c0bSKiyoshi Ueda out:
2212e6ee8c0bSKiyoshi Ueda 	/* mempool bind completed, now no need any mempools in the table */
2213e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
2214e6ee8c0bSKiyoshi Ueda }
2215e6ee8c0bSKiyoshi Ueda 
22161da177e4SLinus Torvalds /*
22171da177e4SLinus Torvalds  * Bind a table to the device.
22181da177e4SLinus Torvalds  */
22191da177e4SLinus Torvalds static void event_callback(void *context)
22201da177e4SLinus Torvalds {
22217a8c3d3bSMike Anderson 	unsigned long flags;
22227a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
22231da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
22241da177e4SLinus Torvalds 
22257a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
22267a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
22277a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
22287a8c3d3bSMike Anderson 
2229ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
22307a8c3d3bSMike Anderson 
22311da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
22321da177e4SLinus Torvalds 	wake_up(&md->eventq);
22331da177e4SLinus Torvalds }
22341da177e4SLinus Torvalds 
2235c217649bSMike Snitzer /*
2236c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
2237c217649bSMike Snitzer  */
22384e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
22391da177e4SLinus Torvalds {
22404e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
22411da177e4SLinus Torvalds 
2242db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
22431da177e4SLinus Torvalds }
22441da177e4SLinus Torvalds 
2245042d2a9bSAlasdair G Kergon /*
2246d5b9dd04SMikulas Patocka  * Return 1 if the queue has a compulsory merge_bvec_fn function.
2247d5b9dd04SMikulas Patocka  *
2248d5b9dd04SMikulas Patocka  * If this function returns 0, then the device is either a non-dm
2249d5b9dd04SMikulas Patocka  * device without a merge_bvec_fn, or it is a dm device that is
2250d5b9dd04SMikulas Patocka  * able to split any bios it receives that are too big.
2251d5b9dd04SMikulas Patocka  */
2252d5b9dd04SMikulas Patocka int dm_queue_merge_is_compulsory(struct request_queue *q)
2253d5b9dd04SMikulas Patocka {
2254d5b9dd04SMikulas Patocka 	struct mapped_device *dev_md;
2255d5b9dd04SMikulas Patocka 
2256d5b9dd04SMikulas Patocka 	if (!q->merge_bvec_fn)
2257d5b9dd04SMikulas Patocka 		return 0;
2258d5b9dd04SMikulas Patocka 
2259d5b9dd04SMikulas Patocka 	if (q->make_request_fn == dm_request) {
2260d5b9dd04SMikulas Patocka 		dev_md = q->queuedata;
2261d5b9dd04SMikulas Patocka 		if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2262d5b9dd04SMikulas Patocka 			return 0;
2263d5b9dd04SMikulas Patocka 	}
2264d5b9dd04SMikulas Patocka 
2265d5b9dd04SMikulas Patocka 	return 1;
2266d5b9dd04SMikulas Patocka }
2267d5b9dd04SMikulas Patocka 
2268d5b9dd04SMikulas Patocka static int dm_device_merge_is_compulsory(struct dm_target *ti,
2269d5b9dd04SMikulas Patocka 					 struct dm_dev *dev, sector_t start,
2270d5b9dd04SMikulas Patocka 					 sector_t len, void *data)
2271d5b9dd04SMikulas Patocka {
2272d5b9dd04SMikulas Patocka 	struct block_device *bdev = dev->bdev;
2273d5b9dd04SMikulas Patocka 	struct request_queue *q = bdev_get_queue(bdev);
2274d5b9dd04SMikulas Patocka 
2275d5b9dd04SMikulas Patocka 	return dm_queue_merge_is_compulsory(q);
2276d5b9dd04SMikulas Patocka }
2277d5b9dd04SMikulas Patocka 
2278d5b9dd04SMikulas Patocka /*
2279d5b9dd04SMikulas Patocka  * Return 1 if it is acceptable to ignore merge_bvec_fn based
2280d5b9dd04SMikulas Patocka  * on the properties of the underlying devices.
2281d5b9dd04SMikulas Patocka  */
2282d5b9dd04SMikulas Patocka static int dm_table_merge_is_optional(struct dm_table *table)
2283d5b9dd04SMikulas Patocka {
2284d5b9dd04SMikulas Patocka 	unsigned i = 0;
2285d5b9dd04SMikulas Patocka 	struct dm_target *ti;
2286d5b9dd04SMikulas Patocka 
2287d5b9dd04SMikulas Patocka 	while (i < dm_table_get_num_targets(table)) {
2288d5b9dd04SMikulas Patocka 		ti = dm_table_get_target(table, i++);
2289d5b9dd04SMikulas Patocka 
2290d5b9dd04SMikulas Patocka 		if (ti->type->iterate_devices &&
2291d5b9dd04SMikulas Patocka 		    ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2292d5b9dd04SMikulas Patocka 			return 0;
2293d5b9dd04SMikulas Patocka 	}
2294d5b9dd04SMikulas Patocka 
2295d5b9dd04SMikulas Patocka 	return 1;
2296d5b9dd04SMikulas Patocka }
2297d5b9dd04SMikulas Patocka 
2298d5b9dd04SMikulas Patocka /*
2299042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2300042d2a9bSAlasdair G Kergon  */
2301042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2302754c5fc7SMike Snitzer 			       struct queue_limits *limits)
23031da177e4SLinus Torvalds {
2304042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
2305165125e1SJens Axboe 	struct request_queue *q = md->queue;
23061da177e4SLinus Torvalds 	sector_t size;
2307d5b9dd04SMikulas Patocka 	int merge_is_optional;
23081da177e4SLinus Torvalds 
23091da177e4SLinus Torvalds 	size = dm_table_get_size(t);
23103ac51e74SDarrick J. Wong 
23113ac51e74SDarrick J. Wong 	/*
23123ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
23133ac51e74SDarrick J. Wong 	 */
2314fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
23153ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
23163ac51e74SDarrick J. Wong 
23174e90188bSAlasdair G Kergon 	__set_size(md, size);
23181da177e4SLinus Torvalds 
2319cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
23202ca3310eSAlasdair G Kergon 
2321e6ee8c0bSKiyoshi Ueda 	/*
2322e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
2323e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
2324e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
2325e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
2326e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
2327e6ee8c0bSKiyoshi Ueda 	 */
2328e6ee8c0bSKiyoshi Ueda 	if (dm_table_request_based(t) && !blk_queue_stopped(q))
2329e6ee8c0bSKiyoshi Ueda 		stop_queue(q);
2330e6ee8c0bSKiyoshi Ueda 
2331e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
2332e6ee8c0bSKiyoshi Ueda 
2333d5b9dd04SMikulas Patocka 	merge_is_optional = dm_table_merge_is_optional(t);
2334d5b9dd04SMikulas Patocka 
2335042d2a9bSAlasdair G Kergon 	old_map = md->map;
233683d5e5b0SMikulas Patocka 	rcu_assign_pointer(md->map, t);
233736a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
233836a0456fSAlasdair G Kergon 
2339754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
2340d5b9dd04SMikulas Patocka 	if (merge_is_optional)
2341d5b9dd04SMikulas Patocka 		set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2342d5b9dd04SMikulas Patocka 	else
2343d5b9dd04SMikulas Patocka 		clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
234483d5e5b0SMikulas Patocka 	dm_sync_table(md);
23452ca3310eSAlasdair G Kergon 
2346042d2a9bSAlasdair G Kergon 	return old_map;
23471da177e4SLinus Torvalds }
23481da177e4SLinus Torvalds 
2349a7940155SAlasdair G Kergon /*
2350a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2351a7940155SAlasdair G Kergon  */
2352a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
23531da177e4SLinus Torvalds {
23541da177e4SLinus Torvalds 	struct dm_table *map = md->map;
23551da177e4SLinus Torvalds 
23561da177e4SLinus Torvalds 	if (!map)
2357a7940155SAlasdair G Kergon 		return NULL;
23581da177e4SLinus Torvalds 
23591da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
23609cdb8520SMonam Agarwal 	RCU_INIT_POINTER(md->map, NULL);
236183d5e5b0SMikulas Patocka 	dm_sync_table(md);
2362a7940155SAlasdair G Kergon 
2363a7940155SAlasdair G Kergon 	return map;
23641da177e4SLinus Torvalds }
23651da177e4SLinus Torvalds 
23661da177e4SLinus Torvalds /*
23671da177e4SLinus Torvalds  * Constructor for a new device.
23681da177e4SLinus Torvalds  */
23692b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
23701da177e4SLinus Torvalds {
23711da177e4SLinus Torvalds 	struct mapped_device *md;
23721da177e4SLinus Torvalds 
23732b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
23741da177e4SLinus Torvalds 	if (!md)
23751da177e4SLinus Torvalds 		return -ENXIO;
23761da177e4SLinus Torvalds 
2377784aae73SMilan Broz 	dm_sysfs_init(md);
2378784aae73SMilan Broz 
23791da177e4SLinus Torvalds 	*result = md;
23801da177e4SLinus Torvalds 	return 0;
23811da177e4SLinus Torvalds }
23821da177e4SLinus Torvalds 
2383a5664dadSMike Snitzer /*
2384a5664dadSMike Snitzer  * Functions to manage md->type.
2385a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2386a5664dadSMike Snitzer  */
2387a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2388a5664dadSMike Snitzer {
2389a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2390a5664dadSMike Snitzer }
2391a5664dadSMike Snitzer 
2392a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2393a5664dadSMike Snitzer {
2394a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2395a5664dadSMike Snitzer }
2396a5664dadSMike Snitzer 
2397a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type)
2398a5664dadSMike Snitzer {
239900c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2400a5664dadSMike Snitzer 	md->type = type;
2401a5664dadSMike Snitzer }
2402a5664dadSMike Snitzer 
2403a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md)
2404a5664dadSMike Snitzer {
240500c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2406a5664dadSMike Snitzer 	return md->type;
2407a5664dadSMike Snitzer }
2408a5664dadSMike Snitzer 
240936a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
241036a0456fSAlasdair G Kergon {
241136a0456fSAlasdair G Kergon 	return md->immutable_target_type;
241236a0456fSAlasdair G Kergon }
241336a0456fSAlasdair G Kergon 
24144a0b4ddfSMike Snitzer /*
2415f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
2416f84cb8a4SMike Snitzer  * count on 'md'.
2417f84cb8a4SMike Snitzer  */
2418f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2419f84cb8a4SMike Snitzer {
2420f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
2421f84cb8a4SMike Snitzer 	return &md->queue->limits;
2422f84cb8a4SMike Snitzer }
2423f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2424f84cb8a4SMike Snitzer 
2425f84cb8a4SMike Snitzer /*
24264a0b4ddfSMike Snitzer  * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
24274a0b4ddfSMike Snitzer  */
24284a0b4ddfSMike Snitzer static int dm_init_request_based_queue(struct mapped_device *md)
24294a0b4ddfSMike Snitzer {
24304a0b4ddfSMike Snitzer 	struct request_queue *q = NULL;
24314a0b4ddfSMike Snitzer 
24324a0b4ddfSMike Snitzer 	if (md->queue->elevator)
24334a0b4ddfSMike Snitzer 		return 1;
24344a0b4ddfSMike Snitzer 
24354a0b4ddfSMike Snitzer 	/* Fully initialize the queue */
24364a0b4ddfSMike Snitzer 	q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
24374a0b4ddfSMike Snitzer 	if (!q)
24384a0b4ddfSMike Snitzer 		return 0;
24394a0b4ddfSMike Snitzer 
24404a0b4ddfSMike Snitzer 	md->queue = q;
24414a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
24424a0b4ddfSMike Snitzer 	blk_queue_softirq_done(md->queue, dm_softirq_done);
24434a0b4ddfSMike Snitzer 	blk_queue_prep_rq(md->queue, dm_prep_fn);
24444a0b4ddfSMike Snitzer 	blk_queue_lld_busy(md->queue, dm_lld_busy);
24454a0b4ddfSMike Snitzer 
24464a0b4ddfSMike Snitzer 	elv_register_queue(md->queue);
24474a0b4ddfSMike Snitzer 
24484a0b4ddfSMike Snitzer 	return 1;
24494a0b4ddfSMike Snitzer }
24504a0b4ddfSMike Snitzer 
24514a0b4ddfSMike Snitzer /*
24524a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
24534a0b4ddfSMike Snitzer  */
24544a0b4ddfSMike Snitzer int dm_setup_md_queue(struct mapped_device *md)
24554a0b4ddfSMike Snitzer {
24564a0b4ddfSMike Snitzer 	if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
24574a0b4ddfSMike Snitzer 	    !dm_init_request_based_queue(md)) {
24584a0b4ddfSMike Snitzer 		DMWARN("Cannot initialize queue for request-based mapped device");
24594a0b4ddfSMike Snitzer 		return -EINVAL;
24604a0b4ddfSMike Snitzer 	}
24614a0b4ddfSMike Snitzer 
24624a0b4ddfSMike Snitzer 	return 0;
24634a0b4ddfSMike Snitzer }
24644a0b4ddfSMike Snitzer 
2465637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev)
24661da177e4SLinus Torvalds {
24671da177e4SLinus Torvalds 	struct mapped_device *md;
24681da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
24691da177e4SLinus Torvalds 
24701da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
24711da177e4SLinus Torvalds 		return NULL;
24721da177e4SLinus Torvalds 
2473f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
24741da177e4SLinus Torvalds 
24751da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
2476fba9f90eSJeff Mahoney 	if (md && (md == MINOR_ALLOCED ||
2477f331c029STejun Heo 		   (MINOR(disk_devt(dm_disk(md))) != minor) ||
2478abdc568bSKiyoshi Ueda 		   dm_deleting_md(md) ||
2479fba9f90eSJeff Mahoney 		   test_bit(DMF_FREEING, &md->flags))) {
2480637842cfSDavid Teigland 		md = NULL;
2481fba9f90eSJeff Mahoney 		goto out;
2482fba9f90eSJeff Mahoney 	}
24831da177e4SLinus Torvalds 
2484fba9f90eSJeff Mahoney out:
2485f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
24861da177e4SLinus Torvalds 
2487637842cfSDavid Teigland 	return md;
2488637842cfSDavid Teigland }
2489637842cfSDavid Teigland 
2490d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev)
2491d229a958SDavid Teigland {
2492d229a958SDavid Teigland 	struct mapped_device *md = dm_find_md(dev);
2493d229a958SDavid Teigland 
2494d229a958SDavid Teigland 	if (md)
2495d229a958SDavid Teigland 		dm_get(md);
2496d229a958SDavid Teigland 
2497d229a958SDavid Teigland 	return md;
2498d229a958SDavid Teigland }
24993cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2500d229a958SDavid Teigland 
25019ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2502637842cfSDavid Teigland {
25039ade92a9SAlasdair G Kergon 	return md->interface_ptr;
25041da177e4SLinus Torvalds }
25051da177e4SLinus Torvalds 
25061da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
25071da177e4SLinus Torvalds {
25081da177e4SLinus Torvalds 	md->interface_ptr = ptr;
25091da177e4SLinus Torvalds }
25101da177e4SLinus Torvalds 
25111da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
25121da177e4SLinus Torvalds {
25131da177e4SLinus Torvalds 	atomic_inc(&md->holders);
25143f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
25151da177e4SLinus Torvalds }
25161da177e4SLinus Torvalds 
251772d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
251872d94861SAlasdair G Kergon {
251972d94861SAlasdair G Kergon 	return md->name;
252072d94861SAlasdair G Kergon }
252172d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
252272d94861SAlasdair G Kergon 
25233f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
25241da177e4SLinus Torvalds {
25251134e5aeSMike Anderson 	struct dm_table *map;
252683d5e5b0SMikulas Patocka 	int srcu_idx;
25271da177e4SLinus Torvalds 
25283f77316dSKiyoshi Ueda 	might_sleep();
2529fba9f90eSJeff Mahoney 
25303f77316dSKiyoshi Ueda 	spin_lock(&_minor_lock);
253183d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
25323f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2533fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2534f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
25353f77316dSKiyoshi Ueda 
25364f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
25371da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
25381da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
25391da177e4SLinus Torvalds 	}
25403f77316dSKiyoshi Ueda 
254183d5e5b0SMikulas Patocka 	/* dm_put_live_table must be before msleep, otherwise deadlock is possible */
254283d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
254383d5e5b0SMikulas Patocka 
25443f77316dSKiyoshi Ueda 	/*
25453f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
25463f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
25473f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
25483f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
25493f77316dSKiyoshi Ueda 	 */
25503f77316dSKiyoshi Ueda 	if (wait)
25513f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
25523f77316dSKiyoshi Ueda 			msleep(1);
25533f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
25543f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
25553f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
25563f77316dSKiyoshi Ueda 
2557784aae73SMilan Broz 	dm_sysfs_exit(md);
2558a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
25591da177e4SLinus Torvalds 	free_dev(md);
25601da177e4SLinus Torvalds }
25613f77316dSKiyoshi Ueda 
25623f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
25633f77316dSKiyoshi Ueda {
25643f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
25653f77316dSKiyoshi Ueda }
25663f77316dSKiyoshi Ueda 
25673f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
25683f77316dSKiyoshi Ueda {
25693f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
25703f77316dSKiyoshi Ueda }
25713f77316dSKiyoshi Ueda 
25723f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
25733f77316dSKiyoshi Ueda {
25743f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
25751da177e4SLinus Torvalds }
257679eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
25771da177e4SLinus Torvalds 
2578401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
257946125c1cSMilan Broz {
258046125c1cSMilan Broz 	int r = 0;
2581b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
2582b44ebeb0SMikulas Patocka 
2583b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
258446125c1cSMilan Broz 
258546125c1cSMilan Broz 	while (1) {
2586401600dfSMikulas Patocka 		set_current_state(interruptible);
258746125c1cSMilan Broz 
2588b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
258946125c1cSMilan Broz 			break;
259046125c1cSMilan Broz 
2591401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
2592401600dfSMikulas Patocka 		    signal_pending(current)) {
259346125c1cSMilan Broz 			r = -EINTR;
259446125c1cSMilan Broz 			break;
259546125c1cSMilan Broz 		}
259646125c1cSMilan Broz 
259746125c1cSMilan Broz 		io_schedule();
259846125c1cSMilan Broz 	}
259946125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
260046125c1cSMilan Broz 
2601b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
2602b44ebeb0SMikulas Patocka 
260346125c1cSMilan Broz 	return r;
260446125c1cSMilan Broz }
260546125c1cSMilan Broz 
26061da177e4SLinus Torvalds /*
26071da177e4SLinus Torvalds  * Process the deferred bios
26081da177e4SLinus Torvalds  */
2609ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
26101da177e4SLinus Torvalds {
2611ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2612ef208587SMikulas Patocka 						work);
26136d6f10dfSMilan Broz 	struct bio *c;
261483d5e5b0SMikulas Patocka 	int srcu_idx;
261583d5e5b0SMikulas Patocka 	struct dm_table *map;
26161da177e4SLinus Torvalds 
261783d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
2618ef208587SMikulas Patocka 
26193b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2620022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2621022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2622022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2623022c2611SMikulas Patocka 
26246a8736d1STejun Heo 		if (!c)
2625df12ee99SAlasdair G Kergon 			break;
262673d410c0SMilan Broz 
2627e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2628e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2629af7e466aSMikulas Patocka 		else
263083d5e5b0SMikulas Patocka 			__split_and_process_bio(md, map, c);
2631e6ee8c0bSKiyoshi Ueda 	}
26323b00b203SMikulas Patocka 
263383d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
26341da177e4SLinus Torvalds }
26351da177e4SLinus Torvalds 
26369a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2637304f3f6aSMilan Broz {
26383b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
26394e857c58SPeter Zijlstra 	smp_mb__after_atomic();
264053d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2641304f3f6aSMilan Broz }
2642304f3f6aSMilan Broz 
26431da177e4SLinus Torvalds /*
2644042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
26451da177e4SLinus Torvalds  */
2646042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
26471da177e4SLinus Torvalds {
264887eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2649754c5fc7SMike Snitzer 	struct queue_limits limits;
2650042d2a9bSAlasdair G Kergon 	int r;
26511da177e4SLinus Torvalds 
2652e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
26531da177e4SLinus Torvalds 
26541da177e4SLinus Torvalds 	/* device must be suspended */
26554f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
265693c534aeSAlasdair G Kergon 		goto out;
26571da177e4SLinus Torvalds 
26583ae70656SMike Snitzer 	/*
26593ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
26603ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
26613ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
26623ae70656SMike Snitzer 	 * reappear.
26633ae70656SMike Snitzer 	 */
26643ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
266583d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
26663ae70656SMike Snitzer 		if (live_map)
26673ae70656SMike Snitzer 			limits = md->queue->limits;
266883d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
26693ae70656SMike Snitzer 	}
26703ae70656SMike Snitzer 
267187eb5b21SMike Christie 	if (!live_map) {
2672754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2673042d2a9bSAlasdair G Kergon 		if (r) {
2674042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2675754c5fc7SMike Snitzer 			goto out;
2676042d2a9bSAlasdair G Kergon 		}
267787eb5b21SMike Christie 	}
2678754c5fc7SMike Snitzer 
2679042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
26801da177e4SLinus Torvalds 
268193c534aeSAlasdair G Kergon out:
2682e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2683042d2a9bSAlasdair G Kergon 	return map;
26841da177e4SLinus Torvalds }
26851da177e4SLinus Torvalds 
26861da177e4SLinus Torvalds /*
26871da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
26881da177e4SLinus Torvalds  * device.
26891da177e4SLinus Torvalds  */
26902ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
26911da177e4SLinus Torvalds {
2692e39e2e95SAlasdair G Kergon 	int r;
26931da177e4SLinus Torvalds 
26941da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2695dfbe03f6SAlasdair G Kergon 
2696db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2697dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2698cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2699e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2700e39e2e95SAlasdair G Kergon 		return r;
2701dfbe03f6SAlasdair G Kergon 	}
2702dfbe03f6SAlasdair G Kergon 
2703aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2704aa8d7c2fSAlasdair G Kergon 
27051da177e4SLinus Torvalds 	return 0;
27061da177e4SLinus Torvalds }
27071da177e4SLinus Torvalds 
27082ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
27091da177e4SLinus Torvalds {
2710aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2711aa8d7c2fSAlasdair G Kergon 		return;
2712aa8d7c2fSAlasdair G Kergon 
2713db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
27141da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2715aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
27161da177e4SLinus Torvalds }
27171da177e4SLinus Torvalds 
27181da177e4SLinus Torvalds /*
27191da177e4SLinus Torvalds  * We need to be able to change a mapping table under a mounted
27201da177e4SLinus Torvalds  * filesystem.  For example we might want to move some data in
27211da177e4SLinus Torvalds  * the background.  Before the table can be swapped with
27221da177e4SLinus Torvalds  * dm_bind_table, dm_suspend must be called to flush any in
27231da177e4SLinus Torvalds  * flight bios and ensure that any further io gets deferred.
27241da177e4SLinus Torvalds  */
2725cec47e3dSKiyoshi Ueda /*
2726cec47e3dSKiyoshi Ueda  * Suspend mechanism in request-based dm.
2727cec47e3dSKiyoshi Ueda  *
27289f518b27SKiyoshi Ueda  * 1. Flush all I/Os by lock_fs() if needed.
27299f518b27SKiyoshi Ueda  * 2. Stop dispatching any I/O by stopping the request_queue.
27309f518b27SKiyoshi Ueda  * 3. Wait for all in-flight I/Os to be completed or requeued.
2731cec47e3dSKiyoshi Ueda  *
27329f518b27SKiyoshi Ueda  * To abort suspend, start the request_queue.
2733cec47e3dSKiyoshi Ueda  */
2734a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
27351da177e4SLinus Torvalds {
27362ca3310eSAlasdair G Kergon 	struct dm_table *map = NULL;
273746125c1cSMilan Broz 	int r = 0;
2738a3d77d35SKiyoshi Ueda 	int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
27392e93ccc1SKiyoshi Ueda 	int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
27401da177e4SLinus Torvalds 
2741e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
27422ca3310eSAlasdair G Kergon 
27434f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
274473d410c0SMilan Broz 		r = -EINVAL;
2745d287483dSAlasdair G Kergon 		goto out_unlock;
274673d410c0SMilan Broz 	}
27471da177e4SLinus Torvalds 
274883d5e5b0SMikulas Patocka 	map = md->map;
2749cf222b37SAlasdair G Kergon 
27502e93ccc1SKiyoshi Ueda 	/*
27512e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
27522e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
27532e93ccc1SKiyoshi Ueda 	 */
27542e93ccc1SKiyoshi Ueda 	if (noflush)
27552e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
27562e93ccc1SKiyoshi Ueda 
2757436d4108SAlasdair G Kergon 	/* This does not get reverted if there's an error later. */
27581da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
27591da177e4SLinus Torvalds 
27602e93ccc1SKiyoshi Ueda 	/*
27619f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
27629f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
27639f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
27649f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
27652e93ccc1SKiyoshi Ueda 	 */
276632a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
27672ca3310eSAlasdair G Kergon 		r = lock_fs(md);
27682ca3310eSAlasdair G Kergon 		if (r)
276983d5e5b0SMikulas Patocka 			goto out_unlock;
2770aa8d7c2fSAlasdair G Kergon 	}
27711da177e4SLinus Torvalds 
27721da177e4SLinus Torvalds 	/*
27733b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
27743b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
27753b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
27763b00b203SMikulas Patocka 	 * dm_wq_work.
27773b00b203SMikulas Patocka 	 *
27783b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
27793b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
27806a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
27816a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
27826a8736d1STejun Heo 	 * flush_workqueue(md->wq).
27831da177e4SLinus Torvalds 	 */
27841eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
278583d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
27861da177e4SLinus Torvalds 
2787d0bcb878SKiyoshi Ueda 	/*
278829e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
278929e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2790d0bcb878SKiyoshi Ueda 	 */
2791cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
27929f518b27SKiyoshi Ueda 		stop_queue(md->queue);
2793cec47e3dSKiyoshi Ueda 
2794d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2795d0bcb878SKiyoshi Ueda 
27961da177e4SLinus Torvalds 	/*
27973b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
27983b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
27993b00b203SMikulas Patocka 	 * to finish.
28001da177e4SLinus Torvalds 	 */
2801401600dfSMikulas Patocka 	r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
28021da177e4SLinus Torvalds 
28036d6f10dfSMilan Broz 	if (noflush)
2804022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
280583d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
28062e93ccc1SKiyoshi Ueda 
28071da177e4SLinus Torvalds 	/* were we interrupted ? */
280846125c1cSMilan Broz 	if (r < 0) {
28099a1fb464SMikulas Patocka 		dm_queue_flush(md);
281073d410c0SMilan Broz 
2811cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
28129f518b27SKiyoshi Ueda 			start_queue(md->queue);
2813cec47e3dSKiyoshi Ueda 
28142ca3310eSAlasdair G Kergon 		unlock_fs(md);
281583d5e5b0SMikulas Patocka 		goto out_unlock; /* pushback list is already flushed, so skip flush */
28162ca3310eSAlasdair G Kergon 	}
28172ca3310eSAlasdair G Kergon 
28183b00b203SMikulas Patocka 	/*
28193b00b203SMikulas Patocka 	 * If dm_wait_for_completion returned 0, the device is completely
28203b00b203SMikulas Patocka 	 * quiescent now. There is no request-processing activity. All new
28213b00b203SMikulas Patocka 	 * requests are being added to md->deferred list.
28223b00b203SMikulas Patocka 	 */
28233b00b203SMikulas Patocka 
28241da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
28251da177e4SLinus Torvalds 
28264d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
28274d4471cbSKiyoshi Ueda 
2828d287483dSAlasdair G Kergon out_unlock:
2829e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2830cf222b37SAlasdair G Kergon 	return r;
28311da177e4SLinus Torvalds }
28321da177e4SLinus Torvalds 
28331da177e4SLinus Torvalds int dm_resume(struct mapped_device *md)
28341da177e4SLinus Torvalds {
2835cf222b37SAlasdair G Kergon 	int r = -EINVAL;
2836cf222b37SAlasdair G Kergon 	struct dm_table *map = NULL;
28371da177e4SLinus Torvalds 
2838e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
28394f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
2840cf222b37SAlasdair G Kergon 		goto out;
2841cf222b37SAlasdair G Kergon 
284283d5e5b0SMikulas Patocka 	map = md->map;
28432ca3310eSAlasdair G Kergon 	if (!map || !dm_table_get_size(map))
2844cf222b37SAlasdair G Kergon 		goto out;
28451da177e4SLinus Torvalds 
28468757b776SMilan Broz 	r = dm_table_resume_targets(map);
28478757b776SMilan Broz 	if (r)
28488757b776SMilan Broz 		goto out;
28492ca3310eSAlasdair G Kergon 
28509a1fb464SMikulas Patocka 	dm_queue_flush(md);
28512ca3310eSAlasdair G Kergon 
2852cec47e3dSKiyoshi Ueda 	/*
2853cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2854cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2855cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2856cec47e3dSKiyoshi Ueda 	 */
2857cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2858cec47e3dSKiyoshi Ueda 		start_queue(md->queue);
2859cec47e3dSKiyoshi Ueda 
28602ca3310eSAlasdair G Kergon 	unlock_fs(md);
28612ca3310eSAlasdair G Kergon 
28622ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
28632ca3310eSAlasdair G Kergon 
2864cf222b37SAlasdair G Kergon 	r = 0;
2865cf222b37SAlasdair G Kergon out:
2866e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
28672ca3310eSAlasdair G Kergon 
2868cf222b37SAlasdair G Kergon 	return r;
28691da177e4SLinus Torvalds }
28701da177e4SLinus Torvalds 
2871fd2ed4d2SMikulas Patocka /*
2872fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
2873fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
2874fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
2875fd2ed4d2SMikulas Patocka  *
2876fd2ed4d2SMikulas Patocka  * Internal suspend holds md->suspend_lock, which prevents interaction with
2877fd2ed4d2SMikulas Patocka  * userspace-driven suspend.
2878fd2ed4d2SMikulas Patocka  */
2879fd2ed4d2SMikulas Patocka 
2880fd2ed4d2SMikulas Patocka void dm_internal_suspend(struct mapped_device *md)
2881fd2ed4d2SMikulas Patocka {
2882fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
2883fd2ed4d2SMikulas Patocka 	if (dm_suspended_md(md))
2884fd2ed4d2SMikulas Patocka 		return;
2885fd2ed4d2SMikulas Patocka 
2886fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2887fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
2888fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
2889fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2890fd2ed4d2SMikulas Patocka }
2891fd2ed4d2SMikulas Patocka 
2892fd2ed4d2SMikulas Patocka void dm_internal_resume(struct mapped_device *md)
2893fd2ed4d2SMikulas Patocka {
2894fd2ed4d2SMikulas Patocka 	if (dm_suspended_md(md))
2895fd2ed4d2SMikulas Patocka 		goto done;
2896fd2ed4d2SMikulas Patocka 
2897fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
2898fd2ed4d2SMikulas Patocka 
2899fd2ed4d2SMikulas Patocka done:
2900fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
2901fd2ed4d2SMikulas Patocka }
2902fd2ed4d2SMikulas Patocka 
29031da177e4SLinus Torvalds /*-----------------------------------------------------------------
29041da177e4SLinus Torvalds  * Event notification.
29051da177e4SLinus Torvalds  *---------------------------------------------------------------*/
29063abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
290760935eb2SMilan Broz 		       unsigned cookie)
290869267a30SAlasdair G Kergon {
290960935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
291060935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
291160935eb2SMilan Broz 
291260935eb2SMilan Broz 	if (!cookie)
29133abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
291460935eb2SMilan Broz 	else {
291560935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
291660935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
29173abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
29183abf85b5SPeter Rajnoha 					  action, envp);
291960935eb2SMilan Broz 	}
292069267a30SAlasdair G Kergon }
292169267a30SAlasdair G Kergon 
29227a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
29237a8c3d3bSMike Anderson {
29247a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
29257a8c3d3bSMike Anderson }
29267a8c3d3bSMike Anderson 
29271da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
29281da177e4SLinus Torvalds {
29291da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
29301da177e4SLinus Torvalds }
29311da177e4SLinus Torvalds 
29321da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
29331da177e4SLinus Torvalds {
29341da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
29351da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
29361da177e4SLinus Torvalds }
29371da177e4SLinus Torvalds 
29387a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
29397a8c3d3bSMike Anderson {
29407a8c3d3bSMike Anderson 	unsigned long flags;
29417a8c3d3bSMike Anderson 
29427a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
29437a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
29447a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
29457a8c3d3bSMike Anderson }
29467a8c3d3bSMike Anderson 
29471da177e4SLinus Torvalds /*
29481da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
29491da177e4SLinus Torvalds  * count on 'md'.
29501da177e4SLinus Torvalds  */
29511da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
29521da177e4SLinus Torvalds {
29531da177e4SLinus Torvalds 	return md->disk;
29541da177e4SLinus Torvalds }
29551da177e4SLinus Torvalds 
2956784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2957784aae73SMilan Broz {
29582995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
2959784aae73SMilan Broz }
2960784aae73SMilan Broz 
2961784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2962784aae73SMilan Broz {
2963784aae73SMilan Broz 	struct mapped_device *md;
2964784aae73SMilan Broz 
29652995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
2966784aae73SMilan Broz 
29674d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
2968432a212cSMike Anderson 	    dm_deleting_md(md))
29694d89b7b4SMilan Broz 		return NULL;
29704d89b7b4SMilan Broz 
2971784aae73SMilan Broz 	dm_get(md);
2972784aae73SMilan Broz 	return md;
2973784aae73SMilan Broz }
2974784aae73SMilan Broz 
29754f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
29761da177e4SLinus Torvalds {
29771da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
29781da177e4SLinus Torvalds }
29791da177e4SLinus Torvalds 
29802c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
29812c140a24SMikulas Patocka {
29822c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
29832c140a24SMikulas Patocka }
29842c140a24SMikulas Patocka 
298564dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
298664dbce58SKiyoshi Ueda {
2987ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
298864dbce58SKiyoshi Ueda }
298964dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
299064dbce58SKiyoshi Ueda 
29912e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
29922e93ccc1SKiyoshi Ueda {
2993ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
29942e93ccc1SKiyoshi Ueda }
29952e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
29962e93ccc1SKiyoshi Ueda 
2997c0820cf5SMikulas Patocka struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity, unsigned per_bio_data_size)
2998e6ee8c0bSKiyoshi Ueda {
29995f015204SJun'ichi Nomura 	struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
30005f015204SJun'ichi Nomura 	struct kmem_cache *cachep;
30015f015204SJun'ichi Nomura 	unsigned int pool_size;
30025f015204SJun'ichi Nomura 	unsigned int front_pad;
3003e6ee8c0bSKiyoshi Ueda 
3004e6ee8c0bSKiyoshi Ueda 	if (!pools)
3005e6ee8c0bSKiyoshi Ueda 		return NULL;
3006e6ee8c0bSKiyoshi Ueda 
300723e5083bSJun'ichi Nomura 	if (type == DM_TYPE_BIO_BASED) {
30085f015204SJun'ichi Nomura 		cachep = _io_cache;
3009e8603136SMike Snitzer 		pool_size = dm_get_reserved_bio_based_ios();
30105f015204SJun'ichi Nomura 		front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
30115f015204SJun'ichi Nomura 	} else if (type == DM_TYPE_REQUEST_BASED) {
30125f015204SJun'ichi Nomura 		cachep = _rq_tio_cache;
3013f4790826SMike Snitzer 		pool_size = dm_get_reserved_rq_based_ios();
30145f015204SJun'ichi Nomura 		front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
30155f015204SJun'ichi Nomura 		/* per_bio_data_size is not used. See __bind_mempools(). */
30165f015204SJun'ichi Nomura 		WARN_ON(per_bio_data_size != 0);
30175f015204SJun'ichi Nomura 	} else
30185f015204SJun'ichi Nomura 		goto out;
30195f015204SJun'ichi Nomura 
30206cfa5857SMike Snitzer 	pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
3021e6ee8c0bSKiyoshi Ueda 	if (!pools->io_pool)
30225f015204SJun'ichi Nomura 		goto out;
3023e6ee8c0bSKiyoshi Ueda 
30243d8aab2dSJunichi Nomura 	pools->bs = bioset_create_nobvec(pool_size, front_pad);
3025e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
30265f015204SJun'ichi Nomura 		goto out;
3027e6ee8c0bSKiyoshi Ueda 
3028a91a2785SMartin K. Petersen 	if (integrity && bioset_integrity_create(pools->bs, pool_size))
30295f015204SJun'ichi Nomura 		goto out;
3030a91a2785SMartin K. Petersen 
3031e6ee8c0bSKiyoshi Ueda 	return pools;
3032e6ee8c0bSKiyoshi Ueda 
30335f015204SJun'ichi Nomura out:
30345f015204SJun'ichi Nomura 	dm_free_md_mempools(pools);
3035e6ee8c0bSKiyoshi Ueda 
3036e6ee8c0bSKiyoshi Ueda 	return NULL;
3037e6ee8c0bSKiyoshi Ueda }
3038e6ee8c0bSKiyoshi Ueda 
3039e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
3040e6ee8c0bSKiyoshi Ueda {
3041e6ee8c0bSKiyoshi Ueda 	if (!pools)
3042e6ee8c0bSKiyoshi Ueda 		return;
3043e6ee8c0bSKiyoshi Ueda 
3044e6ee8c0bSKiyoshi Ueda 	if (pools->io_pool)
3045e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->io_pool);
3046e6ee8c0bSKiyoshi Ueda 
3047e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
3048e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
3049e6ee8c0bSKiyoshi Ueda 
3050e6ee8c0bSKiyoshi Ueda 	kfree(pools);
3051e6ee8c0bSKiyoshi Ueda }
3052e6ee8c0bSKiyoshi Ueda 
305383d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
30541da177e4SLinus Torvalds 	.open = dm_blk_open,
30551da177e4SLinus Torvalds 	.release = dm_blk_close,
3056aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
30573ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
30581da177e4SLinus Torvalds 	.owner = THIS_MODULE
30591da177e4SLinus Torvalds };
30601da177e4SLinus Torvalds 
30611da177e4SLinus Torvalds /*
30621da177e4SLinus Torvalds  * module hooks
30631da177e4SLinus Torvalds  */
30641da177e4SLinus Torvalds module_init(dm_init);
30651da177e4SLinus Torvalds module_exit(dm_exit);
30661da177e4SLinus Torvalds 
30671da177e4SLinus Torvalds module_param(major, uint, 0);
30681da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
3069f4790826SMike Snitzer 
3070e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3071e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3072e8603136SMike Snitzer 
3073f4790826SMike Snitzer module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
3074f4790826SMike Snitzer MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
3075f4790826SMike Snitzer 
30761da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
30771da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
30781da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3079