xref: /openbmc/linux/drivers/md/dm.c (revision 78ccef91234ba331c04d71f3ecb1377451d21056)
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"
1191ccbbacSTushar Sugandhi #include "dm-ima.h"
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include <linux/init.h>
141da177e4SLinus Torvalds #include <linux/module.h>
1548c9c27bSArjan van de Ven #include <linux/mutex.h>
166958c1c6SMikulas Patocka #include <linux/sched/mm.h>
17174cd4b1SIngo Molnar #include <linux/sched/signal.h>
181da177e4SLinus Torvalds #include <linux/blkpg.h>
191da177e4SLinus Torvalds #include <linux/bio.h>
201da177e4SLinus Torvalds #include <linux/mempool.h>
21f26c5719SDan Williams #include <linux/dax.h>
221da177e4SLinus Torvalds #include <linux/slab.h>
231da177e4SLinus Torvalds #include <linux/idr.h>
247e026c8cSDan Williams #include <linux/uio.h>
253ac51e74SDarrick J. Wong #include <linux/hdreg.h>
263f77316dSKiyoshi Ueda #include <linux/delay.h>
27ffcc3936SMike Snitzer #include <linux/wait.h>
2871cdb697SChristoph Hellwig #include <linux/pr.h>
29b0b4d7c6SElena Reshetova #include <linux/refcount.h>
30c6a564ffSChristoph Hellwig #include <linux/part_stat.h>
31a892c8d5SSatya Tangirala #include <linux/blk-crypto.h>
321e8d44bdSEric Biggers #include <linux/blk-crypto-profile.h>
3355782138SLi Zefan 
3472d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
3572d94861SAlasdair G Kergon 
3660935eb2SMilan Broz /*
3760935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
3860935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
3960935eb2SMilan Broz  */
4060935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
4160935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
4260935eb2SMilan Broz 
43b99fdcdcSMing Lei /*
44b99fdcdcSMing Lei  * For REQ_POLLED fs bio, this flag is set if we link mapped underlying
45b99fdcdcSMing Lei  * dm_io into one list, and reuse bio->bi_private as the list head. Before
46b99fdcdcSMing Lei  * ending this fs bio, we will recover its ->bi_private.
47b99fdcdcSMing Lei  */
48b99fdcdcSMing Lei #define REQ_DM_POLL_LIST	REQ_DRV
49b99fdcdcSMing Lei 
501da177e4SLinus Torvalds static const char *_name = DM_NAME;
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds static unsigned int major = 0;
531da177e4SLinus Torvalds static unsigned int _major = 0;
541da177e4SLinus Torvalds 
55d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr);
56d15b774cSAlasdair G Kergon 
57f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
582c140a24SMikulas Patocka 
592c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w);
602c140a24SMikulas Patocka 
612c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
622c140a24SMikulas Patocka 
63acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue;
64acfe0ad7SMikulas Patocka 
6593e6442cSMikulas Patocka atomic_t dm_global_event_nr = ATOMIC_INIT(0);
6693e6442cSMikulas Patocka DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq);
6793e6442cSMikulas Patocka 
6862e08243SMikulas Patocka void dm_issue_global_event(void)
6962e08243SMikulas Patocka {
7062e08243SMikulas Patocka 	atomic_inc(&dm_global_event_nr);
7162e08243SMikulas Patocka 	wake_up(&dm_global_eventq);
7262e08243SMikulas Patocka }
7362e08243SMikulas Patocka 
74442761fdSMike Snitzer DEFINE_STATIC_KEY_FALSE(stats_enabled);
75442761fdSMike Snitzer DEFINE_STATIC_KEY_FALSE(swap_bios_enabled);
76442761fdSMike Snitzer DEFINE_STATIC_KEY_FALSE(zoned_enabled);
77442761fdSMike Snitzer 
781da177e4SLinus Torvalds /*
7964f52b0eSMike Snitzer  * One of these is allocated (on-stack) per original bio.
801da177e4SLinus Torvalds  */
8164f52b0eSMike Snitzer struct clone_info {
8264f52b0eSMike Snitzer 	struct dm_table *map;
8364f52b0eSMike Snitzer 	struct bio *bio;
8464f52b0eSMike Snitzer 	struct dm_io *io;
8564f52b0eSMike Snitzer 	sector_t sector;
8664f52b0eSMike Snitzer 	unsigned sector_count;
874edadf6dSMike Snitzer 	bool is_abnormal_io:1;
884edadf6dSMike Snitzer 	bool submit_as_polled:1;
8964f52b0eSMike Snitzer };
9064f52b0eSMike Snitzer 
9162f26317SJeffle Xu #define DM_TARGET_IO_BIO_OFFSET (offsetof(struct dm_target_io, clone))
9262f26317SJeffle Xu #define DM_IO_BIO_OFFSET \
9362f26317SJeffle Xu 	(offsetof(struct dm_target_io, clone) + offsetof(struct dm_io, tio))
9462f26317SJeffle Xu 
956c23f0bdSChristoph Hellwig static inline struct dm_target_io *clone_to_tio(struct bio *clone)
966c23f0bdSChristoph Hellwig {
976c23f0bdSChristoph Hellwig 	return container_of(clone, struct dm_target_io, clone);
986c23f0bdSChristoph Hellwig }
996c23f0bdSChristoph Hellwig 
10064f52b0eSMike Snitzer void *dm_per_bio_data(struct bio *bio, size_t data_size)
10164f52b0eSMike Snitzer {
102655f3aadSMike Snitzer 	if (!dm_tio_flagged(clone_to_tio(bio), DM_TIO_INSIDE_DM_IO))
10362f26317SJeffle Xu 		return (char *)bio - DM_TARGET_IO_BIO_OFFSET - data_size;
10462f26317SJeffle Xu 	return (char *)bio - DM_IO_BIO_OFFSET - data_size;
10564f52b0eSMike Snitzer }
10664f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_per_bio_data);
10764f52b0eSMike Snitzer 
10864f52b0eSMike Snitzer struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size)
10964f52b0eSMike Snitzer {
11064f52b0eSMike Snitzer 	struct dm_io *io = (struct dm_io *)((char *)data + data_size);
11164f52b0eSMike Snitzer 	if (io->magic == DM_IO_MAGIC)
11262f26317SJeffle Xu 		return (struct bio *)((char *)io + DM_IO_BIO_OFFSET);
11364f52b0eSMike Snitzer 	BUG_ON(io->magic != DM_TIO_MAGIC);
11462f26317SJeffle Xu 	return (struct bio *)((char *)io + DM_TARGET_IO_BIO_OFFSET);
11564f52b0eSMike Snitzer }
11664f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_from_per_bio_data);
11764f52b0eSMike Snitzer 
11864f52b0eSMike Snitzer unsigned dm_bio_get_target_bio_nr(const struct bio *bio)
11964f52b0eSMike Snitzer {
12064f52b0eSMike Snitzer 	return container_of(bio, struct dm_target_io, clone)->target_bio_nr;
12164f52b0eSMike Snitzer }
12264f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_get_target_bio_nr);
12364f52b0eSMike Snitzer 
124ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
125ba61fdd1SJeff Mahoney 
126115485e8SMike Snitzer #define DM_NUMA_NODE NUMA_NO_NODE
127115485e8SMike Snitzer static int dm_numa_node = DM_NUMA_NODE;
128faad87dfSMike Snitzer 
129a666e5c0SMikulas Patocka #define DEFAULT_SWAP_BIOS	(8 * 1048576 / PAGE_SIZE)
130a666e5c0SMikulas Patocka static int swap_bios = DEFAULT_SWAP_BIOS;
131a666e5c0SMikulas Patocka static int get_swap_bios(void)
132a666e5c0SMikulas Patocka {
133a666e5c0SMikulas Patocka 	int latch = READ_ONCE(swap_bios);
134a666e5c0SMikulas Patocka 	if (unlikely(latch <= 0))
135a666e5c0SMikulas Patocka 		latch = DEFAULT_SWAP_BIOS;
136a666e5c0SMikulas Patocka 	return latch;
137a666e5c0SMikulas Patocka }
138a666e5c0SMikulas Patocka 
13986f1152bSBenjamin Marzinski struct table_device {
14086f1152bSBenjamin Marzinski 	struct list_head list;
141b0b4d7c6SElena Reshetova 	refcount_t count;
14286f1152bSBenjamin Marzinski 	struct dm_dev dm_dev;
14386f1152bSBenjamin Marzinski };
14486f1152bSBenjamin Marzinski 
145f4790826SMike Snitzer /*
146e8603136SMike Snitzer  * Bio-based DM's mempools' reserved IOs set by the user.
147e8603136SMike Snitzer  */
1484cc96131SMike Snitzer #define RESERVED_BIO_BASED_IOS		16
149e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
150e8603136SMike Snitzer 
151115485e8SMike Snitzer static int __dm_get_module_param_int(int *module_param, int min, int max)
152115485e8SMike Snitzer {
1536aa7de05SMark Rutland 	int param = READ_ONCE(*module_param);
154115485e8SMike Snitzer 	int modified_param = 0;
155115485e8SMike Snitzer 	bool modified = true;
156115485e8SMike Snitzer 
157115485e8SMike Snitzer 	if (param < min)
158115485e8SMike Snitzer 		modified_param = min;
159115485e8SMike Snitzer 	else if (param > max)
160115485e8SMike Snitzer 		modified_param = max;
161115485e8SMike Snitzer 	else
162115485e8SMike Snitzer 		modified = false;
163115485e8SMike Snitzer 
164115485e8SMike Snitzer 	if (modified) {
165115485e8SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
166115485e8SMike Snitzer 		param = modified_param;
167115485e8SMike Snitzer 	}
168115485e8SMike Snitzer 
169115485e8SMike Snitzer 	return param;
170115485e8SMike Snitzer }
171115485e8SMike Snitzer 
1724cc96131SMike Snitzer unsigned __dm_get_module_param(unsigned *module_param,
173f4790826SMike Snitzer 			       unsigned def, unsigned max)
174f4790826SMike Snitzer {
1756aa7de05SMark Rutland 	unsigned param = READ_ONCE(*module_param);
17609c2d531SMike Snitzer 	unsigned modified_param = 0;
177f4790826SMike Snitzer 
17809c2d531SMike Snitzer 	if (!param)
17909c2d531SMike Snitzer 		modified_param = def;
18009c2d531SMike Snitzer 	else if (param > max)
18109c2d531SMike Snitzer 		modified_param = max;
182f4790826SMike Snitzer 
18309c2d531SMike Snitzer 	if (modified_param) {
18409c2d531SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
18509c2d531SMike Snitzer 		param = modified_param;
186f4790826SMike Snitzer 	}
187f4790826SMike Snitzer 
18809c2d531SMike Snitzer 	return param;
189f4790826SMike Snitzer }
190f4790826SMike Snitzer 
191e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void)
192e8603136SMike Snitzer {
19309c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_bio_based_ios,
1944cc96131SMike Snitzer 				     RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS);
195e8603136SMike Snitzer }
196e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
197e8603136SMike Snitzer 
198115485e8SMike Snitzer static unsigned dm_get_numa_node(void)
199115485e8SMike Snitzer {
200115485e8SMike Snitzer 	return __dm_get_module_param_int(&dm_numa_node,
201115485e8SMike Snitzer 					 DM_NUMA_NODE, num_online_nodes() - 1);
202115485e8SMike Snitzer }
203115485e8SMike Snitzer 
2041da177e4SLinus Torvalds static int __init local_init(void)
2051da177e4SLinus Torvalds {
206e689fbabSMike Snitzer 	int r;
2071ae49ea2SMike Snitzer 
20851e5b2bdSMike Anderson 	r = dm_uevent_init();
20951157b4aSKiyoshi Ueda 	if (r)
210e689fbabSMike Snitzer 		return r;
21151e5b2bdSMike Anderson 
212acfe0ad7SMikulas Patocka 	deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
213acfe0ad7SMikulas Patocka 	if (!deferred_remove_workqueue) {
214acfe0ad7SMikulas Patocka 		r = -ENOMEM;
215acfe0ad7SMikulas Patocka 		goto out_uevent_exit;
216acfe0ad7SMikulas Patocka 	}
217acfe0ad7SMikulas Patocka 
2181da177e4SLinus Torvalds 	_major = major;
2191da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
22051157b4aSKiyoshi Ueda 	if (r < 0)
221acfe0ad7SMikulas Patocka 		goto out_free_workqueue;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	if (!_major)
2241da177e4SLinus Torvalds 		_major = r;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	return 0;
22751157b4aSKiyoshi Ueda 
228acfe0ad7SMikulas Patocka out_free_workqueue:
229acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
23051157b4aSKiyoshi Ueda out_uevent_exit:
23151157b4aSKiyoshi Ueda 	dm_uevent_exit();
23251157b4aSKiyoshi Ueda 
23351157b4aSKiyoshi Ueda 	return r;
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds static void local_exit(void)
2371da177e4SLinus Torvalds {
2382c140a24SMikulas Patocka 	flush_scheduled_work();
239acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
2402c140a24SMikulas Patocka 
24100d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
24251e5b2bdSMike Anderson 	dm_uevent_exit();
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	_major = 0;
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	DMINFO("cleaned up");
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
249b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
2501da177e4SLinus Torvalds 	local_init,
2511da177e4SLinus Torvalds 	dm_target_init,
2521da177e4SLinus Torvalds 	dm_linear_init,
2531da177e4SLinus Torvalds 	dm_stripe_init,
254952b3557SMikulas Patocka 	dm_io_init,
255945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2561da177e4SLinus Torvalds 	dm_interface_init,
257fd2ed4d2SMikulas Patocka 	dm_statistics_init,
2581da177e4SLinus Torvalds };
2591da177e4SLinus Torvalds 
260b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2611da177e4SLinus Torvalds 	local_exit,
2621da177e4SLinus Torvalds 	dm_target_exit,
2631da177e4SLinus Torvalds 	dm_linear_exit,
2641da177e4SLinus Torvalds 	dm_stripe_exit,
265952b3557SMikulas Patocka 	dm_io_exit,
266945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
2671da177e4SLinus Torvalds 	dm_interface_exit,
268fd2ed4d2SMikulas Patocka 	dm_statistics_exit,
2691da177e4SLinus Torvalds };
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds static int __init dm_init(void)
2721da177e4SLinus Torvalds {
2731da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
2741da177e4SLinus Torvalds 	int r, i;
2751da177e4SLinus Torvalds 
276f1cd6cb2STushar Sugandhi #if (IS_ENABLED(CONFIG_IMA) && !IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE))
277f1cd6cb2STushar Sugandhi 	DMWARN("CONFIG_IMA_DISABLE_HTABLE is disabled."
278f1cd6cb2STushar Sugandhi 	       " Duplicate IMA measurements will not be recorded in the IMA log.");
279f1cd6cb2STushar Sugandhi #endif
280f1cd6cb2STushar Sugandhi 
2811da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
2821da177e4SLinus Torvalds 		r = _inits[i]();
2831da177e4SLinus Torvalds 		if (r)
2841da177e4SLinus Torvalds 			goto bad;
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	return 0;
2881da177e4SLinus Torvalds bad:
2891da177e4SLinus Torvalds 	while (i--)
2901da177e4SLinus Torvalds 		_exits[i]();
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	return r;
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds static void __exit dm_exit(void)
2961da177e4SLinus Torvalds {
2971da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	while (i--)
3001da177e4SLinus Torvalds 		_exits[i]();
301d15b774cSAlasdair G Kergon 
302d15b774cSAlasdair G Kergon 	/*
303d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
304d15b774cSAlasdair G Kergon 	 */
305d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
3061da177e4SLinus Torvalds }
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds /*
3091da177e4SLinus Torvalds  * Block device functions
3101da177e4SLinus Torvalds  */
311432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
312432a212cSMike Anderson {
313432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
314432a212cSMike Anderson }
315432a212cSMike Anderson 
316fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3171da177e4SLinus Torvalds {
3181da177e4SLinus Torvalds 	struct mapped_device *md;
3191da177e4SLinus Torvalds 
320fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
321fba9f90eSJeff Mahoney 
322fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
323fba9f90eSJeff Mahoney 	if (!md)
324fba9f90eSJeff Mahoney 		goto out;
325fba9f90eSJeff Mahoney 
3265c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
327432a212cSMike Anderson 	    dm_deleting_md(md)) {
328fba9f90eSJeff Mahoney 		md = NULL;
329fba9f90eSJeff Mahoney 		goto out;
330fba9f90eSJeff Mahoney 	}
331fba9f90eSJeff Mahoney 
3321da177e4SLinus Torvalds 	dm_get(md);
3335c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
334fba9f90eSJeff Mahoney out:
335fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
336fba9f90eSJeff Mahoney 
337fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3381da177e4SLinus Torvalds }
3391da177e4SLinus Torvalds 
340db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode)
3411da177e4SLinus Torvalds {
34263a4f065SMike Snitzer 	struct mapped_device *md;
3436e9624b8SArnd Bergmann 
3444a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
3454a1aeb98SMilan Broz 
34663a4f065SMike Snitzer 	md = disk->private_data;
34763a4f065SMike Snitzer 	if (WARN_ON(!md))
34863a4f065SMike Snitzer 		goto out;
34963a4f065SMike Snitzer 
3502c140a24SMikulas Patocka 	if (atomic_dec_and_test(&md->open_count) &&
3512c140a24SMikulas Patocka 	    (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
352acfe0ad7SMikulas Patocka 		queue_work(deferred_remove_workqueue, &deferred_remove_work);
3532c140a24SMikulas Patocka 
3541da177e4SLinus Torvalds 	dm_put(md);
35563a4f065SMike Snitzer out:
3564a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
3571da177e4SLinus Torvalds }
3581da177e4SLinus Torvalds 
3595c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3605c6bd75dSAlasdair G Kergon {
3615c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3625c6bd75dSAlasdair G Kergon }
3635c6bd75dSAlasdair G Kergon 
3645c6bd75dSAlasdair G Kergon /*
3655c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3665c6bd75dSAlasdair G Kergon  */
3672c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
3685c6bd75dSAlasdair G Kergon {
3695c6bd75dSAlasdair G Kergon 	int r = 0;
3705c6bd75dSAlasdair G Kergon 
3715c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
3725c6bd75dSAlasdair G Kergon 
3732c140a24SMikulas Patocka 	if (dm_open_count(md)) {
3745c6bd75dSAlasdair G Kergon 		r = -EBUSY;
3752c140a24SMikulas Patocka 		if (mark_deferred)
3762c140a24SMikulas Patocka 			set_bit(DMF_DEFERRED_REMOVE, &md->flags);
3772c140a24SMikulas Patocka 	} else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
3782c140a24SMikulas Patocka 		r = -EEXIST;
3795c6bd75dSAlasdair G Kergon 	else
3805c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
3815c6bd75dSAlasdair G Kergon 
3825c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
3835c6bd75dSAlasdair G Kergon 
3845c6bd75dSAlasdair G Kergon 	return r;
3855c6bd75dSAlasdair G Kergon }
3865c6bd75dSAlasdair G Kergon 
3872c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md)
3882c140a24SMikulas Patocka {
3892c140a24SMikulas Patocka 	int r = 0;
3902c140a24SMikulas Patocka 
3912c140a24SMikulas Patocka 	spin_lock(&_minor_lock);
3922c140a24SMikulas Patocka 
3932c140a24SMikulas Patocka 	if (test_bit(DMF_DELETING, &md->flags))
3942c140a24SMikulas Patocka 		r = -EBUSY;
3952c140a24SMikulas Patocka 	else
3962c140a24SMikulas Patocka 		clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
3972c140a24SMikulas Patocka 
3982c140a24SMikulas Patocka 	spin_unlock(&_minor_lock);
3992c140a24SMikulas Patocka 
4002c140a24SMikulas Patocka 	return r;
4012c140a24SMikulas Patocka }
4022c140a24SMikulas Patocka 
4032c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w)
4042c140a24SMikulas Patocka {
4052c140a24SMikulas Patocka 	dm_deferred_remove();
4062c140a24SMikulas Patocka }
4072c140a24SMikulas Patocka 
4083ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4093ac51e74SDarrick J. Wong {
4103ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
4113ac51e74SDarrick J. Wong 
4123ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
4133ac51e74SDarrick J. Wong }
4143ac51e74SDarrick J. Wong 
415971888c4SMike Snitzer static int dm_prepare_ioctl(struct mapped_device *md, int *srcu_idx,
4165bd5e8d8SMike Snitzer 			    struct block_device **bdev)
417aa129a22SMilan Broz {
41866482026SMike Snitzer 	struct dm_target *tgt;
4196c182cd8SHannes Reinecke 	struct dm_table *map;
420971888c4SMike Snitzer 	int r;
421aa129a22SMilan Broz 
4226c182cd8SHannes Reinecke retry:
423e56f81e0SChristoph Hellwig 	r = -ENOTTY;
424971888c4SMike Snitzer 	map = dm_get_live_table(md, srcu_idx);
425aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
426971888c4SMike Snitzer 		return r;
427aa129a22SMilan Broz 
428aa129a22SMilan Broz 	/* We only support devices that have a single target */
429aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
430971888c4SMike Snitzer 		return r;
431aa129a22SMilan Broz 
43266482026SMike Snitzer 	tgt = dm_table_get_target(map, 0);
43366482026SMike Snitzer 	if (!tgt->type->prepare_ioctl)
434e56f81e0SChristoph Hellwig 		return r;
435aa129a22SMilan Broz 
436971888c4SMike Snitzer 	if (dm_suspended_md(md))
437971888c4SMike Snitzer 		return -EAGAIN;
438971888c4SMike Snitzer 
4395bd5e8d8SMike Snitzer 	r = tgt->type->prepare_ioctl(tgt, bdev);
4405bbbfdf6SJunichi Nomura 	if (r == -ENOTCONN && !fatal_signal_pending(current)) {
441971888c4SMike Snitzer 		dm_put_live_table(md, *srcu_idx);
4426c182cd8SHannes Reinecke 		msleep(10);
4436c182cd8SHannes Reinecke 		goto retry;
4446c182cd8SHannes Reinecke 	}
445971888c4SMike Snitzer 
446e56f81e0SChristoph Hellwig 	return r;
447e56f81e0SChristoph Hellwig }
4486c182cd8SHannes Reinecke 
449971888c4SMike Snitzer static void dm_unprepare_ioctl(struct mapped_device *md, int srcu_idx)
450971888c4SMike Snitzer {
451971888c4SMike Snitzer 	dm_put_live_table(md, srcu_idx);
452971888c4SMike Snitzer }
453971888c4SMike Snitzer 
454e56f81e0SChristoph Hellwig static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
455e56f81e0SChristoph Hellwig 			unsigned int cmd, unsigned long arg)
456e56f81e0SChristoph Hellwig {
457e56f81e0SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
458971888c4SMike Snitzer 	int r, srcu_idx;
459e56f81e0SChristoph Hellwig 
4605bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
461e56f81e0SChristoph Hellwig 	if (r < 0)
462971888c4SMike Snitzer 		goto out;
463e56f81e0SChristoph Hellwig 
464e56f81e0SChristoph Hellwig 	if (r > 0) {
465e56f81e0SChristoph Hellwig 		/*
466e980f623SChristoph Hellwig 		 * Target determined this ioctl is being issued against a
467e980f623SChristoph Hellwig 		 * subset of the parent bdev; require extra privileges.
468e56f81e0SChristoph Hellwig 		 */
469e980f623SChristoph Hellwig 		if (!capable(CAP_SYS_RAWIO)) {
4700378c625SMike Snitzer 			DMDEBUG_LIMIT(
471e980f623SChristoph Hellwig 	"%s: sending ioctl %x to DM device without required privilege.",
472e980f623SChristoph Hellwig 				current->comm, cmd);
473e980f623SChristoph Hellwig 			r = -ENOIOCTLCMD;
474e56f81e0SChristoph Hellwig 			goto out;
475e56f81e0SChristoph Hellwig 		}
476e980f623SChristoph Hellwig 	}
477e56f81e0SChristoph Hellwig 
478a7cb3d2fSChristoph Hellwig 	if (!bdev->bd_disk->fops->ioctl)
479a7cb3d2fSChristoph Hellwig 		r = -ENOTTY;
480a7cb3d2fSChristoph Hellwig 	else
481a7cb3d2fSChristoph Hellwig 		r = bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg);
482e56f81e0SChristoph Hellwig out:
483971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
484aa129a22SMilan Broz 	return r;
485aa129a22SMilan Broz }
486aa129a22SMilan Broz 
4877465d7acSMike Snitzer u64 dm_start_time_ns_from_clone(struct bio *bio)
4887465d7acSMike Snitzer {
4896c23f0bdSChristoph Hellwig 	return jiffies_to_nsecs(clone_to_tio(bio)->io->start_time);
4907465d7acSMike Snitzer }
4917465d7acSMike Snitzer EXPORT_SYMBOL_GPL(dm_start_time_ns_from_clone);
4927465d7acSMike Snitzer 
4938d394bc4SMike Snitzer static bool bio_is_flush_with_data(struct bio *bio)
4947465d7acSMike Snitzer {
4958d394bc4SMike Snitzer 	return ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size);
4967465d7acSMike Snitzer }
4977465d7acSMike Snitzer 
498e6926ad0SMing Lei static void dm_io_acct(struct dm_io *io, bool end)
4997465d7acSMike Snitzer {
500e6926ad0SMing Lei 	struct dm_stats_aux *stats_aux = &io->stats_aux;
501e6926ad0SMing Lei 	unsigned long start_time = io->start_time;
502e6926ad0SMing Lei 	struct mapped_device *md = io->md;
503e6926ad0SMing Lei 	struct bio *bio = io->orig_bio;
504d3de6d12SMing Lei 	unsigned int sectors;
5057465d7acSMike Snitzer 
506d3de6d12SMing Lei 	/*
507d3de6d12SMing Lei 	 * If REQ_PREFLUSH set, don't account payload, it will be
508d3de6d12SMing Lei 	 * submitted (and accounted) after this flush completes.
509d3de6d12SMing Lei 	 */
510d3de6d12SMing Lei 	if (bio_is_flush_with_data(bio))
511d3de6d12SMing Lei 		sectors = 0;
5127dd76d1fSMing Lei 	else if (likely(!(dm_io_flagged(io, DM_IO_WAS_SPLIT))))
513d3de6d12SMing Lei 		sectors = bio_sectors(bio);
5147dd76d1fSMing Lei 	else
5157dd76d1fSMing Lei 		sectors = io->sectors;
5168d394bc4SMike Snitzer 
5178d394bc4SMike Snitzer 	if (!end)
518d3de6d12SMing Lei 		bdev_start_io_acct(bio->bi_bdev, sectors, bio_op(bio),
519d3de6d12SMing Lei 				   start_time);
5208d394bc4SMike Snitzer 	else
521d3de6d12SMing Lei 		bdev_end_io_acct(bio->bi_bdev, bio_op(bio), start_time);
5227465d7acSMike Snitzer 
523442761fdSMike Snitzer 	if (static_branch_unlikely(&stats_enabled) &&
5247dd76d1fSMing Lei 	    unlikely(dm_stats_used(&md->stats))) {
5257dd76d1fSMing Lei 		sector_t sector;
5267dd76d1fSMing Lei 
5277dd76d1fSMing Lei 		if (likely(!dm_io_flagged(io, DM_IO_WAS_SPLIT)))
5287dd76d1fSMing Lei 			sector = bio->bi_iter.bi_sector;
5297dd76d1fSMing Lei 		else
5307dd76d1fSMing Lei 			sector = bio_end_sector(bio) - io->sector_offset;
5317dd76d1fSMing Lei 
5327465d7acSMike Snitzer 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
5337dd76d1fSMing Lei 				    sector, sectors,
5348d394bc4SMike Snitzer 				    end, start_time, stats_aux);
5358d394bc4SMike Snitzer 	}
5367dd76d1fSMing Lei }
5378d394bc4SMike Snitzer 
538b992b40dSMing Lei static void __dm_start_io_acct(struct dm_io *io)
5398d394bc4SMike Snitzer {
540e6926ad0SMing Lei 	dm_io_acct(io, false);
5418d394bc4SMike Snitzer }
5428d394bc4SMike Snitzer 
5430fbb4d93SMike Snitzer static void dm_start_io_acct(struct dm_io *io, struct bio *clone)
5448d394bc4SMike Snitzer {
5450fbb4d93SMike Snitzer 	/*
5460fbb4d93SMike Snitzer 	 * Ensure IO accounting is only ever started once.
5470fbb4d93SMike Snitzer 	 */
5483b03f7c1SMike Snitzer 	if (dm_io_flagged(io, DM_IO_ACCOUNTED))
5490fbb4d93SMike Snitzer 		return;
5503b03f7c1SMike Snitzer 
5513b03f7c1SMike Snitzer 	/* Expect no possibility for race unless DM_TIO_IS_DUPLICATE_BIO. */
5523b03f7c1SMike Snitzer 	if (!clone || likely(dm_tio_is_normal(clone_to_tio(clone)))) {
55382f6cdccSMike Snitzer 		dm_io_set_flag(io, DM_IO_ACCOUNTED);
55482f6cdccSMike Snitzer 	} else {
55582f6cdccSMike Snitzer 		unsigned long flags;
556655f3aadSMike Snitzer 		/* Can afford locking given DM_TIO_IS_DUPLICATE_BIO */
5574d7bca13SMike Snitzer 		spin_lock_irqsave(&io->lock, flags);
55810eb3a0dSBenjamin Marzinski 		if (dm_io_flagged(io, DM_IO_ACCOUNTED)) {
55910eb3a0dSBenjamin Marzinski 			spin_unlock_irqrestore(&io->lock, flags);
56010eb3a0dSBenjamin Marzinski 			return;
56110eb3a0dSBenjamin Marzinski 		}
56282f6cdccSMike Snitzer 		dm_io_set_flag(io, DM_IO_ACCOUNTED);
5634d7bca13SMike Snitzer 		spin_unlock_irqrestore(&io->lock, flags);
56482f6cdccSMike Snitzer 	}
5650fbb4d93SMike Snitzer 
566b992b40dSMing Lei 	__dm_start_io_acct(io);
5670fbb4d93SMike Snitzer }
5680fbb4d93SMike Snitzer 
569b992b40dSMing Lei static void dm_end_io_acct(struct dm_io *io)
5700fbb4d93SMike Snitzer {
571e6926ad0SMing Lei 	dm_io_acct(io, true);
5727465d7acSMike Snitzer }
573978e51baSMike Snitzer 
574978e51baSMike Snitzer static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio)
5751da177e4SLinus Torvalds {
57664f52b0eSMike Snitzer 	struct dm_io *io;
57764f52b0eSMike Snitzer 	struct dm_target_io *tio;
57864f52b0eSMike Snitzer 	struct bio *clone;
57964f52b0eSMike Snitzer 
58029dec90aSChristoph Hellwig 	clone = bio_alloc_clone(NULL, bio, GFP_NOIO, &md->mempools->io_bs);
581ca522482SMike Snitzer 	/* Set default bdev, but target must bio_set_dev() before issuing IO */
582ca522482SMike Snitzer 	clone->bi_bdev = md->disk->part0;
58364f52b0eSMike Snitzer 
5846c23f0bdSChristoph Hellwig 	tio = clone_to_tio(clone);
585655f3aadSMike Snitzer 	tio->flags = 0;
586655f3aadSMike Snitzer 	dm_tio_set_flag(tio, DM_TIO_INSIDE_DM_IO);
58764f52b0eSMike Snitzer 	tio->io = NULL;
58864f52b0eSMike Snitzer 
58964f52b0eSMike Snitzer 	io = container_of(tio, struct dm_io, tio);
59064f52b0eSMike Snitzer 	io->magic = DM_IO_MAGIC;
59184b98f4cSMike Snitzer 	io->status = BLK_STS_OK;
5920f14d60aSMing Lei 
5930f14d60aSMing Lei 	/* one ref is for submission, the other is for completion */
5940f14d60aSMing Lei 	atomic_set(&io->io_count, 2);
5959f6dc633SMike Snitzer 	this_cpu_inc(*md->pending_io);
5967dd76d1fSMing Lei 	io->orig_bio = bio;
597978e51baSMike Snitzer 	io->md = md;
5984d7bca13SMike Snitzer 	spin_lock_init(&io->lock);
599b879f915SMike Snitzer 	io->start_time = jiffies;
60082f6cdccSMike Snitzer 	io->flags = 0;
60164f52b0eSMike Snitzer 
602442761fdSMike Snitzer 	if (static_branch_unlikely(&stats_enabled))
6030cdb90f0SMike Snitzer 		dm_stats_record_start(&md->stats, &io->stats_aux);
60464f52b0eSMike Snitzer 
60564f52b0eSMike Snitzer 	return io;
6061da177e4SLinus Torvalds }
6071da177e4SLinus Torvalds 
6080119ab14SMike Snitzer static void free_io(struct dm_io *io)
6091da177e4SLinus Torvalds {
61064f52b0eSMike Snitzer 	bio_put(&io->tio.clone);
61164f52b0eSMike Snitzer }
61264f52b0eSMike Snitzer 
6131d1068ceSChristoph Hellwig static struct bio *alloc_tio(struct clone_info *ci, struct dm_target *ti,
614dc8e2021SChristoph Hellwig 			     unsigned target_bio_nr, unsigned *len, gfp_t gfp_mask)
61564f52b0eSMike Snitzer {
61664f52b0eSMike Snitzer 	struct dm_target_io *tio;
617018b05ebSMike Snitzer 	struct bio *clone;
61864f52b0eSMike Snitzer 
61964f52b0eSMike Snitzer 	if (!ci->io->tio.io) {
62064f52b0eSMike Snitzer 		/* the dm_target_io embedded in ci->io is available */
62164f52b0eSMike Snitzer 		tio = &ci->io->tio;
622018b05ebSMike Snitzer 		/* alloc_io() already initialized embedded clone */
623018b05ebSMike Snitzer 		clone = &tio->clone;
62464f52b0eSMike Snitzer 	} else {
625ca522482SMike Snitzer 		struct mapped_device *md = ci->io->md;
626ca522482SMike Snitzer 
62729dec90aSChristoph Hellwig 		clone = bio_alloc_clone(NULL, ci->bio, gfp_mask,
62829dec90aSChristoph Hellwig 					&md->mempools->bs);
62964f52b0eSMike Snitzer 		if (!clone)
63064f52b0eSMike Snitzer 			return NULL;
631ca522482SMike Snitzer 		/* Set default bdev, but target must bio_set_dev() before issuing IO */
632ca522482SMike Snitzer 		clone->bi_bdev = md->disk->part0;
63364f52b0eSMike Snitzer 
634b99fdcdcSMing Lei 		/* REQ_DM_POLL_LIST shouldn't be inherited */
635b99fdcdcSMing Lei 		clone->bi_opf &= ~REQ_DM_POLL_LIST;
636b99fdcdcSMing Lei 
6376c23f0bdSChristoph Hellwig 		tio = clone_to_tio(clone);
638655f3aadSMike Snitzer 		tio->flags = 0; /* also clears DM_TIO_INSIDE_DM_IO */
63964f52b0eSMike Snitzer 	}
64064f52b0eSMike Snitzer 
64164f52b0eSMike Snitzer 	tio->magic = DM_TIO_MAGIC;
64264f52b0eSMike Snitzer 	tio->io = ci->io;
64364f52b0eSMike Snitzer 	tio->ti = ti;
64464f52b0eSMike Snitzer 	tio->target_bio_nr = target_bio_nr;
645dc8e2021SChristoph Hellwig 	tio->len_ptr = len;
646743598f0SMike Snitzer 	tio->old_sector = 0;
64764f52b0eSMike Snitzer 
648018b05ebSMike Snitzer 	if (len) {
649018b05ebSMike Snitzer 		clone->bi_iter.bi_size = to_bytes(*len);
650018b05ebSMike Snitzer 		if (bio_integrity(clone))
651018b05ebSMike Snitzer 			bio_integrity_trim(clone);
652018b05ebSMike Snitzer 	}
653018b05ebSMike Snitzer 
654018b05ebSMike Snitzer 	return clone;
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds 
6571d1068ceSChristoph Hellwig static void free_tio(struct bio *clone)
6581da177e4SLinus Torvalds {
659655f3aadSMike Snitzer 	if (dm_tio_flagged(clone_to_tio(clone), DM_TIO_INSIDE_DM_IO))
66064f52b0eSMike Snitzer 		return;
6611d1068ceSChristoph Hellwig 	bio_put(clone);
6621da177e4SLinus Torvalds }
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds /*
6651da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
6661da177e4SLinus Torvalds  */
66792c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
6681da177e4SLinus Torvalds {
66905447420SKiyoshi Ueda 	unsigned long flags;
6701da177e4SLinus Torvalds 
67105447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
6721da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
67305447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
67492c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds /*
6781da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
6791da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
68083d5e5b0SMikulas Patocka  * dm_put_live_table() when finished.
6811da177e4SLinus Torvalds  */
682563a225cSMike Snitzer struct dm_table *dm_get_live_table(struct mapped_device *md,
683563a225cSMike Snitzer 				   int *srcu_idx) __acquires(md->io_barrier)
6841da177e4SLinus Torvalds {
68583d5e5b0SMikulas Patocka 	*srcu_idx = srcu_read_lock(&md->io_barrier);
6861da177e4SLinus Torvalds 
68783d5e5b0SMikulas Patocka 	return srcu_dereference(md->map, &md->io_barrier);
68883d5e5b0SMikulas Patocka }
6891da177e4SLinus Torvalds 
690563a225cSMike Snitzer void dm_put_live_table(struct mapped_device *md,
691563a225cSMike Snitzer 		       int srcu_idx) __releases(md->io_barrier)
69283d5e5b0SMikulas Patocka {
69383d5e5b0SMikulas Patocka 	srcu_read_unlock(&md->io_barrier, srcu_idx);
69483d5e5b0SMikulas Patocka }
69583d5e5b0SMikulas Patocka 
69683d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md)
69783d5e5b0SMikulas Patocka {
69883d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
69983d5e5b0SMikulas Patocka 	synchronize_rcu_expedited();
70083d5e5b0SMikulas Patocka }
70183d5e5b0SMikulas Patocka 
70283d5e5b0SMikulas Patocka /*
70383d5e5b0SMikulas Patocka  * A fast alternative to dm_get_live_table/dm_put_live_table.
70483d5e5b0SMikulas Patocka  * The caller must not block between these two functions.
70583d5e5b0SMikulas Patocka  */
70683d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
70783d5e5b0SMikulas Patocka {
70883d5e5b0SMikulas Patocka 	rcu_read_lock();
70983d5e5b0SMikulas Patocka 	return rcu_dereference(md->map);
71083d5e5b0SMikulas Patocka }
71183d5e5b0SMikulas Patocka 
71283d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
71383d5e5b0SMikulas Patocka {
71483d5e5b0SMikulas Patocka 	rcu_read_unlock();
7151da177e4SLinus Torvalds }
7161da177e4SLinus Torvalds 
717563a225cSMike Snitzer static inline struct dm_table *dm_get_live_table_bio(struct mapped_device *md,
7185d7362d0SMikulas Patocka 						     int *srcu_idx, unsigned bio_opf)
719563a225cSMike Snitzer {
7205d7362d0SMikulas Patocka 	if (bio_opf & REQ_NOWAIT)
721563a225cSMike Snitzer 		return dm_get_live_table_fast(md);
722563a225cSMike Snitzer 	else
723563a225cSMike Snitzer 		return dm_get_live_table(md, srcu_idx);
724563a225cSMike Snitzer }
725563a225cSMike Snitzer 
726563a225cSMike Snitzer static inline void dm_put_live_table_bio(struct mapped_device *md, int srcu_idx,
7275d7362d0SMikulas Patocka 					 unsigned bio_opf)
728563a225cSMike Snitzer {
7295d7362d0SMikulas Patocka 	if (bio_opf & REQ_NOWAIT)
730563a225cSMike Snitzer 		dm_put_live_table_fast(md);
731563a225cSMike Snitzer 	else
732563a225cSMike Snitzer 		dm_put_live_table(md, srcu_idx);
733563a225cSMike Snitzer }
734563a225cSMike Snitzer 
735971888c4SMike Snitzer static char *_dm_claim_ptr = "I belong to device-mapper";
736971888c4SMike Snitzer 
7373ac51e74SDarrick J. Wong /*
73886f1152bSBenjamin Marzinski  * Open a table device so we can use it as a map destination.
73986f1152bSBenjamin Marzinski  */
74086f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev,
74186f1152bSBenjamin Marzinski 			     struct mapped_device *md)
74286f1152bSBenjamin Marzinski {
74386f1152bSBenjamin Marzinski 	struct block_device *bdev;
744cd913c76SChristoph Hellwig 	u64 part_off;
74586f1152bSBenjamin Marzinski 	int r;
74686f1152bSBenjamin Marzinski 
74786f1152bSBenjamin Marzinski 	BUG_ON(td->dm_dev.bdev);
74886f1152bSBenjamin Marzinski 
749519049afSMike Snitzer 	bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr);
75086f1152bSBenjamin Marzinski 	if (IS_ERR(bdev))
75186f1152bSBenjamin Marzinski 		return PTR_ERR(bdev);
75286f1152bSBenjamin Marzinski 
75386f1152bSBenjamin Marzinski 	r = bd_link_disk_holder(bdev, dm_disk(md));
75486f1152bSBenjamin Marzinski 	if (r) {
75586f1152bSBenjamin Marzinski 		blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
75686f1152bSBenjamin Marzinski 		return r;
75786f1152bSBenjamin Marzinski 	}
75886f1152bSBenjamin Marzinski 
75986f1152bSBenjamin Marzinski 	td->dm_dev.bdev = bdev;
760cd913c76SChristoph Hellwig 	td->dm_dev.dax_dev = fs_dax_get_by_bdev(bdev, &part_off);
76186f1152bSBenjamin Marzinski 	return 0;
76286f1152bSBenjamin Marzinski }
76386f1152bSBenjamin Marzinski 
76486f1152bSBenjamin Marzinski /*
76586f1152bSBenjamin Marzinski  * Close a table device that we've been using.
76686f1152bSBenjamin Marzinski  */
76786f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md)
76886f1152bSBenjamin Marzinski {
76986f1152bSBenjamin Marzinski 	if (!td->dm_dev.bdev)
77086f1152bSBenjamin Marzinski 		return;
77186f1152bSBenjamin Marzinski 
77286f1152bSBenjamin Marzinski 	bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
77386f1152bSBenjamin Marzinski 	blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
774817bf402SDan Williams 	put_dax(td->dm_dev.dax_dev);
77586f1152bSBenjamin Marzinski 	td->dm_dev.bdev = NULL;
776817bf402SDan Williams 	td->dm_dev.dax_dev = NULL;
77786f1152bSBenjamin Marzinski }
77886f1152bSBenjamin Marzinski 
77986f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev,
7808454fca4SSheetal Singala 					      fmode_t mode)
7818454fca4SSheetal Singala {
78286f1152bSBenjamin Marzinski 	struct table_device *td;
78386f1152bSBenjamin Marzinski 
78486f1152bSBenjamin Marzinski 	list_for_each_entry(td, l, list)
78586f1152bSBenjamin Marzinski 		if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
78686f1152bSBenjamin Marzinski 			return td;
78786f1152bSBenjamin Marzinski 
78886f1152bSBenjamin Marzinski 	return NULL;
78986f1152bSBenjamin Marzinski }
79086f1152bSBenjamin Marzinski 
79186f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
7928454fca4SSheetal Singala 			struct dm_dev **result)
7938454fca4SSheetal Singala {
79486f1152bSBenjamin Marzinski 	int r;
79586f1152bSBenjamin Marzinski 	struct table_device *td;
79686f1152bSBenjamin Marzinski 
79786f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
79886f1152bSBenjamin Marzinski 	td = find_table_device(&md->table_devices, dev, mode);
79986f1152bSBenjamin Marzinski 	if (!td) {
800115485e8SMike Snitzer 		td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
80186f1152bSBenjamin Marzinski 		if (!td) {
80286f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
80386f1152bSBenjamin Marzinski 			return -ENOMEM;
80486f1152bSBenjamin Marzinski 		}
80586f1152bSBenjamin Marzinski 
80686f1152bSBenjamin Marzinski 		td->dm_dev.mode = mode;
80786f1152bSBenjamin Marzinski 		td->dm_dev.bdev = NULL;
80886f1152bSBenjamin Marzinski 
80986f1152bSBenjamin Marzinski 		if ((r = open_table_device(td, dev, md))) {
81086f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
81186f1152bSBenjamin Marzinski 			kfree(td);
81286f1152bSBenjamin Marzinski 			return r;
81386f1152bSBenjamin Marzinski 		}
81486f1152bSBenjamin Marzinski 
81586f1152bSBenjamin Marzinski 		format_dev_t(td->dm_dev.name, dev);
81686f1152bSBenjamin Marzinski 
817b0b4d7c6SElena Reshetova 		refcount_set(&td->count, 1);
81886f1152bSBenjamin Marzinski 		list_add(&td->list, &md->table_devices);
819b0b4d7c6SElena Reshetova 	} else {
820b0b4d7c6SElena Reshetova 		refcount_inc(&td->count);
82186f1152bSBenjamin Marzinski 	}
82286f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
82386f1152bSBenjamin Marzinski 
82486f1152bSBenjamin Marzinski 	*result = &td->dm_dev;
82586f1152bSBenjamin Marzinski 	return 0;
82686f1152bSBenjamin Marzinski }
82786f1152bSBenjamin Marzinski 
82886f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
82986f1152bSBenjamin Marzinski {
83086f1152bSBenjamin Marzinski 	struct table_device *td = container_of(d, struct table_device, dm_dev);
83186f1152bSBenjamin Marzinski 
83286f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
833b0b4d7c6SElena Reshetova 	if (refcount_dec_and_test(&td->count)) {
83486f1152bSBenjamin Marzinski 		close_table_device(td, md);
83586f1152bSBenjamin Marzinski 		list_del(&td->list);
83686f1152bSBenjamin Marzinski 		kfree(td);
83786f1152bSBenjamin Marzinski 	}
83886f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
83986f1152bSBenjamin Marzinski }
84086f1152bSBenjamin Marzinski 
84186f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices)
84286f1152bSBenjamin Marzinski {
84386f1152bSBenjamin Marzinski 	struct list_head *tmp, *next;
84486f1152bSBenjamin Marzinski 
84586f1152bSBenjamin Marzinski 	list_for_each_safe(tmp, next, devices) {
84686f1152bSBenjamin Marzinski 		struct table_device *td = list_entry(tmp, struct table_device, list);
84786f1152bSBenjamin Marzinski 
84886f1152bSBenjamin Marzinski 		DMWARN("dm_destroy: %s still exists with %d references",
849b0b4d7c6SElena Reshetova 		       td->dm_dev.name, refcount_read(&td->count));
85086f1152bSBenjamin Marzinski 		kfree(td);
85186f1152bSBenjamin Marzinski 	}
85286f1152bSBenjamin Marzinski }
85386f1152bSBenjamin Marzinski 
85486f1152bSBenjamin Marzinski /*
8553ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
8563ac51e74SDarrick J. Wong  */
8573ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
8583ac51e74SDarrick J. Wong {
8593ac51e74SDarrick J. Wong 	*geo = md->geometry;
8603ac51e74SDarrick J. Wong 
8613ac51e74SDarrick J. Wong 	return 0;
8623ac51e74SDarrick J. Wong }
8633ac51e74SDarrick J. Wong 
8643ac51e74SDarrick J. Wong /*
8653ac51e74SDarrick J. Wong  * Set the geometry of a device.
8663ac51e74SDarrick J. Wong  */
8673ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
8683ac51e74SDarrick J. Wong {
8693ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
8703ac51e74SDarrick J. Wong 
8713ac51e74SDarrick J. Wong 	if (geo->start > sz) {
8723ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
8733ac51e74SDarrick J. Wong 		return -EINVAL;
8743ac51e74SDarrick J. Wong 	}
8753ac51e74SDarrick J. Wong 
8763ac51e74SDarrick J. Wong 	md->geometry = *geo;
8773ac51e74SDarrick J. Wong 
8783ac51e74SDarrick J. Wong 	return 0;
8793ac51e74SDarrick J. Wong }
8803ac51e74SDarrick J. Wong 
8812e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
8822e93ccc1SKiyoshi Ueda {
8832e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
8842e93ccc1SKiyoshi Ueda }
8852e93ccc1SKiyoshi Ueda 
886e2736347SMike Snitzer static void dm_io_complete(struct dm_io *io)
8871da177e4SLinus Torvalds {
8884e4cbee9SChristoph Hellwig 	blk_status_t io_error;
889b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
890e2736347SMike Snitzer 	struct bio *bio = io->orig_bio;
8912e93ccc1SKiyoshi Ueda 
8924e4cbee9SChristoph Hellwig 	if (io->status == BLK_STS_DM_REQUEUE) {
893e2736347SMike Snitzer 		unsigned long flags;
8942e93ccc1SKiyoshi Ueda 		/*
8952e93ccc1SKiyoshi Ueda 		 * Target requested pushing back the I/O.
8962e93ccc1SKiyoshi Ueda 		 */
897022c2611SMikulas Patocka 		spin_lock_irqsave(&md->deferred_lock, flags);
898bf14e2b2SDamien Le Moal 		if (__noflush_suspending(md) &&
899bf14e2b2SDamien Le Moal 		    !WARN_ON_ONCE(dm_is_zone_write(md, bio))) {
900745dc570SMike Snitzer 			/* NOTE early return due to BLK_STS_DM_REQUEUE below */
901bf14e2b2SDamien Le Moal 			bio_list_add_head(&md->deferred, bio);
902bf14e2b2SDamien Le Moal 		} else {
903bf14e2b2SDamien Le Moal 			/*
904bf14e2b2SDamien Le Moal 			 * noflush suspend was interrupted or this is
905bf14e2b2SDamien Le Moal 			 * a write to a zoned target.
906bf14e2b2SDamien Le Moal 			 */
9074e4cbee9SChristoph Hellwig 			io->status = BLK_STS_IOERR;
908bf14e2b2SDamien Le Moal 		}
909022c2611SMikulas Patocka 		spin_unlock_irqrestore(&md->deferred_lock, flags);
9102e93ccc1SKiyoshi Ueda 	}
9112e93ccc1SKiyoshi Ueda 
9124e4cbee9SChristoph Hellwig 	io_error = io->status;
91382f6cdccSMike Snitzer 	if (dm_io_flagged(io, DM_IO_ACCOUNTED))
914b992b40dSMing Lei 		dm_end_io_acct(io);
9150fbb4d93SMike Snitzer 	else if (!io_error) {
9160fbb4d93SMike Snitzer 		/*
9170fbb4d93SMike Snitzer 		 * Must handle target that DM_MAPIO_SUBMITTED only to
9180fbb4d93SMike Snitzer 		 * then bio_endio() rather than dm_submit_bio_remap()
9190fbb4d93SMike Snitzer 		 */
920b992b40dSMing Lei 		__dm_start_io_acct(io);
921b992b40dSMing Lei 		dm_end_io_acct(io);
9220fbb4d93SMike Snitzer 	}
9230119ab14SMike Snitzer 	free_io(io);
9249f6dc633SMike Snitzer 	smp_wmb();
9259f6dc633SMike Snitzer 	this_cpu_dec(*md->pending_io);
9262056a782SJens Axboe 
9279f6dc633SMike Snitzer 	/* nudge anyone waiting on suspend queue */
9289f6dc633SMike Snitzer 	if (unlikely(wq_has_sleeper(&md->wait)))
9299f6dc633SMike Snitzer 		wake_up(&md->wait);
9301da177e4SLinus Torvalds 
93152919840SMing Lei 	if (io_error == BLK_STS_DM_REQUEUE || io_error == BLK_STS_AGAIN) {
93252919840SMing Lei 		if (bio->bi_opf & REQ_POLLED) {
933b99fdcdcSMing Lei 			/*
93452919840SMing Lei 			 * Upper layer won't help us poll split bio (io->orig_bio
93552919840SMing Lei 			 * may only reflect a subset of the pre-split original)
93652919840SMing Lei 			 * so clear REQ_POLLED in case of requeue.
937b99fdcdcSMing Lei 			 */
938cfc97abcSMike Snitzer 			bio_clear_polled(bio);
93952919840SMing Lei 			if (io_error == BLK_STS_AGAIN) {
94052919840SMing Lei 				/* io_uring doesn't handle BLK_STS_AGAIN (yet) */
94152919840SMing Lei 				queue_io(md, bio);
942*78ccef91SMike Snitzer 				return;
94352919840SMing Lei 			}
94452919840SMing Lei 		}
945*78ccef91SMike Snitzer 		if (io_error == BLK_STS_DM_REQUEUE)
9466a8736d1STejun Heo 			return;
947b99fdcdcSMing Lei 	}
9486a8736d1STejun Heo 
9498d394bc4SMike Snitzer 	if (bio_is_flush_with_data(bio)) {
950af7e466aSMikulas Patocka 		/*
9516a8736d1STejun Heo 		 * Preflush done for flush with data, reissue
95228a8f0d3SMike Christie 		 * without REQ_PREFLUSH.
953af7e466aSMikulas Patocka 		 */
9541eff9d32SJens Axboe 		bio->bi_opf &= ~REQ_PREFLUSH;
9556a8736d1STejun Heo 		queue_io(md, bio);
956af7e466aSMikulas Patocka 	} else {
957b372d360SMike Snitzer 		/* done with normal IO or empty flush */
9588dd601faSNeilBrown 		if (io_error)
9594e4cbee9SChristoph Hellwig 			bio->bi_status = io_error;
9604246a0b6SChristoph Hellwig 		bio_endio(bio);
9612e93ccc1SKiyoshi Ueda 	}
9621da177e4SLinus Torvalds }
963e2736347SMike Snitzer 
964e2736347SMike Snitzer /*
965e2736347SMike Snitzer  * Decrements the number of outstanding ios that a bio has been
966e2736347SMike Snitzer  * cloned into, completing the original io if necc.
967e2736347SMike Snitzer  */
96884b98f4cSMike Snitzer static inline void __dm_io_dec_pending(struct dm_io *io)
969e2736347SMike Snitzer {
97084b98f4cSMike Snitzer 	if (atomic_dec_and_test(&io->io_count))
97184b98f4cSMike Snitzer 		dm_io_complete(io);
97284b98f4cSMike Snitzer }
97384b98f4cSMike Snitzer 
97484b98f4cSMike Snitzer static void dm_io_set_error(struct dm_io *io, blk_status_t error)
97584b98f4cSMike Snitzer {
976e2736347SMike Snitzer 	unsigned long flags;
97784b98f4cSMike Snitzer 
97884b98f4cSMike Snitzer 	/* Push-back supersedes any I/O errors */
9794d7bca13SMike Snitzer 	spin_lock_irqsave(&io->lock, flags);
980e2736347SMike Snitzer 	if (!(io->status == BLK_STS_DM_REQUEUE &&
98184b98f4cSMike Snitzer 	      __noflush_suspending(io->md))) {
982e2736347SMike Snitzer 		io->status = error;
98384b98f4cSMike Snitzer 	}
9844d7bca13SMike Snitzer 	spin_unlock_irqrestore(&io->lock, flags);
985e2736347SMike Snitzer }
986e2736347SMike Snitzer 
9872e803cd9SMing Lei static void dm_io_dec_pending(struct dm_io *io, blk_status_t error)
98884b98f4cSMike Snitzer {
98984b98f4cSMike Snitzer 	if (unlikely(error))
99084b98f4cSMike Snitzer 		dm_io_set_error(io, error);
99184b98f4cSMike Snitzer 
99284b98f4cSMike Snitzer 	__dm_io_dec_pending(io);
993af7e466aSMikulas Patocka }
9941da177e4SLinus Torvalds 
995bcb44433SMike Snitzer void disable_discard(struct mapped_device *md)
996bcb44433SMike Snitzer {
997bcb44433SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
998bcb44433SMike Snitzer 
999bcb44433SMike Snitzer 	/* device doesn't really support DISCARD, disable it */
1000bcb44433SMike Snitzer 	limits->max_discard_sectors = 0;
1001bcb44433SMike Snitzer }
1002bcb44433SMike Snitzer 
1003ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md)
1004ac62d620SChristoph Hellwig {
1005ac62d620SChristoph Hellwig 	struct queue_limits *limits = dm_get_queue_limits(md);
1006ac62d620SChristoph Hellwig 
1007ac62d620SChristoph Hellwig 	/* device doesn't really support WRITE ZEROES, disable it */
1008ac62d620SChristoph Hellwig 	limits->max_write_zeroes_sectors = 0;
1009ac62d620SChristoph Hellwig }
1010ac62d620SChristoph Hellwig 
1011a666e5c0SMikulas Patocka static bool swap_bios_limit(struct dm_target *ti, struct bio *bio)
1012a666e5c0SMikulas Patocka {
1013a666e5c0SMikulas Patocka 	return unlikely((bio->bi_opf & REQ_SWAP) != 0) && unlikely(ti->limit_swap_bios);
1014a666e5c0SMikulas Patocka }
1015a666e5c0SMikulas Patocka 
10164246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio)
10171da177e4SLinus Torvalds {
10184e4cbee9SChristoph Hellwig 	blk_status_t error = bio->bi_status;
10196cbce280SMike Snitzer 	struct dm_target_io *tio = clone_to_tio(bio);
10206cbce280SMike Snitzer 	struct dm_target *ti = tio->ti;
10216cbce280SMike Snitzer 	dm_endio_fn endio = ti->type->end_io;
10226cbce280SMike Snitzer 	struct dm_io *io = tio->io;
10236cbce280SMike Snitzer 	struct mapped_device *md = io->md;
10241da177e4SLinus Torvalds 
10259c37de29SMike Snitzer 	if (unlikely(error == BLK_STS_TARGET)) {
1026bcb44433SMike Snitzer 		if (bio_op(bio) == REQ_OP_DISCARD &&
102770200574SChristoph Hellwig 		    !bdev_max_discard_sectors(bio->bi_bdev))
1028bcb44433SMike Snitzer 			disable_discard(md);
1029bcb44433SMike Snitzer 		else if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
1030dddf3056SMike Snitzer 			 !bdev_write_zeroes_sectors(bio->bi_bdev))
1031ac62d620SChristoph Hellwig 			disable_write_zeroes(md);
1032ac62d620SChristoph Hellwig 	}
10337eee4ae2SMike Snitzer 
1034442761fdSMike Snitzer 	if (static_branch_unlikely(&zoned_enabled) &&
1035dddf3056SMike Snitzer 	    unlikely(blk_queue_is_zoned(bdev_get_queue(bio->bi_bdev))))
1036bb37d772SDamien Le Moal 		dm_zone_endio(io, bio);
1037415c79e1SJohannes Thumshirn 
10381be56909SChristoph Hellwig 	if (endio) {
10396cbce280SMike Snitzer 		int r = endio(ti, bio, &error);
10401be56909SChristoph Hellwig 		switch (r) {
10411be56909SChristoph Hellwig 		case DM_ENDIO_REQUEUE:
1042442761fdSMike Snitzer 			if (static_branch_unlikely(&zoned_enabled)) {
1043bf14e2b2SDamien Le Moal 				/*
1044bf14e2b2SDamien Le Moal 				 * Requeuing writes to a sequential zone of a zoned
1045bf14e2b2SDamien Le Moal 				 * target will break the sequential write pattern:
1046bf14e2b2SDamien Le Moal 				 * fail such IO.
1047bf14e2b2SDamien Le Moal 				 */
1048bf14e2b2SDamien Le Moal 				if (WARN_ON_ONCE(dm_is_zone_write(md, bio)))
1049bf14e2b2SDamien Le Moal 					error = BLK_STS_IOERR;
1050bf14e2b2SDamien Le Moal 				else
10514e4cbee9SChristoph Hellwig 					error = BLK_STS_DM_REQUEUE;
1052442761fdSMike Snitzer 			} else
1053442761fdSMike Snitzer 				error = BLK_STS_DM_REQUEUE;
1054df561f66SGustavo A. R. Silva 			fallthrough;
10551be56909SChristoph Hellwig 		case DM_ENDIO_DONE:
10561be56909SChristoph Hellwig 			break;
10571be56909SChristoph Hellwig 		case DM_ENDIO_INCOMPLETE:
10581be56909SChristoph Hellwig 			/* The target will handle the io */
10591be56909SChristoph Hellwig 			return;
10601be56909SChristoph Hellwig 		default:
10611be56909SChristoph Hellwig 			DMWARN("unimplemented target endio return value: %d", r);
10621be56909SChristoph Hellwig 			BUG();
10631be56909SChristoph Hellwig 		}
10641be56909SChristoph Hellwig 	}
10651be56909SChristoph Hellwig 
1066442761fdSMike Snitzer 	if (static_branch_unlikely(&swap_bios_enabled) &&
1067442761fdSMike Snitzer 	    unlikely(swap_bios_limit(ti, bio)))
1068a666e5c0SMikulas Patocka 		up(&md->swap_bios_semaphore);
1069a666e5c0SMikulas Patocka 
10701d1068ceSChristoph Hellwig 	free_tio(bio);
1071e2118b3cSDamien Le Moal 	dm_io_dec_pending(io, error);
10721da177e4SLinus Torvalds }
10731da177e4SLinus Torvalds 
107478d8e58aSMike Snitzer /*
107556a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
107656a67df7SMike Snitzer  * target boundary.
107756a67df7SMike Snitzer  */
10783720281dSMike Snitzer static inline sector_t max_io_len_target_boundary(struct dm_target *ti,
10793720281dSMike Snitzer 						  sector_t target_offset)
10801da177e4SLinus Torvalds {
108156a67df7SMike Snitzer 	return ti->len - target_offset;
108256a67df7SMike Snitzer }
108356a67df7SMike Snitzer 
10843720281dSMike Snitzer static sector_t max_io_len(struct dm_target *ti, sector_t sector)
108556a67df7SMike Snitzer {
10863720281dSMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
10873720281dSMike Snitzer 	sector_t len = max_io_len_target_boundary(ti, target_offset);
10885091cdecSMike Snitzer 	sector_t max_len;
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	/*
10913ee16db3SMike Snitzer 	 * Does the target need to split IO even further?
10923ee16db3SMike Snitzer 	 * - varied (per target) IO splitting is a tenet of DM; this
10933ee16db3SMike Snitzer 	 *   explains why stacked chunk_sectors based splitting via
10943ee16db3SMike Snitzer 	 *   blk_max_size_offset() isn't possible here. So pass in
10953ee16db3SMike Snitzer 	 *   ti->max_io_len to override stacked chunk_sectors.
10961da177e4SLinus Torvalds 	 */
10973ee16db3SMike Snitzer 	if (ti->max_io_len) {
109833bd6f06SMike Snitzer 		max_len = blk_max_size_offset(ti->table->md->queue,
10993ee16db3SMike Snitzer 					      target_offset, ti->max_io_len);
1100542f9038SMike Snitzer 		if (len > max_len)
1101542f9038SMike Snitzer 			len = max_len;
11023ee16db3SMike Snitzer 	}
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	return len;
11051da177e4SLinus Torvalds }
11061da177e4SLinus Torvalds 
1107542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1108542f9038SMike Snitzer {
1109542f9038SMike Snitzer 	if (len > UINT_MAX) {
1110542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1111542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
1112542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
1113542f9038SMike Snitzer 		return -EINVAL;
1114542f9038SMike Snitzer 	}
1115542f9038SMike Snitzer 
111675ae1936SMikulas Patocka 	ti->max_io_len = (uint32_t) len;
1117542f9038SMike Snitzer 
1118542f9038SMike Snitzer 	return 0;
1119542f9038SMike Snitzer }
1120542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1121542f9038SMike Snitzer 
1122f26c5719SDan Williams static struct dm_target *dm_dax_get_live_target(struct mapped_device *md,
1123f26c5719SDan Williams 						sector_t sector, int *srcu_idx)
11243d97c829SMike Snitzer 	__acquires(md->io_barrier)
1125545ed20eSToshi Kani {
1126545ed20eSToshi Kani 	struct dm_table *map;
1127545ed20eSToshi Kani 	struct dm_target *ti;
1128545ed20eSToshi Kani 
1129f26c5719SDan Williams 	map = dm_get_live_table(md, srcu_idx);
1130545ed20eSToshi Kani 	if (!map)
1131f26c5719SDan Williams 		return NULL;
1132545ed20eSToshi Kani 
1133545ed20eSToshi Kani 	ti = dm_table_find_target(map, sector);
1134123d87d5SMikulas Patocka 	if (!ti)
1135f26c5719SDan Williams 		return NULL;
1136f26c5719SDan Williams 
1137f26c5719SDan Williams 	return ti;
1138f26c5719SDan Williams }
1139f26c5719SDan Williams 
1140f26c5719SDan Williams static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
1141e511c4a3SJane Chu 		long nr_pages, enum dax_access_mode mode, void **kaddr,
1142e511c4a3SJane Chu 		pfn_t *pfn)
1143f26c5719SDan Williams {
1144f26c5719SDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
1145f26c5719SDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
1146f26c5719SDan Williams 	struct dm_target *ti;
1147f26c5719SDan Williams 	long len, ret = -EIO;
1148f26c5719SDan Williams 	int srcu_idx;
1149f26c5719SDan Williams 
1150f26c5719SDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1151f26c5719SDan Williams 
1152f26c5719SDan Williams 	if (!ti)
1153545ed20eSToshi Kani 		goto out;
1154f26c5719SDan Williams 	if (!ti->type->direct_access)
1155f26c5719SDan Williams 		goto out;
11563720281dSMike Snitzer 	len = max_io_len(ti, sector) / PAGE_SECTORS;
1157f26c5719SDan Williams 	if (len < 1)
1158f26c5719SDan Williams 		goto out;
1159f26c5719SDan Williams 	nr_pages = min(len, nr_pages);
1160e511c4a3SJane Chu 	ret = ti->type->direct_access(ti, pgoff, nr_pages, mode, kaddr, pfn);
1161817bf402SDan Williams 
1162545ed20eSToshi Kani  out:
1163545ed20eSToshi Kani 	dm_put_live_table(md, srcu_idx);
1164f26c5719SDan Williams 
1165f26c5719SDan Williams 	return ret;
1166545ed20eSToshi Kani }
1167545ed20eSToshi Kani 
1168cdf6cdcdSVivek Goyal static int dm_dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
1169cdf6cdcdSVivek Goyal 				  size_t nr_pages)
1170cdf6cdcdSVivek Goyal {
1171cdf6cdcdSVivek Goyal 	struct mapped_device *md = dax_get_private(dax_dev);
1172cdf6cdcdSVivek Goyal 	sector_t sector = pgoff * PAGE_SECTORS;
1173cdf6cdcdSVivek Goyal 	struct dm_target *ti;
1174cdf6cdcdSVivek Goyal 	int ret = -EIO;
1175cdf6cdcdSVivek Goyal 	int srcu_idx;
1176cdf6cdcdSVivek Goyal 
1177cdf6cdcdSVivek Goyal 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1178cdf6cdcdSVivek Goyal 
1179cdf6cdcdSVivek Goyal 	if (!ti)
1180cdf6cdcdSVivek Goyal 		goto out;
1181cdf6cdcdSVivek Goyal 	if (WARN_ON(!ti->type->dax_zero_page_range)) {
1182cdf6cdcdSVivek Goyal 		/*
1183cdf6cdcdSVivek Goyal 		 * ->zero_page_range() is mandatory dax operation. If we are
1184cdf6cdcdSVivek Goyal 		 *  here, something is wrong.
1185cdf6cdcdSVivek Goyal 		 */
1186cdf6cdcdSVivek Goyal 		goto out;
1187cdf6cdcdSVivek Goyal 	}
1188cdf6cdcdSVivek Goyal 	ret = ti->type->dax_zero_page_range(ti, pgoff, nr_pages);
1189cdf6cdcdSVivek Goyal  out:
1190cdf6cdcdSVivek Goyal 	dm_put_live_table(md, srcu_idx);
1191cdf6cdcdSVivek Goyal 
1192cdf6cdcdSVivek Goyal 	return ret;
1193cdf6cdcdSVivek Goyal }
1194cdf6cdcdSVivek Goyal 
1195047218ecSJane Chu static size_t dm_dax_recovery_write(struct dax_device *dax_dev, pgoff_t pgoff,
1196047218ecSJane Chu 		void *addr, size_t bytes, struct iov_iter *i)
1197047218ecSJane Chu {
1198047218ecSJane Chu 	struct mapped_device *md = dax_get_private(dax_dev);
1199047218ecSJane Chu 	sector_t sector = pgoff * PAGE_SECTORS;
1200047218ecSJane Chu 	struct dm_target *ti;
1201047218ecSJane Chu 	int srcu_idx;
1202047218ecSJane Chu 	long ret = 0;
1203047218ecSJane Chu 
1204047218ecSJane Chu 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1205047218ecSJane Chu 	if (!ti || !ti->type->dax_recovery_write)
1206047218ecSJane Chu 		goto out;
1207047218ecSJane Chu 
1208047218ecSJane Chu 	ret = ti->type->dax_recovery_write(ti, pgoff, addr, bytes, i);
1209047218ecSJane Chu out:
1210047218ecSJane Chu 	dm_put_live_table(md, srcu_idx);
1211047218ecSJane Chu 	return ret;
1212047218ecSJane Chu }
1213047218ecSJane Chu 
12141dd40c3eSMikulas Patocka /*
12151dd40c3eSMikulas Patocka  * A target may call dm_accept_partial_bio only from the map routine.  It is
12166842d264SDamien Le Moal  * allowed for all bio types except REQ_PREFLUSH, REQ_OP_ZONE_* zone management
1217e6fc9f62SMike Snitzer  * operations, REQ_OP_ZONE_APPEND (zone append writes) and any bio serviced by
1218e6fc9f62SMike Snitzer  * __send_duplicate_bios().
12191dd40c3eSMikulas Patocka  *
12201dd40c3eSMikulas Patocka  * dm_accept_partial_bio informs the dm that the target only wants to process
12211dd40c3eSMikulas Patocka  * additional n_sectors sectors of the bio and the rest of the data should be
12221dd40c3eSMikulas Patocka  * sent in a next bio.
12231dd40c3eSMikulas Patocka  *
12241dd40c3eSMikulas Patocka  * A diagram that explains the arithmetics:
12251dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
12261dd40c3eSMikulas Patocka  * |         1          |       2       |   3   |
12271dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
12281dd40c3eSMikulas Patocka  *
12291dd40c3eSMikulas Patocka  * <-------------- *tio->len_ptr --------------->
1230bdb34759SMike Snitzer  *                      <----- bio_sectors ----->
12311dd40c3eSMikulas Patocka  *                      <-- n_sectors -->
12321dd40c3eSMikulas Patocka  *
12331dd40c3eSMikulas Patocka  * Region 1 was already iterated over with bio_advance or similar function.
12341dd40c3eSMikulas Patocka  *	(it may be empty if the target doesn't use bio_advance)
12351dd40c3eSMikulas Patocka  * Region 2 is the remaining bio size that the target wants to process.
12361dd40c3eSMikulas Patocka  *	(it may be empty if region 1 is non-empty, although there is no reason
12371dd40c3eSMikulas Patocka  *	 to make it empty)
12381dd40c3eSMikulas Patocka  * The target requires that region 3 is to be sent in the next bio.
12391dd40c3eSMikulas Patocka  *
12401dd40c3eSMikulas Patocka  * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
12411dd40c3eSMikulas Patocka  * the partially processed part (the sum of regions 1+2) must be the same for all
12421dd40c3eSMikulas Patocka  * copies of the bio.
12431dd40c3eSMikulas Patocka  */
12441dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
12451dd40c3eSMikulas Patocka {
12466c23f0bdSChristoph Hellwig 	struct dm_target_io *tio = clone_to_tio(bio);
1247bdb34759SMike Snitzer 	unsigned bio_sectors = bio_sectors(bio);
12486842d264SDamien Le Moal 
1249655f3aadSMike Snitzer 	BUG_ON(dm_tio_flagged(tio, DM_TIO_IS_DUPLICATE_BIO));
12506842d264SDamien Le Moal 	BUG_ON(op_is_zone_mgmt(bio_op(bio)));
12516842d264SDamien Le Moal 	BUG_ON(bio_op(bio) == REQ_OP_ZONE_APPEND);
1252bdb34759SMike Snitzer 	BUG_ON(bio_sectors > *tio->len_ptr);
1253bdb34759SMike Snitzer 	BUG_ON(n_sectors > bio_sectors);
12546842d264SDamien Le Moal 
1255bdb34759SMike Snitzer 	*tio->len_ptr -= bio_sectors - n_sectors;
12561dd40c3eSMikulas Patocka 	bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
12577dd76d1fSMing Lei 
12587dd76d1fSMing Lei 	/*
12597dd76d1fSMing Lei 	 * __split_and_process_bio() may have already saved mapped part
12607dd76d1fSMing Lei 	 * for accounting but it is being reduced so update accordingly.
12617dd76d1fSMing Lei 	 */
12627dd76d1fSMing Lei 	dm_io_set_flag(tio->io, DM_IO_WAS_SPLIT);
12637dd76d1fSMing Lei 	tio->io->sectors = n_sectors;
12641dd40c3eSMikulas Patocka }
12651dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
12661dd40c3eSMikulas Patocka 
12670fbb4d93SMike Snitzer /*
12680fbb4d93SMike Snitzer  * @clone: clone bio that DM core passed to target's .map function
12690fbb4d93SMike Snitzer  * @tgt_clone: clone of @clone bio that target needs submitted
12700fbb4d93SMike Snitzer  *
12710fbb4d93SMike Snitzer  * Targets should use this interface to submit bios they take
12720fbb4d93SMike Snitzer  * ownership of when returning DM_MAPIO_SUBMITTED.
12730fbb4d93SMike Snitzer  *
12740fbb4d93SMike Snitzer  * Target should also enable ti->accounts_remapped_io
12750fbb4d93SMike Snitzer  */
1276b7f8dff0SMike Snitzer void dm_submit_bio_remap(struct bio *clone, struct bio *tgt_clone)
12770fbb4d93SMike Snitzer {
12780fbb4d93SMike Snitzer 	struct dm_target_io *tio = clone_to_tio(clone);
12790fbb4d93SMike Snitzer 	struct dm_io *io = tio->io;
12800fbb4d93SMike Snitzer 
12810fbb4d93SMike Snitzer 	/* establish bio that will get submitted */
12820fbb4d93SMike Snitzer 	if (!tgt_clone)
12830fbb4d93SMike Snitzer 		tgt_clone = clone;
12840fbb4d93SMike Snitzer 
12850fbb4d93SMike Snitzer 	/*
12860fbb4d93SMike Snitzer 	 * Account io->origin_bio to DM dev on behalf of target
12870fbb4d93SMike Snitzer 	 * that took ownership of IO with DM_MAPIO_SUBMITTED.
12880fbb4d93SMike Snitzer 	 */
12890fbb4d93SMike Snitzer 	dm_start_io_acct(io, clone);
12900fbb4d93SMike Snitzer 
12919d20653fSMike Snitzer 	trace_block_bio_remap(tgt_clone, disk_devt(io->md->disk),
12920fbb4d93SMike Snitzer 			      tio->old_sector);
12939d20653fSMike Snitzer 	submit_bio_noacct(tgt_clone);
12940fbb4d93SMike Snitzer }
12950fbb4d93SMike Snitzer EXPORT_SYMBOL_GPL(dm_submit_bio_remap);
12960fbb4d93SMike Snitzer 
1297a666e5c0SMikulas Patocka static noinline void __set_swap_bios_limit(struct mapped_device *md, int latch)
1298a666e5c0SMikulas Patocka {
1299a666e5c0SMikulas Patocka 	mutex_lock(&md->swap_bios_lock);
1300a666e5c0SMikulas Patocka 	while (latch < md->swap_bios) {
1301a666e5c0SMikulas Patocka 		cond_resched();
1302a666e5c0SMikulas Patocka 		down(&md->swap_bios_semaphore);
1303a666e5c0SMikulas Patocka 		md->swap_bios--;
1304a666e5c0SMikulas Patocka 	}
1305a666e5c0SMikulas Patocka 	while (latch > md->swap_bios) {
1306a666e5c0SMikulas Patocka 		cond_resched();
1307a666e5c0SMikulas Patocka 		up(&md->swap_bios_semaphore);
1308a666e5c0SMikulas Patocka 		md->swap_bios++;
1309a666e5c0SMikulas Patocka 	}
1310a666e5c0SMikulas Patocka 	mutex_unlock(&md->swap_bios_lock);
1311a666e5c0SMikulas Patocka }
1312a666e5c0SMikulas Patocka 
13131561b396SChristoph Hellwig static void __map_bio(struct bio *clone)
13141da177e4SLinus Torvalds {
13151561b396SChristoph Hellwig 	struct dm_target_io *tio = clone_to_tio(clone);
1316bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
13176cbce280SMike Snitzer 	struct dm_io *io = tio->io;
13186cbce280SMike Snitzer 	struct mapped_device *md = io->md;
13196cbce280SMike Snitzer 	int r;
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 	/*
13240fbb4d93SMike Snitzer 	 * Map the clone.
13251da177e4SLinus Torvalds 	 */
1326743598f0SMike Snitzer 	tio->old_sector = clone->bi_iter.bi_sector;
1327d67a5f4bSMikulas Patocka 
1328442761fdSMike Snitzer 	if (static_branch_unlikely(&swap_bios_enabled) &&
1329442761fdSMike Snitzer 	    unlikely(swap_bios_limit(ti, clone))) {
1330a666e5c0SMikulas Patocka 		int latch = get_swap_bios();
1331a666e5c0SMikulas Patocka 		if (unlikely(latch != md->swap_bios))
1332a666e5c0SMikulas Patocka 			__set_swap_bios_limit(md, latch);
1333a666e5c0SMikulas Patocka 		down(&md->swap_bios_semaphore);
1334a666e5c0SMikulas Patocka 	}
1335a666e5c0SMikulas Patocka 
1336442761fdSMike Snitzer 	if (static_branch_unlikely(&zoned_enabled)) {
1337bb37d772SDamien Le Moal 		/*
1338442761fdSMike Snitzer 		 * Check if the IO needs a special mapping due to zone append
1339442761fdSMike Snitzer 		 * emulation on zoned target. In this case, dm_zone_map_bio()
1340442761fdSMike Snitzer 		 * calls the target map operation.
1341bb37d772SDamien Le Moal 		 */
13426cbce280SMike Snitzer 		if (unlikely(dm_emulate_zone_append(md)))
1343bb37d772SDamien Le Moal 			r = dm_zone_map_bio(tio);
1344bb37d772SDamien Le Moal 		else
13457de3ee57SMikulas Patocka 			r = ti->type->map(ti, clone);
1346442761fdSMike Snitzer 	} else
1347442761fdSMike Snitzer 		r = ti->type->map(ti, clone);
1348bb37d772SDamien Le Moal 
1349846785e6SChristoph Hellwig 	switch (r) {
1350846785e6SChristoph Hellwig 	case DM_MAPIO_SUBMITTED:
13510fbb4d93SMike Snitzer 		/* target has assumed ownership of this io */
13520fbb4d93SMike Snitzer 		if (!ti->accounts_remapped_io)
13539d20653fSMike Snitzer 			dm_start_io_acct(io, clone);
1354846785e6SChristoph Hellwig 		break;
1355846785e6SChristoph Hellwig 	case DM_MAPIO_REMAPPED:
13569d20653fSMike Snitzer 		dm_submit_bio_remap(clone, NULL);
1357846785e6SChristoph Hellwig 		break;
1358846785e6SChristoph Hellwig 	case DM_MAPIO_KILL:
1359846785e6SChristoph Hellwig 	case DM_MAPIO_REQUEUE:
1360442761fdSMike Snitzer 		if (static_branch_unlikely(&swap_bios_enabled) &&
1361442761fdSMike Snitzer 		    unlikely(swap_bios_limit(ti, clone)))
13626cbce280SMike Snitzer 			up(&md->swap_bios_semaphore);
13631d1068ceSChristoph Hellwig 		free_tio(clone);
136490a2326eSMike Snitzer 		if (r == DM_MAPIO_KILL)
136590a2326eSMike Snitzer 			dm_io_dec_pending(io, BLK_STS_IOERR);
136690a2326eSMike Snitzer 		else
1367e2118b3cSDamien Le Moal 			dm_io_dec_pending(io, BLK_STS_DM_REQUEUE);
1368846785e6SChristoph Hellwig 		break;
1369846785e6SChristoph Hellwig 	default:
137045cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
137145cbcd79SKiyoshi Ueda 		BUG();
13721da177e4SLinus Torvalds 	}
13731da177e4SLinus Torvalds }
13741da177e4SLinus Torvalds 
13757dd76d1fSMing Lei static void setup_split_accounting(struct clone_info *ci, unsigned len)
13767dd76d1fSMing Lei {
13777dd76d1fSMing Lei 	struct dm_io *io = ci->io;
13787dd76d1fSMing Lei 
13797dd76d1fSMing Lei 	if (ci->sector_count > len) {
13807dd76d1fSMing Lei 		/*
13817dd76d1fSMing Lei 		 * Split needed, save the mapped part for accounting.
13827dd76d1fSMing Lei 		 * NOTE: dm_accept_partial_bio() will update accordingly.
13837dd76d1fSMing Lei 		 */
13847dd76d1fSMing Lei 		dm_io_set_flag(io, DM_IO_WAS_SPLIT);
13857dd76d1fSMing Lei 		io->sectors = len;
13867dd76d1fSMing Lei 	}
13877dd76d1fSMing Lei 
13887dd76d1fSMing Lei 	if (static_branch_unlikely(&stats_enabled) &&
13897dd76d1fSMing Lei 	    unlikely(dm_stats_used(&io->md->stats))) {
13907dd76d1fSMing Lei 		/*
13917dd76d1fSMing Lei 		 * Save bi_sector in terms of its offset from end of
13927dd76d1fSMing Lei 		 * original bio, only needed for DM-stats' benefit.
13937dd76d1fSMing Lei 		 * - saved regardless of whether split needed so that
13947dd76d1fSMing Lei 		 *   dm_accept_partial_bio() doesn't need to.
13957dd76d1fSMing Lei 		 */
13967dd76d1fSMing Lei 		io->sector_offset = bio_end_sector(ci->bio) - ci->sector;
13977dd76d1fSMing Lei 	}
13987dd76d1fSMing Lei }
13997dd76d1fSMing Lei 
1400318716ddSMike Snitzer static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci,
14017dd06a25SMike Snitzer 				struct dm_target *ti, unsigned num_bios)
1402f9ab94ceSMikulas Patocka {
14031d1068ceSChristoph Hellwig 	struct bio *bio;
1404318716ddSMike Snitzer 	int try;
1405dba14160SMikulas Patocka 
1406318716ddSMike Snitzer 	for (try = 0; try < 2; try++) {
1407318716ddSMike Snitzer 		int bio_nr;
1408318716ddSMike Snitzer 
1409318716ddSMike Snitzer 		if (try)
1410bc02cdbeSMike Snitzer 			mutex_lock(&ci->io->md->table_devices_lock);
1411318716ddSMike Snitzer 		for (bio_nr = 0; bio_nr < num_bios; bio_nr++) {
14127dd06a25SMike Snitzer 			bio = alloc_tio(ci, ti, bio_nr, NULL,
1413dc8e2021SChristoph Hellwig 					try ? GFP_NOIO : GFP_NOWAIT);
14141d1068ceSChristoph Hellwig 			if (!bio)
1415318716ddSMike Snitzer 				break;
1416318716ddSMike Snitzer 
14171d1068ceSChristoph Hellwig 			bio_list_add(blist, bio);
1418318716ddSMike Snitzer 		}
1419318716ddSMike Snitzer 		if (try)
1420bc02cdbeSMike Snitzer 			mutex_unlock(&ci->io->md->table_devices_lock);
1421318716ddSMike Snitzer 		if (bio_nr == num_bios)
1422318716ddSMike Snitzer 			return;
1423318716ddSMike Snitzer 
14246c23f0bdSChristoph Hellwig 		while ((bio = bio_list_pop(blist)))
14251d1068ceSChristoph Hellwig 			free_tio(bio);
1426318716ddSMike Snitzer 	}
1427318716ddSMike Snitzer }
1428f9ab94ceSMikulas Patocka 
14290f14d60aSMing Lei static int __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
14301dd40c3eSMikulas Patocka 				  unsigned num_bios, unsigned *len)
143106a426ceSMike Snitzer {
1432318716ddSMike Snitzer 	struct bio_list blist = BIO_EMPTY_LIST;
14338eabf5d0SChristoph Hellwig 	struct bio *clone;
14340f14d60aSMing Lei 	int ret = 0;
143506a426ceSMike Snitzer 
1436891fced6SChristoph Hellwig 	switch (num_bios) {
1437891fced6SChristoph Hellwig 	case 0:
1438891fced6SChristoph Hellwig 		break;
1439891fced6SChristoph Hellwig 	case 1:
14407dd76d1fSMing Lei 		if (len)
14417dd76d1fSMing Lei 			setup_split_accounting(ci, *len);
1442891fced6SChristoph Hellwig 		clone = alloc_tio(ci, ti, 0, len, GFP_NOIO);
1443891fced6SChristoph Hellwig 		__map_bio(clone);
14440f14d60aSMing Lei 		ret = 1;
1445891fced6SChristoph Hellwig 		break;
1446891fced6SChristoph Hellwig 	default:
14477dd06a25SMike Snitzer 		/* dm_accept_partial_bio() is not supported with shared tio->len_ptr */
14487dd06a25SMike Snitzer 		alloc_multiple_bios(&blist, ci, ti, num_bios);
14498eabf5d0SChristoph Hellwig 		while ((clone = bio_list_pop(&blist))) {
1450655f3aadSMike Snitzer 			dm_tio_set_flag(clone_to_tio(clone), DM_TIO_IS_DUPLICATE_BIO);
14511561b396SChristoph Hellwig 			__map_bio(clone);
14520f14d60aSMing Lei 			ret += 1;
1453f9ab94ceSMikulas Patocka 		}
1454891fced6SChristoph Hellwig 		break;
1455318716ddSMike Snitzer 	}
14560f14d60aSMing Lei 
14570f14d60aSMing Lei 	return ret;
145806a426ceSMike Snitzer }
145906a426ceSMike Snitzer 
1460332f2b1eSMike Snitzer static void __send_empty_flush(struct clone_info *ci)
1461f9ab94ceSMikulas Patocka {
146206a426ceSMike Snitzer 	unsigned target_nr = 0;
1463f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1464828678b8SMike Snitzer 	struct bio flush_bio;
1465828678b8SMike Snitzer 
1466828678b8SMike Snitzer 	/*
1467828678b8SMike Snitzer 	 * Use an on-stack bio for this, it's safe since we don't
1468828678b8SMike Snitzer 	 * need to reference it after submit. It's just used as
1469828678b8SMike Snitzer 	 * the basis for the clone(s).
1470828678b8SMike Snitzer 	 */
147149add496SChristoph Hellwig 	bio_init(&flush_bio, ci->io->md->disk->part0, NULL, 0,
147249add496SChristoph Hellwig 		 REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC);
147347d95102SChristoph Hellwig 
1474828678b8SMike Snitzer 	ci->bio = &flush_bio;
1475828678b8SMike Snitzer 	ci->sector_count = 0;
147692b914e2SShin'ichiro Kawasaki 	ci->io->tio.clone.bi_iter.bi_size = 0;
1477f9ab94ceSMikulas Patocka 
14780f14d60aSMing Lei 	while ((ti = dm_table_get_target(ci->map, target_nr++))) {
14790f14d60aSMing Lei 		int bios;
14800f14d60aSMing Lei 
14810f14d60aSMing Lei 		atomic_add(ti->num_flush_bios, &ci->io->io_count);
14820f14d60aSMing Lei 		bios = __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
14830f14d60aSMing Lei 		atomic_sub(ti->num_flush_bios - bios, &ci->io->io_count);
14840f14d60aSMing Lei 	}
14850f14d60aSMing Lei 
14860f14d60aSMing Lei 	/*
14870f14d60aSMing Lei 	 * alloc_io() takes one extra reference for submission, so the
14880f14d60aSMing Lei 	 * reference won't reach 0 without the following subtraction
14890f14d60aSMing Lei 	 */
14900f14d60aSMing Lei 	atomic_sub(1, &ci->io->io_count);
1491828678b8SMike Snitzer 
1492828678b8SMike Snitzer 	bio_uninit(ci->bio);
1493f9ab94ceSMikulas Patocka }
1494f9ab94ceSMikulas Patocka 
1495e6fc9f62SMike Snitzer static void __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti,
149661697a6aSMike Snitzer 					unsigned num_bios)
14975ae89a87SMike Snitzer {
149851b86f9aSMichael Lass 	unsigned len;
14990f14d60aSMing Lei 	int bios;
15005ae89a87SMike Snitzer 
15013720281dSMike Snitzer 	len = min_t(sector_t, ci->sector_count,
15023720281dSMike Snitzer 		    max_io_len_target_boundary(ti, dm_target_offset(ti, ci->sector)));
150351b86f9aSMichael Lass 
15040f14d60aSMing Lei 	atomic_add(num_bios, &ci->io->io_count);
15050f14d60aSMing Lei 	bios = __send_duplicate_bios(ci, ti, num_bios, &len);
15060f14d60aSMing Lei 	/*
15070f14d60aSMing Lei 	 * alloc_io() takes one extra reference for submission, so the
15080f14d60aSMing Lei 	 * reference won't reach 0 without the following (+1) subtraction
15090f14d60aSMing Lei 	 */
15100f14d60aSMing Lei 	atomic_sub(num_bios - bios + 1, &ci->io->io_count);
15117dd06a25SMike Snitzer 
1512a79245b3SMike Snitzer 	ci->sector += len;
15133d7f4562SMike Snitzer 	ci->sector_count -= len;
15145ae89a87SMike Snitzer }
15155ae89a87SMike Snitzer 
1516568c73a3SMike Snitzer static bool is_abnormal_io(struct bio *bio)
1517568c73a3SMike Snitzer {
15184edadf6dSMike Snitzer 	unsigned int op = bio_op(bio);
1519568c73a3SMike Snitzer 
15204edadf6dSMike Snitzer 	if (op != REQ_OP_READ && op != REQ_OP_WRITE && op != REQ_OP_FLUSH) {
15214edadf6dSMike Snitzer 		switch (op) {
1522568c73a3SMike Snitzer 		case REQ_OP_DISCARD:
1523568c73a3SMike Snitzer 		case REQ_OP_SECURE_ERASE:
1524568c73a3SMike Snitzer 		case REQ_OP_WRITE_ZEROES:
15254edadf6dSMike Snitzer 			return true;
15264edadf6dSMike Snitzer 		default:
1527568c73a3SMike Snitzer 			break;
1528568c73a3SMike Snitzer 		}
1529568c73a3SMike Snitzer 	}
1530568c73a3SMike Snitzer 
15314edadf6dSMike Snitzer 	return false;
15324edadf6dSMike Snitzer }
15334edadf6dSMike Snitzer 
15344edadf6dSMike Snitzer static blk_status_t __process_abnormal_io(struct clone_info *ci,
15354edadf6dSMike Snitzer 					  struct dm_target *ti)
15360519c71eSMike Snitzer {
15379679b5a7SMike Snitzer 	unsigned num_bios = 0;
15380519c71eSMike Snitzer 
1539e6fc9f62SMike Snitzer 	switch (bio_op(ci->bio)) {
15409679b5a7SMike Snitzer 	case REQ_OP_DISCARD:
15419679b5a7SMike Snitzer 		num_bios = ti->num_discard_bios;
15429679b5a7SMike Snitzer 		break;
15439679b5a7SMike Snitzer 	case REQ_OP_SECURE_ERASE:
15449679b5a7SMike Snitzer 		num_bios = ti->num_secure_erase_bios;
15459679b5a7SMike Snitzer 		break;
15469679b5a7SMike Snitzer 	case REQ_OP_WRITE_ZEROES:
15479679b5a7SMike Snitzer 		num_bios = ti->num_write_zeroes_bios;
15489679b5a7SMike Snitzer 		break;
15499679b5a7SMike Snitzer 	}
15500519c71eSMike Snitzer 
1551e6fc9f62SMike Snitzer 	/*
1552e6fc9f62SMike Snitzer 	 * Even though the device advertised support for this type of
1553e6fc9f62SMike Snitzer 	 * request, that does not mean every target supports it, and
1554e6fc9f62SMike Snitzer 	 * reconfiguration might also have changed that since the
1555e6fc9f62SMike Snitzer 	 * check was performed.
1556e6fc9f62SMike Snitzer 	 */
155784b98f4cSMike Snitzer 	if (unlikely(!num_bios))
15584edadf6dSMike Snitzer 		return BLK_STS_NOTSUPP;
15594edadf6dSMike Snitzer 
1560e6fc9f62SMike Snitzer 	__send_changing_extent_only(ci, ti, num_bios);
15614edadf6dSMike Snitzer 	return BLK_STS_OK;
15620519c71eSMike Snitzer }
15630519c71eSMike Snitzer 
1564e4c93811SAlasdair G Kergon /*
1565ec211631SMing Lei  * Reuse ->bi_private as dm_io list head for storing all dm_io instances
1566b99fdcdcSMing Lei  * associated with this bio, and this bio's bi_private needs to be
1567b99fdcdcSMing Lei  * stored in dm_io->data before the reuse.
1568b99fdcdcSMing Lei  *
1569b99fdcdcSMing Lei  * bio->bi_private is owned by fs or upper layer, so block layer won't
1570b99fdcdcSMing Lei  * touch it after splitting. Meantime it won't be changed by anyone after
1571b99fdcdcSMing Lei  * bio is submitted. So this reuse is safe.
1572b99fdcdcSMing Lei  */
1573ec211631SMing Lei static inline struct dm_io **dm_poll_list_head(struct bio *bio)
1574b99fdcdcSMing Lei {
1575ec211631SMing Lei 	return (struct dm_io **)&bio->bi_private;
1576b99fdcdcSMing Lei }
1577b99fdcdcSMing Lei 
1578b99fdcdcSMing Lei static void dm_queue_poll_io(struct bio *bio, struct dm_io *io)
1579b99fdcdcSMing Lei {
1580ec211631SMing Lei 	struct dm_io **head = dm_poll_list_head(bio);
1581b99fdcdcSMing Lei 
1582b99fdcdcSMing Lei 	if (!(bio->bi_opf & REQ_DM_POLL_LIST)) {
1583b99fdcdcSMing Lei 		bio->bi_opf |= REQ_DM_POLL_LIST;
1584b99fdcdcSMing Lei 		/*
1585b99fdcdcSMing Lei 		 * Save .bi_private into dm_io, so that we can reuse
1586ec211631SMing Lei 		 * .bi_private as dm_io list head for storing dm_io list
1587b99fdcdcSMing Lei 		 */
1588b99fdcdcSMing Lei 		io->data = bio->bi_private;
1589b99fdcdcSMing Lei 
1590b99fdcdcSMing Lei 		/* tell block layer to poll for completion */
1591b99fdcdcSMing Lei 		bio->bi_cookie = ~BLK_QC_T_NONE;
1592ec211631SMing Lei 
1593ec211631SMing Lei 		io->next = NULL;
1594b99fdcdcSMing Lei 	} else {
1595b99fdcdcSMing Lei 		/*
1596b99fdcdcSMing Lei 		 * bio recursed due to split, reuse original poll list,
1597b99fdcdcSMing Lei 		 * and save bio->bi_private too.
1598b99fdcdcSMing Lei 		 */
1599ec211631SMing Lei 		io->data = (*head)->data;
1600ec211631SMing Lei 		io->next = *head;
1601b99fdcdcSMing Lei 	}
1602b99fdcdcSMing Lei 
1603ec211631SMing Lei 	*head = io;
1604b99fdcdcSMing Lei }
1605b99fdcdcSMing Lei 
1606b99fdcdcSMing Lei /*
1607e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1608e4c93811SAlasdair G Kergon  */
160984b98f4cSMike Snitzer static blk_status_t __split_and_process_bio(struct clone_info *ci)
1610e4c93811SAlasdair G Kergon {
161166bdaa43SMike Snitzer 	struct bio *clone;
1612e4c93811SAlasdair G Kergon 	struct dm_target *ti;
16131c3b13e6SKent Overstreet 	unsigned len;
1614e4c93811SAlasdair G Kergon 
1615e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
16164edadf6dSMike Snitzer 	if (unlikely(!ti))
16174edadf6dSMike Snitzer 		return BLK_STS_IOERR;
16181ee88de3SMikulas Patocka 
16191ee88de3SMikulas Patocka 	if (unlikely((ci->bio->bi_opf & REQ_NOWAIT) != 0) &&
16201ee88de3SMikulas Patocka 	    unlikely(!dm_target_supports_nowait(ti->type)))
16211ee88de3SMikulas Patocka 		return BLK_STS_NOTSUPP;
16221ee88de3SMikulas Patocka 
16231ee88de3SMikulas Patocka 	if (unlikely(ci->is_abnormal_io))
16244edadf6dSMike Snitzer 		return __process_abnormal_io(ci, ti);
16253d7f4562SMike Snitzer 
1626b99fdcdcSMing Lei 	/*
1627b99fdcdcSMing Lei 	 * Only support bio polling for normal IO, and the target io is
1628b99fdcdcSMing Lei 	 * exactly inside the dm_io instance (verified in dm_poll_dm_io)
1629b99fdcdcSMing Lei 	 */
1630b99fdcdcSMing Lei 	ci->submit_as_polled = ci->bio->bi_opf & REQ_POLLED;
1631e4c93811SAlasdair G Kergon 
1632e4c93811SAlasdair G Kergon 	len = min_t(sector_t, max_io_len(ti, ci->sector), ci->sector_count);
16337dd76d1fSMing Lei 	setup_split_accounting(ci, len);
163466bdaa43SMike Snitzer 	clone = alloc_tio(ci, ti, 0, &len, GFP_NOIO);
163566bdaa43SMike Snitzer 	__map_bio(clone);
1636e4c93811SAlasdair G Kergon 
1637e4c93811SAlasdair G Kergon 	ci->sector += len;
1638e4c93811SAlasdair G Kergon 	ci->sector_count -= len;
1639e4c93811SAlasdair G Kergon 
164084b98f4cSMike Snitzer 	return BLK_STS_OK;
1641e4c93811SAlasdair G Kergon }
1642e4c93811SAlasdair G Kergon 
1643978e51baSMike Snitzer static void init_clone_info(struct clone_info *ci, struct mapped_device *md,
16444edadf6dSMike Snitzer 			    struct dm_table *map, struct bio *bio, bool is_abnormal)
1645978e51baSMike Snitzer {
1646978e51baSMike Snitzer 	ci->map = map;
1647978e51baSMike Snitzer 	ci->io = alloc_io(md, bio);
1648d41e077aSMike Snitzer 	ci->bio = bio;
16494edadf6dSMike Snitzer 	ci->is_abnormal_io = is_abnormal;
1650b99fdcdcSMing Lei 	ci->submit_as_polled = false;
1651978e51baSMike Snitzer 	ci->sector = bio->bi_iter.bi_sector;
1652d41e077aSMike Snitzer 	ci->sector_count = bio_sectors(bio);
1653d41e077aSMike Snitzer 
1654d41e077aSMike Snitzer 	/* Shouldn't happen but sector_count was being set to 0 so... */
1655442761fdSMike Snitzer 	if (static_branch_unlikely(&zoned_enabled) &&
1656442761fdSMike Snitzer 	    WARN_ON_ONCE(op_is_zone_mgmt(bio_op(bio)) && ci->sector_count))
1657d41e077aSMike Snitzer 		ci->sector_count = 0;
1658978e51baSMike Snitzer }
1659978e51baSMike Snitzer 
1660e4c93811SAlasdair G Kergon /*
166114fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
16621da177e4SLinus Torvalds  */
166396c9865cSMike Snitzer static void dm_split_and_process_bio(struct mapped_device *md,
166483d5e5b0SMikulas Patocka 				     struct dm_table *map, struct bio *bio)
16651da177e4SLinus Torvalds {
16661da177e4SLinus Torvalds 	struct clone_info ci;
16674857abf6SMike Snitzer 	struct dm_io *io;
166884b98f4cSMike Snitzer 	blk_status_t error = BLK_STS_OK;
16694edadf6dSMike Snitzer 	bool is_abnormal;
16701da177e4SLinus Torvalds 
16714edadf6dSMike Snitzer 	is_abnormal = is_abnormal_io(bio);
16724edadf6dSMike Snitzer 	if (unlikely(is_abnormal)) {
16734edadf6dSMike Snitzer 		/*
16744edadf6dSMike Snitzer 		 * Use blk_queue_split() for abnormal IO (e.g. discard, etc)
16754edadf6dSMike Snitzer 		 * otherwise associated queue_limits won't be imposed.
16764edadf6dSMike Snitzer 		 */
16774edadf6dSMike Snitzer 		blk_queue_split(&bio);
16784edadf6dSMike Snitzer 	}
16794edadf6dSMike Snitzer 
16804edadf6dSMike Snitzer 	init_clone_info(&ci, md, map, bio, is_abnormal);
16814857abf6SMike Snitzer 	io = ci.io;
1682bd2a49b8SAlasdair G Kergon 
16831eff9d32SJens Axboe 	if (bio->bi_opf & REQ_PREFLUSH) {
1684332f2b1eSMike Snitzer 		__send_empty_flush(&ci);
1685e2736347SMike Snitzer 		/* dm_io_complete submits any data associated with flush */
1686d41e077aSMike Snitzer 		goto out;
1687d41e077aSMike Snitzer 	}
1688d41e077aSMike Snitzer 
168996c9865cSMike Snitzer 	error = __split_and_process_bio(&ci);
1690d41e077aSMike Snitzer 	if (error || !ci.sector_count)
1691d41e077aSMike Snitzer 		goto out;
169218a25da8SNeilBrown 	/*
1693d41e077aSMike Snitzer 	 * Remainder must be passed to submit_bio_noacct() so it gets handled
1694d41e077aSMike Snitzer 	 * *after* bios already submitted have been completely processed.
169518a25da8SNeilBrown 	 */
16967dd76d1fSMing Lei 	bio_trim(bio, io->sectors, ci.sector_count);
16977dd76d1fSMing Lei 	trace_block_split(bio, bio->bi_iter.bi_sector);
16987dd76d1fSMing Lei 	bio_inc_remaining(bio);
16993e08773cSChristoph Hellwig 	submit_bio_noacct(bio);
1700d41e077aSMike Snitzer out:
1701b99fdcdcSMing Lei 	/*
1702b99fdcdcSMing Lei 	 * Drop the extra reference count for non-POLLED bio, and hold one
1703b99fdcdcSMing Lei 	 * reference for POLLED bio, which will be released in dm_poll_bio
1704b99fdcdcSMing Lei 	 *
1705ec211631SMing Lei 	 * Add every dm_io instance into the dm_io list head which is stored
1706ec211631SMing Lei 	 * in bio->bi_private, so that dm_poll_bio can poll them all.
1707b99fdcdcSMing Lei 	 */
17080f14d60aSMing Lei 	if (error || !ci.submit_as_polled) {
17090f14d60aSMing Lei 		/*
17100f14d60aSMing Lei 		 * In case of submission failure, the extra reference for
17110f14d60aSMing Lei 		 * submitting io isn't consumed yet
17120f14d60aSMing Lei 		 */
17130f14d60aSMing Lei 		if (error)
17140f14d60aSMing Lei 			atomic_dec(&io->io_count);
17150f14d60aSMing Lei 		dm_io_dec_pending(io, error);
17160f14d60aSMing Lei 	} else
17174857abf6SMike Snitzer 		dm_queue_poll_io(bio, io);
17181da177e4SLinus Torvalds }
17191da177e4SLinus Torvalds 
17203e08773cSChristoph Hellwig static void dm_submit_bio(struct bio *bio)
17211da177e4SLinus Torvalds {
1722309dca30SChristoph Hellwig 	struct mapped_device *md = bio->bi_bdev->bd_disk->private_data;
172383d5e5b0SMikulas Patocka 	int srcu_idx;
172483d5e5b0SMikulas Patocka 	struct dm_table *map;
17255d7362d0SMikulas Patocka 	unsigned bio_opf = bio->bi_opf;
17261da177e4SLinus Torvalds 
17275d7362d0SMikulas Patocka 	map = dm_get_live_table_bio(md, &srcu_idx, bio_opf);
17288cf7961dSChristoph Hellwig 
1729fa247089SMike Snitzer 	/* If suspended, or map not yet available, queue this IO for later */
1730fa247089SMike Snitzer 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) ||
1731fa247089SMike Snitzer 	    unlikely(!map)) {
17326abc4946SKonstantin Khlebnikov 		if (bio->bi_opf & REQ_NOWAIT)
17336abc4946SKonstantin Khlebnikov 			bio_wouldblock_error(bio);
1734b2abdb1bSMike Snitzer 		else if (bio->bi_opf & REQ_RAHEAD)
17356a8736d1STejun Heo 			bio_io_error(bio);
1736b2abdb1bSMike Snitzer 		else
1737b2abdb1bSMike Snitzer 			queue_io(md, bio);
1738b2abdb1bSMike Snitzer 		goto out;
17391da177e4SLinus Torvalds 	}
17401da177e4SLinus Torvalds 
174196c9865cSMike Snitzer 	dm_split_and_process_bio(md, map, bio);
1742b2abdb1bSMike Snitzer out:
17435d7362d0SMikulas Patocka 	dm_put_live_table_bio(md, srcu_idx, bio_opf);
1744978e51baSMike Snitzer }
1745978e51baSMike Snitzer 
1746b99fdcdcSMing Lei static bool dm_poll_dm_io(struct dm_io *io, struct io_comp_batch *iob,
1747b99fdcdcSMing Lei 			  unsigned int flags)
1748b99fdcdcSMing Lei {
1749655f3aadSMike Snitzer 	WARN_ON_ONCE(!dm_tio_is_normal(&io->tio));
1750b99fdcdcSMing Lei 
1751b99fdcdcSMing Lei 	/* don't poll if the mapped io is done */
1752b99fdcdcSMing Lei 	if (atomic_read(&io->io_count) > 1)
1753b99fdcdcSMing Lei 		bio_poll(&io->tio.clone, iob, flags);
1754b99fdcdcSMing Lei 
1755b99fdcdcSMing Lei 	/* bio_poll holds the last reference */
1756b99fdcdcSMing Lei 	return atomic_read(&io->io_count) == 1;
1757b99fdcdcSMing Lei }
1758b99fdcdcSMing Lei 
1759b99fdcdcSMing Lei static int dm_poll_bio(struct bio *bio, struct io_comp_batch *iob,
1760b99fdcdcSMing Lei 		       unsigned int flags)
1761b99fdcdcSMing Lei {
1762ec211631SMing Lei 	struct dm_io **head = dm_poll_list_head(bio);
1763ec211631SMing Lei 	struct dm_io *list = *head;
1764ec211631SMing Lei 	struct dm_io *tmp = NULL;
1765ec211631SMing Lei 	struct dm_io *curr, *next;
1766b99fdcdcSMing Lei 
1767b99fdcdcSMing Lei 	/* Only poll normal bio which was marked as REQ_DM_POLL_LIST */
1768b99fdcdcSMing Lei 	if (!(bio->bi_opf & REQ_DM_POLL_LIST))
1769b99fdcdcSMing Lei 		return 0;
1770b99fdcdcSMing Lei 
1771ec211631SMing Lei 	WARN_ON_ONCE(!list);
1772b99fdcdcSMing Lei 
1773b99fdcdcSMing Lei 	/*
1774b99fdcdcSMing Lei 	 * Restore .bi_private before possibly completing dm_io.
1775b99fdcdcSMing Lei 	 *
1776b99fdcdcSMing Lei 	 * bio_poll() is only possible once @bio has been completely
1777b99fdcdcSMing Lei 	 * submitted via submit_bio_noacct()'s depth-first submission.
1778b99fdcdcSMing Lei 	 * So there is no dm_queue_poll_io() race associated with
1779b99fdcdcSMing Lei 	 * clearing REQ_DM_POLL_LIST here.
1780b99fdcdcSMing Lei 	 */
1781b99fdcdcSMing Lei 	bio->bi_opf &= ~REQ_DM_POLL_LIST;
1782ec211631SMing Lei 	bio->bi_private = list->data;
1783b99fdcdcSMing Lei 
1784ec211631SMing Lei 	for (curr = list, next = curr->next; curr; curr = next, next =
1785ec211631SMing Lei 			curr ? curr->next : NULL) {
1786ec211631SMing Lei 		if (dm_poll_dm_io(curr, iob, flags)) {
1787b99fdcdcSMing Lei 			/*
178884b98f4cSMike Snitzer 			 * clone_endio() has already occurred, so no
178984b98f4cSMike Snitzer 			 * error handling is needed here.
1790b99fdcdcSMing Lei 			 */
1791ec211631SMing Lei 			__dm_io_dec_pending(curr);
1792ec211631SMing Lei 		} else {
1793ec211631SMing Lei 			curr->next = tmp;
1794ec211631SMing Lei 			tmp = curr;
1795b99fdcdcSMing Lei 		}
1796b99fdcdcSMing Lei 	}
1797b99fdcdcSMing Lei 
1798b99fdcdcSMing Lei 	/* Not done? */
1799ec211631SMing Lei 	if (tmp) {
1800b99fdcdcSMing Lei 		bio->bi_opf |= REQ_DM_POLL_LIST;
1801b99fdcdcSMing Lei 		/* Reset bio->bi_private to dm_io list head */
1802ec211631SMing Lei 		*head = tmp;
1803b99fdcdcSMing Lei 		return 0;
1804b99fdcdcSMing Lei 	}
1805b99fdcdcSMing Lei 	return 1;
1806b99fdcdcSMing Lei }
1807b99fdcdcSMing Lei 
18081da177e4SLinus Torvalds /*-----------------------------------------------------------------
18091da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
18101da177e4SLinus Torvalds  *---------------------------------------------------------------*/
18112b06cfffSAlasdair G Kergon static void free_minor(int minor)
18121da177e4SLinus Torvalds {
1813f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
18141da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1815f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
18161da177e4SLinus Torvalds }
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds /*
18191da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
18201da177e4SLinus Torvalds  */
1821cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
18221da177e4SLinus Torvalds {
1823c9d76be6STejun Heo 	int r;
18241da177e4SLinus Torvalds 
18251da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
18261da177e4SLinus Torvalds 		return -EINVAL;
18271da177e4SLinus Torvalds 
1828c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1829f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
18301da177e4SLinus Torvalds 
1831c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
18321da177e4SLinus Torvalds 
1833f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1834c9d76be6STejun Heo 	idr_preload_end();
1835c9d76be6STejun Heo 	if (r < 0)
1836c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
1837c9d76be6STejun Heo 	return 0;
18381da177e4SLinus Torvalds }
18391da177e4SLinus Torvalds 
1840cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
18411da177e4SLinus Torvalds {
1842c9d76be6STejun Heo 	int r;
18431da177e4SLinus Torvalds 
1844c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1845f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
18461da177e4SLinus Torvalds 
1847c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
18481da177e4SLinus Torvalds 
1849f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1850c9d76be6STejun Heo 	idr_preload_end();
1851c9d76be6STejun Heo 	if (r < 0)
18521da177e4SLinus Torvalds 		return r;
1853c9d76be6STejun Heo 	*minor = r;
1854c9d76be6STejun Heo 	return 0;
18551da177e4SLinus Torvalds }
18561da177e4SLinus Torvalds 
185783d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
1858681cc5e8SMike Snitzer static const struct block_device_operations dm_rq_blk_dops;
1859f26c5719SDan Williams static const struct dax_operations dm_dax_ops;
18601da177e4SLinus Torvalds 
186153d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
186253d5914fSMikulas Patocka 
1863aa6ce87aSSatya Tangirala #ifdef CONFIG_BLK_INLINE_ENCRYPTION
1864cb77cb5aSEric Biggers static void dm_queue_destroy_crypto_profile(struct request_queue *q)
1865aa6ce87aSSatya Tangirala {
1866cb77cb5aSEric Biggers 	dm_destroy_crypto_profile(q->crypto_profile);
1867aa6ce87aSSatya Tangirala }
1868aa6ce87aSSatya Tangirala 
1869aa6ce87aSSatya Tangirala #else /* CONFIG_BLK_INLINE_ENCRYPTION */
1870aa6ce87aSSatya Tangirala 
1871cb77cb5aSEric Biggers static inline void dm_queue_destroy_crypto_profile(struct request_queue *q)
1872aa6ce87aSSatya Tangirala {
1873aa6ce87aSSatya Tangirala }
1874aa6ce87aSSatya Tangirala #endif /* !CONFIG_BLK_INLINE_ENCRYPTION */
1875aa6ce87aSSatya Tangirala 
18760f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md)
18770f20972fSMike Snitzer {
18780f20972fSMike Snitzer 	if (md->wq)
18790f20972fSMike Snitzer 		destroy_workqueue(md->wq);
188029dec90aSChristoph Hellwig 	dm_free_md_mempools(md->mempools);
18810f20972fSMike Snitzer 
1882f26c5719SDan Williams 	if (md->dax_dev) {
1883fb08a190SChristoph Hellwig 		dax_remove_host(md->disk);
1884f26c5719SDan Williams 		kill_dax(md->dax_dev);
1885f26c5719SDan Williams 		put_dax(md->dax_dev);
1886f26c5719SDan Williams 		md->dax_dev = NULL;
1887f26c5719SDan Williams 	}
1888f26c5719SDan Williams 
1889588b7f5dSKirill Tkhai 	dm_cleanup_zoned_dev(md);
18900f20972fSMike Snitzer 	if (md->disk) {
18910f20972fSMike Snitzer 		spin_lock(&_minor_lock);
18920f20972fSMike Snitzer 		md->disk->private_data = NULL;
18930f20972fSMike Snitzer 		spin_unlock(&_minor_lock);
189489f871afSChristoph Hellwig 		if (dm_get_md_type(md) != DM_TYPE_NONE) {
189589f871afSChristoph Hellwig 			dm_sysfs_exit(md);
18960f20972fSMike Snitzer 			del_gendisk(md->disk);
189789f871afSChristoph Hellwig 		}
1898cb77cb5aSEric Biggers 		dm_queue_destroy_crypto_profile(md->queue);
189974fe6ba9SChristoph Hellwig 		blk_cleanup_disk(md->disk);
190074a2b6ecSChristoph Hellwig 	}
19010f20972fSMike Snitzer 
19029f6dc633SMike Snitzer 	if (md->pending_io) {
19039f6dc633SMike Snitzer 		free_percpu(md->pending_io);
19049f6dc633SMike Snitzer 		md->pending_io = NULL;
19059f6dc633SMike Snitzer 	}
19069f6dc633SMike Snitzer 
1907d09960b0STahsin Erdogan 	cleanup_srcu_struct(&md->io_barrier);
1908d09960b0STahsin Erdogan 
1909d5ffebddSMike Snitzer 	mutex_destroy(&md->suspend_lock);
1910d5ffebddSMike Snitzer 	mutex_destroy(&md->type_lock);
1911d5ffebddSMike Snitzer 	mutex_destroy(&md->table_devices_lock);
1912a666e5c0SMikulas Patocka 	mutex_destroy(&md->swap_bios_lock);
1913d5ffebddSMike Snitzer 
19144cc96131SMike Snitzer 	dm_mq_cleanup_mapped_device(md);
19150f20972fSMike Snitzer }
19160f20972fSMike Snitzer 
19171da177e4SLinus Torvalds /*
19181da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
19191da177e4SLinus Torvalds  */
19202b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
19211da177e4SLinus Torvalds {
1922115485e8SMike Snitzer 	int r, numa_node_id = dm_get_numa_node();
1923115485e8SMike Snitzer 	struct mapped_device *md;
1924ba61fdd1SJeff Mahoney 	void *old_md;
19251da177e4SLinus Torvalds 
1926856eb091SMikulas Patocka 	md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
19271da177e4SLinus Torvalds 	if (!md) {
19281da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
19291da177e4SLinus Torvalds 		return NULL;
19301da177e4SLinus Torvalds 	}
19311da177e4SLinus Torvalds 
193210da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
19336ed7ade8SMilan Broz 		goto bad_module_get;
193410da4f79SJeff Mahoney 
19351da177e4SLinus Torvalds 	/* get a minor number for the dev */
19362b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1937cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
19382b06cfffSAlasdair G Kergon 	else
1939cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
19401da177e4SLinus Torvalds 	if (r < 0)
19416ed7ade8SMilan Broz 		goto bad_minor;
19421da177e4SLinus Torvalds 
194383d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
194483d5e5b0SMikulas Patocka 	if (r < 0)
194583d5e5b0SMikulas Patocka 		goto bad_io_barrier;
194683d5e5b0SMikulas Patocka 
1947115485e8SMike Snitzer 	md->numa_node_id = numa_node_id;
1948591ddcfcSMike Snitzer 	md->init_tio_pdu = false;
1949a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
1950e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1951a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
195286f1152bSBenjamin Marzinski 	mutex_init(&md->table_devices_lock);
1953022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
19541da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
19555c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
19561da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
19577a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
19587a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
195986f1152bSBenjamin Marzinski 	INIT_LIST_HEAD(&md->table_devices);
19607a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
19611da177e4SLinus Torvalds 
196247ace7e0SMike Snitzer 	/*
1963c62b37d9SChristoph Hellwig 	 * default to bio-based until DM table is loaded and md->type
1964c62b37d9SChristoph Hellwig 	 * established. If request-based table is loaded: blk-mq will
1965c62b37d9SChristoph Hellwig 	 * override accordingly.
196647ace7e0SMike Snitzer 	 */
196774fe6ba9SChristoph Hellwig 	md->disk = blk_alloc_disk(md->numa_node_id);
19681da177e4SLinus Torvalds 	if (!md->disk)
19690f20972fSMike Snitzer 		goto bad;
197074fe6ba9SChristoph Hellwig 	md->queue = md->disk->queue;
19711da177e4SLinus Torvalds 
1972f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
197353d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1974f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
19752995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
1976f0b04115SJeff Mahoney 
1977a666e5c0SMikulas Patocka 	md->swap_bios = get_swap_bios();
1978a666e5c0SMikulas Patocka 	sema_init(&md->swap_bios_semaphore, md->swap_bios);
1979a666e5c0SMikulas Patocka 	mutex_init(&md->swap_bios_lock);
1980a666e5c0SMikulas Patocka 
19811da177e4SLinus Torvalds 	md->disk->major = _major;
19821da177e4SLinus Torvalds 	md->disk->first_minor = minor;
198374fe6ba9SChristoph Hellwig 	md->disk->minors = 1;
19841ebe2e5fSChristoph Hellwig 	md->disk->flags |= GENHD_FL_NO_PART;
19851da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
19861da177e4SLinus Torvalds 	md->disk->queue = md->queue;
19871da177e4SLinus Torvalds 	md->disk->private_data = md;
19881da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
1989f26c5719SDan Williams 
19905d2a228bSChristoph Hellwig 	if (IS_ENABLED(CONFIG_FS_DAX)) {
199130c6828aSChristoph Hellwig 		md->dax_dev = alloc_dax(md, &dm_dax_ops);
1992d7519392SChristoph Hellwig 		if (IS_ERR(md->dax_dev)) {
1993d7519392SChristoph Hellwig 			md->dax_dev = NULL;
1994f26c5719SDan Williams 			goto bad;
1995976431b0SDan Williams 		}
19967ac5360cSChristoph Hellwig 		set_dax_nocache(md->dax_dev);
19977ac5360cSChristoph Hellwig 		set_dax_nomc(md->dax_dev);
1998fb08a190SChristoph Hellwig 		if (dax_add_host(md->dax_dev, md->disk))
1999f26c5719SDan Williams 			goto bad;
2000f26c5719SDan Williams 	}
20011da177e4SLinus Torvalds 
20027e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
20031da177e4SLinus Torvalds 
2004c7c879eeSMichał Mirosław 	md->wq = alloc_workqueue("kdmflush/%s", WQ_MEM_RECLAIM, 0, md->name);
2005304f3f6aSMilan Broz 	if (!md->wq)
20060f20972fSMike Snitzer 		goto bad;
2007304f3f6aSMilan Broz 
20089f6dc633SMike Snitzer 	md->pending_io = alloc_percpu(unsigned long);
20099f6dc633SMike Snitzer 	if (!md->pending_io)
20109f6dc633SMike Snitzer 		goto bad;
20119f6dc633SMike Snitzer 
2012fd2ed4d2SMikulas Patocka 	dm_stats_init(&md->stats);
2013fd2ed4d2SMikulas Patocka 
2014ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
2015f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
2016ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
2017f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2018ba61fdd1SJeff Mahoney 
2019ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
2020ba61fdd1SJeff Mahoney 
20211da177e4SLinus Torvalds 	return md;
20221da177e4SLinus Torvalds 
20230f20972fSMike Snitzer bad:
20240f20972fSMike Snitzer 	cleanup_mapped_device(md);
202583d5e5b0SMikulas Patocka bad_io_barrier:
20261da177e4SLinus Torvalds 	free_minor(minor);
20276ed7ade8SMilan Broz bad_minor:
202810da4f79SJeff Mahoney 	module_put(THIS_MODULE);
20296ed7ade8SMilan Broz bad_module_get:
2030856eb091SMikulas Patocka 	kvfree(md);
20311da177e4SLinus Torvalds 	return NULL;
20321da177e4SLinus Torvalds }
20331da177e4SLinus Torvalds 
2034ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
2035ae9da83fSJun'ichi Nomura 
20361da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
20371da177e4SLinus Torvalds {
2038f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
203963d94e48SJun'ichi Nomura 
2040ae9da83fSJun'ichi Nomura 	unlock_fs(md);
20412eb6e1e3SKeith Busch 
20420f20972fSMike Snitzer 	cleanup_mapped_device(md);
20430f20972fSMike Snitzer 
20440f20972fSMike Snitzer 	free_table_devices(&md->table_devices);
20450f20972fSMike Snitzer 	dm_stats_cleanup(&md->stats);
204663a4f065SMike Snitzer 	free_minor(minor);
204763a4f065SMike Snitzer 
204810da4f79SJeff Mahoney 	module_put(THIS_MODULE);
2049856eb091SMikulas Patocka 	kvfree(md);
20501da177e4SLinus Torvalds }
20511da177e4SLinus Torvalds 
20521da177e4SLinus Torvalds /*
20531da177e4SLinus Torvalds  * Bind a table to the device.
20541da177e4SLinus Torvalds  */
20551da177e4SLinus Torvalds static void event_callback(void *context)
20561da177e4SLinus Torvalds {
20577a8c3d3bSMike Anderson 	unsigned long flags;
20587a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
20591da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
20601da177e4SLinus Torvalds 
20617a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
20627a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
20637a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
20647a8c3d3bSMike Anderson 
2065ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
20667a8c3d3bSMike Anderson 
20671da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
20681da177e4SLinus Torvalds 	wake_up(&md->eventq);
206962e08243SMikulas Patocka 	dm_issue_global_event();
20701da177e4SLinus Torvalds }
20711da177e4SLinus Torvalds 
2072c217649bSMike Snitzer /*
2073042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2074042d2a9bSAlasdair G Kergon  */
2075042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2076754c5fc7SMike Snitzer 			       struct queue_limits *limits)
20771da177e4SLinus Torvalds {
2078042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
20791da177e4SLinus Torvalds 	sector_t size;
20802a2a4c51SJens Axboe 	int ret;
20811da177e4SLinus Torvalds 
20825a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
20835a8f1f80SBart Van Assche 
20841da177e4SLinus Torvalds 	size = dm_table_get_size(t);
20853ac51e74SDarrick J. Wong 
20863ac51e74SDarrick J. Wong 	/*
20873ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
20883ac51e74SDarrick J. Wong 	 */
2089fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
20903ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
20913ac51e74SDarrick J. Wong 
20925424a0b8SMikulas Patocka 	if (!get_capacity(md->disk))
20935424a0b8SMikulas Patocka 		set_capacity(md->disk, size);
20945424a0b8SMikulas Patocka 	else
2095f64d9b2eSChristoph Hellwig 		set_capacity_and_notify(md->disk, size);
20961da177e4SLinus Torvalds 
2097cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
20982ca3310eSAlasdair G Kergon 
2099f5b4aee1SMike Snitzer 	if (dm_table_request_based(t)) {
210016f12266SMike Snitzer 		/*
21019c37de29SMike Snitzer 		 * Leverage the fact that request-based DM targets are
21029c37de29SMike Snitzer 		 * immutable singletons - used to optimize dm_mq_queue_rq.
210316f12266SMike Snitzer 		 */
210416f12266SMike Snitzer 		md->immutable_target = dm_table_get_immutable_target(t);
2105e6ee8c0bSKiyoshi Ueda 
210629dec90aSChristoph Hellwig 		/*
210729dec90aSChristoph Hellwig 		 * There is no need to reload with request-based dm because the
210829dec90aSChristoph Hellwig 		 * size of front_pad doesn't change.
210929dec90aSChristoph Hellwig 		 *
211029dec90aSChristoph Hellwig 		 * Note for future: If you are to reload bioset, prep-ed
211129dec90aSChristoph Hellwig 		 * requests in the queue may refer to bio from the old bioset,
211229dec90aSChristoph Hellwig 		 * so you must walk through the queue to unprep.
211329dec90aSChristoph Hellwig 		 */
211429dec90aSChristoph Hellwig 		if (!md->mempools) {
211529dec90aSChristoph Hellwig 			md->mempools = t->mempools;
211629dec90aSChristoph Hellwig 			t->mempools = NULL;
211729dec90aSChristoph Hellwig 		}
211829dec90aSChristoph Hellwig 	} else {
211929dec90aSChristoph Hellwig 		/*
212029dec90aSChristoph Hellwig 		 * The md may already have mempools that need changing.
212129dec90aSChristoph Hellwig 		 * If so, reload bioset because front_pad may have changed
212229dec90aSChristoph Hellwig 		 * because a different table was loaded.
212329dec90aSChristoph Hellwig 		 */
212429dec90aSChristoph Hellwig 		dm_free_md_mempools(md->mempools);
212529dec90aSChristoph Hellwig 		md->mempools = t->mempools;
212629dec90aSChristoph Hellwig 		t->mempools = NULL;
21272a2a4c51SJens Axboe 	}
2128e6ee8c0bSKiyoshi Ueda 
2129f5b4aee1SMike Snitzer 	ret = dm_table_set_restrictions(t, md->queue, limits);
2130bb37d772SDamien Le Moal 	if (ret) {
2131bb37d772SDamien Le Moal 		old_map = ERR_PTR(ret);
2132bb37d772SDamien Le Moal 		goto out;
2133bb37d772SDamien Le Moal 	}
2134bb37d772SDamien Le Moal 
2135a12f5d48SEric Dumazet 	old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
21361d3aa6f6SMike Snitzer 	rcu_assign_pointer(md->map, (void *)t);
213736a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
213836a0456fSAlasdair G Kergon 
213941abc4e1SHannes Reinecke 	if (old_map)
214083d5e5b0SMikulas Patocka 		dm_sync_table(md);
21412a2a4c51SJens Axboe out:
2142042d2a9bSAlasdair G Kergon 	return old_map;
21431da177e4SLinus Torvalds }
21441da177e4SLinus Torvalds 
2145a7940155SAlasdair G Kergon /*
2146a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2147a7940155SAlasdair G Kergon  */
2148a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
21491da177e4SLinus Torvalds {
2150a12f5d48SEric Dumazet 	struct dm_table *map = rcu_dereference_protected(md->map, 1);
21511da177e4SLinus Torvalds 
21521da177e4SLinus Torvalds 	if (!map)
2153a7940155SAlasdair G Kergon 		return NULL;
21541da177e4SLinus Torvalds 
21551da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
21569cdb8520SMonam Agarwal 	RCU_INIT_POINTER(md->map, NULL);
215783d5e5b0SMikulas Patocka 	dm_sync_table(md);
2158a7940155SAlasdair G Kergon 
2159a7940155SAlasdair G Kergon 	return map;
21601da177e4SLinus Torvalds }
21611da177e4SLinus Torvalds 
21621da177e4SLinus Torvalds /*
21631da177e4SLinus Torvalds  * Constructor for a new device.
21641da177e4SLinus Torvalds  */
21652b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
21661da177e4SLinus Torvalds {
21671da177e4SLinus Torvalds 	struct mapped_device *md;
21681da177e4SLinus Torvalds 
21692b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
21701da177e4SLinus Torvalds 	if (!md)
21711da177e4SLinus Torvalds 		return -ENXIO;
21721da177e4SLinus Torvalds 
217391ccbbacSTushar Sugandhi 	dm_ima_reset_data(md);
217491ccbbacSTushar Sugandhi 
21751da177e4SLinus Torvalds 	*result = md;
21761da177e4SLinus Torvalds 	return 0;
21771da177e4SLinus Torvalds }
21781da177e4SLinus Torvalds 
2179a5664dadSMike Snitzer /*
2180a5664dadSMike Snitzer  * Functions to manage md->type.
2181a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2182a5664dadSMike Snitzer  */
2183a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2184a5664dadSMike Snitzer {
2185a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2186a5664dadSMike Snitzer }
2187a5664dadSMike Snitzer 
2188a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2189a5664dadSMike Snitzer {
2190a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2191a5664dadSMike Snitzer }
2192a5664dadSMike Snitzer 
21937e0d574fSBart Van Assche void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
2194a5664dadSMike Snitzer {
219500c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2196a5664dadSMike Snitzer 	md->type = type;
2197a5664dadSMike Snitzer }
2198a5664dadSMike Snitzer 
21997e0d574fSBart Van Assche enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
2200a5664dadSMike Snitzer {
2201a5664dadSMike Snitzer 	return md->type;
2202a5664dadSMike Snitzer }
2203a5664dadSMike Snitzer 
220436a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
220536a0456fSAlasdair G Kergon {
220636a0456fSAlasdair G Kergon 	return md->immutable_target_type;
220736a0456fSAlasdair G Kergon }
220836a0456fSAlasdair G Kergon 
22094a0b4ddfSMike Snitzer /*
2210f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
2211f84cb8a4SMike Snitzer  * count on 'md'.
2212f84cb8a4SMike Snitzer  */
2213f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2214f84cb8a4SMike Snitzer {
2215f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
2216f84cb8a4SMike Snitzer 	return &md->queue->limits;
2217f84cb8a4SMike Snitzer }
2218f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2219f84cb8a4SMike Snitzer 
22204a0b4ddfSMike Snitzer /*
22214a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
22224a0b4ddfSMike Snitzer  */
2223591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
22244a0b4ddfSMike Snitzer {
2225ba305859SChristoph Hellwig 	enum dm_queue_mode type = dm_table_get_type(t);
2226c100ec49SMike Snitzer 	struct queue_limits limits;
2227ba305859SChristoph Hellwig 	int r;
2228bfebd1cdSMike Snitzer 
2229545ed20eSToshi Kani 	switch (type) {
2230bfebd1cdSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
2231681cc5e8SMike Snitzer 		md->disk->fops = &dm_rq_blk_dops;
2232e83068a5SMike Snitzer 		r = dm_mq_init_request_queue(md, t);
2233bfebd1cdSMike Snitzer 		if (r) {
2234681cc5e8SMike Snitzer 			DMERR("Cannot initialize queue for request-based dm mapped device");
2235bfebd1cdSMike Snitzer 			return r;
2236bfebd1cdSMike Snitzer 		}
2237bfebd1cdSMike Snitzer 		break;
2238bfebd1cdSMike Snitzer 	case DM_TYPE_BIO_BASED:
2239545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
2240bfebd1cdSMike Snitzer 		break;
22417e0d574fSBart Van Assche 	case DM_TYPE_NONE:
22427e0d574fSBart Van Assche 		WARN_ON_ONCE(true);
22437e0d574fSBart Van Assche 		break;
2244ff36ab34SMike Snitzer 	}
22454a0b4ddfSMike Snitzer 
2246c100ec49SMike Snitzer 	r = dm_calculate_queue_limits(t, &limits);
2247c100ec49SMike Snitzer 	if (r) {
2248c100ec49SMike Snitzer 		DMERR("Cannot calculate initial queue limits");
2249c100ec49SMike Snitzer 		return r;
2250c100ec49SMike Snitzer 	}
2251bb37d772SDamien Le Moal 	r = dm_table_set_restrictions(t, md->queue, &limits);
2252bb37d772SDamien Le Moal 	if (r)
2253bb37d772SDamien Le Moal 		return r;
225489f871afSChristoph Hellwig 
2255e7089f65SLuis Chamberlain 	r = add_disk(md->disk);
2256e7089f65SLuis Chamberlain 	if (r)
2257e7089f65SLuis Chamberlain 		return r;
225889f871afSChristoph Hellwig 
225989f871afSChristoph Hellwig 	r = dm_sysfs_init(md);
226089f871afSChristoph Hellwig 	if (r) {
226189f871afSChristoph Hellwig 		del_gendisk(md->disk);
226289f871afSChristoph Hellwig 		return r;
226389f871afSChristoph Hellwig 	}
2264ba305859SChristoph Hellwig 	md->type = type;
22654a0b4ddfSMike Snitzer 	return 0;
22664a0b4ddfSMike Snitzer }
22674a0b4ddfSMike Snitzer 
22682bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev)
22691da177e4SLinus Torvalds {
22701da177e4SLinus Torvalds 	struct mapped_device *md;
22711da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
22721da177e4SLinus Torvalds 
22731da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
22741da177e4SLinus Torvalds 		return NULL;
22751da177e4SLinus Torvalds 
2276f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
22771da177e4SLinus Torvalds 
22781da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
227949de5769SMike Snitzer 	if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) ||
228049de5769SMike Snitzer 	    test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2281637842cfSDavid Teigland 		md = NULL;
2282fba9f90eSJeff Mahoney 		goto out;
2283fba9f90eSJeff Mahoney 	}
22842bec1f4aSMikulas Patocka 	dm_get(md);
2285fba9f90eSJeff Mahoney out:
2286f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
22871da177e4SLinus Torvalds 
2288637842cfSDavid Teigland 	return md;
2289637842cfSDavid Teigland }
22903cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2291d229a958SDavid Teigland 
22929ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2293637842cfSDavid Teigland {
22949ade92a9SAlasdair G Kergon 	return md->interface_ptr;
22951da177e4SLinus Torvalds }
22961da177e4SLinus Torvalds 
22971da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
22981da177e4SLinus Torvalds {
22991da177e4SLinus Torvalds 	md->interface_ptr = ptr;
23001da177e4SLinus Torvalds }
23011da177e4SLinus Torvalds 
23021da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
23031da177e4SLinus Torvalds {
23041da177e4SLinus Torvalds 	atomic_inc(&md->holders);
23053f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
23061da177e4SLinus Torvalds }
23071da177e4SLinus Torvalds 
230809ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md)
230909ee96b2SMikulas Patocka {
231009ee96b2SMikulas Patocka 	spin_lock(&_minor_lock);
231109ee96b2SMikulas Patocka 	if (test_bit(DMF_FREEING, &md->flags)) {
231209ee96b2SMikulas Patocka 		spin_unlock(&_minor_lock);
231309ee96b2SMikulas Patocka 		return -EBUSY;
231409ee96b2SMikulas Patocka 	}
231509ee96b2SMikulas Patocka 	dm_get(md);
231609ee96b2SMikulas Patocka 	spin_unlock(&_minor_lock);
231709ee96b2SMikulas Patocka 	return 0;
231809ee96b2SMikulas Patocka }
231909ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold);
232009ee96b2SMikulas Patocka 
232172d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
232272d94861SAlasdair G Kergon {
232372d94861SAlasdair G Kergon 	return md->name;
232472d94861SAlasdair G Kergon }
232572d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
232672d94861SAlasdair G Kergon 
23273f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
23281da177e4SLinus Torvalds {
23291134e5aeSMike Anderson 	struct dm_table *map;
233083d5e5b0SMikulas Patocka 	int srcu_idx;
23311da177e4SLinus Torvalds 
23323f77316dSKiyoshi Ueda 	might_sleep();
2333fba9f90eSJeff Mahoney 
233463a4f065SMike Snitzer 	spin_lock(&_minor_lock);
23353f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2336fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2337f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
23383f77316dSKiyoshi Ueda 
23397a5428dcSChristoph Hellwig 	blk_mark_disk_dead(md->disk);
23403b785fbcSBart Van Assche 
2341ab7c7bb6SMikulas Patocka 	/*
2342ab7c7bb6SMikulas Patocka 	 * Take suspend_lock so that presuspend and postsuspend methods
2343ab7c7bb6SMikulas Patocka 	 * do not race with internal suspend.
2344ab7c7bb6SMikulas Patocka 	 */
2345ab7c7bb6SMikulas Patocka 	mutex_lock(&md->suspend_lock);
23462a708cffSJunichi Nomura 	map = dm_get_live_table(md, &srcu_idx);
23474f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
23481da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
2349adc0daadSMikulas Patocka 		set_bit(DMF_SUSPENDED, &md->flags);
23505df96f2bSMikulas Patocka 		set_bit(DMF_POST_SUSPENDING, &md->flags);
23511da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
23521da177e4SLinus Torvalds 	}
235383d5e5b0SMikulas Patocka 	/* dm_put_live_table must be before msleep, otherwise deadlock is possible */
235483d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
23552a708cffSJunichi Nomura 	mutex_unlock(&md->suspend_lock);
235683d5e5b0SMikulas Patocka 
23573f77316dSKiyoshi Ueda 	/*
23583f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
23593f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
23603f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
23613f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
23623f77316dSKiyoshi Ueda 	 */
23633f77316dSKiyoshi Ueda 	if (wait)
23643f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
23653f77316dSKiyoshi Ueda 			msleep(1);
23663f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
23673f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
23683f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
23693f77316dSKiyoshi Ueda 
2370a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
23711da177e4SLinus Torvalds 	free_dev(md);
23721da177e4SLinus Torvalds }
23733f77316dSKiyoshi Ueda 
23743f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
23753f77316dSKiyoshi Ueda {
23763f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
23773f77316dSKiyoshi Ueda }
23783f77316dSKiyoshi Ueda 
23793f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
23803f77316dSKiyoshi Ueda {
23813f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
23823f77316dSKiyoshi Ueda }
23833f77316dSKiyoshi Ueda 
23843f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
23853f77316dSKiyoshi Ueda {
23863f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
23871da177e4SLinus Torvalds }
238879eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
23891da177e4SLinus Torvalds 
23909f6dc633SMike Snitzer static bool dm_in_flight_bios(struct mapped_device *md)
239185067747SMing Lei {
239285067747SMing Lei 	int cpu;
23939f6dc633SMike Snitzer 	unsigned long sum = 0;
239485067747SMing Lei 
23959f6dc633SMike Snitzer 	for_each_possible_cpu(cpu)
23969f6dc633SMike Snitzer 		sum += *per_cpu_ptr(md->pending_io, cpu);
239785067747SMing Lei 
239885067747SMing Lei 	return sum != 0;
239985067747SMing Lei }
240085067747SMing Lei 
24012f064a59SPeter Zijlstra static int dm_wait_for_bios_completion(struct mapped_device *md, unsigned int task_state)
240246125c1cSMilan Broz {
240346125c1cSMilan Broz 	int r = 0;
24049f4c3f87SBart Van Assche 	DEFINE_WAIT(wait);
240546125c1cSMilan Broz 
240685067747SMing Lei 	while (true) {
24079f4c3f87SBart Van Assche 		prepare_to_wait(&md->wait, &wait, task_state);
240846125c1cSMilan Broz 
24099f6dc633SMike Snitzer 		if (!dm_in_flight_bios(md))
241046125c1cSMilan Broz 			break;
241146125c1cSMilan Broz 
2412e3fabdfdSBart Van Assche 		if (signal_pending_state(task_state, current)) {
241346125c1cSMilan Broz 			r = -EINTR;
241446125c1cSMilan Broz 			break;
241546125c1cSMilan Broz 		}
241646125c1cSMilan Broz 
241746125c1cSMilan Broz 		io_schedule();
241846125c1cSMilan Broz 	}
24199f4c3f87SBart Van Assche 	finish_wait(&md->wait, &wait);
2420b44ebeb0SMikulas Patocka 
24219f6dc633SMike Snitzer 	smp_rmb();
24229f6dc633SMike Snitzer 
242346125c1cSMilan Broz 	return r;
242446125c1cSMilan Broz }
242546125c1cSMilan Broz 
24262f064a59SPeter Zijlstra static int dm_wait_for_completion(struct mapped_device *md, unsigned int task_state)
242785067747SMing Lei {
242885067747SMing Lei 	int r = 0;
242985067747SMing Lei 
243085067747SMing Lei 	if (!queue_is_mq(md->queue))
243185067747SMing Lei 		return dm_wait_for_bios_completion(md, task_state);
243285067747SMing Lei 
243385067747SMing Lei 	while (true) {
243485067747SMing Lei 		if (!blk_mq_queue_inflight(md->queue))
243585067747SMing Lei 			break;
243685067747SMing Lei 
243785067747SMing Lei 		if (signal_pending_state(task_state, current)) {
243885067747SMing Lei 			r = -EINTR;
243985067747SMing Lei 			break;
244085067747SMing Lei 		}
244185067747SMing Lei 
244285067747SMing Lei 		msleep(5);
244385067747SMing Lei 	}
244485067747SMing Lei 
244585067747SMing Lei 	return r;
244685067747SMing Lei }
244785067747SMing Lei 
24481da177e4SLinus Torvalds /*
24491da177e4SLinus Torvalds  * Process the deferred bios
24501da177e4SLinus Torvalds  */
2451ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
24521da177e4SLinus Torvalds {
24530c2915b8SMike Snitzer 	struct mapped_device *md = container_of(work, struct mapped_device, work);
24540c2915b8SMike Snitzer 	struct bio *bio;
2455ef208587SMikulas Patocka 
24563b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2457022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
24580c2915b8SMike Snitzer 		bio = bio_list_pop(&md->deferred);
2459022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2460022c2611SMikulas Patocka 
24610c2915b8SMike Snitzer 		if (!bio)
2462df12ee99SAlasdair G Kergon 			break;
246373d410c0SMilan Broz 
24640c2915b8SMike Snitzer 		submit_bio_noacct(bio);
2465e6ee8c0bSKiyoshi Ueda 	}
24661da177e4SLinus Torvalds }
24671da177e4SLinus Torvalds 
24689a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2469304f3f6aSMilan Broz {
24703b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
24714e857c58SPeter Zijlstra 	smp_mb__after_atomic();
247253d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2473304f3f6aSMilan Broz }
2474304f3f6aSMilan Broz 
24751da177e4SLinus Torvalds /*
2476042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
24771da177e4SLinus Torvalds  */
2478042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
24791da177e4SLinus Torvalds {
248087eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2481754c5fc7SMike Snitzer 	struct queue_limits limits;
2482042d2a9bSAlasdair G Kergon 	int r;
24831da177e4SLinus Torvalds 
2484e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
24851da177e4SLinus Torvalds 
24861da177e4SLinus Torvalds 	/* device must be suspended */
24874f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
248893c534aeSAlasdair G Kergon 		goto out;
24891da177e4SLinus Torvalds 
24903ae70656SMike Snitzer 	/*
24913ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
24923ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
24933ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
24943ae70656SMike Snitzer 	 * reappear.
24953ae70656SMike Snitzer 	 */
24963ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
249783d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
24983ae70656SMike Snitzer 		if (live_map)
24993ae70656SMike Snitzer 			limits = md->queue->limits;
250083d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
25013ae70656SMike Snitzer 	}
25023ae70656SMike Snitzer 
250387eb5b21SMike Christie 	if (!live_map) {
2504754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2505042d2a9bSAlasdair G Kergon 		if (r) {
2506042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2507754c5fc7SMike Snitzer 			goto out;
2508042d2a9bSAlasdair G Kergon 		}
250987eb5b21SMike Christie 	}
2510754c5fc7SMike Snitzer 
2511042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
251262e08243SMikulas Patocka 	dm_issue_global_event();
25131da177e4SLinus Torvalds 
251493c534aeSAlasdair G Kergon out:
2515e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2516042d2a9bSAlasdair G Kergon 	return map;
25171da177e4SLinus Torvalds }
25181da177e4SLinus Torvalds 
25191da177e4SLinus Torvalds /*
25201da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
25211da177e4SLinus Torvalds  * device.
25221da177e4SLinus Torvalds  */
25232ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
25241da177e4SLinus Torvalds {
2525e39e2e95SAlasdair G Kergon 	int r;
25261da177e4SLinus Torvalds 
2527040f04bdSChristoph Hellwig 	WARN_ON(test_bit(DMF_FROZEN, &md->flags));
2528dfbe03f6SAlasdair G Kergon 
2529977115c0SChristoph Hellwig 	r = freeze_bdev(md->disk->part0);
2530040f04bdSChristoph Hellwig 	if (!r)
2531aa8d7c2fSAlasdair G Kergon 		set_bit(DMF_FROZEN, &md->flags);
2532040f04bdSChristoph Hellwig 	return r;
25331da177e4SLinus Torvalds }
25341da177e4SLinus Torvalds 
25352ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
25361da177e4SLinus Torvalds {
2537aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2538aa8d7c2fSAlasdair G Kergon 		return;
2539977115c0SChristoph Hellwig 	thaw_bdev(md->disk->part0);
2540aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
25411da177e4SLinus Torvalds }
25421da177e4SLinus Torvalds 
25431da177e4SLinus Torvalds /*
2544b48633f8SBart Van Assche  * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
2545b48633f8SBart Van Assche  * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
2546b48633f8SBart Van Assche  * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
2547b48633f8SBart Van Assche  *
2548ffcc3936SMike Snitzer  * If __dm_suspend returns 0, the device is completely quiescent
2549ffcc3936SMike Snitzer  * now. There is no request-processing activity. All new requests
2550ffcc3936SMike Snitzer  * are being added to md->deferred list.
2551cec47e3dSKiyoshi Ueda  */
2552ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
25532f064a59SPeter Zijlstra 			unsigned suspend_flags, unsigned int task_state,
2554eaf9a736SMike Snitzer 			int dmf_suspended_flag)
25551da177e4SLinus Torvalds {
2556ffcc3936SMike Snitzer 	bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2557ffcc3936SMike Snitzer 	bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2558ffcc3936SMike Snitzer 	int r;
2559cf222b37SAlasdair G Kergon 
25605a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
25615a8f1f80SBart Van Assche 
25622e93ccc1SKiyoshi Ueda 	/*
25632e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
25642e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
25652e93ccc1SKiyoshi Ueda 	 */
25662e93ccc1SKiyoshi Ueda 	if (noflush)
25672e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
256886331f39SBart Van Assche 	else
2569ac75b09fSMike Snitzer 		DMDEBUG("%s: suspending with flush", dm_device_name(md));
25702e93ccc1SKiyoshi Ueda 
2571d67ee213SMike Snitzer 	/*
2572d67ee213SMike Snitzer 	 * This gets reverted if there's an error later and the targets
2573d67ee213SMike Snitzer 	 * provide the .presuspend_undo hook.
2574d67ee213SMike Snitzer 	 */
25751da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
25761da177e4SLinus Torvalds 
25772e93ccc1SKiyoshi Ueda 	/*
25789f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
25799f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
25809f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
25819f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
25822e93ccc1SKiyoshi Ueda 	 */
258332a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
25842ca3310eSAlasdair G Kergon 		r = lock_fs(md);
2585d67ee213SMike Snitzer 		if (r) {
2586d67ee213SMike Snitzer 			dm_table_presuspend_undo_targets(map);
2587ffcc3936SMike Snitzer 			return r;
2588aa8d7c2fSAlasdair G Kergon 		}
2589d67ee213SMike Snitzer 	}
25901da177e4SLinus Torvalds 
25911da177e4SLinus Torvalds 	/*
25923b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
25933b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
259496c9865cSMike Snitzer 	 * dm_split_and_process_bio from dm_submit_bio.
25953b00b203SMikulas Patocka 	 *
259696c9865cSMike Snitzer 	 * To get all processes out of dm_split_and_process_bio in dm_submit_bio,
25973b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
259896c9865cSMike Snitzer 	 * dm_split_and_process_bio from dm_submit_bio and quiesce the thread
25990cede372SMike Snitzer 	 * (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND and call
26006a8736d1STejun Heo 	 * flush_workqueue(md->wq).
26011da177e4SLinus Torvalds 	 */
26021eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
260341abc4e1SHannes Reinecke 	if (map)
260483d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
26051da177e4SLinus Torvalds 
2606d0bcb878SKiyoshi Ueda 	/*
260729e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
260829e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2609d0bcb878SKiyoshi Ueda 	 */
26106a23e05cSJens Axboe 	if (dm_request_based(md))
2611eca7ee6dSMike Snitzer 		dm_stop_queue(md->queue);
2612cec47e3dSKiyoshi Ueda 
2613d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2614d0bcb878SKiyoshi Ueda 
26151da177e4SLinus Torvalds 	/*
26163b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
26173b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
26183b00b203SMikulas Patocka 	 * to finish.
26191da177e4SLinus Torvalds 	 */
2620b48633f8SBart Van Assche 	r = dm_wait_for_completion(md, task_state);
2621eaf9a736SMike Snitzer 	if (!r)
2622eaf9a736SMike Snitzer 		set_bit(dmf_suspended_flag, &md->flags);
26231da177e4SLinus Torvalds 
26246d6f10dfSMilan Broz 	if (noflush)
2625022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
262641abc4e1SHannes Reinecke 	if (map)
262783d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
26282e93ccc1SKiyoshi Ueda 
26291da177e4SLinus Torvalds 	/* were we interrupted ? */
263046125c1cSMilan Broz 	if (r < 0) {
26319a1fb464SMikulas Patocka 		dm_queue_flush(md);
263273d410c0SMilan Broz 
2633cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
2634eca7ee6dSMike Snitzer 			dm_start_queue(md->queue);
2635cec47e3dSKiyoshi Ueda 
26362ca3310eSAlasdair G Kergon 		unlock_fs(md);
2637d67ee213SMike Snitzer 		dm_table_presuspend_undo_targets(map);
2638ffcc3936SMike Snitzer 		/* pushback list is already flushed, so skip flush */
2639ffcc3936SMike Snitzer 	}
2640ffcc3936SMike Snitzer 
2641ffcc3936SMike Snitzer 	return r;
26422ca3310eSAlasdair G Kergon }
26432ca3310eSAlasdair G Kergon 
26443b00b203SMikulas Patocka /*
2645ffcc3936SMike Snitzer  * We need to be able to change a mapping table under a mounted
2646ffcc3936SMike Snitzer  * filesystem.  For example we might want to move some data in
2647ffcc3936SMike Snitzer  * the background.  Before the table can be swapped with
2648ffcc3936SMike Snitzer  * dm_bind_table, dm_suspend must be called to flush any in
2649ffcc3936SMike Snitzer  * flight bios and ensure that any further io gets deferred.
26503b00b203SMikulas Patocka  */
2651ffcc3936SMike Snitzer /*
2652ffcc3936SMike Snitzer  * Suspend mechanism in request-based dm.
2653ffcc3936SMike Snitzer  *
2654ffcc3936SMike Snitzer  * 1. Flush all I/Os by lock_fs() if needed.
2655ffcc3936SMike Snitzer  * 2. Stop dispatching any I/O by stopping the request_queue.
2656ffcc3936SMike Snitzer  * 3. Wait for all in-flight I/Os to be completed or requeued.
2657ffcc3936SMike Snitzer  *
2658ffcc3936SMike Snitzer  * To abort suspend, start the request_queue.
2659ffcc3936SMike Snitzer  */
2660ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2661ffcc3936SMike Snitzer {
2662ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2663ffcc3936SMike Snitzer 	int r = 0;
2664ffcc3936SMike Snitzer 
2665ffcc3936SMike Snitzer retry:
2666ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2667ffcc3936SMike Snitzer 
2668ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2669ffcc3936SMike Snitzer 		r = -EINVAL;
2670ffcc3936SMike Snitzer 		goto out_unlock;
2671ffcc3936SMike Snitzer 	}
2672ffcc3936SMike Snitzer 
2673ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2674ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2675ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2676ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2677ffcc3936SMike Snitzer 		if (r)
2678ffcc3936SMike Snitzer 			return r;
2679ffcc3936SMike Snitzer 		goto retry;
2680ffcc3936SMike Snitzer 	}
2681ffcc3936SMike Snitzer 
2682a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2683ffcc3936SMike Snitzer 
2684eaf9a736SMike Snitzer 	r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
2685ffcc3936SMike Snitzer 	if (r)
2686ffcc3936SMike Snitzer 		goto out_unlock;
26873b00b203SMikulas Patocka 
26885df96f2bSMikulas Patocka 	set_bit(DMF_POST_SUSPENDING, &md->flags);
26894d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
26905df96f2bSMikulas Patocka 	clear_bit(DMF_POST_SUSPENDING, &md->flags);
26914d4471cbSKiyoshi Ueda 
2692d287483dSAlasdair G Kergon out_unlock:
2693e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2694cf222b37SAlasdair G Kergon 	return r;
26951da177e4SLinus Torvalds }
26961da177e4SLinus Torvalds 
2697ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map)
26981da177e4SLinus Torvalds {
2699ffcc3936SMike Snitzer 	if (map) {
2700ffcc3936SMike Snitzer 		int r = dm_table_resume_targets(map);
27018757b776SMilan Broz 		if (r)
2702ffcc3936SMike Snitzer 			return r;
2703ffcc3936SMike Snitzer 	}
27042ca3310eSAlasdair G Kergon 
27059a1fb464SMikulas Patocka 	dm_queue_flush(md);
27062ca3310eSAlasdair G Kergon 
2707cec47e3dSKiyoshi Ueda 	/*
2708cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2709cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2710cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2711cec47e3dSKiyoshi Ueda 	 */
2712cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2713eca7ee6dSMike Snitzer 		dm_start_queue(md->queue);
2714cec47e3dSKiyoshi Ueda 
27152ca3310eSAlasdair G Kergon 	unlock_fs(md);
27162ca3310eSAlasdair G Kergon 
2717ffcc3936SMike Snitzer 	return 0;
2718ffcc3936SMike Snitzer }
2719ffcc3936SMike Snitzer 
2720ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md)
2721ffcc3936SMike Snitzer {
27228dc23658SMinfei Huang 	int r;
2723ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2724ffcc3936SMike Snitzer 
2725ffcc3936SMike Snitzer retry:
27268dc23658SMinfei Huang 	r = -EINVAL;
2727ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2728ffcc3936SMike Snitzer 
2729ffcc3936SMike Snitzer 	if (!dm_suspended_md(md))
2730ffcc3936SMike Snitzer 		goto out;
2731ffcc3936SMike Snitzer 
2732ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2733ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2734ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2735ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2736ffcc3936SMike Snitzer 		if (r)
2737ffcc3936SMike Snitzer 			return r;
2738ffcc3936SMike Snitzer 		goto retry;
2739ffcc3936SMike Snitzer 	}
2740ffcc3936SMike Snitzer 
2741a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2742ffcc3936SMike Snitzer 	if (!map || !dm_table_get_size(map))
2743ffcc3936SMike Snitzer 		goto out;
2744ffcc3936SMike Snitzer 
2745ffcc3936SMike Snitzer 	r = __dm_resume(md, map);
2746ffcc3936SMike Snitzer 	if (r)
2747ffcc3936SMike Snitzer 		goto out;
2748ffcc3936SMike Snitzer 
27492ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
2750cf222b37SAlasdair G Kergon out:
2751e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
27522ca3310eSAlasdair G Kergon 
2753cf222b37SAlasdair G Kergon 	return r;
27541da177e4SLinus Torvalds }
27551da177e4SLinus Torvalds 
2756fd2ed4d2SMikulas Patocka /*
2757fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
2758fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
2759fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
2760fd2ed4d2SMikulas Patocka  */
2761fd2ed4d2SMikulas Patocka 
2762ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
2763ffcc3936SMike Snitzer {
2764ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2765ffcc3936SMike Snitzer 
27661ea0654eSBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
27671ea0654eSBart Van Assche 
276896b26c8cSMikulas Patocka 	if (md->internal_suspend_count++)
2769ffcc3936SMike Snitzer 		return; /* nested internal suspend */
2770ffcc3936SMike Snitzer 
2771ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2772ffcc3936SMike Snitzer 		set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2773ffcc3936SMike Snitzer 		return; /* nest suspend */
2774ffcc3936SMike Snitzer 	}
2775ffcc3936SMike Snitzer 
2776a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2777ffcc3936SMike Snitzer 
2778ffcc3936SMike Snitzer 	/*
2779ffcc3936SMike Snitzer 	 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2780ffcc3936SMike Snitzer 	 * supported.  Properly supporting a TASK_INTERRUPTIBLE internal suspend
2781ffcc3936SMike Snitzer 	 * would require changing .presuspend to return an error -- avoid this
2782ffcc3936SMike Snitzer 	 * until there is a need for more elaborate variants of internal suspend.
2783ffcc3936SMike Snitzer 	 */
2784eaf9a736SMike Snitzer 	(void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
2785eaf9a736SMike Snitzer 			    DMF_SUSPENDED_INTERNALLY);
2786ffcc3936SMike Snitzer 
27875df96f2bSMikulas Patocka 	set_bit(DMF_POST_SUSPENDING, &md->flags);
2788ffcc3936SMike Snitzer 	dm_table_postsuspend_targets(map);
27895df96f2bSMikulas Patocka 	clear_bit(DMF_POST_SUSPENDING, &md->flags);
2790ffcc3936SMike Snitzer }
2791ffcc3936SMike Snitzer 
2792ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md)
2793ffcc3936SMike Snitzer {
279496b26c8cSMikulas Patocka 	BUG_ON(!md->internal_suspend_count);
279596b26c8cSMikulas Patocka 
279696b26c8cSMikulas Patocka 	if (--md->internal_suspend_count)
2797ffcc3936SMike Snitzer 		return; /* resume from nested internal suspend */
2798ffcc3936SMike Snitzer 
2799ffcc3936SMike Snitzer 	if (dm_suspended_md(md))
2800ffcc3936SMike Snitzer 		goto done; /* resume from nested suspend */
2801ffcc3936SMike Snitzer 
2802ffcc3936SMike Snitzer 	/*
2803ffcc3936SMike Snitzer 	 * NOTE: existing callers don't need to call dm_table_resume_targets
2804ffcc3936SMike Snitzer 	 * (which may fail -- so best to avoid it for now by passing NULL map)
2805ffcc3936SMike Snitzer 	 */
2806ffcc3936SMike Snitzer 	(void) __dm_resume(md, NULL);
2807ffcc3936SMike Snitzer 
2808ffcc3936SMike Snitzer done:
2809ffcc3936SMike Snitzer 	clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2810ffcc3936SMike Snitzer 	smp_mb__after_atomic();
2811ffcc3936SMike Snitzer 	wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2812ffcc3936SMike Snitzer }
2813ffcc3936SMike Snitzer 
2814ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md)
2815fd2ed4d2SMikulas Patocka {
2816fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
2817ffcc3936SMike Snitzer 	__dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2818ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2819ffcc3936SMike Snitzer }
2820ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
2821ffcc3936SMike Snitzer 
2822ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md)
2823ffcc3936SMike Snitzer {
2824ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2825ffcc3936SMike Snitzer 	__dm_internal_resume(md);
2826ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2827ffcc3936SMike Snitzer }
2828ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume);
2829ffcc3936SMike Snitzer 
2830ffcc3936SMike Snitzer /*
2831ffcc3936SMike Snitzer  * Fast variants of internal suspend/resume hold md->suspend_lock,
2832ffcc3936SMike Snitzer  * which prevents interaction with userspace-driven suspend.
2833ffcc3936SMike Snitzer  */
2834ffcc3936SMike Snitzer 
2835ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md)
2836ffcc3936SMike Snitzer {
2837ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2838ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2839fd2ed4d2SMikulas Patocka 		return;
2840fd2ed4d2SMikulas Patocka 
2841fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2842fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
2843fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
2844fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2845fd2ed4d2SMikulas Patocka }
2846b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
2847fd2ed4d2SMikulas Patocka 
2848ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md)
2849fd2ed4d2SMikulas Patocka {
2850ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2851fd2ed4d2SMikulas Patocka 		goto done;
2852fd2ed4d2SMikulas Patocka 
2853fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
2854fd2ed4d2SMikulas Patocka 
2855fd2ed4d2SMikulas Patocka done:
2856fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
2857fd2ed4d2SMikulas Patocka }
2858b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
2859fd2ed4d2SMikulas Patocka 
28601da177e4SLinus Torvalds /*-----------------------------------------------------------------
28611da177e4SLinus Torvalds  * Event notification.
28621da177e4SLinus Torvalds  *---------------------------------------------------------------*/
28633abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
286460935eb2SMilan Broz 		       unsigned cookie)
286569267a30SAlasdair G Kergon {
28666958c1c6SMikulas Patocka 	int r;
28676958c1c6SMikulas Patocka 	unsigned noio_flag;
286860935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
286960935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
287060935eb2SMilan Broz 
28716958c1c6SMikulas Patocka 	noio_flag = memalloc_noio_save();
28726958c1c6SMikulas Patocka 
287360935eb2SMilan Broz 	if (!cookie)
28746958c1c6SMikulas Patocka 		r = kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
287560935eb2SMilan Broz 	else {
287660935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
287760935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
28786958c1c6SMikulas Patocka 		r = kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
28793abf85b5SPeter Rajnoha 				       action, envp);
288060935eb2SMilan Broz 	}
28816958c1c6SMikulas Patocka 
28826958c1c6SMikulas Patocka 	memalloc_noio_restore(noio_flag);
28836958c1c6SMikulas Patocka 
28846958c1c6SMikulas Patocka 	return r;
288569267a30SAlasdair G Kergon }
288669267a30SAlasdair G Kergon 
28877a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
28887a8c3d3bSMike Anderson {
28897a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
28907a8c3d3bSMike Anderson }
28917a8c3d3bSMike Anderson 
28921da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
28931da177e4SLinus Torvalds {
28941da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
28951da177e4SLinus Torvalds }
28961da177e4SLinus Torvalds 
28971da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
28981da177e4SLinus Torvalds {
28991da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
29001da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
29011da177e4SLinus Torvalds }
29021da177e4SLinus Torvalds 
29037a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
29047a8c3d3bSMike Anderson {
29057a8c3d3bSMike Anderson 	unsigned long flags;
29067a8c3d3bSMike Anderson 
29077a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
29087a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
29097a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
29107a8c3d3bSMike Anderson }
29117a8c3d3bSMike Anderson 
29121da177e4SLinus Torvalds /*
29131da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
29141da177e4SLinus Torvalds  * count on 'md'.
29151da177e4SLinus Torvalds  */
29161da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
29171da177e4SLinus Torvalds {
29181da177e4SLinus Torvalds 	return md->disk;
29191da177e4SLinus Torvalds }
292065ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk);
29211da177e4SLinus Torvalds 
2922784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2923784aae73SMilan Broz {
29242995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
2925784aae73SMilan Broz }
2926784aae73SMilan Broz 
2927784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2928784aae73SMilan Broz {
2929784aae73SMilan Broz 	struct mapped_device *md;
2930784aae73SMilan Broz 
29312995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
2932784aae73SMilan Broz 
2933b9a41d21SHou Tao 	spin_lock(&_minor_lock);
2934b9a41d21SHou Tao 	if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2935b9a41d21SHou Tao 		md = NULL;
2936b9a41d21SHou Tao 		goto out;
2937b9a41d21SHou Tao 	}
2938784aae73SMilan Broz 	dm_get(md);
2939b9a41d21SHou Tao out:
2940b9a41d21SHou Tao 	spin_unlock(&_minor_lock);
2941b9a41d21SHou Tao 
2942784aae73SMilan Broz 	return md;
2943784aae73SMilan Broz }
2944784aae73SMilan Broz 
29454f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
29461da177e4SLinus Torvalds {
29471da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
29481da177e4SLinus Torvalds }
29491da177e4SLinus Torvalds 
29505df96f2bSMikulas Patocka static int dm_post_suspending_md(struct mapped_device *md)
29515df96f2bSMikulas Patocka {
29525df96f2bSMikulas Patocka 	return test_bit(DMF_POST_SUSPENDING, &md->flags);
29535df96f2bSMikulas Patocka }
29545df96f2bSMikulas Patocka 
2955ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md)
2956ffcc3936SMike Snitzer {
2957ffcc3936SMike Snitzer 	return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2958ffcc3936SMike Snitzer }
2959ffcc3936SMike Snitzer 
29602c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
29612c140a24SMikulas Patocka {
29622c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
29632c140a24SMikulas Patocka }
29642c140a24SMikulas Patocka 
296564dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
296664dbce58SKiyoshi Ueda {
296733bd6f06SMike Snitzer 	return dm_suspended_md(ti->table->md);
296864dbce58SKiyoshi Ueda }
296964dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
297064dbce58SKiyoshi Ueda 
29715df96f2bSMikulas Patocka int dm_post_suspending(struct dm_target *ti)
29725df96f2bSMikulas Patocka {
297333bd6f06SMike Snitzer 	return dm_post_suspending_md(ti->table->md);
29745df96f2bSMikulas Patocka }
29755df96f2bSMikulas Patocka EXPORT_SYMBOL_GPL(dm_post_suspending);
29765df96f2bSMikulas Patocka 
29772e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
29782e93ccc1SKiyoshi Ueda {
297933bd6f06SMike Snitzer 	return __noflush_suspending(ti->table->md);
29802e93ccc1SKiyoshi Ueda }
29812e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
29822e93ccc1SKiyoshi Ueda 
29837e0d574fSBart Van Assche struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type,
2984cfc97abcSMike Snitzer 					    unsigned per_io_data_size, unsigned min_pool_size,
2985cfc97abcSMike Snitzer 					    bool integrity, bool poll)
2986e6ee8c0bSKiyoshi Ueda {
2987115485e8SMike Snitzer 	struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
298878d8e58aSMike Snitzer 	unsigned int pool_size = 0;
298964f52b0eSMike Snitzer 	unsigned int front_pad, io_front_pad;
29906f1c819cSKent Overstreet 	int ret;
2991e6ee8c0bSKiyoshi Ueda 
2992e6ee8c0bSKiyoshi Ueda 	if (!pools)
29934e6e36c3SMike Snitzer 		return NULL;
2994e6ee8c0bSKiyoshi Ueda 
299578d8e58aSMike Snitzer 	switch (type) {
299678d8e58aSMike Snitzer 	case DM_TYPE_BIO_BASED:
2997545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
29980776aa0eSMike Snitzer 		pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size);
299962f26317SJeffle Xu 		front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + DM_TARGET_IO_BIO_OFFSET;
300062f26317SJeffle Xu 		io_front_pad = roundup(per_io_data_size,  __alignof__(struct dm_io)) + DM_IO_BIO_OFFSET;
3001cfc97abcSMike Snitzer 		ret = bioset_init(&pools->io_bs, pool_size, io_front_pad, poll ? BIOSET_PERCPU_CACHE : 0);
30026f1c819cSKent Overstreet 		if (ret)
300364f52b0eSMike Snitzer 			goto out;
30046f1c819cSKent Overstreet 		if (integrity && bioset_integrity_create(&pools->io_bs, pool_size))
3005eb8db831SChristoph Hellwig 			goto out;
300678d8e58aSMike Snitzer 		break;
300778d8e58aSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
30080776aa0eSMike Snitzer 		pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size);
300978d8e58aSMike Snitzer 		front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
3010591ddcfcSMike Snitzer 		/* per_io_data_size is used for blk-mq pdu at queue allocation */
301178d8e58aSMike Snitzer 		break;
301278d8e58aSMike Snitzer 	default:
301378d8e58aSMike Snitzer 		BUG();
301478d8e58aSMike Snitzer 	}
301578d8e58aSMike Snitzer 
30166f1c819cSKent Overstreet 	ret = bioset_init(&pools->bs, pool_size, front_pad, 0);
30176f1c819cSKent Overstreet 	if (ret)
30185f015204SJun'ichi Nomura 		goto out;
3019e6ee8c0bSKiyoshi Ueda 
30206f1c819cSKent Overstreet 	if (integrity && bioset_integrity_create(&pools->bs, pool_size))
30215f015204SJun'ichi Nomura 		goto out;
3022a91a2785SMartin K. Petersen 
3023e6ee8c0bSKiyoshi Ueda 	return pools;
302478d8e58aSMike Snitzer 
30255f015204SJun'ichi Nomura out:
30265f015204SJun'ichi Nomura 	dm_free_md_mempools(pools);
3027e6ee8c0bSKiyoshi Ueda 
30284e6e36c3SMike Snitzer 	return NULL;
3029e6ee8c0bSKiyoshi Ueda }
3030e6ee8c0bSKiyoshi Ueda 
3031e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
3032e6ee8c0bSKiyoshi Ueda {
3033e6ee8c0bSKiyoshi Ueda 	if (!pools)
3034e6ee8c0bSKiyoshi Ueda 		return;
3035e6ee8c0bSKiyoshi Ueda 
30366f1c819cSKent Overstreet 	bioset_exit(&pools->bs);
30376f1c819cSKent Overstreet 	bioset_exit(&pools->io_bs);
3038e6ee8c0bSKiyoshi Ueda 
3039e6ee8c0bSKiyoshi Ueda 	kfree(pools);
3040e6ee8c0bSKiyoshi Ueda }
3041e6ee8c0bSKiyoshi Ueda 
30429c72bad1SChristoph Hellwig struct dm_pr {
30439c72bad1SChristoph Hellwig 	u64	old_key;
30449c72bad1SChristoph Hellwig 	u64	new_key;
30459c72bad1SChristoph Hellwig 	u32	flags;
30469c72bad1SChristoph Hellwig 	bool	fail_early;
30479c72bad1SChristoph Hellwig };
30489c72bad1SChristoph Hellwig 
30499c72bad1SChristoph Hellwig static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
30509c72bad1SChristoph Hellwig 		      void *data)
30519c72bad1SChristoph Hellwig {
30529c72bad1SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
30539c72bad1SChristoph Hellwig 	struct dm_table *table;
30549c72bad1SChristoph Hellwig 	struct dm_target *ti;
30559c72bad1SChristoph Hellwig 	int ret = -ENOTTY, srcu_idx;
30569c72bad1SChristoph Hellwig 
30579c72bad1SChristoph Hellwig 	table = dm_get_live_table(md, &srcu_idx);
30589c72bad1SChristoph Hellwig 	if (!table || !dm_table_get_size(table))
30599c72bad1SChristoph Hellwig 		goto out;
30609c72bad1SChristoph Hellwig 
30619c72bad1SChristoph Hellwig 	/* We only support devices that have a single target */
30629c72bad1SChristoph Hellwig 	if (dm_table_get_num_targets(table) != 1)
30639c72bad1SChristoph Hellwig 		goto out;
30649c72bad1SChristoph Hellwig 	ti = dm_table_get_target(table, 0);
30659c72bad1SChristoph Hellwig 
30669c72bad1SChristoph Hellwig 	ret = -EINVAL;
30679c72bad1SChristoph Hellwig 	if (!ti->type->iterate_devices)
30689c72bad1SChristoph Hellwig 		goto out;
30699c72bad1SChristoph Hellwig 
30709c72bad1SChristoph Hellwig 	ret = ti->type->iterate_devices(ti, fn, data);
30719c72bad1SChristoph Hellwig out:
30729c72bad1SChristoph Hellwig 	dm_put_live_table(md, srcu_idx);
30739c72bad1SChristoph Hellwig 	return ret;
30749c72bad1SChristoph Hellwig }
30759c72bad1SChristoph Hellwig 
30769c72bad1SChristoph Hellwig /*
30779c72bad1SChristoph Hellwig  * For register / unregister we need to manually call out to every path.
30789c72bad1SChristoph Hellwig  */
30799c72bad1SChristoph Hellwig static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
30809c72bad1SChristoph Hellwig 			    sector_t start, sector_t len, void *data)
30819c72bad1SChristoph Hellwig {
30829c72bad1SChristoph Hellwig 	struct dm_pr *pr = data;
30839c72bad1SChristoph Hellwig 	const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
30849c72bad1SChristoph Hellwig 
30859c72bad1SChristoph Hellwig 	if (!ops || !ops->pr_register)
30869c72bad1SChristoph Hellwig 		return -EOPNOTSUPP;
30879c72bad1SChristoph Hellwig 	return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
30889c72bad1SChristoph Hellwig }
30899c72bad1SChristoph Hellwig 
309071cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
309171cdb697SChristoph Hellwig 			  u32 flags)
309271cdb697SChristoph Hellwig {
30939c72bad1SChristoph Hellwig 	struct dm_pr pr = {
30949c72bad1SChristoph Hellwig 		.old_key	= old_key,
30959c72bad1SChristoph Hellwig 		.new_key	= new_key,
30969c72bad1SChristoph Hellwig 		.flags		= flags,
30979c72bad1SChristoph Hellwig 		.fail_early	= true,
30989c72bad1SChristoph Hellwig 	};
30999c72bad1SChristoph Hellwig 	int ret;
310071cdb697SChristoph Hellwig 
31019c72bad1SChristoph Hellwig 	ret = dm_call_pr(bdev, __dm_pr_register, &pr);
31029c72bad1SChristoph Hellwig 	if (ret && new_key) {
31039c72bad1SChristoph Hellwig 		/* unregister all paths if we failed to register any path */
31049c72bad1SChristoph Hellwig 		pr.old_key = new_key;
31059c72bad1SChristoph Hellwig 		pr.new_key = 0;
31069c72bad1SChristoph Hellwig 		pr.flags = 0;
31079c72bad1SChristoph Hellwig 		pr.fail_early = false;
31089c72bad1SChristoph Hellwig 		dm_call_pr(bdev, __dm_pr_register, &pr);
31099c72bad1SChristoph Hellwig 	}
311071cdb697SChristoph Hellwig 
31119c72bad1SChristoph Hellwig 	return ret;
311271cdb697SChristoph Hellwig }
311371cdb697SChristoph Hellwig 
311471cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
311571cdb697SChristoph Hellwig 			 u32 flags)
311671cdb697SChristoph Hellwig {
311771cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
311871cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3119971888c4SMike Snitzer 	int r, srcu_idx;
312071cdb697SChristoph Hellwig 
31215bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
312271cdb697SChristoph Hellwig 	if (r < 0)
3123971888c4SMike Snitzer 		goto out;
312471cdb697SChristoph Hellwig 
312571cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
312671cdb697SChristoph Hellwig 	if (ops && ops->pr_reserve)
312771cdb697SChristoph Hellwig 		r = ops->pr_reserve(bdev, key, type, flags);
312871cdb697SChristoph Hellwig 	else
312971cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3130971888c4SMike Snitzer out:
3131971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
313271cdb697SChristoph Hellwig 	return r;
313371cdb697SChristoph Hellwig }
313471cdb697SChristoph Hellwig 
313571cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
313671cdb697SChristoph Hellwig {
313771cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
313871cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3139971888c4SMike Snitzer 	int r, srcu_idx;
314071cdb697SChristoph Hellwig 
31415bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
314271cdb697SChristoph Hellwig 	if (r < 0)
3143971888c4SMike Snitzer 		goto out;
314471cdb697SChristoph Hellwig 
314571cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
314671cdb697SChristoph Hellwig 	if (ops && ops->pr_release)
314771cdb697SChristoph Hellwig 		r = ops->pr_release(bdev, key, type);
314871cdb697SChristoph Hellwig 	else
314971cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3150971888c4SMike Snitzer out:
3151971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
315271cdb697SChristoph Hellwig 	return r;
315371cdb697SChristoph Hellwig }
315471cdb697SChristoph Hellwig 
315571cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
315671cdb697SChristoph Hellwig 			 enum pr_type type, bool abort)
315771cdb697SChristoph Hellwig {
315871cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
315971cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3160971888c4SMike Snitzer 	int r, srcu_idx;
316171cdb697SChristoph Hellwig 
31625bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
316371cdb697SChristoph Hellwig 	if (r < 0)
3164971888c4SMike Snitzer 		goto out;
316571cdb697SChristoph Hellwig 
316671cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
316771cdb697SChristoph Hellwig 	if (ops && ops->pr_preempt)
316871cdb697SChristoph Hellwig 		r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
316971cdb697SChristoph Hellwig 	else
317071cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3171971888c4SMike Snitzer out:
3172971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
317371cdb697SChristoph Hellwig 	return r;
317471cdb697SChristoph Hellwig }
317571cdb697SChristoph Hellwig 
317671cdb697SChristoph Hellwig static int dm_pr_clear(struct block_device *bdev, u64 key)
317771cdb697SChristoph Hellwig {
317871cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
317971cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3180971888c4SMike Snitzer 	int r, srcu_idx;
318171cdb697SChristoph Hellwig 
31825bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
318371cdb697SChristoph Hellwig 	if (r < 0)
3184971888c4SMike Snitzer 		goto out;
318571cdb697SChristoph Hellwig 
318671cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
318771cdb697SChristoph Hellwig 	if (ops && ops->pr_clear)
318871cdb697SChristoph Hellwig 		r = ops->pr_clear(bdev, key);
318971cdb697SChristoph Hellwig 	else
319071cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3191971888c4SMike Snitzer out:
3192971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
319371cdb697SChristoph Hellwig 	return r;
319471cdb697SChristoph Hellwig }
319571cdb697SChristoph Hellwig 
319671cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = {
319771cdb697SChristoph Hellwig 	.pr_register	= dm_pr_register,
319871cdb697SChristoph Hellwig 	.pr_reserve	= dm_pr_reserve,
319971cdb697SChristoph Hellwig 	.pr_release	= dm_pr_release,
320071cdb697SChristoph Hellwig 	.pr_preempt	= dm_pr_preempt,
320171cdb697SChristoph Hellwig 	.pr_clear	= dm_pr_clear,
320271cdb697SChristoph Hellwig };
320371cdb697SChristoph Hellwig 
320483d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
3205c62b37d9SChristoph Hellwig 	.submit_bio = dm_submit_bio,
3206b99fdcdcSMing Lei 	.poll_bio = dm_poll_bio,
32071da177e4SLinus Torvalds 	.open = dm_blk_open,
32081da177e4SLinus Torvalds 	.release = dm_blk_close,
3209aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
32103ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
3211e76239a3SChristoph Hellwig 	.report_zones = dm_blk_report_zones,
321271cdb697SChristoph Hellwig 	.pr_ops = &dm_pr_ops,
32131da177e4SLinus Torvalds 	.owner = THIS_MODULE
32141da177e4SLinus Torvalds };
32151da177e4SLinus Torvalds 
3216681cc5e8SMike Snitzer static const struct block_device_operations dm_rq_blk_dops = {
3217681cc5e8SMike Snitzer 	.open = dm_blk_open,
3218681cc5e8SMike Snitzer 	.release = dm_blk_close,
3219681cc5e8SMike Snitzer 	.ioctl = dm_blk_ioctl,
3220681cc5e8SMike Snitzer 	.getgeo = dm_blk_getgeo,
3221681cc5e8SMike Snitzer 	.pr_ops = &dm_pr_ops,
3222681cc5e8SMike Snitzer 	.owner = THIS_MODULE
3223681cc5e8SMike Snitzer };
3224681cc5e8SMike Snitzer 
3225f26c5719SDan Williams static const struct dax_operations dm_dax_ops = {
3226f26c5719SDan Williams 	.direct_access = dm_dax_direct_access,
3227cdf6cdcdSVivek Goyal 	.zero_page_range = dm_dax_zero_page_range,
3228047218ecSJane Chu 	.recovery_write = dm_dax_recovery_write,
3229f26c5719SDan Williams };
3230f26c5719SDan Williams 
32311da177e4SLinus Torvalds /*
32321da177e4SLinus Torvalds  * module hooks
32331da177e4SLinus Torvalds  */
32341da177e4SLinus Torvalds module_init(dm_init);
32351da177e4SLinus Torvalds module_exit(dm_exit);
32361da177e4SLinus Torvalds 
32371da177e4SLinus Torvalds module_param(major, uint, 0);
32381da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
3239f4790826SMike Snitzer 
3240e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3241e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3242e8603136SMike Snitzer 
3243115485e8SMike Snitzer module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
3244115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
3245115485e8SMike Snitzer 
3246a666e5c0SMikulas Patocka module_param(swap_bios, int, S_IRUGO | S_IWUSR);
3247a666e5c0SMikulas Patocka MODULE_PARM_DESC(swap_bios, "Maximum allowed inflight swap IOs");
3248a666e5c0SMikulas Patocka 
32491da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
32501da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
32511da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3252