xref: /openbmc/linux/drivers/md/dm.c (revision a91a2785b200864aef2270ed6a3babac7a253a20)
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/buffer_head.h>
181da177e4SLinus Torvalds #include <linux/mempool.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/idr.h>
213ac51e74SDarrick J. Wong #include <linux/hdreg.h>
223f77316dSKiyoshi Ueda #include <linux/delay.h>
2355782138SLi Zefan 
2455782138SLi Zefan #include <trace/events/block.h>
251da177e4SLinus Torvalds 
2672d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
2772d94861SAlasdair G Kergon 
2860935eb2SMilan Broz /*
2960935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
3060935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
3160935eb2SMilan Broz  */
3260935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
3360935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
3460935eb2SMilan Broz 
351da177e4SLinus Torvalds static const char *_name = DM_NAME;
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds static unsigned int major = 0;
381da177e4SLinus Torvalds static unsigned int _major = 0;
391da177e4SLinus Torvalds 
40f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
411da177e4SLinus Torvalds /*
428fbf26adSKiyoshi Ueda  * For bio-based dm.
431da177e4SLinus Torvalds  * One of these is allocated per bio.
441da177e4SLinus Torvalds  */
451da177e4SLinus Torvalds struct dm_io {
461da177e4SLinus Torvalds 	struct mapped_device *md;
471da177e4SLinus Torvalds 	int error;
481da177e4SLinus Torvalds 	atomic_t io_count;
496ae2fa67SRichard Kennedy 	struct bio *bio;
503eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
51f88fb981SKiyoshi Ueda 	spinlock_t endio_lock;
521da177e4SLinus Torvalds };
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds /*
558fbf26adSKiyoshi Ueda  * For bio-based dm.
561da177e4SLinus Torvalds  * One of these is allocated per target within a bio.  Hopefully
571da177e4SLinus Torvalds  * this will be simplified out one day.
581da177e4SLinus Torvalds  */
59028867acSAlasdair G Kergon struct dm_target_io {
601da177e4SLinus Torvalds 	struct dm_io *io;
611da177e4SLinus Torvalds 	struct dm_target *ti;
621da177e4SLinus Torvalds 	union map_info info;
631da177e4SLinus Torvalds };
641da177e4SLinus Torvalds 
658fbf26adSKiyoshi Ueda /*
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 /*
788fbf26adSKiyoshi Ueda  * For request-based dm.
798fbf26adSKiyoshi Ueda  * One of these is allocated per bio.
808fbf26adSKiyoshi Ueda  */
818fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info {
828fbf26adSKiyoshi Ueda 	struct bio *orig;
83cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio;
848fbf26adSKiyoshi Ueda };
858fbf26adSKiyoshi Ueda 
861da177e4SLinus Torvalds union map_info *dm_get_mapinfo(struct bio *bio)
871da177e4SLinus Torvalds {
881da177e4SLinus Torvalds 	if (bio && bio->bi_private)
89028867acSAlasdair G Kergon 		return &((struct dm_target_io *)bio->bi_private)->info;
901da177e4SLinus Torvalds 	return NULL;
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds 
93cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq)
94cec47e3dSKiyoshi Ueda {
95cec47e3dSKiyoshi Ueda 	if (rq && rq->end_io_data)
96cec47e3dSKiyoshi Ueda 		return &((struct dm_rq_target_io *)rq->end_io_data)->info;
97cec47e3dSKiyoshi Ueda 	return NULL;
98cec47e3dSKiyoshi Ueda }
99cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
100cec47e3dSKiyoshi Ueda 
101ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
102ba61fdd1SJeff Mahoney 
1031da177e4SLinus Torvalds /*
1041da177e4SLinus Torvalds  * Bits for the md->flags field.
1051da177e4SLinus Torvalds  */
1061eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
1071da177e4SLinus Torvalds #define DMF_SUSPENDED 1
108aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
109fba9f90eSJeff Mahoney #define DMF_FREEING 3
1105c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
1112e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
1121da177e4SLinus Torvalds 
113304f3f6aSMilan Broz /*
114304f3f6aSMilan Broz  * Work processed by per-device workqueue.
115304f3f6aSMilan Broz  */
1161da177e4SLinus Torvalds struct mapped_device {
1172ca3310eSAlasdair G Kergon 	struct rw_semaphore io_lock;
118e61290a4SDaniel Walker 	struct mutex suspend_lock;
1191da177e4SLinus Torvalds 	rwlock_t map_lock;
1201da177e4SLinus Torvalds 	atomic_t holders;
1215c6bd75dSAlasdair G Kergon 	atomic_t open_count;
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	unsigned long flags;
1241da177e4SLinus Torvalds 
125165125e1SJens Axboe 	struct request_queue *queue;
126a5664dadSMike Snitzer 	unsigned type;
1274a0b4ddfSMike Snitzer 	/* Protect queue and type against concurrent access. */
128a5664dadSMike Snitzer 	struct mutex type_lock;
129a5664dadSMike Snitzer 
1301da177e4SLinus Torvalds 	struct gendisk *disk;
1317e51f257SMike Anderson 	char name[16];
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	void *interface_ptr;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	/*
1361da177e4SLinus Torvalds 	 * A list of ios that arrived while we were suspended.
1371da177e4SLinus Torvalds 	 */
138316d315bSNikanth Karthikesan 	atomic_t pending[2];
1391da177e4SLinus Torvalds 	wait_queue_head_t wait;
14053d5914fSMikulas Patocka 	struct work_struct work;
1411da177e4SLinus Torvalds 	struct bio_list deferred;
142022c2611SMikulas Patocka 	spinlock_t deferred_lock;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	/*
14529e4013dSTejun Heo 	 * Processing queue (flush)
146304f3f6aSMilan Broz 	 */
147304f3f6aSMilan Broz 	struct workqueue_struct *wq;
148304f3f6aSMilan Broz 
149304f3f6aSMilan Broz 	/*
1501da177e4SLinus Torvalds 	 * The current mapping.
1511da177e4SLinus Torvalds 	 */
1521da177e4SLinus Torvalds 	struct dm_table *map;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	/*
1551da177e4SLinus Torvalds 	 * io objects are allocated from here.
1561da177e4SLinus Torvalds 	 */
1571da177e4SLinus Torvalds 	mempool_t *io_pool;
1581da177e4SLinus Torvalds 	mempool_t *tio_pool;
1591da177e4SLinus Torvalds 
1609faf400fSStefan Bader 	struct bio_set *bs;
1619faf400fSStefan Bader 
1621da177e4SLinus Torvalds 	/*
1631da177e4SLinus Torvalds 	 * Event handling.
1641da177e4SLinus Torvalds 	 */
1651da177e4SLinus Torvalds 	atomic_t event_nr;
1661da177e4SLinus Torvalds 	wait_queue_head_t eventq;
1677a8c3d3bSMike Anderson 	atomic_t uevent_seq;
1687a8c3d3bSMike Anderson 	struct list_head uevent_list;
1697a8c3d3bSMike Anderson 	spinlock_t uevent_lock; /* Protect access to uevent_list */
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	/*
1721da177e4SLinus Torvalds 	 * freeze/thaw support require holding onto a super block
1731da177e4SLinus Torvalds 	 */
1741da177e4SLinus Torvalds 	struct super_block *frozen_sb;
175db8fef4fSMikulas Patocka 	struct block_device *bdev;
1763ac51e74SDarrick J. Wong 
1773ac51e74SDarrick J. Wong 	/* forced geometry settings */
1783ac51e74SDarrick J. Wong 	struct hd_geometry geometry;
179784aae73SMilan Broz 
180cec47e3dSKiyoshi Ueda 	/* For saving the address of __make_request for request based dm */
181cec47e3dSKiyoshi Ueda 	make_request_fn *saved_make_request_fn;
182cec47e3dSKiyoshi Ueda 
183784aae73SMilan Broz 	/* sysfs handle */
184784aae73SMilan Broz 	struct kobject kobj;
18552b1fd5aSMikulas Patocka 
186d87f4c14STejun Heo 	/* zero-length flush that will be cloned and submitted to targets */
187d87f4c14STejun Heo 	struct bio flush_bio;
1881da177e4SLinus Torvalds };
1891da177e4SLinus Torvalds 
190e6ee8c0bSKiyoshi Ueda /*
191e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
192e6ee8c0bSKiyoshi Ueda  */
193e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
194e6ee8c0bSKiyoshi Ueda 	mempool_t *io_pool;
195e6ee8c0bSKiyoshi Ueda 	mempool_t *tio_pool;
196e6ee8c0bSKiyoshi Ueda 	struct bio_set *bs;
197e6ee8c0bSKiyoshi Ueda };
198e6ee8c0bSKiyoshi Ueda 
1991da177e4SLinus Torvalds #define MIN_IOS 256
200e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
201e18b890bSChristoph Lameter static struct kmem_cache *_tio_cache;
2028fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
2038fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_bio_info_cache;
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds static int __init local_init(void)
2061da177e4SLinus Torvalds {
20751157b4aSKiyoshi Ueda 	int r = -ENOMEM;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
210028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
2111da177e4SLinus Torvalds 	if (!_io_cache)
21251157b4aSKiyoshi Ueda 		return r;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	/* allocate a slab for the target ios */
215028867acSAlasdair G Kergon 	_tio_cache = KMEM_CACHE(dm_target_io, 0);
21651157b4aSKiyoshi Ueda 	if (!_tio_cache)
21751157b4aSKiyoshi Ueda 		goto out_free_io_cache;
2181da177e4SLinus Torvalds 
2198fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
2208fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
2218fbf26adSKiyoshi Ueda 		goto out_free_tio_cache;
2228fbf26adSKiyoshi Ueda 
2238fbf26adSKiyoshi Ueda 	_rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
2248fbf26adSKiyoshi Ueda 	if (!_rq_bio_info_cache)
2258fbf26adSKiyoshi Ueda 		goto out_free_rq_tio_cache;
2268fbf26adSKiyoshi Ueda 
22751e5b2bdSMike Anderson 	r = dm_uevent_init();
22851157b4aSKiyoshi Ueda 	if (r)
2298fbf26adSKiyoshi Ueda 		goto out_free_rq_bio_info_cache;
23051e5b2bdSMike Anderson 
2311da177e4SLinus Torvalds 	_major = major;
2321da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
23351157b4aSKiyoshi Ueda 	if (r < 0)
23451157b4aSKiyoshi Ueda 		goto out_uevent_exit;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	if (!_major)
2371da177e4SLinus Torvalds 		_major = r;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	return 0;
24051157b4aSKiyoshi Ueda 
24151157b4aSKiyoshi Ueda out_uevent_exit:
24251157b4aSKiyoshi Ueda 	dm_uevent_exit();
2438fbf26adSKiyoshi Ueda out_free_rq_bio_info_cache:
2448fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_bio_info_cache);
2458fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
2468fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
24751157b4aSKiyoshi Ueda out_free_tio_cache:
24851157b4aSKiyoshi Ueda 	kmem_cache_destroy(_tio_cache);
24951157b4aSKiyoshi Ueda out_free_io_cache:
25051157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
25151157b4aSKiyoshi Ueda 
25251157b4aSKiyoshi Ueda 	return r;
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds static void local_exit(void)
2561da177e4SLinus Torvalds {
2578fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_bio_info_cache);
2588fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
2591da177e4SLinus Torvalds 	kmem_cache_destroy(_tio_cache);
2601da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
26100d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
26251e5b2bdSMike Anderson 	dm_uevent_exit();
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	_major = 0;
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	DMINFO("cleaned up");
2671da177e4SLinus Torvalds }
2681da177e4SLinus Torvalds 
269b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
2701da177e4SLinus Torvalds 	local_init,
2711da177e4SLinus Torvalds 	dm_target_init,
2721da177e4SLinus Torvalds 	dm_linear_init,
2731da177e4SLinus Torvalds 	dm_stripe_init,
274952b3557SMikulas Patocka 	dm_io_init,
275945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2761da177e4SLinus Torvalds 	dm_interface_init,
2771da177e4SLinus Torvalds };
2781da177e4SLinus Torvalds 
279b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2801da177e4SLinus Torvalds 	local_exit,
2811da177e4SLinus Torvalds 	dm_target_exit,
2821da177e4SLinus Torvalds 	dm_linear_exit,
2831da177e4SLinus Torvalds 	dm_stripe_exit,
284952b3557SMikulas Patocka 	dm_io_exit,
285945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
2861da177e4SLinus Torvalds 	dm_interface_exit,
2871da177e4SLinus Torvalds };
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds static int __init dm_init(void)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	int r, i;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
2961da177e4SLinus Torvalds 		r = _inits[i]();
2971da177e4SLinus Torvalds 		if (r)
2981da177e4SLinus Torvalds 			goto bad;
2991da177e4SLinus Torvalds 	}
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	return 0;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds       bad:
3041da177e4SLinus Torvalds 	while (i--)
3051da177e4SLinus Torvalds 		_exits[i]();
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	return r;
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds static void __exit dm_exit(void)
3111da177e4SLinus Torvalds {
3121da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	while (i--)
3151da177e4SLinus Torvalds 		_exits[i]();
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds /*
3191da177e4SLinus Torvalds  * Block device functions
3201da177e4SLinus Torvalds  */
321432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
322432a212cSMike Anderson {
323432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
324432a212cSMike Anderson }
325432a212cSMike Anderson 
326fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	struct mapped_device *md;
3291da177e4SLinus Torvalds 
330fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
331fba9f90eSJeff Mahoney 
332fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
333fba9f90eSJeff Mahoney 	if (!md)
334fba9f90eSJeff Mahoney 		goto out;
335fba9f90eSJeff Mahoney 
3365c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
337432a212cSMike Anderson 	    dm_deleting_md(md)) {
338fba9f90eSJeff Mahoney 		md = NULL;
339fba9f90eSJeff Mahoney 		goto out;
340fba9f90eSJeff Mahoney 	}
341fba9f90eSJeff Mahoney 
3421da177e4SLinus Torvalds 	dm_get(md);
3435c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
344fba9f90eSJeff Mahoney 
345fba9f90eSJeff Mahoney out:
346fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
347fba9f90eSJeff Mahoney 
348fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds 
351fe5f9f2cSAl Viro static int dm_blk_close(struct gendisk *disk, fmode_t mode)
3521da177e4SLinus Torvalds {
353fe5f9f2cSAl Viro 	struct mapped_device *md = disk->private_data;
3546e9624b8SArnd Bergmann 
3554a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
3564a1aeb98SMilan Broz 
3575c6bd75dSAlasdair G Kergon 	atomic_dec(&md->open_count);
3581da177e4SLinus Torvalds 	dm_put(md);
3594a1aeb98SMilan Broz 
3604a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
3616e9624b8SArnd Bergmann 
3621da177e4SLinus Torvalds 	return 0;
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds 
3655c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3665c6bd75dSAlasdair G Kergon {
3675c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3685c6bd75dSAlasdair G Kergon }
3695c6bd75dSAlasdair G Kergon 
3705c6bd75dSAlasdair G Kergon /*
3715c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3725c6bd75dSAlasdair G Kergon  */
3735c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md)
3745c6bd75dSAlasdair G Kergon {
3755c6bd75dSAlasdair G Kergon 	int r = 0;
3765c6bd75dSAlasdair G Kergon 
3775c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
3785c6bd75dSAlasdair G Kergon 
3795c6bd75dSAlasdair G Kergon 	if (dm_open_count(md))
3805c6bd75dSAlasdair G Kergon 		r = -EBUSY;
3815c6bd75dSAlasdair G Kergon 	else
3825c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
3835c6bd75dSAlasdair G Kergon 
3845c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
3855c6bd75dSAlasdair G Kergon 
3865c6bd75dSAlasdair G Kergon 	return r;
3875c6bd75dSAlasdair G Kergon }
3885c6bd75dSAlasdair G Kergon 
3893ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3903ac51e74SDarrick J. Wong {
3913ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
3923ac51e74SDarrick J. Wong 
3933ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
3943ac51e74SDarrick J. Wong }
3953ac51e74SDarrick J. Wong 
396fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
397aa129a22SMilan Broz 			unsigned int cmd, unsigned long arg)
398aa129a22SMilan Broz {
399fe5f9f2cSAl Viro 	struct mapped_device *md = bdev->bd_disk->private_data;
4007c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
401aa129a22SMilan Broz 	struct dm_target *tgt;
402aa129a22SMilan Broz 	int r = -ENOTTY;
403aa129a22SMilan Broz 
404aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
405aa129a22SMilan Broz 		goto out;
406aa129a22SMilan Broz 
407aa129a22SMilan Broz 	/* We only support devices that have a single target */
408aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
409aa129a22SMilan Broz 		goto out;
410aa129a22SMilan Broz 
411aa129a22SMilan Broz 	tgt = dm_table_get_target(map, 0);
412aa129a22SMilan Broz 
4134f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
414aa129a22SMilan Broz 		r = -EAGAIN;
415aa129a22SMilan Broz 		goto out;
416aa129a22SMilan Broz 	}
417aa129a22SMilan Broz 
418aa129a22SMilan Broz 	if (tgt->type->ioctl)
419647b3d00SAl Viro 		r = tgt->type->ioctl(tgt, cmd, arg);
420aa129a22SMilan Broz 
421aa129a22SMilan Broz out:
422aa129a22SMilan Broz 	dm_table_put(map);
423aa129a22SMilan Broz 
424aa129a22SMilan Broz 	return r;
425aa129a22SMilan Broz }
426aa129a22SMilan Broz 
427028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
4281da177e4SLinus Torvalds {
4291da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
4301da177e4SLinus Torvalds }
4311da177e4SLinus Torvalds 
432028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
4331da177e4SLinus Torvalds {
4341da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
437028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
4381da177e4SLinus Torvalds {
4391da177e4SLinus Torvalds 	mempool_free(tio, md->tio_pool);
4401da177e4SLinus Torvalds }
4411da177e4SLinus Torvalds 
44208885643SKiyoshi Ueda static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
44308885643SKiyoshi Ueda 					    gfp_t gfp_mask)
444cec47e3dSKiyoshi Ueda {
44508885643SKiyoshi Ueda 	return mempool_alloc(md->tio_pool, gfp_mask);
446cec47e3dSKiyoshi Ueda }
447cec47e3dSKiyoshi Ueda 
448cec47e3dSKiyoshi Ueda static void free_rq_tio(struct dm_rq_target_io *tio)
449cec47e3dSKiyoshi Ueda {
450cec47e3dSKiyoshi Ueda 	mempool_free(tio, tio->md->tio_pool);
451cec47e3dSKiyoshi Ueda }
452cec47e3dSKiyoshi Ueda 
453cec47e3dSKiyoshi Ueda static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
454cec47e3dSKiyoshi Ueda {
455cec47e3dSKiyoshi Ueda 	return mempool_alloc(md->io_pool, GFP_ATOMIC);
456cec47e3dSKiyoshi Ueda }
457cec47e3dSKiyoshi Ueda 
458cec47e3dSKiyoshi Ueda static void free_bio_info(struct dm_rq_clone_bio_info *info)
459cec47e3dSKiyoshi Ueda {
460cec47e3dSKiyoshi Ueda 	mempool_free(info, info->tio->md->io_pool);
461cec47e3dSKiyoshi Ueda }
462cec47e3dSKiyoshi Ueda 
46390abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md)
46490abb8c4SKiyoshi Ueda {
46590abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
46690abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
46790abb8c4SKiyoshi Ueda }
46890abb8c4SKiyoshi Ueda 
4693eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
4703eaf840eSJun'ichi "Nick" Nomura {
4713eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
472c9959059STejun Heo 	int cpu;
473316d315bSNikanth Karthikesan 	int rw = bio_data_dir(io->bio);
4743eaf840eSJun'ichi "Nick" Nomura 
4753eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
4763eaf840eSJun'ichi "Nick" Nomura 
477074a7acaSTejun Heo 	cpu = part_stat_lock();
478074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
479074a7acaSTejun Heo 	part_stat_unlock();
480316d315bSNikanth Karthikesan 	dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
4813eaf840eSJun'ichi "Nick" Nomura }
4823eaf840eSJun'ichi "Nick" Nomura 
483d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
4843eaf840eSJun'ichi "Nick" Nomura {
4853eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
4863eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
4873eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
488c9959059STejun Heo 	int pending, cpu;
4893eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
4903eaf840eSJun'ichi "Nick" Nomura 
491074a7acaSTejun Heo 	cpu = part_stat_lock();
492074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
493074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
494074a7acaSTejun Heo 	part_stat_unlock();
4953eaf840eSJun'ichi "Nick" Nomura 
496af7e466aSMikulas Patocka 	/*
497af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
498d87f4c14STejun Heo 	 * a flush.
499af7e466aSMikulas Patocka 	 */
500316d315bSNikanth Karthikesan 	dm_disk(md)->part0.in_flight[rw] = pending =
501316d315bSNikanth Karthikesan 		atomic_dec_return(&md->pending[rw]);
502316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
5033eaf840eSJun'ichi "Nick" Nomura 
504d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
505d221d2e7SMikulas Patocka 	if (!pending)
506d221d2e7SMikulas Patocka 		wake_up(&md->wait);
5073eaf840eSJun'ichi "Nick" Nomura }
5083eaf840eSJun'ichi "Nick" Nomura 
5091da177e4SLinus Torvalds /*
5101da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
5111da177e4SLinus Torvalds  */
51292c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
5131da177e4SLinus Torvalds {
51405447420SKiyoshi Ueda 	unsigned long flags;
5151da177e4SLinus Torvalds 
51605447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
5171da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
51805447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
51992c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
5201da177e4SLinus Torvalds }
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds /*
5231da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
5241da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
5251da177e4SLinus Torvalds  * dm_table_put() when finished.
5261da177e4SLinus Torvalds  */
5277c666411SAlasdair G Kergon struct dm_table *dm_get_live_table(struct mapped_device *md)
5281da177e4SLinus Torvalds {
5291da177e4SLinus Torvalds 	struct dm_table *t;
530523d9297SKiyoshi Ueda 	unsigned long flags;
5311da177e4SLinus Torvalds 
532523d9297SKiyoshi Ueda 	read_lock_irqsave(&md->map_lock, flags);
5331da177e4SLinus Torvalds 	t = md->map;
5341da177e4SLinus Torvalds 	if (t)
5351da177e4SLinus Torvalds 		dm_table_get(t);
536523d9297SKiyoshi Ueda 	read_unlock_irqrestore(&md->map_lock, flags);
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	return t;
5391da177e4SLinus Torvalds }
5401da177e4SLinus Torvalds 
5413ac51e74SDarrick J. Wong /*
5423ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
5433ac51e74SDarrick J. Wong  */
5443ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
5453ac51e74SDarrick J. Wong {
5463ac51e74SDarrick J. Wong 	*geo = md->geometry;
5473ac51e74SDarrick J. Wong 
5483ac51e74SDarrick J. Wong 	return 0;
5493ac51e74SDarrick J. Wong }
5503ac51e74SDarrick J. Wong 
5513ac51e74SDarrick J. Wong /*
5523ac51e74SDarrick J. Wong  * Set the geometry of a device.
5533ac51e74SDarrick J. Wong  */
5543ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
5553ac51e74SDarrick J. Wong {
5563ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
5573ac51e74SDarrick J. Wong 
5583ac51e74SDarrick J. Wong 	if (geo->start > sz) {
5593ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
5603ac51e74SDarrick J. Wong 		return -EINVAL;
5613ac51e74SDarrick J. Wong 	}
5623ac51e74SDarrick J. Wong 
5633ac51e74SDarrick J. Wong 	md->geometry = *geo;
5643ac51e74SDarrick J. Wong 
5653ac51e74SDarrick J. Wong 	return 0;
5663ac51e74SDarrick J. Wong }
5673ac51e74SDarrick J. Wong 
5681da177e4SLinus Torvalds /*-----------------------------------------------------------------
5691da177e4SLinus Torvalds  * CRUD START:
5701da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
5711da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
5721da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
5731da177e4SLinus Torvalds  *   interests of getting something for people to use I give
5741da177e4SLinus Torvalds  *   you this clearly demarcated crap.
5751da177e4SLinus Torvalds  *---------------------------------------------------------------*/
5761da177e4SLinus Torvalds 
5772e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
5782e93ccc1SKiyoshi Ueda {
5792e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
5802e93ccc1SKiyoshi Ueda }
5812e93ccc1SKiyoshi Ueda 
5821da177e4SLinus Torvalds /*
5831da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
5841da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
5851da177e4SLinus Torvalds  */
586858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
5871da177e4SLinus Torvalds {
5882e93ccc1SKiyoshi Ueda 	unsigned long flags;
589b35f8caaSMilan Broz 	int io_error;
590b35f8caaSMilan Broz 	struct bio *bio;
591b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
5922e93ccc1SKiyoshi Ueda 
5932e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
594f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
595f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
596f88fb981SKiyoshi Ueda 		if (!(io->error > 0 && __noflush_suspending(md)))
5971da177e4SLinus Torvalds 			io->error = error;
598f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
599f88fb981SKiyoshi Ueda 	}
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
6022e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
6032e93ccc1SKiyoshi Ueda 			/*
6042e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
6052e93ccc1SKiyoshi Ueda 			 */
606022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
6076a8736d1STejun Heo 			if (__noflush_suspending(md))
6086a8736d1STejun Heo 				bio_list_add_head(&md->deferred, io->bio);
6096a8736d1STejun Heo 			else
6102e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
6112e93ccc1SKiyoshi Ueda 				io->error = -EIO;
612022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
6132e93ccc1SKiyoshi Ueda 		}
6142e93ccc1SKiyoshi Ueda 
615b35f8caaSMilan Broz 		io_error = io->error;
616b35f8caaSMilan Broz 		bio = io->bio;
617af7e466aSMikulas Patocka 		end_io_acct(io);
618a97f925aSMikulas Patocka 		free_io(md, io);
6191da177e4SLinus Torvalds 
6206a8736d1STejun Heo 		if (io_error == DM_ENDIO_REQUEUE)
6216a8736d1STejun Heo 			return;
6226a8736d1STejun Heo 
623b372d360SMike Snitzer 		if ((bio->bi_rw & REQ_FLUSH) && bio->bi_size) {
6241da177e4SLinus Torvalds 			/*
6256a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
6266a8736d1STejun Heo 			 * without REQ_FLUSH.
6271da177e4SLinus Torvalds 			 */
6286a8736d1STejun Heo 			bio->bi_rw &= ~REQ_FLUSH;
6296a8736d1STejun Heo 			queue_io(md, bio);
6305f3ea37cSArnaldo Carvalho de Melo 		} else {
631b372d360SMike Snitzer 			/* done with normal IO or empty flush */
632b7908c10SJeff Moyer 			trace_block_bio_complete(md->queue, bio, io_error);
633b35f8caaSMilan Broz 			bio_endio(bio, io_error);
6342e93ccc1SKiyoshi Ueda 		}
6351da177e4SLinus Torvalds 	}
636af7e466aSMikulas Patocka }
6371da177e4SLinus Torvalds 
6386712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error)
6391da177e4SLinus Torvalds {
6401da177e4SLinus Torvalds 	int r = 0;
641028867acSAlasdair G Kergon 	struct dm_target_io *tio = bio->bi_private;
642b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
6439faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
6441da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	if (!bio_flagged(bio, BIO_UPTODATE) && !error)
6471da177e4SLinus Torvalds 		error = -EIO;
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	if (endio) {
6501da177e4SLinus Torvalds 		r = endio(tio->ti, bio, error, &tio->info);
6512e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
6522e93ccc1SKiyoshi Ueda 			/*
6532e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
6542e93ccc1SKiyoshi Ueda 			 * in dec_pending().
6552e93ccc1SKiyoshi Ueda 			 */
6561da177e4SLinus Torvalds 			error = r;
65745cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
65845cbcd79SKiyoshi Ueda 			/* The target will handle the io */
6596712ecf8SNeilBrown 			return;
66045cbcd79SKiyoshi Ueda 		else if (r) {
66145cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
66245cbcd79SKiyoshi Ueda 			BUG();
66345cbcd79SKiyoshi Ueda 		}
6641da177e4SLinus Torvalds 	}
6651da177e4SLinus Torvalds 
6669faf400fSStefan Bader 	/*
6679faf400fSStefan Bader 	 * Store md for cleanup instead of tio which is about to get freed.
6689faf400fSStefan Bader 	 */
6699faf400fSStefan Bader 	bio->bi_private = md->bs;
6709faf400fSStefan Bader 
6719faf400fSStefan Bader 	free_tio(md, tio);
672b35f8caaSMilan Broz 	bio_put(bio);
673b35f8caaSMilan Broz 	dec_pending(io, error);
6741da177e4SLinus Torvalds }
6751da177e4SLinus Torvalds 
676cec47e3dSKiyoshi Ueda /*
677cec47e3dSKiyoshi Ueda  * Partial completion handling for request-based dm
678cec47e3dSKiyoshi Ueda  */
679cec47e3dSKiyoshi Ueda static void end_clone_bio(struct bio *clone, int error)
680cec47e3dSKiyoshi Ueda {
681cec47e3dSKiyoshi Ueda 	struct dm_rq_clone_bio_info *info = clone->bi_private;
682cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = info->tio;
683cec47e3dSKiyoshi Ueda 	struct bio *bio = info->orig;
684cec47e3dSKiyoshi Ueda 	unsigned int nr_bytes = info->orig->bi_size;
685cec47e3dSKiyoshi Ueda 
686cec47e3dSKiyoshi Ueda 	bio_put(clone);
687cec47e3dSKiyoshi Ueda 
688cec47e3dSKiyoshi Ueda 	if (tio->error)
689cec47e3dSKiyoshi Ueda 		/*
690cec47e3dSKiyoshi Ueda 		 * An error has already been detected on the request.
691cec47e3dSKiyoshi Ueda 		 * Once error occurred, just let clone->end_io() handle
692cec47e3dSKiyoshi Ueda 		 * the remainder.
693cec47e3dSKiyoshi Ueda 		 */
694cec47e3dSKiyoshi Ueda 		return;
695cec47e3dSKiyoshi Ueda 	else if (error) {
696cec47e3dSKiyoshi Ueda 		/*
697cec47e3dSKiyoshi Ueda 		 * Don't notice the error to the upper layer yet.
698cec47e3dSKiyoshi Ueda 		 * The error handling decision is made by the target driver,
699cec47e3dSKiyoshi Ueda 		 * when the request is completed.
700cec47e3dSKiyoshi Ueda 		 */
701cec47e3dSKiyoshi Ueda 		tio->error = error;
702cec47e3dSKiyoshi Ueda 		return;
703cec47e3dSKiyoshi Ueda 	}
704cec47e3dSKiyoshi Ueda 
705cec47e3dSKiyoshi Ueda 	/*
706cec47e3dSKiyoshi Ueda 	 * I/O for the bio successfully completed.
707cec47e3dSKiyoshi Ueda 	 * Notice the data completion to the upper layer.
708cec47e3dSKiyoshi Ueda 	 */
709cec47e3dSKiyoshi Ueda 
710cec47e3dSKiyoshi Ueda 	/*
711cec47e3dSKiyoshi Ueda 	 * bios are processed from the head of the list.
712cec47e3dSKiyoshi Ueda 	 * So the completing bio should always be rq->bio.
713cec47e3dSKiyoshi Ueda 	 * If it's not, something wrong is happening.
714cec47e3dSKiyoshi Ueda 	 */
715cec47e3dSKiyoshi Ueda 	if (tio->orig->bio != bio)
716cec47e3dSKiyoshi Ueda 		DMERR("bio completion is going in the middle of the request");
717cec47e3dSKiyoshi Ueda 
718cec47e3dSKiyoshi Ueda 	/*
719cec47e3dSKiyoshi Ueda 	 * Update the original request.
720cec47e3dSKiyoshi Ueda 	 * Do not use blk_end_request() here, because it may complete
721cec47e3dSKiyoshi Ueda 	 * the original request before the clone, and break the ordering.
722cec47e3dSKiyoshi Ueda 	 */
723cec47e3dSKiyoshi Ueda 	blk_update_request(tio->orig, 0, nr_bytes);
724cec47e3dSKiyoshi Ueda }
725cec47e3dSKiyoshi Ueda 
726cec47e3dSKiyoshi Ueda /*
727cec47e3dSKiyoshi Ueda  * Don't touch any member of the md after calling this function because
728cec47e3dSKiyoshi Ueda  * the md may be freed in dm_put() at the end of this function.
729cec47e3dSKiyoshi Ueda  * Or do dm_get() before calling this function and dm_put() later.
730cec47e3dSKiyoshi Ueda  */
731b4324feeSKiyoshi Ueda static void rq_completed(struct mapped_device *md, int rw, int run_queue)
732cec47e3dSKiyoshi Ueda {
733b4324feeSKiyoshi Ueda 	atomic_dec(&md->pending[rw]);
734cec47e3dSKiyoshi Ueda 
735cec47e3dSKiyoshi Ueda 	/* nudge anyone waiting on suspend queue */
736b4324feeSKiyoshi Ueda 	if (!md_in_flight(md))
737cec47e3dSKiyoshi Ueda 		wake_up(&md->wait);
738cec47e3dSKiyoshi Ueda 
739cec47e3dSKiyoshi Ueda 	if (run_queue)
740b4324feeSKiyoshi Ueda 		blk_run_queue(md->queue);
741cec47e3dSKiyoshi Ueda 
742cec47e3dSKiyoshi Ueda 	/*
743cec47e3dSKiyoshi Ueda 	 * dm_put() must be at the end of this function. See the comment above
744cec47e3dSKiyoshi Ueda 	 */
745cec47e3dSKiyoshi Ueda 	dm_put(md);
746cec47e3dSKiyoshi Ueda }
747cec47e3dSKiyoshi Ueda 
748a77e28c7SKiyoshi Ueda static void free_rq_clone(struct request *clone)
749a77e28c7SKiyoshi Ueda {
750a77e28c7SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
751a77e28c7SKiyoshi Ueda 
752a77e28c7SKiyoshi Ueda 	blk_rq_unprep_clone(clone);
753a77e28c7SKiyoshi Ueda 	free_rq_tio(tio);
754a77e28c7SKiyoshi Ueda }
755a77e28c7SKiyoshi Ueda 
756980691e5SKiyoshi Ueda /*
757980691e5SKiyoshi Ueda  * Complete the clone and the original request.
758980691e5SKiyoshi Ueda  * Must be called without queue lock.
759980691e5SKiyoshi Ueda  */
760980691e5SKiyoshi Ueda static void dm_end_request(struct request *clone, int error)
761980691e5SKiyoshi Ueda {
762980691e5SKiyoshi Ueda 	int rw = rq_data_dir(clone);
763980691e5SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
764980691e5SKiyoshi Ueda 	struct mapped_device *md = tio->md;
765980691e5SKiyoshi Ueda 	struct request *rq = tio->orig;
766980691e5SKiyoshi Ueda 
76729e4013dSTejun Heo 	if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
768980691e5SKiyoshi Ueda 		rq->errors = clone->errors;
769980691e5SKiyoshi Ueda 		rq->resid_len = clone->resid_len;
770980691e5SKiyoshi Ueda 
771980691e5SKiyoshi Ueda 		if (rq->sense)
772980691e5SKiyoshi Ueda 			/*
773980691e5SKiyoshi Ueda 			 * We are using the sense buffer of the original
774980691e5SKiyoshi Ueda 			 * request.
775980691e5SKiyoshi Ueda 			 * So setting the length of the sense data is enough.
776980691e5SKiyoshi Ueda 			 */
777980691e5SKiyoshi Ueda 			rq->sense_len = clone->sense_len;
778980691e5SKiyoshi Ueda 	}
779980691e5SKiyoshi Ueda 
780980691e5SKiyoshi Ueda 	free_rq_clone(clone);
781980691e5SKiyoshi Ueda 	blk_end_request_all(rq, error);
78229e4013dSTejun Heo 	rq_completed(md, rw, true);
783980691e5SKiyoshi Ueda }
784980691e5SKiyoshi Ueda 
785cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq)
786cec47e3dSKiyoshi Ueda {
787cec47e3dSKiyoshi Ueda 	struct request *clone = rq->special;
788cec47e3dSKiyoshi Ueda 
789cec47e3dSKiyoshi Ueda 	rq->special = NULL;
790cec47e3dSKiyoshi Ueda 	rq->cmd_flags &= ~REQ_DONTPREP;
791cec47e3dSKiyoshi Ueda 
792a77e28c7SKiyoshi Ueda 	free_rq_clone(clone);
793cec47e3dSKiyoshi Ueda }
794cec47e3dSKiyoshi Ueda 
795cec47e3dSKiyoshi Ueda /*
796cec47e3dSKiyoshi Ueda  * Requeue the original request of a clone.
797cec47e3dSKiyoshi Ueda  */
798cec47e3dSKiyoshi Ueda void dm_requeue_unmapped_request(struct request *clone)
799cec47e3dSKiyoshi Ueda {
800b4324feeSKiyoshi Ueda 	int rw = rq_data_dir(clone);
801cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
802cec47e3dSKiyoshi Ueda 	struct mapped_device *md = tio->md;
803cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
804cec47e3dSKiyoshi Ueda 	struct request_queue *q = rq->q;
805cec47e3dSKiyoshi Ueda 	unsigned long flags;
806cec47e3dSKiyoshi Ueda 
807cec47e3dSKiyoshi Ueda 	dm_unprep_request(rq);
808cec47e3dSKiyoshi Ueda 
809cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
810cec47e3dSKiyoshi Ueda 	blk_requeue_request(q, rq);
811cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
812cec47e3dSKiyoshi Ueda 
813b4324feeSKiyoshi Ueda 	rq_completed(md, rw, 0);
814cec47e3dSKiyoshi Ueda }
815cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
816cec47e3dSKiyoshi Ueda 
817cec47e3dSKiyoshi Ueda static void __stop_queue(struct request_queue *q)
818cec47e3dSKiyoshi Ueda {
819cec47e3dSKiyoshi Ueda 	blk_stop_queue(q);
820cec47e3dSKiyoshi Ueda }
821cec47e3dSKiyoshi Ueda 
822cec47e3dSKiyoshi Ueda static void stop_queue(struct request_queue *q)
823cec47e3dSKiyoshi Ueda {
824cec47e3dSKiyoshi Ueda 	unsigned long flags;
825cec47e3dSKiyoshi Ueda 
826cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
827cec47e3dSKiyoshi Ueda 	__stop_queue(q);
828cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
829cec47e3dSKiyoshi Ueda }
830cec47e3dSKiyoshi Ueda 
831cec47e3dSKiyoshi Ueda static void __start_queue(struct request_queue *q)
832cec47e3dSKiyoshi Ueda {
833cec47e3dSKiyoshi Ueda 	if (blk_queue_stopped(q))
834cec47e3dSKiyoshi Ueda 		blk_start_queue(q);
835cec47e3dSKiyoshi Ueda }
836cec47e3dSKiyoshi Ueda 
837cec47e3dSKiyoshi Ueda static void start_queue(struct request_queue *q)
838cec47e3dSKiyoshi Ueda {
839cec47e3dSKiyoshi Ueda 	unsigned long flags;
840cec47e3dSKiyoshi Ueda 
841cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
842cec47e3dSKiyoshi Ueda 	__start_queue(q);
843cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
844cec47e3dSKiyoshi Ueda }
845cec47e3dSKiyoshi Ueda 
84611a68244SKiyoshi Ueda static void dm_done(struct request *clone, int error, bool mapped)
84711a68244SKiyoshi Ueda {
84811a68244SKiyoshi Ueda 	int r = error;
84911a68244SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
85011a68244SKiyoshi Ueda 	dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
85111a68244SKiyoshi Ueda 
85211a68244SKiyoshi Ueda 	if (mapped && rq_end_io)
85311a68244SKiyoshi Ueda 		r = rq_end_io(tio->ti, clone, error, &tio->info);
85411a68244SKiyoshi Ueda 
85511a68244SKiyoshi Ueda 	if (r <= 0)
85611a68244SKiyoshi Ueda 		/* The target wants to complete the I/O */
85711a68244SKiyoshi Ueda 		dm_end_request(clone, r);
85811a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_INCOMPLETE)
85911a68244SKiyoshi Ueda 		/* The target will handle the I/O */
86011a68244SKiyoshi Ueda 		return;
86111a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_REQUEUE)
86211a68244SKiyoshi Ueda 		/* The target wants to requeue the I/O */
86311a68244SKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
86411a68244SKiyoshi Ueda 	else {
86511a68244SKiyoshi Ueda 		DMWARN("unimplemented target endio return value: %d", r);
86611a68244SKiyoshi Ueda 		BUG();
86711a68244SKiyoshi Ueda 	}
86811a68244SKiyoshi Ueda }
86911a68244SKiyoshi Ueda 
870cec47e3dSKiyoshi Ueda /*
871cec47e3dSKiyoshi Ueda  * Request completion handler for request-based dm
872cec47e3dSKiyoshi Ueda  */
873cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq)
874cec47e3dSKiyoshi Ueda {
87511a68244SKiyoshi Ueda 	bool mapped = true;
876cec47e3dSKiyoshi Ueda 	struct request *clone = rq->completion_data;
877cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
878cec47e3dSKiyoshi Ueda 
87911a68244SKiyoshi Ueda 	if (rq->cmd_flags & REQ_FAILED)
88011a68244SKiyoshi Ueda 		mapped = false;
881cec47e3dSKiyoshi Ueda 
88211a68244SKiyoshi Ueda 	dm_done(clone, tio->error, mapped);
883cec47e3dSKiyoshi Ueda }
884cec47e3dSKiyoshi Ueda 
885cec47e3dSKiyoshi Ueda /*
886cec47e3dSKiyoshi Ueda  * Complete the clone and the original request with the error status
887cec47e3dSKiyoshi Ueda  * through softirq context.
888cec47e3dSKiyoshi Ueda  */
889cec47e3dSKiyoshi Ueda static void dm_complete_request(struct request *clone, int error)
890cec47e3dSKiyoshi Ueda {
891cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
892cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
893cec47e3dSKiyoshi Ueda 
894cec47e3dSKiyoshi Ueda 	tio->error = error;
895cec47e3dSKiyoshi Ueda 	rq->completion_data = clone;
896cec47e3dSKiyoshi Ueda 	blk_complete_request(rq);
897cec47e3dSKiyoshi Ueda }
898cec47e3dSKiyoshi Ueda 
899cec47e3dSKiyoshi Ueda /*
900cec47e3dSKiyoshi Ueda  * Complete the not-mapped clone and the original request with the error status
901cec47e3dSKiyoshi Ueda  * through softirq context.
902cec47e3dSKiyoshi Ueda  * Target's rq_end_io() function isn't called.
903cec47e3dSKiyoshi Ueda  * This may be used when the target's map_rq() function fails.
904cec47e3dSKiyoshi Ueda  */
905cec47e3dSKiyoshi Ueda void dm_kill_unmapped_request(struct request *clone, int error)
906cec47e3dSKiyoshi Ueda {
907cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
908cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
909cec47e3dSKiyoshi Ueda 
910cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_FAILED;
911cec47e3dSKiyoshi Ueda 	dm_complete_request(clone, error);
912cec47e3dSKiyoshi Ueda }
913cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
914cec47e3dSKiyoshi Ueda 
915cec47e3dSKiyoshi Ueda /*
916cec47e3dSKiyoshi Ueda  * Called with the queue lock held
917cec47e3dSKiyoshi Ueda  */
918cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error)
919cec47e3dSKiyoshi Ueda {
920cec47e3dSKiyoshi Ueda 	/*
921cec47e3dSKiyoshi Ueda 	 * For just cleaning up the information of the queue in which
922cec47e3dSKiyoshi Ueda 	 * the clone was dispatched.
923cec47e3dSKiyoshi Ueda 	 * The clone is *NOT* freed actually here because it is alloced from
924cec47e3dSKiyoshi Ueda 	 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
925cec47e3dSKiyoshi Ueda 	 */
926cec47e3dSKiyoshi Ueda 	__blk_put_request(clone->q, clone);
927cec47e3dSKiyoshi Ueda 
928cec47e3dSKiyoshi Ueda 	/*
929cec47e3dSKiyoshi Ueda 	 * Actual request completion is done in a softirq context which doesn't
930cec47e3dSKiyoshi Ueda 	 * hold the queue lock.  Otherwise, deadlock could occur because:
931cec47e3dSKiyoshi Ueda 	 *     - another request may be submitted by the upper level driver
932cec47e3dSKiyoshi Ueda 	 *       of the stacking during the completion
933cec47e3dSKiyoshi Ueda 	 *     - the submission which requires queue lock may be done
934cec47e3dSKiyoshi Ueda 	 *       against this queue
935cec47e3dSKiyoshi Ueda 	 */
936cec47e3dSKiyoshi Ueda 	dm_complete_request(clone, error);
937cec47e3dSKiyoshi Ueda }
938cec47e3dSKiyoshi Ueda 
93956a67df7SMike Snitzer /*
94056a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
94156a67df7SMike Snitzer  * target boundary.
94256a67df7SMike Snitzer  */
94356a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
9441da177e4SLinus Torvalds {
94556a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
94656a67df7SMike Snitzer 
94756a67df7SMike Snitzer 	return ti->len - target_offset;
94856a67df7SMike Snitzer }
94956a67df7SMike Snitzer 
95056a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
95156a67df7SMike Snitzer {
95256a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	/*
9551da177e4SLinus Torvalds 	 * Does the target need to split even further ?
9561da177e4SLinus Torvalds 	 */
9571da177e4SLinus Torvalds 	if (ti->split_io) {
9581da177e4SLinus Torvalds 		sector_t boundary;
95956a67df7SMike Snitzer 		sector_t offset = dm_target_offset(ti, sector);
9601da177e4SLinus Torvalds 		boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
9611da177e4SLinus Torvalds 			   - offset;
9621da177e4SLinus Torvalds 		if (len > boundary)
9631da177e4SLinus Torvalds 			len = boundary;
9641da177e4SLinus Torvalds 	}
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 	return len;
9671da177e4SLinus Torvalds }
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds static void __map_bio(struct dm_target *ti, struct bio *clone,
970028867acSAlasdair G Kergon 		      struct dm_target_io *tio)
9711da177e4SLinus Torvalds {
9721da177e4SLinus Torvalds 	int r;
9732056a782SJens Axboe 	sector_t sector;
9749faf400fSStefan Bader 	struct mapped_device *md;
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
9771da177e4SLinus Torvalds 	clone->bi_private = tio;
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	/*
9801da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
9811da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
9821da177e4SLinus Torvalds 	 * this io.
9831da177e4SLinus Torvalds 	 */
9841da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
9852056a782SJens Axboe 	sector = clone->bi_sector;
9861da177e4SLinus Torvalds 	r = ti->type->map(ti, clone, &tio->info);
98745cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
9881da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
9892056a782SJens Axboe 
990d07335e5SMike Snitzer 		trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
99122a7c31aSAlan D. Brunelle 				      tio->io->bio->bi_bdev->bd_dev, sector);
9922056a782SJens Axboe 
9931da177e4SLinus Torvalds 		generic_make_request(clone);
9942e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
9952e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
9969faf400fSStefan Bader 		md = tio->io->md;
9979faf400fSStefan Bader 		dec_pending(tio->io, r);
9989faf400fSStefan Bader 		/*
9999faf400fSStefan Bader 		 * Store bio_set for cleanup.
10009faf400fSStefan Bader 		 */
10019faf400fSStefan Bader 		clone->bi_private = md->bs;
10021da177e4SLinus Torvalds 		bio_put(clone);
10039faf400fSStefan Bader 		free_tio(md, tio);
100445cbcd79SKiyoshi Ueda 	} else if (r) {
100545cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
100645cbcd79SKiyoshi Ueda 		BUG();
10071da177e4SLinus Torvalds 	}
10081da177e4SLinus Torvalds }
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds struct clone_info {
10111da177e4SLinus Torvalds 	struct mapped_device *md;
10121da177e4SLinus Torvalds 	struct dm_table *map;
10131da177e4SLinus Torvalds 	struct bio *bio;
10141da177e4SLinus Torvalds 	struct dm_io *io;
10151da177e4SLinus Torvalds 	sector_t sector;
10161da177e4SLinus Torvalds 	sector_t sector_count;
10171da177e4SLinus Torvalds 	unsigned short idx;
10181da177e4SLinus Torvalds };
10191da177e4SLinus Torvalds 
10203676347aSPeter Osterlund static void dm_bio_destructor(struct bio *bio)
10213676347aSPeter Osterlund {
10229faf400fSStefan Bader 	struct bio_set *bs = bio->bi_private;
10239faf400fSStefan Bader 
10249faf400fSStefan Bader 	bio_free(bio, bs);
10253676347aSPeter Osterlund }
10263676347aSPeter Osterlund 
10271da177e4SLinus Torvalds /*
1028d87f4c14STejun Heo  * Creates a little bio that just does part of a bvec.
10291da177e4SLinus Torvalds  */
10301da177e4SLinus Torvalds static struct bio *split_bvec(struct bio *bio, sector_t sector,
10311da177e4SLinus Torvalds 			      unsigned short idx, unsigned int offset,
10329faf400fSStefan Bader 			      unsigned int len, struct bio_set *bs)
10331da177e4SLinus Torvalds {
10341da177e4SLinus Torvalds 	struct bio *clone;
10351da177e4SLinus Torvalds 	struct bio_vec *bv = bio->bi_io_vec + idx;
10361da177e4SLinus Torvalds 
10379faf400fSStefan Bader 	clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
10383676347aSPeter Osterlund 	clone->bi_destructor = dm_bio_destructor;
10391da177e4SLinus Torvalds 	*clone->bi_io_vec = *bv;
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds 	clone->bi_sector = sector;
10421da177e4SLinus Torvalds 	clone->bi_bdev = bio->bi_bdev;
1043d87f4c14STejun Heo 	clone->bi_rw = bio->bi_rw;
10441da177e4SLinus Torvalds 	clone->bi_vcnt = 1;
10451da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
10461da177e4SLinus Torvalds 	clone->bi_io_vec->bv_offset = offset;
10471da177e4SLinus Torvalds 	clone->bi_io_vec->bv_len = clone->bi_size;
1048f3e1d26eSMartin K. Petersen 	clone->bi_flags |= 1 << BIO_CLONED;
10491da177e4SLinus Torvalds 
10509c47008dSMartin K. Petersen 	if (bio_integrity(bio)) {
10517878cba9SMartin K. Petersen 		bio_integrity_clone(clone, bio, GFP_NOIO, bs);
10529c47008dSMartin K. Petersen 		bio_integrity_trim(clone,
10539c47008dSMartin K. Petersen 				   bio_sector_offset(bio, idx, offset), len);
10549c47008dSMartin K. Petersen 	}
10559c47008dSMartin K. Petersen 
10561da177e4SLinus Torvalds 	return clone;
10571da177e4SLinus Torvalds }
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds /*
10601da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
10611da177e4SLinus Torvalds  */
10621da177e4SLinus Torvalds static struct bio *clone_bio(struct bio *bio, sector_t sector,
10631da177e4SLinus Torvalds 			     unsigned short idx, unsigned short bv_count,
10649faf400fSStefan Bader 			     unsigned int len, struct bio_set *bs)
10651da177e4SLinus Torvalds {
10661da177e4SLinus Torvalds 	struct bio *clone;
10671da177e4SLinus Torvalds 
10689faf400fSStefan Bader 	clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
10699faf400fSStefan Bader 	__bio_clone(clone, bio);
10709faf400fSStefan Bader 	clone->bi_destructor = dm_bio_destructor;
10711da177e4SLinus Torvalds 	clone->bi_sector = sector;
10721da177e4SLinus Torvalds 	clone->bi_idx = idx;
10731da177e4SLinus Torvalds 	clone->bi_vcnt = idx + bv_count;
10741da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
10751da177e4SLinus Torvalds 	clone->bi_flags &= ~(1 << BIO_SEG_VALID);
10761da177e4SLinus Torvalds 
10779c47008dSMartin K. Petersen 	if (bio_integrity(bio)) {
10787878cba9SMartin K. Petersen 		bio_integrity_clone(clone, bio, GFP_NOIO, bs);
10799c47008dSMartin K. Petersen 
10809c47008dSMartin K. Petersen 		if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
10819c47008dSMartin K. Petersen 			bio_integrity_trim(clone,
10829c47008dSMartin K. Petersen 					   bio_sector_offset(bio, idx, 0), len);
10839c47008dSMartin K. Petersen 	}
10849c47008dSMartin K. Petersen 
10851da177e4SLinus Torvalds 	return clone;
10861da177e4SLinus Torvalds }
10871da177e4SLinus Torvalds 
10889015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci,
10899015df24SAlasdair G Kergon 				      struct dm_target *ti)
1090f9ab94ceSMikulas Patocka {
10919015df24SAlasdair G Kergon 	struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
1092f9ab94ceSMikulas Patocka 
1093f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1094f9ab94ceSMikulas Patocka 	tio->ti = ti;
1095f9ab94ceSMikulas Patocka 	memset(&tio->info, 0, sizeof(tio->info));
10969015df24SAlasdair G Kergon 
10979015df24SAlasdair G Kergon 	return tio;
10989015df24SAlasdair G Kergon }
10999015df24SAlasdair G Kergon 
110006a426ceSMike Snitzer static void __issue_target_request(struct clone_info *ci, struct dm_target *ti,
1101a79245b3SMike Snitzer 				   unsigned request_nr, sector_t len)
11029015df24SAlasdair G Kergon {
11039015df24SAlasdair G Kergon 	struct dm_target_io *tio = alloc_tio(ci, ti);
11049015df24SAlasdair G Kergon 	struct bio *clone;
11059015df24SAlasdair G Kergon 
110657cba5d3SMike Snitzer 	tio->info.target_request_nr = request_nr;
1107f9ab94ceSMikulas Patocka 
110806a426ceSMike Snitzer 	/*
110906a426ceSMike Snitzer 	 * Discard requests require the bio's inline iovecs be initialized.
111006a426ceSMike Snitzer 	 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
111106a426ceSMike Snitzer 	 * and discard, so no need for concern about wasted bvec allocations.
111206a426ceSMike Snitzer 	 */
111306a426ceSMike Snitzer 	clone = bio_alloc_bioset(GFP_NOIO, ci->bio->bi_max_vecs, ci->md->bs);
1114f9ab94ceSMikulas Patocka 	__bio_clone(clone, ci->bio);
1115f9ab94ceSMikulas Patocka 	clone->bi_destructor = dm_bio_destructor;
1116a79245b3SMike Snitzer 	if (len) {
1117a79245b3SMike Snitzer 		clone->bi_sector = ci->sector;
1118a79245b3SMike Snitzer 		clone->bi_size = to_bytes(len);
1119a79245b3SMike Snitzer 	}
1120f9ab94ceSMikulas Patocka 
1121f9ab94ceSMikulas Patocka 	__map_bio(ti, clone, tio);
1122f9ab94ceSMikulas Patocka }
1123f9ab94ceSMikulas Patocka 
112406a426ceSMike Snitzer static void __issue_target_requests(struct clone_info *ci, struct dm_target *ti,
1125a79245b3SMike Snitzer 				    unsigned num_requests, sector_t len)
112606a426ceSMike Snitzer {
112706a426ceSMike Snitzer 	unsigned request_nr;
112806a426ceSMike Snitzer 
112906a426ceSMike Snitzer 	for (request_nr = 0; request_nr < num_requests; request_nr++)
1130a79245b3SMike Snitzer 		__issue_target_request(ci, ti, request_nr, len);
113106a426ceSMike Snitzer }
113206a426ceSMike Snitzer 
1133b372d360SMike Snitzer static int __clone_and_map_empty_flush(struct clone_info *ci)
1134f9ab94ceSMikulas Patocka {
113506a426ceSMike Snitzer 	unsigned target_nr = 0;
1136f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1137f9ab94ceSMikulas Patocka 
1138b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1139f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
1140a79245b3SMike Snitzer 		__issue_target_requests(ci, ti, ti->num_flush_requests, 0);
1141f9ab94ceSMikulas Patocka 
1142f9ab94ceSMikulas Patocka 	return 0;
1143f9ab94ceSMikulas Patocka }
1144f9ab94ceSMikulas Patocka 
11455ae89a87SMike Snitzer /*
11465ae89a87SMike Snitzer  * Perform all io with a single clone.
11475ae89a87SMike Snitzer  */
11485ae89a87SMike Snitzer static void __clone_and_map_simple(struct clone_info *ci, struct dm_target *ti)
11495ae89a87SMike Snitzer {
11505ae89a87SMike Snitzer 	struct bio *clone, *bio = ci->bio;
11515ae89a87SMike Snitzer 	struct dm_target_io *tio;
11525ae89a87SMike Snitzer 
11535ae89a87SMike Snitzer 	tio = alloc_tio(ci, ti);
11545ae89a87SMike Snitzer 	clone = clone_bio(bio, ci->sector, ci->idx,
11555ae89a87SMike Snitzer 			  bio->bi_vcnt - ci->idx, ci->sector_count,
11565ae89a87SMike Snitzer 			  ci->md->bs);
11575ae89a87SMike Snitzer 	__map_bio(ti, clone, tio);
11585ae89a87SMike Snitzer 	ci->sector_count = 0;
11595ae89a87SMike Snitzer }
11605ae89a87SMike Snitzer 
11615ae89a87SMike Snitzer static int __clone_and_map_discard(struct clone_info *ci)
11625ae89a87SMike Snitzer {
11635ae89a87SMike Snitzer 	struct dm_target *ti;
1164a79245b3SMike Snitzer 	sector_t len;
11655ae89a87SMike Snitzer 
1166a79245b3SMike Snitzer 	do {
11675ae89a87SMike Snitzer 		ti = dm_table_find_target(ci->map, ci->sector);
11685ae89a87SMike Snitzer 		if (!dm_target_is_valid(ti))
11695ae89a87SMike Snitzer 			return -EIO;
11705ae89a87SMike Snitzer 
11715ae89a87SMike Snitzer 		/*
11725ae89a87SMike Snitzer 		 * Even though the device advertised discard support,
11735ae89a87SMike Snitzer 		 * reconfiguration might have changed that since the
11745ae89a87SMike Snitzer 		 * check was performed.
11755ae89a87SMike Snitzer 		 */
11765ae89a87SMike Snitzer 		if (!ti->num_discard_requests)
11775ae89a87SMike Snitzer 			return -EOPNOTSUPP;
11785ae89a87SMike Snitzer 
1179a79245b3SMike Snitzer 		len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
11805ae89a87SMike Snitzer 
1181a79245b3SMike Snitzer 		__issue_target_requests(ci, ti, ti->num_discard_requests, len);
11825ae89a87SMike Snitzer 
1183a79245b3SMike Snitzer 		ci->sector += len;
1184a79245b3SMike Snitzer 	} while (ci->sector_count -= len);
11855ae89a87SMike Snitzer 
11865ae89a87SMike Snitzer 	return 0;
11875ae89a87SMike Snitzer }
11885ae89a87SMike Snitzer 
1189512875bdSJun'ichi Nomura static int __clone_and_map(struct clone_info *ci)
11901da177e4SLinus Torvalds {
11911da177e4SLinus Torvalds 	struct bio *clone, *bio = ci->bio;
1192512875bdSJun'ichi Nomura 	struct dm_target *ti;
1193512875bdSJun'ichi Nomura 	sector_t len = 0, max;
1194028867acSAlasdair G Kergon 	struct dm_target_io *tio;
11951da177e4SLinus Torvalds 
11965ae89a87SMike Snitzer 	if (unlikely(bio->bi_rw & REQ_DISCARD))
11975ae89a87SMike Snitzer 		return __clone_and_map_discard(ci);
11985ae89a87SMike Snitzer 
1199512875bdSJun'ichi Nomura 	ti = dm_table_find_target(ci->map, ci->sector);
1200512875bdSJun'ichi Nomura 	if (!dm_target_is_valid(ti))
1201512875bdSJun'ichi Nomura 		return -EIO;
1202512875bdSJun'ichi Nomura 
120356a67df7SMike Snitzer 	max = max_io_len(ci->sector, ti);
1204512875bdSJun'ichi Nomura 
12051da177e4SLinus Torvalds 	if (ci->sector_count <= max) {
12061da177e4SLinus Torvalds 		/*
12071da177e4SLinus Torvalds 		 * Optimise for the simple case where we can do all of
12081da177e4SLinus Torvalds 		 * the remaining io with a single clone.
12091da177e4SLinus Torvalds 		 */
12105ae89a87SMike Snitzer 		__clone_and_map_simple(ci, ti);
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 	} else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
12131da177e4SLinus Torvalds 		/*
12141da177e4SLinus Torvalds 		 * There are some bvecs that don't span targets.
12151da177e4SLinus Torvalds 		 * Do as many of these as possible.
12161da177e4SLinus Torvalds 		 */
12171da177e4SLinus Torvalds 		int i;
12181da177e4SLinus Torvalds 		sector_t remaining = max;
12191da177e4SLinus Torvalds 		sector_t bv_len;
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 		for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
12221da177e4SLinus Torvalds 			bv_len = to_sector(bio->bi_io_vec[i].bv_len);
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 			if (bv_len > remaining)
12251da177e4SLinus Torvalds 				break;
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 			remaining -= bv_len;
12281da177e4SLinus Torvalds 			len += bv_len;
12291da177e4SLinus Torvalds 		}
12301da177e4SLinus Torvalds 
12315ae89a87SMike Snitzer 		tio = alloc_tio(ci, ti);
12329faf400fSStefan Bader 		clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
12339faf400fSStefan Bader 				  ci->md->bs);
12341da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 		ci->sector += len;
12371da177e4SLinus Torvalds 		ci->sector_count -= len;
12381da177e4SLinus Torvalds 		ci->idx = i;
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 	} else {
12411da177e4SLinus Torvalds 		/*
1242d2044a94SAlasdair G Kergon 		 * Handle a bvec that must be split between two or more targets.
12431da177e4SLinus Torvalds 		 */
12441da177e4SLinus Torvalds 		struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1245d2044a94SAlasdair G Kergon 		sector_t remaining = to_sector(bv->bv_len);
1246d2044a94SAlasdair G Kergon 		unsigned int offset = 0;
12471da177e4SLinus Torvalds 
1248d2044a94SAlasdair G Kergon 		do {
1249d2044a94SAlasdair G Kergon 			if (offset) {
12501da177e4SLinus Torvalds 				ti = dm_table_find_target(ci->map, ci->sector);
1251512875bdSJun'ichi Nomura 				if (!dm_target_is_valid(ti))
1252512875bdSJun'ichi Nomura 					return -EIO;
1253512875bdSJun'ichi Nomura 
125456a67df7SMike Snitzer 				max = max_io_len(ci->sector, ti);
1255d2044a94SAlasdair G Kergon 			}
1256d2044a94SAlasdair G Kergon 
1257d2044a94SAlasdair G Kergon 			len = min(remaining, max);
1258d2044a94SAlasdair G Kergon 
12595ae89a87SMike Snitzer 			tio = alloc_tio(ci, ti);
1260d2044a94SAlasdair G Kergon 			clone = split_bvec(bio, ci->sector, ci->idx,
12619faf400fSStefan Bader 					   bv->bv_offset + offset, len,
12629faf400fSStefan Bader 					   ci->md->bs);
1263d2044a94SAlasdair G Kergon 
12641da177e4SLinus Torvalds 			__map_bio(ti, clone, tio);
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 			ci->sector += len;
12671da177e4SLinus Torvalds 			ci->sector_count -= len;
1268d2044a94SAlasdair G Kergon 			offset += to_bytes(len);
1269d2044a94SAlasdair G Kergon 		} while (remaining -= len);
1270d2044a94SAlasdair G Kergon 
12711da177e4SLinus Torvalds 		ci->idx++;
12721da177e4SLinus Torvalds 	}
1273512875bdSJun'ichi Nomura 
1274512875bdSJun'ichi Nomura 	return 0;
12751da177e4SLinus Torvalds }
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds /*
12788a53c28dSMikulas Patocka  * Split the bio into several clones and submit it to targets.
12791da177e4SLinus Torvalds  */
1280f0b9a450SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
12811da177e4SLinus Torvalds {
12821da177e4SLinus Torvalds 	struct clone_info ci;
1283512875bdSJun'ichi Nomura 	int error = 0;
12841da177e4SLinus Torvalds 
12857c666411SAlasdair G Kergon 	ci.map = dm_get_live_table(md);
1286f0b9a450SMikulas Patocka 	if (unlikely(!ci.map)) {
1287f0b9a450SMikulas Patocka 		bio_io_error(bio);
1288f0b9a450SMikulas Patocka 		return;
1289f0b9a450SMikulas Patocka 	}
1290692d0eb9SMikulas Patocka 
12911da177e4SLinus Torvalds 	ci.md = md;
12921da177e4SLinus Torvalds 	ci.io = alloc_io(md);
12931da177e4SLinus Torvalds 	ci.io->error = 0;
12941da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
12951da177e4SLinus Torvalds 	ci.io->bio = bio;
12961da177e4SLinus Torvalds 	ci.io->md = md;
1297f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
12981da177e4SLinus Torvalds 	ci.sector = bio->bi_sector;
12991da177e4SLinus Torvalds 	ci.idx = bio->bi_idx;
13001da177e4SLinus Torvalds 
13013eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1302b372d360SMike Snitzer 	if (bio->bi_rw & REQ_FLUSH) {
1303b372d360SMike Snitzer 		ci.bio = &ci.md->flush_bio;
1304b372d360SMike Snitzer 		ci.sector_count = 0;
1305b372d360SMike Snitzer 		error = __clone_and_map_empty_flush(&ci);
1306b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1307b372d360SMike Snitzer 	} else {
13086a8736d1STejun Heo 		ci.bio = bio;
1309f6fccb12SMilan Broz 		ci.sector_count = bio_sectors(bio);
1310512875bdSJun'ichi Nomura 		while (ci.sector_count && !error)
1311512875bdSJun'ichi Nomura 			error = __clone_and_map(&ci);
1312d87f4c14STejun Heo 	}
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 	/* drop the extra reference count */
1315512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
13161da177e4SLinus Torvalds 	dm_table_put(ci.map);
13179e4e5f87SMilan Broz }
13189e4e5f87SMilan Broz /*-----------------------------------------------------------------
13191da177e4SLinus Torvalds  * CRUD END
13201da177e4SLinus Torvalds  *---------------------------------------------------------------*/
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds static int dm_merge_bvec(struct request_queue *q,
13231da177e4SLinus Torvalds 			 struct bvec_merge_data *bvm,
1324f6fccb12SMilan Broz 			 struct bio_vec *biovec)
1325f6fccb12SMilan Broz {
1326f6fccb12SMilan Broz 	struct mapped_device *md = q->queuedata;
13277c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1328f6fccb12SMilan Broz 	struct dm_target *ti;
1329f6fccb12SMilan Broz 	sector_t max_sectors;
1330f6fccb12SMilan Broz 	int max_size = 0;
1331f6fccb12SMilan Broz 
1332f6fccb12SMilan Broz 	if (unlikely(!map))
1333f6fccb12SMilan Broz 		goto out;
1334f6fccb12SMilan Broz 
1335f6fccb12SMilan Broz 	ti = dm_table_find_target(map, bvm->bi_sector);
1336f6fccb12SMilan Broz 	if (!dm_target_is_valid(ti))
1337f6fccb12SMilan Broz 		goto out_table;
1338f6fccb12SMilan Broz 
1339f6fccb12SMilan Broz 	/*
1340f6fccb12SMilan Broz 	 * Find maximum amount of I/O that won't need splitting
1341f6fccb12SMilan Broz 	 */
134256a67df7SMike Snitzer 	max_sectors = min(max_io_len(bvm->bi_sector, ti),
1343f6fccb12SMilan Broz 			  (sector_t) BIO_MAX_SECTORS);
1344f6fccb12SMilan Broz 	max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1345f6fccb12SMilan Broz 	if (max_size < 0)
1346f6fccb12SMilan Broz 		max_size = 0;
1347f6fccb12SMilan Broz 
1348f6fccb12SMilan Broz 	/*
1349f6fccb12SMilan Broz 	 * merge_bvec_fn() returns number of bytes
1350f6fccb12SMilan Broz 	 * it can accept at this offset
1351f6fccb12SMilan Broz 	 * max is precomputed maximal io size
1352f6fccb12SMilan Broz 	 */
1353f6fccb12SMilan Broz 	if (max_size && ti->type->merge)
1354f6fccb12SMilan Broz 		max_size = ti->type->merge(ti, bvm, biovec, max_size);
13558cbeb67aSMikulas Patocka 	/*
13568cbeb67aSMikulas Patocka 	 * If the target doesn't support merge method and some of the devices
13578cbeb67aSMikulas Patocka 	 * provided their merge_bvec method (we know this by looking at
13588cbeb67aSMikulas Patocka 	 * queue_max_hw_sectors), then we can't allow bios with multiple vector
13598cbeb67aSMikulas Patocka 	 * entries.  So always set max_size to 0, and the code below allows
13608cbeb67aSMikulas Patocka 	 * just one page.
13618cbeb67aSMikulas Patocka 	 */
13628cbeb67aSMikulas Patocka 	else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
13638cbeb67aSMikulas Patocka 
13648cbeb67aSMikulas Patocka 		max_size = 0;
1365f6fccb12SMilan Broz 
1366b01cd5acSMikulas Patocka out_table:
13675037108aSMikulas Patocka 	dm_table_put(map);
13685037108aSMikulas Patocka 
13695037108aSMikulas Patocka out:
1370f6fccb12SMilan Broz 	/*
1371f6fccb12SMilan Broz 	 * Always allow an entire first page
1372f6fccb12SMilan Broz 	 */
1373f6fccb12SMilan Broz 	if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1374f6fccb12SMilan Broz 		max_size = biovec->bv_len;
1375f6fccb12SMilan Broz 
1376f6fccb12SMilan Broz 	return max_size;
1377f6fccb12SMilan Broz }
1378f6fccb12SMilan Broz 
13791da177e4SLinus Torvalds /*
13801da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
13811da177e4SLinus Torvalds  * dm_merge_bvec.
13821da177e4SLinus Torvalds  */
1383cec47e3dSKiyoshi Ueda static int _dm_request(struct request_queue *q, struct bio *bio)
13841da177e4SLinus Torvalds {
138512f03a49SKevin Corry 	int rw = bio_data_dir(bio);
13861da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
1387c9959059STejun Heo 	int cpu;
13881da177e4SLinus Torvalds 
13892ca3310eSAlasdair G Kergon 	down_read(&md->io_lock);
13901da177e4SLinus Torvalds 
1391074a7acaSTejun Heo 	cpu = part_stat_lock();
1392074a7acaSTejun Heo 	part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1393074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1394074a7acaSTejun Heo 	part_stat_unlock();
139512f03a49SKevin Corry 
13966a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
13976a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
13982ca3310eSAlasdair G Kergon 		up_read(&md->io_lock);
13991da177e4SLinus Torvalds 
14006a8736d1STejun Heo 		if (bio_rw(bio) != READA)
140192c63902SMikulas Patocka 			queue_io(md, bio);
14026a8736d1STejun Heo 		else
14036a8736d1STejun Heo 			bio_io_error(bio);
140492c63902SMikulas Patocka 		return 0;
14051da177e4SLinus Torvalds 	}
14061da177e4SLinus Torvalds 
1407f0b9a450SMikulas Patocka 	__split_and_process_bio(md, bio);
14082ca3310eSAlasdair G Kergon 	up_read(&md->io_lock);
1409f0b9a450SMikulas Patocka 	return 0;
14101da177e4SLinus Torvalds }
14111da177e4SLinus Torvalds 
1412cec47e3dSKiyoshi Ueda static int dm_make_request(struct request_queue *q, struct bio *bio)
1413cec47e3dSKiyoshi Ueda {
1414cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1415cec47e3dSKiyoshi Ueda 
1416cec47e3dSKiyoshi Ueda 	return md->saved_make_request_fn(q, bio); /* call __make_request() */
1417cec47e3dSKiyoshi Ueda }
1418cec47e3dSKiyoshi Ueda 
1419cec47e3dSKiyoshi Ueda static int dm_request_based(struct mapped_device *md)
1420cec47e3dSKiyoshi Ueda {
1421cec47e3dSKiyoshi Ueda 	return blk_queue_stackable(md->queue);
1422cec47e3dSKiyoshi Ueda }
1423cec47e3dSKiyoshi Ueda 
1424cec47e3dSKiyoshi Ueda static int dm_request(struct request_queue *q, struct bio *bio)
1425cec47e3dSKiyoshi Ueda {
1426cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1427cec47e3dSKiyoshi Ueda 
1428cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
1429cec47e3dSKiyoshi Ueda 		return dm_make_request(q, bio);
1430cec47e3dSKiyoshi Ueda 
1431cec47e3dSKiyoshi Ueda 	return _dm_request(q, bio);
1432cec47e3dSKiyoshi Ueda }
1433cec47e3dSKiyoshi Ueda 
1434cec47e3dSKiyoshi Ueda void dm_dispatch_request(struct request *rq)
1435cec47e3dSKiyoshi Ueda {
1436cec47e3dSKiyoshi Ueda 	int r;
1437cec47e3dSKiyoshi Ueda 
1438cec47e3dSKiyoshi Ueda 	if (blk_queue_io_stat(rq->q))
1439cec47e3dSKiyoshi Ueda 		rq->cmd_flags |= REQ_IO_STAT;
1440cec47e3dSKiyoshi Ueda 
1441cec47e3dSKiyoshi Ueda 	rq->start_time = jiffies;
1442cec47e3dSKiyoshi Ueda 	r = blk_insert_cloned_request(rq->q, rq);
1443cec47e3dSKiyoshi Ueda 	if (r)
1444cec47e3dSKiyoshi Ueda 		dm_complete_request(rq, r);
1445cec47e3dSKiyoshi Ueda }
1446cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_dispatch_request);
1447cec47e3dSKiyoshi Ueda 
1448cec47e3dSKiyoshi Ueda static void dm_rq_bio_destructor(struct bio *bio)
1449cec47e3dSKiyoshi Ueda {
1450cec47e3dSKiyoshi Ueda 	struct dm_rq_clone_bio_info *info = bio->bi_private;
1451cec47e3dSKiyoshi Ueda 	struct mapped_device *md = info->tio->md;
1452cec47e3dSKiyoshi Ueda 
1453cec47e3dSKiyoshi Ueda 	free_bio_info(info);
1454cec47e3dSKiyoshi Ueda 	bio_free(bio, md->bs);
1455cec47e3dSKiyoshi Ueda }
1456cec47e3dSKiyoshi Ueda 
1457cec47e3dSKiyoshi Ueda static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1458cec47e3dSKiyoshi Ueda 				 void *data)
1459cec47e3dSKiyoshi Ueda {
1460cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = data;
1461cec47e3dSKiyoshi Ueda 	struct mapped_device *md = tio->md;
1462cec47e3dSKiyoshi Ueda 	struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1463cec47e3dSKiyoshi Ueda 
1464cec47e3dSKiyoshi Ueda 	if (!info)
1465cec47e3dSKiyoshi Ueda 		return -ENOMEM;
1466cec47e3dSKiyoshi Ueda 
1467cec47e3dSKiyoshi Ueda 	info->orig = bio_orig;
1468cec47e3dSKiyoshi Ueda 	info->tio = tio;
1469cec47e3dSKiyoshi Ueda 	bio->bi_end_io = end_clone_bio;
1470cec47e3dSKiyoshi Ueda 	bio->bi_private = info;
1471cec47e3dSKiyoshi Ueda 	bio->bi_destructor = dm_rq_bio_destructor;
1472cec47e3dSKiyoshi Ueda 
1473cec47e3dSKiyoshi Ueda 	return 0;
1474cec47e3dSKiyoshi Ueda }
1475cec47e3dSKiyoshi Ueda 
1476cec47e3dSKiyoshi Ueda static int setup_clone(struct request *clone, struct request *rq,
1477cec47e3dSKiyoshi Ueda 		       struct dm_rq_target_io *tio)
1478cec47e3dSKiyoshi Ueda {
1479d0bcb878SKiyoshi Ueda 	int r;
1480cec47e3dSKiyoshi Ueda 
1481d0bcb878SKiyoshi Ueda 	r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1482d0bcb878SKiyoshi Ueda 			      dm_rq_bio_constructor, tio);
1483cec47e3dSKiyoshi Ueda 	if (r)
1484cec47e3dSKiyoshi Ueda 		return r;
1485cec47e3dSKiyoshi Ueda 
1486cec47e3dSKiyoshi Ueda 	clone->cmd = rq->cmd;
1487cec47e3dSKiyoshi Ueda 	clone->cmd_len = rq->cmd_len;
1488cec47e3dSKiyoshi Ueda 	clone->sense = rq->sense;
1489cec47e3dSKiyoshi Ueda 	clone->buffer = rq->buffer;
1490cec47e3dSKiyoshi Ueda 	clone->end_io = end_clone_request;
1491cec47e3dSKiyoshi Ueda 	clone->end_io_data = tio;
1492cec47e3dSKiyoshi Ueda 
1493cec47e3dSKiyoshi Ueda 	return 0;
1494cec47e3dSKiyoshi Ueda }
1495cec47e3dSKiyoshi Ueda 
14966facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md,
14976facdaffSKiyoshi Ueda 				gfp_t gfp_mask)
14986facdaffSKiyoshi Ueda {
14996facdaffSKiyoshi Ueda 	struct request *clone;
15006facdaffSKiyoshi Ueda 	struct dm_rq_target_io *tio;
15016facdaffSKiyoshi Ueda 
15026facdaffSKiyoshi Ueda 	tio = alloc_rq_tio(md, gfp_mask);
15036facdaffSKiyoshi Ueda 	if (!tio)
15046facdaffSKiyoshi Ueda 		return NULL;
15056facdaffSKiyoshi Ueda 
15066facdaffSKiyoshi Ueda 	tio->md = md;
15076facdaffSKiyoshi Ueda 	tio->ti = NULL;
15086facdaffSKiyoshi Ueda 	tio->orig = rq;
15096facdaffSKiyoshi Ueda 	tio->error = 0;
15106facdaffSKiyoshi Ueda 	memset(&tio->info, 0, sizeof(tio->info));
15116facdaffSKiyoshi Ueda 
15126facdaffSKiyoshi Ueda 	clone = &tio->clone;
15136facdaffSKiyoshi Ueda 	if (setup_clone(clone, rq, tio)) {
15146facdaffSKiyoshi Ueda 		/* -ENOMEM */
15156facdaffSKiyoshi Ueda 		free_rq_tio(tio);
15166facdaffSKiyoshi Ueda 		return NULL;
15176facdaffSKiyoshi Ueda 	}
15186facdaffSKiyoshi Ueda 
15196facdaffSKiyoshi Ueda 	return clone;
15206facdaffSKiyoshi Ueda }
15216facdaffSKiyoshi Ueda 
1522cec47e3dSKiyoshi Ueda /*
1523cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1524cec47e3dSKiyoshi Ueda  */
1525cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq)
1526cec47e3dSKiyoshi Ueda {
1527cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1528cec47e3dSKiyoshi Ueda 	struct request *clone;
1529cec47e3dSKiyoshi Ueda 
1530cec47e3dSKiyoshi Ueda 	if (unlikely(rq->special)) {
1531cec47e3dSKiyoshi Ueda 		DMWARN("Already has something in rq->special.");
1532cec47e3dSKiyoshi Ueda 		return BLKPREP_KILL;
1533cec47e3dSKiyoshi Ueda 	}
1534cec47e3dSKiyoshi Ueda 
15356facdaffSKiyoshi Ueda 	clone = clone_rq(rq, md, GFP_ATOMIC);
15366facdaffSKiyoshi Ueda 	if (!clone)
1537cec47e3dSKiyoshi Ueda 		return BLKPREP_DEFER;
1538cec47e3dSKiyoshi Ueda 
1539cec47e3dSKiyoshi Ueda 	rq->special = clone;
1540cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_DONTPREP;
1541cec47e3dSKiyoshi Ueda 
1542cec47e3dSKiyoshi Ueda 	return BLKPREP_OK;
1543cec47e3dSKiyoshi Ueda }
1544cec47e3dSKiyoshi Ueda 
15459eef87daSKiyoshi Ueda /*
15469eef87daSKiyoshi Ueda  * Returns:
15479eef87daSKiyoshi Ueda  * 0  : the request has been processed (not requeued)
15489eef87daSKiyoshi Ueda  * !0 : the request has been requeued
15499eef87daSKiyoshi Ueda  */
15509eef87daSKiyoshi Ueda static int map_request(struct dm_target *ti, struct request *clone,
1551cec47e3dSKiyoshi Ueda 		       struct mapped_device *md)
1552cec47e3dSKiyoshi Ueda {
15539eef87daSKiyoshi Ueda 	int r, requeued = 0;
1554cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1555cec47e3dSKiyoshi Ueda 
1556cec47e3dSKiyoshi Ueda 	/*
1557cec47e3dSKiyoshi Ueda 	 * Hold the md reference here for the in-flight I/O.
1558cec47e3dSKiyoshi Ueda 	 * We can't rely on the reference count by device opener,
1559cec47e3dSKiyoshi Ueda 	 * because the device may be closed during the request completion
1560cec47e3dSKiyoshi Ueda 	 * when all bios are completed.
1561cec47e3dSKiyoshi Ueda 	 * See the comment in rq_completed() too.
1562cec47e3dSKiyoshi Ueda 	 */
1563cec47e3dSKiyoshi Ueda 	dm_get(md);
1564cec47e3dSKiyoshi Ueda 
1565cec47e3dSKiyoshi Ueda 	tio->ti = ti;
1566cec47e3dSKiyoshi Ueda 	r = ti->type->map_rq(ti, clone, &tio->info);
1567cec47e3dSKiyoshi Ueda 	switch (r) {
1568cec47e3dSKiyoshi Ueda 	case DM_MAPIO_SUBMITTED:
1569cec47e3dSKiyoshi Ueda 		/* The target has taken the I/O to submit by itself later */
1570cec47e3dSKiyoshi Ueda 		break;
1571cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REMAPPED:
1572cec47e3dSKiyoshi Ueda 		/* The target has remapped the I/O so dispatch it */
15736db4ccd6SJun'ichi Nomura 		trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
15746db4ccd6SJun'ichi Nomura 				     blk_rq_pos(tio->orig));
1575cec47e3dSKiyoshi Ueda 		dm_dispatch_request(clone);
1576cec47e3dSKiyoshi Ueda 		break;
1577cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REQUEUE:
1578cec47e3dSKiyoshi Ueda 		/* The target wants to requeue the I/O */
1579cec47e3dSKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
15809eef87daSKiyoshi Ueda 		requeued = 1;
1581cec47e3dSKiyoshi Ueda 		break;
1582cec47e3dSKiyoshi Ueda 	default:
1583cec47e3dSKiyoshi Ueda 		if (r > 0) {
1584cec47e3dSKiyoshi Ueda 			DMWARN("unimplemented target map return value: %d", r);
1585cec47e3dSKiyoshi Ueda 			BUG();
1586cec47e3dSKiyoshi Ueda 		}
1587cec47e3dSKiyoshi Ueda 
1588cec47e3dSKiyoshi Ueda 		/* The target wants to complete the I/O */
1589cec47e3dSKiyoshi Ueda 		dm_kill_unmapped_request(clone, r);
1590cec47e3dSKiyoshi Ueda 		break;
1591cec47e3dSKiyoshi Ueda 	}
15929eef87daSKiyoshi Ueda 
15939eef87daSKiyoshi Ueda 	return requeued;
1594cec47e3dSKiyoshi Ueda }
1595cec47e3dSKiyoshi Ueda 
1596cec47e3dSKiyoshi Ueda /*
1597cec47e3dSKiyoshi Ueda  * q->request_fn for request-based dm.
1598cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1599cec47e3dSKiyoshi Ueda  */
1600cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q)
1601cec47e3dSKiyoshi Ueda {
1602cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
16037c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1604cec47e3dSKiyoshi Ueda 	struct dm_target *ti;
1605b4324feeSKiyoshi Ueda 	struct request *rq, *clone;
160629e4013dSTejun Heo 	sector_t pos;
1607cec47e3dSKiyoshi Ueda 
1608cec47e3dSKiyoshi Ueda 	/*
1609b4324feeSKiyoshi Ueda 	 * For suspend, check blk_queue_stopped() and increment
1610b4324feeSKiyoshi Ueda 	 * ->pending within a single queue_lock not to increment the
1611b4324feeSKiyoshi Ueda 	 * number of in-flight I/Os after the queue is stopped in
1612b4324feeSKiyoshi Ueda 	 * dm_suspend().
1613cec47e3dSKiyoshi Ueda 	 */
16147eaceaccSJens Axboe 	while (!blk_queue_stopped(q)) {
1615cec47e3dSKiyoshi Ueda 		rq = blk_peek_request(q);
1616cec47e3dSKiyoshi Ueda 		if (!rq)
16177eaceaccSJens Axboe 			goto delay_and_out;
1618cec47e3dSKiyoshi Ueda 
161929e4013dSTejun Heo 		/* always use block 0 to find the target for flushes for now */
162029e4013dSTejun Heo 		pos = 0;
162129e4013dSTejun Heo 		if (!(rq->cmd_flags & REQ_FLUSH))
162229e4013dSTejun Heo 			pos = blk_rq_pos(rq);
1623d0bcb878SKiyoshi Ueda 
162429e4013dSTejun Heo 		ti = dm_table_find_target(map, pos);
162529e4013dSTejun Heo 		BUG_ON(!dm_target_is_valid(ti));
162629e4013dSTejun Heo 
1627cec47e3dSKiyoshi Ueda 		if (ti->type->busy && ti->type->busy(ti))
16287eaceaccSJens Axboe 			goto delay_and_out;
1629cec47e3dSKiyoshi Ueda 
1630cec47e3dSKiyoshi Ueda 		blk_start_request(rq);
1631b4324feeSKiyoshi Ueda 		clone = rq->special;
1632b4324feeSKiyoshi Ueda 		atomic_inc(&md->pending[rq_data_dir(clone)]);
1633b4324feeSKiyoshi Ueda 
1634cec47e3dSKiyoshi Ueda 		spin_unlock(q->queue_lock);
16359eef87daSKiyoshi Ueda 		if (map_request(ti, clone, md))
16369eef87daSKiyoshi Ueda 			goto requeued;
16379eef87daSKiyoshi Ueda 
1638052189a2SKiyoshi Ueda 		BUG_ON(!irqs_disabled());
1639052189a2SKiyoshi Ueda 		spin_lock(q->queue_lock);
1640cec47e3dSKiyoshi Ueda 	}
1641cec47e3dSKiyoshi Ueda 
1642cec47e3dSKiyoshi Ueda 	goto out;
1643cec47e3dSKiyoshi Ueda 
16449eef87daSKiyoshi Ueda requeued:
1645052189a2SKiyoshi Ueda 	BUG_ON(!irqs_disabled());
1646052189a2SKiyoshi Ueda 	spin_lock(q->queue_lock);
16479eef87daSKiyoshi Ueda 
16487eaceaccSJens Axboe delay_and_out:
16497eaceaccSJens Axboe 	blk_delay_queue(q, HZ / 10);
1650cec47e3dSKiyoshi Ueda out:
1651cec47e3dSKiyoshi Ueda 	dm_table_put(map);
1652cec47e3dSKiyoshi Ueda 
1653cec47e3dSKiyoshi Ueda 	return;
1654cec47e3dSKiyoshi Ueda }
1655cec47e3dSKiyoshi Ueda 
1656cec47e3dSKiyoshi Ueda int dm_underlying_device_busy(struct request_queue *q)
1657cec47e3dSKiyoshi Ueda {
1658cec47e3dSKiyoshi Ueda 	return blk_lld_busy(q);
1659cec47e3dSKiyoshi Ueda }
1660cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1661cec47e3dSKiyoshi Ueda 
1662cec47e3dSKiyoshi Ueda static int dm_lld_busy(struct request_queue *q)
1663cec47e3dSKiyoshi Ueda {
1664cec47e3dSKiyoshi Ueda 	int r;
1665cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
16667c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1667cec47e3dSKiyoshi Ueda 
1668cec47e3dSKiyoshi Ueda 	if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1669cec47e3dSKiyoshi Ueda 		r = 1;
1670cec47e3dSKiyoshi Ueda 	else
1671cec47e3dSKiyoshi Ueda 		r = dm_table_any_busy_target(map);
1672cec47e3dSKiyoshi Ueda 
1673cec47e3dSKiyoshi Ueda 	dm_table_put(map);
1674cec47e3dSKiyoshi Ueda 
1675cec47e3dSKiyoshi Ueda 	return r;
1676cec47e3dSKiyoshi Ueda }
1677cec47e3dSKiyoshi Ueda 
16781da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
16791da177e4SLinus Torvalds {
16808a57dfc6SChandra Seetharaman 	int r = bdi_bits;
16818a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
16828a57dfc6SChandra Seetharaman 	struct dm_table *map;
16831da177e4SLinus Torvalds 
16841eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
16857c666411SAlasdair G Kergon 		map = dm_get_live_table(md);
16868a57dfc6SChandra Seetharaman 		if (map) {
1687cec47e3dSKiyoshi Ueda 			/*
1688cec47e3dSKiyoshi Ueda 			 * Request-based dm cares about only own queue for
1689cec47e3dSKiyoshi Ueda 			 * the query about congestion status of request_queue
1690cec47e3dSKiyoshi Ueda 			 */
1691cec47e3dSKiyoshi Ueda 			if (dm_request_based(md))
1692cec47e3dSKiyoshi Ueda 				r = md->queue->backing_dev_info.state &
1693cec47e3dSKiyoshi Ueda 				    bdi_bits;
1694cec47e3dSKiyoshi Ueda 			else
16951da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
1696cec47e3dSKiyoshi Ueda 
16971da177e4SLinus Torvalds 			dm_table_put(map);
16988a57dfc6SChandra Seetharaman 		}
16998a57dfc6SChandra Seetharaman 	}
17008a57dfc6SChandra Seetharaman 
17011da177e4SLinus Torvalds 	return r;
17021da177e4SLinus Torvalds }
17031da177e4SLinus Torvalds 
17041da177e4SLinus Torvalds /*-----------------------------------------------------------------
17051da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
17061da177e4SLinus Torvalds  *---------------------------------------------------------------*/
17071da177e4SLinus Torvalds static DEFINE_IDR(_minor_idr);
17081da177e4SLinus Torvalds 
17092b06cfffSAlasdair G Kergon static void free_minor(int minor)
17101da177e4SLinus Torvalds {
1711f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17121da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1713f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
17141da177e4SLinus Torvalds }
17151da177e4SLinus Torvalds 
17161da177e4SLinus Torvalds /*
17171da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
17181da177e4SLinus Torvalds  */
1719cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
17201da177e4SLinus Torvalds {
17211da177e4SLinus Torvalds 	int r, m;
17221da177e4SLinus Torvalds 
17231da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
17241da177e4SLinus Torvalds 		return -EINVAL;
17251da177e4SLinus Torvalds 
172662f75c2fSJeff Mahoney 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
172762f75c2fSJeff Mahoney 	if (!r)
172862f75c2fSJeff Mahoney 		return -ENOMEM;
172962f75c2fSJeff Mahoney 
1730f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17311da177e4SLinus Torvalds 
17321da177e4SLinus Torvalds 	if (idr_find(&_minor_idr, minor)) {
17331da177e4SLinus Torvalds 		r = -EBUSY;
17341da177e4SLinus Torvalds 		goto out;
17351da177e4SLinus Torvalds 	}
17361da177e4SLinus Torvalds 
1737ba61fdd1SJeff Mahoney 	r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
173862f75c2fSJeff Mahoney 	if (r)
17391da177e4SLinus Torvalds 		goto out;
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds 	if (m != minor) {
17421da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
17431da177e4SLinus Torvalds 		r = -EBUSY;
17441da177e4SLinus Torvalds 		goto out;
17451da177e4SLinus Torvalds 	}
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds out:
1748f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
17491da177e4SLinus Torvalds 	return r;
17501da177e4SLinus Torvalds }
17511da177e4SLinus Torvalds 
1752cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
17531da177e4SLinus Torvalds {
17542b06cfffSAlasdair G Kergon 	int r, m;
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
175762f75c2fSJeff Mahoney 	if (!r)
175862f75c2fSJeff Mahoney 		return -ENOMEM;
175962f75c2fSJeff Mahoney 
1760f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17611da177e4SLinus Torvalds 
1762ba61fdd1SJeff Mahoney 	r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
1763cf13ab8eSFrederik Deweerdt 	if (r)
17641da177e4SLinus Torvalds 		goto out;
17651da177e4SLinus Torvalds 
17661da177e4SLinus Torvalds 	if (m >= (1 << MINORBITS)) {
17671da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
17681da177e4SLinus Torvalds 		r = -ENOSPC;
17691da177e4SLinus Torvalds 		goto out;
17701da177e4SLinus Torvalds 	}
17711da177e4SLinus Torvalds 
17721da177e4SLinus Torvalds 	*minor = m;
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds out:
1775f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
17761da177e4SLinus Torvalds 	return r;
17771da177e4SLinus Torvalds }
17781da177e4SLinus Torvalds 
177983d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
17801da177e4SLinus Torvalds 
178153d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
178253d5914fSMikulas Patocka 
17834a0b4ddfSMike Snitzer static void dm_init_md_queue(struct mapped_device *md)
17844a0b4ddfSMike Snitzer {
17854a0b4ddfSMike Snitzer 	/*
17864a0b4ddfSMike Snitzer 	 * Request-based dm devices cannot be stacked on top of bio-based dm
17874a0b4ddfSMike Snitzer 	 * devices.  The type of this dm device has not been decided yet.
17884a0b4ddfSMike Snitzer 	 * The type is decided at the first table loading time.
17894a0b4ddfSMike Snitzer 	 * To prevent problematic device stacking, clear the queue flag
17904a0b4ddfSMike Snitzer 	 * for request stacking support until then.
17914a0b4ddfSMike Snitzer 	 *
17924a0b4ddfSMike Snitzer 	 * This queue is new, so no concurrency on the queue_flags.
17934a0b4ddfSMike Snitzer 	 */
17944a0b4ddfSMike Snitzer 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
17954a0b4ddfSMike Snitzer 
17964a0b4ddfSMike Snitzer 	md->queue->queuedata = md;
17974a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
17984a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_data = md;
17994a0b4ddfSMike Snitzer 	blk_queue_make_request(md->queue, dm_request);
18004a0b4ddfSMike Snitzer 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
18014a0b4ddfSMike Snitzer 	blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1802d87f4c14STejun Heo 	blk_queue_flush(md->queue, REQ_FLUSH | REQ_FUA);
18034a0b4ddfSMike Snitzer }
18044a0b4ddfSMike Snitzer 
18051da177e4SLinus Torvalds /*
18061da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
18071da177e4SLinus Torvalds  */
18082b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
18091da177e4SLinus Torvalds {
18101da177e4SLinus Torvalds 	int r;
1811cf13ab8eSFrederik Deweerdt 	struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1812ba61fdd1SJeff Mahoney 	void *old_md;
18131da177e4SLinus Torvalds 
18141da177e4SLinus Torvalds 	if (!md) {
18151da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
18161da177e4SLinus Torvalds 		return NULL;
18171da177e4SLinus Torvalds 	}
18181da177e4SLinus Torvalds 
181910da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
18206ed7ade8SMilan Broz 		goto bad_module_get;
182110da4f79SJeff Mahoney 
18221da177e4SLinus Torvalds 	/* get a minor number for the dev */
18232b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1824cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
18252b06cfffSAlasdair G Kergon 	else
1826cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
18271da177e4SLinus Torvalds 	if (r < 0)
18286ed7ade8SMilan Broz 		goto bad_minor;
18291da177e4SLinus Torvalds 
1830a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
18312ca3310eSAlasdair G Kergon 	init_rwsem(&md->io_lock);
1832e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1833a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
1834022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
18351da177e4SLinus Torvalds 	rwlock_init(&md->map_lock);
18361da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
18375c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
18381da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
18397a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
18407a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
18417a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
18421da177e4SLinus Torvalds 
18434a0b4ddfSMike Snitzer 	md->queue = blk_alloc_queue(GFP_KERNEL);
18441da177e4SLinus Torvalds 	if (!md->queue)
18456ed7ade8SMilan Broz 		goto bad_queue;
18461da177e4SLinus Torvalds 
18474a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
18489faf400fSStefan Bader 
18491da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
18501da177e4SLinus Torvalds 	if (!md->disk)
18516ed7ade8SMilan Broz 		goto bad_disk;
18521da177e4SLinus Torvalds 
1853316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
1854316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
1855f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
185653d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1857f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
1858f0b04115SJeff Mahoney 
18591da177e4SLinus Torvalds 	md->disk->major = _major;
18601da177e4SLinus Torvalds 	md->disk->first_minor = minor;
18611da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
18621da177e4SLinus Torvalds 	md->disk->queue = md->queue;
18631da177e4SLinus Torvalds 	md->disk->private_data = md;
18641da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
18651da177e4SLinus Torvalds 	add_disk(md->disk);
18667e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
18671da177e4SLinus Torvalds 
18689c4376deSTejun Heo 	md->wq = alloc_workqueue("kdmflush",
18699c4376deSTejun Heo 				 WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
1870304f3f6aSMilan Broz 	if (!md->wq)
1871304f3f6aSMilan Broz 		goto bad_thread;
1872304f3f6aSMilan Broz 
187332a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
187432a926daSMikulas Patocka 	if (!md->bdev)
187532a926daSMikulas Patocka 		goto bad_bdev;
187632a926daSMikulas Patocka 
18776a8736d1STejun Heo 	bio_init(&md->flush_bio);
18786a8736d1STejun Heo 	md->flush_bio.bi_bdev = md->bdev;
18796a8736d1STejun Heo 	md->flush_bio.bi_rw = WRITE_FLUSH;
18806a8736d1STejun Heo 
1881ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1882f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1883ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1884f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1885ba61fdd1SJeff Mahoney 
1886ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1887ba61fdd1SJeff Mahoney 
18881da177e4SLinus Torvalds 	return md;
18891da177e4SLinus Torvalds 
189032a926daSMikulas Patocka bad_bdev:
189132a926daSMikulas Patocka 	destroy_workqueue(md->wq);
1892304f3f6aSMilan Broz bad_thread:
189303022c54SZdenek Kabelac 	del_gendisk(md->disk);
1894304f3f6aSMilan Broz 	put_disk(md->disk);
18956ed7ade8SMilan Broz bad_disk:
18961312f40eSAl Viro 	blk_cleanup_queue(md->queue);
18976ed7ade8SMilan Broz bad_queue:
18981da177e4SLinus Torvalds 	free_minor(minor);
18996ed7ade8SMilan Broz bad_minor:
190010da4f79SJeff Mahoney 	module_put(THIS_MODULE);
19016ed7ade8SMilan Broz bad_module_get:
19021da177e4SLinus Torvalds 	kfree(md);
19031da177e4SLinus Torvalds 	return NULL;
19041da177e4SLinus Torvalds }
19051da177e4SLinus Torvalds 
1906ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1907ae9da83fSJun'ichi Nomura 
19081da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
19091da177e4SLinus Torvalds {
1910f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
191163d94e48SJun'ichi Nomura 
1912ae9da83fSJun'ichi Nomura 	unlock_fs(md);
1913db8fef4fSMikulas Patocka 	bdput(md->bdev);
1914304f3f6aSMilan Broz 	destroy_workqueue(md->wq);
1915e6ee8c0bSKiyoshi Ueda 	if (md->tio_pool)
19161da177e4SLinus Torvalds 		mempool_destroy(md->tio_pool);
1917e6ee8c0bSKiyoshi Ueda 	if (md->io_pool)
19181da177e4SLinus Torvalds 		mempool_destroy(md->io_pool);
1919e6ee8c0bSKiyoshi Ueda 	if (md->bs)
19209faf400fSStefan Bader 		bioset_free(md->bs);
19219c47008dSMartin K. Petersen 	blk_integrity_unregister(md->disk);
19221da177e4SLinus Torvalds 	del_gendisk(md->disk);
192363d94e48SJun'ichi Nomura 	free_minor(minor);
1924fba9f90eSJeff Mahoney 
1925fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
1926fba9f90eSJeff Mahoney 	md->disk->private_data = NULL;
1927fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
1928fba9f90eSJeff Mahoney 
19291da177e4SLinus Torvalds 	put_disk(md->disk);
19301312f40eSAl Viro 	blk_cleanup_queue(md->queue);
193110da4f79SJeff Mahoney 	module_put(THIS_MODULE);
19321da177e4SLinus Torvalds 	kfree(md);
19331da177e4SLinus Torvalds }
19341da177e4SLinus Torvalds 
1935e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1936e6ee8c0bSKiyoshi Ueda {
1937e6ee8c0bSKiyoshi Ueda 	struct dm_md_mempools *p;
1938e6ee8c0bSKiyoshi Ueda 
1939e6ee8c0bSKiyoshi Ueda 	if (md->io_pool && md->tio_pool && md->bs)
1940e6ee8c0bSKiyoshi Ueda 		/* the md already has necessary mempools */
1941e6ee8c0bSKiyoshi Ueda 		goto out;
1942e6ee8c0bSKiyoshi Ueda 
1943e6ee8c0bSKiyoshi Ueda 	p = dm_table_get_md_mempools(t);
1944e6ee8c0bSKiyoshi Ueda 	BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
1945e6ee8c0bSKiyoshi Ueda 
1946e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
1947e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
1948e6ee8c0bSKiyoshi Ueda 	md->tio_pool = p->tio_pool;
1949e6ee8c0bSKiyoshi Ueda 	p->tio_pool = NULL;
1950e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
1951e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
1952e6ee8c0bSKiyoshi Ueda 
1953e6ee8c0bSKiyoshi Ueda out:
1954e6ee8c0bSKiyoshi Ueda 	/* mempool bind completed, now no need any mempools in the table */
1955e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
1956e6ee8c0bSKiyoshi Ueda }
1957e6ee8c0bSKiyoshi Ueda 
19581da177e4SLinus Torvalds /*
19591da177e4SLinus Torvalds  * Bind a table to the device.
19601da177e4SLinus Torvalds  */
19611da177e4SLinus Torvalds static void event_callback(void *context)
19621da177e4SLinus Torvalds {
19637a8c3d3bSMike Anderson 	unsigned long flags;
19647a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
19651da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
19661da177e4SLinus Torvalds 
19677a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
19687a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
19697a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
19707a8c3d3bSMike Anderson 
1971ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
19727a8c3d3bSMike Anderson 
19731da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
19741da177e4SLinus Torvalds 	wake_up(&md->eventq);
19751da177e4SLinus Torvalds }
19761da177e4SLinus Torvalds 
1977c217649bSMike Snitzer /*
1978c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
1979c217649bSMike Snitzer  */
19804e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
19811da177e4SLinus Torvalds {
19824e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
19831da177e4SLinus Torvalds 
1984db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
19851da177e4SLinus Torvalds }
19861da177e4SLinus Torvalds 
1987042d2a9bSAlasdair G Kergon /*
1988042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
1989042d2a9bSAlasdair G Kergon  */
1990042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
1991754c5fc7SMike Snitzer 			       struct queue_limits *limits)
19921da177e4SLinus Torvalds {
1993042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
1994165125e1SJens Axboe 	struct request_queue *q = md->queue;
19951da177e4SLinus Torvalds 	sector_t size;
1996523d9297SKiyoshi Ueda 	unsigned long flags;
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds 	size = dm_table_get_size(t);
19993ac51e74SDarrick J. Wong 
20003ac51e74SDarrick J. Wong 	/*
20013ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
20023ac51e74SDarrick J. Wong 	 */
20033ac51e74SDarrick J. Wong 	if (size != get_capacity(md->disk))
20043ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
20053ac51e74SDarrick J. Wong 
20064e90188bSAlasdair G Kergon 	__set_size(md, size);
20071da177e4SLinus Torvalds 
2008cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
20092ca3310eSAlasdair G Kergon 
2010e6ee8c0bSKiyoshi Ueda 	/*
2011e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
2012e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
2013e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
2014e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
2015e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
2016e6ee8c0bSKiyoshi Ueda 	 */
2017e6ee8c0bSKiyoshi Ueda 	if (dm_table_request_based(t) && !blk_queue_stopped(q))
2018e6ee8c0bSKiyoshi Ueda 		stop_queue(q);
2019e6ee8c0bSKiyoshi Ueda 
2020e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
2021e6ee8c0bSKiyoshi Ueda 
2022523d9297SKiyoshi Ueda 	write_lock_irqsave(&md->map_lock, flags);
2023042d2a9bSAlasdair G Kergon 	old_map = md->map;
20242ca3310eSAlasdair G Kergon 	md->map = t;
2025754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
2026523d9297SKiyoshi Ueda 	write_unlock_irqrestore(&md->map_lock, flags);
20272ca3310eSAlasdair G Kergon 
2028042d2a9bSAlasdair G Kergon 	return old_map;
20291da177e4SLinus Torvalds }
20301da177e4SLinus Torvalds 
2031a7940155SAlasdair G Kergon /*
2032a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2033a7940155SAlasdair G Kergon  */
2034a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
20351da177e4SLinus Torvalds {
20361da177e4SLinus Torvalds 	struct dm_table *map = md->map;
2037523d9297SKiyoshi Ueda 	unsigned long flags;
20381da177e4SLinus Torvalds 
20391da177e4SLinus Torvalds 	if (!map)
2040a7940155SAlasdair G Kergon 		return NULL;
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
2043523d9297SKiyoshi Ueda 	write_lock_irqsave(&md->map_lock, flags);
20441da177e4SLinus Torvalds 	md->map = NULL;
2045523d9297SKiyoshi Ueda 	write_unlock_irqrestore(&md->map_lock, flags);
2046a7940155SAlasdair G Kergon 
2047a7940155SAlasdair G Kergon 	return map;
20481da177e4SLinus Torvalds }
20491da177e4SLinus Torvalds 
20501da177e4SLinus Torvalds /*
20511da177e4SLinus Torvalds  * Constructor for a new device.
20521da177e4SLinus Torvalds  */
20532b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
20541da177e4SLinus Torvalds {
20551da177e4SLinus Torvalds 	struct mapped_device *md;
20561da177e4SLinus Torvalds 
20572b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
20581da177e4SLinus Torvalds 	if (!md)
20591da177e4SLinus Torvalds 		return -ENXIO;
20601da177e4SLinus Torvalds 
2061784aae73SMilan Broz 	dm_sysfs_init(md);
2062784aae73SMilan Broz 
20631da177e4SLinus Torvalds 	*result = md;
20641da177e4SLinus Torvalds 	return 0;
20651da177e4SLinus Torvalds }
20661da177e4SLinus Torvalds 
2067a5664dadSMike Snitzer /*
2068a5664dadSMike Snitzer  * Functions to manage md->type.
2069a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2070a5664dadSMike Snitzer  */
2071a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2072a5664dadSMike Snitzer {
2073a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2074a5664dadSMike Snitzer }
2075a5664dadSMike Snitzer 
2076a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2077a5664dadSMike Snitzer {
2078a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2079a5664dadSMike Snitzer }
2080a5664dadSMike Snitzer 
2081a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type)
2082a5664dadSMike Snitzer {
2083a5664dadSMike Snitzer 	md->type = type;
2084a5664dadSMike Snitzer }
2085a5664dadSMike Snitzer 
2086a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md)
2087a5664dadSMike Snitzer {
2088a5664dadSMike Snitzer 	return md->type;
2089a5664dadSMike Snitzer }
2090a5664dadSMike Snitzer 
20914a0b4ddfSMike Snitzer /*
20924a0b4ddfSMike Snitzer  * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
20934a0b4ddfSMike Snitzer  */
20944a0b4ddfSMike Snitzer static int dm_init_request_based_queue(struct mapped_device *md)
20954a0b4ddfSMike Snitzer {
20964a0b4ddfSMike Snitzer 	struct request_queue *q = NULL;
20974a0b4ddfSMike Snitzer 
20984a0b4ddfSMike Snitzer 	if (md->queue->elevator)
20994a0b4ddfSMike Snitzer 		return 1;
21004a0b4ddfSMike Snitzer 
21014a0b4ddfSMike Snitzer 	/* Fully initialize the queue */
21024a0b4ddfSMike Snitzer 	q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
21034a0b4ddfSMike Snitzer 	if (!q)
21044a0b4ddfSMike Snitzer 		return 0;
21054a0b4ddfSMike Snitzer 
21064a0b4ddfSMike Snitzer 	md->queue = q;
21074a0b4ddfSMike Snitzer 	md->saved_make_request_fn = md->queue->make_request_fn;
21084a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
21094a0b4ddfSMike Snitzer 	blk_queue_softirq_done(md->queue, dm_softirq_done);
21104a0b4ddfSMike Snitzer 	blk_queue_prep_rq(md->queue, dm_prep_fn);
21114a0b4ddfSMike Snitzer 	blk_queue_lld_busy(md->queue, dm_lld_busy);
21124a0b4ddfSMike Snitzer 
21134a0b4ddfSMike Snitzer 	elv_register_queue(md->queue);
21144a0b4ddfSMike Snitzer 
21154a0b4ddfSMike Snitzer 	return 1;
21164a0b4ddfSMike Snitzer }
21174a0b4ddfSMike Snitzer 
21184a0b4ddfSMike Snitzer /*
21194a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
21204a0b4ddfSMike Snitzer  */
21214a0b4ddfSMike Snitzer int dm_setup_md_queue(struct mapped_device *md)
21224a0b4ddfSMike Snitzer {
21234a0b4ddfSMike Snitzer 	if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
21244a0b4ddfSMike Snitzer 	    !dm_init_request_based_queue(md)) {
21254a0b4ddfSMike Snitzer 		DMWARN("Cannot initialize queue for request-based mapped device");
21264a0b4ddfSMike Snitzer 		return -EINVAL;
21274a0b4ddfSMike Snitzer 	}
21284a0b4ddfSMike Snitzer 
21294a0b4ddfSMike Snitzer 	return 0;
21304a0b4ddfSMike Snitzer }
21314a0b4ddfSMike Snitzer 
2132637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev)
21331da177e4SLinus Torvalds {
21341da177e4SLinus Torvalds 	struct mapped_device *md;
21351da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
21361da177e4SLinus Torvalds 
21371da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
21381da177e4SLinus Torvalds 		return NULL;
21391da177e4SLinus Torvalds 
2140f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
21411da177e4SLinus Torvalds 
21421da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
2143fba9f90eSJeff Mahoney 	if (md && (md == MINOR_ALLOCED ||
2144f331c029STejun Heo 		   (MINOR(disk_devt(dm_disk(md))) != minor) ||
2145abdc568bSKiyoshi Ueda 		   dm_deleting_md(md) ||
2146fba9f90eSJeff Mahoney 		   test_bit(DMF_FREEING, &md->flags))) {
2147637842cfSDavid Teigland 		md = NULL;
2148fba9f90eSJeff Mahoney 		goto out;
2149fba9f90eSJeff Mahoney 	}
21501da177e4SLinus Torvalds 
2151fba9f90eSJeff Mahoney out:
2152f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
21531da177e4SLinus Torvalds 
2154637842cfSDavid Teigland 	return md;
2155637842cfSDavid Teigland }
2156637842cfSDavid Teigland 
2157d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev)
2158d229a958SDavid Teigland {
2159d229a958SDavid Teigland 	struct mapped_device *md = dm_find_md(dev);
2160d229a958SDavid Teigland 
2161d229a958SDavid Teigland 	if (md)
2162d229a958SDavid Teigland 		dm_get(md);
2163d229a958SDavid Teigland 
2164d229a958SDavid Teigland 	return md;
2165d229a958SDavid Teigland }
2166d229a958SDavid Teigland 
21679ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2168637842cfSDavid Teigland {
21699ade92a9SAlasdair G Kergon 	return md->interface_ptr;
21701da177e4SLinus Torvalds }
21711da177e4SLinus Torvalds 
21721da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
21731da177e4SLinus Torvalds {
21741da177e4SLinus Torvalds 	md->interface_ptr = ptr;
21751da177e4SLinus Torvalds }
21761da177e4SLinus Torvalds 
21771da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
21781da177e4SLinus Torvalds {
21791da177e4SLinus Torvalds 	atomic_inc(&md->holders);
21803f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
21811da177e4SLinus Torvalds }
21821da177e4SLinus Torvalds 
218372d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
218472d94861SAlasdair G Kergon {
218572d94861SAlasdair G Kergon 	return md->name;
218672d94861SAlasdair G Kergon }
218772d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
218872d94861SAlasdair G Kergon 
21893f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
21901da177e4SLinus Torvalds {
21911134e5aeSMike Anderson 	struct dm_table *map;
21921da177e4SLinus Torvalds 
21933f77316dSKiyoshi Ueda 	might_sleep();
2194fba9f90eSJeff Mahoney 
21953f77316dSKiyoshi Ueda 	spin_lock(&_minor_lock);
21967c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
21973f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2198fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2199f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
22003f77316dSKiyoshi Ueda 
22014f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
22021da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
22031da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
22041da177e4SLinus Torvalds 	}
22053f77316dSKiyoshi Ueda 
22063f77316dSKiyoshi Ueda 	/*
22073f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
22083f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
22093f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
22103f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
22113f77316dSKiyoshi Ueda 	 */
22123f77316dSKiyoshi Ueda 	if (wait)
22133f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
22143f77316dSKiyoshi Ueda 			msleep(1);
22153f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
22163f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
22173f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
22183f77316dSKiyoshi Ueda 
2219784aae73SMilan Broz 	dm_sysfs_exit(md);
22201134e5aeSMike Anderson 	dm_table_put(map);
2221a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
22221da177e4SLinus Torvalds 	free_dev(md);
22231da177e4SLinus Torvalds }
22243f77316dSKiyoshi Ueda 
22253f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
22263f77316dSKiyoshi Ueda {
22273f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
22283f77316dSKiyoshi Ueda }
22293f77316dSKiyoshi Ueda 
22303f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
22313f77316dSKiyoshi Ueda {
22323f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
22333f77316dSKiyoshi Ueda }
22343f77316dSKiyoshi Ueda 
22353f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
22363f77316dSKiyoshi Ueda {
22373f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
22381da177e4SLinus Torvalds }
223979eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
22401da177e4SLinus Torvalds 
2241401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
224246125c1cSMilan Broz {
224346125c1cSMilan Broz 	int r = 0;
2244b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
2245b44ebeb0SMikulas Patocka 
2246b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
224746125c1cSMilan Broz 
224846125c1cSMilan Broz 	while (1) {
2249401600dfSMikulas Patocka 		set_current_state(interruptible);
225046125c1cSMilan Broz 
225146125c1cSMilan Broz 		smp_mb();
2252b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
225346125c1cSMilan Broz 			break;
225446125c1cSMilan Broz 
2255401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
2256401600dfSMikulas Patocka 		    signal_pending(current)) {
225746125c1cSMilan Broz 			r = -EINTR;
225846125c1cSMilan Broz 			break;
225946125c1cSMilan Broz 		}
226046125c1cSMilan Broz 
226146125c1cSMilan Broz 		io_schedule();
226246125c1cSMilan Broz 	}
226346125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
226446125c1cSMilan Broz 
2265b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
2266b44ebeb0SMikulas Patocka 
226746125c1cSMilan Broz 	return r;
226846125c1cSMilan Broz }
226946125c1cSMilan Broz 
22701da177e4SLinus Torvalds /*
22711da177e4SLinus Torvalds  * Process the deferred bios
22721da177e4SLinus Torvalds  */
2273ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
22741da177e4SLinus Torvalds {
2275ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2276ef208587SMikulas Patocka 						work);
22776d6f10dfSMilan Broz 	struct bio *c;
22781da177e4SLinus Torvalds 
22796a8736d1STejun Heo 	down_read(&md->io_lock);
2280ef208587SMikulas Patocka 
22813b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2282022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2283022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2284022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2285022c2611SMikulas Patocka 
22866a8736d1STejun Heo 		if (!c)
2287df12ee99SAlasdair G Kergon 			break;
228873d410c0SMilan Broz 
22896a8736d1STejun Heo 		up_read(&md->io_lock);
22903b00b203SMikulas Patocka 
2291e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2292e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2293af7e466aSMikulas Patocka 		else
2294df12ee99SAlasdair G Kergon 			__split_and_process_bio(md, c);
22956a8736d1STejun Heo 
22966a8736d1STejun Heo 		down_read(&md->io_lock);
2297e6ee8c0bSKiyoshi Ueda 	}
22983b00b203SMikulas Patocka 
22996a8736d1STejun Heo 	up_read(&md->io_lock);
23001da177e4SLinus Torvalds }
23011da177e4SLinus Torvalds 
23029a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2303304f3f6aSMilan Broz {
23043b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
23053b00b203SMikulas Patocka 	smp_mb__after_clear_bit();
230653d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2307304f3f6aSMilan Broz }
2308304f3f6aSMilan Broz 
23091da177e4SLinus Torvalds /*
2310042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
23111da177e4SLinus Torvalds  */
2312042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
23131da177e4SLinus Torvalds {
2314042d2a9bSAlasdair G Kergon 	struct dm_table *map = ERR_PTR(-EINVAL);
2315754c5fc7SMike Snitzer 	struct queue_limits limits;
2316042d2a9bSAlasdair G Kergon 	int r;
23171da177e4SLinus Torvalds 
2318e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
23191da177e4SLinus Torvalds 
23201da177e4SLinus Torvalds 	/* device must be suspended */
23214f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
232293c534aeSAlasdair G Kergon 		goto out;
23231da177e4SLinus Torvalds 
2324754c5fc7SMike Snitzer 	r = dm_calculate_queue_limits(table, &limits);
2325042d2a9bSAlasdair G Kergon 	if (r) {
2326042d2a9bSAlasdair G Kergon 		map = ERR_PTR(r);
2327754c5fc7SMike Snitzer 		goto out;
2328042d2a9bSAlasdair G Kergon 	}
2329754c5fc7SMike Snitzer 
2330042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
23311da177e4SLinus Torvalds 
233293c534aeSAlasdair G Kergon out:
2333e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2334042d2a9bSAlasdair G Kergon 	return map;
23351da177e4SLinus Torvalds }
23361da177e4SLinus Torvalds 
23371da177e4SLinus Torvalds /*
23381da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
23391da177e4SLinus Torvalds  * device.
23401da177e4SLinus Torvalds  */
23412ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
23421da177e4SLinus Torvalds {
2343e39e2e95SAlasdair G Kergon 	int r;
23441da177e4SLinus Torvalds 
23451da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2346dfbe03f6SAlasdair G Kergon 
2347db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2348dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2349cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2350e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2351e39e2e95SAlasdair G Kergon 		return r;
2352dfbe03f6SAlasdair G Kergon 	}
2353dfbe03f6SAlasdair G Kergon 
2354aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2355aa8d7c2fSAlasdair G Kergon 
23561da177e4SLinus Torvalds 	return 0;
23571da177e4SLinus Torvalds }
23581da177e4SLinus Torvalds 
23592ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
23601da177e4SLinus Torvalds {
2361aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2362aa8d7c2fSAlasdair G Kergon 		return;
2363aa8d7c2fSAlasdair G Kergon 
2364db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
23651da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2366aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
23671da177e4SLinus Torvalds }
23681da177e4SLinus Torvalds 
23691da177e4SLinus Torvalds /*
23701da177e4SLinus Torvalds  * We need to be able to change a mapping table under a mounted
23711da177e4SLinus Torvalds  * filesystem.  For example we might want to move some data in
23721da177e4SLinus Torvalds  * the background.  Before the table can be swapped with
23731da177e4SLinus Torvalds  * dm_bind_table, dm_suspend must be called to flush any in
23741da177e4SLinus Torvalds  * flight bios and ensure that any further io gets deferred.
23751da177e4SLinus Torvalds  */
2376cec47e3dSKiyoshi Ueda /*
2377cec47e3dSKiyoshi Ueda  * Suspend mechanism in request-based dm.
2378cec47e3dSKiyoshi Ueda  *
23799f518b27SKiyoshi Ueda  * 1. Flush all I/Os by lock_fs() if needed.
23809f518b27SKiyoshi Ueda  * 2. Stop dispatching any I/O by stopping the request_queue.
23819f518b27SKiyoshi Ueda  * 3. Wait for all in-flight I/Os to be completed or requeued.
2382cec47e3dSKiyoshi Ueda  *
23839f518b27SKiyoshi Ueda  * To abort suspend, start the request_queue.
2384cec47e3dSKiyoshi Ueda  */
2385a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
23861da177e4SLinus Torvalds {
23872ca3310eSAlasdair G Kergon 	struct dm_table *map = NULL;
238846125c1cSMilan Broz 	int r = 0;
2389a3d77d35SKiyoshi Ueda 	int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
23902e93ccc1SKiyoshi Ueda 	int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
23911da177e4SLinus Torvalds 
2392e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
23932ca3310eSAlasdair G Kergon 
23944f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
239573d410c0SMilan Broz 		r = -EINVAL;
2396d287483dSAlasdair G Kergon 		goto out_unlock;
239773d410c0SMilan Broz 	}
23981da177e4SLinus Torvalds 
23997c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
2400cf222b37SAlasdair G Kergon 
24012e93ccc1SKiyoshi Ueda 	/*
24022e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
24032e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
24042e93ccc1SKiyoshi Ueda 	 */
24052e93ccc1SKiyoshi Ueda 	if (noflush)
24062e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
24072e93ccc1SKiyoshi Ueda 
2408436d4108SAlasdair G Kergon 	/* This does not get reverted if there's an error later. */
24091da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
24101da177e4SLinus Torvalds 
24112e93ccc1SKiyoshi Ueda 	/*
24129f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
24139f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
24149f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
24159f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
24162e93ccc1SKiyoshi Ueda 	 */
241732a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
24182ca3310eSAlasdair G Kergon 		r = lock_fs(md);
24192ca3310eSAlasdair G Kergon 		if (r)
24202ca3310eSAlasdair G Kergon 			goto out;
2421aa8d7c2fSAlasdair G Kergon 	}
24221da177e4SLinus Torvalds 
24231da177e4SLinus Torvalds 	/*
24243b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
24253b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
24263b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
24273b00b203SMikulas Patocka 	 * dm_wq_work.
24283b00b203SMikulas Patocka 	 *
24293b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
24303b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
24316a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
24326a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
24336a8736d1STejun Heo 	 * flush_workqueue(md->wq).
24341da177e4SLinus Torvalds 	 */
24352ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
24361eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
24372ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
24381da177e4SLinus Torvalds 
2439d0bcb878SKiyoshi Ueda 	/*
244029e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
244129e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2442d0bcb878SKiyoshi Ueda 	 */
2443cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
24449f518b27SKiyoshi Ueda 		stop_queue(md->queue);
2445cec47e3dSKiyoshi Ueda 
2446d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2447d0bcb878SKiyoshi Ueda 
24481da177e4SLinus Torvalds 	/*
24493b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
24503b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
24513b00b203SMikulas Patocka 	 * to finish.
24521da177e4SLinus Torvalds 	 */
2453401600dfSMikulas Patocka 	r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
24541da177e4SLinus Torvalds 
24552ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
24566d6f10dfSMilan Broz 	if (noflush)
2457022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
245894d6351eSMilan Broz 	up_write(&md->io_lock);
24592e93ccc1SKiyoshi Ueda 
24601da177e4SLinus Torvalds 	/* were we interrupted ? */
246146125c1cSMilan Broz 	if (r < 0) {
24629a1fb464SMikulas Patocka 		dm_queue_flush(md);
246373d410c0SMilan Broz 
2464cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
24659f518b27SKiyoshi Ueda 			start_queue(md->queue);
2466cec47e3dSKiyoshi Ueda 
24672ca3310eSAlasdair G Kergon 		unlock_fs(md);
24682e93ccc1SKiyoshi Ueda 		goto out; /* pushback list is already flushed, so skip flush */
24692ca3310eSAlasdair G Kergon 	}
24702ca3310eSAlasdair G Kergon 
24713b00b203SMikulas Patocka 	/*
24723b00b203SMikulas Patocka 	 * If dm_wait_for_completion returned 0, the device is completely
24733b00b203SMikulas Patocka 	 * quiescent now. There is no request-processing activity. All new
24743b00b203SMikulas Patocka 	 * requests are being added to md->deferred list.
24753b00b203SMikulas Patocka 	 */
24763b00b203SMikulas Patocka 
24771da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
24781da177e4SLinus Torvalds 
24794d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
24804d4471cbSKiyoshi Ueda 
24812ca3310eSAlasdair G Kergon out:
24821da177e4SLinus Torvalds 	dm_table_put(map);
2483d287483dSAlasdair G Kergon 
2484d287483dSAlasdair G Kergon out_unlock:
2485e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2486cf222b37SAlasdair G Kergon 	return r;
24871da177e4SLinus Torvalds }
24881da177e4SLinus Torvalds 
24891da177e4SLinus Torvalds int dm_resume(struct mapped_device *md)
24901da177e4SLinus Torvalds {
2491cf222b37SAlasdair G Kergon 	int r = -EINVAL;
2492cf222b37SAlasdair G Kergon 	struct dm_table *map = NULL;
24931da177e4SLinus Torvalds 
2494e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
24954f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
2496cf222b37SAlasdair G Kergon 		goto out;
2497cf222b37SAlasdair G Kergon 
24987c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
24992ca3310eSAlasdair G Kergon 	if (!map || !dm_table_get_size(map))
2500cf222b37SAlasdair G Kergon 		goto out;
25011da177e4SLinus Torvalds 
25028757b776SMilan Broz 	r = dm_table_resume_targets(map);
25038757b776SMilan Broz 	if (r)
25048757b776SMilan Broz 		goto out;
25052ca3310eSAlasdair G Kergon 
25069a1fb464SMikulas Patocka 	dm_queue_flush(md);
25072ca3310eSAlasdair G Kergon 
2508cec47e3dSKiyoshi Ueda 	/*
2509cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2510cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2511cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2512cec47e3dSKiyoshi Ueda 	 */
2513cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2514cec47e3dSKiyoshi Ueda 		start_queue(md->queue);
2515cec47e3dSKiyoshi Ueda 
25162ca3310eSAlasdair G Kergon 	unlock_fs(md);
25172ca3310eSAlasdair G Kergon 
25182ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
25192ca3310eSAlasdair G Kergon 
2520cf222b37SAlasdair G Kergon 	r = 0;
2521cf222b37SAlasdair G Kergon out:
2522cf222b37SAlasdair G Kergon 	dm_table_put(map);
2523e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
25242ca3310eSAlasdair G Kergon 
2525cf222b37SAlasdair G Kergon 	return r;
25261da177e4SLinus Torvalds }
25271da177e4SLinus Torvalds 
25281da177e4SLinus Torvalds /*-----------------------------------------------------------------
25291da177e4SLinus Torvalds  * Event notification.
25301da177e4SLinus Torvalds  *---------------------------------------------------------------*/
25313abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
253260935eb2SMilan Broz 		       unsigned cookie)
253369267a30SAlasdair G Kergon {
253460935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
253560935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
253660935eb2SMilan Broz 
253760935eb2SMilan Broz 	if (!cookie)
25383abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
253960935eb2SMilan Broz 	else {
254060935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
254160935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
25423abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
25433abf85b5SPeter Rajnoha 					  action, envp);
254460935eb2SMilan Broz 	}
254569267a30SAlasdair G Kergon }
254669267a30SAlasdair G Kergon 
25477a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
25487a8c3d3bSMike Anderson {
25497a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
25507a8c3d3bSMike Anderson }
25517a8c3d3bSMike Anderson 
25521da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
25531da177e4SLinus Torvalds {
25541da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
25551da177e4SLinus Torvalds }
25561da177e4SLinus Torvalds 
25571da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
25581da177e4SLinus Torvalds {
25591da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
25601da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
25611da177e4SLinus Torvalds }
25621da177e4SLinus Torvalds 
25637a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
25647a8c3d3bSMike Anderson {
25657a8c3d3bSMike Anderson 	unsigned long flags;
25667a8c3d3bSMike Anderson 
25677a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
25687a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
25697a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
25707a8c3d3bSMike Anderson }
25717a8c3d3bSMike Anderson 
25721da177e4SLinus Torvalds /*
25731da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
25741da177e4SLinus Torvalds  * count on 'md'.
25751da177e4SLinus Torvalds  */
25761da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
25771da177e4SLinus Torvalds {
25781da177e4SLinus Torvalds 	return md->disk;
25791da177e4SLinus Torvalds }
25801da177e4SLinus Torvalds 
2581784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2582784aae73SMilan Broz {
2583784aae73SMilan Broz 	return &md->kobj;
2584784aae73SMilan Broz }
2585784aae73SMilan Broz 
2586784aae73SMilan Broz /*
2587784aae73SMilan Broz  * struct mapped_device should not be exported outside of dm.c
2588784aae73SMilan Broz  * so use this check to verify that kobj is part of md structure
2589784aae73SMilan Broz  */
2590784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2591784aae73SMilan Broz {
2592784aae73SMilan Broz 	struct mapped_device *md;
2593784aae73SMilan Broz 
2594784aae73SMilan Broz 	md = container_of(kobj, struct mapped_device, kobj);
2595784aae73SMilan Broz 	if (&md->kobj != kobj)
2596784aae73SMilan Broz 		return NULL;
2597784aae73SMilan Broz 
25984d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
2599432a212cSMike Anderson 	    dm_deleting_md(md))
26004d89b7b4SMilan Broz 		return NULL;
26014d89b7b4SMilan Broz 
2602784aae73SMilan Broz 	dm_get(md);
2603784aae73SMilan Broz 	return md;
2604784aae73SMilan Broz }
2605784aae73SMilan Broz 
26064f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
26071da177e4SLinus Torvalds {
26081da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
26091da177e4SLinus Torvalds }
26101da177e4SLinus Torvalds 
261164dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
261264dbce58SKiyoshi Ueda {
2613ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
261464dbce58SKiyoshi Ueda }
261564dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
261664dbce58SKiyoshi Ueda 
26172e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
26182e93ccc1SKiyoshi Ueda {
2619ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
26202e93ccc1SKiyoshi Ueda }
26212e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
26222e93ccc1SKiyoshi Ueda 
2623*a91a2785SMartin K. Petersen struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity)
2624e6ee8c0bSKiyoshi Ueda {
2625e6ee8c0bSKiyoshi Ueda 	struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2626*a91a2785SMartin K. Petersen 	unsigned int pool_size = (type == DM_TYPE_BIO_BASED) ? 16 : MIN_IOS;
2627e6ee8c0bSKiyoshi Ueda 
2628e6ee8c0bSKiyoshi Ueda 	if (!pools)
2629e6ee8c0bSKiyoshi Ueda 		return NULL;
2630e6ee8c0bSKiyoshi Ueda 
2631e6ee8c0bSKiyoshi Ueda 	pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2632e6ee8c0bSKiyoshi Ueda 			 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2633e6ee8c0bSKiyoshi Ueda 			 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2634e6ee8c0bSKiyoshi Ueda 	if (!pools->io_pool)
2635e6ee8c0bSKiyoshi Ueda 		goto free_pools_and_out;
2636e6ee8c0bSKiyoshi Ueda 
2637e6ee8c0bSKiyoshi Ueda 	pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2638e6ee8c0bSKiyoshi Ueda 			  mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2639e6ee8c0bSKiyoshi Ueda 			  mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2640e6ee8c0bSKiyoshi Ueda 	if (!pools->tio_pool)
2641e6ee8c0bSKiyoshi Ueda 		goto free_io_pool_and_out;
2642e6ee8c0bSKiyoshi Ueda 
2643*a91a2785SMartin K. Petersen 	pools->bs = bioset_create(pool_size, 0);
2644e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
2645e6ee8c0bSKiyoshi Ueda 		goto free_tio_pool_and_out;
2646e6ee8c0bSKiyoshi Ueda 
2647*a91a2785SMartin K. Petersen 	if (integrity && bioset_integrity_create(pools->bs, pool_size))
2648*a91a2785SMartin K. Petersen 		goto free_bioset_and_out;
2649*a91a2785SMartin K. Petersen 
2650e6ee8c0bSKiyoshi Ueda 	return pools;
2651e6ee8c0bSKiyoshi Ueda 
2652*a91a2785SMartin K. Petersen free_bioset_and_out:
2653*a91a2785SMartin K. Petersen 	bioset_free(pools->bs);
2654*a91a2785SMartin K. Petersen 
2655e6ee8c0bSKiyoshi Ueda free_tio_pool_and_out:
2656e6ee8c0bSKiyoshi Ueda 	mempool_destroy(pools->tio_pool);
2657e6ee8c0bSKiyoshi Ueda 
2658e6ee8c0bSKiyoshi Ueda free_io_pool_and_out:
2659e6ee8c0bSKiyoshi Ueda 	mempool_destroy(pools->io_pool);
2660e6ee8c0bSKiyoshi Ueda 
2661e6ee8c0bSKiyoshi Ueda free_pools_and_out:
2662e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2663e6ee8c0bSKiyoshi Ueda 
2664e6ee8c0bSKiyoshi Ueda 	return NULL;
2665e6ee8c0bSKiyoshi Ueda }
2666e6ee8c0bSKiyoshi Ueda 
2667e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
2668e6ee8c0bSKiyoshi Ueda {
2669e6ee8c0bSKiyoshi Ueda 	if (!pools)
2670e6ee8c0bSKiyoshi Ueda 		return;
2671e6ee8c0bSKiyoshi Ueda 
2672e6ee8c0bSKiyoshi Ueda 	if (pools->io_pool)
2673e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->io_pool);
2674e6ee8c0bSKiyoshi Ueda 
2675e6ee8c0bSKiyoshi Ueda 	if (pools->tio_pool)
2676e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->tio_pool);
2677e6ee8c0bSKiyoshi Ueda 
2678e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
2679e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
2680e6ee8c0bSKiyoshi Ueda 
2681e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2682e6ee8c0bSKiyoshi Ueda }
2683e6ee8c0bSKiyoshi Ueda 
268483d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
26851da177e4SLinus Torvalds 	.open = dm_blk_open,
26861da177e4SLinus Torvalds 	.release = dm_blk_close,
2687aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
26883ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
26891da177e4SLinus Torvalds 	.owner = THIS_MODULE
26901da177e4SLinus Torvalds };
26911da177e4SLinus Torvalds 
26921da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo);
26931da177e4SLinus Torvalds 
26941da177e4SLinus Torvalds /*
26951da177e4SLinus Torvalds  * module hooks
26961da177e4SLinus Torvalds  */
26971da177e4SLinus Torvalds module_init(dm_init);
26981da177e4SLinus Torvalds module_exit(dm_exit);
26991da177e4SLinus Torvalds 
27001da177e4SLinus Torvalds module_param(major, uint, 0);
27011da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
27021da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
27031da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
27041da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2705