xref: /openbmc/linux/drivers/md/dm.c (revision 23e5083b4d47e778bf7983329989dab7543def14)
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 
81da177e4SLinus Torvalds #include "dm.h"
951e5b2bdSMike Anderson #include "dm-uevent.h"
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/init.h>
121da177e4SLinus Torvalds #include <linux/module.h>
1348c9c27bSArjan van de Ven #include <linux/mutex.h>
141da177e4SLinus Torvalds #include <linux/moduleparam.h>
151da177e4SLinus Torvalds #include <linux/blkpg.h>
161da177e4SLinus Torvalds #include <linux/bio.h>
171da177e4SLinus Torvalds #include <linux/mempool.h>
181da177e4SLinus Torvalds #include <linux/slab.h>
191da177e4SLinus Torvalds #include <linux/idr.h>
203ac51e74SDarrick J. Wong #include <linux/hdreg.h>
213f77316dSKiyoshi Ueda #include <linux/delay.h>
2255782138SLi Zefan 
2355782138SLi Zefan #include <trace/events/block.h>
241da177e4SLinus Torvalds 
2572d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
2672d94861SAlasdair G Kergon 
2771a16736SNamhyung Kim #ifdef CONFIG_PRINTK
2871a16736SNamhyung Kim /*
2971a16736SNamhyung Kim  * ratelimit state to be used in DMXXX_LIMIT().
3071a16736SNamhyung Kim  */
3171a16736SNamhyung Kim DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
3271a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_INTERVAL,
3371a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_BURST);
3471a16736SNamhyung Kim EXPORT_SYMBOL(dm_ratelimit_state);
3571a16736SNamhyung Kim #endif
3671a16736SNamhyung Kim 
3760935eb2SMilan Broz /*
3860935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
3960935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
4060935eb2SMilan Broz  */
4160935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
4260935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
4360935eb2SMilan Broz 
441da177e4SLinus Torvalds static const char *_name = DM_NAME;
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds static unsigned int major = 0;
471da177e4SLinus Torvalds static unsigned int _major = 0;
481da177e4SLinus Torvalds 
49d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr);
50d15b774cSAlasdair G Kergon 
51f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
521da177e4SLinus Torvalds /*
538fbf26adSKiyoshi Ueda  * For bio-based dm.
541da177e4SLinus Torvalds  * One of these is allocated per bio.
551da177e4SLinus Torvalds  */
561da177e4SLinus Torvalds struct dm_io {
571da177e4SLinus Torvalds 	struct mapped_device *md;
581da177e4SLinus Torvalds 	int error;
591da177e4SLinus Torvalds 	atomic_t io_count;
606ae2fa67SRichard Kennedy 	struct bio *bio;
613eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
62f88fb981SKiyoshi Ueda 	spinlock_t endio_lock;
631da177e4SLinus Torvalds };
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /*
668fbf26adSKiyoshi Ueda  * For request-based dm.
678fbf26adSKiyoshi Ueda  * One of these is allocated per request.
688fbf26adSKiyoshi Ueda  */
698fbf26adSKiyoshi Ueda struct dm_rq_target_io {
708fbf26adSKiyoshi Ueda 	struct mapped_device *md;
718fbf26adSKiyoshi Ueda 	struct dm_target *ti;
728fbf26adSKiyoshi Ueda 	struct request *orig, clone;
738fbf26adSKiyoshi Ueda 	int error;
748fbf26adSKiyoshi Ueda 	union map_info info;
758fbf26adSKiyoshi Ueda };
768fbf26adSKiyoshi Ueda 
778fbf26adSKiyoshi Ueda /*
7894818742SKent Overstreet  * For request-based dm - the bio clones we allocate are embedded in these
7994818742SKent Overstreet  * structs.
8094818742SKent Overstreet  *
8194818742SKent Overstreet  * We allocate these with bio_alloc_bioset, using the front_pad parameter when
8294818742SKent Overstreet  * the bioset is created - this means the bio has to come at the end of the
8394818742SKent Overstreet  * struct.
848fbf26adSKiyoshi Ueda  */
858fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info {
868fbf26adSKiyoshi Ueda 	struct bio *orig;
87cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio;
8894818742SKent Overstreet 	struct bio clone;
898fbf26adSKiyoshi Ueda };
908fbf26adSKiyoshi Ueda 
911da177e4SLinus Torvalds union map_info *dm_get_mapinfo(struct bio *bio)
921da177e4SLinus Torvalds {
931da177e4SLinus Torvalds 	if (bio && bio->bi_private)
94028867acSAlasdair G Kergon 		return &((struct dm_target_io *)bio->bi_private)->info;
951da177e4SLinus Torvalds 	return NULL;
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
98cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq)
99cec47e3dSKiyoshi Ueda {
100cec47e3dSKiyoshi Ueda 	if (rq && rq->end_io_data)
101cec47e3dSKiyoshi Ueda 		return &((struct dm_rq_target_io *)rq->end_io_data)->info;
102cec47e3dSKiyoshi Ueda 	return NULL;
103cec47e3dSKiyoshi Ueda }
104cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
105cec47e3dSKiyoshi Ueda 
106ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
107ba61fdd1SJeff Mahoney 
1081da177e4SLinus Torvalds /*
1091da177e4SLinus Torvalds  * Bits for the md->flags field.
1101da177e4SLinus Torvalds  */
1111eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
1121da177e4SLinus Torvalds #define DMF_SUSPENDED 1
113aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
114fba9f90eSJeff Mahoney #define DMF_FREEING 3
1155c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
1162e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
117d5b9dd04SMikulas Patocka #define DMF_MERGE_IS_OPTIONAL 6
1181da177e4SLinus Torvalds 
119304f3f6aSMilan Broz /*
120304f3f6aSMilan Broz  * Work processed by per-device workqueue.
121304f3f6aSMilan Broz  */
1221da177e4SLinus Torvalds struct mapped_device {
1232ca3310eSAlasdair G Kergon 	struct rw_semaphore io_lock;
124e61290a4SDaniel Walker 	struct mutex suspend_lock;
1251da177e4SLinus Torvalds 	rwlock_t map_lock;
1261da177e4SLinus Torvalds 	atomic_t holders;
1275c6bd75dSAlasdair G Kergon 	atomic_t open_count;
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	unsigned long flags;
1301da177e4SLinus Torvalds 
131165125e1SJens Axboe 	struct request_queue *queue;
132a5664dadSMike Snitzer 	unsigned type;
1334a0b4ddfSMike Snitzer 	/* Protect queue and type against concurrent access. */
134a5664dadSMike Snitzer 	struct mutex type_lock;
135a5664dadSMike Snitzer 
13636a0456fSAlasdair G Kergon 	struct target_type *immutable_target_type;
13736a0456fSAlasdair G Kergon 
1381da177e4SLinus Torvalds 	struct gendisk *disk;
1397e51f257SMike Anderson 	char name[16];
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	void *interface_ptr;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	/*
1441da177e4SLinus Torvalds 	 * A list of ios that arrived while we were suspended.
1451da177e4SLinus Torvalds 	 */
146316d315bSNikanth Karthikesan 	atomic_t pending[2];
1471da177e4SLinus Torvalds 	wait_queue_head_t wait;
14853d5914fSMikulas Patocka 	struct work_struct work;
1491da177e4SLinus Torvalds 	struct bio_list deferred;
150022c2611SMikulas Patocka 	spinlock_t deferred_lock;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	/*
15329e4013dSTejun Heo 	 * Processing queue (flush)
154304f3f6aSMilan Broz 	 */
155304f3f6aSMilan Broz 	struct workqueue_struct *wq;
156304f3f6aSMilan Broz 
157304f3f6aSMilan Broz 	/*
1581da177e4SLinus Torvalds 	 * The current mapping.
1591da177e4SLinus Torvalds 	 */
1601da177e4SLinus Torvalds 	struct dm_table *map;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	/*
1631da177e4SLinus Torvalds 	 * io objects are allocated from here.
1641da177e4SLinus Torvalds 	 */
1651da177e4SLinus Torvalds 	mempool_t *io_pool;
1661da177e4SLinus Torvalds 	mempool_t *tio_pool;
1671da177e4SLinus Torvalds 
1689faf400fSStefan Bader 	struct bio_set *bs;
1699faf400fSStefan Bader 
1701da177e4SLinus Torvalds 	/*
1711da177e4SLinus Torvalds 	 * Event handling.
1721da177e4SLinus Torvalds 	 */
1731da177e4SLinus Torvalds 	atomic_t event_nr;
1741da177e4SLinus Torvalds 	wait_queue_head_t eventq;
1757a8c3d3bSMike Anderson 	atomic_t uevent_seq;
1767a8c3d3bSMike Anderson 	struct list_head uevent_list;
1777a8c3d3bSMike Anderson 	spinlock_t uevent_lock; /* Protect access to uevent_list */
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	/*
1801da177e4SLinus Torvalds 	 * freeze/thaw support require holding onto a super block
1811da177e4SLinus Torvalds 	 */
1821da177e4SLinus Torvalds 	struct super_block *frozen_sb;
183db8fef4fSMikulas Patocka 	struct block_device *bdev;
1843ac51e74SDarrick J. Wong 
1853ac51e74SDarrick J. Wong 	/* forced geometry settings */
1863ac51e74SDarrick J. Wong 	struct hd_geometry geometry;
187784aae73SMilan Broz 
188784aae73SMilan Broz 	/* sysfs handle */
189784aae73SMilan Broz 	struct kobject kobj;
19052b1fd5aSMikulas Patocka 
191d87f4c14STejun Heo 	/* zero-length flush that will be cloned and submitted to targets */
192d87f4c14STejun Heo 	struct bio flush_bio;
1931da177e4SLinus Torvalds };
1941da177e4SLinus Torvalds 
195e6ee8c0bSKiyoshi Ueda /*
196e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
197e6ee8c0bSKiyoshi Ueda  */
198e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
199e6ee8c0bSKiyoshi Ueda 	mempool_t *io_pool;
200e6ee8c0bSKiyoshi Ueda 	mempool_t *tio_pool;
201e6ee8c0bSKiyoshi Ueda 	struct bio_set *bs;
202e6ee8c0bSKiyoshi Ueda };
203e6ee8c0bSKiyoshi Ueda 
2041da177e4SLinus Torvalds #define MIN_IOS 256
205e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
2068fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
20794818742SKent Overstreet 
2081da177e4SLinus Torvalds static int __init local_init(void)
2091da177e4SLinus Torvalds {
21051157b4aSKiyoshi Ueda 	int r = -ENOMEM;
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
213028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
2141da177e4SLinus Torvalds 	if (!_io_cache)
21551157b4aSKiyoshi Ueda 		return r;
2161da177e4SLinus Torvalds 
2178fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
2188fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
219dba14160SMikulas Patocka 		goto out_free_io_cache;
2208fbf26adSKiyoshi Ueda 
22151e5b2bdSMike Anderson 	r = dm_uevent_init();
22251157b4aSKiyoshi Ueda 	if (r)
223*23e5083bSJun'ichi Nomura 		goto out_free_rq_tio_cache;
22451e5b2bdSMike Anderson 
2251da177e4SLinus Torvalds 	_major = major;
2261da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
22751157b4aSKiyoshi Ueda 	if (r < 0)
22851157b4aSKiyoshi Ueda 		goto out_uevent_exit;
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	if (!_major)
2311da177e4SLinus Torvalds 		_major = r;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	return 0;
23451157b4aSKiyoshi Ueda 
23551157b4aSKiyoshi Ueda out_uevent_exit:
23651157b4aSKiyoshi Ueda 	dm_uevent_exit();
2378fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
2388fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
23951157b4aSKiyoshi Ueda out_free_io_cache:
24051157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
24151157b4aSKiyoshi Ueda 
24251157b4aSKiyoshi Ueda 	return r;
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds static void local_exit(void)
2461da177e4SLinus Torvalds {
2478fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
2481da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
24900d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
25051e5b2bdSMike Anderson 	dm_uevent_exit();
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	_major = 0;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	DMINFO("cleaned up");
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
257b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
2581da177e4SLinus Torvalds 	local_init,
2591da177e4SLinus Torvalds 	dm_target_init,
2601da177e4SLinus Torvalds 	dm_linear_init,
2611da177e4SLinus Torvalds 	dm_stripe_init,
262952b3557SMikulas Patocka 	dm_io_init,
263945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2641da177e4SLinus Torvalds 	dm_interface_init,
2651da177e4SLinus Torvalds };
2661da177e4SLinus Torvalds 
267b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2681da177e4SLinus Torvalds 	local_exit,
2691da177e4SLinus Torvalds 	dm_target_exit,
2701da177e4SLinus Torvalds 	dm_linear_exit,
2711da177e4SLinus Torvalds 	dm_stripe_exit,
272952b3557SMikulas Patocka 	dm_io_exit,
273945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
2741da177e4SLinus Torvalds 	dm_interface_exit,
2751da177e4SLinus Torvalds };
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds static int __init dm_init(void)
2781da177e4SLinus Torvalds {
2791da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	int r, i;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
2841da177e4SLinus Torvalds 		r = _inits[i]();
2851da177e4SLinus Torvalds 		if (r)
2861da177e4SLinus Torvalds 			goto bad;
2871da177e4SLinus Torvalds 	}
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 	return 0;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds       bad:
2921da177e4SLinus Torvalds 	while (i--)
2931da177e4SLinus Torvalds 		_exits[i]();
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	return r;
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds static void __exit dm_exit(void)
2991da177e4SLinus Torvalds {
3001da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	while (i--)
3031da177e4SLinus Torvalds 		_exits[i]();
304d15b774cSAlasdair G Kergon 
305d15b774cSAlasdair G Kergon 	/*
306d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
307d15b774cSAlasdair G Kergon 	 */
308d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds /*
3121da177e4SLinus Torvalds  * Block device functions
3131da177e4SLinus Torvalds  */
314432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
315432a212cSMike Anderson {
316432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
317432a212cSMike Anderson }
318432a212cSMike Anderson 
319fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3201da177e4SLinus Torvalds {
3211da177e4SLinus Torvalds 	struct mapped_device *md;
3221da177e4SLinus Torvalds 
323fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
324fba9f90eSJeff Mahoney 
325fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
326fba9f90eSJeff Mahoney 	if (!md)
327fba9f90eSJeff Mahoney 		goto out;
328fba9f90eSJeff Mahoney 
3295c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
330432a212cSMike Anderson 	    dm_deleting_md(md)) {
331fba9f90eSJeff Mahoney 		md = NULL;
332fba9f90eSJeff Mahoney 		goto out;
333fba9f90eSJeff Mahoney 	}
334fba9f90eSJeff Mahoney 
3351da177e4SLinus Torvalds 	dm_get(md);
3365c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
337fba9f90eSJeff Mahoney 
338fba9f90eSJeff Mahoney out:
339fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
340fba9f90eSJeff Mahoney 
341fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
344fe5f9f2cSAl Viro static int dm_blk_close(struct gendisk *disk, fmode_t mode)
3451da177e4SLinus Torvalds {
346fe5f9f2cSAl Viro 	struct mapped_device *md = disk->private_data;
3476e9624b8SArnd Bergmann 
3484a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
3494a1aeb98SMilan Broz 
3505c6bd75dSAlasdair G Kergon 	atomic_dec(&md->open_count);
3511da177e4SLinus Torvalds 	dm_put(md);
3524a1aeb98SMilan Broz 
3534a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
3546e9624b8SArnd Bergmann 
3551da177e4SLinus Torvalds 	return 0;
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds 
3585c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3595c6bd75dSAlasdair G Kergon {
3605c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3615c6bd75dSAlasdair G Kergon }
3625c6bd75dSAlasdair G Kergon 
3635c6bd75dSAlasdair G Kergon /*
3645c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3655c6bd75dSAlasdair G Kergon  */
3665c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md)
3675c6bd75dSAlasdair G Kergon {
3685c6bd75dSAlasdair G Kergon 	int r = 0;
3695c6bd75dSAlasdair G Kergon 
3705c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
3715c6bd75dSAlasdair G Kergon 
3725c6bd75dSAlasdair G Kergon 	if (dm_open_count(md))
3735c6bd75dSAlasdair G Kergon 		r = -EBUSY;
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 
3823ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3833ac51e74SDarrick J. Wong {
3843ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
3853ac51e74SDarrick J. Wong 
3863ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
3873ac51e74SDarrick J. Wong }
3883ac51e74SDarrick J. Wong 
389fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
390aa129a22SMilan Broz 			unsigned int cmd, unsigned long arg)
391aa129a22SMilan Broz {
392fe5f9f2cSAl Viro 	struct mapped_device *md = bdev->bd_disk->private_data;
3937c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
394aa129a22SMilan Broz 	struct dm_target *tgt;
395aa129a22SMilan Broz 	int r = -ENOTTY;
396aa129a22SMilan Broz 
397aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
398aa129a22SMilan Broz 		goto out;
399aa129a22SMilan Broz 
400aa129a22SMilan Broz 	/* We only support devices that have a single target */
401aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
402aa129a22SMilan Broz 		goto out;
403aa129a22SMilan Broz 
404aa129a22SMilan Broz 	tgt = dm_table_get_target(map, 0);
405aa129a22SMilan Broz 
4064f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
407aa129a22SMilan Broz 		r = -EAGAIN;
408aa129a22SMilan Broz 		goto out;
409aa129a22SMilan Broz 	}
410aa129a22SMilan Broz 
411aa129a22SMilan Broz 	if (tgt->type->ioctl)
412647b3d00SAl Viro 		r = tgt->type->ioctl(tgt, cmd, arg);
413aa129a22SMilan Broz 
414aa129a22SMilan Broz out:
415aa129a22SMilan Broz 	dm_table_put(map);
416aa129a22SMilan Broz 
417aa129a22SMilan Broz 	return r;
418aa129a22SMilan Broz }
419aa129a22SMilan Broz 
420028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
4211da177e4SLinus Torvalds {
4221da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
4231da177e4SLinus Torvalds }
4241da177e4SLinus Torvalds 
425028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
4261da177e4SLinus Torvalds {
4271da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
430028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
4311da177e4SLinus Torvalds {
432dba14160SMikulas Patocka 	bio_put(&tio->clone);
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
43508885643SKiyoshi Ueda static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
43608885643SKiyoshi Ueda 					    gfp_t gfp_mask)
437cec47e3dSKiyoshi Ueda {
43808885643SKiyoshi Ueda 	return mempool_alloc(md->tio_pool, gfp_mask);
439cec47e3dSKiyoshi Ueda }
440cec47e3dSKiyoshi Ueda 
441cec47e3dSKiyoshi Ueda static void free_rq_tio(struct dm_rq_target_io *tio)
442cec47e3dSKiyoshi Ueda {
443cec47e3dSKiyoshi Ueda 	mempool_free(tio, tio->md->tio_pool);
444cec47e3dSKiyoshi Ueda }
445cec47e3dSKiyoshi Ueda 
44690abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md)
44790abb8c4SKiyoshi Ueda {
44890abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
44990abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
45090abb8c4SKiyoshi Ueda }
45190abb8c4SKiyoshi Ueda 
4523eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
4533eaf840eSJun'ichi "Nick" Nomura {
4543eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
455c9959059STejun Heo 	int cpu;
456316d315bSNikanth Karthikesan 	int rw = bio_data_dir(io->bio);
4573eaf840eSJun'ichi "Nick" Nomura 
4583eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
4593eaf840eSJun'ichi "Nick" Nomura 
460074a7acaSTejun Heo 	cpu = part_stat_lock();
461074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
462074a7acaSTejun Heo 	part_stat_unlock();
4631e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw],
4641e9bb880SShaohua Li 		atomic_inc_return(&md->pending[rw]));
4653eaf840eSJun'ichi "Nick" Nomura }
4663eaf840eSJun'ichi "Nick" Nomura 
467d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
4683eaf840eSJun'ichi "Nick" Nomura {
4693eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
4703eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
4713eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
472c9959059STejun Heo 	int pending, cpu;
4733eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
4743eaf840eSJun'ichi "Nick" Nomura 
475074a7acaSTejun Heo 	cpu = part_stat_lock();
476074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
477074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
478074a7acaSTejun Heo 	part_stat_unlock();
4793eaf840eSJun'ichi "Nick" Nomura 
480af7e466aSMikulas Patocka 	/*
481af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
482d87f4c14STejun Heo 	 * a flush.
483af7e466aSMikulas Patocka 	 */
4841e9bb880SShaohua Li 	pending = atomic_dec_return(&md->pending[rw]);
4851e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
486316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
4873eaf840eSJun'ichi "Nick" Nomura 
488d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
489d221d2e7SMikulas Patocka 	if (!pending)
490d221d2e7SMikulas Patocka 		wake_up(&md->wait);
4913eaf840eSJun'ichi "Nick" Nomura }
4923eaf840eSJun'ichi "Nick" Nomura 
4931da177e4SLinus Torvalds /*
4941da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
4951da177e4SLinus Torvalds  */
49692c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
4971da177e4SLinus Torvalds {
49805447420SKiyoshi Ueda 	unsigned long flags;
4991da177e4SLinus Torvalds 
50005447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
5011da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
50205447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
50392c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
5041da177e4SLinus Torvalds }
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds /*
5071da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
5081da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
5091da177e4SLinus Torvalds  * dm_table_put() when finished.
5101da177e4SLinus Torvalds  */
5117c666411SAlasdair G Kergon struct dm_table *dm_get_live_table(struct mapped_device *md)
5121da177e4SLinus Torvalds {
5131da177e4SLinus Torvalds 	struct dm_table *t;
514523d9297SKiyoshi Ueda 	unsigned long flags;
5151da177e4SLinus Torvalds 
516523d9297SKiyoshi Ueda 	read_lock_irqsave(&md->map_lock, flags);
5171da177e4SLinus Torvalds 	t = md->map;
5181da177e4SLinus Torvalds 	if (t)
5191da177e4SLinus Torvalds 		dm_table_get(t);
520523d9297SKiyoshi Ueda 	read_unlock_irqrestore(&md->map_lock, flags);
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	return t;
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds 
5253ac51e74SDarrick J. Wong /*
5263ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
5273ac51e74SDarrick J. Wong  */
5283ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
5293ac51e74SDarrick J. Wong {
5303ac51e74SDarrick J. Wong 	*geo = md->geometry;
5313ac51e74SDarrick J. Wong 
5323ac51e74SDarrick J. Wong 	return 0;
5333ac51e74SDarrick J. Wong }
5343ac51e74SDarrick J. Wong 
5353ac51e74SDarrick J. Wong /*
5363ac51e74SDarrick J. Wong  * Set the geometry of a device.
5373ac51e74SDarrick J. Wong  */
5383ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
5393ac51e74SDarrick J. Wong {
5403ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
5413ac51e74SDarrick J. Wong 
5423ac51e74SDarrick J. Wong 	if (geo->start > sz) {
5433ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
5443ac51e74SDarrick J. Wong 		return -EINVAL;
5453ac51e74SDarrick J. Wong 	}
5463ac51e74SDarrick J. Wong 
5473ac51e74SDarrick J. Wong 	md->geometry = *geo;
5483ac51e74SDarrick J. Wong 
5493ac51e74SDarrick J. Wong 	return 0;
5503ac51e74SDarrick J. Wong }
5513ac51e74SDarrick J. Wong 
5521da177e4SLinus Torvalds /*-----------------------------------------------------------------
5531da177e4SLinus Torvalds  * CRUD START:
5541da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
5551da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
5561da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
5571da177e4SLinus Torvalds  *   interests of getting something for people to use I give
5581da177e4SLinus Torvalds  *   you this clearly demarcated crap.
5591da177e4SLinus Torvalds  *---------------------------------------------------------------*/
5601da177e4SLinus Torvalds 
5612e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
5622e93ccc1SKiyoshi Ueda {
5632e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
5642e93ccc1SKiyoshi Ueda }
5652e93ccc1SKiyoshi Ueda 
5661da177e4SLinus Torvalds /*
5671da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
5681da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
5691da177e4SLinus Torvalds  */
570858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
5711da177e4SLinus Torvalds {
5722e93ccc1SKiyoshi Ueda 	unsigned long flags;
573b35f8caaSMilan Broz 	int io_error;
574b35f8caaSMilan Broz 	struct bio *bio;
575b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
5762e93ccc1SKiyoshi Ueda 
5772e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
578f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
579f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
580f88fb981SKiyoshi Ueda 		if (!(io->error > 0 && __noflush_suspending(md)))
5811da177e4SLinus Torvalds 			io->error = error;
582f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
583f88fb981SKiyoshi Ueda 	}
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
5862e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
5872e93ccc1SKiyoshi Ueda 			/*
5882e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
5892e93ccc1SKiyoshi Ueda 			 */
590022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
5916a8736d1STejun Heo 			if (__noflush_suspending(md))
5926a8736d1STejun Heo 				bio_list_add_head(&md->deferred, io->bio);
5936a8736d1STejun Heo 			else
5942e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
5952e93ccc1SKiyoshi Ueda 				io->error = -EIO;
596022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
5972e93ccc1SKiyoshi Ueda 		}
5982e93ccc1SKiyoshi Ueda 
599b35f8caaSMilan Broz 		io_error = io->error;
600b35f8caaSMilan Broz 		bio = io->bio;
601af7e466aSMikulas Patocka 		end_io_acct(io);
602a97f925aSMikulas Patocka 		free_io(md, io);
6031da177e4SLinus Torvalds 
6046a8736d1STejun Heo 		if (io_error == DM_ENDIO_REQUEUE)
6056a8736d1STejun Heo 			return;
6066a8736d1STejun Heo 
607b372d360SMike Snitzer 		if ((bio->bi_rw & REQ_FLUSH) && bio->bi_size) {
6081da177e4SLinus Torvalds 			/*
6096a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
6106a8736d1STejun Heo 			 * without REQ_FLUSH.
6111da177e4SLinus Torvalds 			 */
6126a8736d1STejun Heo 			bio->bi_rw &= ~REQ_FLUSH;
6136a8736d1STejun Heo 			queue_io(md, bio);
6145f3ea37cSArnaldo Carvalho de Melo 		} else {
615b372d360SMike Snitzer 			/* done with normal IO or empty flush */
616b35f8caaSMilan Broz 			bio_endio(bio, io_error);
6172e93ccc1SKiyoshi Ueda 		}
6181da177e4SLinus Torvalds 	}
619af7e466aSMikulas Patocka }
6201da177e4SLinus Torvalds 
6216712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error)
6221da177e4SLinus Torvalds {
6231da177e4SLinus Torvalds 	int r = 0;
624028867acSAlasdair G Kergon 	struct dm_target_io *tio = bio->bi_private;
625b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
6269faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
6271da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	if (!bio_flagged(bio, BIO_UPTODATE) && !error)
6301da177e4SLinus Torvalds 		error = -EIO;
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 	if (endio) {
6337de3ee57SMikulas Patocka 		r = endio(tio->ti, bio, error);
6342e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
6352e93ccc1SKiyoshi Ueda 			/*
6362e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
6372e93ccc1SKiyoshi Ueda 			 * in dec_pending().
6382e93ccc1SKiyoshi Ueda 			 */
6391da177e4SLinus Torvalds 			error = r;
64045cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
64145cbcd79SKiyoshi Ueda 			/* The target will handle the io */
6426712ecf8SNeilBrown 			return;
64345cbcd79SKiyoshi Ueda 		else if (r) {
64445cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
64545cbcd79SKiyoshi Ueda 			BUG();
64645cbcd79SKiyoshi Ueda 		}
6471da177e4SLinus Torvalds 	}
6481da177e4SLinus Torvalds 
6499faf400fSStefan Bader 	free_tio(md, tio);
650b35f8caaSMilan Broz 	dec_pending(io, error);
6511da177e4SLinus Torvalds }
6521da177e4SLinus Torvalds 
653cec47e3dSKiyoshi Ueda /*
654cec47e3dSKiyoshi Ueda  * Partial completion handling for request-based dm
655cec47e3dSKiyoshi Ueda  */
656cec47e3dSKiyoshi Ueda static void end_clone_bio(struct bio *clone, int error)
657cec47e3dSKiyoshi Ueda {
658cec47e3dSKiyoshi Ueda 	struct dm_rq_clone_bio_info *info = clone->bi_private;
659cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = info->tio;
660cec47e3dSKiyoshi Ueda 	struct bio *bio = info->orig;
661cec47e3dSKiyoshi Ueda 	unsigned int nr_bytes = info->orig->bi_size;
662cec47e3dSKiyoshi Ueda 
663cec47e3dSKiyoshi Ueda 	bio_put(clone);
664cec47e3dSKiyoshi Ueda 
665cec47e3dSKiyoshi Ueda 	if (tio->error)
666cec47e3dSKiyoshi Ueda 		/*
667cec47e3dSKiyoshi Ueda 		 * An error has already been detected on the request.
668cec47e3dSKiyoshi Ueda 		 * Once error occurred, just let clone->end_io() handle
669cec47e3dSKiyoshi Ueda 		 * the remainder.
670cec47e3dSKiyoshi Ueda 		 */
671cec47e3dSKiyoshi Ueda 		return;
672cec47e3dSKiyoshi Ueda 	else if (error) {
673cec47e3dSKiyoshi Ueda 		/*
674cec47e3dSKiyoshi Ueda 		 * Don't notice the error to the upper layer yet.
675cec47e3dSKiyoshi Ueda 		 * The error handling decision is made by the target driver,
676cec47e3dSKiyoshi Ueda 		 * when the request is completed.
677cec47e3dSKiyoshi Ueda 		 */
678cec47e3dSKiyoshi Ueda 		tio->error = error;
679cec47e3dSKiyoshi Ueda 		return;
680cec47e3dSKiyoshi Ueda 	}
681cec47e3dSKiyoshi Ueda 
682cec47e3dSKiyoshi Ueda 	/*
683cec47e3dSKiyoshi Ueda 	 * I/O for the bio successfully completed.
684cec47e3dSKiyoshi Ueda 	 * Notice the data completion to the upper layer.
685cec47e3dSKiyoshi Ueda 	 */
686cec47e3dSKiyoshi Ueda 
687cec47e3dSKiyoshi Ueda 	/*
688cec47e3dSKiyoshi Ueda 	 * bios are processed from the head of the list.
689cec47e3dSKiyoshi Ueda 	 * So the completing bio should always be rq->bio.
690cec47e3dSKiyoshi Ueda 	 * If it's not, something wrong is happening.
691cec47e3dSKiyoshi Ueda 	 */
692cec47e3dSKiyoshi Ueda 	if (tio->orig->bio != bio)
693cec47e3dSKiyoshi Ueda 		DMERR("bio completion is going in the middle of the request");
694cec47e3dSKiyoshi Ueda 
695cec47e3dSKiyoshi Ueda 	/*
696cec47e3dSKiyoshi Ueda 	 * Update the original request.
697cec47e3dSKiyoshi Ueda 	 * Do not use blk_end_request() here, because it may complete
698cec47e3dSKiyoshi Ueda 	 * the original request before the clone, and break the ordering.
699cec47e3dSKiyoshi Ueda 	 */
700cec47e3dSKiyoshi Ueda 	blk_update_request(tio->orig, 0, nr_bytes);
701cec47e3dSKiyoshi Ueda }
702cec47e3dSKiyoshi Ueda 
703cec47e3dSKiyoshi Ueda /*
704cec47e3dSKiyoshi Ueda  * Don't touch any member of the md after calling this function because
705cec47e3dSKiyoshi Ueda  * the md may be freed in dm_put() at the end of this function.
706cec47e3dSKiyoshi Ueda  * Or do dm_get() before calling this function and dm_put() later.
707cec47e3dSKiyoshi Ueda  */
708b4324feeSKiyoshi Ueda static void rq_completed(struct mapped_device *md, int rw, int run_queue)
709cec47e3dSKiyoshi Ueda {
710b4324feeSKiyoshi Ueda 	atomic_dec(&md->pending[rw]);
711cec47e3dSKiyoshi Ueda 
712cec47e3dSKiyoshi Ueda 	/* nudge anyone waiting on suspend queue */
713b4324feeSKiyoshi Ueda 	if (!md_in_flight(md))
714cec47e3dSKiyoshi Ueda 		wake_up(&md->wait);
715cec47e3dSKiyoshi Ueda 
716a8c32a5cSJens Axboe 	/*
717a8c32a5cSJens Axboe 	 * Run this off this callpath, as drivers could invoke end_io while
718a8c32a5cSJens Axboe 	 * inside their request_fn (and holding the queue lock). Calling
719a8c32a5cSJens Axboe 	 * back into ->request_fn() could deadlock attempting to grab the
720a8c32a5cSJens Axboe 	 * queue lock again.
721a8c32a5cSJens Axboe 	 */
722cec47e3dSKiyoshi Ueda 	if (run_queue)
723a8c32a5cSJens Axboe 		blk_run_queue_async(md->queue);
724cec47e3dSKiyoshi Ueda 
725cec47e3dSKiyoshi Ueda 	/*
726cec47e3dSKiyoshi Ueda 	 * dm_put() must be at the end of this function. See the comment above
727cec47e3dSKiyoshi Ueda 	 */
728cec47e3dSKiyoshi Ueda 	dm_put(md);
729cec47e3dSKiyoshi Ueda }
730cec47e3dSKiyoshi Ueda 
731a77e28c7SKiyoshi Ueda static void free_rq_clone(struct request *clone)
732a77e28c7SKiyoshi Ueda {
733a77e28c7SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
734a77e28c7SKiyoshi Ueda 
735a77e28c7SKiyoshi Ueda 	blk_rq_unprep_clone(clone);
736a77e28c7SKiyoshi Ueda 	free_rq_tio(tio);
737a77e28c7SKiyoshi Ueda }
738a77e28c7SKiyoshi Ueda 
739980691e5SKiyoshi Ueda /*
740980691e5SKiyoshi Ueda  * Complete the clone and the original request.
741980691e5SKiyoshi Ueda  * Must be called without queue lock.
742980691e5SKiyoshi Ueda  */
743980691e5SKiyoshi Ueda static void dm_end_request(struct request *clone, int error)
744980691e5SKiyoshi Ueda {
745980691e5SKiyoshi Ueda 	int rw = rq_data_dir(clone);
746980691e5SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
747980691e5SKiyoshi Ueda 	struct mapped_device *md = tio->md;
748980691e5SKiyoshi Ueda 	struct request *rq = tio->orig;
749980691e5SKiyoshi Ueda 
75029e4013dSTejun Heo 	if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
751980691e5SKiyoshi Ueda 		rq->errors = clone->errors;
752980691e5SKiyoshi Ueda 		rq->resid_len = clone->resid_len;
753980691e5SKiyoshi Ueda 
754980691e5SKiyoshi Ueda 		if (rq->sense)
755980691e5SKiyoshi Ueda 			/*
756980691e5SKiyoshi Ueda 			 * We are using the sense buffer of the original
757980691e5SKiyoshi Ueda 			 * request.
758980691e5SKiyoshi Ueda 			 * So setting the length of the sense data is enough.
759980691e5SKiyoshi Ueda 			 */
760980691e5SKiyoshi Ueda 			rq->sense_len = clone->sense_len;
761980691e5SKiyoshi Ueda 	}
762980691e5SKiyoshi Ueda 
763980691e5SKiyoshi Ueda 	free_rq_clone(clone);
764980691e5SKiyoshi Ueda 	blk_end_request_all(rq, error);
76529e4013dSTejun Heo 	rq_completed(md, rw, true);
766980691e5SKiyoshi Ueda }
767980691e5SKiyoshi Ueda 
768cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq)
769cec47e3dSKiyoshi Ueda {
770cec47e3dSKiyoshi Ueda 	struct request *clone = rq->special;
771cec47e3dSKiyoshi Ueda 
772cec47e3dSKiyoshi Ueda 	rq->special = NULL;
773cec47e3dSKiyoshi Ueda 	rq->cmd_flags &= ~REQ_DONTPREP;
774cec47e3dSKiyoshi Ueda 
775a77e28c7SKiyoshi Ueda 	free_rq_clone(clone);
776cec47e3dSKiyoshi Ueda }
777cec47e3dSKiyoshi Ueda 
778cec47e3dSKiyoshi Ueda /*
779cec47e3dSKiyoshi Ueda  * Requeue the original request of a clone.
780cec47e3dSKiyoshi Ueda  */
781cec47e3dSKiyoshi Ueda void dm_requeue_unmapped_request(struct request *clone)
782cec47e3dSKiyoshi Ueda {
783b4324feeSKiyoshi Ueda 	int rw = rq_data_dir(clone);
784cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
785cec47e3dSKiyoshi Ueda 	struct mapped_device *md = tio->md;
786cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
787cec47e3dSKiyoshi Ueda 	struct request_queue *q = rq->q;
788cec47e3dSKiyoshi Ueda 	unsigned long flags;
789cec47e3dSKiyoshi Ueda 
790cec47e3dSKiyoshi Ueda 	dm_unprep_request(rq);
791cec47e3dSKiyoshi Ueda 
792cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
793cec47e3dSKiyoshi Ueda 	blk_requeue_request(q, rq);
794cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
795cec47e3dSKiyoshi Ueda 
796b4324feeSKiyoshi Ueda 	rq_completed(md, rw, 0);
797cec47e3dSKiyoshi Ueda }
798cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
799cec47e3dSKiyoshi Ueda 
800cec47e3dSKiyoshi Ueda static void __stop_queue(struct request_queue *q)
801cec47e3dSKiyoshi Ueda {
802cec47e3dSKiyoshi Ueda 	blk_stop_queue(q);
803cec47e3dSKiyoshi Ueda }
804cec47e3dSKiyoshi Ueda 
805cec47e3dSKiyoshi Ueda static void stop_queue(struct request_queue *q)
806cec47e3dSKiyoshi Ueda {
807cec47e3dSKiyoshi Ueda 	unsigned long flags;
808cec47e3dSKiyoshi Ueda 
809cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
810cec47e3dSKiyoshi Ueda 	__stop_queue(q);
811cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
812cec47e3dSKiyoshi Ueda }
813cec47e3dSKiyoshi Ueda 
814cec47e3dSKiyoshi Ueda static void __start_queue(struct request_queue *q)
815cec47e3dSKiyoshi Ueda {
816cec47e3dSKiyoshi Ueda 	if (blk_queue_stopped(q))
817cec47e3dSKiyoshi Ueda 		blk_start_queue(q);
818cec47e3dSKiyoshi Ueda }
819cec47e3dSKiyoshi Ueda 
820cec47e3dSKiyoshi Ueda static void start_queue(struct request_queue *q)
821cec47e3dSKiyoshi Ueda {
822cec47e3dSKiyoshi Ueda 	unsigned long flags;
823cec47e3dSKiyoshi Ueda 
824cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
825cec47e3dSKiyoshi Ueda 	__start_queue(q);
826cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
827cec47e3dSKiyoshi Ueda }
828cec47e3dSKiyoshi Ueda 
82911a68244SKiyoshi Ueda static void dm_done(struct request *clone, int error, bool mapped)
83011a68244SKiyoshi Ueda {
83111a68244SKiyoshi Ueda 	int r = error;
83211a68244SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
833ba1cbad9SMike Snitzer 	dm_request_endio_fn rq_end_io = NULL;
834ba1cbad9SMike Snitzer 
835ba1cbad9SMike Snitzer 	if (tio->ti) {
836ba1cbad9SMike Snitzer 		rq_end_io = tio->ti->type->rq_end_io;
83711a68244SKiyoshi Ueda 
83811a68244SKiyoshi Ueda 		if (mapped && rq_end_io)
83911a68244SKiyoshi Ueda 			r = rq_end_io(tio->ti, clone, error, &tio->info);
840ba1cbad9SMike Snitzer 	}
84111a68244SKiyoshi Ueda 
84211a68244SKiyoshi Ueda 	if (r <= 0)
84311a68244SKiyoshi Ueda 		/* The target wants to complete the I/O */
84411a68244SKiyoshi Ueda 		dm_end_request(clone, r);
84511a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_INCOMPLETE)
84611a68244SKiyoshi Ueda 		/* The target will handle the I/O */
84711a68244SKiyoshi Ueda 		return;
84811a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_REQUEUE)
84911a68244SKiyoshi Ueda 		/* The target wants to requeue the I/O */
85011a68244SKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
85111a68244SKiyoshi Ueda 	else {
85211a68244SKiyoshi Ueda 		DMWARN("unimplemented target endio return value: %d", r);
85311a68244SKiyoshi Ueda 		BUG();
85411a68244SKiyoshi Ueda 	}
85511a68244SKiyoshi Ueda }
85611a68244SKiyoshi Ueda 
857cec47e3dSKiyoshi Ueda /*
858cec47e3dSKiyoshi Ueda  * Request completion handler for request-based dm
859cec47e3dSKiyoshi Ueda  */
860cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq)
861cec47e3dSKiyoshi Ueda {
86211a68244SKiyoshi Ueda 	bool mapped = true;
863cec47e3dSKiyoshi Ueda 	struct request *clone = rq->completion_data;
864cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
865cec47e3dSKiyoshi Ueda 
86611a68244SKiyoshi Ueda 	if (rq->cmd_flags & REQ_FAILED)
86711a68244SKiyoshi Ueda 		mapped = false;
868cec47e3dSKiyoshi Ueda 
86911a68244SKiyoshi Ueda 	dm_done(clone, tio->error, mapped);
870cec47e3dSKiyoshi Ueda }
871cec47e3dSKiyoshi Ueda 
872cec47e3dSKiyoshi Ueda /*
873cec47e3dSKiyoshi Ueda  * Complete the clone and the original request with the error status
874cec47e3dSKiyoshi Ueda  * through softirq context.
875cec47e3dSKiyoshi Ueda  */
876cec47e3dSKiyoshi Ueda static void dm_complete_request(struct request *clone, int error)
877cec47e3dSKiyoshi Ueda {
878cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
879cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
880cec47e3dSKiyoshi Ueda 
881cec47e3dSKiyoshi Ueda 	tio->error = error;
882cec47e3dSKiyoshi Ueda 	rq->completion_data = clone;
883cec47e3dSKiyoshi Ueda 	blk_complete_request(rq);
884cec47e3dSKiyoshi Ueda }
885cec47e3dSKiyoshi Ueda 
886cec47e3dSKiyoshi Ueda /*
887cec47e3dSKiyoshi Ueda  * Complete the not-mapped clone and the original request with the error status
888cec47e3dSKiyoshi Ueda  * through softirq context.
889cec47e3dSKiyoshi Ueda  * Target's rq_end_io() function isn't called.
890cec47e3dSKiyoshi Ueda  * This may be used when the target's map_rq() function fails.
891cec47e3dSKiyoshi Ueda  */
892cec47e3dSKiyoshi Ueda void dm_kill_unmapped_request(struct request *clone, int error)
893cec47e3dSKiyoshi Ueda {
894cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
895cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
896cec47e3dSKiyoshi Ueda 
897cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_FAILED;
898cec47e3dSKiyoshi Ueda 	dm_complete_request(clone, error);
899cec47e3dSKiyoshi Ueda }
900cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
901cec47e3dSKiyoshi Ueda 
902cec47e3dSKiyoshi Ueda /*
903cec47e3dSKiyoshi Ueda  * Called with the queue lock held
904cec47e3dSKiyoshi Ueda  */
905cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error)
906cec47e3dSKiyoshi Ueda {
907cec47e3dSKiyoshi Ueda 	/*
908cec47e3dSKiyoshi Ueda 	 * For just cleaning up the information of the queue in which
909cec47e3dSKiyoshi Ueda 	 * the clone was dispatched.
910cec47e3dSKiyoshi Ueda 	 * The clone is *NOT* freed actually here because it is alloced from
911cec47e3dSKiyoshi Ueda 	 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
912cec47e3dSKiyoshi Ueda 	 */
913cec47e3dSKiyoshi Ueda 	__blk_put_request(clone->q, clone);
914cec47e3dSKiyoshi Ueda 
915cec47e3dSKiyoshi Ueda 	/*
916cec47e3dSKiyoshi Ueda 	 * Actual request completion is done in a softirq context which doesn't
917cec47e3dSKiyoshi Ueda 	 * hold the queue lock.  Otherwise, deadlock could occur because:
918cec47e3dSKiyoshi Ueda 	 *     - another request may be submitted by the upper level driver
919cec47e3dSKiyoshi Ueda 	 *       of the stacking during the completion
920cec47e3dSKiyoshi Ueda 	 *     - the submission which requires queue lock may be done
921cec47e3dSKiyoshi Ueda 	 *       against this queue
922cec47e3dSKiyoshi Ueda 	 */
923cec47e3dSKiyoshi Ueda 	dm_complete_request(clone, error);
924cec47e3dSKiyoshi Ueda }
925cec47e3dSKiyoshi Ueda 
92656a67df7SMike Snitzer /*
92756a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
92856a67df7SMike Snitzer  * target boundary.
92956a67df7SMike Snitzer  */
93056a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
9311da177e4SLinus Torvalds {
93256a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
93356a67df7SMike Snitzer 
93456a67df7SMike Snitzer 	return ti->len - target_offset;
93556a67df7SMike Snitzer }
93656a67df7SMike Snitzer 
93756a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
93856a67df7SMike Snitzer {
93956a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
940542f9038SMike Snitzer 	sector_t offset, max_len;
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 	/*
9431da177e4SLinus Torvalds 	 * Does the target need to split even further?
9441da177e4SLinus Torvalds 	 */
945542f9038SMike Snitzer 	if (ti->max_io_len) {
946542f9038SMike Snitzer 		offset = dm_target_offset(ti, sector);
947542f9038SMike Snitzer 		if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
948542f9038SMike Snitzer 			max_len = sector_div(offset, ti->max_io_len);
949542f9038SMike Snitzer 		else
950542f9038SMike Snitzer 			max_len = offset & (ti->max_io_len - 1);
951542f9038SMike Snitzer 		max_len = ti->max_io_len - max_len;
952542f9038SMike Snitzer 
953542f9038SMike Snitzer 		if (len > max_len)
954542f9038SMike Snitzer 			len = max_len;
9551da177e4SLinus Torvalds 	}
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	return len;
9581da177e4SLinus Torvalds }
9591da177e4SLinus Torvalds 
960542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
961542f9038SMike Snitzer {
962542f9038SMike Snitzer 	if (len > UINT_MAX) {
963542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
964542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
965542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
966542f9038SMike Snitzer 		return -EINVAL;
967542f9038SMike Snitzer 	}
968542f9038SMike Snitzer 
969542f9038SMike Snitzer 	ti->max_io_len = (uint32_t) len;
970542f9038SMike Snitzer 
971542f9038SMike Snitzer 	return 0;
972542f9038SMike Snitzer }
973542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
974542f9038SMike Snitzer 
975bd2a49b8SAlasdair G Kergon static void __map_bio(struct dm_target_io *tio)
9761da177e4SLinus Torvalds {
9771da177e4SLinus Torvalds 	int r;
9782056a782SJens Axboe 	sector_t sector;
9799faf400fSStefan Bader 	struct mapped_device *md;
980dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
981bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
9841da177e4SLinus Torvalds 	clone->bi_private = tio;
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 	/*
9871da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
9881da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
9891da177e4SLinus Torvalds 	 * this io.
9901da177e4SLinus Torvalds 	 */
9911da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
9922056a782SJens Axboe 	sector = clone->bi_sector;
9937de3ee57SMikulas Patocka 	r = ti->type->map(ti, clone);
99445cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
9951da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
9962056a782SJens Axboe 
997d07335e5SMike Snitzer 		trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
99822a7c31aSAlan D. Brunelle 				      tio->io->bio->bi_bdev->bd_dev, sector);
9992056a782SJens Axboe 
10001da177e4SLinus Torvalds 		generic_make_request(clone);
10012e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
10022e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
10039faf400fSStefan Bader 		md = tio->io->md;
10049faf400fSStefan Bader 		dec_pending(tio->io, r);
10059faf400fSStefan Bader 		free_tio(md, tio);
100645cbcd79SKiyoshi Ueda 	} else if (r) {
100745cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
100845cbcd79SKiyoshi Ueda 		BUG();
10091da177e4SLinus Torvalds 	}
10101da177e4SLinus Torvalds }
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds struct clone_info {
10131da177e4SLinus Torvalds 	struct mapped_device *md;
10141da177e4SLinus Torvalds 	struct dm_table *map;
10151da177e4SLinus Torvalds 	struct bio *bio;
10161da177e4SLinus Torvalds 	struct dm_io *io;
10171da177e4SLinus Torvalds 	sector_t sector;
10181da177e4SLinus Torvalds 	sector_t sector_count;
10191da177e4SLinus Torvalds 	unsigned short idx;
10201da177e4SLinus Torvalds };
10211da177e4SLinus Torvalds 
1022bd2a49b8SAlasdair G Kergon static void bio_setup_sector(struct bio *bio, sector_t sector, sector_t len)
1023bd2a49b8SAlasdair G Kergon {
1024bd2a49b8SAlasdair G Kergon 	bio->bi_sector = sector;
1025bd2a49b8SAlasdair G Kergon 	bio->bi_size = to_bytes(len);
1026bd2a49b8SAlasdair G Kergon }
1027bd2a49b8SAlasdair G Kergon 
1028bd2a49b8SAlasdair G Kergon static void bio_setup_bv(struct bio *bio, unsigned short idx, unsigned short bv_count)
1029bd2a49b8SAlasdair G Kergon {
1030bd2a49b8SAlasdair G Kergon 	bio->bi_idx = idx;
1031bd2a49b8SAlasdair G Kergon 	bio->bi_vcnt = idx + bv_count;
1032bd2a49b8SAlasdair G Kergon 	bio->bi_flags &= ~(1 << BIO_SEG_VALID);
1033bd2a49b8SAlasdair G Kergon }
1034bd2a49b8SAlasdair G Kergon 
1035bd2a49b8SAlasdair G Kergon static void clone_bio_integrity(struct bio *bio, struct bio *clone,
1036bd2a49b8SAlasdair G Kergon 				unsigned short idx, unsigned len, unsigned offset,
1037bd2a49b8SAlasdair G Kergon 				unsigned trim)
1038bd2a49b8SAlasdair G Kergon {
1039bd2a49b8SAlasdair G Kergon 	if (!bio_integrity(bio))
1040bd2a49b8SAlasdair G Kergon 		return;
1041bd2a49b8SAlasdair G Kergon 
1042bd2a49b8SAlasdair G Kergon 	bio_integrity_clone(clone, bio, GFP_NOIO);
1043bd2a49b8SAlasdair G Kergon 
1044bd2a49b8SAlasdair G Kergon 	if (trim)
1045bd2a49b8SAlasdair G Kergon 		bio_integrity_trim(clone, bio_sector_offset(bio, idx, offset), len);
1046bd2a49b8SAlasdair G Kergon }
1047bd2a49b8SAlasdair G Kergon 
10481da177e4SLinus Torvalds /*
1049d87f4c14STejun Heo  * Creates a little bio that just does part of a bvec.
10501da177e4SLinus Torvalds  */
105114fe594dSAlasdair G Kergon static void clone_split_bio(struct dm_target_io *tio, struct bio *bio,
105214fe594dSAlasdair G Kergon 			    sector_t sector, unsigned short idx,
105314fe594dSAlasdair G Kergon 			    unsigned offset, unsigned len)
10541da177e4SLinus Torvalds {
1055dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
10561da177e4SLinus Torvalds 	struct bio_vec *bv = bio->bi_io_vec + idx;
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 	*clone->bi_io_vec = *bv;
10591da177e4SLinus Torvalds 
1060bd2a49b8SAlasdair G Kergon 	bio_setup_sector(clone, sector, len);
1061bd2a49b8SAlasdair G Kergon 
10621da177e4SLinus Torvalds 	clone->bi_bdev = bio->bi_bdev;
1063d87f4c14STejun Heo 	clone->bi_rw = bio->bi_rw;
10641da177e4SLinus Torvalds 	clone->bi_vcnt = 1;
10651da177e4SLinus Torvalds 	clone->bi_io_vec->bv_offset = offset;
10661da177e4SLinus Torvalds 	clone->bi_io_vec->bv_len = clone->bi_size;
1067f3e1d26eSMartin K. Petersen 	clone->bi_flags |= 1 << BIO_CLONED;
10681da177e4SLinus Torvalds 
1069bd2a49b8SAlasdair G Kergon 	clone_bio_integrity(bio, clone, idx, len, offset, 1);
10701da177e4SLinus Torvalds }
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds /*
10731da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
10741da177e4SLinus Torvalds  */
1075dba14160SMikulas Patocka static void clone_bio(struct dm_target_io *tio, struct bio *bio,
1076dba14160SMikulas Patocka 		      sector_t sector, unsigned short idx,
1077e4c93811SAlasdair G Kergon 		      unsigned short bv_count, unsigned len)
10781da177e4SLinus Torvalds {
1079dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
1080bd2a49b8SAlasdair G Kergon 	unsigned trim = 0;
10811da177e4SLinus Torvalds 
10829faf400fSStefan Bader 	__bio_clone(clone, bio);
1083bd2a49b8SAlasdair G Kergon 	bio_setup_sector(clone, sector, len);
1084bd2a49b8SAlasdair G Kergon 	bio_setup_bv(clone, idx, bv_count);
10859c47008dSMartin K. Petersen 
10869c47008dSMartin K. Petersen 	if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1087bd2a49b8SAlasdair G Kergon 		trim = 1;
1088bd2a49b8SAlasdair G Kergon 	clone_bio_integrity(bio, clone, idx, len, 0, trim);
10891da177e4SLinus Torvalds }
10901da177e4SLinus Torvalds 
10919015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci,
1092bd2a49b8SAlasdair G Kergon 				      struct dm_target *ti, int nr_iovecs,
109355a62eefSAlasdair G Kergon 				      unsigned target_bio_nr)
1094f9ab94ceSMikulas Patocka {
1095dba14160SMikulas Patocka 	struct dm_target_io *tio;
1096dba14160SMikulas Patocka 	struct bio *clone;
1097dba14160SMikulas Patocka 
1098dba14160SMikulas Patocka 	clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, ci->md->bs);
1099dba14160SMikulas Patocka 	tio = container_of(clone, struct dm_target_io, clone);
1100f9ab94ceSMikulas Patocka 
1101f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1102f9ab94ceSMikulas Patocka 	tio->ti = ti;
1103f9ab94ceSMikulas Patocka 	memset(&tio->info, 0, sizeof(tio->info));
110455a62eefSAlasdair G Kergon 	tio->target_bio_nr = target_bio_nr;
11059015df24SAlasdair G Kergon 
11069015df24SAlasdair G Kergon 	return tio;
11079015df24SAlasdair G Kergon }
11089015df24SAlasdair G Kergon 
110914fe594dSAlasdair G Kergon static void __clone_and_map_simple_bio(struct clone_info *ci,
111014fe594dSAlasdair G Kergon 				       struct dm_target *ti,
111155a62eefSAlasdair G Kergon 				       unsigned target_bio_nr, sector_t len)
11129015df24SAlasdair G Kergon {
111355a62eefSAlasdair G Kergon 	struct dm_target_io *tio = alloc_tio(ci, ti, ci->bio->bi_max_vecs, target_bio_nr);
1114dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
11159015df24SAlasdair G Kergon 
111606a426ceSMike Snitzer 	/*
111706a426ceSMike Snitzer 	 * Discard requests require the bio's inline iovecs be initialized.
111806a426ceSMike Snitzer 	 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
111906a426ceSMike Snitzer 	 * and discard, so no need for concern about wasted bvec allocations.
112006a426ceSMike Snitzer 	 */
1121dba14160SMikulas Patocka 	 __bio_clone(clone, ci->bio);
1122bd2a49b8SAlasdair G Kergon 	if (len)
1123bd2a49b8SAlasdair G Kergon 		bio_setup_sector(clone, ci->sector, len);
1124f9ab94ceSMikulas Patocka 
1125bd2a49b8SAlasdair G Kergon 	__map_bio(tio);
1126f9ab94ceSMikulas Patocka }
1127f9ab94ceSMikulas Patocka 
112814fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
112955a62eefSAlasdair G Kergon 				  unsigned num_bios, sector_t len)
113006a426ceSMike Snitzer {
113155a62eefSAlasdair G Kergon 	unsigned target_bio_nr;
113206a426ceSMike Snitzer 
113355a62eefSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
113414fe594dSAlasdair G Kergon 		__clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
113506a426ceSMike Snitzer }
113606a426ceSMike Snitzer 
113714fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1138f9ab94ceSMikulas Patocka {
113906a426ceSMike Snitzer 	unsigned target_nr = 0;
1140f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1141f9ab94ceSMikulas Patocka 
1142b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1143f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
114414fe594dSAlasdair G Kergon 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, 0);
1145f9ab94ceSMikulas Patocka 
1146f9ab94ceSMikulas Patocka 	return 0;
1147f9ab94ceSMikulas Patocka }
1148f9ab94ceSMikulas Patocka 
1149e4c93811SAlasdair G Kergon static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
1150e4c93811SAlasdair G Kergon 				     sector_t sector, int nr_iovecs,
1151e4c93811SAlasdair G Kergon 				     unsigned short idx, unsigned short bv_count,
1152e4c93811SAlasdair G Kergon 				     unsigned offset, unsigned len,
1153e4c93811SAlasdair G Kergon 				     unsigned split_bvec)
11545ae89a87SMike Snitzer {
1155dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
11565ae89a87SMike Snitzer 	struct dm_target_io *tio;
11575ae89a87SMike Snitzer 
1158e4c93811SAlasdair G Kergon 	tio = alloc_tio(ci, ti, nr_iovecs, 0);
1159e4c93811SAlasdair G Kergon 
1160e4c93811SAlasdair G Kergon 	if (split_bvec)
1161e4c93811SAlasdair G Kergon 		clone_split_bio(tio, bio, sector, idx, offset, len);
1162e4c93811SAlasdair G Kergon 	else
1163e4c93811SAlasdair G Kergon 		clone_bio(tio, bio, sector, idx, bv_count, len);
1164e4c93811SAlasdair G Kergon 
1165bd2a49b8SAlasdair G Kergon 	__map_bio(tio);
11665ae89a87SMike Snitzer }
11675ae89a87SMike Snitzer 
116855a62eefSAlasdair G Kergon typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
116923508a96SMike Snitzer 
117055a62eefSAlasdair G Kergon static unsigned get_num_discard_bios(struct dm_target *ti)
117123508a96SMike Snitzer {
117255a62eefSAlasdair G Kergon 	return ti->num_discard_bios;
117323508a96SMike Snitzer }
117423508a96SMike Snitzer 
117555a62eefSAlasdair G Kergon static unsigned get_num_write_same_bios(struct dm_target *ti)
117623508a96SMike Snitzer {
117755a62eefSAlasdair G Kergon 	return ti->num_write_same_bios;
117823508a96SMike Snitzer }
117923508a96SMike Snitzer 
118023508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti);
118123508a96SMike Snitzer 
118223508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti)
118323508a96SMike Snitzer {
118455a62eefSAlasdair G Kergon 	return ti->split_discard_bios;
118523508a96SMike Snitzer }
118623508a96SMike Snitzer 
118714fe594dSAlasdair G Kergon static int __send_changing_extent_only(struct clone_info *ci,
118855a62eefSAlasdair G Kergon 				       get_num_bios_fn get_num_bios,
118923508a96SMike Snitzer 				       is_split_required_fn is_split_required)
11905ae89a87SMike Snitzer {
11915ae89a87SMike Snitzer 	struct dm_target *ti;
1192a79245b3SMike Snitzer 	sector_t len;
119355a62eefSAlasdair G Kergon 	unsigned num_bios;
11945ae89a87SMike Snitzer 
1195a79245b3SMike Snitzer 	do {
11965ae89a87SMike Snitzer 		ti = dm_table_find_target(ci->map, ci->sector);
11975ae89a87SMike Snitzer 		if (!dm_target_is_valid(ti))
11985ae89a87SMike Snitzer 			return -EIO;
11995ae89a87SMike Snitzer 
12005ae89a87SMike Snitzer 		/*
120123508a96SMike Snitzer 		 * Even though the device advertised support for this type of
120223508a96SMike Snitzer 		 * request, that does not mean every target supports it, and
1203936688d7SMike Snitzer 		 * reconfiguration might also have changed that since the
12045ae89a87SMike Snitzer 		 * check was performed.
12055ae89a87SMike Snitzer 		 */
120655a62eefSAlasdair G Kergon 		num_bios = get_num_bios ? get_num_bios(ti) : 0;
120755a62eefSAlasdair G Kergon 		if (!num_bios)
12085ae89a87SMike Snitzer 			return -EOPNOTSUPP;
12095ae89a87SMike Snitzer 
121023508a96SMike Snitzer 		if (is_split_required && !is_split_required(ti))
1211a79245b3SMike Snitzer 			len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
12127acf0277SMikulas Patocka 		else
12137acf0277SMikulas Patocka 			len = min(ci->sector_count, max_io_len(ci->sector, ti));
12145ae89a87SMike Snitzer 
121514fe594dSAlasdair G Kergon 		__send_duplicate_bios(ci, ti, num_bios, len);
12165ae89a87SMike Snitzer 
1217a79245b3SMike Snitzer 		ci->sector += len;
1218a79245b3SMike Snitzer 	} while (ci->sector_count -= len);
12195ae89a87SMike Snitzer 
12205ae89a87SMike Snitzer 	return 0;
12215ae89a87SMike Snitzer }
12225ae89a87SMike Snitzer 
122314fe594dSAlasdair G Kergon static int __send_discard(struct clone_info *ci)
122423508a96SMike Snitzer {
122514fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_discard_bios,
122623508a96SMike Snitzer 					   is_split_required_for_discard);
122723508a96SMike Snitzer }
122823508a96SMike Snitzer 
122914fe594dSAlasdair G Kergon static int __send_write_same(struct clone_info *ci)
123023508a96SMike Snitzer {
123114fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
123223508a96SMike Snitzer }
123323508a96SMike Snitzer 
1234e4c93811SAlasdair G Kergon /*
1235e4c93811SAlasdair G Kergon  * Find maximum number of sectors / bvecs we can process with a single bio.
1236e4c93811SAlasdair G Kergon  */
1237e4c93811SAlasdair G Kergon static sector_t __len_within_target(struct clone_info *ci, sector_t max, int *idx)
12381da177e4SLinus Torvalds {
1239dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
1240e4c93811SAlasdair G Kergon 	sector_t bv_len, total_len = 0;
12411da177e4SLinus Torvalds 
1242e4c93811SAlasdair G Kergon 	for (*idx = ci->idx; max && (*idx < bio->bi_vcnt); (*idx)++) {
1243e4c93811SAlasdair G Kergon 		bv_len = to_sector(bio->bi_io_vec[*idx].bv_len);
12445ae89a87SMike Snitzer 
1245e4c93811SAlasdair G Kergon 		if (bv_len > max)
12461da177e4SLinus Torvalds 			break;
12471da177e4SLinus Torvalds 
1248e4c93811SAlasdair G Kergon 		max -= bv_len;
1249e4c93811SAlasdair G Kergon 		total_len += bv_len;
12501da177e4SLinus Torvalds 	}
12511da177e4SLinus Torvalds 
1252e4c93811SAlasdair G Kergon 	return total_len;
1253e4c93811SAlasdair G Kergon }
12541da177e4SLinus Torvalds 
1255e4c93811SAlasdair G Kergon static int __split_bvec_across_targets(struct clone_info *ci,
1256e4c93811SAlasdair G Kergon 				       struct dm_target *ti, sector_t max)
1257e4c93811SAlasdair G Kergon {
1258e4c93811SAlasdair G Kergon 	struct bio *bio = ci->bio;
12591da177e4SLinus Torvalds 	struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1260d2044a94SAlasdair G Kergon 	sector_t remaining = to_sector(bv->bv_len);
1261e4c93811SAlasdair G Kergon 	unsigned offset = 0;
1262e4c93811SAlasdair G Kergon 	sector_t len;
12631da177e4SLinus Torvalds 
1264d2044a94SAlasdair G Kergon 	do {
1265d2044a94SAlasdair G Kergon 		if (offset) {
12661da177e4SLinus Torvalds 			ti = dm_table_find_target(ci->map, ci->sector);
1267512875bdSJun'ichi Nomura 			if (!dm_target_is_valid(ti))
1268512875bdSJun'ichi Nomura 				return -EIO;
1269512875bdSJun'ichi Nomura 
127056a67df7SMike Snitzer 			max = max_io_len(ci->sector, ti);
1271d2044a94SAlasdair G Kergon 		}
1272d2044a94SAlasdair G Kergon 
1273d2044a94SAlasdair G Kergon 		len = min(remaining, max);
1274d2044a94SAlasdair G Kergon 
1275e4c93811SAlasdair G Kergon 		__clone_and_map_data_bio(ci, ti, ci->sector, 1, ci->idx, 0,
1276e4c93811SAlasdair G Kergon 					 bv->bv_offset + offset, len, 1);
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 		ci->sector += len;
12791da177e4SLinus Torvalds 		ci->sector_count -= len;
1280d2044a94SAlasdair G Kergon 		offset += to_bytes(len);
1281d2044a94SAlasdair G Kergon 	} while (remaining -= len);
1282d2044a94SAlasdair G Kergon 
12831da177e4SLinus Torvalds 	ci->idx++;
1284512875bdSJun'ichi Nomura 
1285512875bdSJun'ichi Nomura 	return 0;
12861da177e4SLinus Torvalds }
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds /*
1289e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1290e4c93811SAlasdair G Kergon  */
1291e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci)
1292e4c93811SAlasdair G Kergon {
1293e4c93811SAlasdair G Kergon 	struct bio *bio = ci->bio;
1294e4c93811SAlasdair G Kergon 	struct dm_target *ti;
1295e4c93811SAlasdair G Kergon 	sector_t len, max;
1296e4c93811SAlasdair G Kergon 	int idx;
1297e4c93811SAlasdair G Kergon 
1298e4c93811SAlasdair G Kergon 	if (unlikely(bio->bi_rw & REQ_DISCARD))
1299e4c93811SAlasdair G Kergon 		return __send_discard(ci);
1300e4c93811SAlasdair G Kergon 	else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
1301e4c93811SAlasdair G Kergon 		return __send_write_same(ci);
1302e4c93811SAlasdair G Kergon 
1303e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1304e4c93811SAlasdair G Kergon 	if (!dm_target_is_valid(ti))
1305e4c93811SAlasdair G Kergon 		return -EIO;
1306e4c93811SAlasdair G Kergon 
1307e4c93811SAlasdair G Kergon 	max = max_io_len(ci->sector, ti);
1308e4c93811SAlasdair G Kergon 
1309e4c93811SAlasdair G Kergon 	/*
1310e4c93811SAlasdair G Kergon 	 * Optimise for the simple case where we can do all of
1311e4c93811SAlasdair G Kergon 	 * the remaining io with a single clone.
1312e4c93811SAlasdair G Kergon 	 */
1313e4c93811SAlasdair G Kergon 	if (ci->sector_count <= max) {
1314e4c93811SAlasdair G Kergon 		__clone_and_map_data_bio(ci, ti, ci->sector, bio->bi_max_vecs,
1315e4c93811SAlasdair G Kergon 					 ci->idx, bio->bi_vcnt - ci->idx, 0,
1316e4c93811SAlasdair G Kergon 					 ci->sector_count, 0);
1317e4c93811SAlasdair G Kergon 		ci->sector_count = 0;
1318e4c93811SAlasdair G Kergon 		return 0;
1319e4c93811SAlasdair G Kergon 	}
1320e4c93811SAlasdair G Kergon 
1321e4c93811SAlasdair G Kergon 	/*
1322e4c93811SAlasdair G Kergon 	 * There are some bvecs that don't span targets.
1323e4c93811SAlasdair G Kergon 	 * Do as many of these as possible.
1324e4c93811SAlasdair G Kergon 	 */
1325e4c93811SAlasdair G Kergon 	if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1326e4c93811SAlasdair G Kergon 		len = __len_within_target(ci, max, &idx);
1327e4c93811SAlasdair G Kergon 
1328e4c93811SAlasdair G Kergon 		__clone_and_map_data_bio(ci, ti, ci->sector, bio->bi_max_vecs,
1329e4c93811SAlasdair G Kergon 					 ci->idx, idx - ci->idx, 0, len, 0);
1330e4c93811SAlasdair G Kergon 
1331e4c93811SAlasdair G Kergon 		ci->sector += len;
1332e4c93811SAlasdair G Kergon 		ci->sector_count -= len;
1333e4c93811SAlasdair G Kergon 		ci->idx = idx;
1334e4c93811SAlasdair G Kergon 
1335e4c93811SAlasdair G Kergon 		return 0;
1336e4c93811SAlasdair G Kergon 	}
1337e4c93811SAlasdair G Kergon 
1338e4c93811SAlasdair G Kergon 	/*
1339e4c93811SAlasdair G Kergon 	 * Handle a bvec that must be split between two or more targets.
1340e4c93811SAlasdair G Kergon 	 */
1341e4c93811SAlasdair G Kergon 	return __split_bvec_across_targets(ci, ti, max);
1342e4c93811SAlasdair G Kergon }
1343e4c93811SAlasdair G Kergon 
1344e4c93811SAlasdair G Kergon /*
134514fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
13461da177e4SLinus Torvalds  */
1347f0b9a450SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
13481da177e4SLinus Torvalds {
13491da177e4SLinus Torvalds 	struct clone_info ci;
1350512875bdSJun'ichi Nomura 	int error = 0;
13511da177e4SLinus Torvalds 
13527c666411SAlasdair G Kergon 	ci.map = dm_get_live_table(md);
1353f0b9a450SMikulas Patocka 	if (unlikely(!ci.map)) {
1354f0b9a450SMikulas Patocka 		bio_io_error(bio);
1355f0b9a450SMikulas Patocka 		return;
1356f0b9a450SMikulas Patocka 	}
1357692d0eb9SMikulas Patocka 
13581da177e4SLinus Torvalds 	ci.md = md;
13591da177e4SLinus Torvalds 	ci.io = alloc_io(md);
13601da177e4SLinus Torvalds 	ci.io->error = 0;
13611da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
13621da177e4SLinus Torvalds 	ci.io->bio = bio;
13631da177e4SLinus Torvalds 	ci.io->md = md;
1364f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
13651da177e4SLinus Torvalds 	ci.sector = bio->bi_sector;
13661da177e4SLinus Torvalds 	ci.idx = bio->bi_idx;
13671da177e4SLinus Torvalds 
13683eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1369bd2a49b8SAlasdair G Kergon 
1370b372d360SMike Snitzer 	if (bio->bi_rw & REQ_FLUSH) {
1371b372d360SMike Snitzer 		ci.bio = &ci.md->flush_bio;
1372b372d360SMike Snitzer 		ci.sector_count = 0;
137314fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1374b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1375b372d360SMike Snitzer 	} else {
13766a8736d1STejun Heo 		ci.bio = bio;
1377f6fccb12SMilan Broz 		ci.sector_count = bio_sectors(bio);
1378512875bdSJun'ichi Nomura 		while (ci.sector_count && !error)
137914fe594dSAlasdair G Kergon 			error = __split_and_process_non_flush(&ci);
1380d87f4c14STejun Heo 	}
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 	/* drop the extra reference count */
1383512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
13841da177e4SLinus Torvalds 	dm_table_put(ci.map);
13859e4e5f87SMilan Broz }
13869e4e5f87SMilan Broz /*-----------------------------------------------------------------
13871da177e4SLinus Torvalds  * CRUD END
13881da177e4SLinus Torvalds  *---------------------------------------------------------------*/
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds static int dm_merge_bvec(struct request_queue *q,
13911da177e4SLinus Torvalds 			 struct bvec_merge_data *bvm,
1392f6fccb12SMilan Broz 			 struct bio_vec *biovec)
1393f6fccb12SMilan Broz {
1394f6fccb12SMilan Broz 	struct mapped_device *md = q->queuedata;
13957c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1396f6fccb12SMilan Broz 	struct dm_target *ti;
1397f6fccb12SMilan Broz 	sector_t max_sectors;
1398f6fccb12SMilan Broz 	int max_size = 0;
1399f6fccb12SMilan Broz 
1400f6fccb12SMilan Broz 	if (unlikely(!map))
1401f6fccb12SMilan Broz 		goto out;
1402f6fccb12SMilan Broz 
1403f6fccb12SMilan Broz 	ti = dm_table_find_target(map, bvm->bi_sector);
1404f6fccb12SMilan Broz 	if (!dm_target_is_valid(ti))
1405f6fccb12SMilan Broz 		goto out_table;
1406f6fccb12SMilan Broz 
1407f6fccb12SMilan Broz 	/*
1408f6fccb12SMilan Broz 	 * Find maximum amount of I/O that won't need splitting
1409f6fccb12SMilan Broz 	 */
141056a67df7SMike Snitzer 	max_sectors = min(max_io_len(bvm->bi_sector, ti),
1411f6fccb12SMilan Broz 			  (sector_t) BIO_MAX_SECTORS);
1412f6fccb12SMilan Broz 	max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1413f6fccb12SMilan Broz 	if (max_size < 0)
1414f6fccb12SMilan Broz 		max_size = 0;
1415f6fccb12SMilan Broz 
1416f6fccb12SMilan Broz 	/*
1417f6fccb12SMilan Broz 	 * merge_bvec_fn() returns number of bytes
1418f6fccb12SMilan Broz 	 * it can accept at this offset
1419f6fccb12SMilan Broz 	 * max is precomputed maximal io size
1420f6fccb12SMilan Broz 	 */
1421f6fccb12SMilan Broz 	if (max_size && ti->type->merge)
1422f6fccb12SMilan Broz 		max_size = ti->type->merge(ti, bvm, biovec, max_size);
14238cbeb67aSMikulas Patocka 	/*
14248cbeb67aSMikulas Patocka 	 * If the target doesn't support merge method and some of the devices
14258cbeb67aSMikulas Patocka 	 * provided their merge_bvec method (we know this by looking at
14268cbeb67aSMikulas Patocka 	 * queue_max_hw_sectors), then we can't allow bios with multiple vector
14278cbeb67aSMikulas Patocka 	 * entries.  So always set max_size to 0, and the code below allows
14288cbeb67aSMikulas Patocka 	 * just one page.
14298cbeb67aSMikulas Patocka 	 */
14308cbeb67aSMikulas Patocka 	else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
14318cbeb67aSMikulas Patocka 
14328cbeb67aSMikulas Patocka 		max_size = 0;
1433f6fccb12SMilan Broz 
1434b01cd5acSMikulas Patocka out_table:
14355037108aSMikulas Patocka 	dm_table_put(map);
14365037108aSMikulas Patocka 
14375037108aSMikulas Patocka out:
1438f6fccb12SMilan Broz 	/*
1439f6fccb12SMilan Broz 	 * Always allow an entire first page
1440f6fccb12SMilan Broz 	 */
1441f6fccb12SMilan Broz 	if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1442f6fccb12SMilan Broz 		max_size = biovec->bv_len;
1443f6fccb12SMilan Broz 
1444f6fccb12SMilan Broz 	return max_size;
1445f6fccb12SMilan Broz }
1446f6fccb12SMilan Broz 
14471da177e4SLinus Torvalds /*
14481da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
14491da177e4SLinus Torvalds  * dm_merge_bvec.
14501da177e4SLinus Torvalds  */
14515a7bbad2SChristoph Hellwig static void _dm_request(struct request_queue *q, struct bio *bio)
14521da177e4SLinus Torvalds {
145312f03a49SKevin Corry 	int rw = bio_data_dir(bio);
14541da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
1455c9959059STejun Heo 	int cpu;
14561da177e4SLinus Torvalds 
14572ca3310eSAlasdair G Kergon 	down_read(&md->io_lock);
14581da177e4SLinus Torvalds 
1459074a7acaSTejun Heo 	cpu = part_stat_lock();
1460074a7acaSTejun Heo 	part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1461074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1462074a7acaSTejun Heo 	part_stat_unlock();
146312f03a49SKevin Corry 
14646a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
14656a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
14662ca3310eSAlasdair G Kergon 		up_read(&md->io_lock);
14671da177e4SLinus Torvalds 
14686a8736d1STejun Heo 		if (bio_rw(bio) != READA)
146992c63902SMikulas Patocka 			queue_io(md, bio);
14706a8736d1STejun Heo 		else
14716a8736d1STejun Heo 			bio_io_error(bio);
14725a7bbad2SChristoph Hellwig 		return;
14731da177e4SLinus Torvalds 	}
14741da177e4SLinus Torvalds 
1475f0b9a450SMikulas Patocka 	__split_and_process_bio(md, bio);
14762ca3310eSAlasdair G Kergon 	up_read(&md->io_lock);
14775a7bbad2SChristoph Hellwig 	return;
1478cec47e3dSKiyoshi Ueda }
1479cec47e3dSKiyoshi Ueda 
1480cec47e3dSKiyoshi Ueda static int dm_request_based(struct mapped_device *md)
1481cec47e3dSKiyoshi Ueda {
1482cec47e3dSKiyoshi Ueda 	return blk_queue_stackable(md->queue);
1483cec47e3dSKiyoshi Ueda }
1484cec47e3dSKiyoshi Ueda 
14855a7bbad2SChristoph Hellwig static void dm_request(struct request_queue *q, struct bio *bio)
1486cec47e3dSKiyoshi Ueda {
1487cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1488cec47e3dSKiyoshi Ueda 
1489cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
14905a7bbad2SChristoph Hellwig 		blk_queue_bio(q, bio);
14915a7bbad2SChristoph Hellwig 	else
14925a7bbad2SChristoph Hellwig 		_dm_request(q, bio);
1493cec47e3dSKiyoshi Ueda }
1494cec47e3dSKiyoshi Ueda 
1495cec47e3dSKiyoshi Ueda void dm_dispatch_request(struct request *rq)
1496cec47e3dSKiyoshi Ueda {
1497cec47e3dSKiyoshi Ueda 	int r;
1498cec47e3dSKiyoshi Ueda 
1499cec47e3dSKiyoshi Ueda 	if (blk_queue_io_stat(rq->q))
1500cec47e3dSKiyoshi Ueda 		rq->cmd_flags |= REQ_IO_STAT;
1501cec47e3dSKiyoshi Ueda 
1502cec47e3dSKiyoshi Ueda 	rq->start_time = jiffies;
1503cec47e3dSKiyoshi Ueda 	r = blk_insert_cloned_request(rq->q, rq);
1504cec47e3dSKiyoshi Ueda 	if (r)
1505cec47e3dSKiyoshi Ueda 		dm_complete_request(rq, r);
1506cec47e3dSKiyoshi Ueda }
1507cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_dispatch_request);
1508cec47e3dSKiyoshi Ueda 
1509cec47e3dSKiyoshi Ueda static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1510cec47e3dSKiyoshi Ueda 				 void *data)
1511cec47e3dSKiyoshi Ueda {
1512cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = data;
151394818742SKent Overstreet 	struct dm_rq_clone_bio_info *info =
151494818742SKent Overstreet 		container_of(bio, struct dm_rq_clone_bio_info, clone);
1515cec47e3dSKiyoshi Ueda 
1516cec47e3dSKiyoshi Ueda 	info->orig = bio_orig;
1517cec47e3dSKiyoshi Ueda 	info->tio = tio;
1518cec47e3dSKiyoshi Ueda 	bio->bi_end_io = end_clone_bio;
1519cec47e3dSKiyoshi Ueda 	bio->bi_private = info;
1520cec47e3dSKiyoshi Ueda 
1521cec47e3dSKiyoshi Ueda 	return 0;
1522cec47e3dSKiyoshi Ueda }
1523cec47e3dSKiyoshi Ueda 
1524cec47e3dSKiyoshi Ueda static int setup_clone(struct request *clone, struct request *rq,
1525cec47e3dSKiyoshi Ueda 		       struct dm_rq_target_io *tio)
1526cec47e3dSKiyoshi Ueda {
1527d0bcb878SKiyoshi Ueda 	int r;
1528cec47e3dSKiyoshi Ueda 
1529d0bcb878SKiyoshi Ueda 	r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1530d0bcb878SKiyoshi Ueda 			      dm_rq_bio_constructor, tio);
1531cec47e3dSKiyoshi Ueda 	if (r)
1532cec47e3dSKiyoshi Ueda 		return r;
1533cec47e3dSKiyoshi Ueda 
1534cec47e3dSKiyoshi Ueda 	clone->cmd = rq->cmd;
1535cec47e3dSKiyoshi Ueda 	clone->cmd_len = rq->cmd_len;
1536cec47e3dSKiyoshi Ueda 	clone->sense = rq->sense;
1537cec47e3dSKiyoshi Ueda 	clone->buffer = rq->buffer;
1538cec47e3dSKiyoshi Ueda 	clone->end_io = end_clone_request;
1539cec47e3dSKiyoshi Ueda 	clone->end_io_data = tio;
1540cec47e3dSKiyoshi Ueda 
1541cec47e3dSKiyoshi Ueda 	return 0;
1542cec47e3dSKiyoshi Ueda }
1543cec47e3dSKiyoshi Ueda 
15446facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md,
15456facdaffSKiyoshi Ueda 				gfp_t gfp_mask)
15466facdaffSKiyoshi Ueda {
15476facdaffSKiyoshi Ueda 	struct request *clone;
15486facdaffSKiyoshi Ueda 	struct dm_rq_target_io *tio;
15496facdaffSKiyoshi Ueda 
15506facdaffSKiyoshi Ueda 	tio = alloc_rq_tio(md, gfp_mask);
15516facdaffSKiyoshi Ueda 	if (!tio)
15526facdaffSKiyoshi Ueda 		return NULL;
15536facdaffSKiyoshi Ueda 
15546facdaffSKiyoshi Ueda 	tio->md = md;
15556facdaffSKiyoshi Ueda 	tio->ti = NULL;
15566facdaffSKiyoshi Ueda 	tio->orig = rq;
15576facdaffSKiyoshi Ueda 	tio->error = 0;
15586facdaffSKiyoshi Ueda 	memset(&tio->info, 0, sizeof(tio->info));
15596facdaffSKiyoshi Ueda 
15606facdaffSKiyoshi Ueda 	clone = &tio->clone;
15616facdaffSKiyoshi Ueda 	if (setup_clone(clone, rq, tio)) {
15626facdaffSKiyoshi Ueda 		/* -ENOMEM */
15636facdaffSKiyoshi Ueda 		free_rq_tio(tio);
15646facdaffSKiyoshi Ueda 		return NULL;
15656facdaffSKiyoshi Ueda 	}
15666facdaffSKiyoshi Ueda 
15676facdaffSKiyoshi Ueda 	return clone;
15686facdaffSKiyoshi Ueda }
15696facdaffSKiyoshi Ueda 
1570cec47e3dSKiyoshi Ueda /*
1571cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1572cec47e3dSKiyoshi Ueda  */
1573cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq)
1574cec47e3dSKiyoshi Ueda {
1575cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1576cec47e3dSKiyoshi Ueda 	struct request *clone;
1577cec47e3dSKiyoshi Ueda 
1578cec47e3dSKiyoshi Ueda 	if (unlikely(rq->special)) {
1579cec47e3dSKiyoshi Ueda 		DMWARN("Already has something in rq->special.");
1580cec47e3dSKiyoshi Ueda 		return BLKPREP_KILL;
1581cec47e3dSKiyoshi Ueda 	}
1582cec47e3dSKiyoshi Ueda 
15836facdaffSKiyoshi Ueda 	clone = clone_rq(rq, md, GFP_ATOMIC);
15846facdaffSKiyoshi Ueda 	if (!clone)
1585cec47e3dSKiyoshi Ueda 		return BLKPREP_DEFER;
1586cec47e3dSKiyoshi Ueda 
1587cec47e3dSKiyoshi Ueda 	rq->special = clone;
1588cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_DONTPREP;
1589cec47e3dSKiyoshi Ueda 
1590cec47e3dSKiyoshi Ueda 	return BLKPREP_OK;
1591cec47e3dSKiyoshi Ueda }
1592cec47e3dSKiyoshi Ueda 
15939eef87daSKiyoshi Ueda /*
15949eef87daSKiyoshi Ueda  * Returns:
15959eef87daSKiyoshi Ueda  * 0  : the request has been processed (not requeued)
15969eef87daSKiyoshi Ueda  * !0 : the request has been requeued
15979eef87daSKiyoshi Ueda  */
15989eef87daSKiyoshi Ueda static int map_request(struct dm_target *ti, struct request *clone,
1599cec47e3dSKiyoshi Ueda 		       struct mapped_device *md)
1600cec47e3dSKiyoshi Ueda {
16019eef87daSKiyoshi Ueda 	int r, requeued = 0;
1602cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1603cec47e3dSKiyoshi Ueda 
1604cec47e3dSKiyoshi Ueda 	tio->ti = ti;
1605cec47e3dSKiyoshi Ueda 	r = ti->type->map_rq(ti, clone, &tio->info);
1606cec47e3dSKiyoshi Ueda 	switch (r) {
1607cec47e3dSKiyoshi Ueda 	case DM_MAPIO_SUBMITTED:
1608cec47e3dSKiyoshi Ueda 		/* The target has taken the I/O to submit by itself later */
1609cec47e3dSKiyoshi Ueda 		break;
1610cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REMAPPED:
1611cec47e3dSKiyoshi Ueda 		/* The target has remapped the I/O so dispatch it */
16126db4ccd6SJun'ichi Nomura 		trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
16136db4ccd6SJun'ichi Nomura 				     blk_rq_pos(tio->orig));
1614cec47e3dSKiyoshi Ueda 		dm_dispatch_request(clone);
1615cec47e3dSKiyoshi Ueda 		break;
1616cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REQUEUE:
1617cec47e3dSKiyoshi Ueda 		/* The target wants to requeue the I/O */
1618cec47e3dSKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
16199eef87daSKiyoshi Ueda 		requeued = 1;
1620cec47e3dSKiyoshi Ueda 		break;
1621cec47e3dSKiyoshi Ueda 	default:
1622cec47e3dSKiyoshi Ueda 		if (r > 0) {
1623cec47e3dSKiyoshi Ueda 			DMWARN("unimplemented target map return value: %d", r);
1624cec47e3dSKiyoshi Ueda 			BUG();
1625cec47e3dSKiyoshi Ueda 		}
1626cec47e3dSKiyoshi Ueda 
1627cec47e3dSKiyoshi Ueda 		/* The target wants to complete the I/O */
1628cec47e3dSKiyoshi Ueda 		dm_kill_unmapped_request(clone, r);
1629cec47e3dSKiyoshi Ueda 		break;
1630cec47e3dSKiyoshi Ueda 	}
16319eef87daSKiyoshi Ueda 
16329eef87daSKiyoshi Ueda 	return requeued;
1633cec47e3dSKiyoshi Ueda }
1634cec47e3dSKiyoshi Ueda 
1635ba1cbad9SMike Snitzer static struct request *dm_start_request(struct mapped_device *md, struct request *orig)
1636ba1cbad9SMike Snitzer {
1637ba1cbad9SMike Snitzer 	struct request *clone;
1638ba1cbad9SMike Snitzer 
1639ba1cbad9SMike Snitzer 	blk_start_request(orig);
1640ba1cbad9SMike Snitzer 	clone = orig->special;
1641ba1cbad9SMike Snitzer 	atomic_inc(&md->pending[rq_data_dir(clone)]);
1642ba1cbad9SMike Snitzer 
1643ba1cbad9SMike Snitzer 	/*
1644ba1cbad9SMike Snitzer 	 * Hold the md reference here for the in-flight I/O.
1645ba1cbad9SMike Snitzer 	 * We can't rely on the reference count by device opener,
1646ba1cbad9SMike Snitzer 	 * because the device may be closed during the request completion
1647ba1cbad9SMike Snitzer 	 * when all bios are completed.
1648ba1cbad9SMike Snitzer 	 * See the comment in rq_completed() too.
1649ba1cbad9SMike Snitzer 	 */
1650ba1cbad9SMike Snitzer 	dm_get(md);
1651ba1cbad9SMike Snitzer 
1652ba1cbad9SMike Snitzer 	return clone;
1653ba1cbad9SMike Snitzer }
1654ba1cbad9SMike Snitzer 
1655cec47e3dSKiyoshi Ueda /*
1656cec47e3dSKiyoshi Ueda  * q->request_fn for request-based dm.
1657cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1658cec47e3dSKiyoshi Ueda  */
1659cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q)
1660cec47e3dSKiyoshi Ueda {
1661cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
16627c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1663cec47e3dSKiyoshi Ueda 	struct dm_target *ti;
1664b4324feeSKiyoshi Ueda 	struct request *rq, *clone;
166529e4013dSTejun Heo 	sector_t pos;
1666cec47e3dSKiyoshi Ueda 
1667cec47e3dSKiyoshi Ueda 	/*
1668b4324feeSKiyoshi Ueda 	 * For suspend, check blk_queue_stopped() and increment
1669b4324feeSKiyoshi Ueda 	 * ->pending within a single queue_lock not to increment the
1670b4324feeSKiyoshi Ueda 	 * number of in-flight I/Os after the queue is stopped in
1671b4324feeSKiyoshi Ueda 	 * dm_suspend().
1672cec47e3dSKiyoshi Ueda 	 */
16737eaceaccSJens Axboe 	while (!blk_queue_stopped(q)) {
1674cec47e3dSKiyoshi Ueda 		rq = blk_peek_request(q);
1675cec47e3dSKiyoshi Ueda 		if (!rq)
16767eaceaccSJens Axboe 			goto delay_and_out;
1677cec47e3dSKiyoshi Ueda 
167829e4013dSTejun Heo 		/* always use block 0 to find the target for flushes for now */
167929e4013dSTejun Heo 		pos = 0;
168029e4013dSTejun Heo 		if (!(rq->cmd_flags & REQ_FLUSH))
168129e4013dSTejun Heo 			pos = blk_rq_pos(rq);
1682d0bcb878SKiyoshi Ueda 
168329e4013dSTejun Heo 		ti = dm_table_find_target(map, pos);
1684ba1cbad9SMike Snitzer 		if (!dm_target_is_valid(ti)) {
1685ba1cbad9SMike Snitzer 			/*
1686ba1cbad9SMike Snitzer 			 * Must perform setup, that dm_done() requires,
1687ba1cbad9SMike Snitzer 			 * before calling dm_kill_unmapped_request
1688ba1cbad9SMike Snitzer 			 */
1689ba1cbad9SMike Snitzer 			DMERR_LIMIT("request attempted access beyond the end of device");
1690ba1cbad9SMike Snitzer 			clone = dm_start_request(md, rq);
1691ba1cbad9SMike Snitzer 			dm_kill_unmapped_request(clone, -EIO);
1692ba1cbad9SMike Snitzer 			continue;
1693ba1cbad9SMike Snitzer 		}
169429e4013dSTejun Heo 
1695cec47e3dSKiyoshi Ueda 		if (ti->type->busy && ti->type->busy(ti))
16967eaceaccSJens Axboe 			goto delay_and_out;
1697cec47e3dSKiyoshi Ueda 
1698ba1cbad9SMike Snitzer 		clone = dm_start_request(md, rq);
1699b4324feeSKiyoshi Ueda 
1700cec47e3dSKiyoshi Ueda 		spin_unlock(q->queue_lock);
17019eef87daSKiyoshi Ueda 		if (map_request(ti, clone, md))
17029eef87daSKiyoshi Ueda 			goto requeued;
17039eef87daSKiyoshi Ueda 
1704052189a2SKiyoshi Ueda 		BUG_ON(!irqs_disabled());
1705052189a2SKiyoshi Ueda 		spin_lock(q->queue_lock);
1706cec47e3dSKiyoshi Ueda 	}
1707cec47e3dSKiyoshi Ueda 
1708cec47e3dSKiyoshi Ueda 	goto out;
1709cec47e3dSKiyoshi Ueda 
17109eef87daSKiyoshi Ueda requeued:
1711052189a2SKiyoshi Ueda 	BUG_ON(!irqs_disabled());
1712052189a2SKiyoshi Ueda 	spin_lock(q->queue_lock);
17139eef87daSKiyoshi Ueda 
17147eaceaccSJens Axboe delay_and_out:
17157eaceaccSJens Axboe 	blk_delay_queue(q, HZ / 10);
1716cec47e3dSKiyoshi Ueda out:
1717cec47e3dSKiyoshi Ueda 	dm_table_put(map);
1718cec47e3dSKiyoshi Ueda }
1719cec47e3dSKiyoshi Ueda 
1720cec47e3dSKiyoshi Ueda int dm_underlying_device_busy(struct request_queue *q)
1721cec47e3dSKiyoshi Ueda {
1722cec47e3dSKiyoshi Ueda 	return blk_lld_busy(q);
1723cec47e3dSKiyoshi Ueda }
1724cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1725cec47e3dSKiyoshi Ueda 
1726cec47e3dSKiyoshi Ueda static int dm_lld_busy(struct request_queue *q)
1727cec47e3dSKiyoshi Ueda {
1728cec47e3dSKiyoshi Ueda 	int r;
1729cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
17307c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1731cec47e3dSKiyoshi Ueda 
1732cec47e3dSKiyoshi Ueda 	if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1733cec47e3dSKiyoshi Ueda 		r = 1;
1734cec47e3dSKiyoshi Ueda 	else
1735cec47e3dSKiyoshi Ueda 		r = dm_table_any_busy_target(map);
1736cec47e3dSKiyoshi Ueda 
1737cec47e3dSKiyoshi Ueda 	dm_table_put(map);
1738cec47e3dSKiyoshi Ueda 
1739cec47e3dSKiyoshi Ueda 	return r;
1740cec47e3dSKiyoshi Ueda }
1741cec47e3dSKiyoshi Ueda 
17421da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
17431da177e4SLinus Torvalds {
17448a57dfc6SChandra Seetharaman 	int r = bdi_bits;
17458a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
17468a57dfc6SChandra Seetharaman 	struct dm_table *map;
17471da177e4SLinus Torvalds 
17481eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
17497c666411SAlasdair G Kergon 		map = dm_get_live_table(md);
17508a57dfc6SChandra Seetharaman 		if (map) {
1751cec47e3dSKiyoshi Ueda 			/*
1752cec47e3dSKiyoshi Ueda 			 * Request-based dm cares about only own queue for
1753cec47e3dSKiyoshi Ueda 			 * the query about congestion status of request_queue
1754cec47e3dSKiyoshi Ueda 			 */
1755cec47e3dSKiyoshi Ueda 			if (dm_request_based(md))
1756cec47e3dSKiyoshi Ueda 				r = md->queue->backing_dev_info.state &
1757cec47e3dSKiyoshi Ueda 				    bdi_bits;
1758cec47e3dSKiyoshi Ueda 			else
17591da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
1760cec47e3dSKiyoshi Ueda 
17611da177e4SLinus Torvalds 			dm_table_put(map);
17628a57dfc6SChandra Seetharaman 		}
17638a57dfc6SChandra Seetharaman 	}
17648a57dfc6SChandra Seetharaman 
17651da177e4SLinus Torvalds 	return r;
17661da177e4SLinus Torvalds }
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds /*-----------------------------------------------------------------
17691da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
17701da177e4SLinus Torvalds  *---------------------------------------------------------------*/
17712b06cfffSAlasdair G Kergon static void free_minor(int minor)
17721da177e4SLinus Torvalds {
1773f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17741da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1775f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
17761da177e4SLinus Torvalds }
17771da177e4SLinus Torvalds 
17781da177e4SLinus Torvalds /*
17791da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
17801da177e4SLinus Torvalds  */
1781cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
17821da177e4SLinus Torvalds {
1783c9d76be6STejun Heo 	int r;
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
17861da177e4SLinus Torvalds 		return -EINVAL;
17871da177e4SLinus Torvalds 
1788c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1789f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17901da177e4SLinus Torvalds 
1791c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
17921da177e4SLinus Torvalds 
1793f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1794c9d76be6STejun Heo 	idr_preload_end();
1795c9d76be6STejun Heo 	if (r < 0)
1796c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
1797c9d76be6STejun Heo 	return 0;
17981da177e4SLinus Torvalds }
17991da177e4SLinus Torvalds 
1800cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
18011da177e4SLinus Torvalds {
1802c9d76be6STejun Heo 	int r;
18031da177e4SLinus Torvalds 
1804c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1805f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
18061da177e4SLinus Torvalds 
1807c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
18081da177e4SLinus Torvalds 
1809f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1810c9d76be6STejun Heo 	idr_preload_end();
1811c9d76be6STejun Heo 	if (r < 0)
18121da177e4SLinus Torvalds 		return r;
1813c9d76be6STejun Heo 	*minor = r;
1814c9d76be6STejun Heo 	return 0;
18151da177e4SLinus Torvalds }
18161da177e4SLinus Torvalds 
181783d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
18181da177e4SLinus Torvalds 
181953d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
182053d5914fSMikulas Patocka 
18214a0b4ddfSMike Snitzer static void dm_init_md_queue(struct mapped_device *md)
18224a0b4ddfSMike Snitzer {
18234a0b4ddfSMike Snitzer 	/*
18244a0b4ddfSMike Snitzer 	 * Request-based dm devices cannot be stacked on top of bio-based dm
18254a0b4ddfSMike Snitzer 	 * devices.  The type of this dm device has not been decided yet.
18264a0b4ddfSMike Snitzer 	 * The type is decided at the first table loading time.
18274a0b4ddfSMike Snitzer 	 * To prevent problematic device stacking, clear the queue flag
18284a0b4ddfSMike Snitzer 	 * for request stacking support until then.
18294a0b4ddfSMike Snitzer 	 *
18304a0b4ddfSMike Snitzer 	 * This queue is new, so no concurrency on the queue_flags.
18314a0b4ddfSMike Snitzer 	 */
18324a0b4ddfSMike Snitzer 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
18334a0b4ddfSMike Snitzer 
18344a0b4ddfSMike Snitzer 	md->queue->queuedata = md;
18354a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
18364a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_data = md;
18374a0b4ddfSMike Snitzer 	blk_queue_make_request(md->queue, dm_request);
18384a0b4ddfSMike Snitzer 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
18394a0b4ddfSMike Snitzer 	blk_queue_merge_bvec(md->queue, dm_merge_bvec);
18404a0b4ddfSMike Snitzer }
18414a0b4ddfSMike Snitzer 
18421da177e4SLinus Torvalds /*
18431da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
18441da177e4SLinus Torvalds  */
18452b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
18461da177e4SLinus Torvalds {
18471da177e4SLinus Torvalds 	int r;
1848cf13ab8eSFrederik Deweerdt 	struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1849ba61fdd1SJeff Mahoney 	void *old_md;
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds 	if (!md) {
18521da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
18531da177e4SLinus Torvalds 		return NULL;
18541da177e4SLinus Torvalds 	}
18551da177e4SLinus Torvalds 
185610da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
18576ed7ade8SMilan Broz 		goto bad_module_get;
185810da4f79SJeff Mahoney 
18591da177e4SLinus Torvalds 	/* get a minor number for the dev */
18602b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1861cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
18622b06cfffSAlasdair G Kergon 	else
1863cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
18641da177e4SLinus Torvalds 	if (r < 0)
18656ed7ade8SMilan Broz 		goto bad_minor;
18661da177e4SLinus Torvalds 
1867a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
18682ca3310eSAlasdair G Kergon 	init_rwsem(&md->io_lock);
1869e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1870a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
1871022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
18721da177e4SLinus Torvalds 	rwlock_init(&md->map_lock);
18731da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
18745c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
18751da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
18767a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
18777a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
18787a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
18791da177e4SLinus Torvalds 
18804a0b4ddfSMike Snitzer 	md->queue = blk_alloc_queue(GFP_KERNEL);
18811da177e4SLinus Torvalds 	if (!md->queue)
18826ed7ade8SMilan Broz 		goto bad_queue;
18831da177e4SLinus Torvalds 
18844a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
18859faf400fSStefan Bader 
18861da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
18871da177e4SLinus Torvalds 	if (!md->disk)
18886ed7ade8SMilan Broz 		goto bad_disk;
18891da177e4SLinus Torvalds 
1890316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
1891316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
1892f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
189353d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1894f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
1895f0b04115SJeff Mahoney 
18961da177e4SLinus Torvalds 	md->disk->major = _major;
18971da177e4SLinus Torvalds 	md->disk->first_minor = minor;
18981da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
18991da177e4SLinus Torvalds 	md->disk->queue = md->queue;
19001da177e4SLinus Torvalds 	md->disk->private_data = md;
19011da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
19021da177e4SLinus Torvalds 	add_disk(md->disk);
19037e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
19041da177e4SLinus Torvalds 
19059c4376deSTejun Heo 	md->wq = alloc_workqueue("kdmflush",
19069c4376deSTejun Heo 				 WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
1907304f3f6aSMilan Broz 	if (!md->wq)
1908304f3f6aSMilan Broz 		goto bad_thread;
1909304f3f6aSMilan Broz 
191032a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
191132a926daSMikulas Patocka 	if (!md->bdev)
191232a926daSMikulas Patocka 		goto bad_bdev;
191332a926daSMikulas Patocka 
19146a8736d1STejun Heo 	bio_init(&md->flush_bio);
19156a8736d1STejun Heo 	md->flush_bio.bi_bdev = md->bdev;
19166a8736d1STejun Heo 	md->flush_bio.bi_rw = WRITE_FLUSH;
19176a8736d1STejun Heo 
1918ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1919f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1920ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1921f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1922ba61fdd1SJeff Mahoney 
1923ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1924ba61fdd1SJeff Mahoney 
19251da177e4SLinus Torvalds 	return md;
19261da177e4SLinus Torvalds 
192732a926daSMikulas Patocka bad_bdev:
192832a926daSMikulas Patocka 	destroy_workqueue(md->wq);
1929304f3f6aSMilan Broz bad_thread:
193003022c54SZdenek Kabelac 	del_gendisk(md->disk);
1931304f3f6aSMilan Broz 	put_disk(md->disk);
19326ed7ade8SMilan Broz bad_disk:
19331312f40eSAl Viro 	blk_cleanup_queue(md->queue);
19346ed7ade8SMilan Broz bad_queue:
19351da177e4SLinus Torvalds 	free_minor(minor);
19366ed7ade8SMilan Broz bad_minor:
193710da4f79SJeff Mahoney 	module_put(THIS_MODULE);
19386ed7ade8SMilan Broz bad_module_get:
19391da177e4SLinus Torvalds 	kfree(md);
19401da177e4SLinus Torvalds 	return NULL;
19411da177e4SLinus Torvalds }
19421da177e4SLinus Torvalds 
1943ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1944ae9da83fSJun'ichi Nomura 
19451da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
19461da177e4SLinus Torvalds {
1947f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
194863d94e48SJun'ichi Nomura 
1949ae9da83fSJun'ichi Nomura 	unlock_fs(md);
1950db8fef4fSMikulas Patocka 	bdput(md->bdev);
1951304f3f6aSMilan Broz 	destroy_workqueue(md->wq);
1952e6ee8c0bSKiyoshi Ueda 	if (md->tio_pool)
19531da177e4SLinus Torvalds 		mempool_destroy(md->tio_pool);
1954e6ee8c0bSKiyoshi Ueda 	if (md->io_pool)
19551da177e4SLinus Torvalds 		mempool_destroy(md->io_pool);
1956e6ee8c0bSKiyoshi Ueda 	if (md->bs)
19579faf400fSStefan Bader 		bioset_free(md->bs);
19589c47008dSMartin K. Petersen 	blk_integrity_unregister(md->disk);
19591da177e4SLinus Torvalds 	del_gendisk(md->disk);
196063d94e48SJun'ichi Nomura 	free_minor(minor);
1961fba9f90eSJeff Mahoney 
1962fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
1963fba9f90eSJeff Mahoney 	md->disk->private_data = NULL;
1964fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
1965fba9f90eSJeff Mahoney 
19661da177e4SLinus Torvalds 	put_disk(md->disk);
19671312f40eSAl Viro 	blk_cleanup_queue(md->queue);
196810da4f79SJeff Mahoney 	module_put(THIS_MODULE);
19691da177e4SLinus Torvalds 	kfree(md);
19701da177e4SLinus Torvalds }
19711da177e4SLinus Torvalds 
1972e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1973e6ee8c0bSKiyoshi Ueda {
1974c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
1975e6ee8c0bSKiyoshi Ueda 
1976*23e5083bSJun'ichi Nomura 	if (md->bs) {
197716245bdcSJun'ichi Nomura 		/* The md already has necessary mempools. */
197816245bdcSJun'ichi Nomura 		if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
1979c0820cf5SMikulas Patocka 			/*
198016245bdcSJun'ichi Nomura 			 * Reload bioset because front_pad may have changed
198116245bdcSJun'ichi Nomura 			 * because a different table was loaded.
1982c0820cf5SMikulas Patocka 			 */
1983c0820cf5SMikulas Patocka 			bioset_free(md->bs);
1984c0820cf5SMikulas Patocka 			md->bs = p->bs;
1985c0820cf5SMikulas Patocka 			p->bs = NULL;
198616245bdcSJun'ichi Nomura 		} else if (dm_table_get_type(t) == DM_TYPE_REQUEST_BASED) {
198716245bdcSJun'ichi Nomura 			BUG_ON(!md->tio_pool);
198816245bdcSJun'ichi Nomura 			/*
198916245bdcSJun'ichi Nomura 			 * There's no need to reload with request-based dm
199016245bdcSJun'ichi Nomura 			 * because the size of front_pad doesn't change.
199116245bdcSJun'ichi Nomura 			 * Note for future: If you are to reload bioset,
199216245bdcSJun'ichi Nomura 			 * prep-ed requests in the queue may refer
199316245bdcSJun'ichi Nomura 			 * to bio from the old bioset, so you must walk
199416245bdcSJun'ichi Nomura 			 * through the queue to unprep.
199516245bdcSJun'ichi Nomura 			 */
199616245bdcSJun'ichi Nomura 		}
1997e6ee8c0bSKiyoshi Ueda 		goto out;
1998c0820cf5SMikulas Patocka 	}
1999e6ee8c0bSKiyoshi Ueda 
2000e6ee8c0bSKiyoshi Ueda 	BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
2001e6ee8c0bSKiyoshi Ueda 
2002e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
2003e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
2004e6ee8c0bSKiyoshi Ueda 	md->tio_pool = p->tio_pool;
2005e6ee8c0bSKiyoshi Ueda 	p->tio_pool = NULL;
2006e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
2007e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
2008e6ee8c0bSKiyoshi Ueda 
2009e6ee8c0bSKiyoshi Ueda out:
2010e6ee8c0bSKiyoshi Ueda 	/* mempool bind completed, now no need any mempools in the table */
2011e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
2012e6ee8c0bSKiyoshi Ueda }
2013e6ee8c0bSKiyoshi Ueda 
20141da177e4SLinus Torvalds /*
20151da177e4SLinus Torvalds  * Bind a table to the device.
20161da177e4SLinus Torvalds  */
20171da177e4SLinus Torvalds static void event_callback(void *context)
20181da177e4SLinus Torvalds {
20197a8c3d3bSMike Anderson 	unsigned long flags;
20207a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
20211da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
20221da177e4SLinus Torvalds 
20237a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
20247a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
20257a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
20267a8c3d3bSMike Anderson 
2027ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
20287a8c3d3bSMike Anderson 
20291da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
20301da177e4SLinus Torvalds 	wake_up(&md->eventq);
20311da177e4SLinus Torvalds }
20321da177e4SLinus Torvalds 
2033c217649bSMike Snitzer /*
2034c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
2035c217649bSMike Snitzer  */
20364e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
20371da177e4SLinus Torvalds {
20384e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
20391da177e4SLinus Torvalds 
2040db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
20411da177e4SLinus Torvalds }
20421da177e4SLinus Torvalds 
2043042d2a9bSAlasdair G Kergon /*
2044d5b9dd04SMikulas Patocka  * Return 1 if the queue has a compulsory merge_bvec_fn function.
2045d5b9dd04SMikulas Patocka  *
2046d5b9dd04SMikulas Patocka  * If this function returns 0, then the device is either a non-dm
2047d5b9dd04SMikulas Patocka  * device without a merge_bvec_fn, or it is a dm device that is
2048d5b9dd04SMikulas Patocka  * able to split any bios it receives that are too big.
2049d5b9dd04SMikulas Patocka  */
2050d5b9dd04SMikulas Patocka int dm_queue_merge_is_compulsory(struct request_queue *q)
2051d5b9dd04SMikulas Patocka {
2052d5b9dd04SMikulas Patocka 	struct mapped_device *dev_md;
2053d5b9dd04SMikulas Patocka 
2054d5b9dd04SMikulas Patocka 	if (!q->merge_bvec_fn)
2055d5b9dd04SMikulas Patocka 		return 0;
2056d5b9dd04SMikulas Patocka 
2057d5b9dd04SMikulas Patocka 	if (q->make_request_fn == dm_request) {
2058d5b9dd04SMikulas Patocka 		dev_md = q->queuedata;
2059d5b9dd04SMikulas Patocka 		if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2060d5b9dd04SMikulas Patocka 			return 0;
2061d5b9dd04SMikulas Patocka 	}
2062d5b9dd04SMikulas Patocka 
2063d5b9dd04SMikulas Patocka 	return 1;
2064d5b9dd04SMikulas Patocka }
2065d5b9dd04SMikulas Patocka 
2066d5b9dd04SMikulas Patocka static int dm_device_merge_is_compulsory(struct dm_target *ti,
2067d5b9dd04SMikulas Patocka 					 struct dm_dev *dev, sector_t start,
2068d5b9dd04SMikulas Patocka 					 sector_t len, void *data)
2069d5b9dd04SMikulas Patocka {
2070d5b9dd04SMikulas Patocka 	struct block_device *bdev = dev->bdev;
2071d5b9dd04SMikulas Patocka 	struct request_queue *q = bdev_get_queue(bdev);
2072d5b9dd04SMikulas Patocka 
2073d5b9dd04SMikulas Patocka 	return dm_queue_merge_is_compulsory(q);
2074d5b9dd04SMikulas Patocka }
2075d5b9dd04SMikulas Patocka 
2076d5b9dd04SMikulas Patocka /*
2077d5b9dd04SMikulas Patocka  * Return 1 if it is acceptable to ignore merge_bvec_fn based
2078d5b9dd04SMikulas Patocka  * on the properties of the underlying devices.
2079d5b9dd04SMikulas Patocka  */
2080d5b9dd04SMikulas Patocka static int dm_table_merge_is_optional(struct dm_table *table)
2081d5b9dd04SMikulas Patocka {
2082d5b9dd04SMikulas Patocka 	unsigned i = 0;
2083d5b9dd04SMikulas Patocka 	struct dm_target *ti;
2084d5b9dd04SMikulas Patocka 
2085d5b9dd04SMikulas Patocka 	while (i < dm_table_get_num_targets(table)) {
2086d5b9dd04SMikulas Patocka 		ti = dm_table_get_target(table, i++);
2087d5b9dd04SMikulas Patocka 
2088d5b9dd04SMikulas Patocka 		if (ti->type->iterate_devices &&
2089d5b9dd04SMikulas Patocka 		    ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2090d5b9dd04SMikulas Patocka 			return 0;
2091d5b9dd04SMikulas Patocka 	}
2092d5b9dd04SMikulas Patocka 
2093d5b9dd04SMikulas Patocka 	return 1;
2094d5b9dd04SMikulas Patocka }
2095d5b9dd04SMikulas Patocka 
2096d5b9dd04SMikulas Patocka /*
2097042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2098042d2a9bSAlasdair G Kergon  */
2099042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2100754c5fc7SMike Snitzer 			       struct queue_limits *limits)
21011da177e4SLinus Torvalds {
2102042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
2103165125e1SJens Axboe 	struct request_queue *q = md->queue;
21041da177e4SLinus Torvalds 	sector_t size;
2105523d9297SKiyoshi Ueda 	unsigned long flags;
2106d5b9dd04SMikulas Patocka 	int merge_is_optional;
21071da177e4SLinus Torvalds 
21081da177e4SLinus Torvalds 	size = dm_table_get_size(t);
21093ac51e74SDarrick J. Wong 
21103ac51e74SDarrick J. Wong 	/*
21113ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
21123ac51e74SDarrick J. Wong 	 */
21133ac51e74SDarrick J. Wong 	if (size != get_capacity(md->disk))
21143ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
21153ac51e74SDarrick J. Wong 
21164e90188bSAlasdair G Kergon 	__set_size(md, size);
21171da177e4SLinus Torvalds 
2118cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
21192ca3310eSAlasdair G Kergon 
2120e6ee8c0bSKiyoshi Ueda 	/*
2121e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
2122e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
2123e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
2124e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
2125e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
2126e6ee8c0bSKiyoshi Ueda 	 */
2127e6ee8c0bSKiyoshi Ueda 	if (dm_table_request_based(t) && !blk_queue_stopped(q))
2128e6ee8c0bSKiyoshi Ueda 		stop_queue(q);
2129e6ee8c0bSKiyoshi Ueda 
2130e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
2131e6ee8c0bSKiyoshi Ueda 
2132d5b9dd04SMikulas Patocka 	merge_is_optional = dm_table_merge_is_optional(t);
2133d5b9dd04SMikulas Patocka 
2134523d9297SKiyoshi Ueda 	write_lock_irqsave(&md->map_lock, flags);
2135042d2a9bSAlasdair G Kergon 	old_map = md->map;
21362ca3310eSAlasdair G Kergon 	md->map = t;
213736a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
213836a0456fSAlasdair G Kergon 
2139754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
2140d5b9dd04SMikulas Patocka 	if (merge_is_optional)
2141d5b9dd04SMikulas Patocka 		set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2142d5b9dd04SMikulas Patocka 	else
2143d5b9dd04SMikulas Patocka 		clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2144523d9297SKiyoshi Ueda 	write_unlock_irqrestore(&md->map_lock, flags);
21452ca3310eSAlasdair G Kergon 
2146042d2a9bSAlasdair G Kergon 	return old_map;
21471da177e4SLinus Torvalds }
21481da177e4SLinus Torvalds 
2149a7940155SAlasdair G Kergon /*
2150a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2151a7940155SAlasdair G Kergon  */
2152a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
21531da177e4SLinus Torvalds {
21541da177e4SLinus Torvalds 	struct dm_table *map = md->map;
2155523d9297SKiyoshi Ueda 	unsigned long flags;
21561da177e4SLinus Torvalds 
21571da177e4SLinus Torvalds 	if (!map)
2158a7940155SAlasdair G Kergon 		return NULL;
21591da177e4SLinus Torvalds 
21601da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
2161523d9297SKiyoshi Ueda 	write_lock_irqsave(&md->map_lock, flags);
21621da177e4SLinus Torvalds 	md->map = NULL;
2163523d9297SKiyoshi Ueda 	write_unlock_irqrestore(&md->map_lock, flags);
2164a7940155SAlasdair G Kergon 
2165a7940155SAlasdair G Kergon 	return map;
21661da177e4SLinus Torvalds }
21671da177e4SLinus Torvalds 
21681da177e4SLinus Torvalds /*
21691da177e4SLinus Torvalds  * Constructor for a new device.
21701da177e4SLinus Torvalds  */
21712b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
21721da177e4SLinus Torvalds {
21731da177e4SLinus Torvalds 	struct mapped_device *md;
21741da177e4SLinus Torvalds 
21752b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
21761da177e4SLinus Torvalds 	if (!md)
21771da177e4SLinus Torvalds 		return -ENXIO;
21781da177e4SLinus Torvalds 
2179784aae73SMilan Broz 	dm_sysfs_init(md);
2180784aae73SMilan Broz 
21811da177e4SLinus Torvalds 	*result = md;
21821da177e4SLinus Torvalds 	return 0;
21831da177e4SLinus Torvalds }
21841da177e4SLinus Torvalds 
2185a5664dadSMike Snitzer /*
2186a5664dadSMike Snitzer  * Functions to manage md->type.
2187a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2188a5664dadSMike Snitzer  */
2189a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2190a5664dadSMike Snitzer {
2191a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2192a5664dadSMike Snitzer }
2193a5664dadSMike Snitzer 
2194a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2195a5664dadSMike Snitzer {
2196a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2197a5664dadSMike Snitzer }
2198a5664dadSMike Snitzer 
2199a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type)
2200a5664dadSMike Snitzer {
2201a5664dadSMike Snitzer 	md->type = type;
2202a5664dadSMike Snitzer }
2203a5664dadSMike Snitzer 
2204a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md)
2205a5664dadSMike Snitzer {
2206a5664dadSMike Snitzer 	return md->type;
2207a5664dadSMike Snitzer }
2208a5664dadSMike Snitzer 
220936a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
221036a0456fSAlasdair G Kergon {
221136a0456fSAlasdair G Kergon 	return md->immutable_target_type;
221236a0456fSAlasdair G Kergon }
221336a0456fSAlasdair G Kergon 
22144a0b4ddfSMike Snitzer /*
22154a0b4ddfSMike Snitzer  * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
22164a0b4ddfSMike Snitzer  */
22174a0b4ddfSMike Snitzer static int dm_init_request_based_queue(struct mapped_device *md)
22184a0b4ddfSMike Snitzer {
22194a0b4ddfSMike Snitzer 	struct request_queue *q = NULL;
22204a0b4ddfSMike Snitzer 
22214a0b4ddfSMike Snitzer 	if (md->queue->elevator)
22224a0b4ddfSMike Snitzer 		return 1;
22234a0b4ddfSMike Snitzer 
22244a0b4ddfSMike Snitzer 	/* Fully initialize the queue */
22254a0b4ddfSMike Snitzer 	q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
22264a0b4ddfSMike Snitzer 	if (!q)
22274a0b4ddfSMike Snitzer 		return 0;
22284a0b4ddfSMike Snitzer 
22294a0b4ddfSMike Snitzer 	md->queue = q;
22304a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
22314a0b4ddfSMike Snitzer 	blk_queue_softirq_done(md->queue, dm_softirq_done);
22324a0b4ddfSMike Snitzer 	blk_queue_prep_rq(md->queue, dm_prep_fn);
22334a0b4ddfSMike Snitzer 	blk_queue_lld_busy(md->queue, dm_lld_busy);
22344a0b4ddfSMike Snitzer 
22354a0b4ddfSMike Snitzer 	elv_register_queue(md->queue);
22364a0b4ddfSMike Snitzer 
22374a0b4ddfSMike Snitzer 	return 1;
22384a0b4ddfSMike Snitzer }
22394a0b4ddfSMike Snitzer 
22404a0b4ddfSMike Snitzer /*
22414a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
22424a0b4ddfSMike Snitzer  */
22434a0b4ddfSMike Snitzer int dm_setup_md_queue(struct mapped_device *md)
22444a0b4ddfSMike Snitzer {
22454a0b4ddfSMike Snitzer 	if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
22464a0b4ddfSMike Snitzer 	    !dm_init_request_based_queue(md)) {
22474a0b4ddfSMike Snitzer 		DMWARN("Cannot initialize queue for request-based mapped device");
22484a0b4ddfSMike Snitzer 		return -EINVAL;
22494a0b4ddfSMike Snitzer 	}
22504a0b4ddfSMike Snitzer 
22514a0b4ddfSMike Snitzer 	return 0;
22524a0b4ddfSMike Snitzer }
22534a0b4ddfSMike Snitzer 
2254637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev)
22551da177e4SLinus Torvalds {
22561da177e4SLinus Torvalds 	struct mapped_device *md;
22571da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
22581da177e4SLinus Torvalds 
22591da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
22601da177e4SLinus Torvalds 		return NULL;
22611da177e4SLinus Torvalds 
2262f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
22631da177e4SLinus Torvalds 
22641da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
2265fba9f90eSJeff Mahoney 	if (md && (md == MINOR_ALLOCED ||
2266f331c029STejun Heo 		   (MINOR(disk_devt(dm_disk(md))) != minor) ||
2267abdc568bSKiyoshi Ueda 		   dm_deleting_md(md) ||
2268fba9f90eSJeff Mahoney 		   test_bit(DMF_FREEING, &md->flags))) {
2269637842cfSDavid Teigland 		md = NULL;
2270fba9f90eSJeff Mahoney 		goto out;
2271fba9f90eSJeff Mahoney 	}
22721da177e4SLinus Torvalds 
2273fba9f90eSJeff Mahoney out:
2274f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
22751da177e4SLinus Torvalds 
2276637842cfSDavid Teigland 	return md;
2277637842cfSDavid Teigland }
2278637842cfSDavid Teigland 
2279d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev)
2280d229a958SDavid Teigland {
2281d229a958SDavid Teigland 	struct mapped_device *md = dm_find_md(dev);
2282d229a958SDavid Teigland 
2283d229a958SDavid Teigland 	if (md)
2284d229a958SDavid Teigland 		dm_get(md);
2285d229a958SDavid Teigland 
2286d229a958SDavid Teigland 	return md;
2287d229a958SDavid Teigland }
22883cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2289d229a958SDavid Teigland 
22909ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2291637842cfSDavid Teigland {
22929ade92a9SAlasdair G Kergon 	return md->interface_ptr;
22931da177e4SLinus Torvalds }
22941da177e4SLinus Torvalds 
22951da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
22961da177e4SLinus Torvalds {
22971da177e4SLinus Torvalds 	md->interface_ptr = ptr;
22981da177e4SLinus Torvalds }
22991da177e4SLinus Torvalds 
23001da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
23011da177e4SLinus Torvalds {
23021da177e4SLinus Torvalds 	atomic_inc(&md->holders);
23033f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
23041da177e4SLinus Torvalds }
23051da177e4SLinus Torvalds 
230672d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
230772d94861SAlasdair G Kergon {
230872d94861SAlasdair G Kergon 	return md->name;
230972d94861SAlasdair G Kergon }
231072d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
231172d94861SAlasdair G Kergon 
23123f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
23131da177e4SLinus Torvalds {
23141134e5aeSMike Anderson 	struct dm_table *map;
23151da177e4SLinus Torvalds 
23163f77316dSKiyoshi Ueda 	might_sleep();
2317fba9f90eSJeff Mahoney 
23183f77316dSKiyoshi Ueda 	spin_lock(&_minor_lock);
23197c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
23203f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2321fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2322f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
23233f77316dSKiyoshi Ueda 
23244f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
23251da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
23261da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
23271da177e4SLinus Torvalds 	}
23283f77316dSKiyoshi Ueda 
23293f77316dSKiyoshi Ueda 	/*
23303f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
23313f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
23323f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
23333f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
23343f77316dSKiyoshi Ueda 	 */
23353f77316dSKiyoshi Ueda 	if (wait)
23363f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
23373f77316dSKiyoshi Ueda 			msleep(1);
23383f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
23393f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
23403f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
23413f77316dSKiyoshi Ueda 
2342784aae73SMilan Broz 	dm_sysfs_exit(md);
23431134e5aeSMike Anderson 	dm_table_put(map);
2344a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
23451da177e4SLinus Torvalds 	free_dev(md);
23461da177e4SLinus Torvalds }
23473f77316dSKiyoshi Ueda 
23483f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
23493f77316dSKiyoshi Ueda {
23503f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
23513f77316dSKiyoshi Ueda }
23523f77316dSKiyoshi Ueda 
23533f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
23543f77316dSKiyoshi Ueda {
23553f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
23563f77316dSKiyoshi Ueda }
23573f77316dSKiyoshi Ueda 
23583f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
23593f77316dSKiyoshi Ueda {
23603f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
23611da177e4SLinus Torvalds }
236279eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
23631da177e4SLinus Torvalds 
2364401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
236546125c1cSMilan Broz {
236646125c1cSMilan Broz 	int r = 0;
2367b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
2368b44ebeb0SMikulas Patocka 
2369b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
237046125c1cSMilan Broz 
237146125c1cSMilan Broz 	while (1) {
2372401600dfSMikulas Patocka 		set_current_state(interruptible);
237346125c1cSMilan Broz 
2374b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
237546125c1cSMilan Broz 			break;
237646125c1cSMilan Broz 
2377401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
2378401600dfSMikulas Patocka 		    signal_pending(current)) {
237946125c1cSMilan Broz 			r = -EINTR;
238046125c1cSMilan Broz 			break;
238146125c1cSMilan Broz 		}
238246125c1cSMilan Broz 
238346125c1cSMilan Broz 		io_schedule();
238446125c1cSMilan Broz 	}
238546125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
238646125c1cSMilan Broz 
2387b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
2388b44ebeb0SMikulas Patocka 
238946125c1cSMilan Broz 	return r;
239046125c1cSMilan Broz }
239146125c1cSMilan Broz 
23921da177e4SLinus Torvalds /*
23931da177e4SLinus Torvalds  * Process the deferred bios
23941da177e4SLinus Torvalds  */
2395ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
23961da177e4SLinus Torvalds {
2397ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2398ef208587SMikulas Patocka 						work);
23996d6f10dfSMilan Broz 	struct bio *c;
24001da177e4SLinus Torvalds 
24016a8736d1STejun Heo 	down_read(&md->io_lock);
2402ef208587SMikulas Patocka 
24033b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2404022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2405022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2406022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2407022c2611SMikulas Patocka 
24086a8736d1STejun Heo 		if (!c)
2409df12ee99SAlasdair G Kergon 			break;
241073d410c0SMilan Broz 
24116a8736d1STejun Heo 		up_read(&md->io_lock);
24123b00b203SMikulas Patocka 
2413e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2414e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2415af7e466aSMikulas Patocka 		else
2416df12ee99SAlasdair G Kergon 			__split_and_process_bio(md, c);
24176a8736d1STejun Heo 
24186a8736d1STejun Heo 		down_read(&md->io_lock);
2419e6ee8c0bSKiyoshi Ueda 	}
24203b00b203SMikulas Patocka 
24216a8736d1STejun Heo 	up_read(&md->io_lock);
24221da177e4SLinus Torvalds }
24231da177e4SLinus Torvalds 
24249a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2425304f3f6aSMilan Broz {
24263b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
24273b00b203SMikulas Patocka 	smp_mb__after_clear_bit();
242853d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2429304f3f6aSMilan Broz }
2430304f3f6aSMilan Broz 
24311da177e4SLinus Torvalds /*
2432042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
24331da177e4SLinus Torvalds  */
2434042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
24351da177e4SLinus Torvalds {
243687eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2437754c5fc7SMike Snitzer 	struct queue_limits limits;
2438042d2a9bSAlasdair G Kergon 	int r;
24391da177e4SLinus Torvalds 
2440e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
24411da177e4SLinus Torvalds 
24421da177e4SLinus Torvalds 	/* device must be suspended */
24434f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
244493c534aeSAlasdair G Kergon 		goto out;
24451da177e4SLinus Torvalds 
24463ae70656SMike Snitzer 	/*
24473ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
24483ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
24493ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
24503ae70656SMike Snitzer 	 * reappear.
24513ae70656SMike Snitzer 	 */
24523ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
24533ae70656SMike Snitzer 		live_map = dm_get_live_table(md);
24543ae70656SMike Snitzer 		if (live_map)
24553ae70656SMike Snitzer 			limits = md->queue->limits;
24563ae70656SMike Snitzer 		dm_table_put(live_map);
24573ae70656SMike Snitzer 	}
24583ae70656SMike Snitzer 
245987eb5b21SMike Christie 	if (!live_map) {
2460754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2461042d2a9bSAlasdair G Kergon 		if (r) {
2462042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2463754c5fc7SMike Snitzer 			goto out;
2464042d2a9bSAlasdair G Kergon 		}
246587eb5b21SMike Christie 	}
2466754c5fc7SMike Snitzer 
2467042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
24681da177e4SLinus Torvalds 
246993c534aeSAlasdair G Kergon out:
2470e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2471042d2a9bSAlasdair G Kergon 	return map;
24721da177e4SLinus Torvalds }
24731da177e4SLinus Torvalds 
24741da177e4SLinus Torvalds /*
24751da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
24761da177e4SLinus Torvalds  * device.
24771da177e4SLinus Torvalds  */
24782ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
24791da177e4SLinus Torvalds {
2480e39e2e95SAlasdair G Kergon 	int r;
24811da177e4SLinus Torvalds 
24821da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2483dfbe03f6SAlasdair G Kergon 
2484db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2485dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2486cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2487e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2488e39e2e95SAlasdair G Kergon 		return r;
2489dfbe03f6SAlasdair G Kergon 	}
2490dfbe03f6SAlasdair G Kergon 
2491aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2492aa8d7c2fSAlasdair G Kergon 
24931da177e4SLinus Torvalds 	return 0;
24941da177e4SLinus Torvalds }
24951da177e4SLinus Torvalds 
24962ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
24971da177e4SLinus Torvalds {
2498aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2499aa8d7c2fSAlasdair G Kergon 		return;
2500aa8d7c2fSAlasdair G Kergon 
2501db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
25021da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2503aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
25041da177e4SLinus Torvalds }
25051da177e4SLinus Torvalds 
25061da177e4SLinus Torvalds /*
25071da177e4SLinus Torvalds  * We need to be able to change a mapping table under a mounted
25081da177e4SLinus Torvalds  * filesystem.  For example we might want to move some data in
25091da177e4SLinus Torvalds  * the background.  Before the table can be swapped with
25101da177e4SLinus Torvalds  * dm_bind_table, dm_suspend must be called to flush any in
25111da177e4SLinus Torvalds  * flight bios and ensure that any further io gets deferred.
25121da177e4SLinus Torvalds  */
2513cec47e3dSKiyoshi Ueda /*
2514cec47e3dSKiyoshi Ueda  * Suspend mechanism in request-based dm.
2515cec47e3dSKiyoshi Ueda  *
25169f518b27SKiyoshi Ueda  * 1. Flush all I/Os by lock_fs() if needed.
25179f518b27SKiyoshi Ueda  * 2. Stop dispatching any I/O by stopping the request_queue.
25189f518b27SKiyoshi Ueda  * 3. Wait for all in-flight I/Os to be completed or requeued.
2519cec47e3dSKiyoshi Ueda  *
25209f518b27SKiyoshi Ueda  * To abort suspend, start the request_queue.
2521cec47e3dSKiyoshi Ueda  */
2522a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
25231da177e4SLinus Torvalds {
25242ca3310eSAlasdair G Kergon 	struct dm_table *map = NULL;
252546125c1cSMilan Broz 	int r = 0;
2526a3d77d35SKiyoshi Ueda 	int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
25272e93ccc1SKiyoshi Ueda 	int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
25281da177e4SLinus Torvalds 
2529e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
25302ca3310eSAlasdair G Kergon 
25314f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
253273d410c0SMilan Broz 		r = -EINVAL;
2533d287483dSAlasdair G Kergon 		goto out_unlock;
253473d410c0SMilan Broz 	}
25351da177e4SLinus Torvalds 
25367c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
2537cf222b37SAlasdair G Kergon 
25382e93ccc1SKiyoshi Ueda 	/*
25392e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
25402e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
25412e93ccc1SKiyoshi Ueda 	 */
25422e93ccc1SKiyoshi Ueda 	if (noflush)
25432e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
25442e93ccc1SKiyoshi Ueda 
2545436d4108SAlasdair G Kergon 	/* This does not get reverted if there's an error later. */
25461da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
25471da177e4SLinus Torvalds 
25482e93ccc1SKiyoshi Ueda 	/*
25499f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
25509f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
25519f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
25529f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
25532e93ccc1SKiyoshi Ueda 	 */
255432a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
25552ca3310eSAlasdair G Kergon 		r = lock_fs(md);
25562ca3310eSAlasdair G Kergon 		if (r)
25572ca3310eSAlasdair G Kergon 			goto out;
2558aa8d7c2fSAlasdair G Kergon 	}
25591da177e4SLinus Torvalds 
25601da177e4SLinus Torvalds 	/*
25613b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
25623b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
25633b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
25643b00b203SMikulas Patocka 	 * dm_wq_work.
25653b00b203SMikulas Patocka 	 *
25663b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
25673b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
25686a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
25696a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
25706a8736d1STejun Heo 	 * flush_workqueue(md->wq).
25711da177e4SLinus Torvalds 	 */
25722ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
25731eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
25742ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
25751da177e4SLinus Torvalds 
2576d0bcb878SKiyoshi Ueda 	/*
257729e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
257829e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2579d0bcb878SKiyoshi Ueda 	 */
2580cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
25819f518b27SKiyoshi Ueda 		stop_queue(md->queue);
2582cec47e3dSKiyoshi Ueda 
2583d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2584d0bcb878SKiyoshi Ueda 
25851da177e4SLinus Torvalds 	/*
25863b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
25873b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
25883b00b203SMikulas Patocka 	 * to finish.
25891da177e4SLinus Torvalds 	 */
2590401600dfSMikulas Patocka 	r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
25911da177e4SLinus Torvalds 
25922ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
25936d6f10dfSMilan Broz 	if (noflush)
2594022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
259594d6351eSMilan Broz 	up_write(&md->io_lock);
25962e93ccc1SKiyoshi Ueda 
25971da177e4SLinus Torvalds 	/* were we interrupted ? */
259846125c1cSMilan Broz 	if (r < 0) {
25999a1fb464SMikulas Patocka 		dm_queue_flush(md);
260073d410c0SMilan Broz 
2601cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
26029f518b27SKiyoshi Ueda 			start_queue(md->queue);
2603cec47e3dSKiyoshi Ueda 
26042ca3310eSAlasdair G Kergon 		unlock_fs(md);
26052e93ccc1SKiyoshi Ueda 		goto out; /* pushback list is already flushed, so skip flush */
26062ca3310eSAlasdair G Kergon 	}
26072ca3310eSAlasdair G Kergon 
26083b00b203SMikulas Patocka 	/*
26093b00b203SMikulas Patocka 	 * If dm_wait_for_completion returned 0, the device is completely
26103b00b203SMikulas Patocka 	 * quiescent now. There is no request-processing activity. All new
26113b00b203SMikulas Patocka 	 * requests are being added to md->deferred list.
26123b00b203SMikulas Patocka 	 */
26133b00b203SMikulas Patocka 
26141da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
26151da177e4SLinus Torvalds 
26164d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
26174d4471cbSKiyoshi Ueda 
26182ca3310eSAlasdair G Kergon out:
26191da177e4SLinus Torvalds 	dm_table_put(map);
2620d287483dSAlasdair G Kergon 
2621d287483dSAlasdair G Kergon out_unlock:
2622e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2623cf222b37SAlasdair G Kergon 	return r;
26241da177e4SLinus Torvalds }
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds int dm_resume(struct mapped_device *md)
26271da177e4SLinus Torvalds {
2628cf222b37SAlasdair G Kergon 	int r = -EINVAL;
2629cf222b37SAlasdair G Kergon 	struct dm_table *map = NULL;
26301da177e4SLinus Torvalds 
2631e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
26324f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
2633cf222b37SAlasdair G Kergon 		goto out;
2634cf222b37SAlasdair G Kergon 
26357c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
26362ca3310eSAlasdair G Kergon 	if (!map || !dm_table_get_size(map))
2637cf222b37SAlasdair G Kergon 		goto out;
26381da177e4SLinus Torvalds 
26398757b776SMilan Broz 	r = dm_table_resume_targets(map);
26408757b776SMilan Broz 	if (r)
26418757b776SMilan Broz 		goto out;
26422ca3310eSAlasdair G Kergon 
26439a1fb464SMikulas Patocka 	dm_queue_flush(md);
26442ca3310eSAlasdair G Kergon 
2645cec47e3dSKiyoshi Ueda 	/*
2646cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2647cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2648cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2649cec47e3dSKiyoshi Ueda 	 */
2650cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2651cec47e3dSKiyoshi Ueda 		start_queue(md->queue);
2652cec47e3dSKiyoshi Ueda 
26532ca3310eSAlasdair G Kergon 	unlock_fs(md);
26542ca3310eSAlasdair G Kergon 
26552ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
26562ca3310eSAlasdair G Kergon 
2657cf222b37SAlasdair G Kergon 	r = 0;
2658cf222b37SAlasdair G Kergon out:
2659cf222b37SAlasdair G Kergon 	dm_table_put(map);
2660e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
26612ca3310eSAlasdair G Kergon 
2662cf222b37SAlasdair G Kergon 	return r;
26631da177e4SLinus Torvalds }
26641da177e4SLinus Torvalds 
26651da177e4SLinus Torvalds /*-----------------------------------------------------------------
26661da177e4SLinus Torvalds  * Event notification.
26671da177e4SLinus Torvalds  *---------------------------------------------------------------*/
26683abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
266960935eb2SMilan Broz 		       unsigned cookie)
267069267a30SAlasdair G Kergon {
267160935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
267260935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
267360935eb2SMilan Broz 
267460935eb2SMilan Broz 	if (!cookie)
26753abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
267660935eb2SMilan Broz 	else {
267760935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
267860935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
26793abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
26803abf85b5SPeter Rajnoha 					  action, envp);
268160935eb2SMilan Broz 	}
268269267a30SAlasdair G Kergon }
268369267a30SAlasdair G Kergon 
26847a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
26857a8c3d3bSMike Anderson {
26867a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
26877a8c3d3bSMike Anderson }
26887a8c3d3bSMike Anderson 
26891da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
26901da177e4SLinus Torvalds {
26911da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
26921da177e4SLinus Torvalds }
26931da177e4SLinus Torvalds 
26941da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
26951da177e4SLinus Torvalds {
26961da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
26971da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
26981da177e4SLinus Torvalds }
26991da177e4SLinus Torvalds 
27007a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
27017a8c3d3bSMike Anderson {
27027a8c3d3bSMike Anderson 	unsigned long flags;
27037a8c3d3bSMike Anderson 
27047a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
27057a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
27067a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
27077a8c3d3bSMike Anderson }
27087a8c3d3bSMike Anderson 
27091da177e4SLinus Torvalds /*
27101da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
27111da177e4SLinus Torvalds  * count on 'md'.
27121da177e4SLinus Torvalds  */
27131da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
27141da177e4SLinus Torvalds {
27151da177e4SLinus Torvalds 	return md->disk;
27161da177e4SLinus Torvalds }
27171da177e4SLinus Torvalds 
2718784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2719784aae73SMilan Broz {
2720784aae73SMilan Broz 	return &md->kobj;
2721784aae73SMilan Broz }
2722784aae73SMilan Broz 
2723784aae73SMilan Broz /*
2724784aae73SMilan Broz  * struct mapped_device should not be exported outside of dm.c
2725784aae73SMilan Broz  * so use this check to verify that kobj is part of md structure
2726784aae73SMilan Broz  */
2727784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2728784aae73SMilan Broz {
2729784aae73SMilan Broz 	struct mapped_device *md;
2730784aae73SMilan Broz 
2731784aae73SMilan Broz 	md = container_of(kobj, struct mapped_device, kobj);
2732784aae73SMilan Broz 	if (&md->kobj != kobj)
2733784aae73SMilan Broz 		return NULL;
2734784aae73SMilan Broz 
27354d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
2736432a212cSMike Anderson 	    dm_deleting_md(md))
27374d89b7b4SMilan Broz 		return NULL;
27384d89b7b4SMilan Broz 
2739784aae73SMilan Broz 	dm_get(md);
2740784aae73SMilan Broz 	return md;
2741784aae73SMilan Broz }
2742784aae73SMilan Broz 
27434f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
27441da177e4SLinus Torvalds {
27451da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
27461da177e4SLinus Torvalds }
27471da177e4SLinus Torvalds 
274864dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
274964dbce58SKiyoshi Ueda {
2750ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
275164dbce58SKiyoshi Ueda }
275264dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
275364dbce58SKiyoshi Ueda 
27542e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
27552e93ccc1SKiyoshi Ueda {
2756ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
27572e93ccc1SKiyoshi Ueda }
27582e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
27592e93ccc1SKiyoshi Ueda 
2760c0820cf5SMikulas Patocka struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity, unsigned per_bio_data_size)
2761e6ee8c0bSKiyoshi Ueda {
2762e6ee8c0bSKiyoshi Ueda 	struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2763a91a2785SMartin K. Petersen 	unsigned int pool_size = (type == DM_TYPE_BIO_BASED) ? 16 : MIN_IOS;
2764e6ee8c0bSKiyoshi Ueda 
2765e6ee8c0bSKiyoshi Ueda 	if (!pools)
2766e6ee8c0bSKiyoshi Ueda 		return NULL;
2767e6ee8c0bSKiyoshi Ueda 
2768c0820cf5SMikulas Patocka 	per_bio_data_size = roundup(per_bio_data_size, __alignof__(struct dm_target_io));
2769c0820cf5SMikulas Patocka 
2770*23e5083bSJun'ichi Nomura 	pools->io_pool = NULL;
2771*23e5083bSJun'ichi Nomura 	if (type == DM_TYPE_BIO_BASED) {
2772*23e5083bSJun'ichi Nomura 		pools->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
2773e6ee8c0bSKiyoshi Ueda 		if (!pools->io_pool)
2774e6ee8c0bSKiyoshi Ueda 			goto free_pools_and_out;
2775*23e5083bSJun'ichi Nomura 	}
2776e6ee8c0bSKiyoshi Ueda 
2777dba14160SMikulas Patocka 	pools->tio_pool = NULL;
2778dba14160SMikulas Patocka 	if (type == DM_TYPE_REQUEST_BASED) {
2779dba14160SMikulas Patocka 		pools->tio_pool = mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2780e6ee8c0bSKiyoshi Ueda 		if (!pools->tio_pool)
2781e6ee8c0bSKiyoshi Ueda 			goto free_io_pool_and_out;
2782dba14160SMikulas Patocka 	}
2783e6ee8c0bSKiyoshi Ueda 
278494818742SKent Overstreet 	pools->bs = (type == DM_TYPE_BIO_BASED) ?
2785dba14160SMikulas Patocka 		bioset_create(pool_size,
2786c0820cf5SMikulas Patocka 			      per_bio_data_size + offsetof(struct dm_target_io, clone)) :
278794818742SKent Overstreet 		bioset_create(pool_size,
278894818742SKent Overstreet 			      offsetof(struct dm_rq_clone_bio_info, clone));
2789e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
2790e6ee8c0bSKiyoshi Ueda 		goto free_tio_pool_and_out;
2791e6ee8c0bSKiyoshi Ueda 
2792a91a2785SMartin K. Petersen 	if (integrity && bioset_integrity_create(pools->bs, pool_size))
2793a91a2785SMartin K. Petersen 		goto free_bioset_and_out;
2794a91a2785SMartin K. Petersen 
2795e6ee8c0bSKiyoshi Ueda 	return pools;
2796e6ee8c0bSKiyoshi Ueda 
2797a91a2785SMartin K. Petersen free_bioset_and_out:
2798a91a2785SMartin K. Petersen 	bioset_free(pools->bs);
2799a91a2785SMartin K. Petersen 
2800e6ee8c0bSKiyoshi Ueda free_tio_pool_and_out:
2801dba14160SMikulas Patocka 	if (pools->tio_pool)
2802e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->tio_pool);
2803e6ee8c0bSKiyoshi Ueda 
2804e6ee8c0bSKiyoshi Ueda free_io_pool_and_out:
2805*23e5083bSJun'ichi Nomura 	if (pools->io_pool)
2806e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->io_pool);
2807e6ee8c0bSKiyoshi Ueda 
2808e6ee8c0bSKiyoshi Ueda free_pools_and_out:
2809e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2810e6ee8c0bSKiyoshi Ueda 
2811e6ee8c0bSKiyoshi Ueda 	return NULL;
2812e6ee8c0bSKiyoshi Ueda }
2813e6ee8c0bSKiyoshi Ueda 
2814e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
2815e6ee8c0bSKiyoshi Ueda {
2816e6ee8c0bSKiyoshi Ueda 	if (!pools)
2817e6ee8c0bSKiyoshi Ueda 		return;
2818e6ee8c0bSKiyoshi Ueda 
2819e6ee8c0bSKiyoshi Ueda 	if (pools->io_pool)
2820e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->io_pool);
2821e6ee8c0bSKiyoshi Ueda 
2822e6ee8c0bSKiyoshi Ueda 	if (pools->tio_pool)
2823e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->tio_pool);
2824e6ee8c0bSKiyoshi Ueda 
2825e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
2826e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
2827e6ee8c0bSKiyoshi Ueda 
2828e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2829e6ee8c0bSKiyoshi Ueda }
2830e6ee8c0bSKiyoshi Ueda 
283183d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
28321da177e4SLinus Torvalds 	.open = dm_blk_open,
28331da177e4SLinus Torvalds 	.release = dm_blk_close,
2834aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
28353ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
28361da177e4SLinus Torvalds 	.owner = THIS_MODULE
28371da177e4SLinus Torvalds };
28381da177e4SLinus Torvalds 
28391da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo);
28401da177e4SLinus Torvalds 
28411da177e4SLinus Torvalds /*
28421da177e4SLinus Torvalds  * module hooks
28431da177e4SLinus Torvalds  */
28441da177e4SLinus Torvalds module_init(dm_init);
28451da177e4SLinus Torvalds module_exit(dm_exit);
28461da177e4SLinus Torvalds 
28471da177e4SLinus Torvalds module_param(major, uint, 0);
28481da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
28491da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
28501da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
28511da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2852