xref: /openbmc/linux/drivers/md/dm.c (revision 318716ddea0829d3be566efc69d31029c40d51e2)
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>
27b0b4d7c6SElena Reshetova #include <linux/refcount.h>
2855782138SLi Zefan 
2972d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
3072d94861SAlasdair G Kergon 
3160935eb2SMilan Broz /*
3260935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
3360935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
3460935eb2SMilan Broz  */
3560935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
3660935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
3760935eb2SMilan Broz 
381da177e4SLinus Torvalds static const char *_name = DM_NAME;
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds static unsigned int major = 0;
411da177e4SLinus Torvalds static unsigned int _major = 0;
421da177e4SLinus Torvalds 
43d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr);
44d15b774cSAlasdair G Kergon 
45f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
462c140a24SMikulas Patocka 
472c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w);
482c140a24SMikulas Patocka 
492c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
502c140a24SMikulas Patocka 
51acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue;
52acfe0ad7SMikulas Patocka 
5393e6442cSMikulas Patocka atomic_t dm_global_event_nr = ATOMIC_INIT(0);
5493e6442cSMikulas Patocka DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq);
5593e6442cSMikulas Patocka 
5662e08243SMikulas Patocka void dm_issue_global_event(void)
5762e08243SMikulas Patocka {
5862e08243SMikulas Patocka 	atomic_inc(&dm_global_event_nr);
5962e08243SMikulas Patocka 	wake_up(&dm_global_eventq);
6062e08243SMikulas Patocka }
6162e08243SMikulas Patocka 
621da177e4SLinus Torvalds /*
631da177e4SLinus Torvalds  * One of these is allocated per bio.
641da177e4SLinus Torvalds  */
651da177e4SLinus Torvalds struct dm_io {
661da177e4SLinus Torvalds 	struct mapped_device *md;
674e4cbee9SChristoph Hellwig 	blk_status_t status;
681da177e4SLinus Torvalds 	atomic_t io_count;
696ae2fa67SRichard Kennedy 	struct bio *bio;
703eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
71f88fb981SKiyoshi Ueda 	spinlock_t endio_lock;
72fd2ed4d2SMikulas Patocka 	struct dm_stats_aux stats_aux;
731da177e4SLinus Torvalds };
741da177e4SLinus Torvalds 
75ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
76ba61fdd1SJeff Mahoney 
771da177e4SLinus Torvalds /*
781da177e4SLinus Torvalds  * Bits for the md->flags field.
791da177e4SLinus Torvalds  */
801eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
811da177e4SLinus Torvalds #define DMF_SUSPENDED 1
82aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
83fba9f90eSJeff Mahoney #define DMF_FREEING 3
845c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
852e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
868ae12666SKent Overstreet #define DMF_DEFERRED_REMOVE 6
878ae12666SKent Overstreet #define DMF_SUSPENDED_INTERNALLY 7
881da177e4SLinus Torvalds 
89115485e8SMike Snitzer #define DM_NUMA_NODE NUMA_NO_NODE
90115485e8SMike Snitzer static int dm_numa_node = DM_NUMA_NODE;
91faad87dfSMike Snitzer 
92e6ee8c0bSKiyoshi Ueda /*
93e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
94e6ee8c0bSKiyoshi Ueda  */
95e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
96e6ee8c0bSKiyoshi Ueda 	mempool_t *io_pool;
97e6ee8c0bSKiyoshi Ueda 	struct bio_set *bs;
98e6ee8c0bSKiyoshi Ueda };
99e6ee8c0bSKiyoshi Ueda 
10086f1152bSBenjamin Marzinski struct table_device {
10186f1152bSBenjamin Marzinski 	struct list_head list;
102b0b4d7c6SElena Reshetova 	refcount_t count;
10386f1152bSBenjamin Marzinski 	struct dm_dev dm_dev;
10486f1152bSBenjamin Marzinski };
10586f1152bSBenjamin Marzinski 
106e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
1078fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
1081ae49ea2SMike Snitzer static struct kmem_cache *_rq_cache;
10994818742SKent Overstreet 
110f4790826SMike Snitzer /*
111e8603136SMike Snitzer  * Bio-based DM's mempools' reserved IOs set by the user.
112e8603136SMike Snitzer  */
1134cc96131SMike Snitzer #define RESERVED_BIO_BASED_IOS		16
114e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
115e8603136SMike Snitzer 
116115485e8SMike Snitzer static int __dm_get_module_param_int(int *module_param, int min, int max)
117115485e8SMike Snitzer {
1186aa7de05SMark Rutland 	int param = READ_ONCE(*module_param);
119115485e8SMike Snitzer 	int modified_param = 0;
120115485e8SMike Snitzer 	bool modified = true;
121115485e8SMike Snitzer 
122115485e8SMike Snitzer 	if (param < min)
123115485e8SMike Snitzer 		modified_param = min;
124115485e8SMike Snitzer 	else if (param > max)
125115485e8SMike Snitzer 		modified_param = max;
126115485e8SMike Snitzer 	else
127115485e8SMike Snitzer 		modified = false;
128115485e8SMike Snitzer 
129115485e8SMike Snitzer 	if (modified) {
130115485e8SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
131115485e8SMike Snitzer 		param = modified_param;
132115485e8SMike Snitzer 	}
133115485e8SMike Snitzer 
134115485e8SMike Snitzer 	return param;
135115485e8SMike Snitzer }
136115485e8SMike Snitzer 
1374cc96131SMike Snitzer unsigned __dm_get_module_param(unsigned *module_param,
138f4790826SMike Snitzer 			       unsigned def, unsigned max)
139f4790826SMike Snitzer {
1406aa7de05SMark Rutland 	unsigned param = READ_ONCE(*module_param);
14109c2d531SMike Snitzer 	unsigned modified_param = 0;
142f4790826SMike Snitzer 
14309c2d531SMike Snitzer 	if (!param)
14409c2d531SMike Snitzer 		modified_param = def;
14509c2d531SMike Snitzer 	else if (param > max)
14609c2d531SMike Snitzer 		modified_param = max;
147f4790826SMike Snitzer 
14809c2d531SMike Snitzer 	if (modified_param) {
14909c2d531SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
15009c2d531SMike Snitzer 		param = modified_param;
151f4790826SMike Snitzer 	}
152f4790826SMike Snitzer 
15309c2d531SMike Snitzer 	return param;
154f4790826SMike Snitzer }
155f4790826SMike Snitzer 
156e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void)
157e8603136SMike Snitzer {
15809c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_bio_based_ios,
1594cc96131SMike Snitzer 				     RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS);
160e8603136SMike Snitzer }
161e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
162e8603136SMike Snitzer 
163115485e8SMike Snitzer static unsigned dm_get_numa_node(void)
164115485e8SMike Snitzer {
165115485e8SMike Snitzer 	return __dm_get_module_param_int(&dm_numa_node,
166115485e8SMike Snitzer 					 DM_NUMA_NODE, num_online_nodes() - 1);
167115485e8SMike Snitzer }
168115485e8SMike Snitzer 
1691da177e4SLinus Torvalds static int __init local_init(void)
1701da177e4SLinus Torvalds {
17151157b4aSKiyoshi Ueda 	int r = -ENOMEM;
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
174028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
1751da177e4SLinus Torvalds 	if (!_io_cache)
17651157b4aSKiyoshi Ueda 		return r;
1771da177e4SLinus Torvalds 
1788fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
1798fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
180dba14160SMikulas Patocka 		goto out_free_io_cache;
1818fbf26adSKiyoshi Ueda 
182eca7ee6dSMike Snitzer 	_rq_cache = kmem_cache_create("dm_old_clone_request", sizeof(struct request),
1831ae49ea2SMike Snitzer 				      __alignof__(struct request), 0, NULL);
1841ae49ea2SMike Snitzer 	if (!_rq_cache)
1851ae49ea2SMike Snitzer 		goto out_free_rq_tio_cache;
1861ae49ea2SMike Snitzer 
18751e5b2bdSMike Anderson 	r = dm_uevent_init();
18851157b4aSKiyoshi Ueda 	if (r)
1891ae49ea2SMike Snitzer 		goto out_free_rq_cache;
19051e5b2bdSMike Anderson 
191acfe0ad7SMikulas Patocka 	deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
192acfe0ad7SMikulas Patocka 	if (!deferred_remove_workqueue) {
193acfe0ad7SMikulas Patocka 		r = -ENOMEM;
194acfe0ad7SMikulas Patocka 		goto out_uevent_exit;
195acfe0ad7SMikulas Patocka 	}
196acfe0ad7SMikulas Patocka 
1971da177e4SLinus Torvalds 	_major = major;
1981da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
19951157b4aSKiyoshi Ueda 	if (r < 0)
200acfe0ad7SMikulas Patocka 		goto out_free_workqueue;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	if (!_major)
2031da177e4SLinus Torvalds 		_major = r;
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	return 0;
20651157b4aSKiyoshi Ueda 
207acfe0ad7SMikulas Patocka out_free_workqueue:
208acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
20951157b4aSKiyoshi Ueda out_uevent_exit:
21051157b4aSKiyoshi Ueda 	dm_uevent_exit();
2111ae49ea2SMike Snitzer out_free_rq_cache:
2121ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
2138fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
2148fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
21551157b4aSKiyoshi Ueda out_free_io_cache:
21651157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
21751157b4aSKiyoshi Ueda 
21851157b4aSKiyoshi Ueda 	return r;
2191da177e4SLinus Torvalds }
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds static void local_exit(void)
2221da177e4SLinus Torvalds {
2232c140a24SMikulas Patocka 	flush_scheduled_work();
224acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
2252c140a24SMikulas Patocka 
2261ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
2278fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
2281da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
22900d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
23051e5b2bdSMike Anderson 	dm_uevent_exit();
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	_major = 0;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	DMINFO("cleaned up");
2351da177e4SLinus Torvalds }
2361da177e4SLinus Torvalds 
237b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
2381da177e4SLinus Torvalds 	local_init,
2391da177e4SLinus Torvalds 	dm_target_init,
2401da177e4SLinus Torvalds 	dm_linear_init,
2411da177e4SLinus Torvalds 	dm_stripe_init,
242952b3557SMikulas Patocka 	dm_io_init,
243945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2441da177e4SLinus Torvalds 	dm_interface_init,
245fd2ed4d2SMikulas Patocka 	dm_statistics_init,
2461da177e4SLinus Torvalds };
2471da177e4SLinus Torvalds 
248b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2491da177e4SLinus Torvalds 	local_exit,
2501da177e4SLinus Torvalds 	dm_target_exit,
2511da177e4SLinus Torvalds 	dm_linear_exit,
2521da177e4SLinus Torvalds 	dm_stripe_exit,
253952b3557SMikulas Patocka 	dm_io_exit,
254945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
2551da177e4SLinus Torvalds 	dm_interface_exit,
256fd2ed4d2SMikulas Patocka 	dm_statistics_exit,
2571da177e4SLinus Torvalds };
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds static int __init dm_init(void)
2601da177e4SLinus Torvalds {
2611da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	int r, i;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
2661da177e4SLinus Torvalds 		r = _inits[i]();
2671da177e4SLinus Torvalds 		if (r)
2681da177e4SLinus Torvalds 			goto bad;
2691da177e4SLinus Torvalds 	}
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	return 0;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds       bad:
2741da177e4SLinus Torvalds 	while (i--)
2751da177e4SLinus Torvalds 		_exits[i]();
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	return r;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds static void __exit dm_exit(void)
2811da177e4SLinus Torvalds {
2821da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	while (i--)
2851da177e4SLinus Torvalds 		_exits[i]();
286d15b774cSAlasdair G Kergon 
287d15b774cSAlasdair G Kergon 	/*
288d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
289d15b774cSAlasdair G Kergon 	 */
290d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds /*
2941da177e4SLinus Torvalds  * Block device functions
2951da177e4SLinus Torvalds  */
296432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
297432a212cSMike Anderson {
298432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
299432a212cSMike Anderson }
300432a212cSMike Anderson 
301fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3021da177e4SLinus Torvalds {
3031da177e4SLinus Torvalds 	struct mapped_device *md;
3041da177e4SLinus Torvalds 
305fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
306fba9f90eSJeff Mahoney 
307fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
308fba9f90eSJeff Mahoney 	if (!md)
309fba9f90eSJeff Mahoney 		goto out;
310fba9f90eSJeff Mahoney 
3115c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
312432a212cSMike Anderson 	    dm_deleting_md(md)) {
313fba9f90eSJeff Mahoney 		md = NULL;
314fba9f90eSJeff Mahoney 		goto out;
315fba9f90eSJeff Mahoney 	}
316fba9f90eSJeff Mahoney 
3171da177e4SLinus Torvalds 	dm_get(md);
3185c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
319fba9f90eSJeff Mahoney out:
320fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
321fba9f90eSJeff Mahoney 
322fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3231da177e4SLinus Torvalds }
3241da177e4SLinus Torvalds 
325db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode)
3261da177e4SLinus Torvalds {
32763a4f065SMike Snitzer 	struct mapped_device *md;
3286e9624b8SArnd Bergmann 
3294a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
3304a1aeb98SMilan Broz 
33163a4f065SMike Snitzer 	md = disk->private_data;
33263a4f065SMike Snitzer 	if (WARN_ON(!md))
33363a4f065SMike Snitzer 		goto out;
33463a4f065SMike Snitzer 
3352c140a24SMikulas Patocka 	if (atomic_dec_and_test(&md->open_count) &&
3362c140a24SMikulas Patocka 	    (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
337acfe0ad7SMikulas Patocka 		queue_work(deferred_remove_workqueue, &deferred_remove_work);
3382c140a24SMikulas Patocka 
3391da177e4SLinus Torvalds 	dm_put(md);
34063a4f065SMike Snitzer out:
3414a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
3445c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3455c6bd75dSAlasdair G Kergon {
3465c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3475c6bd75dSAlasdair G Kergon }
3485c6bd75dSAlasdair G Kergon 
3495c6bd75dSAlasdair G Kergon /*
3505c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3515c6bd75dSAlasdair G Kergon  */
3522c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
3535c6bd75dSAlasdair G Kergon {
3545c6bd75dSAlasdair G Kergon 	int r = 0;
3555c6bd75dSAlasdair G Kergon 
3565c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
3575c6bd75dSAlasdair G Kergon 
3582c140a24SMikulas Patocka 	if (dm_open_count(md)) {
3595c6bd75dSAlasdair G Kergon 		r = -EBUSY;
3602c140a24SMikulas Patocka 		if (mark_deferred)
3612c140a24SMikulas Patocka 			set_bit(DMF_DEFERRED_REMOVE, &md->flags);
3622c140a24SMikulas Patocka 	} else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
3632c140a24SMikulas Patocka 		r = -EEXIST;
3645c6bd75dSAlasdair G Kergon 	else
3655c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
3665c6bd75dSAlasdair G Kergon 
3675c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
3685c6bd75dSAlasdair G Kergon 
3695c6bd75dSAlasdair G Kergon 	return r;
3705c6bd75dSAlasdair G Kergon }
3715c6bd75dSAlasdair G Kergon 
3722c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md)
3732c140a24SMikulas Patocka {
3742c140a24SMikulas Patocka 	int r = 0;
3752c140a24SMikulas Patocka 
3762c140a24SMikulas Patocka 	spin_lock(&_minor_lock);
3772c140a24SMikulas Patocka 
3782c140a24SMikulas Patocka 	if (test_bit(DMF_DELETING, &md->flags))
3792c140a24SMikulas Patocka 		r = -EBUSY;
3802c140a24SMikulas Patocka 	else
3812c140a24SMikulas Patocka 		clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
3822c140a24SMikulas Patocka 
3832c140a24SMikulas Patocka 	spin_unlock(&_minor_lock);
3842c140a24SMikulas Patocka 
3852c140a24SMikulas Patocka 	return r;
3862c140a24SMikulas Patocka }
3872c140a24SMikulas Patocka 
3882c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w)
3892c140a24SMikulas Patocka {
3902c140a24SMikulas Patocka 	dm_deferred_remove();
3912c140a24SMikulas Patocka }
3922c140a24SMikulas Patocka 
393fd2ed4d2SMikulas Patocka sector_t dm_get_size(struct mapped_device *md)
394fd2ed4d2SMikulas Patocka {
395fd2ed4d2SMikulas Patocka 	return get_capacity(md->disk);
396fd2ed4d2SMikulas Patocka }
397fd2ed4d2SMikulas Patocka 
3989974fa2cSMike Snitzer struct request_queue *dm_get_md_queue(struct mapped_device *md)
3999974fa2cSMike Snitzer {
4009974fa2cSMike Snitzer 	return md->queue;
4019974fa2cSMike Snitzer }
4029974fa2cSMike Snitzer 
403fd2ed4d2SMikulas Patocka struct dm_stats *dm_get_stats(struct mapped_device *md)
404fd2ed4d2SMikulas Patocka {
405fd2ed4d2SMikulas Patocka 	return &md->stats;
406fd2ed4d2SMikulas Patocka }
407fd2ed4d2SMikulas Patocka 
4083ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4093ac51e74SDarrick J. Wong {
4103ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
4113ac51e74SDarrick J. Wong 
4123ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
4133ac51e74SDarrick J. Wong }
4143ac51e74SDarrick J. Wong 
415956a4025SMike Snitzer static int dm_grab_bdev_for_ioctl(struct mapped_device *md,
41666482026SMike Snitzer 				  struct block_device **bdev,
417956a4025SMike Snitzer 				  fmode_t *mode)
418aa129a22SMilan Broz {
41966482026SMike Snitzer 	struct dm_target *tgt;
4206c182cd8SHannes Reinecke 	struct dm_table *map;
421956a4025SMike Snitzer 	int srcu_idx, r;
422aa129a22SMilan Broz 
4236c182cd8SHannes Reinecke retry:
424e56f81e0SChristoph Hellwig 	r = -ENOTTY;
425956a4025SMike Snitzer 	map = dm_get_live_table(md, &srcu_idx);
426aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
427aa129a22SMilan Broz 		goto out;
428aa129a22SMilan Broz 
429aa129a22SMilan Broz 	/* We only support devices that have a single target */
430aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
431aa129a22SMilan Broz 		goto out;
432aa129a22SMilan Broz 
43366482026SMike Snitzer 	tgt = dm_table_get_target(map, 0);
43466482026SMike Snitzer 	if (!tgt->type->prepare_ioctl)
4354d341d82SMike Snitzer 		goto out;
436aa129a22SMilan Broz 
4374f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
438aa129a22SMilan Broz 		r = -EAGAIN;
439aa129a22SMilan Broz 		goto out;
440aa129a22SMilan Broz 	}
441aa129a22SMilan Broz 
44266482026SMike Snitzer 	r = tgt->type->prepare_ioctl(tgt, bdev, mode);
443e56f81e0SChristoph Hellwig 	if (r < 0)
444e56f81e0SChristoph Hellwig 		goto out;
445e56f81e0SChristoph Hellwig 
446956a4025SMike Snitzer 	bdgrab(*bdev);
447956a4025SMike Snitzer 	dm_put_live_table(md, srcu_idx);
448e56f81e0SChristoph Hellwig 	return r;
449aa129a22SMilan Broz 
450aa129a22SMilan Broz out:
451956a4025SMike Snitzer 	dm_put_live_table(md, srcu_idx);
4525bbbfdf6SJunichi Nomura 	if (r == -ENOTCONN && !fatal_signal_pending(current)) {
4536c182cd8SHannes Reinecke 		msleep(10);
4546c182cd8SHannes Reinecke 		goto retry;
4556c182cd8SHannes Reinecke 	}
456e56f81e0SChristoph Hellwig 	return r;
457e56f81e0SChristoph Hellwig }
4586c182cd8SHannes Reinecke 
459e56f81e0SChristoph Hellwig static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
460e56f81e0SChristoph Hellwig 			unsigned int cmd, unsigned long arg)
461e56f81e0SChristoph Hellwig {
462e56f81e0SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
463956a4025SMike Snitzer 	int r;
464e56f81e0SChristoph Hellwig 
465956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
466e56f81e0SChristoph Hellwig 	if (r < 0)
467e56f81e0SChristoph Hellwig 		return r;
468e56f81e0SChristoph Hellwig 
469e56f81e0SChristoph Hellwig 	if (r > 0) {
470e56f81e0SChristoph Hellwig 		/*
471e980f623SChristoph Hellwig 		 * Target determined this ioctl is being issued against a
472e980f623SChristoph Hellwig 		 * subset of the parent bdev; require extra privileges.
473e56f81e0SChristoph Hellwig 		 */
474e980f623SChristoph Hellwig 		if (!capable(CAP_SYS_RAWIO)) {
475e980f623SChristoph Hellwig 			DMWARN_LIMIT(
476e980f623SChristoph Hellwig 	"%s: sending ioctl %x to DM device without required privilege.",
477e980f623SChristoph Hellwig 				current->comm, cmd);
478e980f623SChristoph Hellwig 			r = -ENOIOCTLCMD;
479e56f81e0SChristoph Hellwig 			goto out;
480e56f81e0SChristoph Hellwig 		}
481e980f623SChristoph Hellwig 	}
482e56f81e0SChristoph Hellwig 
48366482026SMike Snitzer 	r =  __blkdev_driver_ioctl(bdev, mode, cmd, arg);
484e56f81e0SChristoph Hellwig out:
485956a4025SMike Snitzer 	bdput(bdev);
486aa129a22SMilan Broz 	return r;
487aa129a22SMilan Broz }
488aa129a22SMilan Broz 
489028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
4901da177e4SLinus Torvalds {
4911da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
4921da177e4SLinus Torvalds }
4931da177e4SLinus Torvalds 
494028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
4951da177e4SLinus Torvalds {
4961da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
4971da177e4SLinus Torvalds }
4981da177e4SLinus Torvalds 
499cfae7529SMike Snitzer static void free_tio(struct dm_target_io *tio)
5001da177e4SLinus Torvalds {
501dba14160SMikulas Patocka 	bio_put(&tio->clone);
5021da177e4SLinus Torvalds }
5031da177e4SLinus Torvalds 
5044cc96131SMike Snitzer int md_in_flight(struct mapped_device *md)
50590abb8c4SKiyoshi Ueda {
50690abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
50790abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
50890abb8c4SKiyoshi Ueda }
50990abb8c4SKiyoshi Ueda 
5103eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
5113eaf840eSJun'ichi "Nick" Nomura {
5123eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
513fd2ed4d2SMikulas Patocka 	struct bio *bio = io->bio;
514c9959059STejun Heo 	int cpu;
515fd2ed4d2SMikulas Patocka 	int rw = bio_data_dir(bio);
5163eaf840eSJun'ichi "Nick" Nomura 
5173eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
5183eaf840eSJun'ichi "Nick" Nomura 
519074a7acaSTejun Heo 	cpu = part_stat_lock();
520d62e26b3SJens Axboe 	part_round_stats(md->queue, cpu, &dm_disk(md)->part0);
521074a7acaSTejun Heo 	part_stat_unlock();
5221e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw],
5231e9bb880SShaohua Li 		atomic_inc_return(&md->pending[rw]));
524fd2ed4d2SMikulas Patocka 
525fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
526528ec5abSMike Christie 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
527528ec5abSMike Christie 				    bio->bi_iter.bi_sector, bio_sectors(bio),
528528ec5abSMike Christie 				    false, 0, &io->stats_aux);
5293eaf840eSJun'ichi "Nick" Nomura }
5303eaf840eSJun'ichi "Nick" Nomura 
531d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
5323eaf840eSJun'ichi "Nick" Nomura {
5333eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
5343eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
5353eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
53618c0b223SGu Zheng 	int pending;
5373eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
5383eaf840eSJun'ichi "Nick" Nomura 
539d62e26b3SJens Axboe 	generic_end_io_acct(md->queue, rw, &dm_disk(md)->part0, io->start_time);
5403eaf840eSJun'ichi "Nick" Nomura 
541fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
542528ec5abSMike Christie 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
543528ec5abSMike Christie 				    bio->bi_iter.bi_sector, bio_sectors(bio),
544528ec5abSMike Christie 				    true, duration, &io->stats_aux);
545fd2ed4d2SMikulas Patocka 
546af7e466aSMikulas Patocka 	/*
547af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
548d87f4c14STejun Heo 	 * a flush.
549af7e466aSMikulas Patocka 	 */
5501e9bb880SShaohua Li 	pending = atomic_dec_return(&md->pending[rw]);
5511e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
552316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
5533eaf840eSJun'ichi "Nick" Nomura 
554d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
555d221d2e7SMikulas Patocka 	if (!pending)
556d221d2e7SMikulas Patocka 		wake_up(&md->wait);
5573eaf840eSJun'ichi "Nick" Nomura }
5583eaf840eSJun'ichi "Nick" Nomura 
5591da177e4SLinus Torvalds /*
5601da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
5611da177e4SLinus Torvalds  */
56292c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
5631da177e4SLinus Torvalds {
56405447420SKiyoshi Ueda 	unsigned long flags;
5651da177e4SLinus Torvalds 
56605447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
5671da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
56805447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
56992c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds /*
5731da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
5741da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
57583d5e5b0SMikulas Patocka  * dm_put_live_table() when finished.
5761da177e4SLinus Torvalds  */
57783d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
5781da177e4SLinus Torvalds {
57983d5e5b0SMikulas Patocka 	*srcu_idx = srcu_read_lock(&md->io_barrier);
5801da177e4SLinus Torvalds 
58183d5e5b0SMikulas Patocka 	return srcu_dereference(md->map, &md->io_barrier);
58283d5e5b0SMikulas Patocka }
5831da177e4SLinus Torvalds 
58483d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
58583d5e5b0SMikulas Patocka {
58683d5e5b0SMikulas Patocka 	srcu_read_unlock(&md->io_barrier, srcu_idx);
58783d5e5b0SMikulas Patocka }
58883d5e5b0SMikulas Patocka 
58983d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md)
59083d5e5b0SMikulas Patocka {
59183d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
59283d5e5b0SMikulas Patocka 	synchronize_rcu_expedited();
59383d5e5b0SMikulas Patocka }
59483d5e5b0SMikulas Patocka 
59583d5e5b0SMikulas Patocka /*
59683d5e5b0SMikulas Patocka  * A fast alternative to dm_get_live_table/dm_put_live_table.
59783d5e5b0SMikulas Patocka  * The caller must not block between these two functions.
59883d5e5b0SMikulas Patocka  */
59983d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
60083d5e5b0SMikulas Patocka {
60183d5e5b0SMikulas Patocka 	rcu_read_lock();
60283d5e5b0SMikulas Patocka 	return rcu_dereference(md->map);
60383d5e5b0SMikulas Patocka }
60483d5e5b0SMikulas Patocka 
60583d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
60683d5e5b0SMikulas Patocka {
60783d5e5b0SMikulas Patocka 	rcu_read_unlock();
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
6103ac51e74SDarrick J. Wong /*
61186f1152bSBenjamin Marzinski  * Open a table device so we can use it as a map destination.
61286f1152bSBenjamin Marzinski  */
61386f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev,
61486f1152bSBenjamin Marzinski 			     struct mapped_device *md)
61586f1152bSBenjamin Marzinski {
61686f1152bSBenjamin Marzinski 	static char *_claim_ptr = "I belong to device-mapper";
61786f1152bSBenjamin Marzinski 	struct block_device *bdev;
61886f1152bSBenjamin Marzinski 
61986f1152bSBenjamin Marzinski 	int r;
62086f1152bSBenjamin Marzinski 
62186f1152bSBenjamin Marzinski 	BUG_ON(td->dm_dev.bdev);
62286f1152bSBenjamin Marzinski 
62386f1152bSBenjamin Marzinski 	bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
62486f1152bSBenjamin Marzinski 	if (IS_ERR(bdev))
62586f1152bSBenjamin Marzinski 		return PTR_ERR(bdev);
62686f1152bSBenjamin Marzinski 
62786f1152bSBenjamin Marzinski 	r = bd_link_disk_holder(bdev, dm_disk(md));
62886f1152bSBenjamin Marzinski 	if (r) {
62986f1152bSBenjamin Marzinski 		blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
63086f1152bSBenjamin Marzinski 		return r;
63186f1152bSBenjamin Marzinski 	}
63286f1152bSBenjamin Marzinski 
63386f1152bSBenjamin Marzinski 	td->dm_dev.bdev = bdev;
634817bf402SDan Williams 	td->dm_dev.dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
63586f1152bSBenjamin Marzinski 	return 0;
63686f1152bSBenjamin Marzinski }
63786f1152bSBenjamin Marzinski 
63886f1152bSBenjamin Marzinski /*
63986f1152bSBenjamin Marzinski  * Close a table device that we've been using.
64086f1152bSBenjamin Marzinski  */
64186f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md)
64286f1152bSBenjamin Marzinski {
64386f1152bSBenjamin Marzinski 	if (!td->dm_dev.bdev)
64486f1152bSBenjamin Marzinski 		return;
64586f1152bSBenjamin Marzinski 
64686f1152bSBenjamin Marzinski 	bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
64786f1152bSBenjamin Marzinski 	blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
648817bf402SDan Williams 	put_dax(td->dm_dev.dax_dev);
64986f1152bSBenjamin Marzinski 	td->dm_dev.bdev = NULL;
650817bf402SDan Williams 	td->dm_dev.dax_dev = NULL;
65186f1152bSBenjamin Marzinski }
65286f1152bSBenjamin Marzinski 
65386f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev,
65486f1152bSBenjamin Marzinski 					      fmode_t mode) {
65586f1152bSBenjamin Marzinski 	struct table_device *td;
65686f1152bSBenjamin Marzinski 
65786f1152bSBenjamin Marzinski 	list_for_each_entry(td, l, list)
65886f1152bSBenjamin Marzinski 		if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
65986f1152bSBenjamin Marzinski 			return td;
66086f1152bSBenjamin Marzinski 
66186f1152bSBenjamin Marzinski 	return NULL;
66286f1152bSBenjamin Marzinski }
66386f1152bSBenjamin Marzinski 
66486f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
66586f1152bSBenjamin Marzinski 			struct dm_dev **result) {
66686f1152bSBenjamin Marzinski 	int r;
66786f1152bSBenjamin Marzinski 	struct table_device *td;
66886f1152bSBenjamin Marzinski 
66986f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
67086f1152bSBenjamin Marzinski 	td = find_table_device(&md->table_devices, dev, mode);
67186f1152bSBenjamin Marzinski 	if (!td) {
672115485e8SMike Snitzer 		td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
67386f1152bSBenjamin Marzinski 		if (!td) {
67486f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
67586f1152bSBenjamin Marzinski 			return -ENOMEM;
67686f1152bSBenjamin Marzinski 		}
67786f1152bSBenjamin Marzinski 
67886f1152bSBenjamin Marzinski 		td->dm_dev.mode = mode;
67986f1152bSBenjamin Marzinski 		td->dm_dev.bdev = NULL;
68086f1152bSBenjamin Marzinski 
68186f1152bSBenjamin Marzinski 		if ((r = open_table_device(td, dev, md))) {
68286f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
68386f1152bSBenjamin Marzinski 			kfree(td);
68486f1152bSBenjamin Marzinski 			return r;
68586f1152bSBenjamin Marzinski 		}
68686f1152bSBenjamin Marzinski 
68786f1152bSBenjamin Marzinski 		format_dev_t(td->dm_dev.name, dev);
68886f1152bSBenjamin Marzinski 
689b0b4d7c6SElena Reshetova 		refcount_set(&td->count, 1);
69086f1152bSBenjamin Marzinski 		list_add(&td->list, &md->table_devices);
691b0b4d7c6SElena Reshetova 	} else {
692b0b4d7c6SElena Reshetova 		refcount_inc(&td->count);
69386f1152bSBenjamin Marzinski 	}
69486f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
69586f1152bSBenjamin Marzinski 
69686f1152bSBenjamin Marzinski 	*result = &td->dm_dev;
69786f1152bSBenjamin Marzinski 	return 0;
69886f1152bSBenjamin Marzinski }
69986f1152bSBenjamin Marzinski EXPORT_SYMBOL_GPL(dm_get_table_device);
70086f1152bSBenjamin Marzinski 
70186f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
70286f1152bSBenjamin Marzinski {
70386f1152bSBenjamin Marzinski 	struct table_device *td = container_of(d, struct table_device, dm_dev);
70486f1152bSBenjamin Marzinski 
70586f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
706b0b4d7c6SElena Reshetova 	if (refcount_dec_and_test(&td->count)) {
70786f1152bSBenjamin Marzinski 		close_table_device(td, md);
70886f1152bSBenjamin Marzinski 		list_del(&td->list);
70986f1152bSBenjamin Marzinski 		kfree(td);
71086f1152bSBenjamin Marzinski 	}
71186f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
71286f1152bSBenjamin Marzinski }
71386f1152bSBenjamin Marzinski EXPORT_SYMBOL(dm_put_table_device);
71486f1152bSBenjamin Marzinski 
71586f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices)
71686f1152bSBenjamin Marzinski {
71786f1152bSBenjamin Marzinski 	struct list_head *tmp, *next;
71886f1152bSBenjamin Marzinski 
71986f1152bSBenjamin Marzinski 	list_for_each_safe(tmp, next, devices) {
72086f1152bSBenjamin Marzinski 		struct table_device *td = list_entry(tmp, struct table_device, list);
72186f1152bSBenjamin Marzinski 
72286f1152bSBenjamin Marzinski 		DMWARN("dm_destroy: %s still exists with %d references",
723b0b4d7c6SElena Reshetova 		       td->dm_dev.name, refcount_read(&td->count));
72486f1152bSBenjamin Marzinski 		kfree(td);
72586f1152bSBenjamin Marzinski 	}
72686f1152bSBenjamin Marzinski }
72786f1152bSBenjamin Marzinski 
72886f1152bSBenjamin Marzinski /*
7293ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
7303ac51e74SDarrick J. Wong  */
7313ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
7323ac51e74SDarrick J. Wong {
7333ac51e74SDarrick J. Wong 	*geo = md->geometry;
7343ac51e74SDarrick J. Wong 
7353ac51e74SDarrick J. Wong 	return 0;
7363ac51e74SDarrick J. Wong }
7373ac51e74SDarrick J. Wong 
7383ac51e74SDarrick J. Wong /*
7393ac51e74SDarrick J. Wong  * Set the geometry of a device.
7403ac51e74SDarrick J. Wong  */
7413ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
7423ac51e74SDarrick J. Wong {
7433ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
7443ac51e74SDarrick J. Wong 
7453ac51e74SDarrick J. Wong 	if (geo->start > sz) {
7463ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
7473ac51e74SDarrick J. Wong 		return -EINVAL;
7483ac51e74SDarrick J. Wong 	}
7493ac51e74SDarrick J. Wong 
7503ac51e74SDarrick J. Wong 	md->geometry = *geo;
7513ac51e74SDarrick J. Wong 
7523ac51e74SDarrick J. Wong 	return 0;
7533ac51e74SDarrick J. Wong }
7543ac51e74SDarrick J. Wong 
7551da177e4SLinus Torvalds /*-----------------------------------------------------------------
7561da177e4SLinus Torvalds  * CRUD START:
7571da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
7581da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
7591da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
7601da177e4SLinus Torvalds  *   interests of getting something for people to use I give
7611da177e4SLinus Torvalds  *   you this clearly demarcated crap.
7621da177e4SLinus Torvalds  *---------------------------------------------------------------*/
7631da177e4SLinus Torvalds 
7642e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
7652e93ccc1SKiyoshi Ueda {
7662e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
7672e93ccc1SKiyoshi Ueda }
7682e93ccc1SKiyoshi Ueda 
7691da177e4SLinus Torvalds /*
7701da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
7711da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
7721da177e4SLinus Torvalds  */
7734e4cbee9SChristoph Hellwig static void dec_pending(struct dm_io *io, blk_status_t error)
7741da177e4SLinus Torvalds {
7752e93ccc1SKiyoshi Ueda 	unsigned long flags;
7764e4cbee9SChristoph Hellwig 	blk_status_t io_error;
777b35f8caaSMilan Broz 	struct bio *bio;
778b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
7792e93ccc1SKiyoshi Ueda 
7802e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
781f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
782f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
7834e4cbee9SChristoph Hellwig 		if (!(io->status == BLK_STS_DM_REQUEUE &&
7844e4cbee9SChristoph Hellwig 				__noflush_suspending(md)))
7854e4cbee9SChristoph Hellwig 			io->status = error;
786f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
787f88fb981SKiyoshi Ueda 	}
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
7904e4cbee9SChristoph Hellwig 		if (io->status == BLK_STS_DM_REQUEUE) {
7912e93ccc1SKiyoshi Ueda 			/*
7922e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
7932e93ccc1SKiyoshi Ueda 			 */
794022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
7956a8736d1STejun Heo 			if (__noflush_suspending(md))
7966a8736d1STejun Heo 				bio_list_add_head(&md->deferred, io->bio);
7976a8736d1STejun Heo 			else
7982e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
7994e4cbee9SChristoph Hellwig 				io->status = BLK_STS_IOERR;
800022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
8012e93ccc1SKiyoshi Ueda 		}
8022e93ccc1SKiyoshi Ueda 
8034e4cbee9SChristoph Hellwig 		io_error = io->status;
804b35f8caaSMilan Broz 		bio = io->bio;
805af7e466aSMikulas Patocka 		end_io_acct(io);
806a97f925aSMikulas Patocka 		free_io(md, io);
8071da177e4SLinus Torvalds 
8084e4cbee9SChristoph Hellwig 		if (io_error == BLK_STS_DM_REQUEUE)
8096a8736d1STejun Heo 			return;
8106a8736d1STejun Heo 
8111eff9d32SJens Axboe 		if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) {
8121da177e4SLinus Torvalds 			/*
8136a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
81428a8f0d3SMike Christie 			 * without REQ_PREFLUSH.
8151da177e4SLinus Torvalds 			 */
8161eff9d32SJens Axboe 			bio->bi_opf &= ~REQ_PREFLUSH;
8176a8736d1STejun Heo 			queue_io(md, bio);
8185f3ea37cSArnaldo Carvalho de Melo 		} else {
819b372d360SMike Snitzer 			/* done with normal IO or empty flush */
8204e4cbee9SChristoph Hellwig 			bio->bi_status = io_error;
8214246a0b6SChristoph Hellwig 			bio_endio(bio);
8222e93ccc1SKiyoshi Ueda 		}
8231da177e4SLinus Torvalds 	}
824af7e466aSMikulas Patocka }
8251da177e4SLinus Torvalds 
8264cc96131SMike Snitzer void disable_write_same(struct mapped_device *md)
8277eee4ae2SMike Snitzer {
8287eee4ae2SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
8297eee4ae2SMike Snitzer 
8307eee4ae2SMike Snitzer 	/* device doesn't really support WRITE SAME, disable it */
8317eee4ae2SMike Snitzer 	limits->max_write_same_sectors = 0;
8327eee4ae2SMike Snitzer }
8337eee4ae2SMike Snitzer 
834ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md)
835ac62d620SChristoph Hellwig {
836ac62d620SChristoph Hellwig 	struct queue_limits *limits = dm_get_queue_limits(md);
837ac62d620SChristoph Hellwig 
838ac62d620SChristoph Hellwig 	/* device doesn't really support WRITE ZEROES, disable it */
839ac62d620SChristoph Hellwig 	limits->max_write_zeroes_sectors = 0;
840ac62d620SChristoph Hellwig }
841ac62d620SChristoph Hellwig 
8424246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio)
8431da177e4SLinus Torvalds {
8444e4cbee9SChristoph Hellwig 	blk_status_t error = bio->bi_status;
845bfc6d41cSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
846b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
8479faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
8481da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
8491da177e4SLinus Torvalds 
8504e4cbee9SChristoph Hellwig 	if (unlikely(error == BLK_STS_TARGET)) {
851ac62d620SChristoph Hellwig 		if (bio_op(bio) == REQ_OP_WRITE_SAME &&
85274d46992SChristoph Hellwig 		    !bio->bi_disk->queue->limits.max_write_same_sectors)
8537eee4ae2SMike Snitzer 			disable_write_same(md);
854ac62d620SChristoph Hellwig 		if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
85574d46992SChristoph Hellwig 		    !bio->bi_disk->queue->limits.max_write_zeroes_sectors)
856ac62d620SChristoph Hellwig 			disable_write_zeroes(md);
857ac62d620SChristoph Hellwig 	}
8587eee4ae2SMike Snitzer 
8591be56909SChristoph Hellwig 	if (endio) {
8604e4cbee9SChristoph Hellwig 		int r = endio(tio->ti, bio, &error);
8611be56909SChristoph Hellwig 		switch (r) {
8621be56909SChristoph Hellwig 		case DM_ENDIO_REQUEUE:
8634e4cbee9SChristoph Hellwig 			error = BLK_STS_DM_REQUEUE;
8641be56909SChristoph Hellwig 			/*FALLTHRU*/
8651be56909SChristoph Hellwig 		case DM_ENDIO_DONE:
8661be56909SChristoph Hellwig 			break;
8671be56909SChristoph Hellwig 		case DM_ENDIO_INCOMPLETE:
8681be56909SChristoph Hellwig 			/* The target will handle the io */
8691be56909SChristoph Hellwig 			return;
8701be56909SChristoph Hellwig 		default:
8711be56909SChristoph Hellwig 			DMWARN("unimplemented target endio return value: %d", r);
8721be56909SChristoph Hellwig 			BUG();
8731be56909SChristoph Hellwig 		}
8741be56909SChristoph Hellwig 	}
8751be56909SChristoph Hellwig 
876cfae7529SMike Snitzer 	free_tio(tio);
877b35f8caaSMilan Broz 	dec_pending(io, error);
8781da177e4SLinus Torvalds }
8791da177e4SLinus Torvalds 
88078d8e58aSMike Snitzer /*
88156a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
88256a67df7SMike Snitzer  * target boundary.
88356a67df7SMike Snitzer  */
88456a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
8851da177e4SLinus Torvalds {
88656a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
88756a67df7SMike Snitzer 
88856a67df7SMike Snitzer 	return ti->len - target_offset;
88956a67df7SMike Snitzer }
89056a67df7SMike Snitzer 
89156a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
89256a67df7SMike Snitzer {
89356a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
894542f9038SMike Snitzer 	sector_t offset, max_len;
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	/*
8971da177e4SLinus Torvalds 	 * Does the target need to split even further?
8981da177e4SLinus Torvalds 	 */
899542f9038SMike Snitzer 	if (ti->max_io_len) {
900542f9038SMike Snitzer 		offset = dm_target_offset(ti, sector);
901542f9038SMike Snitzer 		if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
902542f9038SMike Snitzer 			max_len = sector_div(offset, ti->max_io_len);
903542f9038SMike Snitzer 		else
904542f9038SMike Snitzer 			max_len = offset & (ti->max_io_len - 1);
905542f9038SMike Snitzer 		max_len = ti->max_io_len - max_len;
906542f9038SMike Snitzer 
907542f9038SMike Snitzer 		if (len > max_len)
908542f9038SMike Snitzer 			len = max_len;
9091da177e4SLinus Torvalds 	}
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	return len;
9121da177e4SLinus Torvalds }
9131da177e4SLinus Torvalds 
914542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
915542f9038SMike Snitzer {
916542f9038SMike Snitzer 	if (len > UINT_MAX) {
917542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
918542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
919542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
920542f9038SMike Snitzer 		return -EINVAL;
921542f9038SMike Snitzer 	}
922542f9038SMike Snitzer 
923542f9038SMike Snitzer 	ti->max_io_len = (uint32_t) len;
924542f9038SMike Snitzer 
925542f9038SMike Snitzer 	return 0;
926542f9038SMike Snitzer }
927542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
928542f9038SMike Snitzer 
929f26c5719SDan Williams static struct dm_target *dm_dax_get_live_target(struct mapped_device *md,
930f26c5719SDan Williams 		sector_t sector, int *srcu_idx)
931545ed20eSToshi Kani {
932545ed20eSToshi Kani 	struct dm_table *map;
933545ed20eSToshi Kani 	struct dm_target *ti;
934545ed20eSToshi Kani 
935f26c5719SDan Williams 	map = dm_get_live_table(md, srcu_idx);
936545ed20eSToshi Kani 	if (!map)
937f26c5719SDan Williams 		return NULL;
938545ed20eSToshi Kani 
939545ed20eSToshi Kani 	ti = dm_table_find_target(map, sector);
940545ed20eSToshi Kani 	if (!dm_target_is_valid(ti))
941f26c5719SDan Williams 		return NULL;
942f26c5719SDan Williams 
943f26c5719SDan Williams 	return ti;
944f26c5719SDan Williams }
945f26c5719SDan Williams 
946f26c5719SDan Williams static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
947f26c5719SDan Williams 		long nr_pages, void **kaddr, pfn_t *pfn)
948f26c5719SDan Williams {
949f26c5719SDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
950f26c5719SDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
951f26c5719SDan Williams 	struct dm_target *ti;
952f26c5719SDan Williams 	long len, ret = -EIO;
953f26c5719SDan Williams 	int srcu_idx;
954f26c5719SDan Williams 
955f26c5719SDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
956f26c5719SDan Williams 
957f26c5719SDan Williams 	if (!ti)
958545ed20eSToshi Kani 		goto out;
959f26c5719SDan Williams 	if (!ti->type->direct_access)
960f26c5719SDan Williams 		goto out;
961f26c5719SDan Williams 	len = max_io_len(sector, ti) / PAGE_SECTORS;
962f26c5719SDan Williams 	if (len < 1)
963f26c5719SDan Williams 		goto out;
964f26c5719SDan Williams 	nr_pages = min(len, nr_pages);
965545ed20eSToshi Kani 	if (ti->type->direct_access)
966817bf402SDan Williams 		ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn);
967817bf402SDan Williams 
968545ed20eSToshi Kani  out:
969545ed20eSToshi Kani 	dm_put_live_table(md, srcu_idx);
970f26c5719SDan Williams 
971f26c5719SDan Williams 	return ret;
972545ed20eSToshi Kani }
973545ed20eSToshi Kani 
9747e026c8cSDan Williams static size_t dm_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
9757e026c8cSDan Williams 		void *addr, size_t bytes, struct iov_iter *i)
9767e026c8cSDan Williams {
9777e026c8cSDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
9787e026c8cSDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
9797e026c8cSDan Williams 	struct dm_target *ti;
9807e026c8cSDan Williams 	long ret = 0;
9817e026c8cSDan Williams 	int srcu_idx;
9827e026c8cSDan Williams 
9837e026c8cSDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
9847e026c8cSDan Williams 
9857e026c8cSDan Williams 	if (!ti)
9867e026c8cSDan Williams 		goto out;
9877e026c8cSDan Williams 	if (!ti->type->dax_copy_from_iter) {
9887e026c8cSDan Williams 		ret = copy_from_iter(addr, bytes, i);
9897e026c8cSDan Williams 		goto out;
9907e026c8cSDan Williams 	}
9917e026c8cSDan Williams 	ret = ti->type->dax_copy_from_iter(ti, pgoff, addr, bytes, i);
9927e026c8cSDan Williams  out:
9937e026c8cSDan Williams 	dm_put_live_table(md, srcu_idx);
9947e026c8cSDan Williams 
9957e026c8cSDan Williams 	return ret;
9967e026c8cSDan Williams }
9977e026c8cSDan Williams 
9981dd40c3eSMikulas Patocka /*
9991dd40c3eSMikulas Patocka  * A target may call dm_accept_partial_bio only from the map routine.  It is
1000c06b3e58SNeilBrown  * allowed for all bio types except REQ_PREFLUSH and REQ_OP_ZONE_RESET.
10011dd40c3eSMikulas Patocka  *
10021dd40c3eSMikulas Patocka  * dm_accept_partial_bio informs the dm that the target only wants to process
10031dd40c3eSMikulas Patocka  * additional n_sectors sectors of the bio and the rest of the data should be
10041dd40c3eSMikulas Patocka  * sent in a next bio.
10051dd40c3eSMikulas Patocka  *
10061dd40c3eSMikulas Patocka  * A diagram that explains the arithmetics:
10071dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
10081dd40c3eSMikulas Patocka  * |         1          |       2       |   3   |
10091dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
10101dd40c3eSMikulas Patocka  *
10111dd40c3eSMikulas Patocka  * <-------------- *tio->len_ptr --------------->
10121dd40c3eSMikulas Patocka  *                      <------- bi_size ------->
10131dd40c3eSMikulas Patocka  *                      <-- n_sectors -->
10141dd40c3eSMikulas Patocka  *
10151dd40c3eSMikulas Patocka  * Region 1 was already iterated over with bio_advance or similar function.
10161dd40c3eSMikulas Patocka  *	(it may be empty if the target doesn't use bio_advance)
10171dd40c3eSMikulas Patocka  * Region 2 is the remaining bio size that the target wants to process.
10181dd40c3eSMikulas Patocka  *	(it may be empty if region 1 is non-empty, although there is no reason
10191dd40c3eSMikulas Patocka  *	 to make it empty)
10201dd40c3eSMikulas Patocka  * The target requires that region 3 is to be sent in the next bio.
10211dd40c3eSMikulas Patocka  *
10221dd40c3eSMikulas Patocka  * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
10231dd40c3eSMikulas Patocka  * the partially processed part (the sum of regions 1+2) must be the same for all
10241dd40c3eSMikulas Patocka  * copies of the bio.
10251dd40c3eSMikulas Patocka  */
10261dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
10271dd40c3eSMikulas Patocka {
10281dd40c3eSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
10291dd40c3eSMikulas Patocka 	unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
10301eff9d32SJens Axboe 	BUG_ON(bio->bi_opf & REQ_PREFLUSH);
10311dd40c3eSMikulas Patocka 	BUG_ON(bi_size > *tio->len_ptr);
10321dd40c3eSMikulas Patocka 	BUG_ON(n_sectors > bi_size);
10331dd40c3eSMikulas Patocka 	*tio->len_ptr -= bi_size - n_sectors;
10341dd40c3eSMikulas Patocka 	bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
10351dd40c3eSMikulas Patocka }
10361dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
10371dd40c3eSMikulas Patocka 
1038d67a5f4bSMikulas Patocka /*
103910999307SDamien Le Moal  * The zone descriptors obtained with a zone report indicate
104010999307SDamien Le Moal  * zone positions within the target device. The zone descriptors
104110999307SDamien Le Moal  * must be remapped to match their position within the dm device.
104210999307SDamien Le Moal  * A target may call dm_remap_zone_report after completion of a
104310999307SDamien Le Moal  * REQ_OP_ZONE_REPORT bio to remap the zone descriptors obtained
104410999307SDamien Le Moal  * from the target device mapping to the dm device.
104510999307SDamien Le Moal  */
104610999307SDamien Le Moal void dm_remap_zone_report(struct dm_target *ti, struct bio *bio, sector_t start)
104710999307SDamien Le Moal {
104810999307SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
104910999307SDamien Le Moal 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
105010999307SDamien Le Moal 	struct bio *report_bio = tio->io->bio;
105110999307SDamien Le Moal 	struct blk_zone_report_hdr *hdr = NULL;
105210999307SDamien Le Moal 	struct blk_zone *zone;
105310999307SDamien Le Moal 	unsigned int nr_rep = 0;
105410999307SDamien Le Moal 	unsigned int ofst;
105510999307SDamien Le Moal 	struct bio_vec bvec;
105610999307SDamien Le Moal 	struct bvec_iter iter;
105710999307SDamien Le Moal 	void *addr;
105810999307SDamien Le Moal 
105910999307SDamien Le Moal 	if (bio->bi_status)
106010999307SDamien Le Moal 		return;
106110999307SDamien Le Moal 
106210999307SDamien Le Moal 	/*
106310999307SDamien Le Moal 	 * Remap the start sector of the reported zones. For sequential zones,
106410999307SDamien Le Moal 	 * also remap the write pointer position.
106510999307SDamien Le Moal 	 */
106610999307SDamien Le Moal 	bio_for_each_segment(bvec, report_bio, iter) {
106710999307SDamien Le Moal 		addr = kmap_atomic(bvec.bv_page);
106810999307SDamien Le Moal 
106910999307SDamien Le Moal 		/* Remember the report header in the first page */
107010999307SDamien Le Moal 		if (!hdr) {
107110999307SDamien Le Moal 			hdr = addr;
107210999307SDamien Le Moal 			ofst = sizeof(struct blk_zone_report_hdr);
107310999307SDamien Le Moal 		} else
107410999307SDamien Le Moal 			ofst = 0;
107510999307SDamien Le Moal 
107610999307SDamien Le Moal 		/* Set zones start sector */
107710999307SDamien Le Moal 		while (hdr->nr_zones && ofst < bvec.bv_len) {
107810999307SDamien Le Moal 			zone = addr + ofst;
107910999307SDamien Le Moal 			if (zone->start >= start + ti->len) {
108010999307SDamien Le Moal 				hdr->nr_zones = 0;
108110999307SDamien Le Moal 				break;
108210999307SDamien Le Moal 			}
108310999307SDamien Le Moal 			zone->start = zone->start + ti->begin - start;
108410999307SDamien Le Moal 			if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL) {
108510999307SDamien Le Moal 				if (zone->cond == BLK_ZONE_COND_FULL)
108610999307SDamien Le Moal 					zone->wp = zone->start + zone->len;
108710999307SDamien Le Moal 				else if (zone->cond == BLK_ZONE_COND_EMPTY)
108810999307SDamien Le Moal 					zone->wp = zone->start;
108910999307SDamien Le Moal 				else
109010999307SDamien Le Moal 					zone->wp = zone->wp + ti->begin - start;
109110999307SDamien Le Moal 			}
109210999307SDamien Le Moal 			ofst += sizeof(struct blk_zone);
109310999307SDamien Le Moal 			hdr->nr_zones--;
109410999307SDamien Le Moal 			nr_rep++;
109510999307SDamien Le Moal 		}
109610999307SDamien Le Moal 
109710999307SDamien Le Moal 		if (addr != hdr)
109810999307SDamien Le Moal 			kunmap_atomic(addr);
109910999307SDamien Le Moal 
110010999307SDamien Le Moal 		if (!hdr->nr_zones)
110110999307SDamien Le Moal 			break;
110210999307SDamien Le Moal 	}
110310999307SDamien Le Moal 
110410999307SDamien Le Moal 	if (hdr) {
110510999307SDamien Le Moal 		hdr->nr_zones = nr_rep;
110610999307SDamien Le Moal 		kunmap_atomic(hdr);
110710999307SDamien Le Moal 	}
110810999307SDamien Le Moal 
110910999307SDamien Le Moal 	bio_advance(report_bio, report_bio->bi_iter.bi_size);
111010999307SDamien Le Moal 
111110999307SDamien Le Moal #else /* !CONFIG_BLK_DEV_ZONED */
111210999307SDamien Le Moal 	bio->bi_status = BLK_STS_NOTSUPP;
111310999307SDamien Le Moal #endif
111410999307SDamien Le Moal }
111510999307SDamien Le Moal EXPORT_SYMBOL_GPL(dm_remap_zone_report);
111610999307SDamien Le Moal 
111710999307SDamien Le Moal /*
1118d67a5f4bSMikulas Patocka  * Flush current->bio_list when the target map method blocks.
1119d67a5f4bSMikulas Patocka  * This fixes deadlocks in snapshot and possibly in other targets.
1120d67a5f4bSMikulas Patocka  */
1121d67a5f4bSMikulas Patocka struct dm_offload {
1122d67a5f4bSMikulas Patocka 	struct blk_plug plug;
1123d67a5f4bSMikulas Patocka 	struct blk_plug_cb cb;
1124d67a5f4bSMikulas Patocka };
1125d67a5f4bSMikulas Patocka 
1126d67a5f4bSMikulas Patocka static void flush_current_bio_list(struct blk_plug_cb *cb, bool from_schedule)
1127d67a5f4bSMikulas Patocka {
1128d67a5f4bSMikulas Patocka 	struct dm_offload *o = container_of(cb, struct dm_offload, cb);
1129d67a5f4bSMikulas Patocka 	struct bio_list list;
1130d67a5f4bSMikulas Patocka 	struct bio *bio;
1131f5fe1b51SNeilBrown 	int i;
1132d67a5f4bSMikulas Patocka 
1133d67a5f4bSMikulas Patocka 	INIT_LIST_HEAD(&o->cb.list);
1134d67a5f4bSMikulas Patocka 
1135d67a5f4bSMikulas Patocka 	if (unlikely(!current->bio_list))
1136d67a5f4bSMikulas Patocka 		return;
1137d67a5f4bSMikulas Patocka 
1138f5fe1b51SNeilBrown 	for (i = 0; i < 2; i++) {
1139f5fe1b51SNeilBrown 		list = current->bio_list[i];
1140f5fe1b51SNeilBrown 		bio_list_init(&current->bio_list[i]);
1141d67a5f4bSMikulas Patocka 
1142d67a5f4bSMikulas Patocka 		while ((bio = bio_list_pop(&list))) {
1143d67a5f4bSMikulas Patocka 			struct bio_set *bs = bio->bi_pool;
114447e0fb46SNeilBrown 			if (unlikely(!bs) || bs == fs_bio_set ||
114547e0fb46SNeilBrown 			    !bs->rescue_workqueue) {
1146f5fe1b51SNeilBrown 				bio_list_add(&current->bio_list[i], bio);
1147d67a5f4bSMikulas Patocka 				continue;
1148d67a5f4bSMikulas Patocka 			}
1149d67a5f4bSMikulas Patocka 
1150d67a5f4bSMikulas Patocka 			spin_lock(&bs->rescue_lock);
1151d67a5f4bSMikulas Patocka 			bio_list_add(&bs->rescue_list, bio);
1152d67a5f4bSMikulas Patocka 			queue_work(bs->rescue_workqueue, &bs->rescue_work);
1153d67a5f4bSMikulas Patocka 			spin_unlock(&bs->rescue_lock);
1154d67a5f4bSMikulas Patocka 		}
1155d67a5f4bSMikulas Patocka 	}
1156f5fe1b51SNeilBrown }
1157d67a5f4bSMikulas Patocka 
1158d67a5f4bSMikulas Patocka static void dm_offload_start(struct dm_offload *o)
1159d67a5f4bSMikulas Patocka {
1160d67a5f4bSMikulas Patocka 	blk_start_plug(&o->plug);
1161d67a5f4bSMikulas Patocka 	o->cb.callback = flush_current_bio_list;
1162d67a5f4bSMikulas Patocka 	list_add(&o->cb.list, &current->plug->cb_list);
1163d67a5f4bSMikulas Patocka }
1164d67a5f4bSMikulas Patocka 
1165d67a5f4bSMikulas Patocka static void dm_offload_end(struct dm_offload *o)
1166d67a5f4bSMikulas Patocka {
1167d67a5f4bSMikulas Patocka 	list_del(&o->cb.list);
1168d67a5f4bSMikulas Patocka 	blk_finish_plug(&o->plug);
1169d67a5f4bSMikulas Patocka }
1170d67a5f4bSMikulas Patocka 
1171bd2a49b8SAlasdair G Kergon static void __map_bio(struct dm_target_io *tio)
11721da177e4SLinus Torvalds {
11731da177e4SLinus Torvalds 	int r;
11742056a782SJens Axboe 	sector_t sector;
1175d67a5f4bSMikulas Patocka 	struct dm_offload o;
1176dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
1177bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	/*
11821da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
11831da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
11841da177e4SLinus Torvalds 	 * this io.
11851da177e4SLinus Torvalds 	 */
11861da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
11874f024f37SKent Overstreet 	sector = clone->bi_iter.bi_sector;
1188d67a5f4bSMikulas Patocka 
1189d67a5f4bSMikulas Patocka 	dm_offload_start(&o);
11907de3ee57SMikulas Patocka 	r = ti->type->map(ti, clone);
1191d67a5f4bSMikulas Patocka 	dm_offload_end(&o);
1192d67a5f4bSMikulas Patocka 
1193846785e6SChristoph Hellwig 	switch (r) {
1194846785e6SChristoph Hellwig 	case DM_MAPIO_SUBMITTED:
1195846785e6SChristoph Hellwig 		break;
1196846785e6SChristoph Hellwig 	case DM_MAPIO_REMAPPED:
11971da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
119874d46992SChristoph Hellwig 		trace_block_bio_remap(clone->bi_disk->queue, clone,
119974d46992SChristoph Hellwig 				      bio_dev(tio->io->bio), sector);
12001da177e4SLinus Torvalds 		generic_make_request(clone);
1201846785e6SChristoph Hellwig 		break;
1202846785e6SChristoph Hellwig 	case DM_MAPIO_KILL:
12034e4cbee9SChristoph Hellwig 		dec_pending(tio->io, BLK_STS_IOERR);
12044e4cbee9SChristoph Hellwig 		free_tio(tio);
12054e4cbee9SChristoph Hellwig 		break;
1206846785e6SChristoph Hellwig 	case DM_MAPIO_REQUEUE:
12074e4cbee9SChristoph Hellwig 		dec_pending(tio->io, BLK_STS_DM_REQUEUE);
1208cfae7529SMike Snitzer 		free_tio(tio);
1209846785e6SChristoph Hellwig 		break;
1210846785e6SChristoph Hellwig 	default:
121145cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
121245cbcd79SKiyoshi Ueda 		BUG();
12131da177e4SLinus Torvalds 	}
12141da177e4SLinus Torvalds }
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds struct clone_info {
12171da177e4SLinus Torvalds 	struct mapped_device *md;
12181da177e4SLinus Torvalds 	struct dm_table *map;
12191da177e4SLinus Torvalds 	struct bio *bio;
12201da177e4SLinus Torvalds 	struct dm_io *io;
12211da177e4SLinus Torvalds 	sector_t sector;
1222e0d6609aSMikulas Patocka 	unsigned sector_count;
12231da177e4SLinus Torvalds };
12241da177e4SLinus Torvalds 
1225e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
1226bd2a49b8SAlasdair G Kergon {
12274f024f37SKent Overstreet 	bio->bi_iter.bi_sector = sector;
12284f024f37SKent Overstreet 	bio->bi_iter.bi_size = to_bytes(len);
12291da177e4SLinus Torvalds }
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds /*
12321da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
12331da177e4SLinus Torvalds  */
1234c80914e8SMike Snitzer static int clone_bio(struct dm_target_io *tio, struct bio *bio,
12351c3b13e6SKent Overstreet 		     sector_t sector, unsigned len)
12361da177e4SLinus Torvalds {
1237dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
12381da177e4SLinus Torvalds 
12391c3b13e6SKent Overstreet 	__bio_clone_fast(clone, bio);
12409c47008dSMartin K. Petersen 
1241e2460f2aSMikulas Patocka 	if (unlikely(bio_integrity(bio) != NULL)) {
1242e2460f2aSMikulas Patocka 		int r;
1243e2460f2aSMikulas Patocka 
1244e2460f2aSMikulas Patocka 		if (unlikely(!dm_target_has_integrity(tio->ti->type) &&
1245e2460f2aSMikulas Patocka 			     !dm_target_passes_integrity(tio->ti->type))) {
1246e2460f2aSMikulas Patocka 			DMWARN("%s: the target %s doesn't support integrity data.",
1247e2460f2aSMikulas Patocka 				dm_device_name(tio->io->md),
1248e2460f2aSMikulas Patocka 				tio->ti->type->name);
1249e2460f2aSMikulas Patocka 			return -EIO;
1250e2460f2aSMikulas Patocka 		}
1251e2460f2aSMikulas Patocka 
1252e2460f2aSMikulas Patocka 		r = bio_integrity_clone(clone, bio, GFP_NOIO);
1253c80914e8SMike Snitzer 		if (r < 0)
1254c80914e8SMike Snitzer 			return r;
1255c80914e8SMike Snitzer 	}
12561c3b13e6SKent Overstreet 
1257264c869dSDamien Le Moal 	if (bio_op(bio) != REQ_OP_ZONE_REPORT)
12581c3b13e6SKent Overstreet 		bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
12591c3b13e6SKent Overstreet 	clone->bi_iter.bi_size = to_bytes(len);
12601c3b13e6SKent Overstreet 
1261e2460f2aSMikulas Patocka 	if (unlikely(bio_integrity(bio) != NULL))
1262fbd08e76SDmitry Monakhov 		bio_integrity_trim(clone);
1263c80914e8SMike Snitzer 
1264c80914e8SMike Snitzer 	return 0;
12651da177e4SLinus Torvalds }
12661da177e4SLinus Torvalds 
1267*318716ddSMike Snitzer static struct dm_target_io *alloc_tio(struct clone_info *ci, struct dm_target *ti,
1268*318716ddSMike Snitzer 				      unsigned target_bio_nr, gfp_t gfp_mask)
1269f9ab94ceSMikulas Patocka {
1270dba14160SMikulas Patocka 	struct dm_target_io *tio;
1271dba14160SMikulas Patocka 	struct bio *clone;
1272dba14160SMikulas Patocka 
1273*318716ddSMike Snitzer 	clone = bio_alloc_bioset(gfp_mask, 0, ci->md->bs);
1274*318716ddSMike Snitzer 	if (!clone)
1275*318716ddSMike Snitzer 		return NULL;
1276f9ab94ceSMikulas Patocka 
1277*318716ddSMike Snitzer 	tio = container_of(clone, struct dm_target_io, clone);
1278f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1279f9ab94ceSMikulas Patocka 	tio->ti = ti;
128055a62eefSAlasdair G Kergon 	tio->target_bio_nr = target_bio_nr;
12819015df24SAlasdair G Kergon 
12829015df24SAlasdair G Kergon 	return tio;
12839015df24SAlasdair G Kergon }
12849015df24SAlasdair G Kergon 
1285*318716ddSMike Snitzer static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci,
1286*318716ddSMike Snitzer 				struct dm_target *ti, unsigned num_bios)
12879015df24SAlasdair G Kergon {
1288*318716ddSMike Snitzer 	struct dm_target_io *tio;
1289*318716ddSMike Snitzer 	int try;
1290*318716ddSMike Snitzer 
1291*318716ddSMike Snitzer 	if (!num_bios)
1292*318716ddSMike Snitzer 		return;
1293*318716ddSMike Snitzer 
1294*318716ddSMike Snitzer 	if (num_bios == 1) {
1295*318716ddSMike Snitzer 		tio = alloc_tio(ci, ti, 0, GFP_NOIO);
1296*318716ddSMike Snitzer 		bio_list_add(blist, &tio->clone);
1297*318716ddSMike Snitzer 		return;
1298*318716ddSMike Snitzer 	}
1299*318716ddSMike Snitzer 
1300*318716ddSMike Snitzer 	for (try = 0; try < 2; try++) {
1301*318716ddSMike Snitzer 		int bio_nr;
1302*318716ddSMike Snitzer 		struct bio *bio;
1303*318716ddSMike Snitzer 
1304*318716ddSMike Snitzer 		if (try)
1305*318716ddSMike Snitzer 			mutex_lock(&ci->md->table_devices_lock);
1306*318716ddSMike Snitzer 		for (bio_nr = 0; bio_nr < num_bios; bio_nr++) {
1307*318716ddSMike Snitzer 			tio = alloc_tio(ci, ti, bio_nr, try ? GFP_NOIO : GFP_NOWAIT);
1308*318716ddSMike Snitzer 			if (!tio)
1309*318716ddSMike Snitzer 				break;
1310*318716ddSMike Snitzer 
1311*318716ddSMike Snitzer 			bio_list_add(blist, &tio->clone);
1312*318716ddSMike Snitzer 		}
1313*318716ddSMike Snitzer 		if (try)
1314*318716ddSMike Snitzer 			mutex_unlock(&ci->md->table_devices_lock);
1315*318716ddSMike Snitzer 		if (bio_nr == num_bios)
1316*318716ddSMike Snitzer 			return;
1317*318716ddSMike Snitzer 
1318*318716ddSMike Snitzer 		while ((bio = bio_list_pop(blist))) {
1319*318716ddSMike Snitzer 			tio = container_of(bio, struct dm_target_io, clone);
1320*318716ddSMike Snitzer 			free_tio(tio);
1321*318716ddSMike Snitzer 		}
1322*318716ddSMike Snitzer 	}
1323*318716ddSMike Snitzer }
1324*318716ddSMike Snitzer 
1325*318716ddSMike Snitzer static void __clone_and_map_simple_bio(struct clone_info *ci,
1326*318716ddSMike Snitzer 				       struct dm_target_io *tio, unsigned *len)
1327*318716ddSMike Snitzer {
1328dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
13299015df24SAlasdair G Kergon 
13301dd40c3eSMikulas Patocka 	tio->len_ptr = len;
13311dd40c3eSMikulas Patocka 
13321c3b13e6SKent Overstreet 	__bio_clone_fast(clone, ci->bio);
1333bd2a49b8SAlasdair G Kergon 	if (len)
13341dd40c3eSMikulas Patocka 		bio_setup_sector(clone, ci->sector, *len);
1335f9ab94ceSMikulas Patocka 
1336bd2a49b8SAlasdair G Kergon 	__map_bio(tio);
1337f9ab94ceSMikulas Patocka }
1338f9ab94ceSMikulas Patocka 
133914fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
13401dd40c3eSMikulas Patocka 				  unsigned num_bios, unsigned *len)
134106a426ceSMike Snitzer {
1342*318716ddSMike Snitzer 	struct bio_list blist = BIO_EMPTY_LIST;
1343*318716ddSMike Snitzer 	struct bio *bio;
1344*318716ddSMike Snitzer 	struct dm_target_io *tio;
134506a426ceSMike Snitzer 
1346*318716ddSMike Snitzer 	alloc_multiple_bios(&blist, ci, ti, num_bios);
1347*318716ddSMike Snitzer 
1348*318716ddSMike Snitzer 	while ((bio = bio_list_pop(&blist))) {
1349*318716ddSMike Snitzer 		tio = container_of(bio, struct dm_target_io, clone);
1350*318716ddSMike Snitzer 		__clone_and_map_simple_bio(ci, tio, len);
1351*318716ddSMike Snitzer 	}
135206a426ceSMike Snitzer }
135306a426ceSMike Snitzer 
135414fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1355f9ab94ceSMikulas Patocka {
135606a426ceSMike Snitzer 	unsigned target_nr = 0;
1357f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1358f9ab94ceSMikulas Patocka 
1359b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1360f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
13611dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
1362f9ab94ceSMikulas Patocka 
1363f9ab94ceSMikulas Patocka 	return 0;
1364f9ab94ceSMikulas Patocka }
1365f9ab94ceSMikulas Patocka 
1366c80914e8SMike Snitzer static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
13671dd40c3eSMikulas Patocka 				    sector_t sector, unsigned *len)
13685ae89a87SMike Snitzer {
1369dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
13705ae89a87SMike Snitzer 	struct dm_target_io *tio;
1371f31c21e4SNeilBrown 	int r;
13725ae89a87SMike Snitzer 
1373*318716ddSMike Snitzer 	tio = alloc_tio(ci, ti, 0, GFP_NOIO);
13741dd40c3eSMikulas Patocka 	tio->len_ptr = len;
1375c80914e8SMike Snitzer 	r = clone_bio(tio, bio, sector, *len);
1376072623deSMikulas Patocka 	if (r < 0) {
1377cfae7529SMike Snitzer 		free_tio(tio);
1378f31c21e4SNeilBrown 		return r;
1379072623deSMikulas Patocka 	}
1380bd2a49b8SAlasdair G Kergon 	__map_bio(tio);
1381c80914e8SMike Snitzer 
1382f31c21e4SNeilBrown 	return 0;
1383b0d8ed4dSAlasdair G Kergon }
13845ae89a87SMike Snitzer 
138555a62eefSAlasdair G Kergon typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
138623508a96SMike Snitzer 
138755a62eefSAlasdair G Kergon static unsigned get_num_discard_bios(struct dm_target *ti)
138823508a96SMike Snitzer {
138955a62eefSAlasdair G Kergon 	return ti->num_discard_bios;
139023508a96SMike Snitzer }
139123508a96SMike Snitzer 
139255a62eefSAlasdair G Kergon static unsigned get_num_write_same_bios(struct dm_target *ti)
139323508a96SMike Snitzer {
139455a62eefSAlasdair G Kergon 	return ti->num_write_same_bios;
139523508a96SMike Snitzer }
139623508a96SMike Snitzer 
1397ac62d620SChristoph Hellwig static unsigned get_num_write_zeroes_bios(struct dm_target *ti)
1398ac62d620SChristoph Hellwig {
1399ac62d620SChristoph Hellwig 	return ti->num_write_zeroes_bios;
1400ac62d620SChristoph Hellwig }
1401ac62d620SChristoph Hellwig 
140223508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti);
140323508a96SMike Snitzer 
140423508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti)
140523508a96SMike Snitzer {
140655a62eefSAlasdair G Kergon 	return ti->split_discard_bios;
140723508a96SMike Snitzer }
140823508a96SMike Snitzer 
140914fe594dSAlasdair G Kergon static int __send_changing_extent_only(struct clone_info *ci,
141055a62eefSAlasdair G Kergon 				       get_num_bios_fn get_num_bios,
141123508a96SMike Snitzer 				       is_split_required_fn is_split_required)
14125ae89a87SMike Snitzer {
14135ae89a87SMike Snitzer 	struct dm_target *ti;
1414e0d6609aSMikulas Patocka 	unsigned len;
141555a62eefSAlasdair G Kergon 	unsigned num_bios;
14165ae89a87SMike Snitzer 
1417a79245b3SMike Snitzer 	do {
14185ae89a87SMike Snitzer 		ti = dm_table_find_target(ci->map, ci->sector);
14195ae89a87SMike Snitzer 		if (!dm_target_is_valid(ti))
14205ae89a87SMike Snitzer 			return -EIO;
14215ae89a87SMike Snitzer 
14225ae89a87SMike Snitzer 		/*
142323508a96SMike Snitzer 		 * Even though the device advertised support for this type of
142423508a96SMike Snitzer 		 * request, that does not mean every target supports it, and
1425936688d7SMike Snitzer 		 * reconfiguration might also have changed that since the
14265ae89a87SMike Snitzer 		 * check was performed.
14275ae89a87SMike Snitzer 		 */
142855a62eefSAlasdair G Kergon 		num_bios = get_num_bios ? get_num_bios(ti) : 0;
142955a62eefSAlasdair G Kergon 		if (!num_bios)
14305ae89a87SMike Snitzer 			return -EOPNOTSUPP;
14315ae89a87SMike Snitzer 
143223508a96SMike Snitzer 		if (is_split_required && !is_split_required(ti))
1433e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
14347acf0277SMikulas Patocka 		else
1435e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
14365ae89a87SMike Snitzer 
14371dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, num_bios, &len);
14385ae89a87SMike Snitzer 
1439a79245b3SMike Snitzer 		ci->sector += len;
1440a79245b3SMike Snitzer 	} while (ci->sector_count -= len);
14415ae89a87SMike Snitzer 
14425ae89a87SMike Snitzer 	return 0;
14435ae89a87SMike Snitzer }
14445ae89a87SMike Snitzer 
144514fe594dSAlasdair G Kergon static int __send_discard(struct clone_info *ci)
144623508a96SMike Snitzer {
144714fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_discard_bios,
144823508a96SMike Snitzer 					   is_split_required_for_discard);
144923508a96SMike Snitzer }
145023508a96SMike Snitzer 
145114fe594dSAlasdair G Kergon static int __send_write_same(struct clone_info *ci)
145223508a96SMike Snitzer {
145314fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
145423508a96SMike Snitzer }
145523508a96SMike Snitzer 
1456ac62d620SChristoph Hellwig static int __send_write_zeroes(struct clone_info *ci)
1457ac62d620SChristoph Hellwig {
1458ac62d620SChristoph Hellwig 	return __send_changing_extent_only(ci, get_num_write_zeroes_bios, NULL);
1459ac62d620SChristoph Hellwig }
1460ac62d620SChristoph Hellwig 
1461e4c93811SAlasdair G Kergon /*
1462e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1463e4c93811SAlasdair G Kergon  */
1464e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci)
1465e4c93811SAlasdair G Kergon {
1466e4c93811SAlasdair G Kergon 	struct bio *bio = ci->bio;
1467e4c93811SAlasdair G Kergon 	struct dm_target *ti;
14681c3b13e6SKent Overstreet 	unsigned len;
1469c80914e8SMike Snitzer 	int r;
1470e4c93811SAlasdair G Kergon 
1471e6047149SMike Christie 	if (unlikely(bio_op(bio) == REQ_OP_DISCARD))
1472e4c93811SAlasdair G Kergon 		return __send_discard(ci);
1473e6047149SMike Christie 	else if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
1474e4c93811SAlasdair G Kergon 		return __send_write_same(ci);
1475ac62d620SChristoph Hellwig 	else if (unlikely(bio_op(bio) == REQ_OP_WRITE_ZEROES))
1476ac62d620SChristoph Hellwig 		return __send_write_zeroes(ci);
1477e4c93811SAlasdair G Kergon 
1478e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1479e4c93811SAlasdair G Kergon 	if (!dm_target_is_valid(ti))
1480e4c93811SAlasdair G Kergon 		return -EIO;
1481e4c93811SAlasdair G Kergon 
1482264c869dSDamien Le Moal 	if (bio_op(bio) == REQ_OP_ZONE_REPORT)
1483264c869dSDamien Le Moal 		len = ci->sector_count;
1484264c869dSDamien Le Moal 	else
1485264c869dSDamien Le Moal 		len = min_t(sector_t, max_io_len(ci->sector, ti),
1486264c869dSDamien Le Moal 			    ci->sector_count);
1487e4c93811SAlasdair G Kergon 
1488c80914e8SMike Snitzer 	r = __clone_and_map_data_bio(ci, ti, ci->sector, &len);
1489c80914e8SMike Snitzer 	if (r < 0)
1490c80914e8SMike Snitzer 		return r;
1491e4c93811SAlasdair G Kergon 
1492e4c93811SAlasdair G Kergon 	ci->sector += len;
1493e4c93811SAlasdair G Kergon 	ci->sector_count -= len;
1494e4c93811SAlasdair G Kergon 
1495e4c93811SAlasdair G Kergon 	return 0;
1496e4c93811SAlasdair G Kergon }
1497e4c93811SAlasdair G Kergon 
1498e4c93811SAlasdair G Kergon /*
149914fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
15001da177e4SLinus Torvalds  */
150183d5e5b0SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md,
150283d5e5b0SMikulas Patocka 				    struct dm_table *map, struct bio *bio)
15031da177e4SLinus Torvalds {
15041da177e4SLinus Torvalds 	struct clone_info ci;
1505512875bdSJun'ichi Nomura 	int error = 0;
15061da177e4SLinus Torvalds 
150783d5e5b0SMikulas Patocka 	if (unlikely(!map)) {
1508f0b9a450SMikulas Patocka 		bio_io_error(bio);
1509f0b9a450SMikulas Patocka 		return;
1510f0b9a450SMikulas Patocka 	}
1511692d0eb9SMikulas Patocka 
151283d5e5b0SMikulas Patocka 	ci.map = map;
15131da177e4SLinus Torvalds 	ci.md = md;
15141da177e4SLinus Torvalds 	ci.io = alloc_io(md);
15154e4cbee9SChristoph Hellwig 	ci.io->status = 0;
15161da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
15171da177e4SLinus Torvalds 	ci.io->bio = bio;
15181da177e4SLinus Torvalds 	ci.io->md = md;
1519f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
15204f024f37SKent Overstreet 	ci.sector = bio->bi_iter.bi_sector;
15211da177e4SLinus Torvalds 
15223eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1523bd2a49b8SAlasdair G Kergon 
15241eff9d32SJens Axboe 	if (bio->bi_opf & REQ_PREFLUSH) {
1525b372d360SMike Snitzer 		ci.bio = &ci.md->flush_bio;
1526b372d360SMike Snitzer 		ci.sector_count = 0;
152714fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1528b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1529a4aa5e56SDamien Le Moal 	} else if (bio_op(bio) == REQ_OP_ZONE_RESET) {
1530a4aa5e56SDamien Le Moal 		ci.bio = bio;
1531a4aa5e56SDamien Le Moal 		ci.sector_count = 0;
1532a4aa5e56SDamien Le Moal 		error = __split_and_process_non_flush(&ci);
1533b372d360SMike Snitzer 	} else {
15346a8736d1STejun Heo 		ci.bio = bio;
1535f6fccb12SMilan Broz 		ci.sector_count = bio_sectors(bio);
153618a25da8SNeilBrown 		while (ci.sector_count && !error) {
153714fe594dSAlasdair G Kergon 			error = __split_and_process_non_flush(&ci);
153818a25da8SNeilBrown 			if (current->bio_list && ci.sector_count && !error) {
153918a25da8SNeilBrown 				/*
154018a25da8SNeilBrown 				 * Remainder must be passed to generic_make_request()
154118a25da8SNeilBrown 				 * so that it gets handled *after* bios already submitted
154218a25da8SNeilBrown 				 * have been completely processed.
154318a25da8SNeilBrown 				 * We take a clone of the original to store in
154418a25da8SNeilBrown 				 * ci.io->bio to be used by end_io_acct() and
154518a25da8SNeilBrown 				 * for dec_pending to use for completion handling.
154618a25da8SNeilBrown 				 * As this path is not used for REQ_OP_ZONE_REPORT,
154718a25da8SNeilBrown 				 * the usage of io->bio in dm_remap_zone_report()
154818a25da8SNeilBrown 				 * won't be affected by this reassignment.
154918a25da8SNeilBrown 				 */
155018a25da8SNeilBrown 				struct bio *b = bio_clone_bioset(bio, GFP_NOIO,
155118a25da8SNeilBrown 								 md->queue->bio_split);
155218a25da8SNeilBrown 				ci.io->bio = b;
155318a25da8SNeilBrown 				bio_advance(bio, (bio_sectors(bio) - ci.sector_count) << 9);
155418a25da8SNeilBrown 				bio_chain(b, bio);
155518a25da8SNeilBrown 				generic_make_request(bio);
155618a25da8SNeilBrown 				break;
155718a25da8SNeilBrown 			}
155818a25da8SNeilBrown 		}
1559d87f4c14STejun Heo 	}
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 	/* drop the extra reference count */
156254385bf7SBart Van Assche 	dec_pending(ci.io, errno_to_blk_status(error));
15639e4e5f87SMilan Broz }
15649e4e5f87SMilan Broz /*-----------------------------------------------------------------
15651da177e4SLinus Torvalds  * CRUD END
15661da177e4SLinus Torvalds  *---------------------------------------------------------------*/
15671da177e4SLinus Torvalds 
15681da177e4SLinus Torvalds /*
156918a25da8SNeilBrown  * The request function that remaps the bio to one target and
157018a25da8SNeilBrown  * splits off any remainder.
15711da177e4SLinus Torvalds  */
1572dece1635SJens Axboe static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
15731da177e4SLinus Torvalds {
157412f03a49SKevin Corry 	int rw = bio_data_dir(bio);
15751da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
157683d5e5b0SMikulas Patocka 	int srcu_idx;
157783d5e5b0SMikulas Patocka 	struct dm_table *map;
15781da177e4SLinus Torvalds 
157983d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
15801da177e4SLinus Torvalds 
1581d62e26b3SJens Axboe 	generic_start_io_acct(q, rw, bio_sectors(bio), &dm_disk(md)->part0);
158212f03a49SKevin Corry 
15836a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
15846a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
158583d5e5b0SMikulas Patocka 		dm_put_live_table(md, srcu_idx);
15861da177e4SLinus Torvalds 
15871eff9d32SJens Axboe 		if (!(bio->bi_opf & REQ_RAHEAD))
158892c63902SMikulas Patocka 			queue_io(md, bio);
15896a8736d1STejun Heo 		else
15906a8736d1STejun Heo 			bio_io_error(bio);
1591dece1635SJens Axboe 		return BLK_QC_T_NONE;
15921da177e4SLinus Torvalds 	}
15931da177e4SLinus Torvalds 
159483d5e5b0SMikulas Patocka 	__split_and_process_bio(md, map, bio);
159583d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
1596dece1635SJens Axboe 	return BLK_QC_T_NONE;
1597cec47e3dSKiyoshi Ueda }
1598cec47e3dSKiyoshi Ueda 
15991da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
16001da177e4SLinus Torvalds {
16018a57dfc6SChandra Seetharaman 	int r = bdi_bits;
16028a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
16038a57dfc6SChandra Seetharaman 	struct dm_table *map;
16041da177e4SLinus Torvalds 
16051eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
1606e522c039SMike Snitzer 		if (dm_request_based(md)) {
1607cec47e3dSKiyoshi Ueda 			/*
1608e522c039SMike Snitzer 			 * With request-based DM we only need to check the
1609e522c039SMike Snitzer 			 * top-level queue for congestion.
1610cec47e3dSKiyoshi Ueda 			 */
1611dc3b17ccSJan Kara 			r = md->queue->backing_dev_info->wb.state & bdi_bits;
1612e522c039SMike Snitzer 		} else {
1613e522c039SMike Snitzer 			map = dm_get_live_table_fast(md);
1614e522c039SMike Snitzer 			if (map)
16151da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
161683d5e5b0SMikulas Patocka 			dm_put_live_table_fast(md);
16178a57dfc6SChandra Seetharaman 		}
1618e522c039SMike Snitzer 	}
16198a57dfc6SChandra Seetharaman 
16201da177e4SLinus Torvalds 	return r;
16211da177e4SLinus Torvalds }
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds /*-----------------------------------------------------------------
16241da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
16251da177e4SLinus Torvalds  *---------------------------------------------------------------*/
16262b06cfffSAlasdair G Kergon static void free_minor(int minor)
16271da177e4SLinus Torvalds {
1628f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
16291da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1630f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
16311da177e4SLinus Torvalds }
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds /*
16341da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
16351da177e4SLinus Torvalds  */
1636cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
16371da177e4SLinus Torvalds {
1638c9d76be6STejun Heo 	int r;
16391da177e4SLinus Torvalds 
16401da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
16411da177e4SLinus Torvalds 		return -EINVAL;
16421da177e4SLinus Torvalds 
1643c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1644f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
16451da177e4SLinus Torvalds 
1646c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
16471da177e4SLinus Torvalds 
1648f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1649c9d76be6STejun Heo 	idr_preload_end();
1650c9d76be6STejun Heo 	if (r < 0)
1651c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
1652c9d76be6STejun Heo 	return 0;
16531da177e4SLinus Torvalds }
16541da177e4SLinus Torvalds 
1655cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
16561da177e4SLinus Torvalds {
1657c9d76be6STejun Heo 	int r;
16581da177e4SLinus Torvalds 
1659c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1660f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
16611da177e4SLinus Torvalds 
1662c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
16631da177e4SLinus Torvalds 
1664f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1665c9d76be6STejun Heo 	idr_preload_end();
1666c9d76be6STejun Heo 	if (r < 0)
16671da177e4SLinus Torvalds 		return r;
1668c9d76be6STejun Heo 	*minor = r;
1669c9d76be6STejun Heo 	return 0;
16701da177e4SLinus Torvalds }
16711da177e4SLinus Torvalds 
167283d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
1673f26c5719SDan Williams static const struct dax_operations dm_dax_ops;
16741da177e4SLinus Torvalds 
167553d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
167653d5914fSMikulas Patocka 
16774cc96131SMike Snitzer void dm_init_md_queue(struct mapped_device *md)
16784a0b4ddfSMike Snitzer {
16794a0b4ddfSMike Snitzer 	/*
1680ad5f498fSMikulas Patocka 	 * Initialize data that will only be used by a non-blk-mq DM queue
1681ad5f498fSMikulas Patocka 	 * - must do so here (in alloc_dev callchain) before queue is used
1682ad5f498fSMikulas Patocka 	 */
1683ad5f498fSMikulas Patocka 	md->queue->queuedata = md;
1684dc3b17ccSJan Kara 	md->queue->backing_dev_info->congested_data = md;
1685bfebd1cdSMike Snitzer }
16864a0b4ddfSMike Snitzer 
16874cc96131SMike Snitzer void dm_init_normal_md_queue(struct mapped_device *md)
1688bfebd1cdSMike Snitzer {
168917e149b8SMike Snitzer 	md->use_blk_mq = false;
1690bfebd1cdSMike Snitzer 	dm_init_md_queue(md);
1691bfebd1cdSMike Snitzer 
1692bfebd1cdSMike Snitzer 	/*
1693bfebd1cdSMike Snitzer 	 * Initialize aspects of queue that aren't relevant for blk-mq
1694bfebd1cdSMike Snitzer 	 */
1695dc3b17ccSJan Kara 	md->queue->backing_dev_info->congested_fn = dm_any_congested;
16964a0b4ddfSMike Snitzer }
16974a0b4ddfSMike Snitzer 
16980f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md)
16990f20972fSMike Snitzer {
17000f20972fSMike Snitzer 	if (md->wq)
17010f20972fSMike Snitzer 		destroy_workqueue(md->wq);
17020f20972fSMike Snitzer 	if (md->kworker_task)
17030f20972fSMike Snitzer 		kthread_stop(md->kworker_task);
17040f20972fSMike Snitzer 	mempool_destroy(md->io_pool);
17050f20972fSMike Snitzer 	if (md->bs)
17060f20972fSMike Snitzer 		bioset_free(md->bs);
17070f20972fSMike Snitzer 
1708f26c5719SDan Williams 	if (md->dax_dev) {
1709f26c5719SDan Williams 		kill_dax(md->dax_dev);
1710f26c5719SDan Williams 		put_dax(md->dax_dev);
1711f26c5719SDan Williams 		md->dax_dev = NULL;
1712f26c5719SDan Williams 	}
1713f26c5719SDan Williams 
17140f20972fSMike Snitzer 	if (md->disk) {
17150f20972fSMike Snitzer 		spin_lock(&_minor_lock);
17160f20972fSMike Snitzer 		md->disk->private_data = NULL;
17170f20972fSMike Snitzer 		spin_unlock(&_minor_lock);
17180f20972fSMike Snitzer 		del_gendisk(md->disk);
17190f20972fSMike Snitzer 		put_disk(md->disk);
17200f20972fSMike Snitzer 	}
17210f20972fSMike Snitzer 
17220f20972fSMike Snitzer 	if (md->queue)
17230f20972fSMike Snitzer 		blk_cleanup_queue(md->queue);
17240f20972fSMike Snitzer 
1725d09960b0STahsin Erdogan 	cleanup_srcu_struct(&md->io_barrier);
1726d09960b0STahsin Erdogan 
17270f20972fSMike Snitzer 	if (md->bdev) {
17280f20972fSMike Snitzer 		bdput(md->bdev);
17290f20972fSMike Snitzer 		md->bdev = NULL;
17300f20972fSMike Snitzer 	}
17314cc96131SMike Snitzer 
17324cc96131SMike Snitzer 	dm_mq_cleanup_mapped_device(md);
17330f20972fSMike Snitzer }
17340f20972fSMike Snitzer 
17351da177e4SLinus Torvalds /*
17361da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
17371da177e4SLinus Torvalds  */
17382b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
17391da177e4SLinus Torvalds {
1740115485e8SMike Snitzer 	int r, numa_node_id = dm_get_numa_node();
1741f26c5719SDan Williams 	struct dax_device *dax_dev;
1742115485e8SMike Snitzer 	struct mapped_device *md;
1743ba61fdd1SJeff Mahoney 	void *old_md;
17441da177e4SLinus Torvalds 
1745856eb091SMikulas Patocka 	md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
17461da177e4SLinus Torvalds 	if (!md) {
17471da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
17481da177e4SLinus Torvalds 		return NULL;
17491da177e4SLinus Torvalds 	}
17501da177e4SLinus Torvalds 
175110da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
17526ed7ade8SMilan Broz 		goto bad_module_get;
175310da4f79SJeff Mahoney 
17541da177e4SLinus Torvalds 	/* get a minor number for the dev */
17552b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1756cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
17572b06cfffSAlasdair G Kergon 	else
1758cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
17591da177e4SLinus Torvalds 	if (r < 0)
17606ed7ade8SMilan Broz 		goto bad_minor;
17611da177e4SLinus Torvalds 
176283d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
176383d5e5b0SMikulas Patocka 	if (r < 0)
176483d5e5b0SMikulas Patocka 		goto bad_io_barrier;
176583d5e5b0SMikulas Patocka 
1766115485e8SMike Snitzer 	md->numa_node_id = numa_node_id;
17674cc96131SMike Snitzer 	md->use_blk_mq = dm_use_blk_mq_default();
1768591ddcfcSMike Snitzer 	md->init_tio_pdu = false;
1769a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
1770e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1771a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
177286f1152bSBenjamin Marzinski 	mutex_init(&md->table_devices_lock);
1773022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
17741da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
17755c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
17761da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
17777a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
17787a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
177986f1152bSBenjamin Marzinski 	INIT_LIST_HEAD(&md->table_devices);
17807a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
17811da177e4SLinus Torvalds 
1782115485e8SMike Snitzer 	md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id);
17831da177e4SLinus Torvalds 	if (!md->queue)
17840f20972fSMike Snitzer 		goto bad;
17851da177e4SLinus Torvalds 
17864a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
17879faf400fSStefan Bader 
1788115485e8SMike Snitzer 	md->disk = alloc_disk_node(1, numa_node_id);
17891da177e4SLinus Torvalds 	if (!md->disk)
17900f20972fSMike Snitzer 		goto bad;
17911da177e4SLinus Torvalds 
1792316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
1793316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
1794f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
179553d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1796f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
17972995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
17982eb6e1e3SKeith Busch 	md->kworker_task = NULL;
1799f0b04115SJeff Mahoney 
18001da177e4SLinus Torvalds 	md->disk->major = _major;
18011da177e4SLinus Torvalds 	md->disk->first_minor = minor;
18021da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
18031da177e4SLinus Torvalds 	md->disk->queue = md->queue;
18041da177e4SLinus Torvalds 	md->disk->private_data = md;
18051da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
1806f26c5719SDan Williams 
1807f26c5719SDan Williams 	dax_dev = alloc_dax(md, md->disk->disk_name, &dm_dax_ops);
1808f26c5719SDan Williams 	if (!dax_dev)
1809f26c5719SDan Williams 		goto bad;
1810f26c5719SDan Williams 	md->dax_dev = dax_dev;
1811f26c5719SDan Williams 
18121da177e4SLinus Torvalds 	add_disk(md->disk);
18137e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
18141da177e4SLinus Torvalds 
1815670368a8STejun Heo 	md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
1816304f3f6aSMilan Broz 	if (!md->wq)
18170f20972fSMike Snitzer 		goto bad;
1818304f3f6aSMilan Broz 
181932a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
182032a926daSMikulas Patocka 	if (!md->bdev)
18210f20972fSMike Snitzer 		goto bad;
182232a926daSMikulas Patocka 
18233a83f467SMing Lei 	bio_init(&md->flush_bio, NULL, 0);
182474d46992SChristoph Hellwig 	bio_set_dev(&md->flush_bio, md->bdev);
1825ff0361b3SJan Kara 	md->flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
18266a8736d1STejun Heo 
1827fd2ed4d2SMikulas Patocka 	dm_stats_init(&md->stats);
1828fd2ed4d2SMikulas Patocka 
1829ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1830f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1831ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1832f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1833ba61fdd1SJeff Mahoney 
1834ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1835ba61fdd1SJeff Mahoney 
18361da177e4SLinus Torvalds 	return md;
18371da177e4SLinus Torvalds 
18380f20972fSMike Snitzer bad:
18390f20972fSMike Snitzer 	cleanup_mapped_device(md);
184083d5e5b0SMikulas Patocka bad_io_barrier:
18411da177e4SLinus Torvalds 	free_minor(minor);
18426ed7ade8SMilan Broz bad_minor:
184310da4f79SJeff Mahoney 	module_put(THIS_MODULE);
18446ed7ade8SMilan Broz bad_module_get:
1845856eb091SMikulas Patocka 	kvfree(md);
18461da177e4SLinus Torvalds 	return NULL;
18471da177e4SLinus Torvalds }
18481da177e4SLinus Torvalds 
1849ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1850ae9da83fSJun'ichi Nomura 
18511da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
18521da177e4SLinus Torvalds {
1853f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
185463d94e48SJun'ichi Nomura 
1855ae9da83fSJun'ichi Nomura 	unlock_fs(md);
18562eb6e1e3SKeith Busch 
18570f20972fSMike Snitzer 	cleanup_mapped_device(md);
18580f20972fSMike Snitzer 
18590f20972fSMike Snitzer 	free_table_devices(&md->table_devices);
18600f20972fSMike Snitzer 	dm_stats_cleanup(&md->stats);
186163a4f065SMike Snitzer 	free_minor(minor);
186263a4f065SMike Snitzer 
186310da4f79SJeff Mahoney 	module_put(THIS_MODULE);
1864856eb091SMikulas Patocka 	kvfree(md);
18651da177e4SLinus Torvalds }
18661da177e4SLinus Torvalds 
1867e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1868e6ee8c0bSKiyoshi Ueda {
1869c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
1870e6ee8c0bSKiyoshi Ueda 
18714e6e36c3SMike Snitzer 	if (md->bs) {
18724e6e36c3SMike Snitzer 		/* The md already has necessary mempools. */
1873545ed20eSToshi Kani 		if (dm_table_bio_based(t)) {
1874c0820cf5SMikulas Patocka 			/*
187516245bdcSJun'ichi Nomura 			 * Reload bioset because front_pad may have changed
187616245bdcSJun'ichi Nomura 			 * because a different table was loaded.
1877c0820cf5SMikulas Patocka 			 */
1878c0820cf5SMikulas Patocka 			bioset_free(md->bs);
1879c0820cf5SMikulas Patocka 			md->bs = p->bs;
1880c0820cf5SMikulas Patocka 			p->bs = NULL;
1881c0820cf5SMikulas Patocka 		}
1882cbc4e3c1SMike Snitzer 		/*
18834e6e36c3SMike Snitzer 		 * There's no need to reload with request-based dm
18844e6e36c3SMike Snitzer 		 * because the size of front_pad doesn't change.
18854e6e36c3SMike Snitzer 		 * Note for future: If you are to reload bioset,
18864e6e36c3SMike Snitzer 		 * prep-ed requests in the queue may refer
18874e6e36c3SMike Snitzer 		 * to bio from the old bioset, so you must walk
18884e6e36c3SMike Snitzer 		 * through the queue to unprep.
1889cbc4e3c1SMike Snitzer 		 */
1890cbc4e3c1SMike Snitzer 		goto out;
1891cbc4e3c1SMike Snitzer 	}
1892cbc4e3c1SMike Snitzer 
1893eb8db831SChristoph Hellwig 	BUG_ON(!p || md->io_pool || md->bs);
1894e6ee8c0bSKiyoshi Ueda 
1895e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
1896e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
1897e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
1898e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
18994e6e36c3SMike Snitzer 
1900e6ee8c0bSKiyoshi Ueda out:
190102233342SMike Snitzer 	/* mempool bind completed, no longer need any mempools in the table */
1902e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
1903e6ee8c0bSKiyoshi Ueda }
1904e6ee8c0bSKiyoshi Ueda 
19051da177e4SLinus Torvalds /*
19061da177e4SLinus Torvalds  * Bind a table to the device.
19071da177e4SLinus Torvalds  */
19081da177e4SLinus Torvalds static void event_callback(void *context)
19091da177e4SLinus Torvalds {
19107a8c3d3bSMike Anderson 	unsigned long flags;
19117a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
19121da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
19131da177e4SLinus Torvalds 
19147a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
19157a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
19167a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
19177a8c3d3bSMike Anderson 
1918ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
19197a8c3d3bSMike Anderson 
19201da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
19211da177e4SLinus Torvalds 	wake_up(&md->eventq);
192262e08243SMikulas Patocka 	dm_issue_global_event();
19231da177e4SLinus Torvalds }
19241da177e4SLinus Torvalds 
1925c217649bSMike Snitzer /*
1926c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
1927c217649bSMike Snitzer  */
19284e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
19291da177e4SLinus Torvalds {
19301ea0654eSBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
19311ea0654eSBart Van Assche 
19324e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
19331da177e4SLinus Torvalds 
1934db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
19351da177e4SLinus Torvalds }
19361da177e4SLinus Torvalds 
1937042d2a9bSAlasdair G Kergon /*
1938042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
1939042d2a9bSAlasdair G Kergon  */
1940042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
1941754c5fc7SMike Snitzer 			       struct queue_limits *limits)
19421da177e4SLinus Torvalds {
1943042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
1944165125e1SJens Axboe 	struct request_queue *q = md->queue;
19451da177e4SLinus Torvalds 	sector_t size;
19461da177e4SLinus Torvalds 
19475a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
19485a8f1f80SBart Van Assche 
19491da177e4SLinus Torvalds 	size = dm_table_get_size(t);
19503ac51e74SDarrick J. Wong 
19513ac51e74SDarrick J. Wong 	/*
19523ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
19533ac51e74SDarrick J. Wong 	 */
1954fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
19553ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
19563ac51e74SDarrick J. Wong 
19574e90188bSAlasdair G Kergon 	__set_size(md, size);
19581da177e4SLinus Torvalds 
1959cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
19602ca3310eSAlasdair G Kergon 
1961e6ee8c0bSKiyoshi Ueda 	/*
1962e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
1963e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
1964e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
1965e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
1966e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
1967e6ee8c0bSKiyoshi Ueda 	 */
196816f12266SMike Snitzer 	if (dm_table_request_based(t)) {
1969eca7ee6dSMike Snitzer 		dm_stop_queue(q);
197016f12266SMike Snitzer 		/*
197116f12266SMike Snitzer 		 * Leverage the fact that request-based DM targets are
197216f12266SMike Snitzer 		 * immutable singletons and establish md->immutable_target
197316f12266SMike Snitzer 		 * - used to optimize both dm_request_fn and dm_mq_queue_rq
197416f12266SMike Snitzer 		 */
197516f12266SMike Snitzer 		md->immutable_target = dm_table_get_immutable_target(t);
197616f12266SMike Snitzer 	}
1977e6ee8c0bSKiyoshi Ueda 
1978e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
1979e6ee8c0bSKiyoshi Ueda 
1980a12f5d48SEric Dumazet 	old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
19811d3aa6f6SMike Snitzer 	rcu_assign_pointer(md->map, (void *)t);
198236a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
198336a0456fSAlasdair G Kergon 
1984754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
198541abc4e1SHannes Reinecke 	if (old_map)
198683d5e5b0SMikulas Patocka 		dm_sync_table(md);
19872ca3310eSAlasdair G Kergon 
1988042d2a9bSAlasdair G Kergon 	return old_map;
19891da177e4SLinus Torvalds }
19901da177e4SLinus Torvalds 
1991a7940155SAlasdair G Kergon /*
1992a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
1993a7940155SAlasdair G Kergon  */
1994a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
19951da177e4SLinus Torvalds {
1996a12f5d48SEric Dumazet 	struct dm_table *map = rcu_dereference_protected(md->map, 1);
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds 	if (!map)
1999a7940155SAlasdair G Kergon 		return NULL;
20001da177e4SLinus Torvalds 
20011da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
20029cdb8520SMonam Agarwal 	RCU_INIT_POINTER(md->map, NULL);
200383d5e5b0SMikulas Patocka 	dm_sync_table(md);
2004a7940155SAlasdair G Kergon 
2005a7940155SAlasdair G Kergon 	return map;
20061da177e4SLinus Torvalds }
20071da177e4SLinus Torvalds 
20081da177e4SLinus Torvalds /*
20091da177e4SLinus Torvalds  * Constructor for a new device.
20101da177e4SLinus Torvalds  */
20112b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
20121da177e4SLinus Torvalds {
20131da177e4SLinus Torvalds 	struct mapped_device *md;
20141da177e4SLinus Torvalds 
20152b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
20161da177e4SLinus Torvalds 	if (!md)
20171da177e4SLinus Torvalds 		return -ENXIO;
20181da177e4SLinus Torvalds 
2019784aae73SMilan Broz 	dm_sysfs_init(md);
2020784aae73SMilan Broz 
20211da177e4SLinus Torvalds 	*result = md;
20221da177e4SLinus Torvalds 	return 0;
20231da177e4SLinus Torvalds }
20241da177e4SLinus Torvalds 
2025a5664dadSMike Snitzer /*
2026a5664dadSMike Snitzer  * Functions to manage md->type.
2027a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2028a5664dadSMike Snitzer  */
2029a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2030a5664dadSMike Snitzer {
2031a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2032a5664dadSMike Snitzer }
2033a5664dadSMike Snitzer 
2034a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2035a5664dadSMike Snitzer {
2036a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2037a5664dadSMike Snitzer }
2038a5664dadSMike Snitzer 
20397e0d574fSBart Van Assche void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
2040a5664dadSMike Snitzer {
204100c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2042a5664dadSMike Snitzer 	md->type = type;
2043a5664dadSMike Snitzer }
2044a5664dadSMike Snitzer 
20457e0d574fSBart Van Assche enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
2046a5664dadSMike Snitzer {
2047a5664dadSMike Snitzer 	return md->type;
2048a5664dadSMike Snitzer }
2049a5664dadSMike Snitzer 
205036a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
205136a0456fSAlasdair G Kergon {
205236a0456fSAlasdair G Kergon 	return md->immutable_target_type;
205336a0456fSAlasdair G Kergon }
205436a0456fSAlasdair G Kergon 
20554a0b4ddfSMike Snitzer /*
2056f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
2057f84cb8a4SMike Snitzer  * count on 'md'.
2058f84cb8a4SMike Snitzer  */
2059f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2060f84cb8a4SMike Snitzer {
2061f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
2062f84cb8a4SMike Snitzer 	return &md->queue->limits;
2063f84cb8a4SMike Snitzer }
2064f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2065f84cb8a4SMike Snitzer 
20664a0b4ddfSMike Snitzer /*
20674a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
20684a0b4ddfSMike Snitzer  */
2069591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
20704a0b4ddfSMike Snitzer {
2071bfebd1cdSMike Snitzer 	int r;
20727e0d574fSBart Van Assche 	enum dm_queue_mode type = dm_get_md_type(md);
2073bfebd1cdSMike Snitzer 
2074545ed20eSToshi Kani 	switch (type) {
2075bfebd1cdSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
2076eb8db831SChristoph Hellwig 		r = dm_old_init_request_queue(md, t);
2077bfebd1cdSMike Snitzer 		if (r) {
2078eca7ee6dSMike Snitzer 			DMERR("Cannot initialize queue for request-based mapped device");
2079bfebd1cdSMike Snitzer 			return r;
20804a0b4ddfSMike Snitzer 		}
2081bfebd1cdSMike Snitzer 		break;
2082bfebd1cdSMike Snitzer 	case DM_TYPE_MQ_REQUEST_BASED:
2083e83068a5SMike Snitzer 		r = dm_mq_init_request_queue(md, t);
2084bfebd1cdSMike Snitzer 		if (r) {
2085eca7ee6dSMike Snitzer 			DMERR("Cannot initialize queue for request-based dm-mq mapped device");
2086bfebd1cdSMike Snitzer 			return r;
2087bfebd1cdSMike Snitzer 		}
2088bfebd1cdSMike Snitzer 		break;
2089bfebd1cdSMike Snitzer 	case DM_TYPE_BIO_BASED:
2090545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
2091eca7ee6dSMike Snitzer 		dm_init_normal_md_queue(md);
2092ff36ab34SMike Snitzer 		blk_queue_make_request(md->queue, dm_make_request);
2093545ed20eSToshi Kani 
2094545ed20eSToshi Kani 		if (type == DM_TYPE_DAX_BIO_BASED)
2095545ed20eSToshi Kani 			queue_flag_set_unlocked(QUEUE_FLAG_DAX, md->queue);
2096bfebd1cdSMike Snitzer 		break;
20977e0d574fSBart Van Assche 	case DM_TYPE_NONE:
20987e0d574fSBart Van Assche 		WARN_ON_ONCE(true);
20997e0d574fSBart Van Assche 		break;
2100ff36ab34SMike Snitzer 	}
21014a0b4ddfSMike Snitzer 
21024a0b4ddfSMike Snitzer 	return 0;
21034a0b4ddfSMike Snitzer }
21044a0b4ddfSMike Snitzer 
21052bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev)
21061da177e4SLinus Torvalds {
21071da177e4SLinus Torvalds 	struct mapped_device *md;
21081da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
21091da177e4SLinus Torvalds 
21101da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
21111da177e4SLinus Torvalds 		return NULL;
21121da177e4SLinus Torvalds 
2113f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
21141da177e4SLinus Torvalds 
21151da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
211649de5769SMike Snitzer 	if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) ||
211749de5769SMike Snitzer 	    test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2118637842cfSDavid Teigland 		md = NULL;
2119fba9f90eSJeff Mahoney 		goto out;
2120fba9f90eSJeff Mahoney 	}
21212bec1f4aSMikulas Patocka 	dm_get(md);
2122fba9f90eSJeff Mahoney out:
2123f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
21241da177e4SLinus Torvalds 
2125637842cfSDavid Teigland 	return md;
2126637842cfSDavid Teigland }
21273cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2128d229a958SDavid Teigland 
21299ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2130637842cfSDavid Teigland {
21319ade92a9SAlasdair G Kergon 	return md->interface_ptr;
21321da177e4SLinus Torvalds }
21331da177e4SLinus Torvalds 
21341da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
21351da177e4SLinus Torvalds {
21361da177e4SLinus Torvalds 	md->interface_ptr = ptr;
21371da177e4SLinus Torvalds }
21381da177e4SLinus Torvalds 
21391da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
21401da177e4SLinus Torvalds {
21411da177e4SLinus Torvalds 	atomic_inc(&md->holders);
21423f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
21431da177e4SLinus Torvalds }
21441da177e4SLinus Torvalds 
214509ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md)
214609ee96b2SMikulas Patocka {
214709ee96b2SMikulas Patocka 	spin_lock(&_minor_lock);
214809ee96b2SMikulas Patocka 	if (test_bit(DMF_FREEING, &md->flags)) {
214909ee96b2SMikulas Patocka 		spin_unlock(&_minor_lock);
215009ee96b2SMikulas Patocka 		return -EBUSY;
215109ee96b2SMikulas Patocka 	}
215209ee96b2SMikulas Patocka 	dm_get(md);
215309ee96b2SMikulas Patocka 	spin_unlock(&_minor_lock);
215409ee96b2SMikulas Patocka 	return 0;
215509ee96b2SMikulas Patocka }
215609ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold);
215709ee96b2SMikulas Patocka 
215872d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
215972d94861SAlasdair G Kergon {
216072d94861SAlasdair G Kergon 	return md->name;
216172d94861SAlasdair G Kergon }
216272d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
216372d94861SAlasdair G Kergon 
21643f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
21651da177e4SLinus Torvalds {
21663b785fbcSBart Van Assche 	struct request_queue *q = dm_get_md_queue(md);
21671134e5aeSMike Anderson 	struct dm_table *map;
216883d5e5b0SMikulas Patocka 	int srcu_idx;
21691da177e4SLinus Torvalds 
21703f77316dSKiyoshi Ueda 	might_sleep();
2171fba9f90eSJeff Mahoney 
217263a4f065SMike Snitzer 	spin_lock(&_minor_lock);
21733f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2174fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2175f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
21763f77316dSKiyoshi Ueda 
21772e91c369SBart Van Assche 	blk_set_queue_dying(q);
21783b785fbcSBart Van Assche 
217902233342SMike Snitzer 	if (dm_request_based(md) && md->kworker_task)
21803989144fSPetr Mladek 		kthread_flush_worker(&md->kworker);
21812eb6e1e3SKeith Busch 
2182ab7c7bb6SMikulas Patocka 	/*
2183ab7c7bb6SMikulas Patocka 	 * Take suspend_lock so that presuspend and postsuspend methods
2184ab7c7bb6SMikulas Patocka 	 * do not race with internal suspend.
2185ab7c7bb6SMikulas Patocka 	 */
2186ab7c7bb6SMikulas Patocka 	mutex_lock(&md->suspend_lock);
21872a708cffSJunichi Nomura 	map = dm_get_live_table(md, &srcu_idx);
21884f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
21891da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
21901da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
21911da177e4SLinus Torvalds 	}
219283d5e5b0SMikulas Patocka 	/* dm_put_live_table must be before msleep, otherwise deadlock is possible */
219383d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
21942a708cffSJunichi Nomura 	mutex_unlock(&md->suspend_lock);
219583d5e5b0SMikulas Patocka 
21963f77316dSKiyoshi Ueda 	/*
21973f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
21983f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
21993f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
22003f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
22013f77316dSKiyoshi Ueda 	 */
22023f77316dSKiyoshi Ueda 	if (wait)
22033f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
22043f77316dSKiyoshi Ueda 			msleep(1);
22053f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
22063f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
22073f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
22083f77316dSKiyoshi Ueda 
2209784aae73SMilan Broz 	dm_sysfs_exit(md);
2210a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
22111da177e4SLinus Torvalds 	free_dev(md);
22121da177e4SLinus Torvalds }
22133f77316dSKiyoshi Ueda 
22143f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
22153f77316dSKiyoshi Ueda {
22163f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
22173f77316dSKiyoshi Ueda }
22183f77316dSKiyoshi Ueda 
22193f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
22203f77316dSKiyoshi Ueda {
22213f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
22223f77316dSKiyoshi Ueda }
22233f77316dSKiyoshi Ueda 
22243f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
22253f77316dSKiyoshi Ueda {
22263f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
22271da177e4SLinus Torvalds }
222879eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
22291da177e4SLinus Torvalds 
2230b48633f8SBart Van Assche static int dm_wait_for_completion(struct mapped_device *md, long task_state)
223146125c1cSMilan Broz {
223246125c1cSMilan Broz 	int r = 0;
22339f4c3f87SBart Van Assche 	DEFINE_WAIT(wait);
223446125c1cSMilan Broz 
223546125c1cSMilan Broz 	while (1) {
22369f4c3f87SBart Van Assche 		prepare_to_wait(&md->wait, &wait, task_state);
223746125c1cSMilan Broz 
2238b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
223946125c1cSMilan Broz 			break;
224046125c1cSMilan Broz 
2241e3fabdfdSBart Van Assche 		if (signal_pending_state(task_state, current)) {
224246125c1cSMilan Broz 			r = -EINTR;
224346125c1cSMilan Broz 			break;
224446125c1cSMilan Broz 		}
224546125c1cSMilan Broz 
224646125c1cSMilan Broz 		io_schedule();
224746125c1cSMilan Broz 	}
22489f4c3f87SBart Van Assche 	finish_wait(&md->wait, &wait);
2249b44ebeb0SMikulas Patocka 
225046125c1cSMilan Broz 	return r;
225146125c1cSMilan Broz }
225246125c1cSMilan Broz 
22531da177e4SLinus Torvalds /*
22541da177e4SLinus Torvalds  * Process the deferred bios
22551da177e4SLinus Torvalds  */
2256ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
22571da177e4SLinus Torvalds {
2258ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2259ef208587SMikulas Patocka 						work);
22606d6f10dfSMilan Broz 	struct bio *c;
226183d5e5b0SMikulas Patocka 	int srcu_idx;
226283d5e5b0SMikulas Patocka 	struct dm_table *map;
22631da177e4SLinus Torvalds 
226483d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
2265ef208587SMikulas Patocka 
22663b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2267022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2268022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2269022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2270022c2611SMikulas Patocka 
22716a8736d1STejun Heo 		if (!c)
2272df12ee99SAlasdair G Kergon 			break;
227373d410c0SMilan Broz 
2274e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2275e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2276af7e466aSMikulas Patocka 		else
227783d5e5b0SMikulas Patocka 			__split_and_process_bio(md, map, c);
2278e6ee8c0bSKiyoshi Ueda 	}
22793b00b203SMikulas Patocka 
228083d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
22811da177e4SLinus Torvalds }
22821da177e4SLinus Torvalds 
22839a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2284304f3f6aSMilan Broz {
22853b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
22864e857c58SPeter Zijlstra 	smp_mb__after_atomic();
228753d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2288304f3f6aSMilan Broz }
2289304f3f6aSMilan Broz 
22901da177e4SLinus Torvalds /*
2291042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
22921da177e4SLinus Torvalds  */
2293042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
22941da177e4SLinus Torvalds {
229587eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2296754c5fc7SMike Snitzer 	struct queue_limits limits;
2297042d2a9bSAlasdair G Kergon 	int r;
22981da177e4SLinus Torvalds 
2299e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
23001da177e4SLinus Torvalds 
23011da177e4SLinus Torvalds 	/* device must be suspended */
23024f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
230393c534aeSAlasdair G Kergon 		goto out;
23041da177e4SLinus Torvalds 
23053ae70656SMike Snitzer 	/*
23063ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
23073ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
23083ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
23093ae70656SMike Snitzer 	 * reappear.
23103ae70656SMike Snitzer 	 */
23113ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
231283d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
23133ae70656SMike Snitzer 		if (live_map)
23143ae70656SMike Snitzer 			limits = md->queue->limits;
231583d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
23163ae70656SMike Snitzer 	}
23173ae70656SMike Snitzer 
231887eb5b21SMike Christie 	if (!live_map) {
2319754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2320042d2a9bSAlasdair G Kergon 		if (r) {
2321042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2322754c5fc7SMike Snitzer 			goto out;
2323042d2a9bSAlasdair G Kergon 		}
232487eb5b21SMike Christie 	}
2325754c5fc7SMike Snitzer 
2326042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
232762e08243SMikulas Patocka 	dm_issue_global_event();
23281da177e4SLinus Torvalds 
232993c534aeSAlasdair G Kergon out:
2330e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2331042d2a9bSAlasdair G Kergon 	return map;
23321da177e4SLinus Torvalds }
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds /*
23351da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
23361da177e4SLinus Torvalds  * device.
23371da177e4SLinus Torvalds  */
23382ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
23391da177e4SLinus Torvalds {
2340e39e2e95SAlasdair G Kergon 	int r;
23411da177e4SLinus Torvalds 
23421da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2343dfbe03f6SAlasdair G Kergon 
2344db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2345dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2346cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2347e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2348e39e2e95SAlasdair G Kergon 		return r;
2349dfbe03f6SAlasdair G Kergon 	}
2350dfbe03f6SAlasdair G Kergon 
2351aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2352aa8d7c2fSAlasdair G Kergon 
23531da177e4SLinus Torvalds 	return 0;
23541da177e4SLinus Torvalds }
23551da177e4SLinus Torvalds 
23562ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
23571da177e4SLinus Torvalds {
2358aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2359aa8d7c2fSAlasdair G Kergon 		return;
2360aa8d7c2fSAlasdair G Kergon 
2361db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
23621da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2363aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
23641da177e4SLinus Torvalds }
23651da177e4SLinus Torvalds 
23661da177e4SLinus Torvalds /*
2367b48633f8SBart Van Assche  * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
2368b48633f8SBart Van Assche  * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
2369b48633f8SBart Van Assche  * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
2370b48633f8SBart Van Assche  *
2371ffcc3936SMike Snitzer  * If __dm_suspend returns 0, the device is completely quiescent
2372ffcc3936SMike Snitzer  * now. There is no request-processing activity. All new requests
2373ffcc3936SMike Snitzer  * are being added to md->deferred list.
2374cec47e3dSKiyoshi Ueda  */
2375ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
2376b48633f8SBart Van Assche 			unsigned suspend_flags, long task_state,
2377eaf9a736SMike Snitzer 			int dmf_suspended_flag)
23781da177e4SLinus Torvalds {
2379ffcc3936SMike Snitzer 	bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2380ffcc3936SMike Snitzer 	bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2381ffcc3936SMike Snitzer 	int r;
2382cf222b37SAlasdair G Kergon 
23835a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
23845a8f1f80SBart Van Assche 
23852e93ccc1SKiyoshi Ueda 	/*
23862e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
23872e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
23882e93ccc1SKiyoshi Ueda 	 */
23892e93ccc1SKiyoshi Ueda 	if (noflush)
23902e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
239186331f39SBart Van Assche 	else
239286331f39SBart Van Assche 		pr_debug("%s: suspending with flush\n", dm_device_name(md));
23932e93ccc1SKiyoshi Ueda 
2394d67ee213SMike Snitzer 	/*
2395d67ee213SMike Snitzer 	 * This gets reverted if there's an error later and the targets
2396d67ee213SMike Snitzer 	 * provide the .presuspend_undo hook.
2397d67ee213SMike Snitzer 	 */
23981da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
23991da177e4SLinus Torvalds 
24002e93ccc1SKiyoshi Ueda 	/*
24019f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
24029f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
24039f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
24049f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
24052e93ccc1SKiyoshi Ueda 	 */
240632a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
24072ca3310eSAlasdair G Kergon 		r = lock_fs(md);
2408d67ee213SMike Snitzer 		if (r) {
2409d67ee213SMike Snitzer 			dm_table_presuspend_undo_targets(map);
2410ffcc3936SMike Snitzer 			return r;
2411aa8d7c2fSAlasdair G Kergon 		}
2412d67ee213SMike Snitzer 	}
24131da177e4SLinus Torvalds 
24141da177e4SLinus Torvalds 	/*
24153b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
24163b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
24173b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
24183b00b203SMikulas Patocka 	 * dm_wq_work.
24193b00b203SMikulas Patocka 	 *
24203b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
24213b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
24226a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
24236a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
24246a8736d1STejun Heo 	 * flush_workqueue(md->wq).
24251da177e4SLinus Torvalds 	 */
24261eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
242741abc4e1SHannes Reinecke 	if (map)
242883d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
24291da177e4SLinus Torvalds 
2430d0bcb878SKiyoshi Ueda 	/*
243129e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
243229e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2433d0bcb878SKiyoshi Ueda 	 */
24342eb6e1e3SKeith Busch 	if (dm_request_based(md)) {
2435eca7ee6dSMike Snitzer 		dm_stop_queue(md->queue);
243602233342SMike Snitzer 		if (md->kworker_task)
24373989144fSPetr Mladek 			kthread_flush_worker(&md->kworker);
24382eb6e1e3SKeith Busch 	}
2439cec47e3dSKiyoshi Ueda 
2440d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2441d0bcb878SKiyoshi Ueda 
24421da177e4SLinus Torvalds 	/*
24433b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
24443b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
24453b00b203SMikulas Patocka 	 * to finish.
24461da177e4SLinus Torvalds 	 */
2447b48633f8SBart Van Assche 	r = dm_wait_for_completion(md, task_state);
2448eaf9a736SMike Snitzer 	if (!r)
2449eaf9a736SMike Snitzer 		set_bit(dmf_suspended_flag, &md->flags);
24501da177e4SLinus Torvalds 
24516d6f10dfSMilan Broz 	if (noflush)
2452022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
245341abc4e1SHannes Reinecke 	if (map)
245483d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
24552e93ccc1SKiyoshi Ueda 
24561da177e4SLinus Torvalds 	/* were we interrupted ? */
245746125c1cSMilan Broz 	if (r < 0) {
24589a1fb464SMikulas Patocka 		dm_queue_flush(md);
245973d410c0SMilan Broz 
2460cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
2461eca7ee6dSMike Snitzer 			dm_start_queue(md->queue);
2462cec47e3dSKiyoshi Ueda 
24632ca3310eSAlasdair G Kergon 		unlock_fs(md);
2464d67ee213SMike Snitzer 		dm_table_presuspend_undo_targets(map);
2465ffcc3936SMike Snitzer 		/* pushback list is already flushed, so skip flush */
2466ffcc3936SMike Snitzer 	}
2467ffcc3936SMike Snitzer 
2468ffcc3936SMike Snitzer 	return r;
24692ca3310eSAlasdair G Kergon }
24702ca3310eSAlasdair G Kergon 
24713b00b203SMikulas Patocka /*
2472ffcc3936SMike Snitzer  * We need to be able to change a mapping table under a mounted
2473ffcc3936SMike Snitzer  * filesystem.  For example we might want to move some data in
2474ffcc3936SMike Snitzer  * the background.  Before the table can be swapped with
2475ffcc3936SMike Snitzer  * dm_bind_table, dm_suspend must be called to flush any in
2476ffcc3936SMike Snitzer  * flight bios and ensure that any further io gets deferred.
24773b00b203SMikulas Patocka  */
2478ffcc3936SMike Snitzer /*
2479ffcc3936SMike Snitzer  * Suspend mechanism in request-based dm.
2480ffcc3936SMike Snitzer  *
2481ffcc3936SMike Snitzer  * 1. Flush all I/Os by lock_fs() if needed.
2482ffcc3936SMike Snitzer  * 2. Stop dispatching any I/O by stopping the request_queue.
2483ffcc3936SMike Snitzer  * 3. Wait for all in-flight I/Os to be completed or requeued.
2484ffcc3936SMike Snitzer  *
2485ffcc3936SMike Snitzer  * To abort suspend, start the request_queue.
2486ffcc3936SMike Snitzer  */
2487ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2488ffcc3936SMike Snitzer {
2489ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2490ffcc3936SMike Snitzer 	int r = 0;
2491ffcc3936SMike Snitzer 
2492ffcc3936SMike Snitzer retry:
2493ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2494ffcc3936SMike Snitzer 
2495ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2496ffcc3936SMike Snitzer 		r = -EINVAL;
2497ffcc3936SMike Snitzer 		goto out_unlock;
2498ffcc3936SMike Snitzer 	}
2499ffcc3936SMike Snitzer 
2500ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2501ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2502ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2503ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2504ffcc3936SMike Snitzer 		if (r)
2505ffcc3936SMike Snitzer 			return r;
2506ffcc3936SMike Snitzer 		goto retry;
2507ffcc3936SMike Snitzer 	}
2508ffcc3936SMike Snitzer 
2509a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2510ffcc3936SMike Snitzer 
2511eaf9a736SMike Snitzer 	r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
2512ffcc3936SMike Snitzer 	if (r)
2513ffcc3936SMike Snitzer 		goto out_unlock;
25143b00b203SMikulas Patocka 
25154d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
25164d4471cbSKiyoshi Ueda 
2517d287483dSAlasdair G Kergon out_unlock:
2518e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2519cf222b37SAlasdair G Kergon 	return r;
25201da177e4SLinus Torvalds }
25211da177e4SLinus Torvalds 
2522ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map)
25231da177e4SLinus Torvalds {
2524ffcc3936SMike Snitzer 	if (map) {
2525ffcc3936SMike Snitzer 		int r = dm_table_resume_targets(map);
25268757b776SMilan Broz 		if (r)
2527ffcc3936SMike Snitzer 			return r;
2528ffcc3936SMike Snitzer 	}
25292ca3310eSAlasdair G Kergon 
25309a1fb464SMikulas Patocka 	dm_queue_flush(md);
25312ca3310eSAlasdair G Kergon 
2532cec47e3dSKiyoshi Ueda 	/*
2533cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2534cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2535cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2536cec47e3dSKiyoshi Ueda 	 */
2537cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2538eca7ee6dSMike Snitzer 		dm_start_queue(md->queue);
2539cec47e3dSKiyoshi Ueda 
25402ca3310eSAlasdair G Kergon 	unlock_fs(md);
25412ca3310eSAlasdair G Kergon 
2542ffcc3936SMike Snitzer 	return 0;
2543ffcc3936SMike Snitzer }
2544ffcc3936SMike Snitzer 
2545ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md)
2546ffcc3936SMike Snitzer {
25478dc23658SMinfei Huang 	int r;
2548ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2549ffcc3936SMike Snitzer 
2550ffcc3936SMike Snitzer retry:
25518dc23658SMinfei Huang 	r = -EINVAL;
2552ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2553ffcc3936SMike Snitzer 
2554ffcc3936SMike Snitzer 	if (!dm_suspended_md(md))
2555ffcc3936SMike Snitzer 		goto out;
2556ffcc3936SMike Snitzer 
2557ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2558ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2559ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2560ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2561ffcc3936SMike Snitzer 		if (r)
2562ffcc3936SMike Snitzer 			return r;
2563ffcc3936SMike Snitzer 		goto retry;
2564ffcc3936SMike Snitzer 	}
2565ffcc3936SMike Snitzer 
2566a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2567ffcc3936SMike Snitzer 	if (!map || !dm_table_get_size(map))
2568ffcc3936SMike Snitzer 		goto out;
2569ffcc3936SMike Snitzer 
2570ffcc3936SMike Snitzer 	r = __dm_resume(md, map);
2571ffcc3936SMike Snitzer 	if (r)
2572ffcc3936SMike Snitzer 		goto out;
2573ffcc3936SMike Snitzer 
25742ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
2575cf222b37SAlasdair G Kergon out:
2576e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
25772ca3310eSAlasdair G Kergon 
2578cf222b37SAlasdair G Kergon 	return r;
25791da177e4SLinus Torvalds }
25801da177e4SLinus Torvalds 
2581fd2ed4d2SMikulas Patocka /*
2582fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
2583fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
2584fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
2585fd2ed4d2SMikulas Patocka  */
2586fd2ed4d2SMikulas Patocka 
2587ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
2588ffcc3936SMike Snitzer {
2589ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2590ffcc3936SMike Snitzer 
25911ea0654eSBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
25921ea0654eSBart Van Assche 
259396b26c8cSMikulas Patocka 	if (md->internal_suspend_count++)
2594ffcc3936SMike Snitzer 		return; /* nested internal suspend */
2595ffcc3936SMike Snitzer 
2596ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2597ffcc3936SMike Snitzer 		set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2598ffcc3936SMike Snitzer 		return; /* nest suspend */
2599ffcc3936SMike Snitzer 	}
2600ffcc3936SMike Snitzer 
2601a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2602ffcc3936SMike Snitzer 
2603ffcc3936SMike Snitzer 	/*
2604ffcc3936SMike Snitzer 	 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2605ffcc3936SMike Snitzer 	 * supported.  Properly supporting a TASK_INTERRUPTIBLE internal suspend
2606ffcc3936SMike Snitzer 	 * would require changing .presuspend to return an error -- avoid this
2607ffcc3936SMike Snitzer 	 * until there is a need for more elaborate variants of internal suspend.
2608ffcc3936SMike Snitzer 	 */
2609eaf9a736SMike Snitzer 	(void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
2610eaf9a736SMike Snitzer 			    DMF_SUSPENDED_INTERNALLY);
2611ffcc3936SMike Snitzer 
2612ffcc3936SMike Snitzer 	dm_table_postsuspend_targets(map);
2613ffcc3936SMike Snitzer }
2614ffcc3936SMike Snitzer 
2615ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md)
2616ffcc3936SMike Snitzer {
261796b26c8cSMikulas Patocka 	BUG_ON(!md->internal_suspend_count);
261896b26c8cSMikulas Patocka 
261996b26c8cSMikulas Patocka 	if (--md->internal_suspend_count)
2620ffcc3936SMike Snitzer 		return; /* resume from nested internal suspend */
2621ffcc3936SMike Snitzer 
2622ffcc3936SMike Snitzer 	if (dm_suspended_md(md))
2623ffcc3936SMike Snitzer 		goto done; /* resume from nested suspend */
2624ffcc3936SMike Snitzer 
2625ffcc3936SMike Snitzer 	/*
2626ffcc3936SMike Snitzer 	 * NOTE: existing callers don't need to call dm_table_resume_targets
2627ffcc3936SMike Snitzer 	 * (which may fail -- so best to avoid it for now by passing NULL map)
2628ffcc3936SMike Snitzer 	 */
2629ffcc3936SMike Snitzer 	(void) __dm_resume(md, NULL);
2630ffcc3936SMike Snitzer 
2631ffcc3936SMike Snitzer done:
2632ffcc3936SMike Snitzer 	clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2633ffcc3936SMike Snitzer 	smp_mb__after_atomic();
2634ffcc3936SMike Snitzer 	wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2635ffcc3936SMike Snitzer }
2636ffcc3936SMike Snitzer 
2637ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md)
2638fd2ed4d2SMikulas Patocka {
2639fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
2640ffcc3936SMike Snitzer 	__dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2641ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2642ffcc3936SMike Snitzer }
2643ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
2644ffcc3936SMike Snitzer 
2645ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md)
2646ffcc3936SMike Snitzer {
2647ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2648ffcc3936SMike Snitzer 	__dm_internal_resume(md);
2649ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2650ffcc3936SMike Snitzer }
2651ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume);
2652ffcc3936SMike Snitzer 
2653ffcc3936SMike Snitzer /*
2654ffcc3936SMike Snitzer  * Fast variants of internal suspend/resume hold md->suspend_lock,
2655ffcc3936SMike Snitzer  * which prevents interaction with userspace-driven suspend.
2656ffcc3936SMike Snitzer  */
2657ffcc3936SMike Snitzer 
2658ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md)
2659ffcc3936SMike Snitzer {
2660ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2661ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2662fd2ed4d2SMikulas Patocka 		return;
2663fd2ed4d2SMikulas Patocka 
2664fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2665fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
2666fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
2667fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2668fd2ed4d2SMikulas Patocka }
2669b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
2670fd2ed4d2SMikulas Patocka 
2671ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md)
2672fd2ed4d2SMikulas Patocka {
2673ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2674fd2ed4d2SMikulas Patocka 		goto done;
2675fd2ed4d2SMikulas Patocka 
2676fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
2677fd2ed4d2SMikulas Patocka 
2678fd2ed4d2SMikulas Patocka done:
2679fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
2680fd2ed4d2SMikulas Patocka }
2681b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
2682fd2ed4d2SMikulas Patocka 
26831da177e4SLinus Torvalds /*-----------------------------------------------------------------
26841da177e4SLinus Torvalds  * Event notification.
26851da177e4SLinus Torvalds  *---------------------------------------------------------------*/
26863abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
268760935eb2SMilan Broz 		       unsigned cookie)
268869267a30SAlasdair G Kergon {
268960935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
269060935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
269160935eb2SMilan Broz 
269260935eb2SMilan Broz 	if (!cookie)
26933abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
269460935eb2SMilan Broz 	else {
269560935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
269660935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
26973abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
26983abf85b5SPeter Rajnoha 					  action, envp);
269960935eb2SMilan Broz 	}
270069267a30SAlasdair G Kergon }
270169267a30SAlasdair G Kergon 
27027a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
27037a8c3d3bSMike Anderson {
27047a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
27057a8c3d3bSMike Anderson }
27067a8c3d3bSMike Anderson 
27071da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
27081da177e4SLinus Torvalds {
27091da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
27101da177e4SLinus Torvalds }
27111da177e4SLinus Torvalds 
27121da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
27131da177e4SLinus Torvalds {
27141da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
27151da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
27161da177e4SLinus Torvalds }
27171da177e4SLinus Torvalds 
27187a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
27197a8c3d3bSMike Anderson {
27207a8c3d3bSMike Anderson 	unsigned long flags;
27217a8c3d3bSMike Anderson 
27227a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
27237a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
27247a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
27257a8c3d3bSMike Anderson }
27267a8c3d3bSMike Anderson 
27271da177e4SLinus Torvalds /*
27281da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
27291da177e4SLinus Torvalds  * count on 'md'.
27301da177e4SLinus Torvalds  */
27311da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
27321da177e4SLinus Torvalds {
27331da177e4SLinus Torvalds 	return md->disk;
27341da177e4SLinus Torvalds }
273565ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk);
27361da177e4SLinus Torvalds 
2737784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2738784aae73SMilan Broz {
27392995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
2740784aae73SMilan Broz }
2741784aae73SMilan Broz 
2742784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2743784aae73SMilan Broz {
2744784aae73SMilan Broz 	struct mapped_device *md;
2745784aae73SMilan Broz 
27462995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
2747784aae73SMilan Broz 
2748b9a41d21SHou Tao 	spin_lock(&_minor_lock);
2749b9a41d21SHou Tao 	if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2750b9a41d21SHou Tao 		md = NULL;
2751b9a41d21SHou Tao 		goto out;
2752b9a41d21SHou Tao 	}
2753784aae73SMilan Broz 	dm_get(md);
2754b9a41d21SHou Tao out:
2755b9a41d21SHou Tao 	spin_unlock(&_minor_lock);
2756b9a41d21SHou Tao 
2757784aae73SMilan Broz 	return md;
2758784aae73SMilan Broz }
2759784aae73SMilan Broz 
27604f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
27611da177e4SLinus Torvalds {
27621da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
27631da177e4SLinus Torvalds }
27641da177e4SLinus Torvalds 
2765ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md)
2766ffcc3936SMike Snitzer {
2767ffcc3936SMike Snitzer 	return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2768ffcc3936SMike Snitzer }
2769ffcc3936SMike Snitzer 
27702c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
27712c140a24SMikulas Patocka {
27722c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
27732c140a24SMikulas Patocka }
27742c140a24SMikulas Patocka 
277564dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
277664dbce58SKiyoshi Ueda {
2777ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
277864dbce58SKiyoshi Ueda }
277964dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
278064dbce58SKiyoshi Ueda 
27812e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
27822e93ccc1SKiyoshi Ueda {
2783ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
27842e93ccc1SKiyoshi Ueda }
27852e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
27862e93ccc1SKiyoshi Ueda 
27877e0d574fSBart Van Assche struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type,
278830187e1dSMike Snitzer 					    unsigned integrity, unsigned per_io_data_size)
2789e6ee8c0bSKiyoshi Ueda {
2790115485e8SMike Snitzer 	struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
279178d8e58aSMike Snitzer 	unsigned int pool_size = 0;
27925f015204SJun'ichi Nomura 	unsigned int front_pad;
2793e6ee8c0bSKiyoshi Ueda 
2794e6ee8c0bSKiyoshi Ueda 	if (!pools)
27954e6e36c3SMike Snitzer 		return NULL;
2796e6ee8c0bSKiyoshi Ueda 
279778d8e58aSMike Snitzer 	switch (type) {
279878d8e58aSMike Snitzer 	case DM_TYPE_BIO_BASED:
2799545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
280078d8e58aSMike Snitzer 		pool_size = dm_get_reserved_bio_based_ios();
280130187e1dSMike Snitzer 		front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
2802eb8db831SChristoph Hellwig 
2803eb8db831SChristoph Hellwig 		pools->io_pool = mempool_create_slab_pool(pool_size, _io_cache);
2804eb8db831SChristoph Hellwig 		if (!pools->io_pool)
2805eb8db831SChristoph Hellwig 			goto out;
280678d8e58aSMike Snitzer 		break;
280778d8e58aSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
280878d8e58aSMike Snitzer 	case DM_TYPE_MQ_REQUEST_BASED:
280978d8e58aSMike Snitzer 		pool_size = dm_get_reserved_rq_based_ios();
281078d8e58aSMike Snitzer 		front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
2811591ddcfcSMike Snitzer 		/* per_io_data_size is used for blk-mq pdu at queue allocation */
281278d8e58aSMike Snitzer 		break;
281378d8e58aSMike Snitzer 	default:
281478d8e58aSMike Snitzer 		BUG();
281578d8e58aSMike Snitzer 	}
281678d8e58aSMike Snitzer 
281747e0fb46SNeilBrown 	pools->bs = bioset_create(pool_size, front_pad, BIOSET_NEED_RESCUER);
2818e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
28195f015204SJun'ichi Nomura 		goto out;
2820e6ee8c0bSKiyoshi Ueda 
2821a91a2785SMartin K. Petersen 	if (integrity && bioset_integrity_create(pools->bs, pool_size))
28225f015204SJun'ichi Nomura 		goto out;
2823a91a2785SMartin K. Petersen 
2824e6ee8c0bSKiyoshi Ueda 	return pools;
282578d8e58aSMike Snitzer 
28265f015204SJun'ichi Nomura out:
28275f015204SJun'ichi Nomura 	dm_free_md_mempools(pools);
2828e6ee8c0bSKiyoshi Ueda 
28294e6e36c3SMike Snitzer 	return NULL;
2830e6ee8c0bSKiyoshi Ueda }
2831e6ee8c0bSKiyoshi Ueda 
2832e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
2833e6ee8c0bSKiyoshi Ueda {
2834e6ee8c0bSKiyoshi Ueda 	if (!pools)
2835e6ee8c0bSKiyoshi Ueda 		return;
2836e6ee8c0bSKiyoshi Ueda 
2837e6ee8c0bSKiyoshi Ueda 	mempool_destroy(pools->io_pool);
28381ae49ea2SMike Snitzer 
2839e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
2840e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
2841e6ee8c0bSKiyoshi Ueda 
2842e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2843e6ee8c0bSKiyoshi Ueda }
2844e6ee8c0bSKiyoshi Ueda 
28459c72bad1SChristoph Hellwig struct dm_pr {
28469c72bad1SChristoph Hellwig 	u64	old_key;
28479c72bad1SChristoph Hellwig 	u64	new_key;
28489c72bad1SChristoph Hellwig 	u32	flags;
28499c72bad1SChristoph Hellwig 	bool	fail_early;
28509c72bad1SChristoph Hellwig };
28519c72bad1SChristoph Hellwig 
28529c72bad1SChristoph Hellwig static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
28539c72bad1SChristoph Hellwig 		      void *data)
28549c72bad1SChristoph Hellwig {
28559c72bad1SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
28569c72bad1SChristoph Hellwig 	struct dm_table *table;
28579c72bad1SChristoph Hellwig 	struct dm_target *ti;
28589c72bad1SChristoph Hellwig 	int ret = -ENOTTY, srcu_idx;
28599c72bad1SChristoph Hellwig 
28609c72bad1SChristoph Hellwig 	table = dm_get_live_table(md, &srcu_idx);
28619c72bad1SChristoph Hellwig 	if (!table || !dm_table_get_size(table))
28629c72bad1SChristoph Hellwig 		goto out;
28639c72bad1SChristoph Hellwig 
28649c72bad1SChristoph Hellwig 	/* We only support devices that have a single target */
28659c72bad1SChristoph Hellwig 	if (dm_table_get_num_targets(table) != 1)
28669c72bad1SChristoph Hellwig 		goto out;
28679c72bad1SChristoph Hellwig 	ti = dm_table_get_target(table, 0);
28689c72bad1SChristoph Hellwig 
28699c72bad1SChristoph Hellwig 	ret = -EINVAL;
28709c72bad1SChristoph Hellwig 	if (!ti->type->iterate_devices)
28719c72bad1SChristoph Hellwig 		goto out;
28729c72bad1SChristoph Hellwig 
28739c72bad1SChristoph Hellwig 	ret = ti->type->iterate_devices(ti, fn, data);
28749c72bad1SChristoph Hellwig out:
28759c72bad1SChristoph Hellwig 	dm_put_live_table(md, srcu_idx);
28769c72bad1SChristoph Hellwig 	return ret;
28779c72bad1SChristoph Hellwig }
28789c72bad1SChristoph Hellwig 
28799c72bad1SChristoph Hellwig /*
28809c72bad1SChristoph Hellwig  * For register / unregister we need to manually call out to every path.
28819c72bad1SChristoph Hellwig  */
28829c72bad1SChristoph Hellwig static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
28839c72bad1SChristoph Hellwig 			    sector_t start, sector_t len, void *data)
28849c72bad1SChristoph Hellwig {
28859c72bad1SChristoph Hellwig 	struct dm_pr *pr = data;
28869c72bad1SChristoph Hellwig 	const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
28879c72bad1SChristoph Hellwig 
28889c72bad1SChristoph Hellwig 	if (!ops || !ops->pr_register)
28899c72bad1SChristoph Hellwig 		return -EOPNOTSUPP;
28909c72bad1SChristoph Hellwig 	return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
28919c72bad1SChristoph Hellwig }
28929c72bad1SChristoph Hellwig 
289371cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
289471cdb697SChristoph Hellwig 			  u32 flags)
289571cdb697SChristoph Hellwig {
28969c72bad1SChristoph Hellwig 	struct dm_pr pr = {
28979c72bad1SChristoph Hellwig 		.old_key	= old_key,
28989c72bad1SChristoph Hellwig 		.new_key	= new_key,
28999c72bad1SChristoph Hellwig 		.flags		= flags,
29009c72bad1SChristoph Hellwig 		.fail_early	= true,
29019c72bad1SChristoph Hellwig 	};
29029c72bad1SChristoph Hellwig 	int ret;
290371cdb697SChristoph Hellwig 
29049c72bad1SChristoph Hellwig 	ret = dm_call_pr(bdev, __dm_pr_register, &pr);
29059c72bad1SChristoph Hellwig 	if (ret && new_key) {
29069c72bad1SChristoph Hellwig 		/* unregister all paths if we failed to register any path */
29079c72bad1SChristoph Hellwig 		pr.old_key = new_key;
29089c72bad1SChristoph Hellwig 		pr.new_key = 0;
29099c72bad1SChristoph Hellwig 		pr.flags = 0;
29109c72bad1SChristoph Hellwig 		pr.fail_early = false;
29119c72bad1SChristoph Hellwig 		dm_call_pr(bdev, __dm_pr_register, &pr);
29129c72bad1SChristoph Hellwig 	}
291371cdb697SChristoph Hellwig 
29149c72bad1SChristoph Hellwig 	return ret;
291571cdb697SChristoph Hellwig }
291671cdb697SChristoph Hellwig 
291771cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
291871cdb697SChristoph Hellwig 			 u32 flags)
291971cdb697SChristoph Hellwig {
292071cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
292171cdb697SChristoph Hellwig 	const struct pr_ops *ops;
292271cdb697SChristoph Hellwig 	fmode_t mode;
2923956a4025SMike Snitzer 	int r;
292471cdb697SChristoph Hellwig 
2925956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
292671cdb697SChristoph Hellwig 	if (r < 0)
292771cdb697SChristoph Hellwig 		return r;
292871cdb697SChristoph Hellwig 
292971cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
293071cdb697SChristoph Hellwig 	if (ops && ops->pr_reserve)
293171cdb697SChristoph Hellwig 		r = ops->pr_reserve(bdev, key, type, flags);
293271cdb697SChristoph Hellwig 	else
293371cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
293471cdb697SChristoph Hellwig 
2935956a4025SMike Snitzer 	bdput(bdev);
293671cdb697SChristoph Hellwig 	return r;
293771cdb697SChristoph Hellwig }
293871cdb697SChristoph Hellwig 
293971cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
294071cdb697SChristoph Hellwig {
294171cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
294271cdb697SChristoph Hellwig 	const struct pr_ops *ops;
294371cdb697SChristoph Hellwig 	fmode_t mode;
2944956a4025SMike Snitzer 	int r;
294571cdb697SChristoph Hellwig 
2946956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
294771cdb697SChristoph Hellwig 	if (r < 0)
294871cdb697SChristoph Hellwig 		return r;
294971cdb697SChristoph Hellwig 
295071cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
295171cdb697SChristoph Hellwig 	if (ops && ops->pr_release)
295271cdb697SChristoph Hellwig 		r = ops->pr_release(bdev, key, type);
295371cdb697SChristoph Hellwig 	else
295471cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
295571cdb697SChristoph Hellwig 
2956956a4025SMike Snitzer 	bdput(bdev);
295771cdb697SChristoph Hellwig 	return r;
295871cdb697SChristoph Hellwig }
295971cdb697SChristoph Hellwig 
296071cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
296171cdb697SChristoph Hellwig 			 enum pr_type type, bool abort)
296271cdb697SChristoph Hellwig {
296371cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
296471cdb697SChristoph Hellwig 	const struct pr_ops *ops;
296571cdb697SChristoph Hellwig 	fmode_t mode;
2966956a4025SMike Snitzer 	int r;
296771cdb697SChristoph Hellwig 
2968956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
296971cdb697SChristoph Hellwig 	if (r < 0)
297071cdb697SChristoph Hellwig 		return r;
297171cdb697SChristoph Hellwig 
297271cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
297371cdb697SChristoph Hellwig 	if (ops && ops->pr_preempt)
297471cdb697SChristoph Hellwig 		r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
297571cdb697SChristoph Hellwig 	else
297671cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
297771cdb697SChristoph Hellwig 
2978956a4025SMike Snitzer 	bdput(bdev);
297971cdb697SChristoph Hellwig 	return r;
298071cdb697SChristoph Hellwig }
298171cdb697SChristoph Hellwig 
298271cdb697SChristoph Hellwig static int dm_pr_clear(struct block_device *bdev, u64 key)
298371cdb697SChristoph Hellwig {
298471cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
298571cdb697SChristoph Hellwig 	const struct pr_ops *ops;
298671cdb697SChristoph Hellwig 	fmode_t mode;
2987956a4025SMike Snitzer 	int r;
298871cdb697SChristoph Hellwig 
2989956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
299071cdb697SChristoph Hellwig 	if (r < 0)
299171cdb697SChristoph Hellwig 		return r;
299271cdb697SChristoph Hellwig 
299371cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
299471cdb697SChristoph Hellwig 	if (ops && ops->pr_clear)
299571cdb697SChristoph Hellwig 		r = ops->pr_clear(bdev, key);
299671cdb697SChristoph Hellwig 	else
299771cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
299871cdb697SChristoph Hellwig 
2999956a4025SMike Snitzer 	bdput(bdev);
300071cdb697SChristoph Hellwig 	return r;
300171cdb697SChristoph Hellwig }
300271cdb697SChristoph Hellwig 
300371cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = {
300471cdb697SChristoph Hellwig 	.pr_register	= dm_pr_register,
300571cdb697SChristoph Hellwig 	.pr_reserve	= dm_pr_reserve,
300671cdb697SChristoph Hellwig 	.pr_release	= dm_pr_release,
300771cdb697SChristoph Hellwig 	.pr_preempt	= dm_pr_preempt,
300871cdb697SChristoph Hellwig 	.pr_clear	= dm_pr_clear,
300971cdb697SChristoph Hellwig };
301071cdb697SChristoph Hellwig 
301183d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
30121da177e4SLinus Torvalds 	.open = dm_blk_open,
30131da177e4SLinus Torvalds 	.release = dm_blk_close,
3014aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
30153ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
301671cdb697SChristoph Hellwig 	.pr_ops = &dm_pr_ops,
30171da177e4SLinus Torvalds 	.owner = THIS_MODULE
30181da177e4SLinus Torvalds };
30191da177e4SLinus Torvalds 
3020f26c5719SDan Williams static const struct dax_operations dm_dax_ops = {
3021f26c5719SDan Williams 	.direct_access = dm_dax_direct_access,
30227e026c8cSDan Williams 	.copy_from_iter = dm_dax_copy_from_iter,
3023f26c5719SDan Williams };
3024f26c5719SDan Williams 
30251da177e4SLinus Torvalds /*
30261da177e4SLinus Torvalds  * module hooks
30271da177e4SLinus Torvalds  */
30281da177e4SLinus Torvalds module_init(dm_init);
30291da177e4SLinus Torvalds module_exit(dm_exit);
30301da177e4SLinus Torvalds 
30311da177e4SLinus Torvalds module_param(major, uint, 0);
30321da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
3033f4790826SMike Snitzer 
3034e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3035e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3036e8603136SMike Snitzer 
3037115485e8SMike Snitzer module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
3038115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
3039115485e8SMike Snitzer 
30401da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
30411da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
30421da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3043