xref: /openbmc/linux/drivers/md/dm.c (revision 55782138e47d9baf2f7d3a7af9e7cf42adf72c56)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3784aae73SMilan Broz  * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This file is released under the GPL.
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include "dm.h"
951e5b2bdSMike Anderson #include "dm-uevent.h"
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/init.h>
121da177e4SLinus Torvalds #include <linux/module.h>
1348c9c27bSArjan van de Ven #include <linux/mutex.h>
141da177e4SLinus Torvalds #include <linux/moduleparam.h>
151da177e4SLinus Torvalds #include <linux/blkpg.h>
161da177e4SLinus Torvalds #include <linux/bio.h>
171da177e4SLinus Torvalds #include <linux/buffer_head.h>
181da177e4SLinus Torvalds #include <linux/mempool.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/idr.h>
213ac51e74SDarrick J. Wong #include <linux/hdreg.h>
222056a782SJens Axboe #include <linux/blktrace_api.h>
23*55782138SLi Zefan 
24*55782138SLi Zefan #include <trace/events/block.h>
251da177e4SLinus Torvalds 
2672d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
2772d94861SAlasdair G Kergon 
281da177e4SLinus Torvalds static const char *_name = DM_NAME;
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds static unsigned int major = 0;
311da177e4SLinus Torvalds static unsigned int _major = 0;
321da177e4SLinus Torvalds 
33f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
341da177e4SLinus Torvalds /*
358fbf26adSKiyoshi Ueda  * For bio-based dm.
361da177e4SLinus Torvalds  * One of these is allocated per bio.
371da177e4SLinus Torvalds  */
381da177e4SLinus Torvalds struct dm_io {
391da177e4SLinus Torvalds 	struct mapped_device *md;
401da177e4SLinus Torvalds 	int error;
411da177e4SLinus Torvalds 	atomic_t io_count;
426ae2fa67SRichard Kennedy 	struct bio *bio;
433eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
441da177e4SLinus Torvalds };
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds /*
478fbf26adSKiyoshi Ueda  * For bio-based dm.
481da177e4SLinus Torvalds  * One of these is allocated per target within a bio.  Hopefully
491da177e4SLinus Torvalds  * this will be simplified out one day.
501da177e4SLinus Torvalds  */
51028867acSAlasdair G Kergon struct dm_target_io {
521da177e4SLinus Torvalds 	struct dm_io *io;
531da177e4SLinus Torvalds 	struct dm_target *ti;
541da177e4SLinus Torvalds 	union map_info info;
551da177e4SLinus Torvalds };
561da177e4SLinus Torvalds 
578fbf26adSKiyoshi Ueda /*
588fbf26adSKiyoshi Ueda  * For request-based dm.
598fbf26adSKiyoshi Ueda  * One of these is allocated per request.
608fbf26adSKiyoshi Ueda  */
618fbf26adSKiyoshi Ueda struct dm_rq_target_io {
628fbf26adSKiyoshi Ueda 	struct mapped_device *md;
638fbf26adSKiyoshi Ueda 	struct dm_target *ti;
648fbf26adSKiyoshi Ueda 	struct request *orig, clone;
658fbf26adSKiyoshi Ueda 	int error;
668fbf26adSKiyoshi Ueda 	union map_info info;
678fbf26adSKiyoshi Ueda };
688fbf26adSKiyoshi Ueda 
698fbf26adSKiyoshi Ueda /*
708fbf26adSKiyoshi Ueda  * For request-based dm.
718fbf26adSKiyoshi Ueda  * One of these is allocated per bio.
728fbf26adSKiyoshi Ueda  */
738fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info {
748fbf26adSKiyoshi Ueda 	struct bio *orig;
758fbf26adSKiyoshi Ueda 	struct request *rq;
768fbf26adSKiyoshi Ueda };
778fbf26adSKiyoshi Ueda 
781da177e4SLinus Torvalds union map_info *dm_get_mapinfo(struct bio *bio)
791da177e4SLinus Torvalds {
801da177e4SLinus Torvalds 	if (bio && bio->bi_private)
81028867acSAlasdair G Kergon 		return &((struct dm_target_io *)bio->bi_private)->info;
821da177e4SLinus Torvalds 	return NULL;
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds 
85ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
86ba61fdd1SJeff Mahoney 
871da177e4SLinus Torvalds /*
881da177e4SLinus Torvalds  * Bits for the md->flags field.
891da177e4SLinus Torvalds  */
901eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
911da177e4SLinus Torvalds #define DMF_SUSPENDED 1
92aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
93fba9f90eSJeff Mahoney #define DMF_FREEING 3
945c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
952e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
961eb787ecSAlasdair G Kergon #define DMF_QUEUE_IO_TO_THREAD 6
971da177e4SLinus Torvalds 
98304f3f6aSMilan Broz /*
99304f3f6aSMilan Broz  * Work processed by per-device workqueue.
100304f3f6aSMilan Broz  */
1011da177e4SLinus Torvalds struct mapped_device {
1022ca3310eSAlasdair G Kergon 	struct rw_semaphore io_lock;
103e61290a4SDaniel Walker 	struct mutex suspend_lock;
1041da177e4SLinus Torvalds 	rwlock_t map_lock;
1051da177e4SLinus Torvalds 	atomic_t holders;
1065c6bd75dSAlasdair G Kergon 	atomic_t open_count;
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 	unsigned long flags;
1091da177e4SLinus Torvalds 
110165125e1SJens Axboe 	struct request_queue *queue;
1111da177e4SLinus Torvalds 	struct gendisk *disk;
1127e51f257SMike Anderson 	char name[16];
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 	void *interface_ptr;
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	/*
1171da177e4SLinus Torvalds 	 * A list of ios that arrived while we were suspended.
1181da177e4SLinus Torvalds 	 */
1191da177e4SLinus Torvalds 	atomic_t pending;
1201da177e4SLinus Torvalds 	wait_queue_head_t wait;
12153d5914fSMikulas Patocka 	struct work_struct work;
1221da177e4SLinus Torvalds 	struct bio_list deferred;
123022c2611SMikulas Patocka 	spinlock_t deferred_lock;
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	/*
126af7e466aSMikulas Patocka 	 * An error from the barrier request currently being processed.
127af7e466aSMikulas Patocka 	 */
128af7e466aSMikulas Patocka 	int barrier_error;
129af7e466aSMikulas Patocka 
130af7e466aSMikulas Patocka 	/*
131304f3f6aSMilan Broz 	 * Processing queue (flush/barriers)
132304f3f6aSMilan Broz 	 */
133304f3f6aSMilan Broz 	struct workqueue_struct *wq;
134304f3f6aSMilan Broz 
135304f3f6aSMilan Broz 	/*
1361da177e4SLinus Torvalds 	 * The current mapping.
1371da177e4SLinus Torvalds 	 */
1381da177e4SLinus Torvalds 	struct dm_table *map;
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 	/*
1411da177e4SLinus Torvalds 	 * io objects are allocated from here.
1421da177e4SLinus Torvalds 	 */
1431da177e4SLinus Torvalds 	mempool_t *io_pool;
1441da177e4SLinus Torvalds 	mempool_t *tio_pool;
1451da177e4SLinus Torvalds 
1469faf400fSStefan Bader 	struct bio_set *bs;
1479faf400fSStefan Bader 
1481da177e4SLinus Torvalds 	/*
1491da177e4SLinus Torvalds 	 * Event handling.
1501da177e4SLinus Torvalds 	 */
1511da177e4SLinus Torvalds 	atomic_t event_nr;
1521da177e4SLinus Torvalds 	wait_queue_head_t eventq;
1537a8c3d3bSMike Anderson 	atomic_t uevent_seq;
1547a8c3d3bSMike Anderson 	struct list_head uevent_list;
1557a8c3d3bSMike Anderson 	spinlock_t uevent_lock; /* Protect access to uevent_list */
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	/*
1581da177e4SLinus Torvalds 	 * freeze/thaw support require holding onto a super block
1591da177e4SLinus Torvalds 	 */
1601da177e4SLinus Torvalds 	struct super_block *frozen_sb;
161e39e2e95SAlasdair G Kergon 	struct block_device *suspended_bdev;
1623ac51e74SDarrick J. Wong 
1633ac51e74SDarrick J. Wong 	/* forced geometry settings */
1643ac51e74SDarrick J. Wong 	struct hd_geometry geometry;
165784aae73SMilan Broz 
166784aae73SMilan Broz 	/* sysfs handle */
167784aae73SMilan Broz 	struct kobject kobj;
1681da177e4SLinus Torvalds };
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds #define MIN_IOS 256
171e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
172e18b890bSChristoph Lameter static struct kmem_cache *_tio_cache;
1738fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
1748fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_bio_info_cache;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds static int __init local_init(void)
1771da177e4SLinus Torvalds {
17851157b4aSKiyoshi Ueda 	int r = -ENOMEM;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
181028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
1821da177e4SLinus Torvalds 	if (!_io_cache)
18351157b4aSKiyoshi Ueda 		return r;
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	/* allocate a slab for the target ios */
186028867acSAlasdair G Kergon 	_tio_cache = KMEM_CACHE(dm_target_io, 0);
18751157b4aSKiyoshi Ueda 	if (!_tio_cache)
18851157b4aSKiyoshi Ueda 		goto out_free_io_cache;
1891da177e4SLinus Torvalds 
1908fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
1918fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
1928fbf26adSKiyoshi Ueda 		goto out_free_tio_cache;
1938fbf26adSKiyoshi Ueda 
1948fbf26adSKiyoshi Ueda 	_rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
1958fbf26adSKiyoshi Ueda 	if (!_rq_bio_info_cache)
1968fbf26adSKiyoshi Ueda 		goto out_free_rq_tio_cache;
1978fbf26adSKiyoshi Ueda 
19851e5b2bdSMike Anderson 	r = dm_uevent_init();
19951157b4aSKiyoshi Ueda 	if (r)
2008fbf26adSKiyoshi Ueda 		goto out_free_rq_bio_info_cache;
20151e5b2bdSMike Anderson 
2021da177e4SLinus Torvalds 	_major = major;
2031da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
20451157b4aSKiyoshi Ueda 	if (r < 0)
20551157b4aSKiyoshi Ueda 		goto out_uevent_exit;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	if (!_major)
2081da177e4SLinus Torvalds 		_major = r;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	return 0;
21151157b4aSKiyoshi Ueda 
21251157b4aSKiyoshi Ueda out_uevent_exit:
21351157b4aSKiyoshi Ueda 	dm_uevent_exit();
2148fbf26adSKiyoshi Ueda out_free_rq_bio_info_cache:
2158fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_bio_info_cache);
2168fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
2178fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
21851157b4aSKiyoshi Ueda out_free_tio_cache:
21951157b4aSKiyoshi Ueda 	kmem_cache_destroy(_tio_cache);
22051157b4aSKiyoshi Ueda out_free_io_cache:
22151157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
22251157b4aSKiyoshi Ueda 
22351157b4aSKiyoshi Ueda 	return r;
2241da177e4SLinus Torvalds }
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds static void local_exit(void)
2271da177e4SLinus Torvalds {
2288fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_bio_info_cache);
2298fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
2301da177e4SLinus Torvalds 	kmem_cache_destroy(_tio_cache);
2311da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
23200d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
23351e5b2bdSMike Anderson 	dm_uevent_exit();
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	_major = 0;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	DMINFO("cleaned up");
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds 
240b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
2411da177e4SLinus Torvalds 	local_init,
2421da177e4SLinus Torvalds 	dm_target_init,
2431da177e4SLinus Torvalds 	dm_linear_init,
2441da177e4SLinus Torvalds 	dm_stripe_init,
245945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2461da177e4SLinus Torvalds 	dm_interface_init,
2471da177e4SLinus Torvalds };
2481da177e4SLinus Torvalds 
249b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2501da177e4SLinus Torvalds 	local_exit,
2511da177e4SLinus Torvalds 	dm_target_exit,
2521da177e4SLinus Torvalds 	dm_linear_exit,
2531da177e4SLinus Torvalds 	dm_stripe_exit,
254945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
2551da177e4SLinus Torvalds 	dm_interface_exit,
2561da177e4SLinus Torvalds };
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds static int __init dm_init(void)
2591da177e4SLinus Torvalds {
2601da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	int r, i;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
2651da177e4SLinus Torvalds 		r = _inits[i]();
2661da177e4SLinus Torvalds 		if (r)
2671da177e4SLinus Torvalds 			goto bad;
2681da177e4SLinus Torvalds 	}
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	return 0;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds       bad:
2731da177e4SLinus Torvalds 	while (i--)
2741da177e4SLinus Torvalds 		_exits[i]();
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	return r;
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds static void __exit dm_exit(void)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	while (i--)
2841da177e4SLinus Torvalds 		_exits[i]();
2851da177e4SLinus Torvalds }
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds /*
2881da177e4SLinus Torvalds  * Block device functions
2891da177e4SLinus Torvalds  */
290fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	struct mapped_device *md;
2931da177e4SLinus Torvalds 
294fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
295fba9f90eSJeff Mahoney 
296fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
297fba9f90eSJeff Mahoney 	if (!md)
298fba9f90eSJeff Mahoney 		goto out;
299fba9f90eSJeff Mahoney 
3005c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
3015c6bd75dSAlasdair G Kergon 	    test_bit(DMF_DELETING, &md->flags)) {
302fba9f90eSJeff Mahoney 		md = NULL;
303fba9f90eSJeff Mahoney 		goto out;
304fba9f90eSJeff Mahoney 	}
305fba9f90eSJeff Mahoney 
3061da177e4SLinus Torvalds 	dm_get(md);
3075c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
308fba9f90eSJeff Mahoney 
309fba9f90eSJeff Mahoney out:
310fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
311fba9f90eSJeff Mahoney 
312fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds 
315fe5f9f2cSAl Viro static int dm_blk_close(struct gendisk *disk, fmode_t mode)
3161da177e4SLinus Torvalds {
317fe5f9f2cSAl Viro 	struct mapped_device *md = disk->private_data;
3185c6bd75dSAlasdair G Kergon 	atomic_dec(&md->open_count);
3191da177e4SLinus Torvalds 	dm_put(md);
3201da177e4SLinus Torvalds 	return 0;
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
3235c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3245c6bd75dSAlasdair G Kergon {
3255c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3265c6bd75dSAlasdair G Kergon }
3275c6bd75dSAlasdair G Kergon 
3285c6bd75dSAlasdair G Kergon /*
3295c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3305c6bd75dSAlasdair G Kergon  */
3315c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md)
3325c6bd75dSAlasdair G Kergon {
3335c6bd75dSAlasdair G Kergon 	int r = 0;
3345c6bd75dSAlasdair G Kergon 
3355c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
3365c6bd75dSAlasdair G Kergon 
3375c6bd75dSAlasdair G Kergon 	if (dm_open_count(md))
3385c6bd75dSAlasdair G Kergon 		r = -EBUSY;
3395c6bd75dSAlasdair G Kergon 	else
3405c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
3415c6bd75dSAlasdair G Kergon 
3425c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
3435c6bd75dSAlasdair G Kergon 
3445c6bd75dSAlasdair G Kergon 	return r;
3455c6bd75dSAlasdair G Kergon }
3465c6bd75dSAlasdair G Kergon 
3473ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3483ac51e74SDarrick J. Wong {
3493ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
3503ac51e74SDarrick J. Wong 
3513ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
3523ac51e74SDarrick J. Wong }
3533ac51e74SDarrick J. Wong 
354fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
355aa129a22SMilan Broz 			unsigned int cmd, unsigned long arg)
356aa129a22SMilan Broz {
357fe5f9f2cSAl Viro 	struct mapped_device *md = bdev->bd_disk->private_data;
358fe5f9f2cSAl Viro 	struct dm_table *map = dm_get_table(md);
359aa129a22SMilan Broz 	struct dm_target *tgt;
360aa129a22SMilan Broz 	int r = -ENOTTY;
361aa129a22SMilan Broz 
362aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
363aa129a22SMilan Broz 		goto out;
364aa129a22SMilan Broz 
365aa129a22SMilan Broz 	/* We only support devices that have a single target */
366aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
367aa129a22SMilan Broz 		goto out;
368aa129a22SMilan Broz 
369aa129a22SMilan Broz 	tgt = dm_table_get_target(map, 0);
370aa129a22SMilan Broz 
371aa129a22SMilan Broz 	if (dm_suspended(md)) {
372aa129a22SMilan Broz 		r = -EAGAIN;
373aa129a22SMilan Broz 		goto out;
374aa129a22SMilan Broz 	}
375aa129a22SMilan Broz 
376aa129a22SMilan Broz 	if (tgt->type->ioctl)
377647b3d00SAl Viro 		r = tgt->type->ioctl(tgt, cmd, arg);
378aa129a22SMilan Broz 
379aa129a22SMilan Broz out:
380aa129a22SMilan Broz 	dm_table_put(map);
381aa129a22SMilan Broz 
382aa129a22SMilan Broz 	return r;
383aa129a22SMilan Broz }
384aa129a22SMilan Broz 
385028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
3861da177e4SLinus Torvalds {
3871da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
390028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
3911da177e4SLinus Torvalds {
3921da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
3931da177e4SLinus Torvalds }
3941da177e4SLinus Torvalds 
395028867acSAlasdair G Kergon static struct dm_target_io *alloc_tio(struct mapped_device *md)
3961da177e4SLinus Torvalds {
3971da177e4SLinus Torvalds 	return mempool_alloc(md->tio_pool, GFP_NOIO);
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
400028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
4011da177e4SLinus Torvalds {
4021da177e4SLinus Torvalds 	mempool_free(tio, md->tio_pool);
4031da177e4SLinus Torvalds }
4041da177e4SLinus Torvalds 
4053eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
4063eaf840eSJun'ichi "Nick" Nomura {
4073eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
408c9959059STejun Heo 	int cpu;
4093eaf840eSJun'ichi "Nick" Nomura 
4103eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
4113eaf840eSJun'ichi "Nick" Nomura 
412074a7acaSTejun Heo 	cpu = part_stat_lock();
413074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
414074a7acaSTejun Heo 	part_stat_unlock();
415074a7acaSTejun Heo 	dm_disk(md)->part0.in_flight = atomic_inc_return(&md->pending);
4163eaf840eSJun'ichi "Nick" Nomura }
4173eaf840eSJun'ichi "Nick" Nomura 
418d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
4193eaf840eSJun'ichi "Nick" Nomura {
4203eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
4213eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
4223eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
423c9959059STejun Heo 	int pending, cpu;
4243eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
4253eaf840eSJun'ichi "Nick" Nomura 
426074a7acaSTejun Heo 	cpu = part_stat_lock();
427074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
428074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
429074a7acaSTejun Heo 	part_stat_unlock();
4303eaf840eSJun'ichi "Nick" Nomura 
431af7e466aSMikulas Patocka 	/*
432af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
433af7e466aSMikulas Patocka 	 * a barrier.
434af7e466aSMikulas Patocka 	 */
435074a7acaSTejun Heo 	dm_disk(md)->part0.in_flight = pending =
436074a7acaSTejun Heo 		atomic_dec_return(&md->pending);
4373eaf840eSJun'ichi "Nick" Nomura 
438d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
439d221d2e7SMikulas Patocka 	if (!pending)
440d221d2e7SMikulas Patocka 		wake_up(&md->wait);
4413eaf840eSJun'ichi "Nick" Nomura }
4423eaf840eSJun'ichi "Nick" Nomura 
4431da177e4SLinus Torvalds /*
4441da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
4451da177e4SLinus Torvalds  */
44692c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
4471da177e4SLinus Torvalds {
4482ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
4491da177e4SLinus Torvalds 
450022c2611SMikulas Patocka 	spin_lock_irq(&md->deferred_lock);
4511da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
452022c2611SMikulas Patocka 	spin_unlock_irq(&md->deferred_lock);
4531da177e4SLinus Torvalds 
45492c63902SMikulas Patocka 	if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
45592c63902SMikulas Patocka 		queue_work(md->wq, &md->work);
45692c63902SMikulas Patocka 
4572ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
4581da177e4SLinus Torvalds }
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds /*
4611da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
4621da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
4631da177e4SLinus Torvalds  * dm_table_put() when finished.
4641da177e4SLinus Torvalds  */
4651da177e4SLinus Torvalds struct dm_table *dm_get_table(struct mapped_device *md)
4661da177e4SLinus Torvalds {
4671da177e4SLinus Torvalds 	struct dm_table *t;
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	read_lock(&md->map_lock);
4701da177e4SLinus Torvalds 	t = md->map;
4711da177e4SLinus Torvalds 	if (t)
4721da177e4SLinus Torvalds 		dm_table_get(t);
4731da177e4SLinus Torvalds 	read_unlock(&md->map_lock);
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	return t;
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds 
4783ac51e74SDarrick J. Wong /*
4793ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
4803ac51e74SDarrick J. Wong  */
4813ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
4823ac51e74SDarrick J. Wong {
4833ac51e74SDarrick J. Wong 	*geo = md->geometry;
4843ac51e74SDarrick J. Wong 
4853ac51e74SDarrick J. Wong 	return 0;
4863ac51e74SDarrick J. Wong }
4873ac51e74SDarrick J. Wong 
4883ac51e74SDarrick J. Wong /*
4893ac51e74SDarrick J. Wong  * Set the geometry of a device.
4903ac51e74SDarrick J. Wong  */
4913ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
4923ac51e74SDarrick J. Wong {
4933ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
4943ac51e74SDarrick J. Wong 
4953ac51e74SDarrick J. Wong 	if (geo->start > sz) {
4963ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
4973ac51e74SDarrick J. Wong 		return -EINVAL;
4983ac51e74SDarrick J. Wong 	}
4993ac51e74SDarrick J. Wong 
5003ac51e74SDarrick J. Wong 	md->geometry = *geo;
5013ac51e74SDarrick J. Wong 
5023ac51e74SDarrick J. Wong 	return 0;
5033ac51e74SDarrick J. Wong }
5043ac51e74SDarrick J. Wong 
5051da177e4SLinus Torvalds /*-----------------------------------------------------------------
5061da177e4SLinus Torvalds  * CRUD START:
5071da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
5081da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
5091da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
5101da177e4SLinus Torvalds  *   interests of getting something for people to use I give
5111da177e4SLinus Torvalds  *   you this clearly demarcated crap.
5121da177e4SLinus Torvalds  *---------------------------------------------------------------*/
5131da177e4SLinus Torvalds 
5142e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
5152e93ccc1SKiyoshi Ueda {
5162e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
5172e93ccc1SKiyoshi Ueda }
5182e93ccc1SKiyoshi Ueda 
5191da177e4SLinus Torvalds /*
5201da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
5211da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
5221da177e4SLinus Torvalds  */
523858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
5241da177e4SLinus Torvalds {
5252e93ccc1SKiyoshi Ueda 	unsigned long flags;
526b35f8caaSMilan Broz 	int io_error;
527b35f8caaSMilan Broz 	struct bio *bio;
528b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
5292e93ccc1SKiyoshi Ueda 
5302e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
531b35f8caaSMilan Broz 	if (error && !(io->error > 0 && __noflush_suspending(md)))
5321da177e4SLinus Torvalds 		io->error = error;
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
5352e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
5362e93ccc1SKiyoshi Ueda 			/*
5372e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
5382e93ccc1SKiyoshi Ueda 			 */
539022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
540b35f8caaSMilan Broz 			if (__noflush_suspending(md))
541af7e466aSMikulas Patocka 				bio_list_add_head(&md->deferred, io->bio);
5422e93ccc1SKiyoshi Ueda 			else
5432e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
5442e93ccc1SKiyoshi Ueda 				io->error = -EIO;
545022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
5462e93ccc1SKiyoshi Ueda 		}
5472e93ccc1SKiyoshi Ueda 
548b35f8caaSMilan Broz 		io_error = io->error;
549b35f8caaSMilan Broz 		bio = io->bio;
5502056a782SJens Axboe 
551af7e466aSMikulas Patocka 		if (bio_barrier(bio)) {
552af7e466aSMikulas Patocka 			/*
553af7e466aSMikulas Patocka 			 * There can be just one barrier request so we use
554af7e466aSMikulas Patocka 			 * a per-device variable for error reporting.
555af7e466aSMikulas Patocka 			 * Note that you can't touch the bio after end_io_acct
556af7e466aSMikulas Patocka 			 */
557af7e466aSMikulas Patocka 			md->barrier_error = io_error;
558af7e466aSMikulas Patocka 			end_io_acct(io);
559af7e466aSMikulas Patocka 		} else {
560af7e466aSMikulas Patocka 			end_io_acct(io);
561b35f8caaSMilan Broz 
562b35f8caaSMilan Broz 			if (io_error != DM_ENDIO_REQUEUE) {
563b35f8caaSMilan Broz 				trace_block_bio_complete(md->queue, bio);
564b35f8caaSMilan Broz 
565b35f8caaSMilan Broz 				bio_endio(bio, io_error);
5662e93ccc1SKiyoshi Ueda 			}
5671da177e4SLinus Torvalds 		}
568af7e466aSMikulas Patocka 
569af7e466aSMikulas Patocka 		free_io(md, io);
570af7e466aSMikulas Patocka 	}
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds 
5736712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error)
5741da177e4SLinus Torvalds {
5751da177e4SLinus Torvalds 	int r = 0;
576028867acSAlasdair G Kergon 	struct dm_target_io *tio = bio->bi_private;
577b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
5789faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
5791da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	if (!bio_flagged(bio, BIO_UPTODATE) && !error)
5821da177e4SLinus Torvalds 		error = -EIO;
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	if (endio) {
5851da177e4SLinus Torvalds 		r = endio(tio->ti, bio, error, &tio->info);
5862e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
5872e93ccc1SKiyoshi Ueda 			/*
5882e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
5892e93ccc1SKiyoshi Ueda 			 * in dec_pending().
5902e93ccc1SKiyoshi Ueda 			 */
5911da177e4SLinus Torvalds 			error = r;
59245cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
59345cbcd79SKiyoshi Ueda 			/* The target will handle the io */
5946712ecf8SNeilBrown 			return;
59545cbcd79SKiyoshi Ueda 		else if (r) {
59645cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
59745cbcd79SKiyoshi Ueda 			BUG();
59845cbcd79SKiyoshi Ueda 		}
5991da177e4SLinus Torvalds 	}
6001da177e4SLinus Torvalds 
6019faf400fSStefan Bader 	/*
6029faf400fSStefan Bader 	 * Store md for cleanup instead of tio which is about to get freed.
6039faf400fSStefan Bader 	 */
6049faf400fSStefan Bader 	bio->bi_private = md->bs;
6059faf400fSStefan Bader 
6069faf400fSStefan Bader 	free_tio(md, tio);
607b35f8caaSMilan Broz 	bio_put(bio);
608b35f8caaSMilan Broz 	dec_pending(io, error);
6091da177e4SLinus Torvalds }
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds static sector_t max_io_len(struct mapped_device *md,
6121da177e4SLinus Torvalds 			   sector_t sector, struct dm_target *ti)
6131da177e4SLinus Torvalds {
6141da177e4SLinus Torvalds 	sector_t offset = sector - ti->begin;
6151da177e4SLinus Torvalds 	sector_t len = ti->len - offset;
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	/*
6181da177e4SLinus Torvalds 	 * Does the target need to split even further ?
6191da177e4SLinus Torvalds 	 */
6201da177e4SLinus Torvalds 	if (ti->split_io) {
6211da177e4SLinus Torvalds 		sector_t boundary;
6221da177e4SLinus Torvalds 		boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
6231da177e4SLinus Torvalds 			   - offset;
6241da177e4SLinus Torvalds 		if (len > boundary)
6251da177e4SLinus Torvalds 			len = boundary;
6261da177e4SLinus Torvalds 	}
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	return len;
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds static void __map_bio(struct dm_target *ti, struct bio *clone,
632028867acSAlasdair G Kergon 		      struct dm_target_io *tio)
6331da177e4SLinus Torvalds {
6341da177e4SLinus Torvalds 	int r;
6352056a782SJens Axboe 	sector_t sector;
6369faf400fSStefan Bader 	struct mapped_device *md;
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	/*
6391da177e4SLinus Torvalds 	 * Sanity checks.
6401da177e4SLinus Torvalds 	 */
6411da177e4SLinus Torvalds 	BUG_ON(!clone->bi_size);
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
6441da177e4SLinus Torvalds 	clone->bi_private = tio;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	/*
6471da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
6481da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
6491da177e4SLinus Torvalds 	 * this io.
6501da177e4SLinus Torvalds 	 */
6511da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
6522056a782SJens Axboe 	sector = clone->bi_sector;
6531da177e4SLinus Torvalds 	r = ti->type->map(ti, clone, &tio->info);
65445cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
6551da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
6562056a782SJens Axboe 
6575f3ea37cSArnaldo Carvalho de Melo 		trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
65822a7c31aSAlan D. Brunelle 				    tio->io->bio->bi_bdev->bd_dev, sector);
6592056a782SJens Axboe 
6601da177e4SLinus Torvalds 		generic_make_request(clone);
6612e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
6622e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
6639faf400fSStefan Bader 		md = tio->io->md;
6649faf400fSStefan Bader 		dec_pending(tio->io, r);
6659faf400fSStefan Bader 		/*
6669faf400fSStefan Bader 		 * Store bio_set for cleanup.
6679faf400fSStefan Bader 		 */
6689faf400fSStefan Bader 		clone->bi_private = md->bs;
6691da177e4SLinus Torvalds 		bio_put(clone);
6709faf400fSStefan Bader 		free_tio(md, tio);
67145cbcd79SKiyoshi Ueda 	} else if (r) {
67245cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
67345cbcd79SKiyoshi Ueda 		BUG();
6741da177e4SLinus Torvalds 	}
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds struct clone_info {
6781da177e4SLinus Torvalds 	struct mapped_device *md;
6791da177e4SLinus Torvalds 	struct dm_table *map;
6801da177e4SLinus Torvalds 	struct bio *bio;
6811da177e4SLinus Torvalds 	struct dm_io *io;
6821da177e4SLinus Torvalds 	sector_t sector;
6831da177e4SLinus Torvalds 	sector_t sector_count;
6841da177e4SLinus Torvalds 	unsigned short idx;
6851da177e4SLinus Torvalds };
6861da177e4SLinus Torvalds 
6873676347aSPeter Osterlund static void dm_bio_destructor(struct bio *bio)
6883676347aSPeter Osterlund {
6899faf400fSStefan Bader 	struct bio_set *bs = bio->bi_private;
6909faf400fSStefan Bader 
6919faf400fSStefan Bader 	bio_free(bio, bs);
6923676347aSPeter Osterlund }
6933676347aSPeter Osterlund 
6941da177e4SLinus Torvalds /*
6951da177e4SLinus Torvalds  * Creates a little bio that is just does part of a bvec.
6961da177e4SLinus Torvalds  */
6971da177e4SLinus Torvalds static struct bio *split_bvec(struct bio *bio, sector_t sector,
6981da177e4SLinus Torvalds 			      unsigned short idx, unsigned int offset,
6999faf400fSStefan Bader 			      unsigned int len, struct bio_set *bs)
7001da177e4SLinus Torvalds {
7011da177e4SLinus Torvalds 	struct bio *clone;
7021da177e4SLinus Torvalds 	struct bio_vec *bv = bio->bi_io_vec + idx;
7031da177e4SLinus Torvalds 
7049faf400fSStefan Bader 	clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
7053676347aSPeter Osterlund 	clone->bi_destructor = dm_bio_destructor;
7061da177e4SLinus Torvalds 	*clone->bi_io_vec = *bv;
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	clone->bi_sector = sector;
7091da177e4SLinus Torvalds 	clone->bi_bdev = bio->bi_bdev;
710af7e466aSMikulas Patocka 	clone->bi_rw = bio->bi_rw & ~(1 << BIO_RW_BARRIER);
7111da177e4SLinus Torvalds 	clone->bi_vcnt = 1;
7121da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
7131da177e4SLinus Torvalds 	clone->bi_io_vec->bv_offset = offset;
7141da177e4SLinus Torvalds 	clone->bi_io_vec->bv_len = clone->bi_size;
715f3e1d26eSMartin K. Petersen 	clone->bi_flags |= 1 << BIO_CLONED;
7161da177e4SLinus Torvalds 
7179c47008dSMartin K. Petersen 	if (bio_integrity(bio)) {
7189c47008dSMartin K. Petersen 		bio_integrity_clone(clone, bio, GFP_NOIO);
7199c47008dSMartin K. Petersen 		bio_integrity_trim(clone,
7209c47008dSMartin K. Petersen 				   bio_sector_offset(bio, idx, offset), len);
7219c47008dSMartin K. Petersen 	}
7229c47008dSMartin K. Petersen 
7231da177e4SLinus Torvalds 	return clone;
7241da177e4SLinus Torvalds }
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds /*
7271da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
7281da177e4SLinus Torvalds  */
7291da177e4SLinus Torvalds static struct bio *clone_bio(struct bio *bio, sector_t sector,
7301da177e4SLinus Torvalds 			     unsigned short idx, unsigned short bv_count,
7319faf400fSStefan Bader 			     unsigned int len, struct bio_set *bs)
7321da177e4SLinus Torvalds {
7331da177e4SLinus Torvalds 	struct bio *clone;
7341da177e4SLinus Torvalds 
7359faf400fSStefan Bader 	clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
7369faf400fSStefan Bader 	__bio_clone(clone, bio);
737af7e466aSMikulas Patocka 	clone->bi_rw &= ~(1 << BIO_RW_BARRIER);
7389faf400fSStefan Bader 	clone->bi_destructor = dm_bio_destructor;
7391da177e4SLinus Torvalds 	clone->bi_sector = sector;
7401da177e4SLinus Torvalds 	clone->bi_idx = idx;
7411da177e4SLinus Torvalds 	clone->bi_vcnt = idx + bv_count;
7421da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
7431da177e4SLinus Torvalds 	clone->bi_flags &= ~(1 << BIO_SEG_VALID);
7441da177e4SLinus Torvalds 
7459c47008dSMartin K. Petersen 	if (bio_integrity(bio)) {
7469c47008dSMartin K. Petersen 		bio_integrity_clone(clone, bio, GFP_NOIO);
7479c47008dSMartin K. Petersen 
7489c47008dSMartin K. Petersen 		if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
7499c47008dSMartin K. Petersen 			bio_integrity_trim(clone,
7509c47008dSMartin K. Petersen 					   bio_sector_offset(bio, idx, 0), len);
7519c47008dSMartin K. Petersen 	}
7529c47008dSMartin K. Petersen 
7531da177e4SLinus Torvalds 	return clone;
7541da177e4SLinus Torvalds }
7551da177e4SLinus Torvalds 
756512875bdSJun'ichi Nomura static int __clone_and_map(struct clone_info *ci)
7571da177e4SLinus Torvalds {
7581da177e4SLinus Torvalds 	struct bio *clone, *bio = ci->bio;
759512875bdSJun'ichi Nomura 	struct dm_target *ti;
760512875bdSJun'ichi Nomura 	sector_t len = 0, max;
761028867acSAlasdair G Kergon 	struct dm_target_io *tio;
7621da177e4SLinus Torvalds 
763512875bdSJun'ichi Nomura 	ti = dm_table_find_target(ci->map, ci->sector);
764512875bdSJun'ichi Nomura 	if (!dm_target_is_valid(ti))
765512875bdSJun'ichi Nomura 		return -EIO;
766512875bdSJun'ichi Nomura 
767512875bdSJun'ichi Nomura 	max = max_io_len(ci->md, ci->sector, ti);
768512875bdSJun'ichi Nomura 
7691da177e4SLinus Torvalds 	/*
7701da177e4SLinus Torvalds 	 * Allocate a target io object.
7711da177e4SLinus Torvalds 	 */
7721da177e4SLinus Torvalds 	tio = alloc_tio(ci->md);
7731da177e4SLinus Torvalds 	tio->io = ci->io;
7741da177e4SLinus Torvalds 	tio->ti = ti;
7751da177e4SLinus Torvalds 	memset(&tio->info, 0, sizeof(tio->info));
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 	if (ci->sector_count <= max) {
7781da177e4SLinus Torvalds 		/*
7791da177e4SLinus Torvalds 		 * Optimise for the simple case where we can do all of
7801da177e4SLinus Torvalds 		 * the remaining io with a single clone.
7811da177e4SLinus Torvalds 		 */
7821da177e4SLinus Torvalds 		clone = clone_bio(bio, ci->sector, ci->idx,
7839faf400fSStefan Bader 				  bio->bi_vcnt - ci->idx, ci->sector_count,
7849faf400fSStefan Bader 				  ci->md->bs);
7851da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
7861da177e4SLinus Torvalds 		ci->sector_count = 0;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	} else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
7891da177e4SLinus Torvalds 		/*
7901da177e4SLinus Torvalds 		 * There are some bvecs that don't span targets.
7911da177e4SLinus Torvalds 		 * Do as many of these as possible.
7921da177e4SLinus Torvalds 		 */
7931da177e4SLinus Torvalds 		int i;
7941da177e4SLinus Torvalds 		sector_t remaining = max;
7951da177e4SLinus Torvalds 		sector_t bv_len;
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 		for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
7981da177e4SLinus Torvalds 			bv_len = to_sector(bio->bi_io_vec[i].bv_len);
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 			if (bv_len > remaining)
8011da177e4SLinus Torvalds 				break;
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 			remaining -= bv_len;
8041da177e4SLinus Torvalds 			len += bv_len;
8051da177e4SLinus Torvalds 		}
8061da177e4SLinus Torvalds 
8079faf400fSStefan Bader 		clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
8089faf400fSStefan Bader 				  ci->md->bs);
8091da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 		ci->sector += len;
8121da177e4SLinus Torvalds 		ci->sector_count -= len;
8131da177e4SLinus Torvalds 		ci->idx = i;
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	} else {
8161da177e4SLinus Torvalds 		/*
817d2044a94SAlasdair G Kergon 		 * Handle a bvec that must be split between two or more targets.
8181da177e4SLinus Torvalds 		 */
8191da177e4SLinus Torvalds 		struct bio_vec *bv = bio->bi_io_vec + ci->idx;
820d2044a94SAlasdair G Kergon 		sector_t remaining = to_sector(bv->bv_len);
821d2044a94SAlasdair G Kergon 		unsigned int offset = 0;
8221da177e4SLinus Torvalds 
823d2044a94SAlasdair G Kergon 		do {
824d2044a94SAlasdair G Kergon 			if (offset) {
8251da177e4SLinus Torvalds 				ti = dm_table_find_target(ci->map, ci->sector);
826512875bdSJun'ichi Nomura 				if (!dm_target_is_valid(ti))
827512875bdSJun'ichi Nomura 					return -EIO;
828512875bdSJun'ichi Nomura 
829d2044a94SAlasdair G Kergon 				max = max_io_len(ci->md, ci->sector, ti);
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 				tio = alloc_tio(ci->md);
8321da177e4SLinus Torvalds 				tio->io = ci->io;
8331da177e4SLinus Torvalds 				tio->ti = ti;
8341da177e4SLinus Torvalds 				memset(&tio->info, 0, sizeof(tio->info));
835d2044a94SAlasdair G Kergon 			}
836d2044a94SAlasdair G Kergon 
837d2044a94SAlasdair G Kergon 			len = min(remaining, max);
838d2044a94SAlasdair G Kergon 
839d2044a94SAlasdair G Kergon 			clone = split_bvec(bio, ci->sector, ci->idx,
8409faf400fSStefan Bader 					   bv->bv_offset + offset, len,
8419faf400fSStefan Bader 					   ci->md->bs);
842d2044a94SAlasdair G Kergon 
8431da177e4SLinus Torvalds 			__map_bio(ti, clone, tio);
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 			ci->sector += len;
8461da177e4SLinus Torvalds 			ci->sector_count -= len;
847d2044a94SAlasdair G Kergon 			offset += to_bytes(len);
848d2044a94SAlasdair G Kergon 		} while (remaining -= len);
849d2044a94SAlasdair G Kergon 
8501da177e4SLinus Torvalds 		ci->idx++;
8511da177e4SLinus Torvalds 	}
852512875bdSJun'ichi Nomura 
853512875bdSJun'ichi Nomura 	return 0;
8541da177e4SLinus Torvalds }
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds /*
8578a53c28dSMikulas Patocka  * Split the bio into several clones and submit it to targets.
8581da177e4SLinus Torvalds  */
859f0b9a450SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
8601da177e4SLinus Torvalds {
8611da177e4SLinus Torvalds 	struct clone_info ci;
862512875bdSJun'ichi Nomura 	int error = 0;
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	ci.map = dm_get_table(md);
865f0b9a450SMikulas Patocka 	if (unlikely(!ci.map)) {
866af7e466aSMikulas Patocka 		if (!bio_barrier(bio))
867f0b9a450SMikulas Patocka 			bio_io_error(bio);
868af7e466aSMikulas Patocka 		else
869af7e466aSMikulas Patocka 			md->barrier_error = -EIO;
870f0b9a450SMikulas Patocka 		return;
871f0b9a450SMikulas Patocka 	}
872692d0eb9SMikulas Patocka 
8731da177e4SLinus Torvalds 	ci.md = md;
8741da177e4SLinus Torvalds 	ci.bio = bio;
8751da177e4SLinus Torvalds 	ci.io = alloc_io(md);
8761da177e4SLinus Torvalds 	ci.io->error = 0;
8771da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
8781da177e4SLinus Torvalds 	ci.io->bio = bio;
8791da177e4SLinus Torvalds 	ci.io->md = md;
8801da177e4SLinus Torvalds 	ci.sector = bio->bi_sector;
8811da177e4SLinus Torvalds 	ci.sector_count = bio_sectors(bio);
8821da177e4SLinus Torvalds 	ci.idx = bio->bi_idx;
8831da177e4SLinus Torvalds 
8843eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
885512875bdSJun'ichi Nomura 	while (ci.sector_count && !error)
886512875bdSJun'ichi Nomura 		error = __clone_and_map(&ci);
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	/* drop the extra reference count */
889512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
8901da177e4SLinus Torvalds 	dm_table_put(ci.map);
8911da177e4SLinus Torvalds }
8921da177e4SLinus Torvalds /*-----------------------------------------------------------------
8931da177e4SLinus Torvalds  * CRUD END
8941da177e4SLinus Torvalds  *---------------------------------------------------------------*/
8951da177e4SLinus Torvalds 
896f6fccb12SMilan Broz static int dm_merge_bvec(struct request_queue *q,
897f6fccb12SMilan Broz 			 struct bvec_merge_data *bvm,
898f6fccb12SMilan Broz 			 struct bio_vec *biovec)
899f6fccb12SMilan Broz {
900f6fccb12SMilan Broz 	struct mapped_device *md = q->queuedata;
901f6fccb12SMilan Broz 	struct dm_table *map = dm_get_table(md);
902f6fccb12SMilan Broz 	struct dm_target *ti;
903f6fccb12SMilan Broz 	sector_t max_sectors;
9045037108aSMikulas Patocka 	int max_size = 0;
905f6fccb12SMilan Broz 
906f6fccb12SMilan Broz 	if (unlikely(!map))
9075037108aSMikulas Patocka 		goto out;
908f6fccb12SMilan Broz 
909f6fccb12SMilan Broz 	ti = dm_table_find_target(map, bvm->bi_sector);
910b01cd5acSMikulas Patocka 	if (!dm_target_is_valid(ti))
911b01cd5acSMikulas Patocka 		goto out_table;
912f6fccb12SMilan Broz 
913f6fccb12SMilan Broz 	/*
914f6fccb12SMilan Broz 	 * Find maximum amount of I/O that won't need splitting
915f6fccb12SMilan Broz 	 */
916f6fccb12SMilan Broz 	max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
917f6fccb12SMilan Broz 			  (sector_t) BIO_MAX_SECTORS);
918f6fccb12SMilan Broz 	max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
919f6fccb12SMilan Broz 	if (max_size < 0)
920f6fccb12SMilan Broz 		max_size = 0;
921f6fccb12SMilan Broz 
922f6fccb12SMilan Broz 	/*
923f6fccb12SMilan Broz 	 * merge_bvec_fn() returns number of bytes
924f6fccb12SMilan Broz 	 * it can accept at this offset
925f6fccb12SMilan Broz 	 * max is precomputed maximal io size
926f6fccb12SMilan Broz 	 */
927f6fccb12SMilan Broz 	if (max_size && ti->type->merge)
928f6fccb12SMilan Broz 		max_size = ti->type->merge(ti, bvm, biovec, max_size);
929f6fccb12SMilan Broz 
930b01cd5acSMikulas Patocka out_table:
9315037108aSMikulas Patocka 	dm_table_put(map);
9325037108aSMikulas Patocka 
9335037108aSMikulas Patocka out:
934f6fccb12SMilan Broz 	/*
935f6fccb12SMilan Broz 	 * Always allow an entire first page
936f6fccb12SMilan Broz 	 */
937f6fccb12SMilan Broz 	if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
938f6fccb12SMilan Broz 		max_size = biovec->bv_len;
939f6fccb12SMilan Broz 
940f6fccb12SMilan Broz 	return max_size;
941f6fccb12SMilan Broz }
942f6fccb12SMilan Broz 
9431da177e4SLinus Torvalds /*
9441da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
9451da177e4SLinus Torvalds  * dm_merge_bvec.
9461da177e4SLinus Torvalds  */
947165125e1SJens Axboe static int dm_request(struct request_queue *q, struct bio *bio)
9481da177e4SLinus Torvalds {
94912f03a49SKevin Corry 	int rw = bio_data_dir(bio);
9501da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
951c9959059STejun Heo 	int cpu;
9521da177e4SLinus Torvalds 
9532ca3310eSAlasdair G Kergon 	down_read(&md->io_lock);
9541da177e4SLinus Torvalds 
955074a7acaSTejun Heo 	cpu = part_stat_lock();
956074a7acaSTejun Heo 	part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
957074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
958074a7acaSTejun Heo 	part_stat_unlock();
95912f03a49SKevin Corry 
9601da177e4SLinus Torvalds 	/*
9611eb787ecSAlasdair G Kergon 	 * If we're suspended or the thread is processing barriers
9621eb787ecSAlasdair G Kergon 	 * we have to queue this io for later.
9631da177e4SLinus Torvalds 	 */
964af7e466aSMikulas Patocka 	if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
965af7e466aSMikulas Patocka 	    unlikely(bio_barrier(bio))) {
9662ca3310eSAlasdair G Kergon 		up_read(&md->io_lock);
9671da177e4SLinus Torvalds 
96854d9a1b4SAlasdair G Kergon 		if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
96954d9a1b4SAlasdair G Kergon 		    bio_rw(bio) == READA) {
97054d9a1b4SAlasdair G Kergon 			bio_io_error(bio);
97154d9a1b4SAlasdair G Kergon 			return 0;
97254d9a1b4SAlasdair G Kergon 		}
9731da177e4SLinus Torvalds 
97492c63902SMikulas Patocka 		queue_io(md, bio);
9751da177e4SLinus Torvalds 
97692c63902SMikulas Patocka 		return 0;
9771da177e4SLinus Torvalds 	}
9781da177e4SLinus Torvalds 
979f0b9a450SMikulas Patocka 	__split_and_process_bio(md, bio);
9802ca3310eSAlasdair G Kergon 	up_read(&md->io_lock);
981f0b9a450SMikulas Patocka 	return 0;
9821da177e4SLinus Torvalds }
9831da177e4SLinus Torvalds 
984165125e1SJens Axboe static void dm_unplug_all(struct request_queue *q)
9851da177e4SLinus Torvalds {
9861da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
9871da177e4SLinus Torvalds 	struct dm_table *map = dm_get_table(md);
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	if (map) {
9901da177e4SLinus Torvalds 		dm_table_unplug_all(map);
9911da177e4SLinus Torvalds 		dm_table_put(map);
9921da177e4SLinus Torvalds 	}
9931da177e4SLinus Torvalds }
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
9961da177e4SLinus Torvalds {
9978a57dfc6SChandra Seetharaman 	int r = bdi_bits;
9988a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
9998a57dfc6SChandra Seetharaman 	struct dm_table *map;
10001da177e4SLinus Torvalds 
10011eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
10028a57dfc6SChandra Seetharaman 		map = dm_get_table(md);
10038a57dfc6SChandra Seetharaman 		if (map) {
10041da177e4SLinus Torvalds 			r = dm_table_any_congested(map, bdi_bits);
10051da177e4SLinus Torvalds 			dm_table_put(map);
10068a57dfc6SChandra Seetharaman 		}
10078a57dfc6SChandra Seetharaman 	}
10088a57dfc6SChandra Seetharaman 
10091da177e4SLinus Torvalds 	return r;
10101da177e4SLinus Torvalds }
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds /*-----------------------------------------------------------------
10131da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
10141da177e4SLinus Torvalds  *---------------------------------------------------------------*/
10151da177e4SLinus Torvalds static DEFINE_IDR(_minor_idr);
10161da177e4SLinus Torvalds 
10172b06cfffSAlasdair G Kergon static void free_minor(int minor)
10181da177e4SLinus Torvalds {
1019f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
10201da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1021f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
10221da177e4SLinus Torvalds }
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds /*
10251da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
10261da177e4SLinus Torvalds  */
1027cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
10281da177e4SLinus Torvalds {
10291da177e4SLinus Torvalds 	int r, m;
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
10321da177e4SLinus Torvalds 		return -EINVAL;
10331da177e4SLinus Torvalds 
103462f75c2fSJeff Mahoney 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
103562f75c2fSJeff Mahoney 	if (!r)
103662f75c2fSJeff Mahoney 		return -ENOMEM;
103762f75c2fSJeff Mahoney 
1038f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 	if (idr_find(&_minor_idr, minor)) {
10411da177e4SLinus Torvalds 		r = -EBUSY;
10421da177e4SLinus Torvalds 		goto out;
10431da177e4SLinus Torvalds 	}
10441da177e4SLinus Torvalds 
1045ba61fdd1SJeff Mahoney 	r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
104662f75c2fSJeff Mahoney 	if (r)
10471da177e4SLinus Torvalds 		goto out;
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds 	if (m != minor) {
10501da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
10511da177e4SLinus Torvalds 		r = -EBUSY;
10521da177e4SLinus Torvalds 		goto out;
10531da177e4SLinus Torvalds 	}
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds out:
1056f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
10571da177e4SLinus Torvalds 	return r;
10581da177e4SLinus Torvalds }
10591da177e4SLinus Torvalds 
1060cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
10611da177e4SLinus Torvalds {
10622b06cfffSAlasdair G Kergon 	int r, m;
10631da177e4SLinus Torvalds 
10641da177e4SLinus Torvalds 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
106562f75c2fSJeff Mahoney 	if (!r)
106662f75c2fSJeff Mahoney 		return -ENOMEM;
106762f75c2fSJeff Mahoney 
1068f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
10691da177e4SLinus Torvalds 
1070ba61fdd1SJeff Mahoney 	r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
1071cf13ab8eSFrederik Deweerdt 	if (r)
10721da177e4SLinus Torvalds 		goto out;
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds 	if (m >= (1 << MINORBITS)) {
10751da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
10761da177e4SLinus Torvalds 		r = -ENOSPC;
10771da177e4SLinus Torvalds 		goto out;
10781da177e4SLinus Torvalds 	}
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds 	*minor = m;
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds out:
1083f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
10841da177e4SLinus Torvalds 	return r;
10851da177e4SLinus Torvalds }
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops;
10881da177e4SLinus Torvalds 
108953d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
109053d5914fSMikulas Patocka 
10911da177e4SLinus Torvalds /*
10921da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
10931da177e4SLinus Torvalds  */
10942b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
10951da177e4SLinus Torvalds {
10961da177e4SLinus Torvalds 	int r;
1097cf13ab8eSFrederik Deweerdt 	struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1098ba61fdd1SJeff Mahoney 	void *old_md;
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	if (!md) {
11011da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
11021da177e4SLinus Torvalds 		return NULL;
11031da177e4SLinus Torvalds 	}
11041da177e4SLinus Torvalds 
110510da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
11066ed7ade8SMilan Broz 		goto bad_module_get;
110710da4f79SJeff Mahoney 
11081da177e4SLinus Torvalds 	/* get a minor number for the dev */
11092b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1110cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
11112b06cfffSAlasdair G Kergon 	else
1112cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
11131da177e4SLinus Torvalds 	if (r < 0)
11146ed7ade8SMilan Broz 		goto bad_minor;
11151da177e4SLinus Torvalds 
11162ca3310eSAlasdair G Kergon 	init_rwsem(&md->io_lock);
1117e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1118022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
11191da177e4SLinus Torvalds 	rwlock_init(&md->map_lock);
11201da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
11215c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
11221da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
11237a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
11247a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
11257a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds 	md->queue = blk_alloc_queue(GFP_KERNEL);
11281da177e4SLinus Torvalds 	if (!md->queue)
11296ed7ade8SMilan Broz 		goto bad_queue;
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 	md->queue->queuedata = md;
11321da177e4SLinus Torvalds 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
11331da177e4SLinus Torvalds 	md->queue->backing_dev_info.congested_data = md;
11341da177e4SLinus Torvalds 	blk_queue_make_request(md->queue, dm_request);
113599360b4cSMikulas Patocka 	blk_queue_ordered(md->queue, QUEUE_ORDERED_DRAIN, NULL);
1136daef265fSJens Axboe 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
11371da177e4SLinus Torvalds 	md->queue->unplug_fn = dm_unplug_all;
1138f6fccb12SMilan Broz 	blk_queue_merge_bvec(md->queue, dm_merge_bvec);
11391da177e4SLinus Torvalds 
114093d2341cSMatthew Dobson 	md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
11411da177e4SLinus Torvalds 	if (!md->io_pool)
11426ed7ade8SMilan Broz 		goto bad_io_pool;
11431da177e4SLinus Torvalds 
114493d2341cSMatthew Dobson 	md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
11451da177e4SLinus Torvalds 	if (!md->tio_pool)
11466ed7ade8SMilan Broz 		goto bad_tio_pool;
11471da177e4SLinus Torvalds 
1148bb799ca0SJens Axboe 	md->bs = bioset_create(16, 0);
11499faf400fSStefan Bader 	if (!md->bs)
11509faf400fSStefan Bader 		goto bad_no_bioset;
11519faf400fSStefan Bader 
11521da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
11531da177e4SLinus Torvalds 	if (!md->disk)
11546ed7ade8SMilan Broz 		goto bad_disk;
11551da177e4SLinus Torvalds 
1156f0b04115SJeff Mahoney 	atomic_set(&md->pending, 0);
1157f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
115853d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1159f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
1160f0b04115SJeff Mahoney 
11611da177e4SLinus Torvalds 	md->disk->major = _major;
11621da177e4SLinus Torvalds 	md->disk->first_minor = minor;
11631da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
11641da177e4SLinus Torvalds 	md->disk->queue = md->queue;
11651da177e4SLinus Torvalds 	md->disk->private_data = md;
11661da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
11671da177e4SLinus Torvalds 	add_disk(md->disk);
11687e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
11691da177e4SLinus Torvalds 
1170304f3f6aSMilan Broz 	md->wq = create_singlethread_workqueue("kdmflush");
1171304f3f6aSMilan Broz 	if (!md->wq)
1172304f3f6aSMilan Broz 		goto bad_thread;
1173304f3f6aSMilan Broz 
1174ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1175f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1176ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1177f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1178ba61fdd1SJeff Mahoney 
1179ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1180ba61fdd1SJeff Mahoney 
11811da177e4SLinus Torvalds 	return md;
11821da177e4SLinus Torvalds 
1183304f3f6aSMilan Broz bad_thread:
1184304f3f6aSMilan Broz 	put_disk(md->disk);
11856ed7ade8SMilan Broz bad_disk:
11869faf400fSStefan Bader 	bioset_free(md->bs);
11879faf400fSStefan Bader bad_no_bioset:
11881da177e4SLinus Torvalds 	mempool_destroy(md->tio_pool);
11896ed7ade8SMilan Broz bad_tio_pool:
11901da177e4SLinus Torvalds 	mempool_destroy(md->io_pool);
11916ed7ade8SMilan Broz bad_io_pool:
11921312f40eSAl Viro 	blk_cleanup_queue(md->queue);
11936ed7ade8SMilan Broz bad_queue:
11941da177e4SLinus Torvalds 	free_minor(minor);
11956ed7ade8SMilan Broz bad_minor:
119610da4f79SJeff Mahoney 	module_put(THIS_MODULE);
11976ed7ade8SMilan Broz bad_module_get:
11981da177e4SLinus Torvalds 	kfree(md);
11991da177e4SLinus Torvalds 	return NULL;
12001da177e4SLinus Torvalds }
12011da177e4SLinus Torvalds 
1202ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1203ae9da83fSJun'ichi Nomura 
12041da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
12051da177e4SLinus Torvalds {
1206f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
120763d94e48SJun'ichi Nomura 
1208d9dde59bSJun'ichi Nomura 	if (md->suspended_bdev) {
1209ae9da83fSJun'ichi Nomura 		unlock_fs(md);
1210d9dde59bSJun'ichi Nomura 		bdput(md->suspended_bdev);
1211d9dde59bSJun'ichi Nomura 	}
1212304f3f6aSMilan Broz 	destroy_workqueue(md->wq);
12131da177e4SLinus Torvalds 	mempool_destroy(md->tio_pool);
12141da177e4SLinus Torvalds 	mempool_destroy(md->io_pool);
12159faf400fSStefan Bader 	bioset_free(md->bs);
12169c47008dSMartin K. Petersen 	blk_integrity_unregister(md->disk);
12171da177e4SLinus Torvalds 	del_gendisk(md->disk);
121863d94e48SJun'ichi Nomura 	free_minor(minor);
1219fba9f90eSJeff Mahoney 
1220fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
1221fba9f90eSJeff Mahoney 	md->disk->private_data = NULL;
1222fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
1223fba9f90eSJeff Mahoney 
12241da177e4SLinus Torvalds 	put_disk(md->disk);
12251312f40eSAl Viro 	blk_cleanup_queue(md->queue);
122610da4f79SJeff Mahoney 	module_put(THIS_MODULE);
12271da177e4SLinus Torvalds 	kfree(md);
12281da177e4SLinus Torvalds }
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds /*
12311da177e4SLinus Torvalds  * Bind a table to the device.
12321da177e4SLinus Torvalds  */
12331da177e4SLinus Torvalds static void event_callback(void *context)
12341da177e4SLinus Torvalds {
12357a8c3d3bSMike Anderson 	unsigned long flags;
12367a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
12371da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
12381da177e4SLinus Torvalds 
12397a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
12407a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
12417a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
12427a8c3d3bSMike Anderson 
1243ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
12447a8c3d3bSMike Anderson 
12451da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
12461da177e4SLinus Torvalds 	wake_up(&md->eventq);
12471da177e4SLinus Torvalds }
12481da177e4SLinus Torvalds 
12494e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
12501da177e4SLinus Torvalds {
12514e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
12521da177e4SLinus Torvalds 
12531b1dcc1bSJes Sorensen 	mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
1254e39e2e95SAlasdair G Kergon 	i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
12551b1dcc1bSJes Sorensen 	mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
12561da177e4SLinus Torvalds }
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds static int __bind(struct mapped_device *md, struct dm_table *t)
12591da177e4SLinus Torvalds {
1260165125e1SJens Axboe 	struct request_queue *q = md->queue;
12611da177e4SLinus Torvalds 	sector_t size;
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds 	size = dm_table_get_size(t);
12643ac51e74SDarrick J. Wong 
12653ac51e74SDarrick J. Wong 	/*
12663ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
12673ac51e74SDarrick J. Wong 	 */
12683ac51e74SDarrick J. Wong 	if (size != get_capacity(md->disk))
12693ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
12703ac51e74SDarrick J. Wong 
1271bfa152faSJun'ichi Nomura 	if (md->suspended_bdev)
12724e90188bSAlasdair G Kergon 		__set_size(md, size);
12731da177e4SLinus Torvalds 
1274d5816876SMikulas Patocka 	if (!size) {
1275d5816876SMikulas Patocka 		dm_table_destroy(t);
1276d5816876SMikulas Patocka 		return 0;
1277d5816876SMikulas Patocka 	}
1278d5816876SMikulas Patocka 
1279cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
12802ca3310eSAlasdair G Kergon 
12812ca3310eSAlasdair G Kergon 	write_lock(&md->map_lock);
12822ca3310eSAlasdair G Kergon 	md->map = t;
12831da177e4SLinus Torvalds 	dm_table_set_restrictions(t, q);
12842ca3310eSAlasdair G Kergon 	write_unlock(&md->map_lock);
12852ca3310eSAlasdair G Kergon 
12861da177e4SLinus Torvalds 	return 0;
12871da177e4SLinus Torvalds }
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds static void __unbind(struct mapped_device *md)
12901da177e4SLinus Torvalds {
12911da177e4SLinus Torvalds 	struct dm_table *map = md->map;
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 	if (!map)
12941da177e4SLinus Torvalds 		return;
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
12971da177e4SLinus Torvalds 	write_lock(&md->map_lock);
12981da177e4SLinus Torvalds 	md->map = NULL;
12991da177e4SLinus Torvalds 	write_unlock(&md->map_lock);
1300d5816876SMikulas Patocka 	dm_table_destroy(map);
13011da177e4SLinus Torvalds }
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds /*
13041da177e4SLinus Torvalds  * Constructor for a new device.
13051da177e4SLinus Torvalds  */
13062b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
13071da177e4SLinus Torvalds {
13081da177e4SLinus Torvalds 	struct mapped_device *md;
13091da177e4SLinus Torvalds 
13102b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
13111da177e4SLinus Torvalds 	if (!md)
13121da177e4SLinus Torvalds 		return -ENXIO;
13131da177e4SLinus Torvalds 
1314784aae73SMilan Broz 	dm_sysfs_init(md);
1315784aae73SMilan Broz 
13161da177e4SLinus Torvalds 	*result = md;
13171da177e4SLinus Torvalds 	return 0;
13181da177e4SLinus Torvalds }
13191da177e4SLinus Torvalds 
1320637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev)
13211da177e4SLinus Torvalds {
13221da177e4SLinus Torvalds 	struct mapped_device *md;
13231da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
13261da177e4SLinus Torvalds 		return NULL;
13271da177e4SLinus Torvalds 
1328f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
1331fba9f90eSJeff Mahoney 	if (md && (md == MINOR_ALLOCED ||
1332f331c029STejun Heo 		   (MINOR(disk_devt(dm_disk(md))) != minor) ||
1333fba9f90eSJeff Mahoney 		   test_bit(DMF_FREEING, &md->flags))) {
1334637842cfSDavid Teigland 		md = NULL;
1335fba9f90eSJeff Mahoney 		goto out;
1336fba9f90eSJeff Mahoney 	}
13371da177e4SLinus Torvalds 
1338fba9f90eSJeff Mahoney out:
1339f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
13401da177e4SLinus Torvalds 
1341637842cfSDavid Teigland 	return md;
1342637842cfSDavid Teigland }
1343637842cfSDavid Teigland 
1344d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev)
1345d229a958SDavid Teigland {
1346d229a958SDavid Teigland 	struct mapped_device *md = dm_find_md(dev);
1347d229a958SDavid Teigland 
1348d229a958SDavid Teigland 	if (md)
1349d229a958SDavid Teigland 		dm_get(md);
1350d229a958SDavid Teigland 
1351d229a958SDavid Teigland 	return md;
1352d229a958SDavid Teigland }
1353d229a958SDavid Teigland 
13549ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
1355637842cfSDavid Teigland {
13569ade92a9SAlasdair G Kergon 	return md->interface_ptr;
13571da177e4SLinus Torvalds }
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
13601da177e4SLinus Torvalds {
13611da177e4SLinus Torvalds 	md->interface_ptr = ptr;
13621da177e4SLinus Torvalds }
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
13651da177e4SLinus Torvalds {
13661da177e4SLinus Torvalds 	atomic_inc(&md->holders);
13671da177e4SLinus Torvalds }
13681da177e4SLinus Torvalds 
136972d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
137072d94861SAlasdair G Kergon {
137172d94861SAlasdair G Kergon 	return md->name;
137272d94861SAlasdair G Kergon }
137372d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
137472d94861SAlasdair G Kergon 
13751da177e4SLinus Torvalds void dm_put(struct mapped_device *md)
13761da177e4SLinus Torvalds {
13771134e5aeSMike Anderson 	struct dm_table *map;
13781da177e4SLinus Torvalds 
1379fba9f90eSJeff Mahoney 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
1380fba9f90eSJeff Mahoney 
1381f32c10b0SJeff Mahoney 	if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
13821134e5aeSMike Anderson 		map = dm_get_table(md);
1383f331c029STejun Heo 		idr_replace(&_minor_idr, MINOR_ALLOCED,
1384f331c029STejun Heo 			    MINOR(disk_devt(dm_disk(md))));
1385fba9f90eSJeff Mahoney 		set_bit(DMF_FREEING, &md->flags);
1386f32c10b0SJeff Mahoney 		spin_unlock(&_minor_lock);
1387cf222b37SAlasdair G Kergon 		if (!dm_suspended(md)) {
13881da177e4SLinus Torvalds 			dm_table_presuspend_targets(map);
13891da177e4SLinus Torvalds 			dm_table_postsuspend_targets(map);
13901da177e4SLinus Torvalds 		}
1391784aae73SMilan Broz 		dm_sysfs_exit(md);
13921134e5aeSMike Anderson 		dm_table_put(map);
1393a1b51e98SMikulas Patocka 		__unbind(md);
13941da177e4SLinus Torvalds 		free_dev(md);
13951da177e4SLinus Torvalds 	}
13961da177e4SLinus Torvalds }
139779eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
13981da177e4SLinus Torvalds 
1399401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
140046125c1cSMilan Broz {
140146125c1cSMilan Broz 	int r = 0;
1402b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
1403b44ebeb0SMikulas Patocka 
1404b44ebeb0SMikulas Patocka 	dm_unplug_all(md->queue);
1405b44ebeb0SMikulas Patocka 
1406b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
140746125c1cSMilan Broz 
140846125c1cSMilan Broz 	while (1) {
1409401600dfSMikulas Patocka 		set_current_state(interruptible);
141046125c1cSMilan Broz 
141146125c1cSMilan Broz 		smp_mb();
141246125c1cSMilan Broz 		if (!atomic_read(&md->pending))
141346125c1cSMilan Broz 			break;
141446125c1cSMilan Broz 
1415401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
1416401600dfSMikulas Patocka 		    signal_pending(current)) {
141746125c1cSMilan Broz 			r = -EINTR;
141846125c1cSMilan Broz 			break;
141946125c1cSMilan Broz 		}
142046125c1cSMilan Broz 
142146125c1cSMilan Broz 		io_schedule();
142246125c1cSMilan Broz 	}
142346125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
142446125c1cSMilan Broz 
1425b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
1426b44ebeb0SMikulas Patocka 
142746125c1cSMilan Broz 	return r;
142846125c1cSMilan Broz }
142946125c1cSMilan Broz 
1430af7e466aSMikulas Patocka static int dm_flush(struct mapped_device *md)
1431af7e466aSMikulas Patocka {
1432af7e466aSMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
1433af7e466aSMikulas Patocka 	return 0;
1434af7e466aSMikulas Patocka }
1435af7e466aSMikulas Patocka 
1436af7e466aSMikulas Patocka static void process_barrier(struct mapped_device *md, struct bio *bio)
1437af7e466aSMikulas Patocka {
1438af7e466aSMikulas Patocka 	int error = dm_flush(md);
1439af7e466aSMikulas Patocka 
1440af7e466aSMikulas Patocka 	if (unlikely(error)) {
1441af7e466aSMikulas Patocka 		bio_endio(bio, error);
1442af7e466aSMikulas Patocka 		return;
1443af7e466aSMikulas Patocka 	}
1444af7e466aSMikulas Patocka 	if (bio_empty_barrier(bio)) {
1445af7e466aSMikulas Patocka 		bio_endio(bio, 0);
1446af7e466aSMikulas Patocka 		return;
1447af7e466aSMikulas Patocka 	}
1448af7e466aSMikulas Patocka 
1449af7e466aSMikulas Patocka 	__split_and_process_bio(md, bio);
1450af7e466aSMikulas Patocka 
1451af7e466aSMikulas Patocka 	error = dm_flush(md);
1452af7e466aSMikulas Patocka 
1453af7e466aSMikulas Patocka 	if (!error && md->barrier_error)
1454af7e466aSMikulas Patocka 		error = md->barrier_error;
1455af7e466aSMikulas Patocka 
1456af7e466aSMikulas Patocka 	if (md->barrier_error != DM_ENDIO_REQUEUE)
1457af7e466aSMikulas Patocka 		bio_endio(bio, error);
1458af7e466aSMikulas Patocka }
1459af7e466aSMikulas Patocka 
14601da177e4SLinus Torvalds /*
14611da177e4SLinus Torvalds  * Process the deferred bios
14621da177e4SLinus Torvalds  */
1463ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
14641da177e4SLinus Torvalds {
1465ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
1466ef208587SMikulas Patocka 						work);
14676d6f10dfSMilan Broz 	struct bio *c;
14681da177e4SLinus Torvalds 
1469ef208587SMikulas Patocka 	down_write(&md->io_lock);
1470ef208587SMikulas Patocka 
14713b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
1472022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
1473022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
1474022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
1475022c2611SMikulas Patocka 
1476df12ee99SAlasdair G Kergon 		if (!c) {
14771eb787ecSAlasdair G Kergon 			clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
1478df12ee99SAlasdair G Kergon 			break;
1479022c2611SMikulas Patocka 		}
148073d410c0SMilan Broz 
14813b00b203SMikulas Patocka 		up_write(&md->io_lock);
14823b00b203SMikulas Patocka 
1483af7e466aSMikulas Patocka 		if (bio_barrier(c))
1484af7e466aSMikulas Patocka 			process_barrier(md, c);
1485af7e466aSMikulas Patocka 		else
1486df12ee99SAlasdair G Kergon 			__split_and_process_bio(md, c);
14873b00b203SMikulas Patocka 
14883b00b203SMikulas Patocka 		down_write(&md->io_lock);
1489df12ee99SAlasdair G Kergon 	}
1490ef208587SMikulas Patocka 
1491ef208587SMikulas Patocka 	up_write(&md->io_lock);
14921da177e4SLinus Torvalds }
14931da177e4SLinus Torvalds 
14949a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
1495304f3f6aSMilan Broz {
14963b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
14973b00b203SMikulas Patocka 	smp_mb__after_clear_bit();
149853d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
1499304f3f6aSMilan Broz }
1500304f3f6aSMilan Broz 
15011da177e4SLinus Torvalds /*
15021da177e4SLinus Torvalds  * Swap in a new table (destroying old one).
15031da177e4SLinus Torvalds  */
15041da177e4SLinus Torvalds int dm_swap_table(struct mapped_device *md, struct dm_table *table)
15051da177e4SLinus Torvalds {
150693c534aeSAlasdair G Kergon 	int r = -EINVAL;
15071da177e4SLinus Torvalds 
1508e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds 	/* device must be suspended */
1511cf222b37SAlasdair G Kergon 	if (!dm_suspended(md))
151293c534aeSAlasdair G Kergon 		goto out;
15131da177e4SLinus Torvalds 
1514bfa152faSJun'ichi Nomura 	/* without bdev, the device size cannot be changed */
1515bfa152faSJun'ichi Nomura 	if (!md->suspended_bdev)
1516bfa152faSJun'ichi Nomura 		if (get_capacity(md->disk) != dm_table_get_size(table))
1517bfa152faSJun'ichi Nomura 			goto out;
1518bfa152faSJun'ichi Nomura 
15191da177e4SLinus Torvalds 	__unbind(md);
15201da177e4SLinus Torvalds 	r = __bind(md, table);
15211da177e4SLinus Torvalds 
152293c534aeSAlasdair G Kergon out:
1523e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
152493c534aeSAlasdair G Kergon 	return r;
15251da177e4SLinus Torvalds }
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds /*
15281da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
15291da177e4SLinus Torvalds  * device.
15301da177e4SLinus Torvalds  */
15312ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
15321da177e4SLinus Torvalds {
1533e39e2e95SAlasdair G Kergon 	int r;
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
1536dfbe03f6SAlasdair G Kergon 
1537e39e2e95SAlasdair G Kergon 	md->frozen_sb = freeze_bdev(md->suspended_bdev);
1538dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
1539cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
1540e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
1541e39e2e95SAlasdair G Kergon 		return r;
1542dfbe03f6SAlasdair G Kergon 	}
1543dfbe03f6SAlasdair G Kergon 
1544aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
1545aa8d7c2fSAlasdair G Kergon 
15461da177e4SLinus Torvalds 	/* don't bdput right now, we don't want the bdev
1547e39e2e95SAlasdair G Kergon 	 * to go away while it is locked.
15481da177e4SLinus Torvalds 	 */
15491da177e4SLinus Torvalds 	return 0;
15501da177e4SLinus Torvalds }
15511da177e4SLinus Torvalds 
15522ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
15531da177e4SLinus Torvalds {
1554aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
1555aa8d7c2fSAlasdair G Kergon 		return;
1556aa8d7c2fSAlasdair G Kergon 
1557e39e2e95SAlasdair G Kergon 	thaw_bdev(md->suspended_bdev, md->frozen_sb);
15581da177e4SLinus Torvalds 	md->frozen_sb = NULL;
1559aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
15601da177e4SLinus Torvalds }
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds /*
15631da177e4SLinus Torvalds  * We need to be able to change a mapping table under a mounted
15641da177e4SLinus Torvalds  * filesystem.  For example we might want to move some data in
15651da177e4SLinus Torvalds  * the background.  Before the table can be swapped with
15661da177e4SLinus Torvalds  * dm_bind_table, dm_suspend must be called to flush any in
15671da177e4SLinus Torvalds  * flight bios and ensure that any further io gets deferred.
15681da177e4SLinus Torvalds  */
1569a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
15701da177e4SLinus Torvalds {
15712ca3310eSAlasdair G Kergon 	struct dm_table *map = NULL;
157246125c1cSMilan Broz 	int r = 0;
1573a3d77d35SKiyoshi Ueda 	int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
15742e93ccc1SKiyoshi Ueda 	int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
15751da177e4SLinus Torvalds 
1576e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
15772ca3310eSAlasdair G Kergon 
157873d410c0SMilan Broz 	if (dm_suspended(md)) {
157973d410c0SMilan Broz 		r = -EINVAL;
1580d287483dSAlasdair G Kergon 		goto out_unlock;
158173d410c0SMilan Broz 	}
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds 	map = dm_get_table(md);
1584cf222b37SAlasdair G Kergon 
15852e93ccc1SKiyoshi Ueda 	/*
15862e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
15872e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
15882e93ccc1SKiyoshi Ueda 	 */
15892e93ccc1SKiyoshi Ueda 	if (noflush)
15902e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
15912e93ccc1SKiyoshi Ueda 
1592436d4108SAlasdair G Kergon 	/* This does not get reverted if there's an error later. */
15931da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
15941da177e4SLinus Torvalds 
1595bfa152faSJun'ichi Nomura 	/* bdget() can stall if the pending I/Os are not flushed */
1596bfa152faSJun'ichi Nomura 	if (!noflush) {
1597e39e2e95SAlasdair G Kergon 		md->suspended_bdev = bdget_disk(md->disk, 0);
1598e39e2e95SAlasdair G Kergon 		if (!md->suspended_bdev) {
1599e39e2e95SAlasdair G Kergon 			DMWARN("bdget failed in dm_suspend");
1600e39e2e95SAlasdair G Kergon 			r = -ENOMEM;
1601f431d966SKiyoshi Ueda 			goto out;
1602e39e2e95SAlasdair G Kergon 		}
1603e39e2e95SAlasdair G Kergon 
16042e93ccc1SKiyoshi Ueda 		/*
16056d6f10dfSMilan Broz 		 * Flush I/O to the device. noflush supersedes do_lockfs,
16066d6f10dfSMilan Broz 		 * because lock_fs() needs to flush I/Os.
16072e93ccc1SKiyoshi Ueda 		 */
16086d6f10dfSMilan Broz 		if (do_lockfs) {
16092ca3310eSAlasdair G Kergon 			r = lock_fs(md);
16102ca3310eSAlasdair G Kergon 			if (r)
16112ca3310eSAlasdair G Kergon 				goto out;
1612aa8d7c2fSAlasdair G Kergon 		}
16136d6f10dfSMilan Broz 	}
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds 	/*
16163b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
16173b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
16183b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
16193b00b203SMikulas Patocka 	 * dm_wq_work.
16203b00b203SMikulas Patocka 	 *
16213b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
16223b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
16233b00b203SMikulas Patocka 	 * __split_and_process_bio from dm_request, we set
16243b00b203SMikulas Patocka 	 * DMF_QUEUE_IO_TO_THREAD.
16253b00b203SMikulas Patocka 	 *
16263b00b203SMikulas Patocka 	 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
16273b00b203SMikulas Patocka 	 * and call flush_workqueue(md->wq). flush_workqueue will wait until
16283b00b203SMikulas Patocka 	 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
16293b00b203SMikulas Patocka 	 * further calls to __split_and_process_bio from dm_wq_work.
16301da177e4SLinus Torvalds 	 */
16312ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
16321eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
16331eb787ecSAlasdair G Kergon 	set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
16342ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
16351da177e4SLinus Torvalds 
16363b00b203SMikulas Patocka 	flush_workqueue(md->wq);
16373b00b203SMikulas Patocka 
16381da177e4SLinus Torvalds 	/*
16393b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
16403b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
16413b00b203SMikulas Patocka 	 * to finish.
16421da177e4SLinus Torvalds 	 */
1643401600dfSMikulas Patocka 	r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
16441da177e4SLinus Torvalds 
16452ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
16466d6f10dfSMilan Broz 	if (noflush)
1647022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
164894d6351eSMilan Broz 	up_write(&md->io_lock);
16492e93ccc1SKiyoshi Ueda 
16501da177e4SLinus Torvalds 	/* were we interrupted ? */
165146125c1cSMilan Broz 	if (r < 0) {
16529a1fb464SMikulas Patocka 		dm_queue_flush(md);
165373d410c0SMilan Broz 
16542ca3310eSAlasdair G Kergon 		unlock_fs(md);
16552e93ccc1SKiyoshi Ueda 		goto out; /* pushback list is already flushed, so skip flush */
16562ca3310eSAlasdair G Kergon 	}
16572ca3310eSAlasdair G Kergon 
16583b00b203SMikulas Patocka 	/*
16593b00b203SMikulas Patocka 	 * If dm_wait_for_completion returned 0, the device is completely
16603b00b203SMikulas Patocka 	 * quiescent now. There is no request-processing activity. All new
16613b00b203SMikulas Patocka 	 * requests are being added to md->deferred list.
16623b00b203SMikulas Patocka 	 */
16633b00b203SMikulas Patocka 
16642ca3310eSAlasdair G Kergon 	dm_table_postsuspend_targets(map);
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
16671da177e4SLinus Torvalds 
16682ca3310eSAlasdair G Kergon out:
1669e39e2e95SAlasdair G Kergon 	if (r && md->suspended_bdev) {
1670e39e2e95SAlasdair G Kergon 		bdput(md->suspended_bdev);
1671e39e2e95SAlasdair G Kergon 		md->suspended_bdev = NULL;
1672e39e2e95SAlasdair G Kergon 	}
1673e39e2e95SAlasdair G Kergon 
16741da177e4SLinus Torvalds 	dm_table_put(map);
1675d287483dSAlasdair G Kergon 
1676d287483dSAlasdair G Kergon out_unlock:
1677e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
1678cf222b37SAlasdair G Kergon 	return r;
16791da177e4SLinus Torvalds }
16801da177e4SLinus Torvalds 
16811da177e4SLinus Torvalds int dm_resume(struct mapped_device *md)
16821da177e4SLinus Torvalds {
1683cf222b37SAlasdair G Kergon 	int r = -EINVAL;
1684cf222b37SAlasdair G Kergon 	struct dm_table *map = NULL;
16851da177e4SLinus Torvalds 
1686e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
16872ca3310eSAlasdair G Kergon 	if (!dm_suspended(md))
1688cf222b37SAlasdair G Kergon 		goto out;
1689cf222b37SAlasdair G Kergon 
1690cf222b37SAlasdair G Kergon 	map = dm_get_table(md);
16912ca3310eSAlasdair G Kergon 	if (!map || !dm_table_get_size(map))
1692cf222b37SAlasdair G Kergon 		goto out;
16931da177e4SLinus Torvalds 
16948757b776SMilan Broz 	r = dm_table_resume_targets(map);
16958757b776SMilan Broz 	if (r)
16968757b776SMilan Broz 		goto out;
16972ca3310eSAlasdair G Kergon 
16989a1fb464SMikulas Patocka 	dm_queue_flush(md);
16992ca3310eSAlasdair G Kergon 
17002ca3310eSAlasdair G Kergon 	unlock_fs(md);
17012ca3310eSAlasdair G Kergon 
1702bfa152faSJun'ichi Nomura 	if (md->suspended_bdev) {
1703e39e2e95SAlasdair G Kergon 		bdput(md->suspended_bdev);
1704e39e2e95SAlasdair G Kergon 		md->suspended_bdev = NULL;
1705bfa152faSJun'ichi Nomura 	}
1706e39e2e95SAlasdair G Kergon 
17072ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
17082ca3310eSAlasdair G Kergon 
17091da177e4SLinus Torvalds 	dm_table_unplug_all(map);
17101da177e4SLinus Torvalds 
171169267a30SAlasdair G Kergon 	dm_kobject_uevent(md);
17128560ed6fSHannes Reinecke 
1713cf222b37SAlasdair G Kergon 	r = 0;
17142ca3310eSAlasdair G Kergon 
1715cf222b37SAlasdair G Kergon out:
1716cf222b37SAlasdair G Kergon 	dm_table_put(map);
1717e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
17182ca3310eSAlasdair G Kergon 
1719cf222b37SAlasdair G Kergon 	return r;
17201da177e4SLinus Torvalds }
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds /*-----------------------------------------------------------------
17231da177e4SLinus Torvalds  * Event notification.
17241da177e4SLinus Torvalds  *---------------------------------------------------------------*/
172569267a30SAlasdair G Kergon void dm_kobject_uevent(struct mapped_device *md)
172669267a30SAlasdair G Kergon {
1727ed9e1982STejun Heo 	kobject_uevent(&disk_to_dev(md->disk)->kobj, KOBJ_CHANGE);
172869267a30SAlasdair G Kergon }
172969267a30SAlasdair G Kergon 
17307a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
17317a8c3d3bSMike Anderson {
17327a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
17337a8c3d3bSMike Anderson }
17347a8c3d3bSMike Anderson 
17351da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
17361da177e4SLinus Torvalds {
17371da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
17381da177e4SLinus Torvalds }
17391da177e4SLinus Torvalds 
17401da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
17411da177e4SLinus Torvalds {
17421da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
17431da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
17441da177e4SLinus Torvalds }
17451da177e4SLinus Torvalds 
17467a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
17477a8c3d3bSMike Anderson {
17487a8c3d3bSMike Anderson 	unsigned long flags;
17497a8c3d3bSMike Anderson 
17507a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
17517a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
17527a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
17537a8c3d3bSMike Anderson }
17547a8c3d3bSMike Anderson 
17551da177e4SLinus Torvalds /*
17561da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
17571da177e4SLinus Torvalds  * count on 'md'.
17581da177e4SLinus Torvalds  */
17591da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
17601da177e4SLinus Torvalds {
17611da177e4SLinus Torvalds 	return md->disk;
17621da177e4SLinus Torvalds }
17631da177e4SLinus Torvalds 
1764784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
1765784aae73SMilan Broz {
1766784aae73SMilan Broz 	return &md->kobj;
1767784aae73SMilan Broz }
1768784aae73SMilan Broz 
1769784aae73SMilan Broz /*
1770784aae73SMilan Broz  * struct mapped_device should not be exported outside of dm.c
1771784aae73SMilan Broz  * so use this check to verify that kobj is part of md structure
1772784aae73SMilan Broz  */
1773784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
1774784aae73SMilan Broz {
1775784aae73SMilan Broz 	struct mapped_device *md;
1776784aae73SMilan Broz 
1777784aae73SMilan Broz 	md = container_of(kobj, struct mapped_device, kobj);
1778784aae73SMilan Broz 	if (&md->kobj != kobj)
1779784aae73SMilan Broz 		return NULL;
1780784aae73SMilan Broz 
1781784aae73SMilan Broz 	dm_get(md);
1782784aae73SMilan Broz 	return md;
1783784aae73SMilan Broz }
1784784aae73SMilan Broz 
17851da177e4SLinus Torvalds int dm_suspended(struct mapped_device *md)
17861da177e4SLinus Torvalds {
17871da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
17881da177e4SLinus Torvalds }
17891da177e4SLinus Torvalds 
17902e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
17912e93ccc1SKiyoshi Ueda {
17922e93ccc1SKiyoshi Ueda 	struct mapped_device *md = dm_table_get_md(ti->table);
17932e93ccc1SKiyoshi Ueda 	int r = __noflush_suspending(md);
17942e93ccc1SKiyoshi Ueda 
17952e93ccc1SKiyoshi Ueda 	dm_put(md);
17962e93ccc1SKiyoshi Ueda 
17972e93ccc1SKiyoshi Ueda 	return r;
17982e93ccc1SKiyoshi Ueda }
17992e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
18002e93ccc1SKiyoshi Ueda 
18011da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops = {
18021da177e4SLinus Torvalds 	.open = dm_blk_open,
18031da177e4SLinus Torvalds 	.release = dm_blk_close,
1804aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
18053ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
18061da177e4SLinus Torvalds 	.owner = THIS_MODULE
18071da177e4SLinus Torvalds };
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo);
18101da177e4SLinus Torvalds 
18111da177e4SLinus Torvalds /*
18121da177e4SLinus Torvalds  * module hooks
18131da177e4SLinus Torvalds  */
18141da177e4SLinus Torvalds module_init(dm_init);
18151da177e4SLinus Torvalds module_exit(dm_exit);
18161da177e4SLinus Torvalds 
18171da177e4SLinus Torvalds module_param(major, uint, 0);
18181da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
18191da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
18201da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
18211da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1822