xref: /openbmc/linux/drivers/md/dm.c (revision 47ace7e012b9f7ad71d43ac9063d335ea3d6820b)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3784aae73SMilan Broz  * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This file is released under the GPL.
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
84cc96131SMike Snitzer #include "dm-core.h"
94cc96131SMike Snitzer #include "dm-rq.h"
1051e5b2bdSMike Anderson #include "dm-uevent.h"
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/init.h>
131da177e4SLinus Torvalds #include <linux/module.h>
1448c9c27bSArjan van de Ven #include <linux/mutex.h>
15174cd4b1SIngo Molnar #include <linux/sched/signal.h>
161da177e4SLinus Torvalds #include <linux/blkpg.h>
171da177e4SLinus Torvalds #include <linux/bio.h>
181da177e4SLinus Torvalds #include <linux/mempool.h>
19f26c5719SDan Williams #include <linux/dax.h>
201da177e4SLinus Torvalds #include <linux/slab.h>
211da177e4SLinus Torvalds #include <linux/idr.h>
227e026c8cSDan Williams #include <linux/uio.h>
233ac51e74SDarrick J. Wong #include <linux/hdreg.h>
243f77316dSKiyoshi Ueda #include <linux/delay.h>
25ffcc3936SMike Snitzer #include <linux/wait.h>
2671cdb697SChristoph Hellwig #include <linux/pr.h>
27b0b4d7c6SElena Reshetova #include <linux/refcount.h>
2855782138SLi Zefan 
2972d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
3072d94861SAlasdair G Kergon 
3160935eb2SMilan Broz /*
3260935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
3360935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
3460935eb2SMilan Broz  */
3560935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
3660935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
3760935eb2SMilan Broz 
381da177e4SLinus Torvalds static const char *_name = DM_NAME;
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds static unsigned int major = 0;
411da177e4SLinus Torvalds static unsigned int _major = 0;
421da177e4SLinus Torvalds 
43d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr);
44d15b774cSAlasdair G Kergon 
45f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
462c140a24SMikulas Patocka 
472c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w);
482c140a24SMikulas Patocka 
492c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
502c140a24SMikulas Patocka 
51acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue;
52acfe0ad7SMikulas Patocka 
5393e6442cSMikulas Patocka atomic_t dm_global_event_nr = ATOMIC_INIT(0);
5493e6442cSMikulas Patocka DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq);
5593e6442cSMikulas Patocka 
5662e08243SMikulas Patocka void dm_issue_global_event(void)
5762e08243SMikulas Patocka {
5862e08243SMikulas Patocka 	atomic_inc(&dm_global_event_nr);
5962e08243SMikulas Patocka 	wake_up(&dm_global_eventq);
6062e08243SMikulas Patocka }
6162e08243SMikulas Patocka 
621da177e4SLinus Torvalds /*
6364f52b0eSMike Snitzer  * One of these is allocated (on-stack) per original bio.
641da177e4SLinus Torvalds  */
6564f52b0eSMike Snitzer struct clone_info {
6664f52b0eSMike Snitzer 	struct dm_table *map;
6764f52b0eSMike Snitzer 	struct bio *bio;
6864f52b0eSMike Snitzer 	struct dm_io *io;
6964f52b0eSMike Snitzer 	sector_t sector;
7064f52b0eSMike Snitzer 	unsigned sector_count;
7164f52b0eSMike Snitzer };
7264f52b0eSMike Snitzer 
7364f52b0eSMike Snitzer /*
7464f52b0eSMike Snitzer  * One of these is allocated per clone bio.
7564f52b0eSMike Snitzer  */
7664f52b0eSMike Snitzer #define DM_TIO_MAGIC 7282014
7764f52b0eSMike Snitzer struct dm_target_io {
7864f52b0eSMike Snitzer 	unsigned magic;
7964f52b0eSMike Snitzer 	struct dm_io *io;
8064f52b0eSMike Snitzer 	struct dm_target *ti;
8164f52b0eSMike Snitzer 	unsigned target_bio_nr;
8264f52b0eSMike Snitzer 	unsigned *len_ptr;
8364f52b0eSMike Snitzer 	bool inside_dm_io;
8464f52b0eSMike Snitzer 	struct bio clone;
8564f52b0eSMike Snitzer };
8664f52b0eSMike Snitzer 
8764f52b0eSMike Snitzer /*
8864f52b0eSMike Snitzer  * One of these is allocated per original bio.
8964f52b0eSMike Snitzer  * It contains the first clone used for that original.
9064f52b0eSMike Snitzer  */
9164f52b0eSMike Snitzer #define DM_IO_MAGIC 5191977
921da177e4SLinus Torvalds struct dm_io {
9364f52b0eSMike Snitzer 	unsigned magic;
941da177e4SLinus Torvalds 	struct mapped_device *md;
954e4cbee9SChristoph Hellwig 	blk_status_t status;
961da177e4SLinus Torvalds 	atomic_t io_count;
97745dc570SMike Snitzer 	struct bio *orig_bio;
983eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
99f88fb981SKiyoshi Ueda 	spinlock_t endio_lock;
100fd2ed4d2SMikulas Patocka 	struct dm_stats_aux stats_aux;
10164f52b0eSMike Snitzer 	/* last member of dm_target_io is 'struct bio' */
10264f52b0eSMike Snitzer 	struct dm_target_io tio;
1031da177e4SLinus Torvalds };
1041da177e4SLinus Torvalds 
10564f52b0eSMike Snitzer void *dm_per_bio_data(struct bio *bio, size_t data_size)
10664f52b0eSMike Snitzer {
10764f52b0eSMike Snitzer 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
10864f52b0eSMike Snitzer 	if (!tio->inside_dm_io)
10964f52b0eSMike Snitzer 		return (char *)bio - offsetof(struct dm_target_io, clone) - data_size;
11064f52b0eSMike Snitzer 	return (char *)bio - offsetof(struct dm_target_io, clone) - offsetof(struct dm_io, tio) - data_size;
11164f52b0eSMike Snitzer }
11264f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_per_bio_data);
11364f52b0eSMike Snitzer 
11464f52b0eSMike Snitzer struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size)
11564f52b0eSMike Snitzer {
11664f52b0eSMike Snitzer 	struct dm_io *io = (struct dm_io *)((char *)data + data_size);
11764f52b0eSMike Snitzer 	if (io->magic == DM_IO_MAGIC)
11864f52b0eSMike Snitzer 		return (struct bio *)((char *)io + offsetof(struct dm_io, tio) + offsetof(struct dm_target_io, clone));
11964f52b0eSMike Snitzer 	BUG_ON(io->magic != DM_TIO_MAGIC);
12064f52b0eSMike Snitzer 	return (struct bio *)((char *)io + offsetof(struct dm_target_io, clone));
12164f52b0eSMike Snitzer }
12264f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_from_per_bio_data);
12364f52b0eSMike Snitzer 
12464f52b0eSMike Snitzer unsigned dm_bio_get_target_bio_nr(const struct bio *bio)
12564f52b0eSMike Snitzer {
12664f52b0eSMike Snitzer 	return container_of(bio, struct dm_target_io, clone)->target_bio_nr;
12764f52b0eSMike Snitzer }
12864f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_get_target_bio_nr);
12964f52b0eSMike Snitzer 
130ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
131ba61fdd1SJeff Mahoney 
1321da177e4SLinus Torvalds /*
1331da177e4SLinus Torvalds  * Bits for the md->flags field.
1341da177e4SLinus Torvalds  */
1351eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
1361da177e4SLinus Torvalds #define DMF_SUSPENDED 1
137aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
138fba9f90eSJeff Mahoney #define DMF_FREEING 3
1395c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
1402e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
1418ae12666SKent Overstreet #define DMF_DEFERRED_REMOVE 6
1428ae12666SKent Overstreet #define DMF_SUSPENDED_INTERNALLY 7
1431da177e4SLinus Torvalds 
144115485e8SMike Snitzer #define DM_NUMA_NODE NUMA_NO_NODE
145115485e8SMike Snitzer static int dm_numa_node = DM_NUMA_NODE;
146faad87dfSMike Snitzer 
147e6ee8c0bSKiyoshi Ueda /*
148e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
149e6ee8c0bSKiyoshi Ueda  */
150e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
1516f1c819cSKent Overstreet 	struct bio_set bs;
1526f1c819cSKent Overstreet 	struct bio_set io_bs;
153e6ee8c0bSKiyoshi Ueda };
154e6ee8c0bSKiyoshi Ueda 
15586f1152bSBenjamin Marzinski struct table_device {
15686f1152bSBenjamin Marzinski 	struct list_head list;
157b0b4d7c6SElena Reshetova 	refcount_t count;
15886f1152bSBenjamin Marzinski 	struct dm_dev dm_dev;
15986f1152bSBenjamin Marzinski };
16086f1152bSBenjamin Marzinski 
161f4790826SMike Snitzer /*
162e8603136SMike Snitzer  * Bio-based DM's mempools' reserved IOs set by the user.
163e8603136SMike Snitzer  */
1644cc96131SMike Snitzer #define RESERVED_BIO_BASED_IOS		16
165e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
166e8603136SMike Snitzer 
167115485e8SMike Snitzer static int __dm_get_module_param_int(int *module_param, int min, int max)
168115485e8SMike Snitzer {
1696aa7de05SMark Rutland 	int param = READ_ONCE(*module_param);
170115485e8SMike Snitzer 	int modified_param = 0;
171115485e8SMike Snitzer 	bool modified = true;
172115485e8SMike Snitzer 
173115485e8SMike Snitzer 	if (param < min)
174115485e8SMike Snitzer 		modified_param = min;
175115485e8SMike Snitzer 	else if (param > max)
176115485e8SMike Snitzer 		modified_param = max;
177115485e8SMike Snitzer 	else
178115485e8SMike Snitzer 		modified = false;
179115485e8SMike Snitzer 
180115485e8SMike Snitzer 	if (modified) {
181115485e8SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
182115485e8SMike Snitzer 		param = modified_param;
183115485e8SMike Snitzer 	}
184115485e8SMike Snitzer 
185115485e8SMike Snitzer 	return param;
186115485e8SMike Snitzer }
187115485e8SMike Snitzer 
1884cc96131SMike Snitzer unsigned __dm_get_module_param(unsigned *module_param,
189f4790826SMike Snitzer 			       unsigned def, unsigned max)
190f4790826SMike Snitzer {
1916aa7de05SMark Rutland 	unsigned param = READ_ONCE(*module_param);
19209c2d531SMike Snitzer 	unsigned modified_param = 0;
193f4790826SMike Snitzer 
19409c2d531SMike Snitzer 	if (!param)
19509c2d531SMike Snitzer 		modified_param = def;
19609c2d531SMike Snitzer 	else if (param > max)
19709c2d531SMike Snitzer 		modified_param = max;
198f4790826SMike Snitzer 
19909c2d531SMike Snitzer 	if (modified_param) {
20009c2d531SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
20109c2d531SMike Snitzer 		param = modified_param;
202f4790826SMike Snitzer 	}
203f4790826SMike Snitzer 
20409c2d531SMike Snitzer 	return param;
205f4790826SMike Snitzer }
206f4790826SMike Snitzer 
207e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void)
208e8603136SMike Snitzer {
20909c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_bio_based_ios,
2104cc96131SMike Snitzer 				     RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS);
211e8603136SMike Snitzer }
212e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
213e8603136SMike Snitzer 
214115485e8SMike Snitzer static unsigned dm_get_numa_node(void)
215115485e8SMike Snitzer {
216115485e8SMike Snitzer 	return __dm_get_module_param_int(&dm_numa_node,
217115485e8SMike Snitzer 					 DM_NUMA_NODE, num_online_nodes() - 1);
218115485e8SMike Snitzer }
219115485e8SMike Snitzer 
2201da177e4SLinus Torvalds static int __init local_init(void)
2211da177e4SLinus Torvalds {
222e689fbabSMike Snitzer 	int r;
2231ae49ea2SMike Snitzer 
22451e5b2bdSMike Anderson 	r = dm_uevent_init();
22551157b4aSKiyoshi Ueda 	if (r)
226e689fbabSMike Snitzer 		return r;
22751e5b2bdSMike Anderson 
228acfe0ad7SMikulas Patocka 	deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
229acfe0ad7SMikulas Patocka 	if (!deferred_remove_workqueue) {
230acfe0ad7SMikulas Patocka 		r = -ENOMEM;
231acfe0ad7SMikulas Patocka 		goto out_uevent_exit;
232acfe0ad7SMikulas Patocka 	}
233acfe0ad7SMikulas Patocka 
2341da177e4SLinus Torvalds 	_major = major;
2351da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
23651157b4aSKiyoshi Ueda 	if (r < 0)
237acfe0ad7SMikulas Patocka 		goto out_free_workqueue;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	if (!_major)
2401da177e4SLinus Torvalds 		_major = r;
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	return 0;
24351157b4aSKiyoshi Ueda 
244acfe0ad7SMikulas Patocka out_free_workqueue:
245acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
24651157b4aSKiyoshi Ueda out_uevent_exit:
24751157b4aSKiyoshi Ueda 	dm_uevent_exit();
24851157b4aSKiyoshi Ueda 
24951157b4aSKiyoshi Ueda 	return r;
2501da177e4SLinus Torvalds }
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds static void local_exit(void)
2531da177e4SLinus Torvalds {
2542c140a24SMikulas Patocka 	flush_scheduled_work();
255acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
2562c140a24SMikulas Patocka 
25700d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
25851e5b2bdSMike Anderson 	dm_uevent_exit();
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	_major = 0;
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	DMINFO("cleaned up");
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds 
265b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
2661da177e4SLinus Torvalds 	local_init,
2671da177e4SLinus Torvalds 	dm_target_init,
2681da177e4SLinus Torvalds 	dm_linear_init,
2691da177e4SLinus Torvalds 	dm_stripe_init,
270952b3557SMikulas Patocka 	dm_io_init,
271945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2721da177e4SLinus Torvalds 	dm_interface_init,
273fd2ed4d2SMikulas Patocka 	dm_statistics_init,
2741da177e4SLinus Torvalds };
2751da177e4SLinus Torvalds 
276b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2771da177e4SLinus Torvalds 	local_exit,
2781da177e4SLinus Torvalds 	dm_target_exit,
2791da177e4SLinus Torvalds 	dm_linear_exit,
2801da177e4SLinus Torvalds 	dm_stripe_exit,
281952b3557SMikulas Patocka 	dm_io_exit,
282945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
2831da177e4SLinus Torvalds 	dm_interface_exit,
284fd2ed4d2SMikulas Patocka 	dm_statistics_exit,
2851da177e4SLinus Torvalds };
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds static int __init dm_init(void)
2881da177e4SLinus Torvalds {
2891da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	int r, i;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
2941da177e4SLinus Torvalds 		r = _inits[i]();
2951da177e4SLinus Torvalds 		if (r)
2961da177e4SLinus Torvalds 			goto bad;
2971da177e4SLinus Torvalds 	}
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	return 0;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds       bad:
3021da177e4SLinus Torvalds 	while (i--)
3031da177e4SLinus Torvalds 		_exits[i]();
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	return r;
3061da177e4SLinus Torvalds }
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds static void __exit dm_exit(void)
3091da177e4SLinus Torvalds {
3101da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	while (i--)
3131da177e4SLinus Torvalds 		_exits[i]();
314d15b774cSAlasdair G Kergon 
315d15b774cSAlasdair G Kergon 	/*
316d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
317d15b774cSAlasdair G Kergon 	 */
318d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
3191da177e4SLinus Torvalds }
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds /*
3221da177e4SLinus Torvalds  * Block device functions
3231da177e4SLinus Torvalds  */
324432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
325432a212cSMike Anderson {
326432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
327432a212cSMike Anderson }
328432a212cSMike Anderson 
329fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3301da177e4SLinus Torvalds {
3311da177e4SLinus Torvalds 	struct mapped_device *md;
3321da177e4SLinus Torvalds 
333fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
334fba9f90eSJeff Mahoney 
335fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
336fba9f90eSJeff Mahoney 	if (!md)
337fba9f90eSJeff Mahoney 		goto out;
338fba9f90eSJeff Mahoney 
3395c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
340432a212cSMike Anderson 	    dm_deleting_md(md)) {
341fba9f90eSJeff Mahoney 		md = NULL;
342fba9f90eSJeff Mahoney 		goto out;
343fba9f90eSJeff Mahoney 	}
344fba9f90eSJeff Mahoney 
3451da177e4SLinus Torvalds 	dm_get(md);
3465c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
347fba9f90eSJeff Mahoney out:
348fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
349fba9f90eSJeff Mahoney 
350fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
353db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode)
3541da177e4SLinus Torvalds {
35563a4f065SMike Snitzer 	struct mapped_device *md;
3566e9624b8SArnd Bergmann 
3574a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
3584a1aeb98SMilan Broz 
35963a4f065SMike Snitzer 	md = disk->private_data;
36063a4f065SMike Snitzer 	if (WARN_ON(!md))
36163a4f065SMike Snitzer 		goto out;
36263a4f065SMike Snitzer 
3632c140a24SMikulas Patocka 	if (atomic_dec_and_test(&md->open_count) &&
3642c140a24SMikulas Patocka 	    (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
365acfe0ad7SMikulas Patocka 		queue_work(deferred_remove_workqueue, &deferred_remove_work);
3662c140a24SMikulas Patocka 
3671da177e4SLinus Torvalds 	dm_put(md);
36863a4f065SMike Snitzer out:
3694a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
3725c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3735c6bd75dSAlasdair G Kergon {
3745c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3755c6bd75dSAlasdair G Kergon }
3765c6bd75dSAlasdair G Kergon 
3775c6bd75dSAlasdair G Kergon /*
3785c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3795c6bd75dSAlasdair G Kergon  */
3802c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
3815c6bd75dSAlasdair G Kergon {
3825c6bd75dSAlasdair G Kergon 	int r = 0;
3835c6bd75dSAlasdair G Kergon 
3845c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
3855c6bd75dSAlasdair G Kergon 
3862c140a24SMikulas Patocka 	if (dm_open_count(md)) {
3875c6bd75dSAlasdair G Kergon 		r = -EBUSY;
3882c140a24SMikulas Patocka 		if (mark_deferred)
3892c140a24SMikulas Patocka 			set_bit(DMF_DEFERRED_REMOVE, &md->flags);
3902c140a24SMikulas Patocka 	} else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
3912c140a24SMikulas Patocka 		r = -EEXIST;
3925c6bd75dSAlasdair G Kergon 	else
3935c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
3945c6bd75dSAlasdair G Kergon 
3955c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
3965c6bd75dSAlasdair G Kergon 
3975c6bd75dSAlasdair G Kergon 	return r;
3985c6bd75dSAlasdair G Kergon }
3995c6bd75dSAlasdair G Kergon 
4002c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md)
4012c140a24SMikulas Patocka {
4022c140a24SMikulas Patocka 	int r = 0;
4032c140a24SMikulas Patocka 
4042c140a24SMikulas Patocka 	spin_lock(&_minor_lock);
4052c140a24SMikulas Patocka 
4062c140a24SMikulas Patocka 	if (test_bit(DMF_DELETING, &md->flags))
4072c140a24SMikulas Patocka 		r = -EBUSY;
4082c140a24SMikulas Patocka 	else
4092c140a24SMikulas Patocka 		clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
4102c140a24SMikulas Patocka 
4112c140a24SMikulas Patocka 	spin_unlock(&_minor_lock);
4122c140a24SMikulas Patocka 
4132c140a24SMikulas Patocka 	return r;
4142c140a24SMikulas Patocka }
4152c140a24SMikulas Patocka 
4162c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w)
4172c140a24SMikulas Patocka {
4182c140a24SMikulas Patocka 	dm_deferred_remove();
4192c140a24SMikulas Patocka }
4202c140a24SMikulas Patocka 
421fd2ed4d2SMikulas Patocka sector_t dm_get_size(struct mapped_device *md)
422fd2ed4d2SMikulas Patocka {
423fd2ed4d2SMikulas Patocka 	return get_capacity(md->disk);
424fd2ed4d2SMikulas Patocka }
425fd2ed4d2SMikulas Patocka 
4269974fa2cSMike Snitzer struct request_queue *dm_get_md_queue(struct mapped_device *md)
4279974fa2cSMike Snitzer {
4289974fa2cSMike Snitzer 	return md->queue;
4299974fa2cSMike Snitzer }
4309974fa2cSMike Snitzer 
431fd2ed4d2SMikulas Patocka struct dm_stats *dm_get_stats(struct mapped_device *md)
432fd2ed4d2SMikulas Patocka {
433fd2ed4d2SMikulas Patocka 	return &md->stats;
434fd2ed4d2SMikulas Patocka }
435fd2ed4d2SMikulas Patocka 
4363ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4373ac51e74SDarrick J. Wong {
4383ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
4393ac51e74SDarrick J. Wong 
4403ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
4413ac51e74SDarrick J. Wong }
4423ac51e74SDarrick J. Wong 
443e76239a3SChristoph Hellwig #ifdef CONFIG_BLK_DEV_ZONED
444d4100351SChristoph Hellwig int dm_report_zones_cb(struct blk_zone *zone, unsigned int idx, void *data)
445d4100351SChristoph Hellwig {
446d4100351SChristoph Hellwig 	struct dm_report_zones_args *args = data;
447d4100351SChristoph Hellwig 	sector_t sector_diff = args->tgt->begin - args->start;
448d4100351SChristoph Hellwig 
449d4100351SChristoph Hellwig 	/*
450d4100351SChristoph Hellwig 	 * Ignore zones beyond the target range.
451d4100351SChristoph Hellwig 	 */
452d4100351SChristoph Hellwig 	if (zone->start >= args->start + args->tgt->len)
453d4100351SChristoph Hellwig 		return 0;
454d4100351SChristoph Hellwig 
455d4100351SChristoph Hellwig 	/*
456d4100351SChristoph Hellwig 	 * Remap the start sector and write pointer position of the zone
457d4100351SChristoph Hellwig 	 * to match its position in the target range.
458d4100351SChristoph Hellwig 	 */
459d4100351SChristoph Hellwig 	zone->start += sector_diff;
460d4100351SChristoph Hellwig 	if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL) {
461d4100351SChristoph Hellwig 		if (zone->cond == BLK_ZONE_COND_FULL)
462d4100351SChristoph Hellwig 			zone->wp = zone->start + zone->len;
463d4100351SChristoph Hellwig 		else if (zone->cond == BLK_ZONE_COND_EMPTY)
464d4100351SChristoph Hellwig 			zone->wp = zone->start;
465d4100351SChristoph Hellwig 		else
466d4100351SChristoph Hellwig 			zone->wp += sector_diff;
467d4100351SChristoph Hellwig 	}
468d4100351SChristoph Hellwig 
469d4100351SChristoph Hellwig 	args->next_sector = zone->start + zone->len;
470d4100351SChristoph Hellwig 	return args->orig_cb(zone, args->zone_idx++, args->orig_data);
471d4100351SChristoph Hellwig }
472d4100351SChristoph Hellwig EXPORT_SYMBOL_GPL(dm_report_zones_cb);
473d4100351SChristoph Hellwig 
474d4100351SChristoph Hellwig static int dm_blk_report_zones(struct gendisk *disk, sector_t sector,
475d4100351SChristoph Hellwig 		unsigned int nr_zones, report_zones_cb cb, void *data)
476d4100351SChristoph Hellwig {
477e76239a3SChristoph Hellwig 	struct mapped_device *md = disk->private_data;
478e76239a3SChristoph Hellwig 	struct dm_table *map;
479e76239a3SChristoph Hellwig 	int srcu_idx, ret;
480d4100351SChristoph Hellwig 	struct dm_report_zones_args args = {
481d4100351SChristoph Hellwig 		.next_sector = sector,
482d4100351SChristoph Hellwig 		.orig_data = data,
483d4100351SChristoph Hellwig 		.orig_cb = cb,
484d4100351SChristoph Hellwig 	};
485e76239a3SChristoph Hellwig 
486e76239a3SChristoph Hellwig 	if (dm_suspended_md(md))
487e76239a3SChristoph Hellwig 		return -EAGAIN;
488e76239a3SChristoph Hellwig 
489e76239a3SChristoph Hellwig 	map = dm_get_live_table(md, &srcu_idx);
490e76239a3SChristoph Hellwig 	if (!map)
491e76239a3SChristoph Hellwig 		return -EIO;
492e76239a3SChristoph Hellwig 
493d4100351SChristoph Hellwig 	do {
494d4100351SChristoph Hellwig 		struct dm_target *tgt;
495d4100351SChristoph Hellwig 
496d4100351SChristoph Hellwig 		tgt = dm_table_find_target(map, args.next_sector);
497d4100351SChristoph Hellwig 		if (WARN_ON_ONCE(!tgt->type->report_zones)) {
498e76239a3SChristoph Hellwig 			ret = -EIO;
499e76239a3SChristoph Hellwig 			goto out;
500e76239a3SChristoph Hellwig 		}
501e76239a3SChristoph Hellwig 
502d4100351SChristoph Hellwig 		args.tgt = tgt;
503d4100351SChristoph Hellwig 		ret = tgt->type->report_zones(tgt, &args, nr_zones);
504d4100351SChristoph Hellwig 		if (ret < 0)
505e76239a3SChristoph Hellwig 			goto out;
506d4100351SChristoph Hellwig 	} while (args.zone_idx < nr_zones &&
507d4100351SChristoph Hellwig 		 args.next_sector < get_capacity(disk));
508e76239a3SChristoph Hellwig 
509d4100351SChristoph Hellwig 	ret = args.zone_idx;
510e76239a3SChristoph Hellwig out:
511e76239a3SChristoph Hellwig 	dm_put_live_table(md, srcu_idx);
512e76239a3SChristoph Hellwig 	return ret;
513e76239a3SChristoph Hellwig }
514d4100351SChristoph Hellwig #else
515d4100351SChristoph Hellwig #define dm_blk_report_zones		NULL
516d4100351SChristoph Hellwig #endif /* CONFIG_BLK_DEV_ZONED */
517e76239a3SChristoph Hellwig 
518971888c4SMike Snitzer static int dm_prepare_ioctl(struct mapped_device *md, int *srcu_idx,
5195bd5e8d8SMike Snitzer 			    struct block_device **bdev)
520971888c4SMike Snitzer 	__acquires(md->io_barrier)
521aa129a22SMilan Broz {
52266482026SMike Snitzer 	struct dm_target *tgt;
5236c182cd8SHannes Reinecke 	struct dm_table *map;
524971888c4SMike Snitzer 	int r;
525aa129a22SMilan Broz 
5266c182cd8SHannes Reinecke retry:
527e56f81e0SChristoph Hellwig 	r = -ENOTTY;
528971888c4SMike Snitzer 	map = dm_get_live_table(md, srcu_idx);
529aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
530971888c4SMike Snitzer 		return r;
531aa129a22SMilan Broz 
532aa129a22SMilan Broz 	/* We only support devices that have a single target */
533aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
534971888c4SMike Snitzer 		return r;
535aa129a22SMilan Broz 
53666482026SMike Snitzer 	tgt = dm_table_get_target(map, 0);
53766482026SMike Snitzer 	if (!tgt->type->prepare_ioctl)
538e56f81e0SChristoph Hellwig 		return r;
539aa129a22SMilan Broz 
540971888c4SMike Snitzer 	if (dm_suspended_md(md))
541971888c4SMike Snitzer 		return -EAGAIN;
542971888c4SMike Snitzer 
5435bd5e8d8SMike Snitzer 	r = tgt->type->prepare_ioctl(tgt, bdev);
5445bbbfdf6SJunichi Nomura 	if (r == -ENOTCONN && !fatal_signal_pending(current)) {
545971888c4SMike Snitzer 		dm_put_live_table(md, *srcu_idx);
5466c182cd8SHannes Reinecke 		msleep(10);
5476c182cd8SHannes Reinecke 		goto retry;
5486c182cd8SHannes Reinecke 	}
549971888c4SMike Snitzer 
550e56f81e0SChristoph Hellwig 	return r;
551e56f81e0SChristoph Hellwig }
5526c182cd8SHannes Reinecke 
553971888c4SMike Snitzer static void dm_unprepare_ioctl(struct mapped_device *md, int srcu_idx)
554971888c4SMike Snitzer 	__releases(md->io_barrier)
555971888c4SMike Snitzer {
556971888c4SMike Snitzer 	dm_put_live_table(md, srcu_idx);
557971888c4SMike Snitzer }
558971888c4SMike Snitzer 
559e56f81e0SChristoph Hellwig static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
560e56f81e0SChristoph Hellwig 			unsigned int cmd, unsigned long arg)
561e56f81e0SChristoph Hellwig {
562e56f81e0SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
563971888c4SMike Snitzer 	int r, srcu_idx;
564e56f81e0SChristoph Hellwig 
5655bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
566e56f81e0SChristoph Hellwig 	if (r < 0)
567971888c4SMike Snitzer 		goto out;
568e56f81e0SChristoph Hellwig 
569e56f81e0SChristoph Hellwig 	if (r > 0) {
570e56f81e0SChristoph Hellwig 		/*
571e980f623SChristoph Hellwig 		 * Target determined this ioctl is being issued against a
572e980f623SChristoph Hellwig 		 * subset of the parent bdev; require extra privileges.
573e56f81e0SChristoph Hellwig 		 */
574e980f623SChristoph Hellwig 		if (!capable(CAP_SYS_RAWIO)) {
575e980f623SChristoph Hellwig 			DMWARN_LIMIT(
576e980f623SChristoph Hellwig 	"%s: sending ioctl %x to DM device without required privilege.",
577e980f623SChristoph Hellwig 				current->comm, cmd);
578e980f623SChristoph Hellwig 			r = -ENOIOCTLCMD;
579e56f81e0SChristoph Hellwig 			goto out;
580e56f81e0SChristoph Hellwig 		}
581e980f623SChristoph Hellwig 	}
582e56f81e0SChristoph Hellwig 
58366482026SMike Snitzer 	r =  __blkdev_driver_ioctl(bdev, mode, cmd, arg);
584e56f81e0SChristoph Hellwig out:
585971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
586aa129a22SMilan Broz 	return r;
587aa129a22SMilan Broz }
588aa129a22SMilan Broz 
589978e51baSMike Snitzer static void start_io_acct(struct dm_io *io);
590978e51baSMike Snitzer 
591978e51baSMike Snitzer static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio)
5921da177e4SLinus Torvalds {
59364f52b0eSMike Snitzer 	struct dm_io *io;
59464f52b0eSMike Snitzer 	struct dm_target_io *tio;
59564f52b0eSMike Snitzer 	struct bio *clone;
59664f52b0eSMike Snitzer 
5976f1c819cSKent Overstreet 	clone = bio_alloc_bioset(GFP_NOIO, 0, &md->io_bs);
59864f52b0eSMike Snitzer 	if (!clone)
59964f52b0eSMike Snitzer 		return NULL;
60064f52b0eSMike Snitzer 
60164f52b0eSMike Snitzer 	tio = container_of(clone, struct dm_target_io, clone);
60264f52b0eSMike Snitzer 	tio->inside_dm_io = true;
60364f52b0eSMike Snitzer 	tio->io = NULL;
60464f52b0eSMike Snitzer 
60564f52b0eSMike Snitzer 	io = container_of(tio, struct dm_io, tio);
60664f52b0eSMike Snitzer 	io->magic = DM_IO_MAGIC;
607978e51baSMike Snitzer 	io->status = 0;
608978e51baSMike Snitzer 	atomic_set(&io->io_count, 1);
609978e51baSMike Snitzer 	io->orig_bio = bio;
610978e51baSMike Snitzer 	io->md = md;
611978e51baSMike Snitzer 	spin_lock_init(&io->endio_lock);
612978e51baSMike Snitzer 
613978e51baSMike Snitzer 	start_io_acct(io);
61464f52b0eSMike Snitzer 
61564f52b0eSMike Snitzer 	return io;
6161da177e4SLinus Torvalds }
6171da177e4SLinus Torvalds 
618028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
6191da177e4SLinus Torvalds {
62064f52b0eSMike Snitzer 	bio_put(&io->tio.clone);
62164f52b0eSMike Snitzer }
62264f52b0eSMike Snitzer 
62364f52b0eSMike Snitzer static struct dm_target_io *alloc_tio(struct clone_info *ci, struct dm_target *ti,
62464f52b0eSMike Snitzer 				      unsigned target_bio_nr, gfp_t gfp_mask)
62564f52b0eSMike Snitzer {
62664f52b0eSMike Snitzer 	struct dm_target_io *tio;
62764f52b0eSMike Snitzer 
62864f52b0eSMike Snitzer 	if (!ci->io->tio.io) {
62964f52b0eSMike Snitzer 		/* the dm_target_io embedded in ci->io is available */
63064f52b0eSMike Snitzer 		tio = &ci->io->tio;
63164f52b0eSMike Snitzer 	} else {
6326f1c819cSKent Overstreet 		struct bio *clone = bio_alloc_bioset(gfp_mask, 0, &ci->io->md->bs);
63364f52b0eSMike Snitzer 		if (!clone)
63464f52b0eSMike Snitzer 			return NULL;
63564f52b0eSMike Snitzer 
63664f52b0eSMike Snitzer 		tio = container_of(clone, struct dm_target_io, clone);
63764f52b0eSMike Snitzer 		tio->inside_dm_io = false;
63864f52b0eSMike Snitzer 	}
63964f52b0eSMike Snitzer 
64064f52b0eSMike Snitzer 	tio->magic = DM_TIO_MAGIC;
64164f52b0eSMike Snitzer 	tio->io = ci->io;
64264f52b0eSMike Snitzer 	tio->ti = ti;
64364f52b0eSMike Snitzer 	tio->target_bio_nr = target_bio_nr;
64464f52b0eSMike Snitzer 
64564f52b0eSMike Snitzer 	return tio;
6461da177e4SLinus Torvalds }
6471da177e4SLinus Torvalds 
648cfae7529SMike Snitzer static void free_tio(struct dm_target_io *tio)
6491da177e4SLinus Torvalds {
65064f52b0eSMike Snitzer 	if (tio->inside_dm_io)
65164f52b0eSMike Snitzer 		return;
652dba14160SMikulas Patocka 	bio_put(&tio->clone);
6531da177e4SLinus Torvalds }
6541da177e4SLinus Torvalds 
655c4576aedSMike Snitzer static bool md_in_flight_bios(struct mapped_device *md)
65690abb8c4SKiyoshi Ueda {
6576f757231SMikulas Patocka 	int cpu;
6586f757231SMikulas Patocka 	struct hd_struct *part = &dm_disk(md)->part0;
659b7934ba4SJens Axboe 	long sum = 0;
6606f757231SMikulas Patocka 
6616f757231SMikulas Patocka 	for_each_possible_cpu(cpu) {
662b7934ba4SJens Axboe 		sum += part_stat_local_read_cpu(part, in_flight[0], cpu);
663b7934ba4SJens Axboe 		sum += part_stat_local_read_cpu(part, in_flight[1], cpu);
6646f757231SMikulas Patocka 	}
6656f757231SMikulas Patocka 
666b7934ba4SJens Axboe 	return sum != 0;
66790abb8c4SKiyoshi Ueda }
66890abb8c4SKiyoshi Ueda 
669c4576aedSMike Snitzer static bool md_in_flight(struct mapped_device *md)
670c4576aedSMike Snitzer {
671c4576aedSMike Snitzer 	if (queue_is_mq(md->queue))
6723c94d83cSJens Axboe 		return blk_mq_queue_inflight(md->queue);
673c4576aedSMike Snitzer 	else
674c4576aedSMike Snitzer 		return md_in_flight_bios(md);
675cec47e3dSKiyoshi Ueda }
676cec47e3dSKiyoshi Ueda 
6773eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
6783eaf840eSJun'ichi "Nick" Nomura {
6793eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
680745dc570SMike Snitzer 	struct bio *bio = io->orig_bio;
6813eaf840eSJun'ichi "Nick" Nomura 
6823eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
6833eaf840eSJun'ichi "Nick" Nomura 
684ddcf35d3SMichael Callahan 	generic_start_io_acct(md->queue, bio_op(bio), bio_sectors(bio),
685ddcf35d3SMichael Callahan 			      &dm_disk(md)->part0);
686f3986374SMike Snitzer 
687fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
688528ec5abSMike Christie 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
689528ec5abSMike Christie 				    bio->bi_iter.bi_sector, bio_sectors(bio),
690528ec5abSMike Christie 				    false, 0, &io->stats_aux);
6913eaf840eSJun'ichi "Nick" Nomura }
6923eaf840eSJun'ichi "Nick" Nomura 
693d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
6943eaf840eSJun'ichi "Nick" Nomura {
6953eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
696745dc570SMike Snitzer 	struct bio *bio = io->orig_bio;
6973eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
6983eaf840eSJun'ichi "Nick" Nomura 
699ddcf35d3SMichael Callahan 	generic_end_io_acct(md->queue, bio_op(bio), &dm_disk(md)->part0,
700ddcf35d3SMichael Callahan 			    io->start_time);
7013eaf840eSJun'ichi "Nick" Nomura 
702fd2ed4d2SMikulas Patocka 	if (unlikely(dm_stats_used(&md->stats)))
703528ec5abSMike Christie 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
704528ec5abSMike Christie 				    bio->bi_iter.bi_sector, bio_sectors(bio),
705528ec5abSMike Christie 				    true, duration, &io->stats_aux);
706fd2ed4d2SMikulas Patocka 
707d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
708645efa84SMikulas Patocka 	if (unlikely(wq_has_sleeper(&md->wait)))
709d221d2e7SMikulas Patocka 		wake_up(&md->wait);
7103eaf840eSJun'ichi "Nick" Nomura }
7113eaf840eSJun'ichi "Nick" Nomura 
7121da177e4SLinus Torvalds /*
7131da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
7141da177e4SLinus Torvalds  */
71592c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
7161da177e4SLinus Torvalds {
71705447420SKiyoshi Ueda 	unsigned long flags;
7181da177e4SLinus Torvalds 
71905447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
7201da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
72105447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
72292c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
7231da177e4SLinus Torvalds }
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds /*
7261da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
7271da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
72883d5e5b0SMikulas Patocka  * dm_put_live_table() when finished.
7291da177e4SLinus Torvalds  */
73083d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
7311da177e4SLinus Torvalds {
73283d5e5b0SMikulas Patocka 	*srcu_idx = srcu_read_lock(&md->io_barrier);
7331da177e4SLinus Torvalds 
73483d5e5b0SMikulas Patocka 	return srcu_dereference(md->map, &md->io_barrier);
73583d5e5b0SMikulas Patocka }
7361da177e4SLinus Torvalds 
73783d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
73883d5e5b0SMikulas Patocka {
73983d5e5b0SMikulas Patocka 	srcu_read_unlock(&md->io_barrier, srcu_idx);
74083d5e5b0SMikulas Patocka }
74183d5e5b0SMikulas Patocka 
74283d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md)
74383d5e5b0SMikulas Patocka {
74483d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
74583d5e5b0SMikulas Patocka 	synchronize_rcu_expedited();
74683d5e5b0SMikulas Patocka }
74783d5e5b0SMikulas Patocka 
74883d5e5b0SMikulas Patocka /*
74983d5e5b0SMikulas Patocka  * A fast alternative to dm_get_live_table/dm_put_live_table.
75083d5e5b0SMikulas Patocka  * The caller must not block between these two functions.
75183d5e5b0SMikulas Patocka  */
75283d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
75383d5e5b0SMikulas Patocka {
75483d5e5b0SMikulas Patocka 	rcu_read_lock();
75583d5e5b0SMikulas Patocka 	return rcu_dereference(md->map);
75683d5e5b0SMikulas Patocka }
75783d5e5b0SMikulas Patocka 
75883d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
75983d5e5b0SMikulas Patocka {
76083d5e5b0SMikulas Patocka 	rcu_read_unlock();
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds 
763971888c4SMike Snitzer static char *_dm_claim_ptr = "I belong to device-mapper";
764971888c4SMike Snitzer 
7653ac51e74SDarrick J. Wong /*
76686f1152bSBenjamin Marzinski  * Open a table device so we can use it as a map destination.
76786f1152bSBenjamin Marzinski  */
76886f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev,
76986f1152bSBenjamin Marzinski 			     struct mapped_device *md)
77086f1152bSBenjamin Marzinski {
77186f1152bSBenjamin Marzinski 	struct block_device *bdev;
77286f1152bSBenjamin Marzinski 
77386f1152bSBenjamin Marzinski 	int r;
77486f1152bSBenjamin Marzinski 
77586f1152bSBenjamin Marzinski 	BUG_ON(td->dm_dev.bdev);
77686f1152bSBenjamin Marzinski 
777519049afSMike Snitzer 	bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr);
77886f1152bSBenjamin Marzinski 	if (IS_ERR(bdev))
77986f1152bSBenjamin Marzinski 		return PTR_ERR(bdev);
78086f1152bSBenjamin Marzinski 
78186f1152bSBenjamin Marzinski 	r = bd_link_disk_holder(bdev, dm_disk(md));
78286f1152bSBenjamin Marzinski 	if (r) {
78386f1152bSBenjamin Marzinski 		blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
78486f1152bSBenjamin Marzinski 		return r;
78586f1152bSBenjamin Marzinski 	}
78686f1152bSBenjamin Marzinski 
78786f1152bSBenjamin Marzinski 	td->dm_dev.bdev = bdev;
788817bf402SDan Williams 	td->dm_dev.dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
78986f1152bSBenjamin Marzinski 	return 0;
79086f1152bSBenjamin Marzinski }
79186f1152bSBenjamin Marzinski 
79286f1152bSBenjamin Marzinski /*
79386f1152bSBenjamin Marzinski  * Close a table device that we've been using.
79486f1152bSBenjamin Marzinski  */
79586f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md)
79686f1152bSBenjamin Marzinski {
79786f1152bSBenjamin Marzinski 	if (!td->dm_dev.bdev)
79886f1152bSBenjamin Marzinski 		return;
79986f1152bSBenjamin Marzinski 
80086f1152bSBenjamin Marzinski 	bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
80186f1152bSBenjamin Marzinski 	blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
802817bf402SDan Williams 	put_dax(td->dm_dev.dax_dev);
80386f1152bSBenjamin Marzinski 	td->dm_dev.bdev = NULL;
804817bf402SDan Williams 	td->dm_dev.dax_dev = NULL;
80586f1152bSBenjamin Marzinski }
80686f1152bSBenjamin Marzinski 
80786f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev,
8088454fca4SSheetal Singala 					      fmode_t mode)
8098454fca4SSheetal Singala {
81086f1152bSBenjamin Marzinski 	struct table_device *td;
81186f1152bSBenjamin Marzinski 
81286f1152bSBenjamin Marzinski 	list_for_each_entry(td, l, list)
81386f1152bSBenjamin Marzinski 		if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
81486f1152bSBenjamin Marzinski 			return td;
81586f1152bSBenjamin Marzinski 
81686f1152bSBenjamin Marzinski 	return NULL;
81786f1152bSBenjamin Marzinski }
81886f1152bSBenjamin Marzinski 
81986f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
8208454fca4SSheetal Singala 			struct dm_dev **result)
8218454fca4SSheetal Singala {
82286f1152bSBenjamin Marzinski 	int r;
82386f1152bSBenjamin Marzinski 	struct table_device *td;
82486f1152bSBenjamin Marzinski 
82586f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
82686f1152bSBenjamin Marzinski 	td = find_table_device(&md->table_devices, dev, mode);
82786f1152bSBenjamin Marzinski 	if (!td) {
828115485e8SMike Snitzer 		td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
82986f1152bSBenjamin Marzinski 		if (!td) {
83086f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
83186f1152bSBenjamin Marzinski 			return -ENOMEM;
83286f1152bSBenjamin Marzinski 		}
83386f1152bSBenjamin Marzinski 
83486f1152bSBenjamin Marzinski 		td->dm_dev.mode = mode;
83586f1152bSBenjamin Marzinski 		td->dm_dev.bdev = NULL;
83686f1152bSBenjamin Marzinski 
83786f1152bSBenjamin Marzinski 		if ((r = open_table_device(td, dev, md))) {
83886f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
83986f1152bSBenjamin Marzinski 			kfree(td);
84086f1152bSBenjamin Marzinski 			return r;
84186f1152bSBenjamin Marzinski 		}
84286f1152bSBenjamin Marzinski 
84386f1152bSBenjamin Marzinski 		format_dev_t(td->dm_dev.name, dev);
84486f1152bSBenjamin Marzinski 
845b0b4d7c6SElena Reshetova 		refcount_set(&td->count, 1);
84686f1152bSBenjamin Marzinski 		list_add(&td->list, &md->table_devices);
847b0b4d7c6SElena Reshetova 	} else {
848b0b4d7c6SElena Reshetova 		refcount_inc(&td->count);
84986f1152bSBenjamin Marzinski 	}
85086f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
85186f1152bSBenjamin Marzinski 
85286f1152bSBenjamin Marzinski 	*result = &td->dm_dev;
85386f1152bSBenjamin Marzinski 	return 0;
85486f1152bSBenjamin Marzinski }
85586f1152bSBenjamin Marzinski EXPORT_SYMBOL_GPL(dm_get_table_device);
85686f1152bSBenjamin Marzinski 
85786f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
85886f1152bSBenjamin Marzinski {
85986f1152bSBenjamin Marzinski 	struct table_device *td = container_of(d, struct table_device, dm_dev);
86086f1152bSBenjamin Marzinski 
86186f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
862b0b4d7c6SElena Reshetova 	if (refcount_dec_and_test(&td->count)) {
86386f1152bSBenjamin Marzinski 		close_table_device(td, md);
86486f1152bSBenjamin Marzinski 		list_del(&td->list);
86586f1152bSBenjamin Marzinski 		kfree(td);
86686f1152bSBenjamin Marzinski 	}
86786f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
86886f1152bSBenjamin Marzinski }
86986f1152bSBenjamin Marzinski EXPORT_SYMBOL(dm_put_table_device);
87086f1152bSBenjamin Marzinski 
87186f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices)
87286f1152bSBenjamin Marzinski {
87386f1152bSBenjamin Marzinski 	struct list_head *tmp, *next;
87486f1152bSBenjamin Marzinski 
87586f1152bSBenjamin Marzinski 	list_for_each_safe(tmp, next, devices) {
87686f1152bSBenjamin Marzinski 		struct table_device *td = list_entry(tmp, struct table_device, list);
87786f1152bSBenjamin Marzinski 
87886f1152bSBenjamin Marzinski 		DMWARN("dm_destroy: %s still exists with %d references",
879b0b4d7c6SElena Reshetova 		       td->dm_dev.name, refcount_read(&td->count));
88086f1152bSBenjamin Marzinski 		kfree(td);
88186f1152bSBenjamin Marzinski 	}
88286f1152bSBenjamin Marzinski }
88386f1152bSBenjamin Marzinski 
88486f1152bSBenjamin Marzinski /*
8853ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
8863ac51e74SDarrick J. Wong  */
8873ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
8883ac51e74SDarrick J. Wong {
8893ac51e74SDarrick J. Wong 	*geo = md->geometry;
8903ac51e74SDarrick J. Wong 
8913ac51e74SDarrick J. Wong 	return 0;
8923ac51e74SDarrick J. Wong }
8933ac51e74SDarrick J. Wong 
8943ac51e74SDarrick J. Wong /*
8953ac51e74SDarrick J. Wong  * Set the geometry of a device.
8963ac51e74SDarrick J. Wong  */
8973ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
8983ac51e74SDarrick J. Wong {
8993ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
9003ac51e74SDarrick J. Wong 
9013ac51e74SDarrick J. Wong 	if (geo->start > sz) {
9023ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
9033ac51e74SDarrick J. Wong 		return -EINVAL;
9043ac51e74SDarrick J. Wong 	}
9053ac51e74SDarrick J. Wong 
9063ac51e74SDarrick J. Wong 	md->geometry = *geo;
9073ac51e74SDarrick J. Wong 
9083ac51e74SDarrick J. Wong 	return 0;
9093ac51e74SDarrick J. Wong }
9103ac51e74SDarrick J. Wong 
9112e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
9122e93ccc1SKiyoshi Ueda {
9132e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
9142e93ccc1SKiyoshi Ueda }
9152e93ccc1SKiyoshi Ueda 
9161da177e4SLinus Torvalds /*
9171da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
9181da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
9191da177e4SLinus Torvalds  */
9204e4cbee9SChristoph Hellwig static void dec_pending(struct dm_io *io, blk_status_t error)
9211da177e4SLinus Torvalds {
9222e93ccc1SKiyoshi Ueda 	unsigned long flags;
9234e4cbee9SChristoph Hellwig 	blk_status_t io_error;
924b35f8caaSMilan Broz 	struct bio *bio;
925b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
9262e93ccc1SKiyoshi Ueda 
9272e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
928f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
929f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
930745dc570SMike Snitzer 		if (!(io->status == BLK_STS_DM_REQUEUE && __noflush_suspending(md)))
9314e4cbee9SChristoph Hellwig 			io->status = error;
932f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
933f88fb981SKiyoshi Ueda 	}
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
9364e4cbee9SChristoph Hellwig 		if (io->status == BLK_STS_DM_REQUEUE) {
9372e93ccc1SKiyoshi Ueda 			/*
9382e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
9392e93ccc1SKiyoshi Ueda 			 */
940022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
9416a8736d1STejun Heo 			if (__noflush_suspending(md))
942745dc570SMike Snitzer 				/* NOTE early return due to BLK_STS_DM_REQUEUE below */
943745dc570SMike Snitzer 				bio_list_add_head(&md->deferred, io->orig_bio);
9446a8736d1STejun Heo 			else
9452e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
9464e4cbee9SChristoph Hellwig 				io->status = BLK_STS_IOERR;
947022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
9482e93ccc1SKiyoshi Ueda 		}
9492e93ccc1SKiyoshi Ueda 
9504e4cbee9SChristoph Hellwig 		io_error = io->status;
951745dc570SMike Snitzer 		bio = io->orig_bio;
952af7e466aSMikulas Patocka 		end_io_acct(io);
953a97f925aSMikulas Patocka 		free_io(md, io);
9541da177e4SLinus Torvalds 
9554e4cbee9SChristoph Hellwig 		if (io_error == BLK_STS_DM_REQUEUE)
9566a8736d1STejun Heo 			return;
9576a8736d1STejun Heo 
9581eff9d32SJens Axboe 		if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) {
9591da177e4SLinus Torvalds 			/*
9606a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
96128a8f0d3SMike Christie 			 * without REQ_PREFLUSH.
9621da177e4SLinus Torvalds 			 */
9631eff9d32SJens Axboe 			bio->bi_opf &= ~REQ_PREFLUSH;
9646a8736d1STejun Heo 			queue_io(md, bio);
965b35f8caaSMilan Broz 		} else {
966b372d360SMike Snitzer 			/* done with normal IO or empty flush */
9678dd601faSNeilBrown 			if (io_error)
9684e4cbee9SChristoph Hellwig 				bio->bi_status = io_error;
9694246a0b6SChristoph Hellwig 			bio_endio(bio);
9702e93ccc1SKiyoshi Ueda 		}
9711da177e4SLinus Torvalds 	}
972af7e466aSMikulas Patocka }
9731da177e4SLinus Torvalds 
974bcb44433SMike Snitzer void disable_discard(struct mapped_device *md)
975bcb44433SMike Snitzer {
976bcb44433SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
977bcb44433SMike Snitzer 
978bcb44433SMike Snitzer 	/* device doesn't really support DISCARD, disable it */
979bcb44433SMike Snitzer 	limits->max_discard_sectors = 0;
980bcb44433SMike Snitzer 	blk_queue_flag_clear(QUEUE_FLAG_DISCARD, md->queue);
981bcb44433SMike Snitzer }
982bcb44433SMike Snitzer 
9834cc96131SMike Snitzer void disable_write_same(struct mapped_device *md)
9847eee4ae2SMike Snitzer {
9857eee4ae2SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
9867eee4ae2SMike Snitzer 
9877eee4ae2SMike Snitzer 	/* device doesn't really support WRITE SAME, disable it */
9887eee4ae2SMike Snitzer 	limits->max_write_same_sectors = 0;
9897eee4ae2SMike Snitzer }
9907eee4ae2SMike Snitzer 
991ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md)
992ac62d620SChristoph Hellwig {
993ac62d620SChristoph Hellwig 	struct queue_limits *limits = dm_get_queue_limits(md);
994ac62d620SChristoph Hellwig 
995ac62d620SChristoph Hellwig 	/* device doesn't really support WRITE ZEROES, disable it */
996ac62d620SChristoph Hellwig 	limits->max_write_zeroes_sectors = 0;
997ac62d620SChristoph Hellwig }
998ac62d620SChristoph Hellwig 
9994246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio)
10001da177e4SLinus Torvalds {
10014e4cbee9SChristoph Hellwig 	blk_status_t error = bio->bi_status;
1002bfc6d41cSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
1003b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
10049faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
10051da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
10061da177e4SLinus Torvalds 
1007978e51baSMike Snitzer 	if (unlikely(error == BLK_STS_TARGET) && md->type != DM_TYPE_NVME_BIO_BASED) {
1008bcb44433SMike Snitzer 		if (bio_op(bio) == REQ_OP_DISCARD &&
1009bcb44433SMike Snitzer 		    !bio->bi_disk->queue->limits.max_discard_sectors)
1010bcb44433SMike Snitzer 			disable_discard(md);
1011bcb44433SMike Snitzer 		else if (bio_op(bio) == REQ_OP_WRITE_SAME &&
101274d46992SChristoph Hellwig 			 !bio->bi_disk->queue->limits.max_write_same_sectors)
10137eee4ae2SMike Snitzer 			disable_write_same(md);
1014bcb44433SMike Snitzer 		else if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
101574d46992SChristoph Hellwig 			 !bio->bi_disk->queue->limits.max_write_zeroes_sectors)
1016ac62d620SChristoph Hellwig 			disable_write_zeroes(md);
1017ac62d620SChristoph Hellwig 	}
10187eee4ae2SMike Snitzer 
10191be56909SChristoph Hellwig 	if (endio) {
10204e4cbee9SChristoph Hellwig 		int r = endio(tio->ti, bio, &error);
10211be56909SChristoph Hellwig 		switch (r) {
10221be56909SChristoph Hellwig 		case DM_ENDIO_REQUEUE:
10234e4cbee9SChristoph Hellwig 			error = BLK_STS_DM_REQUEUE;
10241be56909SChristoph Hellwig 			/*FALLTHRU*/
10251be56909SChristoph Hellwig 		case DM_ENDIO_DONE:
10261be56909SChristoph Hellwig 			break;
10271be56909SChristoph Hellwig 		case DM_ENDIO_INCOMPLETE:
10281be56909SChristoph Hellwig 			/* The target will handle the io */
10291be56909SChristoph Hellwig 			return;
10301be56909SChristoph Hellwig 		default:
10311be56909SChristoph Hellwig 			DMWARN("unimplemented target endio return value: %d", r);
10321be56909SChristoph Hellwig 			BUG();
10331be56909SChristoph Hellwig 		}
10341be56909SChristoph Hellwig 	}
10351be56909SChristoph Hellwig 
1036cfae7529SMike Snitzer 	free_tio(tio);
1037b35f8caaSMilan Broz 	dec_pending(io, error);
10381da177e4SLinus Torvalds }
10391da177e4SLinus Torvalds 
104078d8e58aSMike Snitzer /*
104156a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
104256a67df7SMike Snitzer  * target boundary.
104356a67df7SMike Snitzer  */
104456a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
10451da177e4SLinus Torvalds {
104656a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
104756a67df7SMike Snitzer 
104856a67df7SMike Snitzer 	return ti->len - target_offset;
104956a67df7SMike Snitzer }
105056a67df7SMike Snitzer 
105156a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
105256a67df7SMike Snitzer {
105356a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
1054542f9038SMike Snitzer 	sector_t offset, max_len;
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 	/*
10571da177e4SLinus Torvalds 	 * Does the target need to split even further?
10581da177e4SLinus Torvalds 	 */
1059542f9038SMike Snitzer 	if (ti->max_io_len) {
1060542f9038SMike Snitzer 		offset = dm_target_offset(ti, sector);
1061542f9038SMike Snitzer 		if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
1062542f9038SMike Snitzer 			max_len = sector_div(offset, ti->max_io_len);
1063542f9038SMike Snitzer 		else
1064542f9038SMike Snitzer 			max_len = offset & (ti->max_io_len - 1);
1065542f9038SMike Snitzer 		max_len = ti->max_io_len - max_len;
1066542f9038SMike Snitzer 
1067542f9038SMike Snitzer 		if (len > max_len)
1068542f9038SMike Snitzer 			len = max_len;
10691da177e4SLinus Torvalds 	}
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 	return len;
10721da177e4SLinus Torvalds }
10731da177e4SLinus Torvalds 
1074542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1075542f9038SMike Snitzer {
1076542f9038SMike Snitzer 	if (len > UINT_MAX) {
1077542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1078542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
1079542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
1080542f9038SMike Snitzer 		return -EINVAL;
1081542f9038SMike Snitzer 	}
1082542f9038SMike Snitzer 
108375ae1936SMikulas Patocka 	ti->max_io_len = (uint32_t) len;
1084542f9038SMike Snitzer 
1085542f9038SMike Snitzer 	return 0;
1086542f9038SMike Snitzer }
1087542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1088542f9038SMike Snitzer 
1089f26c5719SDan Williams static struct dm_target *dm_dax_get_live_target(struct mapped_device *md,
1090f26c5719SDan Williams 						sector_t sector, int *srcu_idx)
10913d97c829SMike Snitzer 	__acquires(md->io_barrier)
1092545ed20eSToshi Kani {
1093545ed20eSToshi Kani 	struct dm_table *map;
1094545ed20eSToshi Kani 	struct dm_target *ti;
1095545ed20eSToshi Kani 
1096f26c5719SDan Williams 	map = dm_get_live_table(md, srcu_idx);
1097545ed20eSToshi Kani 	if (!map)
1098f26c5719SDan Williams 		return NULL;
1099545ed20eSToshi Kani 
1100545ed20eSToshi Kani 	ti = dm_table_find_target(map, sector);
1101123d87d5SMikulas Patocka 	if (!ti)
1102f26c5719SDan Williams 		return NULL;
1103f26c5719SDan Williams 
1104f26c5719SDan Williams 	return ti;
1105f26c5719SDan Williams }
1106f26c5719SDan Williams 
1107f26c5719SDan Williams static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
1108f26c5719SDan Williams 				 long nr_pages, void **kaddr, pfn_t *pfn)
1109f26c5719SDan Williams {
1110f26c5719SDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
1111f26c5719SDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
1112f26c5719SDan Williams 	struct dm_target *ti;
1113f26c5719SDan Williams 	long len, ret = -EIO;
1114f26c5719SDan Williams 	int srcu_idx;
1115f26c5719SDan Williams 
1116f26c5719SDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1117f26c5719SDan Williams 
1118f26c5719SDan Williams 	if (!ti)
1119545ed20eSToshi Kani 		goto out;
1120f26c5719SDan Williams 	if (!ti->type->direct_access)
1121f26c5719SDan Williams 		goto out;
1122f26c5719SDan Williams 	len = max_io_len(sector, ti) / PAGE_SECTORS;
1123f26c5719SDan Williams 	if (len < 1)
1124f26c5719SDan Williams 		goto out;
1125f26c5719SDan Williams 	nr_pages = min(len, nr_pages);
1126817bf402SDan Williams 	ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn);
1127817bf402SDan Williams 
1128545ed20eSToshi Kani  out:
1129545ed20eSToshi Kani 	dm_put_live_table(md, srcu_idx);
1130f26c5719SDan Williams 
1131f26c5719SDan Williams 	return ret;
1132545ed20eSToshi Kani }
1133545ed20eSToshi Kani 
11347bf7eac8SDan Williams static bool dm_dax_supported(struct dax_device *dax_dev, struct block_device *bdev,
11357bf7eac8SDan Williams 		int blocksize, sector_t start, sector_t len)
11367bf7eac8SDan Williams {
11377bf7eac8SDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
11387bf7eac8SDan Williams 	struct dm_table *map;
11397bf7eac8SDan Williams 	int srcu_idx;
11407bf7eac8SDan Williams 	bool ret;
11417bf7eac8SDan Williams 
11427bf7eac8SDan Williams 	map = dm_get_live_table(md, &srcu_idx);
11437bf7eac8SDan Williams 	if (!map)
11447bf7eac8SDan Williams 		return false;
11457bf7eac8SDan Williams 
11462e9ee095SPankaj Gupta 	ret = dm_table_supports_dax(map, device_supports_dax, &blocksize);
11477bf7eac8SDan Williams 
11487bf7eac8SDan Williams 	dm_put_live_table(md, srcu_idx);
11497bf7eac8SDan Williams 
11507bf7eac8SDan Williams 	return ret;
11517bf7eac8SDan Williams }
11527bf7eac8SDan Williams 
11537e026c8cSDan Williams static size_t dm_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
11547e026c8cSDan Williams 				    void *addr, size_t bytes, struct iov_iter *i)
11557e026c8cSDan Williams {
11567e026c8cSDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
11577e026c8cSDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
11587e026c8cSDan Williams 	struct dm_target *ti;
11597e026c8cSDan Williams 	long ret = 0;
11607e026c8cSDan Williams 	int srcu_idx;
11617e026c8cSDan Williams 
11627e026c8cSDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
11637e026c8cSDan Williams 
11647e026c8cSDan Williams 	if (!ti)
11657e026c8cSDan Williams 		goto out;
11667e026c8cSDan Williams 	if (!ti->type->dax_copy_from_iter) {
11677e026c8cSDan Williams 		ret = copy_from_iter(addr, bytes, i);
11687e026c8cSDan Williams 		goto out;
11697e026c8cSDan Williams 	}
11707e026c8cSDan Williams 	ret = ti->type->dax_copy_from_iter(ti, pgoff, addr, bytes, i);
11717e026c8cSDan Williams  out:
11727e026c8cSDan Williams 	dm_put_live_table(md, srcu_idx);
11737e026c8cSDan Williams 
11747e026c8cSDan Williams 	return ret;
11757e026c8cSDan Williams }
11767e026c8cSDan Williams 
1177b3a9a0c3SDan Williams static size_t dm_dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff,
1178b3a9a0c3SDan Williams 		void *addr, size_t bytes, struct iov_iter *i)
1179b3a9a0c3SDan Williams {
1180b3a9a0c3SDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
1181b3a9a0c3SDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
1182b3a9a0c3SDan Williams 	struct dm_target *ti;
1183b3a9a0c3SDan Williams 	long ret = 0;
1184b3a9a0c3SDan Williams 	int srcu_idx;
1185b3a9a0c3SDan Williams 
1186b3a9a0c3SDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1187b3a9a0c3SDan Williams 
1188b3a9a0c3SDan Williams 	if (!ti)
1189b3a9a0c3SDan Williams 		goto out;
1190b3a9a0c3SDan Williams 	if (!ti->type->dax_copy_to_iter) {
1191b3a9a0c3SDan Williams 		ret = copy_to_iter(addr, bytes, i);
1192b3a9a0c3SDan Williams 		goto out;
1193b3a9a0c3SDan Williams 	}
1194b3a9a0c3SDan Williams 	ret = ti->type->dax_copy_to_iter(ti, pgoff, addr, bytes, i);
1195b3a9a0c3SDan Williams  out:
1196b3a9a0c3SDan Williams 	dm_put_live_table(md, srcu_idx);
1197b3a9a0c3SDan Williams 
1198b3a9a0c3SDan Williams 	return ret;
1199b3a9a0c3SDan Williams }
1200b3a9a0c3SDan Williams 
12011dd40c3eSMikulas Patocka /*
12021dd40c3eSMikulas Patocka  * A target may call dm_accept_partial_bio only from the map routine.  It is
12032e2d6f7eSAjay Joshi  * allowed for all bio types except REQ_PREFLUSH, REQ_OP_ZONE_RESET,
12042e2d6f7eSAjay Joshi  * REQ_OP_ZONE_OPEN, REQ_OP_ZONE_CLOSE and REQ_OP_ZONE_FINISH.
12051dd40c3eSMikulas Patocka  *
12061dd40c3eSMikulas Patocka  * dm_accept_partial_bio informs the dm that the target only wants to process
12071dd40c3eSMikulas Patocka  * additional n_sectors sectors of the bio and the rest of the data should be
12081dd40c3eSMikulas Patocka  * sent in a next bio.
12091dd40c3eSMikulas Patocka  *
12101dd40c3eSMikulas Patocka  * A diagram that explains the arithmetics:
12111dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
12121dd40c3eSMikulas Patocka  * |         1          |       2       |   3   |
12131dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
12141dd40c3eSMikulas Patocka  *
12151dd40c3eSMikulas Patocka  * <-------------- *tio->len_ptr --------------->
12161dd40c3eSMikulas Patocka  *                      <------- bi_size ------->
12171dd40c3eSMikulas Patocka  *                      <-- n_sectors -->
12181dd40c3eSMikulas Patocka  *
12191dd40c3eSMikulas Patocka  * Region 1 was already iterated over with bio_advance or similar function.
12201dd40c3eSMikulas Patocka  *	(it may be empty if the target doesn't use bio_advance)
12211dd40c3eSMikulas Patocka  * Region 2 is the remaining bio size that the target wants to process.
12221dd40c3eSMikulas Patocka  *	(it may be empty if region 1 is non-empty, although there is no reason
12231dd40c3eSMikulas Patocka  *	 to make it empty)
12241dd40c3eSMikulas Patocka  * The target requires that region 3 is to be sent in the next bio.
12251dd40c3eSMikulas Patocka  *
12261dd40c3eSMikulas Patocka  * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
12271dd40c3eSMikulas Patocka  * the partially processed part (the sum of regions 1+2) must be the same for all
12281dd40c3eSMikulas Patocka  * copies of the bio.
12291dd40c3eSMikulas Patocka  */
12301dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
12311dd40c3eSMikulas Patocka {
12321dd40c3eSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
12331dd40c3eSMikulas Patocka 	unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
12341eff9d32SJens Axboe 	BUG_ON(bio->bi_opf & REQ_PREFLUSH);
12351dd40c3eSMikulas Patocka 	BUG_ON(bi_size > *tio->len_ptr);
12361dd40c3eSMikulas Patocka 	BUG_ON(n_sectors > bi_size);
12371dd40c3eSMikulas Patocka 	*tio->len_ptr -= bi_size - n_sectors;
12381dd40c3eSMikulas Patocka 	bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
12391dd40c3eSMikulas Patocka }
12401dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
12411dd40c3eSMikulas Patocka 
1242978e51baSMike Snitzer static blk_qc_t __map_bio(struct dm_target_io *tio)
12431da177e4SLinus Torvalds {
12441da177e4SLinus Torvalds 	int r;
12452056a782SJens Axboe 	sector_t sector;
1246dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
124764f52b0eSMike Snitzer 	struct dm_io *io = tio->io;
1248978e51baSMike Snitzer 	struct mapped_device *md = io->md;
1249bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
1250978e51baSMike Snitzer 	blk_qc_t ret = BLK_QC_T_NONE;
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 	/*
12551da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
12561da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
12571da177e4SLinus Torvalds 	 * this io.
12581da177e4SLinus Torvalds 	 */
125964f52b0eSMike Snitzer 	atomic_inc(&io->io_count);
12604f024f37SKent Overstreet 	sector = clone->bi_iter.bi_sector;
1261d67a5f4bSMikulas Patocka 
12627de3ee57SMikulas Patocka 	r = ti->type->map(ti, clone);
1263846785e6SChristoph Hellwig 	switch (r) {
1264846785e6SChristoph Hellwig 	case DM_MAPIO_SUBMITTED:
1265846785e6SChristoph Hellwig 		break;
1266846785e6SChristoph Hellwig 	case DM_MAPIO_REMAPPED:
12671da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
126874d46992SChristoph Hellwig 		trace_block_bio_remap(clone->bi_disk->queue, clone,
126964f52b0eSMike Snitzer 				      bio_dev(io->orig_bio), sector);
1270978e51baSMike Snitzer 		if (md->type == DM_TYPE_NVME_BIO_BASED)
1271978e51baSMike Snitzer 			ret = direct_make_request(clone);
1272978e51baSMike Snitzer 		else
1273978e51baSMike Snitzer 			ret = generic_make_request(clone);
1274846785e6SChristoph Hellwig 		break;
1275846785e6SChristoph Hellwig 	case DM_MAPIO_KILL:
12764e4cbee9SChristoph Hellwig 		free_tio(tio);
127764f52b0eSMike Snitzer 		dec_pending(io, BLK_STS_IOERR);
12784e4cbee9SChristoph Hellwig 		break;
1279846785e6SChristoph Hellwig 	case DM_MAPIO_REQUEUE:
1280cfae7529SMike Snitzer 		free_tio(tio);
128164f52b0eSMike Snitzer 		dec_pending(io, BLK_STS_DM_REQUEUE);
1282846785e6SChristoph Hellwig 		break;
1283846785e6SChristoph Hellwig 	default:
128445cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
128545cbcd79SKiyoshi Ueda 		BUG();
12861da177e4SLinus Torvalds 	}
12871da177e4SLinus Torvalds 
1288978e51baSMike Snitzer 	return ret;
12891da177e4SLinus Torvalds }
12901da177e4SLinus Torvalds 
1291e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
1292bd2a49b8SAlasdair G Kergon {
12934f024f37SKent Overstreet 	bio->bi_iter.bi_sector = sector;
12944f024f37SKent Overstreet 	bio->bi_iter.bi_size = to_bytes(len);
12951da177e4SLinus Torvalds }
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds /*
12981da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
12991da177e4SLinus Torvalds  */
1300c80914e8SMike Snitzer static int clone_bio(struct dm_target_io *tio, struct bio *bio,
13011c3b13e6SKent Overstreet 		     sector_t sector, unsigned len)
13021da177e4SLinus Torvalds {
1303dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
13041da177e4SLinus Torvalds 
13051c3b13e6SKent Overstreet 	__bio_clone_fast(clone, bio);
13069c47008dSMartin K. Petersen 
130757c36519SMike Snitzer 	if (bio_integrity(bio)) {
1308e2460f2aSMikulas Patocka 		int r;
1309e2460f2aSMikulas Patocka 
1310e2460f2aSMikulas Patocka 		if (unlikely(!dm_target_has_integrity(tio->ti->type) &&
1311e2460f2aSMikulas Patocka 			     !dm_target_passes_integrity(tio->ti->type))) {
1312e2460f2aSMikulas Patocka 			DMWARN("%s: the target %s doesn't support integrity data.",
1313e2460f2aSMikulas Patocka 				dm_device_name(tio->io->md),
1314e2460f2aSMikulas Patocka 				tio->ti->type->name);
1315e2460f2aSMikulas Patocka 			return -EIO;
1316e2460f2aSMikulas Patocka 		}
1317e2460f2aSMikulas Patocka 
1318e2460f2aSMikulas Patocka 		r = bio_integrity_clone(clone, bio, GFP_NOIO);
1319c80914e8SMike Snitzer 		if (r < 0)
1320c80914e8SMike Snitzer 			return r;
1321c80914e8SMike Snitzer 	}
13221c3b13e6SKent Overstreet 
1323fa8db494SMike Snitzer 	bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1324fa8db494SMike Snitzer 	clone->bi_iter.bi_size = to_bytes(len);
1325fa8db494SMike Snitzer 
1326fa8db494SMike Snitzer 	if (bio_integrity(bio))
1327fa8db494SMike Snitzer 		bio_integrity_trim(clone);
1328c80914e8SMike Snitzer 
1329c80914e8SMike Snitzer 	return 0;
13301da177e4SLinus Torvalds }
13311da177e4SLinus Torvalds 
1332318716ddSMike Snitzer static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci,
1333318716ddSMike Snitzer 				struct dm_target *ti, unsigned num_bios)
1334f9ab94ceSMikulas Patocka {
1335dba14160SMikulas Patocka 	struct dm_target_io *tio;
1336318716ddSMike Snitzer 	int try;
1337dba14160SMikulas Patocka 
1338318716ddSMike Snitzer 	if (!num_bios)
1339318716ddSMike Snitzer 		return;
1340f9ab94ceSMikulas Patocka 
1341318716ddSMike Snitzer 	if (num_bios == 1) {
1342318716ddSMike Snitzer 		tio = alloc_tio(ci, ti, 0, GFP_NOIO);
1343318716ddSMike Snitzer 		bio_list_add(blist, &tio->clone);
1344318716ddSMike Snitzer 		return;
13459015df24SAlasdair G Kergon 	}
13469015df24SAlasdair G Kergon 
1347318716ddSMike Snitzer 	for (try = 0; try < 2; try++) {
1348318716ddSMike Snitzer 		int bio_nr;
1349318716ddSMike Snitzer 		struct bio *bio;
1350318716ddSMike Snitzer 
1351318716ddSMike Snitzer 		if (try)
1352bc02cdbeSMike Snitzer 			mutex_lock(&ci->io->md->table_devices_lock);
1353318716ddSMike Snitzer 		for (bio_nr = 0; bio_nr < num_bios; bio_nr++) {
1354318716ddSMike Snitzer 			tio = alloc_tio(ci, ti, bio_nr, try ? GFP_NOIO : GFP_NOWAIT);
1355318716ddSMike Snitzer 			if (!tio)
1356318716ddSMike Snitzer 				break;
1357318716ddSMike Snitzer 
1358318716ddSMike Snitzer 			bio_list_add(blist, &tio->clone);
1359318716ddSMike Snitzer 		}
1360318716ddSMike Snitzer 		if (try)
1361bc02cdbeSMike Snitzer 			mutex_unlock(&ci->io->md->table_devices_lock);
1362318716ddSMike Snitzer 		if (bio_nr == num_bios)
1363318716ddSMike Snitzer 			return;
1364318716ddSMike Snitzer 
1365318716ddSMike Snitzer 		while ((bio = bio_list_pop(blist))) {
1366318716ddSMike Snitzer 			tio = container_of(bio, struct dm_target_io, clone);
1367318716ddSMike Snitzer 			free_tio(tio);
1368318716ddSMike Snitzer 		}
1369318716ddSMike Snitzer 	}
1370318716ddSMike Snitzer }
1371318716ddSMike Snitzer 
1372978e51baSMike Snitzer static blk_qc_t __clone_and_map_simple_bio(struct clone_info *ci,
1373318716ddSMike Snitzer 					   struct dm_target_io *tio, unsigned *len)
13749015df24SAlasdair G Kergon {
1375dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
13769015df24SAlasdair G Kergon 
13771dd40c3eSMikulas Patocka 	tio->len_ptr = len;
13781dd40c3eSMikulas Patocka 
13791c3b13e6SKent Overstreet 	__bio_clone_fast(clone, ci->bio);
1380bd2a49b8SAlasdair G Kergon 	if (len)
13811dd40c3eSMikulas Patocka 		bio_setup_sector(clone, ci->sector, *len);
1382f9ab94ceSMikulas Patocka 
1383978e51baSMike Snitzer 	return __map_bio(tio);
1384f9ab94ceSMikulas Patocka }
1385f9ab94ceSMikulas Patocka 
138614fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
13871dd40c3eSMikulas Patocka 				  unsigned num_bios, unsigned *len)
138806a426ceSMike Snitzer {
1389318716ddSMike Snitzer 	struct bio_list blist = BIO_EMPTY_LIST;
1390318716ddSMike Snitzer 	struct bio *bio;
1391318716ddSMike Snitzer 	struct dm_target_io *tio;
139206a426ceSMike Snitzer 
1393318716ddSMike Snitzer 	alloc_multiple_bios(&blist, ci, ti, num_bios);
1394318716ddSMike Snitzer 
1395318716ddSMike Snitzer 	while ((bio = bio_list_pop(&blist))) {
1396318716ddSMike Snitzer 		tio = container_of(bio, struct dm_target_io, clone);
1397978e51baSMike Snitzer 		(void) __clone_and_map_simple_bio(ci, tio, len);
1398318716ddSMike Snitzer 	}
139906a426ceSMike Snitzer }
140006a426ceSMike Snitzer 
140114fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1402f9ab94ceSMikulas Patocka {
140306a426ceSMike Snitzer 	unsigned target_nr = 0;
1404f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1405f9ab94ceSMikulas Patocka 
1406892ad71fSDennis Zhou 	/*
1407dbe3ece1SJens Axboe 	 * Empty flush uses a statically initialized bio, as the base for
1408dbe3ece1SJens Axboe 	 * cloning.  However, blkg association requires that a bdev is
1409dbe3ece1SJens Axboe 	 * associated with a gendisk, which doesn't happen until the bdev is
1410dbe3ece1SJens Axboe 	 * opened.  So, blkg association is done at issue time of the flush
1411dbe3ece1SJens Axboe 	 * rather than when the device is created in alloc_dev().
1412892ad71fSDennis Zhou 	 */
1413892ad71fSDennis Zhou 	bio_set_dev(ci->bio, ci->io->md->bdev);
1414892ad71fSDennis Zhou 
1415b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1416f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
14171dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
1418f9ab94ceSMikulas Patocka 
1419892ad71fSDennis Zhou 	bio_disassociate_blkg(ci->bio);
1420892ad71fSDennis Zhou 
1421f9ab94ceSMikulas Patocka 	return 0;
1422f9ab94ceSMikulas Patocka }
1423f9ab94ceSMikulas Patocka 
1424c80914e8SMike Snitzer static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
14251dd40c3eSMikulas Patocka 				    sector_t sector, unsigned *len)
14265ae89a87SMike Snitzer {
1427dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
14285ae89a87SMike Snitzer 	struct dm_target_io *tio;
1429f31c21e4SNeilBrown 	int r;
14305ae89a87SMike Snitzer 
1431318716ddSMike Snitzer 	tio = alloc_tio(ci, ti, 0, GFP_NOIO);
14321dd40c3eSMikulas Patocka 	tio->len_ptr = len;
1433c80914e8SMike Snitzer 	r = clone_bio(tio, bio, sector, *len);
1434072623deSMikulas Patocka 	if (r < 0) {
1435cfae7529SMike Snitzer 		free_tio(tio);
1436c80914e8SMike Snitzer 		return r;
1437b0d8ed4dSAlasdair G Kergon 	}
1438978e51baSMike Snitzer 	(void) __map_bio(tio);
143955a62eefSAlasdair G Kergon 
1440f31c21e4SNeilBrown 	return 0;
144123508a96SMike Snitzer }
144255a62eefSAlasdair G Kergon 
144323508a96SMike Snitzer typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
144455a62eefSAlasdair G Kergon 
144523508a96SMike Snitzer static unsigned get_num_discard_bios(struct dm_target *ti)
144623508a96SMike Snitzer {
144723508a96SMike Snitzer 	return ti->num_discard_bios;
144823508a96SMike Snitzer }
144923508a96SMike Snitzer 
145000716545SDenis Semakin static unsigned get_num_secure_erase_bios(struct dm_target *ti)
145100716545SDenis Semakin {
145200716545SDenis Semakin 	return ti->num_secure_erase_bios;
145300716545SDenis Semakin }
145400716545SDenis Semakin 
145523508a96SMike Snitzer static unsigned get_num_write_same_bios(struct dm_target *ti)
145623508a96SMike Snitzer {
145723508a96SMike Snitzer 	return ti->num_write_same_bios;
145823508a96SMike Snitzer }
145923508a96SMike Snitzer 
1460ac62d620SChristoph Hellwig static unsigned get_num_write_zeroes_bios(struct dm_target *ti)
1461ac62d620SChristoph Hellwig {
1462ac62d620SChristoph Hellwig 	return ti->num_write_zeroes_bios;
1463ac62d620SChristoph Hellwig }
1464ac62d620SChristoph Hellwig 
14653d7f4562SMike Snitzer static int __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti,
146661697a6aSMike Snitzer 				       unsigned num_bios)
14675ae89a87SMike Snitzer {
146851b86f9aSMichael Lass 	unsigned len;
14695ae89a87SMike Snitzer 
14705ae89a87SMike Snitzer 	/*
147123508a96SMike Snitzer 	 * Even though the device advertised support for this type of
147223508a96SMike Snitzer 	 * request, that does not mean every target supports it, and
1473936688d7SMike Snitzer 	 * reconfiguration might also have changed that since the
14745ae89a87SMike Snitzer 	 * check was performed.
14755ae89a87SMike Snitzer 	 */
147655a62eefSAlasdair G Kergon 	if (!num_bios)
14775ae89a87SMike Snitzer 		return -EOPNOTSUPP;
14785ae89a87SMike Snitzer 
147951b86f9aSMichael Lass 	len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
148051b86f9aSMichael Lass 
14811dd40c3eSMikulas Patocka 	__send_duplicate_bios(ci, ti, num_bios, &len);
14825ae89a87SMike Snitzer 
1483a79245b3SMike Snitzer 	ci->sector += len;
14843d7f4562SMike Snitzer 	ci->sector_count -= len;
14855ae89a87SMike Snitzer 
14865ae89a87SMike Snitzer 	return 0;
14875ae89a87SMike Snitzer }
14885ae89a87SMike Snitzer 
14893d7f4562SMike Snitzer static int __send_discard(struct clone_info *ci, struct dm_target *ti)
149023508a96SMike Snitzer {
149161697a6aSMike Snitzer 	return __send_changing_extent_only(ci, ti, get_num_discard_bios(ti));
149223508a96SMike Snitzer }
149323508a96SMike Snitzer 
149400716545SDenis Semakin static int __send_secure_erase(struct clone_info *ci, struct dm_target *ti)
149500716545SDenis Semakin {
149661697a6aSMike Snitzer 	return __send_changing_extent_only(ci, ti, get_num_secure_erase_bios(ti));
149700716545SDenis Semakin }
149800716545SDenis Semakin 
14993d7f4562SMike Snitzer static int __send_write_same(struct clone_info *ci, struct dm_target *ti)
150023508a96SMike Snitzer {
150161697a6aSMike Snitzer 	return __send_changing_extent_only(ci, ti, get_num_write_same_bios(ti));
150223508a96SMike Snitzer }
150323508a96SMike Snitzer 
15043d7f4562SMike Snitzer static int __send_write_zeroes(struct clone_info *ci, struct dm_target *ti)
1505ac62d620SChristoph Hellwig {
150661697a6aSMike Snitzer 	return __send_changing_extent_only(ci, ti, get_num_write_zeroes_bios(ti));
1507ac62d620SChristoph Hellwig }
1508ac62d620SChristoph Hellwig 
1509568c73a3SMike Snitzer static bool is_abnormal_io(struct bio *bio)
1510568c73a3SMike Snitzer {
1511568c73a3SMike Snitzer 	bool r = false;
1512568c73a3SMike Snitzer 
1513568c73a3SMike Snitzer 	switch (bio_op(bio)) {
1514568c73a3SMike Snitzer 	case REQ_OP_DISCARD:
1515568c73a3SMike Snitzer 	case REQ_OP_SECURE_ERASE:
1516568c73a3SMike Snitzer 	case REQ_OP_WRITE_SAME:
1517568c73a3SMike Snitzer 	case REQ_OP_WRITE_ZEROES:
1518568c73a3SMike Snitzer 		r = true;
1519568c73a3SMike Snitzer 		break;
1520568c73a3SMike Snitzer 	}
1521568c73a3SMike Snitzer 
1522568c73a3SMike Snitzer 	return r;
1523568c73a3SMike Snitzer }
1524568c73a3SMike Snitzer 
15250519c71eSMike Snitzer static bool __process_abnormal_io(struct clone_info *ci, struct dm_target *ti,
15260519c71eSMike Snitzer 				  int *result)
15270519c71eSMike Snitzer {
15280519c71eSMike Snitzer 	struct bio *bio = ci->bio;
15290519c71eSMike Snitzer 
15300519c71eSMike Snitzer 	if (bio_op(bio) == REQ_OP_DISCARD)
15310519c71eSMike Snitzer 		*result = __send_discard(ci, ti);
153200716545SDenis Semakin 	else if (bio_op(bio) == REQ_OP_SECURE_ERASE)
153300716545SDenis Semakin 		*result = __send_secure_erase(ci, ti);
15340519c71eSMike Snitzer 	else if (bio_op(bio) == REQ_OP_WRITE_SAME)
15350519c71eSMike Snitzer 		*result = __send_write_same(ci, ti);
15360519c71eSMike Snitzer 	else if (bio_op(bio) == REQ_OP_WRITE_ZEROES)
15370519c71eSMike Snitzer 		*result = __send_write_zeroes(ci, ti);
15380519c71eSMike Snitzer 	else
15390519c71eSMike Snitzer 		return false;
15400519c71eSMike Snitzer 
15410519c71eSMike Snitzer 	return true;
15420519c71eSMike Snitzer }
15430519c71eSMike Snitzer 
1544e4c93811SAlasdair G Kergon /*
1545e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1546e4c93811SAlasdair G Kergon  */
1547e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci)
1548e4c93811SAlasdair G Kergon {
1549e4c93811SAlasdair G Kergon 	struct dm_target *ti;
15501c3b13e6SKent Overstreet 	unsigned len;
1551c80914e8SMike Snitzer 	int r;
1552e4c93811SAlasdair G Kergon 
1553e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1554123d87d5SMikulas Patocka 	if (!ti)
1555e4c93811SAlasdair G Kergon 		return -EIO;
1556e4c93811SAlasdair G Kergon 
1557568c73a3SMike Snitzer 	if (__process_abnormal_io(ci, ti, &r))
15580519c71eSMike Snitzer 		return r;
15593d7f4562SMike Snitzer 
1560e76239a3SChristoph Hellwig 	len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
1561e4c93811SAlasdair G Kergon 
1562c80914e8SMike Snitzer 	r = __clone_and_map_data_bio(ci, ti, ci->sector, &len);
1563c80914e8SMike Snitzer 	if (r < 0)
1564c80914e8SMike Snitzer 		return r;
1565e4c93811SAlasdair G Kergon 
1566e4c93811SAlasdair G Kergon 	ci->sector += len;
1567e4c93811SAlasdair G Kergon 	ci->sector_count -= len;
1568e4c93811SAlasdair G Kergon 
1569e4c93811SAlasdair G Kergon 	return 0;
1570e4c93811SAlasdair G Kergon }
1571e4c93811SAlasdair G Kergon 
1572978e51baSMike Snitzer static void init_clone_info(struct clone_info *ci, struct mapped_device *md,
1573978e51baSMike Snitzer 			    struct dm_table *map, struct bio *bio)
1574978e51baSMike Snitzer {
1575978e51baSMike Snitzer 	ci->map = map;
1576978e51baSMike Snitzer 	ci->io = alloc_io(md, bio);
1577978e51baSMike Snitzer 	ci->sector = bio->bi_iter.bi_sector;
1578978e51baSMike Snitzer }
1579978e51baSMike Snitzer 
1580a1e1cb72SMike Snitzer #define __dm_part_stat_sub(part, field, subnd)	\
1581a1e1cb72SMike Snitzer 	(part_stat_get(part, field) -= (subnd))
1582a1e1cb72SMike Snitzer 
1583e4c93811SAlasdair G Kergon /*
158414fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
15851da177e4SLinus Torvalds  */
1586978e51baSMike Snitzer static blk_qc_t __split_and_process_bio(struct mapped_device *md,
158783d5e5b0SMikulas Patocka 					struct dm_table *map, struct bio *bio)
15881da177e4SLinus Torvalds {
15891da177e4SLinus Torvalds 	struct clone_info ci;
1590978e51baSMike Snitzer 	blk_qc_t ret = BLK_QC_T_NONE;
1591512875bdSJun'ichi Nomura 	int error = 0;
15921da177e4SLinus Torvalds 
1593978e51baSMike Snitzer 	init_clone_info(&ci, md, map, bio);
1594bd2a49b8SAlasdair G Kergon 
15951eff9d32SJens Axboe 	if (bio->bi_opf & REQ_PREFLUSH) {
1596dbe3ece1SJens Axboe 		struct bio flush_bio;
1597dbe3ece1SJens Axboe 
1598dbe3ece1SJens Axboe 		/*
1599dbe3ece1SJens Axboe 		 * Use an on-stack bio for this, it's safe since we don't
1600dbe3ece1SJens Axboe 		 * need to reference it after submit. It's just used as
1601dbe3ece1SJens Axboe 		 * the basis for the clone(s).
1602dbe3ece1SJens Axboe 		 */
1603dbe3ece1SJens Axboe 		bio_init(&flush_bio, NULL, 0);
1604dbe3ece1SJens Axboe 		flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
1605dbe3ece1SJens Axboe 		ci.bio = &flush_bio;
1606b372d360SMike Snitzer 		ci.sector_count = 0;
160714fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1608b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
16092e2d6f7eSAjay Joshi 	} else if (op_is_zone_mgmt(bio_op(bio))) {
1610a4aa5e56SDamien Le Moal 		ci.bio = bio;
1611a4aa5e56SDamien Le Moal 		ci.sector_count = 0;
1612a4aa5e56SDamien Le Moal 		error = __split_and_process_non_flush(&ci);
1613b372d360SMike Snitzer 	} else {
16146a8736d1STejun Heo 		ci.bio = bio;
16151da177e4SLinus Torvalds 		ci.sector_count = bio_sectors(bio);
161618a25da8SNeilBrown 		while (ci.sector_count && !error) {
161714fe594dSAlasdair G Kergon 			error = __split_and_process_non_flush(&ci);
161818a25da8SNeilBrown 			if (current->bio_list && ci.sector_count && !error) {
161918a25da8SNeilBrown 				/*
162018a25da8SNeilBrown 				 * Remainder must be passed to generic_make_request()
162118a25da8SNeilBrown 				 * so that it gets handled *after* bios already submitted
162218a25da8SNeilBrown 				 * have been completely processed.
162318a25da8SNeilBrown 				 * We take a clone of the original to store in
1624745dc570SMike Snitzer 				 * ci.io->orig_bio to be used by end_io_acct() and
162518a25da8SNeilBrown 				 * for dec_pending to use for completion handling.
162618a25da8SNeilBrown 				 */
1627f21c601aSMike Snitzer 				struct bio *b = bio_split(bio, bio_sectors(bio) - ci.sector_count,
1628f21c601aSMike Snitzer 							  GFP_NOIO, &md->queue->bio_split);
1629745dc570SMike Snitzer 				ci.io->orig_bio = b;
1630a1e1cb72SMike Snitzer 
1631a1e1cb72SMike Snitzer 				/*
1632a1e1cb72SMike Snitzer 				 * Adjust IO stats for each split, otherwise upon queue
1633a1e1cb72SMike Snitzer 				 * reentry there will be redundant IO accounting.
1634a1e1cb72SMike Snitzer 				 * NOTE: this is a stop-gap fix, a proper fix involves
1635a1e1cb72SMike Snitzer 				 * significant refactoring of DM core's bio splitting
1636a1e1cb72SMike Snitzer 				 * (by eliminating DM's splitting and just using bio_split)
1637a1e1cb72SMike Snitzer 				 */
1638a1e1cb72SMike Snitzer 				part_stat_lock();
1639a1e1cb72SMike Snitzer 				__dm_part_stat_sub(&dm_disk(md)->part0,
1640a1e1cb72SMike Snitzer 						   sectors[op_stat_group(bio_op(bio))], ci.sector_count);
1641a1e1cb72SMike Snitzer 				part_stat_unlock();
1642a1e1cb72SMike Snitzer 
164318a25da8SNeilBrown 				bio_chain(b, bio);
1644075c18c3SMike Snitzer 				trace_block_split(md->queue, b, bio->bi_iter.bi_sector);
1645978e51baSMike Snitzer 				ret = generic_make_request(bio);
164618a25da8SNeilBrown 				break;
164718a25da8SNeilBrown 			}
164818a25da8SNeilBrown 		}
1649d87f4c14STejun Heo 	}
16501da177e4SLinus Torvalds 
16511da177e4SLinus Torvalds 	/* drop the extra reference count */
165254385bf7SBart Van Assche 	dec_pending(ci.io, errno_to_blk_status(error));
1653978e51baSMike Snitzer 	return ret;
16541da177e4SLinus Torvalds }
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds /*
1657978e51baSMike Snitzer  * Optimized variant of __split_and_process_bio that leverages the
1658978e51baSMike Snitzer  * fact that targets that use it do _not_ have a need to split bios.
16591da177e4SLinus Torvalds  */
1660568c73a3SMike Snitzer static blk_qc_t __process_bio(struct mapped_device *md, struct dm_table *map,
1661568c73a3SMike Snitzer 			      struct bio *bio, struct dm_target *ti)
16621da177e4SLinus Torvalds {
1663978e51baSMike Snitzer 	struct clone_info ci;
1664978e51baSMike Snitzer 	blk_qc_t ret = BLK_QC_T_NONE;
1665978e51baSMike Snitzer 	int error = 0;
1666978e51baSMike Snitzer 
1667978e51baSMike Snitzer 	init_clone_info(&ci, md, map, bio);
1668978e51baSMike Snitzer 
1669978e51baSMike Snitzer 	if (bio->bi_opf & REQ_PREFLUSH) {
1670dbe3ece1SJens Axboe 		struct bio flush_bio;
1671dbe3ece1SJens Axboe 
1672dbe3ece1SJens Axboe 		/*
1673dbe3ece1SJens Axboe 		 * Use an on-stack bio for this, it's safe since we don't
1674dbe3ece1SJens Axboe 		 * need to reference it after submit. It's just used as
1675dbe3ece1SJens Axboe 		 * the basis for the clone(s).
1676dbe3ece1SJens Axboe 		 */
1677dbe3ece1SJens Axboe 		bio_init(&flush_bio, NULL, 0);
1678dbe3ece1SJens Axboe 		flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
1679dbe3ece1SJens Axboe 		ci.bio = &flush_bio;
1680978e51baSMike Snitzer 		ci.sector_count = 0;
1681978e51baSMike Snitzer 		error = __send_empty_flush(&ci);
1682978e51baSMike Snitzer 		/* dec_pending submits any data associated with flush */
1683978e51baSMike Snitzer 	} else {
1684978e51baSMike Snitzer 		struct dm_target_io *tio;
1685978e51baSMike Snitzer 
1686978e51baSMike Snitzer 		ci.bio = bio;
1687978e51baSMike Snitzer 		ci.sector_count = bio_sectors(bio);
1688568c73a3SMike Snitzer 		if (__process_abnormal_io(&ci, ti, &error))
16890519c71eSMike Snitzer 			goto out;
16900519c71eSMike Snitzer 
16910519c71eSMike Snitzer 		tio = alloc_tio(&ci, ti, 0, GFP_NOIO);
1692978e51baSMike Snitzer 		ret = __clone_and_map_simple_bio(&ci, tio, NULL);
1693978e51baSMike Snitzer 	}
1694978e51baSMike Snitzer out:
1695978e51baSMike Snitzer 	/* drop the extra reference count */
1696978e51baSMike Snitzer 	dec_pending(ci.io, errno_to_blk_status(error));
1697978e51baSMike Snitzer 	return ret;
1698978e51baSMike Snitzer }
1699978e51baSMike Snitzer 
1700568c73a3SMike Snitzer static void dm_queue_split(struct mapped_device *md, struct dm_target *ti, struct bio **bio)
1701568c73a3SMike Snitzer {
1702568c73a3SMike Snitzer 	unsigned len, sector_count;
1703568c73a3SMike Snitzer 
1704568c73a3SMike Snitzer 	sector_count = bio_sectors(*bio);
1705568c73a3SMike Snitzer 	len = min_t(sector_t, max_io_len((*bio)->bi_iter.bi_sector, ti), sector_count);
1706568c73a3SMike Snitzer 
1707568c73a3SMike Snitzer 	if (sector_count > len) {
1708568c73a3SMike Snitzer 		struct bio *split = bio_split(*bio, len, GFP_NOIO, &md->queue->bio_split);
1709568c73a3SMike Snitzer 
1710568c73a3SMike Snitzer 		bio_chain(split, *bio);
1711568c73a3SMike Snitzer 		trace_block_split(md->queue, split, (*bio)->bi_iter.bi_sector);
1712568c73a3SMike Snitzer 		generic_make_request(*bio);
1713568c73a3SMike Snitzer 		*bio = split;
1714568c73a3SMike Snitzer 	}
1715568c73a3SMike Snitzer }
1716568c73a3SMike Snitzer 
17176548c7c5SMike Snitzer static blk_qc_t dm_process_bio(struct mapped_device *md,
17186548c7c5SMike Snitzer 			       struct dm_table *map, struct bio *bio)
17196548c7c5SMike Snitzer {
1720568c73a3SMike Snitzer 	blk_qc_t ret = BLK_QC_T_NONE;
1721568c73a3SMike Snitzer 	struct dm_target *ti = md->immutable_target;
1722568c73a3SMike Snitzer 
1723568c73a3SMike Snitzer 	if (unlikely(!map)) {
1724568c73a3SMike Snitzer 		bio_io_error(bio);
1725568c73a3SMike Snitzer 		return ret;
1726568c73a3SMike Snitzer 	}
1727568c73a3SMike Snitzer 
1728568c73a3SMike Snitzer 	if (!ti) {
1729568c73a3SMike Snitzer 		ti = dm_table_find_target(map, bio->bi_iter.bi_sector);
1730123d87d5SMikulas Patocka 		if (unlikely(!ti)) {
1731568c73a3SMike Snitzer 			bio_io_error(bio);
1732568c73a3SMike Snitzer 			return ret;
1733568c73a3SMike Snitzer 		}
1734568c73a3SMike Snitzer 	}
1735568c73a3SMike Snitzer 
1736568c73a3SMike Snitzer 	/*
1737568c73a3SMike Snitzer 	 * If in ->make_request_fn we need to use blk_queue_split(), otherwise
1738568c73a3SMike Snitzer 	 * queue_limits for abnormal requests (e.g. discard, writesame, etc)
1739568c73a3SMike Snitzer 	 * won't be imposed.
1740568c73a3SMike Snitzer 	 */
1741568c73a3SMike Snitzer 	if (current->bio_list) {
1742568c73a3SMike Snitzer 		blk_queue_split(md->queue, &bio);
1743effd58c9SMike Snitzer 		if (!is_abnormal_io(bio))
1744568c73a3SMike Snitzer 			dm_queue_split(md, ti, &bio);
1745568c73a3SMike Snitzer 	}
1746568c73a3SMike Snitzer 
17476548c7c5SMike Snitzer 	if (dm_get_md_type(md) == DM_TYPE_NVME_BIO_BASED)
1748568c73a3SMike Snitzer 		return __process_bio(md, map, bio, ti);
17496548c7c5SMike Snitzer 	else
17506548c7c5SMike Snitzer 		return __split_and_process_bio(md, map, bio);
17516548c7c5SMike Snitzer }
17526548c7c5SMike Snitzer 
175324113d48SMikulas Patocka static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
17541da177e4SLinus Torvalds {
17551da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
1756978e51baSMike Snitzer 	blk_qc_t ret = BLK_QC_T_NONE;
175783d5e5b0SMikulas Patocka 	int srcu_idx;
175883d5e5b0SMikulas Patocka 	struct dm_table *map;
17591da177e4SLinus Torvalds 
176083d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
17611da177e4SLinus Torvalds 
17626a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
17636a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
176483d5e5b0SMikulas Patocka 		dm_put_live_table(md, srcu_idx);
17651da177e4SLinus Torvalds 
17661eff9d32SJens Axboe 		if (!(bio->bi_opf & REQ_RAHEAD))
176792c63902SMikulas Patocka 			queue_io(md, bio);
17686a8736d1STejun Heo 		else
17696a8736d1STejun Heo 			bio_io_error(bio);
1770978e51baSMike Snitzer 		return ret;
17711da177e4SLinus Torvalds 	}
17721da177e4SLinus Torvalds 
17736548c7c5SMike Snitzer 	ret = dm_process_bio(md, map, bio);
1774978e51baSMike Snitzer 
177583d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
1776978e51baSMike Snitzer 	return ret;
1777978e51baSMike Snitzer }
1778978e51baSMike Snitzer 
17791da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
17801da177e4SLinus Torvalds {
17818a57dfc6SChandra Seetharaman 	int r = bdi_bits;
17828a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
17838a57dfc6SChandra Seetharaman 	struct dm_table *map;
17841da177e4SLinus Torvalds 
17851eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
1786e522c039SMike Snitzer 		if (dm_request_based(md)) {
1787cec47e3dSKiyoshi Ueda 			/*
1788e522c039SMike Snitzer 			 * With request-based DM we only need to check the
1789e522c039SMike Snitzer 			 * top-level queue for congestion.
1790cec47e3dSKiyoshi Ueda 			 */
1791dc3b17ccSJan Kara 			r = md->queue->backing_dev_info->wb.state & bdi_bits;
1792e522c039SMike Snitzer 		} else {
1793e522c039SMike Snitzer 			map = dm_get_live_table_fast(md);
1794e522c039SMike Snitzer 			if (map)
17951da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
179683d5e5b0SMikulas Patocka 			dm_put_live_table_fast(md);
17978a57dfc6SChandra Seetharaman 		}
1798e522c039SMike Snitzer 	}
17998a57dfc6SChandra Seetharaman 
18001da177e4SLinus Torvalds 	return r;
18011da177e4SLinus Torvalds }
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds /*-----------------------------------------------------------------
18041da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
18051da177e4SLinus Torvalds  *---------------------------------------------------------------*/
18062b06cfffSAlasdair G Kergon static void free_minor(int minor)
18071da177e4SLinus Torvalds {
1808f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
18091da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1810f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
18111da177e4SLinus Torvalds }
18121da177e4SLinus Torvalds 
18131da177e4SLinus Torvalds /*
18141da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
18151da177e4SLinus Torvalds  */
1816cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
18171da177e4SLinus Torvalds {
1818c9d76be6STejun Heo 	int r;
18191da177e4SLinus Torvalds 
18201da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
18211da177e4SLinus Torvalds 		return -EINVAL;
18221da177e4SLinus Torvalds 
1823c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1824f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
18251da177e4SLinus Torvalds 
1826c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
18271da177e4SLinus Torvalds 
1828f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1829c9d76be6STejun Heo 	idr_preload_end();
1830c9d76be6STejun Heo 	if (r < 0)
1831c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
1832c9d76be6STejun Heo 	return 0;
18331da177e4SLinus Torvalds }
18341da177e4SLinus Torvalds 
1835cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
18361da177e4SLinus Torvalds {
1837c9d76be6STejun Heo 	int r;
18381da177e4SLinus Torvalds 
1839c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1840f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
18411da177e4SLinus Torvalds 
1842c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
18431da177e4SLinus Torvalds 
1844f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1845c9d76be6STejun Heo 	idr_preload_end();
1846c9d76be6STejun Heo 	if (r < 0)
18471da177e4SLinus Torvalds 		return r;
1848c9d76be6STejun Heo 	*minor = r;
1849c9d76be6STejun Heo 	return 0;
18501da177e4SLinus Torvalds }
18511da177e4SLinus Torvalds 
185283d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
1853f26c5719SDan Williams static const struct dax_operations dm_dax_ops;
18541da177e4SLinus Torvalds 
185553d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
185653d5914fSMikulas Patocka 
1857c12c9a3cSMike Snitzer static void dm_init_normal_md_queue(struct mapped_device *md)
1858bfebd1cdSMike Snitzer {
1859bfebd1cdSMike Snitzer 	/*
1860bfebd1cdSMike Snitzer 	 * Initialize aspects of queue that aren't relevant for blk-mq
1861bfebd1cdSMike Snitzer 	 */
1862*47ace7e0SMike Snitzer 	md->queue->backing_dev_info->congested_data = md;
1863dc3b17ccSJan Kara 	md->queue->backing_dev_info->congested_fn = dm_any_congested;
18644a0b4ddfSMike Snitzer }
18654a0b4ddfSMike Snitzer 
18660f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md)
18670f20972fSMike Snitzer {
18680f20972fSMike Snitzer 	if (md->wq)
18690f20972fSMike Snitzer 		destroy_workqueue(md->wq);
18706f1c819cSKent Overstreet 	bioset_exit(&md->bs);
18716f1c819cSKent Overstreet 	bioset_exit(&md->io_bs);
18720f20972fSMike Snitzer 
1873f26c5719SDan Williams 	if (md->dax_dev) {
1874f26c5719SDan Williams 		kill_dax(md->dax_dev);
1875f26c5719SDan Williams 		put_dax(md->dax_dev);
1876f26c5719SDan Williams 		md->dax_dev = NULL;
1877f26c5719SDan Williams 	}
1878f26c5719SDan Williams 
18790f20972fSMike Snitzer 	if (md->disk) {
18800f20972fSMike Snitzer 		spin_lock(&_minor_lock);
18810f20972fSMike Snitzer 		md->disk->private_data = NULL;
18820f20972fSMike Snitzer 		spin_unlock(&_minor_lock);
18830f20972fSMike Snitzer 		del_gendisk(md->disk);
18840f20972fSMike Snitzer 		put_disk(md->disk);
18850f20972fSMike Snitzer 	}
18860f20972fSMike Snitzer 
18870f20972fSMike Snitzer 	if (md->queue)
18880f20972fSMike Snitzer 		blk_cleanup_queue(md->queue);
18890f20972fSMike Snitzer 
1890d09960b0STahsin Erdogan 	cleanup_srcu_struct(&md->io_barrier);
1891d09960b0STahsin Erdogan 
18920f20972fSMike Snitzer 	if (md->bdev) {
18930f20972fSMike Snitzer 		bdput(md->bdev);
18940f20972fSMike Snitzer 		md->bdev = NULL;
18950f20972fSMike Snitzer 	}
18964cc96131SMike Snitzer 
1897d5ffebddSMike Snitzer 	mutex_destroy(&md->suspend_lock);
1898d5ffebddSMike Snitzer 	mutex_destroy(&md->type_lock);
1899d5ffebddSMike Snitzer 	mutex_destroy(&md->table_devices_lock);
1900d5ffebddSMike Snitzer 
19014cc96131SMike Snitzer 	dm_mq_cleanup_mapped_device(md);
19020f20972fSMike Snitzer }
19030f20972fSMike Snitzer 
19041da177e4SLinus Torvalds /*
19051da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
19061da177e4SLinus Torvalds  */
19072b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
19081da177e4SLinus Torvalds {
1909115485e8SMike Snitzer 	int r, numa_node_id = dm_get_numa_node();
1910115485e8SMike Snitzer 	struct mapped_device *md;
1911ba61fdd1SJeff Mahoney 	void *old_md;
19121da177e4SLinus Torvalds 
1913856eb091SMikulas Patocka 	md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
19141da177e4SLinus Torvalds 	if (!md) {
19151da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
19161da177e4SLinus Torvalds 		return NULL;
19171da177e4SLinus Torvalds 	}
19181da177e4SLinus Torvalds 
191910da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
19206ed7ade8SMilan Broz 		goto bad_module_get;
192110da4f79SJeff Mahoney 
19221da177e4SLinus Torvalds 	/* get a minor number for the dev */
19232b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1924cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
19252b06cfffSAlasdair G Kergon 	else
1926cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
19271da177e4SLinus Torvalds 	if (r < 0)
19286ed7ade8SMilan Broz 		goto bad_minor;
19291da177e4SLinus Torvalds 
193083d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
193183d5e5b0SMikulas Patocka 	if (r < 0)
193283d5e5b0SMikulas Patocka 		goto bad_io_barrier;
193383d5e5b0SMikulas Patocka 
1934115485e8SMike Snitzer 	md->numa_node_id = numa_node_id;
1935591ddcfcSMike Snitzer 	md->init_tio_pdu = false;
1936a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
1937e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1938a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
193986f1152bSBenjamin Marzinski 	mutex_init(&md->table_devices_lock);
1940022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
19411da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
19425c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
19431da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
19447a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
19457a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
194686f1152bSBenjamin Marzinski 	INIT_LIST_HEAD(&md->table_devices);
19477a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
19481da177e4SLinus Torvalds 
19496d469642SChristoph Hellwig 	md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id);
19501da177e4SLinus Torvalds 	if (!md->queue)
19510f20972fSMike Snitzer 		goto bad;
1952c12c9a3cSMike Snitzer 	md->queue->queuedata = md;
1953*47ace7e0SMike Snitzer 	/*
1954*47ace7e0SMike Snitzer 	 * default to bio-based required ->make_request_fn until DM
1955*47ace7e0SMike Snitzer 	 * table is loaded and md->type established. If request-based
1956*47ace7e0SMike Snitzer 	 * table is loaded: blk-mq will override accordingly.
1957*47ace7e0SMike Snitzer 	 */
1958*47ace7e0SMike Snitzer 	blk_queue_make_request(md->queue, dm_make_request);
19591da177e4SLinus Torvalds 
1960c12c9a3cSMike Snitzer 	md->disk = alloc_disk_node(1, md->numa_node_id);
19611da177e4SLinus Torvalds 	if (!md->disk)
19620f20972fSMike Snitzer 		goto bad;
19631da177e4SLinus Torvalds 
1964f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
196553d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1966f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
19672995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
1968f0b04115SJeff Mahoney 
19691da177e4SLinus Torvalds 	md->disk->major = _major;
19701da177e4SLinus Torvalds 	md->disk->first_minor = minor;
19711da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
19721da177e4SLinus Torvalds 	md->disk->queue = md->queue;
19731da177e4SLinus Torvalds 	md->disk->private_data = md;
19741da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
1975f26c5719SDan Williams 
1976976431b0SDan Williams 	if (IS_ENABLED(CONFIG_DAX_DRIVER)) {
1977fefc1d97SPankaj Gupta 		md->dax_dev = alloc_dax(md, md->disk->disk_name,
1978fefc1d97SPankaj Gupta 					&dm_dax_ops, 0);
1979514cf4f8SPeng Wang 		if (!md->dax_dev)
1980f26c5719SDan Williams 			goto bad;
1981976431b0SDan Williams 	}
1982f26c5719SDan Williams 
1983c100ec49SMike Snitzer 	add_disk_no_queue_reg(md->disk);
19847e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
19851da177e4SLinus Torvalds 
1986670368a8STejun Heo 	md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
1987304f3f6aSMilan Broz 	if (!md->wq)
19880f20972fSMike Snitzer 		goto bad;
1989304f3f6aSMilan Broz 
199032a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
199132a926daSMikulas Patocka 	if (!md->bdev)
19920f20972fSMike Snitzer 		goto bad;
199332a926daSMikulas Patocka 
1994fd2ed4d2SMikulas Patocka 	dm_stats_init(&md->stats);
1995fd2ed4d2SMikulas Patocka 
1996ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1997f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1998ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1999f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2000ba61fdd1SJeff Mahoney 
2001ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
2002ba61fdd1SJeff Mahoney 
20031da177e4SLinus Torvalds 	return md;
20041da177e4SLinus Torvalds 
20050f20972fSMike Snitzer bad:
20060f20972fSMike Snitzer 	cleanup_mapped_device(md);
200783d5e5b0SMikulas Patocka bad_io_barrier:
20081da177e4SLinus Torvalds 	free_minor(minor);
20096ed7ade8SMilan Broz bad_minor:
201010da4f79SJeff Mahoney 	module_put(THIS_MODULE);
20116ed7ade8SMilan Broz bad_module_get:
2012856eb091SMikulas Patocka 	kvfree(md);
20131da177e4SLinus Torvalds 	return NULL;
20141da177e4SLinus Torvalds }
20151da177e4SLinus Torvalds 
2016ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
2017ae9da83fSJun'ichi Nomura 
20181da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
20191da177e4SLinus Torvalds {
2020f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
202163d94e48SJun'ichi Nomura 
2022ae9da83fSJun'ichi Nomura 	unlock_fs(md);
20232eb6e1e3SKeith Busch 
20240f20972fSMike Snitzer 	cleanup_mapped_device(md);
20250f20972fSMike Snitzer 
20260f20972fSMike Snitzer 	free_table_devices(&md->table_devices);
20270f20972fSMike Snitzer 	dm_stats_cleanup(&md->stats);
202863a4f065SMike Snitzer 	free_minor(minor);
202963a4f065SMike Snitzer 
203010da4f79SJeff Mahoney 	module_put(THIS_MODULE);
2031856eb091SMikulas Patocka 	kvfree(md);
20321da177e4SLinus Torvalds }
20331da177e4SLinus Torvalds 
20342a2a4c51SJens Axboe static int __bind_mempools(struct mapped_device *md, struct dm_table *t)
2035e6ee8c0bSKiyoshi Ueda {
2036c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
20372a2a4c51SJens Axboe 	int ret = 0;
2038e6ee8c0bSKiyoshi Ueda 
2039545ed20eSToshi Kani 	if (dm_table_bio_based(t)) {
2040c0820cf5SMikulas Patocka 		/*
204164f52b0eSMike Snitzer 		 * The md may already have mempools that need changing.
204264f52b0eSMike Snitzer 		 * If so, reload bioset because front_pad may have changed
204316245bdcSJun'ichi Nomura 		 * because a different table was loaded.
2044c0820cf5SMikulas Patocka 		 */
20456f1c819cSKent Overstreet 		bioset_exit(&md->bs);
20466f1c819cSKent Overstreet 		bioset_exit(&md->io_bs);
20470776aa0eSMike Snitzer 
20486f1c819cSKent Overstreet 	} else if (bioset_initialized(&md->bs)) {
2049cbc4e3c1SMike Snitzer 		/*
20504e6e36c3SMike Snitzer 		 * There's no need to reload with request-based dm
20514e6e36c3SMike Snitzer 		 * because the size of front_pad doesn't change.
20524e6e36c3SMike Snitzer 		 * Note for future: If you are to reload bioset,
20534e6e36c3SMike Snitzer 		 * prep-ed requests in the queue may refer
20544e6e36c3SMike Snitzer 		 * to bio from the old bioset, so you must walk
20554e6e36c3SMike Snitzer 		 * through the queue to unprep.
2056cbc4e3c1SMike Snitzer 		 */
2057cbc4e3c1SMike Snitzer 		goto out;
2058cbc4e3c1SMike Snitzer 	}
2059cbc4e3c1SMike Snitzer 
20606f1c819cSKent Overstreet 	BUG_ON(!p ||
20616f1c819cSKent Overstreet 	       bioset_initialized(&md->bs) ||
20626f1c819cSKent Overstreet 	       bioset_initialized(&md->io_bs));
2063e6ee8c0bSKiyoshi Ueda 
20642a2a4c51SJens Axboe 	ret = bioset_init_from_src(&md->bs, &p->bs);
20652a2a4c51SJens Axboe 	if (ret)
20662a2a4c51SJens Axboe 		goto out;
20672a2a4c51SJens Axboe 	ret = bioset_init_from_src(&md->io_bs, &p->io_bs);
20682a2a4c51SJens Axboe 	if (ret)
20692a2a4c51SJens Axboe 		bioset_exit(&md->bs);
2070e6ee8c0bSKiyoshi Ueda out:
207102233342SMike Snitzer 	/* mempool bind completed, no longer need any mempools in the table */
2072e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
20732a2a4c51SJens Axboe 	return ret;
2074e6ee8c0bSKiyoshi Ueda }
2075e6ee8c0bSKiyoshi Ueda 
20761da177e4SLinus Torvalds /*
20771da177e4SLinus Torvalds  * Bind a table to the device.
20781da177e4SLinus Torvalds  */
20791da177e4SLinus Torvalds static void event_callback(void *context)
20801da177e4SLinus Torvalds {
20817a8c3d3bSMike Anderson 	unsigned long flags;
20827a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
20831da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
20841da177e4SLinus Torvalds 
20857a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
20867a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
20877a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
20887a8c3d3bSMike Anderson 
2089ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
20907a8c3d3bSMike Anderson 
20911da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
20921da177e4SLinus Torvalds 	wake_up(&md->eventq);
209362e08243SMikulas Patocka 	dm_issue_global_event();
20941da177e4SLinus Torvalds }
20951da177e4SLinus Torvalds 
2096c217649bSMike Snitzer /*
2097c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
2098c217649bSMike Snitzer  */
20994e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
21001da177e4SLinus Torvalds {
21011ea0654eSBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
21021ea0654eSBart Van Assche 
21034e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
21041da177e4SLinus Torvalds 
2105db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
21061da177e4SLinus Torvalds }
21071da177e4SLinus Torvalds 
2108042d2a9bSAlasdair G Kergon /*
2109042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2110042d2a9bSAlasdair G Kergon  */
2111042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2112754c5fc7SMike Snitzer 			       struct queue_limits *limits)
21131da177e4SLinus Torvalds {
2114042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
2115165125e1SJens Axboe 	struct request_queue *q = md->queue;
2116978e51baSMike Snitzer 	bool request_based = dm_table_request_based(t);
21171da177e4SLinus Torvalds 	sector_t size;
21182a2a4c51SJens Axboe 	int ret;
21191da177e4SLinus Torvalds 
21205a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
21215a8f1f80SBart Van Assche 
21221da177e4SLinus Torvalds 	size = dm_table_get_size(t);
21233ac51e74SDarrick J. Wong 
21243ac51e74SDarrick J. Wong 	/*
21253ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
21263ac51e74SDarrick J. Wong 	 */
2127fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
21283ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
21293ac51e74SDarrick J. Wong 
21304e90188bSAlasdair G Kergon 	__set_size(md, size);
21311da177e4SLinus Torvalds 
2132cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
21332ca3310eSAlasdair G Kergon 
2134e6ee8c0bSKiyoshi Ueda 	/*
2135e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
2136e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
2137e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
2138e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
2139e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
2140e6ee8c0bSKiyoshi Ueda 	 */
2141978e51baSMike Snitzer 	if (request_based)
2142eca7ee6dSMike Snitzer 		dm_stop_queue(q);
2143978e51baSMike Snitzer 
2144978e51baSMike Snitzer 	if (request_based || md->type == DM_TYPE_NVME_BIO_BASED) {
214516f12266SMike Snitzer 		/*
2146978e51baSMike Snitzer 		 * Leverage the fact that request-based DM targets and
2147978e51baSMike Snitzer 		 * NVMe bio based targets are immutable singletons
2148978e51baSMike Snitzer 		 * - used to optimize both dm_request_fn and dm_mq_queue_rq;
2149978e51baSMike Snitzer 		 *   and __process_bio.
215016f12266SMike Snitzer 		 */
215116f12266SMike Snitzer 		md->immutable_target = dm_table_get_immutable_target(t);
215216f12266SMike Snitzer 	}
2153e6ee8c0bSKiyoshi Ueda 
21542a2a4c51SJens Axboe 	ret = __bind_mempools(md, t);
21552a2a4c51SJens Axboe 	if (ret) {
21562a2a4c51SJens Axboe 		old_map = ERR_PTR(ret);
21572a2a4c51SJens Axboe 		goto out;
21582a2a4c51SJens Axboe 	}
2159e6ee8c0bSKiyoshi Ueda 
2160a12f5d48SEric Dumazet 	old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
21611d3aa6f6SMike Snitzer 	rcu_assign_pointer(md->map, (void *)t);
216236a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
216336a0456fSAlasdair G Kergon 
2164754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
216541abc4e1SHannes Reinecke 	if (old_map)
216683d5e5b0SMikulas Patocka 		dm_sync_table(md);
21672ca3310eSAlasdair G Kergon 
21682a2a4c51SJens Axboe out:
2169042d2a9bSAlasdair G Kergon 	return old_map;
21701da177e4SLinus Torvalds }
21711da177e4SLinus Torvalds 
2172a7940155SAlasdair G Kergon /*
2173a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2174a7940155SAlasdair G Kergon  */
2175a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
21761da177e4SLinus Torvalds {
2177a12f5d48SEric Dumazet 	struct dm_table *map = rcu_dereference_protected(md->map, 1);
21781da177e4SLinus Torvalds 
21791da177e4SLinus Torvalds 	if (!map)
2180a7940155SAlasdair G Kergon 		return NULL;
21811da177e4SLinus Torvalds 
21821da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
21839cdb8520SMonam Agarwal 	RCU_INIT_POINTER(md->map, NULL);
218483d5e5b0SMikulas Patocka 	dm_sync_table(md);
2185a7940155SAlasdair G Kergon 
2186a7940155SAlasdair G Kergon 	return map;
21871da177e4SLinus Torvalds }
21881da177e4SLinus Torvalds 
21891da177e4SLinus Torvalds /*
21901da177e4SLinus Torvalds  * Constructor for a new device.
21911da177e4SLinus Torvalds  */
21922b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
21931da177e4SLinus Torvalds {
2194c12c9a3cSMike Snitzer 	int r;
21951da177e4SLinus Torvalds 	struct mapped_device *md;
21961da177e4SLinus Torvalds 
21972b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
21981da177e4SLinus Torvalds 	if (!md)
21991da177e4SLinus Torvalds 		return -ENXIO;
22001da177e4SLinus Torvalds 
2201c12c9a3cSMike Snitzer 	r = dm_sysfs_init(md);
2202c12c9a3cSMike Snitzer 	if (r) {
2203c12c9a3cSMike Snitzer 		free_dev(md);
2204c12c9a3cSMike Snitzer 		return r;
2205c12c9a3cSMike Snitzer 	}
2206784aae73SMilan Broz 
22071da177e4SLinus Torvalds 	*result = md;
22081da177e4SLinus Torvalds 	return 0;
22091da177e4SLinus Torvalds }
22101da177e4SLinus Torvalds 
2211a5664dadSMike Snitzer /*
2212a5664dadSMike Snitzer  * Functions to manage md->type.
2213a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2214a5664dadSMike Snitzer  */
2215a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2216a5664dadSMike Snitzer {
2217a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2218a5664dadSMike Snitzer }
2219a5664dadSMike Snitzer 
2220a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2221a5664dadSMike Snitzer {
2222a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2223a5664dadSMike Snitzer }
2224a5664dadSMike Snitzer 
22257e0d574fSBart Van Assche void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
2226a5664dadSMike Snitzer {
222700c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2228a5664dadSMike Snitzer 	md->type = type;
2229a5664dadSMike Snitzer }
2230a5664dadSMike Snitzer 
22317e0d574fSBart Van Assche enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
2232a5664dadSMike Snitzer {
2233a5664dadSMike Snitzer 	return md->type;
2234a5664dadSMike Snitzer }
2235a5664dadSMike Snitzer 
223636a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
223736a0456fSAlasdair G Kergon {
223836a0456fSAlasdair G Kergon 	return md->immutable_target_type;
223936a0456fSAlasdair G Kergon }
224036a0456fSAlasdair G Kergon 
22414a0b4ddfSMike Snitzer /*
2242f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
2243f84cb8a4SMike Snitzer  * count on 'md'.
2244f84cb8a4SMike Snitzer  */
2245f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2246f84cb8a4SMike Snitzer {
2247f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
2248f84cb8a4SMike Snitzer 	return &md->queue->limits;
2249f84cb8a4SMike Snitzer }
2250f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2251f84cb8a4SMike Snitzer 
22524a0b4ddfSMike Snitzer /*
22534a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
22544a0b4ddfSMike Snitzer  */
2255591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
22564a0b4ddfSMike Snitzer {
2257bfebd1cdSMike Snitzer 	int r;
2258c100ec49SMike Snitzer 	struct queue_limits limits;
22597e0d574fSBart Van Assche 	enum dm_queue_mode type = dm_get_md_type(md);
2260bfebd1cdSMike Snitzer 
2261545ed20eSToshi Kani 	switch (type) {
2262bfebd1cdSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
2263e83068a5SMike Snitzer 		r = dm_mq_init_request_queue(md, t);
2264bfebd1cdSMike Snitzer 		if (r) {
2265eca7ee6dSMike Snitzer 			DMERR("Cannot initialize queue for request-based dm-mq mapped device");
2266bfebd1cdSMike Snitzer 			return r;
2267bfebd1cdSMike Snitzer 		}
2268bfebd1cdSMike Snitzer 		break;
2269bfebd1cdSMike Snitzer 	case DM_TYPE_BIO_BASED:
2270545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
2271978e51baSMike Snitzer 	case DM_TYPE_NVME_BIO_BASED:
2272978e51baSMike Snitzer 		dm_init_normal_md_queue(md);
2273bfebd1cdSMike Snitzer 		break;
22747e0d574fSBart Van Assche 	case DM_TYPE_NONE:
22757e0d574fSBart Van Assche 		WARN_ON_ONCE(true);
22767e0d574fSBart Van Assche 		break;
2277ff36ab34SMike Snitzer 	}
22784a0b4ddfSMike Snitzer 
2279c100ec49SMike Snitzer 	r = dm_calculate_queue_limits(t, &limits);
2280c100ec49SMike Snitzer 	if (r) {
2281c100ec49SMike Snitzer 		DMERR("Cannot calculate initial queue limits");
2282c100ec49SMike Snitzer 		return r;
2283c100ec49SMike Snitzer 	}
2284c100ec49SMike Snitzer 	dm_table_set_restrictions(t, md->queue, &limits);
2285c100ec49SMike Snitzer 	blk_register_queue(md->disk);
2286c100ec49SMike Snitzer 
22874a0b4ddfSMike Snitzer 	return 0;
22884a0b4ddfSMike Snitzer }
22894a0b4ddfSMike Snitzer 
22902bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev)
22911da177e4SLinus Torvalds {
22921da177e4SLinus Torvalds 	struct mapped_device *md;
22931da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
22941da177e4SLinus Torvalds 
22951da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
22961da177e4SLinus Torvalds 		return NULL;
22971da177e4SLinus Torvalds 
2298f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
22991da177e4SLinus Torvalds 
23001da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
230149de5769SMike Snitzer 	if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) ||
230249de5769SMike Snitzer 	    test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2303637842cfSDavid Teigland 		md = NULL;
2304fba9f90eSJeff Mahoney 		goto out;
2305fba9f90eSJeff Mahoney 	}
23062bec1f4aSMikulas Patocka 	dm_get(md);
2307fba9f90eSJeff Mahoney out:
2308f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
23091da177e4SLinus Torvalds 
2310637842cfSDavid Teigland 	return md;
2311637842cfSDavid Teigland }
23123cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2313d229a958SDavid Teigland 
23149ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2315637842cfSDavid Teigland {
23169ade92a9SAlasdair G Kergon 	return md->interface_ptr;
23171da177e4SLinus Torvalds }
23181da177e4SLinus Torvalds 
23191da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
23201da177e4SLinus Torvalds {
23211da177e4SLinus Torvalds 	md->interface_ptr = ptr;
23221da177e4SLinus Torvalds }
23231da177e4SLinus Torvalds 
23241da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
23251da177e4SLinus Torvalds {
23261da177e4SLinus Torvalds 	atomic_inc(&md->holders);
23273f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
23281da177e4SLinus Torvalds }
23291da177e4SLinus Torvalds 
233009ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md)
233109ee96b2SMikulas Patocka {
233209ee96b2SMikulas Patocka 	spin_lock(&_minor_lock);
233309ee96b2SMikulas Patocka 	if (test_bit(DMF_FREEING, &md->flags)) {
233409ee96b2SMikulas Patocka 		spin_unlock(&_minor_lock);
233509ee96b2SMikulas Patocka 		return -EBUSY;
233609ee96b2SMikulas Patocka 	}
233709ee96b2SMikulas Patocka 	dm_get(md);
233809ee96b2SMikulas Patocka 	spin_unlock(&_minor_lock);
233909ee96b2SMikulas Patocka 	return 0;
234009ee96b2SMikulas Patocka }
234109ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold);
234209ee96b2SMikulas Patocka 
234372d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
234472d94861SAlasdair G Kergon {
234572d94861SAlasdair G Kergon 	return md->name;
234672d94861SAlasdair G Kergon }
234772d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
234872d94861SAlasdair G Kergon 
23493f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
23501da177e4SLinus Torvalds {
23511134e5aeSMike Anderson 	struct dm_table *map;
235283d5e5b0SMikulas Patocka 	int srcu_idx;
23531da177e4SLinus Torvalds 
23543f77316dSKiyoshi Ueda 	might_sleep();
2355fba9f90eSJeff Mahoney 
235663a4f065SMike Snitzer 	spin_lock(&_minor_lock);
23573f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2358fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2359f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
23603f77316dSKiyoshi Ueda 
2361c12c9a3cSMike Snitzer 	blk_set_queue_dying(md->queue);
23623b785fbcSBart Van Assche 
2363ab7c7bb6SMikulas Patocka 	/*
2364ab7c7bb6SMikulas Patocka 	 * Take suspend_lock so that presuspend and postsuspend methods
2365ab7c7bb6SMikulas Patocka 	 * do not race with internal suspend.
2366ab7c7bb6SMikulas Patocka 	 */
2367ab7c7bb6SMikulas Patocka 	mutex_lock(&md->suspend_lock);
23682a708cffSJunichi Nomura 	map = dm_get_live_table(md, &srcu_idx);
23694f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
23701da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
23711da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
23721da177e4SLinus Torvalds 	}
237383d5e5b0SMikulas Patocka 	/* dm_put_live_table must be before msleep, otherwise deadlock is possible */
237483d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
23752a708cffSJunichi Nomura 	mutex_unlock(&md->suspend_lock);
237683d5e5b0SMikulas Patocka 
23773f77316dSKiyoshi Ueda 	/*
23783f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
23793f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
23803f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
23813f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
23823f77316dSKiyoshi Ueda 	 */
23833f77316dSKiyoshi Ueda 	if (wait)
23843f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
23853f77316dSKiyoshi Ueda 			msleep(1);
23863f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
23873f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
23883f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
23893f77316dSKiyoshi Ueda 
2390784aae73SMilan Broz 	dm_sysfs_exit(md);
2391a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
23921da177e4SLinus Torvalds 	free_dev(md);
23931da177e4SLinus Torvalds }
23943f77316dSKiyoshi Ueda 
23953f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
23963f77316dSKiyoshi Ueda {
23973f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
23983f77316dSKiyoshi Ueda }
23993f77316dSKiyoshi Ueda 
24003f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
24013f77316dSKiyoshi Ueda {
24023f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
24033f77316dSKiyoshi Ueda }
24043f77316dSKiyoshi Ueda 
24053f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
24063f77316dSKiyoshi Ueda {
24073f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
24081da177e4SLinus Torvalds }
240979eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
24101da177e4SLinus Torvalds 
2411b48633f8SBart Van Assche static int dm_wait_for_completion(struct mapped_device *md, long task_state)
241246125c1cSMilan Broz {
241346125c1cSMilan Broz 	int r = 0;
24149f4c3f87SBart Van Assche 	DEFINE_WAIT(wait);
241546125c1cSMilan Broz 
241646125c1cSMilan Broz 	while (1) {
24179f4c3f87SBart Van Assche 		prepare_to_wait(&md->wait, &wait, task_state);
241846125c1cSMilan Broz 
2419b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
242046125c1cSMilan Broz 			break;
242146125c1cSMilan Broz 
2422e3fabdfdSBart Van Assche 		if (signal_pending_state(task_state, current)) {
242346125c1cSMilan Broz 			r = -EINTR;
242446125c1cSMilan Broz 			break;
242546125c1cSMilan Broz 		}
242646125c1cSMilan Broz 
242746125c1cSMilan Broz 		io_schedule();
242846125c1cSMilan Broz 	}
24299f4c3f87SBart Van Assche 	finish_wait(&md->wait, &wait);
2430b44ebeb0SMikulas Patocka 
243146125c1cSMilan Broz 	return r;
243246125c1cSMilan Broz }
243346125c1cSMilan Broz 
24341da177e4SLinus Torvalds /*
24351da177e4SLinus Torvalds  * Process the deferred bios
24361da177e4SLinus Torvalds  */
2437ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
24381da177e4SLinus Torvalds {
2439ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2440ef208587SMikulas Patocka 						work);
24416d6f10dfSMilan Broz 	struct bio *c;
244283d5e5b0SMikulas Patocka 	int srcu_idx;
244383d5e5b0SMikulas Patocka 	struct dm_table *map;
24441da177e4SLinus Torvalds 
244583d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
2446ef208587SMikulas Patocka 
24473b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2448022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2449022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2450022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2451022c2611SMikulas Patocka 
24526a8736d1STejun Heo 		if (!c)
2453df12ee99SAlasdair G Kergon 			break;
245473d410c0SMilan Broz 
2455e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
24566548c7c5SMike Snitzer 			(void) generic_make_request(c);
2457af7e466aSMikulas Patocka 		else
24586548c7c5SMike Snitzer 			(void) dm_process_bio(md, map, c);
2459e6ee8c0bSKiyoshi Ueda 	}
24603b00b203SMikulas Patocka 
246183d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
24621da177e4SLinus Torvalds }
24631da177e4SLinus Torvalds 
24649a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2465304f3f6aSMilan Broz {
24663b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
24674e857c58SPeter Zijlstra 	smp_mb__after_atomic();
246853d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2469304f3f6aSMilan Broz }
2470304f3f6aSMilan Broz 
24711da177e4SLinus Torvalds /*
2472042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
24731da177e4SLinus Torvalds  */
2474042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
24751da177e4SLinus Torvalds {
247687eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2477754c5fc7SMike Snitzer 	struct queue_limits limits;
2478042d2a9bSAlasdair G Kergon 	int r;
24791da177e4SLinus Torvalds 
2480e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
24811da177e4SLinus Torvalds 
24821da177e4SLinus Torvalds 	/* device must be suspended */
24834f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
248493c534aeSAlasdair G Kergon 		goto out;
24851da177e4SLinus Torvalds 
24863ae70656SMike Snitzer 	/*
24873ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
24883ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
24893ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
24903ae70656SMike Snitzer 	 * reappear.
24913ae70656SMike Snitzer 	 */
24923ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
249383d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
24943ae70656SMike Snitzer 		if (live_map)
24953ae70656SMike Snitzer 			limits = md->queue->limits;
249683d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
24973ae70656SMike Snitzer 	}
24983ae70656SMike Snitzer 
249987eb5b21SMike Christie 	if (!live_map) {
2500754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2501042d2a9bSAlasdair G Kergon 		if (r) {
2502042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2503754c5fc7SMike Snitzer 			goto out;
2504042d2a9bSAlasdair G Kergon 		}
250587eb5b21SMike Christie 	}
2506754c5fc7SMike Snitzer 
2507042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
250862e08243SMikulas Patocka 	dm_issue_global_event();
25091da177e4SLinus Torvalds 
251093c534aeSAlasdair G Kergon out:
2511e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2512042d2a9bSAlasdair G Kergon 	return map;
25131da177e4SLinus Torvalds }
25141da177e4SLinus Torvalds 
25151da177e4SLinus Torvalds /*
25161da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
25171da177e4SLinus Torvalds  * device.
25181da177e4SLinus Torvalds  */
25192ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
25201da177e4SLinus Torvalds {
2521e39e2e95SAlasdair G Kergon 	int r;
25221da177e4SLinus Torvalds 
25231da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2524dfbe03f6SAlasdair G Kergon 
2525db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2526dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2527cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2528e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2529e39e2e95SAlasdair G Kergon 		return r;
2530dfbe03f6SAlasdair G Kergon 	}
2531dfbe03f6SAlasdair G Kergon 
2532aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2533aa8d7c2fSAlasdair G Kergon 
25341da177e4SLinus Torvalds 	return 0;
25351da177e4SLinus Torvalds }
25361da177e4SLinus Torvalds 
25372ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
25381da177e4SLinus Torvalds {
2539aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2540aa8d7c2fSAlasdair G Kergon 		return;
2541aa8d7c2fSAlasdair G Kergon 
2542db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
25431da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2544aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
25451da177e4SLinus Torvalds }
25461da177e4SLinus Torvalds 
25471da177e4SLinus Torvalds /*
2548b48633f8SBart Van Assche  * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
2549b48633f8SBart Van Assche  * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
2550b48633f8SBart Van Assche  * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
2551b48633f8SBart Van Assche  *
2552ffcc3936SMike Snitzer  * If __dm_suspend returns 0, the device is completely quiescent
2553ffcc3936SMike Snitzer  * now. There is no request-processing activity. All new requests
2554ffcc3936SMike Snitzer  * are being added to md->deferred list.
2555cec47e3dSKiyoshi Ueda  */
2556ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
2557b48633f8SBart Van Assche 			unsigned suspend_flags, long task_state,
2558eaf9a736SMike Snitzer 			int dmf_suspended_flag)
25591da177e4SLinus Torvalds {
2560ffcc3936SMike Snitzer 	bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2561ffcc3936SMike Snitzer 	bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2562ffcc3936SMike Snitzer 	int r;
2563cf222b37SAlasdair G Kergon 
25645a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
25655a8f1f80SBart Van Assche 
25662e93ccc1SKiyoshi Ueda 	/*
25672e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
25682e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
25692e93ccc1SKiyoshi Ueda 	 */
25702e93ccc1SKiyoshi Ueda 	if (noflush)
25712e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
257286331f39SBart Van Assche 	else
257386331f39SBart Van Assche 		pr_debug("%s: suspending with flush\n", dm_device_name(md));
25742e93ccc1SKiyoshi Ueda 
2575d67ee213SMike Snitzer 	/*
2576d67ee213SMike Snitzer 	 * This gets reverted if there's an error later and the targets
2577d67ee213SMike Snitzer 	 * provide the .presuspend_undo hook.
2578d67ee213SMike Snitzer 	 */
25791da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
25801da177e4SLinus Torvalds 
25812e93ccc1SKiyoshi Ueda 	/*
25829f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
25839f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
25849f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
25859f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
25862e93ccc1SKiyoshi Ueda 	 */
258732a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
25882ca3310eSAlasdair G Kergon 		r = lock_fs(md);
2589d67ee213SMike Snitzer 		if (r) {
2590d67ee213SMike Snitzer 			dm_table_presuspend_undo_targets(map);
2591ffcc3936SMike Snitzer 			return r;
2592aa8d7c2fSAlasdair G Kergon 		}
2593d67ee213SMike Snitzer 	}
25941da177e4SLinus Torvalds 
25951da177e4SLinus Torvalds 	/*
25963b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
25973b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
25983b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
25993b00b203SMikulas Patocka 	 * dm_wq_work.
26003b00b203SMikulas Patocka 	 *
26013b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
26023b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
26036a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
26046a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
26056a8736d1STejun Heo 	 * flush_workqueue(md->wq).
26061da177e4SLinus Torvalds 	 */
26071eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
260841abc4e1SHannes Reinecke 	if (map)
260983d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
26101da177e4SLinus Torvalds 
2611d0bcb878SKiyoshi Ueda 	/*
261229e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
261329e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2614d0bcb878SKiyoshi Ueda 	 */
26156a23e05cSJens Axboe 	if (dm_request_based(md))
2616eca7ee6dSMike Snitzer 		dm_stop_queue(md->queue);
2617cec47e3dSKiyoshi Ueda 
2618d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2619d0bcb878SKiyoshi Ueda 
26201da177e4SLinus Torvalds 	/*
26213b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
26223b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
26233b00b203SMikulas Patocka 	 * to finish.
26241da177e4SLinus Torvalds 	 */
2625b48633f8SBart Van Assche 	r = dm_wait_for_completion(md, task_state);
2626eaf9a736SMike Snitzer 	if (!r)
2627eaf9a736SMike Snitzer 		set_bit(dmf_suspended_flag, &md->flags);
26281da177e4SLinus Torvalds 
26296d6f10dfSMilan Broz 	if (noflush)
2630022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
263141abc4e1SHannes Reinecke 	if (map)
263283d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
26332e93ccc1SKiyoshi Ueda 
26341da177e4SLinus Torvalds 	/* were we interrupted ? */
263546125c1cSMilan Broz 	if (r < 0) {
26369a1fb464SMikulas Patocka 		dm_queue_flush(md);
263773d410c0SMilan Broz 
2638cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
2639eca7ee6dSMike Snitzer 			dm_start_queue(md->queue);
2640cec47e3dSKiyoshi Ueda 
26412ca3310eSAlasdair G Kergon 		unlock_fs(md);
2642d67ee213SMike Snitzer 		dm_table_presuspend_undo_targets(map);
2643ffcc3936SMike Snitzer 		/* pushback list is already flushed, so skip flush */
2644ffcc3936SMike Snitzer 	}
2645ffcc3936SMike Snitzer 
2646ffcc3936SMike Snitzer 	return r;
26472ca3310eSAlasdair G Kergon }
26482ca3310eSAlasdair G Kergon 
26493b00b203SMikulas Patocka /*
2650ffcc3936SMike Snitzer  * We need to be able to change a mapping table under a mounted
2651ffcc3936SMike Snitzer  * filesystem.  For example we might want to move some data in
2652ffcc3936SMike Snitzer  * the background.  Before the table can be swapped with
2653ffcc3936SMike Snitzer  * dm_bind_table, dm_suspend must be called to flush any in
2654ffcc3936SMike Snitzer  * flight bios and ensure that any further io gets deferred.
26553b00b203SMikulas Patocka  */
2656ffcc3936SMike Snitzer /*
2657ffcc3936SMike Snitzer  * Suspend mechanism in request-based dm.
2658ffcc3936SMike Snitzer  *
2659ffcc3936SMike Snitzer  * 1. Flush all I/Os by lock_fs() if needed.
2660ffcc3936SMike Snitzer  * 2. Stop dispatching any I/O by stopping the request_queue.
2661ffcc3936SMike Snitzer  * 3. Wait for all in-flight I/Os to be completed or requeued.
2662ffcc3936SMike Snitzer  *
2663ffcc3936SMike Snitzer  * To abort suspend, start the request_queue.
2664ffcc3936SMike Snitzer  */
2665ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2666ffcc3936SMike Snitzer {
2667ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2668ffcc3936SMike Snitzer 	int r = 0;
2669ffcc3936SMike Snitzer 
2670ffcc3936SMike Snitzer retry:
2671ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2672ffcc3936SMike Snitzer 
2673ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2674ffcc3936SMike Snitzer 		r = -EINVAL;
2675ffcc3936SMike Snitzer 		goto out_unlock;
2676ffcc3936SMike Snitzer 	}
2677ffcc3936SMike Snitzer 
2678ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2679ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2680ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2681ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2682ffcc3936SMike Snitzer 		if (r)
2683ffcc3936SMike Snitzer 			return r;
2684ffcc3936SMike Snitzer 		goto retry;
2685ffcc3936SMike Snitzer 	}
2686ffcc3936SMike Snitzer 
2687a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2688ffcc3936SMike Snitzer 
2689eaf9a736SMike Snitzer 	r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
2690ffcc3936SMike Snitzer 	if (r)
2691ffcc3936SMike Snitzer 		goto out_unlock;
26923b00b203SMikulas Patocka 
26934d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
26944d4471cbSKiyoshi Ueda 
2695d287483dSAlasdair G Kergon out_unlock:
2696e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2697cf222b37SAlasdair G Kergon 	return r;
26981da177e4SLinus Torvalds }
26991da177e4SLinus Torvalds 
2700ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map)
27011da177e4SLinus Torvalds {
2702ffcc3936SMike Snitzer 	if (map) {
2703ffcc3936SMike Snitzer 		int r = dm_table_resume_targets(map);
27048757b776SMilan Broz 		if (r)
2705ffcc3936SMike Snitzer 			return r;
2706ffcc3936SMike Snitzer 	}
27072ca3310eSAlasdair G Kergon 
27089a1fb464SMikulas Patocka 	dm_queue_flush(md);
27092ca3310eSAlasdair G Kergon 
2710cec47e3dSKiyoshi Ueda 	/*
2711cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2712cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2713cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2714cec47e3dSKiyoshi Ueda 	 */
2715cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2716eca7ee6dSMike Snitzer 		dm_start_queue(md->queue);
2717cec47e3dSKiyoshi Ueda 
27182ca3310eSAlasdair G Kergon 	unlock_fs(md);
27192ca3310eSAlasdair G Kergon 
2720ffcc3936SMike Snitzer 	return 0;
2721ffcc3936SMike Snitzer }
2722ffcc3936SMike Snitzer 
2723ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md)
2724ffcc3936SMike Snitzer {
27258dc23658SMinfei Huang 	int r;
2726ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2727ffcc3936SMike Snitzer 
2728ffcc3936SMike Snitzer retry:
27298dc23658SMinfei Huang 	r = -EINVAL;
2730ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2731ffcc3936SMike Snitzer 
2732ffcc3936SMike Snitzer 	if (!dm_suspended_md(md))
2733ffcc3936SMike Snitzer 		goto out;
2734ffcc3936SMike Snitzer 
2735ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2736ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2737ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2738ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2739ffcc3936SMike Snitzer 		if (r)
2740ffcc3936SMike Snitzer 			return r;
2741ffcc3936SMike Snitzer 		goto retry;
2742ffcc3936SMike Snitzer 	}
2743ffcc3936SMike Snitzer 
2744a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2745ffcc3936SMike Snitzer 	if (!map || !dm_table_get_size(map))
2746ffcc3936SMike Snitzer 		goto out;
2747ffcc3936SMike Snitzer 
2748ffcc3936SMike Snitzer 	r = __dm_resume(md, map);
2749ffcc3936SMike Snitzer 	if (r)
2750ffcc3936SMike Snitzer 		goto out;
2751ffcc3936SMike Snitzer 
27522ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
2753cf222b37SAlasdair G Kergon out:
2754e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
27552ca3310eSAlasdair G Kergon 
2756cf222b37SAlasdair G Kergon 	return r;
27571da177e4SLinus Torvalds }
27581da177e4SLinus Torvalds 
2759fd2ed4d2SMikulas Patocka /*
2760fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
2761fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
2762fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
2763fd2ed4d2SMikulas Patocka  */
2764fd2ed4d2SMikulas Patocka 
2765ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
2766ffcc3936SMike Snitzer {
2767ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2768ffcc3936SMike Snitzer 
27691ea0654eSBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
27701ea0654eSBart Van Assche 
277196b26c8cSMikulas Patocka 	if (md->internal_suspend_count++)
2772ffcc3936SMike Snitzer 		return; /* nested internal suspend */
2773ffcc3936SMike Snitzer 
2774ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2775ffcc3936SMike Snitzer 		set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2776ffcc3936SMike Snitzer 		return; /* nest suspend */
2777ffcc3936SMike Snitzer 	}
2778ffcc3936SMike Snitzer 
2779a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2780ffcc3936SMike Snitzer 
2781ffcc3936SMike Snitzer 	/*
2782ffcc3936SMike Snitzer 	 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2783ffcc3936SMike Snitzer 	 * supported.  Properly supporting a TASK_INTERRUPTIBLE internal suspend
2784ffcc3936SMike Snitzer 	 * would require changing .presuspend to return an error -- avoid this
2785ffcc3936SMike Snitzer 	 * until there is a need for more elaborate variants of internal suspend.
2786ffcc3936SMike Snitzer 	 */
2787eaf9a736SMike Snitzer 	(void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
2788eaf9a736SMike Snitzer 			    DMF_SUSPENDED_INTERNALLY);
2789ffcc3936SMike Snitzer 
2790ffcc3936SMike Snitzer 	dm_table_postsuspend_targets(map);
2791ffcc3936SMike Snitzer }
2792ffcc3936SMike Snitzer 
2793ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md)
2794ffcc3936SMike Snitzer {
279596b26c8cSMikulas Patocka 	BUG_ON(!md->internal_suspend_count);
279696b26c8cSMikulas Patocka 
279796b26c8cSMikulas Patocka 	if (--md->internal_suspend_count)
2798ffcc3936SMike Snitzer 		return; /* resume from nested internal suspend */
2799ffcc3936SMike Snitzer 
2800ffcc3936SMike Snitzer 	if (dm_suspended_md(md))
2801ffcc3936SMike Snitzer 		goto done; /* resume from nested suspend */
2802ffcc3936SMike Snitzer 
2803ffcc3936SMike Snitzer 	/*
2804ffcc3936SMike Snitzer 	 * NOTE: existing callers don't need to call dm_table_resume_targets
2805ffcc3936SMike Snitzer 	 * (which may fail -- so best to avoid it for now by passing NULL map)
2806ffcc3936SMike Snitzer 	 */
2807ffcc3936SMike Snitzer 	(void) __dm_resume(md, NULL);
2808ffcc3936SMike Snitzer 
2809ffcc3936SMike Snitzer done:
2810ffcc3936SMike Snitzer 	clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2811ffcc3936SMike Snitzer 	smp_mb__after_atomic();
2812ffcc3936SMike Snitzer 	wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2813ffcc3936SMike Snitzer }
2814ffcc3936SMike Snitzer 
2815ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md)
2816fd2ed4d2SMikulas Patocka {
2817fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
2818ffcc3936SMike Snitzer 	__dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2819ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2820ffcc3936SMike Snitzer }
2821ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
2822ffcc3936SMike Snitzer 
2823ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md)
2824ffcc3936SMike Snitzer {
2825ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2826ffcc3936SMike Snitzer 	__dm_internal_resume(md);
2827ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2828ffcc3936SMike Snitzer }
2829ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume);
2830ffcc3936SMike Snitzer 
2831ffcc3936SMike Snitzer /*
2832ffcc3936SMike Snitzer  * Fast variants of internal suspend/resume hold md->suspend_lock,
2833ffcc3936SMike Snitzer  * which prevents interaction with userspace-driven suspend.
2834ffcc3936SMike Snitzer  */
2835ffcc3936SMike Snitzer 
2836ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md)
2837ffcc3936SMike Snitzer {
2838ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2839ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2840fd2ed4d2SMikulas Patocka 		return;
2841fd2ed4d2SMikulas Patocka 
2842fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2843fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
2844fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
2845fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2846fd2ed4d2SMikulas Patocka }
2847b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
2848fd2ed4d2SMikulas Patocka 
2849ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md)
2850fd2ed4d2SMikulas Patocka {
2851ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2852fd2ed4d2SMikulas Patocka 		goto done;
2853fd2ed4d2SMikulas Patocka 
2854fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
2855fd2ed4d2SMikulas Patocka 
2856fd2ed4d2SMikulas Patocka done:
2857fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
2858fd2ed4d2SMikulas Patocka }
2859b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
2860fd2ed4d2SMikulas Patocka 
28611da177e4SLinus Torvalds /*-----------------------------------------------------------------
28621da177e4SLinus Torvalds  * Event notification.
28631da177e4SLinus Torvalds  *---------------------------------------------------------------*/
28643abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
286560935eb2SMilan Broz 		       unsigned cookie)
286669267a30SAlasdair G Kergon {
286760935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
286860935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
286960935eb2SMilan Broz 
287060935eb2SMilan Broz 	if (!cookie)
28713abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
287260935eb2SMilan Broz 	else {
287360935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
287460935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
28753abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
28763abf85b5SPeter Rajnoha 					  action, envp);
287760935eb2SMilan Broz 	}
287869267a30SAlasdair G Kergon }
287969267a30SAlasdair G Kergon 
28807a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
28817a8c3d3bSMike Anderson {
28827a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
28837a8c3d3bSMike Anderson }
28847a8c3d3bSMike Anderson 
28851da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
28861da177e4SLinus Torvalds {
28871da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
28881da177e4SLinus Torvalds }
28891da177e4SLinus Torvalds 
28901da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
28911da177e4SLinus Torvalds {
28921da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
28931da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
28941da177e4SLinus Torvalds }
28951da177e4SLinus Torvalds 
28967a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
28977a8c3d3bSMike Anderson {
28987a8c3d3bSMike Anderson 	unsigned long flags;
28997a8c3d3bSMike Anderson 
29007a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
29017a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
29027a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
29037a8c3d3bSMike Anderson }
29047a8c3d3bSMike Anderson 
29051da177e4SLinus Torvalds /*
29061da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
29071da177e4SLinus Torvalds  * count on 'md'.
29081da177e4SLinus Torvalds  */
29091da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
29101da177e4SLinus Torvalds {
29111da177e4SLinus Torvalds 	return md->disk;
29121da177e4SLinus Torvalds }
291365ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk);
29141da177e4SLinus Torvalds 
2915784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2916784aae73SMilan Broz {
29172995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
2918784aae73SMilan Broz }
2919784aae73SMilan Broz 
2920784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2921784aae73SMilan Broz {
2922784aae73SMilan Broz 	struct mapped_device *md;
2923784aae73SMilan Broz 
29242995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
2925784aae73SMilan Broz 
2926b9a41d21SHou Tao 	spin_lock(&_minor_lock);
2927b9a41d21SHou Tao 	if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2928b9a41d21SHou Tao 		md = NULL;
2929b9a41d21SHou Tao 		goto out;
2930b9a41d21SHou Tao 	}
2931784aae73SMilan Broz 	dm_get(md);
2932b9a41d21SHou Tao out:
2933b9a41d21SHou Tao 	spin_unlock(&_minor_lock);
2934b9a41d21SHou Tao 
2935784aae73SMilan Broz 	return md;
2936784aae73SMilan Broz }
2937784aae73SMilan Broz 
29384f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
29391da177e4SLinus Torvalds {
29401da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
29411da177e4SLinus Torvalds }
29421da177e4SLinus Torvalds 
2943ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md)
2944ffcc3936SMike Snitzer {
2945ffcc3936SMike Snitzer 	return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2946ffcc3936SMike Snitzer }
2947ffcc3936SMike Snitzer 
29482c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
29492c140a24SMikulas Patocka {
29502c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
29512c140a24SMikulas Patocka }
29522c140a24SMikulas Patocka 
295364dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
295464dbce58SKiyoshi Ueda {
2955ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
295664dbce58SKiyoshi Ueda }
295764dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
295864dbce58SKiyoshi Ueda 
29592e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
29602e93ccc1SKiyoshi Ueda {
2961ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
29622e93ccc1SKiyoshi Ueda }
29632e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
29642e93ccc1SKiyoshi Ueda 
29657e0d574fSBart Van Assche struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type,
29660776aa0eSMike Snitzer 					    unsigned integrity, unsigned per_io_data_size,
29670776aa0eSMike Snitzer 					    unsigned min_pool_size)
2968e6ee8c0bSKiyoshi Ueda {
2969115485e8SMike Snitzer 	struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
297078d8e58aSMike Snitzer 	unsigned int pool_size = 0;
297164f52b0eSMike Snitzer 	unsigned int front_pad, io_front_pad;
29726f1c819cSKent Overstreet 	int ret;
2973e6ee8c0bSKiyoshi Ueda 
2974e6ee8c0bSKiyoshi Ueda 	if (!pools)
29754e6e36c3SMike Snitzer 		return NULL;
2976e6ee8c0bSKiyoshi Ueda 
297778d8e58aSMike Snitzer 	switch (type) {
297878d8e58aSMike Snitzer 	case DM_TYPE_BIO_BASED:
2979545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
298022c11858SMike Snitzer 	case DM_TYPE_NVME_BIO_BASED:
29810776aa0eSMike Snitzer 		pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size);
298230187e1dSMike Snitzer 		front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
298364f52b0eSMike Snitzer 		io_front_pad = roundup(front_pad,  __alignof__(struct dm_io)) + offsetof(struct dm_io, tio);
29846f1c819cSKent Overstreet 		ret = bioset_init(&pools->io_bs, pool_size, io_front_pad, 0);
29856f1c819cSKent Overstreet 		if (ret)
298664f52b0eSMike Snitzer 			goto out;
29876f1c819cSKent Overstreet 		if (integrity && bioset_integrity_create(&pools->io_bs, pool_size))
2988eb8db831SChristoph Hellwig 			goto out;
298978d8e58aSMike Snitzer 		break;
299078d8e58aSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
29910776aa0eSMike Snitzer 		pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size);
299278d8e58aSMike Snitzer 		front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
2993591ddcfcSMike Snitzer 		/* per_io_data_size is used for blk-mq pdu at queue allocation */
299478d8e58aSMike Snitzer 		break;
299578d8e58aSMike Snitzer 	default:
299678d8e58aSMike Snitzer 		BUG();
299778d8e58aSMike Snitzer 	}
299878d8e58aSMike Snitzer 
29996f1c819cSKent Overstreet 	ret = bioset_init(&pools->bs, pool_size, front_pad, 0);
30006f1c819cSKent Overstreet 	if (ret)
30015f015204SJun'ichi Nomura 		goto out;
3002e6ee8c0bSKiyoshi Ueda 
30036f1c819cSKent Overstreet 	if (integrity && bioset_integrity_create(&pools->bs, pool_size))
30045f015204SJun'ichi Nomura 		goto out;
3005a91a2785SMartin K. Petersen 
3006e6ee8c0bSKiyoshi Ueda 	return pools;
300778d8e58aSMike Snitzer 
30085f015204SJun'ichi Nomura out:
30095f015204SJun'ichi Nomura 	dm_free_md_mempools(pools);
3010e6ee8c0bSKiyoshi Ueda 
30114e6e36c3SMike Snitzer 	return NULL;
3012e6ee8c0bSKiyoshi Ueda }
3013e6ee8c0bSKiyoshi Ueda 
3014e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
3015e6ee8c0bSKiyoshi Ueda {
3016e6ee8c0bSKiyoshi Ueda 	if (!pools)
3017e6ee8c0bSKiyoshi Ueda 		return;
3018e6ee8c0bSKiyoshi Ueda 
30196f1c819cSKent Overstreet 	bioset_exit(&pools->bs);
30206f1c819cSKent Overstreet 	bioset_exit(&pools->io_bs);
3021e6ee8c0bSKiyoshi Ueda 
3022e6ee8c0bSKiyoshi Ueda 	kfree(pools);
3023e6ee8c0bSKiyoshi Ueda }
3024e6ee8c0bSKiyoshi Ueda 
30259c72bad1SChristoph Hellwig struct dm_pr {
30269c72bad1SChristoph Hellwig 	u64	old_key;
30279c72bad1SChristoph Hellwig 	u64	new_key;
30289c72bad1SChristoph Hellwig 	u32	flags;
30299c72bad1SChristoph Hellwig 	bool	fail_early;
30309c72bad1SChristoph Hellwig };
30319c72bad1SChristoph Hellwig 
30329c72bad1SChristoph Hellwig static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
30339c72bad1SChristoph Hellwig 		      void *data)
30349c72bad1SChristoph Hellwig {
30359c72bad1SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
30369c72bad1SChristoph Hellwig 	struct dm_table *table;
30379c72bad1SChristoph Hellwig 	struct dm_target *ti;
30389c72bad1SChristoph Hellwig 	int ret = -ENOTTY, srcu_idx;
30399c72bad1SChristoph Hellwig 
30409c72bad1SChristoph Hellwig 	table = dm_get_live_table(md, &srcu_idx);
30419c72bad1SChristoph Hellwig 	if (!table || !dm_table_get_size(table))
30429c72bad1SChristoph Hellwig 		goto out;
30439c72bad1SChristoph Hellwig 
30449c72bad1SChristoph Hellwig 	/* We only support devices that have a single target */
30459c72bad1SChristoph Hellwig 	if (dm_table_get_num_targets(table) != 1)
30469c72bad1SChristoph Hellwig 		goto out;
30479c72bad1SChristoph Hellwig 	ti = dm_table_get_target(table, 0);
30489c72bad1SChristoph Hellwig 
30499c72bad1SChristoph Hellwig 	ret = -EINVAL;
30509c72bad1SChristoph Hellwig 	if (!ti->type->iterate_devices)
30519c72bad1SChristoph Hellwig 		goto out;
30529c72bad1SChristoph Hellwig 
30539c72bad1SChristoph Hellwig 	ret = ti->type->iterate_devices(ti, fn, data);
30549c72bad1SChristoph Hellwig out:
30559c72bad1SChristoph Hellwig 	dm_put_live_table(md, srcu_idx);
30569c72bad1SChristoph Hellwig 	return ret;
30579c72bad1SChristoph Hellwig }
30589c72bad1SChristoph Hellwig 
30599c72bad1SChristoph Hellwig /*
30609c72bad1SChristoph Hellwig  * For register / unregister we need to manually call out to every path.
30619c72bad1SChristoph Hellwig  */
30629c72bad1SChristoph Hellwig static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
30639c72bad1SChristoph Hellwig 			    sector_t start, sector_t len, void *data)
30649c72bad1SChristoph Hellwig {
30659c72bad1SChristoph Hellwig 	struct dm_pr *pr = data;
30669c72bad1SChristoph Hellwig 	const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
30679c72bad1SChristoph Hellwig 
30689c72bad1SChristoph Hellwig 	if (!ops || !ops->pr_register)
30699c72bad1SChristoph Hellwig 		return -EOPNOTSUPP;
30709c72bad1SChristoph Hellwig 	return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
30719c72bad1SChristoph Hellwig }
30729c72bad1SChristoph Hellwig 
307371cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
307471cdb697SChristoph Hellwig 			  u32 flags)
307571cdb697SChristoph Hellwig {
30769c72bad1SChristoph Hellwig 	struct dm_pr pr = {
30779c72bad1SChristoph Hellwig 		.old_key	= old_key,
30789c72bad1SChristoph Hellwig 		.new_key	= new_key,
30799c72bad1SChristoph Hellwig 		.flags		= flags,
30809c72bad1SChristoph Hellwig 		.fail_early	= true,
30819c72bad1SChristoph Hellwig 	};
30829c72bad1SChristoph Hellwig 	int ret;
308371cdb697SChristoph Hellwig 
30849c72bad1SChristoph Hellwig 	ret = dm_call_pr(bdev, __dm_pr_register, &pr);
30859c72bad1SChristoph Hellwig 	if (ret && new_key) {
30869c72bad1SChristoph Hellwig 		/* unregister all paths if we failed to register any path */
30879c72bad1SChristoph Hellwig 		pr.old_key = new_key;
30889c72bad1SChristoph Hellwig 		pr.new_key = 0;
30899c72bad1SChristoph Hellwig 		pr.flags = 0;
30909c72bad1SChristoph Hellwig 		pr.fail_early = false;
30919c72bad1SChristoph Hellwig 		dm_call_pr(bdev, __dm_pr_register, &pr);
30929c72bad1SChristoph Hellwig 	}
309371cdb697SChristoph Hellwig 
30949c72bad1SChristoph Hellwig 	return ret;
309571cdb697SChristoph Hellwig }
309671cdb697SChristoph Hellwig 
309771cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
309871cdb697SChristoph Hellwig 			 u32 flags)
309971cdb697SChristoph Hellwig {
310071cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
310171cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3102971888c4SMike Snitzer 	int r, srcu_idx;
310371cdb697SChristoph Hellwig 
31045bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
310571cdb697SChristoph Hellwig 	if (r < 0)
3106971888c4SMike Snitzer 		goto out;
310771cdb697SChristoph Hellwig 
310871cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
310971cdb697SChristoph Hellwig 	if (ops && ops->pr_reserve)
311071cdb697SChristoph Hellwig 		r = ops->pr_reserve(bdev, key, type, flags);
311171cdb697SChristoph Hellwig 	else
311271cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3113971888c4SMike Snitzer out:
3114971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
311571cdb697SChristoph Hellwig 	return r;
311671cdb697SChristoph Hellwig }
311771cdb697SChristoph Hellwig 
311871cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
311971cdb697SChristoph Hellwig {
312071cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
312171cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3122971888c4SMike Snitzer 	int r, srcu_idx;
312371cdb697SChristoph Hellwig 
31245bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
312571cdb697SChristoph Hellwig 	if (r < 0)
3126971888c4SMike Snitzer 		goto out;
312771cdb697SChristoph Hellwig 
312871cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
312971cdb697SChristoph Hellwig 	if (ops && ops->pr_release)
313071cdb697SChristoph Hellwig 		r = ops->pr_release(bdev, key, type);
313171cdb697SChristoph Hellwig 	else
313271cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3133971888c4SMike Snitzer out:
3134971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
313571cdb697SChristoph Hellwig 	return r;
313671cdb697SChristoph Hellwig }
313771cdb697SChristoph Hellwig 
313871cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
313971cdb697SChristoph Hellwig 			 enum pr_type type, bool abort)
314071cdb697SChristoph Hellwig {
314171cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
314271cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3143971888c4SMike Snitzer 	int r, srcu_idx;
314471cdb697SChristoph Hellwig 
31455bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
314671cdb697SChristoph Hellwig 	if (r < 0)
3147971888c4SMike Snitzer 		goto out;
314871cdb697SChristoph Hellwig 
314971cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
315071cdb697SChristoph Hellwig 	if (ops && ops->pr_preempt)
315171cdb697SChristoph Hellwig 		r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
315271cdb697SChristoph Hellwig 	else
315371cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3154971888c4SMike Snitzer out:
3155971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
315671cdb697SChristoph Hellwig 	return r;
315771cdb697SChristoph Hellwig }
315871cdb697SChristoph Hellwig 
315971cdb697SChristoph Hellwig static int dm_pr_clear(struct block_device *bdev, u64 key)
316071cdb697SChristoph Hellwig {
316171cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
316271cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3163971888c4SMike Snitzer 	int r, srcu_idx;
316471cdb697SChristoph Hellwig 
31655bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
316671cdb697SChristoph Hellwig 	if (r < 0)
3167971888c4SMike Snitzer 		goto out;
316871cdb697SChristoph Hellwig 
316971cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
317071cdb697SChristoph Hellwig 	if (ops && ops->pr_clear)
317171cdb697SChristoph Hellwig 		r = ops->pr_clear(bdev, key);
317271cdb697SChristoph Hellwig 	else
317371cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3174971888c4SMike Snitzer out:
3175971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
317671cdb697SChristoph Hellwig 	return r;
317771cdb697SChristoph Hellwig }
317871cdb697SChristoph Hellwig 
317971cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = {
318071cdb697SChristoph Hellwig 	.pr_register	= dm_pr_register,
318171cdb697SChristoph Hellwig 	.pr_reserve	= dm_pr_reserve,
318271cdb697SChristoph Hellwig 	.pr_release	= dm_pr_release,
318371cdb697SChristoph Hellwig 	.pr_preempt	= dm_pr_preempt,
318471cdb697SChristoph Hellwig 	.pr_clear	= dm_pr_clear,
318571cdb697SChristoph Hellwig };
318671cdb697SChristoph Hellwig 
318783d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
31881da177e4SLinus Torvalds 	.open = dm_blk_open,
31891da177e4SLinus Torvalds 	.release = dm_blk_close,
3190aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
31913ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
3192e76239a3SChristoph Hellwig 	.report_zones = dm_blk_report_zones,
319371cdb697SChristoph Hellwig 	.pr_ops = &dm_pr_ops,
31941da177e4SLinus Torvalds 	.owner = THIS_MODULE
31951da177e4SLinus Torvalds };
31961da177e4SLinus Torvalds 
3197f26c5719SDan Williams static const struct dax_operations dm_dax_ops = {
3198f26c5719SDan Williams 	.direct_access = dm_dax_direct_access,
31997bf7eac8SDan Williams 	.dax_supported = dm_dax_supported,
32007e026c8cSDan Williams 	.copy_from_iter = dm_dax_copy_from_iter,
3201b3a9a0c3SDan Williams 	.copy_to_iter = dm_dax_copy_to_iter,
3202f26c5719SDan Williams };
3203f26c5719SDan Williams 
32041da177e4SLinus Torvalds /*
32051da177e4SLinus Torvalds  * module hooks
32061da177e4SLinus Torvalds  */
32071da177e4SLinus Torvalds module_init(dm_init);
32081da177e4SLinus Torvalds module_exit(dm_exit);
32091da177e4SLinus Torvalds 
32101da177e4SLinus Torvalds module_param(major, uint, 0);
32111da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
3212f4790826SMike Snitzer 
3213e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3214e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3215e8603136SMike Snitzer 
3216115485e8SMike Snitzer module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
3217115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
3218115485e8SMike Snitzer 
32191da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
32201da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
32211da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3222