xref: /openbmc/linux/drivers/md/dm.c (revision 74d46992e0d9dee7f1f376de0d56d31614c8a17a)
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 
84cc96131SMike Snitzer #include "dm-core.h"
94cc96131SMike Snitzer #include "dm-rq.h"
1051e5b2bdSMike Anderson #include "dm-uevent.h"
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/init.h>
131da177e4SLinus Torvalds #include <linux/module.h>
1448c9c27bSArjan van de Ven #include <linux/mutex.h>
15174cd4b1SIngo Molnar #include <linux/sched/signal.h>
161da177e4SLinus Torvalds #include <linux/blkpg.h>
171da177e4SLinus Torvalds #include <linux/bio.h>
181da177e4SLinus Torvalds #include <linux/mempool.h>
19f26c5719SDan Williams #include <linux/dax.h>
201da177e4SLinus Torvalds #include <linux/slab.h>
211da177e4SLinus Torvalds #include <linux/idr.h>
227e026c8cSDan Williams #include <linux/uio.h>
233ac51e74SDarrick J. Wong #include <linux/hdreg.h>
243f77316dSKiyoshi Ueda #include <linux/delay.h>
25ffcc3936SMike Snitzer #include <linux/wait.h>
2671cdb697SChristoph Hellwig #include <linux/pr.h>
2755782138SLi Zefan 
2872d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
2972d94861SAlasdair G Kergon 
3071a16736SNamhyung Kim #ifdef CONFIG_PRINTK
3171a16736SNamhyung Kim /*
3271a16736SNamhyung Kim  * ratelimit state to be used in DMXXX_LIMIT().
3371a16736SNamhyung Kim  */
3471a16736SNamhyung Kim DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
3571a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_INTERVAL,
3671a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_BURST);
3771a16736SNamhyung Kim EXPORT_SYMBOL(dm_ratelimit_state);
3871a16736SNamhyung Kim #endif
3971a16736SNamhyung Kim 
4060935eb2SMilan Broz /*
4160935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
4260935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
4360935eb2SMilan Broz  */
4460935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
4560935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
4660935eb2SMilan Broz 
471da177e4SLinus Torvalds static const char *_name = DM_NAME;
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds static unsigned int major = 0;
501da177e4SLinus Torvalds static unsigned int _major = 0;
511da177e4SLinus Torvalds 
52d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr);
53d15b774cSAlasdair G Kergon 
54f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
552c140a24SMikulas Patocka 
562c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w);
572c140a24SMikulas Patocka 
582c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
592c140a24SMikulas Patocka 
60acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue;
61acfe0ad7SMikulas Patocka 
6293e6442cSMikulas Patocka atomic_t dm_global_event_nr = ATOMIC_INIT(0);
6393e6442cSMikulas Patocka DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq);
6493e6442cSMikulas Patocka 
651da177e4SLinus Torvalds /*
661da177e4SLinus Torvalds  * One of these is allocated per bio.
671da177e4SLinus Torvalds  */
681da177e4SLinus Torvalds struct dm_io {
691da177e4SLinus Torvalds 	struct mapped_device *md;
704e4cbee9SChristoph Hellwig 	blk_status_t status;
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 
78ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
79ba61fdd1SJeff Mahoney 
801da177e4SLinus Torvalds /*
811da177e4SLinus Torvalds  * Bits for the md->flags field.
821da177e4SLinus Torvalds  */
831eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
841da177e4SLinus Torvalds #define DMF_SUSPENDED 1
85aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
86fba9f90eSJeff Mahoney #define DMF_FREEING 3
875c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
882e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
898ae12666SKent Overstreet #define DMF_DEFERRED_REMOVE 6
908ae12666SKent Overstreet #define DMF_SUSPENDED_INTERNALLY 7
911da177e4SLinus Torvalds 
92115485e8SMike Snitzer #define DM_NUMA_NODE NUMA_NO_NODE
93115485e8SMike Snitzer static int dm_numa_node = DM_NUMA_NODE;
94faad87dfSMike Snitzer 
95e6ee8c0bSKiyoshi Ueda /*
96e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
97e6ee8c0bSKiyoshi Ueda  */
98e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
99e6ee8c0bSKiyoshi Ueda 	mempool_t *io_pool;
100e6ee8c0bSKiyoshi Ueda 	struct bio_set *bs;
101e6ee8c0bSKiyoshi Ueda };
102e6ee8c0bSKiyoshi Ueda 
10386f1152bSBenjamin Marzinski struct table_device {
10486f1152bSBenjamin Marzinski 	struct list_head list;
10586f1152bSBenjamin Marzinski 	atomic_t count;
10686f1152bSBenjamin Marzinski 	struct dm_dev dm_dev;
10786f1152bSBenjamin Marzinski };
10886f1152bSBenjamin Marzinski 
109e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
1108fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
1111ae49ea2SMike Snitzer static struct kmem_cache *_rq_cache;
11294818742SKent Overstreet 
113f4790826SMike Snitzer /*
114e8603136SMike Snitzer  * Bio-based DM's mempools' reserved IOs set by the user.
115e8603136SMike Snitzer  */
1164cc96131SMike Snitzer #define RESERVED_BIO_BASED_IOS		16
117e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
118e8603136SMike Snitzer 
119115485e8SMike Snitzer static int __dm_get_module_param_int(int *module_param, int min, int max)
120115485e8SMike Snitzer {
121115485e8SMike Snitzer 	int param = ACCESS_ONCE(*module_param);
122115485e8SMike Snitzer 	int modified_param = 0;
123115485e8SMike Snitzer 	bool modified = true;
124115485e8SMike Snitzer 
125115485e8SMike Snitzer 	if (param < min)
126115485e8SMike Snitzer 		modified_param = min;
127115485e8SMike Snitzer 	else if (param > max)
128115485e8SMike Snitzer 		modified_param = max;
129115485e8SMike Snitzer 	else
130115485e8SMike Snitzer 		modified = false;
131115485e8SMike Snitzer 
132115485e8SMike Snitzer 	if (modified) {
133115485e8SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
134115485e8SMike Snitzer 		param = modified_param;
135115485e8SMike Snitzer 	}
136115485e8SMike Snitzer 
137115485e8SMike Snitzer 	return param;
138115485e8SMike Snitzer }
139115485e8SMike Snitzer 
1404cc96131SMike Snitzer unsigned __dm_get_module_param(unsigned *module_param,
141f4790826SMike Snitzer 			       unsigned def, unsigned max)
142f4790826SMike Snitzer {
14309c2d531SMike Snitzer 	unsigned param = ACCESS_ONCE(*module_param);
14409c2d531SMike Snitzer 	unsigned modified_param = 0;
145f4790826SMike Snitzer 
14609c2d531SMike Snitzer 	if (!param)
14709c2d531SMike Snitzer 		modified_param = def;
14809c2d531SMike Snitzer 	else if (param > max)
14909c2d531SMike Snitzer 		modified_param = max;
150f4790826SMike Snitzer 
15109c2d531SMike Snitzer 	if (modified_param) {
15209c2d531SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
15309c2d531SMike Snitzer 		param = modified_param;
154f4790826SMike Snitzer 	}
155f4790826SMike Snitzer 
15609c2d531SMike Snitzer 	return param;
157f4790826SMike Snitzer }
158f4790826SMike Snitzer 
159e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void)
160e8603136SMike Snitzer {
16109c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_bio_based_ios,
1624cc96131SMike Snitzer 				     RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS);
163e8603136SMike Snitzer }
164e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
165e8603136SMike Snitzer 
166115485e8SMike Snitzer static unsigned dm_get_numa_node(void)
167115485e8SMike Snitzer {
168115485e8SMike Snitzer 	return __dm_get_module_param_int(&dm_numa_node,
169115485e8SMike Snitzer 					 DM_NUMA_NODE, num_online_nodes() - 1);
170115485e8SMike Snitzer }
171115485e8SMike Snitzer 
1721da177e4SLinus Torvalds static int __init local_init(void)
1731da177e4SLinus Torvalds {
17451157b4aSKiyoshi Ueda 	int r = -ENOMEM;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
177028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
1781da177e4SLinus Torvalds 	if (!_io_cache)
17951157b4aSKiyoshi Ueda 		return r;
1801da177e4SLinus Torvalds 
1818fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
1828fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
183dba14160SMikulas Patocka 		goto out_free_io_cache;
1848fbf26adSKiyoshi Ueda 
185eca7ee6dSMike Snitzer 	_rq_cache = kmem_cache_create("dm_old_clone_request", sizeof(struct request),
1861ae49ea2SMike Snitzer 				      __alignof__(struct request), 0, NULL);
1871ae49ea2SMike Snitzer 	if (!_rq_cache)
1881ae49ea2SMike Snitzer 		goto out_free_rq_tio_cache;
1891ae49ea2SMike Snitzer 
19051e5b2bdSMike Anderson 	r = dm_uevent_init();
19151157b4aSKiyoshi Ueda 	if (r)
1921ae49ea2SMike Snitzer 		goto out_free_rq_cache;
19351e5b2bdSMike Anderson 
194acfe0ad7SMikulas Patocka 	deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
195acfe0ad7SMikulas Patocka 	if (!deferred_remove_workqueue) {
196acfe0ad7SMikulas Patocka 		r = -ENOMEM;
197acfe0ad7SMikulas Patocka 		goto out_uevent_exit;
198acfe0ad7SMikulas Patocka 	}
199acfe0ad7SMikulas Patocka 
2001da177e4SLinus Torvalds 	_major = major;
2011da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
20251157b4aSKiyoshi Ueda 	if (r < 0)
203acfe0ad7SMikulas Patocka 		goto out_free_workqueue;
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	if (!_major)
2061da177e4SLinus Torvalds 		_major = r;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	return 0;
20951157b4aSKiyoshi Ueda 
210acfe0ad7SMikulas Patocka out_free_workqueue:
211acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
21251157b4aSKiyoshi Ueda out_uevent_exit:
21351157b4aSKiyoshi Ueda 	dm_uevent_exit();
2141ae49ea2SMike Snitzer out_free_rq_cache:
2151ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
2168fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
2178fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
21851157b4aSKiyoshi Ueda out_free_io_cache:
21951157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
22051157b4aSKiyoshi Ueda 
22151157b4aSKiyoshi Ueda 	return r;
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds static void local_exit(void)
2251da177e4SLinus Torvalds {
2262c140a24SMikulas Patocka 	flush_scheduled_work();
227acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
2282c140a24SMikulas Patocka 
2291ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
2308fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
2311da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
23200d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
23351e5b2bdSMike Anderson 	dm_uevent_exit();
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	_major = 0;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	DMINFO("cleaned up");
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds 
240b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
2411da177e4SLinus Torvalds 	local_init,
2421da177e4SLinus Torvalds 	dm_target_init,
2431da177e4SLinus Torvalds 	dm_linear_init,
2441da177e4SLinus Torvalds 	dm_stripe_init,
245952b3557SMikulas Patocka 	dm_io_init,
246945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2471da177e4SLinus Torvalds 	dm_interface_init,
248fd2ed4d2SMikulas Patocka 	dm_statistics_init,
2491da177e4SLinus Torvalds };
2501da177e4SLinus Torvalds 
251b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2521da177e4SLinus Torvalds 	local_exit,
2531da177e4SLinus Torvalds 	dm_target_exit,
2541da177e4SLinus Torvalds 	dm_linear_exit,
2551da177e4SLinus Torvalds 	dm_stripe_exit,
256952b3557SMikulas Patocka 	dm_io_exit,
257945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
2581da177e4SLinus Torvalds 	dm_interface_exit,
259fd2ed4d2SMikulas Patocka 	dm_statistics_exit,
2601da177e4SLinus Torvalds };
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds static int __init dm_init(void)
2631da177e4SLinus Torvalds {
2641da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	int r, i;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
2691da177e4SLinus Torvalds 		r = _inits[i]();
2701da177e4SLinus Torvalds 		if (r)
2711da177e4SLinus Torvalds 			goto bad;
2721da177e4SLinus Torvalds 	}
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	return 0;
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds       bad:
2771da177e4SLinus Torvalds 	while (i--)
2781da177e4SLinus Torvalds 		_exits[i]();
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	return r;
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds static void __exit dm_exit(void)
2841da177e4SLinus Torvalds {
2851da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	while (i--)
2881da177e4SLinus Torvalds 		_exits[i]();
289d15b774cSAlasdair G Kergon 
290d15b774cSAlasdair G Kergon 	/*
291d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
292d15b774cSAlasdair G Kergon 	 */
293d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds /*
2971da177e4SLinus Torvalds  * Block device functions
2981da177e4SLinus Torvalds  */
299432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
300432a212cSMike Anderson {
301432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
302432a212cSMike Anderson }
303432a212cSMike Anderson 
304fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3051da177e4SLinus Torvalds {
3061da177e4SLinus Torvalds 	struct mapped_device *md;
3071da177e4SLinus Torvalds 
308fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
309fba9f90eSJeff Mahoney 
310fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
311fba9f90eSJeff Mahoney 	if (!md)
312fba9f90eSJeff Mahoney 		goto out;
313fba9f90eSJeff Mahoney 
3145c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
315432a212cSMike Anderson 	    dm_deleting_md(md)) {
316fba9f90eSJeff Mahoney 		md = NULL;
317fba9f90eSJeff Mahoney 		goto out;
318fba9f90eSJeff Mahoney 	}
319fba9f90eSJeff Mahoney 
3201da177e4SLinus Torvalds 	dm_get(md);
3215c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
322fba9f90eSJeff Mahoney out:
323fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
324fba9f90eSJeff Mahoney 
325fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3261da177e4SLinus Torvalds }
3271da177e4SLinus Torvalds 
328db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode)
3291da177e4SLinus Torvalds {
33063a4f065SMike Snitzer 	struct mapped_device *md;
3316e9624b8SArnd Bergmann 
3324a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
3334a1aeb98SMilan Broz 
33463a4f065SMike Snitzer 	md = disk->private_data;
33563a4f065SMike Snitzer 	if (WARN_ON(!md))
33663a4f065SMike Snitzer 		goto out;
33763a4f065SMike Snitzer 
3382c140a24SMikulas Patocka 	if (atomic_dec_and_test(&md->open_count) &&
3392c140a24SMikulas Patocka 	    (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
340acfe0ad7SMikulas Patocka 		queue_work(deferred_remove_workqueue, &deferred_remove_work);
3412c140a24SMikulas Patocka 
3421da177e4SLinus Torvalds 	dm_put(md);
34363a4f065SMike Snitzer out:
3444a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
3475c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3485c6bd75dSAlasdair G Kergon {
3495c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3505c6bd75dSAlasdair G Kergon }
3515c6bd75dSAlasdair G Kergon 
3525c6bd75dSAlasdair G Kergon /*
3535c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3545c6bd75dSAlasdair G Kergon  */
3552c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
3565c6bd75dSAlasdair G Kergon {
3575c6bd75dSAlasdair G Kergon 	int r = 0;
3585c6bd75dSAlasdair G Kergon 
3595c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
3605c6bd75dSAlasdair G Kergon 
3612c140a24SMikulas Patocka 	if (dm_open_count(md)) {
3625c6bd75dSAlasdair G Kergon 		r = -EBUSY;
3632c140a24SMikulas Patocka 		if (mark_deferred)
3642c140a24SMikulas Patocka 			set_bit(DMF_DEFERRED_REMOVE, &md->flags);
3652c140a24SMikulas Patocka 	} else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
3662c140a24SMikulas Patocka 		r = -EEXIST;
3675c6bd75dSAlasdair G Kergon 	else
3685c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
3695c6bd75dSAlasdair G Kergon 
3705c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
3715c6bd75dSAlasdair G Kergon 
3725c6bd75dSAlasdair G Kergon 	return r;
3735c6bd75dSAlasdair G Kergon }
3745c6bd75dSAlasdair G Kergon 
3752c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md)
3762c140a24SMikulas Patocka {
3772c140a24SMikulas Patocka 	int r = 0;
3782c140a24SMikulas Patocka 
3792c140a24SMikulas Patocka 	spin_lock(&_minor_lock);
3802c140a24SMikulas Patocka 
3812c140a24SMikulas Patocka 	if (test_bit(DMF_DELETING, &md->flags))
3822c140a24SMikulas Patocka 		r = -EBUSY;
3832c140a24SMikulas Patocka 	else
3842c140a24SMikulas Patocka 		clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
3852c140a24SMikulas Patocka 
3862c140a24SMikulas Patocka 	spin_unlock(&_minor_lock);
3872c140a24SMikulas Patocka 
3882c140a24SMikulas Patocka 	return r;
3892c140a24SMikulas Patocka }
3902c140a24SMikulas Patocka 
3912c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w)
3922c140a24SMikulas Patocka {
3932c140a24SMikulas Patocka 	dm_deferred_remove();
3942c140a24SMikulas Patocka }
3952c140a24SMikulas Patocka 
396fd2ed4d2SMikulas Patocka sector_t dm_get_size(struct mapped_device *md)
397fd2ed4d2SMikulas Patocka {
398fd2ed4d2SMikulas Patocka 	return get_capacity(md->disk);
399fd2ed4d2SMikulas Patocka }
400fd2ed4d2SMikulas Patocka 
4019974fa2cSMike Snitzer struct request_queue *dm_get_md_queue(struct mapped_device *md)
4029974fa2cSMike Snitzer {
4039974fa2cSMike Snitzer 	return md->queue;
4049974fa2cSMike Snitzer }
4059974fa2cSMike Snitzer 
406fd2ed4d2SMikulas Patocka struct dm_stats *dm_get_stats(struct mapped_device *md)
407fd2ed4d2SMikulas Patocka {
408fd2ed4d2SMikulas Patocka 	return &md->stats;
409fd2ed4d2SMikulas Patocka }
410fd2ed4d2SMikulas Patocka 
4113ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4123ac51e74SDarrick J. Wong {
4133ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
4143ac51e74SDarrick J. Wong 
4153ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
4163ac51e74SDarrick J. Wong }
4173ac51e74SDarrick J. Wong 
418956a4025SMike Snitzer static int dm_grab_bdev_for_ioctl(struct mapped_device *md,
41966482026SMike Snitzer 				  struct block_device **bdev,
420956a4025SMike Snitzer 				  fmode_t *mode)
421aa129a22SMilan Broz {
42266482026SMike Snitzer 	struct dm_target *tgt;
4236c182cd8SHannes Reinecke 	struct dm_table *map;
424956a4025SMike Snitzer 	int srcu_idx, r;
425aa129a22SMilan Broz 
4266c182cd8SHannes Reinecke retry:
427e56f81e0SChristoph Hellwig 	r = -ENOTTY;
428956a4025SMike Snitzer 	map = dm_get_live_table(md, &srcu_idx);
429aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
430aa129a22SMilan Broz 		goto out;
431aa129a22SMilan Broz 
432aa129a22SMilan Broz 	/* We only support devices that have a single target */
433aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
434aa129a22SMilan Broz 		goto out;
435aa129a22SMilan Broz 
43666482026SMike Snitzer 	tgt = dm_table_get_target(map, 0);
43766482026SMike Snitzer 	if (!tgt->type->prepare_ioctl)
4384d341d82SMike Snitzer 		goto out;
439aa129a22SMilan Broz 
4404f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
441aa129a22SMilan Broz 		r = -EAGAIN;
442aa129a22SMilan Broz 		goto out;
443aa129a22SMilan Broz 	}
444aa129a22SMilan Broz 
44566482026SMike Snitzer 	r = tgt->type->prepare_ioctl(tgt, bdev, mode);
446e56f81e0SChristoph Hellwig 	if (r < 0)
447e56f81e0SChristoph Hellwig 		goto out;
448e56f81e0SChristoph Hellwig 
449956a4025SMike Snitzer 	bdgrab(*bdev);
450956a4025SMike Snitzer 	dm_put_live_table(md, srcu_idx);
451e56f81e0SChristoph Hellwig 	return r;
452aa129a22SMilan Broz 
453aa129a22SMilan Broz out:
454956a4025SMike Snitzer 	dm_put_live_table(md, srcu_idx);
4555bbbfdf6SJunichi Nomura 	if (r == -ENOTCONN && !fatal_signal_pending(current)) {
4566c182cd8SHannes Reinecke 		msleep(10);
4576c182cd8SHannes Reinecke 		goto retry;
4586c182cd8SHannes Reinecke 	}
459e56f81e0SChristoph Hellwig 	return r;
460e56f81e0SChristoph Hellwig }
4616c182cd8SHannes Reinecke 
462e56f81e0SChristoph Hellwig static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
463e56f81e0SChristoph Hellwig 			unsigned int cmd, unsigned long arg)
464e56f81e0SChristoph Hellwig {
465e56f81e0SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
466956a4025SMike Snitzer 	int r;
467e56f81e0SChristoph Hellwig 
468956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
469e56f81e0SChristoph Hellwig 	if (r < 0)
470e56f81e0SChristoph Hellwig 		return r;
471e56f81e0SChristoph Hellwig 
472e56f81e0SChristoph Hellwig 	if (r > 0) {
473e56f81e0SChristoph Hellwig 		/*
474e980f623SChristoph Hellwig 		 * Target determined this ioctl is being issued against a
475e980f623SChristoph Hellwig 		 * subset of the parent bdev; require extra privileges.
476e56f81e0SChristoph Hellwig 		 */
477e980f623SChristoph Hellwig 		if (!capable(CAP_SYS_RAWIO)) {
478e980f623SChristoph Hellwig 			DMWARN_LIMIT(
479e980f623SChristoph Hellwig 	"%s: sending ioctl %x to DM device without required privilege.",
480e980f623SChristoph Hellwig 				current->comm, cmd);
481e980f623SChristoph Hellwig 			r = -ENOIOCTLCMD;
482e56f81e0SChristoph Hellwig 			goto out;
483e56f81e0SChristoph Hellwig 		}
484e980f623SChristoph Hellwig 	}
485e56f81e0SChristoph Hellwig 
48666482026SMike Snitzer 	r =  __blkdev_driver_ioctl(bdev, mode, cmd, arg);
487e56f81e0SChristoph Hellwig out:
488956a4025SMike Snitzer 	bdput(bdev);
489aa129a22SMilan Broz 	return r;
490aa129a22SMilan Broz }
491aa129a22SMilan Broz 
492028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
4931da177e4SLinus Torvalds {
4941da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
4951da177e4SLinus Torvalds }
4961da177e4SLinus Torvalds 
497028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
4981da177e4SLinus Torvalds {
4991da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds 
502cfae7529SMike Snitzer static void free_tio(struct dm_target_io *tio)
5031da177e4SLinus Torvalds {
504dba14160SMikulas Patocka 	bio_put(&tio->clone);
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds 
5074cc96131SMike Snitzer int md_in_flight(struct mapped_device *md)
50890abb8c4SKiyoshi Ueda {
50990abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
51090abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
51190abb8c4SKiyoshi Ueda }
51290abb8c4SKiyoshi Ueda 
5133eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
5143eaf840eSJun'ichi "Nick" Nomura {
5153eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
516fd2ed4d2SMikulas Patocka 	struct bio *bio = io->bio;
517c9959059STejun Heo 	int cpu;
518fd2ed4d2SMikulas Patocka 	int rw = bio_data_dir(bio);
5193eaf840eSJun'ichi "Nick" Nomura 
5203eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
5213eaf840eSJun'ichi "Nick" Nomura 
522074a7acaSTejun Heo 	cpu = part_stat_lock();
523d62e26b3SJens Axboe 	part_round_stats(md->queue, cpu, &dm_disk(md)->part0);
524074a7acaSTejun Heo 	part_stat_unlock();
5251e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw],
5261e9bb880SShaohua Li 		atomic_inc_return(&md->pending[rw]));
527fd2ed4d2SMikulas Patocka 
528fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
529528ec5abSMike Christie 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
530528ec5abSMike Christie 				    bio->bi_iter.bi_sector, bio_sectors(bio),
531528ec5abSMike Christie 				    false, 0, &io->stats_aux);
5323eaf840eSJun'ichi "Nick" Nomura }
5333eaf840eSJun'ichi "Nick" Nomura 
534d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
5353eaf840eSJun'ichi "Nick" Nomura {
5363eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
5373eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
5383eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
53918c0b223SGu Zheng 	int pending;
5403eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
5413eaf840eSJun'ichi "Nick" Nomura 
542d62e26b3SJens Axboe 	generic_end_io_acct(md->queue, rw, &dm_disk(md)->part0, io->start_time);
5433eaf840eSJun'ichi "Nick" Nomura 
544fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
545528ec5abSMike Christie 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
546528ec5abSMike Christie 				    bio->bi_iter.bi_sector, bio_sectors(bio),
547528ec5abSMike Christie 				    true, duration, &io->stats_aux);
548fd2ed4d2SMikulas Patocka 
549af7e466aSMikulas Patocka 	/*
550af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
551d87f4c14STejun Heo 	 * a flush.
552af7e466aSMikulas Patocka 	 */
5531e9bb880SShaohua Li 	pending = atomic_dec_return(&md->pending[rw]);
5541e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
555316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
5563eaf840eSJun'ichi "Nick" Nomura 
557d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
558d221d2e7SMikulas Patocka 	if (!pending)
559d221d2e7SMikulas Patocka 		wake_up(&md->wait);
5603eaf840eSJun'ichi "Nick" Nomura }
5613eaf840eSJun'ichi "Nick" Nomura 
5621da177e4SLinus Torvalds /*
5631da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
5641da177e4SLinus Torvalds  */
56592c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
5661da177e4SLinus Torvalds {
56705447420SKiyoshi Ueda 	unsigned long flags;
5681da177e4SLinus Torvalds 
56905447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
5701da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
57105447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
57292c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
5731da177e4SLinus Torvalds }
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds /*
5761da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
5771da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
57883d5e5b0SMikulas Patocka  * dm_put_live_table() when finished.
5791da177e4SLinus Torvalds  */
58083d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
5811da177e4SLinus Torvalds {
58283d5e5b0SMikulas Patocka 	*srcu_idx = srcu_read_lock(&md->io_barrier);
5831da177e4SLinus Torvalds 
58483d5e5b0SMikulas Patocka 	return srcu_dereference(md->map, &md->io_barrier);
58583d5e5b0SMikulas Patocka }
5861da177e4SLinus Torvalds 
58783d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
58883d5e5b0SMikulas Patocka {
58983d5e5b0SMikulas Patocka 	srcu_read_unlock(&md->io_barrier, srcu_idx);
59083d5e5b0SMikulas Patocka }
59183d5e5b0SMikulas Patocka 
59283d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md)
59383d5e5b0SMikulas Patocka {
59483d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
59583d5e5b0SMikulas Patocka 	synchronize_rcu_expedited();
59683d5e5b0SMikulas Patocka }
59783d5e5b0SMikulas Patocka 
59883d5e5b0SMikulas Patocka /*
59983d5e5b0SMikulas Patocka  * A fast alternative to dm_get_live_table/dm_put_live_table.
60083d5e5b0SMikulas Patocka  * The caller must not block between these two functions.
60183d5e5b0SMikulas Patocka  */
60283d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
60383d5e5b0SMikulas Patocka {
60483d5e5b0SMikulas Patocka 	rcu_read_lock();
60583d5e5b0SMikulas Patocka 	return rcu_dereference(md->map);
60683d5e5b0SMikulas Patocka }
60783d5e5b0SMikulas Patocka 
60883d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
60983d5e5b0SMikulas Patocka {
61083d5e5b0SMikulas Patocka 	rcu_read_unlock();
6111da177e4SLinus Torvalds }
6121da177e4SLinus Torvalds 
6133ac51e74SDarrick J. Wong /*
61486f1152bSBenjamin Marzinski  * Open a table device so we can use it as a map destination.
61586f1152bSBenjamin Marzinski  */
61686f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev,
61786f1152bSBenjamin Marzinski 			     struct mapped_device *md)
61886f1152bSBenjamin Marzinski {
61986f1152bSBenjamin Marzinski 	static char *_claim_ptr = "I belong to device-mapper";
62086f1152bSBenjamin Marzinski 	struct block_device *bdev;
62186f1152bSBenjamin Marzinski 
62286f1152bSBenjamin Marzinski 	int r;
62386f1152bSBenjamin Marzinski 
62486f1152bSBenjamin Marzinski 	BUG_ON(td->dm_dev.bdev);
62586f1152bSBenjamin Marzinski 
62686f1152bSBenjamin Marzinski 	bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
62786f1152bSBenjamin Marzinski 	if (IS_ERR(bdev))
62886f1152bSBenjamin Marzinski 		return PTR_ERR(bdev);
62986f1152bSBenjamin Marzinski 
63086f1152bSBenjamin Marzinski 	r = bd_link_disk_holder(bdev, dm_disk(md));
63186f1152bSBenjamin Marzinski 	if (r) {
63286f1152bSBenjamin Marzinski 		blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
63386f1152bSBenjamin Marzinski 		return r;
63486f1152bSBenjamin Marzinski 	}
63586f1152bSBenjamin Marzinski 
63686f1152bSBenjamin Marzinski 	td->dm_dev.bdev = bdev;
637817bf402SDan Williams 	td->dm_dev.dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
63886f1152bSBenjamin Marzinski 	return 0;
63986f1152bSBenjamin Marzinski }
64086f1152bSBenjamin Marzinski 
64186f1152bSBenjamin Marzinski /*
64286f1152bSBenjamin Marzinski  * Close a table device that we've been using.
64386f1152bSBenjamin Marzinski  */
64486f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md)
64586f1152bSBenjamin Marzinski {
64686f1152bSBenjamin Marzinski 	if (!td->dm_dev.bdev)
64786f1152bSBenjamin Marzinski 		return;
64886f1152bSBenjamin Marzinski 
64986f1152bSBenjamin Marzinski 	bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
65086f1152bSBenjamin Marzinski 	blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
651817bf402SDan Williams 	put_dax(td->dm_dev.dax_dev);
65286f1152bSBenjamin Marzinski 	td->dm_dev.bdev = NULL;
653817bf402SDan Williams 	td->dm_dev.dax_dev = NULL;
65486f1152bSBenjamin Marzinski }
65586f1152bSBenjamin Marzinski 
65686f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev,
65786f1152bSBenjamin Marzinski 					      fmode_t mode) {
65886f1152bSBenjamin Marzinski 	struct table_device *td;
65986f1152bSBenjamin Marzinski 
66086f1152bSBenjamin Marzinski 	list_for_each_entry(td, l, list)
66186f1152bSBenjamin Marzinski 		if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
66286f1152bSBenjamin Marzinski 			return td;
66386f1152bSBenjamin Marzinski 
66486f1152bSBenjamin Marzinski 	return NULL;
66586f1152bSBenjamin Marzinski }
66686f1152bSBenjamin Marzinski 
66786f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
66886f1152bSBenjamin Marzinski 			struct dm_dev **result) {
66986f1152bSBenjamin Marzinski 	int r;
67086f1152bSBenjamin Marzinski 	struct table_device *td;
67186f1152bSBenjamin Marzinski 
67286f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
67386f1152bSBenjamin Marzinski 	td = find_table_device(&md->table_devices, dev, mode);
67486f1152bSBenjamin Marzinski 	if (!td) {
675115485e8SMike Snitzer 		td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
67686f1152bSBenjamin Marzinski 		if (!td) {
67786f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
67886f1152bSBenjamin Marzinski 			return -ENOMEM;
67986f1152bSBenjamin Marzinski 		}
68086f1152bSBenjamin Marzinski 
68186f1152bSBenjamin Marzinski 		td->dm_dev.mode = mode;
68286f1152bSBenjamin Marzinski 		td->dm_dev.bdev = NULL;
68386f1152bSBenjamin Marzinski 
68486f1152bSBenjamin Marzinski 		if ((r = open_table_device(td, dev, md))) {
68586f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
68686f1152bSBenjamin Marzinski 			kfree(td);
68786f1152bSBenjamin Marzinski 			return r;
68886f1152bSBenjamin Marzinski 		}
68986f1152bSBenjamin Marzinski 
69086f1152bSBenjamin Marzinski 		format_dev_t(td->dm_dev.name, dev);
69186f1152bSBenjamin Marzinski 
69286f1152bSBenjamin Marzinski 		atomic_set(&td->count, 0);
69386f1152bSBenjamin Marzinski 		list_add(&td->list, &md->table_devices);
69486f1152bSBenjamin Marzinski 	}
69586f1152bSBenjamin Marzinski 	atomic_inc(&td->count);
69686f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
69786f1152bSBenjamin Marzinski 
69886f1152bSBenjamin Marzinski 	*result = &td->dm_dev;
69986f1152bSBenjamin Marzinski 	return 0;
70086f1152bSBenjamin Marzinski }
70186f1152bSBenjamin Marzinski EXPORT_SYMBOL_GPL(dm_get_table_device);
70286f1152bSBenjamin Marzinski 
70386f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
70486f1152bSBenjamin Marzinski {
70586f1152bSBenjamin Marzinski 	struct table_device *td = container_of(d, struct table_device, dm_dev);
70686f1152bSBenjamin Marzinski 
70786f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
70886f1152bSBenjamin Marzinski 	if (atomic_dec_and_test(&td->count)) {
70986f1152bSBenjamin Marzinski 		close_table_device(td, md);
71086f1152bSBenjamin Marzinski 		list_del(&td->list);
71186f1152bSBenjamin Marzinski 		kfree(td);
71286f1152bSBenjamin Marzinski 	}
71386f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
71486f1152bSBenjamin Marzinski }
71586f1152bSBenjamin Marzinski EXPORT_SYMBOL(dm_put_table_device);
71686f1152bSBenjamin Marzinski 
71786f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices)
71886f1152bSBenjamin Marzinski {
71986f1152bSBenjamin Marzinski 	struct list_head *tmp, *next;
72086f1152bSBenjamin Marzinski 
72186f1152bSBenjamin Marzinski 	list_for_each_safe(tmp, next, devices) {
72286f1152bSBenjamin Marzinski 		struct table_device *td = list_entry(tmp, struct table_device, list);
72386f1152bSBenjamin Marzinski 
72486f1152bSBenjamin Marzinski 		DMWARN("dm_destroy: %s still exists with %d references",
72586f1152bSBenjamin Marzinski 		       td->dm_dev.name, atomic_read(&td->count));
72686f1152bSBenjamin Marzinski 		kfree(td);
72786f1152bSBenjamin Marzinski 	}
72886f1152bSBenjamin Marzinski }
72986f1152bSBenjamin Marzinski 
73086f1152bSBenjamin Marzinski /*
7313ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
7323ac51e74SDarrick J. Wong  */
7333ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
7343ac51e74SDarrick J. Wong {
7353ac51e74SDarrick J. Wong 	*geo = md->geometry;
7363ac51e74SDarrick J. Wong 
7373ac51e74SDarrick J. Wong 	return 0;
7383ac51e74SDarrick J. Wong }
7393ac51e74SDarrick J. Wong 
7403ac51e74SDarrick J. Wong /*
7413ac51e74SDarrick J. Wong  * Set the geometry of a device.
7423ac51e74SDarrick J. Wong  */
7433ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
7443ac51e74SDarrick J. Wong {
7453ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
7463ac51e74SDarrick J. Wong 
7473ac51e74SDarrick J. Wong 	if (geo->start > sz) {
7483ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
7493ac51e74SDarrick J. Wong 		return -EINVAL;
7503ac51e74SDarrick J. Wong 	}
7513ac51e74SDarrick J. Wong 
7523ac51e74SDarrick J. Wong 	md->geometry = *geo;
7533ac51e74SDarrick J. Wong 
7543ac51e74SDarrick J. Wong 	return 0;
7553ac51e74SDarrick J. Wong }
7563ac51e74SDarrick J. Wong 
7571da177e4SLinus Torvalds /*-----------------------------------------------------------------
7581da177e4SLinus Torvalds  * CRUD START:
7591da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
7601da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
7611da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
7621da177e4SLinus Torvalds  *   interests of getting something for people to use I give
7631da177e4SLinus Torvalds  *   you this clearly demarcated crap.
7641da177e4SLinus Torvalds  *---------------------------------------------------------------*/
7651da177e4SLinus Torvalds 
7662e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
7672e93ccc1SKiyoshi Ueda {
7682e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
7692e93ccc1SKiyoshi Ueda }
7702e93ccc1SKiyoshi Ueda 
7711da177e4SLinus Torvalds /*
7721da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
7731da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
7741da177e4SLinus Torvalds  */
7754e4cbee9SChristoph Hellwig static void dec_pending(struct dm_io *io, blk_status_t error)
7761da177e4SLinus Torvalds {
7772e93ccc1SKiyoshi Ueda 	unsigned long flags;
7784e4cbee9SChristoph Hellwig 	blk_status_t io_error;
779b35f8caaSMilan Broz 	struct bio *bio;
780b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
7812e93ccc1SKiyoshi Ueda 
7822e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
783f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
784f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
7854e4cbee9SChristoph Hellwig 		if (!(io->status == BLK_STS_DM_REQUEUE &&
7864e4cbee9SChristoph Hellwig 				__noflush_suspending(md)))
7874e4cbee9SChristoph Hellwig 			io->status = error;
788f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
789f88fb981SKiyoshi Ueda 	}
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
7924e4cbee9SChristoph Hellwig 		if (io->status == BLK_STS_DM_REQUEUE) {
7932e93ccc1SKiyoshi Ueda 			/*
7942e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
7952e93ccc1SKiyoshi Ueda 			 */
796022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
7976a8736d1STejun Heo 			if (__noflush_suspending(md))
7986a8736d1STejun Heo 				bio_list_add_head(&md->deferred, io->bio);
7996a8736d1STejun Heo 			else
8002e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
8014e4cbee9SChristoph Hellwig 				io->status = BLK_STS_IOERR;
802022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
8032e93ccc1SKiyoshi Ueda 		}
8042e93ccc1SKiyoshi Ueda 
8054e4cbee9SChristoph Hellwig 		io_error = io->status;
806b35f8caaSMilan Broz 		bio = io->bio;
807af7e466aSMikulas Patocka 		end_io_acct(io);
808a97f925aSMikulas Patocka 		free_io(md, io);
8091da177e4SLinus Torvalds 
8104e4cbee9SChristoph Hellwig 		if (io_error == BLK_STS_DM_REQUEUE)
8116a8736d1STejun Heo 			return;
8126a8736d1STejun Heo 
8131eff9d32SJens Axboe 		if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) {
8141da177e4SLinus Torvalds 			/*
8156a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
81628a8f0d3SMike Christie 			 * without REQ_PREFLUSH.
8171da177e4SLinus Torvalds 			 */
8181eff9d32SJens Axboe 			bio->bi_opf &= ~REQ_PREFLUSH;
8196a8736d1STejun Heo 			queue_io(md, bio);
8205f3ea37cSArnaldo Carvalho de Melo 		} else {
821b372d360SMike Snitzer 			/* done with normal IO or empty flush */
8224e4cbee9SChristoph Hellwig 			bio->bi_status = io_error;
8234246a0b6SChristoph Hellwig 			bio_endio(bio);
8242e93ccc1SKiyoshi Ueda 		}
8251da177e4SLinus Torvalds 	}
826af7e466aSMikulas Patocka }
8271da177e4SLinus Torvalds 
8284cc96131SMike Snitzer void disable_write_same(struct mapped_device *md)
8297eee4ae2SMike Snitzer {
8307eee4ae2SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
8317eee4ae2SMike Snitzer 
8327eee4ae2SMike Snitzer 	/* device doesn't really support WRITE SAME, disable it */
8337eee4ae2SMike Snitzer 	limits->max_write_same_sectors = 0;
8347eee4ae2SMike Snitzer }
8357eee4ae2SMike Snitzer 
836ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md)
837ac62d620SChristoph Hellwig {
838ac62d620SChristoph Hellwig 	struct queue_limits *limits = dm_get_queue_limits(md);
839ac62d620SChristoph Hellwig 
840ac62d620SChristoph Hellwig 	/* device doesn't really support WRITE ZEROES, disable it */
841ac62d620SChristoph Hellwig 	limits->max_write_zeroes_sectors = 0;
842ac62d620SChristoph Hellwig }
843ac62d620SChristoph Hellwig 
8444246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio)
8451da177e4SLinus Torvalds {
8464e4cbee9SChristoph Hellwig 	blk_status_t error = bio->bi_status;
847bfc6d41cSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
848b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
8499faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
8501da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
8511da177e4SLinus Torvalds 
8524e4cbee9SChristoph Hellwig 	if (unlikely(error == BLK_STS_TARGET)) {
853ac62d620SChristoph Hellwig 		if (bio_op(bio) == REQ_OP_WRITE_SAME &&
854*74d46992SChristoph Hellwig 		    !bio->bi_disk->queue->limits.max_write_same_sectors)
8557eee4ae2SMike Snitzer 			disable_write_same(md);
856ac62d620SChristoph Hellwig 		if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
857*74d46992SChristoph Hellwig 		    !bio->bi_disk->queue->limits.max_write_zeroes_sectors)
858ac62d620SChristoph Hellwig 			disable_write_zeroes(md);
859ac62d620SChristoph Hellwig 	}
8607eee4ae2SMike Snitzer 
8611be56909SChristoph Hellwig 	if (endio) {
8624e4cbee9SChristoph Hellwig 		int r = endio(tio->ti, bio, &error);
8631be56909SChristoph Hellwig 		switch (r) {
8641be56909SChristoph Hellwig 		case DM_ENDIO_REQUEUE:
8654e4cbee9SChristoph Hellwig 			error = BLK_STS_DM_REQUEUE;
8661be56909SChristoph Hellwig 			/*FALLTHRU*/
8671be56909SChristoph Hellwig 		case DM_ENDIO_DONE:
8681be56909SChristoph Hellwig 			break;
8691be56909SChristoph Hellwig 		case DM_ENDIO_INCOMPLETE:
8701be56909SChristoph Hellwig 			/* The target will handle the io */
8711be56909SChristoph Hellwig 			return;
8721be56909SChristoph Hellwig 		default:
8731be56909SChristoph Hellwig 			DMWARN("unimplemented target endio return value: %d", r);
8741be56909SChristoph Hellwig 			BUG();
8751be56909SChristoph Hellwig 		}
8761be56909SChristoph Hellwig 	}
8771be56909SChristoph Hellwig 
878cfae7529SMike Snitzer 	free_tio(tio);
879b35f8caaSMilan Broz 	dec_pending(io, error);
8801da177e4SLinus Torvalds }
8811da177e4SLinus Torvalds 
88278d8e58aSMike Snitzer /*
88356a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
88456a67df7SMike Snitzer  * target boundary.
88556a67df7SMike Snitzer  */
88656a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
8871da177e4SLinus Torvalds {
88856a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
88956a67df7SMike Snitzer 
89056a67df7SMike Snitzer 	return ti->len - target_offset;
89156a67df7SMike Snitzer }
89256a67df7SMike Snitzer 
89356a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
89456a67df7SMike Snitzer {
89556a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
896542f9038SMike Snitzer 	sector_t offset, max_len;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	/*
8991da177e4SLinus Torvalds 	 * Does the target need to split even further?
9001da177e4SLinus Torvalds 	 */
901542f9038SMike Snitzer 	if (ti->max_io_len) {
902542f9038SMike Snitzer 		offset = dm_target_offset(ti, sector);
903542f9038SMike Snitzer 		if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
904542f9038SMike Snitzer 			max_len = sector_div(offset, ti->max_io_len);
905542f9038SMike Snitzer 		else
906542f9038SMike Snitzer 			max_len = offset & (ti->max_io_len - 1);
907542f9038SMike Snitzer 		max_len = ti->max_io_len - max_len;
908542f9038SMike Snitzer 
909542f9038SMike Snitzer 		if (len > max_len)
910542f9038SMike Snitzer 			len = max_len;
9111da177e4SLinus Torvalds 	}
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 	return len;
9141da177e4SLinus Torvalds }
9151da177e4SLinus Torvalds 
916542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
917542f9038SMike Snitzer {
918542f9038SMike Snitzer 	if (len > UINT_MAX) {
919542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
920542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
921542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
922542f9038SMike Snitzer 		return -EINVAL;
923542f9038SMike Snitzer 	}
924542f9038SMike Snitzer 
925542f9038SMike Snitzer 	ti->max_io_len = (uint32_t) len;
926542f9038SMike Snitzer 
927542f9038SMike Snitzer 	return 0;
928542f9038SMike Snitzer }
929542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
930542f9038SMike Snitzer 
931f26c5719SDan Williams static struct dm_target *dm_dax_get_live_target(struct mapped_device *md,
932f26c5719SDan Williams 		sector_t sector, int *srcu_idx)
933545ed20eSToshi Kani {
934545ed20eSToshi Kani 	struct dm_table *map;
935545ed20eSToshi Kani 	struct dm_target *ti;
936545ed20eSToshi Kani 
937f26c5719SDan Williams 	map = dm_get_live_table(md, srcu_idx);
938545ed20eSToshi Kani 	if (!map)
939f26c5719SDan Williams 		return NULL;
940545ed20eSToshi Kani 
941545ed20eSToshi Kani 	ti = dm_table_find_target(map, sector);
942545ed20eSToshi Kani 	if (!dm_target_is_valid(ti))
943f26c5719SDan Williams 		return NULL;
944f26c5719SDan Williams 
945f26c5719SDan Williams 	return ti;
946f26c5719SDan Williams }
947f26c5719SDan Williams 
948f26c5719SDan Williams static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
949f26c5719SDan Williams 		long nr_pages, void **kaddr, pfn_t *pfn)
950f26c5719SDan Williams {
951f26c5719SDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
952f26c5719SDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
953f26c5719SDan Williams 	struct dm_target *ti;
954f26c5719SDan Williams 	long len, ret = -EIO;
955f26c5719SDan Williams 	int srcu_idx;
956f26c5719SDan Williams 
957f26c5719SDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
958f26c5719SDan Williams 
959f26c5719SDan Williams 	if (!ti)
960545ed20eSToshi Kani 		goto out;
961f26c5719SDan Williams 	if (!ti->type->direct_access)
962f26c5719SDan Williams 		goto out;
963f26c5719SDan Williams 	len = max_io_len(sector, ti) / PAGE_SECTORS;
964f26c5719SDan Williams 	if (len < 1)
965f26c5719SDan Williams 		goto out;
966f26c5719SDan Williams 	nr_pages = min(len, nr_pages);
967545ed20eSToshi Kani 	if (ti->type->direct_access)
968817bf402SDan Williams 		ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn);
969817bf402SDan Williams 
970545ed20eSToshi Kani  out:
971545ed20eSToshi Kani 	dm_put_live_table(md, srcu_idx);
972f26c5719SDan Williams 
973f26c5719SDan Williams 	return ret;
974545ed20eSToshi Kani }
975545ed20eSToshi Kani 
9767e026c8cSDan Williams static size_t dm_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
9777e026c8cSDan Williams 		void *addr, size_t bytes, struct iov_iter *i)
9787e026c8cSDan Williams {
9797e026c8cSDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
9807e026c8cSDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
9817e026c8cSDan Williams 	struct dm_target *ti;
9827e026c8cSDan Williams 	long ret = 0;
9837e026c8cSDan Williams 	int srcu_idx;
9847e026c8cSDan Williams 
9857e026c8cSDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
9867e026c8cSDan Williams 
9877e026c8cSDan Williams 	if (!ti)
9887e026c8cSDan Williams 		goto out;
9897e026c8cSDan Williams 	if (!ti->type->dax_copy_from_iter) {
9907e026c8cSDan Williams 		ret = copy_from_iter(addr, bytes, i);
9917e026c8cSDan Williams 		goto out;
9927e026c8cSDan Williams 	}
9937e026c8cSDan Williams 	ret = ti->type->dax_copy_from_iter(ti, pgoff, addr, bytes, i);
9947e026c8cSDan Williams  out:
9957e026c8cSDan Williams 	dm_put_live_table(md, srcu_idx);
9967e026c8cSDan Williams 
9977e026c8cSDan Williams 	return ret;
9987e026c8cSDan Williams }
9997e026c8cSDan Williams 
1000abebfbe2SDan Williams static void dm_dax_flush(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
1001abebfbe2SDan Williams 		size_t size)
1002abebfbe2SDan Williams {
1003abebfbe2SDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
1004abebfbe2SDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
1005abebfbe2SDan Williams 	struct dm_target *ti;
1006abebfbe2SDan Williams 	int srcu_idx;
1007abebfbe2SDan Williams 
1008abebfbe2SDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1009abebfbe2SDan Williams 
1010abebfbe2SDan Williams 	if (!ti)
1011abebfbe2SDan Williams 		goto out;
1012abebfbe2SDan Williams 	if (ti->type->dax_flush)
1013abebfbe2SDan Williams 		ti->type->dax_flush(ti, pgoff, addr, size);
1014abebfbe2SDan Williams  out:
1015abebfbe2SDan Williams 	dm_put_live_table(md, srcu_idx);
1016abebfbe2SDan Williams }
1017abebfbe2SDan Williams 
10181dd40c3eSMikulas Patocka /*
10191dd40c3eSMikulas Patocka  * A target may call dm_accept_partial_bio only from the map routine.  It is
102028a8f0d3SMike Christie  * allowed for all bio types except REQ_PREFLUSH.
10211dd40c3eSMikulas Patocka  *
10221dd40c3eSMikulas Patocka  * dm_accept_partial_bio informs the dm that the target only wants to process
10231dd40c3eSMikulas Patocka  * additional n_sectors sectors of the bio and the rest of the data should be
10241dd40c3eSMikulas Patocka  * sent in a next bio.
10251dd40c3eSMikulas Patocka  *
10261dd40c3eSMikulas Patocka  * A diagram that explains the arithmetics:
10271dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
10281dd40c3eSMikulas Patocka  * |         1          |       2       |   3   |
10291dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
10301dd40c3eSMikulas Patocka  *
10311dd40c3eSMikulas Patocka  * <-------------- *tio->len_ptr --------------->
10321dd40c3eSMikulas Patocka  *                      <------- bi_size ------->
10331dd40c3eSMikulas Patocka  *                      <-- n_sectors -->
10341dd40c3eSMikulas Patocka  *
10351dd40c3eSMikulas Patocka  * Region 1 was already iterated over with bio_advance or similar function.
10361dd40c3eSMikulas Patocka  *	(it may be empty if the target doesn't use bio_advance)
10371dd40c3eSMikulas Patocka  * Region 2 is the remaining bio size that the target wants to process.
10381dd40c3eSMikulas Patocka  *	(it may be empty if region 1 is non-empty, although there is no reason
10391dd40c3eSMikulas Patocka  *	 to make it empty)
10401dd40c3eSMikulas Patocka  * The target requires that region 3 is to be sent in the next bio.
10411dd40c3eSMikulas Patocka  *
10421dd40c3eSMikulas Patocka  * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
10431dd40c3eSMikulas Patocka  * the partially processed part (the sum of regions 1+2) must be the same for all
10441dd40c3eSMikulas Patocka  * copies of the bio.
10451dd40c3eSMikulas Patocka  */
10461dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
10471dd40c3eSMikulas Patocka {
10481dd40c3eSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
10491dd40c3eSMikulas Patocka 	unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
10501eff9d32SJens Axboe 	BUG_ON(bio->bi_opf & REQ_PREFLUSH);
10511dd40c3eSMikulas Patocka 	BUG_ON(bi_size > *tio->len_ptr);
10521dd40c3eSMikulas Patocka 	BUG_ON(n_sectors > bi_size);
10531dd40c3eSMikulas Patocka 	*tio->len_ptr -= bi_size - n_sectors;
10541dd40c3eSMikulas Patocka 	bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
10551dd40c3eSMikulas Patocka }
10561dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
10571dd40c3eSMikulas Patocka 
1058d67a5f4bSMikulas Patocka /*
105910999307SDamien Le Moal  * The zone descriptors obtained with a zone report indicate
106010999307SDamien Le Moal  * zone positions within the target device. The zone descriptors
106110999307SDamien Le Moal  * must be remapped to match their position within the dm device.
106210999307SDamien Le Moal  * A target may call dm_remap_zone_report after completion of a
106310999307SDamien Le Moal  * REQ_OP_ZONE_REPORT bio to remap the zone descriptors obtained
106410999307SDamien Le Moal  * from the target device mapping to the dm device.
106510999307SDamien Le Moal  */
106610999307SDamien Le Moal void dm_remap_zone_report(struct dm_target *ti, struct bio *bio, sector_t start)
106710999307SDamien Le Moal {
106810999307SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
106910999307SDamien Le Moal 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
107010999307SDamien Le Moal 	struct bio *report_bio = tio->io->bio;
107110999307SDamien Le Moal 	struct blk_zone_report_hdr *hdr = NULL;
107210999307SDamien Le Moal 	struct blk_zone *zone;
107310999307SDamien Le Moal 	unsigned int nr_rep = 0;
107410999307SDamien Le Moal 	unsigned int ofst;
107510999307SDamien Le Moal 	struct bio_vec bvec;
107610999307SDamien Le Moal 	struct bvec_iter iter;
107710999307SDamien Le Moal 	void *addr;
107810999307SDamien Le Moal 
107910999307SDamien Le Moal 	if (bio->bi_status)
108010999307SDamien Le Moal 		return;
108110999307SDamien Le Moal 
108210999307SDamien Le Moal 	/*
108310999307SDamien Le Moal 	 * Remap the start sector of the reported zones. For sequential zones,
108410999307SDamien Le Moal 	 * also remap the write pointer position.
108510999307SDamien Le Moal 	 */
108610999307SDamien Le Moal 	bio_for_each_segment(bvec, report_bio, iter) {
108710999307SDamien Le Moal 		addr = kmap_atomic(bvec.bv_page);
108810999307SDamien Le Moal 
108910999307SDamien Le Moal 		/* Remember the report header in the first page */
109010999307SDamien Le Moal 		if (!hdr) {
109110999307SDamien Le Moal 			hdr = addr;
109210999307SDamien Le Moal 			ofst = sizeof(struct blk_zone_report_hdr);
109310999307SDamien Le Moal 		} else
109410999307SDamien Le Moal 			ofst = 0;
109510999307SDamien Le Moal 
109610999307SDamien Le Moal 		/* Set zones start sector */
109710999307SDamien Le Moal 		while (hdr->nr_zones && ofst < bvec.bv_len) {
109810999307SDamien Le Moal 			zone = addr + ofst;
109910999307SDamien Le Moal 			if (zone->start >= start + ti->len) {
110010999307SDamien Le Moal 				hdr->nr_zones = 0;
110110999307SDamien Le Moal 				break;
110210999307SDamien Le Moal 			}
110310999307SDamien Le Moal 			zone->start = zone->start + ti->begin - start;
110410999307SDamien Le Moal 			if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL) {
110510999307SDamien Le Moal 				if (zone->cond == BLK_ZONE_COND_FULL)
110610999307SDamien Le Moal 					zone->wp = zone->start + zone->len;
110710999307SDamien Le Moal 				else if (zone->cond == BLK_ZONE_COND_EMPTY)
110810999307SDamien Le Moal 					zone->wp = zone->start;
110910999307SDamien Le Moal 				else
111010999307SDamien Le Moal 					zone->wp = zone->wp + ti->begin - start;
111110999307SDamien Le Moal 			}
111210999307SDamien Le Moal 			ofst += sizeof(struct blk_zone);
111310999307SDamien Le Moal 			hdr->nr_zones--;
111410999307SDamien Le Moal 			nr_rep++;
111510999307SDamien Le Moal 		}
111610999307SDamien Le Moal 
111710999307SDamien Le Moal 		if (addr != hdr)
111810999307SDamien Le Moal 			kunmap_atomic(addr);
111910999307SDamien Le Moal 
112010999307SDamien Le Moal 		if (!hdr->nr_zones)
112110999307SDamien Le Moal 			break;
112210999307SDamien Le Moal 	}
112310999307SDamien Le Moal 
112410999307SDamien Le Moal 	if (hdr) {
112510999307SDamien Le Moal 		hdr->nr_zones = nr_rep;
112610999307SDamien Le Moal 		kunmap_atomic(hdr);
112710999307SDamien Le Moal 	}
112810999307SDamien Le Moal 
112910999307SDamien Le Moal 	bio_advance(report_bio, report_bio->bi_iter.bi_size);
113010999307SDamien Le Moal 
113110999307SDamien Le Moal #else /* !CONFIG_BLK_DEV_ZONED */
113210999307SDamien Le Moal 	bio->bi_status = BLK_STS_NOTSUPP;
113310999307SDamien Le Moal #endif
113410999307SDamien Le Moal }
113510999307SDamien Le Moal EXPORT_SYMBOL_GPL(dm_remap_zone_report);
113610999307SDamien Le Moal 
113710999307SDamien Le Moal /*
1138d67a5f4bSMikulas Patocka  * Flush current->bio_list when the target map method blocks.
1139d67a5f4bSMikulas Patocka  * This fixes deadlocks in snapshot and possibly in other targets.
1140d67a5f4bSMikulas Patocka  */
1141d67a5f4bSMikulas Patocka struct dm_offload {
1142d67a5f4bSMikulas Patocka 	struct blk_plug plug;
1143d67a5f4bSMikulas Patocka 	struct blk_plug_cb cb;
1144d67a5f4bSMikulas Patocka };
1145d67a5f4bSMikulas Patocka 
1146d67a5f4bSMikulas Patocka static void flush_current_bio_list(struct blk_plug_cb *cb, bool from_schedule)
1147d67a5f4bSMikulas Patocka {
1148d67a5f4bSMikulas Patocka 	struct dm_offload *o = container_of(cb, struct dm_offload, cb);
1149d67a5f4bSMikulas Patocka 	struct bio_list list;
1150d67a5f4bSMikulas Patocka 	struct bio *bio;
1151f5fe1b51SNeilBrown 	int i;
1152d67a5f4bSMikulas Patocka 
1153d67a5f4bSMikulas Patocka 	INIT_LIST_HEAD(&o->cb.list);
1154d67a5f4bSMikulas Patocka 
1155d67a5f4bSMikulas Patocka 	if (unlikely(!current->bio_list))
1156d67a5f4bSMikulas Patocka 		return;
1157d67a5f4bSMikulas Patocka 
1158f5fe1b51SNeilBrown 	for (i = 0; i < 2; i++) {
1159f5fe1b51SNeilBrown 		list = current->bio_list[i];
1160f5fe1b51SNeilBrown 		bio_list_init(&current->bio_list[i]);
1161d67a5f4bSMikulas Patocka 
1162d67a5f4bSMikulas Patocka 		while ((bio = bio_list_pop(&list))) {
1163d67a5f4bSMikulas Patocka 			struct bio_set *bs = bio->bi_pool;
116447e0fb46SNeilBrown 			if (unlikely(!bs) || bs == fs_bio_set ||
116547e0fb46SNeilBrown 			    !bs->rescue_workqueue) {
1166f5fe1b51SNeilBrown 				bio_list_add(&current->bio_list[i], bio);
1167d67a5f4bSMikulas Patocka 				continue;
1168d67a5f4bSMikulas Patocka 			}
1169d67a5f4bSMikulas Patocka 
1170d67a5f4bSMikulas Patocka 			spin_lock(&bs->rescue_lock);
1171d67a5f4bSMikulas Patocka 			bio_list_add(&bs->rescue_list, bio);
1172d67a5f4bSMikulas Patocka 			queue_work(bs->rescue_workqueue, &bs->rescue_work);
1173d67a5f4bSMikulas Patocka 			spin_unlock(&bs->rescue_lock);
1174d67a5f4bSMikulas Patocka 		}
1175d67a5f4bSMikulas Patocka 	}
1176f5fe1b51SNeilBrown }
1177d67a5f4bSMikulas Patocka 
1178d67a5f4bSMikulas Patocka static void dm_offload_start(struct dm_offload *o)
1179d67a5f4bSMikulas Patocka {
1180d67a5f4bSMikulas Patocka 	blk_start_plug(&o->plug);
1181d67a5f4bSMikulas Patocka 	o->cb.callback = flush_current_bio_list;
1182d67a5f4bSMikulas Patocka 	list_add(&o->cb.list, &current->plug->cb_list);
1183d67a5f4bSMikulas Patocka }
1184d67a5f4bSMikulas Patocka 
1185d67a5f4bSMikulas Patocka static void dm_offload_end(struct dm_offload *o)
1186d67a5f4bSMikulas Patocka {
1187d67a5f4bSMikulas Patocka 	list_del(&o->cb.list);
1188d67a5f4bSMikulas Patocka 	blk_finish_plug(&o->plug);
1189d67a5f4bSMikulas Patocka }
1190d67a5f4bSMikulas Patocka 
1191bd2a49b8SAlasdair G Kergon static void __map_bio(struct dm_target_io *tio)
11921da177e4SLinus Torvalds {
11931da177e4SLinus Torvalds 	int r;
11942056a782SJens Axboe 	sector_t sector;
1195d67a5f4bSMikulas Patocka 	struct dm_offload o;
1196dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
1197bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 	/*
12021da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
12031da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
12041da177e4SLinus Torvalds 	 * this io.
12051da177e4SLinus Torvalds 	 */
12061da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
12074f024f37SKent Overstreet 	sector = clone->bi_iter.bi_sector;
1208d67a5f4bSMikulas Patocka 
1209d67a5f4bSMikulas Patocka 	dm_offload_start(&o);
12107de3ee57SMikulas Patocka 	r = ti->type->map(ti, clone);
1211d67a5f4bSMikulas Patocka 	dm_offload_end(&o);
1212d67a5f4bSMikulas Patocka 
1213846785e6SChristoph Hellwig 	switch (r) {
1214846785e6SChristoph Hellwig 	case DM_MAPIO_SUBMITTED:
1215846785e6SChristoph Hellwig 		break;
1216846785e6SChristoph Hellwig 	case DM_MAPIO_REMAPPED:
12171da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
1218*74d46992SChristoph Hellwig 		trace_block_bio_remap(clone->bi_disk->queue, clone,
1219*74d46992SChristoph Hellwig 				      bio_dev(tio->io->bio), sector);
12201da177e4SLinus Torvalds 		generic_make_request(clone);
1221846785e6SChristoph Hellwig 		break;
1222846785e6SChristoph Hellwig 	case DM_MAPIO_KILL:
12234e4cbee9SChristoph Hellwig 		dec_pending(tio->io, BLK_STS_IOERR);
12244e4cbee9SChristoph Hellwig 		free_tio(tio);
12254e4cbee9SChristoph Hellwig 		break;
1226846785e6SChristoph Hellwig 	case DM_MAPIO_REQUEUE:
12274e4cbee9SChristoph Hellwig 		dec_pending(tio->io, BLK_STS_DM_REQUEUE);
1228cfae7529SMike Snitzer 		free_tio(tio);
1229846785e6SChristoph Hellwig 		break;
1230846785e6SChristoph Hellwig 	default:
123145cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
123245cbcd79SKiyoshi Ueda 		BUG();
12331da177e4SLinus Torvalds 	}
12341da177e4SLinus Torvalds }
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds struct clone_info {
12371da177e4SLinus Torvalds 	struct mapped_device *md;
12381da177e4SLinus Torvalds 	struct dm_table *map;
12391da177e4SLinus Torvalds 	struct bio *bio;
12401da177e4SLinus Torvalds 	struct dm_io *io;
12411da177e4SLinus Torvalds 	sector_t sector;
1242e0d6609aSMikulas Patocka 	unsigned sector_count;
12431da177e4SLinus Torvalds };
12441da177e4SLinus Torvalds 
1245e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
1246bd2a49b8SAlasdair G Kergon {
12474f024f37SKent Overstreet 	bio->bi_iter.bi_sector = sector;
12484f024f37SKent Overstreet 	bio->bi_iter.bi_size = to_bytes(len);
12491da177e4SLinus Torvalds }
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds /*
12521da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
12531da177e4SLinus Torvalds  */
1254c80914e8SMike Snitzer static int clone_bio(struct dm_target_io *tio, struct bio *bio,
12551c3b13e6SKent Overstreet 		     sector_t sector, unsigned len)
12561da177e4SLinus Torvalds {
1257dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
12581da177e4SLinus Torvalds 
12591c3b13e6SKent Overstreet 	__bio_clone_fast(clone, bio);
12609c47008dSMartin K. Petersen 
1261e2460f2aSMikulas Patocka 	if (unlikely(bio_integrity(bio) != NULL)) {
1262e2460f2aSMikulas Patocka 		int r;
1263e2460f2aSMikulas Patocka 
1264e2460f2aSMikulas Patocka 		if (unlikely(!dm_target_has_integrity(tio->ti->type) &&
1265e2460f2aSMikulas Patocka 			     !dm_target_passes_integrity(tio->ti->type))) {
1266e2460f2aSMikulas Patocka 			DMWARN("%s: the target %s doesn't support integrity data.",
1267e2460f2aSMikulas Patocka 				dm_device_name(tio->io->md),
1268e2460f2aSMikulas Patocka 				tio->ti->type->name);
1269e2460f2aSMikulas Patocka 			return -EIO;
1270e2460f2aSMikulas Patocka 		}
1271e2460f2aSMikulas Patocka 
1272e2460f2aSMikulas Patocka 		r = bio_integrity_clone(clone, bio, GFP_NOIO);
1273c80914e8SMike Snitzer 		if (r < 0)
1274c80914e8SMike Snitzer 			return r;
1275c80914e8SMike Snitzer 	}
12761c3b13e6SKent Overstreet 
1277264c869dSDamien Le Moal 	if (bio_op(bio) != REQ_OP_ZONE_REPORT)
12781c3b13e6SKent Overstreet 		bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
12791c3b13e6SKent Overstreet 	clone->bi_iter.bi_size = to_bytes(len);
12801c3b13e6SKent Overstreet 
1281e2460f2aSMikulas Patocka 	if (unlikely(bio_integrity(bio) != NULL))
1282fbd08e76SDmitry Monakhov 		bio_integrity_trim(clone);
1283c80914e8SMike Snitzer 
1284c80914e8SMike Snitzer 	return 0;
12851da177e4SLinus Torvalds }
12861da177e4SLinus Torvalds 
12879015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci,
128899778273SJunichi Nomura 				      struct dm_target *ti,
128955a62eefSAlasdair G Kergon 				      unsigned target_bio_nr)
1290f9ab94ceSMikulas Patocka {
1291dba14160SMikulas Patocka 	struct dm_target_io *tio;
1292dba14160SMikulas Patocka 	struct bio *clone;
1293dba14160SMikulas Patocka 
129499778273SJunichi Nomura 	clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1295dba14160SMikulas Patocka 	tio = container_of(clone, struct dm_target_io, clone);
1296f9ab94ceSMikulas Patocka 
1297f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1298f9ab94ceSMikulas Patocka 	tio->ti = ti;
129955a62eefSAlasdair G Kergon 	tio->target_bio_nr = target_bio_nr;
13009015df24SAlasdair G Kergon 
13019015df24SAlasdair G Kergon 	return tio;
13029015df24SAlasdair G Kergon }
13039015df24SAlasdair G Kergon 
130414fe594dSAlasdair G Kergon static void __clone_and_map_simple_bio(struct clone_info *ci,
130514fe594dSAlasdair G Kergon 				       struct dm_target *ti,
13061dd40c3eSMikulas Patocka 				       unsigned target_bio_nr, unsigned *len)
13079015df24SAlasdair G Kergon {
130899778273SJunichi Nomura 	struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
1309dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
13109015df24SAlasdair G Kergon 
13111dd40c3eSMikulas Patocka 	tio->len_ptr = len;
13121dd40c3eSMikulas Patocka 
13131c3b13e6SKent Overstreet 	__bio_clone_fast(clone, ci->bio);
1314bd2a49b8SAlasdair G Kergon 	if (len)
13151dd40c3eSMikulas Patocka 		bio_setup_sector(clone, ci->sector, *len);
1316f9ab94ceSMikulas Patocka 
1317bd2a49b8SAlasdair G Kergon 	__map_bio(tio);
1318f9ab94ceSMikulas Patocka }
1319f9ab94ceSMikulas Patocka 
132014fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
13211dd40c3eSMikulas Patocka 				  unsigned num_bios, unsigned *len)
132206a426ceSMike Snitzer {
132355a62eefSAlasdair G Kergon 	unsigned target_bio_nr;
132406a426ceSMike Snitzer 
132555a62eefSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
132614fe594dSAlasdair G Kergon 		__clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
132706a426ceSMike Snitzer }
132806a426ceSMike Snitzer 
132914fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1330f9ab94ceSMikulas Patocka {
133106a426ceSMike Snitzer 	unsigned target_nr = 0;
1332f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1333f9ab94ceSMikulas Patocka 
1334b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1335f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
13361dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
1337f9ab94ceSMikulas Patocka 
1338f9ab94ceSMikulas Patocka 	return 0;
1339f9ab94ceSMikulas Patocka }
1340f9ab94ceSMikulas Patocka 
1341c80914e8SMike Snitzer static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
13421dd40c3eSMikulas Patocka 				     sector_t sector, unsigned *len)
13435ae89a87SMike Snitzer {
1344dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
13455ae89a87SMike Snitzer 	struct dm_target_io *tio;
1346b0d8ed4dSAlasdair G Kergon 	unsigned target_bio_nr;
1347b0d8ed4dSAlasdair G Kergon 	unsigned num_target_bios = 1;
1348c80914e8SMike Snitzer 	int r = 0;
13495ae89a87SMike Snitzer 
1350b0d8ed4dSAlasdair G Kergon 	/*
1351b0d8ed4dSAlasdair G Kergon 	 * Does the target want to receive duplicate copies of the bio?
1352b0d8ed4dSAlasdair G Kergon 	 */
1353b0d8ed4dSAlasdair G Kergon 	if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1354b0d8ed4dSAlasdair G Kergon 		num_target_bios = ti->num_write_bios(ti, bio);
1355e4c93811SAlasdair G Kergon 
1356b0d8ed4dSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
135799778273SJunichi Nomura 		tio = alloc_tio(ci, ti, target_bio_nr);
13581dd40c3eSMikulas Patocka 		tio->len_ptr = len;
1359c80914e8SMike Snitzer 		r = clone_bio(tio, bio, sector, *len);
1360072623deSMikulas Patocka 		if (r < 0) {
1361cfae7529SMike Snitzer 			free_tio(tio);
1362c80914e8SMike Snitzer 			break;
1363072623deSMikulas Patocka 		}
1364bd2a49b8SAlasdair G Kergon 		__map_bio(tio);
13655ae89a87SMike Snitzer 	}
1366c80914e8SMike Snitzer 
1367c80914e8SMike Snitzer 	return r;
1368b0d8ed4dSAlasdair G Kergon }
13695ae89a87SMike Snitzer 
137055a62eefSAlasdair G Kergon typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
137123508a96SMike Snitzer 
137255a62eefSAlasdair G Kergon static unsigned get_num_discard_bios(struct dm_target *ti)
137323508a96SMike Snitzer {
137455a62eefSAlasdair G Kergon 	return ti->num_discard_bios;
137523508a96SMike Snitzer }
137623508a96SMike Snitzer 
137755a62eefSAlasdair G Kergon static unsigned get_num_write_same_bios(struct dm_target *ti)
137823508a96SMike Snitzer {
137955a62eefSAlasdair G Kergon 	return ti->num_write_same_bios;
138023508a96SMike Snitzer }
138123508a96SMike Snitzer 
1382ac62d620SChristoph Hellwig static unsigned get_num_write_zeroes_bios(struct dm_target *ti)
1383ac62d620SChristoph Hellwig {
1384ac62d620SChristoph Hellwig 	return ti->num_write_zeroes_bios;
1385ac62d620SChristoph Hellwig }
1386ac62d620SChristoph Hellwig 
138723508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti);
138823508a96SMike Snitzer 
138923508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti)
139023508a96SMike Snitzer {
139155a62eefSAlasdair G Kergon 	return ti->split_discard_bios;
139223508a96SMike Snitzer }
139323508a96SMike Snitzer 
139414fe594dSAlasdair G Kergon static int __send_changing_extent_only(struct clone_info *ci,
139555a62eefSAlasdair G Kergon 				       get_num_bios_fn get_num_bios,
139623508a96SMike Snitzer 				       is_split_required_fn is_split_required)
13975ae89a87SMike Snitzer {
13985ae89a87SMike Snitzer 	struct dm_target *ti;
1399e0d6609aSMikulas Patocka 	unsigned len;
140055a62eefSAlasdair G Kergon 	unsigned num_bios;
14015ae89a87SMike Snitzer 
1402a79245b3SMike Snitzer 	do {
14035ae89a87SMike Snitzer 		ti = dm_table_find_target(ci->map, ci->sector);
14045ae89a87SMike Snitzer 		if (!dm_target_is_valid(ti))
14055ae89a87SMike Snitzer 			return -EIO;
14065ae89a87SMike Snitzer 
14075ae89a87SMike Snitzer 		/*
140823508a96SMike Snitzer 		 * Even though the device advertised support for this type of
140923508a96SMike Snitzer 		 * request, that does not mean every target supports it, and
1410936688d7SMike Snitzer 		 * reconfiguration might also have changed that since the
14115ae89a87SMike Snitzer 		 * check was performed.
14125ae89a87SMike Snitzer 		 */
141355a62eefSAlasdair G Kergon 		num_bios = get_num_bios ? get_num_bios(ti) : 0;
141455a62eefSAlasdair G Kergon 		if (!num_bios)
14155ae89a87SMike Snitzer 			return -EOPNOTSUPP;
14165ae89a87SMike Snitzer 
141723508a96SMike Snitzer 		if (is_split_required && !is_split_required(ti))
1418e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
14197acf0277SMikulas Patocka 		else
1420e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
14215ae89a87SMike Snitzer 
14221dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, num_bios, &len);
14235ae89a87SMike Snitzer 
1424a79245b3SMike Snitzer 		ci->sector += len;
1425a79245b3SMike Snitzer 	} while (ci->sector_count -= len);
14265ae89a87SMike Snitzer 
14275ae89a87SMike Snitzer 	return 0;
14285ae89a87SMike Snitzer }
14295ae89a87SMike Snitzer 
143014fe594dSAlasdair G Kergon static int __send_discard(struct clone_info *ci)
143123508a96SMike Snitzer {
143214fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_discard_bios,
143323508a96SMike Snitzer 					   is_split_required_for_discard);
143423508a96SMike Snitzer }
143523508a96SMike Snitzer 
143614fe594dSAlasdair G Kergon static int __send_write_same(struct clone_info *ci)
143723508a96SMike Snitzer {
143814fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
143923508a96SMike Snitzer }
144023508a96SMike Snitzer 
1441ac62d620SChristoph Hellwig static int __send_write_zeroes(struct clone_info *ci)
1442ac62d620SChristoph Hellwig {
1443ac62d620SChristoph Hellwig 	return __send_changing_extent_only(ci, get_num_write_zeroes_bios, NULL);
1444ac62d620SChristoph Hellwig }
1445ac62d620SChristoph Hellwig 
1446e4c93811SAlasdair G Kergon /*
1447e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1448e4c93811SAlasdair G Kergon  */
1449e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci)
1450e4c93811SAlasdair G Kergon {
1451e4c93811SAlasdair G Kergon 	struct bio *bio = ci->bio;
1452e4c93811SAlasdair G Kergon 	struct dm_target *ti;
14531c3b13e6SKent Overstreet 	unsigned len;
1454c80914e8SMike Snitzer 	int r;
1455e4c93811SAlasdair G Kergon 
1456e6047149SMike Christie 	if (unlikely(bio_op(bio) == REQ_OP_DISCARD))
1457e4c93811SAlasdair G Kergon 		return __send_discard(ci);
1458e6047149SMike Christie 	else if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
1459e4c93811SAlasdair G Kergon 		return __send_write_same(ci);
1460ac62d620SChristoph Hellwig 	else if (unlikely(bio_op(bio) == REQ_OP_WRITE_ZEROES))
1461ac62d620SChristoph Hellwig 		return __send_write_zeroes(ci);
1462e4c93811SAlasdair G Kergon 
1463e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1464e4c93811SAlasdair G Kergon 	if (!dm_target_is_valid(ti))
1465e4c93811SAlasdair G Kergon 		return -EIO;
1466e4c93811SAlasdair G Kergon 
1467264c869dSDamien Le Moal 	if (bio_op(bio) == REQ_OP_ZONE_REPORT)
1468264c869dSDamien Le Moal 		len = ci->sector_count;
1469264c869dSDamien Le Moal 	else
1470264c869dSDamien Le Moal 		len = min_t(sector_t, max_io_len(ci->sector, ti),
1471264c869dSDamien Le Moal 			    ci->sector_count);
1472e4c93811SAlasdair G Kergon 
1473c80914e8SMike Snitzer 	r = __clone_and_map_data_bio(ci, ti, ci->sector, &len);
1474c80914e8SMike Snitzer 	if (r < 0)
1475c80914e8SMike Snitzer 		return r;
1476e4c93811SAlasdair G Kergon 
1477e4c93811SAlasdair G Kergon 	ci->sector += len;
1478e4c93811SAlasdair G Kergon 	ci->sector_count -= len;
1479e4c93811SAlasdair G Kergon 
1480e4c93811SAlasdair G Kergon 	return 0;
1481e4c93811SAlasdair G Kergon }
1482e4c93811SAlasdair G Kergon 
1483e4c93811SAlasdair G Kergon /*
148414fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
14851da177e4SLinus Torvalds  */
148683d5e5b0SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md,
148783d5e5b0SMikulas Patocka 				    struct dm_table *map, struct bio *bio)
14881da177e4SLinus Torvalds {
14891da177e4SLinus Torvalds 	struct clone_info ci;
1490512875bdSJun'ichi Nomura 	int error = 0;
14911da177e4SLinus Torvalds 
149283d5e5b0SMikulas Patocka 	if (unlikely(!map)) {
1493f0b9a450SMikulas Patocka 		bio_io_error(bio);
1494f0b9a450SMikulas Patocka 		return;
1495f0b9a450SMikulas Patocka 	}
1496692d0eb9SMikulas Patocka 
149783d5e5b0SMikulas Patocka 	ci.map = map;
14981da177e4SLinus Torvalds 	ci.md = md;
14991da177e4SLinus Torvalds 	ci.io = alloc_io(md);
15004e4cbee9SChristoph Hellwig 	ci.io->status = 0;
15011da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
15021da177e4SLinus Torvalds 	ci.io->bio = bio;
15031da177e4SLinus Torvalds 	ci.io->md = md;
1504f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
15054f024f37SKent Overstreet 	ci.sector = bio->bi_iter.bi_sector;
15061da177e4SLinus Torvalds 
15073eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1508bd2a49b8SAlasdair G Kergon 
15091eff9d32SJens Axboe 	if (bio->bi_opf & REQ_PREFLUSH) {
1510b372d360SMike Snitzer 		ci.bio = &ci.md->flush_bio;
1511b372d360SMike Snitzer 		ci.sector_count = 0;
151214fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1513b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1514a4aa5e56SDamien Le Moal 	} else if (bio_op(bio) == REQ_OP_ZONE_RESET) {
1515a4aa5e56SDamien Le Moal 		ci.bio = bio;
1516a4aa5e56SDamien Le Moal 		ci.sector_count = 0;
1517a4aa5e56SDamien Le Moal 		error = __split_and_process_non_flush(&ci);
1518b372d360SMike Snitzer 	} else {
15196a8736d1STejun Heo 		ci.bio = bio;
1520f6fccb12SMilan Broz 		ci.sector_count = bio_sectors(bio);
1521512875bdSJun'ichi Nomura 		while (ci.sector_count && !error)
152214fe594dSAlasdair G Kergon 			error = __split_and_process_non_flush(&ci);
1523d87f4c14STejun Heo 	}
15241da177e4SLinus Torvalds 
15251da177e4SLinus Torvalds 	/* drop the extra reference count */
1526512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
15279e4e5f87SMilan Broz }
15289e4e5f87SMilan Broz /*-----------------------------------------------------------------
15291da177e4SLinus Torvalds  * CRUD END
15301da177e4SLinus Torvalds  *---------------------------------------------------------------*/
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds /*
15331da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
15341da177e4SLinus Torvalds  * dm_merge_bvec.
15351da177e4SLinus Torvalds  */
1536dece1635SJens Axboe static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
15371da177e4SLinus Torvalds {
153812f03a49SKevin Corry 	int rw = bio_data_dir(bio);
15391da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
154083d5e5b0SMikulas Patocka 	int srcu_idx;
154183d5e5b0SMikulas Patocka 	struct dm_table *map;
15421da177e4SLinus Torvalds 
154383d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
15441da177e4SLinus Torvalds 
1545d62e26b3SJens Axboe 	generic_start_io_acct(q, rw, bio_sectors(bio), &dm_disk(md)->part0);
154612f03a49SKevin Corry 
15476a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
15486a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
154983d5e5b0SMikulas Patocka 		dm_put_live_table(md, srcu_idx);
15501da177e4SLinus Torvalds 
15511eff9d32SJens Axboe 		if (!(bio->bi_opf & REQ_RAHEAD))
155292c63902SMikulas Patocka 			queue_io(md, bio);
15536a8736d1STejun Heo 		else
15546a8736d1STejun Heo 			bio_io_error(bio);
1555dece1635SJens Axboe 		return BLK_QC_T_NONE;
15561da177e4SLinus Torvalds 	}
15571da177e4SLinus Torvalds 
155883d5e5b0SMikulas Patocka 	__split_and_process_bio(md, map, bio);
155983d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
1560dece1635SJens Axboe 	return BLK_QC_T_NONE;
1561cec47e3dSKiyoshi Ueda }
1562cec47e3dSKiyoshi Ueda 
15631da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
15641da177e4SLinus Torvalds {
15658a57dfc6SChandra Seetharaman 	int r = bdi_bits;
15668a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
15678a57dfc6SChandra Seetharaman 	struct dm_table *map;
15681da177e4SLinus Torvalds 
15691eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
1570e522c039SMike Snitzer 		if (dm_request_based(md)) {
1571cec47e3dSKiyoshi Ueda 			/*
1572e522c039SMike Snitzer 			 * With request-based DM we only need to check the
1573e522c039SMike Snitzer 			 * top-level queue for congestion.
1574cec47e3dSKiyoshi Ueda 			 */
1575dc3b17ccSJan Kara 			r = md->queue->backing_dev_info->wb.state & bdi_bits;
1576e522c039SMike Snitzer 		} else {
1577e522c039SMike Snitzer 			map = dm_get_live_table_fast(md);
1578e522c039SMike Snitzer 			if (map)
15791da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
158083d5e5b0SMikulas Patocka 			dm_put_live_table_fast(md);
15818a57dfc6SChandra Seetharaman 		}
1582e522c039SMike Snitzer 	}
15838a57dfc6SChandra Seetharaman 
15841da177e4SLinus Torvalds 	return r;
15851da177e4SLinus Torvalds }
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds /*-----------------------------------------------------------------
15881da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
15891da177e4SLinus Torvalds  *---------------------------------------------------------------*/
15902b06cfffSAlasdair G Kergon static void free_minor(int minor)
15911da177e4SLinus Torvalds {
1592f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
15931da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1594f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
15951da177e4SLinus Torvalds }
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds /*
15981da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
15991da177e4SLinus Torvalds  */
1600cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
16011da177e4SLinus Torvalds {
1602c9d76be6STejun Heo 	int r;
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
16051da177e4SLinus Torvalds 		return -EINVAL;
16061da177e4SLinus Torvalds 
1607c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1608f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
16091da177e4SLinus Torvalds 
1610c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
16111da177e4SLinus Torvalds 
1612f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1613c9d76be6STejun Heo 	idr_preload_end();
1614c9d76be6STejun Heo 	if (r < 0)
1615c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
1616c9d76be6STejun Heo 	return 0;
16171da177e4SLinus Torvalds }
16181da177e4SLinus Torvalds 
1619cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
16201da177e4SLinus Torvalds {
1621c9d76be6STejun Heo 	int r;
16221da177e4SLinus Torvalds 
1623c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1624f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
16251da177e4SLinus Torvalds 
1626c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
16271da177e4SLinus Torvalds 
1628f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1629c9d76be6STejun Heo 	idr_preload_end();
1630c9d76be6STejun Heo 	if (r < 0)
16311da177e4SLinus Torvalds 		return r;
1632c9d76be6STejun Heo 	*minor = r;
1633c9d76be6STejun Heo 	return 0;
16341da177e4SLinus Torvalds }
16351da177e4SLinus Torvalds 
163683d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
1637f26c5719SDan Williams static const struct dax_operations dm_dax_ops;
16381da177e4SLinus Torvalds 
163953d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
164053d5914fSMikulas Patocka 
16414cc96131SMike Snitzer void dm_init_md_queue(struct mapped_device *md)
16424a0b4ddfSMike Snitzer {
16434a0b4ddfSMike Snitzer 	/*
16444a0b4ddfSMike Snitzer 	 * Request-based dm devices cannot be stacked on top of bio-based dm
1645bfebd1cdSMike Snitzer 	 * devices.  The type of this dm device may not have been decided yet.
16464a0b4ddfSMike Snitzer 	 * The type is decided at the first table loading time.
16474a0b4ddfSMike Snitzer 	 * To prevent problematic device stacking, clear the queue flag
16484a0b4ddfSMike Snitzer 	 * for request stacking support until then.
16494a0b4ddfSMike Snitzer 	 *
16504a0b4ddfSMike Snitzer 	 * This queue is new, so no concurrency on the queue_flags.
16514a0b4ddfSMike Snitzer 	 */
16524a0b4ddfSMike Snitzer 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1653ad5f498fSMikulas Patocka 
1654ad5f498fSMikulas Patocka 	/*
1655ad5f498fSMikulas Patocka 	 * Initialize data that will only be used by a non-blk-mq DM queue
1656ad5f498fSMikulas Patocka 	 * - must do so here (in alloc_dev callchain) before queue is used
1657ad5f498fSMikulas Patocka 	 */
1658ad5f498fSMikulas Patocka 	md->queue->queuedata = md;
1659dc3b17ccSJan Kara 	md->queue->backing_dev_info->congested_data = md;
1660bfebd1cdSMike Snitzer }
16614a0b4ddfSMike Snitzer 
16624cc96131SMike Snitzer void dm_init_normal_md_queue(struct mapped_device *md)
1663bfebd1cdSMike Snitzer {
166417e149b8SMike Snitzer 	md->use_blk_mq = false;
1665bfebd1cdSMike Snitzer 	dm_init_md_queue(md);
1666bfebd1cdSMike Snitzer 
1667bfebd1cdSMike Snitzer 	/*
1668bfebd1cdSMike Snitzer 	 * Initialize aspects of queue that aren't relevant for blk-mq
1669bfebd1cdSMike Snitzer 	 */
1670dc3b17ccSJan Kara 	md->queue->backing_dev_info->congested_fn = dm_any_congested;
16714a0b4ddfSMike Snitzer }
16724a0b4ddfSMike Snitzer 
16730f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md)
16740f20972fSMike Snitzer {
16750f20972fSMike Snitzer 	if (md->wq)
16760f20972fSMike Snitzer 		destroy_workqueue(md->wq);
16770f20972fSMike Snitzer 	if (md->kworker_task)
16780f20972fSMike Snitzer 		kthread_stop(md->kworker_task);
16790f20972fSMike Snitzer 	mempool_destroy(md->io_pool);
16800f20972fSMike Snitzer 	if (md->bs)
16810f20972fSMike Snitzer 		bioset_free(md->bs);
16820f20972fSMike Snitzer 
1683f26c5719SDan Williams 	if (md->dax_dev) {
1684f26c5719SDan Williams 		kill_dax(md->dax_dev);
1685f26c5719SDan Williams 		put_dax(md->dax_dev);
1686f26c5719SDan Williams 		md->dax_dev = NULL;
1687f26c5719SDan Williams 	}
1688f26c5719SDan Williams 
16890f20972fSMike Snitzer 	if (md->disk) {
16900f20972fSMike Snitzer 		spin_lock(&_minor_lock);
16910f20972fSMike Snitzer 		md->disk->private_data = NULL;
16920f20972fSMike Snitzer 		spin_unlock(&_minor_lock);
16930f20972fSMike Snitzer 		del_gendisk(md->disk);
16940f20972fSMike Snitzer 		put_disk(md->disk);
16950f20972fSMike Snitzer 	}
16960f20972fSMike Snitzer 
16970f20972fSMike Snitzer 	if (md->queue)
16980f20972fSMike Snitzer 		blk_cleanup_queue(md->queue);
16990f20972fSMike Snitzer 
1700d09960b0STahsin Erdogan 	cleanup_srcu_struct(&md->io_barrier);
1701d09960b0STahsin Erdogan 
17020f20972fSMike Snitzer 	if (md->bdev) {
17030f20972fSMike Snitzer 		bdput(md->bdev);
17040f20972fSMike Snitzer 		md->bdev = NULL;
17050f20972fSMike Snitzer 	}
17064cc96131SMike Snitzer 
17074cc96131SMike Snitzer 	dm_mq_cleanup_mapped_device(md);
17080f20972fSMike Snitzer }
17090f20972fSMike Snitzer 
17101da177e4SLinus Torvalds /*
17111da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
17121da177e4SLinus Torvalds  */
17132b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
17141da177e4SLinus Torvalds {
1715115485e8SMike Snitzer 	int r, numa_node_id = dm_get_numa_node();
1716f26c5719SDan Williams 	struct dax_device *dax_dev;
1717115485e8SMike Snitzer 	struct mapped_device *md;
1718ba61fdd1SJeff Mahoney 	void *old_md;
17191da177e4SLinus Torvalds 
1720115485e8SMike Snitzer 	md = kzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
17211da177e4SLinus Torvalds 	if (!md) {
17221da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
17231da177e4SLinus Torvalds 		return NULL;
17241da177e4SLinus Torvalds 	}
17251da177e4SLinus Torvalds 
172610da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
17276ed7ade8SMilan Broz 		goto bad_module_get;
172810da4f79SJeff Mahoney 
17291da177e4SLinus Torvalds 	/* get a minor number for the dev */
17302b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1731cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
17322b06cfffSAlasdair G Kergon 	else
1733cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
17341da177e4SLinus Torvalds 	if (r < 0)
17356ed7ade8SMilan Broz 		goto bad_minor;
17361da177e4SLinus Torvalds 
173783d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
173883d5e5b0SMikulas Patocka 	if (r < 0)
173983d5e5b0SMikulas Patocka 		goto bad_io_barrier;
174083d5e5b0SMikulas Patocka 
1741115485e8SMike Snitzer 	md->numa_node_id = numa_node_id;
17424cc96131SMike Snitzer 	md->use_blk_mq = dm_use_blk_mq_default();
1743591ddcfcSMike Snitzer 	md->init_tio_pdu = false;
1744a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
1745e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1746a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
174786f1152bSBenjamin Marzinski 	mutex_init(&md->table_devices_lock);
1748022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
17491da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
17505c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
17511da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
17527a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
17537a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
175486f1152bSBenjamin Marzinski 	INIT_LIST_HEAD(&md->table_devices);
17557a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
17561da177e4SLinus Torvalds 
1757115485e8SMike Snitzer 	md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id);
17581da177e4SLinus Torvalds 	if (!md->queue)
17590f20972fSMike Snitzer 		goto bad;
17601da177e4SLinus Torvalds 
17614a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
17629faf400fSStefan Bader 
1763115485e8SMike Snitzer 	md->disk = alloc_disk_node(1, numa_node_id);
17641da177e4SLinus Torvalds 	if (!md->disk)
17650f20972fSMike Snitzer 		goto bad;
17661da177e4SLinus Torvalds 
1767316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
1768316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
1769f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
177053d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1771f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
17722995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
17732eb6e1e3SKeith Busch 	md->kworker_task = NULL;
1774f0b04115SJeff Mahoney 
17751da177e4SLinus Torvalds 	md->disk->major = _major;
17761da177e4SLinus Torvalds 	md->disk->first_minor = minor;
17771da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
17781da177e4SLinus Torvalds 	md->disk->queue = md->queue;
17791da177e4SLinus Torvalds 	md->disk->private_data = md;
17801da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
1781f26c5719SDan Williams 
1782f26c5719SDan Williams 	dax_dev = alloc_dax(md, md->disk->disk_name, &dm_dax_ops);
1783f26c5719SDan Williams 	if (!dax_dev)
1784f26c5719SDan Williams 		goto bad;
1785f26c5719SDan Williams 	md->dax_dev = dax_dev;
1786f26c5719SDan Williams 
17871da177e4SLinus Torvalds 	add_disk(md->disk);
17887e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
17891da177e4SLinus Torvalds 
1790670368a8STejun Heo 	md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
1791304f3f6aSMilan Broz 	if (!md->wq)
17920f20972fSMike Snitzer 		goto bad;
1793304f3f6aSMilan Broz 
179432a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
179532a926daSMikulas Patocka 	if (!md->bdev)
17960f20972fSMike Snitzer 		goto bad;
179732a926daSMikulas Patocka 
17983a83f467SMing Lei 	bio_init(&md->flush_bio, NULL, 0);
1799*74d46992SChristoph Hellwig 	bio_set_dev(&md->flush_bio, md->bdev);
1800ff0361b3SJan Kara 	md->flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
18016a8736d1STejun Heo 
1802fd2ed4d2SMikulas Patocka 	dm_stats_init(&md->stats);
1803fd2ed4d2SMikulas Patocka 
1804ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1805f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1806ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1807f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1808ba61fdd1SJeff Mahoney 
1809ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1810ba61fdd1SJeff Mahoney 
18111da177e4SLinus Torvalds 	return md;
18121da177e4SLinus Torvalds 
18130f20972fSMike Snitzer bad:
18140f20972fSMike Snitzer 	cleanup_mapped_device(md);
181583d5e5b0SMikulas Patocka bad_io_barrier:
18161da177e4SLinus Torvalds 	free_minor(minor);
18176ed7ade8SMilan Broz bad_minor:
181810da4f79SJeff Mahoney 	module_put(THIS_MODULE);
18196ed7ade8SMilan Broz bad_module_get:
18201da177e4SLinus Torvalds 	kfree(md);
18211da177e4SLinus Torvalds 	return NULL;
18221da177e4SLinus Torvalds }
18231da177e4SLinus Torvalds 
1824ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1825ae9da83fSJun'ichi Nomura 
18261da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
18271da177e4SLinus Torvalds {
1828f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
182963d94e48SJun'ichi Nomura 
1830ae9da83fSJun'ichi Nomura 	unlock_fs(md);
18312eb6e1e3SKeith Busch 
18320f20972fSMike Snitzer 	cleanup_mapped_device(md);
18330f20972fSMike Snitzer 
18340f20972fSMike Snitzer 	free_table_devices(&md->table_devices);
18350f20972fSMike Snitzer 	dm_stats_cleanup(&md->stats);
183663a4f065SMike Snitzer 	free_minor(minor);
183763a4f065SMike Snitzer 
183810da4f79SJeff Mahoney 	module_put(THIS_MODULE);
18391da177e4SLinus Torvalds 	kfree(md);
18401da177e4SLinus Torvalds }
18411da177e4SLinus Torvalds 
1842e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1843e6ee8c0bSKiyoshi Ueda {
1844c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
1845e6ee8c0bSKiyoshi Ueda 
18464e6e36c3SMike Snitzer 	if (md->bs) {
18474e6e36c3SMike Snitzer 		/* The md already has necessary mempools. */
1848545ed20eSToshi Kani 		if (dm_table_bio_based(t)) {
1849c0820cf5SMikulas Patocka 			/*
185016245bdcSJun'ichi Nomura 			 * Reload bioset because front_pad may have changed
185116245bdcSJun'ichi Nomura 			 * because a different table was loaded.
1852c0820cf5SMikulas Patocka 			 */
1853c0820cf5SMikulas Patocka 			bioset_free(md->bs);
1854c0820cf5SMikulas Patocka 			md->bs = p->bs;
1855c0820cf5SMikulas Patocka 			p->bs = NULL;
1856c0820cf5SMikulas Patocka 		}
1857cbc4e3c1SMike Snitzer 		/*
18584e6e36c3SMike Snitzer 		 * There's no need to reload with request-based dm
18594e6e36c3SMike Snitzer 		 * because the size of front_pad doesn't change.
18604e6e36c3SMike Snitzer 		 * Note for future: If you are to reload bioset,
18614e6e36c3SMike Snitzer 		 * prep-ed requests in the queue may refer
18624e6e36c3SMike Snitzer 		 * to bio from the old bioset, so you must walk
18634e6e36c3SMike Snitzer 		 * through the queue to unprep.
1864cbc4e3c1SMike Snitzer 		 */
1865cbc4e3c1SMike Snitzer 		goto out;
1866cbc4e3c1SMike Snitzer 	}
1867cbc4e3c1SMike Snitzer 
1868eb8db831SChristoph Hellwig 	BUG_ON(!p || md->io_pool || md->bs);
1869e6ee8c0bSKiyoshi Ueda 
1870e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
1871e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
1872e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
1873e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
18744e6e36c3SMike Snitzer 
1875e6ee8c0bSKiyoshi Ueda out:
187602233342SMike Snitzer 	/* mempool bind completed, no longer need any mempools in the table */
1877e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
1878e6ee8c0bSKiyoshi Ueda }
1879e6ee8c0bSKiyoshi Ueda 
18801da177e4SLinus Torvalds /*
18811da177e4SLinus Torvalds  * Bind a table to the device.
18821da177e4SLinus Torvalds  */
18831da177e4SLinus Torvalds static void event_callback(void *context)
18841da177e4SLinus Torvalds {
18857a8c3d3bSMike Anderson 	unsigned long flags;
18867a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
18871da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
18881da177e4SLinus Torvalds 
18897a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
18907a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
18917a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
18927a8c3d3bSMike Anderson 
1893ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
18947a8c3d3bSMike Anderson 
18951da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
189693e6442cSMikulas Patocka 	atomic_inc(&dm_global_event_nr);
18971da177e4SLinus Torvalds 	wake_up(&md->eventq);
189893e6442cSMikulas Patocka 	wake_up(&dm_global_eventq);
18991da177e4SLinus Torvalds }
19001da177e4SLinus Torvalds 
1901c217649bSMike Snitzer /*
1902c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
1903c217649bSMike Snitzer  */
19044e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
19051da177e4SLinus Torvalds {
19061ea0654eSBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
19071ea0654eSBart Van Assche 
19084e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
19091da177e4SLinus Torvalds 
1910db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
19111da177e4SLinus Torvalds }
19121da177e4SLinus Torvalds 
1913042d2a9bSAlasdair G Kergon /*
1914042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
1915042d2a9bSAlasdair G Kergon  */
1916042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
1917754c5fc7SMike Snitzer 			       struct queue_limits *limits)
19181da177e4SLinus Torvalds {
1919042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
1920165125e1SJens Axboe 	struct request_queue *q = md->queue;
19211da177e4SLinus Torvalds 	sector_t size;
19221da177e4SLinus Torvalds 
19235a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
19245a8f1f80SBart Van Assche 
19251da177e4SLinus Torvalds 	size = dm_table_get_size(t);
19263ac51e74SDarrick J. Wong 
19273ac51e74SDarrick J. Wong 	/*
19283ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
19293ac51e74SDarrick J. Wong 	 */
1930fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
19313ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
19323ac51e74SDarrick J. Wong 
19334e90188bSAlasdair G Kergon 	__set_size(md, size);
19341da177e4SLinus Torvalds 
1935cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
19362ca3310eSAlasdair G Kergon 
1937e6ee8c0bSKiyoshi Ueda 	/*
1938e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
1939e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
1940e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
1941e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
1942e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
1943e6ee8c0bSKiyoshi Ueda 	 */
194416f12266SMike Snitzer 	if (dm_table_request_based(t)) {
1945eca7ee6dSMike Snitzer 		dm_stop_queue(q);
194616f12266SMike Snitzer 		/*
194716f12266SMike Snitzer 		 * Leverage the fact that request-based DM targets are
194816f12266SMike Snitzer 		 * immutable singletons and establish md->immutable_target
194916f12266SMike Snitzer 		 * - used to optimize both dm_request_fn and dm_mq_queue_rq
195016f12266SMike Snitzer 		 */
195116f12266SMike Snitzer 		md->immutable_target = dm_table_get_immutable_target(t);
195216f12266SMike Snitzer 	}
1953e6ee8c0bSKiyoshi Ueda 
1954e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
1955e6ee8c0bSKiyoshi Ueda 
1956a12f5d48SEric Dumazet 	old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
19571d3aa6f6SMike Snitzer 	rcu_assign_pointer(md->map, (void *)t);
195836a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
195936a0456fSAlasdair G Kergon 
1960754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
196141abc4e1SHannes Reinecke 	if (old_map)
196283d5e5b0SMikulas Patocka 		dm_sync_table(md);
19632ca3310eSAlasdair G Kergon 
1964042d2a9bSAlasdair G Kergon 	return old_map;
19651da177e4SLinus Torvalds }
19661da177e4SLinus Torvalds 
1967a7940155SAlasdair G Kergon /*
1968a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
1969a7940155SAlasdair G Kergon  */
1970a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
19711da177e4SLinus Torvalds {
1972a12f5d48SEric Dumazet 	struct dm_table *map = rcu_dereference_protected(md->map, 1);
19731da177e4SLinus Torvalds 
19741da177e4SLinus Torvalds 	if (!map)
1975a7940155SAlasdair G Kergon 		return NULL;
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
19789cdb8520SMonam Agarwal 	RCU_INIT_POINTER(md->map, NULL);
197983d5e5b0SMikulas Patocka 	dm_sync_table(md);
1980a7940155SAlasdair G Kergon 
1981a7940155SAlasdair G Kergon 	return map;
19821da177e4SLinus Torvalds }
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds /*
19851da177e4SLinus Torvalds  * Constructor for a new device.
19861da177e4SLinus Torvalds  */
19872b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
19881da177e4SLinus Torvalds {
19891da177e4SLinus Torvalds 	struct mapped_device *md;
19901da177e4SLinus Torvalds 
19912b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
19921da177e4SLinus Torvalds 	if (!md)
19931da177e4SLinus Torvalds 		return -ENXIO;
19941da177e4SLinus Torvalds 
1995784aae73SMilan Broz 	dm_sysfs_init(md);
1996784aae73SMilan Broz 
19971da177e4SLinus Torvalds 	*result = md;
19981da177e4SLinus Torvalds 	return 0;
19991da177e4SLinus Torvalds }
20001da177e4SLinus Torvalds 
2001a5664dadSMike Snitzer /*
2002a5664dadSMike Snitzer  * Functions to manage md->type.
2003a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2004a5664dadSMike Snitzer  */
2005a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2006a5664dadSMike Snitzer {
2007a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2008a5664dadSMike Snitzer }
2009a5664dadSMike Snitzer 
2010a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2011a5664dadSMike Snitzer {
2012a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2013a5664dadSMike Snitzer }
2014a5664dadSMike Snitzer 
20157e0d574fSBart Van Assche void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
2016a5664dadSMike Snitzer {
201700c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2018a5664dadSMike Snitzer 	md->type = type;
2019a5664dadSMike Snitzer }
2020a5664dadSMike Snitzer 
20217e0d574fSBart Van Assche enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
2022a5664dadSMike Snitzer {
2023a5664dadSMike Snitzer 	return md->type;
2024a5664dadSMike Snitzer }
2025a5664dadSMike Snitzer 
202636a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
202736a0456fSAlasdair G Kergon {
202836a0456fSAlasdair G Kergon 	return md->immutable_target_type;
202936a0456fSAlasdair G Kergon }
203036a0456fSAlasdair G Kergon 
20314a0b4ddfSMike Snitzer /*
2032f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
2033f84cb8a4SMike Snitzer  * count on 'md'.
2034f84cb8a4SMike Snitzer  */
2035f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2036f84cb8a4SMike Snitzer {
2037f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
2038f84cb8a4SMike Snitzer 	return &md->queue->limits;
2039f84cb8a4SMike Snitzer }
2040f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2041f84cb8a4SMike Snitzer 
20424a0b4ddfSMike Snitzer /*
20434a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
20444a0b4ddfSMike Snitzer  */
2045591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
20464a0b4ddfSMike Snitzer {
2047bfebd1cdSMike Snitzer 	int r;
20487e0d574fSBart Van Assche 	enum dm_queue_mode type = dm_get_md_type(md);
2049bfebd1cdSMike Snitzer 
2050545ed20eSToshi Kani 	switch (type) {
2051bfebd1cdSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
2052eb8db831SChristoph Hellwig 		r = dm_old_init_request_queue(md, t);
2053bfebd1cdSMike Snitzer 		if (r) {
2054eca7ee6dSMike Snitzer 			DMERR("Cannot initialize queue for request-based mapped device");
2055bfebd1cdSMike Snitzer 			return r;
20564a0b4ddfSMike Snitzer 		}
2057bfebd1cdSMike Snitzer 		break;
2058bfebd1cdSMike Snitzer 	case DM_TYPE_MQ_REQUEST_BASED:
2059e83068a5SMike Snitzer 		r = dm_mq_init_request_queue(md, t);
2060bfebd1cdSMike Snitzer 		if (r) {
2061eca7ee6dSMike Snitzer 			DMERR("Cannot initialize queue for request-based dm-mq mapped device");
2062bfebd1cdSMike Snitzer 			return r;
2063bfebd1cdSMike Snitzer 		}
2064bfebd1cdSMike Snitzer 		break;
2065bfebd1cdSMike Snitzer 	case DM_TYPE_BIO_BASED:
2066545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
2067eca7ee6dSMike Snitzer 		dm_init_normal_md_queue(md);
2068ff36ab34SMike Snitzer 		blk_queue_make_request(md->queue, dm_make_request);
2069dbba42d8SMikulas Patocka 		/*
2070dbba42d8SMikulas Patocka 		 * DM handles splitting bios as needed.  Free the bio_split bioset
2071dbba42d8SMikulas Patocka 		 * since it won't be used (saves 1 process per bio-based DM device).
2072dbba42d8SMikulas Patocka 		 */
2073dbba42d8SMikulas Patocka 		bioset_free(md->queue->bio_split);
2074dbba42d8SMikulas Patocka 		md->queue->bio_split = NULL;
2075545ed20eSToshi Kani 
2076545ed20eSToshi Kani 		if (type == DM_TYPE_DAX_BIO_BASED)
2077545ed20eSToshi Kani 			queue_flag_set_unlocked(QUEUE_FLAG_DAX, md->queue);
2078bfebd1cdSMike Snitzer 		break;
20797e0d574fSBart Van Assche 	case DM_TYPE_NONE:
20807e0d574fSBart Van Assche 		WARN_ON_ONCE(true);
20817e0d574fSBart Van Assche 		break;
2082ff36ab34SMike Snitzer 	}
20834a0b4ddfSMike Snitzer 
20844a0b4ddfSMike Snitzer 	return 0;
20854a0b4ddfSMike Snitzer }
20864a0b4ddfSMike Snitzer 
20872bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev)
20881da177e4SLinus Torvalds {
20891da177e4SLinus Torvalds 	struct mapped_device *md;
20901da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
20911da177e4SLinus Torvalds 
20921da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
20931da177e4SLinus Torvalds 		return NULL;
20941da177e4SLinus Torvalds 
2095f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
20961da177e4SLinus Torvalds 
20971da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
20982bec1f4aSMikulas Patocka 	if (md) {
20992bec1f4aSMikulas Patocka 		if ((md == MINOR_ALLOCED ||
2100f331c029STejun Heo 		     (MINOR(disk_devt(dm_disk(md))) != minor) ||
2101abdc568bSKiyoshi Ueda 		     dm_deleting_md(md) ||
2102fba9f90eSJeff Mahoney 		     test_bit(DMF_FREEING, &md->flags))) {
2103637842cfSDavid Teigland 			md = NULL;
2104fba9f90eSJeff Mahoney 			goto out;
2105fba9f90eSJeff Mahoney 		}
21062bec1f4aSMikulas Patocka 		dm_get(md);
21072bec1f4aSMikulas Patocka 	}
21081da177e4SLinus Torvalds 
2109fba9f90eSJeff Mahoney out:
2110f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
21111da177e4SLinus Torvalds 
2112637842cfSDavid Teigland 	return md;
2113637842cfSDavid Teigland }
21143cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2115d229a958SDavid Teigland 
21169ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2117637842cfSDavid Teigland {
21189ade92a9SAlasdair G Kergon 	return md->interface_ptr;
21191da177e4SLinus Torvalds }
21201da177e4SLinus Torvalds 
21211da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
21221da177e4SLinus Torvalds {
21231da177e4SLinus Torvalds 	md->interface_ptr = ptr;
21241da177e4SLinus Torvalds }
21251da177e4SLinus Torvalds 
21261da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
21271da177e4SLinus Torvalds {
21281da177e4SLinus Torvalds 	atomic_inc(&md->holders);
21293f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
21301da177e4SLinus Torvalds }
21311da177e4SLinus Torvalds 
213209ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md)
213309ee96b2SMikulas Patocka {
213409ee96b2SMikulas Patocka 	spin_lock(&_minor_lock);
213509ee96b2SMikulas Patocka 	if (test_bit(DMF_FREEING, &md->flags)) {
213609ee96b2SMikulas Patocka 		spin_unlock(&_minor_lock);
213709ee96b2SMikulas Patocka 		return -EBUSY;
213809ee96b2SMikulas Patocka 	}
213909ee96b2SMikulas Patocka 	dm_get(md);
214009ee96b2SMikulas Patocka 	spin_unlock(&_minor_lock);
214109ee96b2SMikulas Patocka 	return 0;
214209ee96b2SMikulas Patocka }
214309ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold);
214409ee96b2SMikulas Patocka 
214572d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
214672d94861SAlasdair G Kergon {
214772d94861SAlasdair G Kergon 	return md->name;
214872d94861SAlasdair G Kergon }
214972d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
215072d94861SAlasdair G Kergon 
21513f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
21521da177e4SLinus Torvalds {
21533b785fbcSBart Van Assche 	struct request_queue *q = dm_get_md_queue(md);
21541134e5aeSMike Anderson 	struct dm_table *map;
215583d5e5b0SMikulas Patocka 	int srcu_idx;
21561da177e4SLinus Torvalds 
21573f77316dSKiyoshi Ueda 	might_sleep();
2158fba9f90eSJeff Mahoney 
215963a4f065SMike Snitzer 	spin_lock(&_minor_lock);
21603f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2161fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2162f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
21633f77316dSKiyoshi Ueda 
21642e91c369SBart Van Assche 	blk_set_queue_dying(q);
21653b785fbcSBart Van Assche 
216602233342SMike Snitzer 	if (dm_request_based(md) && md->kworker_task)
21673989144fSPetr Mladek 		kthread_flush_worker(&md->kworker);
21682eb6e1e3SKeith Busch 
2169ab7c7bb6SMikulas Patocka 	/*
2170ab7c7bb6SMikulas Patocka 	 * Take suspend_lock so that presuspend and postsuspend methods
2171ab7c7bb6SMikulas Patocka 	 * do not race with internal suspend.
2172ab7c7bb6SMikulas Patocka 	 */
2173ab7c7bb6SMikulas Patocka 	mutex_lock(&md->suspend_lock);
21742a708cffSJunichi Nomura 	map = dm_get_live_table(md, &srcu_idx);
21754f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
21761da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
21771da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
21781da177e4SLinus Torvalds 	}
217983d5e5b0SMikulas Patocka 	/* dm_put_live_table must be before msleep, otherwise deadlock is possible */
218083d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
21812a708cffSJunichi Nomura 	mutex_unlock(&md->suspend_lock);
218283d5e5b0SMikulas Patocka 
21833f77316dSKiyoshi Ueda 	/*
21843f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
21853f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
21863f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
21873f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
21883f77316dSKiyoshi Ueda 	 */
21893f77316dSKiyoshi Ueda 	if (wait)
21903f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
21913f77316dSKiyoshi Ueda 			msleep(1);
21923f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
21933f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
21943f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
21953f77316dSKiyoshi Ueda 
2196784aae73SMilan Broz 	dm_sysfs_exit(md);
2197a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
21981da177e4SLinus Torvalds 	free_dev(md);
21991da177e4SLinus Torvalds }
22003f77316dSKiyoshi Ueda 
22013f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
22023f77316dSKiyoshi Ueda {
22033f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
22043f77316dSKiyoshi Ueda }
22053f77316dSKiyoshi Ueda 
22063f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
22073f77316dSKiyoshi Ueda {
22083f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
22093f77316dSKiyoshi Ueda }
22103f77316dSKiyoshi Ueda 
22113f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
22123f77316dSKiyoshi Ueda {
22133f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
22141da177e4SLinus Torvalds }
221579eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
22161da177e4SLinus Torvalds 
2217b48633f8SBart Van Assche static int dm_wait_for_completion(struct mapped_device *md, long task_state)
221846125c1cSMilan Broz {
221946125c1cSMilan Broz 	int r = 0;
22209f4c3f87SBart Van Assche 	DEFINE_WAIT(wait);
222146125c1cSMilan Broz 
222246125c1cSMilan Broz 	while (1) {
22239f4c3f87SBart Van Assche 		prepare_to_wait(&md->wait, &wait, task_state);
222446125c1cSMilan Broz 
2225b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
222646125c1cSMilan Broz 			break;
222746125c1cSMilan Broz 
2228e3fabdfdSBart Van Assche 		if (signal_pending_state(task_state, current)) {
222946125c1cSMilan Broz 			r = -EINTR;
223046125c1cSMilan Broz 			break;
223146125c1cSMilan Broz 		}
223246125c1cSMilan Broz 
223346125c1cSMilan Broz 		io_schedule();
223446125c1cSMilan Broz 	}
22359f4c3f87SBart Van Assche 	finish_wait(&md->wait, &wait);
2236b44ebeb0SMikulas Patocka 
223746125c1cSMilan Broz 	return r;
223846125c1cSMilan Broz }
223946125c1cSMilan Broz 
22401da177e4SLinus Torvalds /*
22411da177e4SLinus Torvalds  * Process the deferred bios
22421da177e4SLinus Torvalds  */
2243ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
22441da177e4SLinus Torvalds {
2245ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2246ef208587SMikulas Patocka 						work);
22476d6f10dfSMilan Broz 	struct bio *c;
224883d5e5b0SMikulas Patocka 	int srcu_idx;
224983d5e5b0SMikulas Patocka 	struct dm_table *map;
22501da177e4SLinus Torvalds 
225183d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
2252ef208587SMikulas Patocka 
22533b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2254022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2255022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2256022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2257022c2611SMikulas Patocka 
22586a8736d1STejun Heo 		if (!c)
2259df12ee99SAlasdair G Kergon 			break;
226073d410c0SMilan Broz 
2261e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2262e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2263af7e466aSMikulas Patocka 		else
226483d5e5b0SMikulas Patocka 			__split_and_process_bio(md, map, c);
2265e6ee8c0bSKiyoshi Ueda 	}
22663b00b203SMikulas Patocka 
226783d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
22681da177e4SLinus Torvalds }
22691da177e4SLinus Torvalds 
22709a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2271304f3f6aSMilan Broz {
22723b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
22734e857c58SPeter Zijlstra 	smp_mb__after_atomic();
227453d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2275304f3f6aSMilan Broz }
2276304f3f6aSMilan Broz 
22771da177e4SLinus Torvalds /*
2278042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
22791da177e4SLinus Torvalds  */
2280042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
22811da177e4SLinus Torvalds {
228287eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2283754c5fc7SMike Snitzer 	struct queue_limits limits;
2284042d2a9bSAlasdair G Kergon 	int r;
22851da177e4SLinus Torvalds 
2286e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
22871da177e4SLinus Torvalds 
22881da177e4SLinus Torvalds 	/* device must be suspended */
22894f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
229093c534aeSAlasdair G Kergon 		goto out;
22911da177e4SLinus Torvalds 
22923ae70656SMike Snitzer 	/*
22933ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
22943ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
22953ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
22963ae70656SMike Snitzer 	 * reappear.
22973ae70656SMike Snitzer 	 */
22983ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
229983d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
23003ae70656SMike Snitzer 		if (live_map)
23013ae70656SMike Snitzer 			limits = md->queue->limits;
230283d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
23033ae70656SMike Snitzer 	}
23043ae70656SMike Snitzer 
230587eb5b21SMike Christie 	if (!live_map) {
2306754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2307042d2a9bSAlasdair G Kergon 		if (r) {
2308042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2309754c5fc7SMike Snitzer 			goto out;
2310042d2a9bSAlasdair G Kergon 		}
231187eb5b21SMike Christie 	}
2312754c5fc7SMike Snitzer 
2313042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
23141da177e4SLinus Torvalds 
231593c534aeSAlasdair G Kergon out:
2316e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2317042d2a9bSAlasdair G Kergon 	return map;
23181da177e4SLinus Torvalds }
23191da177e4SLinus Torvalds 
23201da177e4SLinus Torvalds /*
23211da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
23221da177e4SLinus Torvalds  * device.
23231da177e4SLinus Torvalds  */
23242ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
23251da177e4SLinus Torvalds {
2326e39e2e95SAlasdair G Kergon 	int r;
23271da177e4SLinus Torvalds 
23281da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2329dfbe03f6SAlasdair G Kergon 
2330db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2331dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2332cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2333e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2334e39e2e95SAlasdair G Kergon 		return r;
2335dfbe03f6SAlasdair G Kergon 	}
2336dfbe03f6SAlasdair G Kergon 
2337aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2338aa8d7c2fSAlasdair G Kergon 
23391da177e4SLinus Torvalds 	return 0;
23401da177e4SLinus Torvalds }
23411da177e4SLinus Torvalds 
23422ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
23431da177e4SLinus Torvalds {
2344aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2345aa8d7c2fSAlasdair G Kergon 		return;
2346aa8d7c2fSAlasdair G Kergon 
2347db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
23481da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2349aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
23501da177e4SLinus Torvalds }
23511da177e4SLinus Torvalds 
23521da177e4SLinus Torvalds /*
2353b48633f8SBart Van Assche  * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
2354b48633f8SBart Van Assche  * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
2355b48633f8SBart Van Assche  * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
2356b48633f8SBart Van Assche  *
2357ffcc3936SMike Snitzer  * If __dm_suspend returns 0, the device is completely quiescent
2358ffcc3936SMike Snitzer  * now. There is no request-processing activity. All new requests
2359ffcc3936SMike Snitzer  * are being added to md->deferred list.
2360cec47e3dSKiyoshi Ueda  */
2361ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
2362b48633f8SBart Van Assche 			unsigned suspend_flags, long task_state,
2363eaf9a736SMike Snitzer 			int dmf_suspended_flag)
23641da177e4SLinus Torvalds {
2365ffcc3936SMike Snitzer 	bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2366ffcc3936SMike Snitzer 	bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2367ffcc3936SMike Snitzer 	int r;
2368cf222b37SAlasdair G Kergon 
23695a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
23705a8f1f80SBart Van Assche 
23712e93ccc1SKiyoshi Ueda 	/*
23722e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
23732e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
23742e93ccc1SKiyoshi Ueda 	 */
23752e93ccc1SKiyoshi Ueda 	if (noflush)
23762e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
237786331f39SBart Van Assche 	else
237886331f39SBart Van Assche 		pr_debug("%s: suspending with flush\n", dm_device_name(md));
23792e93ccc1SKiyoshi Ueda 
2380d67ee213SMike Snitzer 	/*
2381d67ee213SMike Snitzer 	 * This gets reverted if there's an error later and the targets
2382d67ee213SMike Snitzer 	 * provide the .presuspend_undo hook.
2383d67ee213SMike Snitzer 	 */
23841da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
23851da177e4SLinus Torvalds 
23862e93ccc1SKiyoshi Ueda 	/*
23879f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
23889f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
23899f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
23909f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
23912e93ccc1SKiyoshi Ueda 	 */
239232a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
23932ca3310eSAlasdair G Kergon 		r = lock_fs(md);
2394d67ee213SMike Snitzer 		if (r) {
2395d67ee213SMike Snitzer 			dm_table_presuspend_undo_targets(map);
2396ffcc3936SMike Snitzer 			return r;
2397aa8d7c2fSAlasdair G Kergon 		}
2398d67ee213SMike Snitzer 	}
23991da177e4SLinus Torvalds 
24001da177e4SLinus Torvalds 	/*
24013b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
24023b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
24033b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
24043b00b203SMikulas Patocka 	 * dm_wq_work.
24053b00b203SMikulas Patocka 	 *
24063b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
24073b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
24086a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
24096a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
24106a8736d1STejun Heo 	 * flush_workqueue(md->wq).
24111da177e4SLinus Torvalds 	 */
24121eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
241341abc4e1SHannes Reinecke 	if (map)
241483d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
24151da177e4SLinus Torvalds 
2416d0bcb878SKiyoshi Ueda 	/*
241729e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
241829e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2419d0bcb878SKiyoshi Ueda 	 */
24202eb6e1e3SKeith Busch 	if (dm_request_based(md)) {
2421eca7ee6dSMike Snitzer 		dm_stop_queue(md->queue);
242202233342SMike Snitzer 		if (md->kworker_task)
24233989144fSPetr Mladek 			kthread_flush_worker(&md->kworker);
24242eb6e1e3SKeith Busch 	}
2425cec47e3dSKiyoshi Ueda 
2426d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2427d0bcb878SKiyoshi Ueda 
24281da177e4SLinus Torvalds 	/*
24293b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
24303b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
24313b00b203SMikulas Patocka 	 * to finish.
24321da177e4SLinus Torvalds 	 */
2433b48633f8SBart Van Assche 	r = dm_wait_for_completion(md, task_state);
2434eaf9a736SMike Snitzer 	if (!r)
2435eaf9a736SMike Snitzer 		set_bit(dmf_suspended_flag, &md->flags);
24361da177e4SLinus Torvalds 
24376d6f10dfSMilan Broz 	if (noflush)
2438022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
243941abc4e1SHannes Reinecke 	if (map)
244083d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
24412e93ccc1SKiyoshi Ueda 
24421da177e4SLinus Torvalds 	/* were we interrupted ? */
244346125c1cSMilan Broz 	if (r < 0) {
24449a1fb464SMikulas Patocka 		dm_queue_flush(md);
244573d410c0SMilan Broz 
2446cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
2447eca7ee6dSMike Snitzer 			dm_start_queue(md->queue);
2448cec47e3dSKiyoshi Ueda 
24492ca3310eSAlasdair G Kergon 		unlock_fs(md);
2450d67ee213SMike Snitzer 		dm_table_presuspend_undo_targets(map);
2451ffcc3936SMike Snitzer 		/* pushback list is already flushed, so skip flush */
2452ffcc3936SMike Snitzer 	}
2453ffcc3936SMike Snitzer 
2454ffcc3936SMike Snitzer 	return r;
24552ca3310eSAlasdair G Kergon }
24562ca3310eSAlasdair G Kergon 
24573b00b203SMikulas Patocka /*
2458ffcc3936SMike Snitzer  * We need to be able to change a mapping table under a mounted
2459ffcc3936SMike Snitzer  * filesystem.  For example we might want to move some data in
2460ffcc3936SMike Snitzer  * the background.  Before the table can be swapped with
2461ffcc3936SMike Snitzer  * dm_bind_table, dm_suspend must be called to flush any in
2462ffcc3936SMike Snitzer  * flight bios and ensure that any further io gets deferred.
24633b00b203SMikulas Patocka  */
2464ffcc3936SMike Snitzer /*
2465ffcc3936SMike Snitzer  * Suspend mechanism in request-based dm.
2466ffcc3936SMike Snitzer  *
2467ffcc3936SMike Snitzer  * 1. Flush all I/Os by lock_fs() if needed.
2468ffcc3936SMike Snitzer  * 2. Stop dispatching any I/O by stopping the request_queue.
2469ffcc3936SMike Snitzer  * 3. Wait for all in-flight I/Os to be completed or requeued.
2470ffcc3936SMike Snitzer  *
2471ffcc3936SMike Snitzer  * To abort suspend, start the request_queue.
2472ffcc3936SMike Snitzer  */
2473ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2474ffcc3936SMike Snitzer {
2475ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2476ffcc3936SMike Snitzer 	int r = 0;
2477ffcc3936SMike Snitzer 
2478ffcc3936SMike Snitzer retry:
2479ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2480ffcc3936SMike Snitzer 
2481ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2482ffcc3936SMike Snitzer 		r = -EINVAL;
2483ffcc3936SMike Snitzer 		goto out_unlock;
2484ffcc3936SMike Snitzer 	}
2485ffcc3936SMike Snitzer 
2486ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2487ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2488ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2489ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2490ffcc3936SMike Snitzer 		if (r)
2491ffcc3936SMike Snitzer 			return r;
2492ffcc3936SMike Snitzer 		goto retry;
2493ffcc3936SMike Snitzer 	}
2494ffcc3936SMike Snitzer 
2495a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2496ffcc3936SMike Snitzer 
2497eaf9a736SMike Snitzer 	r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
2498ffcc3936SMike Snitzer 	if (r)
2499ffcc3936SMike Snitzer 		goto out_unlock;
25003b00b203SMikulas Patocka 
25014d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
25024d4471cbSKiyoshi Ueda 
2503d287483dSAlasdair G Kergon out_unlock:
2504e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2505cf222b37SAlasdair G Kergon 	return r;
25061da177e4SLinus Torvalds }
25071da177e4SLinus Torvalds 
2508ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map)
25091da177e4SLinus Torvalds {
2510ffcc3936SMike Snitzer 	if (map) {
2511ffcc3936SMike Snitzer 		int r = dm_table_resume_targets(map);
25128757b776SMilan Broz 		if (r)
2513ffcc3936SMike Snitzer 			return r;
2514ffcc3936SMike Snitzer 	}
25152ca3310eSAlasdair G Kergon 
25169a1fb464SMikulas Patocka 	dm_queue_flush(md);
25172ca3310eSAlasdair G Kergon 
2518cec47e3dSKiyoshi Ueda 	/*
2519cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2520cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2521cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2522cec47e3dSKiyoshi Ueda 	 */
2523cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2524eca7ee6dSMike Snitzer 		dm_start_queue(md->queue);
2525cec47e3dSKiyoshi Ueda 
25262ca3310eSAlasdair G Kergon 	unlock_fs(md);
25272ca3310eSAlasdair G Kergon 
2528ffcc3936SMike Snitzer 	return 0;
2529ffcc3936SMike Snitzer }
2530ffcc3936SMike Snitzer 
2531ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md)
2532ffcc3936SMike Snitzer {
25338dc23658SMinfei Huang 	int r;
2534ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2535ffcc3936SMike Snitzer 
2536ffcc3936SMike Snitzer retry:
25378dc23658SMinfei Huang 	r = -EINVAL;
2538ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2539ffcc3936SMike Snitzer 
2540ffcc3936SMike Snitzer 	if (!dm_suspended_md(md))
2541ffcc3936SMike Snitzer 		goto out;
2542ffcc3936SMike Snitzer 
2543ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2544ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2545ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2546ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2547ffcc3936SMike Snitzer 		if (r)
2548ffcc3936SMike Snitzer 			return r;
2549ffcc3936SMike Snitzer 		goto retry;
2550ffcc3936SMike Snitzer 	}
2551ffcc3936SMike Snitzer 
2552a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2553ffcc3936SMike Snitzer 	if (!map || !dm_table_get_size(map))
2554ffcc3936SMike Snitzer 		goto out;
2555ffcc3936SMike Snitzer 
2556ffcc3936SMike Snitzer 	r = __dm_resume(md, map);
2557ffcc3936SMike Snitzer 	if (r)
2558ffcc3936SMike Snitzer 		goto out;
2559ffcc3936SMike Snitzer 
25602ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
2561cf222b37SAlasdair G Kergon out:
2562e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
25632ca3310eSAlasdair G Kergon 
2564cf222b37SAlasdair G Kergon 	return r;
25651da177e4SLinus Torvalds }
25661da177e4SLinus Torvalds 
2567fd2ed4d2SMikulas Patocka /*
2568fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
2569fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
2570fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
2571fd2ed4d2SMikulas Patocka  */
2572fd2ed4d2SMikulas Patocka 
2573ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
2574ffcc3936SMike Snitzer {
2575ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2576ffcc3936SMike Snitzer 
25771ea0654eSBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
25781ea0654eSBart Van Assche 
257996b26c8cSMikulas Patocka 	if (md->internal_suspend_count++)
2580ffcc3936SMike Snitzer 		return; /* nested internal suspend */
2581ffcc3936SMike Snitzer 
2582ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2583ffcc3936SMike Snitzer 		set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2584ffcc3936SMike Snitzer 		return; /* nest suspend */
2585ffcc3936SMike Snitzer 	}
2586ffcc3936SMike Snitzer 
2587a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2588ffcc3936SMike Snitzer 
2589ffcc3936SMike Snitzer 	/*
2590ffcc3936SMike Snitzer 	 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2591ffcc3936SMike Snitzer 	 * supported.  Properly supporting a TASK_INTERRUPTIBLE internal suspend
2592ffcc3936SMike Snitzer 	 * would require changing .presuspend to return an error -- avoid this
2593ffcc3936SMike Snitzer 	 * until there is a need for more elaborate variants of internal suspend.
2594ffcc3936SMike Snitzer 	 */
2595eaf9a736SMike Snitzer 	(void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
2596eaf9a736SMike Snitzer 			    DMF_SUSPENDED_INTERNALLY);
2597ffcc3936SMike Snitzer 
2598ffcc3936SMike Snitzer 	dm_table_postsuspend_targets(map);
2599ffcc3936SMike Snitzer }
2600ffcc3936SMike Snitzer 
2601ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md)
2602ffcc3936SMike Snitzer {
260396b26c8cSMikulas Patocka 	BUG_ON(!md->internal_suspend_count);
260496b26c8cSMikulas Patocka 
260596b26c8cSMikulas Patocka 	if (--md->internal_suspend_count)
2606ffcc3936SMike Snitzer 		return; /* resume from nested internal suspend */
2607ffcc3936SMike Snitzer 
2608ffcc3936SMike Snitzer 	if (dm_suspended_md(md))
2609ffcc3936SMike Snitzer 		goto done; /* resume from nested suspend */
2610ffcc3936SMike Snitzer 
2611ffcc3936SMike Snitzer 	/*
2612ffcc3936SMike Snitzer 	 * NOTE: existing callers don't need to call dm_table_resume_targets
2613ffcc3936SMike Snitzer 	 * (which may fail -- so best to avoid it for now by passing NULL map)
2614ffcc3936SMike Snitzer 	 */
2615ffcc3936SMike Snitzer 	(void) __dm_resume(md, NULL);
2616ffcc3936SMike Snitzer 
2617ffcc3936SMike Snitzer done:
2618ffcc3936SMike Snitzer 	clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2619ffcc3936SMike Snitzer 	smp_mb__after_atomic();
2620ffcc3936SMike Snitzer 	wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2621ffcc3936SMike Snitzer }
2622ffcc3936SMike Snitzer 
2623ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md)
2624fd2ed4d2SMikulas Patocka {
2625fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
2626ffcc3936SMike Snitzer 	__dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2627ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2628ffcc3936SMike Snitzer }
2629ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
2630ffcc3936SMike Snitzer 
2631ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md)
2632ffcc3936SMike Snitzer {
2633ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2634ffcc3936SMike Snitzer 	__dm_internal_resume(md);
2635ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2636ffcc3936SMike Snitzer }
2637ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume);
2638ffcc3936SMike Snitzer 
2639ffcc3936SMike Snitzer /*
2640ffcc3936SMike Snitzer  * Fast variants of internal suspend/resume hold md->suspend_lock,
2641ffcc3936SMike Snitzer  * which prevents interaction with userspace-driven suspend.
2642ffcc3936SMike Snitzer  */
2643ffcc3936SMike Snitzer 
2644ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md)
2645ffcc3936SMike Snitzer {
2646ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2647ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2648fd2ed4d2SMikulas Patocka 		return;
2649fd2ed4d2SMikulas Patocka 
2650fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2651fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
2652fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
2653fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2654fd2ed4d2SMikulas Patocka }
2655b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
2656fd2ed4d2SMikulas Patocka 
2657ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md)
2658fd2ed4d2SMikulas Patocka {
2659ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2660fd2ed4d2SMikulas Patocka 		goto done;
2661fd2ed4d2SMikulas Patocka 
2662fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
2663fd2ed4d2SMikulas Patocka 
2664fd2ed4d2SMikulas Patocka done:
2665fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
2666fd2ed4d2SMikulas Patocka }
2667b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
2668fd2ed4d2SMikulas Patocka 
26691da177e4SLinus Torvalds /*-----------------------------------------------------------------
26701da177e4SLinus Torvalds  * Event notification.
26711da177e4SLinus Torvalds  *---------------------------------------------------------------*/
26723abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
267360935eb2SMilan Broz 		       unsigned cookie)
267469267a30SAlasdair G Kergon {
267560935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
267660935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
267760935eb2SMilan Broz 
267860935eb2SMilan Broz 	if (!cookie)
26793abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
268060935eb2SMilan Broz 	else {
268160935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
268260935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
26833abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
26843abf85b5SPeter Rajnoha 					  action, envp);
268560935eb2SMilan Broz 	}
268669267a30SAlasdair G Kergon }
268769267a30SAlasdair G Kergon 
26887a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
26897a8c3d3bSMike Anderson {
26907a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
26917a8c3d3bSMike Anderson }
26927a8c3d3bSMike Anderson 
26931da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
26941da177e4SLinus Torvalds {
26951da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
26961da177e4SLinus Torvalds }
26971da177e4SLinus Torvalds 
26981da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
26991da177e4SLinus Torvalds {
27001da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
27011da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
27021da177e4SLinus Torvalds }
27031da177e4SLinus Torvalds 
27047a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
27057a8c3d3bSMike Anderson {
27067a8c3d3bSMike Anderson 	unsigned long flags;
27077a8c3d3bSMike Anderson 
27087a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
27097a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
27107a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
27117a8c3d3bSMike Anderson }
27127a8c3d3bSMike Anderson 
27131da177e4SLinus Torvalds /*
27141da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
27151da177e4SLinus Torvalds  * count on 'md'.
27161da177e4SLinus Torvalds  */
27171da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
27181da177e4SLinus Torvalds {
27191da177e4SLinus Torvalds 	return md->disk;
27201da177e4SLinus Torvalds }
272165ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk);
27221da177e4SLinus Torvalds 
2723784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2724784aae73SMilan Broz {
27252995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
2726784aae73SMilan Broz }
2727784aae73SMilan Broz 
2728784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2729784aae73SMilan Broz {
2730784aae73SMilan Broz 	struct mapped_device *md;
2731784aae73SMilan Broz 
27322995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
2733784aae73SMilan Broz 
27344d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
2735432a212cSMike Anderson 	    dm_deleting_md(md))
27364d89b7b4SMilan Broz 		return NULL;
27374d89b7b4SMilan Broz 
2738784aae73SMilan Broz 	dm_get(md);
2739784aae73SMilan Broz 	return md;
2740784aae73SMilan Broz }
2741784aae73SMilan Broz 
27424f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
27431da177e4SLinus Torvalds {
27441da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
27451da177e4SLinus Torvalds }
27461da177e4SLinus Torvalds 
2747ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md)
2748ffcc3936SMike Snitzer {
2749ffcc3936SMike Snitzer 	return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2750ffcc3936SMike Snitzer }
2751ffcc3936SMike Snitzer 
27522c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
27532c140a24SMikulas Patocka {
27542c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
27552c140a24SMikulas Patocka }
27562c140a24SMikulas Patocka 
275764dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
275864dbce58SKiyoshi Ueda {
2759ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
276064dbce58SKiyoshi Ueda }
276164dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
276264dbce58SKiyoshi Ueda 
27632e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
27642e93ccc1SKiyoshi Ueda {
2765ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
27662e93ccc1SKiyoshi Ueda }
27672e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
27682e93ccc1SKiyoshi Ueda 
27697e0d574fSBart Van Assche struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type,
277030187e1dSMike Snitzer 					    unsigned integrity, unsigned per_io_data_size)
2771e6ee8c0bSKiyoshi Ueda {
2772115485e8SMike Snitzer 	struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
277378d8e58aSMike Snitzer 	unsigned int pool_size = 0;
27745f015204SJun'ichi Nomura 	unsigned int front_pad;
2775e6ee8c0bSKiyoshi Ueda 
2776e6ee8c0bSKiyoshi Ueda 	if (!pools)
27774e6e36c3SMike Snitzer 		return NULL;
2778e6ee8c0bSKiyoshi Ueda 
277978d8e58aSMike Snitzer 	switch (type) {
278078d8e58aSMike Snitzer 	case DM_TYPE_BIO_BASED:
2781545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
278278d8e58aSMike Snitzer 		pool_size = dm_get_reserved_bio_based_ios();
278330187e1dSMike Snitzer 		front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
2784eb8db831SChristoph Hellwig 
2785eb8db831SChristoph Hellwig 		pools->io_pool = mempool_create_slab_pool(pool_size, _io_cache);
2786eb8db831SChristoph Hellwig 		if (!pools->io_pool)
2787eb8db831SChristoph Hellwig 			goto out;
278878d8e58aSMike Snitzer 		break;
278978d8e58aSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
279078d8e58aSMike Snitzer 	case DM_TYPE_MQ_REQUEST_BASED:
279178d8e58aSMike Snitzer 		pool_size = dm_get_reserved_rq_based_ios();
279278d8e58aSMike Snitzer 		front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
2793591ddcfcSMike Snitzer 		/* per_io_data_size is used for blk-mq pdu at queue allocation */
279478d8e58aSMike Snitzer 		break;
279578d8e58aSMike Snitzer 	default:
279678d8e58aSMike Snitzer 		BUG();
279778d8e58aSMike Snitzer 	}
279878d8e58aSMike Snitzer 
279947e0fb46SNeilBrown 	pools->bs = bioset_create(pool_size, front_pad, BIOSET_NEED_RESCUER);
2800e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
28015f015204SJun'ichi Nomura 		goto out;
2802e6ee8c0bSKiyoshi Ueda 
2803a91a2785SMartin K. Petersen 	if (integrity && bioset_integrity_create(pools->bs, pool_size))
28045f015204SJun'ichi Nomura 		goto out;
2805a91a2785SMartin K. Petersen 
2806e6ee8c0bSKiyoshi Ueda 	return pools;
280778d8e58aSMike Snitzer 
28085f015204SJun'ichi Nomura out:
28095f015204SJun'ichi Nomura 	dm_free_md_mempools(pools);
2810e6ee8c0bSKiyoshi Ueda 
28114e6e36c3SMike Snitzer 	return NULL;
2812e6ee8c0bSKiyoshi Ueda }
2813e6ee8c0bSKiyoshi Ueda 
2814e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
2815e6ee8c0bSKiyoshi Ueda {
2816e6ee8c0bSKiyoshi Ueda 	if (!pools)
2817e6ee8c0bSKiyoshi Ueda 		return;
2818e6ee8c0bSKiyoshi Ueda 
2819e6ee8c0bSKiyoshi Ueda 	mempool_destroy(pools->io_pool);
28201ae49ea2SMike Snitzer 
2821e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
2822e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
2823e6ee8c0bSKiyoshi Ueda 
2824e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2825e6ee8c0bSKiyoshi Ueda }
2826e6ee8c0bSKiyoshi Ueda 
28279c72bad1SChristoph Hellwig struct dm_pr {
28289c72bad1SChristoph Hellwig 	u64	old_key;
28299c72bad1SChristoph Hellwig 	u64	new_key;
28309c72bad1SChristoph Hellwig 	u32	flags;
28319c72bad1SChristoph Hellwig 	bool	fail_early;
28329c72bad1SChristoph Hellwig };
28339c72bad1SChristoph Hellwig 
28349c72bad1SChristoph Hellwig static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
28359c72bad1SChristoph Hellwig 		      void *data)
28369c72bad1SChristoph Hellwig {
28379c72bad1SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
28389c72bad1SChristoph Hellwig 	struct dm_table *table;
28399c72bad1SChristoph Hellwig 	struct dm_target *ti;
28409c72bad1SChristoph Hellwig 	int ret = -ENOTTY, srcu_idx;
28419c72bad1SChristoph Hellwig 
28429c72bad1SChristoph Hellwig 	table = dm_get_live_table(md, &srcu_idx);
28439c72bad1SChristoph Hellwig 	if (!table || !dm_table_get_size(table))
28449c72bad1SChristoph Hellwig 		goto out;
28459c72bad1SChristoph Hellwig 
28469c72bad1SChristoph Hellwig 	/* We only support devices that have a single target */
28479c72bad1SChristoph Hellwig 	if (dm_table_get_num_targets(table) != 1)
28489c72bad1SChristoph Hellwig 		goto out;
28499c72bad1SChristoph Hellwig 	ti = dm_table_get_target(table, 0);
28509c72bad1SChristoph Hellwig 
28519c72bad1SChristoph Hellwig 	ret = -EINVAL;
28529c72bad1SChristoph Hellwig 	if (!ti->type->iterate_devices)
28539c72bad1SChristoph Hellwig 		goto out;
28549c72bad1SChristoph Hellwig 
28559c72bad1SChristoph Hellwig 	ret = ti->type->iterate_devices(ti, fn, data);
28569c72bad1SChristoph Hellwig out:
28579c72bad1SChristoph Hellwig 	dm_put_live_table(md, srcu_idx);
28589c72bad1SChristoph Hellwig 	return ret;
28599c72bad1SChristoph Hellwig }
28609c72bad1SChristoph Hellwig 
28619c72bad1SChristoph Hellwig /*
28629c72bad1SChristoph Hellwig  * For register / unregister we need to manually call out to every path.
28639c72bad1SChristoph Hellwig  */
28649c72bad1SChristoph Hellwig static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
28659c72bad1SChristoph Hellwig 			    sector_t start, sector_t len, void *data)
28669c72bad1SChristoph Hellwig {
28679c72bad1SChristoph Hellwig 	struct dm_pr *pr = data;
28689c72bad1SChristoph Hellwig 	const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
28699c72bad1SChristoph Hellwig 
28709c72bad1SChristoph Hellwig 	if (!ops || !ops->pr_register)
28719c72bad1SChristoph Hellwig 		return -EOPNOTSUPP;
28729c72bad1SChristoph Hellwig 	return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
28739c72bad1SChristoph Hellwig }
28749c72bad1SChristoph Hellwig 
287571cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
287671cdb697SChristoph Hellwig 			  u32 flags)
287771cdb697SChristoph Hellwig {
28789c72bad1SChristoph Hellwig 	struct dm_pr pr = {
28799c72bad1SChristoph Hellwig 		.old_key	= old_key,
28809c72bad1SChristoph Hellwig 		.new_key	= new_key,
28819c72bad1SChristoph Hellwig 		.flags		= flags,
28829c72bad1SChristoph Hellwig 		.fail_early	= true,
28839c72bad1SChristoph Hellwig 	};
28849c72bad1SChristoph Hellwig 	int ret;
288571cdb697SChristoph Hellwig 
28869c72bad1SChristoph Hellwig 	ret = dm_call_pr(bdev, __dm_pr_register, &pr);
28879c72bad1SChristoph Hellwig 	if (ret && new_key) {
28889c72bad1SChristoph Hellwig 		/* unregister all paths if we failed to register any path */
28899c72bad1SChristoph Hellwig 		pr.old_key = new_key;
28909c72bad1SChristoph Hellwig 		pr.new_key = 0;
28919c72bad1SChristoph Hellwig 		pr.flags = 0;
28929c72bad1SChristoph Hellwig 		pr.fail_early = false;
28939c72bad1SChristoph Hellwig 		dm_call_pr(bdev, __dm_pr_register, &pr);
28949c72bad1SChristoph Hellwig 	}
289571cdb697SChristoph Hellwig 
28969c72bad1SChristoph Hellwig 	return ret;
289771cdb697SChristoph Hellwig }
289871cdb697SChristoph Hellwig 
289971cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
290071cdb697SChristoph Hellwig 			 u32 flags)
290171cdb697SChristoph Hellwig {
290271cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
290371cdb697SChristoph Hellwig 	const struct pr_ops *ops;
290471cdb697SChristoph Hellwig 	fmode_t mode;
2905956a4025SMike Snitzer 	int r;
290671cdb697SChristoph Hellwig 
2907956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
290871cdb697SChristoph Hellwig 	if (r < 0)
290971cdb697SChristoph Hellwig 		return r;
291071cdb697SChristoph Hellwig 
291171cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
291271cdb697SChristoph Hellwig 	if (ops && ops->pr_reserve)
291371cdb697SChristoph Hellwig 		r = ops->pr_reserve(bdev, key, type, flags);
291471cdb697SChristoph Hellwig 	else
291571cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
291671cdb697SChristoph Hellwig 
2917956a4025SMike Snitzer 	bdput(bdev);
291871cdb697SChristoph Hellwig 	return r;
291971cdb697SChristoph Hellwig }
292071cdb697SChristoph Hellwig 
292171cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
292271cdb697SChristoph Hellwig {
292371cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
292471cdb697SChristoph Hellwig 	const struct pr_ops *ops;
292571cdb697SChristoph Hellwig 	fmode_t mode;
2926956a4025SMike Snitzer 	int r;
292771cdb697SChristoph Hellwig 
2928956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
292971cdb697SChristoph Hellwig 	if (r < 0)
293071cdb697SChristoph Hellwig 		return r;
293171cdb697SChristoph Hellwig 
293271cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
293371cdb697SChristoph Hellwig 	if (ops && ops->pr_release)
293471cdb697SChristoph Hellwig 		r = ops->pr_release(bdev, key, type);
293571cdb697SChristoph Hellwig 	else
293671cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
293771cdb697SChristoph Hellwig 
2938956a4025SMike Snitzer 	bdput(bdev);
293971cdb697SChristoph Hellwig 	return r;
294071cdb697SChristoph Hellwig }
294171cdb697SChristoph Hellwig 
294271cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
294371cdb697SChristoph Hellwig 			 enum pr_type type, bool abort)
294471cdb697SChristoph Hellwig {
294571cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
294671cdb697SChristoph Hellwig 	const struct pr_ops *ops;
294771cdb697SChristoph Hellwig 	fmode_t mode;
2948956a4025SMike Snitzer 	int r;
294971cdb697SChristoph Hellwig 
2950956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
295171cdb697SChristoph Hellwig 	if (r < 0)
295271cdb697SChristoph Hellwig 		return r;
295371cdb697SChristoph Hellwig 
295471cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
295571cdb697SChristoph Hellwig 	if (ops && ops->pr_preempt)
295671cdb697SChristoph Hellwig 		r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
295771cdb697SChristoph Hellwig 	else
295871cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
295971cdb697SChristoph Hellwig 
2960956a4025SMike Snitzer 	bdput(bdev);
296171cdb697SChristoph Hellwig 	return r;
296271cdb697SChristoph Hellwig }
296371cdb697SChristoph Hellwig 
296471cdb697SChristoph Hellwig static int dm_pr_clear(struct block_device *bdev, u64 key)
296571cdb697SChristoph Hellwig {
296671cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
296771cdb697SChristoph Hellwig 	const struct pr_ops *ops;
296871cdb697SChristoph Hellwig 	fmode_t mode;
2969956a4025SMike Snitzer 	int r;
297071cdb697SChristoph Hellwig 
2971956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
297271cdb697SChristoph Hellwig 	if (r < 0)
297371cdb697SChristoph Hellwig 		return r;
297471cdb697SChristoph Hellwig 
297571cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
297671cdb697SChristoph Hellwig 	if (ops && ops->pr_clear)
297771cdb697SChristoph Hellwig 		r = ops->pr_clear(bdev, key);
297871cdb697SChristoph Hellwig 	else
297971cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
298071cdb697SChristoph Hellwig 
2981956a4025SMike Snitzer 	bdput(bdev);
298271cdb697SChristoph Hellwig 	return r;
298371cdb697SChristoph Hellwig }
298471cdb697SChristoph Hellwig 
298571cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = {
298671cdb697SChristoph Hellwig 	.pr_register	= dm_pr_register,
298771cdb697SChristoph Hellwig 	.pr_reserve	= dm_pr_reserve,
298871cdb697SChristoph Hellwig 	.pr_release	= dm_pr_release,
298971cdb697SChristoph Hellwig 	.pr_preempt	= dm_pr_preempt,
299071cdb697SChristoph Hellwig 	.pr_clear	= dm_pr_clear,
299171cdb697SChristoph Hellwig };
299271cdb697SChristoph Hellwig 
299383d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
29941da177e4SLinus Torvalds 	.open = dm_blk_open,
29951da177e4SLinus Torvalds 	.release = dm_blk_close,
2996aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
29973ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
299871cdb697SChristoph Hellwig 	.pr_ops = &dm_pr_ops,
29991da177e4SLinus Torvalds 	.owner = THIS_MODULE
30001da177e4SLinus Torvalds };
30011da177e4SLinus Torvalds 
3002f26c5719SDan Williams static const struct dax_operations dm_dax_ops = {
3003f26c5719SDan Williams 	.direct_access = dm_dax_direct_access,
30047e026c8cSDan Williams 	.copy_from_iter = dm_dax_copy_from_iter,
3005abebfbe2SDan Williams 	.flush = dm_dax_flush,
3006f26c5719SDan Williams };
3007f26c5719SDan Williams 
30081da177e4SLinus Torvalds /*
30091da177e4SLinus Torvalds  * module hooks
30101da177e4SLinus Torvalds  */
30111da177e4SLinus Torvalds module_init(dm_init);
30121da177e4SLinus Torvalds module_exit(dm_exit);
30131da177e4SLinus Torvalds 
30141da177e4SLinus Torvalds module_param(major, uint, 0);
30151da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
3016f4790826SMike Snitzer 
3017e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3018e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3019e8603136SMike Snitzer 
3020115485e8SMike Snitzer module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
3021115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
3022115485e8SMike Snitzer 
30231da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
30241da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
30251da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3026