xref: /openbmc/linux/drivers/md/dm.c (revision fb08a1908cb119a4585611d91461ab6d27756b14)
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"
1191ccbbacSTushar Sugandhi #include "dm-ima.h"
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include <linux/init.h>
141da177e4SLinus Torvalds #include <linux/module.h>
1548c9c27bSArjan van de Ven #include <linux/mutex.h>
166958c1c6SMikulas Patocka #include <linux/sched/mm.h>
17174cd4b1SIngo Molnar #include <linux/sched/signal.h>
181da177e4SLinus Torvalds #include <linux/blkpg.h>
191da177e4SLinus Torvalds #include <linux/bio.h>
201da177e4SLinus Torvalds #include <linux/mempool.h>
21f26c5719SDan Williams #include <linux/dax.h>
221da177e4SLinus Torvalds #include <linux/slab.h>
231da177e4SLinus Torvalds #include <linux/idr.h>
247e026c8cSDan Williams #include <linux/uio.h>
253ac51e74SDarrick J. Wong #include <linux/hdreg.h>
263f77316dSKiyoshi Ueda #include <linux/delay.h>
27ffcc3936SMike Snitzer #include <linux/wait.h>
2871cdb697SChristoph Hellwig #include <linux/pr.h>
29b0b4d7c6SElena Reshetova #include <linux/refcount.h>
30c6a564ffSChristoph Hellwig #include <linux/part_stat.h>
31a892c8d5SSatya Tangirala #include <linux/blk-crypto.h>
321e8d44bdSEric Biggers #include <linux/blk-crypto-profile.h>
3355782138SLi Zefan 
3472d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
3572d94861SAlasdair G Kergon 
3660935eb2SMilan Broz /*
3760935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
3860935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
3960935eb2SMilan Broz  */
4060935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
4160935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
4260935eb2SMilan Broz 
431da177e4SLinus Torvalds static const char *_name = DM_NAME;
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds static unsigned int major = 0;
461da177e4SLinus Torvalds static unsigned int _major = 0;
471da177e4SLinus Torvalds 
48d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr);
49d15b774cSAlasdair G Kergon 
50f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
512c140a24SMikulas Patocka 
522c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w);
532c140a24SMikulas Patocka 
542c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
552c140a24SMikulas Patocka 
56acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue;
57acfe0ad7SMikulas Patocka 
5893e6442cSMikulas Patocka atomic_t dm_global_event_nr = ATOMIC_INIT(0);
5993e6442cSMikulas Patocka DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq);
6093e6442cSMikulas Patocka 
6162e08243SMikulas Patocka void dm_issue_global_event(void)
6262e08243SMikulas Patocka {
6362e08243SMikulas Patocka 	atomic_inc(&dm_global_event_nr);
6462e08243SMikulas Patocka 	wake_up(&dm_global_eventq);
6562e08243SMikulas Patocka }
6662e08243SMikulas Patocka 
671da177e4SLinus Torvalds /*
6864f52b0eSMike Snitzer  * One of these is allocated (on-stack) per original bio.
691da177e4SLinus Torvalds  */
7064f52b0eSMike Snitzer struct clone_info {
7164f52b0eSMike Snitzer 	struct dm_table *map;
7264f52b0eSMike Snitzer 	struct bio *bio;
7364f52b0eSMike Snitzer 	struct dm_io *io;
7464f52b0eSMike Snitzer 	sector_t sector;
7564f52b0eSMike Snitzer 	unsigned sector_count;
7664f52b0eSMike Snitzer };
7764f52b0eSMike Snitzer 
7862f26317SJeffle Xu #define DM_TARGET_IO_BIO_OFFSET (offsetof(struct dm_target_io, clone))
7962f26317SJeffle Xu #define DM_IO_BIO_OFFSET \
8062f26317SJeffle Xu 	(offsetof(struct dm_target_io, clone) + offsetof(struct dm_io, tio))
8162f26317SJeffle Xu 
8264f52b0eSMike Snitzer void *dm_per_bio_data(struct bio *bio, size_t data_size)
8364f52b0eSMike Snitzer {
8464f52b0eSMike Snitzer 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
8564f52b0eSMike Snitzer 	if (!tio->inside_dm_io)
8662f26317SJeffle Xu 		return (char *)bio - DM_TARGET_IO_BIO_OFFSET - data_size;
8762f26317SJeffle Xu 	return (char *)bio - DM_IO_BIO_OFFSET - data_size;
8864f52b0eSMike Snitzer }
8964f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_per_bio_data);
9064f52b0eSMike Snitzer 
9164f52b0eSMike Snitzer struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size)
9264f52b0eSMike Snitzer {
9364f52b0eSMike Snitzer 	struct dm_io *io = (struct dm_io *)((char *)data + data_size);
9464f52b0eSMike Snitzer 	if (io->magic == DM_IO_MAGIC)
9562f26317SJeffle Xu 		return (struct bio *)((char *)io + DM_IO_BIO_OFFSET);
9664f52b0eSMike Snitzer 	BUG_ON(io->magic != DM_TIO_MAGIC);
9762f26317SJeffle Xu 	return (struct bio *)((char *)io + DM_TARGET_IO_BIO_OFFSET);
9864f52b0eSMike Snitzer }
9964f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_from_per_bio_data);
10064f52b0eSMike Snitzer 
10164f52b0eSMike Snitzer unsigned dm_bio_get_target_bio_nr(const struct bio *bio)
10264f52b0eSMike Snitzer {
10364f52b0eSMike Snitzer 	return container_of(bio, struct dm_target_io, clone)->target_bio_nr;
10464f52b0eSMike Snitzer }
10564f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_get_target_bio_nr);
10664f52b0eSMike Snitzer 
107ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
108ba61fdd1SJeff Mahoney 
109115485e8SMike Snitzer #define DM_NUMA_NODE NUMA_NO_NODE
110115485e8SMike Snitzer static int dm_numa_node = DM_NUMA_NODE;
111faad87dfSMike Snitzer 
112a666e5c0SMikulas Patocka #define DEFAULT_SWAP_BIOS	(8 * 1048576 / PAGE_SIZE)
113a666e5c0SMikulas Patocka static int swap_bios = DEFAULT_SWAP_BIOS;
114a666e5c0SMikulas Patocka static int get_swap_bios(void)
115a666e5c0SMikulas Patocka {
116a666e5c0SMikulas Patocka 	int latch = READ_ONCE(swap_bios);
117a666e5c0SMikulas Patocka 	if (unlikely(latch <= 0))
118a666e5c0SMikulas Patocka 		latch = DEFAULT_SWAP_BIOS;
119a666e5c0SMikulas Patocka 	return latch;
120a666e5c0SMikulas Patocka }
121a666e5c0SMikulas Patocka 
122e6ee8c0bSKiyoshi Ueda /*
123e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
124e6ee8c0bSKiyoshi Ueda  */
125e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
1266f1c819cSKent Overstreet 	struct bio_set bs;
1276f1c819cSKent Overstreet 	struct bio_set io_bs;
128e6ee8c0bSKiyoshi Ueda };
129e6ee8c0bSKiyoshi Ueda 
13086f1152bSBenjamin Marzinski struct table_device {
13186f1152bSBenjamin Marzinski 	struct list_head list;
132b0b4d7c6SElena Reshetova 	refcount_t count;
13386f1152bSBenjamin Marzinski 	struct dm_dev dm_dev;
13486f1152bSBenjamin Marzinski };
13586f1152bSBenjamin Marzinski 
136f4790826SMike Snitzer /*
137e8603136SMike Snitzer  * Bio-based DM's mempools' reserved IOs set by the user.
138e8603136SMike Snitzer  */
1394cc96131SMike Snitzer #define RESERVED_BIO_BASED_IOS		16
140e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
141e8603136SMike Snitzer 
142115485e8SMike Snitzer static int __dm_get_module_param_int(int *module_param, int min, int max)
143115485e8SMike Snitzer {
1446aa7de05SMark Rutland 	int param = READ_ONCE(*module_param);
145115485e8SMike Snitzer 	int modified_param = 0;
146115485e8SMike Snitzer 	bool modified = true;
147115485e8SMike Snitzer 
148115485e8SMike Snitzer 	if (param < min)
149115485e8SMike Snitzer 		modified_param = min;
150115485e8SMike Snitzer 	else if (param > max)
151115485e8SMike Snitzer 		modified_param = max;
152115485e8SMike Snitzer 	else
153115485e8SMike Snitzer 		modified = false;
154115485e8SMike Snitzer 
155115485e8SMike Snitzer 	if (modified) {
156115485e8SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
157115485e8SMike Snitzer 		param = modified_param;
158115485e8SMike Snitzer 	}
159115485e8SMike Snitzer 
160115485e8SMike Snitzer 	return param;
161115485e8SMike Snitzer }
162115485e8SMike Snitzer 
1634cc96131SMike Snitzer unsigned __dm_get_module_param(unsigned *module_param,
164f4790826SMike Snitzer 			       unsigned def, unsigned max)
165f4790826SMike Snitzer {
1666aa7de05SMark Rutland 	unsigned param = READ_ONCE(*module_param);
16709c2d531SMike Snitzer 	unsigned modified_param = 0;
168f4790826SMike Snitzer 
16909c2d531SMike Snitzer 	if (!param)
17009c2d531SMike Snitzer 		modified_param = def;
17109c2d531SMike Snitzer 	else if (param > max)
17209c2d531SMike Snitzer 		modified_param = max;
173f4790826SMike Snitzer 
17409c2d531SMike Snitzer 	if (modified_param) {
17509c2d531SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
17609c2d531SMike Snitzer 		param = modified_param;
177f4790826SMike Snitzer 	}
178f4790826SMike Snitzer 
17909c2d531SMike Snitzer 	return param;
180f4790826SMike Snitzer }
181f4790826SMike Snitzer 
182e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void)
183e8603136SMike Snitzer {
18409c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_bio_based_ios,
1854cc96131SMike Snitzer 				     RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS);
186e8603136SMike Snitzer }
187e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
188e8603136SMike Snitzer 
189115485e8SMike Snitzer static unsigned dm_get_numa_node(void)
190115485e8SMike Snitzer {
191115485e8SMike Snitzer 	return __dm_get_module_param_int(&dm_numa_node,
192115485e8SMike Snitzer 					 DM_NUMA_NODE, num_online_nodes() - 1);
193115485e8SMike Snitzer }
194115485e8SMike Snitzer 
1951da177e4SLinus Torvalds static int __init local_init(void)
1961da177e4SLinus Torvalds {
197e689fbabSMike Snitzer 	int r;
1981ae49ea2SMike Snitzer 
19951e5b2bdSMike Anderson 	r = dm_uevent_init();
20051157b4aSKiyoshi Ueda 	if (r)
201e689fbabSMike Snitzer 		return r;
20251e5b2bdSMike Anderson 
203acfe0ad7SMikulas Patocka 	deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
204acfe0ad7SMikulas Patocka 	if (!deferred_remove_workqueue) {
205acfe0ad7SMikulas Patocka 		r = -ENOMEM;
206acfe0ad7SMikulas Patocka 		goto out_uevent_exit;
207acfe0ad7SMikulas Patocka 	}
208acfe0ad7SMikulas Patocka 
2091da177e4SLinus Torvalds 	_major = major;
2101da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
21151157b4aSKiyoshi Ueda 	if (r < 0)
212acfe0ad7SMikulas Patocka 		goto out_free_workqueue;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	if (!_major)
2151da177e4SLinus Torvalds 		_major = r;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	return 0;
21851157b4aSKiyoshi Ueda 
219acfe0ad7SMikulas Patocka out_free_workqueue:
220acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
22151157b4aSKiyoshi Ueda out_uevent_exit:
22251157b4aSKiyoshi Ueda 	dm_uevent_exit();
22351157b4aSKiyoshi Ueda 
22451157b4aSKiyoshi Ueda 	return r;
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds static void local_exit(void)
2281da177e4SLinus Torvalds {
2292c140a24SMikulas Patocka 	flush_scheduled_work();
230acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
2312c140a24SMikulas Patocka 
23200d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
23351e5b2bdSMike Anderson 	dm_uevent_exit();
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	_major = 0;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	DMINFO("cleaned up");
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds 
240b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
2411da177e4SLinus Torvalds 	local_init,
2421da177e4SLinus Torvalds 	dm_target_init,
2431da177e4SLinus Torvalds 	dm_linear_init,
2441da177e4SLinus Torvalds 	dm_stripe_init,
245952b3557SMikulas Patocka 	dm_io_init,
246945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2471da177e4SLinus Torvalds 	dm_interface_init,
248fd2ed4d2SMikulas Patocka 	dm_statistics_init,
2491da177e4SLinus Torvalds };
2501da177e4SLinus Torvalds 
251b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2521da177e4SLinus Torvalds 	local_exit,
2531da177e4SLinus Torvalds 	dm_target_exit,
2541da177e4SLinus Torvalds 	dm_linear_exit,
2551da177e4SLinus Torvalds 	dm_stripe_exit,
256952b3557SMikulas Patocka 	dm_io_exit,
257945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
2581da177e4SLinus Torvalds 	dm_interface_exit,
259fd2ed4d2SMikulas Patocka 	dm_statistics_exit,
2601da177e4SLinus Torvalds };
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds static int __init dm_init(void)
2631da177e4SLinus Torvalds {
2641da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
2651da177e4SLinus Torvalds 	int r, i;
2661da177e4SLinus Torvalds 
267f1cd6cb2STushar Sugandhi #if (IS_ENABLED(CONFIG_IMA) && !IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE))
268f1cd6cb2STushar Sugandhi 	DMWARN("CONFIG_IMA_DISABLE_HTABLE is disabled."
269f1cd6cb2STushar Sugandhi 	       " Duplicate IMA measurements will not be recorded in the IMA log.");
270f1cd6cb2STushar Sugandhi #endif
271f1cd6cb2STushar Sugandhi 
2721da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
2731da177e4SLinus Torvalds 		r = _inits[i]();
2741da177e4SLinus Torvalds 		if (r)
2751da177e4SLinus Torvalds 			goto bad;
2761da177e4SLinus Torvalds 	}
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	return 0;
2791da177e4SLinus Torvalds bad:
2801da177e4SLinus Torvalds 	while (i--)
2811da177e4SLinus Torvalds 		_exits[i]();
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	return r;
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds static void __exit dm_exit(void)
2871da177e4SLinus Torvalds {
2881da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	while (i--)
2911da177e4SLinus Torvalds 		_exits[i]();
292d15b774cSAlasdair G Kergon 
293d15b774cSAlasdair G Kergon 	/*
294d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
295d15b774cSAlasdair G Kergon 	 */
296d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds /*
3001da177e4SLinus Torvalds  * Block device functions
3011da177e4SLinus Torvalds  */
302432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
303432a212cSMike Anderson {
304432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
305432a212cSMike Anderson }
306432a212cSMike Anderson 
307fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3081da177e4SLinus Torvalds {
3091da177e4SLinus Torvalds 	struct mapped_device *md;
3101da177e4SLinus Torvalds 
311fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
312fba9f90eSJeff Mahoney 
313fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
314fba9f90eSJeff Mahoney 	if (!md)
315fba9f90eSJeff Mahoney 		goto out;
316fba9f90eSJeff Mahoney 
3175c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
318432a212cSMike Anderson 	    dm_deleting_md(md)) {
319fba9f90eSJeff Mahoney 		md = NULL;
320fba9f90eSJeff Mahoney 		goto out;
321fba9f90eSJeff Mahoney 	}
322fba9f90eSJeff Mahoney 
3231da177e4SLinus Torvalds 	dm_get(md);
3245c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
325fba9f90eSJeff Mahoney out:
326fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
327fba9f90eSJeff Mahoney 
328fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3291da177e4SLinus Torvalds }
3301da177e4SLinus Torvalds 
331db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode)
3321da177e4SLinus Torvalds {
33363a4f065SMike Snitzer 	struct mapped_device *md;
3346e9624b8SArnd Bergmann 
3354a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
3364a1aeb98SMilan Broz 
33763a4f065SMike Snitzer 	md = disk->private_data;
33863a4f065SMike Snitzer 	if (WARN_ON(!md))
33963a4f065SMike Snitzer 		goto out;
34063a4f065SMike Snitzer 
3412c140a24SMikulas Patocka 	if (atomic_dec_and_test(&md->open_count) &&
3422c140a24SMikulas Patocka 	    (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
343acfe0ad7SMikulas Patocka 		queue_work(deferred_remove_workqueue, &deferred_remove_work);
3442c140a24SMikulas Patocka 
3451da177e4SLinus Torvalds 	dm_put(md);
34663a4f065SMike Snitzer out:
3474a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds 
3505c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3515c6bd75dSAlasdair G Kergon {
3525c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3535c6bd75dSAlasdair G Kergon }
3545c6bd75dSAlasdair G Kergon 
3555c6bd75dSAlasdair G Kergon /*
3565c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3575c6bd75dSAlasdair G Kergon  */
3582c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
3595c6bd75dSAlasdair G Kergon {
3605c6bd75dSAlasdair G Kergon 	int r = 0;
3615c6bd75dSAlasdair G Kergon 
3625c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
3635c6bd75dSAlasdair G Kergon 
3642c140a24SMikulas Patocka 	if (dm_open_count(md)) {
3655c6bd75dSAlasdair G Kergon 		r = -EBUSY;
3662c140a24SMikulas Patocka 		if (mark_deferred)
3672c140a24SMikulas Patocka 			set_bit(DMF_DEFERRED_REMOVE, &md->flags);
3682c140a24SMikulas Patocka 	} else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
3692c140a24SMikulas Patocka 		r = -EEXIST;
3705c6bd75dSAlasdair G Kergon 	else
3715c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
3725c6bd75dSAlasdair G Kergon 
3735c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
3745c6bd75dSAlasdair G Kergon 
3755c6bd75dSAlasdair G Kergon 	return r;
3765c6bd75dSAlasdair G Kergon }
3775c6bd75dSAlasdair G Kergon 
3782c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md)
3792c140a24SMikulas Patocka {
3802c140a24SMikulas Patocka 	int r = 0;
3812c140a24SMikulas Patocka 
3822c140a24SMikulas Patocka 	spin_lock(&_minor_lock);
3832c140a24SMikulas Patocka 
3842c140a24SMikulas Patocka 	if (test_bit(DMF_DELETING, &md->flags))
3852c140a24SMikulas Patocka 		r = -EBUSY;
3862c140a24SMikulas Patocka 	else
3872c140a24SMikulas Patocka 		clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
3882c140a24SMikulas Patocka 
3892c140a24SMikulas Patocka 	spin_unlock(&_minor_lock);
3902c140a24SMikulas Patocka 
3912c140a24SMikulas Patocka 	return r;
3922c140a24SMikulas Patocka }
3932c140a24SMikulas Patocka 
3942c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w)
3952c140a24SMikulas Patocka {
3962c140a24SMikulas Patocka 	dm_deferred_remove();
3972c140a24SMikulas Patocka }
3982c140a24SMikulas Patocka 
3993ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4003ac51e74SDarrick J. Wong {
4013ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
4023ac51e74SDarrick J. Wong 
4033ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
4043ac51e74SDarrick J. Wong }
4053ac51e74SDarrick J. Wong 
406971888c4SMike Snitzer static int dm_prepare_ioctl(struct mapped_device *md, int *srcu_idx,
4075bd5e8d8SMike Snitzer 			    struct block_device **bdev)
408aa129a22SMilan Broz {
40966482026SMike Snitzer 	struct dm_target *tgt;
4106c182cd8SHannes Reinecke 	struct dm_table *map;
411971888c4SMike Snitzer 	int r;
412aa129a22SMilan Broz 
4136c182cd8SHannes Reinecke retry:
414e56f81e0SChristoph Hellwig 	r = -ENOTTY;
415971888c4SMike Snitzer 	map = dm_get_live_table(md, srcu_idx);
416aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
417971888c4SMike Snitzer 		return r;
418aa129a22SMilan Broz 
419aa129a22SMilan Broz 	/* We only support devices that have a single target */
420aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
421971888c4SMike Snitzer 		return r;
422aa129a22SMilan Broz 
42366482026SMike Snitzer 	tgt = dm_table_get_target(map, 0);
42466482026SMike Snitzer 	if (!tgt->type->prepare_ioctl)
425e56f81e0SChristoph Hellwig 		return r;
426aa129a22SMilan Broz 
427971888c4SMike Snitzer 	if (dm_suspended_md(md))
428971888c4SMike Snitzer 		return -EAGAIN;
429971888c4SMike Snitzer 
4305bd5e8d8SMike Snitzer 	r = tgt->type->prepare_ioctl(tgt, bdev);
4315bbbfdf6SJunichi Nomura 	if (r == -ENOTCONN && !fatal_signal_pending(current)) {
432971888c4SMike Snitzer 		dm_put_live_table(md, *srcu_idx);
4336c182cd8SHannes Reinecke 		msleep(10);
4346c182cd8SHannes Reinecke 		goto retry;
4356c182cd8SHannes Reinecke 	}
436971888c4SMike Snitzer 
437e56f81e0SChristoph Hellwig 	return r;
438e56f81e0SChristoph Hellwig }
4396c182cd8SHannes Reinecke 
440971888c4SMike Snitzer static void dm_unprepare_ioctl(struct mapped_device *md, int srcu_idx)
441971888c4SMike Snitzer {
442971888c4SMike Snitzer 	dm_put_live_table(md, srcu_idx);
443971888c4SMike Snitzer }
444971888c4SMike Snitzer 
445e56f81e0SChristoph Hellwig static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
446e56f81e0SChristoph Hellwig 			unsigned int cmd, unsigned long arg)
447e56f81e0SChristoph Hellwig {
448e56f81e0SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
449971888c4SMike Snitzer 	int r, srcu_idx;
450e56f81e0SChristoph Hellwig 
4515bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
452e56f81e0SChristoph Hellwig 	if (r < 0)
453971888c4SMike Snitzer 		goto out;
454e56f81e0SChristoph Hellwig 
455e56f81e0SChristoph Hellwig 	if (r > 0) {
456e56f81e0SChristoph Hellwig 		/*
457e980f623SChristoph Hellwig 		 * Target determined this ioctl is being issued against a
458e980f623SChristoph Hellwig 		 * subset of the parent bdev; require extra privileges.
459e56f81e0SChristoph Hellwig 		 */
460e980f623SChristoph Hellwig 		if (!capable(CAP_SYS_RAWIO)) {
4610378c625SMike Snitzer 			DMDEBUG_LIMIT(
462e980f623SChristoph Hellwig 	"%s: sending ioctl %x to DM device without required privilege.",
463e980f623SChristoph Hellwig 				current->comm, cmd);
464e980f623SChristoph Hellwig 			r = -ENOIOCTLCMD;
465e56f81e0SChristoph Hellwig 			goto out;
466e56f81e0SChristoph Hellwig 		}
467e980f623SChristoph Hellwig 	}
468e56f81e0SChristoph Hellwig 
469a7cb3d2fSChristoph Hellwig 	if (!bdev->bd_disk->fops->ioctl)
470a7cb3d2fSChristoph Hellwig 		r = -ENOTTY;
471a7cb3d2fSChristoph Hellwig 	else
472a7cb3d2fSChristoph Hellwig 		r = bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg);
473e56f81e0SChristoph Hellwig out:
474971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
475aa129a22SMilan Broz 	return r;
476aa129a22SMilan Broz }
477aa129a22SMilan Broz 
4787465d7acSMike Snitzer u64 dm_start_time_ns_from_clone(struct bio *bio)
4797465d7acSMike Snitzer {
4807465d7acSMike Snitzer 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
4817465d7acSMike Snitzer 	struct dm_io *io = tio->io;
4827465d7acSMike Snitzer 
4837465d7acSMike Snitzer 	return jiffies_to_nsecs(io->start_time);
4847465d7acSMike Snitzer }
4857465d7acSMike Snitzer EXPORT_SYMBOL_GPL(dm_start_time_ns_from_clone);
4867465d7acSMike Snitzer 
4877465d7acSMike Snitzer static void start_io_acct(struct dm_io *io)
4887465d7acSMike Snitzer {
4897465d7acSMike Snitzer 	struct mapped_device *md = io->md;
4907465d7acSMike Snitzer 	struct bio *bio = io->orig_bio;
4917465d7acSMike Snitzer 
4927465d7acSMike Snitzer 	io->start_time = bio_start_io_acct(bio);
4937465d7acSMike Snitzer 	if (unlikely(dm_stats_used(&md->stats)))
4947465d7acSMike Snitzer 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
4957465d7acSMike Snitzer 				    bio->bi_iter.bi_sector, bio_sectors(bio),
4967465d7acSMike Snitzer 				    false, 0, &io->stats_aux);
4977465d7acSMike Snitzer }
4987465d7acSMike Snitzer 
499d208b894SJiazi Li static void end_io_acct(struct mapped_device *md, struct bio *bio,
500d208b894SJiazi Li 			unsigned long start_time, struct dm_stats_aux *stats_aux)
5017465d7acSMike Snitzer {
502d208b894SJiazi Li 	unsigned long duration = jiffies - start_time;
5037465d7acSMike Snitzer 
504d208b894SJiazi Li 	bio_end_io_acct(bio, start_time);
5057465d7acSMike Snitzer 
5067465d7acSMike Snitzer 	if (unlikely(dm_stats_used(&md->stats)))
5077465d7acSMike Snitzer 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
5087465d7acSMike Snitzer 				    bio->bi_iter.bi_sector, bio_sectors(bio),
509d208b894SJiazi Li 				    true, duration, stats_aux);
5107465d7acSMike Snitzer 
5117465d7acSMike Snitzer 	/* nudge anyone waiting on suspend queue */
5127465d7acSMike Snitzer 	if (unlikely(wq_has_sleeper(&md->wait)))
5137465d7acSMike Snitzer 		wake_up(&md->wait);
5147465d7acSMike Snitzer }
515978e51baSMike Snitzer 
516978e51baSMike Snitzer static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio)
5171da177e4SLinus Torvalds {
51864f52b0eSMike Snitzer 	struct dm_io *io;
51964f52b0eSMike Snitzer 	struct dm_target_io *tio;
52064f52b0eSMike Snitzer 	struct bio *clone;
52164f52b0eSMike Snitzer 
5226f1c819cSKent Overstreet 	clone = bio_alloc_bioset(GFP_NOIO, 0, &md->io_bs);
52364f52b0eSMike Snitzer 	if (!clone)
52464f52b0eSMike Snitzer 		return NULL;
52564f52b0eSMike Snitzer 
52664f52b0eSMike Snitzer 	tio = container_of(clone, struct dm_target_io, clone);
52764f52b0eSMike Snitzer 	tio->inside_dm_io = true;
52864f52b0eSMike Snitzer 	tio->io = NULL;
52964f52b0eSMike Snitzer 
53064f52b0eSMike Snitzer 	io = container_of(tio, struct dm_io, tio);
53164f52b0eSMike Snitzer 	io->magic = DM_IO_MAGIC;
532978e51baSMike Snitzer 	io->status = 0;
533978e51baSMike Snitzer 	atomic_set(&io->io_count, 1);
534978e51baSMike Snitzer 	io->orig_bio = bio;
535978e51baSMike Snitzer 	io->md = md;
536978e51baSMike Snitzer 	spin_lock_init(&io->endio_lock);
537978e51baSMike Snitzer 
538978e51baSMike Snitzer 	start_io_acct(io);
53964f52b0eSMike Snitzer 
54064f52b0eSMike Snitzer 	return io;
5411da177e4SLinus Torvalds }
5421da177e4SLinus Torvalds 
543028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
5441da177e4SLinus Torvalds {
54564f52b0eSMike Snitzer 	bio_put(&io->tio.clone);
54664f52b0eSMike Snitzer }
54764f52b0eSMike Snitzer 
54864f52b0eSMike Snitzer static struct dm_target_io *alloc_tio(struct clone_info *ci, struct dm_target *ti,
54964f52b0eSMike Snitzer 				      unsigned target_bio_nr, gfp_t gfp_mask)
55064f52b0eSMike Snitzer {
55164f52b0eSMike Snitzer 	struct dm_target_io *tio;
55264f52b0eSMike Snitzer 
55364f52b0eSMike Snitzer 	if (!ci->io->tio.io) {
55464f52b0eSMike Snitzer 		/* the dm_target_io embedded in ci->io is available */
55564f52b0eSMike Snitzer 		tio = &ci->io->tio;
55664f52b0eSMike Snitzer 	} else {
5576f1c819cSKent Overstreet 		struct bio *clone = bio_alloc_bioset(gfp_mask, 0, &ci->io->md->bs);
55864f52b0eSMike Snitzer 		if (!clone)
55964f52b0eSMike Snitzer 			return NULL;
56064f52b0eSMike Snitzer 
56164f52b0eSMike Snitzer 		tio = container_of(clone, struct dm_target_io, clone);
56264f52b0eSMike Snitzer 		tio->inside_dm_io = false;
56364f52b0eSMike Snitzer 	}
56464f52b0eSMike Snitzer 
56564f52b0eSMike Snitzer 	tio->magic = DM_TIO_MAGIC;
56664f52b0eSMike Snitzer 	tio->io = ci->io;
56764f52b0eSMike Snitzer 	tio->ti = ti;
56864f52b0eSMike Snitzer 	tio->target_bio_nr = target_bio_nr;
56964f52b0eSMike Snitzer 
57064f52b0eSMike Snitzer 	return tio;
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds 
573cfae7529SMike Snitzer static void free_tio(struct dm_target_io *tio)
5741da177e4SLinus Torvalds {
57564f52b0eSMike Snitzer 	if (tio->inside_dm_io)
57664f52b0eSMike Snitzer 		return;
577dba14160SMikulas Patocka 	bio_put(&tio->clone);
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds /*
5811da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
5821da177e4SLinus Torvalds  */
58392c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
5841da177e4SLinus Torvalds {
58505447420SKiyoshi Ueda 	unsigned long flags;
5861da177e4SLinus Torvalds 
58705447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
5881da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
58905447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
59092c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
5911da177e4SLinus Torvalds }
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds /*
5941da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
5951da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
59683d5e5b0SMikulas Patocka  * dm_put_live_table() when finished.
5971da177e4SLinus Torvalds  */
59883d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
5991da177e4SLinus Torvalds {
60083d5e5b0SMikulas Patocka 	*srcu_idx = srcu_read_lock(&md->io_barrier);
6011da177e4SLinus Torvalds 
60283d5e5b0SMikulas Patocka 	return srcu_dereference(md->map, &md->io_barrier);
60383d5e5b0SMikulas Patocka }
6041da177e4SLinus Torvalds 
60583d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
60683d5e5b0SMikulas Patocka {
60783d5e5b0SMikulas Patocka 	srcu_read_unlock(&md->io_barrier, srcu_idx);
60883d5e5b0SMikulas Patocka }
60983d5e5b0SMikulas Patocka 
61083d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md)
61183d5e5b0SMikulas Patocka {
61283d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
61383d5e5b0SMikulas Patocka 	synchronize_rcu_expedited();
61483d5e5b0SMikulas Patocka }
61583d5e5b0SMikulas Patocka 
61683d5e5b0SMikulas Patocka /*
61783d5e5b0SMikulas Patocka  * A fast alternative to dm_get_live_table/dm_put_live_table.
61883d5e5b0SMikulas Patocka  * The caller must not block between these two functions.
61983d5e5b0SMikulas Patocka  */
62083d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
62183d5e5b0SMikulas Patocka {
62283d5e5b0SMikulas Patocka 	rcu_read_lock();
62383d5e5b0SMikulas Patocka 	return rcu_dereference(md->map);
62483d5e5b0SMikulas Patocka }
62583d5e5b0SMikulas Patocka 
62683d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
62783d5e5b0SMikulas Patocka {
62883d5e5b0SMikulas Patocka 	rcu_read_unlock();
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds 
631971888c4SMike Snitzer static char *_dm_claim_ptr = "I belong to device-mapper";
632971888c4SMike Snitzer 
6333ac51e74SDarrick J. Wong /*
63486f1152bSBenjamin Marzinski  * Open a table device so we can use it as a map destination.
63586f1152bSBenjamin Marzinski  */
63686f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev,
63786f1152bSBenjamin Marzinski 			     struct mapped_device *md)
63886f1152bSBenjamin Marzinski {
63986f1152bSBenjamin Marzinski 	struct block_device *bdev;
64086f1152bSBenjamin Marzinski 
64186f1152bSBenjamin Marzinski 	int r;
64286f1152bSBenjamin Marzinski 
64386f1152bSBenjamin Marzinski 	BUG_ON(td->dm_dev.bdev);
64486f1152bSBenjamin Marzinski 
645519049afSMike Snitzer 	bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr);
64686f1152bSBenjamin Marzinski 	if (IS_ERR(bdev))
64786f1152bSBenjamin Marzinski 		return PTR_ERR(bdev);
64886f1152bSBenjamin Marzinski 
64986f1152bSBenjamin Marzinski 	r = bd_link_disk_holder(bdev, dm_disk(md));
65086f1152bSBenjamin Marzinski 	if (r) {
65186f1152bSBenjamin Marzinski 		blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
65286f1152bSBenjamin Marzinski 		return r;
65386f1152bSBenjamin Marzinski 	}
65486f1152bSBenjamin Marzinski 
65586f1152bSBenjamin Marzinski 	td->dm_dev.bdev = bdev;
656dfa584f6SChristoph Hellwig 	td->dm_dev.dax_dev = fs_dax_get_by_bdev(bdev);
65786f1152bSBenjamin Marzinski 	return 0;
65886f1152bSBenjamin Marzinski }
65986f1152bSBenjamin Marzinski 
66086f1152bSBenjamin Marzinski /*
66186f1152bSBenjamin Marzinski  * Close a table device that we've been using.
66286f1152bSBenjamin Marzinski  */
66386f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md)
66486f1152bSBenjamin Marzinski {
66586f1152bSBenjamin Marzinski 	if (!td->dm_dev.bdev)
66686f1152bSBenjamin Marzinski 		return;
66786f1152bSBenjamin Marzinski 
66886f1152bSBenjamin Marzinski 	bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
66986f1152bSBenjamin Marzinski 	blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
670817bf402SDan Williams 	put_dax(td->dm_dev.dax_dev);
67186f1152bSBenjamin Marzinski 	td->dm_dev.bdev = NULL;
672817bf402SDan Williams 	td->dm_dev.dax_dev = NULL;
67386f1152bSBenjamin Marzinski }
67486f1152bSBenjamin Marzinski 
67586f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev,
6768454fca4SSheetal Singala 					      fmode_t mode)
6778454fca4SSheetal Singala {
67886f1152bSBenjamin Marzinski 	struct table_device *td;
67986f1152bSBenjamin Marzinski 
68086f1152bSBenjamin Marzinski 	list_for_each_entry(td, l, list)
68186f1152bSBenjamin Marzinski 		if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
68286f1152bSBenjamin Marzinski 			return td;
68386f1152bSBenjamin Marzinski 
68486f1152bSBenjamin Marzinski 	return NULL;
68586f1152bSBenjamin Marzinski }
68686f1152bSBenjamin Marzinski 
68786f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
6888454fca4SSheetal Singala 			struct dm_dev **result)
6898454fca4SSheetal Singala {
69086f1152bSBenjamin Marzinski 	int r;
69186f1152bSBenjamin Marzinski 	struct table_device *td;
69286f1152bSBenjamin Marzinski 
69386f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
69486f1152bSBenjamin Marzinski 	td = find_table_device(&md->table_devices, dev, mode);
69586f1152bSBenjamin Marzinski 	if (!td) {
696115485e8SMike Snitzer 		td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
69786f1152bSBenjamin Marzinski 		if (!td) {
69886f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
69986f1152bSBenjamin Marzinski 			return -ENOMEM;
70086f1152bSBenjamin Marzinski 		}
70186f1152bSBenjamin Marzinski 
70286f1152bSBenjamin Marzinski 		td->dm_dev.mode = mode;
70386f1152bSBenjamin Marzinski 		td->dm_dev.bdev = NULL;
70486f1152bSBenjamin Marzinski 
70586f1152bSBenjamin Marzinski 		if ((r = open_table_device(td, dev, md))) {
70686f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
70786f1152bSBenjamin Marzinski 			kfree(td);
70886f1152bSBenjamin Marzinski 			return r;
70986f1152bSBenjamin Marzinski 		}
71086f1152bSBenjamin Marzinski 
71186f1152bSBenjamin Marzinski 		format_dev_t(td->dm_dev.name, dev);
71286f1152bSBenjamin Marzinski 
713b0b4d7c6SElena Reshetova 		refcount_set(&td->count, 1);
71486f1152bSBenjamin Marzinski 		list_add(&td->list, &md->table_devices);
715b0b4d7c6SElena Reshetova 	} else {
716b0b4d7c6SElena Reshetova 		refcount_inc(&td->count);
71786f1152bSBenjamin Marzinski 	}
71886f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
71986f1152bSBenjamin Marzinski 
72086f1152bSBenjamin Marzinski 	*result = &td->dm_dev;
72186f1152bSBenjamin Marzinski 	return 0;
72286f1152bSBenjamin Marzinski }
72386f1152bSBenjamin Marzinski 
72486f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
72586f1152bSBenjamin Marzinski {
72686f1152bSBenjamin Marzinski 	struct table_device *td = container_of(d, struct table_device, dm_dev);
72786f1152bSBenjamin Marzinski 
72886f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
729b0b4d7c6SElena Reshetova 	if (refcount_dec_and_test(&td->count)) {
73086f1152bSBenjamin Marzinski 		close_table_device(td, md);
73186f1152bSBenjamin Marzinski 		list_del(&td->list);
73286f1152bSBenjamin Marzinski 		kfree(td);
73386f1152bSBenjamin Marzinski 	}
73486f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
73586f1152bSBenjamin Marzinski }
73686f1152bSBenjamin Marzinski 
73786f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices)
73886f1152bSBenjamin Marzinski {
73986f1152bSBenjamin Marzinski 	struct list_head *tmp, *next;
74086f1152bSBenjamin Marzinski 
74186f1152bSBenjamin Marzinski 	list_for_each_safe(tmp, next, devices) {
74286f1152bSBenjamin Marzinski 		struct table_device *td = list_entry(tmp, struct table_device, list);
74386f1152bSBenjamin Marzinski 
74486f1152bSBenjamin Marzinski 		DMWARN("dm_destroy: %s still exists with %d references",
745b0b4d7c6SElena Reshetova 		       td->dm_dev.name, refcount_read(&td->count));
74686f1152bSBenjamin Marzinski 		kfree(td);
74786f1152bSBenjamin Marzinski 	}
74886f1152bSBenjamin Marzinski }
74986f1152bSBenjamin Marzinski 
75086f1152bSBenjamin Marzinski /*
7513ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
7523ac51e74SDarrick J. Wong  */
7533ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
7543ac51e74SDarrick J. Wong {
7553ac51e74SDarrick J. Wong 	*geo = md->geometry;
7563ac51e74SDarrick J. Wong 
7573ac51e74SDarrick J. Wong 	return 0;
7583ac51e74SDarrick J. Wong }
7593ac51e74SDarrick J. Wong 
7603ac51e74SDarrick J. Wong /*
7613ac51e74SDarrick J. Wong  * Set the geometry of a device.
7623ac51e74SDarrick J. Wong  */
7633ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
7643ac51e74SDarrick J. Wong {
7653ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
7663ac51e74SDarrick J. Wong 
7673ac51e74SDarrick J. Wong 	if (geo->start > sz) {
7683ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
7693ac51e74SDarrick J. Wong 		return -EINVAL;
7703ac51e74SDarrick J. Wong 	}
7713ac51e74SDarrick J. Wong 
7723ac51e74SDarrick J. Wong 	md->geometry = *geo;
7733ac51e74SDarrick J. Wong 
7743ac51e74SDarrick J. Wong 	return 0;
7753ac51e74SDarrick J. Wong }
7763ac51e74SDarrick J. Wong 
7772e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
7782e93ccc1SKiyoshi Ueda {
7792e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
7802e93ccc1SKiyoshi Ueda }
7812e93ccc1SKiyoshi Ueda 
7821da177e4SLinus Torvalds /*
7831da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
7841da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
7851da177e4SLinus Torvalds  */
786e2118b3cSDamien Le Moal void dm_io_dec_pending(struct dm_io *io, blk_status_t error)
7871da177e4SLinus Torvalds {
7882e93ccc1SKiyoshi Ueda 	unsigned long flags;
7894e4cbee9SChristoph Hellwig 	blk_status_t io_error;
790b35f8caaSMilan Broz 	struct bio *bio;
791b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
792d208b894SJiazi Li 	unsigned long start_time = 0;
793d208b894SJiazi Li 	struct dm_stats_aux stats_aux;
7942e93ccc1SKiyoshi Ueda 
7952e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
796f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
797f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
798745dc570SMike Snitzer 		if (!(io->status == BLK_STS_DM_REQUEUE && __noflush_suspending(md)))
7994e4cbee9SChristoph Hellwig 			io->status = error;
800f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
801f88fb981SKiyoshi Ueda 	}
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
804bf14e2b2SDamien Le Moal 		bio = io->orig_bio;
8054e4cbee9SChristoph Hellwig 		if (io->status == BLK_STS_DM_REQUEUE) {
8062e93ccc1SKiyoshi Ueda 			/*
8072e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
8082e93ccc1SKiyoshi Ueda 			 */
809022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
810bf14e2b2SDamien Le Moal 			if (__noflush_suspending(md) &&
811bf14e2b2SDamien Le Moal 			    !WARN_ON_ONCE(dm_is_zone_write(md, bio))) {
812745dc570SMike Snitzer 				/* NOTE early return due to BLK_STS_DM_REQUEUE below */
813bf14e2b2SDamien Le Moal 				bio_list_add_head(&md->deferred, bio);
814bf14e2b2SDamien Le Moal 			} else {
815bf14e2b2SDamien Le Moal 				/*
816bf14e2b2SDamien Le Moal 				 * noflush suspend was interrupted or this is
817bf14e2b2SDamien Le Moal 				 * a write to a zoned target.
818bf14e2b2SDamien Le Moal 				 */
8194e4cbee9SChristoph Hellwig 				io->status = BLK_STS_IOERR;
820bf14e2b2SDamien Le Moal 			}
821022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
8222e93ccc1SKiyoshi Ueda 		}
8232e93ccc1SKiyoshi Ueda 
8244e4cbee9SChristoph Hellwig 		io_error = io->status;
825d208b894SJiazi Li 		start_time = io->start_time;
826d208b894SJiazi Li 		stats_aux = io->stats_aux;
827a97f925aSMikulas Patocka 		free_io(md, io);
828d208b894SJiazi Li 		end_io_acct(md, bio, start_time, &stats_aux);
8291da177e4SLinus Torvalds 
8304e4cbee9SChristoph Hellwig 		if (io_error == BLK_STS_DM_REQUEUE)
8316a8736d1STejun Heo 			return;
8326a8736d1STejun Heo 
8331eff9d32SJens Axboe 		if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) {
8341da177e4SLinus Torvalds 			/*
8356a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
83628a8f0d3SMike Christie 			 * without REQ_PREFLUSH.
837af7e466aSMikulas Patocka 			 */
8381eff9d32SJens Axboe 			bio->bi_opf &= ~REQ_PREFLUSH;
8396a8736d1STejun Heo 			queue_io(md, bio);
840af7e466aSMikulas Patocka 		} else {
841b372d360SMike Snitzer 			/* done with normal IO or empty flush */
8428dd601faSNeilBrown 			if (io_error)
8434e4cbee9SChristoph Hellwig 				bio->bi_status = io_error;
8444246a0b6SChristoph Hellwig 			bio_endio(bio);
8452e93ccc1SKiyoshi Ueda 		}
8461da177e4SLinus Torvalds 	}
847af7e466aSMikulas Patocka }
8481da177e4SLinus Torvalds 
849bcb44433SMike Snitzer void disable_discard(struct mapped_device *md)
850bcb44433SMike Snitzer {
851bcb44433SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
852bcb44433SMike Snitzer 
853bcb44433SMike Snitzer 	/* device doesn't really support DISCARD, disable it */
854bcb44433SMike Snitzer 	limits->max_discard_sectors = 0;
855bcb44433SMike Snitzer 	blk_queue_flag_clear(QUEUE_FLAG_DISCARD, md->queue);
856bcb44433SMike Snitzer }
857bcb44433SMike Snitzer 
8584cc96131SMike Snitzer void disable_write_same(struct mapped_device *md)
8597eee4ae2SMike Snitzer {
8607eee4ae2SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
8617eee4ae2SMike Snitzer 
8627eee4ae2SMike Snitzer 	/* device doesn't really support WRITE SAME, disable it */
8637eee4ae2SMike Snitzer 	limits->max_write_same_sectors = 0;
8647eee4ae2SMike Snitzer }
8657eee4ae2SMike Snitzer 
866ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md)
867ac62d620SChristoph Hellwig {
868ac62d620SChristoph Hellwig 	struct queue_limits *limits = dm_get_queue_limits(md);
869ac62d620SChristoph Hellwig 
870ac62d620SChristoph Hellwig 	/* device doesn't really support WRITE ZEROES, disable it */
871ac62d620SChristoph Hellwig 	limits->max_write_zeroes_sectors = 0;
872ac62d620SChristoph Hellwig }
873ac62d620SChristoph Hellwig 
874a666e5c0SMikulas Patocka static bool swap_bios_limit(struct dm_target *ti, struct bio *bio)
875a666e5c0SMikulas Patocka {
876a666e5c0SMikulas Patocka 	return unlikely((bio->bi_opf & REQ_SWAP) != 0) && unlikely(ti->limit_swap_bios);
877a666e5c0SMikulas Patocka }
878a666e5c0SMikulas Patocka 
8794246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio)
8801da177e4SLinus Torvalds {
8814e4cbee9SChristoph Hellwig 	blk_status_t error = bio->bi_status;
882bfc6d41cSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
883b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
8849faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
8851da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
886309dca30SChristoph Hellwig 	struct request_queue *q = bio->bi_bdev->bd_disk->queue;
8871da177e4SLinus Torvalds 
8889c37de29SMike Snitzer 	if (unlikely(error == BLK_STS_TARGET)) {
889bcb44433SMike Snitzer 		if (bio_op(bio) == REQ_OP_DISCARD &&
890309dca30SChristoph Hellwig 		    !q->limits.max_discard_sectors)
891bcb44433SMike Snitzer 			disable_discard(md);
892bcb44433SMike Snitzer 		else if (bio_op(bio) == REQ_OP_WRITE_SAME &&
893309dca30SChristoph Hellwig 			 !q->limits.max_write_same_sectors)
8947eee4ae2SMike Snitzer 			disable_write_same(md);
895bcb44433SMike Snitzer 		else if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
896309dca30SChristoph Hellwig 			 !q->limits.max_write_zeroes_sectors)
897ac62d620SChristoph Hellwig 			disable_write_zeroes(md);
898ac62d620SChristoph Hellwig 	}
8997eee4ae2SMike Snitzer 
900bb37d772SDamien Le Moal 	if (blk_queue_is_zoned(q))
901bb37d772SDamien Le Moal 		dm_zone_endio(io, bio);
902415c79e1SJohannes Thumshirn 
9031be56909SChristoph Hellwig 	if (endio) {
9044e4cbee9SChristoph Hellwig 		int r = endio(tio->ti, bio, &error);
9051be56909SChristoph Hellwig 		switch (r) {
9061be56909SChristoph Hellwig 		case DM_ENDIO_REQUEUE:
907bf14e2b2SDamien Le Moal 			/*
908bf14e2b2SDamien Le Moal 			 * Requeuing writes to a sequential zone of a zoned
909bf14e2b2SDamien Le Moal 			 * target will break the sequential write pattern:
910bf14e2b2SDamien Le Moal 			 * fail such IO.
911bf14e2b2SDamien Le Moal 			 */
912bf14e2b2SDamien Le Moal 			if (WARN_ON_ONCE(dm_is_zone_write(md, bio)))
913bf14e2b2SDamien Le Moal 				error = BLK_STS_IOERR;
914bf14e2b2SDamien Le Moal 			else
9154e4cbee9SChristoph Hellwig 				error = BLK_STS_DM_REQUEUE;
916df561f66SGustavo A. R. Silva 			fallthrough;
9171be56909SChristoph Hellwig 		case DM_ENDIO_DONE:
9181be56909SChristoph Hellwig 			break;
9191be56909SChristoph Hellwig 		case DM_ENDIO_INCOMPLETE:
9201be56909SChristoph Hellwig 			/* The target will handle the io */
9211be56909SChristoph Hellwig 			return;
9221be56909SChristoph Hellwig 		default:
9231be56909SChristoph Hellwig 			DMWARN("unimplemented target endio return value: %d", r);
9241be56909SChristoph Hellwig 			BUG();
9251be56909SChristoph Hellwig 		}
9261be56909SChristoph Hellwig 	}
9271be56909SChristoph Hellwig 
928a666e5c0SMikulas Patocka 	if (unlikely(swap_bios_limit(tio->ti, bio))) {
929a666e5c0SMikulas Patocka 		struct mapped_device *md = io->md;
930a666e5c0SMikulas Patocka 		up(&md->swap_bios_semaphore);
931a666e5c0SMikulas Patocka 	}
932a666e5c0SMikulas Patocka 
933cfae7529SMike Snitzer 	free_tio(tio);
934e2118b3cSDamien Le Moal 	dm_io_dec_pending(io, error);
9351da177e4SLinus Torvalds }
9361da177e4SLinus Torvalds 
93778d8e58aSMike Snitzer /*
93856a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
93956a67df7SMike Snitzer  * target boundary.
94056a67df7SMike Snitzer  */
9413720281dSMike Snitzer static inline sector_t max_io_len_target_boundary(struct dm_target *ti,
9423720281dSMike Snitzer 						  sector_t target_offset)
9431da177e4SLinus Torvalds {
94456a67df7SMike Snitzer 	return ti->len - target_offset;
94556a67df7SMike Snitzer }
94656a67df7SMike Snitzer 
9473720281dSMike Snitzer static sector_t max_io_len(struct dm_target *ti, sector_t sector)
94856a67df7SMike Snitzer {
9493720281dSMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
9503720281dSMike Snitzer 	sector_t len = max_io_len_target_boundary(ti, target_offset);
9515091cdecSMike Snitzer 	sector_t max_len;
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	/*
9543ee16db3SMike Snitzer 	 * Does the target need to split IO even further?
9553ee16db3SMike Snitzer 	 * - varied (per target) IO splitting is a tenet of DM; this
9563ee16db3SMike Snitzer 	 *   explains why stacked chunk_sectors based splitting via
9573ee16db3SMike Snitzer 	 *   blk_max_size_offset() isn't possible here. So pass in
9583ee16db3SMike Snitzer 	 *   ti->max_io_len to override stacked chunk_sectors.
9591da177e4SLinus Torvalds 	 */
9603ee16db3SMike Snitzer 	if (ti->max_io_len) {
96133bd6f06SMike Snitzer 		max_len = blk_max_size_offset(ti->table->md->queue,
9623ee16db3SMike Snitzer 					      target_offset, ti->max_io_len);
963542f9038SMike Snitzer 		if (len > max_len)
964542f9038SMike Snitzer 			len = max_len;
9653ee16db3SMike Snitzer 	}
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	return len;
9681da177e4SLinus Torvalds }
9691da177e4SLinus Torvalds 
970542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
971542f9038SMike Snitzer {
972542f9038SMike Snitzer 	if (len > UINT_MAX) {
973542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
974542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
975542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
976542f9038SMike Snitzer 		return -EINVAL;
977542f9038SMike Snitzer 	}
978542f9038SMike Snitzer 
97975ae1936SMikulas Patocka 	ti->max_io_len = (uint32_t) len;
980542f9038SMike Snitzer 
981542f9038SMike Snitzer 	return 0;
982542f9038SMike Snitzer }
983542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
984542f9038SMike Snitzer 
985f26c5719SDan Williams static struct dm_target *dm_dax_get_live_target(struct mapped_device *md,
986f26c5719SDan Williams 						sector_t sector, int *srcu_idx)
9873d97c829SMike Snitzer 	__acquires(md->io_barrier)
988545ed20eSToshi Kani {
989545ed20eSToshi Kani 	struct dm_table *map;
990545ed20eSToshi Kani 	struct dm_target *ti;
991545ed20eSToshi Kani 
992f26c5719SDan Williams 	map = dm_get_live_table(md, srcu_idx);
993545ed20eSToshi Kani 	if (!map)
994f26c5719SDan Williams 		return NULL;
995545ed20eSToshi Kani 
996545ed20eSToshi Kani 	ti = dm_table_find_target(map, sector);
997123d87d5SMikulas Patocka 	if (!ti)
998f26c5719SDan Williams 		return NULL;
999f26c5719SDan Williams 
1000f26c5719SDan Williams 	return ti;
1001f26c5719SDan Williams }
1002f26c5719SDan Williams 
1003f26c5719SDan Williams static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
1004f26c5719SDan Williams 				 long nr_pages, void **kaddr, pfn_t *pfn)
1005f26c5719SDan Williams {
1006f26c5719SDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
1007f26c5719SDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
1008f26c5719SDan Williams 	struct dm_target *ti;
1009f26c5719SDan Williams 	long len, ret = -EIO;
1010f26c5719SDan Williams 	int srcu_idx;
1011f26c5719SDan Williams 
1012f26c5719SDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1013f26c5719SDan Williams 
1014f26c5719SDan Williams 	if (!ti)
1015545ed20eSToshi Kani 		goto out;
1016f26c5719SDan Williams 	if (!ti->type->direct_access)
1017f26c5719SDan Williams 		goto out;
10183720281dSMike Snitzer 	len = max_io_len(ti, sector) / PAGE_SECTORS;
1019f26c5719SDan Williams 	if (len < 1)
1020f26c5719SDan Williams 		goto out;
1021f26c5719SDan Williams 	nr_pages = min(len, nr_pages);
1022817bf402SDan Williams 	ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn);
1023817bf402SDan Williams 
1024545ed20eSToshi Kani  out:
1025545ed20eSToshi Kani 	dm_put_live_table(md, srcu_idx);
1026f26c5719SDan Williams 
1027f26c5719SDan Williams 	return ret;
1028545ed20eSToshi Kani }
1029545ed20eSToshi Kani 
10307bf7eac8SDan Williams static bool dm_dax_supported(struct dax_device *dax_dev, struct block_device *bdev,
10317bf7eac8SDan Williams 		int blocksize, sector_t start, sector_t len)
10327bf7eac8SDan Williams {
10337bf7eac8SDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
10347bf7eac8SDan Williams 	struct dm_table *map;
103502186d88SDan Williams 	bool ret = false;
10367bf7eac8SDan Williams 	int srcu_idx;
10377bf7eac8SDan Williams 
10387bf7eac8SDan Williams 	map = dm_get_live_table(md, &srcu_idx);
10397bf7eac8SDan Williams 	if (!map)
104002186d88SDan Williams 		goto out;
10417bf7eac8SDan Williams 
10425b0fab50SJeffle Xu 	ret = dm_table_supports_dax(map, device_not_dax_capable, &blocksize);
10437bf7eac8SDan Williams 
104402186d88SDan Williams out:
10457bf7eac8SDan Williams 	dm_put_live_table(md, srcu_idx);
10467bf7eac8SDan Williams 
10477bf7eac8SDan Williams 	return ret;
10487bf7eac8SDan Williams }
10497bf7eac8SDan Williams 
10507e026c8cSDan Williams static size_t dm_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
10517e026c8cSDan Williams 				    void *addr, size_t bytes, struct iov_iter *i)
10527e026c8cSDan Williams {
10537e026c8cSDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
10547e026c8cSDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
10557e026c8cSDan Williams 	struct dm_target *ti;
10567e026c8cSDan Williams 	long ret = 0;
10577e026c8cSDan Williams 	int srcu_idx;
10587e026c8cSDan Williams 
10597e026c8cSDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
10607e026c8cSDan Williams 
10617e026c8cSDan Williams 	if (!ti)
10627e026c8cSDan Williams 		goto out;
10637e026c8cSDan Williams 	if (!ti->type->dax_copy_from_iter) {
10647e026c8cSDan Williams 		ret = copy_from_iter(addr, bytes, i);
10657e026c8cSDan Williams 		goto out;
10667e026c8cSDan Williams 	}
10677e026c8cSDan Williams 	ret = ti->type->dax_copy_from_iter(ti, pgoff, addr, bytes, i);
10687e026c8cSDan Williams  out:
10697e026c8cSDan Williams 	dm_put_live_table(md, srcu_idx);
10707e026c8cSDan Williams 
10717e026c8cSDan Williams 	return ret;
10727e026c8cSDan Williams }
10737e026c8cSDan Williams 
1074b3a9a0c3SDan Williams static size_t dm_dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff,
1075b3a9a0c3SDan Williams 		void *addr, size_t bytes, struct iov_iter *i)
1076b3a9a0c3SDan Williams {
1077b3a9a0c3SDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
1078b3a9a0c3SDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
1079b3a9a0c3SDan Williams 	struct dm_target *ti;
1080b3a9a0c3SDan Williams 	long ret = 0;
1081b3a9a0c3SDan Williams 	int srcu_idx;
1082b3a9a0c3SDan Williams 
1083b3a9a0c3SDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1084b3a9a0c3SDan Williams 
1085b3a9a0c3SDan Williams 	if (!ti)
1086b3a9a0c3SDan Williams 		goto out;
1087b3a9a0c3SDan Williams 	if (!ti->type->dax_copy_to_iter) {
1088b3a9a0c3SDan Williams 		ret = copy_to_iter(addr, bytes, i);
1089b3a9a0c3SDan Williams 		goto out;
1090b3a9a0c3SDan Williams 	}
1091b3a9a0c3SDan Williams 	ret = ti->type->dax_copy_to_iter(ti, pgoff, addr, bytes, i);
1092b3a9a0c3SDan Williams  out:
1093b3a9a0c3SDan Williams 	dm_put_live_table(md, srcu_idx);
1094b3a9a0c3SDan Williams 
1095b3a9a0c3SDan Williams 	return ret;
1096b3a9a0c3SDan Williams }
1097b3a9a0c3SDan Williams 
1098cdf6cdcdSVivek Goyal static int dm_dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
1099cdf6cdcdSVivek Goyal 				  size_t nr_pages)
1100cdf6cdcdSVivek Goyal {
1101cdf6cdcdSVivek Goyal 	struct mapped_device *md = dax_get_private(dax_dev);
1102cdf6cdcdSVivek Goyal 	sector_t sector = pgoff * PAGE_SECTORS;
1103cdf6cdcdSVivek Goyal 	struct dm_target *ti;
1104cdf6cdcdSVivek Goyal 	int ret = -EIO;
1105cdf6cdcdSVivek Goyal 	int srcu_idx;
1106cdf6cdcdSVivek Goyal 
1107cdf6cdcdSVivek Goyal 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1108cdf6cdcdSVivek Goyal 
1109cdf6cdcdSVivek Goyal 	if (!ti)
1110cdf6cdcdSVivek Goyal 		goto out;
1111cdf6cdcdSVivek Goyal 	if (WARN_ON(!ti->type->dax_zero_page_range)) {
1112cdf6cdcdSVivek Goyal 		/*
1113cdf6cdcdSVivek Goyal 		 * ->zero_page_range() is mandatory dax operation. If we are
1114cdf6cdcdSVivek Goyal 		 *  here, something is wrong.
1115cdf6cdcdSVivek Goyal 		 */
1116cdf6cdcdSVivek Goyal 		goto out;
1117cdf6cdcdSVivek Goyal 	}
1118cdf6cdcdSVivek Goyal 	ret = ti->type->dax_zero_page_range(ti, pgoff, nr_pages);
1119cdf6cdcdSVivek Goyal  out:
1120cdf6cdcdSVivek Goyal 	dm_put_live_table(md, srcu_idx);
1121cdf6cdcdSVivek Goyal 
1122cdf6cdcdSVivek Goyal 	return ret;
1123cdf6cdcdSVivek Goyal }
1124cdf6cdcdSVivek Goyal 
11251dd40c3eSMikulas Patocka /*
11261dd40c3eSMikulas Patocka  * A target may call dm_accept_partial_bio only from the map routine.  It is
11276842d264SDamien Le Moal  * allowed for all bio types except REQ_PREFLUSH, REQ_OP_ZONE_* zone management
11286842d264SDamien Le Moal  * operations and REQ_OP_ZONE_APPEND (zone append writes).
11291dd40c3eSMikulas Patocka  *
11301dd40c3eSMikulas Patocka  * dm_accept_partial_bio informs the dm that the target only wants to process
11311dd40c3eSMikulas Patocka  * additional n_sectors sectors of the bio and the rest of the data should be
11321dd40c3eSMikulas Patocka  * sent in a next bio.
11331dd40c3eSMikulas Patocka  *
11341dd40c3eSMikulas Patocka  * A diagram that explains the arithmetics:
11351dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
11361dd40c3eSMikulas Patocka  * |         1          |       2       |   3   |
11371dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
11381dd40c3eSMikulas Patocka  *
11391dd40c3eSMikulas Patocka  * <-------------- *tio->len_ptr --------------->
11401dd40c3eSMikulas Patocka  *                      <------- bi_size ------->
11411dd40c3eSMikulas Patocka  *                      <-- n_sectors -->
11421dd40c3eSMikulas Patocka  *
11431dd40c3eSMikulas Patocka  * Region 1 was already iterated over with bio_advance or similar function.
11441dd40c3eSMikulas Patocka  *	(it may be empty if the target doesn't use bio_advance)
11451dd40c3eSMikulas Patocka  * Region 2 is the remaining bio size that the target wants to process.
11461dd40c3eSMikulas Patocka  *	(it may be empty if region 1 is non-empty, although there is no reason
11471dd40c3eSMikulas Patocka  *	 to make it empty)
11481dd40c3eSMikulas Patocka  * The target requires that region 3 is to be sent in the next bio.
11491dd40c3eSMikulas Patocka  *
11501dd40c3eSMikulas Patocka  * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
11511dd40c3eSMikulas Patocka  * the partially processed part (the sum of regions 1+2) must be the same for all
11521dd40c3eSMikulas Patocka  * copies of the bio.
11531dd40c3eSMikulas Patocka  */
11541dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
11551dd40c3eSMikulas Patocka {
11561dd40c3eSMikulas Patocka 	struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
11571dd40c3eSMikulas Patocka 	unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
11586842d264SDamien Le Moal 
11591eff9d32SJens Axboe 	BUG_ON(bio->bi_opf & REQ_PREFLUSH);
11606842d264SDamien Le Moal 	BUG_ON(op_is_zone_mgmt(bio_op(bio)));
11616842d264SDamien Le Moal 	BUG_ON(bio_op(bio) == REQ_OP_ZONE_APPEND);
11621dd40c3eSMikulas Patocka 	BUG_ON(bi_size > *tio->len_ptr);
11631dd40c3eSMikulas Patocka 	BUG_ON(n_sectors > bi_size);
11646842d264SDamien Le Moal 
11651dd40c3eSMikulas Patocka 	*tio->len_ptr -= bi_size - n_sectors;
11661dd40c3eSMikulas Patocka 	bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
11671dd40c3eSMikulas Patocka }
11681dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
11691dd40c3eSMikulas Patocka 
1170a666e5c0SMikulas Patocka static noinline void __set_swap_bios_limit(struct mapped_device *md, int latch)
1171a666e5c0SMikulas Patocka {
1172a666e5c0SMikulas Patocka 	mutex_lock(&md->swap_bios_lock);
1173a666e5c0SMikulas Patocka 	while (latch < md->swap_bios) {
1174a666e5c0SMikulas Patocka 		cond_resched();
1175a666e5c0SMikulas Patocka 		down(&md->swap_bios_semaphore);
1176a666e5c0SMikulas Patocka 		md->swap_bios--;
1177a666e5c0SMikulas Patocka 	}
1178a666e5c0SMikulas Patocka 	while (latch > md->swap_bios) {
1179a666e5c0SMikulas Patocka 		cond_resched();
1180a666e5c0SMikulas Patocka 		up(&md->swap_bios_semaphore);
1181a666e5c0SMikulas Patocka 		md->swap_bios++;
1182a666e5c0SMikulas Patocka 	}
1183a666e5c0SMikulas Patocka 	mutex_unlock(&md->swap_bios_lock);
1184a666e5c0SMikulas Patocka }
1185a666e5c0SMikulas Patocka 
11863e08773cSChristoph Hellwig static void __map_bio(struct dm_target_io *tio)
11871da177e4SLinus Torvalds {
11881da177e4SLinus Torvalds 	int r;
11892056a782SJens Axboe 	sector_t sector;
1190dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
119164f52b0eSMike Snitzer 	struct dm_io *io = tio->io;
1192bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	/*
11971da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
11981da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
11991da177e4SLinus Torvalds 	 * this io.
12001da177e4SLinus Torvalds 	 */
1201e2118b3cSDamien Le Moal 	dm_io_inc_pending(io);
12024f024f37SKent Overstreet 	sector = clone->bi_iter.bi_sector;
1203d67a5f4bSMikulas Patocka 
1204a666e5c0SMikulas Patocka 	if (unlikely(swap_bios_limit(ti, clone))) {
1205a666e5c0SMikulas Patocka 		struct mapped_device *md = io->md;
1206a666e5c0SMikulas Patocka 		int latch = get_swap_bios();
1207a666e5c0SMikulas Patocka 		if (unlikely(latch != md->swap_bios))
1208a666e5c0SMikulas Patocka 			__set_swap_bios_limit(md, latch);
1209a666e5c0SMikulas Patocka 		down(&md->swap_bios_semaphore);
1210a666e5c0SMikulas Patocka 	}
1211a666e5c0SMikulas Patocka 
1212bb37d772SDamien Le Moal 	/*
1213bb37d772SDamien Le Moal 	 * Check if the IO needs a special mapping due to zone append emulation
1214bb37d772SDamien Le Moal 	 * on zoned target. In this case, dm_zone_map_bio() calls the target
1215bb37d772SDamien Le Moal 	 * map operation.
1216bb37d772SDamien Le Moal 	 */
1217bb37d772SDamien Le Moal 	if (dm_emulate_zone_append(io->md))
1218bb37d772SDamien Le Moal 		r = dm_zone_map_bio(tio);
1219bb37d772SDamien Le Moal 	else
12207de3ee57SMikulas Patocka 		r = ti->type->map(ti, clone);
1221bb37d772SDamien Le Moal 
1222846785e6SChristoph Hellwig 	switch (r) {
1223846785e6SChristoph Hellwig 	case DM_MAPIO_SUBMITTED:
1224846785e6SChristoph Hellwig 		break;
1225846785e6SChristoph Hellwig 	case DM_MAPIO_REMAPPED:
12261da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
12271c02fca6SChristoph Hellwig 		trace_block_bio_remap(clone, bio_dev(io->orig_bio), sector);
12283e08773cSChristoph Hellwig 		submit_bio_noacct(clone);
1229846785e6SChristoph Hellwig 		break;
1230846785e6SChristoph Hellwig 	case DM_MAPIO_KILL:
1231a666e5c0SMikulas Patocka 		if (unlikely(swap_bios_limit(ti, clone))) {
1232a666e5c0SMikulas Patocka 			struct mapped_device *md = io->md;
1233a666e5c0SMikulas Patocka 			up(&md->swap_bios_semaphore);
1234a666e5c0SMikulas Patocka 		}
12354e4cbee9SChristoph Hellwig 		free_tio(tio);
1236e2118b3cSDamien Le Moal 		dm_io_dec_pending(io, BLK_STS_IOERR);
12374e4cbee9SChristoph Hellwig 		break;
1238846785e6SChristoph Hellwig 	case DM_MAPIO_REQUEUE:
1239a666e5c0SMikulas Patocka 		if (unlikely(swap_bios_limit(ti, clone))) {
1240a666e5c0SMikulas Patocka 			struct mapped_device *md = io->md;
1241a666e5c0SMikulas Patocka 			up(&md->swap_bios_semaphore);
1242a666e5c0SMikulas Patocka 		}
1243cfae7529SMike Snitzer 		free_tio(tio);
1244e2118b3cSDamien Le Moal 		dm_io_dec_pending(io, BLK_STS_DM_REQUEUE);
1245846785e6SChristoph Hellwig 		break;
1246846785e6SChristoph Hellwig 	default:
124745cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
124845cbcd79SKiyoshi Ueda 		BUG();
12491da177e4SLinus Torvalds 	}
12501da177e4SLinus Torvalds }
12511da177e4SLinus Torvalds 
1252e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
1253bd2a49b8SAlasdair G Kergon {
12544f024f37SKent Overstreet 	bio->bi_iter.bi_sector = sector;
12554f024f37SKent Overstreet 	bio->bi_iter.bi_size = to_bytes(len);
12561da177e4SLinus Torvalds }
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds /*
12591da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
12601da177e4SLinus Torvalds  */
1261c80914e8SMike Snitzer static int clone_bio(struct dm_target_io *tio, struct bio *bio,
12621c3b13e6SKent Overstreet 		     sector_t sector, unsigned len)
12631da177e4SLinus Torvalds {
1264dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
126507560151SEric Biggers 	int r;
12661da177e4SLinus Torvalds 
12671c3b13e6SKent Overstreet 	__bio_clone_fast(clone, bio);
12689c47008dSMartin K. Petersen 
126907560151SEric Biggers 	r = bio_crypt_clone(clone, bio, GFP_NOIO);
127007560151SEric Biggers 	if (r < 0)
127107560151SEric Biggers 		return r;
1272a892c8d5SSatya Tangirala 
127357c36519SMike Snitzer 	if (bio_integrity(bio)) {
1274e2460f2aSMikulas Patocka 		if (unlikely(!dm_target_has_integrity(tio->ti->type) &&
1275e2460f2aSMikulas Patocka 			     !dm_target_passes_integrity(tio->ti->type))) {
1276e2460f2aSMikulas Patocka 			DMWARN("%s: the target %s doesn't support integrity data.",
1277e2460f2aSMikulas Patocka 				dm_device_name(tio->io->md),
1278e2460f2aSMikulas Patocka 				tio->ti->type->name);
1279e2460f2aSMikulas Patocka 			return -EIO;
1280e2460f2aSMikulas Patocka 		}
1281e2460f2aSMikulas Patocka 
1282e2460f2aSMikulas Patocka 		r = bio_integrity_clone(clone, bio, GFP_NOIO);
1283c80914e8SMike Snitzer 		if (r < 0)
1284c80914e8SMike Snitzer 			return r;
1285c80914e8SMike Snitzer 	}
12861c3b13e6SKent Overstreet 
1287fa8db494SMike Snitzer 	bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
1288fa8db494SMike Snitzer 	clone->bi_iter.bi_size = to_bytes(len);
1289fa8db494SMike Snitzer 
1290fa8db494SMike Snitzer 	if (bio_integrity(bio))
1291fa8db494SMike Snitzer 		bio_integrity_trim(clone);
1292c80914e8SMike Snitzer 
1293c80914e8SMike Snitzer 	return 0;
12941da177e4SLinus Torvalds }
12951da177e4SLinus Torvalds 
1296318716ddSMike Snitzer static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci,
1297318716ddSMike Snitzer 				struct dm_target *ti, unsigned num_bios)
1298f9ab94ceSMikulas Patocka {
1299dba14160SMikulas Patocka 	struct dm_target_io *tio;
1300318716ddSMike Snitzer 	int try;
1301dba14160SMikulas Patocka 
1302318716ddSMike Snitzer 	if (!num_bios)
1303318716ddSMike Snitzer 		return;
1304f9ab94ceSMikulas Patocka 
1305318716ddSMike Snitzer 	if (num_bios == 1) {
1306318716ddSMike Snitzer 		tio = alloc_tio(ci, ti, 0, GFP_NOIO);
1307318716ddSMike Snitzer 		bio_list_add(blist, &tio->clone);
1308318716ddSMike Snitzer 		return;
13099015df24SAlasdair G Kergon 	}
13109015df24SAlasdair G Kergon 
1311318716ddSMike Snitzer 	for (try = 0; try < 2; try++) {
1312318716ddSMike Snitzer 		int bio_nr;
1313318716ddSMike Snitzer 		struct bio *bio;
1314318716ddSMike Snitzer 
1315318716ddSMike Snitzer 		if (try)
1316bc02cdbeSMike Snitzer 			mutex_lock(&ci->io->md->table_devices_lock);
1317318716ddSMike Snitzer 		for (bio_nr = 0; bio_nr < num_bios; bio_nr++) {
1318318716ddSMike Snitzer 			tio = alloc_tio(ci, ti, bio_nr, try ? GFP_NOIO : GFP_NOWAIT);
1319318716ddSMike Snitzer 			if (!tio)
1320318716ddSMike Snitzer 				break;
1321318716ddSMike Snitzer 
1322318716ddSMike Snitzer 			bio_list_add(blist, &tio->clone);
1323318716ddSMike Snitzer 		}
1324318716ddSMike Snitzer 		if (try)
1325bc02cdbeSMike Snitzer 			mutex_unlock(&ci->io->md->table_devices_lock);
1326318716ddSMike Snitzer 		if (bio_nr == num_bios)
1327318716ddSMike Snitzer 			return;
1328318716ddSMike Snitzer 
1329318716ddSMike Snitzer 		while ((bio = bio_list_pop(blist))) {
1330318716ddSMike Snitzer 			tio = container_of(bio, struct dm_target_io, clone);
1331318716ddSMike Snitzer 			free_tio(tio);
1332318716ddSMike Snitzer 		}
1333318716ddSMike Snitzer 	}
1334318716ddSMike Snitzer }
1335318716ddSMike Snitzer 
13363e08773cSChristoph Hellwig static void __clone_and_map_simple_bio(struct clone_info *ci,
1337318716ddSMike Snitzer 					   struct dm_target_io *tio, unsigned *len)
13389015df24SAlasdair G Kergon {
1339dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
13409015df24SAlasdair G Kergon 
13411dd40c3eSMikulas Patocka 	tio->len_ptr = len;
13421dd40c3eSMikulas Patocka 
13431c3b13e6SKent Overstreet 	__bio_clone_fast(clone, ci->bio);
1344bd2a49b8SAlasdair G Kergon 	if (len)
13451dd40c3eSMikulas Patocka 		bio_setup_sector(clone, ci->sector, *len);
13463e08773cSChristoph Hellwig 	__map_bio(tio);
1347f9ab94ceSMikulas Patocka }
1348f9ab94ceSMikulas Patocka 
134914fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
13501dd40c3eSMikulas Patocka 				  unsigned num_bios, unsigned *len)
135106a426ceSMike Snitzer {
1352318716ddSMike Snitzer 	struct bio_list blist = BIO_EMPTY_LIST;
1353318716ddSMike Snitzer 	struct bio *bio;
1354318716ddSMike Snitzer 	struct dm_target_io *tio;
135506a426ceSMike Snitzer 
1356318716ddSMike Snitzer 	alloc_multiple_bios(&blist, ci, ti, num_bios);
1357318716ddSMike Snitzer 
1358318716ddSMike Snitzer 	while ((bio = bio_list_pop(&blist))) {
1359318716ddSMike Snitzer 		tio = container_of(bio, struct dm_target_io, clone);
13603e08773cSChristoph Hellwig 		__clone_and_map_simple_bio(ci, tio, len);
1361318716ddSMike Snitzer 	}
136206a426ceSMike Snitzer }
136306a426ceSMike Snitzer 
136414fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1365f9ab94ceSMikulas Patocka {
136606a426ceSMike Snitzer 	unsigned target_nr = 0;
1367f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1368828678b8SMike Snitzer 	struct bio flush_bio;
1369828678b8SMike Snitzer 
1370828678b8SMike Snitzer 	/*
1371828678b8SMike Snitzer 	 * Use an on-stack bio for this, it's safe since we don't
1372828678b8SMike Snitzer 	 * need to reference it after submit. It's just used as
1373828678b8SMike Snitzer 	 * the basis for the clone(s).
1374828678b8SMike Snitzer 	 */
1375828678b8SMike Snitzer 	bio_init(&flush_bio, NULL, 0);
1376828678b8SMike Snitzer 	flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
1377309dca30SChristoph Hellwig 	bio_set_dev(&flush_bio, ci->io->md->disk->part0);
137847d95102SChristoph Hellwig 
1379828678b8SMike Snitzer 	ci->bio = &flush_bio;
1380828678b8SMike Snitzer 	ci->sector_count = 0;
1381f9ab94ceSMikulas Patocka 
1382b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1383f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
13841dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
1385828678b8SMike Snitzer 
1386828678b8SMike Snitzer 	bio_uninit(ci->bio);
1387f9ab94ceSMikulas Patocka 	return 0;
1388f9ab94ceSMikulas Patocka }
1389f9ab94ceSMikulas Patocka 
1390c80914e8SMike Snitzer static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
13911dd40c3eSMikulas Patocka 				    sector_t sector, unsigned *len)
13925ae89a87SMike Snitzer {
1393dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
13945ae89a87SMike Snitzer 	struct dm_target_io *tio;
1395f31c21e4SNeilBrown 	int r;
13965ae89a87SMike Snitzer 
1397318716ddSMike Snitzer 	tio = alloc_tio(ci, ti, 0, GFP_NOIO);
13981dd40c3eSMikulas Patocka 	tio->len_ptr = len;
1399c80914e8SMike Snitzer 	r = clone_bio(tio, bio, sector, *len);
1400072623deSMikulas Patocka 	if (r < 0) {
1401cfae7529SMike Snitzer 		free_tio(tio);
1402c80914e8SMike Snitzer 		return r;
1403b0d8ed4dSAlasdair G Kergon 	}
14043e08773cSChristoph Hellwig 	__map_bio(tio);
140555a62eefSAlasdair G Kergon 
1406f31c21e4SNeilBrown 	return 0;
140723508a96SMike Snitzer }
140855a62eefSAlasdair G Kergon 
14093d7f4562SMike Snitzer static int __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti,
141061697a6aSMike Snitzer 				       unsigned num_bios)
14115ae89a87SMike Snitzer {
141251b86f9aSMichael Lass 	unsigned len;
14135ae89a87SMike Snitzer 
14145ae89a87SMike Snitzer 	/*
141523508a96SMike Snitzer 	 * Even though the device advertised support for this type of
141623508a96SMike Snitzer 	 * request, that does not mean every target supports it, and
1417936688d7SMike Snitzer 	 * reconfiguration might also have changed that since the
14185ae89a87SMike Snitzer 	 * check was performed.
14195ae89a87SMike Snitzer 	 */
142055a62eefSAlasdair G Kergon 	if (!num_bios)
14215ae89a87SMike Snitzer 		return -EOPNOTSUPP;
14225ae89a87SMike Snitzer 
14233720281dSMike Snitzer 	len = min_t(sector_t, ci->sector_count,
14243720281dSMike Snitzer 		    max_io_len_target_boundary(ti, dm_target_offset(ti, ci->sector)));
142551b86f9aSMichael Lass 
14261dd40c3eSMikulas Patocka 	__send_duplicate_bios(ci, ti, num_bios, &len);
14275ae89a87SMike Snitzer 
1428a79245b3SMike Snitzer 	ci->sector += len;
14293d7f4562SMike Snitzer 	ci->sector_count -= len;
14305ae89a87SMike Snitzer 
14315ae89a87SMike Snitzer 	return 0;
14325ae89a87SMike Snitzer }
14335ae89a87SMike Snitzer 
1434568c73a3SMike Snitzer static bool is_abnormal_io(struct bio *bio)
1435568c73a3SMike Snitzer {
1436568c73a3SMike Snitzer 	bool r = false;
1437568c73a3SMike Snitzer 
1438568c73a3SMike Snitzer 	switch (bio_op(bio)) {
1439568c73a3SMike Snitzer 	case REQ_OP_DISCARD:
1440568c73a3SMike Snitzer 	case REQ_OP_SECURE_ERASE:
1441568c73a3SMike Snitzer 	case REQ_OP_WRITE_SAME:
1442568c73a3SMike Snitzer 	case REQ_OP_WRITE_ZEROES:
1443568c73a3SMike Snitzer 		r = true;
1444568c73a3SMike Snitzer 		break;
1445568c73a3SMike Snitzer 	}
1446568c73a3SMike Snitzer 
1447568c73a3SMike Snitzer 	return r;
1448568c73a3SMike Snitzer }
1449568c73a3SMike Snitzer 
14500519c71eSMike Snitzer static bool __process_abnormal_io(struct clone_info *ci, struct dm_target *ti,
14510519c71eSMike Snitzer 				  int *result)
14520519c71eSMike Snitzer {
14530519c71eSMike Snitzer 	struct bio *bio = ci->bio;
14549679b5a7SMike Snitzer 	unsigned num_bios = 0;
14550519c71eSMike Snitzer 
14569679b5a7SMike Snitzer 	switch (bio_op(bio)) {
14579679b5a7SMike Snitzer 	case REQ_OP_DISCARD:
14589679b5a7SMike Snitzer 		num_bios = ti->num_discard_bios;
14599679b5a7SMike Snitzer 		break;
14609679b5a7SMike Snitzer 	case REQ_OP_SECURE_ERASE:
14619679b5a7SMike Snitzer 		num_bios = ti->num_secure_erase_bios;
14629679b5a7SMike Snitzer 		break;
14639679b5a7SMike Snitzer 	case REQ_OP_WRITE_SAME:
14649679b5a7SMike Snitzer 		num_bios = ti->num_write_same_bios;
14659679b5a7SMike Snitzer 		break;
14669679b5a7SMike Snitzer 	case REQ_OP_WRITE_ZEROES:
14679679b5a7SMike Snitzer 		num_bios = ti->num_write_zeroes_bios;
14689679b5a7SMike Snitzer 		break;
14699679b5a7SMike Snitzer 	default:
14700519c71eSMike Snitzer 		return false;
14719679b5a7SMike Snitzer 	}
14720519c71eSMike Snitzer 
14739679b5a7SMike Snitzer 	*result = __send_changing_extent_only(ci, ti, num_bios);
14740519c71eSMike Snitzer 	return true;
14750519c71eSMike Snitzer }
14760519c71eSMike Snitzer 
1477e4c93811SAlasdair G Kergon /*
1478e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1479e4c93811SAlasdair G Kergon  */
1480e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci)
1481e4c93811SAlasdair G Kergon {
1482e4c93811SAlasdair G Kergon 	struct dm_target *ti;
14831c3b13e6SKent Overstreet 	unsigned len;
1484c80914e8SMike Snitzer 	int r;
1485e4c93811SAlasdair G Kergon 
1486e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1487123d87d5SMikulas Patocka 	if (!ti)
1488e4c93811SAlasdair G Kergon 		return -EIO;
1489e4c93811SAlasdair G Kergon 
1490568c73a3SMike Snitzer 	if (__process_abnormal_io(ci, ti, &r))
14910519c71eSMike Snitzer 		return r;
14923d7f4562SMike Snitzer 
14933720281dSMike Snitzer 	len = min_t(sector_t, max_io_len(ti, ci->sector), ci->sector_count);
1494e4c93811SAlasdair G Kergon 
1495c80914e8SMike Snitzer 	r = __clone_and_map_data_bio(ci, ti, ci->sector, &len);
1496c80914e8SMike Snitzer 	if (r < 0)
1497c80914e8SMike Snitzer 		return r;
1498e4c93811SAlasdair G Kergon 
1499e4c93811SAlasdair G Kergon 	ci->sector += len;
1500e4c93811SAlasdair G Kergon 	ci->sector_count -= len;
1501e4c93811SAlasdair G Kergon 
1502e4c93811SAlasdair G Kergon 	return 0;
1503e4c93811SAlasdair G Kergon }
1504e4c93811SAlasdair G Kergon 
1505978e51baSMike Snitzer static void init_clone_info(struct clone_info *ci, struct mapped_device *md,
1506978e51baSMike Snitzer 			    struct dm_table *map, struct bio *bio)
1507978e51baSMike Snitzer {
1508978e51baSMike Snitzer 	ci->map = map;
1509978e51baSMike Snitzer 	ci->io = alloc_io(md, bio);
1510978e51baSMike Snitzer 	ci->sector = bio->bi_iter.bi_sector;
1511978e51baSMike Snitzer }
1512978e51baSMike Snitzer 
1513a1e1cb72SMike Snitzer #define __dm_part_stat_sub(part, field, subnd)	\
1514a1e1cb72SMike Snitzer 	(part_stat_get(part, field) -= (subnd))
1515a1e1cb72SMike Snitzer 
1516e4c93811SAlasdair G Kergon /*
151714fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
15181da177e4SLinus Torvalds  */
15193e08773cSChristoph Hellwig static void __split_and_process_bio(struct mapped_device *md,
152083d5e5b0SMikulas Patocka 					struct dm_table *map, struct bio *bio)
15211da177e4SLinus Torvalds {
15221da177e4SLinus Torvalds 	struct clone_info ci;
1523512875bdSJun'ichi Nomura 	int error = 0;
15241da177e4SLinus Torvalds 
1525978e51baSMike Snitzer 	init_clone_info(&ci, md, map, bio);
1526bd2a49b8SAlasdair G Kergon 
15271eff9d32SJens Axboe 	if (bio->bi_opf & REQ_PREFLUSH) {
152814fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1529e2118b3cSDamien Le Moal 		/* dm_io_dec_pending submits any data associated with flush */
15302e2d6f7eSAjay Joshi 	} else if (op_is_zone_mgmt(bio_op(bio))) {
1531a4aa5e56SDamien Le Moal 		ci.bio = bio;
1532a4aa5e56SDamien Le Moal 		ci.sector_count = 0;
1533a4aa5e56SDamien Le Moal 		error = __split_and_process_non_flush(&ci);
1534b372d360SMike Snitzer 	} else {
15356a8736d1STejun Heo 		ci.bio = bio;
15361da177e4SLinus Torvalds 		ci.sector_count = bio_sectors(bio);
153714fe594dSAlasdair G Kergon 		error = __split_and_process_non_flush(&ci);
1538985eabdcSJeffle Xu 		if (ci.sector_count && !error) {
153918a25da8SNeilBrown 			/*
1540ed00aabdSChristoph Hellwig 			 * Remainder must be passed to submit_bio_noacct()
154118a25da8SNeilBrown 			 * so that it gets handled *after* bios already submitted
154218a25da8SNeilBrown 			 * have been completely processed.
154318a25da8SNeilBrown 			 * We take a clone of the original to store in
1544745dc570SMike Snitzer 			 * ci.io->orig_bio to be used by end_io_acct() and
154518a25da8SNeilBrown 			 * for dec_pending to use for completion handling.
154618a25da8SNeilBrown 			 */
1547f21c601aSMike Snitzer 			struct bio *b = bio_split(bio, bio_sectors(bio) - ci.sector_count,
1548f21c601aSMike Snitzer 						  GFP_NOIO, &md->queue->bio_split);
1549745dc570SMike Snitzer 			ci.io->orig_bio = b;
1550a1e1cb72SMike Snitzer 
1551a1e1cb72SMike Snitzer 			/*
1552a1e1cb72SMike Snitzer 			 * Adjust IO stats for each split, otherwise upon queue
1553a1e1cb72SMike Snitzer 			 * reentry there will be redundant IO accounting.
1554a1e1cb72SMike Snitzer 			 * NOTE: this is a stop-gap fix, a proper fix involves
1555a1e1cb72SMike Snitzer 			 * significant refactoring of DM core's bio splitting
1556a1e1cb72SMike Snitzer 			 * (by eliminating DM's splitting and just using bio_split)
1557a1e1cb72SMike Snitzer 			 */
1558a1e1cb72SMike Snitzer 			part_stat_lock();
15598446fe92SChristoph Hellwig 			__dm_part_stat_sub(dm_disk(md)->part0,
1560a1e1cb72SMike Snitzer 					   sectors[op_stat_group(bio_op(bio))], ci.sector_count);
1561a1e1cb72SMike Snitzer 			part_stat_unlock();
1562a1e1cb72SMike Snitzer 
156318a25da8SNeilBrown 			bio_chain(b, bio);
1564eb6f7f7cSChristoph Hellwig 			trace_block_split(b, bio->bi_iter.bi_sector);
15653e08773cSChristoph Hellwig 			submit_bio_noacct(bio);
156618a25da8SNeilBrown 		}
1567d87f4c14STejun Heo 	}
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 	/* drop the extra reference count */
1570e2118b3cSDamien Le Moal 	dm_io_dec_pending(ci.io, errno_to_blk_status(error));
15711da177e4SLinus Torvalds }
15721da177e4SLinus Torvalds 
15733e08773cSChristoph Hellwig static void dm_submit_bio(struct bio *bio)
15741da177e4SLinus Torvalds {
1575309dca30SChristoph Hellwig 	struct mapped_device *md = bio->bi_bdev->bd_disk->private_data;
157683d5e5b0SMikulas Patocka 	int srcu_idx;
157783d5e5b0SMikulas Patocka 	struct dm_table *map;
15781da177e4SLinus Torvalds 
157983d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
1580b2abdb1bSMike Snitzer 	if (unlikely(!map)) {
1581b2abdb1bSMike Snitzer 		DMERR_LIMIT("%s: mapping table unavailable, erroring io",
1582b2abdb1bSMike Snitzer 			    dm_device_name(md));
15836a8736d1STejun Heo 		bio_io_error(bio);
1584b2abdb1bSMike Snitzer 		goto out;
15851da177e4SLinus Torvalds 	}
158692c63902SMikulas Patocka 
1587b2abdb1bSMike Snitzer 	/* If suspended, queue this IO for later */
15881da177e4SLinus Torvalds 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
15896abc4946SKonstantin Khlebnikov 		if (bio->bi_opf & REQ_NOWAIT)
15906abc4946SKonstantin Khlebnikov 			bio_wouldblock_error(bio);
1591b2abdb1bSMike Snitzer 		else if (bio->bi_opf & REQ_RAHEAD)
15921da177e4SLinus Torvalds 			bio_io_error(bio);
1593b2abdb1bSMike Snitzer 		else
1594b2abdb1bSMike Snitzer 			queue_io(md, bio);
1595b2abdb1bSMike Snitzer 		goto out;
15961da177e4SLinus Torvalds 	}
15971da177e4SLinus Torvalds 
1598b2abdb1bSMike Snitzer 	/*
1599b2abdb1bSMike Snitzer 	 * Use blk_queue_split() for abnormal IO (e.g. discard, writesame, etc)
1600b2abdb1bSMike Snitzer 	 * otherwise associated queue_limits won't be imposed.
1601b2abdb1bSMike Snitzer 	 */
1602b2abdb1bSMike Snitzer 	if (is_abnormal_io(bio))
1603b2abdb1bSMike Snitzer 		blk_queue_split(&bio);
1604978e51baSMike Snitzer 
16053e08773cSChristoph Hellwig 	__split_and_process_bio(md, map, bio);
1606b2abdb1bSMike Snitzer out:
160783d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
1608978e51baSMike Snitzer }
1609978e51baSMike Snitzer 
16101da177e4SLinus Torvalds /*-----------------------------------------------------------------
16111da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
16121da177e4SLinus Torvalds  *---------------------------------------------------------------*/
16132b06cfffSAlasdair G Kergon static void free_minor(int minor)
16141da177e4SLinus Torvalds {
1615f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
16161da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1617f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
16181da177e4SLinus Torvalds }
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds /*
16211da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
16221da177e4SLinus Torvalds  */
1623cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
16241da177e4SLinus Torvalds {
1625c9d76be6STejun Heo 	int r;
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
16281da177e4SLinus Torvalds 		return -EINVAL;
16291da177e4SLinus Torvalds 
1630c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1631f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
16321da177e4SLinus Torvalds 
1633c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
16341da177e4SLinus Torvalds 
1635f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1636c9d76be6STejun Heo 	idr_preload_end();
1637c9d76be6STejun Heo 	if (r < 0)
1638c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
1639c9d76be6STejun Heo 	return 0;
16401da177e4SLinus Torvalds }
16411da177e4SLinus Torvalds 
1642cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
16431da177e4SLinus Torvalds {
1644c9d76be6STejun Heo 	int r;
16451da177e4SLinus Torvalds 
1646c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1647f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
16481da177e4SLinus Torvalds 
1649c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
16501da177e4SLinus Torvalds 
1651f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1652c9d76be6STejun Heo 	idr_preload_end();
1653c9d76be6STejun Heo 	if (r < 0)
16541da177e4SLinus Torvalds 		return r;
1655c9d76be6STejun Heo 	*minor = r;
1656c9d76be6STejun Heo 	return 0;
16571da177e4SLinus Torvalds }
16581da177e4SLinus Torvalds 
165983d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
1660681cc5e8SMike Snitzer static const struct block_device_operations dm_rq_blk_dops;
1661f26c5719SDan Williams static const struct dax_operations dm_dax_ops;
16621da177e4SLinus Torvalds 
166353d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
166453d5914fSMikulas Patocka 
1665aa6ce87aSSatya Tangirala #ifdef CONFIG_BLK_INLINE_ENCRYPTION
1666cb77cb5aSEric Biggers static void dm_queue_destroy_crypto_profile(struct request_queue *q)
1667aa6ce87aSSatya Tangirala {
1668cb77cb5aSEric Biggers 	dm_destroy_crypto_profile(q->crypto_profile);
1669aa6ce87aSSatya Tangirala }
1670aa6ce87aSSatya Tangirala 
1671aa6ce87aSSatya Tangirala #else /* CONFIG_BLK_INLINE_ENCRYPTION */
1672aa6ce87aSSatya Tangirala 
1673cb77cb5aSEric Biggers static inline void dm_queue_destroy_crypto_profile(struct request_queue *q)
1674aa6ce87aSSatya Tangirala {
1675aa6ce87aSSatya Tangirala }
1676aa6ce87aSSatya Tangirala #endif /* !CONFIG_BLK_INLINE_ENCRYPTION */
1677aa6ce87aSSatya Tangirala 
16780f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md)
16790f20972fSMike Snitzer {
16800f20972fSMike Snitzer 	if (md->wq)
16810f20972fSMike Snitzer 		destroy_workqueue(md->wq);
16826f1c819cSKent Overstreet 	bioset_exit(&md->bs);
16836f1c819cSKent Overstreet 	bioset_exit(&md->io_bs);
16840f20972fSMike Snitzer 
1685f26c5719SDan Williams 	if (md->dax_dev) {
1686*fb08a190SChristoph Hellwig 		dax_remove_host(md->disk);
1687f26c5719SDan Williams 		kill_dax(md->dax_dev);
1688f26c5719SDan Williams 		put_dax(md->dax_dev);
1689f26c5719SDan Williams 		md->dax_dev = NULL;
1690f26c5719SDan Williams 	}
1691f26c5719SDan Williams 
16920f20972fSMike Snitzer 	if (md->disk) {
16930f20972fSMike Snitzer 		spin_lock(&_minor_lock);
16940f20972fSMike Snitzer 		md->disk->private_data = NULL;
16950f20972fSMike Snitzer 		spin_unlock(&_minor_lock);
169689f871afSChristoph Hellwig 		if (dm_get_md_type(md) != DM_TYPE_NONE) {
169789f871afSChristoph Hellwig 			dm_sysfs_exit(md);
16980f20972fSMike Snitzer 			del_gendisk(md->disk);
169989f871afSChristoph Hellwig 		}
1700cb77cb5aSEric Biggers 		dm_queue_destroy_crypto_profile(md->queue);
170174fe6ba9SChristoph Hellwig 		blk_cleanup_disk(md->disk);
170274a2b6ecSChristoph Hellwig 	}
17030f20972fSMike Snitzer 
1704d09960b0STahsin Erdogan 	cleanup_srcu_struct(&md->io_barrier);
1705d09960b0STahsin Erdogan 
1706d5ffebddSMike Snitzer 	mutex_destroy(&md->suspend_lock);
1707d5ffebddSMike Snitzer 	mutex_destroy(&md->type_lock);
1708d5ffebddSMike Snitzer 	mutex_destroy(&md->table_devices_lock);
1709a666e5c0SMikulas Patocka 	mutex_destroy(&md->swap_bios_lock);
1710d5ffebddSMike Snitzer 
17114cc96131SMike Snitzer 	dm_mq_cleanup_mapped_device(md);
1712bb37d772SDamien Le Moal 	dm_cleanup_zoned_dev(md);
17130f20972fSMike Snitzer }
17140f20972fSMike Snitzer 
17151da177e4SLinus Torvalds /*
17161da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
17171da177e4SLinus Torvalds  */
17182b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
17191da177e4SLinus Torvalds {
1720115485e8SMike Snitzer 	int r, numa_node_id = dm_get_numa_node();
1721115485e8SMike Snitzer 	struct mapped_device *md;
1722ba61fdd1SJeff Mahoney 	void *old_md;
17231da177e4SLinus Torvalds 
1724856eb091SMikulas Patocka 	md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
17251da177e4SLinus Torvalds 	if (!md) {
17261da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
17271da177e4SLinus Torvalds 		return NULL;
17281da177e4SLinus Torvalds 	}
17291da177e4SLinus Torvalds 
173010da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
17316ed7ade8SMilan Broz 		goto bad_module_get;
173210da4f79SJeff Mahoney 
17331da177e4SLinus Torvalds 	/* get a minor number for the dev */
17342b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1735cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
17362b06cfffSAlasdair G Kergon 	else
1737cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
17381da177e4SLinus Torvalds 	if (r < 0)
17396ed7ade8SMilan Broz 		goto bad_minor;
17401da177e4SLinus Torvalds 
174183d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
174283d5e5b0SMikulas Patocka 	if (r < 0)
174383d5e5b0SMikulas Patocka 		goto bad_io_barrier;
174483d5e5b0SMikulas Patocka 
1745115485e8SMike Snitzer 	md->numa_node_id = numa_node_id;
1746591ddcfcSMike Snitzer 	md->init_tio_pdu = false;
1747a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
1748e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1749a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
175086f1152bSBenjamin Marzinski 	mutex_init(&md->table_devices_lock);
1751022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
17521da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
17535c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
17541da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
17557a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
17567a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
175786f1152bSBenjamin Marzinski 	INIT_LIST_HEAD(&md->table_devices);
17587a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
17591da177e4SLinus Torvalds 
176047ace7e0SMike Snitzer 	/*
1761c62b37d9SChristoph Hellwig 	 * default to bio-based until DM table is loaded and md->type
1762c62b37d9SChristoph Hellwig 	 * established. If request-based table is loaded: blk-mq will
1763c62b37d9SChristoph Hellwig 	 * override accordingly.
176447ace7e0SMike Snitzer 	 */
176574fe6ba9SChristoph Hellwig 	md->disk = blk_alloc_disk(md->numa_node_id);
17661da177e4SLinus Torvalds 	if (!md->disk)
17670f20972fSMike Snitzer 		goto bad;
176874fe6ba9SChristoph Hellwig 	md->queue = md->disk->queue;
17691da177e4SLinus Torvalds 
1770f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
177153d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1772f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
17732995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
1774f0b04115SJeff Mahoney 
1775a666e5c0SMikulas Patocka 	md->swap_bios = get_swap_bios();
1776a666e5c0SMikulas Patocka 	sema_init(&md->swap_bios_semaphore, md->swap_bios);
1777a666e5c0SMikulas Patocka 	mutex_init(&md->swap_bios_lock);
1778a666e5c0SMikulas Patocka 
17791da177e4SLinus Torvalds 	md->disk->major = _major;
17801da177e4SLinus Torvalds 	md->disk->first_minor = minor;
178174fe6ba9SChristoph Hellwig 	md->disk->minors = 1;
17821da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
17831da177e4SLinus Torvalds 	md->disk->queue = md->queue;
17841da177e4SLinus Torvalds 	md->disk->private_data = md;
17851da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
1786f26c5719SDan Williams 
17875d2a228bSChristoph Hellwig 	if (IS_ENABLED(CONFIG_FS_DAX)) {
1788*fb08a190SChristoph Hellwig 		md->dax_dev = alloc_dax(md, &dm_dax_ops, 0);
1789d7519392SChristoph Hellwig 		if (IS_ERR(md->dax_dev)) {
1790d7519392SChristoph Hellwig 			md->dax_dev = NULL;
1791f26c5719SDan Williams 			goto bad;
1792976431b0SDan Williams 		}
1793*fb08a190SChristoph Hellwig 		if (dax_add_host(md->dax_dev, md->disk))
1794*fb08a190SChristoph Hellwig 			goto bad;
1795d7519392SChristoph Hellwig 	}
1796f26c5719SDan Williams 
17977e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
17981da177e4SLinus Torvalds 
1799c7c879eeSMichał Mirosław 	md->wq = alloc_workqueue("kdmflush/%s", WQ_MEM_RECLAIM, 0, md->name);
1800304f3f6aSMilan Broz 	if (!md->wq)
18010f20972fSMike Snitzer 		goto bad;
1802304f3f6aSMilan Broz 
1803fd2ed4d2SMikulas Patocka 	dm_stats_init(&md->stats);
1804fd2ed4d2SMikulas Patocka 
1805ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1806f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1807ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1808f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1809ba61fdd1SJeff Mahoney 
1810ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1811ba61fdd1SJeff Mahoney 
18121da177e4SLinus Torvalds 	return md;
18131da177e4SLinus Torvalds 
18140f20972fSMike Snitzer bad:
18150f20972fSMike Snitzer 	cleanup_mapped_device(md);
181683d5e5b0SMikulas Patocka bad_io_barrier:
18171da177e4SLinus Torvalds 	free_minor(minor);
18186ed7ade8SMilan Broz bad_minor:
181910da4f79SJeff Mahoney 	module_put(THIS_MODULE);
18206ed7ade8SMilan Broz bad_module_get:
1821856eb091SMikulas Patocka 	kvfree(md);
18221da177e4SLinus Torvalds 	return NULL;
18231da177e4SLinus Torvalds }
18241da177e4SLinus Torvalds 
1825ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1826ae9da83fSJun'ichi Nomura 
18271da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
18281da177e4SLinus Torvalds {
1829f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
183063d94e48SJun'ichi Nomura 
1831ae9da83fSJun'ichi Nomura 	unlock_fs(md);
18322eb6e1e3SKeith Busch 
18330f20972fSMike Snitzer 	cleanup_mapped_device(md);
18340f20972fSMike Snitzer 
18350f20972fSMike Snitzer 	free_table_devices(&md->table_devices);
18360f20972fSMike Snitzer 	dm_stats_cleanup(&md->stats);
183763a4f065SMike Snitzer 	free_minor(minor);
183863a4f065SMike Snitzer 
183910da4f79SJeff Mahoney 	module_put(THIS_MODULE);
1840856eb091SMikulas Patocka 	kvfree(md);
18411da177e4SLinus Torvalds }
18421da177e4SLinus Torvalds 
18432a2a4c51SJens Axboe static int __bind_mempools(struct mapped_device *md, struct dm_table *t)
1844e6ee8c0bSKiyoshi Ueda {
1845c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
18462a2a4c51SJens Axboe 	int ret = 0;
1847e6ee8c0bSKiyoshi Ueda 
1848545ed20eSToshi Kani 	if (dm_table_bio_based(t)) {
1849c0820cf5SMikulas Patocka 		/*
185064f52b0eSMike Snitzer 		 * The md may already have mempools that need changing.
185164f52b0eSMike Snitzer 		 * If so, reload bioset because front_pad may have changed
185216245bdcSJun'ichi Nomura 		 * because a different table was loaded.
1853c0820cf5SMikulas Patocka 		 */
18546f1c819cSKent Overstreet 		bioset_exit(&md->bs);
18556f1c819cSKent Overstreet 		bioset_exit(&md->io_bs);
18560776aa0eSMike Snitzer 
18576f1c819cSKent Overstreet 	} else if (bioset_initialized(&md->bs)) {
1858cbc4e3c1SMike Snitzer 		/*
18594e6e36c3SMike Snitzer 		 * There's no need to reload with request-based dm
18604e6e36c3SMike Snitzer 		 * because the size of front_pad doesn't change.
18614e6e36c3SMike Snitzer 		 * Note for future: If you are to reload bioset,
18624e6e36c3SMike Snitzer 		 * prep-ed requests in the queue may refer
18634e6e36c3SMike Snitzer 		 * to bio from the old bioset, so you must walk
18644e6e36c3SMike Snitzer 		 * through the queue to unprep.
1865cbc4e3c1SMike Snitzer 		 */
1866cbc4e3c1SMike Snitzer 		goto out;
1867cbc4e3c1SMike Snitzer 	}
1868cbc4e3c1SMike Snitzer 
18696f1c819cSKent Overstreet 	BUG_ON(!p ||
18706f1c819cSKent Overstreet 	       bioset_initialized(&md->bs) ||
18716f1c819cSKent Overstreet 	       bioset_initialized(&md->io_bs));
1872e6ee8c0bSKiyoshi Ueda 
18732a2a4c51SJens Axboe 	ret = bioset_init_from_src(&md->bs, &p->bs);
18742a2a4c51SJens Axboe 	if (ret)
18752a2a4c51SJens Axboe 		goto out;
18762a2a4c51SJens Axboe 	ret = bioset_init_from_src(&md->io_bs, &p->io_bs);
18772a2a4c51SJens Axboe 	if (ret)
18782a2a4c51SJens Axboe 		bioset_exit(&md->bs);
1879e6ee8c0bSKiyoshi Ueda out:
188002233342SMike Snitzer 	/* mempool bind completed, no longer need any mempools in the table */
1881e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
18822a2a4c51SJens Axboe 	return ret;
1883e6ee8c0bSKiyoshi Ueda }
1884e6ee8c0bSKiyoshi Ueda 
18851da177e4SLinus Torvalds /*
18861da177e4SLinus Torvalds  * Bind a table to the device.
18871da177e4SLinus Torvalds  */
18881da177e4SLinus Torvalds static void event_callback(void *context)
18891da177e4SLinus Torvalds {
18907a8c3d3bSMike Anderson 	unsigned long flags;
18917a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
18921da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
18931da177e4SLinus Torvalds 
18947a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
18957a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
18967a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
18977a8c3d3bSMike Anderson 
1898ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
18997a8c3d3bSMike Anderson 
19001da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
19011da177e4SLinus Torvalds 	wake_up(&md->eventq);
190262e08243SMikulas Patocka 	dm_issue_global_event();
19031da177e4SLinus Torvalds }
19041da177e4SLinus Torvalds 
1905c217649bSMike Snitzer /*
1906042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
1907042d2a9bSAlasdair G Kergon  */
1908042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
1909754c5fc7SMike Snitzer 			       struct queue_limits *limits)
19101da177e4SLinus Torvalds {
1911042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
1912165125e1SJens Axboe 	struct request_queue *q = md->queue;
1913978e51baSMike Snitzer 	bool request_based = dm_table_request_based(t);
19141da177e4SLinus Torvalds 	sector_t size;
19152a2a4c51SJens Axboe 	int ret;
19161da177e4SLinus Torvalds 
19175a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
19185a8f1f80SBart Van Assche 
19191da177e4SLinus Torvalds 	size = dm_table_get_size(t);
19203ac51e74SDarrick J. Wong 
19213ac51e74SDarrick J. Wong 	/*
19223ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
19233ac51e74SDarrick J. Wong 	 */
1924fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
19253ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
19263ac51e74SDarrick J. Wong 
19275424a0b8SMikulas Patocka 	if (!get_capacity(md->disk))
19285424a0b8SMikulas Patocka 		set_capacity(md->disk, size);
19295424a0b8SMikulas Patocka 	else
1930f64d9b2eSChristoph Hellwig 		set_capacity_and_notify(md->disk, size);
19311da177e4SLinus Torvalds 
1932cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
19332ca3310eSAlasdair G Kergon 
19349c37de29SMike Snitzer 	if (request_based) {
193516f12266SMike Snitzer 		/*
19369c37de29SMike Snitzer 		 * Leverage the fact that request-based DM targets are
19379c37de29SMike Snitzer 		 * immutable singletons - used to optimize dm_mq_queue_rq.
193816f12266SMike Snitzer 		 */
193916f12266SMike Snitzer 		md->immutable_target = dm_table_get_immutable_target(t);
194016f12266SMike Snitzer 	}
1941e6ee8c0bSKiyoshi Ueda 
19422a2a4c51SJens Axboe 	ret = __bind_mempools(md, t);
19432a2a4c51SJens Axboe 	if (ret) {
19442a2a4c51SJens Axboe 		old_map = ERR_PTR(ret);
19452a2a4c51SJens Axboe 		goto out;
19462a2a4c51SJens Axboe 	}
1947e6ee8c0bSKiyoshi Ueda 
1948bb37d772SDamien Le Moal 	ret = dm_table_set_restrictions(t, q, limits);
1949bb37d772SDamien Le Moal 	if (ret) {
1950bb37d772SDamien Le Moal 		old_map = ERR_PTR(ret);
1951bb37d772SDamien Le Moal 		goto out;
1952bb37d772SDamien Le Moal 	}
1953bb37d772SDamien Le Moal 
1954a12f5d48SEric Dumazet 	old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
19551d3aa6f6SMike Snitzer 	rcu_assign_pointer(md->map, (void *)t);
195636a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
195736a0456fSAlasdair G Kergon 
195841abc4e1SHannes Reinecke 	if (old_map)
195983d5e5b0SMikulas Patocka 		dm_sync_table(md);
19602ca3310eSAlasdair G Kergon 
19612a2a4c51SJens Axboe out:
1962042d2a9bSAlasdair G Kergon 	return old_map;
19631da177e4SLinus Torvalds }
19641da177e4SLinus Torvalds 
1965a7940155SAlasdair G Kergon /*
1966a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
1967a7940155SAlasdair G Kergon  */
1968a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
19691da177e4SLinus Torvalds {
1970a12f5d48SEric Dumazet 	struct dm_table *map = rcu_dereference_protected(md->map, 1);
19711da177e4SLinus Torvalds 
19721da177e4SLinus Torvalds 	if (!map)
1973a7940155SAlasdair G Kergon 		return NULL;
19741da177e4SLinus Torvalds 
19751da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
19769cdb8520SMonam Agarwal 	RCU_INIT_POINTER(md->map, NULL);
197783d5e5b0SMikulas Patocka 	dm_sync_table(md);
1978a7940155SAlasdair G Kergon 
1979a7940155SAlasdair G Kergon 	return map;
19801da177e4SLinus Torvalds }
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds /*
19831da177e4SLinus Torvalds  * Constructor for a new device.
19841da177e4SLinus Torvalds  */
19852b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
19861da177e4SLinus Torvalds {
19871da177e4SLinus Torvalds 	struct mapped_device *md;
19881da177e4SLinus Torvalds 
19892b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
19901da177e4SLinus Torvalds 	if (!md)
19911da177e4SLinus Torvalds 		return -ENXIO;
19921da177e4SLinus Torvalds 
199391ccbbacSTushar Sugandhi 	dm_ima_reset_data(md);
199491ccbbacSTushar Sugandhi 
19951da177e4SLinus Torvalds 	*result = md;
19961da177e4SLinus Torvalds 	return 0;
19971da177e4SLinus Torvalds }
19981da177e4SLinus Torvalds 
1999a5664dadSMike Snitzer /*
2000a5664dadSMike Snitzer  * Functions to manage md->type.
2001a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2002a5664dadSMike Snitzer  */
2003a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2004a5664dadSMike Snitzer {
2005a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2006a5664dadSMike Snitzer }
2007a5664dadSMike Snitzer 
2008a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2009a5664dadSMike Snitzer {
2010a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2011a5664dadSMike Snitzer }
2012a5664dadSMike Snitzer 
20137e0d574fSBart Van Assche void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
2014a5664dadSMike Snitzer {
201500c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
2016a5664dadSMike Snitzer 	md->type = type;
2017a5664dadSMike Snitzer }
2018a5664dadSMike Snitzer 
20197e0d574fSBart Van Assche enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
2020a5664dadSMike Snitzer {
2021a5664dadSMike Snitzer 	return md->type;
2022a5664dadSMike Snitzer }
2023a5664dadSMike Snitzer 
202436a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
202536a0456fSAlasdair G Kergon {
202636a0456fSAlasdair G Kergon 	return md->immutable_target_type;
202736a0456fSAlasdair G Kergon }
202836a0456fSAlasdair G Kergon 
20294a0b4ddfSMike Snitzer /*
2030f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
2031f84cb8a4SMike Snitzer  * count on 'md'.
2032f84cb8a4SMike Snitzer  */
2033f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
2034f84cb8a4SMike Snitzer {
2035f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
2036f84cb8a4SMike Snitzer 	return &md->queue->limits;
2037f84cb8a4SMike Snitzer }
2038f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
2039f84cb8a4SMike Snitzer 
20404a0b4ddfSMike Snitzer /*
20414a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
20424a0b4ddfSMike Snitzer  */
2043591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
20444a0b4ddfSMike Snitzer {
2045ba305859SChristoph Hellwig 	enum dm_queue_mode type = dm_table_get_type(t);
2046c100ec49SMike Snitzer 	struct queue_limits limits;
2047ba305859SChristoph Hellwig 	int r;
2048bfebd1cdSMike Snitzer 
2049545ed20eSToshi Kani 	switch (type) {
2050bfebd1cdSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
2051681cc5e8SMike Snitzer 		md->disk->fops = &dm_rq_blk_dops;
2052e83068a5SMike Snitzer 		r = dm_mq_init_request_queue(md, t);
2053bfebd1cdSMike Snitzer 		if (r) {
2054681cc5e8SMike Snitzer 			DMERR("Cannot initialize queue for request-based dm mapped device");
2055bfebd1cdSMike Snitzer 			return r;
2056bfebd1cdSMike Snitzer 		}
2057bfebd1cdSMike Snitzer 		break;
2058bfebd1cdSMike Snitzer 	case DM_TYPE_BIO_BASED:
2059545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
2060bfebd1cdSMike Snitzer 		break;
20617e0d574fSBart Van Assche 	case DM_TYPE_NONE:
20627e0d574fSBart Van Assche 		WARN_ON_ONCE(true);
20637e0d574fSBart Van Assche 		break;
2064ff36ab34SMike Snitzer 	}
20654a0b4ddfSMike Snitzer 
2066c100ec49SMike Snitzer 	r = dm_calculate_queue_limits(t, &limits);
2067c100ec49SMike Snitzer 	if (r) {
2068c100ec49SMike Snitzer 		DMERR("Cannot calculate initial queue limits");
2069c100ec49SMike Snitzer 		return r;
2070c100ec49SMike Snitzer 	}
2071bb37d772SDamien Le Moal 	r = dm_table_set_restrictions(t, md->queue, &limits);
2072bb37d772SDamien Le Moal 	if (r)
2073bb37d772SDamien Le Moal 		return r;
207489f871afSChristoph Hellwig 
2075e7089f65SLuis Chamberlain 	r = add_disk(md->disk);
2076e7089f65SLuis Chamberlain 	if (r)
2077e7089f65SLuis Chamberlain 		return r;
207889f871afSChristoph Hellwig 
207989f871afSChristoph Hellwig 	r = dm_sysfs_init(md);
208089f871afSChristoph Hellwig 	if (r) {
208189f871afSChristoph Hellwig 		del_gendisk(md->disk);
208289f871afSChristoph Hellwig 		return r;
208389f871afSChristoph Hellwig 	}
2084ba305859SChristoph Hellwig 	md->type = type;
20854a0b4ddfSMike Snitzer 	return 0;
20864a0b4ddfSMike Snitzer }
20874a0b4ddfSMike Snitzer 
20882bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev)
20891da177e4SLinus Torvalds {
20901da177e4SLinus Torvalds 	struct mapped_device *md;
20911da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
20921da177e4SLinus Torvalds 
20931da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
20941da177e4SLinus Torvalds 		return NULL;
20951da177e4SLinus Torvalds 
2096f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
20971da177e4SLinus Torvalds 
20981da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
209949de5769SMike Snitzer 	if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) ||
210049de5769SMike Snitzer 	    test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2101637842cfSDavid Teigland 		md = NULL;
2102fba9f90eSJeff Mahoney 		goto out;
2103fba9f90eSJeff Mahoney 	}
21042bec1f4aSMikulas Patocka 	dm_get(md);
2105fba9f90eSJeff Mahoney out:
2106f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
21071da177e4SLinus Torvalds 
2108637842cfSDavid Teigland 	return md;
2109637842cfSDavid Teigland }
21103cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2111d229a958SDavid Teigland 
21129ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2113637842cfSDavid Teigland {
21149ade92a9SAlasdair G Kergon 	return md->interface_ptr;
21151da177e4SLinus Torvalds }
21161da177e4SLinus Torvalds 
21171da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
21181da177e4SLinus Torvalds {
21191da177e4SLinus Torvalds 	md->interface_ptr = ptr;
21201da177e4SLinus Torvalds }
21211da177e4SLinus Torvalds 
21221da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
21231da177e4SLinus Torvalds {
21241da177e4SLinus Torvalds 	atomic_inc(&md->holders);
21253f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
21261da177e4SLinus Torvalds }
21271da177e4SLinus Torvalds 
212809ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md)
212909ee96b2SMikulas Patocka {
213009ee96b2SMikulas Patocka 	spin_lock(&_minor_lock);
213109ee96b2SMikulas Patocka 	if (test_bit(DMF_FREEING, &md->flags)) {
213209ee96b2SMikulas Patocka 		spin_unlock(&_minor_lock);
213309ee96b2SMikulas Patocka 		return -EBUSY;
213409ee96b2SMikulas Patocka 	}
213509ee96b2SMikulas Patocka 	dm_get(md);
213609ee96b2SMikulas Patocka 	spin_unlock(&_minor_lock);
213709ee96b2SMikulas Patocka 	return 0;
213809ee96b2SMikulas Patocka }
213909ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold);
214009ee96b2SMikulas Patocka 
214172d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
214272d94861SAlasdair G Kergon {
214372d94861SAlasdair G Kergon 	return md->name;
214472d94861SAlasdair G Kergon }
214572d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
214672d94861SAlasdair G Kergon 
21473f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
21481da177e4SLinus Torvalds {
21491134e5aeSMike Anderson 	struct dm_table *map;
215083d5e5b0SMikulas Patocka 	int srcu_idx;
21511da177e4SLinus Torvalds 
21523f77316dSKiyoshi Ueda 	might_sleep();
2153fba9f90eSJeff Mahoney 
215463a4f065SMike Snitzer 	spin_lock(&_minor_lock);
21553f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2156fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2157f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
21583f77316dSKiyoshi Ueda 
2159c12c9a3cSMike Snitzer 	blk_set_queue_dying(md->queue);
21603b785fbcSBart Van Assche 
2161ab7c7bb6SMikulas Patocka 	/*
2162ab7c7bb6SMikulas Patocka 	 * Take suspend_lock so that presuspend and postsuspend methods
2163ab7c7bb6SMikulas Patocka 	 * do not race with internal suspend.
2164ab7c7bb6SMikulas Patocka 	 */
2165ab7c7bb6SMikulas Patocka 	mutex_lock(&md->suspend_lock);
21662a708cffSJunichi Nomura 	map = dm_get_live_table(md, &srcu_idx);
21674f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
21681da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
2169adc0daadSMikulas Patocka 		set_bit(DMF_SUSPENDED, &md->flags);
21705df96f2bSMikulas Patocka 		set_bit(DMF_POST_SUSPENDING, &md->flags);
21711da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
21721da177e4SLinus Torvalds 	}
217383d5e5b0SMikulas Patocka 	/* dm_put_live_table must be before msleep, otherwise deadlock is possible */
217483d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
21752a708cffSJunichi Nomura 	mutex_unlock(&md->suspend_lock);
217683d5e5b0SMikulas Patocka 
21773f77316dSKiyoshi Ueda 	/*
21783f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
21793f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
21803f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
21813f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
21823f77316dSKiyoshi Ueda 	 */
21833f77316dSKiyoshi Ueda 	if (wait)
21843f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
21853f77316dSKiyoshi Ueda 			msleep(1);
21863f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
21873f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
21883f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
21893f77316dSKiyoshi Ueda 
2190a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
21911da177e4SLinus Torvalds 	free_dev(md);
21921da177e4SLinus Torvalds }
21933f77316dSKiyoshi Ueda 
21943f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
21953f77316dSKiyoshi Ueda {
21963f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
21973f77316dSKiyoshi Ueda }
21983f77316dSKiyoshi Ueda 
21993f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
22003f77316dSKiyoshi Ueda {
22013f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
22023f77316dSKiyoshi Ueda }
22033f77316dSKiyoshi Ueda 
22043f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
22053f77316dSKiyoshi Ueda {
22063f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
22071da177e4SLinus Torvalds }
220879eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
22091da177e4SLinus Torvalds 
221085067747SMing Lei static bool md_in_flight_bios(struct mapped_device *md)
221185067747SMing Lei {
221285067747SMing Lei 	int cpu;
22138446fe92SChristoph Hellwig 	struct block_device *part = dm_disk(md)->part0;
221485067747SMing Lei 	long sum = 0;
221585067747SMing Lei 
221685067747SMing Lei 	for_each_possible_cpu(cpu) {
221785067747SMing Lei 		sum += part_stat_local_read_cpu(part, in_flight[0], cpu);
221885067747SMing Lei 		sum += part_stat_local_read_cpu(part, in_flight[1], cpu);
221985067747SMing Lei 	}
222085067747SMing Lei 
222185067747SMing Lei 	return sum != 0;
222285067747SMing Lei }
222385067747SMing Lei 
22242f064a59SPeter Zijlstra static int dm_wait_for_bios_completion(struct mapped_device *md, unsigned int task_state)
222546125c1cSMilan Broz {
222646125c1cSMilan Broz 	int r = 0;
22279f4c3f87SBart Van Assche 	DEFINE_WAIT(wait);
222846125c1cSMilan Broz 
222985067747SMing Lei 	while (true) {
22309f4c3f87SBart Van Assche 		prepare_to_wait(&md->wait, &wait, task_state);
223146125c1cSMilan Broz 
223285067747SMing Lei 		if (!md_in_flight_bios(md))
223346125c1cSMilan Broz 			break;
223446125c1cSMilan Broz 
2235e3fabdfdSBart Van Assche 		if (signal_pending_state(task_state, current)) {
223646125c1cSMilan Broz 			r = -EINTR;
223746125c1cSMilan Broz 			break;
223846125c1cSMilan Broz 		}
223946125c1cSMilan Broz 
224046125c1cSMilan Broz 		io_schedule();
224146125c1cSMilan Broz 	}
22429f4c3f87SBart Van Assche 	finish_wait(&md->wait, &wait);
2243b44ebeb0SMikulas Patocka 
224446125c1cSMilan Broz 	return r;
224546125c1cSMilan Broz }
224646125c1cSMilan Broz 
22472f064a59SPeter Zijlstra static int dm_wait_for_completion(struct mapped_device *md, unsigned int task_state)
224885067747SMing Lei {
224985067747SMing Lei 	int r = 0;
225085067747SMing Lei 
225185067747SMing Lei 	if (!queue_is_mq(md->queue))
225285067747SMing Lei 		return dm_wait_for_bios_completion(md, task_state);
225385067747SMing Lei 
225485067747SMing Lei 	while (true) {
225585067747SMing Lei 		if (!blk_mq_queue_inflight(md->queue))
225685067747SMing Lei 			break;
225785067747SMing Lei 
225885067747SMing Lei 		if (signal_pending_state(task_state, current)) {
225985067747SMing Lei 			r = -EINTR;
226085067747SMing Lei 			break;
226185067747SMing Lei 		}
226285067747SMing Lei 
226385067747SMing Lei 		msleep(5);
226485067747SMing Lei 	}
226585067747SMing Lei 
226685067747SMing Lei 	return r;
226785067747SMing Lei }
226885067747SMing Lei 
22691da177e4SLinus Torvalds /*
22701da177e4SLinus Torvalds  * Process the deferred bios
22711da177e4SLinus Torvalds  */
2272ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
22731da177e4SLinus Torvalds {
22740c2915b8SMike Snitzer 	struct mapped_device *md = container_of(work, struct mapped_device, work);
22750c2915b8SMike Snitzer 	struct bio *bio;
2276ef208587SMikulas Patocka 
22773b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2278022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
22790c2915b8SMike Snitzer 		bio = bio_list_pop(&md->deferred);
2280022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2281022c2611SMikulas Patocka 
22820c2915b8SMike Snitzer 		if (!bio)
2283df12ee99SAlasdair G Kergon 			break;
228473d410c0SMilan Broz 
22850c2915b8SMike Snitzer 		submit_bio_noacct(bio);
2286e6ee8c0bSKiyoshi Ueda 	}
22871da177e4SLinus Torvalds }
22881da177e4SLinus Torvalds 
22899a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2290304f3f6aSMilan Broz {
22913b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
22924e857c58SPeter Zijlstra 	smp_mb__after_atomic();
229353d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2294304f3f6aSMilan Broz }
2295304f3f6aSMilan Broz 
22961da177e4SLinus Torvalds /*
2297042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
22981da177e4SLinus Torvalds  */
2299042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
23001da177e4SLinus Torvalds {
230187eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2302754c5fc7SMike Snitzer 	struct queue_limits limits;
2303042d2a9bSAlasdair G Kergon 	int r;
23041da177e4SLinus Torvalds 
2305e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
23061da177e4SLinus Torvalds 
23071da177e4SLinus Torvalds 	/* device must be suspended */
23084f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
230993c534aeSAlasdair G Kergon 		goto out;
23101da177e4SLinus Torvalds 
23113ae70656SMike Snitzer 	/*
23123ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
23133ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
23143ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
23153ae70656SMike Snitzer 	 * reappear.
23163ae70656SMike Snitzer 	 */
23173ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
231883d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
23193ae70656SMike Snitzer 		if (live_map)
23203ae70656SMike Snitzer 			limits = md->queue->limits;
232183d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
23223ae70656SMike Snitzer 	}
23233ae70656SMike Snitzer 
232487eb5b21SMike Christie 	if (!live_map) {
2325754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2326042d2a9bSAlasdair G Kergon 		if (r) {
2327042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2328754c5fc7SMike Snitzer 			goto out;
2329042d2a9bSAlasdair G Kergon 		}
233087eb5b21SMike Christie 	}
2331754c5fc7SMike Snitzer 
2332042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
233362e08243SMikulas Patocka 	dm_issue_global_event();
23341da177e4SLinus Torvalds 
233593c534aeSAlasdair G Kergon out:
2336e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2337042d2a9bSAlasdair G Kergon 	return map;
23381da177e4SLinus Torvalds }
23391da177e4SLinus Torvalds 
23401da177e4SLinus Torvalds /*
23411da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
23421da177e4SLinus Torvalds  * device.
23431da177e4SLinus Torvalds  */
23442ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
23451da177e4SLinus Torvalds {
2346e39e2e95SAlasdair G Kergon 	int r;
23471da177e4SLinus Torvalds 
2348040f04bdSChristoph Hellwig 	WARN_ON(test_bit(DMF_FROZEN, &md->flags));
2349dfbe03f6SAlasdair G Kergon 
2350977115c0SChristoph Hellwig 	r = freeze_bdev(md->disk->part0);
2351040f04bdSChristoph Hellwig 	if (!r)
2352aa8d7c2fSAlasdair G Kergon 		set_bit(DMF_FROZEN, &md->flags);
2353040f04bdSChristoph Hellwig 	return r;
23541da177e4SLinus Torvalds }
23551da177e4SLinus Torvalds 
23562ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
23571da177e4SLinus Torvalds {
2358aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2359aa8d7c2fSAlasdair G Kergon 		return;
2360977115c0SChristoph Hellwig 	thaw_bdev(md->disk->part0);
2361aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
23621da177e4SLinus Torvalds }
23631da177e4SLinus Torvalds 
23641da177e4SLinus Torvalds /*
2365b48633f8SBart Van Assche  * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
2366b48633f8SBart Van Assche  * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
2367b48633f8SBart Van Assche  * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
2368b48633f8SBart Van Assche  *
2369ffcc3936SMike Snitzer  * If __dm_suspend returns 0, the device is completely quiescent
2370ffcc3936SMike Snitzer  * now. There is no request-processing activity. All new requests
2371ffcc3936SMike Snitzer  * are being added to md->deferred list.
2372cec47e3dSKiyoshi Ueda  */
2373ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
23742f064a59SPeter Zijlstra 			unsigned suspend_flags, unsigned int task_state,
2375eaf9a736SMike Snitzer 			int dmf_suspended_flag)
23761da177e4SLinus Torvalds {
2377ffcc3936SMike Snitzer 	bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2378ffcc3936SMike Snitzer 	bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2379ffcc3936SMike Snitzer 	int r;
2380cf222b37SAlasdair G Kergon 
23815a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
23825a8f1f80SBart Van Assche 
23832e93ccc1SKiyoshi Ueda 	/*
23842e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
23852e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
23862e93ccc1SKiyoshi Ueda 	 */
23872e93ccc1SKiyoshi Ueda 	if (noflush)
23882e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
238986331f39SBart Van Assche 	else
2390ac75b09fSMike Snitzer 		DMDEBUG("%s: suspending with flush", dm_device_name(md));
23912e93ccc1SKiyoshi Ueda 
2392d67ee213SMike Snitzer 	/*
2393d67ee213SMike Snitzer 	 * This gets reverted if there's an error later and the targets
2394d67ee213SMike Snitzer 	 * provide the .presuspend_undo hook.
2395d67ee213SMike Snitzer 	 */
23961da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
23971da177e4SLinus Torvalds 
23982e93ccc1SKiyoshi Ueda 	/*
23999f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
24009f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
24019f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
24029f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
24032e93ccc1SKiyoshi Ueda 	 */
240432a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
24052ca3310eSAlasdair G Kergon 		r = lock_fs(md);
2406d67ee213SMike Snitzer 		if (r) {
2407d67ee213SMike Snitzer 			dm_table_presuspend_undo_targets(map);
2408ffcc3936SMike Snitzer 			return r;
2409aa8d7c2fSAlasdair G Kergon 		}
2410d67ee213SMike Snitzer 	}
24111da177e4SLinus Torvalds 
24121da177e4SLinus Torvalds 	/*
24133b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
24143b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
24150cede372SMike Snitzer 	 * __split_and_process_bio from dm_submit_bio.
24163b00b203SMikulas Patocka 	 *
24170cede372SMike Snitzer 	 * To get all processes out of __split_and_process_bio in dm_submit_bio,
24183b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
24190cede372SMike Snitzer 	 * __split_and_process_bio from dm_submit_bio and quiesce the thread
24200cede372SMike Snitzer 	 * (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND and call
24216a8736d1STejun Heo 	 * flush_workqueue(md->wq).
24221da177e4SLinus Torvalds 	 */
24231eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
242441abc4e1SHannes Reinecke 	if (map)
242583d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
24261da177e4SLinus Torvalds 
2427d0bcb878SKiyoshi Ueda 	/*
242829e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
242929e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2430d0bcb878SKiyoshi Ueda 	 */
24316a23e05cSJens Axboe 	if (dm_request_based(md))
2432eca7ee6dSMike Snitzer 		dm_stop_queue(md->queue);
2433cec47e3dSKiyoshi Ueda 
2434d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2435d0bcb878SKiyoshi Ueda 
24361da177e4SLinus Torvalds 	/*
24373b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
24383b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
24393b00b203SMikulas Patocka 	 * to finish.
24401da177e4SLinus Torvalds 	 */
2441b48633f8SBart Van Assche 	r = dm_wait_for_completion(md, task_state);
2442eaf9a736SMike Snitzer 	if (!r)
2443eaf9a736SMike Snitzer 		set_bit(dmf_suspended_flag, &md->flags);
24441da177e4SLinus Torvalds 
24456d6f10dfSMilan Broz 	if (noflush)
2446022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
244741abc4e1SHannes Reinecke 	if (map)
244883d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
24492e93ccc1SKiyoshi Ueda 
24501da177e4SLinus Torvalds 	/* were we interrupted ? */
245146125c1cSMilan Broz 	if (r < 0) {
24529a1fb464SMikulas Patocka 		dm_queue_flush(md);
245373d410c0SMilan Broz 
2454cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
2455eca7ee6dSMike Snitzer 			dm_start_queue(md->queue);
2456cec47e3dSKiyoshi Ueda 
24572ca3310eSAlasdair G Kergon 		unlock_fs(md);
2458d67ee213SMike Snitzer 		dm_table_presuspend_undo_targets(map);
2459ffcc3936SMike Snitzer 		/* pushback list is already flushed, so skip flush */
2460ffcc3936SMike Snitzer 	}
2461ffcc3936SMike Snitzer 
2462ffcc3936SMike Snitzer 	return r;
24632ca3310eSAlasdair G Kergon }
24642ca3310eSAlasdair G Kergon 
24653b00b203SMikulas Patocka /*
2466ffcc3936SMike Snitzer  * We need to be able to change a mapping table under a mounted
2467ffcc3936SMike Snitzer  * filesystem.  For example we might want to move some data in
2468ffcc3936SMike Snitzer  * the background.  Before the table can be swapped with
2469ffcc3936SMike Snitzer  * dm_bind_table, dm_suspend must be called to flush any in
2470ffcc3936SMike Snitzer  * flight bios and ensure that any further io gets deferred.
24713b00b203SMikulas Patocka  */
2472ffcc3936SMike Snitzer /*
2473ffcc3936SMike Snitzer  * Suspend mechanism in request-based dm.
2474ffcc3936SMike Snitzer  *
2475ffcc3936SMike Snitzer  * 1. Flush all I/Os by lock_fs() if needed.
2476ffcc3936SMike Snitzer  * 2. Stop dispatching any I/O by stopping the request_queue.
2477ffcc3936SMike Snitzer  * 3. Wait for all in-flight I/Os to be completed or requeued.
2478ffcc3936SMike Snitzer  *
2479ffcc3936SMike Snitzer  * To abort suspend, start the request_queue.
2480ffcc3936SMike Snitzer  */
2481ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2482ffcc3936SMike Snitzer {
2483ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2484ffcc3936SMike Snitzer 	int r = 0;
2485ffcc3936SMike Snitzer 
2486ffcc3936SMike Snitzer retry:
2487ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2488ffcc3936SMike Snitzer 
2489ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2490ffcc3936SMike Snitzer 		r = -EINVAL;
2491ffcc3936SMike Snitzer 		goto out_unlock;
2492ffcc3936SMike Snitzer 	}
2493ffcc3936SMike Snitzer 
2494ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2495ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2496ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2497ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2498ffcc3936SMike Snitzer 		if (r)
2499ffcc3936SMike Snitzer 			return r;
2500ffcc3936SMike Snitzer 		goto retry;
2501ffcc3936SMike Snitzer 	}
2502ffcc3936SMike Snitzer 
2503a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2504ffcc3936SMike Snitzer 
2505eaf9a736SMike Snitzer 	r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
2506ffcc3936SMike Snitzer 	if (r)
2507ffcc3936SMike Snitzer 		goto out_unlock;
25083b00b203SMikulas Patocka 
25095df96f2bSMikulas Patocka 	set_bit(DMF_POST_SUSPENDING, &md->flags);
25104d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
25115df96f2bSMikulas Patocka 	clear_bit(DMF_POST_SUSPENDING, &md->flags);
25124d4471cbSKiyoshi Ueda 
2513d287483dSAlasdair G Kergon out_unlock:
2514e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2515cf222b37SAlasdair G Kergon 	return r;
25161da177e4SLinus Torvalds }
25171da177e4SLinus Torvalds 
2518ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map)
25191da177e4SLinus Torvalds {
2520ffcc3936SMike Snitzer 	if (map) {
2521ffcc3936SMike Snitzer 		int r = dm_table_resume_targets(map);
25228757b776SMilan Broz 		if (r)
2523ffcc3936SMike Snitzer 			return r;
2524ffcc3936SMike Snitzer 	}
25252ca3310eSAlasdair G Kergon 
25269a1fb464SMikulas Patocka 	dm_queue_flush(md);
25272ca3310eSAlasdair G Kergon 
2528cec47e3dSKiyoshi Ueda 	/*
2529cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2530cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2531cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2532cec47e3dSKiyoshi Ueda 	 */
2533cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2534eca7ee6dSMike Snitzer 		dm_start_queue(md->queue);
2535cec47e3dSKiyoshi Ueda 
25362ca3310eSAlasdair G Kergon 	unlock_fs(md);
25372ca3310eSAlasdair G Kergon 
2538ffcc3936SMike Snitzer 	return 0;
2539ffcc3936SMike Snitzer }
2540ffcc3936SMike Snitzer 
2541ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md)
2542ffcc3936SMike Snitzer {
25438dc23658SMinfei Huang 	int r;
2544ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2545ffcc3936SMike Snitzer 
2546ffcc3936SMike Snitzer retry:
25478dc23658SMinfei Huang 	r = -EINVAL;
2548ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2549ffcc3936SMike Snitzer 
2550ffcc3936SMike Snitzer 	if (!dm_suspended_md(md))
2551ffcc3936SMike Snitzer 		goto out;
2552ffcc3936SMike Snitzer 
2553ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2554ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2555ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2556ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2557ffcc3936SMike Snitzer 		if (r)
2558ffcc3936SMike Snitzer 			return r;
2559ffcc3936SMike Snitzer 		goto retry;
2560ffcc3936SMike Snitzer 	}
2561ffcc3936SMike Snitzer 
2562a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2563ffcc3936SMike Snitzer 	if (!map || !dm_table_get_size(map))
2564ffcc3936SMike Snitzer 		goto out;
2565ffcc3936SMike Snitzer 
2566ffcc3936SMike Snitzer 	r = __dm_resume(md, map);
2567ffcc3936SMike Snitzer 	if (r)
2568ffcc3936SMike Snitzer 		goto out;
2569ffcc3936SMike Snitzer 
25702ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
2571cf222b37SAlasdair G Kergon out:
2572e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
25732ca3310eSAlasdair G Kergon 
2574cf222b37SAlasdair G Kergon 	return r;
25751da177e4SLinus Torvalds }
25761da177e4SLinus Torvalds 
2577fd2ed4d2SMikulas Patocka /*
2578fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
2579fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
2580fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
2581fd2ed4d2SMikulas Patocka  */
2582fd2ed4d2SMikulas Patocka 
2583ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
2584ffcc3936SMike Snitzer {
2585ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2586ffcc3936SMike Snitzer 
25871ea0654eSBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
25881ea0654eSBart Van Assche 
258996b26c8cSMikulas Patocka 	if (md->internal_suspend_count++)
2590ffcc3936SMike Snitzer 		return; /* nested internal suspend */
2591ffcc3936SMike Snitzer 
2592ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2593ffcc3936SMike Snitzer 		set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2594ffcc3936SMike Snitzer 		return; /* nest suspend */
2595ffcc3936SMike Snitzer 	}
2596ffcc3936SMike Snitzer 
2597a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2598ffcc3936SMike Snitzer 
2599ffcc3936SMike Snitzer 	/*
2600ffcc3936SMike Snitzer 	 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2601ffcc3936SMike Snitzer 	 * supported.  Properly supporting a TASK_INTERRUPTIBLE internal suspend
2602ffcc3936SMike Snitzer 	 * would require changing .presuspend to return an error -- avoid this
2603ffcc3936SMike Snitzer 	 * until there is a need for more elaborate variants of internal suspend.
2604ffcc3936SMike Snitzer 	 */
2605eaf9a736SMike Snitzer 	(void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
2606eaf9a736SMike Snitzer 			    DMF_SUSPENDED_INTERNALLY);
2607ffcc3936SMike Snitzer 
26085df96f2bSMikulas Patocka 	set_bit(DMF_POST_SUSPENDING, &md->flags);
2609ffcc3936SMike Snitzer 	dm_table_postsuspend_targets(map);
26105df96f2bSMikulas Patocka 	clear_bit(DMF_POST_SUSPENDING, &md->flags);
2611ffcc3936SMike Snitzer }
2612ffcc3936SMike Snitzer 
2613ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md)
2614ffcc3936SMike Snitzer {
261596b26c8cSMikulas Patocka 	BUG_ON(!md->internal_suspend_count);
261696b26c8cSMikulas Patocka 
261796b26c8cSMikulas Patocka 	if (--md->internal_suspend_count)
2618ffcc3936SMike Snitzer 		return; /* resume from nested internal suspend */
2619ffcc3936SMike Snitzer 
2620ffcc3936SMike Snitzer 	if (dm_suspended_md(md))
2621ffcc3936SMike Snitzer 		goto done; /* resume from nested suspend */
2622ffcc3936SMike Snitzer 
2623ffcc3936SMike Snitzer 	/*
2624ffcc3936SMike Snitzer 	 * NOTE: existing callers don't need to call dm_table_resume_targets
2625ffcc3936SMike Snitzer 	 * (which may fail -- so best to avoid it for now by passing NULL map)
2626ffcc3936SMike Snitzer 	 */
2627ffcc3936SMike Snitzer 	(void) __dm_resume(md, NULL);
2628ffcc3936SMike Snitzer 
2629ffcc3936SMike Snitzer done:
2630ffcc3936SMike Snitzer 	clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2631ffcc3936SMike Snitzer 	smp_mb__after_atomic();
2632ffcc3936SMike Snitzer 	wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2633ffcc3936SMike Snitzer }
2634ffcc3936SMike Snitzer 
2635ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md)
2636fd2ed4d2SMikulas Patocka {
2637fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
2638ffcc3936SMike Snitzer 	__dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2639ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2640ffcc3936SMike Snitzer }
2641ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
2642ffcc3936SMike Snitzer 
2643ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md)
2644ffcc3936SMike Snitzer {
2645ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2646ffcc3936SMike Snitzer 	__dm_internal_resume(md);
2647ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2648ffcc3936SMike Snitzer }
2649ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume);
2650ffcc3936SMike Snitzer 
2651ffcc3936SMike Snitzer /*
2652ffcc3936SMike Snitzer  * Fast variants of internal suspend/resume hold md->suspend_lock,
2653ffcc3936SMike Snitzer  * which prevents interaction with userspace-driven suspend.
2654ffcc3936SMike Snitzer  */
2655ffcc3936SMike Snitzer 
2656ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md)
2657ffcc3936SMike Snitzer {
2658ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2659ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2660fd2ed4d2SMikulas Patocka 		return;
2661fd2ed4d2SMikulas Patocka 
2662fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2663fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
2664fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
2665fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2666fd2ed4d2SMikulas Patocka }
2667b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
2668fd2ed4d2SMikulas Patocka 
2669ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md)
2670fd2ed4d2SMikulas Patocka {
2671ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2672fd2ed4d2SMikulas Patocka 		goto done;
2673fd2ed4d2SMikulas Patocka 
2674fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
2675fd2ed4d2SMikulas Patocka 
2676fd2ed4d2SMikulas Patocka done:
2677fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
2678fd2ed4d2SMikulas Patocka }
2679b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
2680fd2ed4d2SMikulas Patocka 
26811da177e4SLinus Torvalds /*-----------------------------------------------------------------
26821da177e4SLinus Torvalds  * Event notification.
26831da177e4SLinus Torvalds  *---------------------------------------------------------------*/
26843abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
268560935eb2SMilan Broz 		       unsigned cookie)
268669267a30SAlasdair G Kergon {
26876958c1c6SMikulas Patocka 	int r;
26886958c1c6SMikulas Patocka 	unsigned noio_flag;
268960935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
269060935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
269160935eb2SMilan Broz 
26926958c1c6SMikulas Patocka 	noio_flag = memalloc_noio_save();
26936958c1c6SMikulas Patocka 
269460935eb2SMilan Broz 	if (!cookie)
26956958c1c6SMikulas Patocka 		r = kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
269660935eb2SMilan Broz 	else {
269760935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
269860935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
26996958c1c6SMikulas Patocka 		r = kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
27003abf85b5SPeter Rajnoha 				       action, envp);
270160935eb2SMilan Broz 	}
27026958c1c6SMikulas Patocka 
27036958c1c6SMikulas Patocka 	memalloc_noio_restore(noio_flag);
27046958c1c6SMikulas Patocka 
27056958c1c6SMikulas Patocka 	return r;
270669267a30SAlasdair G Kergon }
270769267a30SAlasdair G Kergon 
27087a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
27097a8c3d3bSMike Anderson {
27107a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
27117a8c3d3bSMike Anderson }
27127a8c3d3bSMike Anderson 
27131da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
27141da177e4SLinus Torvalds {
27151da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
27161da177e4SLinus Torvalds }
27171da177e4SLinus Torvalds 
27181da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
27191da177e4SLinus Torvalds {
27201da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
27211da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
27221da177e4SLinus Torvalds }
27231da177e4SLinus Torvalds 
27247a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
27257a8c3d3bSMike Anderson {
27267a8c3d3bSMike Anderson 	unsigned long flags;
27277a8c3d3bSMike Anderson 
27287a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
27297a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
27307a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
27317a8c3d3bSMike Anderson }
27327a8c3d3bSMike Anderson 
27331da177e4SLinus Torvalds /*
27341da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
27351da177e4SLinus Torvalds  * count on 'md'.
27361da177e4SLinus Torvalds  */
27371da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
27381da177e4SLinus Torvalds {
27391da177e4SLinus Torvalds 	return md->disk;
27401da177e4SLinus Torvalds }
274165ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk);
27421da177e4SLinus Torvalds 
2743784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2744784aae73SMilan Broz {
27452995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
2746784aae73SMilan Broz }
2747784aae73SMilan Broz 
2748784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2749784aae73SMilan Broz {
2750784aae73SMilan Broz 	struct mapped_device *md;
2751784aae73SMilan Broz 
27522995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
2753784aae73SMilan Broz 
2754b9a41d21SHou Tao 	spin_lock(&_minor_lock);
2755b9a41d21SHou Tao 	if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2756b9a41d21SHou Tao 		md = NULL;
2757b9a41d21SHou Tao 		goto out;
2758b9a41d21SHou Tao 	}
2759784aae73SMilan Broz 	dm_get(md);
2760b9a41d21SHou Tao out:
2761b9a41d21SHou Tao 	spin_unlock(&_minor_lock);
2762b9a41d21SHou Tao 
2763784aae73SMilan Broz 	return md;
2764784aae73SMilan Broz }
2765784aae73SMilan Broz 
27664f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
27671da177e4SLinus Torvalds {
27681da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
27691da177e4SLinus Torvalds }
27701da177e4SLinus Torvalds 
27715df96f2bSMikulas Patocka static int dm_post_suspending_md(struct mapped_device *md)
27725df96f2bSMikulas Patocka {
27735df96f2bSMikulas Patocka 	return test_bit(DMF_POST_SUSPENDING, &md->flags);
27745df96f2bSMikulas Patocka }
27755df96f2bSMikulas Patocka 
2776ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md)
2777ffcc3936SMike Snitzer {
2778ffcc3936SMike Snitzer 	return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2779ffcc3936SMike Snitzer }
2780ffcc3936SMike Snitzer 
27812c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
27822c140a24SMikulas Patocka {
27832c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
27842c140a24SMikulas Patocka }
27852c140a24SMikulas Patocka 
278664dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
278764dbce58SKiyoshi Ueda {
278833bd6f06SMike Snitzer 	return dm_suspended_md(ti->table->md);
278964dbce58SKiyoshi Ueda }
279064dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
279164dbce58SKiyoshi Ueda 
27925df96f2bSMikulas Patocka int dm_post_suspending(struct dm_target *ti)
27935df96f2bSMikulas Patocka {
279433bd6f06SMike Snitzer 	return dm_post_suspending_md(ti->table->md);
27955df96f2bSMikulas Patocka }
27965df96f2bSMikulas Patocka EXPORT_SYMBOL_GPL(dm_post_suspending);
27975df96f2bSMikulas Patocka 
27982e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
27992e93ccc1SKiyoshi Ueda {
280033bd6f06SMike Snitzer 	return __noflush_suspending(ti->table->md);
28012e93ccc1SKiyoshi Ueda }
28022e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
28032e93ccc1SKiyoshi Ueda 
28047e0d574fSBart Van Assche struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type,
28050776aa0eSMike Snitzer 					    unsigned integrity, unsigned per_io_data_size,
28060776aa0eSMike Snitzer 					    unsigned min_pool_size)
2807e6ee8c0bSKiyoshi Ueda {
2808115485e8SMike Snitzer 	struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
280978d8e58aSMike Snitzer 	unsigned int pool_size = 0;
281064f52b0eSMike Snitzer 	unsigned int front_pad, io_front_pad;
28116f1c819cSKent Overstreet 	int ret;
2812e6ee8c0bSKiyoshi Ueda 
2813e6ee8c0bSKiyoshi Ueda 	if (!pools)
28144e6e36c3SMike Snitzer 		return NULL;
2815e6ee8c0bSKiyoshi Ueda 
281678d8e58aSMike Snitzer 	switch (type) {
281778d8e58aSMike Snitzer 	case DM_TYPE_BIO_BASED:
2818545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
28190776aa0eSMike Snitzer 		pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size);
282062f26317SJeffle Xu 		front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + DM_TARGET_IO_BIO_OFFSET;
282162f26317SJeffle Xu 		io_front_pad = roundup(per_io_data_size,  __alignof__(struct dm_io)) + DM_IO_BIO_OFFSET;
28226f1c819cSKent Overstreet 		ret = bioset_init(&pools->io_bs, pool_size, io_front_pad, 0);
28236f1c819cSKent Overstreet 		if (ret)
282464f52b0eSMike Snitzer 			goto out;
28256f1c819cSKent Overstreet 		if (integrity && bioset_integrity_create(&pools->io_bs, pool_size))
2826eb8db831SChristoph Hellwig 			goto out;
282778d8e58aSMike Snitzer 		break;
282878d8e58aSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
28290776aa0eSMike Snitzer 		pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size);
283078d8e58aSMike Snitzer 		front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
2831591ddcfcSMike Snitzer 		/* per_io_data_size is used for blk-mq pdu at queue allocation */
283278d8e58aSMike Snitzer 		break;
283378d8e58aSMike Snitzer 	default:
283478d8e58aSMike Snitzer 		BUG();
283578d8e58aSMike Snitzer 	}
283678d8e58aSMike Snitzer 
28376f1c819cSKent Overstreet 	ret = bioset_init(&pools->bs, pool_size, front_pad, 0);
28386f1c819cSKent Overstreet 	if (ret)
28395f015204SJun'ichi Nomura 		goto out;
2840e6ee8c0bSKiyoshi Ueda 
28416f1c819cSKent Overstreet 	if (integrity && bioset_integrity_create(&pools->bs, pool_size))
28425f015204SJun'ichi Nomura 		goto out;
2843a91a2785SMartin K. Petersen 
2844e6ee8c0bSKiyoshi Ueda 	return pools;
284578d8e58aSMike Snitzer 
28465f015204SJun'ichi Nomura out:
28475f015204SJun'ichi Nomura 	dm_free_md_mempools(pools);
2848e6ee8c0bSKiyoshi Ueda 
28494e6e36c3SMike Snitzer 	return NULL;
2850e6ee8c0bSKiyoshi Ueda }
2851e6ee8c0bSKiyoshi Ueda 
2852e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
2853e6ee8c0bSKiyoshi Ueda {
2854e6ee8c0bSKiyoshi Ueda 	if (!pools)
2855e6ee8c0bSKiyoshi Ueda 		return;
2856e6ee8c0bSKiyoshi Ueda 
28576f1c819cSKent Overstreet 	bioset_exit(&pools->bs);
28586f1c819cSKent Overstreet 	bioset_exit(&pools->io_bs);
2859e6ee8c0bSKiyoshi Ueda 
2860e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2861e6ee8c0bSKiyoshi Ueda }
2862e6ee8c0bSKiyoshi Ueda 
28639c72bad1SChristoph Hellwig struct dm_pr {
28649c72bad1SChristoph Hellwig 	u64	old_key;
28659c72bad1SChristoph Hellwig 	u64	new_key;
28669c72bad1SChristoph Hellwig 	u32	flags;
28679c72bad1SChristoph Hellwig 	bool	fail_early;
28689c72bad1SChristoph Hellwig };
28699c72bad1SChristoph Hellwig 
28709c72bad1SChristoph Hellwig static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
28719c72bad1SChristoph Hellwig 		      void *data)
28729c72bad1SChristoph Hellwig {
28739c72bad1SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
28749c72bad1SChristoph Hellwig 	struct dm_table *table;
28759c72bad1SChristoph Hellwig 	struct dm_target *ti;
28769c72bad1SChristoph Hellwig 	int ret = -ENOTTY, srcu_idx;
28779c72bad1SChristoph Hellwig 
28789c72bad1SChristoph Hellwig 	table = dm_get_live_table(md, &srcu_idx);
28799c72bad1SChristoph Hellwig 	if (!table || !dm_table_get_size(table))
28809c72bad1SChristoph Hellwig 		goto out;
28819c72bad1SChristoph Hellwig 
28829c72bad1SChristoph Hellwig 	/* We only support devices that have a single target */
28839c72bad1SChristoph Hellwig 	if (dm_table_get_num_targets(table) != 1)
28849c72bad1SChristoph Hellwig 		goto out;
28859c72bad1SChristoph Hellwig 	ti = dm_table_get_target(table, 0);
28869c72bad1SChristoph Hellwig 
28879c72bad1SChristoph Hellwig 	ret = -EINVAL;
28889c72bad1SChristoph Hellwig 	if (!ti->type->iterate_devices)
28899c72bad1SChristoph Hellwig 		goto out;
28909c72bad1SChristoph Hellwig 
28919c72bad1SChristoph Hellwig 	ret = ti->type->iterate_devices(ti, fn, data);
28929c72bad1SChristoph Hellwig out:
28939c72bad1SChristoph Hellwig 	dm_put_live_table(md, srcu_idx);
28949c72bad1SChristoph Hellwig 	return ret;
28959c72bad1SChristoph Hellwig }
28969c72bad1SChristoph Hellwig 
28979c72bad1SChristoph Hellwig /*
28989c72bad1SChristoph Hellwig  * For register / unregister we need to manually call out to every path.
28999c72bad1SChristoph Hellwig  */
29009c72bad1SChristoph Hellwig static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
29019c72bad1SChristoph Hellwig 			    sector_t start, sector_t len, void *data)
29029c72bad1SChristoph Hellwig {
29039c72bad1SChristoph Hellwig 	struct dm_pr *pr = data;
29049c72bad1SChristoph Hellwig 	const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
29059c72bad1SChristoph Hellwig 
29069c72bad1SChristoph Hellwig 	if (!ops || !ops->pr_register)
29079c72bad1SChristoph Hellwig 		return -EOPNOTSUPP;
29089c72bad1SChristoph Hellwig 	return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
29099c72bad1SChristoph Hellwig }
29109c72bad1SChristoph Hellwig 
291171cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
291271cdb697SChristoph Hellwig 			  u32 flags)
291371cdb697SChristoph Hellwig {
29149c72bad1SChristoph Hellwig 	struct dm_pr pr = {
29159c72bad1SChristoph Hellwig 		.old_key	= old_key,
29169c72bad1SChristoph Hellwig 		.new_key	= new_key,
29179c72bad1SChristoph Hellwig 		.flags		= flags,
29189c72bad1SChristoph Hellwig 		.fail_early	= true,
29199c72bad1SChristoph Hellwig 	};
29209c72bad1SChristoph Hellwig 	int ret;
292171cdb697SChristoph Hellwig 
29229c72bad1SChristoph Hellwig 	ret = dm_call_pr(bdev, __dm_pr_register, &pr);
29239c72bad1SChristoph Hellwig 	if (ret && new_key) {
29249c72bad1SChristoph Hellwig 		/* unregister all paths if we failed to register any path */
29259c72bad1SChristoph Hellwig 		pr.old_key = new_key;
29269c72bad1SChristoph Hellwig 		pr.new_key = 0;
29279c72bad1SChristoph Hellwig 		pr.flags = 0;
29289c72bad1SChristoph Hellwig 		pr.fail_early = false;
29299c72bad1SChristoph Hellwig 		dm_call_pr(bdev, __dm_pr_register, &pr);
29309c72bad1SChristoph Hellwig 	}
293171cdb697SChristoph Hellwig 
29329c72bad1SChristoph Hellwig 	return ret;
293371cdb697SChristoph Hellwig }
293471cdb697SChristoph Hellwig 
293571cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
293671cdb697SChristoph Hellwig 			 u32 flags)
293771cdb697SChristoph Hellwig {
293871cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
293971cdb697SChristoph Hellwig 	const struct pr_ops *ops;
2940971888c4SMike Snitzer 	int r, srcu_idx;
294171cdb697SChristoph Hellwig 
29425bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
294371cdb697SChristoph Hellwig 	if (r < 0)
2944971888c4SMike Snitzer 		goto out;
294571cdb697SChristoph Hellwig 
294671cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
294771cdb697SChristoph Hellwig 	if (ops && ops->pr_reserve)
294871cdb697SChristoph Hellwig 		r = ops->pr_reserve(bdev, key, type, flags);
294971cdb697SChristoph Hellwig 	else
295071cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
2951971888c4SMike Snitzer out:
2952971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
295371cdb697SChristoph Hellwig 	return r;
295471cdb697SChristoph Hellwig }
295571cdb697SChristoph Hellwig 
295671cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
295771cdb697SChristoph Hellwig {
295871cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
295971cdb697SChristoph Hellwig 	const struct pr_ops *ops;
2960971888c4SMike Snitzer 	int r, srcu_idx;
296171cdb697SChristoph Hellwig 
29625bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
296371cdb697SChristoph Hellwig 	if (r < 0)
2964971888c4SMike Snitzer 		goto out;
296571cdb697SChristoph Hellwig 
296671cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
296771cdb697SChristoph Hellwig 	if (ops && ops->pr_release)
296871cdb697SChristoph Hellwig 		r = ops->pr_release(bdev, key, type);
296971cdb697SChristoph Hellwig 	else
297071cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
2971971888c4SMike Snitzer out:
2972971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
297371cdb697SChristoph Hellwig 	return r;
297471cdb697SChristoph Hellwig }
297571cdb697SChristoph Hellwig 
297671cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
297771cdb697SChristoph Hellwig 			 enum pr_type type, bool abort)
297871cdb697SChristoph Hellwig {
297971cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
298071cdb697SChristoph Hellwig 	const struct pr_ops *ops;
2981971888c4SMike Snitzer 	int r, srcu_idx;
298271cdb697SChristoph Hellwig 
29835bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
298471cdb697SChristoph Hellwig 	if (r < 0)
2985971888c4SMike Snitzer 		goto out;
298671cdb697SChristoph Hellwig 
298771cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
298871cdb697SChristoph Hellwig 	if (ops && ops->pr_preempt)
298971cdb697SChristoph Hellwig 		r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
299071cdb697SChristoph Hellwig 	else
299171cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
2992971888c4SMike Snitzer out:
2993971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
299471cdb697SChristoph Hellwig 	return r;
299571cdb697SChristoph Hellwig }
299671cdb697SChristoph Hellwig 
299771cdb697SChristoph Hellwig static int dm_pr_clear(struct block_device *bdev, u64 key)
299871cdb697SChristoph Hellwig {
299971cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
300071cdb697SChristoph Hellwig 	const struct pr_ops *ops;
3001971888c4SMike Snitzer 	int r, srcu_idx;
300271cdb697SChristoph Hellwig 
30035bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
300471cdb697SChristoph Hellwig 	if (r < 0)
3005971888c4SMike Snitzer 		goto out;
300671cdb697SChristoph Hellwig 
300771cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
300871cdb697SChristoph Hellwig 	if (ops && ops->pr_clear)
300971cdb697SChristoph Hellwig 		r = ops->pr_clear(bdev, key);
301071cdb697SChristoph Hellwig 	else
301171cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
3012971888c4SMike Snitzer out:
3013971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
301471cdb697SChristoph Hellwig 	return r;
301571cdb697SChristoph Hellwig }
301671cdb697SChristoph Hellwig 
301771cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = {
301871cdb697SChristoph Hellwig 	.pr_register	= dm_pr_register,
301971cdb697SChristoph Hellwig 	.pr_reserve	= dm_pr_reserve,
302071cdb697SChristoph Hellwig 	.pr_release	= dm_pr_release,
302171cdb697SChristoph Hellwig 	.pr_preempt	= dm_pr_preempt,
302271cdb697SChristoph Hellwig 	.pr_clear	= dm_pr_clear,
302371cdb697SChristoph Hellwig };
302471cdb697SChristoph Hellwig 
302583d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
3026c62b37d9SChristoph Hellwig 	.submit_bio = dm_submit_bio,
30271da177e4SLinus Torvalds 	.open = dm_blk_open,
30281da177e4SLinus Torvalds 	.release = dm_blk_close,
3029aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
30303ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
3031e76239a3SChristoph Hellwig 	.report_zones = dm_blk_report_zones,
303271cdb697SChristoph Hellwig 	.pr_ops = &dm_pr_ops,
30331da177e4SLinus Torvalds 	.owner = THIS_MODULE
30341da177e4SLinus Torvalds };
30351da177e4SLinus Torvalds 
3036681cc5e8SMike Snitzer static const struct block_device_operations dm_rq_blk_dops = {
3037681cc5e8SMike Snitzer 	.open = dm_blk_open,
3038681cc5e8SMike Snitzer 	.release = dm_blk_close,
3039681cc5e8SMike Snitzer 	.ioctl = dm_blk_ioctl,
3040681cc5e8SMike Snitzer 	.getgeo = dm_blk_getgeo,
3041681cc5e8SMike Snitzer 	.pr_ops = &dm_pr_ops,
3042681cc5e8SMike Snitzer 	.owner = THIS_MODULE
3043681cc5e8SMike Snitzer };
3044681cc5e8SMike Snitzer 
3045f26c5719SDan Williams static const struct dax_operations dm_dax_ops = {
3046f26c5719SDan Williams 	.direct_access = dm_dax_direct_access,
30477bf7eac8SDan Williams 	.dax_supported = dm_dax_supported,
30487e026c8cSDan Williams 	.copy_from_iter = dm_dax_copy_from_iter,
3049b3a9a0c3SDan Williams 	.copy_to_iter = dm_dax_copy_to_iter,
3050cdf6cdcdSVivek Goyal 	.zero_page_range = dm_dax_zero_page_range,
3051f26c5719SDan Williams };
3052f26c5719SDan Williams 
30531da177e4SLinus Torvalds /*
30541da177e4SLinus Torvalds  * module hooks
30551da177e4SLinus Torvalds  */
30561da177e4SLinus Torvalds module_init(dm_init);
30571da177e4SLinus Torvalds module_exit(dm_exit);
30581da177e4SLinus Torvalds 
30591da177e4SLinus Torvalds module_param(major, uint, 0);
30601da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
3061f4790826SMike Snitzer 
3062e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3063e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3064e8603136SMike Snitzer 
3065115485e8SMike Snitzer module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
3066115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
3067115485e8SMike Snitzer 
3068a666e5c0SMikulas Patocka module_param(swap_bios, int, S_IRUGO | S_IWUSR);
3069a666e5c0SMikulas Patocka MODULE_PARM_DESC(swap_bios, "Maximum allowed inflight swap IOs");
3070a666e5c0SMikulas Patocka 
30711da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
30721da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
30731da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3074