xref: /openbmc/linux/drivers/md/dm.c (revision 2995fa78e423d7193f3b57835f6c1c75006a0315)
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 
571da177e4SLinus Torvalds /*
588fbf26adSKiyoshi Ueda  * For bio-based dm.
591da177e4SLinus Torvalds  * One of these is allocated per bio.
601da177e4SLinus Torvalds  */
611da177e4SLinus Torvalds struct dm_io {
621da177e4SLinus Torvalds 	struct mapped_device *md;
631da177e4SLinus Torvalds 	int error;
641da177e4SLinus Torvalds 	atomic_t io_count;
656ae2fa67SRichard Kennedy 	struct bio *bio;
663eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
67f88fb981SKiyoshi Ueda 	spinlock_t endio_lock;
68fd2ed4d2SMikulas Patocka 	struct dm_stats_aux stats_aux;
691da177e4SLinus Torvalds };
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /*
728fbf26adSKiyoshi Ueda  * For request-based dm.
738fbf26adSKiyoshi Ueda  * One of these is allocated per request.
748fbf26adSKiyoshi Ueda  */
758fbf26adSKiyoshi Ueda struct dm_rq_target_io {
768fbf26adSKiyoshi Ueda 	struct mapped_device *md;
778fbf26adSKiyoshi Ueda 	struct dm_target *ti;
788fbf26adSKiyoshi Ueda 	struct request *orig, clone;
798fbf26adSKiyoshi Ueda 	int error;
808fbf26adSKiyoshi Ueda 	union map_info info;
818fbf26adSKiyoshi Ueda };
828fbf26adSKiyoshi Ueda 
838fbf26adSKiyoshi Ueda /*
8494818742SKent Overstreet  * For request-based dm - the bio clones we allocate are embedded in these
8594818742SKent Overstreet  * structs.
8694818742SKent Overstreet  *
8794818742SKent Overstreet  * We allocate these with bio_alloc_bioset, using the front_pad parameter when
8894818742SKent Overstreet  * the bioset is created - this means the bio has to come at the end of the
8994818742SKent Overstreet  * struct.
908fbf26adSKiyoshi Ueda  */
918fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info {
928fbf26adSKiyoshi Ueda 	struct bio *orig;
93cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio;
9494818742SKent Overstreet 	struct bio clone;
958fbf26adSKiyoshi Ueda };
968fbf26adSKiyoshi Ueda 
971da177e4SLinus Torvalds union map_info *dm_get_mapinfo(struct bio *bio)
981da177e4SLinus Torvalds {
991da177e4SLinus Torvalds 	if (bio && bio->bi_private)
100028867acSAlasdair G Kergon 		return &((struct dm_target_io *)bio->bi_private)->info;
1011da177e4SLinus Torvalds 	return NULL;
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
104cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq)
105cec47e3dSKiyoshi Ueda {
106cec47e3dSKiyoshi Ueda 	if (rq && rq->end_io_data)
107cec47e3dSKiyoshi Ueda 		return &((struct dm_rq_target_io *)rq->end_io_data)->info;
108cec47e3dSKiyoshi Ueda 	return NULL;
109cec47e3dSKiyoshi Ueda }
110cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
111cec47e3dSKiyoshi Ueda 
112ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
113ba61fdd1SJeff Mahoney 
1141da177e4SLinus Torvalds /*
1151da177e4SLinus Torvalds  * Bits for the md->flags field.
1161da177e4SLinus Torvalds  */
1171eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
1181da177e4SLinus Torvalds #define DMF_SUSPENDED 1
119aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
120fba9f90eSJeff Mahoney #define DMF_FREEING 3
1215c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
1222e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
123d5b9dd04SMikulas Patocka #define DMF_MERGE_IS_OPTIONAL 6
1242c140a24SMikulas Patocka #define DMF_DEFERRED_REMOVE 7
1251da177e4SLinus Torvalds 
126304f3f6aSMilan Broz /*
12783d5e5b0SMikulas Patocka  * A dummy definition to make RCU happy.
12883d5e5b0SMikulas Patocka  * struct dm_table should never be dereferenced in this file.
12983d5e5b0SMikulas Patocka  */
13083d5e5b0SMikulas Patocka struct dm_table {
13183d5e5b0SMikulas Patocka 	int undefined__;
13283d5e5b0SMikulas Patocka };
13383d5e5b0SMikulas Patocka 
13483d5e5b0SMikulas Patocka /*
135304f3f6aSMilan Broz  * Work processed by per-device workqueue.
136304f3f6aSMilan Broz  */
1371da177e4SLinus Torvalds struct mapped_device {
13883d5e5b0SMikulas Patocka 	struct srcu_struct io_barrier;
139e61290a4SDaniel Walker 	struct mutex suspend_lock;
1401da177e4SLinus Torvalds 	atomic_t holders;
1415c6bd75dSAlasdair G Kergon 	atomic_t open_count;
1421da177e4SLinus Torvalds 
1432a7faeb1SMikulas Patocka 	/*
1442a7faeb1SMikulas Patocka 	 * The current mapping.
1452a7faeb1SMikulas Patocka 	 * Use dm_get_live_table{_fast} or take suspend_lock for
1462a7faeb1SMikulas Patocka 	 * dereference.
1472a7faeb1SMikulas Patocka 	 */
1482a7faeb1SMikulas Patocka 	struct dm_table *map;
1492a7faeb1SMikulas Patocka 
1501da177e4SLinus Torvalds 	unsigned long flags;
1511da177e4SLinus Torvalds 
152165125e1SJens Axboe 	struct request_queue *queue;
153a5664dadSMike Snitzer 	unsigned type;
1544a0b4ddfSMike Snitzer 	/* Protect queue and type against concurrent access. */
155a5664dadSMike Snitzer 	struct mutex type_lock;
156a5664dadSMike Snitzer 
15736a0456fSAlasdair G Kergon 	struct target_type *immutable_target_type;
15836a0456fSAlasdair G Kergon 
1591da177e4SLinus Torvalds 	struct gendisk *disk;
1607e51f257SMike Anderson 	char name[16];
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	void *interface_ptr;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	/*
1651da177e4SLinus Torvalds 	 * A list of ios that arrived while we were suspended.
1661da177e4SLinus Torvalds 	 */
167316d315bSNikanth Karthikesan 	atomic_t pending[2];
1681da177e4SLinus Torvalds 	wait_queue_head_t wait;
16953d5914fSMikulas Patocka 	struct work_struct work;
1701da177e4SLinus Torvalds 	struct bio_list deferred;
171022c2611SMikulas Patocka 	spinlock_t deferred_lock;
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	/*
17429e4013dSTejun Heo 	 * Processing queue (flush)
175304f3f6aSMilan Broz 	 */
176304f3f6aSMilan Broz 	struct workqueue_struct *wq;
177304f3f6aSMilan Broz 
178304f3f6aSMilan Broz 	/*
1791da177e4SLinus Torvalds 	 * io objects are allocated from here.
1801da177e4SLinus Torvalds 	 */
1811da177e4SLinus Torvalds 	mempool_t *io_pool;
1821da177e4SLinus Torvalds 
1839faf400fSStefan Bader 	struct bio_set *bs;
1849faf400fSStefan Bader 
1851da177e4SLinus Torvalds 	/*
1861da177e4SLinus Torvalds 	 * Event handling.
1871da177e4SLinus Torvalds 	 */
1881da177e4SLinus Torvalds 	atomic_t event_nr;
1891da177e4SLinus Torvalds 	wait_queue_head_t eventq;
1907a8c3d3bSMike Anderson 	atomic_t uevent_seq;
1917a8c3d3bSMike Anderson 	struct list_head uevent_list;
1927a8c3d3bSMike Anderson 	spinlock_t uevent_lock; /* Protect access to uevent_list */
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	/*
1951da177e4SLinus Torvalds 	 * freeze/thaw support require holding onto a super block
1961da177e4SLinus Torvalds 	 */
1971da177e4SLinus Torvalds 	struct super_block *frozen_sb;
198db8fef4fSMikulas Patocka 	struct block_device *bdev;
1993ac51e74SDarrick J. Wong 
2003ac51e74SDarrick J. Wong 	/* forced geometry settings */
2013ac51e74SDarrick J. Wong 	struct hd_geometry geometry;
202784aae73SMilan Broz 
203*2995fa78SMikulas Patocka 	/* kobject and completion */
204*2995fa78SMikulas Patocka 	struct dm_kobject_holder kobj_holder;
205be35f486SMikulas Patocka 
206d87f4c14STejun Heo 	/* zero-length flush that will be cloned and submitted to targets */
207d87f4c14STejun Heo 	struct bio flush_bio;
208fd2ed4d2SMikulas Patocka 
209fd2ed4d2SMikulas Patocka 	struct dm_stats stats;
2101da177e4SLinus Torvalds };
2111da177e4SLinus Torvalds 
212e6ee8c0bSKiyoshi Ueda /*
213e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
214e6ee8c0bSKiyoshi Ueda  */
215e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
216e6ee8c0bSKiyoshi Ueda 	mempool_t *io_pool;
217e6ee8c0bSKiyoshi Ueda 	struct bio_set *bs;
218e6ee8c0bSKiyoshi Ueda };
219e6ee8c0bSKiyoshi Ueda 
2206cfa5857SMike Snitzer #define RESERVED_BIO_BASED_IOS		16
2216cfa5857SMike Snitzer #define RESERVED_REQUEST_BASED_IOS	256
222f4790826SMike Snitzer #define RESERVED_MAX_IOS		1024
223e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
2248fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
22594818742SKent Overstreet 
226f4790826SMike Snitzer /*
227e8603136SMike Snitzer  * Bio-based DM's mempools' reserved IOs set by the user.
228e8603136SMike Snitzer  */
229e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
230e8603136SMike Snitzer 
231e8603136SMike Snitzer /*
232f4790826SMike Snitzer  * Request-based DM's mempools' reserved IOs set by the user.
233f4790826SMike Snitzer  */
234f4790826SMike Snitzer static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
235f4790826SMike Snitzer 
236f4790826SMike Snitzer static unsigned __dm_get_reserved_ios(unsigned *reserved_ios,
237f4790826SMike Snitzer 				      unsigned def, unsigned max)
238f4790826SMike Snitzer {
239f4790826SMike Snitzer 	unsigned ios = ACCESS_ONCE(*reserved_ios);
240f4790826SMike Snitzer 	unsigned modified_ios = 0;
241f4790826SMike Snitzer 
242f4790826SMike Snitzer 	if (!ios)
243f4790826SMike Snitzer 		modified_ios = def;
244f4790826SMike Snitzer 	else if (ios > max)
245f4790826SMike Snitzer 		modified_ios = max;
246f4790826SMike Snitzer 
247f4790826SMike Snitzer 	if (modified_ios) {
248f4790826SMike Snitzer 		(void)cmpxchg(reserved_ios, ios, modified_ios);
249f4790826SMike Snitzer 		ios = modified_ios;
250f4790826SMike Snitzer 	}
251f4790826SMike Snitzer 
252f4790826SMike Snitzer 	return ios;
253f4790826SMike Snitzer }
254f4790826SMike Snitzer 
255e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void)
256e8603136SMike Snitzer {
257e8603136SMike Snitzer 	return __dm_get_reserved_ios(&reserved_bio_based_ios,
258e8603136SMike Snitzer 				     RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
259e8603136SMike Snitzer }
260e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
261e8603136SMike Snitzer 
262f4790826SMike Snitzer unsigned dm_get_reserved_rq_based_ios(void)
263f4790826SMike Snitzer {
264f4790826SMike Snitzer 	return __dm_get_reserved_ios(&reserved_rq_based_ios,
265f4790826SMike Snitzer 				     RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
266f4790826SMike Snitzer }
267f4790826SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
268f4790826SMike Snitzer 
2691da177e4SLinus Torvalds static int __init local_init(void)
2701da177e4SLinus Torvalds {
27151157b4aSKiyoshi Ueda 	int r = -ENOMEM;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
274028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
2751da177e4SLinus Torvalds 	if (!_io_cache)
27651157b4aSKiyoshi Ueda 		return r;
2771da177e4SLinus Torvalds 
2788fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
2798fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
280dba14160SMikulas Patocka 		goto out_free_io_cache;
2818fbf26adSKiyoshi Ueda 
28251e5b2bdSMike Anderson 	r = dm_uevent_init();
28351157b4aSKiyoshi Ueda 	if (r)
28423e5083bSJun'ichi Nomura 		goto out_free_rq_tio_cache;
28551e5b2bdSMike Anderson 
2861da177e4SLinus Torvalds 	_major = major;
2871da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
28851157b4aSKiyoshi Ueda 	if (r < 0)
28951157b4aSKiyoshi Ueda 		goto out_uevent_exit;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	if (!_major)
2921da177e4SLinus Torvalds 		_major = r;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	return 0;
29551157b4aSKiyoshi Ueda 
29651157b4aSKiyoshi Ueda out_uevent_exit:
29751157b4aSKiyoshi Ueda 	dm_uevent_exit();
2988fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
2998fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
30051157b4aSKiyoshi Ueda out_free_io_cache:
30151157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
30251157b4aSKiyoshi Ueda 
30351157b4aSKiyoshi Ueda 	return r;
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds static void local_exit(void)
3071da177e4SLinus Torvalds {
3082c140a24SMikulas Patocka 	flush_scheduled_work();
3092c140a24SMikulas Patocka 
3108fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
3111da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
31200d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
31351e5b2bdSMike Anderson 	dm_uevent_exit();
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	_major = 0;
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	DMINFO("cleaned up");
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
320b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
3211da177e4SLinus Torvalds 	local_init,
3221da177e4SLinus Torvalds 	dm_target_init,
3231da177e4SLinus Torvalds 	dm_linear_init,
3241da177e4SLinus Torvalds 	dm_stripe_init,
325952b3557SMikulas Patocka 	dm_io_init,
326945fa4d2SMikulas Patocka 	dm_kcopyd_init,
3271da177e4SLinus Torvalds 	dm_interface_init,
328fd2ed4d2SMikulas Patocka 	dm_statistics_init,
3291da177e4SLinus Torvalds };
3301da177e4SLinus Torvalds 
331b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
3321da177e4SLinus Torvalds 	local_exit,
3331da177e4SLinus Torvalds 	dm_target_exit,
3341da177e4SLinus Torvalds 	dm_linear_exit,
3351da177e4SLinus Torvalds 	dm_stripe_exit,
336952b3557SMikulas Patocka 	dm_io_exit,
337945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
3381da177e4SLinus Torvalds 	dm_interface_exit,
339fd2ed4d2SMikulas Patocka 	dm_statistics_exit,
3401da177e4SLinus Torvalds };
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds static int __init dm_init(void)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	int r, i;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
3491da177e4SLinus Torvalds 		r = _inits[i]();
3501da177e4SLinus Torvalds 		if (r)
3511da177e4SLinus Torvalds 			goto bad;
3521da177e4SLinus Torvalds 	}
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 	return 0;
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds       bad:
3571da177e4SLinus Torvalds 	while (i--)
3581da177e4SLinus Torvalds 		_exits[i]();
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	return r;
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds static void __exit dm_exit(void)
3641da177e4SLinus Torvalds {
3651da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	while (i--)
3681da177e4SLinus Torvalds 		_exits[i]();
369d15b774cSAlasdair G Kergon 
370d15b774cSAlasdair G Kergon 	/*
371d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
372d15b774cSAlasdair G Kergon 	 */
373d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds /*
3771da177e4SLinus Torvalds  * Block device functions
3781da177e4SLinus Torvalds  */
379432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
380432a212cSMike Anderson {
381432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
382432a212cSMike Anderson }
383432a212cSMike Anderson 
384fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3851da177e4SLinus Torvalds {
3861da177e4SLinus Torvalds 	struct mapped_device *md;
3871da177e4SLinus Torvalds 
388fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
389fba9f90eSJeff Mahoney 
390fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
391fba9f90eSJeff Mahoney 	if (!md)
392fba9f90eSJeff Mahoney 		goto out;
393fba9f90eSJeff Mahoney 
3945c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
395432a212cSMike Anderson 	    dm_deleting_md(md)) {
396fba9f90eSJeff Mahoney 		md = NULL;
397fba9f90eSJeff Mahoney 		goto out;
398fba9f90eSJeff Mahoney 	}
399fba9f90eSJeff Mahoney 
4001da177e4SLinus Torvalds 	dm_get(md);
4015c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
402fba9f90eSJeff Mahoney 
403fba9f90eSJeff Mahoney out:
404fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
405fba9f90eSJeff Mahoney 
406fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds 
409db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode)
4101da177e4SLinus Torvalds {
411fe5f9f2cSAl Viro 	struct mapped_device *md = disk->private_data;
4126e9624b8SArnd Bergmann 
4134a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
4144a1aeb98SMilan Broz 
4152c140a24SMikulas Patocka 	if (atomic_dec_and_test(&md->open_count) &&
4162c140a24SMikulas Patocka 	    (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
4172c140a24SMikulas Patocka 		schedule_work(&deferred_remove_work);
4182c140a24SMikulas Patocka 
4191da177e4SLinus Torvalds 	dm_put(md);
4204a1aeb98SMilan Broz 
4214a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds 
4245c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
4255c6bd75dSAlasdair G Kergon {
4265c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
4275c6bd75dSAlasdair G Kergon }
4285c6bd75dSAlasdair G Kergon 
4295c6bd75dSAlasdair G Kergon /*
4305c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
4315c6bd75dSAlasdair G Kergon  */
4322c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
4335c6bd75dSAlasdair G Kergon {
4345c6bd75dSAlasdair G Kergon 	int r = 0;
4355c6bd75dSAlasdair G Kergon 
4365c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
4375c6bd75dSAlasdair G Kergon 
4382c140a24SMikulas Patocka 	if (dm_open_count(md)) {
4395c6bd75dSAlasdair G Kergon 		r = -EBUSY;
4402c140a24SMikulas Patocka 		if (mark_deferred)
4412c140a24SMikulas Patocka 			set_bit(DMF_DEFERRED_REMOVE, &md->flags);
4422c140a24SMikulas Patocka 	} else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
4432c140a24SMikulas Patocka 		r = -EEXIST;
4445c6bd75dSAlasdair G Kergon 	else
4455c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
4465c6bd75dSAlasdair G Kergon 
4475c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
4485c6bd75dSAlasdair G Kergon 
4495c6bd75dSAlasdair G Kergon 	return r;
4505c6bd75dSAlasdair G Kergon }
4515c6bd75dSAlasdair G Kergon 
4522c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md)
4532c140a24SMikulas Patocka {
4542c140a24SMikulas Patocka 	int r = 0;
4552c140a24SMikulas Patocka 
4562c140a24SMikulas Patocka 	spin_lock(&_minor_lock);
4572c140a24SMikulas Patocka 
4582c140a24SMikulas Patocka 	if (test_bit(DMF_DELETING, &md->flags))
4592c140a24SMikulas Patocka 		r = -EBUSY;
4602c140a24SMikulas Patocka 	else
4612c140a24SMikulas Patocka 		clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
4622c140a24SMikulas Patocka 
4632c140a24SMikulas Patocka 	spin_unlock(&_minor_lock);
4642c140a24SMikulas Patocka 
4652c140a24SMikulas Patocka 	return r;
4662c140a24SMikulas Patocka }
4672c140a24SMikulas Patocka 
4682c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w)
4692c140a24SMikulas Patocka {
4702c140a24SMikulas Patocka 	dm_deferred_remove();
4712c140a24SMikulas Patocka }
4722c140a24SMikulas Patocka 
473fd2ed4d2SMikulas Patocka sector_t dm_get_size(struct mapped_device *md)
474fd2ed4d2SMikulas Patocka {
475fd2ed4d2SMikulas Patocka 	return get_capacity(md->disk);
476fd2ed4d2SMikulas Patocka }
477fd2ed4d2SMikulas Patocka 
478fd2ed4d2SMikulas Patocka struct dm_stats *dm_get_stats(struct mapped_device *md)
479fd2ed4d2SMikulas Patocka {
480fd2ed4d2SMikulas Patocka 	return &md->stats;
481fd2ed4d2SMikulas Patocka }
482fd2ed4d2SMikulas Patocka 
4833ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4843ac51e74SDarrick J. Wong {
4853ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
4863ac51e74SDarrick J. Wong 
4873ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
4883ac51e74SDarrick J. Wong }
4893ac51e74SDarrick J. Wong 
490fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
491aa129a22SMilan Broz 			unsigned int cmd, unsigned long arg)
492aa129a22SMilan Broz {
493fe5f9f2cSAl Viro 	struct mapped_device *md = bdev->bd_disk->private_data;
49483d5e5b0SMikulas Patocka 	int srcu_idx;
4956c182cd8SHannes Reinecke 	struct dm_table *map;
496aa129a22SMilan Broz 	struct dm_target *tgt;
497aa129a22SMilan Broz 	int r = -ENOTTY;
498aa129a22SMilan Broz 
4996c182cd8SHannes Reinecke retry:
50083d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
50183d5e5b0SMikulas Patocka 
502aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
503aa129a22SMilan Broz 		goto out;
504aa129a22SMilan Broz 
505aa129a22SMilan Broz 	/* We only support devices that have a single target */
506aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
507aa129a22SMilan Broz 		goto out;
508aa129a22SMilan Broz 
509aa129a22SMilan Broz 	tgt = dm_table_get_target(map, 0);
510aa129a22SMilan Broz 
5114f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
512aa129a22SMilan Broz 		r = -EAGAIN;
513aa129a22SMilan Broz 		goto out;
514aa129a22SMilan Broz 	}
515aa129a22SMilan Broz 
516aa129a22SMilan Broz 	if (tgt->type->ioctl)
517647b3d00SAl Viro 		r = tgt->type->ioctl(tgt, cmd, arg);
518aa129a22SMilan Broz 
519aa129a22SMilan Broz out:
52083d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
521aa129a22SMilan Broz 
5226c182cd8SHannes Reinecke 	if (r == -ENOTCONN) {
5236c182cd8SHannes Reinecke 		msleep(10);
5246c182cd8SHannes Reinecke 		goto retry;
5256c182cd8SHannes Reinecke 	}
5266c182cd8SHannes Reinecke 
527aa129a22SMilan Broz 	return r;
528aa129a22SMilan Broz }
529aa129a22SMilan Broz 
530028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
5311da177e4SLinus Torvalds {
5321da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
5331da177e4SLinus Torvalds }
5341da177e4SLinus Torvalds 
535028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
5361da177e4SLinus Torvalds {
5371da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
5381da177e4SLinus Torvalds }
5391da177e4SLinus Torvalds 
540028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
5411da177e4SLinus Torvalds {
542dba14160SMikulas Patocka 	bio_put(&tio->clone);
5431da177e4SLinus Torvalds }
5441da177e4SLinus Torvalds 
54508885643SKiyoshi Ueda static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
54608885643SKiyoshi Ueda 					    gfp_t gfp_mask)
547cec47e3dSKiyoshi Ueda {
5485f015204SJun'ichi Nomura 	return mempool_alloc(md->io_pool, gfp_mask);
549cec47e3dSKiyoshi Ueda }
550cec47e3dSKiyoshi Ueda 
551cec47e3dSKiyoshi Ueda static void free_rq_tio(struct dm_rq_target_io *tio)
552cec47e3dSKiyoshi Ueda {
5535f015204SJun'ichi Nomura 	mempool_free(tio, tio->md->io_pool);
554cec47e3dSKiyoshi Ueda }
555cec47e3dSKiyoshi Ueda 
55690abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md)
55790abb8c4SKiyoshi Ueda {
55890abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
55990abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
56090abb8c4SKiyoshi Ueda }
56190abb8c4SKiyoshi Ueda 
5623eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
5633eaf840eSJun'ichi "Nick" Nomura {
5643eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
565fd2ed4d2SMikulas Patocka 	struct bio *bio = io->bio;
566c9959059STejun Heo 	int cpu;
567fd2ed4d2SMikulas Patocka 	int rw = bio_data_dir(bio);
5683eaf840eSJun'ichi "Nick" Nomura 
5693eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
5703eaf840eSJun'ichi "Nick" Nomura 
571074a7acaSTejun Heo 	cpu = part_stat_lock();
572074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
573074a7acaSTejun Heo 	part_stat_unlock();
5741e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw],
5751e9bb880SShaohua Li 		atomic_inc_return(&md->pending[rw]));
576fd2ed4d2SMikulas Patocka 
577fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
578fd2ed4d2SMikulas Patocka 		dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_sector,
579fd2ed4d2SMikulas Patocka 				    bio_sectors(bio), false, 0, &io->stats_aux);
5803eaf840eSJun'ichi "Nick" Nomura }
5813eaf840eSJun'ichi "Nick" Nomura 
582d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
5833eaf840eSJun'ichi "Nick" Nomura {
5843eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
5853eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
5863eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
587c9959059STejun Heo 	int pending, cpu;
5883eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
5893eaf840eSJun'ichi "Nick" Nomura 
590074a7acaSTejun Heo 	cpu = part_stat_lock();
591074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
592074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
593074a7acaSTejun Heo 	part_stat_unlock();
5943eaf840eSJun'ichi "Nick" Nomura 
595fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
596fd2ed4d2SMikulas Patocka 		dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_sector,
597fd2ed4d2SMikulas Patocka 				    bio_sectors(bio), true, duration, &io->stats_aux);
598fd2ed4d2SMikulas Patocka 
599af7e466aSMikulas Patocka 	/*
600af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
601d87f4c14STejun Heo 	 * a flush.
602af7e466aSMikulas Patocka 	 */
6031e9bb880SShaohua Li 	pending = atomic_dec_return(&md->pending[rw]);
6041e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
605316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
6063eaf840eSJun'ichi "Nick" Nomura 
607d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
608d221d2e7SMikulas Patocka 	if (!pending)
609d221d2e7SMikulas Patocka 		wake_up(&md->wait);
6103eaf840eSJun'ichi "Nick" Nomura }
6113eaf840eSJun'ichi "Nick" Nomura 
6121da177e4SLinus Torvalds /*
6131da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
6141da177e4SLinus Torvalds  */
61592c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
6161da177e4SLinus Torvalds {
61705447420SKiyoshi Ueda 	unsigned long flags;
6181da177e4SLinus Torvalds 
61905447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
6201da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
62105447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
62292c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
6231da177e4SLinus Torvalds }
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds /*
6261da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
6271da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
62883d5e5b0SMikulas Patocka  * dm_put_live_table() when finished.
6291da177e4SLinus Torvalds  */
63083d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
6311da177e4SLinus Torvalds {
63283d5e5b0SMikulas Patocka 	*srcu_idx = srcu_read_lock(&md->io_barrier);
6331da177e4SLinus Torvalds 
63483d5e5b0SMikulas Patocka 	return srcu_dereference(md->map, &md->io_barrier);
63583d5e5b0SMikulas Patocka }
6361da177e4SLinus Torvalds 
63783d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
63883d5e5b0SMikulas Patocka {
63983d5e5b0SMikulas Patocka 	srcu_read_unlock(&md->io_barrier, srcu_idx);
64083d5e5b0SMikulas Patocka }
64183d5e5b0SMikulas Patocka 
64283d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md)
64383d5e5b0SMikulas Patocka {
64483d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
64583d5e5b0SMikulas Patocka 	synchronize_rcu_expedited();
64683d5e5b0SMikulas Patocka }
64783d5e5b0SMikulas Patocka 
64883d5e5b0SMikulas Patocka /*
64983d5e5b0SMikulas Patocka  * A fast alternative to dm_get_live_table/dm_put_live_table.
65083d5e5b0SMikulas Patocka  * The caller must not block between these two functions.
65183d5e5b0SMikulas Patocka  */
65283d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
65383d5e5b0SMikulas Patocka {
65483d5e5b0SMikulas Patocka 	rcu_read_lock();
65583d5e5b0SMikulas Patocka 	return rcu_dereference(md->map);
65683d5e5b0SMikulas Patocka }
65783d5e5b0SMikulas Patocka 
65883d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
65983d5e5b0SMikulas Patocka {
66083d5e5b0SMikulas Patocka 	rcu_read_unlock();
6611da177e4SLinus Torvalds }
6621da177e4SLinus Torvalds 
6633ac51e74SDarrick J. Wong /*
6643ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
6653ac51e74SDarrick J. Wong  */
6663ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
6673ac51e74SDarrick J. Wong {
6683ac51e74SDarrick J. Wong 	*geo = md->geometry;
6693ac51e74SDarrick J. Wong 
6703ac51e74SDarrick J. Wong 	return 0;
6713ac51e74SDarrick J. Wong }
6723ac51e74SDarrick J. Wong 
6733ac51e74SDarrick J. Wong /*
6743ac51e74SDarrick J. Wong  * Set the geometry of a device.
6753ac51e74SDarrick J. Wong  */
6763ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
6773ac51e74SDarrick J. Wong {
6783ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
6793ac51e74SDarrick J. Wong 
6803ac51e74SDarrick J. Wong 	if (geo->start > sz) {
6813ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
6823ac51e74SDarrick J. Wong 		return -EINVAL;
6833ac51e74SDarrick J. Wong 	}
6843ac51e74SDarrick J. Wong 
6853ac51e74SDarrick J. Wong 	md->geometry = *geo;
6863ac51e74SDarrick J. Wong 
6873ac51e74SDarrick J. Wong 	return 0;
6883ac51e74SDarrick J. Wong }
6893ac51e74SDarrick J. Wong 
6901da177e4SLinus Torvalds /*-----------------------------------------------------------------
6911da177e4SLinus Torvalds  * CRUD START:
6921da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
6931da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
6941da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
6951da177e4SLinus Torvalds  *   interests of getting something for people to use I give
6961da177e4SLinus Torvalds  *   you this clearly demarcated crap.
6971da177e4SLinus Torvalds  *---------------------------------------------------------------*/
6981da177e4SLinus Torvalds 
6992e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
7002e93ccc1SKiyoshi Ueda {
7012e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
7022e93ccc1SKiyoshi Ueda }
7032e93ccc1SKiyoshi Ueda 
7041da177e4SLinus Torvalds /*
7051da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
7061da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
7071da177e4SLinus Torvalds  */
708858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
7091da177e4SLinus Torvalds {
7102e93ccc1SKiyoshi Ueda 	unsigned long flags;
711b35f8caaSMilan Broz 	int io_error;
712b35f8caaSMilan Broz 	struct bio *bio;
713b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
7142e93ccc1SKiyoshi Ueda 
7152e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
716f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
717f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
718f88fb981SKiyoshi Ueda 		if (!(io->error > 0 && __noflush_suspending(md)))
7191da177e4SLinus Torvalds 			io->error = error;
720f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
721f88fb981SKiyoshi Ueda 	}
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
7242e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
7252e93ccc1SKiyoshi Ueda 			/*
7262e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
7272e93ccc1SKiyoshi Ueda 			 */
728022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
7296a8736d1STejun Heo 			if (__noflush_suspending(md))
7306a8736d1STejun Heo 				bio_list_add_head(&md->deferred, io->bio);
7316a8736d1STejun Heo 			else
7322e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
7332e93ccc1SKiyoshi Ueda 				io->error = -EIO;
734022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
7352e93ccc1SKiyoshi Ueda 		}
7362e93ccc1SKiyoshi Ueda 
737b35f8caaSMilan Broz 		io_error = io->error;
738b35f8caaSMilan Broz 		bio = io->bio;
739af7e466aSMikulas Patocka 		end_io_acct(io);
740a97f925aSMikulas Patocka 		free_io(md, io);
7411da177e4SLinus Torvalds 
7426a8736d1STejun Heo 		if (io_error == DM_ENDIO_REQUEUE)
7436a8736d1STejun Heo 			return;
7446a8736d1STejun Heo 
745b372d360SMike Snitzer 		if ((bio->bi_rw & REQ_FLUSH) && bio->bi_size) {
7461da177e4SLinus Torvalds 			/*
7476a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
7486a8736d1STejun Heo 			 * without REQ_FLUSH.
7491da177e4SLinus Torvalds 			 */
7506a8736d1STejun Heo 			bio->bi_rw &= ~REQ_FLUSH;
7516a8736d1STejun Heo 			queue_io(md, bio);
7525f3ea37cSArnaldo Carvalho de Melo 		} else {
753b372d360SMike Snitzer 			/* done with normal IO or empty flush */
7540a82a8d1SLinus Torvalds 			trace_block_bio_complete(md->queue, bio, io_error);
755b35f8caaSMilan Broz 			bio_endio(bio, io_error);
7562e93ccc1SKiyoshi Ueda 		}
7571da177e4SLinus Torvalds 	}
758af7e466aSMikulas Patocka }
7591da177e4SLinus Torvalds 
7606712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error)
7611da177e4SLinus Torvalds {
7621da177e4SLinus Torvalds 	int r = 0;
763028867acSAlasdair G Kergon 	struct dm_target_io *tio = bio->bi_private;
764b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
7659faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
7661da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	if (!bio_flagged(bio, BIO_UPTODATE) && !error)
7691da177e4SLinus Torvalds 		error = -EIO;
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 	if (endio) {
7727de3ee57SMikulas Patocka 		r = endio(tio->ti, bio, error);
7732e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
7742e93ccc1SKiyoshi Ueda 			/*
7752e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
7762e93ccc1SKiyoshi Ueda 			 * in dec_pending().
7772e93ccc1SKiyoshi Ueda 			 */
7781da177e4SLinus Torvalds 			error = r;
77945cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
78045cbcd79SKiyoshi Ueda 			/* The target will handle the io */
7816712ecf8SNeilBrown 			return;
78245cbcd79SKiyoshi Ueda 		else if (r) {
78345cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
78445cbcd79SKiyoshi Ueda 			BUG();
78545cbcd79SKiyoshi Ueda 		}
7861da177e4SLinus Torvalds 	}
7871da177e4SLinus Torvalds 
7889faf400fSStefan Bader 	free_tio(md, tio);
789b35f8caaSMilan Broz 	dec_pending(io, error);
7901da177e4SLinus Torvalds }
7911da177e4SLinus Torvalds 
792cec47e3dSKiyoshi Ueda /*
793cec47e3dSKiyoshi Ueda  * Partial completion handling for request-based dm
794cec47e3dSKiyoshi Ueda  */
795cec47e3dSKiyoshi Ueda static void end_clone_bio(struct bio *clone, int error)
796cec47e3dSKiyoshi Ueda {
797cec47e3dSKiyoshi Ueda 	struct dm_rq_clone_bio_info *info = clone->bi_private;
798cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = info->tio;
799cec47e3dSKiyoshi Ueda 	struct bio *bio = info->orig;
800cec47e3dSKiyoshi Ueda 	unsigned int nr_bytes = info->orig->bi_size;
801cec47e3dSKiyoshi Ueda 
802cec47e3dSKiyoshi Ueda 	bio_put(clone);
803cec47e3dSKiyoshi Ueda 
804cec47e3dSKiyoshi Ueda 	if (tio->error)
805cec47e3dSKiyoshi Ueda 		/*
806cec47e3dSKiyoshi Ueda 		 * An error has already been detected on the request.
807cec47e3dSKiyoshi Ueda 		 * Once error occurred, just let clone->end_io() handle
808cec47e3dSKiyoshi Ueda 		 * the remainder.
809cec47e3dSKiyoshi Ueda 		 */
810cec47e3dSKiyoshi Ueda 		return;
811cec47e3dSKiyoshi Ueda 	else if (error) {
812cec47e3dSKiyoshi Ueda 		/*
813cec47e3dSKiyoshi Ueda 		 * Don't notice the error to the upper layer yet.
814cec47e3dSKiyoshi Ueda 		 * The error handling decision is made by the target driver,
815cec47e3dSKiyoshi Ueda 		 * when the request is completed.
816cec47e3dSKiyoshi Ueda 		 */
817cec47e3dSKiyoshi Ueda 		tio->error = error;
818cec47e3dSKiyoshi Ueda 		return;
819cec47e3dSKiyoshi Ueda 	}
820cec47e3dSKiyoshi Ueda 
821cec47e3dSKiyoshi Ueda 	/*
822cec47e3dSKiyoshi Ueda 	 * I/O for the bio successfully completed.
823cec47e3dSKiyoshi Ueda 	 * Notice the data completion to the upper layer.
824cec47e3dSKiyoshi Ueda 	 */
825cec47e3dSKiyoshi Ueda 
826cec47e3dSKiyoshi Ueda 	/*
827cec47e3dSKiyoshi Ueda 	 * bios are processed from the head of the list.
828cec47e3dSKiyoshi Ueda 	 * So the completing bio should always be rq->bio.
829cec47e3dSKiyoshi Ueda 	 * If it's not, something wrong is happening.
830cec47e3dSKiyoshi Ueda 	 */
831cec47e3dSKiyoshi Ueda 	if (tio->orig->bio != bio)
832cec47e3dSKiyoshi Ueda 		DMERR("bio completion is going in the middle of the request");
833cec47e3dSKiyoshi Ueda 
834cec47e3dSKiyoshi Ueda 	/*
835cec47e3dSKiyoshi Ueda 	 * Update the original request.
836cec47e3dSKiyoshi Ueda 	 * Do not use blk_end_request() here, because it may complete
837cec47e3dSKiyoshi Ueda 	 * the original request before the clone, and break the ordering.
838cec47e3dSKiyoshi Ueda 	 */
839cec47e3dSKiyoshi Ueda 	blk_update_request(tio->orig, 0, nr_bytes);
840cec47e3dSKiyoshi Ueda }
841cec47e3dSKiyoshi Ueda 
842cec47e3dSKiyoshi Ueda /*
843cec47e3dSKiyoshi Ueda  * Don't touch any member of the md after calling this function because
844cec47e3dSKiyoshi Ueda  * the md may be freed in dm_put() at the end of this function.
845cec47e3dSKiyoshi Ueda  * Or do dm_get() before calling this function and dm_put() later.
846cec47e3dSKiyoshi Ueda  */
847b4324feeSKiyoshi Ueda static void rq_completed(struct mapped_device *md, int rw, int run_queue)
848cec47e3dSKiyoshi Ueda {
849b4324feeSKiyoshi Ueda 	atomic_dec(&md->pending[rw]);
850cec47e3dSKiyoshi Ueda 
851cec47e3dSKiyoshi Ueda 	/* nudge anyone waiting on suspend queue */
852b4324feeSKiyoshi Ueda 	if (!md_in_flight(md))
853cec47e3dSKiyoshi Ueda 		wake_up(&md->wait);
854cec47e3dSKiyoshi Ueda 
855a8c32a5cSJens Axboe 	/*
856a8c32a5cSJens Axboe 	 * Run this off this callpath, as drivers could invoke end_io while
857a8c32a5cSJens Axboe 	 * inside their request_fn (and holding the queue lock). Calling
858a8c32a5cSJens Axboe 	 * back into ->request_fn() could deadlock attempting to grab the
859a8c32a5cSJens Axboe 	 * queue lock again.
860a8c32a5cSJens Axboe 	 */
861cec47e3dSKiyoshi Ueda 	if (run_queue)
862a8c32a5cSJens Axboe 		blk_run_queue_async(md->queue);
863cec47e3dSKiyoshi Ueda 
864cec47e3dSKiyoshi Ueda 	/*
865cec47e3dSKiyoshi Ueda 	 * dm_put() must be at the end of this function. See the comment above
866cec47e3dSKiyoshi Ueda 	 */
867cec47e3dSKiyoshi Ueda 	dm_put(md);
868cec47e3dSKiyoshi Ueda }
869cec47e3dSKiyoshi Ueda 
870a77e28c7SKiyoshi Ueda static void free_rq_clone(struct request *clone)
871a77e28c7SKiyoshi Ueda {
872a77e28c7SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
873a77e28c7SKiyoshi Ueda 
874a77e28c7SKiyoshi Ueda 	blk_rq_unprep_clone(clone);
875a77e28c7SKiyoshi Ueda 	free_rq_tio(tio);
876a77e28c7SKiyoshi Ueda }
877a77e28c7SKiyoshi Ueda 
878980691e5SKiyoshi Ueda /*
879980691e5SKiyoshi Ueda  * Complete the clone and the original request.
880980691e5SKiyoshi Ueda  * Must be called without queue lock.
881980691e5SKiyoshi Ueda  */
882980691e5SKiyoshi Ueda static void dm_end_request(struct request *clone, int error)
883980691e5SKiyoshi Ueda {
884980691e5SKiyoshi Ueda 	int rw = rq_data_dir(clone);
885980691e5SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
886980691e5SKiyoshi Ueda 	struct mapped_device *md = tio->md;
887980691e5SKiyoshi Ueda 	struct request *rq = tio->orig;
888980691e5SKiyoshi Ueda 
88929e4013dSTejun Heo 	if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
890980691e5SKiyoshi Ueda 		rq->errors = clone->errors;
891980691e5SKiyoshi Ueda 		rq->resid_len = clone->resid_len;
892980691e5SKiyoshi Ueda 
893980691e5SKiyoshi Ueda 		if (rq->sense)
894980691e5SKiyoshi Ueda 			/*
895980691e5SKiyoshi Ueda 			 * We are using the sense buffer of the original
896980691e5SKiyoshi Ueda 			 * request.
897980691e5SKiyoshi Ueda 			 * So setting the length of the sense data is enough.
898980691e5SKiyoshi Ueda 			 */
899980691e5SKiyoshi Ueda 			rq->sense_len = clone->sense_len;
900980691e5SKiyoshi Ueda 	}
901980691e5SKiyoshi Ueda 
902980691e5SKiyoshi Ueda 	free_rq_clone(clone);
903980691e5SKiyoshi Ueda 	blk_end_request_all(rq, error);
90429e4013dSTejun Heo 	rq_completed(md, rw, true);
905980691e5SKiyoshi Ueda }
906980691e5SKiyoshi Ueda 
907cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq)
908cec47e3dSKiyoshi Ueda {
909cec47e3dSKiyoshi Ueda 	struct request *clone = rq->special;
910cec47e3dSKiyoshi Ueda 
911cec47e3dSKiyoshi Ueda 	rq->special = NULL;
912cec47e3dSKiyoshi Ueda 	rq->cmd_flags &= ~REQ_DONTPREP;
913cec47e3dSKiyoshi Ueda 
914a77e28c7SKiyoshi Ueda 	free_rq_clone(clone);
915cec47e3dSKiyoshi Ueda }
916cec47e3dSKiyoshi Ueda 
917cec47e3dSKiyoshi Ueda /*
918cec47e3dSKiyoshi Ueda  * Requeue the original request of a clone.
919cec47e3dSKiyoshi Ueda  */
920cec47e3dSKiyoshi Ueda void dm_requeue_unmapped_request(struct request *clone)
921cec47e3dSKiyoshi Ueda {
922b4324feeSKiyoshi Ueda 	int rw = rq_data_dir(clone);
923cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
924cec47e3dSKiyoshi Ueda 	struct mapped_device *md = tio->md;
925cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
926cec47e3dSKiyoshi Ueda 	struct request_queue *q = rq->q;
927cec47e3dSKiyoshi Ueda 	unsigned long flags;
928cec47e3dSKiyoshi Ueda 
929cec47e3dSKiyoshi Ueda 	dm_unprep_request(rq);
930cec47e3dSKiyoshi Ueda 
931cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
932cec47e3dSKiyoshi Ueda 	blk_requeue_request(q, rq);
933cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
934cec47e3dSKiyoshi Ueda 
935b4324feeSKiyoshi Ueda 	rq_completed(md, rw, 0);
936cec47e3dSKiyoshi Ueda }
937cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
938cec47e3dSKiyoshi Ueda 
939cec47e3dSKiyoshi Ueda static void __stop_queue(struct request_queue *q)
940cec47e3dSKiyoshi Ueda {
941cec47e3dSKiyoshi Ueda 	blk_stop_queue(q);
942cec47e3dSKiyoshi Ueda }
943cec47e3dSKiyoshi Ueda 
944cec47e3dSKiyoshi Ueda static void stop_queue(struct request_queue *q)
945cec47e3dSKiyoshi Ueda {
946cec47e3dSKiyoshi Ueda 	unsigned long flags;
947cec47e3dSKiyoshi Ueda 
948cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
949cec47e3dSKiyoshi Ueda 	__stop_queue(q);
950cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
951cec47e3dSKiyoshi Ueda }
952cec47e3dSKiyoshi Ueda 
953cec47e3dSKiyoshi Ueda static void __start_queue(struct request_queue *q)
954cec47e3dSKiyoshi Ueda {
955cec47e3dSKiyoshi Ueda 	if (blk_queue_stopped(q))
956cec47e3dSKiyoshi Ueda 		blk_start_queue(q);
957cec47e3dSKiyoshi Ueda }
958cec47e3dSKiyoshi Ueda 
959cec47e3dSKiyoshi Ueda static void start_queue(struct request_queue *q)
960cec47e3dSKiyoshi Ueda {
961cec47e3dSKiyoshi Ueda 	unsigned long flags;
962cec47e3dSKiyoshi Ueda 
963cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
964cec47e3dSKiyoshi Ueda 	__start_queue(q);
965cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
966cec47e3dSKiyoshi Ueda }
967cec47e3dSKiyoshi Ueda 
96811a68244SKiyoshi Ueda static void dm_done(struct request *clone, int error, bool mapped)
96911a68244SKiyoshi Ueda {
97011a68244SKiyoshi Ueda 	int r = error;
97111a68244SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
972ba1cbad9SMike Snitzer 	dm_request_endio_fn rq_end_io = NULL;
973ba1cbad9SMike Snitzer 
974ba1cbad9SMike Snitzer 	if (tio->ti) {
975ba1cbad9SMike Snitzer 		rq_end_io = tio->ti->type->rq_end_io;
97611a68244SKiyoshi Ueda 
97711a68244SKiyoshi Ueda 		if (mapped && rq_end_io)
97811a68244SKiyoshi Ueda 			r = rq_end_io(tio->ti, clone, error, &tio->info);
979ba1cbad9SMike Snitzer 	}
98011a68244SKiyoshi Ueda 
98111a68244SKiyoshi Ueda 	if (r <= 0)
98211a68244SKiyoshi Ueda 		/* The target wants to complete the I/O */
98311a68244SKiyoshi Ueda 		dm_end_request(clone, r);
98411a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_INCOMPLETE)
98511a68244SKiyoshi Ueda 		/* The target will handle the I/O */
98611a68244SKiyoshi Ueda 		return;
98711a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_REQUEUE)
98811a68244SKiyoshi Ueda 		/* The target wants to requeue the I/O */
98911a68244SKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
99011a68244SKiyoshi Ueda 	else {
99111a68244SKiyoshi Ueda 		DMWARN("unimplemented target endio return value: %d", r);
99211a68244SKiyoshi Ueda 		BUG();
99311a68244SKiyoshi Ueda 	}
99411a68244SKiyoshi Ueda }
99511a68244SKiyoshi Ueda 
996cec47e3dSKiyoshi Ueda /*
997cec47e3dSKiyoshi Ueda  * Request completion handler for request-based dm
998cec47e3dSKiyoshi Ueda  */
999cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq)
1000cec47e3dSKiyoshi Ueda {
100111a68244SKiyoshi Ueda 	bool mapped = true;
1002cec47e3dSKiyoshi Ueda 	struct request *clone = rq->completion_data;
1003cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1004cec47e3dSKiyoshi Ueda 
100511a68244SKiyoshi Ueda 	if (rq->cmd_flags & REQ_FAILED)
100611a68244SKiyoshi Ueda 		mapped = false;
1007cec47e3dSKiyoshi Ueda 
100811a68244SKiyoshi Ueda 	dm_done(clone, tio->error, mapped);
1009cec47e3dSKiyoshi Ueda }
1010cec47e3dSKiyoshi Ueda 
1011cec47e3dSKiyoshi Ueda /*
1012cec47e3dSKiyoshi Ueda  * Complete the clone and the original request with the error status
1013cec47e3dSKiyoshi Ueda  * through softirq context.
1014cec47e3dSKiyoshi Ueda  */
1015cec47e3dSKiyoshi Ueda static void dm_complete_request(struct request *clone, int error)
1016cec47e3dSKiyoshi Ueda {
1017cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1018cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
1019cec47e3dSKiyoshi Ueda 
1020cec47e3dSKiyoshi Ueda 	tio->error = error;
1021cec47e3dSKiyoshi Ueda 	rq->completion_data = clone;
1022cec47e3dSKiyoshi Ueda 	blk_complete_request(rq);
1023cec47e3dSKiyoshi Ueda }
1024cec47e3dSKiyoshi Ueda 
1025cec47e3dSKiyoshi Ueda /*
1026cec47e3dSKiyoshi Ueda  * Complete the not-mapped clone and the original request with the error status
1027cec47e3dSKiyoshi Ueda  * through softirq context.
1028cec47e3dSKiyoshi Ueda  * Target's rq_end_io() function isn't called.
1029cec47e3dSKiyoshi Ueda  * This may be used when the target's map_rq() function fails.
1030cec47e3dSKiyoshi Ueda  */
1031cec47e3dSKiyoshi Ueda void dm_kill_unmapped_request(struct request *clone, int error)
1032cec47e3dSKiyoshi Ueda {
1033cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1034cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
1035cec47e3dSKiyoshi Ueda 
1036cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_FAILED;
1037cec47e3dSKiyoshi Ueda 	dm_complete_request(clone, error);
1038cec47e3dSKiyoshi Ueda }
1039cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
1040cec47e3dSKiyoshi Ueda 
1041cec47e3dSKiyoshi Ueda /*
1042cec47e3dSKiyoshi Ueda  * Called with the queue lock held
1043cec47e3dSKiyoshi Ueda  */
1044cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error)
1045cec47e3dSKiyoshi Ueda {
1046cec47e3dSKiyoshi Ueda 	/*
1047cec47e3dSKiyoshi Ueda 	 * For just cleaning up the information of the queue in which
1048cec47e3dSKiyoshi Ueda 	 * the clone was dispatched.
1049cec47e3dSKiyoshi Ueda 	 * The clone is *NOT* freed actually here because it is alloced from
1050cec47e3dSKiyoshi Ueda 	 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
1051cec47e3dSKiyoshi Ueda 	 */
1052cec47e3dSKiyoshi Ueda 	__blk_put_request(clone->q, clone);
1053cec47e3dSKiyoshi Ueda 
1054cec47e3dSKiyoshi Ueda 	/*
1055cec47e3dSKiyoshi Ueda 	 * Actual request completion is done in a softirq context which doesn't
1056cec47e3dSKiyoshi Ueda 	 * hold the queue lock.  Otherwise, deadlock could occur because:
1057cec47e3dSKiyoshi Ueda 	 *     - another request may be submitted by the upper level driver
1058cec47e3dSKiyoshi Ueda 	 *       of the stacking during the completion
1059cec47e3dSKiyoshi Ueda 	 *     - the submission which requires queue lock may be done
1060cec47e3dSKiyoshi Ueda 	 *       against this queue
1061cec47e3dSKiyoshi Ueda 	 */
1062cec47e3dSKiyoshi Ueda 	dm_complete_request(clone, error);
1063cec47e3dSKiyoshi Ueda }
1064cec47e3dSKiyoshi Ueda 
106556a67df7SMike Snitzer /*
106656a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
106756a67df7SMike Snitzer  * target boundary.
106856a67df7SMike Snitzer  */
106956a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
10701da177e4SLinus Torvalds {
107156a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
107256a67df7SMike Snitzer 
107356a67df7SMike Snitzer 	return ti->len - target_offset;
107456a67df7SMike Snitzer }
107556a67df7SMike Snitzer 
107656a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
107756a67df7SMike Snitzer {
107856a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
1079542f9038SMike Snitzer 	sector_t offset, max_len;
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds 	/*
10821da177e4SLinus Torvalds 	 * Does the target need to split even further?
10831da177e4SLinus Torvalds 	 */
1084542f9038SMike Snitzer 	if (ti->max_io_len) {
1085542f9038SMike Snitzer 		offset = dm_target_offset(ti, sector);
1086542f9038SMike Snitzer 		if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1087542f9038SMike Snitzer 			max_len = sector_div(offset, ti->max_io_len);
1088542f9038SMike Snitzer 		else
1089542f9038SMike Snitzer 			max_len = offset & (ti->max_io_len - 1);
1090542f9038SMike Snitzer 		max_len = ti->max_io_len - max_len;
1091542f9038SMike Snitzer 
1092542f9038SMike Snitzer 		if (len > max_len)
1093542f9038SMike Snitzer 			len = max_len;
10941da177e4SLinus Torvalds 	}
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 	return len;
10971da177e4SLinus Torvalds }
10981da177e4SLinus Torvalds 
1099542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1100542f9038SMike Snitzer {
1101542f9038SMike Snitzer 	if (len > UINT_MAX) {
1102542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1103542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
1104542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
1105542f9038SMike Snitzer 		return -EINVAL;
1106542f9038SMike Snitzer 	}
1107542f9038SMike Snitzer 
1108542f9038SMike Snitzer 	ti->max_io_len = (uint32_t) len;
1109542f9038SMike Snitzer 
1110542f9038SMike Snitzer 	return 0;
1111542f9038SMike Snitzer }
1112542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1113542f9038SMike Snitzer 
1114bd2a49b8SAlasdair G Kergon static void __map_bio(struct dm_target_io *tio)
11151da177e4SLinus Torvalds {
11161da177e4SLinus Torvalds 	int r;
11172056a782SJens Axboe 	sector_t sector;
11189faf400fSStefan Bader 	struct mapped_device *md;
1119dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
1120bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
11231da177e4SLinus Torvalds 	clone->bi_private = tio;
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 	/*
11261da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
11271da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
11281da177e4SLinus Torvalds 	 * this io.
11291da177e4SLinus Torvalds 	 */
11301da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
11312056a782SJens Axboe 	sector = clone->bi_sector;
11327de3ee57SMikulas Patocka 	r = ti->type->map(ti, clone);
113345cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
11341da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
11352056a782SJens Axboe 
1136d07335e5SMike Snitzer 		trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
113722a7c31aSAlan D. Brunelle 				      tio->io->bio->bi_bdev->bd_dev, sector);
11382056a782SJens Axboe 
11391da177e4SLinus Torvalds 		generic_make_request(clone);
11402e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
11412e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
11429faf400fSStefan Bader 		md = tio->io->md;
11439faf400fSStefan Bader 		dec_pending(tio->io, r);
11449faf400fSStefan Bader 		free_tio(md, tio);
114545cbcd79SKiyoshi Ueda 	} else if (r) {
114645cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
114745cbcd79SKiyoshi Ueda 		BUG();
11481da177e4SLinus Torvalds 	}
11491da177e4SLinus Torvalds }
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds struct clone_info {
11521da177e4SLinus Torvalds 	struct mapped_device *md;
11531da177e4SLinus Torvalds 	struct dm_table *map;
11541da177e4SLinus Torvalds 	struct bio *bio;
11551da177e4SLinus Torvalds 	struct dm_io *io;
11561da177e4SLinus Torvalds 	sector_t sector;
11571da177e4SLinus Torvalds 	sector_t sector_count;
11581da177e4SLinus Torvalds 	unsigned short idx;
11591da177e4SLinus Torvalds };
11601da177e4SLinus Torvalds 
1161bd2a49b8SAlasdair G Kergon static void bio_setup_sector(struct bio *bio, sector_t sector, sector_t len)
1162bd2a49b8SAlasdair G Kergon {
1163bd2a49b8SAlasdair G Kergon 	bio->bi_sector = sector;
1164bd2a49b8SAlasdair G Kergon 	bio->bi_size = to_bytes(len);
1165bd2a49b8SAlasdair G Kergon }
1166bd2a49b8SAlasdair G Kergon 
1167bd2a49b8SAlasdair G Kergon static void bio_setup_bv(struct bio *bio, unsigned short idx, unsigned short bv_count)
1168bd2a49b8SAlasdair G Kergon {
1169bd2a49b8SAlasdair G Kergon 	bio->bi_idx = idx;
1170bd2a49b8SAlasdair G Kergon 	bio->bi_vcnt = idx + bv_count;
1171bd2a49b8SAlasdair G Kergon 	bio->bi_flags &= ~(1 << BIO_SEG_VALID);
1172bd2a49b8SAlasdair G Kergon }
1173bd2a49b8SAlasdair G Kergon 
1174bd2a49b8SAlasdair G Kergon static void clone_bio_integrity(struct bio *bio, struct bio *clone,
1175bd2a49b8SAlasdair G Kergon 				unsigned short idx, unsigned len, unsigned offset,
1176bd2a49b8SAlasdair G Kergon 				unsigned trim)
1177bd2a49b8SAlasdair G Kergon {
1178bd2a49b8SAlasdair G Kergon 	if (!bio_integrity(bio))
1179bd2a49b8SAlasdair G Kergon 		return;
1180bd2a49b8SAlasdair G Kergon 
1181bd2a49b8SAlasdair G Kergon 	bio_integrity_clone(clone, bio, GFP_NOIO);
1182bd2a49b8SAlasdair G Kergon 
1183bd2a49b8SAlasdair G Kergon 	if (trim)
1184bd2a49b8SAlasdair G Kergon 		bio_integrity_trim(clone, bio_sector_offset(bio, idx, offset), len);
1185bd2a49b8SAlasdair G Kergon }
1186bd2a49b8SAlasdair G Kergon 
11871da177e4SLinus Torvalds /*
1188d87f4c14STejun Heo  * Creates a little bio that just does part of a bvec.
11891da177e4SLinus Torvalds  */
119014fe594dSAlasdair G Kergon static void clone_split_bio(struct dm_target_io *tio, struct bio *bio,
119114fe594dSAlasdair G Kergon 			    sector_t sector, unsigned short idx,
119214fe594dSAlasdair G Kergon 			    unsigned offset, unsigned len)
11931da177e4SLinus Torvalds {
1194dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
11951da177e4SLinus Torvalds 	struct bio_vec *bv = bio->bi_io_vec + idx;
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 	*clone->bi_io_vec = *bv;
11981da177e4SLinus Torvalds 
1199bd2a49b8SAlasdair G Kergon 	bio_setup_sector(clone, sector, len);
1200bd2a49b8SAlasdair G Kergon 
12011da177e4SLinus Torvalds 	clone->bi_bdev = bio->bi_bdev;
1202d87f4c14STejun Heo 	clone->bi_rw = bio->bi_rw;
12031da177e4SLinus Torvalds 	clone->bi_vcnt = 1;
12041da177e4SLinus Torvalds 	clone->bi_io_vec->bv_offset = offset;
12051da177e4SLinus Torvalds 	clone->bi_io_vec->bv_len = clone->bi_size;
1206f3e1d26eSMartin K. Petersen 	clone->bi_flags |= 1 << BIO_CLONED;
12071da177e4SLinus Torvalds 
1208bd2a49b8SAlasdair G Kergon 	clone_bio_integrity(bio, clone, idx, len, offset, 1);
12091da177e4SLinus Torvalds }
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds /*
12121da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
12131da177e4SLinus Torvalds  */
1214dba14160SMikulas Patocka static void clone_bio(struct dm_target_io *tio, struct bio *bio,
1215dba14160SMikulas Patocka 		      sector_t sector, unsigned short idx,
1216e4c93811SAlasdair G Kergon 		      unsigned short bv_count, unsigned len)
12171da177e4SLinus Torvalds {
1218dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
1219bd2a49b8SAlasdair G Kergon 	unsigned trim = 0;
12201da177e4SLinus Torvalds 
12219faf400fSStefan Bader 	__bio_clone(clone, bio);
1222bd2a49b8SAlasdair G Kergon 	bio_setup_sector(clone, sector, len);
1223bd2a49b8SAlasdair G Kergon 	bio_setup_bv(clone, idx, bv_count);
12249c47008dSMartin K. Petersen 
12259c47008dSMartin K. Petersen 	if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1226bd2a49b8SAlasdair G Kergon 		trim = 1;
1227bd2a49b8SAlasdair G Kergon 	clone_bio_integrity(bio, clone, idx, len, 0, trim);
12281da177e4SLinus Torvalds }
12291da177e4SLinus Torvalds 
12309015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci,
1231bd2a49b8SAlasdair G Kergon 				      struct dm_target *ti, int nr_iovecs,
123255a62eefSAlasdair G Kergon 				      unsigned target_bio_nr)
1233f9ab94ceSMikulas Patocka {
1234dba14160SMikulas Patocka 	struct dm_target_io *tio;
1235dba14160SMikulas Patocka 	struct bio *clone;
1236dba14160SMikulas Patocka 
1237dba14160SMikulas Patocka 	clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, ci->md->bs);
1238dba14160SMikulas Patocka 	tio = container_of(clone, struct dm_target_io, clone);
1239f9ab94ceSMikulas Patocka 
1240f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1241f9ab94ceSMikulas Patocka 	tio->ti = ti;
1242f9ab94ceSMikulas Patocka 	memset(&tio->info, 0, sizeof(tio->info));
124355a62eefSAlasdair G Kergon 	tio->target_bio_nr = target_bio_nr;
12449015df24SAlasdair G Kergon 
12459015df24SAlasdair G Kergon 	return tio;
12469015df24SAlasdair G Kergon }
12479015df24SAlasdair G Kergon 
124814fe594dSAlasdair G Kergon static void __clone_and_map_simple_bio(struct clone_info *ci,
124914fe594dSAlasdair G Kergon 				       struct dm_target *ti,
125055a62eefSAlasdair G Kergon 				       unsigned target_bio_nr, sector_t len)
12519015df24SAlasdair G Kergon {
125255a62eefSAlasdair G Kergon 	struct dm_target_io *tio = alloc_tio(ci, ti, ci->bio->bi_max_vecs, target_bio_nr);
1253dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
12549015df24SAlasdair G Kergon 
125506a426ceSMike Snitzer 	/*
125606a426ceSMike Snitzer 	 * Discard requests require the bio's inline iovecs be initialized.
125706a426ceSMike Snitzer 	 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
125806a426ceSMike Snitzer 	 * and discard, so no need for concern about wasted bvec allocations.
125906a426ceSMike Snitzer 	 */
1260dba14160SMikulas Patocka 	 __bio_clone(clone, ci->bio);
1261bd2a49b8SAlasdair G Kergon 	if (len)
1262bd2a49b8SAlasdair G Kergon 		bio_setup_sector(clone, ci->sector, len);
1263f9ab94ceSMikulas Patocka 
1264bd2a49b8SAlasdair G Kergon 	__map_bio(tio);
1265f9ab94ceSMikulas Patocka }
1266f9ab94ceSMikulas Patocka 
126714fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
126855a62eefSAlasdair G Kergon 				  unsigned num_bios, sector_t len)
126906a426ceSMike Snitzer {
127055a62eefSAlasdair G Kergon 	unsigned target_bio_nr;
127106a426ceSMike Snitzer 
127255a62eefSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
127314fe594dSAlasdair G Kergon 		__clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
127406a426ceSMike Snitzer }
127506a426ceSMike Snitzer 
127614fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1277f9ab94ceSMikulas Patocka {
127806a426ceSMike Snitzer 	unsigned target_nr = 0;
1279f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1280f9ab94ceSMikulas Patocka 
1281b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1282f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
128314fe594dSAlasdair G Kergon 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, 0);
1284f9ab94ceSMikulas Patocka 
1285f9ab94ceSMikulas Patocka 	return 0;
1286f9ab94ceSMikulas Patocka }
1287f9ab94ceSMikulas Patocka 
1288e4c93811SAlasdair G Kergon static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
1289e4c93811SAlasdair G Kergon 				     sector_t sector, int nr_iovecs,
1290e4c93811SAlasdair G Kergon 				     unsigned short idx, unsigned short bv_count,
1291e4c93811SAlasdair G Kergon 				     unsigned offset, unsigned len,
1292e4c93811SAlasdair G Kergon 				     unsigned split_bvec)
12935ae89a87SMike Snitzer {
1294dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
12955ae89a87SMike Snitzer 	struct dm_target_io *tio;
1296b0d8ed4dSAlasdair G Kergon 	unsigned target_bio_nr;
1297b0d8ed4dSAlasdair G Kergon 	unsigned num_target_bios = 1;
12985ae89a87SMike Snitzer 
1299b0d8ed4dSAlasdair G Kergon 	/*
1300b0d8ed4dSAlasdair G Kergon 	 * Does the target want to receive duplicate copies of the bio?
1301b0d8ed4dSAlasdair G Kergon 	 */
1302b0d8ed4dSAlasdair G Kergon 	if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1303b0d8ed4dSAlasdair G Kergon 		num_target_bios = ti->num_write_bios(ti, bio);
1304e4c93811SAlasdair G Kergon 
1305b0d8ed4dSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
1306b0d8ed4dSAlasdair G Kergon 		tio = alloc_tio(ci, ti, nr_iovecs, target_bio_nr);
1307e4c93811SAlasdair G Kergon 		if (split_bvec)
1308e4c93811SAlasdair G Kergon 			clone_split_bio(tio, bio, sector, idx, offset, len);
1309e4c93811SAlasdair G Kergon 		else
1310e4c93811SAlasdair G Kergon 			clone_bio(tio, bio, sector, idx, bv_count, len);
1311bd2a49b8SAlasdair G Kergon 		__map_bio(tio);
13125ae89a87SMike Snitzer 	}
1313b0d8ed4dSAlasdair G Kergon }
13145ae89a87SMike Snitzer 
131555a62eefSAlasdair G Kergon typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
131623508a96SMike Snitzer 
131755a62eefSAlasdair G Kergon static unsigned get_num_discard_bios(struct dm_target *ti)
131823508a96SMike Snitzer {
131955a62eefSAlasdair G Kergon 	return ti->num_discard_bios;
132023508a96SMike Snitzer }
132123508a96SMike Snitzer 
132255a62eefSAlasdair G Kergon static unsigned get_num_write_same_bios(struct dm_target *ti)
132323508a96SMike Snitzer {
132455a62eefSAlasdair G Kergon 	return ti->num_write_same_bios;
132523508a96SMike Snitzer }
132623508a96SMike Snitzer 
132723508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti);
132823508a96SMike Snitzer 
132923508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti)
133023508a96SMike Snitzer {
133155a62eefSAlasdair G Kergon 	return ti->split_discard_bios;
133223508a96SMike Snitzer }
133323508a96SMike Snitzer 
133414fe594dSAlasdair G Kergon static int __send_changing_extent_only(struct clone_info *ci,
133555a62eefSAlasdair G Kergon 				       get_num_bios_fn get_num_bios,
133623508a96SMike Snitzer 				       is_split_required_fn is_split_required)
13375ae89a87SMike Snitzer {
13385ae89a87SMike Snitzer 	struct dm_target *ti;
1339a79245b3SMike Snitzer 	sector_t len;
134055a62eefSAlasdair G Kergon 	unsigned num_bios;
13415ae89a87SMike Snitzer 
1342a79245b3SMike Snitzer 	do {
13435ae89a87SMike Snitzer 		ti = dm_table_find_target(ci->map, ci->sector);
13445ae89a87SMike Snitzer 		if (!dm_target_is_valid(ti))
13455ae89a87SMike Snitzer 			return -EIO;
13465ae89a87SMike Snitzer 
13475ae89a87SMike Snitzer 		/*
134823508a96SMike Snitzer 		 * Even though the device advertised support for this type of
134923508a96SMike Snitzer 		 * request, that does not mean every target supports it, and
1350936688d7SMike Snitzer 		 * reconfiguration might also have changed that since the
13515ae89a87SMike Snitzer 		 * check was performed.
13525ae89a87SMike Snitzer 		 */
135355a62eefSAlasdair G Kergon 		num_bios = get_num_bios ? get_num_bios(ti) : 0;
135455a62eefSAlasdair G Kergon 		if (!num_bios)
13555ae89a87SMike Snitzer 			return -EOPNOTSUPP;
13565ae89a87SMike Snitzer 
135723508a96SMike Snitzer 		if (is_split_required && !is_split_required(ti))
1358a79245b3SMike Snitzer 			len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
13597acf0277SMikulas Patocka 		else
13607acf0277SMikulas Patocka 			len = min(ci->sector_count, max_io_len(ci->sector, ti));
13615ae89a87SMike Snitzer 
136214fe594dSAlasdair G Kergon 		__send_duplicate_bios(ci, ti, num_bios, len);
13635ae89a87SMike Snitzer 
1364a79245b3SMike Snitzer 		ci->sector += len;
1365a79245b3SMike Snitzer 	} while (ci->sector_count -= len);
13665ae89a87SMike Snitzer 
13675ae89a87SMike Snitzer 	return 0;
13685ae89a87SMike Snitzer }
13695ae89a87SMike Snitzer 
137014fe594dSAlasdair G Kergon static int __send_discard(struct clone_info *ci)
137123508a96SMike Snitzer {
137214fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_discard_bios,
137323508a96SMike Snitzer 					   is_split_required_for_discard);
137423508a96SMike Snitzer }
137523508a96SMike Snitzer 
137614fe594dSAlasdair G Kergon static int __send_write_same(struct clone_info *ci)
137723508a96SMike Snitzer {
137814fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
137923508a96SMike Snitzer }
138023508a96SMike Snitzer 
1381e4c93811SAlasdair G Kergon /*
1382e4c93811SAlasdair G Kergon  * Find maximum number of sectors / bvecs we can process with a single bio.
1383e4c93811SAlasdair G Kergon  */
1384e4c93811SAlasdair G Kergon static sector_t __len_within_target(struct clone_info *ci, sector_t max, int *idx)
13851da177e4SLinus Torvalds {
1386dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
1387e4c93811SAlasdair G Kergon 	sector_t bv_len, total_len = 0;
13881da177e4SLinus Torvalds 
1389e4c93811SAlasdair G Kergon 	for (*idx = ci->idx; max && (*idx < bio->bi_vcnt); (*idx)++) {
1390e4c93811SAlasdair G Kergon 		bv_len = to_sector(bio->bi_io_vec[*idx].bv_len);
13915ae89a87SMike Snitzer 
1392e4c93811SAlasdair G Kergon 		if (bv_len > max)
13931da177e4SLinus Torvalds 			break;
13941da177e4SLinus Torvalds 
1395e4c93811SAlasdair G Kergon 		max -= bv_len;
1396e4c93811SAlasdair G Kergon 		total_len += bv_len;
13971da177e4SLinus Torvalds 	}
13981da177e4SLinus Torvalds 
1399e4c93811SAlasdair G Kergon 	return total_len;
1400e4c93811SAlasdair G Kergon }
14011da177e4SLinus Torvalds 
1402e4c93811SAlasdair G Kergon static int __split_bvec_across_targets(struct clone_info *ci,
1403e4c93811SAlasdair G Kergon 				       struct dm_target *ti, sector_t max)
1404e4c93811SAlasdair G Kergon {
1405e4c93811SAlasdair G Kergon 	struct bio *bio = ci->bio;
14061da177e4SLinus Torvalds 	struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1407d2044a94SAlasdair G Kergon 	sector_t remaining = to_sector(bv->bv_len);
1408e4c93811SAlasdair G Kergon 	unsigned offset = 0;
1409e4c93811SAlasdair G Kergon 	sector_t len;
14101da177e4SLinus Torvalds 
1411d2044a94SAlasdair G Kergon 	do {
1412d2044a94SAlasdair G Kergon 		if (offset) {
14131da177e4SLinus Torvalds 			ti = dm_table_find_target(ci->map, ci->sector);
1414512875bdSJun'ichi Nomura 			if (!dm_target_is_valid(ti))
1415512875bdSJun'ichi Nomura 				return -EIO;
1416512875bdSJun'ichi Nomura 
141756a67df7SMike Snitzer 			max = max_io_len(ci->sector, ti);
1418d2044a94SAlasdair G Kergon 		}
1419d2044a94SAlasdair G Kergon 
1420d2044a94SAlasdair G Kergon 		len = min(remaining, max);
1421d2044a94SAlasdair G Kergon 
1422e4c93811SAlasdair G Kergon 		__clone_and_map_data_bio(ci, ti, ci->sector, 1, ci->idx, 0,
1423e4c93811SAlasdair G Kergon 					 bv->bv_offset + offset, len, 1);
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds 		ci->sector += len;
14261da177e4SLinus Torvalds 		ci->sector_count -= len;
1427d2044a94SAlasdair G Kergon 		offset += to_bytes(len);
1428d2044a94SAlasdair G Kergon 	} while (remaining -= len);
1429d2044a94SAlasdair G Kergon 
14301da177e4SLinus Torvalds 	ci->idx++;
1431512875bdSJun'ichi Nomura 
1432512875bdSJun'ichi Nomura 	return 0;
14331da177e4SLinus Torvalds }
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds /*
1436e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1437e4c93811SAlasdair G Kergon  */
1438e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci)
1439e4c93811SAlasdair G Kergon {
1440e4c93811SAlasdair G Kergon 	struct bio *bio = ci->bio;
1441e4c93811SAlasdair G Kergon 	struct dm_target *ti;
1442e4c93811SAlasdair G Kergon 	sector_t len, max;
1443e4c93811SAlasdair G Kergon 	int idx;
1444e4c93811SAlasdair G Kergon 
1445e4c93811SAlasdair G Kergon 	if (unlikely(bio->bi_rw & REQ_DISCARD))
1446e4c93811SAlasdair G Kergon 		return __send_discard(ci);
1447e4c93811SAlasdair G Kergon 	else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
1448e4c93811SAlasdair G Kergon 		return __send_write_same(ci);
1449e4c93811SAlasdair G Kergon 
1450e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1451e4c93811SAlasdair G Kergon 	if (!dm_target_is_valid(ti))
1452e4c93811SAlasdair G Kergon 		return -EIO;
1453e4c93811SAlasdair G Kergon 
1454e4c93811SAlasdair G Kergon 	max = max_io_len(ci->sector, ti);
1455e4c93811SAlasdair G Kergon 
1456e4c93811SAlasdair G Kergon 	/*
1457e4c93811SAlasdair G Kergon 	 * Optimise for the simple case where we can do all of
1458e4c93811SAlasdair G Kergon 	 * the remaining io with a single clone.
1459e4c93811SAlasdair G Kergon 	 */
1460e4c93811SAlasdair G Kergon 	if (ci->sector_count <= max) {
1461e4c93811SAlasdair G Kergon 		__clone_and_map_data_bio(ci, ti, ci->sector, bio->bi_max_vecs,
1462e4c93811SAlasdair G Kergon 					 ci->idx, bio->bi_vcnt - ci->idx, 0,
1463e4c93811SAlasdair G Kergon 					 ci->sector_count, 0);
1464e4c93811SAlasdair G Kergon 		ci->sector_count = 0;
1465e4c93811SAlasdair G Kergon 		return 0;
1466e4c93811SAlasdair G Kergon 	}
1467e4c93811SAlasdair G Kergon 
1468e4c93811SAlasdair G Kergon 	/*
1469e4c93811SAlasdair G Kergon 	 * There are some bvecs that don't span targets.
1470e4c93811SAlasdair G Kergon 	 * Do as many of these as possible.
1471e4c93811SAlasdair G Kergon 	 */
1472e4c93811SAlasdair G Kergon 	if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1473e4c93811SAlasdair G Kergon 		len = __len_within_target(ci, max, &idx);
1474e4c93811SAlasdair G Kergon 
1475e4c93811SAlasdair G Kergon 		__clone_and_map_data_bio(ci, ti, ci->sector, bio->bi_max_vecs,
1476e4c93811SAlasdair G Kergon 					 ci->idx, idx - ci->idx, 0, len, 0);
1477e4c93811SAlasdair G Kergon 
1478e4c93811SAlasdair G Kergon 		ci->sector += len;
1479e4c93811SAlasdair G Kergon 		ci->sector_count -= len;
1480e4c93811SAlasdair G Kergon 		ci->idx = idx;
1481e4c93811SAlasdair G Kergon 
1482e4c93811SAlasdair G Kergon 		return 0;
1483e4c93811SAlasdair G Kergon 	}
1484e4c93811SAlasdair G Kergon 
1485e4c93811SAlasdair G Kergon 	/*
1486e4c93811SAlasdair G Kergon 	 * Handle a bvec that must be split between two or more targets.
1487e4c93811SAlasdair G Kergon 	 */
1488e4c93811SAlasdair G Kergon 	return __split_bvec_across_targets(ci, ti, max);
1489e4c93811SAlasdair G Kergon }
1490e4c93811SAlasdair G Kergon 
1491e4c93811SAlasdair G Kergon /*
149214fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
14931da177e4SLinus Torvalds  */
149483d5e5b0SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md,
149583d5e5b0SMikulas Patocka 				    struct dm_table *map, struct bio *bio)
14961da177e4SLinus Torvalds {
14971da177e4SLinus Torvalds 	struct clone_info ci;
1498512875bdSJun'ichi Nomura 	int error = 0;
14991da177e4SLinus Torvalds 
150083d5e5b0SMikulas Patocka 	if (unlikely(!map)) {
1501f0b9a450SMikulas Patocka 		bio_io_error(bio);
1502f0b9a450SMikulas Patocka 		return;
1503f0b9a450SMikulas Patocka 	}
1504692d0eb9SMikulas Patocka 
150583d5e5b0SMikulas Patocka 	ci.map = map;
15061da177e4SLinus Torvalds 	ci.md = md;
15071da177e4SLinus Torvalds 	ci.io = alloc_io(md);
15081da177e4SLinus Torvalds 	ci.io->error = 0;
15091da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
15101da177e4SLinus Torvalds 	ci.io->bio = bio;
15111da177e4SLinus Torvalds 	ci.io->md = md;
1512f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
15131da177e4SLinus Torvalds 	ci.sector = bio->bi_sector;
15141da177e4SLinus Torvalds 	ci.idx = bio->bi_idx;
15151da177e4SLinus Torvalds 
15163eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1517bd2a49b8SAlasdair G Kergon 
1518b372d360SMike Snitzer 	if (bio->bi_rw & REQ_FLUSH) {
1519b372d360SMike Snitzer 		ci.bio = &ci.md->flush_bio;
1520b372d360SMike Snitzer 		ci.sector_count = 0;
152114fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1522b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1523b372d360SMike Snitzer 	} else {
15246a8736d1STejun Heo 		ci.bio = bio;
1525f6fccb12SMilan Broz 		ci.sector_count = bio_sectors(bio);
1526512875bdSJun'ichi Nomura 		while (ci.sector_count && !error)
152714fe594dSAlasdair G Kergon 			error = __split_and_process_non_flush(&ci);
1528d87f4c14STejun Heo 	}
15291da177e4SLinus Torvalds 
15301da177e4SLinus Torvalds 	/* drop the extra reference count */
1531512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
15329e4e5f87SMilan Broz }
15339e4e5f87SMilan Broz /*-----------------------------------------------------------------
15341da177e4SLinus Torvalds  * CRUD END
15351da177e4SLinus Torvalds  *---------------------------------------------------------------*/
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds static int dm_merge_bvec(struct request_queue *q,
15381da177e4SLinus Torvalds 			 struct bvec_merge_data *bvm,
1539f6fccb12SMilan Broz 			 struct bio_vec *biovec)
1540f6fccb12SMilan Broz {
1541f6fccb12SMilan Broz 	struct mapped_device *md = q->queuedata;
154283d5e5b0SMikulas Patocka 	struct dm_table *map = dm_get_live_table_fast(md);
1543f6fccb12SMilan Broz 	struct dm_target *ti;
1544f6fccb12SMilan Broz 	sector_t max_sectors;
1545f6fccb12SMilan Broz 	int max_size = 0;
1546f6fccb12SMilan Broz 
1547f6fccb12SMilan Broz 	if (unlikely(!map))
1548f6fccb12SMilan Broz 		goto out;
1549f6fccb12SMilan Broz 
1550f6fccb12SMilan Broz 	ti = dm_table_find_target(map, bvm->bi_sector);
1551f6fccb12SMilan Broz 	if (!dm_target_is_valid(ti))
155283d5e5b0SMikulas Patocka 		goto out;
1553f6fccb12SMilan Broz 
1554f6fccb12SMilan Broz 	/*
1555f6fccb12SMilan Broz 	 * Find maximum amount of I/O that won't need splitting
1556f6fccb12SMilan Broz 	 */
155756a67df7SMike Snitzer 	max_sectors = min(max_io_len(bvm->bi_sector, ti),
1558f6fccb12SMilan Broz 			  (sector_t) BIO_MAX_SECTORS);
1559f6fccb12SMilan Broz 	max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1560f6fccb12SMilan Broz 	if (max_size < 0)
1561f6fccb12SMilan Broz 		max_size = 0;
1562f6fccb12SMilan Broz 
1563f6fccb12SMilan Broz 	/*
1564f6fccb12SMilan Broz 	 * merge_bvec_fn() returns number of bytes
1565f6fccb12SMilan Broz 	 * it can accept at this offset
1566f6fccb12SMilan Broz 	 * max is precomputed maximal io size
1567f6fccb12SMilan Broz 	 */
1568f6fccb12SMilan Broz 	if (max_size && ti->type->merge)
1569f6fccb12SMilan Broz 		max_size = ti->type->merge(ti, bvm, biovec, max_size);
15708cbeb67aSMikulas Patocka 	/*
15718cbeb67aSMikulas Patocka 	 * If the target doesn't support merge method and some of the devices
15728cbeb67aSMikulas Patocka 	 * provided their merge_bvec method (we know this by looking at
15738cbeb67aSMikulas Patocka 	 * queue_max_hw_sectors), then we can't allow bios with multiple vector
15748cbeb67aSMikulas Patocka 	 * entries.  So always set max_size to 0, and the code below allows
15758cbeb67aSMikulas Patocka 	 * just one page.
15768cbeb67aSMikulas Patocka 	 */
15778cbeb67aSMikulas Patocka 	else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
15788cbeb67aSMikulas Patocka 
15798cbeb67aSMikulas Patocka 		max_size = 0;
1580f6fccb12SMilan Broz 
15815037108aSMikulas Patocka out:
158283d5e5b0SMikulas Patocka 	dm_put_live_table_fast(md);
1583f6fccb12SMilan Broz 	/*
1584f6fccb12SMilan Broz 	 * Always allow an entire first page
1585f6fccb12SMilan Broz 	 */
1586f6fccb12SMilan Broz 	if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1587f6fccb12SMilan Broz 		max_size = biovec->bv_len;
1588f6fccb12SMilan Broz 
1589f6fccb12SMilan Broz 	return max_size;
1590f6fccb12SMilan Broz }
1591f6fccb12SMilan Broz 
15921da177e4SLinus Torvalds /*
15931da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
15941da177e4SLinus Torvalds  * dm_merge_bvec.
15951da177e4SLinus Torvalds  */
15965a7bbad2SChristoph Hellwig static void _dm_request(struct request_queue *q, struct bio *bio)
15971da177e4SLinus Torvalds {
159812f03a49SKevin Corry 	int rw = bio_data_dir(bio);
15991da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
1600c9959059STejun Heo 	int cpu;
160183d5e5b0SMikulas Patocka 	int srcu_idx;
160283d5e5b0SMikulas Patocka 	struct dm_table *map;
16031da177e4SLinus Torvalds 
160483d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
16051da177e4SLinus Torvalds 
1606074a7acaSTejun Heo 	cpu = part_stat_lock();
1607074a7acaSTejun Heo 	part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1608074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1609074a7acaSTejun Heo 	part_stat_unlock();
161012f03a49SKevin Corry 
16116a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
16126a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
161383d5e5b0SMikulas Patocka 		dm_put_live_table(md, srcu_idx);
16141da177e4SLinus Torvalds 
16156a8736d1STejun Heo 		if (bio_rw(bio) != READA)
161692c63902SMikulas Patocka 			queue_io(md, bio);
16176a8736d1STejun Heo 		else
16186a8736d1STejun Heo 			bio_io_error(bio);
16195a7bbad2SChristoph Hellwig 		return;
16201da177e4SLinus Torvalds 	}
16211da177e4SLinus Torvalds 
162283d5e5b0SMikulas Patocka 	__split_and_process_bio(md, map, bio);
162383d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
16245a7bbad2SChristoph Hellwig 	return;
1625cec47e3dSKiyoshi Ueda }
1626cec47e3dSKiyoshi Ueda 
1627fd2ed4d2SMikulas Patocka int dm_request_based(struct mapped_device *md)
1628cec47e3dSKiyoshi Ueda {
1629cec47e3dSKiyoshi Ueda 	return blk_queue_stackable(md->queue);
1630cec47e3dSKiyoshi Ueda }
1631cec47e3dSKiyoshi Ueda 
16325a7bbad2SChristoph Hellwig static void dm_request(struct request_queue *q, struct bio *bio)
1633cec47e3dSKiyoshi Ueda {
1634cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1635cec47e3dSKiyoshi Ueda 
1636cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
16375a7bbad2SChristoph Hellwig 		blk_queue_bio(q, bio);
16385a7bbad2SChristoph Hellwig 	else
16395a7bbad2SChristoph Hellwig 		_dm_request(q, bio);
1640cec47e3dSKiyoshi Ueda }
1641cec47e3dSKiyoshi Ueda 
1642cec47e3dSKiyoshi Ueda void dm_dispatch_request(struct request *rq)
1643cec47e3dSKiyoshi Ueda {
1644cec47e3dSKiyoshi Ueda 	int r;
1645cec47e3dSKiyoshi Ueda 
1646cec47e3dSKiyoshi Ueda 	if (blk_queue_io_stat(rq->q))
1647cec47e3dSKiyoshi Ueda 		rq->cmd_flags |= REQ_IO_STAT;
1648cec47e3dSKiyoshi Ueda 
1649cec47e3dSKiyoshi Ueda 	rq->start_time = jiffies;
1650cec47e3dSKiyoshi Ueda 	r = blk_insert_cloned_request(rq->q, rq);
1651cec47e3dSKiyoshi Ueda 	if (r)
1652cec47e3dSKiyoshi Ueda 		dm_complete_request(rq, r);
1653cec47e3dSKiyoshi Ueda }
1654cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_dispatch_request);
1655cec47e3dSKiyoshi Ueda 
1656cec47e3dSKiyoshi Ueda static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1657cec47e3dSKiyoshi Ueda 				 void *data)
1658cec47e3dSKiyoshi Ueda {
1659cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = data;
166094818742SKent Overstreet 	struct dm_rq_clone_bio_info *info =
166194818742SKent Overstreet 		container_of(bio, struct dm_rq_clone_bio_info, clone);
1662cec47e3dSKiyoshi Ueda 
1663cec47e3dSKiyoshi Ueda 	info->orig = bio_orig;
1664cec47e3dSKiyoshi Ueda 	info->tio = tio;
1665cec47e3dSKiyoshi Ueda 	bio->bi_end_io = end_clone_bio;
1666cec47e3dSKiyoshi Ueda 	bio->bi_private = info;
1667cec47e3dSKiyoshi Ueda 
1668cec47e3dSKiyoshi Ueda 	return 0;
1669cec47e3dSKiyoshi Ueda }
1670cec47e3dSKiyoshi Ueda 
1671cec47e3dSKiyoshi Ueda static int setup_clone(struct request *clone, struct request *rq,
1672cec47e3dSKiyoshi Ueda 		       struct dm_rq_target_io *tio)
1673cec47e3dSKiyoshi Ueda {
1674d0bcb878SKiyoshi Ueda 	int r;
1675cec47e3dSKiyoshi Ueda 
1676d0bcb878SKiyoshi Ueda 	r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1677d0bcb878SKiyoshi Ueda 			      dm_rq_bio_constructor, tio);
1678cec47e3dSKiyoshi Ueda 	if (r)
1679cec47e3dSKiyoshi Ueda 		return r;
1680cec47e3dSKiyoshi Ueda 
1681cec47e3dSKiyoshi Ueda 	clone->cmd = rq->cmd;
1682cec47e3dSKiyoshi Ueda 	clone->cmd_len = rq->cmd_len;
1683cec47e3dSKiyoshi Ueda 	clone->sense = rq->sense;
1684cec47e3dSKiyoshi Ueda 	clone->buffer = rq->buffer;
1685cec47e3dSKiyoshi Ueda 	clone->end_io = end_clone_request;
1686cec47e3dSKiyoshi Ueda 	clone->end_io_data = tio;
1687cec47e3dSKiyoshi Ueda 
1688cec47e3dSKiyoshi Ueda 	return 0;
1689cec47e3dSKiyoshi Ueda }
1690cec47e3dSKiyoshi Ueda 
16916facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md,
16926facdaffSKiyoshi Ueda 				gfp_t gfp_mask)
16936facdaffSKiyoshi Ueda {
16946facdaffSKiyoshi Ueda 	struct request *clone;
16956facdaffSKiyoshi Ueda 	struct dm_rq_target_io *tio;
16966facdaffSKiyoshi Ueda 
16976facdaffSKiyoshi Ueda 	tio = alloc_rq_tio(md, gfp_mask);
16986facdaffSKiyoshi Ueda 	if (!tio)
16996facdaffSKiyoshi Ueda 		return NULL;
17006facdaffSKiyoshi Ueda 
17016facdaffSKiyoshi Ueda 	tio->md = md;
17026facdaffSKiyoshi Ueda 	tio->ti = NULL;
17036facdaffSKiyoshi Ueda 	tio->orig = rq;
17046facdaffSKiyoshi Ueda 	tio->error = 0;
17056facdaffSKiyoshi Ueda 	memset(&tio->info, 0, sizeof(tio->info));
17066facdaffSKiyoshi Ueda 
17076facdaffSKiyoshi Ueda 	clone = &tio->clone;
17086facdaffSKiyoshi Ueda 	if (setup_clone(clone, rq, tio)) {
17096facdaffSKiyoshi Ueda 		/* -ENOMEM */
17106facdaffSKiyoshi Ueda 		free_rq_tio(tio);
17116facdaffSKiyoshi Ueda 		return NULL;
17126facdaffSKiyoshi Ueda 	}
17136facdaffSKiyoshi Ueda 
17146facdaffSKiyoshi Ueda 	return clone;
17156facdaffSKiyoshi Ueda }
17166facdaffSKiyoshi Ueda 
1717cec47e3dSKiyoshi Ueda /*
1718cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1719cec47e3dSKiyoshi Ueda  */
1720cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq)
1721cec47e3dSKiyoshi Ueda {
1722cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1723cec47e3dSKiyoshi Ueda 	struct request *clone;
1724cec47e3dSKiyoshi Ueda 
1725cec47e3dSKiyoshi Ueda 	if (unlikely(rq->special)) {
1726cec47e3dSKiyoshi Ueda 		DMWARN("Already has something in rq->special.");
1727cec47e3dSKiyoshi Ueda 		return BLKPREP_KILL;
1728cec47e3dSKiyoshi Ueda 	}
1729cec47e3dSKiyoshi Ueda 
17306facdaffSKiyoshi Ueda 	clone = clone_rq(rq, md, GFP_ATOMIC);
17316facdaffSKiyoshi Ueda 	if (!clone)
1732cec47e3dSKiyoshi Ueda 		return BLKPREP_DEFER;
1733cec47e3dSKiyoshi Ueda 
1734cec47e3dSKiyoshi Ueda 	rq->special = clone;
1735cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_DONTPREP;
1736cec47e3dSKiyoshi Ueda 
1737cec47e3dSKiyoshi Ueda 	return BLKPREP_OK;
1738cec47e3dSKiyoshi Ueda }
1739cec47e3dSKiyoshi Ueda 
17409eef87daSKiyoshi Ueda /*
17419eef87daSKiyoshi Ueda  * Returns:
17429eef87daSKiyoshi Ueda  * 0  : the request has been processed (not requeued)
17439eef87daSKiyoshi Ueda  * !0 : the request has been requeued
17449eef87daSKiyoshi Ueda  */
17459eef87daSKiyoshi Ueda static int map_request(struct dm_target *ti, struct request *clone,
1746cec47e3dSKiyoshi Ueda 		       struct mapped_device *md)
1747cec47e3dSKiyoshi Ueda {
17489eef87daSKiyoshi Ueda 	int r, requeued = 0;
1749cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1750cec47e3dSKiyoshi Ueda 
1751cec47e3dSKiyoshi Ueda 	tio->ti = ti;
1752cec47e3dSKiyoshi Ueda 	r = ti->type->map_rq(ti, clone, &tio->info);
1753cec47e3dSKiyoshi Ueda 	switch (r) {
1754cec47e3dSKiyoshi Ueda 	case DM_MAPIO_SUBMITTED:
1755cec47e3dSKiyoshi Ueda 		/* The target has taken the I/O to submit by itself later */
1756cec47e3dSKiyoshi Ueda 		break;
1757cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REMAPPED:
1758cec47e3dSKiyoshi Ueda 		/* The target has remapped the I/O so dispatch it */
17596db4ccd6SJun'ichi Nomura 		trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
17606db4ccd6SJun'ichi Nomura 				     blk_rq_pos(tio->orig));
1761cec47e3dSKiyoshi Ueda 		dm_dispatch_request(clone);
1762cec47e3dSKiyoshi Ueda 		break;
1763cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REQUEUE:
1764cec47e3dSKiyoshi Ueda 		/* The target wants to requeue the I/O */
1765cec47e3dSKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
17669eef87daSKiyoshi Ueda 		requeued = 1;
1767cec47e3dSKiyoshi Ueda 		break;
1768cec47e3dSKiyoshi Ueda 	default:
1769cec47e3dSKiyoshi Ueda 		if (r > 0) {
1770cec47e3dSKiyoshi Ueda 			DMWARN("unimplemented target map return value: %d", r);
1771cec47e3dSKiyoshi Ueda 			BUG();
1772cec47e3dSKiyoshi Ueda 		}
1773cec47e3dSKiyoshi Ueda 
1774cec47e3dSKiyoshi Ueda 		/* The target wants to complete the I/O */
1775cec47e3dSKiyoshi Ueda 		dm_kill_unmapped_request(clone, r);
1776cec47e3dSKiyoshi Ueda 		break;
1777cec47e3dSKiyoshi Ueda 	}
17789eef87daSKiyoshi Ueda 
17799eef87daSKiyoshi Ueda 	return requeued;
1780cec47e3dSKiyoshi Ueda }
1781cec47e3dSKiyoshi Ueda 
1782ba1cbad9SMike Snitzer static struct request *dm_start_request(struct mapped_device *md, struct request *orig)
1783ba1cbad9SMike Snitzer {
1784ba1cbad9SMike Snitzer 	struct request *clone;
1785ba1cbad9SMike Snitzer 
1786ba1cbad9SMike Snitzer 	blk_start_request(orig);
1787ba1cbad9SMike Snitzer 	clone = orig->special;
1788ba1cbad9SMike Snitzer 	atomic_inc(&md->pending[rq_data_dir(clone)]);
1789ba1cbad9SMike Snitzer 
1790ba1cbad9SMike Snitzer 	/*
1791ba1cbad9SMike Snitzer 	 * Hold the md reference here for the in-flight I/O.
1792ba1cbad9SMike Snitzer 	 * We can't rely on the reference count by device opener,
1793ba1cbad9SMike Snitzer 	 * because the device may be closed during the request completion
1794ba1cbad9SMike Snitzer 	 * when all bios are completed.
1795ba1cbad9SMike Snitzer 	 * See the comment in rq_completed() too.
1796ba1cbad9SMike Snitzer 	 */
1797ba1cbad9SMike Snitzer 	dm_get(md);
1798ba1cbad9SMike Snitzer 
1799ba1cbad9SMike Snitzer 	return clone;
1800ba1cbad9SMike Snitzer }
1801ba1cbad9SMike Snitzer 
1802cec47e3dSKiyoshi Ueda /*
1803cec47e3dSKiyoshi Ueda  * q->request_fn for request-based dm.
1804cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1805cec47e3dSKiyoshi Ueda  */
1806cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q)
1807cec47e3dSKiyoshi Ueda {
1808cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
180983d5e5b0SMikulas Patocka 	int srcu_idx;
181083d5e5b0SMikulas Patocka 	struct dm_table *map = dm_get_live_table(md, &srcu_idx);
1811cec47e3dSKiyoshi Ueda 	struct dm_target *ti;
1812b4324feeSKiyoshi Ueda 	struct request *rq, *clone;
181329e4013dSTejun Heo 	sector_t pos;
1814cec47e3dSKiyoshi Ueda 
1815cec47e3dSKiyoshi Ueda 	/*
1816b4324feeSKiyoshi Ueda 	 * For suspend, check blk_queue_stopped() and increment
1817b4324feeSKiyoshi Ueda 	 * ->pending within a single queue_lock not to increment the
1818b4324feeSKiyoshi Ueda 	 * number of in-flight I/Os after the queue is stopped in
1819b4324feeSKiyoshi Ueda 	 * dm_suspend().
1820cec47e3dSKiyoshi Ueda 	 */
18217eaceaccSJens Axboe 	while (!blk_queue_stopped(q)) {
1822cec47e3dSKiyoshi Ueda 		rq = blk_peek_request(q);
1823cec47e3dSKiyoshi Ueda 		if (!rq)
18247eaceaccSJens Axboe 			goto delay_and_out;
1825cec47e3dSKiyoshi Ueda 
182629e4013dSTejun Heo 		/* always use block 0 to find the target for flushes for now */
182729e4013dSTejun Heo 		pos = 0;
182829e4013dSTejun Heo 		if (!(rq->cmd_flags & REQ_FLUSH))
182929e4013dSTejun Heo 			pos = blk_rq_pos(rq);
1830d0bcb878SKiyoshi Ueda 
183129e4013dSTejun Heo 		ti = dm_table_find_target(map, pos);
1832ba1cbad9SMike Snitzer 		if (!dm_target_is_valid(ti)) {
1833ba1cbad9SMike Snitzer 			/*
1834ba1cbad9SMike Snitzer 			 * Must perform setup, that dm_done() requires,
1835ba1cbad9SMike Snitzer 			 * before calling dm_kill_unmapped_request
1836ba1cbad9SMike Snitzer 			 */
1837ba1cbad9SMike Snitzer 			DMERR_LIMIT("request attempted access beyond the end of device");
1838ba1cbad9SMike Snitzer 			clone = dm_start_request(md, rq);
1839ba1cbad9SMike Snitzer 			dm_kill_unmapped_request(clone, -EIO);
1840ba1cbad9SMike Snitzer 			continue;
1841ba1cbad9SMike Snitzer 		}
184229e4013dSTejun Heo 
1843cec47e3dSKiyoshi Ueda 		if (ti->type->busy && ti->type->busy(ti))
18447eaceaccSJens Axboe 			goto delay_and_out;
1845cec47e3dSKiyoshi Ueda 
1846ba1cbad9SMike Snitzer 		clone = dm_start_request(md, rq);
1847b4324feeSKiyoshi Ueda 
1848cec47e3dSKiyoshi Ueda 		spin_unlock(q->queue_lock);
18499eef87daSKiyoshi Ueda 		if (map_request(ti, clone, md))
18509eef87daSKiyoshi Ueda 			goto requeued;
18519eef87daSKiyoshi Ueda 
1852052189a2SKiyoshi Ueda 		BUG_ON(!irqs_disabled());
1853052189a2SKiyoshi Ueda 		spin_lock(q->queue_lock);
1854cec47e3dSKiyoshi Ueda 	}
1855cec47e3dSKiyoshi Ueda 
1856cec47e3dSKiyoshi Ueda 	goto out;
1857cec47e3dSKiyoshi Ueda 
18589eef87daSKiyoshi Ueda requeued:
1859052189a2SKiyoshi Ueda 	BUG_ON(!irqs_disabled());
1860052189a2SKiyoshi Ueda 	spin_lock(q->queue_lock);
18619eef87daSKiyoshi Ueda 
18627eaceaccSJens Axboe delay_and_out:
18637eaceaccSJens Axboe 	blk_delay_queue(q, HZ / 10);
1864cec47e3dSKiyoshi Ueda out:
186583d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
1866cec47e3dSKiyoshi Ueda }
1867cec47e3dSKiyoshi Ueda 
1868cec47e3dSKiyoshi Ueda int dm_underlying_device_busy(struct request_queue *q)
1869cec47e3dSKiyoshi Ueda {
1870cec47e3dSKiyoshi Ueda 	return blk_lld_busy(q);
1871cec47e3dSKiyoshi Ueda }
1872cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1873cec47e3dSKiyoshi Ueda 
1874cec47e3dSKiyoshi Ueda static int dm_lld_busy(struct request_queue *q)
1875cec47e3dSKiyoshi Ueda {
1876cec47e3dSKiyoshi Ueda 	int r;
1877cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
187883d5e5b0SMikulas Patocka 	struct dm_table *map = dm_get_live_table_fast(md);
1879cec47e3dSKiyoshi Ueda 
1880cec47e3dSKiyoshi Ueda 	if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1881cec47e3dSKiyoshi Ueda 		r = 1;
1882cec47e3dSKiyoshi Ueda 	else
1883cec47e3dSKiyoshi Ueda 		r = dm_table_any_busy_target(map);
1884cec47e3dSKiyoshi Ueda 
188583d5e5b0SMikulas Patocka 	dm_put_live_table_fast(md);
1886cec47e3dSKiyoshi Ueda 
1887cec47e3dSKiyoshi Ueda 	return r;
1888cec47e3dSKiyoshi Ueda }
1889cec47e3dSKiyoshi Ueda 
18901da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
18911da177e4SLinus Torvalds {
18928a57dfc6SChandra Seetharaman 	int r = bdi_bits;
18938a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
18948a57dfc6SChandra Seetharaman 	struct dm_table *map;
18951da177e4SLinus Torvalds 
18961eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
189783d5e5b0SMikulas Patocka 		map = dm_get_live_table_fast(md);
18988a57dfc6SChandra Seetharaman 		if (map) {
1899cec47e3dSKiyoshi Ueda 			/*
1900cec47e3dSKiyoshi Ueda 			 * Request-based dm cares about only own queue for
1901cec47e3dSKiyoshi Ueda 			 * the query about congestion status of request_queue
1902cec47e3dSKiyoshi Ueda 			 */
1903cec47e3dSKiyoshi Ueda 			if (dm_request_based(md))
1904cec47e3dSKiyoshi Ueda 				r = md->queue->backing_dev_info.state &
1905cec47e3dSKiyoshi Ueda 				    bdi_bits;
1906cec47e3dSKiyoshi Ueda 			else
19071da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
19088a57dfc6SChandra Seetharaman 		}
190983d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
19108a57dfc6SChandra Seetharaman 	}
19118a57dfc6SChandra Seetharaman 
19121da177e4SLinus Torvalds 	return r;
19131da177e4SLinus Torvalds }
19141da177e4SLinus Torvalds 
19151da177e4SLinus Torvalds /*-----------------------------------------------------------------
19161da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
19171da177e4SLinus Torvalds  *---------------------------------------------------------------*/
19182b06cfffSAlasdair G Kergon static void free_minor(int minor)
19191da177e4SLinus Torvalds {
1920f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
19211da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1922f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
19231da177e4SLinus Torvalds }
19241da177e4SLinus Torvalds 
19251da177e4SLinus Torvalds /*
19261da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
19271da177e4SLinus Torvalds  */
1928cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
19291da177e4SLinus Torvalds {
1930c9d76be6STejun Heo 	int r;
19311da177e4SLinus Torvalds 
19321da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
19331da177e4SLinus Torvalds 		return -EINVAL;
19341da177e4SLinus Torvalds 
1935c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1936f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
19371da177e4SLinus Torvalds 
1938c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
19391da177e4SLinus Torvalds 
1940f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1941c9d76be6STejun Heo 	idr_preload_end();
1942c9d76be6STejun Heo 	if (r < 0)
1943c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
1944c9d76be6STejun Heo 	return 0;
19451da177e4SLinus Torvalds }
19461da177e4SLinus Torvalds 
1947cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
19481da177e4SLinus Torvalds {
1949c9d76be6STejun Heo 	int r;
19501da177e4SLinus Torvalds 
1951c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1952f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
19531da177e4SLinus Torvalds 
1954c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
19551da177e4SLinus Torvalds 
1956f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1957c9d76be6STejun Heo 	idr_preload_end();
1958c9d76be6STejun Heo 	if (r < 0)
19591da177e4SLinus Torvalds 		return r;
1960c9d76be6STejun Heo 	*minor = r;
1961c9d76be6STejun Heo 	return 0;
19621da177e4SLinus Torvalds }
19631da177e4SLinus Torvalds 
196483d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
19651da177e4SLinus Torvalds 
196653d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
196753d5914fSMikulas Patocka 
19684a0b4ddfSMike Snitzer static void dm_init_md_queue(struct mapped_device *md)
19694a0b4ddfSMike Snitzer {
19704a0b4ddfSMike Snitzer 	/*
19714a0b4ddfSMike Snitzer 	 * Request-based dm devices cannot be stacked on top of bio-based dm
19724a0b4ddfSMike Snitzer 	 * devices.  The type of this dm device has not been decided yet.
19734a0b4ddfSMike Snitzer 	 * The type is decided at the first table loading time.
19744a0b4ddfSMike Snitzer 	 * To prevent problematic device stacking, clear the queue flag
19754a0b4ddfSMike Snitzer 	 * for request stacking support until then.
19764a0b4ddfSMike Snitzer 	 *
19774a0b4ddfSMike Snitzer 	 * This queue is new, so no concurrency on the queue_flags.
19784a0b4ddfSMike Snitzer 	 */
19794a0b4ddfSMike Snitzer 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
19804a0b4ddfSMike Snitzer 
19814a0b4ddfSMike Snitzer 	md->queue->queuedata = md;
19824a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
19834a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_data = md;
19844a0b4ddfSMike Snitzer 	blk_queue_make_request(md->queue, dm_request);
19854a0b4ddfSMike Snitzer 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
19864a0b4ddfSMike Snitzer 	blk_queue_merge_bvec(md->queue, dm_merge_bvec);
19874a0b4ddfSMike Snitzer }
19884a0b4ddfSMike Snitzer 
19891da177e4SLinus Torvalds /*
19901da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
19911da177e4SLinus Torvalds  */
19922b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
19931da177e4SLinus Torvalds {
19941da177e4SLinus Torvalds 	int r;
1995cf13ab8eSFrederik Deweerdt 	struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1996ba61fdd1SJeff Mahoney 	void *old_md;
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds 	if (!md) {
19991da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
20001da177e4SLinus Torvalds 		return NULL;
20011da177e4SLinus Torvalds 	}
20021da177e4SLinus Torvalds 
200310da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
20046ed7ade8SMilan Broz 		goto bad_module_get;
200510da4f79SJeff Mahoney 
20061da177e4SLinus Torvalds 	/* get a minor number for the dev */
20072b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
2008cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
20092b06cfffSAlasdair G Kergon 	else
2010cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
20111da177e4SLinus Torvalds 	if (r < 0)
20126ed7ade8SMilan Broz 		goto bad_minor;
20131da177e4SLinus Torvalds 
201483d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
201583d5e5b0SMikulas Patocka 	if (r < 0)
201683d5e5b0SMikulas Patocka 		goto bad_io_barrier;
201783d5e5b0SMikulas Patocka 
2018a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
2019e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
2020a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
2021022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
20221da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
20235c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
20241da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
20257a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
20267a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
20277a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
20281da177e4SLinus Torvalds 
20294a0b4ddfSMike Snitzer 	md->queue = blk_alloc_queue(GFP_KERNEL);
20301da177e4SLinus Torvalds 	if (!md->queue)
20316ed7ade8SMilan Broz 		goto bad_queue;
20321da177e4SLinus Torvalds 
20334a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
20349faf400fSStefan Bader 
20351da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
20361da177e4SLinus Torvalds 	if (!md->disk)
20376ed7ade8SMilan Broz 		goto bad_disk;
20381da177e4SLinus Torvalds 
2039316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
2040316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
2041f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
204253d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
2043f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
2044*2995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
2045f0b04115SJeff Mahoney 
20461da177e4SLinus Torvalds 	md->disk->major = _major;
20471da177e4SLinus Torvalds 	md->disk->first_minor = minor;
20481da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
20491da177e4SLinus Torvalds 	md->disk->queue = md->queue;
20501da177e4SLinus Torvalds 	md->disk->private_data = md;
20511da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
20521da177e4SLinus Torvalds 	add_disk(md->disk);
20537e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
20541da177e4SLinus Torvalds 
2055670368a8STejun Heo 	md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
2056304f3f6aSMilan Broz 	if (!md->wq)
2057304f3f6aSMilan Broz 		goto bad_thread;
2058304f3f6aSMilan Broz 
205932a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
206032a926daSMikulas Patocka 	if (!md->bdev)
206132a926daSMikulas Patocka 		goto bad_bdev;
206232a926daSMikulas Patocka 
20636a8736d1STejun Heo 	bio_init(&md->flush_bio);
20646a8736d1STejun Heo 	md->flush_bio.bi_bdev = md->bdev;
20656a8736d1STejun Heo 	md->flush_bio.bi_rw = WRITE_FLUSH;
20666a8736d1STejun Heo 
2067fd2ed4d2SMikulas Patocka 	dm_stats_init(&md->stats);
2068fd2ed4d2SMikulas Patocka 
2069ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
2070f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
2071ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
2072f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2073ba61fdd1SJeff Mahoney 
2074ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
2075ba61fdd1SJeff Mahoney 
20761da177e4SLinus Torvalds 	return md;
20771da177e4SLinus Torvalds 
207832a926daSMikulas Patocka bad_bdev:
207932a926daSMikulas Patocka 	destroy_workqueue(md->wq);
2080304f3f6aSMilan Broz bad_thread:
208103022c54SZdenek Kabelac 	del_gendisk(md->disk);
2082304f3f6aSMilan Broz 	put_disk(md->disk);
20836ed7ade8SMilan Broz bad_disk:
20841312f40eSAl Viro 	blk_cleanup_queue(md->queue);
20856ed7ade8SMilan Broz bad_queue:
208683d5e5b0SMikulas Patocka 	cleanup_srcu_struct(&md->io_barrier);
208783d5e5b0SMikulas Patocka bad_io_barrier:
20881da177e4SLinus Torvalds 	free_minor(minor);
20896ed7ade8SMilan Broz bad_minor:
209010da4f79SJeff Mahoney 	module_put(THIS_MODULE);
20916ed7ade8SMilan Broz bad_module_get:
20921da177e4SLinus Torvalds 	kfree(md);
20931da177e4SLinus Torvalds 	return NULL;
20941da177e4SLinus Torvalds }
20951da177e4SLinus Torvalds 
2096ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
2097ae9da83fSJun'ichi Nomura 
20981da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
20991da177e4SLinus Torvalds {
2100f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
210163d94e48SJun'ichi Nomura 
2102ae9da83fSJun'ichi Nomura 	unlock_fs(md);
2103db8fef4fSMikulas Patocka 	bdput(md->bdev);
2104304f3f6aSMilan Broz 	destroy_workqueue(md->wq);
2105e6ee8c0bSKiyoshi Ueda 	if (md->io_pool)
21061da177e4SLinus Torvalds 		mempool_destroy(md->io_pool);
2107e6ee8c0bSKiyoshi Ueda 	if (md->bs)
21089faf400fSStefan Bader 		bioset_free(md->bs);
21099c47008dSMartin K. Petersen 	blk_integrity_unregister(md->disk);
21101da177e4SLinus Torvalds 	del_gendisk(md->disk);
211183d5e5b0SMikulas Patocka 	cleanup_srcu_struct(&md->io_barrier);
211263d94e48SJun'ichi Nomura 	free_minor(minor);
2113fba9f90eSJeff Mahoney 
2114fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
2115fba9f90eSJeff Mahoney 	md->disk->private_data = NULL;
2116fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
2117fba9f90eSJeff Mahoney 
21181da177e4SLinus Torvalds 	put_disk(md->disk);
21191312f40eSAl Viro 	blk_cleanup_queue(md->queue);
2120fd2ed4d2SMikulas Patocka 	dm_stats_cleanup(&md->stats);
212110da4f79SJeff Mahoney 	module_put(THIS_MODULE);
21221da177e4SLinus Torvalds 	kfree(md);
21231da177e4SLinus Torvalds }
21241da177e4SLinus Torvalds 
2125e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2126e6ee8c0bSKiyoshi Ueda {
2127c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
2128e6ee8c0bSKiyoshi Ueda 
21295f015204SJun'ichi Nomura 	if (md->io_pool && md->bs) {
213016245bdcSJun'ichi Nomura 		/* The md already has necessary mempools. */
213116245bdcSJun'ichi Nomura 		if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
2132c0820cf5SMikulas Patocka 			/*
213316245bdcSJun'ichi Nomura 			 * Reload bioset because front_pad may have changed
213416245bdcSJun'ichi Nomura 			 * because a different table was loaded.
2135c0820cf5SMikulas Patocka 			 */
2136c0820cf5SMikulas Patocka 			bioset_free(md->bs);
2137c0820cf5SMikulas Patocka 			md->bs = p->bs;
2138c0820cf5SMikulas Patocka 			p->bs = NULL;
213916245bdcSJun'ichi Nomura 		} else if (dm_table_get_type(t) == DM_TYPE_REQUEST_BASED) {
214016245bdcSJun'ichi Nomura 			/*
214116245bdcSJun'ichi Nomura 			 * There's no need to reload with request-based dm
214216245bdcSJun'ichi Nomura 			 * because the size of front_pad doesn't change.
214316245bdcSJun'ichi Nomura 			 * Note for future: If you are to reload bioset,
214416245bdcSJun'ichi Nomura 			 * prep-ed requests in the queue may refer
214516245bdcSJun'ichi Nomura 			 * to bio from the old bioset, so you must walk
214616245bdcSJun'ichi Nomura 			 * through the queue to unprep.
214716245bdcSJun'ichi Nomura 			 */
214816245bdcSJun'ichi Nomura 		}
2149e6ee8c0bSKiyoshi Ueda 		goto out;
2150c0820cf5SMikulas Patocka 	}
2151e6ee8c0bSKiyoshi Ueda 
21525f015204SJun'ichi Nomura 	BUG_ON(!p || md->io_pool || md->bs);
2153e6ee8c0bSKiyoshi Ueda 
2154e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
2155e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
2156e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
2157e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
2158e6ee8c0bSKiyoshi Ueda 
2159e6ee8c0bSKiyoshi Ueda out:
2160e6ee8c0bSKiyoshi Ueda 	/* mempool bind completed, now no need any mempools in the table */
2161e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
2162e6ee8c0bSKiyoshi Ueda }
2163e6ee8c0bSKiyoshi Ueda 
21641da177e4SLinus Torvalds /*
21651da177e4SLinus Torvalds  * Bind a table to the device.
21661da177e4SLinus Torvalds  */
21671da177e4SLinus Torvalds static void event_callback(void *context)
21681da177e4SLinus Torvalds {
21697a8c3d3bSMike Anderson 	unsigned long flags;
21707a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
21711da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
21721da177e4SLinus Torvalds 
21737a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
21747a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
21757a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
21767a8c3d3bSMike Anderson 
2177ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
21787a8c3d3bSMike Anderson 
21791da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
21801da177e4SLinus Torvalds 	wake_up(&md->eventq);
21811da177e4SLinus Torvalds }
21821da177e4SLinus Torvalds 
2183c217649bSMike Snitzer /*
2184c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
2185c217649bSMike Snitzer  */
21864e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
21871da177e4SLinus Torvalds {
21884e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
21891da177e4SLinus Torvalds 
2190db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
21911da177e4SLinus Torvalds }
21921da177e4SLinus Torvalds 
2193042d2a9bSAlasdair G Kergon /*
2194d5b9dd04SMikulas Patocka  * Return 1 if the queue has a compulsory merge_bvec_fn function.
2195d5b9dd04SMikulas Patocka  *
2196d5b9dd04SMikulas Patocka  * If this function returns 0, then the device is either a non-dm
2197d5b9dd04SMikulas Patocka  * device without a merge_bvec_fn, or it is a dm device that is
2198d5b9dd04SMikulas Patocka  * able to split any bios it receives that are too big.
2199d5b9dd04SMikulas Patocka  */
2200d5b9dd04SMikulas Patocka int dm_queue_merge_is_compulsory(struct request_queue *q)
2201d5b9dd04SMikulas Patocka {
2202d5b9dd04SMikulas Patocka 	struct mapped_device *dev_md;
2203d5b9dd04SMikulas Patocka 
2204d5b9dd04SMikulas Patocka 	if (!q->merge_bvec_fn)
2205d5b9dd04SMikulas Patocka 		return 0;
2206d5b9dd04SMikulas Patocka 
2207d5b9dd04SMikulas Patocka 	if (q->make_request_fn == dm_request) {
2208d5b9dd04SMikulas Patocka 		dev_md = q->queuedata;
2209d5b9dd04SMikulas Patocka 		if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2210d5b9dd04SMikulas Patocka 			return 0;
2211d5b9dd04SMikulas Patocka 	}
2212d5b9dd04SMikulas Patocka 
2213d5b9dd04SMikulas Patocka 	return 1;
2214d5b9dd04SMikulas Patocka }
2215d5b9dd04SMikulas Patocka 
2216d5b9dd04SMikulas Patocka static int dm_device_merge_is_compulsory(struct dm_target *ti,
2217d5b9dd04SMikulas Patocka 					 struct dm_dev *dev, sector_t start,
2218d5b9dd04SMikulas Patocka 					 sector_t len, void *data)
2219d5b9dd04SMikulas Patocka {
2220d5b9dd04SMikulas Patocka 	struct block_device *bdev = dev->bdev;
2221d5b9dd04SMikulas Patocka 	struct request_queue *q = bdev_get_queue(bdev);
2222d5b9dd04SMikulas Patocka 
2223d5b9dd04SMikulas Patocka 	return dm_queue_merge_is_compulsory(q);
2224d5b9dd04SMikulas Patocka }
2225d5b9dd04SMikulas Patocka 
2226d5b9dd04SMikulas Patocka /*
2227d5b9dd04SMikulas Patocka  * Return 1 if it is acceptable to ignore merge_bvec_fn based
2228d5b9dd04SMikulas Patocka  * on the properties of the underlying devices.
2229d5b9dd04SMikulas Patocka  */
2230d5b9dd04SMikulas Patocka static int dm_table_merge_is_optional(struct dm_table *table)
2231d5b9dd04SMikulas Patocka {
2232d5b9dd04SMikulas Patocka 	unsigned i = 0;
2233d5b9dd04SMikulas Patocka 	struct dm_target *ti;
2234d5b9dd04SMikulas Patocka 
2235d5b9dd04SMikulas Patocka 	while (i < dm_table_get_num_targets(table)) {
2236d5b9dd04SMikulas Patocka 		ti = dm_table_get_target(table, i++);
2237d5b9dd04SMikulas Patocka 
2238d5b9dd04SMikulas Patocka 		if (ti->type->iterate_devices &&
2239d5b9dd04SMikulas Patocka 		    ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2240d5b9dd04SMikulas Patocka 			return 0;
2241d5b9dd04SMikulas Patocka 	}
2242d5b9dd04SMikulas Patocka 
2243d5b9dd04SMikulas Patocka 	return 1;
2244d5b9dd04SMikulas Patocka }
2245d5b9dd04SMikulas Patocka 
2246d5b9dd04SMikulas Patocka /*
2247042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2248042d2a9bSAlasdair G Kergon  */
2249042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2250754c5fc7SMike Snitzer 			       struct queue_limits *limits)
22511da177e4SLinus Torvalds {
2252042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
2253165125e1SJens Axboe 	struct request_queue *q = md->queue;
22541da177e4SLinus Torvalds 	sector_t size;
2255d5b9dd04SMikulas Patocka 	int merge_is_optional;
22561da177e4SLinus Torvalds 
22571da177e4SLinus Torvalds 	size = dm_table_get_size(t);
22583ac51e74SDarrick J. Wong 
22593ac51e74SDarrick J. Wong 	/*
22603ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
22613ac51e74SDarrick J. Wong 	 */
2262fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
22633ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
22643ac51e74SDarrick J. Wong 
22654e90188bSAlasdair G Kergon 	__set_size(md, size);
22661da177e4SLinus Torvalds 
2267cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
22682ca3310eSAlasdair G Kergon 
2269e6ee8c0bSKiyoshi Ueda 	/*
2270e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
2271e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
2272e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
2273e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
2274e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
2275e6ee8c0bSKiyoshi Ueda 	 */
2276e6ee8c0bSKiyoshi Ueda 	if (dm_table_request_based(t) && !blk_queue_stopped(q))
2277e6ee8c0bSKiyoshi Ueda 		stop_queue(q);
2278e6ee8c0bSKiyoshi Ueda 
2279e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
2280e6ee8c0bSKiyoshi Ueda 
2281d5b9dd04SMikulas Patocka 	merge_is_optional = dm_table_merge_is_optional(t);
2282d5b9dd04SMikulas Patocka 
2283042d2a9bSAlasdair G Kergon 	old_map = md->map;
228483d5e5b0SMikulas Patocka 	rcu_assign_pointer(md->map, t);
228536a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
228636a0456fSAlasdair G Kergon 
2287754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
2288d5b9dd04SMikulas Patocka 	if (merge_is_optional)
2289d5b9dd04SMikulas Patocka 		set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2290d5b9dd04SMikulas Patocka 	else
2291d5b9dd04SMikulas Patocka 		clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
229283d5e5b0SMikulas Patocka 	dm_sync_table(md);
22932ca3310eSAlasdair G Kergon 
2294042d2a9bSAlasdair G Kergon 	return old_map;
22951da177e4SLinus Torvalds }
22961da177e4SLinus Torvalds 
2297a7940155SAlasdair G Kergon /*
2298a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2299a7940155SAlasdair G Kergon  */
2300a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
23011da177e4SLinus Torvalds {
23021da177e4SLinus Torvalds 	struct dm_table *map = md->map;
23031da177e4SLinus Torvalds 
23041da177e4SLinus Torvalds 	if (!map)
2305a7940155SAlasdair G Kergon 		return NULL;
23061da177e4SLinus Torvalds 
23071da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
230883d5e5b0SMikulas Patocka 	rcu_assign_pointer(md->map, NULL);
230983d5e5b0SMikulas Patocka 	dm_sync_table(md);
2310a7940155SAlasdair G Kergon 
2311a7940155SAlasdair G Kergon 	return map;
23121da177e4SLinus Torvalds }
23131da177e4SLinus Torvalds 
23141da177e4SLinus Torvalds /*
23151da177e4SLinus Torvalds  * Constructor for a new device.
23161da177e4SLinus Torvalds  */
23172b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
23181da177e4SLinus Torvalds {
23191da177e4SLinus Torvalds 	struct mapped_device *md;
23201da177e4SLinus Torvalds 
23212b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
23221da177e4SLinus Torvalds 	if (!md)
23231da177e4SLinus Torvalds 		return -ENXIO;
23241da177e4SLinus Torvalds 
2325784aae73SMilan Broz 	dm_sysfs_init(md);
2326784aae73SMilan Broz 
23271da177e4SLinus Torvalds 	*result = md;
23281da177e4SLinus Torvalds 	return 0;
23291da177e4SLinus Torvalds }
23301da177e4SLinus Torvalds 
2331a5664dadSMike Snitzer /*
2332a5664dadSMike Snitzer  * Functions to manage md->type.
2333a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2334a5664dadSMike Snitzer  */
2335a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2336a5664dadSMike Snitzer {
2337a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2338a5664dadSMike Snitzer }
2339a5664dadSMike Snitzer 
2340a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2341a5664dadSMike Snitzer {
2342a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2343a5664dadSMike Snitzer }
2344a5664dadSMike Snitzer 
2345a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type)
2346a5664dadSMike Snitzer {
234700c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2348a5664dadSMike Snitzer 	md->type = type;
2349a5664dadSMike Snitzer }
2350a5664dadSMike Snitzer 
2351a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md)
2352a5664dadSMike Snitzer {
235300c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2354a5664dadSMike Snitzer 	return md->type;
2355a5664dadSMike Snitzer }
2356a5664dadSMike Snitzer 
235736a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
235836a0456fSAlasdair G Kergon {
235936a0456fSAlasdair G Kergon 	return md->immutable_target_type;
236036a0456fSAlasdair G Kergon }
236136a0456fSAlasdair G Kergon 
23624a0b4ddfSMike Snitzer /*
2363f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
2364f84cb8a4SMike Snitzer  * count on 'md'.
2365f84cb8a4SMike Snitzer  */
2366f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2367f84cb8a4SMike Snitzer {
2368f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
2369f84cb8a4SMike Snitzer 	return &md->queue->limits;
2370f84cb8a4SMike Snitzer }
2371f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2372f84cb8a4SMike Snitzer 
2373f84cb8a4SMike Snitzer /*
23744a0b4ddfSMike Snitzer  * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
23754a0b4ddfSMike Snitzer  */
23764a0b4ddfSMike Snitzer static int dm_init_request_based_queue(struct mapped_device *md)
23774a0b4ddfSMike Snitzer {
23784a0b4ddfSMike Snitzer 	struct request_queue *q = NULL;
23794a0b4ddfSMike Snitzer 
23804a0b4ddfSMike Snitzer 	if (md->queue->elevator)
23814a0b4ddfSMike Snitzer 		return 1;
23824a0b4ddfSMike Snitzer 
23834a0b4ddfSMike Snitzer 	/* Fully initialize the queue */
23844a0b4ddfSMike Snitzer 	q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
23854a0b4ddfSMike Snitzer 	if (!q)
23864a0b4ddfSMike Snitzer 		return 0;
23874a0b4ddfSMike Snitzer 
23884a0b4ddfSMike Snitzer 	md->queue = q;
23894a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
23904a0b4ddfSMike Snitzer 	blk_queue_softirq_done(md->queue, dm_softirq_done);
23914a0b4ddfSMike Snitzer 	blk_queue_prep_rq(md->queue, dm_prep_fn);
23924a0b4ddfSMike Snitzer 	blk_queue_lld_busy(md->queue, dm_lld_busy);
23934a0b4ddfSMike Snitzer 
23944a0b4ddfSMike Snitzer 	elv_register_queue(md->queue);
23954a0b4ddfSMike Snitzer 
23964a0b4ddfSMike Snitzer 	return 1;
23974a0b4ddfSMike Snitzer }
23984a0b4ddfSMike Snitzer 
23994a0b4ddfSMike Snitzer /*
24004a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
24014a0b4ddfSMike Snitzer  */
24024a0b4ddfSMike Snitzer int dm_setup_md_queue(struct mapped_device *md)
24034a0b4ddfSMike Snitzer {
24044a0b4ddfSMike Snitzer 	if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
24054a0b4ddfSMike Snitzer 	    !dm_init_request_based_queue(md)) {
24064a0b4ddfSMike Snitzer 		DMWARN("Cannot initialize queue for request-based mapped device");
24074a0b4ddfSMike Snitzer 		return -EINVAL;
24084a0b4ddfSMike Snitzer 	}
24094a0b4ddfSMike Snitzer 
24104a0b4ddfSMike Snitzer 	return 0;
24114a0b4ddfSMike Snitzer }
24124a0b4ddfSMike Snitzer 
2413637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev)
24141da177e4SLinus Torvalds {
24151da177e4SLinus Torvalds 	struct mapped_device *md;
24161da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
24171da177e4SLinus Torvalds 
24181da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
24191da177e4SLinus Torvalds 		return NULL;
24201da177e4SLinus Torvalds 
2421f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
24221da177e4SLinus Torvalds 
24231da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
2424fba9f90eSJeff Mahoney 	if (md && (md == MINOR_ALLOCED ||
2425f331c029STejun Heo 		   (MINOR(disk_devt(dm_disk(md))) != minor) ||
2426abdc568bSKiyoshi Ueda 		   dm_deleting_md(md) ||
2427fba9f90eSJeff Mahoney 		   test_bit(DMF_FREEING, &md->flags))) {
2428637842cfSDavid Teigland 		md = NULL;
2429fba9f90eSJeff Mahoney 		goto out;
2430fba9f90eSJeff Mahoney 	}
24311da177e4SLinus Torvalds 
2432fba9f90eSJeff Mahoney out:
2433f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
24341da177e4SLinus Torvalds 
2435637842cfSDavid Teigland 	return md;
2436637842cfSDavid Teigland }
2437637842cfSDavid Teigland 
2438d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev)
2439d229a958SDavid Teigland {
2440d229a958SDavid Teigland 	struct mapped_device *md = dm_find_md(dev);
2441d229a958SDavid Teigland 
2442d229a958SDavid Teigland 	if (md)
2443d229a958SDavid Teigland 		dm_get(md);
2444d229a958SDavid Teigland 
2445d229a958SDavid Teigland 	return md;
2446d229a958SDavid Teigland }
24473cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2448d229a958SDavid Teigland 
24499ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2450637842cfSDavid Teigland {
24519ade92a9SAlasdair G Kergon 	return md->interface_ptr;
24521da177e4SLinus Torvalds }
24531da177e4SLinus Torvalds 
24541da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
24551da177e4SLinus Torvalds {
24561da177e4SLinus Torvalds 	md->interface_ptr = ptr;
24571da177e4SLinus Torvalds }
24581da177e4SLinus Torvalds 
24591da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
24601da177e4SLinus Torvalds {
24611da177e4SLinus Torvalds 	atomic_inc(&md->holders);
24623f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
24631da177e4SLinus Torvalds }
24641da177e4SLinus Torvalds 
246572d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
246672d94861SAlasdair G Kergon {
246772d94861SAlasdair G Kergon 	return md->name;
246872d94861SAlasdair G Kergon }
246972d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
247072d94861SAlasdair G Kergon 
24713f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
24721da177e4SLinus Torvalds {
24731134e5aeSMike Anderson 	struct dm_table *map;
247483d5e5b0SMikulas Patocka 	int srcu_idx;
24751da177e4SLinus Torvalds 
24763f77316dSKiyoshi Ueda 	might_sleep();
2477fba9f90eSJeff Mahoney 
24783f77316dSKiyoshi Ueda 	spin_lock(&_minor_lock);
247983d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
24803f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2481fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2482f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
24833f77316dSKiyoshi Ueda 
24844f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
24851da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
24861da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
24871da177e4SLinus Torvalds 	}
24883f77316dSKiyoshi Ueda 
248983d5e5b0SMikulas Patocka 	/* dm_put_live_table must be before msleep, otherwise deadlock is possible */
249083d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
249183d5e5b0SMikulas Patocka 
24923f77316dSKiyoshi Ueda 	/*
24933f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
24943f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
24953f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
24963f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
24973f77316dSKiyoshi Ueda 	 */
24983f77316dSKiyoshi Ueda 	if (wait)
24993f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
25003f77316dSKiyoshi Ueda 			msleep(1);
25013f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
25023f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
25033f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
25043f77316dSKiyoshi Ueda 
2505784aae73SMilan Broz 	dm_sysfs_exit(md);
2506a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
25071da177e4SLinus Torvalds 	free_dev(md);
25081da177e4SLinus Torvalds }
25093f77316dSKiyoshi Ueda 
25103f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
25113f77316dSKiyoshi Ueda {
25123f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
25133f77316dSKiyoshi Ueda }
25143f77316dSKiyoshi Ueda 
25153f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
25163f77316dSKiyoshi Ueda {
25173f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
25183f77316dSKiyoshi Ueda }
25193f77316dSKiyoshi Ueda 
25203f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
25213f77316dSKiyoshi Ueda {
25223f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
25231da177e4SLinus Torvalds }
252479eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
25251da177e4SLinus Torvalds 
2526401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
252746125c1cSMilan Broz {
252846125c1cSMilan Broz 	int r = 0;
2529b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
2530b44ebeb0SMikulas Patocka 
2531b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
253246125c1cSMilan Broz 
253346125c1cSMilan Broz 	while (1) {
2534401600dfSMikulas Patocka 		set_current_state(interruptible);
253546125c1cSMilan Broz 
2536b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
253746125c1cSMilan Broz 			break;
253846125c1cSMilan Broz 
2539401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
2540401600dfSMikulas Patocka 		    signal_pending(current)) {
254146125c1cSMilan Broz 			r = -EINTR;
254246125c1cSMilan Broz 			break;
254346125c1cSMilan Broz 		}
254446125c1cSMilan Broz 
254546125c1cSMilan Broz 		io_schedule();
254646125c1cSMilan Broz 	}
254746125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
254846125c1cSMilan Broz 
2549b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
2550b44ebeb0SMikulas Patocka 
255146125c1cSMilan Broz 	return r;
255246125c1cSMilan Broz }
255346125c1cSMilan Broz 
25541da177e4SLinus Torvalds /*
25551da177e4SLinus Torvalds  * Process the deferred bios
25561da177e4SLinus Torvalds  */
2557ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
25581da177e4SLinus Torvalds {
2559ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2560ef208587SMikulas Patocka 						work);
25616d6f10dfSMilan Broz 	struct bio *c;
256283d5e5b0SMikulas Patocka 	int srcu_idx;
256383d5e5b0SMikulas Patocka 	struct dm_table *map;
25641da177e4SLinus Torvalds 
256583d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
2566ef208587SMikulas Patocka 
25673b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2568022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2569022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2570022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2571022c2611SMikulas Patocka 
25726a8736d1STejun Heo 		if (!c)
2573df12ee99SAlasdair G Kergon 			break;
257473d410c0SMilan Broz 
2575e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2576e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2577af7e466aSMikulas Patocka 		else
257883d5e5b0SMikulas Patocka 			__split_and_process_bio(md, map, c);
2579e6ee8c0bSKiyoshi Ueda 	}
25803b00b203SMikulas Patocka 
258183d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
25821da177e4SLinus Torvalds }
25831da177e4SLinus Torvalds 
25849a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2585304f3f6aSMilan Broz {
25863b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
25873b00b203SMikulas Patocka 	smp_mb__after_clear_bit();
258853d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2589304f3f6aSMilan Broz }
2590304f3f6aSMilan Broz 
25911da177e4SLinus Torvalds /*
2592042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
25931da177e4SLinus Torvalds  */
2594042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
25951da177e4SLinus Torvalds {
259687eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2597754c5fc7SMike Snitzer 	struct queue_limits limits;
2598042d2a9bSAlasdair G Kergon 	int r;
25991da177e4SLinus Torvalds 
2600e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
26011da177e4SLinus Torvalds 
26021da177e4SLinus Torvalds 	/* device must be suspended */
26034f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
260493c534aeSAlasdair G Kergon 		goto out;
26051da177e4SLinus Torvalds 
26063ae70656SMike Snitzer 	/*
26073ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
26083ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
26093ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
26103ae70656SMike Snitzer 	 * reappear.
26113ae70656SMike Snitzer 	 */
26123ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
261383d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
26143ae70656SMike Snitzer 		if (live_map)
26153ae70656SMike Snitzer 			limits = md->queue->limits;
261683d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
26173ae70656SMike Snitzer 	}
26183ae70656SMike Snitzer 
261987eb5b21SMike Christie 	if (!live_map) {
2620754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2621042d2a9bSAlasdair G Kergon 		if (r) {
2622042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2623754c5fc7SMike Snitzer 			goto out;
2624042d2a9bSAlasdair G Kergon 		}
262587eb5b21SMike Christie 	}
2626754c5fc7SMike Snitzer 
2627042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
26281da177e4SLinus Torvalds 
262993c534aeSAlasdair G Kergon out:
2630e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2631042d2a9bSAlasdair G Kergon 	return map;
26321da177e4SLinus Torvalds }
26331da177e4SLinus Torvalds 
26341da177e4SLinus Torvalds /*
26351da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
26361da177e4SLinus Torvalds  * device.
26371da177e4SLinus Torvalds  */
26382ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
26391da177e4SLinus Torvalds {
2640e39e2e95SAlasdair G Kergon 	int r;
26411da177e4SLinus Torvalds 
26421da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2643dfbe03f6SAlasdair G Kergon 
2644db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2645dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2646cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2647e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2648e39e2e95SAlasdair G Kergon 		return r;
2649dfbe03f6SAlasdair G Kergon 	}
2650dfbe03f6SAlasdair G Kergon 
2651aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2652aa8d7c2fSAlasdair G Kergon 
26531da177e4SLinus Torvalds 	return 0;
26541da177e4SLinus Torvalds }
26551da177e4SLinus Torvalds 
26562ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
26571da177e4SLinus Torvalds {
2658aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2659aa8d7c2fSAlasdair G Kergon 		return;
2660aa8d7c2fSAlasdair G Kergon 
2661db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
26621da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2663aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
26641da177e4SLinus Torvalds }
26651da177e4SLinus Torvalds 
26661da177e4SLinus Torvalds /*
26671da177e4SLinus Torvalds  * We need to be able to change a mapping table under a mounted
26681da177e4SLinus Torvalds  * filesystem.  For example we might want to move some data in
26691da177e4SLinus Torvalds  * the background.  Before the table can be swapped with
26701da177e4SLinus Torvalds  * dm_bind_table, dm_suspend must be called to flush any in
26711da177e4SLinus Torvalds  * flight bios and ensure that any further io gets deferred.
26721da177e4SLinus Torvalds  */
2673cec47e3dSKiyoshi Ueda /*
2674cec47e3dSKiyoshi Ueda  * Suspend mechanism in request-based dm.
2675cec47e3dSKiyoshi Ueda  *
26769f518b27SKiyoshi Ueda  * 1. Flush all I/Os by lock_fs() if needed.
26779f518b27SKiyoshi Ueda  * 2. Stop dispatching any I/O by stopping the request_queue.
26789f518b27SKiyoshi Ueda  * 3. Wait for all in-flight I/Os to be completed or requeued.
2679cec47e3dSKiyoshi Ueda  *
26809f518b27SKiyoshi Ueda  * To abort suspend, start the request_queue.
2681cec47e3dSKiyoshi Ueda  */
2682a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
26831da177e4SLinus Torvalds {
26842ca3310eSAlasdair G Kergon 	struct dm_table *map = NULL;
268546125c1cSMilan Broz 	int r = 0;
2686a3d77d35SKiyoshi Ueda 	int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
26872e93ccc1SKiyoshi Ueda 	int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
26881da177e4SLinus Torvalds 
2689e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
26902ca3310eSAlasdair G Kergon 
26914f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
269273d410c0SMilan Broz 		r = -EINVAL;
2693d287483dSAlasdair G Kergon 		goto out_unlock;
269473d410c0SMilan Broz 	}
26951da177e4SLinus Torvalds 
269683d5e5b0SMikulas Patocka 	map = md->map;
2697cf222b37SAlasdair G Kergon 
26982e93ccc1SKiyoshi Ueda 	/*
26992e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
27002e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
27012e93ccc1SKiyoshi Ueda 	 */
27022e93ccc1SKiyoshi Ueda 	if (noflush)
27032e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
27042e93ccc1SKiyoshi Ueda 
2705436d4108SAlasdair G Kergon 	/* This does not get reverted if there's an error later. */
27061da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
27071da177e4SLinus Torvalds 
27082e93ccc1SKiyoshi Ueda 	/*
27099f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
27109f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
27119f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
27129f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
27132e93ccc1SKiyoshi Ueda 	 */
271432a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
27152ca3310eSAlasdair G Kergon 		r = lock_fs(md);
27162ca3310eSAlasdair G Kergon 		if (r)
271783d5e5b0SMikulas Patocka 			goto out_unlock;
2718aa8d7c2fSAlasdair G Kergon 	}
27191da177e4SLinus Torvalds 
27201da177e4SLinus Torvalds 	/*
27213b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
27223b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
27233b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
27243b00b203SMikulas Patocka 	 * dm_wq_work.
27253b00b203SMikulas Patocka 	 *
27263b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
27273b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
27286a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
27296a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
27306a8736d1STejun Heo 	 * flush_workqueue(md->wq).
27311da177e4SLinus Torvalds 	 */
27321eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
273383d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
27341da177e4SLinus Torvalds 
2735d0bcb878SKiyoshi Ueda 	/*
273629e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
273729e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2738d0bcb878SKiyoshi Ueda 	 */
2739cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
27409f518b27SKiyoshi Ueda 		stop_queue(md->queue);
2741cec47e3dSKiyoshi Ueda 
2742d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2743d0bcb878SKiyoshi Ueda 
27441da177e4SLinus Torvalds 	/*
27453b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
27463b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
27473b00b203SMikulas Patocka 	 * to finish.
27481da177e4SLinus Torvalds 	 */
2749401600dfSMikulas Patocka 	r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
27501da177e4SLinus Torvalds 
27516d6f10dfSMilan Broz 	if (noflush)
2752022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
275383d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
27542e93ccc1SKiyoshi Ueda 
27551da177e4SLinus Torvalds 	/* were we interrupted ? */
275646125c1cSMilan Broz 	if (r < 0) {
27579a1fb464SMikulas Patocka 		dm_queue_flush(md);
275873d410c0SMilan Broz 
2759cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
27609f518b27SKiyoshi Ueda 			start_queue(md->queue);
2761cec47e3dSKiyoshi Ueda 
27622ca3310eSAlasdair G Kergon 		unlock_fs(md);
276383d5e5b0SMikulas Patocka 		goto out_unlock; /* pushback list is already flushed, so skip flush */
27642ca3310eSAlasdair G Kergon 	}
27652ca3310eSAlasdair G Kergon 
27663b00b203SMikulas Patocka 	/*
27673b00b203SMikulas Patocka 	 * If dm_wait_for_completion returned 0, the device is completely
27683b00b203SMikulas Patocka 	 * quiescent now. There is no request-processing activity. All new
27693b00b203SMikulas Patocka 	 * requests are being added to md->deferred list.
27703b00b203SMikulas Patocka 	 */
27713b00b203SMikulas Patocka 
27721da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
27731da177e4SLinus Torvalds 
27744d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
27754d4471cbSKiyoshi Ueda 
2776d287483dSAlasdair G Kergon out_unlock:
2777e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2778cf222b37SAlasdair G Kergon 	return r;
27791da177e4SLinus Torvalds }
27801da177e4SLinus Torvalds 
27811da177e4SLinus Torvalds int dm_resume(struct mapped_device *md)
27821da177e4SLinus Torvalds {
2783cf222b37SAlasdair G Kergon 	int r = -EINVAL;
2784cf222b37SAlasdair G Kergon 	struct dm_table *map = NULL;
27851da177e4SLinus Torvalds 
2786e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
27874f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
2788cf222b37SAlasdair G Kergon 		goto out;
2789cf222b37SAlasdair G Kergon 
279083d5e5b0SMikulas Patocka 	map = md->map;
27912ca3310eSAlasdair G Kergon 	if (!map || !dm_table_get_size(map))
2792cf222b37SAlasdair G Kergon 		goto out;
27931da177e4SLinus Torvalds 
27948757b776SMilan Broz 	r = dm_table_resume_targets(map);
27958757b776SMilan Broz 	if (r)
27968757b776SMilan Broz 		goto out;
27972ca3310eSAlasdair G Kergon 
27989a1fb464SMikulas Patocka 	dm_queue_flush(md);
27992ca3310eSAlasdair G Kergon 
2800cec47e3dSKiyoshi Ueda 	/*
2801cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2802cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2803cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2804cec47e3dSKiyoshi Ueda 	 */
2805cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2806cec47e3dSKiyoshi Ueda 		start_queue(md->queue);
2807cec47e3dSKiyoshi Ueda 
28082ca3310eSAlasdair G Kergon 	unlock_fs(md);
28092ca3310eSAlasdair G Kergon 
28102ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
28112ca3310eSAlasdair G Kergon 
2812cf222b37SAlasdair G Kergon 	r = 0;
2813cf222b37SAlasdair G Kergon out:
2814e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
28152ca3310eSAlasdair G Kergon 
2816cf222b37SAlasdair G Kergon 	return r;
28171da177e4SLinus Torvalds }
28181da177e4SLinus Torvalds 
2819fd2ed4d2SMikulas Patocka /*
2820fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
2821fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
2822fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
2823fd2ed4d2SMikulas Patocka  *
2824fd2ed4d2SMikulas Patocka  * Internal suspend holds md->suspend_lock, which prevents interaction with
2825fd2ed4d2SMikulas Patocka  * userspace-driven suspend.
2826fd2ed4d2SMikulas Patocka  */
2827fd2ed4d2SMikulas Patocka 
2828fd2ed4d2SMikulas Patocka void dm_internal_suspend(struct mapped_device *md)
2829fd2ed4d2SMikulas Patocka {
2830fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
2831fd2ed4d2SMikulas Patocka 	if (dm_suspended_md(md))
2832fd2ed4d2SMikulas Patocka 		return;
2833fd2ed4d2SMikulas Patocka 
2834fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2835fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
2836fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
2837fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2838fd2ed4d2SMikulas Patocka }
2839fd2ed4d2SMikulas Patocka 
2840fd2ed4d2SMikulas Patocka void dm_internal_resume(struct mapped_device *md)
2841fd2ed4d2SMikulas Patocka {
2842fd2ed4d2SMikulas Patocka 	if (dm_suspended_md(md))
2843fd2ed4d2SMikulas Patocka 		goto done;
2844fd2ed4d2SMikulas Patocka 
2845fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
2846fd2ed4d2SMikulas Patocka 
2847fd2ed4d2SMikulas Patocka done:
2848fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
2849fd2ed4d2SMikulas Patocka }
2850fd2ed4d2SMikulas Patocka 
28511da177e4SLinus Torvalds /*-----------------------------------------------------------------
28521da177e4SLinus Torvalds  * Event notification.
28531da177e4SLinus Torvalds  *---------------------------------------------------------------*/
28543abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
285560935eb2SMilan Broz 		       unsigned cookie)
285669267a30SAlasdair G Kergon {
285760935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
285860935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
285960935eb2SMilan Broz 
286060935eb2SMilan Broz 	if (!cookie)
28613abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
286260935eb2SMilan Broz 	else {
286360935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
286460935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
28653abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
28663abf85b5SPeter Rajnoha 					  action, envp);
286760935eb2SMilan Broz 	}
286869267a30SAlasdair G Kergon }
286969267a30SAlasdair G Kergon 
28707a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
28717a8c3d3bSMike Anderson {
28727a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
28737a8c3d3bSMike Anderson }
28747a8c3d3bSMike Anderson 
28751da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
28761da177e4SLinus Torvalds {
28771da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
28781da177e4SLinus Torvalds }
28791da177e4SLinus Torvalds 
28801da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
28811da177e4SLinus Torvalds {
28821da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
28831da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
28841da177e4SLinus Torvalds }
28851da177e4SLinus Torvalds 
28867a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
28877a8c3d3bSMike Anderson {
28887a8c3d3bSMike Anderson 	unsigned long flags;
28897a8c3d3bSMike Anderson 
28907a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
28917a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
28927a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
28937a8c3d3bSMike Anderson }
28947a8c3d3bSMike Anderson 
28951da177e4SLinus Torvalds /*
28961da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
28971da177e4SLinus Torvalds  * count on 'md'.
28981da177e4SLinus Torvalds  */
28991da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
29001da177e4SLinus Torvalds {
29011da177e4SLinus Torvalds 	return md->disk;
29021da177e4SLinus Torvalds }
29031da177e4SLinus Torvalds 
2904784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2905784aae73SMilan Broz {
2906*2995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
2907784aae73SMilan Broz }
2908784aae73SMilan Broz 
2909784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2910784aae73SMilan Broz {
2911784aae73SMilan Broz 	struct mapped_device *md;
2912784aae73SMilan Broz 
2913*2995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
2914784aae73SMilan Broz 
29154d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
2916432a212cSMike Anderson 	    dm_deleting_md(md))
29174d89b7b4SMilan Broz 		return NULL;
29184d89b7b4SMilan Broz 
2919784aae73SMilan Broz 	dm_get(md);
2920784aae73SMilan Broz 	return md;
2921784aae73SMilan Broz }
2922784aae73SMilan Broz 
29234f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
29241da177e4SLinus Torvalds {
29251da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
29261da177e4SLinus Torvalds }
29271da177e4SLinus Torvalds 
29282c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
29292c140a24SMikulas Patocka {
29302c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
29312c140a24SMikulas Patocka }
29322c140a24SMikulas Patocka 
293364dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
293464dbce58SKiyoshi Ueda {
2935ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
293664dbce58SKiyoshi Ueda }
293764dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
293864dbce58SKiyoshi Ueda 
29392e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
29402e93ccc1SKiyoshi Ueda {
2941ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
29422e93ccc1SKiyoshi Ueda }
29432e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
29442e93ccc1SKiyoshi Ueda 
2945c0820cf5SMikulas Patocka struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity, unsigned per_bio_data_size)
2946e6ee8c0bSKiyoshi Ueda {
29475f015204SJun'ichi Nomura 	struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
29485f015204SJun'ichi Nomura 	struct kmem_cache *cachep;
29495f015204SJun'ichi Nomura 	unsigned int pool_size;
29505f015204SJun'ichi Nomura 	unsigned int front_pad;
2951e6ee8c0bSKiyoshi Ueda 
2952e6ee8c0bSKiyoshi Ueda 	if (!pools)
2953e6ee8c0bSKiyoshi Ueda 		return NULL;
2954e6ee8c0bSKiyoshi Ueda 
295523e5083bSJun'ichi Nomura 	if (type == DM_TYPE_BIO_BASED) {
29565f015204SJun'ichi Nomura 		cachep = _io_cache;
2957e8603136SMike Snitzer 		pool_size = dm_get_reserved_bio_based_ios();
29585f015204SJun'ichi Nomura 		front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
29595f015204SJun'ichi Nomura 	} else if (type == DM_TYPE_REQUEST_BASED) {
29605f015204SJun'ichi Nomura 		cachep = _rq_tio_cache;
2961f4790826SMike Snitzer 		pool_size = dm_get_reserved_rq_based_ios();
29625f015204SJun'ichi Nomura 		front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
29635f015204SJun'ichi Nomura 		/* per_bio_data_size is not used. See __bind_mempools(). */
29645f015204SJun'ichi Nomura 		WARN_ON(per_bio_data_size != 0);
29655f015204SJun'ichi Nomura 	} else
29665f015204SJun'ichi Nomura 		goto out;
29675f015204SJun'ichi Nomura 
29686cfa5857SMike Snitzer 	pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
2969e6ee8c0bSKiyoshi Ueda 	if (!pools->io_pool)
29705f015204SJun'ichi Nomura 		goto out;
2971e6ee8c0bSKiyoshi Ueda 
29725f015204SJun'ichi Nomura 	pools->bs = bioset_create(pool_size, front_pad);
2973e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
29745f015204SJun'ichi Nomura 		goto out;
2975e6ee8c0bSKiyoshi Ueda 
2976a91a2785SMartin K. Petersen 	if (integrity && bioset_integrity_create(pools->bs, pool_size))
29775f015204SJun'ichi Nomura 		goto out;
2978a91a2785SMartin K. Petersen 
2979e6ee8c0bSKiyoshi Ueda 	return pools;
2980e6ee8c0bSKiyoshi Ueda 
29815f015204SJun'ichi Nomura out:
29825f015204SJun'ichi Nomura 	dm_free_md_mempools(pools);
2983e6ee8c0bSKiyoshi Ueda 
2984e6ee8c0bSKiyoshi Ueda 	return NULL;
2985e6ee8c0bSKiyoshi Ueda }
2986e6ee8c0bSKiyoshi Ueda 
2987e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
2988e6ee8c0bSKiyoshi Ueda {
2989e6ee8c0bSKiyoshi Ueda 	if (!pools)
2990e6ee8c0bSKiyoshi Ueda 		return;
2991e6ee8c0bSKiyoshi Ueda 
2992e6ee8c0bSKiyoshi Ueda 	if (pools->io_pool)
2993e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->io_pool);
2994e6ee8c0bSKiyoshi Ueda 
2995e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
2996e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
2997e6ee8c0bSKiyoshi Ueda 
2998e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2999e6ee8c0bSKiyoshi Ueda }
3000e6ee8c0bSKiyoshi Ueda 
300183d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
30021da177e4SLinus Torvalds 	.open = dm_blk_open,
30031da177e4SLinus Torvalds 	.release = dm_blk_close,
3004aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
30053ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
30061da177e4SLinus Torvalds 	.owner = THIS_MODULE
30071da177e4SLinus Torvalds };
30081da177e4SLinus Torvalds 
30091da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo);
30101da177e4SLinus Torvalds 
30111da177e4SLinus Torvalds /*
30121da177e4SLinus Torvalds  * module hooks
30131da177e4SLinus Torvalds  */
30141da177e4SLinus Torvalds module_init(dm_init);
30151da177e4SLinus Torvalds module_exit(dm_exit);
30161da177e4SLinus Torvalds 
30171da177e4SLinus Torvalds module_param(major, uint, 0);
30181da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
3019f4790826SMike Snitzer 
3020e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3021e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3022e8603136SMike Snitzer 
3023f4790826SMike Snitzer module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
3024f4790826SMike Snitzer MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
3025f4790826SMike Snitzer 
30261da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
30271da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
30281da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3029