xref: /openbmc/linux/drivers/md/dm.c (revision 6aa7de059173a986114ac43b8f50b297a86f09a8)
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 
3060935eb2SMilan Broz /*
3160935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
3260935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
3360935eb2SMilan Broz  */
3460935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
3560935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
3660935eb2SMilan Broz 
371da177e4SLinus Torvalds static const char *_name = DM_NAME;
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds static unsigned int major = 0;
401da177e4SLinus Torvalds static unsigned int _major = 0;
411da177e4SLinus Torvalds 
42d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr);
43d15b774cSAlasdair G Kergon 
44f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
452c140a24SMikulas Patocka 
462c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w);
472c140a24SMikulas Patocka 
482c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
492c140a24SMikulas Patocka 
50acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue;
51acfe0ad7SMikulas Patocka 
5293e6442cSMikulas Patocka atomic_t dm_global_event_nr = ATOMIC_INIT(0);
5393e6442cSMikulas Patocka DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq);
5493e6442cSMikulas Patocka 
5562e08243SMikulas Patocka void dm_issue_global_event(void)
5662e08243SMikulas Patocka {
5762e08243SMikulas Patocka 	atomic_inc(&dm_global_event_nr);
5862e08243SMikulas Patocka 	wake_up(&dm_global_eventq);
5962e08243SMikulas Patocka }
6062e08243SMikulas Patocka 
611da177e4SLinus Torvalds /*
621da177e4SLinus Torvalds  * One of these is allocated per bio.
631da177e4SLinus Torvalds  */
641da177e4SLinus Torvalds struct dm_io {
651da177e4SLinus Torvalds 	struct mapped_device *md;
664e4cbee9SChristoph Hellwig 	blk_status_t status;
671da177e4SLinus Torvalds 	atomic_t io_count;
686ae2fa67SRichard Kennedy 	struct bio *bio;
693eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
70f88fb981SKiyoshi Ueda 	spinlock_t endio_lock;
71fd2ed4d2SMikulas Patocka 	struct dm_stats_aux stats_aux;
721da177e4SLinus Torvalds };
731da177e4SLinus Torvalds 
74ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
75ba61fdd1SJeff Mahoney 
761da177e4SLinus Torvalds /*
771da177e4SLinus Torvalds  * Bits for the md->flags field.
781da177e4SLinus Torvalds  */
791eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
801da177e4SLinus Torvalds #define DMF_SUSPENDED 1
81aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
82fba9f90eSJeff Mahoney #define DMF_FREEING 3
835c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
842e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
858ae12666SKent Overstreet #define DMF_DEFERRED_REMOVE 6
868ae12666SKent Overstreet #define DMF_SUSPENDED_INTERNALLY 7
871da177e4SLinus Torvalds 
88115485e8SMike Snitzer #define DM_NUMA_NODE NUMA_NO_NODE
89115485e8SMike Snitzer static int dm_numa_node = DM_NUMA_NODE;
90faad87dfSMike Snitzer 
91e6ee8c0bSKiyoshi Ueda /*
92e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
93e6ee8c0bSKiyoshi Ueda  */
94e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
95e6ee8c0bSKiyoshi Ueda 	mempool_t *io_pool;
96e6ee8c0bSKiyoshi Ueda 	struct bio_set *bs;
97e6ee8c0bSKiyoshi Ueda };
98e6ee8c0bSKiyoshi Ueda 
9986f1152bSBenjamin Marzinski struct table_device {
10086f1152bSBenjamin Marzinski 	struct list_head list;
10186f1152bSBenjamin Marzinski 	atomic_t count;
10286f1152bSBenjamin Marzinski 	struct dm_dev dm_dev;
10386f1152bSBenjamin Marzinski };
10486f1152bSBenjamin Marzinski 
105e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
1068fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
1071ae49ea2SMike Snitzer static struct kmem_cache *_rq_cache;
10894818742SKent Overstreet 
109f4790826SMike Snitzer /*
110e8603136SMike Snitzer  * Bio-based DM's mempools' reserved IOs set by the user.
111e8603136SMike Snitzer  */
1124cc96131SMike Snitzer #define RESERVED_BIO_BASED_IOS		16
113e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
114e8603136SMike Snitzer 
115115485e8SMike Snitzer static int __dm_get_module_param_int(int *module_param, int min, int max)
116115485e8SMike Snitzer {
117*6aa7de05SMark Rutland 	int param = READ_ONCE(*module_param);
118115485e8SMike Snitzer 	int modified_param = 0;
119115485e8SMike Snitzer 	bool modified = true;
120115485e8SMike Snitzer 
121115485e8SMike Snitzer 	if (param < min)
122115485e8SMike Snitzer 		modified_param = min;
123115485e8SMike Snitzer 	else if (param > max)
124115485e8SMike Snitzer 		modified_param = max;
125115485e8SMike Snitzer 	else
126115485e8SMike Snitzer 		modified = false;
127115485e8SMike Snitzer 
128115485e8SMike Snitzer 	if (modified) {
129115485e8SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
130115485e8SMike Snitzer 		param = modified_param;
131115485e8SMike Snitzer 	}
132115485e8SMike Snitzer 
133115485e8SMike Snitzer 	return param;
134115485e8SMike Snitzer }
135115485e8SMike Snitzer 
1364cc96131SMike Snitzer unsigned __dm_get_module_param(unsigned *module_param,
137f4790826SMike Snitzer 			       unsigned def, unsigned max)
138f4790826SMike Snitzer {
139*6aa7de05SMark Rutland 	unsigned param = READ_ONCE(*module_param);
14009c2d531SMike Snitzer 	unsigned modified_param = 0;
141f4790826SMike Snitzer 
14209c2d531SMike Snitzer 	if (!param)
14309c2d531SMike Snitzer 		modified_param = def;
14409c2d531SMike Snitzer 	else if (param > max)
14509c2d531SMike Snitzer 		modified_param = max;
146f4790826SMike Snitzer 
14709c2d531SMike Snitzer 	if (modified_param) {
14809c2d531SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
14909c2d531SMike Snitzer 		param = modified_param;
150f4790826SMike Snitzer 	}
151f4790826SMike Snitzer 
15209c2d531SMike Snitzer 	return param;
153f4790826SMike Snitzer }
154f4790826SMike Snitzer 
155e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void)
156e8603136SMike Snitzer {
15709c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_bio_based_ios,
1584cc96131SMike Snitzer 				     RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS);
159e8603136SMike Snitzer }
160e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
161e8603136SMike Snitzer 
162115485e8SMike Snitzer static unsigned dm_get_numa_node(void)
163115485e8SMike Snitzer {
164115485e8SMike Snitzer 	return __dm_get_module_param_int(&dm_numa_node,
165115485e8SMike Snitzer 					 DM_NUMA_NODE, num_online_nodes() - 1);
166115485e8SMike Snitzer }
167115485e8SMike Snitzer 
1681da177e4SLinus Torvalds static int __init local_init(void)
1691da177e4SLinus Torvalds {
17051157b4aSKiyoshi Ueda 	int r = -ENOMEM;
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
173028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
1741da177e4SLinus Torvalds 	if (!_io_cache)
17551157b4aSKiyoshi Ueda 		return r;
1761da177e4SLinus Torvalds 
1778fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
1788fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
179dba14160SMikulas Patocka 		goto out_free_io_cache;
1808fbf26adSKiyoshi Ueda 
181eca7ee6dSMike Snitzer 	_rq_cache = kmem_cache_create("dm_old_clone_request", sizeof(struct request),
1821ae49ea2SMike Snitzer 				      __alignof__(struct request), 0, NULL);
1831ae49ea2SMike Snitzer 	if (!_rq_cache)
1841ae49ea2SMike Snitzer 		goto out_free_rq_tio_cache;
1851ae49ea2SMike Snitzer 
18651e5b2bdSMike Anderson 	r = dm_uevent_init();
18751157b4aSKiyoshi Ueda 	if (r)
1881ae49ea2SMike Snitzer 		goto out_free_rq_cache;
18951e5b2bdSMike Anderson 
190acfe0ad7SMikulas Patocka 	deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
191acfe0ad7SMikulas Patocka 	if (!deferred_remove_workqueue) {
192acfe0ad7SMikulas Patocka 		r = -ENOMEM;
193acfe0ad7SMikulas Patocka 		goto out_uevent_exit;
194acfe0ad7SMikulas Patocka 	}
195acfe0ad7SMikulas Patocka 
1961da177e4SLinus Torvalds 	_major = major;
1971da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
19851157b4aSKiyoshi Ueda 	if (r < 0)
199acfe0ad7SMikulas Patocka 		goto out_free_workqueue;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	if (!_major)
2021da177e4SLinus Torvalds 		_major = r;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	return 0;
20551157b4aSKiyoshi Ueda 
206acfe0ad7SMikulas Patocka out_free_workqueue:
207acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
20851157b4aSKiyoshi Ueda out_uevent_exit:
20951157b4aSKiyoshi Ueda 	dm_uevent_exit();
2101ae49ea2SMike Snitzer out_free_rq_cache:
2111ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
2128fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
2138fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
21451157b4aSKiyoshi Ueda out_free_io_cache:
21551157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
21651157b4aSKiyoshi Ueda 
21751157b4aSKiyoshi Ueda 	return r;
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds static void local_exit(void)
2211da177e4SLinus Torvalds {
2222c140a24SMikulas Patocka 	flush_scheduled_work();
223acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
2242c140a24SMikulas Patocka 
2251ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
2268fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
2271da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
22800d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
22951e5b2bdSMike Anderson 	dm_uevent_exit();
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	_major = 0;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	DMINFO("cleaned up");
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
236b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
2371da177e4SLinus Torvalds 	local_init,
2381da177e4SLinus Torvalds 	dm_target_init,
2391da177e4SLinus Torvalds 	dm_linear_init,
2401da177e4SLinus Torvalds 	dm_stripe_init,
241952b3557SMikulas Patocka 	dm_io_init,
242945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2431da177e4SLinus Torvalds 	dm_interface_init,
244fd2ed4d2SMikulas Patocka 	dm_statistics_init,
2451da177e4SLinus Torvalds };
2461da177e4SLinus Torvalds 
247b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2481da177e4SLinus Torvalds 	local_exit,
2491da177e4SLinus Torvalds 	dm_target_exit,
2501da177e4SLinus Torvalds 	dm_linear_exit,
2511da177e4SLinus Torvalds 	dm_stripe_exit,
252952b3557SMikulas Patocka 	dm_io_exit,
253945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
2541da177e4SLinus Torvalds 	dm_interface_exit,
255fd2ed4d2SMikulas Patocka 	dm_statistics_exit,
2561da177e4SLinus Torvalds };
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds static int __init dm_init(void)
2591da177e4SLinus Torvalds {
2601da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	int r, i;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
2651da177e4SLinus Torvalds 		r = _inits[i]();
2661da177e4SLinus Torvalds 		if (r)
2671da177e4SLinus Torvalds 			goto bad;
2681da177e4SLinus Torvalds 	}
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	return 0;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds       bad:
2731da177e4SLinus Torvalds 	while (i--)
2741da177e4SLinus Torvalds 		_exits[i]();
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	return r;
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds static void __exit dm_exit(void)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	while (i--)
2841da177e4SLinus Torvalds 		_exits[i]();
285d15b774cSAlasdair G Kergon 
286d15b774cSAlasdair G Kergon 	/*
287d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
288d15b774cSAlasdair G Kergon 	 */
289d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
2901da177e4SLinus Torvalds }
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds /*
2931da177e4SLinus Torvalds  * Block device functions
2941da177e4SLinus Torvalds  */
295432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
296432a212cSMike Anderson {
297432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
298432a212cSMike Anderson }
299432a212cSMike Anderson 
300fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3011da177e4SLinus Torvalds {
3021da177e4SLinus Torvalds 	struct mapped_device *md;
3031da177e4SLinus Torvalds 
304fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
305fba9f90eSJeff Mahoney 
306fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
307fba9f90eSJeff Mahoney 	if (!md)
308fba9f90eSJeff Mahoney 		goto out;
309fba9f90eSJeff Mahoney 
3105c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
311432a212cSMike Anderson 	    dm_deleting_md(md)) {
312fba9f90eSJeff Mahoney 		md = NULL;
313fba9f90eSJeff Mahoney 		goto out;
314fba9f90eSJeff Mahoney 	}
315fba9f90eSJeff Mahoney 
3161da177e4SLinus Torvalds 	dm_get(md);
3175c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
318fba9f90eSJeff Mahoney out:
319fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
320fba9f90eSJeff Mahoney 
321fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
324db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode)
3251da177e4SLinus Torvalds {
32663a4f065SMike Snitzer 	struct mapped_device *md;
3276e9624b8SArnd Bergmann 
3284a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
3294a1aeb98SMilan Broz 
33063a4f065SMike Snitzer 	md = disk->private_data;
33163a4f065SMike Snitzer 	if (WARN_ON(!md))
33263a4f065SMike Snitzer 		goto out;
33363a4f065SMike Snitzer 
3342c140a24SMikulas Patocka 	if (atomic_dec_and_test(&md->open_count) &&
3352c140a24SMikulas Patocka 	    (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
336acfe0ad7SMikulas Patocka 		queue_work(deferred_remove_workqueue, &deferred_remove_work);
3372c140a24SMikulas Patocka 
3381da177e4SLinus Torvalds 	dm_put(md);
33963a4f065SMike Snitzer out:
3404a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds 
3435c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3445c6bd75dSAlasdair G Kergon {
3455c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3465c6bd75dSAlasdair G Kergon }
3475c6bd75dSAlasdair G Kergon 
3485c6bd75dSAlasdair G Kergon /*
3495c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3505c6bd75dSAlasdair G Kergon  */
3512c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
3525c6bd75dSAlasdair G Kergon {
3535c6bd75dSAlasdair G Kergon 	int r = 0;
3545c6bd75dSAlasdair G Kergon 
3555c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
3565c6bd75dSAlasdair G Kergon 
3572c140a24SMikulas Patocka 	if (dm_open_count(md)) {
3585c6bd75dSAlasdair G Kergon 		r = -EBUSY;
3592c140a24SMikulas Patocka 		if (mark_deferred)
3602c140a24SMikulas Patocka 			set_bit(DMF_DEFERRED_REMOVE, &md->flags);
3612c140a24SMikulas Patocka 	} else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
3622c140a24SMikulas Patocka 		r = -EEXIST;
3635c6bd75dSAlasdair G Kergon 	else
3645c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
3655c6bd75dSAlasdair G Kergon 
3665c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
3675c6bd75dSAlasdair G Kergon 
3685c6bd75dSAlasdair G Kergon 	return r;
3695c6bd75dSAlasdair G Kergon }
3705c6bd75dSAlasdair G Kergon 
3712c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md)
3722c140a24SMikulas Patocka {
3732c140a24SMikulas Patocka 	int r = 0;
3742c140a24SMikulas Patocka 
3752c140a24SMikulas Patocka 	spin_lock(&_minor_lock);
3762c140a24SMikulas Patocka 
3772c140a24SMikulas Patocka 	if (test_bit(DMF_DELETING, &md->flags))
3782c140a24SMikulas Patocka 		r = -EBUSY;
3792c140a24SMikulas Patocka 	else
3802c140a24SMikulas Patocka 		clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
3812c140a24SMikulas Patocka 
3822c140a24SMikulas Patocka 	spin_unlock(&_minor_lock);
3832c140a24SMikulas Patocka 
3842c140a24SMikulas Patocka 	return r;
3852c140a24SMikulas Patocka }
3862c140a24SMikulas Patocka 
3872c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w)
3882c140a24SMikulas Patocka {
3892c140a24SMikulas Patocka 	dm_deferred_remove();
3902c140a24SMikulas Patocka }
3912c140a24SMikulas Patocka 
392fd2ed4d2SMikulas Patocka sector_t dm_get_size(struct mapped_device *md)
393fd2ed4d2SMikulas Patocka {
394fd2ed4d2SMikulas Patocka 	return get_capacity(md->disk);
395fd2ed4d2SMikulas Patocka }
396fd2ed4d2SMikulas Patocka 
3979974fa2cSMike Snitzer struct request_queue *dm_get_md_queue(struct mapped_device *md)
3989974fa2cSMike Snitzer {
3999974fa2cSMike Snitzer 	return md->queue;
4009974fa2cSMike Snitzer }
4019974fa2cSMike Snitzer 
402fd2ed4d2SMikulas Patocka struct dm_stats *dm_get_stats(struct mapped_device *md)
403fd2ed4d2SMikulas Patocka {
404fd2ed4d2SMikulas Patocka 	return &md->stats;
405fd2ed4d2SMikulas Patocka }
406fd2ed4d2SMikulas Patocka 
4073ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4083ac51e74SDarrick J. Wong {
4093ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
4103ac51e74SDarrick J. Wong 
4113ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
4123ac51e74SDarrick J. Wong }
4133ac51e74SDarrick J. Wong 
414956a4025SMike Snitzer static int dm_grab_bdev_for_ioctl(struct mapped_device *md,
41566482026SMike Snitzer 				  struct block_device **bdev,
416956a4025SMike Snitzer 				  fmode_t *mode)
417aa129a22SMilan Broz {
41866482026SMike Snitzer 	struct dm_target *tgt;
4196c182cd8SHannes Reinecke 	struct dm_table *map;
420956a4025SMike Snitzer 	int srcu_idx, r;
421aa129a22SMilan Broz 
4226c182cd8SHannes Reinecke retry:
423e56f81e0SChristoph Hellwig 	r = -ENOTTY;
424956a4025SMike Snitzer 	map = dm_get_live_table(md, &srcu_idx);
425aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
426aa129a22SMilan Broz 		goto out;
427aa129a22SMilan Broz 
428aa129a22SMilan Broz 	/* We only support devices that have a single target */
429aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
430aa129a22SMilan Broz 		goto out;
431aa129a22SMilan Broz 
43266482026SMike Snitzer 	tgt = dm_table_get_target(map, 0);
43366482026SMike Snitzer 	if (!tgt->type->prepare_ioctl)
4344d341d82SMike Snitzer 		goto out;
435aa129a22SMilan Broz 
4364f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
437aa129a22SMilan Broz 		r = -EAGAIN;
438aa129a22SMilan Broz 		goto out;
439aa129a22SMilan Broz 	}
440aa129a22SMilan Broz 
44166482026SMike Snitzer 	r = tgt->type->prepare_ioctl(tgt, bdev, mode);
442e56f81e0SChristoph Hellwig 	if (r < 0)
443e56f81e0SChristoph Hellwig 		goto out;
444e56f81e0SChristoph Hellwig 
445956a4025SMike Snitzer 	bdgrab(*bdev);
446956a4025SMike Snitzer 	dm_put_live_table(md, srcu_idx);
447e56f81e0SChristoph Hellwig 	return r;
448aa129a22SMilan Broz 
449aa129a22SMilan Broz out:
450956a4025SMike Snitzer 	dm_put_live_table(md, srcu_idx);
4515bbbfdf6SJunichi Nomura 	if (r == -ENOTCONN && !fatal_signal_pending(current)) {
4526c182cd8SHannes Reinecke 		msleep(10);
4536c182cd8SHannes Reinecke 		goto retry;
4546c182cd8SHannes Reinecke 	}
455e56f81e0SChristoph Hellwig 	return r;
456e56f81e0SChristoph Hellwig }
4576c182cd8SHannes Reinecke 
458e56f81e0SChristoph Hellwig static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
459e56f81e0SChristoph Hellwig 			unsigned int cmd, unsigned long arg)
460e56f81e0SChristoph Hellwig {
461e56f81e0SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
462956a4025SMike Snitzer 	int r;
463e56f81e0SChristoph Hellwig 
464956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
465e56f81e0SChristoph Hellwig 	if (r < 0)
466e56f81e0SChristoph Hellwig 		return r;
467e56f81e0SChristoph Hellwig 
468e56f81e0SChristoph Hellwig 	if (r > 0) {
469e56f81e0SChristoph Hellwig 		/*
470e980f623SChristoph Hellwig 		 * Target determined this ioctl is being issued against a
471e980f623SChristoph Hellwig 		 * subset of the parent bdev; require extra privileges.
472e56f81e0SChristoph Hellwig 		 */
473e980f623SChristoph Hellwig 		if (!capable(CAP_SYS_RAWIO)) {
474e980f623SChristoph Hellwig 			DMWARN_LIMIT(
475e980f623SChristoph Hellwig 	"%s: sending ioctl %x to DM device without required privilege.",
476e980f623SChristoph Hellwig 				current->comm, cmd);
477e980f623SChristoph Hellwig 			r = -ENOIOCTLCMD;
478e56f81e0SChristoph Hellwig 			goto out;
479e56f81e0SChristoph Hellwig 		}
480e980f623SChristoph Hellwig 	}
481e56f81e0SChristoph Hellwig 
48266482026SMike Snitzer 	r =  __blkdev_driver_ioctl(bdev, mode, cmd, arg);
483e56f81e0SChristoph Hellwig out:
484956a4025SMike Snitzer 	bdput(bdev);
485aa129a22SMilan Broz 	return r;
486aa129a22SMilan Broz }
487aa129a22SMilan Broz 
488028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
4891da177e4SLinus Torvalds {
4901da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
493028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds 
498cfae7529SMike Snitzer static void free_tio(struct dm_target_io *tio)
4991da177e4SLinus Torvalds {
500dba14160SMikulas Patocka 	bio_put(&tio->clone);
5011da177e4SLinus Torvalds }
5021da177e4SLinus Torvalds 
5034cc96131SMike Snitzer int md_in_flight(struct mapped_device *md)
50490abb8c4SKiyoshi Ueda {
50590abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
50690abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
50790abb8c4SKiyoshi Ueda }
50890abb8c4SKiyoshi Ueda 
5093eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
5103eaf840eSJun'ichi "Nick" Nomura {
5113eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
512fd2ed4d2SMikulas Patocka 	struct bio *bio = io->bio;
513c9959059STejun Heo 	int cpu;
514fd2ed4d2SMikulas Patocka 	int rw = bio_data_dir(bio);
5153eaf840eSJun'ichi "Nick" Nomura 
5163eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
5173eaf840eSJun'ichi "Nick" Nomura 
518074a7acaSTejun Heo 	cpu = part_stat_lock();
519d62e26b3SJens Axboe 	part_round_stats(md->queue, cpu, &dm_disk(md)->part0);
520074a7acaSTejun Heo 	part_stat_unlock();
5211e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw],
5221e9bb880SShaohua Li 		atomic_inc_return(&md->pending[rw]));
523fd2ed4d2SMikulas Patocka 
524fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
525528ec5abSMike Christie 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
526528ec5abSMike Christie 				    bio->bi_iter.bi_sector, bio_sectors(bio),
527528ec5abSMike Christie 				    false, 0, &io->stats_aux);
5283eaf840eSJun'ichi "Nick" Nomura }
5293eaf840eSJun'ichi "Nick" Nomura 
530d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
5313eaf840eSJun'ichi "Nick" Nomura {
5323eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
5333eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
5343eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
53518c0b223SGu Zheng 	int pending;
5363eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
5373eaf840eSJun'ichi "Nick" Nomura 
538d62e26b3SJens Axboe 	generic_end_io_acct(md->queue, rw, &dm_disk(md)->part0, io->start_time);
5393eaf840eSJun'ichi "Nick" Nomura 
540fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
541528ec5abSMike Christie 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
542528ec5abSMike Christie 				    bio->bi_iter.bi_sector, bio_sectors(bio),
543528ec5abSMike Christie 				    true, duration, &io->stats_aux);
544fd2ed4d2SMikulas Patocka 
545af7e466aSMikulas Patocka 	/*
546af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
547d87f4c14STejun Heo 	 * a flush.
548af7e466aSMikulas Patocka 	 */
5491e9bb880SShaohua Li 	pending = atomic_dec_return(&md->pending[rw]);
5501e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
551316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
5523eaf840eSJun'ichi "Nick" Nomura 
553d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
554d221d2e7SMikulas Patocka 	if (!pending)
555d221d2e7SMikulas Patocka 		wake_up(&md->wait);
5563eaf840eSJun'ichi "Nick" Nomura }
5573eaf840eSJun'ichi "Nick" Nomura 
5581da177e4SLinus Torvalds /*
5591da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
5601da177e4SLinus Torvalds  */
56192c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
5621da177e4SLinus Torvalds {
56305447420SKiyoshi Ueda 	unsigned long flags;
5641da177e4SLinus Torvalds 
56505447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
5661da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
56705447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
56892c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds /*
5721da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
5731da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
57483d5e5b0SMikulas Patocka  * dm_put_live_table() when finished.
5751da177e4SLinus Torvalds  */
57683d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
5771da177e4SLinus Torvalds {
57883d5e5b0SMikulas Patocka 	*srcu_idx = srcu_read_lock(&md->io_barrier);
5791da177e4SLinus Torvalds 
58083d5e5b0SMikulas Patocka 	return srcu_dereference(md->map, &md->io_barrier);
58183d5e5b0SMikulas Patocka }
5821da177e4SLinus Torvalds 
58383d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
58483d5e5b0SMikulas Patocka {
58583d5e5b0SMikulas Patocka 	srcu_read_unlock(&md->io_barrier, srcu_idx);
58683d5e5b0SMikulas Patocka }
58783d5e5b0SMikulas Patocka 
58883d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md)
58983d5e5b0SMikulas Patocka {
59083d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
59183d5e5b0SMikulas Patocka 	synchronize_rcu_expedited();
59283d5e5b0SMikulas Patocka }
59383d5e5b0SMikulas Patocka 
59483d5e5b0SMikulas Patocka /*
59583d5e5b0SMikulas Patocka  * A fast alternative to dm_get_live_table/dm_put_live_table.
59683d5e5b0SMikulas Patocka  * The caller must not block between these two functions.
59783d5e5b0SMikulas Patocka  */
59883d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
59983d5e5b0SMikulas Patocka {
60083d5e5b0SMikulas Patocka 	rcu_read_lock();
60183d5e5b0SMikulas Patocka 	return rcu_dereference(md->map);
60283d5e5b0SMikulas Patocka }
60383d5e5b0SMikulas Patocka 
60483d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
60583d5e5b0SMikulas Patocka {
60683d5e5b0SMikulas Patocka 	rcu_read_unlock();
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
6093ac51e74SDarrick J. Wong /*
61086f1152bSBenjamin Marzinski  * Open a table device so we can use it as a map destination.
61186f1152bSBenjamin Marzinski  */
61286f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev,
61386f1152bSBenjamin Marzinski 			     struct mapped_device *md)
61486f1152bSBenjamin Marzinski {
61586f1152bSBenjamin Marzinski 	static char *_claim_ptr = "I belong to device-mapper";
61686f1152bSBenjamin Marzinski 	struct block_device *bdev;
61786f1152bSBenjamin Marzinski 
61886f1152bSBenjamin Marzinski 	int r;
61986f1152bSBenjamin Marzinski 
62086f1152bSBenjamin Marzinski 	BUG_ON(td->dm_dev.bdev);
62186f1152bSBenjamin Marzinski 
62286f1152bSBenjamin Marzinski 	bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
62386f1152bSBenjamin Marzinski 	if (IS_ERR(bdev))
62486f1152bSBenjamin Marzinski 		return PTR_ERR(bdev);
62586f1152bSBenjamin Marzinski 
62686f1152bSBenjamin Marzinski 	r = bd_link_disk_holder(bdev, dm_disk(md));
62786f1152bSBenjamin Marzinski 	if (r) {
62886f1152bSBenjamin Marzinski 		blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
62986f1152bSBenjamin Marzinski 		return r;
63086f1152bSBenjamin Marzinski 	}
63186f1152bSBenjamin Marzinski 
63286f1152bSBenjamin Marzinski 	td->dm_dev.bdev = bdev;
633817bf402SDan Williams 	td->dm_dev.dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
63486f1152bSBenjamin Marzinski 	return 0;
63586f1152bSBenjamin Marzinski }
63686f1152bSBenjamin Marzinski 
63786f1152bSBenjamin Marzinski /*
63886f1152bSBenjamin Marzinski  * Close a table device that we've been using.
63986f1152bSBenjamin Marzinski  */
64086f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md)
64186f1152bSBenjamin Marzinski {
64286f1152bSBenjamin Marzinski 	if (!td->dm_dev.bdev)
64386f1152bSBenjamin Marzinski 		return;
64486f1152bSBenjamin Marzinski 
64586f1152bSBenjamin Marzinski 	bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
64686f1152bSBenjamin Marzinski 	blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
647817bf402SDan Williams 	put_dax(td->dm_dev.dax_dev);
64886f1152bSBenjamin Marzinski 	td->dm_dev.bdev = NULL;
649817bf402SDan Williams 	td->dm_dev.dax_dev = NULL;
65086f1152bSBenjamin Marzinski }
65186f1152bSBenjamin Marzinski 
65286f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev,
65386f1152bSBenjamin Marzinski 					      fmode_t mode) {
65486f1152bSBenjamin Marzinski 	struct table_device *td;
65586f1152bSBenjamin Marzinski 
65686f1152bSBenjamin Marzinski 	list_for_each_entry(td, l, list)
65786f1152bSBenjamin Marzinski 		if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
65886f1152bSBenjamin Marzinski 			return td;
65986f1152bSBenjamin Marzinski 
66086f1152bSBenjamin Marzinski 	return NULL;
66186f1152bSBenjamin Marzinski }
66286f1152bSBenjamin Marzinski 
66386f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
66486f1152bSBenjamin Marzinski 			struct dm_dev **result) {
66586f1152bSBenjamin Marzinski 	int r;
66686f1152bSBenjamin Marzinski 	struct table_device *td;
66786f1152bSBenjamin Marzinski 
66886f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
66986f1152bSBenjamin Marzinski 	td = find_table_device(&md->table_devices, dev, mode);
67086f1152bSBenjamin Marzinski 	if (!td) {
671115485e8SMike Snitzer 		td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
67286f1152bSBenjamin Marzinski 		if (!td) {
67386f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
67486f1152bSBenjamin Marzinski 			return -ENOMEM;
67586f1152bSBenjamin Marzinski 		}
67686f1152bSBenjamin Marzinski 
67786f1152bSBenjamin Marzinski 		td->dm_dev.mode = mode;
67886f1152bSBenjamin Marzinski 		td->dm_dev.bdev = NULL;
67986f1152bSBenjamin Marzinski 
68086f1152bSBenjamin Marzinski 		if ((r = open_table_device(td, dev, md))) {
68186f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
68286f1152bSBenjamin Marzinski 			kfree(td);
68386f1152bSBenjamin Marzinski 			return r;
68486f1152bSBenjamin Marzinski 		}
68586f1152bSBenjamin Marzinski 
68686f1152bSBenjamin Marzinski 		format_dev_t(td->dm_dev.name, dev);
68786f1152bSBenjamin Marzinski 
68886f1152bSBenjamin Marzinski 		atomic_set(&td->count, 0);
68986f1152bSBenjamin Marzinski 		list_add(&td->list, &md->table_devices);
69086f1152bSBenjamin Marzinski 	}
69186f1152bSBenjamin Marzinski 	atomic_inc(&td->count);
69286f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
69386f1152bSBenjamin Marzinski 
69486f1152bSBenjamin Marzinski 	*result = &td->dm_dev;
69586f1152bSBenjamin Marzinski 	return 0;
69686f1152bSBenjamin Marzinski }
69786f1152bSBenjamin Marzinski EXPORT_SYMBOL_GPL(dm_get_table_device);
69886f1152bSBenjamin Marzinski 
69986f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
70086f1152bSBenjamin Marzinski {
70186f1152bSBenjamin Marzinski 	struct table_device *td = container_of(d, struct table_device, dm_dev);
70286f1152bSBenjamin Marzinski 
70386f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
70486f1152bSBenjamin Marzinski 	if (atomic_dec_and_test(&td->count)) {
70586f1152bSBenjamin Marzinski 		close_table_device(td, md);
70686f1152bSBenjamin Marzinski 		list_del(&td->list);
70786f1152bSBenjamin Marzinski 		kfree(td);
70886f1152bSBenjamin Marzinski 	}
70986f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
71086f1152bSBenjamin Marzinski }
71186f1152bSBenjamin Marzinski EXPORT_SYMBOL(dm_put_table_device);
71286f1152bSBenjamin Marzinski 
71386f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices)
71486f1152bSBenjamin Marzinski {
71586f1152bSBenjamin Marzinski 	struct list_head *tmp, *next;
71686f1152bSBenjamin Marzinski 
71786f1152bSBenjamin Marzinski 	list_for_each_safe(tmp, next, devices) {
71886f1152bSBenjamin Marzinski 		struct table_device *td = list_entry(tmp, struct table_device, list);
71986f1152bSBenjamin Marzinski 
72086f1152bSBenjamin Marzinski 		DMWARN("dm_destroy: %s still exists with %d references",
72186f1152bSBenjamin Marzinski 		       td->dm_dev.name, atomic_read(&td->count));
72286f1152bSBenjamin Marzinski 		kfree(td);
72386f1152bSBenjamin Marzinski 	}
72486f1152bSBenjamin Marzinski }
72586f1152bSBenjamin Marzinski 
72686f1152bSBenjamin Marzinski /*
7273ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
7283ac51e74SDarrick J. Wong  */
7293ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
7303ac51e74SDarrick J. Wong {
7313ac51e74SDarrick J. Wong 	*geo = md->geometry;
7323ac51e74SDarrick J. Wong 
7333ac51e74SDarrick J. Wong 	return 0;
7343ac51e74SDarrick J. Wong }
7353ac51e74SDarrick J. Wong 
7363ac51e74SDarrick J. Wong /*
7373ac51e74SDarrick J. Wong  * Set the geometry of a device.
7383ac51e74SDarrick J. Wong  */
7393ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
7403ac51e74SDarrick J. Wong {
7413ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
7423ac51e74SDarrick J. Wong 
7433ac51e74SDarrick J. Wong 	if (geo->start > sz) {
7443ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
7453ac51e74SDarrick J. Wong 		return -EINVAL;
7463ac51e74SDarrick J. Wong 	}
7473ac51e74SDarrick J. Wong 
7483ac51e74SDarrick J. Wong 	md->geometry = *geo;
7493ac51e74SDarrick J. Wong 
7503ac51e74SDarrick J. Wong 	return 0;
7513ac51e74SDarrick J. Wong }
7523ac51e74SDarrick J. Wong 
7531da177e4SLinus Torvalds /*-----------------------------------------------------------------
7541da177e4SLinus Torvalds  * CRUD START:
7551da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
7561da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
7571da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
7581da177e4SLinus Torvalds  *   interests of getting something for people to use I give
7591da177e4SLinus Torvalds  *   you this clearly demarcated crap.
7601da177e4SLinus Torvalds  *---------------------------------------------------------------*/
7611da177e4SLinus Torvalds 
7622e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
7632e93ccc1SKiyoshi Ueda {
7642e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
7652e93ccc1SKiyoshi Ueda }
7662e93ccc1SKiyoshi Ueda 
7671da177e4SLinus Torvalds /*
7681da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
7691da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
7701da177e4SLinus Torvalds  */
7714e4cbee9SChristoph Hellwig static void dec_pending(struct dm_io *io, blk_status_t error)
7721da177e4SLinus Torvalds {
7732e93ccc1SKiyoshi Ueda 	unsigned long flags;
7744e4cbee9SChristoph Hellwig 	blk_status_t io_error;
775b35f8caaSMilan Broz 	struct bio *bio;
776b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
7772e93ccc1SKiyoshi Ueda 
7782e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
779f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
780f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
7814e4cbee9SChristoph Hellwig 		if (!(io->status == BLK_STS_DM_REQUEUE &&
7824e4cbee9SChristoph Hellwig 				__noflush_suspending(md)))
7834e4cbee9SChristoph Hellwig 			io->status = error;
784f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
785f88fb981SKiyoshi Ueda 	}
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
7884e4cbee9SChristoph Hellwig 		if (io->status == BLK_STS_DM_REQUEUE) {
7892e93ccc1SKiyoshi Ueda 			/*
7902e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
7912e93ccc1SKiyoshi Ueda 			 */
792022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
7936a8736d1STejun Heo 			if (__noflush_suspending(md))
7946a8736d1STejun Heo 				bio_list_add_head(&md->deferred, io->bio);
7956a8736d1STejun Heo 			else
7962e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
7974e4cbee9SChristoph Hellwig 				io->status = BLK_STS_IOERR;
798022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
7992e93ccc1SKiyoshi Ueda 		}
8002e93ccc1SKiyoshi Ueda 
8014e4cbee9SChristoph Hellwig 		io_error = io->status;
802b35f8caaSMilan Broz 		bio = io->bio;
803af7e466aSMikulas Patocka 		end_io_acct(io);
804a97f925aSMikulas Patocka 		free_io(md, io);
8051da177e4SLinus Torvalds 
8064e4cbee9SChristoph Hellwig 		if (io_error == BLK_STS_DM_REQUEUE)
8076a8736d1STejun Heo 			return;
8086a8736d1STejun Heo 
8091eff9d32SJens Axboe 		if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) {
8101da177e4SLinus Torvalds 			/*
8116a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
81228a8f0d3SMike Christie 			 * without REQ_PREFLUSH.
8131da177e4SLinus Torvalds 			 */
8141eff9d32SJens Axboe 			bio->bi_opf &= ~REQ_PREFLUSH;
8156a8736d1STejun Heo 			queue_io(md, bio);
8165f3ea37cSArnaldo Carvalho de Melo 		} else {
817b372d360SMike Snitzer 			/* done with normal IO or empty flush */
8184e4cbee9SChristoph Hellwig 			bio->bi_status = io_error;
8194246a0b6SChristoph Hellwig 			bio_endio(bio);
8202e93ccc1SKiyoshi Ueda 		}
8211da177e4SLinus Torvalds 	}
822af7e466aSMikulas Patocka }
8231da177e4SLinus Torvalds 
8244cc96131SMike Snitzer void disable_write_same(struct mapped_device *md)
8257eee4ae2SMike Snitzer {
8267eee4ae2SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
8277eee4ae2SMike Snitzer 
8287eee4ae2SMike Snitzer 	/* device doesn't really support WRITE SAME, disable it */
8297eee4ae2SMike Snitzer 	limits->max_write_same_sectors = 0;
8307eee4ae2SMike Snitzer }
8317eee4ae2SMike Snitzer 
832ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md)
833ac62d620SChristoph Hellwig {
834ac62d620SChristoph Hellwig 	struct queue_limits *limits = dm_get_queue_limits(md);
835ac62d620SChristoph Hellwig 
836ac62d620SChristoph Hellwig 	/* device doesn't really support WRITE ZEROES, disable it */
837ac62d620SChristoph Hellwig 	limits->max_write_zeroes_sectors = 0;
838ac62d620SChristoph Hellwig }
839ac62d620SChristoph Hellwig 
8404246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio)
8411da177e4SLinus Torvalds {
8424e4cbee9SChristoph Hellwig 	blk_status_t error = bio->bi_status;
843bfc6d41cSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
844b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
8459faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
8461da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
8471da177e4SLinus Torvalds 
8484e4cbee9SChristoph Hellwig 	if (unlikely(error == BLK_STS_TARGET)) {
849ac62d620SChristoph Hellwig 		if (bio_op(bio) == REQ_OP_WRITE_SAME &&
85074d46992SChristoph Hellwig 		    !bio->bi_disk->queue->limits.max_write_same_sectors)
8517eee4ae2SMike Snitzer 			disable_write_same(md);
852ac62d620SChristoph Hellwig 		if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
85374d46992SChristoph Hellwig 		    !bio->bi_disk->queue->limits.max_write_zeroes_sectors)
854ac62d620SChristoph Hellwig 			disable_write_zeroes(md);
855ac62d620SChristoph Hellwig 	}
8567eee4ae2SMike Snitzer 
8571be56909SChristoph Hellwig 	if (endio) {
8584e4cbee9SChristoph Hellwig 		int r = endio(tio->ti, bio, &error);
8591be56909SChristoph Hellwig 		switch (r) {
8601be56909SChristoph Hellwig 		case DM_ENDIO_REQUEUE:
8614e4cbee9SChristoph Hellwig 			error = BLK_STS_DM_REQUEUE;
8621be56909SChristoph Hellwig 			/*FALLTHRU*/
8631be56909SChristoph Hellwig 		case DM_ENDIO_DONE:
8641be56909SChristoph Hellwig 			break;
8651be56909SChristoph Hellwig 		case DM_ENDIO_INCOMPLETE:
8661be56909SChristoph Hellwig 			/* The target will handle the io */
8671be56909SChristoph Hellwig 			return;
8681be56909SChristoph Hellwig 		default:
8691be56909SChristoph Hellwig 			DMWARN("unimplemented target endio return value: %d", r);
8701be56909SChristoph Hellwig 			BUG();
8711be56909SChristoph Hellwig 		}
8721be56909SChristoph Hellwig 	}
8731be56909SChristoph Hellwig 
874cfae7529SMike Snitzer 	free_tio(tio);
875b35f8caaSMilan Broz 	dec_pending(io, error);
8761da177e4SLinus Torvalds }
8771da177e4SLinus Torvalds 
87878d8e58aSMike Snitzer /*
87956a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
88056a67df7SMike Snitzer  * target boundary.
88156a67df7SMike Snitzer  */
88256a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
8831da177e4SLinus Torvalds {
88456a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
88556a67df7SMike Snitzer 
88656a67df7SMike Snitzer 	return ti->len - target_offset;
88756a67df7SMike Snitzer }
88856a67df7SMike Snitzer 
88956a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
89056a67df7SMike Snitzer {
89156a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
892542f9038SMike Snitzer 	sector_t offset, max_len;
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	/*
8951da177e4SLinus Torvalds 	 * Does the target need to split even further?
8961da177e4SLinus Torvalds 	 */
897542f9038SMike Snitzer 	if (ti->max_io_len) {
898542f9038SMike Snitzer 		offset = dm_target_offset(ti, sector);
899542f9038SMike Snitzer 		if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
900542f9038SMike Snitzer 			max_len = sector_div(offset, ti->max_io_len);
901542f9038SMike Snitzer 		else
902542f9038SMike Snitzer 			max_len = offset & (ti->max_io_len - 1);
903542f9038SMike Snitzer 		max_len = ti->max_io_len - max_len;
904542f9038SMike Snitzer 
905542f9038SMike Snitzer 		if (len > max_len)
906542f9038SMike Snitzer 			len = max_len;
9071da177e4SLinus Torvalds 	}
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	return len;
9101da177e4SLinus Torvalds }
9111da177e4SLinus Torvalds 
912542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
913542f9038SMike Snitzer {
914542f9038SMike Snitzer 	if (len > UINT_MAX) {
915542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
916542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
917542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
918542f9038SMike Snitzer 		return -EINVAL;
919542f9038SMike Snitzer 	}
920542f9038SMike Snitzer 
921542f9038SMike Snitzer 	ti->max_io_len = (uint32_t) len;
922542f9038SMike Snitzer 
923542f9038SMike Snitzer 	return 0;
924542f9038SMike Snitzer }
925542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
926542f9038SMike Snitzer 
927f26c5719SDan Williams static struct dm_target *dm_dax_get_live_target(struct mapped_device *md,
928f26c5719SDan Williams 		sector_t sector, int *srcu_idx)
929545ed20eSToshi Kani {
930545ed20eSToshi Kani 	struct dm_table *map;
931545ed20eSToshi Kani 	struct dm_target *ti;
932545ed20eSToshi Kani 
933f26c5719SDan Williams 	map = dm_get_live_table(md, srcu_idx);
934545ed20eSToshi Kani 	if (!map)
935f26c5719SDan Williams 		return NULL;
936545ed20eSToshi Kani 
937545ed20eSToshi Kani 	ti = dm_table_find_target(map, sector);
938545ed20eSToshi Kani 	if (!dm_target_is_valid(ti))
939f26c5719SDan Williams 		return NULL;
940f26c5719SDan Williams 
941f26c5719SDan Williams 	return ti;
942f26c5719SDan Williams }
943f26c5719SDan Williams 
944f26c5719SDan Williams static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
945f26c5719SDan Williams 		long nr_pages, void **kaddr, pfn_t *pfn)
946f26c5719SDan Williams {
947f26c5719SDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
948f26c5719SDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
949f26c5719SDan Williams 	struct dm_target *ti;
950f26c5719SDan Williams 	long len, ret = -EIO;
951f26c5719SDan Williams 	int srcu_idx;
952f26c5719SDan Williams 
953f26c5719SDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
954f26c5719SDan Williams 
955f26c5719SDan Williams 	if (!ti)
956545ed20eSToshi Kani 		goto out;
957f26c5719SDan Williams 	if (!ti->type->direct_access)
958f26c5719SDan Williams 		goto out;
959f26c5719SDan Williams 	len = max_io_len(sector, ti) / PAGE_SECTORS;
960f26c5719SDan Williams 	if (len < 1)
961f26c5719SDan Williams 		goto out;
962f26c5719SDan Williams 	nr_pages = min(len, nr_pages);
963545ed20eSToshi Kani 	if (ti->type->direct_access)
964817bf402SDan Williams 		ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn);
965817bf402SDan Williams 
966545ed20eSToshi Kani  out:
967545ed20eSToshi Kani 	dm_put_live_table(md, srcu_idx);
968f26c5719SDan Williams 
969f26c5719SDan Williams 	return ret;
970545ed20eSToshi Kani }
971545ed20eSToshi Kani 
9727e026c8cSDan Williams static size_t dm_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
9737e026c8cSDan Williams 		void *addr, size_t bytes, struct iov_iter *i)
9747e026c8cSDan Williams {
9757e026c8cSDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
9767e026c8cSDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
9777e026c8cSDan Williams 	struct dm_target *ti;
9787e026c8cSDan Williams 	long ret = 0;
9797e026c8cSDan Williams 	int srcu_idx;
9807e026c8cSDan Williams 
9817e026c8cSDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
9827e026c8cSDan Williams 
9837e026c8cSDan Williams 	if (!ti)
9847e026c8cSDan Williams 		goto out;
9857e026c8cSDan Williams 	if (!ti->type->dax_copy_from_iter) {
9867e026c8cSDan Williams 		ret = copy_from_iter(addr, bytes, i);
9877e026c8cSDan Williams 		goto out;
9887e026c8cSDan Williams 	}
9897e026c8cSDan Williams 	ret = ti->type->dax_copy_from_iter(ti, pgoff, addr, bytes, i);
9907e026c8cSDan Williams  out:
9917e026c8cSDan Williams 	dm_put_live_table(md, srcu_idx);
9927e026c8cSDan Williams 
9937e026c8cSDan Williams 	return ret;
9947e026c8cSDan Williams }
9957e026c8cSDan Williams 
9961dd40c3eSMikulas Patocka /*
9971dd40c3eSMikulas Patocka  * A target may call dm_accept_partial_bio only from the map routine.  It is
99828a8f0d3SMike Christie  * allowed for all bio types except REQ_PREFLUSH.
9991dd40c3eSMikulas Patocka  *
10001dd40c3eSMikulas Patocka  * dm_accept_partial_bio informs the dm that the target only wants to process
10011dd40c3eSMikulas Patocka  * additional n_sectors sectors of the bio and the rest of the data should be
10021dd40c3eSMikulas Patocka  * sent in a next bio.
10031dd40c3eSMikulas Patocka  *
10041dd40c3eSMikulas Patocka  * A diagram that explains the arithmetics:
10051dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
10061dd40c3eSMikulas Patocka  * |         1          |       2       |   3   |
10071dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
10081dd40c3eSMikulas Patocka  *
10091dd40c3eSMikulas Patocka  * <-------------- *tio->len_ptr --------------->
10101dd40c3eSMikulas Patocka  *                      <------- bi_size ------->
10111dd40c3eSMikulas Patocka  *                      <-- n_sectors -->
10121dd40c3eSMikulas Patocka  *
10131dd40c3eSMikulas Patocka  * Region 1 was already iterated over with bio_advance or similar function.
10141dd40c3eSMikulas Patocka  *	(it may be empty if the target doesn't use bio_advance)
10151dd40c3eSMikulas Patocka  * Region 2 is the remaining bio size that the target wants to process.
10161dd40c3eSMikulas Patocka  *	(it may be empty if region 1 is non-empty, although there is no reason
10171dd40c3eSMikulas Patocka  *	 to make it empty)
10181dd40c3eSMikulas Patocka  * The target requires that region 3 is to be sent in the next bio.
10191dd40c3eSMikulas Patocka  *
10201dd40c3eSMikulas Patocka  * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
10211dd40c3eSMikulas Patocka  * the partially processed part (the sum of regions 1+2) must be the same for all
10221dd40c3eSMikulas Patocka  * copies of the bio.
10231dd40c3eSMikulas Patocka  */
10241dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
10251dd40c3eSMikulas Patocka {
10261dd40c3eSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
10271dd40c3eSMikulas Patocka 	unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
10281eff9d32SJens Axboe 	BUG_ON(bio->bi_opf & REQ_PREFLUSH);
10291dd40c3eSMikulas Patocka 	BUG_ON(bi_size > *tio->len_ptr);
10301dd40c3eSMikulas Patocka 	BUG_ON(n_sectors > bi_size);
10311dd40c3eSMikulas Patocka 	*tio->len_ptr -= bi_size - n_sectors;
10321dd40c3eSMikulas Patocka 	bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
10331dd40c3eSMikulas Patocka }
10341dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
10351dd40c3eSMikulas Patocka 
1036d67a5f4bSMikulas Patocka /*
103710999307SDamien Le Moal  * The zone descriptors obtained with a zone report indicate
103810999307SDamien Le Moal  * zone positions within the target device. The zone descriptors
103910999307SDamien Le Moal  * must be remapped to match their position within the dm device.
104010999307SDamien Le Moal  * A target may call dm_remap_zone_report after completion of a
104110999307SDamien Le Moal  * REQ_OP_ZONE_REPORT bio to remap the zone descriptors obtained
104210999307SDamien Le Moal  * from the target device mapping to the dm device.
104310999307SDamien Le Moal  */
104410999307SDamien Le Moal void dm_remap_zone_report(struct dm_target *ti, struct bio *bio, sector_t start)
104510999307SDamien Le Moal {
104610999307SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
104710999307SDamien Le Moal 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
104810999307SDamien Le Moal 	struct bio *report_bio = tio->io->bio;
104910999307SDamien Le Moal 	struct blk_zone_report_hdr *hdr = NULL;
105010999307SDamien Le Moal 	struct blk_zone *zone;
105110999307SDamien Le Moal 	unsigned int nr_rep = 0;
105210999307SDamien Le Moal 	unsigned int ofst;
105310999307SDamien Le Moal 	struct bio_vec bvec;
105410999307SDamien Le Moal 	struct bvec_iter iter;
105510999307SDamien Le Moal 	void *addr;
105610999307SDamien Le Moal 
105710999307SDamien Le Moal 	if (bio->bi_status)
105810999307SDamien Le Moal 		return;
105910999307SDamien Le Moal 
106010999307SDamien Le Moal 	/*
106110999307SDamien Le Moal 	 * Remap the start sector of the reported zones. For sequential zones,
106210999307SDamien Le Moal 	 * also remap the write pointer position.
106310999307SDamien Le Moal 	 */
106410999307SDamien Le Moal 	bio_for_each_segment(bvec, report_bio, iter) {
106510999307SDamien Le Moal 		addr = kmap_atomic(bvec.bv_page);
106610999307SDamien Le Moal 
106710999307SDamien Le Moal 		/* Remember the report header in the first page */
106810999307SDamien Le Moal 		if (!hdr) {
106910999307SDamien Le Moal 			hdr = addr;
107010999307SDamien Le Moal 			ofst = sizeof(struct blk_zone_report_hdr);
107110999307SDamien Le Moal 		} else
107210999307SDamien Le Moal 			ofst = 0;
107310999307SDamien Le Moal 
107410999307SDamien Le Moal 		/* Set zones start sector */
107510999307SDamien Le Moal 		while (hdr->nr_zones && ofst < bvec.bv_len) {
107610999307SDamien Le Moal 			zone = addr + ofst;
107710999307SDamien Le Moal 			if (zone->start >= start + ti->len) {
107810999307SDamien Le Moal 				hdr->nr_zones = 0;
107910999307SDamien Le Moal 				break;
108010999307SDamien Le Moal 			}
108110999307SDamien Le Moal 			zone->start = zone->start + ti->begin - start;
108210999307SDamien Le Moal 			if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL) {
108310999307SDamien Le Moal 				if (zone->cond == BLK_ZONE_COND_FULL)
108410999307SDamien Le Moal 					zone->wp = zone->start + zone->len;
108510999307SDamien Le Moal 				else if (zone->cond == BLK_ZONE_COND_EMPTY)
108610999307SDamien Le Moal 					zone->wp = zone->start;
108710999307SDamien Le Moal 				else
108810999307SDamien Le Moal 					zone->wp = zone->wp + ti->begin - start;
108910999307SDamien Le Moal 			}
109010999307SDamien Le Moal 			ofst += sizeof(struct blk_zone);
109110999307SDamien Le Moal 			hdr->nr_zones--;
109210999307SDamien Le Moal 			nr_rep++;
109310999307SDamien Le Moal 		}
109410999307SDamien Le Moal 
109510999307SDamien Le Moal 		if (addr != hdr)
109610999307SDamien Le Moal 			kunmap_atomic(addr);
109710999307SDamien Le Moal 
109810999307SDamien Le Moal 		if (!hdr->nr_zones)
109910999307SDamien Le Moal 			break;
110010999307SDamien Le Moal 	}
110110999307SDamien Le Moal 
110210999307SDamien Le Moal 	if (hdr) {
110310999307SDamien Le Moal 		hdr->nr_zones = nr_rep;
110410999307SDamien Le Moal 		kunmap_atomic(hdr);
110510999307SDamien Le Moal 	}
110610999307SDamien Le Moal 
110710999307SDamien Le Moal 	bio_advance(report_bio, report_bio->bi_iter.bi_size);
110810999307SDamien Le Moal 
110910999307SDamien Le Moal #else /* !CONFIG_BLK_DEV_ZONED */
111010999307SDamien Le Moal 	bio->bi_status = BLK_STS_NOTSUPP;
111110999307SDamien Le Moal #endif
111210999307SDamien Le Moal }
111310999307SDamien Le Moal EXPORT_SYMBOL_GPL(dm_remap_zone_report);
111410999307SDamien Le Moal 
111510999307SDamien Le Moal /*
1116d67a5f4bSMikulas Patocka  * Flush current->bio_list when the target map method blocks.
1117d67a5f4bSMikulas Patocka  * This fixes deadlocks in snapshot and possibly in other targets.
1118d67a5f4bSMikulas Patocka  */
1119d67a5f4bSMikulas Patocka struct dm_offload {
1120d67a5f4bSMikulas Patocka 	struct blk_plug plug;
1121d67a5f4bSMikulas Patocka 	struct blk_plug_cb cb;
1122d67a5f4bSMikulas Patocka };
1123d67a5f4bSMikulas Patocka 
1124d67a5f4bSMikulas Patocka static void flush_current_bio_list(struct blk_plug_cb *cb, bool from_schedule)
1125d67a5f4bSMikulas Patocka {
1126d67a5f4bSMikulas Patocka 	struct dm_offload *o = container_of(cb, struct dm_offload, cb);
1127d67a5f4bSMikulas Patocka 	struct bio_list list;
1128d67a5f4bSMikulas Patocka 	struct bio *bio;
1129f5fe1b51SNeilBrown 	int i;
1130d67a5f4bSMikulas Patocka 
1131d67a5f4bSMikulas Patocka 	INIT_LIST_HEAD(&o->cb.list);
1132d67a5f4bSMikulas Patocka 
1133d67a5f4bSMikulas Patocka 	if (unlikely(!current->bio_list))
1134d67a5f4bSMikulas Patocka 		return;
1135d67a5f4bSMikulas Patocka 
1136f5fe1b51SNeilBrown 	for (i = 0; i < 2; i++) {
1137f5fe1b51SNeilBrown 		list = current->bio_list[i];
1138f5fe1b51SNeilBrown 		bio_list_init(&current->bio_list[i]);
1139d67a5f4bSMikulas Patocka 
1140d67a5f4bSMikulas Patocka 		while ((bio = bio_list_pop(&list))) {
1141d67a5f4bSMikulas Patocka 			struct bio_set *bs = bio->bi_pool;
114247e0fb46SNeilBrown 			if (unlikely(!bs) || bs == fs_bio_set ||
114347e0fb46SNeilBrown 			    !bs->rescue_workqueue) {
1144f5fe1b51SNeilBrown 				bio_list_add(&current->bio_list[i], bio);
1145d67a5f4bSMikulas Patocka 				continue;
1146d67a5f4bSMikulas Patocka 			}
1147d67a5f4bSMikulas Patocka 
1148d67a5f4bSMikulas Patocka 			spin_lock(&bs->rescue_lock);
1149d67a5f4bSMikulas Patocka 			bio_list_add(&bs->rescue_list, bio);
1150d67a5f4bSMikulas Patocka 			queue_work(bs->rescue_workqueue, &bs->rescue_work);
1151d67a5f4bSMikulas Patocka 			spin_unlock(&bs->rescue_lock);
1152d67a5f4bSMikulas Patocka 		}
1153d67a5f4bSMikulas Patocka 	}
1154f5fe1b51SNeilBrown }
1155d67a5f4bSMikulas Patocka 
1156d67a5f4bSMikulas Patocka static void dm_offload_start(struct dm_offload *o)
1157d67a5f4bSMikulas Patocka {
1158d67a5f4bSMikulas Patocka 	blk_start_plug(&o->plug);
1159d67a5f4bSMikulas Patocka 	o->cb.callback = flush_current_bio_list;
1160d67a5f4bSMikulas Patocka 	list_add(&o->cb.list, &current->plug->cb_list);
1161d67a5f4bSMikulas Patocka }
1162d67a5f4bSMikulas Patocka 
1163d67a5f4bSMikulas Patocka static void dm_offload_end(struct dm_offload *o)
1164d67a5f4bSMikulas Patocka {
1165d67a5f4bSMikulas Patocka 	list_del(&o->cb.list);
1166d67a5f4bSMikulas Patocka 	blk_finish_plug(&o->plug);
1167d67a5f4bSMikulas Patocka }
1168d67a5f4bSMikulas Patocka 
1169bd2a49b8SAlasdair G Kergon static void __map_bio(struct dm_target_io *tio)
11701da177e4SLinus Torvalds {
11711da177e4SLinus Torvalds 	int r;
11722056a782SJens Axboe 	sector_t sector;
1173d67a5f4bSMikulas Patocka 	struct dm_offload o;
1174dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
1175bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 	/*
11801da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
11811da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
11821da177e4SLinus Torvalds 	 * this io.
11831da177e4SLinus Torvalds 	 */
11841da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
11854f024f37SKent Overstreet 	sector = clone->bi_iter.bi_sector;
1186d67a5f4bSMikulas Patocka 
1187d67a5f4bSMikulas Patocka 	dm_offload_start(&o);
11887de3ee57SMikulas Patocka 	r = ti->type->map(ti, clone);
1189d67a5f4bSMikulas Patocka 	dm_offload_end(&o);
1190d67a5f4bSMikulas Patocka 
1191846785e6SChristoph Hellwig 	switch (r) {
1192846785e6SChristoph Hellwig 	case DM_MAPIO_SUBMITTED:
1193846785e6SChristoph Hellwig 		break;
1194846785e6SChristoph Hellwig 	case DM_MAPIO_REMAPPED:
11951da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
119674d46992SChristoph Hellwig 		trace_block_bio_remap(clone->bi_disk->queue, clone,
119774d46992SChristoph Hellwig 				      bio_dev(tio->io->bio), sector);
11981da177e4SLinus Torvalds 		generic_make_request(clone);
1199846785e6SChristoph Hellwig 		break;
1200846785e6SChristoph Hellwig 	case DM_MAPIO_KILL:
12014e4cbee9SChristoph Hellwig 		dec_pending(tio->io, BLK_STS_IOERR);
12024e4cbee9SChristoph Hellwig 		free_tio(tio);
12034e4cbee9SChristoph Hellwig 		break;
1204846785e6SChristoph Hellwig 	case DM_MAPIO_REQUEUE:
12054e4cbee9SChristoph Hellwig 		dec_pending(tio->io, BLK_STS_DM_REQUEUE);
1206cfae7529SMike Snitzer 		free_tio(tio);
1207846785e6SChristoph Hellwig 		break;
1208846785e6SChristoph Hellwig 	default:
120945cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
121045cbcd79SKiyoshi Ueda 		BUG();
12111da177e4SLinus Torvalds 	}
12121da177e4SLinus Torvalds }
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds struct clone_info {
12151da177e4SLinus Torvalds 	struct mapped_device *md;
12161da177e4SLinus Torvalds 	struct dm_table *map;
12171da177e4SLinus Torvalds 	struct bio *bio;
12181da177e4SLinus Torvalds 	struct dm_io *io;
12191da177e4SLinus Torvalds 	sector_t sector;
1220e0d6609aSMikulas Patocka 	unsigned sector_count;
12211da177e4SLinus Torvalds };
12221da177e4SLinus Torvalds 
1223e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
1224bd2a49b8SAlasdair G Kergon {
12254f024f37SKent Overstreet 	bio->bi_iter.bi_sector = sector;
12264f024f37SKent Overstreet 	bio->bi_iter.bi_size = to_bytes(len);
12271da177e4SLinus Torvalds }
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds /*
12301da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
12311da177e4SLinus Torvalds  */
1232c80914e8SMike Snitzer static int clone_bio(struct dm_target_io *tio, struct bio *bio,
12331c3b13e6SKent Overstreet 		     sector_t sector, unsigned len)
12341da177e4SLinus Torvalds {
1235dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
12361da177e4SLinus Torvalds 
12371c3b13e6SKent Overstreet 	__bio_clone_fast(clone, bio);
12389c47008dSMartin K. Petersen 
1239e2460f2aSMikulas Patocka 	if (unlikely(bio_integrity(bio) != NULL)) {
1240e2460f2aSMikulas Patocka 		int r;
1241e2460f2aSMikulas Patocka 
1242e2460f2aSMikulas Patocka 		if (unlikely(!dm_target_has_integrity(tio->ti->type) &&
1243e2460f2aSMikulas Patocka 			     !dm_target_passes_integrity(tio->ti->type))) {
1244e2460f2aSMikulas Patocka 			DMWARN("%s: the target %s doesn't support integrity data.",
1245e2460f2aSMikulas Patocka 				dm_device_name(tio->io->md),
1246e2460f2aSMikulas Patocka 				tio->ti->type->name);
1247e2460f2aSMikulas Patocka 			return -EIO;
1248e2460f2aSMikulas Patocka 		}
1249e2460f2aSMikulas Patocka 
1250e2460f2aSMikulas Patocka 		r = bio_integrity_clone(clone, bio, GFP_NOIO);
1251c80914e8SMike Snitzer 		if (r < 0)
1252c80914e8SMike Snitzer 			return r;
1253c80914e8SMike Snitzer 	}
12541c3b13e6SKent Overstreet 
1255264c869dSDamien Le Moal 	if (bio_op(bio) != REQ_OP_ZONE_REPORT)
12561c3b13e6SKent Overstreet 		bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
12571c3b13e6SKent Overstreet 	clone->bi_iter.bi_size = to_bytes(len);
12581c3b13e6SKent Overstreet 
1259e2460f2aSMikulas Patocka 	if (unlikely(bio_integrity(bio) != NULL))
1260fbd08e76SDmitry Monakhov 		bio_integrity_trim(clone);
1261c80914e8SMike Snitzer 
1262c80914e8SMike Snitzer 	return 0;
12631da177e4SLinus Torvalds }
12641da177e4SLinus Torvalds 
12659015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci,
126699778273SJunichi Nomura 				      struct dm_target *ti,
126755a62eefSAlasdair G Kergon 				      unsigned target_bio_nr)
1268f9ab94ceSMikulas Patocka {
1269dba14160SMikulas Patocka 	struct dm_target_io *tio;
1270dba14160SMikulas Patocka 	struct bio *clone;
1271dba14160SMikulas Patocka 
127299778273SJunichi Nomura 	clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1273dba14160SMikulas Patocka 	tio = container_of(clone, struct dm_target_io, clone);
1274f9ab94ceSMikulas Patocka 
1275f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1276f9ab94ceSMikulas Patocka 	tio->ti = ti;
127755a62eefSAlasdair G Kergon 	tio->target_bio_nr = target_bio_nr;
12789015df24SAlasdair G Kergon 
12799015df24SAlasdair G Kergon 	return tio;
12809015df24SAlasdair G Kergon }
12819015df24SAlasdair G Kergon 
128214fe594dSAlasdair G Kergon static void __clone_and_map_simple_bio(struct clone_info *ci,
128314fe594dSAlasdair G Kergon 				       struct dm_target *ti,
12841dd40c3eSMikulas Patocka 				       unsigned target_bio_nr, unsigned *len)
12859015df24SAlasdair G Kergon {
128699778273SJunichi Nomura 	struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
1287dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
12889015df24SAlasdair G Kergon 
12891dd40c3eSMikulas Patocka 	tio->len_ptr = len;
12901dd40c3eSMikulas Patocka 
12911c3b13e6SKent Overstreet 	__bio_clone_fast(clone, ci->bio);
1292bd2a49b8SAlasdair G Kergon 	if (len)
12931dd40c3eSMikulas Patocka 		bio_setup_sector(clone, ci->sector, *len);
1294f9ab94ceSMikulas Patocka 
1295bd2a49b8SAlasdair G Kergon 	__map_bio(tio);
1296f9ab94ceSMikulas Patocka }
1297f9ab94ceSMikulas Patocka 
129814fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
12991dd40c3eSMikulas Patocka 				  unsigned num_bios, unsigned *len)
130006a426ceSMike Snitzer {
130155a62eefSAlasdair G Kergon 	unsigned target_bio_nr;
130206a426ceSMike Snitzer 
130355a62eefSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
130414fe594dSAlasdair G Kergon 		__clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
130506a426ceSMike Snitzer }
130606a426ceSMike Snitzer 
130714fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1308f9ab94ceSMikulas Patocka {
130906a426ceSMike Snitzer 	unsigned target_nr = 0;
1310f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1311f9ab94ceSMikulas Patocka 
1312b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1313f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
13141dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
1315f9ab94ceSMikulas Patocka 
1316f9ab94ceSMikulas Patocka 	return 0;
1317f9ab94ceSMikulas Patocka }
1318f9ab94ceSMikulas Patocka 
1319c80914e8SMike Snitzer static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
13201dd40c3eSMikulas Patocka 				     sector_t sector, unsigned *len)
13215ae89a87SMike Snitzer {
1322dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
13235ae89a87SMike Snitzer 	struct dm_target_io *tio;
1324b0d8ed4dSAlasdair G Kergon 	unsigned target_bio_nr;
1325b0d8ed4dSAlasdair G Kergon 	unsigned num_target_bios = 1;
1326c80914e8SMike Snitzer 	int r = 0;
13275ae89a87SMike Snitzer 
1328b0d8ed4dSAlasdair G Kergon 	/*
1329b0d8ed4dSAlasdair G Kergon 	 * Does the target want to receive duplicate copies of the bio?
1330b0d8ed4dSAlasdair G Kergon 	 */
1331b0d8ed4dSAlasdair G Kergon 	if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1332b0d8ed4dSAlasdair G Kergon 		num_target_bios = ti->num_write_bios(ti, bio);
1333e4c93811SAlasdair G Kergon 
1334b0d8ed4dSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
133599778273SJunichi Nomura 		tio = alloc_tio(ci, ti, target_bio_nr);
13361dd40c3eSMikulas Patocka 		tio->len_ptr = len;
1337c80914e8SMike Snitzer 		r = clone_bio(tio, bio, sector, *len);
1338072623deSMikulas Patocka 		if (r < 0) {
1339cfae7529SMike Snitzer 			free_tio(tio);
1340c80914e8SMike Snitzer 			break;
1341072623deSMikulas Patocka 		}
1342bd2a49b8SAlasdair G Kergon 		__map_bio(tio);
13435ae89a87SMike Snitzer 	}
1344c80914e8SMike Snitzer 
1345c80914e8SMike Snitzer 	return r;
1346b0d8ed4dSAlasdair G Kergon }
13475ae89a87SMike Snitzer 
134855a62eefSAlasdair G Kergon typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
134923508a96SMike Snitzer 
135055a62eefSAlasdair G Kergon static unsigned get_num_discard_bios(struct dm_target *ti)
135123508a96SMike Snitzer {
135255a62eefSAlasdair G Kergon 	return ti->num_discard_bios;
135323508a96SMike Snitzer }
135423508a96SMike Snitzer 
135555a62eefSAlasdair G Kergon static unsigned get_num_write_same_bios(struct dm_target *ti)
135623508a96SMike Snitzer {
135755a62eefSAlasdair G Kergon 	return ti->num_write_same_bios;
135823508a96SMike Snitzer }
135923508a96SMike Snitzer 
1360ac62d620SChristoph Hellwig static unsigned get_num_write_zeroes_bios(struct dm_target *ti)
1361ac62d620SChristoph Hellwig {
1362ac62d620SChristoph Hellwig 	return ti->num_write_zeroes_bios;
1363ac62d620SChristoph Hellwig }
1364ac62d620SChristoph Hellwig 
136523508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti);
136623508a96SMike Snitzer 
136723508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti)
136823508a96SMike Snitzer {
136955a62eefSAlasdair G Kergon 	return ti->split_discard_bios;
137023508a96SMike Snitzer }
137123508a96SMike Snitzer 
137214fe594dSAlasdair G Kergon static int __send_changing_extent_only(struct clone_info *ci,
137355a62eefSAlasdair G Kergon 				       get_num_bios_fn get_num_bios,
137423508a96SMike Snitzer 				       is_split_required_fn is_split_required)
13755ae89a87SMike Snitzer {
13765ae89a87SMike Snitzer 	struct dm_target *ti;
1377e0d6609aSMikulas Patocka 	unsigned len;
137855a62eefSAlasdair G Kergon 	unsigned num_bios;
13795ae89a87SMike Snitzer 
1380a79245b3SMike Snitzer 	do {
13815ae89a87SMike Snitzer 		ti = dm_table_find_target(ci->map, ci->sector);
13825ae89a87SMike Snitzer 		if (!dm_target_is_valid(ti))
13835ae89a87SMike Snitzer 			return -EIO;
13845ae89a87SMike Snitzer 
13855ae89a87SMike Snitzer 		/*
138623508a96SMike Snitzer 		 * Even though the device advertised support for this type of
138723508a96SMike Snitzer 		 * request, that does not mean every target supports it, and
1388936688d7SMike Snitzer 		 * reconfiguration might also have changed that since the
13895ae89a87SMike Snitzer 		 * check was performed.
13905ae89a87SMike Snitzer 		 */
139155a62eefSAlasdair G Kergon 		num_bios = get_num_bios ? get_num_bios(ti) : 0;
139255a62eefSAlasdair G Kergon 		if (!num_bios)
13935ae89a87SMike Snitzer 			return -EOPNOTSUPP;
13945ae89a87SMike Snitzer 
139523508a96SMike Snitzer 		if (is_split_required && !is_split_required(ti))
1396e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
13977acf0277SMikulas Patocka 		else
1398e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
13995ae89a87SMike Snitzer 
14001dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, num_bios, &len);
14015ae89a87SMike Snitzer 
1402a79245b3SMike Snitzer 		ci->sector += len;
1403a79245b3SMike Snitzer 	} while (ci->sector_count -= len);
14045ae89a87SMike Snitzer 
14055ae89a87SMike Snitzer 	return 0;
14065ae89a87SMike Snitzer }
14075ae89a87SMike Snitzer 
140814fe594dSAlasdair G Kergon static int __send_discard(struct clone_info *ci)
140923508a96SMike Snitzer {
141014fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_discard_bios,
141123508a96SMike Snitzer 					   is_split_required_for_discard);
141223508a96SMike Snitzer }
141323508a96SMike Snitzer 
141414fe594dSAlasdair G Kergon static int __send_write_same(struct clone_info *ci)
141523508a96SMike Snitzer {
141614fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
141723508a96SMike Snitzer }
141823508a96SMike Snitzer 
1419ac62d620SChristoph Hellwig static int __send_write_zeroes(struct clone_info *ci)
1420ac62d620SChristoph Hellwig {
1421ac62d620SChristoph Hellwig 	return __send_changing_extent_only(ci, get_num_write_zeroes_bios, NULL);
1422ac62d620SChristoph Hellwig }
1423ac62d620SChristoph Hellwig 
1424e4c93811SAlasdair G Kergon /*
1425e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1426e4c93811SAlasdair G Kergon  */
1427e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci)
1428e4c93811SAlasdair G Kergon {
1429e4c93811SAlasdair G Kergon 	struct bio *bio = ci->bio;
1430e4c93811SAlasdair G Kergon 	struct dm_target *ti;
14311c3b13e6SKent Overstreet 	unsigned len;
1432c80914e8SMike Snitzer 	int r;
1433e4c93811SAlasdair G Kergon 
1434e6047149SMike Christie 	if (unlikely(bio_op(bio) == REQ_OP_DISCARD))
1435e4c93811SAlasdair G Kergon 		return __send_discard(ci);
1436e6047149SMike Christie 	else if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
1437e4c93811SAlasdair G Kergon 		return __send_write_same(ci);
1438ac62d620SChristoph Hellwig 	else if (unlikely(bio_op(bio) == REQ_OP_WRITE_ZEROES))
1439ac62d620SChristoph Hellwig 		return __send_write_zeroes(ci);
1440e4c93811SAlasdair G Kergon 
1441e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1442e4c93811SAlasdair G Kergon 	if (!dm_target_is_valid(ti))
1443e4c93811SAlasdair G Kergon 		return -EIO;
1444e4c93811SAlasdair G Kergon 
1445264c869dSDamien Le Moal 	if (bio_op(bio) == REQ_OP_ZONE_REPORT)
1446264c869dSDamien Le Moal 		len = ci->sector_count;
1447264c869dSDamien Le Moal 	else
1448264c869dSDamien Le Moal 		len = min_t(sector_t, max_io_len(ci->sector, ti),
1449264c869dSDamien Le Moal 			    ci->sector_count);
1450e4c93811SAlasdair G Kergon 
1451c80914e8SMike Snitzer 	r = __clone_and_map_data_bio(ci, ti, ci->sector, &len);
1452c80914e8SMike Snitzer 	if (r < 0)
1453c80914e8SMike Snitzer 		return r;
1454e4c93811SAlasdair G Kergon 
1455e4c93811SAlasdair G Kergon 	ci->sector += len;
1456e4c93811SAlasdair G Kergon 	ci->sector_count -= len;
1457e4c93811SAlasdair G Kergon 
1458e4c93811SAlasdair G Kergon 	return 0;
1459e4c93811SAlasdair G Kergon }
1460e4c93811SAlasdair G Kergon 
1461e4c93811SAlasdair G Kergon /*
146214fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
14631da177e4SLinus Torvalds  */
146483d5e5b0SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md,
146583d5e5b0SMikulas Patocka 				    struct dm_table *map, struct bio *bio)
14661da177e4SLinus Torvalds {
14671da177e4SLinus Torvalds 	struct clone_info ci;
1468512875bdSJun'ichi Nomura 	int error = 0;
14691da177e4SLinus Torvalds 
147083d5e5b0SMikulas Patocka 	if (unlikely(!map)) {
1471f0b9a450SMikulas Patocka 		bio_io_error(bio);
1472f0b9a450SMikulas Patocka 		return;
1473f0b9a450SMikulas Patocka 	}
1474692d0eb9SMikulas Patocka 
147583d5e5b0SMikulas Patocka 	ci.map = map;
14761da177e4SLinus Torvalds 	ci.md = md;
14771da177e4SLinus Torvalds 	ci.io = alloc_io(md);
14784e4cbee9SChristoph Hellwig 	ci.io->status = 0;
14791da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
14801da177e4SLinus Torvalds 	ci.io->bio = bio;
14811da177e4SLinus Torvalds 	ci.io->md = md;
1482f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
14834f024f37SKent Overstreet 	ci.sector = bio->bi_iter.bi_sector;
14841da177e4SLinus Torvalds 
14853eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1486bd2a49b8SAlasdair G Kergon 
14871eff9d32SJens Axboe 	if (bio->bi_opf & REQ_PREFLUSH) {
1488b372d360SMike Snitzer 		ci.bio = &ci.md->flush_bio;
1489b372d360SMike Snitzer 		ci.sector_count = 0;
149014fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1491b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1492a4aa5e56SDamien Le Moal 	} else if (bio_op(bio) == REQ_OP_ZONE_RESET) {
1493a4aa5e56SDamien Le Moal 		ci.bio = bio;
1494a4aa5e56SDamien Le Moal 		ci.sector_count = 0;
1495a4aa5e56SDamien Le Moal 		error = __split_and_process_non_flush(&ci);
1496b372d360SMike Snitzer 	} else {
14976a8736d1STejun Heo 		ci.bio = bio;
1498f6fccb12SMilan Broz 		ci.sector_count = bio_sectors(bio);
1499512875bdSJun'ichi Nomura 		while (ci.sector_count && !error)
150014fe594dSAlasdair G Kergon 			error = __split_and_process_non_flush(&ci);
1501d87f4c14STejun Heo 	}
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds 	/* drop the extra reference count */
150454385bf7SBart Van Assche 	dec_pending(ci.io, errno_to_blk_status(error));
15059e4e5f87SMilan Broz }
15069e4e5f87SMilan Broz /*-----------------------------------------------------------------
15071da177e4SLinus Torvalds  * CRUD END
15081da177e4SLinus Torvalds  *---------------------------------------------------------------*/
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds /*
15111da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
15121da177e4SLinus Torvalds  * dm_merge_bvec.
15131da177e4SLinus Torvalds  */
1514dece1635SJens Axboe static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
15151da177e4SLinus Torvalds {
151612f03a49SKevin Corry 	int rw = bio_data_dir(bio);
15171da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
151883d5e5b0SMikulas Patocka 	int srcu_idx;
151983d5e5b0SMikulas Patocka 	struct dm_table *map;
15201da177e4SLinus Torvalds 
152183d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
15221da177e4SLinus Torvalds 
1523d62e26b3SJens Axboe 	generic_start_io_acct(q, rw, bio_sectors(bio), &dm_disk(md)->part0);
152412f03a49SKevin Corry 
15256a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
15266a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
152783d5e5b0SMikulas Patocka 		dm_put_live_table(md, srcu_idx);
15281da177e4SLinus Torvalds 
15291eff9d32SJens Axboe 		if (!(bio->bi_opf & REQ_RAHEAD))
153092c63902SMikulas Patocka 			queue_io(md, bio);
15316a8736d1STejun Heo 		else
15326a8736d1STejun Heo 			bio_io_error(bio);
1533dece1635SJens Axboe 		return BLK_QC_T_NONE;
15341da177e4SLinus Torvalds 	}
15351da177e4SLinus Torvalds 
153683d5e5b0SMikulas Patocka 	__split_and_process_bio(md, map, bio);
153783d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
1538dece1635SJens Axboe 	return BLK_QC_T_NONE;
1539cec47e3dSKiyoshi Ueda }
1540cec47e3dSKiyoshi Ueda 
15411da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
15421da177e4SLinus Torvalds {
15438a57dfc6SChandra Seetharaman 	int r = bdi_bits;
15448a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
15458a57dfc6SChandra Seetharaman 	struct dm_table *map;
15461da177e4SLinus Torvalds 
15471eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
1548e522c039SMike Snitzer 		if (dm_request_based(md)) {
1549cec47e3dSKiyoshi Ueda 			/*
1550e522c039SMike Snitzer 			 * With request-based DM we only need to check the
1551e522c039SMike Snitzer 			 * top-level queue for congestion.
1552cec47e3dSKiyoshi Ueda 			 */
1553dc3b17ccSJan Kara 			r = md->queue->backing_dev_info->wb.state & bdi_bits;
1554e522c039SMike Snitzer 		} else {
1555e522c039SMike Snitzer 			map = dm_get_live_table_fast(md);
1556e522c039SMike Snitzer 			if (map)
15571da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
155883d5e5b0SMikulas Patocka 			dm_put_live_table_fast(md);
15598a57dfc6SChandra Seetharaman 		}
1560e522c039SMike Snitzer 	}
15618a57dfc6SChandra Seetharaman 
15621da177e4SLinus Torvalds 	return r;
15631da177e4SLinus Torvalds }
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds /*-----------------------------------------------------------------
15661da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
15671da177e4SLinus Torvalds  *---------------------------------------------------------------*/
15682b06cfffSAlasdair G Kergon static void free_minor(int minor)
15691da177e4SLinus Torvalds {
1570f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
15711da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1572f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
15731da177e4SLinus Torvalds }
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds /*
15761da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
15771da177e4SLinus Torvalds  */
1578cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
15791da177e4SLinus Torvalds {
1580c9d76be6STejun Heo 	int r;
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
15831da177e4SLinus Torvalds 		return -EINVAL;
15841da177e4SLinus Torvalds 
1585c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1586f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
15871da177e4SLinus Torvalds 
1588c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
15891da177e4SLinus Torvalds 
1590f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1591c9d76be6STejun Heo 	idr_preload_end();
1592c9d76be6STejun Heo 	if (r < 0)
1593c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
1594c9d76be6STejun Heo 	return 0;
15951da177e4SLinus Torvalds }
15961da177e4SLinus Torvalds 
1597cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
15981da177e4SLinus Torvalds {
1599c9d76be6STejun Heo 	int r;
16001da177e4SLinus Torvalds 
1601c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1602f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
16031da177e4SLinus Torvalds 
1604c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
16051da177e4SLinus Torvalds 
1606f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1607c9d76be6STejun Heo 	idr_preload_end();
1608c9d76be6STejun Heo 	if (r < 0)
16091da177e4SLinus Torvalds 		return r;
1610c9d76be6STejun Heo 	*minor = r;
1611c9d76be6STejun Heo 	return 0;
16121da177e4SLinus Torvalds }
16131da177e4SLinus Torvalds 
161483d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
1615f26c5719SDan Williams static const struct dax_operations dm_dax_ops;
16161da177e4SLinus Torvalds 
161753d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
161853d5914fSMikulas Patocka 
16194cc96131SMike Snitzer void dm_init_md_queue(struct mapped_device *md)
16204a0b4ddfSMike Snitzer {
16214a0b4ddfSMike Snitzer 	/*
16224a0b4ddfSMike Snitzer 	 * Request-based dm devices cannot be stacked on top of bio-based dm
1623bfebd1cdSMike Snitzer 	 * devices.  The type of this dm device may not have been decided yet.
16244a0b4ddfSMike Snitzer 	 * The type is decided at the first table loading time.
16254a0b4ddfSMike Snitzer 	 * To prevent problematic device stacking, clear the queue flag
16264a0b4ddfSMike Snitzer 	 * for request stacking support until then.
16274a0b4ddfSMike Snitzer 	 *
16284a0b4ddfSMike Snitzer 	 * This queue is new, so no concurrency on the queue_flags.
16294a0b4ddfSMike Snitzer 	 */
16304a0b4ddfSMike Snitzer 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1631ad5f498fSMikulas Patocka 
1632ad5f498fSMikulas Patocka 	/*
1633ad5f498fSMikulas Patocka 	 * Initialize data that will only be used by a non-blk-mq DM queue
1634ad5f498fSMikulas Patocka 	 * - must do so here (in alloc_dev callchain) before queue is used
1635ad5f498fSMikulas Patocka 	 */
1636ad5f498fSMikulas Patocka 	md->queue->queuedata = md;
1637dc3b17ccSJan Kara 	md->queue->backing_dev_info->congested_data = md;
1638bfebd1cdSMike Snitzer }
16394a0b4ddfSMike Snitzer 
16404cc96131SMike Snitzer void dm_init_normal_md_queue(struct mapped_device *md)
1641bfebd1cdSMike Snitzer {
164217e149b8SMike Snitzer 	md->use_blk_mq = false;
1643bfebd1cdSMike Snitzer 	dm_init_md_queue(md);
1644bfebd1cdSMike Snitzer 
1645bfebd1cdSMike Snitzer 	/*
1646bfebd1cdSMike Snitzer 	 * Initialize aspects of queue that aren't relevant for blk-mq
1647bfebd1cdSMike Snitzer 	 */
1648dc3b17ccSJan Kara 	md->queue->backing_dev_info->congested_fn = dm_any_congested;
16494a0b4ddfSMike Snitzer }
16504a0b4ddfSMike Snitzer 
16510f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md)
16520f20972fSMike Snitzer {
16530f20972fSMike Snitzer 	if (md->wq)
16540f20972fSMike Snitzer 		destroy_workqueue(md->wq);
16550f20972fSMike Snitzer 	if (md->kworker_task)
16560f20972fSMike Snitzer 		kthread_stop(md->kworker_task);
16570f20972fSMike Snitzer 	mempool_destroy(md->io_pool);
16580f20972fSMike Snitzer 	if (md->bs)
16590f20972fSMike Snitzer 		bioset_free(md->bs);
16600f20972fSMike Snitzer 
1661f26c5719SDan Williams 	if (md->dax_dev) {
1662f26c5719SDan Williams 		kill_dax(md->dax_dev);
1663f26c5719SDan Williams 		put_dax(md->dax_dev);
1664f26c5719SDan Williams 		md->dax_dev = NULL;
1665f26c5719SDan Williams 	}
1666f26c5719SDan Williams 
16670f20972fSMike Snitzer 	if (md->disk) {
16680f20972fSMike Snitzer 		spin_lock(&_minor_lock);
16690f20972fSMike Snitzer 		md->disk->private_data = NULL;
16700f20972fSMike Snitzer 		spin_unlock(&_minor_lock);
16710f20972fSMike Snitzer 		del_gendisk(md->disk);
16720f20972fSMike Snitzer 		put_disk(md->disk);
16730f20972fSMike Snitzer 	}
16740f20972fSMike Snitzer 
16750f20972fSMike Snitzer 	if (md->queue)
16760f20972fSMike Snitzer 		blk_cleanup_queue(md->queue);
16770f20972fSMike Snitzer 
1678d09960b0STahsin Erdogan 	cleanup_srcu_struct(&md->io_barrier);
1679d09960b0STahsin Erdogan 
16800f20972fSMike Snitzer 	if (md->bdev) {
16810f20972fSMike Snitzer 		bdput(md->bdev);
16820f20972fSMike Snitzer 		md->bdev = NULL;
16830f20972fSMike Snitzer 	}
16844cc96131SMike Snitzer 
16854cc96131SMike Snitzer 	dm_mq_cleanup_mapped_device(md);
16860f20972fSMike Snitzer }
16870f20972fSMike Snitzer 
16881da177e4SLinus Torvalds /*
16891da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
16901da177e4SLinus Torvalds  */
16912b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
16921da177e4SLinus Torvalds {
1693115485e8SMike Snitzer 	int r, numa_node_id = dm_get_numa_node();
1694f26c5719SDan Williams 	struct dax_device *dax_dev;
1695115485e8SMike Snitzer 	struct mapped_device *md;
1696ba61fdd1SJeff Mahoney 	void *old_md;
16971da177e4SLinus Torvalds 
1698115485e8SMike Snitzer 	md = kzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
16991da177e4SLinus Torvalds 	if (!md) {
17001da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
17011da177e4SLinus Torvalds 		return NULL;
17021da177e4SLinus Torvalds 	}
17031da177e4SLinus Torvalds 
170410da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
17056ed7ade8SMilan Broz 		goto bad_module_get;
170610da4f79SJeff Mahoney 
17071da177e4SLinus Torvalds 	/* get a minor number for the dev */
17082b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1709cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
17102b06cfffSAlasdair G Kergon 	else
1711cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
17121da177e4SLinus Torvalds 	if (r < 0)
17136ed7ade8SMilan Broz 		goto bad_minor;
17141da177e4SLinus Torvalds 
171583d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
171683d5e5b0SMikulas Patocka 	if (r < 0)
171783d5e5b0SMikulas Patocka 		goto bad_io_barrier;
171883d5e5b0SMikulas Patocka 
1719115485e8SMike Snitzer 	md->numa_node_id = numa_node_id;
17204cc96131SMike Snitzer 	md->use_blk_mq = dm_use_blk_mq_default();
1721591ddcfcSMike Snitzer 	md->init_tio_pdu = false;
1722a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
1723e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1724a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
172586f1152bSBenjamin Marzinski 	mutex_init(&md->table_devices_lock);
1726022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
17271da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
17285c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
17291da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
17307a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
17317a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
173286f1152bSBenjamin Marzinski 	INIT_LIST_HEAD(&md->table_devices);
17337a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
17341da177e4SLinus Torvalds 
1735115485e8SMike Snitzer 	md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id);
17361da177e4SLinus Torvalds 	if (!md->queue)
17370f20972fSMike Snitzer 		goto bad;
17381da177e4SLinus Torvalds 
17394a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
17409faf400fSStefan Bader 
1741115485e8SMike Snitzer 	md->disk = alloc_disk_node(1, numa_node_id);
17421da177e4SLinus Torvalds 	if (!md->disk)
17430f20972fSMike Snitzer 		goto bad;
17441da177e4SLinus Torvalds 
1745316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
1746316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
1747f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
174853d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1749f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
17502995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
17512eb6e1e3SKeith Busch 	md->kworker_task = NULL;
1752f0b04115SJeff Mahoney 
17531da177e4SLinus Torvalds 	md->disk->major = _major;
17541da177e4SLinus Torvalds 	md->disk->first_minor = minor;
17551da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
17561da177e4SLinus Torvalds 	md->disk->queue = md->queue;
17571da177e4SLinus Torvalds 	md->disk->private_data = md;
17581da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
1759f26c5719SDan Williams 
1760f26c5719SDan Williams 	dax_dev = alloc_dax(md, md->disk->disk_name, &dm_dax_ops);
1761f26c5719SDan Williams 	if (!dax_dev)
1762f26c5719SDan Williams 		goto bad;
1763f26c5719SDan Williams 	md->dax_dev = dax_dev;
1764f26c5719SDan Williams 
17651da177e4SLinus Torvalds 	add_disk(md->disk);
17667e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
17671da177e4SLinus Torvalds 
1768670368a8STejun Heo 	md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
1769304f3f6aSMilan Broz 	if (!md->wq)
17700f20972fSMike Snitzer 		goto bad;
1771304f3f6aSMilan Broz 
177232a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
177332a926daSMikulas Patocka 	if (!md->bdev)
17740f20972fSMike Snitzer 		goto bad;
177532a926daSMikulas Patocka 
17763a83f467SMing Lei 	bio_init(&md->flush_bio, NULL, 0);
177774d46992SChristoph Hellwig 	bio_set_dev(&md->flush_bio, md->bdev);
1778ff0361b3SJan Kara 	md->flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
17796a8736d1STejun Heo 
1780fd2ed4d2SMikulas Patocka 	dm_stats_init(&md->stats);
1781fd2ed4d2SMikulas Patocka 
1782ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1783f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1784ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1785f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1786ba61fdd1SJeff Mahoney 
1787ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1788ba61fdd1SJeff Mahoney 
17891da177e4SLinus Torvalds 	return md;
17901da177e4SLinus Torvalds 
17910f20972fSMike Snitzer bad:
17920f20972fSMike Snitzer 	cleanup_mapped_device(md);
179383d5e5b0SMikulas Patocka bad_io_barrier:
17941da177e4SLinus Torvalds 	free_minor(minor);
17956ed7ade8SMilan Broz bad_minor:
179610da4f79SJeff Mahoney 	module_put(THIS_MODULE);
17976ed7ade8SMilan Broz bad_module_get:
17981da177e4SLinus Torvalds 	kfree(md);
17991da177e4SLinus Torvalds 	return NULL;
18001da177e4SLinus Torvalds }
18011da177e4SLinus Torvalds 
1802ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1803ae9da83fSJun'ichi Nomura 
18041da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
18051da177e4SLinus Torvalds {
1806f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
180763d94e48SJun'ichi Nomura 
1808ae9da83fSJun'ichi Nomura 	unlock_fs(md);
18092eb6e1e3SKeith Busch 
18100f20972fSMike Snitzer 	cleanup_mapped_device(md);
18110f20972fSMike Snitzer 
18120f20972fSMike Snitzer 	free_table_devices(&md->table_devices);
18130f20972fSMike Snitzer 	dm_stats_cleanup(&md->stats);
181463a4f065SMike Snitzer 	free_minor(minor);
181563a4f065SMike Snitzer 
181610da4f79SJeff Mahoney 	module_put(THIS_MODULE);
18171da177e4SLinus Torvalds 	kfree(md);
18181da177e4SLinus Torvalds }
18191da177e4SLinus Torvalds 
1820e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1821e6ee8c0bSKiyoshi Ueda {
1822c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
1823e6ee8c0bSKiyoshi Ueda 
18244e6e36c3SMike Snitzer 	if (md->bs) {
18254e6e36c3SMike Snitzer 		/* The md already has necessary mempools. */
1826545ed20eSToshi Kani 		if (dm_table_bio_based(t)) {
1827c0820cf5SMikulas Patocka 			/*
182816245bdcSJun'ichi Nomura 			 * Reload bioset because front_pad may have changed
182916245bdcSJun'ichi Nomura 			 * because a different table was loaded.
1830c0820cf5SMikulas Patocka 			 */
1831c0820cf5SMikulas Patocka 			bioset_free(md->bs);
1832c0820cf5SMikulas Patocka 			md->bs = p->bs;
1833c0820cf5SMikulas Patocka 			p->bs = NULL;
1834c0820cf5SMikulas Patocka 		}
1835cbc4e3c1SMike Snitzer 		/*
18364e6e36c3SMike Snitzer 		 * There's no need to reload with request-based dm
18374e6e36c3SMike Snitzer 		 * because the size of front_pad doesn't change.
18384e6e36c3SMike Snitzer 		 * Note for future: If you are to reload bioset,
18394e6e36c3SMike Snitzer 		 * prep-ed requests in the queue may refer
18404e6e36c3SMike Snitzer 		 * to bio from the old bioset, so you must walk
18414e6e36c3SMike Snitzer 		 * through the queue to unprep.
1842cbc4e3c1SMike Snitzer 		 */
1843cbc4e3c1SMike Snitzer 		goto out;
1844cbc4e3c1SMike Snitzer 	}
1845cbc4e3c1SMike Snitzer 
1846eb8db831SChristoph Hellwig 	BUG_ON(!p || md->io_pool || md->bs);
1847e6ee8c0bSKiyoshi Ueda 
1848e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
1849e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
1850e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
1851e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
18524e6e36c3SMike Snitzer 
1853e6ee8c0bSKiyoshi Ueda out:
185402233342SMike Snitzer 	/* mempool bind completed, no longer need any mempools in the table */
1855e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
1856e6ee8c0bSKiyoshi Ueda }
1857e6ee8c0bSKiyoshi Ueda 
18581da177e4SLinus Torvalds /*
18591da177e4SLinus Torvalds  * Bind a table to the device.
18601da177e4SLinus Torvalds  */
18611da177e4SLinus Torvalds static void event_callback(void *context)
18621da177e4SLinus Torvalds {
18637a8c3d3bSMike Anderson 	unsigned long flags;
18647a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
18651da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
18661da177e4SLinus Torvalds 
18677a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
18687a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
18697a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
18707a8c3d3bSMike Anderson 
1871ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
18727a8c3d3bSMike Anderson 
18731da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
18741da177e4SLinus Torvalds 	wake_up(&md->eventq);
187562e08243SMikulas Patocka 	dm_issue_global_event();
18761da177e4SLinus Torvalds }
18771da177e4SLinus Torvalds 
1878c217649bSMike Snitzer /*
1879c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
1880c217649bSMike Snitzer  */
18814e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
18821da177e4SLinus Torvalds {
18831ea0654eSBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
18841ea0654eSBart Van Assche 
18854e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
18861da177e4SLinus Torvalds 
1887db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
18881da177e4SLinus Torvalds }
18891da177e4SLinus Torvalds 
1890042d2a9bSAlasdair G Kergon /*
1891042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
1892042d2a9bSAlasdair G Kergon  */
1893042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
1894754c5fc7SMike Snitzer 			       struct queue_limits *limits)
18951da177e4SLinus Torvalds {
1896042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
1897165125e1SJens Axboe 	struct request_queue *q = md->queue;
18981da177e4SLinus Torvalds 	sector_t size;
18991da177e4SLinus Torvalds 
19005a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
19015a8f1f80SBart Van Assche 
19021da177e4SLinus Torvalds 	size = dm_table_get_size(t);
19033ac51e74SDarrick J. Wong 
19043ac51e74SDarrick J. Wong 	/*
19053ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
19063ac51e74SDarrick J. Wong 	 */
1907fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
19083ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
19093ac51e74SDarrick J. Wong 
19104e90188bSAlasdair G Kergon 	__set_size(md, size);
19111da177e4SLinus Torvalds 
1912cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
19132ca3310eSAlasdair G Kergon 
1914e6ee8c0bSKiyoshi Ueda 	/*
1915e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
1916e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
1917e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
1918e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
1919e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
1920e6ee8c0bSKiyoshi Ueda 	 */
192116f12266SMike Snitzer 	if (dm_table_request_based(t)) {
1922eca7ee6dSMike Snitzer 		dm_stop_queue(q);
192316f12266SMike Snitzer 		/*
192416f12266SMike Snitzer 		 * Leverage the fact that request-based DM targets are
192516f12266SMike Snitzer 		 * immutable singletons and establish md->immutable_target
192616f12266SMike Snitzer 		 * - used to optimize both dm_request_fn and dm_mq_queue_rq
192716f12266SMike Snitzer 		 */
192816f12266SMike Snitzer 		md->immutable_target = dm_table_get_immutable_target(t);
192916f12266SMike Snitzer 	}
1930e6ee8c0bSKiyoshi Ueda 
1931e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
1932e6ee8c0bSKiyoshi Ueda 
1933a12f5d48SEric Dumazet 	old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
19341d3aa6f6SMike Snitzer 	rcu_assign_pointer(md->map, (void *)t);
193536a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
193636a0456fSAlasdair G Kergon 
1937754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
193841abc4e1SHannes Reinecke 	if (old_map)
193983d5e5b0SMikulas Patocka 		dm_sync_table(md);
19402ca3310eSAlasdair G Kergon 
1941042d2a9bSAlasdair G Kergon 	return old_map;
19421da177e4SLinus Torvalds }
19431da177e4SLinus Torvalds 
1944a7940155SAlasdair G Kergon /*
1945a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
1946a7940155SAlasdair G Kergon  */
1947a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
19481da177e4SLinus Torvalds {
1949a12f5d48SEric Dumazet 	struct dm_table *map = rcu_dereference_protected(md->map, 1);
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds 	if (!map)
1952a7940155SAlasdair G Kergon 		return NULL;
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
19559cdb8520SMonam Agarwal 	RCU_INIT_POINTER(md->map, NULL);
195683d5e5b0SMikulas Patocka 	dm_sync_table(md);
1957a7940155SAlasdair G Kergon 
1958a7940155SAlasdair G Kergon 	return map;
19591da177e4SLinus Torvalds }
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds /*
19621da177e4SLinus Torvalds  * Constructor for a new device.
19631da177e4SLinus Torvalds  */
19642b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
19651da177e4SLinus Torvalds {
19661da177e4SLinus Torvalds 	struct mapped_device *md;
19671da177e4SLinus Torvalds 
19682b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
19691da177e4SLinus Torvalds 	if (!md)
19701da177e4SLinus Torvalds 		return -ENXIO;
19711da177e4SLinus Torvalds 
1972784aae73SMilan Broz 	dm_sysfs_init(md);
1973784aae73SMilan Broz 
19741da177e4SLinus Torvalds 	*result = md;
19751da177e4SLinus Torvalds 	return 0;
19761da177e4SLinus Torvalds }
19771da177e4SLinus Torvalds 
1978a5664dadSMike Snitzer /*
1979a5664dadSMike Snitzer  * Functions to manage md->type.
1980a5664dadSMike Snitzer  * All are required to hold md->type_lock.
1981a5664dadSMike Snitzer  */
1982a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
1983a5664dadSMike Snitzer {
1984a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
1985a5664dadSMike Snitzer }
1986a5664dadSMike Snitzer 
1987a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
1988a5664dadSMike Snitzer {
1989a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
1990a5664dadSMike Snitzer }
1991a5664dadSMike Snitzer 
19927e0d574fSBart Van Assche void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
1993a5664dadSMike Snitzer {
199400c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
1995a5664dadSMike Snitzer 	md->type = type;
1996a5664dadSMike Snitzer }
1997a5664dadSMike Snitzer 
19987e0d574fSBart Van Assche enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
1999a5664dadSMike Snitzer {
2000a5664dadSMike Snitzer 	return md->type;
2001a5664dadSMike Snitzer }
2002a5664dadSMike Snitzer 
200336a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
200436a0456fSAlasdair G Kergon {
200536a0456fSAlasdair G Kergon 	return md->immutable_target_type;
200636a0456fSAlasdair G Kergon }
200736a0456fSAlasdair G Kergon 
20084a0b4ddfSMike Snitzer /*
2009f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
2010f84cb8a4SMike Snitzer  * count on 'md'.
2011f84cb8a4SMike Snitzer  */
2012f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2013f84cb8a4SMike Snitzer {
2014f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
2015f84cb8a4SMike Snitzer 	return &md->queue->limits;
2016f84cb8a4SMike Snitzer }
2017f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2018f84cb8a4SMike Snitzer 
20194a0b4ddfSMike Snitzer /*
20204a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
20214a0b4ddfSMike Snitzer  */
2022591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
20234a0b4ddfSMike Snitzer {
2024bfebd1cdSMike Snitzer 	int r;
20257e0d574fSBart Van Assche 	enum dm_queue_mode type = dm_get_md_type(md);
2026bfebd1cdSMike Snitzer 
2027545ed20eSToshi Kani 	switch (type) {
2028bfebd1cdSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
2029eb8db831SChristoph Hellwig 		r = dm_old_init_request_queue(md, t);
2030bfebd1cdSMike Snitzer 		if (r) {
2031eca7ee6dSMike Snitzer 			DMERR("Cannot initialize queue for request-based mapped device");
2032bfebd1cdSMike Snitzer 			return r;
20334a0b4ddfSMike Snitzer 		}
2034bfebd1cdSMike Snitzer 		break;
2035bfebd1cdSMike Snitzer 	case DM_TYPE_MQ_REQUEST_BASED:
2036e83068a5SMike Snitzer 		r = dm_mq_init_request_queue(md, t);
2037bfebd1cdSMike Snitzer 		if (r) {
2038eca7ee6dSMike Snitzer 			DMERR("Cannot initialize queue for request-based dm-mq mapped device");
2039bfebd1cdSMike Snitzer 			return r;
2040bfebd1cdSMike Snitzer 		}
2041bfebd1cdSMike Snitzer 		break;
2042bfebd1cdSMike Snitzer 	case DM_TYPE_BIO_BASED:
2043545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
2044eca7ee6dSMike Snitzer 		dm_init_normal_md_queue(md);
2045ff36ab34SMike Snitzer 		blk_queue_make_request(md->queue, dm_make_request);
2046dbba42d8SMikulas Patocka 		/*
2047dbba42d8SMikulas Patocka 		 * DM handles splitting bios as needed.  Free the bio_split bioset
2048dbba42d8SMikulas Patocka 		 * since it won't be used (saves 1 process per bio-based DM device).
2049dbba42d8SMikulas Patocka 		 */
2050dbba42d8SMikulas Patocka 		bioset_free(md->queue->bio_split);
2051dbba42d8SMikulas Patocka 		md->queue->bio_split = NULL;
2052545ed20eSToshi Kani 
2053545ed20eSToshi Kani 		if (type == DM_TYPE_DAX_BIO_BASED)
2054545ed20eSToshi Kani 			queue_flag_set_unlocked(QUEUE_FLAG_DAX, md->queue);
2055bfebd1cdSMike Snitzer 		break;
20567e0d574fSBart Van Assche 	case DM_TYPE_NONE:
20577e0d574fSBart Van Assche 		WARN_ON_ONCE(true);
20587e0d574fSBart Van Assche 		break;
2059ff36ab34SMike Snitzer 	}
20604a0b4ddfSMike Snitzer 
20614a0b4ddfSMike Snitzer 	return 0;
20624a0b4ddfSMike Snitzer }
20634a0b4ddfSMike Snitzer 
20642bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev)
20651da177e4SLinus Torvalds {
20661da177e4SLinus Torvalds 	struct mapped_device *md;
20671da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
20681da177e4SLinus Torvalds 
20691da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
20701da177e4SLinus Torvalds 		return NULL;
20711da177e4SLinus Torvalds 
2072f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
20731da177e4SLinus Torvalds 
20741da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
20752bec1f4aSMikulas Patocka 	if (md) {
20762bec1f4aSMikulas Patocka 		if ((md == MINOR_ALLOCED ||
2077f331c029STejun Heo 		     (MINOR(disk_devt(dm_disk(md))) != minor) ||
2078abdc568bSKiyoshi Ueda 		     dm_deleting_md(md) ||
2079fba9f90eSJeff Mahoney 		     test_bit(DMF_FREEING, &md->flags))) {
2080637842cfSDavid Teigland 			md = NULL;
2081fba9f90eSJeff Mahoney 			goto out;
2082fba9f90eSJeff Mahoney 		}
20832bec1f4aSMikulas Patocka 		dm_get(md);
20842bec1f4aSMikulas Patocka 	}
20851da177e4SLinus Torvalds 
2086fba9f90eSJeff Mahoney out:
2087f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
20881da177e4SLinus Torvalds 
2089637842cfSDavid Teigland 	return md;
2090637842cfSDavid Teigland }
20913cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2092d229a958SDavid Teigland 
20939ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2094637842cfSDavid Teigland {
20959ade92a9SAlasdair G Kergon 	return md->interface_ptr;
20961da177e4SLinus Torvalds }
20971da177e4SLinus Torvalds 
20981da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
20991da177e4SLinus Torvalds {
21001da177e4SLinus Torvalds 	md->interface_ptr = ptr;
21011da177e4SLinus Torvalds }
21021da177e4SLinus Torvalds 
21031da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
21041da177e4SLinus Torvalds {
21051da177e4SLinus Torvalds 	atomic_inc(&md->holders);
21063f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
21071da177e4SLinus Torvalds }
21081da177e4SLinus Torvalds 
210909ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md)
211009ee96b2SMikulas Patocka {
211109ee96b2SMikulas Patocka 	spin_lock(&_minor_lock);
211209ee96b2SMikulas Patocka 	if (test_bit(DMF_FREEING, &md->flags)) {
211309ee96b2SMikulas Patocka 		spin_unlock(&_minor_lock);
211409ee96b2SMikulas Patocka 		return -EBUSY;
211509ee96b2SMikulas Patocka 	}
211609ee96b2SMikulas Patocka 	dm_get(md);
211709ee96b2SMikulas Patocka 	spin_unlock(&_minor_lock);
211809ee96b2SMikulas Patocka 	return 0;
211909ee96b2SMikulas Patocka }
212009ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold);
212109ee96b2SMikulas Patocka 
212272d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
212372d94861SAlasdair G Kergon {
212472d94861SAlasdair G Kergon 	return md->name;
212572d94861SAlasdair G Kergon }
212672d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
212772d94861SAlasdair G Kergon 
21283f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
21291da177e4SLinus Torvalds {
21303b785fbcSBart Van Assche 	struct request_queue *q = dm_get_md_queue(md);
21311134e5aeSMike Anderson 	struct dm_table *map;
213283d5e5b0SMikulas Patocka 	int srcu_idx;
21331da177e4SLinus Torvalds 
21343f77316dSKiyoshi Ueda 	might_sleep();
2135fba9f90eSJeff Mahoney 
213663a4f065SMike Snitzer 	spin_lock(&_minor_lock);
21373f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2138fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2139f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
21403f77316dSKiyoshi Ueda 
21412e91c369SBart Van Assche 	blk_set_queue_dying(q);
21423b785fbcSBart Van Assche 
214302233342SMike Snitzer 	if (dm_request_based(md) && md->kworker_task)
21443989144fSPetr Mladek 		kthread_flush_worker(&md->kworker);
21452eb6e1e3SKeith Busch 
2146ab7c7bb6SMikulas Patocka 	/*
2147ab7c7bb6SMikulas Patocka 	 * Take suspend_lock so that presuspend and postsuspend methods
2148ab7c7bb6SMikulas Patocka 	 * do not race with internal suspend.
2149ab7c7bb6SMikulas Patocka 	 */
2150ab7c7bb6SMikulas Patocka 	mutex_lock(&md->suspend_lock);
21512a708cffSJunichi Nomura 	map = dm_get_live_table(md, &srcu_idx);
21524f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
21531da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
21541da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
21551da177e4SLinus Torvalds 	}
215683d5e5b0SMikulas Patocka 	/* dm_put_live_table must be before msleep, otherwise deadlock is possible */
215783d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
21582a708cffSJunichi Nomura 	mutex_unlock(&md->suspend_lock);
215983d5e5b0SMikulas Patocka 
21603f77316dSKiyoshi Ueda 	/*
21613f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
21623f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
21633f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
21643f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
21653f77316dSKiyoshi Ueda 	 */
21663f77316dSKiyoshi Ueda 	if (wait)
21673f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
21683f77316dSKiyoshi Ueda 			msleep(1);
21693f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
21703f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
21713f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
21723f77316dSKiyoshi Ueda 
2173784aae73SMilan Broz 	dm_sysfs_exit(md);
2174a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
21751da177e4SLinus Torvalds 	free_dev(md);
21761da177e4SLinus Torvalds }
21773f77316dSKiyoshi Ueda 
21783f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
21793f77316dSKiyoshi Ueda {
21803f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
21813f77316dSKiyoshi Ueda }
21823f77316dSKiyoshi Ueda 
21833f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
21843f77316dSKiyoshi Ueda {
21853f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
21863f77316dSKiyoshi Ueda }
21873f77316dSKiyoshi Ueda 
21883f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
21893f77316dSKiyoshi Ueda {
21903f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
21911da177e4SLinus Torvalds }
219279eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
21931da177e4SLinus Torvalds 
2194b48633f8SBart Van Assche static int dm_wait_for_completion(struct mapped_device *md, long task_state)
219546125c1cSMilan Broz {
219646125c1cSMilan Broz 	int r = 0;
21979f4c3f87SBart Van Assche 	DEFINE_WAIT(wait);
219846125c1cSMilan Broz 
219946125c1cSMilan Broz 	while (1) {
22009f4c3f87SBart Van Assche 		prepare_to_wait(&md->wait, &wait, task_state);
220146125c1cSMilan Broz 
2202b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
220346125c1cSMilan Broz 			break;
220446125c1cSMilan Broz 
2205e3fabdfdSBart Van Assche 		if (signal_pending_state(task_state, current)) {
220646125c1cSMilan Broz 			r = -EINTR;
220746125c1cSMilan Broz 			break;
220846125c1cSMilan Broz 		}
220946125c1cSMilan Broz 
221046125c1cSMilan Broz 		io_schedule();
221146125c1cSMilan Broz 	}
22129f4c3f87SBart Van Assche 	finish_wait(&md->wait, &wait);
2213b44ebeb0SMikulas Patocka 
221446125c1cSMilan Broz 	return r;
221546125c1cSMilan Broz }
221646125c1cSMilan Broz 
22171da177e4SLinus Torvalds /*
22181da177e4SLinus Torvalds  * Process the deferred bios
22191da177e4SLinus Torvalds  */
2220ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
22211da177e4SLinus Torvalds {
2222ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2223ef208587SMikulas Patocka 						work);
22246d6f10dfSMilan Broz 	struct bio *c;
222583d5e5b0SMikulas Patocka 	int srcu_idx;
222683d5e5b0SMikulas Patocka 	struct dm_table *map;
22271da177e4SLinus Torvalds 
222883d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
2229ef208587SMikulas Patocka 
22303b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2231022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2232022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2233022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2234022c2611SMikulas Patocka 
22356a8736d1STejun Heo 		if (!c)
2236df12ee99SAlasdair G Kergon 			break;
223773d410c0SMilan Broz 
2238e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2239e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2240af7e466aSMikulas Patocka 		else
224183d5e5b0SMikulas Patocka 			__split_and_process_bio(md, map, c);
2242e6ee8c0bSKiyoshi Ueda 	}
22433b00b203SMikulas Patocka 
224483d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
22451da177e4SLinus Torvalds }
22461da177e4SLinus Torvalds 
22479a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2248304f3f6aSMilan Broz {
22493b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
22504e857c58SPeter Zijlstra 	smp_mb__after_atomic();
225153d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2252304f3f6aSMilan Broz }
2253304f3f6aSMilan Broz 
22541da177e4SLinus Torvalds /*
2255042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
22561da177e4SLinus Torvalds  */
2257042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
22581da177e4SLinus Torvalds {
225987eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2260754c5fc7SMike Snitzer 	struct queue_limits limits;
2261042d2a9bSAlasdair G Kergon 	int r;
22621da177e4SLinus Torvalds 
2263e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
22641da177e4SLinus Torvalds 
22651da177e4SLinus Torvalds 	/* device must be suspended */
22664f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
226793c534aeSAlasdair G Kergon 		goto out;
22681da177e4SLinus Torvalds 
22693ae70656SMike Snitzer 	/*
22703ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
22713ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
22723ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
22733ae70656SMike Snitzer 	 * reappear.
22743ae70656SMike Snitzer 	 */
22753ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
227683d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
22773ae70656SMike Snitzer 		if (live_map)
22783ae70656SMike Snitzer 			limits = md->queue->limits;
227983d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
22803ae70656SMike Snitzer 	}
22813ae70656SMike Snitzer 
228287eb5b21SMike Christie 	if (!live_map) {
2283754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2284042d2a9bSAlasdair G Kergon 		if (r) {
2285042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2286754c5fc7SMike Snitzer 			goto out;
2287042d2a9bSAlasdair G Kergon 		}
228887eb5b21SMike Christie 	}
2289754c5fc7SMike Snitzer 
2290042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
229162e08243SMikulas Patocka 	dm_issue_global_event();
22921da177e4SLinus Torvalds 
229393c534aeSAlasdair G Kergon out:
2294e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2295042d2a9bSAlasdair G Kergon 	return map;
22961da177e4SLinus Torvalds }
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds /*
22991da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
23001da177e4SLinus Torvalds  * device.
23011da177e4SLinus Torvalds  */
23022ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
23031da177e4SLinus Torvalds {
2304e39e2e95SAlasdair G Kergon 	int r;
23051da177e4SLinus Torvalds 
23061da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2307dfbe03f6SAlasdair G Kergon 
2308db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2309dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2310cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2311e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2312e39e2e95SAlasdair G Kergon 		return r;
2313dfbe03f6SAlasdair G Kergon 	}
2314dfbe03f6SAlasdair G Kergon 
2315aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2316aa8d7c2fSAlasdair G Kergon 
23171da177e4SLinus Torvalds 	return 0;
23181da177e4SLinus Torvalds }
23191da177e4SLinus Torvalds 
23202ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
23211da177e4SLinus Torvalds {
2322aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2323aa8d7c2fSAlasdair G Kergon 		return;
2324aa8d7c2fSAlasdair G Kergon 
2325db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
23261da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2327aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
23281da177e4SLinus Torvalds }
23291da177e4SLinus Torvalds 
23301da177e4SLinus Torvalds /*
2331b48633f8SBart Van Assche  * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
2332b48633f8SBart Van Assche  * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
2333b48633f8SBart Van Assche  * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
2334b48633f8SBart Van Assche  *
2335ffcc3936SMike Snitzer  * If __dm_suspend returns 0, the device is completely quiescent
2336ffcc3936SMike Snitzer  * now. There is no request-processing activity. All new requests
2337ffcc3936SMike Snitzer  * are being added to md->deferred list.
2338cec47e3dSKiyoshi Ueda  */
2339ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
2340b48633f8SBart Van Assche 			unsigned suspend_flags, long task_state,
2341eaf9a736SMike Snitzer 			int dmf_suspended_flag)
23421da177e4SLinus Torvalds {
2343ffcc3936SMike Snitzer 	bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2344ffcc3936SMike Snitzer 	bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2345ffcc3936SMike Snitzer 	int r;
2346cf222b37SAlasdair G Kergon 
23475a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
23485a8f1f80SBart Van Assche 
23492e93ccc1SKiyoshi Ueda 	/*
23502e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
23512e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
23522e93ccc1SKiyoshi Ueda 	 */
23532e93ccc1SKiyoshi Ueda 	if (noflush)
23542e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
235586331f39SBart Van Assche 	else
235686331f39SBart Van Assche 		pr_debug("%s: suspending with flush\n", dm_device_name(md));
23572e93ccc1SKiyoshi Ueda 
2358d67ee213SMike Snitzer 	/*
2359d67ee213SMike Snitzer 	 * This gets reverted if there's an error later and the targets
2360d67ee213SMike Snitzer 	 * provide the .presuspend_undo hook.
2361d67ee213SMike Snitzer 	 */
23621da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
23631da177e4SLinus Torvalds 
23642e93ccc1SKiyoshi Ueda 	/*
23659f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
23669f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
23679f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
23689f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
23692e93ccc1SKiyoshi Ueda 	 */
237032a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
23712ca3310eSAlasdair G Kergon 		r = lock_fs(md);
2372d67ee213SMike Snitzer 		if (r) {
2373d67ee213SMike Snitzer 			dm_table_presuspend_undo_targets(map);
2374ffcc3936SMike Snitzer 			return r;
2375aa8d7c2fSAlasdair G Kergon 		}
2376d67ee213SMike Snitzer 	}
23771da177e4SLinus Torvalds 
23781da177e4SLinus Torvalds 	/*
23793b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
23803b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
23813b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
23823b00b203SMikulas Patocka 	 * dm_wq_work.
23833b00b203SMikulas Patocka 	 *
23843b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
23853b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
23866a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
23876a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
23886a8736d1STejun Heo 	 * flush_workqueue(md->wq).
23891da177e4SLinus Torvalds 	 */
23901eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
239141abc4e1SHannes Reinecke 	if (map)
239283d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
23931da177e4SLinus Torvalds 
2394d0bcb878SKiyoshi Ueda 	/*
239529e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
239629e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2397d0bcb878SKiyoshi Ueda 	 */
23982eb6e1e3SKeith Busch 	if (dm_request_based(md)) {
2399eca7ee6dSMike Snitzer 		dm_stop_queue(md->queue);
240002233342SMike Snitzer 		if (md->kworker_task)
24013989144fSPetr Mladek 			kthread_flush_worker(&md->kworker);
24022eb6e1e3SKeith Busch 	}
2403cec47e3dSKiyoshi Ueda 
2404d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2405d0bcb878SKiyoshi Ueda 
24061da177e4SLinus Torvalds 	/*
24073b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
24083b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
24093b00b203SMikulas Patocka 	 * to finish.
24101da177e4SLinus Torvalds 	 */
2411b48633f8SBart Van Assche 	r = dm_wait_for_completion(md, task_state);
2412eaf9a736SMike Snitzer 	if (!r)
2413eaf9a736SMike Snitzer 		set_bit(dmf_suspended_flag, &md->flags);
24141da177e4SLinus Torvalds 
24156d6f10dfSMilan Broz 	if (noflush)
2416022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
241741abc4e1SHannes Reinecke 	if (map)
241883d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
24192e93ccc1SKiyoshi Ueda 
24201da177e4SLinus Torvalds 	/* were we interrupted ? */
242146125c1cSMilan Broz 	if (r < 0) {
24229a1fb464SMikulas Patocka 		dm_queue_flush(md);
242373d410c0SMilan Broz 
2424cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
2425eca7ee6dSMike Snitzer 			dm_start_queue(md->queue);
2426cec47e3dSKiyoshi Ueda 
24272ca3310eSAlasdair G Kergon 		unlock_fs(md);
2428d67ee213SMike Snitzer 		dm_table_presuspend_undo_targets(map);
2429ffcc3936SMike Snitzer 		/* pushback list is already flushed, so skip flush */
2430ffcc3936SMike Snitzer 	}
2431ffcc3936SMike Snitzer 
2432ffcc3936SMike Snitzer 	return r;
24332ca3310eSAlasdair G Kergon }
24342ca3310eSAlasdair G Kergon 
24353b00b203SMikulas Patocka /*
2436ffcc3936SMike Snitzer  * We need to be able to change a mapping table under a mounted
2437ffcc3936SMike Snitzer  * filesystem.  For example we might want to move some data in
2438ffcc3936SMike Snitzer  * the background.  Before the table can be swapped with
2439ffcc3936SMike Snitzer  * dm_bind_table, dm_suspend must be called to flush any in
2440ffcc3936SMike Snitzer  * flight bios and ensure that any further io gets deferred.
24413b00b203SMikulas Patocka  */
2442ffcc3936SMike Snitzer /*
2443ffcc3936SMike Snitzer  * Suspend mechanism in request-based dm.
2444ffcc3936SMike Snitzer  *
2445ffcc3936SMike Snitzer  * 1. Flush all I/Os by lock_fs() if needed.
2446ffcc3936SMike Snitzer  * 2. Stop dispatching any I/O by stopping the request_queue.
2447ffcc3936SMike Snitzer  * 3. Wait for all in-flight I/Os to be completed or requeued.
2448ffcc3936SMike Snitzer  *
2449ffcc3936SMike Snitzer  * To abort suspend, start the request_queue.
2450ffcc3936SMike Snitzer  */
2451ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2452ffcc3936SMike Snitzer {
2453ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2454ffcc3936SMike Snitzer 	int r = 0;
2455ffcc3936SMike Snitzer 
2456ffcc3936SMike Snitzer retry:
2457ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2458ffcc3936SMike Snitzer 
2459ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2460ffcc3936SMike Snitzer 		r = -EINVAL;
2461ffcc3936SMike Snitzer 		goto out_unlock;
2462ffcc3936SMike Snitzer 	}
2463ffcc3936SMike Snitzer 
2464ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2465ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2466ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2467ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2468ffcc3936SMike Snitzer 		if (r)
2469ffcc3936SMike Snitzer 			return r;
2470ffcc3936SMike Snitzer 		goto retry;
2471ffcc3936SMike Snitzer 	}
2472ffcc3936SMike Snitzer 
2473a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2474ffcc3936SMike Snitzer 
2475eaf9a736SMike Snitzer 	r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
2476ffcc3936SMike Snitzer 	if (r)
2477ffcc3936SMike Snitzer 		goto out_unlock;
24783b00b203SMikulas Patocka 
24794d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
24804d4471cbSKiyoshi Ueda 
2481d287483dSAlasdair G Kergon out_unlock:
2482e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2483cf222b37SAlasdair G Kergon 	return r;
24841da177e4SLinus Torvalds }
24851da177e4SLinus Torvalds 
2486ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map)
24871da177e4SLinus Torvalds {
2488ffcc3936SMike Snitzer 	if (map) {
2489ffcc3936SMike Snitzer 		int r = dm_table_resume_targets(map);
24908757b776SMilan Broz 		if (r)
2491ffcc3936SMike Snitzer 			return r;
2492ffcc3936SMike Snitzer 	}
24932ca3310eSAlasdair G Kergon 
24949a1fb464SMikulas Patocka 	dm_queue_flush(md);
24952ca3310eSAlasdair G Kergon 
2496cec47e3dSKiyoshi Ueda 	/*
2497cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2498cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2499cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2500cec47e3dSKiyoshi Ueda 	 */
2501cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2502eca7ee6dSMike Snitzer 		dm_start_queue(md->queue);
2503cec47e3dSKiyoshi Ueda 
25042ca3310eSAlasdair G Kergon 	unlock_fs(md);
25052ca3310eSAlasdair G Kergon 
2506ffcc3936SMike Snitzer 	return 0;
2507ffcc3936SMike Snitzer }
2508ffcc3936SMike Snitzer 
2509ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md)
2510ffcc3936SMike Snitzer {
25118dc23658SMinfei Huang 	int r;
2512ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2513ffcc3936SMike Snitzer 
2514ffcc3936SMike Snitzer retry:
25158dc23658SMinfei Huang 	r = -EINVAL;
2516ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2517ffcc3936SMike Snitzer 
2518ffcc3936SMike Snitzer 	if (!dm_suspended_md(md))
2519ffcc3936SMike Snitzer 		goto out;
2520ffcc3936SMike Snitzer 
2521ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2522ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2523ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2524ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2525ffcc3936SMike Snitzer 		if (r)
2526ffcc3936SMike Snitzer 			return r;
2527ffcc3936SMike Snitzer 		goto retry;
2528ffcc3936SMike Snitzer 	}
2529ffcc3936SMike Snitzer 
2530a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2531ffcc3936SMike Snitzer 	if (!map || !dm_table_get_size(map))
2532ffcc3936SMike Snitzer 		goto out;
2533ffcc3936SMike Snitzer 
2534ffcc3936SMike Snitzer 	r = __dm_resume(md, map);
2535ffcc3936SMike Snitzer 	if (r)
2536ffcc3936SMike Snitzer 		goto out;
2537ffcc3936SMike Snitzer 
25382ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
2539cf222b37SAlasdair G Kergon out:
2540e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
25412ca3310eSAlasdair G Kergon 
2542cf222b37SAlasdair G Kergon 	return r;
25431da177e4SLinus Torvalds }
25441da177e4SLinus Torvalds 
2545fd2ed4d2SMikulas Patocka /*
2546fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
2547fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
2548fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
2549fd2ed4d2SMikulas Patocka  */
2550fd2ed4d2SMikulas Patocka 
2551ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
2552ffcc3936SMike Snitzer {
2553ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2554ffcc3936SMike Snitzer 
25551ea0654eSBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
25561ea0654eSBart Van Assche 
255796b26c8cSMikulas Patocka 	if (md->internal_suspend_count++)
2558ffcc3936SMike Snitzer 		return; /* nested internal suspend */
2559ffcc3936SMike Snitzer 
2560ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2561ffcc3936SMike Snitzer 		set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2562ffcc3936SMike Snitzer 		return; /* nest suspend */
2563ffcc3936SMike Snitzer 	}
2564ffcc3936SMike Snitzer 
2565a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2566ffcc3936SMike Snitzer 
2567ffcc3936SMike Snitzer 	/*
2568ffcc3936SMike Snitzer 	 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2569ffcc3936SMike Snitzer 	 * supported.  Properly supporting a TASK_INTERRUPTIBLE internal suspend
2570ffcc3936SMike Snitzer 	 * would require changing .presuspend to return an error -- avoid this
2571ffcc3936SMike Snitzer 	 * until there is a need for more elaborate variants of internal suspend.
2572ffcc3936SMike Snitzer 	 */
2573eaf9a736SMike Snitzer 	(void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
2574eaf9a736SMike Snitzer 			    DMF_SUSPENDED_INTERNALLY);
2575ffcc3936SMike Snitzer 
2576ffcc3936SMike Snitzer 	dm_table_postsuspend_targets(map);
2577ffcc3936SMike Snitzer }
2578ffcc3936SMike Snitzer 
2579ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md)
2580ffcc3936SMike Snitzer {
258196b26c8cSMikulas Patocka 	BUG_ON(!md->internal_suspend_count);
258296b26c8cSMikulas Patocka 
258396b26c8cSMikulas Patocka 	if (--md->internal_suspend_count)
2584ffcc3936SMike Snitzer 		return; /* resume from nested internal suspend */
2585ffcc3936SMike Snitzer 
2586ffcc3936SMike Snitzer 	if (dm_suspended_md(md))
2587ffcc3936SMike Snitzer 		goto done; /* resume from nested suspend */
2588ffcc3936SMike Snitzer 
2589ffcc3936SMike Snitzer 	/*
2590ffcc3936SMike Snitzer 	 * NOTE: existing callers don't need to call dm_table_resume_targets
2591ffcc3936SMike Snitzer 	 * (which may fail -- so best to avoid it for now by passing NULL map)
2592ffcc3936SMike Snitzer 	 */
2593ffcc3936SMike Snitzer 	(void) __dm_resume(md, NULL);
2594ffcc3936SMike Snitzer 
2595ffcc3936SMike Snitzer done:
2596ffcc3936SMike Snitzer 	clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2597ffcc3936SMike Snitzer 	smp_mb__after_atomic();
2598ffcc3936SMike Snitzer 	wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2599ffcc3936SMike Snitzer }
2600ffcc3936SMike Snitzer 
2601ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md)
2602fd2ed4d2SMikulas Patocka {
2603fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
2604ffcc3936SMike Snitzer 	__dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2605ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2606ffcc3936SMike Snitzer }
2607ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
2608ffcc3936SMike Snitzer 
2609ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md)
2610ffcc3936SMike Snitzer {
2611ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2612ffcc3936SMike Snitzer 	__dm_internal_resume(md);
2613ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2614ffcc3936SMike Snitzer }
2615ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume);
2616ffcc3936SMike Snitzer 
2617ffcc3936SMike Snitzer /*
2618ffcc3936SMike Snitzer  * Fast variants of internal suspend/resume hold md->suspend_lock,
2619ffcc3936SMike Snitzer  * which prevents interaction with userspace-driven suspend.
2620ffcc3936SMike Snitzer  */
2621ffcc3936SMike Snitzer 
2622ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md)
2623ffcc3936SMike Snitzer {
2624ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2625ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2626fd2ed4d2SMikulas Patocka 		return;
2627fd2ed4d2SMikulas Patocka 
2628fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2629fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
2630fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
2631fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2632fd2ed4d2SMikulas Patocka }
2633b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
2634fd2ed4d2SMikulas Patocka 
2635ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md)
2636fd2ed4d2SMikulas Patocka {
2637ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2638fd2ed4d2SMikulas Patocka 		goto done;
2639fd2ed4d2SMikulas Patocka 
2640fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
2641fd2ed4d2SMikulas Patocka 
2642fd2ed4d2SMikulas Patocka done:
2643fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
2644fd2ed4d2SMikulas Patocka }
2645b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
2646fd2ed4d2SMikulas Patocka 
26471da177e4SLinus Torvalds /*-----------------------------------------------------------------
26481da177e4SLinus Torvalds  * Event notification.
26491da177e4SLinus Torvalds  *---------------------------------------------------------------*/
26503abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
265160935eb2SMilan Broz 		       unsigned cookie)
265269267a30SAlasdair G Kergon {
265360935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
265460935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
265560935eb2SMilan Broz 
265660935eb2SMilan Broz 	if (!cookie)
26573abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
265860935eb2SMilan Broz 	else {
265960935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
266060935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
26613abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
26623abf85b5SPeter Rajnoha 					  action, envp);
266360935eb2SMilan Broz 	}
266469267a30SAlasdair G Kergon }
266569267a30SAlasdair G Kergon 
26667a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
26677a8c3d3bSMike Anderson {
26687a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
26697a8c3d3bSMike Anderson }
26707a8c3d3bSMike Anderson 
26711da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
26721da177e4SLinus Torvalds {
26731da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
26741da177e4SLinus Torvalds }
26751da177e4SLinus Torvalds 
26761da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
26771da177e4SLinus Torvalds {
26781da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
26791da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
26801da177e4SLinus Torvalds }
26811da177e4SLinus Torvalds 
26827a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
26837a8c3d3bSMike Anderson {
26847a8c3d3bSMike Anderson 	unsigned long flags;
26857a8c3d3bSMike Anderson 
26867a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
26877a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
26887a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
26897a8c3d3bSMike Anderson }
26907a8c3d3bSMike Anderson 
26911da177e4SLinus Torvalds /*
26921da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
26931da177e4SLinus Torvalds  * count on 'md'.
26941da177e4SLinus Torvalds  */
26951da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
26961da177e4SLinus Torvalds {
26971da177e4SLinus Torvalds 	return md->disk;
26981da177e4SLinus Torvalds }
269965ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk);
27001da177e4SLinus Torvalds 
2701784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2702784aae73SMilan Broz {
27032995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
2704784aae73SMilan Broz }
2705784aae73SMilan Broz 
2706784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2707784aae73SMilan Broz {
2708784aae73SMilan Broz 	struct mapped_device *md;
2709784aae73SMilan Broz 
27102995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
2711784aae73SMilan Broz 
27124d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
2713432a212cSMike Anderson 	    dm_deleting_md(md))
27144d89b7b4SMilan Broz 		return NULL;
27154d89b7b4SMilan Broz 
2716784aae73SMilan Broz 	dm_get(md);
2717784aae73SMilan Broz 	return md;
2718784aae73SMilan Broz }
2719784aae73SMilan Broz 
27204f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
27211da177e4SLinus Torvalds {
27221da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
27231da177e4SLinus Torvalds }
27241da177e4SLinus Torvalds 
2725ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md)
2726ffcc3936SMike Snitzer {
2727ffcc3936SMike Snitzer 	return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2728ffcc3936SMike Snitzer }
2729ffcc3936SMike Snitzer 
27302c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
27312c140a24SMikulas Patocka {
27322c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
27332c140a24SMikulas Patocka }
27342c140a24SMikulas Patocka 
273564dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
273664dbce58SKiyoshi Ueda {
2737ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
273864dbce58SKiyoshi Ueda }
273964dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
274064dbce58SKiyoshi Ueda 
27412e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
27422e93ccc1SKiyoshi Ueda {
2743ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
27442e93ccc1SKiyoshi Ueda }
27452e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
27462e93ccc1SKiyoshi Ueda 
27477e0d574fSBart Van Assche struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type,
274830187e1dSMike Snitzer 					    unsigned integrity, unsigned per_io_data_size)
2749e6ee8c0bSKiyoshi Ueda {
2750115485e8SMike Snitzer 	struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
275178d8e58aSMike Snitzer 	unsigned int pool_size = 0;
27525f015204SJun'ichi Nomura 	unsigned int front_pad;
2753e6ee8c0bSKiyoshi Ueda 
2754e6ee8c0bSKiyoshi Ueda 	if (!pools)
27554e6e36c3SMike Snitzer 		return NULL;
2756e6ee8c0bSKiyoshi Ueda 
275778d8e58aSMike Snitzer 	switch (type) {
275878d8e58aSMike Snitzer 	case DM_TYPE_BIO_BASED:
2759545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
276078d8e58aSMike Snitzer 		pool_size = dm_get_reserved_bio_based_ios();
276130187e1dSMike Snitzer 		front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
2762eb8db831SChristoph Hellwig 
2763eb8db831SChristoph Hellwig 		pools->io_pool = mempool_create_slab_pool(pool_size, _io_cache);
2764eb8db831SChristoph Hellwig 		if (!pools->io_pool)
2765eb8db831SChristoph Hellwig 			goto out;
276678d8e58aSMike Snitzer 		break;
276778d8e58aSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
276878d8e58aSMike Snitzer 	case DM_TYPE_MQ_REQUEST_BASED:
276978d8e58aSMike Snitzer 		pool_size = dm_get_reserved_rq_based_ios();
277078d8e58aSMike Snitzer 		front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
2771591ddcfcSMike Snitzer 		/* per_io_data_size is used for blk-mq pdu at queue allocation */
277278d8e58aSMike Snitzer 		break;
277378d8e58aSMike Snitzer 	default:
277478d8e58aSMike Snitzer 		BUG();
277578d8e58aSMike Snitzer 	}
277678d8e58aSMike Snitzer 
277747e0fb46SNeilBrown 	pools->bs = bioset_create(pool_size, front_pad, BIOSET_NEED_RESCUER);
2778e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
27795f015204SJun'ichi Nomura 		goto out;
2780e6ee8c0bSKiyoshi Ueda 
2781a91a2785SMartin K. Petersen 	if (integrity && bioset_integrity_create(pools->bs, pool_size))
27825f015204SJun'ichi Nomura 		goto out;
2783a91a2785SMartin K. Petersen 
2784e6ee8c0bSKiyoshi Ueda 	return pools;
278578d8e58aSMike Snitzer 
27865f015204SJun'ichi Nomura out:
27875f015204SJun'ichi Nomura 	dm_free_md_mempools(pools);
2788e6ee8c0bSKiyoshi Ueda 
27894e6e36c3SMike Snitzer 	return NULL;
2790e6ee8c0bSKiyoshi Ueda }
2791e6ee8c0bSKiyoshi Ueda 
2792e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
2793e6ee8c0bSKiyoshi Ueda {
2794e6ee8c0bSKiyoshi Ueda 	if (!pools)
2795e6ee8c0bSKiyoshi Ueda 		return;
2796e6ee8c0bSKiyoshi Ueda 
2797e6ee8c0bSKiyoshi Ueda 	mempool_destroy(pools->io_pool);
27981ae49ea2SMike Snitzer 
2799e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
2800e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
2801e6ee8c0bSKiyoshi Ueda 
2802e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2803e6ee8c0bSKiyoshi Ueda }
2804e6ee8c0bSKiyoshi Ueda 
28059c72bad1SChristoph Hellwig struct dm_pr {
28069c72bad1SChristoph Hellwig 	u64	old_key;
28079c72bad1SChristoph Hellwig 	u64	new_key;
28089c72bad1SChristoph Hellwig 	u32	flags;
28099c72bad1SChristoph Hellwig 	bool	fail_early;
28109c72bad1SChristoph Hellwig };
28119c72bad1SChristoph Hellwig 
28129c72bad1SChristoph Hellwig static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
28139c72bad1SChristoph Hellwig 		      void *data)
28149c72bad1SChristoph Hellwig {
28159c72bad1SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
28169c72bad1SChristoph Hellwig 	struct dm_table *table;
28179c72bad1SChristoph Hellwig 	struct dm_target *ti;
28189c72bad1SChristoph Hellwig 	int ret = -ENOTTY, srcu_idx;
28199c72bad1SChristoph Hellwig 
28209c72bad1SChristoph Hellwig 	table = dm_get_live_table(md, &srcu_idx);
28219c72bad1SChristoph Hellwig 	if (!table || !dm_table_get_size(table))
28229c72bad1SChristoph Hellwig 		goto out;
28239c72bad1SChristoph Hellwig 
28249c72bad1SChristoph Hellwig 	/* We only support devices that have a single target */
28259c72bad1SChristoph Hellwig 	if (dm_table_get_num_targets(table) != 1)
28269c72bad1SChristoph Hellwig 		goto out;
28279c72bad1SChristoph Hellwig 	ti = dm_table_get_target(table, 0);
28289c72bad1SChristoph Hellwig 
28299c72bad1SChristoph Hellwig 	ret = -EINVAL;
28309c72bad1SChristoph Hellwig 	if (!ti->type->iterate_devices)
28319c72bad1SChristoph Hellwig 		goto out;
28329c72bad1SChristoph Hellwig 
28339c72bad1SChristoph Hellwig 	ret = ti->type->iterate_devices(ti, fn, data);
28349c72bad1SChristoph Hellwig out:
28359c72bad1SChristoph Hellwig 	dm_put_live_table(md, srcu_idx);
28369c72bad1SChristoph Hellwig 	return ret;
28379c72bad1SChristoph Hellwig }
28389c72bad1SChristoph Hellwig 
28399c72bad1SChristoph Hellwig /*
28409c72bad1SChristoph Hellwig  * For register / unregister we need to manually call out to every path.
28419c72bad1SChristoph Hellwig  */
28429c72bad1SChristoph Hellwig static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
28439c72bad1SChristoph Hellwig 			    sector_t start, sector_t len, void *data)
28449c72bad1SChristoph Hellwig {
28459c72bad1SChristoph Hellwig 	struct dm_pr *pr = data;
28469c72bad1SChristoph Hellwig 	const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
28479c72bad1SChristoph Hellwig 
28489c72bad1SChristoph Hellwig 	if (!ops || !ops->pr_register)
28499c72bad1SChristoph Hellwig 		return -EOPNOTSUPP;
28509c72bad1SChristoph Hellwig 	return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
28519c72bad1SChristoph Hellwig }
28529c72bad1SChristoph Hellwig 
285371cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
285471cdb697SChristoph Hellwig 			  u32 flags)
285571cdb697SChristoph Hellwig {
28569c72bad1SChristoph Hellwig 	struct dm_pr pr = {
28579c72bad1SChristoph Hellwig 		.old_key	= old_key,
28589c72bad1SChristoph Hellwig 		.new_key	= new_key,
28599c72bad1SChristoph Hellwig 		.flags		= flags,
28609c72bad1SChristoph Hellwig 		.fail_early	= true,
28619c72bad1SChristoph Hellwig 	};
28629c72bad1SChristoph Hellwig 	int ret;
286371cdb697SChristoph Hellwig 
28649c72bad1SChristoph Hellwig 	ret = dm_call_pr(bdev, __dm_pr_register, &pr);
28659c72bad1SChristoph Hellwig 	if (ret && new_key) {
28669c72bad1SChristoph Hellwig 		/* unregister all paths if we failed to register any path */
28679c72bad1SChristoph Hellwig 		pr.old_key = new_key;
28689c72bad1SChristoph Hellwig 		pr.new_key = 0;
28699c72bad1SChristoph Hellwig 		pr.flags = 0;
28709c72bad1SChristoph Hellwig 		pr.fail_early = false;
28719c72bad1SChristoph Hellwig 		dm_call_pr(bdev, __dm_pr_register, &pr);
28729c72bad1SChristoph Hellwig 	}
287371cdb697SChristoph Hellwig 
28749c72bad1SChristoph Hellwig 	return ret;
287571cdb697SChristoph Hellwig }
287671cdb697SChristoph Hellwig 
287771cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
287871cdb697SChristoph Hellwig 			 u32 flags)
287971cdb697SChristoph Hellwig {
288071cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
288171cdb697SChristoph Hellwig 	const struct pr_ops *ops;
288271cdb697SChristoph Hellwig 	fmode_t mode;
2883956a4025SMike Snitzer 	int r;
288471cdb697SChristoph Hellwig 
2885956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
288671cdb697SChristoph Hellwig 	if (r < 0)
288771cdb697SChristoph Hellwig 		return r;
288871cdb697SChristoph Hellwig 
288971cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
289071cdb697SChristoph Hellwig 	if (ops && ops->pr_reserve)
289171cdb697SChristoph Hellwig 		r = ops->pr_reserve(bdev, key, type, flags);
289271cdb697SChristoph Hellwig 	else
289371cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
289471cdb697SChristoph Hellwig 
2895956a4025SMike Snitzer 	bdput(bdev);
289671cdb697SChristoph Hellwig 	return r;
289771cdb697SChristoph Hellwig }
289871cdb697SChristoph Hellwig 
289971cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
290071cdb697SChristoph Hellwig {
290171cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
290271cdb697SChristoph Hellwig 	const struct pr_ops *ops;
290371cdb697SChristoph Hellwig 	fmode_t mode;
2904956a4025SMike Snitzer 	int r;
290571cdb697SChristoph Hellwig 
2906956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
290771cdb697SChristoph Hellwig 	if (r < 0)
290871cdb697SChristoph Hellwig 		return r;
290971cdb697SChristoph Hellwig 
291071cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
291171cdb697SChristoph Hellwig 	if (ops && ops->pr_release)
291271cdb697SChristoph Hellwig 		r = ops->pr_release(bdev, key, type);
291371cdb697SChristoph Hellwig 	else
291471cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
291571cdb697SChristoph Hellwig 
2916956a4025SMike Snitzer 	bdput(bdev);
291771cdb697SChristoph Hellwig 	return r;
291871cdb697SChristoph Hellwig }
291971cdb697SChristoph Hellwig 
292071cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
292171cdb697SChristoph Hellwig 			 enum pr_type type, bool abort)
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_preempt)
293471cdb697SChristoph Hellwig 		r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
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_clear(struct block_device *bdev, u64 key)
294371cdb697SChristoph Hellwig {
294471cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
294571cdb697SChristoph Hellwig 	const struct pr_ops *ops;
294671cdb697SChristoph Hellwig 	fmode_t mode;
2947956a4025SMike Snitzer 	int r;
294871cdb697SChristoph Hellwig 
2949956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
295071cdb697SChristoph Hellwig 	if (r < 0)
295171cdb697SChristoph Hellwig 		return r;
295271cdb697SChristoph Hellwig 
295371cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
295471cdb697SChristoph Hellwig 	if (ops && ops->pr_clear)
295571cdb697SChristoph Hellwig 		r = ops->pr_clear(bdev, key);
295671cdb697SChristoph Hellwig 	else
295771cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
295871cdb697SChristoph Hellwig 
2959956a4025SMike Snitzer 	bdput(bdev);
296071cdb697SChristoph Hellwig 	return r;
296171cdb697SChristoph Hellwig }
296271cdb697SChristoph Hellwig 
296371cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = {
296471cdb697SChristoph Hellwig 	.pr_register	= dm_pr_register,
296571cdb697SChristoph Hellwig 	.pr_reserve	= dm_pr_reserve,
296671cdb697SChristoph Hellwig 	.pr_release	= dm_pr_release,
296771cdb697SChristoph Hellwig 	.pr_preempt	= dm_pr_preempt,
296871cdb697SChristoph Hellwig 	.pr_clear	= dm_pr_clear,
296971cdb697SChristoph Hellwig };
297071cdb697SChristoph Hellwig 
297183d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
29721da177e4SLinus Torvalds 	.open = dm_blk_open,
29731da177e4SLinus Torvalds 	.release = dm_blk_close,
2974aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
29753ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
297671cdb697SChristoph Hellwig 	.pr_ops = &dm_pr_ops,
29771da177e4SLinus Torvalds 	.owner = THIS_MODULE
29781da177e4SLinus Torvalds };
29791da177e4SLinus Torvalds 
2980f26c5719SDan Williams static const struct dax_operations dm_dax_ops = {
2981f26c5719SDan Williams 	.direct_access = dm_dax_direct_access,
29827e026c8cSDan Williams 	.copy_from_iter = dm_dax_copy_from_iter,
2983f26c5719SDan Williams };
2984f26c5719SDan Williams 
29851da177e4SLinus Torvalds /*
29861da177e4SLinus Torvalds  * module hooks
29871da177e4SLinus Torvalds  */
29881da177e4SLinus Torvalds module_init(dm_init);
29891da177e4SLinus Torvalds module_exit(dm_exit);
29901da177e4SLinus Torvalds 
29911da177e4SLinus Torvalds module_param(major, uint, 0);
29921da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
2993f4790826SMike Snitzer 
2994e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
2995e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
2996e8603136SMike Snitzer 
2997115485e8SMike Snitzer module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
2998115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
2999115485e8SMike Snitzer 
30001da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
30011da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
30021da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3003