xref: /openbmc/linux/drivers/md/dm.c (revision a79245b3e5669dc203fec63644d988c451fe55d5)
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>
186e9624b8SArnd Bergmann #include <linux/smp_lock.h>
191da177e4SLinus Torvalds #include <linux/mempool.h>
201da177e4SLinus Torvalds #include <linux/slab.h>
211da177e4SLinus Torvalds #include <linux/idr.h>
223ac51e74SDarrick J. Wong #include <linux/hdreg.h>
233f77316dSKiyoshi Ueda #include <linux/delay.h>
2455782138SLi Zefan 
2555782138SLi Zefan #include <trace/events/block.h>
261da177e4SLinus Torvalds 
2772d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
2872d94861SAlasdair G Kergon 
2960935eb2SMilan Broz /*
3060935eb2SMilan Broz  * Cookies are numeric values sent with CHANGE and REMOVE
3160935eb2SMilan Broz  * uevents while resuming, removing or renaming the device.
3260935eb2SMilan Broz  */
3360935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
3460935eb2SMilan Broz #define DM_COOKIE_LENGTH 24
3560935eb2SMilan Broz 
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
1131eb787ecSAlasdair G Kergon #define DMF_QUEUE_IO_TO_THREAD 6
1141da177e4SLinus Torvalds 
115304f3f6aSMilan Broz /*
116304f3f6aSMilan Broz  * Work processed by per-device workqueue.
117304f3f6aSMilan Broz  */
1181da177e4SLinus Torvalds struct mapped_device {
1192ca3310eSAlasdair G Kergon 	struct rw_semaphore io_lock;
120e61290a4SDaniel Walker 	struct mutex suspend_lock;
1211da177e4SLinus Torvalds 	rwlock_t map_lock;
1221da177e4SLinus Torvalds 	atomic_t holders;
1235c6bd75dSAlasdair G Kergon 	atomic_t open_count;
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	unsigned long flags;
1261da177e4SLinus Torvalds 
127165125e1SJens Axboe 	struct request_queue *queue;
128a5664dadSMike Snitzer 	unsigned type;
1294a0b4ddfSMike Snitzer 	/* Protect queue and type against concurrent access. */
130a5664dadSMike Snitzer 	struct mutex type_lock;
131a5664dadSMike Snitzer 
1321da177e4SLinus Torvalds 	struct gendisk *disk;
1337e51f257SMike Anderson 	char name[16];
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	void *interface_ptr;
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	/*
1381da177e4SLinus Torvalds 	 * A list of ios that arrived while we were suspended.
1391da177e4SLinus Torvalds 	 */
140316d315bSNikanth Karthikesan 	atomic_t pending[2];
1411da177e4SLinus Torvalds 	wait_queue_head_t wait;
14253d5914fSMikulas Patocka 	struct work_struct work;
1431da177e4SLinus Torvalds 	struct bio_list deferred;
144022c2611SMikulas Patocka 	spinlock_t deferred_lock;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	/*
147af7e466aSMikulas Patocka 	 * An error from the barrier request currently being processed.
148af7e466aSMikulas Patocka 	 */
149af7e466aSMikulas Patocka 	int barrier_error;
150af7e466aSMikulas Patocka 
151af7e466aSMikulas Patocka 	/*
152d0bcb878SKiyoshi Ueda 	 * Protect barrier_error from concurrent endio processing
153d0bcb878SKiyoshi Ueda 	 * in request-based dm.
154d0bcb878SKiyoshi Ueda 	 */
155d0bcb878SKiyoshi Ueda 	spinlock_t barrier_error_lock;
156d0bcb878SKiyoshi Ueda 
157d0bcb878SKiyoshi Ueda 	/*
158304f3f6aSMilan Broz 	 * Processing queue (flush/barriers)
159304f3f6aSMilan Broz 	 */
160304f3f6aSMilan Broz 	struct workqueue_struct *wq;
161d0bcb878SKiyoshi Ueda 	struct work_struct barrier_work;
162d0bcb878SKiyoshi Ueda 
163d0bcb878SKiyoshi Ueda 	/* A pointer to the currently processing pre/post flush request */
164d0bcb878SKiyoshi Ueda 	struct request *flush_request;
165304f3f6aSMilan Broz 
166304f3f6aSMilan Broz 	/*
1671da177e4SLinus Torvalds 	 * The current mapping.
1681da177e4SLinus Torvalds 	 */
1691da177e4SLinus Torvalds 	struct dm_table *map;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	/*
1721da177e4SLinus Torvalds 	 * io objects are allocated from here.
1731da177e4SLinus Torvalds 	 */
1741da177e4SLinus Torvalds 	mempool_t *io_pool;
1751da177e4SLinus Torvalds 	mempool_t *tio_pool;
1761da177e4SLinus Torvalds 
1779faf400fSStefan Bader 	struct bio_set *bs;
1789faf400fSStefan Bader 
1791da177e4SLinus Torvalds 	/*
1801da177e4SLinus Torvalds 	 * Event handling.
1811da177e4SLinus Torvalds 	 */
1821da177e4SLinus Torvalds 	atomic_t event_nr;
1831da177e4SLinus Torvalds 	wait_queue_head_t eventq;
1847a8c3d3bSMike Anderson 	atomic_t uevent_seq;
1857a8c3d3bSMike Anderson 	struct list_head uevent_list;
1867a8c3d3bSMike Anderson 	spinlock_t uevent_lock; /* Protect access to uevent_list */
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	/*
1891da177e4SLinus Torvalds 	 * freeze/thaw support require holding onto a super block
1901da177e4SLinus Torvalds 	 */
1911da177e4SLinus Torvalds 	struct super_block *frozen_sb;
192db8fef4fSMikulas Patocka 	struct block_device *bdev;
1933ac51e74SDarrick J. Wong 
1943ac51e74SDarrick J. Wong 	/* forced geometry settings */
1953ac51e74SDarrick J. Wong 	struct hd_geometry geometry;
196784aae73SMilan Broz 
197cec47e3dSKiyoshi Ueda 	/* For saving the address of __make_request for request based dm */
198cec47e3dSKiyoshi Ueda 	make_request_fn *saved_make_request_fn;
199cec47e3dSKiyoshi Ueda 
200784aae73SMilan Broz 	/* sysfs handle */
201784aae73SMilan Broz 	struct kobject kobj;
20252b1fd5aSMikulas Patocka 
20352b1fd5aSMikulas Patocka 	/* zero-length barrier that will be cloned and submitted to targets */
20452b1fd5aSMikulas Patocka 	struct bio barrier_bio;
2051da177e4SLinus Torvalds };
2061da177e4SLinus Torvalds 
207e6ee8c0bSKiyoshi Ueda /*
208e6ee8c0bSKiyoshi Ueda  * For mempools pre-allocation at the table loading time.
209e6ee8c0bSKiyoshi Ueda  */
210e6ee8c0bSKiyoshi Ueda struct dm_md_mempools {
211e6ee8c0bSKiyoshi Ueda 	mempool_t *io_pool;
212e6ee8c0bSKiyoshi Ueda 	mempool_t *tio_pool;
213e6ee8c0bSKiyoshi Ueda 	struct bio_set *bs;
214e6ee8c0bSKiyoshi Ueda };
215e6ee8c0bSKiyoshi Ueda 
2161da177e4SLinus Torvalds #define MIN_IOS 256
217e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
218e18b890bSChristoph Lameter static struct kmem_cache *_tio_cache;
2198fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
2208fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_bio_info_cache;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds static int __init local_init(void)
2231da177e4SLinus Torvalds {
22451157b4aSKiyoshi Ueda 	int r = -ENOMEM;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
227028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
2281da177e4SLinus Torvalds 	if (!_io_cache)
22951157b4aSKiyoshi Ueda 		return r;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	/* allocate a slab for the target ios */
232028867acSAlasdair G Kergon 	_tio_cache = KMEM_CACHE(dm_target_io, 0);
23351157b4aSKiyoshi Ueda 	if (!_tio_cache)
23451157b4aSKiyoshi Ueda 		goto out_free_io_cache;
2351da177e4SLinus Torvalds 
2368fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
2378fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
2388fbf26adSKiyoshi Ueda 		goto out_free_tio_cache;
2398fbf26adSKiyoshi Ueda 
2408fbf26adSKiyoshi Ueda 	_rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
2418fbf26adSKiyoshi Ueda 	if (!_rq_bio_info_cache)
2428fbf26adSKiyoshi Ueda 		goto out_free_rq_tio_cache;
2438fbf26adSKiyoshi Ueda 
24451e5b2bdSMike Anderson 	r = dm_uevent_init();
24551157b4aSKiyoshi Ueda 	if (r)
2468fbf26adSKiyoshi Ueda 		goto out_free_rq_bio_info_cache;
24751e5b2bdSMike Anderson 
2481da177e4SLinus Torvalds 	_major = major;
2491da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
25051157b4aSKiyoshi Ueda 	if (r < 0)
25151157b4aSKiyoshi Ueda 		goto out_uevent_exit;
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	if (!_major)
2541da177e4SLinus Torvalds 		_major = r;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	return 0;
25751157b4aSKiyoshi Ueda 
25851157b4aSKiyoshi Ueda out_uevent_exit:
25951157b4aSKiyoshi Ueda 	dm_uevent_exit();
2608fbf26adSKiyoshi Ueda out_free_rq_bio_info_cache:
2618fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_bio_info_cache);
2628fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
2638fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
26451157b4aSKiyoshi Ueda out_free_tio_cache:
26551157b4aSKiyoshi Ueda 	kmem_cache_destroy(_tio_cache);
26651157b4aSKiyoshi Ueda out_free_io_cache:
26751157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
26851157b4aSKiyoshi Ueda 
26951157b4aSKiyoshi Ueda 	return r;
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds static void local_exit(void)
2731da177e4SLinus Torvalds {
2748fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_bio_info_cache);
2758fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
2761da177e4SLinus Torvalds 	kmem_cache_destroy(_tio_cache);
2771da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
27800d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
27951e5b2bdSMike Anderson 	dm_uevent_exit();
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	_major = 0;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	DMINFO("cleaned up");
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
286b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
2871da177e4SLinus Torvalds 	local_init,
2881da177e4SLinus Torvalds 	dm_target_init,
2891da177e4SLinus Torvalds 	dm_linear_init,
2901da177e4SLinus Torvalds 	dm_stripe_init,
291952b3557SMikulas Patocka 	dm_io_init,
292945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2931da177e4SLinus Torvalds 	dm_interface_init,
2941da177e4SLinus Torvalds };
2951da177e4SLinus Torvalds 
296b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2971da177e4SLinus Torvalds 	local_exit,
2981da177e4SLinus Torvalds 	dm_target_exit,
2991da177e4SLinus Torvalds 	dm_linear_exit,
3001da177e4SLinus Torvalds 	dm_stripe_exit,
301952b3557SMikulas Patocka 	dm_io_exit,
302945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
3031da177e4SLinus Torvalds 	dm_interface_exit,
3041da177e4SLinus Torvalds };
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds static int __init dm_init(void)
3071da177e4SLinus Torvalds {
3081da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	int r, i;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
3131da177e4SLinus Torvalds 		r = _inits[i]();
3141da177e4SLinus Torvalds 		if (r)
3151da177e4SLinus Torvalds 			goto bad;
3161da177e4SLinus Torvalds 	}
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	return 0;
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds       bad:
3211da177e4SLinus Torvalds 	while (i--)
3221da177e4SLinus Torvalds 		_exits[i]();
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	return r;
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds static void __exit dm_exit(void)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	while (i--)
3321da177e4SLinus Torvalds 		_exits[i]();
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds /*
3361da177e4SLinus Torvalds  * Block device functions
3371da177e4SLinus Torvalds  */
338432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md)
339432a212cSMike Anderson {
340432a212cSMike Anderson 	return test_bit(DMF_DELETING, &md->flags);
341432a212cSMike Anderson }
342432a212cSMike Anderson 
343fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
3441da177e4SLinus Torvalds {
3451da177e4SLinus Torvalds 	struct mapped_device *md;
3461da177e4SLinus Torvalds 
3476e9624b8SArnd Bergmann 	lock_kernel();
348fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
349fba9f90eSJeff Mahoney 
350fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
351fba9f90eSJeff Mahoney 	if (!md)
352fba9f90eSJeff Mahoney 		goto out;
353fba9f90eSJeff Mahoney 
3545c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
355432a212cSMike Anderson 	    dm_deleting_md(md)) {
356fba9f90eSJeff Mahoney 		md = NULL;
357fba9f90eSJeff Mahoney 		goto out;
358fba9f90eSJeff Mahoney 	}
359fba9f90eSJeff Mahoney 
3601da177e4SLinus Torvalds 	dm_get(md);
3615c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
362fba9f90eSJeff Mahoney 
363fba9f90eSJeff Mahoney out:
364fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
3656e9624b8SArnd Bergmann 	unlock_kernel();
366fba9f90eSJeff Mahoney 
367fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
370fe5f9f2cSAl Viro static int dm_blk_close(struct gendisk *disk, fmode_t mode)
3711da177e4SLinus Torvalds {
372fe5f9f2cSAl Viro 	struct mapped_device *md = disk->private_data;
3736e9624b8SArnd Bergmann 
3746e9624b8SArnd Bergmann 	lock_kernel();
3755c6bd75dSAlasdair G Kergon 	atomic_dec(&md->open_count);
3761da177e4SLinus Torvalds 	dm_put(md);
3776e9624b8SArnd Bergmann 	unlock_kernel();
3786e9624b8SArnd Bergmann 
3791da177e4SLinus Torvalds 	return 0;
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds 
3825c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3835c6bd75dSAlasdair G Kergon {
3845c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3855c6bd75dSAlasdair G Kergon }
3865c6bd75dSAlasdair G Kergon 
3875c6bd75dSAlasdair G Kergon /*
3885c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3895c6bd75dSAlasdair G Kergon  */
3905c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md)
3915c6bd75dSAlasdair G Kergon {
3925c6bd75dSAlasdair G Kergon 	int r = 0;
3935c6bd75dSAlasdair G Kergon 
3945c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
3955c6bd75dSAlasdair G Kergon 
3965c6bd75dSAlasdair G Kergon 	if (dm_open_count(md))
3975c6bd75dSAlasdair G Kergon 		r = -EBUSY;
3985c6bd75dSAlasdair G Kergon 	else
3995c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
4005c6bd75dSAlasdair G Kergon 
4015c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
4025c6bd75dSAlasdair G Kergon 
4035c6bd75dSAlasdair G Kergon 	return r;
4045c6bd75dSAlasdair G Kergon }
4055c6bd75dSAlasdair G Kergon 
4063ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4073ac51e74SDarrick J. Wong {
4083ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
4093ac51e74SDarrick J. Wong 
4103ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
4113ac51e74SDarrick J. Wong }
4123ac51e74SDarrick J. Wong 
413fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
414aa129a22SMilan Broz 			unsigned int cmd, unsigned long arg)
415aa129a22SMilan Broz {
416fe5f9f2cSAl Viro 	struct mapped_device *md = bdev->bd_disk->private_data;
4177c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
418aa129a22SMilan Broz 	struct dm_target *tgt;
419aa129a22SMilan Broz 	int r = -ENOTTY;
420aa129a22SMilan Broz 
421aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
422aa129a22SMilan Broz 		goto out;
423aa129a22SMilan Broz 
424aa129a22SMilan Broz 	/* We only support devices that have a single target */
425aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
426aa129a22SMilan Broz 		goto out;
427aa129a22SMilan Broz 
428aa129a22SMilan Broz 	tgt = dm_table_get_target(map, 0);
429aa129a22SMilan Broz 
4304f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
431aa129a22SMilan Broz 		r = -EAGAIN;
432aa129a22SMilan Broz 		goto out;
433aa129a22SMilan Broz 	}
434aa129a22SMilan Broz 
435aa129a22SMilan Broz 	if (tgt->type->ioctl)
436647b3d00SAl Viro 		r = tgt->type->ioctl(tgt, cmd, arg);
437aa129a22SMilan Broz 
438aa129a22SMilan Broz out:
439aa129a22SMilan Broz 	dm_table_put(map);
440aa129a22SMilan Broz 
441aa129a22SMilan Broz 	return r;
442aa129a22SMilan Broz }
443aa129a22SMilan Broz 
444028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
4451da177e4SLinus Torvalds {
4461da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds 
449028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
4501da177e4SLinus Torvalds {
4511da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
4521da177e4SLinus Torvalds }
4531da177e4SLinus Torvalds 
454028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
4551da177e4SLinus Torvalds {
4561da177e4SLinus Torvalds 	mempool_free(tio, md->tio_pool);
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds 
45908885643SKiyoshi Ueda static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
46008885643SKiyoshi Ueda 					    gfp_t gfp_mask)
461cec47e3dSKiyoshi Ueda {
46208885643SKiyoshi Ueda 	return mempool_alloc(md->tio_pool, gfp_mask);
463cec47e3dSKiyoshi Ueda }
464cec47e3dSKiyoshi Ueda 
465cec47e3dSKiyoshi Ueda static void free_rq_tio(struct dm_rq_target_io *tio)
466cec47e3dSKiyoshi Ueda {
467cec47e3dSKiyoshi Ueda 	mempool_free(tio, tio->md->tio_pool);
468cec47e3dSKiyoshi Ueda }
469cec47e3dSKiyoshi Ueda 
470cec47e3dSKiyoshi Ueda static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
471cec47e3dSKiyoshi Ueda {
472cec47e3dSKiyoshi Ueda 	return mempool_alloc(md->io_pool, GFP_ATOMIC);
473cec47e3dSKiyoshi Ueda }
474cec47e3dSKiyoshi Ueda 
475cec47e3dSKiyoshi Ueda static void free_bio_info(struct dm_rq_clone_bio_info *info)
476cec47e3dSKiyoshi Ueda {
477cec47e3dSKiyoshi Ueda 	mempool_free(info, info->tio->md->io_pool);
478cec47e3dSKiyoshi Ueda }
479cec47e3dSKiyoshi Ueda 
48090abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md)
48190abb8c4SKiyoshi Ueda {
48290abb8c4SKiyoshi Ueda 	return atomic_read(&md->pending[READ]) +
48390abb8c4SKiyoshi Ueda 	       atomic_read(&md->pending[WRITE]);
48490abb8c4SKiyoshi Ueda }
48590abb8c4SKiyoshi Ueda 
4863eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
4873eaf840eSJun'ichi "Nick" Nomura {
4883eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
489c9959059STejun Heo 	int cpu;
490316d315bSNikanth Karthikesan 	int rw = bio_data_dir(io->bio);
4913eaf840eSJun'ichi "Nick" Nomura 
4923eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
4933eaf840eSJun'ichi "Nick" Nomura 
494074a7acaSTejun Heo 	cpu = part_stat_lock();
495074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
496074a7acaSTejun Heo 	part_stat_unlock();
497316d315bSNikanth Karthikesan 	dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]);
4983eaf840eSJun'ichi "Nick" Nomura }
4993eaf840eSJun'ichi "Nick" Nomura 
500d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
5013eaf840eSJun'ichi "Nick" Nomura {
5023eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
5033eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
5043eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
505c9959059STejun Heo 	int pending, cpu;
5063eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
5073eaf840eSJun'ichi "Nick" Nomura 
508074a7acaSTejun Heo 	cpu = part_stat_lock();
509074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
510074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
511074a7acaSTejun Heo 	part_stat_unlock();
5123eaf840eSJun'ichi "Nick" Nomura 
513af7e466aSMikulas Patocka 	/*
514af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
515af7e466aSMikulas Patocka 	 * a barrier.
516af7e466aSMikulas Patocka 	 */
517316d315bSNikanth Karthikesan 	dm_disk(md)->part0.in_flight[rw] = pending =
518316d315bSNikanth Karthikesan 		atomic_dec_return(&md->pending[rw]);
519316d315bSNikanth Karthikesan 	pending += atomic_read(&md->pending[rw^0x1]);
5203eaf840eSJun'ichi "Nick" Nomura 
521d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
522d221d2e7SMikulas Patocka 	if (!pending)
523d221d2e7SMikulas Patocka 		wake_up(&md->wait);
5243eaf840eSJun'ichi "Nick" Nomura }
5253eaf840eSJun'ichi "Nick" Nomura 
5261da177e4SLinus Torvalds /*
5271da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
5281da177e4SLinus Torvalds  */
52992c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
5301da177e4SLinus Torvalds {
5312ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
5321da177e4SLinus Torvalds 
533022c2611SMikulas Patocka 	spin_lock_irq(&md->deferred_lock);
5341da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
535022c2611SMikulas Patocka 	spin_unlock_irq(&md->deferred_lock);
5361da177e4SLinus Torvalds 
53792c63902SMikulas Patocka 	if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
53892c63902SMikulas Patocka 		queue_work(md->wq, &md->work);
53992c63902SMikulas Patocka 
5402ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
5411da177e4SLinus Torvalds }
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds /*
5441da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
5451da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
5461da177e4SLinus Torvalds  * dm_table_put() when finished.
5471da177e4SLinus Torvalds  */
5487c666411SAlasdair G Kergon struct dm_table *dm_get_live_table(struct mapped_device *md)
5491da177e4SLinus Torvalds {
5501da177e4SLinus Torvalds 	struct dm_table *t;
551523d9297SKiyoshi Ueda 	unsigned long flags;
5521da177e4SLinus Torvalds 
553523d9297SKiyoshi Ueda 	read_lock_irqsave(&md->map_lock, flags);
5541da177e4SLinus Torvalds 	t = md->map;
5551da177e4SLinus Torvalds 	if (t)
5561da177e4SLinus Torvalds 		dm_table_get(t);
557523d9297SKiyoshi Ueda 	read_unlock_irqrestore(&md->map_lock, flags);
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	return t;
5601da177e4SLinus Torvalds }
5611da177e4SLinus Torvalds 
5623ac51e74SDarrick J. Wong /*
5633ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
5643ac51e74SDarrick J. Wong  */
5653ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
5663ac51e74SDarrick J. Wong {
5673ac51e74SDarrick J. Wong 	*geo = md->geometry;
5683ac51e74SDarrick J. Wong 
5693ac51e74SDarrick J. Wong 	return 0;
5703ac51e74SDarrick J. Wong }
5713ac51e74SDarrick J. Wong 
5723ac51e74SDarrick J. Wong /*
5733ac51e74SDarrick J. Wong  * Set the geometry of a device.
5743ac51e74SDarrick J. Wong  */
5753ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
5763ac51e74SDarrick J. Wong {
5773ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
5783ac51e74SDarrick J. Wong 
5793ac51e74SDarrick J. Wong 	if (geo->start > sz) {
5803ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
5813ac51e74SDarrick J. Wong 		return -EINVAL;
5823ac51e74SDarrick J. Wong 	}
5833ac51e74SDarrick J. Wong 
5843ac51e74SDarrick J. Wong 	md->geometry = *geo;
5853ac51e74SDarrick J. Wong 
5863ac51e74SDarrick J. Wong 	return 0;
5873ac51e74SDarrick J. Wong }
5883ac51e74SDarrick J. Wong 
5891da177e4SLinus Torvalds /*-----------------------------------------------------------------
5901da177e4SLinus Torvalds  * CRUD START:
5911da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
5921da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
5931da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
5941da177e4SLinus Torvalds  *   interests of getting something for people to use I give
5951da177e4SLinus Torvalds  *   you this clearly demarcated crap.
5961da177e4SLinus Torvalds  *---------------------------------------------------------------*/
5971da177e4SLinus Torvalds 
5982e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
5992e93ccc1SKiyoshi Ueda {
6002e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
6012e93ccc1SKiyoshi Ueda }
6022e93ccc1SKiyoshi Ueda 
6031da177e4SLinus Torvalds /*
6041da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
6051da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
6061da177e4SLinus Torvalds  */
607858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
6081da177e4SLinus Torvalds {
6092e93ccc1SKiyoshi Ueda 	unsigned long flags;
610b35f8caaSMilan Broz 	int io_error;
611b35f8caaSMilan Broz 	struct bio *bio;
612b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
6132e93ccc1SKiyoshi Ueda 
6142e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
615f88fb981SKiyoshi Ueda 	if (unlikely(error)) {
616f88fb981SKiyoshi Ueda 		spin_lock_irqsave(&io->endio_lock, flags);
617f88fb981SKiyoshi Ueda 		if (!(io->error > 0 && __noflush_suspending(md)))
6181da177e4SLinus Torvalds 			io->error = error;
619f88fb981SKiyoshi Ueda 		spin_unlock_irqrestore(&io->endio_lock, flags);
620f88fb981SKiyoshi Ueda 	}
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
6232e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
6242e93ccc1SKiyoshi Ueda 			/*
6252e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
6262e93ccc1SKiyoshi Ueda 			 */
627022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
6282761e95fSMikulas Patocka 			if (__noflush_suspending(md)) {
6297b6d91daSChristoph Hellwig 				if (!(io->bio->bi_rw & REQ_HARDBARRIER))
6302761e95fSMikulas Patocka 					bio_list_add_head(&md->deferred,
6312761e95fSMikulas Patocka 							  io->bio);
6322761e95fSMikulas Patocka 			} else
6332e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
6342e93ccc1SKiyoshi Ueda 				io->error = -EIO;
635022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
6362e93ccc1SKiyoshi Ueda 		}
6372e93ccc1SKiyoshi Ueda 
638b35f8caaSMilan Broz 		io_error = io->error;
639b35f8caaSMilan Broz 		bio = io->bio;
6402056a782SJens Axboe 
6417b6d91daSChristoph Hellwig 		if (bio->bi_rw & REQ_HARDBARRIER) {
642af7e466aSMikulas Patocka 			/*
643af7e466aSMikulas Patocka 			 * There can be just one barrier request so we use
644af7e466aSMikulas Patocka 			 * a per-device variable for error reporting.
645af7e466aSMikulas Patocka 			 * Note that you can't touch the bio after end_io_acct
646708e9295SMikulas Patocka 			 *
647708e9295SMikulas Patocka 			 * We ignore -EOPNOTSUPP for empty flush reported by
648708e9295SMikulas Patocka 			 * underlying devices. We assume that if the device
649708e9295SMikulas Patocka 			 * doesn't support empty barriers, it doesn't need
650708e9295SMikulas Patocka 			 * cache flushing commands.
651af7e466aSMikulas Patocka 			 */
652708e9295SMikulas Patocka 			if (!md->barrier_error &&
653708e9295SMikulas Patocka 			    !(bio_empty_barrier(bio) && io_error == -EOPNOTSUPP))
654af7e466aSMikulas Patocka 				md->barrier_error = io_error;
655af7e466aSMikulas Patocka 			end_io_acct(io);
656a97f925aSMikulas Patocka 			free_io(md, io);
657af7e466aSMikulas Patocka 		} else {
658af7e466aSMikulas Patocka 			end_io_acct(io);
659a97f925aSMikulas Patocka 			free_io(md, io);
660b35f8caaSMilan Broz 
661b35f8caaSMilan Broz 			if (io_error != DM_ENDIO_REQUEUE) {
662b35f8caaSMilan Broz 				trace_block_bio_complete(md->queue, bio);
663b35f8caaSMilan Broz 
664b35f8caaSMilan Broz 				bio_endio(bio, io_error);
6652e93ccc1SKiyoshi Ueda 			}
6661da177e4SLinus Torvalds 		}
667af7e466aSMikulas Patocka 	}
6681da177e4SLinus Torvalds }
6691da177e4SLinus Torvalds 
6706712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error)
6711da177e4SLinus Torvalds {
6721da177e4SLinus Torvalds 	int r = 0;
673028867acSAlasdair G Kergon 	struct dm_target_io *tio = bio->bi_private;
674b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
6759faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
6761da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	if (!bio_flagged(bio, BIO_UPTODATE) && !error)
6791da177e4SLinus Torvalds 		error = -EIO;
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 	if (endio) {
6821da177e4SLinus Torvalds 		r = endio(tio->ti, bio, error, &tio->info);
6832e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
6842e93ccc1SKiyoshi Ueda 			/*
6852e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
6862e93ccc1SKiyoshi Ueda 			 * in dec_pending().
6872e93ccc1SKiyoshi Ueda 			 */
6881da177e4SLinus Torvalds 			error = r;
68945cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
69045cbcd79SKiyoshi Ueda 			/* The target will handle the io */
6916712ecf8SNeilBrown 			return;
69245cbcd79SKiyoshi Ueda 		else if (r) {
69345cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
69445cbcd79SKiyoshi Ueda 			BUG();
69545cbcd79SKiyoshi Ueda 		}
6961da177e4SLinus Torvalds 	}
6971da177e4SLinus Torvalds 
6989faf400fSStefan Bader 	/*
6999faf400fSStefan Bader 	 * Store md for cleanup instead of tio which is about to get freed.
7009faf400fSStefan Bader 	 */
7019faf400fSStefan Bader 	bio->bi_private = md->bs;
7029faf400fSStefan Bader 
7039faf400fSStefan Bader 	free_tio(md, tio);
704b35f8caaSMilan Broz 	bio_put(bio);
705b35f8caaSMilan Broz 	dec_pending(io, error);
7061da177e4SLinus Torvalds }
7071da177e4SLinus Torvalds 
708cec47e3dSKiyoshi Ueda /*
709cec47e3dSKiyoshi Ueda  * Partial completion handling for request-based dm
710cec47e3dSKiyoshi Ueda  */
711cec47e3dSKiyoshi Ueda static void end_clone_bio(struct bio *clone, int error)
712cec47e3dSKiyoshi Ueda {
713cec47e3dSKiyoshi Ueda 	struct dm_rq_clone_bio_info *info = clone->bi_private;
714cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = info->tio;
715cec47e3dSKiyoshi Ueda 	struct bio *bio = info->orig;
716cec47e3dSKiyoshi Ueda 	unsigned int nr_bytes = info->orig->bi_size;
717cec47e3dSKiyoshi Ueda 
718cec47e3dSKiyoshi Ueda 	bio_put(clone);
719cec47e3dSKiyoshi Ueda 
720cec47e3dSKiyoshi Ueda 	if (tio->error)
721cec47e3dSKiyoshi Ueda 		/*
722cec47e3dSKiyoshi Ueda 		 * An error has already been detected on the request.
723cec47e3dSKiyoshi Ueda 		 * Once error occurred, just let clone->end_io() handle
724cec47e3dSKiyoshi Ueda 		 * the remainder.
725cec47e3dSKiyoshi Ueda 		 */
726cec47e3dSKiyoshi Ueda 		return;
727cec47e3dSKiyoshi Ueda 	else if (error) {
728cec47e3dSKiyoshi Ueda 		/*
729cec47e3dSKiyoshi Ueda 		 * Don't notice the error to the upper layer yet.
730cec47e3dSKiyoshi Ueda 		 * The error handling decision is made by the target driver,
731cec47e3dSKiyoshi Ueda 		 * when the request is completed.
732cec47e3dSKiyoshi Ueda 		 */
733cec47e3dSKiyoshi Ueda 		tio->error = error;
734cec47e3dSKiyoshi Ueda 		return;
735cec47e3dSKiyoshi Ueda 	}
736cec47e3dSKiyoshi Ueda 
737cec47e3dSKiyoshi Ueda 	/*
738cec47e3dSKiyoshi Ueda 	 * I/O for the bio successfully completed.
739cec47e3dSKiyoshi Ueda 	 * Notice the data completion to the upper layer.
740cec47e3dSKiyoshi Ueda 	 */
741cec47e3dSKiyoshi Ueda 
742cec47e3dSKiyoshi Ueda 	/*
743cec47e3dSKiyoshi Ueda 	 * bios are processed from the head of the list.
744cec47e3dSKiyoshi Ueda 	 * So the completing bio should always be rq->bio.
745cec47e3dSKiyoshi Ueda 	 * If it's not, something wrong is happening.
746cec47e3dSKiyoshi Ueda 	 */
747cec47e3dSKiyoshi Ueda 	if (tio->orig->bio != bio)
748cec47e3dSKiyoshi Ueda 		DMERR("bio completion is going in the middle of the request");
749cec47e3dSKiyoshi Ueda 
750cec47e3dSKiyoshi Ueda 	/*
751cec47e3dSKiyoshi Ueda 	 * Update the original request.
752cec47e3dSKiyoshi Ueda 	 * Do not use blk_end_request() here, because it may complete
753cec47e3dSKiyoshi Ueda 	 * the original request before the clone, and break the ordering.
754cec47e3dSKiyoshi Ueda 	 */
755cec47e3dSKiyoshi Ueda 	blk_update_request(tio->orig, 0, nr_bytes);
756cec47e3dSKiyoshi Ueda }
757cec47e3dSKiyoshi Ueda 
758d0bcb878SKiyoshi Ueda static void store_barrier_error(struct mapped_device *md, int error)
759d0bcb878SKiyoshi Ueda {
760d0bcb878SKiyoshi Ueda 	unsigned long flags;
761d0bcb878SKiyoshi Ueda 
762d0bcb878SKiyoshi Ueda 	spin_lock_irqsave(&md->barrier_error_lock, flags);
763d0bcb878SKiyoshi Ueda 	/*
764d0bcb878SKiyoshi Ueda 	 * Basically, the first error is taken, but:
765d0bcb878SKiyoshi Ueda 	 *   -EOPNOTSUPP supersedes any I/O error.
766d0bcb878SKiyoshi Ueda 	 *   Requeue request supersedes any I/O error but -EOPNOTSUPP.
767d0bcb878SKiyoshi Ueda 	 */
768d0bcb878SKiyoshi Ueda 	if (!md->barrier_error || error == -EOPNOTSUPP ||
769d0bcb878SKiyoshi Ueda 	    (md->barrier_error != -EOPNOTSUPP &&
770d0bcb878SKiyoshi Ueda 	     error == DM_ENDIO_REQUEUE))
771d0bcb878SKiyoshi Ueda 		md->barrier_error = error;
772d0bcb878SKiyoshi Ueda 	spin_unlock_irqrestore(&md->barrier_error_lock, flags);
773d0bcb878SKiyoshi Ueda }
774d0bcb878SKiyoshi Ueda 
775cec47e3dSKiyoshi Ueda /*
776cec47e3dSKiyoshi Ueda  * Don't touch any member of the md after calling this function because
777cec47e3dSKiyoshi Ueda  * the md may be freed in dm_put() at the end of this function.
778cec47e3dSKiyoshi Ueda  * Or do dm_get() before calling this function and dm_put() later.
779cec47e3dSKiyoshi Ueda  */
780b4324feeSKiyoshi Ueda static void rq_completed(struct mapped_device *md, int rw, int run_queue)
781cec47e3dSKiyoshi Ueda {
782b4324feeSKiyoshi Ueda 	atomic_dec(&md->pending[rw]);
783cec47e3dSKiyoshi Ueda 
784cec47e3dSKiyoshi Ueda 	/* nudge anyone waiting on suspend queue */
785b4324feeSKiyoshi Ueda 	if (!md_in_flight(md))
786cec47e3dSKiyoshi Ueda 		wake_up(&md->wait);
787cec47e3dSKiyoshi Ueda 
788cec47e3dSKiyoshi Ueda 	if (run_queue)
789b4324feeSKiyoshi Ueda 		blk_run_queue(md->queue);
790cec47e3dSKiyoshi Ueda 
791cec47e3dSKiyoshi Ueda 	/*
792cec47e3dSKiyoshi Ueda 	 * dm_put() must be at the end of this function. See the comment above
793cec47e3dSKiyoshi Ueda 	 */
794cec47e3dSKiyoshi Ueda 	dm_put(md);
795cec47e3dSKiyoshi Ueda }
796cec47e3dSKiyoshi Ueda 
797a77e28c7SKiyoshi Ueda static void free_rq_clone(struct request *clone)
798a77e28c7SKiyoshi Ueda {
799a77e28c7SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
800a77e28c7SKiyoshi Ueda 
801a77e28c7SKiyoshi Ueda 	blk_rq_unprep_clone(clone);
802a77e28c7SKiyoshi Ueda 	free_rq_tio(tio);
803a77e28c7SKiyoshi Ueda }
804a77e28c7SKiyoshi Ueda 
805980691e5SKiyoshi Ueda /*
806980691e5SKiyoshi Ueda  * Complete the clone and the original request.
807980691e5SKiyoshi Ueda  * Must be called without queue lock.
808980691e5SKiyoshi Ueda  */
809980691e5SKiyoshi Ueda static void dm_end_request(struct request *clone, int error)
810980691e5SKiyoshi Ueda {
811980691e5SKiyoshi Ueda 	int rw = rq_data_dir(clone);
812d0bcb878SKiyoshi Ueda 	int run_queue = 1;
81333659ebbSChristoph Hellwig 	bool is_barrier = clone->cmd_flags & REQ_HARDBARRIER;
814980691e5SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
815980691e5SKiyoshi Ueda 	struct mapped_device *md = tio->md;
816980691e5SKiyoshi Ueda 	struct request *rq = tio->orig;
817980691e5SKiyoshi Ueda 
81833659ebbSChristoph Hellwig 	if (rq->cmd_type == REQ_TYPE_BLOCK_PC && !is_barrier) {
819980691e5SKiyoshi Ueda 		rq->errors = clone->errors;
820980691e5SKiyoshi Ueda 		rq->resid_len = clone->resid_len;
821980691e5SKiyoshi Ueda 
822980691e5SKiyoshi Ueda 		if (rq->sense)
823980691e5SKiyoshi Ueda 			/*
824980691e5SKiyoshi Ueda 			 * We are using the sense buffer of the original
825980691e5SKiyoshi Ueda 			 * request.
826980691e5SKiyoshi Ueda 			 * So setting the length of the sense data is enough.
827980691e5SKiyoshi Ueda 			 */
828980691e5SKiyoshi Ueda 			rq->sense_len = clone->sense_len;
829980691e5SKiyoshi Ueda 	}
830980691e5SKiyoshi Ueda 
831980691e5SKiyoshi Ueda 	free_rq_clone(clone);
832980691e5SKiyoshi Ueda 
833d0bcb878SKiyoshi Ueda 	if (unlikely(is_barrier)) {
834d0bcb878SKiyoshi Ueda 		if (unlikely(error))
835d0bcb878SKiyoshi Ueda 			store_barrier_error(md, error);
836d0bcb878SKiyoshi Ueda 		run_queue = 0;
837d0bcb878SKiyoshi Ueda 	} else
838980691e5SKiyoshi Ueda 		blk_end_request_all(rq, error);
839980691e5SKiyoshi Ueda 
840d0bcb878SKiyoshi Ueda 	rq_completed(md, rw, run_queue);
841980691e5SKiyoshi Ueda }
842980691e5SKiyoshi Ueda 
843cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq)
844cec47e3dSKiyoshi Ueda {
845cec47e3dSKiyoshi Ueda 	struct request *clone = rq->special;
846cec47e3dSKiyoshi Ueda 
847cec47e3dSKiyoshi Ueda 	rq->special = NULL;
848cec47e3dSKiyoshi Ueda 	rq->cmd_flags &= ~REQ_DONTPREP;
849cec47e3dSKiyoshi Ueda 
850a77e28c7SKiyoshi Ueda 	free_rq_clone(clone);
851cec47e3dSKiyoshi Ueda }
852cec47e3dSKiyoshi Ueda 
853cec47e3dSKiyoshi Ueda /*
854cec47e3dSKiyoshi Ueda  * Requeue the original request of a clone.
855cec47e3dSKiyoshi Ueda  */
856cec47e3dSKiyoshi Ueda void dm_requeue_unmapped_request(struct request *clone)
857cec47e3dSKiyoshi Ueda {
858b4324feeSKiyoshi Ueda 	int rw = rq_data_dir(clone);
859cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
860cec47e3dSKiyoshi Ueda 	struct mapped_device *md = tio->md;
861cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
862cec47e3dSKiyoshi Ueda 	struct request_queue *q = rq->q;
863cec47e3dSKiyoshi Ueda 	unsigned long flags;
864cec47e3dSKiyoshi Ueda 
86533659ebbSChristoph Hellwig 	if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
866d0bcb878SKiyoshi Ueda 		/*
867d0bcb878SKiyoshi Ueda 		 * Barrier clones share an original request.
868d0bcb878SKiyoshi Ueda 		 * Leave it to dm_end_request(), which handles this special
869d0bcb878SKiyoshi Ueda 		 * case.
870d0bcb878SKiyoshi Ueda 		 */
871d0bcb878SKiyoshi Ueda 		dm_end_request(clone, DM_ENDIO_REQUEUE);
872d0bcb878SKiyoshi Ueda 		return;
873d0bcb878SKiyoshi Ueda 	}
874d0bcb878SKiyoshi Ueda 
875cec47e3dSKiyoshi Ueda 	dm_unprep_request(rq);
876cec47e3dSKiyoshi Ueda 
877cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
878cec47e3dSKiyoshi Ueda 	if (elv_queue_empty(q))
879cec47e3dSKiyoshi Ueda 		blk_plug_device(q);
880cec47e3dSKiyoshi Ueda 	blk_requeue_request(q, rq);
881cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
882cec47e3dSKiyoshi Ueda 
883b4324feeSKiyoshi Ueda 	rq_completed(md, rw, 0);
884cec47e3dSKiyoshi Ueda }
885cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
886cec47e3dSKiyoshi Ueda 
887cec47e3dSKiyoshi Ueda static void __stop_queue(struct request_queue *q)
888cec47e3dSKiyoshi Ueda {
889cec47e3dSKiyoshi Ueda 	blk_stop_queue(q);
890cec47e3dSKiyoshi Ueda }
891cec47e3dSKiyoshi Ueda 
892cec47e3dSKiyoshi Ueda static void stop_queue(struct request_queue *q)
893cec47e3dSKiyoshi Ueda {
894cec47e3dSKiyoshi Ueda 	unsigned long flags;
895cec47e3dSKiyoshi Ueda 
896cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
897cec47e3dSKiyoshi Ueda 	__stop_queue(q);
898cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
899cec47e3dSKiyoshi Ueda }
900cec47e3dSKiyoshi Ueda 
901cec47e3dSKiyoshi Ueda static void __start_queue(struct request_queue *q)
902cec47e3dSKiyoshi Ueda {
903cec47e3dSKiyoshi Ueda 	if (blk_queue_stopped(q))
904cec47e3dSKiyoshi Ueda 		blk_start_queue(q);
905cec47e3dSKiyoshi Ueda }
906cec47e3dSKiyoshi Ueda 
907cec47e3dSKiyoshi Ueda static void start_queue(struct request_queue *q)
908cec47e3dSKiyoshi Ueda {
909cec47e3dSKiyoshi Ueda 	unsigned long flags;
910cec47e3dSKiyoshi Ueda 
911cec47e3dSKiyoshi Ueda 	spin_lock_irqsave(q->queue_lock, flags);
912cec47e3dSKiyoshi Ueda 	__start_queue(q);
913cec47e3dSKiyoshi Ueda 	spin_unlock_irqrestore(q->queue_lock, flags);
914cec47e3dSKiyoshi Ueda }
915cec47e3dSKiyoshi Ueda 
91611a68244SKiyoshi Ueda static void dm_done(struct request *clone, int error, bool mapped)
91711a68244SKiyoshi Ueda {
91811a68244SKiyoshi Ueda 	int r = error;
91911a68244SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
92011a68244SKiyoshi Ueda 	dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
92111a68244SKiyoshi Ueda 
92211a68244SKiyoshi Ueda 	if (mapped && rq_end_io)
92311a68244SKiyoshi Ueda 		r = rq_end_io(tio->ti, clone, error, &tio->info);
92411a68244SKiyoshi Ueda 
92511a68244SKiyoshi Ueda 	if (r <= 0)
92611a68244SKiyoshi Ueda 		/* The target wants to complete the I/O */
92711a68244SKiyoshi Ueda 		dm_end_request(clone, r);
92811a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_INCOMPLETE)
92911a68244SKiyoshi Ueda 		/* The target will handle the I/O */
93011a68244SKiyoshi Ueda 		return;
93111a68244SKiyoshi Ueda 	else if (r == DM_ENDIO_REQUEUE)
93211a68244SKiyoshi Ueda 		/* The target wants to requeue the I/O */
93311a68244SKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
93411a68244SKiyoshi Ueda 	else {
93511a68244SKiyoshi Ueda 		DMWARN("unimplemented target endio return value: %d", r);
93611a68244SKiyoshi Ueda 		BUG();
93711a68244SKiyoshi Ueda 	}
93811a68244SKiyoshi Ueda }
93911a68244SKiyoshi Ueda 
940cec47e3dSKiyoshi Ueda /*
941cec47e3dSKiyoshi Ueda  * Request completion handler for request-based dm
942cec47e3dSKiyoshi Ueda  */
943cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq)
944cec47e3dSKiyoshi Ueda {
94511a68244SKiyoshi Ueda 	bool mapped = true;
946cec47e3dSKiyoshi Ueda 	struct request *clone = rq->completion_data;
947cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
948cec47e3dSKiyoshi Ueda 
94911a68244SKiyoshi Ueda 	if (rq->cmd_flags & REQ_FAILED)
95011a68244SKiyoshi Ueda 		mapped = false;
951cec47e3dSKiyoshi Ueda 
95211a68244SKiyoshi Ueda 	dm_done(clone, tio->error, mapped);
953cec47e3dSKiyoshi Ueda }
954cec47e3dSKiyoshi Ueda 
955cec47e3dSKiyoshi Ueda /*
956cec47e3dSKiyoshi Ueda  * Complete the clone and the original request with the error status
957cec47e3dSKiyoshi Ueda  * through softirq context.
958cec47e3dSKiyoshi Ueda  */
959cec47e3dSKiyoshi Ueda static void dm_complete_request(struct request *clone, int error)
960cec47e3dSKiyoshi Ueda {
961cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
962cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
963cec47e3dSKiyoshi Ueda 
96433659ebbSChristoph Hellwig 	if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
965d0bcb878SKiyoshi Ueda 		/*
966d0bcb878SKiyoshi Ueda 		 * Barrier clones share an original request.  So can't use
967d0bcb878SKiyoshi Ueda 		 * softirq_done with the original.
968d0bcb878SKiyoshi Ueda 		 * Pass the clone to dm_done() directly in this special case.
969d0bcb878SKiyoshi Ueda 		 * It is safe (even if clone->q->queue_lock is held here)
970d0bcb878SKiyoshi Ueda 		 * because there is no I/O dispatching during the completion
971d0bcb878SKiyoshi Ueda 		 * of barrier clone.
972d0bcb878SKiyoshi Ueda 		 */
973d0bcb878SKiyoshi Ueda 		dm_done(clone, error, true);
974d0bcb878SKiyoshi Ueda 		return;
975d0bcb878SKiyoshi Ueda 	}
976d0bcb878SKiyoshi Ueda 
977cec47e3dSKiyoshi Ueda 	tio->error = error;
978cec47e3dSKiyoshi Ueda 	rq->completion_data = clone;
979cec47e3dSKiyoshi Ueda 	blk_complete_request(rq);
980cec47e3dSKiyoshi Ueda }
981cec47e3dSKiyoshi Ueda 
982cec47e3dSKiyoshi Ueda /*
983cec47e3dSKiyoshi Ueda  * Complete the not-mapped clone and the original request with the error status
984cec47e3dSKiyoshi Ueda  * through softirq context.
985cec47e3dSKiyoshi Ueda  * Target's rq_end_io() function isn't called.
986cec47e3dSKiyoshi Ueda  * This may be used when the target's map_rq() function fails.
987cec47e3dSKiyoshi Ueda  */
988cec47e3dSKiyoshi Ueda void dm_kill_unmapped_request(struct request *clone, int error)
989cec47e3dSKiyoshi Ueda {
990cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
991cec47e3dSKiyoshi Ueda 	struct request *rq = tio->orig;
992cec47e3dSKiyoshi Ueda 
99333659ebbSChristoph Hellwig 	if (unlikely(clone->cmd_flags & REQ_HARDBARRIER)) {
994d0bcb878SKiyoshi Ueda 		/*
995d0bcb878SKiyoshi Ueda 		 * Barrier clones share an original request.
996d0bcb878SKiyoshi Ueda 		 * Leave it to dm_end_request(), which handles this special
997d0bcb878SKiyoshi Ueda 		 * case.
998d0bcb878SKiyoshi Ueda 		 */
999d0bcb878SKiyoshi Ueda 		BUG_ON(error > 0);
1000d0bcb878SKiyoshi Ueda 		dm_end_request(clone, error);
1001d0bcb878SKiyoshi Ueda 		return;
1002d0bcb878SKiyoshi Ueda 	}
1003d0bcb878SKiyoshi Ueda 
1004cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_FAILED;
1005cec47e3dSKiyoshi Ueda 	dm_complete_request(clone, error);
1006cec47e3dSKiyoshi Ueda }
1007cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
1008cec47e3dSKiyoshi Ueda 
1009cec47e3dSKiyoshi Ueda /*
1010cec47e3dSKiyoshi Ueda  * Called with the queue lock held
1011cec47e3dSKiyoshi Ueda  */
1012cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error)
1013cec47e3dSKiyoshi Ueda {
1014cec47e3dSKiyoshi Ueda 	/*
1015cec47e3dSKiyoshi Ueda 	 * For just cleaning up the information of the queue in which
1016cec47e3dSKiyoshi Ueda 	 * the clone was dispatched.
1017cec47e3dSKiyoshi Ueda 	 * The clone is *NOT* freed actually here because it is alloced from
1018cec47e3dSKiyoshi Ueda 	 * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
1019cec47e3dSKiyoshi Ueda 	 */
1020cec47e3dSKiyoshi Ueda 	__blk_put_request(clone->q, clone);
1021cec47e3dSKiyoshi Ueda 
1022cec47e3dSKiyoshi Ueda 	/*
1023cec47e3dSKiyoshi Ueda 	 * Actual request completion is done in a softirq context which doesn't
1024cec47e3dSKiyoshi Ueda 	 * hold the queue lock.  Otherwise, deadlock could occur because:
1025cec47e3dSKiyoshi Ueda 	 *     - another request may be submitted by the upper level driver
1026cec47e3dSKiyoshi Ueda 	 *       of the stacking during the completion
1027cec47e3dSKiyoshi Ueda 	 *     - the submission which requires queue lock may be done
1028cec47e3dSKiyoshi Ueda 	 *       against this queue
1029cec47e3dSKiyoshi Ueda 	 */
1030cec47e3dSKiyoshi Ueda 	dm_complete_request(clone, error);
1031cec47e3dSKiyoshi Ueda }
1032cec47e3dSKiyoshi Ueda 
103356a67df7SMike Snitzer /*
103456a67df7SMike Snitzer  * Return maximum size of I/O possible at the supplied sector up to the current
103556a67df7SMike Snitzer  * target boundary.
103656a67df7SMike Snitzer  */
103756a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
10381da177e4SLinus Torvalds {
103956a67df7SMike Snitzer 	sector_t target_offset = dm_target_offset(ti, sector);
104056a67df7SMike Snitzer 
104156a67df7SMike Snitzer 	return ti->len - target_offset;
104256a67df7SMike Snitzer }
104356a67df7SMike Snitzer 
104456a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti)
104556a67df7SMike Snitzer {
104656a67df7SMike Snitzer 	sector_t len = max_io_len_target_boundary(sector, ti);
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 	/*
10491da177e4SLinus Torvalds 	 * Does the target need to split even further ?
10501da177e4SLinus Torvalds 	 */
10511da177e4SLinus Torvalds 	if (ti->split_io) {
10521da177e4SLinus Torvalds 		sector_t boundary;
105356a67df7SMike Snitzer 		sector_t offset = dm_target_offset(ti, sector);
10541da177e4SLinus Torvalds 		boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
10551da177e4SLinus Torvalds 			   - offset;
10561da177e4SLinus Torvalds 		if (len > boundary)
10571da177e4SLinus Torvalds 			len = boundary;
10581da177e4SLinus Torvalds 	}
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 	return len;
10611da177e4SLinus Torvalds }
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds static void __map_bio(struct dm_target *ti, struct bio *clone,
1064028867acSAlasdair G Kergon 		      struct dm_target_io *tio)
10651da177e4SLinus Torvalds {
10661da177e4SLinus Torvalds 	int r;
10672056a782SJens Axboe 	sector_t sector;
10689faf400fSStefan Bader 	struct mapped_device *md;
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
10711da177e4SLinus Torvalds 	clone->bi_private = tio;
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 	/*
10741da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
10751da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
10761da177e4SLinus Torvalds 	 * this io.
10771da177e4SLinus Torvalds 	 */
10781da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
10792056a782SJens Axboe 	sector = clone->bi_sector;
10801da177e4SLinus Torvalds 	r = ti->type->map(ti, clone, &tio->info);
108145cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
10821da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
10832056a782SJens Axboe 
10845f3ea37cSArnaldo Carvalho de Melo 		trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
108522a7c31aSAlan D. Brunelle 				    tio->io->bio->bi_bdev->bd_dev, sector);
10862056a782SJens Axboe 
10871da177e4SLinus Torvalds 		generic_make_request(clone);
10882e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
10892e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
10909faf400fSStefan Bader 		md = tio->io->md;
10919faf400fSStefan Bader 		dec_pending(tio->io, r);
10929faf400fSStefan Bader 		/*
10939faf400fSStefan Bader 		 * Store bio_set for cleanup.
10949faf400fSStefan Bader 		 */
10959faf400fSStefan Bader 		clone->bi_private = md->bs;
10961da177e4SLinus Torvalds 		bio_put(clone);
10979faf400fSStefan Bader 		free_tio(md, tio);
109845cbcd79SKiyoshi Ueda 	} else if (r) {
109945cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
110045cbcd79SKiyoshi Ueda 		BUG();
11011da177e4SLinus Torvalds 	}
11021da177e4SLinus Torvalds }
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds struct clone_info {
11051da177e4SLinus Torvalds 	struct mapped_device *md;
11061da177e4SLinus Torvalds 	struct dm_table *map;
11071da177e4SLinus Torvalds 	struct bio *bio;
11081da177e4SLinus Torvalds 	struct dm_io *io;
11091da177e4SLinus Torvalds 	sector_t sector;
11101da177e4SLinus Torvalds 	sector_t sector_count;
11111da177e4SLinus Torvalds 	unsigned short idx;
11121da177e4SLinus Torvalds };
11131da177e4SLinus Torvalds 
11143676347aSPeter Osterlund static void dm_bio_destructor(struct bio *bio)
11153676347aSPeter Osterlund {
11169faf400fSStefan Bader 	struct bio_set *bs = bio->bi_private;
11179faf400fSStefan Bader 
11189faf400fSStefan Bader 	bio_free(bio, bs);
11193676347aSPeter Osterlund }
11203676347aSPeter Osterlund 
11211da177e4SLinus Torvalds /*
11221da177e4SLinus Torvalds  * Creates a little bio that is just does part of a bvec.
11231da177e4SLinus Torvalds  */
11241da177e4SLinus Torvalds static struct bio *split_bvec(struct bio *bio, sector_t sector,
11251da177e4SLinus Torvalds 			      unsigned short idx, unsigned int offset,
11269faf400fSStefan Bader 			      unsigned int len, struct bio_set *bs)
11271da177e4SLinus Torvalds {
11281da177e4SLinus Torvalds 	struct bio *clone;
11291da177e4SLinus Torvalds 	struct bio_vec *bv = bio->bi_io_vec + idx;
11301da177e4SLinus Torvalds 
11319faf400fSStefan Bader 	clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
11323676347aSPeter Osterlund 	clone->bi_destructor = dm_bio_destructor;
11331da177e4SLinus Torvalds 	*clone->bi_io_vec = *bv;
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 	clone->bi_sector = sector;
11361da177e4SLinus Torvalds 	clone->bi_bdev = bio->bi_bdev;
11377b6d91daSChristoph Hellwig 	clone->bi_rw = bio->bi_rw & ~REQ_HARDBARRIER;
11381da177e4SLinus Torvalds 	clone->bi_vcnt = 1;
11391da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
11401da177e4SLinus Torvalds 	clone->bi_io_vec->bv_offset = offset;
11411da177e4SLinus Torvalds 	clone->bi_io_vec->bv_len = clone->bi_size;
1142f3e1d26eSMartin K. Petersen 	clone->bi_flags |= 1 << BIO_CLONED;
11431da177e4SLinus Torvalds 
11449c47008dSMartin K. Petersen 	if (bio_integrity(bio)) {
11457878cba9SMartin K. Petersen 		bio_integrity_clone(clone, bio, GFP_NOIO, bs);
11469c47008dSMartin K. Petersen 		bio_integrity_trim(clone,
11479c47008dSMartin K. Petersen 				   bio_sector_offset(bio, idx, offset), len);
11489c47008dSMartin K. Petersen 	}
11499c47008dSMartin K. Petersen 
11501da177e4SLinus Torvalds 	return clone;
11511da177e4SLinus Torvalds }
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds /*
11541da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
11551da177e4SLinus Torvalds  */
11561da177e4SLinus Torvalds static struct bio *clone_bio(struct bio *bio, sector_t sector,
11571da177e4SLinus Torvalds 			     unsigned short idx, unsigned short bv_count,
11589faf400fSStefan Bader 			     unsigned int len, struct bio_set *bs)
11591da177e4SLinus Torvalds {
11601da177e4SLinus Torvalds 	struct bio *clone;
11611da177e4SLinus Torvalds 
11629faf400fSStefan Bader 	clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
11639faf400fSStefan Bader 	__bio_clone(clone, bio);
11647b6d91daSChristoph Hellwig 	clone->bi_rw &= ~REQ_HARDBARRIER;
11659faf400fSStefan Bader 	clone->bi_destructor = dm_bio_destructor;
11661da177e4SLinus Torvalds 	clone->bi_sector = sector;
11671da177e4SLinus Torvalds 	clone->bi_idx = idx;
11681da177e4SLinus Torvalds 	clone->bi_vcnt = idx + bv_count;
11691da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
11701da177e4SLinus Torvalds 	clone->bi_flags &= ~(1 << BIO_SEG_VALID);
11711da177e4SLinus Torvalds 
11729c47008dSMartin K. Petersen 	if (bio_integrity(bio)) {
11737878cba9SMartin K. Petersen 		bio_integrity_clone(clone, bio, GFP_NOIO, bs);
11749c47008dSMartin K. Petersen 
11759c47008dSMartin K. Petersen 		if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
11769c47008dSMartin K. Petersen 			bio_integrity_trim(clone,
11779c47008dSMartin K. Petersen 					   bio_sector_offset(bio, idx, 0), len);
11789c47008dSMartin K. Petersen 	}
11799c47008dSMartin K. Petersen 
11801da177e4SLinus Torvalds 	return clone;
11811da177e4SLinus Torvalds }
11821da177e4SLinus Torvalds 
11839015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci,
11849015df24SAlasdair G Kergon 				      struct dm_target *ti)
1185f9ab94ceSMikulas Patocka {
11869015df24SAlasdair G Kergon 	struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
1187f9ab94ceSMikulas Patocka 
1188f9ab94ceSMikulas Patocka 	tio->io = ci->io;
1189f9ab94ceSMikulas Patocka 	tio->ti = ti;
1190f9ab94ceSMikulas Patocka 	memset(&tio->info, 0, sizeof(tio->info));
11919015df24SAlasdair G Kergon 
11929015df24SAlasdair G Kergon 	return tio;
11939015df24SAlasdair G Kergon }
11949015df24SAlasdair G Kergon 
119506a426ceSMike Snitzer static void __issue_target_request(struct clone_info *ci, struct dm_target *ti,
1196*a79245b3SMike Snitzer 				   unsigned request_nr, sector_t len)
11979015df24SAlasdair G Kergon {
11989015df24SAlasdair G Kergon 	struct dm_target_io *tio = alloc_tio(ci, ti);
11999015df24SAlasdair G Kergon 	struct bio *clone;
12009015df24SAlasdair G Kergon 
120157cba5d3SMike Snitzer 	tio->info.target_request_nr = request_nr;
1202f9ab94ceSMikulas Patocka 
120306a426ceSMike Snitzer 	/*
120406a426ceSMike Snitzer 	 * Discard requests require the bio's inline iovecs be initialized.
120506a426ceSMike Snitzer 	 * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
120606a426ceSMike Snitzer 	 * and discard, so no need for concern about wasted bvec allocations.
120706a426ceSMike Snitzer 	 */
120806a426ceSMike Snitzer 	clone = bio_alloc_bioset(GFP_NOIO, ci->bio->bi_max_vecs, ci->md->bs);
1209f9ab94ceSMikulas Patocka 	__bio_clone(clone, ci->bio);
1210f9ab94ceSMikulas Patocka 	clone->bi_destructor = dm_bio_destructor;
1211*a79245b3SMike Snitzer 	if (len) {
1212*a79245b3SMike Snitzer 		clone->bi_sector = ci->sector;
1213*a79245b3SMike Snitzer 		clone->bi_size = to_bytes(len);
1214*a79245b3SMike Snitzer 	}
1215f9ab94ceSMikulas Patocka 
1216f9ab94ceSMikulas Patocka 	__map_bio(ti, clone, tio);
1217f9ab94ceSMikulas Patocka }
1218f9ab94ceSMikulas Patocka 
121906a426ceSMike Snitzer static void __issue_target_requests(struct clone_info *ci, struct dm_target *ti,
1220*a79245b3SMike Snitzer 				    unsigned num_requests, sector_t len)
122106a426ceSMike Snitzer {
122206a426ceSMike Snitzer 	unsigned request_nr;
122306a426ceSMike Snitzer 
122406a426ceSMike Snitzer 	for (request_nr = 0; request_nr < num_requests; request_nr++)
1225*a79245b3SMike Snitzer 		__issue_target_request(ci, ti, request_nr, len);
122606a426ceSMike Snitzer }
122706a426ceSMike Snitzer 
1228f9ab94ceSMikulas Patocka static int __clone_and_map_empty_barrier(struct clone_info *ci)
1229f9ab94ceSMikulas Patocka {
123006a426ceSMike Snitzer 	unsigned target_nr = 0;
1231f9ab94ceSMikulas Patocka 	struct dm_target *ti;
1232f9ab94ceSMikulas Patocka 
1233f9ab94ceSMikulas Patocka 	while ((ti = dm_table_get_target(ci->map, target_nr++)))
1234*a79245b3SMike Snitzer 		__issue_target_requests(ci, ti, ti->num_flush_requests, 0);
1235f9ab94ceSMikulas Patocka 
1236f9ab94ceSMikulas Patocka 	ci->sector_count = 0;
1237f9ab94ceSMikulas Patocka 
1238f9ab94ceSMikulas Patocka 	return 0;
1239f9ab94ceSMikulas Patocka }
1240f9ab94ceSMikulas Patocka 
12415ae89a87SMike Snitzer /*
12425ae89a87SMike Snitzer  * Perform all io with a single clone.
12435ae89a87SMike Snitzer  */
12445ae89a87SMike Snitzer static void __clone_and_map_simple(struct clone_info *ci, struct dm_target *ti)
12455ae89a87SMike Snitzer {
12465ae89a87SMike Snitzer 	struct bio *clone, *bio = ci->bio;
12475ae89a87SMike Snitzer 	struct dm_target_io *tio;
12485ae89a87SMike Snitzer 
12495ae89a87SMike Snitzer 	tio = alloc_tio(ci, ti);
12505ae89a87SMike Snitzer 	clone = clone_bio(bio, ci->sector, ci->idx,
12515ae89a87SMike Snitzer 			  bio->bi_vcnt - ci->idx, ci->sector_count,
12525ae89a87SMike Snitzer 			  ci->md->bs);
12535ae89a87SMike Snitzer 	__map_bio(ti, clone, tio);
12545ae89a87SMike Snitzer 	ci->sector_count = 0;
12555ae89a87SMike Snitzer }
12565ae89a87SMike Snitzer 
12575ae89a87SMike Snitzer static int __clone_and_map_discard(struct clone_info *ci)
12585ae89a87SMike Snitzer {
12595ae89a87SMike Snitzer 	struct dm_target *ti;
1260*a79245b3SMike Snitzer 	sector_t len;
12615ae89a87SMike Snitzer 
1262*a79245b3SMike Snitzer 	do {
12635ae89a87SMike Snitzer 		ti = dm_table_find_target(ci->map, ci->sector);
12645ae89a87SMike Snitzer 		if (!dm_target_is_valid(ti))
12655ae89a87SMike Snitzer 			return -EIO;
12665ae89a87SMike Snitzer 
12675ae89a87SMike Snitzer 		/*
12685ae89a87SMike Snitzer 		 * Even though the device advertised discard support,
12695ae89a87SMike Snitzer 		 * reconfiguration might have changed that since the
12705ae89a87SMike Snitzer 		 * check was performed.
12715ae89a87SMike Snitzer 		 */
12725ae89a87SMike Snitzer 		if (!ti->num_discard_requests)
12735ae89a87SMike Snitzer 			return -EOPNOTSUPP;
12745ae89a87SMike Snitzer 
1275*a79245b3SMike Snitzer 		len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
12765ae89a87SMike Snitzer 
1277*a79245b3SMike Snitzer 		__issue_target_requests(ci, ti, ti->num_discard_requests, len);
12785ae89a87SMike Snitzer 
1279*a79245b3SMike Snitzer 		ci->sector += len;
1280*a79245b3SMike Snitzer 	} while (ci->sector_count -= len);
12815ae89a87SMike Snitzer 
12825ae89a87SMike Snitzer 	return 0;
12835ae89a87SMike Snitzer }
12845ae89a87SMike Snitzer 
1285512875bdSJun'ichi Nomura static int __clone_and_map(struct clone_info *ci)
12861da177e4SLinus Torvalds {
12871da177e4SLinus Torvalds 	struct bio *clone, *bio = ci->bio;
1288512875bdSJun'ichi Nomura 	struct dm_target *ti;
1289512875bdSJun'ichi Nomura 	sector_t len = 0, max;
1290028867acSAlasdair G Kergon 	struct dm_target_io *tio;
12911da177e4SLinus Torvalds 
1292f9ab94ceSMikulas Patocka 	if (unlikely(bio_empty_barrier(bio)))
1293f9ab94ceSMikulas Patocka 		return __clone_and_map_empty_barrier(ci);
1294f9ab94ceSMikulas Patocka 
12955ae89a87SMike Snitzer 	if (unlikely(bio->bi_rw & REQ_DISCARD))
12965ae89a87SMike Snitzer 		return __clone_and_map_discard(ci);
12975ae89a87SMike Snitzer 
1298512875bdSJun'ichi Nomura 	ti = dm_table_find_target(ci->map, ci->sector);
1299512875bdSJun'ichi Nomura 	if (!dm_target_is_valid(ti))
1300512875bdSJun'ichi Nomura 		return -EIO;
1301512875bdSJun'ichi Nomura 
130256a67df7SMike Snitzer 	max = max_io_len(ci->sector, ti);
1303512875bdSJun'ichi Nomura 
13041da177e4SLinus Torvalds 	if (ci->sector_count <= max) {
13051da177e4SLinus Torvalds 		/*
13061da177e4SLinus Torvalds 		 * Optimise for the simple case where we can do all of
13071da177e4SLinus Torvalds 		 * the remaining io with a single clone.
13081da177e4SLinus Torvalds 		 */
13095ae89a87SMike Snitzer 		__clone_and_map_simple(ci, ti);
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 	} else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
13121da177e4SLinus Torvalds 		/*
13131da177e4SLinus Torvalds 		 * There are some bvecs that don't span targets.
13141da177e4SLinus Torvalds 		 * Do as many of these as possible.
13151da177e4SLinus Torvalds 		 */
13161da177e4SLinus Torvalds 		int i;
13171da177e4SLinus Torvalds 		sector_t remaining = max;
13181da177e4SLinus Torvalds 		sector_t bv_len;
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 		for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
13211da177e4SLinus Torvalds 			bv_len = to_sector(bio->bi_io_vec[i].bv_len);
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 			if (bv_len > remaining)
13241da177e4SLinus Torvalds 				break;
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 			remaining -= bv_len;
13271da177e4SLinus Torvalds 			len += bv_len;
13281da177e4SLinus Torvalds 		}
13291da177e4SLinus Torvalds 
13305ae89a87SMike Snitzer 		tio = alloc_tio(ci, ti);
13319faf400fSStefan Bader 		clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
13329faf400fSStefan Bader 				  ci->md->bs);
13331da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 		ci->sector += len;
13361da177e4SLinus Torvalds 		ci->sector_count -= len;
13371da177e4SLinus Torvalds 		ci->idx = i;
13381da177e4SLinus Torvalds 
13391da177e4SLinus Torvalds 	} else {
13401da177e4SLinus Torvalds 		/*
1341d2044a94SAlasdair G Kergon 		 * Handle a bvec that must be split between two or more targets.
13421da177e4SLinus Torvalds 		 */
13431da177e4SLinus Torvalds 		struct bio_vec *bv = bio->bi_io_vec + ci->idx;
1344d2044a94SAlasdair G Kergon 		sector_t remaining = to_sector(bv->bv_len);
1345d2044a94SAlasdair G Kergon 		unsigned int offset = 0;
13461da177e4SLinus Torvalds 
1347d2044a94SAlasdair G Kergon 		do {
1348d2044a94SAlasdair G Kergon 			if (offset) {
13491da177e4SLinus Torvalds 				ti = dm_table_find_target(ci->map, ci->sector);
1350512875bdSJun'ichi Nomura 				if (!dm_target_is_valid(ti))
1351512875bdSJun'ichi Nomura 					return -EIO;
1352512875bdSJun'ichi Nomura 
135356a67df7SMike Snitzer 				max = max_io_len(ci->sector, ti);
1354d2044a94SAlasdair G Kergon 			}
1355d2044a94SAlasdair G Kergon 
1356d2044a94SAlasdair G Kergon 			len = min(remaining, max);
1357d2044a94SAlasdair G Kergon 
13585ae89a87SMike Snitzer 			tio = alloc_tio(ci, ti);
1359d2044a94SAlasdair G Kergon 			clone = split_bvec(bio, ci->sector, ci->idx,
13609faf400fSStefan Bader 					   bv->bv_offset + offset, len,
13619faf400fSStefan Bader 					   ci->md->bs);
1362d2044a94SAlasdair G Kergon 
13631da177e4SLinus Torvalds 			__map_bio(ti, clone, tio);
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds 			ci->sector += len;
13661da177e4SLinus Torvalds 			ci->sector_count -= len;
1367d2044a94SAlasdair G Kergon 			offset += to_bytes(len);
1368d2044a94SAlasdair G Kergon 		} while (remaining -= len);
1369d2044a94SAlasdair G Kergon 
13701da177e4SLinus Torvalds 		ci->idx++;
13711da177e4SLinus Torvalds 	}
1372512875bdSJun'ichi Nomura 
1373512875bdSJun'ichi Nomura 	return 0;
13741da177e4SLinus Torvalds }
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds /*
13778a53c28dSMikulas Patocka  * Split the bio into several clones and submit it to targets.
13781da177e4SLinus Torvalds  */
1379f0b9a450SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
13801da177e4SLinus Torvalds {
13811da177e4SLinus Torvalds 	struct clone_info ci;
1382512875bdSJun'ichi Nomura 	int error = 0;
13831da177e4SLinus Torvalds 
13847c666411SAlasdair G Kergon 	ci.map = dm_get_live_table(md);
1385f0b9a450SMikulas Patocka 	if (unlikely(!ci.map)) {
13867b6d91daSChristoph Hellwig 		if (!(bio->bi_rw & REQ_HARDBARRIER))
1387f0b9a450SMikulas Patocka 			bio_io_error(bio);
1388af7e466aSMikulas Patocka 		else
13895aa2781dSMikulas Patocka 			if (!md->barrier_error)
1390af7e466aSMikulas Patocka 				md->barrier_error = -EIO;
1391f0b9a450SMikulas Patocka 		return;
1392f0b9a450SMikulas Patocka 	}
1393692d0eb9SMikulas Patocka 
13941da177e4SLinus Torvalds 	ci.md = md;
13951da177e4SLinus Torvalds 	ci.bio = bio;
13961da177e4SLinus Torvalds 	ci.io = alloc_io(md);
13971da177e4SLinus Torvalds 	ci.io->error = 0;
13981da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
13991da177e4SLinus Torvalds 	ci.io->bio = bio;
14001da177e4SLinus Torvalds 	ci.io->md = md;
1401f88fb981SKiyoshi Ueda 	spin_lock_init(&ci.io->endio_lock);
14021da177e4SLinus Torvalds 	ci.sector = bio->bi_sector;
14031da177e4SLinus Torvalds 	ci.sector_count = bio_sectors(bio);
1404f9ab94ceSMikulas Patocka 	if (unlikely(bio_empty_barrier(bio)))
1405f9ab94ceSMikulas Patocka 		ci.sector_count = 1;
14061da177e4SLinus Torvalds 	ci.idx = bio->bi_idx;
14071da177e4SLinus Torvalds 
14083eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
1409512875bdSJun'ichi Nomura 	while (ci.sector_count && !error)
1410512875bdSJun'ichi Nomura 		error = __clone_and_map(&ci);
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds 	/* drop the extra reference count */
1413512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
14141da177e4SLinus Torvalds 	dm_table_put(ci.map);
14151da177e4SLinus Torvalds }
14161da177e4SLinus Torvalds /*-----------------------------------------------------------------
14171da177e4SLinus Torvalds  * CRUD END
14181da177e4SLinus Torvalds  *---------------------------------------------------------------*/
14191da177e4SLinus Torvalds 
1420f6fccb12SMilan Broz static int dm_merge_bvec(struct request_queue *q,
1421f6fccb12SMilan Broz 			 struct bvec_merge_data *bvm,
1422f6fccb12SMilan Broz 			 struct bio_vec *biovec)
1423f6fccb12SMilan Broz {
1424f6fccb12SMilan Broz 	struct mapped_device *md = q->queuedata;
14257c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1426f6fccb12SMilan Broz 	struct dm_target *ti;
1427f6fccb12SMilan Broz 	sector_t max_sectors;
14285037108aSMikulas Patocka 	int max_size = 0;
1429f6fccb12SMilan Broz 
1430f6fccb12SMilan Broz 	if (unlikely(!map))
14315037108aSMikulas Patocka 		goto out;
1432f6fccb12SMilan Broz 
1433f6fccb12SMilan Broz 	ti = dm_table_find_target(map, bvm->bi_sector);
1434b01cd5acSMikulas Patocka 	if (!dm_target_is_valid(ti))
1435b01cd5acSMikulas Patocka 		goto out_table;
1436f6fccb12SMilan Broz 
1437f6fccb12SMilan Broz 	/*
1438f6fccb12SMilan Broz 	 * Find maximum amount of I/O that won't need splitting
1439f6fccb12SMilan Broz 	 */
144056a67df7SMike Snitzer 	max_sectors = min(max_io_len(bvm->bi_sector, ti),
1441f6fccb12SMilan Broz 			  (sector_t) BIO_MAX_SECTORS);
1442f6fccb12SMilan Broz 	max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
1443f6fccb12SMilan Broz 	if (max_size < 0)
1444f6fccb12SMilan Broz 		max_size = 0;
1445f6fccb12SMilan Broz 
1446f6fccb12SMilan Broz 	/*
1447f6fccb12SMilan Broz 	 * merge_bvec_fn() returns number of bytes
1448f6fccb12SMilan Broz 	 * it can accept at this offset
1449f6fccb12SMilan Broz 	 * max is precomputed maximal io size
1450f6fccb12SMilan Broz 	 */
1451f6fccb12SMilan Broz 	if (max_size && ti->type->merge)
1452f6fccb12SMilan Broz 		max_size = ti->type->merge(ti, bvm, biovec, max_size);
14538cbeb67aSMikulas Patocka 	/*
14548cbeb67aSMikulas Patocka 	 * If the target doesn't support merge method and some of the devices
14558cbeb67aSMikulas Patocka 	 * provided their merge_bvec method (we know this by looking at
14568cbeb67aSMikulas Patocka 	 * queue_max_hw_sectors), then we can't allow bios with multiple vector
14578cbeb67aSMikulas Patocka 	 * entries.  So always set max_size to 0, and the code below allows
14588cbeb67aSMikulas Patocka 	 * just one page.
14598cbeb67aSMikulas Patocka 	 */
14608cbeb67aSMikulas Patocka 	else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
14618cbeb67aSMikulas Patocka 
14628cbeb67aSMikulas Patocka 		max_size = 0;
1463f6fccb12SMilan Broz 
1464b01cd5acSMikulas Patocka out_table:
14655037108aSMikulas Patocka 	dm_table_put(map);
14665037108aSMikulas Patocka 
14675037108aSMikulas Patocka out:
1468f6fccb12SMilan Broz 	/*
1469f6fccb12SMilan Broz 	 * Always allow an entire first page
1470f6fccb12SMilan Broz 	 */
1471f6fccb12SMilan Broz 	if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
1472f6fccb12SMilan Broz 		max_size = biovec->bv_len;
1473f6fccb12SMilan Broz 
1474f6fccb12SMilan Broz 	return max_size;
1475f6fccb12SMilan Broz }
1476f6fccb12SMilan Broz 
14771da177e4SLinus Torvalds /*
14781da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
14791da177e4SLinus Torvalds  * dm_merge_bvec.
14801da177e4SLinus Torvalds  */
1481cec47e3dSKiyoshi Ueda static int _dm_request(struct request_queue *q, struct bio *bio)
14821da177e4SLinus Torvalds {
148312f03a49SKevin Corry 	int rw = bio_data_dir(bio);
14841da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
1485c9959059STejun Heo 	int cpu;
14861da177e4SLinus Torvalds 
14872ca3310eSAlasdair G Kergon 	down_read(&md->io_lock);
14881da177e4SLinus Torvalds 
1489074a7acaSTejun Heo 	cpu = part_stat_lock();
1490074a7acaSTejun Heo 	part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
1491074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
1492074a7acaSTejun Heo 	part_stat_unlock();
149312f03a49SKevin Corry 
14941da177e4SLinus Torvalds 	/*
14951eb787ecSAlasdair G Kergon 	 * If we're suspended or the thread is processing barriers
14961eb787ecSAlasdair G Kergon 	 * we have to queue this io for later.
14971da177e4SLinus Torvalds 	 */
1498af7e466aSMikulas Patocka 	if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
14997b6d91daSChristoph Hellwig 	    unlikely(bio->bi_rw & REQ_HARDBARRIER)) {
15002ca3310eSAlasdair G Kergon 		up_read(&md->io_lock);
15011da177e4SLinus Torvalds 
150254d9a1b4SAlasdair G Kergon 		if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
150354d9a1b4SAlasdair G Kergon 		    bio_rw(bio) == READA) {
150454d9a1b4SAlasdair G Kergon 			bio_io_error(bio);
150554d9a1b4SAlasdair G Kergon 			return 0;
150654d9a1b4SAlasdair G Kergon 		}
15071da177e4SLinus Torvalds 
150892c63902SMikulas Patocka 		queue_io(md, bio);
15091da177e4SLinus Torvalds 
151092c63902SMikulas Patocka 		return 0;
15111da177e4SLinus Torvalds 	}
15121da177e4SLinus Torvalds 
1513f0b9a450SMikulas Patocka 	__split_and_process_bio(md, bio);
15142ca3310eSAlasdair G Kergon 	up_read(&md->io_lock);
1515f0b9a450SMikulas Patocka 	return 0;
15161da177e4SLinus Torvalds }
15171da177e4SLinus Torvalds 
1518cec47e3dSKiyoshi Ueda static int dm_make_request(struct request_queue *q, struct bio *bio)
1519cec47e3dSKiyoshi Ueda {
1520cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1521cec47e3dSKiyoshi Ueda 
1522cec47e3dSKiyoshi Ueda 	return md->saved_make_request_fn(q, bio); /* call __make_request() */
1523cec47e3dSKiyoshi Ueda }
1524cec47e3dSKiyoshi Ueda 
1525cec47e3dSKiyoshi Ueda static int dm_request_based(struct mapped_device *md)
1526cec47e3dSKiyoshi Ueda {
1527cec47e3dSKiyoshi Ueda 	return blk_queue_stackable(md->queue);
1528cec47e3dSKiyoshi Ueda }
1529cec47e3dSKiyoshi Ueda 
1530cec47e3dSKiyoshi Ueda static int dm_request(struct request_queue *q, struct bio *bio)
1531cec47e3dSKiyoshi Ueda {
1532cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1533cec47e3dSKiyoshi Ueda 
1534cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
1535cec47e3dSKiyoshi Ueda 		return dm_make_request(q, bio);
1536cec47e3dSKiyoshi Ueda 
1537cec47e3dSKiyoshi Ueda 	return _dm_request(q, bio);
1538cec47e3dSKiyoshi Ueda }
1539cec47e3dSKiyoshi Ueda 
1540d0bcb878SKiyoshi Ueda static bool dm_rq_is_flush_request(struct request *rq)
1541d0bcb878SKiyoshi Ueda {
1542144d6ed5SFUJITA Tomonori 	if (rq->cmd_flags & REQ_FLUSH)
1543d0bcb878SKiyoshi Ueda 		return true;
1544d0bcb878SKiyoshi Ueda 	else
1545d0bcb878SKiyoshi Ueda 		return false;
1546d0bcb878SKiyoshi Ueda }
1547d0bcb878SKiyoshi Ueda 
1548cec47e3dSKiyoshi Ueda void dm_dispatch_request(struct request *rq)
1549cec47e3dSKiyoshi Ueda {
1550cec47e3dSKiyoshi Ueda 	int r;
1551cec47e3dSKiyoshi Ueda 
1552cec47e3dSKiyoshi Ueda 	if (blk_queue_io_stat(rq->q))
1553cec47e3dSKiyoshi Ueda 		rq->cmd_flags |= REQ_IO_STAT;
1554cec47e3dSKiyoshi Ueda 
1555cec47e3dSKiyoshi Ueda 	rq->start_time = jiffies;
1556cec47e3dSKiyoshi Ueda 	r = blk_insert_cloned_request(rq->q, rq);
1557cec47e3dSKiyoshi Ueda 	if (r)
1558cec47e3dSKiyoshi Ueda 		dm_complete_request(rq, r);
1559cec47e3dSKiyoshi Ueda }
1560cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_dispatch_request);
1561cec47e3dSKiyoshi Ueda 
1562cec47e3dSKiyoshi Ueda static void dm_rq_bio_destructor(struct bio *bio)
1563cec47e3dSKiyoshi Ueda {
1564cec47e3dSKiyoshi Ueda 	struct dm_rq_clone_bio_info *info = bio->bi_private;
1565cec47e3dSKiyoshi Ueda 	struct mapped_device *md = info->tio->md;
1566cec47e3dSKiyoshi Ueda 
1567cec47e3dSKiyoshi Ueda 	free_bio_info(info);
1568cec47e3dSKiyoshi Ueda 	bio_free(bio, md->bs);
1569cec47e3dSKiyoshi Ueda }
1570cec47e3dSKiyoshi Ueda 
1571cec47e3dSKiyoshi Ueda static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
1572cec47e3dSKiyoshi Ueda 				 void *data)
1573cec47e3dSKiyoshi Ueda {
1574cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = data;
1575cec47e3dSKiyoshi Ueda 	struct mapped_device *md = tio->md;
1576cec47e3dSKiyoshi Ueda 	struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
1577cec47e3dSKiyoshi Ueda 
1578cec47e3dSKiyoshi Ueda 	if (!info)
1579cec47e3dSKiyoshi Ueda 		return -ENOMEM;
1580cec47e3dSKiyoshi Ueda 
1581cec47e3dSKiyoshi Ueda 	info->orig = bio_orig;
1582cec47e3dSKiyoshi Ueda 	info->tio = tio;
1583cec47e3dSKiyoshi Ueda 	bio->bi_end_io = end_clone_bio;
1584cec47e3dSKiyoshi Ueda 	bio->bi_private = info;
1585cec47e3dSKiyoshi Ueda 	bio->bi_destructor = dm_rq_bio_destructor;
1586cec47e3dSKiyoshi Ueda 
1587cec47e3dSKiyoshi Ueda 	return 0;
1588cec47e3dSKiyoshi Ueda }
1589cec47e3dSKiyoshi Ueda 
1590cec47e3dSKiyoshi Ueda static int setup_clone(struct request *clone, struct request *rq,
1591cec47e3dSKiyoshi Ueda 		       struct dm_rq_target_io *tio)
1592cec47e3dSKiyoshi Ueda {
1593d0bcb878SKiyoshi Ueda 	int r;
1594cec47e3dSKiyoshi Ueda 
1595d0bcb878SKiyoshi Ueda 	if (dm_rq_is_flush_request(rq)) {
1596d0bcb878SKiyoshi Ueda 		blk_rq_init(NULL, clone);
1597d0bcb878SKiyoshi Ueda 		clone->cmd_type = REQ_TYPE_FS;
1598d0bcb878SKiyoshi Ueda 		clone->cmd_flags |= (REQ_HARDBARRIER | WRITE);
1599d0bcb878SKiyoshi Ueda 	} else {
1600d0bcb878SKiyoshi Ueda 		r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
1601d0bcb878SKiyoshi Ueda 				      dm_rq_bio_constructor, tio);
1602cec47e3dSKiyoshi Ueda 		if (r)
1603cec47e3dSKiyoshi Ueda 			return r;
1604cec47e3dSKiyoshi Ueda 
1605cec47e3dSKiyoshi Ueda 		clone->cmd = rq->cmd;
1606cec47e3dSKiyoshi Ueda 		clone->cmd_len = rq->cmd_len;
1607cec47e3dSKiyoshi Ueda 		clone->sense = rq->sense;
1608cec47e3dSKiyoshi Ueda 		clone->buffer = rq->buffer;
1609d0bcb878SKiyoshi Ueda 	}
1610d0bcb878SKiyoshi Ueda 
1611cec47e3dSKiyoshi Ueda 	clone->end_io = end_clone_request;
1612cec47e3dSKiyoshi Ueda 	clone->end_io_data = tio;
1613cec47e3dSKiyoshi Ueda 
1614cec47e3dSKiyoshi Ueda 	return 0;
1615cec47e3dSKiyoshi Ueda }
1616cec47e3dSKiyoshi Ueda 
16176facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md,
16186facdaffSKiyoshi Ueda 				gfp_t gfp_mask)
16196facdaffSKiyoshi Ueda {
16206facdaffSKiyoshi Ueda 	struct request *clone;
16216facdaffSKiyoshi Ueda 	struct dm_rq_target_io *tio;
16226facdaffSKiyoshi Ueda 
16236facdaffSKiyoshi Ueda 	tio = alloc_rq_tio(md, gfp_mask);
16246facdaffSKiyoshi Ueda 	if (!tio)
16256facdaffSKiyoshi Ueda 		return NULL;
16266facdaffSKiyoshi Ueda 
16276facdaffSKiyoshi Ueda 	tio->md = md;
16286facdaffSKiyoshi Ueda 	tio->ti = NULL;
16296facdaffSKiyoshi Ueda 	tio->orig = rq;
16306facdaffSKiyoshi Ueda 	tio->error = 0;
16316facdaffSKiyoshi Ueda 	memset(&tio->info, 0, sizeof(tio->info));
16326facdaffSKiyoshi Ueda 
16336facdaffSKiyoshi Ueda 	clone = &tio->clone;
16346facdaffSKiyoshi Ueda 	if (setup_clone(clone, rq, tio)) {
16356facdaffSKiyoshi Ueda 		/* -ENOMEM */
16366facdaffSKiyoshi Ueda 		free_rq_tio(tio);
16376facdaffSKiyoshi Ueda 		return NULL;
16386facdaffSKiyoshi Ueda 	}
16396facdaffSKiyoshi Ueda 
16406facdaffSKiyoshi Ueda 	return clone;
16416facdaffSKiyoshi Ueda }
16426facdaffSKiyoshi Ueda 
1643cec47e3dSKiyoshi Ueda /*
1644cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1645cec47e3dSKiyoshi Ueda  */
1646cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq)
1647cec47e3dSKiyoshi Ueda {
1648cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
1649cec47e3dSKiyoshi Ueda 	struct request *clone;
1650cec47e3dSKiyoshi Ueda 
1651d0bcb878SKiyoshi Ueda 	if (unlikely(dm_rq_is_flush_request(rq)))
1652d0bcb878SKiyoshi Ueda 		return BLKPREP_OK;
1653d0bcb878SKiyoshi Ueda 
1654cec47e3dSKiyoshi Ueda 	if (unlikely(rq->special)) {
1655cec47e3dSKiyoshi Ueda 		DMWARN("Already has something in rq->special.");
1656cec47e3dSKiyoshi Ueda 		return BLKPREP_KILL;
1657cec47e3dSKiyoshi Ueda 	}
1658cec47e3dSKiyoshi Ueda 
16596facdaffSKiyoshi Ueda 	clone = clone_rq(rq, md, GFP_ATOMIC);
16606facdaffSKiyoshi Ueda 	if (!clone)
1661cec47e3dSKiyoshi Ueda 		return BLKPREP_DEFER;
1662cec47e3dSKiyoshi Ueda 
1663cec47e3dSKiyoshi Ueda 	rq->special = clone;
1664cec47e3dSKiyoshi Ueda 	rq->cmd_flags |= REQ_DONTPREP;
1665cec47e3dSKiyoshi Ueda 
1666cec47e3dSKiyoshi Ueda 	return BLKPREP_OK;
1667cec47e3dSKiyoshi Ueda }
1668cec47e3dSKiyoshi Ueda 
16699eef87daSKiyoshi Ueda /*
16709eef87daSKiyoshi Ueda  * Returns:
16719eef87daSKiyoshi Ueda  * 0  : the request has been processed (not requeued)
16729eef87daSKiyoshi Ueda  * !0 : the request has been requeued
16739eef87daSKiyoshi Ueda  */
16749eef87daSKiyoshi Ueda static int map_request(struct dm_target *ti, struct request *clone,
1675cec47e3dSKiyoshi Ueda 		       struct mapped_device *md)
1676cec47e3dSKiyoshi Ueda {
16779eef87daSKiyoshi Ueda 	int r, requeued = 0;
1678cec47e3dSKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
1679cec47e3dSKiyoshi Ueda 
1680cec47e3dSKiyoshi Ueda 	/*
1681cec47e3dSKiyoshi Ueda 	 * Hold the md reference here for the in-flight I/O.
1682cec47e3dSKiyoshi Ueda 	 * We can't rely on the reference count by device opener,
1683cec47e3dSKiyoshi Ueda 	 * because the device may be closed during the request completion
1684cec47e3dSKiyoshi Ueda 	 * when all bios are completed.
1685cec47e3dSKiyoshi Ueda 	 * See the comment in rq_completed() too.
1686cec47e3dSKiyoshi Ueda 	 */
1687cec47e3dSKiyoshi Ueda 	dm_get(md);
1688cec47e3dSKiyoshi Ueda 
1689cec47e3dSKiyoshi Ueda 	tio->ti = ti;
1690cec47e3dSKiyoshi Ueda 	r = ti->type->map_rq(ti, clone, &tio->info);
1691cec47e3dSKiyoshi Ueda 	switch (r) {
1692cec47e3dSKiyoshi Ueda 	case DM_MAPIO_SUBMITTED:
1693cec47e3dSKiyoshi Ueda 		/* The target has taken the I/O to submit by itself later */
1694cec47e3dSKiyoshi Ueda 		break;
1695cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REMAPPED:
1696cec47e3dSKiyoshi Ueda 		/* The target has remapped the I/O so dispatch it */
16976db4ccd6SJun'ichi Nomura 		trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
16986db4ccd6SJun'ichi Nomura 				     blk_rq_pos(tio->orig));
1699cec47e3dSKiyoshi Ueda 		dm_dispatch_request(clone);
1700cec47e3dSKiyoshi Ueda 		break;
1701cec47e3dSKiyoshi Ueda 	case DM_MAPIO_REQUEUE:
1702cec47e3dSKiyoshi Ueda 		/* The target wants to requeue the I/O */
1703cec47e3dSKiyoshi Ueda 		dm_requeue_unmapped_request(clone);
17049eef87daSKiyoshi Ueda 		requeued = 1;
1705cec47e3dSKiyoshi Ueda 		break;
1706cec47e3dSKiyoshi Ueda 	default:
1707cec47e3dSKiyoshi Ueda 		if (r > 0) {
1708cec47e3dSKiyoshi Ueda 			DMWARN("unimplemented target map return value: %d", r);
1709cec47e3dSKiyoshi Ueda 			BUG();
1710cec47e3dSKiyoshi Ueda 		}
1711cec47e3dSKiyoshi Ueda 
1712cec47e3dSKiyoshi Ueda 		/* The target wants to complete the I/O */
1713cec47e3dSKiyoshi Ueda 		dm_kill_unmapped_request(clone, r);
1714cec47e3dSKiyoshi Ueda 		break;
1715cec47e3dSKiyoshi Ueda 	}
17169eef87daSKiyoshi Ueda 
17179eef87daSKiyoshi Ueda 	return requeued;
1718cec47e3dSKiyoshi Ueda }
1719cec47e3dSKiyoshi Ueda 
1720cec47e3dSKiyoshi Ueda /*
1721cec47e3dSKiyoshi Ueda  * q->request_fn for request-based dm.
1722cec47e3dSKiyoshi Ueda  * Called with the queue lock held.
1723cec47e3dSKiyoshi Ueda  */
1724cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q)
1725cec47e3dSKiyoshi Ueda {
1726cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
17277c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1728cec47e3dSKiyoshi Ueda 	struct dm_target *ti;
1729b4324feeSKiyoshi Ueda 	struct request *rq, *clone;
1730cec47e3dSKiyoshi Ueda 
1731cec47e3dSKiyoshi Ueda 	/*
1732b4324feeSKiyoshi Ueda 	 * For suspend, check blk_queue_stopped() and increment
1733b4324feeSKiyoshi Ueda 	 * ->pending within a single queue_lock not to increment the
1734b4324feeSKiyoshi Ueda 	 * number of in-flight I/Os after the queue is stopped in
1735b4324feeSKiyoshi Ueda 	 * dm_suspend().
1736cec47e3dSKiyoshi Ueda 	 */
1737cec47e3dSKiyoshi Ueda 	while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
1738cec47e3dSKiyoshi Ueda 		rq = blk_peek_request(q);
1739cec47e3dSKiyoshi Ueda 		if (!rq)
1740cec47e3dSKiyoshi Ueda 			goto plug_and_out;
1741cec47e3dSKiyoshi Ueda 
1742d0bcb878SKiyoshi Ueda 		if (unlikely(dm_rq_is_flush_request(rq))) {
1743d0bcb878SKiyoshi Ueda 			BUG_ON(md->flush_request);
1744d0bcb878SKiyoshi Ueda 			md->flush_request = rq;
1745d0bcb878SKiyoshi Ueda 			blk_start_request(rq);
1746d0bcb878SKiyoshi Ueda 			queue_work(md->wq, &md->barrier_work);
1747d0bcb878SKiyoshi Ueda 			goto out;
1748d0bcb878SKiyoshi Ueda 		}
1749d0bcb878SKiyoshi Ueda 
1750cec47e3dSKiyoshi Ueda 		ti = dm_table_find_target(map, blk_rq_pos(rq));
1751cec47e3dSKiyoshi Ueda 		if (ti->type->busy && ti->type->busy(ti))
1752cec47e3dSKiyoshi Ueda 			goto plug_and_out;
1753cec47e3dSKiyoshi Ueda 
1754cec47e3dSKiyoshi Ueda 		blk_start_request(rq);
1755b4324feeSKiyoshi Ueda 		clone = rq->special;
1756b4324feeSKiyoshi Ueda 		atomic_inc(&md->pending[rq_data_dir(clone)]);
1757b4324feeSKiyoshi Ueda 
1758cec47e3dSKiyoshi Ueda 		spin_unlock(q->queue_lock);
17599eef87daSKiyoshi Ueda 		if (map_request(ti, clone, md))
17609eef87daSKiyoshi Ueda 			goto requeued;
17619eef87daSKiyoshi Ueda 
1762cec47e3dSKiyoshi Ueda 		spin_lock_irq(q->queue_lock);
1763cec47e3dSKiyoshi Ueda 	}
1764cec47e3dSKiyoshi Ueda 
1765cec47e3dSKiyoshi Ueda 	goto out;
1766cec47e3dSKiyoshi Ueda 
17679eef87daSKiyoshi Ueda requeued:
17689eef87daSKiyoshi Ueda 	spin_lock_irq(q->queue_lock);
17699eef87daSKiyoshi Ueda 
1770cec47e3dSKiyoshi Ueda plug_and_out:
1771cec47e3dSKiyoshi Ueda 	if (!elv_queue_empty(q))
1772cec47e3dSKiyoshi Ueda 		/* Some requests still remain, retry later */
1773cec47e3dSKiyoshi Ueda 		blk_plug_device(q);
1774cec47e3dSKiyoshi Ueda 
1775cec47e3dSKiyoshi Ueda out:
1776cec47e3dSKiyoshi Ueda 	dm_table_put(map);
1777cec47e3dSKiyoshi Ueda 
1778cec47e3dSKiyoshi Ueda 	return;
1779cec47e3dSKiyoshi Ueda }
1780cec47e3dSKiyoshi Ueda 
1781cec47e3dSKiyoshi Ueda int dm_underlying_device_busy(struct request_queue *q)
1782cec47e3dSKiyoshi Ueda {
1783cec47e3dSKiyoshi Ueda 	return blk_lld_busy(q);
1784cec47e3dSKiyoshi Ueda }
1785cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
1786cec47e3dSKiyoshi Ueda 
1787cec47e3dSKiyoshi Ueda static int dm_lld_busy(struct request_queue *q)
1788cec47e3dSKiyoshi Ueda {
1789cec47e3dSKiyoshi Ueda 	int r;
1790cec47e3dSKiyoshi Ueda 	struct mapped_device *md = q->queuedata;
17917c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
1792cec47e3dSKiyoshi Ueda 
1793cec47e3dSKiyoshi Ueda 	if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
1794cec47e3dSKiyoshi Ueda 		r = 1;
1795cec47e3dSKiyoshi Ueda 	else
1796cec47e3dSKiyoshi Ueda 		r = dm_table_any_busy_target(map);
1797cec47e3dSKiyoshi Ueda 
1798cec47e3dSKiyoshi Ueda 	dm_table_put(map);
1799cec47e3dSKiyoshi Ueda 
1800cec47e3dSKiyoshi Ueda 	return r;
1801cec47e3dSKiyoshi Ueda }
1802cec47e3dSKiyoshi Ueda 
1803165125e1SJens Axboe static void dm_unplug_all(struct request_queue *q)
18041da177e4SLinus Torvalds {
18051da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
18067c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
18071da177e4SLinus Torvalds 
18081da177e4SLinus Torvalds 	if (map) {
1809cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
1810cec47e3dSKiyoshi Ueda 			generic_unplug_device(q);
1811cec47e3dSKiyoshi Ueda 
18121da177e4SLinus Torvalds 		dm_table_unplug_all(map);
18131da177e4SLinus Torvalds 		dm_table_put(map);
18141da177e4SLinus Torvalds 	}
18151da177e4SLinus Torvalds }
18161da177e4SLinus Torvalds 
18171da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
18181da177e4SLinus Torvalds {
18198a57dfc6SChandra Seetharaman 	int r = bdi_bits;
18208a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
18218a57dfc6SChandra Seetharaman 	struct dm_table *map;
18221da177e4SLinus Torvalds 
18231eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
18247c666411SAlasdair G Kergon 		map = dm_get_live_table(md);
18258a57dfc6SChandra Seetharaman 		if (map) {
1826cec47e3dSKiyoshi Ueda 			/*
1827cec47e3dSKiyoshi Ueda 			 * Request-based dm cares about only own queue for
1828cec47e3dSKiyoshi Ueda 			 * the query about congestion status of request_queue
1829cec47e3dSKiyoshi Ueda 			 */
1830cec47e3dSKiyoshi Ueda 			if (dm_request_based(md))
1831cec47e3dSKiyoshi Ueda 				r = md->queue->backing_dev_info.state &
1832cec47e3dSKiyoshi Ueda 				    bdi_bits;
1833cec47e3dSKiyoshi Ueda 			else
18341da177e4SLinus Torvalds 				r = dm_table_any_congested(map, bdi_bits);
1835cec47e3dSKiyoshi Ueda 
18361da177e4SLinus Torvalds 			dm_table_put(map);
18378a57dfc6SChandra Seetharaman 		}
18388a57dfc6SChandra Seetharaman 	}
18398a57dfc6SChandra Seetharaman 
18401da177e4SLinus Torvalds 	return r;
18411da177e4SLinus Torvalds }
18421da177e4SLinus Torvalds 
18431da177e4SLinus Torvalds /*-----------------------------------------------------------------
18441da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
18451da177e4SLinus Torvalds  *---------------------------------------------------------------*/
18461da177e4SLinus Torvalds static DEFINE_IDR(_minor_idr);
18471da177e4SLinus Torvalds 
18482b06cfffSAlasdair G Kergon static void free_minor(int minor)
18491da177e4SLinus Torvalds {
1850f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
18511da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1852f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
18531da177e4SLinus Torvalds }
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds /*
18561da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
18571da177e4SLinus Torvalds  */
1858cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
18591da177e4SLinus Torvalds {
18601da177e4SLinus Torvalds 	int r, m;
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
18631da177e4SLinus Torvalds 		return -EINVAL;
18641da177e4SLinus Torvalds 
186562f75c2fSJeff Mahoney 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
186662f75c2fSJeff Mahoney 	if (!r)
186762f75c2fSJeff Mahoney 		return -ENOMEM;
186862f75c2fSJeff Mahoney 
1869f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
18701da177e4SLinus Torvalds 
18711da177e4SLinus Torvalds 	if (idr_find(&_minor_idr, minor)) {
18721da177e4SLinus Torvalds 		r = -EBUSY;
18731da177e4SLinus Torvalds 		goto out;
18741da177e4SLinus Torvalds 	}
18751da177e4SLinus Torvalds 
1876ba61fdd1SJeff Mahoney 	r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
187762f75c2fSJeff Mahoney 	if (r)
18781da177e4SLinus Torvalds 		goto out;
18791da177e4SLinus Torvalds 
18801da177e4SLinus Torvalds 	if (m != minor) {
18811da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
18821da177e4SLinus Torvalds 		r = -EBUSY;
18831da177e4SLinus Torvalds 		goto out;
18841da177e4SLinus Torvalds 	}
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds out:
1887f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
18881da177e4SLinus Torvalds 	return r;
18891da177e4SLinus Torvalds }
18901da177e4SLinus Torvalds 
1891cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
18921da177e4SLinus Torvalds {
18932b06cfffSAlasdair G Kergon 	int r, m;
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
189662f75c2fSJeff Mahoney 	if (!r)
189762f75c2fSJeff Mahoney 		return -ENOMEM;
189862f75c2fSJeff Mahoney 
1899f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
19001da177e4SLinus Torvalds 
1901ba61fdd1SJeff Mahoney 	r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
1902cf13ab8eSFrederik Deweerdt 	if (r)
19031da177e4SLinus Torvalds 		goto out;
19041da177e4SLinus Torvalds 
19051da177e4SLinus Torvalds 	if (m >= (1 << MINORBITS)) {
19061da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
19071da177e4SLinus Torvalds 		r = -ENOSPC;
19081da177e4SLinus Torvalds 		goto out;
19091da177e4SLinus Torvalds 	}
19101da177e4SLinus Torvalds 
19111da177e4SLinus Torvalds 	*minor = m;
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds out:
1914f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
19151da177e4SLinus Torvalds 	return r;
19161da177e4SLinus Torvalds }
19171da177e4SLinus Torvalds 
191883d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops;
19191da177e4SLinus Torvalds 
192053d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
1921d0bcb878SKiyoshi Ueda static void dm_rq_barrier_work(struct work_struct *work);
192253d5914fSMikulas Patocka 
19234a0b4ddfSMike Snitzer static void dm_init_md_queue(struct mapped_device *md)
19244a0b4ddfSMike Snitzer {
19254a0b4ddfSMike Snitzer 	/*
19264a0b4ddfSMike Snitzer 	 * Request-based dm devices cannot be stacked on top of bio-based dm
19274a0b4ddfSMike Snitzer 	 * devices.  The type of this dm device has not been decided yet.
19284a0b4ddfSMike Snitzer 	 * The type is decided at the first table loading time.
19294a0b4ddfSMike Snitzer 	 * To prevent problematic device stacking, clear the queue flag
19304a0b4ddfSMike Snitzer 	 * for request stacking support until then.
19314a0b4ddfSMike Snitzer 	 *
19324a0b4ddfSMike Snitzer 	 * This queue is new, so no concurrency on the queue_flags.
19334a0b4ddfSMike Snitzer 	 */
19344a0b4ddfSMike Snitzer 	queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
19354a0b4ddfSMike Snitzer 
19364a0b4ddfSMike Snitzer 	md->queue->queuedata = md;
19374a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
19384a0b4ddfSMike Snitzer 	md->queue->backing_dev_info.congested_data = md;
19394a0b4ddfSMike Snitzer 	blk_queue_make_request(md->queue, dm_request);
19404a0b4ddfSMike Snitzer 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
19414a0b4ddfSMike Snitzer 	md->queue->unplug_fn = dm_unplug_all;
19424a0b4ddfSMike Snitzer 	blk_queue_merge_bvec(md->queue, dm_merge_bvec);
19434a0b4ddfSMike Snitzer }
19444a0b4ddfSMike Snitzer 
19451da177e4SLinus Torvalds /*
19461da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
19471da177e4SLinus Torvalds  */
19482b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
19491da177e4SLinus Torvalds {
19501da177e4SLinus Torvalds 	int r;
1951cf13ab8eSFrederik Deweerdt 	struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1952ba61fdd1SJeff Mahoney 	void *old_md;
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 	if (!md) {
19551da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
19561da177e4SLinus Torvalds 		return NULL;
19571da177e4SLinus Torvalds 	}
19581da177e4SLinus Torvalds 
195910da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
19606ed7ade8SMilan Broz 		goto bad_module_get;
196110da4f79SJeff Mahoney 
19621da177e4SLinus Torvalds 	/* get a minor number for the dev */
19632b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1964cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
19652b06cfffSAlasdair G Kergon 	else
1966cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
19671da177e4SLinus Torvalds 	if (r < 0)
19686ed7ade8SMilan Broz 		goto bad_minor;
19691da177e4SLinus Torvalds 
1970a5664dadSMike Snitzer 	md->type = DM_TYPE_NONE;
19712ca3310eSAlasdair G Kergon 	init_rwsem(&md->io_lock);
1972e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1973a5664dadSMike Snitzer 	mutex_init(&md->type_lock);
1974022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
1975d0bcb878SKiyoshi Ueda 	spin_lock_init(&md->barrier_error_lock);
19761da177e4SLinus Torvalds 	rwlock_init(&md->map_lock);
19771da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
19785c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
19791da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
19807a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
19817a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
19827a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
19831da177e4SLinus Torvalds 
19844a0b4ddfSMike Snitzer 	md->queue = blk_alloc_queue(GFP_KERNEL);
19851da177e4SLinus Torvalds 	if (!md->queue)
19866ed7ade8SMilan Broz 		goto bad_queue;
19871da177e4SLinus Torvalds 
19884a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
19899faf400fSStefan Bader 
19901da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
19911da177e4SLinus Torvalds 	if (!md->disk)
19926ed7ade8SMilan Broz 		goto bad_disk;
19931da177e4SLinus Torvalds 
1994316d315bSNikanth Karthikesan 	atomic_set(&md->pending[0], 0);
1995316d315bSNikanth Karthikesan 	atomic_set(&md->pending[1], 0);
1996f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
199753d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1998d0bcb878SKiyoshi Ueda 	INIT_WORK(&md->barrier_work, dm_rq_barrier_work);
1999f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
2000f0b04115SJeff Mahoney 
20011da177e4SLinus Torvalds 	md->disk->major = _major;
20021da177e4SLinus Torvalds 	md->disk->first_minor = minor;
20031da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
20041da177e4SLinus Torvalds 	md->disk->queue = md->queue;
20051da177e4SLinus Torvalds 	md->disk->private_data = md;
20061da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
20071da177e4SLinus Torvalds 	add_disk(md->disk);
20087e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
20091da177e4SLinus Torvalds 
2010304f3f6aSMilan Broz 	md->wq = create_singlethread_workqueue("kdmflush");
2011304f3f6aSMilan Broz 	if (!md->wq)
2012304f3f6aSMilan Broz 		goto bad_thread;
2013304f3f6aSMilan Broz 
201432a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
201532a926daSMikulas Patocka 	if (!md->bdev)
201632a926daSMikulas Patocka 		goto bad_bdev;
201732a926daSMikulas Patocka 
2018ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
2019f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
2020ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
2021f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
2022ba61fdd1SJeff Mahoney 
2023ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
2024ba61fdd1SJeff Mahoney 
20251da177e4SLinus Torvalds 	return md;
20261da177e4SLinus Torvalds 
202732a926daSMikulas Patocka bad_bdev:
202832a926daSMikulas Patocka 	destroy_workqueue(md->wq);
2029304f3f6aSMilan Broz bad_thread:
203003022c54SZdenek Kabelac 	del_gendisk(md->disk);
2031304f3f6aSMilan Broz 	put_disk(md->disk);
20326ed7ade8SMilan Broz bad_disk:
20331312f40eSAl Viro 	blk_cleanup_queue(md->queue);
20346ed7ade8SMilan Broz bad_queue:
20351da177e4SLinus Torvalds 	free_minor(minor);
20366ed7ade8SMilan Broz bad_minor:
203710da4f79SJeff Mahoney 	module_put(THIS_MODULE);
20386ed7ade8SMilan Broz bad_module_get:
20391da177e4SLinus Torvalds 	kfree(md);
20401da177e4SLinus Torvalds 	return NULL;
20411da177e4SLinus Torvalds }
20421da177e4SLinus Torvalds 
2043ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
2044ae9da83fSJun'ichi Nomura 
20451da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
20461da177e4SLinus Torvalds {
2047f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
204863d94e48SJun'ichi Nomura 
2049ae9da83fSJun'ichi Nomura 	unlock_fs(md);
2050db8fef4fSMikulas Patocka 	bdput(md->bdev);
2051304f3f6aSMilan Broz 	destroy_workqueue(md->wq);
2052e6ee8c0bSKiyoshi Ueda 	if (md->tio_pool)
20531da177e4SLinus Torvalds 		mempool_destroy(md->tio_pool);
2054e6ee8c0bSKiyoshi Ueda 	if (md->io_pool)
20551da177e4SLinus Torvalds 		mempool_destroy(md->io_pool);
2056e6ee8c0bSKiyoshi Ueda 	if (md->bs)
20579faf400fSStefan Bader 		bioset_free(md->bs);
20589c47008dSMartin K. Petersen 	blk_integrity_unregister(md->disk);
20591da177e4SLinus Torvalds 	del_gendisk(md->disk);
206063d94e48SJun'ichi Nomura 	free_minor(minor);
2061fba9f90eSJeff Mahoney 
2062fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
2063fba9f90eSJeff Mahoney 	md->disk->private_data = NULL;
2064fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
2065fba9f90eSJeff Mahoney 
20661da177e4SLinus Torvalds 	put_disk(md->disk);
20671312f40eSAl Viro 	blk_cleanup_queue(md->queue);
206810da4f79SJeff Mahoney 	module_put(THIS_MODULE);
20691da177e4SLinus Torvalds 	kfree(md);
20701da177e4SLinus Torvalds }
20711da177e4SLinus Torvalds 
2072e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
2073e6ee8c0bSKiyoshi Ueda {
2074e6ee8c0bSKiyoshi Ueda 	struct dm_md_mempools *p;
2075e6ee8c0bSKiyoshi Ueda 
2076e6ee8c0bSKiyoshi Ueda 	if (md->io_pool && md->tio_pool && md->bs)
2077e6ee8c0bSKiyoshi Ueda 		/* the md already has necessary mempools */
2078e6ee8c0bSKiyoshi Ueda 		goto out;
2079e6ee8c0bSKiyoshi Ueda 
2080e6ee8c0bSKiyoshi Ueda 	p = dm_table_get_md_mempools(t);
2081e6ee8c0bSKiyoshi Ueda 	BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
2082e6ee8c0bSKiyoshi Ueda 
2083e6ee8c0bSKiyoshi Ueda 	md->io_pool = p->io_pool;
2084e6ee8c0bSKiyoshi Ueda 	p->io_pool = NULL;
2085e6ee8c0bSKiyoshi Ueda 	md->tio_pool = p->tio_pool;
2086e6ee8c0bSKiyoshi Ueda 	p->tio_pool = NULL;
2087e6ee8c0bSKiyoshi Ueda 	md->bs = p->bs;
2088e6ee8c0bSKiyoshi Ueda 	p->bs = NULL;
2089e6ee8c0bSKiyoshi Ueda 
2090e6ee8c0bSKiyoshi Ueda out:
2091e6ee8c0bSKiyoshi Ueda 	/* mempool bind completed, now no need any mempools in the table */
2092e6ee8c0bSKiyoshi Ueda 	dm_table_free_md_mempools(t);
2093e6ee8c0bSKiyoshi Ueda }
2094e6ee8c0bSKiyoshi Ueda 
20951da177e4SLinus Torvalds /*
20961da177e4SLinus Torvalds  * Bind a table to the device.
20971da177e4SLinus Torvalds  */
20981da177e4SLinus Torvalds static void event_callback(void *context)
20991da177e4SLinus Torvalds {
21007a8c3d3bSMike Anderson 	unsigned long flags;
21017a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
21021da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
21031da177e4SLinus Torvalds 
21047a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
21057a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
21067a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
21077a8c3d3bSMike Anderson 
2108ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
21097a8c3d3bSMike Anderson 
21101da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
21111da177e4SLinus Torvalds 	wake_up(&md->eventq);
21121da177e4SLinus Torvalds }
21131da177e4SLinus Torvalds 
21144e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
21151da177e4SLinus Torvalds {
21164e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
21171da177e4SLinus Torvalds 
2118db8fef4fSMikulas Patocka 	mutex_lock(&md->bdev->bd_inode->i_mutex);
2119db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
2120db8fef4fSMikulas Patocka 	mutex_unlock(&md->bdev->bd_inode->i_mutex);
21211da177e4SLinus Torvalds }
21221da177e4SLinus Torvalds 
2123042d2a9bSAlasdair G Kergon /*
2124042d2a9bSAlasdair G Kergon  * Returns old map, which caller must destroy.
2125042d2a9bSAlasdair G Kergon  */
2126042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
2127754c5fc7SMike Snitzer 			       struct queue_limits *limits)
21281da177e4SLinus Torvalds {
2129042d2a9bSAlasdair G Kergon 	struct dm_table *old_map;
2130165125e1SJens Axboe 	struct request_queue *q = md->queue;
21311da177e4SLinus Torvalds 	sector_t size;
2132523d9297SKiyoshi Ueda 	unsigned long flags;
21331da177e4SLinus Torvalds 
21341da177e4SLinus Torvalds 	size = dm_table_get_size(t);
21353ac51e74SDarrick J. Wong 
21363ac51e74SDarrick J. Wong 	/*
21373ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
21383ac51e74SDarrick J. Wong 	 */
21393ac51e74SDarrick J. Wong 	if (size != get_capacity(md->disk))
21403ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
21413ac51e74SDarrick J. Wong 
21424e90188bSAlasdair G Kergon 	__set_size(md, size);
21431da177e4SLinus Torvalds 
2144cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
21452ca3310eSAlasdair G Kergon 
2146e6ee8c0bSKiyoshi Ueda 	/*
2147e6ee8c0bSKiyoshi Ueda 	 * The queue hasn't been stopped yet, if the old table type wasn't
2148e6ee8c0bSKiyoshi Ueda 	 * for request-based during suspension.  So stop it to prevent
2149e6ee8c0bSKiyoshi Ueda 	 * I/O mapping before resume.
2150e6ee8c0bSKiyoshi Ueda 	 * This must be done before setting the queue restrictions,
2151e6ee8c0bSKiyoshi Ueda 	 * because request-based dm may be run just after the setting.
2152e6ee8c0bSKiyoshi Ueda 	 */
2153e6ee8c0bSKiyoshi Ueda 	if (dm_table_request_based(t) && !blk_queue_stopped(q))
2154e6ee8c0bSKiyoshi Ueda 		stop_queue(q);
2155e6ee8c0bSKiyoshi Ueda 
2156e6ee8c0bSKiyoshi Ueda 	__bind_mempools(md, t);
2157e6ee8c0bSKiyoshi Ueda 
2158523d9297SKiyoshi Ueda 	write_lock_irqsave(&md->map_lock, flags);
2159042d2a9bSAlasdair G Kergon 	old_map = md->map;
21602ca3310eSAlasdair G Kergon 	md->map = t;
2161754c5fc7SMike Snitzer 	dm_table_set_restrictions(t, q, limits);
2162523d9297SKiyoshi Ueda 	write_unlock_irqrestore(&md->map_lock, flags);
21632ca3310eSAlasdair G Kergon 
2164042d2a9bSAlasdair G Kergon 	return old_map;
21651da177e4SLinus Torvalds }
21661da177e4SLinus Torvalds 
2167a7940155SAlasdair G Kergon /*
2168a7940155SAlasdair G Kergon  * Returns unbound table for the caller to free.
2169a7940155SAlasdair G Kergon  */
2170a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md)
21711da177e4SLinus Torvalds {
21721da177e4SLinus Torvalds 	struct dm_table *map = md->map;
2173523d9297SKiyoshi Ueda 	unsigned long flags;
21741da177e4SLinus Torvalds 
21751da177e4SLinus Torvalds 	if (!map)
2176a7940155SAlasdair G Kergon 		return NULL;
21771da177e4SLinus Torvalds 
21781da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
2179523d9297SKiyoshi Ueda 	write_lock_irqsave(&md->map_lock, flags);
21801da177e4SLinus Torvalds 	md->map = NULL;
2181523d9297SKiyoshi Ueda 	write_unlock_irqrestore(&md->map_lock, flags);
2182a7940155SAlasdair G Kergon 
2183a7940155SAlasdair G Kergon 	return map;
21841da177e4SLinus Torvalds }
21851da177e4SLinus Torvalds 
21861da177e4SLinus Torvalds /*
21871da177e4SLinus Torvalds  * Constructor for a new device.
21881da177e4SLinus Torvalds  */
21892b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
21901da177e4SLinus Torvalds {
21911da177e4SLinus Torvalds 	struct mapped_device *md;
21921da177e4SLinus Torvalds 
21932b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
21941da177e4SLinus Torvalds 	if (!md)
21951da177e4SLinus Torvalds 		return -ENXIO;
21961da177e4SLinus Torvalds 
2197784aae73SMilan Broz 	dm_sysfs_init(md);
2198784aae73SMilan Broz 
21991da177e4SLinus Torvalds 	*result = md;
22001da177e4SLinus Torvalds 	return 0;
22011da177e4SLinus Torvalds }
22021da177e4SLinus Torvalds 
2203a5664dadSMike Snitzer /*
2204a5664dadSMike Snitzer  * Functions to manage md->type.
2205a5664dadSMike Snitzer  * All are required to hold md->type_lock.
2206a5664dadSMike Snitzer  */
2207a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md)
2208a5664dadSMike Snitzer {
2209a5664dadSMike Snitzer 	mutex_lock(&md->type_lock);
2210a5664dadSMike Snitzer }
2211a5664dadSMike Snitzer 
2212a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md)
2213a5664dadSMike Snitzer {
2214a5664dadSMike Snitzer 	mutex_unlock(&md->type_lock);
2215a5664dadSMike Snitzer }
2216a5664dadSMike Snitzer 
2217a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type)
2218a5664dadSMike Snitzer {
2219a5664dadSMike Snitzer 	md->type = type;
2220a5664dadSMike Snitzer }
2221a5664dadSMike Snitzer 
2222a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md)
2223a5664dadSMike Snitzer {
2224a5664dadSMike Snitzer 	return md->type;
2225a5664dadSMike Snitzer }
2226a5664dadSMike Snitzer 
22274a0b4ddfSMike Snitzer /*
22284a0b4ddfSMike Snitzer  * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
22294a0b4ddfSMike Snitzer  */
22304a0b4ddfSMike Snitzer static int dm_init_request_based_queue(struct mapped_device *md)
22314a0b4ddfSMike Snitzer {
22324a0b4ddfSMike Snitzer 	struct request_queue *q = NULL;
22334a0b4ddfSMike Snitzer 
22344a0b4ddfSMike Snitzer 	if (md->queue->elevator)
22354a0b4ddfSMike Snitzer 		return 1;
22364a0b4ddfSMike Snitzer 
22374a0b4ddfSMike Snitzer 	/* Fully initialize the queue */
22384a0b4ddfSMike Snitzer 	q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
22394a0b4ddfSMike Snitzer 	if (!q)
22404a0b4ddfSMike Snitzer 		return 0;
22414a0b4ddfSMike Snitzer 
22424a0b4ddfSMike Snitzer 	md->queue = q;
22434a0b4ddfSMike Snitzer 	md->saved_make_request_fn = md->queue->make_request_fn;
22444a0b4ddfSMike Snitzer 	dm_init_md_queue(md);
22454a0b4ddfSMike Snitzer 	blk_queue_softirq_done(md->queue, dm_softirq_done);
22464a0b4ddfSMike Snitzer 	blk_queue_prep_rq(md->queue, dm_prep_fn);
22474a0b4ddfSMike Snitzer 	blk_queue_lld_busy(md->queue, dm_lld_busy);
22484a0b4ddfSMike Snitzer 	blk_queue_ordered(md->queue, QUEUE_ORDERED_DRAIN_FLUSH);
22494a0b4ddfSMike Snitzer 
22504a0b4ddfSMike Snitzer 	elv_register_queue(md->queue);
22514a0b4ddfSMike Snitzer 
22524a0b4ddfSMike Snitzer 	return 1;
22534a0b4ddfSMike Snitzer }
22544a0b4ddfSMike Snitzer 
22554a0b4ddfSMike Snitzer /*
22564a0b4ddfSMike Snitzer  * Setup the DM device's queue based on md's type
22574a0b4ddfSMike Snitzer  */
22584a0b4ddfSMike Snitzer int dm_setup_md_queue(struct mapped_device *md)
22594a0b4ddfSMike Snitzer {
22604a0b4ddfSMike Snitzer 	if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
22614a0b4ddfSMike Snitzer 	    !dm_init_request_based_queue(md)) {
22624a0b4ddfSMike Snitzer 		DMWARN("Cannot initialize queue for request-based mapped device");
22634a0b4ddfSMike Snitzer 		return -EINVAL;
22644a0b4ddfSMike Snitzer 	}
22654a0b4ddfSMike Snitzer 
22664a0b4ddfSMike Snitzer 	return 0;
22674a0b4ddfSMike Snitzer }
22684a0b4ddfSMike Snitzer 
2269637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev)
22701da177e4SLinus Torvalds {
22711da177e4SLinus Torvalds 	struct mapped_device *md;
22721da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
22731da177e4SLinus Torvalds 
22741da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
22751da177e4SLinus Torvalds 		return NULL;
22761da177e4SLinus Torvalds 
2277f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
22781da177e4SLinus Torvalds 
22791da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
2280fba9f90eSJeff Mahoney 	if (md && (md == MINOR_ALLOCED ||
2281f331c029STejun Heo 		   (MINOR(disk_devt(dm_disk(md))) != minor) ||
2282abdc568bSKiyoshi Ueda 		   dm_deleting_md(md) ||
2283fba9f90eSJeff Mahoney 		   test_bit(DMF_FREEING, &md->flags))) {
2284637842cfSDavid Teigland 		md = NULL;
2285fba9f90eSJeff Mahoney 		goto out;
2286fba9f90eSJeff Mahoney 	}
22871da177e4SLinus Torvalds 
2288fba9f90eSJeff Mahoney out:
2289f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
22901da177e4SLinus Torvalds 
2291637842cfSDavid Teigland 	return md;
2292637842cfSDavid Teigland }
2293637842cfSDavid Teigland 
2294d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev)
2295d229a958SDavid Teigland {
2296d229a958SDavid Teigland 	struct mapped_device *md = dm_find_md(dev);
2297d229a958SDavid Teigland 
2298d229a958SDavid Teigland 	if (md)
2299d229a958SDavid Teigland 		dm_get(md);
2300d229a958SDavid Teigland 
2301d229a958SDavid Teigland 	return md;
2302d229a958SDavid Teigland }
2303d229a958SDavid Teigland 
23049ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
2305637842cfSDavid Teigland {
23069ade92a9SAlasdair G Kergon 	return md->interface_ptr;
23071da177e4SLinus Torvalds }
23081da177e4SLinus Torvalds 
23091da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
23101da177e4SLinus Torvalds {
23111da177e4SLinus Torvalds 	md->interface_ptr = ptr;
23121da177e4SLinus Torvalds }
23131da177e4SLinus Torvalds 
23141da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
23151da177e4SLinus Torvalds {
23161da177e4SLinus Torvalds 	atomic_inc(&md->holders);
23173f77316dSKiyoshi Ueda 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
23181da177e4SLinus Torvalds }
23191da177e4SLinus Torvalds 
232072d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
232172d94861SAlasdair G Kergon {
232272d94861SAlasdair G Kergon 	return md->name;
232372d94861SAlasdair G Kergon }
232472d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
232572d94861SAlasdair G Kergon 
23263f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait)
23271da177e4SLinus Torvalds {
23281134e5aeSMike Anderson 	struct dm_table *map;
23291da177e4SLinus Torvalds 
23303f77316dSKiyoshi Ueda 	might_sleep();
2331fba9f90eSJeff Mahoney 
23323f77316dSKiyoshi Ueda 	spin_lock(&_minor_lock);
23337c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
23343f77316dSKiyoshi Ueda 	idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
2335fba9f90eSJeff Mahoney 	set_bit(DMF_FREEING, &md->flags);
2336f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
23373f77316dSKiyoshi Ueda 
23384f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md)) {
23391da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
23401da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
23411da177e4SLinus Torvalds 	}
23423f77316dSKiyoshi Ueda 
23433f77316dSKiyoshi Ueda 	/*
23443f77316dSKiyoshi Ueda 	 * Rare, but there may be I/O requests still going to complete,
23453f77316dSKiyoshi Ueda 	 * for example.  Wait for all references to disappear.
23463f77316dSKiyoshi Ueda 	 * No one should increment the reference count of the mapped_device,
23473f77316dSKiyoshi Ueda 	 * after the mapped_device state becomes DMF_FREEING.
23483f77316dSKiyoshi Ueda 	 */
23493f77316dSKiyoshi Ueda 	if (wait)
23503f77316dSKiyoshi Ueda 		while (atomic_read(&md->holders))
23513f77316dSKiyoshi Ueda 			msleep(1);
23523f77316dSKiyoshi Ueda 	else if (atomic_read(&md->holders))
23533f77316dSKiyoshi Ueda 		DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
23543f77316dSKiyoshi Ueda 		       dm_device_name(md), atomic_read(&md->holders));
23553f77316dSKiyoshi Ueda 
2356784aae73SMilan Broz 	dm_sysfs_exit(md);
23571134e5aeSMike Anderson 	dm_table_put(map);
2358a7940155SAlasdair G Kergon 	dm_table_destroy(__unbind(md));
23591da177e4SLinus Torvalds 	free_dev(md);
23601da177e4SLinus Torvalds }
23613f77316dSKiyoshi Ueda 
23623f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md)
23633f77316dSKiyoshi Ueda {
23643f77316dSKiyoshi Ueda 	__dm_destroy(md, true);
23653f77316dSKiyoshi Ueda }
23663f77316dSKiyoshi Ueda 
23673f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md)
23683f77316dSKiyoshi Ueda {
23693f77316dSKiyoshi Ueda 	__dm_destroy(md, false);
23703f77316dSKiyoshi Ueda }
23713f77316dSKiyoshi Ueda 
23723f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md)
23733f77316dSKiyoshi Ueda {
23743f77316dSKiyoshi Ueda 	atomic_dec(&md->holders);
23751da177e4SLinus Torvalds }
237679eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
23771da177e4SLinus Torvalds 
2378401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
237946125c1cSMilan Broz {
238046125c1cSMilan Broz 	int r = 0;
2381b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
2382b44ebeb0SMikulas Patocka 
2383b44ebeb0SMikulas Patocka 	dm_unplug_all(md->queue);
2384b44ebeb0SMikulas Patocka 
2385b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
238646125c1cSMilan Broz 
238746125c1cSMilan Broz 	while (1) {
2388401600dfSMikulas Patocka 		set_current_state(interruptible);
238946125c1cSMilan Broz 
239046125c1cSMilan Broz 		smp_mb();
2391b4324feeSKiyoshi Ueda 		if (!md_in_flight(md))
239246125c1cSMilan Broz 			break;
239346125c1cSMilan Broz 
2394401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
2395401600dfSMikulas Patocka 		    signal_pending(current)) {
239646125c1cSMilan Broz 			r = -EINTR;
239746125c1cSMilan Broz 			break;
239846125c1cSMilan Broz 		}
239946125c1cSMilan Broz 
240046125c1cSMilan Broz 		io_schedule();
240146125c1cSMilan Broz 	}
240246125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
240346125c1cSMilan Broz 
2404b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
2405b44ebeb0SMikulas Patocka 
240646125c1cSMilan Broz 	return r;
240746125c1cSMilan Broz }
240846125c1cSMilan Broz 
2409531fe963SMikulas Patocka static void dm_flush(struct mapped_device *md)
2410af7e466aSMikulas Patocka {
2411af7e466aSMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
241252b1fd5aSMikulas Patocka 
241352b1fd5aSMikulas Patocka 	bio_init(&md->barrier_bio);
241452b1fd5aSMikulas Patocka 	md->barrier_bio.bi_bdev = md->bdev;
241552b1fd5aSMikulas Patocka 	md->barrier_bio.bi_rw = WRITE_BARRIER;
241652b1fd5aSMikulas Patocka 	__split_and_process_bio(md, &md->barrier_bio);
241752b1fd5aSMikulas Patocka 
241852b1fd5aSMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2419af7e466aSMikulas Patocka }
2420af7e466aSMikulas Patocka 
2421af7e466aSMikulas Patocka static void process_barrier(struct mapped_device *md, struct bio *bio)
2422af7e466aSMikulas Patocka {
24235aa2781dSMikulas Patocka 	md->barrier_error = 0;
24245aa2781dSMikulas Patocka 
2425531fe963SMikulas Patocka 	dm_flush(md);
2426af7e466aSMikulas Patocka 
24275aa2781dSMikulas Patocka 	if (!bio_empty_barrier(bio)) {
2428af7e466aSMikulas Patocka 		__split_and_process_bio(md, bio);
2429708e9295SMikulas Patocka 		/*
2430708e9295SMikulas Patocka 		 * If the request isn't supported, don't waste time with
2431708e9295SMikulas Patocka 		 * the second flush.
2432708e9295SMikulas Patocka 		 */
2433708e9295SMikulas Patocka 		if (md->barrier_error != -EOPNOTSUPP)
2434531fe963SMikulas Patocka 			dm_flush(md);
24355aa2781dSMikulas Patocka 	}
2436af7e466aSMikulas Patocka 
2437af7e466aSMikulas Patocka 	if (md->barrier_error != DM_ENDIO_REQUEUE)
2438531fe963SMikulas Patocka 		bio_endio(bio, md->barrier_error);
24392761e95fSMikulas Patocka 	else {
24402761e95fSMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
24412761e95fSMikulas Patocka 		bio_list_add_head(&md->deferred, bio);
24422761e95fSMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
24432761e95fSMikulas Patocka 	}
2444af7e466aSMikulas Patocka }
2445af7e466aSMikulas Patocka 
24461da177e4SLinus Torvalds /*
24471da177e4SLinus Torvalds  * Process the deferred bios
24481da177e4SLinus Torvalds  */
2449ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
24501da177e4SLinus Torvalds {
2451ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
2452ef208587SMikulas Patocka 						work);
24536d6f10dfSMilan Broz 	struct bio *c;
24541da177e4SLinus Torvalds 
2455ef208587SMikulas Patocka 	down_write(&md->io_lock);
2456ef208587SMikulas Patocka 
24573b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
2458022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
2459022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
2460022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
2461022c2611SMikulas Patocka 
2462df12ee99SAlasdair G Kergon 		if (!c) {
24631eb787ecSAlasdair G Kergon 			clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
2464df12ee99SAlasdair G Kergon 			break;
2465022c2611SMikulas Patocka 		}
246673d410c0SMilan Broz 
24673b00b203SMikulas Patocka 		up_write(&md->io_lock);
24683b00b203SMikulas Patocka 
2469e6ee8c0bSKiyoshi Ueda 		if (dm_request_based(md))
2470e6ee8c0bSKiyoshi Ueda 			generic_make_request(c);
2471e6ee8c0bSKiyoshi Ueda 		else {
24727b6d91daSChristoph Hellwig 			if (c->bi_rw & REQ_HARDBARRIER)
2473af7e466aSMikulas Patocka 				process_barrier(md, c);
2474af7e466aSMikulas Patocka 			else
2475df12ee99SAlasdair G Kergon 				__split_and_process_bio(md, c);
2476e6ee8c0bSKiyoshi Ueda 		}
24773b00b203SMikulas Patocka 
24783b00b203SMikulas Patocka 		down_write(&md->io_lock);
2479df12ee99SAlasdair G Kergon 	}
2480ef208587SMikulas Patocka 
2481ef208587SMikulas Patocka 	up_write(&md->io_lock);
24821da177e4SLinus Torvalds }
24831da177e4SLinus Torvalds 
24849a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
2485304f3f6aSMilan Broz {
24863b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
24873b00b203SMikulas Patocka 	smp_mb__after_clear_bit();
248853d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
2489304f3f6aSMilan Broz }
2490304f3f6aSMilan Broz 
249157cba5d3SMike Snitzer static void dm_rq_set_target_request_nr(struct request *clone, unsigned request_nr)
2492d0bcb878SKiyoshi Ueda {
2493d0bcb878SKiyoshi Ueda 	struct dm_rq_target_io *tio = clone->end_io_data;
2494d0bcb878SKiyoshi Ueda 
249557cba5d3SMike Snitzer 	tio->info.target_request_nr = request_nr;
2496d0bcb878SKiyoshi Ueda }
2497d0bcb878SKiyoshi Ueda 
2498d0bcb878SKiyoshi Ueda /* Issue barrier requests to targets and wait for their completion. */
2499d0bcb878SKiyoshi Ueda static int dm_rq_barrier(struct mapped_device *md)
2500d0bcb878SKiyoshi Ueda {
2501d0bcb878SKiyoshi Ueda 	int i, j;
25027c666411SAlasdair G Kergon 	struct dm_table *map = dm_get_live_table(md);
2503d0bcb878SKiyoshi Ueda 	unsigned num_targets = dm_table_get_num_targets(map);
2504d0bcb878SKiyoshi Ueda 	struct dm_target *ti;
2505d0bcb878SKiyoshi Ueda 	struct request *clone;
2506d0bcb878SKiyoshi Ueda 
2507d0bcb878SKiyoshi Ueda 	md->barrier_error = 0;
2508d0bcb878SKiyoshi Ueda 
2509d0bcb878SKiyoshi Ueda 	for (i = 0; i < num_targets; i++) {
2510d0bcb878SKiyoshi Ueda 		ti = dm_table_get_target(map, i);
2511d0bcb878SKiyoshi Ueda 		for (j = 0; j < ti->num_flush_requests; j++) {
2512d0bcb878SKiyoshi Ueda 			clone = clone_rq(md->flush_request, md, GFP_NOIO);
251357cba5d3SMike Snitzer 			dm_rq_set_target_request_nr(clone, j);
2514d0bcb878SKiyoshi Ueda 			atomic_inc(&md->pending[rq_data_dir(clone)]);
2515d0bcb878SKiyoshi Ueda 			map_request(ti, clone, md);
2516d0bcb878SKiyoshi Ueda 		}
2517d0bcb878SKiyoshi Ueda 	}
2518d0bcb878SKiyoshi Ueda 
2519d0bcb878SKiyoshi Ueda 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
2520d0bcb878SKiyoshi Ueda 	dm_table_put(map);
2521d0bcb878SKiyoshi Ueda 
2522d0bcb878SKiyoshi Ueda 	return md->barrier_error;
2523d0bcb878SKiyoshi Ueda }
2524d0bcb878SKiyoshi Ueda 
2525d0bcb878SKiyoshi Ueda static void dm_rq_barrier_work(struct work_struct *work)
2526d0bcb878SKiyoshi Ueda {
2527d0bcb878SKiyoshi Ueda 	int error;
2528d0bcb878SKiyoshi Ueda 	struct mapped_device *md = container_of(work, struct mapped_device,
2529d0bcb878SKiyoshi Ueda 						barrier_work);
2530d0bcb878SKiyoshi Ueda 	struct request_queue *q = md->queue;
2531d0bcb878SKiyoshi Ueda 	struct request *rq;
2532d0bcb878SKiyoshi Ueda 	unsigned long flags;
2533d0bcb878SKiyoshi Ueda 
2534d0bcb878SKiyoshi Ueda 	/*
2535d0bcb878SKiyoshi Ueda 	 * Hold the md reference here and leave it at the last part so that
2536d0bcb878SKiyoshi Ueda 	 * the md can't be deleted by device opener when the barrier request
2537d0bcb878SKiyoshi Ueda 	 * completes.
2538d0bcb878SKiyoshi Ueda 	 */
2539d0bcb878SKiyoshi Ueda 	dm_get(md);
2540d0bcb878SKiyoshi Ueda 
2541d0bcb878SKiyoshi Ueda 	error = dm_rq_barrier(md);
2542d0bcb878SKiyoshi Ueda 
2543d0bcb878SKiyoshi Ueda 	rq = md->flush_request;
2544d0bcb878SKiyoshi Ueda 	md->flush_request = NULL;
2545d0bcb878SKiyoshi Ueda 
2546d0bcb878SKiyoshi Ueda 	if (error == DM_ENDIO_REQUEUE) {
2547d0bcb878SKiyoshi Ueda 		spin_lock_irqsave(q->queue_lock, flags);
2548d0bcb878SKiyoshi Ueda 		blk_requeue_request(q, rq);
2549d0bcb878SKiyoshi Ueda 		spin_unlock_irqrestore(q->queue_lock, flags);
2550d0bcb878SKiyoshi Ueda 	} else
2551d0bcb878SKiyoshi Ueda 		blk_end_request_all(rq, error);
2552d0bcb878SKiyoshi Ueda 
2553d0bcb878SKiyoshi Ueda 	blk_run_queue(q);
2554d0bcb878SKiyoshi Ueda 
2555d0bcb878SKiyoshi Ueda 	dm_put(md);
2556d0bcb878SKiyoshi Ueda }
2557d0bcb878SKiyoshi Ueda 
25581da177e4SLinus Torvalds /*
2559042d2a9bSAlasdair G Kergon  * Swap in a new table, returning the old one for the caller to destroy.
25601da177e4SLinus Torvalds  */
2561042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
25621da177e4SLinus Torvalds {
2563042d2a9bSAlasdair G Kergon 	struct dm_table *map = ERR_PTR(-EINVAL);
2564754c5fc7SMike Snitzer 	struct queue_limits limits;
2565042d2a9bSAlasdair G Kergon 	int r;
25661da177e4SLinus Torvalds 
2567e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
25681da177e4SLinus Torvalds 
25691da177e4SLinus Torvalds 	/* device must be suspended */
25704f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
257193c534aeSAlasdair G Kergon 		goto out;
25721da177e4SLinus Torvalds 
2573754c5fc7SMike Snitzer 	r = dm_calculate_queue_limits(table, &limits);
2574042d2a9bSAlasdair G Kergon 	if (r) {
2575042d2a9bSAlasdair G Kergon 		map = ERR_PTR(r);
2576754c5fc7SMike Snitzer 		goto out;
2577042d2a9bSAlasdair G Kergon 	}
2578754c5fc7SMike Snitzer 
2579042d2a9bSAlasdair G Kergon 	map = __bind(md, table, &limits);
25801da177e4SLinus Torvalds 
258193c534aeSAlasdair G Kergon out:
2582e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2583042d2a9bSAlasdair G Kergon 	return map;
25841da177e4SLinus Torvalds }
25851da177e4SLinus Torvalds 
25861da177e4SLinus Torvalds /*
25871da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
25881da177e4SLinus Torvalds  * device.
25891da177e4SLinus Torvalds  */
25902ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
25911da177e4SLinus Torvalds {
2592e39e2e95SAlasdair G Kergon 	int r;
25931da177e4SLinus Torvalds 
25941da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
2595dfbe03f6SAlasdair G Kergon 
2596db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
2597dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
2598cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
2599e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
2600e39e2e95SAlasdair G Kergon 		return r;
2601dfbe03f6SAlasdair G Kergon 	}
2602dfbe03f6SAlasdair G Kergon 
2603aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
2604aa8d7c2fSAlasdair G Kergon 
26051da177e4SLinus Torvalds 	return 0;
26061da177e4SLinus Torvalds }
26071da177e4SLinus Torvalds 
26082ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
26091da177e4SLinus Torvalds {
2610aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
2611aa8d7c2fSAlasdair G Kergon 		return;
2612aa8d7c2fSAlasdair G Kergon 
2613db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
26141da177e4SLinus Torvalds 	md->frozen_sb = NULL;
2615aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
26161da177e4SLinus Torvalds }
26171da177e4SLinus Torvalds 
26181da177e4SLinus Torvalds /*
26191da177e4SLinus Torvalds  * We need to be able to change a mapping table under a mounted
26201da177e4SLinus Torvalds  * filesystem.  For example we might want to move some data in
26211da177e4SLinus Torvalds  * the background.  Before the table can be swapped with
26221da177e4SLinus Torvalds  * dm_bind_table, dm_suspend must be called to flush any in
26231da177e4SLinus Torvalds  * flight bios and ensure that any further io gets deferred.
26241da177e4SLinus Torvalds  */
2625cec47e3dSKiyoshi Ueda /*
2626cec47e3dSKiyoshi Ueda  * Suspend mechanism in request-based dm.
2627cec47e3dSKiyoshi Ueda  *
26289f518b27SKiyoshi Ueda  * 1. Flush all I/Os by lock_fs() if needed.
26299f518b27SKiyoshi Ueda  * 2. Stop dispatching any I/O by stopping the request_queue.
26309f518b27SKiyoshi Ueda  * 3. Wait for all in-flight I/Os to be completed or requeued.
2631cec47e3dSKiyoshi Ueda  *
26329f518b27SKiyoshi Ueda  * To abort suspend, start the request_queue.
2633cec47e3dSKiyoshi Ueda  */
2634a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
26351da177e4SLinus Torvalds {
26362ca3310eSAlasdair G Kergon 	struct dm_table *map = NULL;
263746125c1cSMilan Broz 	int r = 0;
2638a3d77d35SKiyoshi Ueda 	int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
26392e93ccc1SKiyoshi Ueda 	int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
26401da177e4SLinus Torvalds 
2641e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
26422ca3310eSAlasdair G Kergon 
26434f186f8bSKiyoshi Ueda 	if (dm_suspended_md(md)) {
264473d410c0SMilan Broz 		r = -EINVAL;
2645d287483dSAlasdair G Kergon 		goto out_unlock;
264673d410c0SMilan Broz 	}
26471da177e4SLinus Torvalds 
26487c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
2649cf222b37SAlasdair G Kergon 
26502e93ccc1SKiyoshi Ueda 	/*
26512e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
26522e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
26532e93ccc1SKiyoshi Ueda 	 */
26542e93ccc1SKiyoshi Ueda 	if (noflush)
26552e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
26562e93ccc1SKiyoshi Ueda 
2657436d4108SAlasdair G Kergon 	/* This does not get reverted if there's an error later. */
26581da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
26591da177e4SLinus Torvalds 
26602e93ccc1SKiyoshi Ueda 	/*
26619f518b27SKiyoshi Ueda 	 * Flush I/O to the device.
26629f518b27SKiyoshi Ueda 	 * Any I/O submitted after lock_fs() may not be flushed.
26639f518b27SKiyoshi Ueda 	 * noflush takes precedence over do_lockfs.
26649f518b27SKiyoshi Ueda 	 * (lock_fs() flushes I/Os and waits for them to complete.)
26652e93ccc1SKiyoshi Ueda 	 */
266632a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
26672ca3310eSAlasdair G Kergon 		r = lock_fs(md);
26682ca3310eSAlasdair G Kergon 		if (r)
26692ca3310eSAlasdair G Kergon 			goto out;
2670aa8d7c2fSAlasdair G Kergon 	}
26711da177e4SLinus Torvalds 
26721da177e4SLinus Torvalds 	/*
26733b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
26743b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
26753b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
26763b00b203SMikulas Patocka 	 * dm_wq_work.
26773b00b203SMikulas Patocka 	 *
26783b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
26793b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
26803b00b203SMikulas Patocka 	 * __split_and_process_bio from dm_request, we set
26813b00b203SMikulas Patocka 	 * DMF_QUEUE_IO_TO_THREAD.
26823b00b203SMikulas Patocka 	 *
26833b00b203SMikulas Patocka 	 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
26843b00b203SMikulas Patocka 	 * and call flush_workqueue(md->wq). flush_workqueue will wait until
26853b00b203SMikulas Patocka 	 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
26863b00b203SMikulas Patocka 	 * further calls to __split_and_process_bio from dm_wq_work.
26871da177e4SLinus Torvalds 	 */
26882ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
26891eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
26901eb787ecSAlasdair G Kergon 	set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
26912ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
26921da177e4SLinus Torvalds 
2693d0bcb878SKiyoshi Ueda 	/*
2694d0bcb878SKiyoshi Ueda 	 * Request-based dm uses md->wq for barrier (dm_rq_barrier_work) which
2695d0bcb878SKiyoshi Ueda 	 * can be kicked until md->queue is stopped.  So stop md->queue before
2696d0bcb878SKiyoshi Ueda 	 * flushing md->wq.
2697d0bcb878SKiyoshi Ueda 	 */
2698cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
26999f518b27SKiyoshi Ueda 		stop_queue(md->queue);
2700cec47e3dSKiyoshi Ueda 
2701d0bcb878SKiyoshi Ueda 	flush_workqueue(md->wq);
2702d0bcb878SKiyoshi Ueda 
27031da177e4SLinus Torvalds 	/*
27043b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
27053b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
27063b00b203SMikulas Patocka 	 * to finish.
27071da177e4SLinus Torvalds 	 */
2708401600dfSMikulas Patocka 	r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
27091da177e4SLinus Torvalds 
27102ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
27116d6f10dfSMilan Broz 	if (noflush)
2712022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
271394d6351eSMilan Broz 	up_write(&md->io_lock);
27142e93ccc1SKiyoshi Ueda 
27151da177e4SLinus Torvalds 	/* were we interrupted ? */
271646125c1cSMilan Broz 	if (r < 0) {
27179a1fb464SMikulas Patocka 		dm_queue_flush(md);
271873d410c0SMilan Broz 
2719cec47e3dSKiyoshi Ueda 		if (dm_request_based(md))
27209f518b27SKiyoshi Ueda 			start_queue(md->queue);
2721cec47e3dSKiyoshi Ueda 
27222ca3310eSAlasdair G Kergon 		unlock_fs(md);
27232e93ccc1SKiyoshi Ueda 		goto out; /* pushback list is already flushed, so skip flush */
27242ca3310eSAlasdair G Kergon 	}
27252ca3310eSAlasdair G Kergon 
27263b00b203SMikulas Patocka 	/*
27273b00b203SMikulas Patocka 	 * If dm_wait_for_completion returned 0, the device is completely
27283b00b203SMikulas Patocka 	 * quiescent now. There is no request-processing activity. All new
27293b00b203SMikulas Patocka 	 * requests are being added to md->deferred list.
27303b00b203SMikulas Patocka 	 */
27313b00b203SMikulas Patocka 
27321da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
27331da177e4SLinus Torvalds 
27344d4471cbSKiyoshi Ueda 	dm_table_postsuspend_targets(map);
27354d4471cbSKiyoshi Ueda 
27362ca3310eSAlasdair G Kergon out:
27371da177e4SLinus Torvalds 	dm_table_put(map);
2738d287483dSAlasdair G Kergon 
2739d287483dSAlasdair G Kergon out_unlock:
2740e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
2741cf222b37SAlasdair G Kergon 	return r;
27421da177e4SLinus Torvalds }
27431da177e4SLinus Torvalds 
27441da177e4SLinus Torvalds int dm_resume(struct mapped_device *md)
27451da177e4SLinus Torvalds {
2746cf222b37SAlasdair G Kergon 	int r = -EINVAL;
2747cf222b37SAlasdair G Kergon 	struct dm_table *map = NULL;
27481da177e4SLinus Torvalds 
2749e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
27504f186f8bSKiyoshi Ueda 	if (!dm_suspended_md(md))
2751cf222b37SAlasdair G Kergon 		goto out;
2752cf222b37SAlasdair G Kergon 
27537c666411SAlasdair G Kergon 	map = dm_get_live_table(md);
27542ca3310eSAlasdair G Kergon 	if (!map || !dm_table_get_size(map))
2755cf222b37SAlasdair G Kergon 		goto out;
27561da177e4SLinus Torvalds 
27578757b776SMilan Broz 	r = dm_table_resume_targets(map);
27588757b776SMilan Broz 	if (r)
27598757b776SMilan Broz 		goto out;
27602ca3310eSAlasdair G Kergon 
27619a1fb464SMikulas Patocka 	dm_queue_flush(md);
27622ca3310eSAlasdair G Kergon 
2763cec47e3dSKiyoshi Ueda 	/*
2764cec47e3dSKiyoshi Ueda 	 * Flushing deferred I/Os must be done after targets are resumed
2765cec47e3dSKiyoshi Ueda 	 * so that mapping of targets can work correctly.
2766cec47e3dSKiyoshi Ueda 	 * Request-based dm is queueing the deferred I/Os in its request_queue.
2767cec47e3dSKiyoshi Ueda 	 */
2768cec47e3dSKiyoshi Ueda 	if (dm_request_based(md))
2769cec47e3dSKiyoshi Ueda 		start_queue(md->queue);
2770cec47e3dSKiyoshi Ueda 
27712ca3310eSAlasdair G Kergon 	unlock_fs(md);
27722ca3310eSAlasdair G Kergon 
27732ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
27742ca3310eSAlasdair G Kergon 
27751da177e4SLinus Torvalds 	dm_table_unplug_all(map);
2776cf222b37SAlasdair G Kergon 	r = 0;
2777cf222b37SAlasdair G Kergon out:
2778cf222b37SAlasdair G Kergon 	dm_table_put(map);
2779e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
27802ca3310eSAlasdair G Kergon 
2781cf222b37SAlasdair G Kergon 	return r;
27821da177e4SLinus Torvalds }
27831da177e4SLinus Torvalds 
27841da177e4SLinus Torvalds /*-----------------------------------------------------------------
27851da177e4SLinus Torvalds  * Event notification.
27861da177e4SLinus Torvalds  *---------------------------------------------------------------*/
27873abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
278860935eb2SMilan Broz 		       unsigned cookie)
278969267a30SAlasdair G Kergon {
279060935eb2SMilan Broz 	char udev_cookie[DM_COOKIE_LENGTH];
279160935eb2SMilan Broz 	char *envp[] = { udev_cookie, NULL };
279260935eb2SMilan Broz 
279360935eb2SMilan Broz 	if (!cookie)
27943abf85b5SPeter Rajnoha 		return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
279560935eb2SMilan Broz 	else {
279660935eb2SMilan Broz 		snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
279760935eb2SMilan Broz 			 DM_COOKIE_ENV_VAR_NAME, cookie);
27983abf85b5SPeter Rajnoha 		return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
27993abf85b5SPeter Rajnoha 					  action, envp);
280060935eb2SMilan Broz 	}
280169267a30SAlasdair G Kergon }
280269267a30SAlasdair G Kergon 
28037a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
28047a8c3d3bSMike Anderson {
28057a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
28067a8c3d3bSMike Anderson }
28077a8c3d3bSMike Anderson 
28081da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
28091da177e4SLinus Torvalds {
28101da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
28111da177e4SLinus Torvalds }
28121da177e4SLinus Torvalds 
28131da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
28141da177e4SLinus Torvalds {
28151da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
28161da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
28171da177e4SLinus Torvalds }
28181da177e4SLinus Torvalds 
28197a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
28207a8c3d3bSMike Anderson {
28217a8c3d3bSMike Anderson 	unsigned long flags;
28227a8c3d3bSMike Anderson 
28237a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
28247a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
28257a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
28267a8c3d3bSMike Anderson }
28277a8c3d3bSMike Anderson 
28281da177e4SLinus Torvalds /*
28291da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
28301da177e4SLinus Torvalds  * count on 'md'.
28311da177e4SLinus Torvalds  */
28321da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
28331da177e4SLinus Torvalds {
28341da177e4SLinus Torvalds 	return md->disk;
28351da177e4SLinus Torvalds }
28361da177e4SLinus Torvalds 
2837784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
2838784aae73SMilan Broz {
2839784aae73SMilan Broz 	return &md->kobj;
2840784aae73SMilan Broz }
2841784aae73SMilan Broz 
2842784aae73SMilan Broz /*
2843784aae73SMilan Broz  * struct mapped_device should not be exported outside of dm.c
2844784aae73SMilan Broz  * so use this check to verify that kobj is part of md structure
2845784aae73SMilan Broz  */
2846784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
2847784aae73SMilan Broz {
2848784aae73SMilan Broz 	struct mapped_device *md;
2849784aae73SMilan Broz 
2850784aae73SMilan Broz 	md = container_of(kobj, struct mapped_device, kobj);
2851784aae73SMilan Broz 	if (&md->kobj != kobj)
2852784aae73SMilan Broz 		return NULL;
2853784aae73SMilan Broz 
28544d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
2855432a212cSMike Anderson 	    dm_deleting_md(md))
28564d89b7b4SMilan Broz 		return NULL;
28574d89b7b4SMilan Broz 
2858784aae73SMilan Broz 	dm_get(md);
2859784aae73SMilan Broz 	return md;
2860784aae73SMilan Broz }
2861784aae73SMilan Broz 
28624f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md)
28631da177e4SLinus Torvalds {
28641da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
28651da177e4SLinus Torvalds }
28661da177e4SLinus Torvalds 
286764dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti)
286864dbce58SKiyoshi Ueda {
2869ecdb2e25SKiyoshi Ueda 	return dm_suspended_md(dm_table_get_md(ti->table));
287064dbce58SKiyoshi Ueda }
287164dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended);
287264dbce58SKiyoshi Ueda 
28732e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
28742e93ccc1SKiyoshi Ueda {
2875ecdb2e25SKiyoshi Ueda 	return __noflush_suspending(dm_table_get_md(ti->table));
28762e93ccc1SKiyoshi Ueda }
28772e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
28782e93ccc1SKiyoshi Ueda 
2879e6ee8c0bSKiyoshi Ueda struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
2880e6ee8c0bSKiyoshi Ueda {
2881e6ee8c0bSKiyoshi Ueda 	struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
2882e6ee8c0bSKiyoshi Ueda 
2883e6ee8c0bSKiyoshi Ueda 	if (!pools)
2884e6ee8c0bSKiyoshi Ueda 		return NULL;
2885e6ee8c0bSKiyoshi Ueda 
2886e6ee8c0bSKiyoshi Ueda 	pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
2887e6ee8c0bSKiyoshi Ueda 			 mempool_create_slab_pool(MIN_IOS, _io_cache) :
2888e6ee8c0bSKiyoshi Ueda 			 mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
2889e6ee8c0bSKiyoshi Ueda 	if (!pools->io_pool)
2890e6ee8c0bSKiyoshi Ueda 		goto free_pools_and_out;
2891e6ee8c0bSKiyoshi Ueda 
2892e6ee8c0bSKiyoshi Ueda 	pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
2893e6ee8c0bSKiyoshi Ueda 			  mempool_create_slab_pool(MIN_IOS, _tio_cache) :
2894e6ee8c0bSKiyoshi Ueda 			  mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
2895e6ee8c0bSKiyoshi Ueda 	if (!pools->tio_pool)
2896e6ee8c0bSKiyoshi Ueda 		goto free_io_pool_and_out;
2897e6ee8c0bSKiyoshi Ueda 
2898e6ee8c0bSKiyoshi Ueda 	pools->bs = (type == DM_TYPE_BIO_BASED) ?
2899e6ee8c0bSKiyoshi Ueda 		    bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
2900e6ee8c0bSKiyoshi Ueda 	if (!pools->bs)
2901e6ee8c0bSKiyoshi Ueda 		goto free_tio_pool_and_out;
2902e6ee8c0bSKiyoshi Ueda 
2903e6ee8c0bSKiyoshi Ueda 	return pools;
2904e6ee8c0bSKiyoshi Ueda 
2905e6ee8c0bSKiyoshi Ueda free_tio_pool_and_out:
2906e6ee8c0bSKiyoshi Ueda 	mempool_destroy(pools->tio_pool);
2907e6ee8c0bSKiyoshi Ueda 
2908e6ee8c0bSKiyoshi Ueda free_io_pool_and_out:
2909e6ee8c0bSKiyoshi Ueda 	mempool_destroy(pools->io_pool);
2910e6ee8c0bSKiyoshi Ueda 
2911e6ee8c0bSKiyoshi Ueda free_pools_and_out:
2912e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2913e6ee8c0bSKiyoshi Ueda 
2914e6ee8c0bSKiyoshi Ueda 	return NULL;
2915e6ee8c0bSKiyoshi Ueda }
2916e6ee8c0bSKiyoshi Ueda 
2917e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools)
2918e6ee8c0bSKiyoshi Ueda {
2919e6ee8c0bSKiyoshi Ueda 	if (!pools)
2920e6ee8c0bSKiyoshi Ueda 		return;
2921e6ee8c0bSKiyoshi Ueda 
2922e6ee8c0bSKiyoshi Ueda 	if (pools->io_pool)
2923e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->io_pool);
2924e6ee8c0bSKiyoshi Ueda 
2925e6ee8c0bSKiyoshi Ueda 	if (pools->tio_pool)
2926e6ee8c0bSKiyoshi Ueda 		mempool_destroy(pools->tio_pool);
2927e6ee8c0bSKiyoshi Ueda 
2928e6ee8c0bSKiyoshi Ueda 	if (pools->bs)
2929e6ee8c0bSKiyoshi Ueda 		bioset_free(pools->bs);
2930e6ee8c0bSKiyoshi Ueda 
2931e6ee8c0bSKiyoshi Ueda 	kfree(pools);
2932e6ee8c0bSKiyoshi Ueda }
2933e6ee8c0bSKiyoshi Ueda 
293483d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = {
29351da177e4SLinus Torvalds 	.open = dm_blk_open,
29361da177e4SLinus Torvalds 	.release = dm_blk_close,
2937aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
29383ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
29391da177e4SLinus Torvalds 	.owner = THIS_MODULE
29401da177e4SLinus Torvalds };
29411da177e4SLinus Torvalds 
29421da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo);
29431da177e4SLinus Torvalds 
29441da177e4SLinus Torvalds /*
29451da177e4SLinus Torvalds  * module hooks
29461da177e4SLinus Torvalds  */
29471da177e4SLinus Torvalds module_init(dm_init);
29481da177e4SLinus Torvalds module_exit(dm_exit);
29491da177e4SLinus Torvalds 
29501da177e4SLinus Torvalds module_param(major, uint, 0);
29511da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
29521da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
29531da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
29541da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2955