xref: /openbmc/linux/drivers/md/dm.c (revision 55a62eef8d1b50ceff3b7bf46851103bdcc7e5b0)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3784aae73SMilan Broz  * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This file is released under the GPL.
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include "dm.h"
951e5b2bdSMike Anderson #include "dm-uevent.h"
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/init.h>
121da177e4SLinus Torvalds #include <linux/module.h>
1348c9c27bSArjan van de Ven #include <linux/mutex.h>
141da177e4SLinus Torvalds #include <linux/moduleparam.h>
151da177e4SLinus Torvalds #include <linux/blkpg.h>
161da177e4SLinus Torvalds #include <linux/bio.h>
171da177e4SLinus Torvalds #include <linux/mempool.h>
181da177e4SLinus Torvalds #include <linux/slab.h>
191da177e4SLinus Torvalds #include <linux/idr.h>
203ac51e74SDarrick J. Wong #include <linux/hdreg.h>
213f77316dSKiyoshi Ueda #include <linux/delay.h>
2255782138SLi Zefan 
2355782138SLi Zefan #include <trace/events/block.h>
241da177e4SLinus Torvalds 
2572d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
2672d94861SAlasdair G Kergon 
2771a16736SNamhyung Kim #ifdef CONFIG_PRINTK
2871a16736SNamhyung Kim /*
2971a16736SNamhyung Kim  * ratelimit state to be used in DMXXX_LIMIT().
3071a16736SNamhyung Kim  */
3171a16736SNamhyung Kim DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
3271a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_INTERVAL,
3371a16736SNamhyung Kim 		       DEFAULT_RATELIMIT_BURST);
3471a16736SNamhyung Kim EXPORT_SYMBOL(dm_ratelimit_state);
3571a16736SNamhyung Kim #endif
3671a16736SNamhyung Kim 
3760935eb2SMilan Broz /*
3860935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
3960935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
4060935eb2SMilan Broz  */
4160935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
4260935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
4360935eb2SMilan Broz 
441da177e4SLinus Torvalds static const char *_name = DM_NAME;
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds static unsigned int major = 0;
471da177e4SLinus Torvalds static unsigned int _major = 0;
481da177e4SLinus Torvalds 
49d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr);
50d15b774cSAlasdair G Kergon 
51f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
521da177e4SLinus Torvalds /*
538fbf26adSKiyoshi Ueda  * For bio-based dm.
541da177e4SLinus Torvalds  * One of these is allocated per bio.
551da177e4SLinus Torvalds  */
561da177e4SLinus Torvalds struct dm_io {
571da177e4SLinus Torvalds 	struct mapped_device *md;
581da177e4SLinus Torvalds 	int error;
591da177e4SLinus Torvalds 	atomic_t io_count;
606ae2fa67SRichard Kennedy 	struct bio *bio;
613eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
62f88fb981SKiyoshi Ueda 	spinlock_t endio_lock;
631da177e4SLinus Torvalds };
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /*
668fbf26adSKiyoshi Ueda  * For request-based dm.
678fbf26adSKiyoshi Ueda  * One of these is allocated per request.
688fbf26adSKiyoshi Ueda  */
698fbf26adSKiyoshi Ueda struct dm_rq_target_io {
708fbf26adSKiyoshi Ueda 	struct mapped_device *md;
718fbf26adSKiyoshi Ueda 	struct dm_target *ti;
728fbf26adSKiyoshi Ueda 	struct request *orig, clone;
738fbf26adSKiyoshi Ueda 	int error;
748fbf26adSKiyoshi Ueda 	union map_info info;
758fbf26adSKiyoshi Ueda };
768fbf26adSKiyoshi Ueda 
778fbf26adSKiyoshi Ueda /*
7894818742SKent Overstreet  * For request-based dm - the bio clones we allocate are embedded in these
7994818742SKent Overstreet  * structs.
8094818742SKent Overstreet  *
8194818742SKent Overstreet  * We allocate these with bio_alloc_bioset, using the front_pad parameter when
8294818742SKent Overstreet  * the bioset is created - this means the bio has to come at the end of the
8394818742SKent Overstreet  * struct.
848fbf26adSKiyoshi Ueda  */
858fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info {
868fbf26adSKiyoshi Ueda 	struct bio *orig;
87cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio;
8894818742SKent Overstreet 	struct bio clone;
898fbf26adSKiyoshi Ueda };
908fbf26adSKiyoshi Ueda 
911da177e4SLinus Torvalds union map_info *dm_get_mapinfo(struct bio *bio)
921da177e4SLinus Torvalds {
931da177e4SLinus Torvalds 	if (bio && bio->bi_private)
94028867acSAlasdair G Kergon 		return &((struct dm_target_io *)bio->bi_private)->info;
951da177e4SLinus Torvalds 	return NULL;
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
98cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq)
99cec47e3dSKiyoshi Ueda {
100cec47e3dSKiyoshi Ueda 	if (rq && rq->end_io_data)
101cec47e3dSKiyoshi Ueda 		return &((struct dm_rq_target_io *)rq->end_io_data)->info;
102cec47e3dSKiyoshi Ueda 	return NULL;
103cec47e3dSKiyoshi Ueda }
104cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
105cec47e3dSKiyoshi Ueda 
106ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
107ba61fdd1SJeff Mahoney 
1081da177e4SLinus Torvalds /*
1091da177e4SLinus Torvalds  * Bits for the md->flags field.
1101da177e4SLinus Torvalds  */
1111eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
1121da177e4SLinus Torvalds #define DMF_SUSPENDED 1
113aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
114fba9f90eSJeff Mahoney #define DMF_FREEING 3
1155c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
1162e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
117d5b9dd04SMikulas Patocka #define DMF_MERGE_IS_OPTIONAL 6
1181da177e4SLinus Torvalds 
119304f3f6aSMilan Broz /*
120304f3f6aSMilan Broz  * Work processed by per-device workqueue.
121304f3f6aSMilan Broz  */
1221da177e4SLinus Torvalds struct mapped_device {
1232ca3310eSAlasdair G Kergon 	struct rw_semaphore io_lock;
124e61290a4SDaniel Walker 	struct mutex suspend_lock;
1251da177e4SLinus Torvalds 	rwlock_t map_lock;
1261da177e4SLinus Torvalds 	atomic_t holders;
1275c6bd75dSAlasdair G Kergon 	atomic_t open_count;
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	unsigned long flags;
1301da177e4SLinus Torvalds 
131165125e1SJens Axboe 	struct request_queue *queue;
132a5664dadSMike Snitzer 	unsigned type;
1334a0b4ddfSMike Snitzer 	/* Protect queue and type against concurrent access. */
134a5664dadSMike Snitzer 	struct mutex type_lock;
135a5664dadSMike Snitzer 
13636a0456fSAlasdair G Kergon 	struct target_type *immutable_target_type;
13736a0456fSAlasdair G Kergon 
1381da177e4SLinus Torvalds 	struct gendisk *disk;
1397e51f257SMike Anderson 	char name[16];
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	void *interface_ptr;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	/*
1441da177e4SLinus Torvalds 	 * A list of ios that arrived while we were suspended.
1451da177e4SLinus Torvalds 	 */
146316d315bSNikanth Karthikesan 	atomic_t pending[2];
1471da177e4SLinus Torvalds 	wait_queue_head_t wait;
14853d5914fSMikulas Patocka 	struct work_struct work;
1491da177e4SLinus Torvalds 	struct bio_list deferred;
150022c2611SMikulas Patocka 	spinlock_t deferred_lock;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	/*
15329e4013dSTejun Heo 	 * Processing queue (flush)
154304f3f6aSMilan Broz 	 */
155304f3f6aSMilan Broz 	struct workqueue_struct *wq;
156304f3f6aSMilan Broz 
157304f3f6aSMilan Broz 	/*
1581da177e4SLinus Torvalds 	 * The current mapping.
1591da177e4SLinus Torvalds 	 */
1601da177e4SLinus Torvalds 	struct dm_table *map;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	/*
1631da177e4SLinus Torvalds 	 * io objects are allocated from here.
1641da177e4SLinus Torvalds 	 */
1651da177e4SLinus Torvalds 	mempool_t *io_pool;
1661da177e4SLinus Torvalds 	mempool_t *tio_pool;
1671da177e4SLinus Torvalds 
1689faf400fSStefan Bader 	struct bio_set *bs;
1699faf400fSStefan Bader 
1701da177e4SLinus Torvalds 	/*
1711da177e4SLinus Torvalds 	 * Event handling.
1721da177e4SLinus Torvalds 	 */
1731da177e4SLinus Torvalds 	atomic_t event_nr;
1741da177e4SLinus Torvalds 	wait_queue_head_t eventq;
1757a8c3d3bSMike Anderson 	atomic_t uevent_seq;
1767a8c3d3bSMike Anderson 	struct list_head uevent_list;
1777a8c3d3bSMike Anderson 	spinlock_t uevent_lock; /* Protect access to uevent_list */
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	/*
1801da177e4SLinus Torvalds 	 * freeze/thaw support require holding onto a super block
1811da177e4SLinus Torvalds 	 */
1821da177e4SLinus Torvalds 	struct super_block *frozen_sb;
183db8fef4fSMikulas Patocka 	struct block_device *bdev;
1843ac51e74SDarrick J. Wong 
1853ac51e74SDarrick J. Wong 	/* forced geometry settings */
1863ac51e74SDarrick J. Wong 	struct hd_geometry geometry;
187784aae73SMilan Broz 
188784aae73SMilan Broz 	/* sysfs handle */
189784aae73SMilan Broz 	struct kobject kobj;
19052b1fd5aSMikulas Patocka 
191d87f4c14STejun Heo 	/* zero-length flush that will be cloned and submitted to targets */
192d87f4c14STejun Heo 	struct bio flush_bio;
1931da177e4SLinus Torvalds };
1941da177e4SLinus Torvalds 
195e6ee8c0bSKiyoshi Ueda /*
196e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
197e6ee8c0bSKiyoshi Ueda  */
198e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
199e6ee8c0bSKiyoshi Ueda 	mempool_t *io_pool;
200e6ee8c0bSKiyoshi Ueda 	mempool_t *tio_pool;
201e6ee8c0bSKiyoshi Ueda 	struct bio_set *bs;
202e6ee8c0bSKiyoshi Ueda };
203e6ee8c0bSKiyoshi Ueda 
2041da177e4SLinus Torvalds #define MIN_IOS 256
205e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
2068fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
20794818742SKent Overstreet 
20894818742SKent Overstreet /*
20994818742SKent Overstreet  * Unused now, and needs to be deleted. But since io_pool is overloaded and it's
21094818742SKent Overstreet  * still used for _io_cache, I'm leaving this for a later cleanup
21194818742SKent Overstreet  */
2128fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_bio_info_cache;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds static int __init local_init(void)
2151da177e4SLinus Torvalds {
21651157b4aSKiyoshi Ueda 	int r = -ENOMEM;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
219028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
2201da177e4SLinus Torvalds 	if (!_io_cache)
22151157b4aSKiyoshi Ueda 		return r;
2221da177e4SLinus Torvalds 
2238fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
2248fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
225dba14160SMikulas Patocka 		goto out_free_io_cache;
2268fbf26adSKiyoshi Ueda 
2278fbf26adSKiyoshi Ueda 	_rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
2288fbf26adSKiyoshi Ueda 	if (!_rq_bio_info_cache)
2298fbf26adSKiyoshi Ueda 		goto out_free_rq_tio_cache;
2308fbf26adSKiyoshi Ueda 
23151e5b2bdSMike Anderson 	r = dm_uevent_init();
23251157b4aSKiyoshi Ueda 	if (r)
2338fbf26adSKiyoshi Ueda 		goto out_free_rq_bio_info_cache;
23451e5b2bdSMike Anderson 
2351da177e4SLinus Torvalds 	_major = major;
2361da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
23751157b4aSKiyoshi Ueda 	if (r < 0)
23851157b4aSKiyoshi Ueda 		goto out_uevent_exit;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	if (!_major)
2411da177e4SLinus Torvalds 		_major = r;
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	return 0;
24451157b4aSKiyoshi Ueda 
24551157b4aSKiyoshi Ueda out_uevent_exit:
24651157b4aSKiyoshi Ueda 	dm_uevent_exit();
2478fbf26adSKiyoshi Ueda out_free_rq_bio_info_cache:
2488fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_bio_info_cache);
2498fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
2508fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
25151157b4aSKiyoshi Ueda out_free_io_cache:
25251157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
25351157b4aSKiyoshi Ueda 
25451157b4aSKiyoshi Ueda 	return r;
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds static void local_exit(void)
2581da177e4SLinus Torvalds {
2598fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_bio_info_cache);
2608fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
2611da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
26200d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
26351e5b2bdSMike Anderson 	dm_uevent_exit();
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	_major = 0;
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	DMINFO("cleaned up");
2681da177e4SLinus Torvalds }
2691da177e4SLinus Torvalds 
270b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
2711da177e4SLinus Torvalds 	local_init,
2721da177e4SLinus Torvalds 	dm_target_init,
2731da177e4SLinus Torvalds 	dm_linear_init,
2741da177e4SLinus Torvalds 	dm_stripe_init,
275952b3557SMikulas Patocka 	dm_io_init,
276945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2771da177e4SLinus Torvalds 	dm_interface_init,
2781da177e4SLinus Torvalds };
2791da177e4SLinus Torvalds 
280b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2811da177e4SLinus Torvalds 	local_exit,
2821da177e4SLinus Torvalds 	dm_target_exit,
2831da177e4SLinus Torvalds 	dm_linear_exit,
2841da177e4SLinus Torvalds 	dm_stripe_exit,
285952b3557SMikulas Patocka 	dm_io_exit,
286945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
2871da177e4SLinus Torvalds 	dm_interface_exit,
2881da177e4SLinus Torvalds };
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds static int __init dm_init(void)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	int r, i;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
2971da177e4SLinus Torvalds 		r = _inits[i]();
2981da177e4SLinus Torvalds 		if (r)
2991da177e4SLinus Torvalds 			goto bad;
3001da177e4SLinus Torvalds 	}
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	return 0;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds       bad:
3051da177e4SLinus Torvalds 	while (i--)
3061da177e4SLinus Torvalds 		_exits[i]();
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	return r;
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds static void __exit dm_exit(void)
3121da177e4SLinus Torvalds {
3131da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	while (i--)
3161da177e4SLinus Torvalds 		_exits[i]();
317d15b774cSAlasdair G Kergon 
318d15b774cSAlasdair G Kergon 	/*
319d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
320d15b774cSAlasdair G Kergon 	 */
321d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds /*
3251da177e4SLinus Torvalds  * Block device functions
3261da177e4SLinus Torvalds  */
327432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
328432a212cSMike Anderson {
329432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
330432a212cSMike Anderson }
331432a212cSMike Anderson 
332fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	struct mapped_device *md;
3351da177e4SLinus Torvalds 
336fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
337fba9f90eSJeff Mahoney 
338fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
339fba9f90eSJeff Mahoney 	if (!md)
340fba9f90eSJeff Mahoney 		goto out;
341fba9f90eSJeff Mahoney 
3425c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
343432a212cSMike Anderson 	    dm_deleting_md(md)) {
344fba9f90eSJeff Mahoney 		md = NULL;
345fba9f90eSJeff Mahoney 		goto out;
346fba9f90eSJeff Mahoney 	}
347fba9f90eSJeff Mahoney 
3481da177e4SLinus Torvalds 	dm_get(md);
3495c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
350fba9f90eSJeff Mahoney 
351fba9f90eSJeff Mahoney out:
352fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
353fba9f90eSJeff Mahoney 
354fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
357fe5f9f2cSAl Viro static int dm_blk_close(struct gendisk *disk, fmode_t mode)
3581da177e4SLinus Torvalds {
359fe5f9f2cSAl Viro 	struct mapped_device *md = disk->private_data;
3606e9624b8SArnd Bergmann 
3614a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
3624a1aeb98SMilan Broz 
3635c6bd75dSAlasdair G Kergon 	atomic_dec(&md->open_count);
3641da177e4SLinus Torvalds 	dm_put(md);
3654a1aeb98SMilan Broz 
3664a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
3676e9624b8SArnd Bergmann 
3681da177e4SLinus Torvalds 	return 0;
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds 
3715c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3725c6bd75dSAlasdair G Kergon {
3735c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3745c6bd75dSAlasdair G Kergon }
3755c6bd75dSAlasdair G Kergon 
3765c6bd75dSAlasdair G Kergon /*
3775c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3785c6bd75dSAlasdair G Kergon  */
3795c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md)
3805c6bd75dSAlasdair G Kergon {
3815c6bd75dSAlasdair G Kergon 	int r = 0;
3825c6bd75dSAlasdair G Kergon 
3835c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
3845c6bd75dSAlasdair G Kergon 
3855c6bd75dSAlasdair G Kergon 	if (dm_open_count(md))
3865c6bd75dSAlasdair G Kergon 		r = -EBUSY;
3875c6bd75dSAlasdair G Kergon 	else
3885c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
3895c6bd75dSAlasdair G Kergon 
3905c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
3915c6bd75dSAlasdair G Kergon 
3925c6bd75dSAlasdair G Kergon 	return r;
3935c6bd75dSAlasdair G Kergon }
3945c6bd75dSAlasdair G Kergon 
3953ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3963ac51e74SDarrick J. Wong {
3973ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
3983ac51e74SDarrick J. Wong 
3993ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
4003ac51e74SDarrick J. Wong }
4013ac51e74SDarrick J. Wong 
402fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
403aa129a22SMilan Broz 			unsigned int cmd, unsigned long arg)
404aa129a22SMilan Broz {
405fe5f9f2cSAl Viro 	struct mapped_device *md = bdev->bd_disk->private_data;
4067c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
407aa129a22SMilan Broz 	struct dm_target *tgt;
408aa129a22SMilan Broz 	int r = -ENOTTY;
409aa129a22SMilan Broz 
410aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
411aa129a22SMilan Broz 		goto out;
412aa129a22SMilan Broz 
413aa129a22SMilan Broz 	/* We only support devices that have a single target */
414aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
415aa129a22SMilan Broz 		goto out;
416aa129a22SMilan Broz 
417aa129a22SMilan Broz 	tgt = dm_table_get_target(map, 0);
418aa129a22SMilan Broz 
4194f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
420aa129a22SMilan Broz 		r = -EAGAIN;
421aa129a22SMilan Broz 		goto out;
422aa129a22SMilan Broz 	}
423aa129a22SMilan Broz 
424aa129a22SMilan Broz 	if (tgt->type->ioctl)
425647b3d00SAl Viro 		r = tgt->type->ioctl(tgt, cmd, arg);
426aa129a22SMilan Broz 
427aa129a22SMilan Broz out:
428aa129a22SMilan Broz 	dm_table_put(map);
429aa129a22SMilan Broz 
430aa129a22SMilan Broz 	return r;
431aa129a22SMilan Broz }
432aa129a22SMilan Broz 
433028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
4341da177e4SLinus Torvalds {
4351da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
438028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
4391da177e4SLinus Torvalds {
4401da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
443028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
4441da177e4SLinus Torvalds {
445dba14160SMikulas Patocka 	bio_put(&tio->clone);
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
44808885643SKiyoshi Ueda static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
44908885643SKiyoshi Ueda 					    gfp_t gfp_mask)
450cec47e3dSKiyoshi Ueda {
45108885643SKiyoshi Ueda 	return mempool_alloc(md->tio_pool, gfp_mask);
452cec47e3dSKiyoshi Ueda }
453cec47e3dSKiyoshi Ueda 
454cec47e3dSKiyoshi Ueda static void free_rq_tio(struct dm_rq_target_io *tio)
455cec47e3dSKiyoshi Ueda {
456cec47e3dSKiyoshi Ueda 	mempool_free(tio, tio->md->tio_pool);
457cec47e3dSKiyoshi Ueda }
458cec47e3dSKiyoshi Ueda 
45990abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md)
46090abb8c4SKiyoshi Ueda {
46190abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
46290abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
46390abb8c4SKiyoshi Ueda }
46490abb8c4SKiyoshi Ueda 
4653eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
4663eaf840eSJun'ichi "Nick" Nomura {
4673eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
468c9959059STejun Heo 	int cpu;
469316d315bSNikanth Karthikesan 	int rw = bio_data_dir(io->bio);
4703eaf840eSJun'ichi "Nick" Nomura 
4713eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
4723eaf840eSJun'ichi "Nick" Nomura 
473074a7acaSTejun Heo 	cpu = part_stat_lock();
474074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
475074a7acaSTejun Heo 	part_stat_unlock();
4761e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw],
4771e9bb880SShaohua Li 		atomic_inc_return(&md->pending[rw]));
4783eaf840eSJun'ichi "Nick" Nomura }
4793eaf840eSJun'ichi "Nick" Nomura 
480d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
4813eaf840eSJun'ichi "Nick" Nomura {
4823eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
4833eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
4843eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
485c9959059STejun Heo 	int pending, cpu;
4863eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
4873eaf840eSJun'ichi "Nick" Nomura 
488074a7acaSTejun Heo 	cpu = part_stat_lock();
489074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
490074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
491074a7acaSTejun Heo 	part_stat_unlock();
4923eaf840eSJun'ichi "Nick" Nomura 
493af7e466aSMikulas Patocka 	/*
494af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
495d87f4c14STejun Heo 	 * a flush.
496af7e466aSMikulas Patocka 	 */
4971e9bb880SShaohua Li 	pending = atomic_dec_return(&md->pending[rw]);
4981e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
499316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
5003eaf840eSJun'ichi "Nick" Nomura 
501d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
502d221d2e7SMikulas Patocka 	if (!pending)
503d221d2e7SMikulas Patocka 		wake_up(&md->wait);
5043eaf840eSJun'ichi "Nick" Nomura }
5053eaf840eSJun'ichi "Nick" Nomura 
5061da177e4SLinus Torvalds /*
5071da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
5081da177e4SLinus Torvalds  */
50992c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
5101da177e4SLinus Torvalds {
51105447420SKiyoshi Ueda 	unsigned long flags;
5121da177e4SLinus Torvalds 
51305447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
5141da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
51505447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
51692c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
5171da177e4SLinus Torvalds }
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds /*
5201da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
5211da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
5221da177e4SLinus Torvalds  * dm_table_put() when finished.
5231da177e4SLinus Torvalds  */
5247c666411SAlasdair G Kergon struct dm_table *dm_get_live_table(struct mapped_device *md)
5251da177e4SLinus Torvalds {
5261da177e4SLinus Torvalds 	struct dm_table *t;
527523d9297SKiyoshi Ueda 	unsigned long flags;
5281da177e4SLinus Torvalds 
529523d9297SKiyoshi Ueda 	read_lock_irqsave(&md->map_lock, flags);
5301da177e4SLinus Torvalds 	t = md->map;
5311da177e4SLinus Torvalds 	if (t)
5321da177e4SLinus Torvalds 		dm_table_get(t);
533523d9297SKiyoshi Ueda 	read_unlock_irqrestore(&md->map_lock, flags);
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 	return t;
5361da177e4SLinus Torvalds }
5371da177e4SLinus Torvalds 
5383ac51e74SDarrick J. Wong /*
5393ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
5403ac51e74SDarrick J. Wong  */
5413ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
5423ac51e74SDarrick J. Wong {
5433ac51e74SDarrick J. Wong 	*geo = md->geometry;
5443ac51e74SDarrick J. Wong 
5453ac51e74SDarrick J. Wong 	return 0;
5463ac51e74SDarrick J. Wong }
5473ac51e74SDarrick J. Wong 
5483ac51e74SDarrick J. Wong /*
5493ac51e74SDarrick J. Wong  * Set the geometry of a device.
5503ac51e74SDarrick J. Wong  */
5513ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
5523ac51e74SDarrick J. Wong {
5533ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
5543ac51e74SDarrick J. Wong 
5553ac51e74SDarrick J. Wong 	if (geo->start > sz) {
5563ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
5573ac51e74SDarrick J. Wong 		return -EINVAL;
5583ac51e74SDarrick J. Wong 	}
5593ac51e74SDarrick J. Wong 
5603ac51e74SDarrick J. Wong 	md->geometry = *geo;
5613ac51e74SDarrick J. Wong 
5623ac51e74SDarrick J. Wong 	return 0;
5633ac51e74SDarrick J. Wong }
5643ac51e74SDarrick J. Wong 
5651da177e4SLinus Torvalds /*-----------------------------------------------------------------
5661da177e4SLinus Torvalds  * CRUD START:
5671da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
5681da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
5691da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
5701da177e4SLinus Torvalds  *   interests of getting something for people to use I give
5711da177e4SLinus Torvalds  *   you this clearly demarcated crap.
5721da177e4SLinus Torvalds  *---------------------------------------------------------------*/
5731da177e4SLinus Torvalds 
5742e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
5752e93ccc1SKiyoshi Ueda {
5762e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
5772e93ccc1SKiyoshi Ueda }
5782e93ccc1SKiyoshi Ueda 
5791da177e4SLinus Torvalds /*
5801da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
5811da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
5821da177e4SLinus Torvalds  */
583858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
5841da177e4SLinus Torvalds {
5852e93ccc1SKiyoshi Ueda 	unsigned long flags;
586b35f8caaSMilan Broz 	int io_error;
587b35f8caaSMilan Broz 	struct bio *bio;
588b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
5892e93ccc1SKiyoshi Ueda 
5902e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
591f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
592f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
593f88fb981SKiyoshi Ueda 		if (!(io->error > 0 && __noflush_suspending(md)))
5941da177e4SLinus Torvalds 			io->error = error;
595f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
596f88fb981SKiyoshi Ueda 	}
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
5992e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
6002e93ccc1SKiyoshi Ueda 			/*
6012e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
6022e93ccc1SKiyoshi Ueda 			 */
603022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
6046a8736d1STejun Heo 			if (__noflush_suspending(md))
6056a8736d1STejun Heo 				bio_list_add_head(&md->deferred, io->bio);
6066a8736d1STejun Heo 			else
6072e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
6082e93ccc1SKiyoshi Ueda 				io->error = -EIO;
609022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
6102e93ccc1SKiyoshi Ueda 		}
6112e93ccc1SKiyoshi Ueda 
612b35f8caaSMilan Broz 		io_error = io->error;
613b35f8caaSMilan Broz 		bio = io->bio;
614af7e466aSMikulas Patocka 		end_io_acct(io);
615a97f925aSMikulas Patocka 		free_io(md, io);
6161da177e4SLinus Torvalds 
6176a8736d1STejun Heo 		if (io_error == DM_ENDIO_REQUEUE)
6186a8736d1STejun Heo 			return;
6196a8736d1STejun Heo 
620b372d360SMike Snitzer 		if ((bio->bi_rw & REQ_FLUSH) && bio->bi_size) {
6211da177e4SLinus Torvalds 			/*
6226a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
6236a8736d1STejun Heo 			 * without REQ_FLUSH.
6241da177e4SLinus Torvalds 			 */
6256a8736d1STejun Heo 			bio->bi_rw &= ~REQ_FLUSH;
6266a8736d1STejun Heo 			queue_io(md, bio);
6275f3ea37cSArnaldo Carvalho de Melo 		} else {
628b372d360SMike Snitzer 			/* done with normal IO or empty flush */
629b35f8caaSMilan Broz 			bio_endio(bio, io_error);
6302e93ccc1SKiyoshi Ueda 		}
6311da177e4SLinus Torvalds 	}
632af7e466aSMikulas Patocka }
6331da177e4SLinus Torvalds 
6346712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error)
6351da177e4SLinus Torvalds {
6361da177e4SLinus Torvalds 	int r = 0;
637028867acSAlasdair G Kergon 	struct dm_target_io *tio = bio->bi_private;
638b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
6399faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
6401da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	if (!bio_flagged(bio, BIO_UPTODATE) && !error)
6431da177e4SLinus Torvalds 		error = -EIO;
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	if (endio) {
6467de3ee57SMikulas Patocka 		r = endio(tio->ti, bio, error);
6472e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
6482e93ccc1SKiyoshi Ueda 			/*
6492e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
6502e93ccc1SKiyoshi Ueda 			 * in dec_pending().
6512e93ccc1SKiyoshi Ueda 			 */
6521da177e4SLinus Torvalds 			error = r;
65345cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
65445cbcd79SKiyoshi Ueda 			/* The target will handle the io */
6556712ecf8SNeilBrown 			return;
65645cbcd79SKiyoshi Ueda 		else if (r) {
65745cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
65845cbcd79SKiyoshi Ueda 			BUG();
65945cbcd79SKiyoshi Ueda 		}
6601da177e4SLinus Torvalds 	}
6611da177e4SLinus Torvalds 
6629faf400fSStefan Bader 	free_tio(md, tio);
663b35f8caaSMilan Broz 	dec_pending(io, error);
6641da177e4SLinus Torvalds }
6651da177e4SLinus Torvalds 
666cec47e3dSKiyoshi Ueda /*
667cec47e3dSKiyoshi Ueda  * Partial completion handling for request-based dm
668cec47e3dSKiyoshi Ueda  */
669cec47e3dSKiyoshi Ueda static void end_clone_bio(struct bio *clone, int error)
670cec47e3dSKiyoshi Ueda {
671cec47e3dSKiyoshi Ueda 	struct dm_rq_clone_bio_info *info = clone->bi_private;
672cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = info->tio;
673cec47e3dSKiyoshi Ueda 	struct bio *bio = info->orig;
674cec47e3dSKiyoshi Ueda 	unsigned int nr_bytes = info->orig->bi_size;
675cec47e3dSKiyoshi Ueda 
676cec47e3dSKiyoshi Ueda 	bio_put(clone);
677cec47e3dSKiyoshi Ueda 
678cec47e3dSKiyoshi Ueda 	if (tio->error)
679cec47e3dSKiyoshi Ueda 		/*
680cec47e3dSKiyoshi Ueda 		 * An error has already been detected on the request.
681cec47e3dSKiyoshi Ueda 		 * Once error occurred, just let clone->end_io() handle
682cec47e3dSKiyoshi Ueda 		 * the remainder.
683cec47e3dSKiyoshi Ueda 		 */
684cec47e3dSKiyoshi Ueda 		return;
685cec47e3dSKiyoshi Ueda 	else if (error) {
686cec47e3dSKiyoshi Ueda 		/*
687cec47e3dSKiyoshi Ueda 		 * Don't notice the error to the upper layer yet.
688cec47e3dSKiyoshi Ueda 		 * The error handling decision is made by the target driver,
689cec47e3dSKiyoshi Ueda 		 * when the request is completed.
690cec47e3dSKiyoshi Ueda 		 */
691cec47e3dSKiyoshi Ueda 		tio->error = error;
692cec47e3dSKiyoshi Ueda 		return;
693cec47e3dSKiyoshi Ueda 	}
694cec47e3dSKiyoshi Ueda 
695cec47e3dSKiyoshi Ueda 	/*
696cec47e3dSKiyoshi Ueda 	 * I/O for the bio successfully completed.
697cec47e3dSKiyoshi Ueda 	 * Notice the data completion to the upper layer.
698cec47e3dSKiyoshi Ueda 	 */
699cec47e3dSKiyoshi Ueda 
700cec47e3dSKiyoshi Ueda 	/*
701cec47e3dSKiyoshi Ueda 	 * bios are processed from the head of the list.
702cec47e3dSKiyoshi Ueda 	 * So the completing bio should always be rq->bio.
703cec47e3dSKiyoshi Ueda 	 * If it's not, something wrong is happening.
704cec47e3dSKiyoshi Ueda 	 */
705cec47e3dSKiyoshi Ueda 	if (tio->orig->bio != bio)
706cec47e3dSKiyoshi Ueda 		DMERR("bio completion is going in the middle of the request");
707cec47e3dSKiyoshi Ueda 
708cec47e3dSKiyoshi Ueda 	/*
709cec47e3dSKiyoshi Ueda 	 * Update the original request.
710cec47e3dSKiyoshi Ueda 	 * Do not use blk_end_request() here, because it may complete
711cec47e3dSKiyoshi Ueda 	 * the original request before the clone, and break the ordering.
712cec47e3dSKiyoshi Ueda 	 */
713cec47e3dSKiyoshi Ueda 	blk_update_request(tio->orig, 0, nr_bytes);
714cec47e3dSKiyoshi Ueda }
715cec47e3dSKiyoshi Ueda 
716cec47e3dSKiyoshi Ueda /*
717cec47e3dSKiyoshi Ueda  * Don't touch any member of the md after calling this function because
718cec47e3dSKiyoshi Ueda  * the md may be freed in dm_put() at the end of this function.
719cec47e3dSKiyoshi Ueda  * Or do dm_get() before calling this function and dm_put() later.
720cec47e3dSKiyoshi Ueda  */
721b4324feeSKiyoshi Ueda static void rq_completed(struct mapped_device *md, int rw, int run_queue)
722cec47e3dSKiyoshi Ueda {
723b4324feeSKiyoshi Ueda 	atomic_dec(&md->pending[rw]);
724cec47e3dSKiyoshi Ueda 
725cec47e3dSKiyoshi Ueda 	/* nudge anyone waiting on suspend queue */
726b4324feeSKiyoshi Ueda 	if (!md_in_flight(md))
727cec47e3dSKiyoshi Ueda 		wake_up(&md->wait);
728cec47e3dSKiyoshi Ueda 
729a8c32a5cSJens Axboe 	/*
730a8c32a5cSJens Axboe 	 * Run this off this callpath, as drivers could invoke end_io while
731a8c32a5cSJens Axboe 	 * inside their request_fn (and holding the queue lock). Calling
732a8c32a5cSJens Axboe 	 * back into ->request_fn() could deadlock attempting to grab the
733a8c32a5cSJens Axboe 	 * queue lock again.
734a8c32a5cSJens Axboe 	 */
735cec47e3dSKiyoshi Ueda 	if (run_queue)
736a8c32a5cSJens Axboe 		blk_run_queue_async(md->queue);
737cec47e3dSKiyoshi Ueda 
738cec47e3dSKiyoshi Ueda 	/*
739cec47e3dSKiyoshi Ueda 	 * dm_put() must be at the end of this function. See the comment above
740cec47e3dSKiyoshi Ueda 	 */
741cec47e3dSKiyoshi Ueda 	dm_put(md);
742cec47e3dSKiyoshi Ueda }
743cec47e3dSKiyoshi Ueda 
744a77e28c7SKiyoshi Ueda static void free_rq_clone(struct request *clone)
745a77e28c7SKiyoshi Ueda {
746a77e28c7SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
747a77e28c7SKiyoshi Ueda 
748a77e28c7SKiyoshi Ueda 	blk_rq_unprep_clone(clone);
749a77e28c7SKiyoshi Ueda 	free_rq_tio(tio);
750a77e28c7SKiyoshi Ueda }
751a77e28c7SKiyoshi Ueda 
752980691e5SKiyoshi Ueda /*
753980691e5SKiyoshi Ueda  * Complete the clone and the original request.
754980691e5SKiyoshi Ueda  * Must be called without queue lock.
755980691e5SKiyoshi Ueda  */
756980691e5SKiyoshi Ueda static void dm_end_request(struct request *clone, int error)
757980691e5SKiyoshi Ueda {
758980691e5SKiyoshi Ueda 	int rw = rq_data_dir(clone);
759980691e5SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
760980691e5SKiyoshi Ueda 	struct mapped_device *md = tio->md;
761980691e5SKiyoshi Ueda 	struct request *rq = tio->orig;
762980691e5SKiyoshi Ueda 
76329e4013dSTejun Heo 	if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
764980691e5SKiyoshi Ueda 		rq->errors = clone->errors;
765980691e5SKiyoshi Ueda 		rq->resid_len = clone->resid_len;
766980691e5SKiyoshi Ueda 
767980691e5SKiyoshi Ueda 		if (rq->sense)
768980691e5SKiyoshi Ueda 			/*
769980691e5SKiyoshi Ueda 			 * We are using the sense buffer of the original
770980691e5SKiyoshi Ueda 			 * request.
771980691e5SKiyoshi Ueda 			 * So setting the length of the sense data is enough.
772980691e5SKiyoshi Ueda 			 */
773980691e5SKiyoshi Ueda 			rq->sense_len = clone->sense_len;
774980691e5SKiyoshi Ueda 	}
775980691e5SKiyoshi Ueda 
776980691e5SKiyoshi Ueda 	free_rq_clone(clone);
777980691e5SKiyoshi Ueda 	blk_end_request_all(rq, error);
77829e4013dSTejun Heo 	rq_completed(md, rw, true);
779980691e5SKiyoshi Ueda }
780980691e5SKiyoshi Ueda 
781cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq)
782cec47e3dSKiyoshi Ueda {
783cec47e3dSKiyoshi Ueda 	struct request *clone = rq->special;
784cec47e3dSKiyoshi Ueda 
785cec47e3dSKiyoshi Ueda 	rq->special = NULL;
786cec47e3dSKiyoshi Ueda 	rq->cmd_flags &= ~REQ_DONTPREP;
787cec47e3dSKiyoshi Ueda 
788a77e28c7SKiyoshi Ueda 	free_rq_clone(clone);
789cec47e3dSKiyoshi Ueda }
790cec47e3dSKiyoshi Ueda 
791cec47e3dSKiyoshi Ueda /*
792cec47e3dSKiyoshi Ueda  * Requeue the original request of a clone.
793cec47e3dSKiyoshi Ueda  */
794cec47e3dSKiyoshi Ueda void dm_requeue_unmapped_request(struct request *clone)
795cec47e3dSKiyoshi Ueda {
796b4324feeSKiyoshi Ueda 	int rw = rq_data_dir(clone);
797cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
798cec47e3dSKiyoshi Ueda 	struct mapped_device *md = tio->md;
799cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
800cec47e3dSKiyoshi Ueda 	struct request_queue *q = rq->q;
801cec47e3dSKiyoshi Ueda 	unsigned long flags;
802cec47e3dSKiyoshi Ueda 
803cec47e3dSKiyoshi Ueda 	dm_unprep_request(rq);
804cec47e3dSKiyoshi Ueda 
805cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
806cec47e3dSKiyoshi Ueda 	blk_requeue_request(q, rq);
807cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
808cec47e3dSKiyoshi Ueda 
809b4324feeSKiyoshi Ueda 	rq_completed(md, rw, 0);
810cec47e3dSKiyoshi Ueda }
811cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
812cec47e3dSKiyoshi Ueda 
813cec47e3dSKiyoshi Ueda static void __stop_queue(struct request_queue *q)
814cec47e3dSKiyoshi Ueda {
815cec47e3dSKiyoshi Ueda 	blk_stop_queue(q);
816cec47e3dSKiyoshi Ueda }
817cec47e3dSKiyoshi Ueda 
818cec47e3dSKiyoshi Ueda static void stop_queue(struct request_queue *q)
819cec47e3dSKiyoshi Ueda {
820cec47e3dSKiyoshi Ueda 	unsigned long flags;
821cec47e3dSKiyoshi Ueda 
822cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
823cec47e3dSKiyoshi Ueda 	__stop_queue(q);
824cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
825cec47e3dSKiyoshi Ueda }
826cec47e3dSKiyoshi Ueda 
827cec47e3dSKiyoshi Ueda static void __start_queue(struct request_queue *q)
828cec47e3dSKiyoshi Ueda {
829cec47e3dSKiyoshi Ueda 	if (blk_queue_stopped(q))
830cec47e3dSKiyoshi Ueda 		blk_start_queue(q);
831cec47e3dSKiyoshi Ueda }
832cec47e3dSKiyoshi Ueda 
833cec47e3dSKiyoshi Ueda static void start_queue(struct request_queue *q)
834cec47e3dSKiyoshi Ueda {
835cec47e3dSKiyoshi Ueda 	unsigned long flags;
836cec47e3dSKiyoshi Ueda 
837cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
838cec47e3dSKiyoshi Ueda 	__start_queue(q);
839cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
840cec47e3dSKiyoshi Ueda }
841cec47e3dSKiyoshi Ueda 
84211a68244SKiyoshi Ueda static void dm_done(struct request *clone, int error, bool mapped)
84311a68244SKiyoshi Ueda {
84411a68244SKiyoshi Ueda 	int r = error;
84511a68244SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
846ba1cbad9SMike Snitzer 	dm_request_endio_fn rq_end_io = NULL;
847ba1cbad9SMike Snitzer 
848ba1cbad9SMike Snitzer 	if (tio->ti) {
849ba1cbad9SMike Snitzer 		rq_end_io = tio->ti->type->rq_end_io;
85011a68244SKiyoshi Ueda 
85111a68244SKiyoshi Ueda 		if (mapped && rq_end_io)
85211a68244SKiyoshi Ueda 			r = rq_end_io(tio->ti, clone, error, &tio->info);
853ba1cbad9SMike Snitzer 	}
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);
953542f9038SMike Snitzer 	sector_t offset, max_len;
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	/*
9561da177e4SLinus Torvalds 	 * Does the target need to split even further?
9571da177e4SLinus Torvalds 	 */
958542f9038SMike Snitzer 	if (ti->max_io_len) {
959542f9038SMike Snitzer 		offset = dm_target_offset(ti, sector);
960542f9038SMike Snitzer 		if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
961542f9038SMike Snitzer 			max_len = sector_div(offset, ti->max_io_len);
962542f9038SMike Snitzer 		else
963542f9038SMike Snitzer 			max_len = offset & (ti->max_io_len - 1);
964542f9038SMike Snitzer 		max_len = ti->max_io_len - max_len;
965542f9038SMike Snitzer 
966542f9038SMike Snitzer 		if (len > max_len)
967542f9038SMike Snitzer 			len = max_len;
9681da177e4SLinus Torvalds 	}
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	return len;
9711da177e4SLinus Torvalds }
9721da177e4SLinus Torvalds 
973542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
974542f9038SMike Snitzer {
975542f9038SMike Snitzer 	if (len > UINT_MAX) {
976542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
977542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
978542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
979542f9038SMike Snitzer 		return -EINVAL;
980542f9038SMike Snitzer 	}
981542f9038SMike Snitzer 
982542f9038SMike Snitzer 	ti->max_io_len = (uint32_t) len;
983542f9038SMike Snitzer 
984542f9038SMike Snitzer 	return 0;
985542f9038SMike Snitzer }
986542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
987542f9038SMike Snitzer 
988bd2a49b8SAlasdair G Kergon static void __map_bio(struct dm_target_io *tio)
9891da177e4SLinus Torvalds {
9901da177e4SLinus Torvalds 	int r;
9912056a782SJens Axboe 	sector_t sector;
9929faf400fSStefan Bader 	struct mapped_device *md;
993dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
994bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
9971da177e4SLinus Torvalds 	clone->bi_private = tio;
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 	/*
10001da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
10011da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
10021da177e4SLinus Torvalds 	 * this io.
10031da177e4SLinus Torvalds 	 */
10041da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
10052056a782SJens Axboe 	sector = clone->bi_sector;
10067de3ee57SMikulas Patocka 	r = ti->type->map(ti, clone);
100745cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
10081da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
10092056a782SJens Axboe 
1010d07335e5SMike Snitzer 		trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
101122a7c31aSAlan D. Brunelle 				      tio->io->bio->bi_bdev->bd_dev, sector);
10122056a782SJens Axboe 
10131da177e4SLinus Torvalds 		generic_make_request(clone);
10142e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
10152e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
10169faf400fSStefan Bader 		md = tio->io->md;
10179faf400fSStefan Bader 		dec_pending(tio->io, r);
10189faf400fSStefan Bader 		free_tio(md, tio);
101945cbcd79SKiyoshi Ueda 	} else if (r) {
102045cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
102145cbcd79SKiyoshi Ueda 		BUG();
10221da177e4SLinus Torvalds 	}
10231da177e4SLinus Torvalds }
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds struct clone_info {
10261da177e4SLinus Torvalds 	struct mapped_device *md;
10271da177e4SLinus Torvalds 	struct dm_table *map;
10281da177e4SLinus Torvalds 	struct bio *bio;
10291da177e4SLinus Torvalds 	struct dm_io *io;
10301da177e4SLinus Torvalds 	sector_t sector;
10311da177e4SLinus Torvalds 	sector_t sector_count;
10321da177e4SLinus Torvalds 	unsigned short idx;
10331da177e4SLinus Torvalds };
10341da177e4SLinus Torvalds 
1035bd2a49b8SAlasdair G Kergon static void bio_setup_sector(struct bio *bio, sector_t sector, sector_t len)
1036bd2a49b8SAlasdair G Kergon {
1037bd2a49b8SAlasdair G Kergon 	bio->bi_sector = sector;
1038bd2a49b8SAlasdair G Kergon 	bio->bi_size = to_bytes(len);
1039bd2a49b8SAlasdair G Kergon }
1040bd2a49b8SAlasdair G Kergon 
1041bd2a49b8SAlasdair G Kergon static void bio_setup_bv(struct bio *bio, unsigned short idx, unsigned short bv_count)
1042bd2a49b8SAlasdair G Kergon {
1043bd2a49b8SAlasdair G Kergon 	bio->bi_idx = idx;
1044bd2a49b8SAlasdair G Kergon 	bio->bi_vcnt = idx + bv_count;
1045bd2a49b8SAlasdair G Kergon 	bio->bi_flags &= ~(1 << BIO_SEG_VALID);
1046bd2a49b8SAlasdair G Kergon }
1047bd2a49b8SAlasdair G Kergon 
1048bd2a49b8SAlasdair G Kergon static void clone_bio_integrity(struct bio *bio, struct bio *clone,
1049bd2a49b8SAlasdair G Kergon 				unsigned short idx, unsigned len, unsigned offset,
1050bd2a49b8SAlasdair G Kergon 				unsigned trim)
1051bd2a49b8SAlasdair G Kergon {
1052bd2a49b8SAlasdair G Kergon 	if (!bio_integrity(bio))
1053bd2a49b8SAlasdair G Kergon 		return;
1054bd2a49b8SAlasdair G Kergon 
1055bd2a49b8SAlasdair G Kergon 	bio_integrity_clone(clone, bio, GFP_NOIO);
1056bd2a49b8SAlasdair G Kergon 
1057bd2a49b8SAlasdair G Kergon 	if (trim)
1058bd2a49b8SAlasdair G Kergon 		bio_integrity_trim(clone, bio_sector_offset(bio, idx, offset), len);
1059bd2a49b8SAlasdair G Kergon }
1060bd2a49b8SAlasdair G Kergon 
10611da177e4SLinus Torvalds /*
1062d87f4c14STejun Heo  * Creates a little bio that just does part of a bvec.
10631da177e4SLinus Torvalds  */
1064dba14160SMikulas Patocka static void split_bvec(struct dm_target_io *tio, struct bio *bio,
1065dba14160SMikulas Patocka 		       sector_t sector, unsigned short idx, unsigned int offset,
1066bd2a49b8SAlasdair G Kergon 		       unsigned int len)
10671da177e4SLinus Torvalds {
1068dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
10691da177e4SLinus Torvalds 	struct bio_vec *bv = bio->bi_io_vec + idx;
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 	*clone->bi_io_vec = *bv;
10721da177e4SLinus Torvalds 
1073bd2a49b8SAlasdair G Kergon 	bio_setup_sector(clone, sector, len);
1074bd2a49b8SAlasdair G Kergon 
10751da177e4SLinus Torvalds 	clone->bi_bdev = bio->bi_bdev;
1076d87f4c14STejun Heo 	clone->bi_rw = bio->bi_rw;
10771da177e4SLinus Torvalds 	clone->bi_vcnt = 1;
10781da177e4SLinus Torvalds 	clone->bi_io_vec->bv_offset = offset;
10791da177e4SLinus Torvalds 	clone->bi_io_vec->bv_len = clone->bi_size;
1080f3e1d26eSMartin K. Petersen 	clone->bi_flags |= 1 << BIO_CLONED;
10811da177e4SLinus Torvalds 
1082bd2a49b8SAlasdair G Kergon 	clone_bio_integrity(bio, clone, idx, len, offset, 1);
10831da177e4SLinus Torvalds }
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds /*
10861da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
10871da177e4SLinus Torvalds  */
1088dba14160SMikulas Patocka static void clone_bio(struct dm_target_io *tio, struct bio *bio,
1089dba14160SMikulas Patocka 		      sector_t sector, unsigned short idx,
1090bd2a49b8SAlasdair G Kergon 		      unsigned short bv_count, unsigned int len)
10911da177e4SLinus Torvalds {
1092dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
1093bd2a49b8SAlasdair G Kergon 	unsigned trim = 0;
10941da177e4SLinus Torvalds 
10959faf400fSStefan Bader 	__bio_clone(clone, bio);
1096bd2a49b8SAlasdair G Kergon 	bio_setup_sector(clone, sector, len);
1097bd2a49b8SAlasdair G Kergon 	bio_setup_bv(clone, idx, bv_count);
10989c47008dSMartin K. Petersen 
10999c47008dSMartin K. Petersen 	if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1100bd2a49b8SAlasdair G Kergon 		trim = 1;
1101bd2a49b8SAlasdair G Kergon 	clone_bio_integrity(bio, clone, idx, len, 0, trim);
11021da177e4SLinus Torvalds }
11031da177e4SLinus Torvalds 
11049015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci,
1105bd2a49b8SAlasdair G Kergon 				      struct dm_target *ti, int nr_iovecs,
1106*55a62eefSAlasdair G Kergon 				      unsigned target_bio_nr)
1107f9ab94ceSMikulas Patocka {
1108dba14160SMikulas Patocka 	struct dm_target_io *tio;
1109dba14160SMikulas Patocka 	struct bio *clone;
1110dba14160SMikulas Patocka 
1111dba14160SMikulas Patocka 	clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, ci->md->bs);
1112dba14160SMikulas Patocka 	tio = container_of(clone, struct dm_target_io, clone);
1113f9ab94ceSMikulas Patocka 
1114f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1115f9ab94ceSMikulas Patocka 	tio->ti = ti;
1116f9ab94ceSMikulas Patocka 	memset(&tio->info, 0, sizeof(tio->info));
1117*55a62eefSAlasdair G Kergon 	tio->target_bio_nr = target_bio_nr;
11189015df24SAlasdair G Kergon 
11199015df24SAlasdair G Kergon 	return tio;
11209015df24SAlasdair G Kergon }
11219015df24SAlasdair G Kergon 
112206a426ceSMike Snitzer static void __issue_target_request(struct clone_info *ci, struct dm_target *ti,
1123*55a62eefSAlasdair G Kergon 				   unsigned target_bio_nr, sector_t len)
11249015df24SAlasdair G Kergon {
1125*55a62eefSAlasdair G Kergon 	struct dm_target_io *tio = alloc_tio(ci, ti, ci->bio->bi_max_vecs, target_bio_nr);
1126dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
11279015df24SAlasdair G Kergon 
112806a426ceSMike Snitzer 	/*
112906a426ceSMike Snitzer 	 * Discard requests require the bio's inline iovecs be initialized.
113006a426ceSMike Snitzer 	 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
113106a426ceSMike Snitzer 	 * and discard, so no need for concern about wasted bvec allocations.
113206a426ceSMike Snitzer 	 */
1133dba14160SMikulas Patocka 	 __bio_clone(clone, ci->bio);
1134bd2a49b8SAlasdair G Kergon 	if (len)
1135bd2a49b8SAlasdair G Kergon 		bio_setup_sector(clone, ci->sector, len);
1136f9ab94ceSMikulas Patocka 
1137bd2a49b8SAlasdair G Kergon 	__map_bio(tio);
1138f9ab94ceSMikulas Patocka }
1139f9ab94ceSMikulas Patocka 
1140*55a62eefSAlasdair G Kergon static void __issue_target_bios(struct clone_info *ci, struct dm_target *ti,
1141*55a62eefSAlasdair G Kergon 				unsigned num_bios, sector_t len)
114206a426ceSMike Snitzer {
1143*55a62eefSAlasdair G Kergon 	unsigned target_bio_nr;
114406a426ceSMike Snitzer 
1145*55a62eefSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
1146*55a62eefSAlasdair G Kergon 		__issue_target_request(ci, ti, target_bio_nr, len);
114706a426ceSMike Snitzer }
114806a426ceSMike Snitzer 
1149b372d360SMike Snitzer static int __clone_and_map_empty_flush(struct clone_info *ci)
1150f9ab94ceSMikulas Patocka {
115106a426ceSMike Snitzer 	unsigned target_nr = 0;
1152f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1153f9ab94ceSMikulas Patocka 
1154b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1155f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
1156*55a62eefSAlasdair G Kergon 		__issue_target_bios(ci, ti, ti->num_flush_bios, 0);
1157f9ab94ceSMikulas Patocka 
1158f9ab94ceSMikulas Patocka 	return 0;
1159f9ab94ceSMikulas Patocka }
1160f9ab94ceSMikulas Patocka 
11615ae89a87SMike Snitzer /*
11625ae89a87SMike Snitzer  * Perform all io with a single clone.
11635ae89a87SMike Snitzer  */
11645ae89a87SMike Snitzer static void __clone_and_map_simple(struct clone_info *ci, struct dm_target *ti)
11655ae89a87SMike Snitzer {
1166dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
11675ae89a87SMike Snitzer 	struct dm_target_io *tio;
11685ae89a87SMike Snitzer 
1169bd2a49b8SAlasdair G Kergon 	tio = alloc_tio(ci, ti, bio->bi_max_vecs, 0);
1170dba14160SMikulas Patocka 	clone_bio(tio, bio, ci->sector, ci->idx, bio->bi_vcnt - ci->idx,
1171bd2a49b8SAlasdair G Kergon 		  ci->sector_count);
1172bd2a49b8SAlasdair G Kergon 	__map_bio(tio);
11735ae89a87SMike Snitzer 	ci->sector_count = 0;
11745ae89a87SMike Snitzer }
11755ae89a87SMike Snitzer 
1176*55a62eefSAlasdair G Kergon typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
117723508a96SMike Snitzer 
1178*55a62eefSAlasdair G Kergon static unsigned get_num_discard_bios(struct dm_target *ti)
117923508a96SMike Snitzer {
1180*55a62eefSAlasdair G Kergon 	return ti->num_discard_bios;
118123508a96SMike Snitzer }
118223508a96SMike Snitzer 
1183*55a62eefSAlasdair G Kergon static unsigned get_num_write_same_bios(struct dm_target *ti)
118423508a96SMike Snitzer {
1185*55a62eefSAlasdair G Kergon 	return ti->num_write_same_bios;
118623508a96SMike Snitzer }
118723508a96SMike Snitzer 
118823508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti);
118923508a96SMike Snitzer 
119023508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti)
119123508a96SMike Snitzer {
1192*55a62eefSAlasdair G Kergon 	return ti->split_discard_bios;
119323508a96SMike Snitzer }
119423508a96SMike Snitzer 
119523508a96SMike Snitzer static int __clone_and_map_changing_extent_only(struct clone_info *ci,
1196*55a62eefSAlasdair G Kergon 						get_num_bios_fn get_num_bios,
119723508a96SMike Snitzer 						is_split_required_fn is_split_required)
11985ae89a87SMike Snitzer {
11995ae89a87SMike Snitzer 	struct dm_target *ti;
1200a79245b3SMike Snitzer 	sector_t len;
1201*55a62eefSAlasdair G Kergon 	unsigned num_bios;
12025ae89a87SMike Snitzer 
1203a79245b3SMike Snitzer 	do {
12045ae89a87SMike Snitzer 		ti = dm_table_find_target(ci->map, ci->sector);
12055ae89a87SMike Snitzer 		if (!dm_target_is_valid(ti))
12065ae89a87SMike Snitzer 			return -EIO;
12075ae89a87SMike Snitzer 
12085ae89a87SMike Snitzer 		/*
120923508a96SMike Snitzer 		 * Even though the device advertised support for this type of
121023508a96SMike Snitzer 		 * request, that does not mean every target supports it, and
1211936688d7SMike Snitzer 		 * reconfiguration might also have changed that since the
12125ae89a87SMike Snitzer 		 * check was performed.
12135ae89a87SMike Snitzer 		 */
1214*55a62eefSAlasdair G Kergon 		num_bios = get_num_bios ? get_num_bios(ti) : 0;
1215*55a62eefSAlasdair G Kergon 		if (!num_bios)
12165ae89a87SMike Snitzer 			return -EOPNOTSUPP;
12175ae89a87SMike Snitzer 
121823508a96SMike Snitzer 		if (is_split_required && !is_split_required(ti))
1219a79245b3SMike Snitzer 			len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
12207acf0277SMikulas Patocka 		else
12217acf0277SMikulas Patocka 			len = min(ci->sector_count, max_io_len(ci->sector, ti));
12225ae89a87SMike Snitzer 
1223*55a62eefSAlasdair G Kergon 		__issue_target_bios(ci, ti, num_bios, len);
12245ae89a87SMike Snitzer 
1225a79245b3SMike Snitzer 		ci->sector += len;
1226a79245b3SMike Snitzer 	} while (ci->sector_count -= len);
12275ae89a87SMike Snitzer 
12285ae89a87SMike Snitzer 	return 0;
12295ae89a87SMike Snitzer }
12305ae89a87SMike Snitzer 
123123508a96SMike Snitzer static int __clone_and_map_discard(struct clone_info *ci)
123223508a96SMike Snitzer {
1233*55a62eefSAlasdair G Kergon 	return __clone_and_map_changing_extent_only(ci, get_num_discard_bios,
123423508a96SMike Snitzer 						    is_split_required_for_discard);
123523508a96SMike Snitzer }
123623508a96SMike Snitzer 
123723508a96SMike Snitzer static int __clone_and_map_write_same(struct clone_info *ci)
123823508a96SMike Snitzer {
1239*55a62eefSAlasdair G Kergon 	return __clone_and_map_changing_extent_only(ci, get_num_write_same_bios, NULL);
124023508a96SMike Snitzer }
124123508a96SMike Snitzer 
1242512875bdSJun'ichi Nomura static int __clone_and_map(struct clone_info *ci)
12431da177e4SLinus Torvalds {
1244dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
1245512875bdSJun'ichi Nomura 	struct dm_target *ti;
1246512875bdSJun'ichi Nomura 	sector_t len = 0, max;
1247028867acSAlasdair G Kergon 	struct dm_target_io *tio;
12481da177e4SLinus Torvalds 
12495ae89a87SMike Snitzer 	if (unlikely(bio->bi_rw & REQ_DISCARD))
12505ae89a87SMike Snitzer 		return __clone_and_map_discard(ci);
125123508a96SMike Snitzer 	else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
125223508a96SMike Snitzer 		return __clone_and_map_write_same(ci);
12535ae89a87SMike Snitzer 
1254512875bdSJun'ichi Nomura 	ti = dm_table_find_target(ci->map, ci->sector);
1255512875bdSJun'ichi Nomura 	if (!dm_target_is_valid(ti))
1256512875bdSJun'ichi Nomura 		return -EIO;
1257512875bdSJun'ichi Nomura 
125856a67df7SMike Snitzer 	max = max_io_len(ci->sector, ti);
1259512875bdSJun'ichi Nomura 
12601da177e4SLinus Torvalds 	if (ci->sector_count <= max) {
12611da177e4SLinus Torvalds 		/*
12621da177e4SLinus Torvalds 		 * Optimise for the simple case where we can do all of
12631da177e4SLinus Torvalds 		 * the remaining io with a single clone.
12641da177e4SLinus Torvalds 		 */
12655ae89a87SMike Snitzer 		__clone_and_map_simple(ci, ti);
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 	} else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
12681da177e4SLinus Torvalds 		/*
12691da177e4SLinus Torvalds 		 * There are some bvecs that don't span targets.
12701da177e4SLinus Torvalds 		 * Do as many of these as possible.
12711da177e4SLinus Torvalds 		 */
12721da177e4SLinus Torvalds 		int i;
12731da177e4SLinus Torvalds 		sector_t remaining = max;
12741da177e4SLinus Torvalds 		sector_t bv_len;
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds 		for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
12771da177e4SLinus Torvalds 			bv_len = to_sector(bio->bi_io_vec[i].bv_len);
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 			if (bv_len > remaining)
12801da177e4SLinus Torvalds 				break;
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds 			remaining -= bv_len;
12831da177e4SLinus Torvalds 			len += bv_len;
12841da177e4SLinus Torvalds 		}
12851da177e4SLinus Torvalds 
1286bd2a49b8SAlasdair G Kergon 		tio = alloc_tio(ci, ti, bio->bi_max_vecs, 0);
1287bd2a49b8SAlasdair G Kergon 		clone_bio(tio, bio, ci->sector, ci->idx, i - ci->idx, len);
1288bd2a49b8SAlasdair G Kergon 		__map_bio(tio);
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 		ci->sector += len;
12911da177e4SLinus Torvalds 		ci->sector_count -= len;
12921da177e4SLinus Torvalds 		ci->idx = i;
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 	} else {
12951da177e4SLinus Torvalds 		/*
1296d2044a94SAlasdair G Kergon 		 * Handle a bvec that must be split between two or more targets.
12971da177e4SLinus Torvalds 		 */
12981da177e4SLinus Torvalds 		struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1299d2044a94SAlasdair G Kergon 		sector_t remaining = to_sector(bv->bv_len);
1300d2044a94SAlasdair G Kergon 		unsigned int offset = 0;
13011da177e4SLinus Torvalds 
1302d2044a94SAlasdair G Kergon 		do {
1303d2044a94SAlasdair G Kergon 			if (offset) {
13041da177e4SLinus Torvalds 				ti = dm_table_find_target(ci->map, ci->sector);
1305512875bdSJun'ichi Nomura 				if (!dm_target_is_valid(ti))
1306512875bdSJun'ichi Nomura 					return -EIO;
1307512875bdSJun'ichi Nomura 
130856a67df7SMike Snitzer 				max = max_io_len(ci->sector, ti);
1309d2044a94SAlasdair G Kergon 			}
1310d2044a94SAlasdair G Kergon 
1311d2044a94SAlasdair G Kergon 			len = min(remaining, max);
1312d2044a94SAlasdair G Kergon 
1313bd2a49b8SAlasdair G Kergon 			tio = alloc_tio(ci, ti, 1, 0);
1314dba14160SMikulas Patocka 			split_bvec(tio, bio, ci->sector, ci->idx,
1315bd2a49b8SAlasdair G Kergon 				   bv->bv_offset + offset, len);
1316d2044a94SAlasdair G Kergon 
1317bd2a49b8SAlasdair G Kergon 			__map_bio(tio);
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds 			ci->sector += len;
13201da177e4SLinus Torvalds 			ci->sector_count -= len;
1321d2044a94SAlasdair G Kergon 			offset += to_bytes(len);
1322d2044a94SAlasdair G Kergon 		} while (remaining -= len);
1323d2044a94SAlasdair G Kergon 
13241da177e4SLinus Torvalds 		ci->idx++;
13251da177e4SLinus Torvalds 	}
1326512875bdSJun'ichi Nomura 
1327512875bdSJun'ichi Nomura 	return 0;
13281da177e4SLinus Torvalds }
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds /*
13318a53c28dSMikulas Patocka  * Split the bio into several clones and submit it to targets.
13321da177e4SLinus Torvalds  */
1333f0b9a450SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
13341da177e4SLinus Torvalds {
13351da177e4SLinus Torvalds 	struct clone_info ci;
1336512875bdSJun'ichi Nomura 	int error = 0;
13371da177e4SLinus Torvalds 
13387c666411SAlasdair G Kergon 	ci.map = dm_get_live_table(md);
1339f0b9a450SMikulas Patocka 	if (unlikely(!ci.map)) {
1340f0b9a450SMikulas Patocka 		bio_io_error(bio);
1341f0b9a450SMikulas Patocka 		return;
1342f0b9a450SMikulas Patocka 	}
1343692d0eb9SMikulas Patocka 
13441da177e4SLinus Torvalds 	ci.md = md;
13451da177e4SLinus Torvalds 	ci.io = alloc_io(md);
13461da177e4SLinus Torvalds 	ci.io->error = 0;
13471da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
13481da177e4SLinus Torvalds 	ci.io->bio = bio;
13491da177e4SLinus Torvalds 	ci.io->md = md;
1350f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
13511da177e4SLinus Torvalds 	ci.sector = bio->bi_sector;
13521da177e4SLinus Torvalds 	ci.idx = bio->bi_idx;
13531da177e4SLinus Torvalds 
13543eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1355bd2a49b8SAlasdair G Kergon 
1356b372d360SMike Snitzer 	if (bio->bi_rw & REQ_FLUSH) {
1357b372d360SMike Snitzer 		ci.bio = &ci.md->flush_bio;
1358b372d360SMike Snitzer 		ci.sector_count = 0;
1359b372d360SMike Snitzer 		error = __clone_and_map_empty_flush(&ci);
1360b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1361b372d360SMike Snitzer 	} else {
13626a8736d1STejun Heo 		ci.bio = bio;
1363f6fccb12SMilan Broz 		ci.sector_count = bio_sectors(bio);
1364512875bdSJun'ichi Nomura 		while (ci.sector_count && !error)
1365512875bdSJun'ichi Nomura 			error = __clone_and_map(&ci);
1366d87f4c14STejun Heo 	}
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	/* drop the extra reference count */
1369512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
13701da177e4SLinus Torvalds 	dm_table_put(ci.map);
13719e4e5f87SMilan Broz }
13729e4e5f87SMilan Broz /*-----------------------------------------------------------------
13731da177e4SLinus Torvalds  * CRUD END
13741da177e4SLinus Torvalds  *---------------------------------------------------------------*/
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds static int dm_merge_bvec(struct request_queue *q,
13771da177e4SLinus Torvalds 			 struct bvec_merge_data *bvm,
1378f6fccb12SMilan Broz 			 struct bio_vec *biovec)
1379f6fccb12SMilan Broz {
1380f6fccb12SMilan Broz 	struct mapped_device *md = q->queuedata;
13817c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1382f6fccb12SMilan Broz 	struct dm_target *ti;
1383f6fccb12SMilan Broz 	sector_t max_sectors;
1384f6fccb12SMilan Broz 	int max_size = 0;
1385f6fccb12SMilan Broz 
1386f6fccb12SMilan Broz 	if (unlikely(!map))
1387f6fccb12SMilan Broz 		goto out;
1388f6fccb12SMilan Broz 
1389f6fccb12SMilan Broz 	ti = dm_table_find_target(map, bvm->bi_sector);
1390f6fccb12SMilan Broz 	if (!dm_target_is_valid(ti))
1391f6fccb12SMilan Broz 		goto out_table;
1392f6fccb12SMilan Broz 
1393f6fccb12SMilan Broz 	/*
1394f6fccb12SMilan Broz 	 * Find maximum amount of I/O that won't need splitting
1395f6fccb12SMilan Broz 	 */
139656a67df7SMike Snitzer 	max_sectors = min(max_io_len(bvm->bi_sector, ti),
1397f6fccb12SMilan Broz 			  (sector_t) BIO_MAX_SECTORS);
1398f6fccb12SMilan Broz 	max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1399f6fccb12SMilan Broz 	if (max_size < 0)
1400f6fccb12SMilan Broz 		max_size = 0;
1401f6fccb12SMilan Broz 
1402f6fccb12SMilan Broz 	/*
1403f6fccb12SMilan Broz 	 * merge_bvec_fn() returns number of bytes
1404f6fccb12SMilan Broz 	 * it can accept at this offset
1405f6fccb12SMilan Broz 	 * max is precomputed maximal io size
1406f6fccb12SMilan Broz 	 */
1407f6fccb12SMilan Broz 	if (max_size && ti->type->merge)
1408f6fccb12SMilan Broz 		max_size = ti->type->merge(ti, bvm, biovec, max_size);
14098cbeb67aSMikulas Patocka 	/*
14108cbeb67aSMikulas Patocka 	 * If the target doesn't support merge method and some of the devices
14118cbeb67aSMikulas Patocka 	 * provided their merge_bvec method (we know this by looking at
14128cbeb67aSMikulas Patocka 	 * queue_max_hw_sectors), then we can't allow bios with multiple vector
14138cbeb67aSMikulas Patocka 	 * entries.  So always set max_size to 0, and the code below allows
14148cbeb67aSMikulas Patocka 	 * just one page.
14158cbeb67aSMikulas Patocka 	 */
14168cbeb67aSMikulas Patocka 	else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
14178cbeb67aSMikulas Patocka 
14188cbeb67aSMikulas Patocka 		max_size = 0;
1419f6fccb12SMilan Broz 
1420b01cd5acSMikulas Patocka out_table:
14215037108aSMikulas Patocka 	dm_table_put(map);
14225037108aSMikulas Patocka 
14235037108aSMikulas Patocka out:
1424f6fccb12SMilan Broz 	/*
1425f6fccb12SMilan Broz 	 * Always allow an entire first page
1426f6fccb12SMilan Broz 	 */
1427f6fccb12SMilan Broz 	if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1428f6fccb12SMilan Broz 		max_size = biovec->bv_len;
1429f6fccb12SMilan Broz 
1430f6fccb12SMilan Broz 	return max_size;
1431f6fccb12SMilan Broz }
1432f6fccb12SMilan Broz 
14331da177e4SLinus Torvalds /*
14341da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
14351da177e4SLinus Torvalds  * dm_merge_bvec.
14361da177e4SLinus Torvalds  */
14375a7bbad2SChristoph Hellwig static void _dm_request(struct request_queue *q, struct bio *bio)
14381da177e4SLinus Torvalds {
143912f03a49SKevin Corry 	int rw = bio_data_dir(bio);
14401da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
1441c9959059STejun Heo 	int cpu;
14421da177e4SLinus Torvalds 
14432ca3310eSAlasdair G Kergon 	down_read(&md->io_lock);
14441da177e4SLinus Torvalds 
1445074a7acaSTejun Heo 	cpu = part_stat_lock();
1446074a7acaSTejun Heo 	part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1447074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1448074a7acaSTejun Heo 	part_stat_unlock();
144912f03a49SKevin Corry 
14506a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
14516a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
14522ca3310eSAlasdair G Kergon 		up_read(&md->io_lock);
14531da177e4SLinus Torvalds 
14546a8736d1STejun Heo 		if (bio_rw(bio) != READA)
145592c63902SMikulas Patocka 			queue_io(md, bio);
14566a8736d1STejun Heo 		else
14576a8736d1STejun Heo 			bio_io_error(bio);
14585a7bbad2SChristoph Hellwig 		return;
14591da177e4SLinus Torvalds 	}
14601da177e4SLinus Torvalds 
1461f0b9a450SMikulas Patocka 	__split_and_process_bio(md, bio);
14622ca3310eSAlasdair G Kergon 	up_read(&md->io_lock);
14635a7bbad2SChristoph Hellwig 	return;
1464cec47e3dSKiyoshi Ueda }
1465cec47e3dSKiyoshi Ueda 
1466cec47e3dSKiyoshi Ueda static int dm_request_based(struct mapped_device *md)
1467cec47e3dSKiyoshi Ueda {
1468cec47e3dSKiyoshi Ueda 	return blk_queue_stackable(md->queue);
1469cec47e3dSKiyoshi Ueda }
1470cec47e3dSKiyoshi Ueda 
14715a7bbad2SChristoph Hellwig static void dm_request(struct request_queue *q, struct bio *bio)
1472cec47e3dSKiyoshi Ueda {
1473cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1474cec47e3dSKiyoshi Ueda 
1475cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
14765a7bbad2SChristoph Hellwig 		blk_queue_bio(q, bio);
14775a7bbad2SChristoph Hellwig 	else
14785a7bbad2SChristoph Hellwig 		_dm_request(q, bio);
1479cec47e3dSKiyoshi Ueda }
1480cec47e3dSKiyoshi Ueda 
1481cec47e3dSKiyoshi Ueda void dm_dispatch_request(struct request *rq)
1482cec47e3dSKiyoshi Ueda {
1483cec47e3dSKiyoshi Ueda 	int r;
1484cec47e3dSKiyoshi Ueda 
1485cec47e3dSKiyoshi Ueda 	if (blk_queue_io_stat(rq->q))
1486cec47e3dSKiyoshi Ueda 		rq->cmd_flags |= REQ_IO_STAT;
1487cec47e3dSKiyoshi Ueda 
1488cec47e3dSKiyoshi Ueda 	rq->start_time = jiffies;
1489cec47e3dSKiyoshi Ueda 	r = blk_insert_cloned_request(rq->q, rq);
1490cec47e3dSKiyoshi Ueda 	if (r)
1491cec47e3dSKiyoshi Ueda 		dm_complete_request(rq, r);
1492cec47e3dSKiyoshi Ueda }
1493cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_dispatch_request);
1494cec47e3dSKiyoshi Ueda 
1495cec47e3dSKiyoshi Ueda static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1496cec47e3dSKiyoshi Ueda 				 void *data)
1497cec47e3dSKiyoshi Ueda {
1498cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = data;
149994818742SKent Overstreet 	struct dm_rq_clone_bio_info *info =
150094818742SKent Overstreet 		container_of(bio, struct dm_rq_clone_bio_info, clone);
1501cec47e3dSKiyoshi Ueda 
1502cec47e3dSKiyoshi Ueda 	info->orig = bio_orig;
1503cec47e3dSKiyoshi Ueda 	info->tio = tio;
1504cec47e3dSKiyoshi Ueda 	bio->bi_end_io = end_clone_bio;
1505cec47e3dSKiyoshi Ueda 	bio->bi_private = info;
1506cec47e3dSKiyoshi Ueda 
1507cec47e3dSKiyoshi Ueda 	return 0;
1508cec47e3dSKiyoshi Ueda }
1509cec47e3dSKiyoshi Ueda 
1510cec47e3dSKiyoshi Ueda static int setup_clone(struct request *clone, struct request *rq,
1511cec47e3dSKiyoshi Ueda 		       struct dm_rq_target_io *tio)
1512cec47e3dSKiyoshi Ueda {
1513d0bcb878SKiyoshi Ueda 	int r;
1514cec47e3dSKiyoshi Ueda 
1515d0bcb878SKiyoshi Ueda 	r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1516d0bcb878SKiyoshi Ueda 			      dm_rq_bio_constructor, tio);
1517cec47e3dSKiyoshi Ueda 	if (r)
1518cec47e3dSKiyoshi Ueda 		return r;
1519cec47e3dSKiyoshi Ueda 
1520cec47e3dSKiyoshi Ueda 	clone->cmd = rq->cmd;
1521cec47e3dSKiyoshi Ueda 	clone->cmd_len = rq->cmd_len;
1522cec47e3dSKiyoshi Ueda 	clone->sense = rq->sense;
1523cec47e3dSKiyoshi Ueda 	clone->buffer = rq->buffer;
1524cec47e3dSKiyoshi Ueda 	clone->end_io = end_clone_request;
1525cec47e3dSKiyoshi Ueda 	clone->end_io_data = tio;
1526cec47e3dSKiyoshi Ueda 
1527cec47e3dSKiyoshi Ueda 	return 0;
1528cec47e3dSKiyoshi Ueda }
1529cec47e3dSKiyoshi Ueda 
15306facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md,
15316facdaffSKiyoshi Ueda 				gfp_t gfp_mask)
15326facdaffSKiyoshi Ueda {
15336facdaffSKiyoshi Ueda 	struct request *clone;
15346facdaffSKiyoshi Ueda 	struct dm_rq_target_io *tio;
15356facdaffSKiyoshi Ueda 
15366facdaffSKiyoshi Ueda 	tio = alloc_rq_tio(md, gfp_mask);
15376facdaffSKiyoshi Ueda 	if (!tio)
15386facdaffSKiyoshi Ueda 		return NULL;
15396facdaffSKiyoshi Ueda 
15406facdaffSKiyoshi Ueda 	tio->md = md;
15416facdaffSKiyoshi Ueda 	tio->ti = NULL;
15426facdaffSKiyoshi Ueda 	tio->orig = rq;
15436facdaffSKiyoshi Ueda 	tio->error = 0;
15446facdaffSKiyoshi Ueda 	memset(&tio->info, 0, sizeof(tio->info));
15456facdaffSKiyoshi Ueda 
15466facdaffSKiyoshi Ueda 	clone = &tio->clone;
15476facdaffSKiyoshi Ueda 	if (setup_clone(clone, rq, tio)) {
15486facdaffSKiyoshi Ueda 		/* -ENOMEM */
15496facdaffSKiyoshi Ueda 		free_rq_tio(tio);
15506facdaffSKiyoshi Ueda 		return NULL;
15516facdaffSKiyoshi Ueda 	}
15526facdaffSKiyoshi Ueda 
15536facdaffSKiyoshi Ueda 	return clone;
15546facdaffSKiyoshi Ueda }
15556facdaffSKiyoshi Ueda 
1556cec47e3dSKiyoshi Ueda /*
1557cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1558cec47e3dSKiyoshi Ueda  */
1559cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq)
1560cec47e3dSKiyoshi Ueda {
1561cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1562cec47e3dSKiyoshi Ueda 	struct request *clone;
1563cec47e3dSKiyoshi Ueda 
1564cec47e3dSKiyoshi Ueda 	if (unlikely(rq->special)) {
1565cec47e3dSKiyoshi Ueda 		DMWARN("Already has something in rq->special.");
1566cec47e3dSKiyoshi Ueda 		return BLKPREP_KILL;
1567cec47e3dSKiyoshi Ueda 	}
1568cec47e3dSKiyoshi Ueda 
15696facdaffSKiyoshi Ueda 	clone = clone_rq(rq, md, GFP_ATOMIC);
15706facdaffSKiyoshi Ueda 	if (!clone)
1571cec47e3dSKiyoshi Ueda 		return BLKPREP_DEFER;
1572cec47e3dSKiyoshi Ueda 
1573cec47e3dSKiyoshi Ueda 	rq->special = clone;
1574cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_DONTPREP;
1575cec47e3dSKiyoshi Ueda 
1576cec47e3dSKiyoshi Ueda 	return BLKPREP_OK;
1577cec47e3dSKiyoshi Ueda }
1578cec47e3dSKiyoshi Ueda 
15799eef87daSKiyoshi Ueda /*
15809eef87daSKiyoshi Ueda  * Returns:
15819eef87daSKiyoshi Ueda  * 0  : the request has been processed (not requeued)
15829eef87daSKiyoshi Ueda  * !0 : the request has been requeued
15839eef87daSKiyoshi Ueda  */
15849eef87daSKiyoshi Ueda static int map_request(struct dm_target *ti, struct request *clone,
1585cec47e3dSKiyoshi Ueda 		       struct mapped_device *md)
1586cec47e3dSKiyoshi Ueda {
15879eef87daSKiyoshi Ueda 	int r, requeued = 0;
1588cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1589cec47e3dSKiyoshi Ueda 
1590cec47e3dSKiyoshi Ueda 	tio->ti = ti;
1591cec47e3dSKiyoshi Ueda 	r = ti->type->map_rq(ti, clone, &tio->info);
1592cec47e3dSKiyoshi Ueda 	switch (r) {
1593cec47e3dSKiyoshi Ueda 	case DM_MAPIO_SUBMITTED:
1594cec47e3dSKiyoshi Ueda 		/* The target has taken the I/O to submit by itself later */
1595cec47e3dSKiyoshi Ueda 		break;
1596cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REMAPPED:
1597cec47e3dSKiyoshi Ueda 		/* The target has remapped the I/O so dispatch it */
15986db4ccd6SJun'ichi Nomura 		trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
15996db4ccd6SJun'ichi Nomura 				     blk_rq_pos(tio->orig));
1600cec47e3dSKiyoshi Ueda 		dm_dispatch_request(clone);
1601cec47e3dSKiyoshi Ueda 		break;
1602cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REQUEUE:
1603cec47e3dSKiyoshi Ueda 		/* The target wants to requeue the I/O */
1604cec47e3dSKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
16059eef87daSKiyoshi Ueda 		requeued = 1;
1606cec47e3dSKiyoshi Ueda 		break;
1607cec47e3dSKiyoshi Ueda 	default:
1608cec47e3dSKiyoshi Ueda 		if (r > 0) {
1609cec47e3dSKiyoshi Ueda 			DMWARN("unimplemented target map return value: %d", r);
1610cec47e3dSKiyoshi Ueda 			BUG();
1611cec47e3dSKiyoshi Ueda 		}
1612cec47e3dSKiyoshi Ueda 
1613cec47e3dSKiyoshi Ueda 		/* The target wants to complete the I/O */
1614cec47e3dSKiyoshi Ueda 		dm_kill_unmapped_request(clone, r);
1615cec47e3dSKiyoshi Ueda 		break;
1616cec47e3dSKiyoshi Ueda 	}
16179eef87daSKiyoshi Ueda 
16189eef87daSKiyoshi Ueda 	return requeued;
1619cec47e3dSKiyoshi Ueda }
1620cec47e3dSKiyoshi Ueda 
1621ba1cbad9SMike Snitzer static struct request *dm_start_request(struct mapped_device *md, struct request *orig)
1622ba1cbad9SMike Snitzer {
1623ba1cbad9SMike Snitzer 	struct request *clone;
1624ba1cbad9SMike Snitzer 
1625ba1cbad9SMike Snitzer 	blk_start_request(orig);
1626ba1cbad9SMike Snitzer 	clone = orig->special;
1627ba1cbad9SMike Snitzer 	atomic_inc(&md->pending[rq_data_dir(clone)]);
1628ba1cbad9SMike Snitzer 
1629ba1cbad9SMike Snitzer 	/*
1630ba1cbad9SMike Snitzer 	 * Hold the md reference here for the in-flight I/O.
1631ba1cbad9SMike Snitzer 	 * We can't rely on the reference count by device opener,
1632ba1cbad9SMike Snitzer 	 * because the device may be closed during the request completion
1633ba1cbad9SMike Snitzer 	 * when all bios are completed.
1634ba1cbad9SMike Snitzer 	 * See the comment in rq_completed() too.
1635ba1cbad9SMike Snitzer 	 */
1636ba1cbad9SMike Snitzer 	dm_get(md);
1637ba1cbad9SMike Snitzer 
1638ba1cbad9SMike Snitzer 	return clone;
1639ba1cbad9SMike Snitzer }
1640ba1cbad9SMike Snitzer 
1641cec47e3dSKiyoshi Ueda /*
1642cec47e3dSKiyoshi Ueda  * q->request_fn for request-based dm.
1643cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1644cec47e3dSKiyoshi Ueda  */
1645cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q)
1646cec47e3dSKiyoshi Ueda {
1647cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
16487c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1649cec47e3dSKiyoshi Ueda 	struct dm_target *ti;
1650b4324feeSKiyoshi Ueda 	struct request *rq, *clone;
165129e4013dSTejun Heo 	sector_t pos;
1652cec47e3dSKiyoshi Ueda 
1653cec47e3dSKiyoshi Ueda 	/*
1654b4324feeSKiyoshi Ueda 	 * For suspend, check blk_queue_stopped() and increment
1655b4324feeSKiyoshi Ueda 	 * ->pending within a single queue_lock not to increment the
1656b4324feeSKiyoshi Ueda 	 * number of in-flight I/Os after the queue is stopped in
1657b4324feeSKiyoshi Ueda 	 * dm_suspend().
1658cec47e3dSKiyoshi Ueda 	 */
16597eaceaccSJens Axboe 	while (!blk_queue_stopped(q)) {
1660cec47e3dSKiyoshi Ueda 		rq = blk_peek_request(q);
1661cec47e3dSKiyoshi Ueda 		if (!rq)
16627eaceaccSJens Axboe 			goto delay_and_out;
1663cec47e3dSKiyoshi Ueda 
166429e4013dSTejun Heo 		/* always use block 0 to find the target for flushes for now */
166529e4013dSTejun Heo 		pos = 0;
166629e4013dSTejun Heo 		if (!(rq->cmd_flags & REQ_FLUSH))
166729e4013dSTejun Heo 			pos = blk_rq_pos(rq);
1668d0bcb878SKiyoshi Ueda 
166929e4013dSTejun Heo 		ti = dm_table_find_target(map, pos);
1670ba1cbad9SMike Snitzer 		if (!dm_target_is_valid(ti)) {
1671ba1cbad9SMike Snitzer 			/*
1672ba1cbad9SMike Snitzer 			 * Must perform setup, that dm_done() requires,
1673ba1cbad9SMike Snitzer 			 * before calling dm_kill_unmapped_request
1674ba1cbad9SMike Snitzer 			 */
1675ba1cbad9SMike Snitzer 			DMERR_LIMIT("request attempted access beyond the end of device");
1676ba1cbad9SMike Snitzer 			clone = dm_start_request(md, rq);
1677ba1cbad9SMike Snitzer 			dm_kill_unmapped_request(clone, -EIO);
1678ba1cbad9SMike Snitzer 			continue;
1679ba1cbad9SMike Snitzer 		}
168029e4013dSTejun Heo 
1681cec47e3dSKiyoshi Ueda 		if (ti->type->busy && ti->type->busy(ti))
16827eaceaccSJens Axboe 			goto delay_and_out;
1683cec47e3dSKiyoshi Ueda 
1684ba1cbad9SMike Snitzer 		clone = dm_start_request(md, rq);
1685b4324feeSKiyoshi Ueda 
1686cec47e3dSKiyoshi Ueda 		spin_unlock(q->queue_lock);
16879eef87daSKiyoshi Ueda 		if (map_request(ti, clone, md))
16889eef87daSKiyoshi Ueda 			goto requeued;
16899eef87daSKiyoshi Ueda 
1690052189a2SKiyoshi Ueda 		BUG_ON(!irqs_disabled());
1691052189a2SKiyoshi Ueda 		spin_lock(q->queue_lock);
1692cec47e3dSKiyoshi Ueda 	}
1693cec47e3dSKiyoshi Ueda 
1694cec47e3dSKiyoshi Ueda 	goto out;
1695cec47e3dSKiyoshi Ueda 
16969eef87daSKiyoshi Ueda requeued:
1697052189a2SKiyoshi Ueda 	BUG_ON(!irqs_disabled());
1698052189a2SKiyoshi Ueda 	spin_lock(q->queue_lock);
16999eef87daSKiyoshi Ueda 
17007eaceaccSJens Axboe delay_and_out:
17017eaceaccSJens Axboe 	blk_delay_queue(q, HZ / 10);
1702cec47e3dSKiyoshi Ueda out:
1703cec47e3dSKiyoshi Ueda 	dm_table_put(map);
1704cec47e3dSKiyoshi Ueda }
1705cec47e3dSKiyoshi Ueda 
1706cec47e3dSKiyoshi Ueda int dm_underlying_device_busy(struct request_queue *q)
1707cec47e3dSKiyoshi Ueda {
1708cec47e3dSKiyoshi Ueda 	return blk_lld_busy(q);
1709cec47e3dSKiyoshi Ueda }
1710cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1711cec47e3dSKiyoshi Ueda 
1712cec47e3dSKiyoshi Ueda static int dm_lld_busy(struct request_queue *q)
1713cec47e3dSKiyoshi Ueda {
1714cec47e3dSKiyoshi Ueda 	int r;
1715cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
17167c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1717cec47e3dSKiyoshi Ueda 
1718cec47e3dSKiyoshi Ueda 	if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1719cec47e3dSKiyoshi Ueda 		r = 1;
1720cec47e3dSKiyoshi Ueda 	else
1721cec47e3dSKiyoshi Ueda 		r = dm_table_any_busy_target(map);
1722cec47e3dSKiyoshi Ueda 
1723cec47e3dSKiyoshi Ueda 	dm_table_put(map);
1724cec47e3dSKiyoshi Ueda 
1725cec47e3dSKiyoshi Ueda 	return r;
1726cec47e3dSKiyoshi Ueda }
1727cec47e3dSKiyoshi Ueda 
17281da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
17291da177e4SLinus Torvalds {
17308a57dfc6SChandra Seetharaman 	int r = bdi_bits;
17318a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
17328a57dfc6SChandra Seetharaman 	struct dm_table *map;
17331da177e4SLinus Torvalds 
17341eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
17357c666411SAlasdair G Kergon 		map = dm_get_live_table(md);
17368a57dfc6SChandra Seetharaman 		if (map) {
1737cec47e3dSKiyoshi Ueda 			/*
1738cec47e3dSKiyoshi Ueda 			 * Request-based dm cares about only own queue for
1739cec47e3dSKiyoshi Ueda 			 * the query about congestion status of request_queue
1740cec47e3dSKiyoshi Ueda 			 */
1741cec47e3dSKiyoshi Ueda 			if (dm_request_based(md))
1742cec47e3dSKiyoshi Ueda 				r = md->queue->backing_dev_info.state &
1743cec47e3dSKiyoshi Ueda 				    bdi_bits;
1744cec47e3dSKiyoshi Ueda 			else
17451da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
1746cec47e3dSKiyoshi Ueda 
17471da177e4SLinus Torvalds 			dm_table_put(map);
17488a57dfc6SChandra Seetharaman 		}
17498a57dfc6SChandra Seetharaman 	}
17508a57dfc6SChandra Seetharaman 
17511da177e4SLinus Torvalds 	return r;
17521da177e4SLinus Torvalds }
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds /*-----------------------------------------------------------------
17551da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
17561da177e4SLinus Torvalds  *---------------------------------------------------------------*/
17572b06cfffSAlasdair G Kergon static void free_minor(int minor)
17581da177e4SLinus Torvalds {
1759f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17601da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1761f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
17621da177e4SLinus Torvalds }
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds /*
17651da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
17661da177e4SLinus Torvalds  */
1767cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
17681da177e4SLinus Torvalds {
1769c9d76be6STejun Heo 	int r;
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
17721da177e4SLinus Torvalds 		return -EINVAL;
17731da177e4SLinus Torvalds 
1774c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1775f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17761da177e4SLinus Torvalds 
1777c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
17781da177e4SLinus Torvalds 
1779f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1780c9d76be6STejun Heo 	idr_preload_end();
1781c9d76be6STejun Heo 	if (r < 0)
1782c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
1783c9d76be6STejun Heo 	return 0;
17841da177e4SLinus Torvalds }
17851da177e4SLinus Torvalds 
1786cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
17871da177e4SLinus Torvalds {
1788c9d76be6STejun Heo 	int r;
17891da177e4SLinus Torvalds 
1790c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1791f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17921da177e4SLinus Torvalds 
1793c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
17941da177e4SLinus Torvalds 
1795f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1796c9d76be6STejun Heo 	idr_preload_end();
1797c9d76be6STejun Heo 	if (r < 0)
17981da177e4SLinus Torvalds 		return r;
1799c9d76be6STejun Heo 	*minor = r;
1800c9d76be6STejun Heo 	return 0;
18011da177e4SLinus Torvalds }
18021da177e4SLinus Torvalds 
180383d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
18041da177e4SLinus Torvalds 
180553d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
180653d5914fSMikulas Patocka 
18074a0b4ddfSMike Snitzer static void dm_init_md_queue(struct mapped_device *md)
18084a0b4ddfSMike Snitzer {
18094a0b4ddfSMike Snitzer 	/*
18104a0b4ddfSMike Snitzer 	 * Request-based dm devices cannot be stacked on top of bio-based dm
18114a0b4ddfSMike Snitzer 	 * devices.  The type of this dm device has not been decided yet.
18124a0b4ddfSMike Snitzer 	 * The type is decided at the first table loading time.
18134a0b4ddfSMike Snitzer 	 * To prevent problematic device stacking, clear the queue flag
18144a0b4ddfSMike Snitzer 	 * for request stacking support until then.
18154a0b4ddfSMike Snitzer 	 *
18164a0b4ddfSMike Snitzer 	 * This queue is new, so no concurrency on the queue_flags.
18174a0b4ddfSMike Snitzer 	 */
18184a0b4ddfSMike Snitzer 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
18194a0b4ddfSMike Snitzer 
18204a0b4ddfSMike Snitzer 	md->queue->queuedata = md;
18214a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
18224a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_data = md;
18234a0b4ddfSMike Snitzer 	blk_queue_make_request(md->queue, dm_request);
18244a0b4ddfSMike Snitzer 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
18254a0b4ddfSMike Snitzer 	blk_queue_merge_bvec(md->queue, dm_merge_bvec);
18264a0b4ddfSMike Snitzer }
18274a0b4ddfSMike Snitzer 
18281da177e4SLinus Torvalds /*
18291da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
18301da177e4SLinus Torvalds  */
18312b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
18321da177e4SLinus Torvalds {
18331da177e4SLinus Torvalds 	int r;
1834cf13ab8eSFrederik Deweerdt 	struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1835ba61fdd1SJeff Mahoney 	void *old_md;
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds 	if (!md) {
18381da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
18391da177e4SLinus Torvalds 		return NULL;
18401da177e4SLinus Torvalds 	}
18411da177e4SLinus Torvalds 
184210da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
18436ed7ade8SMilan Broz 		goto bad_module_get;
184410da4f79SJeff Mahoney 
18451da177e4SLinus Torvalds 	/* get a minor number for the dev */
18462b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1847cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
18482b06cfffSAlasdair G Kergon 	else
1849cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
18501da177e4SLinus Torvalds 	if (r < 0)
18516ed7ade8SMilan Broz 		goto bad_minor;
18521da177e4SLinus Torvalds 
1853a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
18542ca3310eSAlasdair G Kergon 	init_rwsem(&md->io_lock);
1855e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1856a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
1857022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
18581da177e4SLinus Torvalds 	rwlock_init(&md->map_lock);
18591da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
18605c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
18611da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
18627a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
18637a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
18647a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
18651da177e4SLinus Torvalds 
18664a0b4ddfSMike Snitzer 	md->queue = blk_alloc_queue(GFP_KERNEL);
18671da177e4SLinus Torvalds 	if (!md->queue)
18686ed7ade8SMilan Broz 		goto bad_queue;
18691da177e4SLinus Torvalds 
18704a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
18719faf400fSStefan Bader 
18721da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
18731da177e4SLinus Torvalds 	if (!md->disk)
18746ed7ade8SMilan Broz 		goto bad_disk;
18751da177e4SLinus Torvalds 
1876316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
1877316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
1878f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
187953d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1880f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
1881f0b04115SJeff Mahoney 
18821da177e4SLinus Torvalds 	md->disk->major = _major;
18831da177e4SLinus Torvalds 	md->disk->first_minor = minor;
18841da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
18851da177e4SLinus Torvalds 	md->disk->queue = md->queue;
18861da177e4SLinus Torvalds 	md->disk->private_data = md;
18871da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
18881da177e4SLinus Torvalds 	add_disk(md->disk);
18897e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
18901da177e4SLinus Torvalds 
18919c4376deSTejun Heo 	md->wq = alloc_workqueue("kdmflush",
18929c4376deSTejun Heo 				 WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
1893304f3f6aSMilan Broz 	if (!md->wq)
1894304f3f6aSMilan Broz 		goto bad_thread;
1895304f3f6aSMilan Broz 
189632a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
189732a926daSMikulas Patocka 	if (!md->bdev)
189832a926daSMikulas Patocka 		goto bad_bdev;
189932a926daSMikulas Patocka 
19006a8736d1STejun Heo 	bio_init(&md->flush_bio);
19016a8736d1STejun Heo 	md->flush_bio.bi_bdev = md->bdev;
19026a8736d1STejun Heo 	md->flush_bio.bi_rw = WRITE_FLUSH;
19036a8736d1STejun Heo 
1904ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1905f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1906ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1907f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1908ba61fdd1SJeff Mahoney 
1909ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1910ba61fdd1SJeff Mahoney 
19111da177e4SLinus Torvalds 	return md;
19121da177e4SLinus Torvalds 
191332a926daSMikulas Patocka bad_bdev:
191432a926daSMikulas Patocka 	destroy_workqueue(md->wq);
1915304f3f6aSMilan Broz bad_thread:
191603022c54SZdenek Kabelac 	del_gendisk(md->disk);
1917304f3f6aSMilan Broz 	put_disk(md->disk);
19186ed7ade8SMilan Broz bad_disk:
19191312f40eSAl Viro 	blk_cleanup_queue(md->queue);
19206ed7ade8SMilan Broz bad_queue:
19211da177e4SLinus Torvalds 	free_minor(minor);
19226ed7ade8SMilan Broz bad_minor:
192310da4f79SJeff Mahoney 	module_put(THIS_MODULE);
19246ed7ade8SMilan Broz bad_module_get:
19251da177e4SLinus Torvalds 	kfree(md);
19261da177e4SLinus Torvalds 	return NULL;
19271da177e4SLinus Torvalds }
19281da177e4SLinus Torvalds 
1929ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1930ae9da83fSJun'ichi Nomura 
19311da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
19321da177e4SLinus Torvalds {
1933f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
193463d94e48SJun'ichi Nomura 
1935ae9da83fSJun'ichi Nomura 	unlock_fs(md);
1936db8fef4fSMikulas Patocka 	bdput(md->bdev);
1937304f3f6aSMilan Broz 	destroy_workqueue(md->wq);
1938e6ee8c0bSKiyoshi Ueda 	if (md->tio_pool)
19391da177e4SLinus Torvalds 		mempool_destroy(md->tio_pool);
1940e6ee8c0bSKiyoshi Ueda 	if (md->io_pool)
19411da177e4SLinus Torvalds 		mempool_destroy(md->io_pool);
1942e6ee8c0bSKiyoshi Ueda 	if (md->bs)
19439faf400fSStefan Bader 		bioset_free(md->bs);
19449c47008dSMartin K. Petersen 	blk_integrity_unregister(md->disk);
19451da177e4SLinus Torvalds 	del_gendisk(md->disk);
194663d94e48SJun'ichi Nomura 	free_minor(minor);
1947fba9f90eSJeff Mahoney 
1948fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
1949fba9f90eSJeff Mahoney 	md->disk->private_data = NULL;
1950fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
1951fba9f90eSJeff Mahoney 
19521da177e4SLinus Torvalds 	put_disk(md->disk);
19531312f40eSAl Viro 	blk_cleanup_queue(md->queue);
195410da4f79SJeff Mahoney 	module_put(THIS_MODULE);
19551da177e4SLinus Torvalds 	kfree(md);
19561da177e4SLinus Torvalds }
19571da177e4SLinus Torvalds 
1958e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1959e6ee8c0bSKiyoshi Ueda {
1960c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
1961e6ee8c0bSKiyoshi Ueda 
196216245bdcSJun'ichi Nomura 	if (md->io_pool && md->bs) {
196316245bdcSJun'ichi Nomura 		/* The md already has necessary mempools. */
196416245bdcSJun'ichi Nomura 		if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
1965c0820cf5SMikulas Patocka 			/*
196616245bdcSJun'ichi Nomura 			 * Reload bioset because front_pad may have changed
196716245bdcSJun'ichi Nomura 			 * because a different table was loaded.
1968c0820cf5SMikulas Patocka 			 */
1969c0820cf5SMikulas Patocka 			bioset_free(md->bs);
1970c0820cf5SMikulas Patocka 			md->bs = p->bs;
1971c0820cf5SMikulas Patocka 			p->bs = NULL;
197216245bdcSJun'ichi Nomura 		} else if (dm_table_get_type(t) == DM_TYPE_REQUEST_BASED) {
197316245bdcSJun'ichi Nomura 			BUG_ON(!md->tio_pool);
197416245bdcSJun'ichi Nomura 			/*
197516245bdcSJun'ichi Nomura 			 * There's no need to reload with request-based dm
197616245bdcSJun'ichi Nomura 			 * because the size of front_pad doesn't change.
197716245bdcSJun'ichi Nomura 			 * Note for future: If you are to reload bioset,
197816245bdcSJun'ichi Nomura 			 * prep-ed requests in the queue may refer
197916245bdcSJun'ichi Nomura 			 * to bio from the old bioset, so you must walk
198016245bdcSJun'ichi Nomura 			 * through the queue to unprep.
198116245bdcSJun'ichi Nomura 			 */
198216245bdcSJun'ichi Nomura 		}
1983e6ee8c0bSKiyoshi Ueda 		goto out;
1984c0820cf5SMikulas Patocka 	}
1985e6ee8c0bSKiyoshi Ueda 
1986e6ee8c0bSKiyoshi Ueda 	BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
1987e6ee8c0bSKiyoshi Ueda 
1988e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
1989e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
1990e6ee8c0bSKiyoshi Ueda 	md->tio_pool = p->tio_pool;
1991e6ee8c0bSKiyoshi Ueda 	p->tio_pool = NULL;
1992e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
1993e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
1994e6ee8c0bSKiyoshi Ueda 
1995e6ee8c0bSKiyoshi Ueda out:
1996e6ee8c0bSKiyoshi Ueda 	/* mempool bind completed, now no need any mempools in the table */
1997e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
1998e6ee8c0bSKiyoshi Ueda }
1999e6ee8c0bSKiyoshi Ueda 
20001da177e4SLinus Torvalds /*
20011da177e4SLinus Torvalds  * Bind a table to the device.
20021da177e4SLinus Torvalds  */
20031da177e4SLinus Torvalds static void event_callback(void *context)
20041da177e4SLinus Torvalds {
20057a8c3d3bSMike Anderson 	unsigned long flags;
20067a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
20071da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
20081da177e4SLinus Torvalds 
20097a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
20107a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
20117a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
20127a8c3d3bSMike Anderson 
2013ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
20147a8c3d3bSMike Anderson 
20151da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
20161da177e4SLinus Torvalds 	wake_up(&md->eventq);
20171da177e4SLinus Torvalds }
20181da177e4SLinus Torvalds 
2019c217649bSMike Snitzer /*
2020c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
2021c217649bSMike Snitzer  */
20224e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
20231da177e4SLinus Torvalds {
20244e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
20251da177e4SLinus Torvalds 
2026db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
20271da177e4SLinus Torvalds }
20281da177e4SLinus Torvalds 
2029042d2a9bSAlasdair G Kergon /*
2030d5b9dd04SMikulas Patocka  * Return 1 if the queue has a compulsory merge_bvec_fn function.
2031d5b9dd04SMikulas Patocka  *
2032d5b9dd04SMikulas Patocka  * If this function returns 0, then the device is either a non-dm
2033d5b9dd04SMikulas Patocka  * device without a merge_bvec_fn, or it is a dm device that is
2034d5b9dd04SMikulas Patocka  * able to split any bios it receives that are too big.
2035d5b9dd04SMikulas Patocka  */
2036d5b9dd04SMikulas Patocka int dm_queue_merge_is_compulsory(struct request_queue *q)
2037d5b9dd04SMikulas Patocka {
2038d5b9dd04SMikulas Patocka 	struct mapped_device *dev_md;
2039d5b9dd04SMikulas Patocka 
2040d5b9dd04SMikulas Patocka 	if (!q->merge_bvec_fn)
2041d5b9dd04SMikulas Patocka 		return 0;
2042d5b9dd04SMikulas Patocka 
2043d5b9dd04SMikulas Patocka 	if (q->make_request_fn == dm_request) {
2044d5b9dd04SMikulas Patocka 		dev_md = q->queuedata;
2045d5b9dd04SMikulas Patocka 		if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2046d5b9dd04SMikulas Patocka 			return 0;
2047d5b9dd04SMikulas Patocka 	}
2048d5b9dd04SMikulas Patocka 
2049d5b9dd04SMikulas Patocka 	return 1;
2050d5b9dd04SMikulas Patocka }
2051d5b9dd04SMikulas Patocka 
2052d5b9dd04SMikulas Patocka static int dm_device_merge_is_compulsory(struct dm_target *ti,
2053d5b9dd04SMikulas Patocka 					 struct dm_dev *dev, sector_t start,
2054d5b9dd04SMikulas Patocka 					 sector_t len, void *data)
2055d5b9dd04SMikulas Patocka {
2056d5b9dd04SMikulas Patocka 	struct block_device *bdev = dev->bdev;
2057d5b9dd04SMikulas Patocka 	struct request_queue *q = bdev_get_queue(bdev);
2058d5b9dd04SMikulas Patocka 
2059d5b9dd04SMikulas Patocka 	return dm_queue_merge_is_compulsory(q);
2060d5b9dd04SMikulas Patocka }
2061d5b9dd04SMikulas Patocka 
2062d5b9dd04SMikulas Patocka /*
2063d5b9dd04SMikulas Patocka  * Return 1 if it is acceptable to ignore merge_bvec_fn based
2064d5b9dd04SMikulas Patocka  * on the properties of the underlying devices.
2065d5b9dd04SMikulas Patocka  */
2066d5b9dd04SMikulas Patocka static int dm_table_merge_is_optional(struct dm_table *table)
2067d5b9dd04SMikulas Patocka {
2068d5b9dd04SMikulas Patocka 	unsigned i = 0;
2069d5b9dd04SMikulas Patocka 	struct dm_target *ti;
2070d5b9dd04SMikulas Patocka 
2071d5b9dd04SMikulas Patocka 	while (i < dm_table_get_num_targets(table)) {
2072d5b9dd04SMikulas Patocka 		ti = dm_table_get_target(table, i++);
2073d5b9dd04SMikulas Patocka 
2074d5b9dd04SMikulas Patocka 		if (ti->type->iterate_devices &&
2075d5b9dd04SMikulas Patocka 		    ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2076d5b9dd04SMikulas Patocka 			return 0;
2077d5b9dd04SMikulas Patocka 	}
2078d5b9dd04SMikulas Patocka 
2079d5b9dd04SMikulas Patocka 	return 1;
2080d5b9dd04SMikulas Patocka }
2081d5b9dd04SMikulas Patocka 
2082d5b9dd04SMikulas Patocka /*
2083042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2084042d2a9bSAlasdair G Kergon  */
2085042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2086754c5fc7SMike Snitzer 			       struct queue_limits *limits)
20871da177e4SLinus Torvalds {
2088042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
2089165125e1SJens Axboe 	struct request_queue *q = md->queue;
20901da177e4SLinus Torvalds 	sector_t size;
2091523d9297SKiyoshi Ueda 	unsigned long flags;
2092d5b9dd04SMikulas Patocka 	int merge_is_optional;
20931da177e4SLinus Torvalds 
20941da177e4SLinus Torvalds 	size = dm_table_get_size(t);
20953ac51e74SDarrick J. Wong 
20963ac51e74SDarrick J. Wong 	/*
20973ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
20983ac51e74SDarrick J. Wong 	 */
20993ac51e74SDarrick J. Wong 	if (size != get_capacity(md->disk))
21003ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
21013ac51e74SDarrick J. Wong 
21024e90188bSAlasdair G Kergon 	__set_size(md, size);
21031da177e4SLinus Torvalds 
2104cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
21052ca3310eSAlasdair G Kergon 
2106e6ee8c0bSKiyoshi Ueda 	/*
2107e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
2108e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
2109e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
2110e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
2111e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
2112e6ee8c0bSKiyoshi Ueda 	 */
2113e6ee8c0bSKiyoshi Ueda 	if (dm_table_request_based(t) && !blk_queue_stopped(q))
2114e6ee8c0bSKiyoshi Ueda 		stop_queue(q);
2115e6ee8c0bSKiyoshi Ueda 
2116e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
2117e6ee8c0bSKiyoshi Ueda 
2118d5b9dd04SMikulas Patocka 	merge_is_optional = dm_table_merge_is_optional(t);
2119d5b9dd04SMikulas Patocka 
2120523d9297SKiyoshi Ueda 	write_lock_irqsave(&md->map_lock, flags);
2121042d2a9bSAlasdair G Kergon 	old_map = md->map;
21222ca3310eSAlasdair G Kergon 	md->map = t;
212336a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
212436a0456fSAlasdair G Kergon 
2125754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
2126d5b9dd04SMikulas Patocka 	if (merge_is_optional)
2127d5b9dd04SMikulas Patocka 		set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2128d5b9dd04SMikulas Patocka 	else
2129d5b9dd04SMikulas Patocka 		clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2130523d9297SKiyoshi Ueda 	write_unlock_irqrestore(&md->map_lock, flags);
21312ca3310eSAlasdair G Kergon 
2132042d2a9bSAlasdair G Kergon 	return old_map;
21331da177e4SLinus Torvalds }
21341da177e4SLinus Torvalds 
2135a7940155SAlasdair G Kergon /*
2136a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2137a7940155SAlasdair G Kergon  */
2138a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
21391da177e4SLinus Torvalds {
21401da177e4SLinus Torvalds 	struct dm_table *map = md->map;
2141523d9297SKiyoshi Ueda 	unsigned long flags;
21421da177e4SLinus Torvalds 
21431da177e4SLinus Torvalds 	if (!map)
2144a7940155SAlasdair G Kergon 		return NULL;
21451da177e4SLinus Torvalds 
21461da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
2147523d9297SKiyoshi Ueda 	write_lock_irqsave(&md->map_lock, flags);
21481da177e4SLinus Torvalds 	md->map = NULL;
2149523d9297SKiyoshi Ueda 	write_unlock_irqrestore(&md->map_lock, flags);
2150a7940155SAlasdair G Kergon 
2151a7940155SAlasdair G Kergon 	return map;
21521da177e4SLinus Torvalds }
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds /*
21551da177e4SLinus Torvalds  * Constructor for a new device.
21561da177e4SLinus Torvalds  */
21572b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
21581da177e4SLinus Torvalds {
21591da177e4SLinus Torvalds 	struct mapped_device *md;
21601da177e4SLinus Torvalds 
21612b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
21621da177e4SLinus Torvalds 	if (!md)
21631da177e4SLinus Torvalds 		return -ENXIO;
21641da177e4SLinus Torvalds 
2165784aae73SMilan Broz 	dm_sysfs_init(md);
2166784aae73SMilan Broz 
21671da177e4SLinus Torvalds 	*result = md;
21681da177e4SLinus Torvalds 	return 0;
21691da177e4SLinus Torvalds }
21701da177e4SLinus Torvalds 
2171a5664dadSMike Snitzer /*
2172a5664dadSMike Snitzer  * Functions to manage md->type.
2173a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2174a5664dadSMike Snitzer  */
2175a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2176a5664dadSMike Snitzer {
2177a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2178a5664dadSMike Snitzer }
2179a5664dadSMike Snitzer 
2180a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2181a5664dadSMike Snitzer {
2182a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2183a5664dadSMike Snitzer }
2184a5664dadSMike Snitzer 
2185a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type)
2186a5664dadSMike Snitzer {
2187a5664dadSMike Snitzer 	md->type = type;
2188a5664dadSMike Snitzer }
2189a5664dadSMike Snitzer 
2190a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md)
2191a5664dadSMike Snitzer {
2192a5664dadSMike Snitzer 	return md->type;
2193a5664dadSMike Snitzer }
2194a5664dadSMike Snitzer 
219536a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
219636a0456fSAlasdair G Kergon {
219736a0456fSAlasdair G Kergon 	return md->immutable_target_type;
219836a0456fSAlasdair G Kergon }
219936a0456fSAlasdair G Kergon 
22004a0b4ddfSMike Snitzer /*
22014a0b4ddfSMike Snitzer  * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
22024a0b4ddfSMike Snitzer  */
22034a0b4ddfSMike Snitzer static int dm_init_request_based_queue(struct mapped_device *md)
22044a0b4ddfSMike Snitzer {
22054a0b4ddfSMike Snitzer 	struct request_queue *q = NULL;
22064a0b4ddfSMike Snitzer 
22074a0b4ddfSMike Snitzer 	if (md->queue->elevator)
22084a0b4ddfSMike Snitzer 		return 1;
22094a0b4ddfSMike Snitzer 
22104a0b4ddfSMike Snitzer 	/* Fully initialize the queue */
22114a0b4ddfSMike Snitzer 	q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
22124a0b4ddfSMike Snitzer 	if (!q)
22134a0b4ddfSMike Snitzer 		return 0;
22144a0b4ddfSMike Snitzer 
22154a0b4ddfSMike Snitzer 	md->queue = q;
22164a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
22174a0b4ddfSMike Snitzer 	blk_queue_softirq_done(md->queue, dm_softirq_done);
22184a0b4ddfSMike Snitzer 	blk_queue_prep_rq(md->queue, dm_prep_fn);
22194a0b4ddfSMike Snitzer 	blk_queue_lld_busy(md->queue, dm_lld_busy);
22204a0b4ddfSMike Snitzer 
22214a0b4ddfSMike Snitzer 	elv_register_queue(md->queue);
22224a0b4ddfSMike Snitzer 
22234a0b4ddfSMike Snitzer 	return 1;
22244a0b4ddfSMike Snitzer }
22254a0b4ddfSMike Snitzer 
22264a0b4ddfSMike Snitzer /*
22274a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
22284a0b4ddfSMike Snitzer  */
22294a0b4ddfSMike Snitzer int dm_setup_md_queue(struct mapped_device *md)
22304a0b4ddfSMike Snitzer {
22314a0b4ddfSMike Snitzer 	if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
22324a0b4ddfSMike Snitzer 	    !dm_init_request_based_queue(md)) {
22334a0b4ddfSMike Snitzer 		DMWARN("Cannot initialize queue for request-based mapped device");
22344a0b4ddfSMike Snitzer 		return -EINVAL;
22354a0b4ddfSMike Snitzer 	}
22364a0b4ddfSMike Snitzer 
22374a0b4ddfSMike Snitzer 	return 0;
22384a0b4ddfSMike Snitzer }
22394a0b4ddfSMike Snitzer 
2240637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev)
22411da177e4SLinus Torvalds {
22421da177e4SLinus Torvalds 	struct mapped_device *md;
22431da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
22441da177e4SLinus Torvalds 
22451da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
22461da177e4SLinus Torvalds 		return NULL;
22471da177e4SLinus Torvalds 
2248f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
22491da177e4SLinus Torvalds 
22501da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
2251fba9f90eSJeff Mahoney 	if (md && (md == MINOR_ALLOCED ||
2252f331c029STejun Heo 		   (MINOR(disk_devt(dm_disk(md))) != minor) ||
2253abdc568bSKiyoshi Ueda 		   dm_deleting_md(md) ||
2254fba9f90eSJeff Mahoney 		   test_bit(DMF_FREEING, &md->flags))) {
2255637842cfSDavid Teigland 		md = NULL;
2256fba9f90eSJeff Mahoney 		goto out;
2257fba9f90eSJeff Mahoney 	}
22581da177e4SLinus Torvalds 
2259fba9f90eSJeff Mahoney out:
2260f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
22611da177e4SLinus Torvalds 
2262637842cfSDavid Teigland 	return md;
2263637842cfSDavid Teigland }
2264637842cfSDavid Teigland 
2265d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev)
2266d229a958SDavid Teigland {
2267d229a958SDavid Teigland 	struct mapped_device *md = dm_find_md(dev);
2268d229a958SDavid Teigland 
2269d229a958SDavid Teigland 	if (md)
2270d229a958SDavid Teigland 		dm_get(md);
2271d229a958SDavid Teigland 
2272d229a958SDavid Teigland 	return md;
2273d229a958SDavid Teigland }
22743cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2275d229a958SDavid Teigland 
22769ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2277637842cfSDavid Teigland {
22789ade92a9SAlasdair G Kergon 	return md->interface_ptr;
22791da177e4SLinus Torvalds }
22801da177e4SLinus Torvalds 
22811da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
22821da177e4SLinus Torvalds {
22831da177e4SLinus Torvalds 	md->interface_ptr = ptr;
22841da177e4SLinus Torvalds }
22851da177e4SLinus Torvalds 
22861da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
22871da177e4SLinus Torvalds {
22881da177e4SLinus Torvalds 	atomic_inc(&md->holders);
22893f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
22901da177e4SLinus Torvalds }
22911da177e4SLinus Torvalds 
229272d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
229372d94861SAlasdair G Kergon {
229472d94861SAlasdair G Kergon 	return md->name;
229572d94861SAlasdair G Kergon }
229672d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
229772d94861SAlasdair G Kergon 
22983f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
22991da177e4SLinus Torvalds {
23001134e5aeSMike Anderson 	struct dm_table *map;
23011da177e4SLinus Torvalds 
23023f77316dSKiyoshi Ueda 	might_sleep();
2303fba9f90eSJeff Mahoney 
23043f77316dSKiyoshi Ueda 	spin_lock(&_minor_lock);
23057c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
23063f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2307fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2308f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
23093f77316dSKiyoshi Ueda 
23104f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
23111da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
23121da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
23131da177e4SLinus Torvalds 	}
23143f77316dSKiyoshi Ueda 
23153f77316dSKiyoshi Ueda 	/*
23163f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
23173f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
23183f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
23193f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
23203f77316dSKiyoshi Ueda 	 */
23213f77316dSKiyoshi Ueda 	if (wait)
23223f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
23233f77316dSKiyoshi Ueda 			msleep(1);
23243f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
23253f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
23263f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
23273f77316dSKiyoshi Ueda 
2328784aae73SMilan Broz 	dm_sysfs_exit(md);
23291134e5aeSMike Anderson 	dm_table_put(map);
2330a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
23311da177e4SLinus Torvalds 	free_dev(md);
23321da177e4SLinus Torvalds }
23333f77316dSKiyoshi Ueda 
23343f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
23353f77316dSKiyoshi Ueda {
23363f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
23373f77316dSKiyoshi Ueda }
23383f77316dSKiyoshi Ueda 
23393f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
23403f77316dSKiyoshi Ueda {
23413f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
23423f77316dSKiyoshi Ueda }
23433f77316dSKiyoshi Ueda 
23443f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
23453f77316dSKiyoshi Ueda {
23463f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
23471da177e4SLinus Torvalds }
234879eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
23491da177e4SLinus Torvalds 
2350401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
235146125c1cSMilan Broz {
235246125c1cSMilan Broz 	int r = 0;
2353b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
2354b44ebeb0SMikulas Patocka 
2355b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
235646125c1cSMilan Broz 
235746125c1cSMilan Broz 	while (1) {
2358401600dfSMikulas Patocka 		set_current_state(interruptible);
235946125c1cSMilan Broz 
2360b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
236146125c1cSMilan Broz 			break;
236246125c1cSMilan Broz 
2363401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
2364401600dfSMikulas Patocka 		    signal_pending(current)) {
236546125c1cSMilan Broz 			r = -EINTR;
236646125c1cSMilan Broz 			break;
236746125c1cSMilan Broz 		}
236846125c1cSMilan Broz 
236946125c1cSMilan Broz 		io_schedule();
237046125c1cSMilan Broz 	}
237146125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
237246125c1cSMilan Broz 
2373b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
2374b44ebeb0SMikulas Patocka 
237546125c1cSMilan Broz 	return r;
237646125c1cSMilan Broz }
237746125c1cSMilan Broz 
23781da177e4SLinus Torvalds /*
23791da177e4SLinus Torvalds  * Process the deferred bios
23801da177e4SLinus Torvalds  */
2381ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
23821da177e4SLinus Torvalds {
2383ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2384ef208587SMikulas Patocka 						work);
23856d6f10dfSMilan Broz 	struct bio *c;
23861da177e4SLinus Torvalds 
23876a8736d1STejun Heo 	down_read(&md->io_lock);
2388ef208587SMikulas Patocka 
23893b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2390022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2391022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2392022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2393022c2611SMikulas Patocka 
23946a8736d1STejun Heo 		if (!c)
2395df12ee99SAlasdair G Kergon 			break;
239673d410c0SMilan Broz 
23976a8736d1STejun Heo 		up_read(&md->io_lock);
23983b00b203SMikulas Patocka 
2399e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2400e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2401af7e466aSMikulas Patocka 		else
2402df12ee99SAlasdair G Kergon 			__split_and_process_bio(md, c);
24036a8736d1STejun Heo 
24046a8736d1STejun Heo 		down_read(&md->io_lock);
2405e6ee8c0bSKiyoshi Ueda 	}
24063b00b203SMikulas Patocka 
24076a8736d1STejun Heo 	up_read(&md->io_lock);
24081da177e4SLinus Torvalds }
24091da177e4SLinus Torvalds 
24109a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2411304f3f6aSMilan Broz {
24123b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
24133b00b203SMikulas Patocka 	smp_mb__after_clear_bit();
241453d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2415304f3f6aSMilan Broz }
2416304f3f6aSMilan Broz 
24171da177e4SLinus Torvalds /*
2418042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
24191da177e4SLinus Torvalds  */
2420042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
24211da177e4SLinus Torvalds {
24223ae70656SMike Snitzer 	struct dm_table *live_map, *map = ERR_PTR(-EINVAL);
2423754c5fc7SMike Snitzer 	struct queue_limits limits;
2424042d2a9bSAlasdair G Kergon 	int r;
24251da177e4SLinus Torvalds 
2426e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
24271da177e4SLinus Torvalds 
24281da177e4SLinus Torvalds 	/* device must be suspended */
24294f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
243093c534aeSAlasdair G Kergon 		goto out;
24311da177e4SLinus Torvalds 
24323ae70656SMike Snitzer 	/*
24333ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
24343ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
24353ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
24363ae70656SMike Snitzer 	 * reappear.
24373ae70656SMike Snitzer 	 */
24383ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
24393ae70656SMike Snitzer 		live_map = dm_get_live_table(md);
24403ae70656SMike Snitzer 		if (live_map)
24413ae70656SMike Snitzer 			limits = md->queue->limits;
24423ae70656SMike Snitzer 		dm_table_put(live_map);
24433ae70656SMike Snitzer 	}
24443ae70656SMike Snitzer 
2445754c5fc7SMike Snitzer 	r = dm_calculate_queue_limits(table, &limits);
2446042d2a9bSAlasdair G Kergon 	if (r) {
2447042d2a9bSAlasdair G Kergon 		map = ERR_PTR(r);
2448754c5fc7SMike Snitzer 		goto out;
2449042d2a9bSAlasdair G Kergon 	}
2450754c5fc7SMike Snitzer 
2451042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
24521da177e4SLinus Torvalds 
245393c534aeSAlasdair G Kergon out:
2454e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2455042d2a9bSAlasdair G Kergon 	return map;
24561da177e4SLinus Torvalds }
24571da177e4SLinus Torvalds 
24581da177e4SLinus Torvalds /*
24591da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
24601da177e4SLinus Torvalds  * device.
24611da177e4SLinus Torvalds  */
24622ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
24631da177e4SLinus Torvalds {
2464e39e2e95SAlasdair G Kergon 	int r;
24651da177e4SLinus Torvalds 
24661da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2467dfbe03f6SAlasdair G Kergon 
2468db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2469dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2470cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2471e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2472e39e2e95SAlasdair G Kergon 		return r;
2473dfbe03f6SAlasdair G Kergon 	}
2474dfbe03f6SAlasdair G Kergon 
2475aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2476aa8d7c2fSAlasdair G Kergon 
24771da177e4SLinus Torvalds 	return 0;
24781da177e4SLinus Torvalds }
24791da177e4SLinus Torvalds 
24802ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
24811da177e4SLinus Torvalds {
2482aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2483aa8d7c2fSAlasdair G Kergon 		return;
2484aa8d7c2fSAlasdair G Kergon 
2485db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
24861da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2487aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
24881da177e4SLinus Torvalds }
24891da177e4SLinus Torvalds 
24901da177e4SLinus Torvalds /*
24911da177e4SLinus Torvalds  * We need to be able to change a mapping table under a mounted
24921da177e4SLinus Torvalds  * filesystem.  For example we might want to move some data in
24931da177e4SLinus Torvalds  * the background.  Before the table can be swapped with
24941da177e4SLinus Torvalds  * dm_bind_table, dm_suspend must be called to flush any in
24951da177e4SLinus Torvalds  * flight bios and ensure that any further io gets deferred.
24961da177e4SLinus Torvalds  */
2497cec47e3dSKiyoshi Ueda /*
2498cec47e3dSKiyoshi Ueda  * Suspend mechanism in request-based dm.
2499cec47e3dSKiyoshi Ueda  *
25009f518b27SKiyoshi Ueda  * 1. Flush all I/Os by lock_fs() if needed.
25019f518b27SKiyoshi Ueda  * 2. Stop dispatching any I/O by stopping the request_queue.
25029f518b27SKiyoshi Ueda  * 3. Wait for all in-flight I/Os to be completed or requeued.
2503cec47e3dSKiyoshi Ueda  *
25049f518b27SKiyoshi Ueda  * To abort suspend, start the request_queue.
2505cec47e3dSKiyoshi Ueda  */
2506a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
25071da177e4SLinus Torvalds {
25082ca3310eSAlasdair G Kergon 	struct dm_table *map = NULL;
250946125c1cSMilan Broz 	int r = 0;
2510a3d77d35SKiyoshi Ueda 	int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
25112e93ccc1SKiyoshi Ueda 	int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
25121da177e4SLinus Torvalds 
2513e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
25142ca3310eSAlasdair G Kergon 
25154f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
251673d410c0SMilan Broz 		r = -EINVAL;
2517d287483dSAlasdair G Kergon 		goto out_unlock;
251873d410c0SMilan Broz 	}
25191da177e4SLinus Torvalds 
25207c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
2521cf222b37SAlasdair G Kergon 
25222e93ccc1SKiyoshi Ueda 	/*
25232e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
25242e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
25252e93ccc1SKiyoshi Ueda 	 */
25262e93ccc1SKiyoshi Ueda 	if (noflush)
25272e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
25282e93ccc1SKiyoshi Ueda 
2529436d4108SAlasdair G Kergon 	/* This does not get reverted if there's an error later. */
25301da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
25311da177e4SLinus Torvalds 
25322e93ccc1SKiyoshi Ueda 	/*
25339f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
25349f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
25359f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
25369f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
25372e93ccc1SKiyoshi Ueda 	 */
253832a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
25392ca3310eSAlasdair G Kergon 		r = lock_fs(md);
25402ca3310eSAlasdair G Kergon 		if (r)
25412ca3310eSAlasdair G Kergon 			goto out;
2542aa8d7c2fSAlasdair G Kergon 	}
25431da177e4SLinus Torvalds 
25441da177e4SLinus Torvalds 	/*
25453b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
25463b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
25473b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
25483b00b203SMikulas Patocka 	 * dm_wq_work.
25493b00b203SMikulas Patocka 	 *
25503b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
25513b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
25526a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
25536a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
25546a8736d1STejun Heo 	 * flush_workqueue(md->wq).
25551da177e4SLinus Torvalds 	 */
25562ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
25571eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
25582ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
25591da177e4SLinus Torvalds 
2560d0bcb878SKiyoshi Ueda 	/*
256129e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
256229e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2563d0bcb878SKiyoshi Ueda 	 */
2564cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
25659f518b27SKiyoshi Ueda 		stop_queue(md->queue);
2566cec47e3dSKiyoshi Ueda 
2567d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2568d0bcb878SKiyoshi Ueda 
25691da177e4SLinus Torvalds 	/*
25703b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
25713b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
25723b00b203SMikulas Patocka 	 * to finish.
25731da177e4SLinus Torvalds 	 */
2574401600dfSMikulas Patocka 	r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
25751da177e4SLinus Torvalds 
25762ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
25776d6f10dfSMilan Broz 	if (noflush)
2578022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
257994d6351eSMilan Broz 	up_write(&md->io_lock);
25802e93ccc1SKiyoshi Ueda 
25811da177e4SLinus Torvalds 	/* were we interrupted ? */
258246125c1cSMilan Broz 	if (r < 0) {
25839a1fb464SMikulas Patocka 		dm_queue_flush(md);
258473d410c0SMilan Broz 
2585cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
25869f518b27SKiyoshi Ueda 			start_queue(md->queue);
2587cec47e3dSKiyoshi Ueda 
25882ca3310eSAlasdair G Kergon 		unlock_fs(md);
25892e93ccc1SKiyoshi Ueda 		goto out; /* pushback list is already flushed, so skip flush */
25902ca3310eSAlasdair G Kergon 	}
25912ca3310eSAlasdair G Kergon 
25923b00b203SMikulas Patocka 	/*
25933b00b203SMikulas Patocka 	 * If dm_wait_for_completion returned 0, the device is completely
25943b00b203SMikulas Patocka 	 * quiescent now. There is no request-processing activity. All new
25953b00b203SMikulas Patocka 	 * requests are being added to md->deferred list.
25963b00b203SMikulas Patocka 	 */
25973b00b203SMikulas Patocka 
25981da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
25991da177e4SLinus Torvalds 
26004d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
26014d4471cbSKiyoshi Ueda 
26022ca3310eSAlasdair G Kergon out:
26031da177e4SLinus Torvalds 	dm_table_put(map);
2604d287483dSAlasdair G Kergon 
2605d287483dSAlasdair G Kergon out_unlock:
2606e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2607cf222b37SAlasdair G Kergon 	return r;
26081da177e4SLinus Torvalds }
26091da177e4SLinus Torvalds 
26101da177e4SLinus Torvalds int dm_resume(struct mapped_device *md)
26111da177e4SLinus Torvalds {
2612cf222b37SAlasdair G Kergon 	int r = -EINVAL;
2613cf222b37SAlasdair G Kergon 	struct dm_table *map = NULL;
26141da177e4SLinus Torvalds 
2615e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
26164f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
2617cf222b37SAlasdair G Kergon 		goto out;
2618cf222b37SAlasdair G Kergon 
26197c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
26202ca3310eSAlasdair G Kergon 	if (!map || !dm_table_get_size(map))
2621cf222b37SAlasdair G Kergon 		goto out;
26221da177e4SLinus Torvalds 
26238757b776SMilan Broz 	r = dm_table_resume_targets(map);
26248757b776SMilan Broz 	if (r)
26258757b776SMilan Broz 		goto out;
26262ca3310eSAlasdair G Kergon 
26279a1fb464SMikulas Patocka 	dm_queue_flush(md);
26282ca3310eSAlasdair G Kergon 
2629cec47e3dSKiyoshi Ueda 	/*
2630cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2631cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2632cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2633cec47e3dSKiyoshi Ueda 	 */
2634cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2635cec47e3dSKiyoshi Ueda 		start_queue(md->queue);
2636cec47e3dSKiyoshi Ueda 
26372ca3310eSAlasdair G Kergon 	unlock_fs(md);
26382ca3310eSAlasdair G Kergon 
26392ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
26402ca3310eSAlasdair G Kergon 
2641cf222b37SAlasdair G Kergon 	r = 0;
2642cf222b37SAlasdair G Kergon out:
2643cf222b37SAlasdair G Kergon 	dm_table_put(map);
2644e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
26452ca3310eSAlasdair G Kergon 
2646cf222b37SAlasdair G Kergon 	return r;
26471da177e4SLinus Torvalds }
26481da177e4SLinus Torvalds 
26491da177e4SLinus Torvalds /*-----------------------------------------------------------------
26501da177e4SLinus Torvalds  * Event notification.
26511da177e4SLinus Torvalds  *---------------------------------------------------------------*/
26523abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
265360935eb2SMilan Broz 		       unsigned cookie)
265469267a30SAlasdair G Kergon {
265560935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
265660935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
265760935eb2SMilan Broz 
265860935eb2SMilan Broz 	if (!cookie)
26593abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
266060935eb2SMilan Broz 	else {
266160935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
266260935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
26633abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
26643abf85b5SPeter Rajnoha 					  action, envp);
266560935eb2SMilan Broz 	}
266669267a30SAlasdair G Kergon }
266769267a30SAlasdair G Kergon 
26687a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
26697a8c3d3bSMike Anderson {
26707a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
26717a8c3d3bSMike Anderson }
26727a8c3d3bSMike Anderson 
26731da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
26741da177e4SLinus Torvalds {
26751da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
26761da177e4SLinus Torvalds }
26771da177e4SLinus Torvalds 
26781da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
26791da177e4SLinus Torvalds {
26801da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
26811da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
26821da177e4SLinus Torvalds }
26831da177e4SLinus Torvalds 
26847a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
26857a8c3d3bSMike Anderson {
26867a8c3d3bSMike Anderson 	unsigned long flags;
26877a8c3d3bSMike Anderson 
26887a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
26897a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
26907a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
26917a8c3d3bSMike Anderson }
26927a8c3d3bSMike Anderson 
26931da177e4SLinus Torvalds /*
26941da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
26951da177e4SLinus Torvalds  * count on 'md'.
26961da177e4SLinus Torvalds  */
26971da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
26981da177e4SLinus Torvalds {
26991da177e4SLinus Torvalds 	return md->disk;
27001da177e4SLinus Torvalds }
27011da177e4SLinus Torvalds 
2702784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2703784aae73SMilan Broz {
2704784aae73SMilan Broz 	return &md->kobj;
2705784aae73SMilan Broz }
2706784aae73SMilan Broz 
2707784aae73SMilan Broz /*
2708784aae73SMilan Broz  * struct mapped_device should not be exported outside of dm.c
2709784aae73SMilan Broz  * so use this check to verify that kobj is part of md structure
2710784aae73SMilan Broz  */
2711784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2712784aae73SMilan Broz {
2713784aae73SMilan Broz 	struct mapped_device *md;
2714784aae73SMilan Broz 
2715784aae73SMilan Broz 	md = container_of(kobj, struct mapped_device, kobj);
2716784aae73SMilan Broz 	if (&md->kobj != kobj)
2717784aae73SMilan Broz 		return NULL;
2718784aae73SMilan Broz 
27194d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
2720432a212cSMike Anderson 	    dm_deleting_md(md))
27214d89b7b4SMilan Broz 		return NULL;
27224d89b7b4SMilan Broz 
2723784aae73SMilan Broz 	dm_get(md);
2724784aae73SMilan Broz 	return md;
2725784aae73SMilan Broz }
2726784aae73SMilan Broz 
27274f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
27281da177e4SLinus Torvalds {
27291da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
27301da177e4SLinus Torvalds }
27311da177e4SLinus Torvalds 
273264dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
273364dbce58SKiyoshi Ueda {
2734ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
273564dbce58SKiyoshi Ueda }
273664dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
273764dbce58SKiyoshi Ueda 
27382e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
27392e93ccc1SKiyoshi Ueda {
2740ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
27412e93ccc1SKiyoshi Ueda }
27422e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
27432e93ccc1SKiyoshi Ueda 
2744c0820cf5SMikulas Patocka struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity, unsigned per_bio_data_size)
2745e6ee8c0bSKiyoshi Ueda {
2746e6ee8c0bSKiyoshi Ueda 	struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2747a91a2785SMartin K. Petersen 	unsigned int pool_size = (type == DM_TYPE_BIO_BASED) ? 16 : MIN_IOS;
2748e6ee8c0bSKiyoshi Ueda 
2749e6ee8c0bSKiyoshi Ueda 	if (!pools)
2750e6ee8c0bSKiyoshi Ueda 		return NULL;
2751e6ee8c0bSKiyoshi Ueda 
2752c0820cf5SMikulas Patocka 	per_bio_data_size = roundup(per_bio_data_size, __alignof__(struct dm_target_io));
2753c0820cf5SMikulas Patocka 
2754e6ee8c0bSKiyoshi Ueda 	pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2755e6ee8c0bSKiyoshi Ueda 			 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2756e6ee8c0bSKiyoshi Ueda 			 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2757e6ee8c0bSKiyoshi Ueda 	if (!pools->io_pool)
2758e6ee8c0bSKiyoshi Ueda 		goto free_pools_and_out;
2759e6ee8c0bSKiyoshi Ueda 
2760dba14160SMikulas Patocka 	pools->tio_pool = NULL;
2761dba14160SMikulas Patocka 	if (type == DM_TYPE_REQUEST_BASED) {
2762dba14160SMikulas Patocka 		pools->tio_pool = mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2763e6ee8c0bSKiyoshi Ueda 		if (!pools->tio_pool)
2764e6ee8c0bSKiyoshi Ueda 			goto free_io_pool_and_out;
2765dba14160SMikulas Patocka 	}
2766e6ee8c0bSKiyoshi Ueda 
276794818742SKent Overstreet 	pools->bs = (type == DM_TYPE_BIO_BASED) ?
2768dba14160SMikulas Patocka 		bioset_create(pool_size,
2769c0820cf5SMikulas Patocka 			      per_bio_data_size + offsetof(struct dm_target_io, clone)) :
277094818742SKent Overstreet 		bioset_create(pool_size,
277194818742SKent Overstreet 			      offsetof(struct dm_rq_clone_bio_info, clone));
2772e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
2773e6ee8c0bSKiyoshi Ueda 		goto free_tio_pool_and_out;
2774e6ee8c0bSKiyoshi Ueda 
2775a91a2785SMartin K. Petersen 	if (integrity && bioset_integrity_create(pools->bs, pool_size))
2776a91a2785SMartin K. Petersen 		goto free_bioset_and_out;
2777a91a2785SMartin K. Petersen 
2778e6ee8c0bSKiyoshi Ueda 	return pools;
2779e6ee8c0bSKiyoshi Ueda 
2780a91a2785SMartin K. Petersen free_bioset_and_out:
2781a91a2785SMartin K. Petersen 	bioset_free(pools->bs);
2782a91a2785SMartin K. Petersen 
2783e6ee8c0bSKiyoshi Ueda free_tio_pool_and_out:
2784dba14160SMikulas Patocka 	if (pools->tio_pool)
2785e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->tio_pool);
2786e6ee8c0bSKiyoshi Ueda 
2787e6ee8c0bSKiyoshi Ueda free_io_pool_and_out:
2788e6ee8c0bSKiyoshi Ueda 	mempool_destroy(pools->io_pool);
2789e6ee8c0bSKiyoshi Ueda 
2790e6ee8c0bSKiyoshi Ueda free_pools_and_out:
2791e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2792e6ee8c0bSKiyoshi Ueda 
2793e6ee8c0bSKiyoshi Ueda 	return NULL;
2794e6ee8c0bSKiyoshi Ueda }
2795e6ee8c0bSKiyoshi Ueda 
2796e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
2797e6ee8c0bSKiyoshi Ueda {
2798e6ee8c0bSKiyoshi Ueda 	if (!pools)
2799e6ee8c0bSKiyoshi Ueda 		return;
2800e6ee8c0bSKiyoshi Ueda 
2801e6ee8c0bSKiyoshi Ueda 	if (pools->io_pool)
2802e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->io_pool);
2803e6ee8c0bSKiyoshi Ueda 
2804e6ee8c0bSKiyoshi Ueda 	if (pools->tio_pool)
2805e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->tio_pool);
2806e6ee8c0bSKiyoshi Ueda 
2807e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
2808e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
2809e6ee8c0bSKiyoshi Ueda 
2810e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2811e6ee8c0bSKiyoshi Ueda }
2812e6ee8c0bSKiyoshi Ueda 
281383d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
28141da177e4SLinus Torvalds 	.open = dm_blk_open,
28151da177e4SLinus Torvalds 	.release = dm_blk_close,
2816aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
28173ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
28181da177e4SLinus Torvalds 	.owner = THIS_MODULE
28191da177e4SLinus Torvalds };
28201da177e4SLinus Torvalds 
28211da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo);
28221da177e4SLinus Torvalds 
28231da177e4SLinus Torvalds /*
28241da177e4SLinus Torvalds  * module hooks
28251da177e4SLinus Torvalds  */
28261da177e4SLinus Torvalds module_init(dm_init);
28271da177e4SLinus Torvalds module_exit(dm_exit);
28281da177e4SLinus Torvalds 
28291da177e4SLinus Torvalds module_param(major, uint, 0);
28301da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
28311da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
28321da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
28331da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2834