xref: /openbmc/linux/drivers/md/dm.c (revision b0d8ed4d96a26ef3ac54a4aa8911c9413070662e)
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 
1679faf400fSStefan Bader 	struct bio_set *bs;
1689faf400fSStefan Bader 
1691da177e4SLinus Torvalds 	/*
1701da177e4SLinus Torvalds 	 * Event handling.
1711da177e4SLinus Torvalds 	 */
1721da177e4SLinus Torvalds 	atomic_t event_nr;
1731da177e4SLinus Torvalds 	wait_queue_head_t eventq;
1747a8c3d3bSMike Anderson 	atomic_t uevent_seq;
1757a8c3d3bSMike Anderson 	struct list_head uevent_list;
1767a8c3d3bSMike Anderson 	spinlock_t uevent_lock; /* Protect access to uevent_list */
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	/*
1791da177e4SLinus Torvalds 	 * freeze/thaw support require holding onto a super block
1801da177e4SLinus Torvalds 	 */
1811da177e4SLinus Torvalds 	struct super_block *frozen_sb;
182db8fef4fSMikulas Patocka 	struct block_device *bdev;
1833ac51e74SDarrick J. Wong 
1843ac51e74SDarrick J. Wong 	/* forced geometry settings */
1853ac51e74SDarrick J. Wong 	struct hd_geometry geometry;
186784aae73SMilan Broz 
187784aae73SMilan Broz 	/* sysfs handle */
188784aae73SMilan Broz 	struct kobject kobj;
18952b1fd5aSMikulas Patocka 
190d87f4c14STejun Heo 	/* zero-length flush that will be cloned and submitted to targets */
191d87f4c14STejun Heo 	struct bio flush_bio;
1921da177e4SLinus Torvalds };
1931da177e4SLinus Torvalds 
194e6ee8c0bSKiyoshi Ueda /*
195e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
196e6ee8c0bSKiyoshi Ueda  */
197e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
198e6ee8c0bSKiyoshi Ueda 	mempool_t *io_pool;
199e6ee8c0bSKiyoshi Ueda 	struct bio_set *bs;
200e6ee8c0bSKiyoshi Ueda };
201e6ee8c0bSKiyoshi Ueda 
2021da177e4SLinus Torvalds #define MIN_IOS 256
203e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
2048fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
20594818742SKent Overstreet 
2061da177e4SLinus Torvalds static int __init local_init(void)
2071da177e4SLinus Torvalds {
20851157b4aSKiyoshi Ueda 	int r = -ENOMEM;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
211028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
2121da177e4SLinus Torvalds 	if (!_io_cache)
21351157b4aSKiyoshi Ueda 		return r;
2141da177e4SLinus Torvalds 
2158fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
2168fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
217dba14160SMikulas Patocka 		goto out_free_io_cache;
2188fbf26adSKiyoshi Ueda 
21951e5b2bdSMike Anderson 	r = dm_uevent_init();
22051157b4aSKiyoshi Ueda 	if (r)
22123e5083bSJun'ichi Nomura 		goto out_free_rq_tio_cache;
22251e5b2bdSMike Anderson 
2231da177e4SLinus Torvalds 	_major = major;
2241da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
22551157b4aSKiyoshi Ueda 	if (r < 0)
22651157b4aSKiyoshi Ueda 		goto out_uevent_exit;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	if (!_major)
2291da177e4SLinus Torvalds 		_major = r;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	return 0;
23251157b4aSKiyoshi Ueda 
23351157b4aSKiyoshi Ueda out_uevent_exit:
23451157b4aSKiyoshi Ueda 	dm_uevent_exit();
2358fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
2368fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
23751157b4aSKiyoshi Ueda out_free_io_cache:
23851157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
23951157b4aSKiyoshi Ueda 
24051157b4aSKiyoshi Ueda 	return r;
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds static void local_exit(void)
2441da177e4SLinus Torvalds {
2458fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
2461da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
24700d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
24851e5b2bdSMike Anderson 	dm_uevent_exit();
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	_major = 0;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	DMINFO("cleaned up");
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
255b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
2561da177e4SLinus Torvalds 	local_init,
2571da177e4SLinus Torvalds 	dm_target_init,
2581da177e4SLinus Torvalds 	dm_linear_init,
2591da177e4SLinus Torvalds 	dm_stripe_init,
260952b3557SMikulas Patocka 	dm_io_init,
261945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2621da177e4SLinus Torvalds 	dm_interface_init,
2631da177e4SLinus Torvalds };
2641da177e4SLinus Torvalds 
265b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2661da177e4SLinus Torvalds 	local_exit,
2671da177e4SLinus Torvalds 	dm_target_exit,
2681da177e4SLinus Torvalds 	dm_linear_exit,
2691da177e4SLinus Torvalds 	dm_stripe_exit,
270952b3557SMikulas Patocka 	dm_io_exit,
271945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
2721da177e4SLinus Torvalds 	dm_interface_exit,
2731da177e4SLinus Torvalds };
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds static int __init dm_init(void)
2761da177e4SLinus Torvalds {
2771da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	int r, i;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
2821da177e4SLinus Torvalds 		r = _inits[i]();
2831da177e4SLinus Torvalds 		if (r)
2841da177e4SLinus Torvalds 			goto bad;
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	return 0;
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds       bad:
2901da177e4SLinus Torvalds 	while (i--)
2911da177e4SLinus Torvalds 		_exits[i]();
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	return r;
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds static void __exit dm_exit(void)
2971da177e4SLinus Torvalds {
2981da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	while (i--)
3011da177e4SLinus Torvalds 		_exits[i]();
302d15b774cSAlasdair G Kergon 
303d15b774cSAlasdair G Kergon 	/*
304d15b774cSAlasdair G Kergon 	 * Should be empty by this point.
305d15b774cSAlasdair G Kergon 	 */
306d15b774cSAlasdair G Kergon 	idr_destroy(&_minor_idr);
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds /*
3101da177e4SLinus Torvalds  * Block device functions
3111da177e4SLinus Torvalds  */
312432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
313432a212cSMike Anderson {
314432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
315432a212cSMike Anderson }
316432a212cSMike Anderson 
317fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3181da177e4SLinus Torvalds {
3191da177e4SLinus Torvalds 	struct mapped_device *md;
3201da177e4SLinus Torvalds 
321fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
322fba9f90eSJeff Mahoney 
323fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
324fba9f90eSJeff Mahoney 	if (!md)
325fba9f90eSJeff Mahoney 		goto out;
326fba9f90eSJeff Mahoney 
3275c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
328432a212cSMike Anderson 	    dm_deleting_md(md)) {
329fba9f90eSJeff Mahoney 		md = NULL;
330fba9f90eSJeff Mahoney 		goto out;
331fba9f90eSJeff Mahoney 	}
332fba9f90eSJeff Mahoney 
3331da177e4SLinus Torvalds 	dm_get(md);
3345c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
335fba9f90eSJeff Mahoney 
336fba9f90eSJeff Mahoney out:
337fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
338fba9f90eSJeff Mahoney 
339fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
342fe5f9f2cSAl Viro static int dm_blk_close(struct gendisk *disk, fmode_t mode)
3431da177e4SLinus Torvalds {
344fe5f9f2cSAl Viro 	struct mapped_device *md = disk->private_data;
3456e9624b8SArnd Bergmann 
3464a1aeb98SMilan Broz 	spin_lock(&_minor_lock);
3474a1aeb98SMilan Broz 
3485c6bd75dSAlasdair G Kergon 	atomic_dec(&md->open_count);
3491da177e4SLinus Torvalds 	dm_put(md);
3504a1aeb98SMilan Broz 
3514a1aeb98SMilan Broz 	spin_unlock(&_minor_lock);
3526e9624b8SArnd Bergmann 
3531da177e4SLinus Torvalds 	return 0;
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds 
3565c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3575c6bd75dSAlasdair G Kergon {
3585c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3595c6bd75dSAlasdair G Kergon }
3605c6bd75dSAlasdair G Kergon 
3615c6bd75dSAlasdair G Kergon /*
3625c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3635c6bd75dSAlasdair G Kergon  */
3645c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md)
3655c6bd75dSAlasdair G Kergon {
3665c6bd75dSAlasdair G Kergon 	int r = 0;
3675c6bd75dSAlasdair G Kergon 
3685c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
3695c6bd75dSAlasdair G Kergon 
3705c6bd75dSAlasdair G Kergon 	if (dm_open_count(md))
3715c6bd75dSAlasdair G Kergon 		r = -EBUSY;
3725c6bd75dSAlasdair G Kergon 	else
3735c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
3745c6bd75dSAlasdair G Kergon 
3755c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
3765c6bd75dSAlasdair G Kergon 
3775c6bd75dSAlasdair G Kergon 	return r;
3785c6bd75dSAlasdair G Kergon }
3795c6bd75dSAlasdair G Kergon 
3803ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3813ac51e74SDarrick J. Wong {
3823ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
3833ac51e74SDarrick J. Wong 
3843ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
3853ac51e74SDarrick J. Wong }
3863ac51e74SDarrick J. Wong 
387fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
388aa129a22SMilan Broz 			unsigned int cmd, unsigned long arg)
389aa129a22SMilan Broz {
390fe5f9f2cSAl Viro 	struct mapped_device *md = bdev->bd_disk->private_data;
3917c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
392aa129a22SMilan Broz 	struct dm_target *tgt;
393aa129a22SMilan Broz 	int r = -ENOTTY;
394aa129a22SMilan Broz 
395aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
396aa129a22SMilan Broz 		goto out;
397aa129a22SMilan Broz 
398aa129a22SMilan Broz 	/* We only support devices that have a single target */
399aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
400aa129a22SMilan Broz 		goto out;
401aa129a22SMilan Broz 
402aa129a22SMilan Broz 	tgt = dm_table_get_target(map, 0);
403aa129a22SMilan Broz 
4044f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
405aa129a22SMilan Broz 		r = -EAGAIN;
406aa129a22SMilan Broz 		goto out;
407aa129a22SMilan Broz 	}
408aa129a22SMilan Broz 
409aa129a22SMilan Broz 	if (tgt->type->ioctl)
410647b3d00SAl Viro 		r = tgt->type->ioctl(tgt, cmd, arg);
411aa129a22SMilan Broz 
412aa129a22SMilan Broz out:
413aa129a22SMilan Broz 	dm_table_put(map);
414aa129a22SMilan Broz 
415aa129a22SMilan Broz 	return r;
416aa129a22SMilan Broz }
417aa129a22SMilan Broz 
418028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
4191da177e4SLinus Torvalds {
4201da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
423028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
4241da177e4SLinus Torvalds {
4251da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
428028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
4291da177e4SLinus Torvalds {
430dba14160SMikulas Patocka 	bio_put(&tio->clone);
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds 
43308885643SKiyoshi Ueda static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
43408885643SKiyoshi Ueda 					    gfp_t gfp_mask)
435cec47e3dSKiyoshi Ueda {
4365f015204SJun'ichi Nomura 	return mempool_alloc(md->io_pool, gfp_mask);
437cec47e3dSKiyoshi Ueda }
438cec47e3dSKiyoshi Ueda 
439cec47e3dSKiyoshi Ueda static void free_rq_tio(struct dm_rq_target_io *tio)
440cec47e3dSKiyoshi Ueda {
4415f015204SJun'ichi Nomura 	mempool_free(tio, tio->md->io_pool);
442cec47e3dSKiyoshi Ueda }
443cec47e3dSKiyoshi Ueda 
44490abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md)
44590abb8c4SKiyoshi Ueda {
44690abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
44790abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
44890abb8c4SKiyoshi Ueda }
44990abb8c4SKiyoshi Ueda 
4503eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
4513eaf840eSJun'ichi "Nick" Nomura {
4523eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
453c9959059STejun Heo 	int cpu;
454316d315bSNikanth Karthikesan 	int rw = bio_data_dir(io->bio);
4553eaf840eSJun'ichi "Nick" Nomura 
4563eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
4573eaf840eSJun'ichi "Nick" Nomura 
458074a7acaSTejun Heo 	cpu = part_stat_lock();
459074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
460074a7acaSTejun Heo 	part_stat_unlock();
4611e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw],
4621e9bb880SShaohua Li 		atomic_inc_return(&md->pending[rw]));
4633eaf840eSJun'ichi "Nick" Nomura }
4643eaf840eSJun'ichi "Nick" Nomura 
465d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
4663eaf840eSJun'ichi "Nick" Nomura {
4673eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
4683eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
4693eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
470c9959059STejun Heo 	int pending, cpu;
4713eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
4723eaf840eSJun'ichi "Nick" Nomura 
473074a7acaSTejun Heo 	cpu = part_stat_lock();
474074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
475074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
476074a7acaSTejun Heo 	part_stat_unlock();
4773eaf840eSJun'ichi "Nick" Nomura 
478af7e466aSMikulas Patocka 	/*
479af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
480d87f4c14STejun Heo 	 * a flush.
481af7e466aSMikulas Patocka 	 */
4821e9bb880SShaohua Li 	pending = atomic_dec_return(&md->pending[rw]);
4831e9bb880SShaohua Li 	atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
484316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
4853eaf840eSJun'ichi "Nick" Nomura 
486d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
487d221d2e7SMikulas Patocka 	if (!pending)
488d221d2e7SMikulas Patocka 		wake_up(&md->wait);
4893eaf840eSJun'ichi "Nick" Nomura }
4903eaf840eSJun'ichi "Nick" Nomura 
4911da177e4SLinus Torvalds /*
4921da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
4931da177e4SLinus Torvalds  */
49492c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
4951da177e4SLinus Torvalds {
49605447420SKiyoshi Ueda 	unsigned long flags;
4971da177e4SLinus Torvalds 
49805447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
4991da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
50005447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
50192c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
5021da177e4SLinus Torvalds }
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds /*
5051da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
5061da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
5071da177e4SLinus Torvalds  * dm_table_put() when finished.
5081da177e4SLinus Torvalds  */
5097c666411SAlasdair G Kergon struct dm_table *dm_get_live_table(struct mapped_device *md)
5101da177e4SLinus Torvalds {
5111da177e4SLinus Torvalds 	struct dm_table *t;
512523d9297SKiyoshi Ueda 	unsigned long flags;
5131da177e4SLinus Torvalds 
514523d9297SKiyoshi Ueda 	read_lock_irqsave(&md->map_lock, flags);
5151da177e4SLinus Torvalds 	t = md->map;
5161da177e4SLinus Torvalds 	if (t)
5171da177e4SLinus Torvalds 		dm_table_get(t);
518523d9297SKiyoshi Ueda 	read_unlock_irqrestore(&md->map_lock, flags);
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	return t;
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
5233ac51e74SDarrick J. Wong /*
5243ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
5253ac51e74SDarrick J. Wong  */
5263ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
5273ac51e74SDarrick J. Wong {
5283ac51e74SDarrick J. Wong 	*geo = md->geometry;
5293ac51e74SDarrick J. Wong 
5303ac51e74SDarrick J. Wong 	return 0;
5313ac51e74SDarrick J. Wong }
5323ac51e74SDarrick J. Wong 
5333ac51e74SDarrick J. Wong /*
5343ac51e74SDarrick J. Wong  * Set the geometry of a device.
5353ac51e74SDarrick J. Wong  */
5363ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
5373ac51e74SDarrick J. Wong {
5383ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
5393ac51e74SDarrick J. Wong 
5403ac51e74SDarrick J. Wong 	if (geo->start > sz) {
5413ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
5423ac51e74SDarrick J. Wong 		return -EINVAL;
5433ac51e74SDarrick J. Wong 	}
5443ac51e74SDarrick J. Wong 
5453ac51e74SDarrick J. Wong 	md->geometry = *geo;
5463ac51e74SDarrick J. Wong 
5473ac51e74SDarrick J. Wong 	return 0;
5483ac51e74SDarrick J. Wong }
5493ac51e74SDarrick J. Wong 
5501da177e4SLinus Torvalds /*-----------------------------------------------------------------
5511da177e4SLinus Torvalds  * CRUD START:
5521da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
5531da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
5541da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
5551da177e4SLinus Torvalds  *   interests of getting something for people to use I give
5561da177e4SLinus Torvalds  *   you this clearly demarcated crap.
5571da177e4SLinus Torvalds  *---------------------------------------------------------------*/
5581da177e4SLinus Torvalds 
5592e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
5602e93ccc1SKiyoshi Ueda {
5612e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
5622e93ccc1SKiyoshi Ueda }
5632e93ccc1SKiyoshi Ueda 
5641da177e4SLinus Torvalds /*
5651da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
5661da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
5671da177e4SLinus Torvalds  */
568858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
5691da177e4SLinus Torvalds {
5702e93ccc1SKiyoshi Ueda 	unsigned long flags;
571b35f8caaSMilan Broz 	int io_error;
572b35f8caaSMilan Broz 	struct bio *bio;
573b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
5742e93ccc1SKiyoshi Ueda 
5752e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
576f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
577f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
578f88fb981SKiyoshi Ueda 		if (!(io->error > 0 && __noflush_suspending(md)))
5791da177e4SLinus Torvalds 			io->error = error;
580f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
581f88fb981SKiyoshi Ueda 	}
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
5842e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
5852e93ccc1SKiyoshi Ueda 			/*
5862e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
5872e93ccc1SKiyoshi Ueda 			 */
588022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
5896a8736d1STejun Heo 			if (__noflush_suspending(md))
5906a8736d1STejun Heo 				bio_list_add_head(&md->deferred, io->bio);
5916a8736d1STejun Heo 			else
5922e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
5932e93ccc1SKiyoshi Ueda 				io->error = -EIO;
594022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
5952e93ccc1SKiyoshi Ueda 		}
5962e93ccc1SKiyoshi Ueda 
597b35f8caaSMilan Broz 		io_error = io->error;
598b35f8caaSMilan Broz 		bio = io->bio;
599af7e466aSMikulas Patocka 		end_io_acct(io);
600a97f925aSMikulas Patocka 		free_io(md, io);
6011da177e4SLinus Torvalds 
6026a8736d1STejun Heo 		if (io_error == DM_ENDIO_REQUEUE)
6036a8736d1STejun Heo 			return;
6046a8736d1STejun Heo 
605b372d360SMike Snitzer 		if ((bio->bi_rw & REQ_FLUSH) && bio->bi_size) {
6061da177e4SLinus Torvalds 			/*
6076a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
6086a8736d1STejun Heo 			 * without REQ_FLUSH.
6091da177e4SLinus Torvalds 			 */
6106a8736d1STejun Heo 			bio->bi_rw &= ~REQ_FLUSH;
6116a8736d1STejun Heo 			queue_io(md, bio);
6125f3ea37cSArnaldo Carvalho de Melo 		} else {
613b372d360SMike Snitzer 			/* done with normal IO or empty flush */
614b35f8caaSMilan Broz 			bio_endio(bio, io_error);
6152e93ccc1SKiyoshi Ueda 		}
6161da177e4SLinus Torvalds 	}
617af7e466aSMikulas Patocka }
6181da177e4SLinus Torvalds 
6196712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error)
6201da177e4SLinus Torvalds {
6211da177e4SLinus Torvalds 	int r = 0;
622028867acSAlasdair G Kergon 	struct dm_target_io *tio = bio->bi_private;
623b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
6249faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
6251da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	if (!bio_flagged(bio, BIO_UPTODATE) && !error)
6281da177e4SLinus Torvalds 		error = -EIO;
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 	if (endio) {
6317de3ee57SMikulas Patocka 		r = endio(tio->ti, bio, error);
6322e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
6332e93ccc1SKiyoshi Ueda 			/*
6342e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
6352e93ccc1SKiyoshi Ueda 			 * in dec_pending().
6362e93ccc1SKiyoshi Ueda 			 */
6371da177e4SLinus Torvalds 			error = r;
63845cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
63945cbcd79SKiyoshi Ueda 			/* The target will handle the io */
6406712ecf8SNeilBrown 			return;
64145cbcd79SKiyoshi Ueda 		else if (r) {
64245cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
64345cbcd79SKiyoshi Ueda 			BUG();
64445cbcd79SKiyoshi Ueda 		}
6451da177e4SLinus Torvalds 	}
6461da177e4SLinus Torvalds 
6479faf400fSStefan Bader 	free_tio(md, tio);
648b35f8caaSMilan Broz 	dec_pending(io, error);
6491da177e4SLinus Torvalds }
6501da177e4SLinus Torvalds 
651cec47e3dSKiyoshi Ueda /*
652cec47e3dSKiyoshi Ueda  * Partial completion handling for request-based dm
653cec47e3dSKiyoshi Ueda  */
654cec47e3dSKiyoshi Ueda static void end_clone_bio(struct bio *clone, int error)
655cec47e3dSKiyoshi Ueda {
656cec47e3dSKiyoshi Ueda 	struct dm_rq_clone_bio_info *info = clone->bi_private;
657cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = info->tio;
658cec47e3dSKiyoshi Ueda 	struct bio *bio = info->orig;
659cec47e3dSKiyoshi Ueda 	unsigned int nr_bytes = info->orig->bi_size;
660cec47e3dSKiyoshi Ueda 
661cec47e3dSKiyoshi Ueda 	bio_put(clone);
662cec47e3dSKiyoshi Ueda 
663cec47e3dSKiyoshi Ueda 	if (tio->error)
664cec47e3dSKiyoshi Ueda 		/*
665cec47e3dSKiyoshi Ueda 		 * An error has already been detected on the request.
666cec47e3dSKiyoshi Ueda 		 * Once error occurred, just let clone->end_io() handle
667cec47e3dSKiyoshi Ueda 		 * the remainder.
668cec47e3dSKiyoshi Ueda 		 */
669cec47e3dSKiyoshi Ueda 		return;
670cec47e3dSKiyoshi Ueda 	else if (error) {
671cec47e3dSKiyoshi Ueda 		/*
672cec47e3dSKiyoshi Ueda 		 * Don't notice the error to the upper layer yet.
673cec47e3dSKiyoshi Ueda 		 * The error handling decision is made by the target driver,
674cec47e3dSKiyoshi Ueda 		 * when the request is completed.
675cec47e3dSKiyoshi Ueda 		 */
676cec47e3dSKiyoshi Ueda 		tio->error = error;
677cec47e3dSKiyoshi Ueda 		return;
678cec47e3dSKiyoshi Ueda 	}
679cec47e3dSKiyoshi Ueda 
680cec47e3dSKiyoshi Ueda 	/*
681cec47e3dSKiyoshi Ueda 	 * I/O for the bio successfully completed.
682cec47e3dSKiyoshi Ueda 	 * Notice the data completion to the upper layer.
683cec47e3dSKiyoshi Ueda 	 */
684cec47e3dSKiyoshi Ueda 
685cec47e3dSKiyoshi Ueda 	/*
686cec47e3dSKiyoshi Ueda 	 * bios are processed from the head of the list.
687cec47e3dSKiyoshi Ueda 	 * So the completing bio should always be rq->bio.
688cec47e3dSKiyoshi Ueda 	 * If it's not, something wrong is happening.
689cec47e3dSKiyoshi Ueda 	 */
690cec47e3dSKiyoshi Ueda 	if (tio->orig->bio != bio)
691cec47e3dSKiyoshi Ueda 		DMERR("bio completion is going in the middle of the request");
692cec47e3dSKiyoshi Ueda 
693cec47e3dSKiyoshi Ueda 	/*
694cec47e3dSKiyoshi Ueda 	 * Update the original request.
695cec47e3dSKiyoshi Ueda 	 * Do not use blk_end_request() here, because it may complete
696cec47e3dSKiyoshi Ueda 	 * the original request before the clone, and break the ordering.
697cec47e3dSKiyoshi Ueda 	 */
698cec47e3dSKiyoshi Ueda 	blk_update_request(tio->orig, 0, nr_bytes);
699cec47e3dSKiyoshi Ueda }
700cec47e3dSKiyoshi Ueda 
701cec47e3dSKiyoshi Ueda /*
702cec47e3dSKiyoshi Ueda  * Don't touch any member of the md after calling this function because
703cec47e3dSKiyoshi Ueda  * the md may be freed in dm_put() at the end of this function.
704cec47e3dSKiyoshi Ueda  * Or do dm_get() before calling this function and dm_put() later.
705cec47e3dSKiyoshi Ueda  */
706b4324feeSKiyoshi Ueda static void rq_completed(struct mapped_device *md, int rw, int run_queue)
707cec47e3dSKiyoshi Ueda {
708b4324feeSKiyoshi Ueda 	atomic_dec(&md->pending[rw]);
709cec47e3dSKiyoshi Ueda 
710cec47e3dSKiyoshi Ueda 	/* nudge anyone waiting on suspend queue */
711b4324feeSKiyoshi Ueda 	if (!md_in_flight(md))
712cec47e3dSKiyoshi Ueda 		wake_up(&md->wait);
713cec47e3dSKiyoshi Ueda 
714a8c32a5cSJens Axboe 	/*
715a8c32a5cSJens Axboe 	 * Run this off this callpath, as drivers could invoke end_io while
716a8c32a5cSJens Axboe 	 * inside their request_fn (and holding the queue lock). Calling
717a8c32a5cSJens Axboe 	 * back into ->request_fn() could deadlock attempting to grab the
718a8c32a5cSJens Axboe 	 * queue lock again.
719a8c32a5cSJens Axboe 	 */
720cec47e3dSKiyoshi Ueda 	if (run_queue)
721a8c32a5cSJens Axboe 		blk_run_queue_async(md->queue);
722cec47e3dSKiyoshi Ueda 
723cec47e3dSKiyoshi Ueda 	/*
724cec47e3dSKiyoshi Ueda 	 * dm_put() must be at the end of this function. See the comment above
725cec47e3dSKiyoshi Ueda 	 */
726cec47e3dSKiyoshi Ueda 	dm_put(md);
727cec47e3dSKiyoshi Ueda }
728cec47e3dSKiyoshi Ueda 
729a77e28c7SKiyoshi Ueda static void free_rq_clone(struct request *clone)
730a77e28c7SKiyoshi Ueda {
731a77e28c7SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
732a77e28c7SKiyoshi Ueda 
733a77e28c7SKiyoshi Ueda 	blk_rq_unprep_clone(clone);
734a77e28c7SKiyoshi Ueda 	free_rq_tio(tio);
735a77e28c7SKiyoshi Ueda }
736a77e28c7SKiyoshi Ueda 
737980691e5SKiyoshi Ueda /*
738980691e5SKiyoshi Ueda  * Complete the clone and the original request.
739980691e5SKiyoshi Ueda  * Must be called without queue lock.
740980691e5SKiyoshi Ueda  */
741980691e5SKiyoshi Ueda static void dm_end_request(struct request *clone, int error)
742980691e5SKiyoshi Ueda {
743980691e5SKiyoshi Ueda 	int rw = rq_data_dir(clone);
744980691e5SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
745980691e5SKiyoshi Ueda 	struct mapped_device *md = tio->md;
746980691e5SKiyoshi Ueda 	struct request *rq = tio->orig;
747980691e5SKiyoshi Ueda 
74829e4013dSTejun Heo 	if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
749980691e5SKiyoshi Ueda 		rq->errors = clone->errors;
750980691e5SKiyoshi Ueda 		rq->resid_len = clone->resid_len;
751980691e5SKiyoshi Ueda 
752980691e5SKiyoshi Ueda 		if (rq->sense)
753980691e5SKiyoshi Ueda 			/*
754980691e5SKiyoshi Ueda 			 * We are using the sense buffer of the original
755980691e5SKiyoshi Ueda 			 * request.
756980691e5SKiyoshi Ueda 			 * So setting the length of the sense data is enough.
757980691e5SKiyoshi Ueda 			 */
758980691e5SKiyoshi Ueda 			rq->sense_len = clone->sense_len;
759980691e5SKiyoshi Ueda 	}
760980691e5SKiyoshi Ueda 
761980691e5SKiyoshi Ueda 	free_rq_clone(clone);
762980691e5SKiyoshi Ueda 	blk_end_request_all(rq, error);
76329e4013dSTejun Heo 	rq_completed(md, rw, true);
764980691e5SKiyoshi Ueda }
765980691e5SKiyoshi Ueda 
766cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq)
767cec47e3dSKiyoshi Ueda {
768cec47e3dSKiyoshi Ueda 	struct request *clone = rq->special;
769cec47e3dSKiyoshi Ueda 
770cec47e3dSKiyoshi Ueda 	rq->special = NULL;
771cec47e3dSKiyoshi Ueda 	rq->cmd_flags &= ~REQ_DONTPREP;
772cec47e3dSKiyoshi Ueda 
773a77e28c7SKiyoshi Ueda 	free_rq_clone(clone);
774cec47e3dSKiyoshi Ueda }
775cec47e3dSKiyoshi Ueda 
776cec47e3dSKiyoshi Ueda /*
777cec47e3dSKiyoshi Ueda  * Requeue the original request of a clone.
778cec47e3dSKiyoshi Ueda  */
779cec47e3dSKiyoshi Ueda void dm_requeue_unmapped_request(struct request *clone)
780cec47e3dSKiyoshi Ueda {
781b4324feeSKiyoshi Ueda 	int rw = rq_data_dir(clone);
782cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
783cec47e3dSKiyoshi Ueda 	struct mapped_device *md = tio->md;
784cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
785cec47e3dSKiyoshi Ueda 	struct request_queue *q = rq->q;
786cec47e3dSKiyoshi Ueda 	unsigned long flags;
787cec47e3dSKiyoshi Ueda 
788cec47e3dSKiyoshi Ueda 	dm_unprep_request(rq);
789cec47e3dSKiyoshi Ueda 
790cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
791cec47e3dSKiyoshi Ueda 	blk_requeue_request(q, rq);
792cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
793cec47e3dSKiyoshi Ueda 
794b4324feeSKiyoshi Ueda 	rq_completed(md, rw, 0);
795cec47e3dSKiyoshi Ueda }
796cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
797cec47e3dSKiyoshi Ueda 
798cec47e3dSKiyoshi Ueda static void __stop_queue(struct request_queue *q)
799cec47e3dSKiyoshi Ueda {
800cec47e3dSKiyoshi Ueda 	blk_stop_queue(q);
801cec47e3dSKiyoshi Ueda }
802cec47e3dSKiyoshi Ueda 
803cec47e3dSKiyoshi Ueda static void stop_queue(struct request_queue *q)
804cec47e3dSKiyoshi Ueda {
805cec47e3dSKiyoshi Ueda 	unsigned long flags;
806cec47e3dSKiyoshi Ueda 
807cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
808cec47e3dSKiyoshi Ueda 	__stop_queue(q);
809cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
810cec47e3dSKiyoshi Ueda }
811cec47e3dSKiyoshi Ueda 
812cec47e3dSKiyoshi Ueda static void __start_queue(struct request_queue *q)
813cec47e3dSKiyoshi Ueda {
814cec47e3dSKiyoshi Ueda 	if (blk_queue_stopped(q))
815cec47e3dSKiyoshi Ueda 		blk_start_queue(q);
816cec47e3dSKiyoshi Ueda }
817cec47e3dSKiyoshi Ueda 
818cec47e3dSKiyoshi Ueda static void start_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 	__start_queue(q);
824cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
825cec47e3dSKiyoshi Ueda }
826cec47e3dSKiyoshi Ueda 
82711a68244SKiyoshi Ueda static void dm_done(struct request *clone, int error, bool mapped)
82811a68244SKiyoshi Ueda {
82911a68244SKiyoshi Ueda 	int r = error;
83011a68244SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
831ba1cbad9SMike Snitzer 	dm_request_endio_fn rq_end_io = NULL;
832ba1cbad9SMike Snitzer 
833ba1cbad9SMike Snitzer 	if (tio->ti) {
834ba1cbad9SMike Snitzer 		rq_end_io = tio->ti->type->rq_end_io;
83511a68244SKiyoshi Ueda 
83611a68244SKiyoshi Ueda 		if (mapped && rq_end_io)
83711a68244SKiyoshi Ueda 			r = rq_end_io(tio->ti, clone, error, &tio->info);
838ba1cbad9SMike Snitzer 	}
83911a68244SKiyoshi Ueda 
84011a68244SKiyoshi Ueda 	if (r <= 0)
84111a68244SKiyoshi Ueda 		/* The target wants to complete the I/O */
84211a68244SKiyoshi Ueda 		dm_end_request(clone, r);
84311a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_INCOMPLETE)
84411a68244SKiyoshi Ueda 		/* The target will handle the I/O */
84511a68244SKiyoshi Ueda 		return;
84611a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_REQUEUE)
84711a68244SKiyoshi Ueda 		/* The target wants to requeue the I/O */
84811a68244SKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
84911a68244SKiyoshi Ueda 	else {
85011a68244SKiyoshi Ueda 		DMWARN("unimplemented target endio return value: %d", r);
85111a68244SKiyoshi Ueda 		BUG();
85211a68244SKiyoshi Ueda 	}
85311a68244SKiyoshi Ueda }
85411a68244SKiyoshi Ueda 
855cec47e3dSKiyoshi Ueda /*
856cec47e3dSKiyoshi Ueda  * Request completion handler for request-based dm
857cec47e3dSKiyoshi Ueda  */
858cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq)
859cec47e3dSKiyoshi Ueda {
86011a68244SKiyoshi Ueda 	bool mapped = true;
861cec47e3dSKiyoshi Ueda 	struct request *clone = rq->completion_data;
862cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
863cec47e3dSKiyoshi Ueda 
86411a68244SKiyoshi Ueda 	if (rq->cmd_flags & REQ_FAILED)
86511a68244SKiyoshi Ueda 		mapped = false;
866cec47e3dSKiyoshi Ueda 
86711a68244SKiyoshi Ueda 	dm_done(clone, tio->error, mapped);
868cec47e3dSKiyoshi Ueda }
869cec47e3dSKiyoshi Ueda 
870cec47e3dSKiyoshi Ueda /*
871cec47e3dSKiyoshi Ueda  * Complete the clone and the original request with the error status
872cec47e3dSKiyoshi Ueda  * through softirq context.
873cec47e3dSKiyoshi Ueda  */
874cec47e3dSKiyoshi Ueda static void dm_complete_request(struct request *clone, int error)
875cec47e3dSKiyoshi Ueda {
876cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
877cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
878cec47e3dSKiyoshi Ueda 
879cec47e3dSKiyoshi Ueda 	tio->error = error;
880cec47e3dSKiyoshi Ueda 	rq->completion_data = clone;
881cec47e3dSKiyoshi Ueda 	blk_complete_request(rq);
882cec47e3dSKiyoshi Ueda }
883cec47e3dSKiyoshi Ueda 
884cec47e3dSKiyoshi Ueda /*
885cec47e3dSKiyoshi Ueda  * Complete the not-mapped clone and the original request with the error status
886cec47e3dSKiyoshi Ueda  * through softirq context.
887cec47e3dSKiyoshi Ueda  * Target's rq_end_io() function isn't called.
888cec47e3dSKiyoshi Ueda  * This may be used when the target's map_rq() function fails.
889cec47e3dSKiyoshi Ueda  */
890cec47e3dSKiyoshi Ueda void dm_kill_unmapped_request(struct request *clone, int error)
891cec47e3dSKiyoshi Ueda {
892cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
893cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
894cec47e3dSKiyoshi Ueda 
895cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_FAILED;
896cec47e3dSKiyoshi Ueda 	dm_complete_request(clone, error);
897cec47e3dSKiyoshi Ueda }
898cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
899cec47e3dSKiyoshi Ueda 
900cec47e3dSKiyoshi Ueda /*
901cec47e3dSKiyoshi Ueda  * Called with the queue lock held
902cec47e3dSKiyoshi Ueda  */
903cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error)
904cec47e3dSKiyoshi Ueda {
905cec47e3dSKiyoshi Ueda 	/*
906cec47e3dSKiyoshi Ueda 	 * For just cleaning up the information of the queue in which
907cec47e3dSKiyoshi Ueda 	 * the clone was dispatched.
908cec47e3dSKiyoshi Ueda 	 * The clone is *NOT* freed actually here because it is alloced from
909cec47e3dSKiyoshi Ueda 	 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
910cec47e3dSKiyoshi Ueda 	 */
911cec47e3dSKiyoshi Ueda 	__blk_put_request(clone->q, clone);
912cec47e3dSKiyoshi Ueda 
913cec47e3dSKiyoshi Ueda 	/*
914cec47e3dSKiyoshi Ueda 	 * Actual request completion is done in a softirq context which doesn't
915cec47e3dSKiyoshi Ueda 	 * hold the queue lock.  Otherwise, deadlock could occur because:
916cec47e3dSKiyoshi Ueda 	 *     - another request may be submitted by the upper level driver
917cec47e3dSKiyoshi Ueda 	 *       of the stacking during the completion
918cec47e3dSKiyoshi Ueda 	 *     - the submission which requires queue lock may be done
919cec47e3dSKiyoshi Ueda 	 *       against this queue
920cec47e3dSKiyoshi Ueda 	 */
921cec47e3dSKiyoshi Ueda 	dm_complete_request(clone, error);
922cec47e3dSKiyoshi Ueda }
923cec47e3dSKiyoshi Ueda 
92456a67df7SMike Snitzer /*
92556a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
92656a67df7SMike Snitzer  * target boundary.
92756a67df7SMike Snitzer  */
92856a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
9291da177e4SLinus Torvalds {
93056a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
93156a67df7SMike Snitzer 
93256a67df7SMike Snitzer 	return ti->len - target_offset;
93356a67df7SMike Snitzer }
93456a67df7SMike Snitzer 
93556a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
93656a67df7SMike Snitzer {
93756a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
938542f9038SMike Snitzer 	sector_t offset, max_len;
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 	/*
9411da177e4SLinus Torvalds 	 * Does the target need to split even further?
9421da177e4SLinus Torvalds 	 */
943542f9038SMike Snitzer 	if (ti->max_io_len) {
944542f9038SMike Snitzer 		offset = dm_target_offset(ti, sector);
945542f9038SMike Snitzer 		if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
946542f9038SMike Snitzer 			max_len = sector_div(offset, ti->max_io_len);
947542f9038SMike Snitzer 		else
948542f9038SMike Snitzer 			max_len = offset & (ti->max_io_len - 1);
949542f9038SMike Snitzer 		max_len = ti->max_io_len - max_len;
950542f9038SMike Snitzer 
951542f9038SMike Snitzer 		if (len > max_len)
952542f9038SMike Snitzer 			len = max_len;
9531da177e4SLinus Torvalds 	}
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	return len;
9561da177e4SLinus Torvalds }
9571da177e4SLinus Torvalds 
958542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
959542f9038SMike Snitzer {
960542f9038SMike Snitzer 	if (len > UINT_MAX) {
961542f9038SMike Snitzer 		DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
962542f9038SMike Snitzer 		      (unsigned long long)len, UINT_MAX);
963542f9038SMike Snitzer 		ti->error = "Maximum size of target IO is too large";
964542f9038SMike Snitzer 		return -EINVAL;
965542f9038SMike Snitzer 	}
966542f9038SMike Snitzer 
967542f9038SMike Snitzer 	ti->max_io_len = (uint32_t) len;
968542f9038SMike Snitzer 
969542f9038SMike Snitzer 	return 0;
970542f9038SMike Snitzer }
971542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
972542f9038SMike Snitzer 
973bd2a49b8SAlasdair G Kergon static void __map_bio(struct dm_target_io *tio)
9741da177e4SLinus Torvalds {
9751da177e4SLinus Torvalds 	int r;
9762056a782SJens Axboe 	sector_t sector;
9779faf400fSStefan Bader 	struct mapped_device *md;
978dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
979bd2a49b8SAlasdair G Kergon 	struct dm_target *ti = tio->ti;
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
9821da177e4SLinus Torvalds 	clone->bi_private = tio;
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	/*
9851da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
9861da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
9871da177e4SLinus Torvalds 	 * this io.
9881da177e4SLinus Torvalds 	 */
9891da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
9902056a782SJens Axboe 	sector = clone->bi_sector;
9917de3ee57SMikulas Patocka 	r = ti->type->map(ti, clone);
99245cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
9931da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
9942056a782SJens Axboe 
995d07335e5SMike Snitzer 		trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
99622a7c31aSAlan D. Brunelle 				      tio->io->bio->bi_bdev->bd_dev, sector);
9972056a782SJens Axboe 
9981da177e4SLinus Torvalds 		generic_make_request(clone);
9992e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
10002e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
10019faf400fSStefan Bader 		md = tio->io->md;
10029faf400fSStefan Bader 		dec_pending(tio->io, r);
10039faf400fSStefan Bader 		free_tio(md, tio);
100445cbcd79SKiyoshi Ueda 	} else if (r) {
100545cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
100645cbcd79SKiyoshi Ueda 		BUG();
10071da177e4SLinus Torvalds 	}
10081da177e4SLinus Torvalds }
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds struct clone_info {
10111da177e4SLinus Torvalds 	struct mapped_device *md;
10121da177e4SLinus Torvalds 	struct dm_table *map;
10131da177e4SLinus Torvalds 	struct bio *bio;
10141da177e4SLinus Torvalds 	struct dm_io *io;
10151da177e4SLinus Torvalds 	sector_t sector;
10161da177e4SLinus Torvalds 	sector_t sector_count;
10171da177e4SLinus Torvalds 	unsigned short idx;
10181da177e4SLinus Torvalds };
10191da177e4SLinus Torvalds 
1020bd2a49b8SAlasdair G Kergon static void bio_setup_sector(struct bio *bio, sector_t sector, sector_t len)
1021bd2a49b8SAlasdair G Kergon {
1022bd2a49b8SAlasdair G Kergon 	bio->bi_sector = sector;
1023bd2a49b8SAlasdair G Kergon 	bio->bi_size = to_bytes(len);
1024bd2a49b8SAlasdair G Kergon }
1025bd2a49b8SAlasdair G Kergon 
1026bd2a49b8SAlasdair G Kergon static void bio_setup_bv(struct bio *bio, unsigned short idx, unsigned short bv_count)
1027bd2a49b8SAlasdair G Kergon {
1028bd2a49b8SAlasdair G Kergon 	bio->bi_idx = idx;
1029bd2a49b8SAlasdair G Kergon 	bio->bi_vcnt = idx + bv_count;
1030bd2a49b8SAlasdair G Kergon 	bio->bi_flags &= ~(1 << BIO_SEG_VALID);
1031bd2a49b8SAlasdair G Kergon }
1032bd2a49b8SAlasdair G Kergon 
1033bd2a49b8SAlasdair G Kergon static void clone_bio_integrity(struct bio *bio, struct bio *clone,
1034bd2a49b8SAlasdair G Kergon 				unsigned short idx, unsigned len, unsigned offset,
1035bd2a49b8SAlasdair G Kergon 				unsigned trim)
1036bd2a49b8SAlasdair G Kergon {
1037bd2a49b8SAlasdair G Kergon 	if (!bio_integrity(bio))
1038bd2a49b8SAlasdair G Kergon 		return;
1039bd2a49b8SAlasdair G Kergon 
1040bd2a49b8SAlasdair G Kergon 	bio_integrity_clone(clone, bio, GFP_NOIO);
1041bd2a49b8SAlasdair G Kergon 
1042bd2a49b8SAlasdair G Kergon 	if (trim)
1043bd2a49b8SAlasdair G Kergon 		bio_integrity_trim(clone, bio_sector_offset(bio, idx, offset), len);
1044bd2a49b8SAlasdair G Kergon }
1045bd2a49b8SAlasdair G Kergon 
10461da177e4SLinus Torvalds /*
1047d87f4c14STejun Heo  * Creates a little bio that just does part of a bvec.
10481da177e4SLinus Torvalds  */
104914fe594dSAlasdair G Kergon static void clone_split_bio(struct dm_target_io *tio, struct bio *bio,
105014fe594dSAlasdair G Kergon 			    sector_t sector, unsigned short idx,
105114fe594dSAlasdair G Kergon 			    unsigned offset, unsigned len)
10521da177e4SLinus Torvalds {
1053dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
10541da177e4SLinus Torvalds 	struct bio_vec *bv = bio->bi_io_vec + idx;
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 	*clone->bi_io_vec = *bv;
10571da177e4SLinus Torvalds 
1058bd2a49b8SAlasdair G Kergon 	bio_setup_sector(clone, sector, len);
1059bd2a49b8SAlasdair G Kergon 
10601da177e4SLinus Torvalds 	clone->bi_bdev = bio->bi_bdev;
1061d87f4c14STejun Heo 	clone->bi_rw = bio->bi_rw;
10621da177e4SLinus Torvalds 	clone->bi_vcnt = 1;
10631da177e4SLinus Torvalds 	clone->bi_io_vec->bv_offset = offset;
10641da177e4SLinus Torvalds 	clone->bi_io_vec->bv_len = clone->bi_size;
1065f3e1d26eSMartin K. Petersen 	clone->bi_flags |= 1 << BIO_CLONED;
10661da177e4SLinus Torvalds 
1067bd2a49b8SAlasdair G Kergon 	clone_bio_integrity(bio, clone, idx, len, offset, 1);
10681da177e4SLinus Torvalds }
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds /*
10711da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
10721da177e4SLinus Torvalds  */
1073dba14160SMikulas Patocka static void clone_bio(struct dm_target_io *tio, struct bio *bio,
1074dba14160SMikulas Patocka 		      sector_t sector, unsigned short idx,
1075e4c93811SAlasdair G Kergon 		      unsigned short bv_count, unsigned len)
10761da177e4SLinus Torvalds {
1077dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
1078bd2a49b8SAlasdair G Kergon 	unsigned trim = 0;
10791da177e4SLinus Torvalds 
10809faf400fSStefan Bader 	__bio_clone(clone, bio);
1081bd2a49b8SAlasdair G Kergon 	bio_setup_sector(clone, sector, len);
1082bd2a49b8SAlasdair G Kergon 	bio_setup_bv(clone, idx, bv_count);
10839c47008dSMartin K. Petersen 
10849c47008dSMartin K. Petersen 	if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
1085bd2a49b8SAlasdair G Kergon 		trim = 1;
1086bd2a49b8SAlasdair G Kergon 	clone_bio_integrity(bio, clone, idx, len, 0, trim);
10871da177e4SLinus Torvalds }
10881da177e4SLinus Torvalds 
10899015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci,
1090bd2a49b8SAlasdair G Kergon 				      struct dm_target *ti, int nr_iovecs,
109155a62eefSAlasdair G Kergon 				      unsigned target_bio_nr)
1092f9ab94ceSMikulas Patocka {
1093dba14160SMikulas Patocka 	struct dm_target_io *tio;
1094dba14160SMikulas Patocka 	struct bio *clone;
1095dba14160SMikulas Patocka 
1096dba14160SMikulas Patocka 	clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, ci->md->bs);
1097dba14160SMikulas Patocka 	tio = container_of(clone, struct dm_target_io, clone);
1098f9ab94ceSMikulas Patocka 
1099f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1100f9ab94ceSMikulas Patocka 	tio->ti = ti;
1101f9ab94ceSMikulas Patocka 	memset(&tio->info, 0, sizeof(tio->info));
110255a62eefSAlasdair G Kergon 	tio->target_bio_nr = target_bio_nr;
11039015df24SAlasdair G Kergon 
11049015df24SAlasdair G Kergon 	return tio;
11059015df24SAlasdair G Kergon }
11069015df24SAlasdair G Kergon 
110714fe594dSAlasdair G Kergon static void __clone_and_map_simple_bio(struct clone_info *ci,
110814fe594dSAlasdair G Kergon 				       struct dm_target *ti,
110955a62eefSAlasdair G Kergon 				       unsigned target_bio_nr, sector_t len)
11109015df24SAlasdair G Kergon {
111155a62eefSAlasdair G Kergon 	struct dm_target_io *tio = alloc_tio(ci, ti, ci->bio->bi_max_vecs, target_bio_nr);
1112dba14160SMikulas Patocka 	struct bio *clone = &tio->clone;
11139015df24SAlasdair G Kergon 
111406a426ceSMike Snitzer 	/*
111506a426ceSMike Snitzer 	 * Discard requests require the bio's inline iovecs be initialized.
111606a426ceSMike Snitzer 	 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
111706a426ceSMike Snitzer 	 * and discard, so no need for concern about wasted bvec allocations.
111806a426ceSMike Snitzer 	 */
1119dba14160SMikulas Patocka 	 __bio_clone(clone, ci->bio);
1120bd2a49b8SAlasdair G Kergon 	if (len)
1121bd2a49b8SAlasdair G Kergon 		bio_setup_sector(clone, ci->sector, len);
1122f9ab94ceSMikulas Patocka 
1123bd2a49b8SAlasdair G Kergon 	__map_bio(tio);
1124f9ab94ceSMikulas Patocka }
1125f9ab94ceSMikulas Patocka 
112614fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
112755a62eefSAlasdair G Kergon 				  unsigned num_bios, sector_t len)
112806a426ceSMike Snitzer {
112955a62eefSAlasdair G Kergon 	unsigned target_bio_nr;
113006a426ceSMike Snitzer 
113155a62eefSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
113214fe594dSAlasdair G Kergon 		__clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
113306a426ceSMike Snitzer }
113406a426ceSMike Snitzer 
113514fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci)
1136f9ab94ceSMikulas Patocka {
113706a426ceSMike Snitzer 	unsigned target_nr = 0;
1138f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1139f9ab94ceSMikulas Patocka 
1140b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1141f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
114214fe594dSAlasdair G Kergon 		__send_duplicate_bios(ci, ti, ti->num_flush_bios, 0);
1143f9ab94ceSMikulas Patocka 
1144f9ab94ceSMikulas Patocka 	return 0;
1145f9ab94ceSMikulas Patocka }
1146f9ab94ceSMikulas Patocka 
1147e4c93811SAlasdair G Kergon static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
1148e4c93811SAlasdair G Kergon 				     sector_t sector, int nr_iovecs,
1149e4c93811SAlasdair G Kergon 				     unsigned short idx, unsigned short bv_count,
1150e4c93811SAlasdair G Kergon 				     unsigned offset, unsigned len,
1151e4c93811SAlasdair G Kergon 				     unsigned split_bvec)
11525ae89a87SMike Snitzer {
1153dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
11545ae89a87SMike Snitzer 	struct dm_target_io *tio;
1155*b0d8ed4dSAlasdair G Kergon 	unsigned target_bio_nr;
1156*b0d8ed4dSAlasdair G Kergon 	unsigned num_target_bios = 1;
11575ae89a87SMike Snitzer 
1158*b0d8ed4dSAlasdair G Kergon 	/*
1159*b0d8ed4dSAlasdair G Kergon 	 * Does the target want to receive duplicate copies of the bio?
1160*b0d8ed4dSAlasdair G Kergon 	 */
1161*b0d8ed4dSAlasdair G Kergon 	if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
1162*b0d8ed4dSAlasdair G Kergon 		num_target_bios = ti->num_write_bios(ti, bio);
1163e4c93811SAlasdair G Kergon 
1164*b0d8ed4dSAlasdair G Kergon 	for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
1165*b0d8ed4dSAlasdair G Kergon 		tio = alloc_tio(ci, ti, nr_iovecs, target_bio_nr);
1166e4c93811SAlasdair G Kergon 		if (split_bvec)
1167e4c93811SAlasdair G Kergon 			clone_split_bio(tio, bio, sector, idx, offset, len);
1168e4c93811SAlasdair G Kergon 		else
1169e4c93811SAlasdair G Kergon 			clone_bio(tio, bio, sector, idx, bv_count, len);
1170bd2a49b8SAlasdair G Kergon 		__map_bio(tio);
11715ae89a87SMike Snitzer 	}
1172*b0d8ed4dSAlasdair G Kergon }
11735ae89a87SMike Snitzer 
117455a62eefSAlasdair G Kergon typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
117523508a96SMike Snitzer 
117655a62eefSAlasdair G Kergon static unsigned get_num_discard_bios(struct dm_target *ti)
117723508a96SMike Snitzer {
117855a62eefSAlasdair G Kergon 	return ti->num_discard_bios;
117923508a96SMike Snitzer }
118023508a96SMike Snitzer 
118155a62eefSAlasdair G Kergon static unsigned get_num_write_same_bios(struct dm_target *ti)
118223508a96SMike Snitzer {
118355a62eefSAlasdair G Kergon 	return ti->num_write_same_bios;
118423508a96SMike Snitzer }
118523508a96SMike Snitzer 
118623508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti);
118723508a96SMike Snitzer 
118823508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti)
118923508a96SMike Snitzer {
119055a62eefSAlasdair G Kergon 	return ti->split_discard_bios;
119123508a96SMike Snitzer }
119223508a96SMike Snitzer 
119314fe594dSAlasdair G Kergon static int __send_changing_extent_only(struct clone_info *ci,
119455a62eefSAlasdair G Kergon 				       get_num_bios_fn get_num_bios,
119523508a96SMike Snitzer 				       is_split_required_fn is_split_required)
11965ae89a87SMike Snitzer {
11975ae89a87SMike Snitzer 	struct dm_target *ti;
1198a79245b3SMike Snitzer 	sector_t len;
119955a62eefSAlasdair G Kergon 	unsigned num_bios;
12005ae89a87SMike Snitzer 
1201a79245b3SMike Snitzer 	do {
12025ae89a87SMike Snitzer 		ti = dm_table_find_target(ci->map, ci->sector);
12035ae89a87SMike Snitzer 		if (!dm_target_is_valid(ti))
12045ae89a87SMike Snitzer 			return -EIO;
12055ae89a87SMike Snitzer 
12065ae89a87SMike Snitzer 		/*
120723508a96SMike Snitzer 		 * Even though the device advertised support for this type of
120823508a96SMike Snitzer 		 * request, that does not mean every target supports it, and
1209936688d7SMike Snitzer 		 * reconfiguration might also have changed that since the
12105ae89a87SMike Snitzer 		 * check was performed.
12115ae89a87SMike Snitzer 		 */
121255a62eefSAlasdair G Kergon 		num_bios = get_num_bios ? get_num_bios(ti) : 0;
121355a62eefSAlasdair G Kergon 		if (!num_bios)
12145ae89a87SMike Snitzer 			return -EOPNOTSUPP;
12155ae89a87SMike Snitzer 
121623508a96SMike Snitzer 		if (is_split_required && !is_split_required(ti))
1217a79245b3SMike Snitzer 			len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
12187acf0277SMikulas Patocka 		else
12197acf0277SMikulas Patocka 			len = min(ci->sector_count, max_io_len(ci->sector, ti));
12205ae89a87SMike Snitzer 
122114fe594dSAlasdair G Kergon 		__send_duplicate_bios(ci, ti, num_bios, len);
12225ae89a87SMike Snitzer 
1223a79245b3SMike Snitzer 		ci->sector += len;
1224a79245b3SMike Snitzer 	} while (ci->sector_count -= len);
12255ae89a87SMike Snitzer 
12265ae89a87SMike Snitzer 	return 0;
12275ae89a87SMike Snitzer }
12285ae89a87SMike Snitzer 
122914fe594dSAlasdair G Kergon static int __send_discard(struct clone_info *ci)
123023508a96SMike Snitzer {
123114fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_discard_bios,
123223508a96SMike Snitzer 					   is_split_required_for_discard);
123323508a96SMike Snitzer }
123423508a96SMike Snitzer 
123514fe594dSAlasdair G Kergon static int __send_write_same(struct clone_info *ci)
123623508a96SMike Snitzer {
123714fe594dSAlasdair G Kergon 	return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
123823508a96SMike Snitzer }
123923508a96SMike Snitzer 
1240e4c93811SAlasdair G Kergon /*
1241e4c93811SAlasdair G Kergon  * Find maximum number of sectors / bvecs we can process with a single bio.
1242e4c93811SAlasdair G Kergon  */
1243e4c93811SAlasdair G Kergon static sector_t __len_within_target(struct clone_info *ci, sector_t max, int *idx)
12441da177e4SLinus Torvalds {
1245dba14160SMikulas Patocka 	struct bio *bio = ci->bio;
1246e4c93811SAlasdair G Kergon 	sector_t bv_len, total_len = 0;
12471da177e4SLinus Torvalds 
1248e4c93811SAlasdair G Kergon 	for (*idx = ci->idx; max && (*idx < bio->bi_vcnt); (*idx)++) {
1249e4c93811SAlasdair G Kergon 		bv_len = to_sector(bio->bi_io_vec[*idx].bv_len);
12505ae89a87SMike Snitzer 
1251e4c93811SAlasdair G Kergon 		if (bv_len > max)
12521da177e4SLinus Torvalds 			break;
12531da177e4SLinus Torvalds 
1254e4c93811SAlasdair G Kergon 		max -= bv_len;
1255e4c93811SAlasdair G Kergon 		total_len += bv_len;
12561da177e4SLinus Torvalds 	}
12571da177e4SLinus Torvalds 
1258e4c93811SAlasdair G Kergon 	return total_len;
1259e4c93811SAlasdair G Kergon }
12601da177e4SLinus Torvalds 
1261e4c93811SAlasdair G Kergon static int __split_bvec_across_targets(struct clone_info *ci,
1262e4c93811SAlasdair G Kergon 				       struct dm_target *ti, sector_t max)
1263e4c93811SAlasdair G Kergon {
1264e4c93811SAlasdair G Kergon 	struct bio *bio = ci->bio;
12651da177e4SLinus Torvalds 	struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1266d2044a94SAlasdair G Kergon 	sector_t remaining = to_sector(bv->bv_len);
1267e4c93811SAlasdair G Kergon 	unsigned offset = 0;
1268e4c93811SAlasdair G Kergon 	sector_t len;
12691da177e4SLinus Torvalds 
1270d2044a94SAlasdair G Kergon 	do {
1271d2044a94SAlasdair G Kergon 		if (offset) {
12721da177e4SLinus Torvalds 			ti = dm_table_find_target(ci->map, ci->sector);
1273512875bdSJun'ichi Nomura 			if (!dm_target_is_valid(ti))
1274512875bdSJun'ichi Nomura 				return -EIO;
1275512875bdSJun'ichi Nomura 
127656a67df7SMike Snitzer 			max = max_io_len(ci->sector, ti);
1277d2044a94SAlasdair G Kergon 		}
1278d2044a94SAlasdair G Kergon 
1279d2044a94SAlasdair G Kergon 		len = min(remaining, max);
1280d2044a94SAlasdair G Kergon 
1281e4c93811SAlasdair G Kergon 		__clone_and_map_data_bio(ci, ti, ci->sector, 1, ci->idx, 0,
1282e4c93811SAlasdair G Kergon 					 bv->bv_offset + offset, len, 1);
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 		ci->sector += len;
12851da177e4SLinus Torvalds 		ci->sector_count -= len;
1286d2044a94SAlasdair G Kergon 		offset += to_bytes(len);
1287d2044a94SAlasdair G Kergon 	} while (remaining -= len);
1288d2044a94SAlasdair G Kergon 
12891da177e4SLinus Torvalds 	ci->idx++;
1290512875bdSJun'ichi Nomura 
1291512875bdSJun'ichi Nomura 	return 0;
12921da177e4SLinus Torvalds }
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds /*
1295e4c93811SAlasdair G Kergon  * Select the correct strategy for processing a non-flush bio.
1296e4c93811SAlasdair G Kergon  */
1297e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci)
1298e4c93811SAlasdair G Kergon {
1299e4c93811SAlasdair G Kergon 	struct bio *bio = ci->bio;
1300e4c93811SAlasdair G Kergon 	struct dm_target *ti;
1301e4c93811SAlasdair G Kergon 	sector_t len, max;
1302e4c93811SAlasdair G Kergon 	int idx;
1303e4c93811SAlasdair G Kergon 
1304e4c93811SAlasdair G Kergon 	if (unlikely(bio->bi_rw & REQ_DISCARD))
1305e4c93811SAlasdair G Kergon 		return __send_discard(ci);
1306e4c93811SAlasdair G Kergon 	else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
1307e4c93811SAlasdair G Kergon 		return __send_write_same(ci);
1308e4c93811SAlasdair G Kergon 
1309e4c93811SAlasdair G Kergon 	ti = dm_table_find_target(ci->map, ci->sector);
1310e4c93811SAlasdair G Kergon 	if (!dm_target_is_valid(ti))
1311e4c93811SAlasdair G Kergon 		return -EIO;
1312e4c93811SAlasdair G Kergon 
1313e4c93811SAlasdair G Kergon 	max = max_io_len(ci->sector, ti);
1314e4c93811SAlasdair G Kergon 
1315e4c93811SAlasdair G Kergon 	/*
1316e4c93811SAlasdair G Kergon 	 * Optimise for the simple case where we can do all of
1317e4c93811SAlasdair G Kergon 	 * the remaining io with a single clone.
1318e4c93811SAlasdair G Kergon 	 */
1319e4c93811SAlasdair G Kergon 	if (ci->sector_count <= max) {
1320e4c93811SAlasdair G Kergon 		__clone_and_map_data_bio(ci, ti, ci->sector, bio->bi_max_vecs,
1321e4c93811SAlasdair G Kergon 					 ci->idx, bio->bi_vcnt - ci->idx, 0,
1322e4c93811SAlasdair G Kergon 					 ci->sector_count, 0);
1323e4c93811SAlasdair G Kergon 		ci->sector_count = 0;
1324e4c93811SAlasdair G Kergon 		return 0;
1325e4c93811SAlasdair G Kergon 	}
1326e4c93811SAlasdair G Kergon 
1327e4c93811SAlasdair G Kergon 	/*
1328e4c93811SAlasdair G Kergon 	 * There are some bvecs that don't span targets.
1329e4c93811SAlasdair G Kergon 	 * Do as many of these as possible.
1330e4c93811SAlasdair G Kergon 	 */
1331e4c93811SAlasdair G Kergon 	if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
1332e4c93811SAlasdair G Kergon 		len = __len_within_target(ci, max, &idx);
1333e4c93811SAlasdair G Kergon 
1334e4c93811SAlasdair G Kergon 		__clone_and_map_data_bio(ci, ti, ci->sector, bio->bi_max_vecs,
1335e4c93811SAlasdair G Kergon 					 ci->idx, idx - ci->idx, 0, len, 0);
1336e4c93811SAlasdair G Kergon 
1337e4c93811SAlasdair G Kergon 		ci->sector += len;
1338e4c93811SAlasdair G Kergon 		ci->sector_count -= len;
1339e4c93811SAlasdair G Kergon 		ci->idx = idx;
1340e4c93811SAlasdair G Kergon 
1341e4c93811SAlasdair G Kergon 		return 0;
1342e4c93811SAlasdair G Kergon 	}
1343e4c93811SAlasdair G Kergon 
1344e4c93811SAlasdair G Kergon 	/*
1345e4c93811SAlasdair G Kergon 	 * Handle a bvec that must be split between two or more targets.
1346e4c93811SAlasdair G Kergon 	 */
1347e4c93811SAlasdair G Kergon 	return __split_bvec_across_targets(ci, ti, max);
1348e4c93811SAlasdair G Kergon }
1349e4c93811SAlasdair G Kergon 
1350e4c93811SAlasdair G Kergon /*
135114fe594dSAlasdair G Kergon  * Entry point to split a bio into clones and submit them to the targets.
13521da177e4SLinus Torvalds  */
1353f0b9a450SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
13541da177e4SLinus Torvalds {
13551da177e4SLinus Torvalds 	struct clone_info ci;
1356512875bdSJun'ichi Nomura 	int error = 0;
13571da177e4SLinus Torvalds 
13587c666411SAlasdair G Kergon 	ci.map = dm_get_live_table(md);
1359f0b9a450SMikulas Patocka 	if (unlikely(!ci.map)) {
1360f0b9a450SMikulas Patocka 		bio_io_error(bio);
1361f0b9a450SMikulas Patocka 		return;
1362f0b9a450SMikulas Patocka 	}
1363692d0eb9SMikulas Patocka 
13641da177e4SLinus Torvalds 	ci.md = md;
13651da177e4SLinus Torvalds 	ci.io = alloc_io(md);
13661da177e4SLinus Torvalds 	ci.io->error = 0;
13671da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
13681da177e4SLinus Torvalds 	ci.io->bio = bio;
13691da177e4SLinus Torvalds 	ci.io->md = md;
1370f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
13711da177e4SLinus Torvalds 	ci.sector = bio->bi_sector;
13721da177e4SLinus Torvalds 	ci.idx = bio->bi_idx;
13731da177e4SLinus Torvalds 
13743eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1375bd2a49b8SAlasdair G Kergon 
1376b372d360SMike Snitzer 	if (bio->bi_rw & REQ_FLUSH) {
1377b372d360SMike Snitzer 		ci.bio = &ci.md->flush_bio;
1378b372d360SMike Snitzer 		ci.sector_count = 0;
137914fe594dSAlasdair G Kergon 		error = __send_empty_flush(&ci);
1380b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1381b372d360SMike Snitzer 	} else {
13826a8736d1STejun Heo 		ci.bio = bio;
1383f6fccb12SMilan Broz 		ci.sector_count = bio_sectors(bio);
1384512875bdSJun'ichi Nomura 		while (ci.sector_count && !error)
138514fe594dSAlasdair G Kergon 			error = __split_and_process_non_flush(&ci);
1386d87f4c14STejun Heo 	}
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 	/* drop the extra reference count */
1389512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
13901da177e4SLinus Torvalds 	dm_table_put(ci.map);
13919e4e5f87SMilan Broz }
13929e4e5f87SMilan Broz /*-----------------------------------------------------------------
13931da177e4SLinus Torvalds  * CRUD END
13941da177e4SLinus Torvalds  *---------------------------------------------------------------*/
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds static int dm_merge_bvec(struct request_queue *q,
13971da177e4SLinus Torvalds 			 struct bvec_merge_data *bvm,
1398f6fccb12SMilan Broz 			 struct bio_vec *biovec)
1399f6fccb12SMilan Broz {
1400f6fccb12SMilan Broz 	struct mapped_device *md = q->queuedata;
14017c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1402f6fccb12SMilan Broz 	struct dm_target *ti;
1403f6fccb12SMilan Broz 	sector_t max_sectors;
1404f6fccb12SMilan Broz 	int max_size = 0;
1405f6fccb12SMilan Broz 
1406f6fccb12SMilan Broz 	if (unlikely(!map))
1407f6fccb12SMilan Broz 		goto out;
1408f6fccb12SMilan Broz 
1409f6fccb12SMilan Broz 	ti = dm_table_find_target(map, bvm->bi_sector);
1410f6fccb12SMilan Broz 	if (!dm_target_is_valid(ti))
1411f6fccb12SMilan Broz 		goto out_table;
1412f6fccb12SMilan Broz 
1413f6fccb12SMilan Broz 	/*
1414f6fccb12SMilan Broz 	 * Find maximum amount of I/O that won't need splitting
1415f6fccb12SMilan Broz 	 */
141656a67df7SMike Snitzer 	max_sectors = min(max_io_len(bvm->bi_sector, ti),
1417f6fccb12SMilan Broz 			  (sector_t) BIO_MAX_SECTORS);
1418f6fccb12SMilan Broz 	max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1419f6fccb12SMilan Broz 	if (max_size < 0)
1420f6fccb12SMilan Broz 		max_size = 0;
1421f6fccb12SMilan Broz 
1422f6fccb12SMilan Broz 	/*
1423f6fccb12SMilan Broz 	 * merge_bvec_fn() returns number of bytes
1424f6fccb12SMilan Broz 	 * it can accept at this offset
1425f6fccb12SMilan Broz 	 * max is precomputed maximal io size
1426f6fccb12SMilan Broz 	 */
1427f6fccb12SMilan Broz 	if (max_size && ti->type->merge)
1428f6fccb12SMilan Broz 		max_size = ti->type->merge(ti, bvm, biovec, max_size);
14298cbeb67aSMikulas Patocka 	/*
14308cbeb67aSMikulas Patocka 	 * If the target doesn't support merge method and some of the devices
14318cbeb67aSMikulas Patocka 	 * provided their merge_bvec method (we know this by looking at
14328cbeb67aSMikulas Patocka 	 * queue_max_hw_sectors), then we can't allow bios with multiple vector
14338cbeb67aSMikulas Patocka 	 * entries.  So always set max_size to 0, and the code below allows
14348cbeb67aSMikulas Patocka 	 * just one page.
14358cbeb67aSMikulas Patocka 	 */
14368cbeb67aSMikulas Patocka 	else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
14378cbeb67aSMikulas Patocka 
14388cbeb67aSMikulas Patocka 		max_size = 0;
1439f6fccb12SMilan Broz 
1440b01cd5acSMikulas Patocka out_table:
14415037108aSMikulas Patocka 	dm_table_put(map);
14425037108aSMikulas Patocka 
14435037108aSMikulas Patocka out:
1444f6fccb12SMilan Broz 	/*
1445f6fccb12SMilan Broz 	 * Always allow an entire first page
1446f6fccb12SMilan Broz 	 */
1447f6fccb12SMilan Broz 	if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1448f6fccb12SMilan Broz 		max_size = biovec->bv_len;
1449f6fccb12SMilan Broz 
1450f6fccb12SMilan Broz 	return max_size;
1451f6fccb12SMilan Broz }
1452f6fccb12SMilan Broz 
14531da177e4SLinus Torvalds /*
14541da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
14551da177e4SLinus Torvalds  * dm_merge_bvec.
14561da177e4SLinus Torvalds  */
14575a7bbad2SChristoph Hellwig static void _dm_request(struct request_queue *q, struct bio *bio)
14581da177e4SLinus Torvalds {
145912f03a49SKevin Corry 	int rw = bio_data_dir(bio);
14601da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
1461c9959059STejun Heo 	int cpu;
14621da177e4SLinus Torvalds 
14632ca3310eSAlasdair G Kergon 	down_read(&md->io_lock);
14641da177e4SLinus Torvalds 
1465074a7acaSTejun Heo 	cpu = part_stat_lock();
1466074a7acaSTejun Heo 	part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1467074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1468074a7acaSTejun Heo 	part_stat_unlock();
146912f03a49SKevin Corry 
14706a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
14716a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
14722ca3310eSAlasdair G Kergon 		up_read(&md->io_lock);
14731da177e4SLinus Torvalds 
14746a8736d1STejun Heo 		if (bio_rw(bio) != READA)
147592c63902SMikulas Patocka 			queue_io(md, bio);
14766a8736d1STejun Heo 		else
14776a8736d1STejun Heo 			bio_io_error(bio);
14785a7bbad2SChristoph Hellwig 		return;
14791da177e4SLinus Torvalds 	}
14801da177e4SLinus Torvalds 
1481f0b9a450SMikulas Patocka 	__split_and_process_bio(md, bio);
14822ca3310eSAlasdair G Kergon 	up_read(&md->io_lock);
14835a7bbad2SChristoph Hellwig 	return;
1484cec47e3dSKiyoshi Ueda }
1485cec47e3dSKiyoshi Ueda 
1486cec47e3dSKiyoshi Ueda static int dm_request_based(struct mapped_device *md)
1487cec47e3dSKiyoshi Ueda {
1488cec47e3dSKiyoshi Ueda 	return blk_queue_stackable(md->queue);
1489cec47e3dSKiyoshi Ueda }
1490cec47e3dSKiyoshi Ueda 
14915a7bbad2SChristoph Hellwig static void dm_request(struct request_queue *q, struct bio *bio)
1492cec47e3dSKiyoshi Ueda {
1493cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1494cec47e3dSKiyoshi Ueda 
1495cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
14965a7bbad2SChristoph Hellwig 		blk_queue_bio(q, bio);
14975a7bbad2SChristoph Hellwig 	else
14985a7bbad2SChristoph Hellwig 		_dm_request(q, bio);
1499cec47e3dSKiyoshi Ueda }
1500cec47e3dSKiyoshi Ueda 
1501cec47e3dSKiyoshi Ueda void dm_dispatch_request(struct request *rq)
1502cec47e3dSKiyoshi Ueda {
1503cec47e3dSKiyoshi Ueda 	int r;
1504cec47e3dSKiyoshi Ueda 
1505cec47e3dSKiyoshi Ueda 	if (blk_queue_io_stat(rq->q))
1506cec47e3dSKiyoshi Ueda 		rq->cmd_flags |= REQ_IO_STAT;
1507cec47e3dSKiyoshi Ueda 
1508cec47e3dSKiyoshi Ueda 	rq->start_time = jiffies;
1509cec47e3dSKiyoshi Ueda 	r = blk_insert_cloned_request(rq->q, rq);
1510cec47e3dSKiyoshi Ueda 	if (r)
1511cec47e3dSKiyoshi Ueda 		dm_complete_request(rq, r);
1512cec47e3dSKiyoshi Ueda }
1513cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_dispatch_request);
1514cec47e3dSKiyoshi Ueda 
1515cec47e3dSKiyoshi Ueda static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1516cec47e3dSKiyoshi Ueda 				 void *data)
1517cec47e3dSKiyoshi Ueda {
1518cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = data;
151994818742SKent Overstreet 	struct dm_rq_clone_bio_info *info =
152094818742SKent Overstreet 		container_of(bio, struct dm_rq_clone_bio_info, clone);
1521cec47e3dSKiyoshi Ueda 
1522cec47e3dSKiyoshi Ueda 	info->orig = bio_orig;
1523cec47e3dSKiyoshi Ueda 	info->tio = tio;
1524cec47e3dSKiyoshi Ueda 	bio->bi_end_io = end_clone_bio;
1525cec47e3dSKiyoshi Ueda 	bio->bi_private = info;
1526cec47e3dSKiyoshi Ueda 
1527cec47e3dSKiyoshi Ueda 	return 0;
1528cec47e3dSKiyoshi Ueda }
1529cec47e3dSKiyoshi Ueda 
1530cec47e3dSKiyoshi Ueda static int setup_clone(struct request *clone, struct request *rq,
1531cec47e3dSKiyoshi Ueda 		       struct dm_rq_target_io *tio)
1532cec47e3dSKiyoshi Ueda {
1533d0bcb878SKiyoshi Ueda 	int r;
1534cec47e3dSKiyoshi Ueda 
1535d0bcb878SKiyoshi Ueda 	r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1536d0bcb878SKiyoshi Ueda 			      dm_rq_bio_constructor, tio);
1537cec47e3dSKiyoshi Ueda 	if (r)
1538cec47e3dSKiyoshi Ueda 		return r;
1539cec47e3dSKiyoshi Ueda 
1540cec47e3dSKiyoshi Ueda 	clone->cmd = rq->cmd;
1541cec47e3dSKiyoshi Ueda 	clone->cmd_len = rq->cmd_len;
1542cec47e3dSKiyoshi Ueda 	clone->sense = rq->sense;
1543cec47e3dSKiyoshi Ueda 	clone->buffer = rq->buffer;
1544cec47e3dSKiyoshi Ueda 	clone->end_io = end_clone_request;
1545cec47e3dSKiyoshi Ueda 	clone->end_io_data = tio;
1546cec47e3dSKiyoshi Ueda 
1547cec47e3dSKiyoshi Ueda 	return 0;
1548cec47e3dSKiyoshi Ueda }
1549cec47e3dSKiyoshi Ueda 
15506facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md,
15516facdaffSKiyoshi Ueda 				gfp_t gfp_mask)
15526facdaffSKiyoshi Ueda {
15536facdaffSKiyoshi Ueda 	struct request *clone;
15546facdaffSKiyoshi Ueda 	struct dm_rq_target_io *tio;
15556facdaffSKiyoshi Ueda 
15566facdaffSKiyoshi Ueda 	tio = alloc_rq_tio(md, gfp_mask);
15576facdaffSKiyoshi Ueda 	if (!tio)
15586facdaffSKiyoshi Ueda 		return NULL;
15596facdaffSKiyoshi Ueda 
15606facdaffSKiyoshi Ueda 	tio->md = md;
15616facdaffSKiyoshi Ueda 	tio->ti = NULL;
15626facdaffSKiyoshi Ueda 	tio->orig = rq;
15636facdaffSKiyoshi Ueda 	tio->error = 0;
15646facdaffSKiyoshi Ueda 	memset(&tio->info, 0, sizeof(tio->info));
15656facdaffSKiyoshi Ueda 
15666facdaffSKiyoshi Ueda 	clone = &tio->clone;
15676facdaffSKiyoshi Ueda 	if (setup_clone(clone, rq, tio)) {
15686facdaffSKiyoshi Ueda 		/* -ENOMEM */
15696facdaffSKiyoshi Ueda 		free_rq_tio(tio);
15706facdaffSKiyoshi Ueda 		return NULL;
15716facdaffSKiyoshi Ueda 	}
15726facdaffSKiyoshi Ueda 
15736facdaffSKiyoshi Ueda 	return clone;
15746facdaffSKiyoshi Ueda }
15756facdaffSKiyoshi Ueda 
1576cec47e3dSKiyoshi Ueda /*
1577cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1578cec47e3dSKiyoshi Ueda  */
1579cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq)
1580cec47e3dSKiyoshi Ueda {
1581cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1582cec47e3dSKiyoshi Ueda 	struct request *clone;
1583cec47e3dSKiyoshi Ueda 
1584cec47e3dSKiyoshi Ueda 	if (unlikely(rq->special)) {
1585cec47e3dSKiyoshi Ueda 		DMWARN("Already has something in rq->special.");
1586cec47e3dSKiyoshi Ueda 		return BLKPREP_KILL;
1587cec47e3dSKiyoshi Ueda 	}
1588cec47e3dSKiyoshi Ueda 
15896facdaffSKiyoshi Ueda 	clone = clone_rq(rq, md, GFP_ATOMIC);
15906facdaffSKiyoshi Ueda 	if (!clone)
1591cec47e3dSKiyoshi Ueda 		return BLKPREP_DEFER;
1592cec47e3dSKiyoshi Ueda 
1593cec47e3dSKiyoshi Ueda 	rq->special = clone;
1594cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_DONTPREP;
1595cec47e3dSKiyoshi Ueda 
1596cec47e3dSKiyoshi Ueda 	return BLKPREP_OK;
1597cec47e3dSKiyoshi Ueda }
1598cec47e3dSKiyoshi Ueda 
15999eef87daSKiyoshi Ueda /*
16009eef87daSKiyoshi Ueda  * Returns:
16019eef87daSKiyoshi Ueda  * 0  : the request has been processed (not requeued)
16029eef87daSKiyoshi Ueda  * !0 : the request has been requeued
16039eef87daSKiyoshi Ueda  */
16049eef87daSKiyoshi Ueda static int map_request(struct dm_target *ti, struct request *clone,
1605cec47e3dSKiyoshi Ueda 		       struct mapped_device *md)
1606cec47e3dSKiyoshi Ueda {
16079eef87daSKiyoshi Ueda 	int r, requeued = 0;
1608cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1609cec47e3dSKiyoshi Ueda 
1610cec47e3dSKiyoshi Ueda 	tio->ti = ti;
1611cec47e3dSKiyoshi Ueda 	r = ti->type->map_rq(ti, clone, &tio->info);
1612cec47e3dSKiyoshi Ueda 	switch (r) {
1613cec47e3dSKiyoshi Ueda 	case DM_MAPIO_SUBMITTED:
1614cec47e3dSKiyoshi Ueda 		/* The target has taken the I/O to submit by itself later */
1615cec47e3dSKiyoshi Ueda 		break;
1616cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REMAPPED:
1617cec47e3dSKiyoshi Ueda 		/* The target has remapped the I/O so dispatch it */
16186db4ccd6SJun'ichi Nomura 		trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
16196db4ccd6SJun'ichi Nomura 				     blk_rq_pos(tio->orig));
1620cec47e3dSKiyoshi Ueda 		dm_dispatch_request(clone);
1621cec47e3dSKiyoshi Ueda 		break;
1622cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REQUEUE:
1623cec47e3dSKiyoshi Ueda 		/* The target wants to requeue the I/O */
1624cec47e3dSKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
16259eef87daSKiyoshi Ueda 		requeued = 1;
1626cec47e3dSKiyoshi Ueda 		break;
1627cec47e3dSKiyoshi Ueda 	default:
1628cec47e3dSKiyoshi Ueda 		if (r > 0) {
1629cec47e3dSKiyoshi Ueda 			DMWARN("unimplemented target map return value: %d", r);
1630cec47e3dSKiyoshi Ueda 			BUG();
1631cec47e3dSKiyoshi Ueda 		}
1632cec47e3dSKiyoshi Ueda 
1633cec47e3dSKiyoshi Ueda 		/* The target wants to complete the I/O */
1634cec47e3dSKiyoshi Ueda 		dm_kill_unmapped_request(clone, r);
1635cec47e3dSKiyoshi Ueda 		break;
1636cec47e3dSKiyoshi Ueda 	}
16379eef87daSKiyoshi Ueda 
16389eef87daSKiyoshi Ueda 	return requeued;
1639cec47e3dSKiyoshi Ueda }
1640cec47e3dSKiyoshi Ueda 
1641ba1cbad9SMike Snitzer static struct request *dm_start_request(struct mapped_device *md, struct request *orig)
1642ba1cbad9SMike Snitzer {
1643ba1cbad9SMike Snitzer 	struct request *clone;
1644ba1cbad9SMike Snitzer 
1645ba1cbad9SMike Snitzer 	blk_start_request(orig);
1646ba1cbad9SMike Snitzer 	clone = orig->special;
1647ba1cbad9SMike Snitzer 	atomic_inc(&md->pending[rq_data_dir(clone)]);
1648ba1cbad9SMike Snitzer 
1649ba1cbad9SMike Snitzer 	/*
1650ba1cbad9SMike Snitzer 	 * Hold the md reference here for the in-flight I/O.
1651ba1cbad9SMike Snitzer 	 * We can't rely on the reference count by device opener,
1652ba1cbad9SMike Snitzer 	 * because the device may be closed during the request completion
1653ba1cbad9SMike Snitzer 	 * when all bios are completed.
1654ba1cbad9SMike Snitzer 	 * See the comment in rq_completed() too.
1655ba1cbad9SMike Snitzer 	 */
1656ba1cbad9SMike Snitzer 	dm_get(md);
1657ba1cbad9SMike Snitzer 
1658ba1cbad9SMike Snitzer 	return clone;
1659ba1cbad9SMike Snitzer }
1660ba1cbad9SMike Snitzer 
1661cec47e3dSKiyoshi Ueda /*
1662cec47e3dSKiyoshi Ueda  * q->request_fn for request-based dm.
1663cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1664cec47e3dSKiyoshi Ueda  */
1665cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q)
1666cec47e3dSKiyoshi Ueda {
1667cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
16687c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1669cec47e3dSKiyoshi Ueda 	struct dm_target *ti;
1670b4324feeSKiyoshi Ueda 	struct request *rq, *clone;
167129e4013dSTejun Heo 	sector_t pos;
1672cec47e3dSKiyoshi Ueda 
1673cec47e3dSKiyoshi Ueda 	/*
1674b4324feeSKiyoshi Ueda 	 * For suspend, check blk_queue_stopped() and increment
1675b4324feeSKiyoshi Ueda 	 * ->pending within a single queue_lock not to increment the
1676b4324feeSKiyoshi Ueda 	 * number of in-flight I/Os after the queue is stopped in
1677b4324feeSKiyoshi Ueda 	 * dm_suspend().
1678cec47e3dSKiyoshi Ueda 	 */
16797eaceaccSJens Axboe 	while (!blk_queue_stopped(q)) {
1680cec47e3dSKiyoshi Ueda 		rq = blk_peek_request(q);
1681cec47e3dSKiyoshi Ueda 		if (!rq)
16827eaceaccSJens Axboe 			goto delay_and_out;
1683cec47e3dSKiyoshi Ueda 
168429e4013dSTejun Heo 		/* always use block 0 to find the target for flushes for now */
168529e4013dSTejun Heo 		pos = 0;
168629e4013dSTejun Heo 		if (!(rq->cmd_flags & REQ_FLUSH))
168729e4013dSTejun Heo 			pos = blk_rq_pos(rq);
1688d0bcb878SKiyoshi Ueda 
168929e4013dSTejun Heo 		ti = dm_table_find_target(map, pos);
1690ba1cbad9SMike Snitzer 		if (!dm_target_is_valid(ti)) {
1691ba1cbad9SMike Snitzer 			/*
1692ba1cbad9SMike Snitzer 			 * Must perform setup, that dm_done() requires,
1693ba1cbad9SMike Snitzer 			 * before calling dm_kill_unmapped_request
1694ba1cbad9SMike Snitzer 			 */
1695ba1cbad9SMike Snitzer 			DMERR_LIMIT("request attempted access beyond the end of device");
1696ba1cbad9SMike Snitzer 			clone = dm_start_request(md, rq);
1697ba1cbad9SMike Snitzer 			dm_kill_unmapped_request(clone, -EIO);
1698ba1cbad9SMike Snitzer 			continue;
1699ba1cbad9SMike Snitzer 		}
170029e4013dSTejun Heo 
1701cec47e3dSKiyoshi Ueda 		if (ti->type->busy && ti->type->busy(ti))
17027eaceaccSJens Axboe 			goto delay_and_out;
1703cec47e3dSKiyoshi Ueda 
1704ba1cbad9SMike Snitzer 		clone = dm_start_request(md, rq);
1705b4324feeSKiyoshi Ueda 
1706cec47e3dSKiyoshi Ueda 		spin_unlock(q->queue_lock);
17079eef87daSKiyoshi Ueda 		if (map_request(ti, clone, md))
17089eef87daSKiyoshi Ueda 			goto requeued;
17099eef87daSKiyoshi Ueda 
1710052189a2SKiyoshi Ueda 		BUG_ON(!irqs_disabled());
1711052189a2SKiyoshi Ueda 		spin_lock(q->queue_lock);
1712cec47e3dSKiyoshi Ueda 	}
1713cec47e3dSKiyoshi Ueda 
1714cec47e3dSKiyoshi Ueda 	goto out;
1715cec47e3dSKiyoshi Ueda 
17169eef87daSKiyoshi Ueda requeued:
1717052189a2SKiyoshi Ueda 	BUG_ON(!irqs_disabled());
1718052189a2SKiyoshi Ueda 	spin_lock(q->queue_lock);
17199eef87daSKiyoshi Ueda 
17207eaceaccSJens Axboe delay_and_out:
17217eaceaccSJens Axboe 	blk_delay_queue(q, HZ / 10);
1722cec47e3dSKiyoshi Ueda out:
1723cec47e3dSKiyoshi Ueda 	dm_table_put(map);
1724cec47e3dSKiyoshi Ueda }
1725cec47e3dSKiyoshi Ueda 
1726cec47e3dSKiyoshi Ueda int dm_underlying_device_busy(struct request_queue *q)
1727cec47e3dSKiyoshi Ueda {
1728cec47e3dSKiyoshi Ueda 	return blk_lld_busy(q);
1729cec47e3dSKiyoshi Ueda }
1730cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1731cec47e3dSKiyoshi Ueda 
1732cec47e3dSKiyoshi Ueda static int dm_lld_busy(struct request_queue *q)
1733cec47e3dSKiyoshi Ueda {
1734cec47e3dSKiyoshi Ueda 	int r;
1735cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
17367c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1737cec47e3dSKiyoshi Ueda 
1738cec47e3dSKiyoshi Ueda 	if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1739cec47e3dSKiyoshi Ueda 		r = 1;
1740cec47e3dSKiyoshi Ueda 	else
1741cec47e3dSKiyoshi Ueda 		r = dm_table_any_busy_target(map);
1742cec47e3dSKiyoshi Ueda 
1743cec47e3dSKiyoshi Ueda 	dm_table_put(map);
1744cec47e3dSKiyoshi Ueda 
1745cec47e3dSKiyoshi Ueda 	return r;
1746cec47e3dSKiyoshi Ueda }
1747cec47e3dSKiyoshi Ueda 
17481da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
17491da177e4SLinus Torvalds {
17508a57dfc6SChandra Seetharaman 	int r = bdi_bits;
17518a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
17528a57dfc6SChandra Seetharaman 	struct dm_table *map;
17531da177e4SLinus Torvalds 
17541eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
17557c666411SAlasdair G Kergon 		map = dm_get_live_table(md);
17568a57dfc6SChandra Seetharaman 		if (map) {
1757cec47e3dSKiyoshi Ueda 			/*
1758cec47e3dSKiyoshi Ueda 			 * Request-based dm cares about only own queue for
1759cec47e3dSKiyoshi Ueda 			 * the query about congestion status of request_queue
1760cec47e3dSKiyoshi Ueda 			 */
1761cec47e3dSKiyoshi Ueda 			if (dm_request_based(md))
1762cec47e3dSKiyoshi Ueda 				r = md->queue->backing_dev_info.state &
1763cec47e3dSKiyoshi Ueda 				    bdi_bits;
1764cec47e3dSKiyoshi Ueda 			else
17651da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
1766cec47e3dSKiyoshi Ueda 
17671da177e4SLinus Torvalds 			dm_table_put(map);
17688a57dfc6SChandra Seetharaman 		}
17698a57dfc6SChandra Seetharaman 	}
17708a57dfc6SChandra Seetharaman 
17711da177e4SLinus Torvalds 	return r;
17721da177e4SLinus Torvalds }
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds /*-----------------------------------------------------------------
17751da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
17761da177e4SLinus Torvalds  *---------------------------------------------------------------*/
17772b06cfffSAlasdair G Kergon static void free_minor(int minor)
17781da177e4SLinus Torvalds {
1779f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17801da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1781f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
17821da177e4SLinus Torvalds }
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds /*
17851da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
17861da177e4SLinus Torvalds  */
1787cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
17881da177e4SLinus Torvalds {
1789c9d76be6STejun Heo 	int r;
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
17921da177e4SLinus Torvalds 		return -EINVAL;
17931da177e4SLinus Torvalds 
1794c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1795f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17961da177e4SLinus Torvalds 
1797c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
17981da177e4SLinus Torvalds 
1799f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1800c9d76be6STejun Heo 	idr_preload_end();
1801c9d76be6STejun Heo 	if (r < 0)
1802c9d76be6STejun Heo 		return r == -ENOSPC ? -EBUSY : r;
1803c9d76be6STejun Heo 	return 0;
18041da177e4SLinus Torvalds }
18051da177e4SLinus Torvalds 
1806cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
18071da177e4SLinus Torvalds {
1808c9d76be6STejun Heo 	int r;
18091da177e4SLinus Torvalds 
1810c9d76be6STejun Heo 	idr_preload(GFP_KERNEL);
1811f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
18121da177e4SLinus Torvalds 
1813c9d76be6STejun Heo 	r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
18141da177e4SLinus Torvalds 
1815f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1816c9d76be6STejun Heo 	idr_preload_end();
1817c9d76be6STejun Heo 	if (r < 0)
18181da177e4SLinus Torvalds 		return r;
1819c9d76be6STejun Heo 	*minor = r;
1820c9d76be6STejun Heo 	return 0;
18211da177e4SLinus Torvalds }
18221da177e4SLinus Torvalds 
182383d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
18241da177e4SLinus Torvalds 
182553d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
182653d5914fSMikulas Patocka 
18274a0b4ddfSMike Snitzer static void dm_init_md_queue(struct mapped_device *md)
18284a0b4ddfSMike Snitzer {
18294a0b4ddfSMike Snitzer 	/*
18304a0b4ddfSMike Snitzer 	 * Request-based dm devices cannot be stacked on top of bio-based dm
18314a0b4ddfSMike Snitzer 	 * devices.  The type of this dm device has not been decided yet.
18324a0b4ddfSMike Snitzer 	 * The type is decided at the first table loading time.
18334a0b4ddfSMike Snitzer 	 * To prevent problematic device stacking, clear the queue flag
18344a0b4ddfSMike Snitzer 	 * for request stacking support until then.
18354a0b4ddfSMike Snitzer 	 *
18364a0b4ddfSMike Snitzer 	 * This queue is new, so no concurrency on the queue_flags.
18374a0b4ddfSMike Snitzer 	 */
18384a0b4ddfSMike Snitzer 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
18394a0b4ddfSMike Snitzer 
18404a0b4ddfSMike Snitzer 	md->queue->queuedata = md;
18414a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
18424a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_data = md;
18434a0b4ddfSMike Snitzer 	blk_queue_make_request(md->queue, dm_request);
18444a0b4ddfSMike Snitzer 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
18454a0b4ddfSMike Snitzer 	blk_queue_merge_bvec(md->queue, dm_merge_bvec);
18464a0b4ddfSMike Snitzer }
18474a0b4ddfSMike Snitzer 
18481da177e4SLinus Torvalds /*
18491da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
18501da177e4SLinus Torvalds  */
18512b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
18521da177e4SLinus Torvalds {
18531da177e4SLinus Torvalds 	int r;
1854cf13ab8eSFrederik Deweerdt 	struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1855ba61fdd1SJeff Mahoney 	void *old_md;
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds 	if (!md) {
18581da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
18591da177e4SLinus Torvalds 		return NULL;
18601da177e4SLinus Torvalds 	}
18611da177e4SLinus Torvalds 
186210da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
18636ed7ade8SMilan Broz 		goto bad_module_get;
186410da4f79SJeff Mahoney 
18651da177e4SLinus Torvalds 	/* get a minor number for the dev */
18662b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1867cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
18682b06cfffSAlasdair G Kergon 	else
1869cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
18701da177e4SLinus Torvalds 	if (r < 0)
18716ed7ade8SMilan Broz 		goto bad_minor;
18721da177e4SLinus Torvalds 
1873a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
18742ca3310eSAlasdair G Kergon 	init_rwsem(&md->io_lock);
1875e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1876a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
1877022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
18781da177e4SLinus Torvalds 	rwlock_init(&md->map_lock);
18791da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
18805c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
18811da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
18827a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
18837a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
18847a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
18851da177e4SLinus Torvalds 
18864a0b4ddfSMike Snitzer 	md->queue = blk_alloc_queue(GFP_KERNEL);
18871da177e4SLinus Torvalds 	if (!md->queue)
18886ed7ade8SMilan Broz 		goto bad_queue;
18891da177e4SLinus Torvalds 
18904a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
18919faf400fSStefan Bader 
18921da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
18931da177e4SLinus Torvalds 	if (!md->disk)
18946ed7ade8SMilan Broz 		goto bad_disk;
18951da177e4SLinus Torvalds 
1896316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
1897316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
1898f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
189953d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1900f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
1901f0b04115SJeff Mahoney 
19021da177e4SLinus Torvalds 	md->disk->major = _major;
19031da177e4SLinus Torvalds 	md->disk->first_minor = minor;
19041da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
19051da177e4SLinus Torvalds 	md->disk->queue = md->queue;
19061da177e4SLinus Torvalds 	md->disk->private_data = md;
19071da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
19081da177e4SLinus Torvalds 	add_disk(md->disk);
19097e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
19101da177e4SLinus Torvalds 
19119c4376deSTejun Heo 	md->wq = alloc_workqueue("kdmflush",
19129c4376deSTejun Heo 				 WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0);
1913304f3f6aSMilan Broz 	if (!md->wq)
1914304f3f6aSMilan Broz 		goto bad_thread;
1915304f3f6aSMilan Broz 
191632a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
191732a926daSMikulas Patocka 	if (!md->bdev)
191832a926daSMikulas Patocka 		goto bad_bdev;
191932a926daSMikulas Patocka 
19206a8736d1STejun Heo 	bio_init(&md->flush_bio);
19216a8736d1STejun Heo 	md->flush_bio.bi_bdev = md->bdev;
19226a8736d1STejun Heo 	md->flush_bio.bi_rw = WRITE_FLUSH;
19236a8736d1STejun Heo 
1924ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1925f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1926ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1927f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1928ba61fdd1SJeff Mahoney 
1929ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1930ba61fdd1SJeff Mahoney 
19311da177e4SLinus Torvalds 	return md;
19321da177e4SLinus Torvalds 
193332a926daSMikulas Patocka bad_bdev:
193432a926daSMikulas Patocka 	destroy_workqueue(md->wq);
1935304f3f6aSMilan Broz bad_thread:
193603022c54SZdenek Kabelac 	del_gendisk(md->disk);
1937304f3f6aSMilan Broz 	put_disk(md->disk);
19386ed7ade8SMilan Broz bad_disk:
19391312f40eSAl Viro 	blk_cleanup_queue(md->queue);
19406ed7ade8SMilan Broz bad_queue:
19411da177e4SLinus Torvalds 	free_minor(minor);
19426ed7ade8SMilan Broz bad_minor:
194310da4f79SJeff Mahoney 	module_put(THIS_MODULE);
19446ed7ade8SMilan Broz bad_module_get:
19451da177e4SLinus Torvalds 	kfree(md);
19461da177e4SLinus Torvalds 	return NULL;
19471da177e4SLinus Torvalds }
19481da177e4SLinus Torvalds 
1949ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1950ae9da83fSJun'ichi Nomura 
19511da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
19521da177e4SLinus Torvalds {
1953f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
195463d94e48SJun'ichi Nomura 
1955ae9da83fSJun'ichi Nomura 	unlock_fs(md);
1956db8fef4fSMikulas Patocka 	bdput(md->bdev);
1957304f3f6aSMilan Broz 	destroy_workqueue(md->wq);
1958e6ee8c0bSKiyoshi Ueda 	if (md->io_pool)
19591da177e4SLinus Torvalds 		mempool_destroy(md->io_pool);
1960e6ee8c0bSKiyoshi Ueda 	if (md->bs)
19619faf400fSStefan Bader 		bioset_free(md->bs);
19629c47008dSMartin K. Petersen 	blk_integrity_unregister(md->disk);
19631da177e4SLinus Torvalds 	del_gendisk(md->disk);
196463d94e48SJun'ichi Nomura 	free_minor(minor);
1965fba9f90eSJeff Mahoney 
1966fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
1967fba9f90eSJeff Mahoney 	md->disk->private_data = NULL;
1968fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
1969fba9f90eSJeff Mahoney 
19701da177e4SLinus Torvalds 	put_disk(md->disk);
19711312f40eSAl Viro 	blk_cleanup_queue(md->queue);
197210da4f79SJeff Mahoney 	module_put(THIS_MODULE);
19731da177e4SLinus Torvalds 	kfree(md);
19741da177e4SLinus Torvalds }
19751da177e4SLinus Torvalds 
1976e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1977e6ee8c0bSKiyoshi Ueda {
1978c0820cf5SMikulas Patocka 	struct dm_md_mempools *p = dm_table_get_md_mempools(t);
1979e6ee8c0bSKiyoshi Ueda 
19805f015204SJun'ichi Nomura 	if (md->io_pool && md->bs) {
198116245bdcSJun'ichi Nomura 		/* The md already has necessary mempools. */
198216245bdcSJun'ichi Nomura 		if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
1983c0820cf5SMikulas Patocka 			/*
198416245bdcSJun'ichi Nomura 			 * Reload bioset because front_pad may have changed
198516245bdcSJun'ichi Nomura 			 * because a different table was loaded.
1986c0820cf5SMikulas Patocka 			 */
1987c0820cf5SMikulas Patocka 			bioset_free(md->bs);
1988c0820cf5SMikulas Patocka 			md->bs = p->bs;
1989c0820cf5SMikulas Patocka 			p->bs = NULL;
199016245bdcSJun'ichi Nomura 		} else if (dm_table_get_type(t) == DM_TYPE_REQUEST_BASED) {
199116245bdcSJun'ichi Nomura 			/*
199216245bdcSJun'ichi Nomura 			 * There's no need to reload with request-based dm
199316245bdcSJun'ichi Nomura 			 * because the size of front_pad doesn't change.
199416245bdcSJun'ichi Nomura 			 * Note for future: If you are to reload bioset,
199516245bdcSJun'ichi Nomura 			 * prep-ed requests in the queue may refer
199616245bdcSJun'ichi Nomura 			 * to bio from the old bioset, so you must walk
199716245bdcSJun'ichi Nomura 			 * through the queue to unprep.
199816245bdcSJun'ichi Nomura 			 */
199916245bdcSJun'ichi Nomura 		}
2000e6ee8c0bSKiyoshi Ueda 		goto out;
2001c0820cf5SMikulas Patocka 	}
2002e6ee8c0bSKiyoshi Ueda 
20035f015204SJun'ichi Nomura 	BUG_ON(!p || md->io_pool || md->bs);
2004e6ee8c0bSKiyoshi Ueda 
2005e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
2006e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
2007e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
2008e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
2009e6ee8c0bSKiyoshi Ueda 
2010e6ee8c0bSKiyoshi Ueda out:
2011e6ee8c0bSKiyoshi Ueda 	/* mempool bind completed, now no need any mempools in the table */
2012e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
2013e6ee8c0bSKiyoshi Ueda }
2014e6ee8c0bSKiyoshi Ueda 
20151da177e4SLinus Torvalds /*
20161da177e4SLinus Torvalds  * Bind a table to the device.
20171da177e4SLinus Torvalds  */
20181da177e4SLinus Torvalds static void event_callback(void *context)
20191da177e4SLinus Torvalds {
20207a8c3d3bSMike Anderson 	unsigned long flags;
20217a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
20221da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
20231da177e4SLinus Torvalds 
20247a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
20257a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
20267a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
20277a8c3d3bSMike Anderson 
2028ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
20297a8c3d3bSMike Anderson 
20301da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
20311da177e4SLinus Torvalds 	wake_up(&md->eventq);
20321da177e4SLinus Torvalds }
20331da177e4SLinus Torvalds 
2034c217649bSMike Snitzer /*
2035c217649bSMike Snitzer  * Protected by md->suspend_lock obtained by dm_swap_table().
2036c217649bSMike Snitzer  */
20374e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
20381da177e4SLinus Torvalds {
20394e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
20401da177e4SLinus Torvalds 
2041db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
20421da177e4SLinus Torvalds }
20431da177e4SLinus Torvalds 
2044042d2a9bSAlasdair G Kergon /*
2045d5b9dd04SMikulas Patocka  * Return 1 if the queue has a compulsory merge_bvec_fn function.
2046d5b9dd04SMikulas Patocka  *
2047d5b9dd04SMikulas Patocka  * If this function returns 0, then the device is either a non-dm
2048d5b9dd04SMikulas Patocka  * device without a merge_bvec_fn, or it is a dm device that is
2049d5b9dd04SMikulas Patocka  * able to split any bios it receives that are too big.
2050d5b9dd04SMikulas Patocka  */
2051d5b9dd04SMikulas Patocka int dm_queue_merge_is_compulsory(struct request_queue *q)
2052d5b9dd04SMikulas Patocka {
2053d5b9dd04SMikulas Patocka 	struct mapped_device *dev_md;
2054d5b9dd04SMikulas Patocka 
2055d5b9dd04SMikulas Patocka 	if (!q->merge_bvec_fn)
2056d5b9dd04SMikulas Patocka 		return 0;
2057d5b9dd04SMikulas Patocka 
2058d5b9dd04SMikulas Patocka 	if (q->make_request_fn == dm_request) {
2059d5b9dd04SMikulas Patocka 		dev_md = q->queuedata;
2060d5b9dd04SMikulas Patocka 		if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
2061d5b9dd04SMikulas Patocka 			return 0;
2062d5b9dd04SMikulas Patocka 	}
2063d5b9dd04SMikulas Patocka 
2064d5b9dd04SMikulas Patocka 	return 1;
2065d5b9dd04SMikulas Patocka }
2066d5b9dd04SMikulas Patocka 
2067d5b9dd04SMikulas Patocka static int dm_device_merge_is_compulsory(struct dm_target *ti,
2068d5b9dd04SMikulas Patocka 					 struct dm_dev *dev, sector_t start,
2069d5b9dd04SMikulas Patocka 					 sector_t len, void *data)
2070d5b9dd04SMikulas Patocka {
2071d5b9dd04SMikulas Patocka 	struct block_device *bdev = dev->bdev;
2072d5b9dd04SMikulas Patocka 	struct request_queue *q = bdev_get_queue(bdev);
2073d5b9dd04SMikulas Patocka 
2074d5b9dd04SMikulas Patocka 	return dm_queue_merge_is_compulsory(q);
2075d5b9dd04SMikulas Patocka }
2076d5b9dd04SMikulas Patocka 
2077d5b9dd04SMikulas Patocka /*
2078d5b9dd04SMikulas Patocka  * Return 1 if it is acceptable to ignore merge_bvec_fn based
2079d5b9dd04SMikulas Patocka  * on the properties of the underlying devices.
2080d5b9dd04SMikulas Patocka  */
2081d5b9dd04SMikulas Patocka static int dm_table_merge_is_optional(struct dm_table *table)
2082d5b9dd04SMikulas Patocka {
2083d5b9dd04SMikulas Patocka 	unsigned i = 0;
2084d5b9dd04SMikulas Patocka 	struct dm_target *ti;
2085d5b9dd04SMikulas Patocka 
2086d5b9dd04SMikulas Patocka 	while (i < dm_table_get_num_targets(table)) {
2087d5b9dd04SMikulas Patocka 		ti = dm_table_get_target(table, i++);
2088d5b9dd04SMikulas Patocka 
2089d5b9dd04SMikulas Patocka 		if (ti->type->iterate_devices &&
2090d5b9dd04SMikulas Patocka 		    ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
2091d5b9dd04SMikulas Patocka 			return 0;
2092d5b9dd04SMikulas Patocka 	}
2093d5b9dd04SMikulas Patocka 
2094d5b9dd04SMikulas Patocka 	return 1;
2095d5b9dd04SMikulas Patocka }
2096d5b9dd04SMikulas Patocka 
2097d5b9dd04SMikulas Patocka /*
2098042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2099042d2a9bSAlasdair G Kergon  */
2100042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2101754c5fc7SMike Snitzer 			       struct queue_limits *limits)
21021da177e4SLinus Torvalds {
2103042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
2104165125e1SJens Axboe 	struct request_queue *q = md->queue;
21051da177e4SLinus Torvalds 	sector_t size;
2106523d9297SKiyoshi Ueda 	unsigned long flags;
2107d5b9dd04SMikulas Patocka 	int merge_is_optional;
21081da177e4SLinus Torvalds 
21091da177e4SLinus Torvalds 	size = dm_table_get_size(t);
21103ac51e74SDarrick J. Wong 
21113ac51e74SDarrick J. Wong 	/*
21123ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
21133ac51e74SDarrick J. Wong 	 */
21143ac51e74SDarrick J. Wong 	if (size != get_capacity(md->disk))
21153ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
21163ac51e74SDarrick J. Wong 
21174e90188bSAlasdair G Kergon 	__set_size(md, size);
21181da177e4SLinus Torvalds 
2119cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
21202ca3310eSAlasdair G Kergon 
2121e6ee8c0bSKiyoshi Ueda 	/*
2122e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
2123e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
2124e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
2125e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
2126e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
2127e6ee8c0bSKiyoshi Ueda 	 */
2128e6ee8c0bSKiyoshi Ueda 	if (dm_table_request_based(t) && !blk_queue_stopped(q))
2129e6ee8c0bSKiyoshi Ueda 		stop_queue(q);
2130e6ee8c0bSKiyoshi Ueda 
2131e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
2132e6ee8c0bSKiyoshi Ueda 
2133d5b9dd04SMikulas Patocka 	merge_is_optional = dm_table_merge_is_optional(t);
2134d5b9dd04SMikulas Patocka 
2135523d9297SKiyoshi Ueda 	write_lock_irqsave(&md->map_lock, flags);
2136042d2a9bSAlasdair G Kergon 	old_map = md->map;
21372ca3310eSAlasdair G Kergon 	md->map = t;
213836a0456fSAlasdair G Kergon 	md->immutable_target_type = dm_table_get_immutable_target_type(t);
213936a0456fSAlasdair G Kergon 
2140754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
2141d5b9dd04SMikulas Patocka 	if (merge_is_optional)
2142d5b9dd04SMikulas Patocka 		set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2143d5b9dd04SMikulas Patocka 	else
2144d5b9dd04SMikulas Patocka 		clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
2145523d9297SKiyoshi Ueda 	write_unlock_irqrestore(&md->map_lock, flags);
21462ca3310eSAlasdair G Kergon 
2147042d2a9bSAlasdair G Kergon 	return old_map;
21481da177e4SLinus Torvalds }
21491da177e4SLinus Torvalds 
2150a7940155SAlasdair G Kergon /*
2151a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2152a7940155SAlasdair G Kergon  */
2153a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
21541da177e4SLinus Torvalds {
21551da177e4SLinus Torvalds 	struct dm_table *map = md->map;
2156523d9297SKiyoshi Ueda 	unsigned long flags;
21571da177e4SLinus Torvalds 
21581da177e4SLinus Torvalds 	if (!map)
2159a7940155SAlasdair G Kergon 		return NULL;
21601da177e4SLinus Torvalds 
21611da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
2162523d9297SKiyoshi Ueda 	write_lock_irqsave(&md->map_lock, flags);
21631da177e4SLinus Torvalds 	md->map = NULL;
2164523d9297SKiyoshi Ueda 	write_unlock_irqrestore(&md->map_lock, flags);
2165a7940155SAlasdair G Kergon 
2166a7940155SAlasdair G Kergon 	return map;
21671da177e4SLinus Torvalds }
21681da177e4SLinus Torvalds 
21691da177e4SLinus Torvalds /*
21701da177e4SLinus Torvalds  * Constructor for a new device.
21711da177e4SLinus Torvalds  */
21722b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
21731da177e4SLinus Torvalds {
21741da177e4SLinus Torvalds 	struct mapped_device *md;
21751da177e4SLinus Torvalds 
21762b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
21771da177e4SLinus Torvalds 	if (!md)
21781da177e4SLinus Torvalds 		return -ENXIO;
21791da177e4SLinus Torvalds 
2180784aae73SMilan Broz 	dm_sysfs_init(md);
2181784aae73SMilan Broz 
21821da177e4SLinus Torvalds 	*result = md;
21831da177e4SLinus Torvalds 	return 0;
21841da177e4SLinus Torvalds }
21851da177e4SLinus Torvalds 
2186a5664dadSMike Snitzer /*
2187a5664dadSMike Snitzer  * Functions to manage md->type.
2188a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2189a5664dadSMike Snitzer  */
2190a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2191a5664dadSMike Snitzer {
2192a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2193a5664dadSMike Snitzer }
2194a5664dadSMike Snitzer 
2195a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2196a5664dadSMike Snitzer {
2197a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2198a5664dadSMike Snitzer }
2199a5664dadSMike Snitzer 
2200a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type)
2201a5664dadSMike Snitzer {
2202a5664dadSMike Snitzer 	md->type = type;
2203a5664dadSMike Snitzer }
2204a5664dadSMike Snitzer 
2205a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md)
2206a5664dadSMike Snitzer {
2207a5664dadSMike Snitzer 	return md->type;
2208a5664dadSMike Snitzer }
2209a5664dadSMike Snitzer 
221036a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
221136a0456fSAlasdair G Kergon {
221236a0456fSAlasdair G Kergon 	return md->immutable_target_type;
221336a0456fSAlasdair G Kergon }
221436a0456fSAlasdair G Kergon 
22154a0b4ddfSMike Snitzer /*
22164a0b4ddfSMike Snitzer  * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
22174a0b4ddfSMike Snitzer  */
22184a0b4ddfSMike Snitzer static int dm_init_request_based_queue(struct mapped_device *md)
22194a0b4ddfSMike Snitzer {
22204a0b4ddfSMike Snitzer 	struct request_queue *q = NULL;
22214a0b4ddfSMike Snitzer 
22224a0b4ddfSMike Snitzer 	if (md->queue->elevator)
22234a0b4ddfSMike Snitzer 		return 1;
22244a0b4ddfSMike Snitzer 
22254a0b4ddfSMike Snitzer 	/* Fully initialize the queue */
22264a0b4ddfSMike Snitzer 	q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
22274a0b4ddfSMike Snitzer 	if (!q)
22284a0b4ddfSMike Snitzer 		return 0;
22294a0b4ddfSMike Snitzer 
22304a0b4ddfSMike Snitzer 	md->queue = q;
22314a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
22324a0b4ddfSMike Snitzer 	blk_queue_softirq_done(md->queue, dm_softirq_done);
22334a0b4ddfSMike Snitzer 	blk_queue_prep_rq(md->queue, dm_prep_fn);
22344a0b4ddfSMike Snitzer 	blk_queue_lld_busy(md->queue, dm_lld_busy);
22354a0b4ddfSMike Snitzer 
22364a0b4ddfSMike Snitzer 	elv_register_queue(md->queue);
22374a0b4ddfSMike Snitzer 
22384a0b4ddfSMike Snitzer 	return 1;
22394a0b4ddfSMike Snitzer }
22404a0b4ddfSMike Snitzer 
22414a0b4ddfSMike Snitzer /*
22424a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
22434a0b4ddfSMike Snitzer  */
22444a0b4ddfSMike Snitzer int dm_setup_md_queue(struct mapped_device *md)
22454a0b4ddfSMike Snitzer {
22464a0b4ddfSMike Snitzer 	if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
22474a0b4ddfSMike Snitzer 	    !dm_init_request_based_queue(md)) {
22484a0b4ddfSMike Snitzer 		DMWARN("Cannot initialize queue for request-based mapped device");
22494a0b4ddfSMike Snitzer 		return -EINVAL;
22504a0b4ddfSMike Snitzer 	}
22514a0b4ddfSMike Snitzer 
22524a0b4ddfSMike Snitzer 	return 0;
22534a0b4ddfSMike Snitzer }
22544a0b4ddfSMike Snitzer 
2255637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev)
22561da177e4SLinus Torvalds {
22571da177e4SLinus Torvalds 	struct mapped_device *md;
22581da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
22591da177e4SLinus Torvalds 
22601da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
22611da177e4SLinus Torvalds 		return NULL;
22621da177e4SLinus Torvalds 
2263f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
22641da177e4SLinus Torvalds 
22651da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
2266fba9f90eSJeff Mahoney 	if (md && (md == MINOR_ALLOCED ||
2267f331c029STejun Heo 		   (MINOR(disk_devt(dm_disk(md))) != minor) ||
2268abdc568bSKiyoshi Ueda 		   dm_deleting_md(md) ||
2269fba9f90eSJeff Mahoney 		   test_bit(DMF_FREEING, &md->flags))) {
2270637842cfSDavid Teigland 		md = NULL;
2271fba9f90eSJeff Mahoney 		goto out;
2272fba9f90eSJeff Mahoney 	}
22731da177e4SLinus Torvalds 
2274fba9f90eSJeff Mahoney out:
2275f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
22761da177e4SLinus Torvalds 
2277637842cfSDavid Teigland 	return md;
2278637842cfSDavid Teigland }
2279637842cfSDavid Teigland 
2280d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev)
2281d229a958SDavid Teigland {
2282d229a958SDavid Teigland 	struct mapped_device *md = dm_find_md(dev);
2283d229a958SDavid Teigland 
2284d229a958SDavid Teigland 	if (md)
2285d229a958SDavid Teigland 		dm_get(md);
2286d229a958SDavid Teigland 
2287d229a958SDavid Teigland 	return md;
2288d229a958SDavid Teigland }
22893cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md);
2290d229a958SDavid Teigland 
22919ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2292637842cfSDavid Teigland {
22939ade92a9SAlasdair G Kergon 	return md->interface_ptr;
22941da177e4SLinus Torvalds }
22951da177e4SLinus Torvalds 
22961da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
22971da177e4SLinus Torvalds {
22981da177e4SLinus Torvalds 	md->interface_ptr = ptr;
22991da177e4SLinus Torvalds }
23001da177e4SLinus Torvalds 
23011da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
23021da177e4SLinus Torvalds {
23031da177e4SLinus Torvalds 	atomic_inc(&md->holders);
23043f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
23051da177e4SLinus Torvalds }
23061da177e4SLinus Torvalds 
230772d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
230872d94861SAlasdair G Kergon {
230972d94861SAlasdair G Kergon 	return md->name;
231072d94861SAlasdair G Kergon }
231172d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
231272d94861SAlasdair G Kergon 
23133f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
23141da177e4SLinus Torvalds {
23151134e5aeSMike Anderson 	struct dm_table *map;
23161da177e4SLinus Torvalds 
23173f77316dSKiyoshi Ueda 	might_sleep();
2318fba9f90eSJeff Mahoney 
23193f77316dSKiyoshi Ueda 	spin_lock(&_minor_lock);
23207c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
23213f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2322fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2323f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
23243f77316dSKiyoshi Ueda 
23254f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
23261da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
23271da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
23281da177e4SLinus Torvalds 	}
23293f77316dSKiyoshi Ueda 
23303f77316dSKiyoshi Ueda 	/*
23313f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
23323f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
23333f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
23343f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
23353f77316dSKiyoshi Ueda 	 */
23363f77316dSKiyoshi Ueda 	if (wait)
23373f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
23383f77316dSKiyoshi Ueda 			msleep(1);
23393f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
23403f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
23413f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
23423f77316dSKiyoshi Ueda 
2343784aae73SMilan Broz 	dm_sysfs_exit(md);
23441134e5aeSMike Anderson 	dm_table_put(map);
2345a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
23461da177e4SLinus Torvalds 	free_dev(md);
23471da177e4SLinus Torvalds }
23483f77316dSKiyoshi Ueda 
23493f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
23503f77316dSKiyoshi Ueda {
23513f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
23523f77316dSKiyoshi Ueda }
23533f77316dSKiyoshi Ueda 
23543f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
23553f77316dSKiyoshi Ueda {
23563f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
23573f77316dSKiyoshi Ueda }
23583f77316dSKiyoshi Ueda 
23593f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
23603f77316dSKiyoshi Ueda {
23613f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
23621da177e4SLinus Torvalds }
236379eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
23641da177e4SLinus Torvalds 
2365401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
236646125c1cSMilan Broz {
236746125c1cSMilan Broz 	int r = 0;
2368b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
2369b44ebeb0SMikulas Patocka 
2370b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
237146125c1cSMilan Broz 
237246125c1cSMilan Broz 	while (1) {
2373401600dfSMikulas Patocka 		set_current_state(interruptible);
237446125c1cSMilan Broz 
2375b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
237646125c1cSMilan Broz 			break;
237746125c1cSMilan Broz 
2378401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
2379401600dfSMikulas Patocka 		    signal_pending(current)) {
238046125c1cSMilan Broz 			r = -EINTR;
238146125c1cSMilan Broz 			break;
238246125c1cSMilan Broz 		}
238346125c1cSMilan Broz 
238446125c1cSMilan Broz 		io_schedule();
238546125c1cSMilan Broz 	}
238646125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
238746125c1cSMilan Broz 
2388b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
2389b44ebeb0SMikulas Patocka 
239046125c1cSMilan Broz 	return r;
239146125c1cSMilan Broz }
239246125c1cSMilan Broz 
23931da177e4SLinus Torvalds /*
23941da177e4SLinus Torvalds  * Process the deferred bios
23951da177e4SLinus Torvalds  */
2396ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
23971da177e4SLinus Torvalds {
2398ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2399ef208587SMikulas Patocka 						work);
24006d6f10dfSMilan Broz 	struct bio *c;
24011da177e4SLinus Torvalds 
24026a8736d1STejun Heo 	down_read(&md->io_lock);
2403ef208587SMikulas Patocka 
24043b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2405022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2406022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2407022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2408022c2611SMikulas Patocka 
24096a8736d1STejun Heo 		if (!c)
2410df12ee99SAlasdair G Kergon 			break;
241173d410c0SMilan Broz 
24126a8736d1STejun Heo 		up_read(&md->io_lock);
24133b00b203SMikulas Patocka 
2414e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2415e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2416af7e466aSMikulas Patocka 		else
2417df12ee99SAlasdair G Kergon 			__split_and_process_bio(md, c);
24186a8736d1STejun Heo 
24196a8736d1STejun Heo 		down_read(&md->io_lock);
2420e6ee8c0bSKiyoshi Ueda 	}
24213b00b203SMikulas Patocka 
24226a8736d1STejun Heo 	up_read(&md->io_lock);
24231da177e4SLinus Torvalds }
24241da177e4SLinus Torvalds 
24259a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2426304f3f6aSMilan Broz {
24273b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
24283b00b203SMikulas Patocka 	smp_mb__after_clear_bit();
242953d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2430304f3f6aSMilan Broz }
2431304f3f6aSMilan Broz 
24321da177e4SLinus Torvalds /*
2433042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
24341da177e4SLinus Torvalds  */
2435042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
24361da177e4SLinus Torvalds {
243787eb5b21SMike Christie 	struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
2438754c5fc7SMike Snitzer 	struct queue_limits limits;
2439042d2a9bSAlasdair G Kergon 	int r;
24401da177e4SLinus Torvalds 
2441e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
24421da177e4SLinus Torvalds 
24431da177e4SLinus Torvalds 	/* device must be suspended */
24444f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
244593c534aeSAlasdair G Kergon 		goto out;
24461da177e4SLinus Torvalds 
24473ae70656SMike Snitzer 	/*
24483ae70656SMike Snitzer 	 * If the new table has no data devices, retain the existing limits.
24493ae70656SMike Snitzer 	 * This helps multipath with queue_if_no_path if all paths disappear,
24503ae70656SMike Snitzer 	 * then new I/O is queued based on these limits, and then some paths
24513ae70656SMike Snitzer 	 * reappear.
24523ae70656SMike Snitzer 	 */
24533ae70656SMike Snitzer 	if (dm_table_has_no_data_devices(table)) {
24543ae70656SMike Snitzer 		live_map = dm_get_live_table(md);
24553ae70656SMike Snitzer 		if (live_map)
24563ae70656SMike Snitzer 			limits = md->queue->limits;
24573ae70656SMike Snitzer 		dm_table_put(live_map);
24583ae70656SMike Snitzer 	}
24593ae70656SMike Snitzer 
246087eb5b21SMike Christie 	if (!live_map) {
2461754c5fc7SMike Snitzer 		r = dm_calculate_queue_limits(table, &limits);
2462042d2a9bSAlasdair G Kergon 		if (r) {
2463042d2a9bSAlasdair G Kergon 			map = ERR_PTR(r);
2464754c5fc7SMike Snitzer 			goto out;
2465042d2a9bSAlasdair G Kergon 		}
246687eb5b21SMike Christie 	}
2467754c5fc7SMike Snitzer 
2468042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
24691da177e4SLinus Torvalds 
247093c534aeSAlasdair G Kergon out:
2471e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2472042d2a9bSAlasdair G Kergon 	return map;
24731da177e4SLinus Torvalds }
24741da177e4SLinus Torvalds 
24751da177e4SLinus Torvalds /*
24761da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
24771da177e4SLinus Torvalds  * device.
24781da177e4SLinus Torvalds  */
24792ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
24801da177e4SLinus Torvalds {
2481e39e2e95SAlasdair G Kergon 	int r;
24821da177e4SLinus Torvalds 
24831da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2484dfbe03f6SAlasdair G Kergon 
2485db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2486dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2487cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2488e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2489e39e2e95SAlasdair G Kergon 		return r;
2490dfbe03f6SAlasdair G Kergon 	}
2491dfbe03f6SAlasdair G Kergon 
2492aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2493aa8d7c2fSAlasdair G Kergon 
24941da177e4SLinus Torvalds 	return 0;
24951da177e4SLinus Torvalds }
24961da177e4SLinus Torvalds 
24972ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
24981da177e4SLinus Torvalds {
2499aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2500aa8d7c2fSAlasdair G Kergon 		return;
2501aa8d7c2fSAlasdair G Kergon 
2502db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
25031da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2504aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
25051da177e4SLinus Torvalds }
25061da177e4SLinus Torvalds 
25071da177e4SLinus Torvalds /*
25081da177e4SLinus Torvalds  * We need to be able to change a mapping table under a mounted
25091da177e4SLinus Torvalds  * filesystem.  For example we might want to move some data in
25101da177e4SLinus Torvalds  * the background.  Before the table can be swapped with
25111da177e4SLinus Torvalds  * dm_bind_table, dm_suspend must be called to flush any in
25121da177e4SLinus Torvalds  * flight bios and ensure that any further io gets deferred.
25131da177e4SLinus Torvalds  */
2514cec47e3dSKiyoshi Ueda /*
2515cec47e3dSKiyoshi Ueda  * Suspend mechanism in request-based dm.
2516cec47e3dSKiyoshi Ueda  *
25179f518b27SKiyoshi Ueda  * 1. Flush all I/Os by lock_fs() if needed.
25189f518b27SKiyoshi Ueda  * 2. Stop dispatching any I/O by stopping the request_queue.
25199f518b27SKiyoshi Ueda  * 3. Wait for all in-flight I/Os to be completed or requeued.
2520cec47e3dSKiyoshi Ueda  *
25219f518b27SKiyoshi Ueda  * To abort suspend, start the request_queue.
2522cec47e3dSKiyoshi Ueda  */
2523a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
25241da177e4SLinus Torvalds {
25252ca3310eSAlasdair G Kergon 	struct dm_table *map = NULL;
252646125c1cSMilan Broz 	int r = 0;
2527a3d77d35SKiyoshi Ueda 	int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
25282e93ccc1SKiyoshi Ueda 	int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
25291da177e4SLinus Torvalds 
2530e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
25312ca3310eSAlasdair G Kergon 
25324f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
253373d410c0SMilan Broz 		r = -EINVAL;
2534d287483dSAlasdair G Kergon 		goto out_unlock;
253573d410c0SMilan Broz 	}
25361da177e4SLinus Torvalds 
25377c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
2538cf222b37SAlasdair G Kergon 
25392e93ccc1SKiyoshi Ueda 	/*
25402e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
25412e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
25422e93ccc1SKiyoshi Ueda 	 */
25432e93ccc1SKiyoshi Ueda 	if (noflush)
25442e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
25452e93ccc1SKiyoshi Ueda 
2546436d4108SAlasdair G Kergon 	/* This does not get reverted if there's an error later. */
25471da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
25481da177e4SLinus Torvalds 
25492e93ccc1SKiyoshi Ueda 	/*
25509f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
25519f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
25529f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
25539f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
25542e93ccc1SKiyoshi Ueda 	 */
255532a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
25562ca3310eSAlasdair G Kergon 		r = lock_fs(md);
25572ca3310eSAlasdair G Kergon 		if (r)
25582ca3310eSAlasdair G Kergon 			goto out;
2559aa8d7c2fSAlasdair G Kergon 	}
25601da177e4SLinus Torvalds 
25611da177e4SLinus Torvalds 	/*
25623b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
25633b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
25643b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
25653b00b203SMikulas Patocka 	 * dm_wq_work.
25663b00b203SMikulas Patocka 	 *
25673b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
25683b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
25696a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
25706a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
25716a8736d1STejun Heo 	 * flush_workqueue(md->wq).
25721da177e4SLinus Torvalds 	 */
25732ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
25741eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
25752ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
25761da177e4SLinus Torvalds 
2577d0bcb878SKiyoshi Ueda 	/*
257829e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
257929e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2580d0bcb878SKiyoshi Ueda 	 */
2581cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
25829f518b27SKiyoshi Ueda 		stop_queue(md->queue);
2583cec47e3dSKiyoshi Ueda 
2584d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2585d0bcb878SKiyoshi Ueda 
25861da177e4SLinus Torvalds 	/*
25873b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
25883b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
25893b00b203SMikulas Patocka 	 * to finish.
25901da177e4SLinus Torvalds 	 */
2591401600dfSMikulas Patocka 	r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
25921da177e4SLinus Torvalds 
25932ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
25946d6f10dfSMilan Broz 	if (noflush)
2595022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
259694d6351eSMilan Broz 	up_write(&md->io_lock);
25972e93ccc1SKiyoshi Ueda 
25981da177e4SLinus Torvalds 	/* were we interrupted ? */
259946125c1cSMilan Broz 	if (r < 0) {
26009a1fb464SMikulas Patocka 		dm_queue_flush(md);
260173d410c0SMilan Broz 
2602cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
26039f518b27SKiyoshi Ueda 			start_queue(md->queue);
2604cec47e3dSKiyoshi Ueda 
26052ca3310eSAlasdair G Kergon 		unlock_fs(md);
26062e93ccc1SKiyoshi Ueda 		goto out; /* pushback list is already flushed, so skip flush */
26072ca3310eSAlasdair G Kergon 	}
26082ca3310eSAlasdair G Kergon 
26093b00b203SMikulas Patocka 	/*
26103b00b203SMikulas Patocka 	 * If dm_wait_for_completion returned 0, the device is completely
26113b00b203SMikulas Patocka 	 * quiescent now. There is no request-processing activity. All new
26123b00b203SMikulas Patocka 	 * requests are being added to md->deferred list.
26133b00b203SMikulas Patocka 	 */
26143b00b203SMikulas Patocka 
26151da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
26161da177e4SLinus Torvalds 
26174d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
26184d4471cbSKiyoshi Ueda 
26192ca3310eSAlasdair G Kergon out:
26201da177e4SLinus Torvalds 	dm_table_put(map);
2621d287483dSAlasdair G Kergon 
2622d287483dSAlasdair G Kergon out_unlock:
2623e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2624cf222b37SAlasdair G Kergon 	return r;
26251da177e4SLinus Torvalds }
26261da177e4SLinus Torvalds 
26271da177e4SLinus Torvalds int dm_resume(struct mapped_device *md)
26281da177e4SLinus Torvalds {
2629cf222b37SAlasdair G Kergon 	int r = -EINVAL;
2630cf222b37SAlasdair G Kergon 	struct dm_table *map = NULL;
26311da177e4SLinus Torvalds 
2632e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
26334f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
2634cf222b37SAlasdair G Kergon 		goto out;
2635cf222b37SAlasdair G Kergon 
26367c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
26372ca3310eSAlasdair G Kergon 	if (!map || !dm_table_get_size(map))
2638cf222b37SAlasdair G Kergon 		goto out;
26391da177e4SLinus Torvalds 
26408757b776SMilan Broz 	r = dm_table_resume_targets(map);
26418757b776SMilan Broz 	if (r)
26428757b776SMilan Broz 		goto out;
26432ca3310eSAlasdair G Kergon 
26449a1fb464SMikulas Patocka 	dm_queue_flush(md);
26452ca3310eSAlasdair G Kergon 
2646cec47e3dSKiyoshi Ueda 	/*
2647cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2648cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2649cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2650cec47e3dSKiyoshi Ueda 	 */
2651cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2652cec47e3dSKiyoshi Ueda 		start_queue(md->queue);
2653cec47e3dSKiyoshi Ueda 
26542ca3310eSAlasdair G Kergon 	unlock_fs(md);
26552ca3310eSAlasdair G Kergon 
26562ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
26572ca3310eSAlasdair G Kergon 
2658cf222b37SAlasdair G Kergon 	r = 0;
2659cf222b37SAlasdair G Kergon out:
2660cf222b37SAlasdair G Kergon 	dm_table_put(map);
2661e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
26622ca3310eSAlasdair G Kergon 
2663cf222b37SAlasdair G Kergon 	return r;
26641da177e4SLinus Torvalds }
26651da177e4SLinus Torvalds 
26661da177e4SLinus Torvalds /*-----------------------------------------------------------------
26671da177e4SLinus Torvalds  * Event notification.
26681da177e4SLinus Torvalds  *---------------------------------------------------------------*/
26693abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
267060935eb2SMilan Broz 		       unsigned cookie)
267169267a30SAlasdair G Kergon {
267260935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
267360935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
267460935eb2SMilan Broz 
267560935eb2SMilan Broz 	if (!cookie)
26763abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
267760935eb2SMilan Broz 	else {
267860935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
267960935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
26803abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
26813abf85b5SPeter Rajnoha 					  action, envp);
268260935eb2SMilan Broz 	}
268369267a30SAlasdair G Kergon }
268469267a30SAlasdair G Kergon 
26857a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
26867a8c3d3bSMike Anderson {
26877a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
26887a8c3d3bSMike Anderson }
26897a8c3d3bSMike Anderson 
26901da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
26911da177e4SLinus Torvalds {
26921da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
26931da177e4SLinus Torvalds }
26941da177e4SLinus Torvalds 
26951da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
26961da177e4SLinus Torvalds {
26971da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
26981da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
26991da177e4SLinus Torvalds }
27001da177e4SLinus Torvalds 
27017a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
27027a8c3d3bSMike Anderson {
27037a8c3d3bSMike Anderson 	unsigned long flags;
27047a8c3d3bSMike Anderson 
27057a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
27067a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
27077a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
27087a8c3d3bSMike Anderson }
27097a8c3d3bSMike Anderson 
27101da177e4SLinus Torvalds /*
27111da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
27121da177e4SLinus Torvalds  * count on 'md'.
27131da177e4SLinus Torvalds  */
27141da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
27151da177e4SLinus Torvalds {
27161da177e4SLinus Torvalds 	return md->disk;
27171da177e4SLinus Torvalds }
27181da177e4SLinus Torvalds 
2719784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2720784aae73SMilan Broz {
2721784aae73SMilan Broz 	return &md->kobj;
2722784aae73SMilan Broz }
2723784aae73SMilan Broz 
2724784aae73SMilan Broz /*
2725784aae73SMilan Broz  * struct mapped_device should not be exported outside of dm.c
2726784aae73SMilan Broz  * so use this check to verify that kobj is part of md structure
2727784aae73SMilan Broz  */
2728784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2729784aae73SMilan Broz {
2730784aae73SMilan Broz 	struct mapped_device *md;
2731784aae73SMilan Broz 
2732784aae73SMilan Broz 	md = container_of(kobj, struct mapped_device, kobj);
2733784aae73SMilan Broz 	if (&md->kobj != kobj)
2734784aae73SMilan Broz 		return NULL;
2735784aae73SMilan Broz 
27364d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
2737432a212cSMike Anderson 	    dm_deleting_md(md))
27384d89b7b4SMilan Broz 		return NULL;
27394d89b7b4SMilan Broz 
2740784aae73SMilan Broz 	dm_get(md);
2741784aae73SMilan Broz 	return md;
2742784aae73SMilan Broz }
2743784aae73SMilan Broz 
27444f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
27451da177e4SLinus Torvalds {
27461da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
27471da177e4SLinus Torvalds }
27481da177e4SLinus Torvalds 
274964dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
275064dbce58SKiyoshi Ueda {
2751ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
275264dbce58SKiyoshi Ueda }
275364dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
275464dbce58SKiyoshi Ueda 
27552e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
27562e93ccc1SKiyoshi Ueda {
2757ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
27582e93ccc1SKiyoshi Ueda }
27592e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
27602e93ccc1SKiyoshi Ueda 
2761c0820cf5SMikulas Patocka struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity, unsigned per_bio_data_size)
2762e6ee8c0bSKiyoshi Ueda {
27635f015204SJun'ichi Nomura 	struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
27645f015204SJun'ichi Nomura 	struct kmem_cache *cachep;
27655f015204SJun'ichi Nomura 	unsigned int pool_size;
27665f015204SJun'ichi Nomura 	unsigned int front_pad;
2767e6ee8c0bSKiyoshi Ueda 
2768e6ee8c0bSKiyoshi Ueda 	if (!pools)
2769e6ee8c0bSKiyoshi Ueda 		return NULL;
2770e6ee8c0bSKiyoshi Ueda 
277123e5083bSJun'ichi Nomura 	if (type == DM_TYPE_BIO_BASED) {
27725f015204SJun'ichi Nomura 		cachep = _io_cache;
27735f015204SJun'ichi Nomura 		pool_size = 16;
27745f015204SJun'ichi Nomura 		front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
27755f015204SJun'ichi Nomura 	} else if (type == DM_TYPE_REQUEST_BASED) {
27765f015204SJun'ichi Nomura 		cachep = _rq_tio_cache;
27775f015204SJun'ichi Nomura 		pool_size = MIN_IOS;
27785f015204SJun'ichi Nomura 		front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
27795f015204SJun'ichi Nomura 		/* per_bio_data_size is not used. See __bind_mempools(). */
27805f015204SJun'ichi Nomura 		WARN_ON(per_bio_data_size != 0);
27815f015204SJun'ichi Nomura 	} else
27825f015204SJun'ichi Nomura 		goto out;
27835f015204SJun'ichi Nomura 
27845f015204SJun'ichi Nomura 	pools->io_pool = mempool_create_slab_pool(MIN_IOS, cachep);
2785e6ee8c0bSKiyoshi Ueda 	if (!pools->io_pool)
27865f015204SJun'ichi Nomura 		goto out;
2787e6ee8c0bSKiyoshi Ueda 
27885f015204SJun'ichi Nomura 	pools->bs = bioset_create(pool_size, front_pad);
2789e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
27905f015204SJun'ichi Nomura 		goto out;
2791e6ee8c0bSKiyoshi Ueda 
2792a91a2785SMartin K. Petersen 	if (integrity && bioset_integrity_create(pools->bs, pool_size))
27935f015204SJun'ichi Nomura 		goto out;
2794a91a2785SMartin K. Petersen 
2795e6ee8c0bSKiyoshi Ueda 	return pools;
2796e6ee8c0bSKiyoshi Ueda 
27975f015204SJun'ichi Nomura out:
27985f015204SJun'ichi Nomura 	dm_free_md_mempools(pools);
2799e6ee8c0bSKiyoshi Ueda 
2800e6ee8c0bSKiyoshi Ueda 	return NULL;
2801e6ee8c0bSKiyoshi Ueda }
2802e6ee8c0bSKiyoshi Ueda 
2803e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
2804e6ee8c0bSKiyoshi Ueda {
2805e6ee8c0bSKiyoshi Ueda 	if (!pools)
2806e6ee8c0bSKiyoshi Ueda 		return;
2807e6ee8c0bSKiyoshi Ueda 
2808e6ee8c0bSKiyoshi Ueda 	if (pools->io_pool)
2809e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->io_pool);
2810e6ee8c0bSKiyoshi Ueda 
2811e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
2812e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
2813e6ee8c0bSKiyoshi Ueda 
2814e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2815e6ee8c0bSKiyoshi Ueda }
2816e6ee8c0bSKiyoshi Ueda 
281783d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
28181da177e4SLinus Torvalds 	.open = dm_blk_open,
28191da177e4SLinus Torvalds 	.release = dm_blk_close,
2820aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
28213ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
28221da177e4SLinus Torvalds 	.owner = THIS_MODULE
28231da177e4SLinus Torvalds };
28241da177e4SLinus Torvalds 
28251da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo);
28261da177e4SLinus Torvalds 
28271da177e4SLinus Torvalds /*
28281da177e4SLinus Torvalds  * module hooks
28291da177e4SLinus Torvalds  */
28301da177e4SLinus Torvalds module_init(dm_init);
28311da177e4SLinus Torvalds module_exit(dm_exit);
28321da177e4SLinus Torvalds 
28331da177e4SLinus Torvalds module_param(major, uint, 0);
28341da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
28351da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
28361da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
28371da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2838