xref: /openbmc/linux/drivers/md/dm.c (revision 61697a6abd24acba941359c6268a94f4afe4a53d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3784aae73SMilan Broz  * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This file is released under the GPL.
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
84cc96131SMike Snitzer #include "dm-core.h"
94cc96131SMike Snitzer #include "dm-rq.h"
1051e5b2bdSMike Anderson #include "dm-uevent.h"
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/init.h>
131da177e4SLinus Torvalds #include <linux/module.h>
1448c9c27bSArjan van de Ven #include <linux/mutex.h>
15174cd4b1SIngo Molnar #include <linux/sched/signal.h>
161da177e4SLinus Torvalds #include <linux/blkpg.h>
171da177e4SLinus Torvalds #include <linux/bio.h>
181da177e4SLinus Torvalds #include <linux/mempool.h>
19f26c5719SDan Williams #include <linux/dax.h>
201da177e4SLinus Torvalds #include <linux/slab.h>
211da177e4SLinus Torvalds #include <linux/idr.h>
227e026c8cSDan Williams #include <linux/uio.h>
233ac51e74SDarrick J. Wong #include <linux/hdreg.h>
243f77316dSKiyoshi Ueda #include <linux/delay.h>
25ffcc3936SMike Snitzer #include <linux/wait.h>
2671cdb697SChristoph Hellwig #include <linux/pr.h>
27b0b4d7c6SElena Reshetova #include <linux/refcount.h>
2855782138SLi Zefan 
2972d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
3072d94861SAlasdair G Kergon 
3160935eb2SMilan Broz /*
3260935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
3360935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
3460935eb2SMilan Broz  */
3560935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
3660935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
3760935eb2SMilan Broz 
381da177e4SLinus Torvalds static const char *_name = DM_NAME;
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds static unsigned int major = 0;
411da177e4SLinus Torvalds static unsigned int _major = 0;
421da177e4SLinus Torvalds 
43d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr);
44d15b774cSAlasdair G Kergon 
45f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
462c140a24SMikulas Patocka 
472c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w);
482c140a24SMikulas Patocka 
492c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
502c140a24SMikulas Patocka 
51acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue;
52acfe0ad7SMikulas Patocka 
5393e6442cSMikulas Patocka atomic_t dm_global_event_nr = ATOMIC_INIT(0);
5493e6442cSMikulas Patocka DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq);
5593e6442cSMikulas Patocka 
5662e08243SMikulas Patocka void dm_issue_global_event(void)
5762e08243SMikulas Patocka {
5862e08243SMikulas Patocka 	atomic_inc(&dm_global_event_nr);
5962e08243SMikulas Patocka 	wake_up(&dm_global_eventq);
6062e08243SMikulas Patocka }
6162e08243SMikulas Patocka 
621da177e4SLinus Torvalds /*
6364f52b0eSMike Snitzer  * One of these is allocated (on-stack) per original bio.
641da177e4SLinus Torvalds  */
6564f52b0eSMike Snitzer struct clone_info {
6664f52b0eSMike Snitzer 	struct dm_table *map;
6764f52b0eSMike Snitzer 	struct bio *bio;
6864f52b0eSMike Snitzer 	struct dm_io *io;
6964f52b0eSMike Snitzer 	sector_t sector;
7064f52b0eSMike Snitzer 	unsigned sector_count;
7164f52b0eSMike Snitzer };
7264f52b0eSMike Snitzer 
7364f52b0eSMike Snitzer /*
7464f52b0eSMike Snitzer  * One of these is allocated per clone bio.
7564f52b0eSMike Snitzer  */
7664f52b0eSMike Snitzer #define DM_TIO_MAGIC 7282014
7764f52b0eSMike Snitzer struct dm_target_io {
7864f52b0eSMike Snitzer 	unsigned magic;
7964f52b0eSMike Snitzer 	struct dm_io *io;
8064f52b0eSMike Snitzer 	struct dm_target *ti;
8164f52b0eSMike Snitzer 	unsigned target_bio_nr;
8264f52b0eSMike Snitzer 	unsigned *len_ptr;
8364f52b0eSMike Snitzer 	bool inside_dm_io;
8464f52b0eSMike Snitzer 	struct bio clone;
8564f52b0eSMike Snitzer };
8664f52b0eSMike Snitzer 
8764f52b0eSMike Snitzer /*
8864f52b0eSMike Snitzer  * One of these is allocated per original bio.
8964f52b0eSMike Snitzer  * It contains the first clone used for that original.
9064f52b0eSMike Snitzer  */
9164f52b0eSMike Snitzer #define DM_IO_MAGIC 5191977
921da177e4SLinus Torvalds struct dm_io {
9364f52b0eSMike Snitzer 	unsigned magic;
941da177e4SLinus Torvalds 	struct mapped_device *md;
954e4cbee9SChristoph Hellwig 	blk_status_t status;
961da177e4SLinus Torvalds 	atomic_t io_count;
97745dc570SMike Snitzer 	struct bio *orig_bio;
983eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
99f88fb981SKiyoshi Ueda 	spinlock_t endio_lock;
100fd2ed4d2SMikulas Patocka 	struct dm_stats_aux stats_aux;
10164f52b0eSMike Snitzer 	/* last member of dm_target_io is 'struct bio' */
10264f52b0eSMike Snitzer 	struct dm_target_io tio;
1031da177e4SLinus Torvalds };
1041da177e4SLinus Torvalds 
10564f52b0eSMike Snitzer void *dm_per_bio_data(struct bio *bio, size_t data_size)
10664f52b0eSMike Snitzer {
10764f52b0eSMike Snitzer 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
10864f52b0eSMike Snitzer 	if (!tio->inside_dm_io)
10964f52b0eSMike Snitzer 		return (char *)bio - offsetof(struct dm_target_io, clone) - data_size;
11064f52b0eSMike Snitzer 	return (char *)bio - offsetof(struct dm_target_io, clone) - offsetof(struct dm_io, tio) - data_size;
11164f52b0eSMike Snitzer }
11264f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_per_bio_data);
11364f52b0eSMike Snitzer 
11464f52b0eSMike Snitzer struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size)
11564f52b0eSMike Snitzer {
11664f52b0eSMike Snitzer 	struct dm_io *io = (struct dm_io *)((char *)data + data_size);
11764f52b0eSMike Snitzer 	if (io->magic == DM_IO_MAGIC)
11864f52b0eSMike Snitzer 		return (struct bio *)((char *)io + offsetof(struct dm_io, tio) + offsetof(struct dm_target_io, clone));
11964f52b0eSMike Snitzer 	BUG_ON(io->magic != DM_TIO_MAGIC);
12064f52b0eSMike Snitzer 	return (struct bio *)((char *)io + offsetof(struct dm_target_io, clone));
12164f52b0eSMike Snitzer }
12264f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_from_per_bio_data);
12364f52b0eSMike Snitzer 
12464f52b0eSMike Snitzer unsigned dm_bio_get_target_bio_nr(const struct bio *bio)
12564f52b0eSMike Snitzer {
12664f52b0eSMike Snitzer 	return container_of(bio, struct dm_target_io, clone)->target_bio_nr;
12764f52b0eSMike Snitzer }
12864f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_get_target_bio_nr);
12964f52b0eSMike Snitzer 
130ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
131ba61fdd1SJeff Mahoney 
1321da177e4SLinus Torvalds /*
1331da177e4SLinus Torvalds  * Bits for the md->flags field.
1341da177e4SLinus Torvalds  */
1351eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
1361da177e4SLinus Torvalds #define DMF_SUSPENDED 1
137aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
138fba9f90eSJeff Mahoney #define DMF_FREEING 3
1395c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
1402e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
1418ae12666SKent Overstreet #define DMF_DEFERRED_REMOVE 6
1428ae12666SKent Overstreet #define DMF_SUSPENDED_INTERNALLY 7
1431da177e4SLinus Torvalds 
144115485e8SMike Snitzer #define DM_NUMA_NODE NUMA_NO_NODE
145115485e8SMike Snitzer static int dm_numa_node = DM_NUMA_NODE;
146faad87dfSMike Snitzer 
147e6ee8c0bSKiyoshi Ueda /*
148e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
149e6ee8c0bSKiyoshi Ueda  */
150e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
1516f1c819cSKent Overstreet 	struct bio_set bs;
1526f1c819cSKent Overstreet 	struct bio_set io_bs;
153e6ee8c0bSKiyoshi Ueda };
154e6ee8c0bSKiyoshi Ueda 
15586f1152bSBenjamin Marzinski struct table_device {
15686f1152bSBenjamin Marzinski 	struct list_head list;
157b0b4d7c6SElena Reshetova 	refcount_t count;
15886f1152bSBenjamin Marzinski 	struct dm_dev dm_dev;
15986f1152bSBenjamin Marzinski };
16086f1152bSBenjamin Marzinski 
1618fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
1621ae49ea2SMike Snitzer static struct kmem_cache *_rq_cache;
16394818742SKent Overstreet 
164f4790826SMike Snitzer /*
165e8603136SMike Snitzer  * Bio-based DM's mempools' reserved IOs set by the user.
166e8603136SMike Snitzer  */
1674cc96131SMike Snitzer #define RESERVED_BIO_BASED_IOS		16
168e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
169e8603136SMike Snitzer 
170115485e8SMike Snitzer static int __dm_get_module_param_int(int *module_param, int min, int max)
171115485e8SMike Snitzer {
1726aa7de05SMark Rutland 	int param = READ_ONCE(*module_param);
173115485e8SMike Snitzer 	int modified_param = 0;
174115485e8SMike Snitzer 	bool modified = true;
175115485e8SMike Snitzer 
176115485e8SMike Snitzer 	if (param < min)
177115485e8SMike Snitzer 		modified_param = min;
178115485e8SMike Snitzer 	else if (param > max)
179115485e8SMike Snitzer 		modified_param = max;
180115485e8SMike Snitzer 	else
181115485e8SMike Snitzer 		modified = false;
182115485e8SMike Snitzer 
183115485e8SMike Snitzer 	if (modified) {
184115485e8SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
185115485e8SMike Snitzer 		param = modified_param;
186115485e8SMike Snitzer 	}
187115485e8SMike Snitzer 
188115485e8SMike Snitzer 	return param;
189115485e8SMike Snitzer }
190115485e8SMike Snitzer 
1914cc96131SMike Snitzer unsigned __dm_get_module_param(unsigned *module_param,
192f4790826SMike Snitzer 			       unsigned def, unsigned max)
193f4790826SMike Snitzer {
1946aa7de05SMark Rutland 	unsigned param = READ_ONCE(*module_param);
19509c2d531SMike Snitzer 	unsigned modified_param = 0;
196f4790826SMike Snitzer 
19709c2d531SMike Snitzer 	if (!param)
19809c2d531SMike Snitzer 		modified_param = def;
19909c2d531SMike Snitzer 	else if (param > max)
20009c2d531SMike Snitzer 		modified_param = max;
201f4790826SMike Snitzer 
20209c2d531SMike Snitzer 	if (modified_param) {
20309c2d531SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
20409c2d531SMike Snitzer 		param = modified_param;
205f4790826SMike Snitzer 	}
206f4790826SMike Snitzer 
20709c2d531SMike Snitzer 	return param;
208f4790826SMike Snitzer }
209f4790826SMike Snitzer 
210e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void)
211e8603136SMike Snitzer {
21209c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_bio_based_ios,
2134cc96131SMike Snitzer 				     RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS);
214e8603136SMike Snitzer }
215e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
216e8603136SMike Snitzer 
217115485e8SMike Snitzer static unsigned dm_get_numa_node(void)
218115485e8SMike Snitzer {
219115485e8SMike Snitzer 	return __dm_get_module_param_int(&dm_numa_node,
220115485e8SMike Snitzer 					 DM_NUMA_NODE, num_online_nodes() - 1);
221115485e8SMike Snitzer }
222115485e8SMike Snitzer 
2231da177e4SLinus Torvalds static int __init local_init(void)
2241da177e4SLinus Torvalds {
22551157b4aSKiyoshi Ueda 	int r = -ENOMEM;
2261da177e4SLinus Torvalds 
2278fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
2288fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
229dde1e1ecSMike Snitzer 		return r;
2308fbf26adSKiyoshi Ueda 
231eca7ee6dSMike Snitzer 	_rq_cache = kmem_cache_create("dm_old_clone_request", sizeof(struct request),
2321ae49ea2SMike Snitzer 				      __alignof__(struct request), 0, NULL);
2331ae49ea2SMike Snitzer 	if (!_rq_cache)
2341ae49ea2SMike Snitzer 		goto out_free_rq_tio_cache;
2351ae49ea2SMike Snitzer 
23651e5b2bdSMike Anderson 	r = dm_uevent_init();
23751157b4aSKiyoshi Ueda 	if (r)
2381ae49ea2SMike Snitzer 		goto out_free_rq_cache;
23951e5b2bdSMike Anderson 
240acfe0ad7SMikulas Patocka 	deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
241acfe0ad7SMikulas Patocka 	if (!deferred_remove_workqueue) {
242acfe0ad7SMikulas Patocka 		r = -ENOMEM;
243acfe0ad7SMikulas Patocka 		goto out_uevent_exit;
244acfe0ad7SMikulas Patocka 	}
245acfe0ad7SMikulas Patocka 
2461da177e4SLinus Torvalds 	_major = major;
2471da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
24851157b4aSKiyoshi Ueda 	if (r < 0)
249acfe0ad7SMikulas Patocka 		goto out_free_workqueue;
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	if (!_major)
2521da177e4SLinus Torvalds 		_major = r;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	return 0;
25551157b4aSKiyoshi Ueda 
256acfe0ad7SMikulas Patocka out_free_workqueue:
257acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
25851157b4aSKiyoshi Ueda out_uevent_exit:
25951157b4aSKiyoshi Ueda 	dm_uevent_exit();
2601ae49ea2SMike Snitzer out_free_rq_cache:
2611ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
2628fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
2638fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
26451157b4aSKiyoshi Ueda 
26551157b4aSKiyoshi Ueda 	return r;
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds static void local_exit(void)
2691da177e4SLinus Torvalds {
2702c140a24SMikulas Patocka 	flush_scheduled_work();
271acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
2722c140a24SMikulas Patocka 
2731ae49ea2SMike Snitzer 	kmem_cache_destroy(_rq_cache);
2748fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
27500d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
27651e5b2bdSMike Anderson 	dm_uevent_exit();
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	_major = 0;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	DMINFO("cleaned up");
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
283b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
2841da177e4SLinus Torvalds 	local_init,
2851da177e4SLinus Torvalds 	dm_target_init,
2861da177e4SLinus Torvalds 	dm_linear_init,
2871da177e4SLinus Torvalds 	dm_stripe_init,
288952b3557SMikulas Patocka 	dm_io_init,
289945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2901da177e4SLinus Torvalds 	dm_interface_init,
291fd2ed4d2SMikulas Patocka 	dm_statistics_init,
2921da177e4SLinus Torvalds };
2931da177e4SLinus Torvalds 
294b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2951da177e4SLinus Torvalds 	local_exit,
2961da177e4SLinus Torvalds 	dm_target_exit,
2971da177e4SLinus Torvalds 	dm_linear_exit,
2981da177e4SLinus Torvalds 	dm_stripe_exit,
299952b3557SMikulas Patocka 	dm_io_exit,
300945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
3011da177e4SLinus Torvalds 	dm_interface_exit,
302fd2ed4d2SMikulas Patocka 	dm_statistics_exit,
3031da177e4SLinus Torvalds };
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds static int __init dm_init(void)
3061da177e4SLinus Torvalds {
3071da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	int r, i;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
3121da177e4SLinus Torvalds 		r = _inits[i]();
3131da177e4SLinus Torvalds 		if (r)
3141da177e4SLinus Torvalds 			goto bad;
3151da177e4SLinus Torvalds 	}
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	return 0;
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds       bad:
3201da177e4SLinus Torvalds 	while (i--)
3211da177e4SLinus Torvalds 		_exits[i]();
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	return r;
3241da177e4SLinus Torvalds }
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds static void __exit dm_exit(void)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	while (i--)
3311da177e4SLinus Torvalds 		_exits[i]();
332d15b774cSAlasdair G Kergon 
333d15b774cSAlasdair G Kergon 	/*
334d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
335d15b774cSAlasdair G Kergon 	 */
336d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds /*
3401da177e4SLinus Torvalds  * Block device functions
3411da177e4SLinus Torvalds  */
342432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
343432a212cSMike Anderson {
344432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
345432a212cSMike Anderson }
346432a212cSMike Anderson 
347fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3481da177e4SLinus Torvalds {
3491da177e4SLinus Torvalds 	struct mapped_device *md;
3501da177e4SLinus Torvalds 
351fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
352fba9f90eSJeff Mahoney 
353fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
354fba9f90eSJeff Mahoney 	if (!md)
355fba9f90eSJeff Mahoney 		goto out;
356fba9f90eSJeff Mahoney 
3575c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
358432a212cSMike Anderson 	    dm_deleting_md(md)) {
359fba9f90eSJeff Mahoney 		md = NULL;
360fba9f90eSJeff Mahoney 		goto out;
361fba9f90eSJeff Mahoney 	}
362fba9f90eSJeff Mahoney 
3631da177e4SLinus Torvalds 	dm_get(md);
3645c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
365fba9f90eSJeff Mahoney out:
366fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
367fba9f90eSJeff Mahoney 
368fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds 
371db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode)
3721da177e4SLinus Torvalds {
37363a4f065SMike Snitzer 	struct mapped_device *md;
3746e9624b8SArnd Bergmann 
3754a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
3764a1aeb98SMilan Broz 
37763a4f065SMike Snitzer 	md = disk->private_data;
37863a4f065SMike Snitzer 	if (WARN_ON(!md))
37963a4f065SMike Snitzer 		goto out;
38063a4f065SMike Snitzer 
3812c140a24SMikulas Patocka 	if (atomic_dec_and_test(&md->open_count) &&
3822c140a24SMikulas Patocka 	    (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
383acfe0ad7SMikulas Patocka 		queue_work(deferred_remove_workqueue, &deferred_remove_work);
3842c140a24SMikulas Patocka 
3851da177e4SLinus Torvalds 	dm_put(md);
38663a4f065SMike Snitzer out:
3874a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3905c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3915c6bd75dSAlasdair G Kergon {
3925c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3935c6bd75dSAlasdair G Kergon }
3945c6bd75dSAlasdair G Kergon 
3955c6bd75dSAlasdair G Kergon /*
3965c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3975c6bd75dSAlasdair G Kergon  */
3982c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
3995c6bd75dSAlasdair G Kergon {
4005c6bd75dSAlasdair G Kergon 	int r = 0;
4015c6bd75dSAlasdair G Kergon 
4025c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
4035c6bd75dSAlasdair G Kergon 
4042c140a24SMikulas Patocka 	if (dm_open_count(md)) {
4055c6bd75dSAlasdair G Kergon 		r = -EBUSY;
4062c140a24SMikulas Patocka 		if (mark_deferred)
4072c140a24SMikulas Patocka 			set_bit(DMF_DEFERRED_REMOVE, &md->flags);
4082c140a24SMikulas Patocka 	} else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
4092c140a24SMikulas Patocka 		r = -EEXIST;
4105c6bd75dSAlasdair G Kergon 	else
4115c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
4125c6bd75dSAlasdair G Kergon 
4135c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
4145c6bd75dSAlasdair G Kergon 
4155c6bd75dSAlasdair G Kergon 	return r;
4165c6bd75dSAlasdair G Kergon }
4175c6bd75dSAlasdair G Kergon 
4182c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md)
4192c140a24SMikulas Patocka {
4202c140a24SMikulas Patocka 	int r = 0;
4212c140a24SMikulas Patocka 
4222c140a24SMikulas Patocka 	spin_lock(&_minor_lock);
4232c140a24SMikulas Patocka 
4242c140a24SMikulas Patocka 	if (test_bit(DMF_DELETING, &md->flags))
4252c140a24SMikulas Patocka 		r = -EBUSY;
4262c140a24SMikulas Patocka 	else
4272c140a24SMikulas Patocka 		clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
4282c140a24SMikulas Patocka 
4292c140a24SMikulas Patocka 	spin_unlock(&_minor_lock);
4302c140a24SMikulas Patocka 
4312c140a24SMikulas Patocka 	return r;
4322c140a24SMikulas Patocka }
4332c140a24SMikulas Patocka 
4342c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w)
4352c140a24SMikulas Patocka {
4362c140a24SMikulas Patocka 	dm_deferred_remove();
4372c140a24SMikulas Patocka }
4382c140a24SMikulas Patocka 
439fd2ed4d2SMikulas Patocka sector_t dm_get_size(struct mapped_device *md)
440fd2ed4d2SMikulas Patocka {
441fd2ed4d2SMikulas Patocka 	return get_capacity(md->disk);
442fd2ed4d2SMikulas Patocka }
443fd2ed4d2SMikulas Patocka 
4449974fa2cSMike Snitzer struct request_queue *dm_get_md_queue(struct mapped_device *md)
4459974fa2cSMike Snitzer {
4469974fa2cSMike Snitzer 	return md->queue;
4479974fa2cSMike Snitzer }
4489974fa2cSMike Snitzer 
449fd2ed4d2SMikulas Patocka struct dm_stats *dm_get_stats(struct mapped_device *md)
450fd2ed4d2SMikulas Patocka {
451fd2ed4d2SMikulas Patocka 	return &md->stats;
452fd2ed4d2SMikulas Patocka }
453fd2ed4d2SMikulas Patocka 
4543ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4553ac51e74SDarrick J. Wong {
4563ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
4573ac51e74SDarrick J. Wong 
4583ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
4593ac51e74SDarrick J. Wong }
4603ac51e74SDarrick J. Wong 
461e76239a3SChristoph Hellwig static int dm_blk_report_zones(struct gendisk *disk, sector_t sector,
462e76239a3SChristoph Hellwig 			       struct blk_zone *zones, unsigned int *nr_zones,
463e76239a3SChristoph Hellwig 			       gfp_t gfp_mask)
464e76239a3SChristoph Hellwig {
465e76239a3SChristoph Hellwig #ifdef CONFIG_BLK_DEV_ZONED
466e76239a3SChristoph Hellwig 	struct mapped_device *md = disk->private_data;
467e76239a3SChristoph Hellwig 	struct dm_target *tgt;
468e76239a3SChristoph Hellwig 	struct dm_table *map;
469e76239a3SChristoph Hellwig 	int srcu_idx, ret;
470e76239a3SChristoph Hellwig 
471e76239a3SChristoph Hellwig 	if (dm_suspended_md(md))
472e76239a3SChristoph Hellwig 		return -EAGAIN;
473e76239a3SChristoph Hellwig 
474e76239a3SChristoph Hellwig 	map = dm_get_live_table(md, &srcu_idx);
475e76239a3SChristoph Hellwig 	if (!map)
476e76239a3SChristoph Hellwig 		return -EIO;
477e76239a3SChristoph Hellwig 
478e76239a3SChristoph Hellwig 	tgt = dm_table_find_target(map, sector);
479e76239a3SChristoph Hellwig 	if (!dm_target_is_valid(tgt)) {
480e76239a3SChristoph Hellwig 		ret = -EIO;
481e76239a3SChristoph Hellwig 		goto out;
482e76239a3SChristoph Hellwig 	}
483e76239a3SChristoph Hellwig 
484e76239a3SChristoph Hellwig 	/*
485e76239a3SChristoph Hellwig 	 * If we are executing this, we already know that the block device
486e76239a3SChristoph Hellwig 	 * is a zoned device and so each target should have support for that
487e76239a3SChristoph Hellwig 	 * type of drive. A missing report_zones method means that the target
488e76239a3SChristoph Hellwig 	 * driver has a problem.
489e76239a3SChristoph Hellwig 	 */
490e76239a3SChristoph Hellwig 	if (WARN_ON(!tgt->type->report_zones)) {
491e76239a3SChristoph Hellwig 		ret = -EIO;
492e76239a3SChristoph Hellwig 		goto out;
493e76239a3SChristoph Hellwig 	}
494e76239a3SChristoph Hellwig 
495e76239a3SChristoph Hellwig 	/*
496e76239a3SChristoph Hellwig 	 * blkdev_report_zones() will loop and call this again to cover all the
497e76239a3SChristoph Hellwig 	 * zones of the target, eventually moving on to the next target.
498e76239a3SChristoph Hellwig 	 * So there is no need to loop here trying to fill the entire array
499e76239a3SChristoph Hellwig 	 * of zones.
500e76239a3SChristoph Hellwig 	 */
501e76239a3SChristoph Hellwig 	ret = tgt->type->report_zones(tgt, sector, zones,
502e76239a3SChristoph Hellwig 				      nr_zones, gfp_mask);
503e76239a3SChristoph Hellwig 
504e76239a3SChristoph Hellwig out:
505e76239a3SChristoph Hellwig 	dm_put_live_table(md, srcu_idx);
506e76239a3SChristoph Hellwig 	return ret;
507e76239a3SChristoph Hellwig #else
508e76239a3SChristoph Hellwig 	return -ENOTSUPP;
509e76239a3SChristoph Hellwig #endif
510e76239a3SChristoph Hellwig }
511e76239a3SChristoph Hellwig 
512971888c4SMike Snitzer static int dm_prepare_ioctl(struct mapped_device *md, int *srcu_idx,
5135bd5e8d8SMike Snitzer 			    struct block_device **bdev)
514971888c4SMike Snitzer 	__acquires(md->io_barrier)
515aa129a22SMilan Broz {
51666482026SMike Snitzer 	struct dm_target *tgt;
5176c182cd8SHannes Reinecke 	struct dm_table *map;
518971888c4SMike Snitzer 	int r;
519aa129a22SMilan Broz 
5206c182cd8SHannes Reinecke retry:
521e56f81e0SChristoph Hellwig 	r = -ENOTTY;
522971888c4SMike Snitzer 	map = dm_get_live_table(md, srcu_idx);
523aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
524971888c4SMike Snitzer 		return r;
525aa129a22SMilan Broz 
526aa129a22SMilan Broz 	/* We only support devices that have a single target */
527aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
528971888c4SMike Snitzer 		return r;
529aa129a22SMilan Broz 
53066482026SMike Snitzer 	tgt = dm_table_get_target(map, 0);
53166482026SMike Snitzer 	if (!tgt->type->prepare_ioctl)
532e56f81e0SChristoph Hellwig 		return r;
533aa129a22SMilan Broz 
534971888c4SMike Snitzer 	if (dm_suspended_md(md))
535971888c4SMike Snitzer 		return -EAGAIN;
536971888c4SMike Snitzer 
5375bd5e8d8SMike Snitzer 	r = tgt->type->prepare_ioctl(tgt, bdev);
5385bbbfdf6SJunichi Nomura 	if (r == -ENOTCONN && !fatal_signal_pending(current)) {
539971888c4SMike Snitzer 		dm_put_live_table(md, *srcu_idx);
5406c182cd8SHannes Reinecke 		msleep(10);
5416c182cd8SHannes Reinecke 		goto retry;
5426c182cd8SHannes Reinecke 	}
543971888c4SMike Snitzer 
544e56f81e0SChristoph Hellwig 	return r;
545e56f81e0SChristoph Hellwig }
5466c182cd8SHannes Reinecke 
547971888c4SMike Snitzer static void dm_unprepare_ioctl(struct mapped_device *md, int srcu_idx)
548971888c4SMike Snitzer 	__releases(md->io_barrier)
549971888c4SMike Snitzer {
550971888c4SMike Snitzer 	dm_put_live_table(md, srcu_idx);
551971888c4SMike Snitzer }
552971888c4SMike Snitzer 
553e56f81e0SChristoph Hellwig static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
554e56f81e0SChristoph Hellwig 			unsigned int cmd, unsigned long arg)
555e56f81e0SChristoph Hellwig {
556e56f81e0SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
557971888c4SMike Snitzer 	int r, srcu_idx;
558e56f81e0SChristoph Hellwig 
5595bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
560e56f81e0SChristoph Hellwig 	if (r < 0)
561971888c4SMike Snitzer 		goto out;
562e56f81e0SChristoph Hellwig 
563e56f81e0SChristoph Hellwig 	if (r > 0) {
564e56f81e0SChristoph Hellwig 		/*
565e980f623SChristoph Hellwig 		 * Target determined this ioctl is being issued against a
566e980f623SChristoph Hellwig 		 * subset of the parent bdev; require extra privileges.
567e56f81e0SChristoph Hellwig 		 */
568e980f623SChristoph Hellwig 		if (!capable(CAP_SYS_RAWIO)) {
569e980f623SChristoph Hellwig 			DMWARN_LIMIT(
570e980f623SChristoph Hellwig 	"%s: sending ioctl %x to DM device without required privilege.",
571e980f623SChristoph Hellwig 				current->comm, cmd);
572e980f623SChristoph Hellwig 			r = -ENOIOCTLCMD;
573e56f81e0SChristoph Hellwig 			goto out;
574e56f81e0SChristoph Hellwig 		}
575e980f623SChristoph Hellwig 	}
576e56f81e0SChristoph Hellwig 
57766482026SMike Snitzer 	r =  __blkdev_driver_ioctl(bdev, mode, cmd, arg);
578e56f81e0SChristoph Hellwig out:
579971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
580aa129a22SMilan Broz 	return r;
581aa129a22SMilan Broz }
582aa129a22SMilan Broz 
583978e51baSMike Snitzer static void start_io_acct(struct dm_io *io);
584978e51baSMike Snitzer 
585978e51baSMike Snitzer static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio)
5861da177e4SLinus Torvalds {
58764f52b0eSMike Snitzer 	struct dm_io *io;
58864f52b0eSMike Snitzer 	struct dm_target_io *tio;
58964f52b0eSMike Snitzer 	struct bio *clone;
59064f52b0eSMike Snitzer 
5916f1c819cSKent Overstreet 	clone = bio_alloc_bioset(GFP_NOIO, 0, &md->io_bs);
59264f52b0eSMike Snitzer 	if (!clone)
59364f52b0eSMike Snitzer 		return NULL;
59464f52b0eSMike Snitzer 
59564f52b0eSMike Snitzer 	tio = container_of(clone, struct dm_target_io, clone);
59664f52b0eSMike Snitzer 	tio->inside_dm_io = true;
59764f52b0eSMike Snitzer 	tio->io = NULL;
59864f52b0eSMike Snitzer 
59964f52b0eSMike Snitzer 	io = container_of(tio, struct dm_io, tio);
60064f52b0eSMike Snitzer 	io->magic = DM_IO_MAGIC;
601978e51baSMike Snitzer 	io->status = 0;
602978e51baSMike Snitzer 	atomic_set(&io->io_count, 1);
603978e51baSMike Snitzer 	io->orig_bio = bio;
604978e51baSMike Snitzer 	io->md = md;
605978e51baSMike Snitzer 	spin_lock_init(&io->endio_lock);
606978e51baSMike Snitzer 
607978e51baSMike Snitzer 	start_io_acct(io);
60864f52b0eSMike Snitzer 
60964f52b0eSMike Snitzer 	return io;
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds 
612028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
6131da177e4SLinus Torvalds {
61464f52b0eSMike Snitzer 	bio_put(&io->tio.clone);
61564f52b0eSMike Snitzer }
61664f52b0eSMike Snitzer 
61764f52b0eSMike Snitzer static struct dm_target_io *alloc_tio(struct clone_info *ci, struct dm_target *ti,
61864f52b0eSMike Snitzer 				      unsigned target_bio_nr, gfp_t gfp_mask)
61964f52b0eSMike Snitzer {
62064f52b0eSMike Snitzer 	struct dm_target_io *tio;
62164f52b0eSMike Snitzer 
62264f52b0eSMike Snitzer 	if (!ci->io->tio.io) {
62364f52b0eSMike Snitzer 		/* the dm_target_io embedded in ci->io is available */
62464f52b0eSMike Snitzer 		tio = &ci->io->tio;
62564f52b0eSMike Snitzer 	} else {
6266f1c819cSKent Overstreet 		struct bio *clone = bio_alloc_bioset(gfp_mask, 0, &ci->io->md->bs);
62764f52b0eSMike Snitzer 		if (!clone)
62864f52b0eSMike Snitzer 			return NULL;
62964f52b0eSMike Snitzer 
63064f52b0eSMike Snitzer 		tio = container_of(clone, struct dm_target_io, clone);
63164f52b0eSMike Snitzer 		tio->inside_dm_io = false;
63264f52b0eSMike Snitzer 	}
63364f52b0eSMike Snitzer 
63464f52b0eSMike Snitzer 	tio->magic = DM_TIO_MAGIC;
63564f52b0eSMike Snitzer 	tio->io = ci->io;
63664f52b0eSMike Snitzer 	tio->ti = ti;
63764f52b0eSMike Snitzer 	tio->target_bio_nr = target_bio_nr;
63864f52b0eSMike Snitzer 
63964f52b0eSMike Snitzer 	return tio;
6401da177e4SLinus Torvalds }
6411da177e4SLinus Torvalds 
642cfae7529SMike Snitzer static void free_tio(struct dm_target_io *tio)
6431da177e4SLinus Torvalds {
64464f52b0eSMike Snitzer 	if (tio->inside_dm_io)
64564f52b0eSMike Snitzer 		return;
646dba14160SMikulas Patocka 	bio_put(&tio->clone);
6471da177e4SLinus Torvalds }
6481da177e4SLinus Torvalds 
649c4576aedSMike Snitzer static bool md_in_flight_bios(struct mapped_device *md)
65090abb8c4SKiyoshi Ueda {
6516f757231SMikulas Patocka 	int cpu;
6526f757231SMikulas Patocka 	struct hd_struct *part = &dm_disk(md)->part0;
653b7934ba4SJens Axboe 	long sum = 0;
6546f757231SMikulas Patocka 
6556f757231SMikulas Patocka 	for_each_possible_cpu(cpu) {
656b7934ba4SJens Axboe 		sum += part_stat_local_read_cpu(part, in_flight[0], cpu);
657b7934ba4SJens Axboe 		sum += part_stat_local_read_cpu(part, in_flight[1], cpu);
6586f757231SMikulas Patocka 	}
6596f757231SMikulas Patocka 
660b7934ba4SJens Axboe 	return sum != 0;
66190abb8c4SKiyoshi Ueda }
66290abb8c4SKiyoshi Ueda 
663c4576aedSMike Snitzer static bool md_in_flight(struct mapped_device *md)
664c4576aedSMike Snitzer {
665c4576aedSMike Snitzer 	if (queue_is_mq(md->queue))
6663c94d83cSJens Axboe 		return blk_mq_queue_inflight(md->queue);
667c4576aedSMike Snitzer 	else
668c4576aedSMike Snitzer 		return md_in_flight_bios(md);
669cec47e3dSKiyoshi Ueda }
670cec47e3dSKiyoshi Ueda 
6713eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
6723eaf840eSJun'ichi "Nick" Nomura {
6733eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
674745dc570SMike Snitzer 	struct bio *bio = io->orig_bio;
6753eaf840eSJun'ichi "Nick" Nomura 
6763eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
6773eaf840eSJun'ichi "Nick" Nomura 
678ddcf35d3SMichael Callahan 	generic_start_io_acct(md->queue, bio_op(bio), bio_sectors(bio),
679ddcf35d3SMichael Callahan 			      &dm_disk(md)->part0);
680f3986374SMike Snitzer 
681fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
682528ec5abSMike Christie 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
683528ec5abSMike Christie 				    bio->bi_iter.bi_sector, bio_sectors(bio),
684528ec5abSMike Christie 				    false, 0, &io->stats_aux);
6853eaf840eSJun'ichi "Nick" Nomura }
6863eaf840eSJun'ichi "Nick" Nomura 
687d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
6883eaf840eSJun'ichi "Nick" Nomura {
6893eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
690745dc570SMike Snitzer 	struct bio *bio = io->orig_bio;
6913eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
6923eaf840eSJun'ichi "Nick" Nomura 
693ddcf35d3SMichael Callahan 	generic_end_io_acct(md->queue, bio_op(bio), &dm_disk(md)->part0,
694ddcf35d3SMichael Callahan 			    io->start_time);
6953eaf840eSJun'ichi "Nick" Nomura 
696fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
697528ec5abSMike Christie 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
698528ec5abSMike Christie 				    bio->bi_iter.bi_sector, bio_sectors(bio),
699528ec5abSMike Christie 				    true, duration, &io->stats_aux);
700fd2ed4d2SMikulas Patocka 
701d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
702645efa84SMikulas Patocka 	if (unlikely(wq_has_sleeper(&md->wait)))
703d221d2e7SMikulas Patocka 		wake_up(&md->wait);
7043eaf840eSJun'ichi "Nick" Nomura }
7053eaf840eSJun'ichi "Nick" Nomura 
7061da177e4SLinus Torvalds /*
7071da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
7081da177e4SLinus Torvalds  */
70992c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
7101da177e4SLinus Torvalds {
71105447420SKiyoshi Ueda 	unsigned long flags;
7121da177e4SLinus Torvalds 
71305447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
7141da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
71505447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
71692c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
7171da177e4SLinus Torvalds }
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds /*
7201da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
7211da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
72283d5e5b0SMikulas Patocka  * dm_put_live_table() when finished.
7231da177e4SLinus Torvalds  */
72483d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
7251da177e4SLinus Torvalds {
72683d5e5b0SMikulas Patocka 	*srcu_idx = srcu_read_lock(&md->io_barrier);
7271da177e4SLinus Torvalds 
72883d5e5b0SMikulas Patocka 	return srcu_dereference(md->map, &md->io_barrier);
72983d5e5b0SMikulas Patocka }
7301da177e4SLinus Torvalds 
73183d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
73283d5e5b0SMikulas Patocka {
73383d5e5b0SMikulas Patocka 	srcu_read_unlock(&md->io_barrier, srcu_idx);
73483d5e5b0SMikulas Patocka }
73583d5e5b0SMikulas Patocka 
73683d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md)
73783d5e5b0SMikulas Patocka {
73883d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
73983d5e5b0SMikulas Patocka 	synchronize_rcu_expedited();
74083d5e5b0SMikulas Patocka }
74183d5e5b0SMikulas Patocka 
74283d5e5b0SMikulas Patocka /*
74383d5e5b0SMikulas Patocka  * A fast alternative to dm_get_live_table/dm_put_live_table.
74483d5e5b0SMikulas Patocka  * The caller must not block between these two functions.
74583d5e5b0SMikulas Patocka  */
74683d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
74783d5e5b0SMikulas Patocka {
74883d5e5b0SMikulas Patocka 	rcu_read_lock();
74983d5e5b0SMikulas Patocka 	return rcu_dereference(md->map);
75083d5e5b0SMikulas Patocka }
75183d5e5b0SMikulas Patocka 
75283d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
75383d5e5b0SMikulas Patocka {
75483d5e5b0SMikulas Patocka 	rcu_read_unlock();
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds 
757971888c4SMike Snitzer static char *_dm_claim_ptr = "I belong to device-mapper";
758971888c4SMike Snitzer 
7593ac51e74SDarrick J. Wong /*
76086f1152bSBenjamin Marzinski  * Open a table device so we can use it as a map destination.
76186f1152bSBenjamin Marzinski  */
76286f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev,
76386f1152bSBenjamin Marzinski 			     struct mapped_device *md)
76486f1152bSBenjamin Marzinski {
76586f1152bSBenjamin Marzinski 	struct block_device *bdev;
76686f1152bSBenjamin Marzinski 
76786f1152bSBenjamin Marzinski 	int r;
76886f1152bSBenjamin Marzinski 
76986f1152bSBenjamin Marzinski 	BUG_ON(td->dm_dev.bdev);
77086f1152bSBenjamin Marzinski 
771519049afSMike Snitzer 	bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr);
77286f1152bSBenjamin Marzinski 	if (IS_ERR(bdev))
77386f1152bSBenjamin Marzinski 		return PTR_ERR(bdev);
77486f1152bSBenjamin Marzinski 
77586f1152bSBenjamin Marzinski 	r = bd_link_disk_holder(bdev, dm_disk(md));
77686f1152bSBenjamin Marzinski 	if (r) {
77786f1152bSBenjamin Marzinski 		blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
77886f1152bSBenjamin Marzinski 		return r;
77986f1152bSBenjamin Marzinski 	}
78086f1152bSBenjamin Marzinski 
78186f1152bSBenjamin Marzinski 	td->dm_dev.bdev = bdev;
782817bf402SDan Williams 	td->dm_dev.dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
78386f1152bSBenjamin Marzinski 	return 0;
78486f1152bSBenjamin Marzinski }
78586f1152bSBenjamin Marzinski 
78686f1152bSBenjamin Marzinski /*
78786f1152bSBenjamin Marzinski  * Close a table device that we've been using.
78886f1152bSBenjamin Marzinski  */
78986f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md)
79086f1152bSBenjamin Marzinski {
79186f1152bSBenjamin Marzinski 	if (!td->dm_dev.bdev)
79286f1152bSBenjamin Marzinski 		return;
79386f1152bSBenjamin Marzinski 
79486f1152bSBenjamin Marzinski 	bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
79586f1152bSBenjamin Marzinski 	blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
796817bf402SDan Williams 	put_dax(td->dm_dev.dax_dev);
79786f1152bSBenjamin Marzinski 	td->dm_dev.bdev = NULL;
798817bf402SDan Williams 	td->dm_dev.dax_dev = NULL;
79986f1152bSBenjamin Marzinski }
80086f1152bSBenjamin Marzinski 
80186f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev,
80286f1152bSBenjamin Marzinski 					      fmode_t mode) {
80386f1152bSBenjamin Marzinski 	struct table_device *td;
80486f1152bSBenjamin Marzinski 
80586f1152bSBenjamin Marzinski 	list_for_each_entry(td, l, list)
80686f1152bSBenjamin Marzinski 		if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
80786f1152bSBenjamin Marzinski 			return td;
80886f1152bSBenjamin Marzinski 
80986f1152bSBenjamin Marzinski 	return NULL;
81086f1152bSBenjamin Marzinski }
81186f1152bSBenjamin Marzinski 
81286f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
81386f1152bSBenjamin Marzinski 			struct dm_dev **result) {
81486f1152bSBenjamin Marzinski 	int r;
81586f1152bSBenjamin Marzinski 	struct table_device *td;
81686f1152bSBenjamin Marzinski 
81786f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
81886f1152bSBenjamin Marzinski 	td = find_table_device(&md->table_devices, dev, mode);
81986f1152bSBenjamin Marzinski 	if (!td) {
820115485e8SMike Snitzer 		td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
82186f1152bSBenjamin Marzinski 		if (!td) {
82286f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
82386f1152bSBenjamin Marzinski 			return -ENOMEM;
82486f1152bSBenjamin Marzinski 		}
82586f1152bSBenjamin Marzinski 
82686f1152bSBenjamin Marzinski 		td->dm_dev.mode = mode;
82786f1152bSBenjamin Marzinski 		td->dm_dev.bdev = NULL;
82886f1152bSBenjamin Marzinski 
82986f1152bSBenjamin Marzinski 		if ((r = open_table_device(td, dev, md))) {
83086f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
83186f1152bSBenjamin Marzinski 			kfree(td);
83286f1152bSBenjamin Marzinski 			return r;
83386f1152bSBenjamin Marzinski 		}
83486f1152bSBenjamin Marzinski 
83586f1152bSBenjamin Marzinski 		format_dev_t(td->dm_dev.name, dev);
83686f1152bSBenjamin Marzinski 
837b0b4d7c6SElena Reshetova 		refcount_set(&td->count, 1);
83886f1152bSBenjamin Marzinski 		list_add(&td->list, &md->table_devices);
839b0b4d7c6SElena Reshetova 	} else {
840b0b4d7c6SElena Reshetova 		refcount_inc(&td->count);
84186f1152bSBenjamin Marzinski 	}
84286f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
84386f1152bSBenjamin Marzinski 
84486f1152bSBenjamin Marzinski 	*result = &td->dm_dev;
84586f1152bSBenjamin Marzinski 	return 0;
84686f1152bSBenjamin Marzinski }
84786f1152bSBenjamin Marzinski EXPORT_SYMBOL_GPL(dm_get_table_device);
84886f1152bSBenjamin Marzinski 
84986f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
85086f1152bSBenjamin Marzinski {
85186f1152bSBenjamin Marzinski 	struct table_device *td = container_of(d, struct table_device, dm_dev);
85286f1152bSBenjamin Marzinski 
85386f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
854b0b4d7c6SElena Reshetova 	if (refcount_dec_and_test(&td->count)) {
85586f1152bSBenjamin Marzinski 		close_table_device(td, md);
85686f1152bSBenjamin Marzinski 		list_del(&td->list);
85786f1152bSBenjamin Marzinski 		kfree(td);
85886f1152bSBenjamin Marzinski 	}
85986f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
86086f1152bSBenjamin Marzinski }
86186f1152bSBenjamin Marzinski EXPORT_SYMBOL(dm_put_table_device);
86286f1152bSBenjamin Marzinski 
86386f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices)
86486f1152bSBenjamin Marzinski {
86586f1152bSBenjamin Marzinski 	struct list_head *tmp, *next;
86686f1152bSBenjamin Marzinski 
86786f1152bSBenjamin Marzinski 	list_for_each_safe(tmp, next, devices) {
86886f1152bSBenjamin Marzinski 		struct table_device *td = list_entry(tmp, struct table_device, list);
86986f1152bSBenjamin Marzinski 
87086f1152bSBenjamin Marzinski 		DMWARN("dm_destroy: %s still exists with %d references",
871b0b4d7c6SElena Reshetova 		       td->dm_dev.name, refcount_read(&td->count));
87286f1152bSBenjamin Marzinski 		kfree(td);
87386f1152bSBenjamin Marzinski 	}
87486f1152bSBenjamin Marzinski }
87586f1152bSBenjamin Marzinski 
87686f1152bSBenjamin Marzinski /*
8773ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
8783ac51e74SDarrick J. Wong  */
8793ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
8803ac51e74SDarrick J. Wong {
8813ac51e74SDarrick J. Wong 	*geo = md->geometry;
8823ac51e74SDarrick J. Wong 
8833ac51e74SDarrick J. Wong 	return 0;
8843ac51e74SDarrick J. Wong }
8853ac51e74SDarrick J. Wong 
8863ac51e74SDarrick J. Wong /*
8873ac51e74SDarrick J. Wong  * Set the geometry of a device.
8883ac51e74SDarrick J. Wong  */
8893ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
8903ac51e74SDarrick J. Wong {
8913ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
8923ac51e74SDarrick J. Wong 
8933ac51e74SDarrick J. Wong 	if (geo->start > sz) {
8943ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
8953ac51e74SDarrick J. Wong 		return -EINVAL;
8963ac51e74SDarrick J. Wong 	}
8973ac51e74SDarrick J. Wong 
8983ac51e74SDarrick J. Wong 	md->geometry = *geo;
8993ac51e74SDarrick J. Wong 
9003ac51e74SDarrick J. Wong 	return 0;
9013ac51e74SDarrick J. Wong }
9023ac51e74SDarrick J. Wong 
9032e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
9042e93ccc1SKiyoshi Ueda {
9052e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
9062e93ccc1SKiyoshi Ueda }
9072e93ccc1SKiyoshi Ueda 
9081da177e4SLinus Torvalds /*
9091da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
9101da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
9111da177e4SLinus Torvalds  */
9124e4cbee9SChristoph Hellwig static void dec_pending(struct dm_io *io, blk_status_t error)
9131da177e4SLinus Torvalds {
9142e93ccc1SKiyoshi Ueda 	unsigned long flags;
9154e4cbee9SChristoph Hellwig 	blk_status_t io_error;
916b35f8caaSMilan Broz 	struct bio *bio;
917b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
9182e93ccc1SKiyoshi Ueda 
9192e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
920f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
921f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
922745dc570SMike Snitzer 		if (!(io->status == BLK_STS_DM_REQUEUE && __noflush_suspending(md)))
9234e4cbee9SChristoph Hellwig 			io->status = error;
924f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
925f88fb981SKiyoshi Ueda 	}
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
9284e4cbee9SChristoph Hellwig 		if (io->status == BLK_STS_DM_REQUEUE) {
9292e93ccc1SKiyoshi Ueda 			/*
9302e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
9312e93ccc1SKiyoshi Ueda 			 */
932022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
9336a8736d1STejun Heo 			if (__noflush_suspending(md))
934745dc570SMike Snitzer 				/* NOTE early return due to BLK_STS_DM_REQUEUE below */
935745dc570SMike Snitzer 				bio_list_add_head(&md->deferred, io->orig_bio);
9366a8736d1STejun Heo 			else
9372e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
9384e4cbee9SChristoph Hellwig 				io->status = BLK_STS_IOERR;
939022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
9402e93ccc1SKiyoshi Ueda 		}
9412e93ccc1SKiyoshi Ueda 
9424e4cbee9SChristoph Hellwig 		io_error = io->status;
943745dc570SMike Snitzer 		bio = io->orig_bio;
944af7e466aSMikulas Patocka 		end_io_acct(io);
945a97f925aSMikulas Patocka 		free_io(md, io);
9461da177e4SLinus Torvalds 
9474e4cbee9SChristoph Hellwig 		if (io_error == BLK_STS_DM_REQUEUE)
9486a8736d1STejun Heo 			return;
9496a8736d1STejun Heo 
9501eff9d32SJens Axboe 		if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) {
9511da177e4SLinus Torvalds 			/*
9526a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
95328a8f0d3SMike Christie 			 * without REQ_PREFLUSH.
9541da177e4SLinus Torvalds 			 */
9551eff9d32SJens Axboe 			bio->bi_opf &= ~REQ_PREFLUSH;
9566a8736d1STejun Heo 			queue_io(md, bio);
957b35f8caaSMilan Broz 		} else {
958b372d360SMike Snitzer 			/* done with normal IO or empty flush */
9598dd601faSNeilBrown 			if (io_error)
9604e4cbee9SChristoph Hellwig 				bio->bi_status = io_error;
9614246a0b6SChristoph Hellwig 			bio_endio(bio);
9622e93ccc1SKiyoshi Ueda 		}
9631da177e4SLinus Torvalds 	}
964af7e466aSMikulas Patocka }
9651da177e4SLinus Torvalds 
9664cc96131SMike Snitzer void disable_write_same(struct mapped_device *md)
9677eee4ae2SMike Snitzer {
9687eee4ae2SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
9697eee4ae2SMike Snitzer 
9707eee4ae2SMike Snitzer 	/* device doesn't really support WRITE SAME, disable it */
9717eee4ae2SMike Snitzer 	limits->max_write_same_sectors = 0;
9727eee4ae2SMike Snitzer }
9737eee4ae2SMike Snitzer 
974ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md)
975ac62d620SChristoph Hellwig {
976ac62d620SChristoph Hellwig 	struct queue_limits *limits = dm_get_queue_limits(md);
977ac62d620SChristoph Hellwig 
978ac62d620SChristoph Hellwig 	/* device doesn't really support WRITE ZEROES, disable it */
979ac62d620SChristoph Hellwig 	limits->max_write_zeroes_sectors = 0;
980ac62d620SChristoph Hellwig }
981ac62d620SChristoph Hellwig 
9824246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio)
9831da177e4SLinus Torvalds {
9844e4cbee9SChristoph Hellwig 	blk_status_t error = bio->bi_status;
985bfc6d41cSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
986b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
9879faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
9881da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
9891da177e4SLinus Torvalds 
990978e51baSMike Snitzer 	if (unlikely(error == BLK_STS_TARGET) && md->type != DM_TYPE_NVME_BIO_BASED) {
991ac62d620SChristoph Hellwig 		if (bio_op(bio) == REQ_OP_WRITE_SAME &&
99274d46992SChristoph Hellwig 		    !bio->bi_disk->queue->limits.max_write_same_sectors)
9937eee4ae2SMike Snitzer 			disable_write_same(md);
994ac62d620SChristoph Hellwig 		if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
99574d46992SChristoph Hellwig 		    !bio->bi_disk->queue->limits.max_write_zeroes_sectors)
996ac62d620SChristoph Hellwig 			disable_write_zeroes(md);
997ac62d620SChristoph Hellwig 	}
9987eee4ae2SMike Snitzer 
9991be56909SChristoph Hellwig 	if (endio) {
10004e4cbee9SChristoph Hellwig 		int r = endio(tio->ti, bio, &error);
10011be56909SChristoph Hellwig 		switch (r) {
10021be56909SChristoph Hellwig 		case DM_ENDIO_REQUEUE:
10034e4cbee9SChristoph Hellwig 			error = BLK_STS_DM_REQUEUE;
10041be56909SChristoph Hellwig 			/*FALLTHRU*/
10051be56909SChristoph Hellwig 		case DM_ENDIO_DONE:
10061be56909SChristoph Hellwig 			break;
10071be56909SChristoph Hellwig 		case DM_ENDIO_INCOMPLETE:
10081be56909SChristoph Hellwig 			/* The target will handle the io */
10091be56909SChristoph Hellwig 			return;
10101be56909SChristoph Hellwig 		default:
10111be56909SChristoph Hellwig 			DMWARN("unimplemented target endio return value: %d", r);
10121be56909SChristoph Hellwig 			BUG();
10131be56909SChristoph Hellwig 		}
10141be56909SChristoph Hellwig 	}
10151be56909SChristoph Hellwig 
1016cfae7529SMike Snitzer 	free_tio(tio);
1017b35f8caaSMilan Broz 	dec_pending(io, error);
10181da177e4SLinus Torvalds }
10191da177e4SLinus Torvalds 
102078d8e58aSMike Snitzer /*
102156a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
102256a67df7SMike Snitzer  * target boundary.
102356a67df7SMike Snitzer  */
102456a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
10251da177e4SLinus Torvalds {
102656a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
102756a67df7SMike Snitzer 
102856a67df7SMike Snitzer 	return ti->len - target_offset;
102956a67df7SMike Snitzer }
103056a67df7SMike Snitzer 
103156a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
103256a67df7SMike Snitzer {
103356a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
1034542f9038SMike Snitzer 	sector_t offset, max_len;
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	/*
10371da177e4SLinus Torvalds 	 * Does the target need to split even further?
10381da177e4SLinus Torvalds 	 */
1039542f9038SMike Snitzer 	if (ti->max_io_len) {
1040542f9038SMike Snitzer 		offset = dm_target_offset(ti, sector);
1041542f9038SMike Snitzer 		if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1042542f9038SMike Snitzer 			max_len = sector_div(offset, ti->max_io_len);
1043542f9038SMike Snitzer 		else
1044542f9038SMike Snitzer 			max_len = offset & (ti->max_io_len - 1);
1045542f9038SMike Snitzer 		max_len = ti->max_io_len - max_len;
1046542f9038SMike Snitzer 
1047542f9038SMike Snitzer 		if (len > max_len)
1048542f9038SMike Snitzer 			len = max_len;
10491da177e4SLinus Torvalds 	}
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 	return len;
10521da177e4SLinus Torvalds }
10531da177e4SLinus Torvalds 
1054542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1055542f9038SMike Snitzer {
1056542f9038SMike Snitzer 	if (len > UINT_MAX) {
1057542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1058542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
1059542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
1060542f9038SMike Snitzer 		return -EINVAL;
1061542f9038SMike Snitzer 	}
1062542f9038SMike Snitzer 
10638f50e358SMing Lei 	/*
10648f50e358SMing Lei 	 * BIO based queue uses its own splitting. When multipage bvecs
10658f50e358SMing Lei 	 * is switched on, size of the incoming bio may be too big to
10668f50e358SMing Lei 	 * be handled in some targets, such as crypt.
10678f50e358SMing Lei 	 *
10688f50e358SMing Lei 	 * When these targets are ready for the big bio, we can remove
10698f50e358SMing Lei 	 * the limit.
10708f50e358SMing Lei 	 */
10718f50e358SMing Lei 	ti->max_io_len = min_t(uint32_t, len, BIO_MAX_PAGES * PAGE_SIZE);
1072542f9038SMike Snitzer 
1073542f9038SMike Snitzer 	return 0;
1074542f9038SMike Snitzer }
1075542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1076542f9038SMike Snitzer 
1077f26c5719SDan Williams static struct dm_target *dm_dax_get_live_target(struct mapped_device *md,
1078f26c5719SDan Williams 						sector_t sector, int *srcu_idx)
10793d97c829SMike Snitzer 	__acquires(md->io_barrier)
1080545ed20eSToshi Kani {
1081545ed20eSToshi Kani 	struct dm_table *map;
1082545ed20eSToshi Kani 	struct dm_target *ti;
1083545ed20eSToshi Kani 
1084f26c5719SDan Williams 	map = dm_get_live_table(md, srcu_idx);
1085545ed20eSToshi Kani 	if (!map)
1086f26c5719SDan Williams 		return NULL;
1087545ed20eSToshi Kani 
1088545ed20eSToshi Kani 	ti = dm_table_find_target(map, sector);
1089545ed20eSToshi Kani 	if (!dm_target_is_valid(ti))
1090f26c5719SDan Williams 		return NULL;
1091f26c5719SDan Williams 
1092f26c5719SDan Williams 	return ti;
1093f26c5719SDan Williams }
1094f26c5719SDan Williams 
1095f26c5719SDan Williams static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
1096f26c5719SDan Williams 				 long nr_pages, void **kaddr, pfn_t *pfn)
1097f26c5719SDan Williams {
1098f26c5719SDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
1099f26c5719SDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
1100f26c5719SDan Williams 	struct dm_target *ti;
1101f26c5719SDan Williams 	long len, ret = -EIO;
1102f26c5719SDan Williams 	int srcu_idx;
1103f26c5719SDan Williams 
1104f26c5719SDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1105f26c5719SDan Williams 
1106f26c5719SDan Williams 	if (!ti)
1107545ed20eSToshi Kani 		goto out;
1108f26c5719SDan Williams 	if (!ti->type->direct_access)
1109f26c5719SDan Williams 		goto out;
1110f26c5719SDan Williams 	len = max_io_len(sector, ti) / PAGE_SECTORS;
1111f26c5719SDan Williams 	if (len < 1)
1112f26c5719SDan Williams 		goto out;
1113f26c5719SDan Williams 	nr_pages = min(len, nr_pages);
1114817bf402SDan Williams 	ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn);
1115817bf402SDan Williams 
1116545ed20eSToshi Kani  out:
1117545ed20eSToshi Kani 	dm_put_live_table(md, srcu_idx);
1118f26c5719SDan Williams 
1119f26c5719SDan Williams 	return ret;
1120545ed20eSToshi Kani }
1121545ed20eSToshi Kani 
11227e026c8cSDan Williams static size_t dm_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
11237e026c8cSDan Williams 				    void *addr, size_t bytes, struct iov_iter *i)
11247e026c8cSDan Williams {
11257e026c8cSDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
11267e026c8cSDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
11277e026c8cSDan Williams 	struct dm_target *ti;
11287e026c8cSDan Williams 	long ret = 0;
11297e026c8cSDan Williams 	int srcu_idx;
11307e026c8cSDan Williams 
11317e026c8cSDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
11327e026c8cSDan Williams 
11337e026c8cSDan Williams 	if (!ti)
11347e026c8cSDan Williams 		goto out;
11357e026c8cSDan Williams 	if (!ti->type->dax_copy_from_iter) {
11367e026c8cSDan Williams 		ret = copy_from_iter(addr, bytes, i);
11377e026c8cSDan Williams 		goto out;
11387e026c8cSDan Williams 	}
11397e026c8cSDan Williams 	ret = ti->type->dax_copy_from_iter(ti, pgoff, addr, bytes, i);
11407e026c8cSDan Williams  out:
11417e026c8cSDan Williams 	dm_put_live_table(md, srcu_idx);
11427e026c8cSDan Williams 
11437e026c8cSDan Williams 	return ret;
11447e026c8cSDan Williams }
11457e026c8cSDan Williams 
1146b3a9a0c3SDan Williams static size_t dm_dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff,
1147b3a9a0c3SDan Williams 		void *addr, size_t bytes, struct iov_iter *i)
1148b3a9a0c3SDan Williams {
1149b3a9a0c3SDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
1150b3a9a0c3SDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
1151b3a9a0c3SDan Williams 	struct dm_target *ti;
1152b3a9a0c3SDan Williams 	long ret = 0;
1153b3a9a0c3SDan Williams 	int srcu_idx;
1154b3a9a0c3SDan Williams 
1155b3a9a0c3SDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1156b3a9a0c3SDan Williams 
1157b3a9a0c3SDan Williams 	if (!ti)
1158b3a9a0c3SDan Williams 		goto out;
1159b3a9a0c3SDan Williams 	if (!ti->type->dax_copy_to_iter) {
1160b3a9a0c3SDan Williams 		ret = copy_to_iter(addr, bytes, i);
1161b3a9a0c3SDan Williams 		goto out;
1162b3a9a0c3SDan Williams 	}
1163b3a9a0c3SDan Williams 	ret = ti->type->dax_copy_to_iter(ti, pgoff, addr, bytes, i);
1164b3a9a0c3SDan Williams  out:
1165b3a9a0c3SDan Williams 	dm_put_live_table(md, srcu_idx);
1166b3a9a0c3SDan Williams 
1167b3a9a0c3SDan Williams 	return ret;
1168b3a9a0c3SDan Williams }
1169b3a9a0c3SDan Williams 
11701dd40c3eSMikulas Patocka /*
11711dd40c3eSMikulas Patocka  * A target may call dm_accept_partial_bio only from the map routine.  It is
1172c06b3e58SNeilBrown  * allowed for all bio types except REQ_PREFLUSH and REQ_OP_ZONE_RESET.
11731dd40c3eSMikulas Patocka  *
11741dd40c3eSMikulas Patocka  * dm_accept_partial_bio informs the dm that the target only wants to process
11751dd40c3eSMikulas Patocka  * additional n_sectors sectors of the bio and the rest of the data should be
11761dd40c3eSMikulas Patocka  * sent in a next bio.
11771dd40c3eSMikulas Patocka  *
11781dd40c3eSMikulas Patocka  * A diagram that explains the arithmetics:
11791dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
11801dd40c3eSMikulas Patocka  * |         1          |       2       |   3   |
11811dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
11821dd40c3eSMikulas Patocka  *
11831dd40c3eSMikulas Patocka  * <-------------- *tio->len_ptr --------------->
11841dd40c3eSMikulas Patocka  *                      <------- bi_size ------->
11851dd40c3eSMikulas Patocka  *                      <-- n_sectors -->
11861dd40c3eSMikulas Patocka  *
11871dd40c3eSMikulas Patocka  * Region 1 was already iterated over with bio_advance or similar function.
11881dd40c3eSMikulas Patocka  *	(it may be empty if the target doesn't use bio_advance)
11891dd40c3eSMikulas Patocka  * Region 2 is the remaining bio size that the target wants to process.
11901dd40c3eSMikulas Patocka  *	(it may be empty if region 1 is non-empty, although there is no reason
11911dd40c3eSMikulas Patocka  *	 to make it empty)
11921dd40c3eSMikulas Patocka  * The target requires that region 3 is to be sent in the next bio.
11931dd40c3eSMikulas Patocka  *
11941dd40c3eSMikulas Patocka  * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
11951dd40c3eSMikulas Patocka  * the partially processed part (the sum of regions 1+2) must be the same for all
11961dd40c3eSMikulas Patocka  * copies of the bio.
11971dd40c3eSMikulas Patocka  */
11981dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
11991dd40c3eSMikulas Patocka {
12001dd40c3eSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
12011dd40c3eSMikulas Patocka 	unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
12021eff9d32SJens Axboe 	BUG_ON(bio->bi_opf & REQ_PREFLUSH);
12031dd40c3eSMikulas Patocka 	BUG_ON(bi_size > *tio->len_ptr);
12041dd40c3eSMikulas Patocka 	BUG_ON(n_sectors > bi_size);
12051dd40c3eSMikulas Patocka 	*tio->len_ptr -= bi_size - n_sectors;
12061dd40c3eSMikulas Patocka 	bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
12071dd40c3eSMikulas Patocka }
12081dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
12091dd40c3eSMikulas Patocka 
1210d67a5f4bSMikulas Patocka /*
1211e76239a3SChristoph Hellwig  * The zone descriptors obtained with a zone report indicate
1212e76239a3SChristoph Hellwig  * zone positions within the underlying device of the target. The zone
1213e76239a3SChristoph Hellwig  * descriptors must be remapped to match their position within the dm device.
1214e76239a3SChristoph Hellwig  * The caller target should obtain the zones information using
1215e76239a3SChristoph Hellwig  * blkdev_report_zones() to ensure that remapping for partition offset is
1216e76239a3SChristoph Hellwig  * already handled.
121710999307SDamien Le Moal  */
1218e76239a3SChristoph Hellwig void dm_remap_zone_report(struct dm_target *ti, sector_t start,
1219e76239a3SChristoph Hellwig 			  struct blk_zone *zones, unsigned int *nr_zones)
122010999307SDamien Le Moal {
122110999307SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
122210999307SDamien Le Moal 	struct blk_zone *zone;
1223e76239a3SChristoph Hellwig 	unsigned int nrz = *nr_zones;
1224e76239a3SChristoph Hellwig 	int i;
122510999307SDamien Le Moal 
122610999307SDamien Le Moal 	/*
1227e76239a3SChristoph Hellwig 	 * Remap the start sector and write pointer position of the zones in
1228e76239a3SChristoph Hellwig 	 * the array. Since we may have obtained from the target underlying
1229e76239a3SChristoph Hellwig 	 * device more zones that the target size, also adjust the number
1230e76239a3SChristoph Hellwig 	 * of zones.
12319864cd5dSDamien Le Moal 	 */
1232e76239a3SChristoph Hellwig 	for (i = 0; i < nrz; i++) {
1233e76239a3SChristoph Hellwig 		zone = zones + i;
123410999307SDamien Le Moal 		if (zone->start >= start + ti->len) {
1235e76239a3SChristoph Hellwig 			memset(zone, 0, sizeof(struct blk_zone) * (nrz - i));
123610999307SDamien Le Moal 			break;
123710999307SDamien Le Moal 		}
1238e76239a3SChristoph Hellwig 
123910999307SDamien Le Moal 		zone->start = zone->start + ti->begin - start;
1240e76239a3SChristoph Hellwig 		if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
1241e76239a3SChristoph Hellwig 			continue;
1242e76239a3SChristoph Hellwig 
124310999307SDamien Le Moal 		if (zone->cond == BLK_ZONE_COND_FULL)
124410999307SDamien Le Moal 			zone->wp = zone->start + zone->len;
124510999307SDamien Le Moal 		else if (zone->cond == BLK_ZONE_COND_EMPTY)
124610999307SDamien Le Moal 			zone->wp = zone->start;
124710999307SDamien Le Moal 		else
1248e76239a3SChristoph Hellwig 			zone->wp = zone->wp + ti->begin - start;
124910999307SDamien Le Moal 	}
125010999307SDamien Le Moal 
1251e76239a3SChristoph Hellwig 	*nr_zones = i;
125210999307SDamien Le Moal #else /* !CONFIG_BLK_DEV_ZONED */
1253e76239a3SChristoph Hellwig 	*nr_zones = 0;
125410999307SDamien Le Moal #endif
125510999307SDamien Le Moal }
125610999307SDamien Le Moal EXPORT_SYMBOL_GPL(dm_remap_zone_report);
125710999307SDamien Le Moal 
1258978e51baSMike Snitzer static blk_qc_t __map_bio(struct dm_target_io *tio)
12591da177e4SLinus Torvalds {
12601da177e4SLinus Torvalds 	int r;
12612056a782SJens Axboe 	sector_t sector;
1262dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
126364f52b0eSMike Snitzer 	struct dm_io *io = tio->io;
1264978e51baSMike Snitzer 	struct mapped_device *md = io->md;
1265bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
1266978e51baSMike Snitzer 	blk_qc_t ret = BLK_QC_T_NONE;
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 	/*
12711da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
12721da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
12731da177e4SLinus Torvalds 	 * this io.
12741da177e4SLinus Torvalds 	 */
127564f52b0eSMike Snitzer 	atomic_inc(&io->io_count);
12764f024f37SKent Overstreet 	sector = clone->bi_iter.bi_sector;
1277d67a5f4bSMikulas Patocka 
12787de3ee57SMikulas Patocka 	r = ti->type->map(ti, clone);
1279846785e6SChristoph Hellwig 	switch (r) {
1280846785e6SChristoph Hellwig 	case DM_MAPIO_SUBMITTED:
1281846785e6SChristoph Hellwig 		break;
1282846785e6SChristoph Hellwig 	case DM_MAPIO_REMAPPED:
12831da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
128474d46992SChristoph Hellwig 		trace_block_bio_remap(clone->bi_disk->queue, clone,
128564f52b0eSMike Snitzer 				      bio_dev(io->orig_bio), sector);
1286978e51baSMike Snitzer 		if (md->type == DM_TYPE_NVME_BIO_BASED)
1287978e51baSMike Snitzer 			ret = direct_make_request(clone);
1288978e51baSMike Snitzer 		else
1289978e51baSMike Snitzer 			ret = generic_make_request(clone);
1290846785e6SChristoph Hellwig 		break;
1291846785e6SChristoph Hellwig 	case DM_MAPIO_KILL:
12924e4cbee9SChristoph Hellwig 		free_tio(tio);
129364f52b0eSMike Snitzer 		dec_pending(io, BLK_STS_IOERR);
12944e4cbee9SChristoph Hellwig 		break;
1295846785e6SChristoph Hellwig 	case DM_MAPIO_REQUEUE:
1296cfae7529SMike Snitzer 		free_tio(tio);
129764f52b0eSMike Snitzer 		dec_pending(io, BLK_STS_DM_REQUEUE);
1298846785e6SChristoph Hellwig 		break;
1299846785e6SChristoph Hellwig 	default:
130045cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
130145cbcd79SKiyoshi Ueda 		BUG();
13021da177e4SLinus Torvalds 	}
13031da177e4SLinus Torvalds 
1304978e51baSMike Snitzer 	return ret;
13051da177e4SLinus Torvalds }
13061da177e4SLinus Torvalds 
1307e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
1308bd2a49b8SAlasdair G Kergon {
13094f024f37SKent Overstreet 	bio->bi_iter.bi_sector = sector;
13104f024f37SKent Overstreet 	bio->bi_iter.bi_size = to_bytes(len);
13111da177e4SLinus Torvalds }
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds /*
13141da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
13151da177e4SLinus Torvalds  */
1316c80914e8SMike Snitzer static int clone_bio(struct dm_target_io *tio, struct bio *bio,
13171c3b13e6SKent Overstreet 		     sector_t sector, unsigned len)
13181da177e4SLinus Torvalds {
1319dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
13201da177e4SLinus Torvalds 
13211c3b13e6SKent Overstreet 	__bio_clone_fast(clone, bio);
13229c47008dSMartin K. Petersen 
132357c36519SMike Snitzer 	if (bio_integrity(bio)) {
1324e2460f2aSMikulas Patocka 		int r;
1325e2460f2aSMikulas Patocka 
1326e2460f2aSMikulas Patocka 		if (unlikely(!dm_target_has_integrity(tio->ti->type) &&
1327e2460f2aSMikulas Patocka 			     !dm_target_passes_integrity(tio->ti->type))) {
1328e2460f2aSMikulas Patocka 			DMWARN("%s: the target %s doesn't support integrity data.",
1329e2460f2aSMikulas Patocka 				dm_device_name(tio->io->md),
1330e2460f2aSMikulas Patocka 				tio->ti->type->name);
1331e2460f2aSMikulas Patocka 			return -EIO;
1332e2460f2aSMikulas Patocka 		}
1333e2460f2aSMikulas Patocka 
1334e2460f2aSMikulas Patocka 		r = bio_integrity_clone(clone, bio, GFP_NOIO);
1335c80914e8SMike Snitzer 		if (r < 0)
1336c80914e8SMike Snitzer 			return r;
1337c80914e8SMike Snitzer 	}
13381c3b13e6SKent Overstreet 
1339fa8db494SMike Snitzer 	bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1340fa8db494SMike Snitzer 	clone->bi_iter.bi_size = to_bytes(len);
1341fa8db494SMike Snitzer 
1342fa8db494SMike Snitzer 	if (bio_integrity(bio))
1343fa8db494SMike Snitzer 		bio_integrity_trim(clone);
1344c80914e8SMike Snitzer 
1345c80914e8SMike Snitzer 	return 0;
13461da177e4SLinus Torvalds }
13471da177e4SLinus Torvalds 
1348318716ddSMike Snitzer static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci,
1349318716ddSMike Snitzer 				struct dm_target *ti, unsigned num_bios)
1350f9ab94ceSMikulas Patocka {
1351dba14160SMikulas Patocka 	struct dm_target_io *tio;
1352318716ddSMike Snitzer 	int try;
1353dba14160SMikulas Patocka 
1354318716ddSMike Snitzer 	if (!num_bios)
1355318716ddSMike Snitzer 		return;
1356f9ab94ceSMikulas Patocka 
1357318716ddSMike Snitzer 	if (num_bios == 1) {
1358318716ddSMike Snitzer 		tio = alloc_tio(ci, ti, 0, GFP_NOIO);
1359318716ddSMike Snitzer 		bio_list_add(blist, &tio->clone);
1360318716ddSMike Snitzer 		return;
13619015df24SAlasdair G Kergon 	}
13629015df24SAlasdair G Kergon 
1363318716ddSMike Snitzer 	for (try = 0; try < 2; try++) {
1364318716ddSMike Snitzer 		int bio_nr;
1365318716ddSMike Snitzer 		struct bio *bio;
1366318716ddSMike Snitzer 
1367318716ddSMike Snitzer 		if (try)
1368bc02cdbeSMike Snitzer 			mutex_lock(&ci->io->md->table_devices_lock);
1369318716ddSMike Snitzer 		for (bio_nr = 0; bio_nr < num_bios; bio_nr++) {
1370318716ddSMike Snitzer 			tio = alloc_tio(ci, ti, bio_nr, try ? GFP_NOIO : GFP_NOWAIT);
1371318716ddSMike Snitzer 			if (!tio)
1372318716ddSMike Snitzer 				break;
1373318716ddSMike Snitzer 
1374318716ddSMike Snitzer 			bio_list_add(blist, &tio->clone);
1375318716ddSMike Snitzer 		}
1376318716ddSMike Snitzer 		if (try)
1377bc02cdbeSMike Snitzer 			mutex_unlock(&ci->io->md->table_devices_lock);
1378318716ddSMike Snitzer 		if (bio_nr == num_bios)
1379318716ddSMike Snitzer 			return;
1380318716ddSMike Snitzer 
1381318716ddSMike Snitzer 		while ((bio = bio_list_pop(blist))) {
1382318716ddSMike Snitzer 			tio = container_of(bio, struct dm_target_io, clone);
1383318716ddSMike Snitzer 			free_tio(tio);
1384318716ddSMike Snitzer 		}
1385318716ddSMike Snitzer 	}
1386318716ddSMike Snitzer }
1387318716ddSMike Snitzer 
1388978e51baSMike Snitzer static blk_qc_t __clone_and_map_simple_bio(struct clone_info *ci,
1389318716ddSMike Snitzer 					   struct dm_target_io *tio, unsigned *len)
13909015df24SAlasdair G Kergon {
1391dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
13929015df24SAlasdair G Kergon 
13931dd40c3eSMikulas Patocka 	tio->len_ptr = len;
13941dd40c3eSMikulas Patocka 
13951c3b13e6SKent Overstreet 	__bio_clone_fast(clone, ci->bio);
1396bd2a49b8SAlasdair G Kergon 	if (len)
13971dd40c3eSMikulas Patocka 		bio_setup_sector(clone, ci->sector, *len);
1398f9ab94ceSMikulas Patocka 
1399978e51baSMike Snitzer 	return __map_bio(tio);
1400f9ab94ceSMikulas Patocka }
1401f9ab94ceSMikulas Patocka 
140214fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
14031dd40c3eSMikulas Patocka 				  unsigned num_bios, unsigned *len)
140406a426ceSMike Snitzer {
1405318716ddSMike Snitzer 	struct bio_list blist = BIO_EMPTY_LIST;
1406318716ddSMike Snitzer 	struct bio *bio;
1407318716ddSMike Snitzer 	struct dm_target_io *tio;
140806a426ceSMike Snitzer 
1409318716ddSMike Snitzer 	alloc_multiple_bios(&blist, ci, ti, num_bios);
1410318716ddSMike Snitzer 
1411318716ddSMike Snitzer 	while ((bio = bio_list_pop(&blist))) {
1412318716ddSMike Snitzer 		tio = container_of(bio, struct dm_target_io, clone);
1413978e51baSMike Snitzer 		(void) __clone_and_map_simple_bio(ci, tio, len);
1414318716ddSMike Snitzer 	}
141506a426ceSMike Snitzer }
141606a426ceSMike Snitzer 
141714fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1418f9ab94ceSMikulas Patocka {
141906a426ceSMike Snitzer 	unsigned target_nr = 0;
1420f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1421f9ab94ceSMikulas Patocka 
1422892ad71fSDennis Zhou 	/*
1423dbe3ece1SJens Axboe 	 * Empty flush uses a statically initialized bio, as the base for
1424dbe3ece1SJens Axboe 	 * cloning.  However, blkg association requires that a bdev is
1425dbe3ece1SJens Axboe 	 * associated with a gendisk, which doesn't happen until the bdev is
1426dbe3ece1SJens Axboe 	 * opened.  So, blkg association is done at issue time of the flush
1427dbe3ece1SJens Axboe 	 * rather than when the device is created in alloc_dev().
1428892ad71fSDennis Zhou 	 */
1429892ad71fSDennis Zhou 	bio_set_dev(ci->bio, ci->io->md->bdev);
1430892ad71fSDennis Zhou 
1431b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1432f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
14331dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
1434f9ab94ceSMikulas Patocka 
1435892ad71fSDennis Zhou 	bio_disassociate_blkg(ci->bio);
1436892ad71fSDennis Zhou 
1437f9ab94ceSMikulas Patocka 	return 0;
1438f9ab94ceSMikulas Patocka }
1439f9ab94ceSMikulas Patocka 
1440c80914e8SMike Snitzer static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
14411dd40c3eSMikulas Patocka 				    sector_t sector, unsigned *len)
14425ae89a87SMike Snitzer {
1443dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
14445ae89a87SMike Snitzer 	struct dm_target_io *tio;
1445f31c21e4SNeilBrown 	int r;
14465ae89a87SMike Snitzer 
1447318716ddSMike Snitzer 	tio = alloc_tio(ci, ti, 0, GFP_NOIO);
14481dd40c3eSMikulas Patocka 	tio->len_ptr = len;
1449c80914e8SMike Snitzer 	r = clone_bio(tio, bio, sector, *len);
1450072623deSMikulas Patocka 	if (r < 0) {
1451cfae7529SMike Snitzer 		free_tio(tio);
1452c80914e8SMike Snitzer 		return r;
1453b0d8ed4dSAlasdair G Kergon 	}
1454978e51baSMike Snitzer 	(void) __map_bio(tio);
145555a62eefSAlasdair G Kergon 
1456f31c21e4SNeilBrown 	return 0;
145723508a96SMike Snitzer }
145855a62eefSAlasdair G Kergon 
145923508a96SMike Snitzer typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
146055a62eefSAlasdair G Kergon 
146123508a96SMike Snitzer static unsigned get_num_discard_bios(struct dm_target *ti)
146223508a96SMike Snitzer {
146323508a96SMike Snitzer 	return ti->num_discard_bios;
146423508a96SMike Snitzer }
146523508a96SMike Snitzer 
146600716545SDenis Semakin static unsigned get_num_secure_erase_bios(struct dm_target *ti)
146700716545SDenis Semakin {
146800716545SDenis Semakin 	return ti->num_secure_erase_bios;
146900716545SDenis Semakin }
147000716545SDenis Semakin 
147123508a96SMike Snitzer static unsigned get_num_write_same_bios(struct dm_target *ti)
147223508a96SMike Snitzer {
147323508a96SMike Snitzer 	return ti->num_write_same_bios;
147423508a96SMike Snitzer }
147523508a96SMike Snitzer 
1476ac62d620SChristoph Hellwig static unsigned get_num_write_zeroes_bios(struct dm_target *ti)
1477ac62d620SChristoph Hellwig {
1478ac62d620SChristoph Hellwig 	return ti->num_write_zeroes_bios;
1479ac62d620SChristoph Hellwig }
1480ac62d620SChristoph Hellwig 
14813d7f4562SMike Snitzer static int __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti,
1482*61697a6aSMike Snitzer 				       unsigned num_bios)
14835ae89a87SMike Snitzer {
1484*61697a6aSMike Snitzer 	unsigned len = ci->sector_count;
14855ae89a87SMike Snitzer 
14865ae89a87SMike Snitzer 	/*
148723508a96SMike Snitzer 	 * Even though the device advertised support for this type of
148823508a96SMike Snitzer 	 * request, that does not mean every target supports it, and
1489936688d7SMike Snitzer 	 * reconfiguration might also have changed that since the
14905ae89a87SMike Snitzer 	 * check was performed.
14915ae89a87SMike Snitzer 	 */
149255a62eefSAlasdair G Kergon 	if (!num_bios)
14935ae89a87SMike Snitzer 		return -EOPNOTSUPP;
14945ae89a87SMike Snitzer 
14951dd40c3eSMikulas Patocka 	__send_duplicate_bios(ci, ti, num_bios, &len);
14965ae89a87SMike Snitzer 
1497a79245b3SMike Snitzer 	ci->sector += len;
14983d7f4562SMike Snitzer 	ci->sector_count -= len;
14995ae89a87SMike Snitzer 
15005ae89a87SMike Snitzer 	return 0;
15015ae89a87SMike Snitzer }
15025ae89a87SMike Snitzer 
15033d7f4562SMike Snitzer static int __send_discard(struct clone_info *ci, struct dm_target *ti)
150423508a96SMike Snitzer {
1505*61697a6aSMike Snitzer 	return __send_changing_extent_only(ci, ti, get_num_discard_bios(ti));
150623508a96SMike Snitzer }
150723508a96SMike Snitzer 
150800716545SDenis Semakin static int __send_secure_erase(struct clone_info *ci, struct dm_target *ti)
150900716545SDenis Semakin {
1510*61697a6aSMike Snitzer 	return __send_changing_extent_only(ci, ti, get_num_secure_erase_bios(ti));
151100716545SDenis Semakin }
151200716545SDenis Semakin 
15133d7f4562SMike Snitzer static int __send_write_same(struct clone_info *ci, struct dm_target *ti)
151423508a96SMike Snitzer {
1515*61697a6aSMike Snitzer 	return __send_changing_extent_only(ci, ti, get_num_write_same_bios(ti));
151623508a96SMike Snitzer }
151723508a96SMike Snitzer 
15183d7f4562SMike Snitzer static int __send_write_zeroes(struct clone_info *ci, struct dm_target *ti)
1519ac62d620SChristoph Hellwig {
1520*61697a6aSMike Snitzer 	return __send_changing_extent_only(ci, ti, get_num_write_zeroes_bios(ti));
1521ac62d620SChristoph Hellwig }
1522ac62d620SChristoph Hellwig 
1523568c73a3SMike Snitzer static bool is_abnormal_io(struct bio *bio)
1524568c73a3SMike Snitzer {
1525568c73a3SMike Snitzer 	bool r = false;
1526568c73a3SMike Snitzer 
1527568c73a3SMike Snitzer 	switch (bio_op(bio)) {
1528568c73a3SMike Snitzer 	case REQ_OP_DISCARD:
1529568c73a3SMike Snitzer 	case REQ_OP_SECURE_ERASE:
1530568c73a3SMike Snitzer 	case REQ_OP_WRITE_SAME:
1531568c73a3SMike Snitzer 	case REQ_OP_WRITE_ZEROES:
1532568c73a3SMike Snitzer 		r = true;
1533568c73a3SMike Snitzer 		break;
1534568c73a3SMike Snitzer 	}
1535568c73a3SMike Snitzer 
1536568c73a3SMike Snitzer 	return r;
1537568c73a3SMike Snitzer }
1538568c73a3SMike Snitzer 
15390519c71eSMike Snitzer static bool __process_abnormal_io(struct clone_info *ci, struct dm_target *ti,
15400519c71eSMike Snitzer 				  int *result)
15410519c71eSMike Snitzer {
15420519c71eSMike Snitzer 	struct bio *bio = ci->bio;
15430519c71eSMike Snitzer 
15440519c71eSMike Snitzer 	if (bio_op(bio) == REQ_OP_DISCARD)
15450519c71eSMike Snitzer 		*result = __send_discard(ci, ti);
154600716545SDenis Semakin 	else if (bio_op(bio) == REQ_OP_SECURE_ERASE)
154700716545SDenis Semakin 		*result = __send_secure_erase(ci, ti);
15480519c71eSMike Snitzer 	else if (bio_op(bio) == REQ_OP_WRITE_SAME)
15490519c71eSMike Snitzer 		*result = __send_write_same(ci, ti);
15500519c71eSMike Snitzer 	else if (bio_op(bio) == REQ_OP_WRITE_ZEROES)
15510519c71eSMike Snitzer 		*result = __send_write_zeroes(ci, ti);
15520519c71eSMike Snitzer 	else
15530519c71eSMike Snitzer 		return false;
15540519c71eSMike Snitzer 
15550519c71eSMike Snitzer 	return true;
15560519c71eSMike Snitzer }
15570519c71eSMike Snitzer 
1558e4c93811SAlasdair G Kergon /*
1559e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1560e4c93811SAlasdair G Kergon  */
1561e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci)
1562e4c93811SAlasdair G Kergon {
1563e4c93811SAlasdair G Kergon 	struct dm_target *ti;
15641c3b13e6SKent Overstreet 	unsigned len;
1565c80914e8SMike Snitzer 	int r;
1566e4c93811SAlasdair G Kergon 
1567e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1568e4c93811SAlasdair G Kergon 	if (!dm_target_is_valid(ti))
1569e4c93811SAlasdair G Kergon 		return -EIO;
1570e4c93811SAlasdair G Kergon 
1571568c73a3SMike Snitzer 	if (__process_abnormal_io(ci, ti, &r))
15720519c71eSMike Snitzer 		return r;
15733d7f4562SMike Snitzer 
1574e76239a3SChristoph Hellwig 	len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
1575e4c93811SAlasdair G Kergon 
1576c80914e8SMike Snitzer 	r = __clone_and_map_data_bio(ci, ti, ci->sector, &len);
1577c80914e8SMike Snitzer 	if (r < 0)
1578c80914e8SMike Snitzer 		return r;
1579e4c93811SAlasdair G Kergon 
1580e4c93811SAlasdair G Kergon 	ci->sector += len;
1581e4c93811SAlasdair G Kergon 	ci->sector_count -= len;
1582e4c93811SAlasdair G Kergon 
1583e4c93811SAlasdair G Kergon 	return 0;
1584e4c93811SAlasdair G Kergon }
1585e4c93811SAlasdair G Kergon 
1586978e51baSMike Snitzer static void init_clone_info(struct clone_info *ci, struct mapped_device *md,
1587978e51baSMike Snitzer 			    struct dm_table *map, struct bio *bio)
1588978e51baSMike Snitzer {
1589978e51baSMike Snitzer 	ci->map = map;
1590978e51baSMike Snitzer 	ci->io = alloc_io(md, bio);
1591978e51baSMike Snitzer 	ci->sector = bio->bi_iter.bi_sector;
1592978e51baSMike Snitzer }
1593978e51baSMike Snitzer 
1594a1e1cb72SMike Snitzer #define __dm_part_stat_sub(part, field, subnd)	\
1595a1e1cb72SMike Snitzer 	(part_stat_get(part, field) -= (subnd))
1596a1e1cb72SMike Snitzer 
1597e4c93811SAlasdair G Kergon /*
159814fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
15991da177e4SLinus Torvalds  */
1600978e51baSMike Snitzer static blk_qc_t __split_and_process_bio(struct mapped_device *md,
160183d5e5b0SMikulas Patocka 					struct dm_table *map, struct bio *bio)
16021da177e4SLinus Torvalds {
16031da177e4SLinus Torvalds 	struct clone_info ci;
1604978e51baSMike Snitzer 	blk_qc_t ret = BLK_QC_T_NONE;
1605512875bdSJun'ichi Nomura 	int error = 0;
16061da177e4SLinus Torvalds 
1607978e51baSMike Snitzer 	init_clone_info(&ci, md, map, bio);
1608bd2a49b8SAlasdair G Kergon 
16091eff9d32SJens Axboe 	if (bio->bi_opf & REQ_PREFLUSH) {
1610dbe3ece1SJens Axboe 		struct bio flush_bio;
1611dbe3ece1SJens Axboe 
1612dbe3ece1SJens Axboe 		/*
1613dbe3ece1SJens Axboe 		 * Use an on-stack bio for this, it's safe since we don't
1614dbe3ece1SJens Axboe 		 * need to reference it after submit. It's just used as
1615dbe3ece1SJens Axboe 		 * the basis for the clone(s).
1616dbe3ece1SJens Axboe 		 */
1617dbe3ece1SJens Axboe 		bio_init(&flush_bio, NULL, 0);
1618dbe3ece1SJens Axboe 		flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
1619dbe3ece1SJens Axboe 		ci.bio = &flush_bio;
1620b372d360SMike Snitzer 		ci.sector_count = 0;
162114fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1622b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1623a4aa5e56SDamien Le Moal 	} else if (bio_op(bio) == REQ_OP_ZONE_RESET) {
1624a4aa5e56SDamien Le Moal 		ci.bio = bio;
1625a4aa5e56SDamien Le Moal 		ci.sector_count = 0;
1626a4aa5e56SDamien Le Moal 		error = __split_and_process_non_flush(&ci);
1627b372d360SMike Snitzer 	} else {
16286a8736d1STejun Heo 		ci.bio = bio;
16291da177e4SLinus Torvalds 		ci.sector_count = bio_sectors(bio);
163018a25da8SNeilBrown 		while (ci.sector_count && !error) {
163114fe594dSAlasdair G Kergon 			error = __split_and_process_non_flush(&ci);
163218a25da8SNeilBrown 			if (current->bio_list && ci.sector_count && !error) {
163318a25da8SNeilBrown 				/*
163418a25da8SNeilBrown 				 * Remainder must be passed to generic_make_request()
163518a25da8SNeilBrown 				 * so that it gets handled *after* bios already submitted
163618a25da8SNeilBrown 				 * have been completely processed.
163718a25da8SNeilBrown 				 * We take a clone of the original to store in
1638745dc570SMike Snitzer 				 * ci.io->orig_bio to be used by end_io_acct() and
163918a25da8SNeilBrown 				 * for dec_pending to use for completion handling.
164018a25da8SNeilBrown 				 */
1641f21c601aSMike Snitzer 				struct bio *b = bio_split(bio, bio_sectors(bio) - ci.sector_count,
1642f21c601aSMike Snitzer 							  GFP_NOIO, &md->queue->bio_split);
1643745dc570SMike Snitzer 				ci.io->orig_bio = b;
1644a1e1cb72SMike Snitzer 
1645a1e1cb72SMike Snitzer 				/*
1646a1e1cb72SMike Snitzer 				 * Adjust IO stats for each split, otherwise upon queue
1647a1e1cb72SMike Snitzer 				 * reentry there will be redundant IO accounting.
1648a1e1cb72SMike Snitzer 				 * NOTE: this is a stop-gap fix, a proper fix involves
1649a1e1cb72SMike Snitzer 				 * significant refactoring of DM core's bio splitting
1650a1e1cb72SMike Snitzer 				 * (by eliminating DM's splitting and just using bio_split)
1651a1e1cb72SMike Snitzer 				 */
1652a1e1cb72SMike Snitzer 				part_stat_lock();
1653a1e1cb72SMike Snitzer 				__dm_part_stat_sub(&dm_disk(md)->part0,
1654a1e1cb72SMike Snitzer 						   sectors[op_stat_group(bio_op(bio))], ci.sector_count);
1655a1e1cb72SMike Snitzer 				part_stat_unlock();
1656a1e1cb72SMike Snitzer 
165718a25da8SNeilBrown 				bio_chain(b, bio);
1658075c18c3SMike Snitzer 				trace_block_split(md->queue, b, bio->bi_iter.bi_sector);
1659978e51baSMike Snitzer 				ret = generic_make_request(bio);
166018a25da8SNeilBrown 				break;
166118a25da8SNeilBrown 			}
166218a25da8SNeilBrown 		}
1663d87f4c14STejun Heo 	}
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds 	/* drop the extra reference count */
166654385bf7SBart Van Assche 	dec_pending(ci.io, errno_to_blk_status(error));
1667978e51baSMike Snitzer 	return ret;
16681da177e4SLinus Torvalds }
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds /*
1671978e51baSMike Snitzer  * Optimized variant of __split_and_process_bio that leverages the
1672978e51baSMike Snitzer  * fact that targets that use it do _not_ have a need to split bios.
16731da177e4SLinus Torvalds  */
1674568c73a3SMike Snitzer static blk_qc_t __process_bio(struct mapped_device *md, struct dm_table *map,
1675568c73a3SMike Snitzer 			      struct bio *bio, struct dm_target *ti)
16761da177e4SLinus Torvalds {
1677978e51baSMike Snitzer 	struct clone_info ci;
1678978e51baSMike Snitzer 	blk_qc_t ret = BLK_QC_T_NONE;
1679978e51baSMike Snitzer 	int error = 0;
1680978e51baSMike Snitzer 
1681978e51baSMike Snitzer 	init_clone_info(&ci, md, map, bio);
1682978e51baSMike Snitzer 
1683978e51baSMike Snitzer 	if (bio->bi_opf & REQ_PREFLUSH) {
1684dbe3ece1SJens Axboe 		struct bio flush_bio;
1685dbe3ece1SJens Axboe 
1686dbe3ece1SJens Axboe 		/*
1687dbe3ece1SJens Axboe 		 * Use an on-stack bio for this, it's safe since we don't
1688dbe3ece1SJens Axboe 		 * need to reference it after submit. It's just used as
1689dbe3ece1SJens Axboe 		 * the basis for the clone(s).
1690dbe3ece1SJens Axboe 		 */
1691dbe3ece1SJens Axboe 		bio_init(&flush_bio, NULL, 0);
1692dbe3ece1SJens Axboe 		flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
1693dbe3ece1SJens Axboe 		ci.bio = &flush_bio;
1694978e51baSMike Snitzer 		ci.sector_count = 0;
1695978e51baSMike Snitzer 		error = __send_empty_flush(&ci);
1696978e51baSMike Snitzer 		/* dec_pending submits any data associated with flush */
1697978e51baSMike Snitzer 	} else {
1698978e51baSMike Snitzer 		struct dm_target_io *tio;
1699978e51baSMike Snitzer 
1700978e51baSMike Snitzer 		ci.bio = bio;
1701978e51baSMike Snitzer 		ci.sector_count = bio_sectors(bio);
1702568c73a3SMike Snitzer 		if (__process_abnormal_io(&ci, ti, &error))
17030519c71eSMike Snitzer 			goto out;
17040519c71eSMike Snitzer 
17050519c71eSMike Snitzer 		tio = alloc_tio(&ci, ti, 0, GFP_NOIO);
1706978e51baSMike Snitzer 		ret = __clone_and_map_simple_bio(&ci, tio, NULL);
1707978e51baSMike Snitzer 	}
1708978e51baSMike Snitzer out:
1709978e51baSMike Snitzer 	/* drop the extra reference count */
1710978e51baSMike Snitzer 	dec_pending(ci.io, errno_to_blk_status(error));
1711978e51baSMike Snitzer 	return ret;
1712978e51baSMike Snitzer }
1713978e51baSMike Snitzer 
1714568c73a3SMike Snitzer static void dm_queue_split(struct mapped_device *md, struct dm_target *ti, struct bio **bio)
1715568c73a3SMike Snitzer {
1716568c73a3SMike Snitzer 	unsigned len, sector_count;
1717568c73a3SMike Snitzer 
1718568c73a3SMike Snitzer 	sector_count = bio_sectors(*bio);
1719568c73a3SMike Snitzer 	len = min_t(sector_t, max_io_len((*bio)->bi_iter.bi_sector, ti), sector_count);
1720568c73a3SMike Snitzer 
1721568c73a3SMike Snitzer 	if (sector_count > len) {
1722568c73a3SMike Snitzer 		struct bio *split = bio_split(*bio, len, GFP_NOIO, &md->queue->bio_split);
1723568c73a3SMike Snitzer 
1724568c73a3SMike Snitzer 		bio_chain(split, *bio);
1725568c73a3SMike Snitzer 		trace_block_split(md->queue, split, (*bio)->bi_iter.bi_sector);
1726568c73a3SMike Snitzer 		generic_make_request(*bio);
1727568c73a3SMike Snitzer 		*bio = split;
1728568c73a3SMike Snitzer 	}
1729568c73a3SMike Snitzer }
1730568c73a3SMike Snitzer 
17316548c7c5SMike Snitzer static blk_qc_t dm_process_bio(struct mapped_device *md,
17326548c7c5SMike Snitzer 			       struct dm_table *map, struct bio *bio)
17336548c7c5SMike Snitzer {
1734568c73a3SMike Snitzer 	blk_qc_t ret = BLK_QC_T_NONE;
1735568c73a3SMike Snitzer 	struct dm_target *ti = md->immutable_target;
1736568c73a3SMike Snitzer 
1737568c73a3SMike Snitzer 	if (unlikely(!map)) {
1738568c73a3SMike Snitzer 		bio_io_error(bio);
1739568c73a3SMike Snitzer 		return ret;
1740568c73a3SMike Snitzer 	}
1741568c73a3SMike Snitzer 
1742568c73a3SMike Snitzer 	if (!ti) {
1743568c73a3SMike Snitzer 		ti = dm_table_find_target(map, bio->bi_iter.bi_sector);
1744568c73a3SMike Snitzer 		if (unlikely(!ti || !dm_target_is_valid(ti))) {
1745568c73a3SMike Snitzer 			bio_io_error(bio);
1746568c73a3SMike Snitzer 			return ret;
1747568c73a3SMike Snitzer 		}
1748568c73a3SMike Snitzer 	}
1749568c73a3SMike Snitzer 
1750568c73a3SMike Snitzer 	/*
1751568c73a3SMike Snitzer 	 * If in ->make_request_fn we need to use blk_queue_split(), otherwise
1752568c73a3SMike Snitzer 	 * queue_limits for abnormal requests (e.g. discard, writesame, etc)
1753568c73a3SMike Snitzer 	 * won't be imposed.
1754568c73a3SMike Snitzer 	 */
1755568c73a3SMike Snitzer 	if (current->bio_list) {
1756568c73a3SMike Snitzer 		if (is_abnormal_io(bio))
1757568c73a3SMike Snitzer 			blk_queue_split(md->queue, &bio);
1758568c73a3SMike Snitzer 		else
1759568c73a3SMike Snitzer 			dm_queue_split(md, ti, &bio);
1760568c73a3SMike Snitzer 	}
1761568c73a3SMike Snitzer 
17626548c7c5SMike Snitzer 	if (dm_get_md_type(md) == DM_TYPE_NVME_BIO_BASED)
1763568c73a3SMike Snitzer 		return __process_bio(md, map, bio, ti);
17646548c7c5SMike Snitzer 	else
17656548c7c5SMike Snitzer 		return __split_and_process_bio(md, map, bio);
17666548c7c5SMike Snitzer }
17676548c7c5SMike Snitzer 
176824113d48SMikulas Patocka static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
17691da177e4SLinus Torvalds {
17701da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
1771978e51baSMike Snitzer 	blk_qc_t ret = BLK_QC_T_NONE;
177283d5e5b0SMikulas Patocka 	int srcu_idx;
177383d5e5b0SMikulas Patocka 	struct dm_table *map;
17741da177e4SLinus Torvalds 
177583d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
17761da177e4SLinus Torvalds 
17776a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
17786a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
177983d5e5b0SMikulas Patocka 		dm_put_live_table(md, srcu_idx);
17801da177e4SLinus Torvalds 
17811eff9d32SJens Axboe 		if (!(bio->bi_opf & REQ_RAHEAD))
178292c63902SMikulas Patocka 			queue_io(md, bio);
17836a8736d1STejun Heo 		else
17846a8736d1STejun Heo 			bio_io_error(bio);
1785978e51baSMike Snitzer 		return ret;
17861da177e4SLinus Torvalds 	}
17871da177e4SLinus Torvalds 
17886548c7c5SMike Snitzer 	ret = dm_process_bio(md, map, bio);
1789978e51baSMike Snitzer 
179083d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
1791978e51baSMike Snitzer 	return ret;
1792978e51baSMike Snitzer }
1793978e51baSMike Snitzer 
17941da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
17951da177e4SLinus Torvalds {
17968a57dfc6SChandra Seetharaman 	int r = bdi_bits;
17978a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
17988a57dfc6SChandra Seetharaman 	struct dm_table *map;
17991da177e4SLinus Torvalds 
18001eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
1801e522c039SMike Snitzer 		if (dm_request_based(md)) {
1802cec47e3dSKiyoshi Ueda 			/*
1803e522c039SMike Snitzer 			 * With request-based DM we only need to check the
1804e522c039SMike Snitzer 			 * top-level queue for congestion.
1805cec47e3dSKiyoshi Ueda 			 */
1806dc3b17ccSJan Kara 			r = md->queue->backing_dev_info->wb.state & bdi_bits;
1807e522c039SMike Snitzer 		} else {
1808e522c039SMike Snitzer 			map = dm_get_live_table_fast(md);
1809e522c039SMike Snitzer 			if (map)
18101da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
181183d5e5b0SMikulas Patocka 			dm_put_live_table_fast(md);
18128a57dfc6SChandra Seetharaman 		}
1813e522c039SMike Snitzer 	}
18148a57dfc6SChandra Seetharaman 
18151da177e4SLinus Torvalds 	return r;
18161da177e4SLinus Torvalds }
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds /*-----------------------------------------------------------------
18191da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
18201da177e4SLinus Torvalds  *---------------------------------------------------------------*/
18212b06cfffSAlasdair G Kergon static void free_minor(int minor)
18221da177e4SLinus Torvalds {
1823f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
18241da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1825f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
18261da177e4SLinus Torvalds }
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds /*
18291da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
18301da177e4SLinus Torvalds  */
1831cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
18321da177e4SLinus Torvalds {
1833c9d76be6STejun Heo 	int r;
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
18361da177e4SLinus Torvalds 		return -EINVAL;
18371da177e4SLinus Torvalds 
1838c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1839f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
18401da177e4SLinus Torvalds 
1841c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
18421da177e4SLinus Torvalds 
1843f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1844c9d76be6STejun Heo 	idr_preload_end();
1845c9d76be6STejun Heo 	if (r < 0)
1846c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
1847c9d76be6STejun Heo 	return 0;
18481da177e4SLinus Torvalds }
18491da177e4SLinus Torvalds 
1850cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
18511da177e4SLinus Torvalds {
1852c9d76be6STejun Heo 	int r;
18531da177e4SLinus Torvalds 
1854c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1855f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
18561da177e4SLinus Torvalds 
1857c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
18581da177e4SLinus Torvalds 
1859f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1860c9d76be6STejun Heo 	idr_preload_end();
1861c9d76be6STejun Heo 	if (r < 0)
18621da177e4SLinus Torvalds 		return r;
1863c9d76be6STejun Heo 	*minor = r;
1864c9d76be6STejun Heo 	return 0;
18651da177e4SLinus Torvalds }
18661da177e4SLinus Torvalds 
186783d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
1868f26c5719SDan Williams static const struct dax_operations dm_dax_ops;
18691da177e4SLinus Torvalds 
187053d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
187153d5914fSMikulas Patocka 
1872c12c9a3cSMike Snitzer static void dm_init_normal_md_queue(struct mapped_device *md)
1873bfebd1cdSMike Snitzer {
1874bfebd1cdSMike Snitzer 	/*
1875bfebd1cdSMike Snitzer 	 * Initialize aspects of queue that aren't relevant for blk-mq
1876bfebd1cdSMike Snitzer 	 */
1877dc3b17ccSJan Kara 	md->queue->backing_dev_info->congested_fn = dm_any_congested;
18784a0b4ddfSMike Snitzer }
18794a0b4ddfSMike Snitzer 
18800f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md)
18810f20972fSMike Snitzer {
18820f20972fSMike Snitzer 	if (md->wq)
18830f20972fSMike Snitzer 		destroy_workqueue(md->wq);
18846f1c819cSKent Overstreet 	bioset_exit(&md->bs);
18856f1c819cSKent Overstreet 	bioset_exit(&md->io_bs);
18860f20972fSMike Snitzer 
1887f26c5719SDan Williams 	if (md->dax_dev) {
1888f26c5719SDan Williams 		kill_dax(md->dax_dev);
1889f26c5719SDan Williams 		put_dax(md->dax_dev);
1890f26c5719SDan Williams 		md->dax_dev = NULL;
1891f26c5719SDan Williams 	}
1892f26c5719SDan Williams 
18930f20972fSMike Snitzer 	if (md->disk) {
18940f20972fSMike Snitzer 		spin_lock(&_minor_lock);
18950f20972fSMike Snitzer 		md->disk->private_data = NULL;
18960f20972fSMike Snitzer 		spin_unlock(&_minor_lock);
18970f20972fSMike Snitzer 		del_gendisk(md->disk);
18980f20972fSMike Snitzer 		put_disk(md->disk);
18990f20972fSMike Snitzer 	}
19000f20972fSMike Snitzer 
19010f20972fSMike Snitzer 	if (md->queue)
19020f20972fSMike Snitzer 		blk_cleanup_queue(md->queue);
19030f20972fSMike Snitzer 
1904d09960b0STahsin Erdogan 	cleanup_srcu_struct(&md->io_barrier);
1905d09960b0STahsin Erdogan 
19060f20972fSMike Snitzer 	if (md->bdev) {
19070f20972fSMike Snitzer 		bdput(md->bdev);
19080f20972fSMike Snitzer 		md->bdev = NULL;
19090f20972fSMike Snitzer 	}
19104cc96131SMike Snitzer 
1911d5ffebddSMike Snitzer 	mutex_destroy(&md->suspend_lock);
1912d5ffebddSMike Snitzer 	mutex_destroy(&md->type_lock);
1913d5ffebddSMike Snitzer 	mutex_destroy(&md->table_devices_lock);
1914d5ffebddSMike Snitzer 
19154cc96131SMike Snitzer 	dm_mq_cleanup_mapped_device(md);
19160f20972fSMike Snitzer }
19170f20972fSMike Snitzer 
19181da177e4SLinus Torvalds /*
19191da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
19201da177e4SLinus Torvalds  */
19212b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
19221da177e4SLinus Torvalds {
1923115485e8SMike Snitzer 	int r, numa_node_id = dm_get_numa_node();
1924976431b0SDan Williams 	struct dax_device *dax_dev = NULL;
1925115485e8SMike Snitzer 	struct mapped_device *md;
1926ba61fdd1SJeff Mahoney 	void *old_md;
19271da177e4SLinus Torvalds 
1928856eb091SMikulas Patocka 	md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
19291da177e4SLinus Torvalds 	if (!md) {
19301da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
19311da177e4SLinus Torvalds 		return NULL;
19321da177e4SLinus Torvalds 	}
19331da177e4SLinus Torvalds 
193410da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
19356ed7ade8SMilan Broz 		goto bad_module_get;
193610da4f79SJeff Mahoney 
19371da177e4SLinus Torvalds 	/* get a minor number for the dev */
19382b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1939cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
19402b06cfffSAlasdair G Kergon 	else
1941cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
19421da177e4SLinus Torvalds 	if (r < 0)
19436ed7ade8SMilan Broz 		goto bad_minor;
19441da177e4SLinus Torvalds 
194583d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
194683d5e5b0SMikulas Patocka 	if (r < 0)
194783d5e5b0SMikulas Patocka 		goto bad_io_barrier;
194883d5e5b0SMikulas Patocka 
1949115485e8SMike Snitzer 	md->numa_node_id = numa_node_id;
1950591ddcfcSMike Snitzer 	md->init_tio_pdu = false;
1951a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
1952e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1953a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
195486f1152bSBenjamin Marzinski 	mutex_init(&md->table_devices_lock);
1955022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
19561da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
19575c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
19581da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
19597a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
19607a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
196186f1152bSBenjamin Marzinski 	INIT_LIST_HEAD(&md->table_devices);
19627a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
19631da177e4SLinus Torvalds 
19646d469642SChristoph Hellwig 	md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id);
19651da177e4SLinus Torvalds 	if (!md->queue)
19660f20972fSMike Snitzer 		goto bad;
1967c12c9a3cSMike Snitzer 	md->queue->queuedata = md;
1968c12c9a3cSMike Snitzer 	md->queue->backing_dev_info->congested_data = md;
19691da177e4SLinus Torvalds 
1970c12c9a3cSMike Snitzer 	md->disk = alloc_disk_node(1, md->numa_node_id);
19711da177e4SLinus Torvalds 	if (!md->disk)
19720f20972fSMike Snitzer 		goto bad;
19731da177e4SLinus Torvalds 
1974f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
197553d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1976f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
19772995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
1978f0b04115SJeff Mahoney 
19791da177e4SLinus Torvalds 	md->disk->major = _major;
19801da177e4SLinus Torvalds 	md->disk->first_minor = minor;
19811da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
19821da177e4SLinus Torvalds 	md->disk->queue = md->queue;
19831da177e4SLinus Torvalds 	md->disk->private_data = md;
19841da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
1985f26c5719SDan Williams 
1986976431b0SDan Williams 	if (IS_ENABLED(CONFIG_DAX_DRIVER)) {
1987f26c5719SDan Williams 		dax_dev = alloc_dax(md, md->disk->disk_name, &dm_dax_ops);
1988f26c5719SDan Williams 		if (!dax_dev)
1989f26c5719SDan Williams 			goto bad;
1990976431b0SDan Williams 	}
1991f26c5719SDan Williams 	md->dax_dev = dax_dev;
1992f26c5719SDan Williams 
1993c100ec49SMike Snitzer 	add_disk_no_queue_reg(md->disk);
19947e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
19951da177e4SLinus Torvalds 
1996670368a8STejun Heo 	md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
1997304f3f6aSMilan Broz 	if (!md->wq)
19980f20972fSMike Snitzer 		goto bad;
1999304f3f6aSMilan Broz 
200032a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
200132a926daSMikulas Patocka 	if (!md->bdev)
20020f20972fSMike Snitzer 		goto bad;
200332a926daSMikulas Patocka 
2004fd2ed4d2SMikulas Patocka 	dm_stats_init(&md->stats);
2005fd2ed4d2SMikulas Patocka 
2006ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
2007f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
2008ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
2009f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2010ba61fdd1SJeff Mahoney 
2011ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
2012ba61fdd1SJeff Mahoney 
20131da177e4SLinus Torvalds 	return md;
20141da177e4SLinus Torvalds 
20150f20972fSMike Snitzer bad:
20160f20972fSMike Snitzer 	cleanup_mapped_device(md);
201783d5e5b0SMikulas Patocka bad_io_barrier:
20181da177e4SLinus Torvalds 	free_minor(minor);
20196ed7ade8SMilan Broz bad_minor:
202010da4f79SJeff Mahoney 	module_put(THIS_MODULE);
20216ed7ade8SMilan Broz bad_module_get:
2022856eb091SMikulas Patocka 	kvfree(md);
20231da177e4SLinus Torvalds 	return NULL;
20241da177e4SLinus Torvalds }
20251da177e4SLinus Torvalds 
2026ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
2027ae9da83fSJun'ichi Nomura 
20281da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
20291da177e4SLinus Torvalds {
2030f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
203163d94e48SJun'ichi Nomura 
2032ae9da83fSJun'ichi Nomura 	unlock_fs(md);
20332eb6e1e3SKeith Busch 
20340f20972fSMike Snitzer 	cleanup_mapped_device(md);
20350f20972fSMike Snitzer 
20360f20972fSMike Snitzer 	free_table_devices(&md->table_devices);
20370f20972fSMike Snitzer 	dm_stats_cleanup(&md->stats);
203863a4f065SMike Snitzer 	free_minor(minor);
203963a4f065SMike Snitzer 
204010da4f79SJeff Mahoney 	module_put(THIS_MODULE);
2041856eb091SMikulas Patocka 	kvfree(md);
20421da177e4SLinus Torvalds }
20431da177e4SLinus Torvalds 
20442a2a4c51SJens Axboe static int __bind_mempools(struct mapped_device *md, struct dm_table *t)
2045e6ee8c0bSKiyoshi Ueda {
2046c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
20472a2a4c51SJens Axboe 	int ret = 0;
2048e6ee8c0bSKiyoshi Ueda 
2049545ed20eSToshi Kani 	if (dm_table_bio_based(t)) {
2050c0820cf5SMikulas Patocka 		/*
205164f52b0eSMike Snitzer 		 * The md may already have mempools that need changing.
205264f52b0eSMike Snitzer 		 * If so, reload bioset because front_pad may have changed
205316245bdcSJun'ichi Nomura 		 * because a different table was loaded.
2054c0820cf5SMikulas Patocka 		 */
20556f1c819cSKent Overstreet 		bioset_exit(&md->bs);
20566f1c819cSKent Overstreet 		bioset_exit(&md->io_bs);
20570776aa0eSMike Snitzer 
20586f1c819cSKent Overstreet 	} else if (bioset_initialized(&md->bs)) {
2059cbc4e3c1SMike Snitzer 		/*
20604e6e36c3SMike Snitzer 		 * There's no need to reload with request-based dm
20614e6e36c3SMike Snitzer 		 * because the size of front_pad doesn't change.
20624e6e36c3SMike Snitzer 		 * Note for future: If you are to reload bioset,
20634e6e36c3SMike Snitzer 		 * prep-ed requests in the queue may refer
20644e6e36c3SMike Snitzer 		 * to bio from the old bioset, so you must walk
20654e6e36c3SMike Snitzer 		 * through the queue to unprep.
2066cbc4e3c1SMike Snitzer 		 */
2067cbc4e3c1SMike Snitzer 		goto out;
2068cbc4e3c1SMike Snitzer 	}
2069cbc4e3c1SMike Snitzer 
20706f1c819cSKent Overstreet 	BUG_ON(!p ||
20716f1c819cSKent Overstreet 	       bioset_initialized(&md->bs) ||
20726f1c819cSKent Overstreet 	       bioset_initialized(&md->io_bs));
2073e6ee8c0bSKiyoshi Ueda 
20742a2a4c51SJens Axboe 	ret = bioset_init_from_src(&md->bs, &p->bs);
20752a2a4c51SJens Axboe 	if (ret)
20762a2a4c51SJens Axboe 		goto out;
20772a2a4c51SJens Axboe 	ret = bioset_init_from_src(&md->io_bs, &p->io_bs);
20782a2a4c51SJens Axboe 	if (ret)
20792a2a4c51SJens Axboe 		bioset_exit(&md->bs);
2080e6ee8c0bSKiyoshi Ueda out:
208102233342SMike Snitzer 	/* mempool bind completed, no longer need any mempools in the table */
2082e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
20832a2a4c51SJens Axboe 	return ret;
2084e6ee8c0bSKiyoshi Ueda }
2085e6ee8c0bSKiyoshi Ueda 
20861da177e4SLinus Torvalds /*
20871da177e4SLinus Torvalds  * Bind a table to the device.
20881da177e4SLinus Torvalds  */
20891da177e4SLinus Torvalds static void event_callback(void *context)
20901da177e4SLinus Torvalds {
20917a8c3d3bSMike Anderson 	unsigned long flags;
20927a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
20931da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
20941da177e4SLinus Torvalds 
20957a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
20967a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
20977a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
20987a8c3d3bSMike Anderson 
2099ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
21007a8c3d3bSMike Anderson 
21011da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
21021da177e4SLinus Torvalds 	wake_up(&md->eventq);
210362e08243SMikulas Patocka 	dm_issue_global_event();
21041da177e4SLinus Torvalds }
21051da177e4SLinus Torvalds 
2106c217649bSMike Snitzer /*
2107c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
2108c217649bSMike Snitzer  */
21094e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
21101da177e4SLinus Torvalds {
21111ea0654eSBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
21121ea0654eSBart Van Assche 
21134e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
21141da177e4SLinus Torvalds 
2115db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
21161da177e4SLinus Torvalds }
21171da177e4SLinus Torvalds 
2118042d2a9bSAlasdair G Kergon /*
2119042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2120042d2a9bSAlasdair G Kergon  */
2121042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2122754c5fc7SMike Snitzer 			       struct queue_limits *limits)
21231da177e4SLinus Torvalds {
2124042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
2125165125e1SJens Axboe 	struct request_queue *q = md->queue;
2126978e51baSMike Snitzer 	bool request_based = dm_table_request_based(t);
21271da177e4SLinus Torvalds 	sector_t size;
21282a2a4c51SJens Axboe 	int ret;
21291da177e4SLinus Torvalds 
21305a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
21315a8f1f80SBart Van Assche 
21321da177e4SLinus Torvalds 	size = dm_table_get_size(t);
21333ac51e74SDarrick J. Wong 
21343ac51e74SDarrick J. Wong 	/*
21353ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
21363ac51e74SDarrick J. Wong 	 */
2137fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
21383ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
21393ac51e74SDarrick J. Wong 
21404e90188bSAlasdair G Kergon 	__set_size(md, size);
21411da177e4SLinus Torvalds 
2142cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
21432ca3310eSAlasdair G Kergon 
2144e6ee8c0bSKiyoshi Ueda 	/*
2145e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
2146e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
2147e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
2148e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
2149e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
2150e6ee8c0bSKiyoshi Ueda 	 */
2151978e51baSMike Snitzer 	if (request_based)
2152eca7ee6dSMike Snitzer 		dm_stop_queue(q);
2153978e51baSMike Snitzer 
2154978e51baSMike Snitzer 	if (request_based || md->type == DM_TYPE_NVME_BIO_BASED) {
215516f12266SMike Snitzer 		/*
2156978e51baSMike Snitzer 		 * Leverage the fact that request-based DM targets and
2157978e51baSMike Snitzer 		 * NVMe bio based targets are immutable singletons
2158978e51baSMike Snitzer 		 * - used to optimize both dm_request_fn and dm_mq_queue_rq;
2159978e51baSMike Snitzer 		 *   and __process_bio.
216016f12266SMike Snitzer 		 */
216116f12266SMike Snitzer 		md->immutable_target = dm_table_get_immutable_target(t);
216216f12266SMike Snitzer 	}
2163e6ee8c0bSKiyoshi Ueda 
21642a2a4c51SJens Axboe 	ret = __bind_mempools(md, t);
21652a2a4c51SJens Axboe 	if (ret) {
21662a2a4c51SJens Axboe 		old_map = ERR_PTR(ret);
21672a2a4c51SJens Axboe 		goto out;
21682a2a4c51SJens Axboe 	}
2169e6ee8c0bSKiyoshi Ueda 
2170a12f5d48SEric Dumazet 	old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
21711d3aa6f6SMike Snitzer 	rcu_assign_pointer(md->map, (void *)t);
217236a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
217336a0456fSAlasdair G Kergon 
2174754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
217541abc4e1SHannes Reinecke 	if (old_map)
217683d5e5b0SMikulas Patocka 		dm_sync_table(md);
21772ca3310eSAlasdair G Kergon 
21782a2a4c51SJens Axboe out:
2179042d2a9bSAlasdair G Kergon 	return old_map;
21801da177e4SLinus Torvalds }
21811da177e4SLinus Torvalds 
2182a7940155SAlasdair G Kergon /*
2183a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2184a7940155SAlasdair G Kergon  */
2185a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
21861da177e4SLinus Torvalds {
2187a12f5d48SEric Dumazet 	struct dm_table *map = rcu_dereference_protected(md->map, 1);
21881da177e4SLinus Torvalds 
21891da177e4SLinus Torvalds 	if (!map)
2190a7940155SAlasdair G Kergon 		return NULL;
21911da177e4SLinus Torvalds 
21921da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
21939cdb8520SMonam Agarwal 	RCU_INIT_POINTER(md->map, NULL);
219483d5e5b0SMikulas Patocka 	dm_sync_table(md);
2195a7940155SAlasdair G Kergon 
2196a7940155SAlasdair G Kergon 	return map;
21971da177e4SLinus Torvalds }
21981da177e4SLinus Torvalds 
21991da177e4SLinus Torvalds /*
22001da177e4SLinus Torvalds  * Constructor for a new device.
22011da177e4SLinus Torvalds  */
22022b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
22031da177e4SLinus Torvalds {
2204c12c9a3cSMike Snitzer 	int r;
22051da177e4SLinus Torvalds 	struct mapped_device *md;
22061da177e4SLinus Torvalds 
22072b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
22081da177e4SLinus Torvalds 	if (!md)
22091da177e4SLinus Torvalds 		return -ENXIO;
22101da177e4SLinus Torvalds 
2211c12c9a3cSMike Snitzer 	r = dm_sysfs_init(md);
2212c12c9a3cSMike Snitzer 	if (r) {
2213c12c9a3cSMike Snitzer 		free_dev(md);
2214c12c9a3cSMike Snitzer 		return r;
2215c12c9a3cSMike Snitzer 	}
2216784aae73SMilan Broz 
22171da177e4SLinus Torvalds 	*result = md;
22181da177e4SLinus Torvalds 	return 0;
22191da177e4SLinus Torvalds }
22201da177e4SLinus Torvalds 
2221a5664dadSMike Snitzer /*
2222a5664dadSMike Snitzer  * Functions to manage md->type.
2223a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2224a5664dadSMike Snitzer  */
2225a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2226a5664dadSMike Snitzer {
2227a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2228a5664dadSMike Snitzer }
2229a5664dadSMike Snitzer 
2230a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2231a5664dadSMike Snitzer {
2232a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2233a5664dadSMike Snitzer }
2234a5664dadSMike Snitzer 
22357e0d574fSBart Van Assche void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
2236a5664dadSMike Snitzer {
223700c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2238a5664dadSMike Snitzer 	md->type = type;
2239a5664dadSMike Snitzer }
2240a5664dadSMike Snitzer 
22417e0d574fSBart Van Assche enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
2242a5664dadSMike Snitzer {
2243a5664dadSMike Snitzer 	return md->type;
2244a5664dadSMike Snitzer }
2245a5664dadSMike Snitzer 
224636a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
224736a0456fSAlasdair G Kergon {
224836a0456fSAlasdair G Kergon 	return md->immutable_target_type;
224936a0456fSAlasdair G Kergon }
225036a0456fSAlasdair G Kergon 
22514a0b4ddfSMike Snitzer /*
2252f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
2253f84cb8a4SMike Snitzer  * count on 'md'.
2254f84cb8a4SMike Snitzer  */
2255f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2256f84cb8a4SMike Snitzer {
2257f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
2258f84cb8a4SMike Snitzer 	return &md->queue->limits;
2259f84cb8a4SMike Snitzer }
2260f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2261f84cb8a4SMike Snitzer 
22624a0b4ddfSMike Snitzer /*
22634a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
22644a0b4ddfSMike Snitzer  */
2265591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
22664a0b4ddfSMike Snitzer {
2267bfebd1cdSMike Snitzer 	int r;
2268c100ec49SMike Snitzer 	struct queue_limits limits;
22697e0d574fSBart Van Assche 	enum dm_queue_mode type = dm_get_md_type(md);
2270bfebd1cdSMike Snitzer 
2271545ed20eSToshi Kani 	switch (type) {
2272bfebd1cdSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
2273e83068a5SMike Snitzer 		r = dm_mq_init_request_queue(md, t);
2274bfebd1cdSMike Snitzer 		if (r) {
2275eca7ee6dSMike Snitzer 			DMERR("Cannot initialize queue for request-based dm-mq mapped device");
2276bfebd1cdSMike Snitzer 			return r;
2277bfebd1cdSMike Snitzer 		}
2278bfebd1cdSMike Snitzer 		break;
2279bfebd1cdSMike Snitzer 	case DM_TYPE_BIO_BASED:
2280545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
2281978e51baSMike Snitzer 	case DM_TYPE_NVME_BIO_BASED:
2282978e51baSMike Snitzer 		dm_init_normal_md_queue(md);
228324113d48SMikulas Patocka 		blk_queue_make_request(md->queue, dm_make_request);
2284bfebd1cdSMike Snitzer 		break;
22857e0d574fSBart Van Assche 	case DM_TYPE_NONE:
22867e0d574fSBart Van Assche 		WARN_ON_ONCE(true);
22877e0d574fSBart Van Assche 		break;
2288ff36ab34SMike Snitzer 	}
22894a0b4ddfSMike Snitzer 
2290c100ec49SMike Snitzer 	r = dm_calculate_queue_limits(t, &limits);
2291c100ec49SMike Snitzer 	if (r) {
2292c100ec49SMike Snitzer 		DMERR("Cannot calculate initial queue limits");
2293c100ec49SMike Snitzer 		return r;
2294c100ec49SMike Snitzer 	}
2295c100ec49SMike Snitzer 	dm_table_set_restrictions(t, md->queue, &limits);
2296c100ec49SMike Snitzer 	blk_register_queue(md->disk);
2297c100ec49SMike Snitzer 
22984a0b4ddfSMike Snitzer 	return 0;
22994a0b4ddfSMike Snitzer }
23004a0b4ddfSMike Snitzer 
23012bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev)
23021da177e4SLinus Torvalds {
23031da177e4SLinus Torvalds 	struct mapped_device *md;
23041da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
23051da177e4SLinus Torvalds 
23061da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
23071da177e4SLinus Torvalds 		return NULL;
23081da177e4SLinus Torvalds 
2309f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
23101da177e4SLinus Torvalds 
23111da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
231249de5769SMike Snitzer 	if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) ||
231349de5769SMike Snitzer 	    test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2314637842cfSDavid Teigland 		md = NULL;
2315fba9f90eSJeff Mahoney 		goto out;
2316fba9f90eSJeff Mahoney 	}
23172bec1f4aSMikulas Patocka 	dm_get(md);
2318fba9f90eSJeff Mahoney out:
2319f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
23201da177e4SLinus Torvalds 
2321637842cfSDavid Teigland 	return md;
2322637842cfSDavid Teigland }
23233cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2324d229a958SDavid Teigland 
23259ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2326637842cfSDavid Teigland {
23279ade92a9SAlasdair G Kergon 	return md->interface_ptr;
23281da177e4SLinus Torvalds }
23291da177e4SLinus Torvalds 
23301da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
23311da177e4SLinus Torvalds {
23321da177e4SLinus Torvalds 	md->interface_ptr = ptr;
23331da177e4SLinus Torvalds }
23341da177e4SLinus Torvalds 
23351da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
23361da177e4SLinus Torvalds {
23371da177e4SLinus Torvalds 	atomic_inc(&md->holders);
23383f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
23391da177e4SLinus Torvalds }
23401da177e4SLinus Torvalds 
234109ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md)
234209ee96b2SMikulas Patocka {
234309ee96b2SMikulas Patocka 	spin_lock(&_minor_lock);
234409ee96b2SMikulas Patocka 	if (test_bit(DMF_FREEING, &md->flags)) {
234509ee96b2SMikulas Patocka 		spin_unlock(&_minor_lock);
234609ee96b2SMikulas Patocka 		return -EBUSY;
234709ee96b2SMikulas Patocka 	}
234809ee96b2SMikulas Patocka 	dm_get(md);
234909ee96b2SMikulas Patocka 	spin_unlock(&_minor_lock);
235009ee96b2SMikulas Patocka 	return 0;
235109ee96b2SMikulas Patocka }
235209ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold);
235309ee96b2SMikulas Patocka 
235472d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
235572d94861SAlasdair G Kergon {
235672d94861SAlasdair G Kergon 	return md->name;
235772d94861SAlasdair G Kergon }
235872d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
235972d94861SAlasdair G Kergon 
23603f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
23611da177e4SLinus Torvalds {
23621134e5aeSMike Anderson 	struct dm_table *map;
236383d5e5b0SMikulas Patocka 	int srcu_idx;
23641da177e4SLinus Torvalds 
23653f77316dSKiyoshi Ueda 	might_sleep();
2366fba9f90eSJeff Mahoney 
236763a4f065SMike Snitzer 	spin_lock(&_minor_lock);
23683f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2369fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2370f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
23713f77316dSKiyoshi Ueda 
2372c12c9a3cSMike Snitzer 	blk_set_queue_dying(md->queue);
23733b785fbcSBart Van Assche 
2374ab7c7bb6SMikulas Patocka 	/*
2375ab7c7bb6SMikulas Patocka 	 * Take suspend_lock so that presuspend and postsuspend methods
2376ab7c7bb6SMikulas Patocka 	 * do not race with internal suspend.
2377ab7c7bb6SMikulas Patocka 	 */
2378ab7c7bb6SMikulas Patocka 	mutex_lock(&md->suspend_lock);
23792a708cffSJunichi Nomura 	map = dm_get_live_table(md, &srcu_idx);
23804f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
23811da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
23821da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
23831da177e4SLinus Torvalds 	}
238483d5e5b0SMikulas Patocka 	/* dm_put_live_table must be before msleep, otherwise deadlock is possible */
238583d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
23862a708cffSJunichi Nomura 	mutex_unlock(&md->suspend_lock);
238783d5e5b0SMikulas Patocka 
23883f77316dSKiyoshi Ueda 	/*
23893f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
23903f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
23913f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
23923f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
23933f77316dSKiyoshi Ueda 	 */
23943f77316dSKiyoshi Ueda 	if (wait)
23953f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
23963f77316dSKiyoshi Ueda 			msleep(1);
23973f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
23983f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
23993f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
24003f77316dSKiyoshi Ueda 
2401784aae73SMilan Broz 	dm_sysfs_exit(md);
2402a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
24031da177e4SLinus Torvalds 	free_dev(md);
24041da177e4SLinus Torvalds }
24053f77316dSKiyoshi Ueda 
24063f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
24073f77316dSKiyoshi Ueda {
24083f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
24093f77316dSKiyoshi Ueda }
24103f77316dSKiyoshi Ueda 
24113f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
24123f77316dSKiyoshi Ueda {
24133f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
24143f77316dSKiyoshi Ueda }
24153f77316dSKiyoshi Ueda 
24163f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
24173f77316dSKiyoshi Ueda {
24183f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
24191da177e4SLinus Torvalds }
242079eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
24211da177e4SLinus Torvalds 
2422b48633f8SBart Van Assche static int dm_wait_for_completion(struct mapped_device *md, long task_state)
242346125c1cSMilan Broz {
242446125c1cSMilan Broz 	int r = 0;
24259f4c3f87SBart Van Assche 	DEFINE_WAIT(wait);
242646125c1cSMilan Broz 
242746125c1cSMilan Broz 	while (1) {
24289f4c3f87SBart Van Assche 		prepare_to_wait(&md->wait, &wait, task_state);
242946125c1cSMilan Broz 
2430b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
243146125c1cSMilan Broz 			break;
243246125c1cSMilan Broz 
2433e3fabdfdSBart Van Assche 		if (signal_pending_state(task_state, current)) {
243446125c1cSMilan Broz 			r = -EINTR;
243546125c1cSMilan Broz 			break;
243646125c1cSMilan Broz 		}
243746125c1cSMilan Broz 
243846125c1cSMilan Broz 		io_schedule();
243946125c1cSMilan Broz 	}
24409f4c3f87SBart Van Assche 	finish_wait(&md->wait, &wait);
2441b44ebeb0SMikulas Patocka 
244246125c1cSMilan Broz 	return r;
244346125c1cSMilan Broz }
244446125c1cSMilan Broz 
24451da177e4SLinus Torvalds /*
24461da177e4SLinus Torvalds  * Process the deferred bios
24471da177e4SLinus Torvalds  */
2448ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
24491da177e4SLinus Torvalds {
2450ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2451ef208587SMikulas Patocka 						work);
24526d6f10dfSMilan Broz 	struct bio *c;
245383d5e5b0SMikulas Patocka 	int srcu_idx;
245483d5e5b0SMikulas Patocka 	struct dm_table *map;
24551da177e4SLinus Torvalds 
245683d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
2457ef208587SMikulas Patocka 
24583b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2459022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2460022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2461022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2462022c2611SMikulas Patocka 
24636a8736d1STejun Heo 		if (!c)
2464df12ee99SAlasdair G Kergon 			break;
246573d410c0SMilan Broz 
2466e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
24676548c7c5SMike Snitzer 			(void) generic_make_request(c);
2468af7e466aSMikulas Patocka 		else
24696548c7c5SMike Snitzer 			(void) dm_process_bio(md, map, c);
2470e6ee8c0bSKiyoshi Ueda 	}
24713b00b203SMikulas Patocka 
247283d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
24731da177e4SLinus Torvalds }
24741da177e4SLinus Torvalds 
24759a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2476304f3f6aSMilan Broz {
24773b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
24784e857c58SPeter Zijlstra 	smp_mb__after_atomic();
247953d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2480304f3f6aSMilan Broz }
2481304f3f6aSMilan Broz 
24821da177e4SLinus Torvalds /*
2483042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
24841da177e4SLinus Torvalds  */
2485042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
24861da177e4SLinus Torvalds {
248787eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2488754c5fc7SMike Snitzer 	struct queue_limits limits;
2489042d2a9bSAlasdair G Kergon 	int r;
24901da177e4SLinus Torvalds 
2491e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
24921da177e4SLinus Torvalds 
24931da177e4SLinus Torvalds 	/* device must be suspended */
24944f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
249593c534aeSAlasdair G Kergon 		goto out;
24961da177e4SLinus Torvalds 
24973ae70656SMike Snitzer 	/*
24983ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
24993ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
25003ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
25013ae70656SMike Snitzer 	 * reappear.
25023ae70656SMike Snitzer 	 */
25033ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
250483d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
25053ae70656SMike Snitzer 		if (live_map)
25063ae70656SMike Snitzer 			limits = md->queue->limits;
250783d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
25083ae70656SMike Snitzer 	}
25093ae70656SMike Snitzer 
251087eb5b21SMike Christie 	if (!live_map) {
2511754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2512042d2a9bSAlasdair G Kergon 		if (r) {
2513042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2514754c5fc7SMike Snitzer 			goto out;
2515042d2a9bSAlasdair G Kergon 		}
251687eb5b21SMike Christie 	}
2517754c5fc7SMike Snitzer 
2518042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
251962e08243SMikulas Patocka 	dm_issue_global_event();
25201da177e4SLinus Torvalds 
252193c534aeSAlasdair G Kergon out:
2522e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2523042d2a9bSAlasdair G Kergon 	return map;
25241da177e4SLinus Torvalds }
25251da177e4SLinus Torvalds 
25261da177e4SLinus Torvalds /*
25271da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
25281da177e4SLinus Torvalds  * device.
25291da177e4SLinus Torvalds  */
25302ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
25311da177e4SLinus Torvalds {
2532e39e2e95SAlasdair G Kergon 	int r;
25331da177e4SLinus Torvalds 
25341da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2535dfbe03f6SAlasdair G Kergon 
2536db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2537dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2538cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2539e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2540e39e2e95SAlasdair G Kergon 		return r;
2541dfbe03f6SAlasdair G Kergon 	}
2542dfbe03f6SAlasdair G Kergon 
2543aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2544aa8d7c2fSAlasdair G Kergon 
25451da177e4SLinus Torvalds 	return 0;
25461da177e4SLinus Torvalds }
25471da177e4SLinus Torvalds 
25482ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
25491da177e4SLinus Torvalds {
2550aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2551aa8d7c2fSAlasdair G Kergon 		return;
2552aa8d7c2fSAlasdair G Kergon 
2553db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
25541da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2555aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
25561da177e4SLinus Torvalds }
25571da177e4SLinus Torvalds 
25581da177e4SLinus Torvalds /*
2559b48633f8SBart Van Assche  * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
2560b48633f8SBart Van Assche  * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
2561b48633f8SBart Van Assche  * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
2562b48633f8SBart Van Assche  *
2563ffcc3936SMike Snitzer  * If __dm_suspend returns 0, the device is completely quiescent
2564ffcc3936SMike Snitzer  * now. There is no request-processing activity. All new requests
2565ffcc3936SMike Snitzer  * are being added to md->deferred list.
2566cec47e3dSKiyoshi Ueda  */
2567ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
2568b48633f8SBart Van Assche 			unsigned suspend_flags, long task_state,
2569eaf9a736SMike Snitzer 			int dmf_suspended_flag)
25701da177e4SLinus Torvalds {
2571ffcc3936SMike Snitzer 	bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2572ffcc3936SMike Snitzer 	bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2573ffcc3936SMike Snitzer 	int r;
2574cf222b37SAlasdair G Kergon 
25755a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
25765a8f1f80SBart Van Assche 
25772e93ccc1SKiyoshi Ueda 	/*
25782e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
25792e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
25802e93ccc1SKiyoshi Ueda 	 */
25812e93ccc1SKiyoshi Ueda 	if (noflush)
25822e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
258386331f39SBart Van Assche 	else
258486331f39SBart Van Assche 		pr_debug("%s: suspending with flush\n", dm_device_name(md));
25852e93ccc1SKiyoshi Ueda 
2586d67ee213SMike Snitzer 	/*
2587d67ee213SMike Snitzer 	 * This gets reverted if there's an error later and the targets
2588d67ee213SMike Snitzer 	 * provide the .presuspend_undo hook.
2589d67ee213SMike Snitzer 	 */
25901da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
25911da177e4SLinus Torvalds 
25922e93ccc1SKiyoshi Ueda 	/*
25939f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
25949f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
25959f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
25969f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
25972e93ccc1SKiyoshi Ueda 	 */
259832a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
25992ca3310eSAlasdair G Kergon 		r = lock_fs(md);
2600d67ee213SMike Snitzer 		if (r) {
2601d67ee213SMike Snitzer 			dm_table_presuspend_undo_targets(map);
2602ffcc3936SMike Snitzer 			return r;
2603aa8d7c2fSAlasdair G Kergon 		}
2604d67ee213SMike Snitzer 	}
26051da177e4SLinus Torvalds 
26061da177e4SLinus Torvalds 	/*
26073b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
26083b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
26093b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
26103b00b203SMikulas Patocka 	 * dm_wq_work.
26113b00b203SMikulas Patocka 	 *
26123b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
26133b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
26146a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
26156a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
26166a8736d1STejun Heo 	 * flush_workqueue(md->wq).
26171da177e4SLinus Torvalds 	 */
26181eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
261941abc4e1SHannes Reinecke 	if (map)
262083d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
26211da177e4SLinus Torvalds 
2622d0bcb878SKiyoshi Ueda 	/*
262329e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
262429e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2625d0bcb878SKiyoshi Ueda 	 */
26266a23e05cSJens Axboe 	if (dm_request_based(md))
2627eca7ee6dSMike Snitzer 		dm_stop_queue(md->queue);
2628cec47e3dSKiyoshi Ueda 
2629d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2630d0bcb878SKiyoshi Ueda 
26311da177e4SLinus Torvalds 	/*
26323b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
26333b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
26343b00b203SMikulas Patocka 	 * to finish.
26351da177e4SLinus Torvalds 	 */
2636b48633f8SBart Van Assche 	r = dm_wait_for_completion(md, task_state);
2637eaf9a736SMike Snitzer 	if (!r)
2638eaf9a736SMike Snitzer 		set_bit(dmf_suspended_flag, &md->flags);
26391da177e4SLinus Torvalds 
26406d6f10dfSMilan Broz 	if (noflush)
2641022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
264241abc4e1SHannes Reinecke 	if (map)
264383d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
26442e93ccc1SKiyoshi Ueda 
26451da177e4SLinus Torvalds 	/* were we interrupted ? */
264646125c1cSMilan Broz 	if (r < 0) {
26479a1fb464SMikulas Patocka 		dm_queue_flush(md);
264873d410c0SMilan Broz 
2649cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
2650eca7ee6dSMike Snitzer 			dm_start_queue(md->queue);
2651cec47e3dSKiyoshi Ueda 
26522ca3310eSAlasdair G Kergon 		unlock_fs(md);
2653d67ee213SMike Snitzer 		dm_table_presuspend_undo_targets(map);
2654ffcc3936SMike Snitzer 		/* pushback list is already flushed, so skip flush */
2655ffcc3936SMike Snitzer 	}
2656ffcc3936SMike Snitzer 
2657ffcc3936SMike Snitzer 	return r;
26582ca3310eSAlasdair G Kergon }
26592ca3310eSAlasdair G Kergon 
26603b00b203SMikulas Patocka /*
2661ffcc3936SMike Snitzer  * We need to be able to change a mapping table under a mounted
2662ffcc3936SMike Snitzer  * filesystem.  For example we might want to move some data in
2663ffcc3936SMike Snitzer  * the background.  Before the table can be swapped with
2664ffcc3936SMike Snitzer  * dm_bind_table, dm_suspend must be called to flush any in
2665ffcc3936SMike Snitzer  * flight bios and ensure that any further io gets deferred.
26663b00b203SMikulas Patocka  */
2667ffcc3936SMike Snitzer /*
2668ffcc3936SMike Snitzer  * Suspend mechanism in request-based dm.
2669ffcc3936SMike Snitzer  *
2670ffcc3936SMike Snitzer  * 1. Flush all I/Os by lock_fs() if needed.
2671ffcc3936SMike Snitzer  * 2. Stop dispatching any I/O by stopping the request_queue.
2672ffcc3936SMike Snitzer  * 3. Wait for all in-flight I/Os to be completed or requeued.
2673ffcc3936SMike Snitzer  *
2674ffcc3936SMike Snitzer  * To abort suspend, start the request_queue.
2675ffcc3936SMike Snitzer  */
2676ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2677ffcc3936SMike Snitzer {
2678ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2679ffcc3936SMike Snitzer 	int r = 0;
2680ffcc3936SMike Snitzer 
2681ffcc3936SMike Snitzer retry:
2682ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2683ffcc3936SMike Snitzer 
2684ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2685ffcc3936SMike Snitzer 		r = -EINVAL;
2686ffcc3936SMike Snitzer 		goto out_unlock;
2687ffcc3936SMike Snitzer 	}
2688ffcc3936SMike Snitzer 
2689ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2690ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2691ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2692ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2693ffcc3936SMike Snitzer 		if (r)
2694ffcc3936SMike Snitzer 			return r;
2695ffcc3936SMike Snitzer 		goto retry;
2696ffcc3936SMike Snitzer 	}
2697ffcc3936SMike Snitzer 
2698a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2699ffcc3936SMike Snitzer 
2700eaf9a736SMike Snitzer 	r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
2701ffcc3936SMike Snitzer 	if (r)
2702ffcc3936SMike Snitzer 		goto out_unlock;
27033b00b203SMikulas Patocka 
27044d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
27054d4471cbSKiyoshi Ueda 
2706d287483dSAlasdair G Kergon out_unlock:
2707e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2708cf222b37SAlasdair G Kergon 	return r;
27091da177e4SLinus Torvalds }
27101da177e4SLinus Torvalds 
2711ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map)
27121da177e4SLinus Torvalds {
2713ffcc3936SMike Snitzer 	if (map) {
2714ffcc3936SMike Snitzer 		int r = dm_table_resume_targets(map);
27158757b776SMilan Broz 		if (r)
2716ffcc3936SMike Snitzer 			return r;
2717ffcc3936SMike Snitzer 	}
27182ca3310eSAlasdair G Kergon 
27199a1fb464SMikulas Patocka 	dm_queue_flush(md);
27202ca3310eSAlasdair G Kergon 
2721cec47e3dSKiyoshi Ueda 	/*
2722cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2723cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2724cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2725cec47e3dSKiyoshi Ueda 	 */
2726cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2727eca7ee6dSMike Snitzer 		dm_start_queue(md->queue);
2728cec47e3dSKiyoshi Ueda 
27292ca3310eSAlasdair G Kergon 	unlock_fs(md);
27302ca3310eSAlasdair G Kergon 
2731ffcc3936SMike Snitzer 	return 0;
2732ffcc3936SMike Snitzer }
2733ffcc3936SMike Snitzer 
2734ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md)
2735ffcc3936SMike Snitzer {
27368dc23658SMinfei Huang 	int r;
2737ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2738ffcc3936SMike Snitzer 
2739ffcc3936SMike Snitzer retry:
27408dc23658SMinfei Huang 	r = -EINVAL;
2741ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2742ffcc3936SMike Snitzer 
2743ffcc3936SMike Snitzer 	if (!dm_suspended_md(md))
2744ffcc3936SMike Snitzer 		goto out;
2745ffcc3936SMike Snitzer 
2746ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2747ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2748ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2749ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2750ffcc3936SMike Snitzer 		if (r)
2751ffcc3936SMike Snitzer 			return r;
2752ffcc3936SMike Snitzer 		goto retry;
2753ffcc3936SMike Snitzer 	}
2754ffcc3936SMike Snitzer 
2755a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2756ffcc3936SMike Snitzer 	if (!map || !dm_table_get_size(map))
2757ffcc3936SMike Snitzer 		goto out;
2758ffcc3936SMike Snitzer 
2759ffcc3936SMike Snitzer 	r = __dm_resume(md, map);
2760ffcc3936SMike Snitzer 	if (r)
2761ffcc3936SMike Snitzer 		goto out;
2762ffcc3936SMike Snitzer 
27632ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
2764cf222b37SAlasdair G Kergon out:
2765e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
27662ca3310eSAlasdair G Kergon 
2767cf222b37SAlasdair G Kergon 	return r;
27681da177e4SLinus Torvalds }
27691da177e4SLinus Torvalds 
2770fd2ed4d2SMikulas Patocka /*
2771fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
2772fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
2773fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
2774fd2ed4d2SMikulas Patocka  */
2775fd2ed4d2SMikulas Patocka 
2776ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
2777ffcc3936SMike Snitzer {
2778ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2779ffcc3936SMike Snitzer 
27801ea0654eSBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
27811ea0654eSBart Van Assche 
278296b26c8cSMikulas Patocka 	if (md->internal_suspend_count++)
2783ffcc3936SMike Snitzer 		return; /* nested internal suspend */
2784ffcc3936SMike Snitzer 
2785ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2786ffcc3936SMike Snitzer 		set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2787ffcc3936SMike Snitzer 		return; /* nest suspend */
2788ffcc3936SMike Snitzer 	}
2789ffcc3936SMike Snitzer 
2790a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2791ffcc3936SMike Snitzer 
2792ffcc3936SMike Snitzer 	/*
2793ffcc3936SMike Snitzer 	 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2794ffcc3936SMike Snitzer 	 * supported.  Properly supporting a TASK_INTERRUPTIBLE internal suspend
2795ffcc3936SMike Snitzer 	 * would require changing .presuspend to return an error -- avoid this
2796ffcc3936SMike Snitzer 	 * until there is a need for more elaborate variants of internal suspend.
2797ffcc3936SMike Snitzer 	 */
2798eaf9a736SMike Snitzer 	(void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
2799eaf9a736SMike Snitzer 			    DMF_SUSPENDED_INTERNALLY);
2800ffcc3936SMike Snitzer 
2801ffcc3936SMike Snitzer 	dm_table_postsuspend_targets(map);
2802ffcc3936SMike Snitzer }
2803ffcc3936SMike Snitzer 
2804ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md)
2805ffcc3936SMike Snitzer {
280696b26c8cSMikulas Patocka 	BUG_ON(!md->internal_suspend_count);
280796b26c8cSMikulas Patocka 
280896b26c8cSMikulas Patocka 	if (--md->internal_suspend_count)
2809ffcc3936SMike Snitzer 		return; /* resume from nested internal suspend */
2810ffcc3936SMike Snitzer 
2811ffcc3936SMike Snitzer 	if (dm_suspended_md(md))
2812ffcc3936SMike Snitzer 		goto done; /* resume from nested suspend */
2813ffcc3936SMike Snitzer 
2814ffcc3936SMike Snitzer 	/*
2815ffcc3936SMike Snitzer 	 * NOTE: existing callers don't need to call dm_table_resume_targets
2816ffcc3936SMike Snitzer 	 * (which may fail -- so best to avoid it for now by passing NULL map)
2817ffcc3936SMike Snitzer 	 */
2818ffcc3936SMike Snitzer 	(void) __dm_resume(md, NULL);
2819ffcc3936SMike Snitzer 
2820ffcc3936SMike Snitzer done:
2821ffcc3936SMike Snitzer 	clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2822ffcc3936SMike Snitzer 	smp_mb__after_atomic();
2823ffcc3936SMike Snitzer 	wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2824ffcc3936SMike Snitzer }
2825ffcc3936SMike Snitzer 
2826ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md)
2827fd2ed4d2SMikulas Patocka {
2828fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
2829ffcc3936SMike Snitzer 	__dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2830ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2831ffcc3936SMike Snitzer }
2832ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
2833ffcc3936SMike Snitzer 
2834ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md)
2835ffcc3936SMike Snitzer {
2836ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2837ffcc3936SMike Snitzer 	__dm_internal_resume(md);
2838ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2839ffcc3936SMike Snitzer }
2840ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume);
2841ffcc3936SMike Snitzer 
2842ffcc3936SMike Snitzer /*
2843ffcc3936SMike Snitzer  * Fast variants of internal suspend/resume hold md->suspend_lock,
2844ffcc3936SMike Snitzer  * which prevents interaction with userspace-driven suspend.
2845ffcc3936SMike Snitzer  */
2846ffcc3936SMike Snitzer 
2847ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md)
2848ffcc3936SMike Snitzer {
2849ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2850ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2851fd2ed4d2SMikulas Patocka 		return;
2852fd2ed4d2SMikulas Patocka 
2853fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2854fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
2855fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
2856fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2857fd2ed4d2SMikulas Patocka }
2858b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
2859fd2ed4d2SMikulas Patocka 
2860ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md)
2861fd2ed4d2SMikulas Patocka {
2862ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2863fd2ed4d2SMikulas Patocka 		goto done;
2864fd2ed4d2SMikulas Patocka 
2865fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
2866fd2ed4d2SMikulas Patocka 
2867fd2ed4d2SMikulas Patocka done:
2868fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
2869fd2ed4d2SMikulas Patocka }
2870b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
2871fd2ed4d2SMikulas Patocka 
28721da177e4SLinus Torvalds /*-----------------------------------------------------------------
28731da177e4SLinus Torvalds  * Event notification.
28741da177e4SLinus Torvalds  *---------------------------------------------------------------*/
28753abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
287660935eb2SMilan Broz 		       unsigned cookie)
287769267a30SAlasdair G Kergon {
287860935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
287960935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
288060935eb2SMilan Broz 
288160935eb2SMilan Broz 	if (!cookie)
28823abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
288360935eb2SMilan Broz 	else {
288460935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
288560935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
28863abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
28873abf85b5SPeter Rajnoha 					  action, envp);
288860935eb2SMilan Broz 	}
288969267a30SAlasdair G Kergon }
289069267a30SAlasdair G Kergon 
28917a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
28927a8c3d3bSMike Anderson {
28937a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
28947a8c3d3bSMike Anderson }
28957a8c3d3bSMike Anderson 
28961da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
28971da177e4SLinus Torvalds {
28981da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
28991da177e4SLinus Torvalds }
29001da177e4SLinus Torvalds 
29011da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
29021da177e4SLinus Torvalds {
29031da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
29041da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
29051da177e4SLinus Torvalds }
29061da177e4SLinus Torvalds 
29077a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
29087a8c3d3bSMike Anderson {
29097a8c3d3bSMike Anderson 	unsigned long flags;
29107a8c3d3bSMike Anderson 
29117a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
29127a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
29137a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
29147a8c3d3bSMike Anderson }
29157a8c3d3bSMike Anderson 
29161da177e4SLinus Torvalds /*
29171da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
29181da177e4SLinus Torvalds  * count on 'md'.
29191da177e4SLinus Torvalds  */
29201da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
29211da177e4SLinus Torvalds {
29221da177e4SLinus Torvalds 	return md->disk;
29231da177e4SLinus Torvalds }
292465ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk);
29251da177e4SLinus Torvalds 
2926784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2927784aae73SMilan Broz {
29282995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
2929784aae73SMilan Broz }
2930784aae73SMilan Broz 
2931784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2932784aae73SMilan Broz {
2933784aae73SMilan Broz 	struct mapped_device *md;
2934784aae73SMilan Broz 
29352995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
2936784aae73SMilan Broz 
2937b9a41d21SHou Tao 	spin_lock(&_minor_lock);
2938b9a41d21SHou Tao 	if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2939b9a41d21SHou Tao 		md = NULL;
2940b9a41d21SHou Tao 		goto out;
2941b9a41d21SHou Tao 	}
2942784aae73SMilan Broz 	dm_get(md);
2943b9a41d21SHou Tao out:
2944b9a41d21SHou Tao 	spin_unlock(&_minor_lock);
2945b9a41d21SHou Tao 
2946784aae73SMilan Broz 	return md;
2947784aae73SMilan Broz }
2948784aae73SMilan Broz 
29494f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
29501da177e4SLinus Torvalds {
29511da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
29521da177e4SLinus Torvalds }
29531da177e4SLinus Torvalds 
2954ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md)
2955ffcc3936SMike Snitzer {
2956ffcc3936SMike Snitzer 	return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2957ffcc3936SMike Snitzer }
2958ffcc3936SMike Snitzer 
29592c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
29602c140a24SMikulas Patocka {
29612c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
29622c140a24SMikulas Patocka }
29632c140a24SMikulas Patocka 
296464dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
296564dbce58SKiyoshi Ueda {
2966ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
296764dbce58SKiyoshi Ueda }
296864dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
296964dbce58SKiyoshi Ueda 
29702e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
29712e93ccc1SKiyoshi Ueda {
2972ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
29732e93ccc1SKiyoshi Ueda }
29742e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
29752e93ccc1SKiyoshi Ueda 
29767e0d574fSBart Van Assche struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type,
29770776aa0eSMike Snitzer 					    unsigned integrity, unsigned per_io_data_size,
29780776aa0eSMike Snitzer 					    unsigned min_pool_size)
2979e6ee8c0bSKiyoshi Ueda {
2980115485e8SMike Snitzer 	struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
298178d8e58aSMike Snitzer 	unsigned int pool_size = 0;
298264f52b0eSMike Snitzer 	unsigned int front_pad, io_front_pad;
29836f1c819cSKent Overstreet 	int ret;
2984e6ee8c0bSKiyoshi Ueda 
2985e6ee8c0bSKiyoshi Ueda 	if (!pools)
29864e6e36c3SMike Snitzer 		return NULL;
2987e6ee8c0bSKiyoshi Ueda 
298878d8e58aSMike Snitzer 	switch (type) {
298978d8e58aSMike Snitzer 	case DM_TYPE_BIO_BASED:
2990545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
299122c11858SMike Snitzer 	case DM_TYPE_NVME_BIO_BASED:
29920776aa0eSMike Snitzer 		pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size);
299330187e1dSMike Snitzer 		front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
299464f52b0eSMike Snitzer 		io_front_pad = roundup(front_pad,  __alignof__(struct dm_io)) + offsetof(struct dm_io, tio);
29956f1c819cSKent Overstreet 		ret = bioset_init(&pools->io_bs, pool_size, io_front_pad, 0);
29966f1c819cSKent Overstreet 		if (ret)
299764f52b0eSMike Snitzer 			goto out;
29986f1c819cSKent Overstreet 		if (integrity && bioset_integrity_create(&pools->io_bs, pool_size))
2999eb8db831SChristoph Hellwig 			goto out;
300078d8e58aSMike Snitzer 		break;
300178d8e58aSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
30020776aa0eSMike Snitzer 		pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size);
300378d8e58aSMike Snitzer 		front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
3004591ddcfcSMike Snitzer 		/* per_io_data_size is used for blk-mq pdu at queue allocation */
300578d8e58aSMike Snitzer 		break;
300678d8e58aSMike Snitzer 	default:
300778d8e58aSMike Snitzer 		BUG();
300878d8e58aSMike Snitzer 	}
300978d8e58aSMike Snitzer 
30106f1c819cSKent Overstreet 	ret = bioset_init(&pools->bs, pool_size, front_pad, 0);
30116f1c819cSKent Overstreet 	if (ret)
30125f015204SJun'ichi Nomura 		goto out;
3013e6ee8c0bSKiyoshi Ueda 
30146f1c819cSKent Overstreet 	if (integrity && bioset_integrity_create(&pools->bs, pool_size))
30155f015204SJun'ichi Nomura 		goto out;
3016a91a2785SMartin K. Petersen 
3017e6ee8c0bSKiyoshi Ueda 	return pools;
301878d8e58aSMike Snitzer 
30195f015204SJun'ichi Nomura out:
30205f015204SJun'ichi Nomura 	dm_free_md_mempools(pools);
3021e6ee8c0bSKiyoshi Ueda 
30224e6e36c3SMike Snitzer 	return NULL;
3023e6ee8c0bSKiyoshi Ueda }
3024e6ee8c0bSKiyoshi Ueda 
3025e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
3026e6ee8c0bSKiyoshi Ueda {
3027e6ee8c0bSKiyoshi Ueda 	if (!pools)
3028e6ee8c0bSKiyoshi Ueda 		return;
3029e6ee8c0bSKiyoshi Ueda 
30306f1c819cSKent Overstreet 	bioset_exit(&pools->bs);
30316f1c819cSKent Overstreet 	bioset_exit(&pools->io_bs);
3032e6ee8c0bSKiyoshi Ueda 
3033e6ee8c0bSKiyoshi Ueda 	kfree(pools);
3034e6ee8c0bSKiyoshi Ueda }
3035e6ee8c0bSKiyoshi Ueda 
30369c72bad1SChristoph Hellwig struct dm_pr {
30379c72bad1SChristoph Hellwig 	u64	old_key;
30389c72bad1SChristoph Hellwig 	u64	new_key;
30399c72bad1SChristoph Hellwig 	u32	flags;
30409c72bad1SChristoph Hellwig 	bool	fail_early;
30419c72bad1SChristoph Hellwig };
30429c72bad1SChristoph Hellwig 
30439c72bad1SChristoph Hellwig static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
30449c72bad1SChristoph Hellwig 		      void *data)
30459c72bad1SChristoph Hellwig {
30469c72bad1SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
30479c72bad1SChristoph Hellwig 	struct dm_table *table;
30489c72bad1SChristoph Hellwig 	struct dm_target *ti;
30499c72bad1SChristoph Hellwig 	int ret = -ENOTTY, srcu_idx;
30509c72bad1SChristoph Hellwig 
30519c72bad1SChristoph Hellwig 	table = dm_get_live_table(md, &srcu_idx);
30529c72bad1SChristoph Hellwig 	if (!table || !dm_table_get_size(table))
30539c72bad1SChristoph Hellwig 		goto out;
30549c72bad1SChristoph Hellwig 
30559c72bad1SChristoph Hellwig 	/* We only support devices that have a single target */
30569c72bad1SChristoph Hellwig 	if (dm_table_get_num_targets(table) != 1)
30579c72bad1SChristoph Hellwig 		goto out;
30589c72bad1SChristoph Hellwig 	ti = dm_table_get_target(table, 0);
30599c72bad1SChristoph Hellwig 
30609c72bad1SChristoph Hellwig 	ret = -EINVAL;
30619c72bad1SChristoph Hellwig 	if (!ti->type->iterate_devices)
30629c72bad1SChristoph Hellwig 		goto out;
30639c72bad1SChristoph Hellwig 
30649c72bad1SChristoph Hellwig 	ret = ti->type->iterate_devices(ti, fn, data);
30659c72bad1SChristoph Hellwig out:
30669c72bad1SChristoph Hellwig 	dm_put_live_table(md, srcu_idx);
30679c72bad1SChristoph Hellwig 	return ret;
30689c72bad1SChristoph Hellwig }
30699c72bad1SChristoph Hellwig 
30709c72bad1SChristoph Hellwig /*
30719c72bad1SChristoph Hellwig  * For register / unregister we need to manually call out to every path.
30729c72bad1SChristoph Hellwig  */
30739c72bad1SChristoph Hellwig static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
30749c72bad1SChristoph Hellwig 			    sector_t start, sector_t len, void *data)
30759c72bad1SChristoph Hellwig {
30769c72bad1SChristoph Hellwig 	struct dm_pr *pr = data;
30779c72bad1SChristoph Hellwig 	const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
30789c72bad1SChristoph Hellwig 
30799c72bad1SChristoph Hellwig 	if (!ops || !ops->pr_register)
30809c72bad1SChristoph Hellwig 		return -EOPNOTSUPP;
30819c72bad1SChristoph Hellwig 	return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
30829c72bad1SChristoph Hellwig }
30839c72bad1SChristoph Hellwig 
308471cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
308571cdb697SChristoph Hellwig 			  u32 flags)
308671cdb697SChristoph Hellwig {
30879c72bad1SChristoph Hellwig 	struct dm_pr pr = {
30889c72bad1SChristoph Hellwig 		.old_key	= old_key,
30899c72bad1SChristoph Hellwig 		.new_key	= new_key,
30909c72bad1SChristoph Hellwig 		.flags		= flags,
30919c72bad1SChristoph Hellwig 		.fail_early	= true,
30929c72bad1SChristoph Hellwig 	};
30939c72bad1SChristoph Hellwig 	int ret;
309471cdb697SChristoph Hellwig 
30959c72bad1SChristoph Hellwig 	ret = dm_call_pr(bdev, __dm_pr_register, &pr);
30969c72bad1SChristoph Hellwig 	if (ret && new_key) {
30979c72bad1SChristoph Hellwig 		/* unregister all paths if we failed to register any path */
30989c72bad1SChristoph Hellwig 		pr.old_key = new_key;
30999c72bad1SChristoph Hellwig 		pr.new_key = 0;
31009c72bad1SChristoph Hellwig 		pr.flags = 0;
31019c72bad1SChristoph Hellwig 		pr.fail_early = false;
31029c72bad1SChristoph Hellwig 		dm_call_pr(bdev, __dm_pr_register, &pr);
31039c72bad1SChristoph Hellwig 	}
310471cdb697SChristoph Hellwig 
31059c72bad1SChristoph Hellwig 	return ret;
310671cdb697SChristoph Hellwig }
310771cdb697SChristoph Hellwig 
310871cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
310971cdb697SChristoph Hellwig 			 u32 flags)
311071cdb697SChristoph Hellwig {
311171cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
311271cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3113971888c4SMike Snitzer 	int r, srcu_idx;
311471cdb697SChristoph Hellwig 
31155bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
311671cdb697SChristoph Hellwig 	if (r < 0)
3117971888c4SMike Snitzer 		goto out;
311871cdb697SChristoph Hellwig 
311971cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
312071cdb697SChristoph Hellwig 	if (ops && ops->pr_reserve)
312171cdb697SChristoph Hellwig 		r = ops->pr_reserve(bdev, key, type, flags);
312271cdb697SChristoph Hellwig 	else
312371cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3124971888c4SMike Snitzer out:
3125971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
312671cdb697SChristoph Hellwig 	return r;
312771cdb697SChristoph Hellwig }
312871cdb697SChristoph Hellwig 
312971cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
313071cdb697SChristoph Hellwig {
313171cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
313271cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3133971888c4SMike Snitzer 	int r, srcu_idx;
313471cdb697SChristoph Hellwig 
31355bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
313671cdb697SChristoph Hellwig 	if (r < 0)
3137971888c4SMike Snitzer 		goto out;
313871cdb697SChristoph Hellwig 
313971cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
314071cdb697SChristoph Hellwig 	if (ops && ops->pr_release)
314171cdb697SChristoph Hellwig 		r = ops->pr_release(bdev, key, type);
314271cdb697SChristoph Hellwig 	else
314371cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3144971888c4SMike Snitzer out:
3145971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
314671cdb697SChristoph Hellwig 	return r;
314771cdb697SChristoph Hellwig }
314871cdb697SChristoph Hellwig 
314971cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
315071cdb697SChristoph Hellwig 			 enum pr_type type, bool abort)
315171cdb697SChristoph Hellwig {
315271cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
315371cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3154971888c4SMike Snitzer 	int r, srcu_idx;
315571cdb697SChristoph Hellwig 
31565bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
315771cdb697SChristoph Hellwig 	if (r < 0)
3158971888c4SMike Snitzer 		goto out;
315971cdb697SChristoph Hellwig 
316071cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
316171cdb697SChristoph Hellwig 	if (ops && ops->pr_preempt)
316271cdb697SChristoph Hellwig 		r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
316371cdb697SChristoph Hellwig 	else
316471cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3165971888c4SMike Snitzer out:
3166971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
316771cdb697SChristoph Hellwig 	return r;
316871cdb697SChristoph Hellwig }
316971cdb697SChristoph Hellwig 
317071cdb697SChristoph Hellwig static int dm_pr_clear(struct block_device *bdev, u64 key)
317171cdb697SChristoph Hellwig {
317271cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
317371cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3174971888c4SMike Snitzer 	int r, srcu_idx;
317571cdb697SChristoph Hellwig 
31765bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
317771cdb697SChristoph Hellwig 	if (r < 0)
3178971888c4SMike Snitzer 		goto out;
317971cdb697SChristoph Hellwig 
318071cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
318171cdb697SChristoph Hellwig 	if (ops && ops->pr_clear)
318271cdb697SChristoph Hellwig 		r = ops->pr_clear(bdev, key);
318371cdb697SChristoph Hellwig 	else
318471cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3185971888c4SMike Snitzer out:
3186971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
318771cdb697SChristoph Hellwig 	return r;
318871cdb697SChristoph Hellwig }
318971cdb697SChristoph Hellwig 
319071cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = {
319171cdb697SChristoph Hellwig 	.pr_register	= dm_pr_register,
319271cdb697SChristoph Hellwig 	.pr_reserve	= dm_pr_reserve,
319371cdb697SChristoph Hellwig 	.pr_release	= dm_pr_release,
319471cdb697SChristoph Hellwig 	.pr_preempt	= dm_pr_preempt,
319571cdb697SChristoph Hellwig 	.pr_clear	= dm_pr_clear,
319671cdb697SChristoph Hellwig };
319771cdb697SChristoph Hellwig 
319883d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
31991da177e4SLinus Torvalds 	.open = dm_blk_open,
32001da177e4SLinus Torvalds 	.release = dm_blk_close,
3201aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
32023ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
3203e76239a3SChristoph Hellwig 	.report_zones = dm_blk_report_zones,
320471cdb697SChristoph Hellwig 	.pr_ops = &dm_pr_ops,
32051da177e4SLinus Torvalds 	.owner = THIS_MODULE
32061da177e4SLinus Torvalds };
32071da177e4SLinus Torvalds 
3208f26c5719SDan Williams static const struct dax_operations dm_dax_ops = {
3209f26c5719SDan Williams 	.direct_access = dm_dax_direct_access,
32107e026c8cSDan Williams 	.copy_from_iter = dm_dax_copy_from_iter,
3211b3a9a0c3SDan Williams 	.copy_to_iter = dm_dax_copy_to_iter,
3212f26c5719SDan Williams };
3213f26c5719SDan Williams 
32141da177e4SLinus Torvalds /*
32151da177e4SLinus Torvalds  * module hooks
32161da177e4SLinus Torvalds  */
32171da177e4SLinus Torvalds module_init(dm_init);
32181da177e4SLinus Torvalds module_exit(dm_exit);
32191da177e4SLinus Torvalds 
32201da177e4SLinus Torvalds module_param(major, uint, 0);
32211da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
3222f4790826SMike Snitzer 
3223e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3224e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3225e8603136SMike Snitzer 
3226115485e8SMike Snitzer module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
3227115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
3228115485e8SMike Snitzer 
32291da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
32301da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
32311da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3232