xref: /openbmc/linux/drivers/md/dm.c (revision edfaa7c36574f1bf09c65ad602412db9da5f96bf)
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;
1167a8c3d3bSMike Anderson 	atomic_t uevent_seq;
1177a8c3d3bSMike Anderson 	struct list_head uevent_list;
1187a8c3d3bSMike 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 
675512875bdSJun'ichi Nomura static int __clone_and_map(struct clone_info *ci)
6761da177e4SLinus Torvalds {
6771da177e4SLinus Torvalds 	struct bio *clone, *bio = ci->bio;
678512875bdSJun'ichi Nomura 	struct dm_target *ti;
679512875bdSJun'ichi Nomura 	sector_t len = 0, max;
680028867acSAlasdair G Kergon 	struct dm_target_io *tio;
6811da177e4SLinus Torvalds 
682512875bdSJun'ichi Nomura 	ti = dm_table_find_target(ci->map, ci->sector);
683512875bdSJun'ichi Nomura 	if (!dm_target_is_valid(ti))
684512875bdSJun'ichi Nomura 		return -EIO;
685512875bdSJun'ichi Nomura 
686512875bdSJun'ichi Nomura 	max = max_io_len(ci->md, ci->sector, ti);
687512875bdSJun'ichi Nomura 
6881da177e4SLinus Torvalds 	/*
6891da177e4SLinus Torvalds 	 * Allocate a target io object.
6901da177e4SLinus Torvalds 	 */
6911da177e4SLinus Torvalds 	tio = alloc_tio(ci->md);
6921da177e4SLinus Torvalds 	tio->io = ci->io;
6931da177e4SLinus Torvalds 	tio->ti = ti;
6941da177e4SLinus Torvalds 	memset(&tio->info, 0, sizeof(tio->info));
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 	if (ci->sector_count <= max) {
6971da177e4SLinus Torvalds 		/*
6981da177e4SLinus Torvalds 		 * Optimise for the simple case where we can do all of
6991da177e4SLinus Torvalds 		 * the remaining io with a single clone.
7001da177e4SLinus Torvalds 		 */
7011da177e4SLinus Torvalds 		clone = clone_bio(bio, ci->sector, ci->idx,
7029faf400fSStefan Bader 				  bio->bi_vcnt - ci->idx, ci->sector_count,
7039faf400fSStefan Bader 				  ci->md->bs);
7041da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
7051da177e4SLinus Torvalds 		ci->sector_count = 0;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	} else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
7081da177e4SLinus Torvalds 		/*
7091da177e4SLinus Torvalds 		 * There are some bvecs that don't span targets.
7101da177e4SLinus Torvalds 		 * Do as many of these as possible.
7111da177e4SLinus Torvalds 		 */
7121da177e4SLinus Torvalds 		int i;
7131da177e4SLinus Torvalds 		sector_t remaining = max;
7141da177e4SLinus Torvalds 		sector_t bv_len;
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 		for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
7171da177e4SLinus Torvalds 			bv_len = to_sector(bio->bi_io_vec[i].bv_len);
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 			if (bv_len > remaining)
7201da177e4SLinus Torvalds 				break;
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 			remaining -= bv_len;
7231da177e4SLinus Torvalds 			len += bv_len;
7241da177e4SLinus Torvalds 		}
7251da177e4SLinus Torvalds 
7269faf400fSStefan Bader 		clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
7279faf400fSStefan Bader 				  ci->md->bs);
7281da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 		ci->sector += len;
7311da177e4SLinus Torvalds 		ci->sector_count -= len;
7321da177e4SLinus Torvalds 		ci->idx = i;
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 	} else {
7351da177e4SLinus Torvalds 		/*
736d2044a94SAlasdair G Kergon 		 * Handle a bvec that must be split between two or more targets.
7371da177e4SLinus Torvalds 		 */
7381da177e4SLinus Torvalds 		struct bio_vec *bv = bio->bi_io_vec + ci->idx;
739d2044a94SAlasdair G Kergon 		sector_t remaining = to_sector(bv->bv_len);
740d2044a94SAlasdair G Kergon 		unsigned int offset = 0;
7411da177e4SLinus Torvalds 
742d2044a94SAlasdair G Kergon 		do {
743d2044a94SAlasdair G Kergon 			if (offset) {
7441da177e4SLinus Torvalds 				ti = dm_table_find_target(ci->map, ci->sector);
745512875bdSJun'ichi Nomura 				if (!dm_target_is_valid(ti))
746512875bdSJun'ichi Nomura 					return -EIO;
747512875bdSJun'ichi Nomura 
748d2044a94SAlasdair G Kergon 				max = max_io_len(ci->md, ci->sector, ti);
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 				tio = alloc_tio(ci->md);
7511da177e4SLinus Torvalds 				tio->io = ci->io;
7521da177e4SLinus Torvalds 				tio->ti = ti;
7531da177e4SLinus Torvalds 				memset(&tio->info, 0, sizeof(tio->info));
754d2044a94SAlasdair G Kergon 			}
755d2044a94SAlasdair G Kergon 
756d2044a94SAlasdair G Kergon 			len = min(remaining, max);
757d2044a94SAlasdair G Kergon 
758d2044a94SAlasdair G Kergon 			clone = split_bvec(bio, ci->sector, ci->idx,
7599faf400fSStefan Bader 					   bv->bv_offset + offset, len,
7609faf400fSStefan Bader 					   ci->md->bs);
761d2044a94SAlasdair G Kergon 
7621da177e4SLinus Torvalds 			__map_bio(ti, clone, tio);
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 			ci->sector += len;
7651da177e4SLinus Torvalds 			ci->sector_count -= len;
766d2044a94SAlasdair G Kergon 			offset += to_bytes(len);
767d2044a94SAlasdair G Kergon 		} while (remaining -= len);
768d2044a94SAlasdair G Kergon 
7691da177e4SLinus Torvalds 		ci->idx++;
7701da177e4SLinus Torvalds 	}
771512875bdSJun'ichi Nomura 
772512875bdSJun'ichi Nomura 	return 0;
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds /*
7761da177e4SLinus Torvalds  * Split the bio into several clones.
7771da177e4SLinus Torvalds  */
7789e4e5f87SMilan Broz static int __split_bio(struct mapped_device *md, struct bio *bio)
7791da177e4SLinus Torvalds {
7801da177e4SLinus Torvalds 	struct clone_info ci;
781512875bdSJun'ichi Nomura 	int error = 0;
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	ci.map = dm_get_table(md);
7849e4e5f87SMilan Broz 	if (unlikely(!ci.map))
7859e4e5f87SMilan Broz 		return -EIO;
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	ci.md = md;
7881da177e4SLinus Torvalds 	ci.bio = bio;
7891da177e4SLinus Torvalds 	ci.io = alloc_io(md);
7901da177e4SLinus Torvalds 	ci.io->error = 0;
7911da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
7921da177e4SLinus Torvalds 	ci.io->bio = bio;
7931da177e4SLinus Torvalds 	ci.io->md = md;
7941da177e4SLinus Torvalds 	ci.sector = bio->bi_sector;
7951da177e4SLinus Torvalds 	ci.sector_count = bio_sectors(bio);
7961da177e4SLinus Torvalds 	ci.idx = bio->bi_idx;
7971da177e4SLinus Torvalds 
7983eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
799512875bdSJun'ichi Nomura 	while (ci.sector_count && !error)
800512875bdSJun'ichi Nomura 		error = __clone_and_map(&ci);
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	/* drop the extra reference count */
803512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
8041da177e4SLinus Torvalds 	dm_table_put(ci.map);
8059e4e5f87SMilan Broz 
8069e4e5f87SMilan Broz 	return 0;
8071da177e4SLinus Torvalds }
8081da177e4SLinus Torvalds /*-----------------------------------------------------------------
8091da177e4SLinus Torvalds  * CRUD END
8101da177e4SLinus Torvalds  *---------------------------------------------------------------*/
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds /*
8131da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
8141da177e4SLinus Torvalds  * dm_merge_bvec.
8151da177e4SLinus Torvalds  */
816165125e1SJens Axboe static int dm_request(struct request_queue *q, struct bio *bio)
8171da177e4SLinus Torvalds {
8189e4e5f87SMilan Broz 	int r = -EIO;
81912f03a49SKevin Corry 	int rw = bio_data_dir(bio);
8201da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
8211da177e4SLinus Torvalds 
82207a83c47SStefan Bader 	/*
82307a83c47SStefan Bader 	 * There is no use in forwarding any barrier request since we can't
82407a83c47SStefan Bader 	 * guarantee it is (or can be) handled by the targets correctly.
82507a83c47SStefan Bader 	 */
82607a83c47SStefan Bader 	if (unlikely(bio_barrier(bio))) {
8276712ecf8SNeilBrown 		bio_endio(bio, -EOPNOTSUPP);
82807a83c47SStefan Bader 		return 0;
82907a83c47SStefan Bader 	}
83007a83c47SStefan Bader 
8312ca3310eSAlasdair G Kergon 	down_read(&md->io_lock);
8321da177e4SLinus Torvalds 
83312f03a49SKevin Corry 	disk_stat_inc(dm_disk(md), ios[rw]);
83412f03a49SKevin Corry 	disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
83512f03a49SKevin Corry 
8361da177e4SLinus Torvalds 	/*
8371da177e4SLinus Torvalds 	 * If we're suspended we have to queue
8381da177e4SLinus Torvalds 	 * this io for later.
8391da177e4SLinus Torvalds 	 */
8401da177e4SLinus Torvalds 	while (test_bit(DMF_BLOCK_IO, &md->flags)) {
8412ca3310eSAlasdair G Kergon 		up_read(&md->io_lock);
8421da177e4SLinus Torvalds 
8439e4e5f87SMilan Broz 		if (bio_rw(bio) != READA)
8441da177e4SLinus Torvalds 			r = queue_io(md, bio);
8451da177e4SLinus Torvalds 
8469e4e5f87SMilan Broz 		if (r <= 0)
8479e4e5f87SMilan Broz 			goto out_req;
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 		/*
8501da177e4SLinus Torvalds 		 * We're in a while loop, because someone could suspend
8511da177e4SLinus Torvalds 		 * before we get to the following read lock.
8521da177e4SLinus Torvalds 		 */
8532ca3310eSAlasdair G Kergon 		down_read(&md->io_lock);
8541da177e4SLinus Torvalds 	}
8551da177e4SLinus Torvalds 
8569e4e5f87SMilan Broz 	r = __split_bio(md, bio);
8572ca3310eSAlasdair G Kergon 	up_read(&md->io_lock);
8589e4e5f87SMilan Broz 
8599e4e5f87SMilan Broz out_req:
8609e4e5f87SMilan Broz 	if (r < 0)
8619e4e5f87SMilan Broz 		bio_io_error(bio);
8629e4e5f87SMilan Broz 
8631da177e4SLinus Torvalds 	return 0;
8641da177e4SLinus Torvalds }
8651da177e4SLinus Torvalds 
866165125e1SJens Axboe static void dm_unplug_all(struct request_queue *q)
8671da177e4SLinus Torvalds {
8681da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
8691da177e4SLinus Torvalds 	struct dm_table *map = dm_get_table(md);
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	if (map) {
8721da177e4SLinus Torvalds 		dm_table_unplug_all(map);
8731da177e4SLinus Torvalds 		dm_table_put(map);
8741da177e4SLinus Torvalds 	}
8751da177e4SLinus Torvalds }
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
8781da177e4SLinus Torvalds {
8791da177e4SLinus Torvalds 	int r;
8801da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) congested_data;
8811da177e4SLinus Torvalds 	struct dm_table *map = dm_get_table(md);
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
8841da177e4SLinus Torvalds 		r = bdi_bits;
8851da177e4SLinus Torvalds 	else
8861da177e4SLinus Torvalds 		r = dm_table_any_congested(map, bdi_bits);
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	dm_table_put(map);
8891da177e4SLinus Torvalds 	return r;
8901da177e4SLinus Torvalds }
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds /*-----------------------------------------------------------------
8931da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
8941da177e4SLinus Torvalds  *---------------------------------------------------------------*/
8951da177e4SLinus Torvalds static DEFINE_IDR(_minor_idr);
8961da177e4SLinus Torvalds 
8972b06cfffSAlasdair G Kergon static void free_minor(int minor)
8981da177e4SLinus Torvalds {
899f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
9001da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
901f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
9021da177e4SLinus Torvalds }
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds /*
9051da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
9061da177e4SLinus Torvalds  */
9072b06cfffSAlasdair G Kergon static int specific_minor(struct mapped_device *md, int minor)
9081da177e4SLinus Torvalds {
9091da177e4SLinus Torvalds 	int r, m;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
9121da177e4SLinus Torvalds 		return -EINVAL;
9131da177e4SLinus Torvalds 
91462f75c2fSJeff Mahoney 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
91562f75c2fSJeff Mahoney 	if (!r)
91662f75c2fSJeff Mahoney 		return -ENOMEM;
91762f75c2fSJeff Mahoney 
918f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 	if (idr_find(&_minor_idr, minor)) {
9211da177e4SLinus Torvalds 		r = -EBUSY;
9221da177e4SLinus Torvalds 		goto out;
9231da177e4SLinus Torvalds 	}
9241da177e4SLinus Torvalds 
925ba61fdd1SJeff Mahoney 	r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
92662f75c2fSJeff Mahoney 	if (r)
9271da177e4SLinus Torvalds 		goto out;
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	if (m != minor) {
9301da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
9311da177e4SLinus Torvalds 		r = -EBUSY;
9321da177e4SLinus Torvalds 		goto out;
9331da177e4SLinus Torvalds 	}
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds out:
936f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
9371da177e4SLinus Torvalds 	return r;
9381da177e4SLinus Torvalds }
9391da177e4SLinus Torvalds 
9402b06cfffSAlasdair G Kergon static int next_free_minor(struct mapped_device *md, int *minor)
9411da177e4SLinus Torvalds {
9422b06cfffSAlasdair G Kergon 	int r, m;
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
94562f75c2fSJeff Mahoney 	if (!r)
94662f75c2fSJeff Mahoney 		return -ENOMEM;
94762f75c2fSJeff Mahoney 
948f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
9491da177e4SLinus Torvalds 
950ba61fdd1SJeff Mahoney 	r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
9511da177e4SLinus Torvalds 	if (r) {
9521da177e4SLinus Torvalds 		goto out;
9531da177e4SLinus Torvalds 	}
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	if (m >= (1 << MINORBITS)) {
9561da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
9571da177e4SLinus Torvalds 		r = -ENOSPC;
9581da177e4SLinus Torvalds 		goto out;
9591da177e4SLinus Torvalds 	}
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	*minor = m;
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds out:
964f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
9651da177e4SLinus Torvalds 	return r;
9661da177e4SLinus Torvalds }
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops;
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds /*
9711da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
9721da177e4SLinus Torvalds  */
9732b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
9741da177e4SLinus Torvalds {
9751da177e4SLinus Torvalds 	int r;
9761da177e4SLinus Torvalds 	struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
977ba61fdd1SJeff Mahoney 	void *old_md;
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	if (!md) {
9801da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
9811da177e4SLinus Torvalds 		return NULL;
9821da177e4SLinus Torvalds 	}
9831da177e4SLinus Torvalds 
98410da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
98510da4f79SJeff Mahoney 		goto bad0;
98610da4f79SJeff Mahoney 
9871da177e4SLinus Torvalds 	/* get a minor number for the dev */
9882b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
9892b06cfffSAlasdair G Kergon 		r = next_free_minor(md, &minor);
9902b06cfffSAlasdair G Kergon 	else
9912b06cfffSAlasdair G Kergon 		r = specific_minor(md, minor);
9921da177e4SLinus Torvalds 	if (r < 0)
9931da177e4SLinus Torvalds 		goto bad1;
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	memset(md, 0, sizeof(*md));
9962ca3310eSAlasdair G Kergon 	init_rwsem(&md->io_lock);
9972ca3310eSAlasdair G Kergon 	init_MUTEX(&md->suspend_lock);
9982e93ccc1SKiyoshi Ueda 	spin_lock_init(&md->pushback_lock);
9991da177e4SLinus Torvalds 	rwlock_init(&md->map_lock);
10001da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
10015c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
10021da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
10037a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
10047a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
10057a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	md->queue = blk_alloc_queue(GFP_KERNEL);
10081da177e4SLinus Torvalds 	if (!md->queue)
1009e3f6ac61SIshai Rabinovitz 		goto bad1_free_minor;
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 	md->queue->queuedata = md;
10121da177e4SLinus Torvalds 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
10131da177e4SLinus Torvalds 	md->queue->backing_dev_info.congested_data = md;
10141da177e4SLinus Torvalds 	blk_queue_make_request(md->queue, dm_request);
1015daef265fSJens Axboe 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
10161da177e4SLinus Torvalds 	md->queue->unplug_fn = dm_unplug_all;
10171da177e4SLinus Torvalds 
101893d2341cSMatthew Dobson 	md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
10191da177e4SLinus Torvalds 	if (!md->io_pool)
10201da177e4SLinus Torvalds 		goto bad2;
10211da177e4SLinus Torvalds 
102293d2341cSMatthew Dobson 	md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
10231da177e4SLinus Torvalds 	if (!md->tio_pool)
10241da177e4SLinus Torvalds 		goto bad3;
10251da177e4SLinus Torvalds 
10265972511bSJens Axboe 	md->bs = bioset_create(16, 16);
10279faf400fSStefan Bader 	if (!md->bs)
10289faf400fSStefan Bader 		goto bad_no_bioset;
10299faf400fSStefan Bader 
10301da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
10311da177e4SLinus Torvalds 	if (!md->disk)
10321da177e4SLinus Torvalds 		goto bad4;
10331da177e4SLinus Torvalds 
1034f0b04115SJeff Mahoney 	atomic_set(&md->pending, 0);
1035f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
1036f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
1037f0b04115SJeff Mahoney 
10381da177e4SLinus Torvalds 	md->disk->major = _major;
10391da177e4SLinus Torvalds 	md->disk->first_minor = minor;
10401da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
10411da177e4SLinus Torvalds 	md->disk->queue = md->queue;
10421da177e4SLinus Torvalds 	md->disk->private_data = md;
10431da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
10441da177e4SLinus Torvalds 	add_disk(md->disk);
10457e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
10461da177e4SLinus Torvalds 
1047ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1048f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1049ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1050f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1051ba61fdd1SJeff Mahoney 
1052ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1053ba61fdd1SJeff Mahoney 
10541da177e4SLinus Torvalds 	return md;
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds  bad4:
10579faf400fSStefan Bader 	bioset_free(md->bs);
10589faf400fSStefan Bader  bad_no_bioset:
10591da177e4SLinus Torvalds 	mempool_destroy(md->tio_pool);
10601da177e4SLinus Torvalds  bad3:
10611da177e4SLinus Torvalds 	mempool_destroy(md->io_pool);
10621da177e4SLinus Torvalds  bad2:
10631312f40eSAl Viro 	blk_cleanup_queue(md->queue);
1064e3f6ac61SIshai Rabinovitz  bad1_free_minor:
10651da177e4SLinus Torvalds 	free_minor(minor);
10661da177e4SLinus Torvalds  bad1:
106710da4f79SJeff Mahoney 	module_put(THIS_MODULE);
106810da4f79SJeff Mahoney  bad0:
10691da177e4SLinus Torvalds 	kfree(md);
10701da177e4SLinus Torvalds 	return NULL;
10711da177e4SLinus Torvalds }
10721da177e4SLinus Torvalds 
1073ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1074ae9da83fSJun'ichi Nomura 
10751da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
10761da177e4SLinus Torvalds {
10772b06cfffSAlasdair G Kergon 	int minor = md->disk->first_minor;
107863d94e48SJun'ichi Nomura 
1079d9dde59bSJun'ichi Nomura 	if (md->suspended_bdev) {
1080ae9da83fSJun'ichi Nomura 		unlock_fs(md);
1081d9dde59bSJun'ichi Nomura 		bdput(md->suspended_bdev);
1082d9dde59bSJun'ichi Nomura 	}
10831da177e4SLinus Torvalds 	mempool_destroy(md->tio_pool);
10841da177e4SLinus Torvalds 	mempool_destroy(md->io_pool);
10859faf400fSStefan Bader 	bioset_free(md->bs);
10861da177e4SLinus Torvalds 	del_gendisk(md->disk);
108763d94e48SJun'ichi Nomura 	free_minor(minor);
1088fba9f90eSJeff Mahoney 
1089fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
1090fba9f90eSJeff Mahoney 	md->disk->private_data = NULL;
1091fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
1092fba9f90eSJeff Mahoney 
10931da177e4SLinus Torvalds 	put_disk(md->disk);
10941312f40eSAl Viro 	blk_cleanup_queue(md->queue);
109510da4f79SJeff Mahoney 	module_put(THIS_MODULE);
10961da177e4SLinus Torvalds 	kfree(md);
10971da177e4SLinus Torvalds }
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds /*
11001da177e4SLinus Torvalds  * Bind a table to the device.
11011da177e4SLinus Torvalds  */
11021da177e4SLinus Torvalds static void event_callback(void *context)
11031da177e4SLinus Torvalds {
11047a8c3d3bSMike Anderson 	unsigned long flags;
11057a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
11061da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
11071da177e4SLinus Torvalds 
11087a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
11097a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
11107a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
11117a8c3d3bSMike Anderson 
1112*edfaa7c3SKay Sievers 	dm_send_uevents(&uevents, &md->disk->dev.kobj);
11137a8c3d3bSMike Anderson 
11141da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
11151da177e4SLinus Torvalds 	wake_up(&md->eventq);
11161da177e4SLinus Torvalds }
11171da177e4SLinus Torvalds 
11184e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
11191da177e4SLinus Torvalds {
11204e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
11211da177e4SLinus Torvalds 
11221b1dcc1bSJes Sorensen 	mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
1123e39e2e95SAlasdair G Kergon 	i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
11241b1dcc1bSJes Sorensen 	mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
11251da177e4SLinus Torvalds }
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds static int __bind(struct mapped_device *md, struct dm_table *t)
11281da177e4SLinus Torvalds {
1129165125e1SJens Axboe 	struct request_queue *q = md->queue;
11301da177e4SLinus Torvalds 	sector_t size;
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 	size = dm_table_get_size(t);
11333ac51e74SDarrick J. Wong 
11343ac51e74SDarrick J. Wong 	/*
11353ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
11363ac51e74SDarrick J. Wong 	 */
11373ac51e74SDarrick J. Wong 	if (size != get_capacity(md->disk))
11383ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
11393ac51e74SDarrick J. Wong 
1140bfa152faSJun'ichi Nomura 	if (md->suspended_bdev)
11414e90188bSAlasdair G Kergon 		__set_size(md, size);
11421da177e4SLinus Torvalds 	if (size == 0)
11431da177e4SLinus Torvalds 		return 0;
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 	dm_table_get(t);
1146cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
11472ca3310eSAlasdair G Kergon 
11482ca3310eSAlasdair G Kergon 	write_lock(&md->map_lock);
11492ca3310eSAlasdair G Kergon 	md->map = t;
11501da177e4SLinus Torvalds 	dm_table_set_restrictions(t, q);
11512ca3310eSAlasdair G Kergon 	write_unlock(&md->map_lock);
11522ca3310eSAlasdair G Kergon 
11531da177e4SLinus Torvalds 	return 0;
11541da177e4SLinus Torvalds }
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds static void __unbind(struct mapped_device *md)
11571da177e4SLinus Torvalds {
11581da177e4SLinus Torvalds 	struct dm_table *map = md->map;
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	if (!map)
11611da177e4SLinus Torvalds 		return;
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
11641da177e4SLinus Torvalds 	write_lock(&md->map_lock);
11651da177e4SLinus Torvalds 	md->map = NULL;
11661da177e4SLinus Torvalds 	write_unlock(&md->map_lock);
11671da177e4SLinus Torvalds 	dm_table_put(map);
11681da177e4SLinus Torvalds }
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds /*
11711da177e4SLinus Torvalds  * Constructor for a new device.
11721da177e4SLinus Torvalds  */
11732b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
11741da177e4SLinus Torvalds {
11751da177e4SLinus Torvalds 	struct mapped_device *md;
11761da177e4SLinus Torvalds 
11772b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
11781da177e4SLinus Torvalds 	if (!md)
11791da177e4SLinus Torvalds 		return -ENXIO;
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	*result = md;
11821da177e4SLinus Torvalds 	return 0;
11831da177e4SLinus Torvalds }
11841da177e4SLinus Torvalds 
1185637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev)
11861da177e4SLinus Torvalds {
11871da177e4SLinus Torvalds 	struct mapped_device *md;
11881da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
11911da177e4SLinus Torvalds 		return NULL;
11921da177e4SLinus Torvalds 
1193f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
1196fba9f90eSJeff Mahoney 	if (md && (md == MINOR_ALLOCED ||
1197fba9f90eSJeff Mahoney 		   (dm_disk(md)->first_minor != minor) ||
1198fba9f90eSJeff Mahoney 		   test_bit(DMF_FREEING, &md->flags))) {
1199637842cfSDavid Teigland 		md = NULL;
1200fba9f90eSJeff Mahoney 		goto out;
1201fba9f90eSJeff Mahoney 	}
12021da177e4SLinus Torvalds 
1203fba9f90eSJeff Mahoney out:
1204f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
12051da177e4SLinus Torvalds 
1206637842cfSDavid Teigland 	return md;
1207637842cfSDavid Teigland }
1208637842cfSDavid Teigland 
1209d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev)
1210d229a958SDavid Teigland {
1211d229a958SDavid Teigland 	struct mapped_device *md = dm_find_md(dev);
1212d229a958SDavid Teigland 
1213d229a958SDavid Teigland 	if (md)
1214d229a958SDavid Teigland 		dm_get(md);
1215d229a958SDavid Teigland 
1216d229a958SDavid Teigland 	return md;
1217d229a958SDavid Teigland }
1218d229a958SDavid Teigland 
12199ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
1220637842cfSDavid Teigland {
12219ade92a9SAlasdair G Kergon 	return md->interface_ptr;
12221da177e4SLinus Torvalds }
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
12251da177e4SLinus Torvalds {
12261da177e4SLinus Torvalds 	md->interface_ptr = ptr;
12271da177e4SLinus Torvalds }
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
12301da177e4SLinus Torvalds {
12311da177e4SLinus Torvalds 	atomic_inc(&md->holders);
12321da177e4SLinus Torvalds }
12331da177e4SLinus Torvalds 
123472d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
123572d94861SAlasdair G Kergon {
123672d94861SAlasdair G Kergon 	return md->name;
123772d94861SAlasdair G Kergon }
123872d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
123972d94861SAlasdair G Kergon 
12401da177e4SLinus Torvalds void dm_put(struct mapped_device *md)
12411da177e4SLinus Torvalds {
12421134e5aeSMike Anderson 	struct dm_table *map;
12431da177e4SLinus Torvalds 
1244fba9f90eSJeff Mahoney 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
1245fba9f90eSJeff Mahoney 
1246f32c10b0SJeff Mahoney 	if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
12471134e5aeSMike Anderson 		map = dm_get_table(md);
1248ba61fdd1SJeff Mahoney 		idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
1249fba9f90eSJeff Mahoney 		set_bit(DMF_FREEING, &md->flags);
1250f32c10b0SJeff Mahoney 		spin_unlock(&_minor_lock);
1251cf222b37SAlasdair G Kergon 		if (!dm_suspended(md)) {
12521da177e4SLinus Torvalds 			dm_table_presuspend_targets(map);
12531da177e4SLinus Torvalds 			dm_table_postsuspend_targets(map);
12541da177e4SLinus Torvalds 		}
12551da177e4SLinus Torvalds 		__unbind(md);
12561134e5aeSMike Anderson 		dm_table_put(map);
12571da177e4SLinus Torvalds 		free_dev(md);
12581da177e4SLinus Torvalds 	}
12591da177e4SLinus Torvalds }
126079eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds /*
12631da177e4SLinus Torvalds  * Process the deferred bios
12641da177e4SLinus Torvalds  */
12651da177e4SLinus Torvalds static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
12661da177e4SLinus Torvalds {
12671da177e4SLinus Torvalds 	struct bio *n;
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 	while (c) {
12701da177e4SLinus Torvalds 		n = c->bi_next;
12711da177e4SLinus Torvalds 		c->bi_next = NULL;
12729e4e5f87SMilan Broz 		if (__split_bio(md, c))
12739e4e5f87SMilan Broz 			bio_io_error(c);
12741da177e4SLinus Torvalds 		c = n;
12751da177e4SLinus Torvalds 	}
12761da177e4SLinus Torvalds }
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds /*
12791da177e4SLinus Torvalds  * Swap in a new table (destroying old one).
12801da177e4SLinus Torvalds  */
12811da177e4SLinus Torvalds int dm_swap_table(struct mapped_device *md, struct dm_table *table)
12821da177e4SLinus Torvalds {
128393c534aeSAlasdair G Kergon 	int r = -EINVAL;
12841da177e4SLinus Torvalds 
12852ca3310eSAlasdair G Kergon 	down(&md->suspend_lock);
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 	/* device must be suspended */
1288cf222b37SAlasdair G Kergon 	if (!dm_suspended(md))
128993c534aeSAlasdair G Kergon 		goto out;
12901da177e4SLinus Torvalds 
1291bfa152faSJun'ichi Nomura 	/* without bdev, the device size cannot be changed */
1292bfa152faSJun'ichi Nomura 	if (!md->suspended_bdev)
1293bfa152faSJun'ichi Nomura 		if (get_capacity(md->disk) != dm_table_get_size(table))
1294bfa152faSJun'ichi Nomura 			goto out;
1295bfa152faSJun'ichi Nomura 
12961da177e4SLinus Torvalds 	__unbind(md);
12971da177e4SLinus Torvalds 	r = __bind(md, table);
12981da177e4SLinus Torvalds 
129993c534aeSAlasdair G Kergon out:
13002ca3310eSAlasdair G Kergon 	up(&md->suspend_lock);
130193c534aeSAlasdair G Kergon 	return r;
13021da177e4SLinus Torvalds }
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds /*
13051da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
13061da177e4SLinus Torvalds  * device.
13071da177e4SLinus Torvalds  */
13082ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
13091da177e4SLinus Torvalds {
1310e39e2e95SAlasdair G Kergon 	int r;
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
1313dfbe03f6SAlasdair G Kergon 
1314e39e2e95SAlasdair G Kergon 	md->frozen_sb = freeze_bdev(md->suspended_bdev);
1315dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
1316cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
1317e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
1318e39e2e95SAlasdair G Kergon 		return r;
1319dfbe03f6SAlasdair G Kergon 	}
1320dfbe03f6SAlasdair G Kergon 
1321aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
1322aa8d7c2fSAlasdair G Kergon 
13231da177e4SLinus Torvalds 	/* don't bdput right now, we don't want the bdev
1324e39e2e95SAlasdair G Kergon 	 * to go away while it is locked.
13251da177e4SLinus Torvalds 	 */
13261da177e4SLinus Torvalds 	return 0;
13271da177e4SLinus Torvalds }
13281da177e4SLinus Torvalds 
13292ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
13301da177e4SLinus Torvalds {
1331aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
1332aa8d7c2fSAlasdair G Kergon 		return;
1333aa8d7c2fSAlasdair G Kergon 
1334e39e2e95SAlasdair G Kergon 	thaw_bdev(md->suspended_bdev, md->frozen_sb);
13351da177e4SLinus Torvalds 	md->frozen_sb = NULL;
1336aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
13371da177e4SLinus Torvalds }
13381da177e4SLinus Torvalds 
13391da177e4SLinus Torvalds /*
13401da177e4SLinus Torvalds  * We need to be able to change a mapping table under a mounted
13411da177e4SLinus Torvalds  * filesystem.  For example we might want to move some data in
13421da177e4SLinus Torvalds  * the background.  Before the table can be swapped with
13431da177e4SLinus Torvalds  * dm_bind_table, dm_suspend must be called to flush any in
13441da177e4SLinus Torvalds  * flight bios and ensure that any further io gets deferred.
13451da177e4SLinus Torvalds  */
1346a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
13471da177e4SLinus Torvalds {
13482ca3310eSAlasdair G Kergon 	struct dm_table *map = NULL;
13492e93ccc1SKiyoshi Ueda 	unsigned long flags;
13501da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
13511ecac7fdSJun'ichi Nomura 	struct bio *def;
1352cf222b37SAlasdair G Kergon 	int r = -EINVAL;
1353a3d77d35SKiyoshi Ueda 	int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
13542e93ccc1SKiyoshi Ueda 	int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
13551da177e4SLinus Torvalds 
13562ca3310eSAlasdair G Kergon 	down(&md->suspend_lock);
13572ca3310eSAlasdair G Kergon 
13582ca3310eSAlasdair G Kergon 	if (dm_suspended(md))
1359d287483dSAlasdair G Kergon 		goto out_unlock;
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds 	map = dm_get_table(md);
1362cf222b37SAlasdair G Kergon 
13632e93ccc1SKiyoshi Ueda 	/*
13642e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
13652e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
13662e93ccc1SKiyoshi Ueda 	 */
13672e93ccc1SKiyoshi Ueda 	if (noflush)
13682e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
13692e93ccc1SKiyoshi Ueda 
1370436d4108SAlasdair G Kergon 	/* This does not get reverted if there's an error later. */
13711da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
13721da177e4SLinus Torvalds 
1373bfa152faSJun'ichi Nomura 	/* bdget() can stall if the pending I/Os are not flushed */
1374bfa152faSJun'ichi Nomura 	if (!noflush) {
1375e39e2e95SAlasdair G Kergon 		md->suspended_bdev = bdget_disk(md->disk, 0);
1376e39e2e95SAlasdair G Kergon 		if (!md->suspended_bdev) {
1377e39e2e95SAlasdair G Kergon 			DMWARN("bdget failed in dm_suspend");
1378e39e2e95SAlasdair G Kergon 			r = -ENOMEM;
13792e93ccc1SKiyoshi Ueda 			goto flush_and_out;
1380e39e2e95SAlasdair G Kergon 		}
1381bfa152faSJun'ichi Nomura 	}
1382e39e2e95SAlasdair G Kergon 
13832e93ccc1SKiyoshi Ueda 	/*
13842e93ccc1SKiyoshi Ueda 	 * Flush I/O to the device.
13852e93ccc1SKiyoshi Ueda 	 * noflush supersedes do_lockfs, because lock_fs() needs to flush I/Os.
13862e93ccc1SKiyoshi Ueda 	 */
13872e93ccc1SKiyoshi Ueda 	if (do_lockfs && !noflush) {
13882ca3310eSAlasdair G Kergon 		r = lock_fs(md);
13892ca3310eSAlasdair G Kergon 		if (r)
13902ca3310eSAlasdair G Kergon 			goto out;
1391aa8d7c2fSAlasdair G Kergon 	}
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	/*
1394354e0071SAlasdair G Kergon 	 * First we set the BLOCK_IO flag so no more ios will be mapped.
13951da177e4SLinus Torvalds 	 */
13962ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
13972ca3310eSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO, &md->flags);
13981da177e4SLinus Torvalds 
13991da177e4SLinus Torvalds 	add_wait_queue(&md->wait, &wait);
14002ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 	/* unplug */
14032ca3310eSAlasdair G Kergon 	if (map)
14041da177e4SLinus Torvalds 		dm_table_unplug_all(map);
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds 	/*
14071da177e4SLinus Torvalds 	 * Then we wait for the already mapped ios to
14081da177e4SLinus Torvalds 	 * complete.
14091da177e4SLinus Torvalds 	 */
14101da177e4SLinus Torvalds 	while (1) {
14111da177e4SLinus Torvalds 		set_current_state(TASK_INTERRUPTIBLE);
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds 		if (!atomic_read(&md->pending) || signal_pending(current))
14141da177e4SLinus Torvalds 			break;
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds 		io_schedule();
14171da177e4SLinus Torvalds 	}
14181da177e4SLinus Torvalds 	set_current_state(TASK_RUNNING);
14191da177e4SLinus Torvalds 
14202ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
14211da177e4SLinus Torvalds 	remove_wait_queue(&md->wait, &wait);
14221da177e4SLinus Torvalds 
14232e93ccc1SKiyoshi Ueda 	if (noflush) {
14242e93ccc1SKiyoshi Ueda 		spin_lock_irqsave(&md->pushback_lock, flags);
14252e93ccc1SKiyoshi Ueda 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
14262e93ccc1SKiyoshi Ueda 		bio_list_merge_head(&md->deferred, &md->pushback);
14272e93ccc1SKiyoshi Ueda 		bio_list_init(&md->pushback);
14282e93ccc1SKiyoshi Ueda 		spin_unlock_irqrestore(&md->pushback_lock, flags);
14292e93ccc1SKiyoshi Ueda 	}
14302e93ccc1SKiyoshi Ueda 
14311da177e4SLinus Torvalds 	/* were we interrupted ? */
1432cf222b37SAlasdair G Kergon 	r = -EINTR;
14332ca3310eSAlasdair G Kergon 	if (atomic_read(&md->pending)) {
14341ecac7fdSJun'ichi Nomura 		clear_bit(DMF_BLOCK_IO, &md->flags);
14351ecac7fdSJun'ichi Nomura 		def = bio_list_get(&md->deferred);
14361ecac7fdSJun'ichi Nomura 		__flush_deferred_io(md, def);
14372ca3310eSAlasdair G Kergon 		up_write(&md->io_lock);
14382ca3310eSAlasdair G Kergon 		unlock_fs(md);
14392e93ccc1SKiyoshi Ueda 		goto out; /* pushback list is already flushed, so skip flush */
14402ca3310eSAlasdair G Kergon 	}
14412ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
14422ca3310eSAlasdair G Kergon 
14432ca3310eSAlasdair G Kergon 	dm_table_postsuspend_targets(map);
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
14461da177e4SLinus Torvalds 
14472ca3310eSAlasdair G Kergon 	r = 0;
14482ca3310eSAlasdair G Kergon 
14492e93ccc1SKiyoshi Ueda flush_and_out:
14502e93ccc1SKiyoshi Ueda 	if (r && noflush) {
14512e93ccc1SKiyoshi Ueda 		/*
14522e93ccc1SKiyoshi Ueda 		 * Because there may be already I/Os in the pushback list,
14532e93ccc1SKiyoshi Ueda 		 * flush them before return.
14542e93ccc1SKiyoshi Ueda 		 */
14552e93ccc1SKiyoshi Ueda 		down_write(&md->io_lock);
14562e93ccc1SKiyoshi Ueda 
14572e93ccc1SKiyoshi Ueda 		spin_lock_irqsave(&md->pushback_lock, flags);
14582e93ccc1SKiyoshi Ueda 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
14592e93ccc1SKiyoshi Ueda 		bio_list_merge_head(&md->deferred, &md->pushback);
14602e93ccc1SKiyoshi Ueda 		bio_list_init(&md->pushback);
14612e93ccc1SKiyoshi Ueda 		spin_unlock_irqrestore(&md->pushback_lock, flags);
14622e93ccc1SKiyoshi Ueda 
14632e93ccc1SKiyoshi Ueda 		def = bio_list_get(&md->deferred);
14642e93ccc1SKiyoshi Ueda 		__flush_deferred_io(md, def);
14652e93ccc1SKiyoshi Ueda 		up_write(&md->io_lock);
14662e93ccc1SKiyoshi Ueda 	}
14672e93ccc1SKiyoshi Ueda 
14682ca3310eSAlasdair G Kergon out:
1469e39e2e95SAlasdair G Kergon 	if (r && md->suspended_bdev) {
1470e39e2e95SAlasdair G Kergon 		bdput(md->suspended_bdev);
1471e39e2e95SAlasdair G Kergon 		md->suspended_bdev = NULL;
1472e39e2e95SAlasdair G Kergon 	}
1473e39e2e95SAlasdair G Kergon 
14741da177e4SLinus Torvalds 	dm_table_put(map);
1475d287483dSAlasdair G Kergon 
1476d287483dSAlasdair G Kergon out_unlock:
14772ca3310eSAlasdair G Kergon 	up(&md->suspend_lock);
1478cf222b37SAlasdair G Kergon 	return r;
14791da177e4SLinus Torvalds }
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds int dm_resume(struct mapped_device *md)
14821da177e4SLinus Torvalds {
1483cf222b37SAlasdair G Kergon 	int r = -EINVAL;
14841da177e4SLinus Torvalds 	struct bio *def;
1485cf222b37SAlasdair G Kergon 	struct dm_table *map = NULL;
14861da177e4SLinus Torvalds 
14872ca3310eSAlasdair G Kergon 	down(&md->suspend_lock);
14882ca3310eSAlasdair G Kergon 	if (!dm_suspended(md))
1489cf222b37SAlasdair G Kergon 		goto out;
1490cf222b37SAlasdair G Kergon 
1491cf222b37SAlasdair G Kergon 	map = dm_get_table(md);
14922ca3310eSAlasdair G Kergon 	if (!map || !dm_table_get_size(map))
1493cf222b37SAlasdair G Kergon 		goto out;
14941da177e4SLinus Torvalds 
14958757b776SMilan Broz 	r = dm_table_resume_targets(map);
14968757b776SMilan Broz 	if (r)
14978757b776SMilan Broz 		goto out;
14982ca3310eSAlasdair G Kergon 
14992ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
15001da177e4SLinus Torvalds 	clear_bit(DMF_BLOCK_IO, &md->flags);
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds 	def = bio_list_get(&md->deferred);
15031da177e4SLinus Torvalds 	__flush_deferred_io(md, def);
15042ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
15052ca3310eSAlasdair G Kergon 
15062ca3310eSAlasdair G Kergon 	unlock_fs(md);
15072ca3310eSAlasdair G Kergon 
1508bfa152faSJun'ichi Nomura 	if (md->suspended_bdev) {
1509e39e2e95SAlasdair G Kergon 		bdput(md->suspended_bdev);
1510e39e2e95SAlasdair G Kergon 		md->suspended_bdev = NULL;
1511bfa152faSJun'ichi Nomura 	}
1512e39e2e95SAlasdair G Kergon 
15132ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
15142ca3310eSAlasdair G Kergon 
15151da177e4SLinus Torvalds 	dm_table_unplug_all(map);
15161da177e4SLinus Torvalds 
151769267a30SAlasdair G Kergon 	dm_kobject_uevent(md);
15188560ed6fSHannes Reinecke 
1519cf222b37SAlasdair G Kergon 	r = 0;
15202ca3310eSAlasdair G Kergon 
1521cf222b37SAlasdair G Kergon out:
1522cf222b37SAlasdair G Kergon 	dm_table_put(map);
15232ca3310eSAlasdair G Kergon 	up(&md->suspend_lock);
15242ca3310eSAlasdair G Kergon 
1525cf222b37SAlasdair G Kergon 	return r;
15261da177e4SLinus Torvalds }
15271da177e4SLinus Torvalds 
15281da177e4SLinus Torvalds /*-----------------------------------------------------------------
15291da177e4SLinus Torvalds  * Event notification.
15301da177e4SLinus Torvalds  *---------------------------------------------------------------*/
153169267a30SAlasdair G Kergon void dm_kobject_uevent(struct mapped_device *md)
153269267a30SAlasdair G Kergon {
1533*edfaa7c3SKay Sievers 	kobject_uevent(&md->disk->dev.kobj, KOBJ_CHANGE);
153469267a30SAlasdair G Kergon }
153569267a30SAlasdair G Kergon 
15367a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
15377a8c3d3bSMike Anderson {
15387a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
15397a8c3d3bSMike Anderson }
15407a8c3d3bSMike Anderson 
15411da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
15421da177e4SLinus Torvalds {
15431da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
15441da177e4SLinus Torvalds }
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
15471da177e4SLinus Torvalds {
15481da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
15491da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
15501da177e4SLinus Torvalds }
15511da177e4SLinus Torvalds 
15527a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
15537a8c3d3bSMike Anderson {
15547a8c3d3bSMike Anderson 	unsigned long flags;
15557a8c3d3bSMike Anderson 
15567a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
15577a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
15587a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
15597a8c3d3bSMike Anderson }
15607a8c3d3bSMike Anderson 
15611da177e4SLinus Torvalds /*
15621da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
15631da177e4SLinus Torvalds  * count on 'md'.
15641da177e4SLinus Torvalds  */
15651da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
15661da177e4SLinus Torvalds {
15671da177e4SLinus Torvalds 	return md->disk;
15681da177e4SLinus Torvalds }
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds int dm_suspended(struct mapped_device *md)
15711da177e4SLinus Torvalds {
15721da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
15731da177e4SLinus Torvalds }
15741da177e4SLinus Torvalds 
15752e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
15762e93ccc1SKiyoshi Ueda {
15772e93ccc1SKiyoshi Ueda 	struct mapped_device *md = dm_table_get_md(ti->table);
15782e93ccc1SKiyoshi Ueda 	int r = __noflush_suspending(md);
15792e93ccc1SKiyoshi Ueda 
15802e93ccc1SKiyoshi Ueda 	dm_put(md);
15812e93ccc1SKiyoshi Ueda 
15822e93ccc1SKiyoshi Ueda 	return r;
15832e93ccc1SKiyoshi Ueda }
15842e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
15852e93ccc1SKiyoshi Ueda 
15861da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops = {
15871da177e4SLinus Torvalds 	.open = dm_blk_open,
15881da177e4SLinus Torvalds 	.release = dm_blk_close,
1589aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
15903ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
15911da177e4SLinus Torvalds 	.owner = THIS_MODULE
15921da177e4SLinus Torvalds };
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo);
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds /*
15971da177e4SLinus Torvalds  * module hooks
15981da177e4SLinus Torvalds  */
15991da177e4SLinus Torvalds module_init(dm_init);
16001da177e4SLinus Torvalds module_exit(dm_exit);
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds module_param(major, uint, 0);
16031da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
16041da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
16051da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
16061da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1607