xref: /openbmc/linux/drivers/md/dm.c (revision 6facdaff229f2b25d0de82be9be99b9f562e72ba)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3784aae73SMilan Broz  * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This file is released under the GPL.
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include "dm.h"
951e5b2bdSMike Anderson #include "dm-uevent.h"
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/init.h>
121da177e4SLinus Torvalds #include <linux/module.h>
1348c9c27bSArjan van de Ven #include <linux/mutex.h>
141da177e4SLinus Torvalds #include <linux/moduleparam.h>
151da177e4SLinus Torvalds #include <linux/blkpg.h>
161da177e4SLinus Torvalds #include <linux/bio.h>
171da177e4SLinus Torvalds #include <linux/buffer_head.h>
181da177e4SLinus Torvalds #include <linux/mempool.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/idr.h>
213ac51e74SDarrick J. Wong #include <linux/hdreg.h>
2255782138SLi Zefan 
2355782138SLi Zefan #include <trace/events/block.h>
241da177e4SLinus Torvalds 
2572d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
2672d94861SAlasdair G Kergon 
2760935eb2SMilan Broz /*
2860935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
2960935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
3060935eb2SMilan Broz  */
3160935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
3260935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
3360935eb2SMilan Broz 
341da177e4SLinus Torvalds static const char *_name = DM_NAME;
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds static unsigned int major = 0;
371da177e4SLinus Torvalds static unsigned int _major = 0;
381da177e4SLinus Torvalds 
39f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
401da177e4SLinus Torvalds /*
418fbf26adSKiyoshi Ueda  * For bio-based dm.
421da177e4SLinus Torvalds  * One of these is allocated per bio.
431da177e4SLinus Torvalds  */
441da177e4SLinus Torvalds struct dm_io {
451da177e4SLinus Torvalds 	struct mapped_device *md;
461da177e4SLinus Torvalds 	int error;
471da177e4SLinus Torvalds 	atomic_t io_count;
486ae2fa67SRichard Kennedy 	struct bio *bio;
493eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
50f88fb981SKiyoshi Ueda 	spinlock_t endio_lock;
511da177e4SLinus Torvalds };
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds /*
548fbf26adSKiyoshi Ueda  * For bio-based dm.
551da177e4SLinus Torvalds  * One of these is allocated per target within a bio.  Hopefully
561da177e4SLinus Torvalds  * this will be simplified out one day.
571da177e4SLinus Torvalds  */
58028867acSAlasdair G Kergon struct dm_target_io {
591da177e4SLinus Torvalds 	struct dm_io *io;
601da177e4SLinus Torvalds 	struct dm_target *ti;
611da177e4SLinus Torvalds 	union map_info info;
621da177e4SLinus Torvalds };
631da177e4SLinus Torvalds 
648fbf26adSKiyoshi Ueda /*
658fbf26adSKiyoshi Ueda  * For request-based dm.
668fbf26adSKiyoshi Ueda  * One of these is allocated per request.
678fbf26adSKiyoshi Ueda  */
688fbf26adSKiyoshi Ueda struct dm_rq_target_io {
698fbf26adSKiyoshi Ueda 	struct mapped_device *md;
708fbf26adSKiyoshi Ueda 	struct dm_target *ti;
718fbf26adSKiyoshi Ueda 	struct request *orig, clone;
728fbf26adSKiyoshi Ueda 	int error;
738fbf26adSKiyoshi Ueda 	union map_info info;
748fbf26adSKiyoshi Ueda };
758fbf26adSKiyoshi Ueda 
768fbf26adSKiyoshi Ueda /*
778fbf26adSKiyoshi Ueda  * For request-based dm.
788fbf26adSKiyoshi Ueda  * One of these is allocated per bio.
798fbf26adSKiyoshi Ueda  */
808fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info {
818fbf26adSKiyoshi Ueda 	struct bio *orig;
82cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio;
838fbf26adSKiyoshi Ueda };
848fbf26adSKiyoshi Ueda 
851da177e4SLinus Torvalds union map_info *dm_get_mapinfo(struct bio *bio)
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds 	if (bio && bio->bi_private)
88028867acSAlasdair G Kergon 		return &((struct dm_target_io *)bio->bi_private)->info;
891da177e4SLinus Torvalds 	return NULL;
901da177e4SLinus Torvalds }
911da177e4SLinus Torvalds 
92cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq)
93cec47e3dSKiyoshi Ueda {
94cec47e3dSKiyoshi Ueda 	if (rq && rq->end_io_data)
95cec47e3dSKiyoshi Ueda 		return &((struct dm_rq_target_io *)rq->end_io_data)->info;
96cec47e3dSKiyoshi Ueda 	return NULL;
97cec47e3dSKiyoshi Ueda }
98cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
99cec47e3dSKiyoshi Ueda 
100ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
101ba61fdd1SJeff Mahoney 
1021da177e4SLinus Torvalds /*
1031da177e4SLinus Torvalds  * Bits for the md->flags field.
1041da177e4SLinus Torvalds  */
1051eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
1061da177e4SLinus Torvalds #define DMF_SUSPENDED 1
107aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
108fba9f90eSJeff Mahoney #define DMF_FREEING 3
1095c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
1102e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
1111eb787ecSAlasdair G Kergon #define DMF_QUEUE_IO_TO_THREAD 6
1121da177e4SLinus Torvalds 
113304f3f6aSMilan Broz /*
114304f3f6aSMilan Broz  * Work processed by per-device workqueue.
115304f3f6aSMilan Broz  */
1161da177e4SLinus Torvalds struct mapped_device {
1172ca3310eSAlasdair G Kergon 	struct rw_semaphore io_lock;
118e61290a4SDaniel Walker 	struct mutex suspend_lock;
1191da177e4SLinus Torvalds 	rwlock_t map_lock;
1201da177e4SLinus Torvalds 	atomic_t holders;
1215c6bd75dSAlasdair G Kergon 	atomic_t open_count;
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	unsigned long flags;
1241da177e4SLinus Torvalds 
125165125e1SJens Axboe 	struct request_queue *queue;
1261da177e4SLinus Torvalds 	struct gendisk *disk;
1277e51f257SMike Anderson 	char name[16];
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	void *interface_ptr;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	/*
1321da177e4SLinus Torvalds 	 * A list of ios that arrived while we were suspended.
1331da177e4SLinus Torvalds 	 */
134316d315bSNikanth Karthikesan 	atomic_t pending[2];
1351da177e4SLinus Torvalds 	wait_queue_head_t wait;
13653d5914fSMikulas Patocka 	struct work_struct work;
1371da177e4SLinus Torvalds 	struct bio_list deferred;
138022c2611SMikulas Patocka 	spinlock_t deferred_lock;
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 	/*
141af7e466aSMikulas Patocka 	 * An error from the barrier request currently being processed.
142af7e466aSMikulas Patocka 	 */
143af7e466aSMikulas Patocka 	int barrier_error;
144af7e466aSMikulas Patocka 
145af7e466aSMikulas Patocka 	/*
146304f3f6aSMilan Broz 	 * Processing queue (flush/barriers)
147304f3f6aSMilan Broz 	 */
148304f3f6aSMilan Broz 	struct workqueue_struct *wq;
149304f3f6aSMilan Broz 
150304f3f6aSMilan Broz 	/*
1511da177e4SLinus Torvalds 	 * The current mapping.
1521da177e4SLinus Torvalds 	 */
1531da177e4SLinus Torvalds 	struct dm_table *map;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	/*
1561da177e4SLinus Torvalds 	 * io objects are allocated from here.
1571da177e4SLinus Torvalds 	 */
1581da177e4SLinus Torvalds 	mempool_t *io_pool;
1591da177e4SLinus Torvalds 	mempool_t *tio_pool;
1601da177e4SLinus Torvalds 
1619faf400fSStefan Bader 	struct bio_set *bs;
1629faf400fSStefan Bader 
1631da177e4SLinus Torvalds 	/*
1641da177e4SLinus Torvalds 	 * Event handling.
1651da177e4SLinus Torvalds 	 */
1661da177e4SLinus Torvalds 	atomic_t event_nr;
1671da177e4SLinus Torvalds 	wait_queue_head_t eventq;
1687a8c3d3bSMike Anderson 	atomic_t uevent_seq;
1697a8c3d3bSMike Anderson 	struct list_head uevent_list;
1707a8c3d3bSMike Anderson 	spinlock_t uevent_lock; /* Protect access to uevent_list */
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	/*
1731da177e4SLinus Torvalds 	 * freeze/thaw support require holding onto a super block
1741da177e4SLinus Torvalds 	 */
1751da177e4SLinus Torvalds 	struct super_block *frozen_sb;
176db8fef4fSMikulas Patocka 	struct block_device *bdev;
1773ac51e74SDarrick J. Wong 
1783ac51e74SDarrick J. Wong 	/* forced geometry settings */
1793ac51e74SDarrick J. Wong 	struct hd_geometry geometry;
180784aae73SMilan Broz 
181cec47e3dSKiyoshi Ueda 	/* marker of flush suspend for request-based dm */
182cec47e3dSKiyoshi Ueda 	struct request suspend_rq;
183cec47e3dSKiyoshi Ueda 
184cec47e3dSKiyoshi Ueda 	/* For saving the address of __make_request for request based dm */
185cec47e3dSKiyoshi Ueda 	make_request_fn *saved_make_request_fn;
186cec47e3dSKiyoshi Ueda 
187784aae73SMilan Broz 	/* sysfs handle */
188784aae73SMilan Broz 	struct kobject kobj;
18952b1fd5aSMikulas Patocka 
19052b1fd5aSMikulas Patocka 	/* zero-length barrier that will be cloned and submitted to targets */
19152b1fd5aSMikulas Patocka 	struct bio barrier_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 	mempool_t *tio_pool;
200e6ee8c0bSKiyoshi Ueda 	struct bio_set *bs;
201e6ee8c0bSKiyoshi Ueda };
202e6ee8c0bSKiyoshi Ueda 
2031da177e4SLinus Torvalds #define MIN_IOS 256
204e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
205e18b890bSChristoph Lameter static struct kmem_cache *_tio_cache;
2068fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
2078fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_bio_info_cache;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds static int __init local_init(void)
2101da177e4SLinus Torvalds {
21151157b4aSKiyoshi Ueda 	int r = -ENOMEM;
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
214028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
2151da177e4SLinus Torvalds 	if (!_io_cache)
21651157b4aSKiyoshi Ueda 		return r;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	/* allocate a slab for the target ios */
219028867acSAlasdair G Kergon 	_tio_cache = KMEM_CACHE(dm_target_io, 0);
22051157b4aSKiyoshi Ueda 	if (!_tio_cache)
22151157b4aSKiyoshi Ueda 		goto out_free_io_cache;
2221da177e4SLinus Torvalds 
2238fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
2248fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
2258fbf26adSKiyoshi Ueda 		goto out_free_tio_cache;
2268fbf26adSKiyoshi Ueda 
2278fbf26adSKiyoshi Ueda 	_rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
2288fbf26adSKiyoshi Ueda 	if (!_rq_bio_info_cache)
2298fbf26adSKiyoshi Ueda 		goto out_free_rq_tio_cache;
2308fbf26adSKiyoshi Ueda 
23151e5b2bdSMike Anderson 	r = dm_uevent_init();
23251157b4aSKiyoshi Ueda 	if (r)
2338fbf26adSKiyoshi Ueda 		goto out_free_rq_bio_info_cache;
23451e5b2bdSMike Anderson 
2351da177e4SLinus Torvalds 	_major = major;
2361da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
23751157b4aSKiyoshi Ueda 	if (r < 0)
23851157b4aSKiyoshi Ueda 		goto out_uevent_exit;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	if (!_major)
2411da177e4SLinus Torvalds 		_major = r;
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	return 0;
24451157b4aSKiyoshi Ueda 
24551157b4aSKiyoshi Ueda out_uevent_exit:
24651157b4aSKiyoshi Ueda 	dm_uevent_exit();
2478fbf26adSKiyoshi Ueda out_free_rq_bio_info_cache:
2488fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_bio_info_cache);
2498fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
2508fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
25151157b4aSKiyoshi Ueda out_free_tio_cache:
25251157b4aSKiyoshi Ueda 	kmem_cache_destroy(_tio_cache);
25351157b4aSKiyoshi Ueda out_free_io_cache:
25451157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
25551157b4aSKiyoshi Ueda 
25651157b4aSKiyoshi Ueda 	return r;
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds static void local_exit(void)
2601da177e4SLinus Torvalds {
2618fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_bio_info_cache);
2628fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
2631da177e4SLinus Torvalds 	kmem_cache_destroy(_tio_cache);
2641da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
26500d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
26651e5b2bdSMike Anderson 	dm_uevent_exit();
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	_major = 0;
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	DMINFO("cleaned up");
2711da177e4SLinus Torvalds }
2721da177e4SLinus Torvalds 
273b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
2741da177e4SLinus Torvalds 	local_init,
2751da177e4SLinus Torvalds 	dm_target_init,
2761da177e4SLinus Torvalds 	dm_linear_init,
2771da177e4SLinus Torvalds 	dm_stripe_init,
278952b3557SMikulas Patocka 	dm_io_init,
279945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2801da177e4SLinus Torvalds 	dm_interface_init,
2811da177e4SLinus Torvalds };
2821da177e4SLinus Torvalds 
283b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2841da177e4SLinus Torvalds 	local_exit,
2851da177e4SLinus Torvalds 	dm_target_exit,
2861da177e4SLinus Torvalds 	dm_linear_exit,
2871da177e4SLinus Torvalds 	dm_stripe_exit,
288952b3557SMikulas Patocka 	dm_io_exit,
289945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
2901da177e4SLinus Torvalds 	dm_interface_exit,
2911da177e4SLinus Torvalds };
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds static int __init dm_init(void)
2941da177e4SLinus Torvalds {
2951da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	int r, i;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
3001da177e4SLinus Torvalds 		r = _inits[i]();
3011da177e4SLinus Torvalds 		if (r)
3021da177e4SLinus Torvalds 			goto bad;
3031da177e4SLinus Torvalds 	}
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	return 0;
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds       bad:
3081da177e4SLinus Torvalds 	while (i--)
3091da177e4SLinus Torvalds 		_exits[i]();
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	return r;
3121da177e4SLinus Torvalds }
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds static void __exit dm_exit(void)
3151da177e4SLinus Torvalds {
3161da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	while (i--)
3191da177e4SLinus Torvalds 		_exits[i]();
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds /*
3231da177e4SLinus Torvalds  * Block device functions
3241da177e4SLinus Torvalds  */
325fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds 	struct mapped_device *md;
3281da177e4SLinus Torvalds 
329fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
330fba9f90eSJeff Mahoney 
331fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
332fba9f90eSJeff Mahoney 	if (!md)
333fba9f90eSJeff Mahoney 		goto out;
334fba9f90eSJeff Mahoney 
3355c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
3365c6bd75dSAlasdair G Kergon 	    test_bit(DMF_DELETING, &md->flags)) {
337fba9f90eSJeff Mahoney 		md = NULL;
338fba9f90eSJeff Mahoney 		goto out;
339fba9f90eSJeff Mahoney 	}
340fba9f90eSJeff Mahoney 
3411da177e4SLinus Torvalds 	dm_get(md);
3425c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
343fba9f90eSJeff Mahoney 
344fba9f90eSJeff Mahoney out:
345fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
346fba9f90eSJeff Mahoney 
347fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds 
350fe5f9f2cSAl Viro static int dm_blk_close(struct gendisk *disk, fmode_t mode)
3511da177e4SLinus Torvalds {
352fe5f9f2cSAl Viro 	struct mapped_device *md = disk->private_data;
3535c6bd75dSAlasdair G Kergon 	atomic_dec(&md->open_count);
3541da177e4SLinus Torvalds 	dm_put(md);
3551da177e4SLinus Torvalds 	return 0;
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds 
3585c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3595c6bd75dSAlasdair G Kergon {
3605c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3615c6bd75dSAlasdair G Kergon }
3625c6bd75dSAlasdair G Kergon 
3635c6bd75dSAlasdair G Kergon /*
3645c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3655c6bd75dSAlasdair G Kergon  */
3665c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md)
3675c6bd75dSAlasdair G Kergon {
3685c6bd75dSAlasdair G Kergon 	int r = 0;
3695c6bd75dSAlasdair G Kergon 
3705c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
3715c6bd75dSAlasdair G Kergon 
3725c6bd75dSAlasdair G Kergon 	if (dm_open_count(md))
3735c6bd75dSAlasdair G Kergon 		r = -EBUSY;
3745c6bd75dSAlasdair G Kergon 	else
3755c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
3765c6bd75dSAlasdair G Kergon 
3775c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
3785c6bd75dSAlasdair G Kergon 
3795c6bd75dSAlasdair G Kergon 	return r;
3805c6bd75dSAlasdair G Kergon }
3815c6bd75dSAlasdair G Kergon 
3823ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3833ac51e74SDarrick J. Wong {
3843ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
3853ac51e74SDarrick J. Wong 
3863ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
3873ac51e74SDarrick J. Wong }
3883ac51e74SDarrick J. Wong 
389fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
390aa129a22SMilan Broz 			unsigned int cmd, unsigned long arg)
391aa129a22SMilan Broz {
392fe5f9f2cSAl Viro 	struct mapped_device *md = bdev->bd_disk->private_data;
393fe5f9f2cSAl Viro 	struct dm_table *map = dm_get_table(md);
394aa129a22SMilan Broz 	struct dm_target *tgt;
395aa129a22SMilan Broz 	int r = -ENOTTY;
396aa129a22SMilan Broz 
397aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
398aa129a22SMilan Broz 		goto out;
399aa129a22SMilan Broz 
400aa129a22SMilan Broz 	/* We only support devices that have a single target */
401aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
402aa129a22SMilan Broz 		goto out;
403aa129a22SMilan Broz 
404aa129a22SMilan Broz 	tgt = dm_table_get_target(map, 0);
405aa129a22SMilan Broz 
406aa129a22SMilan Broz 	if (dm_suspended(md)) {
407aa129a22SMilan Broz 		r = -EAGAIN;
408aa129a22SMilan Broz 		goto out;
409aa129a22SMilan Broz 	}
410aa129a22SMilan Broz 
411aa129a22SMilan Broz 	if (tgt->type->ioctl)
412647b3d00SAl Viro 		r = tgt->type->ioctl(tgt, cmd, arg);
413aa129a22SMilan Broz 
414aa129a22SMilan Broz out:
415aa129a22SMilan Broz 	dm_table_put(map);
416aa129a22SMilan Broz 
417aa129a22SMilan Broz 	return r;
418aa129a22SMilan Broz }
419aa129a22SMilan Broz 
420028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
4211da177e4SLinus Torvalds {
4221da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
4231da177e4SLinus Torvalds }
4241da177e4SLinus Torvalds 
425028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
4261da177e4SLinus Torvalds {
4271da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
430028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
4311da177e4SLinus Torvalds {
4321da177e4SLinus Torvalds 	mempool_free(tio, md->tio_pool);
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
43508885643SKiyoshi Ueda static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
43608885643SKiyoshi Ueda 					    gfp_t gfp_mask)
437cec47e3dSKiyoshi Ueda {
43808885643SKiyoshi Ueda 	return mempool_alloc(md->tio_pool, gfp_mask);
439cec47e3dSKiyoshi Ueda }
440cec47e3dSKiyoshi Ueda 
441cec47e3dSKiyoshi Ueda static void free_rq_tio(struct dm_rq_target_io *tio)
442cec47e3dSKiyoshi Ueda {
443cec47e3dSKiyoshi Ueda 	mempool_free(tio, tio->md->tio_pool);
444cec47e3dSKiyoshi Ueda }
445cec47e3dSKiyoshi Ueda 
446cec47e3dSKiyoshi Ueda static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
447cec47e3dSKiyoshi Ueda {
448cec47e3dSKiyoshi Ueda 	return mempool_alloc(md->io_pool, GFP_ATOMIC);
449cec47e3dSKiyoshi Ueda }
450cec47e3dSKiyoshi Ueda 
451cec47e3dSKiyoshi Ueda static void free_bio_info(struct dm_rq_clone_bio_info *info)
452cec47e3dSKiyoshi Ueda {
453cec47e3dSKiyoshi Ueda 	mempool_free(info, info->tio->md->io_pool);
454cec47e3dSKiyoshi Ueda }
455cec47e3dSKiyoshi Ueda 
45690abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md)
45790abb8c4SKiyoshi Ueda {
45890abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
45990abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
46090abb8c4SKiyoshi Ueda }
46190abb8c4SKiyoshi Ueda 
4623eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
4633eaf840eSJun'ichi "Nick" Nomura {
4643eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
465c9959059STejun Heo 	int cpu;
466316d315bSNikanth Karthikesan 	int rw = bio_data_dir(io->bio);
4673eaf840eSJun'ichi "Nick" Nomura 
4683eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
4693eaf840eSJun'ichi "Nick" Nomura 
470074a7acaSTejun Heo 	cpu = part_stat_lock();
471074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
472074a7acaSTejun Heo 	part_stat_unlock();
473316d315bSNikanth Karthikesan 	dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
4743eaf840eSJun'ichi "Nick" Nomura }
4753eaf840eSJun'ichi "Nick" Nomura 
476d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
4773eaf840eSJun'ichi "Nick" Nomura {
4783eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
4793eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
4803eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
481c9959059STejun Heo 	int pending, cpu;
4823eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
4833eaf840eSJun'ichi "Nick" Nomura 
484074a7acaSTejun Heo 	cpu = part_stat_lock();
485074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
486074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
487074a7acaSTejun Heo 	part_stat_unlock();
4883eaf840eSJun'ichi "Nick" Nomura 
489af7e466aSMikulas Patocka 	/*
490af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
491af7e466aSMikulas Patocka 	 * a barrier.
492af7e466aSMikulas Patocka 	 */
493316d315bSNikanth Karthikesan 	dm_disk(md)->part0.in_flight[rw] = pending =
494316d315bSNikanth Karthikesan 		atomic_dec_return(&md->pending[rw]);
495316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
4963eaf840eSJun'ichi "Nick" Nomura 
497d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
498d221d2e7SMikulas Patocka 	if (!pending)
499d221d2e7SMikulas Patocka 		wake_up(&md->wait);
5003eaf840eSJun'ichi "Nick" Nomura }
5013eaf840eSJun'ichi "Nick" Nomura 
5021da177e4SLinus Torvalds /*
5031da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
5041da177e4SLinus Torvalds  */
50592c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
5061da177e4SLinus Torvalds {
5072ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
5081da177e4SLinus Torvalds 
509022c2611SMikulas Patocka 	spin_lock_irq(&md->deferred_lock);
5101da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
511022c2611SMikulas Patocka 	spin_unlock_irq(&md->deferred_lock);
5121da177e4SLinus Torvalds 
51392c63902SMikulas Patocka 	if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
51492c63902SMikulas Patocka 		queue_work(md->wq, &md->work);
51592c63902SMikulas Patocka 
5162ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
5171da177e4SLinus Torvalds }
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds /*
5201da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
5211da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
5221da177e4SLinus Torvalds  * dm_table_put() when finished.
5231da177e4SLinus Torvalds  */
5241da177e4SLinus Torvalds struct dm_table *dm_get_table(struct mapped_device *md)
5251da177e4SLinus Torvalds {
5261da177e4SLinus Torvalds 	struct dm_table *t;
527523d9297SKiyoshi Ueda 	unsigned long flags;
5281da177e4SLinus Torvalds 
529523d9297SKiyoshi Ueda 	read_lock_irqsave(&md->map_lock, flags);
5301da177e4SLinus Torvalds 	t = md->map;
5311da177e4SLinus Torvalds 	if (t)
5321da177e4SLinus Torvalds 		dm_table_get(t);
533523d9297SKiyoshi Ueda 	read_unlock_irqrestore(&md->map_lock, flags);
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 	return t;
5361da177e4SLinus Torvalds }
5371da177e4SLinus Torvalds 
5383ac51e74SDarrick J. Wong /*
5393ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
5403ac51e74SDarrick J. Wong  */
5413ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
5423ac51e74SDarrick J. Wong {
5433ac51e74SDarrick J. Wong 	*geo = md->geometry;
5443ac51e74SDarrick J. Wong 
5453ac51e74SDarrick J. Wong 	return 0;
5463ac51e74SDarrick J. Wong }
5473ac51e74SDarrick J. Wong 
5483ac51e74SDarrick J. Wong /*
5493ac51e74SDarrick J. Wong  * Set the geometry of a device.
5503ac51e74SDarrick J. Wong  */
5513ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
5523ac51e74SDarrick J. Wong {
5533ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
5543ac51e74SDarrick J. Wong 
5553ac51e74SDarrick J. Wong 	if (geo->start > sz) {
5563ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
5573ac51e74SDarrick J. Wong 		return -EINVAL;
5583ac51e74SDarrick J. Wong 	}
5593ac51e74SDarrick J. Wong 
5603ac51e74SDarrick J. Wong 	md->geometry = *geo;
5613ac51e74SDarrick J. Wong 
5623ac51e74SDarrick J. Wong 	return 0;
5633ac51e74SDarrick J. Wong }
5643ac51e74SDarrick J. Wong 
5651da177e4SLinus Torvalds /*-----------------------------------------------------------------
5661da177e4SLinus Torvalds  * CRUD START:
5671da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
5681da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
5691da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
5701da177e4SLinus Torvalds  *   interests of getting something for people to use I give
5711da177e4SLinus Torvalds  *   you this clearly demarcated crap.
5721da177e4SLinus Torvalds  *---------------------------------------------------------------*/
5731da177e4SLinus Torvalds 
5742e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
5752e93ccc1SKiyoshi Ueda {
5762e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
5772e93ccc1SKiyoshi Ueda }
5782e93ccc1SKiyoshi Ueda 
5791da177e4SLinus Torvalds /*
5801da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
5811da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
5821da177e4SLinus Torvalds  */
583858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
5841da177e4SLinus Torvalds {
5852e93ccc1SKiyoshi Ueda 	unsigned long flags;
586b35f8caaSMilan Broz 	int io_error;
587b35f8caaSMilan Broz 	struct bio *bio;
588b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
5892e93ccc1SKiyoshi Ueda 
5902e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
591f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
592f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
593f88fb981SKiyoshi Ueda 		if (!(io->error > 0 && __noflush_suspending(md)))
5941da177e4SLinus Torvalds 			io->error = error;
595f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
596f88fb981SKiyoshi Ueda 	}
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
5992e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
6002e93ccc1SKiyoshi Ueda 			/*
6012e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
6022e93ccc1SKiyoshi Ueda 			 */
603022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
6042761e95fSMikulas Patocka 			if (__noflush_suspending(md)) {
6051f98a13fSJens Axboe 				if (!bio_rw_flagged(io->bio, BIO_RW_BARRIER))
6062761e95fSMikulas Patocka 					bio_list_add_head(&md->deferred,
6072761e95fSMikulas Patocka 							  io->bio);
6082761e95fSMikulas Patocka 			} else
6092e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
6102e93ccc1SKiyoshi Ueda 				io->error = -EIO;
611022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
6122e93ccc1SKiyoshi Ueda 		}
6132e93ccc1SKiyoshi Ueda 
614b35f8caaSMilan Broz 		io_error = io->error;
615b35f8caaSMilan Broz 		bio = io->bio;
6162056a782SJens Axboe 
6171f98a13fSJens Axboe 		if (bio_rw_flagged(bio, BIO_RW_BARRIER)) {
618af7e466aSMikulas Patocka 			/*
619af7e466aSMikulas Patocka 			 * There can be just one barrier request so we use
620af7e466aSMikulas Patocka 			 * a per-device variable for error reporting.
621af7e466aSMikulas Patocka 			 * Note that you can't touch the bio after end_io_acct
622af7e466aSMikulas Patocka 			 */
623fdb9572bSMikulas Patocka 			if (!md->barrier_error && io_error != -EOPNOTSUPP)
624af7e466aSMikulas Patocka 				md->barrier_error = io_error;
625af7e466aSMikulas Patocka 			end_io_acct(io);
626af7e466aSMikulas Patocka 		} else {
627af7e466aSMikulas Patocka 			end_io_acct(io);
628b35f8caaSMilan Broz 
629b35f8caaSMilan Broz 			if (io_error != DM_ENDIO_REQUEUE) {
630b35f8caaSMilan Broz 				trace_block_bio_complete(md->queue, bio);
631b35f8caaSMilan Broz 
632b35f8caaSMilan Broz 				bio_endio(bio, io_error);
6332e93ccc1SKiyoshi Ueda 			}
6341da177e4SLinus Torvalds 		}
635af7e466aSMikulas Patocka 
636af7e466aSMikulas Patocka 		free_io(md, io);
637af7e466aSMikulas Patocka 	}
6381da177e4SLinus Torvalds }
6391da177e4SLinus Torvalds 
6406712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error)
6411da177e4SLinus Torvalds {
6421da177e4SLinus Torvalds 	int r = 0;
643028867acSAlasdair G Kergon 	struct dm_target_io *tio = bio->bi_private;
644b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
6459faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
6461da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	if (!bio_flagged(bio, BIO_UPTODATE) && !error)
6491da177e4SLinus Torvalds 		error = -EIO;
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	if (endio) {
6521da177e4SLinus Torvalds 		r = endio(tio->ti, bio, error, &tio->info);
6532e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
6542e93ccc1SKiyoshi Ueda 			/*
6552e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
6562e93ccc1SKiyoshi Ueda 			 * in dec_pending().
6572e93ccc1SKiyoshi Ueda 			 */
6581da177e4SLinus Torvalds 			error = r;
65945cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
66045cbcd79SKiyoshi Ueda 			/* The target will handle the io */
6616712ecf8SNeilBrown 			return;
66245cbcd79SKiyoshi Ueda 		else if (r) {
66345cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
66445cbcd79SKiyoshi Ueda 			BUG();
66545cbcd79SKiyoshi Ueda 		}
6661da177e4SLinus Torvalds 	}
6671da177e4SLinus Torvalds 
6689faf400fSStefan Bader 	/*
6699faf400fSStefan Bader 	 * Store md for cleanup instead of tio which is about to get freed.
6709faf400fSStefan Bader 	 */
6719faf400fSStefan Bader 	bio->bi_private = md->bs;
6729faf400fSStefan Bader 
6739faf400fSStefan Bader 	free_tio(md, tio);
674b35f8caaSMilan Broz 	bio_put(bio);
675b35f8caaSMilan Broz 	dec_pending(io, error);
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
678cec47e3dSKiyoshi Ueda /*
679cec47e3dSKiyoshi Ueda  * Partial completion handling for request-based dm
680cec47e3dSKiyoshi Ueda  */
681cec47e3dSKiyoshi Ueda static void end_clone_bio(struct bio *clone, int error)
682cec47e3dSKiyoshi Ueda {
683cec47e3dSKiyoshi Ueda 	struct dm_rq_clone_bio_info *info = clone->bi_private;
684cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = info->tio;
685cec47e3dSKiyoshi Ueda 	struct bio *bio = info->orig;
686cec47e3dSKiyoshi Ueda 	unsigned int nr_bytes = info->orig->bi_size;
687cec47e3dSKiyoshi Ueda 
688cec47e3dSKiyoshi Ueda 	bio_put(clone);
689cec47e3dSKiyoshi Ueda 
690cec47e3dSKiyoshi Ueda 	if (tio->error)
691cec47e3dSKiyoshi Ueda 		/*
692cec47e3dSKiyoshi Ueda 		 * An error has already been detected on the request.
693cec47e3dSKiyoshi Ueda 		 * Once error occurred, just let clone->end_io() handle
694cec47e3dSKiyoshi Ueda 		 * the remainder.
695cec47e3dSKiyoshi Ueda 		 */
696cec47e3dSKiyoshi Ueda 		return;
697cec47e3dSKiyoshi Ueda 	else if (error) {
698cec47e3dSKiyoshi Ueda 		/*
699cec47e3dSKiyoshi Ueda 		 * Don't notice the error to the upper layer yet.
700cec47e3dSKiyoshi Ueda 		 * The error handling decision is made by the target driver,
701cec47e3dSKiyoshi Ueda 		 * when the request is completed.
702cec47e3dSKiyoshi Ueda 		 */
703cec47e3dSKiyoshi Ueda 		tio->error = error;
704cec47e3dSKiyoshi Ueda 		return;
705cec47e3dSKiyoshi Ueda 	}
706cec47e3dSKiyoshi Ueda 
707cec47e3dSKiyoshi Ueda 	/*
708cec47e3dSKiyoshi Ueda 	 * I/O for the bio successfully completed.
709cec47e3dSKiyoshi Ueda 	 * Notice the data completion to the upper layer.
710cec47e3dSKiyoshi Ueda 	 */
711cec47e3dSKiyoshi Ueda 
712cec47e3dSKiyoshi Ueda 	/*
713cec47e3dSKiyoshi Ueda 	 * bios are processed from the head of the list.
714cec47e3dSKiyoshi Ueda 	 * So the completing bio should always be rq->bio.
715cec47e3dSKiyoshi Ueda 	 * If it's not, something wrong is happening.
716cec47e3dSKiyoshi Ueda 	 */
717cec47e3dSKiyoshi Ueda 	if (tio->orig->bio != bio)
718cec47e3dSKiyoshi Ueda 		DMERR("bio completion is going in the middle of the request");
719cec47e3dSKiyoshi Ueda 
720cec47e3dSKiyoshi Ueda 	/*
721cec47e3dSKiyoshi Ueda 	 * Update the original request.
722cec47e3dSKiyoshi Ueda 	 * Do not use blk_end_request() here, because it may complete
723cec47e3dSKiyoshi Ueda 	 * the original request before the clone, and break the ordering.
724cec47e3dSKiyoshi Ueda 	 */
725cec47e3dSKiyoshi Ueda 	blk_update_request(tio->orig, 0, nr_bytes);
726cec47e3dSKiyoshi Ueda }
727cec47e3dSKiyoshi Ueda 
728cec47e3dSKiyoshi Ueda /*
729cec47e3dSKiyoshi Ueda  * Don't touch any member of the md after calling this function because
730cec47e3dSKiyoshi Ueda  * the md may be freed in dm_put() at the end of this function.
731cec47e3dSKiyoshi Ueda  * Or do dm_get() before calling this function and dm_put() later.
732cec47e3dSKiyoshi Ueda  */
733cec47e3dSKiyoshi Ueda static void rq_completed(struct mapped_device *md, int run_queue)
734cec47e3dSKiyoshi Ueda {
735cec47e3dSKiyoshi Ueda 	int wakeup_waiters = 0;
736cec47e3dSKiyoshi Ueda 	struct request_queue *q = md->queue;
737cec47e3dSKiyoshi Ueda 	unsigned long flags;
738cec47e3dSKiyoshi Ueda 
739cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
740cec47e3dSKiyoshi Ueda 	if (!queue_in_flight(q))
741cec47e3dSKiyoshi Ueda 		wakeup_waiters = 1;
742cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
743cec47e3dSKiyoshi Ueda 
744cec47e3dSKiyoshi Ueda 	/* nudge anyone waiting on suspend queue */
745cec47e3dSKiyoshi Ueda 	if (wakeup_waiters)
746cec47e3dSKiyoshi Ueda 		wake_up(&md->wait);
747cec47e3dSKiyoshi Ueda 
748cec47e3dSKiyoshi Ueda 	if (run_queue)
749cec47e3dSKiyoshi Ueda 		blk_run_queue(q);
750cec47e3dSKiyoshi Ueda 
751cec47e3dSKiyoshi Ueda 	/*
752cec47e3dSKiyoshi Ueda 	 * dm_put() must be at the end of this function. See the comment above
753cec47e3dSKiyoshi Ueda 	 */
754cec47e3dSKiyoshi Ueda 	dm_put(md);
755cec47e3dSKiyoshi Ueda }
756cec47e3dSKiyoshi Ueda 
757a77e28c7SKiyoshi Ueda static void free_rq_clone(struct request *clone)
758a77e28c7SKiyoshi Ueda {
759a77e28c7SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
760a77e28c7SKiyoshi Ueda 
761a77e28c7SKiyoshi Ueda 	blk_rq_unprep_clone(clone);
762a77e28c7SKiyoshi Ueda 	free_rq_tio(tio);
763a77e28c7SKiyoshi Ueda }
764a77e28c7SKiyoshi Ueda 
765cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq)
766cec47e3dSKiyoshi Ueda {
767cec47e3dSKiyoshi Ueda 	struct request *clone = rq->special;
768cec47e3dSKiyoshi Ueda 
769cec47e3dSKiyoshi Ueda 	rq->special = NULL;
770cec47e3dSKiyoshi Ueda 	rq->cmd_flags &= ~REQ_DONTPREP;
771cec47e3dSKiyoshi Ueda 
772a77e28c7SKiyoshi Ueda 	free_rq_clone(clone);
773cec47e3dSKiyoshi Ueda }
774cec47e3dSKiyoshi Ueda 
775cec47e3dSKiyoshi Ueda /*
776cec47e3dSKiyoshi Ueda  * Requeue the original request of a clone.
777cec47e3dSKiyoshi Ueda  */
778cec47e3dSKiyoshi Ueda void dm_requeue_unmapped_request(struct request *clone)
779cec47e3dSKiyoshi Ueda {
780cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
781cec47e3dSKiyoshi Ueda 	struct mapped_device *md = tio->md;
782cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
783cec47e3dSKiyoshi Ueda 	struct request_queue *q = rq->q;
784cec47e3dSKiyoshi Ueda 	unsigned long flags;
785cec47e3dSKiyoshi Ueda 
786cec47e3dSKiyoshi Ueda 	dm_unprep_request(rq);
787cec47e3dSKiyoshi Ueda 
788cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
789cec47e3dSKiyoshi Ueda 	if (elv_queue_empty(q))
790cec47e3dSKiyoshi Ueda 		blk_plug_device(q);
791cec47e3dSKiyoshi Ueda 	blk_requeue_request(q, rq);
792cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
793cec47e3dSKiyoshi Ueda 
794cec47e3dSKiyoshi Ueda 	rq_completed(md, 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 
827cec47e3dSKiyoshi Ueda /*
828cec47e3dSKiyoshi Ueda  * Complete the clone and the original request.
829cec47e3dSKiyoshi Ueda  * Must be called without queue lock.
830cec47e3dSKiyoshi Ueda  */
831cec47e3dSKiyoshi Ueda static void dm_end_request(struct request *clone, int error)
832cec47e3dSKiyoshi Ueda {
833cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
834cec47e3dSKiyoshi Ueda 	struct mapped_device *md = tio->md;
835cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
836cec47e3dSKiyoshi Ueda 
837cec47e3dSKiyoshi Ueda 	if (blk_pc_request(rq)) {
838cec47e3dSKiyoshi Ueda 		rq->errors = clone->errors;
839cec47e3dSKiyoshi Ueda 		rq->resid_len = clone->resid_len;
840cec47e3dSKiyoshi Ueda 
841cec47e3dSKiyoshi Ueda 		if (rq->sense)
842cec47e3dSKiyoshi Ueda 			/*
843cec47e3dSKiyoshi Ueda 			 * We are using the sense buffer of the original
844cec47e3dSKiyoshi Ueda 			 * request.
845cec47e3dSKiyoshi Ueda 			 * So setting the length of the sense data is enough.
846cec47e3dSKiyoshi Ueda 			 */
847cec47e3dSKiyoshi Ueda 			rq->sense_len = clone->sense_len;
848cec47e3dSKiyoshi Ueda 	}
849cec47e3dSKiyoshi Ueda 
850a77e28c7SKiyoshi Ueda 	free_rq_clone(clone);
851cec47e3dSKiyoshi Ueda 
852cec47e3dSKiyoshi Ueda 	blk_end_request_all(rq, error);
853cec47e3dSKiyoshi Ueda 
854cec47e3dSKiyoshi Ueda 	rq_completed(md, 1);
855cec47e3dSKiyoshi Ueda }
856cec47e3dSKiyoshi Ueda 
857cec47e3dSKiyoshi Ueda /*
858cec47e3dSKiyoshi Ueda  * Request completion handler for request-based dm
859cec47e3dSKiyoshi Ueda  */
860cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq)
861cec47e3dSKiyoshi Ueda {
862cec47e3dSKiyoshi Ueda 	struct request *clone = rq->completion_data;
863cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
864cec47e3dSKiyoshi Ueda 	dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
865cec47e3dSKiyoshi Ueda 	int error = tio->error;
866cec47e3dSKiyoshi Ueda 
867cec47e3dSKiyoshi Ueda 	if (!(rq->cmd_flags & REQ_FAILED) && rq_end_io)
868cec47e3dSKiyoshi Ueda 		error = rq_end_io(tio->ti, clone, error, &tio->info);
869cec47e3dSKiyoshi Ueda 
870cec47e3dSKiyoshi Ueda 	if (error <= 0)
871cec47e3dSKiyoshi Ueda 		/* The target wants to complete the I/O */
872cec47e3dSKiyoshi Ueda 		dm_end_request(clone, error);
873cec47e3dSKiyoshi Ueda 	else if (error == DM_ENDIO_INCOMPLETE)
874cec47e3dSKiyoshi Ueda 		/* The target will handle the I/O */
875cec47e3dSKiyoshi Ueda 		return;
876cec47e3dSKiyoshi Ueda 	else if (error == DM_ENDIO_REQUEUE)
877cec47e3dSKiyoshi Ueda 		/* The target wants to requeue the I/O */
878cec47e3dSKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
879cec47e3dSKiyoshi Ueda 	else {
880cec47e3dSKiyoshi Ueda 		DMWARN("unimplemented target endio return value: %d", error);
881cec47e3dSKiyoshi Ueda 		BUG();
882cec47e3dSKiyoshi Ueda 	}
883cec47e3dSKiyoshi Ueda }
884cec47e3dSKiyoshi Ueda 
885cec47e3dSKiyoshi Ueda /*
886cec47e3dSKiyoshi Ueda  * Complete the clone and the original request with the error status
887cec47e3dSKiyoshi Ueda  * through softirq context.
888cec47e3dSKiyoshi Ueda  */
889cec47e3dSKiyoshi Ueda static void dm_complete_request(struct request *clone, int error)
890cec47e3dSKiyoshi Ueda {
891cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
892cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
893cec47e3dSKiyoshi Ueda 
894cec47e3dSKiyoshi Ueda 	tio->error = error;
895cec47e3dSKiyoshi Ueda 	rq->completion_data = clone;
896cec47e3dSKiyoshi Ueda 	blk_complete_request(rq);
897cec47e3dSKiyoshi Ueda }
898cec47e3dSKiyoshi Ueda 
899cec47e3dSKiyoshi Ueda /*
900cec47e3dSKiyoshi Ueda  * Complete the not-mapped clone and the original request with the error status
901cec47e3dSKiyoshi Ueda  * through softirq context.
902cec47e3dSKiyoshi Ueda  * Target's rq_end_io() function isn't called.
903cec47e3dSKiyoshi Ueda  * This may be used when the target's map_rq() function fails.
904cec47e3dSKiyoshi Ueda  */
905cec47e3dSKiyoshi Ueda void dm_kill_unmapped_request(struct request *clone, int error)
906cec47e3dSKiyoshi Ueda {
907cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
908cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
909cec47e3dSKiyoshi Ueda 
910cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_FAILED;
911cec47e3dSKiyoshi Ueda 	dm_complete_request(clone, error);
912cec47e3dSKiyoshi Ueda }
913cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
914cec47e3dSKiyoshi Ueda 
915cec47e3dSKiyoshi Ueda /*
916cec47e3dSKiyoshi Ueda  * Called with the queue lock held
917cec47e3dSKiyoshi Ueda  */
918cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error)
919cec47e3dSKiyoshi Ueda {
920cec47e3dSKiyoshi Ueda 	/*
921cec47e3dSKiyoshi Ueda 	 * For just cleaning up the information of the queue in which
922cec47e3dSKiyoshi Ueda 	 * the clone was dispatched.
923cec47e3dSKiyoshi Ueda 	 * The clone is *NOT* freed actually here because it is alloced from
924cec47e3dSKiyoshi Ueda 	 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
925cec47e3dSKiyoshi Ueda 	 */
926cec47e3dSKiyoshi Ueda 	__blk_put_request(clone->q, clone);
927cec47e3dSKiyoshi Ueda 
928cec47e3dSKiyoshi Ueda 	/*
929cec47e3dSKiyoshi Ueda 	 * Actual request completion is done in a softirq context which doesn't
930cec47e3dSKiyoshi Ueda 	 * hold the queue lock.  Otherwise, deadlock could occur because:
931cec47e3dSKiyoshi Ueda 	 *     - another request may be submitted by the upper level driver
932cec47e3dSKiyoshi Ueda 	 *       of the stacking during the completion
933cec47e3dSKiyoshi Ueda 	 *     - the submission which requires queue lock may be done
934cec47e3dSKiyoshi Ueda 	 *       against this queue
935cec47e3dSKiyoshi Ueda 	 */
936cec47e3dSKiyoshi Ueda 	dm_complete_request(clone, error);
937cec47e3dSKiyoshi Ueda }
938cec47e3dSKiyoshi Ueda 
9391da177e4SLinus Torvalds static sector_t max_io_len(struct mapped_device *md,
9401da177e4SLinus Torvalds 			   sector_t sector, struct dm_target *ti)
9411da177e4SLinus Torvalds {
9421da177e4SLinus Torvalds 	sector_t offset = sector - ti->begin;
9431da177e4SLinus Torvalds 	sector_t len = ti->len - offset;
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	/*
9461da177e4SLinus Torvalds 	 * Does the target need to split even further ?
9471da177e4SLinus Torvalds 	 */
9481da177e4SLinus Torvalds 	if (ti->split_io) {
9491da177e4SLinus Torvalds 		sector_t boundary;
9501da177e4SLinus Torvalds 		boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
9511da177e4SLinus Torvalds 			   - offset;
9521da177e4SLinus Torvalds 		if (len > boundary)
9531da177e4SLinus Torvalds 			len = boundary;
9541da177e4SLinus Torvalds 	}
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	return len;
9571da177e4SLinus Torvalds }
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds static void __map_bio(struct dm_target *ti, struct bio *clone,
960028867acSAlasdair G Kergon 		      struct dm_target_io *tio)
9611da177e4SLinus Torvalds {
9621da177e4SLinus Torvalds 	int r;
9632056a782SJens Axboe 	sector_t sector;
9649faf400fSStefan Bader 	struct mapped_device *md;
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
9671da177e4SLinus Torvalds 	clone->bi_private = tio;
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	/*
9701da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
9711da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
9721da177e4SLinus Torvalds 	 * this io.
9731da177e4SLinus Torvalds 	 */
9741da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
9752056a782SJens Axboe 	sector = clone->bi_sector;
9761da177e4SLinus Torvalds 	r = ti->type->map(ti, clone, &tio->info);
97745cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
9781da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
9792056a782SJens Axboe 
9805f3ea37cSArnaldo Carvalho de Melo 		trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
98122a7c31aSAlan D. Brunelle 				    tio->io->bio->bi_bdev->bd_dev, sector);
9822056a782SJens Axboe 
9831da177e4SLinus Torvalds 		generic_make_request(clone);
9842e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
9852e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
9869faf400fSStefan Bader 		md = tio->io->md;
9879faf400fSStefan Bader 		dec_pending(tio->io, r);
9889faf400fSStefan Bader 		/*
9899faf400fSStefan Bader 		 * Store bio_set for cleanup.
9909faf400fSStefan Bader 		 */
9919faf400fSStefan Bader 		clone->bi_private = md->bs;
9921da177e4SLinus Torvalds 		bio_put(clone);
9939faf400fSStefan Bader 		free_tio(md, tio);
99445cbcd79SKiyoshi Ueda 	} else if (r) {
99545cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
99645cbcd79SKiyoshi Ueda 		BUG();
9971da177e4SLinus Torvalds 	}
9981da177e4SLinus Torvalds }
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds struct clone_info {
10011da177e4SLinus Torvalds 	struct mapped_device *md;
10021da177e4SLinus Torvalds 	struct dm_table *map;
10031da177e4SLinus Torvalds 	struct bio *bio;
10041da177e4SLinus Torvalds 	struct dm_io *io;
10051da177e4SLinus Torvalds 	sector_t sector;
10061da177e4SLinus Torvalds 	sector_t sector_count;
10071da177e4SLinus Torvalds 	unsigned short idx;
10081da177e4SLinus Torvalds };
10091da177e4SLinus Torvalds 
10103676347aSPeter Osterlund static void dm_bio_destructor(struct bio *bio)
10113676347aSPeter Osterlund {
10129faf400fSStefan Bader 	struct bio_set *bs = bio->bi_private;
10139faf400fSStefan Bader 
10149faf400fSStefan Bader 	bio_free(bio, bs);
10153676347aSPeter Osterlund }
10163676347aSPeter Osterlund 
10171da177e4SLinus Torvalds /*
10181da177e4SLinus Torvalds  * Creates a little bio that is just does part of a bvec.
10191da177e4SLinus Torvalds  */
10201da177e4SLinus Torvalds static struct bio *split_bvec(struct bio *bio, sector_t sector,
10211da177e4SLinus Torvalds 			      unsigned short idx, unsigned int offset,
10229faf400fSStefan Bader 			      unsigned int len, struct bio_set *bs)
10231da177e4SLinus Torvalds {
10241da177e4SLinus Torvalds 	struct bio *clone;
10251da177e4SLinus Torvalds 	struct bio_vec *bv = bio->bi_io_vec + idx;
10261da177e4SLinus Torvalds 
10279faf400fSStefan Bader 	clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
10283676347aSPeter Osterlund 	clone->bi_destructor = dm_bio_destructor;
10291da177e4SLinus Torvalds 	*clone->bi_io_vec = *bv;
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 	clone->bi_sector = sector;
10321da177e4SLinus Torvalds 	clone->bi_bdev = bio->bi_bdev;
1033af7e466aSMikulas Patocka 	clone->bi_rw = bio->bi_rw & ~(1 << BIO_RW_BARRIER);
10341da177e4SLinus Torvalds 	clone->bi_vcnt = 1;
10351da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
10361da177e4SLinus Torvalds 	clone->bi_io_vec->bv_offset = offset;
10371da177e4SLinus Torvalds 	clone->bi_io_vec->bv_len = clone->bi_size;
1038f3e1d26eSMartin K. Petersen 	clone->bi_flags |= 1 << BIO_CLONED;
10391da177e4SLinus Torvalds 
10409c47008dSMartin K. Petersen 	if (bio_integrity(bio)) {
10417878cba9SMartin K. Petersen 		bio_integrity_clone(clone, bio, GFP_NOIO, bs);
10429c47008dSMartin K. Petersen 		bio_integrity_trim(clone,
10439c47008dSMartin K. Petersen 				   bio_sector_offset(bio, idx, offset), len);
10449c47008dSMartin K. Petersen 	}
10459c47008dSMartin K. Petersen 
10461da177e4SLinus Torvalds 	return clone;
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds /*
10501da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
10511da177e4SLinus Torvalds  */
10521da177e4SLinus Torvalds static struct bio *clone_bio(struct bio *bio, sector_t sector,
10531da177e4SLinus Torvalds 			     unsigned short idx, unsigned short bv_count,
10549faf400fSStefan Bader 			     unsigned int len, struct bio_set *bs)
10551da177e4SLinus Torvalds {
10561da177e4SLinus Torvalds 	struct bio *clone;
10571da177e4SLinus Torvalds 
10589faf400fSStefan Bader 	clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
10599faf400fSStefan Bader 	__bio_clone(clone, bio);
1060af7e466aSMikulas Patocka 	clone->bi_rw &= ~(1 << BIO_RW_BARRIER);
10619faf400fSStefan Bader 	clone->bi_destructor = dm_bio_destructor;
10621da177e4SLinus Torvalds 	clone->bi_sector = sector;
10631da177e4SLinus Torvalds 	clone->bi_idx = idx;
10641da177e4SLinus Torvalds 	clone->bi_vcnt = idx + bv_count;
10651da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
10661da177e4SLinus Torvalds 	clone->bi_flags &= ~(1 << BIO_SEG_VALID);
10671da177e4SLinus Torvalds 
10689c47008dSMartin K. Petersen 	if (bio_integrity(bio)) {
10697878cba9SMartin K. Petersen 		bio_integrity_clone(clone, bio, GFP_NOIO, bs);
10709c47008dSMartin K. Petersen 
10719c47008dSMartin K. Petersen 		if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
10729c47008dSMartin K. Petersen 			bio_integrity_trim(clone,
10739c47008dSMartin K. Petersen 					   bio_sector_offset(bio, idx, 0), len);
10749c47008dSMartin K. Petersen 	}
10759c47008dSMartin K. Petersen 
10761da177e4SLinus Torvalds 	return clone;
10771da177e4SLinus Torvalds }
10781da177e4SLinus Torvalds 
10799015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci,
10809015df24SAlasdair G Kergon 				      struct dm_target *ti)
1081f9ab94ceSMikulas Patocka {
10829015df24SAlasdair G Kergon 	struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
1083f9ab94ceSMikulas Patocka 
1084f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1085f9ab94ceSMikulas Patocka 	tio->ti = ti;
1086f9ab94ceSMikulas Patocka 	memset(&tio->info, 0, sizeof(tio->info));
10879015df24SAlasdair G Kergon 
10889015df24SAlasdair G Kergon 	return tio;
10899015df24SAlasdair G Kergon }
10909015df24SAlasdair G Kergon 
10919015df24SAlasdair G Kergon static void __flush_target(struct clone_info *ci, struct dm_target *ti,
10929015df24SAlasdair G Kergon 			  unsigned flush_nr)
10939015df24SAlasdair G Kergon {
10949015df24SAlasdair G Kergon 	struct dm_target_io *tio = alloc_tio(ci, ti);
10959015df24SAlasdair G Kergon 	struct bio *clone;
10969015df24SAlasdair G Kergon 
1097f9ab94ceSMikulas Patocka 	tio->info.flush_request = flush_nr;
1098f9ab94ceSMikulas Patocka 
1099f9ab94ceSMikulas Patocka 	clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1100f9ab94ceSMikulas Patocka 	__bio_clone(clone, ci->bio);
1101f9ab94ceSMikulas Patocka 	clone->bi_destructor = dm_bio_destructor;
1102f9ab94ceSMikulas Patocka 
1103f9ab94ceSMikulas Patocka 	__map_bio(ti, clone, tio);
1104f9ab94ceSMikulas Patocka }
1105f9ab94ceSMikulas Patocka 
1106f9ab94ceSMikulas Patocka static int __clone_and_map_empty_barrier(struct clone_info *ci)
1107f9ab94ceSMikulas Patocka {
1108f9ab94ceSMikulas Patocka 	unsigned target_nr = 0, flush_nr;
1109f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1110f9ab94ceSMikulas Patocka 
1111f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
1112f9ab94ceSMikulas Patocka 		for (flush_nr = 0; flush_nr < ti->num_flush_requests;
1113f9ab94ceSMikulas Patocka 		     flush_nr++)
1114f9ab94ceSMikulas Patocka 			__flush_target(ci, ti, flush_nr);
1115f9ab94ceSMikulas Patocka 
1116f9ab94ceSMikulas Patocka 	ci->sector_count = 0;
1117f9ab94ceSMikulas Patocka 
1118f9ab94ceSMikulas Patocka 	return 0;
1119f9ab94ceSMikulas Patocka }
1120f9ab94ceSMikulas Patocka 
1121512875bdSJun'ichi Nomura static int __clone_and_map(struct clone_info *ci)
11221da177e4SLinus Torvalds {
11231da177e4SLinus Torvalds 	struct bio *clone, *bio = ci->bio;
1124512875bdSJun'ichi Nomura 	struct dm_target *ti;
1125512875bdSJun'ichi Nomura 	sector_t len = 0, max;
1126028867acSAlasdair G Kergon 	struct dm_target_io *tio;
11271da177e4SLinus Torvalds 
1128f9ab94ceSMikulas Patocka 	if (unlikely(bio_empty_barrier(bio)))
1129f9ab94ceSMikulas Patocka 		return __clone_and_map_empty_barrier(ci);
1130f9ab94ceSMikulas Patocka 
1131512875bdSJun'ichi Nomura 	ti = dm_table_find_target(ci->map, ci->sector);
1132512875bdSJun'ichi Nomura 	if (!dm_target_is_valid(ti))
1133512875bdSJun'ichi Nomura 		return -EIO;
1134512875bdSJun'ichi Nomura 
1135512875bdSJun'ichi Nomura 	max = max_io_len(ci->md, ci->sector, ti);
1136512875bdSJun'ichi Nomura 
11371da177e4SLinus Torvalds 	/*
11381da177e4SLinus Torvalds 	 * Allocate a target io object.
11391da177e4SLinus Torvalds 	 */
11409015df24SAlasdair G Kergon 	tio = alloc_tio(ci, ti);
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	if (ci->sector_count <= max) {
11431da177e4SLinus Torvalds 		/*
11441da177e4SLinus Torvalds 		 * Optimise for the simple case where we can do all of
11451da177e4SLinus Torvalds 		 * the remaining io with a single clone.
11461da177e4SLinus Torvalds 		 */
11471da177e4SLinus Torvalds 		clone = clone_bio(bio, ci->sector, ci->idx,
11489faf400fSStefan Bader 				  bio->bi_vcnt - ci->idx, ci->sector_count,
11499faf400fSStefan Bader 				  ci->md->bs);
11501da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
11511da177e4SLinus Torvalds 		ci->sector_count = 0;
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds 	} else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
11541da177e4SLinus Torvalds 		/*
11551da177e4SLinus Torvalds 		 * There are some bvecs that don't span targets.
11561da177e4SLinus Torvalds 		 * Do as many of these as possible.
11571da177e4SLinus Torvalds 		 */
11581da177e4SLinus Torvalds 		int i;
11591da177e4SLinus Torvalds 		sector_t remaining = max;
11601da177e4SLinus Torvalds 		sector_t bv_len;
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 		for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
11631da177e4SLinus Torvalds 			bv_len = to_sector(bio->bi_io_vec[i].bv_len);
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 			if (bv_len > remaining)
11661da177e4SLinus Torvalds 				break;
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 			remaining -= bv_len;
11691da177e4SLinus Torvalds 			len += bv_len;
11701da177e4SLinus Torvalds 		}
11711da177e4SLinus Torvalds 
11729faf400fSStefan Bader 		clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
11739faf400fSStefan Bader 				  ci->md->bs);
11741da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 		ci->sector += len;
11771da177e4SLinus Torvalds 		ci->sector_count -= len;
11781da177e4SLinus Torvalds 		ci->idx = i;
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 	} else {
11811da177e4SLinus Torvalds 		/*
1182d2044a94SAlasdair G Kergon 		 * Handle a bvec that must be split between two or more targets.
11831da177e4SLinus Torvalds 		 */
11841da177e4SLinus Torvalds 		struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1185d2044a94SAlasdair G Kergon 		sector_t remaining = to_sector(bv->bv_len);
1186d2044a94SAlasdair G Kergon 		unsigned int offset = 0;
11871da177e4SLinus Torvalds 
1188d2044a94SAlasdair G Kergon 		do {
1189d2044a94SAlasdair G Kergon 			if (offset) {
11901da177e4SLinus Torvalds 				ti = dm_table_find_target(ci->map, ci->sector);
1191512875bdSJun'ichi Nomura 				if (!dm_target_is_valid(ti))
1192512875bdSJun'ichi Nomura 					return -EIO;
1193512875bdSJun'ichi Nomura 
1194d2044a94SAlasdair G Kergon 				max = max_io_len(ci->md, ci->sector, ti);
11951da177e4SLinus Torvalds 
11969015df24SAlasdair G Kergon 				tio = alloc_tio(ci, ti);
1197d2044a94SAlasdair G Kergon 			}
1198d2044a94SAlasdair G Kergon 
1199d2044a94SAlasdair G Kergon 			len = min(remaining, max);
1200d2044a94SAlasdair G Kergon 
1201d2044a94SAlasdair G Kergon 			clone = split_bvec(bio, ci->sector, ci->idx,
12029faf400fSStefan Bader 					   bv->bv_offset + offset, len,
12039faf400fSStefan Bader 					   ci->md->bs);
1204d2044a94SAlasdair G Kergon 
12051da177e4SLinus Torvalds 			__map_bio(ti, clone, tio);
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 			ci->sector += len;
12081da177e4SLinus Torvalds 			ci->sector_count -= len;
1209d2044a94SAlasdair G Kergon 			offset += to_bytes(len);
1210d2044a94SAlasdair G Kergon 		} while (remaining -= len);
1211d2044a94SAlasdair G Kergon 
12121da177e4SLinus Torvalds 		ci->idx++;
12131da177e4SLinus Torvalds 	}
1214512875bdSJun'ichi Nomura 
1215512875bdSJun'ichi Nomura 	return 0;
12161da177e4SLinus Torvalds }
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds /*
12198a53c28dSMikulas Patocka  * Split the bio into several clones and submit it to targets.
12201da177e4SLinus Torvalds  */
1221f0b9a450SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
12221da177e4SLinus Torvalds {
12231da177e4SLinus Torvalds 	struct clone_info ci;
1224512875bdSJun'ichi Nomura 	int error = 0;
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 	ci.map = dm_get_table(md);
1227f0b9a450SMikulas Patocka 	if (unlikely(!ci.map)) {
12281f98a13fSJens Axboe 		if (!bio_rw_flagged(bio, BIO_RW_BARRIER))
1229f0b9a450SMikulas Patocka 			bio_io_error(bio);
1230af7e466aSMikulas Patocka 		else
12315aa2781dSMikulas Patocka 			if (!md->barrier_error)
1232af7e466aSMikulas Patocka 				md->barrier_error = -EIO;
1233f0b9a450SMikulas Patocka 		return;
1234f0b9a450SMikulas Patocka 	}
1235692d0eb9SMikulas Patocka 
12361da177e4SLinus Torvalds 	ci.md = md;
12371da177e4SLinus Torvalds 	ci.bio = bio;
12381da177e4SLinus Torvalds 	ci.io = alloc_io(md);
12391da177e4SLinus Torvalds 	ci.io->error = 0;
12401da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
12411da177e4SLinus Torvalds 	ci.io->bio = bio;
12421da177e4SLinus Torvalds 	ci.io->md = md;
1243f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
12441da177e4SLinus Torvalds 	ci.sector = bio->bi_sector;
12451da177e4SLinus Torvalds 	ci.sector_count = bio_sectors(bio);
1246f9ab94ceSMikulas Patocka 	if (unlikely(bio_empty_barrier(bio)))
1247f9ab94ceSMikulas Patocka 		ci.sector_count = 1;
12481da177e4SLinus Torvalds 	ci.idx = bio->bi_idx;
12491da177e4SLinus Torvalds 
12503eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1251512875bdSJun'ichi Nomura 	while (ci.sector_count && !error)
1252512875bdSJun'ichi Nomura 		error = __clone_and_map(&ci);
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 	/* drop the extra reference count */
1255512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
12561da177e4SLinus Torvalds 	dm_table_put(ci.map);
12571da177e4SLinus Torvalds }
12581da177e4SLinus Torvalds /*-----------------------------------------------------------------
12591da177e4SLinus Torvalds  * CRUD END
12601da177e4SLinus Torvalds  *---------------------------------------------------------------*/
12611da177e4SLinus Torvalds 
1262f6fccb12SMilan Broz static int dm_merge_bvec(struct request_queue *q,
1263f6fccb12SMilan Broz 			 struct bvec_merge_data *bvm,
1264f6fccb12SMilan Broz 			 struct bio_vec *biovec)
1265f6fccb12SMilan Broz {
1266f6fccb12SMilan Broz 	struct mapped_device *md = q->queuedata;
1267f6fccb12SMilan Broz 	struct dm_table *map = dm_get_table(md);
1268f6fccb12SMilan Broz 	struct dm_target *ti;
1269f6fccb12SMilan Broz 	sector_t max_sectors;
12705037108aSMikulas Patocka 	int max_size = 0;
1271f6fccb12SMilan Broz 
1272f6fccb12SMilan Broz 	if (unlikely(!map))
12735037108aSMikulas Patocka 		goto out;
1274f6fccb12SMilan Broz 
1275f6fccb12SMilan Broz 	ti = dm_table_find_target(map, bvm->bi_sector);
1276b01cd5acSMikulas Patocka 	if (!dm_target_is_valid(ti))
1277b01cd5acSMikulas Patocka 		goto out_table;
1278f6fccb12SMilan Broz 
1279f6fccb12SMilan Broz 	/*
1280f6fccb12SMilan Broz 	 * Find maximum amount of I/O that won't need splitting
1281f6fccb12SMilan Broz 	 */
1282f6fccb12SMilan Broz 	max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
1283f6fccb12SMilan Broz 			  (sector_t) BIO_MAX_SECTORS);
1284f6fccb12SMilan Broz 	max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1285f6fccb12SMilan Broz 	if (max_size < 0)
1286f6fccb12SMilan Broz 		max_size = 0;
1287f6fccb12SMilan Broz 
1288f6fccb12SMilan Broz 	/*
1289f6fccb12SMilan Broz 	 * merge_bvec_fn() returns number of bytes
1290f6fccb12SMilan Broz 	 * it can accept at this offset
1291f6fccb12SMilan Broz 	 * max is precomputed maximal io size
1292f6fccb12SMilan Broz 	 */
1293f6fccb12SMilan Broz 	if (max_size && ti->type->merge)
1294f6fccb12SMilan Broz 		max_size = ti->type->merge(ti, bvm, biovec, max_size);
12958cbeb67aSMikulas Patocka 	/*
12968cbeb67aSMikulas Patocka 	 * If the target doesn't support merge method and some of the devices
12978cbeb67aSMikulas Patocka 	 * provided their merge_bvec method (we know this by looking at
12988cbeb67aSMikulas Patocka 	 * queue_max_hw_sectors), then we can't allow bios with multiple vector
12998cbeb67aSMikulas Patocka 	 * entries.  So always set max_size to 0, and the code below allows
13008cbeb67aSMikulas Patocka 	 * just one page.
13018cbeb67aSMikulas Patocka 	 */
13028cbeb67aSMikulas Patocka 	else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
13038cbeb67aSMikulas Patocka 
13048cbeb67aSMikulas Patocka 		max_size = 0;
1305f6fccb12SMilan Broz 
1306b01cd5acSMikulas Patocka out_table:
13075037108aSMikulas Patocka 	dm_table_put(map);
13085037108aSMikulas Patocka 
13095037108aSMikulas Patocka out:
1310f6fccb12SMilan Broz 	/*
1311f6fccb12SMilan Broz 	 * Always allow an entire first page
1312f6fccb12SMilan Broz 	 */
1313f6fccb12SMilan Broz 	if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1314f6fccb12SMilan Broz 		max_size = biovec->bv_len;
1315f6fccb12SMilan Broz 
1316f6fccb12SMilan Broz 	return max_size;
1317f6fccb12SMilan Broz }
1318f6fccb12SMilan Broz 
13191da177e4SLinus Torvalds /*
13201da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
13211da177e4SLinus Torvalds  * dm_merge_bvec.
13221da177e4SLinus Torvalds  */
1323cec47e3dSKiyoshi Ueda static int _dm_request(struct request_queue *q, struct bio *bio)
13241da177e4SLinus Torvalds {
132512f03a49SKevin Corry 	int rw = bio_data_dir(bio);
13261da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
1327c9959059STejun Heo 	int cpu;
13281da177e4SLinus Torvalds 
13292ca3310eSAlasdair G Kergon 	down_read(&md->io_lock);
13301da177e4SLinus Torvalds 
1331074a7acaSTejun Heo 	cpu = part_stat_lock();
1332074a7acaSTejun Heo 	part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1333074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1334074a7acaSTejun Heo 	part_stat_unlock();
133512f03a49SKevin Corry 
13361da177e4SLinus Torvalds 	/*
13371eb787ecSAlasdair G Kergon 	 * If we're suspended or the thread is processing barriers
13381eb787ecSAlasdair G Kergon 	 * we have to queue this io for later.
13391da177e4SLinus Torvalds 	 */
1340af7e466aSMikulas Patocka 	if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
13411f98a13fSJens Axboe 	    unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
13422ca3310eSAlasdair G Kergon 		up_read(&md->io_lock);
13431da177e4SLinus Torvalds 
134454d9a1b4SAlasdair G Kergon 		if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
134554d9a1b4SAlasdair G Kergon 		    bio_rw(bio) == READA) {
134654d9a1b4SAlasdair G Kergon 			bio_io_error(bio);
134754d9a1b4SAlasdair G Kergon 			return 0;
134854d9a1b4SAlasdair G Kergon 		}
13491da177e4SLinus Torvalds 
135092c63902SMikulas Patocka 		queue_io(md, bio);
13511da177e4SLinus Torvalds 
135292c63902SMikulas Patocka 		return 0;
13531da177e4SLinus Torvalds 	}
13541da177e4SLinus Torvalds 
1355f0b9a450SMikulas Patocka 	__split_and_process_bio(md, bio);
13562ca3310eSAlasdair G Kergon 	up_read(&md->io_lock);
1357f0b9a450SMikulas Patocka 	return 0;
13581da177e4SLinus Torvalds }
13591da177e4SLinus Torvalds 
1360cec47e3dSKiyoshi Ueda static int dm_make_request(struct request_queue *q, struct bio *bio)
1361cec47e3dSKiyoshi Ueda {
1362cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1363cec47e3dSKiyoshi Ueda 
13641f98a13fSJens Axboe 	if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
1365cec47e3dSKiyoshi Ueda 		bio_endio(bio, -EOPNOTSUPP);
1366cec47e3dSKiyoshi Ueda 		return 0;
1367cec47e3dSKiyoshi Ueda 	}
1368cec47e3dSKiyoshi Ueda 
1369cec47e3dSKiyoshi Ueda 	return md->saved_make_request_fn(q, bio); /* call __make_request() */
1370cec47e3dSKiyoshi Ueda }
1371cec47e3dSKiyoshi Ueda 
1372cec47e3dSKiyoshi Ueda static int dm_request_based(struct mapped_device *md)
1373cec47e3dSKiyoshi Ueda {
1374cec47e3dSKiyoshi Ueda 	return blk_queue_stackable(md->queue);
1375cec47e3dSKiyoshi Ueda }
1376cec47e3dSKiyoshi Ueda 
1377cec47e3dSKiyoshi Ueda static int dm_request(struct request_queue *q, struct bio *bio)
1378cec47e3dSKiyoshi Ueda {
1379cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1380cec47e3dSKiyoshi Ueda 
1381cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
1382cec47e3dSKiyoshi Ueda 		return dm_make_request(q, bio);
1383cec47e3dSKiyoshi Ueda 
1384cec47e3dSKiyoshi Ueda 	return _dm_request(q, bio);
1385cec47e3dSKiyoshi Ueda }
1386cec47e3dSKiyoshi Ueda 
1387cec47e3dSKiyoshi Ueda void dm_dispatch_request(struct request *rq)
1388cec47e3dSKiyoshi Ueda {
1389cec47e3dSKiyoshi Ueda 	int r;
1390cec47e3dSKiyoshi Ueda 
1391cec47e3dSKiyoshi Ueda 	if (blk_queue_io_stat(rq->q))
1392cec47e3dSKiyoshi Ueda 		rq->cmd_flags |= REQ_IO_STAT;
1393cec47e3dSKiyoshi Ueda 
1394cec47e3dSKiyoshi Ueda 	rq->start_time = jiffies;
1395cec47e3dSKiyoshi Ueda 	r = blk_insert_cloned_request(rq->q, rq);
1396cec47e3dSKiyoshi Ueda 	if (r)
1397cec47e3dSKiyoshi Ueda 		dm_complete_request(rq, r);
1398cec47e3dSKiyoshi Ueda }
1399cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_dispatch_request);
1400cec47e3dSKiyoshi Ueda 
1401cec47e3dSKiyoshi Ueda static void dm_rq_bio_destructor(struct bio *bio)
1402cec47e3dSKiyoshi Ueda {
1403cec47e3dSKiyoshi Ueda 	struct dm_rq_clone_bio_info *info = bio->bi_private;
1404cec47e3dSKiyoshi Ueda 	struct mapped_device *md = info->tio->md;
1405cec47e3dSKiyoshi Ueda 
1406cec47e3dSKiyoshi Ueda 	free_bio_info(info);
1407cec47e3dSKiyoshi Ueda 	bio_free(bio, md->bs);
1408cec47e3dSKiyoshi Ueda }
1409cec47e3dSKiyoshi Ueda 
1410cec47e3dSKiyoshi Ueda static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1411cec47e3dSKiyoshi Ueda 				 void *data)
1412cec47e3dSKiyoshi Ueda {
1413cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = data;
1414cec47e3dSKiyoshi Ueda 	struct mapped_device *md = tio->md;
1415cec47e3dSKiyoshi Ueda 	struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1416cec47e3dSKiyoshi Ueda 
1417cec47e3dSKiyoshi Ueda 	if (!info)
1418cec47e3dSKiyoshi Ueda 		return -ENOMEM;
1419cec47e3dSKiyoshi Ueda 
1420cec47e3dSKiyoshi Ueda 	info->orig = bio_orig;
1421cec47e3dSKiyoshi Ueda 	info->tio = tio;
1422cec47e3dSKiyoshi Ueda 	bio->bi_end_io = end_clone_bio;
1423cec47e3dSKiyoshi Ueda 	bio->bi_private = info;
1424cec47e3dSKiyoshi Ueda 	bio->bi_destructor = dm_rq_bio_destructor;
1425cec47e3dSKiyoshi Ueda 
1426cec47e3dSKiyoshi Ueda 	return 0;
1427cec47e3dSKiyoshi Ueda }
1428cec47e3dSKiyoshi Ueda 
1429cec47e3dSKiyoshi Ueda static int setup_clone(struct request *clone, struct request *rq,
1430cec47e3dSKiyoshi Ueda 		       struct dm_rq_target_io *tio)
1431cec47e3dSKiyoshi Ueda {
1432cec47e3dSKiyoshi Ueda 	int r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1433cec47e3dSKiyoshi Ueda 				  dm_rq_bio_constructor, tio);
1434cec47e3dSKiyoshi Ueda 
1435cec47e3dSKiyoshi Ueda 	if (r)
1436cec47e3dSKiyoshi Ueda 		return r;
1437cec47e3dSKiyoshi Ueda 
1438cec47e3dSKiyoshi Ueda 	clone->cmd = rq->cmd;
1439cec47e3dSKiyoshi Ueda 	clone->cmd_len = rq->cmd_len;
1440cec47e3dSKiyoshi Ueda 	clone->sense = rq->sense;
1441cec47e3dSKiyoshi Ueda 	clone->buffer = rq->buffer;
1442cec47e3dSKiyoshi Ueda 	clone->end_io = end_clone_request;
1443cec47e3dSKiyoshi Ueda 	clone->end_io_data = tio;
1444cec47e3dSKiyoshi Ueda 
1445cec47e3dSKiyoshi Ueda 	return 0;
1446cec47e3dSKiyoshi Ueda }
1447cec47e3dSKiyoshi Ueda 
1448*6facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md,
1449*6facdaffSKiyoshi Ueda 				gfp_t gfp_mask)
1450*6facdaffSKiyoshi Ueda {
1451*6facdaffSKiyoshi Ueda 	struct request *clone;
1452*6facdaffSKiyoshi Ueda 	struct dm_rq_target_io *tio;
1453*6facdaffSKiyoshi Ueda 
1454*6facdaffSKiyoshi Ueda 	tio = alloc_rq_tio(md, gfp_mask);
1455*6facdaffSKiyoshi Ueda 	if (!tio)
1456*6facdaffSKiyoshi Ueda 		return NULL;
1457*6facdaffSKiyoshi Ueda 
1458*6facdaffSKiyoshi Ueda 	tio->md = md;
1459*6facdaffSKiyoshi Ueda 	tio->ti = NULL;
1460*6facdaffSKiyoshi Ueda 	tio->orig = rq;
1461*6facdaffSKiyoshi Ueda 	tio->error = 0;
1462*6facdaffSKiyoshi Ueda 	memset(&tio->info, 0, sizeof(tio->info));
1463*6facdaffSKiyoshi Ueda 
1464*6facdaffSKiyoshi Ueda 	clone = &tio->clone;
1465*6facdaffSKiyoshi Ueda 	if (setup_clone(clone, rq, tio)) {
1466*6facdaffSKiyoshi Ueda 		/* -ENOMEM */
1467*6facdaffSKiyoshi Ueda 		free_rq_tio(tio);
1468*6facdaffSKiyoshi Ueda 		return NULL;
1469*6facdaffSKiyoshi Ueda 	}
1470*6facdaffSKiyoshi Ueda 
1471*6facdaffSKiyoshi Ueda 	return clone;
1472*6facdaffSKiyoshi Ueda }
1473*6facdaffSKiyoshi Ueda 
1474cec47e3dSKiyoshi Ueda static int dm_rq_flush_suspending(struct mapped_device *md)
1475cec47e3dSKiyoshi Ueda {
1476cec47e3dSKiyoshi Ueda 	return !md->suspend_rq.special;
1477cec47e3dSKiyoshi Ueda }
1478cec47e3dSKiyoshi Ueda 
1479cec47e3dSKiyoshi Ueda /*
1480cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1481cec47e3dSKiyoshi Ueda  */
1482cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq)
1483cec47e3dSKiyoshi Ueda {
1484cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1485cec47e3dSKiyoshi Ueda 	struct request *clone;
1486cec47e3dSKiyoshi Ueda 
1487cec47e3dSKiyoshi Ueda 	if (unlikely(rq == &md->suspend_rq)) {
1488cec47e3dSKiyoshi Ueda 		if (dm_rq_flush_suspending(md))
1489cec47e3dSKiyoshi Ueda 			return BLKPREP_OK;
1490cec47e3dSKiyoshi Ueda 		else
1491cec47e3dSKiyoshi Ueda 			/* The flush suspend was interrupted */
1492cec47e3dSKiyoshi Ueda 			return BLKPREP_KILL;
1493cec47e3dSKiyoshi Ueda 	}
1494cec47e3dSKiyoshi Ueda 
1495cec47e3dSKiyoshi Ueda 	if (unlikely(rq->special)) {
1496cec47e3dSKiyoshi Ueda 		DMWARN("Already has something in rq->special.");
1497cec47e3dSKiyoshi Ueda 		return BLKPREP_KILL;
1498cec47e3dSKiyoshi Ueda 	}
1499cec47e3dSKiyoshi Ueda 
1500*6facdaffSKiyoshi Ueda 	clone = clone_rq(rq, md, GFP_ATOMIC);
1501*6facdaffSKiyoshi Ueda 	if (!clone)
1502cec47e3dSKiyoshi Ueda 		return BLKPREP_DEFER;
1503cec47e3dSKiyoshi Ueda 
1504cec47e3dSKiyoshi Ueda 	rq->special = clone;
1505cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_DONTPREP;
1506cec47e3dSKiyoshi Ueda 
1507cec47e3dSKiyoshi Ueda 	return BLKPREP_OK;
1508cec47e3dSKiyoshi Ueda }
1509cec47e3dSKiyoshi Ueda 
1510598de409SKiyoshi Ueda static void map_request(struct dm_target *ti, struct request *clone,
1511cec47e3dSKiyoshi Ueda 			struct mapped_device *md)
1512cec47e3dSKiyoshi Ueda {
1513cec47e3dSKiyoshi Ueda 	int r;
1514cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1515cec47e3dSKiyoshi Ueda 
1516cec47e3dSKiyoshi Ueda 	/*
1517cec47e3dSKiyoshi Ueda 	 * Hold the md reference here for the in-flight I/O.
1518cec47e3dSKiyoshi Ueda 	 * We can't rely on the reference count by device opener,
1519cec47e3dSKiyoshi Ueda 	 * because the device may be closed during the request completion
1520cec47e3dSKiyoshi Ueda 	 * when all bios are completed.
1521cec47e3dSKiyoshi Ueda 	 * See the comment in rq_completed() too.
1522cec47e3dSKiyoshi Ueda 	 */
1523cec47e3dSKiyoshi Ueda 	dm_get(md);
1524cec47e3dSKiyoshi Ueda 
1525cec47e3dSKiyoshi Ueda 	tio->ti = ti;
1526cec47e3dSKiyoshi Ueda 	r = ti->type->map_rq(ti, clone, &tio->info);
1527cec47e3dSKiyoshi Ueda 	switch (r) {
1528cec47e3dSKiyoshi Ueda 	case DM_MAPIO_SUBMITTED:
1529cec47e3dSKiyoshi Ueda 		/* The target has taken the I/O to submit by itself later */
1530cec47e3dSKiyoshi Ueda 		break;
1531cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REMAPPED:
1532cec47e3dSKiyoshi Ueda 		/* The target has remapped the I/O so dispatch it */
1533cec47e3dSKiyoshi Ueda 		dm_dispatch_request(clone);
1534cec47e3dSKiyoshi Ueda 		break;
1535cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REQUEUE:
1536cec47e3dSKiyoshi Ueda 		/* The target wants to requeue the I/O */
1537cec47e3dSKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
1538cec47e3dSKiyoshi Ueda 		break;
1539cec47e3dSKiyoshi Ueda 	default:
1540cec47e3dSKiyoshi Ueda 		if (r > 0) {
1541cec47e3dSKiyoshi Ueda 			DMWARN("unimplemented target map return value: %d", r);
1542cec47e3dSKiyoshi Ueda 			BUG();
1543cec47e3dSKiyoshi Ueda 		}
1544cec47e3dSKiyoshi Ueda 
1545cec47e3dSKiyoshi Ueda 		/* The target wants to complete the I/O */
1546cec47e3dSKiyoshi Ueda 		dm_kill_unmapped_request(clone, r);
1547cec47e3dSKiyoshi Ueda 		break;
1548cec47e3dSKiyoshi Ueda 	}
1549cec47e3dSKiyoshi Ueda }
1550cec47e3dSKiyoshi Ueda 
1551cec47e3dSKiyoshi Ueda /*
1552cec47e3dSKiyoshi Ueda  * q->request_fn for request-based dm.
1553cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1554cec47e3dSKiyoshi Ueda  */
1555cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q)
1556cec47e3dSKiyoshi Ueda {
1557cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1558cec47e3dSKiyoshi Ueda 	struct dm_table *map = dm_get_table(md);
1559cec47e3dSKiyoshi Ueda 	struct dm_target *ti;
1560cec47e3dSKiyoshi Ueda 	struct request *rq;
1561cec47e3dSKiyoshi Ueda 
1562cec47e3dSKiyoshi Ueda 	/*
1563cec47e3dSKiyoshi Ueda 	 * For noflush suspend, check blk_queue_stopped() to immediately
1564cec47e3dSKiyoshi Ueda 	 * quit I/O dispatching.
1565cec47e3dSKiyoshi Ueda 	 */
1566cec47e3dSKiyoshi Ueda 	while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1567cec47e3dSKiyoshi Ueda 		rq = blk_peek_request(q);
1568cec47e3dSKiyoshi Ueda 		if (!rq)
1569cec47e3dSKiyoshi Ueda 			goto plug_and_out;
1570cec47e3dSKiyoshi Ueda 
1571cec47e3dSKiyoshi Ueda 		if (unlikely(rq == &md->suspend_rq)) { /* Flush suspend maker */
1572cec47e3dSKiyoshi Ueda 			if (queue_in_flight(q))
1573cec47e3dSKiyoshi Ueda 				/* Not quiet yet.  Wait more */
1574cec47e3dSKiyoshi Ueda 				goto plug_and_out;
1575cec47e3dSKiyoshi Ueda 
1576cec47e3dSKiyoshi Ueda 			/* This device should be quiet now */
1577cec47e3dSKiyoshi Ueda 			__stop_queue(q);
1578cec47e3dSKiyoshi Ueda 			blk_start_request(rq);
1579cec47e3dSKiyoshi Ueda 			__blk_end_request_all(rq, 0);
1580cec47e3dSKiyoshi Ueda 			wake_up(&md->wait);
1581cec47e3dSKiyoshi Ueda 			goto out;
1582cec47e3dSKiyoshi Ueda 		}
1583cec47e3dSKiyoshi Ueda 
1584cec47e3dSKiyoshi Ueda 		ti = dm_table_find_target(map, blk_rq_pos(rq));
1585cec47e3dSKiyoshi Ueda 		if (ti->type->busy && ti->type->busy(ti))
1586cec47e3dSKiyoshi Ueda 			goto plug_and_out;
1587cec47e3dSKiyoshi Ueda 
1588cec47e3dSKiyoshi Ueda 		blk_start_request(rq);
1589cec47e3dSKiyoshi Ueda 		spin_unlock(q->queue_lock);
1590598de409SKiyoshi Ueda 		map_request(ti, rq->special, md);
1591cec47e3dSKiyoshi Ueda 		spin_lock_irq(q->queue_lock);
1592cec47e3dSKiyoshi Ueda 	}
1593cec47e3dSKiyoshi Ueda 
1594cec47e3dSKiyoshi Ueda 	goto out;
1595cec47e3dSKiyoshi Ueda 
1596cec47e3dSKiyoshi Ueda plug_and_out:
1597cec47e3dSKiyoshi Ueda 	if (!elv_queue_empty(q))
1598cec47e3dSKiyoshi Ueda 		/* Some requests still remain, retry later */
1599cec47e3dSKiyoshi Ueda 		blk_plug_device(q);
1600cec47e3dSKiyoshi Ueda 
1601cec47e3dSKiyoshi Ueda out:
1602cec47e3dSKiyoshi Ueda 	dm_table_put(map);
1603cec47e3dSKiyoshi Ueda 
1604cec47e3dSKiyoshi Ueda 	return;
1605cec47e3dSKiyoshi Ueda }
1606cec47e3dSKiyoshi Ueda 
1607cec47e3dSKiyoshi Ueda int dm_underlying_device_busy(struct request_queue *q)
1608cec47e3dSKiyoshi Ueda {
1609cec47e3dSKiyoshi Ueda 	return blk_lld_busy(q);
1610cec47e3dSKiyoshi Ueda }
1611cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1612cec47e3dSKiyoshi Ueda 
1613cec47e3dSKiyoshi Ueda static int dm_lld_busy(struct request_queue *q)
1614cec47e3dSKiyoshi Ueda {
1615cec47e3dSKiyoshi Ueda 	int r;
1616cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1617cec47e3dSKiyoshi Ueda 	struct dm_table *map = dm_get_table(md);
1618cec47e3dSKiyoshi Ueda 
1619cec47e3dSKiyoshi Ueda 	if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1620cec47e3dSKiyoshi Ueda 		r = 1;
1621cec47e3dSKiyoshi Ueda 	else
1622cec47e3dSKiyoshi Ueda 		r = dm_table_any_busy_target(map);
1623cec47e3dSKiyoshi Ueda 
1624cec47e3dSKiyoshi Ueda 	dm_table_put(map);
1625cec47e3dSKiyoshi Ueda 
1626cec47e3dSKiyoshi Ueda 	return r;
1627cec47e3dSKiyoshi Ueda }
1628cec47e3dSKiyoshi Ueda 
1629165125e1SJens Axboe static void dm_unplug_all(struct request_queue *q)
16301da177e4SLinus Torvalds {
16311da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
16321da177e4SLinus Torvalds 	struct dm_table *map = dm_get_table(md);
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds 	if (map) {
1635cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
1636cec47e3dSKiyoshi Ueda 			generic_unplug_device(q);
1637cec47e3dSKiyoshi Ueda 
16381da177e4SLinus Torvalds 		dm_table_unplug_all(map);
16391da177e4SLinus Torvalds 		dm_table_put(map);
16401da177e4SLinus Torvalds 	}
16411da177e4SLinus Torvalds }
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
16441da177e4SLinus Torvalds {
16458a57dfc6SChandra Seetharaman 	int r = bdi_bits;
16468a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
16478a57dfc6SChandra Seetharaman 	struct dm_table *map;
16481da177e4SLinus Torvalds 
16491eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
16508a57dfc6SChandra Seetharaman 		map = dm_get_table(md);
16518a57dfc6SChandra Seetharaman 		if (map) {
1652cec47e3dSKiyoshi Ueda 			/*
1653cec47e3dSKiyoshi Ueda 			 * Request-based dm cares about only own queue for
1654cec47e3dSKiyoshi Ueda 			 * the query about congestion status of request_queue
1655cec47e3dSKiyoshi Ueda 			 */
1656cec47e3dSKiyoshi Ueda 			if (dm_request_based(md))
1657cec47e3dSKiyoshi Ueda 				r = md->queue->backing_dev_info.state &
1658cec47e3dSKiyoshi Ueda 				    bdi_bits;
1659cec47e3dSKiyoshi Ueda 			else
16601da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
1661cec47e3dSKiyoshi Ueda 
16621da177e4SLinus Torvalds 			dm_table_put(map);
16638a57dfc6SChandra Seetharaman 		}
16648a57dfc6SChandra Seetharaman 	}
16658a57dfc6SChandra Seetharaman 
16661da177e4SLinus Torvalds 	return r;
16671da177e4SLinus Torvalds }
16681da177e4SLinus Torvalds 
16691da177e4SLinus Torvalds /*-----------------------------------------------------------------
16701da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
16711da177e4SLinus Torvalds  *---------------------------------------------------------------*/
16721da177e4SLinus Torvalds static DEFINE_IDR(_minor_idr);
16731da177e4SLinus Torvalds 
16742b06cfffSAlasdair G Kergon static void free_minor(int minor)
16751da177e4SLinus Torvalds {
1676f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
16771da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1678f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
16791da177e4SLinus Torvalds }
16801da177e4SLinus Torvalds 
16811da177e4SLinus Torvalds /*
16821da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
16831da177e4SLinus Torvalds  */
1684cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
16851da177e4SLinus Torvalds {
16861da177e4SLinus Torvalds 	int r, m;
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
16891da177e4SLinus Torvalds 		return -EINVAL;
16901da177e4SLinus Torvalds 
169162f75c2fSJeff Mahoney 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
169262f75c2fSJeff Mahoney 	if (!r)
169362f75c2fSJeff Mahoney 		return -ENOMEM;
169462f75c2fSJeff Mahoney 
1695f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
16961da177e4SLinus Torvalds 
16971da177e4SLinus Torvalds 	if (idr_find(&_minor_idr, minor)) {
16981da177e4SLinus Torvalds 		r = -EBUSY;
16991da177e4SLinus Torvalds 		goto out;
17001da177e4SLinus Torvalds 	}
17011da177e4SLinus Torvalds 
1702ba61fdd1SJeff Mahoney 	r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
170362f75c2fSJeff Mahoney 	if (r)
17041da177e4SLinus Torvalds 		goto out;
17051da177e4SLinus Torvalds 
17061da177e4SLinus Torvalds 	if (m != minor) {
17071da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
17081da177e4SLinus Torvalds 		r = -EBUSY;
17091da177e4SLinus Torvalds 		goto out;
17101da177e4SLinus Torvalds 	}
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds out:
1713f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
17141da177e4SLinus Torvalds 	return r;
17151da177e4SLinus Torvalds }
17161da177e4SLinus Torvalds 
1717cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
17181da177e4SLinus Torvalds {
17192b06cfffSAlasdair G Kergon 	int r, m;
17201da177e4SLinus Torvalds 
17211da177e4SLinus Torvalds 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
172262f75c2fSJeff Mahoney 	if (!r)
172362f75c2fSJeff Mahoney 		return -ENOMEM;
172462f75c2fSJeff Mahoney 
1725f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17261da177e4SLinus Torvalds 
1727ba61fdd1SJeff Mahoney 	r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
1728cf13ab8eSFrederik Deweerdt 	if (r)
17291da177e4SLinus Torvalds 		goto out;
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds 	if (m >= (1 << MINORBITS)) {
17321da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
17331da177e4SLinus Torvalds 		r = -ENOSPC;
17341da177e4SLinus Torvalds 		goto out;
17351da177e4SLinus Torvalds 	}
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds 	*minor = m;
17381da177e4SLinus Torvalds 
17391da177e4SLinus Torvalds out:
1740f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
17411da177e4SLinus Torvalds 	return r;
17421da177e4SLinus Torvalds }
17431da177e4SLinus Torvalds 
174483d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
17451da177e4SLinus Torvalds 
174653d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
174753d5914fSMikulas Patocka 
17481da177e4SLinus Torvalds /*
17491da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
17501da177e4SLinus Torvalds  */
17512b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
17521da177e4SLinus Torvalds {
17531da177e4SLinus Torvalds 	int r;
1754cf13ab8eSFrederik Deweerdt 	struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1755ba61fdd1SJeff Mahoney 	void *old_md;
17561da177e4SLinus Torvalds 
17571da177e4SLinus Torvalds 	if (!md) {
17581da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
17591da177e4SLinus Torvalds 		return NULL;
17601da177e4SLinus Torvalds 	}
17611da177e4SLinus Torvalds 
176210da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
17636ed7ade8SMilan Broz 		goto bad_module_get;
176410da4f79SJeff Mahoney 
17651da177e4SLinus Torvalds 	/* get a minor number for the dev */
17662b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1767cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
17682b06cfffSAlasdair G Kergon 	else
1769cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
17701da177e4SLinus Torvalds 	if (r < 0)
17716ed7ade8SMilan Broz 		goto bad_minor;
17721da177e4SLinus Torvalds 
17732ca3310eSAlasdair G Kergon 	init_rwsem(&md->io_lock);
1774e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1775022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
17761da177e4SLinus Torvalds 	rwlock_init(&md->map_lock);
17771da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
17785c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
17791da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
17807a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
17817a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
17827a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
17831da177e4SLinus Torvalds 
1784e6ee8c0bSKiyoshi Ueda 	md->queue = blk_init_queue(dm_request_fn, NULL);
17851da177e4SLinus Torvalds 	if (!md->queue)
17866ed7ade8SMilan Broz 		goto bad_queue;
17871da177e4SLinus Torvalds 
1788e6ee8c0bSKiyoshi Ueda 	/*
1789e6ee8c0bSKiyoshi Ueda 	 * Request-based dm devices cannot be stacked on top of bio-based dm
1790e6ee8c0bSKiyoshi Ueda 	 * devices.  The type of this dm device has not been decided yet,
1791e6ee8c0bSKiyoshi Ueda 	 * although we initialized the queue using blk_init_queue().
1792e6ee8c0bSKiyoshi Ueda 	 * The type is decided at the first table loading time.
1793e6ee8c0bSKiyoshi Ueda 	 * To prevent problematic device stacking, clear the queue flag
1794e6ee8c0bSKiyoshi Ueda 	 * for request stacking support until then.
1795e6ee8c0bSKiyoshi Ueda 	 *
1796e6ee8c0bSKiyoshi Ueda 	 * This queue is new, so no concurrency on the queue_flags.
1797e6ee8c0bSKiyoshi Ueda 	 */
1798e6ee8c0bSKiyoshi Ueda 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1799e6ee8c0bSKiyoshi Ueda 	md->saved_make_request_fn = md->queue->make_request_fn;
18001da177e4SLinus Torvalds 	md->queue->queuedata = md;
18011da177e4SLinus Torvalds 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
18021da177e4SLinus Torvalds 	md->queue->backing_dev_info.congested_data = md;
18031da177e4SLinus Torvalds 	blk_queue_make_request(md->queue, dm_request);
1804daef265fSJens Axboe 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
18051da177e4SLinus Torvalds 	md->queue->unplug_fn = dm_unplug_all;
1806f6fccb12SMilan Broz 	blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1807e6ee8c0bSKiyoshi Ueda 	blk_queue_softirq_done(md->queue, dm_softirq_done);
1808e6ee8c0bSKiyoshi Ueda 	blk_queue_prep_rq(md->queue, dm_prep_fn);
1809e6ee8c0bSKiyoshi Ueda 	blk_queue_lld_busy(md->queue, dm_lld_busy);
18109faf400fSStefan Bader 
18111da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
18121da177e4SLinus Torvalds 	if (!md->disk)
18136ed7ade8SMilan Broz 		goto bad_disk;
18141da177e4SLinus Torvalds 
1815316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
1816316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
1817f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
181853d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1819f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
1820f0b04115SJeff Mahoney 
18211da177e4SLinus Torvalds 	md->disk->major = _major;
18221da177e4SLinus Torvalds 	md->disk->first_minor = minor;
18231da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
18241da177e4SLinus Torvalds 	md->disk->queue = md->queue;
18251da177e4SLinus Torvalds 	md->disk->private_data = md;
18261da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
18271da177e4SLinus Torvalds 	add_disk(md->disk);
18287e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
18291da177e4SLinus Torvalds 
1830304f3f6aSMilan Broz 	md->wq = create_singlethread_workqueue("kdmflush");
1831304f3f6aSMilan Broz 	if (!md->wq)
1832304f3f6aSMilan Broz 		goto bad_thread;
1833304f3f6aSMilan Broz 
183432a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
183532a926daSMikulas Patocka 	if (!md->bdev)
183632a926daSMikulas Patocka 		goto bad_bdev;
183732a926daSMikulas Patocka 
1838ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1839f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1840ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1841f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1842ba61fdd1SJeff Mahoney 
1843ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1844ba61fdd1SJeff Mahoney 
18451da177e4SLinus Torvalds 	return md;
18461da177e4SLinus Torvalds 
184732a926daSMikulas Patocka bad_bdev:
184832a926daSMikulas Patocka 	destroy_workqueue(md->wq);
1849304f3f6aSMilan Broz bad_thread:
185003022c54SZdenek Kabelac 	del_gendisk(md->disk);
1851304f3f6aSMilan Broz 	put_disk(md->disk);
18526ed7ade8SMilan Broz bad_disk:
18531312f40eSAl Viro 	blk_cleanup_queue(md->queue);
18546ed7ade8SMilan Broz bad_queue:
18551da177e4SLinus Torvalds 	free_minor(minor);
18566ed7ade8SMilan Broz bad_minor:
185710da4f79SJeff Mahoney 	module_put(THIS_MODULE);
18586ed7ade8SMilan Broz bad_module_get:
18591da177e4SLinus Torvalds 	kfree(md);
18601da177e4SLinus Torvalds 	return NULL;
18611da177e4SLinus Torvalds }
18621da177e4SLinus Torvalds 
1863ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1864ae9da83fSJun'ichi Nomura 
18651da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
18661da177e4SLinus Torvalds {
1867f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
186863d94e48SJun'ichi Nomura 
1869ae9da83fSJun'ichi Nomura 	unlock_fs(md);
1870db8fef4fSMikulas Patocka 	bdput(md->bdev);
1871304f3f6aSMilan Broz 	destroy_workqueue(md->wq);
1872e6ee8c0bSKiyoshi Ueda 	if (md->tio_pool)
18731da177e4SLinus Torvalds 		mempool_destroy(md->tio_pool);
1874e6ee8c0bSKiyoshi Ueda 	if (md->io_pool)
18751da177e4SLinus Torvalds 		mempool_destroy(md->io_pool);
1876e6ee8c0bSKiyoshi Ueda 	if (md->bs)
18779faf400fSStefan Bader 		bioset_free(md->bs);
18789c47008dSMartin K. Petersen 	blk_integrity_unregister(md->disk);
18791da177e4SLinus Torvalds 	del_gendisk(md->disk);
188063d94e48SJun'ichi Nomura 	free_minor(minor);
1881fba9f90eSJeff Mahoney 
1882fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
1883fba9f90eSJeff Mahoney 	md->disk->private_data = NULL;
1884fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
1885fba9f90eSJeff Mahoney 
18861da177e4SLinus Torvalds 	put_disk(md->disk);
18871312f40eSAl Viro 	blk_cleanup_queue(md->queue);
188810da4f79SJeff Mahoney 	module_put(THIS_MODULE);
18891da177e4SLinus Torvalds 	kfree(md);
18901da177e4SLinus Torvalds }
18911da177e4SLinus Torvalds 
1892e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1893e6ee8c0bSKiyoshi Ueda {
1894e6ee8c0bSKiyoshi Ueda 	struct dm_md_mempools *p;
1895e6ee8c0bSKiyoshi Ueda 
1896e6ee8c0bSKiyoshi Ueda 	if (md->io_pool && md->tio_pool && md->bs)
1897e6ee8c0bSKiyoshi Ueda 		/* the md already has necessary mempools */
1898e6ee8c0bSKiyoshi Ueda 		goto out;
1899e6ee8c0bSKiyoshi Ueda 
1900e6ee8c0bSKiyoshi Ueda 	p = dm_table_get_md_mempools(t);
1901e6ee8c0bSKiyoshi Ueda 	BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
1902e6ee8c0bSKiyoshi Ueda 
1903e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
1904e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
1905e6ee8c0bSKiyoshi Ueda 	md->tio_pool = p->tio_pool;
1906e6ee8c0bSKiyoshi Ueda 	p->tio_pool = NULL;
1907e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
1908e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
1909e6ee8c0bSKiyoshi Ueda 
1910e6ee8c0bSKiyoshi Ueda out:
1911e6ee8c0bSKiyoshi Ueda 	/* mempool bind completed, now no need any mempools in the table */
1912e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
1913e6ee8c0bSKiyoshi Ueda }
1914e6ee8c0bSKiyoshi Ueda 
19151da177e4SLinus Torvalds /*
19161da177e4SLinus Torvalds  * Bind a table to the device.
19171da177e4SLinus Torvalds  */
19181da177e4SLinus Torvalds static void event_callback(void *context)
19191da177e4SLinus Torvalds {
19207a8c3d3bSMike Anderson 	unsigned long flags;
19217a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
19221da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
19231da177e4SLinus Torvalds 
19247a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
19257a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
19267a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
19277a8c3d3bSMike Anderson 
1928ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
19297a8c3d3bSMike Anderson 
19301da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
19311da177e4SLinus Torvalds 	wake_up(&md->eventq);
19321da177e4SLinus Torvalds }
19331da177e4SLinus Torvalds 
19344e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
19351da177e4SLinus Torvalds {
19364e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
19371da177e4SLinus Torvalds 
1938db8fef4fSMikulas Patocka 	mutex_lock(&md->bdev->bd_inode->i_mutex);
1939db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1940db8fef4fSMikulas Patocka 	mutex_unlock(&md->bdev->bd_inode->i_mutex);
19411da177e4SLinus Torvalds }
19421da177e4SLinus Torvalds 
1943754c5fc7SMike Snitzer static int __bind(struct mapped_device *md, struct dm_table *t,
1944754c5fc7SMike Snitzer 		  struct queue_limits *limits)
19451da177e4SLinus Torvalds {
1946165125e1SJens Axboe 	struct request_queue *q = md->queue;
19471da177e4SLinus Torvalds 	sector_t size;
1948523d9297SKiyoshi Ueda 	unsigned long flags;
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds 	size = dm_table_get_size(t);
19513ac51e74SDarrick J. Wong 
19523ac51e74SDarrick J. Wong 	/*
19533ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
19543ac51e74SDarrick J. Wong 	 */
19553ac51e74SDarrick J. Wong 	if (size != get_capacity(md->disk))
19563ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
19573ac51e74SDarrick J. Wong 
19584e90188bSAlasdair G Kergon 	__set_size(md, size);
19591da177e4SLinus Torvalds 
1960d5816876SMikulas Patocka 	if (!size) {
1961d5816876SMikulas Patocka 		dm_table_destroy(t);
1962d5816876SMikulas Patocka 		return 0;
1963d5816876SMikulas Patocka 	}
1964d5816876SMikulas Patocka 
1965cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
19662ca3310eSAlasdair G Kergon 
1967e6ee8c0bSKiyoshi Ueda 	/*
1968e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
1969e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
1970e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
1971e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
1972e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
1973e6ee8c0bSKiyoshi Ueda 	 */
1974e6ee8c0bSKiyoshi Ueda 	if (dm_table_request_based(t) && !blk_queue_stopped(q))
1975e6ee8c0bSKiyoshi Ueda 		stop_queue(q);
1976e6ee8c0bSKiyoshi Ueda 
1977e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
1978e6ee8c0bSKiyoshi Ueda 
1979523d9297SKiyoshi Ueda 	write_lock_irqsave(&md->map_lock, flags);
19802ca3310eSAlasdair G Kergon 	md->map = t;
1981754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
1982523d9297SKiyoshi Ueda 	write_unlock_irqrestore(&md->map_lock, flags);
19832ca3310eSAlasdair G Kergon 
19841da177e4SLinus Torvalds 	return 0;
19851da177e4SLinus Torvalds }
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds static void __unbind(struct mapped_device *md)
19881da177e4SLinus Torvalds {
19891da177e4SLinus Torvalds 	struct dm_table *map = md->map;
1990523d9297SKiyoshi Ueda 	unsigned long flags;
19911da177e4SLinus Torvalds 
19921da177e4SLinus Torvalds 	if (!map)
19931da177e4SLinus Torvalds 		return;
19941da177e4SLinus Torvalds 
19951da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
1996523d9297SKiyoshi Ueda 	write_lock_irqsave(&md->map_lock, flags);
19971da177e4SLinus Torvalds 	md->map = NULL;
1998523d9297SKiyoshi Ueda 	write_unlock_irqrestore(&md->map_lock, flags);
1999d5816876SMikulas Patocka 	dm_table_destroy(map);
20001da177e4SLinus Torvalds }
20011da177e4SLinus Torvalds 
20021da177e4SLinus Torvalds /*
20031da177e4SLinus Torvalds  * Constructor for a new device.
20041da177e4SLinus Torvalds  */
20052b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
20061da177e4SLinus Torvalds {
20071da177e4SLinus Torvalds 	struct mapped_device *md;
20081da177e4SLinus Torvalds 
20092b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
20101da177e4SLinus Torvalds 	if (!md)
20111da177e4SLinus Torvalds 		return -ENXIO;
20121da177e4SLinus Torvalds 
2013784aae73SMilan Broz 	dm_sysfs_init(md);
2014784aae73SMilan Broz 
20151da177e4SLinus Torvalds 	*result = md;
20161da177e4SLinus Torvalds 	return 0;
20171da177e4SLinus Torvalds }
20181da177e4SLinus Torvalds 
2019637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev)
20201da177e4SLinus Torvalds {
20211da177e4SLinus Torvalds 	struct mapped_device *md;
20221da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
20231da177e4SLinus Torvalds 
20241da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
20251da177e4SLinus Torvalds 		return NULL;
20261da177e4SLinus Torvalds 
2027f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
20281da177e4SLinus Torvalds 
20291da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
2030fba9f90eSJeff Mahoney 	if (md && (md == MINOR_ALLOCED ||
2031f331c029STejun Heo 		   (MINOR(disk_devt(dm_disk(md))) != minor) ||
2032fba9f90eSJeff Mahoney 		   test_bit(DMF_FREEING, &md->flags))) {
2033637842cfSDavid Teigland 		md = NULL;
2034fba9f90eSJeff Mahoney 		goto out;
2035fba9f90eSJeff Mahoney 	}
20361da177e4SLinus Torvalds 
2037fba9f90eSJeff Mahoney out:
2038f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
20391da177e4SLinus Torvalds 
2040637842cfSDavid Teigland 	return md;
2041637842cfSDavid Teigland }
2042637842cfSDavid Teigland 
2043d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev)
2044d229a958SDavid Teigland {
2045d229a958SDavid Teigland 	struct mapped_device *md = dm_find_md(dev);
2046d229a958SDavid Teigland 
2047d229a958SDavid Teigland 	if (md)
2048d229a958SDavid Teigland 		dm_get(md);
2049d229a958SDavid Teigland 
2050d229a958SDavid Teigland 	return md;
2051d229a958SDavid Teigland }
2052d229a958SDavid Teigland 
20539ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2054637842cfSDavid Teigland {
20559ade92a9SAlasdair G Kergon 	return md->interface_ptr;
20561da177e4SLinus Torvalds }
20571da177e4SLinus Torvalds 
20581da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
20591da177e4SLinus Torvalds {
20601da177e4SLinus Torvalds 	md->interface_ptr = ptr;
20611da177e4SLinus Torvalds }
20621da177e4SLinus Torvalds 
20631da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
20641da177e4SLinus Torvalds {
20651da177e4SLinus Torvalds 	atomic_inc(&md->holders);
20661da177e4SLinus Torvalds }
20671da177e4SLinus Torvalds 
206872d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
206972d94861SAlasdair G Kergon {
207072d94861SAlasdair G Kergon 	return md->name;
207172d94861SAlasdair G Kergon }
207272d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
207372d94861SAlasdair G Kergon 
20741da177e4SLinus Torvalds void dm_put(struct mapped_device *md)
20751da177e4SLinus Torvalds {
20761134e5aeSMike Anderson 	struct dm_table *map;
20771da177e4SLinus Torvalds 
2078fba9f90eSJeff Mahoney 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
2079fba9f90eSJeff Mahoney 
2080f32c10b0SJeff Mahoney 	if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
20811134e5aeSMike Anderson 		map = dm_get_table(md);
2082f331c029STejun Heo 		idr_replace(&_minor_idr, MINOR_ALLOCED,
2083f331c029STejun Heo 			    MINOR(disk_devt(dm_disk(md))));
2084fba9f90eSJeff Mahoney 		set_bit(DMF_FREEING, &md->flags);
2085f32c10b0SJeff Mahoney 		spin_unlock(&_minor_lock);
2086cf222b37SAlasdair G Kergon 		if (!dm_suspended(md)) {
20871da177e4SLinus Torvalds 			dm_table_presuspend_targets(map);
20881da177e4SLinus Torvalds 			dm_table_postsuspend_targets(map);
20891da177e4SLinus Torvalds 		}
2090784aae73SMilan Broz 		dm_sysfs_exit(md);
20911134e5aeSMike Anderson 		dm_table_put(map);
2092a1b51e98SMikulas Patocka 		__unbind(md);
20931da177e4SLinus Torvalds 		free_dev(md);
20941da177e4SLinus Torvalds 	}
20951da177e4SLinus Torvalds }
209679eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
20971da177e4SLinus Torvalds 
2098401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
209946125c1cSMilan Broz {
210046125c1cSMilan Broz 	int r = 0;
2101b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
2102cec47e3dSKiyoshi Ueda 	struct request_queue *q = md->queue;
2103cec47e3dSKiyoshi Ueda 	unsigned long flags;
2104b44ebeb0SMikulas Patocka 
2105b44ebeb0SMikulas Patocka 	dm_unplug_all(md->queue);
2106b44ebeb0SMikulas Patocka 
2107b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
210846125c1cSMilan Broz 
210946125c1cSMilan Broz 	while (1) {
2110401600dfSMikulas Patocka 		set_current_state(interruptible);
211146125c1cSMilan Broz 
211246125c1cSMilan Broz 		smp_mb();
2113cec47e3dSKiyoshi Ueda 		if (dm_request_based(md)) {
2114cec47e3dSKiyoshi Ueda 			spin_lock_irqsave(q->queue_lock, flags);
2115cec47e3dSKiyoshi Ueda 			if (!queue_in_flight(q) && blk_queue_stopped(q)) {
2116cec47e3dSKiyoshi Ueda 				spin_unlock_irqrestore(q->queue_lock, flags);
2117cec47e3dSKiyoshi Ueda 				break;
2118cec47e3dSKiyoshi Ueda 			}
2119cec47e3dSKiyoshi Ueda 			spin_unlock_irqrestore(q->queue_lock, flags);
212090abb8c4SKiyoshi Ueda 		} else if (!md_in_flight(md))
212146125c1cSMilan Broz 			break;
212246125c1cSMilan Broz 
2123401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
2124401600dfSMikulas Patocka 		    signal_pending(current)) {
212546125c1cSMilan Broz 			r = -EINTR;
212646125c1cSMilan Broz 			break;
212746125c1cSMilan Broz 		}
212846125c1cSMilan Broz 
212946125c1cSMilan Broz 		io_schedule();
213046125c1cSMilan Broz 	}
213146125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
213246125c1cSMilan Broz 
2133b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
2134b44ebeb0SMikulas Patocka 
213546125c1cSMilan Broz 	return r;
213646125c1cSMilan Broz }
213746125c1cSMilan Broz 
2138531fe963SMikulas Patocka static void dm_flush(struct mapped_device *md)
2139af7e466aSMikulas Patocka {
2140af7e466aSMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
214152b1fd5aSMikulas Patocka 
214252b1fd5aSMikulas Patocka 	bio_init(&md->barrier_bio);
214352b1fd5aSMikulas Patocka 	md->barrier_bio.bi_bdev = md->bdev;
214452b1fd5aSMikulas Patocka 	md->barrier_bio.bi_rw = WRITE_BARRIER;
214552b1fd5aSMikulas Patocka 	__split_and_process_bio(md, &md->barrier_bio);
214652b1fd5aSMikulas Patocka 
214752b1fd5aSMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2148af7e466aSMikulas Patocka }
2149af7e466aSMikulas Patocka 
2150af7e466aSMikulas Patocka static void process_barrier(struct mapped_device *md, struct bio *bio)
2151af7e466aSMikulas Patocka {
21525aa2781dSMikulas Patocka 	md->barrier_error = 0;
21535aa2781dSMikulas Patocka 
2154531fe963SMikulas Patocka 	dm_flush(md);
2155af7e466aSMikulas Patocka 
21565aa2781dSMikulas Patocka 	if (!bio_empty_barrier(bio)) {
2157af7e466aSMikulas Patocka 		__split_and_process_bio(md, bio);
2158531fe963SMikulas Patocka 		dm_flush(md);
21595aa2781dSMikulas Patocka 	}
2160af7e466aSMikulas Patocka 
2161af7e466aSMikulas Patocka 	if (md->barrier_error != DM_ENDIO_REQUEUE)
2162531fe963SMikulas Patocka 		bio_endio(bio, md->barrier_error);
21632761e95fSMikulas Patocka 	else {
21642761e95fSMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
21652761e95fSMikulas Patocka 		bio_list_add_head(&md->deferred, bio);
21662761e95fSMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
21672761e95fSMikulas Patocka 	}
2168af7e466aSMikulas Patocka }
2169af7e466aSMikulas Patocka 
21701da177e4SLinus Torvalds /*
21711da177e4SLinus Torvalds  * Process the deferred bios
21721da177e4SLinus Torvalds  */
2173ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
21741da177e4SLinus Torvalds {
2175ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2176ef208587SMikulas Patocka 						work);
21776d6f10dfSMilan Broz 	struct bio *c;
21781da177e4SLinus Torvalds 
2179ef208587SMikulas Patocka 	down_write(&md->io_lock);
2180ef208587SMikulas Patocka 
21813b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2182022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2183022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2184022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2185022c2611SMikulas Patocka 
2186df12ee99SAlasdair G Kergon 		if (!c) {
21871eb787ecSAlasdair G Kergon 			clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
2188df12ee99SAlasdair G Kergon 			break;
2189022c2611SMikulas Patocka 		}
219073d410c0SMilan Broz 
21913b00b203SMikulas Patocka 		up_write(&md->io_lock);
21923b00b203SMikulas Patocka 
2193e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2194e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2195e6ee8c0bSKiyoshi Ueda 		else {
21961f98a13fSJens Axboe 			if (bio_rw_flagged(c, BIO_RW_BARRIER))
2197af7e466aSMikulas Patocka 				process_barrier(md, c);
2198af7e466aSMikulas Patocka 			else
2199df12ee99SAlasdair G Kergon 				__split_and_process_bio(md, c);
2200e6ee8c0bSKiyoshi Ueda 		}
22013b00b203SMikulas Patocka 
22023b00b203SMikulas Patocka 		down_write(&md->io_lock);
2203df12ee99SAlasdair G Kergon 	}
2204ef208587SMikulas Patocka 
2205ef208587SMikulas Patocka 	up_write(&md->io_lock);
22061da177e4SLinus Torvalds }
22071da177e4SLinus Torvalds 
22089a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2209304f3f6aSMilan Broz {
22103b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
22113b00b203SMikulas Patocka 	smp_mb__after_clear_bit();
221253d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2213304f3f6aSMilan Broz }
2214304f3f6aSMilan Broz 
22151da177e4SLinus Torvalds /*
22161da177e4SLinus Torvalds  * Swap in a new table (destroying old one).
22171da177e4SLinus Torvalds  */
22181da177e4SLinus Torvalds int dm_swap_table(struct mapped_device *md, struct dm_table *table)
22191da177e4SLinus Torvalds {
2220754c5fc7SMike Snitzer 	struct queue_limits limits;
222193c534aeSAlasdair G Kergon 	int r = -EINVAL;
22221da177e4SLinus Torvalds 
2223e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
22241da177e4SLinus Torvalds 
22251da177e4SLinus Torvalds 	/* device must be suspended */
2226cf222b37SAlasdair G Kergon 	if (!dm_suspended(md))
222793c534aeSAlasdair G Kergon 		goto out;
22281da177e4SLinus Torvalds 
2229754c5fc7SMike Snitzer 	r = dm_calculate_queue_limits(table, &limits);
2230754c5fc7SMike Snitzer 	if (r)
2231754c5fc7SMike Snitzer 		goto out;
2232754c5fc7SMike Snitzer 
2233e6ee8c0bSKiyoshi Ueda 	/* cannot change the device type, once a table is bound */
2234e6ee8c0bSKiyoshi Ueda 	if (md->map &&
2235e6ee8c0bSKiyoshi Ueda 	    (dm_table_get_type(md->map) != dm_table_get_type(table))) {
2236e6ee8c0bSKiyoshi Ueda 		DMWARN("can't change the device type after a table is bound");
2237e6ee8c0bSKiyoshi Ueda 		goto out;
2238e6ee8c0bSKiyoshi Ueda 	}
2239e6ee8c0bSKiyoshi Ueda 
22401da177e4SLinus Torvalds 	__unbind(md);
2241754c5fc7SMike Snitzer 	r = __bind(md, table, &limits);
22421da177e4SLinus Torvalds 
224393c534aeSAlasdair G Kergon out:
2244e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
224593c534aeSAlasdair G Kergon 	return r;
22461da177e4SLinus Torvalds }
22471da177e4SLinus Torvalds 
2248cec47e3dSKiyoshi Ueda static void dm_rq_invalidate_suspend_marker(struct mapped_device *md)
2249cec47e3dSKiyoshi Ueda {
2250cec47e3dSKiyoshi Ueda 	md->suspend_rq.special = (void *)0x1;
2251cec47e3dSKiyoshi Ueda }
2252cec47e3dSKiyoshi Ueda 
2253cec47e3dSKiyoshi Ueda static void dm_rq_abort_suspend(struct mapped_device *md, int noflush)
2254cec47e3dSKiyoshi Ueda {
2255cec47e3dSKiyoshi Ueda 	struct request_queue *q = md->queue;
2256cec47e3dSKiyoshi Ueda 	unsigned long flags;
2257cec47e3dSKiyoshi Ueda 
2258cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
2259cec47e3dSKiyoshi Ueda 	if (!noflush)
2260cec47e3dSKiyoshi Ueda 		dm_rq_invalidate_suspend_marker(md);
2261cec47e3dSKiyoshi Ueda 	__start_queue(q);
2262cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
2263cec47e3dSKiyoshi Ueda }
2264cec47e3dSKiyoshi Ueda 
2265cec47e3dSKiyoshi Ueda static void dm_rq_start_suspend(struct mapped_device *md, int noflush)
2266cec47e3dSKiyoshi Ueda {
2267cec47e3dSKiyoshi Ueda 	struct request *rq = &md->suspend_rq;
2268cec47e3dSKiyoshi Ueda 	struct request_queue *q = md->queue;
2269cec47e3dSKiyoshi Ueda 
2270cec47e3dSKiyoshi Ueda 	if (noflush)
2271cec47e3dSKiyoshi Ueda 		stop_queue(q);
2272cec47e3dSKiyoshi Ueda 	else {
2273cec47e3dSKiyoshi Ueda 		blk_rq_init(q, rq);
2274cec47e3dSKiyoshi Ueda 		blk_insert_request(q, rq, 0, NULL);
2275cec47e3dSKiyoshi Ueda 	}
2276cec47e3dSKiyoshi Ueda }
2277cec47e3dSKiyoshi Ueda 
2278cec47e3dSKiyoshi Ueda static int dm_rq_suspend_available(struct mapped_device *md, int noflush)
2279cec47e3dSKiyoshi Ueda {
2280cec47e3dSKiyoshi Ueda 	int r = 1;
2281cec47e3dSKiyoshi Ueda 	struct request *rq = &md->suspend_rq;
2282cec47e3dSKiyoshi Ueda 	struct request_queue *q = md->queue;
2283cec47e3dSKiyoshi Ueda 	unsigned long flags;
2284cec47e3dSKiyoshi Ueda 
2285cec47e3dSKiyoshi Ueda 	if (noflush)
2286cec47e3dSKiyoshi Ueda 		return r;
2287cec47e3dSKiyoshi Ueda 
2288cec47e3dSKiyoshi Ueda 	/* The marker must be protected by queue lock if it is in use */
2289cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
2290cec47e3dSKiyoshi Ueda 	if (unlikely(rq->ref_count)) {
2291cec47e3dSKiyoshi Ueda 		/*
2292cec47e3dSKiyoshi Ueda 		 * This can happen, when the previous flush suspend was
2293cec47e3dSKiyoshi Ueda 		 * interrupted, the marker is still in the queue and
2294cec47e3dSKiyoshi Ueda 		 * this flush suspend has been invoked, because we don't
2295cec47e3dSKiyoshi Ueda 		 * remove the marker at the time of suspend interruption.
2296cec47e3dSKiyoshi Ueda 		 * We have only one marker per mapped_device, so we can't
2297cec47e3dSKiyoshi Ueda 		 * start another flush suspend while it is in use.
2298cec47e3dSKiyoshi Ueda 		 */
2299cec47e3dSKiyoshi Ueda 		BUG_ON(!rq->special); /* The marker should be invalidated */
2300cec47e3dSKiyoshi Ueda 		DMWARN("Invalidating the previous flush suspend is still in"
2301cec47e3dSKiyoshi Ueda 		       " progress.  Please retry later.");
2302cec47e3dSKiyoshi Ueda 		r = 0;
2303cec47e3dSKiyoshi Ueda 	}
2304cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
2305cec47e3dSKiyoshi Ueda 
2306cec47e3dSKiyoshi Ueda 	return r;
2307cec47e3dSKiyoshi Ueda }
2308cec47e3dSKiyoshi Ueda 
23091da177e4SLinus Torvalds /*
23101da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
23111da177e4SLinus Torvalds  * device.
23121da177e4SLinus Torvalds  */
23132ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
23141da177e4SLinus Torvalds {
2315e39e2e95SAlasdair G Kergon 	int r;
23161da177e4SLinus Torvalds 
23171da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2318dfbe03f6SAlasdair G Kergon 
2319db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2320dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2321cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2322e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2323e39e2e95SAlasdair G Kergon 		return r;
2324dfbe03f6SAlasdair G Kergon 	}
2325dfbe03f6SAlasdair G Kergon 
2326aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2327aa8d7c2fSAlasdair G Kergon 
23281da177e4SLinus Torvalds 	return 0;
23291da177e4SLinus Torvalds }
23301da177e4SLinus Torvalds 
23312ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
23321da177e4SLinus Torvalds {
2333aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2334aa8d7c2fSAlasdair G Kergon 		return;
2335aa8d7c2fSAlasdair G Kergon 
2336db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
23371da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2338aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
23391da177e4SLinus Torvalds }
23401da177e4SLinus Torvalds 
23411da177e4SLinus Torvalds /*
23421da177e4SLinus Torvalds  * We need to be able to change a mapping table under a mounted
23431da177e4SLinus Torvalds  * filesystem.  For example we might want to move some data in
23441da177e4SLinus Torvalds  * the background.  Before the table can be swapped with
23451da177e4SLinus Torvalds  * dm_bind_table, dm_suspend must be called to flush any in
23461da177e4SLinus Torvalds  * flight bios and ensure that any further io gets deferred.
23471da177e4SLinus Torvalds  */
2348cec47e3dSKiyoshi Ueda /*
2349cec47e3dSKiyoshi Ueda  * Suspend mechanism in request-based dm.
2350cec47e3dSKiyoshi Ueda  *
2351cec47e3dSKiyoshi Ueda  * After the suspend starts, further incoming requests are kept in
2352cec47e3dSKiyoshi Ueda  * the request_queue and deferred.
2353cec47e3dSKiyoshi Ueda  * Remaining requests in the request_queue at the start of suspend are flushed
2354cec47e3dSKiyoshi Ueda  * if it is flush suspend.
2355cec47e3dSKiyoshi Ueda  * The suspend completes when the following conditions have been satisfied,
2356cec47e3dSKiyoshi Ueda  * so wait for it:
2357cec47e3dSKiyoshi Ueda  *    1. q->in_flight is 0 (which means no in_flight request)
2358cec47e3dSKiyoshi Ueda  *    2. queue has been stopped (which means no request dispatching)
2359cec47e3dSKiyoshi Ueda  *
2360cec47e3dSKiyoshi Ueda  *
2361cec47e3dSKiyoshi Ueda  * Noflush suspend
2362cec47e3dSKiyoshi Ueda  * ---------------
2363cec47e3dSKiyoshi Ueda  * Noflush suspend doesn't need to dispatch remaining requests.
2364cec47e3dSKiyoshi Ueda  * So stop the queue immediately.  Then, wait for all in_flight requests
2365cec47e3dSKiyoshi Ueda  * to be completed or requeued.
2366cec47e3dSKiyoshi Ueda  *
2367cec47e3dSKiyoshi Ueda  * To abort noflush suspend, start the queue.
2368cec47e3dSKiyoshi Ueda  *
2369cec47e3dSKiyoshi Ueda  *
2370cec47e3dSKiyoshi Ueda  * Flush suspend
2371cec47e3dSKiyoshi Ueda  * -------------
2372cec47e3dSKiyoshi Ueda  * Flush suspend needs to dispatch remaining requests.  So stop the queue
2373cec47e3dSKiyoshi Ueda  * after the remaining requests are completed. (Requeued request must be also
2374cec47e3dSKiyoshi Ueda  * re-dispatched and completed.  Until then, we can't stop the queue.)
2375cec47e3dSKiyoshi Ueda  *
2376cec47e3dSKiyoshi Ueda  * During flushing the remaining requests, further incoming requests are also
2377cec47e3dSKiyoshi Ueda  * inserted to the same queue.  To distinguish which requests are to be
2378cec47e3dSKiyoshi Ueda  * flushed, we insert a marker request to the queue at the time of starting
2379cec47e3dSKiyoshi Ueda  * flush suspend, like a barrier.
2380cec47e3dSKiyoshi Ueda  * The dispatching is blocked when the marker is found on the top of the queue.
2381cec47e3dSKiyoshi Ueda  * And the queue is stopped when all in_flight requests are completed, since
2382cec47e3dSKiyoshi Ueda  * that means the remaining requests are completely flushed.
2383cec47e3dSKiyoshi Ueda  * Then, the marker is removed from the queue.
2384cec47e3dSKiyoshi Ueda  *
2385cec47e3dSKiyoshi Ueda  * To abort flush suspend, we also need to take care of the marker, not only
2386cec47e3dSKiyoshi Ueda  * starting the queue.
2387cec47e3dSKiyoshi Ueda  * We don't remove the marker forcibly from the queue since it's against
2388cec47e3dSKiyoshi Ueda  * the block-layer manner.  Instead, we put a invalidated mark on the marker.
2389cec47e3dSKiyoshi Ueda  * When the invalidated marker is found on the top of the queue, it is
2390cec47e3dSKiyoshi Ueda  * immediately removed from the queue, so it doesn't block dispatching.
2391cec47e3dSKiyoshi Ueda  * Because we have only one marker per mapped_device, we can't start another
2392cec47e3dSKiyoshi Ueda  * flush suspend until the invalidated marker is removed from the queue.
2393cec47e3dSKiyoshi Ueda  * So fail and return with -EBUSY in such a case.
2394cec47e3dSKiyoshi Ueda  */
2395a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
23961da177e4SLinus Torvalds {
23972ca3310eSAlasdair G Kergon 	struct dm_table *map = NULL;
239846125c1cSMilan Broz 	int r = 0;
2399a3d77d35SKiyoshi Ueda 	int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
24002e93ccc1SKiyoshi Ueda 	int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
24011da177e4SLinus Torvalds 
2402e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
24032ca3310eSAlasdair G Kergon 
240473d410c0SMilan Broz 	if (dm_suspended(md)) {
240573d410c0SMilan Broz 		r = -EINVAL;
2406d287483dSAlasdair G Kergon 		goto out_unlock;
240773d410c0SMilan Broz 	}
24081da177e4SLinus Torvalds 
2409cec47e3dSKiyoshi Ueda 	if (dm_request_based(md) && !dm_rq_suspend_available(md, noflush)) {
2410cec47e3dSKiyoshi Ueda 		r = -EBUSY;
2411cec47e3dSKiyoshi Ueda 		goto out_unlock;
2412cec47e3dSKiyoshi Ueda 	}
2413cec47e3dSKiyoshi Ueda 
24141da177e4SLinus Torvalds 	map = dm_get_table(md);
2415cf222b37SAlasdair G Kergon 
24162e93ccc1SKiyoshi Ueda 	/*
24172e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
24182e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
24192e93ccc1SKiyoshi Ueda 	 */
24202e93ccc1SKiyoshi Ueda 	if (noflush)
24212e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
24222e93ccc1SKiyoshi Ueda 
2423436d4108SAlasdair G Kergon 	/* This does not get reverted if there's an error later. */
24241da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
24251da177e4SLinus Torvalds 
24262e93ccc1SKiyoshi Ueda 	/*
24276d6f10dfSMilan Broz 	 * Flush I/O to the device. noflush supersedes do_lockfs,
24286d6f10dfSMilan Broz 	 * because lock_fs() needs to flush I/Os.
24292e93ccc1SKiyoshi Ueda 	 */
243032a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
24312ca3310eSAlasdair G Kergon 		r = lock_fs(md);
24322ca3310eSAlasdair G Kergon 		if (r)
24332ca3310eSAlasdair G Kergon 			goto out;
2434aa8d7c2fSAlasdair G Kergon 	}
24351da177e4SLinus Torvalds 
24361da177e4SLinus Torvalds 	/*
24373b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
24383b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
24393b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
24403b00b203SMikulas Patocka 	 * dm_wq_work.
24413b00b203SMikulas Patocka 	 *
24423b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
24433b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
24443b00b203SMikulas Patocka 	 * __split_and_process_bio from dm_request, we set
24453b00b203SMikulas Patocka 	 * DMF_QUEUE_IO_TO_THREAD.
24463b00b203SMikulas Patocka 	 *
24473b00b203SMikulas Patocka 	 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
24483b00b203SMikulas Patocka 	 * and call flush_workqueue(md->wq). flush_workqueue will wait until
24493b00b203SMikulas Patocka 	 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
24503b00b203SMikulas Patocka 	 * further calls to __split_and_process_bio from dm_wq_work.
24511da177e4SLinus Torvalds 	 */
24522ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
24531eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
24541eb787ecSAlasdair G Kergon 	set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
24552ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
24561da177e4SLinus Torvalds 
24573b00b203SMikulas Patocka 	flush_workqueue(md->wq);
24583b00b203SMikulas Patocka 
2459cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2460cec47e3dSKiyoshi Ueda 		dm_rq_start_suspend(md, noflush);
2461cec47e3dSKiyoshi Ueda 
24621da177e4SLinus Torvalds 	/*
24633b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
24643b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
24653b00b203SMikulas Patocka 	 * to finish.
24661da177e4SLinus Torvalds 	 */
2467401600dfSMikulas Patocka 	r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
24681da177e4SLinus Torvalds 
24692ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
24706d6f10dfSMilan Broz 	if (noflush)
2471022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
247294d6351eSMilan Broz 	up_write(&md->io_lock);
24732e93ccc1SKiyoshi Ueda 
24741da177e4SLinus Torvalds 	/* were we interrupted ? */
247546125c1cSMilan Broz 	if (r < 0) {
24769a1fb464SMikulas Patocka 		dm_queue_flush(md);
247773d410c0SMilan Broz 
2478cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
2479cec47e3dSKiyoshi Ueda 			dm_rq_abort_suspend(md, noflush);
2480cec47e3dSKiyoshi Ueda 
24812ca3310eSAlasdair G Kergon 		unlock_fs(md);
24822e93ccc1SKiyoshi Ueda 		goto out; /* pushback list is already flushed, so skip flush */
24832ca3310eSAlasdair G Kergon 	}
24842ca3310eSAlasdair G Kergon 
24853b00b203SMikulas Patocka 	/*
24863b00b203SMikulas Patocka 	 * If dm_wait_for_completion returned 0, the device is completely
24873b00b203SMikulas Patocka 	 * quiescent now. There is no request-processing activity. All new
24883b00b203SMikulas Patocka 	 * requests are being added to md->deferred list.
24893b00b203SMikulas Patocka 	 */
24903b00b203SMikulas Patocka 
24912ca3310eSAlasdair G Kergon 	dm_table_postsuspend_targets(map);
24921da177e4SLinus Torvalds 
24931da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
24941da177e4SLinus Torvalds 
24952ca3310eSAlasdair G Kergon out:
24961da177e4SLinus Torvalds 	dm_table_put(map);
2497d287483dSAlasdair G Kergon 
2498d287483dSAlasdair G Kergon out_unlock:
2499e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2500cf222b37SAlasdair G Kergon 	return r;
25011da177e4SLinus Torvalds }
25021da177e4SLinus Torvalds 
25031da177e4SLinus Torvalds int dm_resume(struct mapped_device *md)
25041da177e4SLinus Torvalds {
2505cf222b37SAlasdair G Kergon 	int r = -EINVAL;
2506cf222b37SAlasdair G Kergon 	struct dm_table *map = NULL;
25071da177e4SLinus Torvalds 
2508e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
25092ca3310eSAlasdair G Kergon 	if (!dm_suspended(md))
2510cf222b37SAlasdair G Kergon 		goto out;
2511cf222b37SAlasdair G Kergon 
2512cf222b37SAlasdair G Kergon 	map = dm_get_table(md);
25132ca3310eSAlasdair G Kergon 	if (!map || !dm_table_get_size(map))
2514cf222b37SAlasdair G Kergon 		goto out;
25151da177e4SLinus Torvalds 
25168757b776SMilan Broz 	r = dm_table_resume_targets(map);
25178757b776SMilan Broz 	if (r)
25188757b776SMilan Broz 		goto out;
25192ca3310eSAlasdair G Kergon 
25209a1fb464SMikulas Patocka 	dm_queue_flush(md);
25212ca3310eSAlasdair G Kergon 
2522cec47e3dSKiyoshi Ueda 	/*
2523cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2524cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2525cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2526cec47e3dSKiyoshi Ueda 	 */
2527cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2528cec47e3dSKiyoshi Ueda 		start_queue(md->queue);
2529cec47e3dSKiyoshi Ueda 
25302ca3310eSAlasdair G Kergon 	unlock_fs(md);
25312ca3310eSAlasdair G Kergon 
25322ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
25332ca3310eSAlasdair G Kergon 
25341da177e4SLinus Torvalds 	dm_table_unplug_all(map);
2535cf222b37SAlasdair G Kergon 	r = 0;
2536cf222b37SAlasdair G Kergon out:
2537cf222b37SAlasdair G Kergon 	dm_table_put(map);
2538e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
25392ca3310eSAlasdair G Kergon 
2540cf222b37SAlasdair G Kergon 	return r;
25411da177e4SLinus Torvalds }
25421da177e4SLinus Torvalds 
25431da177e4SLinus Torvalds /*-----------------------------------------------------------------
25441da177e4SLinus Torvalds  * Event notification.
25451da177e4SLinus Torvalds  *---------------------------------------------------------------*/
254660935eb2SMilan Broz void dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
254760935eb2SMilan Broz 		       unsigned cookie)
254869267a30SAlasdair G Kergon {
254960935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
255060935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
255160935eb2SMilan Broz 
255260935eb2SMilan Broz 	if (!cookie)
255360935eb2SMilan Broz 		kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
255460935eb2SMilan Broz 	else {
255560935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
255660935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
255760935eb2SMilan Broz 		kobject_uevent_env(&disk_to_dev(md->disk)->kobj, action, envp);
255860935eb2SMilan Broz 	}
255969267a30SAlasdair G Kergon }
256069267a30SAlasdair G Kergon 
25617a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
25627a8c3d3bSMike Anderson {
25637a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
25647a8c3d3bSMike Anderson }
25657a8c3d3bSMike Anderson 
25661da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
25671da177e4SLinus Torvalds {
25681da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
25691da177e4SLinus Torvalds }
25701da177e4SLinus Torvalds 
25711da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
25721da177e4SLinus Torvalds {
25731da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
25741da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
25751da177e4SLinus Torvalds }
25761da177e4SLinus Torvalds 
25777a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
25787a8c3d3bSMike Anderson {
25797a8c3d3bSMike Anderson 	unsigned long flags;
25807a8c3d3bSMike Anderson 
25817a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
25827a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
25837a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
25847a8c3d3bSMike Anderson }
25857a8c3d3bSMike Anderson 
25861da177e4SLinus Torvalds /*
25871da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
25881da177e4SLinus Torvalds  * count on 'md'.
25891da177e4SLinus Torvalds  */
25901da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
25911da177e4SLinus Torvalds {
25921da177e4SLinus Torvalds 	return md->disk;
25931da177e4SLinus Torvalds }
25941da177e4SLinus Torvalds 
2595784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2596784aae73SMilan Broz {
2597784aae73SMilan Broz 	return &md->kobj;
2598784aae73SMilan Broz }
2599784aae73SMilan Broz 
2600784aae73SMilan Broz /*
2601784aae73SMilan Broz  * struct mapped_device should not be exported outside of dm.c
2602784aae73SMilan Broz  * so use this check to verify that kobj is part of md structure
2603784aae73SMilan Broz  */
2604784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2605784aae73SMilan Broz {
2606784aae73SMilan Broz 	struct mapped_device *md;
2607784aae73SMilan Broz 
2608784aae73SMilan Broz 	md = container_of(kobj, struct mapped_device, kobj);
2609784aae73SMilan Broz 	if (&md->kobj != kobj)
2610784aae73SMilan Broz 		return NULL;
2611784aae73SMilan Broz 
26124d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
26134d89b7b4SMilan Broz 	    test_bit(DMF_DELETING, &md->flags))
26144d89b7b4SMilan Broz 		return NULL;
26154d89b7b4SMilan Broz 
2616784aae73SMilan Broz 	dm_get(md);
2617784aae73SMilan Broz 	return md;
2618784aae73SMilan Broz }
2619784aae73SMilan Broz 
26201da177e4SLinus Torvalds int dm_suspended(struct mapped_device *md)
26211da177e4SLinus Torvalds {
26221da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
26231da177e4SLinus Torvalds }
26241da177e4SLinus Torvalds 
26252e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
26262e93ccc1SKiyoshi Ueda {
26272e93ccc1SKiyoshi Ueda 	struct mapped_device *md = dm_table_get_md(ti->table);
26282e93ccc1SKiyoshi Ueda 	int r = __noflush_suspending(md);
26292e93ccc1SKiyoshi Ueda 
26302e93ccc1SKiyoshi Ueda 	dm_put(md);
26312e93ccc1SKiyoshi Ueda 
26322e93ccc1SKiyoshi Ueda 	return r;
26332e93ccc1SKiyoshi Ueda }
26342e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
26352e93ccc1SKiyoshi Ueda 
2636e6ee8c0bSKiyoshi Ueda struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2637e6ee8c0bSKiyoshi Ueda {
2638e6ee8c0bSKiyoshi Ueda 	struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2639e6ee8c0bSKiyoshi Ueda 
2640e6ee8c0bSKiyoshi Ueda 	if (!pools)
2641e6ee8c0bSKiyoshi Ueda 		return NULL;
2642e6ee8c0bSKiyoshi Ueda 
2643e6ee8c0bSKiyoshi Ueda 	pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2644e6ee8c0bSKiyoshi Ueda 			 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2645e6ee8c0bSKiyoshi Ueda 			 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2646e6ee8c0bSKiyoshi Ueda 	if (!pools->io_pool)
2647e6ee8c0bSKiyoshi Ueda 		goto free_pools_and_out;
2648e6ee8c0bSKiyoshi Ueda 
2649e6ee8c0bSKiyoshi Ueda 	pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2650e6ee8c0bSKiyoshi Ueda 			  mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2651e6ee8c0bSKiyoshi Ueda 			  mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2652e6ee8c0bSKiyoshi Ueda 	if (!pools->tio_pool)
2653e6ee8c0bSKiyoshi Ueda 		goto free_io_pool_and_out;
2654e6ee8c0bSKiyoshi Ueda 
2655e6ee8c0bSKiyoshi Ueda 	pools->bs = (type == DM_TYPE_BIO_BASED) ?
2656e6ee8c0bSKiyoshi Ueda 		    bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2657e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
2658e6ee8c0bSKiyoshi Ueda 		goto free_tio_pool_and_out;
2659e6ee8c0bSKiyoshi Ueda 
2660e6ee8c0bSKiyoshi Ueda 	return pools;
2661e6ee8c0bSKiyoshi Ueda 
2662e6ee8c0bSKiyoshi Ueda free_tio_pool_and_out:
2663e6ee8c0bSKiyoshi Ueda 	mempool_destroy(pools->tio_pool);
2664e6ee8c0bSKiyoshi Ueda 
2665e6ee8c0bSKiyoshi Ueda free_io_pool_and_out:
2666e6ee8c0bSKiyoshi Ueda 	mempool_destroy(pools->io_pool);
2667e6ee8c0bSKiyoshi Ueda 
2668e6ee8c0bSKiyoshi Ueda free_pools_and_out:
2669e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2670e6ee8c0bSKiyoshi Ueda 
2671e6ee8c0bSKiyoshi Ueda 	return NULL;
2672e6ee8c0bSKiyoshi Ueda }
2673e6ee8c0bSKiyoshi Ueda 
2674e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
2675e6ee8c0bSKiyoshi Ueda {
2676e6ee8c0bSKiyoshi Ueda 	if (!pools)
2677e6ee8c0bSKiyoshi Ueda 		return;
2678e6ee8c0bSKiyoshi Ueda 
2679e6ee8c0bSKiyoshi Ueda 	if (pools->io_pool)
2680e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->io_pool);
2681e6ee8c0bSKiyoshi Ueda 
2682e6ee8c0bSKiyoshi Ueda 	if (pools->tio_pool)
2683e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->tio_pool);
2684e6ee8c0bSKiyoshi Ueda 
2685e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
2686e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
2687e6ee8c0bSKiyoshi Ueda 
2688e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2689e6ee8c0bSKiyoshi Ueda }
2690e6ee8c0bSKiyoshi Ueda 
269183d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
26921da177e4SLinus Torvalds 	.open = dm_blk_open,
26931da177e4SLinus Torvalds 	.release = dm_blk_close,
2694aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
26953ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
26961da177e4SLinus Torvalds 	.owner = THIS_MODULE
26971da177e4SLinus Torvalds };
26981da177e4SLinus Torvalds 
26991da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo);
27001da177e4SLinus Torvalds 
27011da177e4SLinus Torvalds /*
27021da177e4SLinus Torvalds  * module hooks
27031da177e4SLinus Torvalds  */
27041da177e4SLinus Torvalds module_init(dm_init);
27051da177e4SLinus Torvalds module_exit(dm_exit);
27061da177e4SLinus Torvalds 
27071da177e4SLinus Torvalds module_param(major, uint, 0);
27081da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
27091da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
27101da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
27111da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2712