xref: /openbmc/linux/drivers/md/dm.c (revision 00716545c894fc464e00612809d9cb836b180c99)
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 {
151e6ee8c0bSKiyoshi Ueda 	struct bio_set *bs;
15264f52b0eSMike Snitzer 	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 
461519049afSMike Snitzer static char *_dm_claim_ptr = "I belong to device-mapper";
462519049afSMike Snitzer 
463519049afSMike Snitzer static int dm_get_bdev_for_ioctl(struct mapped_device *md,
46466482026SMike Snitzer 				 struct block_device **bdev,
465956a4025SMike Snitzer 				 fmode_t *mode)
466aa129a22SMilan Broz {
46766482026SMike Snitzer 	struct dm_target *tgt;
4686c182cd8SHannes Reinecke 	struct dm_table *map;
469da5dadb4SMike Snitzer 	int srcu_idx, r, r2;
470aa129a22SMilan Broz 
4716c182cd8SHannes Reinecke retry:
472e56f81e0SChristoph Hellwig 	r = -ENOTTY;
473956a4025SMike Snitzer 	map = dm_get_live_table(md, &srcu_idx);
474aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
475aa129a22SMilan Broz 		goto out;
476aa129a22SMilan Broz 
477aa129a22SMilan Broz 	/* We only support devices that have a single target */
478aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
479aa129a22SMilan Broz 		goto out;
480aa129a22SMilan Broz 
48166482026SMike Snitzer 	tgt = dm_table_get_target(map, 0);
48266482026SMike Snitzer 	if (!tgt->type->prepare_ioctl)
4834d341d82SMike Snitzer 		goto out;
484aa129a22SMilan Broz 
4854f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
486aa129a22SMilan Broz 		r = -EAGAIN;
487aa129a22SMilan Broz 		goto out;
488aa129a22SMilan Broz 	}
489aa129a22SMilan Broz 
49066482026SMike Snitzer 	r = tgt->type->prepare_ioctl(tgt, bdev, mode);
491e56f81e0SChristoph Hellwig 	if (r < 0)
492e56f81e0SChristoph Hellwig 		goto out;
493e56f81e0SChristoph Hellwig 
494956a4025SMike Snitzer 	bdgrab(*bdev);
495da5dadb4SMike Snitzer 	r2 = blkdev_get(*bdev, *mode, _dm_claim_ptr);
496da5dadb4SMike Snitzer 	if (r2 < 0) {
497da5dadb4SMike Snitzer 		r = r2;
498519049afSMike Snitzer 		goto out;
499da5dadb4SMike Snitzer 	}
500519049afSMike Snitzer 
501956a4025SMike Snitzer 	dm_put_live_table(md, srcu_idx);
502e56f81e0SChristoph Hellwig 	return r;
503aa129a22SMilan Broz 
504aa129a22SMilan Broz out:
505956a4025SMike Snitzer 	dm_put_live_table(md, srcu_idx);
5065bbbfdf6SJunichi Nomura 	if (r == -ENOTCONN && !fatal_signal_pending(current)) {
5076c182cd8SHannes Reinecke 		msleep(10);
5086c182cd8SHannes Reinecke 		goto retry;
5096c182cd8SHannes Reinecke 	}
510e56f81e0SChristoph Hellwig 	return r;
511e56f81e0SChristoph Hellwig }
5126c182cd8SHannes Reinecke 
513e56f81e0SChristoph Hellwig static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
514e56f81e0SChristoph Hellwig 			unsigned int cmd, unsigned long arg)
515e56f81e0SChristoph Hellwig {
516e56f81e0SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
517956a4025SMike Snitzer 	int r;
518e56f81e0SChristoph Hellwig 
519519049afSMike Snitzer 	r = dm_get_bdev_for_ioctl(md, &bdev, &mode);
520e56f81e0SChristoph Hellwig 	if (r < 0)
521e56f81e0SChristoph Hellwig 		return r;
522e56f81e0SChristoph Hellwig 
523e56f81e0SChristoph Hellwig 	if (r > 0) {
524e56f81e0SChristoph Hellwig 		/*
525e980f623SChristoph Hellwig 		 * Target determined this ioctl is being issued against a
526e980f623SChristoph Hellwig 		 * subset of the parent bdev; require extra privileges.
527e56f81e0SChristoph Hellwig 		 */
528e980f623SChristoph Hellwig 		if (!capable(CAP_SYS_RAWIO)) {
529e980f623SChristoph Hellwig 			DMWARN_LIMIT(
530e980f623SChristoph Hellwig 	"%s: sending ioctl %x to DM device without required privilege.",
531e980f623SChristoph Hellwig 				current->comm, cmd);
532e980f623SChristoph Hellwig 			r = -ENOIOCTLCMD;
533e56f81e0SChristoph Hellwig 			goto out;
534e56f81e0SChristoph Hellwig 		}
535e980f623SChristoph Hellwig 	}
536e56f81e0SChristoph Hellwig 
53766482026SMike Snitzer 	r =  __blkdev_driver_ioctl(bdev, mode, cmd, arg);
538e56f81e0SChristoph Hellwig out:
539519049afSMike Snitzer 	blkdev_put(bdev, mode);
540aa129a22SMilan Broz 	return r;
541aa129a22SMilan Broz }
542aa129a22SMilan Broz 
543978e51baSMike Snitzer static void start_io_acct(struct dm_io *io);
544978e51baSMike Snitzer 
545978e51baSMike Snitzer static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio)
5461da177e4SLinus Torvalds {
54764f52b0eSMike Snitzer 	struct dm_io *io;
54864f52b0eSMike Snitzer 	struct dm_target_io *tio;
54964f52b0eSMike Snitzer 	struct bio *clone;
55064f52b0eSMike Snitzer 
55164f52b0eSMike Snitzer 	clone = bio_alloc_bioset(GFP_NOIO, 0, md->io_bs);
55264f52b0eSMike Snitzer 	if (!clone)
55364f52b0eSMike Snitzer 		return NULL;
55464f52b0eSMike Snitzer 
55564f52b0eSMike Snitzer 	tio = container_of(clone, struct dm_target_io, clone);
55664f52b0eSMike Snitzer 	tio->inside_dm_io = true;
55764f52b0eSMike Snitzer 	tio->io = NULL;
55864f52b0eSMike Snitzer 
55964f52b0eSMike Snitzer 	io = container_of(tio, struct dm_io, tio);
56064f52b0eSMike Snitzer 	io->magic = DM_IO_MAGIC;
561978e51baSMike Snitzer 	io->status = 0;
562978e51baSMike Snitzer 	atomic_set(&io->io_count, 1);
563978e51baSMike Snitzer 	io->orig_bio = bio;
564978e51baSMike Snitzer 	io->md = md;
565978e51baSMike Snitzer 	spin_lock_init(&io->endio_lock);
566978e51baSMike Snitzer 
567978e51baSMike Snitzer 	start_io_acct(io);
56864f52b0eSMike Snitzer 
56964f52b0eSMike Snitzer 	return io;
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds 
572028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
5731da177e4SLinus Torvalds {
57464f52b0eSMike Snitzer 	bio_put(&io->tio.clone);
57564f52b0eSMike Snitzer }
57664f52b0eSMike Snitzer 
57764f52b0eSMike Snitzer static struct dm_target_io *alloc_tio(struct clone_info *ci, struct dm_target *ti,
57864f52b0eSMike Snitzer 				      unsigned target_bio_nr, gfp_t gfp_mask)
57964f52b0eSMike Snitzer {
58064f52b0eSMike Snitzer 	struct dm_target_io *tio;
58164f52b0eSMike Snitzer 
58264f52b0eSMike Snitzer 	if (!ci->io->tio.io) {
58364f52b0eSMike Snitzer 		/* the dm_target_io embedded in ci->io is available */
58464f52b0eSMike Snitzer 		tio = &ci->io->tio;
58564f52b0eSMike Snitzer 	} else {
586bc02cdbeSMike Snitzer 		struct bio *clone = bio_alloc_bioset(gfp_mask, 0, ci->io->md->bs);
58764f52b0eSMike Snitzer 		if (!clone)
58864f52b0eSMike Snitzer 			return NULL;
58964f52b0eSMike Snitzer 
59064f52b0eSMike Snitzer 		tio = container_of(clone, struct dm_target_io, clone);
59164f52b0eSMike Snitzer 		tio->inside_dm_io = false;
59264f52b0eSMike Snitzer 	}
59364f52b0eSMike Snitzer 
59464f52b0eSMike Snitzer 	tio->magic = DM_TIO_MAGIC;
59564f52b0eSMike Snitzer 	tio->io = ci->io;
59664f52b0eSMike Snitzer 	tio->ti = ti;
59764f52b0eSMike Snitzer 	tio->target_bio_nr = target_bio_nr;
59864f52b0eSMike Snitzer 
59964f52b0eSMike Snitzer 	return tio;
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds 
602cfae7529SMike Snitzer static void free_tio(struct dm_target_io *tio)
6031da177e4SLinus Torvalds {
60464f52b0eSMike Snitzer 	if (tio->inside_dm_io)
60564f52b0eSMike Snitzer 		return;
606dba14160SMikulas Patocka 	bio_put(&tio->clone);
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
6094cc96131SMike Snitzer int md_in_flight(struct mapped_device *md)
61090abb8c4SKiyoshi Ueda {
61190abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
61290abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
61390abb8c4SKiyoshi Ueda }
61490abb8c4SKiyoshi Ueda 
6153eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
6163eaf840eSJun'ichi "Nick" Nomura {
6173eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
618745dc570SMike Snitzer 	struct bio *bio = io->orig_bio;
619fd2ed4d2SMikulas Patocka 	int rw = bio_data_dir(bio);
6203eaf840eSJun'ichi "Nick" Nomura 
6213eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
6223eaf840eSJun'ichi "Nick" Nomura 
623f3986374SMike Snitzer 	generic_start_io_acct(md->queue, rw, bio_sectors(bio), &dm_disk(md)->part0);
624f3986374SMike Snitzer 
6251e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw],
6261e9bb880SShaohua Li 		   atomic_inc_return(&md->pending[rw]));
627fd2ed4d2SMikulas Patocka 
628fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
629528ec5abSMike Christie 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
630528ec5abSMike Christie 				    bio->bi_iter.bi_sector, bio_sectors(bio),
631528ec5abSMike Christie 				    false, 0, &io->stats_aux);
6323eaf840eSJun'ichi "Nick" Nomura }
6333eaf840eSJun'ichi "Nick" Nomura 
634d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
6353eaf840eSJun'ichi "Nick" Nomura {
6363eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
637745dc570SMike Snitzer 	struct bio *bio = io->orig_bio;
6383eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
63918c0b223SGu Zheng 	int pending;
6403eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
6413eaf840eSJun'ichi "Nick" Nomura 
642d62e26b3SJens Axboe 	generic_end_io_acct(md->queue, rw, &dm_disk(md)->part0, io->start_time);
6433eaf840eSJun'ichi "Nick" Nomura 
644fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
645528ec5abSMike Christie 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
646528ec5abSMike Christie 				    bio->bi_iter.bi_sector, bio_sectors(bio),
647528ec5abSMike Christie 				    true, duration, &io->stats_aux);
648fd2ed4d2SMikulas Patocka 
649af7e466aSMikulas Patocka 	/*
650af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
651d87f4c14STejun Heo 	 * a flush.
652af7e466aSMikulas Patocka 	 */
6531e9bb880SShaohua Li 	pending = atomic_dec_return(&md->pending[rw]);
6541e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
655316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
6563eaf840eSJun'ichi "Nick" Nomura 
657d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
658d221d2e7SMikulas Patocka 	if (!pending)
659d221d2e7SMikulas Patocka 		wake_up(&md->wait);
6603eaf840eSJun'ichi "Nick" Nomura }
6613eaf840eSJun'ichi "Nick" Nomura 
6621da177e4SLinus Torvalds /*
6631da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
6641da177e4SLinus Torvalds  */
66592c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
6661da177e4SLinus Torvalds {
66705447420SKiyoshi Ueda 	unsigned long flags;
6681da177e4SLinus Torvalds 
66905447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
6701da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
67105447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
67292c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds /*
6761da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
6771da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
67883d5e5b0SMikulas Patocka  * dm_put_live_table() when finished.
6791da177e4SLinus Torvalds  */
68083d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
6811da177e4SLinus Torvalds {
68283d5e5b0SMikulas Patocka 	*srcu_idx = srcu_read_lock(&md->io_barrier);
6831da177e4SLinus Torvalds 
68483d5e5b0SMikulas Patocka 	return srcu_dereference(md->map, &md->io_barrier);
68583d5e5b0SMikulas Patocka }
6861da177e4SLinus Torvalds 
68783d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
68883d5e5b0SMikulas Patocka {
68983d5e5b0SMikulas Patocka 	srcu_read_unlock(&md->io_barrier, srcu_idx);
69083d5e5b0SMikulas Patocka }
69183d5e5b0SMikulas Patocka 
69283d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md)
69383d5e5b0SMikulas Patocka {
69483d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
69583d5e5b0SMikulas Patocka 	synchronize_rcu_expedited();
69683d5e5b0SMikulas Patocka }
69783d5e5b0SMikulas Patocka 
69883d5e5b0SMikulas Patocka /*
69983d5e5b0SMikulas Patocka  * A fast alternative to dm_get_live_table/dm_put_live_table.
70083d5e5b0SMikulas Patocka  * The caller must not block between these two functions.
70183d5e5b0SMikulas Patocka  */
70283d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
70383d5e5b0SMikulas Patocka {
70483d5e5b0SMikulas Patocka 	rcu_read_lock();
70583d5e5b0SMikulas Patocka 	return rcu_dereference(md->map);
70683d5e5b0SMikulas Patocka }
70783d5e5b0SMikulas Patocka 
70883d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
70983d5e5b0SMikulas Patocka {
71083d5e5b0SMikulas Patocka 	rcu_read_unlock();
7111da177e4SLinus Torvalds }
7121da177e4SLinus Torvalds 
7133ac51e74SDarrick J. Wong /*
71486f1152bSBenjamin Marzinski  * Open a table device so we can use it as a map destination.
71586f1152bSBenjamin Marzinski  */
71686f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev,
71786f1152bSBenjamin Marzinski 			     struct mapped_device *md)
71886f1152bSBenjamin Marzinski {
71986f1152bSBenjamin Marzinski 	struct block_device *bdev;
72086f1152bSBenjamin Marzinski 
72186f1152bSBenjamin Marzinski 	int r;
72286f1152bSBenjamin Marzinski 
72386f1152bSBenjamin Marzinski 	BUG_ON(td->dm_dev.bdev);
72486f1152bSBenjamin Marzinski 
725519049afSMike Snitzer 	bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr);
72686f1152bSBenjamin Marzinski 	if (IS_ERR(bdev))
72786f1152bSBenjamin Marzinski 		return PTR_ERR(bdev);
72886f1152bSBenjamin Marzinski 
72986f1152bSBenjamin Marzinski 	r = bd_link_disk_holder(bdev, dm_disk(md));
73086f1152bSBenjamin Marzinski 	if (r) {
73186f1152bSBenjamin Marzinski 		blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
73286f1152bSBenjamin Marzinski 		return r;
73386f1152bSBenjamin Marzinski 	}
73486f1152bSBenjamin Marzinski 
73586f1152bSBenjamin Marzinski 	td->dm_dev.bdev = bdev;
736817bf402SDan Williams 	td->dm_dev.dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
73786f1152bSBenjamin Marzinski 	return 0;
73886f1152bSBenjamin Marzinski }
73986f1152bSBenjamin Marzinski 
74086f1152bSBenjamin Marzinski /*
74186f1152bSBenjamin Marzinski  * Close a table device that we've been using.
74286f1152bSBenjamin Marzinski  */
74386f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md)
74486f1152bSBenjamin Marzinski {
74586f1152bSBenjamin Marzinski 	if (!td->dm_dev.bdev)
74686f1152bSBenjamin Marzinski 		return;
74786f1152bSBenjamin Marzinski 
74886f1152bSBenjamin Marzinski 	bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
74986f1152bSBenjamin Marzinski 	blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
750817bf402SDan Williams 	put_dax(td->dm_dev.dax_dev);
75186f1152bSBenjamin Marzinski 	td->dm_dev.bdev = NULL;
752817bf402SDan Williams 	td->dm_dev.dax_dev = NULL;
75386f1152bSBenjamin Marzinski }
75486f1152bSBenjamin Marzinski 
75586f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev,
75686f1152bSBenjamin Marzinski 					      fmode_t mode) {
75786f1152bSBenjamin Marzinski 	struct table_device *td;
75886f1152bSBenjamin Marzinski 
75986f1152bSBenjamin Marzinski 	list_for_each_entry(td, l, list)
76086f1152bSBenjamin Marzinski 		if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
76186f1152bSBenjamin Marzinski 			return td;
76286f1152bSBenjamin Marzinski 
76386f1152bSBenjamin Marzinski 	return NULL;
76486f1152bSBenjamin Marzinski }
76586f1152bSBenjamin Marzinski 
76686f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
76786f1152bSBenjamin Marzinski 			struct dm_dev **result) {
76886f1152bSBenjamin Marzinski 	int r;
76986f1152bSBenjamin Marzinski 	struct table_device *td;
77086f1152bSBenjamin Marzinski 
77186f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
77286f1152bSBenjamin Marzinski 	td = find_table_device(&md->table_devices, dev, mode);
77386f1152bSBenjamin Marzinski 	if (!td) {
774115485e8SMike Snitzer 		td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
77586f1152bSBenjamin Marzinski 		if (!td) {
77686f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
77786f1152bSBenjamin Marzinski 			return -ENOMEM;
77886f1152bSBenjamin Marzinski 		}
77986f1152bSBenjamin Marzinski 
78086f1152bSBenjamin Marzinski 		td->dm_dev.mode = mode;
78186f1152bSBenjamin Marzinski 		td->dm_dev.bdev = NULL;
78286f1152bSBenjamin Marzinski 
78386f1152bSBenjamin Marzinski 		if ((r = open_table_device(td, dev, md))) {
78486f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
78586f1152bSBenjamin Marzinski 			kfree(td);
78686f1152bSBenjamin Marzinski 			return r;
78786f1152bSBenjamin Marzinski 		}
78886f1152bSBenjamin Marzinski 
78986f1152bSBenjamin Marzinski 		format_dev_t(td->dm_dev.name, dev);
79086f1152bSBenjamin Marzinski 
791b0b4d7c6SElena Reshetova 		refcount_set(&td->count, 1);
79286f1152bSBenjamin Marzinski 		list_add(&td->list, &md->table_devices);
793b0b4d7c6SElena Reshetova 	} else {
794b0b4d7c6SElena Reshetova 		refcount_inc(&td->count);
79586f1152bSBenjamin Marzinski 	}
79686f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
79786f1152bSBenjamin Marzinski 
79886f1152bSBenjamin Marzinski 	*result = &td->dm_dev;
79986f1152bSBenjamin Marzinski 	return 0;
80086f1152bSBenjamin Marzinski }
80186f1152bSBenjamin Marzinski EXPORT_SYMBOL_GPL(dm_get_table_device);
80286f1152bSBenjamin Marzinski 
80386f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
80486f1152bSBenjamin Marzinski {
80586f1152bSBenjamin Marzinski 	struct table_device *td = container_of(d, struct table_device, dm_dev);
80686f1152bSBenjamin Marzinski 
80786f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
808b0b4d7c6SElena Reshetova 	if (refcount_dec_and_test(&td->count)) {
80986f1152bSBenjamin Marzinski 		close_table_device(td, md);
81086f1152bSBenjamin Marzinski 		list_del(&td->list);
81186f1152bSBenjamin Marzinski 		kfree(td);
81286f1152bSBenjamin Marzinski 	}
81386f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
81486f1152bSBenjamin Marzinski }
81586f1152bSBenjamin Marzinski EXPORT_SYMBOL(dm_put_table_device);
81686f1152bSBenjamin Marzinski 
81786f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices)
81886f1152bSBenjamin Marzinski {
81986f1152bSBenjamin Marzinski 	struct list_head *tmp, *next;
82086f1152bSBenjamin Marzinski 
82186f1152bSBenjamin Marzinski 	list_for_each_safe(tmp, next, devices) {
82286f1152bSBenjamin Marzinski 		struct table_device *td = list_entry(tmp, struct table_device, list);
82386f1152bSBenjamin Marzinski 
82486f1152bSBenjamin Marzinski 		DMWARN("dm_destroy: %s still exists with %d references",
825b0b4d7c6SElena Reshetova 		       td->dm_dev.name, refcount_read(&td->count));
82686f1152bSBenjamin Marzinski 		kfree(td);
82786f1152bSBenjamin Marzinski 	}
82886f1152bSBenjamin Marzinski }
82986f1152bSBenjamin Marzinski 
83086f1152bSBenjamin Marzinski /*
8313ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
8323ac51e74SDarrick J. Wong  */
8333ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
8343ac51e74SDarrick J. Wong {
8353ac51e74SDarrick J. Wong 	*geo = md->geometry;
8363ac51e74SDarrick J. Wong 
8373ac51e74SDarrick J. Wong 	return 0;
8383ac51e74SDarrick J. Wong }
8393ac51e74SDarrick J. Wong 
8403ac51e74SDarrick J. Wong /*
8413ac51e74SDarrick J. Wong  * Set the geometry of a device.
8423ac51e74SDarrick J. Wong  */
8433ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
8443ac51e74SDarrick J. Wong {
8453ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
8463ac51e74SDarrick J. Wong 
8473ac51e74SDarrick J. Wong 	if (geo->start > sz) {
8483ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
8493ac51e74SDarrick J. Wong 		return -EINVAL;
8503ac51e74SDarrick J. Wong 	}
8513ac51e74SDarrick J. Wong 
8523ac51e74SDarrick J. Wong 	md->geometry = *geo;
8533ac51e74SDarrick J. Wong 
8543ac51e74SDarrick J. Wong 	return 0;
8553ac51e74SDarrick J. Wong }
8563ac51e74SDarrick J. Wong 
8572e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
8582e93ccc1SKiyoshi Ueda {
8592e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
8602e93ccc1SKiyoshi Ueda }
8612e93ccc1SKiyoshi Ueda 
8621da177e4SLinus Torvalds /*
8631da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
8641da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
8651da177e4SLinus Torvalds  */
8664e4cbee9SChristoph Hellwig static void dec_pending(struct dm_io *io, blk_status_t error)
8671da177e4SLinus Torvalds {
8682e93ccc1SKiyoshi Ueda 	unsigned long flags;
8694e4cbee9SChristoph Hellwig 	blk_status_t io_error;
870b35f8caaSMilan Broz 	struct bio *bio;
871b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
8722e93ccc1SKiyoshi Ueda 
8732e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
874f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
875f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
876745dc570SMike Snitzer 		if (!(io->status == BLK_STS_DM_REQUEUE && __noflush_suspending(md)))
8774e4cbee9SChristoph Hellwig 			io->status = error;
878f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
879f88fb981SKiyoshi Ueda 	}
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
8824e4cbee9SChristoph Hellwig 		if (io->status == BLK_STS_DM_REQUEUE) {
8832e93ccc1SKiyoshi Ueda 			/*
8842e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
8852e93ccc1SKiyoshi Ueda 			 */
886022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
8876a8736d1STejun Heo 			if (__noflush_suspending(md))
888745dc570SMike Snitzer 				/* NOTE early return due to BLK_STS_DM_REQUEUE below */
889745dc570SMike Snitzer 				bio_list_add_head(&md->deferred, io->orig_bio);
8906a8736d1STejun Heo 			else
8912e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
8924e4cbee9SChristoph Hellwig 				io->status = BLK_STS_IOERR;
893022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
8942e93ccc1SKiyoshi Ueda 		}
8952e93ccc1SKiyoshi Ueda 
8964e4cbee9SChristoph Hellwig 		io_error = io->status;
897745dc570SMike Snitzer 		bio = io->orig_bio;
898af7e466aSMikulas Patocka 		end_io_acct(io);
899a97f925aSMikulas Patocka 		free_io(md, io);
9001da177e4SLinus Torvalds 
9014e4cbee9SChristoph Hellwig 		if (io_error == BLK_STS_DM_REQUEUE)
9026a8736d1STejun Heo 			return;
9036a8736d1STejun Heo 
9041eff9d32SJens Axboe 		if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) {
9051da177e4SLinus Torvalds 			/*
9066a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
90728a8f0d3SMike Christie 			 * without REQ_PREFLUSH.
9081da177e4SLinus Torvalds 			 */
9091eff9d32SJens Axboe 			bio->bi_opf &= ~REQ_PREFLUSH;
9106a8736d1STejun Heo 			queue_io(md, bio);
911b35f8caaSMilan Broz 		} else {
912b372d360SMike Snitzer 			/* done with normal IO or empty flush */
9138dd601faSNeilBrown 			if (io_error)
9144e4cbee9SChristoph Hellwig 				bio->bi_status = io_error;
9154246a0b6SChristoph Hellwig 			bio_endio(bio);
9162e93ccc1SKiyoshi Ueda 		}
9171da177e4SLinus Torvalds 	}
918af7e466aSMikulas Patocka }
9191da177e4SLinus Torvalds 
9204cc96131SMike Snitzer void disable_write_same(struct mapped_device *md)
9217eee4ae2SMike Snitzer {
9227eee4ae2SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
9237eee4ae2SMike Snitzer 
9247eee4ae2SMike Snitzer 	/* device doesn't really support WRITE SAME, disable it */
9257eee4ae2SMike Snitzer 	limits->max_write_same_sectors = 0;
9267eee4ae2SMike Snitzer }
9277eee4ae2SMike Snitzer 
928ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md)
929ac62d620SChristoph Hellwig {
930ac62d620SChristoph Hellwig 	struct queue_limits *limits = dm_get_queue_limits(md);
931ac62d620SChristoph Hellwig 
932ac62d620SChristoph Hellwig 	/* device doesn't really support WRITE ZEROES, disable it */
933ac62d620SChristoph Hellwig 	limits->max_write_zeroes_sectors = 0;
934ac62d620SChristoph Hellwig }
935ac62d620SChristoph Hellwig 
9364246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio)
9371da177e4SLinus Torvalds {
9384e4cbee9SChristoph Hellwig 	blk_status_t error = bio->bi_status;
939bfc6d41cSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
940b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
9419faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
9421da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
9431da177e4SLinus Torvalds 
944978e51baSMike Snitzer 	if (unlikely(error == BLK_STS_TARGET) && md->type != DM_TYPE_NVME_BIO_BASED) {
945ac62d620SChristoph Hellwig 		if (bio_op(bio) == REQ_OP_WRITE_SAME &&
94674d46992SChristoph Hellwig 		    !bio->bi_disk->queue->limits.max_write_same_sectors)
9477eee4ae2SMike Snitzer 			disable_write_same(md);
948ac62d620SChristoph Hellwig 		if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
94974d46992SChristoph Hellwig 		    !bio->bi_disk->queue->limits.max_write_zeroes_sectors)
950ac62d620SChristoph Hellwig 			disable_write_zeroes(md);
951ac62d620SChristoph Hellwig 	}
9527eee4ae2SMike Snitzer 
9531be56909SChristoph Hellwig 	if (endio) {
9544e4cbee9SChristoph Hellwig 		int r = endio(tio->ti, bio, &error);
9551be56909SChristoph Hellwig 		switch (r) {
9561be56909SChristoph Hellwig 		case DM_ENDIO_REQUEUE:
9574e4cbee9SChristoph Hellwig 			error = BLK_STS_DM_REQUEUE;
9581be56909SChristoph Hellwig 			/*FALLTHRU*/
9591be56909SChristoph Hellwig 		case DM_ENDIO_DONE:
9601be56909SChristoph Hellwig 			break;
9611be56909SChristoph Hellwig 		case DM_ENDIO_INCOMPLETE:
9621be56909SChristoph Hellwig 			/* The target will handle the io */
9631be56909SChristoph Hellwig 			return;
9641be56909SChristoph Hellwig 		default:
9651be56909SChristoph Hellwig 			DMWARN("unimplemented target endio return value: %d", r);
9661be56909SChristoph Hellwig 			BUG();
9671be56909SChristoph Hellwig 		}
9681be56909SChristoph Hellwig 	}
9691be56909SChristoph Hellwig 
970cfae7529SMike Snitzer 	free_tio(tio);
971b35f8caaSMilan Broz 	dec_pending(io, error);
9721da177e4SLinus Torvalds }
9731da177e4SLinus Torvalds 
97478d8e58aSMike Snitzer /*
97556a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
97656a67df7SMike Snitzer  * target boundary.
97756a67df7SMike Snitzer  */
97856a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
9791da177e4SLinus Torvalds {
98056a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
98156a67df7SMike Snitzer 
98256a67df7SMike Snitzer 	return ti->len - target_offset;
98356a67df7SMike Snitzer }
98456a67df7SMike Snitzer 
98556a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
98656a67df7SMike Snitzer {
98756a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
988542f9038SMike Snitzer 	sector_t offset, max_len;
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 	/*
9911da177e4SLinus Torvalds 	 * Does the target need to split even further?
9921da177e4SLinus Torvalds 	 */
993542f9038SMike Snitzer 	if (ti->max_io_len) {
994542f9038SMike Snitzer 		offset = dm_target_offset(ti, sector);
995542f9038SMike Snitzer 		if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
996542f9038SMike Snitzer 			max_len = sector_div(offset, ti->max_io_len);
997542f9038SMike Snitzer 		else
998542f9038SMike Snitzer 			max_len = offset & (ti->max_io_len - 1);
999542f9038SMike Snitzer 		max_len = ti->max_io_len - max_len;
1000542f9038SMike Snitzer 
1001542f9038SMike Snitzer 		if (len > max_len)
1002542f9038SMike Snitzer 			len = max_len;
10031da177e4SLinus Torvalds 	}
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	return len;
10061da177e4SLinus Torvalds }
10071da177e4SLinus Torvalds 
1008542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1009542f9038SMike Snitzer {
1010542f9038SMike Snitzer 	if (len > UINT_MAX) {
1011542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1012542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
1013542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
1014542f9038SMike Snitzer 		return -EINVAL;
1015542f9038SMike Snitzer 	}
1016542f9038SMike Snitzer 
10178f50e358SMing Lei 	/*
10188f50e358SMing Lei 	 * BIO based queue uses its own splitting. When multipage bvecs
10198f50e358SMing Lei 	 * is switched on, size of the incoming bio may be too big to
10208f50e358SMing Lei 	 * be handled in some targets, such as crypt.
10218f50e358SMing Lei 	 *
10228f50e358SMing Lei 	 * When these targets are ready for the big bio, we can remove
10238f50e358SMing Lei 	 * the limit.
10248f50e358SMing Lei 	 */
10258f50e358SMing Lei 	ti->max_io_len = min_t(uint32_t, len, BIO_MAX_PAGES * PAGE_SIZE);
1026542f9038SMike Snitzer 
1027542f9038SMike Snitzer 	return 0;
1028542f9038SMike Snitzer }
1029542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1030542f9038SMike Snitzer 
1031f26c5719SDan Williams static struct dm_target *dm_dax_get_live_target(struct mapped_device *md,
1032f26c5719SDan Williams 		sector_t sector, int *srcu_idx)
1033545ed20eSToshi Kani {
1034545ed20eSToshi Kani 	struct dm_table *map;
1035545ed20eSToshi Kani 	struct dm_target *ti;
1036545ed20eSToshi Kani 
1037f26c5719SDan Williams 	map = dm_get_live_table(md, srcu_idx);
1038545ed20eSToshi Kani 	if (!map)
1039f26c5719SDan Williams 		return NULL;
1040545ed20eSToshi Kani 
1041545ed20eSToshi Kani 	ti = dm_table_find_target(map, sector);
1042545ed20eSToshi Kani 	if (!dm_target_is_valid(ti))
1043f26c5719SDan Williams 		return NULL;
1044f26c5719SDan Williams 
1045f26c5719SDan Williams 	return ti;
1046f26c5719SDan Williams }
1047f26c5719SDan Williams 
1048f26c5719SDan Williams static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
1049f26c5719SDan Williams 		long nr_pages, void **kaddr, pfn_t *pfn)
1050f26c5719SDan Williams {
1051f26c5719SDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
1052f26c5719SDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
1053f26c5719SDan Williams 	struct dm_target *ti;
1054f26c5719SDan Williams 	long len, ret = -EIO;
1055f26c5719SDan Williams 	int srcu_idx;
1056f26c5719SDan Williams 
1057f26c5719SDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1058f26c5719SDan Williams 
1059f26c5719SDan Williams 	if (!ti)
1060545ed20eSToshi Kani 		goto out;
1061f26c5719SDan Williams 	if (!ti->type->direct_access)
1062f26c5719SDan Williams 		goto out;
1063f26c5719SDan Williams 	len = max_io_len(sector, ti) / PAGE_SECTORS;
1064f26c5719SDan Williams 	if (len < 1)
1065f26c5719SDan Williams 		goto out;
1066f26c5719SDan Williams 	nr_pages = min(len, nr_pages);
1067545ed20eSToshi Kani 	if (ti->type->direct_access)
1068817bf402SDan Williams 		ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn);
1069817bf402SDan Williams 
1070545ed20eSToshi Kani  out:
1071545ed20eSToshi Kani 	dm_put_live_table(md, srcu_idx);
1072f26c5719SDan Williams 
1073f26c5719SDan Williams 	return ret;
1074545ed20eSToshi Kani }
1075545ed20eSToshi Kani 
10767e026c8cSDan Williams static size_t dm_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
10777e026c8cSDan Williams 		void *addr, size_t bytes, struct iov_iter *i)
10787e026c8cSDan Williams {
10797e026c8cSDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
10807e026c8cSDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
10817e026c8cSDan Williams 	struct dm_target *ti;
10827e026c8cSDan Williams 	long ret = 0;
10837e026c8cSDan Williams 	int srcu_idx;
10847e026c8cSDan Williams 
10857e026c8cSDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
10867e026c8cSDan Williams 
10877e026c8cSDan Williams 	if (!ti)
10887e026c8cSDan Williams 		goto out;
10897e026c8cSDan Williams 	if (!ti->type->dax_copy_from_iter) {
10907e026c8cSDan Williams 		ret = copy_from_iter(addr, bytes, i);
10917e026c8cSDan Williams 		goto out;
10927e026c8cSDan Williams 	}
10937e026c8cSDan Williams 	ret = ti->type->dax_copy_from_iter(ti, pgoff, addr, bytes, i);
10947e026c8cSDan Williams  out:
10957e026c8cSDan Williams 	dm_put_live_table(md, srcu_idx);
10967e026c8cSDan Williams 
10977e026c8cSDan Williams 	return ret;
10987e026c8cSDan Williams }
10997e026c8cSDan Williams 
11001dd40c3eSMikulas Patocka /*
11011dd40c3eSMikulas Patocka  * A target may call dm_accept_partial_bio only from the map routine.  It is
1102c06b3e58SNeilBrown  * allowed for all bio types except REQ_PREFLUSH and REQ_OP_ZONE_RESET.
11031dd40c3eSMikulas Patocka  *
11041dd40c3eSMikulas Patocka  * dm_accept_partial_bio informs the dm that the target only wants to process
11051dd40c3eSMikulas Patocka  * additional n_sectors sectors of the bio and the rest of the data should be
11061dd40c3eSMikulas Patocka  * sent in a next bio.
11071dd40c3eSMikulas Patocka  *
11081dd40c3eSMikulas Patocka  * A diagram that explains the arithmetics:
11091dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
11101dd40c3eSMikulas Patocka  * |         1          |       2       |   3   |
11111dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
11121dd40c3eSMikulas Patocka  *
11131dd40c3eSMikulas Patocka  * <-------------- *tio->len_ptr --------------->
11141dd40c3eSMikulas Patocka  *                      <------- bi_size ------->
11151dd40c3eSMikulas Patocka  *                      <-- n_sectors -->
11161dd40c3eSMikulas Patocka  *
11171dd40c3eSMikulas Patocka  * Region 1 was already iterated over with bio_advance or similar function.
11181dd40c3eSMikulas Patocka  *	(it may be empty if the target doesn't use bio_advance)
11191dd40c3eSMikulas Patocka  * Region 2 is the remaining bio size that the target wants to process.
11201dd40c3eSMikulas Patocka  *	(it may be empty if region 1 is non-empty, although there is no reason
11211dd40c3eSMikulas Patocka  *	 to make it empty)
11221dd40c3eSMikulas Patocka  * The target requires that region 3 is to be sent in the next bio.
11231dd40c3eSMikulas Patocka  *
11241dd40c3eSMikulas Patocka  * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
11251dd40c3eSMikulas Patocka  * the partially processed part (the sum of regions 1+2) must be the same for all
11261dd40c3eSMikulas Patocka  * copies of the bio.
11271dd40c3eSMikulas Patocka  */
11281dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
11291dd40c3eSMikulas Patocka {
11301dd40c3eSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
11311dd40c3eSMikulas Patocka 	unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
11321eff9d32SJens Axboe 	BUG_ON(bio->bi_opf & REQ_PREFLUSH);
11331dd40c3eSMikulas Patocka 	BUG_ON(bi_size > *tio->len_ptr);
11341dd40c3eSMikulas Patocka 	BUG_ON(n_sectors > bi_size);
11351dd40c3eSMikulas Patocka 	*tio->len_ptr -= bi_size - n_sectors;
11361dd40c3eSMikulas Patocka 	bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
11371dd40c3eSMikulas Patocka }
11381dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
11391dd40c3eSMikulas Patocka 
1140d67a5f4bSMikulas Patocka /*
114110999307SDamien Le Moal  * The zone descriptors obtained with a zone report indicate
114210999307SDamien Le Moal  * zone positions within the target device. The zone descriptors
114310999307SDamien Le Moal  * must be remapped to match their position within the dm device.
114410999307SDamien Le Moal  * A target may call dm_remap_zone_report after completion of a
114510999307SDamien Le Moal  * REQ_OP_ZONE_REPORT bio to remap the zone descriptors obtained
114610999307SDamien Le Moal  * from the target device mapping to the dm device.
114710999307SDamien Le Moal  */
114810999307SDamien Le Moal void dm_remap_zone_report(struct dm_target *ti, struct bio *bio, sector_t start)
114910999307SDamien Le Moal {
115010999307SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
115110999307SDamien Le Moal 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1152745dc570SMike Snitzer 	struct bio *report_bio = tio->io->orig_bio;
115310999307SDamien Le Moal 	struct blk_zone_report_hdr *hdr = NULL;
115410999307SDamien Le Moal 	struct blk_zone *zone;
115510999307SDamien Le Moal 	unsigned int nr_rep = 0;
115610999307SDamien Le Moal 	unsigned int ofst;
115710999307SDamien Le Moal 	struct bio_vec bvec;
115810999307SDamien Le Moal 	struct bvec_iter iter;
115910999307SDamien Le Moal 	void *addr;
116010999307SDamien Le Moal 
116110999307SDamien Le Moal 	if (bio->bi_status)
116210999307SDamien Le Moal 		return;
116310999307SDamien Le Moal 
116410999307SDamien Le Moal 	/*
116510999307SDamien Le Moal 	 * Remap the start sector of the reported zones. For sequential zones,
116610999307SDamien Le Moal 	 * also remap the write pointer position.
116710999307SDamien Le Moal 	 */
116810999307SDamien Le Moal 	bio_for_each_segment(bvec, report_bio, iter) {
116910999307SDamien Le Moal 		addr = kmap_atomic(bvec.bv_page);
117010999307SDamien Le Moal 
117110999307SDamien Le Moal 		/* Remember the report header in the first page */
117210999307SDamien Le Moal 		if (!hdr) {
117310999307SDamien Le Moal 			hdr = addr;
117410999307SDamien Le Moal 			ofst = sizeof(struct blk_zone_report_hdr);
117510999307SDamien Le Moal 		} else
117610999307SDamien Le Moal 			ofst = 0;
117710999307SDamien Le Moal 
117810999307SDamien Le Moal 		/* Set zones start sector */
117910999307SDamien Le Moal 		while (hdr->nr_zones && ofst < bvec.bv_len) {
118010999307SDamien Le Moal 			zone = addr + ofst;
118110999307SDamien Le Moal 			if (zone->start >= start + ti->len) {
118210999307SDamien Le Moal 				hdr->nr_zones = 0;
118310999307SDamien Le Moal 				break;
118410999307SDamien Le Moal 			}
118510999307SDamien Le Moal 			zone->start = zone->start + ti->begin - start;
118610999307SDamien Le Moal 			if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL) {
118710999307SDamien Le Moal 				if (zone->cond == BLK_ZONE_COND_FULL)
118810999307SDamien Le Moal 					zone->wp = zone->start + zone->len;
118910999307SDamien Le Moal 				else if (zone->cond == BLK_ZONE_COND_EMPTY)
119010999307SDamien Le Moal 					zone->wp = zone->start;
119110999307SDamien Le Moal 				else
119210999307SDamien Le Moal 					zone->wp = zone->wp + ti->begin - start;
119310999307SDamien Le Moal 			}
119410999307SDamien Le Moal 			ofst += sizeof(struct blk_zone);
119510999307SDamien Le Moal 			hdr->nr_zones--;
119610999307SDamien Le Moal 			nr_rep++;
119710999307SDamien Le Moal 		}
119810999307SDamien Le Moal 
119910999307SDamien Le Moal 		if (addr != hdr)
120010999307SDamien Le Moal 			kunmap_atomic(addr);
120110999307SDamien Le Moal 
120210999307SDamien Le Moal 		if (!hdr->nr_zones)
120310999307SDamien Le Moal 			break;
120410999307SDamien Le Moal 	}
120510999307SDamien Le Moal 
120610999307SDamien Le Moal 	if (hdr) {
120710999307SDamien Le Moal 		hdr->nr_zones = nr_rep;
120810999307SDamien Le Moal 		kunmap_atomic(hdr);
120910999307SDamien Le Moal 	}
121010999307SDamien Le Moal 
121110999307SDamien Le Moal 	bio_advance(report_bio, report_bio->bi_iter.bi_size);
121210999307SDamien Le Moal 
121310999307SDamien Le Moal #else /* !CONFIG_BLK_DEV_ZONED */
121410999307SDamien Le Moal 	bio->bi_status = BLK_STS_NOTSUPP;
121510999307SDamien Le Moal #endif
121610999307SDamien Le Moal }
121710999307SDamien Le Moal EXPORT_SYMBOL_GPL(dm_remap_zone_report);
121810999307SDamien Le Moal 
1219978e51baSMike Snitzer static blk_qc_t __map_bio(struct dm_target_io *tio)
12201da177e4SLinus Torvalds {
12211da177e4SLinus Torvalds 	int r;
12222056a782SJens Axboe 	sector_t sector;
1223dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
122464f52b0eSMike Snitzer 	struct dm_io *io = tio->io;
1225978e51baSMike Snitzer 	struct mapped_device *md = io->md;
1226bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
1227978e51baSMike Snitzer 	blk_qc_t ret = BLK_QC_T_NONE;
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 	/*
12321da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
12331da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
12341da177e4SLinus Torvalds 	 * this io.
12351da177e4SLinus Torvalds 	 */
123664f52b0eSMike Snitzer 	atomic_inc(&io->io_count);
12374f024f37SKent Overstreet 	sector = clone->bi_iter.bi_sector;
1238d67a5f4bSMikulas Patocka 
12397de3ee57SMikulas Patocka 	r = ti->type->map(ti, clone);
1240846785e6SChristoph Hellwig 	switch (r) {
1241846785e6SChristoph Hellwig 	case DM_MAPIO_SUBMITTED:
1242846785e6SChristoph Hellwig 		break;
1243846785e6SChristoph Hellwig 	case DM_MAPIO_REMAPPED:
12441da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
124574d46992SChristoph Hellwig 		trace_block_bio_remap(clone->bi_disk->queue, clone,
124664f52b0eSMike Snitzer 				      bio_dev(io->orig_bio), sector);
1247978e51baSMike Snitzer 		if (md->type == DM_TYPE_NVME_BIO_BASED)
1248978e51baSMike Snitzer 			ret = direct_make_request(clone);
1249978e51baSMike Snitzer 		else
1250978e51baSMike Snitzer 			ret = generic_make_request(clone);
1251846785e6SChristoph Hellwig 		break;
1252846785e6SChristoph Hellwig 	case DM_MAPIO_KILL:
12534e4cbee9SChristoph Hellwig 		free_tio(tio);
125464f52b0eSMike Snitzer 		dec_pending(io, BLK_STS_IOERR);
12554e4cbee9SChristoph Hellwig 		break;
1256846785e6SChristoph Hellwig 	case DM_MAPIO_REQUEUE:
1257cfae7529SMike Snitzer 		free_tio(tio);
125864f52b0eSMike Snitzer 		dec_pending(io, BLK_STS_DM_REQUEUE);
1259846785e6SChristoph Hellwig 		break;
1260846785e6SChristoph Hellwig 	default:
126145cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
126245cbcd79SKiyoshi Ueda 		BUG();
12631da177e4SLinus Torvalds 	}
12641da177e4SLinus Torvalds 
1265978e51baSMike Snitzer 	return ret;
12661da177e4SLinus Torvalds }
12671da177e4SLinus Torvalds 
1268e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
1269bd2a49b8SAlasdair G Kergon {
12704f024f37SKent Overstreet 	bio->bi_iter.bi_sector = sector;
12714f024f37SKent Overstreet 	bio->bi_iter.bi_size = to_bytes(len);
12721da177e4SLinus Torvalds }
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds /*
12751da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
12761da177e4SLinus Torvalds  */
1277c80914e8SMike Snitzer static int clone_bio(struct dm_target_io *tio, struct bio *bio,
12781c3b13e6SKent Overstreet 		     sector_t sector, unsigned len)
12791da177e4SLinus Torvalds {
1280dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
12811da177e4SLinus Torvalds 
12821c3b13e6SKent Overstreet 	__bio_clone_fast(clone, bio);
12839c47008dSMartin K. Petersen 
1284e2460f2aSMikulas Patocka 	if (unlikely(bio_integrity(bio) != NULL)) {
1285e2460f2aSMikulas Patocka 		int r;
1286e2460f2aSMikulas Patocka 
1287e2460f2aSMikulas Patocka 		if (unlikely(!dm_target_has_integrity(tio->ti->type) &&
1288e2460f2aSMikulas Patocka 			     !dm_target_passes_integrity(tio->ti->type))) {
1289e2460f2aSMikulas Patocka 			DMWARN("%s: the target %s doesn't support integrity data.",
1290e2460f2aSMikulas Patocka 				dm_device_name(tio->io->md),
1291e2460f2aSMikulas Patocka 				tio->ti->type->name);
1292e2460f2aSMikulas Patocka 			return -EIO;
1293e2460f2aSMikulas Patocka 		}
1294e2460f2aSMikulas Patocka 
1295e2460f2aSMikulas Patocka 		r = bio_integrity_clone(clone, bio, GFP_NOIO);
1296c80914e8SMike Snitzer 		if (r < 0)
1297c80914e8SMike Snitzer 			return r;
1298c80914e8SMike Snitzer 	}
12991c3b13e6SKent Overstreet 
1300264c869dSDamien Le Moal 	if (bio_op(bio) != REQ_OP_ZONE_REPORT)
13011c3b13e6SKent Overstreet 		bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
13021c3b13e6SKent Overstreet 	clone->bi_iter.bi_size = to_bytes(len);
13031c3b13e6SKent Overstreet 
1304e2460f2aSMikulas Patocka 	if (unlikely(bio_integrity(bio) != NULL))
1305fbd08e76SDmitry Monakhov 		bio_integrity_trim(clone);
1306c80914e8SMike Snitzer 
1307c80914e8SMike Snitzer 	return 0;
13081da177e4SLinus Torvalds }
13091da177e4SLinus Torvalds 
1310318716ddSMike Snitzer static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci,
1311318716ddSMike Snitzer 				struct dm_target *ti, unsigned num_bios)
1312f9ab94ceSMikulas Patocka {
1313dba14160SMikulas Patocka 	struct dm_target_io *tio;
1314318716ddSMike Snitzer 	int try;
1315dba14160SMikulas Patocka 
1316318716ddSMike Snitzer 	if (!num_bios)
1317318716ddSMike Snitzer 		return;
1318f9ab94ceSMikulas Patocka 
1319318716ddSMike Snitzer 	if (num_bios == 1) {
1320318716ddSMike Snitzer 		tio = alloc_tio(ci, ti, 0, GFP_NOIO);
1321318716ddSMike Snitzer 		bio_list_add(blist, &tio->clone);
1322318716ddSMike Snitzer 		return;
13239015df24SAlasdair G Kergon 	}
13249015df24SAlasdair G Kergon 
1325318716ddSMike Snitzer 	for (try = 0; try < 2; try++) {
1326318716ddSMike Snitzer 		int bio_nr;
1327318716ddSMike Snitzer 		struct bio *bio;
1328318716ddSMike Snitzer 
1329318716ddSMike Snitzer 		if (try)
1330bc02cdbeSMike Snitzer 			mutex_lock(&ci->io->md->table_devices_lock);
1331318716ddSMike Snitzer 		for (bio_nr = 0; bio_nr < num_bios; bio_nr++) {
1332318716ddSMike Snitzer 			tio = alloc_tio(ci, ti, bio_nr, try ? GFP_NOIO : GFP_NOWAIT);
1333318716ddSMike Snitzer 			if (!tio)
1334318716ddSMike Snitzer 				break;
1335318716ddSMike Snitzer 
1336318716ddSMike Snitzer 			bio_list_add(blist, &tio->clone);
1337318716ddSMike Snitzer 		}
1338318716ddSMike Snitzer 		if (try)
1339bc02cdbeSMike Snitzer 			mutex_unlock(&ci->io->md->table_devices_lock);
1340318716ddSMike Snitzer 		if (bio_nr == num_bios)
1341318716ddSMike Snitzer 			return;
1342318716ddSMike Snitzer 
1343318716ddSMike Snitzer 		while ((bio = bio_list_pop(blist))) {
1344318716ddSMike Snitzer 			tio = container_of(bio, struct dm_target_io, clone);
1345318716ddSMike Snitzer 			free_tio(tio);
1346318716ddSMike Snitzer 		}
1347318716ddSMike Snitzer 	}
1348318716ddSMike Snitzer }
1349318716ddSMike Snitzer 
1350978e51baSMike Snitzer static blk_qc_t __clone_and_map_simple_bio(struct clone_info *ci,
1351318716ddSMike Snitzer 					   struct dm_target_io *tio, unsigned *len)
13529015df24SAlasdair G Kergon {
1353dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
13549015df24SAlasdair G Kergon 
13551dd40c3eSMikulas Patocka 	tio->len_ptr = len;
13561dd40c3eSMikulas Patocka 
13571c3b13e6SKent Overstreet 	__bio_clone_fast(clone, ci->bio);
1358bd2a49b8SAlasdair G Kergon 	if (len)
13591dd40c3eSMikulas Patocka 		bio_setup_sector(clone, ci->sector, *len);
1360f9ab94ceSMikulas Patocka 
1361978e51baSMike Snitzer 	return __map_bio(tio);
1362f9ab94ceSMikulas Patocka }
1363f9ab94ceSMikulas Patocka 
136414fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
13651dd40c3eSMikulas Patocka 				  unsigned num_bios, unsigned *len)
136606a426ceSMike Snitzer {
1367318716ddSMike Snitzer 	struct bio_list blist = BIO_EMPTY_LIST;
1368318716ddSMike Snitzer 	struct bio *bio;
1369318716ddSMike Snitzer 	struct dm_target_io *tio;
137006a426ceSMike Snitzer 
1371318716ddSMike Snitzer 	alloc_multiple_bios(&blist, ci, ti, num_bios);
1372318716ddSMike Snitzer 
1373318716ddSMike Snitzer 	while ((bio = bio_list_pop(&blist))) {
1374318716ddSMike Snitzer 		tio = container_of(bio, struct dm_target_io, clone);
1375978e51baSMike Snitzer 		(void) __clone_and_map_simple_bio(ci, tio, len);
1376318716ddSMike Snitzer 	}
137706a426ceSMike Snitzer }
137806a426ceSMike Snitzer 
137914fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1380f9ab94ceSMikulas Patocka {
138106a426ceSMike Snitzer 	unsigned target_nr = 0;
1382f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1383f9ab94ceSMikulas Patocka 
1384b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1385f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
13861dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
1387f9ab94ceSMikulas Patocka 
1388f9ab94ceSMikulas Patocka 	return 0;
1389f9ab94ceSMikulas Patocka }
1390f9ab94ceSMikulas Patocka 
1391c80914e8SMike Snitzer static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
13921dd40c3eSMikulas Patocka 				    sector_t sector, unsigned *len)
13935ae89a87SMike Snitzer {
1394dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
13955ae89a87SMike Snitzer 	struct dm_target_io *tio;
1396f31c21e4SNeilBrown 	int r;
13975ae89a87SMike Snitzer 
1398318716ddSMike Snitzer 	tio = alloc_tio(ci, ti, 0, GFP_NOIO);
13991dd40c3eSMikulas Patocka 	tio->len_ptr = len;
1400c80914e8SMike Snitzer 	r = clone_bio(tio, bio, sector, *len);
1401072623deSMikulas Patocka 	if (r < 0) {
1402cfae7529SMike Snitzer 		free_tio(tio);
1403c80914e8SMike Snitzer 		return r;
1404b0d8ed4dSAlasdair G Kergon 	}
1405978e51baSMike Snitzer 	(void) __map_bio(tio);
140655a62eefSAlasdair G Kergon 
1407f31c21e4SNeilBrown 	return 0;
140823508a96SMike Snitzer }
140955a62eefSAlasdair G Kergon 
141023508a96SMike Snitzer typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
141155a62eefSAlasdair G Kergon 
141223508a96SMike Snitzer static unsigned get_num_discard_bios(struct dm_target *ti)
141323508a96SMike Snitzer {
141423508a96SMike Snitzer 	return ti->num_discard_bios;
141523508a96SMike Snitzer }
141623508a96SMike Snitzer 
1417*00716545SDenis Semakin static unsigned get_num_secure_erase_bios(struct dm_target *ti)
1418*00716545SDenis Semakin {
1419*00716545SDenis Semakin 	return ti->num_secure_erase_bios;
1420*00716545SDenis Semakin }
1421*00716545SDenis Semakin 
142223508a96SMike Snitzer static unsigned get_num_write_same_bios(struct dm_target *ti)
142323508a96SMike Snitzer {
142423508a96SMike Snitzer 	return ti->num_write_same_bios;
142523508a96SMike Snitzer }
142623508a96SMike Snitzer 
1427ac62d620SChristoph Hellwig static unsigned get_num_write_zeroes_bios(struct dm_target *ti)
1428ac62d620SChristoph Hellwig {
1429ac62d620SChristoph Hellwig 	return ti->num_write_zeroes_bios;
1430ac62d620SChristoph Hellwig }
1431ac62d620SChristoph Hellwig 
143223508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti);
143323508a96SMike Snitzer 
143423508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti)
143523508a96SMike Snitzer {
143655a62eefSAlasdair G Kergon 	return ti->split_discard_bios;
143723508a96SMike Snitzer }
143823508a96SMike Snitzer 
14393d7f4562SMike Snitzer static int __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti,
144055a62eefSAlasdair G Kergon 				       get_num_bios_fn get_num_bios,
144123508a96SMike Snitzer 				       is_split_required_fn is_split_required)
14425ae89a87SMike Snitzer {
1443e0d6609aSMikulas Patocka 	unsigned len;
144455a62eefSAlasdair G Kergon 	unsigned num_bios;
14455ae89a87SMike Snitzer 
14465ae89a87SMike Snitzer 	/*
144723508a96SMike Snitzer 	 * Even though the device advertised support for this type of
144823508a96SMike Snitzer 	 * request, that does not mean every target supports it, and
1449936688d7SMike Snitzer 	 * reconfiguration might also have changed that since the
14505ae89a87SMike Snitzer 	 * check was performed.
14515ae89a87SMike Snitzer 	 */
145255a62eefSAlasdair G Kergon 	num_bios = get_num_bios ? get_num_bios(ti) : 0;
145355a62eefSAlasdair G Kergon 	if (!num_bios)
14545ae89a87SMike Snitzer 		return -EOPNOTSUPP;
14555ae89a87SMike Snitzer 
145623508a96SMike Snitzer 	if (is_split_required && !is_split_required(ti))
1457e0d6609aSMikulas Patocka 		len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
14587acf0277SMikulas Patocka 	else
1459e0d6609aSMikulas Patocka 		len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
14605ae89a87SMike Snitzer 
14611dd40c3eSMikulas Patocka 	__send_duplicate_bios(ci, ti, num_bios, &len);
14625ae89a87SMike Snitzer 
1463a79245b3SMike Snitzer 	ci->sector += len;
14643d7f4562SMike Snitzer 	ci->sector_count -= len;
14655ae89a87SMike Snitzer 
14665ae89a87SMike Snitzer 	return 0;
14675ae89a87SMike Snitzer }
14685ae89a87SMike Snitzer 
14693d7f4562SMike Snitzer static int __send_discard(struct clone_info *ci, struct dm_target *ti)
147023508a96SMike Snitzer {
14713d7f4562SMike Snitzer 	return __send_changing_extent_only(ci, ti, get_num_discard_bios,
147223508a96SMike Snitzer 					   is_split_required_for_discard);
147323508a96SMike Snitzer }
147423508a96SMike Snitzer 
1475*00716545SDenis Semakin static int __send_secure_erase(struct clone_info *ci, struct dm_target *ti)
1476*00716545SDenis Semakin {
1477*00716545SDenis Semakin 	return __send_changing_extent_only(ci, ti, get_num_secure_erase_bios, NULL);
1478*00716545SDenis Semakin }
1479*00716545SDenis Semakin 
14803d7f4562SMike Snitzer static int __send_write_same(struct clone_info *ci, struct dm_target *ti)
148123508a96SMike Snitzer {
14823d7f4562SMike Snitzer 	return __send_changing_extent_only(ci, ti, get_num_write_same_bios, NULL);
148323508a96SMike Snitzer }
148423508a96SMike Snitzer 
14853d7f4562SMike Snitzer static int __send_write_zeroes(struct clone_info *ci, struct dm_target *ti)
1486ac62d620SChristoph Hellwig {
14873d7f4562SMike Snitzer 	return __send_changing_extent_only(ci, ti, get_num_write_zeroes_bios, NULL);
1488ac62d620SChristoph Hellwig }
1489ac62d620SChristoph Hellwig 
14900519c71eSMike Snitzer static bool __process_abnormal_io(struct clone_info *ci, struct dm_target *ti,
14910519c71eSMike Snitzer 				  int *result)
14920519c71eSMike Snitzer {
14930519c71eSMike Snitzer 	struct bio *bio = ci->bio;
14940519c71eSMike Snitzer 
14950519c71eSMike Snitzer 	if (bio_op(bio) == REQ_OP_DISCARD)
14960519c71eSMike Snitzer 		*result = __send_discard(ci, ti);
1497*00716545SDenis Semakin 	else if (bio_op(bio) == REQ_OP_SECURE_ERASE)
1498*00716545SDenis Semakin 		*result = __send_secure_erase(ci, ti);
14990519c71eSMike Snitzer 	else if (bio_op(bio) == REQ_OP_WRITE_SAME)
15000519c71eSMike Snitzer 		*result = __send_write_same(ci, ti);
15010519c71eSMike Snitzer 	else if (bio_op(bio) == REQ_OP_WRITE_ZEROES)
15020519c71eSMike Snitzer 		*result = __send_write_zeroes(ci, ti);
15030519c71eSMike Snitzer 	else
15040519c71eSMike Snitzer 		return false;
15050519c71eSMike Snitzer 
15060519c71eSMike Snitzer 	return true;
15070519c71eSMike Snitzer }
15080519c71eSMike Snitzer 
1509e4c93811SAlasdair G Kergon /*
1510e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1511e4c93811SAlasdair G Kergon  */
1512e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci)
1513e4c93811SAlasdair G Kergon {
1514e4c93811SAlasdair G Kergon 	struct bio *bio = ci->bio;
1515e4c93811SAlasdair G Kergon 	struct dm_target *ti;
15161c3b13e6SKent Overstreet 	unsigned len;
1517c80914e8SMike Snitzer 	int r;
1518e4c93811SAlasdair G Kergon 
1519e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1520e4c93811SAlasdair G Kergon 	if (!dm_target_is_valid(ti))
1521e4c93811SAlasdair G Kergon 		return -EIO;
1522e4c93811SAlasdair G Kergon 
15230519c71eSMike Snitzer 	if (unlikely(__process_abnormal_io(ci, ti, &r)))
15240519c71eSMike Snitzer 		return r;
15253d7f4562SMike Snitzer 
1526264c869dSDamien Le Moal 	if (bio_op(bio) == REQ_OP_ZONE_REPORT)
1527264c869dSDamien Le Moal 		len = ci->sector_count;
1528264c869dSDamien Le Moal 	else
1529264c869dSDamien Le Moal 		len = min_t(sector_t, max_io_len(ci->sector, ti),
1530264c869dSDamien Le Moal 			    ci->sector_count);
1531e4c93811SAlasdair G Kergon 
1532c80914e8SMike Snitzer 	r = __clone_and_map_data_bio(ci, ti, ci->sector, &len);
1533c80914e8SMike Snitzer 	if (r < 0)
1534c80914e8SMike Snitzer 		return r;
1535e4c93811SAlasdair G Kergon 
1536e4c93811SAlasdair G Kergon 	ci->sector += len;
1537e4c93811SAlasdair G Kergon 	ci->sector_count -= len;
1538e4c93811SAlasdair G Kergon 
1539e4c93811SAlasdair G Kergon 	return 0;
1540e4c93811SAlasdair G Kergon }
1541e4c93811SAlasdair G Kergon 
1542978e51baSMike Snitzer static void init_clone_info(struct clone_info *ci, struct mapped_device *md,
1543978e51baSMike Snitzer 			    struct dm_table *map, struct bio *bio)
1544978e51baSMike Snitzer {
1545978e51baSMike Snitzer 	ci->map = map;
1546978e51baSMike Snitzer 	ci->io = alloc_io(md, bio);
1547978e51baSMike Snitzer 	ci->sector = bio->bi_iter.bi_sector;
1548978e51baSMike Snitzer }
1549978e51baSMike Snitzer 
1550e4c93811SAlasdair G Kergon /*
155114fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
15521da177e4SLinus Torvalds  */
1553978e51baSMike Snitzer static blk_qc_t __split_and_process_bio(struct mapped_device *md,
155483d5e5b0SMikulas Patocka 					struct dm_table *map, struct bio *bio)
15551da177e4SLinus Torvalds {
15561da177e4SLinus Torvalds 	struct clone_info ci;
1557978e51baSMike Snitzer 	blk_qc_t ret = BLK_QC_T_NONE;
1558512875bdSJun'ichi Nomura 	int error = 0;
15591da177e4SLinus Torvalds 
156083d5e5b0SMikulas Patocka 	if (unlikely(!map)) {
1561f0b9a450SMikulas Patocka 		bio_io_error(bio);
1562978e51baSMike Snitzer 		return ret;
1563f0b9a450SMikulas Patocka 	}
1564692d0eb9SMikulas Patocka 
1565978e51baSMike Snitzer 	init_clone_info(&ci, md, map, bio);
1566bd2a49b8SAlasdair G Kergon 
15671eff9d32SJens Axboe 	if (bio->bi_opf & REQ_PREFLUSH) {
1568bc02cdbeSMike Snitzer 		ci.bio = &ci.io->md->flush_bio;
1569b372d360SMike Snitzer 		ci.sector_count = 0;
157014fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1571b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1572a4aa5e56SDamien Le Moal 	} else if (bio_op(bio) == REQ_OP_ZONE_RESET) {
1573a4aa5e56SDamien Le Moal 		ci.bio = bio;
1574a4aa5e56SDamien Le Moal 		ci.sector_count = 0;
1575a4aa5e56SDamien Le Moal 		error = __split_and_process_non_flush(&ci);
1576b372d360SMike Snitzer 	} else {
15776a8736d1STejun Heo 		ci.bio = bio;
15781da177e4SLinus Torvalds 		ci.sector_count = bio_sectors(bio);
157918a25da8SNeilBrown 		while (ci.sector_count && !error) {
158014fe594dSAlasdair G Kergon 			error = __split_and_process_non_flush(&ci);
158118a25da8SNeilBrown 			if (current->bio_list && ci.sector_count && !error) {
158218a25da8SNeilBrown 				/*
158318a25da8SNeilBrown 				 * Remainder must be passed to generic_make_request()
158418a25da8SNeilBrown 				 * so that it gets handled *after* bios already submitted
158518a25da8SNeilBrown 				 * have been completely processed.
158618a25da8SNeilBrown 				 * We take a clone of the original to store in
1587745dc570SMike Snitzer 				 * ci.io->orig_bio to be used by end_io_acct() and
158818a25da8SNeilBrown 				 * for dec_pending to use for completion handling.
158918a25da8SNeilBrown 				 * As this path is not used for REQ_OP_ZONE_REPORT,
1590745dc570SMike Snitzer 				 * the usage of io->orig_bio in dm_remap_zone_report()
159118a25da8SNeilBrown 				 * won't be affected by this reassignment.
159218a25da8SNeilBrown 				 */
159318a25da8SNeilBrown 				struct bio *b = bio_clone_bioset(bio, GFP_NOIO,
159418a25da8SNeilBrown 								 md->queue->bio_split);
1595745dc570SMike Snitzer 				ci.io->orig_bio = b;
159618a25da8SNeilBrown 				bio_advance(bio, (bio_sectors(bio) - ci.sector_count) << 9);
159718a25da8SNeilBrown 				bio_chain(b, bio);
1598978e51baSMike Snitzer 				ret = generic_make_request(bio);
159918a25da8SNeilBrown 				break;
160018a25da8SNeilBrown 			}
160118a25da8SNeilBrown 		}
1602d87f4c14STejun Heo 	}
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 	/* drop the extra reference count */
160554385bf7SBart Van Assche 	dec_pending(ci.io, errno_to_blk_status(error));
1606978e51baSMike Snitzer 	return ret;
16071da177e4SLinus Torvalds }
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds /*
1610978e51baSMike Snitzer  * Optimized variant of __split_and_process_bio that leverages the
1611978e51baSMike Snitzer  * fact that targets that use it do _not_ have a need to split bios.
16121da177e4SLinus Torvalds  */
1613978e51baSMike Snitzer static blk_qc_t __process_bio(struct mapped_device *md,
1614978e51baSMike Snitzer 			      struct dm_table *map, struct bio *bio)
16151da177e4SLinus Torvalds {
1616978e51baSMike Snitzer 	struct clone_info ci;
1617978e51baSMike Snitzer 	blk_qc_t ret = BLK_QC_T_NONE;
1618978e51baSMike Snitzer 	int error = 0;
1619978e51baSMike Snitzer 
1620978e51baSMike Snitzer 	if (unlikely(!map)) {
1621978e51baSMike Snitzer 		bio_io_error(bio);
1622978e51baSMike Snitzer 		return ret;
1623978e51baSMike Snitzer 	}
1624978e51baSMike Snitzer 
1625978e51baSMike Snitzer 	init_clone_info(&ci, md, map, bio);
1626978e51baSMike Snitzer 
1627978e51baSMike Snitzer 	if (bio->bi_opf & REQ_PREFLUSH) {
1628978e51baSMike Snitzer 		ci.bio = &ci.io->md->flush_bio;
1629978e51baSMike Snitzer 		ci.sector_count = 0;
1630978e51baSMike Snitzer 		error = __send_empty_flush(&ci);
1631978e51baSMike Snitzer 		/* dec_pending submits any data associated with flush */
1632978e51baSMike Snitzer 	} else {
1633978e51baSMike Snitzer 		struct dm_target *ti = md->immutable_target;
1634978e51baSMike Snitzer 		struct dm_target_io *tio;
1635978e51baSMike Snitzer 
1636978e51baSMike Snitzer 		/*
1637978e51baSMike Snitzer 		 * Defend against IO still getting in during teardown
1638978e51baSMike Snitzer 		 * - as was seen for a time with nvme-fcloop
1639978e51baSMike Snitzer 		 */
1640978e51baSMike Snitzer 		if (unlikely(WARN_ON_ONCE(!ti || !dm_target_is_valid(ti)))) {
1641978e51baSMike Snitzer 			error = -EIO;
1642978e51baSMike Snitzer 			goto out;
1643978e51baSMike Snitzer 		}
1644978e51baSMike Snitzer 
1645978e51baSMike Snitzer 		ci.bio = bio;
1646978e51baSMike Snitzer 		ci.sector_count = bio_sectors(bio);
16470519c71eSMike Snitzer 		if (unlikely(__process_abnormal_io(&ci, ti, &error)))
16480519c71eSMike Snitzer 			goto out;
16490519c71eSMike Snitzer 
16500519c71eSMike Snitzer 		tio = alloc_tio(&ci, ti, 0, GFP_NOIO);
1651978e51baSMike Snitzer 		ret = __clone_and_map_simple_bio(&ci, tio, NULL);
1652978e51baSMike Snitzer 	}
1653978e51baSMike Snitzer out:
1654978e51baSMike Snitzer 	/* drop the extra reference count */
1655978e51baSMike Snitzer 	dec_pending(ci.io, errno_to_blk_status(error));
1656978e51baSMike Snitzer 	return ret;
1657978e51baSMike Snitzer }
1658978e51baSMike Snitzer 
1659978e51baSMike Snitzer typedef blk_qc_t (process_bio_fn)(struct mapped_device *, struct dm_table *, struct bio *);
1660978e51baSMike Snitzer 
1661978e51baSMike Snitzer static blk_qc_t __dm_make_request(struct request_queue *q, struct bio *bio,
1662978e51baSMike Snitzer 				  process_bio_fn process_bio)
16631da177e4SLinus Torvalds {
16641da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
1665978e51baSMike Snitzer 	blk_qc_t ret = BLK_QC_T_NONE;
166683d5e5b0SMikulas Patocka 	int srcu_idx;
166783d5e5b0SMikulas Patocka 	struct dm_table *map;
16681da177e4SLinus Torvalds 
166983d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
16701da177e4SLinus Torvalds 
16716a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
16726a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
167383d5e5b0SMikulas Patocka 		dm_put_live_table(md, srcu_idx);
16741da177e4SLinus Torvalds 
16751eff9d32SJens Axboe 		if (!(bio->bi_opf & REQ_RAHEAD))
167692c63902SMikulas Patocka 			queue_io(md, bio);
16776a8736d1STejun Heo 		else
16786a8736d1STejun Heo 			bio_io_error(bio);
1679978e51baSMike Snitzer 		return ret;
16801da177e4SLinus Torvalds 	}
16811da177e4SLinus Torvalds 
1682978e51baSMike Snitzer 	ret = process_bio(md, map, bio);
1683978e51baSMike Snitzer 
168483d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
1685978e51baSMike Snitzer 	return ret;
1686978e51baSMike Snitzer }
1687978e51baSMike Snitzer 
1688978e51baSMike Snitzer /*
1689978e51baSMike Snitzer  * The request function that remaps the bio to one target and
1690978e51baSMike Snitzer  * splits off any remainder.
1691978e51baSMike Snitzer  */
1692978e51baSMike Snitzer static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
1693978e51baSMike Snitzer {
1694978e51baSMike Snitzer 	return __dm_make_request(q, bio, __split_and_process_bio);
1695978e51baSMike Snitzer }
1696978e51baSMike Snitzer 
1697978e51baSMike Snitzer static blk_qc_t dm_make_request_nvme(struct request_queue *q, struct bio *bio)
1698978e51baSMike Snitzer {
1699978e51baSMike Snitzer 	return __dm_make_request(q, bio, __process_bio);
1700cec47e3dSKiyoshi Ueda }
1701cec47e3dSKiyoshi Ueda 
17021da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
17031da177e4SLinus Torvalds {
17048a57dfc6SChandra Seetharaman 	int r = bdi_bits;
17058a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
17068a57dfc6SChandra Seetharaman 	struct dm_table *map;
17071da177e4SLinus Torvalds 
17081eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
1709e522c039SMike Snitzer 		if (dm_request_based(md)) {
1710cec47e3dSKiyoshi Ueda 			/*
1711e522c039SMike Snitzer 			 * With request-based DM we only need to check the
1712e522c039SMike Snitzer 			 * top-level queue for congestion.
1713cec47e3dSKiyoshi Ueda 			 */
1714dc3b17ccSJan Kara 			r = md->queue->backing_dev_info->wb.state & bdi_bits;
1715e522c039SMike Snitzer 		} else {
1716e522c039SMike Snitzer 			map = dm_get_live_table_fast(md);
1717e522c039SMike Snitzer 			if (map)
17181da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
171983d5e5b0SMikulas Patocka 			dm_put_live_table_fast(md);
17208a57dfc6SChandra Seetharaman 		}
1721e522c039SMike Snitzer 	}
17228a57dfc6SChandra Seetharaman 
17231da177e4SLinus Torvalds 	return r;
17241da177e4SLinus Torvalds }
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds /*-----------------------------------------------------------------
17271da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
17281da177e4SLinus Torvalds  *---------------------------------------------------------------*/
17292b06cfffSAlasdair G Kergon static void free_minor(int minor)
17301da177e4SLinus Torvalds {
1731f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17321da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1733f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
17341da177e4SLinus Torvalds }
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds /*
17371da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
17381da177e4SLinus Torvalds  */
1739cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
17401da177e4SLinus Torvalds {
1741c9d76be6STejun Heo 	int r;
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
17441da177e4SLinus Torvalds 		return -EINVAL;
17451da177e4SLinus Torvalds 
1746c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1747f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17481da177e4SLinus Torvalds 
1749c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
17501da177e4SLinus Torvalds 
1751f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1752c9d76be6STejun Heo 	idr_preload_end();
1753c9d76be6STejun Heo 	if (r < 0)
1754c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
1755c9d76be6STejun Heo 	return 0;
17561da177e4SLinus Torvalds }
17571da177e4SLinus Torvalds 
1758cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
17591da177e4SLinus Torvalds {
1760c9d76be6STejun Heo 	int r;
17611da177e4SLinus Torvalds 
1762c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1763f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17641da177e4SLinus Torvalds 
1765c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
17661da177e4SLinus Torvalds 
1767f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1768c9d76be6STejun Heo 	idr_preload_end();
1769c9d76be6STejun Heo 	if (r < 0)
17701da177e4SLinus Torvalds 		return r;
1771c9d76be6STejun Heo 	*minor = r;
1772c9d76be6STejun Heo 	return 0;
17731da177e4SLinus Torvalds }
17741da177e4SLinus Torvalds 
177583d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
1776f26c5719SDan Williams static const struct dax_operations dm_dax_ops;
17771da177e4SLinus Torvalds 
177853d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
177953d5914fSMikulas Patocka 
1780c12c9a3cSMike Snitzer static void dm_init_normal_md_queue(struct mapped_device *md)
1781bfebd1cdSMike Snitzer {
178217e149b8SMike Snitzer 	md->use_blk_mq = false;
1783bfebd1cdSMike Snitzer 
1784bfebd1cdSMike Snitzer 	/*
1785bfebd1cdSMike Snitzer 	 * Initialize aspects of queue that aren't relevant for blk-mq
1786bfebd1cdSMike Snitzer 	 */
1787dc3b17ccSJan Kara 	md->queue->backing_dev_info->congested_fn = dm_any_congested;
17884a0b4ddfSMike Snitzer }
17894a0b4ddfSMike Snitzer 
17900f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md)
17910f20972fSMike Snitzer {
17920f20972fSMike Snitzer 	if (md->wq)
17930f20972fSMike Snitzer 		destroy_workqueue(md->wq);
17940f20972fSMike Snitzer 	if (md->kworker_task)
17950f20972fSMike Snitzer 		kthread_stop(md->kworker_task);
17960f20972fSMike Snitzer 	if (md->bs)
17970f20972fSMike Snitzer 		bioset_free(md->bs);
179864f52b0eSMike Snitzer 	if (md->io_bs)
179964f52b0eSMike Snitzer 		bioset_free(md->io_bs);
18000f20972fSMike Snitzer 
1801f26c5719SDan Williams 	if (md->dax_dev) {
1802f26c5719SDan Williams 		kill_dax(md->dax_dev);
1803f26c5719SDan Williams 		put_dax(md->dax_dev);
1804f26c5719SDan Williams 		md->dax_dev = NULL;
1805f26c5719SDan Williams 	}
1806f26c5719SDan Williams 
18070f20972fSMike Snitzer 	if (md->disk) {
18080f20972fSMike Snitzer 		spin_lock(&_minor_lock);
18090f20972fSMike Snitzer 		md->disk->private_data = NULL;
18100f20972fSMike Snitzer 		spin_unlock(&_minor_lock);
18110f20972fSMike Snitzer 		del_gendisk(md->disk);
18120f20972fSMike Snitzer 		put_disk(md->disk);
18130f20972fSMike Snitzer 	}
18140f20972fSMike Snitzer 
18150f20972fSMike Snitzer 	if (md->queue)
18160f20972fSMike Snitzer 		blk_cleanup_queue(md->queue);
18170f20972fSMike Snitzer 
1818d09960b0STahsin Erdogan 	cleanup_srcu_struct(&md->io_barrier);
1819d09960b0STahsin Erdogan 
18200f20972fSMike Snitzer 	if (md->bdev) {
18210f20972fSMike Snitzer 		bdput(md->bdev);
18220f20972fSMike Snitzer 		md->bdev = NULL;
18230f20972fSMike Snitzer 	}
18244cc96131SMike Snitzer 
1825d5ffebddSMike Snitzer 	mutex_destroy(&md->suspend_lock);
1826d5ffebddSMike Snitzer 	mutex_destroy(&md->type_lock);
1827d5ffebddSMike Snitzer 	mutex_destroy(&md->table_devices_lock);
1828d5ffebddSMike Snitzer 
18294cc96131SMike Snitzer 	dm_mq_cleanup_mapped_device(md);
18300f20972fSMike Snitzer }
18310f20972fSMike Snitzer 
18321da177e4SLinus Torvalds /*
18331da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
18341da177e4SLinus Torvalds  */
18352b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
18361da177e4SLinus Torvalds {
1837115485e8SMike Snitzer 	int r, numa_node_id = dm_get_numa_node();
1838f26c5719SDan Williams 	struct dax_device *dax_dev;
1839115485e8SMike Snitzer 	struct mapped_device *md;
1840ba61fdd1SJeff Mahoney 	void *old_md;
18411da177e4SLinus Torvalds 
1842856eb091SMikulas Patocka 	md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
18431da177e4SLinus Torvalds 	if (!md) {
18441da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
18451da177e4SLinus Torvalds 		return NULL;
18461da177e4SLinus Torvalds 	}
18471da177e4SLinus Torvalds 
184810da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
18496ed7ade8SMilan Broz 		goto bad_module_get;
185010da4f79SJeff Mahoney 
18511da177e4SLinus Torvalds 	/* get a minor number for the dev */
18522b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1853cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
18542b06cfffSAlasdair G Kergon 	else
1855cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
18561da177e4SLinus Torvalds 	if (r < 0)
18576ed7ade8SMilan Broz 		goto bad_minor;
18581da177e4SLinus Torvalds 
185983d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
186083d5e5b0SMikulas Patocka 	if (r < 0)
186183d5e5b0SMikulas Patocka 		goto bad_io_barrier;
186283d5e5b0SMikulas Patocka 
1863115485e8SMike Snitzer 	md->numa_node_id = numa_node_id;
18644cc96131SMike Snitzer 	md->use_blk_mq = dm_use_blk_mq_default();
1865591ddcfcSMike Snitzer 	md->init_tio_pdu = false;
1866a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
1867e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1868a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
186986f1152bSBenjamin Marzinski 	mutex_init(&md->table_devices_lock);
1870022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
18711da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
18725c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
18731da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
18747a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
18757a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
187686f1152bSBenjamin Marzinski 	INIT_LIST_HEAD(&md->table_devices);
18777a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
18781da177e4SLinus Torvalds 
1879115485e8SMike Snitzer 	md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id);
18801da177e4SLinus Torvalds 	if (!md->queue)
18810f20972fSMike Snitzer 		goto bad;
1882c12c9a3cSMike Snitzer 	md->queue->queuedata = md;
1883c12c9a3cSMike Snitzer 	md->queue->backing_dev_info->congested_data = md;
18841da177e4SLinus Torvalds 
1885c12c9a3cSMike Snitzer 	md->disk = alloc_disk_node(1, md->numa_node_id);
18861da177e4SLinus Torvalds 	if (!md->disk)
18870f20972fSMike Snitzer 		goto bad;
18881da177e4SLinus Torvalds 
1889316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
1890316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
1891f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
189253d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1893f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
18942995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
18952eb6e1e3SKeith Busch 	md->kworker_task = NULL;
1896f0b04115SJeff Mahoney 
18971da177e4SLinus Torvalds 	md->disk->major = _major;
18981da177e4SLinus Torvalds 	md->disk->first_minor = minor;
18991da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
19001da177e4SLinus Torvalds 	md->disk->queue = md->queue;
19011da177e4SLinus Torvalds 	md->disk->private_data = md;
19021da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
1903f26c5719SDan Williams 
1904f26c5719SDan Williams 	dax_dev = alloc_dax(md, md->disk->disk_name, &dm_dax_ops);
1905f26c5719SDan Williams 	if (!dax_dev)
1906f26c5719SDan Williams 		goto bad;
1907f26c5719SDan Williams 	md->dax_dev = dax_dev;
1908f26c5719SDan Williams 
1909c100ec49SMike Snitzer 	add_disk_no_queue_reg(md->disk);
19107e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
19111da177e4SLinus Torvalds 
1912670368a8STejun Heo 	md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
1913304f3f6aSMilan Broz 	if (!md->wq)
19140f20972fSMike Snitzer 		goto bad;
1915304f3f6aSMilan Broz 
191632a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
191732a926daSMikulas Patocka 	if (!md->bdev)
19180f20972fSMike Snitzer 		goto bad;
191932a926daSMikulas Patocka 
19203a83f467SMing Lei 	bio_init(&md->flush_bio, NULL, 0);
192174d46992SChristoph Hellwig 	bio_set_dev(&md->flush_bio, md->bdev);
1922ff0361b3SJan Kara 	md->flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
19236a8736d1STejun Heo 
1924fd2ed4d2SMikulas Patocka 	dm_stats_init(&md->stats);
1925fd2ed4d2SMikulas Patocka 
1926ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1927f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1928ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1929f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1930ba61fdd1SJeff Mahoney 
1931ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1932ba61fdd1SJeff Mahoney 
19331da177e4SLinus Torvalds 	return md;
19341da177e4SLinus Torvalds 
19350f20972fSMike Snitzer bad:
19360f20972fSMike Snitzer 	cleanup_mapped_device(md);
193783d5e5b0SMikulas Patocka bad_io_barrier:
19381da177e4SLinus Torvalds 	free_minor(minor);
19396ed7ade8SMilan Broz bad_minor:
194010da4f79SJeff Mahoney 	module_put(THIS_MODULE);
19416ed7ade8SMilan Broz bad_module_get:
1942856eb091SMikulas Patocka 	kvfree(md);
19431da177e4SLinus Torvalds 	return NULL;
19441da177e4SLinus Torvalds }
19451da177e4SLinus Torvalds 
1946ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1947ae9da83fSJun'ichi Nomura 
19481da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
19491da177e4SLinus Torvalds {
1950f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
195163d94e48SJun'ichi Nomura 
1952ae9da83fSJun'ichi Nomura 	unlock_fs(md);
19532eb6e1e3SKeith Busch 
19540f20972fSMike Snitzer 	cleanup_mapped_device(md);
19550f20972fSMike Snitzer 
19560f20972fSMike Snitzer 	free_table_devices(&md->table_devices);
19570f20972fSMike Snitzer 	dm_stats_cleanup(&md->stats);
195863a4f065SMike Snitzer 	free_minor(minor);
195963a4f065SMike Snitzer 
196010da4f79SJeff Mahoney 	module_put(THIS_MODULE);
1961856eb091SMikulas Patocka 	kvfree(md);
19621da177e4SLinus Torvalds }
19631da177e4SLinus Torvalds 
1964e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1965e6ee8c0bSKiyoshi Ueda {
1966c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
1967e6ee8c0bSKiyoshi Ueda 
1968545ed20eSToshi Kani 	if (dm_table_bio_based(t)) {
1969c0820cf5SMikulas Patocka 		/*
197064f52b0eSMike Snitzer 		 * The md may already have mempools that need changing.
197164f52b0eSMike Snitzer 		 * If so, reload bioset because front_pad may have changed
197216245bdcSJun'ichi Nomura 		 * because a different table was loaded.
1973c0820cf5SMikulas Patocka 		 */
197464f52b0eSMike Snitzer 		if (md->bs) {
1975c0820cf5SMikulas Patocka 			bioset_free(md->bs);
19760776aa0eSMike Snitzer 			md->bs = NULL;
1977c0820cf5SMikulas Patocka 		}
197864f52b0eSMike Snitzer 		if (md->io_bs) {
197964f52b0eSMike Snitzer 			bioset_free(md->io_bs);
198064f52b0eSMike Snitzer 			md->io_bs = NULL;
198164f52b0eSMike Snitzer 		}
19820776aa0eSMike Snitzer 
19830776aa0eSMike Snitzer 	} else if (md->bs) {
1984cbc4e3c1SMike Snitzer 		/*
19854e6e36c3SMike Snitzer 		 * There's no need to reload with request-based dm
19864e6e36c3SMike Snitzer 		 * because the size of front_pad doesn't change.
19874e6e36c3SMike Snitzer 		 * Note for future: If you are to reload bioset,
19884e6e36c3SMike Snitzer 		 * prep-ed requests in the queue may refer
19894e6e36c3SMike Snitzer 		 * to bio from the old bioset, so you must walk
19904e6e36c3SMike Snitzer 		 * through the queue to unprep.
1991cbc4e3c1SMike Snitzer 		 */
1992cbc4e3c1SMike Snitzer 		goto out;
1993cbc4e3c1SMike Snitzer 	}
1994cbc4e3c1SMike Snitzer 
1995dde1e1ecSMike Snitzer 	BUG_ON(!p || md->bs || md->io_bs);
1996e6ee8c0bSKiyoshi Ueda 
1997e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
1998e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
199964f52b0eSMike Snitzer 	md->io_bs = p->io_bs;
200064f52b0eSMike Snitzer 	p->io_bs = NULL;
2001e6ee8c0bSKiyoshi Ueda out:
200202233342SMike Snitzer 	/* mempool bind completed, no longer need any mempools in the table */
2003e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
2004e6ee8c0bSKiyoshi Ueda }
2005e6ee8c0bSKiyoshi Ueda 
20061da177e4SLinus Torvalds /*
20071da177e4SLinus Torvalds  * Bind a table to the device.
20081da177e4SLinus Torvalds  */
20091da177e4SLinus Torvalds static void event_callback(void *context)
20101da177e4SLinus Torvalds {
20117a8c3d3bSMike Anderson 	unsigned long flags;
20127a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
20131da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
20141da177e4SLinus Torvalds 
20157a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
20167a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
20177a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
20187a8c3d3bSMike Anderson 
2019ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
20207a8c3d3bSMike Anderson 
20211da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
20221da177e4SLinus Torvalds 	wake_up(&md->eventq);
202362e08243SMikulas Patocka 	dm_issue_global_event();
20241da177e4SLinus Torvalds }
20251da177e4SLinus Torvalds 
2026c217649bSMike Snitzer /*
2027c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
2028c217649bSMike Snitzer  */
20294e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
20301da177e4SLinus Torvalds {
20311ea0654eSBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
20321ea0654eSBart Van Assche 
20334e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
20341da177e4SLinus Torvalds 
2035db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
20361da177e4SLinus Torvalds }
20371da177e4SLinus Torvalds 
2038042d2a9bSAlasdair G Kergon /*
2039042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2040042d2a9bSAlasdair G Kergon  */
2041042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2042754c5fc7SMike Snitzer 			       struct queue_limits *limits)
20431da177e4SLinus Torvalds {
2044042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
2045165125e1SJens Axboe 	struct request_queue *q = md->queue;
2046978e51baSMike Snitzer 	bool request_based = dm_table_request_based(t);
20471da177e4SLinus Torvalds 	sector_t size;
20481da177e4SLinus Torvalds 
20495a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
20505a8f1f80SBart Van Assche 
20511da177e4SLinus Torvalds 	size = dm_table_get_size(t);
20523ac51e74SDarrick J. Wong 
20533ac51e74SDarrick J. Wong 	/*
20543ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
20553ac51e74SDarrick J. Wong 	 */
2056fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
20573ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
20583ac51e74SDarrick J. Wong 
20594e90188bSAlasdair G Kergon 	__set_size(md, size);
20601da177e4SLinus Torvalds 
2061cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
20622ca3310eSAlasdair G Kergon 
2063e6ee8c0bSKiyoshi Ueda 	/*
2064e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
2065e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
2066e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
2067e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
2068e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
2069e6ee8c0bSKiyoshi Ueda 	 */
2070978e51baSMike Snitzer 	if (request_based)
2071eca7ee6dSMike Snitzer 		dm_stop_queue(q);
2072978e51baSMike Snitzer 
2073978e51baSMike Snitzer 	if (request_based || md->type == DM_TYPE_NVME_BIO_BASED) {
207416f12266SMike Snitzer 		/*
2075978e51baSMike Snitzer 		 * Leverage the fact that request-based DM targets and
2076978e51baSMike Snitzer 		 * NVMe bio based targets are immutable singletons
2077978e51baSMike Snitzer 		 * - used to optimize both dm_request_fn and dm_mq_queue_rq;
2078978e51baSMike Snitzer 		 *   and __process_bio.
207916f12266SMike Snitzer 		 */
208016f12266SMike Snitzer 		md->immutable_target = dm_table_get_immutable_target(t);
208116f12266SMike Snitzer 	}
2082e6ee8c0bSKiyoshi Ueda 
2083e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
2084e6ee8c0bSKiyoshi Ueda 
2085a12f5d48SEric Dumazet 	old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
20861d3aa6f6SMike Snitzer 	rcu_assign_pointer(md->map, (void *)t);
208736a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
208836a0456fSAlasdair G Kergon 
2089754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
209041abc4e1SHannes Reinecke 	if (old_map)
209183d5e5b0SMikulas Patocka 		dm_sync_table(md);
20922ca3310eSAlasdair G Kergon 
2093042d2a9bSAlasdair G Kergon 	return old_map;
20941da177e4SLinus Torvalds }
20951da177e4SLinus Torvalds 
2096a7940155SAlasdair G Kergon /*
2097a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2098a7940155SAlasdair G Kergon  */
2099a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
21001da177e4SLinus Torvalds {
2101a12f5d48SEric Dumazet 	struct dm_table *map = rcu_dereference_protected(md->map, 1);
21021da177e4SLinus Torvalds 
21031da177e4SLinus Torvalds 	if (!map)
2104a7940155SAlasdair G Kergon 		return NULL;
21051da177e4SLinus Torvalds 
21061da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
21079cdb8520SMonam Agarwal 	RCU_INIT_POINTER(md->map, NULL);
210883d5e5b0SMikulas Patocka 	dm_sync_table(md);
2109a7940155SAlasdair G Kergon 
2110a7940155SAlasdair G Kergon 	return map;
21111da177e4SLinus Torvalds }
21121da177e4SLinus Torvalds 
21131da177e4SLinus Torvalds /*
21141da177e4SLinus Torvalds  * Constructor for a new device.
21151da177e4SLinus Torvalds  */
21162b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
21171da177e4SLinus Torvalds {
2118c12c9a3cSMike Snitzer 	int r;
21191da177e4SLinus Torvalds 	struct mapped_device *md;
21201da177e4SLinus Torvalds 
21212b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
21221da177e4SLinus Torvalds 	if (!md)
21231da177e4SLinus Torvalds 		return -ENXIO;
21241da177e4SLinus Torvalds 
2125c12c9a3cSMike Snitzer 	r = dm_sysfs_init(md);
2126c12c9a3cSMike Snitzer 	if (r) {
2127c12c9a3cSMike Snitzer 		free_dev(md);
2128c12c9a3cSMike Snitzer 		return r;
2129c12c9a3cSMike Snitzer 	}
2130784aae73SMilan Broz 
21311da177e4SLinus Torvalds 	*result = md;
21321da177e4SLinus Torvalds 	return 0;
21331da177e4SLinus Torvalds }
21341da177e4SLinus Torvalds 
2135a5664dadSMike Snitzer /*
2136a5664dadSMike Snitzer  * Functions to manage md->type.
2137a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2138a5664dadSMike Snitzer  */
2139a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2140a5664dadSMike Snitzer {
2141a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2142a5664dadSMike Snitzer }
2143a5664dadSMike Snitzer 
2144a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2145a5664dadSMike Snitzer {
2146a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2147a5664dadSMike Snitzer }
2148a5664dadSMike Snitzer 
21497e0d574fSBart Van Assche void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
2150a5664dadSMike Snitzer {
215100c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2152a5664dadSMike Snitzer 	md->type = type;
2153a5664dadSMike Snitzer }
2154a5664dadSMike Snitzer 
21557e0d574fSBart Van Assche enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
2156a5664dadSMike Snitzer {
2157a5664dadSMike Snitzer 	return md->type;
2158a5664dadSMike Snitzer }
2159a5664dadSMike Snitzer 
216036a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
216136a0456fSAlasdair G Kergon {
216236a0456fSAlasdair G Kergon 	return md->immutable_target_type;
216336a0456fSAlasdair G Kergon }
216436a0456fSAlasdair G Kergon 
21654a0b4ddfSMike Snitzer /*
2166f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
2167f84cb8a4SMike Snitzer  * count on 'md'.
2168f84cb8a4SMike Snitzer  */
2169f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2170f84cb8a4SMike Snitzer {
2171f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
2172f84cb8a4SMike Snitzer 	return &md->queue->limits;
2173f84cb8a4SMike Snitzer }
2174f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2175f84cb8a4SMike Snitzer 
21764a0b4ddfSMike Snitzer /*
21774a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
21784a0b4ddfSMike Snitzer  */
2179591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
21804a0b4ddfSMike Snitzer {
2181bfebd1cdSMike Snitzer 	int r;
2182c100ec49SMike Snitzer 	struct queue_limits limits;
21837e0d574fSBart Van Assche 	enum dm_queue_mode type = dm_get_md_type(md);
2184bfebd1cdSMike Snitzer 
2185545ed20eSToshi Kani 	switch (type) {
2186bfebd1cdSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
2187c12c9a3cSMike Snitzer 		dm_init_normal_md_queue(md);
2188eb8db831SChristoph Hellwig 		r = dm_old_init_request_queue(md, t);
2189bfebd1cdSMike Snitzer 		if (r) {
2190eca7ee6dSMike Snitzer 			DMERR("Cannot initialize queue for request-based mapped device");
2191bfebd1cdSMike Snitzer 			return r;
21924a0b4ddfSMike Snitzer 		}
2193bfebd1cdSMike Snitzer 		break;
2194bfebd1cdSMike Snitzer 	case DM_TYPE_MQ_REQUEST_BASED:
2195e83068a5SMike Snitzer 		r = dm_mq_init_request_queue(md, t);
2196bfebd1cdSMike Snitzer 		if (r) {
2197eca7ee6dSMike Snitzer 			DMERR("Cannot initialize queue for request-based dm-mq mapped device");
2198bfebd1cdSMike Snitzer 			return r;
2199bfebd1cdSMike Snitzer 		}
2200bfebd1cdSMike Snitzer 		break;
2201bfebd1cdSMike Snitzer 	case DM_TYPE_BIO_BASED:
2202545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
2203eca7ee6dSMike Snitzer 		dm_init_normal_md_queue(md);
2204ff36ab34SMike Snitzer 		blk_queue_make_request(md->queue, dm_make_request);
2205bfebd1cdSMike Snitzer 		break;
2206978e51baSMike Snitzer 	case DM_TYPE_NVME_BIO_BASED:
2207978e51baSMike Snitzer 		dm_init_normal_md_queue(md);
2208978e51baSMike Snitzer 		blk_queue_make_request(md->queue, dm_make_request_nvme);
220973d410c0SMilan Broz 		break;
22107e0d574fSBart Van Assche 	case DM_TYPE_NONE:
22117e0d574fSBart Van Assche 		WARN_ON_ONCE(true);
22127e0d574fSBart Van Assche 		break;
22131da177e4SLinus Torvalds 	}
22141da177e4SLinus Torvalds 
2215c100ec49SMike Snitzer 	r = dm_calculate_queue_limits(t, &limits);
2216c100ec49SMike Snitzer 	if (r) {
2217c100ec49SMike Snitzer 		DMERR("Cannot calculate initial queue limits");
2218c100ec49SMike Snitzer 		return r;
2219c100ec49SMike Snitzer 	}
2220c100ec49SMike Snitzer 	dm_table_set_restrictions(t, md->queue, &limits);
2221c100ec49SMike Snitzer 	blk_register_queue(md->disk);
2222c100ec49SMike Snitzer 
22231da177e4SLinus Torvalds 	return 0;
22241da177e4SLinus Torvalds }
22251da177e4SLinus Torvalds 
22261da177e4SLinus Torvalds struct mapped_device *dm_get_md(dev_t dev)
22271da177e4SLinus Torvalds {
22281da177e4SLinus Torvalds 	struct mapped_device *md;
22291da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
22301da177e4SLinus Torvalds 
22311da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
22321da177e4SLinus Torvalds 		return NULL;
22331da177e4SLinus Torvalds 
22341da177e4SLinus Torvalds 	spin_lock(&_minor_lock);
22351da177e4SLinus Torvalds 
22361da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
223749de5769SMike Snitzer 	if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) ||
223849de5769SMike Snitzer 	    test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
22391da177e4SLinus Torvalds 		md = NULL;
22401da177e4SLinus Torvalds 		goto out;
22411da177e4SLinus Torvalds 	}
22421da177e4SLinus Torvalds 	dm_get(md);
22431da177e4SLinus Torvalds out:
2244f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
22451da177e4SLinus Torvalds 
22461da177e4SLinus Torvalds 	return md;
2247fba9f90eSJeff Mahoney }
2248fba9f90eSJeff Mahoney EXPORT_SYMBOL_GPL(dm_get_md);
2249fba9f90eSJeff Mahoney 
2250637842cfSDavid Teigland void *dm_get_mdptr(struct mapped_device *md)
2251fba9f90eSJeff Mahoney {
2252fba9f90eSJeff Mahoney 	return md->interface_ptr;
22531da177e4SLinus Torvalds }
2254fba9f90eSJeff Mahoney 
2255f32c10b0SJeff Mahoney void dm_set_mdptr(struct mapped_device *md, void *ptr)
22561da177e4SLinus Torvalds {
2257637842cfSDavid Teigland 	md->interface_ptr = ptr;
2258637842cfSDavid Teigland }
2259637842cfSDavid Teigland 
2260d229a958SDavid Teigland void dm_get(struct mapped_device *md)
2261d229a958SDavid Teigland {
2262d229a958SDavid Teigland 	atomic_inc(&md->holders);
2263d229a958SDavid Teigland 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
2264d229a958SDavid Teigland }
2265d229a958SDavid Teigland 
2266d229a958SDavid Teigland int dm_hold(struct mapped_device *md)
2267d229a958SDavid Teigland {
2268d229a958SDavid Teigland 	spin_lock(&_minor_lock);
2269d229a958SDavid Teigland 	if (test_bit(DMF_FREEING, &md->flags)) {
22709ade92a9SAlasdair G Kergon 		spin_unlock(&_minor_lock);
2271637842cfSDavid Teigland 		return -EBUSY;
22729ade92a9SAlasdair G Kergon 	}
22731da177e4SLinus Torvalds 	dm_get(md);
22741da177e4SLinus Torvalds 	spin_unlock(&_minor_lock);
22751da177e4SLinus Torvalds 	return 0;
22761da177e4SLinus Torvalds }
22771da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(dm_hold);
22781da177e4SLinus Torvalds 
22791da177e4SLinus Torvalds const char *dm_device_name(struct mapped_device *md)
22801da177e4SLinus Torvalds {
22811da177e4SLinus Torvalds 	return md->name;
22821da177e4SLinus Torvalds }
22831da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(dm_device_name);
22841da177e4SLinus Torvalds 
228572d94861SAlasdair G Kergon static void __dm_destroy(struct mapped_device *md, bool wait)
228672d94861SAlasdair G Kergon {
228772d94861SAlasdair G Kergon 	struct dm_table *map;
228872d94861SAlasdair G Kergon 	int srcu_idx;
228972d94861SAlasdair G Kergon 
229072d94861SAlasdair G Kergon 	might_sleep();
22911da177e4SLinus Torvalds 
22921da177e4SLinus Torvalds 	spin_lock(&_minor_lock);
22931da177e4SLinus Torvalds 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
22941da177e4SLinus Torvalds 	set_bit(DMF_FREEING, &md->flags);
22951da177e4SLinus Torvalds 	spin_unlock(&_minor_lock);
22961da177e4SLinus Torvalds 
2297c12c9a3cSMike Snitzer 	blk_set_queue_dying(md->queue);
22983b785fbcSBart Van Assche 
229902233342SMike Snitzer 	if (dm_request_based(md) && md->kworker_task)
23003989144fSPetr Mladek 		kthread_flush_worker(&md->kworker);
23011da177e4SLinus Torvalds 
23021da177e4SLinus Torvalds 	/*
23031da177e4SLinus Torvalds 	 * Take suspend_lock so that presuspend and postsuspend methods
23041da177e4SLinus Torvalds 	 * do not race with internal suspend.
23051da177e4SLinus Torvalds 	 */
23061da177e4SLinus Torvalds 	mutex_lock(&md->suspend_lock);
23072a708cffSJunichi Nomura 	map = dm_get_live_table(md, &srcu_idx);
23081da177e4SLinus Torvalds 	if (!dm_suspended_md(md)) {
23091da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
23101da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
23111da177e4SLinus Torvalds 	}
23121da177e4SLinus Torvalds 	/* dm_put_live_table must be before msleep, otherwise deadlock is possible */
23131da177e4SLinus Torvalds 	dm_put_live_table(md, srcu_idx);
23142a708cffSJunichi Nomura 	mutex_unlock(&md->suspend_lock);
23151da177e4SLinus Torvalds 
23161da177e4SLinus Torvalds 	/*
23171da177e4SLinus Torvalds 	 * Rare, but there may be I/O requests still going to complete,
23181da177e4SLinus Torvalds 	 * for example.  Wait for all references to disappear.
23191da177e4SLinus Torvalds 	 * No one should increment the reference count of the mapped_device,
23201da177e4SLinus Torvalds 	 * after the mapped_device state becomes DMF_FREEING.
23211da177e4SLinus Torvalds 	 */
23221da177e4SLinus Torvalds 	if (wait)
23231da177e4SLinus Torvalds 		while (atomic_read(&md->holders))
23241da177e4SLinus Torvalds 			msleep(1);
23251da177e4SLinus Torvalds 	else if (atomic_read(&md->holders))
23261da177e4SLinus Torvalds 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
23271da177e4SLinus Torvalds 		       dm_device_name(md), atomic_read(&md->holders));
23281da177e4SLinus Torvalds 
23291da177e4SLinus Torvalds 	dm_sysfs_exit(md);
23301da177e4SLinus Torvalds 	dm_table_destroy(__unbind(md));
2331cf222b37SAlasdair G Kergon 	free_dev(md);
23321da177e4SLinus Torvalds }
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds void dm_destroy(struct mapped_device *md)
23351da177e4SLinus Torvalds {
23361da177e4SLinus Torvalds 	__dm_destroy(md, true);
23371da177e4SLinus Torvalds }
23381da177e4SLinus Torvalds 
23391da177e4SLinus Torvalds void dm_destroy_immediate(struct mapped_device *md)
23401da177e4SLinus Torvalds {
23411134e5aeSMike Anderson 	__dm_destroy(md, false);
23421da177e4SLinus Torvalds }
2343fba9f90eSJeff Mahoney 
2344fba9f90eSJeff Mahoney void dm_put(struct mapped_device *md)
2345f32c10b0SJeff Mahoney {
23461134e5aeSMike Anderson 	atomic_dec(&md->holders);
2347ba61fdd1SJeff Mahoney }
2348fba9f90eSJeff Mahoney EXPORT_SYMBOL_GPL(dm_put);
2349f32c10b0SJeff Mahoney 
2350b48633f8SBart Van Assche static int dm_wait_for_completion(struct mapped_device *md, long task_state)
23511da177e4SLinus Torvalds {
23521da177e4SLinus Torvalds 	int r = 0;
23539f4c3f87SBart Van Assche 	DEFINE_WAIT(wait);
23541da177e4SLinus Torvalds 
23551da177e4SLinus Torvalds 	while (1) {
23569f4c3f87SBart Van Assche 		prepare_to_wait(&md->wait, &wait, task_state);
23571da177e4SLinus Torvalds 
23581da177e4SLinus Torvalds 		if (!md_in_flight(md))
23591da177e4SLinus Torvalds 			break;
23601da177e4SLinus Torvalds 
2361e3fabdfdSBart Van Assche 		if (signal_pending_state(task_state, current)) {
23621da177e4SLinus Torvalds 			r = -EINTR;
23631da177e4SLinus Torvalds 			break;
23641da177e4SLinus Torvalds 		}
23651da177e4SLinus Torvalds 
23661da177e4SLinus Torvalds 		io_schedule();
23671da177e4SLinus Torvalds 	}
23689f4c3f87SBart Van Assche 	finish_wait(&md->wait, &wait);
23691da177e4SLinus Torvalds 
23701da177e4SLinus Torvalds 	return r;
23711da177e4SLinus Torvalds }
23721da177e4SLinus Torvalds 
23731da177e4SLinus Torvalds /*
237479eb885cSEdward Goggin  * Process the deferred bios
23751da177e4SLinus Torvalds  */
23761da177e4SLinus Torvalds static void dm_wq_work(struct work_struct *work)
23771da177e4SLinus Torvalds {
23781da177e4SLinus Torvalds 	struct mapped_device *md = container_of(work, struct mapped_device,
23796d6f10dfSMilan Broz 						work);
23801da177e4SLinus Torvalds 	struct bio *c;
23816d6f10dfSMilan Broz 	int srcu_idx;
23821da177e4SLinus Torvalds 	struct dm_table *map;
23836d6f10dfSMilan Broz 
23849e4e5f87SMilan Broz 	map = dm_get_live_table(md, &srcu_idx);
23859e4e5f87SMilan Broz 
23861da177e4SLinus Torvalds 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
23873b00b203SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
23883b00b203SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2389af7e466aSMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2390af7e466aSMikulas Patocka 
2391af7e466aSMikulas Patocka 		if (!c)
2392df12ee99SAlasdair G Kergon 			break;
23933b00b203SMikulas Patocka 
2394e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2395e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2396e39e2e95SAlasdair G Kergon 		else
239783d5e5b0SMikulas Patocka 			__split_and_process_bio(md, map, c);
2398e6ee8c0bSKiyoshi Ueda 	}
23991da177e4SLinus Torvalds 
240083d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
24011da177e4SLinus Torvalds }
24022ca3310eSAlasdair G Kergon 
24031da177e4SLinus Torvalds static void dm_queue_flush(struct mapped_device *md)
24041da177e4SLinus Torvalds {
2405cf222b37SAlasdair G Kergon 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
24064e857c58SPeter Zijlstra 	smp_mb__after_atomic();
24071da177e4SLinus Torvalds 	queue_work(md->wq, &md->work);
24081da177e4SLinus Torvalds }
24091da177e4SLinus Torvalds 
24101da177e4SLinus Torvalds /*
2411042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
24121da177e4SLinus Torvalds  */
2413042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
24141da177e4SLinus Torvalds {
241587eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2416754c5fc7SMike Snitzer 	struct queue_limits limits;
2417042d2a9bSAlasdair G Kergon 	int r;
24181da177e4SLinus Torvalds 
24191da177e4SLinus Torvalds 	mutex_lock(&md->suspend_lock);
24201da177e4SLinus Torvalds 
242193c534aeSAlasdair G Kergon 	/* device must be suspended */
24224f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
24231da177e4SLinus Torvalds 		goto out;
24241da177e4SLinus Torvalds 
24253ae70656SMike Snitzer 	/*
24263ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
24273ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
24283ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
24293ae70656SMike Snitzer 	 * reappear.
24303ae70656SMike Snitzer 	 */
24313ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
243283d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
24333ae70656SMike Snitzer 		if (live_map)
24343ae70656SMike Snitzer 			limits = md->queue->limits;
243583d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
24363ae70656SMike Snitzer 	}
24373ae70656SMike Snitzer 
243887eb5b21SMike Christie 	if (!live_map) {
2439754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2440042d2a9bSAlasdair G Kergon 		if (r) {
2441042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2442754c5fc7SMike Snitzer 			goto out;
2443042d2a9bSAlasdair G Kergon 		}
244487eb5b21SMike Christie 	}
2445754c5fc7SMike Snitzer 
2446042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
244762e08243SMikulas Patocka 	dm_issue_global_event();
24481da177e4SLinus Torvalds 
24491da177e4SLinus Torvalds out:
24501da177e4SLinus Torvalds 	mutex_unlock(&md->suspend_lock);
2451042d2a9bSAlasdair G Kergon 	return map;
24522ca3310eSAlasdair G Kergon }
24531da177e4SLinus Torvalds 
2454cf222b37SAlasdair G Kergon /*
2455dfbe03f6SAlasdair G Kergon  * Functions to lock and unlock any filesystem running on the
2456d1782a3bSAlasdair G Kergon  * device.
2457d1782a3bSAlasdair G Kergon  */
24582ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
2459dfbe03f6SAlasdair G Kergon {
24601da177e4SLinus Torvalds 	int r;
24611da177e4SLinus Torvalds 
24621da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2463dfbe03f6SAlasdair G Kergon 
2464db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2465dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2466cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2467e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2468e39e2e95SAlasdair G Kergon 		return r;
2469dfbe03f6SAlasdair G Kergon 	}
2470dfbe03f6SAlasdair G Kergon 
2471aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2472aa8d7c2fSAlasdair G Kergon 
24731da177e4SLinus Torvalds 	return 0;
24741da177e4SLinus Torvalds }
24751da177e4SLinus Torvalds 
24762ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
24771da177e4SLinus Torvalds {
2478aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2479aa8d7c2fSAlasdair G Kergon 		return;
2480aa8d7c2fSAlasdair G Kergon 
2481db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
24821da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2483aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
24841da177e4SLinus Torvalds }
24851da177e4SLinus Torvalds 
24861da177e4SLinus Torvalds /*
2487b48633f8SBart Van Assche  * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
2488b48633f8SBart Van Assche  * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
2489b48633f8SBart Van Assche  * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
2490b48633f8SBart Van Assche  *
2491ffcc3936SMike Snitzer  * If __dm_suspend returns 0, the device is completely quiescent
2492ffcc3936SMike Snitzer  * now. There is no request-processing activity. All new requests
2493ffcc3936SMike Snitzer  * are being added to md->deferred list.
2494cec47e3dSKiyoshi Ueda  */
2495ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
2496b48633f8SBart Van Assche 			unsigned suspend_flags, long task_state,
2497eaf9a736SMike Snitzer 			int dmf_suspended_flag)
24981da177e4SLinus Torvalds {
2499ffcc3936SMike Snitzer 	bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2500ffcc3936SMike Snitzer 	bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2501ffcc3936SMike Snitzer 	int r;
2502cf222b37SAlasdair G Kergon 
25035a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
25045a8f1f80SBart Van Assche 
25052e93ccc1SKiyoshi Ueda 	/*
25062e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
25072e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
25082e93ccc1SKiyoshi Ueda 	 */
25092e93ccc1SKiyoshi Ueda 	if (noflush)
25102e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
251186331f39SBart Van Assche 	else
251286331f39SBart Van Assche 		pr_debug("%s: suspending with flush\n", dm_device_name(md));
25132e93ccc1SKiyoshi Ueda 
2514d67ee213SMike Snitzer 	/*
2515d67ee213SMike Snitzer 	 * This gets reverted if there's an error later and the targets
2516d67ee213SMike Snitzer 	 * provide the .presuspend_undo hook.
2517d67ee213SMike Snitzer 	 */
25181da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
25191da177e4SLinus Torvalds 
25202e93ccc1SKiyoshi Ueda 	/*
25219f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
25229f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
25239f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
25249f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
25252e93ccc1SKiyoshi Ueda 	 */
252632a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
25272ca3310eSAlasdair G Kergon 		r = lock_fs(md);
2528d67ee213SMike Snitzer 		if (r) {
2529d67ee213SMike Snitzer 			dm_table_presuspend_undo_targets(map);
2530ffcc3936SMike Snitzer 			return r;
2531aa8d7c2fSAlasdair G Kergon 		}
2532d67ee213SMike Snitzer 	}
25331da177e4SLinus Torvalds 
25341da177e4SLinus Torvalds 	/*
25353b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
25363b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
25373b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
25383b00b203SMikulas Patocka 	 * dm_wq_work.
25393b00b203SMikulas Patocka 	 *
25403b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
25413b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
25426a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
25436a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
25446a8736d1STejun Heo 	 * flush_workqueue(md->wq).
25451da177e4SLinus Torvalds 	 */
25461eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
254741abc4e1SHannes Reinecke 	if (map)
254883d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
25491da177e4SLinus Torvalds 
2550d0bcb878SKiyoshi Ueda 	/*
255129e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
255229e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2553d0bcb878SKiyoshi Ueda 	 */
25542eb6e1e3SKeith Busch 	if (dm_request_based(md)) {
2555eca7ee6dSMike Snitzer 		dm_stop_queue(md->queue);
255602233342SMike Snitzer 		if (md->kworker_task)
25573989144fSPetr Mladek 			kthread_flush_worker(&md->kworker);
25582eb6e1e3SKeith Busch 	}
2559cec47e3dSKiyoshi Ueda 
2560d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2561d0bcb878SKiyoshi Ueda 
25621da177e4SLinus Torvalds 	/*
25633b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
25643b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
25653b00b203SMikulas Patocka 	 * to finish.
25661da177e4SLinus Torvalds 	 */
2567b48633f8SBart Van Assche 	r = dm_wait_for_completion(md, task_state);
2568eaf9a736SMike Snitzer 	if (!r)
2569eaf9a736SMike Snitzer 		set_bit(dmf_suspended_flag, &md->flags);
25701da177e4SLinus Torvalds 
25716d6f10dfSMilan Broz 	if (noflush)
2572022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
257341abc4e1SHannes Reinecke 	if (map)
257483d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
25752e93ccc1SKiyoshi Ueda 
25761da177e4SLinus Torvalds 	/* were we interrupted ? */
257746125c1cSMilan Broz 	if (r < 0) {
25789a1fb464SMikulas Patocka 		dm_queue_flush(md);
257973d410c0SMilan Broz 
2580cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
2581eca7ee6dSMike Snitzer 			dm_start_queue(md->queue);
2582cec47e3dSKiyoshi Ueda 
25832ca3310eSAlasdair G Kergon 		unlock_fs(md);
2584d67ee213SMike Snitzer 		dm_table_presuspend_undo_targets(map);
2585ffcc3936SMike Snitzer 		/* pushback list is already flushed, so skip flush */
2586ffcc3936SMike Snitzer 	}
2587ffcc3936SMike Snitzer 
2588ffcc3936SMike Snitzer 	return r;
25892ca3310eSAlasdair G Kergon }
25902ca3310eSAlasdair G Kergon 
25913b00b203SMikulas Patocka /*
2592ffcc3936SMike Snitzer  * We need to be able to change a mapping table under a mounted
2593ffcc3936SMike Snitzer  * filesystem.  For example we might want to move some data in
2594ffcc3936SMike Snitzer  * the background.  Before the table can be swapped with
2595ffcc3936SMike Snitzer  * dm_bind_table, dm_suspend must be called to flush any in
2596ffcc3936SMike Snitzer  * flight bios and ensure that any further io gets deferred.
25973b00b203SMikulas Patocka  */
2598ffcc3936SMike Snitzer /*
2599ffcc3936SMike Snitzer  * Suspend mechanism in request-based dm.
2600ffcc3936SMike Snitzer  *
2601ffcc3936SMike Snitzer  * 1. Flush all I/Os by lock_fs() if needed.
2602ffcc3936SMike Snitzer  * 2. Stop dispatching any I/O by stopping the request_queue.
2603ffcc3936SMike Snitzer  * 3. Wait for all in-flight I/Os to be completed or requeued.
2604ffcc3936SMike Snitzer  *
2605ffcc3936SMike Snitzer  * To abort suspend, start the request_queue.
2606ffcc3936SMike Snitzer  */
2607ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2608ffcc3936SMike Snitzer {
2609ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2610ffcc3936SMike Snitzer 	int r = 0;
2611ffcc3936SMike Snitzer 
2612ffcc3936SMike Snitzer retry:
2613ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2614ffcc3936SMike Snitzer 
2615ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2616ffcc3936SMike Snitzer 		r = -EINVAL;
2617ffcc3936SMike Snitzer 		goto out_unlock;
2618ffcc3936SMike Snitzer 	}
2619ffcc3936SMike Snitzer 
2620ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2621ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2622ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2623ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2624ffcc3936SMike Snitzer 		if (r)
2625ffcc3936SMike Snitzer 			return r;
2626ffcc3936SMike Snitzer 		goto retry;
2627ffcc3936SMike Snitzer 	}
2628ffcc3936SMike Snitzer 
2629a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2630ffcc3936SMike Snitzer 
2631eaf9a736SMike Snitzer 	r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
2632ffcc3936SMike Snitzer 	if (r)
2633ffcc3936SMike Snitzer 		goto out_unlock;
26343b00b203SMikulas Patocka 
26354d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
26364d4471cbSKiyoshi Ueda 
2637d287483dSAlasdair G Kergon out_unlock:
2638e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2639cf222b37SAlasdair G Kergon 	return r;
26401da177e4SLinus Torvalds }
26411da177e4SLinus Torvalds 
2642ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map)
26431da177e4SLinus Torvalds {
2644ffcc3936SMike Snitzer 	if (map) {
2645ffcc3936SMike Snitzer 		int r = dm_table_resume_targets(map);
26468757b776SMilan Broz 		if (r)
2647ffcc3936SMike Snitzer 			return r;
2648ffcc3936SMike Snitzer 	}
26492ca3310eSAlasdair G Kergon 
26509a1fb464SMikulas Patocka 	dm_queue_flush(md);
26512ca3310eSAlasdair G Kergon 
2652cec47e3dSKiyoshi Ueda 	/*
2653cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2654cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2655cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2656cec47e3dSKiyoshi Ueda 	 */
2657cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2658eca7ee6dSMike Snitzer 		dm_start_queue(md->queue);
2659cec47e3dSKiyoshi Ueda 
26602ca3310eSAlasdair G Kergon 	unlock_fs(md);
26612ca3310eSAlasdair G Kergon 
2662ffcc3936SMike Snitzer 	return 0;
2663ffcc3936SMike Snitzer }
2664ffcc3936SMike Snitzer 
2665ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md)
2666ffcc3936SMike Snitzer {
26678dc23658SMinfei Huang 	int r;
2668ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2669ffcc3936SMike Snitzer 
2670ffcc3936SMike Snitzer retry:
26718dc23658SMinfei Huang 	r = -EINVAL;
2672ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2673ffcc3936SMike Snitzer 
2674ffcc3936SMike Snitzer 	if (!dm_suspended_md(md))
2675ffcc3936SMike Snitzer 		goto out;
2676ffcc3936SMike Snitzer 
2677ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2678ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2679ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2680ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2681ffcc3936SMike Snitzer 		if (r)
2682ffcc3936SMike Snitzer 			return r;
2683ffcc3936SMike Snitzer 		goto retry;
2684ffcc3936SMike Snitzer 	}
2685ffcc3936SMike Snitzer 
2686a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2687ffcc3936SMike Snitzer 	if (!map || !dm_table_get_size(map))
2688ffcc3936SMike Snitzer 		goto out;
2689ffcc3936SMike Snitzer 
2690ffcc3936SMike Snitzer 	r = __dm_resume(md, map);
2691ffcc3936SMike Snitzer 	if (r)
2692ffcc3936SMike Snitzer 		goto out;
2693ffcc3936SMike Snitzer 
26942ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
2695cf222b37SAlasdair G Kergon out:
2696e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
26972ca3310eSAlasdair G Kergon 
2698cf222b37SAlasdair G Kergon 	return r;
26991da177e4SLinus Torvalds }
27001da177e4SLinus Torvalds 
2701fd2ed4d2SMikulas Patocka /*
2702fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
2703fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
2704fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
2705fd2ed4d2SMikulas Patocka  */
2706fd2ed4d2SMikulas Patocka 
2707ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
2708ffcc3936SMike Snitzer {
2709ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2710ffcc3936SMike Snitzer 
27111ea0654eSBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
27121ea0654eSBart Van Assche 
271396b26c8cSMikulas Patocka 	if (md->internal_suspend_count++)
2714ffcc3936SMike Snitzer 		return; /* nested internal suspend */
2715ffcc3936SMike Snitzer 
2716ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2717ffcc3936SMike Snitzer 		set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2718ffcc3936SMike Snitzer 		return; /* nest suspend */
2719ffcc3936SMike Snitzer 	}
2720ffcc3936SMike Snitzer 
2721a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2722ffcc3936SMike Snitzer 
2723ffcc3936SMike Snitzer 	/*
2724ffcc3936SMike Snitzer 	 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2725ffcc3936SMike Snitzer 	 * supported.  Properly supporting a TASK_INTERRUPTIBLE internal suspend
2726ffcc3936SMike Snitzer 	 * would require changing .presuspend to return an error -- avoid this
2727ffcc3936SMike Snitzer 	 * until there is a need for more elaborate variants of internal suspend.
2728ffcc3936SMike Snitzer 	 */
2729eaf9a736SMike Snitzer 	(void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
2730eaf9a736SMike Snitzer 			    DMF_SUSPENDED_INTERNALLY);
2731ffcc3936SMike Snitzer 
2732ffcc3936SMike Snitzer 	dm_table_postsuspend_targets(map);
2733ffcc3936SMike Snitzer }
2734ffcc3936SMike Snitzer 
2735ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md)
2736ffcc3936SMike Snitzer {
273796b26c8cSMikulas Patocka 	BUG_ON(!md->internal_suspend_count);
273896b26c8cSMikulas Patocka 
273996b26c8cSMikulas Patocka 	if (--md->internal_suspend_count)
2740ffcc3936SMike Snitzer 		return; /* resume from nested internal suspend */
2741ffcc3936SMike Snitzer 
2742ffcc3936SMike Snitzer 	if (dm_suspended_md(md))
2743ffcc3936SMike Snitzer 		goto done; /* resume from nested suspend */
2744ffcc3936SMike Snitzer 
2745ffcc3936SMike Snitzer 	/*
2746ffcc3936SMike Snitzer 	 * NOTE: existing callers don't need to call dm_table_resume_targets
2747ffcc3936SMike Snitzer 	 * (which may fail -- so best to avoid it for now by passing NULL map)
2748ffcc3936SMike Snitzer 	 */
2749ffcc3936SMike Snitzer 	(void) __dm_resume(md, NULL);
2750ffcc3936SMike Snitzer 
2751ffcc3936SMike Snitzer done:
2752ffcc3936SMike Snitzer 	clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2753ffcc3936SMike Snitzer 	smp_mb__after_atomic();
2754ffcc3936SMike Snitzer 	wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2755ffcc3936SMike Snitzer }
2756ffcc3936SMike Snitzer 
2757ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md)
2758fd2ed4d2SMikulas Patocka {
2759fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
2760ffcc3936SMike Snitzer 	__dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2761ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2762ffcc3936SMike Snitzer }
2763ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
2764ffcc3936SMike Snitzer 
2765ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md)
2766ffcc3936SMike Snitzer {
2767ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2768ffcc3936SMike Snitzer 	__dm_internal_resume(md);
2769ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2770ffcc3936SMike Snitzer }
2771ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume);
2772ffcc3936SMike Snitzer 
2773ffcc3936SMike Snitzer /*
2774ffcc3936SMike Snitzer  * Fast variants of internal suspend/resume hold md->suspend_lock,
2775ffcc3936SMike Snitzer  * which prevents interaction with userspace-driven suspend.
2776ffcc3936SMike Snitzer  */
2777ffcc3936SMike Snitzer 
2778ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md)
2779ffcc3936SMike Snitzer {
2780ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2781ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2782fd2ed4d2SMikulas Patocka 		return;
2783fd2ed4d2SMikulas Patocka 
2784fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2785fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
2786fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
2787fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2788fd2ed4d2SMikulas Patocka }
2789b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
2790fd2ed4d2SMikulas Patocka 
2791ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md)
2792fd2ed4d2SMikulas Patocka {
2793ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2794fd2ed4d2SMikulas Patocka 		goto done;
2795fd2ed4d2SMikulas Patocka 
2796fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
2797fd2ed4d2SMikulas Patocka 
2798fd2ed4d2SMikulas Patocka done:
2799fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
2800fd2ed4d2SMikulas Patocka }
2801b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
2802fd2ed4d2SMikulas Patocka 
28031da177e4SLinus Torvalds /*-----------------------------------------------------------------
28041da177e4SLinus Torvalds  * Event notification.
28051da177e4SLinus Torvalds  *---------------------------------------------------------------*/
28063abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
280760935eb2SMilan Broz 		       unsigned cookie)
280869267a30SAlasdair G Kergon {
280960935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
281060935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
281160935eb2SMilan Broz 
281260935eb2SMilan Broz 	if (!cookie)
28133abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
281460935eb2SMilan Broz 	else {
281560935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
281660935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
28173abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
28183abf85b5SPeter Rajnoha 					  action, envp);
281960935eb2SMilan Broz 	}
282069267a30SAlasdair G Kergon }
282169267a30SAlasdair G Kergon 
28227a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
28237a8c3d3bSMike Anderson {
28247a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
28257a8c3d3bSMike Anderson }
28267a8c3d3bSMike Anderson 
28271da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
28281da177e4SLinus Torvalds {
28291da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
28301da177e4SLinus Torvalds }
28311da177e4SLinus Torvalds 
28321da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
28331da177e4SLinus Torvalds {
28341da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
28351da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
28361da177e4SLinus Torvalds }
28371da177e4SLinus Torvalds 
28387a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
28397a8c3d3bSMike Anderson {
28407a8c3d3bSMike Anderson 	unsigned long flags;
28417a8c3d3bSMike Anderson 
28427a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
28437a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
28447a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
28457a8c3d3bSMike Anderson }
28467a8c3d3bSMike Anderson 
28471da177e4SLinus Torvalds /*
28481da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
28491da177e4SLinus Torvalds  * count on 'md'.
28501da177e4SLinus Torvalds  */
28511da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
28521da177e4SLinus Torvalds {
28531da177e4SLinus Torvalds 	return md->disk;
28541da177e4SLinus Torvalds }
285565ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk);
28561da177e4SLinus Torvalds 
2857784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2858784aae73SMilan Broz {
28592995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
2860784aae73SMilan Broz }
2861784aae73SMilan Broz 
2862784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2863784aae73SMilan Broz {
2864784aae73SMilan Broz 	struct mapped_device *md;
2865784aae73SMilan Broz 
28662995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
2867784aae73SMilan Broz 
2868b9a41d21SHou Tao 	spin_lock(&_minor_lock);
2869b9a41d21SHou Tao 	if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2870b9a41d21SHou Tao 		md = NULL;
2871b9a41d21SHou Tao 		goto out;
2872b9a41d21SHou Tao 	}
2873784aae73SMilan Broz 	dm_get(md);
2874b9a41d21SHou Tao out:
2875b9a41d21SHou Tao 	spin_unlock(&_minor_lock);
2876b9a41d21SHou Tao 
2877784aae73SMilan Broz 	return md;
2878784aae73SMilan Broz }
2879784aae73SMilan Broz 
28804f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
28811da177e4SLinus Torvalds {
28821da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
28831da177e4SLinus Torvalds }
28841da177e4SLinus Torvalds 
2885ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md)
2886ffcc3936SMike Snitzer {
2887ffcc3936SMike Snitzer 	return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2888ffcc3936SMike Snitzer }
2889ffcc3936SMike Snitzer 
28902c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
28912c140a24SMikulas Patocka {
28922c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
28932c140a24SMikulas Patocka }
28942c140a24SMikulas Patocka 
289564dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
289664dbce58SKiyoshi Ueda {
2897ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
289864dbce58SKiyoshi Ueda }
289964dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
290064dbce58SKiyoshi Ueda 
29012e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
29022e93ccc1SKiyoshi Ueda {
2903ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
29042e93ccc1SKiyoshi Ueda }
29052e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
29062e93ccc1SKiyoshi Ueda 
29077e0d574fSBart Van Assche struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type,
29080776aa0eSMike Snitzer 					    unsigned integrity, unsigned per_io_data_size,
29090776aa0eSMike Snitzer 					    unsigned min_pool_size)
2910e6ee8c0bSKiyoshi Ueda {
2911115485e8SMike Snitzer 	struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
291278d8e58aSMike Snitzer 	unsigned int pool_size = 0;
291364f52b0eSMike Snitzer 	unsigned int front_pad, io_front_pad;
2914e6ee8c0bSKiyoshi Ueda 
2915e6ee8c0bSKiyoshi Ueda 	if (!pools)
29164e6e36c3SMike Snitzer 		return NULL;
2917e6ee8c0bSKiyoshi Ueda 
291878d8e58aSMike Snitzer 	switch (type) {
291978d8e58aSMike Snitzer 	case DM_TYPE_BIO_BASED:
2920545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
292122c11858SMike Snitzer 	case DM_TYPE_NVME_BIO_BASED:
29220776aa0eSMike Snitzer 		pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size);
292330187e1dSMike Snitzer 		front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
292464f52b0eSMike Snitzer 		io_front_pad = roundup(front_pad,  __alignof__(struct dm_io)) + offsetof(struct dm_io, tio);
292564f52b0eSMike Snitzer 		pools->io_bs = bioset_create(pool_size, io_front_pad, 0);
292664f52b0eSMike Snitzer 		if (!pools->io_bs)
292764f52b0eSMike Snitzer 			goto out;
292864f52b0eSMike Snitzer 		if (integrity && bioset_integrity_create(pools->io_bs, pool_size))
2929eb8db831SChristoph Hellwig 			goto out;
293078d8e58aSMike Snitzer 		break;
293178d8e58aSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
293278d8e58aSMike Snitzer 	case DM_TYPE_MQ_REQUEST_BASED:
29330776aa0eSMike Snitzer 		pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size);
293478d8e58aSMike Snitzer 		front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
2935591ddcfcSMike Snitzer 		/* per_io_data_size is used for blk-mq pdu at queue allocation */
293678d8e58aSMike Snitzer 		break;
293778d8e58aSMike Snitzer 	default:
293878d8e58aSMike Snitzer 		BUG();
293978d8e58aSMike Snitzer 	}
294078d8e58aSMike Snitzer 
29414a3f54d9SMike Snitzer 	pools->bs = bioset_create(pool_size, front_pad, 0);
2942e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
29435f015204SJun'ichi Nomura 		goto out;
2944e6ee8c0bSKiyoshi Ueda 
2945a91a2785SMartin K. Petersen 	if (integrity && bioset_integrity_create(pools->bs, pool_size))
29465f015204SJun'ichi Nomura 		goto out;
2947a91a2785SMartin K. Petersen 
2948e6ee8c0bSKiyoshi Ueda 	return pools;
294978d8e58aSMike Snitzer 
29505f015204SJun'ichi Nomura out:
29515f015204SJun'ichi Nomura 	dm_free_md_mempools(pools);
2952e6ee8c0bSKiyoshi Ueda 
29534e6e36c3SMike Snitzer 	return NULL;
2954e6ee8c0bSKiyoshi Ueda }
2955e6ee8c0bSKiyoshi Ueda 
2956e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
2957e6ee8c0bSKiyoshi Ueda {
2958e6ee8c0bSKiyoshi Ueda 	if (!pools)
2959e6ee8c0bSKiyoshi Ueda 		return;
2960e6ee8c0bSKiyoshi Ueda 
2961e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
2962e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
296364f52b0eSMike Snitzer 	if (pools->io_bs)
296464f52b0eSMike Snitzer 		bioset_free(pools->io_bs);
2965e6ee8c0bSKiyoshi Ueda 
2966e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2967e6ee8c0bSKiyoshi Ueda }
2968e6ee8c0bSKiyoshi Ueda 
29699c72bad1SChristoph Hellwig struct dm_pr {
29709c72bad1SChristoph Hellwig 	u64	old_key;
29719c72bad1SChristoph Hellwig 	u64	new_key;
29729c72bad1SChristoph Hellwig 	u32	flags;
29739c72bad1SChristoph Hellwig 	bool	fail_early;
29749c72bad1SChristoph Hellwig };
29759c72bad1SChristoph Hellwig 
29769c72bad1SChristoph Hellwig static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
29779c72bad1SChristoph Hellwig 		      void *data)
29789c72bad1SChristoph Hellwig {
29799c72bad1SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
29809c72bad1SChristoph Hellwig 	struct dm_table *table;
29819c72bad1SChristoph Hellwig 	struct dm_target *ti;
29829c72bad1SChristoph Hellwig 	int ret = -ENOTTY, srcu_idx;
29839c72bad1SChristoph Hellwig 
29849c72bad1SChristoph Hellwig 	table = dm_get_live_table(md, &srcu_idx);
29859c72bad1SChristoph Hellwig 	if (!table || !dm_table_get_size(table))
29869c72bad1SChristoph Hellwig 		goto out;
29879c72bad1SChristoph Hellwig 
29889c72bad1SChristoph Hellwig 	/* We only support devices that have a single target */
29899c72bad1SChristoph Hellwig 	if (dm_table_get_num_targets(table) != 1)
29909c72bad1SChristoph Hellwig 		goto out;
29919c72bad1SChristoph Hellwig 	ti = dm_table_get_target(table, 0);
29929c72bad1SChristoph Hellwig 
29939c72bad1SChristoph Hellwig 	ret = -EINVAL;
29949c72bad1SChristoph Hellwig 	if (!ti->type->iterate_devices)
29959c72bad1SChristoph Hellwig 		goto out;
29969c72bad1SChristoph Hellwig 
29979c72bad1SChristoph Hellwig 	ret = ti->type->iterate_devices(ti, fn, data);
29989c72bad1SChristoph Hellwig out:
29999c72bad1SChristoph Hellwig 	dm_put_live_table(md, srcu_idx);
30009c72bad1SChristoph Hellwig 	return ret;
30019c72bad1SChristoph Hellwig }
30029c72bad1SChristoph Hellwig 
30039c72bad1SChristoph Hellwig /*
30049c72bad1SChristoph Hellwig  * For register / unregister we need to manually call out to every path.
30059c72bad1SChristoph Hellwig  */
30069c72bad1SChristoph Hellwig static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
30079c72bad1SChristoph Hellwig 			    sector_t start, sector_t len, void *data)
30089c72bad1SChristoph Hellwig {
30099c72bad1SChristoph Hellwig 	struct dm_pr *pr = data;
30109c72bad1SChristoph Hellwig 	const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
30119c72bad1SChristoph Hellwig 
30129c72bad1SChristoph Hellwig 	if (!ops || !ops->pr_register)
30139c72bad1SChristoph Hellwig 		return -EOPNOTSUPP;
30149c72bad1SChristoph Hellwig 	return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
30159c72bad1SChristoph Hellwig }
30169c72bad1SChristoph Hellwig 
301771cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
301871cdb697SChristoph Hellwig 			  u32 flags)
301971cdb697SChristoph Hellwig {
30209c72bad1SChristoph Hellwig 	struct dm_pr pr = {
30219c72bad1SChristoph Hellwig 		.old_key	= old_key,
30229c72bad1SChristoph Hellwig 		.new_key	= new_key,
30239c72bad1SChristoph Hellwig 		.flags		= flags,
30249c72bad1SChristoph Hellwig 		.fail_early	= true,
30259c72bad1SChristoph Hellwig 	};
30269c72bad1SChristoph Hellwig 	int ret;
302771cdb697SChristoph Hellwig 
30289c72bad1SChristoph Hellwig 	ret = dm_call_pr(bdev, __dm_pr_register, &pr);
30299c72bad1SChristoph Hellwig 	if (ret && new_key) {
30309c72bad1SChristoph Hellwig 		/* unregister all paths if we failed to register any path */
30319c72bad1SChristoph Hellwig 		pr.old_key = new_key;
30329c72bad1SChristoph Hellwig 		pr.new_key = 0;
30339c72bad1SChristoph Hellwig 		pr.flags = 0;
30349c72bad1SChristoph Hellwig 		pr.fail_early = false;
30359c72bad1SChristoph Hellwig 		dm_call_pr(bdev, __dm_pr_register, &pr);
30369c72bad1SChristoph Hellwig 	}
303771cdb697SChristoph Hellwig 
30389c72bad1SChristoph Hellwig 	return ret;
303971cdb697SChristoph Hellwig }
304071cdb697SChristoph Hellwig 
304171cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
304271cdb697SChristoph Hellwig 			 u32 flags)
304371cdb697SChristoph Hellwig {
304471cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
304571cdb697SChristoph Hellwig 	const struct pr_ops *ops;
304671cdb697SChristoph Hellwig 	fmode_t mode;
3047956a4025SMike Snitzer 	int r;
304871cdb697SChristoph Hellwig 
3049519049afSMike Snitzer 	r = dm_get_bdev_for_ioctl(md, &bdev, &mode);
305071cdb697SChristoph Hellwig 	if (r < 0)
305171cdb697SChristoph Hellwig 		return r;
305271cdb697SChristoph Hellwig 
305371cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
305471cdb697SChristoph Hellwig 	if (ops && ops->pr_reserve)
305571cdb697SChristoph Hellwig 		r = ops->pr_reserve(bdev, key, type, flags);
305671cdb697SChristoph Hellwig 	else
305771cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
305871cdb697SChristoph Hellwig 
3059519049afSMike Snitzer 	blkdev_put(bdev, mode);
306071cdb697SChristoph Hellwig 	return r;
306171cdb697SChristoph Hellwig }
306271cdb697SChristoph Hellwig 
306371cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
306471cdb697SChristoph Hellwig {
306571cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
306671cdb697SChristoph Hellwig 	const struct pr_ops *ops;
306771cdb697SChristoph Hellwig 	fmode_t mode;
3068956a4025SMike Snitzer 	int r;
306971cdb697SChristoph Hellwig 
3070519049afSMike Snitzer 	r = dm_get_bdev_for_ioctl(md, &bdev, &mode);
307171cdb697SChristoph Hellwig 	if (r < 0)
307271cdb697SChristoph Hellwig 		return r;
307371cdb697SChristoph Hellwig 
307471cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
307571cdb697SChristoph Hellwig 	if (ops && ops->pr_release)
307671cdb697SChristoph Hellwig 		r = ops->pr_release(bdev, key, type);
307771cdb697SChristoph Hellwig 	else
307871cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
307971cdb697SChristoph Hellwig 
3080519049afSMike Snitzer 	blkdev_put(bdev, mode);
308171cdb697SChristoph Hellwig 	return r;
308271cdb697SChristoph Hellwig }
308371cdb697SChristoph Hellwig 
308471cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
308571cdb697SChristoph Hellwig 			 enum pr_type type, bool abort)
308671cdb697SChristoph Hellwig {
308771cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
308871cdb697SChristoph Hellwig 	const struct pr_ops *ops;
308971cdb697SChristoph Hellwig 	fmode_t mode;
3090956a4025SMike Snitzer 	int r;
309171cdb697SChristoph Hellwig 
3092519049afSMike Snitzer 	r = dm_get_bdev_for_ioctl(md, &bdev, &mode);
309371cdb697SChristoph Hellwig 	if (r < 0)
309471cdb697SChristoph Hellwig 		return r;
309571cdb697SChristoph Hellwig 
309671cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
309771cdb697SChristoph Hellwig 	if (ops && ops->pr_preempt)
309871cdb697SChristoph Hellwig 		r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
309971cdb697SChristoph Hellwig 	else
310071cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
310171cdb697SChristoph Hellwig 
3102519049afSMike Snitzer 	blkdev_put(bdev, mode);
310371cdb697SChristoph Hellwig 	return r;
310471cdb697SChristoph Hellwig }
310571cdb697SChristoph Hellwig 
310671cdb697SChristoph Hellwig static int dm_pr_clear(struct block_device *bdev, u64 key)
310771cdb697SChristoph Hellwig {
310871cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
310971cdb697SChristoph Hellwig 	const struct pr_ops *ops;
311071cdb697SChristoph Hellwig 	fmode_t mode;
3111956a4025SMike Snitzer 	int r;
311271cdb697SChristoph Hellwig 
3113519049afSMike Snitzer 	r = dm_get_bdev_for_ioctl(md, &bdev, &mode);
311471cdb697SChristoph Hellwig 	if (r < 0)
311571cdb697SChristoph Hellwig 		return r;
311671cdb697SChristoph Hellwig 
311771cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
311871cdb697SChristoph Hellwig 	if (ops && ops->pr_clear)
311971cdb697SChristoph Hellwig 		r = ops->pr_clear(bdev, key);
312071cdb697SChristoph Hellwig 	else
312171cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
312271cdb697SChristoph Hellwig 
3123519049afSMike Snitzer 	blkdev_put(bdev, mode);
312471cdb697SChristoph Hellwig 	return r;
312571cdb697SChristoph Hellwig }
312671cdb697SChristoph Hellwig 
312771cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = {
312871cdb697SChristoph Hellwig 	.pr_register	= dm_pr_register,
312971cdb697SChristoph Hellwig 	.pr_reserve	= dm_pr_reserve,
313071cdb697SChristoph Hellwig 	.pr_release	= dm_pr_release,
313171cdb697SChristoph Hellwig 	.pr_preempt	= dm_pr_preempt,
313271cdb697SChristoph Hellwig 	.pr_clear	= dm_pr_clear,
313371cdb697SChristoph Hellwig };
313471cdb697SChristoph Hellwig 
313583d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
31361da177e4SLinus Torvalds 	.open = dm_blk_open,
31371da177e4SLinus Torvalds 	.release = dm_blk_close,
3138aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
31393ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
314071cdb697SChristoph Hellwig 	.pr_ops = &dm_pr_ops,
31411da177e4SLinus Torvalds 	.owner = THIS_MODULE
31421da177e4SLinus Torvalds };
31431da177e4SLinus Torvalds 
3144f26c5719SDan Williams static const struct dax_operations dm_dax_ops = {
3145f26c5719SDan Williams 	.direct_access = dm_dax_direct_access,
31467e026c8cSDan Williams 	.copy_from_iter = dm_dax_copy_from_iter,
3147f26c5719SDan Williams };
3148f26c5719SDan Williams 
31491da177e4SLinus Torvalds /*
31501da177e4SLinus Torvalds  * module hooks
31511da177e4SLinus Torvalds  */
31521da177e4SLinus Torvalds module_init(dm_init);
31531da177e4SLinus Torvalds module_exit(dm_exit);
31541da177e4SLinus Torvalds 
31551da177e4SLinus Torvalds module_param(major, uint, 0);
31561da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
3157f4790826SMike Snitzer 
3158e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3159e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3160e8603136SMike Snitzer 
3161115485e8SMike Snitzer module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
3162115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
3163115485e8SMike Snitzer 
31641da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
31651da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
31661da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3167