xref: /openbmc/linux/drivers/md/dm.c (revision d3aa3e060c4a80827eb801fc448debc9daa7c46b)
13bd94003SHeinz Mauelshagen // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
4784aae73SMilan Broz  * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * This file is released under the GPL.
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
94cc96131SMike Snitzer #include "dm-core.h"
104cc96131SMike Snitzer #include "dm-rq.h"
1151e5b2bdSMike Anderson #include "dm-uevent.h"
1291ccbbacSTushar Sugandhi #include "dm-ima.h"
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include <linux/init.h>
151da177e4SLinus Torvalds #include <linux/module.h>
1648c9c27bSArjan van de Ven #include <linux/mutex.h>
176958c1c6SMikulas Patocka #include <linux/sched/mm.h>
18174cd4b1SIngo Molnar #include <linux/sched/signal.h>
191da177e4SLinus Torvalds #include <linux/blkpg.h>
201da177e4SLinus Torvalds #include <linux/bio.h>
211da177e4SLinus Torvalds #include <linux/mempool.h>
22f26c5719SDan Williams #include <linux/dax.h>
231da177e4SLinus Torvalds #include <linux/slab.h>
241da177e4SLinus Torvalds #include <linux/idr.h>
257e026c8cSDan Williams #include <linux/uio.h>
263ac51e74SDarrick J. Wong #include <linux/hdreg.h>
273f77316dSKiyoshi Ueda #include <linux/delay.h>
28ffcc3936SMike Snitzer #include <linux/wait.h>
2971cdb697SChristoph Hellwig #include <linux/pr.h>
30b0b4d7c6SElena Reshetova #include <linux/refcount.h>
31c6a564ffSChristoph Hellwig #include <linux/part_stat.h>
32a892c8d5SSatya Tangirala #include <linux/blk-crypto.h>
331e8d44bdSEric Biggers #include <linux/blk-crypto-profile.h>
3455782138SLi Zefan 
3572d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
3672d94861SAlasdair G Kergon 
3760935eb2SMilan Broz /*
3860935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
3960935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
4060935eb2SMilan Broz  */
4160935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
4260935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
4360935eb2SMilan Broz 
44b99fdcdcSMing Lei /*
45b99fdcdcSMing Lei  * For REQ_POLLED fs bio, this flag is set if we link mapped underlying
46b99fdcdcSMing Lei  * dm_io into one list, and reuse bio->bi_private as the list head. Before
47b99fdcdcSMing Lei  * ending this fs bio, we will recover its ->bi_private.
48b99fdcdcSMing Lei  */
49b99fdcdcSMing Lei #define REQ_DM_POLL_LIST	REQ_DRV
50b99fdcdcSMing Lei 
511da177e4SLinus Torvalds static const char *_name = DM_NAME;
521da177e4SLinus Torvalds 
532f06cd12SHeinz Mauelshagen static unsigned int major;
542f06cd12SHeinz Mauelshagen static unsigned int _major;
551da177e4SLinus Torvalds 
56d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr);
57d15b774cSAlasdair G Kergon 
58f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
592c140a24SMikulas Patocka 
602c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w);
612c140a24SMikulas Patocka 
622c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
632c140a24SMikulas Patocka 
64acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue;
65acfe0ad7SMikulas Patocka 
6693e6442cSMikulas Patocka atomic_t dm_global_event_nr = ATOMIC_INIT(0);
6793e6442cSMikulas Patocka DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq);
6893e6442cSMikulas Patocka 
6962e08243SMikulas Patocka void dm_issue_global_event(void)
7062e08243SMikulas Patocka {
7162e08243SMikulas Patocka 	atomic_inc(&dm_global_event_nr);
7262e08243SMikulas Patocka 	wake_up(&dm_global_eventq);
7362e08243SMikulas Patocka }
7462e08243SMikulas Patocka 
75442761fdSMike Snitzer DEFINE_STATIC_KEY_FALSE(stats_enabled);
76442761fdSMike Snitzer DEFINE_STATIC_KEY_FALSE(swap_bios_enabled);
77442761fdSMike Snitzer DEFINE_STATIC_KEY_FALSE(zoned_enabled);
78442761fdSMike Snitzer 
791da177e4SLinus Torvalds /*
8064f52b0eSMike Snitzer  * One of these is allocated (on-stack) per original bio.
811da177e4SLinus Torvalds  */
8264f52b0eSMike Snitzer struct clone_info {
8364f52b0eSMike Snitzer 	struct dm_table *map;
8464f52b0eSMike Snitzer 	struct bio *bio;
8564f52b0eSMike Snitzer 	struct dm_io *io;
8664f52b0eSMike Snitzer 	sector_t sector;
8786a3238cSHeinz Mauelshagen 	unsigned int sector_count;
884edadf6dSMike Snitzer 	bool is_abnormal_io:1;
894edadf6dSMike Snitzer 	bool submit_as_polled:1;
9064f52b0eSMike Snitzer };
9164f52b0eSMike Snitzer 
926c23f0bdSChristoph Hellwig static inline struct dm_target_io *clone_to_tio(struct bio *clone)
936c23f0bdSChristoph Hellwig {
946c23f0bdSChristoph Hellwig 	return container_of(clone, struct dm_target_io, clone);
956c23f0bdSChristoph Hellwig }
966c23f0bdSChristoph Hellwig 
9764f52b0eSMike Snitzer void *dm_per_bio_data(struct bio *bio, size_t data_size)
9864f52b0eSMike Snitzer {
99655f3aadSMike Snitzer 	if (!dm_tio_flagged(clone_to_tio(bio), DM_TIO_INSIDE_DM_IO))
10062f26317SJeffle Xu 		return (char *)bio - DM_TARGET_IO_BIO_OFFSET - data_size;
10162f26317SJeffle Xu 	return (char *)bio - DM_IO_BIO_OFFSET - data_size;
10264f52b0eSMike Snitzer }
10364f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_per_bio_data);
10464f52b0eSMike Snitzer 
10564f52b0eSMike Snitzer struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size)
10664f52b0eSMike Snitzer {
10764f52b0eSMike Snitzer 	struct dm_io *io = (struct dm_io *)((char *)data + data_size);
108b30f1607SHeinz Mauelshagen 
10964f52b0eSMike Snitzer 	if (io->magic == DM_IO_MAGIC)
11062f26317SJeffle Xu 		return (struct bio *)((char *)io + DM_IO_BIO_OFFSET);
11164f52b0eSMike Snitzer 	BUG_ON(io->magic != DM_TIO_MAGIC);
11262f26317SJeffle Xu 	return (struct bio *)((char *)io + DM_TARGET_IO_BIO_OFFSET);
11364f52b0eSMike Snitzer }
11464f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_from_per_bio_data);
11564f52b0eSMike Snitzer 
11686a3238cSHeinz Mauelshagen unsigned int dm_bio_get_target_bio_nr(const struct bio *bio)
11764f52b0eSMike Snitzer {
11864f52b0eSMike Snitzer 	return container_of(bio, struct dm_target_io, clone)->target_bio_nr;
11964f52b0eSMike Snitzer }
12064f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_get_target_bio_nr);
12164f52b0eSMike Snitzer 
122ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
123ba61fdd1SJeff Mahoney 
124115485e8SMike Snitzer #define DM_NUMA_NODE NUMA_NO_NODE
125115485e8SMike Snitzer static int dm_numa_node = DM_NUMA_NODE;
126faad87dfSMike Snitzer 
127a666e5c0SMikulas Patocka #define DEFAULT_SWAP_BIOS	(8 * 1048576 / PAGE_SIZE)
128a666e5c0SMikulas Patocka static int swap_bios = DEFAULT_SWAP_BIOS;
129a666e5c0SMikulas Patocka static int get_swap_bios(void)
130a666e5c0SMikulas Patocka {
131a666e5c0SMikulas Patocka 	int latch = READ_ONCE(swap_bios);
132b30f1607SHeinz Mauelshagen 
133a666e5c0SMikulas Patocka 	if (unlikely(latch <= 0))
134a666e5c0SMikulas Patocka 		latch = DEFAULT_SWAP_BIOS;
135a666e5c0SMikulas Patocka 	return latch;
136a666e5c0SMikulas Patocka }
137a666e5c0SMikulas Patocka 
13886f1152bSBenjamin Marzinski struct table_device {
13986f1152bSBenjamin Marzinski 	struct list_head list;
140b0b4d7c6SElena Reshetova 	refcount_t count;
14186f1152bSBenjamin Marzinski 	struct dm_dev dm_dev;
14286f1152bSBenjamin Marzinski };
14386f1152bSBenjamin Marzinski 
144f4790826SMike Snitzer /*
145e8603136SMike Snitzer  * Bio-based DM's mempools' reserved IOs set by the user.
146e8603136SMike Snitzer  */
1474cc96131SMike Snitzer #define RESERVED_BIO_BASED_IOS		16
14886a3238cSHeinz Mauelshagen static unsigned int reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
149e8603136SMike Snitzer 
150115485e8SMike Snitzer static int __dm_get_module_param_int(int *module_param, int min, int max)
151115485e8SMike Snitzer {
1526aa7de05SMark Rutland 	int param = READ_ONCE(*module_param);
153115485e8SMike Snitzer 	int modified_param = 0;
154115485e8SMike Snitzer 	bool modified = true;
155115485e8SMike Snitzer 
156115485e8SMike Snitzer 	if (param < min)
157115485e8SMike Snitzer 		modified_param = min;
158115485e8SMike Snitzer 	else if (param > max)
159115485e8SMike Snitzer 		modified_param = max;
160115485e8SMike Snitzer 	else
161115485e8SMike Snitzer 		modified = false;
162115485e8SMike Snitzer 
163115485e8SMike Snitzer 	if (modified) {
164115485e8SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
165115485e8SMike Snitzer 		param = modified_param;
166115485e8SMike Snitzer 	}
167115485e8SMike Snitzer 
168115485e8SMike Snitzer 	return param;
169115485e8SMike Snitzer }
170115485e8SMike Snitzer 
17186a3238cSHeinz Mauelshagen unsigned int __dm_get_module_param(unsigned int *module_param, unsigned int def, unsigned int max)
172f4790826SMike Snitzer {
17386a3238cSHeinz Mauelshagen 	unsigned int param = READ_ONCE(*module_param);
17486a3238cSHeinz Mauelshagen 	unsigned int modified_param = 0;
175f4790826SMike Snitzer 
17609c2d531SMike Snitzer 	if (!param)
17709c2d531SMike Snitzer 		modified_param = def;
17809c2d531SMike Snitzer 	else if (param > max)
17909c2d531SMike Snitzer 		modified_param = max;
180f4790826SMike Snitzer 
18109c2d531SMike Snitzer 	if (modified_param) {
18209c2d531SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
18309c2d531SMike Snitzer 		param = modified_param;
184f4790826SMike Snitzer 	}
185f4790826SMike Snitzer 
18609c2d531SMike Snitzer 	return param;
187f4790826SMike Snitzer }
188f4790826SMike Snitzer 
18986a3238cSHeinz Mauelshagen unsigned int dm_get_reserved_bio_based_ios(void)
190e8603136SMike Snitzer {
19109c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_bio_based_ios,
1924cc96131SMike Snitzer 				     RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS);
193e8603136SMike Snitzer }
194e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
195e8603136SMike Snitzer 
19686a3238cSHeinz Mauelshagen static unsigned int dm_get_numa_node(void)
197115485e8SMike Snitzer {
198115485e8SMike Snitzer 	return __dm_get_module_param_int(&dm_numa_node,
199115485e8SMike Snitzer 					 DM_NUMA_NODE, num_online_nodes() - 1);
200115485e8SMike Snitzer }
201115485e8SMike Snitzer 
2021da177e4SLinus Torvalds static int __init local_init(void)
2031da177e4SLinus Torvalds {
204e689fbabSMike Snitzer 	int r;
2051ae49ea2SMike Snitzer 
20651e5b2bdSMike Anderson 	r = dm_uevent_init();
20751157b4aSKiyoshi Ueda 	if (r)
208e689fbabSMike Snitzer 		return r;
20951e5b2bdSMike Anderson 
210acfe0ad7SMikulas Patocka 	deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
211acfe0ad7SMikulas Patocka 	if (!deferred_remove_workqueue) {
212acfe0ad7SMikulas Patocka 		r = -ENOMEM;
213acfe0ad7SMikulas Patocka 		goto out_uevent_exit;
214acfe0ad7SMikulas Patocka 	}
215acfe0ad7SMikulas Patocka 
2161da177e4SLinus Torvalds 	_major = major;
2171da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
21851157b4aSKiyoshi Ueda 	if (r < 0)
219acfe0ad7SMikulas Patocka 		goto out_free_workqueue;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	if (!_major)
2221da177e4SLinus Torvalds 		_major = r;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	return 0;
22551157b4aSKiyoshi Ueda 
226acfe0ad7SMikulas Patocka out_free_workqueue:
227acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
22851157b4aSKiyoshi Ueda out_uevent_exit:
22951157b4aSKiyoshi Ueda 	dm_uevent_exit();
23051157b4aSKiyoshi Ueda 
23151157b4aSKiyoshi Ueda 	return r;
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds static void local_exit(void)
2351da177e4SLinus Torvalds {
236acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
2372c140a24SMikulas Patocka 
23800d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
23951e5b2bdSMike Anderson 	dm_uevent_exit();
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	_major = 0;
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	DMINFO("cleaned up");
2441da177e4SLinus Torvalds }
2451da177e4SLinus Torvalds 
246b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
2471da177e4SLinus Torvalds 	local_init,
2481da177e4SLinus Torvalds 	dm_target_init,
2491da177e4SLinus Torvalds 	dm_linear_init,
2501da177e4SLinus Torvalds 	dm_stripe_init,
251952b3557SMikulas Patocka 	dm_io_init,
252945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2531da177e4SLinus Torvalds 	dm_interface_init,
254fd2ed4d2SMikulas Patocka 	dm_statistics_init,
2551da177e4SLinus Torvalds };
2561da177e4SLinus Torvalds 
257b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2581da177e4SLinus Torvalds 	local_exit,
2591da177e4SLinus Torvalds 	dm_target_exit,
2601da177e4SLinus Torvalds 	dm_linear_exit,
2611da177e4SLinus Torvalds 	dm_stripe_exit,
262952b3557SMikulas Patocka 	dm_io_exit,
263945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
2641da177e4SLinus Torvalds 	dm_interface_exit,
265fd2ed4d2SMikulas Patocka 	dm_statistics_exit,
2661da177e4SLinus Torvalds };
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds static int __init dm_init(void)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
2711da177e4SLinus Torvalds 	int r, i;
2721da177e4SLinus Torvalds 
273f1cd6cb2STushar Sugandhi #if (IS_ENABLED(CONFIG_IMA) && !IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE))
274f1cd6cb2STushar Sugandhi 	DMWARN("CONFIG_IMA_DISABLE_HTABLE is disabled."
275f1cd6cb2STushar Sugandhi 	       " Duplicate IMA measurements will not be recorded in the IMA log.");
276f1cd6cb2STushar Sugandhi #endif
277f1cd6cb2STushar Sugandhi 
2781da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
2791da177e4SLinus Torvalds 		r = _inits[i]();
2801da177e4SLinus Torvalds 		if (r)
2811da177e4SLinus Torvalds 			goto bad;
2821da177e4SLinus Torvalds 	}
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	return 0;
2851da177e4SLinus Torvalds bad:
2861da177e4SLinus Torvalds 	while (i--)
2871da177e4SLinus Torvalds 		_exits[i]();
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 	return r;
2901da177e4SLinus Torvalds }
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds static void __exit dm_exit(void)
2931da177e4SLinus Torvalds {
2941da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	while (i--)
2971da177e4SLinus Torvalds 		_exits[i]();
298d15b774cSAlasdair G Kergon 
299d15b774cSAlasdair G Kergon 	/*
300d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
301d15b774cSAlasdair G Kergon 	 */
302d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds /*
3061da177e4SLinus Torvalds  * Block device functions
3071da177e4SLinus Torvalds  */
308432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
309432a212cSMike Anderson {
310432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
311432a212cSMike Anderson }
312432a212cSMike Anderson 
313fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3141da177e4SLinus Torvalds {
3151da177e4SLinus Torvalds 	struct mapped_device *md;
3161da177e4SLinus Torvalds 
317fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
318fba9f90eSJeff Mahoney 
319fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
320fba9f90eSJeff Mahoney 	if (!md)
321fba9f90eSJeff Mahoney 		goto out;
322fba9f90eSJeff Mahoney 
3235c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
324432a212cSMike Anderson 	    dm_deleting_md(md)) {
325fba9f90eSJeff Mahoney 		md = NULL;
326fba9f90eSJeff Mahoney 		goto out;
327fba9f90eSJeff Mahoney 	}
328fba9f90eSJeff Mahoney 
3291da177e4SLinus Torvalds 	dm_get(md);
3305c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
331fba9f90eSJeff Mahoney out:
332fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
333fba9f90eSJeff Mahoney 
334fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
337db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode)
3381da177e4SLinus Torvalds {
33963a4f065SMike Snitzer 	struct mapped_device *md;
3406e9624b8SArnd Bergmann 
3414a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
3424a1aeb98SMilan Broz 
34363a4f065SMike Snitzer 	md = disk->private_data;
34463a4f065SMike Snitzer 	if (WARN_ON(!md))
34563a4f065SMike Snitzer 		goto out;
34663a4f065SMike Snitzer 
3472c140a24SMikulas Patocka 	if (atomic_dec_and_test(&md->open_count) &&
3482c140a24SMikulas Patocka 	    (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
349acfe0ad7SMikulas Patocka 		queue_work(deferred_remove_workqueue, &deferred_remove_work);
3502c140a24SMikulas Patocka 
3511da177e4SLinus Torvalds 	dm_put(md);
35263a4f065SMike Snitzer out:
3534a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds 
3565c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3575c6bd75dSAlasdair G Kergon {
3585c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3595c6bd75dSAlasdair G Kergon }
3605c6bd75dSAlasdair G Kergon 
3615c6bd75dSAlasdair G Kergon /*
3625c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3635c6bd75dSAlasdair G Kergon  */
3642c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
3655c6bd75dSAlasdair G Kergon {
3665c6bd75dSAlasdair G Kergon 	int r = 0;
3675c6bd75dSAlasdair G Kergon 
3685c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
3695c6bd75dSAlasdair G Kergon 
3702c140a24SMikulas Patocka 	if (dm_open_count(md)) {
3715c6bd75dSAlasdair G Kergon 		r = -EBUSY;
3722c140a24SMikulas Patocka 		if (mark_deferred)
3732c140a24SMikulas Patocka 			set_bit(DMF_DEFERRED_REMOVE, &md->flags);
3742c140a24SMikulas Patocka 	} else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
3752c140a24SMikulas Patocka 		r = -EEXIST;
3765c6bd75dSAlasdair G Kergon 	else
3775c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
3785c6bd75dSAlasdair G Kergon 
3795c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
3805c6bd75dSAlasdair G Kergon 
3815c6bd75dSAlasdair G Kergon 	return r;
3825c6bd75dSAlasdair G Kergon }
3835c6bd75dSAlasdair G Kergon 
3842c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md)
3852c140a24SMikulas Patocka {
3862c140a24SMikulas Patocka 	int r = 0;
3872c140a24SMikulas Patocka 
3882c140a24SMikulas Patocka 	spin_lock(&_minor_lock);
3892c140a24SMikulas Patocka 
3902c140a24SMikulas Patocka 	if (test_bit(DMF_DELETING, &md->flags))
3912c140a24SMikulas Patocka 		r = -EBUSY;
3922c140a24SMikulas Patocka 	else
3932c140a24SMikulas Patocka 		clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
3942c140a24SMikulas Patocka 
3952c140a24SMikulas Patocka 	spin_unlock(&_minor_lock);
3962c140a24SMikulas Patocka 
3972c140a24SMikulas Patocka 	return r;
3982c140a24SMikulas Patocka }
3992c140a24SMikulas Patocka 
4002c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w)
4012c140a24SMikulas Patocka {
4022c140a24SMikulas Patocka 	dm_deferred_remove();
4032c140a24SMikulas Patocka }
4042c140a24SMikulas Patocka 
4053ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4063ac51e74SDarrick J. Wong {
4073ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
4083ac51e74SDarrick J. Wong 
4093ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
4103ac51e74SDarrick J. Wong }
4113ac51e74SDarrick J. Wong 
412971888c4SMike Snitzer static int dm_prepare_ioctl(struct mapped_device *md, int *srcu_idx,
4135bd5e8d8SMike Snitzer 			    struct block_device **bdev)
414aa129a22SMilan Broz {
415564b5c54SMike Snitzer 	struct dm_target *ti;
4166c182cd8SHannes Reinecke 	struct dm_table *map;
417971888c4SMike Snitzer 	int r;
418aa129a22SMilan Broz 
4196c182cd8SHannes Reinecke retry:
420e56f81e0SChristoph Hellwig 	r = -ENOTTY;
421971888c4SMike Snitzer 	map = dm_get_live_table(md, srcu_idx);
422aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
423971888c4SMike Snitzer 		return r;
424aa129a22SMilan Broz 
425aa129a22SMilan Broz 	/* We only support devices that have a single target */
4262aec377aSMike Snitzer 	if (map->num_targets != 1)
427971888c4SMike Snitzer 		return r;
428aa129a22SMilan Broz 
429564b5c54SMike Snitzer 	ti = dm_table_get_target(map, 0);
430564b5c54SMike Snitzer 	if (!ti->type->prepare_ioctl)
431e56f81e0SChristoph Hellwig 		return r;
432aa129a22SMilan Broz 
433971888c4SMike Snitzer 	if (dm_suspended_md(md))
434971888c4SMike Snitzer 		return -EAGAIN;
435971888c4SMike Snitzer 
436564b5c54SMike Snitzer 	r = ti->type->prepare_ioctl(ti, bdev);
4375bbbfdf6SJunichi Nomura 	if (r == -ENOTCONN && !fatal_signal_pending(current)) {
438971888c4SMike Snitzer 		dm_put_live_table(md, *srcu_idx);
439238d991fSHeinz Mauelshagen 		fsleep(10000);
4406c182cd8SHannes Reinecke 		goto retry;
4416c182cd8SHannes Reinecke 	}
442971888c4SMike Snitzer 
443e56f81e0SChristoph Hellwig 	return r;
444e56f81e0SChristoph Hellwig }
4456c182cd8SHannes Reinecke 
446971888c4SMike Snitzer static void dm_unprepare_ioctl(struct mapped_device *md, int srcu_idx)
447971888c4SMike Snitzer {
448971888c4SMike Snitzer 	dm_put_live_table(md, srcu_idx);
449971888c4SMike Snitzer }
450971888c4SMike Snitzer 
451e56f81e0SChristoph Hellwig static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
452e56f81e0SChristoph Hellwig 			unsigned int cmd, unsigned long arg)
453e56f81e0SChristoph Hellwig {
454e56f81e0SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
455971888c4SMike Snitzer 	int r, srcu_idx;
456e56f81e0SChristoph Hellwig 
4575bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
458e56f81e0SChristoph Hellwig 	if (r < 0)
459971888c4SMike Snitzer 		goto out;
460e56f81e0SChristoph Hellwig 
461e56f81e0SChristoph Hellwig 	if (r > 0) {
462e56f81e0SChristoph Hellwig 		/*
463e980f623SChristoph Hellwig 		 * Target determined this ioctl is being issued against a
464e980f623SChristoph Hellwig 		 * subset of the parent bdev; require extra privileges.
465e56f81e0SChristoph Hellwig 		 */
466e980f623SChristoph Hellwig 		if (!capable(CAP_SYS_RAWIO)) {
4670378c625SMike Snitzer 			DMDEBUG_LIMIT(
468e980f623SChristoph Hellwig 	"%s: sending ioctl %x to DM device without required privilege.",
469e980f623SChristoph Hellwig 				current->comm, cmd);
470e980f623SChristoph Hellwig 			r = -ENOIOCTLCMD;
471e56f81e0SChristoph Hellwig 			goto out;
472e56f81e0SChristoph Hellwig 		}
473e980f623SChristoph Hellwig 	}
474e56f81e0SChristoph Hellwig 
475a7cb3d2fSChristoph Hellwig 	if (!bdev->bd_disk->fops->ioctl)
476a7cb3d2fSChristoph Hellwig 		r = -ENOTTY;
477a7cb3d2fSChristoph Hellwig 	else
478a7cb3d2fSChristoph Hellwig 		r = bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg);
479e56f81e0SChristoph Hellwig out:
480971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
481aa129a22SMilan Broz 	return r;
482aa129a22SMilan Broz }
483aa129a22SMilan Broz 
4847465d7acSMike Snitzer u64 dm_start_time_ns_from_clone(struct bio *bio)
4857465d7acSMike Snitzer {
4866c23f0bdSChristoph Hellwig 	return jiffies_to_nsecs(clone_to_tio(bio)->io->start_time);
4877465d7acSMike Snitzer }
4887465d7acSMike Snitzer EXPORT_SYMBOL_GPL(dm_start_time_ns_from_clone);
4897465d7acSMike Snitzer 
4908d394bc4SMike Snitzer static bool bio_is_flush_with_data(struct bio *bio)
4917465d7acSMike Snitzer {
4928d394bc4SMike Snitzer 	return ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size);
4937465d7acSMike Snitzer }
4947465d7acSMike Snitzer 
495e6926ad0SMing Lei static void dm_io_acct(struct dm_io *io, bool end)
4967465d7acSMike Snitzer {
497e6926ad0SMing Lei 	struct dm_stats_aux *stats_aux = &io->stats_aux;
498e6926ad0SMing Lei 	unsigned long start_time = io->start_time;
499e6926ad0SMing Lei 	struct mapped_device *md = io->md;
500e6926ad0SMing Lei 	struct bio *bio = io->orig_bio;
501d3de6d12SMing Lei 	unsigned int sectors;
5027465d7acSMike Snitzer 
503d3de6d12SMing Lei 	/*
504d3de6d12SMing Lei 	 * If REQ_PREFLUSH set, don't account payload, it will be
505d3de6d12SMing Lei 	 * submitted (and accounted) after this flush completes.
506d3de6d12SMing Lei 	 */
507d3de6d12SMing Lei 	if (bio_is_flush_with_data(bio))
508d3de6d12SMing Lei 		sectors = 0;
5097dd76d1fSMing Lei 	else if (likely(!(dm_io_flagged(io, DM_IO_WAS_SPLIT))))
510d3de6d12SMing Lei 		sectors = bio_sectors(bio);
5117dd76d1fSMing Lei 	else
5127dd76d1fSMing Lei 		sectors = io->sectors;
5138d394bc4SMike Snitzer 
5148d394bc4SMike Snitzer 	if (!end)
515d3de6d12SMing Lei 		bdev_start_io_acct(bio->bi_bdev, sectors, bio_op(bio),
516d3de6d12SMing Lei 				   start_time);
5178d394bc4SMike Snitzer 	else
518d3de6d12SMing Lei 		bdev_end_io_acct(bio->bi_bdev, bio_op(bio), start_time);
5197465d7acSMike Snitzer 
520442761fdSMike Snitzer 	if (static_branch_unlikely(&stats_enabled) &&
5217dd76d1fSMing Lei 	    unlikely(dm_stats_used(&md->stats))) {
5227dd76d1fSMing Lei 		sector_t sector;
5237dd76d1fSMing Lei 
5247dd76d1fSMing Lei 		if (likely(!dm_io_flagged(io, DM_IO_WAS_SPLIT)))
5257dd76d1fSMing Lei 			sector = bio->bi_iter.bi_sector;
5267dd76d1fSMing Lei 		else
5277dd76d1fSMing Lei 			sector = bio_end_sector(bio) - io->sector_offset;
5287dd76d1fSMing Lei 
5297465d7acSMike Snitzer 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
5307dd76d1fSMing Lei 				    sector, sectors,
5318d394bc4SMike Snitzer 				    end, start_time, stats_aux);
5328d394bc4SMike Snitzer 	}
5337dd76d1fSMing Lei }
5348d394bc4SMike Snitzer 
535b992b40dSMing Lei static void __dm_start_io_acct(struct dm_io *io)
5368d394bc4SMike Snitzer {
537e6926ad0SMing Lei 	dm_io_acct(io, false);
5388d394bc4SMike Snitzer }
5398d394bc4SMike Snitzer 
5400fbb4d93SMike Snitzer static void dm_start_io_acct(struct dm_io *io, struct bio *clone)
5418d394bc4SMike Snitzer {
5420fbb4d93SMike Snitzer 	/*
5430fbb4d93SMike Snitzer 	 * Ensure IO accounting is only ever started once.
5440fbb4d93SMike Snitzer 	 */
5453b03f7c1SMike Snitzer 	if (dm_io_flagged(io, DM_IO_ACCOUNTED))
5460fbb4d93SMike Snitzer 		return;
5473b03f7c1SMike Snitzer 
5483b03f7c1SMike Snitzer 	/* Expect no possibility for race unless DM_TIO_IS_DUPLICATE_BIO. */
5493b03f7c1SMike Snitzer 	if (!clone || likely(dm_tio_is_normal(clone_to_tio(clone)))) {
55082f6cdccSMike Snitzer 		dm_io_set_flag(io, DM_IO_ACCOUNTED);
55182f6cdccSMike Snitzer 	} else {
55282f6cdccSMike Snitzer 		unsigned long flags;
553655f3aadSMike Snitzer 		/* Can afford locking given DM_TIO_IS_DUPLICATE_BIO */
5544d7bca13SMike Snitzer 		spin_lock_irqsave(&io->lock, flags);
55510eb3a0dSBenjamin Marzinski 		if (dm_io_flagged(io, DM_IO_ACCOUNTED)) {
55610eb3a0dSBenjamin Marzinski 			spin_unlock_irqrestore(&io->lock, flags);
55710eb3a0dSBenjamin Marzinski 			return;
55810eb3a0dSBenjamin Marzinski 		}
55982f6cdccSMike Snitzer 		dm_io_set_flag(io, DM_IO_ACCOUNTED);
5604d7bca13SMike Snitzer 		spin_unlock_irqrestore(&io->lock, flags);
56182f6cdccSMike Snitzer 	}
5620fbb4d93SMike Snitzer 
563b992b40dSMing Lei 	__dm_start_io_acct(io);
5640fbb4d93SMike Snitzer }
5650fbb4d93SMike Snitzer 
566b992b40dSMing Lei static void dm_end_io_acct(struct dm_io *io)
5670fbb4d93SMike Snitzer {
568e6926ad0SMing Lei 	dm_io_acct(io, true);
5697465d7acSMike Snitzer }
570978e51baSMike Snitzer 
571978e51baSMike Snitzer static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio)
5721da177e4SLinus Torvalds {
57364f52b0eSMike Snitzer 	struct dm_io *io;
57464f52b0eSMike Snitzer 	struct dm_target_io *tio;
57564f52b0eSMike Snitzer 	struct bio *clone;
57664f52b0eSMike Snitzer 
57729dec90aSChristoph Hellwig 	clone = bio_alloc_clone(NULL, bio, GFP_NOIO, &md->mempools->io_bs);
5786c23f0bdSChristoph Hellwig 	tio = clone_to_tio(clone);
579655f3aadSMike Snitzer 	tio->flags = 0;
580655f3aadSMike Snitzer 	dm_tio_set_flag(tio, DM_TIO_INSIDE_DM_IO);
58164f52b0eSMike Snitzer 	tio->io = NULL;
58264f52b0eSMike Snitzer 
58364f52b0eSMike Snitzer 	io = container_of(tio, struct dm_io, tio);
58464f52b0eSMike Snitzer 	io->magic = DM_IO_MAGIC;
58584b98f4cSMike Snitzer 	io->status = BLK_STS_OK;
5860f14d60aSMing Lei 
5870f14d60aSMing Lei 	/* one ref is for submission, the other is for completion */
5880f14d60aSMing Lei 	atomic_set(&io->io_count, 2);
5899f6dc633SMike Snitzer 	this_cpu_inc(*md->pending_io);
5907dd76d1fSMing Lei 	io->orig_bio = bio;
591978e51baSMike Snitzer 	io->md = md;
5924d7bca13SMike Snitzer 	spin_lock_init(&io->lock);
593b879f915SMike Snitzer 	io->start_time = jiffies;
59482f6cdccSMike Snitzer 	io->flags = 0;
59564f52b0eSMike Snitzer 
596442761fdSMike Snitzer 	if (static_branch_unlikely(&stats_enabled))
5970cdb90f0SMike Snitzer 		dm_stats_record_start(&md->stats, &io->stats_aux);
59864f52b0eSMike Snitzer 
59964f52b0eSMike Snitzer 	return io;
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds 
6020119ab14SMike Snitzer static void free_io(struct dm_io *io)
6031da177e4SLinus Torvalds {
60464f52b0eSMike Snitzer 	bio_put(&io->tio.clone);
60564f52b0eSMike Snitzer }
60664f52b0eSMike Snitzer 
6071d1068ceSChristoph Hellwig static struct bio *alloc_tio(struct clone_info *ci, struct dm_target *ti,
60886a3238cSHeinz Mauelshagen 			     unsigned int target_bio_nr, unsigned int *len, gfp_t gfp_mask)
60964f52b0eSMike Snitzer {
6109dd1cd32SMike Snitzer 	struct mapped_device *md = ci->io->md;
61164f52b0eSMike Snitzer 	struct dm_target_io *tio;
612018b05ebSMike Snitzer 	struct bio *clone;
61364f52b0eSMike Snitzer 
61464f52b0eSMike Snitzer 	if (!ci->io->tio.io) {
61564f52b0eSMike Snitzer 		/* the dm_target_io embedded in ci->io is available */
61664f52b0eSMike Snitzer 		tio = &ci->io->tio;
617018b05ebSMike Snitzer 		/* alloc_io() already initialized embedded clone */
618018b05ebSMike Snitzer 		clone = &tio->clone;
61964f52b0eSMike Snitzer 	} else {
62029dec90aSChristoph Hellwig 		clone = bio_alloc_clone(NULL, ci->bio, gfp_mask,
62129dec90aSChristoph Hellwig 					&md->mempools->bs);
62264f52b0eSMike Snitzer 		if (!clone)
62364f52b0eSMike Snitzer 			return NULL;
62464f52b0eSMike Snitzer 
625b99fdcdcSMing Lei 		/* REQ_DM_POLL_LIST shouldn't be inherited */
626b99fdcdcSMing Lei 		clone->bi_opf &= ~REQ_DM_POLL_LIST;
627b99fdcdcSMing Lei 
6286c23f0bdSChristoph Hellwig 		tio = clone_to_tio(clone);
629655f3aadSMike Snitzer 		tio->flags = 0; /* also clears DM_TIO_INSIDE_DM_IO */
63064f52b0eSMike Snitzer 	}
63164f52b0eSMike Snitzer 
63264f52b0eSMike Snitzer 	tio->magic = DM_TIO_MAGIC;
63364f52b0eSMike Snitzer 	tio->io = ci->io;
63464f52b0eSMike Snitzer 	tio->ti = ti;
63564f52b0eSMike Snitzer 	tio->target_bio_nr = target_bio_nr;
636dc8e2021SChristoph Hellwig 	tio->len_ptr = len;
637743598f0SMike Snitzer 	tio->old_sector = 0;
63864f52b0eSMike Snitzer 
6399dd1cd32SMike Snitzer 	/* Set default bdev, but target must bio_set_dev() before issuing IO */
6409dd1cd32SMike Snitzer 	clone->bi_bdev = md->disk->part0;
6419dd1cd32SMike Snitzer 	if (unlikely(ti->needs_bio_set_dev))
6429dd1cd32SMike Snitzer 		bio_set_dev(clone, md->disk->part0);
6439dd1cd32SMike 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,
714a3282b43SBart Van Assche 					int *srcu_idx, blk_opf_t 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,
723a3282b43SBart Van Assche 					 blk_opf_t 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  */
736b9a785d2SChristoph Hellwig static struct table_device *open_table_device(struct mapped_device *md,
737b9a785d2SChristoph Hellwig 		dev_t dev, fmode_t mode)
73886f1152bSBenjamin Marzinski {
739b9a785d2SChristoph Hellwig 	struct table_device *td;
74086f1152bSBenjamin Marzinski 	struct block_device *bdev;
741cd913c76SChristoph Hellwig 	u64 part_off;
74286f1152bSBenjamin Marzinski 	int r;
74386f1152bSBenjamin Marzinski 
744b9a785d2SChristoph Hellwig 	td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
745b9a785d2SChristoph Hellwig 	if (!td)
746b9a785d2SChristoph Hellwig 		return ERR_PTR(-ENOMEM);
747b9a785d2SChristoph Hellwig 	refcount_set(&td->count, 1);
74886f1152bSBenjamin Marzinski 
749b9a785d2SChristoph Hellwig 	bdev = blkdev_get_by_dev(dev, mode | FMODE_EXCL, _dm_claim_ptr);
750b9a785d2SChristoph Hellwig 	if (IS_ERR(bdev)) {
751b9a785d2SChristoph Hellwig 		r = PTR_ERR(bdev);
752b9a785d2SChristoph Hellwig 		goto out_free_td;
75386f1152bSBenjamin Marzinski 	}
75486f1152bSBenjamin Marzinski 
7551a581b72SChristoph Hellwig 	/*
7561a581b72SChristoph Hellwig 	 * We can be called before the dm disk is added.  In that case we can't
7571a581b72SChristoph Hellwig 	 * register the holder relation here.  It will be done once add_disk was
7581a581b72SChristoph Hellwig 	 * called.
7591a581b72SChristoph Hellwig 	 */
7601a581b72SChristoph Hellwig 	if (md->disk->slave_dir) {
7611a581b72SChristoph Hellwig 		r = bd_link_disk_holder(bdev, md->disk);
762b9a785d2SChristoph Hellwig 		if (r)
763b9a785d2SChristoph Hellwig 			goto out_blkdev_put;
7641a581b72SChristoph Hellwig 	}
765b9a785d2SChristoph Hellwig 
766b9a785d2SChristoph Hellwig 	td->dm_dev.mode = mode;
76786f1152bSBenjamin Marzinski 	td->dm_dev.bdev = bdev;
7688012b866SShiyang Ruan 	td->dm_dev.dax_dev = fs_dax_get_by_bdev(bdev, &part_off, NULL, NULL);
769b9a785d2SChristoph Hellwig 	format_dev_t(td->dm_dev.name, dev);
770b9a785d2SChristoph Hellwig 	list_add(&td->list, &md->table_devices);
771b9a785d2SChristoph Hellwig 	return td;
772b9a785d2SChristoph Hellwig 
773b9a785d2SChristoph Hellwig out_blkdev_put:
774b9a785d2SChristoph Hellwig 	blkdev_put(bdev, mode | FMODE_EXCL);
775b9a785d2SChristoph Hellwig out_free_td:
776b9a785d2SChristoph Hellwig 	kfree(td);
777b9a785d2SChristoph Hellwig 	return ERR_PTR(r);
77886f1152bSBenjamin Marzinski }
77986f1152bSBenjamin Marzinski 
78086f1152bSBenjamin Marzinski /*
78186f1152bSBenjamin Marzinski  * Close a table device that we've been using.
78286f1152bSBenjamin Marzinski  */
78386f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md)
78486f1152bSBenjamin Marzinski {
7851a581b72SChristoph Hellwig 	if (md->disk->slave_dir)
7861a581b72SChristoph Hellwig 		bd_unlink_disk_holder(td->dm_dev.bdev, md->disk);
78786f1152bSBenjamin Marzinski 	blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
788817bf402SDan Williams 	put_dax(td->dm_dev.dax_dev);
7897b586583SChristoph Hellwig 	list_del(&td->list);
7907b586583SChristoph Hellwig 	kfree(td);
79186f1152bSBenjamin Marzinski }
79286f1152bSBenjamin Marzinski 
79386f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev,
7948454fca4SSheetal Singala 					      fmode_t mode)
7958454fca4SSheetal Singala {
79686f1152bSBenjamin Marzinski 	struct table_device *td;
79786f1152bSBenjamin Marzinski 
79886f1152bSBenjamin Marzinski 	list_for_each_entry(td, l, list)
79986f1152bSBenjamin Marzinski 		if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
80086f1152bSBenjamin Marzinski 			return td;
80186f1152bSBenjamin Marzinski 
80286f1152bSBenjamin Marzinski 	return NULL;
80386f1152bSBenjamin Marzinski }
80486f1152bSBenjamin Marzinski 
80586f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
8068454fca4SSheetal Singala 			struct dm_dev **result)
8078454fca4SSheetal Singala {
80886f1152bSBenjamin Marzinski 	struct table_device *td;
80986f1152bSBenjamin Marzinski 
81086f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
81186f1152bSBenjamin Marzinski 	td = find_table_device(&md->table_devices, dev, mode);
81286f1152bSBenjamin Marzinski 	if (!td) {
813b9a785d2SChristoph Hellwig 		td = open_table_device(md, dev, mode);
814b9a785d2SChristoph Hellwig 		if (IS_ERR(td)) {
81586f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
816b9a785d2SChristoph Hellwig 			return PTR_ERR(td);
81786f1152bSBenjamin Marzinski 		}
818b0b4d7c6SElena Reshetova 	} else {
819b0b4d7c6SElena Reshetova 		refcount_inc(&td->count);
82086f1152bSBenjamin Marzinski 	}
82186f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
82286f1152bSBenjamin Marzinski 
82386f1152bSBenjamin Marzinski 	*result = &td->dm_dev;
82486f1152bSBenjamin Marzinski 	return 0;
82586f1152bSBenjamin Marzinski }
82686f1152bSBenjamin Marzinski 
82786f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
82886f1152bSBenjamin Marzinski {
82986f1152bSBenjamin Marzinski 	struct table_device *td = container_of(d, struct table_device, dm_dev);
83086f1152bSBenjamin Marzinski 
83186f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
8327b586583SChristoph Hellwig 	if (refcount_dec_and_test(&td->count))
83386f1152bSBenjamin Marzinski 		close_table_device(td, md);
83486f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
83586f1152bSBenjamin Marzinski }
83686f1152bSBenjamin Marzinski 
83786f1152bSBenjamin Marzinski /*
8383ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
8393ac51e74SDarrick J. Wong  */
8403ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
8413ac51e74SDarrick J. Wong {
8423ac51e74SDarrick J. Wong 	*geo = md->geometry;
8433ac51e74SDarrick J. Wong 
8443ac51e74SDarrick J. Wong 	return 0;
8453ac51e74SDarrick J. Wong }
8463ac51e74SDarrick J. Wong 
8473ac51e74SDarrick J. Wong /*
8483ac51e74SDarrick J. Wong  * Set the geometry of a device.
8493ac51e74SDarrick J. Wong  */
8503ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
8513ac51e74SDarrick J. Wong {
8523ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
8533ac51e74SDarrick J. Wong 
8543ac51e74SDarrick J. Wong 	if (geo->start > sz) {
85543e6c111SMikulas Patocka 		DMERR("Start sector is beyond the geometry limits.");
8563ac51e74SDarrick J. Wong 		return -EINVAL;
8573ac51e74SDarrick J. Wong 	}
8583ac51e74SDarrick J. Wong 
8593ac51e74SDarrick J. Wong 	md->geometry = *geo;
8603ac51e74SDarrick J. Wong 
8613ac51e74SDarrick J. Wong 	return 0;
8623ac51e74SDarrick J. Wong }
8633ac51e74SDarrick J. Wong 
8642e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
8652e93ccc1SKiyoshi Ueda {
8662e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
8672e93ccc1SKiyoshi Ueda }
8682e93ccc1SKiyoshi Ueda 
8698b211aacSMing Lei static void dm_requeue_add_io(struct dm_io *io, bool first_stage)
8701da177e4SLinus Torvalds {
871b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
8722e93ccc1SKiyoshi Ueda 
8738b211aacSMing Lei 	if (first_stage) {
8748b211aacSMing Lei 		struct dm_io *next = md->requeue_list;
8758b211aacSMing Lei 
8768b211aacSMing Lei 		md->requeue_list = io;
8778b211aacSMing Lei 		io->next = next;
8788b211aacSMing Lei 	} else {
8798b211aacSMing Lei 		bio_list_add_head(&md->deferred, io->orig_bio);
8808b211aacSMing Lei 	}
8818b211aacSMing Lei }
8828b211aacSMing Lei 
8838b211aacSMing Lei static void dm_kick_requeue(struct mapped_device *md, bool first_stage)
8848b211aacSMing Lei {
8858b211aacSMing Lei 	if (first_stage)
8868b211aacSMing Lei 		queue_work(md->wq, &md->requeue_work);
8878b211aacSMing Lei 	else
8888b211aacSMing Lei 		queue_work(md->wq, &md->work);
8898b211aacSMing Lei }
8908b211aacSMing Lei 
8912e93ccc1SKiyoshi Ueda /*
892444fe04fSMing Lei  * Return true if the dm_io's original bio is requeued.
893444fe04fSMing Lei  * io->status is updated with error if requeue disallowed.
894444fe04fSMing Lei  */
8958b211aacSMing Lei static bool dm_handle_requeue(struct dm_io *io, bool first_stage)
896444fe04fSMing Lei {
8978b211aacSMing Lei 	struct bio *bio = io->orig_bio;
898444fe04fSMing Lei 	bool handle_requeue = (io->status == BLK_STS_DM_REQUEUE);
899444fe04fSMing Lei 	bool handle_polled_eagain = ((io->status == BLK_STS_AGAIN) &&
900444fe04fSMing Lei 				     (bio->bi_opf & REQ_POLLED));
901444fe04fSMing Lei 	struct mapped_device *md = io->md;
902444fe04fSMing Lei 	bool requeued = false;
903444fe04fSMing Lei 
904444fe04fSMing Lei 	if (handle_requeue || handle_polled_eagain) {
905444fe04fSMing Lei 		unsigned long flags;
906444fe04fSMing Lei 
907444fe04fSMing Lei 		if (bio->bi_opf & REQ_POLLED) {
908444fe04fSMing Lei 			/*
909444fe04fSMing Lei 			 * Upper layer won't help us poll split bio
910444fe04fSMing Lei 			 * (io->orig_bio may only reflect a subset of the
911444fe04fSMing Lei 			 * pre-split original) so clear REQ_POLLED.
912444fe04fSMing Lei 			 */
913444fe04fSMing Lei 			bio_clear_polled(bio);
914444fe04fSMing Lei 		}
915444fe04fSMing Lei 
916444fe04fSMing Lei 		/*
917444fe04fSMing Lei 		 * Target requested pushing back the I/O or
918444fe04fSMing Lei 		 * polled IO hit BLK_STS_AGAIN.
9192e93ccc1SKiyoshi Ueda 		 */
920022c2611SMikulas Patocka 		spin_lock_irqsave(&md->deferred_lock, flags);
921444fe04fSMing Lei 		if ((__noflush_suspending(md) &&
922444fe04fSMing Lei 		     !WARN_ON_ONCE(dm_is_zone_write(md, bio))) ||
9238b211aacSMing Lei 		    handle_polled_eagain || first_stage) {
9248b211aacSMing Lei 			dm_requeue_add_io(io, first_stage);
925444fe04fSMing Lei 			requeued = true;
926bf14e2b2SDamien Le Moal 		} else {
927bf14e2b2SDamien Le Moal 			/*
928bf14e2b2SDamien Le Moal 			 * noflush suspend was interrupted or this is
929bf14e2b2SDamien Le Moal 			 * a write to a zoned target.
930bf14e2b2SDamien Le Moal 			 */
9314e4cbee9SChristoph Hellwig 			io->status = BLK_STS_IOERR;
932bf14e2b2SDamien Le Moal 		}
933022c2611SMikulas Patocka 		spin_unlock_irqrestore(&md->deferred_lock, flags);
9342e93ccc1SKiyoshi Ueda 	}
9352e93ccc1SKiyoshi Ueda 
936444fe04fSMing Lei 	if (requeued)
9378b211aacSMing Lei 		dm_kick_requeue(md, first_stage);
938444fe04fSMing Lei 
939444fe04fSMing Lei 	return requeued;
940444fe04fSMing Lei }
941444fe04fSMing Lei 
9428b211aacSMing Lei static void __dm_io_complete(struct dm_io *io, bool first_stage)
943444fe04fSMing Lei {
9448b211aacSMing Lei 	struct bio *bio = io->orig_bio;
945444fe04fSMing Lei 	struct mapped_device *md = io->md;
946444fe04fSMing Lei 	blk_status_t io_error;
947444fe04fSMing Lei 	bool requeued;
948444fe04fSMing Lei 
9498b211aacSMing Lei 	requeued = dm_handle_requeue(io, first_stage);
9508b211aacSMing Lei 	if (requeued && first_stage)
9518b211aacSMing Lei 		return;
952444fe04fSMing Lei 
9534e4cbee9SChristoph Hellwig 	io_error = io->status;
95482f6cdccSMike Snitzer 	if (dm_io_flagged(io, DM_IO_ACCOUNTED))
955b992b40dSMing Lei 		dm_end_io_acct(io);
9560fbb4d93SMike Snitzer 	else if (!io_error) {
9570fbb4d93SMike Snitzer 		/*
9580fbb4d93SMike Snitzer 		 * Must handle target that DM_MAPIO_SUBMITTED only to
9590fbb4d93SMike Snitzer 		 * then bio_endio() rather than dm_submit_bio_remap()
9600fbb4d93SMike Snitzer 		 */
961b992b40dSMing Lei 		__dm_start_io_acct(io);
962b992b40dSMing Lei 		dm_end_io_acct(io);
9630fbb4d93SMike Snitzer 	}
9640119ab14SMike Snitzer 	free_io(io);
9659f6dc633SMike Snitzer 	smp_wmb();
9669f6dc633SMike Snitzer 	this_cpu_dec(*md->pending_io);
9672056a782SJens Axboe 
9689f6dc633SMike Snitzer 	/* nudge anyone waiting on suspend queue */
9699f6dc633SMike Snitzer 	if (unlikely(wq_has_sleeper(&md->wait)))
9709f6dc633SMike Snitzer 		wake_up(&md->wait);
9711da177e4SLinus Torvalds 
972444fe04fSMing Lei 	/* Return early if the original bio was requeued */
973444fe04fSMing Lei 	if (requeued)
97478ccef91SMike Snitzer 		return;
9756a8736d1STejun Heo 
9768d394bc4SMike Snitzer 	if (bio_is_flush_with_data(bio)) {
977af7e466aSMikulas Patocka 		/*
9786a8736d1STejun Heo 		 * Preflush done for flush with data, reissue
97928a8f0d3SMike Christie 		 * without REQ_PREFLUSH.
980af7e466aSMikulas Patocka 		 */
9811eff9d32SJens Axboe 		bio->bi_opf &= ~REQ_PREFLUSH;
9826a8736d1STejun Heo 		queue_io(md, bio);
983af7e466aSMikulas Patocka 	} else {
984b372d360SMike Snitzer 		/* done with normal IO or empty flush */
9858dd601faSNeilBrown 		if (io_error)
9864e4cbee9SChristoph Hellwig 			bio->bi_status = io_error;
9874246a0b6SChristoph Hellwig 		bio_endio(bio);
9882e93ccc1SKiyoshi Ueda 	}
9891da177e4SLinus Torvalds }
990e2736347SMike Snitzer 
9918b211aacSMing Lei static void dm_wq_requeue_work(struct work_struct *work)
9928b211aacSMing Lei {
9938b211aacSMing Lei 	struct mapped_device *md = container_of(work, struct mapped_device,
9948b211aacSMing Lei 						requeue_work);
9958b211aacSMing Lei 	unsigned long flags;
9968b211aacSMing Lei 	struct dm_io *io;
9978b211aacSMing Lei 
9988b211aacSMing Lei 	/* reuse deferred lock to simplify dm_handle_requeue */
9998b211aacSMing Lei 	spin_lock_irqsave(&md->deferred_lock, flags);
10008b211aacSMing Lei 	io = md->requeue_list;
10018b211aacSMing Lei 	md->requeue_list = NULL;
10028b211aacSMing Lei 	spin_unlock_irqrestore(&md->deferred_lock, flags);
10038b211aacSMing Lei 
10048b211aacSMing Lei 	while (io) {
10058b211aacSMing Lei 		struct dm_io *next = io->next;
10068b211aacSMing Lei 
100746754bd0SChristoph Hellwig 		dm_io_rewind(io, &md->disk->bio_split);
10088b211aacSMing Lei 
10098b211aacSMing Lei 		io->next = NULL;
10108b211aacSMing Lei 		__dm_io_complete(io, false);
10118b211aacSMing Lei 		io = next;
1012f77692d6SMike Snitzer 		cond_resched();
10138b211aacSMing Lei 	}
10148b211aacSMing Lei }
10158b211aacSMing Lei 
10168b211aacSMing Lei /*
10178b211aacSMing Lei  * Two staged requeue:
10188b211aacSMing Lei  *
10198b211aacSMing Lei  * 1) io->orig_bio points to the real original bio, and the part mapped to
10208b211aacSMing Lei  *    this io must be requeued, instead of other parts of the original bio.
10218b211aacSMing Lei  *
10228b211aacSMing Lei  * 2) io->orig_bio points to new cloned bio which matches the requeued dm_io.
10238b211aacSMing Lei  */
10248b211aacSMing Lei static void dm_io_complete(struct dm_io *io)
10258b211aacSMing Lei {
10268b211aacSMing Lei 	bool first_requeue;
10278b211aacSMing Lei 
10288b211aacSMing Lei 	/*
10298b211aacSMing Lei 	 * Only dm_io that has been split needs two stage requeue, otherwise
10308b211aacSMing Lei 	 * we may run into long bio clone chain during suspend and OOM could
10318b211aacSMing Lei 	 * be triggered.
10328b211aacSMing Lei 	 *
10338b211aacSMing Lei 	 * Also flush data dm_io won't be marked as DM_IO_WAS_SPLIT, so they
10348b211aacSMing Lei 	 * also aren't handled via the first stage requeue.
10358b211aacSMing Lei 	 */
10368b211aacSMing Lei 	if (dm_io_flagged(io, DM_IO_WAS_SPLIT))
10378b211aacSMing Lei 		first_requeue = true;
10388b211aacSMing Lei 	else
10398b211aacSMing Lei 		first_requeue = false;
10408b211aacSMing Lei 
10418b211aacSMing Lei 	__dm_io_complete(io, first_requeue);
10428b211aacSMing Lei }
10438b211aacSMing Lei 
1044e2736347SMike Snitzer /*
1045e2736347SMike Snitzer  * Decrements the number of outstanding ios that a bio has been
1046e2736347SMike Snitzer  * cloned into, completing the original io if necc.
1047e2736347SMike Snitzer  */
104884b98f4cSMike Snitzer static inline void __dm_io_dec_pending(struct dm_io *io)
1049e2736347SMike Snitzer {
105084b98f4cSMike Snitzer 	if (atomic_dec_and_test(&io->io_count))
105184b98f4cSMike Snitzer 		dm_io_complete(io);
105284b98f4cSMike Snitzer }
105384b98f4cSMike Snitzer 
105484b98f4cSMike Snitzer static void dm_io_set_error(struct dm_io *io, blk_status_t error)
105584b98f4cSMike Snitzer {
1056e2736347SMike Snitzer 	unsigned long flags;
105784b98f4cSMike Snitzer 
105884b98f4cSMike Snitzer 	/* Push-back supersedes any I/O errors */
10594d7bca13SMike Snitzer 	spin_lock_irqsave(&io->lock, flags);
1060e2736347SMike Snitzer 	if (!(io->status == BLK_STS_DM_REQUEUE &&
106184b98f4cSMike Snitzer 	      __noflush_suspending(io->md))) {
1062e2736347SMike Snitzer 		io->status = error;
106384b98f4cSMike Snitzer 	}
10644d7bca13SMike Snitzer 	spin_unlock_irqrestore(&io->lock, flags);
1065e2736347SMike Snitzer }
1066e2736347SMike Snitzer 
10672e803cd9SMing Lei static void dm_io_dec_pending(struct dm_io *io, blk_status_t error)
106884b98f4cSMike Snitzer {
106984b98f4cSMike Snitzer 	if (unlikely(error))
107084b98f4cSMike Snitzer 		dm_io_set_error(io, error);
107184b98f4cSMike Snitzer 
107284b98f4cSMike Snitzer 	__dm_io_dec_pending(io);
1073af7e466aSMikulas Patocka }
10741da177e4SLinus Torvalds 
1075bcb44433SMike Snitzer void disable_discard(struct mapped_device *md)
1076bcb44433SMike Snitzer {
1077bcb44433SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
1078bcb44433SMike Snitzer 
1079bcb44433SMike Snitzer 	/* device doesn't really support DISCARD, disable it */
1080bcb44433SMike Snitzer 	limits->max_discard_sectors = 0;
1081bcb44433SMike Snitzer }
1082bcb44433SMike Snitzer 
1083ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md)
1084ac62d620SChristoph Hellwig {
1085ac62d620SChristoph Hellwig 	struct queue_limits *limits = dm_get_queue_limits(md);
1086ac62d620SChristoph Hellwig 
1087ac62d620SChristoph Hellwig 	/* device doesn't really support WRITE ZEROES, disable it */
1088ac62d620SChristoph Hellwig 	limits->max_write_zeroes_sectors = 0;
1089ac62d620SChristoph Hellwig }
1090ac62d620SChristoph Hellwig 
1091a666e5c0SMikulas Patocka static bool swap_bios_limit(struct dm_target *ti, struct bio *bio)
1092a666e5c0SMikulas Patocka {
1093a666e5c0SMikulas Patocka 	return unlikely((bio->bi_opf & REQ_SWAP) != 0) && unlikely(ti->limit_swap_bios);
1094a666e5c0SMikulas Patocka }
1095a666e5c0SMikulas Patocka 
10964246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio)
10971da177e4SLinus Torvalds {
10984e4cbee9SChristoph Hellwig 	blk_status_t error = bio->bi_status;
10996cbce280SMike Snitzer 	struct dm_target_io *tio = clone_to_tio(bio);
11006cbce280SMike Snitzer 	struct dm_target *ti = tio->ti;
11016cbce280SMike Snitzer 	dm_endio_fn endio = ti->type->end_io;
11026cbce280SMike Snitzer 	struct dm_io *io = tio->io;
11036cbce280SMike Snitzer 	struct mapped_device *md = io->md;
11041da177e4SLinus Torvalds 
11059c37de29SMike Snitzer 	if (unlikely(error == BLK_STS_TARGET)) {
1106bcb44433SMike Snitzer 		if (bio_op(bio) == REQ_OP_DISCARD &&
110770200574SChristoph Hellwig 		    !bdev_max_discard_sectors(bio->bi_bdev))
1108bcb44433SMike Snitzer 			disable_discard(md);
1109bcb44433SMike Snitzer 		else if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
1110dddf3056SMike Snitzer 			 !bdev_write_zeroes_sectors(bio->bi_bdev))
1111ac62d620SChristoph Hellwig 			disable_write_zeroes(md);
1112ac62d620SChristoph Hellwig 	}
11137eee4ae2SMike Snitzer 
1114442761fdSMike Snitzer 	if (static_branch_unlikely(&zoned_enabled) &&
1115edd1dbc8SChristoph Hellwig 	    unlikely(bdev_is_zoned(bio->bi_bdev)))
1116bb37d772SDamien Le Moal 		dm_zone_endio(io, bio);
1117415c79e1SJohannes Thumshirn 
11181be56909SChristoph Hellwig 	if (endio) {
11196cbce280SMike Snitzer 		int r = endio(ti, bio, &error);
1120b30f1607SHeinz Mauelshagen 
11211be56909SChristoph Hellwig 		switch (r) {
11221be56909SChristoph Hellwig 		case DM_ENDIO_REQUEUE:
1123442761fdSMike Snitzer 			if (static_branch_unlikely(&zoned_enabled)) {
1124bf14e2b2SDamien Le Moal 				/*
1125bf14e2b2SDamien Le Moal 				 * Requeuing writes to a sequential zone of a zoned
1126bf14e2b2SDamien Le Moal 				 * target will break the sequential write pattern:
1127bf14e2b2SDamien Le Moal 				 * fail such IO.
1128bf14e2b2SDamien Le Moal 				 */
1129bf14e2b2SDamien Le Moal 				if (WARN_ON_ONCE(dm_is_zone_write(md, bio)))
1130bf14e2b2SDamien Le Moal 					error = BLK_STS_IOERR;
1131bf14e2b2SDamien Le Moal 				else
11324e4cbee9SChristoph Hellwig 					error = BLK_STS_DM_REQUEUE;
1133442761fdSMike Snitzer 			} else
1134442761fdSMike Snitzer 				error = BLK_STS_DM_REQUEUE;
1135df561f66SGustavo A. R. Silva 			fallthrough;
11361be56909SChristoph Hellwig 		case DM_ENDIO_DONE:
11371be56909SChristoph Hellwig 			break;
11381be56909SChristoph Hellwig 		case DM_ENDIO_INCOMPLETE:
11391be56909SChristoph Hellwig 			/* The target will handle the io */
11401be56909SChristoph Hellwig 			return;
11411be56909SChristoph Hellwig 		default:
114243e6c111SMikulas Patocka 			DMCRIT("unimplemented target endio return value: %d", r);
11431be56909SChristoph Hellwig 			BUG();
11441be56909SChristoph Hellwig 		}
11451be56909SChristoph Hellwig 	}
11461be56909SChristoph Hellwig 
1147442761fdSMike Snitzer 	if (static_branch_unlikely(&swap_bios_enabled) &&
1148442761fdSMike Snitzer 	    unlikely(swap_bios_limit(ti, bio)))
1149a666e5c0SMikulas Patocka 		up(&md->swap_bios_semaphore);
1150a666e5c0SMikulas Patocka 
11511d1068ceSChristoph Hellwig 	free_tio(bio);
1152e2118b3cSDamien Le Moal 	dm_io_dec_pending(io, error);
11531da177e4SLinus Torvalds }
11541da177e4SLinus Torvalds 
115578d8e58aSMike Snitzer /*
115656a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
115756a67df7SMike Snitzer  * target boundary.
115856a67df7SMike Snitzer  */
11593720281dSMike Snitzer static inline sector_t max_io_len_target_boundary(struct dm_target *ti,
11603720281dSMike Snitzer 						  sector_t target_offset)
11611da177e4SLinus Torvalds {
116256a67df7SMike Snitzer 	return ti->len - target_offset;
116356a67df7SMike Snitzer }
116456a67df7SMike Snitzer 
11653720281dSMike Snitzer static sector_t max_io_len(struct dm_target *ti, sector_t sector)
116656a67df7SMike Snitzer {
11673720281dSMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
11683720281dSMike Snitzer 	sector_t len = max_io_len_target_boundary(ti, target_offset);
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	/*
11713ee16db3SMike Snitzer 	 * Does the target need to split IO even further?
11723ee16db3SMike Snitzer 	 * - varied (per target) IO splitting is a tenet of DM; this
11733ee16db3SMike Snitzer 	 *   explains why stacked chunk_sectors based splitting via
11745a97806fSChristoph Hellwig 	 *   bio_split_to_limits() isn't possible here.
11751da177e4SLinus Torvalds 	 */
1176c3949322SChristoph Hellwig 	if (!ti->max_io_len)
11771da177e4SLinus Torvalds 		return len;
1178c3949322SChristoph Hellwig 	return min_t(sector_t, len,
1179c3949322SChristoph Hellwig 		min(queue_max_sectors(ti->table->md->queue),
1180c3949322SChristoph Hellwig 		    blk_chunk_sectors_left(target_offset, ti->max_io_len)));
11811da177e4SLinus Torvalds }
11821da177e4SLinus Torvalds 
1183542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1184542f9038SMike Snitzer {
1185542f9038SMike Snitzer 	if (len > UINT_MAX) {
1186542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1187542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
1188542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
1189542f9038SMike Snitzer 		return -EINVAL;
1190542f9038SMike Snitzer 	}
1191542f9038SMike Snitzer 
119275ae1936SMikulas Patocka 	ti->max_io_len = (uint32_t) len;
1193542f9038SMike Snitzer 
1194542f9038SMike Snitzer 	return 0;
1195542f9038SMike Snitzer }
1196542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1197542f9038SMike Snitzer 
1198f26c5719SDan Williams static struct dm_target *dm_dax_get_live_target(struct mapped_device *md,
1199f26c5719SDan Williams 						sector_t sector, int *srcu_idx)
12003d97c829SMike Snitzer 	__acquires(md->io_barrier)
1201545ed20eSToshi Kani {
1202545ed20eSToshi Kani 	struct dm_table *map;
1203545ed20eSToshi Kani 	struct dm_target *ti;
1204545ed20eSToshi Kani 
1205f26c5719SDan Williams 	map = dm_get_live_table(md, srcu_idx);
1206545ed20eSToshi Kani 	if (!map)
1207f26c5719SDan Williams 		return NULL;
1208545ed20eSToshi Kani 
1209545ed20eSToshi Kani 	ti = dm_table_find_target(map, sector);
1210123d87d5SMikulas Patocka 	if (!ti)
1211f26c5719SDan Williams 		return NULL;
1212f26c5719SDan Williams 
1213f26c5719SDan Williams 	return ti;
1214f26c5719SDan Williams }
1215f26c5719SDan Williams 
1216f26c5719SDan Williams static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
1217e511c4a3SJane Chu 		long nr_pages, enum dax_access_mode mode, void **kaddr,
1218e511c4a3SJane Chu 		pfn_t *pfn)
1219f26c5719SDan Williams {
1220f26c5719SDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
1221f26c5719SDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
1222f26c5719SDan Williams 	struct dm_target *ti;
1223f26c5719SDan Williams 	long len, ret = -EIO;
1224f26c5719SDan Williams 	int srcu_idx;
1225f26c5719SDan Williams 
1226f26c5719SDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1227f26c5719SDan Williams 
1228f26c5719SDan Williams 	if (!ti)
1229545ed20eSToshi Kani 		goto out;
1230f26c5719SDan Williams 	if (!ti->type->direct_access)
1231f26c5719SDan Williams 		goto out;
12323720281dSMike Snitzer 	len = max_io_len(ti, sector) / PAGE_SECTORS;
1233f26c5719SDan Williams 	if (len < 1)
1234f26c5719SDan Williams 		goto out;
1235f26c5719SDan Williams 	nr_pages = min(len, nr_pages);
1236e511c4a3SJane Chu 	ret = ti->type->direct_access(ti, pgoff, nr_pages, mode, kaddr, pfn);
1237817bf402SDan Williams 
1238545ed20eSToshi Kani  out:
1239545ed20eSToshi Kani 	dm_put_live_table(md, srcu_idx);
1240f26c5719SDan Williams 
1241f26c5719SDan Williams 	return ret;
1242545ed20eSToshi Kani }
1243545ed20eSToshi Kani 
1244cdf6cdcdSVivek Goyal static int dm_dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
1245cdf6cdcdSVivek Goyal 				  size_t nr_pages)
1246cdf6cdcdSVivek Goyal {
1247cdf6cdcdSVivek Goyal 	struct mapped_device *md = dax_get_private(dax_dev);
1248cdf6cdcdSVivek Goyal 	sector_t sector = pgoff * PAGE_SECTORS;
1249cdf6cdcdSVivek Goyal 	struct dm_target *ti;
1250cdf6cdcdSVivek Goyal 	int ret = -EIO;
1251cdf6cdcdSVivek Goyal 	int srcu_idx;
1252cdf6cdcdSVivek Goyal 
1253cdf6cdcdSVivek Goyal 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1254cdf6cdcdSVivek Goyal 
1255cdf6cdcdSVivek Goyal 	if (!ti)
1256cdf6cdcdSVivek Goyal 		goto out;
1257cdf6cdcdSVivek Goyal 	if (WARN_ON(!ti->type->dax_zero_page_range)) {
1258cdf6cdcdSVivek Goyal 		/*
1259cdf6cdcdSVivek Goyal 		 * ->zero_page_range() is mandatory dax operation. If we are
1260cdf6cdcdSVivek Goyal 		 *  here, something is wrong.
1261cdf6cdcdSVivek Goyal 		 */
1262cdf6cdcdSVivek Goyal 		goto out;
1263cdf6cdcdSVivek Goyal 	}
1264cdf6cdcdSVivek Goyal 	ret = ti->type->dax_zero_page_range(ti, pgoff, nr_pages);
1265cdf6cdcdSVivek Goyal  out:
1266cdf6cdcdSVivek Goyal 	dm_put_live_table(md, srcu_idx);
1267cdf6cdcdSVivek Goyal 
1268cdf6cdcdSVivek Goyal 	return ret;
1269cdf6cdcdSVivek Goyal }
1270cdf6cdcdSVivek Goyal 
1271047218ecSJane Chu static size_t dm_dax_recovery_write(struct dax_device *dax_dev, pgoff_t pgoff,
1272047218ecSJane Chu 		void *addr, size_t bytes, struct iov_iter *i)
1273047218ecSJane Chu {
1274047218ecSJane Chu 	struct mapped_device *md = dax_get_private(dax_dev);
1275047218ecSJane Chu 	sector_t sector = pgoff * PAGE_SECTORS;
1276047218ecSJane Chu 	struct dm_target *ti;
1277047218ecSJane Chu 	int srcu_idx;
1278047218ecSJane Chu 	long ret = 0;
1279047218ecSJane Chu 
1280047218ecSJane Chu 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1281047218ecSJane Chu 	if (!ti || !ti->type->dax_recovery_write)
1282047218ecSJane Chu 		goto out;
1283047218ecSJane Chu 
1284047218ecSJane Chu 	ret = ti->type->dax_recovery_write(ti, pgoff, addr, bytes, i);
1285047218ecSJane Chu out:
1286047218ecSJane Chu 	dm_put_live_table(md, srcu_idx);
1287047218ecSJane Chu 	return ret;
1288047218ecSJane Chu }
1289047218ecSJane Chu 
12901dd40c3eSMikulas Patocka /*
12911dd40c3eSMikulas Patocka  * A target may call dm_accept_partial_bio only from the map routine.  It is
12926842d264SDamien Le Moal  * allowed for all bio types except REQ_PREFLUSH, REQ_OP_ZONE_* zone management
1293e6fc9f62SMike Snitzer  * operations, REQ_OP_ZONE_APPEND (zone append writes) and any bio serviced by
1294e6fc9f62SMike Snitzer  * __send_duplicate_bios().
12951dd40c3eSMikulas Patocka  *
12961dd40c3eSMikulas Patocka  * dm_accept_partial_bio informs the dm that the target only wants to process
12971dd40c3eSMikulas Patocka  * additional n_sectors sectors of the bio and the rest of the data should be
12981dd40c3eSMikulas Patocka  * sent in a next bio.
12991dd40c3eSMikulas Patocka  *
13001dd40c3eSMikulas Patocka  * A diagram that explains the arithmetics:
13011dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
13021dd40c3eSMikulas Patocka  * |         1          |       2       |   3   |
13031dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
13041dd40c3eSMikulas Patocka  *
13051dd40c3eSMikulas Patocka  * <-------------- *tio->len_ptr --------------->
1306bdb34759SMike Snitzer  *                      <----- bio_sectors ----->
13071dd40c3eSMikulas Patocka  *                      <-- n_sectors -->
13081dd40c3eSMikulas Patocka  *
13091dd40c3eSMikulas Patocka  * Region 1 was already iterated over with bio_advance or similar function.
13101dd40c3eSMikulas Patocka  *	(it may be empty if the target doesn't use bio_advance)
13111dd40c3eSMikulas Patocka  * Region 2 is the remaining bio size that the target wants to process.
13121dd40c3eSMikulas Patocka  *	(it may be empty if region 1 is non-empty, although there is no reason
13131dd40c3eSMikulas Patocka  *	 to make it empty)
13141dd40c3eSMikulas Patocka  * The target requires that region 3 is to be sent in the next bio.
13151dd40c3eSMikulas Patocka  *
13161dd40c3eSMikulas Patocka  * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
13171dd40c3eSMikulas Patocka  * the partially processed part (the sum of regions 1+2) must be the same for all
13181dd40c3eSMikulas Patocka  * copies of the bio.
13191dd40c3eSMikulas Patocka  */
132086a3238cSHeinz Mauelshagen void dm_accept_partial_bio(struct bio *bio, unsigned int n_sectors)
13211dd40c3eSMikulas Patocka {
13226c23f0bdSChristoph Hellwig 	struct dm_target_io *tio = clone_to_tio(bio);
13238b211aacSMing Lei 	struct dm_io *io = tio->io;
132486a3238cSHeinz Mauelshagen 	unsigned int bio_sectors = bio_sectors(bio);
13256842d264SDamien Le Moal 
1326655f3aadSMike Snitzer 	BUG_ON(dm_tio_flagged(tio, DM_TIO_IS_DUPLICATE_BIO));
13276842d264SDamien Le Moal 	BUG_ON(op_is_zone_mgmt(bio_op(bio)));
13286842d264SDamien Le Moal 	BUG_ON(bio_op(bio) == REQ_OP_ZONE_APPEND);
1329bdb34759SMike Snitzer 	BUG_ON(bio_sectors > *tio->len_ptr);
1330bdb34759SMike Snitzer 	BUG_ON(n_sectors > bio_sectors);
13316842d264SDamien Le Moal 
1332bdb34759SMike Snitzer 	*tio->len_ptr -= bio_sectors - n_sectors;
13331dd40c3eSMikulas Patocka 	bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
13347dd76d1fSMing Lei 
13357dd76d1fSMing Lei 	/*
13367dd76d1fSMing Lei 	 * __split_and_process_bio() may have already saved mapped part
13377dd76d1fSMing Lei 	 * for accounting but it is being reduced so update accordingly.
13387dd76d1fSMing Lei 	 */
13398b211aacSMing Lei 	dm_io_set_flag(io, DM_IO_WAS_SPLIT);
13408b211aacSMing Lei 	io->sectors = n_sectors;
13418b211aacSMing Lei 	io->sector_offset = bio_sectors(io->orig_bio);
13421dd40c3eSMikulas Patocka }
13431dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
13441dd40c3eSMikulas Patocka 
13450fbb4d93SMike Snitzer /*
13460fbb4d93SMike Snitzer  * @clone: clone bio that DM core passed to target's .map function
13470fbb4d93SMike Snitzer  * @tgt_clone: clone of @clone bio that target needs submitted
13480fbb4d93SMike Snitzer  *
13490fbb4d93SMike Snitzer  * Targets should use this interface to submit bios they take
13500fbb4d93SMike Snitzer  * ownership of when returning DM_MAPIO_SUBMITTED.
13510fbb4d93SMike Snitzer  *
13520fbb4d93SMike Snitzer  * Target should also enable ti->accounts_remapped_io
13530fbb4d93SMike Snitzer  */
1354b7f8dff0SMike Snitzer void dm_submit_bio_remap(struct bio *clone, struct bio *tgt_clone)
13550fbb4d93SMike Snitzer {
13560fbb4d93SMike Snitzer 	struct dm_target_io *tio = clone_to_tio(clone);
13570fbb4d93SMike Snitzer 	struct dm_io *io = tio->io;
13580fbb4d93SMike Snitzer 
13590fbb4d93SMike Snitzer 	/* establish bio that will get submitted */
13600fbb4d93SMike Snitzer 	if (!tgt_clone)
13610fbb4d93SMike Snitzer 		tgt_clone = clone;
13620fbb4d93SMike Snitzer 
13630fbb4d93SMike Snitzer 	/*
13640fbb4d93SMike Snitzer 	 * Account io->origin_bio to DM dev on behalf of target
13650fbb4d93SMike Snitzer 	 * that took ownership of IO with DM_MAPIO_SUBMITTED.
13660fbb4d93SMike Snitzer 	 */
13670fbb4d93SMike Snitzer 	dm_start_io_acct(io, clone);
13680fbb4d93SMike Snitzer 
13699d20653fSMike Snitzer 	trace_block_bio_remap(tgt_clone, disk_devt(io->md->disk),
13700fbb4d93SMike Snitzer 			      tio->old_sector);
13719d20653fSMike Snitzer 	submit_bio_noacct(tgt_clone);
13720fbb4d93SMike Snitzer }
13730fbb4d93SMike Snitzer EXPORT_SYMBOL_GPL(dm_submit_bio_remap);
13740fbb4d93SMike Snitzer 
1375a666e5c0SMikulas Patocka static noinline void __set_swap_bios_limit(struct mapped_device *md, int latch)
1376a666e5c0SMikulas Patocka {
1377a666e5c0SMikulas Patocka 	mutex_lock(&md->swap_bios_lock);
1378a666e5c0SMikulas Patocka 	while (latch < md->swap_bios) {
1379a666e5c0SMikulas Patocka 		cond_resched();
1380a666e5c0SMikulas Patocka 		down(&md->swap_bios_semaphore);
1381a666e5c0SMikulas Patocka 		md->swap_bios--;
1382a666e5c0SMikulas Patocka 	}
1383a666e5c0SMikulas Patocka 	while (latch > md->swap_bios) {
1384a666e5c0SMikulas Patocka 		cond_resched();
1385a666e5c0SMikulas Patocka 		up(&md->swap_bios_semaphore);
1386a666e5c0SMikulas Patocka 		md->swap_bios++;
1387a666e5c0SMikulas Patocka 	}
1388a666e5c0SMikulas Patocka 	mutex_unlock(&md->swap_bios_lock);
1389a666e5c0SMikulas Patocka }
1390a666e5c0SMikulas Patocka 
13911561b396SChristoph Hellwig static void __map_bio(struct bio *clone)
13921da177e4SLinus Torvalds {
13931561b396SChristoph Hellwig 	struct dm_target_io *tio = clone_to_tio(clone);
1394bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
13956cbce280SMike Snitzer 	struct dm_io *io = tio->io;
13966cbce280SMike Snitzer 	struct mapped_device *md = io->md;
13976cbce280SMike Snitzer 	int r;
13981da177e4SLinus Torvalds 
13991da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 	/*
14020fbb4d93SMike Snitzer 	 * Map the clone.
14031da177e4SLinus Torvalds 	 */
1404743598f0SMike Snitzer 	tio->old_sector = clone->bi_iter.bi_sector;
1405d67a5f4bSMikulas Patocka 
1406442761fdSMike Snitzer 	if (static_branch_unlikely(&swap_bios_enabled) &&
1407442761fdSMike Snitzer 	    unlikely(swap_bios_limit(ti, clone))) {
1408a666e5c0SMikulas Patocka 		int latch = get_swap_bios();
1409b30f1607SHeinz Mauelshagen 
1410a666e5c0SMikulas Patocka 		if (unlikely(latch != md->swap_bios))
1411a666e5c0SMikulas Patocka 			__set_swap_bios_limit(md, latch);
1412a666e5c0SMikulas Patocka 		down(&md->swap_bios_semaphore);
1413a666e5c0SMikulas Patocka 	}
1414a666e5c0SMikulas Patocka 
1415442761fdSMike Snitzer 	if (static_branch_unlikely(&zoned_enabled)) {
1416bb37d772SDamien Le Moal 		/*
1417442761fdSMike Snitzer 		 * Check if the IO needs a special mapping due to zone append
1418442761fdSMike Snitzer 		 * emulation on zoned target. In this case, dm_zone_map_bio()
1419442761fdSMike Snitzer 		 * calls the target map operation.
1420bb37d772SDamien Le Moal 		 */
14216cbce280SMike Snitzer 		if (unlikely(dm_emulate_zone_append(md)))
1422bb37d772SDamien Le Moal 			r = dm_zone_map_bio(tio);
1423bb37d772SDamien Le Moal 		else
14247de3ee57SMikulas Patocka 			r = ti->type->map(ti, clone);
1425442761fdSMike Snitzer 	} else
1426442761fdSMike Snitzer 		r = ti->type->map(ti, clone);
1427bb37d772SDamien Le Moal 
1428846785e6SChristoph Hellwig 	switch (r) {
1429846785e6SChristoph Hellwig 	case DM_MAPIO_SUBMITTED:
14300fbb4d93SMike Snitzer 		/* target has assumed ownership of this io */
14310fbb4d93SMike Snitzer 		if (!ti->accounts_remapped_io)
14329d20653fSMike Snitzer 			dm_start_io_acct(io, clone);
1433846785e6SChristoph Hellwig 		break;
1434846785e6SChristoph Hellwig 	case DM_MAPIO_REMAPPED:
14359d20653fSMike Snitzer 		dm_submit_bio_remap(clone, NULL);
1436846785e6SChristoph Hellwig 		break;
1437846785e6SChristoph Hellwig 	case DM_MAPIO_KILL:
1438846785e6SChristoph Hellwig 	case DM_MAPIO_REQUEUE:
1439442761fdSMike Snitzer 		if (static_branch_unlikely(&swap_bios_enabled) &&
1440442761fdSMike Snitzer 		    unlikely(swap_bios_limit(ti, clone)))
14416cbce280SMike Snitzer 			up(&md->swap_bios_semaphore);
14421d1068ceSChristoph Hellwig 		free_tio(clone);
144390a2326eSMike Snitzer 		if (r == DM_MAPIO_KILL)
144490a2326eSMike Snitzer 			dm_io_dec_pending(io, BLK_STS_IOERR);
144590a2326eSMike Snitzer 		else
1446e2118b3cSDamien Le Moal 			dm_io_dec_pending(io, BLK_STS_DM_REQUEUE);
1447846785e6SChristoph Hellwig 		break;
1448846785e6SChristoph Hellwig 	default:
144943e6c111SMikulas Patocka 		DMCRIT("unimplemented target map return value: %d", r);
145045cbcd79SKiyoshi Ueda 		BUG();
14511da177e4SLinus Torvalds 	}
14521da177e4SLinus Torvalds }
14531da177e4SLinus Torvalds 
145486a3238cSHeinz Mauelshagen static void setup_split_accounting(struct clone_info *ci, unsigned int len)
14557dd76d1fSMing Lei {
14567dd76d1fSMing Lei 	struct dm_io *io = ci->io;
14577dd76d1fSMing Lei 
14587dd76d1fSMing Lei 	if (ci->sector_count > len) {
14597dd76d1fSMing Lei 		/*
14607dd76d1fSMing Lei 		 * Split needed, save the mapped part for accounting.
14617dd76d1fSMing Lei 		 * NOTE: dm_accept_partial_bio() will update accordingly.
14627dd76d1fSMing Lei 		 */
14637dd76d1fSMing Lei 		dm_io_set_flag(io, DM_IO_WAS_SPLIT);
14647dd76d1fSMing Lei 		io->sectors = len;
14658b211aacSMing Lei 		io->sector_offset = bio_sectors(ci->bio);
14667dd76d1fSMing Lei 	}
14677dd76d1fSMing Lei }
14687dd76d1fSMing Lei 
1469318716ddSMike Snitzer static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci,
147086a3238cSHeinz Mauelshagen 				struct dm_target *ti, unsigned int num_bios)
1471f9ab94ceSMikulas Patocka {
14721d1068ceSChristoph Hellwig 	struct bio *bio;
1473318716ddSMike Snitzer 	int try;
1474dba14160SMikulas Patocka 
1475318716ddSMike Snitzer 	for (try = 0; try < 2; try++) {
1476318716ddSMike Snitzer 		int bio_nr;
1477318716ddSMike Snitzer 
1478318716ddSMike Snitzer 		if (try)
1479bc02cdbeSMike Snitzer 			mutex_lock(&ci->io->md->table_devices_lock);
1480318716ddSMike Snitzer 		for (bio_nr = 0; bio_nr < num_bios; bio_nr++) {
14817dd06a25SMike Snitzer 			bio = alloc_tio(ci, ti, bio_nr, NULL,
1482dc8e2021SChristoph Hellwig 					try ? GFP_NOIO : GFP_NOWAIT);
14831d1068ceSChristoph Hellwig 			if (!bio)
1484318716ddSMike Snitzer 				break;
1485318716ddSMike Snitzer 
14861d1068ceSChristoph Hellwig 			bio_list_add(blist, bio);
1487318716ddSMike Snitzer 		}
1488318716ddSMike Snitzer 		if (try)
1489bc02cdbeSMike Snitzer 			mutex_unlock(&ci->io->md->table_devices_lock);
1490318716ddSMike Snitzer 		if (bio_nr == num_bios)
1491318716ddSMike Snitzer 			return;
1492318716ddSMike Snitzer 
14936c23f0bdSChristoph Hellwig 		while ((bio = bio_list_pop(blist)))
14941d1068ceSChristoph Hellwig 			free_tio(bio);
1495318716ddSMike Snitzer 	}
1496318716ddSMike Snitzer }
1497f9ab94ceSMikulas Patocka 
14980f14d60aSMing Lei static int __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
149986a3238cSHeinz Mauelshagen 				 unsigned int num_bios, unsigned int *len)
150006a426ceSMike Snitzer {
1501318716ddSMike Snitzer 	struct bio_list blist = BIO_EMPTY_LIST;
15028eabf5d0SChristoph Hellwig 	struct bio *clone;
1503564b5c54SMike Snitzer 	unsigned int ret = 0;
150406a426ceSMike Snitzer 
1505891fced6SChristoph Hellwig 	switch (num_bios) {
1506891fced6SChristoph Hellwig 	case 0:
1507891fced6SChristoph Hellwig 		break;
1508891fced6SChristoph Hellwig 	case 1:
15097dd76d1fSMing Lei 		if (len)
15107dd76d1fSMing Lei 			setup_split_accounting(ci, *len);
1511891fced6SChristoph Hellwig 		clone = alloc_tio(ci, ti, 0, len, GFP_NOIO);
1512891fced6SChristoph Hellwig 		__map_bio(clone);
15130f14d60aSMing Lei 		ret = 1;
1514891fced6SChristoph Hellwig 		break;
1515891fced6SChristoph Hellwig 	default:
15167dd06a25SMike Snitzer 		/* dm_accept_partial_bio() is not supported with shared tio->len_ptr */
15177dd06a25SMike Snitzer 		alloc_multiple_bios(&blist, ci, ti, num_bios);
15188eabf5d0SChristoph Hellwig 		while ((clone = bio_list_pop(&blist))) {
1519655f3aadSMike Snitzer 			dm_tio_set_flag(clone_to_tio(clone), DM_TIO_IS_DUPLICATE_BIO);
15201561b396SChristoph Hellwig 			__map_bio(clone);
15210f14d60aSMing Lei 			ret += 1;
1522f9ab94ceSMikulas Patocka 		}
1523891fced6SChristoph Hellwig 		break;
1524318716ddSMike Snitzer 	}
15250f14d60aSMing Lei 
15260f14d60aSMing Lei 	return ret;
152706a426ceSMike Snitzer }
152806a426ceSMike Snitzer 
1529332f2b1eSMike Snitzer static void __send_empty_flush(struct clone_info *ci)
1530f9ab94ceSMikulas Patocka {
1531564b5c54SMike Snitzer 	struct dm_table *t = ci->map;
1532828678b8SMike Snitzer 	struct bio flush_bio;
1533828678b8SMike Snitzer 
1534828678b8SMike Snitzer 	/*
1535828678b8SMike Snitzer 	 * Use an on-stack bio for this, it's safe since we don't
1536828678b8SMike Snitzer 	 * need to reference it after submit. It's just used as
1537828678b8SMike Snitzer 	 * the basis for the clone(s).
1538828678b8SMike Snitzer 	 */
153949add496SChristoph Hellwig 	bio_init(&flush_bio, ci->io->md->disk->part0, NULL, 0,
154049add496SChristoph Hellwig 		 REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC);
154147d95102SChristoph Hellwig 
1542828678b8SMike Snitzer 	ci->bio = &flush_bio;
1543828678b8SMike Snitzer 	ci->sector_count = 0;
154492b914e2SShin'ichiro Kawasaki 	ci->io->tio.clone.bi_iter.bi_size = 0;
1545f9ab94ceSMikulas Patocka 
1546564b5c54SMike Snitzer 	for (unsigned int i = 0; i < t->num_targets; i++) {
1547564b5c54SMike Snitzer 		unsigned int bios;
1548564b5c54SMike Snitzer 		struct dm_target *ti = dm_table_get_target(t, i);
15490f14d60aSMing Lei 
15500f14d60aSMing Lei 		atomic_add(ti->num_flush_bios, &ci->io->io_count);
15510f14d60aSMing Lei 		bios = __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
15520f14d60aSMing Lei 		atomic_sub(ti->num_flush_bios - bios, &ci->io->io_count);
15530f14d60aSMing Lei 	}
15540f14d60aSMing Lei 
15550f14d60aSMing Lei 	/*
15560f14d60aSMing Lei 	 * alloc_io() takes one extra reference for submission, so the
15570f14d60aSMing Lei 	 * reference won't reach 0 without the following subtraction
15580f14d60aSMing Lei 	 */
15590f14d60aSMing Lei 	atomic_sub(1, &ci->io->io_count);
1560828678b8SMike Snitzer 
1561828678b8SMike Snitzer 	bio_uninit(ci->bio);
1562f9ab94ceSMikulas Patocka }
1563f9ab94ceSMikulas Patocka 
1564e6fc9f62SMike Snitzer static void __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti,
156586a3238cSHeinz Mauelshagen 					unsigned int num_bios)
15665ae89a87SMike Snitzer {
156786a3238cSHeinz Mauelshagen 	unsigned int len, bios;
15685ae89a87SMike Snitzer 
15693720281dSMike Snitzer 	len = min_t(sector_t, ci->sector_count,
15703720281dSMike Snitzer 		    max_io_len_target_boundary(ti, dm_target_offset(ti, ci->sector)));
157151b86f9aSMichael Lass 
15720f14d60aSMing Lei 	atomic_add(num_bios, &ci->io->io_count);
15730f14d60aSMing Lei 	bios = __send_duplicate_bios(ci, ti, num_bios, &len);
15740f14d60aSMing Lei 	/*
15750f14d60aSMing Lei 	 * alloc_io() takes one extra reference for submission, so the
15760f14d60aSMing Lei 	 * reference won't reach 0 without the following (+1) subtraction
15770f14d60aSMing Lei 	 */
15780f14d60aSMing Lei 	atomic_sub(num_bios - bios + 1, &ci->io->io_count);
15797dd06a25SMike Snitzer 
1580a79245b3SMike Snitzer 	ci->sector += len;
15813d7f4562SMike Snitzer 	ci->sector_count -= len;
15825ae89a87SMike Snitzer }
15835ae89a87SMike Snitzer 
1584568c73a3SMike Snitzer static bool is_abnormal_io(struct bio *bio)
1585568c73a3SMike Snitzer {
1586a3282b43SBart Van Assche 	enum req_op op = bio_op(bio);
1587568c73a3SMike Snitzer 
15884edadf6dSMike Snitzer 	if (op != REQ_OP_READ && op != REQ_OP_WRITE && op != REQ_OP_FLUSH) {
15894edadf6dSMike Snitzer 		switch (op) {
1590568c73a3SMike Snitzer 		case REQ_OP_DISCARD:
1591568c73a3SMike Snitzer 		case REQ_OP_SECURE_ERASE:
1592568c73a3SMike Snitzer 		case REQ_OP_WRITE_ZEROES:
15934edadf6dSMike Snitzer 			return true;
15944edadf6dSMike Snitzer 		default:
1595568c73a3SMike Snitzer 			break;
1596568c73a3SMike Snitzer 		}
1597568c73a3SMike Snitzer 	}
1598568c73a3SMike Snitzer 
15994edadf6dSMike Snitzer 	return false;
16004edadf6dSMike Snitzer }
16014edadf6dSMike Snitzer 
16024edadf6dSMike Snitzer static blk_status_t __process_abnormal_io(struct clone_info *ci,
16034edadf6dSMike Snitzer 					  struct dm_target *ti)
16040519c71eSMike Snitzer {
160586a3238cSHeinz Mauelshagen 	unsigned int num_bios = 0;
16060519c71eSMike Snitzer 
1607e6fc9f62SMike Snitzer 	switch (bio_op(ci->bio)) {
16089679b5a7SMike Snitzer 	case REQ_OP_DISCARD:
16099679b5a7SMike Snitzer 		num_bios = ti->num_discard_bios;
16109679b5a7SMike Snitzer 		break;
16119679b5a7SMike Snitzer 	case REQ_OP_SECURE_ERASE:
16129679b5a7SMike Snitzer 		num_bios = ti->num_secure_erase_bios;
16139679b5a7SMike Snitzer 		break;
16149679b5a7SMike Snitzer 	case REQ_OP_WRITE_ZEROES:
16159679b5a7SMike Snitzer 		num_bios = ti->num_write_zeroes_bios;
16169679b5a7SMike Snitzer 		break;
16172d9b02beSBart Van Assche 	default:
16182d9b02beSBart Van Assche 		break;
16199679b5a7SMike Snitzer 	}
16200519c71eSMike Snitzer 
1621e6fc9f62SMike Snitzer 	/*
1622e6fc9f62SMike Snitzer 	 * Even though the device advertised support for this type of
1623e6fc9f62SMike Snitzer 	 * request, that does not mean every target supports it, and
1624e6fc9f62SMike Snitzer 	 * reconfiguration might also have changed that since the
1625e6fc9f62SMike Snitzer 	 * check was performed.
1626e6fc9f62SMike Snitzer 	 */
162784b98f4cSMike Snitzer 	if (unlikely(!num_bios))
16284edadf6dSMike Snitzer 		return BLK_STS_NOTSUPP;
16294edadf6dSMike Snitzer 
1630e6fc9f62SMike Snitzer 	__send_changing_extent_only(ci, ti, num_bios);
16314edadf6dSMike Snitzer 	return BLK_STS_OK;
16320519c71eSMike Snitzer }
16330519c71eSMike Snitzer 
1634e4c93811SAlasdair G Kergon /*
1635ec211631SMing Lei  * Reuse ->bi_private as dm_io list head for storing all dm_io instances
1636b99fdcdcSMing Lei  * associated with this bio, and this bio's bi_private needs to be
1637b99fdcdcSMing Lei  * stored in dm_io->data before the reuse.
1638b99fdcdcSMing Lei  *
1639b99fdcdcSMing Lei  * bio->bi_private is owned by fs or upper layer, so block layer won't
1640b99fdcdcSMing Lei  * touch it after splitting. Meantime it won't be changed by anyone after
1641b99fdcdcSMing Lei  * bio is submitted. So this reuse is safe.
1642b99fdcdcSMing Lei  */
1643ec211631SMing Lei static inline struct dm_io **dm_poll_list_head(struct bio *bio)
1644b99fdcdcSMing Lei {
1645ec211631SMing Lei 	return (struct dm_io **)&bio->bi_private;
1646b99fdcdcSMing Lei }
1647b99fdcdcSMing Lei 
1648b99fdcdcSMing Lei static void dm_queue_poll_io(struct bio *bio, struct dm_io *io)
1649b99fdcdcSMing Lei {
1650ec211631SMing Lei 	struct dm_io **head = dm_poll_list_head(bio);
1651b99fdcdcSMing Lei 
1652b99fdcdcSMing Lei 	if (!(bio->bi_opf & REQ_DM_POLL_LIST)) {
1653b99fdcdcSMing Lei 		bio->bi_opf |= REQ_DM_POLL_LIST;
1654b99fdcdcSMing Lei 		/*
1655b99fdcdcSMing Lei 		 * Save .bi_private into dm_io, so that we can reuse
1656ec211631SMing Lei 		 * .bi_private as dm_io list head for storing dm_io list
1657b99fdcdcSMing Lei 		 */
1658b99fdcdcSMing Lei 		io->data = bio->bi_private;
1659b99fdcdcSMing Lei 
1660b99fdcdcSMing Lei 		/* tell block layer to poll for completion */
1661b99fdcdcSMing Lei 		bio->bi_cookie = ~BLK_QC_T_NONE;
1662ec211631SMing Lei 
1663ec211631SMing Lei 		io->next = NULL;
1664b99fdcdcSMing Lei 	} else {
1665b99fdcdcSMing Lei 		/*
1666b99fdcdcSMing Lei 		 * bio recursed due to split, reuse original poll list,
1667b99fdcdcSMing Lei 		 * and save bio->bi_private too.
1668b99fdcdcSMing Lei 		 */
1669ec211631SMing Lei 		io->data = (*head)->data;
1670ec211631SMing Lei 		io->next = *head;
1671b99fdcdcSMing Lei 	}
1672b99fdcdcSMing Lei 
1673ec211631SMing Lei 	*head = io;
1674b99fdcdcSMing Lei }
1675b99fdcdcSMing Lei 
1676b99fdcdcSMing Lei /*
1677e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1678e4c93811SAlasdair G Kergon  */
167984b98f4cSMike Snitzer static blk_status_t __split_and_process_bio(struct clone_info *ci)
1680e4c93811SAlasdair G Kergon {
168166bdaa43SMike Snitzer 	struct bio *clone;
1682e4c93811SAlasdair G Kergon 	struct dm_target *ti;
168386a3238cSHeinz Mauelshagen 	unsigned int len;
1684e4c93811SAlasdair G Kergon 
1685e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
16864edadf6dSMike Snitzer 	if (unlikely(!ti))
16874edadf6dSMike Snitzer 		return BLK_STS_IOERR;
16881ee88de3SMikulas Patocka 
16891ee88de3SMikulas Patocka 	if (unlikely((ci->bio->bi_opf & REQ_NOWAIT) != 0) &&
16901ee88de3SMikulas Patocka 	    unlikely(!dm_target_supports_nowait(ti->type)))
16911ee88de3SMikulas Patocka 		return BLK_STS_NOTSUPP;
16921ee88de3SMikulas Patocka 
16931ee88de3SMikulas Patocka 	if (unlikely(ci->is_abnormal_io))
16944edadf6dSMike Snitzer 		return __process_abnormal_io(ci, ti);
16953d7f4562SMike Snitzer 
1696b99fdcdcSMing Lei 	/*
1697b99fdcdcSMing Lei 	 * Only support bio polling for normal IO, and the target io is
1698b99fdcdcSMing Lei 	 * exactly inside the dm_io instance (verified in dm_poll_dm_io)
1699b99fdcdcSMing Lei 	 */
1700a3282b43SBart Van Assche 	ci->submit_as_polled = !!(ci->bio->bi_opf & REQ_POLLED);
1701e4c93811SAlasdair G Kergon 
1702e4c93811SAlasdair G Kergon 	len = min_t(sector_t, max_io_len(ti, ci->sector), ci->sector_count);
17037dd76d1fSMing Lei 	setup_split_accounting(ci, len);
170466bdaa43SMike Snitzer 	clone = alloc_tio(ci, ti, 0, &len, GFP_NOIO);
170566bdaa43SMike Snitzer 	__map_bio(clone);
1706e4c93811SAlasdair G Kergon 
1707e4c93811SAlasdair G Kergon 	ci->sector += len;
1708e4c93811SAlasdair G Kergon 	ci->sector_count -= len;
1709e4c93811SAlasdair G Kergon 
171084b98f4cSMike Snitzer 	return BLK_STS_OK;
1711e4c93811SAlasdair G Kergon }
1712e4c93811SAlasdair G Kergon 
1713978e51baSMike Snitzer static void init_clone_info(struct clone_info *ci, struct mapped_device *md,
17144edadf6dSMike Snitzer 			    struct dm_table *map, struct bio *bio, bool is_abnormal)
1715978e51baSMike Snitzer {
1716978e51baSMike Snitzer 	ci->map = map;
1717978e51baSMike Snitzer 	ci->io = alloc_io(md, bio);
1718d41e077aSMike Snitzer 	ci->bio = bio;
17194edadf6dSMike Snitzer 	ci->is_abnormal_io = is_abnormal;
1720b99fdcdcSMing Lei 	ci->submit_as_polled = false;
1721978e51baSMike Snitzer 	ci->sector = bio->bi_iter.bi_sector;
1722d41e077aSMike Snitzer 	ci->sector_count = bio_sectors(bio);
1723d41e077aSMike Snitzer 
1724d41e077aSMike Snitzer 	/* Shouldn't happen but sector_count was being set to 0 so... */
1725442761fdSMike Snitzer 	if (static_branch_unlikely(&zoned_enabled) &&
1726442761fdSMike Snitzer 	    WARN_ON_ONCE(op_is_zone_mgmt(bio_op(bio)) && ci->sector_count))
1727d41e077aSMike Snitzer 		ci->sector_count = 0;
1728978e51baSMike Snitzer }
1729978e51baSMike Snitzer 
1730e4c93811SAlasdair G Kergon /*
173114fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
17321da177e4SLinus Torvalds  */
173396c9865cSMike Snitzer static void dm_split_and_process_bio(struct mapped_device *md,
173483d5e5b0SMikulas Patocka 				     struct dm_table *map, struct bio *bio)
17351da177e4SLinus Torvalds {
17361da177e4SLinus Torvalds 	struct clone_info ci;
17374857abf6SMike Snitzer 	struct dm_io *io;
173884b98f4cSMike Snitzer 	blk_status_t error = BLK_STS_OK;
17394edadf6dSMike Snitzer 	bool is_abnormal;
17401da177e4SLinus Torvalds 
17414edadf6dSMike Snitzer 	is_abnormal = is_abnormal_io(bio);
17424edadf6dSMike Snitzer 	if (unlikely(is_abnormal)) {
17434edadf6dSMike Snitzer 		/*
17445a97806fSChristoph Hellwig 		 * Use bio_split_to_limits() for abnormal IO (e.g. discard, etc)
17454edadf6dSMike Snitzer 		 * otherwise associated queue_limits won't be imposed.
17464edadf6dSMike Snitzer 		 */
17475a97806fSChristoph Hellwig 		bio = bio_split_to_limits(bio);
1748613b1488SJens Axboe 		if (!bio)
1749613b1488SJens Axboe 			return;
17504edadf6dSMike Snitzer 	}
17514edadf6dSMike Snitzer 
17524edadf6dSMike Snitzer 	init_clone_info(&ci, md, map, bio, is_abnormal);
17534857abf6SMike Snitzer 	io = ci.io;
1754bd2a49b8SAlasdair G Kergon 
17551eff9d32SJens Axboe 	if (bio->bi_opf & REQ_PREFLUSH) {
1756332f2b1eSMike Snitzer 		__send_empty_flush(&ci);
1757e2736347SMike Snitzer 		/* dm_io_complete submits any data associated with flush */
1758d41e077aSMike Snitzer 		goto out;
1759d41e077aSMike Snitzer 	}
1760d41e077aSMike Snitzer 
176196c9865cSMike Snitzer 	error = __split_and_process_bio(&ci);
1762d41e077aSMike Snitzer 	if (error || !ci.sector_count)
1763d41e077aSMike Snitzer 		goto out;
176418a25da8SNeilBrown 	/*
1765d41e077aSMike Snitzer 	 * Remainder must be passed to submit_bio_noacct() so it gets handled
1766d41e077aSMike Snitzer 	 * *after* bios already submitted have been completely processed.
176718a25da8SNeilBrown 	 */
17688b211aacSMing Lei 	bio_trim(bio, io->sectors, ci.sector_count);
17698b211aacSMing Lei 	trace_block_split(bio, bio->bi_iter.bi_sector);
17708b211aacSMing Lei 	bio_inc_remaining(bio);
17713e08773cSChristoph Hellwig 	submit_bio_noacct(bio);
1772d41e077aSMike Snitzer out:
1773b99fdcdcSMing Lei 	/*
1774b99fdcdcSMing Lei 	 * Drop the extra reference count for non-POLLED bio, and hold one
1775b99fdcdcSMing Lei 	 * reference for POLLED bio, which will be released in dm_poll_bio
1776b99fdcdcSMing Lei 	 *
1777ec211631SMing Lei 	 * Add every dm_io instance into the dm_io list head which is stored
1778ec211631SMing Lei 	 * in bio->bi_private, so that dm_poll_bio can poll them all.
1779b99fdcdcSMing Lei 	 */
17800f14d60aSMing Lei 	if (error || !ci.submit_as_polled) {
17810f14d60aSMing Lei 		/*
17820f14d60aSMing Lei 		 * In case of submission failure, the extra reference for
17830f14d60aSMing Lei 		 * submitting io isn't consumed yet
17840f14d60aSMing Lei 		 */
17850f14d60aSMing Lei 		if (error)
17860f14d60aSMing Lei 			atomic_dec(&io->io_count);
17870f14d60aSMing Lei 		dm_io_dec_pending(io, error);
17880f14d60aSMing Lei 	} else
17894857abf6SMike Snitzer 		dm_queue_poll_io(bio, io);
17901da177e4SLinus Torvalds }
17911da177e4SLinus Torvalds 
17923e08773cSChristoph Hellwig static void dm_submit_bio(struct bio *bio)
17931da177e4SLinus Torvalds {
1794309dca30SChristoph Hellwig 	struct mapped_device *md = bio->bi_bdev->bd_disk->private_data;
179583d5e5b0SMikulas Patocka 	int srcu_idx;
179683d5e5b0SMikulas Patocka 	struct dm_table *map;
1797a3282b43SBart Van Assche 	blk_opf_t bio_opf = bio->bi_opf;
17981da177e4SLinus Torvalds 
17995d7362d0SMikulas Patocka 	map = dm_get_live_table_bio(md, &srcu_idx, bio_opf);
18008cf7961dSChristoph Hellwig 
1801fa247089SMike Snitzer 	/* If suspended, or map not yet available, queue this IO for later */
1802fa247089SMike Snitzer 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) ||
1803fa247089SMike Snitzer 	    unlikely(!map)) {
18046abc4946SKonstantin Khlebnikov 		if (bio->bi_opf & REQ_NOWAIT)
18056abc4946SKonstantin Khlebnikov 			bio_wouldblock_error(bio);
1806b2abdb1bSMike Snitzer 		else if (bio->bi_opf & REQ_RAHEAD)
18076a8736d1STejun Heo 			bio_io_error(bio);
1808b2abdb1bSMike Snitzer 		else
1809b2abdb1bSMike Snitzer 			queue_io(md, bio);
1810b2abdb1bSMike Snitzer 		goto out;
18111da177e4SLinus Torvalds 	}
18121da177e4SLinus Torvalds 
181396c9865cSMike Snitzer 	dm_split_and_process_bio(md, map, bio);
1814b2abdb1bSMike Snitzer out:
18155d7362d0SMikulas Patocka 	dm_put_live_table_bio(md, srcu_idx, bio_opf);
1816978e51baSMike Snitzer }
1817978e51baSMike Snitzer 
1818b99fdcdcSMing Lei static bool dm_poll_dm_io(struct dm_io *io, struct io_comp_batch *iob,
1819b99fdcdcSMing Lei 			  unsigned int flags)
1820b99fdcdcSMing Lei {
1821655f3aadSMike Snitzer 	WARN_ON_ONCE(!dm_tio_is_normal(&io->tio));
1822b99fdcdcSMing Lei 
1823b99fdcdcSMing Lei 	/* don't poll if the mapped io is done */
1824b99fdcdcSMing Lei 	if (atomic_read(&io->io_count) > 1)
1825b99fdcdcSMing Lei 		bio_poll(&io->tio.clone, iob, flags);
1826b99fdcdcSMing Lei 
1827b99fdcdcSMing Lei 	/* bio_poll holds the last reference */
1828b99fdcdcSMing Lei 	return atomic_read(&io->io_count) == 1;
1829b99fdcdcSMing Lei }
1830b99fdcdcSMing Lei 
1831b99fdcdcSMing Lei static int dm_poll_bio(struct bio *bio, struct io_comp_batch *iob,
1832b99fdcdcSMing Lei 		       unsigned int flags)
1833b99fdcdcSMing Lei {
1834ec211631SMing Lei 	struct dm_io **head = dm_poll_list_head(bio);
1835ec211631SMing Lei 	struct dm_io *list = *head;
1836ec211631SMing Lei 	struct dm_io *tmp = NULL;
1837ec211631SMing Lei 	struct dm_io *curr, *next;
1838b99fdcdcSMing Lei 
1839b99fdcdcSMing Lei 	/* Only poll normal bio which was marked as REQ_DM_POLL_LIST */
1840b99fdcdcSMing Lei 	if (!(bio->bi_opf & REQ_DM_POLL_LIST))
1841b99fdcdcSMing Lei 		return 0;
1842b99fdcdcSMing Lei 
1843ec211631SMing Lei 	WARN_ON_ONCE(!list);
1844b99fdcdcSMing Lei 
1845b99fdcdcSMing Lei 	/*
1846b99fdcdcSMing Lei 	 * Restore .bi_private before possibly completing dm_io.
1847b99fdcdcSMing Lei 	 *
1848b99fdcdcSMing Lei 	 * bio_poll() is only possible once @bio has been completely
1849b99fdcdcSMing Lei 	 * submitted via submit_bio_noacct()'s depth-first submission.
1850b99fdcdcSMing Lei 	 * So there is no dm_queue_poll_io() race associated with
1851b99fdcdcSMing Lei 	 * clearing REQ_DM_POLL_LIST here.
1852b99fdcdcSMing Lei 	 */
1853b99fdcdcSMing Lei 	bio->bi_opf &= ~REQ_DM_POLL_LIST;
1854ec211631SMing Lei 	bio->bi_private = list->data;
1855b99fdcdcSMing Lei 
1856ec211631SMing Lei 	for (curr = list, next = curr->next; curr; curr = next, next =
1857ec211631SMing Lei 			curr ? curr->next : NULL) {
1858ec211631SMing Lei 		if (dm_poll_dm_io(curr, iob, flags)) {
1859b99fdcdcSMing Lei 			/*
186084b98f4cSMike Snitzer 			 * clone_endio() has already occurred, so no
186184b98f4cSMike Snitzer 			 * error handling is needed here.
1862b99fdcdcSMing Lei 			 */
1863ec211631SMing Lei 			__dm_io_dec_pending(curr);
1864ec211631SMing Lei 		} else {
1865ec211631SMing Lei 			curr->next = tmp;
1866ec211631SMing Lei 			tmp = curr;
1867b99fdcdcSMing Lei 		}
1868b99fdcdcSMing Lei 	}
1869b99fdcdcSMing Lei 
1870b99fdcdcSMing Lei 	/* Not done? */
1871ec211631SMing Lei 	if (tmp) {
1872b99fdcdcSMing Lei 		bio->bi_opf |= REQ_DM_POLL_LIST;
1873b99fdcdcSMing Lei 		/* Reset bio->bi_private to dm_io list head */
1874ec211631SMing Lei 		*head = tmp;
1875b99fdcdcSMing Lei 		return 0;
1876b99fdcdcSMing Lei 	}
1877b99fdcdcSMing Lei 	return 1;
1878b99fdcdcSMing Lei }
1879b99fdcdcSMing Lei 
1880a4a82ce3SHeinz Mauelshagen /*
1881a4a82ce3SHeinz Mauelshagen  *---------------------------------------------------------------
18821da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
1883a4a82ce3SHeinz Mauelshagen  *---------------------------------------------------------------
1884a4a82ce3SHeinz Mauelshagen  */
18852b06cfffSAlasdair G Kergon static void free_minor(int minor)
18861da177e4SLinus Torvalds {
1887f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
18881da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1889f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
18901da177e4SLinus Torvalds }
18911da177e4SLinus Torvalds 
18921da177e4SLinus Torvalds /*
18931da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
18941da177e4SLinus Torvalds  */
1895cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
18961da177e4SLinus Torvalds {
1897c9d76be6STejun Heo 	int r;
18981da177e4SLinus Torvalds 
18991da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
19001da177e4SLinus Torvalds 		return -EINVAL;
19011da177e4SLinus Torvalds 
1902c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1903f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
19041da177e4SLinus Torvalds 
1905c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
19061da177e4SLinus Torvalds 
1907f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1908c9d76be6STejun Heo 	idr_preload_end();
1909c9d76be6STejun Heo 	if (r < 0)
1910c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
1911c9d76be6STejun Heo 	return 0;
19121da177e4SLinus Torvalds }
19131da177e4SLinus Torvalds 
1914cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
19151da177e4SLinus Torvalds {
1916c9d76be6STejun Heo 	int r;
19171da177e4SLinus Torvalds 
1918c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1919f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
19201da177e4SLinus Torvalds 
1921c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
19221da177e4SLinus Torvalds 
1923f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1924c9d76be6STejun Heo 	idr_preload_end();
1925c9d76be6STejun Heo 	if (r < 0)
19261da177e4SLinus Torvalds 		return r;
1927c9d76be6STejun Heo 	*minor = r;
1928c9d76be6STejun Heo 	return 0;
19291da177e4SLinus Torvalds }
19301da177e4SLinus Torvalds 
193183d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
1932681cc5e8SMike Snitzer static const struct block_device_operations dm_rq_blk_dops;
1933f26c5719SDan Williams static const struct dax_operations dm_dax_ops;
19341da177e4SLinus Torvalds 
193553d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
193653d5914fSMikulas Patocka 
1937aa6ce87aSSatya Tangirala #ifdef CONFIG_BLK_INLINE_ENCRYPTION
1938cb77cb5aSEric Biggers static void dm_queue_destroy_crypto_profile(struct request_queue *q)
1939aa6ce87aSSatya Tangirala {
1940cb77cb5aSEric Biggers 	dm_destroy_crypto_profile(q->crypto_profile);
1941aa6ce87aSSatya Tangirala }
1942aa6ce87aSSatya Tangirala 
1943aa6ce87aSSatya Tangirala #else /* CONFIG_BLK_INLINE_ENCRYPTION */
1944aa6ce87aSSatya Tangirala 
1945cb77cb5aSEric Biggers static inline void dm_queue_destroy_crypto_profile(struct request_queue *q)
1946aa6ce87aSSatya Tangirala {
1947aa6ce87aSSatya Tangirala }
1948aa6ce87aSSatya Tangirala #endif /* !CONFIG_BLK_INLINE_ENCRYPTION */
1949aa6ce87aSSatya Tangirala 
19500f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md)
19510f20972fSMike Snitzer {
19520f20972fSMike Snitzer 	if (md->wq)
19530f20972fSMike Snitzer 		destroy_workqueue(md->wq);
195429dec90aSChristoph Hellwig 	dm_free_md_mempools(md->mempools);
19550f20972fSMike Snitzer 
1956f26c5719SDan Williams 	if (md->dax_dev) {
1957fb08a190SChristoph Hellwig 		dax_remove_host(md->disk);
1958f26c5719SDan Williams 		kill_dax(md->dax_dev);
1959f26c5719SDan Williams 		put_dax(md->dax_dev);
1960f26c5719SDan Williams 		md->dax_dev = NULL;
1961f26c5719SDan Williams 	}
1962f26c5719SDan Williams 
1963588b7f5dSKirill Tkhai 	dm_cleanup_zoned_dev(md);
19640f20972fSMike Snitzer 	if (md->disk) {
19650f20972fSMike Snitzer 		spin_lock(&_minor_lock);
19660f20972fSMike Snitzer 		md->disk->private_data = NULL;
19670f20972fSMike Snitzer 		spin_unlock(&_minor_lock);
196889f871afSChristoph Hellwig 		if (dm_get_md_type(md) != DM_TYPE_NONE) {
19691a581b72SChristoph Hellwig 			struct table_device *td;
19701a581b72SChristoph Hellwig 
197189f871afSChristoph Hellwig 			dm_sysfs_exit(md);
19721a581b72SChristoph Hellwig 			list_for_each_entry(td, &md->table_devices, list) {
19731a581b72SChristoph Hellwig 				bd_unlink_disk_holder(td->dm_dev.bdev,
19741a581b72SChristoph Hellwig 						      md->disk);
19751a581b72SChristoph Hellwig 			}
1976d563792cSYu Kuai 
1977d563792cSYu Kuai 			/*
1978d563792cSYu Kuai 			 * Hold lock to make sure del_gendisk() won't concurrent
1979d563792cSYu Kuai 			 * with open/close_table_device().
1980d563792cSYu Kuai 			 */
1981d563792cSYu Kuai 			mutex_lock(&md->table_devices_lock);
19820f20972fSMike Snitzer 			del_gendisk(md->disk);
1983d563792cSYu Kuai 			mutex_unlock(&md->table_devices_lock);
198489f871afSChristoph Hellwig 		}
1985cb77cb5aSEric Biggers 		dm_queue_destroy_crypto_profile(md->queue);
19868b9ab626SChristoph Hellwig 		put_disk(md->disk);
198774a2b6ecSChristoph Hellwig 	}
19880f20972fSMike Snitzer 
19899f6dc633SMike Snitzer 	if (md->pending_io) {
19909f6dc633SMike Snitzer 		free_percpu(md->pending_io);
19919f6dc633SMike Snitzer 		md->pending_io = NULL;
19929f6dc633SMike Snitzer 	}
19939f6dc633SMike Snitzer 
1994d09960b0STahsin Erdogan 	cleanup_srcu_struct(&md->io_barrier);
1995d09960b0STahsin Erdogan 
1996d5ffebddSMike Snitzer 	mutex_destroy(&md->suspend_lock);
1997d5ffebddSMike Snitzer 	mutex_destroy(&md->type_lock);
1998d5ffebddSMike Snitzer 	mutex_destroy(&md->table_devices_lock);
1999a666e5c0SMikulas Patocka 	mutex_destroy(&md->swap_bios_lock);
2000d5ffebddSMike Snitzer 
20014cc96131SMike Snitzer 	dm_mq_cleanup_mapped_device(md);
20020f20972fSMike Snitzer }
20030f20972fSMike Snitzer 
20041da177e4SLinus Torvalds /*
20051da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
20061da177e4SLinus Torvalds  */
20072b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
20081da177e4SLinus Torvalds {
2009115485e8SMike Snitzer 	int r, numa_node_id = dm_get_numa_node();
2010115485e8SMike Snitzer 	struct mapped_device *md;
2011ba61fdd1SJeff Mahoney 	void *old_md;
20121da177e4SLinus Torvalds 
2013856eb091SMikulas Patocka 	md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
20141da177e4SLinus Torvalds 	if (!md) {
201543e6c111SMikulas Patocka 		DMERR("unable to allocate device, out of memory.");
20161da177e4SLinus Torvalds 		return NULL;
20171da177e4SLinus Torvalds 	}
20181da177e4SLinus Torvalds 
201910da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
20206ed7ade8SMilan Broz 		goto bad_module_get;
202110da4f79SJeff Mahoney 
20221da177e4SLinus Torvalds 	/* get a minor number for the dev */
20232b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
2024cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
20252b06cfffSAlasdair G Kergon 	else
2026cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
20271da177e4SLinus Torvalds 	if (r < 0)
20286ed7ade8SMilan Broz 		goto bad_minor;
20291da177e4SLinus Torvalds 
203083d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
203183d5e5b0SMikulas Patocka 	if (r < 0)
203283d5e5b0SMikulas Patocka 		goto bad_io_barrier;
203383d5e5b0SMikulas Patocka 
2034115485e8SMike Snitzer 	md->numa_node_id = numa_node_id;
2035591ddcfcSMike Snitzer 	md->init_tio_pdu = false;
2036a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
2037e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
2038a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
203986f1152bSBenjamin Marzinski 	mutex_init(&md->table_devices_lock);
2040022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
20411da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
20425c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
20431da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
20447a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
20457a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
204686f1152bSBenjamin Marzinski 	INIT_LIST_HEAD(&md->table_devices);
20477a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
20481da177e4SLinus Torvalds 
204947ace7e0SMike Snitzer 	/*
2050c62b37d9SChristoph Hellwig 	 * default to bio-based until DM table is loaded and md->type
2051c62b37d9SChristoph Hellwig 	 * established. If request-based table is loaded: blk-mq will
2052c62b37d9SChristoph Hellwig 	 * override accordingly.
205347ace7e0SMike Snitzer 	 */
205474fe6ba9SChristoph Hellwig 	md->disk = blk_alloc_disk(md->numa_node_id);
20551da177e4SLinus Torvalds 	if (!md->disk)
20560f20972fSMike Snitzer 		goto bad;
205774fe6ba9SChristoph Hellwig 	md->queue = md->disk->queue;
20581da177e4SLinus Torvalds 
2059f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
206053d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
20618b211aacSMing Lei 	INIT_WORK(&md->requeue_work, dm_wq_requeue_work);
2062f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
20632995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
2064f0b04115SJeff Mahoney 
20658b211aacSMing Lei 	md->requeue_list = NULL;
2066a666e5c0SMikulas Patocka 	md->swap_bios = get_swap_bios();
2067a666e5c0SMikulas Patocka 	sema_init(&md->swap_bios_semaphore, md->swap_bios);
2068a666e5c0SMikulas Patocka 	mutex_init(&md->swap_bios_lock);
2069a666e5c0SMikulas Patocka 
20701da177e4SLinus Torvalds 	md->disk->major = _major;
20711da177e4SLinus Torvalds 	md->disk->first_minor = minor;
207274fe6ba9SChristoph Hellwig 	md->disk->minors = 1;
20731ebe2e5fSChristoph Hellwig 	md->disk->flags |= GENHD_FL_NO_PART;
20741da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
20751da177e4SLinus Torvalds 	md->disk->private_data = md;
20761da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
2077f26c5719SDan Williams 
20785d2a228bSChristoph Hellwig 	if (IS_ENABLED(CONFIG_FS_DAX)) {
207930c6828aSChristoph Hellwig 		md->dax_dev = alloc_dax(md, &dm_dax_ops);
2080d7519392SChristoph Hellwig 		if (IS_ERR(md->dax_dev)) {
2081d7519392SChristoph Hellwig 			md->dax_dev = NULL;
2082f26c5719SDan Williams 			goto bad;
2083976431b0SDan Williams 		}
20847ac5360cSChristoph Hellwig 		set_dax_nocache(md->dax_dev);
20857ac5360cSChristoph Hellwig 		set_dax_nomc(md->dax_dev);
2086fb08a190SChristoph Hellwig 		if (dax_add_host(md->dax_dev, md->disk))
2087f26c5719SDan Williams 			goto bad;
2088f26c5719SDan Williams 	}
20891da177e4SLinus Torvalds 
20907e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
20911da177e4SLinus Torvalds 
2092c7c879eeSMichał Mirosław 	md->wq = alloc_workqueue("kdmflush/%s", WQ_MEM_RECLAIM, 0, md->name);
2093304f3f6aSMilan Broz 	if (!md->wq)
20940f20972fSMike Snitzer 		goto bad;
2095304f3f6aSMilan Broz 
20969f6dc633SMike Snitzer 	md->pending_io = alloc_percpu(unsigned long);
20979f6dc633SMike Snitzer 	if (!md->pending_io)
20989f6dc633SMike Snitzer 		goto bad;
20999f6dc633SMike Snitzer 
2100*d3aa3e06SJiasheng Jiang 	r = dm_stats_init(&md->stats);
2101*d3aa3e06SJiasheng Jiang 	if (r < 0)
2102*d3aa3e06SJiasheng Jiang 		goto bad;
2103fd2ed4d2SMikulas Patocka 
2104ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
2105f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
2106ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
2107f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2108ba61fdd1SJeff Mahoney 
2109ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
2110ba61fdd1SJeff Mahoney 
21111da177e4SLinus Torvalds 	return md;
21121da177e4SLinus Torvalds 
21130f20972fSMike Snitzer bad:
21140f20972fSMike Snitzer 	cleanup_mapped_device(md);
211583d5e5b0SMikulas Patocka bad_io_barrier:
21161da177e4SLinus Torvalds 	free_minor(minor);
21176ed7ade8SMilan Broz bad_minor:
211810da4f79SJeff Mahoney 	module_put(THIS_MODULE);
21196ed7ade8SMilan Broz bad_module_get:
2120856eb091SMikulas Patocka 	kvfree(md);
21211da177e4SLinus Torvalds 	return NULL;
21221da177e4SLinus Torvalds }
21231da177e4SLinus Torvalds 
2124ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
2125ae9da83fSJun'ichi Nomura 
21261da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
21271da177e4SLinus Torvalds {
2128f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
212963d94e48SJun'ichi Nomura 
2130ae9da83fSJun'ichi Nomura 	unlock_fs(md);
21312eb6e1e3SKeith Busch 
21320f20972fSMike Snitzer 	cleanup_mapped_device(md);
21330f20972fSMike Snitzer 
2134992ec6a9SChristoph Hellwig 	WARN_ON_ONCE(!list_empty(&md->table_devices));
21350f20972fSMike Snitzer 	dm_stats_cleanup(&md->stats);
213663a4f065SMike Snitzer 	free_minor(minor);
213763a4f065SMike Snitzer 
213810da4f79SJeff Mahoney 	module_put(THIS_MODULE);
2139856eb091SMikulas Patocka 	kvfree(md);
21401da177e4SLinus Torvalds }
21411da177e4SLinus Torvalds 
21421da177e4SLinus Torvalds /*
21431da177e4SLinus Torvalds  * Bind a table to the device.
21441da177e4SLinus Torvalds  */
21451da177e4SLinus Torvalds static void event_callback(void *context)
21461da177e4SLinus Torvalds {
21477a8c3d3bSMike Anderson 	unsigned long flags;
21487a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
2149d695e441SXU pengfei 	struct mapped_device *md = context;
21501da177e4SLinus Torvalds 
21517a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
21527a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
21537a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
21547a8c3d3bSMike Anderson 
2155ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
21567a8c3d3bSMike Anderson 
21571da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
21581da177e4SLinus Torvalds 	wake_up(&md->eventq);
215962e08243SMikulas Patocka 	dm_issue_global_event();
21601da177e4SLinus Torvalds }
21611da177e4SLinus Torvalds 
2162c217649bSMike Snitzer /*
2163042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2164042d2a9bSAlasdair G Kergon  */
2165042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2166754c5fc7SMike Snitzer 			       struct queue_limits *limits)
21671da177e4SLinus Torvalds {
2168042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
21691da177e4SLinus Torvalds 	sector_t size;
21702a2a4c51SJens Axboe 	int ret;
21711da177e4SLinus Torvalds 
21725a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
21735a8f1f80SBart Van Assche 
21741da177e4SLinus Torvalds 	size = dm_table_get_size(t);
21753ac51e74SDarrick J. Wong 
21763ac51e74SDarrick J. Wong 	/*
21773ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
21783ac51e74SDarrick J. Wong 	 */
2179fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
21803ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
21813ac51e74SDarrick J. Wong 
21825424a0b8SMikulas Patocka 	set_capacity(md->disk, size);
21831da177e4SLinus Torvalds 
2184cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
21852ca3310eSAlasdair G Kergon 
2186f5b4aee1SMike Snitzer 	if (dm_table_request_based(t)) {
218716f12266SMike Snitzer 		/*
21889c37de29SMike Snitzer 		 * Leverage the fact that request-based DM targets are
21899c37de29SMike Snitzer 		 * immutable singletons - used to optimize dm_mq_queue_rq.
219016f12266SMike Snitzer 		 */
219116f12266SMike Snitzer 		md->immutable_target = dm_table_get_immutable_target(t);
2192e6ee8c0bSKiyoshi Ueda 
219329dec90aSChristoph Hellwig 		/*
219429dec90aSChristoph Hellwig 		 * There is no need to reload with request-based dm because the
219529dec90aSChristoph Hellwig 		 * size of front_pad doesn't change.
219629dec90aSChristoph Hellwig 		 *
219729dec90aSChristoph Hellwig 		 * Note for future: If you are to reload bioset, prep-ed
219829dec90aSChristoph Hellwig 		 * requests in the queue may refer to bio from the old bioset,
219929dec90aSChristoph Hellwig 		 * so you must walk through the queue to unprep.
220029dec90aSChristoph Hellwig 		 */
220129dec90aSChristoph Hellwig 		if (!md->mempools) {
220229dec90aSChristoph Hellwig 			md->mempools = t->mempools;
220329dec90aSChristoph Hellwig 			t->mempools = NULL;
220429dec90aSChristoph Hellwig 		}
220529dec90aSChristoph Hellwig 	} else {
220629dec90aSChristoph Hellwig 		/*
220729dec90aSChristoph Hellwig 		 * The md may already have mempools that need changing.
220829dec90aSChristoph Hellwig 		 * If so, reload bioset because front_pad may have changed
220929dec90aSChristoph Hellwig 		 * because a different table was loaded.
221029dec90aSChristoph Hellwig 		 */
221129dec90aSChristoph Hellwig 		dm_free_md_mempools(md->mempools);
221229dec90aSChristoph Hellwig 		md->mempools = t->mempools;
221329dec90aSChristoph Hellwig 		t->mempools = NULL;
22142a2a4c51SJens Axboe 	}
2215e6ee8c0bSKiyoshi Ueda 
2216f5b4aee1SMike Snitzer 	ret = dm_table_set_restrictions(t, md->queue, limits);
2217bb37d772SDamien Le Moal 	if (ret) {
2218bb37d772SDamien Le Moal 		old_map = ERR_PTR(ret);
2219bb37d772SDamien Le Moal 		goto out;
2220bb37d772SDamien Le Moal 	}
2221bb37d772SDamien Le Moal 
2222a12f5d48SEric Dumazet 	old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
22231d3aa6f6SMike Snitzer 	rcu_assign_pointer(md->map, (void *)t);
222436a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
222536a0456fSAlasdair G Kergon 
222641abc4e1SHannes Reinecke 	if (old_map)
222783d5e5b0SMikulas Patocka 		dm_sync_table(md);
22282a2a4c51SJens Axboe out:
2229042d2a9bSAlasdair G Kergon 	return old_map;
22301da177e4SLinus Torvalds }
22311da177e4SLinus Torvalds 
2232a7940155SAlasdair G Kergon /*
2233a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2234a7940155SAlasdair G Kergon  */
2235a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
22361da177e4SLinus Torvalds {
2237a12f5d48SEric Dumazet 	struct dm_table *map = rcu_dereference_protected(md->map, 1);
22381da177e4SLinus Torvalds 
22391da177e4SLinus Torvalds 	if (!map)
2240a7940155SAlasdair G Kergon 		return NULL;
22411da177e4SLinus Torvalds 
22421da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
22439cdb8520SMonam Agarwal 	RCU_INIT_POINTER(md->map, NULL);
224483d5e5b0SMikulas Patocka 	dm_sync_table(md);
2245a7940155SAlasdair G Kergon 
2246a7940155SAlasdair G Kergon 	return map;
22471da177e4SLinus Torvalds }
22481da177e4SLinus Torvalds 
22491da177e4SLinus Torvalds /*
22501da177e4SLinus Torvalds  * Constructor for a new device.
22511da177e4SLinus Torvalds  */
22522b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
22531da177e4SLinus Torvalds {
22541da177e4SLinus Torvalds 	struct mapped_device *md;
22551da177e4SLinus Torvalds 
22562b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
22571da177e4SLinus Torvalds 	if (!md)
22581da177e4SLinus Torvalds 		return -ENXIO;
22591da177e4SLinus Torvalds 
226091ccbbacSTushar Sugandhi 	dm_ima_reset_data(md);
226191ccbbacSTushar Sugandhi 
22621da177e4SLinus Torvalds 	*result = md;
22631da177e4SLinus Torvalds 	return 0;
22641da177e4SLinus Torvalds }
22651da177e4SLinus Torvalds 
2266a5664dadSMike Snitzer /*
2267a5664dadSMike Snitzer  * Functions to manage md->type.
2268a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2269a5664dadSMike Snitzer  */
2270a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2271a5664dadSMike Snitzer {
2272a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2273a5664dadSMike Snitzer }
2274a5664dadSMike Snitzer 
2275a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2276a5664dadSMike Snitzer {
2277a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2278a5664dadSMike Snitzer }
2279a5664dadSMike Snitzer 
22807e0d574fSBart Van Assche void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
2281a5664dadSMike Snitzer {
228200c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2283a5664dadSMike Snitzer 	md->type = type;
2284a5664dadSMike Snitzer }
2285a5664dadSMike Snitzer 
22867e0d574fSBart Van Assche enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
2287a5664dadSMike Snitzer {
2288a5664dadSMike Snitzer 	return md->type;
2289a5664dadSMike Snitzer }
2290a5664dadSMike Snitzer 
229136a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
229236a0456fSAlasdair G Kergon {
229336a0456fSAlasdair G Kergon 	return md->immutable_target_type;
229436a0456fSAlasdair G Kergon }
229536a0456fSAlasdair G Kergon 
22964a0b4ddfSMike Snitzer /*
2297f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
2298f84cb8a4SMike Snitzer  * count on 'md'.
2299f84cb8a4SMike Snitzer  */
2300f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2301f84cb8a4SMike Snitzer {
2302f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
2303f84cb8a4SMike Snitzer 	return &md->queue->limits;
2304f84cb8a4SMike Snitzer }
2305f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2306f84cb8a4SMike Snitzer 
23074a0b4ddfSMike Snitzer /*
23084a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
23094a0b4ddfSMike Snitzer  */
2310591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
23114a0b4ddfSMike Snitzer {
2312ba305859SChristoph Hellwig 	enum dm_queue_mode type = dm_table_get_type(t);
2313c100ec49SMike Snitzer 	struct queue_limits limits;
23141a581b72SChristoph Hellwig 	struct table_device *td;
2315ba305859SChristoph Hellwig 	int r;
2316bfebd1cdSMike Snitzer 
2317545ed20eSToshi Kani 	switch (type) {
2318bfebd1cdSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
2319681cc5e8SMike Snitzer 		md->disk->fops = &dm_rq_blk_dops;
2320e83068a5SMike Snitzer 		r = dm_mq_init_request_queue(md, t);
2321bfebd1cdSMike Snitzer 		if (r) {
2322681cc5e8SMike Snitzer 			DMERR("Cannot initialize queue for request-based dm mapped device");
2323bfebd1cdSMike Snitzer 			return r;
2324bfebd1cdSMike Snitzer 		}
2325bfebd1cdSMike Snitzer 		break;
2326bfebd1cdSMike Snitzer 	case DM_TYPE_BIO_BASED:
2327545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
2328bfebd1cdSMike Snitzer 		break;
23297e0d574fSBart Van Assche 	case DM_TYPE_NONE:
23307e0d574fSBart Van Assche 		WARN_ON_ONCE(true);
23317e0d574fSBart Van Assche 		break;
2332ff36ab34SMike Snitzer 	}
23334a0b4ddfSMike Snitzer 
2334c100ec49SMike Snitzer 	r = dm_calculate_queue_limits(t, &limits);
2335c100ec49SMike Snitzer 	if (r) {
2336c100ec49SMike Snitzer 		DMERR("Cannot calculate initial queue limits");
2337c100ec49SMike Snitzer 		return r;
2338c100ec49SMike Snitzer 	}
2339bb37d772SDamien Le Moal 	r = dm_table_set_restrictions(t, md->queue, &limits);
2340bb37d772SDamien Le Moal 	if (r)
2341bb37d772SDamien Le Moal 		return r;
234289f871afSChristoph Hellwig 
2343d563792cSYu Kuai 	/*
2344d563792cSYu Kuai 	 * Hold lock to make sure add_disk() and del_gendisk() won't concurrent
2345d563792cSYu Kuai 	 * with open_table_device() and close_table_device().
2346d563792cSYu Kuai 	 */
2347d563792cSYu Kuai 	mutex_lock(&md->table_devices_lock);
2348e7089f65SLuis Chamberlain 	r = add_disk(md->disk);
2349d563792cSYu Kuai 	mutex_unlock(&md->table_devices_lock);
2350e7089f65SLuis Chamberlain 	if (r)
2351e7089f65SLuis Chamberlain 		return r;
235289f871afSChristoph Hellwig 
23531a581b72SChristoph Hellwig 	/*
23541a581b72SChristoph Hellwig 	 * Register the holder relationship for devices added before the disk
23551a581b72SChristoph Hellwig 	 * was live.
23561a581b72SChristoph Hellwig 	 */
23571a581b72SChristoph Hellwig 	list_for_each_entry(td, &md->table_devices, list) {
23581a581b72SChristoph Hellwig 		r = bd_link_disk_holder(td->dm_dev.bdev, md->disk);
23591a581b72SChristoph Hellwig 		if (r)
23601a581b72SChristoph Hellwig 			goto out_undo_holders;
23611a581b72SChristoph Hellwig 	}
23621a581b72SChristoph Hellwig 
236389f871afSChristoph Hellwig 	r = dm_sysfs_init(md);
23641a581b72SChristoph Hellwig 	if (r)
23651a581b72SChristoph Hellwig 		goto out_undo_holders;
23661a581b72SChristoph Hellwig 
23671a581b72SChristoph Hellwig 	md->type = type;
23681a581b72SChristoph Hellwig 	return 0;
23691a581b72SChristoph Hellwig 
23701a581b72SChristoph Hellwig out_undo_holders:
23711a581b72SChristoph Hellwig 	list_for_each_entry_continue_reverse(td, &md->table_devices, list)
23721a581b72SChristoph Hellwig 		bd_unlink_disk_holder(td->dm_dev.bdev, md->disk);
2373d563792cSYu Kuai 	mutex_lock(&md->table_devices_lock);
237489f871afSChristoph Hellwig 	del_gendisk(md->disk);
2375d563792cSYu Kuai 	mutex_unlock(&md->table_devices_lock);
237689f871afSChristoph Hellwig 	return r;
237789f871afSChristoph Hellwig }
2378d563792cSYu Kuai 
23792bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev)
23801da177e4SLinus Torvalds {
23811da177e4SLinus Torvalds 	struct mapped_device *md;
238286a3238cSHeinz Mauelshagen 	unsigned int minor = MINOR(dev);
23831da177e4SLinus Torvalds 
23841da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
23851da177e4SLinus Torvalds 		return NULL;
23861da177e4SLinus Torvalds 
2387f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
23881da177e4SLinus Torvalds 
23891da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
239049de5769SMike Snitzer 	if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) ||
239149de5769SMike Snitzer 	    test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2392637842cfSDavid Teigland 		md = NULL;
2393fba9f90eSJeff Mahoney 		goto out;
2394fba9f90eSJeff Mahoney 	}
23952bec1f4aSMikulas Patocka 	dm_get(md);
2396fba9f90eSJeff Mahoney out:
2397f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
23981da177e4SLinus Torvalds 
2399637842cfSDavid Teigland 	return md;
2400637842cfSDavid Teigland }
24013cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2402d229a958SDavid Teigland 
24039ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2404637842cfSDavid Teigland {
24059ade92a9SAlasdair G Kergon 	return md->interface_ptr;
24061da177e4SLinus Torvalds }
24071da177e4SLinus Torvalds 
24081da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
24091da177e4SLinus Torvalds {
24101da177e4SLinus Torvalds 	md->interface_ptr = ptr;
24111da177e4SLinus Torvalds }
24121da177e4SLinus Torvalds 
24131da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
24141da177e4SLinus Torvalds {
24151da177e4SLinus Torvalds 	atomic_inc(&md->holders);
24163f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
24171da177e4SLinus Torvalds }
24181da177e4SLinus Torvalds 
241909ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md)
242009ee96b2SMikulas Patocka {
242109ee96b2SMikulas Patocka 	spin_lock(&_minor_lock);
242209ee96b2SMikulas Patocka 	if (test_bit(DMF_FREEING, &md->flags)) {
242309ee96b2SMikulas Patocka 		spin_unlock(&_minor_lock);
242409ee96b2SMikulas Patocka 		return -EBUSY;
242509ee96b2SMikulas Patocka 	}
242609ee96b2SMikulas Patocka 	dm_get(md);
242709ee96b2SMikulas Patocka 	spin_unlock(&_minor_lock);
242809ee96b2SMikulas Patocka 	return 0;
242909ee96b2SMikulas Patocka }
243009ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold);
243109ee96b2SMikulas Patocka 
243272d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
243372d94861SAlasdair G Kergon {
243472d94861SAlasdair G Kergon 	return md->name;
243572d94861SAlasdair G Kergon }
243672d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
243772d94861SAlasdair G Kergon 
24383f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
24391da177e4SLinus Torvalds {
24401134e5aeSMike Anderson 	struct dm_table *map;
244183d5e5b0SMikulas Patocka 	int srcu_idx;
24421da177e4SLinus Torvalds 
24433f77316dSKiyoshi Ueda 	might_sleep();
2444fba9f90eSJeff Mahoney 
244563a4f065SMike Snitzer 	spin_lock(&_minor_lock);
24463f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2447fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2448f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
24493f77316dSKiyoshi Ueda 
24507a5428dcSChristoph Hellwig 	blk_mark_disk_dead(md->disk);
24513b785fbcSBart Van Assche 
2452ab7c7bb6SMikulas Patocka 	/*
2453ab7c7bb6SMikulas Patocka 	 * Take suspend_lock so that presuspend and postsuspend methods
2454ab7c7bb6SMikulas Patocka 	 * do not race with internal suspend.
2455ab7c7bb6SMikulas Patocka 	 */
2456ab7c7bb6SMikulas Patocka 	mutex_lock(&md->suspend_lock);
24572a708cffSJunichi Nomura 	map = dm_get_live_table(md, &srcu_idx);
24584f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
24591da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
2460adc0daadSMikulas Patocka 		set_bit(DMF_SUSPENDED, &md->flags);
24615df96f2bSMikulas Patocka 		set_bit(DMF_POST_SUSPENDING, &md->flags);
24621da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
24631da177e4SLinus Torvalds 	}
2464238d991fSHeinz Mauelshagen 	/* dm_put_live_table must be before fsleep, otherwise deadlock is possible */
246583d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
24662a708cffSJunichi Nomura 	mutex_unlock(&md->suspend_lock);
246783d5e5b0SMikulas Patocka 
24683f77316dSKiyoshi Ueda 	/*
24693f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
24703f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
24713f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
24723f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
24733f77316dSKiyoshi Ueda 	 */
24743f77316dSKiyoshi Ueda 	if (wait)
24753f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
2476238d991fSHeinz Mauelshagen 			fsleep(1000);
24773f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
24783f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
24793f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
24803f77316dSKiyoshi Ueda 
2481a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
24821da177e4SLinus Torvalds 	free_dev(md);
24831da177e4SLinus Torvalds }
24843f77316dSKiyoshi Ueda 
24853f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
24863f77316dSKiyoshi Ueda {
24873f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
24883f77316dSKiyoshi Ueda }
24893f77316dSKiyoshi Ueda 
24903f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
24913f77316dSKiyoshi Ueda {
24923f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
24933f77316dSKiyoshi Ueda }
24943f77316dSKiyoshi Ueda 
24953f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
24963f77316dSKiyoshi Ueda {
24973f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
24981da177e4SLinus Torvalds }
249979eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
25001da177e4SLinus Torvalds 
25019f6dc633SMike Snitzer static bool dm_in_flight_bios(struct mapped_device *md)
250285067747SMing Lei {
250385067747SMing Lei 	int cpu;
25049f6dc633SMike Snitzer 	unsigned long sum = 0;
250585067747SMing Lei 
25069f6dc633SMike Snitzer 	for_each_possible_cpu(cpu)
25079f6dc633SMike Snitzer 		sum += *per_cpu_ptr(md->pending_io, cpu);
250885067747SMing Lei 
250985067747SMing Lei 	return sum != 0;
251085067747SMing Lei }
251185067747SMing Lei 
25122f064a59SPeter Zijlstra static int dm_wait_for_bios_completion(struct mapped_device *md, unsigned int task_state)
251346125c1cSMilan Broz {
251446125c1cSMilan Broz 	int r = 0;
25159f4c3f87SBart Van Assche 	DEFINE_WAIT(wait);
251646125c1cSMilan Broz 
251785067747SMing Lei 	while (true) {
25189f4c3f87SBart Van Assche 		prepare_to_wait(&md->wait, &wait, task_state);
251946125c1cSMilan Broz 
25209f6dc633SMike Snitzer 		if (!dm_in_flight_bios(md))
252146125c1cSMilan Broz 			break;
252246125c1cSMilan Broz 
2523e3fabdfdSBart Van Assche 		if (signal_pending_state(task_state, current)) {
252446125c1cSMilan Broz 			r = -EINTR;
252546125c1cSMilan Broz 			break;
252646125c1cSMilan Broz 		}
252746125c1cSMilan Broz 
252846125c1cSMilan Broz 		io_schedule();
252946125c1cSMilan Broz 	}
25309f4c3f87SBart Van Assche 	finish_wait(&md->wait, &wait);
2531b44ebeb0SMikulas Patocka 
25329f6dc633SMike Snitzer 	smp_rmb();
25339f6dc633SMike Snitzer 
253446125c1cSMilan Broz 	return r;
253546125c1cSMilan Broz }
253646125c1cSMilan Broz 
25372f064a59SPeter Zijlstra static int dm_wait_for_completion(struct mapped_device *md, unsigned int task_state)
253885067747SMing Lei {
253985067747SMing Lei 	int r = 0;
254085067747SMing Lei 
254185067747SMing Lei 	if (!queue_is_mq(md->queue))
254285067747SMing Lei 		return dm_wait_for_bios_completion(md, task_state);
254385067747SMing Lei 
254485067747SMing Lei 	while (true) {
254585067747SMing Lei 		if (!blk_mq_queue_inflight(md->queue))
254685067747SMing Lei 			break;
254785067747SMing Lei 
254885067747SMing Lei 		if (signal_pending_state(task_state, current)) {
254985067747SMing Lei 			r = -EINTR;
255085067747SMing Lei 			break;
255185067747SMing Lei 		}
255285067747SMing Lei 
2553238d991fSHeinz Mauelshagen 		fsleep(5000);
255485067747SMing Lei 	}
255585067747SMing Lei 
255685067747SMing Lei 	return r;
255785067747SMing Lei }
255885067747SMing Lei 
25591da177e4SLinus Torvalds /*
25601da177e4SLinus Torvalds  * Process the deferred bios
25611da177e4SLinus Torvalds  */
2562ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
25631da177e4SLinus Torvalds {
25640c2915b8SMike Snitzer 	struct mapped_device *md = container_of(work, struct mapped_device, work);
25650c2915b8SMike Snitzer 	struct bio *bio;
2566ef208587SMikulas Patocka 
25673b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2568022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
25690c2915b8SMike Snitzer 		bio = bio_list_pop(&md->deferred);
2570022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2571022c2611SMikulas Patocka 
25720c2915b8SMike Snitzer 		if (!bio)
2573df12ee99SAlasdair G Kergon 			break;
257473d410c0SMilan Broz 
25750c2915b8SMike Snitzer 		submit_bio_noacct(bio);
25760ca44fceSPingfan Liu 		cond_resched();
2577e6ee8c0bSKiyoshi Ueda 	}
25781da177e4SLinus Torvalds }
25791da177e4SLinus Torvalds 
25809a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2581304f3f6aSMilan Broz {
25823b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
25834e857c58SPeter Zijlstra 	smp_mb__after_atomic();
258453d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2585304f3f6aSMilan Broz }
2586304f3f6aSMilan Broz 
25871da177e4SLinus Torvalds /*
2588042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
25891da177e4SLinus Torvalds  */
2590042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
25911da177e4SLinus Torvalds {
259287eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2593754c5fc7SMike Snitzer 	struct queue_limits limits;
2594042d2a9bSAlasdair G Kergon 	int r;
25951da177e4SLinus Torvalds 
2596e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
25971da177e4SLinus Torvalds 
25981da177e4SLinus Torvalds 	/* device must be suspended */
25994f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
260093c534aeSAlasdair G Kergon 		goto out;
26011da177e4SLinus Torvalds 
26023ae70656SMike Snitzer 	/*
26033ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
26043ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
26053ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
26063ae70656SMike Snitzer 	 * reappear.
26073ae70656SMike Snitzer 	 */
26083ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
260983d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
26103ae70656SMike Snitzer 		if (live_map)
26113ae70656SMike Snitzer 			limits = md->queue->limits;
261283d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
26133ae70656SMike Snitzer 	}
26143ae70656SMike Snitzer 
261587eb5b21SMike Christie 	if (!live_map) {
2616754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2617042d2a9bSAlasdair G Kergon 		if (r) {
2618042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2619754c5fc7SMike Snitzer 			goto out;
2620042d2a9bSAlasdair G Kergon 		}
262187eb5b21SMike Christie 	}
2622754c5fc7SMike Snitzer 
2623042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
262462e08243SMikulas Patocka 	dm_issue_global_event();
26251da177e4SLinus Torvalds 
262693c534aeSAlasdair G Kergon out:
2627e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2628042d2a9bSAlasdair G Kergon 	return map;
26291da177e4SLinus Torvalds }
26301da177e4SLinus Torvalds 
26311da177e4SLinus Torvalds /*
26321da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
26331da177e4SLinus Torvalds  * device.
26341da177e4SLinus Torvalds  */
26352ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
26361da177e4SLinus Torvalds {
2637e39e2e95SAlasdair G Kergon 	int r;
26381da177e4SLinus Torvalds 
2639040f04bdSChristoph Hellwig 	WARN_ON(test_bit(DMF_FROZEN, &md->flags));
2640dfbe03f6SAlasdair G Kergon 
2641977115c0SChristoph Hellwig 	r = freeze_bdev(md->disk->part0);
2642040f04bdSChristoph Hellwig 	if (!r)
2643aa8d7c2fSAlasdair G Kergon 		set_bit(DMF_FROZEN, &md->flags);
2644040f04bdSChristoph Hellwig 	return r;
26451da177e4SLinus Torvalds }
26461da177e4SLinus Torvalds 
26472ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
26481da177e4SLinus Torvalds {
2649aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2650aa8d7c2fSAlasdair G Kergon 		return;
2651977115c0SChristoph Hellwig 	thaw_bdev(md->disk->part0);
2652aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
26531da177e4SLinus Torvalds }
26541da177e4SLinus Torvalds 
26551da177e4SLinus Torvalds /*
2656b48633f8SBart Van Assche  * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
2657b48633f8SBart Van Assche  * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
2658b48633f8SBart Van Assche  * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
2659b48633f8SBart Van Assche  *
2660ffcc3936SMike Snitzer  * If __dm_suspend returns 0, the device is completely quiescent
2661ffcc3936SMike Snitzer  * now. There is no request-processing activity. All new requests
2662ffcc3936SMike Snitzer  * are being added to md->deferred list.
2663cec47e3dSKiyoshi Ueda  */
2664ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
266586a3238cSHeinz Mauelshagen 			unsigned int suspend_flags, unsigned int task_state,
2666eaf9a736SMike Snitzer 			int dmf_suspended_flag)
26671da177e4SLinus Torvalds {
2668ffcc3936SMike Snitzer 	bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2669ffcc3936SMike Snitzer 	bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2670ffcc3936SMike Snitzer 	int r;
2671cf222b37SAlasdair G Kergon 
26725a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
26735a8f1f80SBart Van Assche 
26742e93ccc1SKiyoshi Ueda 	/*
26752e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
26762e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
26772e93ccc1SKiyoshi Ueda 	 */
26782e93ccc1SKiyoshi Ueda 	if (noflush)
26792e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
268086331f39SBart Van Assche 	else
2681ac75b09fSMike Snitzer 		DMDEBUG("%s: suspending with flush", dm_device_name(md));
26822e93ccc1SKiyoshi Ueda 
2683d67ee213SMike Snitzer 	/*
2684d67ee213SMike Snitzer 	 * This gets reverted if there's an error later and the targets
2685d67ee213SMike Snitzer 	 * provide the .presuspend_undo hook.
2686d67ee213SMike Snitzer 	 */
26871da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
26881da177e4SLinus Torvalds 
26892e93ccc1SKiyoshi Ueda 	/*
26909f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
26919f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
26929f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
26939f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
26942e93ccc1SKiyoshi Ueda 	 */
269532a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
26962ca3310eSAlasdair G Kergon 		r = lock_fs(md);
2697d67ee213SMike Snitzer 		if (r) {
2698d67ee213SMike Snitzer 			dm_table_presuspend_undo_targets(map);
2699ffcc3936SMike Snitzer 			return r;
2700aa8d7c2fSAlasdair G Kergon 		}
2701d67ee213SMike Snitzer 	}
27021da177e4SLinus Torvalds 
27031da177e4SLinus Torvalds 	/*
27043b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
27053b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
270696c9865cSMike Snitzer 	 * dm_split_and_process_bio from dm_submit_bio.
27073b00b203SMikulas Patocka 	 *
270896c9865cSMike Snitzer 	 * To get all processes out of dm_split_and_process_bio in dm_submit_bio,
27093b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
271096c9865cSMike Snitzer 	 * dm_split_and_process_bio from dm_submit_bio and quiesce the thread
27110cede372SMike Snitzer 	 * (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND and call
27126a8736d1STejun Heo 	 * flush_workqueue(md->wq).
27131da177e4SLinus Torvalds 	 */
27141eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
271541abc4e1SHannes Reinecke 	if (map)
271683d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
27171da177e4SLinus Torvalds 
2718d0bcb878SKiyoshi Ueda 	/*
271929e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
272029e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2721d0bcb878SKiyoshi Ueda 	 */
27226a23e05cSJens Axboe 	if (dm_request_based(md))
2723eca7ee6dSMike Snitzer 		dm_stop_queue(md->queue);
2724cec47e3dSKiyoshi Ueda 
2725d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2726d0bcb878SKiyoshi Ueda 
27271da177e4SLinus Torvalds 	/*
27283b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
27293b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
27303b00b203SMikulas Patocka 	 * to finish.
27311da177e4SLinus Torvalds 	 */
2732b48633f8SBart Van Assche 	r = dm_wait_for_completion(md, task_state);
2733eaf9a736SMike Snitzer 	if (!r)
2734eaf9a736SMike Snitzer 		set_bit(dmf_suspended_flag, &md->flags);
27351da177e4SLinus Torvalds 
27366d6f10dfSMilan Broz 	if (noflush)
2737022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
273841abc4e1SHannes Reinecke 	if (map)
273983d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
27402e93ccc1SKiyoshi Ueda 
27411da177e4SLinus Torvalds 	/* were we interrupted ? */
274246125c1cSMilan Broz 	if (r < 0) {
27439a1fb464SMikulas Patocka 		dm_queue_flush(md);
274473d410c0SMilan Broz 
2745cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
2746eca7ee6dSMike Snitzer 			dm_start_queue(md->queue);
2747cec47e3dSKiyoshi Ueda 
27482ca3310eSAlasdair G Kergon 		unlock_fs(md);
2749d67ee213SMike Snitzer 		dm_table_presuspend_undo_targets(map);
2750ffcc3936SMike Snitzer 		/* pushback list is already flushed, so skip flush */
2751ffcc3936SMike Snitzer 	}
2752ffcc3936SMike Snitzer 
2753ffcc3936SMike Snitzer 	return r;
27542ca3310eSAlasdair G Kergon }
27552ca3310eSAlasdair G Kergon 
27563b00b203SMikulas Patocka /*
2757ffcc3936SMike Snitzer  * We need to be able to change a mapping table under a mounted
2758ffcc3936SMike Snitzer  * filesystem.  For example we might want to move some data in
2759ffcc3936SMike Snitzer  * the background.  Before the table can be swapped with
2760ffcc3936SMike Snitzer  * dm_bind_table, dm_suspend must be called to flush any in
2761ffcc3936SMike Snitzer  * flight bios and ensure that any further io gets deferred.
27623b00b203SMikulas Patocka  */
2763ffcc3936SMike Snitzer /*
2764ffcc3936SMike Snitzer  * Suspend mechanism in request-based dm.
2765ffcc3936SMike Snitzer  *
2766ffcc3936SMike Snitzer  * 1. Flush all I/Os by lock_fs() if needed.
2767ffcc3936SMike Snitzer  * 2. Stop dispatching any I/O by stopping the request_queue.
2768ffcc3936SMike Snitzer  * 3. Wait for all in-flight I/Os to be completed or requeued.
2769ffcc3936SMike Snitzer  *
2770ffcc3936SMike Snitzer  * To abort suspend, start the request_queue.
2771ffcc3936SMike Snitzer  */
277286a3238cSHeinz Mauelshagen int dm_suspend(struct mapped_device *md, unsigned int suspend_flags)
2773ffcc3936SMike Snitzer {
2774ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2775ffcc3936SMike Snitzer 	int r = 0;
2776ffcc3936SMike Snitzer 
2777ffcc3936SMike Snitzer retry:
2778ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2779ffcc3936SMike Snitzer 
2780ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2781ffcc3936SMike Snitzer 		r = -EINVAL;
2782ffcc3936SMike Snitzer 		goto out_unlock;
2783ffcc3936SMike Snitzer 	}
2784ffcc3936SMike Snitzer 
2785ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2786ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2787ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2788ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2789ffcc3936SMike Snitzer 		if (r)
2790ffcc3936SMike Snitzer 			return r;
2791ffcc3936SMike Snitzer 		goto retry;
2792ffcc3936SMike Snitzer 	}
2793ffcc3936SMike Snitzer 
2794a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2795ffcc3936SMike Snitzer 
2796eaf9a736SMike Snitzer 	r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
2797ffcc3936SMike Snitzer 	if (r)
2798ffcc3936SMike Snitzer 		goto out_unlock;
27993b00b203SMikulas Patocka 
28005df96f2bSMikulas Patocka 	set_bit(DMF_POST_SUSPENDING, &md->flags);
28014d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
28025df96f2bSMikulas Patocka 	clear_bit(DMF_POST_SUSPENDING, &md->flags);
28034d4471cbSKiyoshi Ueda 
2804d287483dSAlasdair G Kergon out_unlock:
2805e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2806cf222b37SAlasdair G Kergon 	return r;
28071da177e4SLinus Torvalds }
28081da177e4SLinus Torvalds 
2809ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map)
28101da177e4SLinus Torvalds {
2811ffcc3936SMike Snitzer 	if (map) {
2812ffcc3936SMike Snitzer 		int r = dm_table_resume_targets(map);
2813b30f1607SHeinz Mauelshagen 
28148757b776SMilan Broz 		if (r)
2815ffcc3936SMike Snitzer 			return r;
2816ffcc3936SMike Snitzer 	}
28172ca3310eSAlasdair G Kergon 
28189a1fb464SMikulas Patocka 	dm_queue_flush(md);
28192ca3310eSAlasdair G Kergon 
2820cec47e3dSKiyoshi Ueda 	/*
2821cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2822cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2823cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2824cec47e3dSKiyoshi Ueda 	 */
2825cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2826eca7ee6dSMike Snitzer 		dm_start_queue(md->queue);
2827cec47e3dSKiyoshi Ueda 
28282ca3310eSAlasdair G Kergon 	unlock_fs(md);
28292ca3310eSAlasdair G Kergon 
2830ffcc3936SMike Snitzer 	return 0;
2831ffcc3936SMike Snitzer }
2832ffcc3936SMike Snitzer 
2833ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md)
2834ffcc3936SMike Snitzer {
28358dc23658SMinfei Huang 	int r;
2836ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2837ffcc3936SMike Snitzer 
2838ffcc3936SMike Snitzer retry:
28398dc23658SMinfei Huang 	r = -EINVAL;
2840ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2841ffcc3936SMike Snitzer 
2842ffcc3936SMike Snitzer 	if (!dm_suspended_md(md))
2843ffcc3936SMike Snitzer 		goto out;
2844ffcc3936SMike Snitzer 
2845ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2846ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2847ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2848ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2849ffcc3936SMike Snitzer 		if (r)
2850ffcc3936SMike Snitzer 			return r;
2851ffcc3936SMike Snitzer 		goto retry;
2852ffcc3936SMike Snitzer 	}
2853ffcc3936SMike Snitzer 
2854a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2855ffcc3936SMike Snitzer 	if (!map || !dm_table_get_size(map))
2856ffcc3936SMike Snitzer 		goto out;
2857ffcc3936SMike Snitzer 
2858ffcc3936SMike Snitzer 	r = __dm_resume(md, map);
2859ffcc3936SMike Snitzer 	if (r)
2860ffcc3936SMike Snitzer 		goto out;
2861ffcc3936SMike Snitzer 
28622ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
2863cf222b37SAlasdair G Kergon out:
2864e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
28652ca3310eSAlasdair G Kergon 
2866cf222b37SAlasdair G Kergon 	return r;
28671da177e4SLinus Torvalds }
28681da177e4SLinus Torvalds 
2869fd2ed4d2SMikulas Patocka /*
2870fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
2871fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
2872fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
2873fd2ed4d2SMikulas Patocka  */
2874fd2ed4d2SMikulas Patocka 
287586a3238cSHeinz Mauelshagen static void __dm_internal_suspend(struct mapped_device *md, unsigned int suspend_flags)
2876ffcc3936SMike Snitzer {
2877ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2878ffcc3936SMike Snitzer 
28791ea0654eSBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
28801ea0654eSBart Van Assche 
288196b26c8cSMikulas Patocka 	if (md->internal_suspend_count++)
2882ffcc3936SMike Snitzer 		return; /* nested internal suspend */
2883ffcc3936SMike Snitzer 
2884ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2885ffcc3936SMike Snitzer 		set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2886ffcc3936SMike Snitzer 		return; /* nest suspend */
2887ffcc3936SMike Snitzer 	}
2888ffcc3936SMike Snitzer 
2889a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2890ffcc3936SMike Snitzer 
2891ffcc3936SMike Snitzer 	/*
2892ffcc3936SMike Snitzer 	 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2893ffcc3936SMike Snitzer 	 * supported.  Properly supporting a TASK_INTERRUPTIBLE internal suspend
2894ffcc3936SMike Snitzer 	 * would require changing .presuspend to return an error -- avoid this
2895ffcc3936SMike Snitzer 	 * until there is a need for more elaborate variants of internal suspend.
2896ffcc3936SMike Snitzer 	 */
2897eaf9a736SMike Snitzer 	(void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
2898eaf9a736SMike Snitzer 			    DMF_SUSPENDED_INTERNALLY);
2899ffcc3936SMike Snitzer 
29005df96f2bSMikulas Patocka 	set_bit(DMF_POST_SUSPENDING, &md->flags);
2901ffcc3936SMike Snitzer 	dm_table_postsuspend_targets(map);
29025df96f2bSMikulas Patocka 	clear_bit(DMF_POST_SUSPENDING, &md->flags);
2903ffcc3936SMike Snitzer }
2904ffcc3936SMike Snitzer 
2905ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md)
2906ffcc3936SMike Snitzer {
290796b26c8cSMikulas Patocka 	BUG_ON(!md->internal_suspend_count);
290896b26c8cSMikulas Patocka 
290996b26c8cSMikulas Patocka 	if (--md->internal_suspend_count)
2910ffcc3936SMike Snitzer 		return; /* resume from nested internal suspend */
2911ffcc3936SMike Snitzer 
2912ffcc3936SMike Snitzer 	if (dm_suspended_md(md))
2913ffcc3936SMike Snitzer 		goto done; /* resume from nested suspend */
2914ffcc3936SMike Snitzer 
2915ffcc3936SMike Snitzer 	/*
2916ffcc3936SMike Snitzer 	 * NOTE: existing callers don't need to call dm_table_resume_targets
2917ffcc3936SMike Snitzer 	 * (which may fail -- so best to avoid it for now by passing NULL map)
2918ffcc3936SMike Snitzer 	 */
2919ffcc3936SMike Snitzer 	(void) __dm_resume(md, NULL);
2920ffcc3936SMike Snitzer 
2921ffcc3936SMike Snitzer done:
2922ffcc3936SMike Snitzer 	clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2923ffcc3936SMike Snitzer 	smp_mb__after_atomic();
2924ffcc3936SMike Snitzer 	wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2925ffcc3936SMike Snitzer }
2926ffcc3936SMike Snitzer 
2927ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md)
2928fd2ed4d2SMikulas Patocka {
2929fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
2930ffcc3936SMike Snitzer 	__dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2931ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2932ffcc3936SMike Snitzer }
2933ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
2934ffcc3936SMike Snitzer 
2935ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md)
2936ffcc3936SMike Snitzer {
2937ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2938ffcc3936SMike Snitzer 	__dm_internal_resume(md);
2939ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2940ffcc3936SMike Snitzer }
2941ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume);
2942ffcc3936SMike Snitzer 
2943ffcc3936SMike Snitzer /*
2944ffcc3936SMike Snitzer  * Fast variants of internal suspend/resume hold md->suspend_lock,
2945ffcc3936SMike Snitzer  * which prevents interaction with userspace-driven suspend.
2946ffcc3936SMike Snitzer  */
2947ffcc3936SMike Snitzer 
2948ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md)
2949ffcc3936SMike Snitzer {
2950ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2951ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2952fd2ed4d2SMikulas Patocka 		return;
2953fd2ed4d2SMikulas Patocka 
2954fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2955fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
2956fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
2957fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2958fd2ed4d2SMikulas Patocka }
2959b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
2960fd2ed4d2SMikulas Patocka 
2961ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md)
2962fd2ed4d2SMikulas Patocka {
2963ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2964fd2ed4d2SMikulas Patocka 		goto done;
2965fd2ed4d2SMikulas Patocka 
2966fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
2967fd2ed4d2SMikulas Patocka 
2968fd2ed4d2SMikulas Patocka done:
2969fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
2970fd2ed4d2SMikulas Patocka }
2971b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
2972fd2ed4d2SMikulas Patocka 
2973a4a82ce3SHeinz Mauelshagen /*
2974a4a82ce3SHeinz Mauelshagen  *---------------------------------------------------------------
29751da177e4SLinus Torvalds  * Event notification.
2976a4a82ce3SHeinz Mauelshagen  *---------------------------------------------------------------
2977a4a82ce3SHeinz Mauelshagen  */
29783abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
297986a3238cSHeinz Mauelshagen 		      unsigned int cookie, bool need_resize_uevent)
298069267a30SAlasdair G Kergon {
29816958c1c6SMikulas Patocka 	int r;
298286a3238cSHeinz Mauelshagen 	unsigned int noio_flag;
298360935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
29847533afa1SMikulas Patocka 	char *envp[3] = { NULL, NULL, NULL };
29857533afa1SMikulas Patocka 	char **envpp = envp;
29867533afa1SMikulas Patocka 	if (cookie) {
29877533afa1SMikulas Patocka 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
29887533afa1SMikulas Patocka 			 DM_COOKIE_ENV_VAR_NAME, cookie);
29897533afa1SMikulas Patocka 		*envpp++ = udev_cookie;
29907533afa1SMikulas Patocka 	}
29917533afa1SMikulas Patocka 	if (need_resize_uevent) {
29927533afa1SMikulas Patocka 		*envpp++ = "RESIZE=1";
29937533afa1SMikulas Patocka 	}
299460935eb2SMilan Broz 
29956958c1c6SMikulas Patocka 	noio_flag = memalloc_noio_save();
29966958c1c6SMikulas Patocka 
29977533afa1SMikulas Patocka 	r = kobject_uevent_env(&disk_to_dev(md->disk)->kobj, action, envp);
29986958c1c6SMikulas Patocka 
29996958c1c6SMikulas Patocka 	memalloc_noio_restore(noio_flag);
30006958c1c6SMikulas Patocka 
30016958c1c6SMikulas Patocka 	return r;
300269267a30SAlasdair G Kergon }
300369267a30SAlasdair G Kergon 
30047a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
30057a8c3d3bSMike Anderson {
30067a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
30077a8c3d3bSMike Anderson }
30087a8c3d3bSMike Anderson 
30091da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
30101da177e4SLinus Torvalds {
30111da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
30121da177e4SLinus Torvalds }
30131da177e4SLinus Torvalds 
30141da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
30151da177e4SLinus Torvalds {
30161da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
30171da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
30181da177e4SLinus Torvalds }
30191da177e4SLinus Torvalds 
30207a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
30217a8c3d3bSMike Anderson {
30227a8c3d3bSMike Anderson 	unsigned long flags;
30237a8c3d3bSMike Anderson 
30247a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
30257a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
30267a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
30277a8c3d3bSMike Anderson }
30287a8c3d3bSMike Anderson 
30291da177e4SLinus Torvalds /*
30301da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
30311da177e4SLinus Torvalds  * count on 'md'.
30321da177e4SLinus Torvalds  */
30331da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
30341da177e4SLinus Torvalds {
30351da177e4SLinus Torvalds 	return md->disk;
30361da177e4SLinus Torvalds }
303765ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk);
30381da177e4SLinus Torvalds 
3039784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
3040784aae73SMilan Broz {
30412995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
3042784aae73SMilan Broz }
3043784aae73SMilan Broz 
3044784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
3045784aae73SMilan Broz {
3046784aae73SMilan Broz 	struct mapped_device *md;
3047784aae73SMilan Broz 
30482995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
3049784aae73SMilan Broz 
3050b9a41d21SHou Tao 	spin_lock(&_minor_lock);
3051b9a41d21SHou Tao 	if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
3052b9a41d21SHou Tao 		md = NULL;
3053b9a41d21SHou Tao 		goto out;
3054b9a41d21SHou Tao 	}
3055784aae73SMilan Broz 	dm_get(md);
3056b9a41d21SHou Tao out:
3057b9a41d21SHou Tao 	spin_unlock(&_minor_lock);
3058b9a41d21SHou Tao 
3059784aae73SMilan Broz 	return md;
3060784aae73SMilan Broz }
3061784aae73SMilan Broz 
30624f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
30631da177e4SLinus Torvalds {
30641da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
30651da177e4SLinus Torvalds }
30661da177e4SLinus Torvalds 
30675df96f2bSMikulas Patocka static int dm_post_suspending_md(struct mapped_device *md)
30685df96f2bSMikulas Patocka {
30695df96f2bSMikulas Patocka 	return test_bit(DMF_POST_SUSPENDING, &md->flags);
30705df96f2bSMikulas Patocka }
30715df96f2bSMikulas Patocka 
3072ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md)
3073ffcc3936SMike Snitzer {
3074ffcc3936SMike Snitzer 	return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
3075ffcc3936SMike Snitzer }
3076ffcc3936SMike Snitzer 
30772c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
30782c140a24SMikulas Patocka {
30792c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
30802c140a24SMikulas Patocka }
30812c140a24SMikulas Patocka 
308264dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
308364dbce58SKiyoshi Ueda {
308433bd6f06SMike Snitzer 	return dm_suspended_md(ti->table->md);
308564dbce58SKiyoshi Ueda }
308664dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
308764dbce58SKiyoshi Ueda 
30885df96f2bSMikulas Patocka int dm_post_suspending(struct dm_target *ti)
30895df96f2bSMikulas Patocka {
309033bd6f06SMike Snitzer 	return dm_post_suspending_md(ti->table->md);
30915df96f2bSMikulas Patocka }
30925df96f2bSMikulas Patocka EXPORT_SYMBOL_GPL(dm_post_suspending);
30935df96f2bSMikulas Patocka 
30942e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
30952e93ccc1SKiyoshi Ueda {
309633bd6f06SMike Snitzer 	return __noflush_suspending(ti->table->md);
30972e93ccc1SKiyoshi Ueda }
30982e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
30992e93ccc1SKiyoshi Ueda 
3100e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
3101e6ee8c0bSKiyoshi Ueda {
3102e6ee8c0bSKiyoshi Ueda 	if (!pools)
3103e6ee8c0bSKiyoshi Ueda 		return;
3104e6ee8c0bSKiyoshi Ueda 
31056f1c819cSKent Overstreet 	bioset_exit(&pools->bs);
31066f1c819cSKent Overstreet 	bioset_exit(&pools->io_bs);
3107e6ee8c0bSKiyoshi Ueda 
3108e6ee8c0bSKiyoshi Ueda 	kfree(pools);
3109e6ee8c0bSKiyoshi Ueda }
3110e6ee8c0bSKiyoshi Ueda 
31119c72bad1SChristoph Hellwig struct dm_pr {
31129c72bad1SChristoph Hellwig 	u64	old_key;
31139c72bad1SChristoph Hellwig 	u64	new_key;
31149c72bad1SChristoph Hellwig 	u32	flags;
3115c6adada5SMike Christie 	bool	abort;
31169c72bad1SChristoph Hellwig 	bool	fail_early;
31178dd87f3cSMike Christie 	int	ret;
311870151087SMike Christie 	enum pr_type type;
31199c72bad1SChristoph Hellwig };
31209c72bad1SChristoph Hellwig 
31219c72bad1SChristoph Hellwig static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
31228dd87f3cSMike Christie 		      struct dm_pr *pr)
31239c72bad1SChristoph Hellwig {
31249c72bad1SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
31259c72bad1SChristoph Hellwig 	struct dm_table *table;
31269c72bad1SChristoph Hellwig 	struct dm_target *ti;
31279c72bad1SChristoph Hellwig 	int ret = -ENOTTY, srcu_idx;
31289c72bad1SChristoph Hellwig 
31299c72bad1SChristoph Hellwig 	table = dm_get_live_table(md, &srcu_idx);
31309c72bad1SChristoph Hellwig 	if (!table || !dm_table_get_size(table))
31319c72bad1SChristoph Hellwig 		goto out;
31329c72bad1SChristoph Hellwig 
31339c72bad1SChristoph Hellwig 	/* We only support devices that have a single target */
31342aec377aSMike Snitzer 	if (table->num_targets != 1)
31359c72bad1SChristoph Hellwig 		goto out;
31369c72bad1SChristoph Hellwig 	ti = dm_table_get_target(table, 0);
31379c72bad1SChristoph Hellwig 
3138e120a5f1SMike Snitzer 	if (dm_suspended_md(md)) {
3139e120a5f1SMike Snitzer 		ret = -EAGAIN;
3140e120a5f1SMike Snitzer 		goto out;
3141e120a5f1SMike Snitzer 	}
3142e120a5f1SMike Snitzer 
31439c72bad1SChristoph Hellwig 	ret = -EINVAL;
31449c72bad1SChristoph Hellwig 	if (!ti->type->iterate_devices)
31459c72bad1SChristoph Hellwig 		goto out;
31469c72bad1SChristoph Hellwig 
31478dd87f3cSMike Christie 	ti->type->iterate_devices(ti, fn, pr);
31488dd87f3cSMike Christie 	ret = 0;
31499c72bad1SChristoph Hellwig out:
31509c72bad1SChristoph Hellwig 	dm_put_live_table(md, srcu_idx);
31519c72bad1SChristoph Hellwig 	return ret;
31529c72bad1SChristoph Hellwig }
31539c72bad1SChristoph Hellwig 
31549c72bad1SChristoph Hellwig /*
31559c72bad1SChristoph Hellwig  * For register / unregister we need to manually call out to every path.
31569c72bad1SChristoph Hellwig  */
31579c72bad1SChristoph Hellwig static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
31589c72bad1SChristoph Hellwig 			    sector_t start, sector_t len, void *data)
31599c72bad1SChristoph Hellwig {
31609c72bad1SChristoph Hellwig 	struct dm_pr *pr = data;
31619c72bad1SChristoph Hellwig 	const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
31628dd87f3cSMike Christie 	int ret;
31639c72bad1SChristoph Hellwig 
31648dd87f3cSMike Christie 	if (!ops || !ops->pr_register) {
31658dd87f3cSMike Christie 		pr->ret = -EOPNOTSUPP;
31668dd87f3cSMike Christie 		return -1;
31678dd87f3cSMike Christie 	}
31688dd87f3cSMike Christie 
31698dd87f3cSMike Christie 	ret = ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
31708dd87f3cSMike Christie 	if (!ret)
31718dd87f3cSMike Christie 		return 0;
31728dd87f3cSMike Christie 
31738dd87f3cSMike Christie 	if (!pr->ret)
31748dd87f3cSMike Christie 		pr->ret = ret;
31758dd87f3cSMike Christie 
31768dd87f3cSMike Christie 	if (pr->fail_early)
31778dd87f3cSMike Christie 		return -1;
31788dd87f3cSMike Christie 
31798dd87f3cSMike Christie 	return 0;
31809c72bad1SChristoph Hellwig }
31819c72bad1SChristoph Hellwig 
318271cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
318371cdb697SChristoph Hellwig 			  u32 flags)
318471cdb697SChristoph Hellwig {
31859c72bad1SChristoph Hellwig 	struct dm_pr pr = {
31869c72bad1SChristoph Hellwig 		.old_key	= old_key,
31879c72bad1SChristoph Hellwig 		.new_key	= new_key,
31889c72bad1SChristoph Hellwig 		.flags		= flags,
31899c72bad1SChristoph Hellwig 		.fail_early	= true,
31908dd87f3cSMike Christie 		.ret		= 0,
31919c72bad1SChristoph Hellwig 	};
31929c72bad1SChristoph Hellwig 	int ret;
319371cdb697SChristoph Hellwig 
31949c72bad1SChristoph Hellwig 	ret = dm_call_pr(bdev, __dm_pr_register, &pr);
31958dd87f3cSMike Christie 	if (ret) {
31968dd87f3cSMike Christie 		/* Didn't even get to register a path */
31978dd87f3cSMike Christie 		return ret;
31988dd87f3cSMike Christie 	}
31998dd87f3cSMike Christie 
32008dd87f3cSMike Christie 	if (!pr.ret)
32018dd87f3cSMike Christie 		return 0;
32028dd87f3cSMike Christie 	ret = pr.ret;
32038dd87f3cSMike Christie 
32048dd87f3cSMike Christie 	if (!new_key)
32058dd87f3cSMike Christie 		return ret;
32068dd87f3cSMike Christie 
32079c72bad1SChristoph Hellwig 	/* unregister all paths if we failed to register any path */
32089c72bad1SChristoph Hellwig 	pr.old_key = new_key;
32099c72bad1SChristoph Hellwig 	pr.new_key = 0;
32109c72bad1SChristoph Hellwig 	pr.flags = 0;
32119c72bad1SChristoph Hellwig 	pr.fail_early = false;
32128dd87f3cSMike Christie 	(void) dm_call_pr(bdev, __dm_pr_register, &pr);
32139c72bad1SChristoph Hellwig 	return ret;
321471cdb697SChristoph Hellwig }
3215956a4025SMike Snitzer 
321670151087SMike Christie 
321770151087SMike Christie static int __dm_pr_reserve(struct dm_target *ti, struct dm_dev *dev,
321870151087SMike Christie 			   sector_t start, sector_t len, void *data)
321970151087SMike Christie {
322070151087SMike Christie 	struct dm_pr *pr = data;
322170151087SMike Christie 	const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
322270151087SMike Christie 
322370151087SMike Christie 	if (!ops || !ops->pr_reserve) {
322470151087SMike Christie 		pr->ret = -EOPNOTSUPP;
322570151087SMike Christie 		return -1;
322670151087SMike Christie 	}
322770151087SMike Christie 
322870151087SMike Christie 	pr->ret = ops->pr_reserve(dev->bdev, pr->old_key, pr->type, pr->flags);
322970151087SMike Christie 	if (!pr->ret)
323070151087SMike Christie 		return -1;
323170151087SMike Christie 
323270151087SMike Christie 	return 0;
323371cdb697SChristoph Hellwig }
323471cdb697SChristoph Hellwig 
323571cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
323671cdb697SChristoph Hellwig 			 u32 flags)
323771cdb697SChristoph Hellwig {
323870151087SMike Christie 	struct dm_pr pr = {
323970151087SMike Christie 		.old_key	= key,
324070151087SMike Christie 		.flags		= flags,
324170151087SMike Christie 		.type		= type,
324270151087SMike Christie 		.fail_early	= false,
324370151087SMike Christie 		.ret		= 0,
324470151087SMike Christie 	};
324570151087SMike Christie 	int ret;
324671cdb697SChristoph Hellwig 
324770151087SMike Christie 	ret = dm_call_pr(bdev, __dm_pr_reserve, &pr);
324870151087SMike Christie 	if (ret)
324970151087SMike Christie 		return ret;
325071cdb697SChristoph Hellwig 
325170151087SMike Christie 	return pr.ret;
325271cdb697SChristoph Hellwig }
325371cdb697SChristoph Hellwig 
325408a3c338SMike Christie /*
325508a3c338SMike Christie  * If there is a non-All Registrants type of reservation, the release must be
325608a3c338SMike Christie  * sent down the holding path. For the cases where there is no reservation or
325708a3c338SMike Christie  * the path is not the holder the device will also return success, so we must
325808a3c338SMike Christie  * try each path to make sure we got the correct path.
325908a3c338SMike Christie  */
326008a3c338SMike Christie static int __dm_pr_release(struct dm_target *ti, struct dm_dev *dev,
326108a3c338SMike Christie 			   sector_t start, sector_t len, void *data)
326208a3c338SMike Christie {
326308a3c338SMike Christie 	struct dm_pr *pr = data;
326408a3c338SMike Christie 	const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
326508a3c338SMike Christie 
326608a3c338SMike Christie 	if (!ops || !ops->pr_release) {
326708a3c338SMike Christie 		pr->ret = -EOPNOTSUPP;
326808a3c338SMike Christie 		return -1;
326908a3c338SMike Christie 	}
327008a3c338SMike Christie 
327108a3c338SMike Christie 	pr->ret = ops->pr_release(dev->bdev, pr->old_key, pr->type);
327208a3c338SMike Christie 	if (pr->ret)
327308a3c338SMike Christie 		return -1;
327408a3c338SMike Christie 
327508a3c338SMike Christie 	return 0;
327671cdb697SChristoph Hellwig }
327771cdb697SChristoph Hellwig 
327871cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
327971cdb697SChristoph Hellwig {
328008a3c338SMike Christie 	struct dm_pr pr = {
328108a3c338SMike Christie 		.old_key	= key,
328208a3c338SMike Christie 		.type		= type,
328308a3c338SMike Christie 		.fail_early	= false,
328408a3c338SMike Christie 	};
328508a3c338SMike Christie 	int ret;
328671cdb697SChristoph Hellwig 
328708a3c338SMike Christie 	ret = dm_call_pr(bdev, __dm_pr_release, &pr);
328808a3c338SMike Christie 	if (ret)
328908a3c338SMike Christie 		return ret;
329071cdb697SChristoph Hellwig 
329108a3c338SMike Christie 	return pr.ret;
329271cdb697SChristoph Hellwig }
329371cdb697SChristoph Hellwig 
3294c6adada5SMike Christie static int __dm_pr_preempt(struct dm_target *ti, struct dm_dev *dev,
3295c6adada5SMike Christie 			   sector_t start, sector_t len, void *data)
3296c6adada5SMike Christie {
3297c6adada5SMike Christie 	struct dm_pr *pr = data;
3298c6adada5SMike Christie 	const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
3299c6adada5SMike Christie 
3300c6adada5SMike Christie 	if (!ops || !ops->pr_preempt) {
3301c6adada5SMike Christie 		pr->ret = -EOPNOTSUPP;
3302c6adada5SMike Christie 		return -1;
3303c6adada5SMike Christie 	}
3304c6adada5SMike Christie 
3305c6adada5SMike Christie 	pr->ret = ops->pr_preempt(dev->bdev, pr->old_key, pr->new_key, pr->type,
3306c6adada5SMike Christie 				  pr->abort);
3307c6adada5SMike Christie 	if (!pr->ret)
3308c6adada5SMike Christie 		return -1;
3309c6adada5SMike Christie 
3310c6adada5SMike Christie 	return 0;
331171cdb697SChristoph Hellwig }
331271cdb697SChristoph Hellwig 
331371cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
331471cdb697SChristoph Hellwig 			 enum pr_type type, bool abort)
331571cdb697SChristoph Hellwig {
3316c6adada5SMike Christie 	struct dm_pr pr = {
3317c6adada5SMike Christie 		.new_key	= new_key,
3318c6adada5SMike Christie 		.old_key	= old_key,
3319c6adada5SMike Christie 		.type		= type,
3320c6adada5SMike Christie 		.fail_early	= false,
3321c6adada5SMike Christie 	};
3322c6adada5SMike Christie 	int ret;
332371cdb697SChristoph Hellwig 
3324c6adada5SMike Christie 	ret = dm_call_pr(bdev, __dm_pr_preempt, &pr);
3325c6adada5SMike Christie 	if (ret)
3326c6adada5SMike Christie 		return ret;
332771cdb697SChristoph Hellwig 
3328c6adada5SMike Christie 	return pr.ret;
332971cdb697SChristoph Hellwig }
333071cdb697SChristoph Hellwig 
333171cdb697SChristoph Hellwig static int dm_pr_clear(struct block_device *bdev, u64 key)
333271cdb697SChristoph Hellwig {
333371cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
333471cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3335971888c4SMike Snitzer 	int r, srcu_idx;
333671cdb697SChristoph Hellwig 
33375bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
333871cdb697SChristoph Hellwig 	if (r < 0)
3339971888c4SMike Snitzer 		goto out;
334071cdb697SChristoph Hellwig 
334171cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
334271cdb697SChristoph Hellwig 	if (ops && ops->pr_clear)
334371cdb697SChristoph Hellwig 		r = ops->pr_clear(bdev, key);
334471cdb697SChristoph Hellwig 	else
334571cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3346971888c4SMike Snitzer out:
3347971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
334871cdb697SChristoph Hellwig 	return r;
334971cdb697SChristoph Hellwig }
335071cdb697SChristoph Hellwig 
335171cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = {
335271cdb697SChristoph Hellwig 	.pr_register	= dm_pr_register,
335371cdb697SChristoph Hellwig 	.pr_reserve	= dm_pr_reserve,
335471cdb697SChristoph Hellwig 	.pr_release	= dm_pr_release,
335571cdb697SChristoph Hellwig 	.pr_preempt	= dm_pr_preempt,
335671cdb697SChristoph Hellwig 	.pr_clear	= dm_pr_clear,
335771cdb697SChristoph Hellwig };
335871cdb697SChristoph Hellwig 
335983d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
3360c62b37d9SChristoph Hellwig 	.submit_bio = dm_submit_bio,
3361b99fdcdcSMing Lei 	.poll_bio = dm_poll_bio,
33621da177e4SLinus Torvalds 	.open = dm_blk_open,
33631da177e4SLinus Torvalds 	.release = dm_blk_close,
3364aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
33653ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
3366e76239a3SChristoph Hellwig 	.report_zones = dm_blk_report_zones,
336771cdb697SChristoph Hellwig 	.pr_ops = &dm_pr_ops,
33681da177e4SLinus Torvalds 	.owner = THIS_MODULE
33691da177e4SLinus Torvalds };
33701da177e4SLinus Torvalds 
3371681cc5e8SMike Snitzer static const struct block_device_operations dm_rq_blk_dops = {
3372681cc5e8SMike Snitzer 	.open = dm_blk_open,
3373681cc5e8SMike Snitzer 	.release = dm_blk_close,
3374681cc5e8SMike Snitzer 	.ioctl = dm_blk_ioctl,
3375681cc5e8SMike Snitzer 	.getgeo = dm_blk_getgeo,
3376681cc5e8SMike Snitzer 	.pr_ops = &dm_pr_ops,
3377681cc5e8SMike Snitzer 	.owner = THIS_MODULE
3378681cc5e8SMike Snitzer };
3379681cc5e8SMike Snitzer 
3380f26c5719SDan Williams static const struct dax_operations dm_dax_ops = {
3381f26c5719SDan Williams 	.direct_access = dm_dax_direct_access,
3382cdf6cdcdSVivek Goyal 	.zero_page_range = dm_dax_zero_page_range,
3383047218ecSJane Chu 	.recovery_write = dm_dax_recovery_write,
3384f26c5719SDan Williams };
3385f26c5719SDan Williams 
33861da177e4SLinus Torvalds /*
33871da177e4SLinus Torvalds  * module hooks
33881da177e4SLinus Torvalds  */
33891da177e4SLinus Torvalds module_init(dm_init);
33901da177e4SLinus Torvalds module_exit(dm_exit);
33911da177e4SLinus Torvalds 
33921da177e4SLinus Torvalds module_param(major, uint, 0);
33931da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
3394f4790826SMike Snitzer 
33956a808034SHeinz Mauelshagen module_param(reserved_bio_based_ios, uint, 0644);
3396e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3397e8603136SMike Snitzer 
33986a808034SHeinz Mauelshagen module_param(dm_numa_node, int, 0644);
3399115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
3400115485e8SMike Snitzer 
34016a808034SHeinz Mauelshagen module_param(swap_bios, int, 0644);
3402a666e5c0SMikulas Patocka MODULE_PARM_DESC(swap_bios, "Maximum allowed inflight swap IOs");
3403a666e5c0SMikulas Patocka 
34041da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
34051da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
34061da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3407