xref: /openbmc/linux/drivers/md/dm.c (revision 8ae126660fddbeebb9251a174e6fa45b6ad8f932)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3784aae73SMilan Broz  * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This file is released under the GPL.
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include "dm.h"
951e5b2bdSMike Anderson #include "dm-uevent.h"
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/init.h>
121da177e4SLinus Torvalds #include <linux/module.h>
1348c9c27bSArjan van de Ven #include <linux/mutex.h>
141da177e4SLinus Torvalds #include <linux/moduleparam.h>
151da177e4SLinus Torvalds #include <linux/blkpg.h>
161da177e4SLinus Torvalds #include <linux/bio.h>
171da177e4SLinus Torvalds #include <linux/mempool.h>
181da177e4SLinus Torvalds #include <linux/slab.h>
191da177e4SLinus Torvalds #include <linux/idr.h>
203ac51e74SDarrick J. Wong #include <linux/hdreg.h>
213f77316dSKiyoshi Ueda #include <linux/delay.h>
22ffcc3936SMike Snitzer #include <linux/wait.h>
232eb6e1e3SKeith Busch #include <linux/kthread.h>
240ce65797SMike Snitzer #include <linux/ktime.h>
25de3ec86dSMike Snitzer #include <linux/elevator.h> /* for rq_end_sector() */
26bfebd1cdSMike Snitzer #include <linux/blk-mq.h>
2755782138SLi Zefan 
2855782138SLi Zefan #include <trace/events/block.h>
291da177e4SLinus Torvalds 
3072d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
3172d94861SAlasdair G Kergon 
3271a16736SNamhyung Kim #ifdef CONFIG_PRINTK
3371a16736SNamhyung Kim /*
3471a16736SNamhyung Kim  * ratelimit state to be used in DMXXX_LIMIT().
3571a16736SNamhyung Kim  */
3671a16736SNamhyung Kim DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
3771a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_INTERVAL,
3871a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_BURST);
3971a16736SNamhyung Kim EXPORT_SYMBOL(dm_ratelimit_state);
4071a16736SNamhyung Kim #endif
4171a16736SNamhyung Kim 
4260935eb2SMilan Broz /*
4360935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
4460935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
4560935eb2SMilan Broz  */
4660935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
4760935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
4860935eb2SMilan Broz 
491da177e4SLinus Torvalds static const char *_name = DM_NAME;
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds static unsigned int major = 0;
521da177e4SLinus Torvalds static unsigned int _major = 0;
531da177e4SLinus Torvalds 
54d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr);
55d15b774cSAlasdair G Kergon 
56f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
572c140a24SMikulas Patocka 
582c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w);
592c140a24SMikulas Patocka 
602c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
612c140a24SMikulas Patocka 
62acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue;
63acfe0ad7SMikulas Patocka 
641da177e4SLinus Torvalds /*
658fbf26adSKiyoshi Ueda  * For bio-based dm.
661da177e4SLinus Torvalds  * One of these is allocated per bio.
671da177e4SLinus Torvalds  */
681da177e4SLinus Torvalds struct dm_io {
691da177e4SLinus Torvalds 	struct mapped_device *md;
701da177e4SLinus Torvalds 	int error;
711da177e4SLinus Torvalds 	atomic_t io_count;
726ae2fa67SRichard Kennedy 	struct bio *bio;
733eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
74f88fb981SKiyoshi Ueda 	spinlock_t endio_lock;
75fd2ed4d2SMikulas Patocka 	struct dm_stats_aux stats_aux;
761da177e4SLinus Torvalds };
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds /*
798fbf26adSKiyoshi Ueda  * For request-based dm.
808fbf26adSKiyoshi Ueda  * One of these is allocated per request.
818fbf26adSKiyoshi Ueda  */
828fbf26adSKiyoshi Ueda struct dm_rq_target_io {
838fbf26adSKiyoshi Ueda 	struct mapped_device *md;
848fbf26adSKiyoshi Ueda 	struct dm_target *ti;
851ae49ea2SMike Snitzer 	struct request *orig, *clone;
862eb6e1e3SKeith Busch 	struct kthread_work work;
878fbf26adSKiyoshi Ueda 	int error;
888fbf26adSKiyoshi Ueda 	union map_info info;
89e262f347SMikulas Patocka 	struct dm_stats_aux stats_aux;
90e262f347SMikulas Patocka 	unsigned long duration_jiffies;
91e262f347SMikulas Patocka 	unsigned n_sectors;
928fbf26adSKiyoshi Ueda };
938fbf26adSKiyoshi Ueda 
948fbf26adSKiyoshi Ueda /*
9594818742SKent Overstreet  * For request-based dm - the bio clones we allocate are embedded in these
9694818742SKent Overstreet  * structs.
9794818742SKent Overstreet  *
9894818742SKent Overstreet  * We allocate these with bio_alloc_bioset, using the front_pad parameter when
9994818742SKent Overstreet  * the bioset is created - this means the bio has to come at the end of the
10094818742SKent Overstreet  * struct.
1018fbf26adSKiyoshi Ueda  */
1028fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info {
1038fbf26adSKiyoshi Ueda 	struct bio *orig;
104cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio;
10594818742SKent Overstreet 	struct bio clone;
1068fbf26adSKiyoshi Ueda };
1078fbf26adSKiyoshi Ueda 
108cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq)
109cec47e3dSKiyoshi Ueda {
110cec47e3dSKiyoshi Ueda 	if (rq && rq->end_io_data)
111cec47e3dSKiyoshi Ueda 		return &((struct dm_rq_target_io *)rq->end_io_data)->info;
112cec47e3dSKiyoshi Ueda 	return NULL;
113cec47e3dSKiyoshi Ueda }
114cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
115cec47e3dSKiyoshi Ueda 
116ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
117ba61fdd1SJeff Mahoney 
1181da177e4SLinus Torvalds /*
1191da177e4SLinus Torvalds  * Bits for the md->flags field.
1201da177e4SLinus Torvalds  */
1211eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
1221da177e4SLinus Torvalds #define DMF_SUSPENDED 1
123aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
124fba9f90eSJeff Mahoney #define DMF_FREEING 3
1255c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
1262e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
127*8ae12666SKent Overstreet #define DMF_DEFERRED_REMOVE 6
128*8ae12666SKent Overstreet #define DMF_SUSPENDED_INTERNALLY 7
1291da177e4SLinus Torvalds 
130304f3f6aSMilan Broz /*
13183d5e5b0SMikulas Patocka  * A dummy definition to make RCU happy.
13283d5e5b0SMikulas Patocka  * struct dm_table should never be dereferenced in this file.
13383d5e5b0SMikulas Patocka  */
13483d5e5b0SMikulas Patocka struct dm_table {
13583d5e5b0SMikulas Patocka 	int undefined__;
13683d5e5b0SMikulas Patocka };
13783d5e5b0SMikulas Patocka 
13883d5e5b0SMikulas Patocka /*
139304f3f6aSMilan Broz  * Work processed by per-device workqueue.
140304f3f6aSMilan Broz  */
1411da177e4SLinus Torvalds struct mapped_device {
14283d5e5b0SMikulas Patocka 	struct srcu_struct io_barrier;
143e61290a4SDaniel Walker 	struct mutex suspend_lock;
1441da177e4SLinus Torvalds 	atomic_t holders;
1455c6bd75dSAlasdair G Kergon 	atomic_t open_count;
1461da177e4SLinus Torvalds 
1472a7faeb1SMikulas Patocka 	/*
1482a7faeb1SMikulas Patocka 	 * The current mapping.
1492a7faeb1SMikulas Patocka 	 * Use dm_get_live_table{_fast} or take suspend_lock for
1502a7faeb1SMikulas Patocka 	 * dereference.
1512a7faeb1SMikulas Patocka 	 */
1526fa99520SPranith Kumar 	struct dm_table __rcu *map;
1532a7faeb1SMikulas Patocka 
15486f1152bSBenjamin Marzinski 	struct list_head table_devices;
15586f1152bSBenjamin Marzinski 	struct mutex table_devices_lock;
15686f1152bSBenjamin Marzinski 
1571da177e4SLinus Torvalds 	unsigned long flags;
1581da177e4SLinus Torvalds 
159165125e1SJens Axboe 	struct request_queue *queue;
160a5664dadSMike Snitzer 	unsigned type;
1614a0b4ddfSMike Snitzer 	/* Protect queue and type against concurrent access. */
162a5664dadSMike Snitzer 	struct mutex type_lock;
163a5664dadSMike Snitzer 
16436a0456fSAlasdair G Kergon 	struct target_type *immutable_target_type;
16536a0456fSAlasdair G Kergon 
1661da177e4SLinus Torvalds 	struct gendisk *disk;
1677e51f257SMike Anderson 	char name[16];
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	void *interface_ptr;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	/*
1721da177e4SLinus Torvalds 	 * A list of ios that arrived while we were suspended.
1731da177e4SLinus Torvalds 	 */
174316d315bSNikanth Karthikesan 	atomic_t pending[2];
1751da177e4SLinus Torvalds 	wait_queue_head_t wait;
17653d5914fSMikulas Patocka 	struct work_struct work;
1771da177e4SLinus Torvalds 	struct bio_list deferred;
178022c2611SMikulas Patocka 	spinlock_t deferred_lock;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	/*
18129e4013dSTejun Heo 	 * Processing queue (flush)
182304f3f6aSMilan Broz 	 */
183304f3f6aSMilan Broz 	struct workqueue_struct *wq;
184304f3f6aSMilan Broz 
185304f3f6aSMilan Broz 	/*
1861da177e4SLinus Torvalds 	 * io objects are allocated from here.
1871da177e4SLinus Torvalds 	 */
1881da177e4SLinus Torvalds 	mempool_t *io_pool;
1891ae49ea2SMike Snitzer 	mempool_t *rq_pool;
1901da177e4SLinus Torvalds 
1919faf400fSStefan Bader 	struct bio_set *bs;
1929faf400fSStefan Bader 
1931da177e4SLinus Torvalds 	/*
1941da177e4SLinus Torvalds 	 * Event handling.
1951da177e4SLinus Torvalds 	 */
1961da177e4SLinus Torvalds 	atomic_t event_nr;
1971da177e4SLinus Torvalds 	wait_queue_head_t eventq;
1987a8c3d3bSMike Anderson 	atomic_t uevent_seq;
1997a8c3d3bSMike Anderson 	struct list_head uevent_list;
2007a8c3d3bSMike Anderson 	spinlock_t uevent_lock; /* Protect access to uevent_list */
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	/*
2031da177e4SLinus Torvalds 	 * freeze/thaw support require holding onto a super block
2041da177e4SLinus Torvalds 	 */
2051da177e4SLinus Torvalds 	struct super_block *frozen_sb;
206db8fef4fSMikulas Patocka 	struct block_device *bdev;
2073ac51e74SDarrick J. Wong 
2083ac51e74SDarrick J. Wong 	/* forced geometry settings */
2093ac51e74SDarrick J. Wong 	struct hd_geometry geometry;
210784aae73SMilan Broz 
2112995fa78SMikulas Patocka 	/* kobject and completion */
2122995fa78SMikulas Patocka 	struct dm_kobject_holder kobj_holder;
213be35f486SMikulas Patocka 
214d87f4c14STejun Heo 	/* zero-length flush that will be cloned and submitted to targets */
215d87f4c14STejun Heo 	struct bio flush_bio;
216fd2ed4d2SMikulas Patocka 
21796b26c8cSMikulas Patocka 	/* the number of internal suspends */
21896b26c8cSMikulas Patocka 	unsigned internal_suspend_count;
21996b26c8cSMikulas Patocka 
220fd2ed4d2SMikulas Patocka 	struct dm_stats stats;
2212eb6e1e3SKeith Busch 
2222eb6e1e3SKeith Busch 	struct kthread_worker kworker;
2232eb6e1e3SKeith Busch 	struct task_struct *kworker_task;
224de3ec86dSMike Snitzer 
225de3ec86dSMike Snitzer 	/* for request-based merge heuristic in dm_request_fn() */
2260ce65797SMike Snitzer 	unsigned seq_rq_merge_deadline_usecs;
227de3ec86dSMike Snitzer 	int last_rq_rw;
2280ce65797SMike Snitzer 	sector_t last_rq_pos;
2290ce65797SMike Snitzer 	ktime_t last_rq_start_time;
230bfebd1cdSMike Snitzer 
231bfebd1cdSMike Snitzer 	/* for blk-mq request-based DM support */
232bfebd1cdSMike Snitzer 	struct blk_mq_tag_set tag_set;
23317e149b8SMike Snitzer 	bool use_blk_mq;
2341da177e4SLinus Torvalds };
2351da177e4SLinus Torvalds 
23617e149b8SMike Snitzer #ifdef CONFIG_DM_MQ_DEFAULT
23717e149b8SMike Snitzer static bool use_blk_mq = true;
23817e149b8SMike Snitzer #else
23917e149b8SMike Snitzer static bool use_blk_mq = false;
24017e149b8SMike Snitzer #endif
24117e149b8SMike Snitzer 
24217e149b8SMike Snitzer bool dm_use_blk_mq(struct mapped_device *md)
24317e149b8SMike Snitzer {
24417e149b8SMike Snitzer 	return md->use_blk_mq;
24517e149b8SMike Snitzer }
24617e149b8SMike Snitzer 
247e6ee8c0bSKiyoshi Ueda /*
248e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
249e6ee8c0bSKiyoshi Ueda  */
250e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
251e6ee8c0bSKiyoshi Ueda 	mempool_t *io_pool;
2521ae49ea2SMike Snitzer 	mempool_t *rq_pool;
253e6ee8c0bSKiyoshi Ueda 	struct bio_set *bs;
254e6ee8c0bSKiyoshi Ueda };
255e6ee8c0bSKiyoshi Ueda 
25686f1152bSBenjamin Marzinski struct table_device {
25786f1152bSBenjamin Marzinski 	struct list_head list;
25886f1152bSBenjamin Marzinski 	atomic_t count;
25986f1152bSBenjamin Marzinski 	struct dm_dev dm_dev;
26086f1152bSBenjamin Marzinski };
26186f1152bSBenjamin Marzinski 
2626cfa5857SMike Snitzer #define RESERVED_BIO_BASED_IOS		16
2636cfa5857SMike Snitzer #define RESERVED_REQUEST_BASED_IOS	256
264f4790826SMike Snitzer #define RESERVED_MAX_IOS		1024
265e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
2668fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
2671ae49ea2SMike Snitzer static struct kmem_cache *_rq_cache;
26894818742SKent Overstreet 
269f4790826SMike Snitzer /*
270e8603136SMike Snitzer  * Bio-based DM's mempools' reserved IOs set by the user.
271e8603136SMike Snitzer  */
272e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
273e8603136SMike Snitzer 
274e8603136SMike Snitzer /*
275f4790826SMike Snitzer  * Request-based DM's mempools' reserved IOs set by the user.
276f4790826SMike Snitzer  */
277f4790826SMike Snitzer static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
278f4790826SMike Snitzer 
27909c2d531SMike Snitzer static unsigned __dm_get_module_param(unsigned *module_param,
280f4790826SMike Snitzer 				      unsigned def, unsigned max)
281f4790826SMike Snitzer {
28209c2d531SMike Snitzer 	unsigned param = ACCESS_ONCE(*module_param);
28309c2d531SMike Snitzer 	unsigned modified_param = 0;
284f4790826SMike Snitzer 
28509c2d531SMike Snitzer 	if (!param)
28609c2d531SMike Snitzer 		modified_param = def;
28709c2d531SMike Snitzer 	else if (param > max)
28809c2d531SMike Snitzer 		modified_param = max;
289f4790826SMike Snitzer 
29009c2d531SMike Snitzer 	if (modified_param) {
29109c2d531SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
29209c2d531SMike Snitzer 		param = modified_param;
293f4790826SMike Snitzer 	}
294f4790826SMike Snitzer 
29509c2d531SMike Snitzer 	return param;
296f4790826SMike Snitzer }
297f4790826SMike Snitzer 
298e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void)
299e8603136SMike Snitzer {
30009c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_bio_based_ios,
301e8603136SMike Snitzer 				     RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
302e8603136SMike Snitzer }
303e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
304e8603136SMike Snitzer 
305f4790826SMike Snitzer unsigned dm_get_reserved_rq_based_ios(void)
306f4790826SMike Snitzer {
30709c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_rq_based_ios,
308f4790826SMike Snitzer 				     RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
309f4790826SMike Snitzer }
310f4790826SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
311f4790826SMike Snitzer 
3121da177e4SLinus Torvalds static int __init local_init(void)
3131da177e4SLinus Torvalds {
31451157b4aSKiyoshi Ueda 	int r = -ENOMEM;
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
317028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
3181da177e4SLinus Torvalds 	if (!_io_cache)
31951157b4aSKiyoshi Ueda 		return r;
3201da177e4SLinus Torvalds 
3218fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
3228fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
323dba14160SMikulas Patocka 		goto out_free_io_cache;
3248fbf26adSKiyoshi Ueda 
3251ae49ea2SMike Snitzer 	_rq_cache = kmem_cache_create("dm_clone_request", sizeof(struct request),
3261ae49ea2SMike Snitzer 				      __alignof__(struct request), 0, NULL);
3271ae49ea2SMike Snitzer 	if (!_rq_cache)
3281ae49ea2SMike Snitzer 		goto out_free_rq_tio_cache;
3291ae49ea2SMike Snitzer 
33051e5b2bdSMike Anderson 	r = dm_uevent_init();
33151157b4aSKiyoshi Ueda 	if (r)
3321ae49ea2SMike Snitzer 		goto out_free_rq_cache;
33351e5b2bdSMike Anderson 
334acfe0ad7SMikulas Patocka 	deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
335acfe0ad7SMikulas Patocka 	if (!deferred_remove_workqueue) {
336acfe0ad7SMikulas Patocka 		r = -ENOMEM;
337acfe0ad7SMikulas Patocka 		goto out_uevent_exit;
338acfe0ad7SMikulas Patocka 	}
339acfe0ad7SMikulas Patocka 
3401da177e4SLinus Torvalds 	_major = major;
3411da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
34251157b4aSKiyoshi Ueda 	if (r < 0)
343acfe0ad7SMikulas Patocka 		goto out_free_workqueue;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	if (!_major)
3461da177e4SLinus Torvalds 		_major = r;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	return 0;
34951157b4aSKiyoshi Ueda 
350acfe0ad7SMikulas Patocka out_free_workqueue:
351acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
35251157b4aSKiyoshi Ueda out_uevent_exit:
35351157b4aSKiyoshi Ueda 	dm_uevent_exit();
3541ae49ea2SMike Snitzer out_free_rq_cache:
3551ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
3568fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
3578fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
35851157b4aSKiyoshi Ueda out_free_io_cache:
35951157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
36051157b4aSKiyoshi Ueda 
36151157b4aSKiyoshi Ueda 	return r;
3621da177e4SLinus Torvalds }
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds static void local_exit(void)
3651da177e4SLinus Torvalds {
3662c140a24SMikulas Patocka 	flush_scheduled_work();
367acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
3682c140a24SMikulas Patocka 
3691ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
3708fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
3711da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
37200d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
37351e5b2bdSMike Anderson 	dm_uevent_exit();
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	_major = 0;
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	DMINFO("cleaned up");
3781da177e4SLinus Torvalds }
3791da177e4SLinus Torvalds 
380b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
3811da177e4SLinus Torvalds 	local_init,
3821da177e4SLinus Torvalds 	dm_target_init,
3831da177e4SLinus Torvalds 	dm_linear_init,
3841da177e4SLinus Torvalds 	dm_stripe_init,
385952b3557SMikulas Patocka 	dm_io_init,
386945fa4d2SMikulas Patocka 	dm_kcopyd_init,
3871da177e4SLinus Torvalds 	dm_interface_init,
388fd2ed4d2SMikulas Patocka 	dm_statistics_init,
3891da177e4SLinus Torvalds };
3901da177e4SLinus Torvalds 
391b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
3921da177e4SLinus Torvalds 	local_exit,
3931da177e4SLinus Torvalds 	dm_target_exit,
3941da177e4SLinus Torvalds 	dm_linear_exit,
3951da177e4SLinus Torvalds 	dm_stripe_exit,
396952b3557SMikulas Patocka 	dm_io_exit,
397945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
3981da177e4SLinus Torvalds 	dm_interface_exit,
399fd2ed4d2SMikulas Patocka 	dm_statistics_exit,
4001da177e4SLinus Torvalds };
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds static int __init dm_init(void)
4031da177e4SLinus Torvalds {
4041da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	int r, i;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
4091da177e4SLinus Torvalds 		r = _inits[i]();
4101da177e4SLinus Torvalds 		if (r)
4111da177e4SLinus Torvalds 			goto bad;
4121da177e4SLinus Torvalds 	}
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	return 0;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds       bad:
4171da177e4SLinus Torvalds 	while (i--)
4181da177e4SLinus Torvalds 		_exits[i]();
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	return r;
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds static void __exit dm_exit(void)
4241da177e4SLinus Torvalds {
4251da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	while (i--)
4281da177e4SLinus Torvalds 		_exits[i]();
429d15b774cSAlasdair G Kergon 
430d15b774cSAlasdair G Kergon 	/*
431d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
432d15b774cSAlasdair G Kergon 	 */
433d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds /*
4371da177e4SLinus Torvalds  * Block device functions
4381da177e4SLinus Torvalds  */
439432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
440432a212cSMike Anderson {
441432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
442432a212cSMike Anderson }
443432a212cSMike Anderson 
444fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
4451da177e4SLinus Torvalds {
4461da177e4SLinus Torvalds 	struct mapped_device *md;
4471da177e4SLinus Torvalds 
448fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
449fba9f90eSJeff Mahoney 
450fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
451fba9f90eSJeff Mahoney 	if (!md)
452fba9f90eSJeff Mahoney 		goto out;
453fba9f90eSJeff Mahoney 
4545c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
455432a212cSMike Anderson 	    dm_deleting_md(md)) {
456fba9f90eSJeff Mahoney 		md = NULL;
457fba9f90eSJeff Mahoney 		goto out;
458fba9f90eSJeff Mahoney 	}
459fba9f90eSJeff Mahoney 
4601da177e4SLinus Torvalds 	dm_get(md);
4615c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
462fba9f90eSJeff Mahoney out:
463fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
464fba9f90eSJeff Mahoney 
465fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds 
468db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode)
4691da177e4SLinus Torvalds {
47063a4f065SMike Snitzer 	struct mapped_device *md;
4716e9624b8SArnd Bergmann 
4724a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
4734a1aeb98SMilan Broz 
47463a4f065SMike Snitzer 	md = disk->private_data;
47563a4f065SMike Snitzer 	if (WARN_ON(!md))
47663a4f065SMike Snitzer 		goto out;
47763a4f065SMike Snitzer 
4782c140a24SMikulas Patocka 	if (atomic_dec_and_test(&md->open_count) &&
4792c140a24SMikulas Patocka 	    (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
480acfe0ad7SMikulas Patocka 		queue_work(deferred_remove_workqueue, &deferred_remove_work);
4812c140a24SMikulas Patocka 
4821da177e4SLinus Torvalds 	dm_put(md);
48363a4f065SMike Snitzer out:
4844a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
4875c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
4885c6bd75dSAlasdair G Kergon {
4895c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
4905c6bd75dSAlasdair G Kergon }
4915c6bd75dSAlasdair G Kergon 
4925c6bd75dSAlasdair G Kergon /*
4935c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
4945c6bd75dSAlasdair G Kergon  */
4952c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
4965c6bd75dSAlasdair G Kergon {
4975c6bd75dSAlasdair G Kergon 	int r = 0;
4985c6bd75dSAlasdair G Kergon 
4995c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
5005c6bd75dSAlasdair G Kergon 
5012c140a24SMikulas Patocka 	if (dm_open_count(md)) {
5025c6bd75dSAlasdair G Kergon 		r = -EBUSY;
5032c140a24SMikulas Patocka 		if (mark_deferred)
5042c140a24SMikulas Patocka 			set_bit(DMF_DEFERRED_REMOVE, &md->flags);
5052c140a24SMikulas Patocka 	} else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
5062c140a24SMikulas Patocka 		r = -EEXIST;
5075c6bd75dSAlasdair G Kergon 	else
5085c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
5095c6bd75dSAlasdair G Kergon 
5105c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
5115c6bd75dSAlasdair G Kergon 
5125c6bd75dSAlasdair G Kergon 	return r;
5135c6bd75dSAlasdair G Kergon }
5145c6bd75dSAlasdair G Kergon 
5152c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md)
5162c140a24SMikulas Patocka {
5172c140a24SMikulas Patocka 	int r = 0;
5182c140a24SMikulas Patocka 
5192c140a24SMikulas Patocka 	spin_lock(&_minor_lock);
5202c140a24SMikulas Patocka 
5212c140a24SMikulas Patocka 	if (test_bit(DMF_DELETING, &md->flags))
5222c140a24SMikulas Patocka 		r = -EBUSY;
5232c140a24SMikulas Patocka 	else
5242c140a24SMikulas Patocka 		clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
5252c140a24SMikulas Patocka 
5262c140a24SMikulas Patocka 	spin_unlock(&_minor_lock);
5272c140a24SMikulas Patocka 
5282c140a24SMikulas Patocka 	return r;
5292c140a24SMikulas Patocka }
5302c140a24SMikulas Patocka 
5312c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w)
5322c140a24SMikulas Patocka {
5332c140a24SMikulas Patocka 	dm_deferred_remove();
5342c140a24SMikulas Patocka }
5352c140a24SMikulas Patocka 
536fd2ed4d2SMikulas Patocka sector_t dm_get_size(struct mapped_device *md)
537fd2ed4d2SMikulas Patocka {
538fd2ed4d2SMikulas Patocka 	return get_capacity(md->disk);
539fd2ed4d2SMikulas Patocka }
540fd2ed4d2SMikulas Patocka 
5419974fa2cSMike Snitzer struct request_queue *dm_get_md_queue(struct mapped_device *md)
5429974fa2cSMike Snitzer {
5439974fa2cSMike Snitzer 	return md->queue;
5449974fa2cSMike Snitzer }
5459974fa2cSMike Snitzer 
546fd2ed4d2SMikulas Patocka struct dm_stats *dm_get_stats(struct mapped_device *md)
547fd2ed4d2SMikulas Patocka {
548fd2ed4d2SMikulas Patocka 	return &md->stats;
549fd2ed4d2SMikulas Patocka }
550fd2ed4d2SMikulas Patocka 
5513ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5523ac51e74SDarrick J. Wong {
5533ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
5543ac51e74SDarrick J. Wong 
5553ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
5563ac51e74SDarrick J. Wong }
5573ac51e74SDarrick J. Wong 
558fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
559aa129a22SMilan Broz 			unsigned int cmd, unsigned long arg)
560aa129a22SMilan Broz {
561fe5f9f2cSAl Viro 	struct mapped_device *md = bdev->bd_disk->private_data;
56283d5e5b0SMikulas Patocka 	int srcu_idx;
5636c182cd8SHannes Reinecke 	struct dm_table *map;
564aa129a22SMilan Broz 	struct dm_target *tgt;
565aa129a22SMilan Broz 	int r = -ENOTTY;
566aa129a22SMilan Broz 
5676c182cd8SHannes Reinecke retry:
56883d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
56983d5e5b0SMikulas Patocka 
570aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
571aa129a22SMilan Broz 		goto out;
572aa129a22SMilan Broz 
573aa129a22SMilan Broz 	/* We only support devices that have a single target */
574aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
575aa129a22SMilan Broz 		goto out;
576aa129a22SMilan Broz 
577aa129a22SMilan Broz 	tgt = dm_table_get_target(map, 0);
5784d341d82SMike Snitzer 	if (!tgt->type->ioctl)
5794d341d82SMike Snitzer 		goto out;
580aa129a22SMilan Broz 
5814f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
582aa129a22SMilan Broz 		r = -EAGAIN;
583aa129a22SMilan Broz 		goto out;
584aa129a22SMilan Broz 	}
585aa129a22SMilan Broz 
586647b3d00SAl Viro 	r = tgt->type->ioctl(tgt, cmd, arg);
587aa129a22SMilan Broz 
588aa129a22SMilan Broz out:
58983d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
590aa129a22SMilan Broz 
5916c182cd8SHannes Reinecke 	if (r == -ENOTCONN) {
5926c182cd8SHannes Reinecke 		msleep(10);
5936c182cd8SHannes Reinecke 		goto retry;
5946c182cd8SHannes Reinecke 	}
5956c182cd8SHannes Reinecke 
596aa129a22SMilan Broz 	return r;
597aa129a22SMilan Broz }
598aa129a22SMilan Broz 
599028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
6001da177e4SLinus Torvalds {
6011da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
6021da177e4SLinus Torvalds }
6031da177e4SLinus Torvalds 
604028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
6051da177e4SLinus Torvalds {
6061da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
609028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
6101da177e4SLinus Torvalds {
611dba14160SMikulas Patocka 	bio_put(&tio->clone);
6121da177e4SLinus Torvalds }
6131da177e4SLinus Torvalds 
61408885643SKiyoshi Ueda static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
61508885643SKiyoshi Ueda 					    gfp_t gfp_mask)
616cec47e3dSKiyoshi Ueda {
6175f015204SJun'ichi Nomura 	return mempool_alloc(md->io_pool, gfp_mask);
618cec47e3dSKiyoshi Ueda }
619cec47e3dSKiyoshi Ueda 
620cec47e3dSKiyoshi Ueda static void free_rq_tio(struct dm_rq_target_io *tio)
621cec47e3dSKiyoshi Ueda {
6225f015204SJun'ichi Nomura 	mempool_free(tio, tio->md->io_pool);
623cec47e3dSKiyoshi Ueda }
624cec47e3dSKiyoshi Ueda 
6251ae49ea2SMike Snitzer static struct request *alloc_clone_request(struct mapped_device *md,
6261ae49ea2SMike Snitzer 					   gfp_t gfp_mask)
6271ae49ea2SMike Snitzer {
6281ae49ea2SMike Snitzer 	return mempool_alloc(md->rq_pool, gfp_mask);
6291ae49ea2SMike Snitzer }
6301ae49ea2SMike Snitzer 
6311ae49ea2SMike Snitzer static void free_clone_request(struct mapped_device *md, struct request *rq)
6321ae49ea2SMike Snitzer {
6331ae49ea2SMike Snitzer 	mempool_free(rq, md->rq_pool);
6341ae49ea2SMike Snitzer }
6351ae49ea2SMike Snitzer 
63690abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md)
63790abb8c4SKiyoshi Ueda {
63890abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
63990abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
64090abb8c4SKiyoshi Ueda }
64190abb8c4SKiyoshi Ueda 
6423eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
6433eaf840eSJun'ichi "Nick" Nomura {
6443eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
645fd2ed4d2SMikulas Patocka 	struct bio *bio = io->bio;
646c9959059STejun Heo 	int cpu;
647fd2ed4d2SMikulas Patocka 	int rw = bio_data_dir(bio);
6483eaf840eSJun'ichi "Nick" Nomura 
6493eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
6503eaf840eSJun'ichi "Nick" Nomura 
651074a7acaSTejun Heo 	cpu = part_stat_lock();
652074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
653074a7acaSTejun Heo 	part_stat_unlock();
6541e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw],
6551e9bb880SShaohua Li 		atomic_inc_return(&md->pending[rw]));
656fd2ed4d2SMikulas Patocka 
657fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
6584f024f37SKent Overstreet 		dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
659fd2ed4d2SMikulas Patocka 				    bio_sectors(bio), false, 0, &io->stats_aux);
6603eaf840eSJun'ichi "Nick" Nomura }
6613eaf840eSJun'ichi "Nick" Nomura 
662d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
6633eaf840eSJun'ichi "Nick" Nomura {
6643eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
6653eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
6663eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
66718c0b223SGu Zheng 	int pending;
6683eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
6693eaf840eSJun'ichi "Nick" Nomura 
67018c0b223SGu Zheng 	generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time);
6713eaf840eSJun'ichi "Nick" Nomura 
672fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
6734f024f37SKent Overstreet 		dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
674fd2ed4d2SMikulas Patocka 				    bio_sectors(bio), true, duration, &io->stats_aux);
675fd2ed4d2SMikulas Patocka 
676af7e466aSMikulas Patocka 	/*
677af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
678d87f4c14STejun Heo 	 * a flush.
679af7e466aSMikulas Patocka 	 */
6801e9bb880SShaohua Li 	pending = atomic_dec_return(&md->pending[rw]);
6811e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
682316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
6833eaf840eSJun'ichi "Nick" Nomura 
684d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
685d221d2e7SMikulas Patocka 	if (!pending)
686d221d2e7SMikulas Patocka 		wake_up(&md->wait);
6873eaf840eSJun'ichi "Nick" Nomura }
6883eaf840eSJun'ichi "Nick" Nomura 
6891da177e4SLinus Torvalds /*
6901da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
6911da177e4SLinus Torvalds  */
69292c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
6931da177e4SLinus Torvalds {
69405447420SKiyoshi Ueda 	unsigned long flags;
6951da177e4SLinus Torvalds 
69605447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
6971da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
69805447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
69992c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
7001da177e4SLinus Torvalds }
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds /*
7031da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
7041da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
70583d5e5b0SMikulas Patocka  * dm_put_live_table() when finished.
7061da177e4SLinus Torvalds  */
70783d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
7081da177e4SLinus Torvalds {
70983d5e5b0SMikulas Patocka 	*srcu_idx = srcu_read_lock(&md->io_barrier);
7101da177e4SLinus Torvalds 
71183d5e5b0SMikulas Patocka 	return srcu_dereference(md->map, &md->io_barrier);
71283d5e5b0SMikulas Patocka }
7131da177e4SLinus Torvalds 
71483d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
71583d5e5b0SMikulas Patocka {
71683d5e5b0SMikulas Patocka 	srcu_read_unlock(&md->io_barrier, srcu_idx);
71783d5e5b0SMikulas Patocka }
71883d5e5b0SMikulas Patocka 
71983d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md)
72083d5e5b0SMikulas Patocka {
72183d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
72283d5e5b0SMikulas Patocka 	synchronize_rcu_expedited();
72383d5e5b0SMikulas Patocka }
72483d5e5b0SMikulas Patocka 
72583d5e5b0SMikulas Patocka /*
72683d5e5b0SMikulas Patocka  * A fast alternative to dm_get_live_table/dm_put_live_table.
72783d5e5b0SMikulas Patocka  * The caller must not block between these two functions.
72883d5e5b0SMikulas Patocka  */
72983d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
73083d5e5b0SMikulas Patocka {
73183d5e5b0SMikulas Patocka 	rcu_read_lock();
73283d5e5b0SMikulas Patocka 	return rcu_dereference(md->map);
73383d5e5b0SMikulas Patocka }
73483d5e5b0SMikulas Patocka 
73583d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
73683d5e5b0SMikulas Patocka {
73783d5e5b0SMikulas Patocka 	rcu_read_unlock();
7381da177e4SLinus Torvalds }
7391da177e4SLinus Torvalds 
7403ac51e74SDarrick J. Wong /*
74186f1152bSBenjamin Marzinski  * Open a table device so we can use it as a map destination.
74286f1152bSBenjamin Marzinski  */
74386f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev,
74486f1152bSBenjamin Marzinski 			     struct mapped_device *md)
74586f1152bSBenjamin Marzinski {
74686f1152bSBenjamin Marzinski 	static char *_claim_ptr = "I belong to device-mapper";
74786f1152bSBenjamin Marzinski 	struct block_device *bdev;
74886f1152bSBenjamin Marzinski 
74986f1152bSBenjamin Marzinski 	int r;
75086f1152bSBenjamin Marzinski 
75186f1152bSBenjamin Marzinski 	BUG_ON(td->dm_dev.bdev);
75286f1152bSBenjamin Marzinski 
75386f1152bSBenjamin Marzinski 	bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
75486f1152bSBenjamin Marzinski 	if (IS_ERR(bdev))
75586f1152bSBenjamin Marzinski 		return PTR_ERR(bdev);
75686f1152bSBenjamin Marzinski 
75786f1152bSBenjamin Marzinski 	r = bd_link_disk_holder(bdev, dm_disk(md));
75886f1152bSBenjamin Marzinski 	if (r) {
75986f1152bSBenjamin Marzinski 		blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
76086f1152bSBenjamin Marzinski 		return r;
76186f1152bSBenjamin Marzinski 	}
76286f1152bSBenjamin Marzinski 
76386f1152bSBenjamin Marzinski 	td->dm_dev.bdev = bdev;
76486f1152bSBenjamin Marzinski 	return 0;
76586f1152bSBenjamin Marzinski }
76686f1152bSBenjamin Marzinski 
76786f1152bSBenjamin Marzinski /*
76886f1152bSBenjamin Marzinski  * Close a table device that we've been using.
76986f1152bSBenjamin Marzinski  */
77086f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md)
77186f1152bSBenjamin Marzinski {
77286f1152bSBenjamin Marzinski 	if (!td->dm_dev.bdev)
77386f1152bSBenjamin Marzinski 		return;
77486f1152bSBenjamin Marzinski 
77586f1152bSBenjamin Marzinski 	bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
77686f1152bSBenjamin Marzinski 	blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
77786f1152bSBenjamin Marzinski 	td->dm_dev.bdev = NULL;
77886f1152bSBenjamin Marzinski }
77986f1152bSBenjamin Marzinski 
78086f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev,
78186f1152bSBenjamin Marzinski 					      fmode_t mode) {
78286f1152bSBenjamin Marzinski 	struct table_device *td;
78386f1152bSBenjamin Marzinski 
78486f1152bSBenjamin Marzinski 	list_for_each_entry(td, l, list)
78586f1152bSBenjamin Marzinski 		if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
78686f1152bSBenjamin Marzinski 			return td;
78786f1152bSBenjamin Marzinski 
78886f1152bSBenjamin Marzinski 	return NULL;
78986f1152bSBenjamin Marzinski }
79086f1152bSBenjamin Marzinski 
79186f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
79286f1152bSBenjamin Marzinski 			struct dm_dev **result) {
79386f1152bSBenjamin Marzinski 	int r;
79486f1152bSBenjamin Marzinski 	struct table_device *td;
79586f1152bSBenjamin Marzinski 
79686f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
79786f1152bSBenjamin Marzinski 	td = find_table_device(&md->table_devices, dev, mode);
79886f1152bSBenjamin Marzinski 	if (!td) {
79986f1152bSBenjamin Marzinski 		td = kmalloc(sizeof(*td), GFP_KERNEL);
80086f1152bSBenjamin Marzinski 		if (!td) {
80186f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
80286f1152bSBenjamin Marzinski 			return -ENOMEM;
80386f1152bSBenjamin Marzinski 		}
80486f1152bSBenjamin Marzinski 
80586f1152bSBenjamin Marzinski 		td->dm_dev.mode = mode;
80686f1152bSBenjamin Marzinski 		td->dm_dev.bdev = NULL;
80786f1152bSBenjamin Marzinski 
80886f1152bSBenjamin Marzinski 		if ((r = open_table_device(td, dev, md))) {
80986f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
81086f1152bSBenjamin Marzinski 			kfree(td);
81186f1152bSBenjamin Marzinski 			return r;
81286f1152bSBenjamin Marzinski 		}
81386f1152bSBenjamin Marzinski 
81486f1152bSBenjamin Marzinski 		format_dev_t(td->dm_dev.name, dev);
81586f1152bSBenjamin Marzinski 
81686f1152bSBenjamin Marzinski 		atomic_set(&td->count, 0);
81786f1152bSBenjamin Marzinski 		list_add(&td->list, &md->table_devices);
81886f1152bSBenjamin Marzinski 	}
81986f1152bSBenjamin Marzinski 	atomic_inc(&td->count);
82086f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
82186f1152bSBenjamin Marzinski 
82286f1152bSBenjamin Marzinski 	*result = &td->dm_dev;
82386f1152bSBenjamin Marzinski 	return 0;
82486f1152bSBenjamin Marzinski }
82586f1152bSBenjamin Marzinski EXPORT_SYMBOL_GPL(dm_get_table_device);
82686f1152bSBenjamin Marzinski 
82786f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
82886f1152bSBenjamin Marzinski {
82986f1152bSBenjamin Marzinski 	struct table_device *td = container_of(d, struct table_device, dm_dev);
83086f1152bSBenjamin Marzinski 
83186f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
83286f1152bSBenjamin Marzinski 	if (atomic_dec_and_test(&td->count)) {
83386f1152bSBenjamin Marzinski 		close_table_device(td, md);
83486f1152bSBenjamin Marzinski 		list_del(&td->list);
83586f1152bSBenjamin Marzinski 		kfree(td);
83686f1152bSBenjamin Marzinski 	}
83786f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
83886f1152bSBenjamin Marzinski }
83986f1152bSBenjamin Marzinski EXPORT_SYMBOL(dm_put_table_device);
84086f1152bSBenjamin Marzinski 
84186f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices)
84286f1152bSBenjamin Marzinski {
84386f1152bSBenjamin Marzinski 	struct list_head *tmp, *next;
84486f1152bSBenjamin Marzinski 
84586f1152bSBenjamin Marzinski 	list_for_each_safe(tmp, next, devices) {
84686f1152bSBenjamin Marzinski 		struct table_device *td = list_entry(tmp, struct table_device, list);
84786f1152bSBenjamin Marzinski 
84886f1152bSBenjamin Marzinski 		DMWARN("dm_destroy: %s still exists with %d references",
84986f1152bSBenjamin Marzinski 		       td->dm_dev.name, atomic_read(&td->count));
85086f1152bSBenjamin Marzinski 		kfree(td);
85186f1152bSBenjamin Marzinski 	}
85286f1152bSBenjamin Marzinski }
85386f1152bSBenjamin Marzinski 
85486f1152bSBenjamin Marzinski /*
8553ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
8563ac51e74SDarrick J. Wong  */
8573ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
8583ac51e74SDarrick J. Wong {
8593ac51e74SDarrick J. Wong 	*geo = md->geometry;
8603ac51e74SDarrick J. Wong 
8613ac51e74SDarrick J. Wong 	return 0;
8623ac51e74SDarrick J. Wong }
8633ac51e74SDarrick J. Wong 
8643ac51e74SDarrick J. Wong /*
8653ac51e74SDarrick J. Wong  * Set the geometry of a device.
8663ac51e74SDarrick J. Wong  */
8673ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
8683ac51e74SDarrick J. Wong {
8693ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
8703ac51e74SDarrick J. Wong 
8713ac51e74SDarrick J. Wong 	if (geo->start > sz) {
8723ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
8733ac51e74SDarrick J. Wong 		return -EINVAL;
8743ac51e74SDarrick J. Wong 	}
8753ac51e74SDarrick J. Wong 
8763ac51e74SDarrick J. Wong 	md->geometry = *geo;
8773ac51e74SDarrick J. Wong 
8783ac51e74SDarrick J. Wong 	return 0;
8793ac51e74SDarrick J. Wong }
8803ac51e74SDarrick J. Wong 
8811da177e4SLinus Torvalds /*-----------------------------------------------------------------
8821da177e4SLinus Torvalds  * CRUD START:
8831da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
8841da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
8851da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
8861da177e4SLinus Torvalds  *   interests of getting something for people to use I give
8871da177e4SLinus Torvalds  *   you this clearly demarcated crap.
8881da177e4SLinus Torvalds  *---------------------------------------------------------------*/
8891da177e4SLinus Torvalds 
8902e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
8912e93ccc1SKiyoshi Ueda {
8922e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
8932e93ccc1SKiyoshi Ueda }
8942e93ccc1SKiyoshi Ueda 
8951da177e4SLinus Torvalds /*
8961da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
8971da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
8981da177e4SLinus Torvalds  */
899858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
9001da177e4SLinus Torvalds {
9012e93ccc1SKiyoshi Ueda 	unsigned long flags;
902b35f8caaSMilan Broz 	int io_error;
903b35f8caaSMilan Broz 	struct bio *bio;
904b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
9052e93ccc1SKiyoshi Ueda 
9062e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
907f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
908f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
909f88fb981SKiyoshi Ueda 		if (!(io->error > 0 && __noflush_suspending(md)))
9101da177e4SLinus Torvalds 			io->error = error;
911f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
912f88fb981SKiyoshi Ueda 	}
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
9152e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
9162e93ccc1SKiyoshi Ueda 			/*
9172e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
9182e93ccc1SKiyoshi Ueda 			 */
919022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
9206a8736d1STejun Heo 			if (__noflush_suspending(md))
9216a8736d1STejun Heo 				bio_list_add_head(&md->deferred, io->bio);
9226a8736d1STejun Heo 			else
9232e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
9242e93ccc1SKiyoshi Ueda 				io->error = -EIO;
925022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
9262e93ccc1SKiyoshi Ueda 		}
9272e93ccc1SKiyoshi Ueda 
928b35f8caaSMilan Broz 		io_error = io->error;
929b35f8caaSMilan Broz 		bio = io->bio;
930af7e466aSMikulas Patocka 		end_io_acct(io);
931a97f925aSMikulas Patocka 		free_io(md, io);
9321da177e4SLinus Torvalds 
9336a8736d1STejun Heo 		if (io_error == DM_ENDIO_REQUEUE)
9346a8736d1STejun Heo 			return;
9356a8736d1STejun Heo 
9364f024f37SKent Overstreet 		if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
9371da177e4SLinus Torvalds 			/*
9386a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
9396a8736d1STejun Heo 			 * without REQ_FLUSH.
9401da177e4SLinus Torvalds 			 */
9416a8736d1STejun Heo 			bio->bi_rw &= ~REQ_FLUSH;
9426a8736d1STejun Heo 			queue_io(md, bio);
9435f3ea37cSArnaldo Carvalho de Melo 		} else {
944b372d360SMike Snitzer 			/* done with normal IO or empty flush */
9450a82a8d1SLinus Torvalds 			trace_block_bio_complete(md->queue, bio, io_error);
9464246a0b6SChristoph Hellwig 			bio->bi_error = io_error;
9474246a0b6SChristoph Hellwig 			bio_endio(bio);
9482e93ccc1SKiyoshi Ueda 		}
9491da177e4SLinus Torvalds 	}
950af7e466aSMikulas Patocka }
9511da177e4SLinus Torvalds 
9527eee4ae2SMike Snitzer static void disable_write_same(struct mapped_device *md)
9537eee4ae2SMike Snitzer {
9547eee4ae2SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
9557eee4ae2SMike Snitzer 
9567eee4ae2SMike Snitzer 	/* device doesn't really support WRITE SAME, disable it */
9577eee4ae2SMike Snitzer 	limits->max_write_same_sectors = 0;
9587eee4ae2SMike Snitzer }
9597eee4ae2SMike Snitzer 
9604246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio)
9611da177e4SLinus Torvalds {
9624246a0b6SChristoph Hellwig 	int error = bio->bi_error;
9635164beceSzhendong chen 	int r = error;
964bfc6d41cSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
965b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
9669faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
9671da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	if (endio) {
9707de3ee57SMikulas Patocka 		r = endio(tio->ti, bio, error);
9712e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
9722e93ccc1SKiyoshi Ueda 			/*
9732e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
9742e93ccc1SKiyoshi Ueda 			 * in dec_pending().
9752e93ccc1SKiyoshi Ueda 			 */
9761da177e4SLinus Torvalds 			error = r;
97745cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
97845cbcd79SKiyoshi Ueda 			/* The target will handle the io */
9796712ecf8SNeilBrown 			return;
98045cbcd79SKiyoshi Ueda 		else if (r) {
98145cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
98245cbcd79SKiyoshi Ueda 			BUG();
98345cbcd79SKiyoshi Ueda 		}
9841da177e4SLinus Torvalds 	}
9851da177e4SLinus Torvalds 
9867eee4ae2SMike Snitzer 	if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) &&
9877eee4ae2SMike Snitzer 		     !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
9887eee4ae2SMike Snitzer 		disable_write_same(md);
9897eee4ae2SMike Snitzer 
9909faf400fSStefan Bader 	free_tio(md, tio);
991b35f8caaSMilan Broz 	dec_pending(io, error);
9921da177e4SLinus Torvalds }
9931da177e4SLinus Torvalds 
99478d8e58aSMike Snitzer /*
99578d8e58aSMike Snitzer  * Partial completion handling for request-based dm
99678d8e58aSMike Snitzer  */
9974246a0b6SChristoph Hellwig static void end_clone_bio(struct bio *clone)
99878d8e58aSMike Snitzer {
99978d8e58aSMike Snitzer 	struct dm_rq_clone_bio_info *info =
100078d8e58aSMike Snitzer 		container_of(clone, struct dm_rq_clone_bio_info, clone);
100178d8e58aSMike Snitzer 	struct dm_rq_target_io *tio = info->tio;
100278d8e58aSMike Snitzer 	struct bio *bio = info->orig;
100378d8e58aSMike Snitzer 	unsigned int nr_bytes = info->orig->bi_iter.bi_size;
100478d8e58aSMike Snitzer 
100578d8e58aSMike Snitzer 	bio_put(clone);
100678d8e58aSMike Snitzer 
100778d8e58aSMike Snitzer 	if (tio->error)
100878d8e58aSMike Snitzer 		/*
100978d8e58aSMike Snitzer 		 * An error has already been detected on the request.
101078d8e58aSMike Snitzer 		 * Once error occurred, just let clone->end_io() handle
101178d8e58aSMike Snitzer 		 * the remainder.
101278d8e58aSMike Snitzer 		 */
101378d8e58aSMike Snitzer 		return;
10144246a0b6SChristoph Hellwig 	else if (bio->bi_error) {
101578d8e58aSMike Snitzer 		/*
101678d8e58aSMike Snitzer 		 * Don't notice the error to the upper layer yet.
101778d8e58aSMike Snitzer 		 * The error handling decision is made by the target driver,
101878d8e58aSMike Snitzer 		 * when the request is completed.
101978d8e58aSMike Snitzer 		 */
10204246a0b6SChristoph Hellwig 		tio->error = bio->bi_error;
102178d8e58aSMike Snitzer 		return;
102278d8e58aSMike Snitzer 	}
102378d8e58aSMike Snitzer 
102478d8e58aSMike Snitzer 	/*
102578d8e58aSMike Snitzer 	 * I/O for the bio successfully completed.
102678d8e58aSMike Snitzer 	 * Notice the data completion to the upper layer.
102778d8e58aSMike Snitzer 	 */
102878d8e58aSMike Snitzer 
102978d8e58aSMike Snitzer 	/*
103078d8e58aSMike Snitzer 	 * bios are processed from the head of the list.
103178d8e58aSMike Snitzer 	 * So the completing bio should always be rq->bio.
103278d8e58aSMike Snitzer 	 * If it's not, something wrong is happening.
103378d8e58aSMike Snitzer 	 */
103478d8e58aSMike Snitzer 	if (tio->orig->bio != bio)
103578d8e58aSMike Snitzer 		DMERR("bio completion is going in the middle of the request");
103678d8e58aSMike Snitzer 
103778d8e58aSMike Snitzer 	/*
103878d8e58aSMike Snitzer 	 * Update the original request.
103978d8e58aSMike Snitzer 	 * Do not use blk_end_request() here, because it may complete
104078d8e58aSMike Snitzer 	 * the original request before the clone, and break the ordering.
104178d8e58aSMike Snitzer 	 */
104278d8e58aSMike Snitzer 	blk_update_request(tio->orig, 0, nr_bytes);
104378d8e58aSMike Snitzer }
104478d8e58aSMike Snitzer 
1045bfebd1cdSMike Snitzer static struct dm_rq_target_io *tio_from_request(struct request *rq)
1046bfebd1cdSMike Snitzer {
1047bfebd1cdSMike Snitzer 	return (rq->q->mq_ops ? blk_mq_rq_to_pdu(rq) : rq->special);
1048bfebd1cdSMike Snitzer }
1049bfebd1cdSMike Snitzer 
1050e262f347SMikulas Patocka static void rq_end_stats(struct mapped_device *md, struct request *orig)
1051e262f347SMikulas Patocka {
1052e262f347SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats))) {
1053e262f347SMikulas Patocka 		struct dm_rq_target_io *tio = tio_from_request(orig);
1054e262f347SMikulas Patocka 		tio->duration_jiffies = jiffies - tio->duration_jiffies;
1055e262f347SMikulas Patocka 		dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
1056e262f347SMikulas Patocka 				    tio->n_sectors, true, tio->duration_jiffies,
1057e262f347SMikulas Patocka 				    &tio->stats_aux);
1058e262f347SMikulas Patocka 	}
1059e262f347SMikulas Patocka }
1060e262f347SMikulas Patocka 
1061cec47e3dSKiyoshi Ueda /*
1062cec47e3dSKiyoshi Ueda  * Don't touch any member of the md after calling this function because
1063cec47e3dSKiyoshi Ueda  * the md may be freed in dm_put() at the end of this function.
1064cec47e3dSKiyoshi Ueda  * Or do dm_get() before calling this function and dm_put() later.
1065cec47e3dSKiyoshi Ueda  */
1066466d89a6SKeith Busch static void rq_completed(struct mapped_device *md, int rw, bool run_queue)
1067cec47e3dSKiyoshi Ueda {
10689a0e609eSMike Snitzer 	int nr_requests_pending;
10699a0e609eSMike Snitzer 
1070b4324feeSKiyoshi Ueda 	atomic_dec(&md->pending[rw]);
1071cec47e3dSKiyoshi Ueda 
1072cec47e3dSKiyoshi Ueda 	/* nudge anyone waiting on suspend queue */
10739a0e609eSMike Snitzer 	nr_requests_pending = md_in_flight(md);
10749a0e609eSMike Snitzer 	if (!nr_requests_pending)
1075cec47e3dSKiyoshi Ueda 		wake_up(&md->wait);
1076cec47e3dSKiyoshi Ueda 
1077a8c32a5cSJens Axboe 	/*
1078a8c32a5cSJens Axboe 	 * Run this off this callpath, as drivers could invoke end_io while
1079a8c32a5cSJens Axboe 	 * inside their request_fn (and holding the queue lock). Calling
1080a8c32a5cSJens Axboe 	 * back into ->request_fn() could deadlock attempting to grab the
1081a8c32a5cSJens Axboe 	 * queue lock again.
1082a8c32a5cSJens Axboe 	 */
10839a0e609eSMike Snitzer 	if (run_queue) {
1084bfebd1cdSMike Snitzer 		if (md->queue->mq_ops)
1085bfebd1cdSMike Snitzer 			blk_mq_run_hw_queues(md->queue, true);
1086bfebd1cdSMike Snitzer 		else if (!nr_requests_pending ||
10879a0e609eSMike Snitzer 			 (nr_requests_pending >= md->queue->nr_congestion_on))
1088a8c32a5cSJens Axboe 			blk_run_queue_async(md->queue);
10899a0e609eSMike Snitzer 	}
1090cec47e3dSKiyoshi Ueda 
1091cec47e3dSKiyoshi Ueda 	/*
1092cec47e3dSKiyoshi Ueda 	 * dm_put() must be at the end of this function. See the comment above
1093cec47e3dSKiyoshi Ueda 	 */
1094cec47e3dSKiyoshi Ueda 	dm_put(md);
1095cec47e3dSKiyoshi Ueda }
1096cec47e3dSKiyoshi Ueda 
1097e5d8de32SMike Snitzer static void free_rq_clone(struct request *clone)
1098a77e28c7SKiyoshi Ueda {
1099a77e28c7SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1100bfebd1cdSMike Snitzer 	struct mapped_device *md = tio->md;
1101a77e28c7SKiyoshi Ueda 
110278d8e58aSMike Snitzer 	blk_rq_unprep_clone(clone);
110378d8e58aSMike Snitzer 
1104aa6df8ddSMike Snitzer 	if (md->type == DM_TYPE_MQ_REQUEST_BASED)
1105aa6df8ddSMike Snitzer 		/* stacked on blk-mq queue(s) */
1106e5863d9aSMike Snitzer 		tio->ti->type->release_clone_rq(clone);
110702233342SMike Snitzer 	else if (!md->queue->mq_ops)
110802233342SMike Snitzer 		/* request_fn queue stacked on request_fn queue(s) */
1109bfebd1cdSMike Snitzer 		free_clone_request(md, clone);
1110aa6df8ddSMike Snitzer 	/*
1111aa6df8ddSMike Snitzer 	 * NOTE: for the blk-mq queue stacked on request_fn queue(s) case:
1112aa6df8ddSMike Snitzer 	 * no need to call free_clone_request() because we leverage blk-mq by
1113aa6df8ddSMike Snitzer 	 * allocating the clone at the end of the blk-mq pdu (see: clone_rq)
1114aa6df8ddSMike Snitzer 	 */
1115bfebd1cdSMike Snitzer 
1116bfebd1cdSMike Snitzer 	if (!md->queue->mq_ops)
1117a77e28c7SKiyoshi Ueda 		free_rq_tio(tio);
1118a77e28c7SKiyoshi Ueda }
1119a77e28c7SKiyoshi Ueda 
1120980691e5SKiyoshi Ueda /*
1121980691e5SKiyoshi Ueda  * Complete the clone and the original request.
1122466d89a6SKeith Busch  * Must be called without clone's queue lock held,
1123466d89a6SKeith Busch  * see end_clone_request() for more details.
1124980691e5SKiyoshi Ueda  */
1125980691e5SKiyoshi Ueda static void dm_end_request(struct request *clone, int error)
1126980691e5SKiyoshi Ueda {
1127980691e5SKiyoshi Ueda 	int rw = rq_data_dir(clone);
1128980691e5SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1129980691e5SKiyoshi Ueda 	struct mapped_device *md = tio->md;
1130980691e5SKiyoshi Ueda 	struct request *rq = tio->orig;
1131980691e5SKiyoshi Ueda 
113229e4013dSTejun Heo 	if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
1133980691e5SKiyoshi Ueda 		rq->errors = clone->errors;
1134980691e5SKiyoshi Ueda 		rq->resid_len = clone->resid_len;
1135980691e5SKiyoshi Ueda 
1136980691e5SKiyoshi Ueda 		if (rq->sense)
1137980691e5SKiyoshi Ueda 			/*
1138980691e5SKiyoshi Ueda 			 * We are using the sense buffer of the original
1139980691e5SKiyoshi Ueda 			 * request.
1140980691e5SKiyoshi Ueda 			 * So setting the length of the sense data is enough.
1141980691e5SKiyoshi Ueda 			 */
1142980691e5SKiyoshi Ueda 			rq->sense_len = clone->sense_len;
1143980691e5SKiyoshi Ueda 	}
1144980691e5SKiyoshi Ueda 
1145e5d8de32SMike Snitzer 	free_rq_clone(clone);
1146e262f347SMikulas Patocka 	rq_end_stats(md, rq);
1147bfebd1cdSMike Snitzer 	if (!rq->q->mq_ops)
1148980691e5SKiyoshi Ueda 		blk_end_request_all(rq, error);
1149bfebd1cdSMike Snitzer 	else
1150bfebd1cdSMike Snitzer 		blk_mq_end_request(rq, error);
115129e4013dSTejun Heo 	rq_completed(md, rw, true);
1152980691e5SKiyoshi Ueda }
1153980691e5SKiyoshi Ueda 
1154cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq)
1155cec47e3dSKiyoshi Ueda {
1156bfebd1cdSMike Snitzer 	struct dm_rq_target_io *tio = tio_from_request(rq);
1157466d89a6SKeith Busch 	struct request *clone = tio->clone;
1158cec47e3dSKiyoshi Ueda 
1159bfebd1cdSMike Snitzer 	if (!rq->q->mq_ops) {
1160cec47e3dSKiyoshi Ueda 		rq->special = NULL;
1161cec47e3dSKiyoshi Ueda 		rq->cmd_flags &= ~REQ_DONTPREP;
1162bfebd1cdSMike Snitzer 	}
1163cec47e3dSKiyoshi Ueda 
1164e5863d9aSMike Snitzer 	if (clone)
1165e5d8de32SMike Snitzer 		free_rq_clone(clone);
1166cec47e3dSKiyoshi Ueda }
1167cec47e3dSKiyoshi Ueda 
1168cec47e3dSKiyoshi Ueda /*
1169cec47e3dSKiyoshi Ueda  * Requeue the original request of a clone.
1170cec47e3dSKiyoshi Ueda  */
1171bfebd1cdSMike Snitzer static void old_requeue_request(struct request *rq)
1172cec47e3dSKiyoshi Ueda {
1173cec47e3dSKiyoshi Ueda 	struct request_queue *q = rq->q;
1174cec47e3dSKiyoshi Ueda 	unsigned long flags;
1175cec47e3dSKiyoshi Ueda 
1176cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
1177cec47e3dSKiyoshi Ueda 	blk_requeue_request(q, rq);
11784ae9944dSJunichi Nomura 	blk_run_queue_async(q);
1179cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
1180bfebd1cdSMike Snitzer }
1181bfebd1cdSMike Snitzer 
11822d76fff1SMike Snitzer static void dm_requeue_original_request(struct mapped_device *md,
1183bfebd1cdSMike Snitzer 					struct request *rq)
1184bfebd1cdSMike Snitzer {
1185bfebd1cdSMike Snitzer 	int rw = rq_data_dir(rq);
1186bfebd1cdSMike Snitzer 
1187bfebd1cdSMike Snitzer 	dm_unprep_request(rq);
1188bfebd1cdSMike Snitzer 
1189e262f347SMikulas Patocka 	rq_end_stats(md, rq);
1190bfebd1cdSMike Snitzer 	if (!rq->q->mq_ops)
1191bfebd1cdSMike Snitzer 		old_requeue_request(rq);
1192bfebd1cdSMike Snitzer 	else {
1193bfebd1cdSMike Snitzer 		blk_mq_requeue_request(rq);
1194bfebd1cdSMike Snitzer 		blk_mq_kick_requeue_list(rq->q);
1195bfebd1cdSMike Snitzer 	}
1196cec47e3dSKiyoshi Ueda 
1197466d89a6SKeith Busch 	rq_completed(md, rw, false);
1198cec47e3dSKiyoshi Ueda }
1199466d89a6SKeith Busch 
1200bfebd1cdSMike Snitzer static void old_stop_queue(struct request_queue *q)
1201cec47e3dSKiyoshi Ueda {
1202bfebd1cdSMike Snitzer 	unsigned long flags;
1203bfebd1cdSMike Snitzer 
1204bfebd1cdSMike Snitzer 	if (blk_queue_stopped(q))
1205bfebd1cdSMike Snitzer 		return;
1206bfebd1cdSMike Snitzer 
1207bfebd1cdSMike Snitzer 	spin_lock_irqsave(q->queue_lock, flags);
1208cec47e3dSKiyoshi Ueda 	blk_stop_queue(q);
1209bfebd1cdSMike Snitzer 	spin_unlock_irqrestore(q->queue_lock, flags);
1210cec47e3dSKiyoshi Ueda }
1211cec47e3dSKiyoshi Ueda 
1212cec47e3dSKiyoshi Ueda static void stop_queue(struct request_queue *q)
1213cec47e3dSKiyoshi Ueda {
1214bfebd1cdSMike Snitzer 	if (!q->mq_ops)
1215bfebd1cdSMike Snitzer 		old_stop_queue(q);
1216bfebd1cdSMike Snitzer 	else
1217bfebd1cdSMike Snitzer 		blk_mq_stop_hw_queues(q);
1218bfebd1cdSMike Snitzer }
1219bfebd1cdSMike Snitzer 
1220bfebd1cdSMike Snitzer static void old_start_queue(struct request_queue *q)
1221bfebd1cdSMike Snitzer {
1222cec47e3dSKiyoshi Ueda 	unsigned long flags;
1223cec47e3dSKiyoshi Ueda 
1224cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
1225cec47e3dSKiyoshi Ueda 	if (blk_queue_stopped(q))
1226cec47e3dSKiyoshi Ueda 		blk_start_queue(q);
1227bfebd1cdSMike Snitzer 	spin_unlock_irqrestore(q->queue_lock, flags);
1228cec47e3dSKiyoshi Ueda }
1229cec47e3dSKiyoshi Ueda 
1230cec47e3dSKiyoshi Ueda static void start_queue(struct request_queue *q)
1231cec47e3dSKiyoshi Ueda {
1232bfebd1cdSMike Snitzer 	if (!q->mq_ops)
1233bfebd1cdSMike Snitzer 		old_start_queue(q);
1234bfebd1cdSMike Snitzer 	else
1235bfebd1cdSMike Snitzer 		blk_mq_start_stopped_hw_queues(q, true);
1236cec47e3dSKiyoshi Ueda }
1237cec47e3dSKiyoshi Ueda 
123811a68244SKiyoshi Ueda static void dm_done(struct request *clone, int error, bool mapped)
123911a68244SKiyoshi Ueda {
124011a68244SKiyoshi Ueda 	int r = error;
124111a68244SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1242ba1cbad9SMike Snitzer 	dm_request_endio_fn rq_end_io = NULL;
1243ba1cbad9SMike Snitzer 
1244ba1cbad9SMike Snitzer 	if (tio->ti) {
1245ba1cbad9SMike Snitzer 		rq_end_io = tio->ti->type->rq_end_io;
124611a68244SKiyoshi Ueda 
124711a68244SKiyoshi Ueda 		if (mapped && rq_end_io)
124811a68244SKiyoshi Ueda 			r = rq_end_io(tio->ti, clone, error, &tio->info);
1249ba1cbad9SMike Snitzer 	}
125011a68244SKiyoshi Ueda 
12517eee4ae2SMike Snitzer 	if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) &&
12527eee4ae2SMike Snitzer 		     !clone->q->limits.max_write_same_sectors))
12537eee4ae2SMike Snitzer 		disable_write_same(tio->md);
12547eee4ae2SMike Snitzer 
125511a68244SKiyoshi Ueda 	if (r <= 0)
125611a68244SKiyoshi Ueda 		/* The target wants to complete the I/O */
125711a68244SKiyoshi Ueda 		dm_end_request(clone, r);
125811a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_INCOMPLETE)
125911a68244SKiyoshi Ueda 		/* The target will handle the I/O */
126011a68244SKiyoshi Ueda 		return;
126111a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_REQUEUE)
126211a68244SKiyoshi Ueda 		/* The target wants to requeue the I/O */
12632d76fff1SMike Snitzer 		dm_requeue_original_request(tio->md, tio->orig);
126411a68244SKiyoshi Ueda 	else {
126511a68244SKiyoshi Ueda 		DMWARN("unimplemented target endio return value: %d", r);
126611a68244SKiyoshi Ueda 		BUG();
126711a68244SKiyoshi Ueda 	}
126811a68244SKiyoshi Ueda }
126911a68244SKiyoshi Ueda 
1270cec47e3dSKiyoshi Ueda /*
1271cec47e3dSKiyoshi Ueda  * Request completion handler for request-based dm
1272cec47e3dSKiyoshi Ueda  */
1273cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq)
1274cec47e3dSKiyoshi Ueda {
127511a68244SKiyoshi Ueda 	bool mapped = true;
1276bfebd1cdSMike Snitzer 	struct dm_rq_target_io *tio = tio_from_request(rq);
1277466d89a6SKeith Busch 	struct request *clone = tio->clone;
1278bfebd1cdSMike Snitzer 	int rw;
1279cec47e3dSKiyoshi Ueda 
1280e5863d9aSMike Snitzer 	if (!clone) {
1281e262f347SMikulas Patocka 		rq_end_stats(tio->md, rq);
1282bfebd1cdSMike Snitzer 		rw = rq_data_dir(rq);
1283bfebd1cdSMike Snitzer 		if (!rq->q->mq_ops) {
1284e5863d9aSMike Snitzer 			blk_end_request_all(rq, tio->error);
1285bfebd1cdSMike Snitzer 			rq_completed(tio->md, rw, false);
1286e5863d9aSMike Snitzer 			free_rq_tio(tio);
1287bfebd1cdSMike Snitzer 		} else {
1288bfebd1cdSMike Snitzer 			blk_mq_end_request(rq, tio->error);
1289bfebd1cdSMike Snitzer 			rq_completed(tio->md, rw, false);
1290bfebd1cdSMike Snitzer 		}
1291e5863d9aSMike Snitzer 		return;
1292e5863d9aSMike Snitzer 	}
1293cec47e3dSKiyoshi Ueda 
129411a68244SKiyoshi Ueda 	if (rq->cmd_flags & REQ_FAILED)
129511a68244SKiyoshi Ueda 		mapped = false;
1296cec47e3dSKiyoshi Ueda 
129711a68244SKiyoshi Ueda 	dm_done(clone, tio->error, mapped);
1298cec47e3dSKiyoshi Ueda }
1299cec47e3dSKiyoshi Ueda 
1300cec47e3dSKiyoshi Ueda /*
1301cec47e3dSKiyoshi Ueda  * Complete the clone and the original request with the error status
1302cec47e3dSKiyoshi Ueda  * through softirq context.
1303cec47e3dSKiyoshi Ueda  */
1304466d89a6SKeith Busch static void dm_complete_request(struct request *rq, int error)
1305cec47e3dSKiyoshi Ueda {
1306bfebd1cdSMike Snitzer 	struct dm_rq_target_io *tio = tio_from_request(rq);
1307cec47e3dSKiyoshi Ueda 
1308cec47e3dSKiyoshi Ueda 	tio->error = error;
1309cec47e3dSKiyoshi Ueda 	blk_complete_request(rq);
1310cec47e3dSKiyoshi Ueda }
1311cec47e3dSKiyoshi Ueda 
1312cec47e3dSKiyoshi Ueda /*
1313cec47e3dSKiyoshi Ueda  * Complete the not-mapped clone and the original request with the error status
1314cec47e3dSKiyoshi Ueda  * through softirq context.
1315cec47e3dSKiyoshi Ueda  * Target's rq_end_io() function isn't called.
1316e5863d9aSMike Snitzer  * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
1317cec47e3dSKiyoshi Ueda  */
1318466d89a6SKeith Busch static void dm_kill_unmapped_request(struct request *rq, int error)
1319cec47e3dSKiyoshi Ueda {
1320cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_FAILED;
1321466d89a6SKeith Busch 	dm_complete_request(rq, error);
1322cec47e3dSKiyoshi Ueda }
1323cec47e3dSKiyoshi Ueda 
1324cec47e3dSKiyoshi Ueda /*
1325bfebd1cdSMike Snitzer  * Called with the clone's queue lock held (for non-blk-mq)
1326cec47e3dSKiyoshi Ueda  */
1327cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error)
1328cec47e3dSKiyoshi Ueda {
1329466d89a6SKeith Busch 	struct dm_rq_target_io *tio = clone->end_io_data;
1330466d89a6SKeith Busch 
1331e5863d9aSMike Snitzer 	if (!clone->q->mq_ops) {
1332cec47e3dSKiyoshi Ueda 		/*
1333cec47e3dSKiyoshi Ueda 		 * For just cleaning up the information of the queue in which
1334cec47e3dSKiyoshi Ueda 		 * the clone was dispatched.
1335e5863d9aSMike Snitzer 		 * The clone is *NOT* freed actually here because it is alloced
1336e5863d9aSMike Snitzer 		 * from dm own mempool (REQ_ALLOCED isn't set).
1337cec47e3dSKiyoshi Ueda 		 */
1338cec47e3dSKiyoshi Ueda 		__blk_put_request(clone->q, clone);
1339e5863d9aSMike Snitzer 	}
1340cec47e3dSKiyoshi Ueda 
1341cec47e3dSKiyoshi Ueda 	/*
1342cec47e3dSKiyoshi Ueda 	 * Actual request completion is done in a softirq context which doesn't
1343466d89a6SKeith Busch 	 * hold the clone's queue lock.  Otherwise, deadlock could occur because:
1344cec47e3dSKiyoshi Ueda 	 *     - another request may be submitted by the upper level driver
1345cec47e3dSKiyoshi Ueda 	 *       of the stacking during the completion
1346cec47e3dSKiyoshi Ueda 	 *     - the submission which requires queue lock may be done
1347466d89a6SKeith Busch 	 *       against this clone's queue
1348cec47e3dSKiyoshi Ueda 	 */
1349466d89a6SKeith Busch 	dm_complete_request(tio->orig, error);
1350cec47e3dSKiyoshi Ueda }
1351cec47e3dSKiyoshi Ueda 
135256a67df7SMike Snitzer /*
135356a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
135456a67df7SMike Snitzer  * target boundary.
135556a67df7SMike Snitzer  */
135656a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
13571da177e4SLinus Torvalds {
135856a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
135956a67df7SMike Snitzer 
136056a67df7SMike Snitzer 	return ti->len - target_offset;
136156a67df7SMike Snitzer }
136256a67df7SMike Snitzer 
136356a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
136456a67df7SMike Snitzer {
136556a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
1366542f9038SMike Snitzer 	sector_t offset, max_len;
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	/*
13691da177e4SLinus Torvalds 	 * Does the target need to split even further?
13701da177e4SLinus Torvalds 	 */
1371542f9038SMike Snitzer 	if (ti->max_io_len) {
1372542f9038SMike Snitzer 		offset = dm_target_offset(ti, sector);
1373542f9038SMike Snitzer 		if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1374542f9038SMike Snitzer 			max_len = sector_div(offset, ti->max_io_len);
1375542f9038SMike Snitzer 		else
1376542f9038SMike Snitzer 			max_len = offset & (ti->max_io_len - 1);
1377542f9038SMike Snitzer 		max_len = ti->max_io_len - max_len;
1378542f9038SMike Snitzer 
1379542f9038SMike Snitzer 		if (len > max_len)
1380542f9038SMike Snitzer 			len = max_len;
13811da177e4SLinus Torvalds 	}
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 	return len;
13841da177e4SLinus Torvalds }
13851da177e4SLinus Torvalds 
1386542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1387542f9038SMike Snitzer {
1388542f9038SMike Snitzer 	if (len > UINT_MAX) {
1389542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1390542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
1391542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
1392542f9038SMike Snitzer 		return -EINVAL;
1393542f9038SMike Snitzer 	}
1394542f9038SMike Snitzer 
1395542f9038SMike Snitzer 	ti->max_io_len = (uint32_t) len;
1396542f9038SMike Snitzer 
1397542f9038SMike Snitzer 	return 0;
1398542f9038SMike Snitzer }
1399542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1400542f9038SMike Snitzer 
14011dd40c3eSMikulas Patocka /*
14021dd40c3eSMikulas Patocka  * A target may call dm_accept_partial_bio only from the map routine.  It is
14031dd40c3eSMikulas Patocka  * allowed for all bio types except REQ_FLUSH.
14041dd40c3eSMikulas Patocka  *
14051dd40c3eSMikulas Patocka  * dm_accept_partial_bio informs the dm that the target only wants to process
14061dd40c3eSMikulas Patocka  * additional n_sectors sectors of the bio and the rest of the data should be
14071dd40c3eSMikulas Patocka  * sent in a next bio.
14081dd40c3eSMikulas Patocka  *
14091dd40c3eSMikulas Patocka  * A diagram that explains the arithmetics:
14101dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
14111dd40c3eSMikulas Patocka  * |         1          |       2       |   3   |
14121dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
14131dd40c3eSMikulas Patocka  *
14141dd40c3eSMikulas Patocka  * <-------------- *tio->len_ptr --------------->
14151dd40c3eSMikulas Patocka  *                      <------- bi_size ------->
14161dd40c3eSMikulas Patocka  *                      <-- n_sectors -->
14171dd40c3eSMikulas Patocka  *
14181dd40c3eSMikulas Patocka  * Region 1 was already iterated over with bio_advance or similar function.
14191dd40c3eSMikulas Patocka  *	(it may be empty if the target doesn't use bio_advance)
14201dd40c3eSMikulas Patocka  * Region 2 is the remaining bio size that the target wants to process.
14211dd40c3eSMikulas Patocka  *	(it may be empty if region 1 is non-empty, although there is no reason
14221dd40c3eSMikulas Patocka  *	 to make it empty)
14231dd40c3eSMikulas Patocka  * The target requires that region 3 is to be sent in the next bio.
14241dd40c3eSMikulas Patocka  *
14251dd40c3eSMikulas Patocka  * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
14261dd40c3eSMikulas Patocka  * the partially processed part (the sum of regions 1+2) must be the same for all
14271dd40c3eSMikulas Patocka  * copies of the bio.
14281dd40c3eSMikulas Patocka  */
14291dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
14301dd40c3eSMikulas Patocka {
14311dd40c3eSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
14321dd40c3eSMikulas Patocka 	unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
14331dd40c3eSMikulas Patocka 	BUG_ON(bio->bi_rw & REQ_FLUSH);
14341dd40c3eSMikulas Patocka 	BUG_ON(bi_size > *tio->len_ptr);
14351dd40c3eSMikulas Patocka 	BUG_ON(n_sectors > bi_size);
14361dd40c3eSMikulas Patocka 	*tio->len_ptr -= bi_size - n_sectors;
14371dd40c3eSMikulas Patocka 	bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
14381dd40c3eSMikulas Patocka }
14391dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
14401dd40c3eSMikulas Patocka 
1441bd2a49b8SAlasdair G Kergon static void __map_bio(struct dm_target_io *tio)
14421da177e4SLinus Torvalds {
14431da177e4SLinus Torvalds 	int r;
14442056a782SJens Axboe 	sector_t sector;
14459faf400fSStefan Bader 	struct mapped_device *md;
1446dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
1447bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds 	/*
14521da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
14531da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
14541da177e4SLinus Torvalds 	 * this io.
14551da177e4SLinus Torvalds 	 */
14561da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
14574f024f37SKent Overstreet 	sector = clone->bi_iter.bi_sector;
14587de3ee57SMikulas Patocka 	r = ti->type->map(ti, clone);
145945cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
14601da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
14612056a782SJens Axboe 
1462d07335e5SMike Snitzer 		trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
146322a7c31aSAlan D. Brunelle 				      tio->io->bio->bi_bdev->bd_dev, sector);
14642056a782SJens Axboe 
14651da177e4SLinus Torvalds 		generic_make_request(clone);
14662e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
14672e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
14689faf400fSStefan Bader 		md = tio->io->md;
14699faf400fSStefan Bader 		dec_pending(tio->io, r);
14709faf400fSStefan Bader 		free_tio(md, tio);
147145cbcd79SKiyoshi Ueda 	} else if (r) {
147245cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
147345cbcd79SKiyoshi Ueda 		BUG();
14741da177e4SLinus Torvalds 	}
14751da177e4SLinus Torvalds }
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds struct clone_info {
14781da177e4SLinus Torvalds 	struct mapped_device *md;
14791da177e4SLinus Torvalds 	struct dm_table *map;
14801da177e4SLinus Torvalds 	struct bio *bio;
14811da177e4SLinus Torvalds 	struct dm_io *io;
14821da177e4SLinus Torvalds 	sector_t sector;
1483e0d6609aSMikulas Patocka 	unsigned sector_count;
14841da177e4SLinus Torvalds };
14851da177e4SLinus Torvalds 
1486e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
1487bd2a49b8SAlasdair G Kergon {
14884f024f37SKent Overstreet 	bio->bi_iter.bi_sector = sector;
14894f024f37SKent Overstreet 	bio->bi_iter.bi_size = to_bytes(len);
14901da177e4SLinus Torvalds }
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds /*
14931da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
14941da177e4SLinus Torvalds  */
1495dba14160SMikulas Patocka static void clone_bio(struct dm_target_io *tio, struct bio *bio,
14961c3b13e6SKent Overstreet 		      sector_t sector, unsigned len)
14971da177e4SLinus Torvalds {
1498dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
14991da177e4SLinus Torvalds 
15001c3b13e6SKent Overstreet 	__bio_clone_fast(clone, bio);
15019c47008dSMartin K. Petersen 
15021c3b13e6SKent Overstreet 	if (bio_integrity(bio))
15031c3b13e6SKent Overstreet 		bio_integrity_clone(clone, bio, GFP_NOIO);
15041c3b13e6SKent Overstreet 
15051c3b13e6SKent Overstreet 	bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
15061c3b13e6SKent Overstreet 	clone->bi_iter.bi_size = to_bytes(len);
15071c3b13e6SKent Overstreet 
15081c3b13e6SKent Overstreet 	if (bio_integrity(bio))
15091c3b13e6SKent Overstreet 		bio_integrity_trim(clone, 0, len);
15101da177e4SLinus Torvalds }
15111da177e4SLinus Torvalds 
15129015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci,
151399778273SJunichi Nomura 				      struct dm_target *ti,
151455a62eefSAlasdair G Kergon 				      unsigned target_bio_nr)
1515f9ab94ceSMikulas Patocka {
1516dba14160SMikulas Patocka 	struct dm_target_io *tio;
1517dba14160SMikulas Patocka 	struct bio *clone;
1518dba14160SMikulas Patocka 
151999778273SJunichi Nomura 	clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1520dba14160SMikulas Patocka 	tio = container_of(clone, struct dm_target_io, clone);
1521f9ab94ceSMikulas Patocka 
1522f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1523f9ab94ceSMikulas Patocka 	tio->ti = ti;
152455a62eefSAlasdair G Kergon 	tio->target_bio_nr = target_bio_nr;
15259015df24SAlasdair G Kergon 
15269015df24SAlasdair G Kergon 	return tio;
15279015df24SAlasdair G Kergon }
15289015df24SAlasdair G Kergon 
152914fe594dSAlasdair G Kergon static void __clone_and_map_simple_bio(struct clone_info *ci,
153014fe594dSAlasdair G Kergon 				       struct dm_target *ti,
15311dd40c3eSMikulas Patocka 				       unsigned target_bio_nr, unsigned *len)
15329015df24SAlasdair G Kergon {
153399778273SJunichi Nomura 	struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
1534dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
15359015df24SAlasdair G Kergon 
15361dd40c3eSMikulas Patocka 	tio->len_ptr = len;
15371dd40c3eSMikulas Patocka 
15381c3b13e6SKent Overstreet 	__bio_clone_fast(clone, ci->bio);
1539bd2a49b8SAlasdair G Kergon 	if (len)
15401dd40c3eSMikulas Patocka 		bio_setup_sector(clone, ci->sector, *len);
1541f9ab94ceSMikulas Patocka 
1542bd2a49b8SAlasdair G Kergon 	__map_bio(tio);
1543f9ab94ceSMikulas Patocka }
1544f9ab94ceSMikulas Patocka 
154514fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
15461dd40c3eSMikulas Patocka 				  unsigned num_bios, unsigned *len)
154706a426ceSMike Snitzer {
154855a62eefSAlasdair G Kergon 	unsigned target_bio_nr;
154906a426ceSMike Snitzer 
155055a62eefSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
155114fe594dSAlasdair G Kergon 		__clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
155206a426ceSMike Snitzer }
155306a426ceSMike Snitzer 
155414fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1555f9ab94ceSMikulas Patocka {
155606a426ceSMike Snitzer 	unsigned target_nr = 0;
1557f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1558f9ab94ceSMikulas Patocka 
1559b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1560f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
15611dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
1562f9ab94ceSMikulas Patocka 
1563f9ab94ceSMikulas Patocka 	return 0;
1564f9ab94ceSMikulas Patocka }
1565f9ab94ceSMikulas Patocka 
1566e4c93811SAlasdair G Kergon static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
15671dd40c3eSMikulas Patocka 				     sector_t sector, unsigned *len)
15685ae89a87SMike Snitzer {
1569dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
15705ae89a87SMike Snitzer 	struct dm_target_io *tio;
1571b0d8ed4dSAlasdair G Kergon 	unsigned target_bio_nr;
1572b0d8ed4dSAlasdair G Kergon 	unsigned num_target_bios = 1;
15735ae89a87SMike Snitzer 
1574b0d8ed4dSAlasdair G Kergon 	/*
1575b0d8ed4dSAlasdair G Kergon 	 * Does the target want to receive duplicate copies of the bio?
1576b0d8ed4dSAlasdair G Kergon 	 */
1577b0d8ed4dSAlasdair G Kergon 	if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1578b0d8ed4dSAlasdair G Kergon 		num_target_bios = ti->num_write_bios(ti, bio);
1579e4c93811SAlasdair G Kergon 
1580b0d8ed4dSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
158199778273SJunichi Nomura 		tio = alloc_tio(ci, ti, target_bio_nr);
15821dd40c3eSMikulas Patocka 		tio->len_ptr = len;
15831dd40c3eSMikulas Patocka 		clone_bio(tio, bio, sector, *len);
1584bd2a49b8SAlasdair G Kergon 		__map_bio(tio);
15855ae89a87SMike Snitzer 	}
1586b0d8ed4dSAlasdair G Kergon }
15875ae89a87SMike Snitzer 
158855a62eefSAlasdair G Kergon typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
158923508a96SMike Snitzer 
159055a62eefSAlasdair G Kergon static unsigned get_num_discard_bios(struct dm_target *ti)
159123508a96SMike Snitzer {
159255a62eefSAlasdair G Kergon 	return ti->num_discard_bios;
159323508a96SMike Snitzer }
159423508a96SMike Snitzer 
159555a62eefSAlasdair G Kergon static unsigned get_num_write_same_bios(struct dm_target *ti)
159623508a96SMike Snitzer {
159755a62eefSAlasdair G Kergon 	return ti->num_write_same_bios;
159823508a96SMike Snitzer }
159923508a96SMike Snitzer 
160023508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti);
160123508a96SMike Snitzer 
160223508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti)
160323508a96SMike Snitzer {
160455a62eefSAlasdair G Kergon 	return ti->split_discard_bios;
160523508a96SMike Snitzer }
160623508a96SMike Snitzer 
160714fe594dSAlasdair G Kergon static int __send_changing_extent_only(struct clone_info *ci,
160855a62eefSAlasdair G Kergon 				       get_num_bios_fn get_num_bios,
160923508a96SMike Snitzer 				       is_split_required_fn is_split_required)
16105ae89a87SMike Snitzer {
16115ae89a87SMike Snitzer 	struct dm_target *ti;
1612e0d6609aSMikulas Patocka 	unsigned len;
161355a62eefSAlasdair G Kergon 	unsigned num_bios;
16145ae89a87SMike Snitzer 
1615a79245b3SMike Snitzer 	do {
16165ae89a87SMike Snitzer 		ti = dm_table_find_target(ci->map, ci->sector);
16175ae89a87SMike Snitzer 		if (!dm_target_is_valid(ti))
16185ae89a87SMike Snitzer 			return -EIO;
16195ae89a87SMike Snitzer 
16205ae89a87SMike Snitzer 		/*
162123508a96SMike Snitzer 		 * Even though the device advertised support for this type of
162223508a96SMike Snitzer 		 * request, that does not mean every target supports it, and
1623936688d7SMike Snitzer 		 * reconfiguration might also have changed that since the
16245ae89a87SMike Snitzer 		 * check was performed.
16255ae89a87SMike Snitzer 		 */
162655a62eefSAlasdair G Kergon 		num_bios = get_num_bios ? get_num_bios(ti) : 0;
162755a62eefSAlasdair G Kergon 		if (!num_bios)
16285ae89a87SMike Snitzer 			return -EOPNOTSUPP;
16295ae89a87SMike Snitzer 
163023508a96SMike Snitzer 		if (is_split_required && !is_split_required(ti))
1631e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
16327acf0277SMikulas Patocka 		else
1633e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
16345ae89a87SMike Snitzer 
16351dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, num_bios, &len);
16365ae89a87SMike Snitzer 
1637a79245b3SMike Snitzer 		ci->sector += len;
1638a79245b3SMike Snitzer 	} while (ci->sector_count -= len);
16395ae89a87SMike Snitzer 
16405ae89a87SMike Snitzer 	return 0;
16415ae89a87SMike Snitzer }
16425ae89a87SMike Snitzer 
164314fe594dSAlasdair G Kergon static int __send_discard(struct clone_info *ci)
164423508a96SMike Snitzer {
164514fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_discard_bios,
164623508a96SMike Snitzer 					   is_split_required_for_discard);
164723508a96SMike Snitzer }
164823508a96SMike Snitzer 
164914fe594dSAlasdair G Kergon static int __send_write_same(struct clone_info *ci)
165023508a96SMike Snitzer {
165114fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
165223508a96SMike Snitzer }
165323508a96SMike Snitzer 
1654e4c93811SAlasdair G Kergon /*
1655e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1656e4c93811SAlasdair G Kergon  */
1657e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci)
1658e4c93811SAlasdair G Kergon {
1659e4c93811SAlasdair G Kergon 	struct bio *bio = ci->bio;
1660e4c93811SAlasdair G Kergon 	struct dm_target *ti;
16611c3b13e6SKent Overstreet 	unsigned len;
1662e4c93811SAlasdair G Kergon 
1663e4c93811SAlasdair G Kergon 	if (unlikely(bio->bi_rw & REQ_DISCARD))
1664e4c93811SAlasdair G Kergon 		return __send_discard(ci);
1665e4c93811SAlasdair G Kergon 	else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
1666e4c93811SAlasdair G Kergon 		return __send_write_same(ci);
1667e4c93811SAlasdair G Kergon 
1668e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1669e4c93811SAlasdair G Kergon 	if (!dm_target_is_valid(ti))
1670e4c93811SAlasdair G Kergon 		return -EIO;
1671e4c93811SAlasdair G Kergon 
16721c3b13e6SKent Overstreet 	len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
1673e4c93811SAlasdair G Kergon 
16741dd40c3eSMikulas Patocka 	__clone_and_map_data_bio(ci, ti, ci->sector, &len);
1675e4c93811SAlasdair G Kergon 
1676e4c93811SAlasdair G Kergon 	ci->sector += len;
1677e4c93811SAlasdair G Kergon 	ci->sector_count -= len;
1678e4c93811SAlasdair G Kergon 
1679e4c93811SAlasdair G Kergon 	return 0;
1680e4c93811SAlasdair G Kergon }
1681e4c93811SAlasdair G Kergon 
1682e4c93811SAlasdair G Kergon /*
168314fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
16841da177e4SLinus Torvalds  */
168583d5e5b0SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md,
168683d5e5b0SMikulas Patocka 				    struct dm_table *map, struct bio *bio)
16871da177e4SLinus Torvalds {
16881da177e4SLinus Torvalds 	struct clone_info ci;
1689512875bdSJun'ichi Nomura 	int error = 0;
16901da177e4SLinus Torvalds 
169183d5e5b0SMikulas Patocka 	if (unlikely(!map)) {
1692f0b9a450SMikulas Patocka 		bio_io_error(bio);
1693f0b9a450SMikulas Patocka 		return;
1694f0b9a450SMikulas Patocka 	}
1695692d0eb9SMikulas Patocka 
169683d5e5b0SMikulas Patocka 	ci.map = map;
16971da177e4SLinus Torvalds 	ci.md = md;
16981da177e4SLinus Torvalds 	ci.io = alloc_io(md);
16991da177e4SLinus Torvalds 	ci.io->error = 0;
17001da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
17011da177e4SLinus Torvalds 	ci.io->bio = bio;
17021da177e4SLinus Torvalds 	ci.io->md = md;
1703f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
17044f024f37SKent Overstreet 	ci.sector = bio->bi_iter.bi_sector;
17051da177e4SLinus Torvalds 
17063eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1707bd2a49b8SAlasdair G Kergon 
1708b372d360SMike Snitzer 	if (bio->bi_rw & REQ_FLUSH) {
1709b372d360SMike Snitzer 		ci.bio = &ci.md->flush_bio;
1710b372d360SMike Snitzer 		ci.sector_count = 0;
171114fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1712b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1713b372d360SMike Snitzer 	} else {
17146a8736d1STejun Heo 		ci.bio = bio;
1715f6fccb12SMilan Broz 		ci.sector_count = bio_sectors(bio);
1716512875bdSJun'ichi Nomura 		while (ci.sector_count && !error)
171714fe594dSAlasdair G Kergon 			error = __split_and_process_non_flush(&ci);
1718d87f4c14STejun Heo 	}
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds 	/* drop the extra reference count */
1721512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
17229e4e5f87SMilan Broz }
17239e4e5f87SMilan Broz /*-----------------------------------------------------------------
17241da177e4SLinus Torvalds  * CRUD END
17251da177e4SLinus Torvalds  *---------------------------------------------------------------*/
17261da177e4SLinus Torvalds 
17271da177e4SLinus Torvalds /*
17281da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
17291da177e4SLinus Torvalds  * dm_merge_bvec.
17301da177e4SLinus Torvalds  */
1731ff36ab34SMike Snitzer static void dm_make_request(struct request_queue *q, struct bio *bio)
17321da177e4SLinus Torvalds {
173312f03a49SKevin Corry 	int rw = bio_data_dir(bio);
17341da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
173583d5e5b0SMikulas Patocka 	int srcu_idx;
173683d5e5b0SMikulas Patocka 	struct dm_table *map;
17371da177e4SLinus Torvalds 
173883d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
17391da177e4SLinus Torvalds 
174054efd50bSKent Overstreet 	blk_queue_split(q, &bio, q->bio_split);
174154efd50bSKent Overstreet 
174218c0b223SGu Zheng 	generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
174312f03a49SKevin Corry 
17446a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
17456a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
174683d5e5b0SMikulas Patocka 		dm_put_live_table(md, srcu_idx);
17471da177e4SLinus Torvalds 
17486a8736d1STejun Heo 		if (bio_rw(bio) != READA)
174992c63902SMikulas Patocka 			queue_io(md, bio);
17506a8736d1STejun Heo 		else
17516a8736d1STejun Heo 			bio_io_error(bio);
17525a7bbad2SChristoph Hellwig 		return;
17531da177e4SLinus Torvalds 	}
17541da177e4SLinus Torvalds 
175583d5e5b0SMikulas Patocka 	__split_and_process_bio(md, map, bio);
175683d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
17575a7bbad2SChristoph Hellwig 	return;
1758cec47e3dSKiyoshi Ueda }
1759cec47e3dSKiyoshi Ueda 
1760fd2ed4d2SMikulas Patocka int dm_request_based(struct mapped_device *md)
1761cec47e3dSKiyoshi Ueda {
1762cec47e3dSKiyoshi Ueda 	return blk_queue_stackable(md->queue);
1763cec47e3dSKiyoshi Ueda }
1764cec47e3dSKiyoshi Ueda 
1765466d89a6SKeith Busch static void dm_dispatch_clone_request(struct request *clone, struct request *rq)
1766cec47e3dSKiyoshi Ueda {
1767cec47e3dSKiyoshi Ueda 	int r;
1768cec47e3dSKiyoshi Ueda 
1769466d89a6SKeith Busch 	if (blk_queue_io_stat(clone->q))
1770466d89a6SKeith Busch 		clone->cmd_flags |= REQ_IO_STAT;
1771cec47e3dSKiyoshi Ueda 
1772466d89a6SKeith Busch 	clone->start_time = jiffies;
1773466d89a6SKeith Busch 	r = blk_insert_cloned_request(clone->q, clone);
1774cec47e3dSKiyoshi Ueda 	if (r)
1775466d89a6SKeith Busch 		/* must complete clone in terms of original request */
1776cec47e3dSKiyoshi Ueda 		dm_complete_request(rq, r);
1777cec47e3dSKiyoshi Ueda }
1778cec47e3dSKiyoshi Ueda 
177978d8e58aSMike Snitzer static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
178078d8e58aSMike Snitzer 				 void *data)
1781cec47e3dSKiyoshi Ueda {
178278d8e58aSMike Snitzer 	struct dm_rq_target_io *tio = data;
178378d8e58aSMike Snitzer 	struct dm_rq_clone_bio_info *info =
178478d8e58aSMike Snitzer 		container_of(bio, struct dm_rq_clone_bio_info, clone);
178578d8e58aSMike Snitzer 
178678d8e58aSMike Snitzer 	info->orig = bio_orig;
178778d8e58aSMike Snitzer 	info->tio = tio;
178878d8e58aSMike Snitzer 	bio->bi_end_io = end_clone_bio;
178978d8e58aSMike Snitzer 
179078d8e58aSMike Snitzer 	return 0;
179178d8e58aSMike Snitzer }
179278d8e58aSMike Snitzer 
179378d8e58aSMike Snitzer static int setup_clone(struct request *clone, struct request *rq,
179478d8e58aSMike Snitzer 		       struct dm_rq_target_io *tio, gfp_t gfp_mask)
179578d8e58aSMike Snitzer {
179678d8e58aSMike Snitzer 	int r;
179778d8e58aSMike Snitzer 
179878d8e58aSMike Snitzer 	r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask,
179978d8e58aSMike Snitzer 			      dm_rq_bio_constructor, tio);
180078d8e58aSMike Snitzer 	if (r)
180178d8e58aSMike Snitzer 		return r;
180278d8e58aSMike Snitzer 
180378d8e58aSMike Snitzer 	clone->cmd = rq->cmd;
180478d8e58aSMike Snitzer 	clone->cmd_len = rq->cmd_len;
180578d8e58aSMike Snitzer 	clone->sense = rq->sense;
1806cec47e3dSKiyoshi Ueda 	clone->end_io = end_clone_request;
1807cec47e3dSKiyoshi Ueda 	clone->end_io_data = tio;
180878d8e58aSMike Snitzer 
18091ae49ea2SMike Snitzer 	tio->clone = clone;
181078d8e58aSMike Snitzer 
181178d8e58aSMike Snitzer 	return 0;
1812cec47e3dSKiyoshi Ueda }
1813cec47e3dSKiyoshi Ueda 
18146facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md,
18151ae49ea2SMike Snitzer 				struct dm_rq_target_io *tio, gfp_t gfp_mask)
18166facdaffSKiyoshi Ueda {
181702233342SMike Snitzer 	/*
181802233342SMike Snitzer 	 * Do not allocate a clone if tio->clone was already set
181902233342SMike Snitzer 	 * (see: dm_mq_queue_rq).
182002233342SMike Snitzer 	 */
182102233342SMike Snitzer 	bool alloc_clone = !tio->clone;
182202233342SMike Snitzer 	struct request *clone;
18231ae49ea2SMike Snitzer 
182402233342SMike Snitzer 	if (alloc_clone) {
182502233342SMike Snitzer 		clone = alloc_clone_request(md, gfp_mask);
18261ae49ea2SMike Snitzer 		if (!clone)
18271ae49ea2SMike Snitzer 			return NULL;
182802233342SMike Snitzer 	} else
182902233342SMike Snitzer 		clone = tio->clone;
18301ae49ea2SMike Snitzer 
18311ae49ea2SMike Snitzer 	blk_rq_init(NULL, clone);
183278d8e58aSMike Snitzer 	if (setup_clone(clone, rq, tio, gfp_mask)) {
183378d8e58aSMike Snitzer 		/* -ENOMEM */
183478d8e58aSMike Snitzer 		if (alloc_clone)
183578d8e58aSMike Snitzer 			free_clone_request(md, clone);
183678d8e58aSMike Snitzer 		return NULL;
183778d8e58aSMike Snitzer 	}
18381ae49ea2SMike Snitzer 
18391ae49ea2SMike Snitzer 	return clone;
18401ae49ea2SMike Snitzer }
18411ae49ea2SMike Snitzer 
18422eb6e1e3SKeith Busch static void map_tio_request(struct kthread_work *work);
18432eb6e1e3SKeith Busch 
1844bfebd1cdSMike Snitzer static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
1845bfebd1cdSMike Snitzer 		     struct mapped_device *md)
1846bfebd1cdSMike Snitzer {
1847bfebd1cdSMike Snitzer 	tio->md = md;
1848bfebd1cdSMike Snitzer 	tio->ti = NULL;
1849bfebd1cdSMike Snitzer 	tio->clone = NULL;
1850bfebd1cdSMike Snitzer 	tio->orig = rq;
1851bfebd1cdSMike Snitzer 	tio->error = 0;
1852bfebd1cdSMike Snitzer 	memset(&tio->info, 0, sizeof(tio->info));
185302233342SMike Snitzer 	if (md->kworker_task)
1854bfebd1cdSMike Snitzer 		init_kthread_work(&tio->work, map_tio_request);
1855bfebd1cdSMike Snitzer }
1856bfebd1cdSMike Snitzer 
1857466d89a6SKeith Busch static struct dm_rq_target_io *prep_tio(struct request *rq,
1858466d89a6SKeith Busch 					struct mapped_device *md, gfp_t gfp_mask)
18596facdaffSKiyoshi Ueda {
18606facdaffSKiyoshi Ueda 	struct dm_rq_target_io *tio;
1861e5863d9aSMike Snitzer 	int srcu_idx;
1862e5863d9aSMike Snitzer 	struct dm_table *table;
18636facdaffSKiyoshi Ueda 
18646facdaffSKiyoshi Ueda 	tio = alloc_rq_tio(md, gfp_mask);
18656facdaffSKiyoshi Ueda 	if (!tio)
18666facdaffSKiyoshi Ueda 		return NULL;
18676facdaffSKiyoshi Ueda 
1868bfebd1cdSMike Snitzer 	init_tio(tio, rq, md);
18696facdaffSKiyoshi Ueda 
1870e5863d9aSMike Snitzer 	table = dm_get_live_table(md, &srcu_idx);
1871e5863d9aSMike Snitzer 	if (!dm_table_mq_request_based(table)) {
1872466d89a6SKeith Busch 		if (!clone_rq(rq, md, tio, gfp_mask)) {
1873e5863d9aSMike Snitzer 			dm_put_live_table(md, srcu_idx);
18746facdaffSKiyoshi Ueda 			free_rq_tio(tio);
18756facdaffSKiyoshi Ueda 			return NULL;
18766facdaffSKiyoshi Ueda 		}
1877e5863d9aSMike Snitzer 	}
1878e5863d9aSMike Snitzer 	dm_put_live_table(md, srcu_idx);
18796facdaffSKiyoshi Ueda 
1880466d89a6SKeith Busch 	return tio;
18816facdaffSKiyoshi Ueda }
18826facdaffSKiyoshi Ueda 
1883cec47e3dSKiyoshi Ueda /*
1884cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1885cec47e3dSKiyoshi Ueda  */
1886cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq)
1887cec47e3dSKiyoshi Ueda {
1888cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1889466d89a6SKeith Busch 	struct dm_rq_target_io *tio;
1890cec47e3dSKiyoshi Ueda 
1891cec47e3dSKiyoshi Ueda 	if (unlikely(rq->special)) {
1892cec47e3dSKiyoshi Ueda 		DMWARN("Already has something in rq->special.");
1893cec47e3dSKiyoshi Ueda 		return BLKPREP_KILL;
1894cec47e3dSKiyoshi Ueda 	}
1895cec47e3dSKiyoshi Ueda 
1896466d89a6SKeith Busch 	tio = prep_tio(rq, md, GFP_ATOMIC);
1897466d89a6SKeith Busch 	if (!tio)
1898cec47e3dSKiyoshi Ueda 		return BLKPREP_DEFER;
1899cec47e3dSKiyoshi Ueda 
1900466d89a6SKeith Busch 	rq->special = tio;
1901cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_DONTPREP;
1902cec47e3dSKiyoshi Ueda 
1903cec47e3dSKiyoshi Ueda 	return BLKPREP_OK;
1904cec47e3dSKiyoshi Ueda }
1905cec47e3dSKiyoshi Ueda 
19069eef87daSKiyoshi Ueda /*
19079eef87daSKiyoshi Ueda  * Returns:
1908e5863d9aSMike Snitzer  * 0                : the request has been processed
1909e5863d9aSMike Snitzer  * DM_MAPIO_REQUEUE : the original request needs to be requeued
1910e5863d9aSMike Snitzer  * < 0              : the request was completed due to failure
19119eef87daSKiyoshi Ueda  */
1912bfebd1cdSMike Snitzer static int map_request(struct dm_rq_target_io *tio, struct request *rq,
1913cec47e3dSKiyoshi Ueda 		       struct mapped_device *md)
1914cec47e3dSKiyoshi Ueda {
1915e5863d9aSMike Snitzer 	int r;
1916bfebd1cdSMike Snitzer 	struct dm_target *ti = tio->ti;
1917e5863d9aSMike Snitzer 	struct request *clone = NULL;
1918cec47e3dSKiyoshi Ueda 
1919e5863d9aSMike Snitzer 	if (tio->clone) {
1920e5863d9aSMike Snitzer 		clone = tio->clone;
1921cec47e3dSKiyoshi Ueda 		r = ti->type->map_rq(ti, clone, &tio->info);
1922e5863d9aSMike Snitzer 	} else {
1923e5863d9aSMike Snitzer 		r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
1924e5863d9aSMike Snitzer 		if (r < 0) {
1925e5863d9aSMike Snitzer 			/* The target wants to complete the I/O */
1926e5863d9aSMike Snitzer 			dm_kill_unmapped_request(rq, r);
1927e5863d9aSMike Snitzer 			return r;
1928e5863d9aSMike Snitzer 		}
19293a140755SJunichi Nomura 		if (r != DM_MAPIO_REMAPPED)
19303a140755SJunichi Nomura 			return r;
193178d8e58aSMike Snitzer 		if (setup_clone(clone, rq, tio, GFP_ATOMIC)) {
193278d8e58aSMike Snitzer 			/* -ENOMEM */
193378d8e58aSMike Snitzer 			ti->type->release_clone_rq(clone);
193478d8e58aSMike Snitzer 			return DM_MAPIO_REQUEUE;
193578d8e58aSMike Snitzer 		}
1936e5863d9aSMike Snitzer 	}
1937e5863d9aSMike Snitzer 
1938cec47e3dSKiyoshi Ueda 	switch (r) {
1939cec47e3dSKiyoshi Ueda 	case DM_MAPIO_SUBMITTED:
1940cec47e3dSKiyoshi Ueda 		/* The target has taken the I/O to submit by itself later */
1941cec47e3dSKiyoshi Ueda 		break;
1942cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REMAPPED:
1943cec47e3dSKiyoshi Ueda 		/* The target has remapped the I/O so dispatch it */
19446db4ccd6SJun'ichi Nomura 		trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
1945466d89a6SKeith Busch 				     blk_rq_pos(rq));
1946466d89a6SKeith Busch 		dm_dispatch_clone_request(clone, rq);
1947cec47e3dSKiyoshi Ueda 		break;
1948cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REQUEUE:
1949cec47e3dSKiyoshi Ueda 		/* The target wants to requeue the I/O */
19502d76fff1SMike Snitzer 		dm_requeue_original_request(md, tio->orig);
1951cec47e3dSKiyoshi Ueda 		break;
1952cec47e3dSKiyoshi Ueda 	default:
1953cec47e3dSKiyoshi Ueda 		if (r > 0) {
1954cec47e3dSKiyoshi Ueda 			DMWARN("unimplemented target map return value: %d", r);
1955cec47e3dSKiyoshi Ueda 			BUG();
1956cec47e3dSKiyoshi Ueda 		}
1957cec47e3dSKiyoshi Ueda 
1958cec47e3dSKiyoshi Ueda 		/* The target wants to complete the I/O */
1959466d89a6SKeith Busch 		dm_kill_unmapped_request(rq, r);
1960e5863d9aSMike Snitzer 		return r;
1961cec47e3dSKiyoshi Ueda 	}
19629eef87daSKiyoshi Ueda 
1963e5863d9aSMike Snitzer 	return 0;
1964cec47e3dSKiyoshi Ueda }
1965cec47e3dSKiyoshi Ueda 
19662eb6e1e3SKeith Busch static void map_tio_request(struct kthread_work *work)
1967ba1cbad9SMike Snitzer {
19682eb6e1e3SKeith Busch 	struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work);
1969e5863d9aSMike Snitzer 	struct request *rq = tio->orig;
1970e5863d9aSMike Snitzer 	struct mapped_device *md = tio->md;
1971ba1cbad9SMike Snitzer 
1972bfebd1cdSMike Snitzer 	if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE)
19732d76fff1SMike Snitzer 		dm_requeue_original_request(md, rq);
19742eb6e1e3SKeith Busch }
19752eb6e1e3SKeith Busch 
1976466d89a6SKeith Busch static void dm_start_request(struct mapped_device *md, struct request *orig)
1977ba1cbad9SMike Snitzer {
1978bfebd1cdSMike Snitzer 	if (!orig->q->mq_ops)
1979ba1cbad9SMike Snitzer 		blk_start_request(orig);
1980bfebd1cdSMike Snitzer 	else
1981bfebd1cdSMike Snitzer 		blk_mq_start_request(orig);
1982466d89a6SKeith Busch 	atomic_inc(&md->pending[rq_data_dir(orig)]);
1983ba1cbad9SMike Snitzer 
19840ce65797SMike Snitzer 	if (md->seq_rq_merge_deadline_usecs) {
1985de3ec86dSMike Snitzer 		md->last_rq_pos = rq_end_sector(orig);
1986de3ec86dSMike Snitzer 		md->last_rq_rw = rq_data_dir(orig);
19870ce65797SMike Snitzer 		md->last_rq_start_time = ktime_get();
19880ce65797SMike Snitzer 	}
1989de3ec86dSMike Snitzer 
1990e262f347SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats))) {
1991e262f347SMikulas Patocka 		struct dm_rq_target_io *tio = tio_from_request(orig);
1992e262f347SMikulas Patocka 		tio->duration_jiffies = jiffies;
1993e262f347SMikulas Patocka 		tio->n_sectors = blk_rq_sectors(orig);
1994e262f347SMikulas Patocka 		dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
1995e262f347SMikulas Patocka 				    tio->n_sectors, false, 0, &tio->stats_aux);
1996e262f347SMikulas Patocka 	}
1997e262f347SMikulas Patocka 
1998ba1cbad9SMike Snitzer 	/*
1999ba1cbad9SMike Snitzer 	 * Hold the md reference here for the in-flight I/O.
2000ba1cbad9SMike Snitzer 	 * We can't rely on the reference count by device opener,
2001ba1cbad9SMike Snitzer 	 * because the device may be closed during the request completion
2002ba1cbad9SMike Snitzer 	 * when all bios are completed.
2003ba1cbad9SMike Snitzer 	 * See the comment in rq_completed() too.
2004ba1cbad9SMike Snitzer 	 */
2005ba1cbad9SMike Snitzer 	dm_get(md);
2006ba1cbad9SMike Snitzer }
2007ba1cbad9SMike Snitzer 
20080ce65797SMike Snitzer #define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000
20090ce65797SMike Snitzer 
20100ce65797SMike Snitzer ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
20110ce65797SMike Snitzer {
20120ce65797SMike Snitzer 	return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs);
20130ce65797SMike Snitzer }
20140ce65797SMike Snitzer 
20150ce65797SMike Snitzer ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
20160ce65797SMike Snitzer 						     const char *buf, size_t count)
20170ce65797SMike Snitzer {
20180ce65797SMike Snitzer 	unsigned deadline;
20190ce65797SMike Snitzer 
202017e149b8SMike Snitzer 	if (!dm_request_based(md) || md->use_blk_mq)
20210ce65797SMike Snitzer 		return count;
20220ce65797SMike Snitzer 
20230ce65797SMike Snitzer 	if (kstrtouint(buf, 10, &deadline))
20240ce65797SMike Snitzer 		return -EINVAL;
20250ce65797SMike Snitzer 
20260ce65797SMike Snitzer 	if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS)
20270ce65797SMike Snitzer 		deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS;
20280ce65797SMike Snitzer 
20290ce65797SMike Snitzer 	md->seq_rq_merge_deadline_usecs = deadline;
20300ce65797SMike Snitzer 
20310ce65797SMike Snitzer 	return count;
20320ce65797SMike Snitzer }
20330ce65797SMike Snitzer 
20340ce65797SMike Snitzer static bool dm_request_peeked_before_merge_deadline(struct mapped_device *md)
20350ce65797SMike Snitzer {
20360ce65797SMike Snitzer 	ktime_t kt_deadline;
20370ce65797SMike Snitzer 
20380ce65797SMike Snitzer 	if (!md->seq_rq_merge_deadline_usecs)
20390ce65797SMike Snitzer 		return false;
20400ce65797SMike Snitzer 
20410ce65797SMike Snitzer 	kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC);
20420ce65797SMike Snitzer 	kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline);
20430ce65797SMike Snitzer 
20440ce65797SMike Snitzer 	return !ktime_after(ktime_get(), kt_deadline);
20450ce65797SMike Snitzer }
20460ce65797SMike Snitzer 
2047cec47e3dSKiyoshi Ueda /*
2048cec47e3dSKiyoshi Ueda  * q->request_fn for request-based dm.
2049cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
2050cec47e3dSKiyoshi Ueda  */
2051cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q)
2052cec47e3dSKiyoshi Ueda {
2053cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
205483d5e5b0SMikulas Patocka 	int srcu_idx;
205583d5e5b0SMikulas Patocka 	struct dm_table *map = dm_get_live_table(md, &srcu_idx);
2056cec47e3dSKiyoshi Ueda 	struct dm_target *ti;
2057466d89a6SKeith Busch 	struct request *rq;
20582eb6e1e3SKeith Busch 	struct dm_rq_target_io *tio;
205929e4013dSTejun Heo 	sector_t pos;
2060cec47e3dSKiyoshi Ueda 
2061cec47e3dSKiyoshi Ueda 	/*
2062b4324feeSKiyoshi Ueda 	 * For suspend, check blk_queue_stopped() and increment
2063b4324feeSKiyoshi Ueda 	 * ->pending within a single queue_lock not to increment the
2064b4324feeSKiyoshi Ueda 	 * number of in-flight I/Os after the queue is stopped in
2065b4324feeSKiyoshi Ueda 	 * dm_suspend().
2066cec47e3dSKiyoshi Ueda 	 */
20677eaceaccSJens Axboe 	while (!blk_queue_stopped(q)) {
2068cec47e3dSKiyoshi Ueda 		rq = blk_peek_request(q);
2069cec47e3dSKiyoshi Ueda 		if (!rq)
20709d1deb83SMike Snitzer 			goto out;
2071cec47e3dSKiyoshi Ueda 
207229e4013dSTejun Heo 		/* always use block 0 to find the target for flushes for now */
207329e4013dSTejun Heo 		pos = 0;
207429e4013dSTejun Heo 		if (!(rq->cmd_flags & REQ_FLUSH))
207529e4013dSTejun Heo 			pos = blk_rq_pos(rq);
2076d0bcb878SKiyoshi Ueda 
207729e4013dSTejun Heo 		ti = dm_table_find_target(map, pos);
2078ba1cbad9SMike Snitzer 		if (!dm_target_is_valid(ti)) {
2079ba1cbad9SMike Snitzer 			/*
2080466d89a6SKeith Busch 			 * Must perform setup, that rq_completed() requires,
2081ba1cbad9SMike Snitzer 			 * before calling dm_kill_unmapped_request
2082ba1cbad9SMike Snitzer 			 */
2083ba1cbad9SMike Snitzer 			DMERR_LIMIT("request attempted access beyond the end of device");
2084466d89a6SKeith Busch 			dm_start_request(md, rq);
2085466d89a6SKeith Busch 			dm_kill_unmapped_request(rq, -EIO);
2086ba1cbad9SMike Snitzer 			continue;
2087ba1cbad9SMike Snitzer 		}
208829e4013dSTejun Heo 
20890ce65797SMike Snitzer 		if (dm_request_peeked_before_merge_deadline(md) &&
20900ce65797SMike Snitzer 		    md_in_flight(md) && rq->bio && rq->bio->bi_vcnt == 1 &&
2091de3ec86dSMike Snitzer 		    md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq))
2092de3ec86dSMike Snitzer 			goto delay_and_out;
2093de3ec86dSMike Snitzer 
2094cec47e3dSKiyoshi Ueda 		if (ti->type->busy && ti->type->busy(ti))
20957eaceaccSJens Axboe 			goto delay_and_out;
2096cec47e3dSKiyoshi Ueda 
2097466d89a6SKeith Busch 		dm_start_request(md, rq);
2098b4324feeSKiyoshi Ueda 
2099bfebd1cdSMike Snitzer 		tio = tio_from_request(rq);
21002eb6e1e3SKeith Busch 		/* Establish tio->ti before queuing work (map_tio_request) */
21012eb6e1e3SKeith Busch 		tio->ti = ti;
21022eb6e1e3SKeith Busch 		queue_kthread_work(&md->kworker, &tio->work);
2103052189a2SKiyoshi Ueda 		BUG_ON(!irqs_disabled());
2104cec47e3dSKiyoshi Ueda 	}
2105cec47e3dSKiyoshi Ueda 
2106cec47e3dSKiyoshi Ueda 	goto out;
2107cec47e3dSKiyoshi Ueda 
21087eaceaccSJens Axboe delay_and_out:
2109d548b34bSMike Snitzer 	blk_delay_queue(q, HZ / 100);
2110cec47e3dSKiyoshi Ueda out:
211183d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
2112cec47e3dSKiyoshi Ueda }
2113cec47e3dSKiyoshi Ueda 
21141da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
21151da177e4SLinus Torvalds {
21168a57dfc6SChandra Seetharaman 	int r = bdi_bits;
21178a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
21188a57dfc6SChandra Seetharaman 	struct dm_table *map;
21191da177e4SLinus Torvalds 
21201eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
212183d5e5b0SMikulas Patocka 		map = dm_get_live_table_fast(md);
21228a57dfc6SChandra Seetharaman 		if (map) {
2123cec47e3dSKiyoshi Ueda 			/*
2124cec47e3dSKiyoshi Ueda 			 * Request-based dm cares about only own queue for
2125cec47e3dSKiyoshi Ueda 			 * the query about congestion status of request_queue
2126cec47e3dSKiyoshi Ueda 			 */
2127cec47e3dSKiyoshi Ueda 			if (dm_request_based(md))
21284452226eSTejun Heo 				r = md->queue->backing_dev_info.wb.state &
2129cec47e3dSKiyoshi Ueda 				    bdi_bits;
2130cec47e3dSKiyoshi Ueda 			else
21311da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
21328a57dfc6SChandra Seetharaman 		}
213383d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
21348a57dfc6SChandra Seetharaman 	}
21358a57dfc6SChandra Seetharaman 
21361da177e4SLinus Torvalds 	return r;
21371da177e4SLinus Torvalds }
21381da177e4SLinus Torvalds 
21391da177e4SLinus Torvalds /*-----------------------------------------------------------------
21401da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
21411da177e4SLinus Torvalds  *---------------------------------------------------------------*/
21422b06cfffSAlasdair G Kergon static void free_minor(int minor)
21431da177e4SLinus Torvalds {
2144f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
21451da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
2146f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
21471da177e4SLinus Torvalds }
21481da177e4SLinus Torvalds 
21491da177e4SLinus Torvalds /*
21501da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
21511da177e4SLinus Torvalds  */
2152cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
21531da177e4SLinus Torvalds {
2154c9d76be6STejun Heo 	int r;
21551da177e4SLinus Torvalds 
21561da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
21571da177e4SLinus Torvalds 		return -EINVAL;
21581da177e4SLinus Torvalds 
2159c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
2160f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
21611da177e4SLinus Torvalds 
2162c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
21631da177e4SLinus Torvalds 
2164f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2165c9d76be6STejun Heo 	idr_preload_end();
2166c9d76be6STejun Heo 	if (r < 0)
2167c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
2168c9d76be6STejun Heo 	return 0;
21691da177e4SLinus Torvalds }
21701da177e4SLinus Torvalds 
2171cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
21721da177e4SLinus Torvalds {
2173c9d76be6STejun Heo 	int r;
21741da177e4SLinus Torvalds 
2175c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
2176f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
21771da177e4SLinus Torvalds 
2178c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
21791da177e4SLinus Torvalds 
2180f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2181c9d76be6STejun Heo 	idr_preload_end();
2182c9d76be6STejun Heo 	if (r < 0)
21831da177e4SLinus Torvalds 		return r;
2184c9d76be6STejun Heo 	*minor = r;
2185c9d76be6STejun Heo 	return 0;
21861da177e4SLinus Torvalds }
21871da177e4SLinus Torvalds 
218883d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
21891da177e4SLinus Torvalds 
219053d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
219153d5914fSMikulas Patocka 
21924a0b4ddfSMike Snitzer static void dm_init_md_queue(struct mapped_device *md)
21934a0b4ddfSMike Snitzer {
21944a0b4ddfSMike Snitzer 	/*
21954a0b4ddfSMike Snitzer 	 * Request-based dm devices cannot be stacked on top of bio-based dm
2196bfebd1cdSMike Snitzer 	 * devices.  The type of this dm device may not have been decided yet.
21974a0b4ddfSMike Snitzer 	 * The type is decided at the first table loading time.
21984a0b4ddfSMike Snitzer 	 * To prevent problematic device stacking, clear the queue flag
21994a0b4ddfSMike Snitzer 	 * for request stacking support until then.
22004a0b4ddfSMike Snitzer 	 *
22014a0b4ddfSMike Snitzer 	 * This queue is new, so no concurrency on the queue_flags.
22024a0b4ddfSMike Snitzer 	 */
22034a0b4ddfSMike Snitzer 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
2204bfebd1cdSMike Snitzer }
22054a0b4ddfSMike Snitzer 
2206bfebd1cdSMike Snitzer static void dm_init_old_md_queue(struct mapped_device *md)
2207bfebd1cdSMike Snitzer {
220817e149b8SMike Snitzer 	md->use_blk_mq = false;
2209bfebd1cdSMike Snitzer 	dm_init_md_queue(md);
2210bfebd1cdSMike Snitzer 
2211bfebd1cdSMike Snitzer 	/*
2212bfebd1cdSMike Snitzer 	 * Initialize aspects of queue that aren't relevant for blk-mq
2213bfebd1cdSMike Snitzer 	 */
22144a0b4ddfSMike Snitzer 	md->queue->queuedata = md;
22154a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
22164a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_data = md;
2217ff36ab34SMike Snitzer 
22184a0b4ddfSMike Snitzer 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
22194a0b4ddfSMike Snitzer }
22204a0b4ddfSMike Snitzer 
22210f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md)
22220f20972fSMike Snitzer {
22230f20972fSMike Snitzer 	cleanup_srcu_struct(&md->io_barrier);
22240f20972fSMike Snitzer 
22250f20972fSMike Snitzer 	if (md->wq)
22260f20972fSMike Snitzer 		destroy_workqueue(md->wq);
22270f20972fSMike Snitzer 	if (md->kworker_task)
22280f20972fSMike Snitzer 		kthread_stop(md->kworker_task);
22290f20972fSMike Snitzer 	if (md->io_pool)
22300f20972fSMike Snitzer 		mempool_destroy(md->io_pool);
22310f20972fSMike Snitzer 	if (md->rq_pool)
22320f20972fSMike Snitzer 		mempool_destroy(md->rq_pool);
22330f20972fSMike Snitzer 	if (md->bs)
22340f20972fSMike Snitzer 		bioset_free(md->bs);
22350f20972fSMike Snitzer 
22360f20972fSMike Snitzer 	if (md->disk) {
22370f20972fSMike Snitzer 		spin_lock(&_minor_lock);
22380f20972fSMike Snitzer 		md->disk->private_data = NULL;
22390f20972fSMike Snitzer 		spin_unlock(&_minor_lock);
22400f20972fSMike Snitzer 		if (blk_get_integrity(md->disk))
22410f20972fSMike Snitzer 			blk_integrity_unregister(md->disk);
22420f20972fSMike Snitzer 		del_gendisk(md->disk);
22430f20972fSMike Snitzer 		put_disk(md->disk);
22440f20972fSMike Snitzer 	}
22450f20972fSMike Snitzer 
22460f20972fSMike Snitzer 	if (md->queue)
22470f20972fSMike Snitzer 		blk_cleanup_queue(md->queue);
22480f20972fSMike Snitzer 
22490f20972fSMike Snitzer 	if (md->bdev) {
22500f20972fSMike Snitzer 		bdput(md->bdev);
22510f20972fSMike Snitzer 		md->bdev = NULL;
22520f20972fSMike Snitzer 	}
22530f20972fSMike Snitzer }
22540f20972fSMike Snitzer 
22551da177e4SLinus Torvalds /*
22561da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
22571da177e4SLinus Torvalds  */
22582b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
22591da177e4SLinus Torvalds {
22601da177e4SLinus Torvalds 	int r;
2261cf13ab8eSFrederik Deweerdt 	struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
2262ba61fdd1SJeff Mahoney 	void *old_md;
22631da177e4SLinus Torvalds 
22641da177e4SLinus Torvalds 	if (!md) {
22651da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
22661da177e4SLinus Torvalds 		return NULL;
22671da177e4SLinus Torvalds 	}
22681da177e4SLinus Torvalds 
226910da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
22706ed7ade8SMilan Broz 		goto bad_module_get;
227110da4f79SJeff Mahoney 
22721da177e4SLinus Torvalds 	/* get a minor number for the dev */
22732b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
2274cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
22752b06cfffSAlasdair G Kergon 	else
2276cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
22771da177e4SLinus Torvalds 	if (r < 0)
22786ed7ade8SMilan Broz 		goto bad_minor;
22791da177e4SLinus Torvalds 
228083d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
228183d5e5b0SMikulas Patocka 	if (r < 0)
228283d5e5b0SMikulas Patocka 		goto bad_io_barrier;
228383d5e5b0SMikulas Patocka 
228417e149b8SMike Snitzer 	md->use_blk_mq = use_blk_mq;
2285a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
2286e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
2287a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
228886f1152bSBenjamin Marzinski 	mutex_init(&md->table_devices_lock);
2289022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
22901da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
22915c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
22921da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
22937a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
22947a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
229586f1152bSBenjamin Marzinski 	INIT_LIST_HEAD(&md->table_devices);
22967a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
22971da177e4SLinus Torvalds 
22984a0b4ddfSMike Snitzer 	md->queue = blk_alloc_queue(GFP_KERNEL);
22991da177e4SLinus Torvalds 	if (!md->queue)
23000f20972fSMike Snitzer 		goto bad;
23011da177e4SLinus Torvalds 
23024a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
23039faf400fSStefan Bader 
23041da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
23051da177e4SLinus Torvalds 	if (!md->disk)
23060f20972fSMike Snitzer 		goto bad;
23071da177e4SLinus Torvalds 
2308316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
2309316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
2310f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
231153d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
2312f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
23132995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
23142eb6e1e3SKeith Busch 	md->kworker_task = NULL;
2315f0b04115SJeff Mahoney 
23161da177e4SLinus Torvalds 	md->disk->major = _major;
23171da177e4SLinus Torvalds 	md->disk->first_minor = minor;
23181da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
23191da177e4SLinus Torvalds 	md->disk->queue = md->queue;
23201da177e4SLinus Torvalds 	md->disk->private_data = md;
23211da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
23221da177e4SLinus Torvalds 	add_disk(md->disk);
23237e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
23241da177e4SLinus Torvalds 
2325670368a8STejun Heo 	md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
2326304f3f6aSMilan Broz 	if (!md->wq)
23270f20972fSMike Snitzer 		goto bad;
2328304f3f6aSMilan Broz 
232932a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
233032a926daSMikulas Patocka 	if (!md->bdev)
23310f20972fSMike Snitzer 		goto bad;
233232a926daSMikulas Patocka 
23336a8736d1STejun Heo 	bio_init(&md->flush_bio);
23346a8736d1STejun Heo 	md->flush_bio.bi_bdev = md->bdev;
23356a8736d1STejun Heo 	md->flush_bio.bi_rw = WRITE_FLUSH;
23366a8736d1STejun Heo 
2337fd2ed4d2SMikulas Patocka 	dm_stats_init(&md->stats);
2338fd2ed4d2SMikulas Patocka 
2339ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
2340f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
2341ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
2342f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2343ba61fdd1SJeff Mahoney 
2344ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
2345ba61fdd1SJeff Mahoney 
23461da177e4SLinus Torvalds 	return md;
23471da177e4SLinus Torvalds 
23480f20972fSMike Snitzer bad:
23490f20972fSMike Snitzer 	cleanup_mapped_device(md);
235083d5e5b0SMikulas Patocka bad_io_barrier:
23511da177e4SLinus Torvalds 	free_minor(minor);
23526ed7ade8SMilan Broz bad_minor:
235310da4f79SJeff Mahoney 	module_put(THIS_MODULE);
23546ed7ade8SMilan Broz bad_module_get:
23551da177e4SLinus Torvalds 	kfree(md);
23561da177e4SLinus Torvalds 	return NULL;
23571da177e4SLinus Torvalds }
23581da177e4SLinus Torvalds 
2359ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
2360ae9da83fSJun'ichi Nomura 
23611da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
23621da177e4SLinus Torvalds {
2363f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
236463d94e48SJun'ichi Nomura 
2365ae9da83fSJun'ichi Nomura 	unlock_fs(md);
23662eb6e1e3SKeith Busch 
23670f20972fSMike Snitzer 	cleanup_mapped_device(md);
236817e149b8SMike Snitzer 	if (md->use_blk_mq)
2369bfebd1cdSMike Snitzer 		blk_mq_free_tag_set(&md->tag_set);
23700f20972fSMike Snitzer 
23710f20972fSMike Snitzer 	free_table_devices(&md->table_devices);
23720f20972fSMike Snitzer 	dm_stats_cleanup(&md->stats);
237363a4f065SMike Snitzer 	free_minor(minor);
237463a4f065SMike Snitzer 
237510da4f79SJeff Mahoney 	module_put(THIS_MODULE);
23761da177e4SLinus Torvalds 	kfree(md);
23771da177e4SLinus Torvalds }
23781da177e4SLinus Torvalds 
2379e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2380e6ee8c0bSKiyoshi Ueda {
2381c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
2382e6ee8c0bSKiyoshi Ueda 
23834e6e36c3SMike Snitzer 	if (md->bs) {
23844e6e36c3SMike Snitzer 		/* The md already has necessary mempools. */
23854e6e36c3SMike Snitzer 		if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
2386c0820cf5SMikulas Patocka 			/*
238716245bdcSJun'ichi Nomura 			 * Reload bioset because front_pad may have changed
238816245bdcSJun'ichi Nomura 			 * because a different table was loaded.
2389c0820cf5SMikulas Patocka 			 */
2390c0820cf5SMikulas Patocka 			bioset_free(md->bs);
2391c0820cf5SMikulas Patocka 			md->bs = p->bs;
2392c0820cf5SMikulas Patocka 			p->bs = NULL;
2393c0820cf5SMikulas Patocka 		}
2394cbc4e3c1SMike Snitzer 		/*
23954e6e36c3SMike Snitzer 		 * There's no need to reload with request-based dm
23964e6e36c3SMike Snitzer 		 * because the size of front_pad doesn't change.
23974e6e36c3SMike Snitzer 		 * Note for future: If you are to reload bioset,
23984e6e36c3SMike Snitzer 		 * prep-ed requests in the queue may refer
23994e6e36c3SMike Snitzer 		 * to bio from the old bioset, so you must walk
24004e6e36c3SMike Snitzer 		 * through the queue to unprep.
2401cbc4e3c1SMike Snitzer 		 */
2402cbc4e3c1SMike Snitzer 		goto out;
2403cbc4e3c1SMike Snitzer 	}
2404cbc4e3c1SMike Snitzer 
2405cbc4e3c1SMike Snitzer 	BUG_ON(!p || md->io_pool || md->rq_pool || md->bs);
2406e6ee8c0bSKiyoshi Ueda 
2407e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
2408e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
24091ae49ea2SMike Snitzer 	md->rq_pool = p->rq_pool;
24101ae49ea2SMike Snitzer 	p->rq_pool = NULL;
2411e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
2412e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
24134e6e36c3SMike Snitzer 
2414e6ee8c0bSKiyoshi Ueda out:
241502233342SMike Snitzer 	/* mempool bind completed, no longer need any mempools in the table */
2416e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
2417e6ee8c0bSKiyoshi Ueda }
2418e6ee8c0bSKiyoshi Ueda 
24191da177e4SLinus Torvalds /*
24201da177e4SLinus Torvalds  * Bind a table to the device.
24211da177e4SLinus Torvalds  */
24221da177e4SLinus Torvalds static void event_callback(void *context)
24231da177e4SLinus Torvalds {
24247a8c3d3bSMike Anderson 	unsigned long flags;
24257a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
24261da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
24271da177e4SLinus Torvalds 
24287a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
24297a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
24307a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
24317a8c3d3bSMike Anderson 
2432ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
24337a8c3d3bSMike Anderson 
24341da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
24351da177e4SLinus Torvalds 	wake_up(&md->eventq);
24361da177e4SLinus Torvalds }
24371da177e4SLinus Torvalds 
2438c217649bSMike Snitzer /*
2439c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
2440c217649bSMike Snitzer  */
24414e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
24421da177e4SLinus Torvalds {
24434e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
24441da177e4SLinus Torvalds 
2445db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
24461da177e4SLinus Torvalds }
24471da177e4SLinus Torvalds 
2448042d2a9bSAlasdair G Kergon /*
2449042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2450042d2a9bSAlasdair G Kergon  */
2451042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2452754c5fc7SMike Snitzer 			       struct queue_limits *limits)
24531da177e4SLinus Torvalds {
2454042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
2455165125e1SJens Axboe 	struct request_queue *q = md->queue;
24561da177e4SLinus Torvalds 	sector_t size;
24571da177e4SLinus Torvalds 
24581da177e4SLinus Torvalds 	size = dm_table_get_size(t);
24593ac51e74SDarrick J. Wong 
24603ac51e74SDarrick J. Wong 	/*
24613ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
24623ac51e74SDarrick J. Wong 	 */
2463fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
24643ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
24653ac51e74SDarrick J. Wong 
24664e90188bSAlasdair G Kergon 	__set_size(md, size);
24671da177e4SLinus Torvalds 
2468cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
24692ca3310eSAlasdair G Kergon 
2470e6ee8c0bSKiyoshi Ueda 	/*
2471e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
2472e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
2473e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
2474e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
2475e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
2476e6ee8c0bSKiyoshi Ueda 	 */
2477bfebd1cdSMike Snitzer 	if (dm_table_request_based(t))
2478e6ee8c0bSKiyoshi Ueda 		stop_queue(q);
2479e6ee8c0bSKiyoshi Ueda 
2480e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
2481e6ee8c0bSKiyoshi Ueda 
2482a12f5d48SEric Dumazet 	old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
248383d5e5b0SMikulas Patocka 	rcu_assign_pointer(md->map, t);
248436a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
248536a0456fSAlasdair G Kergon 
2486754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
248741abc4e1SHannes Reinecke 	if (old_map)
248883d5e5b0SMikulas Patocka 		dm_sync_table(md);
24892ca3310eSAlasdair G Kergon 
2490042d2a9bSAlasdair G Kergon 	return old_map;
24911da177e4SLinus Torvalds }
24921da177e4SLinus Torvalds 
2493a7940155SAlasdair G Kergon /*
2494a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2495a7940155SAlasdair G Kergon  */
2496a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
24971da177e4SLinus Torvalds {
2498a12f5d48SEric Dumazet 	struct dm_table *map = rcu_dereference_protected(md->map, 1);
24991da177e4SLinus Torvalds 
25001da177e4SLinus Torvalds 	if (!map)
2501a7940155SAlasdair G Kergon 		return NULL;
25021da177e4SLinus Torvalds 
25031da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
25049cdb8520SMonam Agarwal 	RCU_INIT_POINTER(md->map, NULL);
250583d5e5b0SMikulas Patocka 	dm_sync_table(md);
2506a7940155SAlasdair G Kergon 
2507a7940155SAlasdair G Kergon 	return map;
25081da177e4SLinus Torvalds }
25091da177e4SLinus Torvalds 
25101da177e4SLinus Torvalds /*
25111da177e4SLinus Torvalds  * Constructor for a new device.
25121da177e4SLinus Torvalds  */
25132b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
25141da177e4SLinus Torvalds {
25151da177e4SLinus Torvalds 	struct mapped_device *md;
25161da177e4SLinus Torvalds 
25172b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
25181da177e4SLinus Torvalds 	if (!md)
25191da177e4SLinus Torvalds 		return -ENXIO;
25201da177e4SLinus Torvalds 
2521784aae73SMilan Broz 	dm_sysfs_init(md);
2522784aae73SMilan Broz 
25231da177e4SLinus Torvalds 	*result = md;
25241da177e4SLinus Torvalds 	return 0;
25251da177e4SLinus Torvalds }
25261da177e4SLinus Torvalds 
2527a5664dadSMike Snitzer /*
2528a5664dadSMike Snitzer  * Functions to manage md->type.
2529a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2530a5664dadSMike Snitzer  */
2531a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2532a5664dadSMike Snitzer {
2533a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2534a5664dadSMike Snitzer }
2535a5664dadSMike Snitzer 
2536a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2537a5664dadSMike Snitzer {
2538a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2539a5664dadSMike Snitzer }
2540a5664dadSMike Snitzer 
2541a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type)
2542a5664dadSMike Snitzer {
254300c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2544a5664dadSMike Snitzer 	md->type = type;
2545a5664dadSMike Snitzer }
2546a5664dadSMike Snitzer 
2547a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md)
2548a5664dadSMike Snitzer {
254900c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2550a5664dadSMike Snitzer 	return md->type;
2551a5664dadSMike Snitzer }
2552a5664dadSMike Snitzer 
255336a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
255436a0456fSAlasdair G Kergon {
255536a0456fSAlasdair G Kergon 	return md->immutable_target_type;
255636a0456fSAlasdair G Kergon }
255736a0456fSAlasdair G Kergon 
25584a0b4ddfSMike Snitzer /*
2559f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
2560f84cb8a4SMike Snitzer  * count on 'md'.
2561f84cb8a4SMike Snitzer  */
2562f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2563f84cb8a4SMike Snitzer {
2564f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
2565f84cb8a4SMike Snitzer 	return &md->queue->limits;
2566f84cb8a4SMike Snitzer }
2567f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2568f84cb8a4SMike Snitzer 
2569bfebd1cdSMike Snitzer static void init_rq_based_worker_thread(struct mapped_device *md)
2570bfebd1cdSMike Snitzer {
2571bfebd1cdSMike Snitzer 	/* Initialize the request-based DM worker thread */
2572bfebd1cdSMike Snitzer 	init_kthread_worker(&md->kworker);
2573bfebd1cdSMike Snitzer 	md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker,
2574bfebd1cdSMike Snitzer 				       "kdmwork-%s", dm_device_name(md));
2575bfebd1cdSMike Snitzer }
2576bfebd1cdSMike Snitzer 
2577f84cb8a4SMike Snitzer /*
25784a0b4ddfSMike Snitzer  * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
25794a0b4ddfSMike Snitzer  */
25804a0b4ddfSMike Snitzer static int dm_init_request_based_queue(struct mapped_device *md)
25814a0b4ddfSMike Snitzer {
25824a0b4ddfSMike Snitzer 	struct request_queue *q = NULL;
25834a0b4ddfSMike Snitzer 
25844a0b4ddfSMike Snitzer 	/* Fully initialize the queue */
25854a0b4ddfSMike Snitzer 	q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
25864a0b4ddfSMike Snitzer 	if (!q)
2587bfebd1cdSMike Snitzer 		return -EINVAL;
25884a0b4ddfSMike Snitzer 
25890ce65797SMike Snitzer 	/* disable dm_request_fn's merge heuristic by default */
25900ce65797SMike Snitzer 	md->seq_rq_merge_deadline_usecs = 0;
25910ce65797SMike Snitzer 
25924a0b4ddfSMike Snitzer 	md->queue = q;
2593bfebd1cdSMike Snitzer 	dm_init_old_md_queue(md);
25944a0b4ddfSMike Snitzer 	blk_queue_softirq_done(md->queue, dm_softirq_done);
25954a0b4ddfSMike Snitzer 	blk_queue_prep_rq(md->queue, dm_prep_fn);
25964a0b4ddfSMike Snitzer 
2597bfebd1cdSMike Snitzer 	init_rq_based_worker_thread(md);
25982eb6e1e3SKeith Busch 
25994a0b4ddfSMike Snitzer 	elv_register_queue(md->queue);
26004a0b4ddfSMike Snitzer 
2601bfebd1cdSMike Snitzer 	return 0;
2602bfebd1cdSMike Snitzer }
2603bfebd1cdSMike Snitzer 
2604bfebd1cdSMike Snitzer static int dm_mq_init_request(void *data, struct request *rq,
2605bfebd1cdSMike Snitzer 			      unsigned int hctx_idx, unsigned int request_idx,
2606bfebd1cdSMike Snitzer 			      unsigned int numa_node)
2607bfebd1cdSMike Snitzer {
2608bfebd1cdSMike Snitzer 	struct mapped_device *md = data;
2609bfebd1cdSMike Snitzer 	struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2610bfebd1cdSMike Snitzer 
2611bfebd1cdSMike Snitzer 	/*
2612bfebd1cdSMike Snitzer 	 * Must initialize md member of tio, otherwise it won't
2613bfebd1cdSMike Snitzer 	 * be available in dm_mq_queue_rq.
2614bfebd1cdSMike Snitzer 	 */
2615bfebd1cdSMike Snitzer 	tio->md = md;
2616bfebd1cdSMike Snitzer 
2617bfebd1cdSMike Snitzer 	return 0;
2618bfebd1cdSMike Snitzer }
2619bfebd1cdSMike Snitzer 
2620bfebd1cdSMike Snitzer static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
2621bfebd1cdSMike Snitzer 			  const struct blk_mq_queue_data *bd)
2622bfebd1cdSMike Snitzer {
2623bfebd1cdSMike Snitzer 	struct request *rq = bd->rq;
2624bfebd1cdSMike Snitzer 	struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
2625bfebd1cdSMike Snitzer 	struct mapped_device *md = tio->md;
2626bfebd1cdSMike Snitzer 	int srcu_idx;
2627bfebd1cdSMike Snitzer 	struct dm_table *map = dm_get_live_table(md, &srcu_idx);
2628bfebd1cdSMike Snitzer 	struct dm_target *ti;
2629bfebd1cdSMike Snitzer 	sector_t pos;
2630bfebd1cdSMike Snitzer 
2631bfebd1cdSMike Snitzer 	/* always use block 0 to find the target for flushes for now */
2632bfebd1cdSMike Snitzer 	pos = 0;
2633bfebd1cdSMike Snitzer 	if (!(rq->cmd_flags & REQ_FLUSH))
2634bfebd1cdSMike Snitzer 		pos = blk_rq_pos(rq);
2635bfebd1cdSMike Snitzer 
2636bfebd1cdSMike Snitzer 	ti = dm_table_find_target(map, pos);
2637bfebd1cdSMike Snitzer 	if (!dm_target_is_valid(ti)) {
2638bfebd1cdSMike Snitzer 		dm_put_live_table(md, srcu_idx);
2639bfebd1cdSMike Snitzer 		DMERR_LIMIT("request attempted access beyond the end of device");
2640bfebd1cdSMike Snitzer 		/*
2641bfebd1cdSMike Snitzer 		 * Must perform setup, that rq_completed() requires,
2642bfebd1cdSMike Snitzer 		 * before returning BLK_MQ_RQ_QUEUE_ERROR
2643bfebd1cdSMike Snitzer 		 */
2644bfebd1cdSMike Snitzer 		dm_start_request(md, rq);
2645bfebd1cdSMike Snitzer 		return BLK_MQ_RQ_QUEUE_ERROR;
2646bfebd1cdSMike Snitzer 	}
2647bfebd1cdSMike Snitzer 	dm_put_live_table(md, srcu_idx);
2648bfebd1cdSMike Snitzer 
2649bfebd1cdSMike Snitzer 	if (ti->type->busy && ti->type->busy(ti))
2650bfebd1cdSMike Snitzer 		return BLK_MQ_RQ_QUEUE_BUSY;
2651bfebd1cdSMike Snitzer 
2652bfebd1cdSMike Snitzer 	dm_start_request(md, rq);
2653bfebd1cdSMike Snitzer 
2654bfebd1cdSMike Snitzer 	/* Init tio using md established in .init_request */
2655bfebd1cdSMike Snitzer 	init_tio(tio, rq, md);
2656bfebd1cdSMike Snitzer 
265702233342SMike Snitzer 	/*
265802233342SMike Snitzer 	 * Establish tio->ti before queuing work (map_tio_request)
265902233342SMike Snitzer 	 * or making direct call to map_request().
266002233342SMike Snitzer 	 */
2661bfebd1cdSMike Snitzer 	tio->ti = ti;
266202233342SMike Snitzer 
266302233342SMike Snitzer 	/* Clone the request if underlying devices aren't blk-mq */
266402233342SMike Snitzer 	if (dm_table_get_type(map) == DM_TYPE_REQUEST_BASED) {
266502233342SMike Snitzer 		/* clone request is allocated at the end of the pdu */
266602233342SMike Snitzer 		tio->clone = (void *)blk_mq_rq_to_pdu(rq) + sizeof(struct dm_rq_target_io);
266745714fbeSMike Snitzer 		(void) clone_rq(rq, md, tio, GFP_ATOMIC);
2668bfebd1cdSMike Snitzer 		queue_kthread_work(&md->kworker, &tio->work);
266902233342SMike Snitzer 	} else {
267002233342SMike Snitzer 		/* Direct call is fine since .queue_rq allows allocations */
267145714fbeSMike Snitzer 		if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) {
267245714fbeSMike Snitzer 			/* Undo dm_start_request() before requeuing */
2673e262f347SMikulas Patocka 			rq_end_stats(md, rq);
267445714fbeSMike Snitzer 			rq_completed(md, rq_data_dir(rq), false);
267545714fbeSMike Snitzer 			return BLK_MQ_RQ_QUEUE_BUSY;
267645714fbeSMike Snitzer 		}
267702233342SMike Snitzer 	}
2678bfebd1cdSMike Snitzer 
2679bfebd1cdSMike Snitzer 	return BLK_MQ_RQ_QUEUE_OK;
2680bfebd1cdSMike Snitzer }
2681bfebd1cdSMike Snitzer 
2682bfebd1cdSMike Snitzer static struct blk_mq_ops dm_mq_ops = {
2683bfebd1cdSMike Snitzer 	.queue_rq = dm_mq_queue_rq,
2684bfebd1cdSMike Snitzer 	.map_queue = blk_mq_map_queue,
2685bfebd1cdSMike Snitzer 	.complete = dm_softirq_done,
2686bfebd1cdSMike Snitzer 	.init_request = dm_mq_init_request,
2687bfebd1cdSMike Snitzer };
2688bfebd1cdSMike Snitzer 
2689bfebd1cdSMike Snitzer static int dm_init_request_based_blk_mq_queue(struct mapped_device *md)
2690bfebd1cdSMike Snitzer {
269102233342SMike Snitzer 	unsigned md_type = dm_get_md_type(md);
2692bfebd1cdSMike Snitzer 	struct request_queue *q;
2693bfebd1cdSMike Snitzer 	int err;
2694bfebd1cdSMike Snitzer 
2695bfebd1cdSMike Snitzer 	memset(&md->tag_set, 0, sizeof(md->tag_set));
2696bfebd1cdSMike Snitzer 	md->tag_set.ops = &dm_mq_ops;
2697bfebd1cdSMike Snitzer 	md->tag_set.queue_depth = BLKDEV_MAX_RQ;
2698bfebd1cdSMike Snitzer 	md->tag_set.numa_node = NUMA_NO_NODE;
2699bfebd1cdSMike Snitzer 	md->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
2700bfebd1cdSMike Snitzer 	md->tag_set.nr_hw_queues = 1;
270102233342SMike Snitzer 	if (md_type == DM_TYPE_REQUEST_BASED) {
270202233342SMike Snitzer 		/* make the memory for non-blk-mq clone part of the pdu */
270302233342SMike Snitzer 		md->tag_set.cmd_size = sizeof(struct dm_rq_target_io) + sizeof(struct request);
270402233342SMike Snitzer 	} else
2705bfebd1cdSMike Snitzer 		md->tag_set.cmd_size = sizeof(struct dm_rq_target_io);
2706bfebd1cdSMike Snitzer 	md->tag_set.driver_data = md;
2707bfebd1cdSMike Snitzer 
2708bfebd1cdSMike Snitzer 	err = blk_mq_alloc_tag_set(&md->tag_set);
2709bfebd1cdSMike Snitzer 	if (err)
2710bfebd1cdSMike Snitzer 		return err;
2711bfebd1cdSMike Snitzer 
2712bfebd1cdSMike Snitzer 	q = blk_mq_init_allocated_queue(&md->tag_set, md->queue);
2713bfebd1cdSMike Snitzer 	if (IS_ERR(q)) {
2714bfebd1cdSMike Snitzer 		err = PTR_ERR(q);
2715bfebd1cdSMike Snitzer 		goto out_tag_set;
2716bfebd1cdSMike Snitzer 	}
2717bfebd1cdSMike Snitzer 	md->queue = q;
2718bfebd1cdSMike Snitzer 	dm_init_md_queue(md);
2719bfebd1cdSMike Snitzer 
2720bfebd1cdSMike Snitzer 	/* backfill 'mq' sysfs registration normally done in blk_register_queue */
2721bfebd1cdSMike Snitzer 	blk_mq_register_disk(md->disk);
2722bfebd1cdSMike Snitzer 
272302233342SMike Snitzer 	if (md_type == DM_TYPE_REQUEST_BASED)
2724bfebd1cdSMike Snitzer 		init_rq_based_worker_thread(md);
2725bfebd1cdSMike Snitzer 
2726bfebd1cdSMike Snitzer 	return 0;
2727bfebd1cdSMike Snitzer 
2728bfebd1cdSMike Snitzer out_tag_set:
2729bfebd1cdSMike Snitzer 	blk_mq_free_tag_set(&md->tag_set);
2730bfebd1cdSMike Snitzer 	return err;
27314a0b4ddfSMike Snitzer }
27324a0b4ddfSMike Snitzer 
27334e6e36c3SMike Snitzer static unsigned filter_md_type(unsigned type, struct mapped_device *md)
27344e6e36c3SMike Snitzer {
27354e6e36c3SMike Snitzer 	if (type == DM_TYPE_BIO_BASED)
27364e6e36c3SMike Snitzer 		return type;
27374e6e36c3SMike Snitzer 
27384e6e36c3SMike Snitzer 	return !md->use_blk_mq ? DM_TYPE_REQUEST_BASED : DM_TYPE_MQ_REQUEST_BASED;
27394e6e36c3SMike Snitzer }
27404e6e36c3SMike Snitzer 
27414a0b4ddfSMike Snitzer /*
27424a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
27434a0b4ddfSMike Snitzer  */
27444a0b4ddfSMike Snitzer int dm_setup_md_queue(struct mapped_device *md)
27454a0b4ddfSMike Snitzer {
2746bfebd1cdSMike Snitzer 	int r;
274717e149b8SMike Snitzer 	unsigned md_type = filter_md_type(dm_get_md_type(md), md);
2748bfebd1cdSMike Snitzer 
2749bfebd1cdSMike Snitzer 	switch (md_type) {
2750bfebd1cdSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
2751bfebd1cdSMike Snitzer 		r = dm_init_request_based_queue(md);
2752bfebd1cdSMike Snitzer 		if (r) {
27534a0b4ddfSMike Snitzer 			DMWARN("Cannot initialize queue for request-based mapped device");
2754bfebd1cdSMike Snitzer 			return r;
27554a0b4ddfSMike Snitzer 		}
2756bfebd1cdSMike Snitzer 		break;
2757bfebd1cdSMike Snitzer 	case DM_TYPE_MQ_REQUEST_BASED:
2758bfebd1cdSMike Snitzer 		r = dm_init_request_based_blk_mq_queue(md);
2759bfebd1cdSMike Snitzer 		if (r) {
2760bfebd1cdSMike Snitzer 			DMWARN("Cannot initialize queue for request-based blk-mq mapped device");
2761bfebd1cdSMike Snitzer 			return r;
2762bfebd1cdSMike Snitzer 		}
2763bfebd1cdSMike Snitzer 		break;
2764bfebd1cdSMike Snitzer 	case DM_TYPE_BIO_BASED:
2765bfebd1cdSMike Snitzer 		dm_init_old_md_queue(md);
2766ff36ab34SMike Snitzer 		blk_queue_make_request(md->queue, dm_make_request);
2767bfebd1cdSMike Snitzer 		break;
2768ff36ab34SMike Snitzer 	}
27694a0b4ddfSMike Snitzer 
27704a0b4ddfSMike Snitzer 	return 0;
27714a0b4ddfSMike Snitzer }
27724a0b4ddfSMike Snitzer 
27732bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev)
27741da177e4SLinus Torvalds {
27751da177e4SLinus Torvalds 	struct mapped_device *md;
27761da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
27771da177e4SLinus Torvalds 
27781da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
27791da177e4SLinus Torvalds 		return NULL;
27801da177e4SLinus Torvalds 
2781f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
27821da177e4SLinus Torvalds 
27831da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
27842bec1f4aSMikulas Patocka 	if (md) {
27852bec1f4aSMikulas Patocka 		if ((md == MINOR_ALLOCED ||
2786f331c029STejun Heo 		     (MINOR(disk_devt(dm_disk(md))) != minor) ||
2787abdc568bSKiyoshi Ueda 		     dm_deleting_md(md) ||
2788fba9f90eSJeff Mahoney 		     test_bit(DMF_FREEING, &md->flags))) {
2789637842cfSDavid Teigland 			md = NULL;
2790fba9f90eSJeff Mahoney 			goto out;
2791fba9f90eSJeff Mahoney 		}
27922bec1f4aSMikulas Patocka 		dm_get(md);
27932bec1f4aSMikulas Patocka 	}
27941da177e4SLinus Torvalds 
2795fba9f90eSJeff Mahoney out:
2796f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
27971da177e4SLinus Torvalds 
2798637842cfSDavid Teigland 	return md;
2799637842cfSDavid Teigland }
28003cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2801d229a958SDavid Teigland 
28029ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2803637842cfSDavid Teigland {
28049ade92a9SAlasdair G Kergon 	return md->interface_ptr;
28051da177e4SLinus Torvalds }
28061da177e4SLinus Torvalds 
28071da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
28081da177e4SLinus Torvalds {
28091da177e4SLinus Torvalds 	md->interface_ptr = ptr;
28101da177e4SLinus Torvalds }
28111da177e4SLinus Torvalds 
28121da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
28131da177e4SLinus Torvalds {
28141da177e4SLinus Torvalds 	atomic_inc(&md->holders);
28153f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
28161da177e4SLinus Torvalds }
28171da177e4SLinus Torvalds 
281809ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md)
281909ee96b2SMikulas Patocka {
282009ee96b2SMikulas Patocka 	spin_lock(&_minor_lock);
282109ee96b2SMikulas Patocka 	if (test_bit(DMF_FREEING, &md->flags)) {
282209ee96b2SMikulas Patocka 		spin_unlock(&_minor_lock);
282309ee96b2SMikulas Patocka 		return -EBUSY;
282409ee96b2SMikulas Patocka 	}
282509ee96b2SMikulas Patocka 	dm_get(md);
282609ee96b2SMikulas Patocka 	spin_unlock(&_minor_lock);
282709ee96b2SMikulas Patocka 	return 0;
282809ee96b2SMikulas Patocka }
282909ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold);
283009ee96b2SMikulas Patocka 
283172d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
283272d94861SAlasdair G Kergon {
283372d94861SAlasdair G Kergon 	return md->name;
283472d94861SAlasdair G Kergon }
283572d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
283672d94861SAlasdair G Kergon 
28373f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
28381da177e4SLinus Torvalds {
28391134e5aeSMike Anderson 	struct dm_table *map;
284083d5e5b0SMikulas Patocka 	int srcu_idx;
28411da177e4SLinus Torvalds 
28423f77316dSKiyoshi Ueda 	might_sleep();
2843fba9f90eSJeff Mahoney 
284483d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
284563a4f065SMike Snitzer 
284663a4f065SMike Snitzer 	spin_lock(&_minor_lock);
28473f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2848fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2849f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
28503f77316dSKiyoshi Ueda 
285102233342SMike Snitzer 	if (dm_request_based(md) && md->kworker_task)
28522eb6e1e3SKeith Busch 		flush_kthread_worker(&md->kworker);
28532eb6e1e3SKeith Busch 
2854ab7c7bb6SMikulas Patocka 	/*
2855ab7c7bb6SMikulas Patocka 	 * Take suspend_lock so that presuspend and postsuspend methods
2856ab7c7bb6SMikulas Patocka 	 * do not race with internal suspend.
2857ab7c7bb6SMikulas Patocka 	 */
2858ab7c7bb6SMikulas Patocka 	mutex_lock(&md->suspend_lock);
28594f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
28601da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
28611da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
28621da177e4SLinus Torvalds 	}
2863ab7c7bb6SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
28643f77316dSKiyoshi Ueda 
286583d5e5b0SMikulas Patocka 	/* dm_put_live_table must be before msleep, otherwise deadlock is possible */
286683d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
286783d5e5b0SMikulas Patocka 
28683f77316dSKiyoshi Ueda 	/*
28693f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
28703f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
28713f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
28723f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
28733f77316dSKiyoshi Ueda 	 */
28743f77316dSKiyoshi Ueda 	if (wait)
28753f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
28763f77316dSKiyoshi Ueda 			msleep(1);
28773f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
28783f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
28793f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
28803f77316dSKiyoshi Ueda 
2881784aae73SMilan Broz 	dm_sysfs_exit(md);
2882a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
28831da177e4SLinus Torvalds 	free_dev(md);
28841da177e4SLinus Torvalds }
28853f77316dSKiyoshi Ueda 
28863f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
28873f77316dSKiyoshi Ueda {
28883f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
28893f77316dSKiyoshi Ueda }
28903f77316dSKiyoshi Ueda 
28913f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
28923f77316dSKiyoshi Ueda {
28933f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
28943f77316dSKiyoshi Ueda }
28953f77316dSKiyoshi Ueda 
28963f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
28973f77316dSKiyoshi Ueda {
28983f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
28991da177e4SLinus Torvalds }
290079eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
29011da177e4SLinus Torvalds 
2902401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
290346125c1cSMilan Broz {
290446125c1cSMilan Broz 	int r = 0;
2905b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
2906b44ebeb0SMikulas Patocka 
2907b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
290846125c1cSMilan Broz 
290946125c1cSMilan Broz 	while (1) {
2910401600dfSMikulas Patocka 		set_current_state(interruptible);
291146125c1cSMilan Broz 
2912b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
291346125c1cSMilan Broz 			break;
291446125c1cSMilan Broz 
2915401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
2916401600dfSMikulas Patocka 		    signal_pending(current)) {
291746125c1cSMilan Broz 			r = -EINTR;
291846125c1cSMilan Broz 			break;
291946125c1cSMilan Broz 		}
292046125c1cSMilan Broz 
292146125c1cSMilan Broz 		io_schedule();
292246125c1cSMilan Broz 	}
292346125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
292446125c1cSMilan Broz 
2925b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
2926b44ebeb0SMikulas Patocka 
292746125c1cSMilan Broz 	return r;
292846125c1cSMilan Broz }
292946125c1cSMilan Broz 
29301da177e4SLinus Torvalds /*
29311da177e4SLinus Torvalds  * Process the deferred bios
29321da177e4SLinus Torvalds  */
2933ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
29341da177e4SLinus Torvalds {
2935ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2936ef208587SMikulas Patocka 						work);
29376d6f10dfSMilan Broz 	struct bio *c;
293883d5e5b0SMikulas Patocka 	int srcu_idx;
293983d5e5b0SMikulas Patocka 	struct dm_table *map;
29401da177e4SLinus Torvalds 
294183d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
2942ef208587SMikulas Patocka 
29433b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2944022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2945022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2946022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2947022c2611SMikulas Patocka 
29486a8736d1STejun Heo 		if (!c)
2949df12ee99SAlasdair G Kergon 			break;
295073d410c0SMilan Broz 
2951e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2952e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2953af7e466aSMikulas Patocka 		else
295483d5e5b0SMikulas Patocka 			__split_and_process_bio(md, map, c);
2955e6ee8c0bSKiyoshi Ueda 	}
29563b00b203SMikulas Patocka 
295783d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
29581da177e4SLinus Torvalds }
29591da177e4SLinus Torvalds 
29609a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2961304f3f6aSMilan Broz {
29623b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
29634e857c58SPeter Zijlstra 	smp_mb__after_atomic();
296453d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2965304f3f6aSMilan Broz }
2966304f3f6aSMilan Broz 
29671da177e4SLinus Torvalds /*
2968042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
29691da177e4SLinus Torvalds  */
2970042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
29711da177e4SLinus Torvalds {
297287eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2973754c5fc7SMike Snitzer 	struct queue_limits limits;
2974042d2a9bSAlasdair G Kergon 	int r;
29751da177e4SLinus Torvalds 
2976e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
29771da177e4SLinus Torvalds 
29781da177e4SLinus Torvalds 	/* device must be suspended */
29794f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
298093c534aeSAlasdair G Kergon 		goto out;
29811da177e4SLinus Torvalds 
29823ae70656SMike Snitzer 	/*
29833ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
29843ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
29853ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
29863ae70656SMike Snitzer 	 * reappear.
29873ae70656SMike Snitzer 	 */
29883ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
298983d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
29903ae70656SMike Snitzer 		if (live_map)
29913ae70656SMike Snitzer 			limits = md->queue->limits;
299283d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
29933ae70656SMike Snitzer 	}
29943ae70656SMike Snitzer 
299587eb5b21SMike Christie 	if (!live_map) {
2996754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2997042d2a9bSAlasdair G Kergon 		if (r) {
2998042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2999754c5fc7SMike Snitzer 			goto out;
3000042d2a9bSAlasdair G Kergon 		}
300187eb5b21SMike Christie 	}
3002754c5fc7SMike Snitzer 
3003042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
30041da177e4SLinus Torvalds 
300593c534aeSAlasdair G Kergon out:
3006e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
3007042d2a9bSAlasdair G Kergon 	return map;
30081da177e4SLinus Torvalds }
30091da177e4SLinus Torvalds 
30101da177e4SLinus Torvalds /*
30111da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
30121da177e4SLinus Torvalds  * device.
30131da177e4SLinus Torvalds  */
30142ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
30151da177e4SLinus Torvalds {
3016e39e2e95SAlasdair G Kergon 	int r;
30171da177e4SLinus Torvalds 
30181da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
3019dfbe03f6SAlasdair G Kergon 
3020db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
3021dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
3022cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
3023e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
3024e39e2e95SAlasdair G Kergon 		return r;
3025dfbe03f6SAlasdair G Kergon 	}
3026dfbe03f6SAlasdair G Kergon 
3027aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
3028aa8d7c2fSAlasdair G Kergon 
30291da177e4SLinus Torvalds 	return 0;
30301da177e4SLinus Torvalds }
30311da177e4SLinus Torvalds 
30322ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
30331da177e4SLinus Torvalds {
3034aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
3035aa8d7c2fSAlasdair G Kergon 		return;
3036aa8d7c2fSAlasdair G Kergon 
3037db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
30381da177e4SLinus Torvalds 	md->frozen_sb = NULL;
3039aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
30401da177e4SLinus Torvalds }
30411da177e4SLinus Torvalds 
30421da177e4SLinus Torvalds /*
3043ffcc3936SMike Snitzer  * If __dm_suspend returns 0, the device is completely quiescent
3044ffcc3936SMike Snitzer  * now. There is no request-processing activity. All new requests
3045ffcc3936SMike Snitzer  * are being added to md->deferred list.
3046cec47e3dSKiyoshi Ueda  *
3047ffcc3936SMike Snitzer  * Caller must hold md->suspend_lock
3048cec47e3dSKiyoshi Ueda  */
3049ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
3050ffcc3936SMike Snitzer 			unsigned suspend_flags, int interruptible)
30511da177e4SLinus Torvalds {
3052ffcc3936SMike Snitzer 	bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
3053ffcc3936SMike Snitzer 	bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
3054ffcc3936SMike Snitzer 	int r;
3055cf222b37SAlasdair G Kergon 
30562e93ccc1SKiyoshi Ueda 	/*
30572e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
30582e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
30592e93ccc1SKiyoshi Ueda 	 */
30602e93ccc1SKiyoshi Ueda 	if (noflush)
30612e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
30622e93ccc1SKiyoshi Ueda 
3063d67ee213SMike Snitzer 	/*
3064d67ee213SMike Snitzer 	 * This gets reverted if there's an error later and the targets
3065d67ee213SMike Snitzer 	 * provide the .presuspend_undo hook.
3066d67ee213SMike Snitzer 	 */
30671da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
30681da177e4SLinus Torvalds 
30692e93ccc1SKiyoshi Ueda 	/*
30709f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
30719f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
30729f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
30739f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
30742e93ccc1SKiyoshi Ueda 	 */
307532a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
30762ca3310eSAlasdair G Kergon 		r = lock_fs(md);
3077d67ee213SMike Snitzer 		if (r) {
3078d67ee213SMike Snitzer 			dm_table_presuspend_undo_targets(map);
3079ffcc3936SMike Snitzer 			return r;
3080aa8d7c2fSAlasdair G Kergon 		}
3081d67ee213SMike Snitzer 	}
30821da177e4SLinus Torvalds 
30831da177e4SLinus Torvalds 	/*
30843b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
30853b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
30863b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
30873b00b203SMikulas Patocka 	 * dm_wq_work.
30883b00b203SMikulas Patocka 	 *
30893b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
30903b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
30916a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
30926a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
30936a8736d1STejun Heo 	 * flush_workqueue(md->wq).
30941da177e4SLinus Torvalds 	 */
30951eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
309641abc4e1SHannes Reinecke 	if (map)
309783d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
30981da177e4SLinus Torvalds 
3099d0bcb878SKiyoshi Ueda 	/*
310029e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
310129e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
3102d0bcb878SKiyoshi Ueda 	 */
31032eb6e1e3SKeith Busch 	if (dm_request_based(md)) {
31049f518b27SKiyoshi Ueda 		stop_queue(md->queue);
310502233342SMike Snitzer 		if (md->kworker_task)
31062eb6e1e3SKeith Busch 			flush_kthread_worker(&md->kworker);
31072eb6e1e3SKeith Busch 	}
3108cec47e3dSKiyoshi Ueda 
3109d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
3110d0bcb878SKiyoshi Ueda 
31111da177e4SLinus Torvalds 	/*
31123b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
31133b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
31143b00b203SMikulas Patocka 	 * to finish.
31151da177e4SLinus Torvalds 	 */
3116ffcc3936SMike Snitzer 	r = dm_wait_for_completion(md, interruptible);
31171da177e4SLinus Torvalds 
31186d6f10dfSMilan Broz 	if (noflush)
3119022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
312041abc4e1SHannes Reinecke 	if (map)
312183d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
31222e93ccc1SKiyoshi Ueda 
31231da177e4SLinus Torvalds 	/* were we interrupted ? */
312446125c1cSMilan Broz 	if (r < 0) {
31259a1fb464SMikulas Patocka 		dm_queue_flush(md);
312673d410c0SMilan Broz 
3127cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
31289f518b27SKiyoshi Ueda 			start_queue(md->queue);
3129cec47e3dSKiyoshi Ueda 
31302ca3310eSAlasdair G Kergon 		unlock_fs(md);
3131d67ee213SMike Snitzer 		dm_table_presuspend_undo_targets(map);
3132ffcc3936SMike Snitzer 		/* pushback list is already flushed, so skip flush */
3133ffcc3936SMike Snitzer 	}
3134ffcc3936SMike Snitzer 
3135ffcc3936SMike Snitzer 	return r;
31362ca3310eSAlasdair G Kergon }
31372ca3310eSAlasdair G Kergon 
31383b00b203SMikulas Patocka /*
3139ffcc3936SMike Snitzer  * We need to be able to change a mapping table under a mounted
3140ffcc3936SMike Snitzer  * filesystem.  For example we might want to move some data in
3141ffcc3936SMike Snitzer  * the background.  Before the table can be swapped with
3142ffcc3936SMike Snitzer  * dm_bind_table, dm_suspend must be called to flush any in
3143ffcc3936SMike Snitzer  * flight bios and ensure that any further io gets deferred.
31443b00b203SMikulas Patocka  */
3145ffcc3936SMike Snitzer /*
3146ffcc3936SMike Snitzer  * Suspend mechanism in request-based dm.
3147ffcc3936SMike Snitzer  *
3148ffcc3936SMike Snitzer  * 1. Flush all I/Os by lock_fs() if needed.
3149ffcc3936SMike Snitzer  * 2. Stop dispatching any I/O by stopping the request_queue.
3150ffcc3936SMike Snitzer  * 3. Wait for all in-flight I/Os to be completed or requeued.
3151ffcc3936SMike Snitzer  *
3152ffcc3936SMike Snitzer  * To abort suspend, start the request_queue.
3153ffcc3936SMike Snitzer  */
3154ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
3155ffcc3936SMike Snitzer {
3156ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
3157ffcc3936SMike Snitzer 	int r = 0;
3158ffcc3936SMike Snitzer 
3159ffcc3936SMike Snitzer retry:
3160ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3161ffcc3936SMike Snitzer 
3162ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
3163ffcc3936SMike Snitzer 		r = -EINVAL;
3164ffcc3936SMike Snitzer 		goto out_unlock;
3165ffcc3936SMike Snitzer 	}
3166ffcc3936SMike Snitzer 
3167ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
3168ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
3169ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
3170ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3171ffcc3936SMike Snitzer 		if (r)
3172ffcc3936SMike Snitzer 			return r;
3173ffcc3936SMike Snitzer 		goto retry;
3174ffcc3936SMike Snitzer 	}
3175ffcc3936SMike Snitzer 
3176a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
3177ffcc3936SMike Snitzer 
3178ffcc3936SMike Snitzer 	r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE);
3179ffcc3936SMike Snitzer 	if (r)
3180ffcc3936SMike Snitzer 		goto out_unlock;
31813b00b203SMikulas Patocka 
31821da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
31831da177e4SLinus Torvalds 
31844d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
31854d4471cbSKiyoshi Ueda 
3186d287483dSAlasdair G Kergon out_unlock:
3187e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
3188cf222b37SAlasdair G Kergon 	return r;
31891da177e4SLinus Torvalds }
31901da177e4SLinus Torvalds 
3191ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map)
31921da177e4SLinus Torvalds {
3193ffcc3936SMike Snitzer 	if (map) {
3194ffcc3936SMike Snitzer 		int r = dm_table_resume_targets(map);
31958757b776SMilan Broz 		if (r)
3196ffcc3936SMike Snitzer 			return r;
3197ffcc3936SMike Snitzer 	}
31982ca3310eSAlasdair G Kergon 
31999a1fb464SMikulas Patocka 	dm_queue_flush(md);
32002ca3310eSAlasdair G Kergon 
3201cec47e3dSKiyoshi Ueda 	/*
3202cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
3203cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
3204cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
3205cec47e3dSKiyoshi Ueda 	 */
3206cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
3207cec47e3dSKiyoshi Ueda 		start_queue(md->queue);
3208cec47e3dSKiyoshi Ueda 
32092ca3310eSAlasdair G Kergon 	unlock_fs(md);
32102ca3310eSAlasdair G Kergon 
3211ffcc3936SMike Snitzer 	return 0;
3212ffcc3936SMike Snitzer }
3213ffcc3936SMike Snitzer 
3214ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md)
3215ffcc3936SMike Snitzer {
3216ffcc3936SMike Snitzer 	int r = -EINVAL;
3217ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
3218ffcc3936SMike Snitzer 
3219ffcc3936SMike Snitzer retry:
3220ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
3221ffcc3936SMike Snitzer 
3222ffcc3936SMike Snitzer 	if (!dm_suspended_md(md))
3223ffcc3936SMike Snitzer 		goto out;
3224ffcc3936SMike Snitzer 
3225ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
3226ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
3227ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
3228ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
3229ffcc3936SMike Snitzer 		if (r)
3230ffcc3936SMike Snitzer 			return r;
3231ffcc3936SMike Snitzer 		goto retry;
3232ffcc3936SMike Snitzer 	}
3233ffcc3936SMike Snitzer 
3234a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
3235ffcc3936SMike Snitzer 	if (!map || !dm_table_get_size(map))
3236ffcc3936SMike Snitzer 		goto out;
3237ffcc3936SMike Snitzer 
3238ffcc3936SMike Snitzer 	r = __dm_resume(md, map);
3239ffcc3936SMike Snitzer 	if (r)
3240ffcc3936SMike Snitzer 		goto out;
3241ffcc3936SMike Snitzer 
32422ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
32432ca3310eSAlasdair G Kergon 
3244cf222b37SAlasdair G Kergon 	r = 0;
3245cf222b37SAlasdair G Kergon out:
3246e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
32472ca3310eSAlasdair G Kergon 
3248cf222b37SAlasdair G Kergon 	return r;
32491da177e4SLinus Torvalds }
32501da177e4SLinus Torvalds 
3251fd2ed4d2SMikulas Patocka /*
3252fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
3253fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
3254fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
3255fd2ed4d2SMikulas Patocka  */
3256fd2ed4d2SMikulas Patocka 
3257ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
3258ffcc3936SMike Snitzer {
3259ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
3260ffcc3936SMike Snitzer 
326196b26c8cSMikulas Patocka 	if (md->internal_suspend_count++)
3262ffcc3936SMike Snitzer 		return; /* nested internal suspend */
3263ffcc3936SMike Snitzer 
3264ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
3265ffcc3936SMike Snitzer 		set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3266ffcc3936SMike Snitzer 		return; /* nest suspend */
3267ffcc3936SMike Snitzer 	}
3268ffcc3936SMike Snitzer 
3269a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
3270ffcc3936SMike Snitzer 
3271ffcc3936SMike Snitzer 	/*
3272ffcc3936SMike Snitzer 	 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
3273ffcc3936SMike Snitzer 	 * supported.  Properly supporting a TASK_INTERRUPTIBLE internal suspend
3274ffcc3936SMike Snitzer 	 * would require changing .presuspend to return an error -- avoid this
3275ffcc3936SMike Snitzer 	 * until there is a need for more elaborate variants of internal suspend.
3276ffcc3936SMike Snitzer 	 */
3277ffcc3936SMike Snitzer 	(void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE);
3278ffcc3936SMike Snitzer 
3279ffcc3936SMike Snitzer 	set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3280ffcc3936SMike Snitzer 
3281ffcc3936SMike Snitzer 	dm_table_postsuspend_targets(map);
3282ffcc3936SMike Snitzer }
3283ffcc3936SMike Snitzer 
3284ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md)
3285ffcc3936SMike Snitzer {
328696b26c8cSMikulas Patocka 	BUG_ON(!md->internal_suspend_count);
328796b26c8cSMikulas Patocka 
328896b26c8cSMikulas Patocka 	if (--md->internal_suspend_count)
3289ffcc3936SMike Snitzer 		return; /* resume from nested internal suspend */
3290ffcc3936SMike Snitzer 
3291ffcc3936SMike Snitzer 	if (dm_suspended_md(md))
3292ffcc3936SMike Snitzer 		goto done; /* resume from nested suspend */
3293ffcc3936SMike Snitzer 
3294ffcc3936SMike Snitzer 	/*
3295ffcc3936SMike Snitzer 	 * NOTE: existing callers don't need to call dm_table_resume_targets
3296ffcc3936SMike Snitzer 	 * (which may fail -- so best to avoid it for now by passing NULL map)
3297ffcc3936SMike Snitzer 	 */
3298ffcc3936SMike Snitzer 	(void) __dm_resume(md, NULL);
3299ffcc3936SMike Snitzer 
3300ffcc3936SMike Snitzer done:
3301ffcc3936SMike Snitzer 	clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3302ffcc3936SMike Snitzer 	smp_mb__after_atomic();
3303ffcc3936SMike Snitzer 	wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
3304ffcc3936SMike Snitzer }
3305ffcc3936SMike Snitzer 
3306ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md)
3307fd2ed4d2SMikulas Patocka {
3308fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
3309ffcc3936SMike Snitzer 	__dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
3310ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
3311ffcc3936SMike Snitzer }
3312ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
3313ffcc3936SMike Snitzer 
3314ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md)
3315ffcc3936SMike Snitzer {
3316ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
3317ffcc3936SMike Snitzer 	__dm_internal_resume(md);
3318ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
3319ffcc3936SMike Snitzer }
3320ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume);
3321ffcc3936SMike Snitzer 
3322ffcc3936SMike Snitzer /*
3323ffcc3936SMike Snitzer  * Fast variants of internal suspend/resume hold md->suspend_lock,
3324ffcc3936SMike Snitzer  * which prevents interaction with userspace-driven suspend.
3325ffcc3936SMike Snitzer  */
3326ffcc3936SMike Snitzer 
3327ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md)
3328ffcc3936SMike Snitzer {
3329ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
3330ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
3331fd2ed4d2SMikulas Patocka 		return;
3332fd2ed4d2SMikulas Patocka 
3333fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
3334fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
3335fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
3336fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
3337fd2ed4d2SMikulas Patocka }
3338b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
3339fd2ed4d2SMikulas Patocka 
3340ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md)
3341fd2ed4d2SMikulas Patocka {
3342ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
3343fd2ed4d2SMikulas Patocka 		goto done;
3344fd2ed4d2SMikulas Patocka 
3345fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
3346fd2ed4d2SMikulas Patocka 
3347fd2ed4d2SMikulas Patocka done:
3348fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
3349fd2ed4d2SMikulas Patocka }
3350b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
3351fd2ed4d2SMikulas Patocka 
33521da177e4SLinus Torvalds /*-----------------------------------------------------------------
33531da177e4SLinus Torvalds  * Event notification.
33541da177e4SLinus Torvalds  *---------------------------------------------------------------*/
33553abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
335660935eb2SMilan Broz 		       unsigned cookie)
335769267a30SAlasdair G Kergon {
335860935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
335960935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
336060935eb2SMilan Broz 
336160935eb2SMilan Broz 	if (!cookie)
33623abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
336360935eb2SMilan Broz 	else {
336460935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
336560935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
33663abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
33673abf85b5SPeter Rajnoha 					  action, envp);
336860935eb2SMilan Broz 	}
336969267a30SAlasdair G Kergon }
337069267a30SAlasdair G Kergon 
33717a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
33727a8c3d3bSMike Anderson {
33737a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
33747a8c3d3bSMike Anderson }
33757a8c3d3bSMike Anderson 
33761da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
33771da177e4SLinus Torvalds {
33781da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
33791da177e4SLinus Torvalds }
33801da177e4SLinus Torvalds 
33811da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
33821da177e4SLinus Torvalds {
33831da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
33841da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
33851da177e4SLinus Torvalds }
33861da177e4SLinus Torvalds 
33877a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
33887a8c3d3bSMike Anderson {
33897a8c3d3bSMike Anderson 	unsigned long flags;
33907a8c3d3bSMike Anderson 
33917a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
33927a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
33937a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
33947a8c3d3bSMike Anderson }
33957a8c3d3bSMike Anderson 
33961da177e4SLinus Torvalds /*
33971da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
33981da177e4SLinus Torvalds  * count on 'md'.
33991da177e4SLinus Torvalds  */
34001da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
34011da177e4SLinus Torvalds {
34021da177e4SLinus Torvalds 	return md->disk;
34031da177e4SLinus Torvalds }
340465ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk);
34051da177e4SLinus Torvalds 
3406784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
3407784aae73SMilan Broz {
34082995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
3409784aae73SMilan Broz }
3410784aae73SMilan Broz 
3411784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
3412784aae73SMilan Broz {
3413784aae73SMilan Broz 	struct mapped_device *md;
3414784aae73SMilan Broz 
34152995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
3416784aae73SMilan Broz 
34174d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
3418432a212cSMike Anderson 	    dm_deleting_md(md))
34194d89b7b4SMilan Broz 		return NULL;
34204d89b7b4SMilan Broz 
3421784aae73SMilan Broz 	dm_get(md);
3422784aae73SMilan Broz 	return md;
3423784aae73SMilan Broz }
3424784aae73SMilan Broz 
34254f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
34261da177e4SLinus Torvalds {
34271da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
34281da177e4SLinus Torvalds }
34291da177e4SLinus Torvalds 
3430ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md)
3431ffcc3936SMike Snitzer {
3432ffcc3936SMike Snitzer 	return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3433ffcc3936SMike Snitzer }
3434ffcc3936SMike Snitzer 
34352c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
34362c140a24SMikulas Patocka {
34372c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
34382c140a24SMikulas Patocka }
34392c140a24SMikulas Patocka 
344064dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
344164dbce58SKiyoshi Ueda {
3442ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
344364dbce58SKiyoshi Ueda }
344464dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
344564dbce58SKiyoshi Ueda 
34462e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
34472e93ccc1SKiyoshi Ueda {
3448ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
34492e93ccc1SKiyoshi Ueda }
34502e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
34512e93ccc1SKiyoshi Ueda 
345278d8e58aSMike Snitzer struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type,
345378d8e58aSMike Snitzer 					    unsigned integrity, unsigned per_bio_data_size)
3454e6ee8c0bSKiyoshi Ueda {
345578d8e58aSMike Snitzer 	struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
345678d8e58aSMike Snitzer 	struct kmem_cache *cachep = NULL;
345778d8e58aSMike Snitzer 	unsigned int pool_size = 0;
34585f015204SJun'ichi Nomura 	unsigned int front_pad;
3459e6ee8c0bSKiyoshi Ueda 
3460e6ee8c0bSKiyoshi Ueda 	if (!pools)
34614e6e36c3SMike Snitzer 		return NULL;
3462e6ee8c0bSKiyoshi Ueda 
346378d8e58aSMike Snitzer 	type = filter_md_type(type, md);
346417e149b8SMike Snitzer 
346578d8e58aSMike Snitzer 	switch (type) {
346678d8e58aSMike Snitzer 	case DM_TYPE_BIO_BASED:
346778d8e58aSMike Snitzer 		cachep = _io_cache;
346878d8e58aSMike Snitzer 		pool_size = dm_get_reserved_bio_based_ios();
346978d8e58aSMike Snitzer 		front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
347078d8e58aSMike Snitzer 		break;
347178d8e58aSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
347278d8e58aSMike Snitzer 		cachep = _rq_tio_cache;
347378d8e58aSMike Snitzer 		pool_size = dm_get_reserved_rq_based_ios();
347478d8e58aSMike Snitzer 		pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache);
347578d8e58aSMike Snitzer 		if (!pools->rq_pool)
347678d8e58aSMike Snitzer 			goto out;
347778d8e58aSMike Snitzer 		/* fall through to setup remaining rq-based pools */
347878d8e58aSMike Snitzer 	case DM_TYPE_MQ_REQUEST_BASED:
347978d8e58aSMike Snitzer 		if (!pool_size)
348078d8e58aSMike Snitzer 			pool_size = dm_get_reserved_rq_based_ios();
348178d8e58aSMike Snitzer 		front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
348278d8e58aSMike Snitzer 		/* per_bio_data_size is not used. See __bind_mempools(). */
348378d8e58aSMike Snitzer 		WARN_ON(per_bio_data_size != 0);
348478d8e58aSMike Snitzer 		break;
348578d8e58aSMike Snitzer 	default:
348678d8e58aSMike Snitzer 		BUG();
348778d8e58aSMike Snitzer 	}
348878d8e58aSMike Snitzer 
348978d8e58aSMike Snitzer 	if (cachep) {
349078d8e58aSMike Snitzer 		pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
3491e6ee8c0bSKiyoshi Ueda 		if (!pools->io_pool)
34925f015204SJun'ichi Nomura 			goto out;
349378d8e58aSMike Snitzer 	}
3494e6ee8c0bSKiyoshi Ueda 
34953d8aab2dSJunichi Nomura 	pools->bs = bioset_create_nobvec(pool_size, front_pad);
3496e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
34975f015204SJun'ichi Nomura 		goto out;
3498e6ee8c0bSKiyoshi Ueda 
3499a91a2785SMartin K. Petersen 	if (integrity && bioset_integrity_create(pools->bs, pool_size))
35005f015204SJun'ichi Nomura 		goto out;
3501a91a2785SMartin K. Petersen 
3502e6ee8c0bSKiyoshi Ueda 	return pools;
350378d8e58aSMike Snitzer 
35045f015204SJun'ichi Nomura out:
35055f015204SJun'ichi Nomura 	dm_free_md_mempools(pools);
3506e6ee8c0bSKiyoshi Ueda 
35074e6e36c3SMike Snitzer 	return NULL;
3508e6ee8c0bSKiyoshi Ueda }
3509e6ee8c0bSKiyoshi Ueda 
3510e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
3511e6ee8c0bSKiyoshi Ueda {
3512e6ee8c0bSKiyoshi Ueda 	if (!pools)
3513e6ee8c0bSKiyoshi Ueda 		return;
3514e6ee8c0bSKiyoshi Ueda 
3515e6ee8c0bSKiyoshi Ueda 	if (pools->io_pool)
3516e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->io_pool);
3517e6ee8c0bSKiyoshi Ueda 
35181ae49ea2SMike Snitzer 	if (pools->rq_pool)
35191ae49ea2SMike Snitzer 		mempool_destroy(pools->rq_pool);
35201ae49ea2SMike Snitzer 
3521e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
3522e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
3523e6ee8c0bSKiyoshi Ueda 
3524e6ee8c0bSKiyoshi Ueda 	kfree(pools);
3525e6ee8c0bSKiyoshi Ueda }
3526e6ee8c0bSKiyoshi Ueda 
352783d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
35281da177e4SLinus Torvalds 	.open = dm_blk_open,
35291da177e4SLinus Torvalds 	.release = dm_blk_close,
3530aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
35313ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
35321da177e4SLinus Torvalds 	.owner = THIS_MODULE
35331da177e4SLinus Torvalds };
35341da177e4SLinus Torvalds 
35351da177e4SLinus Torvalds /*
35361da177e4SLinus Torvalds  * module hooks
35371da177e4SLinus Torvalds  */
35381da177e4SLinus Torvalds module_init(dm_init);
35391da177e4SLinus Torvalds module_exit(dm_exit);
35401da177e4SLinus Torvalds 
35411da177e4SLinus Torvalds module_param(major, uint, 0);
35421da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
3543f4790826SMike Snitzer 
3544e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3545e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3546e8603136SMike Snitzer 
3547f4790826SMike Snitzer module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
3548f4790826SMike Snitzer MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
3549f4790826SMike Snitzer 
355017e149b8SMike Snitzer module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
355117e149b8SMike Snitzer MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
355217e149b8SMike Snitzer 
35531da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
35541da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
35551da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3556