xref: /openbmc/linux/drivers/md/dm.c (revision 93c534aefb906824d71ea779ed0c7f1573843f4e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
31da177e4SLinus Torvalds  * Copyright (C) 2004 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>
131da177e4SLinus Torvalds #include <linux/moduleparam.h>
141da177e4SLinus Torvalds #include <linux/blkpg.h>
151da177e4SLinus Torvalds #include <linux/bio.h>
161da177e4SLinus Torvalds #include <linux/buffer_head.h>
171da177e4SLinus Torvalds #include <linux/mempool.h>
181da177e4SLinus Torvalds #include <linux/slab.h>
191da177e4SLinus Torvalds #include <linux/idr.h>
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds static const char *_name = DM_NAME;
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds static unsigned int major = 0;
241da177e4SLinus Torvalds static unsigned int _major = 0;
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds /*
271da177e4SLinus Torvalds  * One of these is allocated per bio.
281da177e4SLinus Torvalds  */
291da177e4SLinus Torvalds struct dm_io {
301da177e4SLinus Torvalds 	struct mapped_device *md;
311da177e4SLinus Torvalds 	int error;
321da177e4SLinus Torvalds 	struct bio *bio;
331da177e4SLinus Torvalds 	atomic_t io_count;
341da177e4SLinus Torvalds };
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds /*
371da177e4SLinus Torvalds  * One of these is allocated per target within a bio.  Hopefully
381da177e4SLinus Torvalds  * this will be simplified out one day.
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds struct target_io {
411da177e4SLinus Torvalds 	struct dm_io *io;
421da177e4SLinus Torvalds 	struct dm_target *ti;
431da177e4SLinus Torvalds 	union map_info info;
441da177e4SLinus Torvalds };
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds union map_info *dm_get_mapinfo(struct bio *bio)
471da177e4SLinus Torvalds {
481da177e4SLinus Torvalds         if (bio && bio->bi_private)
491da177e4SLinus Torvalds                 return &((struct target_io *)bio->bi_private)->info;
501da177e4SLinus Torvalds         return NULL;
511da177e4SLinus Torvalds }
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds /*
541da177e4SLinus Torvalds  * Bits for the md->flags field.
551da177e4SLinus Torvalds  */
561da177e4SLinus Torvalds #define DMF_BLOCK_IO 0
571da177e4SLinus Torvalds #define DMF_SUSPENDED 1
581da177e4SLinus Torvalds #define DMF_FS_LOCKED 2
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds struct mapped_device {
611da177e4SLinus Torvalds 	struct rw_semaphore lock;
621da177e4SLinus Torvalds 	rwlock_t map_lock;
631da177e4SLinus Torvalds 	atomic_t holders;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	unsigned long flags;
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 	request_queue_t *queue;
681da177e4SLinus Torvalds 	struct gendisk *disk;
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds 	void *interface_ptr;
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	/*
731da177e4SLinus Torvalds 	 * A list of ios that arrived while we were suspended.
741da177e4SLinus Torvalds 	 */
751da177e4SLinus Torvalds 	atomic_t pending;
761da177e4SLinus Torvalds 	wait_queue_head_t wait;
771da177e4SLinus Torvalds  	struct bio_list deferred;
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	/*
801da177e4SLinus Torvalds 	 * The current mapping.
811da177e4SLinus Torvalds 	 */
821da177e4SLinus Torvalds 	struct dm_table *map;
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	/*
851da177e4SLinus Torvalds 	 * io objects are allocated from here.
861da177e4SLinus Torvalds 	 */
871da177e4SLinus Torvalds 	mempool_t *io_pool;
881da177e4SLinus Torvalds 	mempool_t *tio_pool;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	/*
911da177e4SLinus Torvalds 	 * Event handling.
921da177e4SLinus Torvalds 	 */
931da177e4SLinus Torvalds 	atomic_t event_nr;
941da177e4SLinus Torvalds 	wait_queue_head_t eventq;
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	/*
971da177e4SLinus Torvalds 	 * freeze/thaw support require holding onto a super block
981da177e4SLinus Torvalds 	 */
991da177e4SLinus Torvalds 	struct super_block *frozen_sb;
100d1782a3bSAlasdair G Kergon 	struct block_device *frozen_bdev;
1011da177e4SLinus Torvalds };
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds #define MIN_IOS 256
1041da177e4SLinus Torvalds static kmem_cache_t *_io_cache;
1051da177e4SLinus Torvalds static kmem_cache_t *_tio_cache;
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds static struct bio_set *dm_set;
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds static int __init local_init(void)
1101da177e4SLinus Torvalds {
1111da177e4SLinus Torvalds 	int r;
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	dm_set = bioset_create(16, 16, 4);
1141da177e4SLinus Torvalds 	if (!dm_set)
1151da177e4SLinus Torvalds 		return -ENOMEM;
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
1181da177e4SLinus Torvalds 	_io_cache = kmem_cache_create("dm_io",
1191da177e4SLinus Torvalds 				      sizeof(struct dm_io), 0, 0, NULL, NULL);
1201da177e4SLinus Torvalds 	if (!_io_cache)
1211da177e4SLinus Torvalds 		return -ENOMEM;
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	/* allocate a slab for the target ios */
1241da177e4SLinus Torvalds 	_tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io),
1251da177e4SLinus Torvalds 				       0, 0, NULL, NULL);
1261da177e4SLinus Torvalds 	if (!_tio_cache) {
1271da177e4SLinus Torvalds 		kmem_cache_destroy(_io_cache);
1281da177e4SLinus Torvalds 		return -ENOMEM;
1291da177e4SLinus Torvalds 	}
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	_major = major;
1321da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
1331da177e4SLinus Torvalds 	if (r < 0) {
1341da177e4SLinus Torvalds 		kmem_cache_destroy(_tio_cache);
1351da177e4SLinus Torvalds 		kmem_cache_destroy(_io_cache);
1361da177e4SLinus Torvalds 		return r;
1371da177e4SLinus Torvalds 	}
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	if (!_major)
1401da177e4SLinus Torvalds 		_major = r;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	return 0;
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds static void local_exit(void)
1461da177e4SLinus Torvalds {
1471da177e4SLinus Torvalds 	kmem_cache_destroy(_tio_cache);
1481da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	bioset_free(dm_set);
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	if (unregister_blkdev(_major, _name) < 0)
1531da177e4SLinus Torvalds 		DMERR("devfs_unregister_blkdev failed");
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	_major = 0;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	DMINFO("cleaned up");
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds int (*_inits[])(void) __initdata = {
1611da177e4SLinus Torvalds 	local_init,
1621da177e4SLinus Torvalds 	dm_target_init,
1631da177e4SLinus Torvalds 	dm_linear_init,
1641da177e4SLinus Torvalds 	dm_stripe_init,
1651da177e4SLinus Torvalds 	dm_interface_init,
1661da177e4SLinus Torvalds };
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds void (*_exits[])(void) = {
1691da177e4SLinus Torvalds 	local_exit,
1701da177e4SLinus Torvalds 	dm_target_exit,
1711da177e4SLinus Torvalds 	dm_linear_exit,
1721da177e4SLinus Torvalds 	dm_stripe_exit,
1731da177e4SLinus Torvalds 	dm_interface_exit,
1741da177e4SLinus Torvalds };
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds static int __init dm_init(void)
1771da177e4SLinus Torvalds {
1781da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	int r, i;
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
1831da177e4SLinus Torvalds 		r = _inits[i]();
1841da177e4SLinus Torvalds 		if (r)
1851da177e4SLinus Torvalds 			goto bad;
1861da177e4SLinus Torvalds 	}
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	return 0;
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds       bad:
1911da177e4SLinus Torvalds 	while (i--)
1921da177e4SLinus Torvalds 		_exits[i]();
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	return r;
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds static void __exit dm_exit(void)
1981da177e4SLinus Torvalds {
1991da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	while (i--)
2021da177e4SLinus Torvalds 		_exits[i]();
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds /*
2061da177e4SLinus Torvalds  * Block device functions
2071da177e4SLinus Torvalds  */
2081da177e4SLinus Torvalds static int dm_blk_open(struct inode *inode, struct file *file)
2091da177e4SLinus Torvalds {
2101da177e4SLinus Torvalds 	struct mapped_device *md;
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	md = inode->i_bdev->bd_disk->private_data;
2131da177e4SLinus Torvalds 	dm_get(md);
2141da177e4SLinus Torvalds 	return 0;
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds static int dm_blk_close(struct inode *inode, struct file *file)
2181da177e4SLinus Torvalds {
2191da177e4SLinus Torvalds 	struct mapped_device *md;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	md = inode->i_bdev->bd_disk->private_data;
2221da177e4SLinus Torvalds 	dm_put(md);
2231da177e4SLinus Torvalds 	return 0;
2241da177e4SLinus Torvalds }
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds static inline struct dm_io *alloc_io(struct mapped_device *md)
2271da177e4SLinus Torvalds {
2281da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
2291da177e4SLinus Torvalds }
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds static inline void free_io(struct mapped_device *md, struct dm_io *io)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds static inline struct target_io *alloc_tio(struct mapped_device *md)
2371da177e4SLinus Torvalds {
2381da177e4SLinus Torvalds 	return mempool_alloc(md->tio_pool, GFP_NOIO);
2391da177e4SLinus Torvalds }
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds static inline void free_tio(struct mapped_device *md, struct target_io *tio)
2421da177e4SLinus Torvalds {
2431da177e4SLinus Torvalds 	mempool_free(tio, md->tio_pool);
2441da177e4SLinus Torvalds }
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds /*
2471da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
2481da177e4SLinus Torvalds  */
2491da177e4SLinus Torvalds static int queue_io(struct mapped_device *md, struct bio *bio)
2501da177e4SLinus Torvalds {
2511da177e4SLinus Torvalds 	down_write(&md->lock);
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
2541da177e4SLinus Torvalds 		up_write(&md->lock);
2551da177e4SLinus Torvalds 		return 1;
2561da177e4SLinus Torvalds 	}
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	up_write(&md->lock);
2611da177e4SLinus Torvalds 	return 0;		/* deferred successfully */
2621da177e4SLinus Torvalds }
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds /*
2651da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
2661da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
2671da177e4SLinus Torvalds  * dm_table_put() when finished.
2681da177e4SLinus Torvalds  */
2691da177e4SLinus Torvalds struct dm_table *dm_get_table(struct mapped_device *md)
2701da177e4SLinus Torvalds {
2711da177e4SLinus Torvalds 	struct dm_table *t;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	read_lock(&md->map_lock);
2741da177e4SLinus Torvalds 	t = md->map;
2751da177e4SLinus Torvalds 	if (t)
2761da177e4SLinus Torvalds 		dm_table_get(t);
2771da177e4SLinus Torvalds 	read_unlock(&md->map_lock);
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	return t;
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds /*-----------------------------------------------------------------
2831da177e4SLinus Torvalds  * CRUD START:
2841da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
2851da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
2861da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
2871da177e4SLinus Torvalds  *   interests of getting something for people to use I give
2881da177e4SLinus Torvalds  *   you this clearly demarcated crap.
2891da177e4SLinus Torvalds  *---------------------------------------------------------------*/
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds /*
2921da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
2931da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
2941da177e4SLinus Torvalds  */
2951da177e4SLinus Torvalds static inline void dec_pending(struct dm_io *io, int error)
2961da177e4SLinus Torvalds {
2971da177e4SLinus Torvalds 	if (error)
2981da177e4SLinus Torvalds 		io->error = error;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
3011da177e4SLinus Torvalds 		if (atomic_dec_and_test(&io->md->pending))
3021da177e4SLinus Torvalds 			/* nudge anyone waiting on suspend queue */
3031da177e4SLinus Torvalds 			wake_up(&io->md->wait);
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 		bio_endio(io->bio, io->bio->bi_size, io->error);
3061da177e4SLinus Torvalds 		free_io(io->md, io);
3071da177e4SLinus Torvalds 	}
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds static int clone_endio(struct bio *bio, unsigned int done, int error)
3111da177e4SLinus Torvalds {
3121da177e4SLinus Torvalds 	int r = 0;
3131da177e4SLinus Torvalds 	struct target_io *tio = bio->bi_private;
3141da177e4SLinus Torvalds 	struct dm_io *io = tio->io;
3151da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	if (bio->bi_size)
3181da177e4SLinus Torvalds 		return 1;
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	if (!bio_flagged(bio, BIO_UPTODATE) && !error)
3211da177e4SLinus Torvalds 		error = -EIO;
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	if (endio) {
3241da177e4SLinus Torvalds 		r = endio(tio->ti, bio, error, &tio->info);
3251da177e4SLinus Torvalds 		if (r < 0)
3261da177e4SLinus Torvalds 			error = r;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 		else if (r > 0)
3291da177e4SLinus Torvalds 			/* the target wants another shot at the io */
3301da177e4SLinus Torvalds 			return 1;
3311da177e4SLinus Torvalds 	}
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	free_tio(io->md, tio);
3341da177e4SLinus Torvalds 	dec_pending(io, error);
3351da177e4SLinus Torvalds 	bio_put(bio);
3361da177e4SLinus Torvalds 	return r;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds static sector_t max_io_len(struct mapped_device *md,
3401da177e4SLinus Torvalds 			   sector_t sector, struct dm_target *ti)
3411da177e4SLinus Torvalds {
3421da177e4SLinus Torvalds 	sector_t offset = sector - ti->begin;
3431da177e4SLinus Torvalds 	sector_t len = ti->len - offset;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	/*
3461da177e4SLinus Torvalds 	 * Does the target need to split even further ?
3471da177e4SLinus Torvalds 	 */
3481da177e4SLinus Torvalds 	if (ti->split_io) {
3491da177e4SLinus Torvalds 		sector_t boundary;
3501da177e4SLinus Torvalds 		boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
3511da177e4SLinus Torvalds 			   - offset;
3521da177e4SLinus Torvalds 		if (len > boundary)
3531da177e4SLinus Torvalds 			len = boundary;
3541da177e4SLinus Torvalds 	}
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	return len;
3571da177e4SLinus Torvalds }
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds static void __map_bio(struct dm_target *ti, struct bio *clone,
3601da177e4SLinus Torvalds 		      struct target_io *tio)
3611da177e4SLinus Torvalds {
3621da177e4SLinus Torvalds 	int r;
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	/*
3651da177e4SLinus Torvalds 	 * Sanity checks.
3661da177e4SLinus Torvalds 	 */
3671da177e4SLinus Torvalds 	BUG_ON(!clone->bi_size);
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
3701da177e4SLinus Torvalds 	clone->bi_private = tio;
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	/*
3731da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
3741da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
3751da177e4SLinus Torvalds 	 * this io.
3761da177e4SLinus Torvalds 	 */
3771da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
3781da177e4SLinus Torvalds 	r = ti->type->map(ti, clone, &tio->info);
3791da177e4SLinus Torvalds 	if (r > 0)
3801da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
3811da177e4SLinus Torvalds 		generic_make_request(clone);
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	else if (r < 0) {
3841da177e4SLinus Torvalds 		/* error the io and bail out */
3851da177e4SLinus Torvalds 		struct dm_io *io = tio->io;
3861da177e4SLinus Torvalds 		free_tio(tio->io->md, tio);
387f6a80ea8SAlasdair G Kergon 		dec_pending(io, r);
3881da177e4SLinus Torvalds 		bio_put(clone);
3891da177e4SLinus Torvalds 	}
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds struct clone_info {
3931da177e4SLinus Torvalds 	struct mapped_device *md;
3941da177e4SLinus Torvalds 	struct dm_table *map;
3951da177e4SLinus Torvalds 	struct bio *bio;
3961da177e4SLinus Torvalds 	struct dm_io *io;
3971da177e4SLinus Torvalds 	sector_t sector;
3981da177e4SLinus Torvalds 	sector_t sector_count;
3991da177e4SLinus Torvalds 	unsigned short idx;
4001da177e4SLinus Torvalds };
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds /*
4031da177e4SLinus Torvalds  * Creates a little bio that is just does part of a bvec.
4041da177e4SLinus Torvalds  */
4051da177e4SLinus Torvalds static struct bio *split_bvec(struct bio *bio, sector_t sector,
4061da177e4SLinus Torvalds 			      unsigned short idx, unsigned int offset,
4071da177e4SLinus Torvalds 			      unsigned int len)
4081da177e4SLinus Torvalds {
4091da177e4SLinus Torvalds 	struct bio *clone;
4101da177e4SLinus Torvalds 	struct bio_vec *bv = bio->bi_io_vec + idx;
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	clone = bio_alloc_bioset(GFP_NOIO, 1, dm_set);
4131da177e4SLinus Torvalds 	*clone->bi_io_vec = *bv;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	clone->bi_sector = sector;
4161da177e4SLinus Torvalds 	clone->bi_bdev = bio->bi_bdev;
4171da177e4SLinus Torvalds 	clone->bi_rw = bio->bi_rw;
4181da177e4SLinus Torvalds 	clone->bi_vcnt = 1;
4191da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
4201da177e4SLinus Torvalds 	clone->bi_io_vec->bv_offset = offset;
4211da177e4SLinus Torvalds 	clone->bi_io_vec->bv_len = clone->bi_size;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	return clone;
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds /*
4271da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
4281da177e4SLinus Torvalds  */
4291da177e4SLinus Torvalds static struct bio *clone_bio(struct bio *bio, sector_t sector,
4301da177e4SLinus Torvalds 			     unsigned short idx, unsigned short bv_count,
4311da177e4SLinus Torvalds 			     unsigned int len)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	struct bio *clone;
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	clone = bio_clone(bio, GFP_NOIO);
4361da177e4SLinus Torvalds 	clone->bi_sector = sector;
4371da177e4SLinus Torvalds 	clone->bi_idx = idx;
4381da177e4SLinus Torvalds 	clone->bi_vcnt = idx + bv_count;
4391da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
4401da177e4SLinus Torvalds 	clone->bi_flags &= ~(1 << BIO_SEG_VALID);
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	return clone;
4431da177e4SLinus Torvalds }
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds static void __clone_and_map(struct clone_info *ci)
4461da177e4SLinus Torvalds {
4471da177e4SLinus Torvalds 	struct bio *clone, *bio = ci->bio;
4481da177e4SLinus Torvalds 	struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
4491da177e4SLinus Torvalds 	sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
4501da177e4SLinus Torvalds 	struct target_io *tio;
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	/*
4531da177e4SLinus Torvalds 	 * Allocate a target io object.
4541da177e4SLinus Torvalds 	 */
4551da177e4SLinus Torvalds 	tio = alloc_tio(ci->md);
4561da177e4SLinus Torvalds 	tio->io = ci->io;
4571da177e4SLinus Torvalds 	tio->ti = ti;
4581da177e4SLinus Torvalds 	memset(&tio->info, 0, sizeof(tio->info));
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	if (ci->sector_count <= max) {
4611da177e4SLinus Torvalds 		/*
4621da177e4SLinus Torvalds 		 * Optimise for the simple case where we can do all of
4631da177e4SLinus Torvalds 		 * the remaining io with a single clone.
4641da177e4SLinus Torvalds 		 */
4651da177e4SLinus Torvalds 		clone = clone_bio(bio, ci->sector, ci->idx,
4661da177e4SLinus Torvalds 				  bio->bi_vcnt - ci->idx, ci->sector_count);
4671da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
4681da177e4SLinus Torvalds 		ci->sector_count = 0;
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	} else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
4711da177e4SLinus Torvalds 		/*
4721da177e4SLinus Torvalds 		 * There are some bvecs that don't span targets.
4731da177e4SLinus Torvalds 		 * Do as many of these as possible.
4741da177e4SLinus Torvalds 		 */
4751da177e4SLinus Torvalds 		int i;
4761da177e4SLinus Torvalds 		sector_t remaining = max;
4771da177e4SLinus Torvalds 		sector_t bv_len;
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 		for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
4801da177e4SLinus Torvalds 			bv_len = to_sector(bio->bi_io_vec[i].bv_len);
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 			if (bv_len > remaining)
4831da177e4SLinus Torvalds 				break;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 			remaining -= bv_len;
4861da177e4SLinus Torvalds 			len += bv_len;
4871da177e4SLinus Torvalds 		}
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 		clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len);
4901da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 		ci->sector += len;
4931da177e4SLinus Torvalds 		ci->sector_count -= len;
4941da177e4SLinus Torvalds 		ci->idx = i;
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	} else {
4971da177e4SLinus Torvalds 		/*
4981da177e4SLinus Torvalds 		 * Create two copy bios to deal with io that has
4991da177e4SLinus Torvalds 		 * been split across a target.
5001da177e4SLinus Torvalds 		 */
5011da177e4SLinus Torvalds 		struct bio_vec *bv = bio->bi_io_vec + ci->idx;
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 		clone = split_bvec(bio, ci->sector, ci->idx,
5041da177e4SLinus Torvalds 				   bv->bv_offset, max);
5051da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 		ci->sector += max;
5081da177e4SLinus Torvalds 		ci->sector_count -= max;
5091da177e4SLinus Torvalds 		ti = dm_table_find_target(ci->map, ci->sector);
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 		len = to_sector(bv->bv_len) - max;
5121da177e4SLinus Torvalds 		clone = split_bvec(bio, ci->sector, ci->idx,
5131da177e4SLinus Torvalds 				   bv->bv_offset + to_bytes(max), len);
5141da177e4SLinus Torvalds 		tio = alloc_tio(ci->md);
5151da177e4SLinus Torvalds 		tio->io = ci->io;
5161da177e4SLinus Torvalds 		tio->ti = ti;
5171da177e4SLinus Torvalds 		memset(&tio->info, 0, sizeof(tio->info));
5181da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 		ci->sector += len;
5211da177e4SLinus Torvalds 		ci->sector_count -= len;
5221da177e4SLinus Torvalds 		ci->idx++;
5231da177e4SLinus Torvalds 	}
5241da177e4SLinus Torvalds }
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds /*
5271da177e4SLinus Torvalds  * Split the bio into several clones.
5281da177e4SLinus Torvalds  */
5291da177e4SLinus Torvalds static void __split_bio(struct mapped_device *md, struct bio *bio)
5301da177e4SLinus Torvalds {
5311da177e4SLinus Torvalds 	struct clone_info ci;
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	ci.map = dm_get_table(md);
5341da177e4SLinus Torvalds 	if (!ci.map) {
5351da177e4SLinus Torvalds 		bio_io_error(bio, bio->bi_size);
5361da177e4SLinus Torvalds 		return;
5371da177e4SLinus Torvalds 	}
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	ci.md = md;
5401da177e4SLinus Torvalds 	ci.bio = bio;
5411da177e4SLinus Torvalds 	ci.io = alloc_io(md);
5421da177e4SLinus Torvalds 	ci.io->error = 0;
5431da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
5441da177e4SLinus Torvalds 	ci.io->bio = bio;
5451da177e4SLinus Torvalds 	ci.io->md = md;
5461da177e4SLinus Torvalds 	ci.sector = bio->bi_sector;
5471da177e4SLinus Torvalds 	ci.sector_count = bio_sectors(bio);
5481da177e4SLinus Torvalds 	ci.idx = bio->bi_idx;
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	atomic_inc(&md->pending);
5511da177e4SLinus Torvalds 	while (ci.sector_count)
5521da177e4SLinus Torvalds 		__clone_and_map(&ci);
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 	/* drop the extra reference count */
5551da177e4SLinus Torvalds 	dec_pending(ci.io, 0);
5561da177e4SLinus Torvalds 	dm_table_put(ci.map);
5571da177e4SLinus Torvalds }
5581da177e4SLinus Torvalds /*-----------------------------------------------------------------
5591da177e4SLinus Torvalds  * CRUD END
5601da177e4SLinus Torvalds  *---------------------------------------------------------------*/
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds /*
5631da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
5641da177e4SLinus Torvalds  * dm_merge_bvec.
5651da177e4SLinus Torvalds  */
5661da177e4SLinus Torvalds static int dm_request(request_queue_t *q, struct bio *bio)
5671da177e4SLinus Torvalds {
5681da177e4SLinus Torvalds 	int r;
5691da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	down_read(&md->lock);
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 	/*
5741da177e4SLinus Torvalds 	 * If we're suspended we have to queue
5751da177e4SLinus Torvalds 	 * this io for later.
5761da177e4SLinus Torvalds 	 */
5771da177e4SLinus Torvalds 	while (test_bit(DMF_BLOCK_IO, &md->flags)) {
5781da177e4SLinus Torvalds 		up_read(&md->lock);
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 		if (bio_rw(bio) == READA) {
5811da177e4SLinus Torvalds 			bio_io_error(bio, bio->bi_size);
5821da177e4SLinus Torvalds 			return 0;
5831da177e4SLinus Torvalds 		}
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 		r = queue_io(md, bio);
5861da177e4SLinus Torvalds 		if (r < 0) {
5871da177e4SLinus Torvalds 			bio_io_error(bio, bio->bi_size);
5881da177e4SLinus Torvalds 			return 0;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 		} else if (r == 0)
5911da177e4SLinus Torvalds 			return 0;	/* deferred successfully */
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 		/*
5941da177e4SLinus Torvalds 		 * We're in a while loop, because someone could suspend
5951da177e4SLinus Torvalds 		 * before we get to the following read lock.
5961da177e4SLinus Torvalds 		 */
5971da177e4SLinus Torvalds 		down_read(&md->lock);
5981da177e4SLinus Torvalds 	}
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 	__split_bio(md, bio);
6011da177e4SLinus Torvalds 	up_read(&md->lock);
6021da177e4SLinus Torvalds 	return 0;
6031da177e4SLinus Torvalds }
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
6061da177e4SLinus Torvalds 			sector_t *error_sector)
6071da177e4SLinus Torvalds {
6081da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
6091da177e4SLinus Torvalds 	struct dm_table *map = dm_get_table(md);
6101da177e4SLinus Torvalds 	int ret = -ENXIO;
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	if (map) {
6131da177e4SLinus Torvalds 		ret = dm_table_flush_all(md->map);
6141da177e4SLinus Torvalds 		dm_table_put(map);
6151da177e4SLinus Torvalds 	}
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	return ret;
6181da177e4SLinus Torvalds }
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds static void dm_unplug_all(request_queue_t *q)
6211da177e4SLinus Torvalds {
6221da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
6231da177e4SLinus Torvalds 	struct dm_table *map = dm_get_table(md);
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	if (map) {
6261da177e4SLinus Torvalds 		dm_table_unplug_all(map);
6271da177e4SLinus Torvalds 		dm_table_put(map);
6281da177e4SLinus Torvalds 	}
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
6321da177e4SLinus Torvalds {
6331da177e4SLinus Torvalds 	int r;
6341da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) congested_data;
6351da177e4SLinus Torvalds 	struct dm_table *map = dm_get_table(md);
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
6381da177e4SLinus Torvalds 		r = bdi_bits;
6391da177e4SLinus Torvalds 	else
6401da177e4SLinus Torvalds 		r = dm_table_any_congested(map, bdi_bits);
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	dm_table_put(map);
6431da177e4SLinus Torvalds 	return r;
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds /*-----------------------------------------------------------------
6471da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
6481da177e4SLinus Torvalds  *---------------------------------------------------------------*/
6491da177e4SLinus Torvalds static DECLARE_MUTEX(_minor_lock);
6501da177e4SLinus Torvalds static DEFINE_IDR(_minor_idr);
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds static void free_minor(unsigned int minor)
6531da177e4SLinus Torvalds {
6541da177e4SLinus Torvalds 	down(&_minor_lock);
6551da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
6561da177e4SLinus Torvalds 	up(&_minor_lock);
6571da177e4SLinus Torvalds }
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds /*
6601da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
6611da177e4SLinus Torvalds  */
6621da177e4SLinus Torvalds static int specific_minor(struct mapped_device *md, unsigned int minor)
6631da177e4SLinus Torvalds {
6641da177e4SLinus Torvalds 	int r, m;
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
6671da177e4SLinus Torvalds 		return -EINVAL;
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	down(&_minor_lock);
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 	if (idr_find(&_minor_idr, minor)) {
6721da177e4SLinus Torvalds 		r = -EBUSY;
6731da177e4SLinus Torvalds 		goto out;
6741da177e4SLinus Torvalds 	}
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
6771da177e4SLinus Torvalds 	if (!r) {
6781da177e4SLinus Torvalds 		r = -ENOMEM;
6791da177e4SLinus Torvalds 		goto out;
6801da177e4SLinus Torvalds 	}
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	r = idr_get_new_above(&_minor_idr, md, minor, &m);
6831da177e4SLinus Torvalds 	if (r) {
6841da177e4SLinus Torvalds 		goto out;
6851da177e4SLinus Torvalds 	}
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	if (m != minor) {
6881da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
6891da177e4SLinus Torvalds 		r = -EBUSY;
6901da177e4SLinus Torvalds 		goto out;
6911da177e4SLinus Torvalds 	}
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds out:
6941da177e4SLinus Torvalds 	up(&_minor_lock);
6951da177e4SLinus Torvalds 	return r;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds static int next_free_minor(struct mapped_device *md, unsigned int *minor)
6991da177e4SLinus Torvalds {
7001da177e4SLinus Torvalds 	int r;
7011da177e4SLinus Torvalds 	unsigned int m;
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	down(&_minor_lock);
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
7061da177e4SLinus Torvalds 	if (!r) {
7071da177e4SLinus Torvalds 		r = -ENOMEM;
7081da177e4SLinus Torvalds 		goto out;
7091da177e4SLinus Torvalds 	}
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	r = idr_get_new(&_minor_idr, md, &m);
7121da177e4SLinus Torvalds 	if (r) {
7131da177e4SLinus Torvalds 		goto out;
7141da177e4SLinus Torvalds 	}
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	if (m >= (1 << MINORBITS)) {
7171da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
7181da177e4SLinus Torvalds 		r = -ENOSPC;
7191da177e4SLinus Torvalds 		goto out;
7201da177e4SLinus Torvalds 	}
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	*minor = m;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds out:
7251da177e4SLinus Torvalds 	up(&_minor_lock);
7261da177e4SLinus Torvalds 	return r;
7271da177e4SLinus Torvalds }
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops;
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds /*
7321da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
7331da177e4SLinus Torvalds  */
7341da177e4SLinus Torvalds static struct mapped_device *alloc_dev(unsigned int minor, int persistent)
7351da177e4SLinus Torvalds {
7361da177e4SLinus Torvalds 	int r;
7371da177e4SLinus Torvalds 	struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	if (!md) {
7401da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
7411da177e4SLinus Torvalds 		return NULL;
7421da177e4SLinus Torvalds 	}
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	/* get a minor number for the dev */
7451da177e4SLinus Torvalds 	r = persistent ? specific_minor(md, minor) : next_free_minor(md, &minor);
7461da177e4SLinus Torvalds 	if (r < 0)
7471da177e4SLinus Torvalds 		goto bad1;
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	memset(md, 0, sizeof(*md));
7501da177e4SLinus Torvalds 	init_rwsem(&md->lock);
7511da177e4SLinus Torvalds 	rwlock_init(&md->map_lock);
7521da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
7531da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	md->queue = blk_alloc_queue(GFP_KERNEL);
7561da177e4SLinus Torvalds 	if (!md->queue)
7571da177e4SLinus Torvalds 		goto bad1;
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	md->queue->queuedata = md;
7601da177e4SLinus Torvalds 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
7611da177e4SLinus Torvalds 	md->queue->backing_dev_info.congested_data = md;
7621da177e4SLinus Torvalds 	blk_queue_make_request(md->queue, dm_request);
7631da177e4SLinus Torvalds 	md->queue->unplug_fn = dm_unplug_all;
7641da177e4SLinus Torvalds 	md->queue->issue_flush_fn = dm_flush_all;
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 	md->io_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
7671da177e4SLinus Torvalds 				     mempool_free_slab, _io_cache);
7681da177e4SLinus Torvalds  	if (!md->io_pool)
7691da177e4SLinus Torvalds  		goto bad2;
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 	md->tio_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
7721da177e4SLinus Torvalds 				      mempool_free_slab, _tio_cache);
7731da177e4SLinus Torvalds 	if (!md->tio_pool)
7741da177e4SLinus Torvalds 		goto bad3;
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
7771da177e4SLinus Torvalds 	if (!md->disk)
7781da177e4SLinus Torvalds 		goto bad4;
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	md->disk->major = _major;
7811da177e4SLinus Torvalds 	md->disk->first_minor = minor;
7821da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
7831da177e4SLinus Torvalds 	md->disk->queue = md->queue;
7841da177e4SLinus Torvalds 	md->disk->private_data = md;
7851da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
7861da177e4SLinus Torvalds 	add_disk(md->disk);
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	atomic_set(&md->pending, 0);
7891da177e4SLinus Torvalds 	init_waitqueue_head(&md->wait);
7901da177e4SLinus Torvalds 	init_waitqueue_head(&md->eventq);
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	return md;
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds  bad4:
7951da177e4SLinus Torvalds 	mempool_destroy(md->tio_pool);
7961da177e4SLinus Torvalds  bad3:
7971da177e4SLinus Torvalds 	mempool_destroy(md->io_pool);
7981da177e4SLinus Torvalds  bad2:
7991da177e4SLinus Torvalds 	blk_put_queue(md->queue);
8001da177e4SLinus Torvalds 	free_minor(minor);
8011da177e4SLinus Torvalds  bad1:
8021da177e4SLinus Torvalds 	kfree(md);
8031da177e4SLinus Torvalds 	return NULL;
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
8071da177e4SLinus Torvalds {
8081da177e4SLinus Torvalds 	free_minor(md->disk->first_minor);
8091da177e4SLinus Torvalds 	mempool_destroy(md->tio_pool);
8101da177e4SLinus Torvalds 	mempool_destroy(md->io_pool);
8111da177e4SLinus Torvalds 	del_gendisk(md->disk);
8121da177e4SLinus Torvalds 	put_disk(md->disk);
8131da177e4SLinus Torvalds 	blk_put_queue(md->queue);
8141da177e4SLinus Torvalds 	kfree(md);
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds /*
8181da177e4SLinus Torvalds  * Bind a table to the device.
8191da177e4SLinus Torvalds  */
8201da177e4SLinus Torvalds static void event_callback(void *context)
8211da177e4SLinus Torvalds {
8221da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
8251da177e4SLinus Torvalds 	wake_up(&md->eventq);
8261da177e4SLinus Torvalds }
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds static void __set_size(struct gendisk *disk, sector_t size)
8291da177e4SLinus Torvalds {
8301da177e4SLinus Torvalds 	struct block_device *bdev;
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	set_capacity(disk, size);
8331da177e4SLinus Torvalds 	bdev = bdget_disk(disk, 0);
8341da177e4SLinus Torvalds 	if (bdev) {
8351da177e4SLinus Torvalds 		down(&bdev->bd_inode->i_sem);
8361da177e4SLinus Torvalds 		i_size_write(bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
8371da177e4SLinus Torvalds 		up(&bdev->bd_inode->i_sem);
8381da177e4SLinus Torvalds 		bdput(bdev);
8391da177e4SLinus Torvalds 	}
8401da177e4SLinus Torvalds }
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds static int __bind(struct mapped_device *md, struct dm_table *t)
8431da177e4SLinus Torvalds {
8441da177e4SLinus Torvalds 	request_queue_t *q = md->queue;
8451da177e4SLinus Torvalds 	sector_t size;
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	size = dm_table_get_size(t);
8481da177e4SLinus Torvalds 	__set_size(md->disk, size);
8491da177e4SLinus Torvalds 	if (size == 0)
8501da177e4SLinus Torvalds 		return 0;
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 	write_lock(&md->map_lock);
8531da177e4SLinus Torvalds 	md->map = t;
8541da177e4SLinus Torvalds 	write_unlock(&md->map_lock);
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	dm_table_get(t);
8571da177e4SLinus Torvalds 	dm_table_event_callback(md->map, event_callback, md);
8581da177e4SLinus Torvalds 	dm_table_set_restrictions(t, q);
8591da177e4SLinus Torvalds 	return 0;
8601da177e4SLinus Torvalds }
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds static void __unbind(struct mapped_device *md)
8631da177e4SLinus Torvalds {
8641da177e4SLinus Torvalds 	struct dm_table *map = md->map;
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	if (!map)
8671da177e4SLinus Torvalds 		return;
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
8701da177e4SLinus Torvalds 	write_lock(&md->map_lock);
8711da177e4SLinus Torvalds 	md->map = NULL;
8721da177e4SLinus Torvalds 	write_unlock(&md->map_lock);
8731da177e4SLinus Torvalds 	dm_table_put(map);
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds /*
8771da177e4SLinus Torvalds  * Constructor for a new device.
8781da177e4SLinus Torvalds  */
8791da177e4SLinus Torvalds static int create_aux(unsigned int minor, int persistent,
8801da177e4SLinus Torvalds 		      struct mapped_device **result)
8811da177e4SLinus Torvalds {
8821da177e4SLinus Torvalds 	struct mapped_device *md;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	md = alloc_dev(minor, persistent);
8851da177e4SLinus Torvalds 	if (!md)
8861da177e4SLinus Torvalds 		return -ENXIO;
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	*result = md;
8891da177e4SLinus Torvalds 	return 0;
8901da177e4SLinus Torvalds }
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds int dm_create(struct mapped_device **result)
8931da177e4SLinus Torvalds {
8941da177e4SLinus Torvalds 	return create_aux(0, 0, result);
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds int dm_create_with_minor(unsigned int minor, struct mapped_device **result)
8981da177e4SLinus Torvalds {
8991da177e4SLinus Torvalds 	return create_aux(minor, 1, result);
9001da177e4SLinus Torvalds }
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds void *dm_get_mdptr(dev_t dev)
9031da177e4SLinus Torvalds {
9041da177e4SLinus Torvalds 	struct mapped_device *md;
9051da177e4SLinus Torvalds 	void *mdptr = NULL;
9061da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
9091da177e4SLinus Torvalds 		return NULL;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	down(&_minor_lock);
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	if (md && (dm_disk(md)->first_minor == minor))
9161da177e4SLinus Torvalds 		mdptr = md->interface_ptr;
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	up(&_minor_lock);
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 	return mdptr;
9211da177e4SLinus Torvalds }
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
9241da177e4SLinus Torvalds {
9251da177e4SLinus Torvalds 	md->interface_ptr = ptr;
9261da177e4SLinus Torvalds }
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
9291da177e4SLinus Torvalds {
9301da177e4SLinus Torvalds 	atomic_inc(&md->holders);
9311da177e4SLinus Torvalds }
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds void dm_put(struct mapped_device *md)
9341da177e4SLinus Torvalds {
9351da177e4SLinus Torvalds 	struct dm_table *map = dm_get_table(md);
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 	if (atomic_dec_and_test(&md->holders)) {
9381da177e4SLinus Torvalds 		if (!test_bit(DMF_SUSPENDED, &md->flags) && map) {
9391da177e4SLinus Torvalds 			dm_table_presuspend_targets(map);
9401da177e4SLinus Torvalds 			dm_table_postsuspend_targets(map);
9411da177e4SLinus Torvalds 		}
9421da177e4SLinus Torvalds 		__unbind(md);
9431da177e4SLinus Torvalds 		free_dev(md);
9441da177e4SLinus Torvalds 	}
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	dm_table_put(map);
9471da177e4SLinus Torvalds }
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds /*
9501da177e4SLinus Torvalds  * Process the deferred bios
9511da177e4SLinus Torvalds  */
9521da177e4SLinus Torvalds static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
9531da177e4SLinus Torvalds {
9541da177e4SLinus Torvalds 	struct bio *n;
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	while (c) {
9571da177e4SLinus Torvalds 		n = c->bi_next;
9581da177e4SLinus Torvalds 		c->bi_next = NULL;
9591da177e4SLinus Torvalds 		__split_bio(md, c);
9601da177e4SLinus Torvalds 		c = n;
9611da177e4SLinus Torvalds 	}
9621da177e4SLinus Torvalds }
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds /*
9651da177e4SLinus Torvalds  * Swap in a new table (destroying old one).
9661da177e4SLinus Torvalds  */
9671da177e4SLinus Torvalds int dm_swap_table(struct mapped_device *md, struct dm_table *table)
9681da177e4SLinus Torvalds {
969*93c534aeSAlasdair G Kergon 	int r = -EINVAL;
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	down_write(&md->lock);
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	/* device must be suspended */
974*93c534aeSAlasdair G Kergon 	if (!test_bit(DMF_SUSPENDED, &md->flags))
975*93c534aeSAlasdair G Kergon 		goto out;
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	__unbind(md);
9781da177e4SLinus Torvalds 	r = __bind(md, table);
9791da177e4SLinus Torvalds 
980*93c534aeSAlasdair G Kergon out:
9811da177e4SLinus Torvalds 	up_write(&md->lock);
982*93c534aeSAlasdair G Kergon 	return r;
9831da177e4SLinus Torvalds }
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds /*
9861da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
9871da177e4SLinus Torvalds  * device.
9881da177e4SLinus Torvalds  */
9891da177e4SLinus Torvalds static int __lock_fs(struct mapped_device *md)
9901da177e4SLinus Torvalds {
991dfbe03f6SAlasdair G Kergon 	int error = -ENOMEM;
992dfbe03f6SAlasdair G Kergon 
9931da177e4SLinus Torvalds 	if (test_and_set_bit(DMF_FS_LOCKED, &md->flags))
9941da177e4SLinus Torvalds 		return 0;
9951da177e4SLinus Torvalds 
996d1782a3bSAlasdair G Kergon 	md->frozen_bdev = bdget_disk(md->disk, 0);
997d1782a3bSAlasdair G Kergon 	if (!md->frozen_bdev) {
9981da177e4SLinus Torvalds 		DMWARN("bdget failed in __lock_fs");
999dfbe03f6SAlasdair G Kergon 		goto out;
10001da177e4SLinus Torvalds 	}
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
1003dfbe03f6SAlasdair G Kergon 
1004d1782a3bSAlasdair G Kergon 	md->frozen_sb = freeze_bdev(md->frozen_bdev);
1005dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
1006dfbe03f6SAlasdair G Kergon 		error = PTR_ERR(md->frozen_sb);
1007dfbe03f6SAlasdair G Kergon 		goto out_bdput;
1008dfbe03f6SAlasdair G Kergon 	}
1009dfbe03f6SAlasdair G Kergon 
10101da177e4SLinus Torvalds 	/* don't bdput right now, we don't want the bdev
10111da177e4SLinus Torvalds 	 * to go away while it is locked.  We'll bdput
10121da177e4SLinus Torvalds 	 * in __unlock_fs
10131da177e4SLinus Torvalds 	 */
10141da177e4SLinus Torvalds 	return 0;
1015dfbe03f6SAlasdair G Kergon 
1016dfbe03f6SAlasdair G Kergon out_bdput:
1017dfbe03f6SAlasdair G Kergon 	bdput(md->frozen_bdev);
1018dfbe03f6SAlasdair G Kergon 	md->frozen_sb = NULL;
1019dfbe03f6SAlasdair G Kergon 	md->frozen_bdev = NULL;
1020dfbe03f6SAlasdair G Kergon out:
1021dfbe03f6SAlasdair G Kergon 	clear_bit(DMF_FS_LOCKED, &md->flags);
1022dfbe03f6SAlasdair G Kergon 	return error;
10231da177e4SLinus Torvalds }
10241da177e4SLinus Torvalds 
10253dcee806SAlasdair G Kergon static void __unlock_fs(struct mapped_device *md)
10261da177e4SLinus Torvalds {
10271da177e4SLinus Torvalds 	if (!test_and_clear_bit(DMF_FS_LOCKED, &md->flags))
10283dcee806SAlasdair G Kergon 		return;
10291da177e4SLinus Torvalds 
1030d1782a3bSAlasdair G Kergon 	thaw_bdev(md->frozen_bdev, md->frozen_sb);
1031d1782a3bSAlasdair G Kergon 	bdput(md->frozen_bdev);
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 	md->frozen_sb = NULL;
1034d1782a3bSAlasdair G Kergon 	md->frozen_bdev = NULL;
10351da177e4SLinus Torvalds }
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds /*
10381da177e4SLinus Torvalds  * We need to be able to change a mapping table under a mounted
10391da177e4SLinus Torvalds  * filesystem.  For example we might want to move some data in
10401da177e4SLinus Torvalds  * the background.  Before the table can be swapped with
10411da177e4SLinus Torvalds  * dm_bind_table, dm_suspend must be called to flush any in
10421da177e4SLinus Torvalds  * flight bios and ensure that any further io gets deferred.
10431da177e4SLinus Torvalds  */
10441da177e4SLinus Torvalds int dm_suspend(struct mapped_device *md)
10451da177e4SLinus Torvalds {
10461da177e4SLinus Torvalds 	struct dm_table *map;
10471da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
1048b84b0287SAlasdair G Kergon 	int error = -EINVAL;
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	/* Flush I/O to the device. */
10511da177e4SLinus Torvalds 	down_read(&md->lock);
1052b84b0287SAlasdair G Kergon 	if (test_bit(DMF_BLOCK_IO, &md->flags))
1053b84b0287SAlasdair G Kergon 		goto out_read_unlock;
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	map = dm_get_table(md);
10561da177e4SLinus Torvalds 	if (map)
1057436d4108SAlasdair G Kergon 		/* This does not get reverted if there's an error later. */
10581da177e4SLinus Torvalds 		dm_table_presuspend_targets(map);
10591da177e4SLinus Torvalds 
1060436d4108SAlasdair G Kergon 	error = __lock_fs(md);
1061436d4108SAlasdair G Kergon 	if (error) {
1062436d4108SAlasdair G Kergon 		dm_table_put(map);
1063436d4108SAlasdair G Kergon 		goto out_read_unlock;
1064436d4108SAlasdair G Kergon 	}
1065436d4108SAlasdair G Kergon 
10661da177e4SLinus Torvalds 	up_read(&md->lock);
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	/*
1069354e0071SAlasdair G Kergon 	 * First we set the BLOCK_IO flag so no more ios will be mapped.
1070354e0071SAlasdair G Kergon 	 *
1071354e0071SAlasdair G Kergon 	 * If the flag is already set we know another thread is trying to
1072354e0071SAlasdair G Kergon 	 * suspend as well, so we leave the fs locked for this thread.
10731da177e4SLinus Torvalds 	 */
1074b84b0287SAlasdair G Kergon 	error = -EINVAL;
10751da177e4SLinus Torvalds 	down_write(&md->lock);
1076b84b0287SAlasdair G Kergon 	if (test_and_set_bit(DMF_BLOCK_IO, &md->flags)) {
1077354e0071SAlasdair G Kergon 		if (map)
1078354e0071SAlasdair G Kergon 			dm_table_put(map);
1079b84b0287SAlasdair G Kergon 		goto out_write_unlock;
10801da177e4SLinus Torvalds 	}
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 	add_wait_queue(&md->wait, &wait);
10831da177e4SLinus Torvalds 	up_write(&md->lock);
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 	/* unplug */
10861da177e4SLinus Torvalds 	if (map) {
10871da177e4SLinus Torvalds 		dm_table_unplug_all(map);
10881da177e4SLinus Torvalds 		dm_table_put(map);
10891da177e4SLinus Torvalds 	}
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 	/*
10921da177e4SLinus Torvalds 	 * Then we wait for the already mapped ios to
10931da177e4SLinus Torvalds 	 * complete.
10941da177e4SLinus Torvalds 	 */
10951da177e4SLinus Torvalds 	while (1) {
10961da177e4SLinus Torvalds 		set_current_state(TASK_INTERRUPTIBLE);
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 		if (!atomic_read(&md->pending) || signal_pending(current))
10991da177e4SLinus Torvalds 			break;
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds 		io_schedule();
11021da177e4SLinus Torvalds 	}
11031da177e4SLinus Torvalds 	set_current_state(TASK_RUNNING);
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds 	down_write(&md->lock);
11061da177e4SLinus Torvalds 	remove_wait_queue(&md->wait, &wait);
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 	/* were we interrupted ? */
1109b84b0287SAlasdair G Kergon 	error = -EINTR;
1110b84b0287SAlasdair G Kergon 	if (atomic_read(&md->pending))
1111b84b0287SAlasdair G Kergon 		goto out_unfreeze;
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 	map = dm_get_table(md);
11161da177e4SLinus Torvalds 	if (map)
11171da177e4SLinus Torvalds 		dm_table_postsuspend_targets(map);
11181da177e4SLinus Torvalds 	dm_table_put(map);
11191da177e4SLinus Torvalds 	up_write(&md->lock);
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	return 0;
1122b84b0287SAlasdair G Kergon 
1123b84b0287SAlasdair G Kergon out_unfreeze:
1124b84b0287SAlasdair G Kergon 	__unlock_fs(md);
1125b84b0287SAlasdair G Kergon 	clear_bit(DMF_BLOCK_IO, &md->flags);
1126b84b0287SAlasdair G Kergon out_write_unlock:
1127b84b0287SAlasdair G Kergon 	up_write(&md->lock);
1128b84b0287SAlasdair G Kergon 	return error;
1129b84b0287SAlasdair G Kergon 
1130b84b0287SAlasdair G Kergon out_read_unlock:
1131b84b0287SAlasdair G Kergon 	up_read(&md->lock);
1132b84b0287SAlasdair G Kergon 	return error;
11331da177e4SLinus Torvalds }
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds int dm_resume(struct mapped_device *md)
11361da177e4SLinus Torvalds {
11371da177e4SLinus Torvalds 	struct bio *def;
11381da177e4SLinus Torvalds 	struct dm_table *map = dm_get_table(md);
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 	down_write(&md->lock);
11411da177e4SLinus Torvalds 	if (!map ||
11421da177e4SLinus Torvalds 	    !test_bit(DMF_SUSPENDED, &md->flags) ||
11431da177e4SLinus Torvalds 	    !dm_table_get_size(map)) {
11441da177e4SLinus Torvalds 		up_write(&md->lock);
11451da177e4SLinus Torvalds 		dm_table_put(map);
11461da177e4SLinus Torvalds 		return -EINVAL;
11471da177e4SLinus Torvalds 	}
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 	dm_table_resume_targets(map);
11501da177e4SLinus Torvalds 	clear_bit(DMF_SUSPENDED, &md->flags);
11511da177e4SLinus Torvalds 	clear_bit(DMF_BLOCK_IO, &md->flags);
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds 	def = bio_list_get(&md->deferred);
11541da177e4SLinus Torvalds 	__flush_deferred_io(md, def);
11551da177e4SLinus Torvalds 	up_write(&md->lock);
11561da177e4SLinus Torvalds 	__unlock_fs(md);
11571da177e4SLinus Torvalds 	dm_table_unplug_all(map);
11581da177e4SLinus Torvalds 	dm_table_put(map);
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	return 0;
11611da177e4SLinus Torvalds }
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds /*-----------------------------------------------------------------
11641da177e4SLinus Torvalds  * Event notification.
11651da177e4SLinus Torvalds  *---------------------------------------------------------------*/
11661da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
11671da177e4SLinus Torvalds {
11681da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
11691da177e4SLinus Torvalds }
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
11721da177e4SLinus Torvalds {
11731da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
11741da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
11751da177e4SLinus Torvalds }
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds /*
11781da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
11791da177e4SLinus Torvalds  * count on 'md'.
11801da177e4SLinus Torvalds  */
11811da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
11821da177e4SLinus Torvalds {
11831da177e4SLinus Torvalds 	return md->disk;
11841da177e4SLinus Torvalds }
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds int dm_suspended(struct mapped_device *md)
11871da177e4SLinus Torvalds {
11881da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
11891da177e4SLinus Torvalds }
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops = {
11921da177e4SLinus Torvalds 	.open = dm_blk_open,
11931da177e4SLinus Torvalds 	.release = dm_blk_close,
11941da177e4SLinus Torvalds 	.owner = THIS_MODULE
11951da177e4SLinus Torvalds };
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo);
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds /*
12001da177e4SLinus Torvalds  * module hooks
12011da177e4SLinus Torvalds  */
12021da177e4SLinus Torvalds module_init(dm_init);
12031da177e4SLinus Torvalds module_exit(dm_exit);
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds module_param(major, uint, 0);
12061da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
12071da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
12081da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
12091da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1210