xref: /openbmc/linux/drivers/md/dm.c (revision e83068a5faafb8ca65d3b58bd1e1e3959ce1ddce)
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>
151da177e4SLinus Torvalds #include <linux/blkpg.h>
161da177e4SLinus Torvalds #include <linux/bio.h>
171da177e4SLinus Torvalds #include <linux/mempool.h>
181da177e4SLinus Torvalds #include <linux/slab.h>
191da177e4SLinus Torvalds #include <linux/idr.h>
203ac51e74SDarrick J. Wong #include <linux/hdreg.h>
213f77316dSKiyoshi Ueda #include <linux/delay.h>
22ffcc3936SMike Snitzer #include <linux/wait.h>
2371cdb697SChristoph Hellwig #include <linux/pr.h>
2455782138SLi Zefan 
2572d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
2672d94861SAlasdair G Kergon 
2771a16736SNamhyung Kim #ifdef CONFIG_PRINTK
2871a16736SNamhyung Kim /*
2971a16736SNamhyung Kim  * ratelimit state to be used in DMXXX_LIMIT().
3071a16736SNamhyung Kim  */
3171a16736SNamhyung Kim DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
3271a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_INTERVAL,
3371a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_BURST);
3471a16736SNamhyung Kim EXPORT_SYMBOL(dm_ratelimit_state);
3571a16736SNamhyung Kim #endif
3671a16736SNamhyung Kim 
3760935eb2SMilan Broz /*
3860935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
3960935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
4060935eb2SMilan Broz  */
4160935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
4260935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
4360935eb2SMilan Broz 
441da177e4SLinus Torvalds static const char *_name = DM_NAME;
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds static unsigned int major = 0;
471da177e4SLinus Torvalds static unsigned int _major = 0;
481da177e4SLinus Torvalds 
49d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr);
50d15b774cSAlasdair G Kergon 
51f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
522c140a24SMikulas Patocka 
532c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w);
542c140a24SMikulas Patocka 
552c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
562c140a24SMikulas Patocka 
57acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue;
58acfe0ad7SMikulas Patocka 
591da177e4SLinus Torvalds /*
601da177e4SLinus Torvalds  * One of these is allocated per bio.
611da177e4SLinus Torvalds  */
621da177e4SLinus Torvalds struct dm_io {
631da177e4SLinus Torvalds 	struct mapped_device *md;
641da177e4SLinus Torvalds 	int error;
651da177e4SLinus Torvalds 	atomic_t io_count;
666ae2fa67SRichard Kennedy 	struct bio *bio;
673eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
68f88fb981SKiyoshi Ueda 	spinlock_t endio_lock;
69fd2ed4d2SMikulas Patocka 	struct dm_stats_aux stats_aux;
701da177e4SLinus Torvalds };
711da177e4SLinus Torvalds 
72ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
73ba61fdd1SJeff Mahoney 
741da177e4SLinus Torvalds /*
751da177e4SLinus Torvalds  * Bits for the md->flags field.
761da177e4SLinus Torvalds  */
771eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
781da177e4SLinus Torvalds #define DMF_SUSPENDED 1
79aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
80fba9f90eSJeff Mahoney #define DMF_FREEING 3
815c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
822e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
838ae12666SKent Overstreet #define DMF_DEFERRED_REMOVE 6
848ae12666SKent Overstreet #define DMF_SUSPENDED_INTERNALLY 7
851da177e4SLinus Torvalds 
86115485e8SMike Snitzer #define DM_NUMA_NODE NUMA_NO_NODE
87115485e8SMike Snitzer static int dm_numa_node = DM_NUMA_NODE;
88faad87dfSMike Snitzer 
89e6ee8c0bSKiyoshi Ueda /*
90e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
91e6ee8c0bSKiyoshi Ueda  */
92e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
93e6ee8c0bSKiyoshi Ueda 	mempool_t *io_pool;
941ae49ea2SMike Snitzer 	mempool_t *rq_pool;
95e6ee8c0bSKiyoshi Ueda 	struct bio_set *bs;
96e6ee8c0bSKiyoshi Ueda };
97e6ee8c0bSKiyoshi Ueda 
9886f1152bSBenjamin Marzinski struct table_device {
9986f1152bSBenjamin Marzinski 	struct list_head list;
10086f1152bSBenjamin Marzinski 	atomic_t count;
10186f1152bSBenjamin Marzinski 	struct dm_dev dm_dev;
10286f1152bSBenjamin Marzinski };
10386f1152bSBenjamin Marzinski 
104e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
1058fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
1061ae49ea2SMike Snitzer static struct kmem_cache *_rq_cache;
10794818742SKent Overstreet 
108f4790826SMike Snitzer /*
109e8603136SMike Snitzer  * Bio-based DM's mempools' reserved IOs set by the user.
110e8603136SMike Snitzer  */
1114cc96131SMike Snitzer #define RESERVED_BIO_BASED_IOS		16
112e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
113e8603136SMike Snitzer 
114115485e8SMike Snitzer static int __dm_get_module_param_int(int *module_param, int min, int max)
115115485e8SMike Snitzer {
116115485e8SMike Snitzer 	int param = ACCESS_ONCE(*module_param);
117115485e8SMike Snitzer 	int modified_param = 0;
118115485e8SMike Snitzer 	bool modified = true;
119115485e8SMike Snitzer 
120115485e8SMike Snitzer 	if (param < min)
121115485e8SMike Snitzer 		modified_param = min;
122115485e8SMike Snitzer 	else if (param > max)
123115485e8SMike Snitzer 		modified_param = max;
124115485e8SMike Snitzer 	else
125115485e8SMike Snitzer 		modified = false;
126115485e8SMike Snitzer 
127115485e8SMike Snitzer 	if (modified) {
128115485e8SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
129115485e8SMike Snitzer 		param = modified_param;
130115485e8SMike Snitzer 	}
131115485e8SMike Snitzer 
132115485e8SMike Snitzer 	return param;
133115485e8SMike Snitzer }
134115485e8SMike Snitzer 
1354cc96131SMike Snitzer unsigned __dm_get_module_param(unsigned *module_param,
136f4790826SMike Snitzer 			       unsigned def, unsigned max)
137f4790826SMike Snitzer {
13809c2d531SMike Snitzer 	unsigned param = ACCESS_ONCE(*module_param);
13909c2d531SMike Snitzer 	unsigned modified_param = 0;
140f4790826SMike Snitzer 
14109c2d531SMike Snitzer 	if (!param)
14209c2d531SMike Snitzer 		modified_param = def;
14309c2d531SMike Snitzer 	else if (param > max)
14409c2d531SMike Snitzer 		modified_param = max;
145f4790826SMike Snitzer 
14609c2d531SMike Snitzer 	if (modified_param) {
14709c2d531SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
14809c2d531SMike Snitzer 		param = modified_param;
149f4790826SMike Snitzer 	}
150f4790826SMike Snitzer 
15109c2d531SMike Snitzer 	return param;
152f4790826SMike Snitzer }
153f4790826SMike Snitzer 
154e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void)
155e8603136SMike Snitzer {
15609c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_bio_based_ios,
1574cc96131SMike Snitzer 				     RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS);
158e8603136SMike Snitzer }
159e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
160e8603136SMike Snitzer 
161115485e8SMike Snitzer static unsigned dm_get_numa_node(void)
162115485e8SMike Snitzer {
163115485e8SMike Snitzer 	return __dm_get_module_param_int(&dm_numa_node,
164115485e8SMike Snitzer 					 DM_NUMA_NODE, num_online_nodes() - 1);
165115485e8SMike Snitzer }
166115485e8SMike Snitzer 
1671da177e4SLinus Torvalds static int __init local_init(void)
1681da177e4SLinus Torvalds {
16951157b4aSKiyoshi Ueda 	int r = -ENOMEM;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
172028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
1731da177e4SLinus Torvalds 	if (!_io_cache)
17451157b4aSKiyoshi Ueda 		return r;
1751da177e4SLinus Torvalds 
1768fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
1778fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
178dba14160SMikulas Patocka 		goto out_free_io_cache;
1798fbf26adSKiyoshi Ueda 
180eca7ee6dSMike Snitzer 	_rq_cache = kmem_cache_create("dm_old_clone_request", sizeof(struct request),
1811ae49ea2SMike Snitzer 				      __alignof__(struct request), 0, NULL);
1821ae49ea2SMike Snitzer 	if (!_rq_cache)
1831ae49ea2SMike Snitzer 		goto out_free_rq_tio_cache;
1841ae49ea2SMike Snitzer 
18551e5b2bdSMike Anderson 	r = dm_uevent_init();
18651157b4aSKiyoshi Ueda 	if (r)
1871ae49ea2SMike Snitzer 		goto out_free_rq_cache;
18851e5b2bdSMike Anderson 
189acfe0ad7SMikulas Patocka 	deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
190acfe0ad7SMikulas Patocka 	if (!deferred_remove_workqueue) {
191acfe0ad7SMikulas Patocka 		r = -ENOMEM;
192acfe0ad7SMikulas Patocka 		goto out_uevent_exit;
193acfe0ad7SMikulas Patocka 	}
194acfe0ad7SMikulas Patocka 
1951da177e4SLinus Torvalds 	_major = major;
1961da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
19751157b4aSKiyoshi Ueda 	if (r < 0)
198acfe0ad7SMikulas Patocka 		goto out_free_workqueue;
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	if (!_major)
2011da177e4SLinus Torvalds 		_major = r;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	return 0;
20451157b4aSKiyoshi Ueda 
205acfe0ad7SMikulas Patocka out_free_workqueue:
206acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
20751157b4aSKiyoshi Ueda out_uevent_exit:
20851157b4aSKiyoshi Ueda 	dm_uevent_exit();
2091ae49ea2SMike Snitzer out_free_rq_cache:
2101ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
2118fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
2128fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
21351157b4aSKiyoshi Ueda out_free_io_cache:
21451157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
21551157b4aSKiyoshi Ueda 
21651157b4aSKiyoshi Ueda 	return r;
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds static void local_exit(void)
2201da177e4SLinus Torvalds {
2212c140a24SMikulas Patocka 	flush_scheduled_work();
222acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
2232c140a24SMikulas Patocka 
2241ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
2258fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
2261da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
22700d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
22851e5b2bdSMike Anderson 	dm_uevent_exit();
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	_major = 0;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	DMINFO("cleaned up");
2331da177e4SLinus Torvalds }
2341da177e4SLinus Torvalds 
235b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
2361da177e4SLinus Torvalds 	local_init,
2371da177e4SLinus Torvalds 	dm_target_init,
2381da177e4SLinus Torvalds 	dm_linear_init,
2391da177e4SLinus Torvalds 	dm_stripe_init,
240952b3557SMikulas Patocka 	dm_io_init,
241945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2421da177e4SLinus Torvalds 	dm_interface_init,
243fd2ed4d2SMikulas Patocka 	dm_statistics_init,
2441da177e4SLinus Torvalds };
2451da177e4SLinus Torvalds 
246b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2471da177e4SLinus Torvalds 	local_exit,
2481da177e4SLinus Torvalds 	dm_target_exit,
2491da177e4SLinus Torvalds 	dm_linear_exit,
2501da177e4SLinus Torvalds 	dm_stripe_exit,
251952b3557SMikulas Patocka 	dm_io_exit,
252945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
2531da177e4SLinus Torvalds 	dm_interface_exit,
254fd2ed4d2SMikulas Patocka 	dm_statistics_exit,
2551da177e4SLinus Torvalds };
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds static int __init dm_init(void)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	int r, i;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
2641da177e4SLinus Torvalds 		r = _inits[i]();
2651da177e4SLinus Torvalds 		if (r)
2661da177e4SLinus Torvalds 			goto bad;
2671da177e4SLinus Torvalds 	}
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	return 0;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds       bad:
2721da177e4SLinus Torvalds 	while (i--)
2731da177e4SLinus Torvalds 		_exits[i]();
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	return r;
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds static void __exit dm_exit(void)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	while (i--)
2831da177e4SLinus Torvalds 		_exits[i]();
284d15b774cSAlasdair G Kergon 
285d15b774cSAlasdair G Kergon 	/*
286d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
287d15b774cSAlasdair G Kergon 	 */
288d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds /*
2921da177e4SLinus Torvalds  * Block device functions
2931da177e4SLinus Torvalds  */
294432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
295432a212cSMike Anderson {
296432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
297432a212cSMike Anderson }
298432a212cSMike Anderson 
299fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3001da177e4SLinus Torvalds {
3011da177e4SLinus Torvalds 	struct mapped_device *md;
3021da177e4SLinus Torvalds 
303fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
304fba9f90eSJeff Mahoney 
305fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
306fba9f90eSJeff Mahoney 	if (!md)
307fba9f90eSJeff Mahoney 		goto out;
308fba9f90eSJeff Mahoney 
3095c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
310432a212cSMike Anderson 	    dm_deleting_md(md)) {
311fba9f90eSJeff Mahoney 		md = NULL;
312fba9f90eSJeff Mahoney 		goto out;
313fba9f90eSJeff Mahoney 	}
314fba9f90eSJeff Mahoney 
3151da177e4SLinus Torvalds 	dm_get(md);
3165c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
317fba9f90eSJeff Mahoney out:
318fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
319fba9f90eSJeff Mahoney 
320fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
323db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode)
3241da177e4SLinus Torvalds {
32563a4f065SMike Snitzer 	struct mapped_device *md;
3266e9624b8SArnd Bergmann 
3274a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
3284a1aeb98SMilan Broz 
32963a4f065SMike Snitzer 	md = disk->private_data;
33063a4f065SMike Snitzer 	if (WARN_ON(!md))
33163a4f065SMike Snitzer 		goto out;
33263a4f065SMike Snitzer 
3332c140a24SMikulas Patocka 	if (atomic_dec_and_test(&md->open_count) &&
3342c140a24SMikulas Patocka 	    (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
335acfe0ad7SMikulas Patocka 		queue_work(deferred_remove_workqueue, &deferred_remove_work);
3362c140a24SMikulas Patocka 
3371da177e4SLinus Torvalds 	dm_put(md);
33863a4f065SMike Snitzer out:
3394a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
3425c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3435c6bd75dSAlasdair G Kergon {
3445c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3455c6bd75dSAlasdair G Kergon }
3465c6bd75dSAlasdair G Kergon 
3475c6bd75dSAlasdair G Kergon /*
3485c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3495c6bd75dSAlasdair G Kergon  */
3502c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
3515c6bd75dSAlasdair G Kergon {
3525c6bd75dSAlasdair G Kergon 	int r = 0;
3535c6bd75dSAlasdair G Kergon 
3545c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
3555c6bd75dSAlasdair G Kergon 
3562c140a24SMikulas Patocka 	if (dm_open_count(md)) {
3575c6bd75dSAlasdair G Kergon 		r = -EBUSY;
3582c140a24SMikulas Patocka 		if (mark_deferred)
3592c140a24SMikulas Patocka 			set_bit(DMF_DEFERRED_REMOVE, &md->flags);
3602c140a24SMikulas Patocka 	} else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
3612c140a24SMikulas Patocka 		r = -EEXIST;
3625c6bd75dSAlasdair G Kergon 	else
3635c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
3645c6bd75dSAlasdair G Kergon 
3655c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
3665c6bd75dSAlasdair G Kergon 
3675c6bd75dSAlasdair G Kergon 	return r;
3685c6bd75dSAlasdair G Kergon }
3695c6bd75dSAlasdair G Kergon 
3702c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md)
3712c140a24SMikulas Patocka {
3722c140a24SMikulas Patocka 	int r = 0;
3732c140a24SMikulas Patocka 
3742c140a24SMikulas Patocka 	spin_lock(&_minor_lock);
3752c140a24SMikulas Patocka 
3762c140a24SMikulas Patocka 	if (test_bit(DMF_DELETING, &md->flags))
3772c140a24SMikulas Patocka 		r = -EBUSY;
3782c140a24SMikulas Patocka 	else
3792c140a24SMikulas Patocka 		clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
3802c140a24SMikulas Patocka 
3812c140a24SMikulas Patocka 	spin_unlock(&_minor_lock);
3822c140a24SMikulas Patocka 
3832c140a24SMikulas Patocka 	return r;
3842c140a24SMikulas Patocka }
3852c140a24SMikulas Patocka 
3862c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w)
3872c140a24SMikulas Patocka {
3882c140a24SMikulas Patocka 	dm_deferred_remove();
3892c140a24SMikulas Patocka }
3902c140a24SMikulas Patocka 
391fd2ed4d2SMikulas Patocka sector_t dm_get_size(struct mapped_device *md)
392fd2ed4d2SMikulas Patocka {
393fd2ed4d2SMikulas Patocka 	return get_capacity(md->disk);
394fd2ed4d2SMikulas Patocka }
395fd2ed4d2SMikulas Patocka 
3969974fa2cSMike Snitzer struct request_queue *dm_get_md_queue(struct mapped_device *md)
3979974fa2cSMike Snitzer {
3989974fa2cSMike Snitzer 	return md->queue;
3999974fa2cSMike Snitzer }
4009974fa2cSMike Snitzer 
401fd2ed4d2SMikulas Patocka struct dm_stats *dm_get_stats(struct mapped_device *md)
402fd2ed4d2SMikulas Patocka {
403fd2ed4d2SMikulas Patocka 	return &md->stats;
404fd2ed4d2SMikulas Patocka }
405fd2ed4d2SMikulas Patocka 
4063ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4073ac51e74SDarrick J. Wong {
4083ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
4093ac51e74SDarrick J. Wong 
4103ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
4113ac51e74SDarrick J. Wong }
4123ac51e74SDarrick J. Wong 
413956a4025SMike Snitzer static int dm_grab_bdev_for_ioctl(struct mapped_device *md,
41466482026SMike Snitzer 				  struct block_device **bdev,
415956a4025SMike Snitzer 				  fmode_t *mode)
416aa129a22SMilan Broz {
41766482026SMike Snitzer 	struct dm_target *tgt;
4186c182cd8SHannes Reinecke 	struct dm_table *map;
419956a4025SMike Snitzer 	int srcu_idx, r;
420aa129a22SMilan Broz 
4216c182cd8SHannes Reinecke retry:
422e56f81e0SChristoph Hellwig 	r = -ENOTTY;
423956a4025SMike Snitzer 	map = dm_get_live_table(md, &srcu_idx);
424aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
425aa129a22SMilan Broz 		goto out;
426aa129a22SMilan Broz 
427aa129a22SMilan Broz 	/* We only support devices that have a single target */
428aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
429aa129a22SMilan Broz 		goto out;
430aa129a22SMilan Broz 
43166482026SMike Snitzer 	tgt = dm_table_get_target(map, 0);
43266482026SMike Snitzer 	if (!tgt->type->prepare_ioctl)
4334d341d82SMike Snitzer 		goto out;
434aa129a22SMilan Broz 
4354f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
436aa129a22SMilan Broz 		r = -EAGAIN;
437aa129a22SMilan Broz 		goto out;
438aa129a22SMilan Broz 	}
439aa129a22SMilan Broz 
44066482026SMike Snitzer 	r = tgt->type->prepare_ioctl(tgt, bdev, mode);
441e56f81e0SChristoph Hellwig 	if (r < 0)
442e56f81e0SChristoph Hellwig 		goto out;
443e56f81e0SChristoph Hellwig 
444956a4025SMike Snitzer 	bdgrab(*bdev);
445956a4025SMike Snitzer 	dm_put_live_table(md, srcu_idx);
446e56f81e0SChristoph Hellwig 	return r;
447aa129a22SMilan Broz 
448aa129a22SMilan Broz out:
449956a4025SMike Snitzer 	dm_put_live_table(md, srcu_idx);
4505bbbfdf6SJunichi Nomura 	if (r == -ENOTCONN && !fatal_signal_pending(current)) {
4516c182cd8SHannes Reinecke 		msleep(10);
4526c182cd8SHannes Reinecke 		goto retry;
4536c182cd8SHannes Reinecke 	}
454e56f81e0SChristoph Hellwig 	return r;
455e56f81e0SChristoph Hellwig }
4566c182cd8SHannes Reinecke 
457e56f81e0SChristoph Hellwig static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
458e56f81e0SChristoph Hellwig 			unsigned int cmd, unsigned long arg)
459e56f81e0SChristoph Hellwig {
460e56f81e0SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
461956a4025SMike Snitzer 	int r;
462e56f81e0SChristoph Hellwig 
463956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
464e56f81e0SChristoph Hellwig 	if (r < 0)
465e56f81e0SChristoph Hellwig 		return r;
466e56f81e0SChristoph Hellwig 
467e56f81e0SChristoph Hellwig 	if (r > 0) {
468e56f81e0SChristoph Hellwig 		/*
469e56f81e0SChristoph Hellwig 		 * Target determined this ioctl is being issued against
470e56f81e0SChristoph Hellwig 		 * a logical partition of the parent bdev; so extra
471e56f81e0SChristoph Hellwig 		 * validation is needed.
472e56f81e0SChristoph Hellwig 		 */
473e56f81e0SChristoph Hellwig 		r = scsi_verify_blk_ioctl(NULL, cmd);
474e56f81e0SChristoph Hellwig 		if (r)
475e56f81e0SChristoph Hellwig 			goto out;
476e56f81e0SChristoph Hellwig 	}
477e56f81e0SChristoph Hellwig 
47866482026SMike Snitzer 	r =  __blkdev_driver_ioctl(bdev, mode, cmd, arg);
479e56f81e0SChristoph Hellwig out:
480956a4025SMike Snitzer 	bdput(bdev);
481aa129a22SMilan Broz 	return r;
482aa129a22SMilan Broz }
483aa129a22SMilan Broz 
484028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
4851da177e4SLinus Torvalds {
4861da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
489028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
4901da177e4SLinus Torvalds {
4911da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
4921da177e4SLinus Torvalds }
4931da177e4SLinus Torvalds 
494cfae7529SMike Snitzer static void free_tio(struct dm_target_io *tio)
4951da177e4SLinus Torvalds {
496dba14160SMikulas Patocka 	bio_put(&tio->clone);
4971da177e4SLinus Torvalds }
4981da177e4SLinus Torvalds 
4994cc96131SMike Snitzer int md_in_flight(struct mapped_device *md)
50090abb8c4SKiyoshi Ueda {
50190abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
50290abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
50390abb8c4SKiyoshi Ueda }
50490abb8c4SKiyoshi Ueda 
5053eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
5063eaf840eSJun'ichi "Nick" Nomura {
5073eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
508fd2ed4d2SMikulas Patocka 	struct bio *bio = io->bio;
509c9959059STejun Heo 	int cpu;
510fd2ed4d2SMikulas Patocka 	int rw = bio_data_dir(bio);
5113eaf840eSJun'ichi "Nick" Nomura 
5123eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
5133eaf840eSJun'ichi "Nick" Nomura 
514074a7acaSTejun Heo 	cpu = part_stat_lock();
515074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
516074a7acaSTejun Heo 	part_stat_unlock();
5171e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw],
5181e9bb880SShaohua Li 		atomic_inc_return(&md->pending[rw]));
519fd2ed4d2SMikulas Patocka 
520fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
521528ec5abSMike Christie 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
522528ec5abSMike Christie 				    bio->bi_iter.bi_sector, bio_sectors(bio),
523528ec5abSMike Christie 				    false, 0, &io->stats_aux);
5243eaf840eSJun'ichi "Nick" Nomura }
5253eaf840eSJun'ichi "Nick" Nomura 
526d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
5273eaf840eSJun'ichi "Nick" Nomura {
5283eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
5293eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
5303eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
53118c0b223SGu Zheng 	int pending;
5323eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
5333eaf840eSJun'ichi "Nick" Nomura 
53418c0b223SGu Zheng 	generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time);
5353eaf840eSJun'ichi "Nick" Nomura 
536fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
537528ec5abSMike Christie 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
538528ec5abSMike Christie 				    bio->bi_iter.bi_sector, bio_sectors(bio),
539528ec5abSMike Christie 				    true, duration, &io->stats_aux);
540fd2ed4d2SMikulas Patocka 
541af7e466aSMikulas Patocka 	/*
542af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
543d87f4c14STejun Heo 	 * a flush.
544af7e466aSMikulas Patocka 	 */
5451e9bb880SShaohua Li 	pending = atomic_dec_return(&md->pending[rw]);
5461e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
547316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
5483eaf840eSJun'ichi "Nick" Nomura 
549d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
550d221d2e7SMikulas Patocka 	if (!pending)
551d221d2e7SMikulas Patocka 		wake_up(&md->wait);
5523eaf840eSJun'ichi "Nick" Nomura }
5533eaf840eSJun'ichi "Nick" Nomura 
5541da177e4SLinus Torvalds /*
5551da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
5561da177e4SLinus Torvalds  */
55792c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
5581da177e4SLinus Torvalds {
55905447420SKiyoshi Ueda 	unsigned long flags;
5601da177e4SLinus Torvalds 
56105447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
5621da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
56305447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
56492c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
5651da177e4SLinus Torvalds }
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds /*
5681da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
5691da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
57083d5e5b0SMikulas Patocka  * dm_put_live_table() when finished.
5711da177e4SLinus Torvalds  */
57283d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
5731da177e4SLinus Torvalds {
57483d5e5b0SMikulas Patocka 	*srcu_idx = srcu_read_lock(&md->io_barrier);
5751da177e4SLinus Torvalds 
57683d5e5b0SMikulas Patocka 	return srcu_dereference(md->map, &md->io_barrier);
57783d5e5b0SMikulas Patocka }
5781da177e4SLinus Torvalds 
57983d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
58083d5e5b0SMikulas Patocka {
58183d5e5b0SMikulas Patocka 	srcu_read_unlock(&md->io_barrier, srcu_idx);
58283d5e5b0SMikulas Patocka }
58383d5e5b0SMikulas Patocka 
58483d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md)
58583d5e5b0SMikulas Patocka {
58683d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
58783d5e5b0SMikulas Patocka 	synchronize_rcu_expedited();
58883d5e5b0SMikulas Patocka }
58983d5e5b0SMikulas Patocka 
59083d5e5b0SMikulas Patocka /*
59183d5e5b0SMikulas Patocka  * A fast alternative to dm_get_live_table/dm_put_live_table.
59283d5e5b0SMikulas Patocka  * The caller must not block between these two functions.
59383d5e5b0SMikulas Patocka  */
59483d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
59583d5e5b0SMikulas Patocka {
59683d5e5b0SMikulas Patocka 	rcu_read_lock();
59783d5e5b0SMikulas Patocka 	return rcu_dereference(md->map);
59883d5e5b0SMikulas Patocka }
59983d5e5b0SMikulas Patocka 
60083d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
60183d5e5b0SMikulas Patocka {
60283d5e5b0SMikulas Patocka 	rcu_read_unlock();
6031da177e4SLinus Torvalds }
6041da177e4SLinus Torvalds 
6053ac51e74SDarrick J. Wong /*
60686f1152bSBenjamin Marzinski  * Open a table device so we can use it as a map destination.
60786f1152bSBenjamin Marzinski  */
60886f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev,
60986f1152bSBenjamin Marzinski 			     struct mapped_device *md)
61086f1152bSBenjamin Marzinski {
61186f1152bSBenjamin Marzinski 	static char *_claim_ptr = "I belong to device-mapper";
61286f1152bSBenjamin Marzinski 	struct block_device *bdev;
61386f1152bSBenjamin Marzinski 
61486f1152bSBenjamin Marzinski 	int r;
61586f1152bSBenjamin Marzinski 
61686f1152bSBenjamin Marzinski 	BUG_ON(td->dm_dev.bdev);
61786f1152bSBenjamin Marzinski 
61886f1152bSBenjamin Marzinski 	bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
61986f1152bSBenjamin Marzinski 	if (IS_ERR(bdev))
62086f1152bSBenjamin Marzinski 		return PTR_ERR(bdev);
62186f1152bSBenjamin Marzinski 
62286f1152bSBenjamin Marzinski 	r = bd_link_disk_holder(bdev, dm_disk(md));
62386f1152bSBenjamin Marzinski 	if (r) {
62486f1152bSBenjamin Marzinski 		blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
62586f1152bSBenjamin Marzinski 		return r;
62686f1152bSBenjamin Marzinski 	}
62786f1152bSBenjamin Marzinski 
62886f1152bSBenjamin Marzinski 	td->dm_dev.bdev = bdev;
62986f1152bSBenjamin Marzinski 	return 0;
63086f1152bSBenjamin Marzinski }
63186f1152bSBenjamin Marzinski 
63286f1152bSBenjamin Marzinski /*
63386f1152bSBenjamin Marzinski  * Close a table device that we've been using.
63486f1152bSBenjamin Marzinski  */
63586f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md)
63686f1152bSBenjamin Marzinski {
63786f1152bSBenjamin Marzinski 	if (!td->dm_dev.bdev)
63886f1152bSBenjamin Marzinski 		return;
63986f1152bSBenjamin Marzinski 
64086f1152bSBenjamin Marzinski 	bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
64186f1152bSBenjamin Marzinski 	blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
64286f1152bSBenjamin Marzinski 	td->dm_dev.bdev = NULL;
64386f1152bSBenjamin Marzinski }
64486f1152bSBenjamin Marzinski 
64586f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev,
64686f1152bSBenjamin Marzinski 					      fmode_t mode) {
64786f1152bSBenjamin Marzinski 	struct table_device *td;
64886f1152bSBenjamin Marzinski 
64986f1152bSBenjamin Marzinski 	list_for_each_entry(td, l, list)
65086f1152bSBenjamin Marzinski 		if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
65186f1152bSBenjamin Marzinski 			return td;
65286f1152bSBenjamin Marzinski 
65386f1152bSBenjamin Marzinski 	return NULL;
65486f1152bSBenjamin Marzinski }
65586f1152bSBenjamin Marzinski 
65686f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
65786f1152bSBenjamin Marzinski 			struct dm_dev **result) {
65886f1152bSBenjamin Marzinski 	int r;
65986f1152bSBenjamin Marzinski 	struct table_device *td;
66086f1152bSBenjamin Marzinski 
66186f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
66286f1152bSBenjamin Marzinski 	td = find_table_device(&md->table_devices, dev, mode);
66386f1152bSBenjamin Marzinski 	if (!td) {
664115485e8SMike Snitzer 		td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
66586f1152bSBenjamin Marzinski 		if (!td) {
66686f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
66786f1152bSBenjamin Marzinski 			return -ENOMEM;
66886f1152bSBenjamin Marzinski 		}
66986f1152bSBenjamin Marzinski 
67086f1152bSBenjamin Marzinski 		td->dm_dev.mode = mode;
67186f1152bSBenjamin Marzinski 		td->dm_dev.bdev = NULL;
67286f1152bSBenjamin Marzinski 
67386f1152bSBenjamin Marzinski 		if ((r = open_table_device(td, dev, md))) {
67486f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
67586f1152bSBenjamin Marzinski 			kfree(td);
67686f1152bSBenjamin Marzinski 			return r;
67786f1152bSBenjamin Marzinski 		}
67886f1152bSBenjamin Marzinski 
67986f1152bSBenjamin Marzinski 		format_dev_t(td->dm_dev.name, dev);
68086f1152bSBenjamin Marzinski 
68186f1152bSBenjamin Marzinski 		atomic_set(&td->count, 0);
68286f1152bSBenjamin Marzinski 		list_add(&td->list, &md->table_devices);
68386f1152bSBenjamin Marzinski 	}
68486f1152bSBenjamin Marzinski 	atomic_inc(&td->count);
68586f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
68686f1152bSBenjamin Marzinski 
68786f1152bSBenjamin Marzinski 	*result = &td->dm_dev;
68886f1152bSBenjamin Marzinski 	return 0;
68986f1152bSBenjamin Marzinski }
69086f1152bSBenjamin Marzinski EXPORT_SYMBOL_GPL(dm_get_table_device);
69186f1152bSBenjamin Marzinski 
69286f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
69386f1152bSBenjamin Marzinski {
69486f1152bSBenjamin Marzinski 	struct table_device *td = container_of(d, struct table_device, dm_dev);
69586f1152bSBenjamin Marzinski 
69686f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
69786f1152bSBenjamin Marzinski 	if (atomic_dec_and_test(&td->count)) {
69886f1152bSBenjamin Marzinski 		close_table_device(td, md);
69986f1152bSBenjamin Marzinski 		list_del(&td->list);
70086f1152bSBenjamin Marzinski 		kfree(td);
70186f1152bSBenjamin Marzinski 	}
70286f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
70386f1152bSBenjamin Marzinski }
70486f1152bSBenjamin Marzinski EXPORT_SYMBOL(dm_put_table_device);
70586f1152bSBenjamin Marzinski 
70686f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices)
70786f1152bSBenjamin Marzinski {
70886f1152bSBenjamin Marzinski 	struct list_head *tmp, *next;
70986f1152bSBenjamin Marzinski 
71086f1152bSBenjamin Marzinski 	list_for_each_safe(tmp, next, devices) {
71186f1152bSBenjamin Marzinski 		struct table_device *td = list_entry(tmp, struct table_device, list);
71286f1152bSBenjamin Marzinski 
71386f1152bSBenjamin Marzinski 		DMWARN("dm_destroy: %s still exists with %d references",
71486f1152bSBenjamin Marzinski 		       td->dm_dev.name, atomic_read(&td->count));
71586f1152bSBenjamin Marzinski 		kfree(td);
71686f1152bSBenjamin Marzinski 	}
71786f1152bSBenjamin Marzinski }
71886f1152bSBenjamin Marzinski 
71986f1152bSBenjamin Marzinski /*
7203ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
7213ac51e74SDarrick J. Wong  */
7223ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
7233ac51e74SDarrick J. Wong {
7243ac51e74SDarrick J. Wong 	*geo = md->geometry;
7253ac51e74SDarrick J. Wong 
7263ac51e74SDarrick J. Wong 	return 0;
7273ac51e74SDarrick J. Wong }
7283ac51e74SDarrick J. Wong 
7293ac51e74SDarrick J. Wong /*
7303ac51e74SDarrick J. Wong  * Set the geometry of a device.
7313ac51e74SDarrick J. Wong  */
7323ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
7333ac51e74SDarrick J. Wong {
7343ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
7353ac51e74SDarrick J. Wong 
7363ac51e74SDarrick J. Wong 	if (geo->start > sz) {
7373ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
7383ac51e74SDarrick J. Wong 		return -EINVAL;
7393ac51e74SDarrick J. Wong 	}
7403ac51e74SDarrick J. Wong 
7413ac51e74SDarrick J. Wong 	md->geometry = *geo;
7423ac51e74SDarrick J. Wong 
7433ac51e74SDarrick J. Wong 	return 0;
7443ac51e74SDarrick J. Wong }
7453ac51e74SDarrick J. Wong 
7461da177e4SLinus Torvalds /*-----------------------------------------------------------------
7471da177e4SLinus Torvalds  * CRUD START:
7481da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
7491da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
7501da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
7511da177e4SLinus Torvalds  *   interests of getting something for people to use I give
7521da177e4SLinus Torvalds  *   you this clearly demarcated crap.
7531da177e4SLinus Torvalds  *---------------------------------------------------------------*/
7541da177e4SLinus Torvalds 
7552e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
7562e93ccc1SKiyoshi Ueda {
7572e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
7582e93ccc1SKiyoshi Ueda }
7592e93ccc1SKiyoshi Ueda 
7601da177e4SLinus Torvalds /*
7611da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
7621da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
7631da177e4SLinus Torvalds  */
764858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
7651da177e4SLinus Torvalds {
7662e93ccc1SKiyoshi Ueda 	unsigned long flags;
767b35f8caaSMilan Broz 	int io_error;
768b35f8caaSMilan Broz 	struct bio *bio;
769b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
7702e93ccc1SKiyoshi Ueda 
7712e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
772f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
773f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
774f88fb981SKiyoshi Ueda 		if (!(io->error > 0 && __noflush_suspending(md)))
7751da177e4SLinus Torvalds 			io->error = error;
776f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
777f88fb981SKiyoshi Ueda 	}
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
7802e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
7812e93ccc1SKiyoshi Ueda 			/*
7822e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
7832e93ccc1SKiyoshi Ueda 			 */
784022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
7856a8736d1STejun Heo 			if (__noflush_suspending(md))
7866a8736d1STejun Heo 				bio_list_add_head(&md->deferred, io->bio);
7876a8736d1STejun Heo 			else
7882e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
7892e93ccc1SKiyoshi Ueda 				io->error = -EIO;
790022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
7912e93ccc1SKiyoshi Ueda 		}
7922e93ccc1SKiyoshi Ueda 
793b35f8caaSMilan Broz 		io_error = io->error;
794b35f8caaSMilan Broz 		bio = io->bio;
795af7e466aSMikulas Patocka 		end_io_acct(io);
796a97f925aSMikulas Patocka 		free_io(md, io);
7971da177e4SLinus Torvalds 
7986a8736d1STejun Heo 		if (io_error == DM_ENDIO_REQUEUE)
7996a8736d1STejun Heo 			return;
8006a8736d1STejun Heo 
80128a8f0d3SMike Christie 		if ((bio->bi_rw & REQ_PREFLUSH) && bio->bi_iter.bi_size) {
8021da177e4SLinus Torvalds 			/*
8036a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
80428a8f0d3SMike Christie 			 * without REQ_PREFLUSH.
8051da177e4SLinus Torvalds 			 */
80628a8f0d3SMike Christie 			bio->bi_rw &= ~REQ_PREFLUSH;
8076a8736d1STejun Heo 			queue_io(md, bio);
8085f3ea37cSArnaldo Carvalho de Melo 		} else {
809b372d360SMike Snitzer 			/* done with normal IO or empty flush */
8100a82a8d1SLinus Torvalds 			trace_block_bio_complete(md->queue, bio, io_error);
8114246a0b6SChristoph Hellwig 			bio->bi_error = io_error;
8124246a0b6SChristoph Hellwig 			bio_endio(bio);
8132e93ccc1SKiyoshi Ueda 		}
8141da177e4SLinus Torvalds 	}
815af7e466aSMikulas Patocka }
8161da177e4SLinus Torvalds 
8174cc96131SMike Snitzer void disable_write_same(struct mapped_device *md)
8187eee4ae2SMike Snitzer {
8197eee4ae2SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
8207eee4ae2SMike Snitzer 
8217eee4ae2SMike Snitzer 	/* device doesn't really support WRITE SAME, disable it */
8227eee4ae2SMike Snitzer 	limits->max_write_same_sectors = 0;
8237eee4ae2SMike Snitzer }
8247eee4ae2SMike Snitzer 
8254246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio)
8261da177e4SLinus Torvalds {
8274246a0b6SChristoph Hellwig 	int error = bio->bi_error;
8285164beceSzhendong chen 	int r = error;
829bfc6d41cSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
830b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
8319faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
8321da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	if (endio) {
8357de3ee57SMikulas Patocka 		r = endio(tio->ti, bio, error);
8362e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
8372e93ccc1SKiyoshi Ueda 			/*
8382e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
8392e93ccc1SKiyoshi Ueda 			 * in dec_pending().
8402e93ccc1SKiyoshi Ueda 			 */
8411da177e4SLinus Torvalds 			error = r;
84245cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
84345cbcd79SKiyoshi Ueda 			/* The target will handle the io */
8446712ecf8SNeilBrown 			return;
84545cbcd79SKiyoshi Ueda 		else if (r) {
84645cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
84745cbcd79SKiyoshi Ueda 			BUG();
84845cbcd79SKiyoshi Ueda 		}
8491da177e4SLinus Torvalds 	}
8501da177e4SLinus Torvalds 
851e6047149SMike Christie 	if (unlikely(r == -EREMOTEIO && (bio_op(bio) == REQ_OP_WRITE_SAME) &&
8527eee4ae2SMike Snitzer 		     !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
8537eee4ae2SMike Snitzer 		disable_write_same(md);
8547eee4ae2SMike Snitzer 
855cfae7529SMike Snitzer 	free_tio(tio);
856b35f8caaSMilan Broz 	dec_pending(io, error);
8571da177e4SLinus Torvalds }
8581da177e4SLinus Torvalds 
85978d8e58aSMike Snitzer /*
86056a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
86156a67df7SMike Snitzer  * target boundary.
86256a67df7SMike Snitzer  */
86356a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
8641da177e4SLinus Torvalds {
86556a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
86656a67df7SMike Snitzer 
86756a67df7SMike Snitzer 	return ti->len - target_offset;
86856a67df7SMike Snitzer }
86956a67df7SMike Snitzer 
87056a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
87156a67df7SMike Snitzer {
87256a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
873542f9038SMike Snitzer 	sector_t offset, max_len;
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds 	/*
8761da177e4SLinus Torvalds 	 * Does the target need to split even further?
8771da177e4SLinus Torvalds 	 */
878542f9038SMike Snitzer 	if (ti->max_io_len) {
879542f9038SMike Snitzer 		offset = dm_target_offset(ti, sector);
880542f9038SMike Snitzer 		if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
881542f9038SMike Snitzer 			max_len = sector_div(offset, ti->max_io_len);
882542f9038SMike Snitzer 		else
883542f9038SMike Snitzer 			max_len = offset & (ti->max_io_len - 1);
884542f9038SMike Snitzer 		max_len = ti->max_io_len - max_len;
885542f9038SMike Snitzer 
886542f9038SMike Snitzer 		if (len > max_len)
887542f9038SMike Snitzer 			len = max_len;
8881da177e4SLinus Torvalds 	}
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	return len;
8911da177e4SLinus Torvalds }
8921da177e4SLinus Torvalds 
893542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
894542f9038SMike Snitzer {
895542f9038SMike Snitzer 	if (len > UINT_MAX) {
896542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
897542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
898542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
899542f9038SMike Snitzer 		return -EINVAL;
900542f9038SMike Snitzer 	}
901542f9038SMike Snitzer 
902542f9038SMike Snitzer 	ti->max_io_len = (uint32_t) len;
903542f9038SMike Snitzer 
904542f9038SMike Snitzer 	return 0;
905542f9038SMike Snitzer }
906542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
907542f9038SMike Snitzer 
9081dd40c3eSMikulas Patocka /*
9091dd40c3eSMikulas Patocka  * A target may call dm_accept_partial_bio only from the map routine.  It is
91028a8f0d3SMike Christie  * allowed for all bio types except REQ_PREFLUSH.
9111dd40c3eSMikulas Patocka  *
9121dd40c3eSMikulas Patocka  * dm_accept_partial_bio informs the dm that the target only wants to process
9131dd40c3eSMikulas Patocka  * additional n_sectors sectors of the bio and the rest of the data should be
9141dd40c3eSMikulas Patocka  * sent in a next bio.
9151dd40c3eSMikulas Patocka  *
9161dd40c3eSMikulas Patocka  * A diagram that explains the arithmetics:
9171dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
9181dd40c3eSMikulas Patocka  * |         1          |       2       |   3   |
9191dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
9201dd40c3eSMikulas Patocka  *
9211dd40c3eSMikulas Patocka  * <-------------- *tio->len_ptr --------------->
9221dd40c3eSMikulas Patocka  *                      <------- bi_size ------->
9231dd40c3eSMikulas Patocka  *                      <-- n_sectors -->
9241dd40c3eSMikulas Patocka  *
9251dd40c3eSMikulas Patocka  * Region 1 was already iterated over with bio_advance or similar function.
9261dd40c3eSMikulas Patocka  *	(it may be empty if the target doesn't use bio_advance)
9271dd40c3eSMikulas Patocka  * Region 2 is the remaining bio size that the target wants to process.
9281dd40c3eSMikulas Patocka  *	(it may be empty if region 1 is non-empty, although there is no reason
9291dd40c3eSMikulas Patocka  *	 to make it empty)
9301dd40c3eSMikulas Patocka  * The target requires that region 3 is to be sent in the next bio.
9311dd40c3eSMikulas Patocka  *
9321dd40c3eSMikulas Patocka  * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
9331dd40c3eSMikulas Patocka  * the partially processed part (the sum of regions 1+2) must be the same for all
9341dd40c3eSMikulas Patocka  * copies of the bio.
9351dd40c3eSMikulas Patocka  */
9361dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
9371dd40c3eSMikulas Patocka {
9381dd40c3eSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
9391dd40c3eSMikulas Patocka 	unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
94028a8f0d3SMike Christie 	BUG_ON(bio->bi_rw & REQ_PREFLUSH);
9411dd40c3eSMikulas Patocka 	BUG_ON(bi_size > *tio->len_ptr);
9421dd40c3eSMikulas Patocka 	BUG_ON(n_sectors > bi_size);
9431dd40c3eSMikulas Patocka 	*tio->len_ptr -= bi_size - n_sectors;
9441dd40c3eSMikulas Patocka 	bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
9451dd40c3eSMikulas Patocka }
9461dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
9471dd40c3eSMikulas Patocka 
948bd2a49b8SAlasdair G Kergon static void __map_bio(struct dm_target_io *tio)
9491da177e4SLinus Torvalds {
9501da177e4SLinus Torvalds 	int r;
9512056a782SJens Axboe 	sector_t sector;
952dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
953bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	/*
9581da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
9591da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
9601da177e4SLinus Torvalds 	 * this io.
9611da177e4SLinus Torvalds 	 */
9621da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
9634f024f37SKent Overstreet 	sector = clone->bi_iter.bi_sector;
9647de3ee57SMikulas Patocka 	r = ti->type->map(ti, clone);
96545cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
9661da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
9672056a782SJens Axboe 
968d07335e5SMike Snitzer 		trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
96922a7c31aSAlan D. Brunelle 				      tio->io->bio->bi_bdev->bd_dev, sector);
9702056a782SJens Axboe 
9711da177e4SLinus Torvalds 		generic_make_request(clone);
9722e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
9732e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
9749faf400fSStefan Bader 		dec_pending(tio->io, r);
975cfae7529SMike Snitzer 		free_tio(tio);
976ab37844dSMikulas Patocka 	} else if (r != DM_MAPIO_SUBMITTED) {
97745cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
97845cbcd79SKiyoshi Ueda 		BUG();
9791da177e4SLinus Torvalds 	}
9801da177e4SLinus Torvalds }
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds struct clone_info {
9831da177e4SLinus Torvalds 	struct mapped_device *md;
9841da177e4SLinus Torvalds 	struct dm_table *map;
9851da177e4SLinus Torvalds 	struct bio *bio;
9861da177e4SLinus Torvalds 	struct dm_io *io;
9871da177e4SLinus Torvalds 	sector_t sector;
988e0d6609aSMikulas Patocka 	unsigned sector_count;
9891da177e4SLinus Torvalds };
9901da177e4SLinus Torvalds 
991e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
992bd2a49b8SAlasdair G Kergon {
9934f024f37SKent Overstreet 	bio->bi_iter.bi_sector = sector;
9944f024f37SKent Overstreet 	bio->bi_iter.bi_size = to_bytes(len);
9951da177e4SLinus Torvalds }
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds /*
9981da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
9991da177e4SLinus Torvalds  */
1000c80914e8SMike Snitzer static int clone_bio(struct dm_target_io *tio, struct bio *bio,
10011c3b13e6SKent Overstreet 		     sector_t sector, unsigned len)
10021da177e4SLinus Torvalds {
1003dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
10041da177e4SLinus Torvalds 
10051c3b13e6SKent Overstreet 	__bio_clone_fast(clone, bio);
10069c47008dSMartin K. Petersen 
1007c80914e8SMike Snitzer 	if (bio_integrity(bio)) {
1008c80914e8SMike Snitzer 		int r = bio_integrity_clone(clone, bio, GFP_NOIO);
1009c80914e8SMike Snitzer 		if (r < 0)
1010c80914e8SMike Snitzer 			return r;
1011c80914e8SMike Snitzer 	}
10121c3b13e6SKent Overstreet 
10131c3b13e6SKent Overstreet 	bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
10141c3b13e6SKent Overstreet 	clone->bi_iter.bi_size = to_bytes(len);
10151c3b13e6SKent Overstreet 
10161c3b13e6SKent Overstreet 	if (bio_integrity(bio))
10171c3b13e6SKent Overstreet 		bio_integrity_trim(clone, 0, len);
1018c80914e8SMike Snitzer 
1019c80914e8SMike Snitzer 	return 0;
10201da177e4SLinus Torvalds }
10211da177e4SLinus Torvalds 
10229015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci,
102399778273SJunichi Nomura 				      struct dm_target *ti,
102455a62eefSAlasdair G Kergon 				      unsigned target_bio_nr)
1025f9ab94ceSMikulas Patocka {
1026dba14160SMikulas Patocka 	struct dm_target_io *tio;
1027dba14160SMikulas Patocka 	struct bio *clone;
1028dba14160SMikulas Patocka 
102999778273SJunichi Nomura 	clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1030dba14160SMikulas Patocka 	tio = container_of(clone, struct dm_target_io, clone);
1031f9ab94ceSMikulas Patocka 
1032f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1033f9ab94ceSMikulas Patocka 	tio->ti = ti;
103455a62eefSAlasdair G Kergon 	tio->target_bio_nr = target_bio_nr;
10359015df24SAlasdair G Kergon 
10369015df24SAlasdair G Kergon 	return tio;
10379015df24SAlasdair G Kergon }
10389015df24SAlasdair G Kergon 
103914fe594dSAlasdair G Kergon static void __clone_and_map_simple_bio(struct clone_info *ci,
104014fe594dSAlasdair G Kergon 				       struct dm_target *ti,
10411dd40c3eSMikulas Patocka 				       unsigned target_bio_nr, unsigned *len)
10429015df24SAlasdair G Kergon {
104399778273SJunichi Nomura 	struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
1044dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
10459015df24SAlasdair G Kergon 
10461dd40c3eSMikulas Patocka 	tio->len_ptr = len;
10471dd40c3eSMikulas Patocka 
10481c3b13e6SKent Overstreet 	__bio_clone_fast(clone, ci->bio);
1049bd2a49b8SAlasdair G Kergon 	if (len)
10501dd40c3eSMikulas Patocka 		bio_setup_sector(clone, ci->sector, *len);
1051f9ab94ceSMikulas Patocka 
1052bd2a49b8SAlasdair G Kergon 	__map_bio(tio);
1053f9ab94ceSMikulas Patocka }
1054f9ab94ceSMikulas Patocka 
105514fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
10561dd40c3eSMikulas Patocka 				  unsigned num_bios, unsigned *len)
105706a426ceSMike Snitzer {
105855a62eefSAlasdair G Kergon 	unsigned target_bio_nr;
105906a426ceSMike Snitzer 
106055a62eefSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
106114fe594dSAlasdair G Kergon 		__clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
106206a426ceSMike Snitzer }
106306a426ceSMike Snitzer 
106414fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1065f9ab94ceSMikulas Patocka {
106606a426ceSMike Snitzer 	unsigned target_nr = 0;
1067f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1068f9ab94ceSMikulas Patocka 
1069b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1070f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
10711dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
1072f9ab94ceSMikulas Patocka 
1073f9ab94ceSMikulas Patocka 	return 0;
1074f9ab94ceSMikulas Patocka }
1075f9ab94ceSMikulas Patocka 
1076c80914e8SMike Snitzer static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
10771dd40c3eSMikulas Patocka 				     sector_t sector, unsigned *len)
10785ae89a87SMike Snitzer {
1079dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
10805ae89a87SMike Snitzer 	struct dm_target_io *tio;
1081b0d8ed4dSAlasdair G Kergon 	unsigned target_bio_nr;
1082b0d8ed4dSAlasdair G Kergon 	unsigned num_target_bios = 1;
1083c80914e8SMike Snitzer 	int r = 0;
10845ae89a87SMike Snitzer 
1085b0d8ed4dSAlasdair G Kergon 	/*
1086b0d8ed4dSAlasdair G Kergon 	 * Does the target want to receive duplicate copies of the bio?
1087b0d8ed4dSAlasdair G Kergon 	 */
1088b0d8ed4dSAlasdair G Kergon 	if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1089b0d8ed4dSAlasdair G Kergon 		num_target_bios = ti->num_write_bios(ti, bio);
1090e4c93811SAlasdair G Kergon 
1091b0d8ed4dSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
109299778273SJunichi Nomura 		tio = alloc_tio(ci, ti, target_bio_nr);
10931dd40c3eSMikulas Patocka 		tio->len_ptr = len;
1094c80914e8SMike Snitzer 		r = clone_bio(tio, bio, sector, *len);
1095072623deSMikulas Patocka 		if (r < 0) {
1096cfae7529SMike Snitzer 			free_tio(tio);
1097c80914e8SMike Snitzer 			break;
1098072623deSMikulas Patocka 		}
1099bd2a49b8SAlasdair G Kergon 		__map_bio(tio);
11005ae89a87SMike Snitzer 	}
1101c80914e8SMike Snitzer 
1102c80914e8SMike Snitzer 	return r;
1103b0d8ed4dSAlasdair G Kergon }
11045ae89a87SMike Snitzer 
110555a62eefSAlasdair G Kergon typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
110623508a96SMike Snitzer 
110755a62eefSAlasdair G Kergon static unsigned get_num_discard_bios(struct dm_target *ti)
110823508a96SMike Snitzer {
110955a62eefSAlasdair G Kergon 	return ti->num_discard_bios;
111023508a96SMike Snitzer }
111123508a96SMike Snitzer 
111255a62eefSAlasdair G Kergon static unsigned get_num_write_same_bios(struct dm_target *ti)
111323508a96SMike Snitzer {
111455a62eefSAlasdair G Kergon 	return ti->num_write_same_bios;
111523508a96SMike Snitzer }
111623508a96SMike Snitzer 
111723508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti);
111823508a96SMike Snitzer 
111923508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti)
112023508a96SMike Snitzer {
112155a62eefSAlasdair G Kergon 	return ti->split_discard_bios;
112223508a96SMike Snitzer }
112323508a96SMike Snitzer 
112414fe594dSAlasdair G Kergon static int __send_changing_extent_only(struct clone_info *ci,
112555a62eefSAlasdair G Kergon 				       get_num_bios_fn get_num_bios,
112623508a96SMike Snitzer 				       is_split_required_fn is_split_required)
11275ae89a87SMike Snitzer {
11285ae89a87SMike Snitzer 	struct dm_target *ti;
1129e0d6609aSMikulas Patocka 	unsigned len;
113055a62eefSAlasdair G Kergon 	unsigned num_bios;
11315ae89a87SMike Snitzer 
1132a79245b3SMike Snitzer 	do {
11335ae89a87SMike Snitzer 		ti = dm_table_find_target(ci->map, ci->sector);
11345ae89a87SMike Snitzer 		if (!dm_target_is_valid(ti))
11355ae89a87SMike Snitzer 			return -EIO;
11365ae89a87SMike Snitzer 
11375ae89a87SMike Snitzer 		/*
113823508a96SMike Snitzer 		 * Even though the device advertised support for this type of
113923508a96SMike Snitzer 		 * request, that does not mean every target supports it, and
1140936688d7SMike Snitzer 		 * reconfiguration might also have changed that since the
11415ae89a87SMike Snitzer 		 * check was performed.
11425ae89a87SMike Snitzer 		 */
114355a62eefSAlasdair G Kergon 		num_bios = get_num_bios ? get_num_bios(ti) : 0;
114455a62eefSAlasdair G Kergon 		if (!num_bios)
11455ae89a87SMike Snitzer 			return -EOPNOTSUPP;
11465ae89a87SMike Snitzer 
114723508a96SMike Snitzer 		if (is_split_required && !is_split_required(ti))
1148e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
11497acf0277SMikulas Patocka 		else
1150e0d6609aSMikulas Patocka 			len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
11515ae89a87SMike Snitzer 
11521dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, num_bios, &len);
11535ae89a87SMike Snitzer 
1154a79245b3SMike Snitzer 		ci->sector += len;
1155a79245b3SMike Snitzer 	} while (ci->sector_count -= len);
11565ae89a87SMike Snitzer 
11575ae89a87SMike Snitzer 	return 0;
11585ae89a87SMike Snitzer }
11595ae89a87SMike Snitzer 
116014fe594dSAlasdair G Kergon static int __send_discard(struct clone_info *ci)
116123508a96SMike Snitzer {
116214fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_discard_bios,
116323508a96SMike Snitzer 					   is_split_required_for_discard);
116423508a96SMike Snitzer }
116523508a96SMike Snitzer 
116614fe594dSAlasdair G Kergon static int __send_write_same(struct clone_info *ci)
116723508a96SMike Snitzer {
116814fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
116923508a96SMike Snitzer }
117023508a96SMike Snitzer 
1171e4c93811SAlasdair G Kergon /*
1172e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1173e4c93811SAlasdair G Kergon  */
1174e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci)
1175e4c93811SAlasdair G Kergon {
1176e4c93811SAlasdair G Kergon 	struct bio *bio = ci->bio;
1177e4c93811SAlasdair G Kergon 	struct dm_target *ti;
11781c3b13e6SKent Overstreet 	unsigned len;
1179c80914e8SMike Snitzer 	int r;
1180e4c93811SAlasdair G Kergon 
1181e6047149SMike Christie 	if (unlikely(bio_op(bio) == REQ_OP_DISCARD))
1182e4c93811SAlasdair G Kergon 		return __send_discard(ci);
1183e6047149SMike Christie 	else if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
1184e4c93811SAlasdair G Kergon 		return __send_write_same(ci);
1185e4c93811SAlasdair G Kergon 
1186e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1187e4c93811SAlasdair G Kergon 	if (!dm_target_is_valid(ti))
1188e4c93811SAlasdair G Kergon 		return -EIO;
1189e4c93811SAlasdair G Kergon 
11901c3b13e6SKent Overstreet 	len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
1191e4c93811SAlasdair G Kergon 
1192c80914e8SMike Snitzer 	r = __clone_and_map_data_bio(ci, ti, ci->sector, &len);
1193c80914e8SMike Snitzer 	if (r < 0)
1194c80914e8SMike Snitzer 		return r;
1195e4c93811SAlasdair G Kergon 
1196e4c93811SAlasdair G Kergon 	ci->sector += len;
1197e4c93811SAlasdair G Kergon 	ci->sector_count -= len;
1198e4c93811SAlasdair G Kergon 
1199e4c93811SAlasdair G Kergon 	return 0;
1200e4c93811SAlasdair G Kergon }
1201e4c93811SAlasdair G Kergon 
1202e4c93811SAlasdair G Kergon /*
120314fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
12041da177e4SLinus Torvalds  */
120583d5e5b0SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md,
120683d5e5b0SMikulas Patocka 				    struct dm_table *map, struct bio *bio)
12071da177e4SLinus Torvalds {
12081da177e4SLinus Torvalds 	struct clone_info ci;
1209512875bdSJun'ichi Nomura 	int error = 0;
12101da177e4SLinus Torvalds 
121183d5e5b0SMikulas Patocka 	if (unlikely(!map)) {
1212f0b9a450SMikulas Patocka 		bio_io_error(bio);
1213f0b9a450SMikulas Patocka 		return;
1214f0b9a450SMikulas Patocka 	}
1215692d0eb9SMikulas Patocka 
121683d5e5b0SMikulas Patocka 	ci.map = map;
12171da177e4SLinus Torvalds 	ci.md = md;
12181da177e4SLinus Torvalds 	ci.io = alloc_io(md);
12191da177e4SLinus Torvalds 	ci.io->error = 0;
12201da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
12211da177e4SLinus Torvalds 	ci.io->bio = bio;
12221da177e4SLinus Torvalds 	ci.io->md = md;
1223f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
12244f024f37SKent Overstreet 	ci.sector = bio->bi_iter.bi_sector;
12251da177e4SLinus Torvalds 
12263eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1227bd2a49b8SAlasdair G Kergon 
122828a8f0d3SMike Christie 	if (bio->bi_rw & REQ_PREFLUSH) {
1229b372d360SMike Snitzer 		ci.bio = &ci.md->flush_bio;
1230b372d360SMike Snitzer 		ci.sector_count = 0;
123114fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1232b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1233b372d360SMike Snitzer 	} else {
12346a8736d1STejun Heo 		ci.bio = bio;
1235f6fccb12SMilan Broz 		ci.sector_count = bio_sectors(bio);
1236512875bdSJun'ichi Nomura 		while (ci.sector_count && !error)
123714fe594dSAlasdair G Kergon 			error = __split_and_process_non_flush(&ci);
1238d87f4c14STejun Heo 	}
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 	/* drop the extra reference count */
1241512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
12429e4e5f87SMilan Broz }
12439e4e5f87SMilan Broz /*-----------------------------------------------------------------
12441da177e4SLinus Torvalds  * CRUD END
12451da177e4SLinus Torvalds  *---------------------------------------------------------------*/
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds /*
12481da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
12491da177e4SLinus Torvalds  * dm_merge_bvec.
12501da177e4SLinus Torvalds  */
1251dece1635SJens Axboe static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
12521da177e4SLinus Torvalds {
125312f03a49SKevin Corry 	int rw = bio_data_dir(bio);
12541da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
125583d5e5b0SMikulas Patocka 	int srcu_idx;
125683d5e5b0SMikulas Patocka 	struct dm_table *map;
12571da177e4SLinus Torvalds 
125883d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
12591da177e4SLinus Torvalds 
126018c0b223SGu Zheng 	generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
126112f03a49SKevin Corry 
12626a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
12636a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
126483d5e5b0SMikulas Patocka 		dm_put_live_table(md, srcu_idx);
12651da177e4SLinus Torvalds 
12666a8736d1STejun Heo 		if (bio_rw(bio) != READA)
126792c63902SMikulas Patocka 			queue_io(md, bio);
12686a8736d1STejun Heo 		else
12696a8736d1STejun Heo 			bio_io_error(bio);
1270dece1635SJens Axboe 		return BLK_QC_T_NONE;
12711da177e4SLinus Torvalds 	}
12721da177e4SLinus Torvalds 
127383d5e5b0SMikulas Patocka 	__split_and_process_bio(md, map, bio);
127483d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
1275dece1635SJens Axboe 	return BLK_QC_T_NONE;
1276cec47e3dSKiyoshi Ueda }
1277cec47e3dSKiyoshi Ueda 
12781da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
12791da177e4SLinus Torvalds {
12808a57dfc6SChandra Seetharaman 	int r = bdi_bits;
12818a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
12828a57dfc6SChandra Seetharaman 	struct dm_table *map;
12831da177e4SLinus Torvalds 
12841eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
1285e522c039SMike Snitzer 		if (dm_request_based(md)) {
1286cec47e3dSKiyoshi Ueda 			/*
1287e522c039SMike Snitzer 			 * With request-based DM we only need to check the
1288e522c039SMike Snitzer 			 * top-level queue for congestion.
1289cec47e3dSKiyoshi Ueda 			 */
1290e522c039SMike Snitzer 			r = md->queue->backing_dev_info.wb.state & bdi_bits;
1291e522c039SMike Snitzer 		} else {
1292e522c039SMike Snitzer 			map = dm_get_live_table_fast(md);
1293e522c039SMike Snitzer 			if (map)
12941da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
129583d5e5b0SMikulas Patocka 			dm_put_live_table_fast(md);
12968a57dfc6SChandra Seetharaman 		}
1297e522c039SMike Snitzer 	}
12988a57dfc6SChandra Seetharaman 
12991da177e4SLinus Torvalds 	return r;
13001da177e4SLinus Torvalds }
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds /*-----------------------------------------------------------------
13031da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
13041da177e4SLinus Torvalds  *---------------------------------------------------------------*/
13052b06cfffSAlasdair G Kergon static void free_minor(int minor)
13061da177e4SLinus Torvalds {
1307f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
13081da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1309f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
13101da177e4SLinus Torvalds }
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds /*
13131da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
13141da177e4SLinus Torvalds  */
1315cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
13161da177e4SLinus Torvalds {
1317c9d76be6STejun Heo 	int r;
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
13201da177e4SLinus Torvalds 		return -EINVAL;
13211da177e4SLinus Torvalds 
1322c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1323f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
13241da177e4SLinus Torvalds 
1325c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
13261da177e4SLinus Torvalds 
1327f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1328c9d76be6STejun Heo 	idr_preload_end();
1329c9d76be6STejun Heo 	if (r < 0)
1330c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
1331c9d76be6STejun Heo 	return 0;
13321da177e4SLinus Torvalds }
13331da177e4SLinus Torvalds 
1334cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
13351da177e4SLinus Torvalds {
1336c9d76be6STejun Heo 	int r;
13371da177e4SLinus Torvalds 
1338c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1339f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
13401da177e4SLinus Torvalds 
1341c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
13421da177e4SLinus Torvalds 
1343f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1344c9d76be6STejun Heo 	idr_preload_end();
1345c9d76be6STejun Heo 	if (r < 0)
13461da177e4SLinus Torvalds 		return r;
1347c9d76be6STejun Heo 	*minor = r;
1348c9d76be6STejun Heo 	return 0;
13491da177e4SLinus Torvalds }
13501da177e4SLinus Torvalds 
135183d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
13521da177e4SLinus Torvalds 
135353d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
135453d5914fSMikulas Patocka 
13554cc96131SMike Snitzer void dm_init_md_queue(struct mapped_device *md)
13564a0b4ddfSMike Snitzer {
13574a0b4ddfSMike Snitzer 	/*
13584a0b4ddfSMike Snitzer 	 * Request-based dm devices cannot be stacked on top of bio-based dm
1359bfebd1cdSMike Snitzer 	 * devices.  The type of this dm device may not have been decided yet.
13604a0b4ddfSMike Snitzer 	 * The type is decided at the first table loading time.
13614a0b4ddfSMike Snitzer 	 * To prevent problematic device stacking, clear the queue flag
13624a0b4ddfSMike Snitzer 	 * for request stacking support until then.
13634a0b4ddfSMike Snitzer 	 *
13644a0b4ddfSMike Snitzer 	 * This queue is new, so no concurrency on the queue_flags.
13654a0b4ddfSMike Snitzer 	 */
13664a0b4ddfSMike Snitzer 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1367ad5f498fSMikulas Patocka 
1368ad5f498fSMikulas Patocka 	/*
1369ad5f498fSMikulas Patocka 	 * Initialize data that will only be used by a non-blk-mq DM queue
1370ad5f498fSMikulas Patocka 	 * - must do so here (in alloc_dev callchain) before queue is used
1371ad5f498fSMikulas Patocka 	 */
1372ad5f498fSMikulas Patocka 	md->queue->queuedata = md;
1373ad5f498fSMikulas Patocka 	md->queue->backing_dev_info.congested_data = md;
1374bfebd1cdSMike Snitzer }
13754a0b4ddfSMike Snitzer 
13764cc96131SMike Snitzer void dm_init_normal_md_queue(struct mapped_device *md)
1377bfebd1cdSMike Snitzer {
137817e149b8SMike Snitzer 	md->use_blk_mq = false;
1379bfebd1cdSMike Snitzer 	dm_init_md_queue(md);
1380bfebd1cdSMike Snitzer 
1381bfebd1cdSMike Snitzer 	/*
1382bfebd1cdSMike Snitzer 	 * Initialize aspects of queue that aren't relevant for blk-mq
1383bfebd1cdSMike Snitzer 	 */
13844a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
13854a0b4ddfSMike Snitzer 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
13864a0b4ddfSMike Snitzer }
13874a0b4ddfSMike Snitzer 
13880f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md)
13890f20972fSMike Snitzer {
13900f20972fSMike Snitzer 	if (md->wq)
13910f20972fSMike Snitzer 		destroy_workqueue(md->wq);
13920f20972fSMike Snitzer 	if (md->kworker_task)
13930f20972fSMike Snitzer 		kthread_stop(md->kworker_task);
13940f20972fSMike Snitzer 	mempool_destroy(md->io_pool);
13950f20972fSMike Snitzer 	mempool_destroy(md->rq_pool);
13960f20972fSMike Snitzer 	if (md->bs)
13970f20972fSMike Snitzer 		bioset_free(md->bs);
13980f20972fSMike Snitzer 
1399b06075a9SMikulas Patocka 	cleanup_srcu_struct(&md->io_barrier);
1400b06075a9SMikulas Patocka 
14010f20972fSMike Snitzer 	if (md->disk) {
14020f20972fSMike Snitzer 		spin_lock(&_minor_lock);
14030f20972fSMike Snitzer 		md->disk->private_data = NULL;
14040f20972fSMike Snitzer 		spin_unlock(&_minor_lock);
14050f20972fSMike Snitzer 		del_gendisk(md->disk);
14060f20972fSMike Snitzer 		put_disk(md->disk);
14070f20972fSMike Snitzer 	}
14080f20972fSMike Snitzer 
14090f20972fSMike Snitzer 	if (md->queue)
14100f20972fSMike Snitzer 		blk_cleanup_queue(md->queue);
14110f20972fSMike Snitzer 
14120f20972fSMike Snitzer 	if (md->bdev) {
14130f20972fSMike Snitzer 		bdput(md->bdev);
14140f20972fSMike Snitzer 		md->bdev = NULL;
14150f20972fSMike Snitzer 	}
14164cc96131SMike Snitzer 
14174cc96131SMike Snitzer 	dm_mq_cleanup_mapped_device(md);
14180f20972fSMike Snitzer }
14190f20972fSMike Snitzer 
14201da177e4SLinus Torvalds /*
14211da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
14221da177e4SLinus Torvalds  */
14232b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
14241da177e4SLinus Torvalds {
1425115485e8SMike Snitzer 	int r, numa_node_id = dm_get_numa_node();
1426115485e8SMike Snitzer 	struct mapped_device *md;
1427ba61fdd1SJeff Mahoney 	void *old_md;
14281da177e4SLinus Torvalds 
1429115485e8SMike Snitzer 	md = kzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
14301da177e4SLinus Torvalds 	if (!md) {
14311da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
14321da177e4SLinus Torvalds 		return NULL;
14331da177e4SLinus Torvalds 	}
14341da177e4SLinus Torvalds 
143510da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
14366ed7ade8SMilan Broz 		goto bad_module_get;
143710da4f79SJeff Mahoney 
14381da177e4SLinus Torvalds 	/* get a minor number for the dev */
14392b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1440cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
14412b06cfffSAlasdair G Kergon 	else
1442cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
14431da177e4SLinus Torvalds 	if (r < 0)
14446ed7ade8SMilan Broz 		goto bad_minor;
14451da177e4SLinus Torvalds 
144683d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
144783d5e5b0SMikulas Patocka 	if (r < 0)
144883d5e5b0SMikulas Patocka 		goto bad_io_barrier;
144983d5e5b0SMikulas Patocka 
1450115485e8SMike Snitzer 	md->numa_node_id = numa_node_id;
14514cc96131SMike Snitzer 	md->use_blk_mq = dm_use_blk_mq_default();
1452591ddcfcSMike Snitzer 	md->init_tio_pdu = false;
1453a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
1454e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1455a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
145686f1152bSBenjamin Marzinski 	mutex_init(&md->table_devices_lock);
1457022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
14581da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
14595c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
14601da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
14617a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
14627a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
146386f1152bSBenjamin Marzinski 	INIT_LIST_HEAD(&md->table_devices);
14647a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
14651da177e4SLinus Torvalds 
1466115485e8SMike Snitzer 	md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id);
14671da177e4SLinus Torvalds 	if (!md->queue)
14680f20972fSMike Snitzer 		goto bad;
14691da177e4SLinus Torvalds 
14704a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
14719faf400fSStefan Bader 
1472115485e8SMike Snitzer 	md->disk = alloc_disk_node(1, numa_node_id);
14731da177e4SLinus Torvalds 	if (!md->disk)
14740f20972fSMike Snitzer 		goto bad;
14751da177e4SLinus Torvalds 
1476316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
1477316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
1478f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
147953d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1480f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
14812995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
14822eb6e1e3SKeith Busch 	md->kworker_task = NULL;
1483f0b04115SJeff Mahoney 
14841da177e4SLinus Torvalds 	md->disk->major = _major;
14851da177e4SLinus Torvalds 	md->disk->first_minor = minor;
14861da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
14871da177e4SLinus Torvalds 	md->disk->queue = md->queue;
14881da177e4SLinus Torvalds 	md->disk->private_data = md;
14891da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
14901da177e4SLinus Torvalds 	add_disk(md->disk);
14917e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
14921da177e4SLinus Torvalds 
1493670368a8STejun Heo 	md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
1494304f3f6aSMilan Broz 	if (!md->wq)
14950f20972fSMike Snitzer 		goto bad;
1496304f3f6aSMilan Broz 
149732a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
149832a926daSMikulas Patocka 	if (!md->bdev)
14990f20972fSMike Snitzer 		goto bad;
150032a926daSMikulas Patocka 
15016a8736d1STejun Heo 	bio_init(&md->flush_bio);
15026a8736d1STejun Heo 	md->flush_bio.bi_bdev = md->bdev;
1503e6047149SMike Christie 	bio_set_op_attrs(&md->flush_bio, REQ_OP_WRITE, WRITE_FLUSH);
15046a8736d1STejun Heo 
1505fd2ed4d2SMikulas Patocka 	dm_stats_init(&md->stats);
1506fd2ed4d2SMikulas Patocka 
1507ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1508f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1509ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1510f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1511ba61fdd1SJeff Mahoney 
1512ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1513ba61fdd1SJeff Mahoney 
15141da177e4SLinus Torvalds 	return md;
15151da177e4SLinus Torvalds 
15160f20972fSMike Snitzer bad:
15170f20972fSMike Snitzer 	cleanup_mapped_device(md);
151883d5e5b0SMikulas Patocka bad_io_barrier:
15191da177e4SLinus Torvalds 	free_minor(minor);
15206ed7ade8SMilan Broz bad_minor:
152110da4f79SJeff Mahoney 	module_put(THIS_MODULE);
15226ed7ade8SMilan Broz bad_module_get:
15231da177e4SLinus Torvalds 	kfree(md);
15241da177e4SLinus Torvalds 	return NULL;
15251da177e4SLinus Torvalds }
15261da177e4SLinus Torvalds 
1527ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1528ae9da83fSJun'ichi Nomura 
15291da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
15301da177e4SLinus Torvalds {
1531f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
153263d94e48SJun'ichi Nomura 
1533ae9da83fSJun'ichi Nomura 	unlock_fs(md);
15342eb6e1e3SKeith Busch 
15350f20972fSMike Snitzer 	cleanup_mapped_device(md);
15360f20972fSMike Snitzer 
15370f20972fSMike Snitzer 	free_table_devices(&md->table_devices);
15380f20972fSMike Snitzer 	dm_stats_cleanup(&md->stats);
153963a4f065SMike Snitzer 	free_minor(minor);
154063a4f065SMike Snitzer 
154110da4f79SJeff Mahoney 	module_put(THIS_MODULE);
15421da177e4SLinus Torvalds 	kfree(md);
15431da177e4SLinus Torvalds }
15441da177e4SLinus Torvalds 
1545e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1546e6ee8c0bSKiyoshi Ueda {
1547c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
1548e6ee8c0bSKiyoshi Ueda 
15494e6e36c3SMike Snitzer 	if (md->bs) {
15504e6e36c3SMike Snitzer 		/* The md already has necessary mempools. */
15514e6e36c3SMike Snitzer 		if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
1552c0820cf5SMikulas Patocka 			/*
155316245bdcSJun'ichi Nomura 			 * Reload bioset because front_pad may have changed
155416245bdcSJun'ichi Nomura 			 * because a different table was loaded.
1555c0820cf5SMikulas Patocka 			 */
1556c0820cf5SMikulas Patocka 			bioset_free(md->bs);
1557c0820cf5SMikulas Patocka 			md->bs = p->bs;
1558c0820cf5SMikulas Patocka 			p->bs = NULL;
1559c0820cf5SMikulas Patocka 		}
1560cbc4e3c1SMike Snitzer 		/*
15614e6e36c3SMike Snitzer 		 * There's no need to reload with request-based dm
15624e6e36c3SMike Snitzer 		 * because the size of front_pad doesn't change.
15634e6e36c3SMike Snitzer 		 * Note for future: If you are to reload bioset,
15644e6e36c3SMike Snitzer 		 * prep-ed requests in the queue may refer
15654e6e36c3SMike Snitzer 		 * to bio from the old bioset, so you must walk
15664e6e36c3SMike Snitzer 		 * through the queue to unprep.
1567cbc4e3c1SMike Snitzer 		 */
1568cbc4e3c1SMike Snitzer 		goto out;
1569cbc4e3c1SMike Snitzer 	}
1570cbc4e3c1SMike Snitzer 
1571cbc4e3c1SMike Snitzer 	BUG_ON(!p || md->io_pool || md->rq_pool || md->bs);
1572e6ee8c0bSKiyoshi Ueda 
1573e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
1574e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
15751ae49ea2SMike Snitzer 	md->rq_pool = p->rq_pool;
15761ae49ea2SMike Snitzer 	p->rq_pool = NULL;
1577e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
1578e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
15794e6e36c3SMike Snitzer 
1580e6ee8c0bSKiyoshi Ueda out:
158102233342SMike Snitzer 	/* mempool bind completed, no longer need any mempools in the table */
1582e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
1583e6ee8c0bSKiyoshi Ueda }
1584e6ee8c0bSKiyoshi Ueda 
15851da177e4SLinus Torvalds /*
15861da177e4SLinus Torvalds  * Bind a table to the device.
15871da177e4SLinus Torvalds  */
15881da177e4SLinus Torvalds static void event_callback(void *context)
15891da177e4SLinus Torvalds {
15907a8c3d3bSMike Anderson 	unsigned long flags;
15917a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
15921da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
15931da177e4SLinus Torvalds 
15947a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
15957a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
15967a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
15977a8c3d3bSMike Anderson 
1598ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
15997a8c3d3bSMike Anderson 
16001da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
16011da177e4SLinus Torvalds 	wake_up(&md->eventq);
16021da177e4SLinus Torvalds }
16031da177e4SLinus Torvalds 
1604c217649bSMike Snitzer /*
1605c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
1606c217649bSMike Snitzer  */
16074e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
16081da177e4SLinus Torvalds {
16094e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
16101da177e4SLinus Torvalds 
1611db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
16121da177e4SLinus Torvalds }
16131da177e4SLinus Torvalds 
1614042d2a9bSAlasdair G Kergon /*
1615042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
1616042d2a9bSAlasdair G Kergon  */
1617042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
1618754c5fc7SMike Snitzer 			       struct queue_limits *limits)
16191da177e4SLinus Torvalds {
1620042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
1621165125e1SJens Axboe 	struct request_queue *q = md->queue;
16221da177e4SLinus Torvalds 	sector_t size;
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 	size = dm_table_get_size(t);
16253ac51e74SDarrick J. Wong 
16263ac51e74SDarrick J. Wong 	/*
16273ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
16283ac51e74SDarrick J. Wong 	 */
1629fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
16303ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
16313ac51e74SDarrick J. Wong 
16324e90188bSAlasdair G Kergon 	__set_size(md, size);
16331da177e4SLinus Torvalds 
1634cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
16352ca3310eSAlasdair G Kergon 
1636e6ee8c0bSKiyoshi Ueda 	/*
1637e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
1638e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
1639e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
1640e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
1641e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
1642e6ee8c0bSKiyoshi Ueda 	 */
164316f12266SMike Snitzer 	if (dm_table_request_based(t)) {
1644eca7ee6dSMike Snitzer 		dm_stop_queue(q);
164516f12266SMike Snitzer 		/*
164616f12266SMike Snitzer 		 * Leverage the fact that request-based DM targets are
164716f12266SMike Snitzer 		 * immutable singletons and establish md->immutable_target
164816f12266SMike Snitzer 		 * - used to optimize both dm_request_fn and dm_mq_queue_rq
164916f12266SMike Snitzer 		 */
165016f12266SMike Snitzer 		md->immutable_target = dm_table_get_immutable_target(t);
165116f12266SMike Snitzer 	}
1652e6ee8c0bSKiyoshi Ueda 
1653e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
1654e6ee8c0bSKiyoshi Ueda 
1655a12f5d48SEric Dumazet 	old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
16561d3aa6f6SMike Snitzer 	rcu_assign_pointer(md->map, (void *)t);
165736a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
165836a0456fSAlasdair G Kergon 
1659754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
166041abc4e1SHannes Reinecke 	if (old_map)
166183d5e5b0SMikulas Patocka 		dm_sync_table(md);
16622ca3310eSAlasdair G Kergon 
1663042d2a9bSAlasdair G Kergon 	return old_map;
16641da177e4SLinus Torvalds }
16651da177e4SLinus Torvalds 
1666a7940155SAlasdair G Kergon /*
1667a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
1668a7940155SAlasdair G Kergon  */
1669a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
16701da177e4SLinus Torvalds {
1671a12f5d48SEric Dumazet 	struct dm_table *map = rcu_dereference_protected(md->map, 1);
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 	if (!map)
1674a7940155SAlasdair G Kergon 		return NULL;
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
16779cdb8520SMonam Agarwal 	RCU_INIT_POINTER(md->map, NULL);
167883d5e5b0SMikulas Patocka 	dm_sync_table(md);
1679a7940155SAlasdair G Kergon 
1680a7940155SAlasdair G Kergon 	return map;
16811da177e4SLinus Torvalds }
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds /*
16841da177e4SLinus Torvalds  * Constructor for a new device.
16851da177e4SLinus Torvalds  */
16862b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
16871da177e4SLinus Torvalds {
16881da177e4SLinus Torvalds 	struct mapped_device *md;
16891da177e4SLinus Torvalds 
16902b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
16911da177e4SLinus Torvalds 	if (!md)
16921da177e4SLinus Torvalds 		return -ENXIO;
16931da177e4SLinus Torvalds 
1694784aae73SMilan Broz 	dm_sysfs_init(md);
1695784aae73SMilan Broz 
16961da177e4SLinus Torvalds 	*result = md;
16971da177e4SLinus Torvalds 	return 0;
16981da177e4SLinus Torvalds }
16991da177e4SLinus Torvalds 
1700a5664dadSMike Snitzer /*
1701a5664dadSMike Snitzer  * Functions to manage md->type.
1702a5664dadSMike Snitzer  * All are required to hold md->type_lock.
1703a5664dadSMike Snitzer  */
1704a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
1705a5664dadSMike Snitzer {
1706a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
1707a5664dadSMike Snitzer }
1708a5664dadSMike Snitzer 
1709a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
1710a5664dadSMike Snitzer {
1711a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
1712a5664dadSMike Snitzer }
1713a5664dadSMike Snitzer 
1714a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type)
1715a5664dadSMike Snitzer {
171600c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
1717a5664dadSMike Snitzer 	md->type = type;
1718a5664dadSMike Snitzer }
1719a5664dadSMike Snitzer 
1720a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md)
1721a5664dadSMike Snitzer {
1722a5664dadSMike Snitzer 	return md->type;
1723a5664dadSMike Snitzer }
1724a5664dadSMike Snitzer 
172536a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
172636a0456fSAlasdair G Kergon {
172736a0456fSAlasdair G Kergon 	return md->immutable_target_type;
172836a0456fSAlasdair G Kergon }
172936a0456fSAlasdair G Kergon 
17304a0b4ddfSMike Snitzer /*
1731f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
1732f84cb8a4SMike Snitzer  * count on 'md'.
1733f84cb8a4SMike Snitzer  */
1734f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
1735f84cb8a4SMike Snitzer {
1736f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
1737f84cb8a4SMike Snitzer 	return &md->queue->limits;
1738f84cb8a4SMike Snitzer }
1739f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
1740f84cb8a4SMike Snitzer 
17414a0b4ddfSMike Snitzer /*
17424a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
17434a0b4ddfSMike Snitzer  */
1744591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
17454a0b4ddfSMike Snitzer {
1746bfebd1cdSMike Snitzer 	int r;
1747bfebd1cdSMike Snitzer 
1748*e83068a5SMike Snitzer 	switch (dm_get_md_type(md)) {
1749bfebd1cdSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
1750eca7ee6dSMike Snitzer 		r = dm_old_init_request_queue(md);
1751bfebd1cdSMike Snitzer 		if (r) {
1752eca7ee6dSMike Snitzer 			DMERR("Cannot initialize queue for request-based mapped device");
1753bfebd1cdSMike Snitzer 			return r;
17544a0b4ddfSMike Snitzer 		}
1755bfebd1cdSMike Snitzer 		break;
1756bfebd1cdSMike Snitzer 	case DM_TYPE_MQ_REQUEST_BASED:
1757*e83068a5SMike Snitzer 		r = dm_mq_init_request_queue(md, t);
1758bfebd1cdSMike Snitzer 		if (r) {
1759eca7ee6dSMike Snitzer 			DMERR("Cannot initialize queue for request-based dm-mq mapped device");
1760bfebd1cdSMike Snitzer 			return r;
1761bfebd1cdSMike Snitzer 		}
1762bfebd1cdSMike Snitzer 		break;
1763bfebd1cdSMike Snitzer 	case DM_TYPE_BIO_BASED:
1764eca7ee6dSMike Snitzer 		dm_init_normal_md_queue(md);
1765ff36ab34SMike Snitzer 		blk_queue_make_request(md->queue, dm_make_request);
1766dbba42d8SMikulas Patocka 		/*
1767dbba42d8SMikulas Patocka 		 * DM handles splitting bios as needed.  Free the bio_split bioset
1768dbba42d8SMikulas Patocka 		 * since it won't be used (saves 1 process per bio-based DM device).
1769dbba42d8SMikulas Patocka 		 */
1770dbba42d8SMikulas Patocka 		bioset_free(md->queue->bio_split);
1771dbba42d8SMikulas Patocka 		md->queue->bio_split = NULL;
1772bfebd1cdSMike Snitzer 		break;
1773ff36ab34SMike Snitzer 	}
17744a0b4ddfSMike Snitzer 
17754a0b4ddfSMike Snitzer 	return 0;
17764a0b4ddfSMike Snitzer }
17774a0b4ddfSMike Snitzer 
17782bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev)
17791da177e4SLinus Torvalds {
17801da177e4SLinus Torvalds 	struct mapped_device *md;
17811da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
17841da177e4SLinus Torvalds 		return NULL;
17851da177e4SLinus Torvalds 
1786f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
17892bec1f4aSMikulas Patocka 	if (md) {
17902bec1f4aSMikulas Patocka 		if ((md == MINOR_ALLOCED ||
1791f331c029STejun Heo 		     (MINOR(disk_devt(dm_disk(md))) != minor) ||
1792abdc568bSKiyoshi Ueda 		     dm_deleting_md(md) ||
1793fba9f90eSJeff Mahoney 		     test_bit(DMF_FREEING, &md->flags))) {
1794637842cfSDavid Teigland 			md = NULL;
1795fba9f90eSJeff Mahoney 			goto out;
1796fba9f90eSJeff Mahoney 		}
17972bec1f4aSMikulas Patocka 		dm_get(md);
17982bec1f4aSMikulas Patocka 	}
17991da177e4SLinus Torvalds 
1800fba9f90eSJeff Mahoney out:
1801f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
18021da177e4SLinus Torvalds 
1803637842cfSDavid Teigland 	return md;
1804637842cfSDavid Teigland }
18053cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
1806d229a958SDavid Teigland 
18079ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
1808637842cfSDavid Teigland {
18099ade92a9SAlasdair G Kergon 	return md->interface_ptr;
18101da177e4SLinus Torvalds }
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
18131da177e4SLinus Torvalds {
18141da177e4SLinus Torvalds 	md->interface_ptr = ptr;
18151da177e4SLinus Torvalds }
18161da177e4SLinus Torvalds 
18171da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
18181da177e4SLinus Torvalds {
18191da177e4SLinus Torvalds 	atomic_inc(&md->holders);
18203f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
18211da177e4SLinus Torvalds }
18221da177e4SLinus Torvalds 
182309ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md)
182409ee96b2SMikulas Patocka {
182509ee96b2SMikulas Patocka 	spin_lock(&_minor_lock);
182609ee96b2SMikulas Patocka 	if (test_bit(DMF_FREEING, &md->flags)) {
182709ee96b2SMikulas Patocka 		spin_unlock(&_minor_lock);
182809ee96b2SMikulas Patocka 		return -EBUSY;
182909ee96b2SMikulas Patocka 	}
183009ee96b2SMikulas Patocka 	dm_get(md);
183109ee96b2SMikulas Patocka 	spin_unlock(&_minor_lock);
183209ee96b2SMikulas Patocka 	return 0;
183309ee96b2SMikulas Patocka }
183409ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold);
183509ee96b2SMikulas Patocka 
183672d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
183772d94861SAlasdair G Kergon {
183872d94861SAlasdair G Kergon 	return md->name;
183972d94861SAlasdair G Kergon }
184072d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
184172d94861SAlasdair G Kergon 
18423f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
18431da177e4SLinus Torvalds {
18441134e5aeSMike Anderson 	struct dm_table *map;
184583d5e5b0SMikulas Patocka 	int srcu_idx;
18461da177e4SLinus Torvalds 
18473f77316dSKiyoshi Ueda 	might_sleep();
1848fba9f90eSJeff Mahoney 
184963a4f065SMike Snitzer 	spin_lock(&_minor_lock);
18503f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
1851fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
1852f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
18533f77316dSKiyoshi Ueda 
185402233342SMike Snitzer 	if (dm_request_based(md) && md->kworker_task)
18552eb6e1e3SKeith Busch 		flush_kthread_worker(&md->kworker);
18562eb6e1e3SKeith Busch 
1857ab7c7bb6SMikulas Patocka 	/*
1858ab7c7bb6SMikulas Patocka 	 * Take suspend_lock so that presuspend and postsuspend methods
1859ab7c7bb6SMikulas Patocka 	 * do not race with internal suspend.
1860ab7c7bb6SMikulas Patocka 	 */
1861ab7c7bb6SMikulas Patocka 	mutex_lock(&md->suspend_lock);
18622a708cffSJunichi Nomura 	map = dm_get_live_table(md, &srcu_idx);
18634f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
18641da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
18651da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
18661da177e4SLinus Torvalds 	}
186783d5e5b0SMikulas Patocka 	/* dm_put_live_table must be before msleep, otherwise deadlock is possible */
186883d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
18692a708cffSJunichi Nomura 	mutex_unlock(&md->suspend_lock);
187083d5e5b0SMikulas Patocka 
18713f77316dSKiyoshi Ueda 	/*
18723f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
18733f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
18743f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
18753f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
18763f77316dSKiyoshi Ueda 	 */
18773f77316dSKiyoshi Ueda 	if (wait)
18783f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
18793f77316dSKiyoshi Ueda 			msleep(1);
18803f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
18813f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
18823f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
18833f77316dSKiyoshi Ueda 
1884784aae73SMilan Broz 	dm_sysfs_exit(md);
1885a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
18861da177e4SLinus Torvalds 	free_dev(md);
18871da177e4SLinus Torvalds }
18883f77316dSKiyoshi Ueda 
18893f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
18903f77316dSKiyoshi Ueda {
18913f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
18923f77316dSKiyoshi Ueda }
18933f77316dSKiyoshi Ueda 
18943f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
18953f77316dSKiyoshi Ueda {
18963f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
18973f77316dSKiyoshi Ueda }
18983f77316dSKiyoshi Ueda 
18993f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
19003f77316dSKiyoshi Ueda {
19013f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
19021da177e4SLinus Torvalds }
190379eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
19041da177e4SLinus Torvalds 
1905401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
190646125c1cSMilan Broz {
190746125c1cSMilan Broz 	int r = 0;
1908b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
1909b44ebeb0SMikulas Patocka 
1910b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
191146125c1cSMilan Broz 
191246125c1cSMilan Broz 	while (1) {
1913401600dfSMikulas Patocka 		set_current_state(interruptible);
191446125c1cSMilan Broz 
1915b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
191646125c1cSMilan Broz 			break;
191746125c1cSMilan Broz 
1918401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
1919401600dfSMikulas Patocka 		    signal_pending(current)) {
192046125c1cSMilan Broz 			r = -EINTR;
192146125c1cSMilan Broz 			break;
192246125c1cSMilan Broz 		}
192346125c1cSMilan Broz 
192446125c1cSMilan Broz 		io_schedule();
192546125c1cSMilan Broz 	}
192646125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
192746125c1cSMilan Broz 
1928b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
1929b44ebeb0SMikulas Patocka 
193046125c1cSMilan Broz 	return r;
193146125c1cSMilan Broz }
193246125c1cSMilan Broz 
19331da177e4SLinus Torvalds /*
19341da177e4SLinus Torvalds  * Process the deferred bios
19351da177e4SLinus Torvalds  */
1936ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
19371da177e4SLinus Torvalds {
1938ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
1939ef208587SMikulas Patocka 						work);
19406d6f10dfSMilan Broz 	struct bio *c;
194183d5e5b0SMikulas Patocka 	int srcu_idx;
194283d5e5b0SMikulas Patocka 	struct dm_table *map;
19431da177e4SLinus Torvalds 
194483d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
1945ef208587SMikulas Patocka 
19463b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
1947022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
1948022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
1949022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
1950022c2611SMikulas Patocka 
19516a8736d1STejun Heo 		if (!c)
1952df12ee99SAlasdair G Kergon 			break;
195373d410c0SMilan Broz 
1954e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
1955e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
1956af7e466aSMikulas Patocka 		else
195783d5e5b0SMikulas Patocka 			__split_and_process_bio(md, map, c);
1958e6ee8c0bSKiyoshi Ueda 	}
19593b00b203SMikulas Patocka 
196083d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
19611da177e4SLinus Torvalds }
19621da177e4SLinus Torvalds 
19639a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
1964304f3f6aSMilan Broz {
19653b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
19664e857c58SPeter Zijlstra 	smp_mb__after_atomic();
196753d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
1968304f3f6aSMilan Broz }
1969304f3f6aSMilan Broz 
19701da177e4SLinus Torvalds /*
1971042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
19721da177e4SLinus Torvalds  */
1973042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
19741da177e4SLinus Torvalds {
197587eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
1976754c5fc7SMike Snitzer 	struct queue_limits limits;
1977042d2a9bSAlasdair G Kergon 	int r;
19781da177e4SLinus Torvalds 
1979e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
19801da177e4SLinus Torvalds 
19811da177e4SLinus Torvalds 	/* device must be suspended */
19824f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
198393c534aeSAlasdair G Kergon 		goto out;
19841da177e4SLinus Torvalds 
19853ae70656SMike Snitzer 	/*
19863ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
19873ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
19883ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
19893ae70656SMike Snitzer 	 * reappear.
19903ae70656SMike Snitzer 	 */
19913ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
199283d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
19933ae70656SMike Snitzer 		if (live_map)
19943ae70656SMike Snitzer 			limits = md->queue->limits;
199583d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
19963ae70656SMike Snitzer 	}
19973ae70656SMike Snitzer 
199887eb5b21SMike Christie 	if (!live_map) {
1999754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2000042d2a9bSAlasdair G Kergon 		if (r) {
2001042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2002754c5fc7SMike Snitzer 			goto out;
2003042d2a9bSAlasdair G Kergon 		}
200487eb5b21SMike Christie 	}
2005754c5fc7SMike Snitzer 
2006042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
20071da177e4SLinus Torvalds 
200893c534aeSAlasdair G Kergon out:
2009e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2010042d2a9bSAlasdair G Kergon 	return map;
20111da177e4SLinus Torvalds }
20121da177e4SLinus Torvalds 
20131da177e4SLinus Torvalds /*
20141da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
20151da177e4SLinus Torvalds  * device.
20161da177e4SLinus Torvalds  */
20172ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
20181da177e4SLinus Torvalds {
2019e39e2e95SAlasdair G Kergon 	int r;
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2022dfbe03f6SAlasdair G Kergon 
2023db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2024dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2025cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2026e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2027e39e2e95SAlasdair G Kergon 		return r;
2028dfbe03f6SAlasdair G Kergon 	}
2029dfbe03f6SAlasdair G Kergon 
2030aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2031aa8d7c2fSAlasdair G Kergon 
20321da177e4SLinus Torvalds 	return 0;
20331da177e4SLinus Torvalds }
20341da177e4SLinus Torvalds 
20352ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
20361da177e4SLinus Torvalds {
2037aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2038aa8d7c2fSAlasdair G Kergon 		return;
2039aa8d7c2fSAlasdair G Kergon 
2040db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
20411da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2042aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
20431da177e4SLinus Torvalds }
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds /*
2046ffcc3936SMike Snitzer  * If __dm_suspend returns 0, the device is completely quiescent
2047ffcc3936SMike Snitzer  * now. There is no request-processing activity. All new requests
2048ffcc3936SMike Snitzer  * are being added to md->deferred list.
2049cec47e3dSKiyoshi Ueda  *
2050ffcc3936SMike Snitzer  * Caller must hold md->suspend_lock
2051cec47e3dSKiyoshi Ueda  */
2052ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
2053ffcc3936SMike Snitzer 			unsigned suspend_flags, int interruptible)
20541da177e4SLinus Torvalds {
2055ffcc3936SMike Snitzer 	bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2056ffcc3936SMike Snitzer 	bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2057ffcc3936SMike Snitzer 	int r;
2058cf222b37SAlasdair G Kergon 
20592e93ccc1SKiyoshi Ueda 	/*
20602e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
20612e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
20622e93ccc1SKiyoshi Ueda 	 */
20632e93ccc1SKiyoshi Ueda 	if (noflush)
20642e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
20652e93ccc1SKiyoshi Ueda 
2066d67ee213SMike Snitzer 	/*
2067d67ee213SMike Snitzer 	 * This gets reverted if there's an error later and the targets
2068d67ee213SMike Snitzer 	 * provide the .presuspend_undo hook.
2069d67ee213SMike Snitzer 	 */
20701da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
20711da177e4SLinus Torvalds 
20722e93ccc1SKiyoshi Ueda 	/*
20739f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
20749f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
20759f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
20769f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
20772e93ccc1SKiyoshi Ueda 	 */
207832a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
20792ca3310eSAlasdair G Kergon 		r = lock_fs(md);
2080d67ee213SMike Snitzer 		if (r) {
2081d67ee213SMike Snitzer 			dm_table_presuspend_undo_targets(map);
2082ffcc3936SMike Snitzer 			return r;
2083aa8d7c2fSAlasdair G Kergon 		}
2084d67ee213SMike Snitzer 	}
20851da177e4SLinus Torvalds 
20861da177e4SLinus Torvalds 	/*
20873b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
20883b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
20893b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
20903b00b203SMikulas Patocka 	 * dm_wq_work.
20913b00b203SMikulas Patocka 	 *
20923b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
20933b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
20946a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
20956a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
20966a8736d1STejun Heo 	 * flush_workqueue(md->wq).
20971da177e4SLinus Torvalds 	 */
20981eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
209941abc4e1SHannes Reinecke 	if (map)
210083d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
21011da177e4SLinus Torvalds 
2102d0bcb878SKiyoshi Ueda 	/*
210329e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
210429e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2105d0bcb878SKiyoshi Ueda 	 */
21062eb6e1e3SKeith Busch 	if (dm_request_based(md)) {
2107eca7ee6dSMike Snitzer 		dm_stop_queue(md->queue);
210802233342SMike Snitzer 		if (md->kworker_task)
21092eb6e1e3SKeith Busch 			flush_kthread_worker(&md->kworker);
21102eb6e1e3SKeith Busch 	}
2111cec47e3dSKiyoshi Ueda 
2112d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2113d0bcb878SKiyoshi Ueda 
21141da177e4SLinus Torvalds 	/*
21153b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
21163b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
21173b00b203SMikulas Patocka 	 * to finish.
21181da177e4SLinus Torvalds 	 */
2119ffcc3936SMike Snitzer 	r = dm_wait_for_completion(md, interruptible);
21201da177e4SLinus Torvalds 
21216d6f10dfSMilan Broz 	if (noflush)
2122022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
212341abc4e1SHannes Reinecke 	if (map)
212483d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
21252e93ccc1SKiyoshi Ueda 
21261da177e4SLinus Torvalds 	/* were we interrupted ? */
212746125c1cSMilan Broz 	if (r < 0) {
21289a1fb464SMikulas Patocka 		dm_queue_flush(md);
212973d410c0SMilan Broz 
2130cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
2131eca7ee6dSMike Snitzer 			dm_start_queue(md->queue);
2132cec47e3dSKiyoshi Ueda 
21332ca3310eSAlasdair G Kergon 		unlock_fs(md);
2134d67ee213SMike Snitzer 		dm_table_presuspend_undo_targets(map);
2135ffcc3936SMike Snitzer 		/* pushback list is already flushed, so skip flush */
2136ffcc3936SMike Snitzer 	}
2137ffcc3936SMike Snitzer 
2138ffcc3936SMike Snitzer 	return r;
21392ca3310eSAlasdair G Kergon }
21402ca3310eSAlasdair G Kergon 
21413b00b203SMikulas Patocka /*
2142ffcc3936SMike Snitzer  * We need to be able to change a mapping table under a mounted
2143ffcc3936SMike Snitzer  * filesystem.  For example we might want to move some data in
2144ffcc3936SMike Snitzer  * the background.  Before the table can be swapped with
2145ffcc3936SMike Snitzer  * dm_bind_table, dm_suspend must be called to flush any in
2146ffcc3936SMike Snitzer  * flight bios and ensure that any further io gets deferred.
21473b00b203SMikulas Patocka  */
2148ffcc3936SMike Snitzer /*
2149ffcc3936SMike Snitzer  * Suspend mechanism in request-based dm.
2150ffcc3936SMike Snitzer  *
2151ffcc3936SMike Snitzer  * 1. Flush all I/Os by lock_fs() if needed.
2152ffcc3936SMike Snitzer  * 2. Stop dispatching any I/O by stopping the request_queue.
2153ffcc3936SMike Snitzer  * 3. Wait for all in-flight I/Os to be completed or requeued.
2154ffcc3936SMike Snitzer  *
2155ffcc3936SMike Snitzer  * To abort suspend, start the request_queue.
2156ffcc3936SMike Snitzer  */
2157ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2158ffcc3936SMike Snitzer {
2159ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2160ffcc3936SMike Snitzer 	int r = 0;
2161ffcc3936SMike Snitzer 
2162ffcc3936SMike Snitzer retry:
2163ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2164ffcc3936SMike Snitzer 
2165ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2166ffcc3936SMike Snitzer 		r = -EINVAL;
2167ffcc3936SMike Snitzer 		goto out_unlock;
2168ffcc3936SMike Snitzer 	}
2169ffcc3936SMike Snitzer 
2170ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2171ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2172ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2173ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2174ffcc3936SMike Snitzer 		if (r)
2175ffcc3936SMike Snitzer 			return r;
2176ffcc3936SMike Snitzer 		goto retry;
2177ffcc3936SMike Snitzer 	}
2178ffcc3936SMike Snitzer 
2179a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2180ffcc3936SMike Snitzer 
2181ffcc3936SMike Snitzer 	r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE);
2182ffcc3936SMike Snitzer 	if (r)
2183ffcc3936SMike Snitzer 		goto out_unlock;
21843b00b203SMikulas Patocka 
21851da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
21861da177e4SLinus Torvalds 
21874d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
21884d4471cbSKiyoshi Ueda 
2189d287483dSAlasdair G Kergon out_unlock:
2190e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2191cf222b37SAlasdair G Kergon 	return r;
21921da177e4SLinus Torvalds }
21931da177e4SLinus Torvalds 
2194ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map)
21951da177e4SLinus Torvalds {
2196ffcc3936SMike Snitzer 	if (map) {
2197ffcc3936SMike Snitzer 		int r = dm_table_resume_targets(map);
21988757b776SMilan Broz 		if (r)
2199ffcc3936SMike Snitzer 			return r;
2200ffcc3936SMike Snitzer 	}
22012ca3310eSAlasdair G Kergon 
22029a1fb464SMikulas Patocka 	dm_queue_flush(md);
22032ca3310eSAlasdair G Kergon 
2204cec47e3dSKiyoshi Ueda 	/*
2205cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2206cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2207cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2208cec47e3dSKiyoshi Ueda 	 */
2209cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2210eca7ee6dSMike Snitzer 		dm_start_queue(md->queue);
2211cec47e3dSKiyoshi Ueda 
22122ca3310eSAlasdair G Kergon 	unlock_fs(md);
22132ca3310eSAlasdair G Kergon 
2214ffcc3936SMike Snitzer 	return 0;
2215ffcc3936SMike Snitzer }
2216ffcc3936SMike Snitzer 
2217ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md)
2218ffcc3936SMike Snitzer {
2219ffcc3936SMike Snitzer 	int r = -EINVAL;
2220ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2221ffcc3936SMike Snitzer 
2222ffcc3936SMike Snitzer retry:
2223ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2224ffcc3936SMike Snitzer 
2225ffcc3936SMike Snitzer 	if (!dm_suspended_md(md))
2226ffcc3936SMike Snitzer 		goto out;
2227ffcc3936SMike Snitzer 
2228ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2229ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2230ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2231ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2232ffcc3936SMike Snitzer 		if (r)
2233ffcc3936SMike Snitzer 			return r;
2234ffcc3936SMike Snitzer 		goto retry;
2235ffcc3936SMike Snitzer 	}
2236ffcc3936SMike Snitzer 
2237a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2238ffcc3936SMike Snitzer 	if (!map || !dm_table_get_size(map))
2239ffcc3936SMike Snitzer 		goto out;
2240ffcc3936SMike Snitzer 
2241ffcc3936SMike Snitzer 	r = __dm_resume(md, map);
2242ffcc3936SMike Snitzer 	if (r)
2243ffcc3936SMike Snitzer 		goto out;
2244ffcc3936SMike Snitzer 
22452ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
22462ca3310eSAlasdair G Kergon 
2247cf222b37SAlasdair G Kergon 	r = 0;
2248cf222b37SAlasdair G Kergon out:
2249e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
22502ca3310eSAlasdair G Kergon 
2251cf222b37SAlasdair G Kergon 	return r;
22521da177e4SLinus Torvalds }
22531da177e4SLinus Torvalds 
2254fd2ed4d2SMikulas Patocka /*
2255fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
2256fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
2257fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
2258fd2ed4d2SMikulas Patocka  */
2259fd2ed4d2SMikulas Patocka 
2260ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
2261ffcc3936SMike Snitzer {
2262ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2263ffcc3936SMike Snitzer 
226496b26c8cSMikulas Patocka 	if (md->internal_suspend_count++)
2265ffcc3936SMike Snitzer 		return; /* nested internal suspend */
2266ffcc3936SMike Snitzer 
2267ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2268ffcc3936SMike Snitzer 		set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2269ffcc3936SMike Snitzer 		return; /* nest suspend */
2270ffcc3936SMike Snitzer 	}
2271ffcc3936SMike Snitzer 
2272a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2273ffcc3936SMike Snitzer 
2274ffcc3936SMike Snitzer 	/*
2275ffcc3936SMike Snitzer 	 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2276ffcc3936SMike Snitzer 	 * supported.  Properly supporting a TASK_INTERRUPTIBLE internal suspend
2277ffcc3936SMike Snitzer 	 * would require changing .presuspend to return an error -- avoid this
2278ffcc3936SMike Snitzer 	 * until there is a need for more elaborate variants of internal suspend.
2279ffcc3936SMike Snitzer 	 */
2280ffcc3936SMike Snitzer 	(void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE);
2281ffcc3936SMike Snitzer 
2282ffcc3936SMike Snitzer 	set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2283ffcc3936SMike Snitzer 
2284ffcc3936SMike Snitzer 	dm_table_postsuspend_targets(map);
2285ffcc3936SMike Snitzer }
2286ffcc3936SMike Snitzer 
2287ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md)
2288ffcc3936SMike Snitzer {
228996b26c8cSMikulas Patocka 	BUG_ON(!md->internal_suspend_count);
229096b26c8cSMikulas Patocka 
229196b26c8cSMikulas Patocka 	if (--md->internal_suspend_count)
2292ffcc3936SMike Snitzer 		return; /* resume from nested internal suspend */
2293ffcc3936SMike Snitzer 
2294ffcc3936SMike Snitzer 	if (dm_suspended_md(md))
2295ffcc3936SMike Snitzer 		goto done; /* resume from nested suspend */
2296ffcc3936SMike Snitzer 
2297ffcc3936SMike Snitzer 	/*
2298ffcc3936SMike Snitzer 	 * NOTE: existing callers don't need to call dm_table_resume_targets
2299ffcc3936SMike Snitzer 	 * (which may fail -- so best to avoid it for now by passing NULL map)
2300ffcc3936SMike Snitzer 	 */
2301ffcc3936SMike Snitzer 	(void) __dm_resume(md, NULL);
2302ffcc3936SMike Snitzer 
2303ffcc3936SMike Snitzer done:
2304ffcc3936SMike Snitzer 	clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2305ffcc3936SMike Snitzer 	smp_mb__after_atomic();
2306ffcc3936SMike Snitzer 	wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2307ffcc3936SMike Snitzer }
2308ffcc3936SMike Snitzer 
2309ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md)
2310fd2ed4d2SMikulas Patocka {
2311fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
2312ffcc3936SMike Snitzer 	__dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2313ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2314ffcc3936SMike Snitzer }
2315ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
2316ffcc3936SMike Snitzer 
2317ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md)
2318ffcc3936SMike Snitzer {
2319ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2320ffcc3936SMike Snitzer 	__dm_internal_resume(md);
2321ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2322ffcc3936SMike Snitzer }
2323ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume);
2324ffcc3936SMike Snitzer 
2325ffcc3936SMike Snitzer /*
2326ffcc3936SMike Snitzer  * Fast variants of internal suspend/resume hold md->suspend_lock,
2327ffcc3936SMike Snitzer  * which prevents interaction with userspace-driven suspend.
2328ffcc3936SMike Snitzer  */
2329ffcc3936SMike Snitzer 
2330ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md)
2331ffcc3936SMike Snitzer {
2332ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2333ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2334fd2ed4d2SMikulas Patocka 		return;
2335fd2ed4d2SMikulas Patocka 
2336fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2337fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
2338fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
2339fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2340fd2ed4d2SMikulas Patocka }
2341b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
2342fd2ed4d2SMikulas Patocka 
2343ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md)
2344fd2ed4d2SMikulas Patocka {
2345ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2346fd2ed4d2SMikulas Patocka 		goto done;
2347fd2ed4d2SMikulas Patocka 
2348fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
2349fd2ed4d2SMikulas Patocka 
2350fd2ed4d2SMikulas Patocka done:
2351fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
2352fd2ed4d2SMikulas Patocka }
2353b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
2354fd2ed4d2SMikulas Patocka 
23551da177e4SLinus Torvalds /*-----------------------------------------------------------------
23561da177e4SLinus Torvalds  * Event notification.
23571da177e4SLinus Torvalds  *---------------------------------------------------------------*/
23583abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
235960935eb2SMilan Broz 		       unsigned cookie)
236069267a30SAlasdair G Kergon {
236160935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
236260935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
236360935eb2SMilan Broz 
236460935eb2SMilan Broz 	if (!cookie)
23653abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
236660935eb2SMilan Broz 	else {
236760935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
236860935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
23693abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
23703abf85b5SPeter Rajnoha 					  action, envp);
237160935eb2SMilan Broz 	}
237269267a30SAlasdair G Kergon }
237369267a30SAlasdair G Kergon 
23747a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
23757a8c3d3bSMike Anderson {
23767a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
23777a8c3d3bSMike Anderson }
23787a8c3d3bSMike Anderson 
23791da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
23801da177e4SLinus Torvalds {
23811da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
23821da177e4SLinus Torvalds }
23831da177e4SLinus Torvalds 
23841da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
23851da177e4SLinus Torvalds {
23861da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
23871da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
23881da177e4SLinus Torvalds }
23891da177e4SLinus Torvalds 
23907a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
23917a8c3d3bSMike Anderson {
23927a8c3d3bSMike Anderson 	unsigned long flags;
23937a8c3d3bSMike Anderson 
23947a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
23957a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
23967a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
23977a8c3d3bSMike Anderson }
23987a8c3d3bSMike Anderson 
23991da177e4SLinus Torvalds /*
24001da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
24011da177e4SLinus Torvalds  * count on 'md'.
24021da177e4SLinus Torvalds  */
24031da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
24041da177e4SLinus Torvalds {
24051da177e4SLinus Torvalds 	return md->disk;
24061da177e4SLinus Torvalds }
240765ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk);
24081da177e4SLinus Torvalds 
2409784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2410784aae73SMilan Broz {
24112995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
2412784aae73SMilan Broz }
2413784aae73SMilan Broz 
2414784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2415784aae73SMilan Broz {
2416784aae73SMilan Broz 	struct mapped_device *md;
2417784aae73SMilan Broz 
24182995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
2419784aae73SMilan Broz 
24204d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
2421432a212cSMike Anderson 	    dm_deleting_md(md))
24224d89b7b4SMilan Broz 		return NULL;
24234d89b7b4SMilan Broz 
2424784aae73SMilan Broz 	dm_get(md);
2425784aae73SMilan Broz 	return md;
2426784aae73SMilan Broz }
2427784aae73SMilan Broz 
24284f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
24291da177e4SLinus Torvalds {
24301da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
24311da177e4SLinus Torvalds }
24321da177e4SLinus Torvalds 
2433ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md)
2434ffcc3936SMike Snitzer {
2435ffcc3936SMike Snitzer 	return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2436ffcc3936SMike Snitzer }
2437ffcc3936SMike Snitzer 
24382c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
24392c140a24SMikulas Patocka {
24402c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
24412c140a24SMikulas Patocka }
24422c140a24SMikulas Patocka 
244364dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
244464dbce58SKiyoshi Ueda {
2445ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
244664dbce58SKiyoshi Ueda }
244764dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
244864dbce58SKiyoshi Ueda 
24492e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
24502e93ccc1SKiyoshi Ueda {
2451ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
24522e93ccc1SKiyoshi Ueda }
24532e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
24542e93ccc1SKiyoshi Ueda 
245578d8e58aSMike Snitzer struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type,
245630187e1dSMike Snitzer 					    unsigned integrity, unsigned per_io_data_size)
2457e6ee8c0bSKiyoshi Ueda {
2458115485e8SMike Snitzer 	struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
245978d8e58aSMike Snitzer 	struct kmem_cache *cachep = NULL;
246078d8e58aSMike Snitzer 	unsigned int pool_size = 0;
24615f015204SJun'ichi Nomura 	unsigned int front_pad;
2462e6ee8c0bSKiyoshi Ueda 
2463e6ee8c0bSKiyoshi Ueda 	if (!pools)
24644e6e36c3SMike Snitzer 		return NULL;
2465e6ee8c0bSKiyoshi Ueda 
246678d8e58aSMike Snitzer 	switch (type) {
246778d8e58aSMike Snitzer 	case DM_TYPE_BIO_BASED:
246878d8e58aSMike Snitzer 		cachep = _io_cache;
246978d8e58aSMike Snitzer 		pool_size = dm_get_reserved_bio_based_ios();
247030187e1dSMike Snitzer 		front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
247178d8e58aSMike Snitzer 		break;
247278d8e58aSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
247378d8e58aSMike Snitzer 		cachep = _rq_tio_cache;
247478d8e58aSMike Snitzer 		pool_size = dm_get_reserved_rq_based_ios();
247578d8e58aSMike Snitzer 		pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache);
247678d8e58aSMike Snitzer 		if (!pools->rq_pool)
247778d8e58aSMike Snitzer 			goto out;
247878d8e58aSMike Snitzer 		/* fall through to setup remaining rq-based pools */
247978d8e58aSMike Snitzer 	case DM_TYPE_MQ_REQUEST_BASED:
248078d8e58aSMike Snitzer 		if (!pool_size)
248178d8e58aSMike Snitzer 			pool_size = dm_get_reserved_rq_based_ios();
248278d8e58aSMike Snitzer 		front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
2483591ddcfcSMike Snitzer 		/* per_io_data_size is used for blk-mq pdu at queue allocation */
248478d8e58aSMike Snitzer 		break;
248578d8e58aSMike Snitzer 	default:
248678d8e58aSMike Snitzer 		BUG();
248778d8e58aSMike Snitzer 	}
248878d8e58aSMike Snitzer 
248978d8e58aSMike Snitzer 	if (cachep) {
249078d8e58aSMike Snitzer 		pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
2491e6ee8c0bSKiyoshi Ueda 		if (!pools->io_pool)
24925f015204SJun'ichi Nomura 			goto out;
249378d8e58aSMike Snitzer 	}
2494e6ee8c0bSKiyoshi Ueda 
24953d8aab2dSJunichi Nomura 	pools->bs = bioset_create_nobvec(pool_size, front_pad);
2496e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
24975f015204SJun'ichi Nomura 		goto out;
2498e6ee8c0bSKiyoshi Ueda 
2499a91a2785SMartin K. Petersen 	if (integrity && bioset_integrity_create(pools->bs, pool_size))
25005f015204SJun'ichi Nomura 		goto out;
2501a91a2785SMartin K. Petersen 
2502e6ee8c0bSKiyoshi Ueda 	return pools;
250378d8e58aSMike Snitzer 
25045f015204SJun'ichi Nomura out:
25055f015204SJun'ichi Nomura 	dm_free_md_mempools(pools);
2506e6ee8c0bSKiyoshi Ueda 
25074e6e36c3SMike Snitzer 	return NULL;
2508e6ee8c0bSKiyoshi Ueda }
2509e6ee8c0bSKiyoshi Ueda 
2510e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
2511e6ee8c0bSKiyoshi Ueda {
2512e6ee8c0bSKiyoshi Ueda 	if (!pools)
2513e6ee8c0bSKiyoshi Ueda 		return;
2514e6ee8c0bSKiyoshi Ueda 
2515e6ee8c0bSKiyoshi Ueda 	mempool_destroy(pools->io_pool);
25161ae49ea2SMike Snitzer 	mempool_destroy(pools->rq_pool);
25171ae49ea2SMike Snitzer 
2518e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
2519e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
2520e6ee8c0bSKiyoshi Ueda 
2521e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2522e6ee8c0bSKiyoshi Ueda }
2523e6ee8c0bSKiyoshi Ueda 
252471cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
252571cdb697SChristoph Hellwig 			  u32 flags)
252671cdb697SChristoph Hellwig {
252771cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
252871cdb697SChristoph Hellwig 	const struct pr_ops *ops;
252971cdb697SChristoph Hellwig 	fmode_t mode;
2530956a4025SMike Snitzer 	int r;
253171cdb697SChristoph Hellwig 
2532956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
253371cdb697SChristoph Hellwig 	if (r < 0)
253471cdb697SChristoph Hellwig 		return r;
253571cdb697SChristoph Hellwig 
253671cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
253771cdb697SChristoph Hellwig 	if (ops && ops->pr_register)
253871cdb697SChristoph Hellwig 		r = ops->pr_register(bdev, old_key, new_key, flags);
253971cdb697SChristoph Hellwig 	else
254071cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
254171cdb697SChristoph Hellwig 
2542956a4025SMike Snitzer 	bdput(bdev);
254371cdb697SChristoph Hellwig 	return r;
254471cdb697SChristoph Hellwig }
254571cdb697SChristoph Hellwig 
254671cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
254771cdb697SChristoph Hellwig 			 u32 flags)
254871cdb697SChristoph Hellwig {
254971cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
255071cdb697SChristoph Hellwig 	const struct pr_ops *ops;
255171cdb697SChristoph Hellwig 	fmode_t mode;
2552956a4025SMike Snitzer 	int r;
255371cdb697SChristoph Hellwig 
2554956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
255571cdb697SChristoph Hellwig 	if (r < 0)
255671cdb697SChristoph Hellwig 		return r;
255771cdb697SChristoph Hellwig 
255871cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
255971cdb697SChristoph Hellwig 	if (ops && ops->pr_reserve)
256071cdb697SChristoph Hellwig 		r = ops->pr_reserve(bdev, key, type, flags);
256171cdb697SChristoph Hellwig 	else
256271cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
256371cdb697SChristoph Hellwig 
2564956a4025SMike Snitzer 	bdput(bdev);
256571cdb697SChristoph Hellwig 	return r;
256671cdb697SChristoph Hellwig }
256771cdb697SChristoph Hellwig 
256871cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
256971cdb697SChristoph Hellwig {
257071cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
257171cdb697SChristoph Hellwig 	const struct pr_ops *ops;
257271cdb697SChristoph Hellwig 	fmode_t mode;
2573956a4025SMike Snitzer 	int r;
257471cdb697SChristoph Hellwig 
2575956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
257671cdb697SChristoph Hellwig 	if (r < 0)
257771cdb697SChristoph Hellwig 		return r;
257871cdb697SChristoph Hellwig 
257971cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
258071cdb697SChristoph Hellwig 	if (ops && ops->pr_release)
258171cdb697SChristoph Hellwig 		r = ops->pr_release(bdev, key, type);
258271cdb697SChristoph Hellwig 	else
258371cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
258471cdb697SChristoph Hellwig 
2585956a4025SMike Snitzer 	bdput(bdev);
258671cdb697SChristoph Hellwig 	return r;
258771cdb697SChristoph Hellwig }
258871cdb697SChristoph Hellwig 
258971cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
259071cdb697SChristoph Hellwig 			 enum pr_type type, bool abort)
259171cdb697SChristoph Hellwig {
259271cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
259371cdb697SChristoph Hellwig 	const struct pr_ops *ops;
259471cdb697SChristoph Hellwig 	fmode_t mode;
2595956a4025SMike Snitzer 	int r;
259671cdb697SChristoph Hellwig 
2597956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
259871cdb697SChristoph Hellwig 	if (r < 0)
259971cdb697SChristoph Hellwig 		return r;
260071cdb697SChristoph Hellwig 
260171cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
260271cdb697SChristoph Hellwig 	if (ops && ops->pr_preempt)
260371cdb697SChristoph Hellwig 		r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
260471cdb697SChristoph Hellwig 	else
260571cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
260671cdb697SChristoph Hellwig 
2607956a4025SMike Snitzer 	bdput(bdev);
260871cdb697SChristoph Hellwig 	return r;
260971cdb697SChristoph Hellwig }
261071cdb697SChristoph Hellwig 
261171cdb697SChristoph Hellwig static int dm_pr_clear(struct block_device *bdev, u64 key)
261271cdb697SChristoph Hellwig {
261371cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
261471cdb697SChristoph Hellwig 	const struct pr_ops *ops;
261571cdb697SChristoph Hellwig 	fmode_t mode;
2616956a4025SMike Snitzer 	int r;
261771cdb697SChristoph Hellwig 
2618956a4025SMike Snitzer 	r = dm_grab_bdev_for_ioctl(md, &bdev, &mode);
261971cdb697SChristoph Hellwig 	if (r < 0)
262071cdb697SChristoph Hellwig 		return r;
262171cdb697SChristoph Hellwig 
262271cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
262371cdb697SChristoph Hellwig 	if (ops && ops->pr_clear)
262471cdb697SChristoph Hellwig 		r = ops->pr_clear(bdev, key);
262571cdb697SChristoph Hellwig 	else
262671cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
262771cdb697SChristoph Hellwig 
2628956a4025SMike Snitzer 	bdput(bdev);
262971cdb697SChristoph Hellwig 	return r;
263071cdb697SChristoph Hellwig }
263171cdb697SChristoph Hellwig 
263271cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = {
263371cdb697SChristoph Hellwig 	.pr_register	= dm_pr_register,
263471cdb697SChristoph Hellwig 	.pr_reserve	= dm_pr_reserve,
263571cdb697SChristoph Hellwig 	.pr_release	= dm_pr_release,
263671cdb697SChristoph Hellwig 	.pr_preempt	= dm_pr_preempt,
263771cdb697SChristoph Hellwig 	.pr_clear	= dm_pr_clear,
263871cdb697SChristoph Hellwig };
263971cdb697SChristoph Hellwig 
264083d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
26411da177e4SLinus Torvalds 	.open = dm_blk_open,
26421da177e4SLinus Torvalds 	.release = dm_blk_close,
2643aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
26443ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
264571cdb697SChristoph Hellwig 	.pr_ops = &dm_pr_ops,
26461da177e4SLinus Torvalds 	.owner = THIS_MODULE
26471da177e4SLinus Torvalds };
26481da177e4SLinus Torvalds 
26491da177e4SLinus Torvalds /*
26501da177e4SLinus Torvalds  * module hooks
26511da177e4SLinus Torvalds  */
26521da177e4SLinus Torvalds module_init(dm_init);
26531da177e4SLinus Torvalds module_exit(dm_exit);
26541da177e4SLinus Torvalds 
26551da177e4SLinus Torvalds module_param(major, uint, 0);
26561da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
2657f4790826SMike Snitzer 
2658e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
2659e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
2660e8603136SMike Snitzer 
2661115485e8SMike Snitzer module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
2662115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
2663115485e8SMike Snitzer 
26641da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
26651da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
26661da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2667