xref: /openbmc/linux/drivers/md/dm.c (revision a3d77d35be6f416a250c528c3ed5c70013a915e8)
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  */
481da177e4SLinus Torvalds struct 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)
571da177e4SLinus Torvalds 		return &((struct 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
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds struct mapped_device {
732ca3310eSAlasdair G Kergon 	struct rw_semaphore io_lock;
742ca3310eSAlasdair G Kergon 	struct semaphore suspend_lock;
751da177e4SLinus Torvalds 	rwlock_t map_lock;
761da177e4SLinus Torvalds 	atomic_t holders;
775c6bd75dSAlasdair G Kergon 	atomic_t open_count;
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	unsigned long flags;
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	request_queue_t *queue;
821da177e4SLinus Torvalds 	struct gendisk *disk;
837e51f257SMike Anderson 	char name[16];
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	void *interface_ptr;
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	/*
881da177e4SLinus Torvalds 	 * A list of ios that arrived while we were suspended.
891da177e4SLinus Torvalds 	 */
901da177e4SLinus Torvalds 	atomic_t pending;
911da177e4SLinus Torvalds 	wait_queue_head_t wait;
921da177e4SLinus Torvalds 	struct bio_list deferred;
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	/*
951da177e4SLinus Torvalds 	 * The current mapping.
961da177e4SLinus Torvalds 	 */
971da177e4SLinus Torvalds 	struct dm_table *map;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	/*
1001da177e4SLinus Torvalds 	 * io objects are allocated from here.
1011da177e4SLinus Torvalds 	 */
1021da177e4SLinus Torvalds 	mempool_t *io_pool;
1031da177e4SLinus Torvalds 	mempool_t *tio_pool;
1041da177e4SLinus Torvalds 
1059faf400fSStefan Bader 	struct bio_set *bs;
1069faf400fSStefan Bader 
1071da177e4SLinus Torvalds 	/*
1081da177e4SLinus Torvalds 	 * Event handling.
1091da177e4SLinus Torvalds 	 */
1101da177e4SLinus Torvalds 	atomic_t event_nr;
1111da177e4SLinus Torvalds 	wait_queue_head_t eventq;
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	/*
1141da177e4SLinus Torvalds 	 * freeze/thaw support require holding onto a super block
1151da177e4SLinus Torvalds 	 */
1161da177e4SLinus Torvalds 	struct super_block *frozen_sb;
117e39e2e95SAlasdair G Kergon 	struct block_device *suspended_bdev;
1183ac51e74SDarrick J. Wong 
1193ac51e74SDarrick J. Wong 	/* forced geometry settings */
1203ac51e74SDarrick J. Wong 	struct hd_geometry geometry;
1211da177e4SLinus Torvalds };
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds #define MIN_IOS 256
124e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
125e18b890bSChristoph Lameter static struct kmem_cache *_tio_cache;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds static int __init local_init(void)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	int r;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
1321da177e4SLinus Torvalds 	_io_cache = kmem_cache_create("dm_io",
1331da177e4SLinus Torvalds 				      sizeof(struct dm_io), 0, 0, NULL, NULL);
1341da177e4SLinus Torvalds 	if (!_io_cache)
1351da177e4SLinus Torvalds 		return -ENOMEM;
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	/* allocate a slab for the target ios */
1381da177e4SLinus Torvalds 	_tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io),
1391da177e4SLinus Torvalds 				       0, 0, NULL, NULL);
1401da177e4SLinus Torvalds 	if (!_tio_cache) {
1411da177e4SLinus Torvalds 		kmem_cache_destroy(_io_cache);
1421da177e4SLinus Torvalds 		return -ENOMEM;
1431da177e4SLinus Torvalds 	}
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	_major = major;
1461da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
1471da177e4SLinus Torvalds 	if (r < 0) {
1481da177e4SLinus Torvalds 		kmem_cache_destroy(_tio_cache);
1491da177e4SLinus Torvalds 		kmem_cache_destroy(_io_cache);
1501da177e4SLinus Torvalds 		return r;
1511da177e4SLinus Torvalds 	}
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	if (!_major)
1541da177e4SLinus Torvalds 		_major = r;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	return 0;
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds static void local_exit(void)
1601da177e4SLinus Torvalds {
1611da177e4SLinus Torvalds 	kmem_cache_destroy(_tio_cache);
1621da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	if (unregister_blkdev(_major, _name) < 0)
165890fbae2SGreg Kroah-Hartman 		DMERR("unregister_blkdev failed");
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	_major = 0;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	DMINFO("cleaned up");
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds int (*_inits[])(void) __initdata = {
1731da177e4SLinus Torvalds 	local_init,
1741da177e4SLinus Torvalds 	dm_target_init,
1751da177e4SLinus Torvalds 	dm_linear_init,
1761da177e4SLinus Torvalds 	dm_stripe_init,
1771da177e4SLinus Torvalds 	dm_interface_init,
1781da177e4SLinus Torvalds };
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds void (*_exits[])(void) = {
1811da177e4SLinus Torvalds 	local_exit,
1821da177e4SLinus Torvalds 	dm_target_exit,
1831da177e4SLinus Torvalds 	dm_linear_exit,
1841da177e4SLinus Torvalds 	dm_stripe_exit,
1851da177e4SLinus Torvalds 	dm_interface_exit,
1861da177e4SLinus Torvalds };
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds static int __init dm_init(void)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	int r, i;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
1951da177e4SLinus Torvalds 		r = _inits[i]();
1961da177e4SLinus Torvalds 		if (r)
1971da177e4SLinus Torvalds 			goto bad;
1981da177e4SLinus Torvalds 	}
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	return 0;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds       bad:
2031da177e4SLinus Torvalds 	while (i--)
2041da177e4SLinus Torvalds 		_exits[i]();
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	return r;
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds static void __exit dm_exit(void)
2101da177e4SLinus Torvalds {
2111da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	while (i--)
2141da177e4SLinus Torvalds 		_exits[i]();
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds /*
2181da177e4SLinus Torvalds  * Block device functions
2191da177e4SLinus Torvalds  */
2201da177e4SLinus Torvalds static int dm_blk_open(struct inode *inode, struct file *file)
2211da177e4SLinus Torvalds {
2221da177e4SLinus Torvalds 	struct mapped_device *md;
2231da177e4SLinus Torvalds 
224fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
225fba9f90eSJeff Mahoney 
2261da177e4SLinus Torvalds 	md = inode->i_bdev->bd_disk->private_data;
227fba9f90eSJeff Mahoney 	if (!md)
228fba9f90eSJeff Mahoney 		goto out;
229fba9f90eSJeff Mahoney 
2305c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
2315c6bd75dSAlasdair G Kergon 	    test_bit(DMF_DELETING, &md->flags)) {
232fba9f90eSJeff Mahoney 		md = NULL;
233fba9f90eSJeff Mahoney 		goto out;
234fba9f90eSJeff Mahoney 	}
235fba9f90eSJeff Mahoney 
2361da177e4SLinus Torvalds 	dm_get(md);
2375c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
238fba9f90eSJeff Mahoney 
239fba9f90eSJeff Mahoney out:
240fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
241fba9f90eSJeff Mahoney 
242fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds static int dm_blk_close(struct inode *inode, struct file *file)
2461da177e4SLinus Torvalds {
2471da177e4SLinus Torvalds 	struct mapped_device *md;
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	md = inode->i_bdev->bd_disk->private_data;
2505c6bd75dSAlasdair G Kergon 	atomic_dec(&md->open_count);
2511da177e4SLinus Torvalds 	dm_put(md);
2521da177e4SLinus Torvalds 	return 0;
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
2555c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
2565c6bd75dSAlasdair G Kergon {
2575c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
2585c6bd75dSAlasdair G Kergon }
2595c6bd75dSAlasdair G Kergon 
2605c6bd75dSAlasdair G Kergon /*
2615c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
2625c6bd75dSAlasdair G Kergon  */
2635c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md)
2645c6bd75dSAlasdair G Kergon {
2655c6bd75dSAlasdair G Kergon 	int r = 0;
2665c6bd75dSAlasdair G Kergon 
2675c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
2685c6bd75dSAlasdair G Kergon 
2695c6bd75dSAlasdair G Kergon 	if (dm_open_count(md))
2705c6bd75dSAlasdair G Kergon 		r = -EBUSY;
2715c6bd75dSAlasdair G Kergon 	else
2725c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
2735c6bd75dSAlasdair G Kergon 
2745c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
2755c6bd75dSAlasdair G Kergon 
2765c6bd75dSAlasdair G Kergon 	return r;
2775c6bd75dSAlasdair G Kergon }
2785c6bd75dSAlasdair G Kergon 
2793ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
2803ac51e74SDarrick J. Wong {
2813ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
2823ac51e74SDarrick J. Wong 
2833ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
2843ac51e74SDarrick J. Wong }
2853ac51e74SDarrick J. Wong 
286aa129a22SMilan Broz static int dm_blk_ioctl(struct inode *inode, struct file *file,
287aa129a22SMilan Broz 			unsigned int cmd, unsigned long arg)
288aa129a22SMilan Broz {
289aa129a22SMilan Broz 	struct mapped_device *md;
290aa129a22SMilan Broz 	struct dm_table *map;
291aa129a22SMilan Broz 	struct dm_target *tgt;
292aa129a22SMilan Broz 	int r = -ENOTTY;
293aa129a22SMilan Broz 
294aa129a22SMilan Broz 	/* We don't really need this lock, but we do need 'inode'. */
295aa129a22SMilan Broz 	unlock_kernel();
296aa129a22SMilan Broz 
297aa129a22SMilan Broz 	md = inode->i_bdev->bd_disk->private_data;
298aa129a22SMilan Broz 
299aa129a22SMilan Broz 	map = dm_get_table(md);
300aa129a22SMilan Broz 
301aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
302aa129a22SMilan Broz 		goto out;
303aa129a22SMilan Broz 
304aa129a22SMilan Broz 	/* We only support devices that have a single target */
305aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
306aa129a22SMilan Broz 		goto out;
307aa129a22SMilan Broz 
308aa129a22SMilan Broz 	tgt = dm_table_get_target(map, 0);
309aa129a22SMilan Broz 
310aa129a22SMilan Broz 	if (dm_suspended(md)) {
311aa129a22SMilan Broz 		r = -EAGAIN;
312aa129a22SMilan Broz 		goto out;
313aa129a22SMilan Broz 	}
314aa129a22SMilan Broz 
315aa129a22SMilan Broz 	if (tgt->type->ioctl)
316aa129a22SMilan Broz 		r = tgt->type->ioctl(tgt, inode, file, cmd, arg);
317aa129a22SMilan Broz 
318aa129a22SMilan Broz out:
319aa129a22SMilan Broz 	dm_table_put(map);
320aa129a22SMilan Broz 
321aa129a22SMilan Broz 	lock_kernel();
322aa129a22SMilan Broz 	return r;
323aa129a22SMilan Broz }
324aa129a22SMilan Broz 
3251da177e4SLinus Torvalds static inline struct dm_io *alloc_io(struct mapped_device *md)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds static inline void free_io(struct mapped_device *md, struct dm_io *io)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds static inline struct target_io *alloc_tio(struct mapped_device *md)
3361da177e4SLinus Torvalds {
3371da177e4SLinus Torvalds 	return mempool_alloc(md->tio_pool, GFP_NOIO);
3381da177e4SLinus Torvalds }
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds static inline void free_tio(struct mapped_device *md, struct target_io *tio)
3411da177e4SLinus Torvalds {
3421da177e4SLinus Torvalds 	mempool_free(tio, md->tio_pool);
3431da177e4SLinus Torvalds }
3441da177e4SLinus Torvalds 
3453eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
3463eaf840eSJun'ichi "Nick" Nomura {
3473eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
3483eaf840eSJun'ichi "Nick" Nomura 
3493eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
3503eaf840eSJun'ichi "Nick" Nomura 
3513eaf840eSJun'ichi "Nick" Nomura 	preempt_disable();
3523eaf840eSJun'ichi "Nick" Nomura 	disk_round_stats(dm_disk(md));
3533eaf840eSJun'ichi "Nick" Nomura 	preempt_enable();
3543eaf840eSJun'ichi "Nick" Nomura 	dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
3553eaf840eSJun'ichi "Nick" Nomura }
3563eaf840eSJun'ichi "Nick" Nomura 
3573eaf840eSJun'ichi "Nick" Nomura static int end_io_acct(struct dm_io *io)
3583eaf840eSJun'ichi "Nick" Nomura {
3593eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
3603eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
3613eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
3623eaf840eSJun'ichi "Nick" Nomura 	int pending;
3633eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
3643eaf840eSJun'ichi "Nick" Nomura 
3653eaf840eSJun'ichi "Nick" Nomura 	preempt_disable();
3663eaf840eSJun'ichi "Nick" Nomura 	disk_round_stats(dm_disk(md));
3673eaf840eSJun'ichi "Nick" Nomura 	preempt_enable();
3683eaf840eSJun'ichi "Nick" Nomura 	dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
3693eaf840eSJun'ichi "Nick" Nomura 
3703eaf840eSJun'ichi "Nick" Nomura 	disk_stat_add(dm_disk(md), ticks[rw], duration);
3713eaf840eSJun'ichi "Nick" Nomura 
3723eaf840eSJun'ichi "Nick" Nomura 	return !pending;
3733eaf840eSJun'ichi "Nick" Nomura }
3743eaf840eSJun'ichi "Nick" Nomura 
3751da177e4SLinus Torvalds /*
3761da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
3771da177e4SLinus Torvalds  */
3781da177e4SLinus Torvalds static int queue_io(struct mapped_device *md, struct bio *bio)
3791da177e4SLinus Torvalds {
3802ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
3832ca3310eSAlasdair G Kergon 		up_write(&md->io_lock);
3841da177e4SLinus Torvalds 		return 1;
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
3881da177e4SLinus Torvalds 
3892ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
3901da177e4SLinus Torvalds 	return 0;		/* deferred successfully */
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds /*
3941da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
3951da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
3961da177e4SLinus Torvalds  * dm_table_put() when finished.
3971da177e4SLinus Torvalds  */
3981da177e4SLinus Torvalds struct dm_table *dm_get_table(struct mapped_device *md)
3991da177e4SLinus Torvalds {
4001da177e4SLinus Torvalds 	struct dm_table *t;
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	read_lock(&md->map_lock);
4031da177e4SLinus Torvalds 	t = md->map;
4041da177e4SLinus Torvalds 	if (t)
4051da177e4SLinus Torvalds 		dm_table_get(t);
4061da177e4SLinus Torvalds 	read_unlock(&md->map_lock);
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	return t;
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds 
4113ac51e74SDarrick J. Wong /*
4123ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
4133ac51e74SDarrick J. Wong  */
4143ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
4153ac51e74SDarrick J. Wong {
4163ac51e74SDarrick J. Wong 	*geo = md->geometry;
4173ac51e74SDarrick J. Wong 
4183ac51e74SDarrick J. Wong 	return 0;
4193ac51e74SDarrick J. Wong }
4203ac51e74SDarrick J. Wong 
4213ac51e74SDarrick J. Wong /*
4223ac51e74SDarrick J. Wong  * Set the geometry of a device.
4233ac51e74SDarrick J. Wong  */
4243ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
4253ac51e74SDarrick J. Wong {
4263ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
4273ac51e74SDarrick J. Wong 
4283ac51e74SDarrick J. Wong 	if (geo->start > sz) {
4293ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
4303ac51e74SDarrick J. Wong 		return -EINVAL;
4313ac51e74SDarrick J. Wong 	}
4323ac51e74SDarrick J. Wong 
4333ac51e74SDarrick J. Wong 	md->geometry = *geo;
4343ac51e74SDarrick J. Wong 
4353ac51e74SDarrick J. Wong 	return 0;
4363ac51e74SDarrick J. Wong }
4373ac51e74SDarrick J. Wong 
4381da177e4SLinus Torvalds /*-----------------------------------------------------------------
4391da177e4SLinus Torvalds  * CRUD START:
4401da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
4411da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
4421da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
4431da177e4SLinus Torvalds  *   interests of getting something for people to use I give
4441da177e4SLinus Torvalds  *   you this clearly demarcated crap.
4451da177e4SLinus Torvalds  *---------------------------------------------------------------*/
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds /*
4481da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
4491da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
4501da177e4SLinus Torvalds  */
451858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
4521da177e4SLinus Torvalds {
4531da177e4SLinus Torvalds 	if (error)
4541da177e4SLinus Torvalds 		io->error = error;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
4573eaf840eSJun'ichi "Nick" Nomura 		if (end_io_acct(io))
4581da177e4SLinus Torvalds 			/* nudge anyone waiting on suspend queue */
4591da177e4SLinus Torvalds 			wake_up(&io->md->wait);
4601da177e4SLinus Torvalds 
4612056a782SJens Axboe 		blk_add_trace_bio(io->md->queue, io->bio, BLK_TA_COMPLETE);
4622056a782SJens Axboe 
4631da177e4SLinus Torvalds 		bio_endio(io->bio, io->bio->bi_size, io->error);
4641da177e4SLinus Torvalds 		free_io(io->md, io);
4651da177e4SLinus Torvalds 	}
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds static int clone_endio(struct bio *bio, unsigned int done, int error)
4691da177e4SLinus Torvalds {
4701da177e4SLinus Torvalds 	int r = 0;
4711da177e4SLinus Torvalds 	struct target_io *tio = bio->bi_private;
4729faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
4731da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	if (bio->bi_size)
4761da177e4SLinus Torvalds 		return 1;
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	if (!bio_flagged(bio, BIO_UPTODATE) && !error)
4791da177e4SLinus Torvalds 		error = -EIO;
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	if (endio) {
4821da177e4SLinus Torvalds 		r = endio(tio->ti, bio, error, &tio->info);
4831da177e4SLinus Torvalds 		if (r < 0)
4841da177e4SLinus Torvalds 			error = r;
4851da177e4SLinus Torvalds 		else if (r > 0)
4861da177e4SLinus Torvalds 			/* the target wants another shot at the io */
4871da177e4SLinus Torvalds 			return 1;
4881da177e4SLinus Torvalds 	}
4891da177e4SLinus Torvalds 
4909faf400fSStefan Bader 	dec_pending(tio->io, error);
4919faf400fSStefan Bader 
4929faf400fSStefan Bader 	/*
4939faf400fSStefan Bader 	 * Store md for cleanup instead of tio which is about to get freed.
4949faf400fSStefan Bader 	 */
4959faf400fSStefan Bader 	bio->bi_private = md->bs;
4969faf400fSStefan Bader 
4971da177e4SLinus Torvalds 	bio_put(bio);
4989faf400fSStefan Bader 	free_tio(md, tio);
4991da177e4SLinus Torvalds 	return r;
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds static sector_t max_io_len(struct mapped_device *md,
5031da177e4SLinus Torvalds 			   sector_t sector, struct dm_target *ti)
5041da177e4SLinus Torvalds {
5051da177e4SLinus Torvalds 	sector_t offset = sector - ti->begin;
5061da177e4SLinus Torvalds 	sector_t len = ti->len - offset;
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 	/*
5091da177e4SLinus Torvalds 	 * Does the target need to split even further ?
5101da177e4SLinus Torvalds 	 */
5111da177e4SLinus Torvalds 	if (ti->split_io) {
5121da177e4SLinus Torvalds 		sector_t boundary;
5131da177e4SLinus Torvalds 		boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
5141da177e4SLinus Torvalds 			   - offset;
5151da177e4SLinus Torvalds 		if (len > boundary)
5161da177e4SLinus Torvalds 			len = boundary;
5171da177e4SLinus Torvalds 	}
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	return len;
5201da177e4SLinus Torvalds }
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds static void __map_bio(struct dm_target *ti, struct bio *clone,
5231da177e4SLinus Torvalds 		      struct target_io *tio)
5241da177e4SLinus Torvalds {
5251da177e4SLinus Torvalds 	int r;
5262056a782SJens Axboe 	sector_t sector;
5279faf400fSStefan Bader 	struct mapped_device *md;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	/*
5301da177e4SLinus Torvalds 	 * Sanity checks.
5311da177e4SLinus Torvalds 	 */
5321da177e4SLinus Torvalds 	BUG_ON(!clone->bi_size);
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
5351da177e4SLinus Torvalds 	clone->bi_private = tio;
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	/*
5381da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
5391da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
5401da177e4SLinus Torvalds 	 * this io.
5411da177e4SLinus Torvalds 	 */
5421da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
5432056a782SJens Axboe 	sector = clone->bi_sector;
5441da177e4SLinus Torvalds 	r = ti->type->map(ti, clone, &tio->info);
5452056a782SJens Axboe 	if (r > 0) {
5461da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
5472056a782SJens Axboe 
5482056a782SJens Axboe 		blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
5492056a782SJens Axboe 				    tio->io->bio->bi_bdev->bd_dev, sector,
5502056a782SJens Axboe 				    clone->bi_sector);
5512056a782SJens Axboe 
5521da177e4SLinus Torvalds 		generic_make_request(clone);
55374859364SKiyoshi Ueda 	} else if (r < 0) {
5541da177e4SLinus Torvalds 		/* error the io and bail out */
5559faf400fSStefan Bader 		md = tio->io->md;
5569faf400fSStefan Bader 		dec_pending(tio->io, r);
5579faf400fSStefan Bader 		/*
5589faf400fSStefan Bader 		 * Store bio_set for cleanup.
5599faf400fSStefan Bader 		 */
5609faf400fSStefan Bader 		clone->bi_private = md->bs;
5611da177e4SLinus Torvalds 		bio_put(clone);
5629faf400fSStefan Bader 		free_tio(md, tio);
5631da177e4SLinus Torvalds 	}
5641da177e4SLinus Torvalds }
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds struct clone_info {
5671da177e4SLinus Torvalds 	struct mapped_device *md;
5681da177e4SLinus Torvalds 	struct dm_table *map;
5691da177e4SLinus Torvalds 	struct bio *bio;
5701da177e4SLinus Torvalds 	struct dm_io *io;
5711da177e4SLinus Torvalds 	sector_t sector;
5721da177e4SLinus Torvalds 	sector_t sector_count;
5731da177e4SLinus Torvalds 	unsigned short idx;
5741da177e4SLinus Torvalds };
5751da177e4SLinus Torvalds 
5763676347aSPeter Osterlund static void dm_bio_destructor(struct bio *bio)
5773676347aSPeter Osterlund {
5789faf400fSStefan Bader 	struct bio_set *bs = bio->bi_private;
5799faf400fSStefan Bader 
5809faf400fSStefan Bader 	bio_free(bio, bs);
5813676347aSPeter Osterlund }
5823676347aSPeter Osterlund 
5831da177e4SLinus Torvalds /*
5841da177e4SLinus Torvalds  * Creates a little bio that is just does part of a bvec.
5851da177e4SLinus Torvalds  */
5861da177e4SLinus Torvalds static struct bio *split_bvec(struct bio *bio, sector_t sector,
5871da177e4SLinus Torvalds 			      unsigned short idx, unsigned int offset,
5889faf400fSStefan Bader 			      unsigned int len, struct bio_set *bs)
5891da177e4SLinus Torvalds {
5901da177e4SLinus Torvalds 	struct bio *clone;
5911da177e4SLinus Torvalds 	struct bio_vec *bv = bio->bi_io_vec + idx;
5921da177e4SLinus Torvalds 
5939faf400fSStefan Bader 	clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
5943676347aSPeter Osterlund 	clone->bi_destructor = dm_bio_destructor;
5951da177e4SLinus Torvalds 	*clone->bi_io_vec = *bv;
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	clone->bi_sector = sector;
5981da177e4SLinus Torvalds 	clone->bi_bdev = bio->bi_bdev;
5991da177e4SLinus Torvalds 	clone->bi_rw = bio->bi_rw;
6001da177e4SLinus Torvalds 	clone->bi_vcnt = 1;
6011da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
6021da177e4SLinus Torvalds 	clone->bi_io_vec->bv_offset = offset;
6031da177e4SLinus Torvalds 	clone->bi_io_vec->bv_len = clone->bi_size;
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	return clone;
6061da177e4SLinus Torvalds }
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds /*
6091da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
6101da177e4SLinus Torvalds  */
6111da177e4SLinus Torvalds static struct bio *clone_bio(struct bio *bio, sector_t sector,
6121da177e4SLinus Torvalds 			     unsigned short idx, unsigned short bv_count,
6139faf400fSStefan Bader 			     unsigned int len, struct bio_set *bs)
6141da177e4SLinus Torvalds {
6151da177e4SLinus Torvalds 	struct bio *clone;
6161da177e4SLinus Torvalds 
6179faf400fSStefan Bader 	clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
6189faf400fSStefan Bader 	__bio_clone(clone, bio);
6199faf400fSStefan Bader 	clone->bi_destructor = dm_bio_destructor;
6201da177e4SLinus Torvalds 	clone->bi_sector = sector;
6211da177e4SLinus Torvalds 	clone->bi_idx = idx;
6221da177e4SLinus Torvalds 	clone->bi_vcnt = idx + bv_count;
6231da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
6241da177e4SLinus Torvalds 	clone->bi_flags &= ~(1 << BIO_SEG_VALID);
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	return clone;
6271da177e4SLinus Torvalds }
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds static void __clone_and_map(struct clone_info *ci)
6301da177e4SLinus Torvalds {
6311da177e4SLinus Torvalds 	struct bio *clone, *bio = ci->bio;
6321da177e4SLinus Torvalds 	struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
6331da177e4SLinus Torvalds 	sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
6341da177e4SLinus Torvalds 	struct target_io *tio;
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	/*
6371da177e4SLinus Torvalds 	 * Allocate a target io object.
6381da177e4SLinus Torvalds 	 */
6391da177e4SLinus Torvalds 	tio = alloc_tio(ci->md);
6401da177e4SLinus Torvalds 	tio->io = ci->io;
6411da177e4SLinus Torvalds 	tio->ti = ti;
6421da177e4SLinus Torvalds 	memset(&tio->info, 0, sizeof(tio->info));
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	if (ci->sector_count <= max) {
6451da177e4SLinus Torvalds 		/*
6461da177e4SLinus Torvalds 		 * Optimise for the simple case where we can do all of
6471da177e4SLinus Torvalds 		 * the remaining io with a single clone.
6481da177e4SLinus Torvalds 		 */
6491da177e4SLinus Torvalds 		clone = clone_bio(bio, ci->sector, ci->idx,
6509faf400fSStefan Bader 				  bio->bi_vcnt - ci->idx, ci->sector_count,
6519faf400fSStefan Bader 				  ci->md->bs);
6521da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
6531da177e4SLinus Torvalds 		ci->sector_count = 0;
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 	} else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
6561da177e4SLinus Torvalds 		/*
6571da177e4SLinus Torvalds 		 * There are some bvecs that don't span targets.
6581da177e4SLinus Torvalds 		 * Do as many of these as possible.
6591da177e4SLinus Torvalds 		 */
6601da177e4SLinus Torvalds 		int i;
6611da177e4SLinus Torvalds 		sector_t remaining = max;
6621da177e4SLinus Torvalds 		sector_t bv_len;
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 		for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
6651da177e4SLinus Torvalds 			bv_len = to_sector(bio->bi_io_vec[i].bv_len);
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 			if (bv_len > remaining)
6681da177e4SLinus Torvalds 				break;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 			remaining -= bv_len;
6711da177e4SLinus Torvalds 			len += bv_len;
6721da177e4SLinus Torvalds 		}
6731da177e4SLinus Torvalds 
6749faf400fSStefan Bader 		clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
6759faf400fSStefan Bader 				  ci->md->bs);
6761da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 		ci->sector += len;
6791da177e4SLinus Torvalds 		ci->sector_count -= len;
6801da177e4SLinus Torvalds 		ci->idx = i;
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	} else {
6831da177e4SLinus Torvalds 		/*
684d2044a94SAlasdair G Kergon 		 * Handle a bvec that must be split between two or more targets.
6851da177e4SLinus Torvalds 		 */
6861da177e4SLinus Torvalds 		struct bio_vec *bv = bio->bi_io_vec + ci->idx;
687d2044a94SAlasdair G Kergon 		sector_t remaining = to_sector(bv->bv_len);
688d2044a94SAlasdair G Kergon 		unsigned int offset = 0;
6891da177e4SLinus Torvalds 
690d2044a94SAlasdair G Kergon 		do {
691d2044a94SAlasdair G Kergon 			if (offset) {
6921da177e4SLinus Torvalds 				ti = dm_table_find_target(ci->map, ci->sector);
693d2044a94SAlasdair G Kergon 				max = max_io_len(ci->md, ci->sector, ti);
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 				tio = alloc_tio(ci->md);
6961da177e4SLinus Torvalds 				tio->io = ci->io;
6971da177e4SLinus Torvalds 				tio->ti = ti;
6981da177e4SLinus Torvalds 				memset(&tio->info, 0, sizeof(tio->info));
699d2044a94SAlasdair G Kergon 			}
700d2044a94SAlasdair G Kergon 
701d2044a94SAlasdair G Kergon 			len = min(remaining, max);
702d2044a94SAlasdair G Kergon 
703d2044a94SAlasdair G Kergon 			clone = split_bvec(bio, ci->sector, ci->idx,
7049faf400fSStefan Bader 					   bv->bv_offset + offset, len,
7059faf400fSStefan Bader 					   ci->md->bs);
706d2044a94SAlasdair G Kergon 
7071da177e4SLinus Torvalds 			__map_bio(ti, clone, tio);
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 			ci->sector += len;
7101da177e4SLinus Torvalds 			ci->sector_count -= len;
711d2044a94SAlasdair G Kergon 			offset += to_bytes(len);
712d2044a94SAlasdair G Kergon 		} while (remaining -= len);
713d2044a94SAlasdair G Kergon 
7141da177e4SLinus Torvalds 		ci->idx++;
7151da177e4SLinus Torvalds 	}
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds /*
7191da177e4SLinus Torvalds  * Split the bio into several clones.
7201da177e4SLinus Torvalds  */
7211da177e4SLinus Torvalds static void __split_bio(struct mapped_device *md, struct bio *bio)
7221da177e4SLinus Torvalds {
7231da177e4SLinus Torvalds 	struct clone_info ci;
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	ci.map = dm_get_table(md);
7261da177e4SLinus Torvalds 	if (!ci.map) {
7271da177e4SLinus Torvalds 		bio_io_error(bio, bio->bi_size);
7281da177e4SLinus Torvalds 		return;
7291da177e4SLinus Torvalds 	}
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	ci.md = md;
7321da177e4SLinus Torvalds 	ci.bio = bio;
7331da177e4SLinus Torvalds 	ci.io = alloc_io(md);
7341da177e4SLinus Torvalds 	ci.io->error = 0;
7351da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
7361da177e4SLinus Torvalds 	ci.io->bio = bio;
7371da177e4SLinus Torvalds 	ci.io->md = md;
7381da177e4SLinus Torvalds 	ci.sector = bio->bi_sector;
7391da177e4SLinus Torvalds 	ci.sector_count = bio_sectors(bio);
7401da177e4SLinus Torvalds 	ci.idx = bio->bi_idx;
7411da177e4SLinus Torvalds 
7423eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
7431da177e4SLinus Torvalds 	while (ci.sector_count)
7441da177e4SLinus Torvalds 		__clone_and_map(&ci);
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	/* drop the extra reference count */
7471da177e4SLinus Torvalds 	dec_pending(ci.io, 0);
7481da177e4SLinus Torvalds 	dm_table_put(ci.map);
7491da177e4SLinus Torvalds }
7501da177e4SLinus Torvalds /*-----------------------------------------------------------------
7511da177e4SLinus Torvalds  * CRUD END
7521da177e4SLinus Torvalds  *---------------------------------------------------------------*/
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds /*
7551da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
7561da177e4SLinus Torvalds  * dm_merge_bvec.
7571da177e4SLinus Torvalds  */
7581da177e4SLinus Torvalds static int dm_request(request_queue_t *q, struct bio *bio)
7591da177e4SLinus Torvalds {
7601da177e4SLinus Torvalds 	int r;
76112f03a49SKevin Corry 	int rw = bio_data_dir(bio);
7621da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
7631da177e4SLinus Torvalds 
7642ca3310eSAlasdair G Kergon 	down_read(&md->io_lock);
7651da177e4SLinus Torvalds 
76612f03a49SKevin Corry 	disk_stat_inc(dm_disk(md), ios[rw]);
76712f03a49SKevin Corry 	disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
76812f03a49SKevin Corry 
7691da177e4SLinus Torvalds 	/*
7701da177e4SLinus Torvalds 	 * If we're suspended we have to queue
7711da177e4SLinus Torvalds 	 * this io for later.
7721da177e4SLinus Torvalds 	 */
7731da177e4SLinus Torvalds 	while (test_bit(DMF_BLOCK_IO, &md->flags)) {
7742ca3310eSAlasdair G Kergon 		up_read(&md->io_lock);
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 		if (bio_rw(bio) == READA) {
7771da177e4SLinus Torvalds 			bio_io_error(bio, bio->bi_size);
7781da177e4SLinus Torvalds 			return 0;
7791da177e4SLinus Torvalds 		}
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 		r = queue_io(md, bio);
7821da177e4SLinus Torvalds 		if (r < 0) {
7831da177e4SLinus Torvalds 			bio_io_error(bio, bio->bi_size);
7841da177e4SLinus Torvalds 			return 0;
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 		} else if (r == 0)
7871da177e4SLinus Torvalds 			return 0;	/* deferred successfully */
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 		/*
7901da177e4SLinus Torvalds 		 * We're in a while loop, because someone could suspend
7911da177e4SLinus Torvalds 		 * before we get to the following read lock.
7921da177e4SLinus Torvalds 		 */
7932ca3310eSAlasdair G Kergon 		down_read(&md->io_lock);
7941da177e4SLinus Torvalds 	}
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	__split_bio(md, bio);
7972ca3310eSAlasdair G Kergon 	up_read(&md->io_lock);
7981da177e4SLinus Torvalds 	return 0;
7991da177e4SLinus Torvalds }
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
8021da177e4SLinus Torvalds 			sector_t *error_sector)
8031da177e4SLinus Torvalds {
8041da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
8051da177e4SLinus Torvalds 	struct dm_table *map = dm_get_table(md);
8061da177e4SLinus Torvalds 	int ret = -ENXIO;
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	if (map) {
809cf222b37SAlasdair G Kergon 		ret = dm_table_flush_all(map);
8101da177e4SLinus Torvalds 		dm_table_put(map);
8111da177e4SLinus Torvalds 	}
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	return ret;
8141da177e4SLinus Torvalds }
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds static void dm_unplug_all(request_queue_t *q)
8171da177e4SLinus Torvalds {
8181da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
8191da177e4SLinus Torvalds 	struct dm_table *map = dm_get_table(md);
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	if (map) {
8221da177e4SLinus Torvalds 		dm_table_unplug_all(map);
8231da177e4SLinus Torvalds 		dm_table_put(map);
8241da177e4SLinus Torvalds 	}
8251da177e4SLinus Torvalds }
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
8281da177e4SLinus Torvalds {
8291da177e4SLinus Torvalds 	int r;
8301da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) congested_data;
8311da177e4SLinus Torvalds 	struct dm_table *map = dm_get_table(md);
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
8341da177e4SLinus Torvalds 		r = bdi_bits;
8351da177e4SLinus Torvalds 	else
8361da177e4SLinus Torvalds 		r = dm_table_any_congested(map, bdi_bits);
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	dm_table_put(map);
8391da177e4SLinus Torvalds 	return r;
8401da177e4SLinus Torvalds }
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds /*-----------------------------------------------------------------
8431da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
8441da177e4SLinus Torvalds  *---------------------------------------------------------------*/
8451da177e4SLinus Torvalds static DEFINE_IDR(_minor_idr);
8461da177e4SLinus Torvalds 
8472b06cfffSAlasdair G Kergon static void free_minor(int minor)
8481da177e4SLinus Torvalds {
849f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
8501da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
851f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
8521da177e4SLinus Torvalds }
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds /*
8551da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
8561da177e4SLinus Torvalds  */
8572b06cfffSAlasdair G Kergon static int specific_minor(struct mapped_device *md, int minor)
8581da177e4SLinus Torvalds {
8591da177e4SLinus Torvalds 	int r, m;
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
8621da177e4SLinus Torvalds 		return -EINVAL;
8631da177e4SLinus Torvalds 
86462f75c2fSJeff Mahoney 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
86562f75c2fSJeff Mahoney 	if (!r)
86662f75c2fSJeff Mahoney 		return -ENOMEM;
86762f75c2fSJeff Mahoney 
868f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	if (idr_find(&_minor_idr, minor)) {
8711da177e4SLinus Torvalds 		r = -EBUSY;
8721da177e4SLinus Torvalds 		goto out;
8731da177e4SLinus Torvalds 	}
8741da177e4SLinus Torvalds 
875ba61fdd1SJeff Mahoney 	r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
87662f75c2fSJeff Mahoney 	if (r)
8771da177e4SLinus Torvalds 		goto out;
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	if (m != minor) {
8801da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
8811da177e4SLinus Torvalds 		r = -EBUSY;
8821da177e4SLinus Torvalds 		goto out;
8831da177e4SLinus Torvalds 	}
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds out:
886f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
8871da177e4SLinus Torvalds 	return r;
8881da177e4SLinus Torvalds }
8891da177e4SLinus Torvalds 
8902b06cfffSAlasdair G Kergon static int next_free_minor(struct mapped_device *md, int *minor)
8911da177e4SLinus Torvalds {
8922b06cfffSAlasdair G Kergon 	int r, m;
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
89562f75c2fSJeff Mahoney 	if (!r)
89662f75c2fSJeff Mahoney 		return -ENOMEM;
89762f75c2fSJeff Mahoney 
898f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
8991da177e4SLinus Torvalds 
900ba61fdd1SJeff Mahoney 	r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
9011da177e4SLinus Torvalds 	if (r) {
9021da177e4SLinus Torvalds 		goto out;
9031da177e4SLinus Torvalds 	}
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	if (m >= (1 << MINORBITS)) {
9061da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
9071da177e4SLinus Torvalds 		r = -ENOSPC;
9081da177e4SLinus Torvalds 		goto out;
9091da177e4SLinus Torvalds 	}
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	*minor = m;
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds out:
914f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
9151da177e4SLinus Torvalds 	return r;
9161da177e4SLinus Torvalds }
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops;
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds /*
9211da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
9221da177e4SLinus Torvalds  */
9232b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
9241da177e4SLinus Torvalds {
9251da177e4SLinus Torvalds 	int r;
9261da177e4SLinus Torvalds 	struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
927ba61fdd1SJeff Mahoney 	void *old_md;
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	if (!md) {
9301da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
9311da177e4SLinus Torvalds 		return NULL;
9321da177e4SLinus Torvalds 	}
9331da177e4SLinus Torvalds 
93410da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
93510da4f79SJeff Mahoney 		goto bad0;
93610da4f79SJeff Mahoney 
9371da177e4SLinus Torvalds 	/* get a minor number for the dev */
9382b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
9392b06cfffSAlasdair G Kergon 		r = next_free_minor(md, &minor);
9402b06cfffSAlasdair G Kergon 	else
9412b06cfffSAlasdair G Kergon 		r = specific_minor(md, minor);
9421da177e4SLinus Torvalds 	if (r < 0)
9431da177e4SLinus Torvalds 		goto bad1;
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	memset(md, 0, sizeof(*md));
9462ca3310eSAlasdair G Kergon 	init_rwsem(&md->io_lock);
9472ca3310eSAlasdair G Kergon 	init_MUTEX(&md->suspend_lock);
9481da177e4SLinus Torvalds 	rwlock_init(&md->map_lock);
9491da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
9505c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
9511da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	md->queue = blk_alloc_queue(GFP_KERNEL);
9541da177e4SLinus Torvalds 	if (!md->queue)
955e3f6ac61SIshai Rabinovitz 		goto bad1_free_minor;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	md->queue->queuedata = md;
9581da177e4SLinus Torvalds 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
9591da177e4SLinus Torvalds 	md->queue->backing_dev_info.congested_data = md;
9601da177e4SLinus Torvalds 	blk_queue_make_request(md->queue, dm_request);
961daef265fSJens Axboe 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
9621da177e4SLinus Torvalds 	md->queue->unplug_fn = dm_unplug_all;
9631da177e4SLinus Torvalds 	md->queue->issue_flush_fn = dm_flush_all;
9641da177e4SLinus Torvalds 
96593d2341cSMatthew Dobson 	md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
9661da177e4SLinus Torvalds 	if (!md->io_pool)
9671da177e4SLinus Torvalds 		goto bad2;
9681da177e4SLinus Torvalds 
96993d2341cSMatthew Dobson 	md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
9701da177e4SLinus Torvalds 	if (!md->tio_pool)
9711da177e4SLinus Torvalds 		goto bad3;
9721da177e4SLinus Torvalds 
9739faf400fSStefan Bader 	md->bs = bioset_create(16, 16, 4);
9749faf400fSStefan Bader 	if (!md->bs)
9759faf400fSStefan Bader 		goto bad_no_bioset;
9769faf400fSStefan Bader 
9771da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
9781da177e4SLinus Torvalds 	if (!md->disk)
9791da177e4SLinus Torvalds 		goto bad4;
9801da177e4SLinus Torvalds 
981f0b04115SJeff Mahoney 	atomic_set(&md->pending, 0);
982f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
983f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
984f0b04115SJeff Mahoney 
9851da177e4SLinus Torvalds 	md->disk->major = _major;
9861da177e4SLinus Torvalds 	md->disk->first_minor = minor;
9871da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
9881da177e4SLinus Torvalds 	md->disk->queue = md->queue;
9891da177e4SLinus Torvalds 	md->disk->private_data = md;
9901da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
9911da177e4SLinus Torvalds 	add_disk(md->disk);
9927e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
9931da177e4SLinus Torvalds 
994ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
995f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
996ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
997f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
998ba61fdd1SJeff Mahoney 
999ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1000ba61fdd1SJeff Mahoney 
10011da177e4SLinus Torvalds 	return md;
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds  bad4:
10049faf400fSStefan Bader 	bioset_free(md->bs);
10059faf400fSStefan Bader  bad_no_bioset:
10061da177e4SLinus Torvalds 	mempool_destroy(md->tio_pool);
10071da177e4SLinus Torvalds  bad3:
10081da177e4SLinus Torvalds 	mempool_destroy(md->io_pool);
10091da177e4SLinus Torvalds  bad2:
10101312f40eSAl Viro 	blk_cleanup_queue(md->queue);
1011e3f6ac61SIshai Rabinovitz  bad1_free_minor:
10121da177e4SLinus Torvalds 	free_minor(minor);
10131da177e4SLinus Torvalds  bad1:
101410da4f79SJeff Mahoney 	module_put(THIS_MODULE);
101510da4f79SJeff Mahoney  bad0:
10161da177e4SLinus Torvalds 	kfree(md);
10171da177e4SLinus Torvalds 	return NULL;
10181da177e4SLinus Torvalds }
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
10211da177e4SLinus Torvalds {
10222b06cfffSAlasdair G Kergon 	int minor = md->disk->first_minor;
102363d94e48SJun'ichi Nomura 
1024d9dde59bSJun'ichi Nomura 	if (md->suspended_bdev) {
1025d9dde59bSJun'ichi Nomura 		thaw_bdev(md->suspended_bdev, NULL);
1026d9dde59bSJun'ichi Nomura 		bdput(md->suspended_bdev);
1027d9dde59bSJun'ichi Nomura 	}
10281da177e4SLinus Torvalds 	mempool_destroy(md->tio_pool);
10291da177e4SLinus Torvalds 	mempool_destroy(md->io_pool);
10309faf400fSStefan Bader 	bioset_free(md->bs);
10311da177e4SLinus Torvalds 	del_gendisk(md->disk);
103263d94e48SJun'ichi Nomura 	free_minor(minor);
1033fba9f90eSJeff Mahoney 
1034fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
1035fba9f90eSJeff Mahoney 	md->disk->private_data = NULL;
1036fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
1037fba9f90eSJeff Mahoney 
10381da177e4SLinus Torvalds 	put_disk(md->disk);
10391312f40eSAl Viro 	blk_cleanup_queue(md->queue);
104010da4f79SJeff Mahoney 	module_put(THIS_MODULE);
10411da177e4SLinus Torvalds 	kfree(md);
10421da177e4SLinus Torvalds }
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds /*
10451da177e4SLinus Torvalds  * Bind a table to the device.
10461da177e4SLinus Torvalds  */
10471da177e4SLinus Torvalds static void event_callback(void *context)
10481da177e4SLinus Torvalds {
10491da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
10521da177e4SLinus Torvalds 	wake_up(&md->eventq);
10531da177e4SLinus Torvalds }
10541da177e4SLinus Torvalds 
10554e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
10561da177e4SLinus Torvalds {
10574e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
10581da177e4SLinus Torvalds 
10591b1dcc1bSJes Sorensen 	mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
1060e39e2e95SAlasdair G Kergon 	i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
10611b1dcc1bSJes Sorensen 	mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
10621da177e4SLinus Torvalds }
10631da177e4SLinus Torvalds 
10641da177e4SLinus Torvalds static int __bind(struct mapped_device *md, struct dm_table *t)
10651da177e4SLinus Torvalds {
10661da177e4SLinus Torvalds 	request_queue_t *q = md->queue;
10671da177e4SLinus Torvalds 	sector_t size;
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 	size = dm_table_get_size(t);
10703ac51e74SDarrick J. Wong 
10713ac51e74SDarrick J. Wong 	/*
10723ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
10733ac51e74SDarrick J. Wong 	 */
10743ac51e74SDarrick J. Wong 	if (size != get_capacity(md->disk))
10753ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
10763ac51e74SDarrick J. Wong 
10774e90188bSAlasdair G Kergon 	__set_size(md, size);
10781da177e4SLinus Torvalds 	if (size == 0)
10791da177e4SLinus Torvalds 		return 0;
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds 	dm_table_get(t);
1082cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
10832ca3310eSAlasdair G Kergon 
10842ca3310eSAlasdair G Kergon 	write_lock(&md->map_lock);
10852ca3310eSAlasdair G Kergon 	md->map = t;
10861da177e4SLinus Torvalds 	dm_table_set_restrictions(t, q);
10872ca3310eSAlasdair G Kergon 	write_unlock(&md->map_lock);
10882ca3310eSAlasdair G Kergon 
10891da177e4SLinus Torvalds 	return 0;
10901da177e4SLinus Torvalds }
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds static void __unbind(struct mapped_device *md)
10931da177e4SLinus Torvalds {
10941da177e4SLinus Torvalds 	struct dm_table *map = md->map;
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 	if (!map)
10971da177e4SLinus Torvalds 		return;
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
11001da177e4SLinus Torvalds 	write_lock(&md->map_lock);
11011da177e4SLinus Torvalds 	md->map = NULL;
11021da177e4SLinus Torvalds 	write_unlock(&md->map_lock);
11031da177e4SLinus Torvalds 	dm_table_put(map);
11041da177e4SLinus Torvalds }
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds /*
11071da177e4SLinus Torvalds  * Constructor for a new device.
11081da177e4SLinus Torvalds  */
11092b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
11101da177e4SLinus Torvalds {
11111da177e4SLinus Torvalds 	struct mapped_device *md;
11121da177e4SLinus Torvalds 
11132b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
11141da177e4SLinus Torvalds 	if (!md)
11151da177e4SLinus Torvalds 		return -ENXIO;
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	*result = md;
11181da177e4SLinus Torvalds 	return 0;
11191da177e4SLinus Torvalds }
11201da177e4SLinus Torvalds 
1121637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev)
11221da177e4SLinus Torvalds {
11231da177e4SLinus Torvalds 	struct mapped_device *md;
11241da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
11271da177e4SLinus Torvalds 		return NULL;
11281da177e4SLinus Torvalds 
1129f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
1132fba9f90eSJeff Mahoney 	if (md && (md == MINOR_ALLOCED ||
1133fba9f90eSJeff Mahoney 		   (dm_disk(md)->first_minor != minor) ||
1134fba9f90eSJeff Mahoney 		   test_bit(DMF_FREEING, &md->flags))) {
1135637842cfSDavid Teigland 		md = NULL;
1136fba9f90eSJeff Mahoney 		goto out;
1137fba9f90eSJeff Mahoney 	}
11381da177e4SLinus Torvalds 
1139fba9f90eSJeff Mahoney out:
1140f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
11411da177e4SLinus Torvalds 
1142637842cfSDavid Teigland 	return md;
1143637842cfSDavid Teigland }
1144637842cfSDavid Teigland 
1145d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev)
1146d229a958SDavid Teigland {
1147d229a958SDavid Teigland 	struct mapped_device *md = dm_find_md(dev);
1148d229a958SDavid Teigland 
1149d229a958SDavid Teigland 	if (md)
1150d229a958SDavid Teigland 		dm_get(md);
1151d229a958SDavid Teigland 
1152d229a958SDavid Teigland 	return md;
1153d229a958SDavid Teigland }
1154d229a958SDavid Teigland 
11559ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
1156637842cfSDavid Teigland {
11579ade92a9SAlasdair G Kergon 	return md->interface_ptr;
11581da177e4SLinus Torvalds }
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
11611da177e4SLinus Torvalds {
11621da177e4SLinus Torvalds 	md->interface_ptr = ptr;
11631da177e4SLinus Torvalds }
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
11661da177e4SLinus Torvalds {
11671da177e4SLinus Torvalds 	atomic_inc(&md->holders);
11681da177e4SLinus Torvalds }
11691da177e4SLinus Torvalds 
117072d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
117172d94861SAlasdair G Kergon {
117272d94861SAlasdair G Kergon 	return md->name;
117372d94861SAlasdair G Kergon }
117472d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
117572d94861SAlasdair G Kergon 
11761da177e4SLinus Torvalds void dm_put(struct mapped_device *md)
11771da177e4SLinus Torvalds {
11781134e5aeSMike Anderson 	struct dm_table *map;
11791da177e4SLinus Torvalds 
1180fba9f90eSJeff Mahoney 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
1181fba9f90eSJeff Mahoney 
1182f32c10b0SJeff Mahoney 	if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
11831134e5aeSMike Anderson 		map = dm_get_table(md);
1184ba61fdd1SJeff Mahoney 		idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
1185fba9f90eSJeff Mahoney 		set_bit(DMF_FREEING, &md->flags);
1186f32c10b0SJeff Mahoney 		spin_unlock(&_minor_lock);
1187cf222b37SAlasdair G Kergon 		if (!dm_suspended(md)) {
11881da177e4SLinus Torvalds 			dm_table_presuspend_targets(map);
11891da177e4SLinus Torvalds 			dm_table_postsuspend_targets(map);
11901da177e4SLinus Torvalds 		}
11911da177e4SLinus Torvalds 		__unbind(md);
11921134e5aeSMike Anderson 		dm_table_put(map);
11931da177e4SLinus Torvalds 		free_dev(md);
11941da177e4SLinus Torvalds 	}
11951da177e4SLinus Torvalds }
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds /*
11981da177e4SLinus Torvalds  * Process the deferred bios
11991da177e4SLinus Torvalds  */
12001da177e4SLinus Torvalds static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
12011da177e4SLinus Torvalds {
12021da177e4SLinus Torvalds 	struct bio *n;
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 	while (c) {
12051da177e4SLinus Torvalds 		n = c->bi_next;
12061da177e4SLinus Torvalds 		c->bi_next = NULL;
12071da177e4SLinus Torvalds 		__split_bio(md, c);
12081da177e4SLinus Torvalds 		c = n;
12091da177e4SLinus Torvalds 	}
12101da177e4SLinus Torvalds }
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds /*
12131da177e4SLinus Torvalds  * Swap in a new table (destroying old one).
12141da177e4SLinus Torvalds  */
12151da177e4SLinus Torvalds int dm_swap_table(struct mapped_device *md, struct dm_table *table)
12161da177e4SLinus Torvalds {
121793c534aeSAlasdair G Kergon 	int r = -EINVAL;
12181da177e4SLinus Torvalds 
12192ca3310eSAlasdair G Kergon 	down(&md->suspend_lock);
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 	/* device must be suspended */
1222cf222b37SAlasdair G Kergon 	if (!dm_suspended(md))
122393c534aeSAlasdair G Kergon 		goto out;
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 	__unbind(md);
12261da177e4SLinus Torvalds 	r = __bind(md, table);
12271da177e4SLinus Torvalds 
122893c534aeSAlasdair G Kergon out:
12292ca3310eSAlasdair G Kergon 	up(&md->suspend_lock);
123093c534aeSAlasdair G Kergon 	return r;
12311da177e4SLinus Torvalds }
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds /*
12341da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
12351da177e4SLinus Torvalds  * device.
12361da177e4SLinus Torvalds  */
12372ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
12381da177e4SLinus Torvalds {
1239e39e2e95SAlasdair G Kergon 	int r;
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
1242dfbe03f6SAlasdair G Kergon 
1243e39e2e95SAlasdair G Kergon 	md->frozen_sb = freeze_bdev(md->suspended_bdev);
1244dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
1245cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
1246e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
1247e39e2e95SAlasdair G Kergon 		return r;
1248dfbe03f6SAlasdair G Kergon 	}
1249dfbe03f6SAlasdair G Kergon 
1250aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
1251aa8d7c2fSAlasdair G Kergon 
12521da177e4SLinus Torvalds 	/* don't bdput right now, we don't want the bdev
1253e39e2e95SAlasdair G Kergon 	 * to go away while it is locked.
12541da177e4SLinus Torvalds 	 */
12551da177e4SLinus Torvalds 	return 0;
12561da177e4SLinus Torvalds }
12571da177e4SLinus Torvalds 
12582ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
12591da177e4SLinus Torvalds {
1260aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
1261aa8d7c2fSAlasdair G Kergon 		return;
1262aa8d7c2fSAlasdair G Kergon 
1263e39e2e95SAlasdair G Kergon 	thaw_bdev(md->suspended_bdev, md->frozen_sb);
12641da177e4SLinus Torvalds 	md->frozen_sb = NULL;
1265aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
12661da177e4SLinus Torvalds }
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds /*
12691da177e4SLinus Torvalds  * We need to be able to change a mapping table under a mounted
12701da177e4SLinus Torvalds  * filesystem.  For example we might want to move some data in
12711da177e4SLinus Torvalds  * the background.  Before the table can be swapped with
12721da177e4SLinus Torvalds  * dm_bind_table, dm_suspend must be called to flush any in
12731da177e4SLinus Torvalds  * flight bios and ensure that any further io gets deferred.
12741da177e4SLinus Torvalds  */
1275*a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
12761da177e4SLinus Torvalds {
12772ca3310eSAlasdair G Kergon 	struct dm_table *map = NULL;
12781da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
12791ecac7fdSJun'ichi Nomura 	struct bio *def;
1280cf222b37SAlasdair G Kergon 	int r = -EINVAL;
1281*a3d77d35SKiyoshi Ueda 	int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
12821da177e4SLinus Torvalds 
12832ca3310eSAlasdair G Kergon 	down(&md->suspend_lock);
12842ca3310eSAlasdair G Kergon 
12852ca3310eSAlasdair G Kergon 	if (dm_suspended(md))
1286d287483dSAlasdair G Kergon 		goto out_unlock;
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 	map = dm_get_table(md);
1289cf222b37SAlasdair G Kergon 
1290436d4108SAlasdair G Kergon 	/* This does not get reverted if there's an error later. */
12911da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
12921da177e4SLinus Torvalds 
1293e39e2e95SAlasdair G Kergon 	md->suspended_bdev = bdget_disk(md->disk, 0);
1294e39e2e95SAlasdair G Kergon 	if (!md->suspended_bdev) {
1295e39e2e95SAlasdair G Kergon 		DMWARN("bdget failed in dm_suspend");
1296e39e2e95SAlasdair G Kergon 		r = -ENOMEM;
1297e39e2e95SAlasdair G Kergon 		goto out;
1298e39e2e95SAlasdair G Kergon 	}
1299e39e2e95SAlasdair G Kergon 
1300cf222b37SAlasdair G Kergon 	/* Flush I/O to the device. */
1301aa8d7c2fSAlasdair G Kergon 	if (do_lockfs) {
13022ca3310eSAlasdair G Kergon 		r = lock_fs(md);
13032ca3310eSAlasdair G Kergon 		if (r)
13042ca3310eSAlasdair G Kergon 			goto out;
1305aa8d7c2fSAlasdair G Kergon 	}
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 	/*
1308354e0071SAlasdair G Kergon 	 * First we set the BLOCK_IO flag so no more ios will be mapped.
13091da177e4SLinus Torvalds 	 */
13102ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
13112ca3310eSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO, &md->flags);
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds 	add_wait_queue(&md->wait, &wait);
13142ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds 	/* unplug */
13172ca3310eSAlasdair G Kergon 	if (map)
13181da177e4SLinus Torvalds 		dm_table_unplug_all(map);
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 	/*
13211da177e4SLinus Torvalds 	 * Then we wait for the already mapped ios to
13221da177e4SLinus Torvalds 	 * complete.
13231da177e4SLinus Torvalds 	 */
13241da177e4SLinus Torvalds 	while (1) {
13251da177e4SLinus Torvalds 		set_current_state(TASK_INTERRUPTIBLE);
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 		if (!atomic_read(&md->pending) || signal_pending(current))
13281da177e4SLinus Torvalds 			break;
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds 		io_schedule();
13311da177e4SLinus Torvalds 	}
13321da177e4SLinus Torvalds 	set_current_state(TASK_RUNNING);
13331da177e4SLinus Torvalds 
13342ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
13351da177e4SLinus Torvalds 	remove_wait_queue(&md->wait, &wait);
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds 	/* were we interrupted ? */
1338cf222b37SAlasdair G Kergon 	r = -EINTR;
13392ca3310eSAlasdair G Kergon 	if (atomic_read(&md->pending)) {
13401ecac7fdSJun'ichi Nomura 		clear_bit(DMF_BLOCK_IO, &md->flags);
13411ecac7fdSJun'ichi Nomura 		def = bio_list_get(&md->deferred);
13421ecac7fdSJun'ichi Nomura 		__flush_deferred_io(md, def);
13432ca3310eSAlasdair G Kergon 		up_write(&md->io_lock);
13442ca3310eSAlasdair G Kergon 		unlock_fs(md);
13452ca3310eSAlasdair G Kergon 		goto out;
13462ca3310eSAlasdair G Kergon 	}
13472ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
13482ca3310eSAlasdair G Kergon 
13492ca3310eSAlasdair G Kergon 	dm_table_postsuspend_targets(map);
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
13521da177e4SLinus Torvalds 
13532ca3310eSAlasdair G Kergon 	r = 0;
13542ca3310eSAlasdair G Kergon 
13552ca3310eSAlasdair G Kergon out:
1356e39e2e95SAlasdair G Kergon 	if (r && md->suspended_bdev) {
1357e39e2e95SAlasdair G Kergon 		bdput(md->suspended_bdev);
1358e39e2e95SAlasdair G Kergon 		md->suspended_bdev = NULL;
1359e39e2e95SAlasdair G Kergon 	}
1360e39e2e95SAlasdair G Kergon 
13611da177e4SLinus Torvalds 	dm_table_put(map);
1362d287483dSAlasdair G Kergon 
1363d287483dSAlasdair G Kergon out_unlock:
13642ca3310eSAlasdair G Kergon 	up(&md->suspend_lock);
1365cf222b37SAlasdair G Kergon 	return r;
13661da177e4SLinus Torvalds }
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds int dm_resume(struct mapped_device *md)
13691da177e4SLinus Torvalds {
1370cf222b37SAlasdair G Kergon 	int r = -EINVAL;
13711da177e4SLinus Torvalds 	struct bio *def;
1372cf222b37SAlasdair G Kergon 	struct dm_table *map = NULL;
13731da177e4SLinus Torvalds 
13742ca3310eSAlasdair G Kergon 	down(&md->suspend_lock);
13752ca3310eSAlasdair G Kergon 	if (!dm_suspended(md))
1376cf222b37SAlasdair G Kergon 		goto out;
1377cf222b37SAlasdair G Kergon 
1378cf222b37SAlasdair G Kergon 	map = dm_get_table(md);
13792ca3310eSAlasdair G Kergon 	if (!map || !dm_table_get_size(map))
1380cf222b37SAlasdair G Kergon 		goto out;
13811da177e4SLinus Torvalds 
13828757b776SMilan Broz 	r = dm_table_resume_targets(map);
13838757b776SMilan Broz 	if (r)
13848757b776SMilan Broz 		goto out;
13852ca3310eSAlasdair G Kergon 
13862ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
13871da177e4SLinus Torvalds 	clear_bit(DMF_BLOCK_IO, &md->flags);
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds 	def = bio_list_get(&md->deferred);
13901da177e4SLinus Torvalds 	__flush_deferred_io(md, def);
13912ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
13922ca3310eSAlasdair G Kergon 
13932ca3310eSAlasdair G Kergon 	unlock_fs(md);
13942ca3310eSAlasdair G Kergon 
1395e39e2e95SAlasdair G Kergon 	bdput(md->suspended_bdev);
1396e39e2e95SAlasdair G Kergon 	md->suspended_bdev = NULL;
1397e39e2e95SAlasdair G Kergon 
13982ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
13992ca3310eSAlasdair G Kergon 
14001da177e4SLinus Torvalds 	dm_table_unplug_all(map);
14011da177e4SLinus Torvalds 
14028560ed6fSHannes Reinecke 	kobject_uevent(&md->disk->kobj, KOBJ_CHANGE);
14038560ed6fSHannes Reinecke 
1404cf222b37SAlasdair G Kergon 	r = 0;
14052ca3310eSAlasdair G Kergon 
1406cf222b37SAlasdair G Kergon out:
1407cf222b37SAlasdair G Kergon 	dm_table_put(map);
14082ca3310eSAlasdair G Kergon 	up(&md->suspend_lock);
14092ca3310eSAlasdair G Kergon 
1410cf222b37SAlasdair G Kergon 	return r;
14111da177e4SLinus Torvalds }
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds /*-----------------------------------------------------------------
14141da177e4SLinus Torvalds  * Event notification.
14151da177e4SLinus Torvalds  *---------------------------------------------------------------*/
14161da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
14171da177e4SLinus Torvalds {
14181da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
14191da177e4SLinus Torvalds }
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
14221da177e4SLinus Torvalds {
14231da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
14241da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
14251da177e4SLinus Torvalds }
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds /*
14281da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
14291da177e4SLinus Torvalds  * count on 'md'.
14301da177e4SLinus Torvalds  */
14311da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
14321da177e4SLinus Torvalds {
14331da177e4SLinus Torvalds 	return md->disk;
14341da177e4SLinus Torvalds }
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds int dm_suspended(struct mapped_device *md)
14371da177e4SLinus Torvalds {
14381da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
14391da177e4SLinus Torvalds }
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops = {
14421da177e4SLinus Torvalds 	.open = dm_blk_open,
14431da177e4SLinus Torvalds 	.release = dm_blk_close,
1444aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
14453ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
14461da177e4SLinus Torvalds 	.owner = THIS_MODULE
14471da177e4SLinus Torvalds };
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo);
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds /*
14521da177e4SLinus Torvalds  * module hooks
14531da177e4SLinus Torvalds  */
14541da177e4SLinus Torvalds module_init(dm_init);
14551da177e4SLinus Torvalds module_exit(dm_exit);
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds module_param(major, uint, 0);
14581da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
14591da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
14601da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
14611da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1462