xref: /openbmc/linux/drivers/md/dm.c (revision 7a8c3d3b92883798e4ead21dd48c16db0ec0ff6f)
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"
1051e5b2bdSMike Anderson #include "dm-uevent.h"
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/init.h>
131da177e4SLinus Torvalds #include <linux/module.h>
1448c9c27bSArjan van de Ven #include <linux/mutex.h>
151da177e4SLinus Torvalds #include <linux/moduleparam.h>
161da177e4SLinus Torvalds #include <linux/blkpg.h>
171da177e4SLinus Torvalds #include <linux/bio.h>
181da177e4SLinus Torvalds #include <linux/buffer_head.h>
191da177e4SLinus Torvalds #include <linux/mempool.h>
201da177e4SLinus Torvalds #include <linux/slab.h>
211da177e4SLinus Torvalds #include <linux/idr.h>
223ac51e74SDarrick J. Wong #include <linux/hdreg.h>
232056a782SJens Axboe #include <linux/blktrace_api.h>
24aa129a22SMilan Broz #include <linux/smp_lock.h>
251da177e4SLinus Torvalds 
2672d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
2772d94861SAlasdair G Kergon 
281da177e4SLinus Torvalds static const char *_name = DM_NAME;
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds static unsigned int major = 0;
311da177e4SLinus Torvalds static unsigned int _major = 0;
321da177e4SLinus Torvalds 
33f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
341da177e4SLinus Torvalds /*
351da177e4SLinus Torvalds  * One of these is allocated per bio.
361da177e4SLinus Torvalds  */
371da177e4SLinus Torvalds struct dm_io {
381da177e4SLinus Torvalds 	struct mapped_device *md;
391da177e4SLinus Torvalds 	int error;
401da177e4SLinus Torvalds 	struct bio *bio;
411da177e4SLinus Torvalds 	atomic_t io_count;
423eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
431da177e4SLinus Torvalds };
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds /*
461da177e4SLinus Torvalds  * One of these is allocated per target within a bio.  Hopefully
471da177e4SLinus Torvalds  * this will be simplified out one day.
481da177e4SLinus Torvalds  */
49028867acSAlasdair G Kergon struct dm_target_io {
501da177e4SLinus Torvalds 	struct dm_io *io;
511da177e4SLinus Torvalds 	struct dm_target *ti;
521da177e4SLinus Torvalds 	union map_info info;
531da177e4SLinus Torvalds };
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds union map_info *dm_get_mapinfo(struct bio *bio)
561da177e4SLinus Torvalds {
571da177e4SLinus Torvalds 	if (bio && bio->bi_private)
58028867acSAlasdair G Kergon 		return &((struct dm_target_io *)bio->bi_private)->info;
591da177e4SLinus Torvalds 	return NULL;
601da177e4SLinus Torvalds }
611da177e4SLinus Torvalds 
62ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
63ba61fdd1SJeff Mahoney 
641da177e4SLinus Torvalds /*
651da177e4SLinus Torvalds  * Bits for the md->flags field.
661da177e4SLinus Torvalds  */
671da177e4SLinus Torvalds #define DMF_BLOCK_IO 0
681da177e4SLinus Torvalds #define DMF_SUSPENDED 1
69aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
70fba9f90eSJeff Mahoney #define DMF_FREEING 3
715c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
722e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds struct mapped_device {
752ca3310eSAlasdair G Kergon 	struct rw_semaphore io_lock;
762ca3310eSAlasdair G Kergon 	struct semaphore suspend_lock;
772e93ccc1SKiyoshi Ueda 	spinlock_t pushback_lock;
781da177e4SLinus Torvalds 	rwlock_t map_lock;
791da177e4SLinus Torvalds 	atomic_t holders;
805c6bd75dSAlasdair G Kergon 	atomic_t open_count;
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	unsigned long flags;
831da177e4SLinus Torvalds 
84165125e1SJens Axboe 	struct request_queue *queue;
851da177e4SLinus Torvalds 	struct gendisk *disk;
867e51f257SMike Anderson 	char name[16];
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	void *interface_ptr;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	/*
911da177e4SLinus Torvalds 	 * A list of ios that arrived while we were suspended.
921da177e4SLinus Torvalds 	 */
931da177e4SLinus Torvalds 	atomic_t pending;
941da177e4SLinus Torvalds 	wait_queue_head_t wait;
951da177e4SLinus Torvalds 	struct bio_list deferred;
962e93ccc1SKiyoshi Ueda 	struct bio_list pushback;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	/*
991da177e4SLinus Torvalds 	 * The current mapping.
1001da177e4SLinus Torvalds 	 */
1011da177e4SLinus Torvalds 	struct dm_table *map;
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	/*
1041da177e4SLinus Torvalds 	 * io objects are allocated from here.
1051da177e4SLinus Torvalds 	 */
1061da177e4SLinus Torvalds 	mempool_t *io_pool;
1071da177e4SLinus Torvalds 	mempool_t *tio_pool;
1081da177e4SLinus Torvalds 
1099faf400fSStefan Bader 	struct bio_set *bs;
1109faf400fSStefan Bader 
1111da177e4SLinus Torvalds 	/*
1121da177e4SLinus Torvalds 	 * Event handling.
1131da177e4SLinus Torvalds 	 */
1141da177e4SLinus Torvalds 	atomic_t event_nr;
1151da177e4SLinus Torvalds 	wait_queue_head_t eventq;
116*7a8c3d3bSMike Anderson 	atomic_t uevent_seq;
117*7a8c3d3bSMike Anderson 	struct list_head uevent_list;
118*7a8c3d3bSMike Anderson 	spinlock_t uevent_lock; /* Protect access to uevent_list */
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	/*
1211da177e4SLinus Torvalds 	 * freeze/thaw support require holding onto a super block
1221da177e4SLinus Torvalds 	 */
1231da177e4SLinus Torvalds 	struct super_block *frozen_sb;
124e39e2e95SAlasdair G Kergon 	struct block_device *suspended_bdev;
1253ac51e74SDarrick J. Wong 
1263ac51e74SDarrick J. Wong 	/* forced geometry settings */
1273ac51e74SDarrick J. Wong 	struct hd_geometry geometry;
1281da177e4SLinus Torvalds };
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds #define MIN_IOS 256
131e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
132e18b890bSChristoph Lameter static struct kmem_cache *_tio_cache;
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds static int __init local_init(void)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	int r;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
139028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
1401da177e4SLinus Torvalds 	if (!_io_cache)
1411da177e4SLinus Torvalds 		return -ENOMEM;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	/* allocate a slab for the target ios */
144028867acSAlasdair G Kergon 	_tio_cache = KMEM_CACHE(dm_target_io, 0);
1451da177e4SLinus Torvalds 	if (!_tio_cache) {
1461da177e4SLinus Torvalds 		kmem_cache_destroy(_io_cache);
1471da177e4SLinus Torvalds 		return -ENOMEM;
1481da177e4SLinus Torvalds 	}
1491da177e4SLinus Torvalds 
15051e5b2bdSMike Anderson 	r = dm_uevent_init();
15151e5b2bdSMike Anderson 	if (r) {
15251e5b2bdSMike Anderson 		kmem_cache_destroy(_tio_cache);
15351e5b2bdSMike Anderson 		kmem_cache_destroy(_io_cache);
15451e5b2bdSMike Anderson 		return r;
15551e5b2bdSMike Anderson 	}
15651e5b2bdSMike Anderson 
1571da177e4SLinus Torvalds 	_major = major;
1581da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
1591da177e4SLinus Torvalds 	if (r < 0) {
1601da177e4SLinus Torvalds 		kmem_cache_destroy(_tio_cache);
1611da177e4SLinus Torvalds 		kmem_cache_destroy(_io_cache);
16251e5b2bdSMike Anderson 		dm_uevent_exit();
1631da177e4SLinus Torvalds 		return r;
1641da177e4SLinus Torvalds 	}
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	if (!_major)
1671da177e4SLinus Torvalds 		_major = r;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	return 0;
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds static void local_exit(void)
1731da177e4SLinus Torvalds {
1741da177e4SLinus Torvalds 	kmem_cache_destroy(_tio_cache);
1751da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
17600d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
17751e5b2bdSMike Anderson 	dm_uevent_exit();
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	_major = 0;
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	DMINFO("cleaned up");
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds int (*_inits[])(void) __initdata = {
1851da177e4SLinus Torvalds 	local_init,
1861da177e4SLinus Torvalds 	dm_target_init,
1871da177e4SLinus Torvalds 	dm_linear_init,
1881da177e4SLinus Torvalds 	dm_stripe_init,
1891da177e4SLinus Torvalds 	dm_interface_init,
1901da177e4SLinus Torvalds };
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds void (*_exits[])(void) = {
1931da177e4SLinus Torvalds 	local_exit,
1941da177e4SLinus Torvalds 	dm_target_exit,
1951da177e4SLinus Torvalds 	dm_linear_exit,
1961da177e4SLinus Torvalds 	dm_stripe_exit,
1971da177e4SLinus Torvalds 	dm_interface_exit,
1981da177e4SLinus Torvalds };
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds static int __init dm_init(void)
2011da177e4SLinus Torvalds {
2021da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	int r, i;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
2071da177e4SLinus Torvalds 		r = _inits[i]();
2081da177e4SLinus Torvalds 		if (r)
2091da177e4SLinus Torvalds 			goto bad;
2101da177e4SLinus Torvalds 	}
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	return 0;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds       bad:
2151da177e4SLinus Torvalds 	while (i--)
2161da177e4SLinus Torvalds 		_exits[i]();
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	return r;
2191da177e4SLinus Torvalds }
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds static void __exit dm_exit(void)
2221da177e4SLinus Torvalds {
2231da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	while (i--)
2261da177e4SLinus Torvalds 		_exits[i]();
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds /*
2301da177e4SLinus Torvalds  * Block device functions
2311da177e4SLinus Torvalds  */
2321da177e4SLinus Torvalds static int dm_blk_open(struct inode *inode, struct file *file)
2331da177e4SLinus Torvalds {
2341da177e4SLinus Torvalds 	struct mapped_device *md;
2351da177e4SLinus Torvalds 
236fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
237fba9f90eSJeff Mahoney 
2381da177e4SLinus Torvalds 	md = inode->i_bdev->bd_disk->private_data;
239fba9f90eSJeff Mahoney 	if (!md)
240fba9f90eSJeff Mahoney 		goto out;
241fba9f90eSJeff Mahoney 
2425c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
2435c6bd75dSAlasdair G Kergon 	    test_bit(DMF_DELETING, &md->flags)) {
244fba9f90eSJeff Mahoney 		md = NULL;
245fba9f90eSJeff Mahoney 		goto out;
246fba9f90eSJeff Mahoney 	}
247fba9f90eSJeff Mahoney 
2481da177e4SLinus Torvalds 	dm_get(md);
2495c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
250fba9f90eSJeff Mahoney 
251fba9f90eSJeff Mahoney out:
252fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
253fba9f90eSJeff Mahoney 
254fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds static int dm_blk_close(struct inode *inode, struct file *file)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	struct mapped_device *md;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	md = inode->i_bdev->bd_disk->private_data;
2625c6bd75dSAlasdair G Kergon 	atomic_dec(&md->open_count);
2631da177e4SLinus Torvalds 	dm_put(md);
2641da177e4SLinus Torvalds 	return 0;
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds 
2675c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
2685c6bd75dSAlasdair G Kergon {
2695c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
2705c6bd75dSAlasdair G Kergon }
2715c6bd75dSAlasdair G Kergon 
2725c6bd75dSAlasdair G Kergon /*
2735c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
2745c6bd75dSAlasdair G Kergon  */
2755c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md)
2765c6bd75dSAlasdair G Kergon {
2775c6bd75dSAlasdair G Kergon 	int r = 0;
2785c6bd75dSAlasdair G Kergon 
2795c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
2805c6bd75dSAlasdair G Kergon 
2815c6bd75dSAlasdair G Kergon 	if (dm_open_count(md))
2825c6bd75dSAlasdair G Kergon 		r = -EBUSY;
2835c6bd75dSAlasdair G Kergon 	else
2845c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
2855c6bd75dSAlasdair G Kergon 
2865c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
2875c6bd75dSAlasdair G Kergon 
2885c6bd75dSAlasdair G Kergon 	return r;
2895c6bd75dSAlasdair G Kergon }
2905c6bd75dSAlasdair G Kergon 
2913ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
2923ac51e74SDarrick J. Wong {
2933ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
2943ac51e74SDarrick J. Wong 
2953ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
2963ac51e74SDarrick J. Wong }
2973ac51e74SDarrick J. Wong 
298aa129a22SMilan Broz static int dm_blk_ioctl(struct inode *inode, struct file *file,
299aa129a22SMilan Broz 			unsigned int cmd, unsigned long arg)
300aa129a22SMilan Broz {
301aa129a22SMilan Broz 	struct mapped_device *md;
302aa129a22SMilan Broz 	struct dm_table *map;
303aa129a22SMilan Broz 	struct dm_target *tgt;
304aa129a22SMilan Broz 	int r = -ENOTTY;
305aa129a22SMilan Broz 
306aa129a22SMilan Broz 	/* We don't really need this lock, but we do need 'inode'. */
307aa129a22SMilan Broz 	unlock_kernel();
308aa129a22SMilan Broz 
309aa129a22SMilan Broz 	md = inode->i_bdev->bd_disk->private_data;
310aa129a22SMilan Broz 
311aa129a22SMilan Broz 	map = dm_get_table(md);
312aa129a22SMilan Broz 
313aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
314aa129a22SMilan Broz 		goto out;
315aa129a22SMilan Broz 
316aa129a22SMilan Broz 	/* We only support devices that have a single target */
317aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
318aa129a22SMilan Broz 		goto out;
319aa129a22SMilan Broz 
320aa129a22SMilan Broz 	tgt = dm_table_get_target(map, 0);
321aa129a22SMilan Broz 
322aa129a22SMilan Broz 	if (dm_suspended(md)) {
323aa129a22SMilan Broz 		r = -EAGAIN;
324aa129a22SMilan Broz 		goto out;
325aa129a22SMilan Broz 	}
326aa129a22SMilan Broz 
327aa129a22SMilan Broz 	if (tgt->type->ioctl)
328aa129a22SMilan Broz 		r = tgt->type->ioctl(tgt, inode, file, cmd, arg);
329aa129a22SMilan Broz 
330aa129a22SMilan Broz out:
331aa129a22SMilan Broz 	dm_table_put(map);
332aa129a22SMilan Broz 
333aa129a22SMilan Broz 	lock_kernel();
334aa129a22SMilan Broz 	return r;
335aa129a22SMilan Broz }
336aa129a22SMilan Broz 
337028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
3381da177e4SLinus Torvalds {
3391da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
342028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
347028867acSAlasdair G Kergon static struct dm_target_io *alloc_tio(struct mapped_device *md)
3481da177e4SLinus Torvalds {
3491da177e4SLinus Torvalds 	return mempool_alloc(md->tio_pool, GFP_NOIO);
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
352028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
3531da177e4SLinus Torvalds {
3541da177e4SLinus Torvalds 	mempool_free(tio, md->tio_pool);
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
3573eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
3583eaf840eSJun'ichi "Nick" Nomura {
3593eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
3603eaf840eSJun'ichi "Nick" Nomura 
3613eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
3623eaf840eSJun'ichi "Nick" Nomura 
3633eaf840eSJun'ichi "Nick" Nomura 	preempt_disable();
3643eaf840eSJun'ichi "Nick" Nomura 	disk_round_stats(dm_disk(md));
3653eaf840eSJun'ichi "Nick" Nomura 	preempt_enable();
3663eaf840eSJun'ichi "Nick" Nomura 	dm_disk(md)->in_flight = atomic_inc_return(&md->pending);
3673eaf840eSJun'ichi "Nick" Nomura }
3683eaf840eSJun'ichi "Nick" Nomura 
3693eaf840eSJun'ichi "Nick" Nomura static int end_io_acct(struct dm_io *io)
3703eaf840eSJun'ichi "Nick" Nomura {
3713eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
3723eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
3733eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
3743eaf840eSJun'ichi "Nick" Nomura 	int pending;
3753eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
3763eaf840eSJun'ichi "Nick" Nomura 
3773eaf840eSJun'ichi "Nick" Nomura 	preempt_disable();
3783eaf840eSJun'ichi "Nick" Nomura 	disk_round_stats(dm_disk(md));
3793eaf840eSJun'ichi "Nick" Nomura 	preempt_enable();
3803eaf840eSJun'ichi "Nick" Nomura 	dm_disk(md)->in_flight = pending = atomic_dec_return(&md->pending);
3813eaf840eSJun'ichi "Nick" Nomura 
3823eaf840eSJun'ichi "Nick" Nomura 	disk_stat_add(dm_disk(md), ticks[rw], duration);
3833eaf840eSJun'ichi "Nick" Nomura 
3843eaf840eSJun'ichi "Nick" Nomura 	return !pending;
3853eaf840eSJun'ichi "Nick" Nomura }
3863eaf840eSJun'ichi "Nick" Nomura 
3871da177e4SLinus Torvalds /*
3881da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
3891da177e4SLinus Torvalds  */
3901da177e4SLinus Torvalds static int queue_io(struct mapped_device *md, struct bio *bio)
3911da177e4SLinus Torvalds {
3922ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
3952ca3310eSAlasdair G Kergon 		up_write(&md->io_lock);
3961da177e4SLinus Torvalds 		return 1;
3971da177e4SLinus Torvalds 	}
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
4001da177e4SLinus Torvalds 
4012ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
4021da177e4SLinus Torvalds 	return 0;		/* deferred successfully */
4031da177e4SLinus Torvalds }
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds /*
4061da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
4071da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
4081da177e4SLinus Torvalds  * dm_table_put() when finished.
4091da177e4SLinus Torvalds  */
4101da177e4SLinus Torvalds struct dm_table *dm_get_table(struct mapped_device *md)
4111da177e4SLinus Torvalds {
4121da177e4SLinus Torvalds 	struct dm_table *t;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	read_lock(&md->map_lock);
4151da177e4SLinus Torvalds 	t = md->map;
4161da177e4SLinus Torvalds 	if (t)
4171da177e4SLinus Torvalds 		dm_table_get(t);
4181da177e4SLinus Torvalds 	read_unlock(&md->map_lock);
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	return t;
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
4233ac51e74SDarrick J. Wong /*
4243ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
4253ac51e74SDarrick J. Wong  */
4263ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
4273ac51e74SDarrick J. Wong {
4283ac51e74SDarrick J. Wong 	*geo = md->geometry;
4293ac51e74SDarrick J. Wong 
4303ac51e74SDarrick J. Wong 	return 0;
4313ac51e74SDarrick J. Wong }
4323ac51e74SDarrick J. Wong 
4333ac51e74SDarrick J. Wong /*
4343ac51e74SDarrick J. Wong  * Set the geometry of a device.
4353ac51e74SDarrick J. Wong  */
4363ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
4373ac51e74SDarrick J. Wong {
4383ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
4393ac51e74SDarrick J. Wong 
4403ac51e74SDarrick J. Wong 	if (geo->start > sz) {
4413ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
4423ac51e74SDarrick J. Wong 		return -EINVAL;
4433ac51e74SDarrick J. Wong 	}
4443ac51e74SDarrick J. Wong 
4453ac51e74SDarrick J. Wong 	md->geometry = *geo;
4463ac51e74SDarrick J. Wong 
4473ac51e74SDarrick J. Wong 	return 0;
4483ac51e74SDarrick J. Wong }
4493ac51e74SDarrick J. Wong 
4501da177e4SLinus Torvalds /*-----------------------------------------------------------------
4511da177e4SLinus Torvalds  * CRUD START:
4521da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
4531da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
4541da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
4551da177e4SLinus Torvalds  *   interests of getting something for people to use I give
4561da177e4SLinus Torvalds  *   you this clearly demarcated crap.
4571da177e4SLinus Torvalds  *---------------------------------------------------------------*/
4581da177e4SLinus Torvalds 
4592e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
4602e93ccc1SKiyoshi Ueda {
4612e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
4622e93ccc1SKiyoshi Ueda }
4632e93ccc1SKiyoshi Ueda 
4641da177e4SLinus Torvalds /*
4651da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
4661da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
4671da177e4SLinus Torvalds  */
468858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
4691da177e4SLinus Torvalds {
4702e93ccc1SKiyoshi Ueda 	unsigned long flags;
4712e93ccc1SKiyoshi Ueda 
4722e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
4732e93ccc1SKiyoshi Ueda 	if (error && !(io->error > 0 && __noflush_suspending(io->md)))
4741da177e4SLinus Torvalds 		io->error = error;
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
4772e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
4782e93ccc1SKiyoshi Ueda 			/*
4792e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
4802e93ccc1SKiyoshi Ueda 			 * This must be handled before the sleeper on
4812e93ccc1SKiyoshi Ueda 			 * suspend queue merges the pushback list.
4822e93ccc1SKiyoshi Ueda 			 */
4832e93ccc1SKiyoshi Ueda 			spin_lock_irqsave(&io->md->pushback_lock, flags);
4842e93ccc1SKiyoshi Ueda 			if (__noflush_suspending(io->md))
4852e93ccc1SKiyoshi Ueda 				bio_list_add(&io->md->pushback, io->bio);
4862e93ccc1SKiyoshi Ueda 			else
4872e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
4882e93ccc1SKiyoshi Ueda 				io->error = -EIO;
4892e93ccc1SKiyoshi Ueda 			spin_unlock_irqrestore(&io->md->pushback_lock, flags);
4902e93ccc1SKiyoshi Ueda 		}
4912e93ccc1SKiyoshi Ueda 
4923eaf840eSJun'ichi "Nick" Nomura 		if (end_io_acct(io))
4931da177e4SLinus Torvalds 			/* nudge anyone waiting on suspend queue */
4941da177e4SLinus Torvalds 			wake_up(&io->md->wait);
4951da177e4SLinus Torvalds 
4962e93ccc1SKiyoshi Ueda 		if (io->error != DM_ENDIO_REQUEUE) {
4972e93ccc1SKiyoshi Ueda 			blk_add_trace_bio(io->md->queue, io->bio,
4982e93ccc1SKiyoshi Ueda 					  BLK_TA_COMPLETE);
4992056a782SJens Axboe 
5006712ecf8SNeilBrown 			bio_endio(io->bio, io->error);
5012e93ccc1SKiyoshi Ueda 		}
5022e93ccc1SKiyoshi Ueda 
5031da177e4SLinus Torvalds 		free_io(io->md, io);
5041da177e4SLinus Torvalds 	}
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds 
5076712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error)
5081da177e4SLinus Torvalds {
5091da177e4SLinus Torvalds 	int r = 0;
510028867acSAlasdair G Kergon 	struct dm_target_io *tio = bio->bi_private;
5119faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
5121da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	if (!bio_flagged(bio, BIO_UPTODATE) && !error)
5151da177e4SLinus Torvalds 		error = -EIO;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	if (endio) {
5181da177e4SLinus Torvalds 		r = endio(tio->ti, bio, error, &tio->info);
5192e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
5202e93ccc1SKiyoshi Ueda 			/*
5212e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
5222e93ccc1SKiyoshi Ueda 			 * in dec_pending().
5232e93ccc1SKiyoshi Ueda 			 */
5241da177e4SLinus Torvalds 			error = r;
52545cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
52645cbcd79SKiyoshi Ueda 			/* The target will handle the io */
5276712ecf8SNeilBrown 			return;
52845cbcd79SKiyoshi Ueda 		else if (r) {
52945cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
53045cbcd79SKiyoshi Ueda 			BUG();
53145cbcd79SKiyoshi Ueda 		}
5321da177e4SLinus Torvalds 	}
5331da177e4SLinus Torvalds 
5349faf400fSStefan Bader 	dec_pending(tio->io, error);
5359faf400fSStefan Bader 
5369faf400fSStefan Bader 	/*
5379faf400fSStefan Bader 	 * Store md for cleanup instead of tio which is about to get freed.
5389faf400fSStefan Bader 	 */
5399faf400fSStefan Bader 	bio->bi_private = md->bs;
5409faf400fSStefan Bader 
5411da177e4SLinus Torvalds 	bio_put(bio);
5429faf400fSStefan Bader 	free_tio(md, tio);
5431da177e4SLinus Torvalds }
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds static sector_t max_io_len(struct mapped_device *md,
5461da177e4SLinus Torvalds 			   sector_t sector, struct dm_target *ti)
5471da177e4SLinus Torvalds {
5481da177e4SLinus Torvalds 	sector_t offset = sector - ti->begin;
5491da177e4SLinus Torvalds 	sector_t len = ti->len - offset;
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	/*
5521da177e4SLinus Torvalds 	 * Does the target need to split even further ?
5531da177e4SLinus Torvalds 	 */
5541da177e4SLinus Torvalds 	if (ti->split_io) {
5551da177e4SLinus Torvalds 		sector_t boundary;
5561da177e4SLinus Torvalds 		boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
5571da177e4SLinus Torvalds 			   - offset;
5581da177e4SLinus Torvalds 		if (len > boundary)
5591da177e4SLinus Torvalds 			len = boundary;
5601da177e4SLinus Torvalds 	}
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	return len;
5631da177e4SLinus Torvalds }
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds static void __map_bio(struct dm_target *ti, struct bio *clone,
566028867acSAlasdair G Kergon 		      struct dm_target_io *tio)
5671da177e4SLinus Torvalds {
5681da177e4SLinus Torvalds 	int r;
5692056a782SJens Axboe 	sector_t sector;
5709faf400fSStefan Bader 	struct mapped_device *md;
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	/*
5731da177e4SLinus Torvalds 	 * Sanity checks.
5741da177e4SLinus Torvalds 	 */
5751da177e4SLinus Torvalds 	BUG_ON(!clone->bi_size);
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
5781da177e4SLinus Torvalds 	clone->bi_private = tio;
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	/*
5811da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
5821da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
5831da177e4SLinus Torvalds 	 * this io.
5841da177e4SLinus Torvalds 	 */
5851da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
5862056a782SJens Axboe 	sector = clone->bi_sector;
5871da177e4SLinus Torvalds 	r = ti->type->map(ti, clone, &tio->info);
58845cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
5891da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
5902056a782SJens Axboe 
5912056a782SJens Axboe 		blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
592c7149d6bSAlan D. Brunelle 				    tio->io->bio->bi_bdev->bd_dev,
593c7149d6bSAlan D. Brunelle 				    clone->bi_sector, sector);
5942056a782SJens Axboe 
5951da177e4SLinus Torvalds 		generic_make_request(clone);
5962e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
5972e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
5989faf400fSStefan Bader 		md = tio->io->md;
5999faf400fSStefan Bader 		dec_pending(tio->io, r);
6009faf400fSStefan Bader 		/*
6019faf400fSStefan Bader 		 * Store bio_set for cleanup.
6029faf400fSStefan Bader 		 */
6039faf400fSStefan Bader 		clone->bi_private = md->bs;
6041da177e4SLinus Torvalds 		bio_put(clone);
6059faf400fSStefan Bader 		free_tio(md, tio);
60645cbcd79SKiyoshi Ueda 	} else if (r) {
60745cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
60845cbcd79SKiyoshi Ueda 		BUG();
6091da177e4SLinus Torvalds 	}
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds struct clone_info {
6131da177e4SLinus Torvalds 	struct mapped_device *md;
6141da177e4SLinus Torvalds 	struct dm_table *map;
6151da177e4SLinus Torvalds 	struct bio *bio;
6161da177e4SLinus Torvalds 	struct dm_io *io;
6171da177e4SLinus Torvalds 	sector_t sector;
6181da177e4SLinus Torvalds 	sector_t sector_count;
6191da177e4SLinus Torvalds 	unsigned short idx;
6201da177e4SLinus Torvalds };
6211da177e4SLinus Torvalds 
6223676347aSPeter Osterlund static void dm_bio_destructor(struct bio *bio)
6233676347aSPeter Osterlund {
6249faf400fSStefan Bader 	struct bio_set *bs = bio->bi_private;
6259faf400fSStefan Bader 
6269faf400fSStefan Bader 	bio_free(bio, bs);
6273676347aSPeter Osterlund }
6283676347aSPeter Osterlund 
6291da177e4SLinus Torvalds /*
6301da177e4SLinus Torvalds  * Creates a little bio that is just does part of a bvec.
6311da177e4SLinus Torvalds  */
6321da177e4SLinus Torvalds static struct bio *split_bvec(struct bio *bio, sector_t sector,
6331da177e4SLinus Torvalds 			      unsigned short idx, unsigned int offset,
6349faf400fSStefan Bader 			      unsigned int len, struct bio_set *bs)
6351da177e4SLinus Torvalds {
6361da177e4SLinus Torvalds 	struct bio *clone;
6371da177e4SLinus Torvalds 	struct bio_vec *bv = bio->bi_io_vec + idx;
6381da177e4SLinus Torvalds 
6399faf400fSStefan Bader 	clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
6403676347aSPeter Osterlund 	clone->bi_destructor = dm_bio_destructor;
6411da177e4SLinus Torvalds 	*clone->bi_io_vec = *bv;
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	clone->bi_sector = sector;
6441da177e4SLinus Torvalds 	clone->bi_bdev = bio->bi_bdev;
6451da177e4SLinus Torvalds 	clone->bi_rw = bio->bi_rw;
6461da177e4SLinus Torvalds 	clone->bi_vcnt = 1;
6471da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
6481da177e4SLinus Torvalds 	clone->bi_io_vec->bv_offset = offset;
6491da177e4SLinus Torvalds 	clone->bi_io_vec->bv_len = clone->bi_size;
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	return clone;
6521da177e4SLinus Torvalds }
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds /*
6551da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
6561da177e4SLinus Torvalds  */
6571da177e4SLinus Torvalds static struct bio *clone_bio(struct bio *bio, sector_t sector,
6581da177e4SLinus Torvalds 			     unsigned short idx, unsigned short bv_count,
6599faf400fSStefan Bader 			     unsigned int len, struct bio_set *bs)
6601da177e4SLinus Torvalds {
6611da177e4SLinus Torvalds 	struct bio *clone;
6621da177e4SLinus Torvalds 
6639faf400fSStefan Bader 	clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
6649faf400fSStefan Bader 	__bio_clone(clone, bio);
6659faf400fSStefan Bader 	clone->bi_destructor = dm_bio_destructor;
6661da177e4SLinus Torvalds 	clone->bi_sector = sector;
6671da177e4SLinus Torvalds 	clone->bi_idx = idx;
6681da177e4SLinus Torvalds 	clone->bi_vcnt = idx + bv_count;
6691da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
6701da177e4SLinus Torvalds 	clone->bi_flags &= ~(1 << BIO_SEG_VALID);
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	return clone;
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds static void __clone_and_map(struct clone_info *ci)
6761da177e4SLinus Torvalds {
6771da177e4SLinus Torvalds 	struct bio *clone, *bio = ci->bio;
6781da177e4SLinus Torvalds 	struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
6791da177e4SLinus Torvalds 	sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
680028867acSAlasdair G Kergon 	struct dm_target_io *tio;
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	/*
6831da177e4SLinus Torvalds 	 * Allocate a target io object.
6841da177e4SLinus Torvalds 	 */
6851da177e4SLinus Torvalds 	tio = alloc_tio(ci->md);
6861da177e4SLinus Torvalds 	tio->io = ci->io;
6871da177e4SLinus Torvalds 	tio->ti = ti;
6881da177e4SLinus Torvalds 	memset(&tio->info, 0, sizeof(tio->info));
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	if (ci->sector_count <= max) {
6911da177e4SLinus Torvalds 		/*
6921da177e4SLinus Torvalds 		 * Optimise for the simple case where we can do all of
6931da177e4SLinus Torvalds 		 * the remaining io with a single clone.
6941da177e4SLinus Torvalds 		 */
6951da177e4SLinus Torvalds 		clone = clone_bio(bio, ci->sector, ci->idx,
6969faf400fSStefan Bader 				  bio->bi_vcnt - ci->idx, ci->sector_count,
6979faf400fSStefan Bader 				  ci->md->bs);
6981da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
6991da177e4SLinus Torvalds 		ci->sector_count = 0;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	} else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
7021da177e4SLinus Torvalds 		/*
7031da177e4SLinus Torvalds 		 * There are some bvecs that don't span targets.
7041da177e4SLinus Torvalds 		 * Do as many of these as possible.
7051da177e4SLinus Torvalds 		 */
7061da177e4SLinus Torvalds 		int i;
7071da177e4SLinus Torvalds 		sector_t remaining = max;
7081da177e4SLinus Torvalds 		sector_t bv_len;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 		for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
7111da177e4SLinus Torvalds 			bv_len = to_sector(bio->bi_io_vec[i].bv_len);
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 			if (bv_len > remaining)
7141da177e4SLinus Torvalds 				break;
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 			remaining -= bv_len;
7171da177e4SLinus Torvalds 			len += bv_len;
7181da177e4SLinus Torvalds 		}
7191da177e4SLinus Torvalds 
7209faf400fSStefan Bader 		clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
7219faf400fSStefan Bader 				  ci->md->bs);
7221da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 		ci->sector += len;
7251da177e4SLinus Torvalds 		ci->sector_count -= len;
7261da177e4SLinus Torvalds 		ci->idx = i;
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	} else {
7291da177e4SLinus Torvalds 		/*
730d2044a94SAlasdair G Kergon 		 * Handle a bvec that must be split between two or more targets.
7311da177e4SLinus Torvalds 		 */
7321da177e4SLinus Torvalds 		struct bio_vec *bv = bio->bi_io_vec + ci->idx;
733d2044a94SAlasdair G Kergon 		sector_t remaining = to_sector(bv->bv_len);
734d2044a94SAlasdair G Kergon 		unsigned int offset = 0;
7351da177e4SLinus Torvalds 
736d2044a94SAlasdair G Kergon 		do {
737d2044a94SAlasdair G Kergon 			if (offset) {
7381da177e4SLinus Torvalds 				ti = dm_table_find_target(ci->map, ci->sector);
739d2044a94SAlasdair G Kergon 				max = max_io_len(ci->md, ci->sector, ti);
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 				tio = alloc_tio(ci->md);
7421da177e4SLinus Torvalds 				tio->io = ci->io;
7431da177e4SLinus Torvalds 				tio->ti = ti;
7441da177e4SLinus Torvalds 				memset(&tio->info, 0, sizeof(tio->info));
745d2044a94SAlasdair G Kergon 			}
746d2044a94SAlasdair G Kergon 
747d2044a94SAlasdair G Kergon 			len = min(remaining, max);
748d2044a94SAlasdair G Kergon 
749d2044a94SAlasdair G Kergon 			clone = split_bvec(bio, ci->sector, ci->idx,
7509faf400fSStefan Bader 					   bv->bv_offset + offset, len,
7519faf400fSStefan Bader 					   ci->md->bs);
752d2044a94SAlasdair G Kergon 
7531da177e4SLinus Torvalds 			__map_bio(ti, clone, tio);
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 			ci->sector += len;
7561da177e4SLinus Torvalds 			ci->sector_count -= len;
757d2044a94SAlasdair G Kergon 			offset += to_bytes(len);
758d2044a94SAlasdair G Kergon 		} while (remaining -= len);
759d2044a94SAlasdair G Kergon 
7601da177e4SLinus Torvalds 		ci->idx++;
7611da177e4SLinus Torvalds 	}
7621da177e4SLinus Torvalds }
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds /*
7651da177e4SLinus Torvalds  * Split the bio into several clones.
7661da177e4SLinus Torvalds  */
7679e4e5f87SMilan Broz static int __split_bio(struct mapped_device *md, struct bio *bio)
7681da177e4SLinus Torvalds {
7691da177e4SLinus Torvalds 	struct clone_info ci;
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 	ci.map = dm_get_table(md);
7729e4e5f87SMilan Broz 	if (unlikely(!ci.map))
7739e4e5f87SMilan Broz 		return -EIO;
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	ci.md = md;
7761da177e4SLinus Torvalds 	ci.bio = bio;
7771da177e4SLinus Torvalds 	ci.io = alloc_io(md);
7781da177e4SLinus Torvalds 	ci.io->error = 0;
7791da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
7801da177e4SLinus Torvalds 	ci.io->bio = bio;
7811da177e4SLinus Torvalds 	ci.io->md = md;
7821da177e4SLinus Torvalds 	ci.sector = bio->bi_sector;
7831da177e4SLinus Torvalds 	ci.sector_count = bio_sectors(bio);
7841da177e4SLinus Torvalds 	ci.idx = bio->bi_idx;
7851da177e4SLinus Torvalds 
7863eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
7871da177e4SLinus Torvalds 	while (ci.sector_count)
7881da177e4SLinus Torvalds 		__clone_and_map(&ci);
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	/* drop the extra reference count */
7911da177e4SLinus Torvalds 	dec_pending(ci.io, 0);
7921da177e4SLinus Torvalds 	dm_table_put(ci.map);
7939e4e5f87SMilan Broz 
7949e4e5f87SMilan Broz 	return 0;
7951da177e4SLinus Torvalds }
7961da177e4SLinus Torvalds /*-----------------------------------------------------------------
7971da177e4SLinus Torvalds  * CRUD END
7981da177e4SLinus Torvalds  *---------------------------------------------------------------*/
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds /*
8011da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
8021da177e4SLinus Torvalds  * dm_merge_bvec.
8031da177e4SLinus Torvalds  */
804165125e1SJens Axboe static int dm_request(struct request_queue *q, struct bio *bio)
8051da177e4SLinus Torvalds {
8069e4e5f87SMilan Broz 	int r = -EIO;
80712f03a49SKevin Corry 	int rw = bio_data_dir(bio);
8081da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
8091da177e4SLinus Torvalds 
81007a83c47SStefan Bader 	/*
81107a83c47SStefan Bader 	 * There is no use in forwarding any barrier request since we can't
81207a83c47SStefan Bader 	 * guarantee it is (or can be) handled by the targets correctly.
81307a83c47SStefan Bader 	 */
81407a83c47SStefan Bader 	if (unlikely(bio_barrier(bio))) {
8156712ecf8SNeilBrown 		bio_endio(bio, -EOPNOTSUPP);
81607a83c47SStefan Bader 		return 0;
81707a83c47SStefan Bader 	}
81807a83c47SStefan Bader 
8192ca3310eSAlasdair G Kergon 	down_read(&md->io_lock);
8201da177e4SLinus Torvalds 
82112f03a49SKevin Corry 	disk_stat_inc(dm_disk(md), ios[rw]);
82212f03a49SKevin Corry 	disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
82312f03a49SKevin Corry 
8241da177e4SLinus Torvalds 	/*
8251da177e4SLinus Torvalds 	 * If we're suspended we have to queue
8261da177e4SLinus Torvalds 	 * this io for later.
8271da177e4SLinus Torvalds 	 */
8281da177e4SLinus Torvalds 	while (test_bit(DMF_BLOCK_IO, &md->flags)) {
8292ca3310eSAlasdair G Kergon 		up_read(&md->io_lock);
8301da177e4SLinus Torvalds 
8319e4e5f87SMilan Broz 		if (bio_rw(bio) != READA)
8321da177e4SLinus Torvalds 			r = queue_io(md, bio);
8331da177e4SLinus Torvalds 
8349e4e5f87SMilan Broz 		if (r <= 0)
8359e4e5f87SMilan Broz 			goto out_req;
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 		/*
8381da177e4SLinus Torvalds 		 * We're in a while loop, because someone could suspend
8391da177e4SLinus Torvalds 		 * before we get to the following read lock.
8401da177e4SLinus Torvalds 		 */
8412ca3310eSAlasdair G Kergon 		down_read(&md->io_lock);
8421da177e4SLinus Torvalds 	}
8431da177e4SLinus Torvalds 
8449e4e5f87SMilan Broz 	r = __split_bio(md, bio);
8452ca3310eSAlasdair G Kergon 	up_read(&md->io_lock);
8469e4e5f87SMilan Broz 
8479e4e5f87SMilan Broz out_req:
8489e4e5f87SMilan Broz 	if (r < 0)
8499e4e5f87SMilan Broz 		bio_io_error(bio);
8509e4e5f87SMilan Broz 
8511da177e4SLinus Torvalds 	return 0;
8521da177e4SLinus Torvalds }
8531da177e4SLinus Torvalds 
854165125e1SJens Axboe static void dm_unplug_all(struct request_queue *q)
8551da177e4SLinus Torvalds {
8561da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
8571da177e4SLinus Torvalds 	struct dm_table *map = dm_get_table(md);
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	if (map) {
8601da177e4SLinus Torvalds 		dm_table_unplug_all(map);
8611da177e4SLinus Torvalds 		dm_table_put(map);
8621da177e4SLinus Torvalds 	}
8631da177e4SLinus Torvalds }
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
8661da177e4SLinus Torvalds {
8671da177e4SLinus Torvalds 	int r;
8681da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) congested_data;
8691da177e4SLinus Torvalds 	struct dm_table *map = dm_get_table(md);
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
8721da177e4SLinus Torvalds 		r = bdi_bits;
8731da177e4SLinus Torvalds 	else
8741da177e4SLinus Torvalds 		r = dm_table_any_congested(map, bdi_bits);
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	dm_table_put(map);
8771da177e4SLinus Torvalds 	return r;
8781da177e4SLinus Torvalds }
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds /*-----------------------------------------------------------------
8811da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
8821da177e4SLinus Torvalds  *---------------------------------------------------------------*/
8831da177e4SLinus Torvalds static DEFINE_IDR(_minor_idr);
8841da177e4SLinus Torvalds 
8852b06cfffSAlasdair G Kergon static void free_minor(int minor)
8861da177e4SLinus Torvalds {
887f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
8881da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
889f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
8901da177e4SLinus Torvalds }
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds /*
8931da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
8941da177e4SLinus Torvalds  */
8952b06cfffSAlasdair G Kergon static int specific_minor(struct mapped_device *md, int minor)
8961da177e4SLinus Torvalds {
8971da177e4SLinus Torvalds 	int r, m;
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
9001da177e4SLinus Torvalds 		return -EINVAL;
9011da177e4SLinus Torvalds 
90262f75c2fSJeff Mahoney 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
90362f75c2fSJeff Mahoney 	if (!r)
90462f75c2fSJeff Mahoney 		return -ENOMEM;
90562f75c2fSJeff Mahoney 
906f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	if (idr_find(&_minor_idr, minor)) {
9091da177e4SLinus Torvalds 		r = -EBUSY;
9101da177e4SLinus Torvalds 		goto out;
9111da177e4SLinus Torvalds 	}
9121da177e4SLinus Torvalds 
913ba61fdd1SJeff Mahoney 	r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
91462f75c2fSJeff Mahoney 	if (r)
9151da177e4SLinus Torvalds 		goto out;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 	if (m != minor) {
9181da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
9191da177e4SLinus Torvalds 		r = -EBUSY;
9201da177e4SLinus Torvalds 		goto out;
9211da177e4SLinus Torvalds 	}
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds out:
924f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
9251da177e4SLinus Torvalds 	return r;
9261da177e4SLinus Torvalds }
9271da177e4SLinus Torvalds 
9282b06cfffSAlasdair G Kergon static int next_free_minor(struct mapped_device *md, int *minor)
9291da177e4SLinus Torvalds {
9302b06cfffSAlasdair G Kergon 	int r, m;
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
93362f75c2fSJeff Mahoney 	if (!r)
93462f75c2fSJeff Mahoney 		return -ENOMEM;
93562f75c2fSJeff Mahoney 
936f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
9371da177e4SLinus Torvalds 
938ba61fdd1SJeff Mahoney 	r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
9391da177e4SLinus Torvalds 	if (r) {
9401da177e4SLinus Torvalds 		goto out;
9411da177e4SLinus Torvalds 	}
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	if (m >= (1 << MINORBITS)) {
9441da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
9451da177e4SLinus Torvalds 		r = -ENOSPC;
9461da177e4SLinus Torvalds 		goto out;
9471da177e4SLinus Torvalds 	}
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	*minor = m;
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds out:
952f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
9531da177e4SLinus Torvalds 	return r;
9541da177e4SLinus Torvalds }
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops;
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds /*
9591da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
9601da177e4SLinus Torvalds  */
9612b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
9621da177e4SLinus Torvalds {
9631da177e4SLinus Torvalds 	int r;
9641da177e4SLinus Torvalds 	struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
965ba61fdd1SJeff Mahoney 	void *old_md;
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	if (!md) {
9681da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
9691da177e4SLinus Torvalds 		return NULL;
9701da177e4SLinus Torvalds 	}
9711da177e4SLinus Torvalds 
97210da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
97310da4f79SJeff Mahoney 		goto bad0;
97410da4f79SJeff Mahoney 
9751da177e4SLinus Torvalds 	/* get a minor number for the dev */
9762b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
9772b06cfffSAlasdair G Kergon 		r = next_free_minor(md, &minor);
9782b06cfffSAlasdair G Kergon 	else
9792b06cfffSAlasdair G Kergon 		r = specific_minor(md, minor);
9801da177e4SLinus Torvalds 	if (r < 0)
9811da177e4SLinus Torvalds 		goto bad1;
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 	memset(md, 0, sizeof(*md));
9842ca3310eSAlasdair G Kergon 	init_rwsem(&md->io_lock);
9852ca3310eSAlasdair G Kergon 	init_MUTEX(&md->suspend_lock);
9862e93ccc1SKiyoshi Ueda 	spin_lock_init(&md->pushback_lock);
9871da177e4SLinus Torvalds 	rwlock_init(&md->map_lock);
9881da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
9895c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
9901da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
991*7a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
992*7a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
993*7a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	md->queue = blk_alloc_queue(GFP_KERNEL);
9961da177e4SLinus Torvalds 	if (!md->queue)
997e3f6ac61SIshai Rabinovitz 		goto bad1_free_minor;
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 	md->queue->queuedata = md;
10001da177e4SLinus Torvalds 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
10011da177e4SLinus Torvalds 	md->queue->backing_dev_info.congested_data = md;
10021da177e4SLinus Torvalds 	blk_queue_make_request(md->queue, dm_request);
1003daef265fSJens Axboe 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
10041da177e4SLinus Torvalds 	md->queue->unplug_fn = dm_unplug_all;
10051da177e4SLinus Torvalds 
100693d2341cSMatthew Dobson 	md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
10071da177e4SLinus Torvalds 	if (!md->io_pool)
10081da177e4SLinus Torvalds 		goto bad2;
10091da177e4SLinus Torvalds 
101093d2341cSMatthew Dobson 	md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
10111da177e4SLinus Torvalds 	if (!md->tio_pool)
10121da177e4SLinus Torvalds 		goto bad3;
10131da177e4SLinus Torvalds 
10145972511bSJens Axboe 	md->bs = bioset_create(16, 16);
10159faf400fSStefan Bader 	if (!md->bs)
10169faf400fSStefan Bader 		goto bad_no_bioset;
10179faf400fSStefan Bader 
10181da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
10191da177e4SLinus Torvalds 	if (!md->disk)
10201da177e4SLinus Torvalds 		goto bad4;
10211da177e4SLinus Torvalds 
1022f0b04115SJeff Mahoney 	atomic_set(&md->pending, 0);
1023f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
1024f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
1025f0b04115SJeff Mahoney 
10261da177e4SLinus Torvalds 	md->disk->major = _major;
10271da177e4SLinus Torvalds 	md->disk->first_minor = minor;
10281da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
10291da177e4SLinus Torvalds 	md->disk->queue = md->queue;
10301da177e4SLinus Torvalds 	md->disk->private_data = md;
10311da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
10321da177e4SLinus Torvalds 	add_disk(md->disk);
10337e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
10341da177e4SLinus Torvalds 
1035ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1036f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1037ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1038f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1039ba61fdd1SJeff Mahoney 
1040ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1041ba61fdd1SJeff Mahoney 
10421da177e4SLinus Torvalds 	return md;
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds  bad4:
10459faf400fSStefan Bader 	bioset_free(md->bs);
10469faf400fSStefan Bader  bad_no_bioset:
10471da177e4SLinus Torvalds 	mempool_destroy(md->tio_pool);
10481da177e4SLinus Torvalds  bad3:
10491da177e4SLinus Torvalds 	mempool_destroy(md->io_pool);
10501da177e4SLinus Torvalds  bad2:
10511312f40eSAl Viro 	blk_cleanup_queue(md->queue);
1052e3f6ac61SIshai Rabinovitz  bad1_free_minor:
10531da177e4SLinus Torvalds 	free_minor(minor);
10541da177e4SLinus Torvalds  bad1:
105510da4f79SJeff Mahoney 	module_put(THIS_MODULE);
105610da4f79SJeff Mahoney  bad0:
10571da177e4SLinus Torvalds 	kfree(md);
10581da177e4SLinus Torvalds 	return NULL;
10591da177e4SLinus Torvalds }
10601da177e4SLinus Torvalds 
1061ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1062ae9da83fSJun'ichi Nomura 
10631da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
10641da177e4SLinus Torvalds {
10652b06cfffSAlasdair G Kergon 	int minor = md->disk->first_minor;
106663d94e48SJun'ichi Nomura 
1067d9dde59bSJun'ichi Nomura 	if (md->suspended_bdev) {
1068ae9da83fSJun'ichi Nomura 		unlock_fs(md);
1069d9dde59bSJun'ichi Nomura 		bdput(md->suspended_bdev);
1070d9dde59bSJun'ichi Nomura 	}
10711da177e4SLinus Torvalds 	mempool_destroy(md->tio_pool);
10721da177e4SLinus Torvalds 	mempool_destroy(md->io_pool);
10739faf400fSStefan Bader 	bioset_free(md->bs);
10741da177e4SLinus Torvalds 	del_gendisk(md->disk);
107563d94e48SJun'ichi Nomura 	free_minor(minor);
1076fba9f90eSJeff Mahoney 
1077fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
1078fba9f90eSJeff Mahoney 	md->disk->private_data = NULL;
1079fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
1080fba9f90eSJeff Mahoney 
10811da177e4SLinus Torvalds 	put_disk(md->disk);
10821312f40eSAl Viro 	blk_cleanup_queue(md->queue);
108310da4f79SJeff Mahoney 	module_put(THIS_MODULE);
10841da177e4SLinus Torvalds 	kfree(md);
10851da177e4SLinus Torvalds }
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds /*
10881da177e4SLinus Torvalds  * Bind a table to the device.
10891da177e4SLinus Torvalds  */
10901da177e4SLinus Torvalds static void event_callback(void *context)
10911da177e4SLinus Torvalds {
1092*7a8c3d3bSMike Anderson 	unsigned long flags;
1093*7a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
10941da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
10951da177e4SLinus Torvalds 
1096*7a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
1097*7a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
1098*7a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
1099*7a8c3d3bSMike Anderson 
1100*7a8c3d3bSMike Anderson 	dm_send_uevents(&uevents, &md->disk->kobj);
1101*7a8c3d3bSMike Anderson 
11021da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
11031da177e4SLinus Torvalds 	wake_up(&md->eventq);
11041da177e4SLinus Torvalds }
11051da177e4SLinus Torvalds 
11064e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
11071da177e4SLinus Torvalds {
11084e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
11091da177e4SLinus Torvalds 
11101b1dcc1bSJes Sorensen 	mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
1111e39e2e95SAlasdair G Kergon 	i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
11121b1dcc1bSJes Sorensen 	mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
11131da177e4SLinus Torvalds }
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds static int __bind(struct mapped_device *md, struct dm_table *t)
11161da177e4SLinus Torvalds {
1117165125e1SJens Axboe 	struct request_queue *q = md->queue;
11181da177e4SLinus Torvalds 	sector_t size;
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 	size = dm_table_get_size(t);
11213ac51e74SDarrick J. Wong 
11223ac51e74SDarrick J. Wong 	/*
11233ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
11243ac51e74SDarrick J. Wong 	 */
11253ac51e74SDarrick J. Wong 	if (size != get_capacity(md->disk))
11263ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
11273ac51e74SDarrick J. Wong 
1128bfa152faSJun'ichi Nomura 	if (md->suspended_bdev)
11294e90188bSAlasdair G Kergon 		__set_size(md, size);
11301da177e4SLinus Torvalds 	if (size == 0)
11311da177e4SLinus Torvalds 		return 0;
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 	dm_table_get(t);
1134cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
11352ca3310eSAlasdair G Kergon 
11362ca3310eSAlasdair G Kergon 	write_lock(&md->map_lock);
11372ca3310eSAlasdair G Kergon 	md->map = t;
11381da177e4SLinus Torvalds 	dm_table_set_restrictions(t, q);
11392ca3310eSAlasdair G Kergon 	write_unlock(&md->map_lock);
11402ca3310eSAlasdair G Kergon 
11411da177e4SLinus Torvalds 	return 0;
11421da177e4SLinus Torvalds }
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds static void __unbind(struct mapped_device *md)
11451da177e4SLinus Torvalds {
11461da177e4SLinus Torvalds 	struct dm_table *map = md->map;
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 	if (!map)
11491da177e4SLinus Torvalds 		return;
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
11521da177e4SLinus Torvalds 	write_lock(&md->map_lock);
11531da177e4SLinus Torvalds 	md->map = NULL;
11541da177e4SLinus Torvalds 	write_unlock(&md->map_lock);
11551da177e4SLinus Torvalds 	dm_table_put(map);
11561da177e4SLinus Torvalds }
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds /*
11591da177e4SLinus Torvalds  * Constructor for a new device.
11601da177e4SLinus Torvalds  */
11612b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
11621da177e4SLinus Torvalds {
11631da177e4SLinus Torvalds 	struct mapped_device *md;
11641da177e4SLinus Torvalds 
11652b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
11661da177e4SLinus Torvalds 	if (!md)
11671da177e4SLinus Torvalds 		return -ENXIO;
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	*result = md;
11701da177e4SLinus Torvalds 	return 0;
11711da177e4SLinus Torvalds }
11721da177e4SLinus Torvalds 
1173637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev)
11741da177e4SLinus Torvalds {
11751da177e4SLinus Torvalds 	struct mapped_device *md;
11761da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
11791da177e4SLinus Torvalds 		return NULL;
11801da177e4SLinus Torvalds 
1181f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
1184fba9f90eSJeff Mahoney 	if (md && (md == MINOR_ALLOCED ||
1185fba9f90eSJeff Mahoney 		   (dm_disk(md)->first_minor != minor) ||
1186fba9f90eSJeff Mahoney 		   test_bit(DMF_FREEING, &md->flags))) {
1187637842cfSDavid Teigland 		md = NULL;
1188fba9f90eSJeff Mahoney 		goto out;
1189fba9f90eSJeff Mahoney 	}
11901da177e4SLinus Torvalds 
1191fba9f90eSJeff Mahoney out:
1192f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
11931da177e4SLinus Torvalds 
1194637842cfSDavid Teigland 	return md;
1195637842cfSDavid Teigland }
1196637842cfSDavid Teigland 
1197d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev)
1198d229a958SDavid Teigland {
1199d229a958SDavid Teigland 	struct mapped_device *md = dm_find_md(dev);
1200d229a958SDavid Teigland 
1201d229a958SDavid Teigland 	if (md)
1202d229a958SDavid Teigland 		dm_get(md);
1203d229a958SDavid Teigland 
1204d229a958SDavid Teigland 	return md;
1205d229a958SDavid Teigland }
1206d229a958SDavid Teigland 
12079ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
1208637842cfSDavid Teigland {
12099ade92a9SAlasdair G Kergon 	return md->interface_ptr;
12101da177e4SLinus Torvalds }
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
12131da177e4SLinus Torvalds {
12141da177e4SLinus Torvalds 	md->interface_ptr = ptr;
12151da177e4SLinus Torvalds }
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
12181da177e4SLinus Torvalds {
12191da177e4SLinus Torvalds 	atomic_inc(&md->holders);
12201da177e4SLinus Torvalds }
12211da177e4SLinus Torvalds 
122272d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
122372d94861SAlasdair G Kergon {
122472d94861SAlasdair G Kergon 	return md->name;
122572d94861SAlasdair G Kergon }
122672d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
122772d94861SAlasdair G Kergon 
12281da177e4SLinus Torvalds void dm_put(struct mapped_device *md)
12291da177e4SLinus Torvalds {
12301134e5aeSMike Anderson 	struct dm_table *map;
12311da177e4SLinus Torvalds 
1232fba9f90eSJeff Mahoney 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
1233fba9f90eSJeff Mahoney 
1234f32c10b0SJeff Mahoney 	if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
12351134e5aeSMike Anderson 		map = dm_get_table(md);
1236ba61fdd1SJeff Mahoney 		idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
1237fba9f90eSJeff Mahoney 		set_bit(DMF_FREEING, &md->flags);
1238f32c10b0SJeff Mahoney 		spin_unlock(&_minor_lock);
1239cf222b37SAlasdair G Kergon 		if (!dm_suspended(md)) {
12401da177e4SLinus Torvalds 			dm_table_presuspend_targets(map);
12411da177e4SLinus Torvalds 			dm_table_postsuspend_targets(map);
12421da177e4SLinus Torvalds 		}
12431da177e4SLinus Torvalds 		__unbind(md);
12441134e5aeSMike Anderson 		dm_table_put(map);
12451da177e4SLinus Torvalds 		free_dev(md);
12461da177e4SLinus Torvalds 	}
12471da177e4SLinus Torvalds }
124879eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds /*
12511da177e4SLinus Torvalds  * Process the deferred bios
12521da177e4SLinus Torvalds  */
12531da177e4SLinus Torvalds static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
12541da177e4SLinus Torvalds {
12551da177e4SLinus Torvalds 	struct bio *n;
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds 	while (c) {
12581da177e4SLinus Torvalds 		n = c->bi_next;
12591da177e4SLinus Torvalds 		c->bi_next = NULL;
12609e4e5f87SMilan Broz 		if (__split_bio(md, c))
12619e4e5f87SMilan Broz 			bio_io_error(c);
12621da177e4SLinus Torvalds 		c = n;
12631da177e4SLinus Torvalds 	}
12641da177e4SLinus Torvalds }
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds /*
12671da177e4SLinus Torvalds  * Swap in a new table (destroying old one).
12681da177e4SLinus Torvalds  */
12691da177e4SLinus Torvalds int dm_swap_table(struct mapped_device *md, struct dm_table *table)
12701da177e4SLinus Torvalds {
127193c534aeSAlasdair G Kergon 	int r = -EINVAL;
12721da177e4SLinus Torvalds 
12732ca3310eSAlasdair G Kergon 	down(&md->suspend_lock);
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 	/* device must be suspended */
1276cf222b37SAlasdair G Kergon 	if (!dm_suspended(md))
127793c534aeSAlasdair G Kergon 		goto out;
12781da177e4SLinus Torvalds 
1279bfa152faSJun'ichi Nomura 	/* without bdev, the device size cannot be changed */
1280bfa152faSJun'ichi Nomura 	if (!md->suspended_bdev)
1281bfa152faSJun'ichi Nomura 		if (get_capacity(md->disk) != dm_table_get_size(table))
1282bfa152faSJun'ichi Nomura 			goto out;
1283bfa152faSJun'ichi Nomura 
12841da177e4SLinus Torvalds 	__unbind(md);
12851da177e4SLinus Torvalds 	r = __bind(md, table);
12861da177e4SLinus Torvalds 
128793c534aeSAlasdair G Kergon out:
12882ca3310eSAlasdair G Kergon 	up(&md->suspend_lock);
128993c534aeSAlasdair G Kergon 	return r;
12901da177e4SLinus Torvalds }
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds /*
12931da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
12941da177e4SLinus Torvalds  * device.
12951da177e4SLinus Torvalds  */
12962ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
12971da177e4SLinus Torvalds {
1298e39e2e95SAlasdair G Kergon 	int r;
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
1301dfbe03f6SAlasdair G Kergon 
1302e39e2e95SAlasdair G Kergon 	md->frozen_sb = freeze_bdev(md->suspended_bdev);
1303dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
1304cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
1305e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
1306e39e2e95SAlasdair G Kergon 		return r;
1307dfbe03f6SAlasdair G Kergon 	}
1308dfbe03f6SAlasdair G Kergon 
1309aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
1310aa8d7c2fSAlasdair G Kergon 
13111da177e4SLinus Torvalds 	/* don't bdput right now, we don't want the bdev
1312e39e2e95SAlasdair G Kergon 	 * to go away while it is locked.
13131da177e4SLinus Torvalds 	 */
13141da177e4SLinus Torvalds 	return 0;
13151da177e4SLinus Torvalds }
13161da177e4SLinus Torvalds 
13172ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
13181da177e4SLinus Torvalds {
1319aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
1320aa8d7c2fSAlasdair G Kergon 		return;
1321aa8d7c2fSAlasdair G Kergon 
1322e39e2e95SAlasdair G Kergon 	thaw_bdev(md->suspended_bdev, md->frozen_sb);
13231da177e4SLinus Torvalds 	md->frozen_sb = NULL;
1324aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
13251da177e4SLinus Torvalds }
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds /*
13281da177e4SLinus Torvalds  * We need to be able to change a mapping table under a mounted
13291da177e4SLinus Torvalds  * filesystem.  For example we might want to move some data in
13301da177e4SLinus Torvalds  * the background.  Before the table can be swapped with
13311da177e4SLinus Torvalds  * dm_bind_table, dm_suspend must be called to flush any in
13321da177e4SLinus Torvalds  * flight bios and ensure that any further io gets deferred.
13331da177e4SLinus Torvalds  */
1334a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
13351da177e4SLinus Torvalds {
13362ca3310eSAlasdair G Kergon 	struct dm_table *map = NULL;
13372e93ccc1SKiyoshi Ueda 	unsigned long flags;
13381da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
13391ecac7fdSJun'ichi Nomura 	struct bio *def;
1340cf222b37SAlasdair G Kergon 	int r = -EINVAL;
1341a3d77d35SKiyoshi Ueda 	int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
13422e93ccc1SKiyoshi Ueda 	int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
13431da177e4SLinus Torvalds 
13442ca3310eSAlasdair G Kergon 	down(&md->suspend_lock);
13452ca3310eSAlasdair G Kergon 
13462ca3310eSAlasdair G Kergon 	if (dm_suspended(md))
1347d287483dSAlasdair G Kergon 		goto out_unlock;
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 	map = dm_get_table(md);
1350cf222b37SAlasdair G Kergon 
13512e93ccc1SKiyoshi Ueda 	/*
13522e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
13532e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
13542e93ccc1SKiyoshi Ueda 	 */
13552e93ccc1SKiyoshi Ueda 	if (noflush)
13562e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
13572e93ccc1SKiyoshi Ueda 
1358436d4108SAlasdair G Kergon 	/* This does not get reverted if there's an error later. */
13591da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
13601da177e4SLinus Torvalds 
1361bfa152faSJun'ichi Nomura 	/* bdget() can stall if the pending I/Os are not flushed */
1362bfa152faSJun'ichi Nomura 	if (!noflush) {
1363e39e2e95SAlasdair G Kergon 		md->suspended_bdev = bdget_disk(md->disk, 0);
1364e39e2e95SAlasdair G Kergon 		if (!md->suspended_bdev) {
1365e39e2e95SAlasdair G Kergon 			DMWARN("bdget failed in dm_suspend");
1366e39e2e95SAlasdair G Kergon 			r = -ENOMEM;
13672e93ccc1SKiyoshi Ueda 			goto flush_and_out;
1368e39e2e95SAlasdair G Kergon 		}
1369bfa152faSJun'ichi Nomura 	}
1370e39e2e95SAlasdair G Kergon 
13712e93ccc1SKiyoshi Ueda 	/*
13722e93ccc1SKiyoshi Ueda 	 * Flush I/O to the device.
13732e93ccc1SKiyoshi Ueda 	 * noflush supersedes do_lockfs, because lock_fs() needs to flush I/Os.
13742e93ccc1SKiyoshi Ueda 	 */
13752e93ccc1SKiyoshi Ueda 	if (do_lockfs && !noflush) {
13762ca3310eSAlasdair G Kergon 		r = lock_fs(md);
13772ca3310eSAlasdair G Kergon 		if (r)
13782ca3310eSAlasdair G Kergon 			goto out;
1379aa8d7c2fSAlasdair G Kergon 	}
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 	/*
1382354e0071SAlasdair G Kergon 	 * First we set the BLOCK_IO flag so no more ios will be mapped.
13831da177e4SLinus Torvalds 	 */
13842ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
13852ca3310eSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO, &md->flags);
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	add_wait_queue(&md->wait, &wait);
13882ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 	/* unplug */
13912ca3310eSAlasdair G Kergon 	if (map)
13921da177e4SLinus Torvalds 		dm_table_unplug_all(map);
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds 	/*
13951da177e4SLinus Torvalds 	 * Then we wait for the already mapped ios to
13961da177e4SLinus Torvalds 	 * complete.
13971da177e4SLinus Torvalds 	 */
13981da177e4SLinus Torvalds 	while (1) {
13991da177e4SLinus Torvalds 		set_current_state(TASK_INTERRUPTIBLE);
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 		if (!atomic_read(&md->pending) || signal_pending(current))
14021da177e4SLinus Torvalds 			break;
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds 		io_schedule();
14051da177e4SLinus Torvalds 	}
14061da177e4SLinus Torvalds 	set_current_state(TASK_RUNNING);
14071da177e4SLinus Torvalds 
14082ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
14091da177e4SLinus Torvalds 	remove_wait_queue(&md->wait, &wait);
14101da177e4SLinus Torvalds 
14112e93ccc1SKiyoshi Ueda 	if (noflush) {
14122e93ccc1SKiyoshi Ueda 		spin_lock_irqsave(&md->pushback_lock, flags);
14132e93ccc1SKiyoshi Ueda 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
14142e93ccc1SKiyoshi Ueda 		bio_list_merge_head(&md->deferred, &md->pushback);
14152e93ccc1SKiyoshi Ueda 		bio_list_init(&md->pushback);
14162e93ccc1SKiyoshi Ueda 		spin_unlock_irqrestore(&md->pushback_lock, flags);
14172e93ccc1SKiyoshi Ueda 	}
14182e93ccc1SKiyoshi Ueda 
14191da177e4SLinus Torvalds 	/* were we interrupted ? */
1420cf222b37SAlasdair G Kergon 	r = -EINTR;
14212ca3310eSAlasdair G Kergon 	if (atomic_read(&md->pending)) {
14221ecac7fdSJun'ichi Nomura 		clear_bit(DMF_BLOCK_IO, &md->flags);
14231ecac7fdSJun'ichi Nomura 		def = bio_list_get(&md->deferred);
14241ecac7fdSJun'ichi Nomura 		__flush_deferred_io(md, def);
14252ca3310eSAlasdair G Kergon 		up_write(&md->io_lock);
14262ca3310eSAlasdair G Kergon 		unlock_fs(md);
14272e93ccc1SKiyoshi Ueda 		goto out; /* pushback list is already flushed, so skip flush */
14282ca3310eSAlasdair G Kergon 	}
14292ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
14302ca3310eSAlasdair G Kergon 
14312ca3310eSAlasdair G Kergon 	dm_table_postsuspend_targets(map);
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
14341da177e4SLinus Torvalds 
14352ca3310eSAlasdair G Kergon 	r = 0;
14362ca3310eSAlasdair G Kergon 
14372e93ccc1SKiyoshi Ueda flush_and_out:
14382e93ccc1SKiyoshi Ueda 	if (r && noflush) {
14392e93ccc1SKiyoshi Ueda 		/*
14402e93ccc1SKiyoshi Ueda 		 * Because there may be already I/Os in the pushback list,
14412e93ccc1SKiyoshi Ueda 		 * flush them before return.
14422e93ccc1SKiyoshi Ueda 		 */
14432e93ccc1SKiyoshi Ueda 		down_write(&md->io_lock);
14442e93ccc1SKiyoshi Ueda 
14452e93ccc1SKiyoshi Ueda 		spin_lock_irqsave(&md->pushback_lock, flags);
14462e93ccc1SKiyoshi Ueda 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
14472e93ccc1SKiyoshi Ueda 		bio_list_merge_head(&md->deferred, &md->pushback);
14482e93ccc1SKiyoshi Ueda 		bio_list_init(&md->pushback);
14492e93ccc1SKiyoshi Ueda 		spin_unlock_irqrestore(&md->pushback_lock, flags);
14502e93ccc1SKiyoshi Ueda 
14512e93ccc1SKiyoshi Ueda 		def = bio_list_get(&md->deferred);
14522e93ccc1SKiyoshi Ueda 		__flush_deferred_io(md, def);
14532e93ccc1SKiyoshi Ueda 		up_write(&md->io_lock);
14542e93ccc1SKiyoshi Ueda 	}
14552e93ccc1SKiyoshi Ueda 
14562ca3310eSAlasdair G Kergon out:
1457e39e2e95SAlasdair G Kergon 	if (r && md->suspended_bdev) {
1458e39e2e95SAlasdair G Kergon 		bdput(md->suspended_bdev);
1459e39e2e95SAlasdair G Kergon 		md->suspended_bdev = NULL;
1460e39e2e95SAlasdair G Kergon 	}
1461e39e2e95SAlasdair G Kergon 
14621da177e4SLinus Torvalds 	dm_table_put(map);
1463d287483dSAlasdair G Kergon 
1464d287483dSAlasdair G Kergon out_unlock:
14652ca3310eSAlasdair G Kergon 	up(&md->suspend_lock);
1466cf222b37SAlasdair G Kergon 	return r;
14671da177e4SLinus Torvalds }
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds int dm_resume(struct mapped_device *md)
14701da177e4SLinus Torvalds {
1471cf222b37SAlasdair G Kergon 	int r = -EINVAL;
14721da177e4SLinus Torvalds 	struct bio *def;
1473cf222b37SAlasdair G Kergon 	struct dm_table *map = NULL;
14741da177e4SLinus Torvalds 
14752ca3310eSAlasdair G Kergon 	down(&md->suspend_lock);
14762ca3310eSAlasdair G Kergon 	if (!dm_suspended(md))
1477cf222b37SAlasdair G Kergon 		goto out;
1478cf222b37SAlasdair G Kergon 
1479cf222b37SAlasdair G Kergon 	map = dm_get_table(md);
14802ca3310eSAlasdair G Kergon 	if (!map || !dm_table_get_size(map))
1481cf222b37SAlasdair G Kergon 		goto out;
14821da177e4SLinus Torvalds 
14838757b776SMilan Broz 	r = dm_table_resume_targets(map);
14848757b776SMilan Broz 	if (r)
14858757b776SMilan Broz 		goto out;
14862ca3310eSAlasdair G Kergon 
14872ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
14881da177e4SLinus Torvalds 	clear_bit(DMF_BLOCK_IO, &md->flags);
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds 	def = bio_list_get(&md->deferred);
14911da177e4SLinus Torvalds 	__flush_deferred_io(md, def);
14922ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
14932ca3310eSAlasdair G Kergon 
14942ca3310eSAlasdair G Kergon 	unlock_fs(md);
14952ca3310eSAlasdair G Kergon 
1496bfa152faSJun'ichi Nomura 	if (md->suspended_bdev) {
1497e39e2e95SAlasdair G Kergon 		bdput(md->suspended_bdev);
1498e39e2e95SAlasdair G Kergon 		md->suspended_bdev = NULL;
1499bfa152faSJun'ichi Nomura 	}
1500e39e2e95SAlasdair G Kergon 
15012ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
15022ca3310eSAlasdair G Kergon 
15031da177e4SLinus Torvalds 	dm_table_unplug_all(map);
15041da177e4SLinus Torvalds 
15058560ed6fSHannes Reinecke 	kobject_uevent(&md->disk->kobj, KOBJ_CHANGE);
15068560ed6fSHannes Reinecke 
1507cf222b37SAlasdair G Kergon 	r = 0;
15082ca3310eSAlasdair G Kergon 
1509cf222b37SAlasdair G Kergon out:
1510cf222b37SAlasdair G Kergon 	dm_table_put(map);
15112ca3310eSAlasdair G Kergon 	up(&md->suspend_lock);
15122ca3310eSAlasdair G Kergon 
1513cf222b37SAlasdair G Kergon 	return r;
15141da177e4SLinus Torvalds }
15151da177e4SLinus Torvalds 
15161da177e4SLinus Torvalds /*-----------------------------------------------------------------
15171da177e4SLinus Torvalds  * Event notification.
15181da177e4SLinus Torvalds  *---------------------------------------------------------------*/
1519*7a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
1520*7a8c3d3bSMike Anderson {
1521*7a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
1522*7a8c3d3bSMike Anderson }
1523*7a8c3d3bSMike Anderson 
15241da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
15251da177e4SLinus Torvalds {
15261da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
15271da177e4SLinus Torvalds }
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
15301da177e4SLinus Torvalds {
15311da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
15321da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
15331da177e4SLinus Torvalds }
15341da177e4SLinus Torvalds 
1535*7a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
1536*7a8c3d3bSMike Anderson {
1537*7a8c3d3bSMike Anderson 	unsigned long flags;
1538*7a8c3d3bSMike Anderson 
1539*7a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
1540*7a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
1541*7a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
1542*7a8c3d3bSMike Anderson }
1543*7a8c3d3bSMike Anderson 
15441da177e4SLinus Torvalds /*
15451da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
15461da177e4SLinus Torvalds  * count on 'md'.
15471da177e4SLinus Torvalds  */
15481da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
15491da177e4SLinus Torvalds {
15501da177e4SLinus Torvalds 	return md->disk;
15511da177e4SLinus Torvalds }
15521da177e4SLinus Torvalds 
15531da177e4SLinus Torvalds int dm_suspended(struct mapped_device *md)
15541da177e4SLinus Torvalds {
15551da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
15561da177e4SLinus Torvalds }
15571da177e4SLinus Torvalds 
15582e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
15592e93ccc1SKiyoshi Ueda {
15602e93ccc1SKiyoshi Ueda 	struct mapped_device *md = dm_table_get_md(ti->table);
15612e93ccc1SKiyoshi Ueda 	int r = __noflush_suspending(md);
15622e93ccc1SKiyoshi Ueda 
15632e93ccc1SKiyoshi Ueda 	dm_put(md);
15642e93ccc1SKiyoshi Ueda 
15652e93ccc1SKiyoshi Ueda 	return r;
15662e93ccc1SKiyoshi Ueda }
15672e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
15682e93ccc1SKiyoshi Ueda 
15691da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops = {
15701da177e4SLinus Torvalds 	.open = dm_blk_open,
15711da177e4SLinus Torvalds 	.release = dm_blk_close,
1572aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
15733ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
15741da177e4SLinus Torvalds 	.owner = THIS_MODULE
15751da177e4SLinus Torvalds };
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo);
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds /*
15801da177e4SLinus Torvalds  * module hooks
15811da177e4SLinus Torvalds  */
15821da177e4SLinus Torvalds module_init(dm_init);
15831da177e4SLinus Torvalds module_exit(dm_exit);
15841da177e4SLinus Torvalds 
15851da177e4SLinus Torvalds module_param(major, uint, 0);
15861da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
15871da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
15881da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
15891da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1590