xref: /openbmc/linux/drivers/md/dm.c (revision f5b4aee10c024a35901cc2f1b9b4d80986a93937)
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 
826c23f0bdSChristoph Hellwig static inline struct dm_target_io *clone_to_tio(struct bio *clone)
836c23f0bdSChristoph Hellwig {
846c23f0bdSChristoph Hellwig 	return container_of(clone, struct dm_target_io, clone);
856c23f0bdSChristoph Hellwig }
866c23f0bdSChristoph Hellwig 
8764f52b0eSMike Snitzer void *dm_per_bio_data(struct bio *bio, size_t data_size)
8864f52b0eSMike Snitzer {
896c23f0bdSChristoph Hellwig 	if (!clone_to_tio(bio)->inside_dm_io)
9062f26317SJeffle Xu 		return (char *)bio - DM_TARGET_IO_BIO_OFFSET - data_size;
9162f26317SJeffle Xu 	return (char *)bio - DM_IO_BIO_OFFSET - data_size;
9264f52b0eSMike Snitzer }
9364f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_per_bio_data);
9464f52b0eSMike Snitzer 
9564f52b0eSMike Snitzer struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size)
9664f52b0eSMike Snitzer {
9764f52b0eSMike Snitzer 	struct dm_io *io = (struct dm_io *)((char *)data + data_size);
9864f52b0eSMike Snitzer 	if (io->magic == DM_IO_MAGIC)
9962f26317SJeffle Xu 		return (struct bio *)((char *)io + DM_IO_BIO_OFFSET);
10064f52b0eSMike Snitzer 	BUG_ON(io->magic != DM_TIO_MAGIC);
10162f26317SJeffle Xu 	return (struct bio *)((char *)io + DM_TARGET_IO_BIO_OFFSET);
10264f52b0eSMike Snitzer }
10364f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_from_per_bio_data);
10464f52b0eSMike Snitzer 
10564f52b0eSMike Snitzer unsigned dm_bio_get_target_bio_nr(const struct bio *bio)
10664f52b0eSMike Snitzer {
10764f52b0eSMike Snitzer 	return container_of(bio, struct dm_target_io, clone)->target_bio_nr;
10864f52b0eSMike Snitzer }
10964f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_get_target_bio_nr);
11064f52b0eSMike Snitzer 
111ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
112ba61fdd1SJeff Mahoney 
113115485e8SMike Snitzer #define DM_NUMA_NODE NUMA_NO_NODE
114115485e8SMike Snitzer static int dm_numa_node = DM_NUMA_NODE;
115faad87dfSMike Snitzer 
116a666e5c0SMikulas Patocka #define DEFAULT_SWAP_BIOS	(8 * 1048576 / PAGE_SIZE)
117a666e5c0SMikulas Patocka static int swap_bios = DEFAULT_SWAP_BIOS;
118a666e5c0SMikulas Patocka static int get_swap_bios(void)
119a666e5c0SMikulas Patocka {
120a666e5c0SMikulas Patocka 	int latch = READ_ONCE(swap_bios);
121a666e5c0SMikulas Patocka 	if (unlikely(latch <= 0))
122a666e5c0SMikulas Patocka 		latch = DEFAULT_SWAP_BIOS;
123a666e5c0SMikulas Patocka 	return latch;
124a666e5c0SMikulas Patocka }
125a666e5c0SMikulas Patocka 
126e6ee8c0bSKiyoshi Ueda /*
127e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
128e6ee8c0bSKiyoshi Ueda  */
129e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
1306f1c819cSKent Overstreet 	struct bio_set bs;
1316f1c819cSKent Overstreet 	struct bio_set io_bs;
132e6ee8c0bSKiyoshi Ueda };
133e6ee8c0bSKiyoshi Ueda 
13486f1152bSBenjamin Marzinski struct table_device {
13586f1152bSBenjamin Marzinski 	struct list_head list;
136b0b4d7c6SElena Reshetova 	refcount_t count;
13786f1152bSBenjamin Marzinski 	struct dm_dev dm_dev;
13886f1152bSBenjamin Marzinski };
13986f1152bSBenjamin Marzinski 
140f4790826SMike Snitzer /*
141e8603136SMike Snitzer  * Bio-based DM's mempools' reserved IOs set by the user.
142e8603136SMike Snitzer  */
1434cc96131SMike Snitzer #define RESERVED_BIO_BASED_IOS		16
144e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
145e8603136SMike Snitzer 
146115485e8SMike Snitzer static int __dm_get_module_param_int(int *module_param, int min, int max)
147115485e8SMike Snitzer {
1486aa7de05SMark Rutland 	int param = READ_ONCE(*module_param);
149115485e8SMike Snitzer 	int modified_param = 0;
150115485e8SMike Snitzer 	bool modified = true;
151115485e8SMike Snitzer 
152115485e8SMike Snitzer 	if (param < min)
153115485e8SMike Snitzer 		modified_param = min;
154115485e8SMike Snitzer 	else if (param > max)
155115485e8SMike Snitzer 		modified_param = max;
156115485e8SMike Snitzer 	else
157115485e8SMike Snitzer 		modified = false;
158115485e8SMike Snitzer 
159115485e8SMike Snitzer 	if (modified) {
160115485e8SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
161115485e8SMike Snitzer 		param = modified_param;
162115485e8SMike Snitzer 	}
163115485e8SMike Snitzer 
164115485e8SMike Snitzer 	return param;
165115485e8SMike Snitzer }
166115485e8SMike Snitzer 
1674cc96131SMike Snitzer unsigned __dm_get_module_param(unsigned *module_param,
168f4790826SMike Snitzer 			       unsigned def, unsigned max)
169f4790826SMike Snitzer {
1706aa7de05SMark Rutland 	unsigned param = READ_ONCE(*module_param);
17109c2d531SMike Snitzer 	unsigned modified_param = 0;
172f4790826SMike Snitzer 
17309c2d531SMike Snitzer 	if (!param)
17409c2d531SMike Snitzer 		modified_param = def;
17509c2d531SMike Snitzer 	else if (param > max)
17609c2d531SMike Snitzer 		modified_param = max;
177f4790826SMike Snitzer 
17809c2d531SMike Snitzer 	if (modified_param) {
17909c2d531SMike Snitzer 		(void)cmpxchg(module_param, param, modified_param);
18009c2d531SMike Snitzer 		param = modified_param;
181f4790826SMike Snitzer 	}
182f4790826SMike Snitzer 
18309c2d531SMike Snitzer 	return param;
184f4790826SMike Snitzer }
185f4790826SMike Snitzer 
186e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void)
187e8603136SMike Snitzer {
18809c2d531SMike Snitzer 	return __dm_get_module_param(&reserved_bio_based_ios,
1894cc96131SMike Snitzer 				     RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS);
190e8603136SMike Snitzer }
191e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
192e8603136SMike Snitzer 
193115485e8SMike Snitzer static unsigned dm_get_numa_node(void)
194115485e8SMike Snitzer {
195115485e8SMike Snitzer 	return __dm_get_module_param_int(&dm_numa_node,
196115485e8SMike Snitzer 					 DM_NUMA_NODE, num_online_nodes() - 1);
197115485e8SMike Snitzer }
198115485e8SMike Snitzer 
1991da177e4SLinus Torvalds static int __init local_init(void)
2001da177e4SLinus Torvalds {
201e689fbabSMike Snitzer 	int r;
2021ae49ea2SMike Snitzer 
20351e5b2bdSMike Anderson 	r = dm_uevent_init();
20451157b4aSKiyoshi Ueda 	if (r)
205e689fbabSMike Snitzer 		return r;
20651e5b2bdSMike Anderson 
207acfe0ad7SMikulas Patocka 	deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
208acfe0ad7SMikulas Patocka 	if (!deferred_remove_workqueue) {
209acfe0ad7SMikulas Patocka 		r = -ENOMEM;
210acfe0ad7SMikulas Patocka 		goto out_uevent_exit;
211acfe0ad7SMikulas Patocka 	}
212acfe0ad7SMikulas Patocka 
2131da177e4SLinus Torvalds 	_major = major;
2141da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
21551157b4aSKiyoshi Ueda 	if (r < 0)
216acfe0ad7SMikulas Patocka 		goto out_free_workqueue;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	if (!_major)
2191da177e4SLinus Torvalds 		_major = r;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	return 0;
22251157b4aSKiyoshi Ueda 
223acfe0ad7SMikulas Patocka out_free_workqueue:
224acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
22551157b4aSKiyoshi Ueda out_uevent_exit:
22651157b4aSKiyoshi Ueda 	dm_uevent_exit();
22751157b4aSKiyoshi Ueda 
22851157b4aSKiyoshi Ueda 	return r;
2291da177e4SLinus Torvalds }
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds static void local_exit(void)
2321da177e4SLinus Torvalds {
2332c140a24SMikulas Patocka 	flush_scheduled_work();
234acfe0ad7SMikulas Patocka 	destroy_workqueue(deferred_remove_workqueue);
2352c140a24SMikulas Patocka 
23600d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
23751e5b2bdSMike Anderson 	dm_uevent_exit();
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	_major = 0;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	DMINFO("cleaned up");
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds 
244b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
2451da177e4SLinus Torvalds 	local_init,
2461da177e4SLinus Torvalds 	dm_target_init,
2471da177e4SLinus Torvalds 	dm_linear_init,
2481da177e4SLinus Torvalds 	dm_stripe_init,
249952b3557SMikulas Patocka 	dm_io_init,
250945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2511da177e4SLinus Torvalds 	dm_interface_init,
252fd2ed4d2SMikulas Patocka 	dm_statistics_init,
2531da177e4SLinus Torvalds };
2541da177e4SLinus Torvalds 
255b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2561da177e4SLinus Torvalds 	local_exit,
2571da177e4SLinus Torvalds 	dm_target_exit,
2581da177e4SLinus Torvalds 	dm_linear_exit,
2591da177e4SLinus Torvalds 	dm_stripe_exit,
260952b3557SMikulas Patocka 	dm_io_exit,
261945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
2621da177e4SLinus Torvalds 	dm_interface_exit,
263fd2ed4d2SMikulas Patocka 	dm_statistics_exit,
2641da177e4SLinus Torvalds };
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds static int __init dm_init(void)
2671da177e4SLinus Torvalds {
2681da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
2691da177e4SLinus Torvalds 	int r, i;
2701da177e4SLinus Torvalds 
271f1cd6cb2STushar Sugandhi #if (IS_ENABLED(CONFIG_IMA) && !IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE))
272f1cd6cb2STushar Sugandhi 	DMWARN("CONFIG_IMA_DISABLE_HTABLE is disabled."
273f1cd6cb2STushar Sugandhi 	       " Duplicate IMA measurements will not be recorded in the IMA log.");
274f1cd6cb2STushar Sugandhi #endif
275f1cd6cb2STushar Sugandhi 
2761da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
2771da177e4SLinus Torvalds 		r = _inits[i]();
2781da177e4SLinus Torvalds 		if (r)
2791da177e4SLinus Torvalds 			goto bad;
2801da177e4SLinus Torvalds 	}
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	return 0;
2831da177e4SLinus Torvalds bad:
2841da177e4SLinus Torvalds 	while (i--)
2851da177e4SLinus Torvalds 		_exits[i]();
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	return r;
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds static void __exit dm_exit(void)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	while (i--)
2951da177e4SLinus Torvalds 		_exits[i]();
296d15b774cSAlasdair G Kergon 
297d15b774cSAlasdair G Kergon 	/*
298d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
299d15b774cSAlasdair G Kergon 	 */
300d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds /*
3041da177e4SLinus Torvalds  * Block device functions
3051da177e4SLinus Torvalds  */
306432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
307432a212cSMike Anderson {
308432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
309432a212cSMike Anderson }
310432a212cSMike Anderson 
311fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3121da177e4SLinus Torvalds {
3131da177e4SLinus Torvalds 	struct mapped_device *md;
3141da177e4SLinus Torvalds 
315fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
316fba9f90eSJeff Mahoney 
317fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
318fba9f90eSJeff Mahoney 	if (!md)
319fba9f90eSJeff Mahoney 		goto out;
320fba9f90eSJeff Mahoney 
3215c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
322432a212cSMike Anderson 	    dm_deleting_md(md)) {
323fba9f90eSJeff Mahoney 		md = NULL;
324fba9f90eSJeff Mahoney 		goto out;
325fba9f90eSJeff Mahoney 	}
326fba9f90eSJeff Mahoney 
3271da177e4SLinus Torvalds 	dm_get(md);
3285c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
329fba9f90eSJeff Mahoney out:
330fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
331fba9f90eSJeff Mahoney 
332fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds 
335db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode)
3361da177e4SLinus Torvalds {
33763a4f065SMike Snitzer 	struct mapped_device *md;
3386e9624b8SArnd Bergmann 
3394a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
3404a1aeb98SMilan Broz 
34163a4f065SMike Snitzer 	md = disk->private_data;
34263a4f065SMike Snitzer 	if (WARN_ON(!md))
34363a4f065SMike Snitzer 		goto out;
34463a4f065SMike Snitzer 
3452c140a24SMikulas Patocka 	if (atomic_dec_and_test(&md->open_count) &&
3462c140a24SMikulas Patocka 	    (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
347acfe0ad7SMikulas Patocka 		queue_work(deferred_remove_workqueue, &deferred_remove_work);
3482c140a24SMikulas Patocka 
3491da177e4SLinus Torvalds 	dm_put(md);
35063a4f065SMike Snitzer out:
3514a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
3545c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3555c6bd75dSAlasdair G Kergon {
3565c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3575c6bd75dSAlasdair G Kergon }
3585c6bd75dSAlasdair G Kergon 
3595c6bd75dSAlasdair G Kergon /*
3605c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3615c6bd75dSAlasdair G Kergon  */
3622c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
3635c6bd75dSAlasdair G Kergon {
3645c6bd75dSAlasdair G Kergon 	int r = 0;
3655c6bd75dSAlasdair G Kergon 
3665c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
3675c6bd75dSAlasdair G Kergon 
3682c140a24SMikulas Patocka 	if (dm_open_count(md)) {
3695c6bd75dSAlasdair G Kergon 		r = -EBUSY;
3702c140a24SMikulas Patocka 		if (mark_deferred)
3712c140a24SMikulas Patocka 			set_bit(DMF_DEFERRED_REMOVE, &md->flags);
3722c140a24SMikulas Patocka 	} else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
3732c140a24SMikulas Patocka 		r = -EEXIST;
3745c6bd75dSAlasdair G Kergon 	else
3755c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
3765c6bd75dSAlasdair G Kergon 
3775c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
3785c6bd75dSAlasdair G Kergon 
3795c6bd75dSAlasdair G Kergon 	return r;
3805c6bd75dSAlasdair G Kergon }
3815c6bd75dSAlasdair G Kergon 
3822c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md)
3832c140a24SMikulas Patocka {
3842c140a24SMikulas Patocka 	int r = 0;
3852c140a24SMikulas Patocka 
3862c140a24SMikulas Patocka 	spin_lock(&_minor_lock);
3872c140a24SMikulas Patocka 
3882c140a24SMikulas Patocka 	if (test_bit(DMF_DELETING, &md->flags))
3892c140a24SMikulas Patocka 		r = -EBUSY;
3902c140a24SMikulas Patocka 	else
3912c140a24SMikulas Patocka 		clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
3922c140a24SMikulas Patocka 
3932c140a24SMikulas Patocka 	spin_unlock(&_minor_lock);
3942c140a24SMikulas Patocka 
3952c140a24SMikulas Patocka 	return r;
3962c140a24SMikulas Patocka }
3972c140a24SMikulas Patocka 
3982c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w)
3992c140a24SMikulas Patocka {
4002c140a24SMikulas Patocka 	dm_deferred_remove();
4012c140a24SMikulas Patocka }
4022c140a24SMikulas Patocka 
4033ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4043ac51e74SDarrick J. Wong {
4053ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
4063ac51e74SDarrick J. Wong 
4073ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
4083ac51e74SDarrick J. Wong }
4093ac51e74SDarrick J. Wong 
410971888c4SMike Snitzer static int dm_prepare_ioctl(struct mapped_device *md, int *srcu_idx,
4115bd5e8d8SMike Snitzer 			    struct block_device **bdev)
412aa129a22SMilan Broz {
41366482026SMike Snitzer 	struct dm_target *tgt;
4146c182cd8SHannes Reinecke 	struct dm_table *map;
415971888c4SMike Snitzer 	int r;
416aa129a22SMilan Broz 
4176c182cd8SHannes Reinecke retry:
418e56f81e0SChristoph Hellwig 	r = -ENOTTY;
419971888c4SMike Snitzer 	map = dm_get_live_table(md, srcu_idx);
420aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
421971888c4SMike Snitzer 		return r;
422aa129a22SMilan Broz 
423aa129a22SMilan Broz 	/* We only support devices that have a single target */
424aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
425971888c4SMike Snitzer 		return r;
426aa129a22SMilan Broz 
42766482026SMike Snitzer 	tgt = dm_table_get_target(map, 0);
42866482026SMike Snitzer 	if (!tgt->type->prepare_ioctl)
429e56f81e0SChristoph Hellwig 		return r;
430aa129a22SMilan Broz 
431971888c4SMike Snitzer 	if (dm_suspended_md(md))
432971888c4SMike Snitzer 		return -EAGAIN;
433971888c4SMike Snitzer 
4345bd5e8d8SMike Snitzer 	r = tgt->type->prepare_ioctl(tgt, bdev);
4355bbbfdf6SJunichi Nomura 	if (r == -ENOTCONN && !fatal_signal_pending(current)) {
436971888c4SMike Snitzer 		dm_put_live_table(md, *srcu_idx);
4376c182cd8SHannes Reinecke 		msleep(10);
4386c182cd8SHannes Reinecke 		goto retry;
4396c182cd8SHannes Reinecke 	}
440971888c4SMike Snitzer 
441e56f81e0SChristoph Hellwig 	return r;
442e56f81e0SChristoph Hellwig }
4436c182cd8SHannes Reinecke 
444971888c4SMike Snitzer static void dm_unprepare_ioctl(struct mapped_device *md, int srcu_idx)
445971888c4SMike Snitzer {
446971888c4SMike Snitzer 	dm_put_live_table(md, srcu_idx);
447971888c4SMike Snitzer }
448971888c4SMike Snitzer 
449e56f81e0SChristoph Hellwig static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
450e56f81e0SChristoph Hellwig 			unsigned int cmd, unsigned long arg)
451e56f81e0SChristoph Hellwig {
452e56f81e0SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
453971888c4SMike Snitzer 	int r, srcu_idx;
454e56f81e0SChristoph Hellwig 
4555bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
456e56f81e0SChristoph Hellwig 	if (r < 0)
457971888c4SMike Snitzer 		goto out;
458e56f81e0SChristoph Hellwig 
459e56f81e0SChristoph Hellwig 	if (r > 0) {
460e56f81e0SChristoph Hellwig 		/*
461e980f623SChristoph Hellwig 		 * Target determined this ioctl is being issued against a
462e980f623SChristoph Hellwig 		 * subset of the parent bdev; require extra privileges.
463e56f81e0SChristoph Hellwig 		 */
464e980f623SChristoph Hellwig 		if (!capable(CAP_SYS_RAWIO)) {
4650378c625SMike Snitzer 			DMDEBUG_LIMIT(
466e980f623SChristoph Hellwig 	"%s: sending ioctl %x to DM device without required privilege.",
467e980f623SChristoph Hellwig 				current->comm, cmd);
468e980f623SChristoph Hellwig 			r = -ENOIOCTLCMD;
469e56f81e0SChristoph Hellwig 			goto out;
470e56f81e0SChristoph Hellwig 		}
471e980f623SChristoph Hellwig 	}
472e56f81e0SChristoph Hellwig 
473a7cb3d2fSChristoph Hellwig 	if (!bdev->bd_disk->fops->ioctl)
474a7cb3d2fSChristoph Hellwig 		r = -ENOTTY;
475a7cb3d2fSChristoph Hellwig 	else
476a7cb3d2fSChristoph Hellwig 		r = bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg);
477e56f81e0SChristoph Hellwig out:
478971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
479aa129a22SMilan Broz 	return r;
480aa129a22SMilan Broz }
481aa129a22SMilan Broz 
4827465d7acSMike Snitzer u64 dm_start_time_ns_from_clone(struct bio *bio)
4837465d7acSMike Snitzer {
4846c23f0bdSChristoph Hellwig 	return jiffies_to_nsecs(clone_to_tio(bio)->io->start_time);
4857465d7acSMike Snitzer }
4867465d7acSMike Snitzer EXPORT_SYMBOL_GPL(dm_start_time_ns_from_clone);
4877465d7acSMike Snitzer 
4888d394bc4SMike Snitzer static bool bio_is_flush_with_data(struct bio *bio)
4897465d7acSMike Snitzer {
4908d394bc4SMike Snitzer 	return ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size);
4917465d7acSMike Snitzer }
4927465d7acSMike Snitzer 
4938d394bc4SMike Snitzer static void dm_io_acct(bool end, struct mapped_device *md, struct bio *bio,
494d208b894SJiazi Li 		       unsigned long start_time, struct dm_stats_aux *stats_aux)
4957465d7acSMike Snitzer {
4968d394bc4SMike Snitzer 	bool is_flush_with_data;
4978d394bc4SMike Snitzer 	unsigned int bi_size;
4987465d7acSMike Snitzer 
4998d394bc4SMike Snitzer 	/* If REQ_PREFLUSH set save any payload but do not account it */
5008d394bc4SMike Snitzer 	is_flush_with_data = bio_is_flush_with_data(bio);
5018d394bc4SMike Snitzer 	if (is_flush_with_data) {
5028d394bc4SMike Snitzer 		bi_size = bio->bi_iter.bi_size;
5038d394bc4SMike Snitzer 		bio->bi_iter.bi_size = 0;
5048d394bc4SMike Snitzer 	}
5058d394bc4SMike Snitzer 
5068d394bc4SMike Snitzer 	if (!end)
5078d394bc4SMike Snitzer 		bio_start_io_acct_time(bio, start_time);
5088d394bc4SMike Snitzer 	else
509d208b894SJiazi Li 		bio_end_io_acct(bio, start_time);
5107465d7acSMike Snitzer 
5117465d7acSMike Snitzer 	if (unlikely(dm_stats_used(&md->stats)))
5127465d7acSMike Snitzer 		dm_stats_account_io(&md->stats, bio_data_dir(bio),
5137465d7acSMike Snitzer 				    bio->bi_iter.bi_sector, bio_sectors(bio),
5148d394bc4SMike Snitzer 				    end, start_time, stats_aux);
5158d394bc4SMike Snitzer 
5168d394bc4SMike Snitzer 	/* Restore bio's payload so it does get accounted upon requeue */
5178d394bc4SMike Snitzer 	if (is_flush_with_data)
5188d394bc4SMike Snitzer 		bio->bi_iter.bi_size = bi_size;
5198d394bc4SMike Snitzer }
5208d394bc4SMike Snitzer 
5210fbb4d93SMike Snitzer static void __dm_start_io_acct(struct dm_io *io, struct bio *bio)
5228d394bc4SMike Snitzer {
5230fbb4d93SMike Snitzer 	dm_io_acct(false, io->md, bio, io->start_time, &io->stats_aux);
5248d394bc4SMike Snitzer }
5258d394bc4SMike Snitzer 
5260fbb4d93SMike Snitzer static void dm_start_io_acct(struct dm_io *io, struct bio *clone)
5278d394bc4SMike Snitzer {
5280fbb4d93SMike Snitzer 	/* Must account IO to DM device in terms of orig_bio */
5290fbb4d93SMike Snitzer 	struct bio *bio = io->orig_bio;
5300fbb4d93SMike Snitzer 
5310fbb4d93SMike Snitzer 	/*
5320fbb4d93SMike Snitzer 	 * Ensure IO accounting is only ever started once.
5330fbb4d93SMike Snitzer 	 * Expect no possibility for race unless is_duplicate_bio.
5340fbb4d93SMike Snitzer 	 */
5350fbb4d93SMike Snitzer 	if (!clone || likely(!clone_to_tio(clone)->is_duplicate_bio)) {
5360fbb4d93SMike Snitzer 		if (WARN_ON(io->was_accounted))
5370fbb4d93SMike Snitzer 			return;
5380fbb4d93SMike Snitzer 		io->was_accounted = 1;
5390fbb4d93SMike Snitzer 	} else if (xchg(&io->was_accounted, 1) == 1)
5400fbb4d93SMike Snitzer 		return;
5410fbb4d93SMike Snitzer 
5420fbb4d93SMike Snitzer 	__dm_start_io_acct(io, bio);
5430fbb4d93SMike Snitzer }
5440fbb4d93SMike Snitzer 
5450fbb4d93SMike Snitzer static void dm_end_io_acct(struct dm_io *io, struct bio *bio)
5460fbb4d93SMike Snitzer {
5470fbb4d93SMike Snitzer 	dm_io_acct(true, io->md, bio, io->start_time, &io->stats_aux);
5487465d7acSMike Snitzer }
549978e51baSMike Snitzer 
550978e51baSMike Snitzer static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio)
5511da177e4SLinus Torvalds {
55264f52b0eSMike Snitzer 	struct dm_io *io;
55364f52b0eSMike Snitzer 	struct dm_target_io *tio;
55464f52b0eSMike Snitzer 	struct bio *clone;
55564f52b0eSMike Snitzer 
556abfc426dSChristoph Hellwig 	clone = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO, &md->io_bs);
55764f52b0eSMike Snitzer 
5586c23f0bdSChristoph Hellwig 	tio = clone_to_tio(clone);
55964f52b0eSMike Snitzer 	tio->inside_dm_io = true;
56064f52b0eSMike Snitzer 	tio->io = NULL;
56164f52b0eSMike Snitzer 
56264f52b0eSMike Snitzer 	io = container_of(tio, struct dm_io, tio);
56364f52b0eSMike Snitzer 	io->magic = DM_IO_MAGIC;
564978e51baSMike Snitzer 	io->status = 0;
565978e51baSMike Snitzer 	atomic_set(&io->io_count, 1);
5669f6dc633SMike Snitzer 	this_cpu_inc(*md->pending_io);
5670fbb4d93SMike Snitzer 	io->orig_bio = NULL;
568978e51baSMike Snitzer 	io->md = md;
569978e51baSMike Snitzer 	spin_lock_init(&io->endio_lock);
570978e51baSMike Snitzer 
571b879f915SMike Snitzer 	io->start_time = jiffies;
5720fbb4d93SMike Snitzer 	io->start_io_acct = false;
5730fbb4d93SMike Snitzer 	io->was_accounted = 0;
57464f52b0eSMike Snitzer 
5750cdb90f0SMike Snitzer 	dm_stats_record_start(&md->stats, &io->stats_aux);
5760cdb90f0SMike Snitzer 
57764f52b0eSMike Snitzer 	return io;
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds 
5800119ab14SMike Snitzer static void free_io(struct dm_io *io)
5811da177e4SLinus Torvalds {
58264f52b0eSMike Snitzer 	bio_put(&io->tio.clone);
58364f52b0eSMike Snitzer }
58464f52b0eSMike Snitzer 
5851d1068ceSChristoph Hellwig static struct bio *alloc_tio(struct clone_info *ci, struct dm_target *ti,
586dc8e2021SChristoph Hellwig 		unsigned target_bio_nr, unsigned *len, gfp_t gfp_mask)
58764f52b0eSMike Snitzer {
58864f52b0eSMike Snitzer 	struct dm_target_io *tio;
589018b05ebSMike Snitzer 	struct bio *clone;
59064f52b0eSMike Snitzer 
59164f52b0eSMike Snitzer 	if (!ci->io->tio.io) {
59264f52b0eSMike Snitzer 		/* the dm_target_io embedded in ci->io is available */
59364f52b0eSMike Snitzer 		tio = &ci->io->tio;
594018b05ebSMike Snitzer 		/* alloc_io() already initialized embedded clone */
595018b05ebSMike Snitzer 		clone = &tio->clone;
59664f52b0eSMike Snitzer 	} else {
597018b05ebSMike Snitzer 		clone = bio_alloc_clone(ci->bio->bi_bdev, ci->bio,
598abfc426dSChristoph Hellwig 					gfp_mask, &ci->io->md->bs);
59964f52b0eSMike Snitzer 		if (!clone)
60064f52b0eSMike Snitzer 			return NULL;
60164f52b0eSMike Snitzer 
6026c23f0bdSChristoph Hellwig 		tio = clone_to_tio(clone);
60364f52b0eSMike Snitzer 		tio->inside_dm_io = false;
60464f52b0eSMike Snitzer 	}
60556b4b5abSChristoph Hellwig 
60664f52b0eSMike Snitzer 	tio->magic = DM_TIO_MAGIC;
60764f52b0eSMike Snitzer 	tio->io = ci->io;
60864f52b0eSMike Snitzer 	tio->ti = ti;
60964f52b0eSMike Snitzer 	tio->target_bio_nr = target_bio_nr;
610e6fc9f62SMike Snitzer 	tio->is_duplicate_bio = false;
611dc8e2021SChristoph Hellwig 	tio->len_ptr = len;
612743598f0SMike Snitzer 	tio->old_sector = 0;
61364f52b0eSMike Snitzer 
614018b05ebSMike Snitzer 	if (len) {
615018b05ebSMike Snitzer 		clone->bi_iter.bi_size = to_bytes(*len);
616018b05ebSMike Snitzer 		if (bio_integrity(clone))
617018b05ebSMike Snitzer 			bio_integrity_trim(clone);
618018b05ebSMike Snitzer 	}
619018b05ebSMike Snitzer 
620018b05ebSMike Snitzer 	return clone;
6211da177e4SLinus Torvalds }
6221da177e4SLinus Torvalds 
6231d1068ceSChristoph Hellwig static void free_tio(struct bio *clone)
6241da177e4SLinus Torvalds {
6251d1068ceSChristoph Hellwig 	if (clone_to_tio(clone)->inside_dm_io)
62664f52b0eSMike Snitzer 		return;
6271d1068ceSChristoph Hellwig 	bio_put(clone);
6281da177e4SLinus Torvalds }
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds /*
6311da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
6321da177e4SLinus Torvalds  */
63392c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
6341da177e4SLinus Torvalds {
63505447420SKiyoshi Ueda 	unsigned long flags;
6361da177e4SLinus Torvalds 
63705447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
6381da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
63905447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
64092c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
6411da177e4SLinus Torvalds }
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds /*
6441da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
6451da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
64683d5e5b0SMikulas Patocka  * dm_put_live_table() when finished.
6471da177e4SLinus Torvalds  */
64883d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
6491da177e4SLinus Torvalds {
65083d5e5b0SMikulas Patocka 	*srcu_idx = srcu_read_lock(&md->io_barrier);
6511da177e4SLinus Torvalds 
65283d5e5b0SMikulas Patocka 	return srcu_dereference(md->map, &md->io_barrier);
65383d5e5b0SMikulas Patocka }
6541da177e4SLinus Torvalds 
65583d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
65683d5e5b0SMikulas Patocka {
65783d5e5b0SMikulas Patocka 	srcu_read_unlock(&md->io_barrier, srcu_idx);
65883d5e5b0SMikulas Patocka }
65983d5e5b0SMikulas Patocka 
66083d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md)
66183d5e5b0SMikulas Patocka {
66283d5e5b0SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
66383d5e5b0SMikulas Patocka 	synchronize_rcu_expedited();
66483d5e5b0SMikulas Patocka }
66583d5e5b0SMikulas Patocka 
66683d5e5b0SMikulas Patocka /*
66783d5e5b0SMikulas Patocka  * A fast alternative to dm_get_live_table/dm_put_live_table.
66883d5e5b0SMikulas Patocka  * The caller must not block between these two functions.
66983d5e5b0SMikulas Patocka  */
67083d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
67183d5e5b0SMikulas Patocka {
67283d5e5b0SMikulas Patocka 	rcu_read_lock();
67383d5e5b0SMikulas Patocka 	return rcu_dereference(md->map);
67483d5e5b0SMikulas Patocka }
67583d5e5b0SMikulas Patocka 
67683d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
67783d5e5b0SMikulas Patocka {
67883d5e5b0SMikulas Patocka 	rcu_read_unlock();
6791da177e4SLinus Torvalds }
6801da177e4SLinus Torvalds 
681971888c4SMike Snitzer static char *_dm_claim_ptr = "I belong to device-mapper";
682971888c4SMike Snitzer 
6833ac51e74SDarrick J. Wong /*
68486f1152bSBenjamin Marzinski  * Open a table device so we can use it as a map destination.
68586f1152bSBenjamin Marzinski  */
68686f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev,
68786f1152bSBenjamin Marzinski 			     struct mapped_device *md)
68886f1152bSBenjamin Marzinski {
68986f1152bSBenjamin Marzinski 	struct block_device *bdev;
690cd913c76SChristoph Hellwig 	u64 part_off;
69186f1152bSBenjamin Marzinski 	int r;
69286f1152bSBenjamin Marzinski 
69386f1152bSBenjamin Marzinski 	BUG_ON(td->dm_dev.bdev);
69486f1152bSBenjamin Marzinski 
695519049afSMike Snitzer 	bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr);
69686f1152bSBenjamin Marzinski 	if (IS_ERR(bdev))
69786f1152bSBenjamin Marzinski 		return PTR_ERR(bdev);
69886f1152bSBenjamin Marzinski 
69986f1152bSBenjamin Marzinski 	r = bd_link_disk_holder(bdev, dm_disk(md));
70086f1152bSBenjamin Marzinski 	if (r) {
70186f1152bSBenjamin Marzinski 		blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
70286f1152bSBenjamin Marzinski 		return r;
70386f1152bSBenjamin Marzinski 	}
70486f1152bSBenjamin Marzinski 
70586f1152bSBenjamin Marzinski 	td->dm_dev.bdev = bdev;
706cd913c76SChristoph Hellwig 	td->dm_dev.dax_dev = fs_dax_get_by_bdev(bdev, &part_off);
70786f1152bSBenjamin Marzinski 	return 0;
70886f1152bSBenjamin Marzinski }
70986f1152bSBenjamin Marzinski 
71086f1152bSBenjamin Marzinski /*
71186f1152bSBenjamin Marzinski  * Close a table device that we've been using.
71286f1152bSBenjamin Marzinski  */
71386f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md)
71486f1152bSBenjamin Marzinski {
71586f1152bSBenjamin Marzinski 	if (!td->dm_dev.bdev)
71686f1152bSBenjamin Marzinski 		return;
71786f1152bSBenjamin Marzinski 
71886f1152bSBenjamin Marzinski 	bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
71986f1152bSBenjamin Marzinski 	blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
720817bf402SDan Williams 	put_dax(td->dm_dev.dax_dev);
72186f1152bSBenjamin Marzinski 	td->dm_dev.bdev = NULL;
722817bf402SDan Williams 	td->dm_dev.dax_dev = NULL;
72386f1152bSBenjamin Marzinski }
72486f1152bSBenjamin Marzinski 
72586f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev,
7268454fca4SSheetal Singala 					      fmode_t mode)
7278454fca4SSheetal Singala {
72886f1152bSBenjamin Marzinski 	struct table_device *td;
72986f1152bSBenjamin Marzinski 
73086f1152bSBenjamin Marzinski 	list_for_each_entry(td, l, list)
73186f1152bSBenjamin Marzinski 		if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
73286f1152bSBenjamin Marzinski 			return td;
73386f1152bSBenjamin Marzinski 
73486f1152bSBenjamin Marzinski 	return NULL;
73586f1152bSBenjamin Marzinski }
73686f1152bSBenjamin Marzinski 
73786f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
7388454fca4SSheetal Singala 			struct dm_dev **result)
7398454fca4SSheetal Singala {
74086f1152bSBenjamin Marzinski 	int r;
74186f1152bSBenjamin Marzinski 	struct table_device *td;
74286f1152bSBenjamin Marzinski 
74386f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
74486f1152bSBenjamin Marzinski 	td = find_table_device(&md->table_devices, dev, mode);
74586f1152bSBenjamin Marzinski 	if (!td) {
746115485e8SMike Snitzer 		td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
74786f1152bSBenjamin Marzinski 		if (!td) {
74886f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
74986f1152bSBenjamin Marzinski 			return -ENOMEM;
75086f1152bSBenjamin Marzinski 		}
75186f1152bSBenjamin Marzinski 
75286f1152bSBenjamin Marzinski 		td->dm_dev.mode = mode;
75386f1152bSBenjamin Marzinski 		td->dm_dev.bdev = NULL;
75486f1152bSBenjamin Marzinski 
75586f1152bSBenjamin Marzinski 		if ((r = open_table_device(td, dev, md))) {
75686f1152bSBenjamin Marzinski 			mutex_unlock(&md->table_devices_lock);
75786f1152bSBenjamin Marzinski 			kfree(td);
75886f1152bSBenjamin Marzinski 			return r;
75986f1152bSBenjamin Marzinski 		}
76086f1152bSBenjamin Marzinski 
76186f1152bSBenjamin Marzinski 		format_dev_t(td->dm_dev.name, dev);
76286f1152bSBenjamin Marzinski 
763b0b4d7c6SElena Reshetova 		refcount_set(&td->count, 1);
76486f1152bSBenjamin Marzinski 		list_add(&td->list, &md->table_devices);
765b0b4d7c6SElena Reshetova 	} else {
766b0b4d7c6SElena Reshetova 		refcount_inc(&td->count);
76786f1152bSBenjamin Marzinski 	}
76886f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
76986f1152bSBenjamin Marzinski 
77086f1152bSBenjamin Marzinski 	*result = &td->dm_dev;
77186f1152bSBenjamin Marzinski 	return 0;
77286f1152bSBenjamin Marzinski }
77386f1152bSBenjamin Marzinski 
77486f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
77586f1152bSBenjamin Marzinski {
77686f1152bSBenjamin Marzinski 	struct table_device *td = container_of(d, struct table_device, dm_dev);
77786f1152bSBenjamin Marzinski 
77886f1152bSBenjamin Marzinski 	mutex_lock(&md->table_devices_lock);
779b0b4d7c6SElena Reshetova 	if (refcount_dec_and_test(&td->count)) {
78086f1152bSBenjamin Marzinski 		close_table_device(td, md);
78186f1152bSBenjamin Marzinski 		list_del(&td->list);
78286f1152bSBenjamin Marzinski 		kfree(td);
78386f1152bSBenjamin Marzinski 	}
78486f1152bSBenjamin Marzinski 	mutex_unlock(&md->table_devices_lock);
78586f1152bSBenjamin Marzinski }
78686f1152bSBenjamin Marzinski 
78786f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices)
78886f1152bSBenjamin Marzinski {
78986f1152bSBenjamin Marzinski 	struct list_head *tmp, *next;
79086f1152bSBenjamin Marzinski 
79186f1152bSBenjamin Marzinski 	list_for_each_safe(tmp, next, devices) {
79286f1152bSBenjamin Marzinski 		struct table_device *td = list_entry(tmp, struct table_device, list);
79386f1152bSBenjamin Marzinski 
79486f1152bSBenjamin Marzinski 		DMWARN("dm_destroy: %s still exists with %d references",
795b0b4d7c6SElena Reshetova 		       td->dm_dev.name, refcount_read(&td->count));
79686f1152bSBenjamin Marzinski 		kfree(td);
79786f1152bSBenjamin Marzinski 	}
79886f1152bSBenjamin Marzinski }
79986f1152bSBenjamin Marzinski 
80086f1152bSBenjamin Marzinski /*
8013ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
8023ac51e74SDarrick J. Wong  */
8033ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
8043ac51e74SDarrick J. Wong {
8053ac51e74SDarrick J. Wong 	*geo = md->geometry;
8063ac51e74SDarrick J. Wong 
8073ac51e74SDarrick J. Wong 	return 0;
8083ac51e74SDarrick J. Wong }
8093ac51e74SDarrick J. Wong 
8103ac51e74SDarrick J. Wong /*
8113ac51e74SDarrick J. Wong  * Set the geometry of a device.
8123ac51e74SDarrick J. Wong  */
8133ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
8143ac51e74SDarrick J. Wong {
8153ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
8163ac51e74SDarrick J. Wong 
8173ac51e74SDarrick J. Wong 	if (geo->start > sz) {
8183ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
8193ac51e74SDarrick J. Wong 		return -EINVAL;
8203ac51e74SDarrick J. Wong 	}
8213ac51e74SDarrick J. Wong 
8223ac51e74SDarrick J. Wong 	md->geometry = *geo;
8233ac51e74SDarrick J. Wong 
8243ac51e74SDarrick J. Wong 	return 0;
8253ac51e74SDarrick J. Wong }
8263ac51e74SDarrick J. Wong 
8272e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
8282e93ccc1SKiyoshi Ueda {
8292e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
8302e93ccc1SKiyoshi Ueda }
8312e93ccc1SKiyoshi Ueda 
8321da177e4SLinus Torvalds /*
8331da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
8341da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
8351da177e4SLinus Torvalds  */
836e2118b3cSDamien Le Moal void dm_io_dec_pending(struct dm_io *io, blk_status_t error)
8371da177e4SLinus Torvalds {
8382e93ccc1SKiyoshi Ueda 	unsigned long flags;
8394e4cbee9SChristoph Hellwig 	blk_status_t io_error;
840b35f8caaSMilan Broz 	struct bio *bio;
841b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
8422e93ccc1SKiyoshi Ueda 
8432e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
844f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
845f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
846745dc570SMike Snitzer 		if (!(io->status == BLK_STS_DM_REQUEUE && __noflush_suspending(md)))
8474e4cbee9SChristoph Hellwig 			io->status = error;
848f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
849f88fb981SKiyoshi Ueda 	}
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
852bf14e2b2SDamien Le Moal 		bio = io->orig_bio;
8534e4cbee9SChristoph Hellwig 		if (io->status == BLK_STS_DM_REQUEUE) {
8542e93ccc1SKiyoshi Ueda 			/*
8552e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
8562e93ccc1SKiyoshi Ueda 			 */
857022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
858bf14e2b2SDamien Le Moal 			if (__noflush_suspending(md) &&
859bf14e2b2SDamien Le Moal 			    !WARN_ON_ONCE(dm_is_zone_write(md, bio))) {
860745dc570SMike Snitzer 				/* NOTE early return due to BLK_STS_DM_REQUEUE below */
861bf14e2b2SDamien Le Moal 				bio_list_add_head(&md->deferred, bio);
862bf14e2b2SDamien Le Moal 			} else {
863bf14e2b2SDamien Le Moal 				/*
864bf14e2b2SDamien Le Moal 				 * noflush suspend was interrupted or this is
865bf14e2b2SDamien Le Moal 				 * a write to a zoned target.
866bf14e2b2SDamien Le Moal 				 */
8674e4cbee9SChristoph Hellwig 				io->status = BLK_STS_IOERR;
868bf14e2b2SDamien Le Moal 			}
869022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
8702e93ccc1SKiyoshi Ueda 		}
8712e93ccc1SKiyoshi Ueda 
8724e4cbee9SChristoph Hellwig 		io_error = io->status;
8730fbb4d93SMike Snitzer 		if (io->was_accounted)
8740fbb4d93SMike Snitzer 			dm_end_io_acct(io, bio);
8750fbb4d93SMike Snitzer 		else if (!io_error) {
8760fbb4d93SMike Snitzer 			/*
8770fbb4d93SMike Snitzer 			 * Must handle target that DM_MAPIO_SUBMITTED only to
8780fbb4d93SMike Snitzer 			 * then bio_endio() rather than dm_submit_bio_remap()
8790fbb4d93SMike Snitzer 			 */
8800fbb4d93SMike Snitzer 			__dm_start_io_acct(io, bio);
8810fbb4d93SMike Snitzer 			dm_end_io_acct(io, bio);
8820fbb4d93SMike Snitzer 		}
8830119ab14SMike Snitzer 		free_io(io);
8849f6dc633SMike Snitzer 		smp_wmb();
8859f6dc633SMike Snitzer 		this_cpu_dec(*md->pending_io);
8869f6dc633SMike Snitzer 
8879f6dc633SMike Snitzer 		/* nudge anyone waiting on suspend queue */
8889f6dc633SMike Snitzer 		if (unlikely(wq_has_sleeper(&md->wait)))
8899f6dc633SMike Snitzer 			wake_up(&md->wait);
8902056a782SJens Axboe 
8914e4cbee9SChristoph Hellwig 		if (io_error == BLK_STS_DM_REQUEUE)
8926a8736d1STejun Heo 			return;
8936a8736d1STejun Heo 
8948d394bc4SMike Snitzer 		if (bio_is_flush_with_data(bio)) {
895af7e466aSMikulas Patocka 			/*
8966a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
89728a8f0d3SMike Christie 			 * without REQ_PREFLUSH.
898af7e466aSMikulas Patocka 			 */
8991eff9d32SJens Axboe 			bio->bi_opf &= ~REQ_PREFLUSH;
9006a8736d1STejun Heo 			queue_io(md, bio);
901af7e466aSMikulas Patocka 		} else {
902b372d360SMike Snitzer 			/* done with normal IO or empty flush */
9038dd601faSNeilBrown 			if (io_error)
9044e4cbee9SChristoph Hellwig 				bio->bi_status = io_error;
9054246a0b6SChristoph Hellwig 			bio_endio(bio);
9062e93ccc1SKiyoshi Ueda 		}
9071da177e4SLinus Torvalds 	}
908af7e466aSMikulas Patocka }
9091da177e4SLinus Torvalds 
910bcb44433SMike Snitzer void disable_discard(struct mapped_device *md)
911bcb44433SMike Snitzer {
912bcb44433SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
913bcb44433SMike Snitzer 
914bcb44433SMike Snitzer 	/* device doesn't really support DISCARD, disable it */
915bcb44433SMike Snitzer 	limits->max_discard_sectors = 0;
916bcb44433SMike Snitzer 	blk_queue_flag_clear(QUEUE_FLAG_DISCARD, md->queue);
917bcb44433SMike Snitzer }
918bcb44433SMike Snitzer 
9194cc96131SMike Snitzer void disable_write_same(struct mapped_device *md)
9207eee4ae2SMike Snitzer {
9217eee4ae2SMike Snitzer 	struct queue_limits *limits = dm_get_queue_limits(md);
9227eee4ae2SMike Snitzer 
9237eee4ae2SMike Snitzer 	/* device doesn't really support WRITE SAME, disable it */
9247eee4ae2SMike Snitzer 	limits->max_write_same_sectors = 0;
9257eee4ae2SMike Snitzer }
9267eee4ae2SMike Snitzer 
927ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md)
928ac62d620SChristoph Hellwig {
929ac62d620SChristoph Hellwig 	struct queue_limits *limits = dm_get_queue_limits(md);
930ac62d620SChristoph Hellwig 
931ac62d620SChristoph Hellwig 	/* device doesn't really support WRITE ZEROES, disable it */
932ac62d620SChristoph Hellwig 	limits->max_write_zeroes_sectors = 0;
933ac62d620SChristoph Hellwig }
934ac62d620SChristoph Hellwig 
935a666e5c0SMikulas Patocka static bool swap_bios_limit(struct dm_target *ti, struct bio *bio)
936a666e5c0SMikulas Patocka {
937a666e5c0SMikulas Patocka 	return unlikely((bio->bi_opf & REQ_SWAP) != 0) && unlikely(ti->limit_swap_bios);
938a666e5c0SMikulas Patocka }
939a666e5c0SMikulas Patocka 
9404246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio)
9411da177e4SLinus Torvalds {
9424e4cbee9SChristoph Hellwig 	blk_status_t error = bio->bi_status;
9436c23f0bdSChristoph Hellwig 	struct dm_target_io *tio = clone_to_tio(bio);
944b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
9459faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
9461da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
947309dca30SChristoph Hellwig 	struct request_queue *q = bio->bi_bdev->bd_disk->queue;
9481da177e4SLinus Torvalds 
9499c37de29SMike Snitzer 	if (unlikely(error == BLK_STS_TARGET)) {
950bcb44433SMike Snitzer 		if (bio_op(bio) == REQ_OP_DISCARD &&
951309dca30SChristoph Hellwig 		    !q->limits.max_discard_sectors)
952bcb44433SMike Snitzer 			disable_discard(md);
953bcb44433SMike Snitzer 		else if (bio_op(bio) == REQ_OP_WRITE_SAME &&
954309dca30SChristoph Hellwig 			 !q->limits.max_write_same_sectors)
9557eee4ae2SMike Snitzer 			disable_write_same(md);
956bcb44433SMike Snitzer 		else if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
957309dca30SChristoph Hellwig 			 !q->limits.max_write_zeroes_sectors)
958ac62d620SChristoph Hellwig 			disable_write_zeroes(md);
959ac62d620SChristoph Hellwig 	}
9607eee4ae2SMike Snitzer 
961bb37d772SDamien Le Moal 	if (blk_queue_is_zoned(q))
962bb37d772SDamien Le Moal 		dm_zone_endio(io, bio);
963415c79e1SJohannes Thumshirn 
9641be56909SChristoph Hellwig 	if (endio) {
9654e4cbee9SChristoph Hellwig 		int r = endio(tio->ti, bio, &error);
9661be56909SChristoph Hellwig 		switch (r) {
9671be56909SChristoph Hellwig 		case DM_ENDIO_REQUEUE:
968bf14e2b2SDamien Le Moal 			/*
969bf14e2b2SDamien Le Moal 			 * Requeuing writes to a sequential zone of a zoned
970bf14e2b2SDamien Le Moal 			 * target will break the sequential write pattern:
971bf14e2b2SDamien Le Moal 			 * fail such IO.
972bf14e2b2SDamien Le Moal 			 */
973bf14e2b2SDamien Le Moal 			if (WARN_ON_ONCE(dm_is_zone_write(md, bio)))
974bf14e2b2SDamien Le Moal 				error = BLK_STS_IOERR;
975bf14e2b2SDamien Le Moal 			else
9764e4cbee9SChristoph Hellwig 				error = BLK_STS_DM_REQUEUE;
977df561f66SGustavo A. R. Silva 			fallthrough;
9781be56909SChristoph Hellwig 		case DM_ENDIO_DONE:
9791be56909SChristoph Hellwig 			break;
9801be56909SChristoph Hellwig 		case DM_ENDIO_INCOMPLETE:
9811be56909SChristoph Hellwig 			/* The target will handle the io */
9821be56909SChristoph Hellwig 			return;
9831be56909SChristoph Hellwig 		default:
9841be56909SChristoph Hellwig 			DMWARN("unimplemented target endio return value: %d", r);
9851be56909SChristoph Hellwig 			BUG();
9861be56909SChristoph Hellwig 		}
9871be56909SChristoph Hellwig 	}
9881be56909SChristoph Hellwig 
989a666e5c0SMikulas Patocka 	if (unlikely(swap_bios_limit(tio->ti, bio))) {
990a666e5c0SMikulas Patocka 		struct mapped_device *md = io->md;
991a666e5c0SMikulas Patocka 		up(&md->swap_bios_semaphore);
992a666e5c0SMikulas Patocka 	}
993a666e5c0SMikulas Patocka 
9941d1068ceSChristoph Hellwig 	free_tio(bio);
995e2118b3cSDamien Le Moal 	dm_io_dec_pending(io, error);
9961da177e4SLinus Torvalds }
9971da177e4SLinus Torvalds 
99878d8e58aSMike Snitzer /*
99956a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
100056a67df7SMike Snitzer  * target boundary.
100156a67df7SMike Snitzer  */
10023720281dSMike Snitzer static inline sector_t max_io_len_target_boundary(struct dm_target *ti,
10033720281dSMike Snitzer 						  sector_t target_offset)
10041da177e4SLinus Torvalds {
100556a67df7SMike Snitzer 	return ti->len - target_offset;
100656a67df7SMike Snitzer }
100756a67df7SMike Snitzer 
10083720281dSMike Snitzer static sector_t max_io_len(struct dm_target *ti, sector_t sector)
100956a67df7SMike Snitzer {
10103720281dSMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
10113720281dSMike Snitzer 	sector_t len = max_io_len_target_boundary(ti, target_offset);
10125091cdecSMike Snitzer 	sector_t max_len;
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 	/*
10153ee16db3SMike Snitzer 	 * Does the target need to split IO even further?
10163ee16db3SMike Snitzer 	 * - varied (per target) IO splitting is a tenet of DM; this
10173ee16db3SMike Snitzer 	 *   explains why stacked chunk_sectors based splitting via
10183ee16db3SMike Snitzer 	 *   blk_max_size_offset() isn't possible here. So pass in
10193ee16db3SMike Snitzer 	 *   ti->max_io_len to override stacked chunk_sectors.
10201da177e4SLinus Torvalds 	 */
10213ee16db3SMike Snitzer 	if (ti->max_io_len) {
102233bd6f06SMike Snitzer 		max_len = blk_max_size_offset(ti->table->md->queue,
10233ee16db3SMike Snitzer 					      target_offset, ti->max_io_len);
1024542f9038SMike Snitzer 		if (len > max_len)
1025542f9038SMike Snitzer 			len = max_len;
10263ee16db3SMike Snitzer 	}
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 	return len;
10291da177e4SLinus Torvalds }
10301da177e4SLinus Torvalds 
1031542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
1032542f9038SMike Snitzer {
1033542f9038SMike Snitzer 	if (len > UINT_MAX) {
1034542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
1035542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
1036542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
1037542f9038SMike Snitzer 		return -EINVAL;
1038542f9038SMike Snitzer 	}
1039542f9038SMike Snitzer 
104075ae1936SMikulas Patocka 	ti->max_io_len = (uint32_t) len;
1041542f9038SMike Snitzer 
1042542f9038SMike Snitzer 	return 0;
1043542f9038SMike Snitzer }
1044542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
1045542f9038SMike Snitzer 
1046f26c5719SDan Williams static struct dm_target *dm_dax_get_live_target(struct mapped_device *md,
1047f26c5719SDan Williams 						sector_t sector, int *srcu_idx)
10483d97c829SMike Snitzer 	__acquires(md->io_barrier)
1049545ed20eSToshi Kani {
1050545ed20eSToshi Kani 	struct dm_table *map;
1051545ed20eSToshi Kani 	struct dm_target *ti;
1052545ed20eSToshi Kani 
1053f26c5719SDan Williams 	map = dm_get_live_table(md, srcu_idx);
1054545ed20eSToshi Kani 	if (!map)
1055f26c5719SDan Williams 		return NULL;
1056545ed20eSToshi Kani 
1057545ed20eSToshi Kani 	ti = dm_table_find_target(map, sector);
1058123d87d5SMikulas Patocka 	if (!ti)
1059f26c5719SDan Williams 		return NULL;
1060f26c5719SDan Williams 
1061f26c5719SDan Williams 	return ti;
1062f26c5719SDan Williams }
1063f26c5719SDan Williams 
1064f26c5719SDan Williams static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
1065f26c5719SDan Williams 				 long nr_pages, void **kaddr, pfn_t *pfn)
1066f26c5719SDan Williams {
1067f26c5719SDan Williams 	struct mapped_device *md = dax_get_private(dax_dev);
1068f26c5719SDan Williams 	sector_t sector = pgoff * PAGE_SECTORS;
1069f26c5719SDan Williams 	struct dm_target *ti;
1070f26c5719SDan Williams 	long len, ret = -EIO;
1071f26c5719SDan Williams 	int srcu_idx;
1072f26c5719SDan Williams 
1073f26c5719SDan Williams 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1074f26c5719SDan Williams 
1075f26c5719SDan Williams 	if (!ti)
1076545ed20eSToshi Kani 		goto out;
1077f26c5719SDan Williams 	if (!ti->type->direct_access)
1078f26c5719SDan Williams 		goto out;
10793720281dSMike Snitzer 	len = max_io_len(ti, sector) / PAGE_SECTORS;
1080f26c5719SDan Williams 	if (len < 1)
1081f26c5719SDan Williams 		goto out;
1082f26c5719SDan Williams 	nr_pages = min(len, nr_pages);
1083817bf402SDan Williams 	ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn);
1084817bf402SDan Williams 
1085545ed20eSToshi Kani  out:
1086545ed20eSToshi Kani 	dm_put_live_table(md, srcu_idx);
1087f26c5719SDan Williams 
1088f26c5719SDan Williams 	return ret;
1089545ed20eSToshi Kani }
1090545ed20eSToshi Kani 
1091cdf6cdcdSVivek Goyal static int dm_dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
1092cdf6cdcdSVivek Goyal 				  size_t nr_pages)
1093cdf6cdcdSVivek Goyal {
1094cdf6cdcdSVivek Goyal 	struct mapped_device *md = dax_get_private(dax_dev);
1095cdf6cdcdSVivek Goyal 	sector_t sector = pgoff * PAGE_SECTORS;
1096cdf6cdcdSVivek Goyal 	struct dm_target *ti;
1097cdf6cdcdSVivek Goyal 	int ret = -EIO;
1098cdf6cdcdSVivek Goyal 	int srcu_idx;
1099cdf6cdcdSVivek Goyal 
1100cdf6cdcdSVivek Goyal 	ti = dm_dax_get_live_target(md, sector, &srcu_idx);
1101cdf6cdcdSVivek Goyal 
1102cdf6cdcdSVivek Goyal 	if (!ti)
1103cdf6cdcdSVivek Goyal 		goto out;
1104cdf6cdcdSVivek Goyal 	if (WARN_ON(!ti->type->dax_zero_page_range)) {
1105cdf6cdcdSVivek Goyal 		/*
1106cdf6cdcdSVivek Goyal 		 * ->zero_page_range() is mandatory dax operation. If we are
1107cdf6cdcdSVivek Goyal 		 *  here, something is wrong.
1108cdf6cdcdSVivek Goyal 		 */
1109cdf6cdcdSVivek Goyal 		goto out;
1110cdf6cdcdSVivek Goyal 	}
1111cdf6cdcdSVivek Goyal 	ret = ti->type->dax_zero_page_range(ti, pgoff, nr_pages);
1112cdf6cdcdSVivek Goyal  out:
1113cdf6cdcdSVivek Goyal 	dm_put_live_table(md, srcu_idx);
1114cdf6cdcdSVivek Goyal 
1115cdf6cdcdSVivek Goyal 	return ret;
1116cdf6cdcdSVivek Goyal }
1117cdf6cdcdSVivek Goyal 
11181dd40c3eSMikulas Patocka /*
11191dd40c3eSMikulas Patocka  * A target may call dm_accept_partial_bio only from the map routine.  It is
11206842d264SDamien Le Moal  * allowed for all bio types except REQ_PREFLUSH, REQ_OP_ZONE_* zone management
1121e6fc9f62SMike Snitzer  * operations, REQ_OP_ZONE_APPEND (zone append writes) and any bio serviced by
1122e6fc9f62SMike Snitzer  * __send_duplicate_bios().
11231dd40c3eSMikulas Patocka  *
11241dd40c3eSMikulas Patocka  * dm_accept_partial_bio informs the dm that the target only wants to process
11251dd40c3eSMikulas Patocka  * additional n_sectors sectors of the bio and the rest of the data should be
11261dd40c3eSMikulas Patocka  * sent in a next bio.
11271dd40c3eSMikulas Patocka  *
11281dd40c3eSMikulas Patocka  * A diagram that explains the arithmetics:
11291dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
11301dd40c3eSMikulas Patocka  * |         1          |       2       |   3   |
11311dd40c3eSMikulas Patocka  * +--------------------+---------------+-------+
11321dd40c3eSMikulas Patocka  *
11331dd40c3eSMikulas Patocka  * <-------------- *tio->len_ptr --------------->
11341dd40c3eSMikulas Patocka  *                      <------- bi_size ------->
11351dd40c3eSMikulas Patocka  *                      <-- n_sectors -->
11361dd40c3eSMikulas Patocka  *
11371dd40c3eSMikulas Patocka  * Region 1 was already iterated over with bio_advance or similar function.
11381dd40c3eSMikulas Patocka  *	(it may be empty if the target doesn't use bio_advance)
11391dd40c3eSMikulas Patocka  * Region 2 is the remaining bio size that the target wants to process.
11401dd40c3eSMikulas Patocka  *	(it may be empty if region 1 is non-empty, although there is no reason
11411dd40c3eSMikulas Patocka  *	 to make it empty)
11421dd40c3eSMikulas Patocka  * The target requires that region 3 is to be sent in the next bio.
11431dd40c3eSMikulas Patocka  *
11441dd40c3eSMikulas Patocka  * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
11451dd40c3eSMikulas Patocka  * the partially processed part (the sum of regions 1+2) must be the same for all
11461dd40c3eSMikulas Patocka  * copies of the bio.
11471dd40c3eSMikulas Patocka  */
11481dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
11491dd40c3eSMikulas Patocka {
11506c23f0bdSChristoph Hellwig 	struct dm_target_io *tio = clone_to_tio(bio);
11511dd40c3eSMikulas Patocka 	unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
11526842d264SDamien Le Moal 
1153e6fc9f62SMike Snitzer 	BUG_ON(tio->is_duplicate_bio);
11546842d264SDamien Le Moal 	BUG_ON(op_is_zone_mgmt(bio_op(bio)));
11556842d264SDamien Le Moal 	BUG_ON(bio_op(bio) == REQ_OP_ZONE_APPEND);
11561dd40c3eSMikulas Patocka 	BUG_ON(bi_size > *tio->len_ptr);
11571dd40c3eSMikulas Patocka 	BUG_ON(n_sectors > bi_size);
11586842d264SDamien Le Moal 
11591dd40c3eSMikulas Patocka 	*tio->len_ptr -= bi_size - n_sectors;
11601dd40c3eSMikulas Patocka 	bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
11611dd40c3eSMikulas Patocka }
11621dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
11631dd40c3eSMikulas Patocka 
11640fbb4d93SMike Snitzer static inline void __dm_submit_bio_remap(struct bio *clone,
11650fbb4d93SMike Snitzer 					 dev_t dev, sector_t old_sector)
11660fbb4d93SMike Snitzer {
11670fbb4d93SMike Snitzer 	trace_block_bio_remap(clone, dev, old_sector);
11680fbb4d93SMike Snitzer 	submit_bio_noacct(clone);
11690fbb4d93SMike Snitzer }
11700fbb4d93SMike Snitzer 
11710fbb4d93SMike Snitzer /*
11720fbb4d93SMike Snitzer  * @clone: clone bio that DM core passed to target's .map function
11730fbb4d93SMike Snitzer  * @tgt_clone: clone of @clone bio that target needs submitted
11740fbb4d93SMike Snitzer  * @from_wq: caller is a workqueue thread managed by DM target
11750fbb4d93SMike Snitzer  *
11760fbb4d93SMike Snitzer  * Targets should use this interface to submit bios they take
11770fbb4d93SMike Snitzer  * ownership of when returning DM_MAPIO_SUBMITTED.
11780fbb4d93SMike Snitzer  *
11790fbb4d93SMike Snitzer  * Target should also enable ti->accounts_remapped_io
11800fbb4d93SMike Snitzer  */
11810fbb4d93SMike Snitzer void dm_submit_bio_remap(struct bio *clone, struct bio *tgt_clone,
11820fbb4d93SMike Snitzer 			 bool from_wq)
11830fbb4d93SMike Snitzer {
11840fbb4d93SMike Snitzer 	struct dm_target_io *tio = clone_to_tio(clone);
11850fbb4d93SMike Snitzer 	struct dm_io *io = tio->io;
11860fbb4d93SMike Snitzer 
11870fbb4d93SMike Snitzer 	/* establish bio that will get submitted */
11880fbb4d93SMike Snitzer 	if (!tgt_clone)
11890fbb4d93SMike Snitzer 		tgt_clone = clone;
11900fbb4d93SMike Snitzer 
11910fbb4d93SMike Snitzer 	/*
11920fbb4d93SMike Snitzer 	 * Account io->origin_bio to DM dev on behalf of target
11930fbb4d93SMike Snitzer 	 * that took ownership of IO with DM_MAPIO_SUBMITTED.
11940fbb4d93SMike Snitzer 	 */
11950fbb4d93SMike Snitzer 	if (!from_wq) {
11960fbb4d93SMike Snitzer 		/* Still in target's map function */
11970fbb4d93SMike Snitzer 		io->start_io_acct = true;
11980fbb4d93SMike Snitzer 	} else {
11990fbb4d93SMike Snitzer 		/*
12000fbb4d93SMike Snitzer 		 * Called by another thread, managed by DM target,
12010fbb4d93SMike Snitzer 		 * wait for dm_split_and_process_bio() to store
12020fbb4d93SMike Snitzer 		 * io->orig_bio
12030fbb4d93SMike Snitzer 		 */
12040fbb4d93SMike Snitzer 		while (unlikely(!smp_load_acquire(&io->orig_bio)))
12050fbb4d93SMike Snitzer 			msleep(1);
12060fbb4d93SMike Snitzer 		dm_start_io_acct(io, clone);
12070fbb4d93SMike Snitzer 	}
12080fbb4d93SMike Snitzer 
12090fbb4d93SMike Snitzer 	__dm_submit_bio_remap(tgt_clone, disk_devt(io->md->disk),
12100fbb4d93SMike Snitzer 			      tio->old_sector);
12110fbb4d93SMike Snitzer }
12120fbb4d93SMike Snitzer EXPORT_SYMBOL_GPL(dm_submit_bio_remap);
12130fbb4d93SMike Snitzer 
1214a666e5c0SMikulas Patocka static noinline void __set_swap_bios_limit(struct mapped_device *md, int latch)
1215a666e5c0SMikulas Patocka {
1216a666e5c0SMikulas Patocka 	mutex_lock(&md->swap_bios_lock);
1217a666e5c0SMikulas Patocka 	while (latch < md->swap_bios) {
1218a666e5c0SMikulas Patocka 		cond_resched();
1219a666e5c0SMikulas Patocka 		down(&md->swap_bios_semaphore);
1220a666e5c0SMikulas Patocka 		md->swap_bios--;
1221a666e5c0SMikulas Patocka 	}
1222a666e5c0SMikulas Patocka 	while (latch > md->swap_bios) {
1223a666e5c0SMikulas Patocka 		cond_resched();
1224a666e5c0SMikulas Patocka 		up(&md->swap_bios_semaphore);
1225a666e5c0SMikulas Patocka 		md->swap_bios++;
1226a666e5c0SMikulas Patocka 	}
1227a666e5c0SMikulas Patocka 	mutex_unlock(&md->swap_bios_lock);
1228a666e5c0SMikulas Patocka }
1229a666e5c0SMikulas Patocka 
12301561b396SChristoph Hellwig static void __map_bio(struct bio *clone)
12311da177e4SLinus Torvalds {
12321561b396SChristoph Hellwig 	struct dm_target_io *tio = clone_to_tio(clone);
12331da177e4SLinus Torvalds 	int r;
123464f52b0eSMike Snitzer 	struct dm_io *io = tio->io;
1235bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 	/*
12400fbb4d93SMike Snitzer 	 * Map the clone.
12411da177e4SLinus Torvalds 	 */
1242e2118b3cSDamien Le Moal 	dm_io_inc_pending(io);
1243743598f0SMike Snitzer 	tio->old_sector = clone->bi_iter.bi_sector;
1244d67a5f4bSMikulas Patocka 
1245a666e5c0SMikulas Patocka 	if (unlikely(swap_bios_limit(ti, clone))) {
1246a666e5c0SMikulas Patocka 		struct mapped_device *md = io->md;
1247a666e5c0SMikulas Patocka 		int latch = get_swap_bios();
1248a666e5c0SMikulas Patocka 		if (unlikely(latch != md->swap_bios))
1249a666e5c0SMikulas Patocka 			__set_swap_bios_limit(md, latch);
1250a666e5c0SMikulas Patocka 		down(&md->swap_bios_semaphore);
1251a666e5c0SMikulas Patocka 	}
1252a666e5c0SMikulas Patocka 
1253bb37d772SDamien Le Moal 	/*
1254bb37d772SDamien Le Moal 	 * Check if the IO needs a special mapping due to zone append emulation
1255bb37d772SDamien Le Moal 	 * on zoned target. In this case, dm_zone_map_bio() calls the target
1256bb37d772SDamien Le Moal 	 * map operation.
1257bb37d772SDamien Le Moal 	 */
1258bb37d772SDamien Le Moal 	if (dm_emulate_zone_append(io->md))
1259bb37d772SDamien Le Moal 		r = dm_zone_map_bio(tio);
1260bb37d772SDamien Le Moal 	else
12617de3ee57SMikulas Patocka 		r = ti->type->map(ti, clone);
1262bb37d772SDamien Le Moal 
1263846785e6SChristoph Hellwig 	switch (r) {
1264846785e6SChristoph Hellwig 	case DM_MAPIO_SUBMITTED:
12650fbb4d93SMike Snitzer 		/* target has assumed ownership of this io */
12660fbb4d93SMike Snitzer 		if (!ti->accounts_remapped_io)
12670fbb4d93SMike Snitzer 			io->start_io_acct = true;
1268846785e6SChristoph Hellwig 		break;
1269846785e6SChristoph Hellwig 	case DM_MAPIO_REMAPPED:
12700fbb4d93SMike Snitzer 		/*
12710fbb4d93SMike Snitzer 		 * the bio has been remapped so dispatch it, but defer
12720fbb4d93SMike Snitzer 		 * dm_start_io_acct() until after possible bio_split().
12730fbb4d93SMike Snitzer 		 */
12740fbb4d93SMike Snitzer 		__dm_submit_bio_remap(clone, disk_devt(io->md->disk),
1275743598f0SMike Snitzer 				      tio->old_sector);
12760fbb4d93SMike Snitzer 		io->start_io_acct = true;
1277846785e6SChristoph Hellwig 		break;
1278846785e6SChristoph Hellwig 	case DM_MAPIO_KILL:
1279846785e6SChristoph Hellwig 	case DM_MAPIO_REQUEUE:
128090a2326eSMike Snitzer 		if (unlikely(swap_bios_limit(ti, clone)))
128190a2326eSMike Snitzer 			up(&io->md->swap_bios_semaphore);
12821d1068ceSChristoph Hellwig 		free_tio(clone);
128390a2326eSMike Snitzer 		if (r == DM_MAPIO_KILL)
128490a2326eSMike Snitzer 			dm_io_dec_pending(io, BLK_STS_IOERR);
128590a2326eSMike Snitzer 		else
1286e2118b3cSDamien Le Moal 			dm_io_dec_pending(io, BLK_STS_DM_REQUEUE);
1287846785e6SChristoph Hellwig 		break;
1288846785e6SChristoph Hellwig 	default:
128945cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
129045cbcd79SKiyoshi Ueda 		BUG();
12911da177e4SLinus Torvalds 	}
12921da177e4SLinus Torvalds }
12931da177e4SLinus Torvalds 
1294318716ddSMike Snitzer static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci,
1295dc8e2021SChristoph Hellwig 				struct dm_target *ti, unsigned num_bios,
1296dc8e2021SChristoph Hellwig 				unsigned *len)
1297f9ab94ceSMikulas Patocka {
12981d1068ceSChristoph Hellwig 	struct bio *bio;
1299318716ddSMike Snitzer 	int try;
1300dba14160SMikulas Patocka 
1301318716ddSMike Snitzer 	for (try = 0; try < 2; try++) {
1302318716ddSMike Snitzer 		int bio_nr;
1303318716ddSMike Snitzer 
1304318716ddSMike Snitzer 		if (try)
1305bc02cdbeSMike Snitzer 			mutex_lock(&ci->io->md->table_devices_lock);
1306318716ddSMike Snitzer 		for (bio_nr = 0; bio_nr < num_bios; bio_nr++) {
13071d1068ceSChristoph Hellwig 			bio = alloc_tio(ci, ti, bio_nr, len,
1308dc8e2021SChristoph Hellwig 					try ? GFP_NOIO : GFP_NOWAIT);
13091d1068ceSChristoph Hellwig 			if (!bio)
1310318716ddSMike Snitzer 				break;
1311318716ddSMike Snitzer 
13121d1068ceSChristoph Hellwig 			bio_list_add(blist, bio);
1313318716ddSMike Snitzer 		}
1314318716ddSMike Snitzer 		if (try)
1315bc02cdbeSMike Snitzer 			mutex_unlock(&ci->io->md->table_devices_lock);
1316318716ddSMike Snitzer 		if (bio_nr == num_bios)
1317318716ddSMike Snitzer 			return;
1318318716ddSMike Snitzer 
13196c23f0bdSChristoph Hellwig 		while ((bio = bio_list_pop(blist)))
13201d1068ceSChristoph Hellwig 			free_tio(bio);
1321318716ddSMike Snitzer 	}
1322318716ddSMike Snitzer }
1323318716ddSMike Snitzer 
13248eabf5d0SChristoph Hellwig static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
13258eabf5d0SChristoph Hellwig 				  unsigned num_bios, unsigned *len)
13269015df24SAlasdair G Kergon {
13278eabf5d0SChristoph Hellwig 	struct bio_list blist = BIO_EMPTY_LIST;
13288eabf5d0SChristoph Hellwig 	struct bio *clone;
13298eabf5d0SChristoph Hellwig 
1330891fced6SChristoph Hellwig 	switch (num_bios) {
1331891fced6SChristoph Hellwig 	case 0:
1332891fced6SChristoph Hellwig 		break;
1333891fced6SChristoph Hellwig 	case 1:
1334891fced6SChristoph Hellwig 		clone = alloc_tio(ci, ti, 0, len, GFP_NOIO);
1335e6fc9f62SMike Snitzer 		clone_to_tio(clone)->is_duplicate_bio = true;
1336891fced6SChristoph Hellwig 		__map_bio(clone);
1337891fced6SChristoph Hellwig 		break;
1338891fced6SChristoph Hellwig 	default:
1339dc8e2021SChristoph Hellwig 		alloc_multiple_bios(&blist, ci, ti, num_bios, len);
1340e6fc9f62SMike Snitzer 		while ((clone = bio_list_pop(&blist))) {
1341e6fc9f62SMike Snitzer 			clone_to_tio(clone)->is_duplicate_bio = true;
13421561b396SChristoph Hellwig 			__map_bio(clone);
1343e6fc9f62SMike Snitzer 		}
1344891fced6SChristoph Hellwig 		break;
1345891fced6SChristoph Hellwig 	}
134606a426ceSMike Snitzer }
134706a426ceSMike Snitzer 
134814fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1349f9ab94ceSMikulas Patocka {
135006a426ceSMike Snitzer 	unsigned target_nr = 0;
1351f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1352828678b8SMike Snitzer 	struct bio flush_bio;
1353828678b8SMike Snitzer 
1354828678b8SMike Snitzer 	/*
1355828678b8SMike Snitzer 	 * Use an on-stack bio for this, it's safe since we don't
1356828678b8SMike Snitzer 	 * need to reference it after submit. It's just used as
1357828678b8SMike Snitzer 	 * the basis for the clone(s).
1358828678b8SMike Snitzer 	 */
135949add496SChristoph Hellwig 	bio_init(&flush_bio, ci->io->md->disk->part0, NULL, 0,
136049add496SChristoph Hellwig 		 REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC);
136147d95102SChristoph Hellwig 
1362828678b8SMike Snitzer 	ci->bio = &flush_bio;
1363828678b8SMike Snitzer 	ci->sector_count = 0;
1364f9ab94ceSMikulas Patocka 
1365f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
13661dd40c3eSMikulas Patocka 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
1367828678b8SMike Snitzer 
1368828678b8SMike Snitzer 	bio_uninit(ci->bio);
1369f9ab94ceSMikulas Patocka 	return 0;
1370f9ab94ceSMikulas Patocka }
1371f9ab94ceSMikulas Patocka 
1372e6fc9f62SMike Snitzer static void __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti,
137361697a6aSMike Snitzer 					unsigned num_bios)
13745ae89a87SMike Snitzer {
137551b86f9aSMichael Lass 	unsigned len;
13765ae89a87SMike Snitzer 
13773720281dSMike Snitzer 	len = min_t(sector_t, ci->sector_count,
13783720281dSMike Snitzer 		    max_io_len_target_boundary(ti, dm_target_offset(ti, ci->sector)));
137951b86f9aSMichael Lass 
1380e6fc9f62SMike Snitzer 	/*
1381e6fc9f62SMike Snitzer 	 * dm_accept_partial_bio cannot be used with duplicate bios,
1382e6fc9f62SMike Snitzer 	 * so update clone_info cursor before __send_duplicate_bios().
1383e6fc9f62SMike Snitzer 	 */
1384a79245b3SMike Snitzer 	ci->sector += len;
13853d7f4562SMike Snitzer 	ci->sector_count -= len;
13865ae89a87SMike Snitzer 
1387e6fc9f62SMike Snitzer 	__send_duplicate_bios(ci, ti, num_bios, &len);
13885ae89a87SMike Snitzer }
13895ae89a87SMike Snitzer 
1390568c73a3SMike Snitzer static bool is_abnormal_io(struct bio *bio)
1391568c73a3SMike Snitzer {
1392568c73a3SMike Snitzer 	bool r = false;
1393568c73a3SMike Snitzer 
1394568c73a3SMike Snitzer 	switch (bio_op(bio)) {
1395568c73a3SMike Snitzer 	case REQ_OP_DISCARD:
1396568c73a3SMike Snitzer 	case REQ_OP_SECURE_ERASE:
1397568c73a3SMike Snitzer 	case REQ_OP_WRITE_SAME:
1398568c73a3SMike Snitzer 	case REQ_OP_WRITE_ZEROES:
1399568c73a3SMike Snitzer 		r = true;
1400568c73a3SMike Snitzer 		break;
1401568c73a3SMike Snitzer 	}
1402568c73a3SMike Snitzer 
1403568c73a3SMike Snitzer 	return r;
1404568c73a3SMike Snitzer }
1405568c73a3SMike Snitzer 
14060519c71eSMike Snitzer static bool __process_abnormal_io(struct clone_info *ci, struct dm_target *ti,
14070519c71eSMike Snitzer 				  int *result)
14080519c71eSMike Snitzer {
14099679b5a7SMike Snitzer 	unsigned num_bios = 0;
14100519c71eSMike Snitzer 
1411e6fc9f62SMike Snitzer 	switch (bio_op(ci->bio)) {
14129679b5a7SMike Snitzer 	case REQ_OP_DISCARD:
14139679b5a7SMike Snitzer 		num_bios = ti->num_discard_bios;
14149679b5a7SMike Snitzer 		break;
14159679b5a7SMike Snitzer 	case REQ_OP_SECURE_ERASE:
14169679b5a7SMike Snitzer 		num_bios = ti->num_secure_erase_bios;
14179679b5a7SMike Snitzer 		break;
14189679b5a7SMike Snitzer 	case REQ_OP_WRITE_SAME:
14199679b5a7SMike Snitzer 		num_bios = ti->num_write_same_bios;
14209679b5a7SMike Snitzer 		break;
14219679b5a7SMike Snitzer 	case REQ_OP_WRITE_ZEROES:
14229679b5a7SMike Snitzer 		num_bios = ti->num_write_zeroes_bios;
14239679b5a7SMike Snitzer 		break;
14249679b5a7SMike Snitzer 	default:
14250519c71eSMike Snitzer 		return false;
14269679b5a7SMike Snitzer 	}
14270519c71eSMike Snitzer 
1428e6fc9f62SMike Snitzer 	/*
1429e6fc9f62SMike Snitzer 	 * Even though the device advertised support for this type of
1430e6fc9f62SMike Snitzer 	 * request, that does not mean every target supports it, and
1431e6fc9f62SMike Snitzer 	 * reconfiguration might also have changed that since the
1432e6fc9f62SMike Snitzer 	 * check was performed.
1433e6fc9f62SMike Snitzer 	 */
1434e6fc9f62SMike Snitzer 	if (!num_bios)
1435e6fc9f62SMike Snitzer 		*result = -EOPNOTSUPP;
1436e6fc9f62SMike Snitzer 	else {
1437e6fc9f62SMike Snitzer 		__send_changing_extent_only(ci, ti, num_bios);
1438e6fc9f62SMike Snitzer 		*result = 0;
1439e6fc9f62SMike Snitzer 	}
14400519c71eSMike Snitzer 	return true;
14410519c71eSMike Snitzer }
14420519c71eSMike Snitzer 
1443e4c93811SAlasdair G Kergon /*
1444e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1445e4c93811SAlasdair G Kergon  */
144696c9865cSMike Snitzer static int __split_and_process_bio(struct clone_info *ci)
1447e4c93811SAlasdair G Kergon {
144866bdaa43SMike Snitzer 	struct bio *clone;
1449e4c93811SAlasdair G Kergon 	struct dm_target *ti;
14501c3b13e6SKent Overstreet 	unsigned len;
1451c80914e8SMike Snitzer 	int r;
1452e4c93811SAlasdair G Kergon 
1453e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1454123d87d5SMikulas Patocka 	if (!ti)
1455e4c93811SAlasdair G Kergon 		return -EIO;
1456e4c93811SAlasdair G Kergon 
1457568c73a3SMike Snitzer 	if (__process_abnormal_io(ci, ti, &r))
14580519c71eSMike Snitzer 		return r;
14593d7f4562SMike Snitzer 
14603720281dSMike Snitzer 	len = min_t(sector_t, max_io_len(ti, ci->sector), ci->sector_count);
146166bdaa43SMike Snitzer 	clone = alloc_tio(ci, ti, 0, &len, GFP_NOIO);
146266bdaa43SMike Snitzer 	__map_bio(clone);
1463e4c93811SAlasdair G Kergon 
1464e4c93811SAlasdair G Kergon 	ci->sector += len;
1465e4c93811SAlasdair G Kergon 	ci->sector_count -= len;
1466e4c93811SAlasdair G Kergon 
1467e4c93811SAlasdair G Kergon 	return 0;
1468e4c93811SAlasdair G Kergon }
1469e4c93811SAlasdair G Kergon 
1470978e51baSMike Snitzer static void init_clone_info(struct clone_info *ci, struct mapped_device *md,
1471978e51baSMike Snitzer 			    struct dm_table *map, struct bio *bio)
1472978e51baSMike Snitzer {
1473978e51baSMike Snitzer 	ci->map = map;
1474978e51baSMike Snitzer 	ci->io = alloc_io(md, bio);
1475d41e077aSMike Snitzer 	ci->bio = bio;
1476978e51baSMike Snitzer 	ci->sector = bio->bi_iter.bi_sector;
1477d41e077aSMike Snitzer 	ci->sector_count = bio_sectors(bio);
1478d41e077aSMike Snitzer 
1479d41e077aSMike Snitzer 	/* Shouldn't happen but sector_count was being set to 0 so... */
1480d41e077aSMike Snitzer 	if (WARN_ON_ONCE(op_is_zone_mgmt(bio_op(bio)) && ci->sector_count))
1481d41e077aSMike Snitzer 		ci->sector_count = 0;
1482978e51baSMike Snitzer }
1483978e51baSMike Snitzer 
1484e4c93811SAlasdair G Kergon /*
148514fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
14861da177e4SLinus Torvalds  */
148796c9865cSMike Snitzer static void dm_split_and_process_bio(struct mapped_device *md,
148883d5e5b0SMikulas Patocka 				     struct dm_table *map, struct bio *bio)
14891da177e4SLinus Torvalds {
14901da177e4SLinus Torvalds 	struct clone_info ci;
14910fbb4d93SMike Snitzer 	struct bio *orig_bio = NULL;
1492512875bdSJun'ichi Nomura 	int error = 0;
14931da177e4SLinus Torvalds 
1494978e51baSMike Snitzer 	init_clone_info(&ci, md, map, bio);
1495bd2a49b8SAlasdair G Kergon 
14961eff9d32SJens Axboe 	if (bio->bi_opf & REQ_PREFLUSH) {
149714fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1498e2118b3cSDamien Le Moal 		/* dm_io_dec_pending submits any data associated with flush */
1499d41e077aSMike Snitzer 		goto out;
1500d41e077aSMike Snitzer 	}
1501d41e077aSMike Snitzer 
150296c9865cSMike Snitzer 	error = __split_and_process_bio(&ci);
1503d41e077aSMike Snitzer 	if (error || !ci.sector_count)
1504d41e077aSMike Snitzer 		goto out;
1505d41e077aSMike Snitzer 
150618a25da8SNeilBrown 	/*
1507d41e077aSMike Snitzer 	 * Remainder must be passed to submit_bio_noacct() so it gets handled
1508d41e077aSMike Snitzer 	 * *after* bios already submitted have been completely processed.
1509d41e077aSMike Snitzer 	 * We take a clone of the original to store in ci.io->orig_bio to be
1510d41e077aSMike Snitzer 	 * used by dm_end_io_acct() and for dm_io_dec_pending() to use for
1511d41e077aSMike Snitzer 	 * completion handling.
151218a25da8SNeilBrown 	 */
15130fbb4d93SMike Snitzer 	orig_bio = bio_split(bio, bio_sectors(bio) - ci.sector_count,
1514f21c601aSMike Snitzer 			     GFP_NOIO, &md->queue->bio_split);
15150fbb4d93SMike Snitzer 	bio_chain(orig_bio, bio);
15160fbb4d93SMike Snitzer 	trace_block_split(orig_bio, bio->bi_iter.bi_sector);
15173e08773cSChristoph Hellwig 	submit_bio_noacct(bio);
1518d41e077aSMike Snitzer out:
15190fbb4d93SMike Snitzer 	if (!orig_bio)
15200fbb4d93SMike Snitzer 		orig_bio = bio;
15210fbb4d93SMike Snitzer 	smp_store_release(&ci.io->orig_bio, orig_bio);
15220fbb4d93SMike Snitzer 	if (ci.io->start_io_acct)
15230fbb4d93SMike Snitzer 		dm_start_io_acct(ci.io, NULL);
15240fbb4d93SMike Snitzer 
15251da177e4SLinus Torvalds 	/* drop the extra reference count */
1526e2118b3cSDamien Le Moal 	dm_io_dec_pending(ci.io, errno_to_blk_status(error));
15271da177e4SLinus Torvalds }
15281da177e4SLinus Torvalds 
15293e08773cSChristoph Hellwig static void dm_submit_bio(struct bio *bio)
15301da177e4SLinus Torvalds {
1531309dca30SChristoph Hellwig 	struct mapped_device *md = bio->bi_bdev->bd_disk->private_data;
153283d5e5b0SMikulas Patocka 	int srcu_idx;
153383d5e5b0SMikulas Patocka 	struct dm_table *map;
15341da177e4SLinus Torvalds 
153583d5e5b0SMikulas Patocka 	map = dm_get_live_table(md, &srcu_idx);
15366a8736d1STejun Heo 
1537fa247089SMike Snitzer 	/* If suspended, or map not yet available, queue this IO for later */
1538fa247089SMike Snitzer 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) ||
1539fa247089SMike Snitzer 	    unlikely(!map)) {
15406abc4946SKonstantin Khlebnikov 		if (bio->bi_opf & REQ_NOWAIT)
15416abc4946SKonstantin Khlebnikov 			bio_wouldblock_error(bio);
1542b2abdb1bSMike Snitzer 		else if (bio->bi_opf & REQ_RAHEAD)
15436a8736d1STejun Heo 			bio_io_error(bio);
1544b2abdb1bSMike Snitzer 		else
1545b2abdb1bSMike Snitzer 			queue_io(md, bio);
1546b2abdb1bSMike Snitzer 		goto out;
15471da177e4SLinus Torvalds 	}
15481da177e4SLinus Torvalds 
1549b2abdb1bSMike Snitzer 	/*
1550b2abdb1bSMike Snitzer 	 * Use blk_queue_split() for abnormal IO (e.g. discard, writesame, etc)
1551b2abdb1bSMike Snitzer 	 * otherwise associated queue_limits won't be imposed.
1552b2abdb1bSMike Snitzer 	 */
1553b2abdb1bSMike Snitzer 	if (is_abnormal_io(bio))
1554b2abdb1bSMike Snitzer 		blk_queue_split(&bio);
1555978e51baSMike Snitzer 
155696c9865cSMike Snitzer 	dm_split_and_process_bio(md, map, bio);
1557b2abdb1bSMike Snitzer out:
155883d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
1559978e51baSMike Snitzer }
1560978e51baSMike Snitzer 
15611da177e4SLinus Torvalds /*-----------------------------------------------------------------
15621da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
15631da177e4SLinus Torvalds  *---------------------------------------------------------------*/
15642b06cfffSAlasdair G Kergon static void free_minor(int minor)
15651da177e4SLinus Torvalds {
1566f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
15671da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1568f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
15691da177e4SLinus Torvalds }
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds /*
15721da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
15731da177e4SLinus Torvalds  */
1574cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
15751da177e4SLinus Torvalds {
1576c9d76be6STejun Heo 	int r;
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
15791da177e4SLinus Torvalds 		return -EINVAL;
15801da177e4SLinus Torvalds 
1581c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1582f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
15831da177e4SLinus Torvalds 
1584c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
15851da177e4SLinus Torvalds 
1586f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1587c9d76be6STejun Heo 	idr_preload_end();
1588c9d76be6STejun Heo 	if (r < 0)
1589c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
1590c9d76be6STejun Heo 	return 0;
15911da177e4SLinus Torvalds }
15921da177e4SLinus Torvalds 
1593cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
15941da177e4SLinus Torvalds {
1595c9d76be6STejun Heo 	int r;
15961da177e4SLinus Torvalds 
1597c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1598f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
15991da177e4SLinus Torvalds 
1600c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
16011da177e4SLinus Torvalds 
1602f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1603c9d76be6STejun Heo 	idr_preload_end();
1604c9d76be6STejun Heo 	if (r < 0)
16051da177e4SLinus Torvalds 		return r;
1606c9d76be6STejun Heo 	*minor = r;
1607c9d76be6STejun Heo 	return 0;
16081da177e4SLinus Torvalds }
16091da177e4SLinus Torvalds 
161083d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
1611681cc5e8SMike Snitzer static const struct block_device_operations dm_rq_blk_dops;
1612f26c5719SDan Williams static const struct dax_operations dm_dax_ops;
16131da177e4SLinus Torvalds 
161453d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
161553d5914fSMikulas Patocka 
1616aa6ce87aSSatya Tangirala #ifdef CONFIG_BLK_INLINE_ENCRYPTION
1617cb77cb5aSEric Biggers static void dm_queue_destroy_crypto_profile(struct request_queue *q)
1618aa6ce87aSSatya Tangirala {
1619cb77cb5aSEric Biggers 	dm_destroy_crypto_profile(q->crypto_profile);
1620aa6ce87aSSatya Tangirala }
1621aa6ce87aSSatya Tangirala 
1622aa6ce87aSSatya Tangirala #else /* CONFIG_BLK_INLINE_ENCRYPTION */
1623aa6ce87aSSatya Tangirala 
1624cb77cb5aSEric Biggers static inline void dm_queue_destroy_crypto_profile(struct request_queue *q)
1625aa6ce87aSSatya Tangirala {
1626aa6ce87aSSatya Tangirala }
1627aa6ce87aSSatya Tangirala #endif /* !CONFIG_BLK_INLINE_ENCRYPTION */
1628aa6ce87aSSatya Tangirala 
16290f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md)
16300f20972fSMike Snitzer {
16310f20972fSMike Snitzer 	if (md->wq)
16320f20972fSMike Snitzer 		destroy_workqueue(md->wq);
16336f1c819cSKent Overstreet 	bioset_exit(&md->bs);
16346f1c819cSKent Overstreet 	bioset_exit(&md->io_bs);
16350f20972fSMike Snitzer 
1636f26c5719SDan Williams 	if (md->dax_dev) {
1637fb08a190SChristoph Hellwig 		dax_remove_host(md->disk);
1638f26c5719SDan Williams 		kill_dax(md->dax_dev);
1639f26c5719SDan Williams 		put_dax(md->dax_dev);
1640f26c5719SDan Williams 		md->dax_dev = NULL;
1641f26c5719SDan Williams 	}
1642f26c5719SDan Williams 
1643588b7f5dSKirill Tkhai 	dm_cleanup_zoned_dev(md);
16440f20972fSMike Snitzer 	if (md->disk) {
16450f20972fSMike Snitzer 		spin_lock(&_minor_lock);
16460f20972fSMike Snitzer 		md->disk->private_data = NULL;
16470f20972fSMike Snitzer 		spin_unlock(&_minor_lock);
164889f871afSChristoph Hellwig 		if (dm_get_md_type(md) != DM_TYPE_NONE) {
164989f871afSChristoph Hellwig 			dm_sysfs_exit(md);
16500f20972fSMike Snitzer 			del_gendisk(md->disk);
165189f871afSChristoph Hellwig 		}
1652cb77cb5aSEric Biggers 		dm_queue_destroy_crypto_profile(md->queue);
165374fe6ba9SChristoph Hellwig 		blk_cleanup_disk(md->disk);
165474a2b6ecSChristoph Hellwig 	}
16550f20972fSMike Snitzer 
16569f6dc633SMike Snitzer 	if (md->pending_io) {
16579f6dc633SMike Snitzer 		free_percpu(md->pending_io);
16589f6dc633SMike Snitzer 		md->pending_io = NULL;
16599f6dc633SMike Snitzer 	}
16609f6dc633SMike Snitzer 
1661d09960b0STahsin Erdogan 	cleanup_srcu_struct(&md->io_barrier);
1662d09960b0STahsin Erdogan 
1663d5ffebddSMike Snitzer 	mutex_destroy(&md->suspend_lock);
1664d5ffebddSMike Snitzer 	mutex_destroy(&md->type_lock);
1665d5ffebddSMike Snitzer 	mutex_destroy(&md->table_devices_lock);
1666a666e5c0SMikulas Patocka 	mutex_destroy(&md->swap_bios_lock);
1667d5ffebddSMike Snitzer 
16684cc96131SMike Snitzer 	dm_mq_cleanup_mapped_device(md);
16690f20972fSMike Snitzer }
16700f20972fSMike Snitzer 
16711da177e4SLinus Torvalds /*
16721da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
16731da177e4SLinus Torvalds  */
16742b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
16751da177e4SLinus Torvalds {
1676115485e8SMike Snitzer 	int r, numa_node_id = dm_get_numa_node();
1677115485e8SMike Snitzer 	struct mapped_device *md;
1678ba61fdd1SJeff Mahoney 	void *old_md;
16791da177e4SLinus Torvalds 
1680856eb091SMikulas Patocka 	md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
16811da177e4SLinus Torvalds 	if (!md) {
16821da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
16831da177e4SLinus Torvalds 		return NULL;
16841da177e4SLinus Torvalds 	}
16851da177e4SLinus Torvalds 
168610da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
16876ed7ade8SMilan Broz 		goto bad_module_get;
168810da4f79SJeff Mahoney 
16891da177e4SLinus Torvalds 	/* get a minor number for the dev */
16902b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1691cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
16922b06cfffSAlasdair G Kergon 	else
1693cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
16941da177e4SLinus Torvalds 	if (r < 0)
16956ed7ade8SMilan Broz 		goto bad_minor;
16961da177e4SLinus Torvalds 
169783d5e5b0SMikulas Patocka 	r = init_srcu_struct(&md->io_barrier);
169883d5e5b0SMikulas Patocka 	if (r < 0)
169983d5e5b0SMikulas Patocka 		goto bad_io_barrier;
170083d5e5b0SMikulas Patocka 
1701115485e8SMike Snitzer 	md->numa_node_id = numa_node_id;
1702591ddcfcSMike Snitzer 	md->init_tio_pdu = false;
1703a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
1704e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1705a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
170686f1152bSBenjamin Marzinski 	mutex_init(&md->table_devices_lock);
1707022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
17081da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
17095c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
17101da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
17117a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
17127a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
171386f1152bSBenjamin Marzinski 	INIT_LIST_HEAD(&md->table_devices);
17147a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
17151da177e4SLinus Torvalds 
171647ace7e0SMike Snitzer 	/*
1717c62b37d9SChristoph Hellwig 	 * default to bio-based until DM table is loaded and md->type
1718c62b37d9SChristoph Hellwig 	 * established. If request-based table is loaded: blk-mq will
1719c62b37d9SChristoph Hellwig 	 * override accordingly.
172047ace7e0SMike Snitzer 	 */
172174fe6ba9SChristoph Hellwig 	md->disk = blk_alloc_disk(md->numa_node_id);
17221da177e4SLinus Torvalds 	if (!md->disk)
17230f20972fSMike Snitzer 		goto bad;
172474fe6ba9SChristoph Hellwig 	md->queue = md->disk->queue;
17251da177e4SLinus Torvalds 
1726f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
172753d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1728f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
17292995fa78SMikulas Patocka 	init_completion(&md->kobj_holder.completion);
1730f0b04115SJeff Mahoney 
1731a666e5c0SMikulas Patocka 	md->swap_bios = get_swap_bios();
1732a666e5c0SMikulas Patocka 	sema_init(&md->swap_bios_semaphore, md->swap_bios);
1733a666e5c0SMikulas Patocka 	mutex_init(&md->swap_bios_lock);
1734a666e5c0SMikulas Patocka 
17351da177e4SLinus Torvalds 	md->disk->major = _major;
17361da177e4SLinus Torvalds 	md->disk->first_minor = minor;
173774fe6ba9SChristoph Hellwig 	md->disk->minors = 1;
17381ebe2e5fSChristoph Hellwig 	md->disk->flags |= GENHD_FL_NO_PART;
17391da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
17401da177e4SLinus Torvalds 	md->disk->queue = md->queue;
17411da177e4SLinus Torvalds 	md->disk->private_data = md;
17421da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
1743f26c5719SDan Williams 
17445d2a228bSChristoph Hellwig 	if (IS_ENABLED(CONFIG_FS_DAX)) {
174530c6828aSChristoph Hellwig 		md->dax_dev = alloc_dax(md, &dm_dax_ops);
1746d7519392SChristoph Hellwig 		if (IS_ERR(md->dax_dev)) {
1747d7519392SChristoph Hellwig 			md->dax_dev = NULL;
1748f26c5719SDan Williams 			goto bad;
1749976431b0SDan Williams 		}
17507ac5360cSChristoph Hellwig 		set_dax_nocache(md->dax_dev);
17517ac5360cSChristoph Hellwig 		set_dax_nomc(md->dax_dev);
1752fb08a190SChristoph Hellwig 		if (dax_add_host(md->dax_dev, md->disk))
1753f26c5719SDan Williams 			goto bad;
1754f26c5719SDan Williams 	}
17551da177e4SLinus Torvalds 
17567e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
17571da177e4SLinus Torvalds 
1758c7c879eeSMichał Mirosław 	md->wq = alloc_workqueue("kdmflush/%s", WQ_MEM_RECLAIM, 0, md->name);
1759304f3f6aSMilan Broz 	if (!md->wq)
17600f20972fSMike Snitzer 		goto bad;
1761304f3f6aSMilan Broz 
17629f6dc633SMike Snitzer 	md->pending_io = alloc_percpu(unsigned long);
17639f6dc633SMike Snitzer 	if (!md->pending_io)
17649f6dc633SMike Snitzer 		goto bad;
17659f6dc633SMike Snitzer 
1766fd2ed4d2SMikulas Patocka 	dm_stats_init(&md->stats);
1767fd2ed4d2SMikulas Patocka 
1768ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1769f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1770ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1771f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1772ba61fdd1SJeff Mahoney 
1773ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1774ba61fdd1SJeff Mahoney 
17751da177e4SLinus Torvalds 	return md;
17761da177e4SLinus Torvalds 
17770f20972fSMike Snitzer bad:
17780f20972fSMike Snitzer 	cleanup_mapped_device(md);
177983d5e5b0SMikulas Patocka bad_io_barrier:
17801da177e4SLinus Torvalds 	free_minor(minor);
17816ed7ade8SMilan Broz bad_minor:
178210da4f79SJeff Mahoney 	module_put(THIS_MODULE);
17836ed7ade8SMilan Broz bad_module_get:
1784856eb091SMikulas Patocka 	kvfree(md);
17851da177e4SLinus Torvalds 	return NULL;
17861da177e4SLinus Torvalds }
17871da177e4SLinus Torvalds 
1788ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1789ae9da83fSJun'ichi Nomura 
17901da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
17911da177e4SLinus Torvalds {
1792f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
179363d94e48SJun'ichi Nomura 
1794ae9da83fSJun'ichi Nomura 	unlock_fs(md);
17952eb6e1e3SKeith Busch 
17960f20972fSMike Snitzer 	cleanup_mapped_device(md);
17970f20972fSMike Snitzer 
17980f20972fSMike Snitzer 	free_table_devices(&md->table_devices);
17990f20972fSMike Snitzer 	dm_stats_cleanup(&md->stats);
180063a4f065SMike Snitzer 	free_minor(minor);
180163a4f065SMike Snitzer 
180210da4f79SJeff Mahoney 	module_put(THIS_MODULE);
1803856eb091SMikulas Patocka 	kvfree(md);
18041da177e4SLinus Torvalds }
18051da177e4SLinus Torvalds 
18062a2a4c51SJens Axboe static int __bind_mempools(struct mapped_device *md, struct dm_table *t)
1807e6ee8c0bSKiyoshi Ueda {
1808c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
18092a2a4c51SJens Axboe 	int ret = 0;
1810e6ee8c0bSKiyoshi Ueda 
1811545ed20eSToshi Kani 	if (dm_table_bio_based(t)) {
1812c0820cf5SMikulas Patocka 		/*
181364f52b0eSMike Snitzer 		 * The md may already have mempools that need changing.
181464f52b0eSMike Snitzer 		 * If so, reload bioset because front_pad may have changed
181516245bdcSJun'ichi Nomura 		 * because a different table was loaded.
1816c0820cf5SMikulas Patocka 		 */
18176f1c819cSKent Overstreet 		bioset_exit(&md->bs);
18186f1c819cSKent Overstreet 		bioset_exit(&md->io_bs);
18190776aa0eSMike Snitzer 
18206f1c819cSKent Overstreet 	} else if (bioset_initialized(&md->bs)) {
1821cbc4e3c1SMike Snitzer 		/*
18224e6e36c3SMike Snitzer 		 * There's no need to reload with request-based dm
18234e6e36c3SMike Snitzer 		 * because the size of front_pad doesn't change.
18244e6e36c3SMike Snitzer 		 * Note for future: If you are to reload bioset,
18254e6e36c3SMike Snitzer 		 * prep-ed requests in the queue may refer
18264e6e36c3SMike Snitzer 		 * to bio from the old bioset, so you must walk
18274e6e36c3SMike Snitzer 		 * through the queue to unprep.
1828cbc4e3c1SMike Snitzer 		 */
1829cbc4e3c1SMike Snitzer 		goto out;
1830cbc4e3c1SMike Snitzer 	}
1831cbc4e3c1SMike Snitzer 
18326f1c819cSKent Overstreet 	BUG_ON(!p ||
18336f1c819cSKent Overstreet 	       bioset_initialized(&md->bs) ||
18346f1c819cSKent Overstreet 	       bioset_initialized(&md->io_bs));
1835e6ee8c0bSKiyoshi Ueda 
18362a2a4c51SJens Axboe 	ret = bioset_init_from_src(&md->bs, &p->bs);
18372a2a4c51SJens Axboe 	if (ret)
18382a2a4c51SJens Axboe 		goto out;
18392a2a4c51SJens Axboe 	ret = bioset_init_from_src(&md->io_bs, &p->io_bs);
18402a2a4c51SJens Axboe 	if (ret)
18412a2a4c51SJens Axboe 		bioset_exit(&md->bs);
1842e6ee8c0bSKiyoshi Ueda out:
184302233342SMike Snitzer 	/* mempool bind completed, no longer need any mempools in the table */
1844e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
18452a2a4c51SJens Axboe 	return ret;
1846e6ee8c0bSKiyoshi Ueda }
1847e6ee8c0bSKiyoshi Ueda 
18481da177e4SLinus Torvalds /*
18491da177e4SLinus Torvalds  * Bind a table to the device.
18501da177e4SLinus Torvalds  */
18511da177e4SLinus Torvalds static void event_callback(void *context)
18521da177e4SLinus Torvalds {
18537a8c3d3bSMike Anderson 	unsigned long flags;
18547a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
18551da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
18561da177e4SLinus Torvalds 
18577a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
18587a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
18597a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
18607a8c3d3bSMike Anderson 
1861ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
18627a8c3d3bSMike Anderson 
18631da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
18641da177e4SLinus Torvalds 	wake_up(&md->eventq);
186562e08243SMikulas Patocka 	dm_issue_global_event();
18661da177e4SLinus Torvalds }
18671da177e4SLinus Torvalds 
1868c217649bSMike Snitzer /*
1869042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
1870042d2a9bSAlasdair G Kergon  */
1871042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
1872754c5fc7SMike Snitzer 			       struct queue_limits *limits)
18731da177e4SLinus Torvalds {
1874042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
18751da177e4SLinus Torvalds 	sector_t size;
18762a2a4c51SJens Axboe 	int ret;
18771da177e4SLinus Torvalds 
18785a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
18795a8f1f80SBart Van Assche 
18801da177e4SLinus Torvalds 	size = dm_table_get_size(t);
18813ac51e74SDarrick J. Wong 
18823ac51e74SDarrick J. Wong 	/*
18833ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
18843ac51e74SDarrick J. Wong 	 */
1885fd2ed4d2SMikulas Patocka 	if (size != dm_get_size(md))
18863ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
18873ac51e74SDarrick J. Wong 
18885424a0b8SMikulas Patocka 	if (!get_capacity(md->disk))
18895424a0b8SMikulas Patocka 		set_capacity(md->disk, size);
18905424a0b8SMikulas Patocka 	else
1891f64d9b2eSChristoph Hellwig 		set_capacity_and_notify(md->disk, size);
18921da177e4SLinus Torvalds 
1893cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
18942ca3310eSAlasdair G Kergon 
1895*f5b4aee1SMike Snitzer 	if (dm_table_request_based(t)) {
189616f12266SMike Snitzer 		/*
18979c37de29SMike Snitzer 		 * Leverage the fact that request-based DM targets are
18989c37de29SMike Snitzer 		 * immutable singletons - used to optimize dm_mq_queue_rq.
189916f12266SMike Snitzer 		 */
190016f12266SMike Snitzer 		md->immutable_target = dm_table_get_immutable_target(t);
190116f12266SMike Snitzer 	}
1902e6ee8c0bSKiyoshi Ueda 
19032a2a4c51SJens Axboe 	ret = __bind_mempools(md, t);
19042a2a4c51SJens Axboe 	if (ret) {
19052a2a4c51SJens Axboe 		old_map = ERR_PTR(ret);
19062a2a4c51SJens Axboe 		goto out;
19072a2a4c51SJens Axboe 	}
1908e6ee8c0bSKiyoshi Ueda 
1909*f5b4aee1SMike Snitzer 	ret = dm_table_set_restrictions(t, md->queue, limits);
1910bb37d772SDamien Le Moal 	if (ret) {
1911bb37d772SDamien Le Moal 		old_map = ERR_PTR(ret);
1912bb37d772SDamien Le Moal 		goto out;
1913bb37d772SDamien Le Moal 	}
1914bb37d772SDamien Le Moal 
1915a12f5d48SEric Dumazet 	old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
19161d3aa6f6SMike Snitzer 	rcu_assign_pointer(md->map, (void *)t);
191736a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
191836a0456fSAlasdair G Kergon 
191941abc4e1SHannes Reinecke 	if (old_map)
192083d5e5b0SMikulas Patocka 		dm_sync_table(md);
19212a2a4c51SJens Axboe out:
1922042d2a9bSAlasdair G Kergon 	return old_map;
19231da177e4SLinus Torvalds }
19241da177e4SLinus Torvalds 
1925a7940155SAlasdair G Kergon /*
1926a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
1927a7940155SAlasdair G Kergon  */
1928a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
19291da177e4SLinus Torvalds {
1930a12f5d48SEric Dumazet 	struct dm_table *map = rcu_dereference_protected(md->map, 1);
19311da177e4SLinus Torvalds 
19321da177e4SLinus Torvalds 	if (!map)
1933a7940155SAlasdair G Kergon 		return NULL;
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
19369cdb8520SMonam Agarwal 	RCU_INIT_POINTER(md->map, NULL);
193783d5e5b0SMikulas Patocka 	dm_sync_table(md);
1938a7940155SAlasdair G Kergon 
1939a7940155SAlasdair G Kergon 	return map;
19401da177e4SLinus Torvalds }
19411da177e4SLinus Torvalds 
19421da177e4SLinus Torvalds /*
19431da177e4SLinus Torvalds  * Constructor for a new device.
19441da177e4SLinus Torvalds  */
19452b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
19461da177e4SLinus Torvalds {
19471da177e4SLinus Torvalds 	struct mapped_device *md;
19481da177e4SLinus Torvalds 
19492b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
19501da177e4SLinus Torvalds 	if (!md)
19511da177e4SLinus Torvalds 		return -ENXIO;
19521da177e4SLinus Torvalds 
195391ccbbacSTushar Sugandhi 	dm_ima_reset_data(md);
195491ccbbacSTushar Sugandhi 
19551da177e4SLinus Torvalds 	*result = md;
19561da177e4SLinus Torvalds 	return 0;
19571da177e4SLinus Torvalds }
19581da177e4SLinus Torvalds 
1959a5664dadSMike Snitzer /*
1960a5664dadSMike Snitzer  * Functions to manage md->type.
1961a5664dadSMike Snitzer  * All are required to hold md->type_lock.
1962a5664dadSMike Snitzer  */
1963a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
1964a5664dadSMike Snitzer {
1965a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
1966a5664dadSMike Snitzer }
1967a5664dadSMike Snitzer 
1968a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
1969a5664dadSMike Snitzer {
1970a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
1971a5664dadSMike Snitzer }
1972a5664dadSMike Snitzer 
19737e0d574fSBart Van Assche void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
1974a5664dadSMike Snitzer {
197500c4fc3bSMike Snitzer 	BUG_ON(!mutex_is_locked(&md->type_lock));
1976a5664dadSMike Snitzer 	md->type = type;
1977a5664dadSMike Snitzer }
1978a5664dadSMike Snitzer 
19797e0d574fSBart Van Assche enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
1980a5664dadSMike Snitzer {
1981a5664dadSMike Snitzer 	return md->type;
1982a5664dadSMike Snitzer }
1983a5664dadSMike Snitzer 
198436a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
198536a0456fSAlasdair G Kergon {
198636a0456fSAlasdair G Kergon 	return md->immutable_target_type;
198736a0456fSAlasdair G Kergon }
198836a0456fSAlasdair G Kergon 
19894a0b4ddfSMike Snitzer /*
1990f84cb8a4SMike Snitzer  * The queue_limits are only valid as long as you have a reference
1991f84cb8a4SMike Snitzer  * count on 'md'.
1992f84cb8a4SMike Snitzer  */
1993f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
1994f84cb8a4SMike Snitzer {
1995f84cb8a4SMike Snitzer 	BUG_ON(!atomic_read(&md->holders));
1996f84cb8a4SMike Snitzer 	return &md->queue->limits;
1997f84cb8a4SMike Snitzer }
1998f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits);
1999f84cb8a4SMike Snitzer 
20004a0b4ddfSMike Snitzer /*
20014a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
20024a0b4ddfSMike Snitzer  */
2003591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
20044a0b4ddfSMike Snitzer {
2005ba305859SChristoph Hellwig 	enum dm_queue_mode type = dm_table_get_type(t);
2006c100ec49SMike Snitzer 	struct queue_limits limits;
2007ba305859SChristoph Hellwig 	int r;
2008bfebd1cdSMike Snitzer 
2009545ed20eSToshi Kani 	switch (type) {
2010bfebd1cdSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
2011681cc5e8SMike Snitzer 		md->disk->fops = &dm_rq_blk_dops;
2012e83068a5SMike Snitzer 		r = dm_mq_init_request_queue(md, t);
2013bfebd1cdSMike Snitzer 		if (r) {
2014681cc5e8SMike Snitzer 			DMERR("Cannot initialize queue for request-based dm mapped device");
2015bfebd1cdSMike Snitzer 			return r;
2016bfebd1cdSMike Snitzer 		}
2017bfebd1cdSMike Snitzer 		break;
2018bfebd1cdSMike Snitzer 	case DM_TYPE_BIO_BASED:
2019545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
2020bfebd1cdSMike Snitzer 		break;
20217e0d574fSBart Van Assche 	case DM_TYPE_NONE:
20227e0d574fSBart Van Assche 		WARN_ON_ONCE(true);
20237e0d574fSBart Van Assche 		break;
2024ff36ab34SMike Snitzer 	}
20254a0b4ddfSMike Snitzer 
2026c100ec49SMike Snitzer 	r = dm_calculate_queue_limits(t, &limits);
2027c100ec49SMike Snitzer 	if (r) {
2028c100ec49SMike Snitzer 		DMERR("Cannot calculate initial queue limits");
2029c100ec49SMike Snitzer 		return r;
2030c100ec49SMike Snitzer 	}
2031bb37d772SDamien Le Moal 	r = dm_table_set_restrictions(t, md->queue, &limits);
2032bb37d772SDamien Le Moal 	if (r)
2033bb37d772SDamien Le Moal 		return r;
203489f871afSChristoph Hellwig 
2035e7089f65SLuis Chamberlain 	r = add_disk(md->disk);
2036e7089f65SLuis Chamberlain 	if (r)
2037e7089f65SLuis Chamberlain 		return r;
203889f871afSChristoph Hellwig 
203989f871afSChristoph Hellwig 	r = dm_sysfs_init(md);
204089f871afSChristoph Hellwig 	if (r) {
204189f871afSChristoph Hellwig 		del_gendisk(md->disk);
204289f871afSChristoph Hellwig 		return r;
204389f871afSChristoph Hellwig 	}
2044ba305859SChristoph Hellwig 	md->type = type;
20454a0b4ddfSMike Snitzer 	return 0;
20464a0b4ddfSMike Snitzer }
20474a0b4ddfSMike Snitzer 
20482bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev)
20491da177e4SLinus Torvalds {
20501da177e4SLinus Torvalds 	struct mapped_device *md;
20511da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
20521da177e4SLinus Torvalds 
20531da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
20541da177e4SLinus Torvalds 		return NULL;
20551da177e4SLinus Torvalds 
2056f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
20571da177e4SLinus Torvalds 
20581da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
205949de5769SMike Snitzer 	if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) ||
206049de5769SMike Snitzer 	    test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2061637842cfSDavid Teigland 		md = NULL;
2062fba9f90eSJeff Mahoney 		goto out;
2063fba9f90eSJeff Mahoney 	}
20642bec1f4aSMikulas Patocka 	dm_get(md);
2065fba9f90eSJeff Mahoney out:
2066f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
20671da177e4SLinus Torvalds 
2068637842cfSDavid Teigland 	return md;
2069637842cfSDavid Teigland }
20703cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2071d229a958SDavid Teigland 
20729ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2073637842cfSDavid Teigland {
20749ade92a9SAlasdair G Kergon 	return md->interface_ptr;
20751da177e4SLinus Torvalds }
20761da177e4SLinus Torvalds 
20771da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
20781da177e4SLinus Torvalds {
20791da177e4SLinus Torvalds 	md->interface_ptr = ptr;
20801da177e4SLinus Torvalds }
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
20831da177e4SLinus Torvalds {
20841da177e4SLinus Torvalds 	atomic_inc(&md->holders);
20853f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
20861da177e4SLinus Torvalds }
20871da177e4SLinus Torvalds 
208809ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md)
208909ee96b2SMikulas Patocka {
209009ee96b2SMikulas Patocka 	spin_lock(&_minor_lock);
209109ee96b2SMikulas Patocka 	if (test_bit(DMF_FREEING, &md->flags)) {
209209ee96b2SMikulas Patocka 		spin_unlock(&_minor_lock);
209309ee96b2SMikulas Patocka 		return -EBUSY;
209409ee96b2SMikulas Patocka 	}
209509ee96b2SMikulas Patocka 	dm_get(md);
209609ee96b2SMikulas Patocka 	spin_unlock(&_minor_lock);
209709ee96b2SMikulas Patocka 	return 0;
209809ee96b2SMikulas Patocka }
209909ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold);
210009ee96b2SMikulas Patocka 
210172d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
210272d94861SAlasdair G Kergon {
210372d94861SAlasdair G Kergon 	return md->name;
210472d94861SAlasdair G Kergon }
210572d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
210672d94861SAlasdair G Kergon 
21073f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
21081da177e4SLinus Torvalds {
21091134e5aeSMike Anderson 	struct dm_table *map;
211083d5e5b0SMikulas Patocka 	int srcu_idx;
21111da177e4SLinus Torvalds 
21123f77316dSKiyoshi Ueda 	might_sleep();
2113fba9f90eSJeff Mahoney 
211463a4f065SMike Snitzer 	spin_lock(&_minor_lock);
21153f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2116fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2117f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
21183f77316dSKiyoshi Ueda 
2119c12c9a3cSMike Snitzer 	blk_set_queue_dying(md->queue);
21203b785fbcSBart Van Assche 
2121ab7c7bb6SMikulas Patocka 	/*
2122ab7c7bb6SMikulas Patocka 	 * Take suspend_lock so that presuspend and postsuspend methods
2123ab7c7bb6SMikulas Patocka 	 * do not race with internal suspend.
2124ab7c7bb6SMikulas Patocka 	 */
2125ab7c7bb6SMikulas Patocka 	mutex_lock(&md->suspend_lock);
21262a708cffSJunichi Nomura 	map = dm_get_live_table(md, &srcu_idx);
21274f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
21281da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
2129adc0daadSMikulas Patocka 		set_bit(DMF_SUSPENDED, &md->flags);
21305df96f2bSMikulas Patocka 		set_bit(DMF_POST_SUSPENDING, &md->flags);
21311da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
21321da177e4SLinus Torvalds 	}
213383d5e5b0SMikulas Patocka 	/* dm_put_live_table must be before msleep, otherwise deadlock is possible */
213483d5e5b0SMikulas Patocka 	dm_put_live_table(md, srcu_idx);
21352a708cffSJunichi Nomura 	mutex_unlock(&md->suspend_lock);
213683d5e5b0SMikulas Patocka 
21373f77316dSKiyoshi Ueda 	/*
21383f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
21393f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
21403f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
21413f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
21423f77316dSKiyoshi Ueda 	 */
21433f77316dSKiyoshi Ueda 	if (wait)
21443f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
21453f77316dSKiyoshi Ueda 			msleep(1);
21463f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
21473f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
21483f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
21493f77316dSKiyoshi Ueda 
2150a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
21511da177e4SLinus Torvalds 	free_dev(md);
21521da177e4SLinus Torvalds }
21533f77316dSKiyoshi Ueda 
21543f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
21553f77316dSKiyoshi Ueda {
21563f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
21573f77316dSKiyoshi Ueda }
21583f77316dSKiyoshi Ueda 
21593f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
21603f77316dSKiyoshi Ueda {
21613f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
21623f77316dSKiyoshi Ueda }
21633f77316dSKiyoshi Ueda 
21643f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
21653f77316dSKiyoshi Ueda {
21663f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
21671da177e4SLinus Torvalds }
216879eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
21691da177e4SLinus Torvalds 
21709f6dc633SMike Snitzer static bool dm_in_flight_bios(struct mapped_device *md)
217185067747SMing Lei {
217285067747SMing Lei 	int cpu;
21739f6dc633SMike Snitzer 	unsigned long sum = 0;
217485067747SMing Lei 
21759f6dc633SMike Snitzer 	for_each_possible_cpu(cpu)
21769f6dc633SMike Snitzer 		sum += *per_cpu_ptr(md->pending_io, cpu);
217785067747SMing Lei 
217885067747SMing Lei 	return sum != 0;
217985067747SMing Lei }
218085067747SMing Lei 
21812f064a59SPeter Zijlstra static int dm_wait_for_bios_completion(struct mapped_device *md, unsigned int task_state)
218246125c1cSMilan Broz {
218346125c1cSMilan Broz 	int r = 0;
21849f4c3f87SBart Van Assche 	DEFINE_WAIT(wait);
218546125c1cSMilan Broz 
218685067747SMing Lei 	while (true) {
21879f4c3f87SBart Van Assche 		prepare_to_wait(&md->wait, &wait, task_state);
218846125c1cSMilan Broz 
21899f6dc633SMike Snitzer 		if (!dm_in_flight_bios(md))
219046125c1cSMilan Broz 			break;
219146125c1cSMilan Broz 
2192e3fabdfdSBart Van Assche 		if (signal_pending_state(task_state, current)) {
219346125c1cSMilan Broz 			r = -EINTR;
219446125c1cSMilan Broz 			break;
219546125c1cSMilan Broz 		}
219646125c1cSMilan Broz 
219746125c1cSMilan Broz 		io_schedule();
219846125c1cSMilan Broz 	}
21999f4c3f87SBart Van Assche 	finish_wait(&md->wait, &wait);
2200b44ebeb0SMikulas Patocka 
22019f6dc633SMike Snitzer 	smp_rmb();
22029f6dc633SMike Snitzer 
220346125c1cSMilan Broz 	return r;
220446125c1cSMilan Broz }
220546125c1cSMilan Broz 
22062f064a59SPeter Zijlstra static int dm_wait_for_completion(struct mapped_device *md, unsigned int task_state)
220785067747SMing Lei {
220885067747SMing Lei 	int r = 0;
220985067747SMing Lei 
221085067747SMing Lei 	if (!queue_is_mq(md->queue))
221185067747SMing Lei 		return dm_wait_for_bios_completion(md, task_state);
221285067747SMing Lei 
221385067747SMing Lei 	while (true) {
221485067747SMing Lei 		if (!blk_mq_queue_inflight(md->queue))
221585067747SMing Lei 			break;
221685067747SMing Lei 
221785067747SMing Lei 		if (signal_pending_state(task_state, current)) {
221885067747SMing Lei 			r = -EINTR;
221985067747SMing Lei 			break;
222085067747SMing Lei 		}
222185067747SMing Lei 
222285067747SMing Lei 		msleep(5);
222385067747SMing Lei 	}
222485067747SMing Lei 
222585067747SMing Lei 	return r;
222685067747SMing Lei }
222785067747SMing Lei 
22281da177e4SLinus Torvalds /*
22291da177e4SLinus Torvalds  * Process the deferred bios
22301da177e4SLinus Torvalds  */
2231ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
22321da177e4SLinus Torvalds {
22330c2915b8SMike Snitzer 	struct mapped_device *md = container_of(work, struct mapped_device, work);
22340c2915b8SMike Snitzer 	struct bio *bio;
2235ef208587SMikulas Patocka 
22363b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2237022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
22380c2915b8SMike Snitzer 		bio = bio_list_pop(&md->deferred);
2239022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2240022c2611SMikulas Patocka 
22410c2915b8SMike Snitzer 		if (!bio)
2242df12ee99SAlasdair G Kergon 			break;
224373d410c0SMilan Broz 
22440c2915b8SMike Snitzer 		submit_bio_noacct(bio);
2245e6ee8c0bSKiyoshi Ueda 	}
22461da177e4SLinus Torvalds }
22471da177e4SLinus Torvalds 
22489a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2249304f3f6aSMilan Broz {
22503b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
22514e857c58SPeter Zijlstra 	smp_mb__after_atomic();
225253d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2253304f3f6aSMilan Broz }
2254304f3f6aSMilan Broz 
22551da177e4SLinus Torvalds /*
2256042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
22571da177e4SLinus Torvalds  */
2258042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
22591da177e4SLinus Torvalds {
226087eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2261754c5fc7SMike Snitzer 	struct queue_limits limits;
2262042d2a9bSAlasdair G Kergon 	int r;
22631da177e4SLinus Torvalds 
2264e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
22651da177e4SLinus Torvalds 
22661da177e4SLinus Torvalds 	/* device must be suspended */
22674f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
226893c534aeSAlasdair G Kergon 		goto out;
22691da177e4SLinus Torvalds 
22703ae70656SMike Snitzer 	/*
22713ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
22723ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
22733ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
22743ae70656SMike Snitzer 	 * reappear.
22753ae70656SMike Snitzer 	 */
22763ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
227783d5e5b0SMikulas Patocka 		live_map = dm_get_live_table_fast(md);
22783ae70656SMike Snitzer 		if (live_map)
22793ae70656SMike Snitzer 			limits = md->queue->limits;
228083d5e5b0SMikulas Patocka 		dm_put_live_table_fast(md);
22813ae70656SMike Snitzer 	}
22823ae70656SMike Snitzer 
228387eb5b21SMike Christie 	if (!live_map) {
2284754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2285042d2a9bSAlasdair G Kergon 		if (r) {
2286042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2287754c5fc7SMike Snitzer 			goto out;
2288042d2a9bSAlasdair G Kergon 		}
228987eb5b21SMike Christie 	}
2290754c5fc7SMike Snitzer 
2291042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
229262e08243SMikulas Patocka 	dm_issue_global_event();
22931da177e4SLinus Torvalds 
229493c534aeSAlasdair G Kergon out:
2295e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2296042d2a9bSAlasdair G Kergon 	return map;
22971da177e4SLinus Torvalds }
22981da177e4SLinus Torvalds 
22991da177e4SLinus Torvalds /*
23001da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
23011da177e4SLinus Torvalds  * device.
23021da177e4SLinus Torvalds  */
23032ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
23041da177e4SLinus Torvalds {
2305e39e2e95SAlasdair G Kergon 	int r;
23061da177e4SLinus Torvalds 
2307040f04bdSChristoph Hellwig 	WARN_ON(test_bit(DMF_FROZEN, &md->flags));
2308dfbe03f6SAlasdair G Kergon 
2309977115c0SChristoph Hellwig 	r = freeze_bdev(md->disk->part0);
2310040f04bdSChristoph Hellwig 	if (!r)
2311aa8d7c2fSAlasdair G Kergon 		set_bit(DMF_FROZEN, &md->flags);
2312040f04bdSChristoph Hellwig 	return r;
23131da177e4SLinus Torvalds }
23141da177e4SLinus Torvalds 
23152ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
23161da177e4SLinus Torvalds {
2317aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2318aa8d7c2fSAlasdair G Kergon 		return;
2319977115c0SChristoph Hellwig 	thaw_bdev(md->disk->part0);
2320aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
23211da177e4SLinus Torvalds }
23221da177e4SLinus Torvalds 
23231da177e4SLinus Torvalds /*
2324b48633f8SBart Van Assche  * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
2325b48633f8SBart Van Assche  * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
2326b48633f8SBart Van Assche  * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
2327b48633f8SBart Van Assche  *
2328ffcc3936SMike Snitzer  * If __dm_suspend returns 0, the device is completely quiescent
2329ffcc3936SMike Snitzer  * now. There is no request-processing activity. All new requests
2330ffcc3936SMike Snitzer  * are being added to md->deferred list.
2331cec47e3dSKiyoshi Ueda  */
2332ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
23332f064a59SPeter Zijlstra 			unsigned suspend_flags, unsigned int task_state,
2334eaf9a736SMike Snitzer 			int dmf_suspended_flag)
23351da177e4SLinus Torvalds {
2336ffcc3936SMike Snitzer 	bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
2337ffcc3936SMike Snitzer 	bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
2338ffcc3936SMike Snitzer 	int r;
2339cf222b37SAlasdair G Kergon 
23405a8f1f80SBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
23415a8f1f80SBart Van Assche 
23422e93ccc1SKiyoshi Ueda 	/*
23432e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
23442e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
23452e93ccc1SKiyoshi Ueda 	 */
23462e93ccc1SKiyoshi Ueda 	if (noflush)
23472e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
234886331f39SBart Van Assche 	else
2349ac75b09fSMike Snitzer 		DMDEBUG("%s: suspending with flush", dm_device_name(md));
23502e93ccc1SKiyoshi Ueda 
2351d67ee213SMike Snitzer 	/*
2352d67ee213SMike Snitzer 	 * This gets reverted if there's an error later and the targets
2353d67ee213SMike Snitzer 	 * provide the .presuspend_undo hook.
2354d67ee213SMike Snitzer 	 */
23551da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
23561da177e4SLinus Torvalds 
23572e93ccc1SKiyoshi Ueda 	/*
23589f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
23599f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
23609f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
23619f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
23622e93ccc1SKiyoshi Ueda 	 */
236332a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
23642ca3310eSAlasdair G Kergon 		r = lock_fs(md);
2365d67ee213SMike Snitzer 		if (r) {
2366d67ee213SMike Snitzer 			dm_table_presuspend_undo_targets(map);
2367ffcc3936SMike Snitzer 			return r;
2368aa8d7c2fSAlasdair G Kergon 		}
2369d67ee213SMike Snitzer 	}
23701da177e4SLinus Torvalds 
23711da177e4SLinus Torvalds 	/*
23723b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
23733b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
237496c9865cSMike Snitzer 	 * dm_split_and_process_bio from dm_submit_bio.
23753b00b203SMikulas Patocka 	 *
237696c9865cSMike Snitzer 	 * To get all processes out of dm_split_and_process_bio in dm_submit_bio,
23773b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
237896c9865cSMike Snitzer 	 * dm_split_and_process_bio from dm_submit_bio and quiesce the thread
23790cede372SMike Snitzer 	 * (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND and call
23806a8736d1STejun Heo 	 * flush_workqueue(md->wq).
23811da177e4SLinus Torvalds 	 */
23821eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
238341abc4e1SHannes Reinecke 	if (map)
238483d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
23851da177e4SLinus Torvalds 
2386d0bcb878SKiyoshi Ueda 	/*
238729e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
238829e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2389d0bcb878SKiyoshi Ueda 	 */
23906a23e05cSJens Axboe 	if (dm_request_based(md))
2391eca7ee6dSMike Snitzer 		dm_stop_queue(md->queue);
2392cec47e3dSKiyoshi Ueda 
2393d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2394d0bcb878SKiyoshi Ueda 
23951da177e4SLinus Torvalds 	/*
23963b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
23973b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
23983b00b203SMikulas Patocka 	 * to finish.
23991da177e4SLinus Torvalds 	 */
2400b48633f8SBart Van Assche 	r = dm_wait_for_completion(md, task_state);
2401eaf9a736SMike Snitzer 	if (!r)
2402eaf9a736SMike Snitzer 		set_bit(dmf_suspended_flag, &md->flags);
24031da177e4SLinus Torvalds 
24046d6f10dfSMilan Broz 	if (noflush)
2405022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
240641abc4e1SHannes Reinecke 	if (map)
240783d5e5b0SMikulas Patocka 		synchronize_srcu(&md->io_barrier);
24082e93ccc1SKiyoshi Ueda 
24091da177e4SLinus Torvalds 	/* were we interrupted ? */
241046125c1cSMilan Broz 	if (r < 0) {
24119a1fb464SMikulas Patocka 		dm_queue_flush(md);
241273d410c0SMilan Broz 
2413cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
2414eca7ee6dSMike Snitzer 			dm_start_queue(md->queue);
2415cec47e3dSKiyoshi Ueda 
24162ca3310eSAlasdair G Kergon 		unlock_fs(md);
2417d67ee213SMike Snitzer 		dm_table_presuspend_undo_targets(map);
2418ffcc3936SMike Snitzer 		/* pushback list is already flushed, so skip flush */
2419ffcc3936SMike Snitzer 	}
2420ffcc3936SMike Snitzer 
2421ffcc3936SMike Snitzer 	return r;
24222ca3310eSAlasdair G Kergon }
24232ca3310eSAlasdair G Kergon 
24243b00b203SMikulas Patocka /*
2425ffcc3936SMike Snitzer  * We need to be able to change a mapping table under a mounted
2426ffcc3936SMike Snitzer  * filesystem.  For example we might want to move some data in
2427ffcc3936SMike Snitzer  * the background.  Before the table can be swapped with
2428ffcc3936SMike Snitzer  * dm_bind_table, dm_suspend must be called to flush any in
2429ffcc3936SMike Snitzer  * flight bios and ensure that any further io gets deferred.
24303b00b203SMikulas Patocka  */
2431ffcc3936SMike Snitzer /*
2432ffcc3936SMike Snitzer  * Suspend mechanism in request-based dm.
2433ffcc3936SMike Snitzer  *
2434ffcc3936SMike Snitzer  * 1. Flush all I/Os by lock_fs() if needed.
2435ffcc3936SMike Snitzer  * 2. Stop dispatching any I/O by stopping the request_queue.
2436ffcc3936SMike Snitzer  * 3. Wait for all in-flight I/Os to be completed or requeued.
2437ffcc3936SMike Snitzer  *
2438ffcc3936SMike Snitzer  * To abort suspend, start the request_queue.
2439ffcc3936SMike Snitzer  */
2440ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
2441ffcc3936SMike Snitzer {
2442ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2443ffcc3936SMike Snitzer 	int r = 0;
2444ffcc3936SMike Snitzer 
2445ffcc3936SMike Snitzer retry:
2446ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2447ffcc3936SMike Snitzer 
2448ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2449ffcc3936SMike Snitzer 		r = -EINVAL;
2450ffcc3936SMike Snitzer 		goto out_unlock;
2451ffcc3936SMike Snitzer 	}
2452ffcc3936SMike Snitzer 
2453ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2454ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2455ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2456ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2457ffcc3936SMike Snitzer 		if (r)
2458ffcc3936SMike Snitzer 			return r;
2459ffcc3936SMike Snitzer 		goto retry;
2460ffcc3936SMike Snitzer 	}
2461ffcc3936SMike Snitzer 
2462a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2463ffcc3936SMike Snitzer 
2464eaf9a736SMike Snitzer 	r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
2465ffcc3936SMike Snitzer 	if (r)
2466ffcc3936SMike Snitzer 		goto out_unlock;
24673b00b203SMikulas Patocka 
24685df96f2bSMikulas Patocka 	set_bit(DMF_POST_SUSPENDING, &md->flags);
24694d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
24705df96f2bSMikulas Patocka 	clear_bit(DMF_POST_SUSPENDING, &md->flags);
24714d4471cbSKiyoshi Ueda 
2472d287483dSAlasdair G Kergon out_unlock:
2473e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2474cf222b37SAlasdair G Kergon 	return r;
24751da177e4SLinus Torvalds }
24761da177e4SLinus Torvalds 
2477ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map)
24781da177e4SLinus Torvalds {
2479ffcc3936SMike Snitzer 	if (map) {
2480ffcc3936SMike Snitzer 		int r = dm_table_resume_targets(map);
24818757b776SMilan Broz 		if (r)
2482ffcc3936SMike Snitzer 			return r;
2483ffcc3936SMike Snitzer 	}
24842ca3310eSAlasdair G Kergon 
24859a1fb464SMikulas Patocka 	dm_queue_flush(md);
24862ca3310eSAlasdair G Kergon 
2487cec47e3dSKiyoshi Ueda 	/*
2488cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2489cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2490cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2491cec47e3dSKiyoshi Ueda 	 */
2492cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2493eca7ee6dSMike Snitzer 		dm_start_queue(md->queue);
2494cec47e3dSKiyoshi Ueda 
24952ca3310eSAlasdair G Kergon 	unlock_fs(md);
24962ca3310eSAlasdair G Kergon 
2497ffcc3936SMike Snitzer 	return 0;
2498ffcc3936SMike Snitzer }
2499ffcc3936SMike Snitzer 
2500ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md)
2501ffcc3936SMike Snitzer {
25028dc23658SMinfei Huang 	int r;
2503ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2504ffcc3936SMike Snitzer 
2505ffcc3936SMike Snitzer retry:
25068dc23658SMinfei Huang 	r = -EINVAL;
2507ffcc3936SMike Snitzer 	mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
2508ffcc3936SMike Snitzer 
2509ffcc3936SMike Snitzer 	if (!dm_suspended_md(md))
2510ffcc3936SMike Snitzer 		goto out;
2511ffcc3936SMike Snitzer 
2512ffcc3936SMike Snitzer 	if (dm_suspended_internally_md(md)) {
2513ffcc3936SMike Snitzer 		/* already internally suspended, wait for internal resume */
2514ffcc3936SMike Snitzer 		mutex_unlock(&md->suspend_lock);
2515ffcc3936SMike Snitzer 		r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
2516ffcc3936SMike Snitzer 		if (r)
2517ffcc3936SMike Snitzer 			return r;
2518ffcc3936SMike Snitzer 		goto retry;
2519ffcc3936SMike Snitzer 	}
2520ffcc3936SMike Snitzer 
2521a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2522ffcc3936SMike Snitzer 	if (!map || !dm_table_get_size(map))
2523ffcc3936SMike Snitzer 		goto out;
2524ffcc3936SMike Snitzer 
2525ffcc3936SMike Snitzer 	r = __dm_resume(md, map);
2526ffcc3936SMike Snitzer 	if (r)
2527ffcc3936SMike Snitzer 		goto out;
2528ffcc3936SMike Snitzer 
25292ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
2530cf222b37SAlasdair G Kergon out:
2531e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
25322ca3310eSAlasdair G Kergon 
2533cf222b37SAlasdair G Kergon 	return r;
25341da177e4SLinus Torvalds }
25351da177e4SLinus Torvalds 
2536fd2ed4d2SMikulas Patocka /*
2537fd2ed4d2SMikulas Patocka  * Internal suspend/resume works like userspace-driven suspend. It waits
2538fd2ed4d2SMikulas Patocka  * until all bios finish and prevents issuing new bios to the target drivers.
2539fd2ed4d2SMikulas Patocka  * It may be used only from the kernel.
2540fd2ed4d2SMikulas Patocka  */
2541fd2ed4d2SMikulas Patocka 
2542ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
2543ffcc3936SMike Snitzer {
2544ffcc3936SMike Snitzer 	struct dm_table *map = NULL;
2545ffcc3936SMike Snitzer 
25461ea0654eSBart Van Assche 	lockdep_assert_held(&md->suspend_lock);
25471ea0654eSBart Van Assche 
254896b26c8cSMikulas Patocka 	if (md->internal_suspend_count++)
2549ffcc3936SMike Snitzer 		return; /* nested internal suspend */
2550ffcc3936SMike Snitzer 
2551ffcc3936SMike Snitzer 	if (dm_suspended_md(md)) {
2552ffcc3936SMike Snitzer 		set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2553ffcc3936SMike Snitzer 		return; /* nest suspend */
2554ffcc3936SMike Snitzer 	}
2555ffcc3936SMike Snitzer 
2556a12f5d48SEric Dumazet 	map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
2557ffcc3936SMike Snitzer 
2558ffcc3936SMike Snitzer 	/*
2559ffcc3936SMike Snitzer 	 * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
2560ffcc3936SMike Snitzer 	 * supported.  Properly supporting a TASK_INTERRUPTIBLE internal suspend
2561ffcc3936SMike Snitzer 	 * would require changing .presuspend to return an error -- avoid this
2562ffcc3936SMike Snitzer 	 * until there is a need for more elaborate variants of internal suspend.
2563ffcc3936SMike Snitzer 	 */
2564eaf9a736SMike Snitzer 	(void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
2565eaf9a736SMike Snitzer 			    DMF_SUSPENDED_INTERNALLY);
2566ffcc3936SMike Snitzer 
25675df96f2bSMikulas Patocka 	set_bit(DMF_POST_SUSPENDING, &md->flags);
2568ffcc3936SMike Snitzer 	dm_table_postsuspend_targets(map);
25695df96f2bSMikulas Patocka 	clear_bit(DMF_POST_SUSPENDING, &md->flags);
2570ffcc3936SMike Snitzer }
2571ffcc3936SMike Snitzer 
2572ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md)
2573ffcc3936SMike Snitzer {
257496b26c8cSMikulas Patocka 	BUG_ON(!md->internal_suspend_count);
257596b26c8cSMikulas Patocka 
257696b26c8cSMikulas Patocka 	if (--md->internal_suspend_count)
2577ffcc3936SMike Snitzer 		return; /* resume from nested internal suspend */
2578ffcc3936SMike Snitzer 
2579ffcc3936SMike Snitzer 	if (dm_suspended_md(md))
2580ffcc3936SMike Snitzer 		goto done; /* resume from nested suspend */
2581ffcc3936SMike Snitzer 
2582ffcc3936SMike Snitzer 	/*
2583ffcc3936SMike Snitzer 	 * NOTE: existing callers don't need to call dm_table_resume_targets
2584ffcc3936SMike Snitzer 	 * (which may fail -- so best to avoid it for now by passing NULL map)
2585ffcc3936SMike Snitzer 	 */
2586ffcc3936SMike Snitzer 	(void) __dm_resume(md, NULL);
2587ffcc3936SMike Snitzer 
2588ffcc3936SMike Snitzer done:
2589ffcc3936SMike Snitzer 	clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2590ffcc3936SMike Snitzer 	smp_mb__after_atomic();
2591ffcc3936SMike Snitzer 	wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
2592ffcc3936SMike Snitzer }
2593ffcc3936SMike Snitzer 
2594ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md)
2595fd2ed4d2SMikulas Patocka {
2596fd2ed4d2SMikulas Patocka 	mutex_lock(&md->suspend_lock);
2597ffcc3936SMike Snitzer 	__dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
2598ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2599ffcc3936SMike Snitzer }
2600ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
2601ffcc3936SMike Snitzer 
2602ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md)
2603ffcc3936SMike Snitzer {
2604ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2605ffcc3936SMike Snitzer 	__dm_internal_resume(md);
2606ffcc3936SMike Snitzer 	mutex_unlock(&md->suspend_lock);
2607ffcc3936SMike Snitzer }
2608ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume);
2609ffcc3936SMike Snitzer 
2610ffcc3936SMike Snitzer /*
2611ffcc3936SMike Snitzer  * Fast variants of internal suspend/resume hold md->suspend_lock,
2612ffcc3936SMike Snitzer  * which prevents interaction with userspace-driven suspend.
2613ffcc3936SMike Snitzer  */
2614ffcc3936SMike Snitzer 
2615ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md)
2616ffcc3936SMike Snitzer {
2617ffcc3936SMike Snitzer 	mutex_lock(&md->suspend_lock);
2618ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2619fd2ed4d2SMikulas Patocka 		return;
2620fd2ed4d2SMikulas Patocka 
2621fd2ed4d2SMikulas Patocka 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
2622fd2ed4d2SMikulas Patocka 	synchronize_srcu(&md->io_barrier);
2623fd2ed4d2SMikulas Patocka 	flush_workqueue(md->wq);
2624fd2ed4d2SMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2625fd2ed4d2SMikulas Patocka }
2626b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
2627fd2ed4d2SMikulas Patocka 
2628ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md)
2629fd2ed4d2SMikulas Patocka {
2630ffcc3936SMike Snitzer 	if (dm_suspended_md(md) || dm_suspended_internally_md(md))
2631fd2ed4d2SMikulas Patocka 		goto done;
2632fd2ed4d2SMikulas Patocka 
2633fd2ed4d2SMikulas Patocka 	dm_queue_flush(md);
2634fd2ed4d2SMikulas Patocka 
2635fd2ed4d2SMikulas Patocka done:
2636fd2ed4d2SMikulas Patocka 	mutex_unlock(&md->suspend_lock);
2637fd2ed4d2SMikulas Patocka }
2638b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
2639fd2ed4d2SMikulas Patocka 
26401da177e4SLinus Torvalds /*-----------------------------------------------------------------
26411da177e4SLinus Torvalds  * Event notification.
26421da177e4SLinus Torvalds  *---------------------------------------------------------------*/
26433abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
264460935eb2SMilan Broz 		       unsigned cookie)
264569267a30SAlasdair G Kergon {
26466958c1c6SMikulas Patocka 	int r;
26476958c1c6SMikulas Patocka 	unsigned noio_flag;
264860935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
264960935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
265060935eb2SMilan Broz 
26516958c1c6SMikulas Patocka 	noio_flag = memalloc_noio_save();
26526958c1c6SMikulas Patocka 
265360935eb2SMilan Broz 	if (!cookie)
26546958c1c6SMikulas Patocka 		r = kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
265560935eb2SMilan Broz 	else {
265660935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
265760935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
26586958c1c6SMikulas Patocka 		r = kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
26593abf85b5SPeter Rajnoha 				       action, envp);
266060935eb2SMilan Broz 	}
26616958c1c6SMikulas Patocka 
26626958c1c6SMikulas Patocka 	memalloc_noio_restore(noio_flag);
26636958c1c6SMikulas Patocka 
26646958c1c6SMikulas Patocka 	return r;
266569267a30SAlasdair G Kergon }
266669267a30SAlasdair G Kergon 
26677a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
26687a8c3d3bSMike Anderson {
26697a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
26707a8c3d3bSMike Anderson }
26717a8c3d3bSMike Anderson 
26721da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
26731da177e4SLinus Torvalds {
26741da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
26751da177e4SLinus Torvalds }
26761da177e4SLinus Torvalds 
26771da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
26781da177e4SLinus Torvalds {
26791da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
26801da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
26811da177e4SLinus Torvalds }
26821da177e4SLinus Torvalds 
26837a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
26847a8c3d3bSMike Anderson {
26857a8c3d3bSMike Anderson 	unsigned long flags;
26867a8c3d3bSMike Anderson 
26877a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
26887a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
26897a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
26907a8c3d3bSMike Anderson }
26917a8c3d3bSMike Anderson 
26921da177e4SLinus Torvalds /*
26931da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
26941da177e4SLinus Torvalds  * count on 'md'.
26951da177e4SLinus Torvalds  */
26961da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
26971da177e4SLinus Torvalds {
26981da177e4SLinus Torvalds 	return md->disk;
26991da177e4SLinus Torvalds }
270065ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk);
27011da177e4SLinus Torvalds 
2702784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2703784aae73SMilan Broz {
27042995fa78SMikulas Patocka 	return &md->kobj_holder.kobj;
2705784aae73SMilan Broz }
2706784aae73SMilan Broz 
2707784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2708784aae73SMilan Broz {
2709784aae73SMilan Broz 	struct mapped_device *md;
2710784aae73SMilan Broz 
27112995fa78SMikulas Patocka 	md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
2712784aae73SMilan Broz 
2713b9a41d21SHou Tao 	spin_lock(&_minor_lock);
2714b9a41d21SHou Tao 	if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
2715b9a41d21SHou Tao 		md = NULL;
2716b9a41d21SHou Tao 		goto out;
2717b9a41d21SHou Tao 	}
2718784aae73SMilan Broz 	dm_get(md);
2719b9a41d21SHou Tao out:
2720b9a41d21SHou Tao 	spin_unlock(&_minor_lock);
2721b9a41d21SHou Tao 
2722784aae73SMilan Broz 	return md;
2723784aae73SMilan Broz }
2724784aae73SMilan Broz 
27254f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
27261da177e4SLinus Torvalds {
27271da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
27281da177e4SLinus Torvalds }
27291da177e4SLinus Torvalds 
27305df96f2bSMikulas Patocka static int dm_post_suspending_md(struct mapped_device *md)
27315df96f2bSMikulas Patocka {
27325df96f2bSMikulas Patocka 	return test_bit(DMF_POST_SUSPENDING, &md->flags);
27335df96f2bSMikulas Patocka }
27345df96f2bSMikulas Patocka 
2735ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md)
2736ffcc3936SMike Snitzer {
2737ffcc3936SMike Snitzer 	return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
2738ffcc3936SMike Snitzer }
2739ffcc3936SMike Snitzer 
27402c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md)
27412c140a24SMikulas Patocka {
27422c140a24SMikulas Patocka 	return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
27432c140a24SMikulas Patocka }
27442c140a24SMikulas Patocka 
274564dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
274664dbce58SKiyoshi Ueda {
274733bd6f06SMike Snitzer 	return dm_suspended_md(ti->table->md);
274864dbce58SKiyoshi Ueda }
274964dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
275064dbce58SKiyoshi Ueda 
27515df96f2bSMikulas Patocka int dm_post_suspending(struct dm_target *ti)
27525df96f2bSMikulas Patocka {
275333bd6f06SMike Snitzer 	return dm_post_suspending_md(ti->table->md);
27545df96f2bSMikulas Patocka }
27555df96f2bSMikulas Patocka EXPORT_SYMBOL_GPL(dm_post_suspending);
27565df96f2bSMikulas Patocka 
27572e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
27582e93ccc1SKiyoshi Ueda {
275933bd6f06SMike Snitzer 	return __noflush_suspending(ti->table->md);
27602e93ccc1SKiyoshi Ueda }
27612e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
27622e93ccc1SKiyoshi Ueda 
27637e0d574fSBart Van Assche struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type,
27640776aa0eSMike Snitzer 					    unsigned integrity, unsigned per_io_data_size,
27650776aa0eSMike Snitzer 					    unsigned min_pool_size)
2766e6ee8c0bSKiyoshi Ueda {
2767115485e8SMike Snitzer 	struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
276878d8e58aSMike Snitzer 	unsigned int pool_size = 0;
276964f52b0eSMike Snitzer 	unsigned int front_pad, io_front_pad;
27706f1c819cSKent Overstreet 	int ret;
2771e6ee8c0bSKiyoshi Ueda 
2772e6ee8c0bSKiyoshi Ueda 	if (!pools)
27734e6e36c3SMike Snitzer 		return NULL;
2774e6ee8c0bSKiyoshi Ueda 
277578d8e58aSMike Snitzer 	switch (type) {
277678d8e58aSMike Snitzer 	case DM_TYPE_BIO_BASED:
2777545ed20eSToshi Kani 	case DM_TYPE_DAX_BIO_BASED:
27780776aa0eSMike Snitzer 		pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size);
277962f26317SJeffle Xu 		front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + DM_TARGET_IO_BIO_OFFSET;
278062f26317SJeffle Xu 		io_front_pad = roundup(per_io_data_size,  __alignof__(struct dm_io)) + DM_IO_BIO_OFFSET;
27816f1c819cSKent Overstreet 		ret = bioset_init(&pools->io_bs, pool_size, io_front_pad, 0);
27826f1c819cSKent Overstreet 		if (ret)
278364f52b0eSMike Snitzer 			goto out;
27846f1c819cSKent Overstreet 		if (integrity && bioset_integrity_create(&pools->io_bs, pool_size))
2785eb8db831SChristoph Hellwig 			goto out;
278678d8e58aSMike Snitzer 		break;
278778d8e58aSMike Snitzer 	case DM_TYPE_REQUEST_BASED:
27880776aa0eSMike Snitzer 		pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size);
278978d8e58aSMike Snitzer 		front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
2790591ddcfcSMike Snitzer 		/* per_io_data_size is used for blk-mq pdu at queue allocation */
279178d8e58aSMike Snitzer 		break;
279278d8e58aSMike Snitzer 	default:
279378d8e58aSMike Snitzer 		BUG();
279478d8e58aSMike Snitzer 	}
279578d8e58aSMike Snitzer 
27966f1c819cSKent Overstreet 	ret = bioset_init(&pools->bs, pool_size, front_pad, 0);
27976f1c819cSKent Overstreet 	if (ret)
27985f015204SJun'ichi Nomura 		goto out;
2799e6ee8c0bSKiyoshi Ueda 
28006f1c819cSKent Overstreet 	if (integrity && bioset_integrity_create(&pools->bs, pool_size))
28015f015204SJun'ichi Nomura 		goto out;
2802a91a2785SMartin K. Petersen 
2803e6ee8c0bSKiyoshi Ueda 	return pools;
280478d8e58aSMike Snitzer 
28055f015204SJun'ichi Nomura out:
28065f015204SJun'ichi Nomura 	dm_free_md_mempools(pools);
2807e6ee8c0bSKiyoshi Ueda 
28084e6e36c3SMike Snitzer 	return NULL;
2809e6ee8c0bSKiyoshi Ueda }
2810e6ee8c0bSKiyoshi Ueda 
2811e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
2812e6ee8c0bSKiyoshi Ueda {
2813e6ee8c0bSKiyoshi Ueda 	if (!pools)
2814e6ee8c0bSKiyoshi Ueda 		return;
2815e6ee8c0bSKiyoshi Ueda 
28166f1c819cSKent Overstreet 	bioset_exit(&pools->bs);
28176f1c819cSKent Overstreet 	bioset_exit(&pools->io_bs);
2818e6ee8c0bSKiyoshi Ueda 
2819e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2820e6ee8c0bSKiyoshi Ueda }
2821e6ee8c0bSKiyoshi Ueda 
28229c72bad1SChristoph Hellwig struct dm_pr {
28239c72bad1SChristoph Hellwig 	u64	old_key;
28249c72bad1SChristoph Hellwig 	u64	new_key;
28259c72bad1SChristoph Hellwig 	u32	flags;
28269c72bad1SChristoph Hellwig 	bool	fail_early;
28279c72bad1SChristoph Hellwig };
28289c72bad1SChristoph Hellwig 
28299c72bad1SChristoph Hellwig static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
28309c72bad1SChristoph Hellwig 		      void *data)
28319c72bad1SChristoph Hellwig {
28329c72bad1SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
28339c72bad1SChristoph Hellwig 	struct dm_table *table;
28349c72bad1SChristoph Hellwig 	struct dm_target *ti;
28359c72bad1SChristoph Hellwig 	int ret = -ENOTTY, srcu_idx;
28369c72bad1SChristoph Hellwig 
28379c72bad1SChristoph Hellwig 	table = dm_get_live_table(md, &srcu_idx);
28389c72bad1SChristoph Hellwig 	if (!table || !dm_table_get_size(table))
28399c72bad1SChristoph Hellwig 		goto out;
28409c72bad1SChristoph Hellwig 
28419c72bad1SChristoph Hellwig 	/* We only support devices that have a single target */
28429c72bad1SChristoph Hellwig 	if (dm_table_get_num_targets(table) != 1)
28439c72bad1SChristoph Hellwig 		goto out;
28449c72bad1SChristoph Hellwig 	ti = dm_table_get_target(table, 0);
28459c72bad1SChristoph Hellwig 
28469c72bad1SChristoph Hellwig 	ret = -EINVAL;
28479c72bad1SChristoph Hellwig 	if (!ti->type->iterate_devices)
28489c72bad1SChristoph Hellwig 		goto out;
28499c72bad1SChristoph Hellwig 
28509c72bad1SChristoph Hellwig 	ret = ti->type->iterate_devices(ti, fn, data);
28519c72bad1SChristoph Hellwig out:
28529c72bad1SChristoph Hellwig 	dm_put_live_table(md, srcu_idx);
28539c72bad1SChristoph Hellwig 	return ret;
28549c72bad1SChristoph Hellwig }
28559c72bad1SChristoph Hellwig 
28569c72bad1SChristoph Hellwig /*
28579c72bad1SChristoph Hellwig  * For register / unregister we need to manually call out to every path.
28589c72bad1SChristoph Hellwig  */
28599c72bad1SChristoph Hellwig static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
28609c72bad1SChristoph Hellwig 			    sector_t start, sector_t len, void *data)
28619c72bad1SChristoph Hellwig {
28629c72bad1SChristoph Hellwig 	struct dm_pr *pr = data;
28639c72bad1SChristoph Hellwig 	const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
28649c72bad1SChristoph Hellwig 
28659c72bad1SChristoph Hellwig 	if (!ops || !ops->pr_register)
28669c72bad1SChristoph Hellwig 		return -EOPNOTSUPP;
28679c72bad1SChristoph Hellwig 	return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
28689c72bad1SChristoph Hellwig }
28699c72bad1SChristoph Hellwig 
287071cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
287171cdb697SChristoph Hellwig 			  u32 flags)
287271cdb697SChristoph Hellwig {
28739c72bad1SChristoph Hellwig 	struct dm_pr pr = {
28749c72bad1SChristoph Hellwig 		.old_key	= old_key,
28759c72bad1SChristoph Hellwig 		.new_key	= new_key,
28769c72bad1SChristoph Hellwig 		.flags		= flags,
28779c72bad1SChristoph Hellwig 		.fail_early	= true,
28789c72bad1SChristoph Hellwig 	};
28799c72bad1SChristoph Hellwig 	int ret;
288071cdb697SChristoph Hellwig 
28819c72bad1SChristoph Hellwig 	ret = dm_call_pr(bdev, __dm_pr_register, &pr);
28829c72bad1SChristoph Hellwig 	if (ret && new_key) {
28839c72bad1SChristoph Hellwig 		/* unregister all paths if we failed to register any path */
28849c72bad1SChristoph Hellwig 		pr.old_key = new_key;
28859c72bad1SChristoph Hellwig 		pr.new_key = 0;
28869c72bad1SChristoph Hellwig 		pr.flags = 0;
28879c72bad1SChristoph Hellwig 		pr.fail_early = false;
28889c72bad1SChristoph Hellwig 		dm_call_pr(bdev, __dm_pr_register, &pr);
28899c72bad1SChristoph Hellwig 	}
289071cdb697SChristoph Hellwig 
28919c72bad1SChristoph Hellwig 	return ret;
289271cdb697SChristoph Hellwig }
289371cdb697SChristoph Hellwig 
289471cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
289571cdb697SChristoph Hellwig 			 u32 flags)
289671cdb697SChristoph Hellwig {
289771cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
289871cdb697SChristoph Hellwig 	const struct pr_ops *ops;
2899971888c4SMike Snitzer 	int r, srcu_idx;
290071cdb697SChristoph Hellwig 
29015bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
290271cdb697SChristoph Hellwig 	if (r < 0)
2903971888c4SMike Snitzer 		goto out;
290471cdb697SChristoph Hellwig 
290571cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
290671cdb697SChristoph Hellwig 	if (ops && ops->pr_reserve)
290771cdb697SChristoph Hellwig 		r = ops->pr_reserve(bdev, key, type, flags);
290871cdb697SChristoph Hellwig 	else
290971cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
2910971888c4SMike Snitzer out:
2911971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
291271cdb697SChristoph Hellwig 	return r;
291371cdb697SChristoph Hellwig }
291471cdb697SChristoph Hellwig 
291571cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
291671cdb697SChristoph Hellwig {
291771cdb697SChristoph Hellwig 	struct mapped_device *md = bdev->bd_disk->private_data;
291871cdb697SChristoph Hellwig 	const struct pr_ops *ops;
2919971888c4SMike Snitzer 	int r, srcu_idx;
292071cdb697SChristoph Hellwig 
29215bd5e8d8SMike Snitzer 	r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
292271cdb697SChristoph Hellwig 	if (r < 0)
2923971888c4SMike Snitzer 		goto out;
292471cdb697SChristoph Hellwig 
292571cdb697SChristoph Hellwig 	ops = bdev->bd_disk->fops->pr_ops;
292671cdb697SChristoph Hellwig 	if (ops && ops->pr_release)
292771cdb697SChristoph Hellwig 		r = ops->pr_release(bdev, key, type);
292871cdb697SChristoph Hellwig 	else
292971cdb697SChristoph Hellwig 		r = -EOPNOTSUPP;
2930971888c4SMike Snitzer out:
2931971888c4SMike Snitzer 	dm_unprepare_ioctl(md, srcu_idx);
293271cdb697SChristoph Hellwig 	return r;
293371cdb697SChristoph Hellwig }
293471cdb697SChristoph Hellwig 
293571cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
293671cdb697SChristoph Hellwig 			 enum pr_type type, bool abort)
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_preempt)
294871cdb697SChristoph Hellwig 		r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
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_clear(struct block_device *bdev, u64 key)
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_clear)
296871cdb697SChristoph Hellwig 		r = ops->pr_clear(bdev, key);
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 const struct pr_ops dm_pr_ops = {
297771cdb697SChristoph Hellwig 	.pr_register	= dm_pr_register,
297871cdb697SChristoph Hellwig 	.pr_reserve	= dm_pr_reserve,
297971cdb697SChristoph Hellwig 	.pr_release	= dm_pr_release,
298071cdb697SChristoph Hellwig 	.pr_preempt	= dm_pr_preempt,
298171cdb697SChristoph Hellwig 	.pr_clear	= dm_pr_clear,
298271cdb697SChristoph Hellwig };
298371cdb697SChristoph Hellwig 
298483d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
2985c62b37d9SChristoph Hellwig 	.submit_bio = dm_submit_bio,
29861da177e4SLinus Torvalds 	.open = dm_blk_open,
29871da177e4SLinus Torvalds 	.release = dm_blk_close,
2988aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
29893ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
2990e76239a3SChristoph Hellwig 	.report_zones = dm_blk_report_zones,
299171cdb697SChristoph Hellwig 	.pr_ops = &dm_pr_ops,
29921da177e4SLinus Torvalds 	.owner = THIS_MODULE
29931da177e4SLinus Torvalds };
29941da177e4SLinus Torvalds 
2995681cc5e8SMike Snitzer static const struct block_device_operations dm_rq_blk_dops = {
2996681cc5e8SMike Snitzer 	.open = dm_blk_open,
2997681cc5e8SMike Snitzer 	.release = dm_blk_close,
2998681cc5e8SMike Snitzer 	.ioctl = dm_blk_ioctl,
2999681cc5e8SMike Snitzer 	.getgeo = dm_blk_getgeo,
3000681cc5e8SMike Snitzer 	.pr_ops = &dm_pr_ops,
3001681cc5e8SMike Snitzer 	.owner = THIS_MODULE
3002681cc5e8SMike Snitzer };
3003681cc5e8SMike Snitzer 
3004f26c5719SDan Williams static const struct dax_operations dm_dax_ops = {
3005f26c5719SDan Williams 	.direct_access = dm_dax_direct_access,
3006cdf6cdcdSVivek Goyal 	.zero_page_range = dm_dax_zero_page_range,
3007f26c5719SDan Williams };
3008f26c5719SDan Williams 
30091da177e4SLinus Torvalds /*
30101da177e4SLinus Torvalds  * module hooks
30111da177e4SLinus Torvalds  */
30121da177e4SLinus Torvalds module_init(dm_init);
30131da177e4SLinus Torvalds module_exit(dm_exit);
30141da177e4SLinus Torvalds 
30151da177e4SLinus Torvalds module_param(major, uint, 0);
30161da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
3017f4790826SMike Snitzer 
3018e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
3019e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
3020e8603136SMike Snitzer 
3021115485e8SMike Snitzer module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
3022115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
3023115485e8SMike Snitzer 
3024a666e5c0SMikulas Patocka module_param(swap_bios, int, S_IRUGO | S_IWUSR);
3025a666e5c0SMikulas Patocka MODULE_PARM_DESC(swap_bios, "Maximum allowed inflight swap IOs");
3026a666e5c0SMikulas Patocka 
30271da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
30281da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
30291da177e4SLinus Torvalds MODULE_LICENSE("GPL");
3030