xref: /openbmc/linux/drivers/md/dm.c (revision d07335e51df0c6dec202d315fc4f1f7e100eec4e)
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>
223f77316dSKiyoshi Ueda #include <linux/delay.h>
2355782138SLi Zefan 
2455782138SLi Zefan #include <trace/events/block.h>
251da177e4SLinus Torvalds 
2672d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
2772d94861SAlasdair G Kergon 
2860935eb2SMilan Broz /*
2960935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
3060935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
3160935eb2SMilan Broz  */
3260935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
3360935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
3460935eb2SMilan Broz 
352a48fc0aSArnd Bergmann static DEFINE_MUTEX(dm_mutex);
361da177e4SLinus Torvalds static const char *_name = DM_NAME;
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds static unsigned int major = 0;
391da177e4SLinus Torvalds static unsigned int _major = 0;
401da177e4SLinus Torvalds 
41f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
421da177e4SLinus Torvalds /*
438fbf26adSKiyoshi Ueda  * For bio-based dm.
441da177e4SLinus Torvalds  * One of these is allocated per bio.
451da177e4SLinus Torvalds  */
461da177e4SLinus Torvalds struct dm_io {
471da177e4SLinus Torvalds 	struct mapped_device *md;
481da177e4SLinus Torvalds 	int error;
491da177e4SLinus Torvalds 	atomic_t io_count;
506ae2fa67SRichard Kennedy 	struct bio *bio;
513eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
52f88fb981SKiyoshi Ueda 	spinlock_t endio_lock;
531da177e4SLinus Torvalds };
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds /*
568fbf26adSKiyoshi Ueda  * For bio-based dm.
571da177e4SLinus Torvalds  * One of these is allocated per target within a bio.  Hopefully
581da177e4SLinus Torvalds  * this will be simplified out one day.
591da177e4SLinus Torvalds  */
60028867acSAlasdair G Kergon struct dm_target_io {
611da177e4SLinus Torvalds 	struct dm_io *io;
621da177e4SLinus Torvalds 	struct dm_target *ti;
631da177e4SLinus Torvalds 	union map_info info;
641da177e4SLinus Torvalds };
651da177e4SLinus Torvalds 
668fbf26adSKiyoshi Ueda /*
678fbf26adSKiyoshi Ueda  * For request-based dm.
688fbf26adSKiyoshi Ueda  * One of these is allocated per request.
698fbf26adSKiyoshi Ueda  */
708fbf26adSKiyoshi Ueda struct dm_rq_target_io {
718fbf26adSKiyoshi Ueda 	struct mapped_device *md;
728fbf26adSKiyoshi Ueda 	struct dm_target *ti;
738fbf26adSKiyoshi Ueda 	struct request *orig, clone;
748fbf26adSKiyoshi Ueda 	int error;
758fbf26adSKiyoshi Ueda 	union map_info info;
768fbf26adSKiyoshi Ueda };
778fbf26adSKiyoshi Ueda 
788fbf26adSKiyoshi Ueda /*
798fbf26adSKiyoshi Ueda  * For request-based dm.
808fbf26adSKiyoshi Ueda  * One of these is allocated per bio.
818fbf26adSKiyoshi Ueda  */
828fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info {
838fbf26adSKiyoshi Ueda 	struct bio *orig;
84cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio;
858fbf26adSKiyoshi Ueda };
868fbf26adSKiyoshi Ueda 
871da177e4SLinus Torvalds union map_info *dm_get_mapinfo(struct bio *bio)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds 	if (bio && bio->bi_private)
90028867acSAlasdair G Kergon 		return &((struct dm_target_io *)bio->bi_private)->info;
911da177e4SLinus Torvalds 	return NULL;
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
94cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq)
95cec47e3dSKiyoshi Ueda {
96cec47e3dSKiyoshi Ueda 	if (rq && rq->end_io_data)
97cec47e3dSKiyoshi Ueda 		return &((struct dm_rq_target_io *)rq->end_io_data)->info;
98cec47e3dSKiyoshi Ueda 	return NULL;
99cec47e3dSKiyoshi Ueda }
100cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
101cec47e3dSKiyoshi Ueda 
102ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
103ba61fdd1SJeff Mahoney 
1041da177e4SLinus Torvalds /*
1051da177e4SLinus Torvalds  * Bits for the md->flags field.
1061da177e4SLinus Torvalds  */
1071eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
1081da177e4SLinus Torvalds #define DMF_SUSPENDED 1
109aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
110fba9f90eSJeff Mahoney #define DMF_FREEING 3
1115c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
1122e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
1131da177e4SLinus Torvalds 
114304f3f6aSMilan Broz /*
115304f3f6aSMilan Broz  * Work processed by per-device workqueue.
116304f3f6aSMilan Broz  */
1171da177e4SLinus Torvalds struct mapped_device {
1182ca3310eSAlasdair G Kergon 	struct rw_semaphore io_lock;
119e61290a4SDaniel Walker 	struct mutex suspend_lock;
1201da177e4SLinus Torvalds 	rwlock_t map_lock;
1211da177e4SLinus Torvalds 	atomic_t holders;
1225c6bd75dSAlasdair G Kergon 	atomic_t open_count;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	unsigned long flags;
1251da177e4SLinus Torvalds 
126165125e1SJens Axboe 	struct request_queue *queue;
127a5664dadSMike Snitzer 	unsigned type;
1284a0b4ddfSMike Snitzer 	/* Protect queue and type against concurrent access. */
129a5664dadSMike Snitzer 	struct mutex type_lock;
130a5664dadSMike Snitzer 
1311da177e4SLinus Torvalds 	struct gendisk *disk;
1327e51f257SMike Anderson 	char name[16];
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	void *interface_ptr;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	/*
1371da177e4SLinus Torvalds 	 * A list of ios that arrived while we were suspended.
1381da177e4SLinus Torvalds 	 */
139316d315bSNikanth Karthikesan 	atomic_t pending[2];
1401da177e4SLinus Torvalds 	wait_queue_head_t wait;
14153d5914fSMikulas Patocka 	struct work_struct work;
1421da177e4SLinus Torvalds 	struct bio_list deferred;
143022c2611SMikulas Patocka 	spinlock_t deferred_lock;
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	/*
14629e4013dSTejun Heo 	 * Processing queue (flush)
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 	/* For saving the address of __make_request for request based dm */
182cec47e3dSKiyoshi Ueda 	make_request_fn *saved_make_request_fn;
183cec47e3dSKiyoshi Ueda 
184784aae73SMilan Broz 	/* sysfs handle */
185784aae73SMilan Broz 	struct kobject kobj;
18652b1fd5aSMikulas Patocka 
187d87f4c14STejun Heo 	/* zero-length flush that will be cloned and submitted to targets */
188d87f4c14STejun Heo 	struct bio flush_bio;
1891da177e4SLinus Torvalds };
1901da177e4SLinus Torvalds 
191e6ee8c0bSKiyoshi Ueda /*
192e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
193e6ee8c0bSKiyoshi Ueda  */
194e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
195e6ee8c0bSKiyoshi Ueda 	mempool_t *io_pool;
196e6ee8c0bSKiyoshi Ueda 	mempool_t *tio_pool;
197e6ee8c0bSKiyoshi Ueda 	struct bio_set *bs;
198e6ee8c0bSKiyoshi Ueda };
199e6ee8c0bSKiyoshi Ueda 
2001da177e4SLinus Torvalds #define MIN_IOS 256
201e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
202e18b890bSChristoph Lameter static struct kmem_cache *_tio_cache;
2038fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
2048fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_bio_info_cache;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds static int __init local_init(void)
2071da177e4SLinus Torvalds {
20851157b4aSKiyoshi Ueda 	int r = -ENOMEM;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
211028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
2121da177e4SLinus Torvalds 	if (!_io_cache)
21351157b4aSKiyoshi Ueda 		return r;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	/* allocate a slab for the target ios */
216028867acSAlasdair G Kergon 	_tio_cache = KMEM_CACHE(dm_target_io, 0);
21751157b4aSKiyoshi Ueda 	if (!_tio_cache)
21851157b4aSKiyoshi Ueda 		goto out_free_io_cache;
2191da177e4SLinus Torvalds 
2208fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
2218fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
2228fbf26adSKiyoshi Ueda 		goto out_free_tio_cache;
2238fbf26adSKiyoshi Ueda 
2248fbf26adSKiyoshi Ueda 	_rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
2258fbf26adSKiyoshi Ueda 	if (!_rq_bio_info_cache)
2268fbf26adSKiyoshi Ueda 		goto out_free_rq_tio_cache;
2278fbf26adSKiyoshi Ueda 
22851e5b2bdSMike Anderson 	r = dm_uevent_init();
22951157b4aSKiyoshi Ueda 	if (r)
2308fbf26adSKiyoshi Ueda 		goto out_free_rq_bio_info_cache;
23151e5b2bdSMike Anderson 
2321da177e4SLinus Torvalds 	_major = major;
2331da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
23451157b4aSKiyoshi Ueda 	if (r < 0)
23551157b4aSKiyoshi Ueda 		goto out_uevent_exit;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	if (!_major)
2381da177e4SLinus Torvalds 		_major = r;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	return 0;
24151157b4aSKiyoshi Ueda 
24251157b4aSKiyoshi Ueda out_uevent_exit:
24351157b4aSKiyoshi Ueda 	dm_uevent_exit();
2448fbf26adSKiyoshi Ueda out_free_rq_bio_info_cache:
2458fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_bio_info_cache);
2468fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
2478fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
24851157b4aSKiyoshi Ueda out_free_tio_cache:
24951157b4aSKiyoshi Ueda 	kmem_cache_destroy(_tio_cache);
25051157b4aSKiyoshi Ueda out_free_io_cache:
25151157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
25251157b4aSKiyoshi Ueda 
25351157b4aSKiyoshi Ueda 	return r;
2541da177e4SLinus Torvalds }
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds static void local_exit(void)
2571da177e4SLinus Torvalds {
2588fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_bio_info_cache);
2598fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
2601da177e4SLinus Torvalds 	kmem_cache_destroy(_tio_cache);
2611da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
26200d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
26351e5b2bdSMike Anderson 	dm_uevent_exit();
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	_major = 0;
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	DMINFO("cleaned up");
2681da177e4SLinus Torvalds }
2691da177e4SLinus Torvalds 
270b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
2711da177e4SLinus Torvalds 	local_init,
2721da177e4SLinus Torvalds 	dm_target_init,
2731da177e4SLinus Torvalds 	dm_linear_init,
2741da177e4SLinus Torvalds 	dm_stripe_init,
275952b3557SMikulas Patocka 	dm_io_init,
276945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2771da177e4SLinus Torvalds 	dm_interface_init,
2781da177e4SLinus Torvalds };
2791da177e4SLinus Torvalds 
280b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2811da177e4SLinus Torvalds 	local_exit,
2821da177e4SLinus Torvalds 	dm_target_exit,
2831da177e4SLinus Torvalds 	dm_linear_exit,
2841da177e4SLinus Torvalds 	dm_stripe_exit,
285952b3557SMikulas Patocka 	dm_io_exit,
286945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
2871da177e4SLinus Torvalds 	dm_interface_exit,
2881da177e4SLinus Torvalds };
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds static int __init dm_init(void)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	int r, i;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
2971da177e4SLinus Torvalds 		r = _inits[i]();
2981da177e4SLinus Torvalds 		if (r)
2991da177e4SLinus Torvalds 			goto bad;
3001da177e4SLinus Torvalds 	}
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	return 0;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds       bad:
3051da177e4SLinus Torvalds 	while (i--)
3061da177e4SLinus Torvalds 		_exits[i]();
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	return r;
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds static void __exit dm_exit(void)
3121da177e4SLinus Torvalds {
3131da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	while (i--)
3161da177e4SLinus Torvalds 		_exits[i]();
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds /*
3201da177e4SLinus Torvalds  * Block device functions
3211da177e4SLinus Torvalds  */
322432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
323432a212cSMike Anderson {
324432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
325432a212cSMike Anderson }
326432a212cSMike Anderson 
327fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	struct mapped_device *md;
3301da177e4SLinus Torvalds 
3312a48fc0aSArnd Bergmann 	mutex_lock(&dm_mutex);
332fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
333fba9f90eSJeff Mahoney 
334fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
335fba9f90eSJeff Mahoney 	if (!md)
336fba9f90eSJeff Mahoney 		goto out;
337fba9f90eSJeff Mahoney 
3385c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
339432a212cSMike Anderson 	    dm_deleting_md(md)) {
340fba9f90eSJeff Mahoney 		md = NULL;
341fba9f90eSJeff Mahoney 		goto out;
342fba9f90eSJeff Mahoney 	}
343fba9f90eSJeff Mahoney 
3441da177e4SLinus Torvalds 	dm_get(md);
3455c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
346fba9f90eSJeff Mahoney 
347fba9f90eSJeff Mahoney out:
348fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
3492a48fc0aSArnd Bergmann 	mutex_unlock(&dm_mutex);
350fba9f90eSJeff Mahoney 
351fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
354fe5f9f2cSAl Viro static int dm_blk_close(struct gendisk *disk, fmode_t mode)
3551da177e4SLinus Torvalds {
356fe5f9f2cSAl Viro 	struct mapped_device *md = disk->private_data;
3576e9624b8SArnd Bergmann 
3582a48fc0aSArnd Bergmann 	mutex_lock(&dm_mutex);
3595c6bd75dSAlasdair G Kergon 	atomic_dec(&md->open_count);
3601da177e4SLinus Torvalds 	dm_put(md);
3612a48fc0aSArnd Bergmann 	mutex_unlock(&dm_mutex);
3626e9624b8SArnd Bergmann 
3631da177e4SLinus Torvalds 	return 0;
3641da177e4SLinus Torvalds }
3651da177e4SLinus Torvalds 
3665c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3675c6bd75dSAlasdair G Kergon {
3685c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3695c6bd75dSAlasdair G Kergon }
3705c6bd75dSAlasdair G Kergon 
3715c6bd75dSAlasdair G Kergon /*
3725c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3735c6bd75dSAlasdair G Kergon  */
3745c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md)
3755c6bd75dSAlasdair G Kergon {
3765c6bd75dSAlasdair G Kergon 	int r = 0;
3775c6bd75dSAlasdair G Kergon 
3785c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
3795c6bd75dSAlasdair G Kergon 
3805c6bd75dSAlasdair G Kergon 	if (dm_open_count(md))
3815c6bd75dSAlasdair G Kergon 		r = -EBUSY;
3825c6bd75dSAlasdair G Kergon 	else
3835c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
3845c6bd75dSAlasdair G Kergon 
3855c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
3865c6bd75dSAlasdair G Kergon 
3875c6bd75dSAlasdair G Kergon 	return r;
3885c6bd75dSAlasdair G Kergon }
3895c6bd75dSAlasdair G Kergon 
3903ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3913ac51e74SDarrick J. Wong {
3923ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
3933ac51e74SDarrick J. Wong 
3943ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
3953ac51e74SDarrick J. Wong }
3963ac51e74SDarrick J. Wong 
397fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
398aa129a22SMilan Broz 			unsigned int cmd, unsigned long arg)
399aa129a22SMilan Broz {
400fe5f9f2cSAl Viro 	struct mapped_device *md = bdev->bd_disk->private_data;
4017c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
402aa129a22SMilan Broz 	struct dm_target *tgt;
403aa129a22SMilan Broz 	int r = -ENOTTY;
404aa129a22SMilan Broz 
405aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
406aa129a22SMilan Broz 		goto out;
407aa129a22SMilan Broz 
408aa129a22SMilan Broz 	/* We only support devices that have a single target */
409aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
410aa129a22SMilan Broz 		goto out;
411aa129a22SMilan Broz 
412aa129a22SMilan Broz 	tgt = dm_table_get_target(map, 0);
413aa129a22SMilan Broz 
4144f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
415aa129a22SMilan Broz 		r = -EAGAIN;
416aa129a22SMilan Broz 		goto out;
417aa129a22SMilan Broz 	}
418aa129a22SMilan Broz 
419aa129a22SMilan Broz 	if (tgt->type->ioctl)
420647b3d00SAl Viro 		r = tgt->type->ioctl(tgt, cmd, arg);
421aa129a22SMilan Broz 
422aa129a22SMilan Broz out:
423aa129a22SMilan Broz 	dm_table_put(map);
424aa129a22SMilan Broz 
425aa129a22SMilan Broz 	return r;
426aa129a22SMilan Broz }
427aa129a22SMilan Broz 
428028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
4291da177e4SLinus Torvalds {
4301da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds 
433028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
4341da177e4SLinus Torvalds {
4351da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
438028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
4391da177e4SLinus Torvalds {
4401da177e4SLinus Torvalds 	mempool_free(tio, md->tio_pool);
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
44308885643SKiyoshi Ueda static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
44408885643SKiyoshi Ueda 					    gfp_t gfp_mask)
445cec47e3dSKiyoshi Ueda {
44608885643SKiyoshi Ueda 	return mempool_alloc(md->tio_pool, gfp_mask);
447cec47e3dSKiyoshi Ueda }
448cec47e3dSKiyoshi Ueda 
449cec47e3dSKiyoshi Ueda static void free_rq_tio(struct dm_rq_target_io *tio)
450cec47e3dSKiyoshi Ueda {
451cec47e3dSKiyoshi Ueda 	mempool_free(tio, tio->md->tio_pool);
452cec47e3dSKiyoshi Ueda }
453cec47e3dSKiyoshi Ueda 
454cec47e3dSKiyoshi Ueda static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
455cec47e3dSKiyoshi Ueda {
456cec47e3dSKiyoshi Ueda 	return mempool_alloc(md->io_pool, GFP_ATOMIC);
457cec47e3dSKiyoshi Ueda }
458cec47e3dSKiyoshi Ueda 
459cec47e3dSKiyoshi Ueda static void free_bio_info(struct dm_rq_clone_bio_info *info)
460cec47e3dSKiyoshi Ueda {
461cec47e3dSKiyoshi Ueda 	mempool_free(info, info->tio->md->io_pool);
462cec47e3dSKiyoshi Ueda }
463cec47e3dSKiyoshi Ueda 
46490abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md)
46590abb8c4SKiyoshi Ueda {
46690abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
46790abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
46890abb8c4SKiyoshi Ueda }
46990abb8c4SKiyoshi Ueda 
4703eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
4713eaf840eSJun'ichi "Nick" Nomura {
4723eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
473c9959059STejun Heo 	int cpu;
474316d315bSNikanth Karthikesan 	int rw = bio_data_dir(io->bio);
4753eaf840eSJun'ichi "Nick" Nomura 
4763eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
4773eaf840eSJun'ichi "Nick" Nomura 
478074a7acaSTejun Heo 	cpu = part_stat_lock();
479074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
480074a7acaSTejun Heo 	part_stat_unlock();
481316d315bSNikanth Karthikesan 	dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
4823eaf840eSJun'ichi "Nick" Nomura }
4833eaf840eSJun'ichi "Nick" Nomura 
484d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
4853eaf840eSJun'ichi "Nick" Nomura {
4863eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
4873eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
4883eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
489c9959059STejun Heo 	int pending, cpu;
4903eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
4913eaf840eSJun'ichi "Nick" Nomura 
492074a7acaSTejun Heo 	cpu = part_stat_lock();
493074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
494074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
495074a7acaSTejun Heo 	part_stat_unlock();
4963eaf840eSJun'ichi "Nick" Nomura 
497af7e466aSMikulas Patocka 	/*
498af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
499d87f4c14STejun Heo 	 * a flush.
500af7e466aSMikulas Patocka 	 */
501316d315bSNikanth Karthikesan 	dm_disk(md)->part0.in_flight[rw] = pending =
502316d315bSNikanth Karthikesan 		atomic_dec_return(&md->pending[rw]);
503316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
5043eaf840eSJun'ichi "Nick" Nomura 
505d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
506d221d2e7SMikulas Patocka 	if (!pending)
507d221d2e7SMikulas Patocka 		wake_up(&md->wait);
5083eaf840eSJun'ichi "Nick" Nomura }
5093eaf840eSJun'ichi "Nick" Nomura 
5101da177e4SLinus Torvalds /*
5111da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
5121da177e4SLinus Torvalds  */
51392c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
5141da177e4SLinus Torvalds {
51505447420SKiyoshi Ueda 	unsigned long flags;
5161da177e4SLinus Torvalds 
51705447420SKiyoshi Ueda 	spin_lock_irqsave(&md->deferred_lock, flags);
5181da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
51905447420SKiyoshi Ueda 	spin_unlock_irqrestore(&md->deferred_lock, flags);
52092c63902SMikulas Patocka 	queue_work(md->wq, &md->work);
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds /*
5241da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
5251da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
5261da177e4SLinus Torvalds  * dm_table_put() when finished.
5271da177e4SLinus Torvalds  */
5287c666411SAlasdair G Kergon struct dm_table *dm_get_live_table(struct mapped_device *md)
5291da177e4SLinus Torvalds {
5301da177e4SLinus Torvalds 	struct dm_table *t;
531523d9297SKiyoshi Ueda 	unsigned long flags;
5321da177e4SLinus Torvalds 
533523d9297SKiyoshi Ueda 	read_lock_irqsave(&md->map_lock, flags);
5341da177e4SLinus Torvalds 	t = md->map;
5351da177e4SLinus Torvalds 	if (t)
5361da177e4SLinus Torvalds 		dm_table_get(t);
537523d9297SKiyoshi Ueda 	read_unlock_irqrestore(&md->map_lock, flags);
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	return t;
5401da177e4SLinus Torvalds }
5411da177e4SLinus Torvalds 
5423ac51e74SDarrick J. Wong /*
5433ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
5443ac51e74SDarrick J. Wong  */
5453ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
5463ac51e74SDarrick J. Wong {
5473ac51e74SDarrick J. Wong 	*geo = md->geometry;
5483ac51e74SDarrick J. Wong 
5493ac51e74SDarrick J. Wong 	return 0;
5503ac51e74SDarrick J. Wong }
5513ac51e74SDarrick J. Wong 
5523ac51e74SDarrick J. Wong /*
5533ac51e74SDarrick J. Wong  * Set the geometry of a device.
5543ac51e74SDarrick J. Wong  */
5553ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
5563ac51e74SDarrick J. Wong {
5573ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
5583ac51e74SDarrick J. Wong 
5593ac51e74SDarrick J. Wong 	if (geo->start > sz) {
5603ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
5613ac51e74SDarrick J. Wong 		return -EINVAL;
5623ac51e74SDarrick J. Wong 	}
5633ac51e74SDarrick J. Wong 
5643ac51e74SDarrick J. Wong 	md->geometry = *geo;
5653ac51e74SDarrick J. Wong 
5663ac51e74SDarrick J. Wong 	return 0;
5673ac51e74SDarrick J. Wong }
5683ac51e74SDarrick J. Wong 
5691da177e4SLinus Torvalds /*-----------------------------------------------------------------
5701da177e4SLinus Torvalds  * CRUD START:
5711da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
5721da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
5731da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
5741da177e4SLinus Torvalds  *   interests of getting something for people to use I give
5751da177e4SLinus Torvalds  *   you this clearly demarcated crap.
5761da177e4SLinus Torvalds  *---------------------------------------------------------------*/
5771da177e4SLinus Torvalds 
5782e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
5792e93ccc1SKiyoshi Ueda {
5802e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
5812e93ccc1SKiyoshi Ueda }
5822e93ccc1SKiyoshi Ueda 
5831da177e4SLinus Torvalds /*
5841da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
5851da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
5861da177e4SLinus Torvalds  */
587858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
5881da177e4SLinus Torvalds {
5892e93ccc1SKiyoshi Ueda 	unsigned long flags;
590b35f8caaSMilan Broz 	int io_error;
591b35f8caaSMilan Broz 	struct bio *bio;
592b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
5932e93ccc1SKiyoshi Ueda 
5942e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
595f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
596f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
597f88fb981SKiyoshi Ueda 		if (!(io->error > 0 && __noflush_suspending(md)))
5981da177e4SLinus Torvalds 			io->error = error;
599f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
600f88fb981SKiyoshi Ueda 	}
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
6032e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
6042e93ccc1SKiyoshi Ueda 			/*
6052e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
6062e93ccc1SKiyoshi Ueda 			 */
607022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
6086a8736d1STejun Heo 			if (__noflush_suspending(md))
6096a8736d1STejun Heo 				bio_list_add_head(&md->deferred, io->bio);
6106a8736d1STejun Heo 			else
6112e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
6122e93ccc1SKiyoshi Ueda 				io->error = -EIO;
613022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
6142e93ccc1SKiyoshi Ueda 		}
6152e93ccc1SKiyoshi Ueda 
616b35f8caaSMilan Broz 		io_error = io->error;
617b35f8caaSMilan Broz 		bio = io->bio;
618af7e466aSMikulas Patocka 		end_io_acct(io);
619a97f925aSMikulas Patocka 		free_io(md, io);
6201da177e4SLinus Torvalds 
6216a8736d1STejun Heo 		if (io_error == DM_ENDIO_REQUEUE)
6226a8736d1STejun Heo 			return;
6236a8736d1STejun Heo 
624b372d360SMike Snitzer 		if ((bio->bi_rw & REQ_FLUSH) && bio->bi_size) {
6251da177e4SLinus Torvalds 			/*
6266a8736d1STejun Heo 			 * Preflush done for flush with data, reissue
6276a8736d1STejun Heo 			 * without REQ_FLUSH.
6281da177e4SLinus Torvalds 			 */
6296a8736d1STejun Heo 			bio->bi_rw &= ~REQ_FLUSH;
6306a8736d1STejun Heo 			queue_io(md, bio);
6315f3ea37cSArnaldo Carvalho de Melo 		} else {
632b372d360SMike Snitzer 			/* done with normal IO or empty flush */
633b35f8caaSMilan Broz 			trace_block_bio_complete(md->queue, bio);
634b35f8caaSMilan Broz 			bio_endio(bio, io_error);
6352e93ccc1SKiyoshi Ueda 		}
6361da177e4SLinus Torvalds 	}
637af7e466aSMikulas Patocka }
6381da177e4SLinus Torvalds 
6396712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error)
6401da177e4SLinus Torvalds {
6411da177e4SLinus Torvalds 	int r = 0;
642028867acSAlasdair G Kergon 	struct dm_target_io *tio = bio->bi_private;
643b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
6449faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
6451da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 	if (!bio_flagged(bio, BIO_UPTODATE) && !error)
6481da177e4SLinus Torvalds 		error = -EIO;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	if (endio) {
6511da177e4SLinus Torvalds 		r = endio(tio->ti, bio, error, &tio->info);
6522e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
6532e93ccc1SKiyoshi Ueda 			/*
6542e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
6552e93ccc1SKiyoshi Ueda 			 * in dec_pending().
6562e93ccc1SKiyoshi Ueda 			 */
6571da177e4SLinus Torvalds 			error = r;
65845cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
65945cbcd79SKiyoshi Ueda 			/* The target will handle the io */
6606712ecf8SNeilBrown 			return;
66145cbcd79SKiyoshi Ueda 		else if (r) {
66245cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
66345cbcd79SKiyoshi Ueda 			BUG();
66445cbcd79SKiyoshi Ueda 		}
6651da177e4SLinus Torvalds 	}
6661da177e4SLinus Torvalds 
6679faf400fSStefan Bader 	/*
6689faf400fSStefan Bader 	 * Store md for cleanup instead of tio which is about to get freed.
6699faf400fSStefan Bader 	 */
6709faf400fSStefan Bader 	bio->bi_private = md->bs;
6719faf400fSStefan Bader 
6729faf400fSStefan Bader 	free_tio(md, tio);
673b35f8caaSMilan Broz 	bio_put(bio);
674b35f8caaSMilan Broz 	dec_pending(io, error);
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds 
677cec47e3dSKiyoshi Ueda /*
678cec47e3dSKiyoshi Ueda  * Partial completion handling for request-based dm
679cec47e3dSKiyoshi Ueda  */
680cec47e3dSKiyoshi Ueda static void end_clone_bio(struct bio *clone, int error)
681cec47e3dSKiyoshi Ueda {
682cec47e3dSKiyoshi Ueda 	struct dm_rq_clone_bio_info *info = clone->bi_private;
683cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = info->tio;
684cec47e3dSKiyoshi Ueda 	struct bio *bio = info->orig;
685cec47e3dSKiyoshi Ueda 	unsigned int nr_bytes = info->orig->bi_size;
686cec47e3dSKiyoshi Ueda 
687cec47e3dSKiyoshi Ueda 	bio_put(clone);
688cec47e3dSKiyoshi Ueda 
689cec47e3dSKiyoshi Ueda 	if (tio->error)
690cec47e3dSKiyoshi Ueda 		/*
691cec47e3dSKiyoshi Ueda 		 * An error has already been detected on the request.
692cec47e3dSKiyoshi Ueda 		 * Once error occurred, just let clone->end_io() handle
693cec47e3dSKiyoshi Ueda 		 * the remainder.
694cec47e3dSKiyoshi Ueda 		 */
695cec47e3dSKiyoshi Ueda 		return;
696cec47e3dSKiyoshi Ueda 	else if (error) {
697cec47e3dSKiyoshi Ueda 		/*
698cec47e3dSKiyoshi Ueda 		 * Don't notice the error to the upper layer yet.
699cec47e3dSKiyoshi Ueda 		 * The error handling decision is made by the target driver,
700cec47e3dSKiyoshi Ueda 		 * when the request is completed.
701cec47e3dSKiyoshi Ueda 		 */
702cec47e3dSKiyoshi Ueda 		tio->error = error;
703cec47e3dSKiyoshi Ueda 		return;
704cec47e3dSKiyoshi Ueda 	}
705cec47e3dSKiyoshi Ueda 
706cec47e3dSKiyoshi Ueda 	/*
707cec47e3dSKiyoshi Ueda 	 * I/O for the bio successfully completed.
708cec47e3dSKiyoshi Ueda 	 * Notice the data completion to the upper layer.
709cec47e3dSKiyoshi Ueda 	 */
710cec47e3dSKiyoshi Ueda 
711cec47e3dSKiyoshi Ueda 	/*
712cec47e3dSKiyoshi Ueda 	 * bios are processed from the head of the list.
713cec47e3dSKiyoshi Ueda 	 * So the completing bio should always be rq->bio.
714cec47e3dSKiyoshi Ueda 	 * If it's not, something wrong is happening.
715cec47e3dSKiyoshi Ueda 	 */
716cec47e3dSKiyoshi Ueda 	if (tio->orig->bio != bio)
717cec47e3dSKiyoshi Ueda 		DMERR("bio completion is going in the middle of the request");
718cec47e3dSKiyoshi Ueda 
719cec47e3dSKiyoshi Ueda 	/*
720cec47e3dSKiyoshi Ueda 	 * Update the original request.
721cec47e3dSKiyoshi Ueda 	 * Do not use blk_end_request() here, because it may complete
722cec47e3dSKiyoshi Ueda 	 * the original request before the clone, and break the ordering.
723cec47e3dSKiyoshi Ueda 	 */
724cec47e3dSKiyoshi Ueda 	blk_update_request(tio->orig, 0, nr_bytes);
725cec47e3dSKiyoshi Ueda }
726cec47e3dSKiyoshi Ueda 
727cec47e3dSKiyoshi Ueda /*
728cec47e3dSKiyoshi Ueda  * Don't touch any member of the md after calling this function because
729cec47e3dSKiyoshi Ueda  * the md may be freed in dm_put() at the end of this function.
730cec47e3dSKiyoshi Ueda  * Or do dm_get() before calling this function and dm_put() later.
731cec47e3dSKiyoshi Ueda  */
732b4324feeSKiyoshi Ueda static void rq_completed(struct mapped_device *md, int rw, int run_queue)
733cec47e3dSKiyoshi Ueda {
734b4324feeSKiyoshi Ueda 	atomic_dec(&md->pending[rw]);
735cec47e3dSKiyoshi Ueda 
736cec47e3dSKiyoshi Ueda 	/* nudge anyone waiting on suspend queue */
737b4324feeSKiyoshi Ueda 	if (!md_in_flight(md))
738cec47e3dSKiyoshi Ueda 		wake_up(&md->wait);
739cec47e3dSKiyoshi Ueda 
740cec47e3dSKiyoshi Ueda 	if (run_queue)
741b4324feeSKiyoshi Ueda 		blk_run_queue(md->queue);
742cec47e3dSKiyoshi Ueda 
743cec47e3dSKiyoshi Ueda 	/*
744cec47e3dSKiyoshi Ueda 	 * dm_put() must be at the end of this function. See the comment above
745cec47e3dSKiyoshi Ueda 	 */
746cec47e3dSKiyoshi Ueda 	dm_put(md);
747cec47e3dSKiyoshi Ueda }
748cec47e3dSKiyoshi Ueda 
749a77e28c7SKiyoshi Ueda static void free_rq_clone(struct request *clone)
750a77e28c7SKiyoshi Ueda {
751a77e28c7SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
752a77e28c7SKiyoshi Ueda 
753a77e28c7SKiyoshi Ueda 	blk_rq_unprep_clone(clone);
754a77e28c7SKiyoshi Ueda 	free_rq_tio(tio);
755a77e28c7SKiyoshi Ueda }
756a77e28c7SKiyoshi Ueda 
757980691e5SKiyoshi Ueda /*
758980691e5SKiyoshi Ueda  * Complete the clone and the original request.
759980691e5SKiyoshi Ueda  * Must be called without queue lock.
760980691e5SKiyoshi Ueda  */
761980691e5SKiyoshi Ueda static void dm_end_request(struct request *clone, int error)
762980691e5SKiyoshi Ueda {
763980691e5SKiyoshi Ueda 	int rw = rq_data_dir(clone);
764980691e5SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
765980691e5SKiyoshi Ueda 	struct mapped_device *md = tio->md;
766980691e5SKiyoshi Ueda 	struct request *rq = tio->orig;
767980691e5SKiyoshi Ueda 
76829e4013dSTejun Heo 	if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
769980691e5SKiyoshi Ueda 		rq->errors = clone->errors;
770980691e5SKiyoshi Ueda 		rq->resid_len = clone->resid_len;
771980691e5SKiyoshi Ueda 
772980691e5SKiyoshi Ueda 		if (rq->sense)
773980691e5SKiyoshi Ueda 			/*
774980691e5SKiyoshi Ueda 			 * We are using the sense buffer of the original
775980691e5SKiyoshi Ueda 			 * request.
776980691e5SKiyoshi Ueda 			 * So setting the length of the sense data is enough.
777980691e5SKiyoshi Ueda 			 */
778980691e5SKiyoshi Ueda 			rq->sense_len = clone->sense_len;
779980691e5SKiyoshi Ueda 	}
780980691e5SKiyoshi Ueda 
781980691e5SKiyoshi Ueda 	free_rq_clone(clone);
782980691e5SKiyoshi Ueda 	blk_end_request_all(rq, error);
78329e4013dSTejun Heo 	rq_completed(md, rw, true);
784980691e5SKiyoshi Ueda }
785980691e5SKiyoshi Ueda 
786cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq)
787cec47e3dSKiyoshi Ueda {
788cec47e3dSKiyoshi Ueda 	struct request *clone = rq->special;
789cec47e3dSKiyoshi Ueda 
790cec47e3dSKiyoshi Ueda 	rq->special = NULL;
791cec47e3dSKiyoshi Ueda 	rq->cmd_flags &= ~REQ_DONTPREP;
792cec47e3dSKiyoshi Ueda 
793a77e28c7SKiyoshi Ueda 	free_rq_clone(clone);
794cec47e3dSKiyoshi Ueda }
795cec47e3dSKiyoshi Ueda 
796cec47e3dSKiyoshi Ueda /*
797cec47e3dSKiyoshi Ueda  * Requeue the original request of a clone.
798cec47e3dSKiyoshi Ueda  */
799cec47e3dSKiyoshi Ueda void dm_requeue_unmapped_request(struct request *clone)
800cec47e3dSKiyoshi Ueda {
801b4324feeSKiyoshi Ueda 	int rw = rq_data_dir(clone);
802cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
803cec47e3dSKiyoshi Ueda 	struct mapped_device *md = tio->md;
804cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
805cec47e3dSKiyoshi Ueda 	struct request_queue *q = rq->q;
806cec47e3dSKiyoshi Ueda 	unsigned long flags;
807cec47e3dSKiyoshi Ueda 
808cec47e3dSKiyoshi Ueda 	dm_unprep_request(rq);
809cec47e3dSKiyoshi Ueda 
810cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
811cec47e3dSKiyoshi Ueda 	if (elv_queue_empty(q))
812cec47e3dSKiyoshi Ueda 		blk_plug_device(q);
813cec47e3dSKiyoshi Ueda 	blk_requeue_request(q, rq);
814cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
815cec47e3dSKiyoshi Ueda 
816b4324feeSKiyoshi Ueda 	rq_completed(md, rw, 0);
817cec47e3dSKiyoshi Ueda }
818cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
819cec47e3dSKiyoshi Ueda 
820cec47e3dSKiyoshi Ueda static void __stop_queue(struct request_queue *q)
821cec47e3dSKiyoshi Ueda {
822cec47e3dSKiyoshi Ueda 	blk_stop_queue(q);
823cec47e3dSKiyoshi Ueda }
824cec47e3dSKiyoshi Ueda 
825cec47e3dSKiyoshi Ueda static void stop_queue(struct request_queue *q)
826cec47e3dSKiyoshi Ueda {
827cec47e3dSKiyoshi Ueda 	unsigned long flags;
828cec47e3dSKiyoshi Ueda 
829cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
830cec47e3dSKiyoshi Ueda 	__stop_queue(q);
831cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
832cec47e3dSKiyoshi Ueda }
833cec47e3dSKiyoshi Ueda 
834cec47e3dSKiyoshi Ueda static void __start_queue(struct request_queue *q)
835cec47e3dSKiyoshi Ueda {
836cec47e3dSKiyoshi Ueda 	if (blk_queue_stopped(q))
837cec47e3dSKiyoshi Ueda 		blk_start_queue(q);
838cec47e3dSKiyoshi Ueda }
839cec47e3dSKiyoshi Ueda 
840cec47e3dSKiyoshi Ueda static void start_queue(struct request_queue *q)
841cec47e3dSKiyoshi Ueda {
842cec47e3dSKiyoshi Ueda 	unsigned long flags;
843cec47e3dSKiyoshi Ueda 
844cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
845cec47e3dSKiyoshi Ueda 	__start_queue(q);
846cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
847cec47e3dSKiyoshi Ueda }
848cec47e3dSKiyoshi Ueda 
84911a68244SKiyoshi Ueda static void dm_done(struct request *clone, int error, bool mapped)
85011a68244SKiyoshi Ueda {
85111a68244SKiyoshi Ueda 	int r = error;
85211a68244SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
85311a68244SKiyoshi Ueda 	dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
85411a68244SKiyoshi Ueda 
85511a68244SKiyoshi Ueda 	if (mapped && rq_end_io)
85611a68244SKiyoshi Ueda 		r = rq_end_io(tio->ti, clone, error, &tio->info);
85711a68244SKiyoshi Ueda 
85811a68244SKiyoshi Ueda 	if (r <= 0)
85911a68244SKiyoshi Ueda 		/* The target wants to complete the I/O */
86011a68244SKiyoshi Ueda 		dm_end_request(clone, r);
86111a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_INCOMPLETE)
86211a68244SKiyoshi Ueda 		/* The target will handle the I/O */
86311a68244SKiyoshi Ueda 		return;
86411a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_REQUEUE)
86511a68244SKiyoshi Ueda 		/* The target wants to requeue the I/O */
86611a68244SKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
86711a68244SKiyoshi Ueda 	else {
86811a68244SKiyoshi Ueda 		DMWARN("unimplemented target endio return value: %d", r);
86911a68244SKiyoshi Ueda 		BUG();
87011a68244SKiyoshi Ueda 	}
87111a68244SKiyoshi Ueda }
87211a68244SKiyoshi Ueda 
873cec47e3dSKiyoshi Ueda /*
874cec47e3dSKiyoshi Ueda  * Request completion handler for request-based dm
875cec47e3dSKiyoshi Ueda  */
876cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq)
877cec47e3dSKiyoshi Ueda {
87811a68244SKiyoshi Ueda 	bool mapped = true;
879cec47e3dSKiyoshi Ueda 	struct request *clone = rq->completion_data;
880cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
881cec47e3dSKiyoshi Ueda 
88211a68244SKiyoshi Ueda 	if (rq->cmd_flags & REQ_FAILED)
88311a68244SKiyoshi Ueda 		mapped = false;
884cec47e3dSKiyoshi Ueda 
88511a68244SKiyoshi Ueda 	dm_done(clone, tio->error, mapped);
886cec47e3dSKiyoshi Ueda }
887cec47e3dSKiyoshi Ueda 
888cec47e3dSKiyoshi Ueda /*
889cec47e3dSKiyoshi Ueda  * Complete the clone and the original request with the error status
890cec47e3dSKiyoshi Ueda  * through softirq context.
891cec47e3dSKiyoshi Ueda  */
892cec47e3dSKiyoshi Ueda static void dm_complete_request(struct request *clone, int error)
893cec47e3dSKiyoshi Ueda {
894cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
895cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
896cec47e3dSKiyoshi Ueda 
897cec47e3dSKiyoshi Ueda 	tio->error = error;
898cec47e3dSKiyoshi Ueda 	rq->completion_data = clone;
899cec47e3dSKiyoshi Ueda 	blk_complete_request(rq);
900cec47e3dSKiyoshi Ueda }
901cec47e3dSKiyoshi Ueda 
902cec47e3dSKiyoshi Ueda /*
903cec47e3dSKiyoshi Ueda  * Complete the not-mapped clone and the original request with the error status
904cec47e3dSKiyoshi Ueda  * through softirq context.
905cec47e3dSKiyoshi Ueda  * Target's rq_end_io() function isn't called.
906cec47e3dSKiyoshi Ueda  * This may be used when the target's map_rq() function fails.
907cec47e3dSKiyoshi Ueda  */
908cec47e3dSKiyoshi Ueda void dm_kill_unmapped_request(struct request *clone, int error)
909cec47e3dSKiyoshi Ueda {
910cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
911cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
912cec47e3dSKiyoshi Ueda 
913cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_FAILED;
914cec47e3dSKiyoshi Ueda 	dm_complete_request(clone, error);
915cec47e3dSKiyoshi Ueda }
916cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
917cec47e3dSKiyoshi Ueda 
918cec47e3dSKiyoshi Ueda /*
919cec47e3dSKiyoshi Ueda  * Called with the queue lock held
920cec47e3dSKiyoshi Ueda  */
921cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error)
922cec47e3dSKiyoshi Ueda {
923cec47e3dSKiyoshi Ueda 	/*
924cec47e3dSKiyoshi Ueda 	 * For just cleaning up the information of the queue in which
925cec47e3dSKiyoshi Ueda 	 * the clone was dispatched.
926cec47e3dSKiyoshi Ueda 	 * The clone is *NOT* freed actually here because it is alloced from
927cec47e3dSKiyoshi Ueda 	 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
928cec47e3dSKiyoshi Ueda 	 */
929cec47e3dSKiyoshi Ueda 	__blk_put_request(clone->q, clone);
930cec47e3dSKiyoshi Ueda 
931cec47e3dSKiyoshi Ueda 	/*
932cec47e3dSKiyoshi Ueda 	 * Actual request completion is done in a softirq context which doesn't
933cec47e3dSKiyoshi Ueda 	 * hold the queue lock.  Otherwise, deadlock could occur because:
934cec47e3dSKiyoshi Ueda 	 *     - another request may be submitted by the upper level driver
935cec47e3dSKiyoshi Ueda 	 *       of the stacking during the completion
936cec47e3dSKiyoshi Ueda 	 *     - the submission which requires queue lock may be done
937cec47e3dSKiyoshi Ueda 	 *       against this queue
938cec47e3dSKiyoshi Ueda 	 */
939cec47e3dSKiyoshi Ueda 	dm_complete_request(clone, error);
940cec47e3dSKiyoshi Ueda }
941cec47e3dSKiyoshi Ueda 
94256a67df7SMike Snitzer /*
94356a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
94456a67df7SMike Snitzer  * target boundary.
94556a67df7SMike Snitzer  */
94656a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
9471da177e4SLinus Torvalds {
94856a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
94956a67df7SMike Snitzer 
95056a67df7SMike Snitzer 	return ti->len - target_offset;
95156a67df7SMike Snitzer }
95256a67df7SMike Snitzer 
95356a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
95456a67df7SMike Snitzer {
95556a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	/*
9581da177e4SLinus Torvalds 	 * Does the target need to split even further ?
9591da177e4SLinus Torvalds 	 */
9601da177e4SLinus Torvalds 	if (ti->split_io) {
9611da177e4SLinus Torvalds 		sector_t boundary;
96256a67df7SMike Snitzer 		sector_t offset = dm_target_offset(ti, sector);
9631da177e4SLinus Torvalds 		boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
9641da177e4SLinus Torvalds 			   - offset;
9651da177e4SLinus Torvalds 		if (len > boundary)
9661da177e4SLinus Torvalds 			len = boundary;
9671da177e4SLinus Torvalds 	}
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	return len;
9701da177e4SLinus Torvalds }
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds static void __map_bio(struct dm_target *ti, struct bio *clone,
973028867acSAlasdair G Kergon 		      struct dm_target_io *tio)
9741da177e4SLinus Torvalds {
9751da177e4SLinus Torvalds 	int r;
9762056a782SJens Axboe 	sector_t sector;
9779faf400fSStefan Bader 	struct mapped_device *md;
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
9801da177e4SLinus Torvalds 	clone->bi_private = tio;
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	/*
9831da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
9841da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
9851da177e4SLinus Torvalds 	 * this io.
9861da177e4SLinus Torvalds 	 */
9871da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
9882056a782SJens Axboe 	sector = clone->bi_sector;
9891da177e4SLinus Torvalds 	r = ti->type->map(ti, clone, &tio->info);
99045cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
9911da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
9922056a782SJens Axboe 
993*d07335e5SMike Snitzer 		trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
99422a7c31aSAlan D. Brunelle 				      tio->io->bio->bi_bdev->bd_dev, sector);
9952056a782SJens Axboe 
9961da177e4SLinus Torvalds 		generic_make_request(clone);
9972e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
9982e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
9999faf400fSStefan Bader 		md = tio->io->md;
10009faf400fSStefan Bader 		dec_pending(tio->io, r);
10019faf400fSStefan Bader 		/*
10029faf400fSStefan Bader 		 * Store bio_set for cleanup.
10039faf400fSStefan Bader 		 */
10049faf400fSStefan Bader 		clone->bi_private = md->bs;
10051da177e4SLinus Torvalds 		bio_put(clone);
10069faf400fSStefan Bader 		free_tio(md, tio);
100745cbcd79SKiyoshi Ueda 	} else if (r) {
100845cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
100945cbcd79SKiyoshi Ueda 		BUG();
10101da177e4SLinus Torvalds 	}
10111da177e4SLinus Torvalds }
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds struct clone_info {
10141da177e4SLinus Torvalds 	struct mapped_device *md;
10151da177e4SLinus Torvalds 	struct dm_table *map;
10161da177e4SLinus Torvalds 	struct bio *bio;
10171da177e4SLinus Torvalds 	struct dm_io *io;
10181da177e4SLinus Torvalds 	sector_t sector;
10191da177e4SLinus Torvalds 	sector_t sector_count;
10201da177e4SLinus Torvalds 	unsigned short idx;
10211da177e4SLinus Torvalds };
10221da177e4SLinus Torvalds 
10233676347aSPeter Osterlund static void dm_bio_destructor(struct bio *bio)
10243676347aSPeter Osterlund {
10259faf400fSStefan Bader 	struct bio_set *bs = bio->bi_private;
10269faf400fSStefan Bader 
10279faf400fSStefan Bader 	bio_free(bio, bs);
10283676347aSPeter Osterlund }
10293676347aSPeter Osterlund 
10301da177e4SLinus Torvalds /*
1031d87f4c14STejun Heo  * Creates a little bio that just does part of a bvec.
10321da177e4SLinus Torvalds  */
10331da177e4SLinus Torvalds static struct bio *split_bvec(struct bio *bio, sector_t sector,
10341da177e4SLinus Torvalds 			      unsigned short idx, unsigned int offset,
10359faf400fSStefan Bader 			      unsigned int len, struct bio_set *bs)
10361da177e4SLinus Torvalds {
10371da177e4SLinus Torvalds 	struct bio *clone;
10381da177e4SLinus Torvalds 	struct bio_vec *bv = bio->bi_io_vec + idx;
10391da177e4SLinus Torvalds 
10409faf400fSStefan Bader 	clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
10413676347aSPeter Osterlund 	clone->bi_destructor = dm_bio_destructor;
10421da177e4SLinus Torvalds 	*clone->bi_io_vec = *bv;
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	clone->bi_sector = sector;
10451da177e4SLinus Torvalds 	clone->bi_bdev = bio->bi_bdev;
1046d87f4c14STejun Heo 	clone->bi_rw = bio->bi_rw;
10471da177e4SLinus Torvalds 	clone->bi_vcnt = 1;
10481da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
10491da177e4SLinus Torvalds 	clone->bi_io_vec->bv_offset = offset;
10501da177e4SLinus Torvalds 	clone->bi_io_vec->bv_len = clone->bi_size;
1051f3e1d26eSMartin K. Petersen 	clone->bi_flags |= 1 << BIO_CLONED;
10521da177e4SLinus Torvalds 
10539c47008dSMartin K. Petersen 	if (bio_integrity(bio)) {
10547878cba9SMartin K. Petersen 		bio_integrity_clone(clone, bio, GFP_NOIO, bs);
10559c47008dSMartin K. Petersen 		bio_integrity_trim(clone,
10569c47008dSMartin K. Petersen 				   bio_sector_offset(bio, idx, offset), len);
10579c47008dSMartin K. Petersen 	}
10589c47008dSMartin K. Petersen 
10591da177e4SLinus Torvalds 	return clone;
10601da177e4SLinus Torvalds }
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds /*
10631da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
10641da177e4SLinus Torvalds  */
10651da177e4SLinus Torvalds static struct bio *clone_bio(struct bio *bio, sector_t sector,
10661da177e4SLinus Torvalds 			     unsigned short idx, unsigned short bv_count,
10679faf400fSStefan Bader 			     unsigned int len, struct bio_set *bs)
10681da177e4SLinus Torvalds {
10691da177e4SLinus Torvalds 	struct bio *clone;
10701da177e4SLinus Torvalds 
10719faf400fSStefan Bader 	clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
10729faf400fSStefan Bader 	__bio_clone(clone, bio);
10739faf400fSStefan Bader 	clone->bi_destructor = dm_bio_destructor;
10741da177e4SLinus Torvalds 	clone->bi_sector = sector;
10751da177e4SLinus Torvalds 	clone->bi_idx = idx;
10761da177e4SLinus Torvalds 	clone->bi_vcnt = idx + bv_count;
10771da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
10781da177e4SLinus Torvalds 	clone->bi_flags &= ~(1 << BIO_SEG_VALID);
10791da177e4SLinus Torvalds 
10809c47008dSMartin K. Petersen 	if (bio_integrity(bio)) {
10817878cba9SMartin K. Petersen 		bio_integrity_clone(clone, bio, GFP_NOIO, bs);
10829c47008dSMartin K. Petersen 
10839c47008dSMartin K. Petersen 		if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
10849c47008dSMartin K. Petersen 			bio_integrity_trim(clone,
10859c47008dSMartin K. Petersen 					   bio_sector_offset(bio, idx, 0), len);
10869c47008dSMartin K. Petersen 	}
10879c47008dSMartin K. Petersen 
10881da177e4SLinus Torvalds 	return clone;
10891da177e4SLinus Torvalds }
10901da177e4SLinus Torvalds 
10919015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci,
10929015df24SAlasdair G Kergon 				      struct dm_target *ti)
1093f9ab94ceSMikulas Patocka {
10949015df24SAlasdair G Kergon 	struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
1095f9ab94ceSMikulas Patocka 
1096f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1097f9ab94ceSMikulas Patocka 	tio->ti = ti;
1098f9ab94ceSMikulas Patocka 	memset(&tio->info, 0, sizeof(tio->info));
10999015df24SAlasdair G Kergon 
11009015df24SAlasdair G Kergon 	return tio;
11019015df24SAlasdair G Kergon }
11029015df24SAlasdair G Kergon 
110306a426ceSMike Snitzer static void __issue_target_request(struct clone_info *ci, struct dm_target *ti,
1104a79245b3SMike Snitzer 				   unsigned request_nr, sector_t len)
11059015df24SAlasdair G Kergon {
11069015df24SAlasdair G Kergon 	struct dm_target_io *tio = alloc_tio(ci, ti);
11079015df24SAlasdair G Kergon 	struct bio *clone;
11089015df24SAlasdair G Kergon 
110957cba5d3SMike Snitzer 	tio->info.target_request_nr = request_nr;
1110f9ab94ceSMikulas Patocka 
111106a426ceSMike Snitzer 	/*
111206a426ceSMike Snitzer 	 * Discard requests require the bio's inline iovecs be initialized.
111306a426ceSMike Snitzer 	 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
111406a426ceSMike Snitzer 	 * and discard, so no need for concern about wasted bvec allocations.
111506a426ceSMike Snitzer 	 */
111606a426ceSMike Snitzer 	clone = bio_alloc_bioset(GFP_NOIO, ci->bio->bi_max_vecs, ci->md->bs);
1117f9ab94ceSMikulas Patocka 	__bio_clone(clone, ci->bio);
1118f9ab94ceSMikulas Patocka 	clone->bi_destructor = dm_bio_destructor;
1119a79245b3SMike Snitzer 	if (len) {
1120a79245b3SMike Snitzer 		clone->bi_sector = ci->sector;
1121a79245b3SMike Snitzer 		clone->bi_size = to_bytes(len);
1122a79245b3SMike Snitzer 	}
1123f9ab94ceSMikulas Patocka 
1124f9ab94ceSMikulas Patocka 	__map_bio(ti, clone, tio);
1125f9ab94ceSMikulas Patocka }
1126f9ab94ceSMikulas Patocka 
112706a426ceSMike Snitzer static void __issue_target_requests(struct clone_info *ci, struct dm_target *ti,
1128a79245b3SMike Snitzer 				    unsigned num_requests, sector_t len)
112906a426ceSMike Snitzer {
113006a426ceSMike Snitzer 	unsigned request_nr;
113106a426ceSMike Snitzer 
113206a426ceSMike Snitzer 	for (request_nr = 0; request_nr < num_requests; request_nr++)
1133a79245b3SMike Snitzer 		__issue_target_request(ci, ti, request_nr, len);
113406a426ceSMike Snitzer }
113506a426ceSMike Snitzer 
1136b372d360SMike Snitzer static int __clone_and_map_empty_flush(struct clone_info *ci)
1137f9ab94ceSMikulas Patocka {
113806a426ceSMike Snitzer 	unsigned target_nr = 0;
1139f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1140f9ab94ceSMikulas Patocka 
1141b372d360SMike Snitzer 	BUG_ON(bio_has_data(ci->bio));
1142f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
1143a79245b3SMike Snitzer 		__issue_target_requests(ci, ti, ti->num_flush_requests, 0);
1144f9ab94ceSMikulas Patocka 
1145f9ab94ceSMikulas Patocka 	return 0;
1146f9ab94ceSMikulas Patocka }
1147f9ab94ceSMikulas Patocka 
11485ae89a87SMike Snitzer /*
11495ae89a87SMike Snitzer  * Perform all io with a single clone.
11505ae89a87SMike Snitzer  */
11515ae89a87SMike Snitzer static void __clone_and_map_simple(struct clone_info *ci, struct dm_target *ti)
11525ae89a87SMike Snitzer {
11535ae89a87SMike Snitzer 	struct bio *clone, *bio = ci->bio;
11545ae89a87SMike Snitzer 	struct dm_target_io *tio;
11555ae89a87SMike Snitzer 
11565ae89a87SMike Snitzer 	tio = alloc_tio(ci, ti);
11575ae89a87SMike Snitzer 	clone = clone_bio(bio, ci->sector, ci->idx,
11585ae89a87SMike Snitzer 			  bio->bi_vcnt - ci->idx, ci->sector_count,
11595ae89a87SMike Snitzer 			  ci->md->bs);
11605ae89a87SMike Snitzer 	__map_bio(ti, clone, tio);
11615ae89a87SMike Snitzer 	ci->sector_count = 0;
11625ae89a87SMike Snitzer }
11635ae89a87SMike Snitzer 
11645ae89a87SMike Snitzer static int __clone_and_map_discard(struct clone_info *ci)
11655ae89a87SMike Snitzer {
11665ae89a87SMike Snitzer 	struct dm_target *ti;
1167a79245b3SMike Snitzer 	sector_t len;
11685ae89a87SMike Snitzer 
1169a79245b3SMike Snitzer 	do {
11705ae89a87SMike Snitzer 		ti = dm_table_find_target(ci->map, ci->sector);
11715ae89a87SMike Snitzer 		if (!dm_target_is_valid(ti))
11725ae89a87SMike Snitzer 			return -EIO;
11735ae89a87SMike Snitzer 
11745ae89a87SMike Snitzer 		/*
11755ae89a87SMike Snitzer 		 * Even though the device advertised discard support,
11765ae89a87SMike Snitzer 		 * reconfiguration might have changed that since the
11775ae89a87SMike Snitzer 		 * check was performed.
11785ae89a87SMike Snitzer 		 */
11795ae89a87SMike Snitzer 		if (!ti->num_discard_requests)
11805ae89a87SMike Snitzer 			return -EOPNOTSUPP;
11815ae89a87SMike Snitzer 
1182a79245b3SMike Snitzer 		len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
11835ae89a87SMike Snitzer 
1184a79245b3SMike Snitzer 		__issue_target_requests(ci, ti, ti->num_discard_requests, len);
11855ae89a87SMike Snitzer 
1186a79245b3SMike Snitzer 		ci->sector += len;
1187a79245b3SMike Snitzer 	} while (ci->sector_count -= len);
11885ae89a87SMike Snitzer 
11895ae89a87SMike Snitzer 	return 0;
11905ae89a87SMike Snitzer }
11915ae89a87SMike Snitzer 
1192512875bdSJun'ichi Nomura static int __clone_and_map(struct clone_info *ci)
11931da177e4SLinus Torvalds {
11941da177e4SLinus Torvalds 	struct bio *clone, *bio = ci->bio;
1195512875bdSJun'ichi Nomura 	struct dm_target *ti;
1196512875bdSJun'ichi Nomura 	sector_t len = 0, max;
1197028867acSAlasdair G Kergon 	struct dm_target_io *tio;
11981da177e4SLinus Torvalds 
11995ae89a87SMike Snitzer 	if (unlikely(bio->bi_rw & REQ_DISCARD))
12005ae89a87SMike Snitzer 		return __clone_and_map_discard(ci);
12015ae89a87SMike Snitzer 
1202512875bdSJun'ichi Nomura 	ti = dm_table_find_target(ci->map, ci->sector);
1203512875bdSJun'ichi Nomura 	if (!dm_target_is_valid(ti))
1204512875bdSJun'ichi Nomura 		return -EIO;
1205512875bdSJun'ichi Nomura 
120656a67df7SMike Snitzer 	max = max_io_len(ci->sector, ti);
1207512875bdSJun'ichi Nomura 
12081da177e4SLinus Torvalds 	if (ci->sector_count <= max) {
12091da177e4SLinus Torvalds 		/*
12101da177e4SLinus Torvalds 		 * Optimise for the simple case where we can do all of
12111da177e4SLinus Torvalds 		 * the remaining io with a single clone.
12121da177e4SLinus Torvalds 		 */
12135ae89a87SMike Snitzer 		__clone_and_map_simple(ci, ti);
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds 	} else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
12161da177e4SLinus Torvalds 		/*
12171da177e4SLinus Torvalds 		 * There are some bvecs that don't span targets.
12181da177e4SLinus Torvalds 		 * Do as many of these as possible.
12191da177e4SLinus Torvalds 		 */
12201da177e4SLinus Torvalds 		int i;
12211da177e4SLinus Torvalds 		sector_t remaining = max;
12221da177e4SLinus Torvalds 		sector_t bv_len;
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 		for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
12251da177e4SLinus Torvalds 			bv_len = to_sector(bio->bi_io_vec[i].bv_len);
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 			if (bv_len > remaining)
12281da177e4SLinus Torvalds 				break;
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 			remaining -= bv_len;
12311da177e4SLinus Torvalds 			len += bv_len;
12321da177e4SLinus Torvalds 		}
12331da177e4SLinus Torvalds 
12345ae89a87SMike Snitzer 		tio = alloc_tio(ci, ti);
12359faf400fSStefan Bader 		clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
12369faf400fSStefan Bader 				  ci->md->bs);
12371da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 		ci->sector += len;
12401da177e4SLinus Torvalds 		ci->sector_count -= len;
12411da177e4SLinus Torvalds 		ci->idx = i;
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	} else {
12441da177e4SLinus Torvalds 		/*
1245d2044a94SAlasdair G Kergon 		 * Handle a bvec that must be split between two or more targets.
12461da177e4SLinus Torvalds 		 */
12471da177e4SLinus Torvalds 		struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1248d2044a94SAlasdair G Kergon 		sector_t remaining = to_sector(bv->bv_len);
1249d2044a94SAlasdair G Kergon 		unsigned int offset = 0;
12501da177e4SLinus Torvalds 
1251d2044a94SAlasdair G Kergon 		do {
1252d2044a94SAlasdair G Kergon 			if (offset) {
12531da177e4SLinus Torvalds 				ti = dm_table_find_target(ci->map, ci->sector);
1254512875bdSJun'ichi Nomura 				if (!dm_target_is_valid(ti))
1255512875bdSJun'ichi Nomura 					return -EIO;
1256512875bdSJun'ichi Nomura 
125756a67df7SMike Snitzer 				max = max_io_len(ci->sector, ti);
1258d2044a94SAlasdair G Kergon 			}
1259d2044a94SAlasdair G Kergon 
1260d2044a94SAlasdair G Kergon 			len = min(remaining, max);
1261d2044a94SAlasdair G Kergon 
12625ae89a87SMike Snitzer 			tio = alloc_tio(ci, ti);
1263d2044a94SAlasdair G Kergon 			clone = split_bvec(bio, ci->sector, ci->idx,
12649faf400fSStefan Bader 					   bv->bv_offset + offset, len,
12659faf400fSStefan Bader 					   ci->md->bs);
1266d2044a94SAlasdair G Kergon 
12671da177e4SLinus Torvalds 			__map_bio(ti, clone, tio);
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 			ci->sector += len;
12701da177e4SLinus Torvalds 			ci->sector_count -= len;
1271d2044a94SAlasdair G Kergon 			offset += to_bytes(len);
1272d2044a94SAlasdair G Kergon 		} while (remaining -= len);
1273d2044a94SAlasdair G Kergon 
12741da177e4SLinus Torvalds 		ci->idx++;
12751da177e4SLinus Torvalds 	}
1276512875bdSJun'ichi Nomura 
1277512875bdSJun'ichi Nomura 	return 0;
12781da177e4SLinus Torvalds }
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds /*
12818a53c28dSMikulas Patocka  * Split the bio into several clones and submit it to targets.
12821da177e4SLinus Torvalds  */
1283f0b9a450SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
12841da177e4SLinus Torvalds {
12851da177e4SLinus Torvalds 	struct clone_info ci;
1286512875bdSJun'ichi Nomura 	int error = 0;
12871da177e4SLinus Torvalds 
12887c666411SAlasdair G Kergon 	ci.map = dm_get_live_table(md);
1289f0b9a450SMikulas Patocka 	if (unlikely(!ci.map)) {
1290f0b9a450SMikulas Patocka 		bio_io_error(bio);
1291f0b9a450SMikulas Patocka 		return;
1292f0b9a450SMikulas Patocka 	}
1293692d0eb9SMikulas Patocka 
12941da177e4SLinus Torvalds 	ci.md = md;
12951da177e4SLinus Torvalds 	ci.io = alloc_io(md);
12961da177e4SLinus Torvalds 	ci.io->error = 0;
12971da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
12981da177e4SLinus Torvalds 	ci.io->bio = bio;
12991da177e4SLinus Torvalds 	ci.io->md = md;
1300f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
13011da177e4SLinus Torvalds 	ci.sector = bio->bi_sector;
13021da177e4SLinus Torvalds 	ci.idx = bio->bi_idx;
13031da177e4SLinus Torvalds 
13043eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1305b372d360SMike Snitzer 	if (bio->bi_rw & REQ_FLUSH) {
1306b372d360SMike Snitzer 		ci.bio = &ci.md->flush_bio;
1307b372d360SMike Snitzer 		ci.sector_count = 0;
1308b372d360SMike Snitzer 		error = __clone_and_map_empty_flush(&ci);
1309b372d360SMike Snitzer 		/* dec_pending submits any data associated with flush */
1310b372d360SMike Snitzer 	} else {
13116a8736d1STejun Heo 		ci.bio = bio;
1312f6fccb12SMilan Broz 		ci.sector_count = bio_sectors(bio);
1313512875bdSJun'ichi Nomura 		while (ci.sector_count && !error)
1314512875bdSJun'ichi Nomura 			error = __clone_and_map(&ci);
1315d87f4c14STejun Heo 	}
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 	/* drop the extra reference count */
1318512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
13191da177e4SLinus Torvalds 	dm_table_put(ci.map);
13209e4e5f87SMilan Broz }
13219e4e5f87SMilan Broz /*-----------------------------------------------------------------
13221da177e4SLinus Torvalds  * CRUD END
13231da177e4SLinus Torvalds  *---------------------------------------------------------------*/
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds static int dm_merge_bvec(struct request_queue *q,
13261da177e4SLinus Torvalds 			 struct bvec_merge_data *bvm,
1327f6fccb12SMilan Broz 			 struct bio_vec *biovec)
1328f6fccb12SMilan Broz {
1329f6fccb12SMilan Broz 	struct mapped_device *md = q->queuedata;
13307c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1331f6fccb12SMilan Broz 	struct dm_target *ti;
1332f6fccb12SMilan Broz 	sector_t max_sectors;
1333f6fccb12SMilan Broz 	int max_size = 0;
1334f6fccb12SMilan Broz 
1335f6fccb12SMilan Broz 	if (unlikely(!map))
1336f6fccb12SMilan Broz 		goto out;
1337f6fccb12SMilan Broz 
1338f6fccb12SMilan Broz 	ti = dm_table_find_target(map, bvm->bi_sector);
1339f6fccb12SMilan Broz 	if (!dm_target_is_valid(ti))
1340f6fccb12SMilan Broz 		goto out_table;
1341f6fccb12SMilan Broz 
1342f6fccb12SMilan Broz 	/*
1343f6fccb12SMilan Broz 	 * Find maximum amount of I/O that won't need splitting
1344f6fccb12SMilan Broz 	 */
134556a67df7SMike Snitzer 	max_sectors = min(max_io_len(bvm->bi_sector, ti),
1346f6fccb12SMilan Broz 			  (sector_t) BIO_MAX_SECTORS);
1347f6fccb12SMilan Broz 	max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1348f6fccb12SMilan Broz 	if (max_size < 0)
1349f6fccb12SMilan Broz 		max_size = 0;
1350f6fccb12SMilan Broz 
1351f6fccb12SMilan Broz 	/*
1352f6fccb12SMilan Broz 	 * merge_bvec_fn() returns number of bytes
1353f6fccb12SMilan Broz 	 * it can accept at this offset
1354f6fccb12SMilan Broz 	 * max is precomputed maximal io size
1355f6fccb12SMilan Broz 	 */
1356f6fccb12SMilan Broz 	if (max_size && ti->type->merge)
1357f6fccb12SMilan Broz 		max_size = ti->type->merge(ti, bvm, biovec, max_size);
13588cbeb67aSMikulas Patocka 	/*
13598cbeb67aSMikulas Patocka 	 * If the target doesn't support merge method and some of the devices
13608cbeb67aSMikulas Patocka 	 * provided their merge_bvec method (we know this by looking at
13618cbeb67aSMikulas Patocka 	 * queue_max_hw_sectors), then we can't allow bios with multiple vector
13628cbeb67aSMikulas Patocka 	 * entries.  So always set max_size to 0, and the code below allows
13638cbeb67aSMikulas Patocka 	 * just one page.
13648cbeb67aSMikulas Patocka 	 */
13658cbeb67aSMikulas Patocka 	else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
13668cbeb67aSMikulas Patocka 
13678cbeb67aSMikulas Patocka 		max_size = 0;
1368f6fccb12SMilan Broz 
1369b01cd5acSMikulas Patocka out_table:
13705037108aSMikulas Patocka 	dm_table_put(map);
13715037108aSMikulas Patocka 
13725037108aSMikulas Patocka out:
1373f6fccb12SMilan Broz 	/*
1374f6fccb12SMilan Broz 	 * Always allow an entire first page
1375f6fccb12SMilan Broz 	 */
1376f6fccb12SMilan Broz 	if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1377f6fccb12SMilan Broz 		max_size = biovec->bv_len;
1378f6fccb12SMilan Broz 
1379f6fccb12SMilan Broz 	return max_size;
1380f6fccb12SMilan Broz }
1381f6fccb12SMilan Broz 
13821da177e4SLinus Torvalds /*
13831da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
13841da177e4SLinus Torvalds  * dm_merge_bvec.
13851da177e4SLinus Torvalds  */
1386cec47e3dSKiyoshi Ueda static int _dm_request(struct request_queue *q, struct bio *bio)
13871da177e4SLinus Torvalds {
138812f03a49SKevin Corry 	int rw = bio_data_dir(bio);
13891da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
1390c9959059STejun Heo 	int cpu;
13911da177e4SLinus Torvalds 
13922ca3310eSAlasdair G Kergon 	down_read(&md->io_lock);
13931da177e4SLinus Torvalds 
1394074a7acaSTejun Heo 	cpu = part_stat_lock();
1395074a7acaSTejun Heo 	part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1396074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1397074a7acaSTejun Heo 	part_stat_unlock();
139812f03a49SKevin Corry 
13996a8736d1STejun Heo 	/* if we're suspended, we have to queue this io for later */
14006a8736d1STejun Heo 	if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
14012ca3310eSAlasdair G Kergon 		up_read(&md->io_lock);
14021da177e4SLinus Torvalds 
14036a8736d1STejun Heo 		if (bio_rw(bio) != READA)
140492c63902SMikulas Patocka 			queue_io(md, bio);
14056a8736d1STejun Heo 		else
14066a8736d1STejun Heo 			bio_io_error(bio);
140792c63902SMikulas Patocka 		return 0;
14081da177e4SLinus Torvalds 	}
14091da177e4SLinus Torvalds 
1410f0b9a450SMikulas Patocka 	__split_and_process_bio(md, bio);
14112ca3310eSAlasdair G Kergon 	up_read(&md->io_lock);
1412f0b9a450SMikulas Patocka 	return 0;
14131da177e4SLinus Torvalds }
14141da177e4SLinus Torvalds 
1415cec47e3dSKiyoshi Ueda static int dm_make_request(struct request_queue *q, struct bio *bio)
1416cec47e3dSKiyoshi Ueda {
1417cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1418cec47e3dSKiyoshi Ueda 
1419cec47e3dSKiyoshi Ueda 	return md->saved_make_request_fn(q, bio); /* call __make_request() */
1420cec47e3dSKiyoshi Ueda }
1421cec47e3dSKiyoshi Ueda 
1422cec47e3dSKiyoshi Ueda static int dm_request_based(struct mapped_device *md)
1423cec47e3dSKiyoshi Ueda {
1424cec47e3dSKiyoshi Ueda 	return blk_queue_stackable(md->queue);
1425cec47e3dSKiyoshi Ueda }
1426cec47e3dSKiyoshi Ueda 
1427cec47e3dSKiyoshi Ueda static int dm_request(struct request_queue *q, struct bio *bio)
1428cec47e3dSKiyoshi Ueda {
1429cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1430cec47e3dSKiyoshi Ueda 
1431cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
1432cec47e3dSKiyoshi Ueda 		return dm_make_request(q, bio);
1433cec47e3dSKiyoshi Ueda 
1434cec47e3dSKiyoshi Ueda 	return _dm_request(q, bio);
1435cec47e3dSKiyoshi Ueda }
1436cec47e3dSKiyoshi Ueda 
1437cec47e3dSKiyoshi Ueda void dm_dispatch_request(struct request *rq)
1438cec47e3dSKiyoshi Ueda {
1439cec47e3dSKiyoshi Ueda 	int r;
1440cec47e3dSKiyoshi Ueda 
1441cec47e3dSKiyoshi Ueda 	if (blk_queue_io_stat(rq->q))
1442cec47e3dSKiyoshi Ueda 		rq->cmd_flags |= REQ_IO_STAT;
1443cec47e3dSKiyoshi Ueda 
1444cec47e3dSKiyoshi Ueda 	rq->start_time = jiffies;
1445cec47e3dSKiyoshi Ueda 	r = blk_insert_cloned_request(rq->q, rq);
1446cec47e3dSKiyoshi Ueda 	if (r)
1447cec47e3dSKiyoshi Ueda 		dm_complete_request(rq, r);
1448cec47e3dSKiyoshi Ueda }
1449cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_dispatch_request);
1450cec47e3dSKiyoshi Ueda 
1451cec47e3dSKiyoshi Ueda static void dm_rq_bio_destructor(struct bio *bio)
1452cec47e3dSKiyoshi Ueda {
1453cec47e3dSKiyoshi Ueda 	struct dm_rq_clone_bio_info *info = bio->bi_private;
1454cec47e3dSKiyoshi Ueda 	struct mapped_device *md = info->tio->md;
1455cec47e3dSKiyoshi Ueda 
1456cec47e3dSKiyoshi Ueda 	free_bio_info(info);
1457cec47e3dSKiyoshi Ueda 	bio_free(bio, md->bs);
1458cec47e3dSKiyoshi Ueda }
1459cec47e3dSKiyoshi Ueda 
1460cec47e3dSKiyoshi Ueda static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1461cec47e3dSKiyoshi Ueda 				 void *data)
1462cec47e3dSKiyoshi Ueda {
1463cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = data;
1464cec47e3dSKiyoshi Ueda 	struct mapped_device *md = tio->md;
1465cec47e3dSKiyoshi Ueda 	struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1466cec47e3dSKiyoshi Ueda 
1467cec47e3dSKiyoshi Ueda 	if (!info)
1468cec47e3dSKiyoshi Ueda 		return -ENOMEM;
1469cec47e3dSKiyoshi Ueda 
1470cec47e3dSKiyoshi Ueda 	info->orig = bio_orig;
1471cec47e3dSKiyoshi Ueda 	info->tio = tio;
1472cec47e3dSKiyoshi Ueda 	bio->bi_end_io = end_clone_bio;
1473cec47e3dSKiyoshi Ueda 	bio->bi_private = info;
1474cec47e3dSKiyoshi Ueda 	bio->bi_destructor = dm_rq_bio_destructor;
1475cec47e3dSKiyoshi Ueda 
1476cec47e3dSKiyoshi Ueda 	return 0;
1477cec47e3dSKiyoshi Ueda }
1478cec47e3dSKiyoshi Ueda 
1479cec47e3dSKiyoshi Ueda static int setup_clone(struct request *clone, struct request *rq,
1480cec47e3dSKiyoshi Ueda 		       struct dm_rq_target_io *tio)
1481cec47e3dSKiyoshi Ueda {
1482d0bcb878SKiyoshi Ueda 	int r;
1483cec47e3dSKiyoshi Ueda 
1484d0bcb878SKiyoshi Ueda 	r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1485d0bcb878SKiyoshi Ueda 			      dm_rq_bio_constructor, tio);
1486cec47e3dSKiyoshi Ueda 	if (r)
1487cec47e3dSKiyoshi Ueda 		return r;
1488cec47e3dSKiyoshi Ueda 
1489cec47e3dSKiyoshi Ueda 	clone->cmd = rq->cmd;
1490cec47e3dSKiyoshi Ueda 	clone->cmd_len = rq->cmd_len;
1491cec47e3dSKiyoshi Ueda 	clone->sense = rq->sense;
1492cec47e3dSKiyoshi Ueda 	clone->buffer = rq->buffer;
1493cec47e3dSKiyoshi Ueda 	clone->end_io = end_clone_request;
1494cec47e3dSKiyoshi Ueda 	clone->end_io_data = tio;
1495cec47e3dSKiyoshi Ueda 
1496cec47e3dSKiyoshi Ueda 	return 0;
1497cec47e3dSKiyoshi Ueda }
1498cec47e3dSKiyoshi Ueda 
14996facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md,
15006facdaffSKiyoshi Ueda 				gfp_t gfp_mask)
15016facdaffSKiyoshi Ueda {
15026facdaffSKiyoshi Ueda 	struct request *clone;
15036facdaffSKiyoshi Ueda 	struct dm_rq_target_io *tio;
15046facdaffSKiyoshi Ueda 
15056facdaffSKiyoshi Ueda 	tio = alloc_rq_tio(md, gfp_mask);
15066facdaffSKiyoshi Ueda 	if (!tio)
15076facdaffSKiyoshi Ueda 		return NULL;
15086facdaffSKiyoshi Ueda 
15096facdaffSKiyoshi Ueda 	tio->md = md;
15106facdaffSKiyoshi Ueda 	tio->ti = NULL;
15116facdaffSKiyoshi Ueda 	tio->orig = rq;
15126facdaffSKiyoshi Ueda 	tio->error = 0;
15136facdaffSKiyoshi Ueda 	memset(&tio->info, 0, sizeof(tio->info));
15146facdaffSKiyoshi Ueda 
15156facdaffSKiyoshi Ueda 	clone = &tio->clone;
15166facdaffSKiyoshi Ueda 	if (setup_clone(clone, rq, tio)) {
15176facdaffSKiyoshi Ueda 		/* -ENOMEM */
15186facdaffSKiyoshi Ueda 		free_rq_tio(tio);
15196facdaffSKiyoshi Ueda 		return NULL;
15206facdaffSKiyoshi Ueda 	}
15216facdaffSKiyoshi Ueda 
15226facdaffSKiyoshi Ueda 	return clone;
15236facdaffSKiyoshi Ueda }
15246facdaffSKiyoshi Ueda 
1525cec47e3dSKiyoshi Ueda /*
1526cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1527cec47e3dSKiyoshi Ueda  */
1528cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq)
1529cec47e3dSKiyoshi Ueda {
1530cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1531cec47e3dSKiyoshi Ueda 	struct request *clone;
1532cec47e3dSKiyoshi Ueda 
1533cec47e3dSKiyoshi Ueda 	if (unlikely(rq->special)) {
1534cec47e3dSKiyoshi Ueda 		DMWARN("Already has something in rq->special.");
1535cec47e3dSKiyoshi Ueda 		return BLKPREP_KILL;
1536cec47e3dSKiyoshi Ueda 	}
1537cec47e3dSKiyoshi Ueda 
15386facdaffSKiyoshi Ueda 	clone = clone_rq(rq, md, GFP_ATOMIC);
15396facdaffSKiyoshi Ueda 	if (!clone)
1540cec47e3dSKiyoshi Ueda 		return BLKPREP_DEFER;
1541cec47e3dSKiyoshi Ueda 
1542cec47e3dSKiyoshi Ueda 	rq->special = clone;
1543cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_DONTPREP;
1544cec47e3dSKiyoshi Ueda 
1545cec47e3dSKiyoshi Ueda 	return BLKPREP_OK;
1546cec47e3dSKiyoshi Ueda }
1547cec47e3dSKiyoshi Ueda 
15489eef87daSKiyoshi Ueda /*
15499eef87daSKiyoshi Ueda  * Returns:
15509eef87daSKiyoshi Ueda  * 0  : the request has been processed (not requeued)
15519eef87daSKiyoshi Ueda  * !0 : the request has been requeued
15529eef87daSKiyoshi Ueda  */
15539eef87daSKiyoshi Ueda static int map_request(struct dm_target *ti, struct request *clone,
1554cec47e3dSKiyoshi Ueda 		       struct mapped_device *md)
1555cec47e3dSKiyoshi Ueda {
15569eef87daSKiyoshi Ueda 	int r, requeued = 0;
1557cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1558cec47e3dSKiyoshi Ueda 
1559cec47e3dSKiyoshi Ueda 	/*
1560cec47e3dSKiyoshi Ueda 	 * Hold the md reference here for the in-flight I/O.
1561cec47e3dSKiyoshi Ueda 	 * We can't rely on the reference count by device opener,
1562cec47e3dSKiyoshi Ueda 	 * because the device may be closed during the request completion
1563cec47e3dSKiyoshi Ueda 	 * when all bios are completed.
1564cec47e3dSKiyoshi Ueda 	 * See the comment in rq_completed() too.
1565cec47e3dSKiyoshi Ueda 	 */
1566cec47e3dSKiyoshi Ueda 	dm_get(md);
1567cec47e3dSKiyoshi Ueda 
1568cec47e3dSKiyoshi Ueda 	tio->ti = ti;
1569cec47e3dSKiyoshi Ueda 	r = ti->type->map_rq(ti, clone, &tio->info);
1570cec47e3dSKiyoshi Ueda 	switch (r) {
1571cec47e3dSKiyoshi Ueda 	case DM_MAPIO_SUBMITTED:
1572cec47e3dSKiyoshi Ueda 		/* The target has taken the I/O to submit by itself later */
1573cec47e3dSKiyoshi Ueda 		break;
1574cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REMAPPED:
1575cec47e3dSKiyoshi Ueda 		/* The target has remapped the I/O so dispatch it */
15766db4ccd6SJun'ichi Nomura 		trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
15776db4ccd6SJun'ichi Nomura 				     blk_rq_pos(tio->orig));
1578cec47e3dSKiyoshi Ueda 		dm_dispatch_request(clone);
1579cec47e3dSKiyoshi Ueda 		break;
1580cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REQUEUE:
1581cec47e3dSKiyoshi Ueda 		/* The target wants to requeue the I/O */
1582cec47e3dSKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
15839eef87daSKiyoshi Ueda 		requeued = 1;
1584cec47e3dSKiyoshi Ueda 		break;
1585cec47e3dSKiyoshi Ueda 	default:
1586cec47e3dSKiyoshi Ueda 		if (r > 0) {
1587cec47e3dSKiyoshi Ueda 			DMWARN("unimplemented target map return value: %d", r);
1588cec47e3dSKiyoshi Ueda 			BUG();
1589cec47e3dSKiyoshi Ueda 		}
1590cec47e3dSKiyoshi Ueda 
1591cec47e3dSKiyoshi Ueda 		/* The target wants to complete the I/O */
1592cec47e3dSKiyoshi Ueda 		dm_kill_unmapped_request(clone, r);
1593cec47e3dSKiyoshi Ueda 		break;
1594cec47e3dSKiyoshi Ueda 	}
15959eef87daSKiyoshi Ueda 
15969eef87daSKiyoshi Ueda 	return requeued;
1597cec47e3dSKiyoshi Ueda }
1598cec47e3dSKiyoshi Ueda 
1599cec47e3dSKiyoshi Ueda /*
1600cec47e3dSKiyoshi Ueda  * q->request_fn for request-based dm.
1601cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1602cec47e3dSKiyoshi Ueda  */
1603cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q)
1604cec47e3dSKiyoshi Ueda {
1605cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
16067c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1607cec47e3dSKiyoshi Ueda 	struct dm_target *ti;
1608b4324feeSKiyoshi Ueda 	struct request *rq, *clone;
160929e4013dSTejun Heo 	sector_t pos;
1610cec47e3dSKiyoshi Ueda 
1611cec47e3dSKiyoshi Ueda 	/*
1612b4324feeSKiyoshi Ueda 	 * For suspend, check blk_queue_stopped() and increment
1613b4324feeSKiyoshi Ueda 	 * ->pending within a single queue_lock not to increment the
1614b4324feeSKiyoshi Ueda 	 * number of in-flight I/Os after the queue is stopped in
1615b4324feeSKiyoshi Ueda 	 * dm_suspend().
1616cec47e3dSKiyoshi Ueda 	 */
1617cec47e3dSKiyoshi Ueda 	while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1618cec47e3dSKiyoshi Ueda 		rq = blk_peek_request(q);
1619cec47e3dSKiyoshi Ueda 		if (!rq)
1620cec47e3dSKiyoshi Ueda 			goto plug_and_out;
1621cec47e3dSKiyoshi Ueda 
162229e4013dSTejun Heo 		/* always use block 0 to find the target for flushes for now */
162329e4013dSTejun Heo 		pos = 0;
162429e4013dSTejun Heo 		if (!(rq->cmd_flags & REQ_FLUSH))
162529e4013dSTejun Heo 			pos = blk_rq_pos(rq);
1626d0bcb878SKiyoshi Ueda 
162729e4013dSTejun Heo 		ti = dm_table_find_target(map, pos);
162829e4013dSTejun Heo 		BUG_ON(!dm_target_is_valid(ti));
162929e4013dSTejun Heo 
1630cec47e3dSKiyoshi Ueda 		if (ti->type->busy && ti->type->busy(ti))
1631cec47e3dSKiyoshi Ueda 			goto plug_and_out;
1632cec47e3dSKiyoshi Ueda 
1633cec47e3dSKiyoshi Ueda 		blk_start_request(rq);
1634b4324feeSKiyoshi Ueda 		clone = rq->special;
1635b4324feeSKiyoshi Ueda 		atomic_inc(&md->pending[rq_data_dir(clone)]);
1636b4324feeSKiyoshi Ueda 
1637cec47e3dSKiyoshi Ueda 		spin_unlock(q->queue_lock);
16389eef87daSKiyoshi Ueda 		if (map_request(ti, clone, md))
16399eef87daSKiyoshi Ueda 			goto requeued;
16409eef87daSKiyoshi Ueda 
1641cec47e3dSKiyoshi Ueda 		spin_lock_irq(q->queue_lock);
1642cec47e3dSKiyoshi Ueda 	}
1643cec47e3dSKiyoshi Ueda 
1644cec47e3dSKiyoshi Ueda 	goto out;
1645cec47e3dSKiyoshi Ueda 
16469eef87daSKiyoshi Ueda requeued:
16479eef87daSKiyoshi Ueda 	spin_lock_irq(q->queue_lock);
16489eef87daSKiyoshi Ueda 
1649cec47e3dSKiyoshi Ueda plug_and_out:
1650cec47e3dSKiyoshi Ueda 	if (!elv_queue_empty(q))
1651cec47e3dSKiyoshi Ueda 		/* Some requests still remain, retry later */
1652cec47e3dSKiyoshi Ueda 		blk_plug_device(q);
1653cec47e3dSKiyoshi Ueda 
1654cec47e3dSKiyoshi Ueda out:
1655cec47e3dSKiyoshi Ueda 	dm_table_put(map);
1656cec47e3dSKiyoshi Ueda 
1657cec47e3dSKiyoshi Ueda 	return;
1658cec47e3dSKiyoshi Ueda }
1659cec47e3dSKiyoshi Ueda 
1660cec47e3dSKiyoshi Ueda int dm_underlying_device_busy(struct request_queue *q)
1661cec47e3dSKiyoshi Ueda {
1662cec47e3dSKiyoshi Ueda 	return blk_lld_busy(q);
1663cec47e3dSKiyoshi Ueda }
1664cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1665cec47e3dSKiyoshi Ueda 
1666cec47e3dSKiyoshi Ueda static int dm_lld_busy(struct request_queue *q)
1667cec47e3dSKiyoshi Ueda {
1668cec47e3dSKiyoshi Ueda 	int r;
1669cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
16707c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1671cec47e3dSKiyoshi Ueda 
1672cec47e3dSKiyoshi Ueda 	if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1673cec47e3dSKiyoshi Ueda 		r = 1;
1674cec47e3dSKiyoshi Ueda 	else
1675cec47e3dSKiyoshi Ueda 		r = dm_table_any_busy_target(map);
1676cec47e3dSKiyoshi Ueda 
1677cec47e3dSKiyoshi Ueda 	dm_table_put(map);
1678cec47e3dSKiyoshi Ueda 
1679cec47e3dSKiyoshi Ueda 	return r;
1680cec47e3dSKiyoshi Ueda }
1681cec47e3dSKiyoshi Ueda 
1682165125e1SJens Axboe static void dm_unplug_all(struct request_queue *q)
16831da177e4SLinus Torvalds {
16841da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
16857c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds 	if (map) {
1688cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
1689cec47e3dSKiyoshi Ueda 			generic_unplug_device(q);
1690cec47e3dSKiyoshi Ueda 
16911da177e4SLinus Torvalds 		dm_table_unplug_all(map);
16921da177e4SLinus Torvalds 		dm_table_put(map);
16931da177e4SLinus Torvalds 	}
16941da177e4SLinus Torvalds }
16951da177e4SLinus Torvalds 
16961da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
16971da177e4SLinus Torvalds {
16988a57dfc6SChandra Seetharaman 	int r = bdi_bits;
16998a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
17008a57dfc6SChandra Seetharaman 	struct dm_table *map;
17011da177e4SLinus Torvalds 
17021eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
17037c666411SAlasdair G Kergon 		map = dm_get_live_table(md);
17048a57dfc6SChandra Seetharaman 		if (map) {
1705cec47e3dSKiyoshi Ueda 			/*
1706cec47e3dSKiyoshi Ueda 			 * Request-based dm cares about only own queue for
1707cec47e3dSKiyoshi Ueda 			 * the query about congestion status of request_queue
1708cec47e3dSKiyoshi Ueda 			 */
1709cec47e3dSKiyoshi Ueda 			if (dm_request_based(md))
1710cec47e3dSKiyoshi Ueda 				r = md->queue->backing_dev_info.state &
1711cec47e3dSKiyoshi Ueda 				    bdi_bits;
1712cec47e3dSKiyoshi Ueda 			else
17131da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
1714cec47e3dSKiyoshi Ueda 
17151da177e4SLinus Torvalds 			dm_table_put(map);
17168a57dfc6SChandra Seetharaman 		}
17178a57dfc6SChandra Seetharaman 	}
17188a57dfc6SChandra Seetharaman 
17191da177e4SLinus Torvalds 	return r;
17201da177e4SLinus Torvalds }
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds /*-----------------------------------------------------------------
17231da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
17241da177e4SLinus Torvalds  *---------------------------------------------------------------*/
17251da177e4SLinus Torvalds static DEFINE_IDR(_minor_idr);
17261da177e4SLinus Torvalds 
17272b06cfffSAlasdair G Kergon static void free_minor(int minor)
17281da177e4SLinus Torvalds {
1729f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17301da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1731f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
17321da177e4SLinus Torvalds }
17331da177e4SLinus Torvalds 
17341da177e4SLinus Torvalds /*
17351da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
17361da177e4SLinus Torvalds  */
1737cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
17381da177e4SLinus Torvalds {
17391da177e4SLinus Torvalds 	int r, m;
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
17421da177e4SLinus Torvalds 		return -EINVAL;
17431da177e4SLinus Torvalds 
174462f75c2fSJeff Mahoney 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
174562f75c2fSJeff Mahoney 	if (!r)
174662f75c2fSJeff Mahoney 		return -ENOMEM;
174762f75c2fSJeff Mahoney 
1748f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17491da177e4SLinus Torvalds 
17501da177e4SLinus Torvalds 	if (idr_find(&_minor_idr, minor)) {
17511da177e4SLinus Torvalds 		r = -EBUSY;
17521da177e4SLinus Torvalds 		goto out;
17531da177e4SLinus Torvalds 	}
17541da177e4SLinus Torvalds 
1755ba61fdd1SJeff Mahoney 	r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
175662f75c2fSJeff Mahoney 	if (r)
17571da177e4SLinus Torvalds 		goto out;
17581da177e4SLinus Torvalds 
17591da177e4SLinus Torvalds 	if (m != minor) {
17601da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
17611da177e4SLinus Torvalds 		r = -EBUSY;
17621da177e4SLinus Torvalds 		goto out;
17631da177e4SLinus Torvalds 	}
17641da177e4SLinus Torvalds 
17651da177e4SLinus Torvalds out:
1766f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
17671da177e4SLinus Torvalds 	return r;
17681da177e4SLinus Torvalds }
17691da177e4SLinus Torvalds 
1770cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
17711da177e4SLinus Torvalds {
17722b06cfffSAlasdair G Kergon 	int r, m;
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
177562f75c2fSJeff Mahoney 	if (!r)
177662f75c2fSJeff Mahoney 		return -ENOMEM;
177762f75c2fSJeff Mahoney 
1778f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
17791da177e4SLinus Torvalds 
1780ba61fdd1SJeff Mahoney 	r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
1781cf13ab8eSFrederik Deweerdt 	if (r)
17821da177e4SLinus Torvalds 		goto out;
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds 	if (m >= (1 << MINORBITS)) {
17851da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
17861da177e4SLinus Torvalds 		r = -ENOSPC;
17871da177e4SLinus Torvalds 		goto out;
17881da177e4SLinus Torvalds 	}
17891da177e4SLinus Torvalds 
17901da177e4SLinus Torvalds 	*minor = m;
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds out:
1793f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
17941da177e4SLinus Torvalds 	return r;
17951da177e4SLinus Torvalds }
17961da177e4SLinus Torvalds 
179783d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
17981da177e4SLinus Torvalds 
179953d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
180053d5914fSMikulas Patocka 
18014a0b4ddfSMike Snitzer static void dm_init_md_queue(struct mapped_device *md)
18024a0b4ddfSMike Snitzer {
18034a0b4ddfSMike Snitzer 	/*
18044a0b4ddfSMike Snitzer 	 * Request-based dm devices cannot be stacked on top of bio-based dm
18054a0b4ddfSMike Snitzer 	 * devices.  The type of this dm device has not been decided yet.
18064a0b4ddfSMike Snitzer 	 * The type is decided at the first table loading time.
18074a0b4ddfSMike Snitzer 	 * To prevent problematic device stacking, clear the queue flag
18084a0b4ddfSMike Snitzer 	 * for request stacking support until then.
18094a0b4ddfSMike Snitzer 	 *
18104a0b4ddfSMike Snitzer 	 * This queue is new, so no concurrency on the queue_flags.
18114a0b4ddfSMike Snitzer 	 */
18124a0b4ddfSMike Snitzer 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
18134a0b4ddfSMike Snitzer 
18144a0b4ddfSMike Snitzer 	md->queue->queuedata = md;
18154a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
18164a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_data = md;
18174a0b4ddfSMike Snitzer 	blk_queue_make_request(md->queue, dm_request);
18184a0b4ddfSMike Snitzer 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
18194a0b4ddfSMike Snitzer 	md->queue->unplug_fn = dm_unplug_all;
18204a0b4ddfSMike Snitzer 	blk_queue_merge_bvec(md->queue, dm_merge_bvec);
1821d87f4c14STejun Heo 	blk_queue_flush(md->queue, REQ_FLUSH | REQ_FUA);
18224a0b4ddfSMike Snitzer }
18234a0b4ddfSMike Snitzer 
18241da177e4SLinus Torvalds /*
18251da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
18261da177e4SLinus Torvalds  */
18272b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
18281da177e4SLinus Torvalds {
18291da177e4SLinus Torvalds 	int r;
1830cf13ab8eSFrederik Deweerdt 	struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1831ba61fdd1SJeff Mahoney 	void *old_md;
18321da177e4SLinus Torvalds 
18331da177e4SLinus Torvalds 	if (!md) {
18341da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
18351da177e4SLinus Torvalds 		return NULL;
18361da177e4SLinus Torvalds 	}
18371da177e4SLinus Torvalds 
183810da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
18396ed7ade8SMilan Broz 		goto bad_module_get;
184010da4f79SJeff Mahoney 
18411da177e4SLinus Torvalds 	/* get a minor number for the dev */
18422b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1843cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
18442b06cfffSAlasdair G Kergon 	else
1845cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
18461da177e4SLinus Torvalds 	if (r < 0)
18476ed7ade8SMilan Broz 		goto bad_minor;
18481da177e4SLinus Torvalds 
1849a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
18502ca3310eSAlasdair G Kergon 	init_rwsem(&md->io_lock);
1851e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1852a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
1853022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
18541da177e4SLinus Torvalds 	rwlock_init(&md->map_lock);
18551da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
18565c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
18571da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
18587a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
18597a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
18607a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
18611da177e4SLinus Torvalds 
18624a0b4ddfSMike Snitzer 	md->queue = blk_alloc_queue(GFP_KERNEL);
18631da177e4SLinus Torvalds 	if (!md->queue)
18646ed7ade8SMilan Broz 		goto bad_queue;
18651da177e4SLinus Torvalds 
18664a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
18679faf400fSStefan Bader 
18681da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
18691da177e4SLinus Torvalds 	if (!md->disk)
18706ed7ade8SMilan Broz 		goto bad_disk;
18711da177e4SLinus Torvalds 
1872316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
1873316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
1874f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
187553d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1876f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
1877f0b04115SJeff Mahoney 
18781da177e4SLinus Torvalds 	md->disk->major = _major;
18791da177e4SLinus Torvalds 	md->disk->first_minor = minor;
18801da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
18811da177e4SLinus Torvalds 	md->disk->queue = md->queue;
18821da177e4SLinus Torvalds 	md->disk->private_data = md;
18831da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
18841da177e4SLinus Torvalds 	add_disk(md->disk);
18857e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
18861da177e4SLinus Torvalds 
1887304f3f6aSMilan Broz 	md->wq = create_singlethread_workqueue("kdmflush");
1888304f3f6aSMilan Broz 	if (!md->wq)
1889304f3f6aSMilan Broz 		goto bad_thread;
1890304f3f6aSMilan Broz 
189132a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
189232a926daSMikulas Patocka 	if (!md->bdev)
189332a926daSMikulas Patocka 		goto bad_bdev;
189432a926daSMikulas Patocka 
18956a8736d1STejun Heo 	bio_init(&md->flush_bio);
18966a8736d1STejun Heo 	md->flush_bio.bi_bdev = md->bdev;
18976a8736d1STejun Heo 	md->flush_bio.bi_rw = WRITE_FLUSH;
18986a8736d1STejun Heo 
1899ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1900f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1901ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1902f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1903ba61fdd1SJeff Mahoney 
1904ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1905ba61fdd1SJeff Mahoney 
19061da177e4SLinus Torvalds 	return md;
19071da177e4SLinus Torvalds 
190832a926daSMikulas Patocka bad_bdev:
190932a926daSMikulas Patocka 	destroy_workqueue(md->wq);
1910304f3f6aSMilan Broz bad_thread:
191103022c54SZdenek Kabelac 	del_gendisk(md->disk);
1912304f3f6aSMilan Broz 	put_disk(md->disk);
19136ed7ade8SMilan Broz bad_disk:
19141312f40eSAl Viro 	blk_cleanup_queue(md->queue);
19156ed7ade8SMilan Broz bad_queue:
19161da177e4SLinus Torvalds 	free_minor(minor);
19176ed7ade8SMilan Broz bad_minor:
191810da4f79SJeff Mahoney 	module_put(THIS_MODULE);
19196ed7ade8SMilan Broz bad_module_get:
19201da177e4SLinus Torvalds 	kfree(md);
19211da177e4SLinus Torvalds 	return NULL;
19221da177e4SLinus Torvalds }
19231da177e4SLinus Torvalds 
1924ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1925ae9da83fSJun'ichi Nomura 
19261da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
19271da177e4SLinus Torvalds {
1928f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
192963d94e48SJun'ichi Nomura 
1930ae9da83fSJun'ichi Nomura 	unlock_fs(md);
1931db8fef4fSMikulas Patocka 	bdput(md->bdev);
1932304f3f6aSMilan Broz 	destroy_workqueue(md->wq);
1933e6ee8c0bSKiyoshi Ueda 	if (md->tio_pool)
19341da177e4SLinus Torvalds 		mempool_destroy(md->tio_pool);
1935e6ee8c0bSKiyoshi Ueda 	if (md->io_pool)
19361da177e4SLinus Torvalds 		mempool_destroy(md->io_pool);
1937e6ee8c0bSKiyoshi Ueda 	if (md->bs)
19389faf400fSStefan Bader 		bioset_free(md->bs);
19399c47008dSMartin K. Petersen 	blk_integrity_unregister(md->disk);
19401da177e4SLinus Torvalds 	del_gendisk(md->disk);
194163d94e48SJun'ichi Nomura 	free_minor(minor);
1942fba9f90eSJeff Mahoney 
1943fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
1944fba9f90eSJeff Mahoney 	md->disk->private_data = NULL;
1945fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
1946fba9f90eSJeff Mahoney 
19471da177e4SLinus Torvalds 	put_disk(md->disk);
19481312f40eSAl Viro 	blk_cleanup_queue(md->queue);
194910da4f79SJeff Mahoney 	module_put(THIS_MODULE);
19501da177e4SLinus Torvalds 	kfree(md);
19511da177e4SLinus Torvalds }
19521da177e4SLinus Torvalds 
1953e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
1954e6ee8c0bSKiyoshi Ueda {
1955e6ee8c0bSKiyoshi Ueda 	struct dm_md_mempools *p;
1956e6ee8c0bSKiyoshi Ueda 
1957e6ee8c0bSKiyoshi Ueda 	if (md->io_pool && md->tio_pool && md->bs)
1958e6ee8c0bSKiyoshi Ueda 		/* the md already has necessary mempools */
1959e6ee8c0bSKiyoshi Ueda 		goto out;
1960e6ee8c0bSKiyoshi Ueda 
1961e6ee8c0bSKiyoshi Ueda 	p = dm_table_get_md_mempools(t);
1962e6ee8c0bSKiyoshi Ueda 	BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
1963e6ee8c0bSKiyoshi Ueda 
1964e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
1965e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
1966e6ee8c0bSKiyoshi Ueda 	md->tio_pool = p->tio_pool;
1967e6ee8c0bSKiyoshi Ueda 	p->tio_pool = NULL;
1968e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
1969e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
1970e6ee8c0bSKiyoshi Ueda 
1971e6ee8c0bSKiyoshi Ueda out:
1972e6ee8c0bSKiyoshi Ueda 	/* mempool bind completed, now no need any mempools in the table */
1973e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
1974e6ee8c0bSKiyoshi Ueda }
1975e6ee8c0bSKiyoshi Ueda 
19761da177e4SLinus Torvalds /*
19771da177e4SLinus Torvalds  * Bind a table to the device.
19781da177e4SLinus Torvalds  */
19791da177e4SLinus Torvalds static void event_callback(void *context)
19801da177e4SLinus Torvalds {
19817a8c3d3bSMike Anderson 	unsigned long flags;
19827a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
19831da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
19841da177e4SLinus Torvalds 
19857a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
19867a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
19877a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
19887a8c3d3bSMike Anderson 
1989ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
19907a8c3d3bSMike Anderson 
19911da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
19921da177e4SLinus Torvalds 	wake_up(&md->eventq);
19931da177e4SLinus Torvalds }
19941da177e4SLinus Torvalds 
19954e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
19961da177e4SLinus Torvalds {
19974e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
19981da177e4SLinus Torvalds 
1999db8fef4fSMikulas Patocka 	mutex_lock(&md->bdev->bd_inode->i_mutex);
2000db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
2001db8fef4fSMikulas Patocka 	mutex_unlock(&md->bdev->bd_inode->i_mutex);
20021da177e4SLinus Torvalds }
20031da177e4SLinus Torvalds 
2004042d2a9bSAlasdair G Kergon /*
2005042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2006042d2a9bSAlasdair G Kergon  */
2007042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2008754c5fc7SMike Snitzer 			       struct queue_limits *limits)
20091da177e4SLinus Torvalds {
2010042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
2011165125e1SJens Axboe 	struct request_queue *q = md->queue;
20121da177e4SLinus Torvalds 	sector_t size;
2013523d9297SKiyoshi Ueda 	unsigned long flags;
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds 	size = dm_table_get_size(t);
20163ac51e74SDarrick J. Wong 
20173ac51e74SDarrick J. Wong 	/*
20183ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
20193ac51e74SDarrick J. Wong 	 */
20203ac51e74SDarrick J. Wong 	if (size != get_capacity(md->disk))
20213ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
20223ac51e74SDarrick J. Wong 
20234e90188bSAlasdair G Kergon 	__set_size(md, size);
20241da177e4SLinus Torvalds 
2025cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
20262ca3310eSAlasdair G Kergon 
2027e6ee8c0bSKiyoshi Ueda 	/*
2028e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
2029e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
2030e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
2031e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
2032e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
2033e6ee8c0bSKiyoshi Ueda 	 */
2034e6ee8c0bSKiyoshi Ueda 	if (dm_table_request_based(t) && !blk_queue_stopped(q))
2035e6ee8c0bSKiyoshi Ueda 		stop_queue(q);
2036e6ee8c0bSKiyoshi Ueda 
2037e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
2038e6ee8c0bSKiyoshi Ueda 
2039523d9297SKiyoshi Ueda 	write_lock_irqsave(&md->map_lock, flags);
2040042d2a9bSAlasdair G Kergon 	old_map = md->map;
20412ca3310eSAlasdair G Kergon 	md->map = t;
2042754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
2043523d9297SKiyoshi Ueda 	write_unlock_irqrestore(&md->map_lock, flags);
20442ca3310eSAlasdair G Kergon 
2045042d2a9bSAlasdair G Kergon 	return old_map;
20461da177e4SLinus Torvalds }
20471da177e4SLinus Torvalds 
2048a7940155SAlasdair G Kergon /*
2049a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2050a7940155SAlasdair G Kergon  */
2051a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
20521da177e4SLinus Torvalds {
20531da177e4SLinus Torvalds 	struct dm_table *map = md->map;
2054523d9297SKiyoshi Ueda 	unsigned long flags;
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 	if (!map)
2057a7940155SAlasdair G Kergon 		return NULL;
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
2060523d9297SKiyoshi Ueda 	write_lock_irqsave(&md->map_lock, flags);
20611da177e4SLinus Torvalds 	md->map = NULL;
2062523d9297SKiyoshi Ueda 	write_unlock_irqrestore(&md->map_lock, flags);
2063a7940155SAlasdair G Kergon 
2064a7940155SAlasdair G Kergon 	return map;
20651da177e4SLinus Torvalds }
20661da177e4SLinus Torvalds 
20671da177e4SLinus Torvalds /*
20681da177e4SLinus Torvalds  * Constructor for a new device.
20691da177e4SLinus Torvalds  */
20702b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
20711da177e4SLinus Torvalds {
20721da177e4SLinus Torvalds 	struct mapped_device *md;
20731da177e4SLinus Torvalds 
20742b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
20751da177e4SLinus Torvalds 	if (!md)
20761da177e4SLinus Torvalds 		return -ENXIO;
20771da177e4SLinus Torvalds 
2078784aae73SMilan Broz 	dm_sysfs_init(md);
2079784aae73SMilan Broz 
20801da177e4SLinus Torvalds 	*result = md;
20811da177e4SLinus Torvalds 	return 0;
20821da177e4SLinus Torvalds }
20831da177e4SLinus Torvalds 
2084a5664dadSMike Snitzer /*
2085a5664dadSMike Snitzer  * Functions to manage md->type.
2086a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2087a5664dadSMike Snitzer  */
2088a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2089a5664dadSMike Snitzer {
2090a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2091a5664dadSMike Snitzer }
2092a5664dadSMike Snitzer 
2093a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2094a5664dadSMike Snitzer {
2095a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2096a5664dadSMike Snitzer }
2097a5664dadSMike Snitzer 
2098a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type)
2099a5664dadSMike Snitzer {
2100a5664dadSMike Snitzer 	md->type = type;
2101a5664dadSMike Snitzer }
2102a5664dadSMike Snitzer 
2103a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md)
2104a5664dadSMike Snitzer {
2105a5664dadSMike Snitzer 	return md->type;
2106a5664dadSMike Snitzer }
2107a5664dadSMike Snitzer 
21084a0b4ddfSMike Snitzer /*
21094a0b4ddfSMike Snitzer  * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
21104a0b4ddfSMike Snitzer  */
21114a0b4ddfSMike Snitzer static int dm_init_request_based_queue(struct mapped_device *md)
21124a0b4ddfSMike Snitzer {
21134a0b4ddfSMike Snitzer 	struct request_queue *q = NULL;
21144a0b4ddfSMike Snitzer 
21154a0b4ddfSMike Snitzer 	if (md->queue->elevator)
21164a0b4ddfSMike Snitzer 		return 1;
21174a0b4ddfSMike Snitzer 
21184a0b4ddfSMike Snitzer 	/* Fully initialize the queue */
21194a0b4ddfSMike Snitzer 	q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
21204a0b4ddfSMike Snitzer 	if (!q)
21214a0b4ddfSMike Snitzer 		return 0;
21224a0b4ddfSMike Snitzer 
21234a0b4ddfSMike Snitzer 	md->queue = q;
21244a0b4ddfSMike Snitzer 	md->saved_make_request_fn = md->queue->make_request_fn;
21254a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
21264a0b4ddfSMike Snitzer 	blk_queue_softirq_done(md->queue, dm_softirq_done);
21274a0b4ddfSMike Snitzer 	blk_queue_prep_rq(md->queue, dm_prep_fn);
21284a0b4ddfSMike Snitzer 	blk_queue_lld_busy(md->queue, dm_lld_busy);
21294a0b4ddfSMike Snitzer 
21304a0b4ddfSMike Snitzer 	elv_register_queue(md->queue);
21314a0b4ddfSMike Snitzer 
21324a0b4ddfSMike Snitzer 	return 1;
21334a0b4ddfSMike Snitzer }
21344a0b4ddfSMike Snitzer 
21354a0b4ddfSMike Snitzer /*
21364a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
21374a0b4ddfSMike Snitzer  */
21384a0b4ddfSMike Snitzer int dm_setup_md_queue(struct mapped_device *md)
21394a0b4ddfSMike Snitzer {
21404a0b4ddfSMike Snitzer 	if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
21414a0b4ddfSMike Snitzer 	    !dm_init_request_based_queue(md)) {
21424a0b4ddfSMike Snitzer 		DMWARN("Cannot initialize queue for request-based mapped device");
21434a0b4ddfSMike Snitzer 		return -EINVAL;
21444a0b4ddfSMike Snitzer 	}
21454a0b4ddfSMike Snitzer 
21464a0b4ddfSMike Snitzer 	return 0;
21474a0b4ddfSMike Snitzer }
21484a0b4ddfSMike Snitzer 
2149637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev)
21501da177e4SLinus Torvalds {
21511da177e4SLinus Torvalds 	struct mapped_device *md;
21521da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
21551da177e4SLinus Torvalds 		return NULL;
21561da177e4SLinus Torvalds 
2157f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
2160fba9f90eSJeff Mahoney 	if (md && (md == MINOR_ALLOCED ||
2161f331c029STejun Heo 		   (MINOR(disk_devt(dm_disk(md))) != minor) ||
2162abdc568bSKiyoshi Ueda 		   dm_deleting_md(md) ||
2163fba9f90eSJeff Mahoney 		   test_bit(DMF_FREEING, &md->flags))) {
2164637842cfSDavid Teigland 		md = NULL;
2165fba9f90eSJeff Mahoney 		goto out;
2166fba9f90eSJeff Mahoney 	}
21671da177e4SLinus Torvalds 
2168fba9f90eSJeff Mahoney out:
2169f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
21701da177e4SLinus Torvalds 
2171637842cfSDavid Teigland 	return md;
2172637842cfSDavid Teigland }
2173637842cfSDavid Teigland 
2174d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev)
2175d229a958SDavid Teigland {
2176d229a958SDavid Teigland 	struct mapped_device *md = dm_find_md(dev);
2177d229a958SDavid Teigland 
2178d229a958SDavid Teigland 	if (md)
2179d229a958SDavid Teigland 		dm_get(md);
2180d229a958SDavid Teigland 
2181d229a958SDavid Teigland 	return md;
2182d229a958SDavid Teigland }
2183d229a958SDavid Teigland 
21849ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2185637842cfSDavid Teigland {
21869ade92a9SAlasdair G Kergon 	return md->interface_ptr;
21871da177e4SLinus Torvalds }
21881da177e4SLinus Torvalds 
21891da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
21901da177e4SLinus Torvalds {
21911da177e4SLinus Torvalds 	md->interface_ptr = ptr;
21921da177e4SLinus Torvalds }
21931da177e4SLinus Torvalds 
21941da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
21951da177e4SLinus Torvalds {
21961da177e4SLinus Torvalds 	atomic_inc(&md->holders);
21973f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
21981da177e4SLinus Torvalds }
21991da177e4SLinus Torvalds 
220072d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
220172d94861SAlasdair G Kergon {
220272d94861SAlasdair G Kergon 	return md->name;
220372d94861SAlasdair G Kergon }
220472d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
220572d94861SAlasdair G Kergon 
22063f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
22071da177e4SLinus Torvalds {
22081134e5aeSMike Anderson 	struct dm_table *map;
22091da177e4SLinus Torvalds 
22103f77316dSKiyoshi Ueda 	might_sleep();
2211fba9f90eSJeff Mahoney 
22123f77316dSKiyoshi Ueda 	spin_lock(&_minor_lock);
22137c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
22143f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2215fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2216f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
22173f77316dSKiyoshi Ueda 
22184f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
22191da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
22201da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
22211da177e4SLinus Torvalds 	}
22223f77316dSKiyoshi Ueda 
22233f77316dSKiyoshi Ueda 	/*
22243f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
22253f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
22263f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
22273f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
22283f77316dSKiyoshi Ueda 	 */
22293f77316dSKiyoshi Ueda 	if (wait)
22303f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
22313f77316dSKiyoshi Ueda 			msleep(1);
22323f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
22333f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
22343f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
22353f77316dSKiyoshi Ueda 
2236784aae73SMilan Broz 	dm_sysfs_exit(md);
22371134e5aeSMike Anderson 	dm_table_put(map);
2238a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
22391da177e4SLinus Torvalds 	free_dev(md);
22401da177e4SLinus Torvalds }
22413f77316dSKiyoshi Ueda 
22423f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
22433f77316dSKiyoshi Ueda {
22443f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
22453f77316dSKiyoshi Ueda }
22463f77316dSKiyoshi Ueda 
22473f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
22483f77316dSKiyoshi Ueda {
22493f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
22503f77316dSKiyoshi Ueda }
22513f77316dSKiyoshi Ueda 
22523f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
22533f77316dSKiyoshi Ueda {
22543f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
22551da177e4SLinus Torvalds }
225679eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
22571da177e4SLinus Torvalds 
2258401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
225946125c1cSMilan Broz {
226046125c1cSMilan Broz 	int r = 0;
2261b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
2262b44ebeb0SMikulas Patocka 
2263b44ebeb0SMikulas Patocka 	dm_unplug_all(md->queue);
2264b44ebeb0SMikulas Patocka 
2265b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
226646125c1cSMilan Broz 
226746125c1cSMilan Broz 	while (1) {
2268401600dfSMikulas Patocka 		set_current_state(interruptible);
226946125c1cSMilan Broz 
227046125c1cSMilan Broz 		smp_mb();
2271b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
227246125c1cSMilan Broz 			break;
227346125c1cSMilan Broz 
2274401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
2275401600dfSMikulas Patocka 		    signal_pending(current)) {
227646125c1cSMilan Broz 			r = -EINTR;
227746125c1cSMilan Broz 			break;
227846125c1cSMilan Broz 		}
227946125c1cSMilan Broz 
228046125c1cSMilan Broz 		io_schedule();
228146125c1cSMilan Broz 	}
228246125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
228346125c1cSMilan Broz 
2284b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
2285b44ebeb0SMikulas Patocka 
228646125c1cSMilan Broz 	return r;
228746125c1cSMilan Broz }
228846125c1cSMilan Broz 
22891da177e4SLinus Torvalds /*
22901da177e4SLinus Torvalds  * Process the deferred bios
22911da177e4SLinus Torvalds  */
2292ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
22931da177e4SLinus Torvalds {
2294ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2295ef208587SMikulas Patocka 						work);
22966d6f10dfSMilan Broz 	struct bio *c;
22971da177e4SLinus Torvalds 
22986a8736d1STejun Heo 	down_read(&md->io_lock);
2299ef208587SMikulas Patocka 
23003b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2301022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2302022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2303022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2304022c2611SMikulas Patocka 
23056a8736d1STejun Heo 		if (!c)
2306df12ee99SAlasdair G Kergon 			break;
230773d410c0SMilan Broz 
23086a8736d1STejun Heo 		up_read(&md->io_lock);
23093b00b203SMikulas Patocka 
2310e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2311e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2312af7e466aSMikulas Patocka 		else
2313df12ee99SAlasdair G Kergon 			__split_and_process_bio(md, c);
23146a8736d1STejun Heo 
23156a8736d1STejun Heo 		down_read(&md->io_lock);
2316e6ee8c0bSKiyoshi Ueda 	}
23173b00b203SMikulas Patocka 
23186a8736d1STejun Heo 	up_read(&md->io_lock);
23191da177e4SLinus Torvalds }
23201da177e4SLinus Torvalds 
23219a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2322304f3f6aSMilan Broz {
23233b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
23243b00b203SMikulas Patocka 	smp_mb__after_clear_bit();
232553d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2326304f3f6aSMilan Broz }
2327304f3f6aSMilan Broz 
23281da177e4SLinus Torvalds /*
2329042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
23301da177e4SLinus Torvalds  */
2331042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
23321da177e4SLinus Torvalds {
2333042d2a9bSAlasdair G Kergon 	struct dm_table *map = ERR_PTR(-EINVAL);
2334754c5fc7SMike Snitzer 	struct queue_limits limits;
2335042d2a9bSAlasdair G Kergon 	int r;
23361da177e4SLinus Torvalds 
2337e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
23381da177e4SLinus Torvalds 
23391da177e4SLinus Torvalds 	/* device must be suspended */
23404f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
234193c534aeSAlasdair G Kergon 		goto out;
23421da177e4SLinus Torvalds 
2343754c5fc7SMike Snitzer 	r = dm_calculate_queue_limits(table, &limits);
2344042d2a9bSAlasdair G Kergon 	if (r) {
2345042d2a9bSAlasdair G Kergon 		map = ERR_PTR(r);
2346754c5fc7SMike Snitzer 		goto out;
2347042d2a9bSAlasdair G Kergon 	}
2348754c5fc7SMike Snitzer 
2349042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
23501da177e4SLinus Torvalds 
235193c534aeSAlasdair G Kergon out:
2352e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2353042d2a9bSAlasdair G Kergon 	return map;
23541da177e4SLinus Torvalds }
23551da177e4SLinus Torvalds 
23561da177e4SLinus Torvalds /*
23571da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
23581da177e4SLinus Torvalds  * device.
23591da177e4SLinus Torvalds  */
23602ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
23611da177e4SLinus Torvalds {
2362e39e2e95SAlasdair G Kergon 	int r;
23631da177e4SLinus Torvalds 
23641da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2365dfbe03f6SAlasdair G Kergon 
2366db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2367dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2368cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2369e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2370e39e2e95SAlasdair G Kergon 		return r;
2371dfbe03f6SAlasdair G Kergon 	}
2372dfbe03f6SAlasdair G Kergon 
2373aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2374aa8d7c2fSAlasdair G Kergon 
23751da177e4SLinus Torvalds 	return 0;
23761da177e4SLinus Torvalds }
23771da177e4SLinus Torvalds 
23782ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
23791da177e4SLinus Torvalds {
2380aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2381aa8d7c2fSAlasdair G Kergon 		return;
2382aa8d7c2fSAlasdair G Kergon 
2383db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
23841da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2385aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
23861da177e4SLinus Torvalds }
23871da177e4SLinus Torvalds 
23881da177e4SLinus Torvalds /*
23891da177e4SLinus Torvalds  * We need to be able to change a mapping table under a mounted
23901da177e4SLinus Torvalds  * filesystem.  For example we might want to move some data in
23911da177e4SLinus Torvalds  * the background.  Before the table can be swapped with
23921da177e4SLinus Torvalds  * dm_bind_table, dm_suspend must be called to flush any in
23931da177e4SLinus Torvalds  * flight bios and ensure that any further io gets deferred.
23941da177e4SLinus Torvalds  */
2395cec47e3dSKiyoshi Ueda /*
2396cec47e3dSKiyoshi Ueda  * Suspend mechanism in request-based dm.
2397cec47e3dSKiyoshi Ueda  *
23989f518b27SKiyoshi Ueda  * 1. Flush all I/Os by lock_fs() if needed.
23999f518b27SKiyoshi Ueda  * 2. Stop dispatching any I/O by stopping the request_queue.
24009f518b27SKiyoshi Ueda  * 3. Wait for all in-flight I/Os to be completed or requeued.
2401cec47e3dSKiyoshi Ueda  *
24029f518b27SKiyoshi Ueda  * To abort suspend, start the request_queue.
2403cec47e3dSKiyoshi Ueda  */
2404a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
24051da177e4SLinus Torvalds {
24062ca3310eSAlasdair G Kergon 	struct dm_table *map = NULL;
240746125c1cSMilan Broz 	int r = 0;
2408a3d77d35SKiyoshi Ueda 	int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
24092e93ccc1SKiyoshi Ueda 	int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
24101da177e4SLinus Torvalds 
2411e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
24122ca3310eSAlasdair G Kergon 
24134f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
241473d410c0SMilan Broz 		r = -EINVAL;
2415d287483dSAlasdair G Kergon 		goto out_unlock;
241673d410c0SMilan Broz 	}
24171da177e4SLinus Torvalds 
24187c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
2419cf222b37SAlasdair G Kergon 
24202e93ccc1SKiyoshi Ueda 	/*
24212e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
24222e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
24232e93ccc1SKiyoshi Ueda 	 */
24242e93ccc1SKiyoshi Ueda 	if (noflush)
24252e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
24262e93ccc1SKiyoshi Ueda 
2427436d4108SAlasdair G Kergon 	/* This does not get reverted if there's an error later. */
24281da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
24291da177e4SLinus Torvalds 
24302e93ccc1SKiyoshi Ueda 	/*
24319f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
24329f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
24339f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
24349f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
24352e93ccc1SKiyoshi Ueda 	 */
243632a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
24372ca3310eSAlasdair G Kergon 		r = lock_fs(md);
24382ca3310eSAlasdair G Kergon 		if (r)
24392ca3310eSAlasdair G Kergon 			goto out;
2440aa8d7c2fSAlasdair G Kergon 	}
24411da177e4SLinus Torvalds 
24421da177e4SLinus Torvalds 	/*
24433b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
24443b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
24453b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
24463b00b203SMikulas Patocka 	 * dm_wq_work.
24473b00b203SMikulas Patocka 	 *
24483b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
24493b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
24506a8736d1STejun Heo 	 * __split_and_process_bio from dm_request and quiesce the thread
24516a8736d1STejun Heo 	 * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
24526a8736d1STejun Heo 	 * flush_workqueue(md->wq).
24531da177e4SLinus Torvalds 	 */
24542ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
24551eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
24562ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
24571da177e4SLinus Torvalds 
2458d0bcb878SKiyoshi Ueda 	/*
245929e4013dSTejun Heo 	 * Stop md->queue before flushing md->wq in case request-based
246029e4013dSTejun Heo 	 * dm defers requests to md->wq from md->queue.
2461d0bcb878SKiyoshi Ueda 	 */
2462cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
24639f518b27SKiyoshi Ueda 		stop_queue(md->queue);
2464cec47e3dSKiyoshi Ueda 
2465d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2466d0bcb878SKiyoshi Ueda 
24671da177e4SLinus Torvalds 	/*
24683b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
24693b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
24703b00b203SMikulas Patocka 	 * to finish.
24711da177e4SLinus Torvalds 	 */
2472401600dfSMikulas Patocka 	r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
24731da177e4SLinus Torvalds 
24742ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
24756d6f10dfSMilan Broz 	if (noflush)
2476022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
247794d6351eSMilan Broz 	up_write(&md->io_lock);
24782e93ccc1SKiyoshi Ueda 
24791da177e4SLinus Torvalds 	/* were we interrupted ? */
248046125c1cSMilan Broz 	if (r < 0) {
24819a1fb464SMikulas Patocka 		dm_queue_flush(md);
248273d410c0SMilan Broz 
2483cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
24849f518b27SKiyoshi Ueda 			start_queue(md->queue);
2485cec47e3dSKiyoshi Ueda 
24862ca3310eSAlasdair G Kergon 		unlock_fs(md);
24872e93ccc1SKiyoshi Ueda 		goto out; /* pushback list is already flushed, so skip flush */
24882ca3310eSAlasdair G Kergon 	}
24892ca3310eSAlasdair G Kergon 
24903b00b203SMikulas Patocka 	/*
24913b00b203SMikulas Patocka 	 * If dm_wait_for_completion returned 0, the device is completely
24923b00b203SMikulas Patocka 	 * quiescent now. There is no request-processing activity. All new
24933b00b203SMikulas Patocka 	 * requests are being added to md->deferred list.
24943b00b203SMikulas Patocka 	 */
24953b00b203SMikulas Patocka 
24961da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
24971da177e4SLinus Torvalds 
24984d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
24994d4471cbSKiyoshi Ueda 
25002ca3310eSAlasdair G Kergon out:
25011da177e4SLinus Torvalds 	dm_table_put(map);
2502d287483dSAlasdair G Kergon 
2503d287483dSAlasdair G Kergon out_unlock:
2504e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2505cf222b37SAlasdair G Kergon 	return r;
25061da177e4SLinus Torvalds }
25071da177e4SLinus Torvalds 
25081da177e4SLinus Torvalds int dm_resume(struct mapped_device *md)
25091da177e4SLinus Torvalds {
2510cf222b37SAlasdair G Kergon 	int r = -EINVAL;
2511cf222b37SAlasdair G Kergon 	struct dm_table *map = NULL;
25121da177e4SLinus Torvalds 
2513e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
25144f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
2515cf222b37SAlasdair G Kergon 		goto out;
2516cf222b37SAlasdair G Kergon 
25177c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
25182ca3310eSAlasdair G Kergon 	if (!map || !dm_table_get_size(map))
2519cf222b37SAlasdair G Kergon 		goto out;
25201da177e4SLinus Torvalds 
25218757b776SMilan Broz 	r = dm_table_resume_targets(map);
25228757b776SMilan Broz 	if (r)
25238757b776SMilan Broz 		goto out;
25242ca3310eSAlasdair G Kergon 
25259a1fb464SMikulas Patocka 	dm_queue_flush(md);
25262ca3310eSAlasdair G Kergon 
2527cec47e3dSKiyoshi Ueda 	/*
2528cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2529cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2530cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2531cec47e3dSKiyoshi Ueda 	 */
2532cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2533cec47e3dSKiyoshi Ueda 		start_queue(md->queue);
2534cec47e3dSKiyoshi Ueda 
25352ca3310eSAlasdair G Kergon 	unlock_fs(md);
25362ca3310eSAlasdair G Kergon 
25372ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
25382ca3310eSAlasdair G Kergon 
25391da177e4SLinus Torvalds 	dm_table_unplug_all(map);
2540cf222b37SAlasdair G Kergon 	r = 0;
2541cf222b37SAlasdair G Kergon out:
2542cf222b37SAlasdair G Kergon 	dm_table_put(map);
2543e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
25442ca3310eSAlasdair G Kergon 
2545cf222b37SAlasdair G Kergon 	return r;
25461da177e4SLinus Torvalds }
25471da177e4SLinus Torvalds 
25481da177e4SLinus Torvalds /*-----------------------------------------------------------------
25491da177e4SLinus Torvalds  * Event notification.
25501da177e4SLinus Torvalds  *---------------------------------------------------------------*/
25513abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
255260935eb2SMilan Broz 		       unsigned cookie)
255369267a30SAlasdair G Kergon {
255460935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
255560935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
255660935eb2SMilan Broz 
255760935eb2SMilan Broz 	if (!cookie)
25583abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
255960935eb2SMilan Broz 	else {
256060935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
256160935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
25623abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
25633abf85b5SPeter Rajnoha 					  action, envp);
256460935eb2SMilan Broz 	}
256569267a30SAlasdair G Kergon }
256669267a30SAlasdair G Kergon 
25677a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
25687a8c3d3bSMike Anderson {
25697a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
25707a8c3d3bSMike Anderson }
25717a8c3d3bSMike Anderson 
25721da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
25731da177e4SLinus Torvalds {
25741da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
25751da177e4SLinus Torvalds }
25761da177e4SLinus Torvalds 
25771da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
25781da177e4SLinus Torvalds {
25791da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
25801da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
25811da177e4SLinus Torvalds }
25821da177e4SLinus Torvalds 
25837a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
25847a8c3d3bSMike Anderson {
25857a8c3d3bSMike Anderson 	unsigned long flags;
25867a8c3d3bSMike Anderson 
25877a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
25887a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
25897a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
25907a8c3d3bSMike Anderson }
25917a8c3d3bSMike Anderson 
25921da177e4SLinus Torvalds /*
25931da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
25941da177e4SLinus Torvalds  * count on 'md'.
25951da177e4SLinus Torvalds  */
25961da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
25971da177e4SLinus Torvalds {
25981da177e4SLinus Torvalds 	return md->disk;
25991da177e4SLinus Torvalds }
26001da177e4SLinus Torvalds 
2601784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2602784aae73SMilan Broz {
2603784aae73SMilan Broz 	return &md->kobj;
2604784aae73SMilan Broz }
2605784aae73SMilan Broz 
2606784aae73SMilan Broz /*
2607784aae73SMilan Broz  * struct mapped_device should not be exported outside of dm.c
2608784aae73SMilan Broz  * so use this check to verify that kobj is part of md structure
2609784aae73SMilan Broz  */
2610784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2611784aae73SMilan Broz {
2612784aae73SMilan Broz 	struct mapped_device *md;
2613784aae73SMilan Broz 
2614784aae73SMilan Broz 	md = container_of(kobj, struct mapped_device, kobj);
2615784aae73SMilan Broz 	if (&md->kobj != kobj)
2616784aae73SMilan Broz 		return NULL;
2617784aae73SMilan Broz 
26184d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
2619432a212cSMike Anderson 	    dm_deleting_md(md))
26204d89b7b4SMilan Broz 		return NULL;
26214d89b7b4SMilan Broz 
2622784aae73SMilan Broz 	dm_get(md);
2623784aae73SMilan Broz 	return md;
2624784aae73SMilan Broz }
2625784aae73SMilan Broz 
26264f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
26271da177e4SLinus Torvalds {
26281da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
26291da177e4SLinus Torvalds }
26301da177e4SLinus Torvalds 
263164dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
263264dbce58SKiyoshi Ueda {
2633ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
263464dbce58SKiyoshi Ueda }
263564dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
263664dbce58SKiyoshi Ueda 
26372e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
26382e93ccc1SKiyoshi Ueda {
2639ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
26402e93ccc1SKiyoshi Ueda }
26412e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
26422e93ccc1SKiyoshi Ueda 
2643e6ee8c0bSKiyoshi Ueda struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2644e6ee8c0bSKiyoshi Ueda {
2645e6ee8c0bSKiyoshi Ueda 	struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2646e6ee8c0bSKiyoshi Ueda 
2647e6ee8c0bSKiyoshi Ueda 	if (!pools)
2648e6ee8c0bSKiyoshi Ueda 		return NULL;
2649e6ee8c0bSKiyoshi Ueda 
2650e6ee8c0bSKiyoshi Ueda 	pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2651e6ee8c0bSKiyoshi Ueda 			 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2652e6ee8c0bSKiyoshi Ueda 			 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2653e6ee8c0bSKiyoshi Ueda 	if (!pools->io_pool)
2654e6ee8c0bSKiyoshi Ueda 		goto free_pools_and_out;
2655e6ee8c0bSKiyoshi Ueda 
2656e6ee8c0bSKiyoshi Ueda 	pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2657e6ee8c0bSKiyoshi Ueda 			  mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2658e6ee8c0bSKiyoshi Ueda 			  mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2659e6ee8c0bSKiyoshi Ueda 	if (!pools->tio_pool)
2660e6ee8c0bSKiyoshi Ueda 		goto free_io_pool_and_out;
2661e6ee8c0bSKiyoshi Ueda 
2662e6ee8c0bSKiyoshi Ueda 	pools->bs = (type == DM_TYPE_BIO_BASED) ?
2663e6ee8c0bSKiyoshi Ueda 		    bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2664e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
2665e6ee8c0bSKiyoshi Ueda 		goto free_tio_pool_and_out;
2666e6ee8c0bSKiyoshi Ueda 
2667e6ee8c0bSKiyoshi Ueda 	return pools;
2668e6ee8c0bSKiyoshi Ueda 
2669e6ee8c0bSKiyoshi Ueda free_tio_pool_and_out:
2670e6ee8c0bSKiyoshi Ueda 	mempool_destroy(pools->tio_pool);
2671e6ee8c0bSKiyoshi Ueda 
2672e6ee8c0bSKiyoshi Ueda free_io_pool_and_out:
2673e6ee8c0bSKiyoshi Ueda 	mempool_destroy(pools->io_pool);
2674e6ee8c0bSKiyoshi Ueda 
2675e6ee8c0bSKiyoshi Ueda free_pools_and_out:
2676e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2677e6ee8c0bSKiyoshi Ueda 
2678e6ee8c0bSKiyoshi Ueda 	return NULL;
2679e6ee8c0bSKiyoshi Ueda }
2680e6ee8c0bSKiyoshi Ueda 
2681e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
2682e6ee8c0bSKiyoshi Ueda {
2683e6ee8c0bSKiyoshi Ueda 	if (!pools)
2684e6ee8c0bSKiyoshi Ueda 		return;
2685e6ee8c0bSKiyoshi Ueda 
2686e6ee8c0bSKiyoshi Ueda 	if (pools->io_pool)
2687e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->io_pool);
2688e6ee8c0bSKiyoshi Ueda 
2689e6ee8c0bSKiyoshi Ueda 	if (pools->tio_pool)
2690e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->tio_pool);
2691e6ee8c0bSKiyoshi Ueda 
2692e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
2693e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
2694e6ee8c0bSKiyoshi Ueda 
2695e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2696e6ee8c0bSKiyoshi Ueda }
2697e6ee8c0bSKiyoshi Ueda 
269883d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
26991da177e4SLinus Torvalds 	.open = dm_blk_open,
27001da177e4SLinus Torvalds 	.release = dm_blk_close,
2701aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
27023ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
27031da177e4SLinus Torvalds 	.owner = THIS_MODULE
27041da177e4SLinus Torvalds };
27051da177e4SLinus Torvalds 
27061da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo);
27071da177e4SLinus Torvalds 
27081da177e4SLinus Torvalds /*
27091da177e4SLinus Torvalds  * module hooks
27101da177e4SLinus Torvalds  */
27111da177e4SLinus Torvalds module_init(dm_init);
27121da177e4SLinus Torvalds module_exit(dm_exit);
27131da177e4SLinus Torvalds 
27141da177e4SLinus Torvalds module_param(major, uint, 0);
27151da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
27161da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
27171da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
27181da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2719