xref: /openbmc/linux/drivers/md/dm.c (revision f88fb981183e71daf40bbd84bc8251bbf7b59e19)
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;
50*f88fb981SKiyoshi 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,
278945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2791da177e4SLinus Torvalds 	dm_interface_init,
2801da177e4SLinus Torvalds };
2811da177e4SLinus Torvalds 
282b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2831da177e4SLinus Torvalds 	local_exit,
2841da177e4SLinus Torvalds 	dm_target_exit,
2851da177e4SLinus Torvalds 	dm_linear_exit,
2861da177e4SLinus Torvalds 	dm_stripe_exit,
287945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
2881da177e4SLinus Torvalds 	dm_interface_exit,
2891da177e4SLinus Torvalds };
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds static int __init dm_init(void)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	int r, i;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
2981da177e4SLinus Torvalds 		r = _inits[i]();
2991da177e4SLinus Torvalds 		if (r)
3001da177e4SLinus Torvalds 			goto bad;
3011da177e4SLinus Torvalds 	}
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	return 0;
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds       bad:
3061da177e4SLinus Torvalds 	while (i--)
3071da177e4SLinus Torvalds 		_exits[i]();
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	return r;
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds static void __exit dm_exit(void)
3131da177e4SLinus Torvalds {
3141da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	while (i--)
3171da177e4SLinus Torvalds 		_exits[i]();
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds /*
3211da177e4SLinus Torvalds  * Block device functions
3221da177e4SLinus Torvalds  */
323fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3241da177e4SLinus Torvalds {
3251da177e4SLinus Torvalds 	struct mapped_device *md;
3261da177e4SLinus Torvalds 
327fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
328fba9f90eSJeff Mahoney 
329fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
330fba9f90eSJeff Mahoney 	if (!md)
331fba9f90eSJeff Mahoney 		goto out;
332fba9f90eSJeff Mahoney 
3335c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
3345c6bd75dSAlasdair G Kergon 	    test_bit(DMF_DELETING, &md->flags)) {
335fba9f90eSJeff Mahoney 		md = NULL;
336fba9f90eSJeff Mahoney 		goto out;
337fba9f90eSJeff Mahoney 	}
338fba9f90eSJeff Mahoney 
3391da177e4SLinus Torvalds 	dm_get(md);
3405c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
341fba9f90eSJeff Mahoney 
342fba9f90eSJeff Mahoney out:
343fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
344fba9f90eSJeff Mahoney 
345fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds 
348fe5f9f2cSAl Viro static int dm_blk_close(struct gendisk *disk, fmode_t mode)
3491da177e4SLinus Torvalds {
350fe5f9f2cSAl Viro 	struct mapped_device *md = disk->private_data;
3515c6bd75dSAlasdair G Kergon 	atomic_dec(&md->open_count);
3521da177e4SLinus Torvalds 	dm_put(md);
3531da177e4SLinus Torvalds 	return 0;
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds 
3565c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3575c6bd75dSAlasdair G Kergon {
3585c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3595c6bd75dSAlasdair G Kergon }
3605c6bd75dSAlasdair G Kergon 
3615c6bd75dSAlasdair G Kergon /*
3625c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3635c6bd75dSAlasdair G Kergon  */
3645c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md)
3655c6bd75dSAlasdair G Kergon {
3665c6bd75dSAlasdair G Kergon 	int r = 0;
3675c6bd75dSAlasdair G Kergon 
3685c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
3695c6bd75dSAlasdair G Kergon 
3705c6bd75dSAlasdair G Kergon 	if (dm_open_count(md))
3715c6bd75dSAlasdair G Kergon 		r = -EBUSY;
3725c6bd75dSAlasdair G Kergon 	else
3735c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
3745c6bd75dSAlasdair G Kergon 
3755c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
3765c6bd75dSAlasdair G Kergon 
3775c6bd75dSAlasdair G Kergon 	return r;
3785c6bd75dSAlasdair G Kergon }
3795c6bd75dSAlasdair G Kergon 
3803ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3813ac51e74SDarrick J. Wong {
3823ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
3833ac51e74SDarrick J. Wong 
3843ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
3853ac51e74SDarrick J. Wong }
3863ac51e74SDarrick J. Wong 
387fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
388aa129a22SMilan Broz 			unsigned int cmd, unsigned long arg)
389aa129a22SMilan Broz {
390fe5f9f2cSAl Viro 	struct mapped_device *md = bdev->bd_disk->private_data;
391fe5f9f2cSAl Viro 	struct dm_table *map = dm_get_table(md);
392aa129a22SMilan Broz 	struct dm_target *tgt;
393aa129a22SMilan Broz 	int r = -ENOTTY;
394aa129a22SMilan Broz 
395aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
396aa129a22SMilan Broz 		goto out;
397aa129a22SMilan Broz 
398aa129a22SMilan Broz 	/* We only support devices that have a single target */
399aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
400aa129a22SMilan Broz 		goto out;
401aa129a22SMilan Broz 
402aa129a22SMilan Broz 	tgt = dm_table_get_target(map, 0);
403aa129a22SMilan Broz 
404aa129a22SMilan Broz 	if (dm_suspended(md)) {
405aa129a22SMilan Broz 		r = -EAGAIN;
406aa129a22SMilan Broz 		goto out;
407aa129a22SMilan Broz 	}
408aa129a22SMilan Broz 
409aa129a22SMilan Broz 	if (tgt->type->ioctl)
410647b3d00SAl Viro 		r = tgt->type->ioctl(tgt, cmd, arg);
411aa129a22SMilan Broz 
412aa129a22SMilan Broz out:
413aa129a22SMilan Broz 	dm_table_put(map);
414aa129a22SMilan Broz 
415aa129a22SMilan Broz 	return r;
416aa129a22SMilan Broz }
417aa129a22SMilan Broz 
418028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
4191da177e4SLinus Torvalds {
4201da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
423028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
4241da177e4SLinus Torvalds {
4251da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
428028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
4291da177e4SLinus Torvalds {
4301da177e4SLinus Torvalds 	mempool_free(tio, md->tio_pool);
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds 
433cec47e3dSKiyoshi Ueda static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md)
434cec47e3dSKiyoshi Ueda {
435cec47e3dSKiyoshi Ueda 	return mempool_alloc(md->tio_pool, GFP_ATOMIC);
436cec47e3dSKiyoshi Ueda }
437cec47e3dSKiyoshi Ueda 
438cec47e3dSKiyoshi Ueda static void free_rq_tio(struct dm_rq_target_io *tio)
439cec47e3dSKiyoshi Ueda {
440cec47e3dSKiyoshi Ueda 	mempool_free(tio, tio->md->tio_pool);
441cec47e3dSKiyoshi Ueda }
442cec47e3dSKiyoshi Ueda 
443cec47e3dSKiyoshi Ueda static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
444cec47e3dSKiyoshi Ueda {
445cec47e3dSKiyoshi Ueda 	return mempool_alloc(md->io_pool, GFP_ATOMIC);
446cec47e3dSKiyoshi Ueda }
447cec47e3dSKiyoshi Ueda 
448cec47e3dSKiyoshi Ueda static void free_bio_info(struct dm_rq_clone_bio_info *info)
449cec47e3dSKiyoshi Ueda {
450cec47e3dSKiyoshi Ueda 	mempool_free(info, info->tio->md->io_pool);
451cec47e3dSKiyoshi Ueda }
452cec47e3dSKiyoshi Ueda 
4533eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
4543eaf840eSJun'ichi "Nick" Nomura {
4553eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
456c9959059STejun Heo 	int cpu;
457316d315bSNikanth Karthikesan 	int rw = bio_data_dir(io->bio);
4583eaf840eSJun'ichi "Nick" Nomura 
4593eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
4603eaf840eSJun'ichi "Nick" Nomura 
461074a7acaSTejun Heo 	cpu = part_stat_lock();
462074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
463074a7acaSTejun Heo 	part_stat_unlock();
464316d315bSNikanth Karthikesan 	dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
4653eaf840eSJun'ichi "Nick" Nomura }
4663eaf840eSJun'ichi "Nick" Nomura 
467d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
4683eaf840eSJun'ichi "Nick" Nomura {
4693eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
4703eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
4713eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
472c9959059STejun Heo 	int pending, cpu;
4733eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
4743eaf840eSJun'ichi "Nick" Nomura 
475074a7acaSTejun Heo 	cpu = part_stat_lock();
476074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
477074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
478074a7acaSTejun Heo 	part_stat_unlock();
4793eaf840eSJun'ichi "Nick" Nomura 
480af7e466aSMikulas Patocka 	/*
481af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
482af7e466aSMikulas Patocka 	 * a barrier.
483af7e466aSMikulas Patocka 	 */
484316d315bSNikanth Karthikesan 	dm_disk(md)->part0.in_flight[rw] = pending =
485316d315bSNikanth Karthikesan 		atomic_dec_return(&md->pending[rw]);
486316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
4873eaf840eSJun'ichi "Nick" Nomura 
488d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
489d221d2e7SMikulas Patocka 	if (!pending)
490d221d2e7SMikulas Patocka 		wake_up(&md->wait);
4913eaf840eSJun'ichi "Nick" Nomura }
4923eaf840eSJun'ichi "Nick" Nomura 
4931da177e4SLinus Torvalds /*
4941da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
4951da177e4SLinus Torvalds  */
49692c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
4971da177e4SLinus Torvalds {
4982ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
4991da177e4SLinus Torvalds 
500022c2611SMikulas Patocka 	spin_lock_irq(&md->deferred_lock);
5011da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
502022c2611SMikulas Patocka 	spin_unlock_irq(&md->deferred_lock);
5031da177e4SLinus Torvalds 
50492c63902SMikulas Patocka 	if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
50592c63902SMikulas Patocka 		queue_work(md->wq, &md->work);
50692c63902SMikulas Patocka 
5072ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds /*
5111da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
5121da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
5131da177e4SLinus Torvalds  * dm_table_put() when finished.
5141da177e4SLinus Torvalds  */
5151da177e4SLinus Torvalds struct dm_table *dm_get_table(struct mapped_device *md)
5161da177e4SLinus Torvalds {
5171da177e4SLinus Torvalds 	struct dm_table *t;
518523d9297SKiyoshi Ueda 	unsigned long flags;
5191da177e4SLinus Torvalds 
520523d9297SKiyoshi Ueda 	read_lock_irqsave(&md->map_lock, flags);
5211da177e4SLinus Torvalds 	t = md->map;
5221da177e4SLinus Torvalds 	if (t)
5231da177e4SLinus Torvalds 		dm_table_get(t);
524523d9297SKiyoshi Ueda 	read_unlock_irqrestore(&md->map_lock, flags);
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	return t;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
5293ac51e74SDarrick J. Wong /*
5303ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
5313ac51e74SDarrick J. Wong  */
5323ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
5333ac51e74SDarrick J. Wong {
5343ac51e74SDarrick J. Wong 	*geo = md->geometry;
5353ac51e74SDarrick J. Wong 
5363ac51e74SDarrick J. Wong 	return 0;
5373ac51e74SDarrick J. Wong }
5383ac51e74SDarrick J. Wong 
5393ac51e74SDarrick J. Wong /*
5403ac51e74SDarrick J. Wong  * Set the geometry of a device.
5413ac51e74SDarrick J. Wong  */
5423ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
5433ac51e74SDarrick J. Wong {
5443ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
5453ac51e74SDarrick J. Wong 
5463ac51e74SDarrick J. Wong 	if (geo->start > sz) {
5473ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
5483ac51e74SDarrick J. Wong 		return -EINVAL;
5493ac51e74SDarrick J. Wong 	}
5503ac51e74SDarrick J. Wong 
5513ac51e74SDarrick J. Wong 	md->geometry = *geo;
5523ac51e74SDarrick J. Wong 
5533ac51e74SDarrick J. Wong 	return 0;
5543ac51e74SDarrick J. Wong }
5553ac51e74SDarrick J. Wong 
5561da177e4SLinus Torvalds /*-----------------------------------------------------------------
5571da177e4SLinus Torvalds  * CRUD START:
5581da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
5591da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
5601da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
5611da177e4SLinus Torvalds  *   interests of getting something for people to use I give
5621da177e4SLinus Torvalds  *   you this clearly demarcated crap.
5631da177e4SLinus Torvalds  *---------------------------------------------------------------*/
5641da177e4SLinus Torvalds 
5652e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
5662e93ccc1SKiyoshi Ueda {
5672e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
5682e93ccc1SKiyoshi Ueda }
5692e93ccc1SKiyoshi Ueda 
5701da177e4SLinus Torvalds /*
5711da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
5721da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
5731da177e4SLinus Torvalds  */
574858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
5751da177e4SLinus Torvalds {
5762e93ccc1SKiyoshi Ueda 	unsigned long flags;
577b35f8caaSMilan Broz 	int io_error;
578b35f8caaSMilan Broz 	struct bio *bio;
579b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
5802e93ccc1SKiyoshi Ueda 
5812e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
582*f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
583*f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
584*f88fb981SKiyoshi Ueda 		if (!(io->error > 0 && __noflush_suspending(md)))
5851da177e4SLinus Torvalds 			io->error = error;
586*f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
587*f88fb981SKiyoshi Ueda 	}
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
5902e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
5912e93ccc1SKiyoshi Ueda 			/*
5922e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
5932e93ccc1SKiyoshi Ueda 			 */
594022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
5952761e95fSMikulas Patocka 			if (__noflush_suspending(md)) {
5961f98a13fSJens Axboe 				if (!bio_rw_flagged(io->bio, BIO_RW_BARRIER))
5972761e95fSMikulas Patocka 					bio_list_add_head(&md->deferred,
5982761e95fSMikulas Patocka 							  io->bio);
5992761e95fSMikulas Patocka 			} else
6002e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
6012e93ccc1SKiyoshi Ueda 				io->error = -EIO;
602022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
6032e93ccc1SKiyoshi Ueda 		}
6042e93ccc1SKiyoshi Ueda 
605b35f8caaSMilan Broz 		io_error = io->error;
606b35f8caaSMilan Broz 		bio = io->bio;
6072056a782SJens Axboe 
6081f98a13fSJens Axboe 		if (bio_rw_flagged(bio, BIO_RW_BARRIER)) {
609af7e466aSMikulas Patocka 			/*
610af7e466aSMikulas Patocka 			 * There can be just one barrier request so we use
611af7e466aSMikulas Patocka 			 * a per-device variable for error reporting.
612af7e466aSMikulas Patocka 			 * Note that you can't touch the bio after end_io_acct
613af7e466aSMikulas Patocka 			 */
614fdb9572bSMikulas Patocka 			if (!md->barrier_error && io_error != -EOPNOTSUPP)
615af7e466aSMikulas Patocka 				md->barrier_error = io_error;
616af7e466aSMikulas Patocka 			end_io_acct(io);
617af7e466aSMikulas Patocka 		} else {
618af7e466aSMikulas Patocka 			end_io_acct(io);
619b35f8caaSMilan Broz 
620b35f8caaSMilan Broz 			if (io_error != DM_ENDIO_REQUEUE) {
621b35f8caaSMilan Broz 				trace_block_bio_complete(md->queue, bio);
622b35f8caaSMilan Broz 
623b35f8caaSMilan Broz 				bio_endio(bio, io_error);
6242e93ccc1SKiyoshi Ueda 			}
6251da177e4SLinus Torvalds 		}
626af7e466aSMikulas Patocka 
627af7e466aSMikulas Patocka 		free_io(md, io);
628af7e466aSMikulas Patocka 	}
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds 
6316712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error)
6321da177e4SLinus Torvalds {
6331da177e4SLinus Torvalds 	int r = 0;
634028867acSAlasdair G Kergon 	struct dm_target_io *tio = bio->bi_private;
635b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
6369faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
6371da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	if (!bio_flagged(bio, BIO_UPTODATE) && !error)
6401da177e4SLinus Torvalds 		error = -EIO;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	if (endio) {
6431da177e4SLinus Torvalds 		r = endio(tio->ti, bio, error, &tio->info);
6442e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
6452e93ccc1SKiyoshi Ueda 			/*
6462e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
6472e93ccc1SKiyoshi Ueda 			 * in dec_pending().
6482e93ccc1SKiyoshi Ueda 			 */
6491da177e4SLinus Torvalds 			error = r;
65045cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
65145cbcd79SKiyoshi Ueda 			/* The target will handle the io */
6526712ecf8SNeilBrown 			return;
65345cbcd79SKiyoshi Ueda 		else if (r) {
65445cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
65545cbcd79SKiyoshi Ueda 			BUG();
65645cbcd79SKiyoshi Ueda 		}
6571da177e4SLinus Torvalds 	}
6581da177e4SLinus Torvalds 
6599faf400fSStefan Bader 	/*
6609faf400fSStefan Bader 	 * Store md for cleanup instead of tio which is about to get freed.
6619faf400fSStefan Bader 	 */
6629faf400fSStefan Bader 	bio->bi_private = md->bs;
6639faf400fSStefan Bader 
6649faf400fSStefan Bader 	free_tio(md, tio);
665b35f8caaSMilan Broz 	bio_put(bio);
666b35f8caaSMilan Broz 	dec_pending(io, error);
6671da177e4SLinus Torvalds }
6681da177e4SLinus Torvalds 
669cec47e3dSKiyoshi Ueda /*
670cec47e3dSKiyoshi Ueda  * Partial completion handling for request-based dm
671cec47e3dSKiyoshi Ueda  */
672cec47e3dSKiyoshi Ueda static void end_clone_bio(struct bio *clone, int error)
673cec47e3dSKiyoshi Ueda {
674cec47e3dSKiyoshi Ueda 	struct dm_rq_clone_bio_info *info = clone->bi_private;
675cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = info->tio;
676cec47e3dSKiyoshi Ueda 	struct bio *bio = info->orig;
677cec47e3dSKiyoshi Ueda 	unsigned int nr_bytes = info->orig->bi_size;
678cec47e3dSKiyoshi Ueda 
679cec47e3dSKiyoshi Ueda 	bio_put(clone);
680cec47e3dSKiyoshi Ueda 
681cec47e3dSKiyoshi Ueda 	if (tio->error)
682cec47e3dSKiyoshi Ueda 		/*
683cec47e3dSKiyoshi Ueda 		 * An error has already been detected on the request.
684cec47e3dSKiyoshi Ueda 		 * Once error occurred, just let clone->end_io() handle
685cec47e3dSKiyoshi Ueda 		 * the remainder.
686cec47e3dSKiyoshi Ueda 		 */
687cec47e3dSKiyoshi Ueda 		return;
688cec47e3dSKiyoshi Ueda 	else if (error) {
689cec47e3dSKiyoshi Ueda 		/*
690cec47e3dSKiyoshi Ueda 		 * Don't notice the error to the upper layer yet.
691cec47e3dSKiyoshi Ueda 		 * The error handling decision is made by the target driver,
692cec47e3dSKiyoshi Ueda 		 * when the request is completed.
693cec47e3dSKiyoshi Ueda 		 */
694cec47e3dSKiyoshi Ueda 		tio->error = error;
695cec47e3dSKiyoshi Ueda 		return;
696cec47e3dSKiyoshi Ueda 	}
697cec47e3dSKiyoshi Ueda 
698cec47e3dSKiyoshi Ueda 	/*
699cec47e3dSKiyoshi Ueda 	 * I/O for the bio successfully completed.
700cec47e3dSKiyoshi Ueda 	 * Notice the data completion to the upper layer.
701cec47e3dSKiyoshi Ueda 	 */
702cec47e3dSKiyoshi Ueda 
703cec47e3dSKiyoshi Ueda 	/*
704cec47e3dSKiyoshi Ueda 	 * bios are processed from the head of the list.
705cec47e3dSKiyoshi Ueda 	 * So the completing bio should always be rq->bio.
706cec47e3dSKiyoshi Ueda 	 * If it's not, something wrong is happening.
707cec47e3dSKiyoshi Ueda 	 */
708cec47e3dSKiyoshi Ueda 	if (tio->orig->bio != bio)
709cec47e3dSKiyoshi Ueda 		DMERR("bio completion is going in the middle of the request");
710cec47e3dSKiyoshi Ueda 
711cec47e3dSKiyoshi Ueda 	/*
712cec47e3dSKiyoshi Ueda 	 * Update the original request.
713cec47e3dSKiyoshi Ueda 	 * Do not use blk_end_request() here, because it may complete
714cec47e3dSKiyoshi Ueda 	 * the original request before the clone, and break the ordering.
715cec47e3dSKiyoshi Ueda 	 */
716cec47e3dSKiyoshi Ueda 	blk_update_request(tio->orig, 0, nr_bytes);
717cec47e3dSKiyoshi Ueda }
718cec47e3dSKiyoshi Ueda 
719cec47e3dSKiyoshi Ueda /*
720cec47e3dSKiyoshi Ueda  * Don't touch any member of the md after calling this function because
721cec47e3dSKiyoshi Ueda  * the md may be freed in dm_put() at the end of this function.
722cec47e3dSKiyoshi Ueda  * Or do dm_get() before calling this function and dm_put() later.
723cec47e3dSKiyoshi Ueda  */
724cec47e3dSKiyoshi Ueda static void rq_completed(struct mapped_device *md, int run_queue)
725cec47e3dSKiyoshi Ueda {
726cec47e3dSKiyoshi Ueda 	int wakeup_waiters = 0;
727cec47e3dSKiyoshi Ueda 	struct request_queue *q = md->queue;
728cec47e3dSKiyoshi Ueda 	unsigned long flags;
729cec47e3dSKiyoshi Ueda 
730cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
731cec47e3dSKiyoshi Ueda 	if (!queue_in_flight(q))
732cec47e3dSKiyoshi Ueda 		wakeup_waiters = 1;
733cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
734cec47e3dSKiyoshi Ueda 
735cec47e3dSKiyoshi Ueda 	/* nudge anyone waiting on suspend queue */
736cec47e3dSKiyoshi Ueda 	if (wakeup_waiters)
737cec47e3dSKiyoshi Ueda 		wake_up(&md->wait);
738cec47e3dSKiyoshi Ueda 
739cec47e3dSKiyoshi Ueda 	if (run_queue)
740cec47e3dSKiyoshi Ueda 		blk_run_queue(q);
741cec47e3dSKiyoshi Ueda 
742cec47e3dSKiyoshi Ueda 	/*
743cec47e3dSKiyoshi Ueda 	 * dm_put() must be at the end of this function. See the comment above
744cec47e3dSKiyoshi Ueda 	 */
745cec47e3dSKiyoshi Ueda 	dm_put(md);
746cec47e3dSKiyoshi Ueda }
747cec47e3dSKiyoshi Ueda 
748a77e28c7SKiyoshi Ueda static void free_rq_clone(struct request *clone)
749a77e28c7SKiyoshi Ueda {
750a77e28c7SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
751a77e28c7SKiyoshi Ueda 
752a77e28c7SKiyoshi Ueda 	blk_rq_unprep_clone(clone);
753a77e28c7SKiyoshi Ueda 	free_rq_tio(tio);
754a77e28c7SKiyoshi Ueda }
755a77e28c7SKiyoshi Ueda 
756cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq)
757cec47e3dSKiyoshi Ueda {
758cec47e3dSKiyoshi Ueda 	struct request *clone = rq->special;
759cec47e3dSKiyoshi Ueda 
760cec47e3dSKiyoshi Ueda 	rq->special = NULL;
761cec47e3dSKiyoshi Ueda 	rq->cmd_flags &= ~REQ_DONTPREP;
762cec47e3dSKiyoshi Ueda 
763a77e28c7SKiyoshi Ueda 	free_rq_clone(clone);
764cec47e3dSKiyoshi Ueda }
765cec47e3dSKiyoshi Ueda 
766cec47e3dSKiyoshi Ueda /*
767cec47e3dSKiyoshi Ueda  * Requeue the original request of a clone.
768cec47e3dSKiyoshi Ueda  */
769cec47e3dSKiyoshi Ueda void dm_requeue_unmapped_request(struct request *clone)
770cec47e3dSKiyoshi Ueda {
771cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
772cec47e3dSKiyoshi Ueda 	struct mapped_device *md = tio->md;
773cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
774cec47e3dSKiyoshi Ueda 	struct request_queue *q = rq->q;
775cec47e3dSKiyoshi Ueda 	unsigned long flags;
776cec47e3dSKiyoshi Ueda 
777cec47e3dSKiyoshi Ueda 	dm_unprep_request(rq);
778cec47e3dSKiyoshi Ueda 
779cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
780cec47e3dSKiyoshi Ueda 	if (elv_queue_empty(q))
781cec47e3dSKiyoshi Ueda 		blk_plug_device(q);
782cec47e3dSKiyoshi Ueda 	blk_requeue_request(q, rq);
783cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
784cec47e3dSKiyoshi Ueda 
785cec47e3dSKiyoshi Ueda 	rq_completed(md, 0);
786cec47e3dSKiyoshi Ueda }
787cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
788cec47e3dSKiyoshi Ueda 
789cec47e3dSKiyoshi Ueda static void __stop_queue(struct request_queue *q)
790cec47e3dSKiyoshi Ueda {
791cec47e3dSKiyoshi Ueda 	blk_stop_queue(q);
792cec47e3dSKiyoshi Ueda }
793cec47e3dSKiyoshi Ueda 
794cec47e3dSKiyoshi Ueda static void stop_queue(struct request_queue *q)
795cec47e3dSKiyoshi Ueda {
796cec47e3dSKiyoshi Ueda 	unsigned long flags;
797cec47e3dSKiyoshi Ueda 
798cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
799cec47e3dSKiyoshi Ueda 	__stop_queue(q);
800cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
801cec47e3dSKiyoshi Ueda }
802cec47e3dSKiyoshi Ueda 
803cec47e3dSKiyoshi Ueda static void __start_queue(struct request_queue *q)
804cec47e3dSKiyoshi Ueda {
805cec47e3dSKiyoshi Ueda 	if (blk_queue_stopped(q))
806cec47e3dSKiyoshi Ueda 		blk_start_queue(q);
807cec47e3dSKiyoshi Ueda }
808cec47e3dSKiyoshi Ueda 
809cec47e3dSKiyoshi Ueda static void start_queue(struct request_queue *q)
810cec47e3dSKiyoshi Ueda {
811cec47e3dSKiyoshi Ueda 	unsigned long flags;
812cec47e3dSKiyoshi Ueda 
813cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
814cec47e3dSKiyoshi Ueda 	__start_queue(q);
815cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
816cec47e3dSKiyoshi Ueda }
817cec47e3dSKiyoshi Ueda 
818cec47e3dSKiyoshi Ueda /*
819cec47e3dSKiyoshi Ueda  * Complete the clone and the original request.
820cec47e3dSKiyoshi Ueda  * Must be called without queue lock.
821cec47e3dSKiyoshi Ueda  */
822cec47e3dSKiyoshi Ueda static void dm_end_request(struct request *clone, int error)
823cec47e3dSKiyoshi Ueda {
824cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
825cec47e3dSKiyoshi Ueda 	struct mapped_device *md = tio->md;
826cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
827cec47e3dSKiyoshi Ueda 
828cec47e3dSKiyoshi Ueda 	if (blk_pc_request(rq)) {
829cec47e3dSKiyoshi Ueda 		rq->errors = clone->errors;
830cec47e3dSKiyoshi Ueda 		rq->resid_len = clone->resid_len;
831cec47e3dSKiyoshi Ueda 
832cec47e3dSKiyoshi Ueda 		if (rq->sense)
833cec47e3dSKiyoshi Ueda 			/*
834cec47e3dSKiyoshi Ueda 			 * We are using the sense buffer of the original
835cec47e3dSKiyoshi Ueda 			 * request.
836cec47e3dSKiyoshi Ueda 			 * So setting the length of the sense data is enough.
837cec47e3dSKiyoshi Ueda 			 */
838cec47e3dSKiyoshi Ueda 			rq->sense_len = clone->sense_len;
839cec47e3dSKiyoshi Ueda 	}
840cec47e3dSKiyoshi Ueda 
841a77e28c7SKiyoshi Ueda 	free_rq_clone(clone);
842cec47e3dSKiyoshi Ueda 
843cec47e3dSKiyoshi Ueda 	blk_end_request_all(rq, error);
844cec47e3dSKiyoshi Ueda 
845cec47e3dSKiyoshi Ueda 	rq_completed(md, 1);
846cec47e3dSKiyoshi Ueda }
847cec47e3dSKiyoshi Ueda 
848cec47e3dSKiyoshi Ueda /*
849cec47e3dSKiyoshi Ueda  * Request completion handler for request-based dm
850cec47e3dSKiyoshi Ueda  */
851cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq)
852cec47e3dSKiyoshi Ueda {
853cec47e3dSKiyoshi Ueda 	struct request *clone = rq->completion_data;
854cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
855cec47e3dSKiyoshi Ueda 	dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
856cec47e3dSKiyoshi Ueda 	int error = tio->error;
857cec47e3dSKiyoshi Ueda 
858cec47e3dSKiyoshi Ueda 	if (!(rq->cmd_flags & REQ_FAILED) && rq_end_io)
859cec47e3dSKiyoshi Ueda 		error = rq_end_io(tio->ti, clone, error, &tio->info);
860cec47e3dSKiyoshi Ueda 
861cec47e3dSKiyoshi Ueda 	if (error <= 0)
862cec47e3dSKiyoshi Ueda 		/* The target wants to complete the I/O */
863cec47e3dSKiyoshi Ueda 		dm_end_request(clone, error);
864cec47e3dSKiyoshi Ueda 	else if (error == DM_ENDIO_INCOMPLETE)
865cec47e3dSKiyoshi Ueda 		/* The target will handle the I/O */
866cec47e3dSKiyoshi Ueda 		return;
867cec47e3dSKiyoshi Ueda 	else if (error == DM_ENDIO_REQUEUE)
868cec47e3dSKiyoshi Ueda 		/* The target wants to requeue the I/O */
869cec47e3dSKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
870cec47e3dSKiyoshi Ueda 	else {
871cec47e3dSKiyoshi Ueda 		DMWARN("unimplemented target endio return value: %d", error);
872cec47e3dSKiyoshi Ueda 		BUG();
873cec47e3dSKiyoshi Ueda 	}
874cec47e3dSKiyoshi Ueda }
875cec47e3dSKiyoshi Ueda 
876cec47e3dSKiyoshi Ueda /*
877cec47e3dSKiyoshi Ueda  * Complete the clone and the original request with the error status
878cec47e3dSKiyoshi Ueda  * through softirq context.
879cec47e3dSKiyoshi Ueda  */
880cec47e3dSKiyoshi Ueda static void dm_complete_request(struct request *clone, int error)
881cec47e3dSKiyoshi Ueda {
882cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
883cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
884cec47e3dSKiyoshi Ueda 
885cec47e3dSKiyoshi Ueda 	tio->error = error;
886cec47e3dSKiyoshi Ueda 	rq->completion_data = clone;
887cec47e3dSKiyoshi Ueda 	blk_complete_request(rq);
888cec47e3dSKiyoshi Ueda }
889cec47e3dSKiyoshi Ueda 
890cec47e3dSKiyoshi Ueda /*
891cec47e3dSKiyoshi Ueda  * Complete the not-mapped clone and the original request with the error status
892cec47e3dSKiyoshi Ueda  * through softirq context.
893cec47e3dSKiyoshi Ueda  * Target's rq_end_io() function isn't called.
894cec47e3dSKiyoshi Ueda  * This may be used when the target's map_rq() function fails.
895cec47e3dSKiyoshi Ueda  */
896cec47e3dSKiyoshi Ueda void dm_kill_unmapped_request(struct request *clone, int error)
897cec47e3dSKiyoshi Ueda {
898cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
899cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
900cec47e3dSKiyoshi Ueda 
901cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_FAILED;
902cec47e3dSKiyoshi Ueda 	dm_complete_request(clone, error);
903cec47e3dSKiyoshi Ueda }
904cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
905cec47e3dSKiyoshi Ueda 
906cec47e3dSKiyoshi Ueda /*
907cec47e3dSKiyoshi Ueda  * Called with the queue lock held
908cec47e3dSKiyoshi Ueda  */
909cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error)
910cec47e3dSKiyoshi Ueda {
911cec47e3dSKiyoshi Ueda 	/*
912cec47e3dSKiyoshi Ueda 	 * For just cleaning up the information of the queue in which
913cec47e3dSKiyoshi Ueda 	 * the clone was dispatched.
914cec47e3dSKiyoshi Ueda 	 * The clone is *NOT* freed actually here because it is alloced from
915cec47e3dSKiyoshi Ueda 	 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
916cec47e3dSKiyoshi Ueda 	 */
917cec47e3dSKiyoshi Ueda 	__blk_put_request(clone->q, clone);
918cec47e3dSKiyoshi Ueda 
919cec47e3dSKiyoshi Ueda 	/*
920cec47e3dSKiyoshi Ueda 	 * Actual request completion is done in a softirq context which doesn't
921cec47e3dSKiyoshi Ueda 	 * hold the queue lock.  Otherwise, deadlock could occur because:
922cec47e3dSKiyoshi Ueda 	 *     - another request may be submitted by the upper level driver
923cec47e3dSKiyoshi Ueda 	 *       of the stacking during the completion
924cec47e3dSKiyoshi Ueda 	 *     - the submission which requires queue lock may be done
925cec47e3dSKiyoshi Ueda 	 *       against this queue
926cec47e3dSKiyoshi Ueda 	 */
927cec47e3dSKiyoshi Ueda 	dm_complete_request(clone, error);
928cec47e3dSKiyoshi Ueda }
929cec47e3dSKiyoshi Ueda 
9301da177e4SLinus Torvalds static sector_t max_io_len(struct mapped_device *md,
9311da177e4SLinus Torvalds 			   sector_t sector, struct dm_target *ti)
9321da177e4SLinus Torvalds {
9331da177e4SLinus Torvalds 	sector_t offset = sector - ti->begin;
9341da177e4SLinus Torvalds 	sector_t len = ti->len - offset;
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	/*
9371da177e4SLinus Torvalds 	 * Does the target need to split even further ?
9381da177e4SLinus Torvalds 	 */
9391da177e4SLinus Torvalds 	if (ti->split_io) {
9401da177e4SLinus Torvalds 		sector_t boundary;
9411da177e4SLinus Torvalds 		boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
9421da177e4SLinus Torvalds 			   - offset;
9431da177e4SLinus Torvalds 		if (len > boundary)
9441da177e4SLinus Torvalds 			len = boundary;
9451da177e4SLinus Torvalds 	}
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	return len;
9481da177e4SLinus Torvalds }
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds static void __map_bio(struct dm_target *ti, struct bio *clone,
951028867acSAlasdair G Kergon 		      struct dm_target_io *tio)
9521da177e4SLinus Torvalds {
9531da177e4SLinus Torvalds 	int r;
9542056a782SJens Axboe 	sector_t sector;
9559faf400fSStefan Bader 	struct mapped_device *md;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
9581da177e4SLinus Torvalds 	clone->bi_private = tio;
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	/*
9611da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
9621da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
9631da177e4SLinus Torvalds 	 * this io.
9641da177e4SLinus Torvalds 	 */
9651da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
9662056a782SJens Axboe 	sector = clone->bi_sector;
9671da177e4SLinus Torvalds 	r = ti->type->map(ti, clone, &tio->info);
96845cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
9691da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
9702056a782SJens Axboe 
9715f3ea37cSArnaldo Carvalho de Melo 		trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
97222a7c31aSAlan D. Brunelle 				    tio->io->bio->bi_bdev->bd_dev, sector);
9732056a782SJens Axboe 
9741da177e4SLinus Torvalds 		generic_make_request(clone);
9752e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
9762e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
9779faf400fSStefan Bader 		md = tio->io->md;
9789faf400fSStefan Bader 		dec_pending(tio->io, r);
9799faf400fSStefan Bader 		/*
9809faf400fSStefan Bader 		 * Store bio_set for cleanup.
9819faf400fSStefan Bader 		 */
9829faf400fSStefan Bader 		clone->bi_private = md->bs;
9831da177e4SLinus Torvalds 		bio_put(clone);
9849faf400fSStefan Bader 		free_tio(md, tio);
98545cbcd79SKiyoshi Ueda 	} else if (r) {
98645cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
98745cbcd79SKiyoshi Ueda 		BUG();
9881da177e4SLinus Torvalds 	}
9891da177e4SLinus Torvalds }
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds struct clone_info {
9921da177e4SLinus Torvalds 	struct mapped_device *md;
9931da177e4SLinus Torvalds 	struct dm_table *map;
9941da177e4SLinus Torvalds 	struct bio *bio;
9951da177e4SLinus Torvalds 	struct dm_io *io;
9961da177e4SLinus Torvalds 	sector_t sector;
9971da177e4SLinus Torvalds 	sector_t sector_count;
9981da177e4SLinus Torvalds 	unsigned short idx;
9991da177e4SLinus Torvalds };
10001da177e4SLinus Torvalds 
10013676347aSPeter Osterlund static void dm_bio_destructor(struct bio *bio)
10023676347aSPeter Osterlund {
10039faf400fSStefan Bader 	struct bio_set *bs = bio->bi_private;
10049faf400fSStefan Bader 
10059faf400fSStefan Bader 	bio_free(bio, bs);
10063676347aSPeter Osterlund }
10073676347aSPeter Osterlund 
10081da177e4SLinus Torvalds /*
10091da177e4SLinus Torvalds  * Creates a little bio that is just does part of a bvec.
10101da177e4SLinus Torvalds  */
10111da177e4SLinus Torvalds static struct bio *split_bvec(struct bio *bio, sector_t sector,
10121da177e4SLinus Torvalds 			      unsigned short idx, unsigned int offset,
10139faf400fSStefan Bader 			      unsigned int len, struct bio_set *bs)
10141da177e4SLinus Torvalds {
10151da177e4SLinus Torvalds 	struct bio *clone;
10161da177e4SLinus Torvalds 	struct bio_vec *bv = bio->bi_io_vec + idx;
10171da177e4SLinus Torvalds 
10189faf400fSStefan Bader 	clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
10193676347aSPeter Osterlund 	clone->bi_destructor = dm_bio_destructor;
10201da177e4SLinus Torvalds 	*clone->bi_io_vec = *bv;
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	clone->bi_sector = sector;
10231da177e4SLinus Torvalds 	clone->bi_bdev = bio->bi_bdev;
1024af7e466aSMikulas Patocka 	clone->bi_rw = bio->bi_rw & ~(1 << BIO_RW_BARRIER);
10251da177e4SLinus Torvalds 	clone->bi_vcnt = 1;
10261da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
10271da177e4SLinus Torvalds 	clone->bi_io_vec->bv_offset = offset;
10281da177e4SLinus Torvalds 	clone->bi_io_vec->bv_len = clone->bi_size;
1029f3e1d26eSMartin K. Petersen 	clone->bi_flags |= 1 << BIO_CLONED;
10301da177e4SLinus Torvalds 
10319c47008dSMartin K. Petersen 	if (bio_integrity(bio)) {
10327878cba9SMartin K. Petersen 		bio_integrity_clone(clone, bio, GFP_NOIO, bs);
10339c47008dSMartin K. Petersen 		bio_integrity_trim(clone,
10349c47008dSMartin K. Petersen 				   bio_sector_offset(bio, idx, offset), len);
10359c47008dSMartin K. Petersen 	}
10369c47008dSMartin K. Petersen 
10371da177e4SLinus Torvalds 	return clone;
10381da177e4SLinus Torvalds }
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds /*
10411da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
10421da177e4SLinus Torvalds  */
10431da177e4SLinus Torvalds static struct bio *clone_bio(struct bio *bio, sector_t sector,
10441da177e4SLinus Torvalds 			     unsigned short idx, unsigned short bv_count,
10459faf400fSStefan Bader 			     unsigned int len, struct bio_set *bs)
10461da177e4SLinus Torvalds {
10471da177e4SLinus Torvalds 	struct bio *clone;
10481da177e4SLinus Torvalds 
10499faf400fSStefan Bader 	clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
10509faf400fSStefan Bader 	__bio_clone(clone, bio);
1051af7e466aSMikulas Patocka 	clone->bi_rw &= ~(1 << BIO_RW_BARRIER);
10529faf400fSStefan Bader 	clone->bi_destructor = dm_bio_destructor;
10531da177e4SLinus Torvalds 	clone->bi_sector = sector;
10541da177e4SLinus Torvalds 	clone->bi_idx = idx;
10551da177e4SLinus Torvalds 	clone->bi_vcnt = idx + bv_count;
10561da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
10571da177e4SLinus Torvalds 	clone->bi_flags &= ~(1 << BIO_SEG_VALID);
10581da177e4SLinus Torvalds 
10599c47008dSMartin K. Petersen 	if (bio_integrity(bio)) {
10607878cba9SMartin K. Petersen 		bio_integrity_clone(clone, bio, GFP_NOIO, bs);
10619c47008dSMartin K. Petersen 
10629c47008dSMartin K. Petersen 		if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
10639c47008dSMartin K. Petersen 			bio_integrity_trim(clone,
10649c47008dSMartin K. Petersen 					   bio_sector_offset(bio, idx, 0), len);
10659c47008dSMartin K. Petersen 	}
10669c47008dSMartin K. Petersen 
10671da177e4SLinus Torvalds 	return clone;
10681da177e4SLinus Torvalds }
10691da177e4SLinus Torvalds 
10709015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci,
10719015df24SAlasdair G Kergon 				      struct dm_target *ti)
1072f9ab94ceSMikulas Patocka {
10739015df24SAlasdair G Kergon 	struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
1074f9ab94ceSMikulas Patocka 
1075f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1076f9ab94ceSMikulas Patocka 	tio->ti = ti;
1077f9ab94ceSMikulas Patocka 	memset(&tio->info, 0, sizeof(tio->info));
10789015df24SAlasdair G Kergon 
10799015df24SAlasdair G Kergon 	return tio;
10809015df24SAlasdair G Kergon }
10819015df24SAlasdair G Kergon 
10829015df24SAlasdair G Kergon static void __flush_target(struct clone_info *ci, struct dm_target *ti,
10839015df24SAlasdair G Kergon 			  unsigned flush_nr)
10849015df24SAlasdair G Kergon {
10859015df24SAlasdair G Kergon 	struct dm_target_io *tio = alloc_tio(ci, ti);
10869015df24SAlasdair G Kergon 	struct bio *clone;
10879015df24SAlasdair G Kergon 
1088f9ab94ceSMikulas Patocka 	tio->info.flush_request = flush_nr;
1089f9ab94ceSMikulas Patocka 
1090f9ab94ceSMikulas Patocka 	clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
1091f9ab94ceSMikulas Patocka 	__bio_clone(clone, ci->bio);
1092f9ab94ceSMikulas Patocka 	clone->bi_destructor = dm_bio_destructor;
1093f9ab94ceSMikulas Patocka 
1094f9ab94ceSMikulas Patocka 	__map_bio(ti, clone, tio);
1095f9ab94ceSMikulas Patocka }
1096f9ab94ceSMikulas Patocka 
1097f9ab94ceSMikulas Patocka static int __clone_and_map_empty_barrier(struct clone_info *ci)
1098f9ab94ceSMikulas Patocka {
1099f9ab94ceSMikulas Patocka 	unsigned target_nr = 0, flush_nr;
1100f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1101f9ab94ceSMikulas Patocka 
1102f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
1103f9ab94ceSMikulas Patocka 		for (flush_nr = 0; flush_nr < ti->num_flush_requests;
1104f9ab94ceSMikulas Patocka 		     flush_nr++)
1105f9ab94ceSMikulas Patocka 			__flush_target(ci, ti, flush_nr);
1106f9ab94ceSMikulas Patocka 
1107f9ab94ceSMikulas Patocka 	ci->sector_count = 0;
1108f9ab94ceSMikulas Patocka 
1109f9ab94ceSMikulas Patocka 	return 0;
1110f9ab94ceSMikulas Patocka }
1111f9ab94ceSMikulas Patocka 
1112512875bdSJun'ichi Nomura static int __clone_and_map(struct clone_info *ci)
11131da177e4SLinus Torvalds {
11141da177e4SLinus Torvalds 	struct bio *clone, *bio = ci->bio;
1115512875bdSJun'ichi Nomura 	struct dm_target *ti;
1116512875bdSJun'ichi Nomura 	sector_t len = 0, max;
1117028867acSAlasdair G Kergon 	struct dm_target_io *tio;
11181da177e4SLinus Torvalds 
1119f9ab94ceSMikulas Patocka 	if (unlikely(bio_empty_barrier(bio)))
1120f9ab94ceSMikulas Patocka 		return __clone_and_map_empty_barrier(ci);
1121f9ab94ceSMikulas Patocka 
1122512875bdSJun'ichi Nomura 	ti = dm_table_find_target(ci->map, ci->sector);
1123512875bdSJun'ichi Nomura 	if (!dm_target_is_valid(ti))
1124512875bdSJun'ichi Nomura 		return -EIO;
1125512875bdSJun'ichi Nomura 
1126512875bdSJun'ichi Nomura 	max = max_io_len(ci->md, ci->sector, ti);
1127512875bdSJun'ichi Nomura 
11281da177e4SLinus Torvalds 	/*
11291da177e4SLinus Torvalds 	 * Allocate a target io object.
11301da177e4SLinus Torvalds 	 */
11319015df24SAlasdair G Kergon 	tio = alloc_tio(ci, ti);
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 	if (ci->sector_count <= max) {
11341da177e4SLinus Torvalds 		/*
11351da177e4SLinus Torvalds 		 * Optimise for the simple case where we can do all of
11361da177e4SLinus Torvalds 		 * the remaining io with a single clone.
11371da177e4SLinus Torvalds 		 */
11381da177e4SLinus Torvalds 		clone = clone_bio(bio, ci->sector, ci->idx,
11399faf400fSStefan Bader 				  bio->bi_vcnt - ci->idx, ci->sector_count,
11409faf400fSStefan Bader 				  ci->md->bs);
11411da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
11421da177e4SLinus Torvalds 		ci->sector_count = 0;
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	} else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
11451da177e4SLinus Torvalds 		/*
11461da177e4SLinus Torvalds 		 * There are some bvecs that don't span targets.
11471da177e4SLinus Torvalds 		 * Do as many of these as possible.
11481da177e4SLinus Torvalds 		 */
11491da177e4SLinus Torvalds 		int i;
11501da177e4SLinus Torvalds 		sector_t remaining = max;
11511da177e4SLinus Torvalds 		sector_t bv_len;
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds 		for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
11541da177e4SLinus Torvalds 			bv_len = to_sector(bio->bi_io_vec[i].bv_len);
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds 			if (bv_len > remaining)
11571da177e4SLinus Torvalds 				break;
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 			remaining -= bv_len;
11601da177e4SLinus Torvalds 			len += bv_len;
11611da177e4SLinus Torvalds 		}
11621da177e4SLinus Torvalds 
11639faf400fSStefan Bader 		clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
11649faf400fSStefan Bader 				  ci->md->bs);
11651da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 		ci->sector += len;
11681da177e4SLinus Torvalds 		ci->sector_count -= len;
11691da177e4SLinus Torvalds 		ci->idx = i;
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 	} else {
11721da177e4SLinus Torvalds 		/*
1173d2044a94SAlasdair G Kergon 		 * Handle a bvec that must be split between two or more targets.
11741da177e4SLinus Torvalds 		 */
11751da177e4SLinus Torvalds 		struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1176d2044a94SAlasdair G Kergon 		sector_t remaining = to_sector(bv->bv_len);
1177d2044a94SAlasdair G Kergon 		unsigned int offset = 0;
11781da177e4SLinus Torvalds 
1179d2044a94SAlasdair G Kergon 		do {
1180d2044a94SAlasdair G Kergon 			if (offset) {
11811da177e4SLinus Torvalds 				ti = dm_table_find_target(ci->map, ci->sector);
1182512875bdSJun'ichi Nomura 				if (!dm_target_is_valid(ti))
1183512875bdSJun'ichi Nomura 					return -EIO;
1184512875bdSJun'ichi Nomura 
1185d2044a94SAlasdair G Kergon 				max = max_io_len(ci->md, ci->sector, ti);
11861da177e4SLinus Torvalds 
11879015df24SAlasdair G Kergon 				tio = alloc_tio(ci, ti);
1188d2044a94SAlasdair G Kergon 			}
1189d2044a94SAlasdair G Kergon 
1190d2044a94SAlasdair G Kergon 			len = min(remaining, max);
1191d2044a94SAlasdair G Kergon 
1192d2044a94SAlasdair G Kergon 			clone = split_bvec(bio, ci->sector, ci->idx,
11939faf400fSStefan Bader 					   bv->bv_offset + offset, len,
11949faf400fSStefan Bader 					   ci->md->bs);
1195d2044a94SAlasdair G Kergon 
11961da177e4SLinus Torvalds 			__map_bio(ti, clone, tio);
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 			ci->sector += len;
11991da177e4SLinus Torvalds 			ci->sector_count -= len;
1200d2044a94SAlasdair G Kergon 			offset += to_bytes(len);
1201d2044a94SAlasdair G Kergon 		} while (remaining -= len);
1202d2044a94SAlasdair G Kergon 
12031da177e4SLinus Torvalds 		ci->idx++;
12041da177e4SLinus Torvalds 	}
1205512875bdSJun'ichi Nomura 
1206512875bdSJun'ichi Nomura 	return 0;
12071da177e4SLinus Torvalds }
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds /*
12108a53c28dSMikulas Patocka  * Split the bio into several clones and submit it to targets.
12111da177e4SLinus Torvalds  */
1212f0b9a450SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
12131da177e4SLinus Torvalds {
12141da177e4SLinus Torvalds 	struct clone_info ci;
1215512875bdSJun'ichi Nomura 	int error = 0;
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	ci.map = dm_get_table(md);
1218f0b9a450SMikulas Patocka 	if (unlikely(!ci.map)) {
12191f98a13fSJens Axboe 		if (!bio_rw_flagged(bio, BIO_RW_BARRIER))
1220f0b9a450SMikulas Patocka 			bio_io_error(bio);
1221af7e466aSMikulas Patocka 		else
12225aa2781dSMikulas Patocka 			if (!md->barrier_error)
1223af7e466aSMikulas Patocka 				md->barrier_error = -EIO;
1224f0b9a450SMikulas Patocka 		return;
1225f0b9a450SMikulas Patocka 	}
1226692d0eb9SMikulas Patocka 
12271da177e4SLinus Torvalds 	ci.md = md;
12281da177e4SLinus Torvalds 	ci.bio = bio;
12291da177e4SLinus Torvalds 	ci.io = alloc_io(md);
12301da177e4SLinus Torvalds 	ci.io->error = 0;
12311da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
12321da177e4SLinus Torvalds 	ci.io->bio = bio;
12331da177e4SLinus Torvalds 	ci.io->md = md;
1234*f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
12351da177e4SLinus Torvalds 	ci.sector = bio->bi_sector;
12361da177e4SLinus Torvalds 	ci.sector_count = bio_sectors(bio);
1237f9ab94ceSMikulas Patocka 	if (unlikely(bio_empty_barrier(bio)))
1238f9ab94ceSMikulas Patocka 		ci.sector_count = 1;
12391da177e4SLinus Torvalds 	ci.idx = bio->bi_idx;
12401da177e4SLinus Torvalds 
12413eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1242512875bdSJun'ichi Nomura 	while (ci.sector_count && !error)
1243512875bdSJun'ichi Nomura 		error = __clone_and_map(&ci);
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 	/* drop the extra reference count */
1246512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
12471da177e4SLinus Torvalds 	dm_table_put(ci.map);
12481da177e4SLinus Torvalds }
12491da177e4SLinus Torvalds /*-----------------------------------------------------------------
12501da177e4SLinus Torvalds  * CRUD END
12511da177e4SLinus Torvalds  *---------------------------------------------------------------*/
12521da177e4SLinus Torvalds 
1253f6fccb12SMilan Broz static int dm_merge_bvec(struct request_queue *q,
1254f6fccb12SMilan Broz 			 struct bvec_merge_data *bvm,
1255f6fccb12SMilan Broz 			 struct bio_vec *biovec)
1256f6fccb12SMilan Broz {
1257f6fccb12SMilan Broz 	struct mapped_device *md = q->queuedata;
1258f6fccb12SMilan Broz 	struct dm_table *map = dm_get_table(md);
1259f6fccb12SMilan Broz 	struct dm_target *ti;
1260f6fccb12SMilan Broz 	sector_t max_sectors;
12615037108aSMikulas Patocka 	int max_size = 0;
1262f6fccb12SMilan Broz 
1263f6fccb12SMilan Broz 	if (unlikely(!map))
12645037108aSMikulas Patocka 		goto out;
1265f6fccb12SMilan Broz 
1266f6fccb12SMilan Broz 	ti = dm_table_find_target(map, bvm->bi_sector);
1267b01cd5acSMikulas Patocka 	if (!dm_target_is_valid(ti))
1268b01cd5acSMikulas Patocka 		goto out_table;
1269f6fccb12SMilan Broz 
1270f6fccb12SMilan Broz 	/*
1271f6fccb12SMilan Broz 	 * Find maximum amount of I/O that won't need splitting
1272f6fccb12SMilan Broz 	 */
1273f6fccb12SMilan Broz 	max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
1274f6fccb12SMilan Broz 			  (sector_t) BIO_MAX_SECTORS);
1275f6fccb12SMilan Broz 	max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1276f6fccb12SMilan Broz 	if (max_size < 0)
1277f6fccb12SMilan Broz 		max_size = 0;
1278f6fccb12SMilan Broz 
1279f6fccb12SMilan Broz 	/*
1280f6fccb12SMilan Broz 	 * merge_bvec_fn() returns number of bytes
1281f6fccb12SMilan Broz 	 * it can accept at this offset
1282f6fccb12SMilan Broz 	 * max is precomputed maximal io size
1283f6fccb12SMilan Broz 	 */
1284f6fccb12SMilan Broz 	if (max_size && ti->type->merge)
1285f6fccb12SMilan Broz 		max_size = ti->type->merge(ti, bvm, biovec, max_size);
12868cbeb67aSMikulas Patocka 	/*
12878cbeb67aSMikulas Patocka 	 * If the target doesn't support merge method and some of the devices
12888cbeb67aSMikulas Patocka 	 * provided their merge_bvec method (we know this by looking at
12898cbeb67aSMikulas Patocka 	 * queue_max_hw_sectors), then we can't allow bios with multiple vector
12908cbeb67aSMikulas Patocka 	 * entries.  So always set max_size to 0, and the code below allows
12918cbeb67aSMikulas Patocka 	 * just one page.
12928cbeb67aSMikulas Patocka 	 */
12938cbeb67aSMikulas Patocka 	else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
12948cbeb67aSMikulas Patocka 
12958cbeb67aSMikulas Patocka 		max_size = 0;
1296f6fccb12SMilan Broz 
1297b01cd5acSMikulas Patocka out_table:
12985037108aSMikulas Patocka 	dm_table_put(map);
12995037108aSMikulas Patocka 
13005037108aSMikulas Patocka out:
1301f6fccb12SMilan Broz 	/*
1302f6fccb12SMilan Broz 	 * Always allow an entire first page
1303f6fccb12SMilan Broz 	 */
1304f6fccb12SMilan Broz 	if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1305f6fccb12SMilan Broz 		max_size = biovec->bv_len;
1306f6fccb12SMilan Broz 
1307f6fccb12SMilan Broz 	return max_size;
1308f6fccb12SMilan Broz }
1309f6fccb12SMilan Broz 
13101da177e4SLinus Torvalds /*
13111da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
13121da177e4SLinus Torvalds  * dm_merge_bvec.
13131da177e4SLinus Torvalds  */
1314cec47e3dSKiyoshi Ueda static int _dm_request(struct request_queue *q, struct bio *bio)
13151da177e4SLinus Torvalds {
131612f03a49SKevin Corry 	int rw = bio_data_dir(bio);
13171da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
1318c9959059STejun Heo 	int cpu;
13191da177e4SLinus Torvalds 
13202ca3310eSAlasdair G Kergon 	down_read(&md->io_lock);
13211da177e4SLinus Torvalds 
1322074a7acaSTejun Heo 	cpu = part_stat_lock();
1323074a7acaSTejun Heo 	part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1324074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1325074a7acaSTejun Heo 	part_stat_unlock();
132612f03a49SKevin Corry 
13271da177e4SLinus Torvalds 	/*
13281eb787ecSAlasdair G Kergon 	 * If we're suspended or the thread is processing barriers
13291eb787ecSAlasdair G Kergon 	 * we have to queue this io for later.
13301da177e4SLinus Torvalds 	 */
1331af7e466aSMikulas Patocka 	if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
13321f98a13fSJens Axboe 	    unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
13332ca3310eSAlasdair G Kergon 		up_read(&md->io_lock);
13341da177e4SLinus Torvalds 
133554d9a1b4SAlasdair G Kergon 		if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
133654d9a1b4SAlasdair G Kergon 		    bio_rw(bio) == READA) {
133754d9a1b4SAlasdair G Kergon 			bio_io_error(bio);
133854d9a1b4SAlasdair G Kergon 			return 0;
133954d9a1b4SAlasdair G Kergon 		}
13401da177e4SLinus Torvalds 
134192c63902SMikulas Patocka 		queue_io(md, bio);
13421da177e4SLinus Torvalds 
134392c63902SMikulas Patocka 		return 0;
13441da177e4SLinus Torvalds 	}
13451da177e4SLinus Torvalds 
1346f0b9a450SMikulas Patocka 	__split_and_process_bio(md, bio);
13472ca3310eSAlasdair G Kergon 	up_read(&md->io_lock);
1348f0b9a450SMikulas Patocka 	return 0;
13491da177e4SLinus Torvalds }
13501da177e4SLinus Torvalds 
1351cec47e3dSKiyoshi Ueda static int dm_make_request(struct request_queue *q, struct bio *bio)
1352cec47e3dSKiyoshi Ueda {
1353cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1354cec47e3dSKiyoshi Ueda 
13551f98a13fSJens Axboe 	if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
1356cec47e3dSKiyoshi Ueda 		bio_endio(bio, -EOPNOTSUPP);
1357cec47e3dSKiyoshi Ueda 		return 0;
1358cec47e3dSKiyoshi Ueda 	}
1359cec47e3dSKiyoshi Ueda 
1360cec47e3dSKiyoshi Ueda 	return md->saved_make_request_fn(q, bio); /* call __make_request() */
1361cec47e3dSKiyoshi Ueda }
1362cec47e3dSKiyoshi Ueda 
1363cec47e3dSKiyoshi Ueda static int dm_request_based(struct mapped_device *md)
1364cec47e3dSKiyoshi Ueda {
1365cec47e3dSKiyoshi Ueda 	return blk_queue_stackable(md->queue);
1366cec47e3dSKiyoshi Ueda }
1367cec47e3dSKiyoshi Ueda 
1368cec47e3dSKiyoshi Ueda static int dm_request(struct request_queue *q, struct bio *bio)
1369cec47e3dSKiyoshi Ueda {
1370cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1371cec47e3dSKiyoshi Ueda 
1372cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
1373cec47e3dSKiyoshi Ueda 		return dm_make_request(q, bio);
1374cec47e3dSKiyoshi Ueda 
1375cec47e3dSKiyoshi Ueda 	return _dm_request(q, bio);
1376cec47e3dSKiyoshi Ueda }
1377cec47e3dSKiyoshi Ueda 
1378cec47e3dSKiyoshi Ueda void dm_dispatch_request(struct request *rq)
1379cec47e3dSKiyoshi Ueda {
1380cec47e3dSKiyoshi Ueda 	int r;
1381cec47e3dSKiyoshi Ueda 
1382cec47e3dSKiyoshi Ueda 	if (blk_queue_io_stat(rq->q))
1383cec47e3dSKiyoshi Ueda 		rq->cmd_flags |= REQ_IO_STAT;
1384cec47e3dSKiyoshi Ueda 
1385cec47e3dSKiyoshi Ueda 	rq->start_time = jiffies;
1386cec47e3dSKiyoshi Ueda 	r = blk_insert_cloned_request(rq->q, rq);
1387cec47e3dSKiyoshi Ueda 	if (r)
1388cec47e3dSKiyoshi Ueda 		dm_complete_request(rq, r);
1389cec47e3dSKiyoshi Ueda }
1390cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_dispatch_request);
1391cec47e3dSKiyoshi Ueda 
1392cec47e3dSKiyoshi Ueda static void dm_rq_bio_destructor(struct bio *bio)
1393cec47e3dSKiyoshi Ueda {
1394cec47e3dSKiyoshi Ueda 	struct dm_rq_clone_bio_info *info = bio->bi_private;
1395cec47e3dSKiyoshi Ueda 	struct mapped_device *md = info->tio->md;
1396cec47e3dSKiyoshi Ueda 
1397cec47e3dSKiyoshi Ueda 	free_bio_info(info);
1398cec47e3dSKiyoshi Ueda 	bio_free(bio, md->bs);
1399cec47e3dSKiyoshi Ueda }
1400cec47e3dSKiyoshi Ueda 
1401cec47e3dSKiyoshi Ueda static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1402cec47e3dSKiyoshi Ueda 				 void *data)
1403cec47e3dSKiyoshi Ueda {
1404cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = data;
1405cec47e3dSKiyoshi Ueda 	struct mapped_device *md = tio->md;
1406cec47e3dSKiyoshi Ueda 	struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1407cec47e3dSKiyoshi Ueda 
1408cec47e3dSKiyoshi Ueda 	if (!info)
1409cec47e3dSKiyoshi Ueda 		return -ENOMEM;
1410cec47e3dSKiyoshi Ueda 
1411cec47e3dSKiyoshi Ueda 	info->orig = bio_orig;
1412cec47e3dSKiyoshi Ueda 	info->tio = tio;
1413cec47e3dSKiyoshi Ueda 	bio->bi_end_io = end_clone_bio;
1414cec47e3dSKiyoshi Ueda 	bio->bi_private = info;
1415cec47e3dSKiyoshi Ueda 	bio->bi_destructor = dm_rq_bio_destructor;
1416cec47e3dSKiyoshi Ueda 
1417cec47e3dSKiyoshi Ueda 	return 0;
1418cec47e3dSKiyoshi Ueda }
1419cec47e3dSKiyoshi Ueda 
1420cec47e3dSKiyoshi Ueda static int setup_clone(struct request *clone, struct request *rq,
1421cec47e3dSKiyoshi Ueda 		       struct dm_rq_target_io *tio)
1422cec47e3dSKiyoshi Ueda {
1423cec47e3dSKiyoshi Ueda 	int r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1424cec47e3dSKiyoshi Ueda 				  dm_rq_bio_constructor, tio);
1425cec47e3dSKiyoshi Ueda 
1426cec47e3dSKiyoshi Ueda 	if (r)
1427cec47e3dSKiyoshi Ueda 		return r;
1428cec47e3dSKiyoshi Ueda 
1429cec47e3dSKiyoshi Ueda 	clone->cmd = rq->cmd;
1430cec47e3dSKiyoshi Ueda 	clone->cmd_len = rq->cmd_len;
1431cec47e3dSKiyoshi Ueda 	clone->sense = rq->sense;
1432cec47e3dSKiyoshi Ueda 	clone->buffer = rq->buffer;
1433cec47e3dSKiyoshi Ueda 	clone->end_io = end_clone_request;
1434cec47e3dSKiyoshi Ueda 	clone->end_io_data = tio;
1435cec47e3dSKiyoshi Ueda 
1436cec47e3dSKiyoshi Ueda 	return 0;
1437cec47e3dSKiyoshi Ueda }
1438cec47e3dSKiyoshi Ueda 
1439cec47e3dSKiyoshi Ueda static int dm_rq_flush_suspending(struct mapped_device *md)
1440cec47e3dSKiyoshi Ueda {
1441cec47e3dSKiyoshi Ueda 	return !md->suspend_rq.special;
1442cec47e3dSKiyoshi Ueda }
1443cec47e3dSKiyoshi Ueda 
1444cec47e3dSKiyoshi Ueda /*
1445cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1446cec47e3dSKiyoshi Ueda  */
1447cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq)
1448cec47e3dSKiyoshi Ueda {
1449cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1450cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio;
1451cec47e3dSKiyoshi Ueda 	struct request *clone;
1452cec47e3dSKiyoshi Ueda 
1453cec47e3dSKiyoshi Ueda 	if (unlikely(rq == &md->suspend_rq)) {
1454cec47e3dSKiyoshi Ueda 		if (dm_rq_flush_suspending(md))
1455cec47e3dSKiyoshi Ueda 			return BLKPREP_OK;
1456cec47e3dSKiyoshi Ueda 		else
1457cec47e3dSKiyoshi Ueda 			/* The flush suspend was interrupted */
1458cec47e3dSKiyoshi Ueda 			return BLKPREP_KILL;
1459cec47e3dSKiyoshi Ueda 	}
1460cec47e3dSKiyoshi Ueda 
1461cec47e3dSKiyoshi Ueda 	if (unlikely(rq->special)) {
1462cec47e3dSKiyoshi Ueda 		DMWARN("Already has something in rq->special.");
1463cec47e3dSKiyoshi Ueda 		return BLKPREP_KILL;
1464cec47e3dSKiyoshi Ueda 	}
1465cec47e3dSKiyoshi Ueda 
1466cec47e3dSKiyoshi Ueda 	tio = alloc_rq_tio(md); /* Only one for each original request */
1467cec47e3dSKiyoshi Ueda 	if (!tio)
1468cec47e3dSKiyoshi Ueda 		/* -ENOMEM */
1469cec47e3dSKiyoshi Ueda 		return BLKPREP_DEFER;
1470cec47e3dSKiyoshi Ueda 
1471cec47e3dSKiyoshi Ueda 	tio->md = md;
1472cec47e3dSKiyoshi Ueda 	tio->ti = NULL;
1473cec47e3dSKiyoshi Ueda 	tio->orig = rq;
1474cec47e3dSKiyoshi Ueda 	tio->error = 0;
1475cec47e3dSKiyoshi Ueda 	memset(&tio->info, 0, sizeof(tio->info));
1476cec47e3dSKiyoshi Ueda 
1477cec47e3dSKiyoshi Ueda 	clone = &tio->clone;
1478cec47e3dSKiyoshi Ueda 	if (setup_clone(clone, rq, tio)) {
1479cec47e3dSKiyoshi Ueda 		/* -ENOMEM */
1480cec47e3dSKiyoshi Ueda 		free_rq_tio(tio);
1481cec47e3dSKiyoshi Ueda 		return BLKPREP_DEFER;
1482cec47e3dSKiyoshi Ueda 	}
1483cec47e3dSKiyoshi Ueda 
1484cec47e3dSKiyoshi Ueda 	rq->special = clone;
1485cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_DONTPREP;
1486cec47e3dSKiyoshi Ueda 
1487cec47e3dSKiyoshi Ueda 	return BLKPREP_OK;
1488cec47e3dSKiyoshi Ueda }
1489cec47e3dSKiyoshi Ueda 
1490cec47e3dSKiyoshi Ueda static void map_request(struct dm_target *ti, struct request *rq,
1491cec47e3dSKiyoshi Ueda 			struct mapped_device *md)
1492cec47e3dSKiyoshi Ueda {
1493cec47e3dSKiyoshi Ueda 	int r;
1494cec47e3dSKiyoshi Ueda 	struct request *clone = rq->special;
1495cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1496cec47e3dSKiyoshi Ueda 
1497cec47e3dSKiyoshi Ueda 	/*
1498cec47e3dSKiyoshi Ueda 	 * Hold the md reference here for the in-flight I/O.
1499cec47e3dSKiyoshi Ueda 	 * We can't rely on the reference count by device opener,
1500cec47e3dSKiyoshi Ueda 	 * because the device may be closed during the request completion
1501cec47e3dSKiyoshi Ueda 	 * when all bios are completed.
1502cec47e3dSKiyoshi Ueda 	 * See the comment in rq_completed() too.
1503cec47e3dSKiyoshi Ueda 	 */
1504cec47e3dSKiyoshi Ueda 	dm_get(md);
1505cec47e3dSKiyoshi Ueda 
1506cec47e3dSKiyoshi Ueda 	tio->ti = ti;
1507cec47e3dSKiyoshi Ueda 	r = ti->type->map_rq(ti, clone, &tio->info);
1508cec47e3dSKiyoshi Ueda 	switch (r) {
1509cec47e3dSKiyoshi Ueda 	case DM_MAPIO_SUBMITTED:
1510cec47e3dSKiyoshi Ueda 		/* The target has taken the I/O to submit by itself later */
1511cec47e3dSKiyoshi Ueda 		break;
1512cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REMAPPED:
1513cec47e3dSKiyoshi Ueda 		/* The target has remapped the I/O so dispatch it */
1514cec47e3dSKiyoshi Ueda 		dm_dispatch_request(clone);
1515cec47e3dSKiyoshi Ueda 		break;
1516cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REQUEUE:
1517cec47e3dSKiyoshi Ueda 		/* The target wants to requeue the I/O */
1518cec47e3dSKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
1519cec47e3dSKiyoshi Ueda 		break;
1520cec47e3dSKiyoshi Ueda 	default:
1521cec47e3dSKiyoshi Ueda 		if (r > 0) {
1522cec47e3dSKiyoshi Ueda 			DMWARN("unimplemented target map return value: %d", r);
1523cec47e3dSKiyoshi Ueda 			BUG();
1524cec47e3dSKiyoshi Ueda 		}
1525cec47e3dSKiyoshi Ueda 
1526cec47e3dSKiyoshi Ueda 		/* The target wants to complete the I/O */
1527cec47e3dSKiyoshi Ueda 		dm_kill_unmapped_request(clone, r);
1528cec47e3dSKiyoshi Ueda 		break;
1529cec47e3dSKiyoshi Ueda 	}
1530cec47e3dSKiyoshi Ueda }
1531cec47e3dSKiyoshi Ueda 
1532cec47e3dSKiyoshi Ueda /*
1533cec47e3dSKiyoshi Ueda  * q->request_fn for request-based dm.
1534cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1535cec47e3dSKiyoshi Ueda  */
1536cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q)
1537cec47e3dSKiyoshi Ueda {
1538cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1539cec47e3dSKiyoshi Ueda 	struct dm_table *map = dm_get_table(md);
1540cec47e3dSKiyoshi Ueda 	struct dm_target *ti;
1541cec47e3dSKiyoshi Ueda 	struct request *rq;
1542cec47e3dSKiyoshi Ueda 
1543cec47e3dSKiyoshi Ueda 	/*
1544cec47e3dSKiyoshi Ueda 	 * For noflush suspend, check blk_queue_stopped() to immediately
1545cec47e3dSKiyoshi Ueda 	 * quit I/O dispatching.
1546cec47e3dSKiyoshi Ueda 	 */
1547cec47e3dSKiyoshi Ueda 	while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1548cec47e3dSKiyoshi Ueda 		rq = blk_peek_request(q);
1549cec47e3dSKiyoshi Ueda 		if (!rq)
1550cec47e3dSKiyoshi Ueda 			goto plug_and_out;
1551cec47e3dSKiyoshi Ueda 
1552cec47e3dSKiyoshi Ueda 		if (unlikely(rq == &md->suspend_rq)) { /* Flush suspend maker */
1553cec47e3dSKiyoshi Ueda 			if (queue_in_flight(q))
1554cec47e3dSKiyoshi Ueda 				/* Not quiet yet.  Wait more */
1555cec47e3dSKiyoshi Ueda 				goto plug_and_out;
1556cec47e3dSKiyoshi Ueda 
1557cec47e3dSKiyoshi Ueda 			/* This device should be quiet now */
1558cec47e3dSKiyoshi Ueda 			__stop_queue(q);
1559cec47e3dSKiyoshi Ueda 			blk_start_request(rq);
1560cec47e3dSKiyoshi Ueda 			__blk_end_request_all(rq, 0);
1561cec47e3dSKiyoshi Ueda 			wake_up(&md->wait);
1562cec47e3dSKiyoshi Ueda 			goto out;
1563cec47e3dSKiyoshi Ueda 		}
1564cec47e3dSKiyoshi Ueda 
1565cec47e3dSKiyoshi Ueda 		ti = dm_table_find_target(map, blk_rq_pos(rq));
1566cec47e3dSKiyoshi Ueda 		if (ti->type->busy && ti->type->busy(ti))
1567cec47e3dSKiyoshi Ueda 			goto plug_and_out;
1568cec47e3dSKiyoshi Ueda 
1569cec47e3dSKiyoshi Ueda 		blk_start_request(rq);
1570cec47e3dSKiyoshi Ueda 		spin_unlock(q->queue_lock);
1571cec47e3dSKiyoshi Ueda 		map_request(ti, rq, md);
1572cec47e3dSKiyoshi Ueda 		spin_lock_irq(q->queue_lock);
1573cec47e3dSKiyoshi Ueda 	}
1574cec47e3dSKiyoshi Ueda 
1575cec47e3dSKiyoshi Ueda 	goto out;
1576cec47e3dSKiyoshi Ueda 
1577cec47e3dSKiyoshi Ueda plug_and_out:
1578cec47e3dSKiyoshi Ueda 	if (!elv_queue_empty(q))
1579cec47e3dSKiyoshi Ueda 		/* Some requests still remain, retry later */
1580cec47e3dSKiyoshi Ueda 		blk_plug_device(q);
1581cec47e3dSKiyoshi Ueda 
1582cec47e3dSKiyoshi Ueda out:
1583cec47e3dSKiyoshi Ueda 	dm_table_put(map);
1584cec47e3dSKiyoshi Ueda 
1585cec47e3dSKiyoshi Ueda 	return;
1586cec47e3dSKiyoshi Ueda }
1587cec47e3dSKiyoshi Ueda 
1588cec47e3dSKiyoshi Ueda int dm_underlying_device_busy(struct request_queue *q)
1589cec47e3dSKiyoshi Ueda {
1590cec47e3dSKiyoshi Ueda 	return blk_lld_busy(q);
1591cec47e3dSKiyoshi Ueda }
1592cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1593cec47e3dSKiyoshi Ueda 
1594cec47e3dSKiyoshi Ueda static int dm_lld_busy(struct request_queue *q)
1595cec47e3dSKiyoshi Ueda {
1596cec47e3dSKiyoshi Ueda 	int r;
1597cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1598cec47e3dSKiyoshi Ueda 	struct dm_table *map = dm_get_table(md);
1599cec47e3dSKiyoshi Ueda 
1600cec47e3dSKiyoshi Ueda 	if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1601cec47e3dSKiyoshi Ueda 		r = 1;
1602cec47e3dSKiyoshi Ueda 	else
1603cec47e3dSKiyoshi Ueda 		r = dm_table_any_busy_target(map);
1604cec47e3dSKiyoshi Ueda 
1605cec47e3dSKiyoshi Ueda 	dm_table_put(map);
1606cec47e3dSKiyoshi Ueda 
1607cec47e3dSKiyoshi Ueda 	return r;
1608cec47e3dSKiyoshi Ueda }
1609cec47e3dSKiyoshi Ueda 
1610165125e1SJens Axboe static void dm_unplug_all(struct request_queue *q)
16111da177e4SLinus Torvalds {
16121da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
16131da177e4SLinus Torvalds 	struct dm_table *map = dm_get_table(md);
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds 	if (map) {
1616cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
1617cec47e3dSKiyoshi Ueda 			generic_unplug_device(q);
1618cec47e3dSKiyoshi Ueda 
16191da177e4SLinus Torvalds 		dm_table_unplug_all(map);
16201da177e4SLinus Torvalds 		dm_table_put(map);
16211da177e4SLinus Torvalds 	}
16221da177e4SLinus Torvalds }
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
16251da177e4SLinus Torvalds {
16268a57dfc6SChandra Seetharaman 	int r = bdi_bits;
16278a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
16288a57dfc6SChandra Seetharaman 	struct dm_table *map;
16291da177e4SLinus Torvalds 
16301eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
16318a57dfc6SChandra Seetharaman 		map = dm_get_table(md);
16328a57dfc6SChandra Seetharaman 		if (map) {
1633cec47e3dSKiyoshi Ueda 			/*
1634cec47e3dSKiyoshi Ueda 			 * Request-based dm cares about only own queue for
1635cec47e3dSKiyoshi Ueda 			 * the query about congestion status of request_queue
1636cec47e3dSKiyoshi Ueda 			 */
1637cec47e3dSKiyoshi Ueda 			if (dm_request_based(md))
1638cec47e3dSKiyoshi Ueda 				r = md->queue->backing_dev_info.state &
1639cec47e3dSKiyoshi Ueda 				    bdi_bits;
1640cec47e3dSKiyoshi Ueda 			else
16411da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
1642cec47e3dSKiyoshi Ueda 
16431da177e4SLinus Torvalds 			dm_table_put(map);
16448a57dfc6SChandra Seetharaman 		}
16458a57dfc6SChandra Seetharaman 	}
16468a57dfc6SChandra Seetharaman 
16471da177e4SLinus Torvalds 	return r;
16481da177e4SLinus Torvalds }
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds /*-----------------------------------------------------------------
16511da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
16521da177e4SLinus Torvalds  *---------------------------------------------------------------*/
16531da177e4SLinus Torvalds static DEFINE_IDR(_minor_idr);
16541da177e4SLinus Torvalds 
16552b06cfffSAlasdair G Kergon static void free_minor(int minor)
16561da177e4SLinus Torvalds {
1657f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
16581da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1659f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
16601da177e4SLinus Torvalds }
16611da177e4SLinus Torvalds 
16621da177e4SLinus Torvalds /*
16631da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
16641da177e4SLinus Torvalds  */
1665cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
16661da177e4SLinus Torvalds {
16671da177e4SLinus Torvalds 	int r, m;
16681da177e4SLinus Torvalds 
16691da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
16701da177e4SLinus Torvalds 		return -EINVAL;
16711da177e4SLinus Torvalds 
167262f75c2fSJeff Mahoney 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
167362f75c2fSJeff Mahoney 	if (!r)
167462f75c2fSJeff Mahoney 		return -ENOMEM;
167562f75c2fSJeff Mahoney 
1676f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
16771da177e4SLinus Torvalds 
16781da177e4SLinus Torvalds 	if (idr_find(&_minor_idr, minor)) {
16791da177e4SLinus Torvalds 		r = -EBUSY;
16801da177e4SLinus Torvalds 		goto out;
16811da177e4SLinus Torvalds 	}
16821da177e4SLinus Torvalds 
1683ba61fdd1SJeff Mahoney 	r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
168462f75c2fSJeff Mahoney 	if (r)
16851da177e4SLinus Torvalds 		goto out;
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds 	if (m != minor) {
16881da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
16891da177e4SLinus Torvalds 		r = -EBUSY;
16901da177e4SLinus Torvalds 		goto out;
16911da177e4SLinus Torvalds 	}
16921da177e4SLinus Torvalds 
16931da177e4SLinus Torvalds out:
1694f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
16951da177e4SLinus Torvalds 	return r;
16961da177e4SLinus Torvalds }
16971da177e4SLinus Torvalds 
1698cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
16991da177e4SLinus Torvalds {
17002b06cfffSAlasdair G Kergon 	int r, m;
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
170362f75c2fSJeff Mahoney 	if (!r)
170462f75c2fSJeff Mahoney 		return -ENOMEM;
170562f75c2fSJeff Mahoney 
1706f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17071da177e4SLinus Torvalds 
1708ba61fdd1SJeff Mahoney 	r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
1709cf13ab8eSFrederik Deweerdt 	if (r)
17101da177e4SLinus Torvalds 		goto out;
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	if (m >= (1 << MINORBITS)) {
17131da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
17141da177e4SLinus Torvalds 		r = -ENOSPC;
17151da177e4SLinus Torvalds 		goto out;
17161da177e4SLinus Torvalds 	}
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds 	*minor = m;
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds out:
1721f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
17221da177e4SLinus Torvalds 	return r;
17231da177e4SLinus Torvalds }
17241da177e4SLinus Torvalds 
172583d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
17261da177e4SLinus Torvalds 
172753d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
172853d5914fSMikulas Patocka 
17291da177e4SLinus Torvalds /*
17301da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
17311da177e4SLinus Torvalds  */
17322b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
17331da177e4SLinus Torvalds {
17341da177e4SLinus Torvalds 	int r;
1735cf13ab8eSFrederik Deweerdt 	struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1736ba61fdd1SJeff Mahoney 	void *old_md;
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds 	if (!md) {
17391da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
17401da177e4SLinus Torvalds 		return NULL;
17411da177e4SLinus Torvalds 	}
17421da177e4SLinus Torvalds 
174310da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
17446ed7ade8SMilan Broz 		goto bad_module_get;
174510da4f79SJeff Mahoney 
17461da177e4SLinus Torvalds 	/* get a minor number for the dev */
17472b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1748cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
17492b06cfffSAlasdair G Kergon 	else
1750cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
17511da177e4SLinus Torvalds 	if (r < 0)
17526ed7ade8SMilan Broz 		goto bad_minor;
17531da177e4SLinus Torvalds 
17542ca3310eSAlasdair G Kergon 	init_rwsem(&md->io_lock);
1755e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1756022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
17571da177e4SLinus Torvalds 	rwlock_init(&md->map_lock);
17581da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
17595c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
17601da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
17617a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
17627a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
17637a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
17641da177e4SLinus Torvalds 
1765e6ee8c0bSKiyoshi Ueda 	md->queue = blk_init_queue(dm_request_fn, NULL);
17661da177e4SLinus Torvalds 	if (!md->queue)
17676ed7ade8SMilan Broz 		goto bad_queue;
17681da177e4SLinus Torvalds 
1769e6ee8c0bSKiyoshi Ueda 	/*
1770e6ee8c0bSKiyoshi Ueda 	 * Request-based dm devices cannot be stacked on top of bio-based dm
1771e6ee8c0bSKiyoshi Ueda 	 * devices.  The type of this dm device has not been decided yet,
1772e6ee8c0bSKiyoshi Ueda 	 * although we initialized the queue using blk_init_queue().
1773e6ee8c0bSKiyoshi Ueda 	 * The type is decided at the first table loading time.
1774e6ee8c0bSKiyoshi Ueda 	 * To prevent problematic device stacking, clear the queue flag
1775e6ee8c0bSKiyoshi Ueda 	 * for request stacking support until then.
1776e6ee8c0bSKiyoshi Ueda 	 *
1777e6ee8c0bSKiyoshi Ueda 	 * This queue is new, so no concurrency on the queue_flags.
1778e6ee8c0bSKiyoshi Ueda 	 */
1779e6ee8c0bSKiyoshi Ueda 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
1780e6ee8c0bSKiyoshi Ueda 	md->saved_make_request_fn = md->queue->make_request_fn;
17811da177e4SLinus Torvalds 	md->queue->queuedata = md;
17821da177e4SLinus Torvalds 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
17831da177e4SLinus Torvalds 	md->queue->backing_dev_info.congested_data = md;
17841da177e4SLinus Torvalds 	blk_queue_make_request(md->queue, dm_request);
1785daef265fSJens Axboe 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
17861da177e4SLinus Torvalds 	md->queue->unplug_fn = dm_unplug_all;
1787f6fccb12SMilan Broz 	blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1788e6ee8c0bSKiyoshi Ueda 	blk_queue_softirq_done(md->queue, dm_softirq_done);
1789e6ee8c0bSKiyoshi Ueda 	blk_queue_prep_rq(md->queue, dm_prep_fn);
1790e6ee8c0bSKiyoshi Ueda 	blk_queue_lld_busy(md->queue, dm_lld_busy);
17919faf400fSStefan Bader 
17921da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
17931da177e4SLinus Torvalds 	if (!md->disk)
17946ed7ade8SMilan Broz 		goto bad_disk;
17951da177e4SLinus Torvalds 
1796316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
1797316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
1798f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
179953d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1800f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
1801f0b04115SJeff Mahoney 
18021da177e4SLinus Torvalds 	md->disk->major = _major;
18031da177e4SLinus Torvalds 	md->disk->first_minor = minor;
18041da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
18051da177e4SLinus Torvalds 	md->disk->queue = md->queue;
18061da177e4SLinus Torvalds 	md->disk->private_data = md;
18071da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
18081da177e4SLinus Torvalds 	add_disk(md->disk);
18097e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
18101da177e4SLinus Torvalds 
1811304f3f6aSMilan Broz 	md->wq = create_singlethread_workqueue("kdmflush");
1812304f3f6aSMilan Broz 	if (!md->wq)
1813304f3f6aSMilan Broz 		goto bad_thread;
1814304f3f6aSMilan Broz 
181532a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
181632a926daSMikulas Patocka 	if (!md->bdev)
181732a926daSMikulas Patocka 		goto bad_bdev;
181832a926daSMikulas Patocka 
1819ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1820f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1821ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1822f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1823ba61fdd1SJeff Mahoney 
1824ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1825ba61fdd1SJeff Mahoney 
18261da177e4SLinus Torvalds 	return md;
18271da177e4SLinus Torvalds 
182832a926daSMikulas Patocka bad_bdev:
182932a926daSMikulas Patocka 	destroy_workqueue(md->wq);
1830304f3f6aSMilan Broz bad_thread:
183103022c54SZdenek Kabelac 	del_gendisk(md->disk);
1832304f3f6aSMilan Broz 	put_disk(md->disk);
18336ed7ade8SMilan Broz bad_disk:
18341312f40eSAl Viro 	blk_cleanup_queue(md->queue);
18356ed7ade8SMilan Broz bad_queue:
18361da177e4SLinus Torvalds 	free_minor(minor);
18376ed7ade8SMilan Broz bad_minor:
183810da4f79SJeff Mahoney 	module_put(THIS_MODULE);
18396ed7ade8SMilan Broz bad_module_get:
18401da177e4SLinus Torvalds 	kfree(md);
18411da177e4SLinus Torvalds 	return NULL;
18421da177e4SLinus Torvalds }
18431da177e4SLinus Torvalds 
1844ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1845ae9da83fSJun'ichi Nomura 
18461da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
18471da177e4SLinus Torvalds {
1848f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
184963d94e48SJun'ichi Nomura 
1850ae9da83fSJun'ichi Nomura 	unlock_fs(md);
1851db8fef4fSMikulas Patocka 	bdput(md->bdev);
1852304f3f6aSMilan Broz 	destroy_workqueue(md->wq);
1853e6ee8c0bSKiyoshi Ueda 	if (md->tio_pool)
18541da177e4SLinus Torvalds 		mempool_destroy(md->tio_pool);
1855e6ee8c0bSKiyoshi Ueda 	if (md->io_pool)
18561da177e4SLinus Torvalds 		mempool_destroy(md->io_pool);
1857e6ee8c0bSKiyoshi Ueda 	if (md->bs)
18589faf400fSStefan Bader 		bioset_free(md->bs);
18599c47008dSMartin K. Petersen 	blk_integrity_unregister(md->disk);
18601da177e4SLinus Torvalds 	del_gendisk(md->disk);
186163d94e48SJun'ichi Nomura 	free_minor(minor);
1862fba9f90eSJeff Mahoney 
1863fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
1864fba9f90eSJeff Mahoney 	md->disk->private_data = NULL;
1865fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
1866fba9f90eSJeff Mahoney 
18671da177e4SLinus Torvalds 	put_disk(md->disk);
18681312f40eSAl Viro 	blk_cleanup_queue(md->queue);
186910da4f79SJeff Mahoney 	module_put(THIS_MODULE);
18701da177e4SLinus Torvalds 	kfree(md);
18711da177e4SLinus Torvalds }
18721da177e4SLinus Torvalds 
1873e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1874e6ee8c0bSKiyoshi Ueda {
1875e6ee8c0bSKiyoshi Ueda 	struct dm_md_mempools *p;
1876e6ee8c0bSKiyoshi Ueda 
1877e6ee8c0bSKiyoshi Ueda 	if (md->io_pool && md->tio_pool && md->bs)
1878e6ee8c0bSKiyoshi Ueda 		/* the md already has necessary mempools */
1879e6ee8c0bSKiyoshi Ueda 		goto out;
1880e6ee8c0bSKiyoshi Ueda 
1881e6ee8c0bSKiyoshi Ueda 	p = dm_table_get_md_mempools(t);
1882e6ee8c0bSKiyoshi Ueda 	BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
1883e6ee8c0bSKiyoshi Ueda 
1884e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
1885e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
1886e6ee8c0bSKiyoshi Ueda 	md->tio_pool = p->tio_pool;
1887e6ee8c0bSKiyoshi Ueda 	p->tio_pool = NULL;
1888e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
1889e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
1890e6ee8c0bSKiyoshi Ueda 
1891e6ee8c0bSKiyoshi Ueda out:
1892e6ee8c0bSKiyoshi Ueda 	/* mempool bind completed, now no need any mempools in the table */
1893e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
1894e6ee8c0bSKiyoshi Ueda }
1895e6ee8c0bSKiyoshi Ueda 
18961da177e4SLinus Torvalds /*
18971da177e4SLinus Torvalds  * Bind a table to the device.
18981da177e4SLinus Torvalds  */
18991da177e4SLinus Torvalds static void event_callback(void *context)
19001da177e4SLinus Torvalds {
19017a8c3d3bSMike Anderson 	unsigned long flags;
19027a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
19031da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
19041da177e4SLinus Torvalds 
19057a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
19067a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
19077a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
19087a8c3d3bSMike Anderson 
1909ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
19107a8c3d3bSMike Anderson 
19111da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
19121da177e4SLinus Torvalds 	wake_up(&md->eventq);
19131da177e4SLinus Torvalds }
19141da177e4SLinus Torvalds 
19154e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
19161da177e4SLinus Torvalds {
19174e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
19181da177e4SLinus Torvalds 
1919db8fef4fSMikulas Patocka 	mutex_lock(&md->bdev->bd_inode->i_mutex);
1920db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1921db8fef4fSMikulas Patocka 	mutex_unlock(&md->bdev->bd_inode->i_mutex);
19221da177e4SLinus Torvalds }
19231da177e4SLinus Torvalds 
1924754c5fc7SMike Snitzer static int __bind(struct mapped_device *md, struct dm_table *t,
1925754c5fc7SMike Snitzer 		  struct queue_limits *limits)
19261da177e4SLinus Torvalds {
1927165125e1SJens Axboe 	struct request_queue *q = md->queue;
19281da177e4SLinus Torvalds 	sector_t size;
1929523d9297SKiyoshi Ueda 	unsigned long flags;
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds 	size = dm_table_get_size(t);
19323ac51e74SDarrick J. Wong 
19333ac51e74SDarrick J. Wong 	/*
19343ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
19353ac51e74SDarrick J. Wong 	 */
19363ac51e74SDarrick J. Wong 	if (size != get_capacity(md->disk))
19373ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
19383ac51e74SDarrick J. Wong 
19394e90188bSAlasdair G Kergon 	__set_size(md, size);
19401da177e4SLinus Torvalds 
1941d5816876SMikulas Patocka 	if (!size) {
1942d5816876SMikulas Patocka 		dm_table_destroy(t);
1943d5816876SMikulas Patocka 		return 0;
1944d5816876SMikulas Patocka 	}
1945d5816876SMikulas Patocka 
1946cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
19472ca3310eSAlasdair G Kergon 
1948e6ee8c0bSKiyoshi Ueda 	/*
1949e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
1950e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
1951e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
1952e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
1953e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
1954e6ee8c0bSKiyoshi Ueda 	 */
1955e6ee8c0bSKiyoshi Ueda 	if (dm_table_request_based(t) && !blk_queue_stopped(q))
1956e6ee8c0bSKiyoshi Ueda 		stop_queue(q);
1957e6ee8c0bSKiyoshi Ueda 
1958e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
1959e6ee8c0bSKiyoshi Ueda 
1960523d9297SKiyoshi Ueda 	write_lock_irqsave(&md->map_lock, flags);
19612ca3310eSAlasdair G Kergon 	md->map = t;
1962754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
1963523d9297SKiyoshi Ueda 	write_unlock_irqrestore(&md->map_lock, flags);
19642ca3310eSAlasdair G Kergon 
19651da177e4SLinus Torvalds 	return 0;
19661da177e4SLinus Torvalds }
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds static void __unbind(struct mapped_device *md)
19691da177e4SLinus Torvalds {
19701da177e4SLinus Torvalds 	struct dm_table *map = md->map;
1971523d9297SKiyoshi Ueda 	unsigned long flags;
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds 	if (!map)
19741da177e4SLinus Torvalds 		return;
19751da177e4SLinus Torvalds 
19761da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
1977523d9297SKiyoshi Ueda 	write_lock_irqsave(&md->map_lock, flags);
19781da177e4SLinus Torvalds 	md->map = NULL;
1979523d9297SKiyoshi Ueda 	write_unlock_irqrestore(&md->map_lock, flags);
1980d5816876SMikulas Patocka 	dm_table_destroy(map);
19811da177e4SLinus Torvalds }
19821da177e4SLinus Torvalds 
19831da177e4SLinus Torvalds /*
19841da177e4SLinus Torvalds  * Constructor for a new device.
19851da177e4SLinus Torvalds  */
19862b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
19871da177e4SLinus Torvalds {
19881da177e4SLinus Torvalds 	struct mapped_device *md;
19891da177e4SLinus Torvalds 
19902b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
19911da177e4SLinus Torvalds 	if (!md)
19921da177e4SLinus Torvalds 		return -ENXIO;
19931da177e4SLinus Torvalds 
1994784aae73SMilan Broz 	dm_sysfs_init(md);
1995784aae73SMilan Broz 
19961da177e4SLinus Torvalds 	*result = md;
19971da177e4SLinus Torvalds 	return 0;
19981da177e4SLinus Torvalds }
19991da177e4SLinus Torvalds 
2000637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev)
20011da177e4SLinus Torvalds {
20021da177e4SLinus Torvalds 	struct mapped_device *md;
20031da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
20041da177e4SLinus Torvalds 
20051da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
20061da177e4SLinus Torvalds 		return NULL;
20071da177e4SLinus Torvalds 
2008f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
20091da177e4SLinus Torvalds 
20101da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
2011fba9f90eSJeff Mahoney 	if (md && (md == MINOR_ALLOCED ||
2012f331c029STejun Heo 		   (MINOR(disk_devt(dm_disk(md))) != minor) ||
2013fba9f90eSJeff Mahoney 		   test_bit(DMF_FREEING, &md->flags))) {
2014637842cfSDavid Teigland 		md = NULL;
2015fba9f90eSJeff Mahoney 		goto out;
2016fba9f90eSJeff Mahoney 	}
20171da177e4SLinus Torvalds 
2018fba9f90eSJeff Mahoney out:
2019f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
20201da177e4SLinus Torvalds 
2021637842cfSDavid Teigland 	return md;
2022637842cfSDavid Teigland }
2023637842cfSDavid Teigland 
2024d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev)
2025d229a958SDavid Teigland {
2026d229a958SDavid Teigland 	struct mapped_device *md = dm_find_md(dev);
2027d229a958SDavid Teigland 
2028d229a958SDavid Teigland 	if (md)
2029d229a958SDavid Teigland 		dm_get(md);
2030d229a958SDavid Teigland 
2031d229a958SDavid Teigland 	return md;
2032d229a958SDavid Teigland }
2033d229a958SDavid Teigland 
20349ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2035637842cfSDavid Teigland {
20369ade92a9SAlasdair G Kergon 	return md->interface_ptr;
20371da177e4SLinus Torvalds }
20381da177e4SLinus Torvalds 
20391da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
20401da177e4SLinus Torvalds {
20411da177e4SLinus Torvalds 	md->interface_ptr = ptr;
20421da177e4SLinus Torvalds }
20431da177e4SLinus Torvalds 
20441da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
20451da177e4SLinus Torvalds {
20461da177e4SLinus Torvalds 	atomic_inc(&md->holders);
20471da177e4SLinus Torvalds }
20481da177e4SLinus Torvalds 
204972d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
205072d94861SAlasdair G Kergon {
205172d94861SAlasdair G Kergon 	return md->name;
205272d94861SAlasdair G Kergon }
205372d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
205472d94861SAlasdair G Kergon 
20551da177e4SLinus Torvalds void dm_put(struct mapped_device *md)
20561da177e4SLinus Torvalds {
20571134e5aeSMike Anderson 	struct dm_table *map;
20581da177e4SLinus Torvalds 
2059fba9f90eSJeff Mahoney 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
2060fba9f90eSJeff Mahoney 
2061f32c10b0SJeff Mahoney 	if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
20621134e5aeSMike Anderson 		map = dm_get_table(md);
2063f331c029STejun Heo 		idr_replace(&_minor_idr, MINOR_ALLOCED,
2064f331c029STejun Heo 			    MINOR(disk_devt(dm_disk(md))));
2065fba9f90eSJeff Mahoney 		set_bit(DMF_FREEING, &md->flags);
2066f32c10b0SJeff Mahoney 		spin_unlock(&_minor_lock);
2067cf222b37SAlasdair G Kergon 		if (!dm_suspended(md)) {
20681da177e4SLinus Torvalds 			dm_table_presuspend_targets(map);
20691da177e4SLinus Torvalds 			dm_table_postsuspend_targets(map);
20701da177e4SLinus Torvalds 		}
2071784aae73SMilan Broz 		dm_sysfs_exit(md);
20721134e5aeSMike Anderson 		dm_table_put(map);
2073a1b51e98SMikulas Patocka 		__unbind(md);
20741da177e4SLinus Torvalds 		free_dev(md);
20751da177e4SLinus Torvalds 	}
20761da177e4SLinus Torvalds }
207779eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
20781da177e4SLinus Torvalds 
2079401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
208046125c1cSMilan Broz {
208146125c1cSMilan Broz 	int r = 0;
2082b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
2083cec47e3dSKiyoshi Ueda 	struct request_queue *q = md->queue;
2084cec47e3dSKiyoshi Ueda 	unsigned long flags;
2085b44ebeb0SMikulas Patocka 
2086b44ebeb0SMikulas Patocka 	dm_unplug_all(md->queue);
2087b44ebeb0SMikulas Patocka 
2088b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
208946125c1cSMilan Broz 
209046125c1cSMilan Broz 	while (1) {
2091401600dfSMikulas Patocka 		set_current_state(interruptible);
209246125c1cSMilan Broz 
209346125c1cSMilan Broz 		smp_mb();
2094cec47e3dSKiyoshi Ueda 		if (dm_request_based(md)) {
2095cec47e3dSKiyoshi Ueda 			spin_lock_irqsave(q->queue_lock, flags);
2096cec47e3dSKiyoshi Ueda 			if (!queue_in_flight(q) && blk_queue_stopped(q)) {
2097cec47e3dSKiyoshi Ueda 				spin_unlock_irqrestore(q->queue_lock, flags);
2098cec47e3dSKiyoshi Ueda 				break;
2099cec47e3dSKiyoshi Ueda 			}
2100cec47e3dSKiyoshi Ueda 			spin_unlock_irqrestore(q->queue_lock, flags);
2101316d315bSNikanth Karthikesan 		} else if (!atomic_read(&md->pending[0]) &&
2102316d315bSNikanth Karthikesan 					!atomic_read(&md->pending[1]))
210346125c1cSMilan Broz 			break;
210446125c1cSMilan Broz 
2105401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
2106401600dfSMikulas Patocka 		    signal_pending(current)) {
210746125c1cSMilan Broz 			r = -EINTR;
210846125c1cSMilan Broz 			break;
210946125c1cSMilan Broz 		}
211046125c1cSMilan Broz 
211146125c1cSMilan Broz 		io_schedule();
211246125c1cSMilan Broz 	}
211346125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
211446125c1cSMilan Broz 
2115b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
2116b44ebeb0SMikulas Patocka 
211746125c1cSMilan Broz 	return r;
211846125c1cSMilan Broz }
211946125c1cSMilan Broz 
2120531fe963SMikulas Patocka static void dm_flush(struct mapped_device *md)
2121af7e466aSMikulas Patocka {
2122af7e466aSMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
212352b1fd5aSMikulas Patocka 
212452b1fd5aSMikulas Patocka 	bio_init(&md->barrier_bio);
212552b1fd5aSMikulas Patocka 	md->barrier_bio.bi_bdev = md->bdev;
212652b1fd5aSMikulas Patocka 	md->barrier_bio.bi_rw = WRITE_BARRIER;
212752b1fd5aSMikulas Patocka 	__split_and_process_bio(md, &md->barrier_bio);
212852b1fd5aSMikulas Patocka 
212952b1fd5aSMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2130af7e466aSMikulas Patocka }
2131af7e466aSMikulas Patocka 
2132af7e466aSMikulas Patocka static void process_barrier(struct mapped_device *md, struct bio *bio)
2133af7e466aSMikulas Patocka {
21345aa2781dSMikulas Patocka 	md->barrier_error = 0;
21355aa2781dSMikulas Patocka 
2136531fe963SMikulas Patocka 	dm_flush(md);
2137af7e466aSMikulas Patocka 
21385aa2781dSMikulas Patocka 	if (!bio_empty_barrier(bio)) {
2139af7e466aSMikulas Patocka 		__split_and_process_bio(md, bio);
2140531fe963SMikulas Patocka 		dm_flush(md);
21415aa2781dSMikulas Patocka 	}
2142af7e466aSMikulas Patocka 
2143af7e466aSMikulas Patocka 	if (md->barrier_error != DM_ENDIO_REQUEUE)
2144531fe963SMikulas Patocka 		bio_endio(bio, md->barrier_error);
21452761e95fSMikulas Patocka 	else {
21462761e95fSMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
21472761e95fSMikulas Patocka 		bio_list_add_head(&md->deferred, bio);
21482761e95fSMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
21492761e95fSMikulas Patocka 	}
2150af7e466aSMikulas Patocka }
2151af7e466aSMikulas Patocka 
21521da177e4SLinus Torvalds /*
21531da177e4SLinus Torvalds  * Process the deferred bios
21541da177e4SLinus Torvalds  */
2155ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
21561da177e4SLinus Torvalds {
2157ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2158ef208587SMikulas Patocka 						work);
21596d6f10dfSMilan Broz 	struct bio *c;
21601da177e4SLinus Torvalds 
2161ef208587SMikulas Patocka 	down_write(&md->io_lock);
2162ef208587SMikulas Patocka 
21633b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2164022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2165022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2166022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2167022c2611SMikulas Patocka 
2168df12ee99SAlasdair G Kergon 		if (!c) {
21691eb787ecSAlasdair G Kergon 			clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
2170df12ee99SAlasdair G Kergon 			break;
2171022c2611SMikulas Patocka 		}
217273d410c0SMilan Broz 
21733b00b203SMikulas Patocka 		up_write(&md->io_lock);
21743b00b203SMikulas Patocka 
2175e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2176e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2177e6ee8c0bSKiyoshi Ueda 		else {
21781f98a13fSJens Axboe 			if (bio_rw_flagged(c, BIO_RW_BARRIER))
2179af7e466aSMikulas Patocka 				process_barrier(md, c);
2180af7e466aSMikulas Patocka 			else
2181df12ee99SAlasdair G Kergon 				__split_and_process_bio(md, c);
2182e6ee8c0bSKiyoshi Ueda 		}
21833b00b203SMikulas Patocka 
21843b00b203SMikulas Patocka 		down_write(&md->io_lock);
2185df12ee99SAlasdair G Kergon 	}
2186ef208587SMikulas Patocka 
2187ef208587SMikulas Patocka 	up_write(&md->io_lock);
21881da177e4SLinus Torvalds }
21891da177e4SLinus Torvalds 
21909a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2191304f3f6aSMilan Broz {
21923b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
21933b00b203SMikulas Patocka 	smp_mb__after_clear_bit();
219453d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2195304f3f6aSMilan Broz }
2196304f3f6aSMilan Broz 
21971da177e4SLinus Torvalds /*
21981da177e4SLinus Torvalds  * Swap in a new table (destroying old one).
21991da177e4SLinus Torvalds  */
22001da177e4SLinus Torvalds int dm_swap_table(struct mapped_device *md, struct dm_table *table)
22011da177e4SLinus Torvalds {
2202754c5fc7SMike Snitzer 	struct queue_limits limits;
220393c534aeSAlasdair G Kergon 	int r = -EINVAL;
22041da177e4SLinus Torvalds 
2205e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
22061da177e4SLinus Torvalds 
22071da177e4SLinus Torvalds 	/* device must be suspended */
2208cf222b37SAlasdair G Kergon 	if (!dm_suspended(md))
220993c534aeSAlasdair G Kergon 		goto out;
22101da177e4SLinus Torvalds 
2211754c5fc7SMike Snitzer 	r = dm_calculate_queue_limits(table, &limits);
2212754c5fc7SMike Snitzer 	if (r)
2213754c5fc7SMike Snitzer 		goto out;
2214754c5fc7SMike Snitzer 
2215e6ee8c0bSKiyoshi Ueda 	/* cannot change the device type, once a table is bound */
2216e6ee8c0bSKiyoshi Ueda 	if (md->map &&
2217e6ee8c0bSKiyoshi Ueda 	    (dm_table_get_type(md->map) != dm_table_get_type(table))) {
2218e6ee8c0bSKiyoshi Ueda 		DMWARN("can't change the device type after a table is bound");
2219e6ee8c0bSKiyoshi Ueda 		goto out;
2220e6ee8c0bSKiyoshi Ueda 	}
2221e6ee8c0bSKiyoshi Ueda 
22221da177e4SLinus Torvalds 	__unbind(md);
2223754c5fc7SMike Snitzer 	r = __bind(md, table, &limits);
22241da177e4SLinus Torvalds 
222593c534aeSAlasdair G Kergon out:
2226e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
222793c534aeSAlasdair G Kergon 	return r;
22281da177e4SLinus Torvalds }
22291da177e4SLinus Torvalds 
2230cec47e3dSKiyoshi Ueda static void dm_rq_invalidate_suspend_marker(struct mapped_device *md)
2231cec47e3dSKiyoshi Ueda {
2232cec47e3dSKiyoshi Ueda 	md->suspend_rq.special = (void *)0x1;
2233cec47e3dSKiyoshi Ueda }
2234cec47e3dSKiyoshi Ueda 
2235cec47e3dSKiyoshi Ueda static void dm_rq_abort_suspend(struct mapped_device *md, int noflush)
2236cec47e3dSKiyoshi Ueda {
2237cec47e3dSKiyoshi Ueda 	struct request_queue *q = md->queue;
2238cec47e3dSKiyoshi Ueda 	unsigned long flags;
2239cec47e3dSKiyoshi Ueda 
2240cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
2241cec47e3dSKiyoshi Ueda 	if (!noflush)
2242cec47e3dSKiyoshi Ueda 		dm_rq_invalidate_suspend_marker(md);
2243cec47e3dSKiyoshi Ueda 	__start_queue(q);
2244cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
2245cec47e3dSKiyoshi Ueda }
2246cec47e3dSKiyoshi Ueda 
2247cec47e3dSKiyoshi Ueda static void dm_rq_start_suspend(struct mapped_device *md, int noflush)
2248cec47e3dSKiyoshi Ueda {
2249cec47e3dSKiyoshi Ueda 	struct request *rq = &md->suspend_rq;
2250cec47e3dSKiyoshi Ueda 	struct request_queue *q = md->queue;
2251cec47e3dSKiyoshi Ueda 
2252cec47e3dSKiyoshi Ueda 	if (noflush)
2253cec47e3dSKiyoshi Ueda 		stop_queue(q);
2254cec47e3dSKiyoshi Ueda 	else {
2255cec47e3dSKiyoshi Ueda 		blk_rq_init(q, rq);
2256cec47e3dSKiyoshi Ueda 		blk_insert_request(q, rq, 0, NULL);
2257cec47e3dSKiyoshi Ueda 	}
2258cec47e3dSKiyoshi Ueda }
2259cec47e3dSKiyoshi Ueda 
2260cec47e3dSKiyoshi Ueda static int dm_rq_suspend_available(struct mapped_device *md, int noflush)
2261cec47e3dSKiyoshi Ueda {
2262cec47e3dSKiyoshi Ueda 	int r = 1;
2263cec47e3dSKiyoshi Ueda 	struct request *rq = &md->suspend_rq;
2264cec47e3dSKiyoshi Ueda 	struct request_queue *q = md->queue;
2265cec47e3dSKiyoshi Ueda 	unsigned long flags;
2266cec47e3dSKiyoshi Ueda 
2267cec47e3dSKiyoshi Ueda 	if (noflush)
2268cec47e3dSKiyoshi Ueda 		return r;
2269cec47e3dSKiyoshi Ueda 
2270cec47e3dSKiyoshi Ueda 	/* The marker must be protected by queue lock if it is in use */
2271cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
2272cec47e3dSKiyoshi Ueda 	if (unlikely(rq->ref_count)) {
2273cec47e3dSKiyoshi Ueda 		/*
2274cec47e3dSKiyoshi Ueda 		 * This can happen, when the previous flush suspend was
2275cec47e3dSKiyoshi Ueda 		 * interrupted, the marker is still in the queue and
2276cec47e3dSKiyoshi Ueda 		 * this flush suspend has been invoked, because we don't
2277cec47e3dSKiyoshi Ueda 		 * remove the marker at the time of suspend interruption.
2278cec47e3dSKiyoshi Ueda 		 * We have only one marker per mapped_device, so we can't
2279cec47e3dSKiyoshi Ueda 		 * start another flush suspend while it is in use.
2280cec47e3dSKiyoshi Ueda 		 */
2281cec47e3dSKiyoshi Ueda 		BUG_ON(!rq->special); /* The marker should be invalidated */
2282cec47e3dSKiyoshi Ueda 		DMWARN("Invalidating the previous flush suspend is still in"
2283cec47e3dSKiyoshi Ueda 		       " progress.  Please retry later.");
2284cec47e3dSKiyoshi Ueda 		r = 0;
2285cec47e3dSKiyoshi Ueda 	}
2286cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
2287cec47e3dSKiyoshi Ueda 
2288cec47e3dSKiyoshi Ueda 	return r;
2289cec47e3dSKiyoshi Ueda }
2290cec47e3dSKiyoshi Ueda 
22911da177e4SLinus Torvalds /*
22921da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
22931da177e4SLinus Torvalds  * device.
22941da177e4SLinus Torvalds  */
22952ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
22961da177e4SLinus Torvalds {
2297e39e2e95SAlasdair G Kergon 	int r;
22981da177e4SLinus Torvalds 
22991da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2300dfbe03f6SAlasdair G Kergon 
2301db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2302dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2303cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2304e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2305e39e2e95SAlasdair G Kergon 		return r;
2306dfbe03f6SAlasdair G Kergon 	}
2307dfbe03f6SAlasdair G Kergon 
2308aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2309aa8d7c2fSAlasdair G Kergon 
23101da177e4SLinus Torvalds 	return 0;
23111da177e4SLinus Torvalds }
23121da177e4SLinus Torvalds 
23132ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
23141da177e4SLinus Torvalds {
2315aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2316aa8d7c2fSAlasdair G Kergon 		return;
2317aa8d7c2fSAlasdair G Kergon 
2318db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
23191da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2320aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
23211da177e4SLinus Torvalds }
23221da177e4SLinus Torvalds 
23231da177e4SLinus Torvalds /*
23241da177e4SLinus Torvalds  * We need to be able to change a mapping table under a mounted
23251da177e4SLinus Torvalds  * filesystem.  For example we might want to move some data in
23261da177e4SLinus Torvalds  * the background.  Before the table can be swapped with
23271da177e4SLinus Torvalds  * dm_bind_table, dm_suspend must be called to flush any in
23281da177e4SLinus Torvalds  * flight bios and ensure that any further io gets deferred.
23291da177e4SLinus Torvalds  */
2330cec47e3dSKiyoshi Ueda /*
2331cec47e3dSKiyoshi Ueda  * Suspend mechanism in request-based dm.
2332cec47e3dSKiyoshi Ueda  *
2333cec47e3dSKiyoshi Ueda  * After the suspend starts, further incoming requests are kept in
2334cec47e3dSKiyoshi Ueda  * the request_queue and deferred.
2335cec47e3dSKiyoshi Ueda  * Remaining requests in the request_queue at the start of suspend are flushed
2336cec47e3dSKiyoshi Ueda  * if it is flush suspend.
2337cec47e3dSKiyoshi Ueda  * The suspend completes when the following conditions have been satisfied,
2338cec47e3dSKiyoshi Ueda  * so wait for it:
2339cec47e3dSKiyoshi Ueda  *    1. q->in_flight is 0 (which means no in_flight request)
2340cec47e3dSKiyoshi Ueda  *    2. queue has been stopped (which means no request dispatching)
2341cec47e3dSKiyoshi Ueda  *
2342cec47e3dSKiyoshi Ueda  *
2343cec47e3dSKiyoshi Ueda  * Noflush suspend
2344cec47e3dSKiyoshi Ueda  * ---------------
2345cec47e3dSKiyoshi Ueda  * Noflush suspend doesn't need to dispatch remaining requests.
2346cec47e3dSKiyoshi Ueda  * So stop the queue immediately.  Then, wait for all in_flight requests
2347cec47e3dSKiyoshi Ueda  * to be completed or requeued.
2348cec47e3dSKiyoshi Ueda  *
2349cec47e3dSKiyoshi Ueda  * To abort noflush suspend, start the queue.
2350cec47e3dSKiyoshi Ueda  *
2351cec47e3dSKiyoshi Ueda  *
2352cec47e3dSKiyoshi Ueda  * Flush suspend
2353cec47e3dSKiyoshi Ueda  * -------------
2354cec47e3dSKiyoshi Ueda  * Flush suspend needs to dispatch remaining requests.  So stop the queue
2355cec47e3dSKiyoshi Ueda  * after the remaining requests are completed. (Requeued request must be also
2356cec47e3dSKiyoshi Ueda  * re-dispatched and completed.  Until then, we can't stop the queue.)
2357cec47e3dSKiyoshi Ueda  *
2358cec47e3dSKiyoshi Ueda  * During flushing the remaining requests, further incoming requests are also
2359cec47e3dSKiyoshi Ueda  * inserted to the same queue.  To distinguish which requests are to be
2360cec47e3dSKiyoshi Ueda  * flushed, we insert a marker request to the queue at the time of starting
2361cec47e3dSKiyoshi Ueda  * flush suspend, like a barrier.
2362cec47e3dSKiyoshi Ueda  * The dispatching is blocked when the marker is found on the top of the queue.
2363cec47e3dSKiyoshi Ueda  * And the queue is stopped when all in_flight requests are completed, since
2364cec47e3dSKiyoshi Ueda  * that means the remaining requests are completely flushed.
2365cec47e3dSKiyoshi Ueda  * Then, the marker is removed from the queue.
2366cec47e3dSKiyoshi Ueda  *
2367cec47e3dSKiyoshi Ueda  * To abort flush suspend, we also need to take care of the marker, not only
2368cec47e3dSKiyoshi Ueda  * starting the queue.
2369cec47e3dSKiyoshi Ueda  * We don't remove the marker forcibly from the queue since it's against
2370cec47e3dSKiyoshi Ueda  * the block-layer manner.  Instead, we put a invalidated mark on the marker.
2371cec47e3dSKiyoshi Ueda  * When the invalidated marker is found on the top of the queue, it is
2372cec47e3dSKiyoshi Ueda  * immediately removed from the queue, so it doesn't block dispatching.
2373cec47e3dSKiyoshi Ueda  * Because we have only one marker per mapped_device, we can't start another
2374cec47e3dSKiyoshi Ueda  * flush suspend until the invalidated marker is removed from the queue.
2375cec47e3dSKiyoshi Ueda  * So fail and return with -EBUSY in such a case.
2376cec47e3dSKiyoshi Ueda  */
2377a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
23781da177e4SLinus Torvalds {
23792ca3310eSAlasdair G Kergon 	struct dm_table *map = NULL;
238046125c1cSMilan Broz 	int r = 0;
2381a3d77d35SKiyoshi Ueda 	int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
23822e93ccc1SKiyoshi Ueda 	int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
23831da177e4SLinus Torvalds 
2384e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
23852ca3310eSAlasdair G Kergon 
238673d410c0SMilan Broz 	if (dm_suspended(md)) {
238773d410c0SMilan Broz 		r = -EINVAL;
2388d287483dSAlasdair G Kergon 		goto out_unlock;
238973d410c0SMilan Broz 	}
23901da177e4SLinus Torvalds 
2391cec47e3dSKiyoshi Ueda 	if (dm_request_based(md) && !dm_rq_suspend_available(md, noflush)) {
2392cec47e3dSKiyoshi Ueda 		r = -EBUSY;
2393cec47e3dSKiyoshi Ueda 		goto out_unlock;
2394cec47e3dSKiyoshi Ueda 	}
2395cec47e3dSKiyoshi Ueda 
23961da177e4SLinus Torvalds 	map = dm_get_table(md);
2397cf222b37SAlasdair G Kergon 
23982e93ccc1SKiyoshi Ueda 	/*
23992e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
24002e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
24012e93ccc1SKiyoshi Ueda 	 */
24022e93ccc1SKiyoshi Ueda 	if (noflush)
24032e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
24042e93ccc1SKiyoshi Ueda 
2405436d4108SAlasdair G Kergon 	/* This does not get reverted if there's an error later. */
24061da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
24071da177e4SLinus Torvalds 
24082e93ccc1SKiyoshi Ueda 	/*
24096d6f10dfSMilan Broz 	 * Flush I/O to the device. noflush supersedes do_lockfs,
24106d6f10dfSMilan Broz 	 * because lock_fs() needs to flush I/Os.
24112e93ccc1SKiyoshi Ueda 	 */
241232a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
24132ca3310eSAlasdair G Kergon 		r = lock_fs(md);
24142ca3310eSAlasdair G Kergon 		if (r)
24152ca3310eSAlasdair G Kergon 			goto out;
2416aa8d7c2fSAlasdair G Kergon 	}
24171da177e4SLinus Torvalds 
24181da177e4SLinus Torvalds 	/*
24193b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
24203b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
24213b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
24223b00b203SMikulas Patocka 	 * dm_wq_work.
24233b00b203SMikulas Patocka 	 *
24243b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
24253b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
24263b00b203SMikulas Patocka 	 * __split_and_process_bio from dm_request, we set
24273b00b203SMikulas Patocka 	 * DMF_QUEUE_IO_TO_THREAD.
24283b00b203SMikulas Patocka 	 *
24293b00b203SMikulas Patocka 	 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
24303b00b203SMikulas Patocka 	 * and call flush_workqueue(md->wq). flush_workqueue will wait until
24313b00b203SMikulas Patocka 	 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
24323b00b203SMikulas Patocka 	 * further calls to __split_and_process_bio from dm_wq_work.
24331da177e4SLinus Torvalds 	 */
24342ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
24351eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
24361eb787ecSAlasdair G Kergon 	set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
24372ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
24381da177e4SLinus Torvalds 
24393b00b203SMikulas Patocka 	flush_workqueue(md->wq);
24403b00b203SMikulas Patocka 
2441cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2442cec47e3dSKiyoshi Ueda 		dm_rq_start_suspend(md, noflush);
2443cec47e3dSKiyoshi Ueda 
24441da177e4SLinus Torvalds 	/*
24453b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
24463b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
24473b00b203SMikulas Patocka 	 * to finish.
24481da177e4SLinus Torvalds 	 */
2449401600dfSMikulas Patocka 	r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
24501da177e4SLinus Torvalds 
24512ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
24526d6f10dfSMilan Broz 	if (noflush)
2453022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
245494d6351eSMilan Broz 	up_write(&md->io_lock);
24552e93ccc1SKiyoshi Ueda 
24561da177e4SLinus Torvalds 	/* were we interrupted ? */
245746125c1cSMilan Broz 	if (r < 0) {
24589a1fb464SMikulas Patocka 		dm_queue_flush(md);
245973d410c0SMilan Broz 
2460cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
2461cec47e3dSKiyoshi Ueda 			dm_rq_abort_suspend(md, noflush);
2462cec47e3dSKiyoshi Ueda 
24632ca3310eSAlasdair G Kergon 		unlock_fs(md);
24642e93ccc1SKiyoshi Ueda 		goto out; /* pushback list is already flushed, so skip flush */
24652ca3310eSAlasdair G Kergon 	}
24662ca3310eSAlasdair G Kergon 
24673b00b203SMikulas Patocka 	/*
24683b00b203SMikulas Patocka 	 * If dm_wait_for_completion returned 0, the device is completely
24693b00b203SMikulas Patocka 	 * quiescent now. There is no request-processing activity. All new
24703b00b203SMikulas Patocka 	 * requests are being added to md->deferred list.
24713b00b203SMikulas Patocka 	 */
24723b00b203SMikulas Patocka 
24732ca3310eSAlasdair G Kergon 	dm_table_postsuspend_targets(map);
24741da177e4SLinus Torvalds 
24751da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
24761da177e4SLinus Torvalds 
24772ca3310eSAlasdair G Kergon out:
24781da177e4SLinus Torvalds 	dm_table_put(map);
2479d287483dSAlasdair G Kergon 
2480d287483dSAlasdair G Kergon out_unlock:
2481e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2482cf222b37SAlasdair G Kergon 	return r;
24831da177e4SLinus Torvalds }
24841da177e4SLinus Torvalds 
24851da177e4SLinus Torvalds int dm_resume(struct mapped_device *md)
24861da177e4SLinus Torvalds {
2487cf222b37SAlasdair G Kergon 	int r = -EINVAL;
2488cf222b37SAlasdair G Kergon 	struct dm_table *map = NULL;
24891da177e4SLinus Torvalds 
2490e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
24912ca3310eSAlasdair G Kergon 	if (!dm_suspended(md))
2492cf222b37SAlasdair G Kergon 		goto out;
2493cf222b37SAlasdair G Kergon 
2494cf222b37SAlasdair G Kergon 	map = dm_get_table(md);
24952ca3310eSAlasdair G Kergon 	if (!map || !dm_table_get_size(map))
2496cf222b37SAlasdair G Kergon 		goto out;
24971da177e4SLinus Torvalds 
24988757b776SMilan Broz 	r = dm_table_resume_targets(map);
24998757b776SMilan Broz 	if (r)
25008757b776SMilan Broz 		goto out;
25012ca3310eSAlasdair G Kergon 
25029a1fb464SMikulas Patocka 	dm_queue_flush(md);
25032ca3310eSAlasdair G Kergon 
2504cec47e3dSKiyoshi Ueda 	/*
2505cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2506cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2507cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2508cec47e3dSKiyoshi Ueda 	 */
2509cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2510cec47e3dSKiyoshi Ueda 		start_queue(md->queue);
2511cec47e3dSKiyoshi Ueda 
25122ca3310eSAlasdair G Kergon 	unlock_fs(md);
25132ca3310eSAlasdair G Kergon 
25142ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
25152ca3310eSAlasdair G Kergon 
25161da177e4SLinus Torvalds 	dm_table_unplug_all(map);
2517cf222b37SAlasdair G Kergon 	r = 0;
2518cf222b37SAlasdair G Kergon out:
2519cf222b37SAlasdair G Kergon 	dm_table_put(map);
2520e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
25212ca3310eSAlasdair G Kergon 
2522cf222b37SAlasdair G Kergon 	return r;
25231da177e4SLinus Torvalds }
25241da177e4SLinus Torvalds 
25251da177e4SLinus Torvalds /*-----------------------------------------------------------------
25261da177e4SLinus Torvalds  * Event notification.
25271da177e4SLinus Torvalds  *---------------------------------------------------------------*/
252860935eb2SMilan Broz void dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
252960935eb2SMilan Broz 		       unsigned cookie)
253069267a30SAlasdair G Kergon {
253160935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
253260935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
253360935eb2SMilan Broz 
253460935eb2SMilan Broz 	if (!cookie)
253560935eb2SMilan Broz 		kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
253660935eb2SMilan Broz 	else {
253760935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
253860935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
253960935eb2SMilan Broz 		kobject_uevent_env(&disk_to_dev(md->disk)->kobj, action, envp);
254060935eb2SMilan Broz 	}
254169267a30SAlasdair G Kergon }
254269267a30SAlasdair G Kergon 
25437a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
25447a8c3d3bSMike Anderson {
25457a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
25467a8c3d3bSMike Anderson }
25477a8c3d3bSMike Anderson 
25481da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
25491da177e4SLinus Torvalds {
25501da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
25511da177e4SLinus Torvalds }
25521da177e4SLinus Torvalds 
25531da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
25541da177e4SLinus Torvalds {
25551da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
25561da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
25571da177e4SLinus Torvalds }
25581da177e4SLinus Torvalds 
25597a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
25607a8c3d3bSMike Anderson {
25617a8c3d3bSMike Anderson 	unsigned long flags;
25627a8c3d3bSMike Anderson 
25637a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
25647a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
25657a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
25667a8c3d3bSMike Anderson }
25677a8c3d3bSMike Anderson 
25681da177e4SLinus Torvalds /*
25691da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
25701da177e4SLinus Torvalds  * count on 'md'.
25711da177e4SLinus Torvalds  */
25721da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
25731da177e4SLinus Torvalds {
25741da177e4SLinus Torvalds 	return md->disk;
25751da177e4SLinus Torvalds }
25761da177e4SLinus Torvalds 
2577784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2578784aae73SMilan Broz {
2579784aae73SMilan Broz 	return &md->kobj;
2580784aae73SMilan Broz }
2581784aae73SMilan Broz 
2582784aae73SMilan Broz /*
2583784aae73SMilan Broz  * struct mapped_device should not be exported outside of dm.c
2584784aae73SMilan Broz  * so use this check to verify that kobj is part of md structure
2585784aae73SMilan Broz  */
2586784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2587784aae73SMilan Broz {
2588784aae73SMilan Broz 	struct mapped_device *md;
2589784aae73SMilan Broz 
2590784aae73SMilan Broz 	md = container_of(kobj, struct mapped_device, kobj);
2591784aae73SMilan Broz 	if (&md->kobj != kobj)
2592784aae73SMilan Broz 		return NULL;
2593784aae73SMilan Broz 
25944d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
25954d89b7b4SMilan Broz 	    test_bit(DMF_DELETING, &md->flags))
25964d89b7b4SMilan Broz 		return NULL;
25974d89b7b4SMilan Broz 
2598784aae73SMilan Broz 	dm_get(md);
2599784aae73SMilan Broz 	return md;
2600784aae73SMilan Broz }
2601784aae73SMilan Broz 
26021da177e4SLinus Torvalds int dm_suspended(struct mapped_device *md)
26031da177e4SLinus Torvalds {
26041da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
26051da177e4SLinus Torvalds }
26061da177e4SLinus Torvalds 
26072e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
26082e93ccc1SKiyoshi Ueda {
26092e93ccc1SKiyoshi Ueda 	struct mapped_device *md = dm_table_get_md(ti->table);
26102e93ccc1SKiyoshi Ueda 	int r = __noflush_suspending(md);
26112e93ccc1SKiyoshi Ueda 
26122e93ccc1SKiyoshi Ueda 	dm_put(md);
26132e93ccc1SKiyoshi Ueda 
26142e93ccc1SKiyoshi Ueda 	return r;
26152e93ccc1SKiyoshi Ueda }
26162e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
26172e93ccc1SKiyoshi Ueda 
2618e6ee8c0bSKiyoshi Ueda struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2619e6ee8c0bSKiyoshi Ueda {
2620e6ee8c0bSKiyoshi Ueda 	struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2621e6ee8c0bSKiyoshi Ueda 
2622e6ee8c0bSKiyoshi Ueda 	if (!pools)
2623e6ee8c0bSKiyoshi Ueda 		return NULL;
2624e6ee8c0bSKiyoshi Ueda 
2625e6ee8c0bSKiyoshi Ueda 	pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2626e6ee8c0bSKiyoshi Ueda 			 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2627e6ee8c0bSKiyoshi Ueda 			 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2628e6ee8c0bSKiyoshi Ueda 	if (!pools->io_pool)
2629e6ee8c0bSKiyoshi Ueda 		goto free_pools_and_out;
2630e6ee8c0bSKiyoshi Ueda 
2631e6ee8c0bSKiyoshi Ueda 	pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2632e6ee8c0bSKiyoshi Ueda 			  mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2633e6ee8c0bSKiyoshi Ueda 			  mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2634e6ee8c0bSKiyoshi Ueda 	if (!pools->tio_pool)
2635e6ee8c0bSKiyoshi Ueda 		goto free_io_pool_and_out;
2636e6ee8c0bSKiyoshi Ueda 
2637e6ee8c0bSKiyoshi Ueda 	pools->bs = (type == DM_TYPE_BIO_BASED) ?
2638e6ee8c0bSKiyoshi Ueda 		    bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2639e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
2640e6ee8c0bSKiyoshi Ueda 		goto free_tio_pool_and_out;
2641e6ee8c0bSKiyoshi Ueda 
2642e6ee8c0bSKiyoshi Ueda 	return pools;
2643e6ee8c0bSKiyoshi Ueda 
2644e6ee8c0bSKiyoshi Ueda free_tio_pool_and_out:
2645e6ee8c0bSKiyoshi Ueda 	mempool_destroy(pools->tio_pool);
2646e6ee8c0bSKiyoshi Ueda 
2647e6ee8c0bSKiyoshi Ueda free_io_pool_and_out:
2648e6ee8c0bSKiyoshi Ueda 	mempool_destroy(pools->io_pool);
2649e6ee8c0bSKiyoshi Ueda 
2650e6ee8c0bSKiyoshi Ueda free_pools_and_out:
2651e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2652e6ee8c0bSKiyoshi Ueda 
2653e6ee8c0bSKiyoshi Ueda 	return NULL;
2654e6ee8c0bSKiyoshi Ueda }
2655e6ee8c0bSKiyoshi Ueda 
2656e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
2657e6ee8c0bSKiyoshi Ueda {
2658e6ee8c0bSKiyoshi Ueda 	if (!pools)
2659e6ee8c0bSKiyoshi Ueda 		return;
2660e6ee8c0bSKiyoshi Ueda 
2661e6ee8c0bSKiyoshi Ueda 	if (pools->io_pool)
2662e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->io_pool);
2663e6ee8c0bSKiyoshi Ueda 
2664e6ee8c0bSKiyoshi Ueda 	if (pools->tio_pool)
2665e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->tio_pool);
2666e6ee8c0bSKiyoshi Ueda 
2667e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
2668e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
2669e6ee8c0bSKiyoshi Ueda 
2670e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2671e6ee8c0bSKiyoshi Ueda }
2672e6ee8c0bSKiyoshi Ueda 
267383d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
26741da177e4SLinus Torvalds 	.open = dm_blk_open,
26751da177e4SLinus Torvalds 	.release = dm_blk_close,
2676aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
26773ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
26781da177e4SLinus Torvalds 	.owner = THIS_MODULE
26791da177e4SLinus Torvalds };
26801da177e4SLinus Torvalds 
26811da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo);
26821da177e4SLinus Torvalds 
26831da177e4SLinus Torvalds /*
26841da177e4SLinus Torvalds  * module hooks
26851da177e4SLinus Torvalds  */
26861da177e4SLinus Torvalds module_init(dm_init);
26871da177e4SLinus Torvalds module_exit(dm_exit);
26881da177e4SLinus Torvalds 
26891da177e4SLinus Torvalds module_param(major, uint, 0);
26901da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
26911da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
26921da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
26931da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2694