xref: /openbmc/linux/drivers/md/dm.c (revision 2aec377a29250b942f14d3c16d49783da3e9df11)
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 
916c23f0bdSChristoph Hellwig static inline struct dm_target_io *clone_to_tio(struct bio *clone)
926c23f0bdSChristoph Hellwig {
936c23f0bdSChristoph Hellwig 	return container_of(clone, struct dm_target_io, clone);
946c23f0bdSChristoph Hellwig }
956c23f0bdSChristoph Hellwig 
9664f52b0eSMike Snitzer void *dm_per_bio_data(struct bio *bio, size_t data_size)
9764f52b0eSMike Snitzer {
98655f3aadSMike Snitzer 	if (!dm_tio_flagged(clone_to_tio(bio), DM_TIO_INSIDE_DM_IO))
9962f26317SJeffle Xu 		return (char *)bio - DM_TARGET_IO_BIO_OFFSET - data_size;
10062f26317SJeffle Xu 	return (char *)bio - DM_IO_BIO_OFFSET - data_size;
10164f52b0eSMike Snitzer }
10264f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_per_bio_data);
10364f52b0eSMike Snitzer 
10464f52b0eSMike Snitzer struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size)
10564f52b0eSMike Snitzer {
10664f52b0eSMike Snitzer 	struct dm_io *io = (struct dm_io *)((char *)data + data_size);
10764f52b0eSMike Snitzer 	if (io->magic == DM_IO_MAGIC)
10862f26317SJeffle Xu 		return (struct bio *)((char *)io + DM_IO_BIO_OFFSET);
10964f52b0eSMike Snitzer 	BUG_ON(io->magic != DM_TIO_MAGIC);
11062f26317SJeffle Xu 	return (struct bio *)((char *)io + DM_TARGET_IO_BIO_OFFSET);
11164f52b0eSMike Snitzer }
11264f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_from_per_bio_data);
11364f52b0eSMike Snitzer 
11464f52b0eSMike Snitzer unsigned dm_bio_get_target_bio_nr(const struct bio *bio)
11564f52b0eSMike Snitzer {
11664f52b0eSMike Snitzer 	return container_of(bio, struct dm_target_io, clone)->target_bio_nr;
11764f52b0eSMike Snitzer }
11864f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_get_target_bio_nr);
11964f52b0eSMike Snitzer 
120ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
121ba61fdd1SJeff Mahoney 
122115485e8SMike Snitzer #define DM_NUMA_NODE NUMA_NO_NODE
123115485e8SMike Snitzer static int dm_numa_node = DM_NUMA_NODE;
124faad87dfSMike Snitzer 
125a666e5c0SMikulas Patocka #define DEFAULT_SWAP_BIOS	(8 * 1048576 / PAGE_SIZE)
126a666e5c0SMikulas Patocka static int swap_bios = DEFAULT_SWAP_BIOS;
127a666e5c0SMikulas Patocka static int get_swap_bios(void)
128a666e5c0SMikulas Patocka {
129a666e5c0SMikulas Patocka 	int latch = READ_ONCE(swap_bios);
130a666e5c0SMikulas Patocka 	if (unlikely(latch <= 0))
131a666e5c0SMikulas Patocka 		latch = DEFAULT_SWAP_BIOS;
132a666e5c0SMikulas Patocka 	return latch;
133a666e5c0SMikulas Patocka }
134a666e5c0SMikulas Patocka 
13586f1152bSBenjamin Marzinski struct table_device {
13686f1152bSBenjamin Marzinski 	struct list_head list;
137b0b4d7c6SElena Reshetova 	refcount_t count;
13886f1152bSBenjamin Marzinski 	struct dm_dev dm_dev;
13986f1152bSBenjamin Marzinski };
14086f1152bSBenjamin Marzinski 
141f4790826SMike Snitzer /*
142e8603136SMike Snitzer  * Bio-based DM's mempools' reserved IOs set by the user.
143e8603136SMike Snitzer  */
1444cc96131SMike Snitzer #define RESERVED_BIO_BASED_IOS		16
145e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
146e8603136SMike Snitzer 
147115485e8SMike Snitzer static int __dm_get_module_param_int(int *module_param, int min, int max)
148115485e8SMike Snitzer {
1496aa7de05SMark Rutland 	int param = READ_ONCE(*module_param);
150115485e8SMike Snitzer 	int modified_param = 0;
151115485e8SMike Snitzer 	bool modified = true;
152115485e8SMike Snitzer 
153115485e8SMike Snitzer 	if (param < min)
154115485e8SMike Snitzer 		modified_param = min;
155115485e8SMike Snitzer 	else if (param > max)
156115485e8SMike Snitzer 		modified_param = max;
157115485e8SMike Snitzer 	else
158115485e8SMike Snitzer 		modified = false;
159115485e8SMike Snitzer 
160115485e8SMike Snitzer 	if (modified) {
161115485e8SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
162115485e8SMike Snitzer 		param = modified_param;
163115485e8SMike Snitzer 	}
164115485e8SMike Snitzer 
165115485e8SMike Snitzer 	return param;
166115485e8SMike Snitzer }
167115485e8SMike Snitzer 
1684cc96131SMike Snitzer unsigned __dm_get_module_param(unsigned *module_param,
169f4790826SMike Snitzer 			       unsigned def, unsigned max)
170f4790826SMike Snitzer {
1716aa7de05SMark Rutland 	unsigned param = READ_ONCE(*module_param);
17209c2d531SMike Snitzer 	unsigned modified_param = 0;
173f4790826SMike Snitzer 
17409c2d531SMike Snitzer 	if (!param)
17509c2d531SMike Snitzer 		modified_param = def;
17609c2d531SMike Snitzer 	else if (param > max)
17709c2d531SMike Snitzer 		modified_param = max;
178f4790826SMike Snitzer 
17909c2d531SMike Snitzer 	if (modified_param) {
18009c2d531SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
18109c2d531SMike Snitzer 		param = modified_param;
182f4790826SMike Snitzer 	}
183f4790826SMike Snitzer 
18409c2d531SMike Snitzer 	return param;
185f4790826SMike Snitzer }
186f4790826SMike Snitzer 
187e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void)
188e8603136SMike Snitzer {
18909c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_bio_based_ios,
1904cc96131SMike Snitzer 				     RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS);
191e8603136SMike Snitzer }
192e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
193e8603136SMike Snitzer 
194115485e8SMike Snitzer static unsigned dm_get_numa_node(void)
195115485e8SMike Snitzer {
196115485e8SMike Snitzer 	return __dm_get_module_param_int(&dm_numa_node,
197115485e8SMike Snitzer 					 DM_NUMA_NODE, num_online_nodes() - 1);
198115485e8SMike Snitzer }
199115485e8SMike Snitzer 
2001da177e4SLinus Torvalds static int __init local_init(void)
2011da177e4SLinus Torvalds {
202e689fbabSMike Snitzer 	int r;
2031ae49ea2SMike Snitzer 
20451e5b2bdSMike Anderson 	r = dm_uevent_init();
20551157b4aSKiyoshi Ueda 	if (r)
206e689fbabSMike Snitzer 		return r;
20751e5b2bdSMike Anderson 
208acfe0ad7SMikulas Patocka 	deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
209acfe0ad7SMikulas Patocka 	if (!deferred_remove_workqueue) {
210acfe0ad7SMikulas Patocka 		r = -ENOMEM;
211acfe0ad7SMikulas Patocka 		goto out_uevent_exit;
212acfe0ad7SMikulas Patocka 	}
213acfe0ad7SMikulas Patocka 
2141da177e4SLinus Torvalds 	_major = major;
2151da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
21651157b4aSKiyoshi Ueda 	if (r < 0)
217acfe0ad7SMikulas Patocka 		goto out_free_workqueue;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	if (!_major)
2201da177e4SLinus Torvalds 		_major = r;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	return 0;
22351157b4aSKiyoshi Ueda 
224acfe0ad7SMikulas Patocka out_free_workqueue:
225acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
22651157b4aSKiyoshi Ueda out_uevent_exit:
22751157b4aSKiyoshi Ueda 	dm_uevent_exit();
22851157b4aSKiyoshi Ueda 
22951157b4aSKiyoshi Ueda 	return r;
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds static void local_exit(void)
2331da177e4SLinus Torvalds {
2342c140a24SMikulas Patocka 	flush_scheduled_work();
235acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
2362c140a24SMikulas Patocka 
23700d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
23851e5b2bdSMike Anderson 	dm_uevent_exit();
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	_major = 0;
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	DMINFO("cleaned up");
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
245b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
2461da177e4SLinus Torvalds 	local_init,
2471da177e4SLinus Torvalds 	dm_target_init,
2481da177e4SLinus Torvalds 	dm_linear_init,
2491da177e4SLinus Torvalds 	dm_stripe_init,
250952b3557SMikulas Patocka 	dm_io_init,
251945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2521da177e4SLinus Torvalds 	dm_interface_init,
253fd2ed4d2SMikulas Patocka 	dm_statistics_init,
2541da177e4SLinus Torvalds };
2551da177e4SLinus Torvalds 
256b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2571da177e4SLinus Torvalds 	local_exit,
2581da177e4SLinus Torvalds 	dm_target_exit,
2591da177e4SLinus Torvalds 	dm_linear_exit,
2601da177e4SLinus Torvalds 	dm_stripe_exit,
261952b3557SMikulas Patocka 	dm_io_exit,
262945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
2631da177e4SLinus Torvalds 	dm_interface_exit,
264fd2ed4d2SMikulas Patocka 	dm_statistics_exit,
2651da177e4SLinus Torvalds };
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds static int __init dm_init(void)
2681da177e4SLinus Torvalds {
2691da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
2701da177e4SLinus Torvalds 	int r, i;
2711da177e4SLinus Torvalds 
272f1cd6cb2STushar Sugandhi #if (IS_ENABLED(CONFIG_IMA) && !IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE))
273f1cd6cb2STushar Sugandhi 	DMWARN("CONFIG_IMA_DISABLE_HTABLE is disabled."
274f1cd6cb2STushar Sugandhi 	       " Duplicate IMA measurements will not be recorded in the IMA log.");
275f1cd6cb2STushar Sugandhi #endif
276f1cd6cb2STushar Sugandhi 
2771da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
2781da177e4SLinus Torvalds 		r = _inits[i]();
2791da177e4SLinus Torvalds 		if (r)
2801da177e4SLinus Torvalds 			goto bad;
2811da177e4SLinus Torvalds 	}
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	return 0;
2841da177e4SLinus Torvalds bad:
2851da177e4SLinus Torvalds 	while (i--)
2861da177e4SLinus Torvalds 		_exits[i]();
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	return r;
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds static void __exit dm_exit(void)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	while (i--)
2961da177e4SLinus Torvalds 		_exits[i]();
297d15b774cSAlasdair G Kergon 
298d15b774cSAlasdair G Kergon 	/*
299d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
300d15b774cSAlasdair G Kergon 	 */
301d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds /*
3051da177e4SLinus Torvalds  * Block device functions
3061da177e4SLinus Torvalds  */
307432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
308432a212cSMike Anderson {
309432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
310432a212cSMike Anderson }
311432a212cSMike Anderson 
312fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3131da177e4SLinus Torvalds {
3141da177e4SLinus Torvalds 	struct mapped_device *md;
3151da177e4SLinus Torvalds 
316fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
317fba9f90eSJeff Mahoney 
318fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
319fba9f90eSJeff Mahoney 	if (!md)
320fba9f90eSJeff Mahoney 		goto out;
321fba9f90eSJeff Mahoney 
3225c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
323432a212cSMike Anderson 	    dm_deleting_md(md)) {
324fba9f90eSJeff Mahoney 		md = NULL;
325fba9f90eSJeff Mahoney 		goto out;
326fba9f90eSJeff Mahoney 	}
327fba9f90eSJeff Mahoney 
3281da177e4SLinus Torvalds 	dm_get(md);
3295c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
330fba9f90eSJeff Mahoney out:
331fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
332fba9f90eSJeff Mahoney 
333fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
336db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode)
3371da177e4SLinus Torvalds {
33863a4f065SMike Snitzer 	struct mapped_device *md;
3396e9624b8SArnd Bergmann 
3404a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
3414a1aeb98SMilan Broz 
34263a4f065SMike Snitzer 	md = disk->private_data;
34363a4f065SMike Snitzer 	if (WARN_ON(!md))
34463a4f065SMike Snitzer 		goto out;
34563a4f065SMike Snitzer 
3462c140a24SMikulas Patocka 	if (atomic_dec_and_test(&md->open_count) &&
3472c140a24SMikulas Patocka 	    (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
348acfe0ad7SMikulas Patocka 		queue_work(deferred_remove_workqueue, &deferred_remove_work);
3492c140a24SMikulas Patocka 
3501da177e4SLinus Torvalds 	dm_put(md);
35163a4f065SMike Snitzer out:
3524a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
3531da177e4SLinus Torvalds }
3541da177e4SLinus Torvalds 
3555c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3565c6bd75dSAlasdair G Kergon {
3575c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3585c6bd75dSAlasdair G Kergon }
3595c6bd75dSAlasdair G Kergon 
3605c6bd75dSAlasdair G Kergon /*
3615c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3625c6bd75dSAlasdair G Kergon  */
3632c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
3645c6bd75dSAlasdair G Kergon {
3655c6bd75dSAlasdair G Kergon 	int r = 0;
3665c6bd75dSAlasdair G Kergon 
3675c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
3685c6bd75dSAlasdair G Kergon 
3692c140a24SMikulas Patocka 	if (dm_open_count(md)) {
3705c6bd75dSAlasdair G Kergon 		r = -EBUSY;
3712c140a24SMikulas Patocka 		if (mark_deferred)
3722c140a24SMikulas Patocka 			set_bit(DMF_DEFERRED_REMOVE, &md->flags);
3732c140a24SMikulas Patocka 	} else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
3742c140a24SMikulas Patocka 		r = -EEXIST;
3755c6bd75dSAlasdair G Kergon 	else
3765c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
3775c6bd75dSAlasdair G Kergon 
3785c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
3795c6bd75dSAlasdair G Kergon 
3805c6bd75dSAlasdair G Kergon 	return r;
3815c6bd75dSAlasdair G Kergon }
3825c6bd75dSAlasdair G Kergon 
3832c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md)
3842c140a24SMikulas Patocka {
3852c140a24SMikulas Patocka 	int r = 0;
3862c140a24SMikulas Patocka 
3872c140a24SMikulas Patocka 	spin_lock(&_minor_lock);
3882c140a24SMikulas Patocka 
3892c140a24SMikulas Patocka 	if (test_bit(DMF_DELETING, &md->flags))
3902c140a24SMikulas Patocka 		r = -EBUSY;
3912c140a24SMikulas Patocka 	else
3922c140a24SMikulas Patocka 		clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
3932c140a24SMikulas Patocka 
3942c140a24SMikulas Patocka 	spin_unlock(&_minor_lock);
3952c140a24SMikulas Patocka 
3962c140a24SMikulas Patocka 	return r;
3972c140a24SMikulas Patocka }
3982c140a24SMikulas Patocka 
3992c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w)
4002c140a24SMikulas Patocka {
4012c140a24SMikulas Patocka 	dm_deferred_remove();
4022c140a24SMikulas Patocka }
4032c140a24SMikulas Patocka 
4043ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4053ac51e74SDarrick J. Wong {
4063ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
4073ac51e74SDarrick J. Wong 
4083ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
4093ac51e74SDarrick J. Wong }
4103ac51e74SDarrick J. Wong 
411971888c4SMike Snitzer static int dm_prepare_ioctl(struct mapped_device *md, int *srcu_idx,
4125bd5e8d8SMike Snitzer 			    struct block_device **bdev)
413aa129a22SMilan Broz {
41466482026SMike Snitzer 	struct dm_target *tgt;
4156c182cd8SHannes Reinecke 	struct dm_table *map;
416971888c4SMike Snitzer 	int r;
417aa129a22SMilan Broz 
4186c182cd8SHannes Reinecke retry:
419e56f81e0SChristoph Hellwig 	r = -ENOTTY;
420971888c4SMike Snitzer 	map = dm_get_live_table(md, srcu_idx);
421aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
422971888c4SMike Snitzer 		return r;
423aa129a22SMilan Broz 
424aa129a22SMilan Broz 	/* We only support devices that have a single target */
425*2aec377aSMike Snitzer 	if (map->num_targets != 1)
426971888c4SMike Snitzer 		return r;
427aa129a22SMilan Broz 
42866482026SMike Snitzer 	tgt = dm_table_get_target(map, 0);
42966482026SMike Snitzer 	if (!tgt->type->prepare_ioctl)
430e56f81e0SChristoph Hellwig 		return r;
431aa129a22SMilan Broz 
432971888c4SMike Snitzer 	if (dm_suspended_md(md))
433971888c4SMike Snitzer 		return -EAGAIN;
434971888c4SMike Snitzer 
4355bd5e8d8SMike Snitzer 	r = tgt->type->prepare_ioctl(tgt, bdev);
4365bbbfdf6SJunichi Nomura 	if (r == -ENOTCONN && !fatal_signal_pending(current)) {
437971888c4SMike Snitzer 		dm_put_live_table(md, *srcu_idx);
4386c182cd8SHannes Reinecke 		msleep(10);
4396c182cd8SHannes Reinecke 		goto retry;
4406c182cd8SHannes Reinecke 	}
441971888c4SMike Snitzer 
442e56f81e0SChristoph Hellwig 	return r;
443e56f81e0SChristoph Hellwig }
4446c182cd8SHannes Reinecke 
445971888c4SMike Snitzer static void dm_unprepare_ioctl(struct mapped_device *md, int srcu_idx)
446971888c4SMike Snitzer {
447971888c4SMike Snitzer 	dm_put_live_table(md, srcu_idx);
448971888c4SMike Snitzer }
449971888c4SMike Snitzer 
450e56f81e0SChristoph Hellwig static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
451e56f81e0SChristoph Hellwig 			unsigned int cmd, unsigned long arg)
452e56f81e0SChristoph Hellwig {
453e56f81e0SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
454971888c4SMike Snitzer 	int r, srcu_idx;
455e56f81e0SChristoph Hellwig 
4565bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
457e56f81e0SChristoph Hellwig 	if (r < 0)
458971888c4SMike Snitzer 		goto out;
459e56f81e0SChristoph Hellwig 
460e56f81e0SChristoph Hellwig 	if (r > 0) {
461e56f81e0SChristoph Hellwig 		/*
462e980f623SChristoph Hellwig 		 * Target determined this ioctl is being issued against a
463e980f623SChristoph Hellwig 		 * subset of the parent bdev; require extra privileges.
464e56f81e0SChristoph Hellwig 		 */
465e980f623SChristoph Hellwig 		if (!capable(CAP_SYS_RAWIO)) {
4660378c625SMike Snitzer 			DMDEBUG_LIMIT(
467e980f623SChristoph Hellwig 	"%s: sending ioctl %x to DM device without required privilege.",
468e980f623SChristoph Hellwig 				current->comm, cmd);
469e980f623SChristoph Hellwig 			r = -ENOIOCTLCMD;
470e56f81e0SChristoph Hellwig 			goto out;
471e56f81e0SChristoph Hellwig 		}
472e980f623SChristoph Hellwig 	}
473e56f81e0SChristoph Hellwig 
474a7cb3d2fSChristoph Hellwig 	if (!bdev->bd_disk->fops->ioctl)
475a7cb3d2fSChristoph Hellwig 		r = -ENOTTY;
476a7cb3d2fSChristoph Hellwig 	else
477a7cb3d2fSChristoph Hellwig 		r = bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg);
478e56f81e0SChristoph Hellwig out:
479971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
480aa129a22SMilan Broz 	return r;
481aa129a22SMilan Broz }
482aa129a22SMilan Broz 
4837465d7acSMike Snitzer u64 dm_start_time_ns_from_clone(struct bio *bio)
4847465d7acSMike Snitzer {
4856c23f0bdSChristoph Hellwig 	return jiffies_to_nsecs(clone_to_tio(bio)->io->start_time);
4867465d7acSMike Snitzer }
4877465d7acSMike Snitzer EXPORT_SYMBOL_GPL(dm_start_time_ns_from_clone);
4887465d7acSMike Snitzer 
4898d394bc4SMike Snitzer static bool bio_is_flush_with_data(struct bio *bio)
4907465d7acSMike Snitzer {
4918d394bc4SMike Snitzer 	return ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size);
4927465d7acSMike Snitzer }
4937465d7acSMike Snitzer 
494e6926ad0SMing Lei static void dm_io_acct(struct dm_io *io, bool end)
4957465d7acSMike Snitzer {
496e6926ad0SMing Lei 	struct dm_stats_aux *stats_aux = &io->stats_aux;
497e6926ad0SMing Lei 	unsigned long start_time = io->start_time;
498e6926ad0SMing Lei 	struct mapped_device *md = io->md;
499e6926ad0SMing Lei 	struct bio *bio = io->orig_bio;
500d3de6d12SMing Lei 	unsigned int sectors;
5017465d7acSMike Snitzer 
502d3de6d12SMing Lei 	/*
503d3de6d12SMing Lei 	 * If REQ_PREFLUSH set, don't account payload, it will be
504d3de6d12SMing Lei 	 * submitted (and accounted) after this flush completes.
505d3de6d12SMing Lei 	 */
506d3de6d12SMing Lei 	if (bio_is_flush_with_data(bio))
507d3de6d12SMing Lei 		sectors = 0;
5087dd76d1fSMing Lei 	else if (likely(!(dm_io_flagged(io, DM_IO_WAS_SPLIT))))
509d3de6d12SMing Lei 		sectors = bio_sectors(bio);
5107dd76d1fSMing Lei 	else
5117dd76d1fSMing Lei 		sectors = io->sectors;
5128d394bc4SMike Snitzer 
5138d394bc4SMike Snitzer 	if (!end)
514d3de6d12SMing Lei 		bdev_start_io_acct(bio->bi_bdev, sectors, bio_op(bio),
515d3de6d12SMing Lei 				   start_time);
5168d394bc4SMike Snitzer 	else
517d3de6d12SMing Lei 		bdev_end_io_acct(bio->bi_bdev, bio_op(bio), start_time);
5187465d7acSMike Snitzer 
519442761fdSMike Snitzer 	if (static_branch_unlikely(&stats_enabled) &&
5207dd76d1fSMing Lei 	    unlikely(dm_stats_used(&md->stats))) {
5217dd76d1fSMing Lei 		sector_t sector;
5227dd76d1fSMing Lei 
5237dd76d1fSMing Lei 		if (likely(!dm_io_flagged(io, DM_IO_WAS_SPLIT)))
5247dd76d1fSMing Lei 			sector = bio->bi_iter.bi_sector;
5257dd76d1fSMing Lei 		else
5267dd76d1fSMing Lei 			sector = bio_end_sector(bio) - io->sector_offset;
5277dd76d1fSMing Lei 
5287465d7acSMike Snitzer 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
5297dd76d1fSMing Lei 				    sector, sectors,
5308d394bc4SMike Snitzer 				    end, start_time, stats_aux);
5318d394bc4SMike Snitzer 	}
5327dd76d1fSMing Lei }
5338d394bc4SMike Snitzer 
534b992b40dSMing Lei static void __dm_start_io_acct(struct dm_io *io)
5358d394bc4SMike Snitzer {
536e6926ad0SMing Lei 	dm_io_acct(io, false);
5378d394bc4SMike Snitzer }
5388d394bc4SMike Snitzer 
5390fbb4d93SMike Snitzer static void dm_start_io_acct(struct dm_io *io, struct bio *clone)
5408d394bc4SMike Snitzer {
5410fbb4d93SMike Snitzer 	/*
5420fbb4d93SMike Snitzer 	 * Ensure IO accounting is only ever started once.
5430fbb4d93SMike Snitzer 	 */
5443b03f7c1SMike Snitzer 	if (dm_io_flagged(io, DM_IO_ACCOUNTED))
5450fbb4d93SMike Snitzer 		return;
5463b03f7c1SMike Snitzer 
5473b03f7c1SMike Snitzer 	/* Expect no possibility for race unless DM_TIO_IS_DUPLICATE_BIO. */
5483b03f7c1SMike Snitzer 	if (!clone || likely(dm_tio_is_normal(clone_to_tio(clone)))) {
54982f6cdccSMike Snitzer 		dm_io_set_flag(io, DM_IO_ACCOUNTED);
55082f6cdccSMike Snitzer 	} else {
55182f6cdccSMike Snitzer 		unsigned long flags;
552655f3aadSMike Snitzer 		/* Can afford locking given DM_TIO_IS_DUPLICATE_BIO */
5534d7bca13SMike Snitzer 		spin_lock_irqsave(&io->lock, flags);
55410eb3a0dSBenjamin Marzinski 		if (dm_io_flagged(io, DM_IO_ACCOUNTED)) {
55510eb3a0dSBenjamin Marzinski 			spin_unlock_irqrestore(&io->lock, flags);
55610eb3a0dSBenjamin Marzinski 			return;
55710eb3a0dSBenjamin Marzinski 		}
55882f6cdccSMike Snitzer 		dm_io_set_flag(io, DM_IO_ACCOUNTED);
5594d7bca13SMike Snitzer 		spin_unlock_irqrestore(&io->lock, flags);
56082f6cdccSMike Snitzer 	}
5610fbb4d93SMike Snitzer 
562b992b40dSMing Lei 	__dm_start_io_acct(io);
5630fbb4d93SMike Snitzer }
5640fbb4d93SMike Snitzer 
565b992b40dSMing Lei static void dm_end_io_acct(struct dm_io *io)
5660fbb4d93SMike Snitzer {
567e6926ad0SMing Lei 	dm_io_acct(io, true);
5687465d7acSMike Snitzer }
569978e51baSMike Snitzer 
570978e51baSMike Snitzer static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio)
5711da177e4SLinus Torvalds {
57264f52b0eSMike Snitzer 	struct dm_io *io;
57364f52b0eSMike Snitzer 	struct dm_target_io *tio;
57464f52b0eSMike Snitzer 	struct bio *clone;
57564f52b0eSMike Snitzer 
57629dec90aSChristoph Hellwig 	clone = bio_alloc_clone(NULL, bio, GFP_NOIO, &md->mempools->io_bs);
577ca522482SMike Snitzer 	/* Set default bdev, but target must bio_set_dev() before issuing IO */
578ca522482SMike Snitzer 	clone->bi_bdev = md->disk->part0;
57964f52b0eSMike Snitzer 
5806c23f0bdSChristoph Hellwig 	tio = clone_to_tio(clone);
581655f3aadSMike Snitzer 	tio->flags = 0;
582655f3aadSMike Snitzer 	dm_tio_set_flag(tio, DM_TIO_INSIDE_DM_IO);
58364f52b0eSMike Snitzer 	tio->io = NULL;
58464f52b0eSMike Snitzer 
58564f52b0eSMike Snitzer 	io = container_of(tio, struct dm_io, tio);
58664f52b0eSMike Snitzer 	io->magic = DM_IO_MAGIC;
58784b98f4cSMike Snitzer 	io->status = BLK_STS_OK;
5880f14d60aSMing Lei 
5890f14d60aSMing Lei 	/* one ref is for submission, the other is for completion */
5900f14d60aSMing Lei 	atomic_set(&io->io_count, 2);
5919f6dc633SMike Snitzer 	this_cpu_inc(*md->pending_io);
5927dd76d1fSMing Lei 	io->orig_bio = bio;
593978e51baSMike Snitzer 	io->md = md;
5944d7bca13SMike Snitzer 	spin_lock_init(&io->lock);
595b879f915SMike Snitzer 	io->start_time = jiffies;
59682f6cdccSMike Snitzer 	io->flags = 0;
59764f52b0eSMike Snitzer 
598442761fdSMike Snitzer 	if (static_branch_unlikely(&stats_enabled))
5990cdb90f0SMike Snitzer 		dm_stats_record_start(&md->stats, &io->stats_aux);
60064f52b0eSMike Snitzer 
60164f52b0eSMike Snitzer 	return io;
6021da177e4SLinus Torvalds }
6031da177e4SLinus Torvalds 
6040119ab14SMike Snitzer static void free_io(struct dm_io *io)
6051da177e4SLinus Torvalds {
60664f52b0eSMike Snitzer 	bio_put(&io->tio.clone);
60764f52b0eSMike Snitzer }
60864f52b0eSMike Snitzer 
6091d1068ceSChristoph Hellwig static struct bio *alloc_tio(struct clone_info *ci, struct dm_target *ti,
610dc8e2021SChristoph Hellwig 			     unsigned target_bio_nr, unsigned *len, gfp_t gfp_mask)
61164f52b0eSMike Snitzer {
61264f52b0eSMike Snitzer 	struct dm_target_io *tio;
613018b05ebSMike Snitzer 	struct bio *clone;
61464f52b0eSMike Snitzer 
61564f52b0eSMike Snitzer 	if (!ci->io->tio.io) {
61664f52b0eSMike Snitzer 		/* the dm_target_io embedded in ci->io is available */
61764f52b0eSMike Snitzer 		tio = &ci->io->tio;
618018b05ebSMike Snitzer 		/* alloc_io() already initialized embedded clone */
619018b05ebSMike Snitzer 		clone = &tio->clone;
62064f52b0eSMike Snitzer 	} else {
621ca522482SMike Snitzer 		struct mapped_device *md = ci->io->md;
622ca522482SMike Snitzer 
62329dec90aSChristoph Hellwig 		clone = bio_alloc_clone(NULL, ci->bio, gfp_mask,
62429dec90aSChristoph Hellwig 					&md->mempools->bs);
62564f52b0eSMike Snitzer 		if (!clone)
62664f52b0eSMike Snitzer 			return NULL;
627ca522482SMike Snitzer 		/* Set default bdev, but target must bio_set_dev() before issuing IO */
628ca522482SMike Snitzer 		clone->bi_bdev = md->disk->part0;
62964f52b0eSMike Snitzer 
630b99fdcdcSMing Lei 		/* REQ_DM_POLL_LIST shouldn't be inherited */
631b99fdcdcSMing Lei 		clone->bi_opf &= ~REQ_DM_POLL_LIST;
632b99fdcdcSMing Lei 
6336c23f0bdSChristoph Hellwig 		tio = clone_to_tio(clone);
634655f3aadSMike Snitzer 		tio->flags = 0; /* also clears DM_TIO_INSIDE_DM_IO */
63564f52b0eSMike Snitzer 	}
63664f52b0eSMike Snitzer 
63764f52b0eSMike Snitzer 	tio->magic = DM_TIO_MAGIC;
63864f52b0eSMike Snitzer 	tio->io = ci->io;
63964f52b0eSMike Snitzer 	tio->ti = ti;
64064f52b0eSMike Snitzer 	tio->target_bio_nr = target_bio_nr;
641dc8e2021SChristoph Hellwig 	tio->len_ptr = len;
642743598f0SMike Snitzer 	tio->old_sector = 0;
64364f52b0eSMike Snitzer 
644018b05ebSMike Snitzer 	if (len) {
645018b05ebSMike Snitzer 		clone->bi_iter.bi_size = to_bytes(*len);
646018b05ebSMike Snitzer 		if (bio_integrity(clone))
647018b05ebSMike Snitzer 			bio_integrity_trim(clone);
648018b05ebSMike Snitzer 	}
649018b05ebSMike Snitzer 
650018b05ebSMike Snitzer 	return clone;
6511da177e4SLinus Torvalds }
6521da177e4SLinus Torvalds 
6531d1068ceSChristoph Hellwig static void free_tio(struct bio *clone)
6541da177e4SLinus Torvalds {
655655f3aadSMike Snitzer 	if (dm_tio_flagged(clone_to_tio(clone), DM_TIO_INSIDE_DM_IO))
65664f52b0eSMike Snitzer 		return;
6571d1068ceSChristoph Hellwig 	bio_put(clone);
6581da177e4SLinus Torvalds }
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds /*
6611da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
6621da177e4SLinus Torvalds  */
66392c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
6641da177e4SLinus Torvalds {
66505447420SKiyoshi Ueda 	unsigned long flags;
6661da177e4SLinus Torvalds 
66705447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
6681da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
66905447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
67092c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
6711da177e4SLinus Torvalds }
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds /*
6741da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
6751da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
67683d5e5b0SMikulas Patocka  * dm_put_live_table() when finished.
6771da177e4SLinus Torvalds  */
678563a225cSMike Snitzer struct dm_table *dm_get_live_table(struct mapped_device *md,
679563a225cSMike Snitzer 				   int *srcu_idx) __acquires(md->io_barrier)
6801da177e4SLinus Torvalds {
68183d5e5b0SMikulas Patocka 	*srcu_idx = srcu_read_lock(&md->io_barrier);
6821da177e4SLinus Torvalds 
68383d5e5b0SMikulas Patocka 	return srcu_dereference(md->map, &md->io_barrier);
68483d5e5b0SMikulas Patocka }
6851da177e4SLinus Torvalds 
686563a225cSMike Snitzer void dm_put_live_table(struct mapped_device *md,
687563a225cSMike Snitzer 		       int srcu_idx) __releases(md->io_barrier)
68883d5e5b0SMikulas Patocka {
68983d5e5b0SMikulas Patocka 	srcu_read_unlock(&md->io_barrier, srcu_idx);
69083d5e5b0SMikulas Patocka }
69183d5e5b0SMikulas Patocka 
69283d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md)
69383d5e5b0SMikulas Patocka {
69483d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
69583d5e5b0SMikulas Patocka 	synchronize_rcu_expedited();
69683d5e5b0SMikulas Patocka }
69783d5e5b0SMikulas Patocka 
69883d5e5b0SMikulas Patocka /*
69983d5e5b0SMikulas Patocka  * A fast alternative to dm_get_live_table/dm_put_live_table.
70083d5e5b0SMikulas Patocka  * The caller must not block between these two functions.
70183d5e5b0SMikulas Patocka  */
70283d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
70383d5e5b0SMikulas Patocka {
70483d5e5b0SMikulas Patocka 	rcu_read_lock();
70583d5e5b0SMikulas Patocka 	return rcu_dereference(md->map);
70683d5e5b0SMikulas Patocka }
70783d5e5b0SMikulas Patocka 
70883d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
70983d5e5b0SMikulas Patocka {
71083d5e5b0SMikulas Patocka 	rcu_read_unlock();
7111da177e4SLinus Torvalds }
7121da177e4SLinus Torvalds 
713563a225cSMike Snitzer static inline struct dm_table *dm_get_live_table_bio(struct mapped_device *md,
7145d7362d0SMikulas Patocka 						     int *srcu_idx, unsigned bio_opf)
715563a225cSMike Snitzer {
7165d7362d0SMikulas Patocka 	if (bio_opf & REQ_NOWAIT)
717563a225cSMike Snitzer 		return dm_get_live_table_fast(md);
718563a225cSMike Snitzer 	else
719563a225cSMike Snitzer 		return dm_get_live_table(md, srcu_idx);
720563a225cSMike Snitzer }
721563a225cSMike Snitzer 
722563a225cSMike Snitzer static inline void dm_put_live_table_bio(struct mapped_device *md, int srcu_idx,
7235d7362d0SMikulas Patocka 					 unsigned bio_opf)
724563a225cSMike Snitzer {
7255d7362d0SMikulas Patocka 	if (bio_opf & REQ_NOWAIT)
726563a225cSMike Snitzer 		dm_put_live_table_fast(md);
727563a225cSMike Snitzer 	else
728563a225cSMike Snitzer 		dm_put_live_table(md, srcu_idx);
729563a225cSMike Snitzer }
730563a225cSMike Snitzer 
731971888c4SMike Snitzer static char *_dm_claim_ptr = "I belong to device-mapper";
732971888c4SMike Snitzer 
7333ac51e74SDarrick J. Wong /*
73486f1152bSBenjamin Marzinski  * Open a table device so we can use it as a map destination.
73586f1152bSBenjamin Marzinski  */
73686f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev,
73786f1152bSBenjamin Marzinski 			     struct mapped_device *md)
73886f1152bSBenjamin Marzinski {
73986f1152bSBenjamin Marzinski 	struct block_device *bdev;
740cd913c76SChristoph Hellwig 	u64 part_off;
74186f1152bSBenjamin Marzinski 	int r;
74286f1152bSBenjamin Marzinski 
74386f1152bSBenjamin Marzinski 	BUG_ON(td->dm_dev.bdev);
74486f1152bSBenjamin Marzinski 
745519049afSMike Snitzer 	bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr);
74686f1152bSBenjamin Marzinski 	if (IS_ERR(bdev))
74786f1152bSBenjamin Marzinski 		return PTR_ERR(bdev);
74886f1152bSBenjamin Marzinski 
74986f1152bSBenjamin Marzinski 	r = bd_link_disk_holder(bdev, dm_disk(md));
75086f1152bSBenjamin Marzinski 	if (r) {
75186f1152bSBenjamin Marzinski 		blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
75286f1152bSBenjamin Marzinski 		return r;
75386f1152bSBenjamin Marzinski 	}
75486f1152bSBenjamin Marzinski 
75586f1152bSBenjamin Marzinski 	td->dm_dev.bdev = bdev;
756cd913c76SChristoph Hellwig 	td->dm_dev.dax_dev = fs_dax_get_by_bdev(bdev, &part_off);
75786f1152bSBenjamin Marzinski 	return 0;
75886f1152bSBenjamin Marzinski }
75986f1152bSBenjamin Marzinski 
76086f1152bSBenjamin Marzinski /*
76186f1152bSBenjamin Marzinski  * Close a table device that we've been using.
76286f1152bSBenjamin Marzinski  */
76386f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md)
76486f1152bSBenjamin Marzinski {
76586f1152bSBenjamin Marzinski 	if (!td->dm_dev.bdev)
76686f1152bSBenjamin Marzinski 		return;
76786f1152bSBenjamin Marzinski 
76886f1152bSBenjamin Marzinski 	bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
76986f1152bSBenjamin Marzinski 	blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
770817bf402SDan Williams 	put_dax(td->dm_dev.dax_dev);
77186f1152bSBenjamin Marzinski 	td->dm_dev.bdev = NULL;
772817bf402SDan Williams 	td->dm_dev.dax_dev = NULL;
77386f1152bSBenjamin Marzinski }
77486f1152bSBenjamin Marzinski 
77586f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev,
7768454fca4SSheetal Singala 					      fmode_t mode)
7778454fca4SSheetal Singala {
77886f1152bSBenjamin Marzinski 	struct table_device *td;
77986f1152bSBenjamin Marzinski 
78086f1152bSBenjamin Marzinski 	list_for_each_entry(td, l, list)
78186f1152bSBenjamin Marzinski 		if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
78286f1152bSBenjamin Marzinski 			return td;
78386f1152bSBenjamin Marzinski 
78486f1152bSBenjamin Marzinski 	return NULL;
78586f1152bSBenjamin Marzinski }
78686f1152bSBenjamin Marzinski 
78786f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
7888454fca4SSheetal Singala 			struct dm_dev **result)
7898454fca4SSheetal Singala {
79086f1152bSBenjamin Marzinski 	int r;
79186f1152bSBenjamin Marzinski 	struct table_device *td;
79286f1152bSBenjamin Marzinski 
79386f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
79486f1152bSBenjamin Marzinski 	td = find_table_device(&md->table_devices, dev, mode);
79586f1152bSBenjamin Marzinski 	if (!td) {
796115485e8SMike Snitzer 		td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
79786f1152bSBenjamin Marzinski 		if (!td) {
79886f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
79986f1152bSBenjamin Marzinski 			return -ENOMEM;
80086f1152bSBenjamin Marzinski 		}
80186f1152bSBenjamin Marzinski 
80286f1152bSBenjamin Marzinski 		td->dm_dev.mode = mode;
80386f1152bSBenjamin Marzinski 		td->dm_dev.bdev = NULL;
80486f1152bSBenjamin Marzinski 
80586f1152bSBenjamin Marzinski 		if ((r = open_table_device(td, dev, md))) {
80686f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
80786f1152bSBenjamin Marzinski 			kfree(td);
80886f1152bSBenjamin Marzinski 			return r;
80986f1152bSBenjamin Marzinski 		}
81086f1152bSBenjamin Marzinski 
81186f1152bSBenjamin Marzinski 		format_dev_t(td->dm_dev.name, dev);
81286f1152bSBenjamin Marzinski 
813b0b4d7c6SElena Reshetova 		refcount_set(&td->count, 1);
81486f1152bSBenjamin Marzinski 		list_add(&td->list, &md->table_devices);
815b0b4d7c6SElena Reshetova 	} else {
816b0b4d7c6SElena Reshetova 		refcount_inc(&td->count);
81786f1152bSBenjamin Marzinski 	}
81886f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
81986f1152bSBenjamin Marzinski 
82086f1152bSBenjamin Marzinski 	*result = &td->dm_dev;
82186f1152bSBenjamin Marzinski 	return 0;
82286f1152bSBenjamin Marzinski }
82386f1152bSBenjamin Marzinski 
82486f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
82586f1152bSBenjamin Marzinski {
82686f1152bSBenjamin Marzinski 	struct table_device *td = container_of(d, struct table_device, dm_dev);
82786f1152bSBenjamin Marzinski 
82886f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
829b0b4d7c6SElena Reshetova 	if (refcount_dec_and_test(&td->count)) {
83086f1152bSBenjamin Marzinski 		close_table_device(td, md);
83186f1152bSBenjamin Marzinski 		list_del(&td->list);
83286f1152bSBenjamin Marzinski 		kfree(td);
83386f1152bSBenjamin Marzinski 	}
83486f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
83586f1152bSBenjamin Marzinski }
83686f1152bSBenjamin Marzinski 
83786f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices)
83886f1152bSBenjamin Marzinski {
83986f1152bSBenjamin Marzinski 	struct list_head *tmp, *next;
84086f1152bSBenjamin Marzinski 
84186f1152bSBenjamin Marzinski 	list_for_each_safe(tmp, next, devices) {
84286f1152bSBenjamin Marzinski 		struct table_device *td = list_entry(tmp, struct table_device, list);
84386f1152bSBenjamin Marzinski 
84486f1152bSBenjamin Marzinski 		DMWARN("dm_destroy: %s still exists with %d references",
845b0b4d7c6SElena Reshetova 		       td->dm_dev.name, refcount_read(&td->count));
84686f1152bSBenjamin Marzinski 		kfree(td);
84786f1152bSBenjamin Marzinski 	}
84886f1152bSBenjamin Marzinski }
84986f1152bSBenjamin Marzinski 
85086f1152bSBenjamin Marzinski /*
8513ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
8523ac51e74SDarrick J. Wong  */
8533ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
8543ac51e74SDarrick J. Wong {
8553ac51e74SDarrick J. Wong 	*geo = md->geometry;
8563ac51e74SDarrick J. Wong 
8573ac51e74SDarrick J. Wong 	return 0;
8583ac51e74SDarrick J. Wong }
8593ac51e74SDarrick J. Wong 
8603ac51e74SDarrick J. Wong /*
8613ac51e74SDarrick J. Wong  * Set the geometry of a device.
8623ac51e74SDarrick J. Wong  */
8633ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
8643ac51e74SDarrick J. Wong {
8653ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
8663ac51e74SDarrick J. Wong 
8673ac51e74SDarrick J. Wong 	if (geo->start > sz) {
8683ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
8693ac51e74SDarrick J. Wong 		return -EINVAL;
8703ac51e74SDarrick J. Wong 	}
8713ac51e74SDarrick J. Wong 
8723ac51e74SDarrick J. Wong 	md->geometry = *geo;
8733ac51e74SDarrick J. Wong 
8743ac51e74SDarrick J. Wong 	return 0;
8753ac51e74SDarrick J. Wong }
8763ac51e74SDarrick J. Wong 
8772e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
8782e93ccc1SKiyoshi Ueda {
8792e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
8802e93ccc1SKiyoshi Ueda }
8812e93ccc1SKiyoshi Ueda 
8828b211aacSMing Lei static void dm_requeue_add_io(struct dm_io *io, bool first_stage)
8838b211aacSMing Lei {
8848b211aacSMing Lei 	struct mapped_device *md = io->md;
8858b211aacSMing Lei 
8868b211aacSMing Lei 	if (first_stage) {
8878b211aacSMing Lei 		struct dm_io *next = md->requeue_list;
8888b211aacSMing Lei 
8898b211aacSMing Lei 		md->requeue_list = io;
8908b211aacSMing Lei 		io->next = next;
8918b211aacSMing Lei 	} else {
8928b211aacSMing Lei 		bio_list_add_head(&md->deferred, io->orig_bio);
8938b211aacSMing Lei 	}
8948b211aacSMing Lei }
8958b211aacSMing Lei 
8968b211aacSMing Lei static void dm_kick_requeue(struct mapped_device *md, bool first_stage)
8978b211aacSMing Lei {
8988b211aacSMing Lei 	if (first_stage)
8998b211aacSMing Lei 		queue_work(md->wq, &md->requeue_work);
9008b211aacSMing Lei 	else
9018b211aacSMing Lei 		queue_work(md->wq, &md->work);
9028b211aacSMing Lei }
9038b211aacSMing Lei 
9042e93ccc1SKiyoshi Ueda /*
905444fe04fSMing Lei  * Return true if the dm_io's original bio is requeued.
906444fe04fSMing Lei  * io->status is updated with error if requeue disallowed.
907444fe04fSMing Lei  */
9088b211aacSMing Lei static bool dm_handle_requeue(struct dm_io *io, bool first_stage)
909444fe04fSMing Lei {
9108b211aacSMing Lei 	struct bio *bio = io->orig_bio;
911444fe04fSMing Lei 	bool handle_requeue = (io->status == BLK_STS_DM_REQUEUE);
912444fe04fSMing Lei 	bool handle_polled_eagain = ((io->status == BLK_STS_AGAIN) &&
913444fe04fSMing Lei 				     (bio->bi_opf & REQ_POLLED));
914444fe04fSMing Lei 	struct mapped_device *md = io->md;
915444fe04fSMing Lei 	bool requeued = false;
916444fe04fSMing Lei 
917444fe04fSMing Lei 	if (handle_requeue || handle_polled_eagain) {
918444fe04fSMing Lei 		unsigned long flags;
919444fe04fSMing Lei 
920444fe04fSMing Lei 		if (bio->bi_opf & REQ_POLLED) {
921444fe04fSMing Lei 			/*
922444fe04fSMing Lei 			 * Upper layer won't help us poll split bio
923444fe04fSMing Lei 			 * (io->orig_bio may only reflect a subset of the
924444fe04fSMing Lei 			 * pre-split original) so clear REQ_POLLED.
925444fe04fSMing Lei 			 */
926444fe04fSMing Lei 			bio_clear_polled(bio);
927444fe04fSMing Lei 		}
928444fe04fSMing Lei 
929444fe04fSMing Lei 		/*
930444fe04fSMing Lei 		 * Target requested pushing back the I/O or
931444fe04fSMing Lei 		 * polled IO hit BLK_STS_AGAIN.
9322e93ccc1SKiyoshi Ueda 		 */
933022c2611SMikulas Patocka 		spin_lock_irqsave(&md->deferred_lock, flags);
934444fe04fSMing Lei 		if ((__noflush_suspending(md) &&
935444fe04fSMing Lei 		     !WARN_ON_ONCE(dm_is_zone_write(md, bio))) ||
9368b211aacSMing Lei 		    handle_polled_eagain || first_stage) {
9378b211aacSMing Lei 			dm_requeue_add_io(io, first_stage);
938444fe04fSMing Lei 			requeued = true;
939bf14e2b2SDamien Le Moal 		} else {
940bf14e2b2SDamien Le Moal 			/*
941bf14e2b2SDamien Le Moal 			 * noflush suspend was interrupted or this is
942bf14e2b2SDamien Le Moal 			 * a write to a zoned target.
943bf14e2b2SDamien Le Moal 			 */
9444e4cbee9SChristoph Hellwig 			io->status = BLK_STS_IOERR;
945bf14e2b2SDamien Le Moal 		}
946022c2611SMikulas Patocka 		spin_unlock_irqrestore(&md->deferred_lock, flags);
9472e93ccc1SKiyoshi Ueda 	}
9482e93ccc1SKiyoshi Ueda 
949444fe04fSMing Lei 	if (requeued)
9508b211aacSMing Lei 		dm_kick_requeue(md, first_stage);
951444fe04fSMing Lei 
952444fe04fSMing Lei 	return requeued;
953444fe04fSMing Lei }
954444fe04fSMing Lei 
9558b211aacSMing Lei static void __dm_io_complete(struct dm_io *io, bool first_stage)
956444fe04fSMing Lei {
9578b211aacSMing Lei 	struct bio *bio = io->orig_bio;
958444fe04fSMing Lei 	struct mapped_device *md = io->md;
959444fe04fSMing Lei 	blk_status_t io_error;
960444fe04fSMing Lei 	bool requeued;
961444fe04fSMing Lei 
9628b211aacSMing Lei 	requeued = dm_handle_requeue(io, first_stage);
9638b211aacSMing Lei 	if (requeued && first_stage)
9648b211aacSMing Lei 		return;
965444fe04fSMing Lei 
9664e4cbee9SChristoph Hellwig 	io_error = io->status;
96782f6cdccSMike Snitzer 	if (dm_io_flagged(io, DM_IO_ACCOUNTED))
968b992b40dSMing Lei 		dm_end_io_acct(io);
9690fbb4d93SMike Snitzer 	else if (!io_error) {
9700fbb4d93SMike Snitzer 		/*
9710fbb4d93SMike Snitzer 		 * Must handle target that DM_MAPIO_SUBMITTED only to
9720fbb4d93SMike Snitzer 		 * then bio_endio() rather than dm_submit_bio_remap()
9730fbb4d93SMike Snitzer 		 */
974b992b40dSMing Lei 		__dm_start_io_acct(io);
975b992b40dSMing Lei 		dm_end_io_acct(io);
9760fbb4d93SMike Snitzer 	}
9770119ab14SMike Snitzer 	free_io(io);
9789f6dc633SMike Snitzer 	smp_wmb();
9799f6dc633SMike Snitzer 	this_cpu_dec(*md->pending_io);
9802056a782SJens Axboe 
9819f6dc633SMike Snitzer 	/* nudge anyone waiting on suspend queue */
9829f6dc633SMike Snitzer 	if (unlikely(wq_has_sleeper(&md->wait)))
9839f6dc633SMike Snitzer 		wake_up(&md->wait);
9841da177e4SLinus Torvalds 
985444fe04fSMing Lei 	/* Return early if the original bio was requeued */
986444fe04fSMing Lei 	if (requeued)
98778ccef91SMike Snitzer 		return;
9886a8736d1STejun Heo 
9898d394bc4SMike Snitzer 	if (bio_is_flush_with_data(bio)) {
990af7e466aSMikulas Patocka 		/*
9916a8736d1STejun Heo 		 * Preflush done for flush with data, reissue
99228a8f0d3SMike Christie 		 * without REQ_PREFLUSH.
993af7e466aSMikulas Patocka 		 */
9941eff9d32SJens Axboe 		bio->bi_opf &= ~REQ_PREFLUSH;
9956a8736d1STejun Heo 		queue_io(md, bio);
996af7e466aSMikulas Patocka 	} else {
997b372d360SMike Snitzer 		/* done with normal IO or empty flush */
9988dd601faSNeilBrown 		if (io_error)
9994e4cbee9SChristoph Hellwig 			bio->bi_status = io_error;
10004246a0b6SChristoph Hellwig 		bio_endio(bio);
10012e93ccc1SKiyoshi Ueda 	}
10021da177e4SLinus Torvalds }
1003e2736347SMike Snitzer 
10048b211aacSMing Lei static void dm_wq_requeue_work(struct work_struct *work)
10058b211aacSMing Lei {
10068b211aacSMing Lei 	struct mapped_device *md = container_of(work, struct mapped_device,
10078b211aacSMing Lei 						requeue_work);
10088b211aacSMing Lei 	unsigned long flags;
10098b211aacSMing Lei 	struct dm_io *io;
10108b211aacSMing Lei 
10118b211aacSMing Lei 	/* reuse deferred lock to simplify dm_handle_requeue */
10128b211aacSMing Lei 	spin_lock_irqsave(&md->deferred_lock, flags);
10138b211aacSMing Lei 	io = md->requeue_list;
10148b211aacSMing Lei 	md->requeue_list = NULL;
10158b211aacSMing Lei 	spin_unlock_irqrestore(&md->deferred_lock, flags);
10168b211aacSMing Lei 
10178b211aacSMing Lei 	while (io) {
10188b211aacSMing Lei 		struct dm_io *next = io->next;
10198b211aacSMing Lei 
10208b211aacSMing Lei 		dm_io_rewind(io, &md->queue->bio_split);
10218b211aacSMing Lei 
10228b211aacSMing Lei 		io->next = NULL;
10238b211aacSMing Lei 		__dm_io_complete(io, false);
10248b211aacSMing Lei 		io = next;
10258b211aacSMing Lei 	}
10268b211aacSMing Lei }
10278b211aacSMing Lei 
10288b211aacSMing Lei /*
10298b211aacSMing Lei  * Two staged requeue:
10308b211aacSMing Lei  *
10318b211aacSMing Lei  * 1) io->orig_bio points to the real original bio, and the part mapped to
10328b211aacSMing Lei  *    this io must be requeued, instead of other parts of the original bio.
10338b211aacSMing Lei  *
10348b211aacSMing Lei  * 2) io->orig_bio points to new cloned bio which matches the requeued dm_io.
10358b211aacSMing Lei  */
10368b211aacSMing Lei static void dm_io_complete(struct dm_io *io)
10378b211aacSMing Lei {
10388b211aacSMing Lei 	bool first_requeue;
10398b211aacSMing Lei 
10408b211aacSMing Lei 	/*
10418b211aacSMing Lei 	 * Only dm_io that has been split needs two stage requeue, otherwise
10428b211aacSMing Lei 	 * we may run into long bio clone chain during suspend and OOM could
10438b211aacSMing Lei 	 * be triggered.
10448b211aacSMing Lei 	 *
10458b211aacSMing Lei 	 * Also flush data dm_io won't be marked as DM_IO_WAS_SPLIT, so they
10468b211aacSMing Lei 	 * also aren't handled via the first stage requeue.
10478b211aacSMing Lei 	 */
10488b211aacSMing Lei 	if (dm_io_flagged(io, DM_IO_WAS_SPLIT))
10498b211aacSMing Lei 		first_requeue = true;
10508b211aacSMing Lei 	else
10518b211aacSMing Lei 		first_requeue = false;
10528b211aacSMing Lei 
10538b211aacSMing Lei 	__dm_io_complete(io, first_requeue);
10548b211aacSMing Lei }
10558b211aacSMing Lei 
1056e2736347SMike Snitzer /*
1057e2736347SMike Snitzer  * Decrements the number of outstanding ios that a bio has been
1058e2736347SMike Snitzer  * cloned into, completing the original io if necc.
1059e2736347SMike Snitzer  */
106084b98f4cSMike Snitzer static inline void __dm_io_dec_pending(struct dm_io *io)
1061e2736347SMike Snitzer {
106284b98f4cSMike Snitzer 	if (atomic_dec_and_test(&io->io_count))
106384b98f4cSMike Snitzer 		dm_io_complete(io);
106484b98f4cSMike Snitzer }
106584b98f4cSMike Snitzer 
106684b98f4cSMike Snitzer static void dm_io_set_error(struct dm_io *io, blk_status_t error)
106784b98f4cSMike Snitzer {
1068e2736347SMike Snitzer 	unsigned long flags;
106984b98f4cSMike Snitzer 
107084b98f4cSMike Snitzer 	/* Push-back supersedes any I/O errors */
10714d7bca13SMike Snitzer 	spin_lock_irqsave(&io->lock, flags);
1072e2736347SMike Snitzer 	if (!(io->status == BLK_STS_DM_REQUEUE &&
107384b98f4cSMike Snitzer 	      __noflush_suspending(io->md))) {
1074e2736347SMike Snitzer 		io->status = error;
107584b98f4cSMike Snitzer 	}
10764d7bca13SMike Snitzer 	spin_unlock_irqrestore(&io->lock, flags);
1077e2736347SMike Snitzer }
1078e2736347SMike Snitzer 
10792e803cd9SMing Lei static void dm_io_dec_pending(struct dm_io *io, blk_status_t error)
108084b98f4cSMike Snitzer {
108184b98f4cSMike Snitzer 	if (unlikely(error))
108284b98f4cSMike Snitzer 		dm_io_set_error(io, error);
108384b98f4cSMike Snitzer 
108484b98f4cSMike Snitzer 	__dm_io_dec_pending(io);
1085af7e466aSMikulas Patocka }
10861da177e4SLinus Torvalds 
1087bcb44433SMike Snitzer void disable_discard(struct mapped_device *md)
1088bcb44433SMike Snitzer {
1089bcb44433SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
1090bcb44433SMike Snitzer 
1091bcb44433SMike Snitzer 	/* device doesn't really support DISCARD, disable it */
1092bcb44433SMike Snitzer 	limits->max_discard_sectors = 0;
1093bcb44433SMike Snitzer }
1094bcb44433SMike Snitzer 
1095ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md)
1096ac62d620SChristoph Hellwig {
1097ac62d620SChristoph Hellwig 	struct queue_limits *limits = dm_get_queue_limits(md);
1098ac62d620SChristoph Hellwig 
1099ac62d620SChristoph Hellwig 	/* device doesn't really support WRITE ZEROES, disable it */
1100ac62d620SChristoph Hellwig 	limits->max_write_zeroes_sectors = 0;
1101ac62d620SChristoph Hellwig }
1102ac62d620SChristoph Hellwig 
1103a666e5c0SMikulas Patocka static bool swap_bios_limit(struct dm_target *ti, struct bio *bio)
1104a666e5c0SMikulas Patocka {
1105a666e5c0SMikulas Patocka 	return unlikely((bio->bi_opf & REQ_SWAP) != 0) && unlikely(ti->limit_swap_bios);
1106a666e5c0SMikulas Patocka }
1107a666e5c0SMikulas Patocka 
11084246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio)
11091da177e4SLinus Torvalds {
11104e4cbee9SChristoph Hellwig 	blk_status_t error = bio->bi_status;
11116cbce280SMike Snitzer 	struct dm_target_io *tio = clone_to_tio(bio);
11126cbce280SMike Snitzer 	struct dm_target *ti = tio->ti;
11136cbce280SMike Snitzer 	dm_endio_fn endio = ti->type->end_io;
11146cbce280SMike Snitzer 	struct dm_io *io = tio->io;
11156cbce280SMike Snitzer 	struct mapped_device *md = io->md;
11161da177e4SLinus Torvalds 
11179c37de29SMike Snitzer 	if (unlikely(error == BLK_STS_TARGET)) {
1118bcb44433SMike Snitzer 		if (bio_op(bio) == REQ_OP_DISCARD &&
111970200574SChristoph Hellwig 		    !bdev_max_discard_sectors(bio->bi_bdev))
1120bcb44433SMike Snitzer 			disable_discard(md);
1121bcb44433SMike Snitzer 		else if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
1122dddf3056SMike Snitzer 			 !bdev_write_zeroes_sectors(bio->bi_bdev))
1123ac62d620SChristoph Hellwig 			disable_write_zeroes(md);
1124ac62d620SChristoph Hellwig 	}
11257eee4ae2SMike Snitzer 
1126442761fdSMike Snitzer 	if (static_branch_unlikely(&zoned_enabled) &&
1127dddf3056SMike Snitzer 	    unlikely(blk_queue_is_zoned(bdev_get_queue(bio->bi_bdev))))
1128bb37d772SDamien Le Moal 		dm_zone_endio(io, bio);
1129415c79e1SJohannes Thumshirn 
11301be56909SChristoph Hellwig 	if (endio) {
11316cbce280SMike Snitzer 		int r = endio(ti, bio, &error);
11321be56909SChristoph Hellwig 		switch (r) {
11331be56909SChristoph Hellwig 		case DM_ENDIO_REQUEUE:
1134442761fdSMike Snitzer 			if (static_branch_unlikely(&zoned_enabled)) {
1135bf14e2b2SDamien Le Moal 				/*
1136bf14e2b2SDamien Le Moal 				 * Requeuing writes to a sequential zone of a zoned
1137bf14e2b2SDamien Le Moal 				 * target will break the sequential write pattern:
1138bf14e2b2SDamien Le Moal 				 * fail such IO.
1139bf14e2b2SDamien Le Moal 				 */
1140bf14e2b2SDamien Le Moal 				if (WARN_ON_ONCE(dm_is_zone_write(md, bio)))
1141bf14e2b2SDamien Le Moal 					error = BLK_STS_IOERR;
1142bf14e2b2SDamien Le Moal 				else
11434e4cbee9SChristoph Hellwig 					error = BLK_STS_DM_REQUEUE;
1144442761fdSMike Snitzer 			} else
1145442761fdSMike Snitzer 				error = BLK_STS_DM_REQUEUE;
1146df561f66SGustavo A. R. Silva 			fallthrough;
11471be56909SChristoph Hellwig 		case DM_ENDIO_DONE:
11481be56909SChristoph Hellwig 			break;
11491be56909SChristoph Hellwig 		case DM_ENDIO_INCOMPLETE:
11501be56909SChristoph Hellwig 			/* The target will handle the io */
11511be56909SChristoph Hellwig 			return;
11521be56909SChristoph Hellwig 		default:
11531be56909SChristoph Hellwig 			DMWARN("unimplemented target endio return value: %d", r);
11541be56909SChristoph Hellwig 			BUG();
11551be56909SChristoph Hellwig 		}
11561be56909SChristoph Hellwig 	}
11571be56909SChristoph Hellwig 
1158442761fdSMike Snitzer 	if (static_branch_unlikely(&swap_bios_enabled) &&
1159442761fdSMike Snitzer 	    unlikely(swap_bios_limit(ti, bio)))
1160a666e5c0SMikulas Patocka 		up(&md->swap_bios_semaphore);
1161a666e5c0SMikulas Patocka 
11621d1068ceSChristoph Hellwig 	free_tio(bio);
1163e2118b3cSDamien Le Moal 	dm_io_dec_pending(io, error);
11641da177e4SLinus Torvalds }
11651da177e4SLinus Torvalds 
116678d8e58aSMike Snitzer /*
116756a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
116856a67df7SMike Snitzer  * target boundary.
116956a67df7SMike Snitzer  */
11703720281dSMike Snitzer static inline sector_t max_io_len_target_boundary(struct dm_target *ti,
11713720281dSMike Snitzer 						  sector_t target_offset)
11721da177e4SLinus Torvalds {
117356a67df7SMike Snitzer 	return ti->len - target_offset;
117456a67df7SMike Snitzer }
117556a67df7SMike Snitzer 
11763720281dSMike Snitzer static sector_t max_io_len(struct dm_target *ti, sector_t sector)
117756a67df7SMike Snitzer {
11783720281dSMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
11793720281dSMike Snitzer 	sector_t len = max_io_len_target_boundary(ti, target_offset);
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	/*
11823ee16db3SMike Snitzer 	 * Does the target need to split IO even further?
11833ee16db3SMike Snitzer 	 * - varied (per target) IO splitting is a tenet of DM; this
11843ee16db3SMike Snitzer 	 *   explains why stacked chunk_sectors based splitting via
1185c3949322SChristoph Hellwig 	 *   blk_queue_split() isn't possible here.
11861da177e4SLinus Torvalds 	 */
1187c3949322SChristoph Hellwig 	if (!ti->max_io_len)
11881da177e4SLinus Torvalds 		return len;
1189c3949322SChristoph Hellwig 	return min_t(sector_t, len,
1190c3949322SChristoph Hellwig 		min(queue_max_sectors(ti->table->md->queue),
1191c3949322SChristoph Hellwig 		    blk_chunk_sectors_left(target_offset, ti->max_io_len)));
11921da177e4SLinus Torvalds }
11931da177e4SLinus Torvalds 
1194542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1195542f9038SMike Snitzer {
1196542f9038SMike Snitzer 	if (len > UINT_MAX) {
1197542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1198542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
1199542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
1200542f9038SMike Snitzer 		return -EINVAL;
1201542f9038SMike Snitzer 	}
1202542f9038SMike Snitzer 
120375ae1936SMikulas Patocka 	ti->max_io_len = (uint32_t) len;
1204542f9038SMike Snitzer 
1205542f9038SMike Snitzer 	return 0;
1206542f9038SMike Snitzer }
1207542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1208542f9038SMike Snitzer 
1209f26c5719SDan Williams static struct dm_target *dm_dax_get_live_target(struct mapped_device *md,
1210f26c5719SDan Williams 						sector_t sector, int *srcu_idx)
12113d97c829SMike Snitzer 	__acquires(md->io_barrier)
1212545ed20eSToshi Kani {
1213545ed20eSToshi Kani 	struct dm_table *map;
1214545ed20eSToshi Kani 	struct dm_target *ti;
1215545ed20eSToshi Kani 
1216f26c5719SDan Williams 	map = dm_get_live_table(md, srcu_idx);
1217545ed20eSToshi Kani 	if (!map)
1218f26c5719SDan Williams 		return NULL;
1219545ed20eSToshi Kani 
1220545ed20eSToshi Kani 	ti = dm_table_find_target(map, sector);
1221123d87d5SMikulas Patocka 	if (!ti)
1222f26c5719SDan Williams 		return NULL;
1223f26c5719SDan Williams 
1224f26c5719SDan Williams 	return ti;
1225f26c5719SDan Williams }
1226f26c5719SDan Williams 
1227f26c5719SDan Williams static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
1228e511c4a3SJane Chu 		long nr_pages, enum dax_access_mode mode, void **kaddr,
1229e511c4a3SJane Chu 		pfn_t *pfn)
1230f26c5719SDan Williams {
1231f26c5719SDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
1232f26c5719SDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
1233f26c5719SDan Williams 	struct dm_target *ti;
1234f26c5719SDan Williams 	long len, ret = -EIO;
1235f26c5719SDan Williams 	int srcu_idx;
1236f26c5719SDan Williams 
1237f26c5719SDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1238f26c5719SDan Williams 
1239f26c5719SDan Williams 	if (!ti)
1240545ed20eSToshi Kani 		goto out;
1241f26c5719SDan Williams 	if (!ti->type->direct_access)
1242f26c5719SDan Williams 		goto out;
12433720281dSMike Snitzer 	len = max_io_len(ti, sector) / PAGE_SECTORS;
1244f26c5719SDan Williams 	if (len < 1)
1245f26c5719SDan Williams 		goto out;
1246f26c5719SDan Williams 	nr_pages = min(len, nr_pages);
1247e511c4a3SJane Chu 	ret = ti->type->direct_access(ti, pgoff, nr_pages, mode, kaddr, pfn);
1248817bf402SDan Williams 
1249545ed20eSToshi Kani  out:
1250545ed20eSToshi Kani 	dm_put_live_table(md, srcu_idx);
1251f26c5719SDan Williams 
1252f26c5719SDan Williams 	return ret;
1253545ed20eSToshi Kani }
1254545ed20eSToshi Kani 
1255cdf6cdcdSVivek Goyal static int dm_dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
1256cdf6cdcdSVivek Goyal 				  size_t nr_pages)
1257cdf6cdcdSVivek Goyal {
1258cdf6cdcdSVivek Goyal 	struct mapped_device *md = dax_get_private(dax_dev);
1259cdf6cdcdSVivek Goyal 	sector_t sector = pgoff * PAGE_SECTORS;
1260cdf6cdcdSVivek Goyal 	struct dm_target *ti;
1261cdf6cdcdSVivek Goyal 	int ret = -EIO;
1262cdf6cdcdSVivek Goyal 	int srcu_idx;
1263cdf6cdcdSVivek Goyal 
1264cdf6cdcdSVivek Goyal 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1265cdf6cdcdSVivek Goyal 
1266cdf6cdcdSVivek Goyal 	if (!ti)
1267cdf6cdcdSVivek Goyal 		goto out;
1268cdf6cdcdSVivek Goyal 	if (WARN_ON(!ti->type->dax_zero_page_range)) {
1269cdf6cdcdSVivek Goyal 		/*
1270cdf6cdcdSVivek Goyal 		 * ->zero_page_range() is mandatory dax operation. If we are
1271cdf6cdcdSVivek Goyal 		 *  here, something is wrong.
1272cdf6cdcdSVivek Goyal 		 */
1273cdf6cdcdSVivek Goyal 		goto out;
1274cdf6cdcdSVivek Goyal 	}
1275cdf6cdcdSVivek Goyal 	ret = ti->type->dax_zero_page_range(ti, pgoff, nr_pages);
1276cdf6cdcdSVivek Goyal  out:
1277cdf6cdcdSVivek Goyal 	dm_put_live_table(md, srcu_idx);
1278cdf6cdcdSVivek Goyal 
1279cdf6cdcdSVivek Goyal 	return ret;
1280cdf6cdcdSVivek Goyal }
1281cdf6cdcdSVivek Goyal 
1282047218ecSJane Chu static size_t dm_dax_recovery_write(struct dax_device *dax_dev, pgoff_t pgoff,
1283047218ecSJane Chu 		void *addr, size_t bytes, struct iov_iter *i)
1284047218ecSJane Chu {
1285047218ecSJane Chu 	struct mapped_device *md = dax_get_private(dax_dev);
1286047218ecSJane Chu 	sector_t sector = pgoff * PAGE_SECTORS;
1287047218ecSJane Chu 	struct dm_target *ti;
1288047218ecSJane Chu 	int srcu_idx;
1289047218ecSJane Chu 	long ret = 0;
1290047218ecSJane Chu 
1291047218ecSJane Chu 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1292047218ecSJane Chu 	if (!ti || !ti->type->dax_recovery_write)
1293047218ecSJane Chu 		goto out;
1294047218ecSJane Chu 
1295047218ecSJane Chu 	ret = ti->type->dax_recovery_write(ti, pgoff, addr, bytes, i);
1296047218ecSJane Chu out:
1297047218ecSJane Chu 	dm_put_live_table(md, srcu_idx);
1298047218ecSJane Chu 	return ret;
1299047218ecSJane Chu }
1300047218ecSJane Chu 
13011dd40c3eSMikulas Patocka /*
13021dd40c3eSMikulas Patocka  * A target may call dm_accept_partial_bio only from the map routine.  It is
13036842d264SDamien Le Moal  * allowed for all bio types except REQ_PREFLUSH, REQ_OP_ZONE_* zone management
1304e6fc9f62SMike Snitzer  * operations, REQ_OP_ZONE_APPEND (zone append writes) and any bio serviced by
1305e6fc9f62SMike Snitzer  * __send_duplicate_bios().
13061dd40c3eSMikulas Patocka  *
13071dd40c3eSMikulas Patocka  * dm_accept_partial_bio informs the dm that the target only wants to process
13081dd40c3eSMikulas Patocka  * additional n_sectors sectors of the bio and the rest of the data should be
13091dd40c3eSMikulas Patocka  * sent in a next bio.
13101dd40c3eSMikulas Patocka  *
13111dd40c3eSMikulas Patocka  * A diagram that explains the arithmetics:
13121dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
13131dd40c3eSMikulas Patocka  * |         1          |       2       |   3   |
13141dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
13151dd40c3eSMikulas Patocka  *
13161dd40c3eSMikulas Patocka  * <-------------- *tio->len_ptr --------------->
1317bdb34759SMike Snitzer  *                      <----- bio_sectors ----->
13181dd40c3eSMikulas Patocka  *                      <-- n_sectors -->
13191dd40c3eSMikulas Patocka  *
13201dd40c3eSMikulas Patocka  * Region 1 was already iterated over with bio_advance or similar function.
13211dd40c3eSMikulas Patocka  *	(it may be empty if the target doesn't use bio_advance)
13221dd40c3eSMikulas Patocka  * Region 2 is the remaining bio size that the target wants to process.
13231dd40c3eSMikulas Patocka  *	(it may be empty if region 1 is non-empty, although there is no reason
13241dd40c3eSMikulas Patocka  *	 to make it empty)
13251dd40c3eSMikulas Patocka  * The target requires that region 3 is to be sent in the next bio.
13261dd40c3eSMikulas Patocka  *
13271dd40c3eSMikulas Patocka  * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
13281dd40c3eSMikulas Patocka  * the partially processed part (the sum of regions 1+2) must be the same for all
13291dd40c3eSMikulas Patocka  * copies of the bio.
13301dd40c3eSMikulas Patocka  */
13311dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
13321dd40c3eSMikulas Patocka {
13336c23f0bdSChristoph Hellwig 	struct dm_target_io *tio = clone_to_tio(bio);
13348b211aacSMing Lei 	struct dm_io *io = tio->io;
1335bdb34759SMike Snitzer 	unsigned bio_sectors = bio_sectors(bio);
13366842d264SDamien Le Moal 
1337655f3aadSMike Snitzer 	BUG_ON(dm_tio_flagged(tio, DM_TIO_IS_DUPLICATE_BIO));
13386842d264SDamien Le Moal 	BUG_ON(op_is_zone_mgmt(bio_op(bio)));
13396842d264SDamien Le Moal 	BUG_ON(bio_op(bio) == REQ_OP_ZONE_APPEND);
1340bdb34759SMike Snitzer 	BUG_ON(bio_sectors > *tio->len_ptr);
1341bdb34759SMike Snitzer 	BUG_ON(n_sectors > bio_sectors);
13426842d264SDamien Le Moal 
1343bdb34759SMike Snitzer 	*tio->len_ptr -= bio_sectors - n_sectors;
13441dd40c3eSMikulas Patocka 	bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
13457dd76d1fSMing Lei 
13467dd76d1fSMing Lei 	/*
13477dd76d1fSMing Lei 	 * __split_and_process_bio() may have already saved mapped part
13487dd76d1fSMing Lei 	 * for accounting but it is being reduced so update accordingly.
13497dd76d1fSMing Lei 	 */
13508b211aacSMing Lei 	dm_io_set_flag(io, DM_IO_WAS_SPLIT);
13518b211aacSMing Lei 	io->sectors = n_sectors;
13528b211aacSMing Lei 	io->sector_offset = bio_sectors(io->orig_bio);
13531dd40c3eSMikulas Patocka }
13541dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
13551dd40c3eSMikulas Patocka 
13560fbb4d93SMike Snitzer /*
13570fbb4d93SMike Snitzer  * @clone: clone bio that DM core passed to target's .map function
13580fbb4d93SMike Snitzer  * @tgt_clone: clone of @clone bio that target needs submitted
13590fbb4d93SMike Snitzer  *
13600fbb4d93SMike Snitzer  * Targets should use this interface to submit bios they take
13610fbb4d93SMike Snitzer  * ownership of when returning DM_MAPIO_SUBMITTED.
13620fbb4d93SMike Snitzer  *
13630fbb4d93SMike Snitzer  * Target should also enable ti->accounts_remapped_io
13640fbb4d93SMike Snitzer  */
1365b7f8dff0SMike Snitzer void dm_submit_bio_remap(struct bio *clone, struct bio *tgt_clone)
13660fbb4d93SMike Snitzer {
13670fbb4d93SMike Snitzer 	struct dm_target_io *tio = clone_to_tio(clone);
13680fbb4d93SMike Snitzer 	struct dm_io *io = tio->io;
13690fbb4d93SMike Snitzer 
13700fbb4d93SMike Snitzer 	/* establish bio that will get submitted */
13710fbb4d93SMike Snitzer 	if (!tgt_clone)
13720fbb4d93SMike Snitzer 		tgt_clone = clone;
13730fbb4d93SMike Snitzer 
13740fbb4d93SMike Snitzer 	/*
13750fbb4d93SMike Snitzer 	 * Account io->origin_bio to DM dev on behalf of target
13760fbb4d93SMike Snitzer 	 * that took ownership of IO with DM_MAPIO_SUBMITTED.
13770fbb4d93SMike Snitzer 	 */
13780fbb4d93SMike Snitzer 	dm_start_io_acct(io, clone);
13790fbb4d93SMike Snitzer 
13809d20653fSMike Snitzer 	trace_block_bio_remap(tgt_clone, disk_devt(io->md->disk),
13810fbb4d93SMike Snitzer 			      tio->old_sector);
13829d20653fSMike Snitzer 	submit_bio_noacct(tgt_clone);
13830fbb4d93SMike Snitzer }
13840fbb4d93SMike Snitzer EXPORT_SYMBOL_GPL(dm_submit_bio_remap);
13850fbb4d93SMike Snitzer 
1386a666e5c0SMikulas Patocka static noinline void __set_swap_bios_limit(struct mapped_device *md, int latch)
1387a666e5c0SMikulas Patocka {
1388a666e5c0SMikulas Patocka 	mutex_lock(&md->swap_bios_lock);
1389a666e5c0SMikulas Patocka 	while (latch < md->swap_bios) {
1390a666e5c0SMikulas Patocka 		cond_resched();
1391a666e5c0SMikulas Patocka 		down(&md->swap_bios_semaphore);
1392a666e5c0SMikulas Patocka 		md->swap_bios--;
1393a666e5c0SMikulas Patocka 	}
1394a666e5c0SMikulas Patocka 	while (latch > md->swap_bios) {
1395a666e5c0SMikulas Patocka 		cond_resched();
1396a666e5c0SMikulas Patocka 		up(&md->swap_bios_semaphore);
1397a666e5c0SMikulas Patocka 		md->swap_bios++;
1398a666e5c0SMikulas Patocka 	}
1399a666e5c0SMikulas Patocka 	mutex_unlock(&md->swap_bios_lock);
1400a666e5c0SMikulas Patocka }
1401a666e5c0SMikulas Patocka 
14021561b396SChristoph Hellwig static void __map_bio(struct bio *clone)
14031da177e4SLinus Torvalds {
14041561b396SChristoph Hellwig 	struct dm_target_io *tio = clone_to_tio(clone);
1405bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
14066cbce280SMike Snitzer 	struct dm_io *io = tio->io;
14076cbce280SMike Snitzer 	struct mapped_device *md = io->md;
14086cbce280SMike Snitzer 	int r;
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds 	/*
14130fbb4d93SMike Snitzer 	 * Map the clone.
14141da177e4SLinus Torvalds 	 */
1415743598f0SMike Snitzer 	tio->old_sector = clone->bi_iter.bi_sector;
1416d67a5f4bSMikulas Patocka 
1417442761fdSMike Snitzer 	if (static_branch_unlikely(&swap_bios_enabled) &&
1418442761fdSMike Snitzer 	    unlikely(swap_bios_limit(ti, clone))) {
1419a666e5c0SMikulas Patocka 		int latch = get_swap_bios();
1420a666e5c0SMikulas Patocka 		if (unlikely(latch != md->swap_bios))
1421a666e5c0SMikulas Patocka 			__set_swap_bios_limit(md, latch);
1422a666e5c0SMikulas Patocka 		down(&md->swap_bios_semaphore);
1423a666e5c0SMikulas Patocka 	}
1424a666e5c0SMikulas Patocka 
1425442761fdSMike Snitzer 	if (static_branch_unlikely(&zoned_enabled)) {
1426bb37d772SDamien Le Moal 		/*
1427442761fdSMike Snitzer 		 * Check if the IO needs a special mapping due to zone append
1428442761fdSMike Snitzer 		 * emulation on zoned target. In this case, dm_zone_map_bio()
1429442761fdSMike Snitzer 		 * calls the target map operation.
1430bb37d772SDamien Le Moal 		 */
14316cbce280SMike Snitzer 		if (unlikely(dm_emulate_zone_append(md)))
1432bb37d772SDamien Le Moal 			r = dm_zone_map_bio(tio);
1433bb37d772SDamien Le Moal 		else
14347de3ee57SMikulas Patocka 			r = ti->type->map(ti, clone);
1435442761fdSMike Snitzer 	} else
1436442761fdSMike Snitzer 		r = ti->type->map(ti, clone);
1437bb37d772SDamien Le Moal 
1438846785e6SChristoph Hellwig 	switch (r) {
1439846785e6SChristoph Hellwig 	case DM_MAPIO_SUBMITTED:
14400fbb4d93SMike Snitzer 		/* target has assumed ownership of this io */
14410fbb4d93SMike Snitzer 		if (!ti->accounts_remapped_io)
14429d20653fSMike Snitzer 			dm_start_io_acct(io, clone);
1443846785e6SChristoph Hellwig 		break;
1444846785e6SChristoph Hellwig 	case DM_MAPIO_REMAPPED:
14459d20653fSMike Snitzer 		dm_submit_bio_remap(clone, NULL);
1446846785e6SChristoph Hellwig 		break;
1447846785e6SChristoph Hellwig 	case DM_MAPIO_KILL:
1448846785e6SChristoph Hellwig 	case DM_MAPIO_REQUEUE:
1449442761fdSMike Snitzer 		if (static_branch_unlikely(&swap_bios_enabled) &&
1450442761fdSMike Snitzer 		    unlikely(swap_bios_limit(ti, clone)))
14516cbce280SMike Snitzer 			up(&md->swap_bios_semaphore);
14521d1068ceSChristoph Hellwig 		free_tio(clone);
145390a2326eSMike Snitzer 		if (r == DM_MAPIO_KILL)
145490a2326eSMike Snitzer 			dm_io_dec_pending(io, BLK_STS_IOERR);
145590a2326eSMike Snitzer 		else
1456e2118b3cSDamien Le Moal 			dm_io_dec_pending(io, BLK_STS_DM_REQUEUE);
1457846785e6SChristoph Hellwig 		break;
1458846785e6SChristoph Hellwig 	default:
145945cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
146045cbcd79SKiyoshi Ueda 		BUG();
14611da177e4SLinus Torvalds 	}
14621da177e4SLinus Torvalds }
14631da177e4SLinus Torvalds 
14647dd76d1fSMing Lei static void setup_split_accounting(struct clone_info *ci, unsigned len)
14657dd76d1fSMing Lei {
14667dd76d1fSMing Lei 	struct dm_io *io = ci->io;
14677dd76d1fSMing Lei 
14687dd76d1fSMing Lei 	if (ci->sector_count > len) {
14697dd76d1fSMing Lei 		/*
14707dd76d1fSMing Lei 		 * Split needed, save the mapped part for accounting.
14717dd76d1fSMing Lei 		 * NOTE: dm_accept_partial_bio() will update accordingly.
14727dd76d1fSMing Lei 		 */
14737dd76d1fSMing Lei 		dm_io_set_flag(io, DM_IO_WAS_SPLIT);
14747dd76d1fSMing Lei 		io->sectors = len;
14758b211aacSMing Lei 		io->sector_offset = bio_sectors(ci->bio);
14767dd76d1fSMing Lei 	}
14777dd76d1fSMing Lei }
14787dd76d1fSMing Lei 
1479318716ddSMike Snitzer static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci,
14807dd06a25SMike Snitzer 				struct dm_target *ti, unsigned num_bios)
1481f9ab94ceSMikulas Patocka {
14821d1068ceSChristoph Hellwig 	struct bio *bio;
1483318716ddSMike Snitzer 	int try;
1484dba14160SMikulas Patocka 
1485318716ddSMike Snitzer 	for (try = 0; try < 2; try++) {
1486318716ddSMike Snitzer 		int bio_nr;
1487318716ddSMike Snitzer 
1488318716ddSMike Snitzer 		if (try)
1489bc02cdbeSMike Snitzer 			mutex_lock(&ci->io->md->table_devices_lock);
1490318716ddSMike Snitzer 		for (bio_nr = 0; bio_nr < num_bios; bio_nr++) {
14917dd06a25SMike Snitzer 			bio = alloc_tio(ci, ti, bio_nr, NULL,
1492dc8e2021SChristoph Hellwig 					try ? GFP_NOIO : GFP_NOWAIT);
14931d1068ceSChristoph Hellwig 			if (!bio)
1494318716ddSMike Snitzer 				break;
1495318716ddSMike Snitzer 
14961d1068ceSChristoph Hellwig 			bio_list_add(blist, bio);
1497318716ddSMike Snitzer 		}
1498318716ddSMike Snitzer 		if (try)
1499bc02cdbeSMike Snitzer 			mutex_unlock(&ci->io->md->table_devices_lock);
1500318716ddSMike Snitzer 		if (bio_nr == num_bios)
1501318716ddSMike Snitzer 			return;
1502318716ddSMike Snitzer 
15036c23f0bdSChristoph Hellwig 		while ((bio = bio_list_pop(blist)))
15041d1068ceSChristoph Hellwig 			free_tio(bio);
1505318716ddSMike Snitzer 	}
1506318716ddSMike Snitzer }
1507f9ab94ceSMikulas Patocka 
15080f14d60aSMing Lei static int __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
15091dd40c3eSMikulas Patocka 				  unsigned num_bios, unsigned *len)
151006a426ceSMike Snitzer {
1511318716ddSMike Snitzer 	struct bio_list blist = BIO_EMPTY_LIST;
15128eabf5d0SChristoph Hellwig 	struct bio *clone;
15130f14d60aSMing Lei 	int ret = 0;
151406a426ceSMike Snitzer 
1515891fced6SChristoph Hellwig 	switch (num_bios) {
1516891fced6SChristoph Hellwig 	case 0:
1517891fced6SChristoph Hellwig 		break;
1518891fced6SChristoph Hellwig 	case 1:
15197dd76d1fSMing Lei 		if (len)
15207dd76d1fSMing Lei 			setup_split_accounting(ci, *len);
1521891fced6SChristoph Hellwig 		clone = alloc_tio(ci, ti, 0, len, GFP_NOIO);
1522891fced6SChristoph Hellwig 		__map_bio(clone);
15230f14d60aSMing Lei 		ret = 1;
1524891fced6SChristoph Hellwig 		break;
1525891fced6SChristoph Hellwig 	default:
15267dd06a25SMike Snitzer 		/* dm_accept_partial_bio() is not supported with shared tio->len_ptr */
15277dd06a25SMike Snitzer 		alloc_multiple_bios(&blist, ci, ti, num_bios);
15288eabf5d0SChristoph Hellwig 		while ((clone = bio_list_pop(&blist))) {
1529655f3aadSMike Snitzer 			dm_tio_set_flag(clone_to_tio(clone), DM_TIO_IS_DUPLICATE_BIO);
15301561b396SChristoph Hellwig 			__map_bio(clone);
15310f14d60aSMing Lei 			ret += 1;
1532f9ab94ceSMikulas Patocka 		}
1533891fced6SChristoph Hellwig 		break;
1534318716ddSMike Snitzer 	}
15350f14d60aSMing Lei 
15360f14d60aSMing Lei 	return ret;
153706a426ceSMike Snitzer }
153806a426ceSMike Snitzer 
1539332f2b1eSMike Snitzer static void __send_empty_flush(struct clone_info *ci)
1540f9ab94ceSMikulas Patocka {
154106a426ceSMike Snitzer 	unsigned target_nr = 0;
1542f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1543828678b8SMike Snitzer 	struct bio flush_bio;
1544828678b8SMike Snitzer 
1545828678b8SMike Snitzer 	/*
1546828678b8SMike Snitzer 	 * Use an on-stack bio for this, it's safe since we don't
1547828678b8SMike Snitzer 	 * need to reference it after submit. It's just used as
1548828678b8SMike Snitzer 	 * the basis for the clone(s).
1549828678b8SMike Snitzer 	 */
155049add496SChristoph Hellwig 	bio_init(&flush_bio, ci->io->md->disk->part0, NULL, 0,
155149add496SChristoph Hellwig 		 REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC);
155247d95102SChristoph Hellwig 
1553828678b8SMike Snitzer 	ci->bio = &flush_bio;
1554828678b8SMike Snitzer 	ci->sector_count = 0;
155592b914e2SShin'ichiro Kawasaki 	ci->io->tio.clone.bi_iter.bi_size = 0;
1556f9ab94ceSMikulas Patocka 
15570f14d60aSMing Lei 	while ((ti = dm_table_get_target(ci->map, target_nr++))) {
15580f14d60aSMing Lei 		int bios;
15590f14d60aSMing Lei 
15600f14d60aSMing Lei 		atomic_add(ti->num_flush_bios, &ci->io->io_count);
15610f14d60aSMing Lei 		bios = __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
15620f14d60aSMing Lei 		atomic_sub(ti->num_flush_bios - bios, &ci->io->io_count);
15630f14d60aSMing Lei 	}
15640f14d60aSMing Lei 
15650f14d60aSMing Lei 	/*
15660f14d60aSMing Lei 	 * alloc_io() takes one extra reference for submission, so the
15670f14d60aSMing Lei 	 * reference won't reach 0 without the following subtraction
15680f14d60aSMing Lei 	 */
15690f14d60aSMing Lei 	atomic_sub(1, &ci->io->io_count);
1570828678b8SMike Snitzer 
1571828678b8SMike Snitzer 	bio_uninit(ci->bio);
1572f9ab94ceSMikulas Patocka }
1573f9ab94ceSMikulas Patocka 
1574e6fc9f62SMike Snitzer static void __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti,
157561697a6aSMike Snitzer 					unsigned num_bios)
15765ae89a87SMike Snitzer {
157751b86f9aSMichael Lass 	unsigned len;
15780f14d60aSMing Lei 	int bios;
15795ae89a87SMike Snitzer 
15803720281dSMike Snitzer 	len = min_t(sector_t, ci->sector_count,
15813720281dSMike Snitzer 		    max_io_len_target_boundary(ti, dm_target_offset(ti, ci->sector)));
158251b86f9aSMichael Lass 
15830f14d60aSMing Lei 	atomic_add(num_bios, &ci->io->io_count);
15840f14d60aSMing Lei 	bios = __send_duplicate_bios(ci, ti, num_bios, &len);
15850f14d60aSMing Lei 	/*
15860f14d60aSMing Lei 	 * alloc_io() takes one extra reference for submission, so the
15870f14d60aSMing Lei 	 * reference won't reach 0 without the following (+1) subtraction
15880f14d60aSMing Lei 	 */
15890f14d60aSMing Lei 	atomic_sub(num_bios - bios + 1, &ci->io->io_count);
15907dd06a25SMike Snitzer 
1591a79245b3SMike Snitzer 	ci->sector += len;
15923d7f4562SMike Snitzer 	ci->sector_count -= len;
15935ae89a87SMike Snitzer }
15945ae89a87SMike Snitzer 
1595568c73a3SMike Snitzer static bool is_abnormal_io(struct bio *bio)
1596568c73a3SMike Snitzer {
15974edadf6dSMike Snitzer 	unsigned int op = bio_op(bio);
1598568c73a3SMike Snitzer 
15994edadf6dSMike Snitzer 	if (op != REQ_OP_READ && op != REQ_OP_WRITE && op != REQ_OP_FLUSH) {
16004edadf6dSMike Snitzer 		switch (op) {
1601568c73a3SMike Snitzer 		case REQ_OP_DISCARD:
1602568c73a3SMike Snitzer 		case REQ_OP_SECURE_ERASE:
1603568c73a3SMike Snitzer 		case REQ_OP_WRITE_ZEROES:
16044edadf6dSMike Snitzer 			return true;
16054edadf6dSMike Snitzer 		default:
1606568c73a3SMike Snitzer 			break;
1607568c73a3SMike Snitzer 		}
1608568c73a3SMike Snitzer 	}
1609568c73a3SMike Snitzer 
16104edadf6dSMike Snitzer 	return false;
16114edadf6dSMike Snitzer }
16124edadf6dSMike Snitzer 
16134edadf6dSMike Snitzer static blk_status_t __process_abnormal_io(struct clone_info *ci,
16144edadf6dSMike Snitzer 					  struct dm_target *ti)
16150519c71eSMike Snitzer {
16169679b5a7SMike Snitzer 	unsigned num_bios = 0;
16170519c71eSMike Snitzer 
1618e6fc9f62SMike Snitzer 	switch (bio_op(ci->bio)) {
16199679b5a7SMike Snitzer 	case REQ_OP_DISCARD:
16209679b5a7SMike Snitzer 		num_bios = ti->num_discard_bios;
16219679b5a7SMike Snitzer 		break;
16229679b5a7SMike Snitzer 	case REQ_OP_SECURE_ERASE:
16239679b5a7SMike Snitzer 		num_bios = ti->num_secure_erase_bios;
16249679b5a7SMike Snitzer 		break;
16259679b5a7SMike Snitzer 	case REQ_OP_WRITE_ZEROES:
16269679b5a7SMike Snitzer 		num_bios = ti->num_write_zeroes_bios;
16279679b5a7SMike Snitzer 		break;
16289679b5a7SMike Snitzer 	}
16290519c71eSMike Snitzer 
1630e6fc9f62SMike Snitzer 	/*
1631e6fc9f62SMike Snitzer 	 * Even though the device advertised support for this type of
1632e6fc9f62SMike Snitzer 	 * request, that does not mean every target supports it, and
1633e6fc9f62SMike Snitzer 	 * reconfiguration might also have changed that since the
1634e6fc9f62SMike Snitzer 	 * check was performed.
1635e6fc9f62SMike Snitzer 	 */
163684b98f4cSMike Snitzer 	if (unlikely(!num_bios))
16374edadf6dSMike Snitzer 		return BLK_STS_NOTSUPP;
16384edadf6dSMike Snitzer 
1639e6fc9f62SMike Snitzer 	__send_changing_extent_only(ci, ti, num_bios);
16404edadf6dSMike Snitzer 	return BLK_STS_OK;
16410519c71eSMike Snitzer }
16420519c71eSMike Snitzer 
1643e4c93811SAlasdair G Kergon /*
1644ec211631SMing Lei  * Reuse ->bi_private as dm_io list head for storing all dm_io instances
1645b99fdcdcSMing Lei  * associated with this bio, and this bio's bi_private needs to be
1646b99fdcdcSMing Lei  * stored in dm_io->data before the reuse.
1647b99fdcdcSMing Lei  *
1648b99fdcdcSMing Lei  * bio->bi_private is owned by fs or upper layer, so block layer won't
1649b99fdcdcSMing Lei  * touch it after splitting. Meantime it won't be changed by anyone after
1650b99fdcdcSMing Lei  * bio is submitted. So this reuse is safe.
1651b99fdcdcSMing Lei  */
1652ec211631SMing Lei static inline struct dm_io **dm_poll_list_head(struct bio *bio)
1653b99fdcdcSMing Lei {
1654ec211631SMing Lei 	return (struct dm_io **)&bio->bi_private;
1655b99fdcdcSMing Lei }
1656b99fdcdcSMing Lei 
1657b99fdcdcSMing Lei static void dm_queue_poll_io(struct bio *bio, struct dm_io *io)
1658b99fdcdcSMing Lei {
1659ec211631SMing Lei 	struct dm_io **head = dm_poll_list_head(bio);
1660b99fdcdcSMing Lei 
1661b99fdcdcSMing Lei 	if (!(bio->bi_opf & REQ_DM_POLL_LIST)) {
1662b99fdcdcSMing Lei 		bio->bi_opf |= REQ_DM_POLL_LIST;
1663b99fdcdcSMing Lei 		/*
1664b99fdcdcSMing Lei 		 * Save .bi_private into dm_io, so that we can reuse
1665ec211631SMing Lei 		 * .bi_private as dm_io list head for storing dm_io list
1666b99fdcdcSMing Lei 		 */
1667b99fdcdcSMing Lei 		io->data = bio->bi_private;
1668b99fdcdcSMing Lei 
1669b99fdcdcSMing Lei 		/* tell block layer to poll for completion */
1670b99fdcdcSMing Lei 		bio->bi_cookie = ~BLK_QC_T_NONE;
1671ec211631SMing Lei 
1672ec211631SMing Lei 		io->next = NULL;
1673b99fdcdcSMing Lei 	} else {
1674b99fdcdcSMing Lei 		/*
1675b99fdcdcSMing Lei 		 * bio recursed due to split, reuse original poll list,
1676b99fdcdcSMing Lei 		 * and save bio->bi_private too.
1677b99fdcdcSMing Lei 		 */
1678ec211631SMing Lei 		io->data = (*head)->data;
1679ec211631SMing Lei 		io->next = *head;
1680b99fdcdcSMing Lei 	}
1681b99fdcdcSMing Lei 
1682ec211631SMing Lei 	*head = io;
1683b99fdcdcSMing Lei }
1684b99fdcdcSMing Lei 
1685b99fdcdcSMing Lei /*
1686e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1687e4c93811SAlasdair G Kergon  */
168884b98f4cSMike Snitzer static blk_status_t __split_and_process_bio(struct clone_info *ci)
1689e4c93811SAlasdair G Kergon {
169066bdaa43SMike Snitzer 	struct bio *clone;
1691e4c93811SAlasdair G Kergon 	struct dm_target *ti;
16921c3b13e6SKent Overstreet 	unsigned len;
1693e4c93811SAlasdair G Kergon 
1694e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
16954edadf6dSMike Snitzer 	if (unlikely(!ti))
16964edadf6dSMike Snitzer 		return BLK_STS_IOERR;
16971ee88de3SMikulas Patocka 
16981ee88de3SMikulas Patocka 	if (unlikely((ci->bio->bi_opf & REQ_NOWAIT) != 0) &&
16991ee88de3SMikulas Patocka 	    unlikely(!dm_target_supports_nowait(ti->type)))
17001ee88de3SMikulas Patocka 		return BLK_STS_NOTSUPP;
17011ee88de3SMikulas Patocka 
17021ee88de3SMikulas Patocka 	if (unlikely(ci->is_abnormal_io))
17034edadf6dSMike Snitzer 		return __process_abnormal_io(ci, ti);
17043d7f4562SMike Snitzer 
1705b99fdcdcSMing Lei 	/*
1706b99fdcdcSMing Lei 	 * Only support bio polling for normal IO, and the target io is
1707b99fdcdcSMing Lei 	 * exactly inside the dm_io instance (verified in dm_poll_dm_io)
1708b99fdcdcSMing Lei 	 */
1709b99fdcdcSMing Lei 	ci->submit_as_polled = ci->bio->bi_opf & REQ_POLLED;
1710e4c93811SAlasdair G Kergon 
1711e4c93811SAlasdair G Kergon 	len = min_t(sector_t, max_io_len(ti, ci->sector), ci->sector_count);
17127dd76d1fSMing Lei 	setup_split_accounting(ci, len);
171366bdaa43SMike Snitzer 	clone = alloc_tio(ci, ti, 0, &len, GFP_NOIO);
171466bdaa43SMike Snitzer 	__map_bio(clone);
1715e4c93811SAlasdair G Kergon 
1716e4c93811SAlasdair G Kergon 	ci->sector += len;
1717e4c93811SAlasdair G Kergon 	ci->sector_count -= len;
1718e4c93811SAlasdair G Kergon 
171984b98f4cSMike Snitzer 	return BLK_STS_OK;
1720e4c93811SAlasdair G Kergon }
1721e4c93811SAlasdair G Kergon 
1722978e51baSMike Snitzer static void init_clone_info(struct clone_info *ci, struct mapped_device *md,
17234edadf6dSMike Snitzer 			    struct dm_table *map, struct bio *bio, bool is_abnormal)
1724978e51baSMike Snitzer {
1725978e51baSMike Snitzer 	ci->map = map;
1726978e51baSMike Snitzer 	ci->io = alloc_io(md, bio);
1727d41e077aSMike Snitzer 	ci->bio = bio;
17284edadf6dSMike Snitzer 	ci->is_abnormal_io = is_abnormal;
1729b99fdcdcSMing Lei 	ci->submit_as_polled = false;
1730978e51baSMike Snitzer 	ci->sector = bio->bi_iter.bi_sector;
1731d41e077aSMike Snitzer 	ci->sector_count = bio_sectors(bio);
1732d41e077aSMike Snitzer 
1733d41e077aSMike Snitzer 	/* Shouldn't happen but sector_count was being set to 0 so... */
1734442761fdSMike Snitzer 	if (static_branch_unlikely(&zoned_enabled) &&
1735442761fdSMike Snitzer 	    WARN_ON_ONCE(op_is_zone_mgmt(bio_op(bio)) && ci->sector_count))
1736d41e077aSMike Snitzer 		ci->sector_count = 0;
1737978e51baSMike Snitzer }
1738978e51baSMike Snitzer 
1739e4c93811SAlasdair G Kergon /*
174014fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
17411da177e4SLinus Torvalds  */
174296c9865cSMike Snitzer static void dm_split_and_process_bio(struct mapped_device *md,
174383d5e5b0SMikulas Patocka 				     struct dm_table *map, struct bio *bio)
17441da177e4SLinus Torvalds {
17451da177e4SLinus Torvalds 	struct clone_info ci;
17464857abf6SMike Snitzer 	struct dm_io *io;
174784b98f4cSMike Snitzer 	blk_status_t error = BLK_STS_OK;
17484edadf6dSMike Snitzer 	bool is_abnormal;
17491da177e4SLinus Torvalds 
17504edadf6dSMike Snitzer 	is_abnormal = is_abnormal_io(bio);
17514edadf6dSMike Snitzer 	if (unlikely(is_abnormal)) {
17524edadf6dSMike Snitzer 		/*
17534edadf6dSMike Snitzer 		 * Use blk_queue_split() for abnormal IO (e.g. discard, etc)
17544edadf6dSMike Snitzer 		 * otherwise associated queue_limits won't be imposed.
17554edadf6dSMike Snitzer 		 */
17564edadf6dSMike Snitzer 		blk_queue_split(&bio);
17574edadf6dSMike Snitzer 	}
17584edadf6dSMike Snitzer 
17594edadf6dSMike Snitzer 	init_clone_info(&ci, md, map, bio, is_abnormal);
17604857abf6SMike Snitzer 	io = ci.io;
1761bd2a49b8SAlasdair G Kergon 
17621eff9d32SJens Axboe 	if (bio->bi_opf & REQ_PREFLUSH) {
1763332f2b1eSMike Snitzer 		__send_empty_flush(&ci);
1764e2736347SMike Snitzer 		/* dm_io_complete submits any data associated with flush */
1765d41e077aSMike Snitzer 		goto out;
1766d41e077aSMike Snitzer 	}
1767d41e077aSMike Snitzer 
176896c9865cSMike Snitzer 	error = __split_and_process_bio(&ci);
1769d41e077aSMike Snitzer 	if (error || !ci.sector_count)
1770d41e077aSMike Snitzer 		goto out;
177118a25da8SNeilBrown 	/*
1772d41e077aSMike Snitzer 	 * Remainder must be passed to submit_bio_noacct() so it gets handled
1773d41e077aSMike Snitzer 	 * *after* bios already submitted have been completely processed.
177418a25da8SNeilBrown 	 */
17758b211aacSMing Lei 	bio_trim(bio, io->sectors, ci.sector_count);
17768b211aacSMing Lei 	trace_block_split(bio, bio->bi_iter.bi_sector);
17778b211aacSMing Lei 	bio_inc_remaining(bio);
17783e08773cSChristoph Hellwig 	submit_bio_noacct(bio);
1779d41e077aSMike Snitzer out:
1780b99fdcdcSMing Lei 	/*
1781b99fdcdcSMing Lei 	 * Drop the extra reference count for non-POLLED bio, and hold one
1782b99fdcdcSMing Lei 	 * reference for POLLED bio, which will be released in dm_poll_bio
1783b99fdcdcSMing Lei 	 *
1784ec211631SMing Lei 	 * Add every dm_io instance into the dm_io list head which is stored
1785ec211631SMing Lei 	 * in bio->bi_private, so that dm_poll_bio can poll them all.
1786b99fdcdcSMing Lei 	 */
17870f14d60aSMing Lei 	if (error || !ci.submit_as_polled) {
17880f14d60aSMing Lei 		/*
17890f14d60aSMing Lei 		 * In case of submission failure, the extra reference for
17900f14d60aSMing Lei 		 * submitting io isn't consumed yet
17910f14d60aSMing Lei 		 */
17920f14d60aSMing Lei 		if (error)
17930f14d60aSMing Lei 			atomic_dec(&io->io_count);
17940f14d60aSMing Lei 		dm_io_dec_pending(io, error);
17950f14d60aSMing Lei 	} else
17964857abf6SMike Snitzer 		dm_queue_poll_io(bio, io);
17971da177e4SLinus Torvalds }
17981da177e4SLinus Torvalds 
17993e08773cSChristoph Hellwig static void dm_submit_bio(struct bio *bio)
18001da177e4SLinus Torvalds {
1801309dca30SChristoph Hellwig 	struct mapped_device *md = bio->bi_bdev->bd_disk->private_data;
180283d5e5b0SMikulas Patocka 	int srcu_idx;
180383d5e5b0SMikulas Patocka 	struct dm_table *map;
18045d7362d0SMikulas Patocka 	unsigned bio_opf = bio->bi_opf;
18051da177e4SLinus Torvalds 
18065d7362d0SMikulas Patocka 	map = dm_get_live_table_bio(md, &srcu_idx, bio_opf);
18078cf7961dSChristoph Hellwig 
1808fa247089SMike Snitzer 	/* If suspended, or map not yet available, queue this IO for later */
1809fa247089SMike Snitzer 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) ||
1810fa247089SMike Snitzer 	    unlikely(!map)) {
18116abc4946SKonstantin Khlebnikov 		if (bio->bi_opf & REQ_NOWAIT)
18126abc4946SKonstantin Khlebnikov 			bio_wouldblock_error(bio);
1813b2abdb1bSMike Snitzer 		else if (bio->bi_opf & REQ_RAHEAD)
18146a8736d1STejun Heo 			bio_io_error(bio);
1815b2abdb1bSMike Snitzer 		else
1816b2abdb1bSMike Snitzer 			queue_io(md, bio);
1817b2abdb1bSMike Snitzer 		goto out;
18181da177e4SLinus Torvalds 	}
18191da177e4SLinus Torvalds 
182096c9865cSMike Snitzer 	dm_split_and_process_bio(md, map, bio);
1821b2abdb1bSMike Snitzer out:
18225d7362d0SMikulas Patocka 	dm_put_live_table_bio(md, srcu_idx, bio_opf);
1823978e51baSMike Snitzer }
1824978e51baSMike Snitzer 
1825b99fdcdcSMing Lei static bool dm_poll_dm_io(struct dm_io *io, struct io_comp_batch *iob,
1826b99fdcdcSMing Lei 			  unsigned int flags)
1827b99fdcdcSMing Lei {
1828655f3aadSMike Snitzer 	WARN_ON_ONCE(!dm_tio_is_normal(&io->tio));
1829b99fdcdcSMing Lei 
1830b99fdcdcSMing Lei 	/* don't poll if the mapped io is done */
1831b99fdcdcSMing Lei 	if (atomic_read(&io->io_count) > 1)
1832b99fdcdcSMing Lei 		bio_poll(&io->tio.clone, iob, flags);
1833b99fdcdcSMing Lei 
1834b99fdcdcSMing Lei 	/* bio_poll holds the last reference */
1835b99fdcdcSMing Lei 	return atomic_read(&io->io_count) == 1;
1836b99fdcdcSMing Lei }
1837b99fdcdcSMing Lei 
1838b99fdcdcSMing Lei static int dm_poll_bio(struct bio *bio, struct io_comp_batch *iob,
1839b99fdcdcSMing Lei 		       unsigned int flags)
1840b99fdcdcSMing Lei {
1841ec211631SMing Lei 	struct dm_io **head = dm_poll_list_head(bio);
1842ec211631SMing Lei 	struct dm_io *list = *head;
1843ec211631SMing Lei 	struct dm_io *tmp = NULL;
1844ec211631SMing Lei 	struct dm_io *curr, *next;
1845b99fdcdcSMing Lei 
1846b99fdcdcSMing Lei 	/* Only poll normal bio which was marked as REQ_DM_POLL_LIST */
1847b99fdcdcSMing Lei 	if (!(bio->bi_opf & REQ_DM_POLL_LIST))
1848b99fdcdcSMing Lei 		return 0;
1849b99fdcdcSMing Lei 
1850ec211631SMing Lei 	WARN_ON_ONCE(!list);
1851b99fdcdcSMing Lei 
1852b99fdcdcSMing Lei 	/*
1853b99fdcdcSMing Lei 	 * Restore .bi_private before possibly completing dm_io.
1854b99fdcdcSMing Lei 	 *
1855b99fdcdcSMing Lei 	 * bio_poll() is only possible once @bio has been completely
1856b99fdcdcSMing Lei 	 * submitted via submit_bio_noacct()'s depth-first submission.
1857b99fdcdcSMing Lei 	 * So there is no dm_queue_poll_io() race associated with
1858b99fdcdcSMing Lei 	 * clearing REQ_DM_POLL_LIST here.
1859b99fdcdcSMing Lei 	 */
1860b99fdcdcSMing Lei 	bio->bi_opf &= ~REQ_DM_POLL_LIST;
1861ec211631SMing Lei 	bio->bi_private = list->data;
1862b99fdcdcSMing Lei 
1863ec211631SMing Lei 	for (curr = list, next = curr->next; curr; curr = next, next =
1864ec211631SMing Lei 			curr ? curr->next : NULL) {
1865ec211631SMing Lei 		if (dm_poll_dm_io(curr, iob, flags)) {
1866b99fdcdcSMing Lei 			/*
186784b98f4cSMike Snitzer 			 * clone_endio() has already occurred, so no
186884b98f4cSMike Snitzer 			 * error handling is needed here.
1869b99fdcdcSMing Lei 			 */
1870ec211631SMing Lei 			__dm_io_dec_pending(curr);
1871ec211631SMing Lei 		} else {
1872ec211631SMing Lei 			curr->next = tmp;
1873ec211631SMing Lei 			tmp = curr;
1874b99fdcdcSMing Lei 		}
1875b99fdcdcSMing Lei 	}
1876b99fdcdcSMing Lei 
1877b99fdcdcSMing Lei 	/* Not done? */
1878ec211631SMing Lei 	if (tmp) {
1879b99fdcdcSMing Lei 		bio->bi_opf |= REQ_DM_POLL_LIST;
1880b99fdcdcSMing Lei 		/* Reset bio->bi_private to dm_io list head */
1881ec211631SMing Lei 		*head = tmp;
1882b99fdcdcSMing Lei 		return 0;
1883b99fdcdcSMing Lei 	}
1884b99fdcdcSMing Lei 	return 1;
1885b99fdcdcSMing Lei }
1886b99fdcdcSMing Lei 
18871da177e4SLinus Torvalds /*-----------------------------------------------------------------
18881da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
18891da177e4SLinus Torvalds  *---------------------------------------------------------------*/
18902b06cfffSAlasdair G Kergon static void free_minor(int minor)
18911da177e4SLinus Torvalds {
1892f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
18931da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1894f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
18951da177e4SLinus Torvalds }
18961da177e4SLinus Torvalds 
18971da177e4SLinus Torvalds /*
18981da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
18991da177e4SLinus Torvalds  */
1900cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
19011da177e4SLinus Torvalds {
1902c9d76be6STejun Heo 	int r;
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
19051da177e4SLinus Torvalds 		return -EINVAL;
19061da177e4SLinus Torvalds 
1907c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1908f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
19091da177e4SLinus Torvalds 
1910c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
19111da177e4SLinus Torvalds 
1912f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1913c9d76be6STejun Heo 	idr_preload_end();
1914c9d76be6STejun Heo 	if (r < 0)
1915c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
1916c9d76be6STejun Heo 	return 0;
19171da177e4SLinus Torvalds }
19181da177e4SLinus Torvalds 
1919cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
19201da177e4SLinus Torvalds {
1921c9d76be6STejun Heo 	int r;
19221da177e4SLinus Torvalds 
1923c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1924f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
19251da177e4SLinus Torvalds 
1926c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
19271da177e4SLinus Torvalds 
1928f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1929c9d76be6STejun Heo 	idr_preload_end();
1930c9d76be6STejun Heo 	if (r < 0)
19311da177e4SLinus Torvalds 		return r;
1932c9d76be6STejun Heo 	*minor = r;
1933c9d76be6STejun Heo 	return 0;
19341da177e4SLinus Torvalds }
19351da177e4SLinus Torvalds 
193683d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
1937681cc5e8SMike Snitzer static const struct block_device_operations dm_rq_blk_dops;
1938f26c5719SDan Williams static const struct dax_operations dm_dax_ops;
19391da177e4SLinus Torvalds 
194053d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
194153d5914fSMikulas Patocka 
1942aa6ce87aSSatya Tangirala #ifdef CONFIG_BLK_INLINE_ENCRYPTION
1943cb77cb5aSEric Biggers static void dm_queue_destroy_crypto_profile(struct request_queue *q)
1944aa6ce87aSSatya Tangirala {
1945cb77cb5aSEric Biggers 	dm_destroy_crypto_profile(q->crypto_profile);
1946aa6ce87aSSatya Tangirala }
1947aa6ce87aSSatya Tangirala 
1948aa6ce87aSSatya Tangirala #else /* CONFIG_BLK_INLINE_ENCRYPTION */
1949aa6ce87aSSatya Tangirala 
1950cb77cb5aSEric Biggers static inline void dm_queue_destroy_crypto_profile(struct request_queue *q)
1951aa6ce87aSSatya Tangirala {
1952aa6ce87aSSatya Tangirala }
1953aa6ce87aSSatya Tangirala #endif /* !CONFIG_BLK_INLINE_ENCRYPTION */
1954aa6ce87aSSatya Tangirala 
19550f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md)
19560f20972fSMike Snitzer {
19570f20972fSMike Snitzer 	if (md->wq)
19580f20972fSMike Snitzer 		destroy_workqueue(md->wq);
195929dec90aSChristoph Hellwig 	dm_free_md_mempools(md->mempools);
19600f20972fSMike Snitzer 
1961f26c5719SDan Williams 	if (md->dax_dev) {
1962fb08a190SChristoph Hellwig 		dax_remove_host(md->disk);
1963f26c5719SDan Williams 		kill_dax(md->dax_dev);
1964f26c5719SDan Williams 		put_dax(md->dax_dev);
1965f26c5719SDan Williams 		md->dax_dev = NULL;
1966f26c5719SDan Williams 	}
1967f26c5719SDan Williams 
1968588b7f5dSKirill Tkhai 	dm_cleanup_zoned_dev(md);
19690f20972fSMike Snitzer 	if (md->disk) {
19700f20972fSMike Snitzer 		spin_lock(&_minor_lock);
19710f20972fSMike Snitzer 		md->disk->private_data = NULL;
19720f20972fSMike Snitzer 		spin_unlock(&_minor_lock);
197389f871afSChristoph Hellwig 		if (dm_get_md_type(md) != DM_TYPE_NONE) {
197489f871afSChristoph Hellwig 			dm_sysfs_exit(md);
19750f20972fSMike Snitzer 			del_gendisk(md->disk);
197689f871afSChristoph Hellwig 		}
1977cb77cb5aSEric Biggers 		dm_queue_destroy_crypto_profile(md->queue);
19788b9ab626SChristoph Hellwig 		put_disk(md->disk);
197974a2b6ecSChristoph Hellwig 	}
19800f20972fSMike Snitzer 
19819f6dc633SMike Snitzer 	if (md->pending_io) {
19829f6dc633SMike Snitzer 		free_percpu(md->pending_io);
19839f6dc633SMike Snitzer 		md->pending_io = NULL;
19849f6dc633SMike Snitzer 	}
19859f6dc633SMike Snitzer 
1986d09960b0STahsin Erdogan 	cleanup_srcu_struct(&md->io_barrier);
1987d09960b0STahsin Erdogan 
1988d5ffebddSMike Snitzer 	mutex_destroy(&md->suspend_lock);
1989d5ffebddSMike Snitzer 	mutex_destroy(&md->type_lock);
1990d5ffebddSMike Snitzer 	mutex_destroy(&md->table_devices_lock);
1991a666e5c0SMikulas Patocka 	mutex_destroy(&md->swap_bios_lock);
1992d5ffebddSMike Snitzer 
19934cc96131SMike Snitzer 	dm_mq_cleanup_mapped_device(md);
19940f20972fSMike Snitzer }
19950f20972fSMike Snitzer 
19961da177e4SLinus Torvalds /*
19971da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
19981da177e4SLinus Torvalds  */
19992b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
20001da177e4SLinus Torvalds {
2001115485e8SMike Snitzer 	int r, numa_node_id = dm_get_numa_node();
2002115485e8SMike Snitzer 	struct mapped_device *md;
2003ba61fdd1SJeff Mahoney 	void *old_md;
20041da177e4SLinus Torvalds 
2005856eb091SMikulas Patocka 	md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
20061da177e4SLinus Torvalds 	if (!md) {
20071da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
20081da177e4SLinus Torvalds 		return NULL;
20091da177e4SLinus Torvalds 	}
20101da177e4SLinus Torvalds 
201110da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
20126ed7ade8SMilan Broz 		goto bad_module_get;
201310da4f79SJeff Mahoney 
20141da177e4SLinus Torvalds 	/* get a minor number for the dev */
20152b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
2016cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
20172b06cfffSAlasdair G Kergon 	else
2018cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
20191da177e4SLinus Torvalds 	if (r < 0)
20206ed7ade8SMilan Broz 		goto bad_minor;
20211da177e4SLinus Torvalds 
202283d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
202383d5e5b0SMikulas Patocka 	if (r < 0)
202483d5e5b0SMikulas Patocka 		goto bad_io_barrier;
202583d5e5b0SMikulas Patocka 
2026115485e8SMike Snitzer 	md->numa_node_id = numa_node_id;
2027591ddcfcSMike Snitzer 	md->init_tio_pdu = false;
2028a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
2029e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
2030a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
203186f1152bSBenjamin Marzinski 	mutex_init(&md->table_devices_lock);
2032022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
20331da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
20345c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
20351da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
20367a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
20377a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
203886f1152bSBenjamin Marzinski 	INIT_LIST_HEAD(&md->table_devices);
20397a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
20401da177e4SLinus Torvalds 
204147ace7e0SMike Snitzer 	/*
2042c62b37d9SChristoph Hellwig 	 * default to bio-based until DM table is loaded and md->type
2043c62b37d9SChristoph Hellwig 	 * established. If request-based table is loaded: blk-mq will
2044c62b37d9SChristoph Hellwig 	 * override accordingly.
204547ace7e0SMike Snitzer 	 */
204674fe6ba9SChristoph Hellwig 	md->disk = blk_alloc_disk(md->numa_node_id);
20471da177e4SLinus Torvalds 	if (!md->disk)
20480f20972fSMike Snitzer 		goto bad;
204974fe6ba9SChristoph Hellwig 	md->queue = md->disk->queue;
20501da177e4SLinus Torvalds 
2051f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
205253d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
20538b211aacSMing Lei 	INIT_WORK(&md->requeue_work, dm_wq_requeue_work);
2054f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
20552995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
2056f0b04115SJeff Mahoney 
20578b211aacSMing Lei 	md->requeue_list = NULL;
2058a666e5c0SMikulas Patocka 	md->swap_bios = get_swap_bios();
2059a666e5c0SMikulas Patocka 	sema_init(&md->swap_bios_semaphore, md->swap_bios);
2060a666e5c0SMikulas Patocka 	mutex_init(&md->swap_bios_lock);
2061a666e5c0SMikulas Patocka 
20621da177e4SLinus Torvalds 	md->disk->major = _major;
20631da177e4SLinus Torvalds 	md->disk->first_minor = minor;
206474fe6ba9SChristoph Hellwig 	md->disk->minors = 1;
20651ebe2e5fSChristoph Hellwig 	md->disk->flags |= GENHD_FL_NO_PART;
20661da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
20671da177e4SLinus Torvalds 	md->disk->queue = md->queue;
20681da177e4SLinus Torvalds 	md->disk->private_data = md;
20691da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
2070f26c5719SDan Williams 
20715d2a228bSChristoph Hellwig 	if (IS_ENABLED(CONFIG_FS_DAX)) {
207230c6828aSChristoph Hellwig 		md->dax_dev = alloc_dax(md, &dm_dax_ops);
2073d7519392SChristoph Hellwig 		if (IS_ERR(md->dax_dev)) {
2074d7519392SChristoph Hellwig 			md->dax_dev = NULL;
2075f26c5719SDan Williams 			goto bad;
2076976431b0SDan Williams 		}
20777ac5360cSChristoph Hellwig 		set_dax_nocache(md->dax_dev);
20787ac5360cSChristoph Hellwig 		set_dax_nomc(md->dax_dev);
2079fb08a190SChristoph Hellwig 		if (dax_add_host(md->dax_dev, md->disk))
2080f26c5719SDan Williams 			goto bad;
2081f26c5719SDan Williams 	}
20821da177e4SLinus Torvalds 
20837e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
20841da177e4SLinus Torvalds 
2085c7c879eeSMichał Mirosław 	md->wq = alloc_workqueue("kdmflush/%s", WQ_MEM_RECLAIM, 0, md->name);
2086304f3f6aSMilan Broz 	if (!md->wq)
20870f20972fSMike Snitzer 		goto bad;
2088304f3f6aSMilan Broz 
20899f6dc633SMike Snitzer 	md->pending_io = alloc_percpu(unsigned long);
20909f6dc633SMike Snitzer 	if (!md->pending_io)
20919f6dc633SMike Snitzer 		goto bad;
20929f6dc633SMike Snitzer 
2093fd2ed4d2SMikulas Patocka 	dm_stats_init(&md->stats);
2094fd2ed4d2SMikulas Patocka 
2095ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
2096f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
2097ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
2098f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2099ba61fdd1SJeff Mahoney 
2100ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
2101ba61fdd1SJeff Mahoney 
21021da177e4SLinus Torvalds 	return md;
21031da177e4SLinus Torvalds 
21040f20972fSMike Snitzer bad:
21050f20972fSMike Snitzer 	cleanup_mapped_device(md);
210683d5e5b0SMikulas Patocka bad_io_barrier:
21071da177e4SLinus Torvalds 	free_minor(minor);
21086ed7ade8SMilan Broz bad_minor:
210910da4f79SJeff Mahoney 	module_put(THIS_MODULE);
21106ed7ade8SMilan Broz bad_module_get:
2111856eb091SMikulas Patocka 	kvfree(md);
21121da177e4SLinus Torvalds 	return NULL;
21131da177e4SLinus Torvalds }
21141da177e4SLinus Torvalds 
2115ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
2116ae9da83fSJun'ichi Nomura 
21171da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
21181da177e4SLinus Torvalds {
2119f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
212063d94e48SJun'ichi Nomura 
2121ae9da83fSJun'ichi Nomura 	unlock_fs(md);
21222eb6e1e3SKeith Busch 
21230f20972fSMike Snitzer 	cleanup_mapped_device(md);
21240f20972fSMike Snitzer 
21250f20972fSMike Snitzer 	free_table_devices(&md->table_devices);
21260f20972fSMike Snitzer 	dm_stats_cleanup(&md->stats);
212763a4f065SMike Snitzer 	free_minor(minor);
212863a4f065SMike Snitzer 
212910da4f79SJeff Mahoney 	module_put(THIS_MODULE);
2130856eb091SMikulas Patocka 	kvfree(md);
21311da177e4SLinus Torvalds }
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds /*
21341da177e4SLinus Torvalds  * Bind a table to the device.
21351da177e4SLinus Torvalds  */
21361da177e4SLinus Torvalds static void event_callback(void *context)
21371da177e4SLinus Torvalds {
21387a8c3d3bSMike Anderson 	unsigned long flags;
21397a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
21401da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
21411da177e4SLinus Torvalds 
21427a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
21437a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
21447a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
21457a8c3d3bSMike Anderson 
2146ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
21477a8c3d3bSMike Anderson 
21481da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
21491da177e4SLinus Torvalds 	wake_up(&md->eventq);
215062e08243SMikulas Patocka 	dm_issue_global_event();
21511da177e4SLinus Torvalds }
21521da177e4SLinus Torvalds 
2153c217649bSMike Snitzer /*
2154042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2155042d2a9bSAlasdair G Kergon  */
2156042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2157754c5fc7SMike Snitzer 			       struct queue_limits *limits)
21581da177e4SLinus Torvalds {
2159042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
21601da177e4SLinus Torvalds 	sector_t size;
21612a2a4c51SJens Axboe 	int ret;
21621da177e4SLinus Torvalds 
21635a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
21645a8f1f80SBart Van Assche 
21651da177e4SLinus Torvalds 	size = dm_table_get_size(t);
21663ac51e74SDarrick J. Wong 
21673ac51e74SDarrick J. Wong 	/*
21683ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
21693ac51e74SDarrick J. Wong 	 */
2170fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
21713ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
21723ac51e74SDarrick J. Wong 
21735424a0b8SMikulas Patocka 	if (!get_capacity(md->disk))
21745424a0b8SMikulas Patocka 		set_capacity(md->disk, size);
21755424a0b8SMikulas Patocka 	else
2176f64d9b2eSChristoph Hellwig 		set_capacity_and_notify(md->disk, size);
21771da177e4SLinus Torvalds 
2178cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
21792ca3310eSAlasdair G Kergon 
2180f5b4aee1SMike Snitzer 	if (dm_table_request_based(t)) {
218116f12266SMike Snitzer 		/*
21829c37de29SMike Snitzer 		 * Leverage the fact that request-based DM targets are
21839c37de29SMike Snitzer 		 * immutable singletons - used to optimize dm_mq_queue_rq.
218416f12266SMike Snitzer 		 */
218516f12266SMike Snitzer 		md->immutable_target = dm_table_get_immutable_target(t);
2186e6ee8c0bSKiyoshi Ueda 
218729dec90aSChristoph Hellwig 		/*
218829dec90aSChristoph Hellwig 		 * There is no need to reload with request-based dm because the
218929dec90aSChristoph Hellwig 		 * size of front_pad doesn't change.
219029dec90aSChristoph Hellwig 		 *
219129dec90aSChristoph Hellwig 		 * Note for future: If you are to reload bioset, prep-ed
219229dec90aSChristoph Hellwig 		 * requests in the queue may refer to bio from the old bioset,
219329dec90aSChristoph Hellwig 		 * so you must walk through the queue to unprep.
219429dec90aSChristoph Hellwig 		 */
219529dec90aSChristoph Hellwig 		if (!md->mempools) {
219629dec90aSChristoph Hellwig 			md->mempools = t->mempools;
219729dec90aSChristoph Hellwig 			t->mempools = NULL;
219829dec90aSChristoph Hellwig 		}
219929dec90aSChristoph Hellwig 	} else {
220029dec90aSChristoph Hellwig 		/*
220129dec90aSChristoph Hellwig 		 * The md may already have mempools that need changing.
220229dec90aSChristoph Hellwig 		 * If so, reload bioset because front_pad may have changed
220329dec90aSChristoph Hellwig 		 * because a different table was loaded.
220429dec90aSChristoph Hellwig 		 */
220529dec90aSChristoph Hellwig 		dm_free_md_mempools(md->mempools);
220629dec90aSChristoph Hellwig 		md->mempools = t->mempools;
220729dec90aSChristoph Hellwig 		t->mempools = NULL;
22082a2a4c51SJens Axboe 	}
2209e6ee8c0bSKiyoshi Ueda 
2210f5b4aee1SMike Snitzer 	ret = dm_table_set_restrictions(t, md->queue, limits);
2211bb37d772SDamien Le Moal 	if (ret) {
2212bb37d772SDamien Le Moal 		old_map = ERR_PTR(ret);
2213bb37d772SDamien Le Moal 		goto out;
2214bb37d772SDamien Le Moal 	}
2215bb37d772SDamien Le Moal 
2216a12f5d48SEric Dumazet 	old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
22171d3aa6f6SMike Snitzer 	rcu_assign_pointer(md->map, (void *)t);
221836a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
221936a0456fSAlasdair G Kergon 
222041abc4e1SHannes Reinecke 	if (old_map)
222183d5e5b0SMikulas Patocka 		dm_sync_table(md);
22222a2a4c51SJens Axboe out:
2223042d2a9bSAlasdair G Kergon 	return old_map;
22241da177e4SLinus Torvalds }
22251da177e4SLinus Torvalds 
2226a7940155SAlasdair G Kergon /*
2227a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2228a7940155SAlasdair G Kergon  */
2229a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
22301da177e4SLinus Torvalds {
2231a12f5d48SEric Dumazet 	struct dm_table *map = rcu_dereference_protected(md->map, 1);
22321da177e4SLinus Torvalds 
22331da177e4SLinus Torvalds 	if (!map)
2234a7940155SAlasdair G Kergon 		return NULL;
22351da177e4SLinus Torvalds 
22361da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
22379cdb8520SMonam Agarwal 	RCU_INIT_POINTER(md->map, NULL);
223883d5e5b0SMikulas Patocka 	dm_sync_table(md);
2239a7940155SAlasdair G Kergon 
2240a7940155SAlasdair G Kergon 	return map;
22411da177e4SLinus Torvalds }
22421da177e4SLinus Torvalds 
22431da177e4SLinus Torvalds /*
22441da177e4SLinus Torvalds  * Constructor for a new device.
22451da177e4SLinus Torvalds  */
22462b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
22471da177e4SLinus Torvalds {
22481da177e4SLinus Torvalds 	struct mapped_device *md;
22491da177e4SLinus Torvalds 
22502b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
22511da177e4SLinus Torvalds 	if (!md)
22521da177e4SLinus Torvalds 		return -ENXIO;
22531da177e4SLinus Torvalds 
225491ccbbacSTushar Sugandhi 	dm_ima_reset_data(md);
225591ccbbacSTushar Sugandhi 
22561da177e4SLinus Torvalds 	*result = md;
22571da177e4SLinus Torvalds 	return 0;
22581da177e4SLinus Torvalds }
22591da177e4SLinus Torvalds 
2260a5664dadSMike Snitzer /*
2261a5664dadSMike Snitzer  * Functions to manage md->type.
2262a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2263a5664dadSMike Snitzer  */
2264a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2265a5664dadSMike Snitzer {
2266a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2267a5664dadSMike Snitzer }
2268a5664dadSMike Snitzer 
2269a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2270a5664dadSMike Snitzer {
2271a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2272a5664dadSMike Snitzer }
2273a5664dadSMike Snitzer 
22747e0d574fSBart Van Assche void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
2275a5664dadSMike Snitzer {
227600c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2277a5664dadSMike Snitzer 	md->type = type;
2278a5664dadSMike Snitzer }
2279a5664dadSMike Snitzer 
22807e0d574fSBart Van Assche enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
2281a5664dadSMike Snitzer {
2282a5664dadSMike Snitzer 	return md->type;
2283a5664dadSMike Snitzer }
2284a5664dadSMike Snitzer 
228536a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
228636a0456fSAlasdair G Kergon {
228736a0456fSAlasdair G Kergon 	return md->immutable_target_type;
228836a0456fSAlasdair G Kergon }
228936a0456fSAlasdair G Kergon 
22904a0b4ddfSMike Snitzer /*
2291f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
2292f84cb8a4SMike Snitzer  * count on 'md'.
2293f84cb8a4SMike Snitzer  */
2294f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2295f84cb8a4SMike Snitzer {
2296f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
2297f84cb8a4SMike Snitzer 	return &md->queue->limits;
2298f84cb8a4SMike Snitzer }
2299f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2300f84cb8a4SMike Snitzer 
23014a0b4ddfSMike Snitzer /*
23024a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
23034a0b4ddfSMike Snitzer  */
2304591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
23054a0b4ddfSMike Snitzer {
2306ba305859SChristoph Hellwig 	enum dm_queue_mode type = dm_table_get_type(t);
2307c100ec49SMike Snitzer 	struct queue_limits limits;
2308ba305859SChristoph Hellwig 	int r;
2309bfebd1cdSMike Snitzer 
2310545ed20eSToshi Kani 	switch (type) {
2311bfebd1cdSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
2312681cc5e8SMike Snitzer 		md->disk->fops = &dm_rq_blk_dops;
2313e83068a5SMike Snitzer 		r = dm_mq_init_request_queue(md, t);
2314bfebd1cdSMike Snitzer 		if (r) {
2315681cc5e8SMike Snitzer 			DMERR("Cannot initialize queue for request-based dm mapped device");
2316bfebd1cdSMike Snitzer 			return r;
2317bfebd1cdSMike Snitzer 		}
2318bfebd1cdSMike Snitzer 		break;
2319bfebd1cdSMike Snitzer 	case DM_TYPE_BIO_BASED:
2320545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
2321bfebd1cdSMike Snitzer 		break;
23227e0d574fSBart Van Assche 	case DM_TYPE_NONE:
23237e0d574fSBart Van Assche 		WARN_ON_ONCE(true);
23247e0d574fSBart Van Assche 		break;
2325ff36ab34SMike Snitzer 	}
23264a0b4ddfSMike Snitzer 
2327c100ec49SMike Snitzer 	r = dm_calculate_queue_limits(t, &limits);
2328c100ec49SMike Snitzer 	if (r) {
2329c100ec49SMike Snitzer 		DMERR("Cannot calculate initial queue limits");
2330c100ec49SMike Snitzer 		return r;
2331c100ec49SMike Snitzer 	}
2332bb37d772SDamien Le Moal 	r = dm_table_set_restrictions(t, md->queue, &limits);
2333bb37d772SDamien Le Moal 	if (r)
2334bb37d772SDamien Le Moal 		return r;
233589f871afSChristoph Hellwig 
2336e7089f65SLuis Chamberlain 	r = add_disk(md->disk);
2337e7089f65SLuis Chamberlain 	if (r)
2338e7089f65SLuis Chamberlain 		return r;
233989f871afSChristoph Hellwig 
234089f871afSChristoph Hellwig 	r = dm_sysfs_init(md);
234189f871afSChristoph Hellwig 	if (r) {
234289f871afSChristoph Hellwig 		del_gendisk(md->disk);
234389f871afSChristoph Hellwig 		return r;
234489f871afSChristoph Hellwig 	}
2345ba305859SChristoph Hellwig 	md->type = type;
23464a0b4ddfSMike Snitzer 	return 0;
23474a0b4ddfSMike Snitzer }
23484a0b4ddfSMike Snitzer 
23492bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev)
23501da177e4SLinus Torvalds {
23511da177e4SLinus Torvalds 	struct mapped_device *md;
23521da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
23531da177e4SLinus Torvalds 
23541da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
23551da177e4SLinus Torvalds 		return NULL;
23561da177e4SLinus Torvalds 
2357f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
23581da177e4SLinus Torvalds 
23591da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
236049de5769SMike Snitzer 	if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) ||
236149de5769SMike Snitzer 	    test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2362637842cfSDavid Teigland 		md = NULL;
2363fba9f90eSJeff Mahoney 		goto out;
2364fba9f90eSJeff Mahoney 	}
23652bec1f4aSMikulas Patocka 	dm_get(md);
2366fba9f90eSJeff Mahoney out:
2367f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
23681da177e4SLinus Torvalds 
2369637842cfSDavid Teigland 	return md;
2370637842cfSDavid Teigland }
23713cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2372d229a958SDavid Teigland 
23739ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2374637842cfSDavid Teigland {
23759ade92a9SAlasdair G Kergon 	return md->interface_ptr;
23761da177e4SLinus Torvalds }
23771da177e4SLinus Torvalds 
23781da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
23791da177e4SLinus Torvalds {
23801da177e4SLinus Torvalds 	md->interface_ptr = ptr;
23811da177e4SLinus Torvalds }
23821da177e4SLinus Torvalds 
23831da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
23841da177e4SLinus Torvalds {
23851da177e4SLinus Torvalds 	atomic_inc(&md->holders);
23863f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
23871da177e4SLinus Torvalds }
23881da177e4SLinus Torvalds 
238909ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md)
239009ee96b2SMikulas Patocka {
239109ee96b2SMikulas Patocka 	spin_lock(&_minor_lock);
239209ee96b2SMikulas Patocka 	if (test_bit(DMF_FREEING, &md->flags)) {
239309ee96b2SMikulas Patocka 		spin_unlock(&_minor_lock);
239409ee96b2SMikulas Patocka 		return -EBUSY;
239509ee96b2SMikulas Patocka 	}
239609ee96b2SMikulas Patocka 	dm_get(md);
239709ee96b2SMikulas Patocka 	spin_unlock(&_minor_lock);
239809ee96b2SMikulas Patocka 	return 0;
239909ee96b2SMikulas Patocka }
240009ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold);
240109ee96b2SMikulas Patocka 
240272d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
240372d94861SAlasdair G Kergon {
240472d94861SAlasdair G Kergon 	return md->name;
240572d94861SAlasdair G Kergon }
240672d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
240772d94861SAlasdair G Kergon 
24083f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
24091da177e4SLinus Torvalds {
24101134e5aeSMike Anderson 	struct dm_table *map;
241183d5e5b0SMikulas Patocka 	int srcu_idx;
24121da177e4SLinus Torvalds 
24133f77316dSKiyoshi Ueda 	might_sleep();
2414fba9f90eSJeff Mahoney 
241563a4f065SMike Snitzer 	spin_lock(&_minor_lock);
24163f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2417fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2418f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
24193f77316dSKiyoshi Ueda 
24207a5428dcSChristoph Hellwig 	blk_mark_disk_dead(md->disk);
24213b785fbcSBart Van Assche 
2422ab7c7bb6SMikulas Patocka 	/*
2423ab7c7bb6SMikulas Patocka 	 * Take suspend_lock so that presuspend and postsuspend methods
2424ab7c7bb6SMikulas Patocka 	 * do not race with internal suspend.
2425ab7c7bb6SMikulas Patocka 	 */
2426ab7c7bb6SMikulas Patocka 	mutex_lock(&md->suspend_lock);
24272a708cffSJunichi Nomura 	map = dm_get_live_table(md, &srcu_idx);
24284f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
24291da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
2430adc0daadSMikulas Patocka 		set_bit(DMF_SUSPENDED, &md->flags);
24315df96f2bSMikulas Patocka 		set_bit(DMF_POST_SUSPENDING, &md->flags);
24321da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
24331da177e4SLinus Torvalds 	}
243483d5e5b0SMikulas Patocka 	/* dm_put_live_table must be before msleep, otherwise deadlock is possible */
243583d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
24362a708cffSJunichi Nomura 	mutex_unlock(&md->suspend_lock);
243783d5e5b0SMikulas Patocka 
24383f77316dSKiyoshi Ueda 	/*
24393f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
24403f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
24413f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
24423f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
24433f77316dSKiyoshi Ueda 	 */
24443f77316dSKiyoshi Ueda 	if (wait)
24453f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
24463f77316dSKiyoshi Ueda 			msleep(1);
24473f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
24483f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
24493f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
24503f77316dSKiyoshi Ueda 
2451a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
24521da177e4SLinus Torvalds 	free_dev(md);
24531da177e4SLinus Torvalds }
24543f77316dSKiyoshi Ueda 
24553f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
24563f77316dSKiyoshi Ueda {
24573f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
24583f77316dSKiyoshi Ueda }
24593f77316dSKiyoshi Ueda 
24603f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
24613f77316dSKiyoshi Ueda {
24623f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
24633f77316dSKiyoshi Ueda }
24643f77316dSKiyoshi Ueda 
24653f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
24663f77316dSKiyoshi Ueda {
24673f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
24681da177e4SLinus Torvalds }
246979eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
24701da177e4SLinus Torvalds 
24719f6dc633SMike Snitzer static bool dm_in_flight_bios(struct mapped_device *md)
247285067747SMing Lei {
247385067747SMing Lei 	int cpu;
24749f6dc633SMike Snitzer 	unsigned long sum = 0;
247585067747SMing Lei 
24769f6dc633SMike Snitzer 	for_each_possible_cpu(cpu)
24779f6dc633SMike Snitzer 		sum += *per_cpu_ptr(md->pending_io, cpu);
247885067747SMing Lei 
247985067747SMing Lei 	return sum != 0;
248085067747SMing Lei }
248185067747SMing Lei 
24822f064a59SPeter Zijlstra static int dm_wait_for_bios_completion(struct mapped_device *md, unsigned int task_state)
248346125c1cSMilan Broz {
248446125c1cSMilan Broz 	int r = 0;
24859f4c3f87SBart Van Assche 	DEFINE_WAIT(wait);
248646125c1cSMilan Broz 
248785067747SMing Lei 	while (true) {
24889f4c3f87SBart Van Assche 		prepare_to_wait(&md->wait, &wait, task_state);
248946125c1cSMilan Broz 
24909f6dc633SMike Snitzer 		if (!dm_in_flight_bios(md))
249146125c1cSMilan Broz 			break;
249246125c1cSMilan Broz 
2493e3fabdfdSBart Van Assche 		if (signal_pending_state(task_state, current)) {
249446125c1cSMilan Broz 			r = -EINTR;
249546125c1cSMilan Broz 			break;
249646125c1cSMilan Broz 		}
249746125c1cSMilan Broz 
249846125c1cSMilan Broz 		io_schedule();
249946125c1cSMilan Broz 	}
25009f4c3f87SBart Van Assche 	finish_wait(&md->wait, &wait);
2501b44ebeb0SMikulas Patocka 
25029f6dc633SMike Snitzer 	smp_rmb();
25039f6dc633SMike Snitzer 
250446125c1cSMilan Broz 	return r;
250546125c1cSMilan Broz }
250646125c1cSMilan Broz 
25072f064a59SPeter Zijlstra static int dm_wait_for_completion(struct mapped_device *md, unsigned int task_state)
250885067747SMing Lei {
250985067747SMing Lei 	int r = 0;
251085067747SMing Lei 
251185067747SMing Lei 	if (!queue_is_mq(md->queue))
251285067747SMing Lei 		return dm_wait_for_bios_completion(md, task_state);
251385067747SMing Lei 
251485067747SMing Lei 	while (true) {
251585067747SMing Lei 		if (!blk_mq_queue_inflight(md->queue))
251685067747SMing Lei 			break;
251785067747SMing Lei 
251885067747SMing Lei 		if (signal_pending_state(task_state, current)) {
251985067747SMing Lei 			r = -EINTR;
252085067747SMing Lei 			break;
252185067747SMing Lei 		}
252285067747SMing Lei 
252385067747SMing Lei 		msleep(5);
252485067747SMing Lei 	}
252585067747SMing Lei 
252685067747SMing Lei 	return r;
252785067747SMing Lei }
252885067747SMing Lei 
25291da177e4SLinus Torvalds /*
25301da177e4SLinus Torvalds  * Process the deferred bios
25311da177e4SLinus Torvalds  */
2532ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
25331da177e4SLinus Torvalds {
25340c2915b8SMike Snitzer 	struct mapped_device *md = container_of(work, struct mapped_device, work);
25350c2915b8SMike Snitzer 	struct bio *bio;
2536ef208587SMikulas Patocka 
25373b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2538022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
25390c2915b8SMike Snitzer 		bio = bio_list_pop(&md->deferred);
2540022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2541022c2611SMikulas Patocka 
25420c2915b8SMike Snitzer 		if (!bio)
2543df12ee99SAlasdair G Kergon 			break;
254473d410c0SMilan Broz 
25450c2915b8SMike Snitzer 		submit_bio_noacct(bio);
2546e6ee8c0bSKiyoshi Ueda 	}
25471da177e4SLinus Torvalds }
25481da177e4SLinus Torvalds 
25499a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2550304f3f6aSMilan Broz {
25513b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
25524e857c58SPeter Zijlstra 	smp_mb__after_atomic();
255353d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2554304f3f6aSMilan Broz }
2555304f3f6aSMilan Broz 
25561da177e4SLinus Torvalds /*
2557042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
25581da177e4SLinus Torvalds  */
2559042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
25601da177e4SLinus Torvalds {
256187eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2562754c5fc7SMike Snitzer 	struct queue_limits limits;
2563042d2a9bSAlasdair G Kergon 	int r;
25641da177e4SLinus Torvalds 
2565e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
25661da177e4SLinus Torvalds 
25671da177e4SLinus Torvalds 	/* device must be suspended */
25684f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
256993c534aeSAlasdair G Kergon 		goto out;
25701da177e4SLinus Torvalds 
25713ae70656SMike Snitzer 	/*
25723ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
25733ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
25743ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
25753ae70656SMike Snitzer 	 * reappear.
25763ae70656SMike Snitzer 	 */
25773ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
257883d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
25793ae70656SMike Snitzer 		if (live_map)
25803ae70656SMike Snitzer 			limits = md->queue->limits;
258183d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
25823ae70656SMike Snitzer 	}
25833ae70656SMike Snitzer 
258487eb5b21SMike Christie 	if (!live_map) {
2585754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2586042d2a9bSAlasdair G Kergon 		if (r) {
2587042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2588754c5fc7SMike Snitzer 			goto out;
2589042d2a9bSAlasdair G Kergon 		}
259087eb5b21SMike Christie 	}
2591754c5fc7SMike Snitzer 
2592042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
259362e08243SMikulas Patocka 	dm_issue_global_event();
25941da177e4SLinus Torvalds 
259593c534aeSAlasdair G Kergon out:
2596e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2597042d2a9bSAlasdair G Kergon 	return map;
25981da177e4SLinus Torvalds }
25991da177e4SLinus Torvalds 
26001da177e4SLinus Torvalds /*
26011da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
26021da177e4SLinus Torvalds  * device.
26031da177e4SLinus Torvalds  */
26042ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
26051da177e4SLinus Torvalds {
2606e39e2e95SAlasdair G Kergon 	int r;
26071da177e4SLinus Torvalds 
2608040f04bdSChristoph Hellwig 	WARN_ON(test_bit(DMF_FROZEN, &md->flags));
2609dfbe03f6SAlasdair G Kergon 
2610977115c0SChristoph Hellwig 	r = freeze_bdev(md->disk->part0);
2611040f04bdSChristoph Hellwig 	if (!r)
2612aa8d7c2fSAlasdair G Kergon 		set_bit(DMF_FROZEN, &md->flags);
2613040f04bdSChristoph Hellwig 	return r;
26141da177e4SLinus Torvalds }
26151da177e4SLinus Torvalds 
26162ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
26171da177e4SLinus Torvalds {
2618aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2619aa8d7c2fSAlasdair G Kergon 		return;
2620977115c0SChristoph Hellwig 	thaw_bdev(md->disk->part0);
2621aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
26221da177e4SLinus Torvalds }
26231da177e4SLinus Torvalds 
26241da177e4SLinus Torvalds /*
2625b48633f8SBart Van Assche  * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
2626b48633f8SBart Van Assche  * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
2627b48633f8SBart Van Assche  * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
2628b48633f8SBart Van Assche  *
2629ffcc3936SMike Snitzer  * If __dm_suspend returns 0, the device is completely quiescent
2630ffcc3936SMike Snitzer  * now. There is no request-processing activity. All new requests
2631ffcc3936SMike Snitzer  * are being added to md->deferred list.
2632cec47e3dSKiyoshi Ueda  */
2633ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
26342f064a59SPeter Zijlstra 			unsigned suspend_flags, unsigned int task_state,
2635eaf9a736SMike Snitzer 			int dmf_suspended_flag)
26361da177e4SLinus Torvalds {
2637ffcc3936SMike Snitzer 	bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2638ffcc3936SMike Snitzer 	bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2639ffcc3936SMike Snitzer 	int r;
2640cf222b37SAlasdair G Kergon 
26415a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
26425a8f1f80SBart Van Assche 
26432e93ccc1SKiyoshi Ueda 	/*
26442e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
26452e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
26462e93ccc1SKiyoshi Ueda 	 */
26472e93ccc1SKiyoshi Ueda 	if (noflush)
26482e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
264986331f39SBart Van Assche 	else
2650ac75b09fSMike Snitzer 		DMDEBUG("%s: suspending with flush", dm_device_name(md));
26512e93ccc1SKiyoshi Ueda 
2652d67ee213SMike Snitzer 	/*
2653d67ee213SMike Snitzer 	 * This gets reverted if there's an error later and the targets
2654d67ee213SMike Snitzer 	 * provide the .presuspend_undo hook.
2655d67ee213SMike Snitzer 	 */
26561da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
26571da177e4SLinus Torvalds 
26582e93ccc1SKiyoshi Ueda 	/*
26599f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
26609f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
26619f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
26629f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
26632e93ccc1SKiyoshi Ueda 	 */
266432a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
26652ca3310eSAlasdair G Kergon 		r = lock_fs(md);
2666d67ee213SMike Snitzer 		if (r) {
2667d67ee213SMike Snitzer 			dm_table_presuspend_undo_targets(map);
2668ffcc3936SMike Snitzer 			return r;
2669aa8d7c2fSAlasdair G Kergon 		}
2670d67ee213SMike Snitzer 	}
26711da177e4SLinus Torvalds 
26721da177e4SLinus Torvalds 	/*
26733b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
26743b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
267596c9865cSMike Snitzer 	 * dm_split_and_process_bio from dm_submit_bio.
26763b00b203SMikulas Patocka 	 *
267796c9865cSMike Snitzer 	 * To get all processes out of dm_split_and_process_bio in dm_submit_bio,
26783b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
267996c9865cSMike Snitzer 	 * dm_split_and_process_bio from dm_submit_bio and quiesce the thread
26800cede372SMike Snitzer 	 * (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND and call
26816a8736d1STejun Heo 	 * flush_workqueue(md->wq).
26821da177e4SLinus Torvalds 	 */
26831eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
268441abc4e1SHannes Reinecke 	if (map)
268583d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
26861da177e4SLinus Torvalds 
2687d0bcb878SKiyoshi Ueda 	/*
268829e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
268929e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2690d0bcb878SKiyoshi Ueda 	 */
26916a23e05cSJens Axboe 	if (dm_request_based(md))
2692eca7ee6dSMike Snitzer 		dm_stop_queue(md->queue);
2693cec47e3dSKiyoshi Ueda 
2694d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2695d0bcb878SKiyoshi Ueda 
26961da177e4SLinus Torvalds 	/*
26973b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
26983b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
26993b00b203SMikulas Patocka 	 * to finish.
27001da177e4SLinus Torvalds 	 */
2701b48633f8SBart Van Assche 	r = dm_wait_for_completion(md, task_state);
2702eaf9a736SMike Snitzer 	if (!r)
2703eaf9a736SMike Snitzer 		set_bit(dmf_suspended_flag, &md->flags);
27041da177e4SLinus Torvalds 
27056d6f10dfSMilan Broz 	if (noflush)
2706022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
270741abc4e1SHannes Reinecke 	if (map)
270883d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
27092e93ccc1SKiyoshi Ueda 
27101da177e4SLinus Torvalds 	/* were we interrupted ? */
271146125c1cSMilan Broz 	if (r < 0) {
27129a1fb464SMikulas Patocka 		dm_queue_flush(md);
271373d410c0SMilan Broz 
2714cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
2715eca7ee6dSMike Snitzer 			dm_start_queue(md->queue);
2716cec47e3dSKiyoshi Ueda 
27172ca3310eSAlasdair G Kergon 		unlock_fs(md);
2718d67ee213SMike Snitzer 		dm_table_presuspend_undo_targets(map);
2719ffcc3936SMike Snitzer 		/* pushback list is already flushed, so skip flush */
2720ffcc3936SMike Snitzer 	}
2721ffcc3936SMike Snitzer 
2722ffcc3936SMike Snitzer 	return r;
27232ca3310eSAlasdair G Kergon }
27242ca3310eSAlasdair G Kergon 
27253b00b203SMikulas Patocka /*
2726ffcc3936SMike Snitzer  * We need to be able to change a mapping table under a mounted
2727ffcc3936SMike Snitzer  * filesystem.  For example we might want to move some data in
2728ffcc3936SMike Snitzer  * the background.  Before the table can be swapped with
2729ffcc3936SMike Snitzer  * dm_bind_table, dm_suspend must be called to flush any in
2730ffcc3936SMike Snitzer  * flight bios and ensure that any further io gets deferred.
27313b00b203SMikulas Patocka  */
2732ffcc3936SMike Snitzer /*
2733ffcc3936SMike Snitzer  * Suspend mechanism in request-based dm.
2734ffcc3936SMike Snitzer  *
2735ffcc3936SMike Snitzer  * 1. Flush all I/Os by lock_fs() if needed.
2736ffcc3936SMike Snitzer  * 2. Stop dispatching any I/O by stopping the request_queue.
2737ffcc3936SMike Snitzer  * 3. Wait for all in-flight I/Os to be completed or requeued.
2738ffcc3936SMike Snitzer  *
2739ffcc3936SMike Snitzer  * To abort suspend, start the request_queue.
2740ffcc3936SMike Snitzer  */
2741ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2742ffcc3936SMike Snitzer {
2743ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2744ffcc3936SMike Snitzer 	int r = 0;
2745ffcc3936SMike Snitzer 
2746ffcc3936SMike Snitzer retry:
2747ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2748ffcc3936SMike Snitzer 
2749ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2750ffcc3936SMike Snitzer 		r = -EINVAL;
2751ffcc3936SMike Snitzer 		goto out_unlock;
2752ffcc3936SMike Snitzer 	}
2753ffcc3936SMike Snitzer 
2754ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2755ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2756ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2757ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2758ffcc3936SMike Snitzer 		if (r)
2759ffcc3936SMike Snitzer 			return r;
2760ffcc3936SMike Snitzer 		goto retry;
2761ffcc3936SMike Snitzer 	}
2762ffcc3936SMike Snitzer 
2763a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2764ffcc3936SMike Snitzer 
2765eaf9a736SMike Snitzer 	r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
2766ffcc3936SMike Snitzer 	if (r)
2767ffcc3936SMike Snitzer 		goto out_unlock;
27683b00b203SMikulas Patocka 
27695df96f2bSMikulas Patocka 	set_bit(DMF_POST_SUSPENDING, &md->flags);
27704d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
27715df96f2bSMikulas Patocka 	clear_bit(DMF_POST_SUSPENDING, &md->flags);
27724d4471cbSKiyoshi Ueda 
2773d287483dSAlasdair G Kergon out_unlock:
2774e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2775cf222b37SAlasdair G Kergon 	return r;
27761da177e4SLinus Torvalds }
27771da177e4SLinus Torvalds 
2778ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map)
27791da177e4SLinus Torvalds {
2780ffcc3936SMike Snitzer 	if (map) {
2781ffcc3936SMike Snitzer 		int r = dm_table_resume_targets(map);
27828757b776SMilan Broz 		if (r)
2783ffcc3936SMike Snitzer 			return r;
2784ffcc3936SMike Snitzer 	}
27852ca3310eSAlasdair G Kergon 
27869a1fb464SMikulas Patocka 	dm_queue_flush(md);
27872ca3310eSAlasdair G Kergon 
2788cec47e3dSKiyoshi Ueda 	/*
2789cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2790cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2791cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2792cec47e3dSKiyoshi Ueda 	 */
2793cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2794eca7ee6dSMike Snitzer 		dm_start_queue(md->queue);
2795cec47e3dSKiyoshi Ueda 
27962ca3310eSAlasdair G Kergon 	unlock_fs(md);
27972ca3310eSAlasdair G Kergon 
2798ffcc3936SMike Snitzer 	return 0;
2799ffcc3936SMike Snitzer }
2800ffcc3936SMike Snitzer 
2801ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md)
2802ffcc3936SMike Snitzer {
28038dc23658SMinfei Huang 	int r;
2804ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2805ffcc3936SMike Snitzer 
2806ffcc3936SMike Snitzer retry:
28078dc23658SMinfei Huang 	r = -EINVAL;
2808ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2809ffcc3936SMike Snitzer 
2810ffcc3936SMike Snitzer 	if (!dm_suspended_md(md))
2811ffcc3936SMike Snitzer 		goto out;
2812ffcc3936SMike Snitzer 
2813ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2814ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2815ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2816ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2817ffcc3936SMike Snitzer 		if (r)
2818ffcc3936SMike Snitzer 			return r;
2819ffcc3936SMike Snitzer 		goto retry;
2820ffcc3936SMike Snitzer 	}
2821ffcc3936SMike Snitzer 
2822a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2823ffcc3936SMike Snitzer 	if (!map || !dm_table_get_size(map))
2824ffcc3936SMike Snitzer 		goto out;
2825ffcc3936SMike Snitzer 
2826ffcc3936SMike Snitzer 	r = __dm_resume(md, map);
2827ffcc3936SMike Snitzer 	if (r)
2828ffcc3936SMike Snitzer 		goto out;
2829ffcc3936SMike Snitzer 
28302ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
2831cf222b37SAlasdair G Kergon out:
2832e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
28332ca3310eSAlasdair G Kergon 
2834cf222b37SAlasdair G Kergon 	return r;
28351da177e4SLinus Torvalds }
28361da177e4SLinus Torvalds 
2837fd2ed4d2SMikulas Patocka /*
2838fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
2839fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
2840fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
2841fd2ed4d2SMikulas Patocka  */
2842fd2ed4d2SMikulas Patocka 
2843ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
2844ffcc3936SMike Snitzer {
2845ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2846ffcc3936SMike Snitzer 
28471ea0654eSBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
28481ea0654eSBart Van Assche 
284996b26c8cSMikulas Patocka 	if (md->internal_suspend_count++)
2850ffcc3936SMike Snitzer 		return; /* nested internal suspend */
2851ffcc3936SMike Snitzer 
2852ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2853ffcc3936SMike Snitzer 		set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2854ffcc3936SMike Snitzer 		return; /* nest suspend */
2855ffcc3936SMike Snitzer 	}
2856ffcc3936SMike Snitzer 
2857a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2858ffcc3936SMike Snitzer 
2859ffcc3936SMike Snitzer 	/*
2860ffcc3936SMike Snitzer 	 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2861ffcc3936SMike Snitzer 	 * supported.  Properly supporting a TASK_INTERRUPTIBLE internal suspend
2862ffcc3936SMike Snitzer 	 * would require changing .presuspend to return an error -- avoid this
2863ffcc3936SMike Snitzer 	 * until there is a need for more elaborate variants of internal suspend.
2864ffcc3936SMike Snitzer 	 */
2865eaf9a736SMike Snitzer 	(void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
2866eaf9a736SMike Snitzer 			    DMF_SUSPENDED_INTERNALLY);
2867ffcc3936SMike Snitzer 
28685df96f2bSMikulas Patocka 	set_bit(DMF_POST_SUSPENDING, &md->flags);
2869ffcc3936SMike Snitzer 	dm_table_postsuspend_targets(map);
28705df96f2bSMikulas Patocka 	clear_bit(DMF_POST_SUSPENDING, &md->flags);
2871ffcc3936SMike Snitzer }
2872ffcc3936SMike Snitzer 
2873ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md)
2874ffcc3936SMike Snitzer {
287596b26c8cSMikulas Patocka 	BUG_ON(!md->internal_suspend_count);
287696b26c8cSMikulas Patocka 
287796b26c8cSMikulas Patocka 	if (--md->internal_suspend_count)
2878ffcc3936SMike Snitzer 		return; /* resume from nested internal suspend */
2879ffcc3936SMike Snitzer 
2880ffcc3936SMike Snitzer 	if (dm_suspended_md(md))
2881ffcc3936SMike Snitzer 		goto done; /* resume from nested suspend */
2882ffcc3936SMike Snitzer 
2883ffcc3936SMike Snitzer 	/*
2884ffcc3936SMike Snitzer 	 * NOTE: existing callers don't need to call dm_table_resume_targets
2885ffcc3936SMike Snitzer 	 * (which may fail -- so best to avoid it for now by passing NULL map)
2886ffcc3936SMike Snitzer 	 */
2887ffcc3936SMike Snitzer 	(void) __dm_resume(md, NULL);
2888ffcc3936SMike Snitzer 
2889ffcc3936SMike Snitzer done:
2890ffcc3936SMike Snitzer 	clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2891ffcc3936SMike Snitzer 	smp_mb__after_atomic();
2892ffcc3936SMike Snitzer 	wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2893ffcc3936SMike Snitzer }
2894ffcc3936SMike Snitzer 
2895ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md)
2896fd2ed4d2SMikulas Patocka {
2897fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
2898ffcc3936SMike Snitzer 	__dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2899ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2900ffcc3936SMike Snitzer }
2901ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
2902ffcc3936SMike Snitzer 
2903ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md)
2904ffcc3936SMike Snitzer {
2905ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2906ffcc3936SMike Snitzer 	__dm_internal_resume(md);
2907ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2908ffcc3936SMike Snitzer }
2909ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume);
2910ffcc3936SMike Snitzer 
2911ffcc3936SMike Snitzer /*
2912ffcc3936SMike Snitzer  * Fast variants of internal suspend/resume hold md->suspend_lock,
2913ffcc3936SMike Snitzer  * which prevents interaction with userspace-driven suspend.
2914ffcc3936SMike Snitzer  */
2915ffcc3936SMike Snitzer 
2916ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md)
2917ffcc3936SMike Snitzer {
2918ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2919ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2920fd2ed4d2SMikulas Patocka 		return;
2921fd2ed4d2SMikulas Patocka 
2922fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2923fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
2924fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
2925fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2926fd2ed4d2SMikulas Patocka }
2927b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
2928fd2ed4d2SMikulas Patocka 
2929ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md)
2930fd2ed4d2SMikulas Patocka {
2931ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2932fd2ed4d2SMikulas Patocka 		goto done;
2933fd2ed4d2SMikulas Patocka 
2934fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
2935fd2ed4d2SMikulas Patocka 
2936fd2ed4d2SMikulas Patocka done:
2937fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
2938fd2ed4d2SMikulas Patocka }
2939b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
2940fd2ed4d2SMikulas Patocka 
29411da177e4SLinus Torvalds /*-----------------------------------------------------------------
29421da177e4SLinus Torvalds  * Event notification.
29431da177e4SLinus Torvalds  *---------------------------------------------------------------*/
29443abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
294560935eb2SMilan Broz 		       unsigned cookie)
294669267a30SAlasdair G Kergon {
29476958c1c6SMikulas Patocka 	int r;
29486958c1c6SMikulas Patocka 	unsigned noio_flag;
294960935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
295060935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
295160935eb2SMilan Broz 
29526958c1c6SMikulas Patocka 	noio_flag = memalloc_noio_save();
29536958c1c6SMikulas Patocka 
295460935eb2SMilan Broz 	if (!cookie)
29556958c1c6SMikulas Patocka 		r = kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
295660935eb2SMilan Broz 	else {
295760935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
295860935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
29596958c1c6SMikulas Patocka 		r = kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
29603abf85b5SPeter Rajnoha 				       action, envp);
296160935eb2SMilan Broz 	}
29626958c1c6SMikulas Patocka 
29636958c1c6SMikulas Patocka 	memalloc_noio_restore(noio_flag);
29646958c1c6SMikulas Patocka 
29656958c1c6SMikulas Patocka 	return r;
296669267a30SAlasdair G Kergon }
296769267a30SAlasdair G Kergon 
29687a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
29697a8c3d3bSMike Anderson {
29707a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
29717a8c3d3bSMike Anderson }
29727a8c3d3bSMike Anderson 
29731da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
29741da177e4SLinus Torvalds {
29751da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
29761da177e4SLinus Torvalds }
29771da177e4SLinus Torvalds 
29781da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
29791da177e4SLinus Torvalds {
29801da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
29811da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
29821da177e4SLinus Torvalds }
29831da177e4SLinus Torvalds 
29847a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
29857a8c3d3bSMike Anderson {
29867a8c3d3bSMike Anderson 	unsigned long flags;
29877a8c3d3bSMike Anderson 
29887a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
29897a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
29907a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
29917a8c3d3bSMike Anderson }
29927a8c3d3bSMike Anderson 
29931da177e4SLinus Torvalds /*
29941da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
29951da177e4SLinus Torvalds  * count on 'md'.
29961da177e4SLinus Torvalds  */
29971da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
29981da177e4SLinus Torvalds {
29991da177e4SLinus Torvalds 	return md->disk;
30001da177e4SLinus Torvalds }
300165ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk);
30021da177e4SLinus Torvalds 
3003784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
3004784aae73SMilan Broz {
30052995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
3006784aae73SMilan Broz }
3007784aae73SMilan Broz 
3008784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
3009784aae73SMilan Broz {
3010784aae73SMilan Broz 	struct mapped_device *md;
3011784aae73SMilan Broz 
30122995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
3013784aae73SMilan Broz 
3014b9a41d21SHou Tao 	spin_lock(&_minor_lock);
3015b9a41d21SHou Tao 	if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
3016b9a41d21SHou Tao 		md = NULL;
3017b9a41d21SHou Tao 		goto out;
3018b9a41d21SHou Tao 	}
3019784aae73SMilan Broz 	dm_get(md);
3020b9a41d21SHou Tao out:
3021b9a41d21SHou Tao 	spin_unlock(&_minor_lock);
3022b9a41d21SHou Tao 
3023784aae73SMilan Broz 	return md;
3024784aae73SMilan Broz }
3025784aae73SMilan Broz 
30264f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
30271da177e4SLinus Torvalds {
30281da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
30291da177e4SLinus Torvalds }
30301da177e4SLinus Torvalds 
30315df96f2bSMikulas Patocka static int dm_post_suspending_md(struct mapped_device *md)
30325df96f2bSMikulas Patocka {
30335df96f2bSMikulas Patocka 	return test_bit(DMF_POST_SUSPENDING, &md->flags);
30345df96f2bSMikulas Patocka }
30355df96f2bSMikulas Patocka 
3036ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md)
3037ffcc3936SMike Snitzer {
3038ffcc3936SMike Snitzer 	return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3039ffcc3936SMike Snitzer }
3040ffcc3936SMike Snitzer 
30412c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
30422c140a24SMikulas Patocka {
30432c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
30442c140a24SMikulas Patocka }
30452c140a24SMikulas Patocka 
304664dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
304764dbce58SKiyoshi Ueda {
304833bd6f06SMike Snitzer 	return dm_suspended_md(ti->table->md);
304964dbce58SKiyoshi Ueda }
305064dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
305164dbce58SKiyoshi Ueda 
30525df96f2bSMikulas Patocka int dm_post_suspending(struct dm_target *ti)
30535df96f2bSMikulas Patocka {
305433bd6f06SMike Snitzer 	return dm_post_suspending_md(ti->table->md);
30555df96f2bSMikulas Patocka }
30565df96f2bSMikulas Patocka EXPORT_SYMBOL_GPL(dm_post_suspending);
30575df96f2bSMikulas Patocka 
30582e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
30592e93ccc1SKiyoshi Ueda {
306033bd6f06SMike Snitzer 	return __noflush_suspending(ti->table->md);
30612e93ccc1SKiyoshi Ueda }
30622e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
30632e93ccc1SKiyoshi Ueda 
3064e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
3065e6ee8c0bSKiyoshi Ueda {
3066e6ee8c0bSKiyoshi Ueda 	if (!pools)
3067e6ee8c0bSKiyoshi Ueda 		return;
3068e6ee8c0bSKiyoshi Ueda 
30696f1c819cSKent Overstreet 	bioset_exit(&pools->bs);
30706f1c819cSKent Overstreet 	bioset_exit(&pools->io_bs);
3071e6ee8c0bSKiyoshi Ueda 
3072e6ee8c0bSKiyoshi Ueda 	kfree(pools);
3073e6ee8c0bSKiyoshi Ueda }
3074e6ee8c0bSKiyoshi Ueda 
30759c72bad1SChristoph Hellwig struct dm_pr {
30769c72bad1SChristoph Hellwig 	u64	old_key;
30779c72bad1SChristoph Hellwig 	u64	new_key;
30789c72bad1SChristoph Hellwig 	u32	flags;
30799c72bad1SChristoph Hellwig 	bool	fail_early;
30809c72bad1SChristoph Hellwig };
30819c72bad1SChristoph Hellwig 
30829c72bad1SChristoph Hellwig static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
30839c72bad1SChristoph Hellwig 		      void *data)
30849c72bad1SChristoph Hellwig {
30859c72bad1SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
30869c72bad1SChristoph Hellwig 	struct dm_table *table;
30879c72bad1SChristoph Hellwig 	struct dm_target *ti;
30889c72bad1SChristoph Hellwig 	int ret = -ENOTTY, srcu_idx;
30899c72bad1SChristoph Hellwig 
30909c72bad1SChristoph Hellwig 	table = dm_get_live_table(md, &srcu_idx);
30919c72bad1SChristoph Hellwig 	if (!table || !dm_table_get_size(table))
30929c72bad1SChristoph Hellwig 		goto out;
30939c72bad1SChristoph Hellwig 
30949c72bad1SChristoph Hellwig 	/* We only support devices that have a single target */
3095*2aec377aSMike Snitzer 	if (table->num_targets != 1)
30969c72bad1SChristoph Hellwig 		goto out;
30979c72bad1SChristoph Hellwig 	ti = dm_table_get_target(table, 0);
30989c72bad1SChristoph Hellwig 
30999c72bad1SChristoph Hellwig 	ret = -EINVAL;
31009c72bad1SChristoph Hellwig 	if (!ti->type->iterate_devices)
31019c72bad1SChristoph Hellwig 		goto out;
31029c72bad1SChristoph Hellwig 
31039c72bad1SChristoph Hellwig 	ret = ti->type->iterate_devices(ti, fn, data);
31049c72bad1SChristoph Hellwig out:
31059c72bad1SChristoph Hellwig 	dm_put_live_table(md, srcu_idx);
31069c72bad1SChristoph Hellwig 	return ret;
31079c72bad1SChristoph Hellwig }
31089c72bad1SChristoph Hellwig 
31099c72bad1SChristoph Hellwig /*
31109c72bad1SChristoph Hellwig  * For register / unregister we need to manually call out to every path.
31119c72bad1SChristoph Hellwig  */
31129c72bad1SChristoph Hellwig static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
31139c72bad1SChristoph Hellwig 			    sector_t start, sector_t len, void *data)
31149c72bad1SChristoph Hellwig {
31159c72bad1SChristoph Hellwig 	struct dm_pr *pr = data;
31169c72bad1SChristoph Hellwig 	const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
31179c72bad1SChristoph Hellwig 
31189c72bad1SChristoph Hellwig 	if (!ops || !ops->pr_register)
31199c72bad1SChristoph Hellwig 		return -EOPNOTSUPP;
31209c72bad1SChristoph Hellwig 	return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
31219c72bad1SChristoph Hellwig }
31229c72bad1SChristoph Hellwig 
312371cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
312471cdb697SChristoph Hellwig 			  u32 flags)
312571cdb697SChristoph Hellwig {
31269c72bad1SChristoph Hellwig 	struct dm_pr pr = {
31279c72bad1SChristoph Hellwig 		.old_key	= old_key,
31289c72bad1SChristoph Hellwig 		.new_key	= new_key,
31299c72bad1SChristoph Hellwig 		.flags		= flags,
31309c72bad1SChristoph Hellwig 		.fail_early	= true,
31319c72bad1SChristoph Hellwig 	};
31329c72bad1SChristoph Hellwig 	int ret;
313371cdb697SChristoph Hellwig 
31349c72bad1SChristoph Hellwig 	ret = dm_call_pr(bdev, __dm_pr_register, &pr);
31359c72bad1SChristoph Hellwig 	if (ret && new_key) {
31369c72bad1SChristoph Hellwig 		/* unregister all paths if we failed to register any path */
31379c72bad1SChristoph Hellwig 		pr.old_key = new_key;
31389c72bad1SChristoph Hellwig 		pr.new_key = 0;
31399c72bad1SChristoph Hellwig 		pr.flags = 0;
31409c72bad1SChristoph Hellwig 		pr.fail_early = false;
31419c72bad1SChristoph Hellwig 		dm_call_pr(bdev, __dm_pr_register, &pr);
31429c72bad1SChristoph Hellwig 	}
314371cdb697SChristoph Hellwig 
31449c72bad1SChristoph Hellwig 	return ret;
314571cdb697SChristoph Hellwig }
314671cdb697SChristoph Hellwig 
314771cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
314871cdb697SChristoph Hellwig 			 u32 flags)
314971cdb697SChristoph Hellwig {
315071cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
315171cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3152971888c4SMike Snitzer 	int r, srcu_idx;
315371cdb697SChristoph Hellwig 
31545bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
315571cdb697SChristoph Hellwig 	if (r < 0)
3156971888c4SMike Snitzer 		goto out;
315771cdb697SChristoph Hellwig 
315871cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
315971cdb697SChristoph Hellwig 	if (ops && ops->pr_reserve)
316071cdb697SChristoph Hellwig 		r = ops->pr_reserve(bdev, key, type, flags);
316171cdb697SChristoph Hellwig 	else
316271cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3163971888c4SMike Snitzer out:
3164971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
316571cdb697SChristoph Hellwig 	return r;
316671cdb697SChristoph Hellwig }
316771cdb697SChristoph Hellwig 
316871cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
316971cdb697SChristoph Hellwig {
317071cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
317171cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3172971888c4SMike Snitzer 	int r, srcu_idx;
317371cdb697SChristoph Hellwig 
31745bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
317571cdb697SChristoph Hellwig 	if (r < 0)
3176971888c4SMike Snitzer 		goto out;
317771cdb697SChristoph Hellwig 
317871cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
317971cdb697SChristoph Hellwig 	if (ops && ops->pr_release)
318071cdb697SChristoph Hellwig 		r = ops->pr_release(bdev, key, type);
318171cdb697SChristoph Hellwig 	else
318271cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3183971888c4SMike Snitzer out:
3184971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
318571cdb697SChristoph Hellwig 	return r;
318671cdb697SChristoph Hellwig }
318771cdb697SChristoph Hellwig 
318871cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
318971cdb697SChristoph Hellwig 			 enum pr_type type, bool abort)
319071cdb697SChristoph Hellwig {
319171cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
319271cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3193971888c4SMike Snitzer 	int r, srcu_idx;
319471cdb697SChristoph Hellwig 
31955bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
319671cdb697SChristoph Hellwig 	if (r < 0)
3197971888c4SMike Snitzer 		goto out;
319871cdb697SChristoph Hellwig 
319971cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
320071cdb697SChristoph Hellwig 	if (ops && ops->pr_preempt)
320171cdb697SChristoph Hellwig 		r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
320271cdb697SChristoph Hellwig 	else
320371cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3204971888c4SMike Snitzer out:
3205971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
320671cdb697SChristoph Hellwig 	return r;
320771cdb697SChristoph Hellwig }
320871cdb697SChristoph Hellwig 
320971cdb697SChristoph Hellwig static int dm_pr_clear(struct block_device *bdev, u64 key)
321071cdb697SChristoph Hellwig {
321171cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
321271cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3213971888c4SMike Snitzer 	int r, srcu_idx;
321471cdb697SChristoph Hellwig 
32155bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
321671cdb697SChristoph Hellwig 	if (r < 0)
3217971888c4SMike Snitzer 		goto out;
321871cdb697SChristoph Hellwig 
321971cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
322071cdb697SChristoph Hellwig 	if (ops && ops->pr_clear)
322171cdb697SChristoph Hellwig 		r = ops->pr_clear(bdev, key);
322271cdb697SChristoph Hellwig 	else
322371cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3224971888c4SMike Snitzer out:
3225971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
322671cdb697SChristoph Hellwig 	return r;
322771cdb697SChristoph Hellwig }
322871cdb697SChristoph Hellwig 
322971cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = {
323071cdb697SChristoph Hellwig 	.pr_register	= dm_pr_register,
323171cdb697SChristoph Hellwig 	.pr_reserve	= dm_pr_reserve,
323271cdb697SChristoph Hellwig 	.pr_release	= dm_pr_release,
323371cdb697SChristoph Hellwig 	.pr_preempt	= dm_pr_preempt,
323471cdb697SChristoph Hellwig 	.pr_clear	= dm_pr_clear,
323571cdb697SChristoph Hellwig };
323671cdb697SChristoph Hellwig 
323783d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
3238c62b37d9SChristoph Hellwig 	.submit_bio = dm_submit_bio,
3239b99fdcdcSMing Lei 	.poll_bio = dm_poll_bio,
32401da177e4SLinus Torvalds 	.open = dm_blk_open,
32411da177e4SLinus Torvalds 	.release = dm_blk_close,
3242aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
32433ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
3244e76239a3SChristoph Hellwig 	.report_zones = dm_blk_report_zones,
324571cdb697SChristoph Hellwig 	.pr_ops = &dm_pr_ops,
32461da177e4SLinus Torvalds 	.owner = THIS_MODULE
32471da177e4SLinus Torvalds };
32481da177e4SLinus Torvalds 
3249681cc5e8SMike Snitzer static const struct block_device_operations dm_rq_blk_dops = {
3250681cc5e8SMike Snitzer 	.open = dm_blk_open,
3251681cc5e8SMike Snitzer 	.release = dm_blk_close,
3252681cc5e8SMike Snitzer 	.ioctl = dm_blk_ioctl,
3253681cc5e8SMike Snitzer 	.getgeo = dm_blk_getgeo,
3254681cc5e8SMike Snitzer 	.pr_ops = &dm_pr_ops,
3255681cc5e8SMike Snitzer 	.owner = THIS_MODULE
3256681cc5e8SMike Snitzer };
3257681cc5e8SMike Snitzer 
3258f26c5719SDan Williams static const struct dax_operations dm_dax_ops = {
3259f26c5719SDan Williams 	.direct_access = dm_dax_direct_access,
3260cdf6cdcdSVivek Goyal 	.zero_page_range = dm_dax_zero_page_range,
3261047218ecSJane Chu 	.recovery_write = dm_dax_recovery_write,
3262f26c5719SDan Williams };
3263f26c5719SDan Williams 
32641da177e4SLinus Torvalds /*
32651da177e4SLinus Torvalds  * module hooks
32661da177e4SLinus Torvalds  */
32671da177e4SLinus Torvalds module_init(dm_init);
32681da177e4SLinus Torvalds module_exit(dm_exit);
32691da177e4SLinus Torvalds 
32701da177e4SLinus Torvalds module_param(major, uint, 0);
32711da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
3272f4790826SMike Snitzer 
3273e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3274e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3275e8603136SMike Snitzer 
3276115485e8SMike Snitzer module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
3277115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
3278115485e8SMike Snitzer 
3279a666e5c0SMikulas Patocka module_param(swap_bios, int, S_IRUGO | S_IWUSR);
3280a666e5c0SMikulas Patocka MODULE_PARM_DESC(swap_bios, "Maximum allowed inflight swap IOs");
3281a666e5c0SMikulas Patocka 
32821da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
32831da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
32841da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3285