xref: /openbmc/linux/drivers/md/dm.c (revision 87eb5b21d92a92ac2da3163039d62df88c2b8422)
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  */
106414fe594dSAlasdair G Kergon static void clone_split_bio(struct dm_target_io *tio, struct bio *bio,
106514fe594dSAlasdair G Kergon 			    sector_t sector, unsigned short idx,
106614fe594dSAlasdair G Kergon 			    unsigned offset, unsigned 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,
1090e4c93811SAlasdair G Kergon 		      unsigned short bv_count, unsigned 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,
110655a62eefSAlasdair 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));
111755a62eefSAlasdair G Kergon 	tio->target_bio_nr = target_bio_nr;
11189015df24SAlasdair G Kergon 
11199015df24SAlasdair G Kergon 	return tio;
11209015df24SAlasdair G Kergon }
11219015df24SAlasdair G Kergon 
112214fe594dSAlasdair G Kergon static void __clone_and_map_simple_bio(struct clone_info *ci,
112314fe594dSAlasdair G Kergon 				       struct dm_target *ti,
112455a62eefSAlasdair G Kergon 				       unsigned target_bio_nr, sector_t len)
11259015df24SAlasdair G Kergon {
112655a62eefSAlasdair G Kergon 	struct dm_target_io *tio = alloc_tio(ci, ti, ci->bio->bi_max_vecs, target_bio_nr);
1127dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
11289015df24SAlasdair G Kergon 
112906a426ceSMike Snitzer 	/*
113006a426ceSMike Snitzer 	 * Discard requests require the bio's inline iovecs be initialized.
113106a426ceSMike Snitzer 	 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
113206a426ceSMike Snitzer 	 * and discard, so no need for concern about wasted bvec allocations.
113306a426ceSMike Snitzer 	 */
1134dba14160SMikulas Patocka 	 __bio_clone(clone, ci->bio);
1135bd2a49b8SAlasdair G Kergon 	if (len)
1136bd2a49b8SAlasdair G Kergon 		bio_setup_sector(clone, ci->sector, len);
1137f9ab94ceSMikulas Patocka 
1138bd2a49b8SAlasdair G Kergon 	__map_bio(tio);
1139f9ab94ceSMikulas Patocka }
1140f9ab94ceSMikulas Patocka 
114114fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
114255a62eefSAlasdair G Kergon 				  unsigned num_bios, sector_t len)
114306a426ceSMike Snitzer {
114455a62eefSAlasdair G Kergon 	unsigned target_bio_nr;
114506a426ceSMike Snitzer 
114655a62eefSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
114714fe594dSAlasdair G Kergon 		__clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
114806a426ceSMike Snitzer }
114906a426ceSMike Snitzer 
115014fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1151f9ab94ceSMikulas Patocka {
115206a426ceSMike Snitzer 	unsigned target_nr = 0;
1153f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1154f9ab94ceSMikulas Patocka 
1155b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1156f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
115714fe594dSAlasdair G Kergon 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, 0);
1158f9ab94ceSMikulas Patocka 
1159f9ab94ceSMikulas Patocka 	return 0;
1160f9ab94ceSMikulas Patocka }
1161f9ab94ceSMikulas Patocka 
1162e4c93811SAlasdair G Kergon static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
1163e4c93811SAlasdair G Kergon 				     sector_t sector, int nr_iovecs,
1164e4c93811SAlasdair G Kergon 				     unsigned short idx, unsigned short bv_count,
1165e4c93811SAlasdair G Kergon 				     unsigned offset, unsigned len,
1166e4c93811SAlasdair G Kergon 				     unsigned split_bvec)
11675ae89a87SMike Snitzer {
1168dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
11695ae89a87SMike Snitzer 	struct dm_target_io *tio;
11705ae89a87SMike Snitzer 
1171e4c93811SAlasdair G Kergon 	tio = alloc_tio(ci, ti, nr_iovecs, 0);
1172e4c93811SAlasdair G Kergon 
1173e4c93811SAlasdair G Kergon 	if (split_bvec)
1174e4c93811SAlasdair G Kergon 		clone_split_bio(tio, bio, sector, idx, offset, len);
1175e4c93811SAlasdair G Kergon 	else
1176e4c93811SAlasdair G Kergon 		clone_bio(tio, bio, sector, idx, bv_count, len);
1177e4c93811SAlasdair G Kergon 
1178bd2a49b8SAlasdair G Kergon 	__map_bio(tio);
11795ae89a87SMike Snitzer }
11805ae89a87SMike Snitzer 
118155a62eefSAlasdair G Kergon typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
118223508a96SMike Snitzer 
118355a62eefSAlasdair G Kergon static unsigned get_num_discard_bios(struct dm_target *ti)
118423508a96SMike Snitzer {
118555a62eefSAlasdair G Kergon 	return ti->num_discard_bios;
118623508a96SMike Snitzer }
118723508a96SMike Snitzer 
118855a62eefSAlasdair G Kergon static unsigned get_num_write_same_bios(struct dm_target *ti)
118923508a96SMike Snitzer {
119055a62eefSAlasdair G Kergon 	return ti->num_write_same_bios;
119123508a96SMike Snitzer }
119223508a96SMike Snitzer 
119323508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti);
119423508a96SMike Snitzer 
119523508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti)
119623508a96SMike Snitzer {
119755a62eefSAlasdair G Kergon 	return ti->split_discard_bios;
119823508a96SMike Snitzer }
119923508a96SMike Snitzer 
120014fe594dSAlasdair G Kergon static int __send_changing_extent_only(struct clone_info *ci,
120155a62eefSAlasdair G Kergon 				       get_num_bios_fn get_num_bios,
120223508a96SMike Snitzer 				       is_split_required_fn is_split_required)
12035ae89a87SMike Snitzer {
12045ae89a87SMike Snitzer 	struct dm_target *ti;
1205a79245b3SMike Snitzer 	sector_t len;
120655a62eefSAlasdair G Kergon 	unsigned num_bios;
12075ae89a87SMike Snitzer 
1208a79245b3SMike Snitzer 	do {
12095ae89a87SMike Snitzer 		ti = dm_table_find_target(ci->map, ci->sector);
12105ae89a87SMike Snitzer 		if (!dm_target_is_valid(ti))
12115ae89a87SMike Snitzer 			return -EIO;
12125ae89a87SMike Snitzer 
12135ae89a87SMike Snitzer 		/*
121423508a96SMike Snitzer 		 * Even though the device advertised support for this type of
121523508a96SMike Snitzer 		 * request, that does not mean every target supports it, and
1216936688d7SMike Snitzer 		 * reconfiguration might also have changed that since the
12175ae89a87SMike Snitzer 		 * check was performed.
12185ae89a87SMike Snitzer 		 */
121955a62eefSAlasdair G Kergon 		num_bios = get_num_bios ? get_num_bios(ti) : 0;
122055a62eefSAlasdair G Kergon 		if (!num_bios)
12215ae89a87SMike Snitzer 			return -EOPNOTSUPP;
12225ae89a87SMike Snitzer 
122323508a96SMike Snitzer 		if (is_split_required && !is_split_required(ti))
1224a79245b3SMike Snitzer 			len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
12257acf0277SMikulas Patocka 		else
12267acf0277SMikulas Patocka 			len = min(ci->sector_count, max_io_len(ci->sector, ti));
12275ae89a87SMike Snitzer 
122814fe594dSAlasdair G Kergon 		__send_duplicate_bios(ci, ti, num_bios, len);
12295ae89a87SMike Snitzer 
1230a79245b3SMike Snitzer 		ci->sector += len;
1231a79245b3SMike Snitzer 	} while (ci->sector_count -= len);
12325ae89a87SMike Snitzer 
12335ae89a87SMike Snitzer 	return 0;
12345ae89a87SMike Snitzer }
12355ae89a87SMike Snitzer 
123614fe594dSAlasdair G Kergon static int __send_discard(struct clone_info *ci)
123723508a96SMike Snitzer {
123814fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_discard_bios,
123923508a96SMike Snitzer 					   is_split_required_for_discard);
124023508a96SMike Snitzer }
124123508a96SMike Snitzer 
124214fe594dSAlasdair G Kergon static int __send_write_same(struct clone_info *ci)
124323508a96SMike Snitzer {
124414fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
124523508a96SMike Snitzer }
124623508a96SMike Snitzer 
1247e4c93811SAlasdair G Kergon /*
1248e4c93811SAlasdair G Kergon  * Find maximum number of sectors / bvecs we can process with a single bio.
1249e4c93811SAlasdair G Kergon  */
1250e4c93811SAlasdair G Kergon static sector_t __len_within_target(struct clone_info *ci, sector_t max, int *idx)
12511da177e4SLinus Torvalds {
1252dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
1253e4c93811SAlasdair G Kergon 	sector_t bv_len, total_len = 0;
12541da177e4SLinus Torvalds 
1255e4c93811SAlasdair G Kergon 	for (*idx = ci->idx; max && (*idx < bio->bi_vcnt); (*idx)++) {
1256e4c93811SAlasdair G Kergon 		bv_len = to_sector(bio->bi_io_vec[*idx].bv_len);
12575ae89a87SMike Snitzer 
1258e4c93811SAlasdair G Kergon 		if (bv_len > max)
12591da177e4SLinus Torvalds 			break;
12601da177e4SLinus Torvalds 
1261e4c93811SAlasdair G Kergon 		max -= bv_len;
1262e4c93811SAlasdair G Kergon 		total_len += bv_len;
12631da177e4SLinus Torvalds 	}
12641da177e4SLinus Torvalds 
1265e4c93811SAlasdair G Kergon 	return total_len;
1266e4c93811SAlasdair G Kergon }
12671da177e4SLinus Torvalds 
1268e4c93811SAlasdair G Kergon static int __split_bvec_across_targets(struct clone_info *ci,
1269e4c93811SAlasdair G Kergon 				       struct dm_target *ti, sector_t max)
1270e4c93811SAlasdair G Kergon {
1271e4c93811SAlasdair G Kergon 	struct bio *bio = ci->bio;
12721da177e4SLinus Torvalds 	struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1273d2044a94SAlasdair G Kergon 	sector_t remaining = to_sector(bv->bv_len);
1274e4c93811SAlasdair G Kergon 	unsigned offset = 0;
1275e4c93811SAlasdair G Kergon 	sector_t len;
12761da177e4SLinus Torvalds 
1277d2044a94SAlasdair G Kergon 	do {
1278d2044a94SAlasdair G Kergon 		if (offset) {
12791da177e4SLinus Torvalds 			ti = dm_table_find_target(ci->map, ci->sector);
1280512875bdSJun'ichi Nomura 			if (!dm_target_is_valid(ti))
1281512875bdSJun'ichi Nomura 				return -EIO;
1282512875bdSJun'ichi Nomura 
128356a67df7SMike Snitzer 			max = max_io_len(ci->sector, ti);
1284d2044a94SAlasdair G Kergon 		}
1285d2044a94SAlasdair G Kergon 
1286d2044a94SAlasdair G Kergon 		len = min(remaining, max);
1287d2044a94SAlasdair G Kergon 
1288e4c93811SAlasdair G Kergon 		__clone_and_map_data_bio(ci, ti, ci->sector, 1, ci->idx, 0,
1289e4c93811SAlasdair G Kergon 					 bv->bv_offset + offset, len, 1);
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 		ci->sector += len;
12921da177e4SLinus Torvalds 		ci->sector_count -= len;
1293d2044a94SAlasdair G Kergon 		offset += to_bytes(len);
1294d2044a94SAlasdair G Kergon 	} while (remaining -= len);
1295d2044a94SAlasdair G Kergon 
12961da177e4SLinus Torvalds 	ci->idx++;
1297512875bdSJun'ichi Nomura 
1298512875bdSJun'ichi Nomura 	return 0;
12991da177e4SLinus Torvalds }
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds /*
1302e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1303e4c93811SAlasdair G Kergon  */
1304e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci)
1305e4c93811SAlasdair G Kergon {
1306e4c93811SAlasdair G Kergon 	struct bio *bio = ci->bio;
1307e4c93811SAlasdair G Kergon 	struct dm_target *ti;
1308e4c93811SAlasdair G Kergon 	sector_t len, max;
1309e4c93811SAlasdair G Kergon 	int idx;
1310e4c93811SAlasdair G Kergon 
1311e4c93811SAlasdair G Kergon 	if (unlikely(bio->bi_rw & REQ_DISCARD))
1312e4c93811SAlasdair G Kergon 		return __send_discard(ci);
1313e4c93811SAlasdair G Kergon 	else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
1314e4c93811SAlasdair G Kergon 		return __send_write_same(ci);
1315e4c93811SAlasdair G Kergon 
1316e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1317e4c93811SAlasdair G Kergon 	if (!dm_target_is_valid(ti))
1318e4c93811SAlasdair G Kergon 		return -EIO;
1319e4c93811SAlasdair G Kergon 
1320e4c93811SAlasdair G Kergon 	max = max_io_len(ci->sector, ti);
1321e4c93811SAlasdair G Kergon 
1322e4c93811SAlasdair G Kergon 	/*
1323e4c93811SAlasdair G Kergon 	 * Optimise for the simple case where we can do all of
1324e4c93811SAlasdair G Kergon 	 * the remaining io with a single clone.
1325e4c93811SAlasdair G Kergon 	 */
1326e4c93811SAlasdair G Kergon 	if (ci->sector_count <= max) {
1327e4c93811SAlasdair G Kergon 		__clone_and_map_data_bio(ci, ti, ci->sector, bio->bi_max_vecs,
1328e4c93811SAlasdair G Kergon 					 ci->idx, bio->bi_vcnt - ci->idx, 0,
1329e4c93811SAlasdair G Kergon 					 ci->sector_count, 0);
1330e4c93811SAlasdair G Kergon 		ci->sector_count = 0;
1331e4c93811SAlasdair G Kergon 		return 0;
1332e4c93811SAlasdair G Kergon 	}
1333e4c93811SAlasdair G Kergon 
1334e4c93811SAlasdair G Kergon 	/*
1335e4c93811SAlasdair G Kergon 	 * There are some bvecs that don't span targets.
1336e4c93811SAlasdair G Kergon 	 * Do as many of these as possible.
1337e4c93811SAlasdair G Kergon 	 */
1338e4c93811SAlasdair G Kergon 	if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1339e4c93811SAlasdair G Kergon 		len = __len_within_target(ci, max, &idx);
1340e4c93811SAlasdair G Kergon 
1341e4c93811SAlasdair G Kergon 		__clone_and_map_data_bio(ci, ti, ci->sector, bio->bi_max_vecs,
1342e4c93811SAlasdair G Kergon 					 ci->idx, idx - ci->idx, 0, len, 0);
1343e4c93811SAlasdair G Kergon 
1344e4c93811SAlasdair G Kergon 		ci->sector += len;
1345e4c93811SAlasdair G Kergon 		ci->sector_count -= len;
1346e4c93811SAlasdair G Kergon 		ci->idx = idx;
1347e4c93811SAlasdair G Kergon 
1348e4c93811SAlasdair G Kergon 		return 0;
1349e4c93811SAlasdair G Kergon 	}
1350e4c93811SAlasdair G Kergon 
1351e4c93811SAlasdair G Kergon 	/*
1352e4c93811SAlasdair G Kergon 	 * Handle a bvec that must be split between two or more targets.
1353e4c93811SAlasdair G Kergon 	 */
1354e4c93811SAlasdair G Kergon 	return __split_bvec_across_targets(ci, ti, max);
1355e4c93811SAlasdair G Kergon }
1356e4c93811SAlasdair G Kergon 
1357e4c93811SAlasdair G Kergon /*
135814fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
13591da177e4SLinus Torvalds  */
1360f0b9a450SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
13611da177e4SLinus Torvalds {
13621da177e4SLinus Torvalds 	struct clone_info ci;
1363512875bdSJun'ichi Nomura 	int error = 0;
13641da177e4SLinus Torvalds 
13657c666411SAlasdair G Kergon 	ci.map = dm_get_live_table(md);
1366f0b9a450SMikulas Patocka 	if (unlikely(!ci.map)) {
1367f0b9a450SMikulas Patocka 		bio_io_error(bio);
1368f0b9a450SMikulas Patocka 		return;
1369f0b9a450SMikulas Patocka 	}
1370692d0eb9SMikulas Patocka 
13711da177e4SLinus Torvalds 	ci.md = md;
13721da177e4SLinus Torvalds 	ci.io = alloc_io(md);
13731da177e4SLinus Torvalds 	ci.io->error = 0;
13741da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
13751da177e4SLinus Torvalds 	ci.io->bio = bio;
13761da177e4SLinus Torvalds 	ci.io->md = md;
1377f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
13781da177e4SLinus Torvalds 	ci.sector = bio->bi_sector;
13791da177e4SLinus Torvalds 	ci.idx = bio->bi_idx;
13801da177e4SLinus Torvalds 
13813eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1382bd2a49b8SAlasdair G Kergon 
1383b372d360SMike Snitzer 	if (bio->bi_rw & REQ_FLUSH) {
1384b372d360SMike Snitzer 		ci.bio = &ci.md->flush_bio;
1385b372d360SMike Snitzer 		ci.sector_count = 0;
138614fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1387b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1388b372d360SMike Snitzer 	} else {
13896a8736d1STejun Heo 		ci.bio = bio;
1390f6fccb12SMilan Broz 		ci.sector_count = bio_sectors(bio);
1391512875bdSJun'ichi Nomura 		while (ci.sector_count && !error)
139214fe594dSAlasdair G Kergon 			error = __split_and_process_non_flush(&ci);
1393d87f4c14STejun Heo 	}
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds 	/* drop the extra reference count */
1396512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
13971da177e4SLinus Torvalds 	dm_table_put(ci.map);
13989e4e5f87SMilan Broz }
13999e4e5f87SMilan Broz /*-----------------------------------------------------------------
14001da177e4SLinus Torvalds  * CRUD END
14011da177e4SLinus Torvalds  *---------------------------------------------------------------*/
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds static int dm_merge_bvec(struct request_queue *q,
14041da177e4SLinus Torvalds 			 struct bvec_merge_data *bvm,
1405f6fccb12SMilan Broz 			 struct bio_vec *biovec)
1406f6fccb12SMilan Broz {
1407f6fccb12SMilan Broz 	struct mapped_device *md = q->queuedata;
14087c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1409f6fccb12SMilan Broz 	struct dm_target *ti;
1410f6fccb12SMilan Broz 	sector_t max_sectors;
1411f6fccb12SMilan Broz 	int max_size = 0;
1412f6fccb12SMilan Broz 
1413f6fccb12SMilan Broz 	if (unlikely(!map))
1414f6fccb12SMilan Broz 		goto out;
1415f6fccb12SMilan Broz 
1416f6fccb12SMilan Broz 	ti = dm_table_find_target(map, bvm->bi_sector);
1417f6fccb12SMilan Broz 	if (!dm_target_is_valid(ti))
1418f6fccb12SMilan Broz 		goto out_table;
1419f6fccb12SMilan Broz 
1420f6fccb12SMilan Broz 	/*
1421f6fccb12SMilan Broz 	 * Find maximum amount of I/O that won't need splitting
1422f6fccb12SMilan Broz 	 */
142356a67df7SMike Snitzer 	max_sectors = min(max_io_len(bvm->bi_sector, ti),
1424f6fccb12SMilan Broz 			  (sector_t) BIO_MAX_SECTORS);
1425f6fccb12SMilan Broz 	max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1426f6fccb12SMilan Broz 	if (max_size < 0)
1427f6fccb12SMilan Broz 		max_size = 0;
1428f6fccb12SMilan Broz 
1429f6fccb12SMilan Broz 	/*
1430f6fccb12SMilan Broz 	 * merge_bvec_fn() returns number of bytes
1431f6fccb12SMilan Broz 	 * it can accept at this offset
1432f6fccb12SMilan Broz 	 * max is precomputed maximal io size
1433f6fccb12SMilan Broz 	 */
1434f6fccb12SMilan Broz 	if (max_size && ti->type->merge)
1435f6fccb12SMilan Broz 		max_size = ti->type->merge(ti, bvm, biovec, max_size);
14368cbeb67aSMikulas Patocka 	/*
14378cbeb67aSMikulas Patocka 	 * If the target doesn't support merge method and some of the devices
14388cbeb67aSMikulas Patocka 	 * provided their merge_bvec method (we know this by looking at
14398cbeb67aSMikulas Patocka 	 * queue_max_hw_sectors), then we can't allow bios with multiple vector
14408cbeb67aSMikulas Patocka 	 * entries.  So always set max_size to 0, and the code below allows
14418cbeb67aSMikulas Patocka 	 * just one page.
14428cbeb67aSMikulas Patocka 	 */
14438cbeb67aSMikulas Patocka 	else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
14448cbeb67aSMikulas Patocka 
14458cbeb67aSMikulas Patocka 		max_size = 0;
1446f6fccb12SMilan Broz 
1447b01cd5acSMikulas Patocka out_table:
14485037108aSMikulas Patocka 	dm_table_put(map);
14495037108aSMikulas Patocka 
14505037108aSMikulas Patocka out:
1451f6fccb12SMilan Broz 	/*
1452f6fccb12SMilan Broz 	 * Always allow an entire first page
1453f6fccb12SMilan Broz 	 */
1454f6fccb12SMilan Broz 	if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1455f6fccb12SMilan Broz 		max_size = biovec->bv_len;
1456f6fccb12SMilan Broz 
1457f6fccb12SMilan Broz 	return max_size;
1458f6fccb12SMilan Broz }
1459f6fccb12SMilan Broz 
14601da177e4SLinus Torvalds /*
14611da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
14621da177e4SLinus Torvalds  * dm_merge_bvec.
14631da177e4SLinus Torvalds  */
14645a7bbad2SChristoph Hellwig static void _dm_request(struct request_queue *q, struct bio *bio)
14651da177e4SLinus Torvalds {
146612f03a49SKevin Corry 	int rw = bio_data_dir(bio);
14671da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
1468c9959059STejun Heo 	int cpu;
14691da177e4SLinus Torvalds 
14702ca3310eSAlasdair G Kergon 	down_read(&md->io_lock);
14711da177e4SLinus Torvalds 
1472074a7acaSTejun Heo 	cpu = part_stat_lock();
1473074a7acaSTejun Heo 	part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1474074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1475074a7acaSTejun Heo 	part_stat_unlock();
147612f03a49SKevin Corry 
14776a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
14786a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
14792ca3310eSAlasdair G Kergon 		up_read(&md->io_lock);
14801da177e4SLinus Torvalds 
14816a8736d1STejun Heo 		if (bio_rw(bio) != READA)
148292c63902SMikulas Patocka 			queue_io(md, bio);
14836a8736d1STejun Heo 		else
14846a8736d1STejun Heo 			bio_io_error(bio);
14855a7bbad2SChristoph Hellwig 		return;
14861da177e4SLinus Torvalds 	}
14871da177e4SLinus Torvalds 
1488f0b9a450SMikulas Patocka 	__split_and_process_bio(md, bio);
14892ca3310eSAlasdair G Kergon 	up_read(&md->io_lock);
14905a7bbad2SChristoph Hellwig 	return;
1491cec47e3dSKiyoshi Ueda }
1492cec47e3dSKiyoshi Ueda 
1493cec47e3dSKiyoshi Ueda static int dm_request_based(struct mapped_device *md)
1494cec47e3dSKiyoshi Ueda {
1495cec47e3dSKiyoshi Ueda 	return blk_queue_stackable(md->queue);
1496cec47e3dSKiyoshi Ueda }
1497cec47e3dSKiyoshi Ueda 
14985a7bbad2SChristoph Hellwig static void dm_request(struct request_queue *q, struct bio *bio)
1499cec47e3dSKiyoshi Ueda {
1500cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1501cec47e3dSKiyoshi Ueda 
1502cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
15035a7bbad2SChristoph Hellwig 		blk_queue_bio(q, bio);
15045a7bbad2SChristoph Hellwig 	else
15055a7bbad2SChristoph Hellwig 		_dm_request(q, bio);
1506cec47e3dSKiyoshi Ueda }
1507cec47e3dSKiyoshi Ueda 
1508cec47e3dSKiyoshi Ueda void dm_dispatch_request(struct request *rq)
1509cec47e3dSKiyoshi Ueda {
1510cec47e3dSKiyoshi Ueda 	int r;
1511cec47e3dSKiyoshi Ueda 
1512cec47e3dSKiyoshi Ueda 	if (blk_queue_io_stat(rq->q))
1513cec47e3dSKiyoshi Ueda 		rq->cmd_flags |= REQ_IO_STAT;
1514cec47e3dSKiyoshi Ueda 
1515cec47e3dSKiyoshi Ueda 	rq->start_time = jiffies;
1516cec47e3dSKiyoshi Ueda 	r = blk_insert_cloned_request(rq->q, rq);
1517cec47e3dSKiyoshi Ueda 	if (r)
1518cec47e3dSKiyoshi Ueda 		dm_complete_request(rq, r);
1519cec47e3dSKiyoshi Ueda }
1520cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_dispatch_request);
1521cec47e3dSKiyoshi Ueda 
1522cec47e3dSKiyoshi Ueda static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1523cec47e3dSKiyoshi Ueda 				 void *data)
1524cec47e3dSKiyoshi Ueda {
1525cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = data;
152694818742SKent Overstreet 	struct dm_rq_clone_bio_info *info =
152794818742SKent Overstreet 		container_of(bio, struct dm_rq_clone_bio_info, clone);
1528cec47e3dSKiyoshi Ueda 
1529cec47e3dSKiyoshi Ueda 	info->orig = bio_orig;
1530cec47e3dSKiyoshi Ueda 	info->tio = tio;
1531cec47e3dSKiyoshi Ueda 	bio->bi_end_io = end_clone_bio;
1532cec47e3dSKiyoshi Ueda 	bio->bi_private = info;
1533cec47e3dSKiyoshi Ueda 
1534cec47e3dSKiyoshi Ueda 	return 0;
1535cec47e3dSKiyoshi Ueda }
1536cec47e3dSKiyoshi Ueda 
1537cec47e3dSKiyoshi Ueda static int setup_clone(struct request *clone, struct request *rq,
1538cec47e3dSKiyoshi Ueda 		       struct dm_rq_target_io *tio)
1539cec47e3dSKiyoshi Ueda {
1540d0bcb878SKiyoshi Ueda 	int r;
1541cec47e3dSKiyoshi Ueda 
1542d0bcb878SKiyoshi Ueda 	r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1543d0bcb878SKiyoshi Ueda 			      dm_rq_bio_constructor, tio);
1544cec47e3dSKiyoshi Ueda 	if (r)
1545cec47e3dSKiyoshi Ueda 		return r;
1546cec47e3dSKiyoshi Ueda 
1547cec47e3dSKiyoshi Ueda 	clone->cmd = rq->cmd;
1548cec47e3dSKiyoshi Ueda 	clone->cmd_len = rq->cmd_len;
1549cec47e3dSKiyoshi Ueda 	clone->sense = rq->sense;
1550cec47e3dSKiyoshi Ueda 	clone->buffer = rq->buffer;
1551cec47e3dSKiyoshi Ueda 	clone->end_io = end_clone_request;
1552cec47e3dSKiyoshi Ueda 	clone->end_io_data = tio;
1553cec47e3dSKiyoshi Ueda 
1554cec47e3dSKiyoshi Ueda 	return 0;
1555cec47e3dSKiyoshi Ueda }
1556cec47e3dSKiyoshi Ueda 
15576facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md,
15586facdaffSKiyoshi Ueda 				gfp_t gfp_mask)
15596facdaffSKiyoshi Ueda {
15606facdaffSKiyoshi Ueda 	struct request *clone;
15616facdaffSKiyoshi Ueda 	struct dm_rq_target_io *tio;
15626facdaffSKiyoshi Ueda 
15636facdaffSKiyoshi Ueda 	tio = alloc_rq_tio(md, gfp_mask);
15646facdaffSKiyoshi Ueda 	if (!tio)
15656facdaffSKiyoshi Ueda 		return NULL;
15666facdaffSKiyoshi Ueda 
15676facdaffSKiyoshi Ueda 	tio->md = md;
15686facdaffSKiyoshi Ueda 	tio->ti = NULL;
15696facdaffSKiyoshi Ueda 	tio->orig = rq;
15706facdaffSKiyoshi Ueda 	tio->error = 0;
15716facdaffSKiyoshi Ueda 	memset(&tio->info, 0, sizeof(tio->info));
15726facdaffSKiyoshi Ueda 
15736facdaffSKiyoshi Ueda 	clone = &tio->clone;
15746facdaffSKiyoshi Ueda 	if (setup_clone(clone, rq, tio)) {
15756facdaffSKiyoshi Ueda 		/* -ENOMEM */
15766facdaffSKiyoshi Ueda 		free_rq_tio(tio);
15776facdaffSKiyoshi Ueda 		return NULL;
15786facdaffSKiyoshi Ueda 	}
15796facdaffSKiyoshi Ueda 
15806facdaffSKiyoshi Ueda 	return clone;
15816facdaffSKiyoshi Ueda }
15826facdaffSKiyoshi Ueda 
1583cec47e3dSKiyoshi Ueda /*
1584cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1585cec47e3dSKiyoshi Ueda  */
1586cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq)
1587cec47e3dSKiyoshi Ueda {
1588cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1589cec47e3dSKiyoshi Ueda 	struct request *clone;
1590cec47e3dSKiyoshi Ueda 
1591cec47e3dSKiyoshi Ueda 	if (unlikely(rq->special)) {
1592cec47e3dSKiyoshi Ueda 		DMWARN("Already has something in rq->special.");
1593cec47e3dSKiyoshi Ueda 		return BLKPREP_KILL;
1594cec47e3dSKiyoshi Ueda 	}
1595cec47e3dSKiyoshi Ueda 
15966facdaffSKiyoshi Ueda 	clone = clone_rq(rq, md, GFP_ATOMIC);
15976facdaffSKiyoshi Ueda 	if (!clone)
1598cec47e3dSKiyoshi Ueda 		return BLKPREP_DEFER;
1599cec47e3dSKiyoshi Ueda 
1600cec47e3dSKiyoshi Ueda 	rq->special = clone;
1601cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_DONTPREP;
1602cec47e3dSKiyoshi Ueda 
1603cec47e3dSKiyoshi Ueda 	return BLKPREP_OK;
1604cec47e3dSKiyoshi Ueda }
1605cec47e3dSKiyoshi Ueda 
16069eef87daSKiyoshi Ueda /*
16079eef87daSKiyoshi Ueda  * Returns:
16089eef87daSKiyoshi Ueda  * 0  : the request has been processed (not requeued)
16099eef87daSKiyoshi Ueda  * !0 : the request has been requeued
16109eef87daSKiyoshi Ueda  */
16119eef87daSKiyoshi Ueda static int map_request(struct dm_target *ti, struct request *clone,
1612cec47e3dSKiyoshi Ueda 		       struct mapped_device *md)
1613cec47e3dSKiyoshi Ueda {
16149eef87daSKiyoshi Ueda 	int r, requeued = 0;
1615cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1616cec47e3dSKiyoshi Ueda 
1617cec47e3dSKiyoshi Ueda 	tio->ti = ti;
1618cec47e3dSKiyoshi Ueda 	r = ti->type->map_rq(ti, clone, &tio->info);
1619cec47e3dSKiyoshi Ueda 	switch (r) {
1620cec47e3dSKiyoshi Ueda 	case DM_MAPIO_SUBMITTED:
1621cec47e3dSKiyoshi Ueda 		/* The target has taken the I/O to submit by itself later */
1622cec47e3dSKiyoshi Ueda 		break;
1623cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REMAPPED:
1624cec47e3dSKiyoshi Ueda 		/* The target has remapped the I/O so dispatch it */
16256db4ccd6SJun'ichi Nomura 		trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
16266db4ccd6SJun'ichi Nomura 				     blk_rq_pos(tio->orig));
1627cec47e3dSKiyoshi Ueda 		dm_dispatch_request(clone);
1628cec47e3dSKiyoshi Ueda 		break;
1629cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REQUEUE:
1630cec47e3dSKiyoshi Ueda 		/* The target wants to requeue the I/O */
1631cec47e3dSKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
16329eef87daSKiyoshi Ueda 		requeued = 1;
1633cec47e3dSKiyoshi Ueda 		break;
1634cec47e3dSKiyoshi Ueda 	default:
1635cec47e3dSKiyoshi Ueda 		if (r > 0) {
1636cec47e3dSKiyoshi Ueda 			DMWARN("unimplemented target map return value: %d", r);
1637cec47e3dSKiyoshi Ueda 			BUG();
1638cec47e3dSKiyoshi Ueda 		}
1639cec47e3dSKiyoshi Ueda 
1640cec47e3dSKiyoshi Ueda 		/* The target wants to complete the I/O */
1641cec47e3dSKiyoshi Ueda 		dm_kill_unmapped_request(clone, r);
1642cec47e3dSKiyoshi Ueda 		break;
1643cec47e3dSKiyoshi Ueda 	}
16449eef87daSKiyoshi Ueda 
16459eef87daSKiyoshi Ueda 	return requeued;
1646cec47e3dSKiyoshi Ueda }
1647cec47e3dSKiyoshi Ueda 
1648ba1cbad9SMike Snitzer static struct request *dm_start_request(struct mapped_device *md, struct request *orig)
1649ba1cbad9SMike Snitzer {
1650ba1cbad9SMike Snitzer 	struct request *clone;
1651ba1cbad9SMike Snitzer 
1652ba1cbad9SMike Snitzer 	blk_start_request(orig);
1653ba1cbad9SMike Snitzer 	clone = orig->special;
1654ba1cbad9SMike Snitzer 	atomic_inc(&md->pending[rq_data_dir(clone)]);
1655ba1cbad9SMike Snitzer 
1656ba1cbad9SMike Snitzer 	/*
1657ba1cbad9SMike Snitzer 	 * Hold the md reference here for the in-flight I/O.
1658ba1cbad9SMike Snitzer 	 * We can't rely on the reference count by device opener,
1659ba1cbad9SMike Snitzer 	 * because the device may be closed during the request completion
1660ba1cbad9SMike Snitzer 	 * when all bios are completed.
1661ba1cbad9SMike Snitzer 	 * See the comment in rq_completed() too.
1662ba1cbad9SMike Snitzer 	 */
1663ba1cbad9SMike Snitzer 	dm_get(md);
1664ba1cbad9SMike Snitzer 
1665ba1cbad9SMike Snitzer 	return clone;
1666ba1cbad9SMike Snitzer }
1667ba1cbad9SMike Snitzer 
1668cec47e3dSKiyoshi Ueda /*
1669cec47e3dSKiyoshi Ueda  * q->request_fn for request-based dm.
1670cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1671cec47e3dSKiyoshi Ueda  */
1672cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q)
1673cec47e3dSKiyoshi Ueda {
1674cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
16757c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1676cec47e3dSKiyoshi Ueda 	struct dm_target *ti;
1677b4324feeSKiyoshi Ueda 	struct request *rq, *clone;
167829e4013dSTejun Heo 	sector_t pos;
1679cec47e3dSKiyoshi Ueda 
1680cec47e3dSKiyoshi Ueda 	/*
1681b4324feeSKiyoshi Ueda 	 * For suspend, check blk_queue_stopped() and increment
1682b4324feeSKiyoshi Ueda 	 * ->pending within a single queue_lock not to increment the
1683b4324feeSKiyoshi Ueda 	 * number of in-flight I/Os after the queue is stopped in
1684b4324feeSKiyoshi Ueda 	 * dm_suspend().
1685cec47e3dSKiyoshi Ueda 	 */
16867eaceaccSJens Axboe 	while (!blk_queue_stopped(q)) {
1687cec47e3dSKiyoshi Ueda 		rq = blk_peek_request(q);
1688cec47e3dSKiyoshi Ueda 		if (!rq)
16897eaceaccSJens Axboe 			goto delay_and_out;
1690cec47e3dSKiyoshi Ueda 
169129e4013dSTejun Heo 		/* always use block 0 to find the target for flushes for now */
169229e4013dSTejun Heo 		pos = 0;
169329e4013dSTejun Heo 		if (!(rq->cmd_flags & REQ_FLUSH))
169429e4013dSTejun Heo 			pos = blk_rq_pos(rq);
1695d0bcb878SKiyoshi Ueda 
169629e4013dSTejun Heo 		ti = dm_table_find_target(map, pos);
1697ba1cbad9SMike Snitzer 		if (!dm_target_is_valid(ti)) {
1698ba1cbad9SMike Snitzer 			/*
1699ba1cbad9SMike Snitzer 			 * Must perform setup, that dm_done() requires,
1700ba1cbad9SMike Snitzer 			 * before calling dm_kill_unmapped_request
1701ba1cbad9SMike Snitzer 			 */
1702ba1cbad9SMike Snitzer 			DMERR_LIMIT("request attempted access beyond the end of device");
1703ba1cbad9SMike Snitzer 			clone = dm_start_request(md, rq);
1704ba1cbad9SMike Snitzer 			dm_kill_unmapped_request(clone, -EIO);
1705ba1cbad9SMike Snitzer 			continue;
1706ba1cbad9SMike Snitzer 		}
170729e4013dSTejun Heo 
1708cec47e3dSKiyoshi Ueda 		if (ti->type->busy && ti->type->busy(ti))
17097eaceaccSJens Axboe 			goto delay_and_out;
1710cec47e3dSKiyoshi Ueda 
1711ba1cbad9SMike Snitzer 		clone = dm_start_request(md, rq);
1712b4324feeSKiyoshi Ueda 
1713cec47e3dSKiyoshi Ueda 		spin_unlock(q->queue_lock);
17149eef87daSKiyoshi Ueda 		if (map_request(ti, clone, md))
17159eef87daSKiyoshi Ueda 			goto requeued;
17169eef87daSKiyoshi Ueda 
1717052189a2SKiyoshi Ueda 		BUG_ON(!irqs_disabled());
1718052189a2SKiyoshi Ueda 		spin_lock(q->queue_lock);
1719cec47e3dSKiyoshi Ueda 	}
1720cec47e3dSKiyoshi Ueda 
1721cec47e3dSKiyoshi Ueda 	goto out;
1722cec47e3dSKiyoshi Ueda 
17239eef87daSKiyoshi Ueda requeued:
1724052189a2SKiyoshi Ueda 	BUG_ON(!irqs_disabled());
1725052189a2SKiyoshi Ueda 	spin_lock(q->queue_lock);
17269eef87daSKiyoshi Ueda 
17277eaceaccSJens Axboe delay_and_out:
17287eaceaccSJens Axboe 	blk_delay_queue(q, HZ / 10);
1729cec47e3dSKiyoshi Ueda out:
1730cec47e3dSKiyoshi Ueda 	dm_table_put(map);
1731cec47e3dSKiyoshi Ueda }
1732cec47e3dSKiyoshi Ueda 
1733cec47e3dSKiyoshi Ueda int dm_underlying_device_busy(struct request_queue *q)
1734cec47e3dSKiyoshi Ueda {
1735cec47e3dSKiyoshi Ueda 	return blk_lld_busy(q);
1736cec47e3dSKiyoshi Ueda }
1737cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1738cec47e3dSKiyoshi Ueda 
1739cec47e3dSKiyoshi Ueda static int dm_lld_busy(struct request_queue *q)
1740cec47e3dSKiyoshi Ueda {
1741cec47e3dSKiyoshi Ueda 	int r;
1742cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
17437c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1744cec47e3dSKiyoshi Ueda 
1745cec47e3dSKiyoshi Ueda 	if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1746cec47e3dSKiyoshi Ueda 		r = 1;
1747cec47e3dSKiyoshi Ueda 	else
1748cec47e3dSKiyoshi Ueda 		r = dm_table_any_busy_target(map);
1749cec47e3dSKiyoshi Ueda 
1750cec47e3dSKiyoshi Ueda 	dm_table_put(map);
1751cec47e3dSKiyoshi Ueda 
1752cec47e3dSKiyoshi Ueda 	return r;
1753cec47e3dSKiyoshi Ueda }
1754cec47e3dSKiyoshi Ueda 
17551da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
17561da177e4SLinus Torvalds {
17578a57dfc6SChandra Seetharaman 	int r = bdi_bits;
17588a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
17598a57dfc6SChandra Seetharaman 	struct dm_table *map;
17601da177e4SLinus Torvalds 
17611eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
17627c666411SAlasdair G Kergon 		map = dm_get_live_table(md);
17638a57dfc6SChandra Seetharaman 		if (map) {
1764cec47e3dSKiyoshi Ueda 			/*
1765cec47e3dSKiyoshi Ueda 			 * Request-based dm cares about only own queue for
1766cec47e3dSKiyoshi Ueda 			 * the query about congestion status of request_queue
1767cec47e3dSKiyoshi Ueda 			 */
1768cec47e3dSKiyoshi Ueda 			if (dm_request_based(md))
1769cec47e3dSKiyoshi Ueda 				r = md->queue->backing_dev_info.state &
1770cec47e3dSKiyoshi Ueda 				    bdi_bits;
1771cec47e3dSKiyoshi Ueda 			else
17721da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
1773cec47e3dSKiyoshi Ueda 
17741da177e4SLinus Torvalds 			dm_table_put(map);
17758a57dfc6SChandra Seetharaman 		}
17768a57dfc6SChandra Seetharaman 	}
17778a57dfc6SChandra Seetharaman 
17781da177e4SLinus Torvalds 	return r;
17791da177e4SLinus Torvalds }
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds /*-----------------------------------------------------------------
17821da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
17831da177e4SLinus Torvalds  *---------------------------------------------------------------*/
17842b06cfffSAlasdair G Kergon static void free_minor(int minor)
17851da177e4SLinus Torvalds {
1786f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17871da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1788f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
17891da177e4SLinus Torvalds }
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds /*
17921da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
17931da177e4SLinus Torvalds  */
1794cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
17951da177e4SLinus Torvalds {
1796c9d76be6STejun Heo 	int r;
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
17991da177e4SLinus Torvalds 		return -EINVAL;
18001da177e4SLinus Torvalds 
1801c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1802f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
18031da177e4SLinus Torvalds 
1804c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
18051da177e4SLinus Torvalds 
1806f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1807c9d76be6STejun Heo 	idr_preload_end();
1808c9d76be6STejun Heo 	if (r < 0)
1809c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
1810c9d76be6STejun Heo 	return 0;
18111da177e4SLinus Torvalds }
18121da177e4SLinus Torvalds 
1813cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
18141da177e4SLinus Torvalds {
1815c9d76be6STejun Heo 	int r;
18161da177e4SLinus Torvalds 
1817c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1818f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
18191da177e4SLinus Torvalds 
1820c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
18211da177e4SLinus Torvalds 
1822f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1823c9d76be6STejun Heo 	idr_preload_end();
1824c9d76be6STejun Heo 	if (r < 0)
18251da177e4SLinus Torvalds 		return r;
1826c9d76be6STejun Heo 	*minor = r;
1827c9d76be6STejun Heo 	return 0;
18281da177e4SLinus Torvalds }
18291da177e4SLinus Torvalds 
183083d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
18311da177e4SLinus Torvalds 
183253d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
183353d5914fSMikulas Patocka 
18344a0b4ddfSMike Snitzer static void dm_init_md_queue(struct mapped_device *md)
18354a0b4ddfSMike Snitzer {
18364a0b4ddfSMike Snitzer 	/*
18374a0b4ddfSMike Snitzer 	 * Request-based dm devices cannot be stacked on top of bio-based dm
18384a0b4ddfSMike Snitzer 	 * devices.  The type of this dm device has not been decided yet.
18394a0b4ddfSMike Snitzer 	 * The type is decided at the first table loading time.
18404a0b4ddfSMike Snitzer 	 * To prevent problematic device stacking, clear the queue flag
18414a0b4ddfSMike Snitzer 	 * for request stacking support until then.
18424a0b4ddfSMike Snitzer 	 *
18434a0b4ddfSMike Snitzer 	 * This queue is new, so no concurrency on the queue_flags.
18444a0b4ddfSMike Snitzer 	 */
18454a0b4ddfSMike Snitzer 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
18464a0b4ddfSMike Snitzer 
18474a0b4ddfSMike Snitzer 	md->queue->queuedata = md;
18484a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
18494a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_data = md;
18504a0b4ddfSMike Snitzer 	blk_queue_make_request(md->queue, dm_request);
18514a0b4ddfSMike Snitzer 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
18524a0b4ddfSMike Snitzer 	blk_queue_merge_bvec(md->queue, dm_merge_bvec);
18534a0b4ddfSMike Snitzer }
18544a0b4ddfSMike Snitzer 
18551da177e4SLinus Torvalds /*
18561da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
18571da177e4SLinus Torvalds  */
18582b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
18591da177e4SLinus Torvalds {
18601da177e4SLinus Torvalds 	int r;
1861cf13ab8eSFrederik Deweerdt 	struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1862ba61fdd1SJeff Mahoney 	void *old_md;
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds 	if (!md) {
18651da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
18661da177e4SLinus Torvalds 		return NULL;
18671da177e4SLinus Torvalds 	}
18681da177e4SLinus Torvalds 
186910da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
18706ed7ade8SMilan Broz 		goto bad_module_get;
187110da4f79SJeff Mahoney 
18721da177e4SLinus Torvalds 	/* get a minor number for the dev */
18732b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1874cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
18752b06cfffSAlasdair G Kergon 	else
1876cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
18771da177e4SLinus Torvalds 	if (r < 0)
18786ed7ade8SMilan Broz 		goto bad_minor;
18791da177e4SLinus Torvalds 
1880a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
18812ca3310eSAlasdair G Kergon 	init_rwsem(&md->io_lock);
1882e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1883a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
1884022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
18851da177e4SLinus Torvalds 	rwlock_init(&md->map_lock);
18861da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
18875c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
18881da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
18897a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
18907a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
18917a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
18921da177e4SLinus Torvalds 
18934a0b4ddfSMike Snitzer 	md->queue = blk_alloc_queue(GFP_KERNEL);
18941da177e4SLinus Torvalds 	if (!md->queue)
18956ed7ade8SMilan Broz 		goto bad_queue;
18961da177e4SLinus Torvalds 
18974a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
18989faf400fSStefan Bader 
18991da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
19001da177e4SLinus Torvalds 	if (!md->disk)
19016ed7ade8SMilan Broz 		goto bad_disk;
19021da177e4SLinus Torvalds 
1903316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
1904316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
1905f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
190653d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1907f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
1908f0b04115SJeff Mahoney 
19091da177e4SLinus Torvalds 	md->disk->major = _major;
19101da177e4SLinus Torvalds 	md->disk->first_minor = minor;
19111da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
19121da177e4SLinus Torvalds 	md->disk->queue = md->queue;
19131da177e4SLinus Torvalds 	md->disk->private_data = md;
19141da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
19151da177e4SLinus Torvalds 	add_disk(md->disk);
19167e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
19171da177e4SLinus Torvalds 
19189c4376deSTejun Heo 	md->wq = alloc_workqueue("kdmflush",
19199c4376deSTejun Heo 				 WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
1920304f3f6aSMilan Broz 	if (!md->wq)
1921304f3f6aSMilan Broz 		goto bad_thread;
1922304f3f6aSMilan Broz 
192332a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
192432a926daSMikulas Patocka 	if (!md->bdev)
192532a926daSMikulas Patocka 		goto bad_bdev;
192632a926daSMikulas Patocka 
19276a8736d1STejun Heo 	bio_init(&md->flush_bio);
19286a8736d1STejun Heo 	md->flush_bio.bi_bdev = md->bdev;
19296a8736d1STejun Heo 	md->flush_bio.bi_rw = WRITE_FLUSH;
19306a8736d1STejun Heo 
1931ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1932f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1933ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1934f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1935ba61fdd1SJeff Mahoney 
1936ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1937ba61fdd1SJeff Mahoney 
19381da177e4SLinus Torvalds 	return md;
19391da177e4SLinus Torvalds 
194032a926daSMikulas Patocka bad_bdev:
194132a926daSMikulas Patocka 	destroy_workqueue(md->wq);
1942304f3f6aSMilan Broz bad_thread:
194303022c54SZdenek Kabelac 	del_gendisk(md->disk);
1944304f3f6aSMilan Broz 	put_disk(md->disk);
19456ed7ade8SMilan Broz bad_disk:
19461312f40eSAl Viro 	blk_cleanup_queue(md->queue);
19476ed7ade8SMilan Broz bad_queue:
19481da177e4SLinus Torvalds 	free_minor(minor);
19496ed7ade8SMilan Broz bad_minor:
195010da4f79SJeff Mahoney 	module_put(THIS_MODULE);
19516ed7ade8SMilan Broz bad_module_get:
19521da177e4SLinus Torvalds 	kfree(md);
19531da177e4SLinus Torvalds 	return NULL;
19541da177e4SLinus Torvalds }
19551da177e4SLinus Torvalds 
1956ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1957ae9da83fSJun'ichi Nomura 
19581da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
19591da177e4SLinus Torvalds {
1960f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
196163d94e48SJun'ichi Nomura 
1962ae9da83fSJun'ichi Nomura 	unlock_fs(md);
1963db8fef4fSMikulas Patocka 	bdput(md->bdev);
1964304f3f6aSMilan Broz 	destroy_workqueue(md->wq);
1965e6ee8c0bSKiyoshi Ueda 	if (md->tio_pool)
19661da177e4SLinus Torvalds 		mempool_destroy(md->tio_pool);
1967e6ee8c0bSKiyoshi Ueda 	if (md->io_pool)
19681da177e4SLinus Torvalds 		mempool_destroy(md->io_pool);
1969e6ee8c0bSKiyoshi Ueda 	if (md->bs)
19709faf400fSStefan Bader 		bioset_free(md->bs);
19719c47008dSMartin K. Petersen 	blk_integrity_unregister(md->disk);
19721da177e4SLinus Torvalds 	del_gendisk(md->disk);
197363d94e48SJun'ichi Nomura 	free_minor(minor);
1974fba9f90eSJeff Mahoney 
1975fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
1976fba9f90eSJeff Mahoney 	md->disk->private_data = NULL;
1977fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
1978fba9f90eSJeff Mahoney 
19791da177e4SLinus Torvalds 	put_disk(md->disk);
19801312f40eSAl Viro 	blk_cleanup_queue(md->queue);
198110da4f79SJeff Mahoney 	module_put(THIS_MODULE);
19821da177e4SLinus Torvalds 	kfree(md);
19831da177e4SLinus Torvalds }
19841da177e4SLinus Torvalds 
1985e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1986e6ee8c0bSKiyoshi Ueda {
1987c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
1988e6ee8c0bSKiyoshi Ueda 
198916245bdcSJun'ichi Nomura 	if (md->io_pool && md->bs) {
199016245bdcSJun'ichi Nomura 		/* The md already has necessary mempools. */
199116245bdcSJun'ichi Nomura 		if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
1992c0820cf5SMikulas Patocka 			/*
199316245bdcSJun'ichi Nomura 			 * Reload bioset because front_pad may have changed
199416245bdcSJun'ichi Nomura 			 * because a different table was loaded.
1995c0820cf5SMikulas Patocka 			 */
1996c0820cf5SMikulas Patocka 			bioset_free(md->bs);
1997c0820cf5SMikulas Patocka 			md->bs = p->bs;
1998c0820cf5SMikulas Patocka 			p->bs = NULL;
199916245bdcSJun'ichi Nomura 		} else if (dm_table_get_type(t) == DM_TYPE_REQUEST_BASED) {
200016245bdcSJun'ichi Nomura 			BUG_ON(!md->tio_pool);
200116245bdcSJun'ichi Nomura 			/*
200216245bdcSJun'ichi Nomura 			 * There's no need to reload with request-based dm
200316245bdcSJun'ichi Nomura 			 * because the size of front_pad doesn't change.
200416245bdcSJun'ichi Nomura 			 * Note for future: If you are to reload bioset,
200516245bdcSJun'ichi Nomura 			 * prep-ed requests in the queue may refer
200616245bdcSJun'ichi Nomura 			 * to bio from the old bioset, so you must walk
200716245bdcSJun'ichi Nomura 			 * through the queue to unprep.
200816245bdcSJun'ichi Nomura 			 */
200916245bdcSJun'ichi Nomura 		}
2010e6ee8c0bSKiyoshi Ueda 		goto out;
2011c0820cf5SMikulas Patocka 	}
2012e6ee8c0bSKiyoshi Ueda 
2013e6ee8c0bSKiyoshi Ueda 	BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
2014e6ee8c0bSKiyoshi Ueda 
2015e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
2016e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
2017e6ee8c0bSKiyoshi Ueda 	md->tio_pool = p->tio_pool;
2018e6ee8c0bSKiyoshi Ueda 	p->tio_pool = NULL;
2019e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
2020e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
2021e6ee8c0bSKiyoshi Ueda 
2022e6ee8c0bSKiyoshi Ueda out:
2023e6ee8c0bSKiyoshi Ueda 	/* mempool bind completed, now no need any mempools in the table */
2024e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
2025e6ee8c0bSKiyoshi Ueda }
2026e6ee8c0bSKiyoshi Ueda 
20271da177e4SLinus Torvalds /*
20281da177e4SLinus Torvalds  * Bind a table to the device.
20291da177e4SLinus Torvalds  */
20301da177e4SLinus Torvalds static void event_callback(void *context)
20311da177e4SLinus Torvalds {
20327a8c3d3bSMike Anderson 	unsigned long flags;
20337a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
20341da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
20351da177e4SLinus Torvalds 
20367a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
20377a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
20387a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
20397a8c3d3bSMike Anderson 
2040ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
20417a8c3d3bSMike Anderson 
20421da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
20431da177e4SLinus Torvalds 	wake_up(&md->eventq);
20441da177e4SLinus Torvalds }
20451da177e4SLinus Torvalds 
2046c217649bSMike Snitzer /*
2047c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
2048c217649bSMike Snitzer  */
20494e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
20501da177e4SLinus Torvalds {
20514e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
20521da177e4SLinus Torvalds 
2053db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
20541da177e4SLinus Torvalds }
20551da177e4SLinus Torvalds 
2056042d2a9bSAlasdair G Kergon /*
2057d5b9dd04SMikulas Patocka  * Return 1 if the queue has a compulsory merge_bvec_fn function.
2058d5b9dd04SMikulas Patocka  *
2059d5b9dd04SMikulas Patocka  * If this function returns 0, then the device is either a non-dm
2060d5b9dd04SMikulas Patocka  * device without a merge_bvec_fn, or it is a dm device that is
2061d5b9dd04SMikulas Patocka  * able to split any bios it receives that are too big.
2062d5b9dd04SMikulas Patocka  */
2063d5b9dd04SMikulas Patocka int dm_queue_merge_is_compulsory(struct request_queue *q)
2064d5b9dd04SMikulas Patocka {
2065d5b9dd04SMikulas Patocka 	struct mapped_device *dev_md;
2066d5b9dd04SMikulas Patocka 
2067d5b9dd04SMikulas Patocka 	if (!q->merge_bvec_fn)
2068d5b9dd04SMikulas Patocka 		return 0;
2069d5b9dd04SMikulas Patocka 
2070d5b9dd04SMikulas Patocka 	if (q->make_request_fn == dm_request) {
2071d5b9dd04SMikulas Patocka 		dev_md = q->queuedata;
2072d5b9dd04SMikulas Patocka 		if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2073d5b9dd04SMikulas Patocka 			return 0;
2074d5b9dd04SMikulas Patocka 	}
2075d5b9dd04SMikulas Patocka 
2076d5b9dd04SMikulas Patocka 	return 1;
2077d5b9dd04SMikulas Patocka }
2078d5b9dd04SMikulas Patocka 
2079d5b9dd04SMikulas Patocka static int dm_device_merge_is_compulsory(struct dm_target *ti,
2080d5b9dd04SMikulas Patocka 					 struct dm_dev *dev, sector_t start,
2081d5b9dd04SMikulas Patocka 					 sector_t len, void *data)
2082d5b9dd04SMikulas Patocka {
2083d5b9dd04SMikulas Patocka 	struct block_device *bdev = dev->bdev;
2084d5b9dd04SMikulas Patocka 	struct request_queue *q = bdev_get_queue(bdev);
2085d5b9dd04SMikulas Patocka 
2086d5b9dd04SMikulas Patocka 	return dm_queue_merge_is_compulsory(q);
2087d5b9dd04SMikulas Patocka }
2088d5b9dd04SMikulas Patocka 
2089d5b9dd04SMikulas Patocka /*
2090d5b9dd04SMikulas Patocka  * Return 1 if it is acceptable to ignore merge_bvec_fn based
2091d5b9dd04SMikulas Patocka  * on the properties of the underlying devices.
2092d5b9dd04SMikulas Patocka  */
2093d5b9dd04SMikulas Patocka static int dm_table_merge_is_optional(struct dm_table *table)
2094d5b9dd04SMikulas Patocka {
2095d5b9dd04SMikulas Patocka 	unsigned i = 0;
2096d5b9dd04SMikulas Patocka 	struct dm_target *ti;
2097d5b9dd04SMikulas Patocka 
2098d5b9dd04SMikulas Patocka 	while (i < dm_table_get_num_targets(table)) {
2099d5b9dd04SMikulas Patocka 		ti = dm_table_get_target(table, i++);
2100d5b9dd04SMikulas Patocka 
2101d5b9dd04SMikulas Patocka 		if (ti->type->iterate_devices &&
2102d5b9dd04SMikulas Patocka 		    ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2103d5b9dd04SMikulas Patocka 			return 0;
2104d5b9dd04SMikulas Patocka 	}
2105d5b9dd04SMikulas Patocka 
2106d5b9dd04SMikulas Patocka 	return 1;
2107d5b9dd04SMikulas Patocka }
2108d5b9dd04SMikulas Patocka 
2109d5b9dd04SMikulas Patocka /*
2110042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2111042d2a9bSAlasdair G Kergon  */
2112042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2113754c5fc7SMike Snitzer 			       struct queue_limits *limits)
21141da177e4SLinus Torvalds {
2115042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
2116165125e1SJens Axboe 	struct request_queue *q = md->queue;
21171da177e4SLinus Torvalds 	sector_t size;
2118523d9297SKiyoshi Ueda 	unsigned long flags;
2119d5b9dd04SMikulas Patocka 	int merge_is_optional;
21201da177e4SLinus Torvalds 
21211da177e4SLinus Torvalds 	size = dm_table_get_size(t);
21223ac51e74SDarrick J. Wong 
21233ac51e74SDarrick J. Wong 	/*
21243ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
21253ac51e74SDarrick J. Wong 	 */
21263ac51e74SDarrick J. Wong 	if (size != get_capacity(md->disk))
21273ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
21283ac51e74SDarrick J. Wong 
21294e90188bSAlasdair G Kergon 	__set_size(md, size);
21301da177e4SLinus Torvalds 
2131cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
21322ca3310eSAlasdair G Kergon 
2133e6ee8c0bSKiyoshi Ueda 	/*
2134e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
2135e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
2136e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
2137e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
2138e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
2139e6ee8c0bSKiyoshi Ueda 	 */
2140e6ee8c0bSKiyoshi Ueda 	if (dm_table_request_based(t) && !blk_queue_stopped(q))
2141e6ee8c0bSKiyoshi Ueda 		stop_queue(q);
2142e6ee8c0bSKiyoshi Ueda 
2143e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
2144e6ee8c0bSKiyoshi Ueda 
2145d5b9dd04SMikulas Patocka 	merge_is_optional = dm_table_merge_is_optional(t);
2146d5b9dd04SMikulas Patocka 
2147523d9297SKiyoshi Ueda 	write_lock_irqsave(&md->map_lock, flags);
2148042d2a9bSAlasdair G Kergon 	old_map = md->map;
21492ca3310eSAlasdair G Kergon 	md->map = t;
215036a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
215136a0456fSAlasdair G Kergon 
2152754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
2153d5b9dd04SMikulas Patocka 	if (merge_is_optional)
2154d5b9dd04SMikulas Patocka 		set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2155d5b9dd04SMikulas Patocka 	else
2156d5b9dd04SMikulas Patocka 		clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2157523d9297SKiyoshi Ueda 	write_unlock_irqrestore(&md->map_lock, flags);
21582ca3310eSAlasdair G Kergon 
2159042d2a9bSAlasdair G Kergon 	return old_map;
21601da177e4SLinus Torvalds }
21611da177e4SLinus Torvalds 
2162a7940155SAlasdair G Kergon /*
2163a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2164a7940155SAlasdair G Kergon  */
2165a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
21661da177e4SLinus Torvalds {
21671da177e4SLinus Torvalds 	struct dm_table *map = md->map;
2168523d9297SKiyoshi Ueda 	unsigned long flags;
21691da177e4SLinus Torvalds 
21701da177e4SLinus Torvalds 	if (!map)
2171a7940155SAlasdair G Kergon 		return NULL;
21721da177e4SLinus Torvalds 
21731da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
2174523d9297SKiyoshi Ueda 	write_lock_irqsave(&md->map_lock, flags);
21751da177e4SLinus Torvalds 	md->map = NULL;
2176523d9297SKiyoshi Ueda 	write_unlock_irqrestore(&md->map_lock, flags);
2177a7940155SAlasdair G Kergon 
2178a7940155SAlasdair G Kergon 	return map;
21791da177e4SLinus Torvalds }
21801da177e4SLinus Torvalds 
21811da177e4SLinus Torvalds /*
21821da177e4SLinus Torvalds  * Constructor for a new device.
21831da177e4SLinus Torvalds  */
21842b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
21851da177e4SLinus Torvalds {
21861da177e4SLinus Torvalds 	struct mapped_device *md;
21871da177e4SLinus Torvalds 
21882b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
21891da177e4SLinus Torvalds 	if (!md)
21901da177e4SLinus Torvalds 		return -ENXIO;
21911da177e4SLinus Torvalds 
2192784aae73SMilan Broz 	dm_sysfs_init(md);
2193784aae73SMilan Broz 
21941da177e4SLinus Torvalds 	*result = md;
21951da177e4SLinus Torvalds 	return 0;
21961da177e4SLinus Torvalds }
21971da177e4SLinus Torvalds 
2198a5664dadSMike Snitzer /*
2199a5664dadSMike Snitzer  * Functions to manage md->type.
2200a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2201a5664dadSMike Snitzer  */
2202a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2203a5664dadSMike Snitzer {
2204a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2205a5664dadSMike Snitzer }
2206a5664dadSMike Snitzer 
2207a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2208a5664dadSMike Snitzer {
2209a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2210a5664dadSMike Snitzer }
2211a5664dadSMike Snitzer 
2212a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type)
2213a5664dadSMike Snitzer {
2214a5664dadSMike Snitzer 	md->type = type;
2215a5664dadSMike Snitzer }
2216a5664dadSMike Snitzer 
2217a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md)
2218a5664dadSMike Snitzer {
2219a5664dadSMike Snitzer 	return md->type;
2220a5664dadSMike Snitzer }
2221a5664dadSMike Snitzer 
222236a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
222336a0456fSAlasdair G Kergon {
222436a0456fSAlasdair G Kergon 	return md->immutable_target_type;
222536a0456fSAlasdair G Kergon }
222636a0456fSAlasdair G Kergon 
22274a0b4ddfSMike Snitzer /*
22284a0b4ddfSMike Snitzer  * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
22294a0b4ddfSMike Snitzer  */
22304a0b4ddfSMike Snitzer static int dm_init_request_based_queue(struct mapped_device *md)
22314a0b4ddfSMike Snitzer {
22324a0b4ddfSMike Snitzer 	struct request_queue *q = NULL;
22334a0b4ddfSMike Snitzer 
22344a0b4ddfSMike Snitzer 	if (md->queue->elevator)
22354a0b4ddfSMike Snitzer 		return 1;
22364a0b4ddfSMike Snitzer 
22374a0b4ddfSMike Snitzer 	/* Fully initialize the queue */
22384a0b4ddfSMike Snitzer 	q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
22394a0b4ddfSMike Snitzer 	if (!q)
22404a0b4ddfSMike Snitzer 		return 0;
22414a0b4ddfSMike Snitzer 
22424a0b4ddfSMike Snitzer 	md->queue = q;
22434a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
22444a0b4ddfSMike Snitzer 	blk_queue_softirq_done(md->queue, dm_softirq_done);
22454a0b4ddfSMike Snitzer 	blk_queue_prep_rq(md->queue, dm_prep_fn);
22464a0b4ddfSMike Snitzer 	blk_queue_lld_busy(md->queue, dm_lld_busy);
22474a0b4ddfSMike Snitzer 
22484a0b4ddfSMike Snitzer 	elv_register_queue(md->queue);
22494a0b4ddfSMike Snitzer 
22504a0b4ddfSMike Snitzer 	return 1;
22514a0b4ddfSMike Snitzer }
22524a0b4ddfSMike Snitzer 
22534a0b4ddfSMike Snitzer /*
22544a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
22554a0b4ddfSMike Snitzer  */
22564a0b4ddfSMike Snitzer int dm_setup_md_queue(struct mapped_device *md)
22574a0b4ddfSMike Snitzer {
22584a0b4ddfSMike Snitzer 	if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
22594a0b4ddfSMike Snitzer 	    !dm_init_request_based_queue(md)) {
22604a0b4ddfSMike Snitzer 		DMWARN("Cannot initialize queue for request-based mapped device");
22614a0b4ddfSMike Snitzer 		return -EINVAL;
22624a0b4ddfSMike Snitzer 	}
22634a0b4ddfSMike Snitzer 
22644a0b4ddfSMike Snitzer 	return 0;
22654a0b4ddfSMike Snitzer }
22664a0b4ddfSMike Snitzer 
2267637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev)
22681da177e4SLinus Torvalds {
22691da177e4SLinus Torvalds 	struct mapped_device *md;
22701da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
22711da177e4SLinus Torvalds 
22721da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
22731da177e4SLinus Torvalds 		return NULL;
22741da177e4SLinus Torvalds 
2275f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
22761da177e4SLinus Torvalds 
22771da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
2278fba9f90eSJeff Mahoney 	if (md && (md == MINOR_ALLOCED ||
2279f331c029STejun Heo 		   (MINOR(disk_devt(dm_disk(md))) != minor) ||
2280abdc568bSKiyoshi Ueda 		   dm_deleting_md(md) ||
2281fba9f90eSJeff Mahoney 		   test_bit(DMF_FREEING, &md->flags))) {
2282637842cfSDavid Teigland 		md = NULL;
2283fba9f90eSJeff Mahoney 		goto out;
2284fba9f90eSJeff Mahoney 	}
22851da177e4SLinus Torvalds 
2286fba9f90eSJeff Mahoney out:
2287f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
22881da177e4SLinus Torvalds 
2289637842cfSDavid Teigland 	return md;
2290637842cfSDavid Teigland }
2291637842cfSDavid Teigland 
2292d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev)
2293d229a958SDavid Teigland {
2294d229a958SDavid Teigland 	struct mapped_device *md = dm_find_md(dev);
2295d229a958SDavid Teigland 
2296d229a958SDavid Teigland 	if (md)
2297d229a958SDavid Teigland 		dm_get(md);
2298d229a958SDavid Teigland 
2299d229a958SDavid Teigland 	return md;
2300d229a958SDavid Teigland }
23013cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2302d229a958SDavid Teigland 
23039ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2304637842cfSDavid Teigland {
23059ade92a9SAlasdair G Kergon 	return md->interface_ptr;
23061da177e4SLinus Torvalds }
23071da177e4SLinus Torvalds 
23081da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
23091da177e4SLinus Torvalds {
23101da177e4SLinus Torvalds 	md->interface_ptr = ptr;
23111da177e4SLinus Torvalds }
23121da177e4SLinus Torvalds 
23131da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
23141da177e4SLinus Torvalds {
23151da177e4SLinus Torvalds 	atomic_inc(&md->holders);
23163f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
23171da177e4SLinus Torvalds }
23181da177e4SLinus Torvalds 
231972d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
232072d94861SAlasdair G Kergon {
232172d94861SAlasdair G Kergon 	return md->name;
232272d94861SAlasdair G Kergon }
232372d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
232472d94861SAlasdair G Kergon 
23253f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
23261da177e4SLinus Torvalds {
23271134e5aeSMike Anderson 	struct dm_table *map;
23281da177e4SLinus Torvalds 
23293f77316dSKiyoshi Ueda 	might_sleep();
2330fba9f90eSJeff Mahoney 
23313f77316dSKiyoshi Ueda 	spin_lock(&_minor_lock);
23327c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
23333f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2334fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2335f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
23363f77316dSKiyoshi Ueda 
23374f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
23381da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
23391da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
23401da177e4SLinus Torvalds 	}
23413f77316dSKiyoshi Ueda 
23423f77316dSKiyoshi Ueda 	/*
23433f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
23443f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
23453f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
23463f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
23473f77316dSKiyoshi Ueda 	 */
23483f77316dSKiyoshi Ueda 	if (wait)
23493f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
23503f77316dSKiyoshi Ueda 			msleep(1);
23513f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
23523f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
23533f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
23543f77316dSKiyoshi Ueda 
2355784aae73SMilan Broz 	dm_sysfs_exit(md);
23561134e5aeSMike Anderson 	dm_table_put(map);
2357a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
23581da177e4SLinus Torvalds 	free_dev(md);
23591da177e4SLinus Torvalds }
23603f77316dSKiyoshi Ueda 
23613f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
23623f77316dSKiyoshi Ueda {
23633f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
23643f77316dSKiyoshi Ueda }
23653f77316dSKiyoshi Ueda 
23663f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
23673f77316dSKiyoshi Ueda {
23683f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
23693f77316dSKiyoshi Ueda }
23703f77316dSKiyoshi Ueda 
23713f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
23723f77316dSKiyoshi Ueda {
23733f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
23741da177e4SLinus Torvalds }
237579eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
23761da177e4SLinus Torvalds 
2377401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
237846125c1cSMilan Broz {
237946125c1cSMilan Broz 	int r = 0;
2380b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
2381b44ebeb0SMikulas Patocka 
2382b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
238346125c1cSMilan Broz 
238446125c1cSMilan Broz 	while (1) {
2385401600dfSMikulas Patocka 		set_current_state(interruptible);
238646125c1cSMilan Broz 
2387b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
238846125c1cSMilan Broz 			break;
238946125c1cSMilan Broz 
2390401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
2391401600dfSMikulas Patocka 		    signal_pending(current)) {
239246125c1cSMilan Broz 			r = -EINTR;
239346125c1cSMilan Broz 			break;
239446125c1cSMilan Broz 		}
239546125c1cSMilan Broz 
239646125c1cSMilan Broz 		io_schedule();
239746125c1cSMilan Broz 	}
239846125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
239946125c1cSMilan Broz 
2400b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
2401b44ebeb0SMikulas Patocka 
240246125c1cSMilan Broz 	return r;
240346125c1cSMilan Broz }
240446125c1cSMilan Broz 
24051da177e4SLinus Torvalds /*
24061da177e4SLinus Torvalds  * Process the deferred bios
24071da177e4SLinus Torvalds  */
2408ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
24091da177e4SLinus Torvalds {
2410ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2411ef208587SMikulas Patocka 						work);
24126d6f10dfSMilan Broz 	struct bio *c;
24131da177e4SLinus Torvalds 
24146a8736d1STejun Heo 	down_read(&md->io_lock);
2415ef208587SMikulas Patocka 
24163b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2417022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2418022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2419022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2420022c2611SMikulas Patocka 
24216a8736d1STejun Heo 		if (!c)
2422df12ee99SAlasdair G Kergon 			break;
242373d410c0SMilan Broz 
24246a8736d1STejun Heo 		up_read(&md->io_lock);
24253b00b203SMikulas Patocka 
2426e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2427e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2428af7e466aSMikulas Patocka 		else
2429df12ee99SAlasdair G Kergon 			__split_and_process_bio(md, c);
24306a8736d1STejun Heo 
24316a8736d1STejun Heo 		down_read(&md->io_lock);
2432e6ee8c0bSKiyoshi Ueda 	}
24333b00b203SMikulas Patocka 
24346a8736d1STejun Heo 	up_read(&md->io_lock);
24351da177e4SLinus Torvalds }
24361da177e4SLinus Torvalds 
24379a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2438304f3f6aSMilan Broz {
24393b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
24403b00b203SMikulas Patocka 	smp_mb__after_clear_bit();
244153d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2442304f3f6aSMilan Broz }
2443304f3f6aSMilan Broz 
24441da177e4SLinus Torvalds /*
2445042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
24461da177e4SLinus Torvalds  */
2447042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
24481da177e4SLinus Torvalds {
2449*87eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2450754c5fc7SMike Snitzer 	struct queue_limits limits;
2451042d2a9bSAlasdair G Kergon 	int r;
24521da177e4SLinus Torvalds 
2453e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
24541da177e4SLinus Torvalds 
24551da177e4SLinus Torvalds 	/* device must be suspended */
24564f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
245793c534aeSAlasdair G Kergon 		goto out;
24581da177e4SLinus Torvalds 
24593ae70656SMike Snitzer 	/*
24603ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
24613ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
24623ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
24633ae70656SMike Snitzer 	 * reappear.
24643ae70656SMike Snitzer 	 */
24653ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
24663ae70656SMike Snitzer 		live_map = dm_get_live_table(md);
24673ae70656SMike Snitzer 		if (live_map)
24683ae70656SMike Snitzer 			limits = md->queue->limits;
24693ae70656SMike Snitzer 		dm_table_put(live_map);
24703ae70656SMike Snitzer 	}
24713ae70656SMike Snitzer 
2472*87eb5b21SMike Christie 	if (!live_map) {
2473754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2474042d2a9bSAlasdair G Kergon 		if (r) {
2475042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2476754c5fc7SMike Snitzer 			goto out;
2477042d2a9bSAlasdair G Kergon 		}
2478*87eb5b21SMike Christie 	}
2479754c5fc7SMike Snitzer 
2480042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
24811da177e4SLinus Torvalds 
248293c534aeSAlasdair G Kergon out:
2483e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2484042d2a9bSAlasdair G Kergon 	return map;
24851da177e4SLinus Torvalds }
24861da177e4SLinus Torvalds 
24871da177e4SLinus Torvalds /*
24881da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
24891da177e4SLinus Torvalds  * device.
24901da177e4SLinus Torvalds  */
24912ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
24921da177e4SLinus Torvalds {
2493e39e2e95SAlasdair G Kergon 	int r;
24941da177e4SLinus Torvalds 
24951da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2496dfbe03f6SAlasdair G Kergon 
2497db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2498dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2499cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2500e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2501e39e2e95SAlasdair G Kergon 		return r;
2502dfbe03f6SAlasdair G Kergon 	}
2503dfbe03f6SAlasdair G Kergon 
2504aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2505aa8d7c2fSAlasdair G Kergon 
25061da177e4SLinus Torvalds 	return 0;
25071da177e4SLinus Torvalds }
25081da177e4SLinus Torvalds 
25092ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
25101da177e4SLinus Torvalds {
2511aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2512aa8d7c2fSAlasdair G Kergon 		return;
2513aa8d7c2fSAlasdair G Kergon 
2514db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
25151da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2516aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
25171da177e4SLinus Torvalds }
25181da177e4SLinus Torvalds 
25191da177e4SLinus Torvalds /*
25201da177e4SLinus Torvalds  * We need to be able to change a mapping table under a mounted
25211da177e4SLinus Torvalds  * filesystem.  For example we might want to move some data in
25221da177e4SLinus Torvalds  * the background.  Before the table can be swapped with
25231da177e4SLinus Torvalds  * dm_bind_table, dm_suspend must be called to flush any in
25241da177e4SLinus Torvalds  * flight bios and ensure that any further io gets deferred.
25251da177e4SLinus Torvalds  */
2526cec47e3dSKiyoshi Ueda /*
2527cec47e3dSKiyoshi Ueda  * Suspend mechanism in request-based dm.
2528cec47e3dSKiyoshi Ueda  *
25299f518b27SKiyoshi Ueda  * 1. Flush all I/Os by lock_fs() if needed.
25309f518b27SKiyoshi Ueda  * 2. Stop dispatching any I/O by stopping the request_queue.
25319f518b27SKiyoshi Ueda  * 3. Wait for all in-flight I/Os to be completed or requeued.
2532cec47e3dSKiyoshi Ueda  *
25339f518b27SKiyoshi Ueda  * To abort suspend, start the request_queue.
2534cec47e3dSKiyoshi Ueda  */
2535a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
25361da177e4SLinus Torvalds {
25372ca3310eSAlasdair G Kergon 	struct dm_table *map = NULL;
253846125c1cSMilan Broz 	int r = 0;
2539a3d77d35SKiyoshi Ueda 	int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
25402e93ccc1SKiyoshi Ueda 	int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
25411da177e4SLinus Torvalds 
2542e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
25432ca3310eSAlasdair G Kergon 
25444f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
254573d410c0SMilan Broz 		r = -EINVAL;
2546d287483dSAlasdair G Kergon 		goto out_unlock;
254773d410c0SMilan Broz 	}
25481da177e4SLinus Torvalds 
25497c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
2550cf222b37SAlasdair G Kergon 
25512e93ccc1SKiyoshi Ueda 	/*
25522e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
25532e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
25542e93ccc1SKiyoshi Ueda 	 */
25552e93ccc1SKiyoshi Ueda 	if (noflush)
25562e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
25572e93ccc1SKiyoshi Ueda 
2558436d4108SAlasdair G Kergon 	/* This does not get reverted if there's an error later. */
25591da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
25601da177e4SLinus Torvalds 
25612e93ccc1SKiyoshi Ueda 	/*
25629f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
25639f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
25649f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
25659f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
25662e93ccc1SKiyoshi Ueda 	 */
256732a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
25682ca3310eSAlasdair G Kergon 		r = lock_fs(md);
25692ca3310eSAlasdair G Kergon 		if (r)
25702ca3310eSAlasdair G Kergon 			goto out;
2571aa8d7c2fSAlasdair G Kergon 	}
25721da177e4SLinus Torvalds 
25731da177e4SLinus Torvalds 	/*
25743b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
25753b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
25763b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
25773b00b203SMikulas Patocka 	 * dm_wq_work.
25783b00b203SMikulas Patocka 	 *
25793b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
25803b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
25816a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
25826a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
25836a8736d1STejun Heo 	 * flush_workqueue(md->wq).
25841da177e4SLinus Torvalds 	 */
25852ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
25861eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
25872ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
25881da177e4SLinus Torvalds 
2589d0bcb878SKiyoshi Ueda 	/*
259029e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
259129e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2592d0bcb878SKiyoshi Ueda 	 */
2593cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
25949f518b27SKiyoshi Ueda 		stop_queue(md->queue);
2595cec47e3dSKiyoshi Ueda 
2596d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2597d0bcb878SKiyoshi Ueda 
25981da177e4SLinus Torvalds 	/*
25993b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
26003b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
26013b00b203SMikulas Patocka 	 * to finish.
26021da177e4SLinus Torvalds 	 */
2603401600dfSMikulas Patocka 	r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
26041da177e4SLinus Torvalds 
26052ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
26066d6f10dfSMilan Broz 	if (noflush)
2607022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
260894d6351eSMilan Broz 	up_write(&md->io_lock);
26092e93ccc1SKiyoshi Ueda 
26101da177e4SLinus Torvalds 	/* were we interrupted ? */
261146125c1cSMilan Broz 	if (r < 0) {
26129a1fb464SMikulas Patocka 		dm_queue_flush(md);
261373d410c0SMilan Broz 
2614cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
26159f518b27SKiyoshi Ueda 			start_queue(md->queue);
2616cec47e3dSKiyoshi Ueda 
26172ca3310eSAlasdair G Kergon 		unlock_fs(md);
26182e93ccc1SKiyoshi Ueda 		goto out; /* pushback list is already flushed, so skip flush */
26192ca3310eSAlasdair G Kergon 	}
26202ca3310eSAlasdair G Kergon 
26213b00b203SMikulas Patocka 	/*
26223b00b203SMikulas Patocka 	 * If dm_wait_for_completion returned 0, the device is completely
26233b00b203SMikulas Patocka 	 * quiescent now. There is no request-processing activity. All new
26243b00b203SMikulas Patocka 	 * requests are being added to md->deferred list.
26253b00b203SMikulas Patocka 	 */
26263b00b203SMikulas Patocka 
26271da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
26281da177e4SLinus Torvalds 
26294d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
26304d4471cbSKiyoshi Ueda 
26312ca3310eSAlasdair G Kergon out:
26321da177e4SLinus Torvalds 	dm_table_put(map);
2633d287483dSAlasdair G Kergon 
2634d287483dSAlasdair G Kergon out_unlock:
2635e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2636cf222b37SAlasdair G Kergon 	return r;
26371da177e4SLinus Torvalds }
26381da177e4SLinus Torvalds 
26391da177e4SLinus Torvalds int dm_resume(struct mapped_device *md)
26401da177e4SLinus Torvalds {
2641cf222b37SAlasdair G Kergon 	int r = -EINVAL;
2642cf222b37SAlasdair G Kergon 	struct dm_table *map = NULL;
26431da177e4SLinus Torvalds 
2644e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
26454f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
2646cf222b37SAlasdair G Kergon 		goto out;
2647cf222b37SAlasdair G Kergon 
26487c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
26492ca3310eSAlasdair G Kergon 	if (!map || !dm_table_get_size(map))
2650cf222b37SAlasdair G Kergon 		goto out;
26511da177e4SLinus Torvalds 
26528757b776SMilan Broz 	r = dm_table_resume_targets(map);
26538757b776SMilan Broz 	if (r)
26548757b776SMilan Broz 		goto out;
26552ca3310eSAlasdair G Kergon 
26569a1fb464SMikulas Patocka 	dm_queue_flush(md);
26572ca3310eSAlasdair G Kergon 
2658cec47e3dSKiyoshi Ueda 	/*
2659cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2660cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2661cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2662cec47e3dSKiyoshi Ueda 	 */
2663cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2664cec47e3dSKiyoshi Ueda 		start_queue(md->queue);
2665cec47e3dSKiyoshi Ueda 
26662ca3310eSAlasdair G Kergon 	unlock_fs(md);
26672ca3310eSAlasdair G Kergon 
26682ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
26692ca3310eSAlasdair G Kergon 
2670cf222b37SAlasdair G Kergon 	r = 0;
2671cf222b37SAlasdair G Kergon out:
2672cf222b37SAlasdair G Kergon 	dm_table_put(map);
2673e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
26742ca3310eSAlasdair G Kergon 
2675cf222b37SAlasdair G Kergon 	return r;
26761da177e4SLinus Torvalds }
26771da177e4SLinus Torvalds 
26781da177e4SLinus Torvalds /*-----------------------------------------------------------------
26791da177e4SLinus Torvalds  * Event notification.
26801da177e4SLinus Torvalds  *---------------------------------------------------------------*/
26813abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
268260935eb2SMilan Broz 		       unsigned cookie)
268369267a30SAlasdair G Kergon {
268460935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
268560935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
268660935eb2SMilan Broz 
268760935eb2SMilan Broz 	if (!cookie)
26883abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
268960935eb2SMilan Broz 	else {
269060935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
269160935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
26923abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
26933abf85b5SPeter Rajnoha 					  action, envp);
269460935eb2SMilan Broz 	}
269569267a30SAlasdair G Kergon }
269669267a30SAlasdair G Kergon 
26977a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
26987a8c3d3bSMike Anderson {
26997a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
27007a8c3d3bSMike Anderson }
27017a8c3d3bSMike Anderson 
27021da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
27031da177e4SLinus Torvalds {
27041da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
27051da177e4SLinus Torvalds }
27061da177e4SLinus Torvalds 
27071da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
27081da177e4SLinus Torvalds {
27091da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
27101da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
27111da177e4SLinus Torvalds }
27121da177e4SLinus Torvalds 
27137a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
27147a8c3d3bSMike Anderson {
27157a8c3d3bSMike Anderson 	unsigned long flags;
27167a8c3d3bSMike Anderson 
27177a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
27187a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
27197a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
27207a8c3d3bSMike Anderson }
27217a8c3d3bSMike Anderson 
27221da177e4SLinus Torvalds /*
27231da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
27241da177e4SLinus Torvalds  * count on 'md'.
27251da177e4SLinus Torvalds  */
27261da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
27271da177e4SLinus Torvalds {
27281da177e4SLinus Torvalds 	return md->disk;
27291da177e4SLinus Torvalds }
27301da177e4SLinus Torvalds 
2731784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2732784aae73SMilan Broz {
2733784aae73SMilan Broz 	return &md->kobj;
2734784aae73SMilan Broz }
2735784aae73SMilan Broz 
2736784aae73SMilan Broz /*
2737784aae73SMilan Broz  * struct mapped_device should not be exported outside of dm.c
2738784aae73SMilan Broz  * so use this check to verify that kobj is part of md structure
2739784aae73SMilan Broz  */
2740784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2741784aae73SMilan Broz {
2742784aae73SMilan Broz 	struct mapped_device *md;
2743784aae73SMilan Broz 
2744784aae73SMilan Broz 	md = container_of(kobj, struct mapped_device, kobj);
2745784aae73SMilan Broz 	if (&md->kobj != kobj)
2746784aae73SMilan Broz 		return NULL;
2747784aae73SMilan Broz 
27484d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
2749432a212cSMike Anderson 	    dm_deleting_md(md))
27504d89b7b4SMilan Broz 		return NULL;
27514d89b7b4SMilan Broz 
2752784aae73SMilan Broz 	dm_get(md);
2753784aae73SMilan Broz 	return md;
2754784aae73SMilan Broz }
2755784aae73SMilan Broz 
27564f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
27571da177e4SLinus Torvalds {
27581da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
27591da177e4SLinus Torvalds }
27601da177e4SLinus Torvalds 
276164dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
276264dbce58SKiyoshi Ueda {
2763ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
276464dbce58SKiyoshi Ueda }
276564dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
276664dbce58SKiyoshi Ueda 
27672e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
27682e93ccc1SKiyoshi Ueda {
2769ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
27702e93ccc1SKiyoshi Ueda }
27712e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
27722e93ccc1SKiyoshi Ueda 
2773c0820cf5SMikulas Patocka struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity, unsigned per_bio_data_size)
2774e6ee8c0bSKiyoshi Ueda {
2775e6ee8c0bSKiyoshi Ueda 	struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2776a91a2785SMartin K. Petersen 	unsigned int pool_size = (type == DM_TYPE_BIO_BASED) ? 16 : MIN_IOS;
2777e6ee8c0bSKiyoshi Ueda 
2778e6ee8c0bSKiyoshi Ueda 	if (!pools)
2779e6ee8c0bSKiyoshi Ueda 		return NULL;
2780e6ee8c0bSKiyoshi Ueda 
2781c0820cf5SMikulas Patocka 	per_bio_data_size = roundup(per_bio_data_size, __alignof__(struct dm_target_io));
2782c0820cf5SMikulas Patocka 
2783e6ee8c0bSKiyoshi Ueda 	pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2784e6ee8c0bSKiyoshi Ueda 			 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2785e6ee8c0bSKiyoshi Ueda 			 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2786e6ee8c0bSKiyoshi Ueda 	if (!pools->io_pool)
2787e6ee8c0bSKiyoshi Ueda 		goto free_pools_and_out;
2788e6ee8c0bSKiyoshi Ueda 
2789dba14160SMikulas Patocka 	pools->tio_pool = NULL;
2790dba14160SMikulas Patocka 	if (type == DM_TYPE_REQUEST_BASED) {
2791dba14160SMikulas Patocka 		pools->tio_pool = mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2792e6ee8c0bSKiyoshi Ueda 		if (!pools->tio_pool)
2793e6ee8c0bSKiyoshi Ueda 			goto free_io_pool_and_out;
2794dba14160SMikulas Patocka 	}
2795e6ee8c0bSKiyoshi Ueda 
279694818742SKent Overstreet 	pools->bs = (type == DM_TYPE_BIO_BASED) ?
2797dba14160SMikulas Patocka 		bioset_create(pool_size,
2798c0820cf5SMikulas Patocka 			      per_bio_data_size + offsetof(struct dm_target_io, clone)) :
279994818742SKent Overstreet 		bioset_create(pool_size,
280094818742SKent Overstreet 			      offsetof(struct dm_rq_clone_bio_info, clone));
2801e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
2802e6ee8c0bSKiyoshi Ueda 		goto free_tio_pool_and_out;
2803e6ee8c0bSKiyoshi Ueda 
2804a91a2785SMartin K. Petersen 	if (integrity && bioset_integrity_create(pools->bs, pool_size))
2805a91a2785SMartin K. Petersen 		goto free_bioset_and_out;
2806a91a2785SMartin K. Petersen 
2807e6ee8c0bSKiyoshi Ueda 	return pools;
2808e6ee8c0bSKiyoshi Ueda 
2809a91a2785SMartin K. Petersen free_bioset_and_out:
2810a91a2785SMartin K. Petersen 	bioset_free(pools->bs);
2811a91a2785SMartin K. Petersen 
2812e6ee8c0bSKiyoshi Ueda free_tio_pool_and_out:
2813dba14160SMikulas Patocka 	if (pools->tio_pool)
2814e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->tio_pool);
2815e6ee8c0bSKiyoshi Ueda 
2816e6ee8c0bSKiyoshi Ueda free_io_pool_and_out:
2817e6ee8c0bSKiyoshi Ueda 	mempool_destroy(pools->io_pool);
2818e6ee8c0bSKiyoshi Ueda 
2819e6ee8c0bSKiyoshi Ueda free_pools_and_out:
2820e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2821e6ee8c0bSKiyoshi Ueda 
2822e6ee8c0bSKiyoshi Ueda 	return NULL;
2823e6ee8c0bSKiyoshi Ueda }
2824e6ee8c0bSKiyoshi Ueda 
2825e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
2826e6ee8c0bSKiyoshi Ueda {
2827e6ee8c0bSKiyoshi Ueda 	if (!pools)
2828e6ee8c0bSKiyoshi Ueda 		return;
2829e6ee8c0bSKiyoshi Ueda 
2830e6ee8c0bSKiyoshi Ueda 	if (pools->io_pool)
2831e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->io_pool);
2832e6ee8c0bSKiyoshi Ueda 
2833e6ee8c0bSKiyoshi Ueda 	if (pools->tio_pool)
2834e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->tio_pool);
2835e6ee8c0bSKiyoshi Ueda 
2836e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
2837e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
2838e6ee8c0bSKiyoshi Ueda 
2839e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2840e6ee8c0bSKiyoshi Ueda }
2841e6ee8c0bSKiyoshi Ueda 
284283d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
28431da177e4SLinus Torvalds 	.open = dm_blk_open,
28441da177e4SLinus Torvalds 	.release = dm_blk_close,
2845aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
28463ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
28471da177e4SLinus Torvalds 	.owner = THIS_MODULE
28481da177e4SLinus Torvalds };
28491da177e4SLinus Torvalds 
28501da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo);
28511da177e4SLinus Torvalds 
28521da177e4SLinus Torvalds /*
28531da177e4SLinus Torvalds  * module hooks
28541da177e4SLinus Torvalds  */
28551da177e4SLinus Torvalds module_init(dm_init);
28561da177e4SLinus Torvalds module_exit(dm_exit);
28571da177e4SLinus Torvalds 
28581da177e4SLinus Torvalds module_param(major, uint, 0);
28591da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
28601da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
28611da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
28621da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2863