xref: /openbmc/linux/drivers/md/dm.c (revision 16245bdc9d3e22d1460341a655c8b5288953bc14)
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 
988dba14160SMikulas Patocka static void __map_bio(struct dm_target *ti, 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;
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
9961da177e4SLinus Torvalds 	clone->bi_private = tio;
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 	/*
9991da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
10001da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
10011da177e4SLinus Torvalds 	 * this io.
10021da177e4SLinus Torvalds 	 */
10031da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
10042056a782SJens Axboe 	sector = clone->bi_sector;
10057de3ee57SMikulas Patocka 	r = ti->type->map(ti, clone);
100645cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
10071da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
10082056a782SJens Axboe 
1009d07335e5SMike Snitzer 		trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
101022a7c31aSAlan D. Brunelle 				      tio->io->bio->bi_bdev->bd_dev, sector);
10112056a782SJens Axboe 
10121da177e4SLinus Torvalds 		generic_make_request(clone);
10132e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
10142e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
10159faf400fSStefan Bader 		md = tio->io->md;
10169faf400fSStefan Bader 		dec_pending(tio->io, r);
10179faf400fSStefan Bader 		free_tio(md, tio);
101845cbcd79SKiyoshi Ueda 	} else if (r) {
101945cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
102045cbcd79SKiyoshi Ueda 		BUG();
10211da177e4SLinus Torvalds 	}
10221da177e4SLinus Torvalds }
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds struct clone_info {
10251da177e4SLinus Torvalds 	struct mapped_device *md;
10261da177e4SLinus Torvalds 	struct dm_table *map;
10271da177e4SLinus Torvalds 	struct bio *bio;
10281da177e4SLinus Torvalds 	struct dm_io *io;
10291da177e4SLinus Torvalds 	sector_t sector;
10301da177e4SLinus Torvalds 	sector_t sector_count;
10311da177e4SLinus Torvalds 	unsigned short idx;
10321da177e4SLinus Torvalds };
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds /*
1035d87f4c14STejun Heo  * Creates a little bio that just does part of a bvec.
10361da177e4SLinus Torvalds  */
1037dba14160SMikulas Patocka static void split_bvec(struct dm_target_io *tio, struct bio *bio,
1038dba14160SMikulas Patocka 		       sector_t sector, unsigned short idx, unsigned int offset,
10399faf400fSStefan Bader 		       unsigned int len, struct bio_set *bs)
10401da177e4SLinus Torvalds {
1041dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
10421da177e4SLinus Torvalds 	struct bio_vec *bv = bio->bi_io_vec + idx;
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	*clone->bi_io_vec = *bv;
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds 	clone->bi_sector = sector;
10471da177e4SLinus Torvalds 	clone->bi_bdev = bio->bi_bdev;
1048d87f4c14STejun Heo 	clone->bi_rw = bio->bi_rw;
10491da177e4SLinus Torvalds 	clone->bi_vcnt = 1;
10501da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
10511da177e4SLinus Torvalds 	clone->bi_io_vec->bv_offset = offset;
10521da177e4SLinus Torvalds 	clone->bi_io_vec->bv_len = clone->bi_size;
1053f3e1d26eSMartin K. Petersen 	clone->bi_flags |= 1 << BIO_CLONED;
10541da177e4SLinus Torvalds 
10559c47008dSMartin K. Petersen 	if (bio_integrity(bio)) {
10561e2a410fSKent Overstreet 		bio_integrity_clone(clone, bio, GFP_NOIO);
10579c47008dSMartin K. Petersen 		bio_integrity_trim(clone,
10589c47008dSMartin K. Petersen 				   bio_sector_offset(bio, idx, offset), len);
10599c47008dSMartin K. Petersen 	}
10601da177e4SLinus Torvalds }
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds /*
10631da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
10641da177e4SLinus Torvalds  */
1065dba14160SMikulas Patocka static void clone_bio(struct dm_target_io *tio, struct bio *bio,
1066dba14160SMikulas Patocka 		      sector_t sector, unsigned short idx,
1067dba14160SMikulas Patocka 		      unsigned short bv_count, unsigned int len,
1068dba14160SMikulas Patocka 		      struct bio_set *bs)
10691da177e4SLinus Torvalds {
1070dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
10711da177e4SLinus Torvalds 
10729faf400fSStefan Bader 	__bio_clone(clone, bio);
10731da177e4SLinus Torvalds 	clone->bi_sector = sector;
10741da177e4SLinus Torvalds 	clone->bi_idx = idx;
10751da177e4SLinus Torvalds 	clone->bi_vcnt = idx + bv_count;
10761da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
10771da177e4SLinus Torvalds 	clone->bi_flags &= ~(1 << BIO_SEG_VALID);
10781da177e4SLinus Torvalds 
10799c47008dSMartin K. Petersen 	if (bio_integrity(bio)) {
10801e2a410fSKent Overstreet 		bio_integrity_clone(clone, bio, GFP_NOIO);
10819c47008dSMartin K. Petersen 
10829c47008dSMartin K. Petersen 		if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
10839c47008dSMartin K. Petersen 			bio_integrity_trim(clone,
10849c47008dSMartin K. Petersen 					   bio_sector_offset(bio, idx, 0), len);
10859c47008dSMartin K. Petersen 	}
10861da177e4SLinus Torvalds }
10871da177e4SLinus Torvalds 
10889015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci,
1089dba14160SMikulas Patocka 				      struct dm_target *ti, int nr_iovecs)
1090f9ab94ceSMikulas Patocka {
1091dba14160SMikulas Patocka 	struct dm_target_io *tio;
1092dba14160SMikulas Patocka 	struct bio *clone;
1093dba14160SMikulas Patocka 
1094dba14160SMikulas Patocka 	clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, ci->md->bs);
1095dba14160SMikulas Patocka 	tio = container_of(clone, struct dm_target_io, clone);
1096f9ab94ceSMikulas Patocka 
1097f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1098f9ab94ceSMikulas Patocka 	tio->ti = ti;
1099f9ab94ceSMikulas Patocka 	memset(&tio->info, 0, sizeof(tio->info));
1100ddbd658fSMikulas Patocka 	tio->target_request_nr = 0;
11019015df24SAlasdair G Kergon 
11029015df24SAlasdair G Kergon 	return tio;
11039015df24SAlasdair G Kergon }
11049015df24SAlasdair G Kergon 
110506a426ceSMike Snitzer static void __issue_target_request(struct clone_info *ci, struct dm_target *ti,
1106a79245b3SMike Snitzer 				   unsigned request_nr, sector_t len)
11079015df24SAlasdair G Kergon {
1108dba14160SMikulas Patocka 	struct dm_target_io *tio = alloc_tio(ci, ti, ci->bio->bi_max_vecs);
1109dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
11109015df24SAlasdair G Kergon 
1111ddbd658fSMikulas Patocka 	tio->target_request_nr = request_nr;
1112f9ab94ceSMikulas Patocka 
111306a426ceSMike Snitzer 	/*
111406a426ceSMike Snitzer 	 * Discard requests require the bio's inline iovecs be initialized.
111506a426ceSMike Snitzer 	 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
111606a426ceSMike Snitzer 	 * and discard, so no need for concern about wasted bvec allocations.
111706a426ceSMike Snitzer 	 */
1118bf800ef1SKent Overstreet 
1119dba14160SMikulas Patocka 	 __bio_clone(clone, ci->bio);
1120a79245b3SMike Snitzer 	if (len) {
1121a79245b3SMike Snitzer 		clone->bi_sector = ci->sector;
1122a79245b3SMike Snitzer 		clone->bi_size = to_bytes(len);
1123a79245b3SMike Snitzer 	}
1124f9ab94ceSMikulas Patocka 
1125dba14160SMikulas Patocka 	__map_bio(ti, tio);
1126f9ab94ceSMikulas Patocka }
1127f9ab94ceSMikulas Patocka 
112806a426ceSMike Snitzer static void __issue_target_requests(struct clone_info *ci, struct dm_target *ti,
1129a79245b3SMike Snitzer 				    unsigned num_requests, sector_t len)
113006a426ceSMike Snitzer {
113106a426ceSMike Snitzer 	unsigned request_nr;
113206a426ceSMike Snitzer 
113306a426ceSMike Snitzer 	for (request_nr = 0; request_nr < num_requests; request_nr++)
1134a79245b3SMike Snitzer 		__issue_target_request(ci, ti, request_nr, len);
113506a426ceSMike Snitzer }
113606a426ceSMike Snitzer 
1137b372d360SMike Snitzer static int __clone_and_map_empty_flush(struct clone_info *ci)
1138f9ab94ceSMikulas Patocka {
113906a426ceSMike Snitzer 	unsigned target_nr = 0;
1140f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1141f9ab94ceSMikulas Patocka 
1142b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1143f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
1144a79245b3SMike Snitzer 		__issue_target_requests(ci, ti, ti->num_flush_requests, 0);
1145f9ab94ceSMikulas Patocka 
1146f9ab94ceSMikulas Patocka 	return 0;
1147f9ab94ceSMikulas Patocka }
1148f9ab94ceSMikulas Patocka 
11495ae89a87SMike Snitzer /*
11505ae89a87SMike Snitzer  * Perform all io with a single clone.
11515ae89a87SMike Snitzer  */
11525ae89a87SMike Snitzer static void __clone_and_map_simple(struct clone_info *ci, struct dm_target *ti)
11535ae89a87SMike Snitzer {
1154dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
11555ae89a87SMike Snitzer 	struct dm_target_io *tio;
11565ae89a87SMike Snitzer 
1157dba14160SMikulas Patocka 	tio = alloc_tio(ci, ti, bio->bi_max_vecs);
1158dba14160SMikulas Patocka 	clone_bio(tio, bio, ci->sector, ci->idx, bio->bi_vcnt - ci->idx,
1159dba14160SMikulas Patocka 		  ci->sector_count, ci->md->bs);
1160dba14160SMikulas Patocka 	__map_bio(ti, tio);
11615ae89a87SMike Snitzer 	ci->sector_count = 0;
11625ae89a87SMike Snitzer }
11635ae89a87SMike Snitzer 
116423508a96SMike Snitzer typedef unsigned (*get_num_requests_fn)(struct dm_target *ti);
116523508a96SMike Snitzer 
116623508a96SMike Snitzer static unsigned get_num_discard_requests(struct dm_target *ti)
116723508a96SMike Snitzer {
116823508a96SMike Snitzer 	return ti->num_discard_requests;
116923508a96SMike Snitzer }
117023508a96SMike Snitzer 
117123508a96SMike Snitzer static unsigned get_num_write_same_requests(struct dm_target *ti)
117223508a96SMike Snitzer {
117323508a96SMike Snitzer 	return ti->num_write_same_requests;
117423508a96SMike Snitzer }
117523508a96SMike Snitzer 
117623508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti);
117723508a96SMike Snitzer 
117823508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti)
117923508a96SMike Snitzer {
118023508a96SMike Snitzer 	return ti->split_discard_requests;
118123508a96SMike Snitzer }
118223508a96SMike Snitzer 
118323508a96SMike Snitzer static int __clone_and_map_changing_extent_only(struct clone_info *ci,
118423508a96SMike Snitzer 						get_num_requests_fn get_num_requests,
118523508a96SMike Snitzer 						is_split_required_fn is_split_required)
11865ae89a87SMike Snitzer {
11875ae89a87SMike Snitzer 	struct dm_target *ti;
1188a79245b3SMike Snitzer 	sector_t len;
1189fe7af2d3SAlasdair G Kergon 	unsigned num_requests;
11905ae89a87SMike Snitzer 
1191a79245b3SMike Snitzer 	do {
11925ae89a87SMike Snitzer 		ti = dm_table_find_target(ci->map, ci->sector);
11935ae89a87SMike Snitzer 		if (!dm_target_is_valid(ti))
11945ae89a87SMike Snitzer 			return -EIO;
11955ae89a87SMike Snitzer 
11965ae89a87SMike Snitzer 		/*
119723508a96SMike Snitzer 		 * Even though the device advertised support for this type of
119823508a96SMike Snitzer 		 * request, that does not mean every target supports it, and
1199936688d7SMike Snitzer 		 * reconfiguration might also have changed that since the
12005ae89a87SMike Snitzer 		 * check was performed.
12015ae89a87SMike Snitzer 		 */
1202fe7af2d3SAlasdair G Kergon 		num_requests = get_num_requests ? get_num_requests(ti) : 0;
1203fe7af2d3SAlasdair G Kergon 		if (!num_requests)
12045ae89a87SMike Snitzer 			return -EOPNOTSUPP;
12055ae89a87SMike Snitzer 
120623508a96SMike Snitzer 		if (is_split_required && !is_split_required(ti))
1207a79245b3SMike Snitzer 			len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
12087acf0277SMikulas Patocka 		else
12097acf0277SMikulas Patocka 			len = min(ci->sector_count, max_io_len(ci->sector, ti));
12105ae89a87SMike Snitzer 
1211fe7af2d3SAlasdair G Kergon 		__issue_target_requests(ci, ti, num_requests, len);
12125ae89a87SMike Snitzer 
1213a79245b3SMike Snitzer 		ci->sector += len;
1214a79245b3SMike Snitzer 	} while (ci->sector_count -= len);
12155ae89a87SMike Snitzer 
12165ae89a87SMike Snitzer 	return 0;
12175ae89a87SMike Snitzer }
12185ae89a87SMike Snitzer 
121923508a96SMike Snitzer static int __clone_and_map_discard(struct clone_info *ci)
122023508a96SMike Snitzer {
122123508a96SMike Snitzer 	return __clone_and_map_changing_extent_only(ci, get_num_discard_requests,
122223508a96SMike Snitzer 						    is_split_required_for_discard);
122323508a96SMike Snitzer }
122423508a96SMike Snitzer 
122523508a96SMike Snitzer static int __clone_and_map_write_same(struct clone_info *ci)
122623508a96SMike Snitzer {
122723508a96SMike Snitzer 	return __clone_and_map_changing_extent_only(ci, get_num_write_same_requests, NULL);
122823508a96SMike Snitzer }
122923508a96SMike Snitzer 
1230512875bdSJun'ichi Nomura static int __clone_and_map(struct clone_info *ci)
12311da177e4SLinus Torvalds {
1232dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
1233512875bdSJun'ichi Nomura 	struct dm_target *ti;
1234512875bdSJun'ichi Nomura 	sector_t len = 0, max;
1235028867acSAlasdair G Kergon 	struct dm_target_io *tio;
12361da177e4SLinus Torvalds 
12375ae89a87SMike Snitzer 	if (unlikely(bio->bi_rw & REQ_DISCARD))
12385ae89a87SMike Snitzer 		return __clone_and_map_discard(ci);
123923508a96SMike Snitzer 	else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
124023508a96SMike Snitzer 		return __clone_and_map_write_same(ci);
12415ae89a87SMike Snitzer 
1242512875bdSJun'ichi Nomura 	ti = dm_table_find_target(ci->map, ci->sector);
1243512875bdSJun'ichi Nomura 	if (!dm_target_is_valid(ti))
1244512875bdSJun'ichi Nomura 		return -EIO;
1245512875bdSJun'ichi Nomura 
124656a67df7SMike Snitzer 	max = max_io_len(ci->sector, ti);
1247512875bdSJun'ichi Nomura 
12481da177e4SLinus Torvalds 	if (ci->sector_count <= max) {
12491da177e4SLinus Torvalds 		/*
12501da177e4SLinus Torvalds 		 * Optimise for the simple case where we can do all of
12511da177e4SLinus Torvalds 		 * the remaining io with a single clone.
12521da177e4SLinus Torvalds 		 */
12535ae89a87SMike Snitzer 		__clone_and_map_simple(ci, ti);
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds 	} else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
12561da177e4SLinus Torvalds 		/*
12571da177e4SLinus Torvalds 		 * There are some bvecs that don't span targets.
12581da177e4SLinus Torvalds 		 * Do as many of these as possible.
12591da177e4SLinus Torvalds 		 */
12601da177e4SLinus Torvalds 		int i;
12611da177e4SLinus Torvalds 		sector_t remaining = max;
12621da177e4SLinus Torvalds 		sector_t bv_len;
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 		for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
12651da177e4SLinus Torvalds 			bv_len = to_sector(bio->bi_io_vec[i].bv_len);
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 			if (bv_len > remaining)
12681da177e4SLinus Torvalds 				break;
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 			remaining -= bv_len;
12711da177e4SLinus Torvalds 			len += bv_len;
12721da177e4SLinus Torvalds 		}
12731da177e4SLinus Torvalds 
1274dba14160SMikulas Patocka 		tio = alloc_tio(ci, ti, bio->bi_max_vecs);
1275dba14160SMikulas Patocka 		clone_bio(tio, bio, ci->sector, ci->idx, i - ci->idx, len,
12769faf400fSStefan Bader 			  ci->md->bs);
1277dba14160SMikulas Patocka 		__map_bio(ti, tio);
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 		ci->sector += len;
12801da177e4SLinus Torvalds 		ci->sector_count -= len;
12811da177e4SLinus Torvalds 		ci->idx = i;
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	} else {
12841da177e4SLinus Torvalds 		/*
1285d2044a94SAlasdair G Kergon 		 * Handle a bvec that must be split between two or more targets.
12861da177e4SLinus Torvalds 		 */
12871da177e4SLinus Torvalds 		struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1288d2044a94SAlasdair G Kergon 		sector_t remaining = to_sector(bv->bv_len);
1289d2044a94SAlasdair G Kergon 		unsigned int offset = 0;
12901da177e4SLinus Torvalds 
1291d2044a94SAlasdair G Kergon 		do {
1292d2044a94SAlasdair G Kergon 			if (offset) {
12931da177e4SLinus Torvalds 				ti = dm_table_find_target(ci->map, ci->sector);
1294512875bdSJun'ichi Nomura 				if (!dm_target_is_valid(ti))
1295512875bdSJun'ichi Nomura 					return -EIO;
1296512875bdSJun'ichi Nomura 
129756a67df7SMike Snitzer 				max = max_io_len(ci->sector, ti);
1298d2044a94SAlasdair G Kergon 			}
1299d2044a94SAlasdair G Kergon 
1300d2044a94SAlasdair G Kergon 			len = min(remaining, max);
1301d2044a94SAlasdair G Kergon 
1302dba14160SMikulas Patocka 			tio = alloc_tio(ci, ti, 1);
1303dba14160SMikulas Patocka 			split_bvec(tio, bio, ci->sector, ci->idx,
1304dba14160SMikulas Patocka 				   bv->bv_offset + offset, len, ci->md->bs);
1305d2044a94SAlasdair G Kergon 
1306dba14160SMikulas Patocka 			__map_bio(ti, tio);
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 			ci->sector += len;
13091da177e4SLinus Torvalds 			ci->sector_count -= len;
1310d2044a94SAlasdair G Kergon 			offset += to_bytes(len);
1311d2044a94SAlasdair G Kergon 		} while (remaining -= len);
1312d2044a94SAlasdair G Kergon 
13131da177e4SLinus Torvalds 		ci->idx++;
13141da177e4SLinus Torvalds 	}
1315512875bdSJun'ichi Nomura 
1316512875bdSJun'ichi Nomura 	return 0;
13171da177e4SLinus Torvalds }
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds /*
13208a53c28dSMikulas Patocka  * Split the bio into several clones and submit it to targets.
13211da177e4SLinus Torvalds  */
1322f0b9a450SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
13231da177e4SLinus Torvalds {
13241da177e4SLinus Torvalds 	struct clone_info ci;
1325512875bdSJun'ichi Nomura 	int error = 0;
13261da177e4SLinus Torvalds 
13277c666411SAlasdair G Kergon 	ci.map = dm_get_live_table(md);
1328f0b9a450SMikulas Patocka 	if (unlikely(!ci.map)) {
1329f0b9a450SMikulas Patocka 		bio_io_error(bio);
1330f0b9a450SMikulas Patocka 		return;
1331f0b9a450SMikulas Patocka 	}
1332692d0eb9SMikulas Patocka 
13331da177e4SLinus Torvalds 	ci.md = md;
13341da177e4SLinus Torvalds 	ci.io = alloc_io(md);
13351da177e4SLinus Torvalds 	ci.io->error = 0;
13361da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
13371da177e4SLinus Torvalds 	ci.io->bio = bio;
13381da177e4SLinus Torvalds 	ci.io->md = md;
1339f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
13401da177e4SLinus Torvalds 	ci.sector = bio->bi_sector;
13411da177e4SLinus Torvalds 	ci.idx = bio->bi_idx;
13421da177e4SLinus Torvalds 
13433eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1344b372d360SMike Snitzer 	if (bio->bi_rw & REQ_FLUSH) {
1345b372d360SMike Snitzer 		ci.bio = &ci.md->flush_bio;
1346b372d360SMike Snitzer 		ci.sector_count = 0;
1347b372d360SMike Snitzer 		error = __clone_and_map_empty_flush(&ci);
1348b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1349b372d360SMike Snitzer 	} else {
13506a8736d1STejun Heo 		ci.bio = bio;
1351f6fccb12SMilan Broz 		ci.sector_count = bio_sectors(bio);
1352512875bdSJun'ichi Nomura 		while (ci.sector_count && !error)
1353512875bdSJun'ichi Nomura 			error = __clone_and_map(&ci);
1354d87f4c14STejun Heo 	}
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds 	/* drop the extra reference count */
1357512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
13581da177e4SLinus Torvalds 	dm_table_put(ci.map);
13599e4e5f87SMilan Broz }
13609e4e5f87SMilan Broz /*-----------------------------------------------------------------
13611da177e4SLinus Torvalds  * CRUD END
13621da177e4SLinus Torvalds  *---------------------------------------------------------------*/
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds static int dm_merge_bvec(struct request_queue *q,
13651da177e4SLinus Torvalds 			 struct bvec_merge_data *bvm,
1366f6fccb12SMilan Broz 			 struct bio_vec *biovec)
1367f6fccb12SMilan Broz {
1368f6fccb12SMilan Broz 	struct mapped_device *md = q->queuedata;
13697c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1370f6fccb12SMilan Broz 	struct dm_target *ti;
1371f6fccb12SMilan Broz 	sector_t max_sectors;
1372f6fccb12SMilan Broz 	int max_size = 0;
1373f6fccb12SMilan Broz 
1374f6fccb12SMilan Broz 	if (unlikely(!map))
1375f6fccb12SMilan Broz 		goto out;
1376f6fccb12SMilan Broz 
1377f6fccb12SMilan Broz 	ti = dm_table_find_target(map, bvm->bi_sector);
1378f6fccb12SMilan Broz 	if (!dm_target_is_valid(ti))
1379f6fccb12SMilan Broz 		goto out_table;
1380f6fccb12SMilan Broz 
1381f6fccb12SMilan Broz 	/*
1382f6fccb12SMilan Broz 	 * Find maximum amount of I/O that won't need splitting
1383f6fccb12SMilan Broz 	 */
138456a67df7SMike Snitzer 	max_sectors = min(max_io_len(bvm->bi_sector, ti),
1385f6fccb12SMilan Broz 			  (sector_t) BIO_MAX_SECTORS);
1386f6fccb12SMilan Broz 	max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1387f6fccb12SMilan Broz 	if (max_size < 0)
1388f6fccb12SMilan Broz 		max_size = 0;
1389f6fccb12SMilan Broz 
1390f6fccb12SMilan Broz 	/*
1391f6fccb12SMilan Broz 	 * merge_bvec_fn() returns number of bytes
1392f6fccb12SMilan Broz 	 * it can accept at this offset
1393f6fccb12SMilan Broz 	 * max is precomputed maximal io size
1394f6fccb12SMilan Broz 	 */
1395f6fccb12SMilan Broz 	if (max_size && ti->type->merge)
1396f6fccb12SMilan Broz 		max_size = ti->type->merge(ti, bvm, biovec, max_size);
13978cbeb67aSMikulas Patocka 	/*
13988cbeb67aSMikulas Patocka 	 * If the target doesn't support merge method and some of the devices
13998cbeb67aSMikulas Patocka 	 * provided their merge_bvec method (we know this by looking at
14008cbeb67aSMikulas Patocka 	 * queue_max_hw_sectors), then we can't allow bios with multiple vector
14018cbeb67aSMikulas Patocka 	 * entries.  So always set max_size to 0, and the code below allows
14028cbeb67aSMikulas Patocka 	 * just one page.
14038cbeb67aSMikulas Patocka 	 */
14048cbeb67aSMikulas Patocka 	else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
14058cbeb67aSMikulas Patocka 
14068cbeb67aSMikulas Patocka 		max_size = 0;
1407f6fccb12SMilan Broz 
1408b01cd5acSMikulas Patocka out_table:
14095037108aSMikulas Patocka 	dm_table_put(map);
14105037108aSMikulas Patocka 
14115037108aSMikulas Patocka out:
1412f6fccb12SMilan Broz 	/*
1413f6fccb12SMilan Broz 	 * Always allow an entire first page
1414f6fccb12SMilan Broz 	 */
1415f6fccb12SMilan Broz 	if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1416f6fccb12SMilan Broz 		max_size = biovec->bv_len;
1417f6fccb12SMilan Broz 
1418f6fccb12SMilan Broz 	return max_size;
1419f6fccb12SMilan Broz }
1420f6fccb12SMilan Broz 
14211da177e4SLinus Torvalds /*
14221da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
14231da177e4SLinus Torvalds  * dm_merge_bvec.
14241da177e4SLinus Torvalds  */
14255a7bbad2SChristoph Hellwig static void _dm_request(struct request_queue *q, struct bio *bio)
14261da177e4SLinus Torvalds {
142712f03a49SKevin Corry 	int rw = bio_data_dir(bio);
14281da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
1429c9959059STejun Heo 	int cpu;
14301da177e4SLinus Torvalds 
14312ca3310eSAlasdair G Kergon 	down_read(&md->io_lock);
14321da177e4SLinus Torvalds 
1433074a7acaSTejun Heo 	cpu = part_stat_lock();
1434074a7acaSTejun Heo 	part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1435074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1436074a7acaSTejun Heo 	part_stat_unlock();
143712f03a49SKevin Corry 
14386a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
14396a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
14402ca3310eSAlasdair G Kergon 		up_read(&md->io_lock);
14411da177e4SLinus Torvalds 
14426a8736d1STejun Heo 		if (bio_rw(bio) != READA)
144392c63902SMikulas Patocka 			queue_io(md, bio);
14446a8736d1STejun Heo 		else
14456a8736d1STejun Heo 			bio_io_error(bio);
14465a7bbad2SChristoph Hellwig 		return;
14471da177e4SLinus Torvalds 	}
14481da177e4SLinus Torvalds 
1449f0b9a450SMikulas Patocka 	__split_and_process_bio(md, bio);
14502ca3310eSAlasdair G Kergon 	up_read(&md->io_lock);
14515a7bbad2SChristoph Hellwig 	return;
1452cec47e3dSKiyoshi Ueda }
1453cec47e3dSKiyoshi Ueda 
1454cec47e3dSKiyoshi Ueda static int dm_request_based(struct mapped_device *md)
1455cec47e3dSKiyoshi Ueda {
1456cec47e3dSKiyoshi Ueda 	return blk_queue_stackable(md->queue);
1457cec47e3dSKiyoshi Ueda }
1458cec47e3dSKiyoshi Ueda 
14595a7bbad2SChristoph Hellwig static void dm_request(struct request_queue *q, struct bio *bio)
1460cec47e3dSKiyoshi Ueda {
1461cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1462cec47e3dSKiyoshi Ueda 
1463cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
14645a7bbad2SChristoph Hellwig 		blk_queue_bio(q, bio);
14655a7bbad2SChristoph Hellwig 	else
14665a7bbad2SChristoph Hellwig 		_dm_request(q, bio);
1467cec47e3dSKiyoshi Ueda }
1468cec47e3dSKiyoshi Ueda 
1469cec47e3dSKiyoshi Ueda void dm_dispatch_request(struct request *rq)
1470cec47e3dSKiyoshi Ueda {
1471cec47e3dSKiyoshi Ueda 	int r;
1472cec47e3dSKiyoshi Ueda 
1473cec47e3dSKiyoshi Ueda 	if (blk_queue_io_stat(rq->q))
1474cec47e3dSKiyoshi Ueda 		rq->cmd_flags |= REQ_IO_STAT;
1475cec47e3dSKiyoshi Ueda 
1476cec47e3dSKiyoshi Ueda 	rq->start_time = jiffies;
1477cec47e3dSKiyoshi Ueda 	r = blk_insert_cloned_request(rq->q, rq);
1478cec47e3dSKiyoshi Ueda 	if (r)
1479cec47e3dSKiyoshi Ueda 		dm_complete_request(rq, r);
1480cec47e3dSKiyoshi Ueda }
1481cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_dispatch_request);
1482cec47e3dSKiyoshi Ueda 
1483cec47e3dSKiyoshi Ueda static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1484cec47e3dSKiyoshi Ueda 				 void *data)
1485cec47e3dSKiyoshi Ueda {
1486cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = data;
148794818742SKent Overstreet 	struct dm_rq_clone_bio_info *info =
148894818742SKent Overstreet 		container_of(bio, struct dm_rq_clone_bio_info, clone);
1489cec47e3dSKiyoshi Ueda 
1490cec47e3dSKiyoshi Ueda 	info->orig = bio_orig;
1491cec47e3dSKiyoshi Ueda 	info->tio = tio;
1492cec47e3dSKiyoshi Ueda 	bio->bi_end_io = end_clone_bio;
1493cec47e3dSKiyoshi Ueda 	bio->bi_private = info;
1494cec47e3dSKiyoshi Ueda 
1495cec47e3dSKiyoshi Ueda 	return 0;
1496cec47e3dSKiyoshi Ueda }
1497cec47e3dSKiyoshi Ueda 
1498cec47e3dSKiyoshi Ueda static int setup_clone(struct request *clone, struct request *rq,
1499cec47e3dSKiyoshi Ueda 		       struct dm_rq_target_io *tio)
1500cec47e3dSKiyoshi Ueda {
1501d0bcb878SKiyoshi Ueda 	int r;
1502cec47e3dSKiyoshi Ueda 
1503d0bcb878SKiyoshi Ueda 	r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1504d0bcb878SKiyoshi Ueda 			      dm_rq_bio_constructor, tio);
1505cec47e3dSKiyoshi Ueda 	if (r)
1506cec47e3dSKiyoshi Ueda 		return r;
1507cec47e3dSKiyoshi Ueda 
1508cec47e3dSKiyoshi Ueda 	clone->cmd = rq->cmd;
1509cec47e3dSKiyoshi Ueda 	clone->cmd_len = rq->cmd_len;
1510cec47e3dSKiyoshi Ueda 	clone->sense = rq->sense;
1511cec47e3dSKiyoshi Ueda 	clone->buffer = rq->buffer;
1512cec47e3dSKiyoshi Ueda 	clone->end_io = end_clone_request;
1513cec47e3dSKiyoshi Ueda 	clone->end_io_data = tio;
1514cec47e3dSKiyoshi Ueda 
1515cec47e3dSKiyoshi Ueda 	return 0;
1516cec47e3dSKiyoshi Ueda }
1517cec47e3dSKiyoshi Ueda 
15186facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md,
15196facdaffSKiyoshi Ueda 				gfp_t gfp_mask)
15206facdaffSKiyoshi Ueda {
15216facdaffSKiyoshi Ueda 	struct request *clone;
15226facdaffSKiyoshi Ueda 	struct dm_rq_target_io *tio;
15236facdaffSKiyoshi Ueda 
15246facdaffSKiyoshi Ueda 	tio = alloc_rq_tio(md, gfp_mask);
15256facdaffSKiyoshi Ueda 	if (!tio)
15266facdaffSKiyoshi Ueda 		return NULL;
15276facdaffSKiyoshi Ueda 
15286facdaffSKiyoshi Ueda 	tio->md = md;
15296facdaffSKiyoshi Ueda 	tio->ti = NULL;
15306facdaffSKiyoshi Ueda 	tio->orig = rq;
15316facdaffSKiyoshi Ueda 	tio->error = 0;
15326facdaffSKiyoshi Ueda 	memset(&tio->info, 0, sizeof(tio->info));
15336facdaffSKiyoshi Ueda 
15346facdaffSKiyoshi Ueda 	clone = &tio->clone;
15356facdaffSKiyoshi Ueda 	if (setup_clone(clone, rq, tio)) {
15366facdaffSKiyoshi Ueda 		/* -ENOMEM */
15376facdaffSKiyoshi Ueda 		free_rq_tio(tio);
15386facdaffSKiyoshi Ueda 		return NULL;
15396facdaffSKiyoshi Ueda 	}
15406facdaffSKiyoshi Ueda 
15416facdaffSKiyoshi Ueda 	return clone;
15426facdaffSKiyoshi Ueda }
15436facdaffSKiyoshi Ueda 
1544cec47e3dSKiyoshi Ueda /*
1545cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1546cec47e3dSKiyoshi Ueda  */
1547cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq)
1548cec47e3dSKiyoshi Ueda {
1549cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1550cec47e3dSKiyoshi Ueda 	struct request *clone;
1551cec47e3dSKiyoshi Ueda 
1552cec47e3dSKiyoshi Ueda 	if (unlikely(rq->special)) {
1553cec47e3dSKiyoshi Ueda 		DMWARN("Already has something in rq->special.");
1554cec47e3dSKiyoshi Ueda 		return BLKPREP_KILL;
1555cec47e3dSKiyoshi Ueda 	}
1556cec47e3dSKiyoshi Ueda 
15576facdaffSKiyoshi Ueda 	clone = clone_rq(rq, md, GFP_ATOMIC);
15586facdaffSKiyoshi Ueda 	if (!clone)
1559cec47e3dSKiyoshi Ueda 		return BLKPREP_DEFER;
1560cec47e3dSKiyoshi Ueda 
1561cec47e3dSKiyoshi Ueda 	rq->special = clone;
1562cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_DONTPREP;
1563cec47e3dSKiyoshi Ueda 
1564cec47e3dSKiyoshi Ueda 	return BLKPREP_OK;
1565cec47e3dSKiyoshi Ueda }
1566cec47e3dSKiyoshi Ueda 
15679eef87daSKiyoshi Ueda /*
15689eef87daSKiyoshi Ueda  * Returns:
15699eef87daSKiyoshi Ueda  * 0  : the request has been processed (not requeued)
15709eef87daSKiyoshi Ueda  * !0 : the request has been requeued
15719eef87daSKiyoshi Ueda  */
15729eef87daSKiyoshi Ueda static int map_request(struct dm_target *ti, struct request *clone,
1573cec47e3dSKiyoshi Ueda 		       struct mapped_device *md)
1574cec47e3dSKiyoshi Ueda {
15759eef87daSKiyoshi Ueda 	int r, requeued = 0;
1576cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1577cec47e3dSKiyoshi Ueda 
1578cec47e3dSKiyoshi Ueda 	tio->ti = ti;
1579cec47e3dSKiyoshi Ueda 	r = ti->type->map_rq(ti, clone, &tio->info);
1580cec47e3dSKiyoshi Ueda 	switch (r) {
1581cec47e3dSKiyoshi Ueda 	case DM_MAPIO_SUBMITTED:
1582cec47e3dSKiyoshi Ueda 		/* The target has taken the I/O to submit by itself later */
1583cec47e3dSKiyoshi Ueda 		break;
1584cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REMAPPED:
1585cec47e3dSKiyoshi Ueda 		/* The target has remapped the I/O so dispatch it */
15866db4ccd6SJun'ichi Nomura 		trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
15876db4ccd6SJun'ichi Nomura 				     blk_rq_pos(tio->orig));
1588cec47e3dSKiyoshi Ueda 		dm_dispatch_request(clone);
1589cec47e3dSKiyoshi Ueda 		break;
1590cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REQUEUE:
1591cec47e3dSKiyoshi Ueda 		/* The target wants to requeue the I/O */
1592cec47e3dSKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
15939eef87daSKiyoshi Ueda 		requeued = 1;
1594cec47e3dSKiyoshi Ueda 		break;
1595cec47e3dSKiyoshi Ueda 	default:
1596cec47e3dSKiyoshi Ueda 		if (r > 0) {
1597cec47e3dSKiyoshi Ueda 			DMWARN("unimplemented target map return value: %d", r);
1598cec47e3dSKiyoshi Ueda 			BUG();
1599cec47e3dSKiyoshi Ueda 		}
1600cec47e3dSKiyoshi Ueda 
1601cec47e3dSKiyoshi Ueda 		/* The target wants to complete the I/O */
1602cec47e3dSKiyoshi Ueda 		dm_kill_unmapped_request(clone, r);
1603cec47e3dSKiyoshi Ueda 		break;
1604cec47e3dSKiyoshi Ueda 	}
16059eef87daSKiyoshi Ueda 
16069eef87daSKiyoshi Ueda 	return requeued;
1607cec47e3dSKiyoshi Ueda }
1608cec47e3dSKiyoshi Ueda 
1609ba1cbad9SMike Snitzer static struct request *dm_start_request(struct mapped_device *md, struct request *orig)
1610ba1cbad9SMike Snitzer {
1611ba1cbad9SMike Snitzer 	struct request *clone;
1612ba1cbad9SMike Snitzer 
1613ba1cbad9SMike Snitzer 	blk_start_request(orig);
1614ba1cbad9SMike Snitzer 	clone = orig->special;
1615ba1cbad9SMike Snitzer 	atomic_inc(&md->pending[rq_data_dir(clone)]);
1616ba1cbad9SMike Snitzer 
1617ba1cbad9SMike Snitzer 	/*
1618ba1cbad9SMike Snitzer 	 * Hold the md reference here for the in-flight I/O.
1619ba1cbad9SMike Snitzer 	 * We can't rely on the reference count by device opener,
1620ba1cbad9SMike Snitzer 	 * because the device may be closed during the request completion
1621ba1cbad9SMike Snitzer 	 * when all bios are completed.
1622ba1cbad9SMike Snitzer 	 * See the comment in rq_completed() too.
1623ba1cbad9SMike Snitzer 	 */
1624ba1cbad9SMike Snitzer 	dm_get(md);
1625ba1cbad9SMike Snitzer 
1626ba1cbad9SMike Snitzer 	return clone;
1627ba1cbad9SMike Snitzer }
1628ba1cbad9SMike Snitzer 
1629cec47e3dSKiyoshi Ueda /*
1630cec47e3dSKiyoshi Ueda  * q->request_fn for request-based dm.
1631cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1632cec47e3dSKiyoshi Ueda  */
1633cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q)
1634cec47e3dSKiyoshi Ueda {
1635cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
16367c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1637cec47e3dSKiyoshi Ueda 	struct dm_target *ti;
1638b4324feeSKiyoshi Ueda 	struct request *rq, *clone;
163929e4013dSTejun Heo 	sector_t pos;
1640cec47e3dSKiyoshi Ueda 
1641cec47e3dSKiyoshi Ueda 	/*
1642b4324feeSKiyoshi Ueda 	 * For suspend, check blk_queue_stopped() and increment
1643b4324feeSKiyoshi Ueda 	 * ->pending within a single queue_lock not to increment the
1644b4324feeSKiyoshi Ueda 	 * number of in-flight I/Os after the queue is stopped in
1645b4324feeSKiyoshi Ueda 	 * dm_suspend().
1646cec47e3dSKiyoshi Ueda 	 */
16477eaceaccSJens Axboe 	while (!blk_queue_stopped(q)) {
1648cec47e3dSKiyoshi Ueda 		rq = blk_peek_request(q);
1649cec47e3dSKiyoshi Ueda 		if (!rq)
16507eaceaccSJens Axboe 			goto delay_and_out;
1651cec47e3dSKiyoshi Ueda 
165229e4013dSTejun Heo 		/* always use block 0 to find the target for flushes for now */
165329e4013dSTejun Heo 		pos = 0;
165429e4013dSTejun Heo 		if (!(rq->cmd_flags & REQ_FLUSH))
165529e4013dSTejun Heo 			pos = blk_rq_pos(rq);
1656d0bcb878SKiyoshi Ueda 
165729e4013dSTejun Heo 		ti = dm_table_find_target(map, pos);
1658ba1cbad9SMike Snitzer 		if (!dm_target_is_valid(ti)) {
1659ba1cbad9SMike Snitzer 			/*
1660ba1cbad9SMike Snitzer 			 * Must perform setup, that dm_done() requires,
1661ba1cbad9SMike Snitzer 			 * before calling dm_kill_unmapped_request
1662ba1cbad9SMike Snitzer 			 */
1663ba1cbad9SMike Snitzer 			DMERR_LIMIT("request attempted access beyond the end of device");
1664ba1cbad9SMike Snitzer 			clone = dm_start_request(md, rq);
1665ba1cbad9SMike Snitzer 			dm_kill_unmapped_request(clone, -EIO);
1666ba1cbad9SMike Snitzer 			continue;
1667ba1cbad9SMike Snitzer 		}
166829e4013dSTejun Heo 
1669cec47e3dSKiyoshi Ueda 		if (ti->type->busy && ti->type->busy(ti))
16707eaceaccSJens Axboe 			goto delay_and_out;
1671cec47e3dSKiyoshi Ueda 
1672ba1cbad9SMike Snitzer 		clone = dm_start_request(md, rq);
1673b4324feeSKiyoshi Ueda 
1674cec47e3dSKiyoshi Ueda 		spin_unlock(q->queue_lock);
16759eef87daSKiyoshi Ueda 		if (map_request(ti, clone, md))
16769eef87daSKiyoshi Ueda 			goto requeued;
16779eef87daSKiyoshi Ueda 
1678052189a2SKiyoshi Ueda 		BUG_ON(!irqs_disabled());
1679052189a2SKiyoshi Ueda 		spin_lock(q->queue_lock);
1680cec47e3dSKiyoshi Ueda 	}
1681cec47e3dSKiyoshi Ueda 
1682cec47e3dSKiyoshi Ueda 	goto out;
1683cec47e3dSKiyoshi Ueda 
16849eef87daSKiyoshi Ueda requeued:
1685052189a2SKiyoshi Ueda 	BUG_ON(!irqs_disabled());
1686052189a2SKiyoshi Ueda 	spin_lock(q->queue_lock);
16879eef87daSKiyoshi Ueda 
16887eaceaccSJens Axboe delay_and_out:
16897eaceaccSJens Axboe 	blk_delay_queue(q, HZ / 10);
1690cec47e3dSKiyoshi Ueda out:
1691cec47e3dSKiyoshi Ueda 	dm_table_put(map);
1692cec47e3dSKiyoshi Ueda }
1693cec47e3dSKiyoshi Ueda 
1694cec47e3dSKiyoshi Ueda int dm_underlying_device_busy(struct request_queue *q)
1695cec47e3dSKiyoshi Ueda {
1696cec47e3dSKiyoshi Ueda 	return blk_lld_busy(q);
1697cec47e3dSKiyoshi Ueda }
1698cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1699cec47e3dSKiyoshi Ueda 
1700cec47e3dSKiyoshi Ueda static int dm_lld_busy(struct request_queue *q)
1701cec47e3dSKiyoshi Ueda {
1702cec47e3dSKiyoshi Ueda 	int r;
1703cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
17047c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1705cec47e3dSKiyoshi Ueda 
1706cec47e3dSKiyoshi Ueda 	if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1707cec47e3dSKiyoshi Ueda 		r = 1;
1708cec47e3dSKiyoshi Ueda 	else
1709cec47e3dSKiyoshi Ueda 		r = dm_table_any_busy_target(map);
1710cec47e3dSKiyoshi Ueda 
1711cec47e3dSKiyoshi Ueda 	dm_table_put(map);
1712cec47e3dSKiyoshi Ueda 
1713cec47e3dSKiyoshi Ueda 	return r;
1714cec47e3dSKiyoshi Ueda }
1715cec47e3dSKiyoshi Ueda 
17161da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
17171da177e4SLinus Torvalds {
17188a57dfc6SChandra Seetharaman 	int r = bdi_bits;
17198a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
17208a57dfc6SChandra Seetharaman 	struct dm_table *map;
17211da177e4SLinus Torvalds 
17221eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
17237c666411SAlasdair G Kergon 		map = dm_get_live_table(md);
17248a57dfc6SChandra Seetharaman 		if (map) {
1725cec47e3dSKiyoshi Ueda 			/*
1726cec47e3dSKiyoshi Ueda 			 * Request-based dm cares about only own queue for
1727cec47e3dSKiyoshi Ueda 			 * the query about congestion status of request_queue
1728cec47e3dSKiyoshi Ueda 			 */
1729cec47e3dSKiyoshi Ueda 			if (dm_request_based(md))
1730cec47e3dSKiyoshi Ueda 				r = md->queue->backing_dev_info.state &
1731cec47e3dSKiyoshi Ueda 				    bdi_bits;
1732cec47e3dSKiyoshi Ueda 			else
17331da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
1734cec47e3dSKiyoshi Ueda 
17351da177e4SLinus Torvalds 			dm_table_put(map);
17368a57dfc6SChandra Seetharaman 		}
17378a57dfc6SChandra Seetharaman 	}
17388a57dfc6SChandra Seetharaman 
17391da177e4SLinus Torvalds 	return r;
17401da177e4SLinus Torvalds }
17411da177e4SLinus Torvalds 
17421da177e4SLinus Torvalds /*-----------------------------------------------------------------
17431da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
17441da177e4SLinus Torvalds  *---------------------------------------------------------------*/
17452b06cfffSAlasdair G Kergon static void free_minor(int minor)
17461da177e4SLinus Torvalds {
1747f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17481da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1749f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
17501da177e4SLinus Torvalds }
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds /*
17531da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
17541da177e4SLinus Torvalds  */
1755cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
17561da177e4SLinus Torvalds {
1757c9d76be6STejun Heo 	int r;
17581da177e4SLinus Torvalds 
17591da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
17601da177e4SLinus Torvalds 		return -EINVAL;
17611da177e4SLinus Torvalds 
1762c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1763f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17641da177e4SLinus Torvalds 
1765c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
17661da177e4SLinus Torvalds 
1767f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1768c9d76be6STejun Heo 	idr_preload_end();
1769c9d76be6STejun Heo 	if (r < 0)
1770c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
1771c9d76be6STejun Heo 	return 0;
17721da177e4SLinus Torvalds }
17731da177e4SLinus Torvalds 
1774cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
17751da177e4SLinus Torvalds {
1776c9d76be6STejun Heo 	int r;
17771da177e4SLinus Torvalds 
1778c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1779f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17801da177e4SLinus Torvalds 
1781c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
17821da177e4SLinus Torvalds 
1783f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1784c9d76be6STejun Heo 	idr_preload_end();
1785c9d76be6STejun Heo 	if (r < 0)
17861da177e4SLinus Torvalds 		return r;
1787c9d76be6STejun Heo 	*minor = r;
1788c9d76be6STejun Heo 	return 0;
17891da177e4SLinus Torvalds }
17901da177e4SLinus Torvalds 
179183d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
17921da177e4SLinus Torvalds 
179353d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
179453d5914fSMikulas Patocka 
17954a0b4ddfSMike Snitzer static void dm_init_md_queue(struct mapped_device *md)
17964a0b4ddfSMike Snitzer {
17974a0b4ddfSMike Snitzer 	/*
17984a0b4ddfSMike Snitzer 	 * Request-based dm devices cannot be stacked on top of bio-based dm
17994a0b4ddfSMike Snitzer 	 * devices.  The type of this dm device has not been decided yet.
18004a0b4ddfSMike Snitzer 	 * The type is decided at the first table loading time.
18014a0b4ddfSMike Snitzer 	 * To prevent problematic device stacking, clear the queue flag
18024a0b4ddfSMike Snitzer 	 * for request stacking support until then.
18034a0b4ddfSMike Snitzer 	 *
18044a0b4ddfSMike Snitzer 	 * This queue is new, so no concurrency on the queue_flags.
18054a0b4ddfSMike Snitzer 	 */
18064a0b4ddfSMike Snitzer 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
18074a0b4ddfSMike Snitzer 
18084a0b4ddfSMike Snitzer 	md->queue->queuedata = md;
18094a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
18104a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_data = md;
18114a0b4ddfSMike Snitzer 	blk_queue_make_request(md->queue, dm_request);
18124a0b4ddfSMike Snitzer 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
18134a0b4ddfSMike Snitzer 	blk_queue_merge_bvec(md->queue, dm_merge_bvec);
18144a0b4ddfSMike Snitzer }
18154a0b4ddfSMike Snitzer 
18161da177e4SLinus Torvalds /*
18171da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
18181da177e4SLinus Torvalds  */
18192b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
18201da177e4SLinus Torvalds {
18211da177e4SLinus Torvalds 	int r;
1822cf13ab8eSFrederik Deweerdt 	struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1823ba61fdd1SJeff Mahoney 	void *old_md;
18241da177e4SLinus Torvalds 
18251da177e4SLinus Torvalds 	if (!md) {
18261da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
18271da177e4SLinus Torvalds 		return NULL;
18281da177e4SLinus Torvalds 	}
18291da177e4SLinus Torvalds 
183010da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
18316ed7ade8SMilan Broz 		goto bad_module_get;
183210da4f79SJeff Mahoney 
18331da177e4SLinus Torvalds 	/* get a minor number for the dev */
18342b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1835cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
18362b06cfffSAlasdair G Kergon 	else
1837cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
18381da177e4SLinus Torvalds 	if (r < 0)
18396ed7ade8SMilan Broz 		goto bad_minor;
18401da177e4SLinus Torvalds 
1841a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
18422ca3310eSAlasdair G Kergon 	init_rwsem(&md->io_lock);
1843e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1844a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
1845022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
18461da177e4SLinus Torvalds 	rwlock_init(&md->map_lock);
18471da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
18485c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
18491da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
18507a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
18517a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
18527a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
18531da177e4SLinus Torvalds 
18544a0b4ddfSMike Snitzer 	md->queue = blk_alloc_queue(GFP_KERNEL);
18551da177e4SLinus Torvalds 	if (!md->queue)
18566ed7ade8SMilan Broz 		goto bad_queue;
18571da177e4SLinus Torvalds 
18584a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
18599faf400fSStefan Bader 
18601da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
18611da177e4SLinus Torvalds 	if (!md->disk)
18626ed7ade8SMilan Broz 		goto bad_disk;
18631da177e4SLinus Torvalds 
1864316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
1865316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
1866f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
186753d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1868f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
1869f0b04115SJeff Mahoney 
18701da177e4SLinus Torvalds 	md->disk->major = _major;
18711da177e4SLinus Torvalds 	md->disk->first_minor = minor;
18721da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
18731da177e4SLinus Torvalds 	md->disk->queue = md->queue;
18741da177e4SLinus Torvalds 	md->disk->private_data = md;
18751da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
18761da177e4SLinus Torvalds 	add_disk(md->disk);
18777e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
18781da177e4SLinus Torvalds 
18799c4376deSTejun Heo 	md->wq = alloc_workqueue("kdmflush",
18809c4376deSTejun Heo 				 WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
1881304f3f6aSMilan Broz 	if (!md->wq)
1882304f3f6aSMilan Broz 		goto bad_thread;
1883304f3f6aSMilan Broz 
188432a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
188532a926daSMikulas Patocka 	if (!md->bdev)
188632a926daSMikulas Patocka 		goto bad_bdev;
188732a926daSMikulas Patocka 
18886a8736d1STejun Heo 	bio_init(&md->flush_bio);
18896a8736d1STejun Heo 	md->flush_bio.bi_bdev = md->bdev;
18906a8736d1STejun Heo 	md->flush_bio.bi_rw = WRITE_FLUSH;
18916a8736d1STejun Heo 
1892ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1893f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1894ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1895f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1896ba61fdd1SJeff Mahoney 
1897ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1898ba61fdd1SJeff Mahoney 
18991da177e4SLinus Torvalds 	return md;
19001da177e4SLinus Torvalds 
190132a926daSMikulas Patocka bad_bdev:
190232a926daSMikulas Patocka 	destroy_workqueue(md->wq);
1903304f3f6aSMilan Broz bad_thread:
190403022c54SZdenek Kabelac 	del_gendisk(md->disk);
1905304f3f6aSMilan Broz 	put_disk(md->disk);
19066ed7ade8SMilan Broz bad_disk:
19071312f40eSAl Viro 	blk_cleanup_queue(md->queue);
19086ed7ade8SMilan Broz bad_queue:
19091da177e4SLinus Torvalds 	free_minor(minor);
19106ed7ade8SMilan Broz bad_minor:
191110da4f79SJeff Mahoney 	module_put(THIS_MODULE);
19126ed7ade8SMilan Broz bad_module_get:
19131da177e4SLinus Torvalds 	kfree(md);
19141da177e4SLinus Torvalds 	return NULL;
19151da177e4SLinus Torvalds }
19161da177e4SLinus Torvalds 
1917ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1918ae9da83fSJun'ichi Nomura 
19191da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
19201da177e4SLinus Torvalds {
1921f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
192263d94e48SJun'ichi Nomura 
1923ae9da83fSJun'ichi Nomura 	unlock_fs(md);
1924db8fef4fSMikulas Patocka 	bdput(md->bdev);
1925304f3f6aSMilan Broz 	destroy_workqueue(md->wq);
1926e6ee8c0bSKiyoshi Ueda 	if (md->tio_pool)
19271da177e4SLinus Torvalds 		mempool_destroy(md->tio_pool);
1928e6ee8c0bSKiyoshi Ueda 	if (md->io_pool)
19291da177e4SLinus Torvalds 		mempool_destroy(md->io_pool);
1930e6ee8c0bSKiyoshi Ueda 	if (md->bs)
19319faf400fSStefan Bader 		bioset_free(md->bs);
19329c47008dSMartin K. Petersen 	blk_integrity_unregister(md->disk);
19331da177e4SLinus Torvalds 	del_gendisk(md->disk);
193463d94e48SJun'ichi Nomura 	free_minor(minor);
1935fba9f90eSJeff Mahoney 
1936fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
1937fba9f90eSJeff Mahoney 	md->disk->private_data = NULL;
1938fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
1939fba9f90eSJeff Mahoney 
19401da177e4SLinus Torvalds 	put_disk(md->disk);
19411312f40eSAl Viro 	blk_cleanup_queue(md->queue);
194210da4f79SJeff Mahoney 	module_put(THIS_MODULE);
19431da177e4SLinus Torvalds 	kfree(md);
19441da177e4SLinus Torvalds }
19451da177e4SLinus Torvalds 
1946e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1947e6ee8c0bSKiyoshi Ueda {
1948c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
1949e6ee8c0bSKiyoshi Ueda 
1950*16245bdcSJun'ichi Nomura 	if (md->io_pool && md->bs) {
1951*16245bdcSJun'ichi Nomura 		/* The md already has necessary mempools. */
1952*16245bdcSJun'ichi Nomura 		if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
1953c0820cf5SMikulas Patocka 			/*
1954*16245bdcSJun'ichi Nomura 			 * Reload bioset because front_pad may have changed
1955*16245bdcSJun'ichi Nomura 			 * because a different table was loaded.
1956c0820cf5SMikulas Patocka 			 */
1957c0820cf5SMikulas Patocka 			bioset_free(md->bs);
1958c0820cf5SMikulas Patocka 			md->bs = p->bs;
1959c0820cf5SMikulas Patocka 			p->bs = NULL;
1960*16245bdcSJun'ichi Nomura 		} else if (dm_table_get_type(t) == DM_TYPE_REQUEST_BASED) {
1961*16245bdcSJun'ichi Nomura 			BUG_ON(!md->tio_pool);
1962*16245bdcSJun'ichi Nomura 			/*
1963*16245bdcSJun'ichi Nomura 			 * There's no need to reload with request-based dm
1964*16245bdcSJun'ichi Nomura 			 * because the size of front_pad doesn't change.
1965*16245bdcSJun'ichi Nomura 			 * Note for future: If you are to reload bioset,
1966*16245bdcSJun'ichi Nomura 			 * prep-ed requests in the queue may refer
1967*16245bdcSJun'ichi Nomura 			 * to bio from the old bioset, so you must walk
1968*16245bdcSJun'ichi Nomura 			 * through the queue to unprep.
1969*16245bdcSJun'ichi Nomura 			 */
1970*16245bdcSJun'ichi Nomura 		}
1971e6ee8c0bSKiyoshi Ueda 		goto out;
1972c0820cf5SMikulas Patocka 	}
1973e6ee8c0bSKiyoshi Ueda 
1974e6ee8c0bSKiyoshi Ueda 	BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
1975e6ee8c0bSKiyoshi Ueda 
1976e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
1977e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
1978e6ee8c0bSKiyoshi Ueda 	md->tio_pool = p->tio_pool;
1979e6ee8c0bSKiyoshi Ueda 	p->tio_pool = NULL;
1980e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
1981e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
1982e6ee8c0bSKiyoshi Ueda 
1983e6ee8c0bSKiyoshi Ueda out:
1984e6ee8c0bSKiyoshi Ueda 	/* mempool bind completed, now no need any mempools in the table */
1985e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
1986e6ee8c0bSKiyoshi Ueda }
1987e6ee8c0bSKiyoshi Ueda 
19881da177e4SLinus Torvalds /*
19891da177e4SLinus Torvalds  * Bind a table to the device.
19901da177e4SLinus Torvalds  */
19911da177e4SLinus Torvalds static void event_callback(void *context)
19921da177e4SLinus Torvalds {
19937a8c3d3bSMike Anderson 	unsigned long flags;
19947a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
19951da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
19961da177e4SLinus Torvalds 
19977a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
19987a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
19997a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
20007a8c3d3bSMike Anderson 
2001ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
20027a8c3d3bSMike Anderson 
20031da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
20041da177e4SLinus Torvalds 	wake_up(&md->eventq);
20051da177e4SLinus Torvalds }
20061da177e4SLinus Torvalds 
2007c217649bSMike Snitzer /*
2008c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
2009c217649bSMike Snitzer  */
20104e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
20111da177e4SLinus Torvalds {
20124e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
20131da177e4SLinus Torvalds 
2014db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
20151da177e4SLinus Torvalds }
20161da177e4SLinus Torvalds 
2017042d2a9bSAlasdair G Kergon /*
2018d5b9dd04SMikulas Patocka  * Return 1 if the queue has a compulsory merge_bvec_fn function.
2019d5b9dd04SMikulas Patocka  *
2020d5b9dd04SMikulas Patocka  * If this function returns 0, then the device is either a non-dm
2021d5b9dd04SMikulas Patocka  * device without a merge_bvec_fn, or it is a dm device that is
2022d5b9dd04SMikulas Patocka  * able to split any bios it receives that are too big.
2023d5b9dd04SMikulas Patocka  */
2024d5b9dd04SMikulas Patocka int dm_queue_merge_is_compulsory(struct request_queue *q)
2025d5b9dd04SMikulas Patocka {
2026d5b9dd04SMikulas Patocka 	struct mapped_device *dev_md;
2027d5b9dd04SMikulas Patocka 
2028d5b9dd04SMikulas Patocka 	if (!q->merge_bvec_fn)
2029d5b9dd04SMikulas Patocka 		return 0;
2030d5b9dd04SMikulas Patocka 
2031d5b9dd04SMikulas Patocka 	if (q->make_request_fn == dm_request) {
2032d5b9dd04SMikulas Patocka 		dev_md = q->queuedata;
2033d5b9dd04SMikulas Patocka 		if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2034d5b9dd04SMikulas Patocka 			return 0;
2035d5b9dd04SMikulas Patocka 	}
2036d5b9dd04SMikulas Patocka 
2037d5b9dd04SMikulas Patocka 	return 1;
2038d5b9dd04SMikulas Patocka }
2039d5b9dd04SMikulas Patocka 
2040d5b9dd04SMikulas Patocka static int dm_device_merge_is_compulsory(struct dm_target *ti,
2041d5b9dd04SMikulas Patocka 					 struct dm_dev *dev, sector_t start,
2042d5b9dd04SMikulas Patocka 					 sector_t len, void *data)
2043d5b9dd04SMikulas Patocka {
2044d5b9dd04SMikulas Patocka 	struct block_device *bdev = dev->bdev;
2045d5b9dd04SMikulas Patocka 	struct request_queue *q = bdev_get_queue(bdev);
2046d5b9dd04SMikulas Patocka 
2047d5b9dd04SMikulas Patocka 	return dm_queue_merge_is_compulsory(q);
2048d5b9dd04SMikulas Patocka }
2049d5b9dd04SMikulas Patocka 
2050d5b9dd04SMikulas Patocka /*
2051d5b9dd04SMikulas Patocka  * Return 1 if it is acceptable to ignore merge_bvec_fn based
2052d5b9dd04SMikulas Patocka  * on the properties of the underlying devices.
2053d5b9dd04SMikulas Patocka  */
2054d5b9dd04SMikulas Patocka static int dm_table_merge_is_optional(struct dm_table *table)
2055d5b9dd04SMikulas Patocka {
2056d5b9dd04SMikulas Patocka 	unsigned i = 0;
2057d5b9dd04SMikulas Patocka 	struct dm_target *ti;
2058d5b9dd04SMikulas Patocka 
2059d5b9dd04SMikulas Patocka 	while (i < dm_table_get_num_targets(table)) {
2060d5b9dd04SMikulas Patocka 		ti = dm_table_get_target(table, i++);
2061d5b9dd04SMikulas Patocka 
2062d5b9dd04SMikulas Patocka 		if (ti->type->iterate_devices &&
2063d5b9dd04SMikulas Patocka 		    ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2064d5b9dd04SMikulas Patocka 			return 0;
2065d5b9dd04SMikulas Patocka 	}
2066d5b9dd04SMikulas Patocka 
2067d5b9dd04SMikulas Patocka 	return 1;
2068d5b9dd04SMikulas Patocka }
2069d5b9dd04SMikulas Patocka 
2070d5b9dd04SMikulas Patocka /*
2071042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2072042d2a9bSAlasdair G Kergon  */
2073042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2074754c5fc7SMike Snitzer 			       struct queue_limits *limits)
20751da177e4SLinus Torvalds {
2076042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
2077165125e1SJens Axboe 	struct request_queue *q = md->queue;
20781da177e4SLinus Torvalds 	sector_t size;
2079523d9297SKiyoshi Ueda 	unsigned long flags;
2080d5b9dd04SMikulas Patocka 	int merge_is_optional;
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds 	size = dm_table_get_size(t);
20833ac51e74SDarrick J. Wong 
20843ac51e74SDarrick J. Wong 	/*
20853ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
20863ac51e74SDarrick J. Wong 	 */
20873ac51e74SDarrick J. Wong 	if (size != get_capacity(md->disk))
20883ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
20893ac51e74SDarrick J. Wong 
20904e90188bSAlasdair G Kergon 	__set_size(md, size);
20911da177e4SLinus Torvalds 
2092cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
20932ca3310eSAlasdair G Kergon 
2094e6ee8c0bSKiyoshi Ueda 	/*
2095e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
2096e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
2097e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
2098e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
2099e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
2100e6ee8c0bSKiyoshi Ueda 	 */
2101e6ee8c0bSKiyoshi Ueda 	if (dm_table_request_based(t) && !blk_queue_stopped(q))
2102e6ee8c0bSKiyoshi Ueda 		stop_queue(q);
2103e6ee8c0bSKiyoshi Ueda 
2104e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
2105e6ee8c0bSKiyoshi Ueda 
2106d5b9dd04SMikulas Patocka 	merge_is_optional = dm_table_merge_is_optional(t);
2107d5b9dd04SMikulas Patocka 
2108523d9297SKiyoshi Ueda 	write_lock_irqsave(&md->map_lock, flags);
2109042d2a9bSAlasdair G Kergon 	old_map = md->map;
21102ca3310eSAlasdair G Kergon 	md->map = t;
211136a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
211236a0456fSAlasdair G Kergon 
2113754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
2114d5b9dd04SMikulas Patocka 	if (merge_is_optional)
2115d5b9dd04SMikulas Patocka 		set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2116d5b9dd04SMikulas Patocka 	else
2117d5b9dd04SMikulas Patocka 		clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2118523d9297SKiyoshi Ueda 	write_unlock_irqrestore(&md->map_lock, flags);
21192ca3310eSAlasdair G Kergon 
2120042d2a9bSAlasdair G Kergon 	return old_map;
21211da177e4SLinus Torvalds }
21221da177e4SLinus Torvalds 
2123a7940155SAlasdair G Kergon /*
2124a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2125a7940155SAlasdair G Kergon  */
2126a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
21271da177e4SLinus Torvalds {
21281da177e4SLinus Torvalds 	struct dm_table *map = md->map;
2129523d9297SKiyoshi Ueda 	unsigned long flags;
21301da177e4SLinus Torvalds 
21311da177e4SLinus Torvalds 	if (!map)
2132a7940155SAlasdair G Kergon 		return NULL;
21331da177e4SLinus Torvalds 
21341da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
2135523d9297SKiyoshi Ueda 	write_lock_irqsave(&md->map_lock, flags);
21361da177e4SLinus Torvalds 	md->map = NULL;
2137523d9297SKiyoshi Ueda 	write_unlock_irqrestore(&md->map_lock, flags);
2138a7940155SAlasdair G Kergon 
2139a7940155SAlasdair G Kergon 	return map;
21401da177e4SLinus Torvalds }
21411da177e4SLinus Torvalds 
21421da177e4SLinus Torvalds /*
21431da177e4SLinus Torvalds  * Constructor for a new device.
21441da177e4SLinus Torvalds  */
21452b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
21461da177e4SLinus Torvalds {
21471da177e4SLinus Torvalds 	struct mapped_device *md;
21481da177e4SLinus Torvalds 
21492b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
21501da177e4SLinus Torvalds 	if (!md)
21511da177e4SLinus Torvalds 		return -ENXIO;
21521da177e4SLinus Torvalds 
2153784aae73SMilan Broz 	dm_sysfs_init(md);
2154784aae73SMilan Broz 
21551da177e4SLinus Torvalds 	*result = md;
21561da177e4SLinus Torvalds 	return 0;
21571da177e4SLinus Torvalds }
21581da177e4SLinus Torvalds 
2159a5664dadSMike Snitzer /*
2160a5664dadSMike Snitzer  * Functions to manage md->type.
2161a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2162a5664dadSMike Snitzer  */
2163a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2164a5664dadSMike Snitzer {
2165a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2166a5664dadSMike Snitzer }
2167a5664dadSMike Snitzer 
2168a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2169a5664dadSMike Snitzer {
2170a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2171a5664dadSMike Snitzer }
2172a5664dadSMike Snitzer 
2173a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type)
2174a5664dadSMike Snitzer {
2175a5664dadSMike Snitzer 	md->type = type;
2176a5664dadSMike Snitzer }
2177a5664dadSMike Snitzer 
2178a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md)
2179a5664dadSMike Snitzer {
2180a5664dadSMike Snitzer 	return md->type;
2181a5664dadSMike Snitzer }
2182a5664dadSMike Snitzer 
218336a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
218436a0456fSAlasdair G Kergon {
218536a0456fSAlasdair G Kergon 	return md->immutable_target_type;
218636a0456fSAlasdair G Kergon }
218736a0456fSAlasdair G Kergon 
21884a0b4ddfSMike Snitzer /*
21894a0b4ddfSMike Snitzer  * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
21904a0b4ddfSMike Snitzer  */
21914a0b4ddfSMike Snitzer static int dm_init_request_based_queue(struct mapped_device *md)
21924a0b4ddfSMike Snitzer {
21934a0b4ddfSMike Snitzer 	struct request_queue *q = NULL;
21944a0b4ddfSMike Snitzer 
21954a0b4ddfSMike Snitzer 	if (md->queue->elevator)
21964a0b4ddfSMike Snitzer 		return 1;
21974a0b4ddfSMike Snitzer 
21984a0b4ddfSMike Snitzer 	/* Fully initialize the queue */
21994a0b4ddfSMike Snitzer 	q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
22004a0b4ddfSMike Snitzer 	if (!q)
22014a0b4ddfSMike Snitzer 		return 0;
22024a0b4ddfSMike Snitzer 
22034a0b4ddfSMike Snitzer 	md->queue = q;
22044a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
22054a0b4ddfSMike Snitzer 	blk_queue_softirq_done(md->queue, dm_softirq_done);
22064a0b4ddfSMike Snitzer 	blk_queue_prep_rq(md->queue, dm_prep_fn);
22074a0b4ddfSMike Snitzer 	blk_queue_lld_busy(md->queue, dm_lld_busy);
22084a0b4ddfSMike Snitzer 
22094a0b4ddfSMike Snitzer 	elv_register_queue(md->queue);
22104a0b4ddfSMike Snitzer 
22114a0b4ddfSMike Snitzer 	return 1;
22124a0b4ddfSMike Snitzer }
22134a0b4ddfSMike Snitzer 
22144a0b4ddfSMike Snitzer /*
22154a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
22164a0b4ddfSMike Snitzer  */
22174a0b4ddfSMike Snitzer int dm_setup_md_queue(struct mapped_device *md)
22184a0b4ddfSMike Snitzer {
22194a0b4ddfSMike Snitzer 	if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
22204a0b4ddfSMike Snitzer 	    !dm_init_request_based_queue(md)) {
22214a0b4ddfSMike Snitzer 		DMWARN("Cannot initialize queue for request-based mapped device");
22224a0b4ddfSMike Snitzer 		return -EINVAL;
22234a0b4ddfSMike Snitzer 	}
22244a0b4ddfSMike Snitzer 
22254a0b4ddfSMike Snitzer 	return 0;
22264a0b4ddfSMike Snitzer }
22274a0b4ddfSMike Snitzer 
2228637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev)
22291da177e4SLinus Torvalds {
22301da177e4SLinus Torvalds 	struct mapped_device *md;
22311da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
22321da177e4SLinus Torvalds 
22331da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
22341da177e4SLinus Torvalds 		return NULL;
22351da177e4SLinus Torvalds 
2236f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
22371da177e4SLinus Torvalds 
22381da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
2239fba9f90eSJeff Mahoney 	if (md && (md == MINOR_ALLOCED ||
2240f331c029STejun Heo 		   (MINOR(disk_devt(dm_disk(md))) != minor) ||
2241abdc568bSKiyoshi Ueda 		   dm_deleting_md(md) ||
2242fba9f90eSJeff Mahoney 		   test_bit(DMF_FREEING, &md->flags))) {
2243637842cfSDavid Teigland 		md = NULL;
2244fba9f90eSJeff Mahoney 		goto out;
2245fba9f90eSJeff Mahoney 	}
22461da177e4SLinus Torvalds 
2247fba9f90eSJeff Mahoney out:
2248f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
22491da177e4SLinus Torvalds 
2250637842cfSDavid Teigland 	return md;
2251637842cfSDavid Teigland }
2252637842cfSDavid Teigland 
2253d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev)
2254d229a958SDavid Teigland {
2255d229a958SDavid Teigland 	struct mapped_device *md = dm_find_md(dev);
2256d229a958SDavid Teigland 
2257d229a958SDavid Teigland 	if (md)
2258d229a958SDavid Teigland 		dm_get(md);
2259d229a958SDavid Teigland 
2260d229a958SDavid Teigland 	return md;
2261d229a958SDavid Teigland }
22623cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2263d229a958SDavid Teigland 
22649ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2265637842cfSDavid Teigland {
22669ade92a9SAlasdair G Kergon 	return md->interface_ptr;
22671da177e4SLinus Torvalds }
22681da177e4SLinus Torvalds 
22691da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
22701da177e4SLinus Torvalds {
22711da177e4SLinus Torvalds 	md->interface_ptr = ptr;
22721da177e4SLinus Torvalds }
22731da177e4SLinus Torvalds 
22741da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
22751da177e4SLinus Torvalds {
22761da177e4SLinus Torvalds 	atomic_inc(&md->holders);
22773f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
22781da177e4SLinus Torvalds }
22791da177e4SLinus Torvalds 
228072d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
228172d94861SAlasdair G Kergon {
228272d94861SAlasdair G Kergon 	return md->name;
228372d94861SAlasdair G Kergon }
228472d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
228572d94861SAlasdair G Kergon 
22863f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
22871da177e4SLinus Torvalds {
22881134e5aeSMike Anderson 	struct dm_table *map;
22891da177e4SLinus Torvalds 
22903f77316dSKiyoshi Ueda 	might_sleep();
2291fba9f90eSJeff Mahoney 
22923f77316dSKiyoshi Ueda 	spin_lock(&_minor_lock);
22937c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
22943f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2295fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2296f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
22973f77316dSKiyoshi Ueda 
22984f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
22991da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
23001da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
23011da177e4SLinus Torvalds 	}
23023f77316dSKiyoshi Ueda 
23033f77316dSKiyoshi Ueda 	/*
23043f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
23053f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
23063f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
23073f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
23083f77316dSKiyoshi Ueda 	 */
23093f77316dSKiyoshi Ueda 	if (wait)
23103f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
23113f77316dSKiyoshi Ueda 			msleep(1);
23123f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
23133f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
23143f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
23153f77316dSKiyoshi Ueda 
2316784aae73SMilan Broz 	dm_sysfs_exit(md);
23171134e5aeSMike Anderson 	dm_table_put(map);
2318a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
23191da177e4SLinus Torvalds 	free_dev(md);
23201da177e4SLinus Torvalds }
23213f77316dSKiyoshi Ueda 
23223f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
23233f77316dSKiyoshi Ueda {
23243f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
23253f77316dSKiyoshi Ueda }
23263f77316dSKiyoshi Ueda 
23273f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
23283f77316dSKiyoshi Ueda {
23293f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
23303f77316dSKiyoshi Ueda }
23313f77316dSKiyoshi Ueda 
23323f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
23333f77316dSKiyoshi Ueda {
23343f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
23351da177e4SLinus Torvalds }
233679eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
23371da177e4SLinus Torvalds 
2338401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
233946125c1cSMilan Broz {
234046125c1cSMilan Broz 	int r = 0;
2341b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
2342b44ebeb0SMikulas Patocka 
2343b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
234446125c1cSMilan Broz 
234546125c1cSMilan Broz 	while (1) {
2346401600dfSMikulas Patocka 		set_current_state(interruptible);
234746125c1cSMilan Broz 
2348b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
234946125c1cSMilan Broz 			break;
235046125c1cSMilan Broz 
2351401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
2352401600dfSMikulas Patocka 		    signal_pending(current)) {
235346125c1cSMilan Broz 			r = -EINTR;
235446125c1cSMilan Broz 			break;
235546125c1cSMilan Broz 		}
235646125c1cSMilan Broz 
235746125c1cSMilan Broz 		io_schedule();
235846125c1cSMilan Broz 	}
235946125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
236046125c1cSMilan Broz 
2361b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
2362b44ebeb0SMikulas Patocka 
236346125c1cSMilan Broz 	return r;
236446125c1cSMilan Broz }
236546125c1cSMilan Broz 
23661da177e4SLinus Torvalds /*
23671da177e4SLinus Torvalds  * Process the deferred bios
23681da177e4SLinus Torvalds  */
2369ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
23701da177e4SLinus Torvalds {
2371ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2372ef208587SMikulas Patocka 						work);
23736d6f10dfSMilan Broz 	struct bio *c;
23741da177e4SLinus Torvalds 
23756a8736d1STejun Heo 	down_read(&md->io_lock);
2376ef208587SMikulas Patocka 
23773b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2378022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2379022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2380022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2381022c2611SMikulas Patocka 
23826a8736d1STejun Heo 		if (!c)
2383df12ee99SAlasdair G Kergon 			break;
238473d410c0SMilan Broz 
23856a8736d1STejun Heo 		up_read(&md->io_lock);
23863b00b203SMikulas Patocka 
2387e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2388e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2389af7e466aSMikulas Patocka 		else
2390df12ee99SAlasdair G Kergon 			__split_and_process_bio(md, c);
23916a8736d1STejun Heo 
23926a8736d1STejun Heo 		down_read(&md->io_lock);
2393e6ee8c0bSKiyoshi Ueda 	}
23943b00b203SMikulas Patocka 
23956a8736d1STejun Heo 	up_read(&md->io_lock);
23961da177e4SLinus Torvalds }
23971da177e4SLinus Torvalds 
23989a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2399304f3f6aSMilan Broz {
24003b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
24013b00b203SMikulas Patocka 	smp_mb__after_clear_bit();
240253d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2403304f3f6aSMilan Broz }
2404304f3f6aSMilan Broz 
24051da177e4SLinus Torvalds /*
2406042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
24071da177e4SLinus Torvalds  */
2408042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
24091da177e4SLinus Torvalds {
24103ae70656SMike Snitzer 	struct dm_table *live_map, *map = ERR_PTR(-EINVAL);
2411754c5fc7SMike Snitzer 	struct queue_limits limits;
2412042d2a9bSAlasdair G Kergon 	int r;
24131da177e4SLinus Torvalds 
2414e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
24151da177e4SLinus Torvalds 
24161da177e4SLinus Torvalds 	/* device must be suspended */
24174f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
241893c534aeSAlasdair G Kergon 		goto out;
24191da177e4SLinus Torvalds 
24203ae70656SMike Snitzer 	/*
24213ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
24223ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
24233ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
24243ae70656SMike Snitzer 	 * reappear.
24253ae70656SMike Snitzer 	 */
24263ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
24273ae70656SMike Snitzer 		live_map = dm_get_live_table(md);
24283ae70656SMike Snitzer 		if (live_map)
24293ae70656SMike Snitzer 			limits = md->queue->limits;
24303ae70656SMike Snitzer 		dm_table_put(live_map);
24313ae70656SMike Snitzer 	}
24323ae70656SMike Snitzer 
2433754c5fc7SMike Snitzer 	r = dm_calculate_queue_limits(table, &limits);
2434042d2a9bSAlasdair G Kergon 	if (r) {
2435042d2a9bSAlasdair G Kergon 		map = ERR_PTR(r);
2436754c5fc7SMike Snitzer 		goto out;
2437042d2a9bSAlasdair G Kergon 	}
2438754c5fc7SMike Snitzer 
2439042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
24401da177e4SLinus Torvalds 
244193c534aeSAlasdair G Kergon out:
2442e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2443042d2a9bSAlasdair G Kergon 	return map;
24441da177e4SLinus Torvalds }
24451da177e4SLinus Torvalds 
24461da177e4SLinus Torvalds /*
24471da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
24481da177e4SLinus Torvalds  * device.
24491da177e4SLinus Torvalds  */
24502ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
24511da177e4SLinus Torvalds {
2452e39e2e95SAlasdair G Kergon 	int r;
24531da177e4SLinus Torvalds 
24541da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2455dfbe03f6SAlasdair G Kergon 
2456db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2457dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2458cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2459e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2460e39e2e95SAlasdair G Kergon 		return r;
2461dfbe03f6SAlasdair G Kergon 	}
2462dfbe03f6SAlasdair G Kergon 
2463aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2464aa8d7c2fSAlasdair G Kergon 
24651da177e4SLinus Torvalds 	return 0;
24661da177e4SLinus Torvalds }
24671da177e4SLinus Torvalds 
24682ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
24691da177e4SLinus Torvalds {
2470aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2471aa8d7c2fSAlasdair G Kergon 		return;
2472aa8d7c2fSAlasdair G Kergon 
2473db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
24741da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2475aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
24761da177e4SLinus Torvalds }
24771da177e4SLinus Torvalds 
24781da177e4SLinus Torvalds /*
24791da177e4SLinus Torvalds  * We need to be able to change a mapping table under a mounted
24801da177e4SLinus Torvalds  * filesystem.  For example we might want to move some data in
24811da177e4SLinus Torvalds  * the background.  Before the table can be swapped with
24821da177e4SLinus Torvalds  * dm_bind_table, dm_suspend must be called to flush any in
24831da177e4SLinus Torvalds  * flight bios and ensure that any further io gets deferred.
24841da177e4SLinus Torvalds  */
2485cec47e3dSKiyoshi Ueda /*
2486cec47e3dSKiyoshi Ueda  * Suspend mechanism in request-based dm.
2487cec47e3dSKiyoshi Ueda  *
24889f518b27SKiyoshi Ueda  * 1. Flush all I/Os by lock_fs() if needed.
24899f518b27SKiyoshi Ueda  * 2. Stop dispatching any I/O by stopping the request_queue.
24909f518b27SKiyoshi Ueda  * 3. Wait for all in-flight I/Os to be completed or requeued.
2491cec47e3dSKiyoshi Ueda  *
24929f518b27SKiyoshi Ueda  * To abort suspend, start the request_queue.
2493cec47e3dSKiyoshi Ueda  */
2494a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
24951da177e4SLinus Torvalds {
24962ca3310eSAlasdair G Kergon 	struct dm_table *map = NULL;
249746125c1cSMilan Broz 	int r = 0;
2498a3d77d35SKiyoshi Ueda 	int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
24992e93ccc1SKiyoshi Ueda 	int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
25001da177e4SLinus Torvalds 
2501e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
25022ca3310eSAlasdair G Kergon 
25034f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
250473d410c0SMilan Broz 		r = -EINVAL;
2505d287483dSAlasdair G Kergon 		goto out_unlock;
250673d410c0SMilan Broz 	}
25071da177e4SLinus Torvalds 
25087c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
2509cf222b37SAlasdair G Kergon 
25102e93ccc1SKiyoshi Ueda 	/*
25112e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
25122e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
25132e93ccc1SKiyoshi Ueda 	 */
25142e93ccc1SKiyoshi Ueda 	if (noflush)
25152e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
25162e93ccc1SKiyoshi Ueda 
2517436d4108SAlasdair G Kergon 	/* This does not get reverted if there's an error later. */
25181da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
25191da177e4SLinus Torvalds 
25202e93ccc1SKiyoshi Ueda 	/*
25219f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
25229f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
25239f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
25249f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
25252e93ccc1SKiyoshi Ueda 	 */
252632a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
25272ca3310eSAlasdair G Kergon 		r = lock_fs(md);
25282ca3310eSAlasdair G Kergon 		if (r)
25292ca3310eSAlasdair G Kergon 			goto out;
2530aa8d7c2fSAlasdair G Kergon 	}
25311da177e4SLinus Torvalds 
25321da177e4SLinus Torvalds 	/*
25333b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
25343b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
25353b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
25363b00b203SMikulas Patocka 	 * dm_wq_work.
25373b00b203SMikulas Patocka 	 *
25383b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
25393b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
25406a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
25416a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
25426a8736d1STejun Heo 	 * flush_workqueue(md->wq).
25431da177e4SLinus Torvalds 	 */
25442ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
25451eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
25462ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
25471da177e4SLinus Torvalds 
2548d0bcb878SKiyoshi Ueda 	/*
254929e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
255029e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2551d0bcb878SKiyoshi Ueda 	 */
2552cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
25539f518b27SKiyoshi Ueda 		stop_queue(md->queue);
2554cec47e3dSKiyoshi Ueda 
2555d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2556d0bcb878SKiyoshi Ueda 
25571da177e4SLinus Torvalds 	/*
25583b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
25593b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
25603b00b203SMikulas Patocka 	 * to finish.
25611da177e4SLinus Torvalds 	 */
2562401600dfSMikulas Patocka 	r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
25631da177e4SLinus Torvalds 
25642ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
25656d6f10dfSMilan Broz 	if (noflush)
2566022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
256794d6351eSMilan Broz 	up_write(&md->io_lock);
25682e93ccc1SKiyoshi Ueda 
25691da177e4SLinus Torvalds 	/* were we interrupted ? */
257046125c1cSMilan Broz 	if (r < 0) {
25719a1fb464SMikulas Patocka 		dm_queue_flush(md);
257273d410c0SMilan Broz 
2573cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
25749f518b27SKiyoshi Ueda 			start_queue(md->queue);
2575cec47e3dSKiyoshi Ueda 
25762ca3310eSAlasdair G Kergon 		unlock_fs(md);
25772e93ccc1SKiyoshi Ueda 		goto out; /* pushback list is already flushed, so skip flush */
25782ca3310eSAlasdair G Kergon 	}
25792ca3310eSAlasdair G Kergon 
25803b00b203SMikulas Patocka 	/*
25813b00b203SMikulas Patocka 	 * If dm_wait_for_completion returned 0, the device is completely
25823b00b203SMikulas Patocka 	 * quiescent now. There is no request-processing activity. All new
25833b00b203SMikulas Patocka 	 * requests are being added to md->deferred list.
25843b00b203SMikulas Patocka 	 */
25853b00b203SMikulas Patocka 
25861da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
25871da177e4SLinus Torvalds 
25884d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
25894d4471cbSKiyoshi Ueda 
25902ca3310eSAlasdair G Kergon out:
25911da177e4SLinus Torvalds 	dm_table_put(map);
2592d287483dSAlasdair G Kergon 
2593d287483dSAlasdair G Kergon out_unlock:
2594e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2595cf222b37SAlasdair G Kergon 	return r;
25961da177e4SLinus Torvalds }
25971da177e4SLinus Torvalds 
25981da177e4SLinus Torvalds int dm_resume(struct mapped_device *md)
25991da177e4SLinus Torvalds {
2600cf222b37SAlasdair G Kergon 	int r = -EINVAL;
2601cf222b37SAlasdair G Kergon 	struct dm_table *map = NULL;
26021da177e4SLinus Torvalds 
2603e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
26044f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
2605cf222b37SAlasdair G Kergon 		goto out;
2606cf222b37SAlasdair G Kergon 
26077c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
26082ca3310eSAlasdair G Kergon 	if (!map || !dm_table_get_size(map))
2609cf222b37SAlasdair G Kergon 		goto out;
26101da177e4SLinus Torvalds 
26118757b776SMilan Broz 	r = dm_table_resume_targets(map);
26128757b776SMilan Broz 	if (r)
26138757b776SMilan Broz 		goto out;
26142ca3310eSAlasdair G Kergon 
26159a1fb464SMikulas Patocka 	dm_queue_flush(md);
26162ca3310eSAlasdair G Kergon 
2617cec47e3dSKiyoshi Ueda 	/*
2618cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2619cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2620cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2621cec47e3dSKiyoshi Ueda 	 */
2622cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2623cec47e3dSKiyoshi Ueda 		start_queue(md->queue);
2624cec47e3dSKiyoshi Ueda 
26252ca3310eSAlasdair G Kergon 	unlock_fs(md);
26262ca3310eSAlasdair G Kergon 
26272ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
26282ca3310eSAlasdair G Kergon 
2629cf222b37SAlasdair G Kergon 	r = 0;
2630cf222b37SAlasdair G Kergon out:
2631cf222b37SAlasdair G Kergon 	dm_table_put(map);
2632e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
26332ca3310eSAlasdair G Kergon 
2634cf222b37SAlasdair G Kergon 	return r;
26351da177e4SLinus Torvalds }
26361da177e4SLinus Torvalds 
26371da177e4SLinus Torvalds /*-----------------------------------------------------------------
26381da177e4SLinus Torvalds  * Event notification.
26391da177e4SLinus Torvalds  *---------------------------------------------------------------*/
26403abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
264160935eb2SMilan Broz 		       unsigned cookie)
264269267a30SAlasdair G Kergon {
264360935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
264460935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
264560935eb2SMilan Broz 
264660935eb2SMilan Broz 	if (!cookie)
26473abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
264860935eb2SMilan Broz 	else {
264960935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
265060935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
26513abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
26523abf85b5SPeter Rajnoha 					  action, envp);
265360935eb2SMilan Broz 	}
265469267a30SAlasdair G Kergon }
265569267a30SAlasdair G Kergon 
26567a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
26577a8c3d3bSMike Anderson {
26587a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
26597a8c3d3bSMike Anderson }
26607a8c3d3bSMike Anderson 
26611da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
26621da177e4SLinus Torvalds {
26631da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
26641da177e4SLinus Torvalds }
26651da177e4SLinus Torvalds 
26661da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
26671da177e4SLinus Torvalds {
26681da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
26691da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
26701da177e4SLinus Torvalds }
26711da177e4SLinus Torvalds 
26727a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
26737a8c3d3bSMike Anderson {
26747a8c3d3bSMike Anderson 	unsigned long flags;
26757a8c3d3bSMike Anderson 
26767a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
26777a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
26787a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
26797a8c3d3bSMike Anderson }
26807a8c3d3bSMike Anderson 
26811da177e4SLinus Torvalds /*
26821da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
26831da177e4SLinus Torvalds  * count on 'md'.
26841da177e4SLinus Torvalds  */
26851da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
26861da177e4SLinus Torvalds {
26871da177e4SLinus Torvalds 	return md->disk;
26881da177e4SLinus Torvalds }
26891da177e4SLinus Torvalds 
2690784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2691784aae73SMilan Broz {
2692784aae73SMilan Broz 	return &md->kobj;
2693784aae73SMilan Broz }
2694784aae73SMilan Broz 
2695784aae73SMilan Broz /*
2696784aae73SMilan Broz  * struct mapped_device should not be exported outside of dm.c
2697784aae73SMilan Broz  * so use this check to verify that kobj is part of md structure
2698784aae73SMilan Broz  */
2699784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2700784aae73SMilan Broz {
2701784aae73SMilan Broz 	struct mapped_device *md;
2702784aae73SMilan Broz 
2703784aae73SMilan Broz 	md = container_of(kobj, struct mapped_device, kobj);
2704784aae73SMilan Broz 	if (&md->kobj != kobj)
2705784aae73SMilan Broz 		return NULL;
2706784aae73SMilan Broz 
27074d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
2708432a212cSMike Anderson 	    dm_deleting_md(md))
27094d89b7b4SMilan Broz 		return NULL;
27104d89b7b4SMilan Broz 
2711784aae73SMilan Broz 	dm_get(md);
2712784aae73SMilan Broz 	return md;
2713784aae73SMilan Broz }
2714784aae73SMilan Broz 
27154f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
27161da177e4SLinus Torvalds {
27171da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
27181da177e4SLinus Torvalds }
27191da177e4SLinus Torvalds 
272064dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
272164dbce58SKiyoshi Ueda {
2722ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
272364dbce58SKiyoshi Ueda }
272464dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
272564dbce58SKiyoshi Ueda 
27262e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
27272e93ccc1SKiyoshi Ueda {
2728ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
27292e93ccc1SKiyoshi Ueda }
27302e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
27312e93ccc1SKiyoshi Ueda 
2732c0820cf5SMikulas Patocka struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity, unsigned per_bio_data_size)
2733e6ee8c0bSKiyoshi Ueda {
2734e6ee8c0bSKiyoshi Ueda 	struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2735a91a2785SMartin K. Petersen 	unsigned int pool_size = (type == DM_TYPE_BIO_BASED) ? 16 : MIN_IOS;
2736e6ee8c0bSKiyoshi Ueda 
2737e6ee8c0bSKiyoshi Ueda 	if (!pools)
2738e6ee8c0bSKiyoshi Ueda 		return NULL;
2739e6ee8c0bSKiyoshi Ueda 
2740c0820cf5SMikulas Patocka 	per_bio_data_size = roundup(per_bio_data_size, __alignof__(struct dm_target_io));
2741c0820cf5SMikulas Patocka 
2742e6ee8c0bSKiyoshi Ueda 	pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2743e6ee8c0bSKiyoshi Ueda 			 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2744e6ee8c0bSKiyoshi Ueda 			 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2745e6ee8c0bSKiyoshi Ueda 	if (!pools->io_pool)
2746e6ee8c0bSKiyoshi Ueda 		goto free_pools_and_out;
2747e6ee8c0bSKiyoshi Ueda 
2748dba14160SMikulas Patocka 	pools->tio_pool = NULL;
2749dba14160SMikulas Patocka 	if (type == DM_TYPE_REQUEST_BASED) {
2750dba14160SMikulas Patocka 		pools->tio_pool = mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2751e6ee8c0bSKiyoshi Ueda 		if (!pools->tio_pool)
2752e6ee8c0bSKiyoshi Ueda 			goto free_io_pool_and_out;
2753dba14160SMikulas Patocka 	}
2754e6ee8c0bSKiyoshi Ueda 
275594818742SKent Overstreet 	pools->bs = (type == DM_TYPE_BIO_BASED) ?
2756dba14160SMikulas Patocka 		bioset_create(pool_size,
2757c0820cf5SMikulas Patocka 			      per_bio_data_size + offsetof(struct dm_target_io, clone)) :
275894818742SKent Overstreet 		bioset_create(pool_size,
275994818742SKent Overstreet 			      offsetof(struct dm_rq_clone_bio_info, clone));
2760e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
2761e6ee8c0bSKiyoshi Ueda 		goto free_tio_pool_and_out;
2762e6ee8c0bSKiyoshi Ueda 
2763a91a2785SMartin K. Petersen 	if (integrity && bioset_integrity_create(pools->bs, pool_size))
2764a91a2785SMartin K. Petersen 		goto free_bioset_and_out;
2765a91a2785SMartin K. Petersen 
2766e6ee8c0bSKiyoshi Ueda 	return pools;
2767e6ee8c0bSKiyoshi Ueda 
2768a91a2785SMartin K. Petersen free_bioset_and_out:
2769a91a2785SMartin K. Petersen 	bioset_free(pools->bs);
2770a91a2785SMartin K. Petersen 
2771e6ee8c0bSKiyoshi Ueda free_tio_pool_and_out:
2772dba14160SMikulas Patocka 	if (pools->tio_pool)
2773e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->tio_pool);
2774e6ee8c0bSKiyoshi Ueda 
2775e6ee8c0bSKiyoshi Ueda free_io_pool_and_out:
2776e6ee8c0bSKiyoshi Ueda 	mempool_destroy(pools->io_pool);
2777e6ee8c0bSKiyoshi Ueda 
2778e6ee8c0bSKiyoshi Ueda free_pools_and_out:
2779e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2780e6ee8c0bSKiyoshi Ueda 
2781e6ee8c0bSKiyoshi Ueda 	return NULL;
2782e6ee8c0bSKiyoshi Ueda }
2783e6ee8c0bSKiyoshi Ueda 
2784e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
2785e6ee8c0bSKiyoshi Ueda {
2786e6ee8c0bSKiyoshi Ueda 	if (!pools)
2787e6ee8c0bSKiyoshi Ueda 		return;
2788e6ee8c0bSKiyoshi Ueda 
2789e6ee8c0bSKiyoshi Ueda 	if (pools->io_pool)
2790e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->io_pool);
2791e6ee8c0bSKiyoshi Ueda 
2792e6ee8c0bSKiyoshi Ueda 	if (pools->tio_pool)
2793e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->tio_pool);
2794e6ee8c0bSKiyoshi Ueda 
2795e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
2796e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
2797e6ee8c0bSKiyoshi Ueda 
2798e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2799e6ee8c0bSKiyoshi Ueda }
2800e6ee8c0bSKiyoshi Ueda 
280183d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
28021da177e4SLinus Torvalds 	.open = dm_blk_open,
28031da177e4SLinus Torvalds 	.release = dm_blk_close,
2804aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
28053ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
28061da177e4SLinus Torvalds 	.owner = THIS_MODULE
28071da177e4SLinus Torvalds };
28081da177e4SLinus Torvalds 
28091da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo);
28101da177e4SLinus Torvalds 
28111da177e4SLinus Torvalds /*
28121da177e4SLinus Torvalds  * module hooks
28131da177e4SLinus Torvalds  */
28141da177e4SLinus Torvalds module_init(dm_init);
28151da177e4SLinus Torvalds module_exit(dm_exit);
28161da177e4SLinus Torvalds 
28171da177e4SLinus Torvalds module_param(major, uint, 0);
28181da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
28191da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
28201da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
28211da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2822