xref: /openbmc/linux/drivers/md/dm.c (revision 165125e1e480f9510a5ffcfbfee4e3ee38c05f23)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
32b06cfffSAlasdair G Kergon  * Copyright (C) 2004-2006 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"
91da177e4SLinus Torvalds #include "dm-bio-list.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>
23aa129a22SMilan Broz #include <linux/smp_lock.h>
241da177e4SLinus Torvalds 
2572d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
2672d94861SAlasdair G Kergon 
271da177e4SLinus Torvalds static const char *_name = DM_NAME;
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds static unsigned int major = 0;
301da177e4SLinus Torvalds static unsigned int _major = 0;
311da177e4SLinus Torvalds 
32f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
331da177e4SLinus Torvalds /*
341da177e4SLinus Torvalds  * One of these is allocated per bio.
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds struct dm_io {
371da177e4SLinus Torvalds 	struct mapped_device *md;
381da177e4SLinus Torvalds 	int error;
391da177e4SLinus Torvalds 	struct bio *bio;
401da177e4SLinus Torvalds 	atomic_t io_count;
413eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
421da177e4SLinus Torvalds };
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds /*
451da177e4SLinus Torvalds  * One of these is allocated per target within a bio.  Hopefully
461da177e4SLinus Torvalds  * this will be simplified out one day.
471da177e4SLinus Torvalds  */
48028867acSAlasdair G Kergon struct dm_target_io {
491da177e4SLinus Torvalds 	struct dm_io *io;
501da177e4SLinus Torvalds 	struct dm_target *ti;
511da177e4SLinus Torvalds 	union map_info info;
521da177e4SLinus Torvalds };
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds union map_info *dm_get_mapinfo(struct bio *bio)
551da177e4SLinus Torvalds {
561da177e4SLinus Torvalds 	if (bio && bio->bi_private)
57028867acSAlasdair G Kergon 		return &((struct dm_target_io *)bio->bi_private)->info;
581da177e4SLinus Torvalds 	return NULL;
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds 
61ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
62ba61fdd1SJeff Mahoney 
631da177e4SLinus Torvalds /*
641da177e4SLinus Torvalds  * Bits for the md->flags field.
651da177e4SLinus Torvalds  */
661da177e4SLinus Torvalds #define DMF_BLOCK_IO 0
671da177e4SLinus Torvalds #define DMF_SUSPENDED 1
68aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
69fba9f90eSJeff Mahoney #define DMF_FREEING 3
705c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
712e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds struct mapped_device {
742ca3310eSAlasdair G Kergon 	struct rw_semaphore io_lock;
752ca3310eSAlasdair G Kergon 	struct semaphore suspend_lock;
762e93ccc1SKiyoshi Ueda 	spinlock_t pushback_lock;
771da177e4SLinus Torvalds 	rwlock_t map_lock;
781da177e4SLinus Torvalds 	atomic_t holders;
795c6bd75dSAlasdair G Kergon 	atomic_t open_count;
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	unsigned long flags;
821da177e4SLinus Torvalds 
83*165125e1SJens Axboe 	struct request_queue *queue;
841da177e4SLinus Torvalds 	struct gendisk *disk;
857e51f257SMike Anderson 	char name[16];
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	void *interface_ptr;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	/*
901da177e4SLinus Torvalds 	 * A list of ios that arrived while we were suspended.
911da177e4SLinus Torvalds 	 */
921da177e4SLinus Torvalds 	atomic_t pending;
931da177e4SLinus Torvalds 	wait_queue_head_t wait;
941da177e4SLinus Torvalds 	struct bio_list deferred;
952e93ccc1SKiyoshi Ueda 	struct bio_list pushback;
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	/*
981da177e4SLinus Torvalds 	 * The current mapping.
991da177e4SLinus Torvalds 	 */
1001da177e4SLinus Torvalds 	struct dm_table *map;
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 	/*
1031da177e4SLinus Torvalds 	 * io objects are allocated from here.
1041da177e4SLinus Torvalds 	 */
1051da177e4SLinus Torvalds 	mempool_t *io_pool;
1061da177e4SLinus Torvalds 	mempool_t *tio_pool;
1071da177e4SLinus Torvalds 
1089faf400fSStefan Bader 	struct bio_set *bs;
1099faf400fSStefan Bader 
1101da177e4SLinus Torvalds 	/*
1111da177e4SLinus Torvalds 	 * Event handling.
1121da177e4SLinus Torvalds 	 */
1131da177e4SLinus Torvalds 	atomic_t event_nr;
1141da177e4SLinus Torvalds 	wait_queue_head_t eventq;
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	/*
1171da177e4SLinus Torvalds 	 * freeze/thaw support require holding onto a super block
1181da177e4SLinus Torvalds 	 */
1191da177e4SLinus Torvalds 	struct super_block *frozen_sb;
120e39e2e95SAlasdair G Kergon 	struct block_device *suspended_bdev;
1213ac51e74SDarrick J. Wong 
1223ac51e74SDarrick J. Wong 	/* forced geometry settings */
1233ac51e74SDarrick J. Wong 	struct hd_geometry geometry;
1241da177e4SLinus Torvalds };
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds #define MIN_IOS 256
127e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
128e18b890bSChristoph Lameter static struct kmem_cache *_tio_cache;
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds static int __init local_init(void)
1311da177e4SLinus Torvalds {
1321da177e4SLinus Torvalds 	int r;
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
135028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
1361da177e4SLinus Torvalds 	if (!_io_cache)
1371da177e4SLinus Torvalds 		return -ENOMEM;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	/* allocate a slab for the target ios */
140028867acSAlasdair G Kergon 	_tio_cache = KMEM_CACHE(dm_target_io, 0);
1411da177e4SLinus Torvalds 	if (!_tio_cache) {
1421da177e4SLinus Torvalds 		kmem_cache_destroy(_io_cache);
1431da177e4SLinus Torvalds 		return -ENOMEM;
1441da177e4SLinus Torvalds 	}
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	_major = major;
1471da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
1481da177e4SLinus Torvalds 	if (r < 0) {
1491da177e4SLinus Torvalds 		kmem_cache_destroy(_tio_cache);
1501da177e4SLinus Torvalds 		kmem_cache_destroy(_io_cache);
1511da177e4SLinus Torvalds 		return r;
1521da177e4SLinus Torvalds 	}
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	if (!_major)
1551da177e4SLinus Torvalds 		_major = r;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	return 0;
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds static void local_exit(void)
1611da177e4SLinus Torvalds {
1621da177e4SLinus Torvalds 	kmem_cache_destroy(_tio_cache);
1631da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
16400d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	_major = 0;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	DMINFO("cleaned up");
1691da177e4SLinus Torvalds }
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds int (*_inits[])(void) __initdata = {
1721da177e4SLinus Torvalds 	local_init,
1731da177e4SLinus Torvalds 	dm_target_init,
1741da177e4SLinus Torvalds 	dm_linear_init,
1751da177e4SLinus Torvalds 	dm_stripe_init,
1761da177e4SLinus Torvalds 	dm_interface_init,
1771da177e4SLinus Torvalds };
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds void (*_exits[])(void) = {
1801da177e4SLinus Torvalds 	local_exit,
1811da177e4SLinus Torvalds 	dm_target_exit,
1821da177e4SLinus Torvalds 	dm_linear_exit,
1831da177e4SLinus Torvalds 	dm_stripe_exit,
1841da177e4SLinus Torvalds 	dm_interface_exit,
1851da177e4SLinus Torvalds };
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds static int __init dm_init(void)
1881da177e4SLinus Torvalds {
1891da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	int r, i;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
1941da177e4SLinus Torvalds 		r = _inits[i]();
1951da177e4SLinus Torvalds 		if (r)
1961da177e4SLinus Torvalds 			goto bad;
1971da177e4SLinus Torvalds 	}
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	return 0;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds       bad:
2021da177e4SLinus Torvalds 	while (i--)
2031da177e4SLinus Torvalds 		_exits[i]();
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	return r;
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds static void __exit dm_exit(void)
2091da177e4SLinus Torvalds {
2101da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	while (i--)
2131da177e4SLinus Torvalds 		_exits[i]();
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds /*
2171da177e4SLinus Torvalds  * Block device functions
2181da177e4SLinus Torvalds  */
2191da177e4SLinus Torvalds static int dm_blk_open(struct inode *inode, struct file *file)
2201da177e4SLinus Torvalds {
2211da177e4SLinus Torvalds 	struct mapped_device *md;
2221da177e4SLinus Torvalds 
223fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
224fba9f90eSJeff Mahoney 
2251da177e4SLinus Torvalds 	md = inode->i_bdev->bd_disk->private_data;
226fba9f90eSJeff Mahoney 	if (!md)
227fba9f90eSJeff Mahoney 		goto out;
228fba9f90eSJeff Mahoney 
2295c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
2305c6bd75dSAlasdair G Kergon 	    test_bit(DMF_DELETING, &md->flags)) {
231fba9f90eSJeff Mahoney 		md = NULL;
232fba9f90eSJeff Mahoney 		goto out;
233fba9f90eSJeff Mahoney 	}
234fba9f90eSJeff Mahoney 
2351da177e4SLinus Torvalds 	dm_get(md);
2365c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
237fba9f90eSJeff Mahoney 
238fba9f90eSJeff Mahoney out:
239fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
240fba9f90eSJeff Mahoney 
241fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds static int dm_blk_close(struct inode *inode, struct file *file)
2451da177e4SLinus Torvalds {
2461da177e4SLinus Torvalds 	struct mapped_device *md;
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	md = inode->i_bdev->bd_disk->private_data;
2495c6bd75dSAlasdair G Kergon 	atomic_dec(&md->open_count);
2501da177e4SLinus Torvalds 	dm_put(md);
2511da177e4SLinus Torvalds 	return 0;
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds 
2545c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
2555c6bd75dSAlasdair G Kergon {
2565c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
2575c6bd75dSAlasdair G Kergon }
2585c6bd75dSAlasdair G Kergon 
2595c6bd75dSAlasdair G Kergon /*
2605c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
2615c6bd75dSAlasdair G Kergon  */
2625c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md)
2635c6bd75dSAlasdair G Kergon {
2645c6bd75dSAlasdair G Kergon 	int r = 0;
2655c6bd75dSAlasdair G Kergon 
2665c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
2675c6bd75dSAlasdair G Kergon 
2685c6bd75dSAlasdair G Kergon 	if (dm_open_count(md))
2695c6bd75dSAlasdair G Kergon 		r = -EBUSY;
2705c6bd75dSAlasdair G Kergon 	else
2715c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
2725c6bd75dSAlasdair G Kergon 
2735c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
2745c6bd75dSAlasdair G Kergon 
2755c6bd75dSAlasdair G Kergon 	return r;
2765c6bd75dSAlasdair G Kergon }
2775c6bd75dSAlasdair G Kergon 
2783ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
2793ac51e74SDarrick J. Wong {
2803ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
2813ac51e74SDarrick J. Wong 
2823ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
2833ac51e74SDarrick J. Wong }
2843ac51e74SDarrick J. Wong 
285aa129a22SMilan Broz static int dm_blk_ioctl(struct inode *inode, struct file *file,
286aa129a22SMilan Broz 			unsigned int cmd, unsigned long arg)
287aa129a22SMilan Broz {
288aa129a22SMilan Broz 	struct mapped_device *md;
289aa129a22SMilan Broz 	struct dm_table *map;
290aa129a22SMilan Broz 	struct dm_target *tgt;
291aa129a22SMilan Broz 	int r = -ENOTTY;
292aa129a22SMilan Broz 
293aa129a22SMilan Broz 	/* We don't really need this lock, but we do need 'inode'. */
294aa129a22SMilan Broz 	unlock_kernel();
295aa129a22SMilan Broz 
296aa129a22SMilan Broz 	md = inode->i_bdev->bd_disk->private_data;
297aa129a22SMilan Broz 
298aa129a22SMilan Broz 	map = dm_get_table(md);
299aa129a22SMilan Broz 
300aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
301aa129a22SMilan Broz 		goto out;
302aa129a22SMilan Broz 
303aa129a22SMilan Broz 	/* We only support devices that have a single target */
304aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
305aa129a22SMilan Broz 		goto out;
306aa129a22SMilan Broz 
307aa129a22SMilan Broz 	tgt = dm_table_get_target(map, 0);
308aa129a22SMilan Broz 
309aa129a22SMilan Broz 	if (dm_suspended(md)) {
310aa129a22SMilan Broz 		r = -EAGAIN;
311aa129a22SMilan Broz 		goto out;
312aa129a22SMilan Broz 	}
313aa129a22SMilan Broz 
314aa129a22SMilan Broz 	if (tgt->type->ioctl)
315aa129a22SMilan Broz 		r = tgt->type->ioctl(tgt, inode, file, cmd, arg);
316aa129a22SMilan Broz 
317aa129a22SMilan Broz out:
318aa129a22SMilan Broz 	dm_table_put(map);
319aa129a22SMilan Broz 
320aa129a22SMilan Broz 	lock_kernel();
321aa129a22SMilan Broz 	return r;
322aa129a22SMilan Broz }
323aa129a22SMilan Broz 
324028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
329028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
3301da177e4SLinus Torvalds {
3311da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds 
334028867acSAlasdair G Kergon static struct dm_target_io *alloc_tio(struct mapped_device *md)
3351da177e4SLinus Torvalds {
3361da177e4SLinus Torvalds 	return mempool_alloc(md->tio_pool, GFP_NOIO);
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
339028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
3401da177e4SLinus Torvalds {
3411da177e4SLinus Torvalds 	mempool_free(tio, md->tio_pool);
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
3443eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
3453eaf840eSJun'ichi "Nick" Nomura {
3463eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
3473eaf840eSJun'ichi "Nick" Nomura 
3483eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
3493eaf840eSJun'ichi "Nick" Nomura 
3503eaf840eSJun'ichi "Nick" Nomura 	preempt_disable();
3513eaf840eSJun'ichi "Nick" Nomura 	disk_round_stats(dm_disk(md));
3523eaf840eSJun'ichi "Nick" Nomura 	preempt_enable();
3533eaf840eSJun'ichi "Nick" Nomura 	dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
3543eaf840eSJun'ichi "Nick" Nomura }
3553eaf840eSJun'ichi "Nick" Nomura 
3563eaf840eSJun'ichi "Nick" Nomura static int end_io_acct(struct dm_io *io)
3573eaf840eSJun'ichi "Nick" Nomura {
3583eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
3593eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
3603eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
3613eaf840eSJun'ichi "Nick" Nomura 	int pending;
3623eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
3633eaf840eSJun'ichi "Nick" Nomura 
3643eaf840eSJun'ichi "Nick" Nomura 	preempt_disable();
3653eaf840eSJun'ichi "Nick" Nomura 	disk_round_stats(dm_disk(md));
3663eaf840eSJun'ichi "Nick" Nomura 	preempt_enable();
3673eaf840eSJun'ichi "Nick" Nomura 	dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
3683eaf840eSJun'ichi "Nick" Nomura 
3693eaf840eSJun'ichi "Nick" Nomura 	disk_stat_add(dm_disk(md), ticks[rw], duration);
3703eaf840eSJun'ichi "Nick" Nomura 
3713eaf840eSJun'ichi "Nick" Nomura 	return !pending;
3723eaf840eSJun'ichi "Nick" Nomura }
3733eaf840eSJun'ichi "Nick" Nomura 
3741da177e4SLinus Torvalds /*
3751da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
3761da177e4SLinus Torvalds  */
3771da177e4SLinus Torvalds static int queue_io(struct mapped_device *md, struct bio *bio)
3781da177e4SLinus Torvalds {
3792ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
3822ca3310eSAlasdair G Kergon 		up_write(&md->io_lock);
3831da177e4SLinus Torvalds 		return 1;
3841da177e4SLinus Torvalds 	}
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
3871da177e4SLinus Torvalds 
3882ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
3891da177e4SLinus Torvalds 	return 0;		/* deferred successfully */
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds /*
3931da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
3941da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
3951da177e4SLinus Torvalds  * dm_table_put() when finished.
3961da177e4SLinus Torvalds  */
3971da177e4SLinus Torvalds struct dm_table *dm_get_table(struct mapped_device *md)
3981da177e4SLinus Torvalds {
3991da177e4SLinus Torvalds 	struct dm_table *t;
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	read_lock(&md->map_lock);
4021da177e4SLinus Torvalds 	t = md->map;
4031da177e4SLinus Torvalds 	if (t)
4041da177e4SLinus Torvalds 		dm_table_get(t);
4051da177e4SLinus Torvalds 	read_unlock(&md->map_lock);
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	return t;
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds 
4103ac51e74SDarrick J. Wong /*
4113ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
4123ac51e74SDarrick J. Wong  */
4133ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
4143ac51e74SDarrick J. Wong {
4153ac51e74SDarrick J. Wong 	*geo = md->geometry;
4163ac51e74SDarrick J. Wong 
4173ac51e74SDarrick J. Wong 	return 0;
4183ac51e74SDarrick J. Wong }
4193ac51e74SDarrick J. Wong 
4203ac51e74SDarrick J. Wong /*
4213ac51e74SDarrick J. Wong  * Set the geometry of a device.
4223ac51e74SDarrick J. Wong  */
4233ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
4243ac51e74SDarrick J. Wong {
4253ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
4263ac51e74SDarrick J. Wong 
4273ac51e74SDarrick J. Wong 	if (geo->start > sz) {
4283ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
4293ac51e74SDarrick J. Wong 		return -EINVAL;
4303ac51e74SDarrick J. Wong 	}
4313ac51e74SDarrick J. Wong 
4323ac51e74SDarrick J. Wong 	md->geometry = *geo;
4333ac51e74SDarrick J. Wong 
4343ac51e74SDarrick J. Wong 	return 0;
4353ac51e74SDarrick J. Wong }
4363ac51e74SDarrick J. Wong 
4371da177e4SLinus Torvalds /*-----------------------------------------------------------------
4381da177e4SLinus Torvalds  * CRUD START:
4391da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
4401da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
4411da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
4421da177e4SLinus Torvalds  *   interests of getting something for people to use I give
4431da177e4SLinus Torvalds  *   you this clearly demarcated crap.
4441da177e4SLinus Torvalds  *---------------------------------------------------------------*/
4451da177e4SLinus Torvalds 
4462e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
4472e93ccc1SKiyoshi Ueda {
4482e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
4492e93ccc1SKiyoshi Ueda }
4502e93ccc1SKiyoshi Ueda 
4511da177e4SLinus Torvalds /*
4521da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
4531da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
4541da177e4SLinus Torvalds  */
455858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
4561da177e4SLinus Torvalds {
4572e93ccc1SKiyoshi Ueda 	unsigned long flags;
4582e93ccc1SKiyoshi Ueda 
4592e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
4602e93ccc1SKiyoshi Ueda 	if (error && !(io->error > 0 && __noflush_suspending(io->md)))
4611da177e4SLinus Torvalds 		io->error = error;
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
4642e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
4652e93ccc1SKiyoshi Ueda 			/*
4662e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
4672e93ccc1SKiyoshi Ueda 			 * This must be handled before the sleeper on
4682e93ccc1SKiyoshi Ueda 			 * suspend queue merges the pushback list.
4692e93ccc1SKiyoshi Ueda 			 */
4702e93ccc1SKiyoshi Ueda 			spin_lock_irqsave(&io->md->pushback_lock, flags);
4712e93ccc1SKiyoshi Ueda 			if (__noflush_suspending(io->md))
4722e93ccc1SKiyoshi Ueda 				bio_list_add(&io->md->pushback, io->bio);
4732e93ccc1SKiyoshi Ueda 			else
4742e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
4752e93ccc1SKiyoshi Ueda 				io->error = -EIO;
4762e93ccc1SKiyoshi Ueda 			spin_unlock_irqrestore(&io->md->pushback_lock, flags);
4772e93ccc1SKiyoshi Ueda 		}
4782e93ccc1SKiyoshi Ueda 
4793eaf840eSJun'ichi "Nick" Nomura 		if (end_io_acct(io))
4801da177e4SLinus Torvalds 			/* nudge anyone waiting on suspend queue */
4811da177e4SLinus Torvalds 			wake_up(&io->md->wait);
4821da177e4SLinus Torvalds 
4832e93ccc1SKiyoshi Ueda 		if (io->error != DM_ENDIO_REQUEUE) {
4842e93ccc1SKiyoshi Ueda 			blk_add_trace_bio(io->md->queue, io->bio,
4852e93ccc1SKiyoshi Ueda 					  BLK_TA_COMPLETE);
4862056a782SJens Axboe 
4871da177e4SLinus Torvalds 			bio_endio(io->bio, io->bio->bi_size, io->error);
4882e93ccc1SKiyoshi Ueda 		}
4892e93ccc1SKiyoshi Ueda 
4901da177e4SLinus Torvalds 		free_io(io->md, io);
4911da177e4SLinus Torvalds 	}
4921da177e4SLinus Torvalds }
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds static int clone_endio(struct bio *bio, unsigned int done, int error)
4951da177e4SLinus Torvalds {
4961da177e4SLinus Torvalds 	int r = 0;
497028867acSAlasdair G Kergon 	struct dm_target_io *tio = bio->bi_private;
4989faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
4991da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	if (bio->bi_size)
5021da177e4SLinus Torvalds 		return 1;
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	if (!bio_flagged(bio, BIO_UPTODATE) && !error)
5051da177e4SLinus Torvalds 		error = -EIO;
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	if (endio) {
5081da177e4SLinus Torvalds 		r = endio(tio->ti, bio, error, &tio->info);
5092e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
5102e93ccc1SKiyoshi Ueda 			/*
5112e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
5122e93ccc1SKiyoshi Ueda 			 * in dec_pending().
5132e93ccc1SKiyoshi Ueda 			 */
5141da177e4SLinus Torvalds 			error = r;
51545cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
51645cbcd79SKiyoshi Ueda 			/* The target will handle the io */
5171da177e4SLinus Torvalds 			return 1;
51845cbcd79SKiyoshi Ueda 		else if (r) {
51945cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
52045cbcd79SKiyoshi Ueda 			BUG();
52145cbcd79SKiyoshi Ueda 		}
5221da177e4SLinus Torvalds 	}
5231da177e4SLinus Torvalds 
5249faf400fSStefan Bader 	dec_pending(tio->io, error);
5259faf400fSStefan Bader 
5269faf400fSStefan Bader 	/*
5279faf400fSStefan Bader 	 * Store md for cleanup instead of tio which is about to get freed.
5289faf400fSStefan Bader 	 */
5299faf400fSStefan Bader 	bio->bi_private = md->bs;
5309faf400fSStefan Bader 
5311da177e4SLinus Torvalds 	bio_put(bio);
5329faf400fSStefan Bader 	free_tio(md, tio);
5331da177e4SLinus Torvalds 	return r;
5341da177e4SLinus Torvalds }
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds static sector_t max_io_len(struct mapped_device *md,
5371da177e4SLinus Torvalds 			   sector_t sector, struct dm_target *ti)
5381da177e4SLinus Torvalds {
5391da177e4SLinus Torvalds 	sector_t offset = sector - ti->begin;
5401da177e4SLinus Torvalds 	sector_t len = ti->len - offset;
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	/*
5431da177e4SLinus Torvalds 	 * Does the target need to split even further ?
5441da177e4SLinus Torvalds 	 */
5451da177e4SLinus Torvalds 	if (ti->split_io) {
5461da177e4SLinus Torvalds 		sector_t boundary;
5471da177e4SLinus Torvalds 		boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
5481da177e4SLinus Torvalds 			   - offset;
5491da177e4SLinus Torvalds 		if (len > boundary)
5501da177e4SLinus Torvalds 			len = boundary;
5511da177e4SLinus Torvalds 	}
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	return len;
5541da177e4SLinus Torvalds }
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds static void __map_bio(struct dm_target *ti, struct bio *clone,
557028867acSAlasdair G Kergon 		      struct dm_target_io *tio)
5581da177e4SLinus Torvalds {
5591da177e4SLinus Torvalds 	int r;
5602056a782SJens Axboe 	sector_t sector;
5619faf400fSStefan Bader 	struct mapped_device *md;
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	/*
5641da177e4SLinus Torvalds 	 * Sanity checks.
5651da177e4SLinus Torvalds 	 */
5661da177e4SLinus Torvalds 	BUG_ON(!clone->bi_size);
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
5691da177e4SLinus Torvalds 	clone->bi_private = tio;
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	/*
5721da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
5731da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
5741da177e4SLinus Torvalds 	 * this io.
5751da177e4SLinus Torvalds 	 */
5761da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
5772056a782SJens Axboe 	sector = clone->bi_sector;
5781da177e4SLinus Torvalds 	r = ti->type->map(ti, clone, &tio->info);
57945cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
5801da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
5812056a782SJens Axboe 
5822056a782SJens Axboe 		blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
5832056a782SJens Axboe 				    tio->io->bio->bi_bdev->bd_dev, sector,
5842056a782SJens Axboe 				    clone->bi_sector);
5852056a782SJens Axboe 
5861da177e4SLinus Torvalds 		generic_make_request(clone);
5872e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
5882e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
5899faf400fSStefan Bader 		md = tio->io->md;
5909faf400fSStefan Bader 		dec_pending(tio->io, r);
5919faf400fSStefan Bader 		/*
5929faf400fSStefan Bader 		 * Store bio_set for cleanup.
5939faf400fSStefan Bader 		 */
5949faf400fSStefan Bader 		clone->bi_private = md->bs;
5951da177e4SLinus Torvalds 		bio_put(clone);
5969faf400fSStefan Bader 		free_tio(md, tio);
59745cbcd79SKiyoshi Ueda 	} else if (r) {
59845cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
59945cbcd79SKiyoshi Ueda 		BUG();
6001da177e4SLinus Torvalds 	}
6011da177e4SLinus Torvalds }
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds struct clone_info {
6041da177e4SLinus Torvalds 	struct mapped_device *md;
6051da177e4SLinus Torvalds 	struct dm_table *map;
6061da177e4SLinus Torvalds 	struct bio *bio;
6071da177e4SLinus Torvalds 	struct dm_io *io;
6081da177e4SLinus Torvalds 	sector_t sector;
6091da177e4SLinus Torvalds 	sector_t sector_count;
6101da177e4SLinus Torvalds 	unsigned short idx;
6111da177e4SLinus Torvalds };
6121da177e4SLinus Torvalds 
6133676347aSPeter Osterlund static void dm_bio_destructor(struct bio *bio)
6143676347aSPeter Osterlund {
6159faf400fSStefan Bader 	struct bio_set *bs = bio->bi_private;
6169faf400fSStefan Bader 
6179faf400fSStefan Bader 	bio_free(bio, bs);
6183676347aSPeter Osterlund }
6193676347aSPeter Osterlund 
6201da177e4SLinus Torvalds /*
6211da177e4SLinus Torvalds  * Creates a little bio that is just does part of a bvec.
6221da177e4SLinus Torvalds  */
6231da177e4SLinus Torvalds static struct bio *split_bvec(struct bio *bio, sector_t sector,
6241da177e4SLinus Torvalds 			      unsigned short idx, unsigned int offset,
6259faf400fSStefan Bader 			      unsigned int len, struct bio_set *bs)
6261da177e4SLinus Torvalds {
6271da177e4SLinus Torvalds 	struct bio *clone;
6281da177e4SLinus Torvalds 	struct bio_vec *bv = bio->bi_io_vec + idx;
6291da177e4SLinus Torvalds 
6309faf400fSStefan Bader 	clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
6313676347aSPeter Osterlund 	clone->bi_destructor = dm_bio_destructor;
6321da177e4SLinus Torvalds 	*clone->bi_io_vec = *bv;
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	clone->bi_sector = sector;
6351da177e4SLinus Torvalds 	clone->bi_bdev = bio->bi_bdev;
6361da177e4SLinus Torvalds 	clone->bi_rw = bio->bi_rw;
6371da177e4SLinus Torvalds 	clone->bi_vcnt = 1;
6381da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
6391da177e4SLinus Torvalds 	clone->bi_io_vec->bv_offset = offset;
6401da177e4SLinus Torvalds 	clone->bi_io_vec->bv_len = clone->bi_size;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	return clone;
6431da177e4SLinus Torvalds }
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds /*
6461da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
6471da177e4SLinus Torvalds  */
6481da177e4SLinus Torvalds static struct bio *clone_bio(struct bio *bio, sector_t sector,
6491da177e4SLinus Torvalds 			     unsigned short idx, unsigned short bv_count,
6509faf400fSStefan Bader 			     unsigned int len, struct bio_set *bs)
6511da177e4SLinus Torvalds {
6521da177e4SLinus Torvalds 	struct bio *clone;
6531da177e4SLinus Torvalds 
6549faf400fSStefan Bader 	clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
6559faf400fSStefan Bader 	__bio_clone(clone, bio);
6569faf400fSStefan Bader 	clone->bi_destructor = dm_bio_destructor;
6571da177e4SLinus Torvalds 	clone->bi_sector = sector;
6581da177e4SLinus Torvalds 	clone->bi_idx = idx;
6591da177e4SLinus Torvalds 	clone->bi_vcnt = idx + bv_count;
6601da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
6611da177e4SLinus Torvalds 	clone->bi_flags &= ~(1 << BIO_SEG_VALID);
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 	return clone;
6641da177e4SLinus Torvalds }
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds static void __clone_and_map(struct clone_info *ci)
6671da177e4SLinus Torvalds {
6681da177e4SLinus Torvalds 	struct bio *clone, *bio = ci->bio;
6691da177e4SLinus Torvalds 	struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
6701da177e4SLinus Torvalds 	sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
671028867acSAlasdair G Kergon 	struct dm_target_io *tio;
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	/*
6741da177e4SLinus Torvalds 	 * Allocate a target io object.
6751da177e4SLinus Torvalds 	 */
6761da177e4SLinus Torvalds 	tio = alloc_tio(ci->md);
6771da177e4SLinus Torvalds 	tio->io = ci->io;
6781da177e4SLinus Torvalds 	tio->ti = ti;
6791da177e4SLinus Torvalds 	memset(&tio->info, 0, sizeof(tio->info));
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 	if (ci->sector_count <= max) {
6821da177e4SLinus Torvalds 		/*
6831da177e4SLinus Torvalds 		 * Optimise for the simple case where we can do all of
6841da177e4SLinus Torvalds 		 * the remaining io with a single clone.
6851da177e4SLinus Torvalds 		 */
6861da177e4SLinus Torvalds 		clone = clone_bio(bio, ci->sector, ci->idx,
6879faf400fSStefan Bader 				  bio->bi_vcnt - ci->idx, ci->sector_count,
6889faf400fSStefan Bader 				  ci->md->bs);
6891da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
6901da177e4SLinus Torvalds 		ci->sector_count = 0;
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	} else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
6931da177e4SLinus Torvalds 		/*
6941da177e4SLinus Torvalds 		 * There are some bvecs that don't span targets.
6951da177e4SLinus Torvalds 		 * Do as many of these as possible.
6961da177e4SLinus Torvalds 		 */
6971da177e4SLinus Torvalds 		int i;
6981da177e4SLinus Torvalds 		sector_t remaining = max;
6991da177e4SLinus Torvalds 		sector_t bv_len;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 		for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
7021da177e4SLinus Torvalds 			bv_len = to_sector(bio->bi_io_vec[i].bv_len);
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 			if (bv_len > remaining)
7051da177e4SLinus Torvalds 				break;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 			remaining -= bv_len;
7081da177e4SLinus Torvalds 			len += bv_len;
7091da177e4SLinus Torvalds 		}
7101da177e4SLinus Torvalds 
7119faf400fSStefan Bader 		clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
7129faf400fSStefan Bader 				  ci->md->bs);
7131da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 		ci->sector += len;
7161da177e4SLinus Torvalds 		ci->sector_count -= len;
7171da177e4SLinus Torvalds 		ci->idx = i;
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 	} else {
7201da177e4SLinus Torvalds 		/*
721d2044a94SAlasdair G Kergon 		 * Handle a bvec that must be split between two or more targets.
7221da177e4SLinus Torvalds 		 */
7231da177e4SLinus Torvalds 		struct bio_vec *bv = bio->bi_io_vec + ci->idx;
724d2044a94SAlasdair G Kergon 		sector_t remaining = to_sector(bv->bv_len);
725d2044a94SAlasdair G Kergon 		unsigned int offset = 0;
7261da177e4SLinus Torvalds 
727d2044a94SAlasdair G Kergon 		do {
728d2044a94SAlasdair G Kergon 			if (offset) {
7291da177e4SLinus Torvalds 				ti = dm_table_find_target(ci->map, ci->sector);
730d2044a94SAlasdair G Kergon 				max = max_io_len(ci->md, ci->sector, ti);
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 				tio = alloc_tio(ci->md);
7331da177e4SLinus Torvalds 				tio->io = ci->io;
7341da177e4SLinus Torvalds 				tio->ti = ti;
7351da177e4SLinus Torvalds 				memset(&tio->info, 0, sizeof(tio->info));
736d2044a94SAlasdair G Kergon 			}
737d2044a94SAlasdair G Kergon 
738d2044a94SAlasdair G Kergon 			len = min(remaining, max);
739d2044a94SAlasdair G Kergon 
740d2044a94SAlasdair G Kergon 			clone = split_bvec(bio, ci->sector, ci->idx,
7419faf400fSStefan Bader 					   bv->bv_offset + offset, len,
7429faf400fSStefan Bader 					   ci->md->bs);
743d2044a94SAlasdair G Kergon 
7441da177e4SLinus Torvalds 			__map_bio(ti, clone, tio);
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 			ci->sector += len;
7471da177e4SLinus Torvalds 			ci->sector_count -= len;
748d2044a94SAlasdair G Kergon 			offset += to_bytes(len);
749d2044a94SAlasdair G Kergon 		} while (remaining -= len);
750d2044a94SAlasdair G Kergon 
7511da177e4SLinus Torvalds 		ci->idx++;
7521da177e4SLinus Torvalds 	}
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds /*
7561da177e4SLinus Torvalds  * Split the bio into several clones.
7571da177e4SLinus Torvalds  */
7581da177e4SLinus Torvalds static void __split_bio(struct mapped_device *md, struct bio *bio)
7591da177e4SLinus Torvalds {
7601da177e4SLinus Torvalds 	struct clone_info ci;
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	ci.map = dm_get_table(md);
7631da177e4SLinus Torvalds 	if (!ci.map) {
7641da177e4SLinus Torvalds 		bio_io_error(bio, bio->bi_size);
7651da177e4SLinus Torvalds 		return;
7661da177e4SLinus Torvalds 	}
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	ci.md = md;
7691da177e4SLinus Torvalds 	ci.bio = bio;
7701da177e4SLinus Torvalds 	ci.io = alloc_io(md);
7711da177e4SLinus Torvalds 	ci.io->error = 0;
7721da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
7731da177e4SLinus Torvalds 	ci.io->bio = bio;
7741da177e4SLinus Torvalds 	ci.io->md = md;
7751da177e4SLinus Torvalds 	ci.sector = bio->bi_sector;
7761da177e4SLinus Torvalds 	ci.sector_count = bio_sectors(bio);
7771da177e4SLinus Torvalds 	ci.idx = bio->bi_idx;
7781da177e4SLinus Torvalds 
7793eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
7801da177e4SLinus Torvalds 	while (ci.sector_count)
7811da177e4SLinus Torvalds 		__clone_and_map(&ci);
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	/* drop the extra reference count */
7841da177e4SLinus Torvalds 	dec_pending(ci.io, 0);
7851da177e4SLinus Torvalds 	dm_table_put(ci.map);
7861da177e4SLinus Torvalds }
7871da177e4SLinus Torvalds /*-----------------------------------------------------------------
7881da177e4SLinus Torvalds  * CRUD END
7891da177e4SLinus Torvalds  *---------------------------------------------------------------*/
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds /*
7921da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
7931da177e4SLinus Torvalds  * dm_merge_bvec.
7941da177e4SLinus Torvalds  */
795*165125e1SJens Axboe static int dm_request(struct request_queue *q, struct bio *bio)
7961da177e4SLinus Torvalds {
7971da177e4SLinus Torvalds 	int r;
79812f03a49SKevin Corry 	int rw = bio_data_dir(bio);
7991da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
8001da177e4SLinus Torvalds 
80107a83c47SStefan Bader 	/*
80207a83c47SStefan Bader 	 * There is no use in forwarding any barrier request since we can't
80307a83c47SStefan Bader 	 * guarantee it is (or can be) handled by the targets correctly.
80407a83c47SStefan Bader 	 */
80507a83c47SStefan Bader 	if (unlikely(bio_barrier(bio))) {
80607a83c47SStefan Bader 		bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
80707a83c47SStefan Bader 		return 0;
80807a83c47SStefan Bader 	}
80907a83c47SStefan Bader 
8102ca3310eSAlasdair G Kergon 	down_read(&md->io_lock);
8111da177e4SLinus Torvalds 
81212f03a49SKevin Corry 	disk_stat_inc(dm_disk(md), ios[rw]);
81312f03a49SKevin Corry 	disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
81412f03a49SKevin Corry 
8151da177e4SLinus Torvalds 	/*
8161da177e4SLinus Torvalds 	 * If we're suspended we have to queue
8171da177e4SLinus Torvalds 	 * this io for later.
8181da177e4SLinus Torvalds 	 */
8191da177e4SLinus Torvalds 	while (test_bit(DMF_BLOCK_IO, &md->flags)) {
8202ca3310eSAlasdair G Kergon 		up_read(&md->io_lock);
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 		if (bio_rw(bio) == READA) {
8231da177e4SLinus Torvalds 			bio_io_error(bio, bio->bi_size);
8241da177e4SLinus Torvalds 			return 0;
8251da177e4SLinus Torvalds 		}
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 		r = queue_io(md, bio);
8281da177e4SLinus Torvalds 		if (r < 0) {
8291da177e4SLinus Torvalds 			bio_io_error(bio, bio->bi_size);
8301da177e4SLinus Torvalds 			return 0;
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 		} else if (r == 0)
8331da177e4SLinus Torvalds 			return 0;	/* deferred successfully */
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 		/*
8361da177e4SLinus Torvalds 		 * We're in a while loop, because someone could suspend
8371da177e4SLinus Torvalds 		 * before we get to the following read lock.
8381da177e4SLinus Torvalds 		 */
8392ca3310eSAlasdair G Kergon 		down_read(&md->io_lock);
8401da177e4SLinus Torvalds 	}
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	__split_bio(md, bio);
8432ca3310eSAlasdair G Kergon 	up_read(&md->io_lock);
8441da177e4SLinus Torvalds 	return 0;
8451da177e4SLinus Torvalds }
8461da177e4SLinus Torvalds 
847*165125e1SJens Axboe static int dm_flush_all(struct request_queue *q, struct gendisk *disk,
8481da177e4SLinus Torvalds 			sector_t *error_sector)
8491da177e4SLinus Torvalds {
8501da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
8511da177e4SLinus Torvalds 	struct dm_table *map = dm_get_table(md);
8521da177e4SLinus Torvalds 	int ret = -ENXIO;
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	if (map) {
855cf222b37SAlasdair G Kergon 		ret = dm_table_flush_all(map);
8561da177e4SLinus Torvalds 		dm_table_put(map);
8571da177e4SLinus Torvalds 	}
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	return ret;
8601da177e4SLinus Torvalds }
8611da177e4SLinus Torvalds 
862*165125e1SJens Axboe static void dm_unplug_all(struct request_queue *q)
8631da177e4SLinus Torvalds {
8641da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
8651da177e4SLinus Torvalds 	struct dm_table *map = dm_get_table(md);
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	if (map) {
8681da177e4SLinus Torvalds 		dm_table_unplug_all(map);
8691da177e4SLinus Torvalds 		dm_table_put(map);
8701da177e4SLinus Torvalds 	}
8711da177e4SLinus Torvalds }
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
8741da177e4SLinus Torvalds {
8751da177e4SLinus Torvalds 	int r;
8761da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) congested_data;
8771da177e4SLinus Torvalds 	struct dm_table *map = dm_get_table(md);
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
8801da177e4SLinus Torvalds 		r = bdi_bits;
8811da177e4SLinus Torvalds 	else
8821da177e4SLinus Torvalds 		r = dm_table_any_congested(map, bdi_bits);
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	dm_table_put(map);
8851da177e4SLinus Torvalds 	return r;
8861da177e4SLinus Torvalds }
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds /*-----------------------------------------------------------------
8891da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
8901da177e4SLinus Torvalds  *---------------------------------------------------------------*/
8911da177e4SLinus Torvalds static DEFINE_IDR(_minor_idr);
8921da177e4SLinus Torvalds 
8932b06cfffSAlasdair G Kergon static void free_minor(int minor)
8941da177e4SLinus Torvalds {
895f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
8961da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
897f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
8981da177e4SLinus Torvalds }
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds /*
9011da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
9021da177e4SLinus Torvalds  */
9032b06cfffSAlasdair G Kergon static int specific_minor(struct mapped_device *md, int minor)
9041da177e4SLinus Torvalds {
9051da177e4SLinus Torvalds 	int r, m;
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
9081da177e4SLinus Torvalds 		return -EINVAL;
9091da177e4SLinus Torvalds 
91062f75c2fSJeff Mahoney 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
91162f75c2fSJeff Mahoney 	if (!r)
91262f75c2fSJeff Mahoney 		return -ENOMEM;
91362f75c2fSJeff Mahoney 
914f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 	if (idr_find(&_minor_idr, minor)) {
9171da177e4SLinus Torvalds 		r = -EBUSY;
9181da177e4SLinus Torvalds 		goto out;
9191da177e4SLinus Torvalds 	}
9201da177e4SLinus Torvalds 
921ba61fdd1SJeff Mahoney 	r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
92262f75c2fSJeff Mahoney 	if (r)
9231da177e4SLinus Torvalds 		goto out;
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	if (m != minor) {
9261da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
9271da177e4SLinus Torvalds 		r = -EBUSY;
9281da177e4SLinus Torvalds 		goto out;
9291da177e4SLinus Torvalds 	}
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds out:
932f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
9331da177e4SLinus Torvalds 	return r;
9341da177e4SLinus Torvalds }
9351da177e4SLinus Torvalds 
9362b06cfffSAlasdair G Kergon static int next_free_minor(struct mapped_device *md, int *minor)
9371da177e4SLinus Torvalds {
9382b06cfffSAlasdair G Kergon 	int r, m;
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
94162f75c2fSJeff Mahoney 	if (!r)
94262f75c2fSJeff Mahoney 		return -ENOMEM;
94362f75c2fSJeff Mahoney 
944f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
9451da177e4SLinus Torvalds 
946ba61fdd1SJeff Mahoney 	r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
9471da177e4SLinus Torvalds 	if (r) {
9481da177e4SLinus Torvalds 		goto out;
9491da177e4SLinus Torvalds 	}
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	if (m >= (1 << MINORBITS)) {
9521da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
9531da177e4SLinus Torvalds 		r = -ENOSPC;
9541da177e4SLinus Torvalds 		goto out;
9551da177e4SLinus Torvalds 	}
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	*minor = m;
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds out:
960f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
9611da177e4SLinus Torvalds 	return r;
9621da177e4SLinus Torvalds }
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops;
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds /*
9671da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
9681da177e4SLinus Torvalds  */
9692b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
9701da177e4SLinus Torvalds {
9711da177e4SLinus Torvalds 	int r;
9721da177e4SLinus Torvalds 	struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
973ba61fdd1SJeff Mahoney 	void *old_md;
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 	if (!md) {
9761da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
9771da177e4SLinus Torvalds 		return NULL;
9781da177e4SLinus Torvalds 	}
9791da177e4SLinus Torvalds 
98010da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
98110da4f79SJeff Mahoney 		goto bad0;
98210da4f79SJeff Mahoney 
9831da177e4SLinus Torvalds 	/* get a minor number for the dev */
9842b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
9852b06cfffSAlasdair G Kergon 		r = next_free_minor(md, &minor);
9862b06cfffSAlasdair G Kergon 	else
9872b06cfffSAlasdair G Kergon 		r = specific_minor(md, minor);
9881da177e4SLinus Torvalds 	if (r < 0)
9891da177e4SLinus Torvalds 		goto bad1;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	memset(md, 0, sizeof(*md));
9922ca3310eSAlasdair G Kergon 	init_rwsem(&md->io_lock);
9932ca3310eSAlasdair G Kergon 	init_MUTEX(&md->suspend_lock);
9942e93ccc1SKiyoshi Ueda 	spin_lock_init(&md->pushback_lock);
9951da177e4SLinus Torvalds 	rwlock_init(&md->map_lock);
9961da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
9975c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
9981da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds 	md->queue = blk_alloc_queue(GFP_KERNEL);
10011da177e4SLinus Torvalds 	if (!md->queue)
1002e3f6ac61SIshai Rabinovitz 		goto bad1_free_minor;
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	md->queue->queuedata = md;
10051da177e4SLinus Torvalds 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
10061da177e4SLinus Torvalds 	md->queue->backing_dev_info.congested_data = md;
10071da177e4SLinus Torvalds 	blk_queue_make_request(md->queue, dm_request);
1008daef265fSJens Axboe 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
10091da177e4SLinus Torvalds 	md->queue->unplug_fn = dm_unplug_all;
10101da177e4SLinus Torvalds 	md->queue->issue_flush_fn = dm_flush_all;
10111da177e4SLinus Torvalds 
101293d2341cSMatthew Dobson 	md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
10131da177e4SLinus Torvalds 	if (!md->io_pool)
10141da177e4SLinus Torvalds 		goto bad2;
10151da177e4SLinus Torvalds 
101693d2341cSMatthew Dobson 	md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
10171da177e4SLinus Torvalds 	if (!md->tio_pool)
10181da177e4SLinus Torvalds 		goto bad3;
10191da177e4SLinus Torvalds 
10205972511bSJens Axboe 	md->bs = bioset_create(16, 16);
10219faf400fSStefan Bader 	if (!md->bs)
10229faf400fSStefan Bader 		goto bad_no_bioset;
10239faf400fSStefan Bader 
10241da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
10251da177e4SLinus Torvalds 	if (!md->disk)
10261da177e4SLinus Torvalds 		goto bad4;
10271da177e4SLinus Torvalds 
1028f0b04115SJeff Mahoney 	atomic_set(&md->pending, 0);
1029f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
1030f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
1031f0b04115SJeff Mahoney 
10321da177e4SLinus Torvalds 	md->disk->major = _major;
10331da177e4SLinus Torvalds 	md->disk->first_minor = minor;
10341da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
10351da177e4SLinus Torvalds 	md->disk->queue = md->queue;
10361da177e4SLinus Torvalds 	md->disk->private_data = md;
10371da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
10381da177e4SLinus Torvalds 	add_disk(md->disk);
10397e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
10401da177e4SLinus Torvalds 
1041ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1042f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1043ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1044f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1045ba61fdd1SJeff Mahoney 
1046ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1047ba61fdd1SJeff Mahoney 
10481da177e4SLinus Torvalds 	return md;
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds  bad4:
10519faf400fSStefan Bader 	bioset_free(md->bs);
10529faf400fSStefan Bader  bad_no_bioset:
10531da177e4SLinus Torvalds 	mempool_destroy(md->tio_pool);
10541da177e4SLinus Torvalds  bad3:
10551da177e4SLinus Torvalds 	mempool_destroy(md->io_pool);
10561da177e4SLinus Torvalds  bad2:
10571312f40eSAl Viro 	blk_cleanup_queue(md->queue);
1058e3f6ac61SIshai Rabinovitz  bad1_free_minor:
10591da177e4SLinus Torvalds 	free_minor(minor);
10601da177e4SLinus Torvalds  bad1:
106110da4f79SJeff Mahoney 	module_put(THIS_MODULE);
106210da4f79SJeff Mahoney  bad0:
10631da177e4SLinus Torvalds 	kfree(md);
10641da177e4SLinus Torvalds 	return NULL;
10651da177e4SLinus Torvalds }
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
10681da177e4SLinus Torvalds {
10692b06cfffSAlasdair G Kergon 	int minor = md->disk->first_minor;
107063d94e48SJun'ichi Nomura 
1071d9dde59bSJun'ichi Nomura 	if (md->suspended_bdev) {
1072d9dde59bSJun'ichi Nomura 		thaw_bdev(md->suspended_bdev, NULL);
1073d9dde59bSJun'ichi Nomura 		bdput(md->suspended_bdev);
1074d9dde59bSJun'ichi Nomura 	}
10751da177e4SLinus Torvalds 	mempool_destroy(md->tio_pool);
10761da177e4SLinus Torvalds 	mempool_destroy(md->io_pool);
10779faf400fSStefan Bader 	bioset_free(md->bs);
10781da177e4SLinus Torvalds 	del_gendisk(md->disk);
107963d94e48SJun'ichi Nomura 	free_minor(minor);
1080fba9f90eSJeff Mahoney 
1081fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
1082fba9f90eSJeff Mahoney 	md->disk->private_data = NULL;
1083fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
1084fba9f90eSJeff Mahoney 
10851da177e4SLinus Torvalds 	put_disk(md->disk);
10861312f40eSAl Viro 	blk_cleanup_queue(md->queue);
108710da4f79SJeff Mahoney 	module_put(THIS_MODULE);
10881da177e4SLinus Torvalds 	kfree(md);
10891da177e4SLinus Torvalds }
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds /*
10921da177e4SLinus Torvalds  * Bind a table to the device.
10931da177e4SLinus Torvalds  */
10941da177e4SLinus Torvalds static void event_callback(void *context)
10951da177e4SLinus Torvalds {
10961da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
10991da177e4SLinus Torvalds 	wake_up(&md->eventq);
11001da177e4SLinus Torvalds }
11011da177e4SLinus Torvalds 
11024e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
11031da177e4SLinus Torvalds {
11044e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
11051da177e4SLinus Torvalds 
11061b1dcc1bSJes Sorensen 	mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
1107e39e2e95SAlasdair G Kergon 	i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
11081b1dcc1bSJes Sorensen 	mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
11091da177e4SLinus Torvalds }
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds static int __bind(struct mapped_device *md, struct dm_table *t)
11121da177e4SLinus Torvalds {
1113*165125e1SJens Axboe 	struct request_queue *q = md->queue;
11141da177e4SLinus Torvalds 	sector_t size;
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	size = dm_table_get_size(t);
11173ac51e74SDarrick J. Wong 
11183ac51e74SDarrick J. Wong 	/*
11193ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
11203ac51e74SDarrick J. Wong 	 */
11213ac51e74SDarrick J. Wong 	if (size != get_capacity(md->disk))
11223ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
11233ac51e74SDarrick J. Wong 
1124bfa152faSJun'ichi Nomura 	if (md->suspended_bdev)
11254e90188bSAlasdair G Kergon 		__set_size(md, size);
11261da177e4SLinus Torvalds 	if (size == 0)
11271da177e4SLinus Torvalds 		return 0;
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 	dm_table_get(t);
1130cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
11312ca3310eSAlasdair G Kergon 
11322ca3310eSAlasdair G Kergon 	write_lock(&md->map_lock);
11332ca3310eSAlasdair G Kergon 	md->map = t;
11341da177e4SLinus Torvalds 	dm_table_set_restrictions(t, q);
11352ca3310eSAlasdair G Kergon 	write_unlock(&md->map_lock);
11362ca3310eSAlasdair G Kergon 
11371da177e4SLinus Torvalds 	return 0;
11381da177e4SLinus Torvalds }
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds static void __unbind(struct mapped_device *md)
11411da177e4SLinus Torvalds {
11421da177e4SLinus Torvalds 	struct dm_table *map = md->map;
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	if (!map)
11451da177e4SLinus Torvalds 		return;
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
11481da177e4SLinus Torvalds 	write_lock(&md->map_lock);
11491da177e4SLinus Torvalds 	md->map = NULL;
11501da177e4SLinus Torvalds 	write_unlock(&md->map_lock);
11511da177e4SLinus Torvalds 	dm_table_put(map);
11521da177e4SLinus Torvalds }
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds /*
11551da177e4SLinus Torvalds  * Constructor for a new device.
11561da177e4SLinus Torvalds  */
11572b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
11581da177e4SLinus Torvalds {
11591da177e4SLinus Torvalds 	struct mapped_device *md;
11601da177e4SLinus Torvalds 
11612b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
11621da177e4SLinus Torvalds 	if (!md)
11631da177e4SLinus Torvalds 		return -ENXIO;
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	*result = md;
11661da177e4SLinus Torvalds 	return 0;
11671da177e4SLinus Torvalds }
11681da177e4SLinus Torvalds 
1169637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev)
11701da177e4SLinus Torvalds {
11711da177e4SLinus Torvalds 	struct mapped_device *md;
11721da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
11751da177e4SLinus Torvalds 		return NULL;
11761da177e4SLinus Torvalds 
1177f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
1180fba9f90eSJeff Mahoney 	if (md && (md == MINOR_ALLOCED ||
1181fba9f90eSJeff Mahoney 		   (dm_disk(md)->first_minor != minor) ||
1182fba9f90eSJeff Mahoney 		   test_bit(DMF_FREEING, &md->flags))) {
1183637842cfSDavid Teigland 		md = NULL;
1184fba9f90eSJeff Mahoney 		goto out;
1185fba9f90eSJeff Mahoney 	}
11861da177e4SLinus Torvalds 
1187fba9f90eSJeff Mahoney out:
1188f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
11891da177e4SLinus Torvalds 
1190637842cfSDavid Teigland 	return md;
1191637842cfSDavid Teigland }
1192637842cfSDavid Teigland 
1193d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev)
1194d229a958SDavid Teigland {
1195d229a958SDavid Teigland 	struct mapped_device *md = dm_find_md(dev);
1196d229a958SDavid Teigland 
1197d229a958SDavid Teigland 	if (md)
1198d229a958SDavid Teigland 		dm_get(md);
1199d229a958SDavid Teigland 
1200d229a958SDavid Teigland 	return md;
1201d229a958SDavid Teigland }
1202d229a958SDavid Teigland 
12039ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
1204637842cfSDavid Teigland {
12059ade92a9SAlasdair G Kergon 	return md->interface_ptr;
12061da177e4SLinus Torvalds }
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
12091da177e4SLinus Torvalds {
12101da177e4SLinus Torvalds 	md->interface_ptr = ptr;
12111da177e4SLinus Torvalds }
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
12141da177e4SLinus Torvalds {
12151da177e4SLinus Torvalds 	atomic_inc(&md->holders);
12161da177e4SLinus Torvalds }
12171da177e4SLinus Torvalds 
121872d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
121972d94861SAlasdair G Kergon {
122072d94861SAlasdair G Kergon 	return md->name;
122172d94861SAlasdair G Kergon }
122272d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
122372d94861SAlasdair G Kergon 
12241da177e4SLinus Torvalds void dm_put(struct mapped_device *md)
12251da177e4SLinus Torvalds {
12261134e5aeSMike Anderson 	struct dm_table *map;
12271da177e4SLinus Torvalds 
1228fba9f90eSJeff Mahoney 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
1229fba9f90eSJeff Mahoney 
1230f32c10b0SJeff Mahoney 	if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
12311134e5aeSMike Anderson 		map = dm_get_table(md);
1232ba61fdd1SJeff Mahoney 		idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
1233fba9f90eSJeff Mahoney 		set_bit(DMF_FREEING, &md->flags);
1234f32c10b0SJeff Mahoney 		spin_unlock(&_minor_lock);
1235cf222b37SAlasdair G Kergon 		if (!dm_suspended(md)) {
12361da177e4SLinus Torvalds 			dm_table_presuspend_targets(map);
12371da177e4SLinus Torvalds 			dm_table_postsuspend_targets(map);
12381da177e4SLinus Torvalds 		}
12391da177e4SLinus Torvalds 		__unbind(md);
12401134e5aeSMike Anderson 		dm_table_put(map);
12411da177e4SLinus Torvalds 		free_dev(md);
12421da177e4SLinus Torvalds 	}
12431da177e4SLinus Torvalds }
124479eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds /*
12471da177e4SLinus Torvalds  * Process the deferred bios
12481da177e4SLinus Torvalds  */
12491da177e4SLinus Torvalds static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
12501da177e4SLinus Torvalds {
12511da177e4SLinus Torvalds 	struct bio *n;
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 	while (c) {
12541da177e4SLinus Torvalds 		n = c->bi_next;
12551da177e4SLinus Torvalds 		c->bi_next = NULL;
12561da177e4SLinus Torvalds 		__split_bio(md, c);
12571da177e4SLinus Torvalds 		c = n;
12581da177e4SLinus Torvalds 	}
12591da177e4SLinus Torvalds }
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds /*
12621da177e4SLinus Torvalds  * Swap in a new table (destroying old one).
12631da177e4SLinus Torvalds  */
12641da177e4SLinus Torvalds int dm_swap_table(struct mapped_device *md, struct dm_table *table)
12651da177e4SLinus Torvalds {
126693c534aeSAlasdair G Kergon 	int r = -EINVAL;
12671da177e4SLinus Torvalds 
12682ca3310eSAlasdair G Kergon 	down(&md->suspend_lock);
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 	/* device must be suspended */
1271cf222b37SAlasdair G Kergon 	if (!dm_suspended(md))
127293c534aeSAlasdair G Kergon 		goto out;
12731da177e4SLinus Torvalds 
1274bfa152faSJun'ichi Nomura 	/* without bdev, the device size cannot be changed */
1275bfa152faSJun'ichi Nomura 	if (!md->suspended_bdev)
1276bfa152faSJun'ichi Nomura 		if (get_capacity(md->disk) != dm_table_get_size(table))
1277bfa152faSJun'ichi Nomura 			goto out;
1278bfa152faSJun'ichi Nomura 
12791da177e4SLinus Torvalds 	__unbind(md);
12801da177e4SLinus Torvalds 	r = __bind(md, table);
12811da177e4SLinus Torvalds 
128293c534aeSAlasdair G Kergon out:
12832ca3310eSAlasdair G Kergon 	up(&md->suspend_lock);
128493c534aeSAlasdair G Kergon 	return r;
12851da177e4SLinus Torvalds }
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds /*
12881da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
12891da177e4SLinus Torvalds  * device.
12901da177e4SLinus Torvalds  */
12912ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
12921da177e4SLinus Torvalds {
1293e39e2e95SAlasdair G Kergon 	int r;
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
1296dfbe03f6SAlasdair G Kergon 
1297e39e2e95SAlasdair G Kergon 	md->frozen_sb = freeze_bdev(md->suspended_bdev);
1298dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
1299cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
1300e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
1301e39e2e95SAlasdair G Kergon 		return r;
1302dfbe03f6SAlasdair G Kergon 	}
1303dfbe03f6SAlasdair G Kergon 
1304aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
1305aa8d7c2fSAlasdair G Kergon 
13061da177e4SLinus Torvalds 	/* don't bdput right now, we don't want the bdev
1307e39e2e95SAlasdair G Kergon 	 * to go away while it is locked.
13081da177e4SLinus Torvalds 	 */
13091da177e4SLinus Torvalds 	return 0;
13101da177e4SLinus Torvalds }
13111da177e4SLinus Torvalds 
13122ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
13131da177e4SLinus Torvalds {
1314aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
1315aa8d7c2fSAlasdair G Kergon 		return;
1316aa8d7c2fSAlasdair G Kergon 
1317e39e2e95SAlasdair G Kergon 	thaw_bdev(md->suspended_bdev, md->frozen_sb);
13181da177e4SLinus Torvalds 	md->frozen_sb = NULL;
1319aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
13201da177e4SLinus Torvalds }
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds /*
13231da177e4SLinus Torvalds  * We need to be able to change a mapping table under a mounted
13241da177e4SLinus Torvalds  * filesystem.  For example we might want to move some data in
13251da177e4SLinus Torvalds  * the background.  Before the table can be swapped with
13261da177e4SLinus Torvalds  * dm_bind_table, dm_suspend must be called to flush any in
13271da177e4SLinus Torvalds  * flight bios and ensure that any further io gets deferred.
13281da177e4SLinus Torvalds  */
1329a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
13301da177e4SLinus Torvalds {
13312ca3310eSAlasdair G Kergon 	struct dm_table *map = NULL;
13322e93ccc1SKiyoshi Ueda 	unsigned long flags;
13331da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
13341ecac7fdSJun'ichi Nomura 	struct bio *def;
1335cf222b37SAlasdair G Kergon 	int r = -EINVAL;
1336a3d77d35SKiyoshi Ueda 	int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
13372e93ccc1SKiyoshi Ueda 	int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
13381da177e4SLinus Torvalds 
13392ca3310eSAlasdair G Kergon 	down(&md->suspend_lock);
13402ca3310eSAlasdair G Kergon 
13412ca3310eSAlasdair G Kergon 	if (dm_suspended(md))
1342d287483dSAlasdair G Kergon 		goto out_unlock;
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds 	map = dm_get_table(md);
1345cf222b37SAlasdair G Kergon 
13462e93ccc1SKiyoshi Ueda 	/*
13472e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
13482e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
13492e93ccc1SKiyoshi Ueda 	 */
13502e93ccc1SKiyoshi Ueda 	if (noflush)
13512e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
13522e93ccc1SKiyoshi Ueda 
1353436d4108SAlasdair G Kergon 	/* This does not get reverted if there's an error later. */
13541da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
13551da177e4SLinus Torvalds 
1356bfa152faSJun'ichi Nomura 	/* bdget() can stall if the pending I/Os are not flushed */
1357bfa152faSJun'ichi Nomura 	if (!noflush) {
1358e39e2e95SAlasdair G Kergon 		md->suspended_bdev = bdget_disk(md->disk, 0);
1359e39e2e95SAlasdair G Kergon 		if (!md->suspended_bdev) {
1360e39e2e95SAlasdair G Kergon 			DMWARN("bdget failed in dm_suspend");
1361e39e2e95SAlasdair G Kergon 			r = -ENOMEM;
13622e93ccc1SKiyoshi Ueda 			goto flush_and_out;
1363e39e2e95SAlasdair G Kergon 		}
1364bfa152faSJun'ichi Nomura 	}
1365e39e2e95SAlasdair G Kergon 
13662e93ccc1SKiyoshi Ueda 	/*
13672e93ccc1SKiyoshi Ueda 	 * Flush I/O to the device.
13682e93ccc1SKiyoshi Ueda 	 * noflush supersedes do_lockfs, because lock_fs() needs to flush I/Os.
13692e93ccc1SKiyoshi Ueda 	 */
13702e93ccc1SKiyoshi Ueda 	if (do_lockfs && !noflush) {
13712ca3310eSAlasdair G Kergon 		r = lock_fs(md);
13722ca3310eSAlasdair G Kergon 		if (r)
13732ca3310eSAlasdair G Kergon 			goto out;
1374aa8d7c2fSAlasdair G Kergon 	}
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 	/*
1377354e0071SAlasdair G Kergon 	 * First we set the BLOCK_IO flag so no more ios will be mapped.
13781da177e4SLinus Torvalds 	 */
13792ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
13802ca3310eSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO, &md->flags);
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 	add_wait_queue(&md->wait, &wait);
13832ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 	/* unplug */
13862ca3310eSAlasdair G Kergon 	if (map)
13871da177e4SLinus Torvalds 		dm_table_unplug_all(map);
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds 	/*
13901da177e4SLinus Torvalds 	 * Then we wait for the already mapped ios to
13911da177e4SLinus Torvalds 	 * complete.
13921da177e4SLinus Torvalds 	 */
13931da177e4SLinus Torvalds 	while (1) {
13941da177e4SLinus Torvalds 		set_current_state(TASK_INTERRUPTIBLE);
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds 		if (!atomic_read(&md->pending) || signal_pending(current))
13971da177e4SLinus Torvalds 			break;
13981da177e4SLinus Torvalds 
13991da177e4SLinus Torvalds 		io_schedule();
14001da177e4SLinus Torvalds 	}
14011da177e4SLinus Torvalds 	set_current_state(TASK_RUNNING);
14021da177e4SLinus Torvalds 
14032ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
14041da177e4SLinus Torvalds 	remove_wait_queue(&md->wait, &wait);
14051da177e4SLinus Torvalds 
14062e93ccc1SKiyoshi Ueda 	if (noflush) {
14072e93ccc1SKiyoshi Ueda 		spin_lock_irqsave(&md->pushback_lock, flags);
14082e93ccc1SKiyoshi Ueda 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
14092e93ccc1SKiyoshi Ueda 		bio_list_merge_head(&md->deferred, &md->pushback);
14102e93ccc1SKiyoshi Ueda 		bio_list_init(&md->pushback);
14112e93ccc1SKiyoshi Ueda 		spin_unlock_irqrestore(&md->pushback_lock, flags);
14122e93ccc1SKiyoshi Ueda 	}
14132e93ccc1SKiyoshi Ueda 
14141da177e4SLinus Torvalds 	/* were we interrupted ? */
1415cf222b37SAlasdair G Kergon 	r = -EINTR;
14162ca3310eSAlasdair G Kergon 	if (atomic_read(&md->pending)) {
14171ecac7fdSJun'ichi Nomura 		clear_bit(DMF_BLOCK_IO, &md->flags);
14181ecac7fdSJun'ichi Nomura 		def = bio_list_get(&md->deferred);
14191ecac7fdSJun'ichi Nomura 		__flush_deferred_io(md, def);
14202ca3310eSAlasdair G Kergon 		up_write(&md->io_lock);
14212ca3310eSAlasdair G Kergon 		unlock_fs(md);
14222e93ccc1SKiyoshi Ueda 		goto out; /* pushback list is already flushed, so skip flush */
14232ca3310eSAlasdair G Kergon 	}
14242ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
14252ca3310eSAlasdair G Kergon 
14262ca3310eSAlasdair G Kergon 	dm_table_postsuspend_targets(map);
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
14291da177e4SLinus Torvalds 
14302ca3310eSAlasdair G Kergon 	r = 0;
14312ca3310eSAlasdair G Kergon 
14322e93ccc1SKiyoshi Ueda flush_and_out:
14332e93ccc1SKiyoshi Ueda 	if (r && noflush) {
14342e93ccc1SKiyoshi Ueda 		/*
14352e93ccc1SKiyoshi Ueda 		 * Because there may be already I/Os in the pushback list,
14362e93ccc1SKiyoshi Ueda 		 * flush them before return.
14372e93ccc1SKiyoshi Ueda 		 */
14382e93ccc1SKiyoshi Ueda 		down_write(&md->io_lock);
14392e93ccc1SKiyoshi Ueda 
14402e93ccc1SKiyoshi Ueda 		spin_lock_irqsave(&md->pushback_lock, flags);
14412e93ccc1SKiyoshi Ueda 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
14422e93ccc1SKiyoshi Ueda 		bio_list_merge_head(&md->deferred, &md->pushback);
14432e93ccc1SKiyoshi Ueda 		bio_list_init(&md->pushback);
14442e93ccc1SKiyoshi Ueda 		spin_unlock_irqrestore(&md->pushback_lock, flags);
14452e93ccc1SKiyoshi Ueda 
14462e93ccc1SKiyoshi Ueda 		def = bio_list_get(&md->deferred);
14472e93ccc1SKiyoshi Ueda 		__flush_deferred_io(md, def);
14482e93ccc1SKiyoshi Ueda 		up_write(&md->io_lock);
14492e93ccc1SKiyoshi Ueda 	}
14502e93ccc1SKiyoshi Ueda 
14512ca3310eSAlasdair G Kergon out:
1452e39e2e95SAlasdair G Kergon 	if (r && md->suspended_bdev) {
1453e39e2e95SAlasdair G Kergon 		bdput(md->suspended_bdev);
1454e39e2e95SAlasdair G Kergon 		md->suspended_bdev = NULL;
1455e39e2e95SAlasdair G Kergon 	}
1456e39e2e95SAlasdair G Kergon 
14571da177e4SLinus Torvalds 	dm_table_put(map);
1458d287483dSAlasdair G Kergon 
1459d287483dSAlasdair G Kergon out_unlock:
14602ca3310eSAlasdair G Kergon 	up(&md->suspend_lock);
1461cf222b37SAlasdair G Kergon 	return r;
14621da177e4SLinus Torvalds }
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds int dm_resume(struct mapped_device *md)
14651da177e4SLinus Torvalds {
1466cf222b37SAlasdair G Kergon 	int r = -EINVAL;
14671da177e4SLinus Torvalds 	struct bio *def;
1468cf222b37SAlasdair G Kergon 	struct dm_table *map = NULL;
14691da177e4SLinus Torvalds 
14702ca3310eSAlasdair G Kergon 	down(&md->suspend_lock);
14712ca3310eSAlasdair G Kergon 	if (!dm_suspended(md))
1472cf222b37SAlasdair G Kergon 		goto out;
1473cf222b37SAlasdair G Kergon 
1474cf222b37SAlasdair G Kergon 	map = dm_get_table(md);
14752ca3310eSAlasdair G Kergon 	if (!map || !dm_table_get_size(map))
1476cf222b37SAlasdair G Kergon 		goto out;
14771da177e4SLinus Torvalds 
14788757b776SMilan Broz 	r = dm_table_resume_targets(map);
14798757b776SMilan Broz 	if (r)
14808757b776SMilan Broz 		goto out;
14812ca3310eSAlasdair G Kergon 
14822ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
14831da177e4SLinus Torvalds 	clear_bit(DMF_BLOCK_IO, &md->flags);
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds 	def = bio_list_get(&md->deferred);
14861da177e4SLinus Torvalds 	__flush_deferred_io(md, def);
14872ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
14882ca3310eSAlasdair G Kergon 
14892ca3310eSAlasdair G Kergon 	unlock_fs(md);
14902ca3310eSAlasdair G Kergon 
1491bfa152faSJun'ichi Nomura 	if (md->suspended_bdev) {
1492e39e2e95SAlasdair G Kergon 		bdput(md->suspended_bdev);
1493e39e2e95SAlasdair G Kergon 		md->suspended_bdev = NULL;
1494bfa152faSJun'ichi Nomura 	}
1495e39e2e95SAlasdair G Kergon 
14962ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
14972ca3310eSAlasdair G Kergon 
14981da177e4SLinus Torvalds 	dm_table_unplug_all(map);
14991da177e4SLinus Torvalds 
15008560ed6fSHannes Reinecke 	kobject_uevent(&md->disk->kobj, KOBJ_CHANGE);
15018560ed6fSHannes Reinecke 
1502cf222b37SAlasdair G Kergon 	r = 0;
15032ca3310eSAlasdair G Kergon 
1504cf222b37SAlasdair G Kergon out:
1505cf222b37SAlasdair G Kergon 	dm_table_put(map);
15062ca3310eSAlasdair G Kergon 	up(&md->suspend_lock);
15072ca3310eSAlasdair G Kergon 
1508cf222b37SAlasdair G Kergon 	return r;
15091da177e4SLinus Torvalds }
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds /*-----------------------------------------------------------------
15121da177e4SLinus Torvalds  * Event notification.
15131da177e4SLinus Torvalds  *---------------------------------------------------------------*/
15141da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
15151da177e4SLinus Torvalds {
15161da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
15171da177e4SLinus Torvalds }
15181da177e4SLinus Torvalds 
15191da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
15201da177e4SLinus Torvalds {
15211da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
15221da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
15231da177e4SLinus Torvalds }
15241da177e4SLinus Torvalds 
15251da177e4SLinus Torvalds /*
15261da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
15271da177e4SLinus Torvalds  * count on 'md'.
15281da177e4SLinus Torvalds  */
15291da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
15301da177e4SLinus Torvalds {
15311da177e4SLinus Torvalds 	return md->disk;
15321da177e4SLinus Torvalds }
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds int dm_suspended(struct mapped_device *md)
15351da177e4SLinus Torvalds {
15361da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
15371da177e4SLinus Torvalds }
15381da177e4SLinus Torvalds 
15392e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
15402e93ccc1SKiyoshi Ueda {
15412e93ccc1SKiyoshi Ueda 	struct mapped_device *md = dm_table_get_md(ti->table);
15422e93ccc1SKiyoshi Ueda 	int r = __noflush_suspending(md);
15432e93ccc1SKiyoshi Ueda 
15442e93ccc1SKiyoshi Ueda 	dm_put(md);
15452e93ccc1SKiyoshi Ueda 
15462e93ccc1SKiyoshi Ueda 	return r;
15472e93ccc1SKiyoshi Ueda }
15482e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
15492e93ccc1SKiyoshi Ueda 
15501da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops = {
15511da177e4SLinus Torvalds 	.open = dm_blk_open,
15521da177e4SLinus Torvalds 	.release = dm_blk_close,
1553aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
15543ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
15551da177e4SLinus Torvalds 	.owner = THIS_MODULE
15561da177e4SLinus Torvalds };
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo);
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds /*
15611da177e4SLinus Torvalds  * module hooks
15621da177e4SLinus Torvalds  */
15631da177e4SLinus Torvalds module_init(dm_init);
15641da177e4SLinus Torvalds module_exit(dm_exit);
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds module_param(major, uint, 0);
15671da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
15681da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
15691da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
15701da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1571