xref: /openbmc/linux/drivers/md/dm.c (revision fdb9572b73abd008b80931c3b1f157dac3888bb9)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3784aae73SMilan Broz  * Copyright (C) 2004-2008 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"
951e5b2bdSMike Anderson #include "dm-uevent.h"
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/init.h>
121da177e4SLinus Torvalds #include <linux/module.h>
1348c9c27bSArjan van de Ven #include <linux/mutex.h>
141da177e4SLinus Torvalds #include <linux/moduleparam.h>
151da177e4SLinus Torvalds #include <linux/blkpg.h>
161da177e4SLinus Torvalds #include <linux/bio.h>
171da177e4SLinus Torvalds #include <linux/buffer_head.h>
181da177e4SLinus Torvalds #include <linux/mempool.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/idr.h>
213ac51e74SDarrick J. Wong #include <linux/hdreg.h>
2255782138SLi Zefan 
2355782138SLi Zefan #include <trace/events/block.h>
241da177e4SLinus Torvalds 
2572d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core"
2672d94861SAlasdair G Kergon 
271da177e4SLinus Torvalds static const char *_name = DM_NAME;
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds static unsigned int major = 0;
301da177e4SLinus Torvalds static unsigned int _major = 0;
311da177e4SLinus Torvalds 
32f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock);
331da177e4SLinus Torvalds /*
348fbf26adSKiyoshi Ueda  * For bio-based dm.
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 	atomic_t io_count;
416ae2fa67SRichard Kennedy 	struct bio *bio;
423eaf840eSJun'ichi "Nick" Nomura 	unsigned long start_time;
431da177e4SLinus Torvalds };
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds /*
468fbf26adSKiyoshi Ueda  * For bio-based dm.
471da177e4SLinus Torvalds  * One of these is allocated per target within a bio.  Hopefully
481da177e4SLinus Torvalds  * this will be simplified out one day.
491da177e4SLinus Torvalds  */
50028867acSAlasdair G Kergon struct dm_target_io {
511da177e4SLinus Torvalds 	struct dm_io *io;
521da177e4SLinus Torvalds 	struct dm_target *ti;
531da177e4SLinus Torvalds 	union map_info info;
541da177e4SLinus Torvalds };
551da177e4SLinus Torvalds 
568fbf26adSKiyoshi Ueda /*
578fbf26adSKiyoshi Ueda  * For request-based dm.
588fbf26adSKiyoshi Ueda  * One of these is allocated per request.
598fbf26adSKiyoshi Ueda  */
608fbf26adSKiyoshi Ueda struct dm_rq_target_io {
618fbf26adSKiyoshi Ueda 	struct mapped_device *md;
628fbf26adSKiyoshi Ueda 	struct dm_target *ti;
638fbf26adSKiyoshi Ueda 	struct request *orig, clone;
648fbf26adSKiyoshi Ueda 	int error;
658fbf26adSKiyoshi Ueda 	union map_info info;
668fbf26adSKiyoshi Ueda };
678fbf26adSKiyoshi Ueda 
688fbf26adSKiyoshi Ueda /*
698fbf26adSKiyoshi Ueda  * For request-based dm.
708fbf26adSKiyoshi Ueda  * One of these is allocated per bio.
718fbf26adSKiyoshi Ueda  */
728fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info {
738fbf26adSKiyoshi Ueda 	struct bio *orig;
748fbf26adSKiyoshi Ueda 	struct request *rq;
758fbf26adSKiyoshi Ueda };
768fbf26adSKiyoshi Ueda 
771da177e4SLinus Torvalds union map_info *dm_get_mapinfo(struct bio *bio)
781da177e4SLinus Torvalds {
791da177e4SLinus Torvalds 	if (bio && bio->bi_private)
80028867acSAlasdair G Kergon 		return &((struct dm_target_io *)bio->bi_private)->info;
811da177e4SLinus Torvalds 	return NULL;
821da177e4SLinus Torvalds }
831da177e4SLinus Torvalds 
84ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1)
85ba61fdd1SJeff Mahoney 
861da177e4SLinus Torvalds /*
871da177e4SLinus Torvalds  * Bits for the md->flags field.
881da177e4SLinus Torvalds  */
891eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0
901da177e4SLinus Torvalds #define DMF_SUSPENDED 1
91aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2
92fba9f90eSJeff Mahoney #define DMF_FREEING 3
935c6bd75dSAlasdair G Kergon #define DMF_DELETING 4
942e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5
951eb787ecSAlasdair G Kergon #define DMF_QUEUE_IO_TO_THREAD 6
961da177e4SLinus Torvalds 
97304f3f6aSMilan Broz /*
98304f3f6aSMilan Broz  * Work processed by per-device workqueue.
99304f3f6aSMilan Broz  */
1001da177e4SLinus Torvalds struct mapped_device {
1012ca3310eSAlasdair G Kergon 	struct rw_semaphore io_lock;
102e61290a4SDaniel Walker 	struct mutex suspend_lock;
1031da177e4SLinus Torvalds 	rwlock_t map_lock;
1041da177e4SLinus Torvalds 	atomic_t holders;
1055c6bd75dSAlasdair G Kergon 	atomic_t open_count;
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	unsigned long flags;
1081da177e4SLinus Torvalds 
109165125e1SJens Axboe 	struct request_queue *queue;
1101da177e4SLinus Torvalds 	struct gendisk *disk;
1117e51f257SMike Anderson 	char name[16];
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	void *interface_ptr;
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	/*
1161da177e4SLinus Torvalds 	 * A list of ios that arrived while we were suspended.
1171da177e4SLinus Torvalds 	 */
1181da177e4SLinus Torvalds 	atomic_t pending;
1191da177e4SLinus Torvalds 	wait_queue_head_t wait;
12053d5914fSMikulas Patocka 	struct work_struct work;
1211da177e4SLinus Torvalds 	struct bio_list deferred;
122022c2611SMikulas Patocka 	spinlock_t deferred_lock;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	/*
125af7e466aSMikulas Patocka 	 * An error from the barrier request currently being processed.
126af7e466aSMikulas Patocka 	 */
127af7e466aSMikulas Patocka 	int barrier_error;
128af7e466aSMikulas Patocka 
129af7e466aSMikulas Patocka 	/*
130304f3f6aSMilan Broz 	 * Processing queue (flush/barriers)
131304f3f6aSMilan Broz 	 */
132304f3f6aSMilan Broz 	struct workqueue_struct *wq;
133304f3f6aSMilan Broz 
134304f3f6aSMilan Broz 	/*
1351da177e4SLinus Torvalds 	 * The current mapping.
1361da177e4SLinus Torvalds 	 */
1371da177e4SLinus Torvalds 	struct dm_table *map;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	/*
1401da177e4SLinus Torvalds 	 * io objects are allocated from here.
1411da177e4SLinus Torvalds 	 */
1421da177e4SLinus Torvalds 	mempool_t *io_pool;
1431da177e4SLinus Torvalds 	mempool_t *tio_pool;
1441da177e4SLinus Torvalds 
1459faf400fSStefan Bader 	struct bio_set *bs;
1469faf400fSStefan Bader 
1471da177e4SLinus Torvalds 	/*
1481da177e4SLinus Torvalds 	 * Event handling.
1491da177e4SLinus Torvalds 	 */
1501da177e4SLinus Torvalds 	atomic_t event_nr;
1511da177e4SLinus Torvalds 	wait_queue_head_t eventq;
1527a8c3d3bSMike Anderson 	atomic_t uevent_seq;
1537a8c3d3bSMike Anderson 	struct list_head uevent_list;
1547a8c3d3bSMike Anderson 	spinlock_t uevent_lock; /* Protect access to uevent_list */
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	/*
1571da177e4SLinus Torvalds 	 * freeze/thaw support require holding onto a super block
1581da177e4SLinus Torvalds 	 */
1591da177e4SLinus Torvalds 	struct super_block *frozen_sb;
160db8fef4fSMikulas Patocka 	struct block_device *bdev;
1613ac51e74SDarrick J. Wong 
1623ac51e74SDarrick J. Wong 	/* forced geometry settings */
1633ac51e74SDarrick J. Wong 	struct hd_geometry geometry;
164784aae73SMilan Broz 
165784aae73SMilan Broz 	/* sysfs handle */
166784aae73SMilan Broz 	struct kobject kobj;
1671da177e4SLinus Torvalds };
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds #define MIN_IOS 256
170e18b890bSChristoph Lameter static struct kmem_cache *_io_cache;
171e18b890bSChristoph Lameter static struct kmem_cache *_tio_cache;
1728fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache;
1738fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_bio_info_cache;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds static int __init local_init(void)
1761da177e4SLinus Torvalds {
17751157b4aSKiyoshi Ueda 	int r = -ENOMEM;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
180028867acSAlasdair G Kergon 	_io_cache = KMEM_CACHE(dm_io, 0);
1811da177e4SLinus Torvalds 	if (!_io_cache)
18251157b4aSKiyoshi Ueda 		return r;
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	/* allocate a slab for the target ios */
185028867acSAlasdair G Kergon 	_tio_cache = KMEM_CACHE(dm_target_io, 0);
18651157b4aSKiyoshi Ueda 	if (!_tio_cache)
18751157b4aSKiyoshi Ueda 		goto out_free_io_cache;
1881da177e4SLinus Torvalds 
1898fbf26adSKiyoshi Ueda 	_rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
1908fbf26adSKiyoshi Ueda 	if (!_rq_tio_cache)
1918fbf26adSKiyoshi Ueda 		goto out_free_tio_cache;
1928fbf26adSKiyoshi Ueda 
1938fbf26adSKiyoshi Ueda 	_rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
1948fbf26adSKiyoshi Ueda 	if (!_rq_bio_info_cache)
1958fbf26adSKiyoshi Ueda 		goto out_free_rq_tio_cache;
1968fbf26adSKiyoshi Ueda 
19751e5b2bdSMike Anderson 	r = dm_uevent_init();
19851157b4aSKiyoshi Ueda 	if (r)
1998fbf26adSKiyoshi Ueda 		goto out_free_rq_bio_info_cache;
20051e5b2bdSMike Anderson 
2011da177e4SLinus Torvalds 	_major = major;
2021da177e4SLinus Torvalds 	r = register_blkdev(_major, _name);
20351157b4aSKiyoshi Ueda 	if (r < 0)
20451157b4aSKiyoshi Ueda 		goto out_uevent_exit;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	if (!_major)
2071da177e4SLinus Torvalds 		_major = r;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	return 0;
21051157b4aSKiyoshi Ueda 
21151157b4aSKiyoshi Ueda out_uevent_exit:
21251157b4aSKiyoshi Ueda 	dm_uevent_exit();
2138fbf26adSKiyoshi Ueda out_free_rq_bio_info_cache:
2148fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_bio_info_cache);
2158fbf26adSKiyoshi Ueda out_free_rq_tio_cache:
2168fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
21751157b4aSKiyoshi Ueda out_free_tio_cache:
21851157b4aSKiyoshi Ueda 	kmem_cache_destroy(_tio_cache);
21951157b4aSKiyoshi Ueda out_free_io_cache:
22051157b4aSKiyoshi Ueda 	kmem_cache_destroy(_io_cache);
22151157b4aSKiyoshi Ueda 
22251157b4aSKiyoshi Ueda 	return r;
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds static void local_exit(void)
2261da177e4SLinus Torvalds {
2278fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_bio_info_cache);
2288fbf26adSKiyoshi Ueda 	kmem_cache_destroy(_rq_tio_cache);
2291da177e4SLinus Torvalds 	kmem_cache_destroy(_tio_cache);
2301da177e4SLinus Torvalds 	kmem_cache_destroy(_io_cache);
23100d59405SAkinobu Mita 	unregister_blkdev(_major, _name);
23251e5b2bdSMike Anderson 	dm_uevent_exit();
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	_major = 0;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	DMINFO("cleaned up");
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds 
239b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = {
2401da177e4SLinus Torvalds 	local_init,
2411da177e4SLinus Torvalds 	dm_target_init,
2421da177e4SLinus Torvalds 	dm_linear_init,
2431da177e4SLinus Torvalds 	dm_stripe_init,
244945fa4d2SMikulas Patocka 	dm_kcopyd_init,
2451da177e4SLinus Torvalds 	dm_interface_init,
2461da177e4SLinus Torvalds };
2471da177e4SLinus Torvalds 
248b9249e55SAlasdair G Kergon static void (*_exits[])(void) = {
2491da177e4SLinus Torvalds 	local_exit,
2501da177e4SLinus Torvalds 	dm_target_exit,
2511da177e4SLinus Torvalds 	dm_linear_exit,
2521da177e4SLinus Torvalds 	dm_stripe_exit,
253945fa4d2SMikulas Patocka 	dm_kcopyd_exit,
2541da177e4SLinus Torvalds 	dm_interface_exit,
2551da177e4SLinus Torvalds };
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds static int __init dm_init(void)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	const int count = ARRAY_SIZE(_inits);
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	int r, i;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
2641da177e4SLinus Torvalds 		r = _inits[i]();
2651da177e4SLinus Torvalds 		if (r)
2661da177e4SLinus Torvalds 			goto bad;
2671da177e4SLinus Torvalds 	}
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	return 0;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds       bad:
2721da177e4SLinus Torvalds 	while (i--)
2731da177e4SLinus Torvalds 		_exits[i]();
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	return r;
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds static void __exit dm_exit(void)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	int i = ARRAY_SIZE(_exits);
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	while (i--)
2831da177e4SLinus Torvalds 		_exits[i]();
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds /*
2871da177e4SLinus Torvalds  * Block device functions
2881da177e4SLinus Torvalds  */
289fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	struct mapped_device *md;
2921da177e4SLinus Torvalds 
293fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
294fba9f90eSJeff Mahoney 
295fe5f9f2cSAl Viro 	md = bdev->bd_disk->private_data;
296fba9f90eSJeff Mahoney 	if (!md)
297fba9f90eSJeff Mahoney 		goto out;
298fba9f90eSJeff Mahoney 
2995c6bd75dSAlasdair G Kergon 	if (test_bit(DMF_FREEING, &md->flags) ||
3005c6bd75dSAlasdair G Kergon 	    test_bit(DMF_DELETING, &md->flags)) {
301fba9f90eSJeff Mahoney 		md = NULL;
302fba9f90eSJeff Mahoney 		goto out;
303fba9f90eSJeff Mahoney 	}
304fba9f90eSJeff Mahoney 
3051da177e4SLinus Torvalds 	dm_get(md);
3065c6bd75dSAlasdair G Kergon 	atomic_inc(&md->open_count);
307fba9f90eSJeff Mahoney 
308fba9f90eSJeff Mahoney out:
309fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
310fba9f90eSJeff Mahoney 
311fba9f90eSJeff Mahoney 	return md ? 0 : -ENXIO;
3121da177e4SLinus Torvalds }
3131da177e4SLinus Torvalds 
314fe5f9f2cSAl Viro static int dm_blk_close(struct gendisk *disk, fmode_t mode)
3151da177e4SLinus Torvalds {
316fe5f9f2cSAl Viro 	struct mapped_device *md = disk->private_data;
3175c6bd75dSAlasdair G Kergon 	atomic_dec(&md->open_count);
3181da177e4SLinus Torvalds 	dm_put(md);
3191da177e4SLinus Torvalds 	return 0;
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds 
3225c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md)
3235c6bd75dSAlasdair G Kergon {
3245c6bd75dSAlasdair G Kergon 	return atomic_read(&md->open_count);
3255c6bd75dSAlasdair G Kergon }
3265c6bd75dSAlasdair G Kergon 
3275c6bd75dSAlasdair G Kergon /*
3285c6bd75dSAlasdair G Kergon  * Guarantees nothing is using the device before it's deleted.
3295c6bd75dSAlasdair G Kergon  */
3305c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md)
3315c6bd75dSAlasdair G Kergon {
3325c6bd75dSAlasdair G Kergon 	int r = 0;
3335c6bd75dSAlasdair G Kergon 
3345c6bd75dSAlasdair G Kergon 	spin_lock(&_minor_lock);
3355c6bd75dSAlasdair G Kergon 
3365c6bd75dSAlasdair G Kergon 	if (dm_open_count(md))
3375c6bd75dSAlasdair G Kergon 		r = -EBUSY;
3385c6bd75dSAlasdair G Kergon 	else
3395c6bd75dSAlasdair G Kergon 		set_bit(DMF_DELETING, &md->flags);
3405c6bd75dSAlasdair G Kergon 
3415c6bd75dSAlasdair G Kergon 	spin_unlock(&_minor_lock);
3425c6bd75dSAlasdair G Kergon 
3435c6bd75dSAlasdair G Kergon 	return r;
3445c6bd75dSAlasdair G Kergon }
3455c6bd75dSAlasdair G Kergon 
3463ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3473ac51e74SDarrick J. Wong {
3483ac51e74SDarrick J. Wong 	struct mapped_device *md = bdev->bd_disk->private_data;
3493ac51e74SDarrick J. Wong 
3503ac51e74SDarrick J. Wong 	return dm_get_geometry(md, geo);
3513ac51e74SDarrick J. Wong }
3523ac51e74SDarrick J. Wong 
353fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
354aa129a22SMilan Broz 			unsigned int cmd, unsigned long arg)
355aa129a22SMilan Broz {
356fe5f9f2cSAl Viro 	struct mapped_device *md = bdev->bd_disk->private_data;
357fe5f9f2cSAl Viro 	struct dm_table *map = dm_get_table(md);
358aa129a22SMilan Broz 	struct dm_target *tgt;
359aa129a22SMilan Broz 	int r = -ENOTTY;
360aa129a22SMilan Broz 
361aa129a22SMilan Broz 	if (!map || !dm_table_get_size(map))
362aa129a22SMilan Broz 		goto out;
363aa129a22SMilan Broz 
364aa129a22SMilan Broz 	/* We only support devices that have a single target */
365aa129a22SMilan Broz 	if (dm_table_get_num_targets(map) != 1)
366aa129a22SMilan Broz 		goto out;
367aa129a22SMilan Broz 
368aa129a22SMilan Broz 	tgt = dm_table_get_target(map, 0);
369aa129a22SMilan Broz 
370aa129a22SMilan Broz 	if (dm_suspended(md)) {
371aa129a22SMilan Broz 		r = -EAGAIN;
372aa129a22SMilan Broz 		goto out;
373aa129a22SMilan Broz 	}
374aa129a22SMilan Broz 
375aa129a22SMilan Broz 	if (tgt->type->ioctl)
376647b3d00SAl Viro 		r = tgt->type->ioctl(tgt, cmd, arg);
377aa129a22SMilan Broz 
378aa129a22SMilan Broz out:
379aa129a22SMilan Broz 	dm_table_put(map);
380aa129a22SMilan Broz 
381aa129a22SMilan Broz 	return r;
382aa129a22SMilan Broz }
383aa129a22SMilan Broz 
384028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md)
3851da177e4SLinus Torvalds {
3861da177e4SLinus Torvalds 	return mempool_alloc(md->io_pool, GFP_NOIO);
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
389028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io)
3901da177e4SLinus Torvalds {
3911da177e4SLinus Torvalds 	mempool_free(io, md->io_pool);
3921da177e4SLinus Torvalds }
3931da177e4SLinus Torvalds 
394028867acSAlasdair G Kergon static struct dm_target_io *alloc_tio(struct mapped_device *md)
3951da177e4SLinus Torvalds {
3961da177e4SLinus Torvalds 	return mempool_alloc(md->tio_pool, GFP_NOIO);
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
399028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
4001da177e4SLinus Torvalds {
4011da177e4SLinus Torvalds 	mempool_free(tio, md->tio_pool);
4021da177e4SLinus Torvalds }
4031da177e4SLinus Torvalds 
4043eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io)
4053eaf840eSJun'ichi "Nick" Nomura {
4063eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
407c9959059STejun Heo 	int cpu;
4083eaf840eSJun'ichi "Nick" Nomura 
4093eaf840eSJun'ichi "Nick" Nomura 	io->start_time = jiffies;
4103eaf840eSJun'ichi "Nick" Nomura 
411074a7acaSTejun Heo 	cpu = part_stat_lock();
412074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
413074a7acaSTejun Heo 	part_stat_unlock();
414074a7acaSTejun Heo 	dm_disk(md)->part0.in_flight = atomic_inc_return(&md->pending);
4153eaf840eSJun'ichi "Nick" Nomura }
4163eaf840eSJun'ichi "Nick" Nomura 
417d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io)
4183eaf840eSJun'ichi "Nick" Nomura {
4193eaf840eSJun'ichi "Nick" Nomura 	struct mapped_device *md = io->md;
4203eaf840eSJun'ichi "Nick" Nomura 	struct bio *bio = io->bio;
4213eaf840eSJun'ichi "Nick" Nomura 	unsigned long duration = jiffies - io->start_time;
422c9959059STejun Heo 	int pending, cpu;
4233eaf840eSJun'ichi "Nick" Nomura 	int rw = bio_data_dir(bio);
4243eaf840eSJun'ichi "Nick" Nomura 
425074a7acaSTejun Heo 	cpu = part_stat_lock();
426074a7acaSTejun Heo 	part_round_stats(cpu, &dm_disk(md)->part0);
427074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
428074a7acaSTejun Heo 	part_stat_unlock();
4293eaf840eSJun'ichi "Nick" Nomura 
430af7e466aSMikulas Patocka 	/*
431af7e466aSMikulas Patocka 	 * After this is decremented the bio must not be touched if it is
432af7e466aSMikulas Patocka 	 * a barrier.
433af7e466aSMikulas Patocka 	 */
434074a7acaSTejun Heo 	dm_disk(md)->part0.in_flight = pending =
435074a7acaSTejun Heo 		atomic_dec_return(&md->pending);
4363eaf840eSJun'ichi "Nick" Nomura 
437d221d2e7SMikulas Patocka 	/* nudge anyone waiting on suspend queue */
438d221d2e7SMikulas Patocka 	if (!pending)
439d221d2e7SMikulas Patocka 		wake_up(&md->wait);
4403eaf840eSJun'ichi "Nick" Nomura }
4413eaf840eSJun'ichi "Nick" Nomura 
4421da177e4SLinus Torvalds /*
4431da177e4SLinus Torvalds  * Add the bio to the list of deferred io.
4441da177e4SLinus Torvalds  */
44592c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio)
4461da177e4SLinus Torvalds {
4472ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
4481da177e4SLinus Torvalds 
449022c2611SMikulas Patocka 	spin_lock_irq(&md->deferred_lock);
4501da177e4SLinus Torvalds 	bio_list_add(&md->deferred, bio);
451022c2611SMikulas Patocka 	spin_unlock_irq(&md->deferred_lock);
4521da177e4SLinus Torvalds 
45392c63902SMikulas Patocka 	if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
45492c63902SMikulas Patocka 		queue_work(md->wq, &md->work);
45592c63902SMikulas Patocka 
4562ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds /*
4601da177e4SLinus Torvalds  * Everyone (including functions in this file), should use this
4611da177e4SLinus Torvalds  * function to access the md->map field, and make sure they call
4621da177e4SLinus Torvalds  * dm_table_put() when finished.
4631da177e4SLinus Torvalds  */
4641da177e4SLinus Torvalds struct dm_table *dm_get_table(struct mapped_device *md)
4651da177e4SLinus Torvalds {
4661da177e4SLinus Torvalds 	struct dm_table *t;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	read_lock(&md->map_lock);
4691da177e4SLinus Torvalds 	t = md->map;
4701da177e4SLinus Torvalds 	if (t)
4711da177e4SLinus Torvalds 		dm_table_get(t);
4721da177e4SLinus Torvalds 	read_unlock(&md->map_lock);
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	return t;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
4773ac51e74SDarrick J. Wong /*
4783ac51e74SDarrick J. Wong  * Get the geometry associated with a dm device
4793ac51e74SDarrick J. Wong  */
4803ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
4813ac51e74SDarrick J. Wong {
4823ac51e74SDarrick J. Wong 	*geo = md->geometry;
4833ac51e74SDarrick J. Wong 
4843ac51e74SDarrick J. Wong 	return 0;
4853ac51e74SDarrick J. Wong }
4863ac51e74SDarrick J. Wong 
4873ac51e74SDarrick J. Wong /*
4883ac51e74SDarrick J. Wong  * Set the geometry of a device.
4893ac51e74SDarrick J. Wong  */
4903ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
4913ac51e74SDarrick J. Wong {
4923ac51e74SDarrick J. Wong 	sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
4933ac51e74SDarrick J. Wong 
4943ac51e74SDarrick J. Wong 	if (geo->start > sz) {
4953ac51e74SDarrick J. Wong 		DMWARN("Start sector is beyond the geometry limits.");
4963ac51e74SDarrick J. Wong 		return -EINVAL;
4973ac51e74SDarrick J. Wong 	}
4983ac51e74SDarrick J. Wong 
4993ac51e74SDarrick J. Wong 	md->geometry = *geo;
5003ac51e74SDarrick J. Wong 
5013ac51e74SDarrick J. Wong 	return 0;
5023ac51e74SDarrick J. Wong }
5033ac51e74SDarrick J. Wong 
5041da177e4SLinus Torvalds /*-----------------------------------------------------------------
5051da177e4SLinus Torvalds  * CRUD START:
5061da177e4SLinus Torvalds  *   A more elegant soln is in the works that uses the queue
5071da177e4SLinus Torvalds  *   merge fn, unfortunately there are a couple of changes to
5081da177e4SLinus Torvalds  *   the block layer that I want to make for this.  So in the
5091da177e4SLinus Torvalds  *   interests of getting something for people to use I give
5101da177e4SLinus Torvalds  *   you this clearly demarcated crap.
5111da177e4SLinus Torvalds  *---------------------------------------------------------------*/
5121da177e4SLinus Torvalds 
5132e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md)
5142e93ccc1SKiyoshi Ueda {
5152e93ccc1SKiyoshi Ueda 	return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
5162e93ccc1SKiyoshi Ueda }
5172e93ccc1SKiyoshi Ueda 
5181da177e4SLinus Torvalds /*
5191da177e4SLinus Torvalds  * Decrements the number of outstanding ios that a bio has been
5201da177e4SLinus Torvalds  * cloned into, completing the original io if necc.
5211da177e4SLinus Torvalds  */
522858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error)
5231da177e4SLinus Torvalds {
5242e93ccc1SKiyoshi Ueda 	unsigned long flags;
525b35f8caaSMilan Broz 	int io_error;
526b35f8caaSMilan Broz 	struct bio *bio;
527b35f8caaSMilan Broz 	struct mapped_device *md = io->md;
5282e93ccc1SKiyoshi Ueda 
5292e93ccc1SKiyoshi Ueda 	/* Push-back supersedes any I/O errors */
530b35f8caaSMilan Broz 	if (error && !(io->error > 0 && __noflush_suspending(md)))
5311da177e4SLinus Torvalds 		io->error = error;
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	if (atomic_dec_and_test(&io->io_count)) {
5342e93ccc1SKiyoshi Ueda 		if (io->error == DM_ENDIO_REQUEUE) {
5352e93ccc1SKiyoshi Ueda 			/*
5362e93ccc1SKiyoshi Ueda 			 * Target requested pushing back the I/O.
5372e93ccc1SKiyoshi Ueda 			 */
538022c2611SMikulas Patocka 			spin_lock_irqsave(&md->deferred_lock, flags);
5392761e95fSMikulas Patocka 			if (__noflush_suspending(md)) {
5402761e95fSMikulas Patocka 				if (!bio_barrier(io->bio))
5412761e95fSMikulas Patocka 					bio_list_add_head(&md->deferred,
5422761e95fSMikulas Patocka 							  io->bio);
5432761e95fSMikulas Patocka 			} else
5442e93ccc1SKiyoshi Ueda 				/* noflush suspend was interrupted. */
5452e93ccc1SKiyoshi Ueda 				io->error = -EIO;
546022c2611SMikulas Patocka 			spin_unlock_irqrestore(&md->deferred_lock, flags);
5472e93ccc1SKiyoshi Ueda 		}
5482e93ccc1SKiyoshi Ueda 
549b35f8caaSMilan Broz 		io_error = io->error;
550b35f8caaSMilan Broz 		bio = io->bio;
5512056a782SJens Axboe 
552af7e466aSMikulas Patocka 		if (bio_barrier(bio)) {
553af7e466aSMikulas Patocka 			/*
554af7e466aSMikulas Patocka 			 * There can be just one barrier request so we use
555af7e466aSMikulas Patocka 			 * a per-device variable for error reporting.
556af7e466aSMikulas Patocka 			 * Note that you can't touch the bio after end_io_acct
557af7e466aSMikulas Patocka 			 */
558*fdb9572bSMikulas Patocka 			if (!md->barrier_error && io_error != -EOPNOTSUPP)
559af7e466aSMikulas Patocka 				md->barrier_error = io_error;
560af7e466aSMikulas Patocka 			end_io_acct(io);
561af7e466aSMikulas Patocka 		} else {
562af7e466aSMikulas Patocka 			end_io_acct(io);
563b35f8caaSMilan Broz 
564b35f8caaSMilan Broz 			if (io_error != DM_ENDIO_REQUEUE) {
565b35f8caaSMilan Broz 				trace_block_bio_complete(md->queue, bio);
566b35f8caaSMilan Broz 
567b35f8caaSMilan Broz 				bio_endio(bio, io_error);
5682e93ccc1SKiyoshi Ueda 			}
5691da177e4SLinus Torvalds 		}
570af7e466aSMikulas Patocka 
571af7e466aSMikulas Patocka 		free_io(md, io);
572af7e466aSMikulas Patocka 	}
5731da177e4SLinus Torvalds }
5741da177e4SLinus Torvalds 
5756712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error)
5761da177e4SLinus Torvalds {
5771da177e4SLinus Torvalds 	int r = 0;
578028867acSAlasdair G Kergon 	struct dm_target_io *tio = bio->bi_private;
579b35f8caaSMilan Broz 	struct dm_io *io = tio->io;
5809faf400fSStefan Bader 	struct mapped_device *md = tio->io->md;
5811da177e4SLinus Torvalds 	dm_endio_fn endio = tio->ti->type->end_io;
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	if (!bio_flagged(bio, BIO_UPTODATE) && !error)
5841da177e4SLinus Torvalds 		error = -EIO;
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	if (endio) {
5871da177e4SLinus Torvalds 		r = endio(tio->ti, bio, error, &tio->info);
5882e93ccc1SKiyoshi Ueda 		if (r < 0 || r == DM_ENDIO_REQUEUE)
5892e93ccc1SKiyoshi Ueda 			/*
5902e93ccc1SKiyoshi Ueda 			 * error and requeue request are handled
5912e93ccc1SKiyoshi Ueda 			 * in dec_pending().
5922e93ccc1SKiyoshi Ueda 			 */
5931da177e4SLinus Torvalds 			error = r;
59445cbcd79SKiyoshi Ueda 		else if (r == DM_ENDIO_INCOMPLETE)
59545cbcd79SKiyoshi Ueda 			/* The target will handle the io */
5966712ecf8SNeilBrown 			return;
59745cbcd79SKiyoshi Ueda 		else if (r) {
59845cbcd79SKiyoshi Ueda 			DMWARN("unimplemented target endio return value: %d", r);
59945cbcd79SKiyoshi Ueda 			BUG();
60045cbcd79SKiyoshi Ueda 		}
6011da177e4SLinus Torvalds 	}
6021da177e4SLinus Torvalds 
6039faf400fSStefan Bader 	/*
6049faf400fSStefan Bader 	 * Store md for cleanup instead of tio which is about to get freed.
6059faf400fSStefan Bader 	 */
6069faf400fSStefan Bader 	bio->bi_private = md->bs;
6079faf400fSStefan Bader 
6089faf400fSStefan Bader 	free_tio(md, tio);
609b35f8caaSMilan Broz 	bio_put(bio);
610b35f8caaSMilan Broz 	dec_pending(io, error);
6111da177e4SLinus Torvalds }
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds static sector_t max_io_len(struct mapped_device *md,
6141da177e4SLinus Torvalds 			   sector_t sector, struct dm_target *ti)
6151da177e4SLinus Torvalds {
6161da177e4SLinus Torvalds 	sector_t offset = sector - ti->begin;
6171da177e4SLinus Torvalds 	sector_t len = ti->len - offset;
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	/*
6201da177e4SLinus Torvalds 	 * Does the target need to split even further ?
6211da177e4SLinus Torvalds 	 */
6221da177e4SLinus Torvalds 	if (ti->split_io) {
6231da177e4SLinus Torvalds 		sector_t boundary;
6241da177e4SLinus Torvalds 		boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
6251da177e4SLinus Torvalds 			   - offset;
6261da177e4SLinus Torvalds 		if (len > boundary)
6271da177e4SLinus Torvalds 			len = boundary;
6281da177e4SLinus Torvalds 	}
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 	return len;
6311da177e4SLinus Torvalds }
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds static void __map_bio(struct dm_target *ti, struct bio *clone,
634028867acSAlasdair G Kergon 		      struct dm_target_io *tio)
6351da177e4SLinus Torvalds {
6361da177e4SLinus Torvalds 	int r;
6372056a782SJens Axboe 	sector_t sector;
6389faf400fSStefan Bader 	struct mapped_device *md;
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	/*
6411da177e4SLinus Torvalds 	 * Sanity checks.
6421da177e4SLinus Torvalds 	 */
6431da177e4SLinus Torvalds 	BUG_ON(!clone->bi_size);
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	clone->bi_end_io = clone_endio;
6461da177e4SLinus Torvalds 	clone->bi_private = tio;
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	/*
6491da177e4SLinus Torvalds 	 * Map the clone.  If r == 0 we don't need to do
6501da177e4SLinus Torvalds 	 * anything, the target has assumed ownership of
6511da177e4SLinus Torvalds 	 * this io.
6521da177e4SLinus Torvalds 	 */
6531da177e4SLinus Torvalds 	atomic_inc(&tio->io->io_count);
6542056a782SJens Axboe 	sector = clone->bi_sector;
6551da177e4SLinus Torvalds 	r = ti->type->map(ti, clone, &tio->info);
65645cbcd79SKiyoshi Ueda 	if (r == DM_MAPIO_REMAPPED) {
6571da177e4SLinus Torvalds 		/* the bio has been remapped so dispatch it */
6582056a782SJens Axboe 
6595f3ea37cSArnaldo Carvalho de Melo 		trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
66022a7c31aSAlan D. Brunelle 				    tio->io->bio->bi_bdev->bd_dev, sector);
6612056a782SJens Axboe 
6621da177e4SLinus Torvalds 		generic_make_request(clone);
6632e93ccc1SKiyoshi Ueda 	} else if (r < 0 || r == DM_MAPIO_REQUEUE) {
6642e93ccc1SKiyoshi Ueda 		/* error the io and bail out, or requeue it if needed */
6659faf400fSStefan Bader 		md = tio->io->md;
6669faf400fSStefan Bader 		dec_pending(tio->io, r);
6679faf400fSStefan Bader 		/*
6689faf400fSStefan Bader 		 * Store bio_set for cleanup.
6699faf400fSStefan Bader 		 */
6709faf400fSStefan Bader 		clone->bi_private = md->bs;
6711da177e4SLinus Torvalds 		bio_put(clone);
6729faf400fSStefan Bader 		free_tio(md, tio);
67345cbcd79SKiyoshi Ueda 	} else if (r) {
67445cbcd79SKiyoshi Ueda 		DMWARN("unimplemented target map return value: %d", r);
67545cbcd79SKiyoshi Ueda 		BUG();
6761da177e4SLinus Torvalds 	}
6771da177e4SLinus Torvalds }
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds struct clone_info {
6801da177e4SLinus Torvalds 	struct mapped_device *md;
6811da177e4SLinus Torvalds 	struct dm_table *map;
6821da177e4SLinus Torvalds 	struct bio *bio;
6831da177e4SLinus Torvalds 	struct dm_io *io;
6841da177e4SLinus Torvalds 	sector_t sector;
6851da177e4SLinus Torvalds 	sector_t sector_count;
6861da177e4SLinus Torvalds 	unsigned short idx;
6871da177e4SLinus Torvalds };
6881da177e4SLinus Torvalds 
6893676347aSPeter Osterlund static void dm_bio_destructor(struct bio *bio)
6903676347aSPeter Osterlund {
6919faf400fSStefan Bader 	struct bio_set *bs = bio->bi_private;
6929faf400fSStefan Bader 
6939faf400fSStefan Bader 	bio_free(bio, bs);
6943676347aSPeter Osterlund }
6953676347aSPeter Osterlund 
6961da177e4SLinus Torvalds /*
6971da177e4SLinus Torvalds  * Creates a little bio that is just does part of a bvec.
6981da177e4SLinus Torvalds  */
6991da177e4SLinus Torvalds static struct bio *split_bvec(struct bio *bio, sector_t sector,
7001da177e4SLinus Torvalds 			      unsigned short idx, unsigned int offset,
7019faf400fSStefan Bader 			      unsigned int len, struct bio_set *bs)
7021da177e4SLinus Torvalds {
7031da177e4SLinus Torvalds 	struct bio *clone;
7041da177e4SLinus Torvalds 	struct bio_vec *bv = bio->bi_io_vec + idx;
7051da177e4SLinus Torvalds 
7069faf400fSStefan Bader 	clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
7073676347aSPeter Osterlund 	clone->bi_destructor = dm_bio_destructor;
7081da177e4SLinus Torvalds 	*clone->bi_io_vec = *bv;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	clone->bi_sector = sector;
7111da177e4SLinus Torvalds 	clone->bi_bdev = bio->bi_bdev;
712af7e466aSMikulas Patocka 	clone->bi_rw = bio->bi_rw & ~(1 << BIO_RW_BARRIER);
7131da177e4SLinus Torvalds 	clone->bi_vcnt = 1;
7141da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
7151da177e4SLinus Torvalds 	clone->bi_io_vec->bv_offset = offset;
7161da177e4SLinus Torvalds 	clone->bi_io_vec->bv_len = clone->bi_size;
717f3e1d26eSMartin K. Petersen 	clone->bi_flags |= 1 << BIO_CLONED;
7181da177e4SLinus Torvalds 
7199c47008dSMartin K. Petersen 	if (bio_integrity(bio)) {
7209c47008dSMartin K. Petersen 		bio_integrity_clone(clone, bio, GFP_NOIO);
7219c47008dSMartin K. Petersen 		bio_integrity_trim(clone,
7229c47008dSMartin K. Petersen 				   bio_sector_offset(bio, idx, offset), len);
7239c47008dSMartin K. Petersen 	}
7249c47008dSMartin K. Petersen 
7251da177e4SLinus Torvalds 	return clone;
7261da177e4SLinus Torvalds }
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds /*
7291da177e4SLinus Torvalds  * Creates a bio that consists of range of complete bvecs.
7301da177e4SLinus Torvalds  */
7311da177e4SLinus Torvalds static struct bio *clone_bio(struct bio *bio, sector_t sector,
7321da177e4SLinus Torvalds 			     unsigned short idx, unsigned short bv_count,
7339faf400fSStefan Bader 			     unsigned int len, struct bio_set *bs)
7341da177e4SLinus Torvalds {
7351da177e4SLinus Torvalds 	struct bio *clone;
7361da177e4SLinus Torvalds 
7379faf400fSStefan Bader 	clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
7389faf400fSStefan Bader 	__bio_clone(clone, bio);
739af7e466aSMikulas Patocka 	clone->bi_rw &= ~(1 << BIO_RW_BARRIER);
7409faf400fSStefan Bader 	clone->bi_destructor = dm_bio_destructor;
7411da177e4SLinus Torvalds 	clone->bi_sector = sector;
7421da177e4SLinus Torvalds 	clone->bi_idx = idx;
7431da177e4SLinus Torvalds 	clone->bi_vcnt = idx + bv_count;
7441da177e4SLinus Torvalds 	clone->bi_size = to_bytes(len);
7451da177e4SLinus Torvalds 	clone->bi_flags &= ~(1 << BIO_SEG_VALID);
7461da177e4SLinus Torvalds 
7479c47008dSMartin K. Petersen 	if (bio_integrity(bio)) {
7489c47008dSMartin K. Petersen 		bio_integrity_clone(clone, bio, GFP_NOIO);
7499c47008dSMartin K. Petersen 
7509c47008dSMartin K. Petersen 		if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
7519c47008dSMartin K. Petersen 			bio_integrity_trim(clone,
7529c47008dSMartin K. Petersen 					   bio_sector_offset(bio, idx, 0), len);
7539c47008dSMartin K. Petersen 	}
7549c47008dSMartin K. Petersen 
7551da177e4SLinus Torvalds 	return clone;
7561da177e4SLinus Torvalds }
7571da177e4SLinus Torvalds 
758512875bdSJun'ichi Nomura static int __clone_and_map(struct clone_info *ci)
7591da177e4SLinus Torvalds {
7601da177e4SLinus Torvalds 	struct bio *clone, *bio = ci->bio;
761512875bdSJun'ichi Nomura 	struct dm_target *ti;
762512875bdSJun'ichi Nomura 	sector_t len = 0, max;
763028867acSAlasdair G Kergon 	struct dm_target_io *tio;
7641da177e4SLinus Torvalds 
765512875bdSJun'ichi Nomura 	ti = dm_table_find_target(ci->map, ci->sector);
766512875bdSJun'ichi Nomura 	if (!dm_target_is_valid(ti))
767512875bdSJun'ichi Nomura 		return -EIO;
768512875bdSJun'ichi Nomura 
769512875bdSJun'ichi Nomura 	max = max_io_len(ci->md, ci->sector, ti);
770512875bdSJun'ichi Nomura 
7711da177e4SLinus Torvalds 	/*
7721da177e4SLinus Torvalds 	 * Allocate a target io object.
7731da177e4SLinus Torvalds 	 */
7741da177e4SLinus Torvalds 	tio = alloc_tio(ci->md);
7751da177e4SLinus Torvalds 	tio->io = ci->io;
7761da177e4SLinus Torvalds 	tio->ti = ti;
7771da177e4SLinus Torvalds 	memset(&tio->info, 0, sizeof(tio->info));
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	if (ci->sector_count <= max) {
7801da177e4SLinus Torvalds 		/*
7811da177e4SLinus Torvalds 		 * Optimise for the simple case where we can do all of
7821da177e4SLinus Torvalds 		 * the remaining io with a single clone.
7831da177e4SLinus Torvalds 		 */
7841da177e4SLinus Torvalds 		clone = clone_bio(bio, ci->sector, ci->idx,
7859faf400fSStefan Bader 				  bio->bi_vcnt - ci->idx, ci->sector_count,
7869faf400fSStefan Bader 				  ci->md->bs);
7871da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
7881da177e4SLinus Torvalds 		ci->sector_count = 0;
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	} else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
7911da177e4SLinus Torvalds 		/*
7921da177e4SLinus Torvalds 		 * There are some bvecs that don't span targets.
7931da177e4SLinus Torvalds 		 * Do as many of these as possible.
7941da177e4SLinus Torvalds 		 */
7951da177e4SLinus Torvalds 		int i;
7961da177e4SLinus Torvalds 		sector_t remaining = max;
7971da177e4SLinus Torvalds 		sector_t bv_len;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 		for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
8001da177e4SLinus Torvalds 			bv_len = to_sector(bio->bi_io_vec[i].bv_len);
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 			if (bv_len > remaining)
8031da177e4SLinus Torvalds 				break;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 			remaining -= bv_len;
8061da177e4SLinus Torvalds 			len += bv_len;
8071da177e4SLinus Torvalds 		}
8081da177e4SLinus Torvalds 
8099faf400fSStefan Bader 		clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
8109faf400fSStefan Bader 				  ci->md->bs);
8111da177e4SLinus Torvalds 		__map_bio(ti, clone, tio);
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 		ci->sector += len;
8141da177e4SLinus Torvalds 		ci->sector_count -= len;
8151da177e4SLinus Torvalds 		ci->idx = i;
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	} else {
8181da177e4SLinus Torvalds 		/*
819d2044a94SAlasdair G Kergon 		 * Handle a bvec that must be split between two or more targets.
8201da177e4SLinus Torvalds 		 */
8211da177e4SLinus Torvalds 		struct bio_vec *bv = bio->bi_io_vec + ci->idx;
822d2044a94SAlasdair G Kergon 		sector_t remaining = to_sector(bv->bv_len);
823d2044a94SAlasdair G Kergon 		unsigned int offset = 0;
8241da177e4SLinus Torvalds 
825d2044a94SAlasdair G Kergon 		do {
826d2044a94SAlasdair G Kergon 			if (offset) {
8271da177e4SLinus Torvalds 				ti = dm_table_find_target(ci->map, ci->sector);
828512875bdSJun'ichi Nomura 				if (!dm_target_is_valid(ti))
829512875bdSJun'ichi Nomura 					return -EIO;
830512875bdSJun'ichi Nomura 
831d2044a94SAlasdair G Kergon 				max = max_io_len(ci->md, ci->sector, ti);
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 				tio = alloc_tio(ci->md);
8341da177e4SLinus Torvalds 				tio->io = ci->io;
8351da177e4SLinus Torvalds 				tio->ti = ti;
8361da177e4SLinus Torvalds 				memset(&tio->info, 0, sizeof(tio->info));
837d2044a94SAlasdair G Kergon 			}
838d2044a94SAlasdair G Kergon 
839d2044a94SAlasdair G Kergon 			len = min(remaining, max);
840d2044a94SAlasdair G Kergon 
841d2044a94SAlasdair G Kergon 			clone = split_bvec(bio, ci->sector, ci->idx,
8429faf400fSStefan Bader 					   bv->bv_offset + offset, len,
8439faf400fSStefan Bader 					   ci->md->bs);
844d2044a94SAlasdair G Kergon 
8451da177e4SLinus Torvalds 			__map_bio(ti, clone, tio);
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 			ci->sector += len;
8481da177e4SLinus Torvalds 			ci->sector_count -= len;
849d2044a94SAlasdair G Kergon 			offset += to_bytes(len);
850d2044a94SAlasdair G Kergon 		} while (remaining -= len);
851d2044a94SAlasdair G Kergon 
8521da177e4SLinus Torvalds 		ci->idx++;
8531da177e4SLinus Torvalds 	}
854512875bdSJun'ichi Nomura 
855512875bdSJun'ichi Nomura 	return 0;
8561da177e4SLinus Torvalds }
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds /*
8598a53c28dSMikulas Patocka  * Split the bio into several clones and submit it to targets.
8601da177e4SLinus Torvalds  */
861f0b9a450SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
8621da177e4SLinus Torvalds {
8631da177e4SLinus Torvalds 	struct clone_info ci;
864512875bdSJun'ichi Nomura 	int error = 0;
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	ci.map = dm_get_table(md);
867f0b9a450SMikulas Patocka 	if (unlikely(!ci.map)) {
868af7e466aSMikulas Patocka 		if (!bio_barrier(bio))
869f0b9a450SMikulas Patocka 			bio_io_error(bio);
870af7e466aSMikulas Patocka 		else
8715aa2781dSMikulas Patocka 			if (!md->barrier_error)
872af7e466aSMikulas Patocka 				md->barrier_error = -EIO;
873f0b9a450SMikulas Patocka 		return;
874f0b9a450SMikulas Patocka 	}
875692d0eb9SMikulas Patocka 
8761da177e4SLinus Torvalds 	ci.md = md;
8771da177e4SLinus Torvalds 	ci.bio = bio;
8781da177e4SLinus Torvalds 	ci.io = alloc_io(md);
8791da177e4SLinus Torvalds 	ci.io->error = 0;
8801da177e4SLinus Torvalds 	atomic_set(&ci.io->io_count, 1);
8811da177e4SLinus Torvalds 	ci.io->bio = bio;
8821da177e4SLinus Torvalds 	ci.io->md = md;
8831da177e4SLinus Torvalds 	ci.sector = bio->bi_sector;
8841da177e4SLinus Torvalds 	ci.sector_count = bio_sectors(bio);
8851da177e4SLinus Torvalds 	ci.idx = bio->bi_idx;
8861da177e4SLinus Torvalds 
8873eaf840eSJun'ichi "Nick" Nomura 	start_io_acct(ci.io);
888512875bdSJun'ichi Nomura 	while (ci.sector_count && !error)
889512875bdSJun'ichi Nomura 		error = __clone_and_map(&ci);
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	/* drop the extra reference count */
892512875bdSJun'ichi Nomura 	dec_pending(ci.io, error);
8931da177e4SLinus Torvalds 	dm_table_put(ci.map);
8941da177e4SLinus Torvalds }
8951da177e4SLinus Torvalds /*-----------------------------------------------------------------
8961da177e4SLinus Torvalds  * CRUD END
8971da177e4SLinus Torvalds  *---------------------------------------------------------------*/
8981da177e4SLinus Torvalds 
899f6fccb12SMilan Broz static int dm_merge_bvec(struct request_queue *q,
900f6fccb12SMilan Broz 			 struct bvec_merge_data *bvm,
901f6fccb12SMilan Broz 			 struct bio_vec *biovec)
902f6fccb12SMilan Broz {
903f6fccb12SMilan Broz 	struct mapped_device *md = q->queuedata;
904f6fccb12SMilan Broz 	struct dm_table *map = dm_get_table(md);
905f6fccb12SMilan Broz 	struct dm_target *ti;
906f6fccb12SMilan Broz 	sector_t max_sectors;
9075037108aSMikulas Patocka 	int max_size = 0;
908f6fccb12SMilan Broz 
909f6fccb12SMilan Broz 	if (unlikely(!map))
9105037108aSMikulas Patocka 		goto out;
911f6fccb12SMilan Broz 
912f6fccb12SMilan Broz 	ti = dm_table_find_target(map, bvm->bi_sector);
913b01cd5acSMikulas Patocka 	if (!dm_target_is_valid(ti))
914b01cd5acSMikulas Patocka 		goto out_table;
915f6fccb12SMilan Broz 
916f6fccb12SMilan Broz 	/*
917f6fccb12SMilan Broz 	 * Find maximum amount of I/O that won't need splitting
918f6fccb12SMilan Broz 	 */
919f6fccb12SMilan Broz 	max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
920f6fccb12SMilan Broz 			  (sector_t) BIO_MAX_SECTORS);
921f6fccb12SMilan Broz 	max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
922f6fccb12SMilan Broz 	if (max_size < 0)
923f6fccb12SMilan Broz 		max_size = 0;
924f6fccb12SMilan Broz 
925f6fccb12SMilan Broz 	/*
926f6fccb12SMilan Broz 	 * merge_bvec_fn() returns number of bytes
927f6fccb12SMilan Broz 	 * it can accept at this offset
928f6fccb12SMilan Broz 	 * max is precomputed maximal io size
929f6fccb12SMilan Broz 	 */
930f6fccb12SMilan Broz 	if (max_size && ti->type->merge)
931f6fccb12SMilan Broz 		max_size = ti->type->merge(ti, bvm, biovec, max_size);
9328cbeb67aSMikulas Patocka 	/*
9338cbeb67aSMikulas Patocka 	 * If the target doesn't support merge method and some of the devices
9348cbeb67aSMikulas Patocka 	 * provided their merge_bvec method (we know this by looking at
9358cbeb67aSMikulas Patocka 	 * queue_max_hw_sectors), then we can't allow bios with multiple vector
9368cbeb67aSMikulas Patocka 	 * entries.  So always set max_size to 0, and the code below allows
9378cbeb67aSMikulas Patocka 	 * just one page.
9388cbeb67aSMikulas Patocka 	 */
9398cbeb67aSMikulas Patocka 	else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
9408cbeb67aSMikulas Patocka 
9418cbeb67aSMikulas Patocka 		max_size = 0;
942f6fccb12SMilan Broz 
943b01cd5acSMikulas Patocka out_table:
9445037108aSMikulas Patocka 	dm_table_put(map);
9455037108aSMikulas Patocka 
9465037108aSMikulas Patocka out:
947f6fccb12SMilan Broz 	/*
948f6fccb12SMilan Broz 	 * Always allow an entire first page
949f6fccb12SMilan Broz 	 */
950f6fccb12SMilan Broz 	if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
951f6fccb12SMilan Broz 		max_size = biovec->bv_len;
952f6fccb12SMilan Broz 
953f6fccb12SMilan Broz 	return max_size;
954f6fccb12SMilan Broz }
955f6fccb12SMilan Broz 
9561da177e4SLinus Torvalds /*
9571da177e4SLinus Torvalds  * The request function that just remaps the bio built up by
9581da177e4SLinus Torvalds  * dm_merge_bvec.
9591da177e4SLinus Torvalds  */
960165125e1SJens Axboe static int dm_request(struct request_queue *q, struct bio *bio)
9611da177e4SLinus Torvalds {
96212f03a49SKevin Corry 	int rw = bio_data_dir(bio);
9631da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
964c9959059STejun Heo 	int cpu;
9651da177e4SLinus Torvalds 
9662ca3310eSAlasdair G Kergon 	down_read(&md->io_lock);
9671da177e4SLinus Torvalds 
968074a7acaSTejun Heo 	cpu = part_stat_lock();
969074a7acaSTejun Heo 	part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
970074a7acaSTejun Heo 	part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
971074a7acaSTejun Heo 	part_stat_unlock();
97212f03a49SKevin Corry 
9731da177e4SLinus Torvalds 	/*
9741eb787ecSAlasdair G Kergon 	 * If we're suspended or the thread is processing barriers
9751eb787ecSAlasdair G Kergon 	 * we have to queue this io for later.
9761da177e4SLinus Torvalds 	 */
977af7e466aSMikulas Patocka 	if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
978af7e466aSMikulas Patocka 	    unlikely(bio_barrier(bio))) {
9792ca3310eSAlasdair G Kergon 		up_read(&md->io_lock);
9801da177e4SLinus Torvalds 
98154d9a1b4SAlasdair G Kergon 		if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
98254d9a1b4SAlasdair G Kergon 		    bio_rw(bio) == READA) {
98354d9a1b4SAlasdair G Kergon 			bio_io_error(bio);
98454d9a1b4SAlasdair G Kergon 			return 0;
98554d9a1b4SAlasdair G Kergon 		}
9861da177e4SLinus Torvalds 
98792c63902SMikulas Patocka 		queue_io(md, bio);
9881da177e4SLinus Torvalds 
98992c63902SMikulas Patocka 		return 0;
9901da177e4SLinus Torvalds 	}
9911da177e4SLinus Torvalds 
992f0b9a450SMikulas Patocka 	__split_and_process_bio(md, bio);
9932ca3310eSAlasdair G Kergon 	up_read(&md->io_lock);
994f0b9a450SMikulas Patocka 	return 0;
9951da177e4SLinus Torvalds }
9961da177e4SLinus Torvalds 
997165125e1SJens Axboe static void dm_unplug_all(struct request_queue *q)
9981da177e4SLinus Torvalds {
9991da177e4SLinus Torvalds 	struct mapped_device *md = q->queuedata;
10001da177e4SLinus Torvalds 	struct dm_table *map = dm_get_table(md);
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	if (map) {
10031da177e4SLinus Torvalds 		dm_table_unplug_all(map);
10041da177e4SLinus Torvalds 		dm_table_put(map);
10051da177e4SLinus Torvalds 	}
10061da177e4SLinus Torvalds }
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits)
10091da177e4SLinus Torvalds {
10108a57dfc6SChandra Seetharaman 	int r = bdi_bits;
10118a57dfc6SChandra Seetharaman 	struct mapped_device *md = congested_data;
10128a57dfc6SChandra Seetharaman 	struct dm_table *map;
10131da177e4SLinus Torvalds 
10141eb787ecSAlasdair G Kergon 	if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
10158a57dfc6SChandra Seetharaman 		map = dm_get_table(md);
10168a57dfc6SChandra Seetharaman 		if (map) {
10171da177e4SLinus Torvalds 			r = dm_table_any_congested(map, bdi_bits);
10181da177e4SLinus Torvalds 			dm_table_put(map);
10198a57dfc6SChandra Seetharaman 		}
10208a57dfc6SChandra Seetharaman 	}
10218a57dfc6SChandra Seetharaman 
10221da177e4SLinus Torvalds 	return r;
10231da177e4SLinus Torvalds }
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds /*-----------------------------------------------------------------
10261da177e4SLinus Torvalds  * An IDR is used to keep track of allocated minor numbers.
10271da177e4SLinus Torvalds  *---------------------------------------------------------------*/
10281da177e4SLinus Torvalds static DEFINE_IDR(_minor_idr);
10291da177e4SLinus Torvalds 
10302b06cfffSAlasdair G Kergon static void free_minor(int minor)
10311da177e4SLinus Torvalds {
1032f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
10331da177e4SLinus Torvalds 	idr_remove(&_minor_idr, minor);
1034f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
10351da177e4SLinus Torvalds }
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds /*
10381da177e4SLinus Torvalds  * See if the device with a specific minor # is free.
10391da177e4SLinus Torvalds  */
1040cf13ab8eSFrederik Deweerdt static int specific_minor(int minor)
10411da177e4SLinus Torvalds {
10421da177e4SLinus Torvalds 	int r, m;
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	if (minor >= (1 << MINORBITS))
10451da177e4SLinus Torvalds 		return -EINVAL;
10461da177e4SLinus Torvalds 
104762f75c2fSJeff Mahoney 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
104862f75c2fSJeff Mahoney 	if (!r)
104962f75c2fSJeff Mahoney 		return -ENOMEM;
105062f75c2fSJeff Mahoney 
1051f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 	if (idr_find(&_minor_idr, minor)) {
10541da177e4SLinus Torvalds 		r = -EBUSY;
10551da177e4SLinus Torvalds 		goto out;
10561da177e4SLinus Torvalds 	}
10571da177e4SLinus Torvalds 
1058ba61fdd1SJeff Mahoney 	r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
105962f75c2fSJeff Mahoney 	if (r)
10601da177e4SLinus Torvalds 		goto out;
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	if (m != minor) {
10631da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
10641da177e4SLinus Torvalds 		r = -EBUSY;
10651da177e4SLinus Torvalds 		goto out;
10661da177e4SLinus Torvalds 	}
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds out:
1069f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
10701da177e4SLinus Torvalds 	return r;
10711da177e4SLinus Torvalds }
10721da177e4SLinus Torvalds 
1073cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor)
10741da177e4SLinus Torvalds {
10752b06cfffSAlasdair G Kergon 	int r, m;
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 	r = idr_pre_get(&_minor_idr, GFP_KERNEL);
107862f75c2fSJeff Mahoney 	if (!r)
107962f75c2fSJeff Mahoney 		return -ENOMEM;
108062f75c2fSJeff Mahoney 
1081f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
10821da177e4SLinus Torvalds 
1083ba61fdd1SJeff Mahoney 	r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
1084cf13ab8eSFrederik Deweerdt 	if (r)
10851da177e4SLinus Torvalds 		goto out;
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 	if (m >= (1 << MINORBITS)) {
10881da177e4SLinus Torvalds 		idr_remove(&_minor_idr, m);
10891da177e4SLinus Torvalds 		r = -ENOSPC;
10901da177e4SLinus Torvalds 		goto out;
10911da177e4SLinus Torvalds 	}
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds 	*minor = m;
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds out:
1096f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
10971da177e4SLinus Torvalds 	return r;
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops;
11011da177e4SLinus Torvalds 
110253d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work);
110353d5914fSMikulas Patocka 
11041da177e4SLinus Torvalds /*
11051da177e4SLinus Torvalds  * Allocate and initialise a blank device with a given minor.
11061da177e4SLinus Torvalds  */
11072b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor)
11081da177e4SLinus Torvalds {
11091da177e4SLinus Torvalds 	int r;
1110cf13ab8eSFrederik Deweerdt 	struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
1111ba61fdd1SJeff Mahoney 	void *old_md;
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 	if (!md) {
11141da177e4SLinus Torvalds 		DMWARN("unable to allocate device, out of memory.");
11151da177e4SLinus Torvalds 		return NULL;
11161da177e4SLinus Torvalds 	}
11171da177e4SLinus Torvalds 
111810da4f79SJeff Mahoney 	if (!try_module_get(THIS_MODULE))
11196ed7ade8SMilan Broz 		goto bad_module_get;
112010da4f79SJeff Mahoney 
11211da177e4SLinus Torvalds 	/* get a minor number for the dev */
11222b06cfffSAlasdair G Kergon 	if (minor == DM_ANY_MINOR)
1123cf13ab8eSFrederik Deweerdt 		r = next_free_minor(&minor);
11242b06cfffSAlasdair G Kergon 	else
1125cf13ab8eSFrederik Deweerdt 		r = specific_minor(minor);
11261da177e4SLinus Torvalds 	if (r < 0)
11276ed7ade8SMilan Broz 		goto bad_minor;
11281da177e4SLinus Torvalds 
11292ca3310eSAlasdair G Kergon 	init_rwsem(&md->io_lock);
1130e61290a4SDaniel Walker 	mutex_init(&md->suspend_lock);
1131022c2611SMikulas Patocka 	spin_lock_init(&md->deferred_lock);
11321da177e4SLinus Torvalds 	rwlock_init(&md->map_lock);
11331da177e4SLinus Torvalds 	atomic_set(&md->holders, 1);
11345c6bd75dSAlasdair G Kergon 	atomic_set(&md->open_count, 0);
11351da177e4SLinus Torvalds 	atomic_set(&md->event_nr, 0);
11367a8c3d3bSMike Anderson 	atomic_set(&md->uevent_seq, 0);
11377a8c3d3bSMike Anderson 	INIT_LIST_HEAD(&md->uevent_list);
11387a8c3d3bSMike Anderson 	spin_lock_init(&md->uevent_lock);
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 	md->queue = blk_alloc_queue(GFP_KERNEL);
11411da177e4SLinus Torvalds 	if (!md->queue)
11426ed7ade8SMilan Broz 		goto bad_queue;
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	md->queue->queuedata = md;
11451da177e4SLinus Torvalds 	md->queue->backing_dev_info.congested_fn = dm_any_congested;
11461da177e4SLinus Torvalds 	md->queue->backing_dev_info.congested_data = md;
11471da177e4SLinus Torvalds 	blk_queue_make_request(md->queue, dm_request);
114899360b4cSMikulas Patocka 	blk_queue_ordered(md->queue, QUEUE_ORDERED_DRAIN, NULL);
1149daef265fSJens Axboe 	blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
11501da177e4SLinus Torvalds 	md->queue->unplug_fn = dm_unplug_all;
1151f6fccb12SMilan Broz 	blk_queue_merge_bvec(md->queue, dm_merge_bvec);
11521da177e4SLinus Torvalds 
115393d2341cSMatthew Dobson 	md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
11541da177e4SLinus Torvalds 	if (!md->io_pool)
11556ed7ade8SMilan Broz 		goto bad_io_pool;
11561da177e4SLinus Torvalds 
115793d2341cSMatthew Dobson 	md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
11581da177e4SLinus Torvalds 	if (!md->tio_pool)
11596ed7ade8SMilan Broz 		goto bad_tio_pool;
11601da177e4SLinus Torvalds 
1161bb799ca0SJens Axboe 	md->bs = bioset_create(16, 0);
11629faf400fSStefan Bader 	if (!md->bs)
11639faf400fSStefan Bader 		goto bad_no_bioset;
11649faf400fSStefan Bader 
11651da177e4SLinus Torvalds 	md->disk = alloc_disk(1);
11661da177e4SLinus Torvalds 	if (!md->disk)
11676ed7ade8SMilan Broz 		goto bad_disk;
11681da177e4SLinus Torvalds 
1169f0b04115SJeff Mahoney 	atomic_set(&md->pending, 0);
1170f0b04115SJeff Mahoney 	init_waitqueue_head(&md->wait);
117153d5914fSMikulas Patocka 	INIT_WORK(&md->work, dm_wq_work);
1172f0b04115SJeff Mahoney 	init_waitqueue_head(&md->eventq);
1173f0b04115SJeff Mahoney 
11741da177e4SLinus Torvalds 	md->disk->major = _major;
11751da177e4SLinus Torvalds 	md->disk->first_minor = minor;
11761da177e4SLinus Torvalds 	md->disk->fops = &dm_blk_dops;
11771da177e4SLinus Torvalds 	md->disk->queue = md->queue;
11781da177e4SLinus Torvalds 	md->disk->private_data = md;
11791da177e4SLinus Torvalds 	sprintf(md->disk->disk_name, "dm-%d", minor);
11801da177e4SLinus Torvalds 	add_disk(md->disk);
11817e51f257SMike Anderson 	format_dev_t(md->name, MKDEV(_major, minor));
11821da177e4SLinus Torvalds 
1183304f3f6aSMilan Broz 	md->wq = create_singlethread_workqueue("kdmflush");
1184304f3f6aSMilan Broz 	if (!md->wq)
1185304f3f6aSMilan Broz 		goto bad_thread;
1186304f3f6aSMilan Broz 
118732a926daSMikulas Patocka 	md->bdev = bdget_disk(md->disk, 0);
118832a926daSMikulas Patocka 	if (!md->bdev)
118932a926daSMikulas Patocka 		goto bad_bdev;
119032a926daSMikulas Patocka 
1191ba61fdd1SJeff Mahoney 	/* Populate the mapping, nobody knows we exist yet */
1192f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
1193ba61fdd1SJeff Mahoney 	old_md = idr_replace(&_minor_idr, md, minor);
1194f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
1195ba61fdd1SJeff Mahoney 
1196ba61fdd1SJeff Mahoney 	BUG_ON(old_md != MINOR_ALLOCED);
1197ba61fdd1SJeff Mahoney 
11981da177e4SLinus Torvalds 	return md;
11991da177e4SLinus Torvalds 
120032a926daSMikulas Patocka bad_bdev:
120132a926daSMikulas Patocka 	destroy_workqueue(md->wq);
1202304f3f6aSMilan Broz bad_thread:
1203304f3f6aSMilan Broz 	put_disk(md->disk);
12046ed7ade8SMilan Broz bad_disk:
12059faf400fSStefan Bader 	bioset_free(md->bs);
12069faf400fSStefan Bader bad_no_bioset:
12071da177e4SLinus Torvalds 	mempool_destroy(md->tio_pool);
12086ed7ade8SMilan Broz bad_tio_pool:
12091da177e4SLinus Torvalds 	mempool_destroy(md->io_pool);
12106ed7ade8SMilan Broz bad_io_pool:
12111312f40eSAl Viro 	blk_cleanup_queue(md->queue);
12126ed7ade8SMilan Broz bad_queue:
12131da177e4SLinus Torvalds 	free_minor(minor);
12146ed7ade8SMilan Broz bad_minor:
121510da4f79SJeff Mahoney 	module_put(THIS_MODULE);
12166ed7ade8SMilan Broz bad_module_get:
12171da177e4SLinus Torvalds 	kfree(md);
12181da177e4SLinus Torvalds 	return NULL;
12191da177e4SLinus Torvalds }
12201da177e4SLinus Torvalds 
1221ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md);
1222ae9da83fSJun'ichi Nomura 
12231da177e4SLinus Torvalds static void free_dev(struct mapped_device *md)
12241da177e4SLinus Torvalds {
1225f331c029STejun Heo 	int minor = MINOR(disk_devt(md->disk));
122663d94e48SJun'ichi Nomura 
1227ae9da83fSJun'ichi Nomura 	unlock_fs(md);
1228db8fef4fSMikulas Patocka 	bdput(md->bdev);
1229304f3f6aSMilan Broz 	destroy_workqueue(md->wq);
12301da177e4SLinus Torvalds 	mempool_destroy(md->tio_pool);
12311da177e4SLinus Torvalds 	mempool_destroy(md->io_pool);
12329faf400fSStefan Bader 	bioset_free(md->bs);
12339c47008dSMartin K. Petersen 	blk_integrity_unregister(md->disk);
12341da177e4SLinus Torvalds 	del_gendisk(md->disk);
123563d94e48SJun'ichi Nomura 	free_minor(minor);
1236fba9f90eSJeff Mahoney 
1237fba9f90eSJeff Mahoney 	spin_lock(&_minor_lock);
1238fba9f90eSJeff Mahoney 	md->disk->private_data = NULL;
1239fba9f90eSJeff Mahoney 	spin_unlock(&_minor_lock);
1240fba9f90eSJeff Mahoney 
12411da177e4SLinus Torvalds 	put_disk(md->disk);
12421312f40eSAl Viro 	blk_cleanup_queue(md->queue);
124310da4f79SJeff Mahoney 	module_put(THIS_MODULE);
12441da177e4SLinus Torvalds 	kfree(md);
12451da177e4SLinus Torvalds }
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds /*
12481da177e4SLinus Torvalds  * Bind a table to the device.
12491da177e4SLinus Torvalds  */
12501da177e4SLinus Torvalds static void event_callback(void *context)
12511da177e4SLinus Torvalds {
12527a8c3d3bSMike Anderson 	unsigned long flags;
12537a8c3d3bSMike Anderson 	LIST_HEAD(uevents);
12541da177e4SLinus Torvalds 	struct mapped_device *md = (struct mapped_device *) context;
12551da177e4SLinus Torvalds 
12567a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
12577a8c3d3bSMike Anderson 	list_splice_init(&md->uevent_list, &uevents);
12587a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
12597a8c3d3bSMike Anderson 
1260ed9e1982STejun Heo 	dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
12617a8c3d3bSMike Anderson 
12621da177e4SLinus Torvalds 	atomic_inc(&md->event_nr);
12631da177e4SLinus Torvalds 	wake_up(&md->eventq);
12641da177e4SLinus Torvalds }
12651da177e4SLinus Torvalds 
12664e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size)
12671da177e4SLinus Torvalds {
12684e90188bSAlasdair G Kergon 	set_capacity(md->disk, size);
12691da177e4SLinus Torvalds 
1270db8fef4fSMikulas Patocka 	mutex_lock(&md->bdev->bd_inode->i_mutex);
1271db8fef4fSMikulas Patocka 	i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
1272db8fef4fSMikulas Patocka 	mutex_unlock(&md->bdev->bd_inode->i_mutex);
12731da177e4SLinus Torvalds }
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds static int __bind(struct mapped_device *md, struct dm_table *t)
12761da177e4SLinus Torvalds {
1277165125e1SJens Axboe 	struct request_queue *q = md->queue;
12781da177e4SLinus Torvalds 	sector_t size;
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds 	size = dm_table_get_size(t);
12813ac51e74SDarrick J. Wong 
12823ac51e74SDarrick J. Wong 	/*
12833ac51e74SDarrick J. Wong 	 * Wipe any geometry if the size of the table changed.
12843ac51e74SDarrick J. Wong 	 */
12853ac51e74SDarrick J. Wong 	if (size != get_capacity(md->disk))
12863ac51e74SDarrick J. Wong 		memset(&md->geometry, 0, sizeof(md->geometry));
12873ac51e74SDarrick J. Wong 
12884e90188bSAlasdair G Kergon 	__set_size(md, size);
12891da177e4SLinus Torvalds 
1290d5816876SMikulas Patocka 	if (!size) {
1291d5816876SMikulas Patocka 		dm_table_destroy(t);
1292d5816876SMikulas Patocka 		return 0;
1293d5816876SMikulas Patocka 	}
1294d5816876SMikulas Patocka 
1295cf222b37SAlasdair G Kergon 	dm_table_event_callback(t, event_callback, md);
12962ca3310eSAlasdair G Kergon 
12972ca3310eSAlasdair G Kergon 	write_lock(&md->map_lock);
12982ca3310eSAlasdair G Kergon 	md->map = t;
12991da177e4SLinus Torvalds 	dm_table_set_restrictions(t, q);
13002ca3310eSAlasdair G Kergon 	write_unlock(&md->map_lock);
13012ca3310eSAlasdair G Kergon 
13021da177e4SLinus Torvalds 	return 0;
13031da177e4SLinus Torvalds }
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds static void __unbind(struct mapped_device *md)
13061da177e4SLinus Torvalds {
13071da177e4SLinus Torvalds 	struct dm_table *map = md->map;
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 	if (!map)
13101da177e4SLinus Torvalds 		return;
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 	dm_table_event_callback(map, NULL, NULL);
13131da177e4SLinus Torvalds 	write_lock(&md->map_lock);
13141da177e4SLinus Torvalds 	md->map = NULL;
13151da177e4SLinus Torvalds 	write_unlock(&md->map_lock);
1316d5816876SMikulas Patocka 	dm_table_destroy(map);
13171da177e4SLinus Torvalds }
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds /*
13201da177e4SLinus Torvalds  * Constructor for a new device.
13211da177e4SLinus Torvalds  */
13222b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result)
13231da177e4SLinus Torvalds {
13241da177e4SLinus Torvalds 	struct mapped_device *md;
13251da177e4SLinus Torvalds 
13262b06cfffSAlasdair G Kergon 	md = alloc_dev(minor);
13271da177e4SLinus Torvalds 	if (!md)
13281da177e4SLinus Torvalds 		return -ENXIO;
13291da177e4SLinus Torvalds 
1330784aae73SMilan Broz 	dm_sysfs_init(md);
1331784aae73SMilan Broz 
13321da177e4SLinus Torvalds 	*result = md;
13331da177e4SLinus Torvalds 	return 0;
13341da177e4SLinus Torvalds }
13351da177e4SLinus Torvalds 
1336637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev)
13371da177e4SLinus Torvalds {
13381da177e4SLinus Torvalds 	struct mapped_device *md;
13391da177e4SLinus Torvalds 	unsigned minor = MINOR(dev);
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds 	if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
13421da177e4SLinus Torvalds 		return NULL;
13431da177e4SLinus Torvalds 
1344f32c10b0SJeff Mahoney 	spin_lock(&_minor_lock);
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 	md = idr_find(&_minor_idr, minor);
1347fba9f90eSJeff Mahoney 	if (md && (md == MINOR_ALLOCED ||
1348f331c029STejun Heo 		   (MINOR(disk_devt(dm_disk(md))) != minor) ||
1349fba9f90eSJeff Mahoney 		   test_bit(DMF_FREEING, &md->flags))) {
1350637842cfSDavid Teigland 		md = NULL;
1351fba9f90eSJeff Mahoney 		goto out;
1352fba9f90eSJeff Mahoney 	}
13531da177e4SLinus Torvalds 
1354fba9f90eSJeff Mahoney out:
1355f32c10b0SJeff Mahoney 	spin_unlock(&_minor_lock);
13561da177e4SLinus Torvalds 
1357637842cfSDavid Teigland 	return md;
1358637842cfSDavid Teigland }
1359637842cfSDavid Teigland 
1360d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev)
1361d229a958SDavid Teigland {
1362d229a958SDavid Teigland 	struct mapped_device *md = dm_find_md(dev);
1363d229a958SDavid Teigland 
1364d229a958SDavid Teigland 	if (md)
1365d229a958SDavid Teigland 		dm_get(md);
1366d229a958SDavid Teigland 
1367d229a958SDavid Teigland 	return md;
1368d229a958SDavid Teigland }
1369d229a958SDavid Teigland 
13709ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md)
1371637842cfSDavid Teigland {
13729ade92a9SAlasdair G Kergon 	return md->interface_ptr;
13731da177e4SLinus Torvalds }
13741da177e4SLinus Torvalds 
13751da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr)
13761da177e4SLinus Torvalds {
13771da177e4SLinus Torvalds 	md->interface_ptr = ptr;
13781da177e4SLinus Torvalds }
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds void dm_get(struct mapped_device *md)
13811da177e4SLinus Torvalds {
13821da177e4SLinus Torvalds 	atomic_inc(&md->holders);
13831da177e4SLinus Torvalds }
13841da177e4SLinus Torvalds 
138572d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md)
138672d94861SAlasdair G Kergon {
138772d94861SAlasdair G Kergon 	return md->name;
138872d94861SAlasdair G Kergon }
138972d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name);
139072d94861SAlasdair G Kergon 
13911da177e4SLinus Torvalds void dm_put(struct mapped_device *md)
13921da177e4SLinus Torvalds {
13931134e5aeSMike Anderson 	struct dm_table *map;
13941da177e4SLinus Torvalds 
1395fba9f90eSJeff Mahoney 	BUG_ON(test_bit(DMF_FREEING, &md->flags));
1396fba9f90eSJeff Mahoney 
1397f32c10b0SJeff Mahoney 	if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
13981134e5aeSMike Anderson 		map = dm_get_table(md);
1399f331c029STejun Heo 		idr_replace(&_minor_idr, MINOR_ALLOCED,
1400f331c029STejun Heo 			    MINOR(disk_devt(dm_disk(md))));
1401fba9f90eSJeff Mahoney 		set_bit(DMF_FREEING, &md->flags);
1402f32c10b0SJeff Mahoney 		spin_unlock(&_minor_lock);
1403cf222b37SAlasdair G Kergon 		if (!dm_suspended(md)) {
14041da177e4SLinus Torvalds 			dm_table_presuspend_targets(map);
14051da177e4SLinus Torvalds 			dm_table_postsuspend_targets(map);
14061da177e4SLinus Torvalds 		}
1407784aae73SMilan Broz 		dm_sysfs_exit(md);
14081134e5aeSMike Anderson 		dm_table_put(map);
1409a1b51e98SMikulas Patocka 		__unbind(md);
14101da177e4SLinus Torvalds 		free_dev(md);
14111da177e4SLinus Torvalds 	}
14121da177e4SLinus Torvalds }
141379eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put);
14141da177e4SLinus Torvalds 
1415401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
141646125c1cSMilan Broz {
141746125c1cSMilan Broz 	int r = 0;
1418b44ebeb0SMikulas Patocka 	DECLARE_WAITQUEUE(wait, current);
1419b44ebeb0SMikulas Patocka 
1420b44ebeb0SMikulas Patocka 	dm_unplug_all(md->queue);
1421b44ebeb0SMikulas Patocka 
1422b44ebeb0SMikulas Patocka 	add_wait_queue(&md->wait, &wait);
142346125c1cSMilan Broz 
142446125c1cSMilan Broz 	while (1) {
1425401600dfSMikulas Patocka 		set_current_state(interruptible);
142646125c1cSMilan Broz 
142746125c1cSMilan Broz 		smp_mb();
142846125c1cSMilan Broz 		if (!atomic_read(&md->pending))
142946125c1cSMilan Broz 			break;
143046125c1cSMilan Broz 
1431401600dfSMikulas Patocka 		if (interruptible == TASK_INTERRUPTIBLE &&
1432401600dfSMikulas Patocka 		    signal_pending(current)) {
143346125c1cSMilan Broz 			r = -EINTR;
143446125c1cSMilan Broz 			break;
143546125c1cSMilan Broz 		}
143646125c1cSMilan Broz 
143746125c1cSMilan Broz 		io_schedule();
143846125c1cSMilan Broz 	}
143946125c1cSMilan Broz 	set_current_state(TASK_RUNNING);
144046125c1cSMilan Broz 
1441b44ebeb0SMikulas Patocka 	remove_wait_queue(&md->wait, &wait);
1442b44ebeb0SMikulas Patocka 
144346125c1cSMilan Broz 	return r;
144446125c1cSMilan Broz }
144546125c1cSMilan Broz 
1446531fe963SMikulas Patocka static void dm_flush(struct mapped_device *md)
1447af7e466aSMikulas Patocka {
1448af7e466aSMikulas Patocka 	dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
1449af7e466aSMikulas Patocka }
1450af7e466aSMikulas Patocka 
1451af7e466aSMikulas Patocka static void process_barrier(struct mapped_device *md, struct bio *bio)
1452af7e466aSMikulas Patocka {
14535aa2781dSMikulas Patocka 	md->barrier_error = 0;
14545aa2781dSMikulas Patocka 
1455531fe963SMikulas Patocka 	dm_flush(md);
1456af7e466aSMikulas Patocka 
14575aa2781dSMikulas Patocka 	if (!bio_empty_barrier(bio)) {
1458af7e466aSMikulas Patocka 		__split_and_process_bio(md, bio);
1459531fe963SMikulas Patocka 		dm_flush(md);
14605aa2781dSMikulas Patocka 	}
1461af7e466aSMikulas Patocka 
1462af7e466aSMikulas Patocka 	if (md->barrier_error != DM_ENDIO_REQUEUE)
1463531fe963SMikulas Patocka 		bio_endio(bio, md->barrier_error);
14642761e95fSMikulas Patocka 	else {
14652761e95fSMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
14662761e95fSMikulas Patocka 		bio_list_add_head(&md->deferred, bio);
14672761e95fSMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
14682761e95fSMikulas Patocka 	}
1469af7e466aSMikulas Patocka }
1470af7e466aSMikulas Patocka 
14711da177e4SLinus Torvalds /*
14721da177e4SLinus Torvalds  * Process the deferred bios
14731da177e4SLinus Torvalds  */
1474ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work)
14751da177e4SLinus Torvalds {
1476ef208587SMikulas Patocka 	struct mapped_device *md = container_of(work, struct mapped_device,
1477ef208587SMikulas Patocka 						work);
14786d6f10dfSMilan Broz 	struct bio *c;
14791da177e4SLinus Torvalds 
1480ef208587SMikulas Patocka 	down_write(&md->io_lock);
1481ef208587SMikulas Patocka 
14823b00b203SMikulas Patocka 	while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
1483022c2611SMikulas Patocka 		spin_lock_irq(&md->deferred_lock);
1484022c2611SMikulas Patocka 		c = bio_list_pop(&md->deferred);
1485022c2611SMikulas Patocka 		spin_unlock_irq(&md->deferred_lock);
1486022c2611SMikulas Patocka 
1487df12ee99SAlasdair G Kergon 		if (!c) {
14881eb787ecSAlasdair G Kergon 			clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
1489df12ee99SAlasdair G Kergon 			break;
1490022c2611SMikulas Patocka 		}
149173d410c0SMilan Broz 
14923b00b203SMikulas Patocka 		up_write(&md->io_lock);
14933b00b203SMikulas Patocka 
1494af7e466aSMikulas Patocka 		if (bio_barrier(c))
1495af7e466aSMikulas Patocka 			process_barrier(md, c);
1496af7e466aSMikulas Patocka 		else
1497df12ee99SAlasdair G Kergon 			__split_and_process_bio(md, c);
14983b00b203SMikulas Patocka 
14993b00b203SMikulas Patocka 		down_write(&md->io_lock);
1500df12ee99SAlasdair G Kergon 	}
1501ef208587SMikulas Patocka 
1502ef208587SMikulas Patocka 	up_write(&md->io_lock);
15031da177e4SLinus Torvalds }
15041da177e4SLinus Torvalds 
15059a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md)
1506304f3f6aSMilan Broz {
15073b00b203SMikulas Patocka 	clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
15083b00b203SMikulas Patocka 	smp_mb__after_clear_bit();
150953d5914fSMikulas Patocka 	queue_work(md->wq, &md->work);
1510304f3f6aSMilan Broz }
1511304f3f6aSMilan Broz 
15121da177e4SLinus Torvalds /*
15131da177e4SLinus Torvalds  * Swap in a new table (destroying old one).
15141da177e4SLinus Torvalds  */
15151da177e4SLinus Torvalds int dm_swap_table(struct mapped_device *md, struct dm_table *table)
15161da177e4SLinus Torvalds {
151793c534aeSAlasdair G Kergon 	int r = -EINVAL;
15181da177e4SLinus Torvalds 
1519e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds 	/* device must be suspended */
1522cf222b37SAlasdair G Kergon 	if (!dm_suspended(md))
152393c534aeSAlasdair G Kergon 		goto out;
15241da177e4SLinus Torvalds 
15251da177e4SLinus Torvalds 	__unbind(md);
15261da177e4SLinus Torvalds 	r = __bind(md, table);
15271da177e4SLinus Torvalds 
152893c534aeSAlasdair G Kergon out:
1529e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
153093c534aeSAlasdair G Kergon 	return r;
15311da177e4SLinus Torvalds }
15321da177e4SLinus Torvalds 
15331da177e4SLinus Torvalds /*
15341da177e4SLinus Torvalds  * Functions to lock and unlock any filesystem running on the
15351da177e4SLinus Torvalds  * device.
15361da177e4SLinus Torvalds  */
15372ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md)
15381da177e4SLinus Torvalds {
1539e39e2e95SAlasdair G Kergon 	int r;
15401da177e4SLinus Torvalds 
15411da177e4SLinus Torvalds 	WARN_ON(md->frozen_sb);
1542dfbe03f6SAlasdair G Kergon 
1543db8fef4fSMikulas Patocka 	md->frozen_sb = freeze_bdev(md->bdev);
1544dfbe03f6SAlasdair G Kergon 	if (IS_ERR(md->frozen_sb)) {
1545cf222b37SAlasdair G Kergon 		r = PTR_ERR(md->frozen_sb);
1546e39e2e95SAlasdair G Kergon 		md->frozen_sb = NULL;
1547e39e2e95SAlasdair G Kergon 		return r;
1548dfbe03f6SAlasdair G Kergon 	}
1549dfbe03f6SAlasdair G Kergon 
1550aa8d7c2fSAlasdair G Kergon 	set_bit(DMF_FROZEN, &md->flags);
1551aa8d7c2fSAlasdair G Kergon 
15521da177e4SLinus Torvalds 	return 0;
15531da177e4SLinus Torvalds }
15541da177e4SLinus Torvalds 
15552ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md)
15561da177e4SLinus Torvalds {
1557aa8d7c2fSAlasdair G Kergon 	if (!test_bit(DMF_FROZEN, &md->flags))
1558aa8d7c2fSAlasdair G Kergon 		return;
1559aa8d7c2fSAlasdair G Kergon 
1560db8fef4fSMikulas Patocka 	thaw_bdev(md->bdev, md->frozen_sb);
15611da177e4SLinus Torvalds 	md->frozen_sb = NULL;
1562aa8d7c2fSAlasdair G Kergon 	clear_bit(DMF_FROZEN, &md->flags);
15631da177e4SLinus Torvalds }
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds /*
15661da177e4SLinus Torvalds  * We need to be able to change a mapping table under a mounted
15671da177e4SLinus Torvalds  * filesystem.  For example we might want to move some data in
15681da177e4SLinus Torvalds  * the background.  Before the table can be swapped with
15691da177e4SLinus Torvalds  * dm_bind_table, dm_suspend must be called to flush any in
15701da177e4SLinus Torvalds  * flight bios and ensure that any further io gets deferred.
15711da177e4SLinus Torvalds  */
1572a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
15731da177e4SLinus Torvalds {
15742ca3310eSAlasdair G Kergon 	struct dm_table *map = NULL;
157546125c1cSMilan Broz 	int r = 0;
1576a3d77d35SKiyoshi Ueda 	int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
15772e93ccc1SKiyoshi Ueda 	int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
15781da177e4SLinus Torvalds 
1579e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
15802ca3310eSAlasdair G Kergon 
158173d410c0SMilan Broz 	if (dm_suspended(md)) {
158273d410c0SMilan Broz 		r = -EINVAL;
1583d287483dSAlasdair G Kergon 		goto out_unlock;
158473d410c0SMilan Broz 	}
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 	map = dm_get_table(md);
1587cf222b37SAlasdair G Kergon 
15882e93ccc1SKiyoshi Ueda 	/*
15892e93ccc1SKiyoshi Ueda 	 * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
15902e93ccc1SKiyoshi Ueda 	 * This flag is cleared before dm_suspend returns.
15912e93ccc1SKiyoshi Ueda 	 */
15922e93ccc1SKiyoshi Ueda 	if (noflush)
15932e93ccc1SKiyoshi Ueda 		set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
15942e93ccc1SKiyoshi Ueda 
1595436d4108SAlasdair G Kergon 	/* This does not get reverted if there's an error later. */
15961da177e4SLinus Torvalds 	dm_table_presuspend_targets(map);
15971da177e4SLinus Torvalds 
15982e93ccc1SKiyoshi Ueda 	/*
15996d6f10dfSMilan Broz 	 * Flush I/O to the device. noflush supersedes do_lockfs,
16006d6f10dfSMilan Broz 	 * because lock_fs() needs to flush I/Os.
16012e93ccc1SKiyoshi Ueda 	 */
160232a926daSMikulas Patocka 	if (!noflush && do_lockfs) {
16032ca3310eSAlasdair G Kergon 		r = lock_fs(md);
16042ca3310eSAlasdair G Kergon 		if (r)
16052ca3310eSAlasdair G Kergon 			goto out;
1606aa8d7c2fSAlasdair G Kergon 	}
16071da177e4SLinus Torvalds 
16081da177e4SLinus Torvalds 	/*
16093b00b203SMikulas Patocka 	 * Here we must make sure that no processes are submitting requests
16103b00b203SMikulas Patocka 	 * to target drivers i.e. no one may be executing
16113b00b203SMikulas Patocka 	 * __split_and_process_bio. This is called from dm_request and
16123b00b203SMikulas Patocka 	 * dm_wq_work.
16133b00b203SMikulas Patocka 	 *
16143b00b203SMikulas Patocka 	 * To get all processes out of __split_and_process_bio in dm_request,
16153b00b203SMikulas Patocka 	 * we take the write lock. To prevent any process from reentering
16163b00b203SMikulas Patocka 	 * __split_and_process_bio from dm_request, we set
16173b00b203SMikulas Patocka 	 * DMF_QUEUE_IO_TO_THREAD.
16183b00b203SMikulas Patocka 	 *
16193b00b203SMikulas Patocka 	 * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
16203b00b203SMikulas Patocka 	 * and call flush_workqueue(md->wq). flush_workqueue will wait until
16213b00b203SMikulas Patocka 	 * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
16223b00b203SMikulas Patocka 	 * further calls to __split_and_process_bio from dm_wq_work.
16231da177e4SLinus Torvalds 	 */
16242ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
16251eb787ecSAlasdair G Kergon 	set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
16261eb787ecSAlasdair G Kergon 	set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
16272ca3310eSAlasdair G Kergon 	up_write(&md->io_lock);
16281da177e4SLinus Torvalds 
16293b00b203SMikulas Patocka 	flush_workqueue(md->wq);
16303b00b203SMikulas Patocka 
16311da177e4SLinus Torvalds 	/*
16323b00b203SMikulas Patocka 	 * At this point no more requests are entering target request routines.
16333b00b203SMikulas Patocka 	 * We call dm_wait_for_completion to wait for all existing requests
16343b00b203SMikulas Patocka 	 * to finish.
16351da177e4SLinus Torvalds 	 */
1636401600dfSMikulas Patocka 	r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
16371da177e4SLinus Torvalds 
16382ca3310eSAlasdair G Kergon 	down_write(&md->io_lock);
16396d6f10dfSMilan Broz 	if (noflush)
1640022c2611SMikulas Patocka 		clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
164194d6351eSMilan Broz 	up_write(&md->io_lock);
16422e93ccc1SKiyoshi Ueda 
16431da177e4SLinus Torvalds 	/* were we interrupted ? */
164446125c1cSMilan Broz 	if (r < 0) {
16459a1fb464SMikulas Patocka 		dm_queue_flush(md);
164673d410c0SMilan Broz 
16472ca3310eSAlasdair G Kergon 		unlock_fs(md);
16482e93ccc1SKiyoshi Ueda 		goto out; /* pushback list is already flushed, so skip flush */
16492ca3310eSAlasdair G Kergon 	}
16502ca3310eSAlasdair G Kergon 
16513b00b203SMikulas Patocka 	/*
16523b00b203SMikulas Patocka 	 * If dm_wait_for_completion returned 0, the device is completely
16533b00b203SMikulas Patocka 	 * quiescent now. There is no request-processing activity. All new
16543b00b203SMikulas Patocka 	 * requests are being added to md->deferred list.
16553b00b203SMikulas Patocka 	 */
16563b00b203SMikulas Patocka 
16572ca3310eSAlasdair G Kergon 	dm_table_postsuspend_targets(map);
16581da177e4SLinus Torvalds 
16591da177e4SLinus Torvalds 	set_bit(DMF_SUSPENDED, &md->flags);
16601da177e4SLinus Torvalds 
16612ca3310eSAlasdair G Kergon out:
16621da177e4SLinus Torvalds 	dm_table_put(map);
1663d287483dSAlasdair G Kergon 
1664d287483dSAlasdair G Kergon out_unlock:
1665e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
1666cf222b37SAlasdair G Kergon 	return r;
16671da177e4SLinus Torvalds }
16681da177e4SLinus Torvalds 
16691da177e4SLinus Torvalds int dm_resume(struct mapped_device *md)
16701da177e4SLinus Torvalds {
1671cf222b37SAlasdair G Kergon 	int r = -EINVAL;
1672cf222b37SAlasdair G Kergon 	struct dm_table *map = NULL;
16731da177e4SLinus Torvalds 
1674e61290a4SDaniel Walker 	mutex_lock(&md->suspend_lock);
16752ca3310eSAlasdair G Kergon 	if (!dm_suspended(md))
1676cf222b37SAlasdair G Kergon 		goto out;
1677cf222b37SAlasdair G Kergon 
1678cf222b37SAlasdair G Kergon 	map = dm_get_table(md);
16792ca3310eSAlasdair G Kergon 	if (!map || !dm_table_get_size(map))
1680cf222b37SAlasdair G Kergon 		goto out;
16811da177e4SLinus Torvalds 
16828757b776SMilan Broz 	r = dm_table_resume_targets(map);
16838757b776SMilan Broz 	if (r)
16848757b776SMilan Broz 		goto out;
16852ca3310eSAlasdair G Kergon 
16869a1fb464SMikulas Patocka 	dm_queue_flush(md);
16872ca3310eSAlasdair G Kergon 
16882ca3310eSAlasdair G Kergon 	unlock_fs(md);
16892ca3310eSAlasdair G Kergon 
16902ca3310eSAlasdair G Kergon 	clear_bit(DMF_SUSPENDED, &md->flags);
16912ca3310eSAlasdair G Kergon 
16921da177e4SLinus Torvalds 	dm_table_unplug_all(map);
16931da177e4SLinus Torvalds 
169469267a30SAlasdair G Kergon 	dm_kobject_uevent(md);
16958560ed6fSHannes Reinecke 
1696cf222b37SAlasdair G Kergon 	r = 0;
16972ca3310eSAlasdair G Kergon 
1698cf222b37SAlasdair G Kergon out:
1699cf222b37SAlasdair G Kergon 	dm_table_put(map);
1700e61290a4SDaniel Walker 	mutex_unlock(&md->suspend_lock);
17012ca3310eSAlasdair G Kergon 
1702cf222b37SAlasdair G Kergon 	return r;
17031da177e4SLinus Torvalds }
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds /*-----------------------------------------------------------------
17061da177e4SLinus Torvalds  * Event notification.
17071da177e4SLinus Torvalds  *---------------------------------------------------------------*/
170869267a30SAlasdair G Kergon void dm_kobject_uevent(struct mapped_device *md)
170969267a30SAlasdair G Kergon {
1710ed9e1982STejun Heo 	kobject_uevent(&disk_to_dev(md->disk)->kobj, KOBJ_CHANGE);
171169267a30SAlasdair G Kergon }
171269267a30SAlasdair G Kergon 
17137a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md)
17147a8c3d3bSMike Anderson {
17157a8c3d3bSMike Anderson 	return atomic_add_return(1, &md->uevent_seq);
17167a8c3d3bSMike Anderson }
17177a8c3d3bSMike Anderson 
17181da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md)
17191da177e4SLinus Torvalds {
17201da177e4SLinus Torvalds 	return atomic_read(&md->event_nr);
17211da177e4SLinus Torvalds }
17221da177e4SLinus Torvalds 
17231da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr)
17241da177e4SLinus Torvalds {
17251da177e4SLinus Torvalds 	return wait_event_interruptible(md->eventq,
17261da177e4SLinus Torvalds 			(event_nr != atomic_read(&md->event_nr)));
17271da177e4SLinus Torvalds }
17281da177e4SLinus Torvalds 
17297a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
17307a8c3d3bSMike Anderson {
17317a8c3d3bSMike Anderson 	unsigned long flags;
17327a8c3d3bSMike Anderson 
17337a8c3d3bSMike Anderson 	spin_lock_irqsave(&md->uevent_lock, flags);
17347a8c3d3bSMike Anderson 	list_add(elist, &md->uevent_list);
17357a8c3d3bSMike Anderson 	spin_unlock_irqrestore(&md->uevent_lock, flags);
17367a8c3d3bSMike Anderson }
17377a8c3d3bSMike Anderson 
17381da177e4SLinus Torvalds /*
17391da177e4SLinus Torvalds  * The gendisk is only valid as long as you have a reference
17401da177e4SLinus Torvalds  * count on 'md'.
17411da177e4SLinus Torvalds  */
17421da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md)
17431da177e4SLinus Torvalds {
17441da177e4SLinus Torvalds 	return md->disk;
17451da177e4SLinus Torvalds }
17461da177e4SLinus Torvalds 
1747784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md)
1748784aae73SMilan Broz {
1749784aae73SMilan Broz 	return &md->kobj;
1750784aae73SMilan Broz }
1751784aae73SMilan Broz 
1752784aae73SMilan Broz /*
1753784aae73SMilan Broz  * struct mapped_device should not be exported outside of dm.c
1754784aae73SMilan Broz  * so use this check to verify that kobj is part of md structure
1755784aae73SMilan Broz  */
1756784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
1757784aae73SMilan Broz {
1758784aae73SMilan Broz 	struct mapped_device *md;
1759784aae73SMilan Broz 
1760784aae73SMilan Broz 	md = container_of(kobj, struct mapped_device, kobj);
1761784aae73SMilan Broz 	if (&md->kobj != kobj)
1762784aae73SMilan Broz 		return NULL;
1763784aae73SMilan Broz 
17644d89b7b4SMilan Broz 	if (test_bit(DMF_FREEING, &md->flags) ||
17654d89b7b4SMilan Broz 	    test_bit(DMF_DELETING, &md->flags))
17664d89b7b4SMilan Broz 		return NULL;
17674d89b7b4SMilan Broz 
1768784aae73SMilan Broz 	dm_get(md);
1769784aae73SMilan Broz 	return md;
1770784aae73SMilan Broz }
1771784aae73SMilan Broz 
17721da177e4SLinus Torvalds int dm_suspended(struct mapped_device *md)
17731da177e4SLinus Torvalds {
17741da177e4SLinus Torvalds 	return test_bit(DMF_SUSPENDED, &md->flags);
17751da177e4SLinus Torvalds }
17761da177e4SLinus Torvalds 
17772e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti)
17782e93ccc1SKiyoshi Ueda {
17792e93ccc1SKiyoshi Ueda 	struct mapped_device *md = dm_table_get_md(ti->table);
17802e93ccc1SKiyoshi Ueda 	int r = __noflush_suspending(md);
17812e93ccc1SKiyoshi Ueda 
17822e93ccc1SKiyoshi Ueda 	dm_put(md);
17832e93ccc1SKiyoshi Ueda 
17842e93ccc1SKiyoshi Ueda 	return r;
17852e93ccc1SKiyoshi Ueda }
17862e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending);
17872e93ccc1SKiyoshi Ueda 
17881da177e4SLinus Torvalds static struct block_device_operations dm_blk_dops = {
17891da177e4SLinus Torvalds 	.open = dm_blk_open,
17901da177e4SLinus Torvalds 	.release = dm_blk_close,
1791aa129a22SMilan Broz 	.ioctl = dm_blk_ioctl,
17923ac51e74SDarrick J. Wong 	.getgeo = dm_blk_getgeo,
17931da177e4SLinus Torvalds 	.owner = THIS_MODULE
17941da177e4SLinus Torvalds };
17951da177e4SLinus Torvalds 
17961da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo);
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds /*
17991da177e4SLinus Torvalds  * module hooks
18001da177e4SLinus Torvalds  */
18011da177e4SLinus Torvalds module_init(dm_init);
18021da177e4SLinus Torvalds module_exit(dm_exit);
18031da177e4SLinus Torvalds 
18041da177e4SLinus Torvalds module_param(major, uint, 0);
18051da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper");
18061da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver");
18071da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
18081da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1809