xref: /openbmc/linux/drivers/md/dm.c (revision 892ad71f622bbf39c6de321d5ca9b0fdec237c24)
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 
6494cc96131SMike Snitzer int md_in_flight(struct mapped_device *md)
65090abb8c4SKiyoshi Ueda {
65190abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
65290abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
65390abb8c4SKiyoshi Ueda }
65490abb8c4SKiyoshi Ueda 
6553eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
6563eaf840eSJun'ichi "Nick" Nomura {
6573eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
658745dc570SMike Snitzer 	struct bio *bio = io->orig_bio;
659fd2ed4d2SMikulas Patocka 	int rw = bio_data_dir(bio);
6603eaf840eSJun'ichi "Nick" Nomura 
6613eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
6623eaf840eSJun'ichi "Nick" Nomura 
663ddcf35d3SMichael Callahan 	generic_start_io_acct(md->queue, bio_op(bio), bio_sectors(bio),
664ddcf35d3SMichael Callahan 			      &dm_disk(md)->part0);
665f3986374SMike Snitzer 
6661e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw],
6671e9bb880SShaohua Li 		   atomic_inc_return(&md->pending[rw]));
668fd2ed4d2SMikulas Patocka 
669fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
670528ec5abSMike Christie 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
671528ec5abSMike Christie 				    bio->bi_iter.bi_sector, bio_sectors(bio),
672528ec5abSMike Christie 				    false, 0, &io->stats_aux);
6733eaf840eSJun'ichi "Nick" Nomura }
6743eaf840eSJun'ichi "Nick" Nomura 
675d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
6763eaf840eSJun'ichi "Nick" Nomura {
6773eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
678745dc570SMike Snitzer 	struct bio *bio = io->orig_bio;
6793eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
68018c0b223SGu Zheng 	int pending;
6813eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
6823eaf840eSJun'ichi "Nick" Nomura 
683ddcf35d3SMichael Callahan 	generic_end_io_acct(md->queue, bio_op(bio), &dm_disk(md)->part0,
684ddcf35d3SMichael Callahan 			    io->start_time);
6853eaf840eSJun'ichi "Nick" Nomura 
686fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
687528ec5abSMike Christie 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
688528ec5abSMike Christie 				    bio->bi_iter.bi_sector, bio_sectors(bio),
689528ec5abSMike Christie 				    true, duration, &io->stats_aux);
690fd2ed4d2SMikulas Patocka 
691af7e466aSMikulas Patocka 	/*
692af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
693d87f4c14STejun Heo 	 * a flush.
694af7e466aSMikulas Patocka 	 */
6951e9bb880SShaohua Li 	pending = atomic_dec_return(&md->pending[rw]);
6961e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
697316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
6983eaf840eSJun'ichi "Nick" Nomura 
699d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
700d221d2e7SMikulas Patocka 	if (!pending)
701d221d2e7SMikulas Patocka 		wake_up(&md->wait);
7023eaf840eSJun'ichi "Nick" Nomura }
7033eaf840eSJun'ichi "Nick" Nomura 
7041da177e4SLinus Torvalds /*
7051da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
7061da177e4SLinus Torvalds  */
70792c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
7081da177e4SLinus Torvalds {
70905447420SKiyoshi Ueda 	unsigned long flags;
7101da177e4SLinus Torvalds 
71105447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
7121da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
71305447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
71492c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
7151da177e4SLinus Torvalds }
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds /*
7181da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
7191da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
72083d5e5b0SMikulas Patocka  * dm_put_live_table() when finished.
7211da177e4SLinus Torvalds  */
72283d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
7231da177e4SLinus Torvalds {
72483d5e5b0SMikulas Patocka 	*srcu_idx = srcu_read_lock(&md->io_barrier);
7251da177e4SLinus Torvalds 
72683d5e5b0SMikulas Patocka 	return srcu_dereference(md->map, &md->io_barrier);
72783d5e5b0SMikulas Patocka }
7281da177e4SLinus Torvalds 
72983d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
73083d5e5b0SMikulas Patocka {
73183d5e5b0SMikulas Patocka 	srcu_read_unlock(&md->io_barrier, srcu_idx);
73283d5e5b0SMikulas Patocka }
73383d5e5b0SMikulas Patocka 
73483d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md)
73583d5e5b0SMikulas Patocka {
73683d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
73783d5e5b0SMikulas Patocka 	synchronize_rcu_expedited();
73883d5e5b0SMikulas Patocka }
73983d5e5b0SMikulas Patocka 
74083d5e5b0SMikulas Patocka /*
74183d5e5b0SMikulas Patocka  * A fast alternative to dm_get_live_table/dm_put_live_table.
74283d5e5b0SMikulas Patocka  * The caller must not block between these two functions.
74383d5e5b0SMikulas Patocka  */
74483d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
74583d5e5b0SMikulas Patocka {
74683d5e5b0SMikulas Patocka 	rcu_read_lock();
74783d5e5b0SMikulas Patocka 	return rcu_dereference(md->map);
74883d5e5b0SMikulas Patocka }
74983d5e5b0SMikulas Patocka 
75083d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
75183d5e5b0SMikulas Patocka {
75283d5e5b0SMikulas Patocka 	rcu_read_unlock();
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds 
755971888c4SMike Snitzer static char *_dm_claim_ptr = "I belong to device-mapper";
756971888c4SMike Snitzer 
7573ac51e74SDarrick J. Wong /*
75886f1152bSBenjamin Marzinski  * Open a table device so we can use it as a map destination.
75986f1152bSBenjamin Marzinski  */
76086f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev,
76186f1152bSBenjamin Marzinski 			     struct mapped_device *md)
76286f1152bSBenjamin Marzinski {
76386f1152bSBenjamin Marzinski 	struct block_device *bdev;
76486f1152bSBenjamin Marzinski 
76586f1152bSBenjamin Marzinski 	int r;
76686f1152bSBenjamin Marzinski 
76786f1152bSBenjamin Marzinski 	BUG_ON(td->dm_dev.bdev);
76886f1152bSBenjamin Marzinski 
769519049afSMike Snitzer 	bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr);
77086f1152bSBenjamin Marzinski 	if (IS_ERR(bdev))
77186f1152bSBenjamin Marzinski 		return PTR_ERR(bdev);
77286f1152bSBenjamin Marzinski 
77386f1152bSBenjamin Marzinski 	r = bd_link_disk_holder(bdev, dm_disk(md));
77486f1152bSBenjamin Marzinski 	if (r) {
77586f1152bSBenjamin Marzinski 		blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
77686f1152bSBenjamin Marzinski 		return r;
77786f1152bSBenjamin Marzinski 	}
77886f1152bSBenjamin Marzinski 
77986f1152bSBenjamin Marzinski 	td->dm_dev.bdev = bdev;
780817bf402SDan Williams 	td->dm_dev.dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
78186f1152bSBenjamin Marzinski 	return 0;
78286f1152bSBenjamin Marzinski }
78386f1152bSBenjamin Marzinski 
78486f1152bSBenjamin Marzinski /*
78586f1152bSBenjamin Marzinski  * Close a table device that we've been using.
78686f1152bSBenjamin Marzinski  */
78786f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md)
78886f1152bSBenjamin Marzinski {
78986f1152bSBenjamin Marzinski 	if (!td->dm_dev.bdev)
79086f1152bSBenjamin Marzinski 		return;
79186f1152bSBenjamin Marzinski 
79286f1152bSBenjamin Marzinski 	bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
79386f1152bSBenjamin Marzinski 	blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
794817bf402SDan Williams 	put_dax(td->dm_dev.dax_dev);
79586f1152bSBenjamin Marzinski 	td->dm_dev.bdev = NULL;
796817bf402SDan Williams 	td->dm_dev.dax_dev = NULL;
79786f1152bSBenjamin Marzinski }
79886f1152bSBenjamin Marzinski 
79986f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev,
80086f1152bSBenjamin Marzinski 					      fmode_t mode) {
80186f1152bSBenjamin Marzinski 	struct table_device *td;
80286f1152bSBenjamin Marzinski 
80386f1152bSBenjamin Marzinski 	list_for_each_entry(td, l, list)
80486f1152bSBenjamin Marzinski 		if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
80586f1152bSBenjamin Marzinski 			return td;
80686f1152bSBenjamin Marzinski 
80786f1152bSBenjamin Marzinski 	return NULL;
80886f1152bSBenjamin Marzinski }
80986f1152bSBenjamin Marzinski 
81086f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
81186f1152bSBenjamin Marzinski 			struct dm_dev **result) {
81286f1152bSBenjamin Marzinski 	int r;
81386f1152bSBenjamin Marzinski 	struct table_device *td;
81486f1152bSBenjamin Marzinski 
81586f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
81686f1152bSBenjamin Marzinski 	td = find_table_device(&md->table_devices, dev, mode);
81786f1152bSBenjamin Marzinski 	if (!td) {
818115485e8SMike Snitzer 		td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
81986f1152bSBenjamin Marzinski 		if (!td) {
82086f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
82186f1152bSBenjamin Marzinski 			return -ENOMEM;
82286f1152bSBenjamin Marzinski 		}
82386f1152bSBenjamin Marzinski 
82486f1152bSBenjamin Marzinski 		td->dm_dev.mode = mode;
82586f1152bSBenjamin Marzinski 		td->dm_dev.bdev = NULL;
82686f1152bSBenjamin Marzinski 
82786f1152bSBenjamin Marzinski 		if ((r = open_table_device(td, dev, md))) {
82886f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
82986f1152bSBenjamin Marzinski 			kfree(td);
83086f1152bSBenjamin Marzinski 			return r;
83186f1152bSBenjamin Marzinski 		}
83286f1152bSBenjamin Marzinski 
83386f1152bSBenjamin Marzinski 		format_dev_t(td->dm_dev.name, dev);
83486f1152bSBenjamin Marzinski 
835b0b4d7c6SElena Reshetova 		refcount_set(&td->count, 1);
83686f1152bSBenjamin Marzinski 		list_add(&td->list, &md->table_devices);
837b0b4d7c6SElena Reshetova 	} else {
838b0b4d7c6SElena Reshetova 		refcount_inc(&td->count);
83986f1152bSBenjamin Marzinski 	}
84086f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
84186f1152bSBenjamin Marzinski 
84286f1152bSBenjamin Marzinski 	*result = &td->dm_dev;
84386f1152bSBenjamin Marzinski 	return 0;
84486f1152bSBenjamin Marzinski }
84586f1152bSBenjamin Marzinski EXPORT_SYMBOL_GPL(dm_get_table_device);
84686f1152bSBenjamin Marzinski 
84786f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
84886f1152bSBenjamin Marzinski {
84986f1152bSBenjamin Marzinski 	struct table_device *td = container_of(d, struct table_device, dm_dev);
85086f1152bSBenjamin Marzinski 
85186f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
852b0b4d7c6SElena Reshetova 	if (refcount_dec_and_test(&td->count)) {
85386f1152bSBenjamin Marzinski 		close_table_device(td, md);
85486f1152bSBenjamin Marzinski 		list_del(&td->list);
85586f1152bSBenjamin Marzinski 		kfree(td);
85686f1152bSBenjamin Marzinski 	}
85786f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
85886f1152bSBenjamin Marzinski }
85986f1152bSBenjamin Marzinski EXPORT_SYMBOL(dm_put_table_device);
86086f1152bSBenjamin Marzinski 
86186f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices)
86286f1152bSBenjamin Marzinski {
86386f1152bSBenjamin Marzinski 	struct list_head *tmp, *next;
86486f1152bSBenjamin Marzinski 
86586f1152bSBenjamin Marzinski 	list_for_each_safe(tmp, next, devices) {
86686f1152bSBenjamin Marzinski 		struct table_device *td = list_entry(tmp, struct table_device, list);
86786f1152bSBenjamin Marzinski 
86886f1152bSBenjamin Marzinski 		DMWARN("dm_destroy: %s still exists with %d references",
869b0b4d7c6SElena Reshetova 		       td->dm_dev.name, refcount_read(&td->count));
87086f1152bSBenjamin Marzinski 		kfree(td);
87186f1152bSBenjamin Marzinski 	}
87286f1152bSBenjamin Marzinski }
87386f1152bSBenjamin Marzinski 
87486f1152bSBenjamin Marzinski /*
8753ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
8763ac51e74SDarrick J. Wong  */
8773ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
8783ac51e74SDarrick J. Wong {
8793ac51e74SDarrick J. Wong 	*geo = md->geometry;
8803ac51e74SDarrick J. Wong 
8813ac51e74SDarrick J. Wong 	return 0;
8823ac51e74SDarrick J. Wong }
8833ac51e74SDarrick J. Wong 
8843ac51e74SDarrick J. Wong /*
8853ac51e74SDarrick J. Wong  * Set the geometry of a device.
8863ac51e74SDarrick J. Wong  */
8873ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
8883ac51e74SDarrick J. Wong {
8893ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
8903ac51e74SDarrick J. Wong 
8913ac51e74SDarrick J. Wong 	if (geo->start > sz) {
8923ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
8933ac51e74SDarrick J. Wong 		return -EINVAL;
8943ac51e74SDarrick J. Wong 	}
8953ac51e74SDarrick J. Wong 
8963ac51e74SDarrick J. Wong 	md->geometry = *geo;
8973ac51e74SDarrick J. Wong 
8983ac51e74SDarrick J. Wong 	return 0;
8993ac51e74SDarrick J. Wong }
9003ac51e74SDarrick J. Wong 
9012e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
9022e93ccc1SKiyoshi Ueda {
9032e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
9042e93ccc1SKiyoshi Ueda }
9052e93ccc1SKiyoshi Ueda 
9061da177e4SLinus Torvalds /*
9071da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
9081da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
9091da177e4SLinus Torvalds  */
9104e4cbee9SChristoph Hellwig static void dec_pending(struct dm_io *io, blk_status_t error)
9111da177e4SLinus Torvalds {
9122e93ccc1SKiyoshi Ueda 	unsigned long flags;
9134e4cbee9SChristoph Hellwig 	blk_status_t io_error;
914b35f8caaSMilan Broz 	struct bio *bio;
915b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
9162e93ccc1SKiyoshi Ueda 
9172e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
918f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
919f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
920745dc570SMike Snitzer 		if (!(io->status == BLK_STS_DM_REQUEUE && __noflush_suspending(md)))
9214e4cbee9SChristoph Hellwig 			io->status = error;
922f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
923f88fb981SKiyoshi Ueda 	}
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
9264e4cbee9SChristoph Hellwig 		if (io->status == BLK_STS_DM_REQUEUE) {
9272e93ccc1SKiyoshi Ueda 			/*
9282e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
9292e93ccc1SKiyoshi Ueda 			 */
930022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
9316a8736d1STejun Heo 			if (__noflush_suspending(md))
932745dc570SMike Snitzer 				/* NOTE early return due to BLK_STS_DM_REQUEUE below */
933745dc570SMike Snitzer 				bio_list_add_head(&md->deferred, io->orig_bio);
9346a8736d1STejun Heo 			else
9352e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
9364e4cbee9SChristoph Hellwig 				io->status = BLK_STS_IOERR;
937022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
9382e93ccc1SKiyoshi Ueda 		}
9392e93ccc1SKiyoshi Ueda 
9404e4cbee9SChristoph Hellwig 		io_error = io->status;
941745dc570SMike Snitzer 		bio = io->orig_bio;
942af7e466aSMikulas Patocka 		end_io_acct(io);
943a97f925aSMikulas Patocka 		free_io(md, io);
9441da177e4SLinus Torvalds 
9454e4cbee9SChristoph Hellwig 		if (io_error == BLK_STS_DM_REQUEUE)
9466a8736d1STejun Heo 			return;
9476a8736d1STejun Heo 
9481eff9d32SJens Axboe 		if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) {
9491da177e4SLinus Torvalds 			/*
9506a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
95128a8f0d3SMike Christie 			 * without REQ_PREFLUSH.
9521da177e4SLinus Torvalds 			 */
9531eff9d32SJens Axboe 			bio->bi_opf &= ~REQ_PREFLUSH;
9546a8736d1STejun Heo 			queue_io(md, bio);
955b35f8caaSMilan Broz 		} else {
956b372d360SMike Snitzer 			/* done with normal IO or empty flush */
9578dd601faSNeilBrown 			if (io_error)
9584e4cbee9SChristoph Hellwig 				bio->bi_status = io_error;
9594246a0b6SChristoph Hellwig 			bio_endio(bio);
9602e93ccc1SKiyoshi Ueda 		}
9611da177e4SLinus Torvalds 	}
962af7e466aSMikulas Patocka }
9631da177e4SLinus Torvalds 
9644cc96131SMike Snitzer void disable_write_same(struct mapped_device *md)
9657eee4ae2SMike Snitzer {
9667eee4ae2SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
9677eee4ae2SMike Snitzer 
9687eee4ae2SMike Snitzer 	/* device doesn't really support WRITE SAME, disable it */
9697eee4ae2SMike Snitzer 	limits->max_write_same_sectors = 0;
9707eee4ae2SMike Snitzer }
9717eee4ae2SMike Snitzer 
972ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md)
973ac62d620SChristoph Hellwig {
974ac62d620SChristoph Hellwig 	struct queue_limits *limits = dm_get_queue_limits(md);
975ac62d620SChristoph Hellwig 
976ac62d620SChristoph Hellwig 	/* device doesn't really support WRITE ZEROES, disable it */
977ac62d620SChristoph Hellwig 	limits->max_write_zeroes_sectors = 0;
978ac62d620SChristoph Hellwig }
979ac62d620SChristoph Hellwig 
9804246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio)
9811da177e4SLinus Torvalds {
9824e4cbee9SChristoph Hellwig 	blk_status_t error = bio->bi_status;
983bfc6d41cSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
984b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
9859faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
9861da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
9871da177e4SLinus Torvalds 
988978e51baSMike Snitzer 	if (unlikely(error == BLK_STS_TARGET) && md->type != DM_TYPE_NVME_BIO_BASED) {
989ac62d620SChristoph Hellwig 		if (bio_op(bio) == REQ_OP_WRITE_SAME &&
99074d46992SChristoph Hellwig 		    !bio->bi_disk->queue->limits.max_write_same_sectors)
9917eee4ae2SMike Snitzer 			disable_write_same(md);
992ac62d620SChristoph Hellwig 		if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
99374d46992SChristoph Hellwig 		    !bio->bi_disk->queue->limits.max_write_zeroes_sectors)
994ac62d620SChristoph Hellwig 			disable_write_zeroes(md);
995ac62d620SChristoph Hellwig 	}
9967eee4ae2SMike Snitzer 
9971be56909SChristoph Hellwig 	if (endio) {
9984e4cbee9SChristoph Hellwig 		int r = endio(tio->ti, bio, &error);
9991be56909SChristoph Hellwig 		switch (r) {
10001be56909SChristoph Hellwig 		case DM_ENDIO_REQUEUE:
10014e4cbee9SChristoph Hellwig 			error = BLK_STS_DM_REQUEUE;
10021be56909SChristoph Hellwig 			/*FALLTHRU*/
10031be56909SChristoph Hellwig 		case DM_ENDIO_DONE:
10041be56909SChristoph Hellwig 			break;
10051be56909SChristoph Hellwig 		case DM_ENDIO_INCOMPLETE:
10061be56909SChristoph Hellwig 			/* The target will handle the io */
10071be56909SChristoph Hellwig 			return;
10081be56909SChristoph Hellwig 		default:
10091be56909SChristoph Hellwig 			DMWARN("unimplemented target endio return value: %d", r);
10101be56909SChristoph Hellwig 			BUG();
10111be56909SChristoph Hellwig 		}
10121be56909SChristoph Hellwig 	}
10131be56909SChristoph Hellwig 
1014cfae7529SMike Snitzer 	free_tio(tio);
1015b35f8caaSMilan Broz 	dec_pending(io, error);
10161da177e4SLinus Torvalds }
10171da177e4SLinus Torvalds 
101878d8e58aSMike Snitzer /*
101956a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
102056a67df7SMike Snitzer  * target boundary.
102156a67df7SMike Snitzer  */
102256a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
10231da177e4SLinus Torvalds {
102456a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
102556a67df7SMike Snitzer 
102656a67df7SMike Snitzer 	return ti->len - target_offset;
102756a67df7SMike Snitzer }
102856a67df7SMike Snitzer 
102956a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
103056a67df7SMike Snitzer {
103156a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
1032542f9038SMike Snitzer 	sector_t offset, max_len;
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds 	/*
10351da177e4SLinus Torvalds 	 * Does the target need to split even further?
10361da177e4SLinus Torvalds 	 */
1037542f9038SMike Snitzer 	if (ti->max_io_len) {
1038542f9038SMike Snitzer 		offset = dm_target_offset(ti, sector);
1039542f9038SMike Snitzer 		if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1040542f9038SMike Snitzer 			max_len = sector_div(offset, ti->max_io_len);
1041542f9038SMike Snitzer 		else
1042542f9038SMike Snitzer 			max_len = offset & (ti->max_io_len - 1);
1043542f9038SMike Snitzer 		max_len = ti->max_io_len - max_len;
1044542f9038SMike Snitzer 
1045542f9038SMike Snitzer 		if (len > max_len)
1046542f9038SMike Snitzer 			len = max_len;
10471da177e4SLinus Torvalds 	}
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds 	return len;
10501da177e4SLinus Torvalds }
10511da177e4SLinus Torvalds 
1052542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1053542f9038SMike Snitzer {
1054542f9038SMike Snitzer 	if (len > UINT_MAX) {
1055542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1056542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
1057542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
1058542f9038SMike Snitzer 		return -EINVAL;
1059542f9038SMike Snitzer 	}
1060542f9038SMike Snitzer 
10618f50e358SMing Lei 	/*
10628f50e358SMing Lei 	 * BIO based queue uses its own splitting. When multipage bvecs
10638f50e358SMing Lei 	 * is switched on, size of the incoming bio may be too big to
10648f50e358SMing Lei 	 * be handled in some targets, such as crypt.
10658f50e358SMing Lei 	 *
10668f50e358SMing Lei 	 * When these targets are ready for the big bio, we can remove
10678f50e358SMing Lei 	 * the limit.
10688f50e358SMing Lei 	 */
10698f50e358SMing Lei 	ti->max_io_len = min_t(uint32_t, len, BIO_MAX_PAGES * PAGE_SIZE);
1070542f9038SMike Snitzer 
1071542f9038SMike Snitzer 	return 0;
1072542f9038SMike Snitzer }
1073542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1074542f9038SMike Snitzer 
1075f26c5719SDan Williams static struct dm_target *dm_dax_get_live_target(struct mapped_device *md,
1076f26c5719SDan Williams 						sector_t sector, int *srcu_idx)
10773d97c829SMike Snitzer 	__acquires(md->io_barrier)
1078545ed20eSToshi Kani {
1079545ed20eSToshi Kani 	struct dm_table *map;
1080545ed20eSToshi Kani 	struct dm_target *ti;
1081545ed20eSToshi Kani 
1082f26c5719SDan Williams 	map = dm_get_live_table(md, srcu_idx);
1083545ed20eSToshi Kani 	if (!map)
1084f26c5719SDan Williams 		return NULL;
1085545ed20eSToshi Kani 
1086545ed20eSToshi Kani 	ti = dm_table_find_target(map, sector);
1087545ed20eSToshi Kani 	if (!dm_target_is_valid(ti))
1088f26c5719SDan Williams 		return NULL;
1089f26c5719SDan Williams 
1090f26c5719SDan Williams 	return ti;
1091f26c5719SDan Williams }
1092f26c5719SDan Williams 
1093f26c5719SDan Williams static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
1094f26c5719SDan Williams 				 long nr_pages, void **kaddr, pfn_t *pfn)
1095f26c5719SDan Williams {
1096f26c5719SDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
1097f26c5719SDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
1098f26c5719SDan Williams 	struct dm_target *ti;
1099f26c5719SDan Williams 	long len, ret = -EIO;
1100f26c5719SDan Williams 	int srcu_idx;
1101f26c5719SDan Williams 
1102f26c5719SDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1103f26c5719SDan Williams 
1104f26c5719SDan Williams 	if (!ti)
1105545ed20eSToshi Kani 		goto out;
1106f26c5719SDan Williams 	if (!ti->type->direct_access)
1107f26c5719SDan Williams 		goto out;
1108f26c5719SDan Williams 	len = max_io_len(sector, ti) / PAGE_SECTORS;
1109f26c5719SDan Williams 	if (len < 1)
1110f26c5719SDan Williams 		goto out;
1111f26c5719SDan Williams 	nr_pages = min(len, nr_pages);
1112817bf402SDan Williams 	ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn);
1113817bf402SDan Williams 
1114545ed20eSToshi Kani  out:
1115545ed20eSToshi Kani 	dm_put_live_table(md, srcu_idx);
1116f26c5719SDan Williams 
1117f26c5719SDan Williams 	return ret;
1118545ed20eSToshi Kani }
1119545ed20eSToshi Kani 
11207e026c8cSDan Williams static size_t dm_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
11217e026c8cSDan Williams 				    void *addr, size_t bytes, struct iov_iter *i)
11227e026c8cSDan Williams {
11237e026c8cSDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
11247e026c8cSDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
11257e026c8cSDan Williams 	struct dm_target *ti;
11267e026c8cSDan Williams 	long ret = 0;
11277e026c8cSDan Williams 	int srcu_idx;
11287e026c8cSDan Williams 
11297e026c8cSDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
11307e026c8cSDan Williams 
11317e026c8cSDan Williams 	if (!ti)
11327e026c8cSDan Williams 		goto out;
11337e026c8cSDan Williams 	if (!ti->type->dax_copy_from_iter) {
11347e026c8cSDan Williams 		ret = copy_from_iter(addr, bytes, i);
11357e026c8cSDan Williams 		goto out;
11367e026c8cSDan Williams 	}
11377e026c8cSDan Williams 	ret = ti->type->dax_copy_from_iter(ti, pgoff, addr, bytes, i);
11387e026c8cSDan Williams  out:
11397e026c8cSDan Williams 	dm_put_live_table(md, srcu_idx);
11407e026c8cSDan Williams 
11417e026c8cSDan Williams 	return ret;
11427e026c8cSDan Williams }
11437e026c8cSDan Williams 
1144b3a9a0c3SDan Williams static size_t dm_dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff,
1145b3a9a0c3SDan Williams 		void *addr, size_t bytes, struct iov_iter *i)
1146b3a9a0c3SDan Williams {
1147b3a9a0c3SDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
1148b3a9a0c3SDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
1149b3a9a0c3SDan Williams 	struct dm_target *ti;
1150b3a9a0c3SDan Williams 	long ret = 0;
1151b3a9a0c3SDan Williams 	int srcu_idx;
1152b3a9a0c3SDan Williams 
1153b3a9a0c3SDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1154b3a9a0c3SDan Williams 
1155b3a9a0c3SDan Williams 	if (!ti)
1156b3a9a0c3SDan Williams 		goto out;
1157b3a9a0c3SDan Williams 	if (!ti->type->dax_copy_to_iter) {
1158b3a9a0c3SDan Williams 		ret = copy_to_iter(addr, bytes, i);
1159b3a9a0c3SDan Williams 		goto out;
1160b3a9a0c3SDan Williams 	}
1161b3a9a0c3SDan Williams 	ret = ti->type->dax_copy_to_iter(ti, pgoff, addr, bytes, i);
1162b3a9a0c3SDan Williams  out:
1163b3a9a0c3SDan Williams 	dm_put_live_table(md, srcu_idx);
1164b3a9a0c3SDan Williams 
1165b3a9a0c3SDan Williams 	return ret;
1166b3a9a0c3SDan Williams }
1167b3a9a0c3SDan Williams 
11681dd40c3eSMikulas Patocka /*
11691dd40c3eSMikulas Patocka  * A target may call dm_accept_partial_bio only from the map routine.  It is
1170c06b3e58SNeilBrown  * allowed for all bio types except REQ_PREFLUSH and REQ_OP_ZONE_RESET.
11711dd40c3eSMikulas Patocka  *
11721dd40c3eSMikulas Patocka  * dm_accept_partial_bio informs the dm that the target only wants to process
11731dd40c3eSMikulas Patocka  * additional n_sectors sectors of the bio and the rest of the data should be
11741dd40c3eSMikulas Patocka  * sent in a next bio.
11751dd40c3eSMikulas Patocka  *
11761dd40c3eSMikulas Patocka  * A diagram that explains the arithmetics:
11771dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
11781dd40c3eSMikulas Patocka  * |         1          |       2       |   3   |
11791dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
11801dd40c3eSMikulas Patocka  *
11811dd40c3eSMikulas Patocka  * <-------------- *tio->len_ptr --------------->
11821dd40c3eSMikulas Patocka  *                      <------- bi_size ------->
11831dd40c3eSMikulas Patocka  *                      <-- n_sectors -->
11841dd40c3eSMikulas Patocka  *
11851dd40c3eSMikulas Patocka  * Region 1 was already iterated over with bio_advance or similar function.
11861dd40c3eSMikulas Patocka  *	(it may be empty if the target doesn't use bio_advance)
11871dd40c3eSMikulas Patocka  * Region 2 is the remaining bio size that the target wants to process.
11881dd40c3eSMikulas Patocka  *	(it may be empty if region 1 is non-empty, although there is no reason
11891dd40c3eSMikulas Patocka  *	 to make it empty)
11901dd40c3eSMikulas Patocka  * The target requires that region 3 is to be sent in the next bio.
11911dd40c3eSMikulas Patocka  *
11921dd40c3eSMikulas Patocka  * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
11931dd40c3eSMikulas Patocka  * the partially processed part (the sum of regions 1+2) must be the same for all
11941dd40c3eSMikulas Patocka  * copies of the bio.
11951dd40c3eSMikulas Patocka  */
11961dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
11971dd40c3eSMikulas Patocka {
11981dd40c3eSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
11991dd40c3eSMikulas Patocka 	unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
12001eff9d32SJens Axboe 	BUG_ON(bio->bi_opf & REQ_PREFLUSH);
12011dd40c3eSMikulas Patocka 	BUG_ON(bi_size > *tio->len_ptr);
12021dd40c3eSMikulas Patocka 	BUG_ON(n_sectors > bi_size);
12031dd40c3eSMikulas Patocka 	*tio->len_ptr -= bi_size - n_sectors;
12041dd40c3eSMikulas Patocka 	bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
12051dd40c3eSMikulas Patocka }
12061dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
12071dd40c3eSMikulas Patocka 
1208d67a5f4bSMikulas Patocka /*
1209e76239a3SChristoph Hellwig  * The zone descriptors obtained with a zone report indicate
1210e76239a3SChristoph Hellwig  * zone positions within the underlying device of the target. The zone
1211e76239a3SChristoph Hellwig  * descriptors must be remapped to match their position within the dm device.
1212e76239a3SChristoph Hellwig  * The caller target should obtain the zones information using
1213e76239a3SChristoph Hellwig  * blkdev_report_zones() to ensure that remapping for partition offset is
1214e76239a3SChristoph Hellwig  * already handled.
121510999307SDamien Le Moal  */
1216e76239a3SChristoph Hellwig void dm_remap_zone_report(struct dm_target *ti, sector_t start,
1217e76239a3SChristoph Hellwig 			  struct blk_zone *zones, unsigned int *nr_zones)
121810999307SDamien Le Moal {
121910999307SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
122010999307SDamien Le Moal 	struct blk_zone *zone;
1221e76239a3SChristoph Hellwig 	unsigned int nrz = *nr_zones;
1222e76239a3SChristoph Hellwig 	int i;
122310999307SDamien Le Moal 
122410999307SDamien Le Moal 	/*
1225e76239a3SChristoph Hellwig 	 * Remap the start sector and write pointer position of the zones in
1226e76239a3SChristoph Hellwig 	 * the array. Since we may have obtained from the target underlying
1227e76239a3SChristoph Hellwig 	 * device more zones that the target size, also adjust the number
1228e76239a3SChristoph Hellwig 	 * of zones.
12299864cd5dSDamien Le Moal 	 */
1230e76239a3SChristoph Hellwig 	for (i = 0; i < nrz; i++) {
1231e76239a3SChristoph Hellwig 		zone = zones + i;
123210999307SDamien Le Moal 		if (zone->start >= start + ti->len) {
1233e76239a3SChristoph Hellwig 			memset(zone, 0, sizeof(struct blk_zone) * (nrz - i));
123410999307SDamien Le Moal 			break;
123510999307SDamien Le Moal 		}
1236e76239a3SChristoph Hellwig 
123710999307SDamien Le Moal 		zone->start = zone->start + ti->begin - start;
1238e76239a3SChristoph Hellwig 		if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
1239e76239a3SChristoph Hellwig 			continue;
1240e76239a3SChristoph Hellwig 
124110999307SDamien Le Moal 		if (zone->cond == BLK_ZONE_COND_FULL)
124210999307SDamien Le Moal 			zone->wp = zone->start + zone->len;
124310999307SDamien Le Moal 		else if (zone->cond == BLK_ZONE_COND_EMPTY)
124410999307SDamien Le Moal 			zone->wp = zone->start;
124510999307SDamien Le Moal 		else
1246e76239a3SChristoph Hellwig 			zone->wp = zone->wp + ti->begin - start;
124710999307SDamien Le Moal 	}
124810999307SDamien Le Moal 
1249e76239a3SChristoph Hellwig 	*nr_zones = i;
125010999307SDamien Le Moal #else /* !CONFIG_BLK_DEV_ZONED */
1251e76239a3SChristoph Hellwig 	*nr_zones = 0;
125210999307SDamien Le Moal #endif
125310999307SDamien Le Moal }
125410999307SDamien Le Moal EXPORT_SYMBOL_GPL(dm_remap_zone_report);
125510999307SDamien Le Moal 
1256978e51baSMike Snitzer static blk_qc_t __map_bio(struct dm_target_io *tio)
12571da177e4SLinus Torvalds {
12581da177e4SLinus Torvalds 	int r;
12592056a782SJens Axboe 	sector_t sector;
1260dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
126164f52b0eSMike Snitzer 	struct dm_io *io = tio->io;
1262978e51baSMike Snitzer 	struct mapped_device *md = io->md;
1263bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
1264978e51baSMike Snitzer 	blk_qc_t ret = BLK_QC_T_NONE;
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 	/*
12691da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
12701da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
12711da177e4SLinus Torvalds 	 * this io.
12721da177e4SLinus Torvalds 	 */
127364f52b0eSMike Snitzer 	atomic_inc(&io->io_count);
12744f024f37SKent Overstreet 	sector = clone->bi_iter.bi_sector;
1275d67a5f4bSMikulas Patocka 
12767de3ee57SMikulas Patocka 	r = ti->type->map(ti, clone);
1277846785e6SChristoph Hellwig 	switch (r) {
1278846785e6SChristoph Hellwig 	case DM_MAPIO_SUBMITTED:
1279846785e6SChristoph Hellwig 		break;
1280846785e6SChristoph Hellwig 	case DM_MAPIO_REMAPPED:
12811da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
128274d46992SChristoph Hellwig 		trace_block_bio_remap(clone->bi_disk->queue, clone,
128364f52b0eSMike Snitzer 				      bio_dev(io->orig_bio), sector);
1284978e51baSMike Snitzer 		if (md->type == DM_TYPE_NVME_BIO_BASED)
1285978e51baSMike Snitzer 			ret = direct_make_request(clone);
1286978e51baSMike Snitzer 		else
1287978e51baSMike Snitzer 			ret = generic_make_request(clone);
1288846785e6SChristoph Hellwig 		break;
1289846785e6SChristoph Hellwig 	case DM_MAPIO_KILL:
12904e4cbee9SChristoph Hellwig 		free_tio(tio);
129164f52b0eSMike Snitzer 		dec_pending(io, BLK_STS_IOERR);
12924e4cbee9SChristoph Hellwig 		break;
1293846785e6SChristoph Hellwig 	case DM_MAPIO_REQUEUE:
1294cfae7529SMike Snitzer 		free_tio(tio);
129564f52b0eSMike Snitzer 		dec_pending(io, BLK_STS_DM_REQUEUE);
1296846785e6SChristoph Hellwig 		break;
1297846785e6SChristoph Hellwig 	default:
129845cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
129945cbcd79SKiyoshi Ueda 		BUG();
13001da177e4SLinus Torvalds 	}
13011da177e4SLinus Torvalds 
1302978e51baSMike Snitzer 	return ret;
13031da177e4SLinus Torvalds }
13041da177e4SLinus Torvalds 
1305e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
1306bd2a49b8SAlasdair G Kergon {
13074f024f37SKent Overstreet 	bio->bi_iter.bi_sector = sector;
13084f024f37SKent Overstreet 	bio->bi_iter.bi_size = to_bytes(len);
13091da177e4SLinus Torvalds }
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds /*
13121da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
13131da177e4SLinus Torvalds  */
1314c80914e8SMike Snitzer static int clone_bio(struct dm_target_io *tio, struct bio *bio,
13151c3b13e6SKent Overstreet 		     sector_t sector, unsigned len)
13161da177e4SLinus Torvalds {
1317dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
13181da177e4SLinus Torvalds 
13191c3b13e6SKent Overstreet 	__bio_clone_fast(clone, bio);
13209c47008dSMartin K. Petersen 
1321e2460f2aSMikulas Patocka 	if (unlikely(bio_integrity(bio) != NULL)) {
1322e2460f2aSMikulas Patocka 		int r;
1323e2460f2aSMikulas Patocka 
1324e2460f2aSMikulas Patocka 		if (unlikely(!dm_target_has_integrity(tio->ti->type) &&
1325e2460f2aSMikulas Patocka 			     !dm_target_passes_integrity(tio->ti->type))) {
1326e2460f2aSMikulas Patocka 			DMWARN("%s: the target %s doesn't support integrity data.",
1327e2460f2aSMikulas Patocka 				dm_device_name(tio->io->md),
1328e2460f2aSMikulas Patocka 				tio->ti->type->name);
1329e2460f2aSMikulas Patocka 			return -EIO;
1330e2460f2aSMikulas Patocka 		}
1331e2460f2aSMikulas Patocka 
1332e2460f2aSMikulas Patocka 		r = bio_integrity_clone(clone, bio, GFP_NOIO);
1333c80914e8SMike Snitzer 		if (r < 0)
1334c80914e8SMike Snitzer 			return r;
1335c80914e8SMike Snitzer 	}
13361c3b13e6SKent Overstreet 
13371c3b13e6SKent Overstreet 	bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
13381c3b13e6SKent Overstreet 	clone->bi_iter.bi_size = to_bytes(len);
13391c3b13e6SKent Overstreet 
1340e2460f2aSMikulas Patocka 	if (unlikely(bio_integrity(bio) != NULL))
1341fbd08e76SDmitry Monakhov 		bio_integrity_trim(clone);
1342c80914e8SMike Snitzer 
1343c80914e8SMike Snitzer 	return 0;
13441da177e4SLinus Torvalds }
13451da177e4SLinus Torvalds 
1346318716ddSMike Snitzer static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci,
1347318716ddSMike Snitzer 				struct dm_target *ti, unsigned num_bios)
1348f9ab94ceSMikulas Patocka {
1349dba14160SMikulas Patocka 	struct dm_target_io *tio;
1350318716ddSMike Snitzer 	int try;
1351dba14160SMikulas Patocka 
1352318716ddSMike Snitzer 	if (!num_bios)
1353318716ddSMike Snitzer 		return;
1354f9ab94ceSMikulas Patocka 
1355318716ddSMike Snitzer 	if (num_bios == 1) {
1356318716ddSMike Snitzer 		tio = alloc_tio(ci, ti, 0, GFP_NOIO);
1357318716ddSMike Snitzer 		bio_list_add(blist, &tio->clone);
1358318716ddSMike Snitzer 		return;
13599015df24SAlasdair G Kergon 	}
13609015df24SAlasdair G Kergon 
1361318716ddSMike Snitzer 	for (try = 0; try < 2; try++) {
1362318716ddSMike Snitzer 		int bio_nr;
1363318716ddSMike Snitzer 		struct bio *bio;
1364318716ddSMike Snitzer 
1365318716ddSMike Snitzer 		if (try)
1366bc02cdbeSMike Snitzer 			mutex_lock(&ci->io->md->table_devices_lock);
1367318716ddSMike Snitzer 		for (bio_nr = 0; bio_nr < num_bios; bio_nr++) {
1368318716ddSMike Snitzer 			tio = alloc_tio(ci, ti, bio_nr, try ? GFP_NOIO : GFP_NOWAIT);
1369318716ddSMike Snitzer 			if (!tio)
1370318716ddSMike Snitzer 				break;
1371318716ddSMike Snitzer 
1372318716ddSMike Snitzer 			bio_list_add(blist, &tio->clone);
1373318716ddSMike Snitzer 		}
1374318716ddSMike Snitzer 		if (try)
1375bc02cdbeSMike Snitzer 			mutex_unlock(&ci->io->md->table_devices_lock);
1376318716ddSMike Snitzer 		if (bio_nr == num_bios)
1377318716ddSMike Snitzer 			return;
1378318716ddSMike Snitzer 
1379318716ddSMike Snitzer 		while ((bio = bio_list_pop(blist))) {
1380318716ddSMike Snitzer 			tio = container_of(bio, struct dm_target_io, clone);
1381318716ddSMike Snitzer 			free_tio(tio);
1382318716ddSMike Snitzer 		}
1383318716ddSMike Snitzer 	}
1384318716ddSMike Snitzer }
1385318716ddSMike Snitzer 
1386978e51baSMike Snitzer static blk_qc_t __clone_and_map_simple_bio(struct clone_info *ci,
1387318716ddSMike Snitzer 					   struct dm_target_io *tio, unsigned *len)
13889015df24SAlasdair G Kergon {
1389dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
13909015df24SAlasdair G Kergon 
13911dd40c3eSMikulas Patocka 	tio->len_ptr = len;
13921dd40c3eSMikulas Patocka 
13931c3b13e6SKent Overstreet 	__bio_clone_fast(clone, ci->bio);
1394bd2a49b8SAlasdair G Kergon 	if (len)
13951dd40c3eSMikulas Patocka 		bio_setup_sector(clone, ci->sector, *len);
1396f9ab94ceSMikulas Patocka 
1397978e51baSMike Snitzer 	return __map_bio(tio);
1398f9ab94ceSMikulas Patocka }
1399f9ab94ceSMikulas Patocka 
140014fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
14011dd40c3eSMikulas Patocka 				  unsigned num_bios, unsigned *len)
140206a426ceSMike Snitzer {
1403318716ddSMike Snitzer 	struct bio_list blist = BIO_EMPTY_LIST;
1404318716ddSMike Snitzer 	struct bio *bio;
1405318716ddSMike Snitzer 	struct dm_target_io *tio;
140606a426ceSMike Snitzer 
1407318716ddSMike Snitzer 	alloc_multiple_bios(&blist, ci, ti, num_bios);
1408318716ddSMike Snitzer 
1409318716ddSMike Snitzer 	while ((bio = bio_list_pop(&blist))) {
1410318716ddSMike Snitzer 		tio = container_of(bio, struct dm_target_io, clone);
1411978e51baSMike Snitzer 		(void) __clone_and_map_simple_bio(ci, tio, len);
1412318716ddSMike Snitzer 	}
141306a426ceSMike Snitzer }
141406a426ceSMike Snitzer 
141514fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1416f9ab94ceSMikulas Patocka {
141706a426ceSMike Snitzer 	unsigned target_nr = 0;
1418f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1419f9ab94ceSMikulas Patocka 
1420*892ad71fSDennis Zhou 	/*
1421*892ad71fSDennis Zhou 	 * Empty flush uses a statically initialized bio, &md->flush_bio, as
1422*892ad71fSDennis Zhou 	 * the base for cloning.  However, blkg association requires that a
1423*892ad71fSDennis Zhou 	 * bdev is associated with a gendisk, which doesn't happen until the
1424*892ad71fSDennis Zhou 	 * bdev is opened.  So, blkg association is done at issue time of the
1425*892ad71fSDennis Zhou 	 * flush rather than when the device is created in alloc_dev().
1426*892ad71fSDennis Zhou 	 */
1427*892ad71fSDennis Zhou 	bio_set_dev(ci->bio, ci->io->md->bdev);
1428*892ad71fSDennis Zhou 
1429b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1430f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
14311dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
1432f9ab94ceSMikulas Patocka 
1433*892ad71fSDennis Zhou 	bio_disassociate_blkg(ci->bio);
1434*892ad71fSDennis Zhou 
1435f9ab94ceSMikulas Patocka 	return 0;
1436f9ab94ceSMikulas Patocka }
1437f9ab94ceSMikulas Patocka 
1438c80914e8SMike Snitzer static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
14391dd40c3eSMikulas Patocka 				    sector_t sector, unsigned *len)
14405ae89a87SMike Snitzer {
1441dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
14425ae89a87SMike Snitzer 	struct dm_target_io *tio;
1443f31c21e4SNeilBrown 	int r;
14445ae89a87SMike Snitzer 
1445318716ddSMike Snitzer 	tio = alloc_tio(ci, ti, 0, GFP_NOIO);
14461dd40c3eSMikulas Patocka 	tio->len_ptr = len;
1447c80914e8SMike Snitzer 	r = clone_bio(tio, bio, sector, *len);
1448072623deSMikulas Patocka 	if (r < 0) {
1449cfae7529SMike Snitzer 		free_tio(tio);
1450c80914e8SMike Snitzer 		return r;
1451b0d8ed4dSAlasdair G Kergon 	}
1452978e51baSMike Snitzer 	(void) __map_bio(tio);
145355a62eefSAlasdair G Kergon 
1454f31c21e4SNeilBrown 	return 0;
145523508a96SMike Snitzer }
145655a62eefSAlasdair G Kergon 
145723508a96SMike Snitzer typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
145855a62eefSAlasdair G Kergon 
145923508a96SMike Snitzer static unsigned get_num_discard_bios(struct dm_target *ti)
146023508a96SMike Snitzer {
146123508a96SMike Snitzer 	return ti->num_discard_bios;
146223508a96SMike Snitzer }
146323508a96SMike Snitzer 
146400716545SDenis Semakin static unsigned get_num_secure_erase_bios(struct dm_target *ti)
146500716545SDenis Semakin {
146600716545SDenis Semakin 	return ti->num_secure_erase_bios;
146700716545SDenis Semakin }
146800716545SDenis Semakin 
146923508a96SMike Snitzer static unsigned get_num_write_same_bios(struct dm_target *ti)
147023508a96SMike Snitzer {
147123508a96SMike Snitzer 	return ti->num_write_same_bios;
147223508a96SMike Snitzer }
147323508a96SMike Snitzer 
1474ac62d620SChristoph Hellwig static unsigned get_num_write_zeroes_bios(struct dm_target *ti)
1475ac62d620SChristoph Hellwig {
1476ac62d620SChristoph Hellwig 	return ti->num_write_zeroes_bios;
1477ac62d620SChristoph Hellwig }
1478ac62d620SChristoph Hellwig 
147923508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti);
148023508a96SMike Snitzer 
148123508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti)
148223508a96SMike Snitzer {
148355a62eefSAlasdair G Kergon 	return ti->split_discard_bios;
148423508a96SMike Snitzer }
148523508a96SMike Snitzer 
14863d7f4562SMike Snitzer static int __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti,
148755a62eefSAlasdair G Kergon 				       get_num_bios_fn get_num_bios,
148823508a96SMike Snitzer 				       is_split_required_fn is_split_required)
14895ae89a87SMike Snitzer {
1490e0d6609aSMikulas Patocka 	unsigned len;
149155a62eefSAlasdair G Kergon 	unsigned num_bios;
14925ae89a87SMike Snitzer 
14935ae89a87SMike Snitzer 	/*
149423508a96SMike Snitzer 	 * Even though the device advertised support for this type of
149523508a96SMike Snitzer 	 * request, that does not mean every target supports it, and
1496936688d7SMike Snitzer 	 * reconfiguration might also have changed that since the
14975ae89a87SMike Snitzer 	 * check was performed.
14985ae89a87SMike Snitzer 	 */
149955a62eefSAlasdair G Kergon 	num_bios = get_num_bios ? get_num_bios(ti) : 0;
150055a62eefSAlasdair G Kergon 	if (!num_bios)
15015ae89a87SMike Snitzer 		return -EOPNOTSUPP;
15025ae89a87SMike Snitzer 
150323508a96SMike Snitzer 	if (is_split_required && !is_split_required(ti))
1504e0d6609aSMikulas Patocka 		len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
15057acf0277SMikulas Patocka 	else
1506e0d6609aSMikulas Patocka 		len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
15075ae89a87SMike Snitzer 
15081dd40c3eSMikulas Patocka 	__send_duplicate_bios(ci, ti, num_bios, &len);
15095ae89a87SMike Snitzer 
1510a79245b3SMike Snitzer 	ci->sector += len;
15113d7f4562SMike Snitzer 	ci->sector_count -= len;
15125ae89a87SMike Snitzer 
15135ae89a87SMike Snitzer 	return 0;
15145ae89a87SMike Snitzer }
15155ae89a87SMike Snitzer 
15163d7f4562SMike Snitzer static int __send_discard(struct clone_info *ci, struct dm_target *ti)
151723508a96SMike Snitzer {
15183d7f4562SMike Snitzer 	return __send_changing_extent_only(ci, ti, get_num_discard_bios,
151923508a96SMike Snitzer 					   is_split_required_for_discard);
152023508a96SMike Snitzer }
152123508a96SMike Snitzer 
152200716545SDenis Semakin static int __send_secure_erase(struct clone_info *ci, struct dm_target *ti)
152300716545SDenis Semakin {
152400716545SDenis Semakin 	return __send_changing_extent_only(ci, ti, get_num_secure_erase_bios, NULL);
152500716545SDenis Semakin }
152600716545SDenis Semakin 
15273d7f4562SMike Snitzer static int __send_write_same(struct clone_info *ci, struct dm_target *ti)
152823508a96SMike Snitzer {
15293d7f4562SMike Snitzer 	return __send_changing_extent_only(ci, ti, get_num_write_same_bios, NULL);
153023508a96SMike Snitzer }
153123508a96SMike Snitzer 
15323d7f4562SMike Snitzer static int __send_write_zeroes(struct clone_info *ci, struct dm_target *ti)
1533ac62d620SChristoph Hellwig {
15343d7f4562SMike Snitzer 	return __send_changing_extent_only(ci, ti, get_num_write_zeroes_bios, NULL);
1535ac62d620SChristoph Hellwig }
1536ac62d620SChristoph Hellwig 
15370519c71eSMike Snitzer static bool __process_abnormal_io(struct clone_info *ci, struct dm_target *ti,
15380519c71eSMike Snitzer 				  int *result)
15390519c71eSMike Snitzer {
15400519c71eSMike Snitzer 	struct bio *bio = ci->bio;
15410519c71eSMike Snitzer 
15420519c71eSMike Snitzer 	if (bio_op(bio) == REQ_OP_DISCARD)
15430519c71eSMike Snitzer 		*result = __send_discard(ci, ti);
154400716545SDenis Semakin 	else if (bio_op(bio) == REQ_OP_SECURE_ERASE)
154500716545SDenis Semakin 		*result = __send_secure_erase(ci, ti);
15460519c71eSMike Snitzer 	else if (bio_op(bio) == REQ_OP_WRITE_SAME)
15470519c71eSMike Snitzer 		*result = __send_write_same(ci, ti);
15480519c71eSMike Snitzer 	else if (bio_op(bio) == REQ_OP_WRITE_ZEROES)
15490519c71eSMike Snitzer 		*result = __send_write_zeroes(ci, ti);
15500519c71eSMike Snitzer 	else
15510519c71eSMike Snitzer 		return false;
15520519c71eSMike Snitzer 
15530519c71eSMike Snitzer 	return true;
15540519c71eSMike Snitzer }
15550519c71eSMike Snitzer 
1556e4c93811SAlasdair G Kergon /*
1557e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1558e4c93811SAlasdair G Kergon  */
1559e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci)
1560e4c93811SAlasdair G Kergon {
1561e4c93811SAlasdair G Kergon 	struct dm_target *ti;
15621c3b13e6SKent Overstreet 	unsigned len;
1563c80914e8SMike Snitzer 	int r;
1564e4c93811SAlasdair G Kergon 
1565e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1566e4c93811SAlasdair G Kergon 	if (!dm_target_is_valid(ti))
1567e4c93811SAlasdair G Kergon 		return -EIO;
1568e4c93811SAlasdair G Kergon 
15690519c71eSMike Snitzer 	if (unlikely(__process_abnormal_io(ci, ti, &r)))
15700519c71eSMike Snitzer 		return r;
15713d7f4562SMike Snitzer 
1572e76239a3SChristoph Hellwig 	len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
1573e4c93811SAlasdair G Kergon 
1574c80914e8SMike Snitzer 	r = __clone_and_map_data_bio(ci, ti, ci->sector, &len);
1575c80914e8SMike Snitzer 	if (r < 0)
1576c80914e8SMike Snitzer 		return r;
1577e4c93811SAlasdair G Kergon 
1578e4c93811SAlasdair G Kergon 	ci->sector += len;
1579e4c93811SAlasdair G Kergon 	ci->sector_count -= len;
1580e4c93811SAlasdair G Kergon 
1581e4c93811SAlasdair G Kergon 	return 0;
1582e4c93811SAlasdair G Kergon }
1583e4c93811SAlasdair G Kergon 
1584978e51baSMike Snitzer static void init_clone_info(struct clone_info *ci, struct mapped_device *md,
1585978e51baSMike Snitzer 			    struct dm_table *map, struct bio *bio)
1586978e51baSMike Snitzer {
1587978e51baSMike Snitzer 	ci->map = map;
1588978e51baSMike Snitzer 	ci->io = alloc_io(md, bio);
1589978e51baSMike Snitzer 	ci->sector = bio->bi_iter.bi_sector;
1590978e51baSMike Snitzer }
1591978e51baSMike Snitzer 
1592e4c93811SAlasdair G Kergon /*
159314fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
15941da177e4SLinus Torvalds  */
1595978e51baSMike Snitzer static blk_qc_t __split_and_process_bio(struct mapped_device *md,
159683d5e5b0SMikulas Patocka 					struct dm_table *map, struct bio *bio)
15971da177e4SLinus Torvalds {
15981da177e4SLinus Torvalds 	struct clone_info ci;
1599978e51baSMike Snitzer 	blk_qc_t ret = BLK_QC_T_NONE;
1600512875bdSJun'ichi Nomura 	int error = 0;
16011da177e4SLinus Torvalds 
160283d5e5b0SMikulas Patocka 	if (unlikely(!map)) {
1603f0b9a450SMikulas Patocka 		bio_io_error(bio);
1604978e51baSMike Snitzer 		return ret;
1605f0b9a450SMikulas Patocka 	}
1606692d0eb9SMikulas Patocka 
1607978e51baSMike Snitzer 	init_clone_info(&ci, md, map, bio);
1608bd2a49b8SAlasdair G Kergon 
16091eff9d32SJens Axboe 	if (bio->bi_opf & REQ_PREFLUSH) {
1610bc02cdbeSMike Snitzer 		ci.bio = &ci.io->md->flush_bio;
1611b372d360SMike Snitzer 		ci.sector_count = 0;
161214fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1613b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1614a4aa5e56SDamien Le Moal 	} else if (bio_op(bio) == REQ_OP_ZONE_RESET) {
1615a4aa5e56SDamien Le Moal 		ci.bio = bio;
1616a4aa5e56SDamien Le Moal 		ci.sector_count = 0;
1617a4aa5e56SDamien Le Moal 		error = __split_and_process_non_flush(&ci);
1618b372d360SMike Snitzer 	} else {
16196a8736d1STejun Heo 		ci.bio = bio;
16201da177e4SLinus Torvalds 		ci.sector_count = bio_sectors(bio);
162118a25da8SNeilBrown 		while (ci.sector_count && !error) {
162214fe594dSAlasdair G Kergon 			error = __split_and_process_non_flush(&ci);
162318a25da8SNeilBrown 			if (current->bio_list && ci.sector_count && !error) {
162418a25da8SNeilBrown 				/*
162518a25da8SNeilBrown 				 * Remainder must be passed to generic_make_request()
162618a25da8SNeilBrown 				 * so that it gets handled *after* bios already submitted
162718a25da8SNeilBrown 				 * have been completely processed.
162818a25da8SNeilBrown 				 * We take a clone of the original to store in
1629745dc570SMike Snitzer 				 * ci.io->orig_bio to be used by end_io_acct() and
163018a25da8SNeilBrown 				 * for dec_pending to use for completion handling.
163118a25da8SNeilBrown 				 */
1632f21c601aSMike Snitzer 				struct bio *b = bio_split(bio, bio_sectors(bio) - ci.sector_count,
1633f21c601aSMike Snitzer 							  GFP_NOIO, &md->queue->bio_split);
1634745dc570SMike Snitzer 				ci.io->orig_bio = b;
163518a25da8SNeilBrown 				bio_chain(b, bio);
1636978e51baSMike Snitzer 				ret = generic_make_request(bio);
163718a25da8SNeilBrown 				break;
163818a25da8SNeilBrown 			}
163918a25da8SNeilBrown 		}
1640d87f4c14STejun Heo 	}
16411da177e4SLinus Torvalds 
16421da177e4SLinus Torvalds 	/* drop the extra reference count */
164354385bf7SBart Van Assche 	dec_pending(ci.io, errno_to_blk_status(error));
1644978e51baSMike Snitzer 	return ret;
16451da177e4SLinus Torvalds }
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds /*
1648978e51baSMike Snitzer  * Optimized variant of __split_and_process_bio that leverages the
1649978e51baSMike Snitzer  * fact that targets that use it do _not_ have a need to split bios.
16501da177e4SLinus Torvalds  */
1651978e51baSMike Snitzer static blk_qc_t __process_bio(struct mapped_device *md,
1652978e51baSMike Snitzer 			      struct dm_table *map, struct bio *bio)
16531da177e4SLinus Torvalds {
1654978e51baSMike Snitzer 	struct clone_info ci;
1655978e51baSMike Snitzer 	blk_qc_t ret = BLK_QC_T_NONE;
1656978e51baSMike Snitzer 	int error = 0;
1657978e51baSMike Snitzer 
1658978e51baSMike Snitzer 	if (unlikely(!map)) {
1659978e51baSMike Snitzer 		bio_io_error(bio);
1660978e51baSMike Snitzer 		return ret;
1661978e51baSMike Snitzer 	}
1662978e51baSMike Snitzer 
1663978e51baSMike Snitzer 	init_clone_info(&ci, md, map, bio);
1664978e51baSMike Snitzer 
1665978e51baSMike Snitzer 	if (bio->bi_opf & REQ_PREFLUSH) {
1666978e51baSMike Snitzer 		ci.bio = &ci.io->md->flush_bio;
1667978e51baSMike Snitzer 		ci.sector_count = 0;
1668978e51baSMike Snitzer 		error = __send_empty_flush(&ci);
1669978e51baSMike Snitzer 		/* dec_pending submits any data associated with flush */
1670978e51baSMike Snitzer 	} else {
1671978e51baSMike Snitzer 		struct dm_target *ti = md->immutable_target;
1672978e51baSMike Snitzer 		struct dm_target_io *tio;
1673978e51baSMike Snitzer 
1674978e51baSMike Snitzer 		/*
1675978e51baSMike Snitzer 		 * Defend against IO still getting in during teardown
1676978e51baSMike Snitzer 		 * - as was seen for a time with nvme-fcloop
1677978e51baSMike Snitzer 		 */
1678bab5d988SIgor Stoppa 		if (WARN_ON_ONCE(!ti || !dm_target_is_valid(ti))) {
1679978e51baSMike Snitzer 			error = -EIO;
1680978e51baSMike Snitzer 			goto out;
1681978e51baSMike Snitzer 		}
1682978e51baSMike Snitzer 
1683978e51baSMike Snitzer 		ci.bio = bio;
1684978e51baSMike Snitzer 		ci.sector_count = bio_sectors(bio);
16850519c71eSMike Snitzer 		if (unlikely(__process_abnormal_io(&ci, ti, &error)))
16860519c71eSMike Snitzer 			goto out;
16870519c71eSMike Snitzer 
16880519c71eSMike Snitzer 		tio = alloc_tio(&ci, ti, 0, GFP_NOIO);
1689978e51baSMike Snitzer 		ret = __clone_and_map_simple_bio(&ci, tio, NULL);
1690978e51baSMike Snitzer 	}
1691978e51baSMike Snitzer out:
1692978e51baSMike Snitzer 	/* drop the extra reference count */
1693978e51baSMike Snitzer 	dec_pending(ci.io, errno_to_blk_status(error));
1694978e51baSMike Snitzer 	return ret;
1695978e51baSMike Snitzer }
1696978e51baSMike Snitzer 
1697978e51baSMike Snitzer typedef blk_qc_t (process_bio_fn)(struct mapped_device *, struct dm_table *, struct bio *);
1698978e51baSMike Snitzer 
1699978e51baSMike Snitzer static blk_qc_t __dm_make_request(struct request_queue *q, struct bio *bio,
1700978e51baSMike Snitzer 				  process_bio_fn process_bio)
17011da177e4SLinus Torvalds {
17021da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
1703978e51baSMike Snitzer 	blk_qc_t ret = BLK_QC_T_NONE;
170483d5e5b0SMikulas Patocka 	int srcu_idx;
170583d5e5b0SMikulas Patocka 	struct dm_table *map;
17061da177e4SLinus Torvalds 
170783d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
17081da177e4SLinus Torvalds 
17096a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
17106a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
171183d5e5b0SMikulas Patocka 		dm_put_live_table(md, srcu_idx);
17121da177e4SLinus Torvalds 
17131eff9d32SJens Axboe 		if (!(bio->bi_opf & REQ_RAHEAD))
171492c63902SMikulas Patocka 			queue_io(md, bio);
17156a8736d1STejun Heo 		else
17166a8736d1STejun Heo 			bio_io_error(bio);
1717978e51baSMike Snitzer 		return ret;
17181da177e4SLinus Torvalds 	}
17191da177e4SLinus Torvalds 
1720978e51baSMike Snitzer 	ret = process_bio(md, map, bio);
1721978e51baSMike Snitzer 
172283d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
1723978e51baSMike Snitzer 	return ret;
1724978e51baSMike Snitzer }
1725978e51baSMike Snitzer 
1726978e51baSMike Snitzer /*
1727978e51baSMike Snitzer  * The request function that remaps the bio to one target and
1728978e51baSMike Snitzer  * splits off any remainder.
1729978e51baSMike Snitzer  */
1730978e51baSMike Snitzer static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
1731978e51baSMike Snitzer {
1732978e51baSMike Snitzer 	return __dm_make_request(q, bio, __split_and_process_bio);
1733978e51baSMike Snitzer }
1734978e51baSMike Snitzer 
1735978e51baSMike Snitzer static blk_qc_t dm_make_request_nvme(struct request_queue *q, struct bio *bio)
1736978e51baSMike Snitzer {
1737978e51baSMike Snitzer 	return __dm_make_request(q, bio, __process_bio);
1738cec47e3dSKiyoshi Ueda }
1739cec47e3dSKiyoshi Ueda 
17401da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
17411da177e4SLinus Torvalds {
17428a57dfc6SChandra Seetharaman 	int r = bdi_bits;
17438a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
17448a57dfc6SChandra Seetharaman 	struct dm_table *map;
17451da177e4SLinus Torvalds 
17461eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
1747e522c039SMike Snitzer 		if (dm_request_based(md)) {
1748cec47e3dSKiyoshi Ueda 			/*
1749e522c039SMike Snitzer 			 * With request-based DM we only need to check the
1750e522c039SMike Snitzer 			 * top-level queue for congestion.
1751cec47e3dSKiyoshi Ueda 			 */
1752dc3b17ccSJan Kara 			r = md->queue->backing_dev_info->wb.state & bdi_bits;
1753e522c039SMike Snitzer 		} else {
1754e522c039SMike Snitzer 			map = dm_get_live_table_fast(md);
1755e522c039SMike Snitzer 			if (map)
17561da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
175783d5e5b0SMikulas Patocka 			dm_put_live_table_fast(md);
17588a57dfc6SChandra Seetharaman 		}
1759e522c039SMike Snitzer 	}
17608a57dfc6SChandra Seetharaman 
17611da177e4SLinus Torvalds 	return r;
17621da177e4SLinus Torvalds }
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds /*-----------------------------------------------------------------
17651da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
17661da177e4SLinus Torvalds  *---------------------------------------------------------------*/
17672b06cfffSAlasdair G Kergon static void free_minor(int minor)
17681da177e4SLinus Torvalds {
1769f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17701da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1771f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
17721da177e4SLinus Torvalds }
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds /*
17751da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
17761da177e4SLinus Torvalds  */
1777cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
17781da177e4SLinus Torvalds {
1779c9d76be6STejun Heo 	int r;
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
17821da177e4SLinus Torvalds 		return -EINVAL;
17831da177e4SLinus Torvalds 
1784c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1785f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17861da177e4SLinus Torvalds 
1787c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
17881da177e4SLinus Torvalds 
1789f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1790c9d76be6STejun Heo 	idr_preload_end();
1791c9d76be6STejun Heo 	if (r < 0)
1792c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
1793c9d76be6STejun Heo 	return 0;
17941da177e4SLinus Torvalds }
17951da177e4SLinus Torvalds 
1796cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
17971da177e4SLinus Torvalds {
1798c9d76be6STejun Heo 	int r;
17991da177e4SLinus Torvalds 
1800c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1801f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
18021da177e4SLinus Torvalds 
1803c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
18041da177e4SLinus Torvalds 
1805f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1806c9d76be6STejun Heo 	idr_preload_end();
1807c9d76be6STejun Heo 	if (r < 0)
18081da177e4SLinus Torvalds 		return r;
1809c9d76be6STejun Heo 	*minor = r;
1810c9d76be6STejun Heo 	return 0;
18111da177e4SLinus Torvalds }
18121da177e4SLinus Torvalds 
181383d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
1814f26c5719SDan Williams static const struct dax_operations dm_dax_ops;
18151da177e4SLinus Torvalds 
181653d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
181753d5914fSMikulas Patocka 
1818c12c9a3cSMike Snitzer static void dm_init_normal_md_queue(struct mapped_device *md)
1819bfebd1cdSMike Snitzer {
1820bfebd1cdSMike Snitzer 	/*
1821bfebd1cdSMike Snitzer 	 * Initialize aspects of queue that aren't relevant for blk-mq
1822bfebd1cdSMike Snitzer 	 */
1823dc3b17ccSJan Kara 	md->queue->backing_dev_info->congested_fn = dm_any_congested;
18244a0b4ddfSMike Snitzer }
18254a0b4ddfSMike Snitzer 
18260f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md)
18270f20972fSMike Snitzer {
18280f20972fSMike Snitzer 	if (md->wq)
18290f20972fSMike Snitzer 		destroy_workqueue(md->wq);
18306f1c819cSKent Overstreet 	bioset_exit(&md->bs);
18316f1c819cSKent Overstreet 	bioset_exit(&md->io_bs);
18320f20972fSMike Snitzer 
1833f26c5719SDan Williams 	if (md->dax_dev) {
1834f26c5719SDan Williams 		kill_dax(md->dax_dev);
1835f26c5719SDan Williams 		put_dax(md->dax_dev);
1836f26c5719SDan Williams 		md->dax_dev = NULL;
1837f26c5719SDan Williams 	}
1838f26c5719SDan Williams 
18390f20972fSMike Snitzer 	if (md->disk) {
18400f20972fSMike Snitzer 		spin_lock(&_minor_lock);
18410f20972fSMike Snitzer 		md->disk->private_data = NULL;
18420f20972fSMike Snitzer 		spin_unlock(&_minor_lock);
18430f20972fSMike Snitzer 		del_gendisk(md->disk);
18440f20972fSMike Snitzer 		put_disk(md->disk);
18450f20972fSMike Snitzer 	}
18460f20972fSMike Snitzer 
18470f20972fSMike Snitzer 	if (md->queue)
18480f20972fSMike Snitzer 		blk_cleanup_queue(md->queue);
18490f20972fSMike Snitzer 
1850d09960b0STahsin Erdogan 	cleanup_srcu_struct(&md->io_barrier);
1851d09960b0STahsin Erdogan 
18520f20972fSMike Snitzer 	if (md->bdev) {
18530f20972fSMike Snitzer 		bdput(md->bdev);
18540f20972fSMike Snitzer 		md->bdev = NULL;
18550f20972fSMike Snitzer 	}
18564cc96131SMike Snitzer 
1857d5ffebddSMike Snitzer 	mutex_destroy(&md->suspend_lock);
1858d5ffebddSMike Snitzer 	mutex_destroy(&md->type_lock);
1859d5ffebddSMike Snitzer 	mutex_destroy(&md->table_devices_lock);
1860d5ffebddSMike Snitzer 
18614cc96131SMike Snitzer 	dm_mq_cleanup_mapped_device(md);
18620f20972fSMike Snitzer }
18630f20972fSMike Snitzer 
18641da177e4SLinus Torvalds /*
18651da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
18661da177e4SLinus Torvalds  */
18672b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
18681da177e4SLinus Torvalds {
1869115485e8SMike Snitzer 	int r, numa_node_id = dm_get_numa_node();
1870976431b0SDan Williams 	struct dax_device *dax_dev = NULL;
1871115485e8SMike Snitzer 	struct mapped_device *md;
1872ba61fdd1SJeff Mahoney 	void *old_md;
18731da177e4SLinus Torvalds 
1874856eb091SMikulas Patocka 	md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
18751da177e4SLinus Torvalds 	if (!md) {
18761da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
18771da177e4SLinus Torvalds 		return NULL;
18781da177e4SLinus Torvalds 	}
18791da177e4SLinus Torvalds 
188010da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
18816ed7ade8SMilan Broz 		goto bad_module_get;
188210da4f79SJeff Mahoney 
18831da177e4SLinus Torvalds 	/* get a minor number for the dev */
18842b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1885cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
18862b06cfffSAlasdair G Kergon 	else
1887cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
18881da177e4SLinus Torvalds 	if (r < 0)
18896ed7ade8SMilan Broz 		goto bad_minor;
18901da177e4SLinus Torvalds 
189183d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
189283d5e5b0SMikulas Patocka 	if (r < 0)
189383d5e5b0SMikulas Patocka 		goto bad_io_barrier;
189483d5e5b0SMikulas Patocka 
1895115485e8SMike Snitzer 	md->numa_node_id = numa_node_id;
1896591ddcfcSMike Snitzer 	md->init_tio_pdu = false;
1897a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
1898e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1899a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
190086f1152bSBenjamin Marzinski 	mutex_init(&md->table_devices_lock);
1901022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
19021da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
19035c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
19041da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
19057a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
19067a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
190786f1152bSBenjamin Marzinski 	INIT_LIST_HEAD(&md->table_devices);
19087a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
19091da177e4SLinus Torvalds 
19106d469642SChristoph Hellwig 	md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id);
19111da177e4SLinus Torvalds 	if (!md->queue)
19120f20972fSMike Snitzer 		goto bad;
1913c12c9a3cSMike Snitzer 	md->queue->queuedata = md;
1914c12c9a3cSMike Snitzer 	md->queue->backing_dev_info->congested_data = md;
19151da177e4SLinus Torvalds 
1916c12c9a3cSMike Snitzer 	md->disk = alloc_disk_node(1, md->numa_node_id);
19171da177e4SLinus Torvalds 	if (!md->disk)
19180f20972fSMike Snitzer 		goto bad;
19191da177e4SLinus Torvalds 
1920316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
1921316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
1922f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
192353d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1924f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
19252995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
1926f0b04115SJeff Mahoney 
19271da177e4SLinus Torvalds 	md->disk->major = _major;
19281da177e4SLinus Torvalds 	md->disk->first_minor = minor;
19291da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
19301da177e4SLinus Torvalds 	md->disk->queue = md->queue;
19311da177e4SLinus Torvalds 	md->disk->private_data = md;
19321da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
1933f26c5719SDan Williams 
1934976431b0SDan Williams 	if (IS_ENABLED(CONFIG_DAX_DRIVER)) {
1935f26c5719SDan Williams 		dax_dev = alloc_dax(md, md->disk->disk_name, &dm_dax_ops);
1936f26c5719SDan Williams 		if (!dax_dev)
1937f26c5719SDan Williams 			goto bad;
1938976431b0SDan Williams 	}
1939f26c5719SDan Williams 	md->dax_dev = dax_dev;
1940f26c5719SDan Williams 
1941c100ec49SMike Snitzer 	add_disk_no_queue_reg(md->disk);
19427e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
19431da177e4SLinus Torvalds 
1944670368a8STejun Heo 	md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
1945304f3f6aSMilan Broz 	if (!md->wq)
19460f20972fSMike Snitzer 		goto bad;
1947304f3f6aSMilan Broz 
194832a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
194932a926daSMikulas Patocka 	if (!md->bdev)
19500f20972fSMike Snitzer 		goto bad;
195132a926daSMikulas Patocka 
19523a83f467SMing Lei 	bio_init(&md->flush_bio, NULL, 0);
1953ff0361b3SJan Kara 	md->flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
19546a8736d1STejun Heo 
1955fd2ed4d2SMikulas Patocka 	dm_stats_init(&md->stats);
1956fd2ed4d2SMikulas Patocka 
1957ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1958f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1959ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1960f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1961ba61fdd1SJeff Mahoney 
1962ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1963ba61fdd1SJeff Mahoney 
19641da177e4SLinus Torvalds 	return md;
19651da177e4SLinus Torvalds 
19660f20972fSMike Snitzer bad:
19670f20972fSMike Snitzer 	cleanup_mapped_device(md);
196883d5e5b0SMikulas Patocka bad_io_barrier:
19691da177e4SLinus Torvalds 	free_minor(minor);
19706ed7ade8SMilan Broz bad_minor:
197110da4f79SJeff Mahoney 	module_put(THIS_MODULE);
19726ed7ade8SMilan Broz bad_module_get:
1973856eb091SMikulas Patocka 	kvfree(md);
19741da177e4SLinus Torvalds 	return NULL;
19751da177e4SLinus Torvalds }
19761da177e4SLinus Torvalds 
1977ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1978ae9da83fSJun'ichi Nomura 
19791da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
19801da177e4SLinus Torvalds {
1981f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
198263d94e48SJun'ichi Nomura 
1983ae9da83fSJun'ichi Nomura 	unlock_fs(md);
19842eb6e1e3SKeith Busch 
19850f20972fSMike Snitzer 	cleanup_mapped_device(md);
19860f20972fSMike Snitzer 
19870f20972fSMike Snitzer 	free_table_devices(&md->table_devices);
19880f20972fSMike Snitzer 	dm_stats_cleanup(&md->stats);
198963a4f065SMike Snitzer 	free_minor(minor);
199063a4f065SMike Snitzer 
199110da4f79SJeff Mahoney 	module_put(THIS_MODULE);
1992856eb091SMikulas Patocka 	kvfree(md);
19931da177e4SLinus Torvalds }
19941da177e4SLinus Torvalds 
19952a2a4c51SJens Axboe static int __bind_mempools(struct mapped_device *md, struct dm_table *t)
1996e6ee8c0bSKiyoshi Ueda {
1997c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
19982a2a4c51SJens Axboe 	int ret = 0;
1999e6ee8c0bSKiyoshi Ueda 
2000545ed20eSToshi Kani 	if (dm_table_bio_based(t)) {
2001c0820cf5SMikulas Patocka 		/*
200264f52b0eSMike Snitzer 		 * The md may already have mempools that need changing.
200364f52b0eSMike Snitzer 		 * If so, reload bioset because front_pad may have changed
200416245bdcSJun'ichi Nomura 		 * because a different table was loaded.
2005c0820cf5SMikulas Patocka 		 */
20066f1c819cSKent Overstreet 		bioset_exit(&md->bs);
20076f1c819cSKent Overstreet 		bioset_exit(&md->io_bs);
20080776aa0eSMike Snitzer 
20096f1c819cSKent Overstreet 	} else if (bioset_initialized(&md->bs)) {
2010cbc4e3c1SMike Snitzer 		/*
20114e6e36c3SMike Snitzer 		 * There's no need to reload with request-based dm
20124e6e36c3SMike Snitzer 		 * because the size of front_pad doesn't change.
20134e6e36c3SMike Snitzer 		 * Note for future: If you are to reload bioset,
20144e6e36c3SMike Snitzer 		 * prep-ed requests in the queue may refer
20154e6e36c3SMike Snitzer 		 * to bio from the old bioset, so you must walk
20164e6e36c3SMike Snitzer 		 * through the queue to unprep.
2017cbc4e3c1SMike Snitzer 		 */
2018cbc4e3c1SMike Snitzer 		goto out;
2019cbc4e3c1SMike Snitzer 	}
2020cbc4e3c1SMike Snitzer 
20216f1c819cSKent Overstreet 	BUG_ON(!p ||
20226f1c819cSKent Overstreet 	       bioset_initialized(&md->bs) ||
20236f1c819cSKent Overstreet 	       bioset_initialized(&md->io_bs));
2024e6ee8c0bSKiyoshi Ueda 
20252a2a4c51SJens Axboe 	ret = bioset_init_from_src(&md->bs, &p->bs);
20262a2a4c51SJens Axboe 	if (ret)
20272a2a4c51SJens Axboe 		goto out;
20282a2a4c51SJens Axboe 	ret = bioset_init_from_src(&md->io_bs, &p->io_bs);
20292a2a4c51SJens Axboe 	if (ret)
20302a2a4c51SJens Axboe 		bioset_exit(&md->bs);
2031e6ee8c0bSKiyoshi Ueda out:
203202233342SMike Snitzer 	/* mempool bind completed, no longer need any mempools in the table */
2033e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
20342a2a4c51SJens Axboe 	return ret;
2035e6ee8c0bSKiyoshi Ueda }
2036e6ee8c0bSKiyoshi Ueda 
20371da177e4SLinus Torvalds /*
20381da177e4SLinus Torvalds  * Bind a table to the device.
20391da177e4SLinus Torvalds  */
20401da177e4SLinus Torvalds static void event_callback(void *context)
20411da177e4SLinus Torvalds {
20427a8c3d3bSMike Anderson 	unsigned long flags;
20437a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
20441da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
20451da177e4SLinus Torvalds 
20467a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
20477a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
20487a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
20497a8c3d3bSMike Anderson 
2050ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
20517a8c3d3bSMike Anderson 
20521da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
20531da177e4SLinus Torvalds 	wake_up(&md->eventq);
205462e08243SMikulas Patocka 	dm_issue_global_event();
20551da177e4SLinus Torvalds }
20561da177e4SLinus Torvalds 
2057c217649bSMike Snitzer /*
2058c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
2059c217649bSMike Snitzer  */
20604e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
20611da177e4SLinus Torvalds {
20621ea0654eSBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
20631ea0654eSBart Van Assche 
20644e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
20651da177e4SLinus Torvalds 
2066db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
20671da177e4SLinus Torvalds }
20681da177e4SLinus Torvalds 
2069042d2a9bSAlasdair G Kergon /*
2070042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2071042d2a9bSAlasdair G Kergon  */
2072042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2073754c5fc7SMike Snitzer 			       struct queue_limits *limits)
20741da177e4SLinus Torvalds {
2075042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
2076165125e1SJens Axboe 	struct request_queue *q = md->queue;
2077978e51baSMike Snitzer 	bool request_based = dm_table_request_based(t);
20781da177e4SLinus Torvalds 	sector_t size;
20792a2a4c51SJens Axboe 	int ret;
20801da177e4SLinus Torvalds 
20815a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
20825a8f1f80SBart Van Assche 
20831da177e4SLinus Torvalds 	size = dm_table_get_size(t);
20843ac51e74SDarrick J. Wong 
20853ac51e74SDarrick J. Wong 	/*
20863ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
20873ac51e74SDarrick J. Wong 	 */
2088fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
20893ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
20903ac51e74SDarrick J. Wong 
20914e90188bSAlasdair G Kergon 	__set_size(md, size);
20921da177e4SLinus Torvalds 
2093cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
20942ca3310eSAlasdair G Kergon 
2095e6ee8c0bSKiyoshi Ueda 	/*
2096e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
2097e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
2098e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
2099e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
2100e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
2101e6ee8c0bSKiyoshi Ueda 	 */
2102978e51baSMike Snitzer 	if (request_based)
2103eca7ee6dSMike Snitzer 		dm_stop_queue(q);
2104978e51baSMike Snitzer 
2105978e51baSMike Snitzer 	if (request_based || md->type == DM_TYPE_NVME_BIO_BASED) {
210616f12266SMike Snitzer 		/*
2107978e51baSMike Snitzer 		 * Leverage the fact that request-based DM targets and
2108978e51baSMike Snitzer 		 * NVMe bio based targets are immutable singletons
2109978e51baSMike Snitzer 		 * - used to optimize both dm_request_fn and dm_mq_queue_rq;
2110978e51baSMike Snitzer 		 *   and __process_bio.
211116f12266SMike Snitzer 		 */
211216f12266SMike Snitzer 		md->immutable_target = dm_table_get_immutable_target(t);
211316f12266SMike Snitzer 	}
2114e6ee8c0bSKiyoshi Ueda 
21152a2a4c51SJens Axboe 	ret = __bind_mempools(md, t);
21162a2a4c51SJens Axboe 	if (ret) {
21172a2a4c51SJens Axboe 		old_map = ERR_PTR(ret);
21182a2a4c51SJens Axboe 		goto out;
21192a2a4c51SJens Axboe 	}
2120e6ee8c0bSKiyoshi Ueda 
2121a12f5d48SEric Dumazet 	old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
21221d3aa6f6SMike Snitzer 	rcu_assign_pointer(md->map, (void *)t);
212336a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
212436a0456fSAlasdair G Kergon 
2125754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
212641abc4e1SHannes Reinecke 	if (old_map)
212783d5e5b0SMikulas Patocka 		dm_sync_table(md);
21282ca3310eSAlasdair G Kergon 
21292a2a4c51SJens Axboe out:
2130042d2a9bSAlasdair G Kergon 	return old_map;
21311da177e4SLinus Torvalds }
21321da177e4SLinus Torvalds 
2133a7940155SAlasdair G Kergon /*
2134a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2135a7940155SAlasdair G Kergon  */
2136a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
21371da177e4SLinus Torvalds {
2138a12f5d48SEric Dumazet 	struct dm_table *map = rcu_dereference_protected(md->map, 1);
21391da177e4SLinus Torvalds 
21401da177e4SLinus Torvalds 	if (!map)
2141a7940155SAlasdair G Kergon 		return NULL;
21421da177e4SLinus Torvalds 
21431da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
21449cdb8520SMonam Agarwal 	RCU_INIT_POINTER(md->map, NULL);
214583d5e5b0SMikulas Patocka 	dm_sync_table(md);
2146a7940155SAlasdair G Kergon 
2147a7940155SAlasdair G Kergon 	return map;
21481da177e4SLinus Torvalds }
21491da177e4SLinus Torvalds 
21501da177e4SLinus Torvalds /*
21511da177e4SLinus Torvalds  * Constructor for a new device.
21521da177e4SLinus Torvalds  */
21532b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
21541da177e4SLinus Torvalds {
2155c12c9a3cSMike Snitzer 	int r;
21561da177e4SLinus Torvalds 	struct mapped_device *md;
21571da177e4SLinus Torvalds 
21582b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
21591da177e4SLinus Torvalds 	if (!md)
21601da177e4SLinus Torvalds 		return -ENXIO;
21611da177e4SLinus Torvalds 
2162c12c9a3cSMike Snitzer 	r = dm_sysfs_init(md);
2163c12c9a3cSMike Snitzer 	if (r) {
2164c12c9a3cSMike Snitzer 		free_dev(md);
2165c12c9a3cSMike Snitzer 		return r;
2166c12c9a3cSMike Snitzer 	}
2167784aae73SMilan Broz 
21681da177e4SLinus Torvalds 	*result = md;
21691da177e4SLinus Torvalds 	return 0;
21701da177e4SLinus Torvalds }
21711da177e4SLinus Torvalds 
2172a5664dadSMike Snitzer /*
2173a5664dadSMike Snitzer  * Functions to manage md->type.
2174a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2175a5664dadSMike Snitzer  */
2176a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2177a5664dadSMike Snitzer {
2178a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2179a5664dadSMike Snitzer }
2180a5664dadSMike Snitzer 
2181a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2182a5664dadSMike Snitzer {
2183a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2184a5664dadSMike Snitzer }
2185a5664dadSMike Snitzer 
21867e0d574fSBart Van Assche void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
2187a5664dadSMike Snitzer {
218800c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2189a5664dadSMike Snitzer 	md->type = type;
2190a5664dadSMike Snitzer }
2191a5664dadSMike Snitzer 
21927e0d574fSBart Van Assche enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
2193a5664dadSMike Snitzer {
2194a5664dadSMike Snitzer 	return md->type;
2195a5664dadSMike Snitzer }
2196a5664dadSMike Snitzer 
219736a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
219836a0456fSAlasdair G Kergon {
219936a0456fSAlasdair G Kergon 	return md->immutable_target_type;
220036a0456fSAlasdair G Kergon }
220136a0456fSAlasdair G Kergon 
22024a0b4ddfSMike Snitzer /*
2203f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
2204f84cb8a4SMike Snitzer  * count on 'md'.
2205f84cb8a4SMike Snitzer  */
2206f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2207f84cb8a4SMike Snitzer {
2208f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
2209f84cb8a4SMike Snitzer 	return &md->queue->limits;
2210f84cb8a4SMike Snitzer }
2211f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2212f84cb8a4SMike Snitzer 
22134a0b4ddfSMike Snitzer /*
22144a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
22154a0b4ddfSMike Snitzer  */
2216591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
22174a0b4ddfSMike Snitzer {
2218bfebd1cdSMike Snitzer 	int r;
2219c100ec49SMike Snitzer 	struct queue_limits limits;
22207e0d574fSBart Van Assche 	enum dm_queue_mode type = dm_get_md_type(md);
2221bfebd1cdSMike Snitzer 
2222545ed20eSToshi Kani 	switch (type) {
2223bfebd1cdSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
2224e83068a5SMike Snitzer 		r = dm_mq_init_request_queue(md, t);
2225bfebd1cdSMike Snitzer 		if (r) {
2226eca7ee6dSMike Snitzer 			DMERR("Cannot initialize queue for request-based dm-mq mapped device");
2227bfebd1cdSMike Snitzer 			return r;
2228bfebd1cdSMike Snitzer 		}
2229bfebd1cdSMike Snitzer 		break;
2230bfebd1cdSMike Snitzer 	case DM_TYPE_BIO_BASED:
2231545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
2232eca7ee6dSMike Snitzer 		dm_init_normal_md_queue(md);
2233ff36ab34SMike Snitzer 		blk_queue_make_request(md->queue, dm_make_request);
2234bfebd1cdSMike Snitzer 		break;
2235978e51baSMike Snitzer 	case DM_TYPE_NVME_BIO_BASED:
2236978e51baSMike Snitzer 		dm_init_normal_md_queue(md);
2237978e51baSMike Snitzer 		blk_queue_make_request(md->queue, dm_make_request_nvme);
223873d410c0SMilan Broz 		break;
22397e0d574fSBart Van Assche 	case DM_TYPE_NONE:
22407e0d574fSBart Van Assche 		WARN_ON_ONCE(true);
22417e0d574fSBart Van Assche 		break;
22421da177e4SLinus Torvalds 	}
22431da177e4SLinus Torvalds 
2244c100ec49SMike Snitzer 	r = dm_calculate_queue_limits(t, &limits);
2245c100ec49SMike Snitzer 	if (r) {
2246c100ec49SMike Snitzer 		DMERR("Cannot calculate initial queue limits");
2247c100ec49SMike Snitzer 		return r;
2248c100ec49SMike Snitzer 	}
2249c100ec49SMike Snitzer 	dm_table_set_restrictions(t, md->queue, &limits);
2250c100ec49SMike Snitzer 	blk_register_queue(md->disk);
2251c100ec49SMike Snitzer 
22521da177e4SLinus Torvalds 	return 0;
22531da177e4SLinus Torvalds }
22541da177e4SLinus Torvalds 
22551da177e4SLinus Torvalds struct mapped_device *dm_get_md(dev_t dev)
22561da177e4SLinus Torvalds {
22571da177e4SLinus Torvalds 	struct mapped_device *md;
22581da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
22591da177e4SLinus Torvalds 
22601da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
22611da177e4SLinus Torvalds 		return NULL;
22621da177e4SLinus Torvalds 
22631da177e4SLinus Torvalds 	spin_lock(&_minor_lock);
22641da177e4SLinus Torvalds 
22651da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
226649de5769SMike Snitzer 	if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) ||
226749de5769SMike Snitzer 	    test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
22681da177e4SLinus Torvalds 		md = NULL;
22691da177e4SLinus Torvalds 		goto out;
22701da177e4SLinus Torvalds 	}
22711da177e4SLinus Torvalds 	dm_get(md);
22721da177e4SLinus Torvalds out:
2273f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
22741da177e4SLinus Torvalds 
22751da177e4SLinus Torvalds 	return md;
2276fba9f90eSJeff Mahoney }
2277fba9f90eSJeff Mahoney EXPORT_SYMBOL_GPL(dm_get_md);
2278fba9f90eSJeff Mahoney 
2279637842cfSDavid Teigland void *dm_get_mdptr(struct mapped_device *md)
2280fba9f90eSJeff Mahoney {
2281fba9f90eSJeff Mahoney 	return md->interface_ptr;
22821da177e4SLinus Torvalds }
2283fba9f90eSJeff Mahoney 
2284f32c10b0SJeff Mahoney void dm_set_mdptr(struct mapped_device *md, void *ptr)
22851da177e4SLinus Torvalds {
2286637842cfSDavid Teigland 	md->interface_ptr = ptr;
2287637842cfSDavid Teigland }
2288637842cfSDavid Teigland 
2289d229a958SDavid Teigland void dm_get(struct mapped_device *md)
2290d229a958SDavid Teigland {
2291d229a958SDavid Teigland 	atomic_inc(&md->holders);
2292d229a958SDavid Teigland 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
2293d229a958SDavid Teigland }
2294d229a958SDavid Teigland 
2295d229a958SDavid Teigland int dm_hold(struct mapped_device *md)
2296d229a958SDavid Teigland {
2297d229a958SDavid Teigland 	spin_lock(&_minor_lock);
2298d229a958SDavid Teigland 	if (test_bit(DMF_FREEING, &md->flags)) {
22999ade92a9SAlasdair G Kergon 		spin_unlock(&_minor_lock);
2300637842cfSDavid Teigland 		return -EBUSY;
23019ade92a9SAlasdair G Kergon 	}
23021da177e4SLinus Torvalds 	dm_get(md);
23031da177e4SLinus Torvalds 	spin_unlock(&_minor_lock);
23041da177e4SLinus Torvalds 	return 0;
23051da177e4SLinus Torvalds }
23061da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(dm_hold);
23071da177e4SLinus Torvalds 
23081da177e4SLinus Torvalds const char *dm_device_name(struct mapped_device *md)
23091da177e4SLinus Torvalds {
23101da177e4SLinus Torvalds 	return md->name;
23111da177e4SLinus Torvalds }
23121da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(dm_device_name);
23131da177e4SLinus Torvalds 
231472d94861SAlasdair G Kergon static void __dm_destroy(struct mapped_device *md, bool wait)
231572d94861SAlasdair G Kergon {
231672d94861SAlasdair G Kergon 	struct dm_table *map;
231772d94861SAlasdair G Kergon 	int srcu_idx;
231872d94861SAlasdair G Kergon 
231972d94861SAlasdair G Kergon 	might_sleep();
23201da177e4SLinus Torvalds 
23211da177e4SLinus Torvalds 	spin_lock(&_minor_lock);
23221da177e4SLinus Torvalds 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
23231da177e4SLinus Torvalds 	set_bit(DMF_FREEING, &md->flags);
23241da177e4SLinus Torvalds 	spin_unlock(&_minor_lock);
23251da177e4SLinus Torvalds 
2326c12c9a3cSMike Snitzer 	blk_set_queue_dying(md->queue);
23273b785fbcSBart Van Assche 
23281da177e4SLinus Torvalds 	/*
23291da177e4SLinus Torvalds 	 * Take suspend_lock so that presuspend and postsuspend methods
23301da177e4SLinus Torvalds 	 * do not race with internal suspend.
23311da177e4SLinus Torvalds 	 */
23321da177e4SLinus Torvalds 	mutex_lock(&md->suspend_lock);
23332a708cffSJunichi Nomura 	map = dm_get_live_table(md, &srcu_idx);
23341da177e4SLinus Torvalds 	if (!dm_suspended_md(md)) {
23351da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
23361da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
23371da177e4SLinus Torvalds 	}
23381da177e4SLinus Torvalds 	/* dm_put_live_table must be before msleep, otherwise deadlock is possible */
23391da177e4SLinus Torvalds 	dm_put_live_table(md, srcu_idx);
23402a708cffSJunichi Nomura 	mutex_unlock(&md->suspend_lock);
23411da177e4SLinus Torvalds 
23421da177e4SLinus Torvalds 	/*
23431da177e4SLinus Torvalds 	 * Rare, but there may be I/O requests still going to complete,
23441da177e4SLinus Torvalds 	 * for example.  Wait for all references to disappear.
23451da177e4SLinus Torvalds 	 * No one should increment the reference count of the mapped_device,
23461da177e4SLinus Torvalds 	 * after the mapped_device state becomes DMF_FREEING.
23471da177e4SLinus Torvalds 	 */
23481da177e4SLinus Torvalds 	if (wait)
23491da177e4SLinus Torvalds 		while (atomic_read(&md->holders))
23501da177e4SLinus Torvalds 			msleep(1);
23511da177e4SLinus Torvalds 	else if (atomic_read(&md->holders))
23521da177e4SLinus Torvalds 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
23531da177e4SLinus Torvalds 		       dm_device_name(md), atomic_read(&md->holders));
23541da177e4SLinus Torvalds 
23551da177e4SLinus Torvalds 	dm_sysfs_exit(md);
23561da177e4SLinus Torvalds 	dm_table_destroy(__unbind(md));
2357cf222b37SAlasdair G Kergon 	free_dev(md);
23581da177e4SLinus Torvalds }
23591da177e4SLinus Torvalds 
23601da177e4SLinus Torvalds void dm_destroy(struct mapped_device *md)
23611da177e4SLinus Torvalds {
23621da177e4SLinus Torvalds 	__dm_destroy(md, true);
23631da177e4SLinus Torvalds }
23641da177e4SLinus Torvalds 
23651da177e4SLinus Torvalds void dm_destroy_immediate(struct mapped_device *md)
23661da177e4SLinus Torvalds {
23671134e5aeSMike Anderson 	__dm_destroy(md, false);
23681da177e4SLinus Torvalds }
2369fba9f90eSJeff Mahoney 
2370fba9f90eSJeff Mahoney void dm_put(struct mapped_device *md)
2371f32c10b0SJeff Mahoney {
23721134e5aeSMike Anderson 	atomic_dec(&md->holders);
2373ba61fdd1SJeff Mahoney }
2374fba9f90eSJeff Mahoney EXPORT_SYMBOL_GPL(dm_put);
2375f32c10b0SJeff Mahoney 
2376b48633f8SBart Van Assche static int dm_wait_for_completion(struct mapped_device *md, long task_state)
23771da177e4SLinus Torvalds {
23781da177e4SLinus Torvalds 	int r = 0;
23799f4c3f87SBart Van Assche 	DEFINE_WAIT(wait);
23801da177e4SLinus Torvalds 
23811da177e4SLinus Torvalds 	while (1) {
23829f4c3f87SBart Van Assche 		prepare_to_wait(&md->wait, &wait, task_state);
23831da177e4SLinus Torvalds 
23841da177e4SLinus Torvalds 		if (!md_in_flight(md))
23851da177e4SLinus Torvalds 			break;
23861da177e4SLinus Torvalds 
2387e3fabdfdSBart Van Assche 		if (signal_pending_state(task_state, current)) {
23881da177e4SLinus Torvalds 			r = -EINTR;
23891da177e4SLinus Torvalds 			break;
23901da177e4SLinus Torvalds 		}
23911da177e4SLinus Torvalds 
23921da177e4SLinus Torvalds 		io_schedule();
23931da177e4SLinus Torvalds 	}
23949f4c3f87SBart Van Assche 	finish_wait(&md->wait, &wait);
23951da177e4SLinus Torvalds 
23961da177e4SLinus Torvalds 	return r;
23971da177e4SLinus Torvalds }
23981da177e4SLinus Torvalds 
23991da177e4SLinus Torvalds /*
240079eb885cSEdward Goggin  * Process the deferred bios
24011da177e4SLinus Torvalds  */
24021da177e4SLinus Torvalds static void dm_wq_work(struct work_struct *work)
24031da177e4SLinus Torvalds {
24041da177e4SLinus Torvalds 	struct mapped_device *md = container_of(work, struct mapped_device,
24056d6f10dfSMilan Broz 						work);
24061da177e4SLinus Torvalds 	struct bio *c;
24076d6f10dfSMilan Broz 	int srcu_idx;
24081da177e4SLinus Torvalds 	struct dm_table *map;
24096d6f10dfSMilan Broz 
24109e4e5f87SMilan Broz 	map = dm_get_live_table(md, &srcu_idx);
24119e4e5f87SMilan Broz 
24121da177e4SLinus Torvalds 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
24133b00b203SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
24143b00b203SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2415af7e466aSMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2416af7e466aSMikulas Patocka 
2417af7e466aSMikulas Patocka 		if (!c)
2418df12ee99SAlasdair G Kergon 			break;
24193b00b203SMikulas Patocka 
2420e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2421e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2422e39e2e95SAlasdair G Kergon 		else
242383d5e5b0SMikulas Patocka 			__split_and_process_bio(md, map, c);
2424e6ee8c0bSKiyoshi Ueda 	}
24251da177e4SLinus Torvalds 
242683d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
24271da177e4SLinus Torvalds }
24282ca3310eSAlasdair G Kergon 
24291da177e4SLinus Torvalds static void dm_queue_flush(struct mapped_device *md)
24301da177e4SLinus Torvalds {
2431cf222b37SAlasdair G Kergon 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
24324e857c58SPeter Zijlstra 	smp_mb__after_atomic();
24331da177e4SLinus Torvalds 	queue_work(md->wq, &md->work);
24341da177e4SLinus Torvalds }
24351da177e4SLinus Torvalds 
24361da177e4SLinus Torvalds /*
2437042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
24381da177e4SLinus Torvalds  */
2439042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
24401da177e4SLinus Torvalds {
244187eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2442754c5fc7SMike Snitzer 	struct queue_limits limits;
2443042d2a9bSAlasdair G Kergon 	int r;
24441da177e4SLinus Torvalds 
24451da177e4SLinus Torvalds 	mutex_lock(&md->suspend_lock);
24461da177e4SLinus Torvalds 
244793c534aeSAlasdair G Kergon 	/* device must be suspended */
24484f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
24491da177e4SLinus Torvalds 		goto out;
24501da177e4SLinus Torvalds 
24513ae70656SMike Snitzer 	/*
24523ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
24533ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
24543ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
24553ae70656SMike Snitzer 	 * reappear.
24563ae70656SMike Snitzer 	 */
24573ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
245883d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
24593ae70656SMike Snitzer 		if (live_map)
24603ae70656SMike Snitzer 			limits = md->queue->limits;
246183d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
24623ae70656SMike Snitzer 	}
24633ae70656SMike Snitzer 
246487eb5b21SMike Christie 	if (!live_map) {
2465754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2466042d2a9bSAlasdair G Kergon 		if (r) {
2467042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2468754c5fc7SMike Snitzer 			goto out;
2469042d2a9bSAlasdair G Kergon 		}
247087eb5b21SMike Christie 	}
2471754c5fc7SMike Snitzer 
2472042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
247362e08243SMikulas Patocka 	dm_issue_global_event();
24741da177e4SLinus Torvalds 
24751da177e4SLinus Torvalds out:
24761da177e4SLinus Torvalds 	mutex_unlock(&md->suspend_lock);
2477042d2a9bSAlasdair G Kergon 	return map;
24782ca3310eSAlasdair G Kergon }
24791da177e4SLinus Torvalds 
2480cf222b37SAlasdair G Kergon /*
2481dfbe03f6SAlasdair G Kergon  * Functions to lock and unlock any filesystem running on the
2482d1782a3bSAlasdair G Kergon  * device.
2483d1782a3bSAlasdair G Kergon  */
24842ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
2485dfbe03f6SAlasdair G Kergon {
24861da177e4SLinus Torvalds 	int r;
24871da177e4SLinus Torvalds 
24881da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2489dfbe03f6SAlasdair G Kergon 
2490db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2491dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2492cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2493e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2494e39e2e95SAlasdair G Kergon 		return r;
2495dfbe03f6SAlasdair G Kergon 	}
2496dfbe03f6SAlasdair G Kergon 
2497aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2498aa8d7c2fSAlasdair G Kergon 
24991da177e4SLinus Torvalds 	return 0;
25001da177e4SLinus Torvalds }
25011da177e4SLinus Torvalds 
25022ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
25031da177e4SLinus Torvalds {
2504aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2505aa8d7c2fSAlasdair G Kergon 		return;
2506aa8d7c2fSAlasdair G Kergon 
2507db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
25081da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2509aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
25101da177e4SLinus Torvalds }
25111da177e4SLinus Torvalds 
25121da177e4SLinus Torvalds /*
2513b48633f8SBart Van Assche  * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
2514b48633f8SBart Van Assche  * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
2515b48633f8SBart Van Assche  * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
2516b48633f8SBart Van Assche  *
2517ffcc3936SMike Snitzer  * If __dm_suspend returns 0, the device is completely quiescent
2518ffcc3936SMike Snitzer  * now. There is no request-processing activity. All new requests
2519ffcc3936SMike Snitzer  * are being added to md->deferred list.
2520cec47e3dSKiyoshi Ueda  */
2521ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
2522b48633f8SBart Van Assche 			unsigned suspend_flags, long task_state,
2523eaf9a736SMike Snitzer 			int dmf_suspended_flag)
25241da177e4SLinus Torvalds {
2525ffcc3936SMike Snitzer 	bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2526ffcc3936SMike Snitzer 	bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2527ffcc3936SMike Snitzer 	int r;
2528cf222b37SAlasdair G Kergon 
25295a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
25305a8f1f80SBart Van Assche 
25312e93ccc1SKiyoshi Ueda 	/*
25322e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
25332e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
25342e93ccc1SKiyoshi Ueda 	 */
25352e93ccc1SKiyoshi Ueda 	if (noflush)
25362e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
253786331f39SBart Van Assche 	else
253886331f39SBart Van Assche 		pr_debug("%s: suspending with flush\n", dm_device_name(md));
25392e93ccc1SKiyoshi Ueda 
2540d67ee213SMike Snitzer 	/*
2541d67ee213SMike Snitzer 	 * This gets reverted if there's an error later and the targets
2542d67ee213SMike Snitzer 	 * provide the .presuspend_undo hook.
2543d67ee213SMike Snitzer 	 */
25441da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
25451da177e4SLinus Torvalds 
25462e93ccc1SKiyoshi Ueda 	/*
25479f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
25489f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
25499f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
25509f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
25512e93ccc1SKiyoshi Ueda 	 */
255232a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
25532ca3310eSAlasdair G Kergon 		r = lock_fs(md);
2554d67ee213SMike Snitzer 		if (r) {
2555d67ee213SMike Snitzer 			dm_table_presuspend_undo_targets(map);
2556ffcc3936SMike Snitzer 			return r;
2557aa8d7c2fSAlasdair G Kergon 		}
2558d67ee213SMike Snitzer 	}
25591da177e4SLinus Torvalds 
25601da177e4SLinus Torvalds 	/*
25613b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
25623b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
25633b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
25643b00b203SMikulas Patocka 	 * dm_wq_work.
25653b00b203SMikulas Patocka 	 *
25663b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
25673b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
25686a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
25696a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
25706a8736d1STejun Heo 	 * flush_workqueue(md->wq).
25711da177e4SLinus Torvalds 	 */
25721eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
257341abc4e1SHannes Reinecke 	if (map)
257483d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
25751da177e4SLinus Torvalds 
2576d0bcb878SKiyoshi Ueda 	/*
257729e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
257829e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2579d0bcb878SKiyoshi Ueda 	 */
25806a23e05cSJens Axboe 	if (dm_request_based(md))
2581eca7ee6dSMike Snitzer 		dm_stop_queue(md->queue);
2582cec47e3dSKiyoshi Ueda 
2583d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2584d0bcb878SKiyoshi Ueda 
25851da177e4SLinus Torvalds 	/*
25863b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
25873b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
25883b00b203SMikulas Patocka 	 * to finish.
25891da177e4SLinus Torvalds 	 */
2590b48633f8SBart Van Assche 	r = dm_wait_for_completion(md, task_state);
2591eaf9a736SMike Snitzer 	if (!r)
2592eaf9a736SMike Snitzer 		set_bit(dmf_suspended_flag, &md->flags);
25931da177e4SLinus Torvalds 
25946d6f10dfSMilan Broz 	if (noflush)
2595022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
259641abc4e1SHannes Reinecke 	if (map)
259783d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
25982e93ccc1SKiyoshi Ueda 
25991da177e4SLinus Torvalds 	/* were we interrupted ? */
260046125c1cSMilan Broz 	if (r < 0) {
26019a1fb464SMikulas Patocka 		dm_queue_flush(md);
260273d410c0SMilan Broz 
2603cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
2604eca7ee6dSMike Snitzer 			dm_start_queue(md->queue);
2605cec47e3dSKiyoshi Ueda 
26062ca3310eSAlasdair G Kergon 		unlock_fs(md);
2607d67ee213SMike Snitzer 		dm_table_presuspend_undo_targets(map);
2608ffcc3936SMike Snitzer 		/* pushback list is already flushed, so skip flush */
2609ffcc3936SMike Snitzer 	}
2610ffcc3936SMike Snitzer 
2611ffcc3936SMike Snitzer 	return r;
26122ca3310eSAlasdair G Kergon }
26132ca3310eSAlasdair G Kergon 
26143b00b203SMikulas Patocka /*
2615ffcc3936SMike Snitzer  * We need to be able to change a mapping table under a mounted
2616ffcc3936SMike Snitzer  * filesystem.  For example we might want to move some data in
2617ffcc3936SMike Snitzer  * the background.  Before the table can be swapped with
2618ffcc3936SMike Snitzer  * dm_bind_table, dm_suspend must be called to flush any in
2619ffcc3936SMike Snitzer  * flight bios and ensure that any further io gets deferred.
26203b00b203SMikulas Patocka  */
2621ffcc3936SMike Snitzer /*
2622ffcc3936SMike Snitzer  * Suspend mechanism in request-based dm.
2623ffcc3936SMike Snitzer  *
2624ffcc3936SMike Snitzer  * 1. Flush all I/Os by lock_fs() if needed.
2625ffcc3936SMike Snitzer  * 2. Stop dispatching any I/O by stopping the request_queue.
2626ffcc3936SMike Snitzer  * 3. Wait for all in-flight I/Os to be completed or requeued.
2627ffcc3936SMike Snitzer  *
2628ffcc3936SMike Snitzer  * To abort suspend, start the request_queue.
2629ffcc3936SMike Snitzer  */
2630ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2631ffcc3936SMike Snitzer {
2632ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2633ffcc3936SMike Snitzer 	int r = 0;
2634ffcc3936SMike Snitzer 
2635ffcc3936SMike Snitzer retry:
2636ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2637ffcc3936SMike Snitzer 
2638ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2639ffcc3936SMike Snitzer 		r = -EINVAL;
2640ffcc3936SMike Snitzer 		goto out_unlock;
2641ffcc3936SMike Snitzer 	}
2642ffcc3936SMike Snitzer 
2643ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2644ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2645ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2646ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2647ffcc3936SMike Snitzer 		if (r)
2648ffcc3936SMike Snitzer 			return r;
2649ffcc3936SMike Snitzer 		goto retry;
2650ffcc3936SMike Snitzer 	}
2651ffcc3936SMike Snitzer 
2652a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2653ffcc3936SMike Snitzer 
2654eaf9a736SMike Snitzer 	r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
2655ffcc3936SMike Snitzer 	if (r)
2656ffcc3936SMike Snitzer 		goto out_unlock;
26573b00b203SMikulas Patocka 
26584d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
26594d4471cbSKiyoshi Ueda 
2660d287483dSAlasdair G Kergon out_unlock:
2661e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2662cf222b37SAlasdair G Kergon 	return r;
26631da177e4SLinus Torvalds }
26641da177e4SLinus Torvalds 
2665ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map)
26661da177e4SLinus Torvalds {
2667ffcc3936SMike Snitzer 	if (map) {
2668ffcc3936SMike Snitzer 		int r = dm_table_resume_targets(map);
26698757b776SMilan Broz 		if (r)
2670ffcc3936SMike Snitzer 			return r;
2671ffcc3936SMike Snitzer 	}
26722ca3310eSAlasdair G Kergon 
26739a1fb464SMikulas Patocka 	dm_queue_flush(md);
26742ca3310eSAlasdair G Kergon 
2675cec47e3dSKiyoshi Ueda 	/*
2676cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2677cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2678cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2679cec47e3dSKiyoshi Ueda 	 */
2680cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2681eca7ee6dSMike Snitzer 		dm_start_queue(md->queue);
2682cec47e3dSKiyoshi Ueda 
26832ca3310eSAlasdair G Kergon 	unlock_fs(md);
26842ca3310eSAlasdair G Kergon 
2685ffcc3936SMike Snitzer 	return 0;
2686ffcc3936SMike Snitzer }
2687ffcc3936SMike Snitzer 
2688ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md)
2689ffcc3936SMike Snitzer {
26908dc23658SMinfei Huang 	int r;
2691ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2692ffcc3936SMike Snitzer 
2693ffcc3936SMike Snitzer retry:
26948dc23658SMinfei Huang 	r = -EINVAL;
2695ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2696ffcc3936SMike Snitzer 
2697ffcc3936SMike Snitzer 	if (!dm_suspended_md(md))
2698ffcc3936SMike Snitzer 		goto out;
2699ffcc3936SMike Snitzer 
2700ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2701ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2702ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2703ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2704ffcc3936SMike Snitzer 		if (r)
2705ffcc3936SMike Snitzer 			return r;
2706ffcc3936SMike Snitzer 		goto retry;
2707ffcc3936SMike Snitzer 	}
2708ffcc3936SMike Snitzer 
2709a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2710ffcc3936SMike Snitzer 	if (!map || !dm_table_get_size(map))
2711ffcc3936SMike Snitzer 		goto out;
2712ffcc3936SMike Snitzer 
2713ffcc3936SMike Snitzer 	r = __dm_resume(md, map);
2714ffcc3936SMike Snitzer 	if (r)
2715ffcc3936SMike Snitzer 		goto out;
2716ffcc3936SMike Snitzer 
27172ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
2718cf222b37SAlasdair G Kergon out:
2719e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
27202ca3310eSAlasdair G Kergon 
2721cf222b37SAlasdair G Kergon 	return r;
27221da177e4SLinus Torvalds }
27231da177e4SLinus Torvalds 
2724fd2ed4d2SMikulas Patocka /*
2725fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
2726fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
2727fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
2728fd2ed4d2SMikulas Patocka  */
2729fd2ed4d2SMikulas Patocka 
2730ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
2731ffcc3936SMike Snitzer {
2732ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2733ffcc3936SMike Snitzer 
27341ea0654eSBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
27351ea0654eSBart Van Assche 
273696b26c8cSMikulas Patocka 	if (md->internal_suspend_count++)
2737ffcc3936SMike Snitzer 		return; /* nested internal suspend */
2738ffcc3936SMike Snitzer 
2739ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2740ffcc3936SMike Snitzer 		set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2741ffcc3936SMike Snitzer 		return; /* nest suspend */
2742ffcc3936SMike Snitzer 	}
2743ffcc3936SMike Snitzer 
2744a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2745ffcc3936SMike Snitzer 
2746ffcc3936SMike Snitzer 	/*
2747ffcc3936SMike Snitzer 	 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2748ffcc3936SMike Snitzer 	 * supported.  Properly supporting a TASK_INTERRUPTIBLE internal suspend
2749ffcc3936SMike Snitzer 	 * would require changing .presuspend to return an error -- avoid this
2750ffcc3936SMike Snitzer 	 * until there is a need for more elaborate variants of internal suspend.
2751ffcc3936SMike Snitzer 	 */
2752eaf9a736SMike Snitzer 	(void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
2753eaf9a736SMike Snitzer 			    DMF_SUSPENDED_INTERNALLY);
2754ffcc3936SMike Snitzer 
2755ffcc3936SMike Snitzer 	dm_table_postsuspend_targets(map);
2756ffcc3936SMike Snitzer }
2757ffcc3936SMike Snitzer 
2758ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md)
2759ffcc3936SMike Snitzer {
276096b26c8cSMikulas Patocka 	BUG_ON(!md->internal_suspend_count);
276196b26c8cSMikulas Patocka 
276296b26c8cSMikulas Patocka 	if (--md->internal_suspend_count)
2763ffcc3936SMike Snitzer 		return; /* resume from nested internal suspend */
2764ffcc3936SMike Snitzer 
2765ffcc3936SMike Snitzer 	if (dm_suspended_md(md))
2766ffcc3936SMike Snitzer 		goto done; /* resume from nested suspend */
2767ffcc3936SMike Snitzer 
2768ffcc3936SMike Snitzer 	/*
2769ffcc3936SMike Snitzer 	 * NOTE: existing callers don't need to call dm_table_resume_targets
2770ffcc3936SMike Snitzer 	 * (which may fail -- so best to avoid it for now by passing NULL map)
2771ffcc3936SMike Snitzer 	 */
2772ffcc3936SMike Snitzer 	(void) __dm_resume(md, NULL);
2773ffcc3936SMike Snitzer 
2774ffcc3936SMike Snitzer done:
2775ffcc3936SMike Snitzer 	clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2776ffcc3936SMike Snitzer 	smp_mb__after_atomic();
2777ffcc3936SMike Snitzer 	wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2778ffcc3936SMike Snitzer }
2779ffcc3936SMike Snitzer 
2780ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md)
2781fd2ed4d2SMikulas Patocka {
2782fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
2783ffcc3936SMike Snitzer 	__dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2784ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2785ffcc3936SMike Snitzer }
2786ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
2787ffcc3936SMike Snitzer 
2788ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md)
2789ffcc3936SMike Snitzer {
2790ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2791ffcc3936SMike Snitzer 	__dm_internal_resume(md);
2792ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2793ffcc3936SMike Snitzer }
2794ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume);
2795ffcc3936SMike Snitzer 
2796ffcc3936SMike Snitzer /*
2797ffcc3936SMike Snitzer  * Fast variants of internal suspend/resume hold md->suspend_lock,
2798ffcc3936SMike Snitzer  * which prevents interaction with userspace-driven suspend.
2799ffcc3936SMike Snitzer  */
2800ffcc3936SMike Snitzer 
2801ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md)
2802ffcc3936SMike Snitzer {
2803ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2804ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2805fd2ed4d2SMikulas Patocka 		return;
2806fd2ed4d2SMikulas Patocka 
2807fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2808fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
2809fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
2810fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2811fd2ed4d2SMikulas Patocka }
2812b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
2813fd2ed4d2SMikulas Patocka 
2814ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md)
2815fd2ed4d2SMikulas Patocka {
2816ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2817fd2ed4d2SMikulas Patocka 		goto done;
2818fd2ed4d2SMikulas Patocka 
2819fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
2820fd2ed4d2SMikulas Patocka 
2821fd2ed4d2SMikulas Patocka done:
2822fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
2823fd2ed4d2SMikulas Patocka }
2824b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
2825fd2ed4d2SMikulas Patocka 
28261da177e4SLinus Torvalds /*-----------------------------------------------------------------
28271da177e4SLinus Torvalds  * Event notification.
28281da177e4SLinus Torvalds  *---------------------------------------------------------------*/
28293abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
283060935eb2SMilan Broz 		       unsigned cookie)
283169267a30SAlasdair G Kergon {
283260935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
283360935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
283460935eb2SMilan Broz 
283560935eb2SMilan Broz 	if (!cookie)
28363abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
283760935eb2SMilan Broz 	else {
283860935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
283960935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
28403abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
28413abf85b5SPeter Rajnoha 					  action, envp);
284260935eb2SMilan Broz 	}
284369267a30SAlasdair G Kergon }
284469267a30SAlasdair G Kergon 
28457a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
28467a8c3d3bSMike Anderson {
28477a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
28487a8c3d3bSMike Anderson }
28497a8c3d3bSMike Anderson 
28501da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
28511da177e4SLinus Torvalds {
28521da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
28531da177e4SLinus Torvalds }
28541da177e4SLinus Torvalds 
28551da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
28561da177e4SLinus Torvalds {
28571da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
28581da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
28591da177e4SLinus Torvalds }
28601da177e4SLinus Torvalds 
28617a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
28627a8c3d3bSMike Anderson {
28637a8c3d3bSMike Anderson 	unsigned long flags;
28647a8c3d3bSMike Anderson 
28657a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
28667a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
28677a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
28687a8c3d3bSMike Anderson }
28697a8c3d3bSMike Anderson 
28701da177e4SLinus Torvalds /*
28711da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
28721da177e4SLinus Torvalds  * count on 'md'.
28731da177e4SLinus Torvalds  */
28741da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
28751da177e4SLinus Torvalds {
28761da177e4SLinus Torvalds 	return md->disk;
28771da177e4SLinus Torvalds }
287865ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk);
28791da177e4SLinus Torvalds 
2880784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2881784aae73SMilan Broz {
28822995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
2883784aae73SMilan Broz }
2884784aae73SMilan Broz 
2885784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2886784aae73SMilan Broz {
2887784aae73SMilan Broz 	struct mapped_device *md;
2888784aae73SMilan Broz 
28892995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
2890784aae73SMilan Broz 
2891b9a41d21SHou Tao 	spin_lock(&_minor_lock);
2892b9a41d21SHou Tao 	if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2893b9a41d21SHou Tao 		md = NULL;
2894b9a41d21SHou Tao 		goto out;
2895b9a41d21SHou Tao 	}
2896784aae73SMilan Broz 	dm_get(md);
2897b9a41d21SHou Tao out:
2898b9a41d21SHou Tao 	spin_unlock(&_minor_lock);
2899b9a41d21SHou Tao 
2900784aae73SMilan Broz 	return md;
2901784aae73SMilan Broz }
2902784aae73SMilan Broz 
29034f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
29041da177e4SLinus Torvalds {
29051da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
29061da177e4SLinus Torvalds }
29071da177e4SLinus Torvalds 
2908ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md)
2909ffcc3936SMike Snitzer {
2910ffcc3936SMike Snitzer 	return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2911ffcc3936SMike Snitzer }
2912ffcc3936SMike Snitzer 
29132c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
29142c140a24SMikulas Patocka {
29152c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
29162c140a24SMikulas Patocka }
29172c140a24SMikulas Patocka 
291864dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
291964dbce58SKiyoshi Ueda {
2920ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
292164dbce58SKiyoshi Ueda }
292264dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
292364dbce58SKiyoshi Ueda 
29242e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
29252e93ccc1SKiyoshi Ueda {
2926ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
29272e93ccc1SKiyoshi Ueda }
29282e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
29292e93ccc1SKiyoshi Ueda 
29307e0d574fSBart Van Assche struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type,
29310776aa0eSMike Snitzer 					    unsigned integrity, unsigned per_io_data_size,
29320776aa0eSMike Snitzer 					    unsigned min_pool_size)
2933e6ee8c0bSKiyoshi Ueda {
2934115485e8SMike Snitzer 	struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
293578d8e58aSMike Snitzer 	unsigned int pool_size = 0;
293664f52b0eSMike Snitzer 	unsigned int front_pad, io_front_pad;
29376f1c819cSKent Overstreet 	int ret;
2938e6ee8c0bSKiyoshi Ueda 
2939e6ee8c0bSKiyoshi Ueda 	if (!pools)
29404e6e36c3SMike Snitzer 		return NULL;
2941e6ee8c0bSKiyoshi Ueda 
294278d8e58aSMike Snitzer 	switch (type) {
294378d8e58aSMike Snitzer 	case DM_TYPE_BIO_BASED:
2944545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
294522c11858SMike Snitzer 	case DM_TYPE_NVME_BIO_BASED:
29460776aa0eSMike Snitzer 		pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size);
294730187e1dSMike Snitzer 		front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
294864f52b0eSMike Snitzer 		io_front_pad = roundup(front_pad,  __alignof__(struct dm_io)) + offsetof(struct dm_io, tio);
29496f1c819cSKent Overstreet 		ret = bioset_init(&pools->io_bs, pool_size, io_front_pad, 0);
29506f1c819cSKent Overstreet 		if (ret)
295164f52b0eSMike Snitzer 			goto out;
29526f1c819cSKent Overstreet 		if (integrity && bioset_integrity_create(&pools->io_bs, pool_size))
2953eb8db831SChristoph Hellwig 			goto out;
295478d8e58aSMike Snitzer 		break;
295578d8e58aSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
29560776aa0eSMike Snitzer 		pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size);
295778d8e58aSMike Snitzer 		front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
2958591ddcfcSMike Snitzer 		/* per_io_data_size is used for blk-mq pdu at queue allocation */
295978d8e58aSMike Snitzer 		break;
296078d8e58aSMike Snitzer 	default:
296178d8e58aSMike Snitzer 		BUG();
296278d8e58aSMike Snitzer 	}
296378d8e58aSMike Snitzer 
29646f1c819cSKent Overstreet 	ret = bioset_init(&pools->bs, pool_size, front_pad, 0);
29656f1c819cSKent Overstreet 	if (ret)
29665f015204SJun'ichi Nomura 		goto out;
2967e6ee8c0bSKiyoshi Ueda 
29686f1c819cSKent Overstreet 	if (integrity && bioset_integrity_create(&pools->bs, pool_size))
29695f015204SJun'ichi Nomura 		goto out;
2970a91a2785SMartin K. Petersen 
2971e6ee8c0bSKiyoshi Ueda 	return pools;
297278d8e58aSMike Snitzer 
29735f015204SJun'ichi Nomura out:
29745f015204SJun'ichi Nomura 	dm_free_md_mempools(pools);
2975e6ee8c0bSKiyoshi Ueda 
29764e6e36c3SMike Snitzer 	return NULL;
2977e6ee8c0bSKiyoshi Ueda }
2978e6ee8c0bSKiyoshi Ueda 
2979e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
2980e6ee8c0bSKiyoshi Ueda {
2981e6ee8c0bSKiyoshi Ueda 	if (!pools)
2982e6ee8c0bSKiyoshi Ueda 		return;
2983e6ee8c0bSKiyoshi Ueda 
29846f1c819cSKent Overstreet 	bioset_exit(&pools->bs);
29856f1c819cSKent Overstreet 	bioset_exit(&pools->io_bs);
2986e6ee8c0bSKiyoshi Ueda 
2987e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2988e6ee8c0bSKiyoshi Ueda }
2989e6ee8c0bSKiyoshi Ueda 
29909c72bad1SChristoph Hellwig struct dm_pr {
29919c72bad1SChristoph Hellwig 	u64	old_key;
29929c72bad1SChristoph Hellwig 	u64	new_key;
29939c72bad1SChristoph Hellwig 	u32	flags;
29949c72bad1SChristoph Hellwig 	bool	fail_early;
29959c72bad1SChristoph Hellwig };
29969c72bad1SChristoph Hellwig 
29979c72bad1SChristoph Hellwig static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
29989c72bad1SChristoph Hellwig 		      void *data)
29999c72bad1SChristoph Hellwig {
30009c72bad1SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
30019c72bad1SChristoph Hellwig 	struct dm_table *table;
30029c72bad1SChristoph Hellwig 	struct dm_target *ti;
30039c72bad1SChristoph Hellwig 	int ret = -ENOTTY, srcu_idx;
30049c72bad1SChristoph Hellwig 
30059c72bad1SChristoph Hellwig 	table = dm_get_live_table(md, &srcu_idx);
30069c72bad1SChristoph Hellwig 	if (!table || !dm_table_get_size(table))
30079c72bad1SChristoph Hellwig 		goto out;
30089c72bad1SChristoph Hellwig 
30099c72bad1SChristoph Hellwig 	/* We only support devices that have a single target */
30109c72bad1SChristoph Hellwig 	if (dm_table_get_num_targets(table) != 1)
30119c72bad1SChristoph Hellwig 		goto out;
30129c72bad1SChristoph Hellwig 	ti = dm_table_get_target(table, 0);
30139c72bad1SChristoph Hellwig 
30149c72bad1SChristoph Hellwig 	ret = -EINVAL;
30159c72bad1SChristoph Hellwig 	if (!ti->type->iterate_devices)
30169c72bad1SChristoph Hellwig 		goto out;
30179c72bad1SChristoph Hellwig 
30189c72bad1SChristoph Hellwig 	ret = ti->type->iterate_devices(ti, fn, data);
30199c72bad1SChristoph Hellwig out:
30209c72bad1SChristoph Hellwig 	dm_put_live_table(md, srcu_idx);
30219c72bad1SChristoph Hellwig 	return ret;
30229c72bad1SChristoph Hellwig }
30239c72bad1SChristoph Hellwig 
30249c72bad1SChristoph Hellwig /*
30259c72bad1SChristoph Hellwig  * For register / unregister we need to manually call out to every path.
30269c72bad1SChristoph Hellwig  */
30279c72bad1SChristoph Hellwig static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
30289c72bad1SChristoph Hellwig 			    sector_t start, sector_t len, void *data)
30299c72bad1SChristoph Hellwig {
30309c72bad1SChristoph Hellwig 	struct dm_pr *pr = data;
30319c72bad1SChristoph Hellwig 	const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
30329c72bad1SChristoph Hellwig 
30339c72bad1SChristoph Hellwig 	if (!ops || !ops->pr_register)
30349c72bad1SChristoph Hellwig 		return -EOPNOTSUPP;
30359c72bad1SChristoph Hellwig 	return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
30369c72bad1SChristoph Hellwig }
30379c72bad1SChristoph Hellwig 
303871cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
303971cdb697SChristoph Hellwig 			  u32 flags)
304071cdb697SChristoph Hellwig {
30419c72bad1SChristoph Hellwig 	struct dm_pr pr = {
30429c72bad1SChristoph Hellwig 		.old_key	= old_key,
30439c72bad1SChristoph Hellwig 		.new_key	= new_key,
30449c72bad1SChristoph Hellwig 		.flags		= flags,
30459c72bad1SChristoph Hellwig 		.fail_early	= true,
30469c72bad1SChristoph Hellwig 	};
30479c72bad1SChristoph Hellwig 	int ret;
304871cdb697SChristoph Hellwig 
30499c72bad1SChristoph Hellwig 	ret = dm_call_pr(bdev, __dm_pr_register, &pr);
30509c72bad1SChristoph Hellwig 	if (ret && new_key) {
30519c72bad1SChristoph Hellwig 		/* unregister all paths if we failed to register any path */
30529c72bad1SChristoph Hellwig 		pr.old_key = new_key;
30539c72bad1SChristoph Hellwig 		pr.new_key = 0;
30549c72bad1SChristoph Hellwig 		pr.flags = 0;
30559c72bad1SChristoph Hellwig 		pr.fail_early = false;
30569c72bad1SChristoph Hellwig 		dm_call_pr(bdev, __dm_pr_register, &pr);
30579c72bad1SChristoph Hellwig 	}
305871cdb697SChristoph Hellwig 
30599c72bad1SChristoph Hellwig 	return ret;
306071cdb697SChristoph Hellwig }
306171cdb697SChristoph Hellwig 
306271cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
306371cdb697SChristoph Hellwig 			 u32 flags)
306471cdb697SChristoph Hellwig {
306571cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
306671cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3067971888c4SMike Snitzer 	int r, srcu_idx;
306871cdb697SChristoph Hellwig 
30695bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
307071cdb697SChristoph Hellwig 	if (r < 0)
3071971888c4SMike Snitzer 		goto out;
307271cdb697SChristoph Hellwig 
307371cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
307471cdb697SChristoph Hellwig 	if (ops && ops->pr_reserve)
307571cdb697SChristoph Hellwig 		r = ops->pr_reserve(bdev, key, type, flags);
307671cdb697SChristoph Hellwig 	else
307771cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3078971888c4SMike Snitzer out:
3079971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
308071cdb697SChristoph Hellwig 	return r;
308171cdb697SChristoph Hellwig }
308271cdb697SChristoph Hellwig 
308371cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
308471cdb697SChristoph Hellwig {
308571cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
308671cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3087971888c4SMike Snitzer 	int r, srcu_idx;
308871cdb697SChristoph Hellwig 
30895bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
309071cdb697SChristoph Hellwig 	if (r < 0)
3091971888c4SMike Snitzer 		goto out;
309271cdb697SChristoph Hellwig 
309371cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
309471cdb697SChristoph Hellwig 	if (ops && ops->pr_release)
309571cdb697SChristoph Hellwig 		r = ops->pr_release(bdev, key, type);
309671cdb697SChristoph Hellwig 	else
309771cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3098971888c4SMike Snitzer out:
3099971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
310071cdb697SChristoph Hellwig 	return r;
310171cdb697SChristoph Hellwig }
310271cdb697SChristoph Hellwig 
310371cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
310471cdb697SChristoph Hellwig 			 enum pr_type type, bool abort)
310571cdb697SChristoph Hellwig {
310671cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
310771cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3108971888c4SMike Snitzer 	int r, srcu_idx;
310971cdb697SChristoph Hellwig 
31105bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
311171cdb697SChristoph Hellwig 	if (r < 0)
3112971888c4SMike Snitzer 		goto out;
311371cdb697SChristoph Hellwig 
311471cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
311571cdb697SChristoph Hellwig 	if (ops && ops->pr_preempt)
311671cdb697SChristoph Hellwig 		r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
311771cdb697SChristoph Hellwig 	else
311871cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3119971888c4SMike Snitzer out:
3120971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
312171cdb697SChristoph Hellwig 	return r;
312271cdb697SChristoph Hellwig }
312371cdb697SChristoph Hellwig 
312471cdb697SChristoph Hellwig static int dm_pr_clear(struct block_device *bdev, u64 key)
312571cdb697SChristoph Hellwig {
312671cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
312771cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3128971888c4SMike Snitzer 	int r, srcu_idx;
312971cdb697SChristoph Hellwig 
31305bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
313171cdb697SChristoph Hellwig 	if (r < 0)
3132971888c4SMike Snitzer 		goto out;
313371cdb697SChristoph Hellwig 
313471cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
313571cdb697SChristoph Hellwig 	if (ops && ops->pr_clear)
313671cdb697SChristoph Hellwig 		r = ops->pr_clear(bdev, key);
313771cdb697SChristoph Hellwig 	else
313871cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3139971888c4SMike Snitzer out:
3140971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
314171cdb697SChristoph Hellwig 	return r;
314271cdb697SChristoph Hellwig }
314371cdb697SChristoph Hellwig 
314471cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = {
314571cdb697SChristoph Hellwig 	.pr_register	= dm_pr_register,
314671cdb697SChristoph Hellwig 	.pr_reserve	= dm_pr_reserve,
314771cdb697SChristoph Hellwig 	.pr_release	= dm_pr_release,
314871cdb697SChristoph Hellwig 	.pr_preempt	= dm_pr_preempt,
314971cdb697SChristoph Hellwig 	.pr_clear	= dm_pr_clear,
315071cdb697SChristoph Hellwig };
315171cdb697SChristoph Hellwig 
315283d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
31531da177e4SLinus Torvalds 	.open = dm_blk_open,
31541da177e4SLinus Torvalds 	.release = dm_blk_close,
3155aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
31563ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
3157e76239a3SChristoph Hellwig 	.report_zones = dm_blk_report_zones,
315871cdb697SChristoph Hellwig 	.pr_ops = &dm_pr_ops,
31591da177e4SLinus Torvalds 	.owner = THIS_MODULE
31601da177e4SLinus Torvalds };
31611da177e4SLinus Torvalds 
3162f26c5719SDan Williams static const struct dax_operations dm_dax_ops = {
3163f26c5719SDan Williams 	.direct_access = dm_dax_direct_access,
31647e026c8cSDan Williams 	.copy_from_iter = dm_dax_copy_from_iter,
3165b3a9a0c3SDan Williams 	.copy_to_iter = dm_dax_copy_to_iter,
3166f26c5719SDan Williams };
3167f26c5719SDan Williams 
31681da177e4SLinus Torvalds /*
31691da177e4SLinus Torvalds  * module hooks
31701da177e4SLinus Torvalds  */
31711da177e4SLinus Torvalds module_init(dm_init);
31721da177e4SLinus Torvalds module_exit(dm_exit);
31731da177e4SLinus Torvalds 
31741da177e4SLinus Torvalds module_param(major, uint, 0);
31751da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
3176f4790826SMike Snitzer 
3177e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3178e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3179e8603136SMike Snitzer 
3180115485e8SMike Snitzer module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
3181115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
3182115485e8SMike Snitzer 
31831da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
31841da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
31851da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3186