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 2760935eb2SMilan Broz /* 2860935eb2SMilan Broz * Cookies are numeric values sent with CHANGE and REMOVE 2960935eb2SMilan Broz * uevents while resuming, removing or renaming the device. 3060935eb2SMilan Broz */ 3160935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE" 3260935eb2SMilan Broz #define DM_COOKIE_LENGTH 24 3360935eb2SMilan Broz 341da177e4SLinus Torvalds static const char *_name = DM_NAME; 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds static unsigned int major = 0; 371da177e4SLinus Torvalds static unsigned int _major = 0; 381da177e4SLinus Torvalds 39f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock); 401da177e4SLinus Torvalds /* 418fbf26adSKiyoshi Ueda * For bio-based dm. 421da177e4SLinus Torvalds * One of these is allocated per bio. 431da177e4SLinus Torvalds */ 441da177e4SLinus Torvalds struct dm_io { 451da177e4SLinus Torvalds struct mapped_device *md; 461da177e4SLinus Torvalds int error; 471da177e4SLinus Torvalds atomic_t io_count; 486ae2fa67SRichard Kennedy struct bio *bio; 493eaf840eSJun'ichi "Nick" Nomura unsigned long start_time; 50f88fb981SKiyoshi Ueda spinlock_t endio_lock; 511da177e4SLinus Torvalds }; 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds /* 548fbf26adSKiyoshi Ueda * For bio-based dm. 551da177e4SLinus Torvalds * One of these is allocated per target within a bio. Hopefully 561da177e4SLinus Torvalds * this will be simplified out one day. 571da177e4SLinus Torvalds */ 58028867acSAlasdair G Kergon struct dm_target_io { 591da177e4SLinus Torvalds struct dm_io *io; 601da177e4SLinus Torvalds struct dm_target *ti; 611da177e4SLinus Torvalds union map_info info; 621da177e4SLinus Torvalds }; 631da177e4SLinus Torvalds 648fbf26adSKiyoshi Ueda /* 658fbf26adSKiyoshi Ueda * For request-based dm. 668fbf26adSKiyoshi Ueda * One of these is allocated per request. 678fbf26adSKiyoshi Ueda */ 688fbf26adSKiyoshi Ueda struct dm_rq_target_io { 698fbf26adSKiyoshi Ueda struct mapped_device *md; 708fbf26adSKiyoshi Ueda struct dm_target *ti; 718fbf26adSKiyoshi Ueda struct request *orig, clone; 728fbf26adSKiyoshi Ueda int error; 738fbf26adSKiyoshi Ueda union map_info info; 748fbf26adSKiyoshi Ueda }; 758fbf26adSKiyoshi Ueda 768fbf26adSKiyoshi Ueda /* 778fbf26adSKiyoshi Ueda * For request-based dm. 788fbf26adSKiyoshi Ueda * One of these is allocated per bio. 798fbf26adSKiyoshi Ueda */ 808fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info { 818fbf26adSKiyoshi Ueda struct bio *orig; 82cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio; 838fbf26adSKiyoshi Ueda }; 848fbf26adSKiyoshi Ueda 851da177e4SLinus Torvalds union map_info *dm_get_mapinfo(struct bio *bio) 861da177e4SLinus Torvalds { 871da177e4SLinus Torvalds if (bio && bio->bi_private) 88028867acSAlasdair G Kergon return &((struct dm_target_io *)bio->bi_private)->info; 891da177e4SLinus Torvalds return NULL; 901da177e4SLinus Torvalds } 911da177e4SLinus Torvalds 92cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq) 93cec47e3dSKiyoshi Ueda { 94cec47e3dSKiyoshi Ueda if (rq && rq->end_io_data) 95cec47e3dSKiyoshi Ueda return &((struct dm_rq_target_io *)rq->end_io_data)->info; 96cec47e3dSKiyoshi Ueda return NULL; 97cec47e3dSKiyoshi Ueda } 98cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo); 99cec47e3dSKiyoshi Ueda 100ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1) 101ba61fdd1SJeff Mahoney 1021da177e4SLinus Torvalds /* 1031da177e4SLinus Torvalds * Bits for the md->flags field. 1041da177e4SLinus Torvalds */ 1051eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0 1061da177e4SLinus Torvalds #define DMF_SUSPENDED 1 107aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2 108fba9f90eSJeff Mahoney #define DMF_FREEING 3 1095c6bd75dSAlasdair G Kergon #define DMF_DELETING 4 1102e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5 1111eb787ecSAlasdair G Kergon #define DMF_QUEUE_IO_TO_THREAD 6 1121da177e4SLinus Torvalds 113304f3f6aSMilan Broz /* 114304f3f6aSMilan Broz * Work processed by per-device workqueue. 115304f3f6aSMilan Broz */ 1161da177e4SLinus Torvalds struct mapped_device { 1172ca3310eSAlasdair G Kergon struct rw_semaphore io_lock; 118e61290a4SDaniel Walker struct mutex suspend_lock; 1191da177e4SLinus Torvalds rwlock_t map_lock; 1201da177e4SLinus Torvalds atomic_t holders; 1215c6bd75dSAlasdair G Kergon atomic_t open_count; 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds unsigned long flags; 1241da177e4SLinus Torvalds 125165125e1SJens Axboe struct request_queue *queue; 1261da177e4SLinus Torvalds struct gendisk *disk; 1277e51f257SMike Anderson char name[16]; 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds void *interface_ptr; 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds /* 1321da177e4SLinus Torvalds * A list of ios that arrived while we were suspended. 1331da177e4SLinus Torvalds */ 134316d315bSNikanth Karthikesan atomic_t pending[2]; 1351da177e4SLinus Torvalds wait_queue_head_t wait; 13653d5914fSMikulas Patocka struct work_struct work; 1371da177e4SLinus Torvalds struct bio_list deferred; 138022c2611SMikulas Patocka spinlock_t deferred_lock; 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds /* 141af7e466aSMikulas Patocka * An error from the barrier request currently being processed. 142af7e466aSMikulas Patocka */ 143af7e466aSMikulas Patocka int barrier_error; 144af7e466aSMikulas Patocka 145af7e466aSMikulas Patocka /* 146304f3f6aSMilan Broz * Processing queue (flush/barriers) 147304f3f6aSMilan Broz */ 148304f3f6aSMilan Broz struct workqueue_struct *wq; 149304f3f6aSMilan Broz 150304f3f6aSMilan Broz /* 1511da177e4SLinus Torvalds * The current mapping. 1521da177e4SLinus Torvalds */ 1531da177e4SLinus Torvalds struct dm_table *map; 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds /* 1561da177e4SLinus Torvalds * io objects are allocated from here. 1571da177e4SLinus Torvalds */ 1581da177e4SLinus Torvalds mempool_t *io_pool; 1591da177e4SLinus Torvalds mempool_t *tio_pool; 1601da177e4SLinus Torvalds 1619faf400fSStefan Bader struct bio_set *bs; 1629faf400fSStefan Bader 1631da177e4SLinus Torvalds /* 1641da177e4SLinus Torvalds * Event handling. 1651da177e4SLinus Torvalds */ 1661da177e4SLinus Torvalds atomic_t event_nr; 1671da177e4SLinus Torvalds wait_queue_head_t eventq; 1687a8c3d3bSMike Anderson atomic_t uevent_seq; 1697a8c3d3bSMike Anderson struct list_head uevent_list; 1707a8c3d3bSMike Anderson spinlock_t uevent_lock; /* Protect access to uevent_list */ 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds /* 1731da177e4SLinus Torvalds * freeze/thaw support require holding onto a super block 1741da177e4SLinus Torvalds */ 1751da177e4SLinus Torvalds struct super_block *frozen_sb; 176db8fef4fSMikulas Patocka struct block_device *bdev; 1773ac51e74SDarrick J. Wong 1783ac51e74SDarrick J. Wong /* forced geometry settings */ 1793ac51e74SDarrick J. Wong struct hd_geometry geometry; 180784aae73SMilan Broz 181cec47e3dSKiyoshi Ueda /* For saving the address of __make_request for request based dm */ 182cec47e3dSKiyoshi Ueda make_request_fn *saved_make_request_fn; 183cec47e3dSKiyoshi Ueda 184784aae73SMilan Broz /* sysfs handle */ 185784aae73SMilan Broz struct kobject kobj; 18652b1fd5aSMikulas Patocka 18752b1fd5aSMikulas Patocka /* zero-length barrier that will be cloned and submitted to targets */ 18852b1fd5aSMikulas Patocka struct bio barrier_bio; 1891da177e4SLinus Torvalds }; 1901da177e4SLinus Torvalds 191e6ee8c0bSKiyoshi Ueda /* 192e6ee8c0bSKiyoshi Ueda * For mempools pre-allocation at the table loading time. 193e6ee8c0bSKiyoshi Ueda */ 194e6ee8c0bSKiyoshi Ueda struct dm_md_mempools { 195e6ee8c0bSKiyoshi Ueda mempool_t *io_pool; 196e6ee8c0bSKiyoshi Ueda mempool_t *tio_pool; 197e6ee8c0bSKiyoshi Ueda struct bio_set *bs; 198e6ee8c0bSKiyoshi Ueda }; 199e6ee8c0bSKiyoshi Ueda 2001da177e4SLinus Torvalds #define MIN_IOS 256 201e18b890bSChristoph Lameter static struct kmem_cache *_io_cache; 202e18b890bSChristoph Lameter static struct kmem_cache *_tio_cache; 2038fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache; 2048fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_bio_info_cache; 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds static int __init local_init(void) 2071da177e4SLinus Torvalds { 20851157b4aSKiyoshi Ueda int r = -ENOMEM; 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds /* allocate a slab for the dm_ios */ 211028867acSAlasdair G Kergon _io_cache = KMEM_CACHE(dm_io, 0); 2121da177e4SLinus Torvalds if (!_io_cache) 21351157b4aSKiyoshi Ueda return r; 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds /* allocate a slab for the target ios */ 216028867acSAlasdair G Kergon _tio_cache = KMEM_CACHE(dm_target_io, 0); 21751157b4aSKiyoshi Ueda if (!_tio_cache) 21851157b4aSKiyoshi Ueda goto out_free_io_cache; 2191da177e4SLinus Torvalds 2208fbf26adSKiyoshi Ueda _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0); 2218fbf26adSKiyoshi Ueda if (!_rq_tio_cache) 2228fbf26adSKiyoshi Ueda goto out_free_tio_cache; 2238fbf26adSKiyoshi Ueda 2248fbf26adSKiyoshi Ueda _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0); 2258fbf26adSKiyoshi Ueda if (!_rq_bio_info_cache) 2268fbf26adSKiyoshi Ueda goto out_free_rq_tio_cache; 2278fbf26adSKiyoshi Ueda 22851e5b2bdSMike Anderson r = dm_uevent_init(); 22951157b4aSKiyoshi Ueda if (r) 2308fbf26adSKiyoshi Ueda goto out_free_rq_bio_info_cache; 23151e5b2bdSMike Anderson 2321da177e4SLinus Torvalds _major = major; 2331da177e4SLinus Torvalds r = register_blkdev(_major, _name); 23451157b4aSKiyoshi Ueda if (r < 0) 23551157b4aSKiyoshi Ueda goto out_uevent_exit; 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds if (!_major) 2381da177e4SLinus Torvalds _major = r; 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds return 0; 24151157b4aSKiyoshi Ueda 24251157b4aSKiyoshi Ueda out_uevent_exit: 24351157b4aSKiyoshi Ueda dm_uevent_exit(); 2448fbf26adSKiyoshi Ueda out_free_rq_bio_info_cache: 2458fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_bio_info_cache); 2468fbf26adSKiyoshi Ueda out_free_rq_tio_cache: 2478fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_tio_cache); 24851157b4aSKiyoshi Ueda out_free_tio_cache: 24951157b4aSKiyoshi Ueda kmem_cache_destroy(_tio_cache); 25051157b4aSKiyoshi Ueda out_free_io_cache: 25151157b4aSKiyoshi Ueda kmem_cache_destroy(_io_cache); 25251157b4aSKiyoshi Ueda 25351157b4aSKiyoshi Ueda return r; 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds static void local_exit(void) 2571da177e4SLinus Torvalds { 2588fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_bio_info_cache); 2598fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_tio_cache); 2601da177e4SLinus Torvalds kmem_cache_destroy(_tio_cache); 2611da177e4SLinus Torvalds kmem_cache_destroy(_io_cache); 26200d59405SAkinobu Mita unregister_blkdev(_major, _name); 26351e5b2bdSMike Anderson dm_uevent_exit(); 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds _major = 0; 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds DMINFO("cleaned up"); 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds 270b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = { 2711da177e4SLinus Torvalds local_init, 2721da177e4SLinus Torvalds dm_target_init, 2731da177e4SLinus Torvalds dm_linear_init, 2741da177e4SLinus Torvalds dm_stripe_init, 275952b3557SMikulas Patocka dm_io_init, 276945fa4d2SMikulas Patocka dm_kcopyd_init, 2771da177e4SLinus Torvalds dm_interface_init, 2781da177e4SLinus Torvalds }; 2791da177e4SLinus Torvalds 280b9249e55SAlasdair G Kergon static void (*_exits[])(void) = { 2811da177e4SLinus Torvalds local_exit, 2821da177e4SLinus Torvalds dm_target_exit, 2831da177e4SLinus Torvalds dm_linear_exit, 2841da177e4SLinus Torvalds dm_stripe_exit, 285952b3557SMikulas Patocka dm_io_exit, 286945fa4d2SMikulas Patocka dm_kcopyd_exit, 2871da177e4SLinus Torvalds dm_interface_exit, 2881da177e4SLinus Torvalds }; 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds static int __init dm_init(void) 2911da177e4SLinus Torvalds { 2921da177e4SLinus Torvalds const int count = ARRAY_SIZE(_inits); 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds int r, i; 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds for (i = 0; i < count; i++) { 2971da177e4SLinus Torvalds r = _inits[i](); 2981da177e4SLinus Torvalds if (r) 2991da177e4SLinus Torvalds goto bad; 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds return 0; 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds bad: 3051da177e4SLinus Torvalds while (i--) 3061da177e4SLinus Torvalds _exits[i](); 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds return r; 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds static void __exit dm_exit(void) 3121da177e4SLinus Torvalds { 3131da177e4SLinus Torvalds int i = ARRAY_SIZE(_exits); 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds while (i--) 3161da177e4SLinus Torvalds _exits[i](); 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds /* 3201da177e4SLinus Torvalds * Block device functions 3211da177e4SLinus Torvalds */ 322fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode) 3231da177e4SLinus Torvalds { 3241da177e4SLinus Torvalds struct mapped_device *md; 3251da177e4SLinus Torvalds 326fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 327fba9f90eSJeff Mahoney 328fe5f9f2cSAl Viro md = bdev->bd_disk->private_data; 329fba9f90eSJeff Mahoney if (!md) 330fba9f90eSJeff Mahoney goto out; 331fba9f90eSJeff Mahoney 3325c6bd75dSAlasdair G Kergon if (test_bit(DMF_FREEING, &md->flags) || 3335c6bd75dSAlasdair G Kergon test_bit(DMF_DELETING, &md->flags)) { 334fba9f90eSJeff Mahoney md = NULL; 335fba9f90eSJeff Mahoney goto out; 336fba9f90eSJeff Mahoney } 337fba9f90eSJeff Mahoney 3381da177e4SLinus Torvalds dm_get(md); 3395c6bd75dSAlasdair G Kergon atomic_inc(&md->open_count); 340fba9f90eSJeff Mahoney 341fba9f90eSJeff Mahoney out: 342fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 343fba9f90eSJeff Mahoney 344fba9f90eSJeff Mahoney return md ? 0 : -ENXIO; 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds 347fe5f9f2cSAl Viro static int dm_blk_close(struct gendisk *disk, fmode_t mode) 3481da177e4SLinus Torvalds { 349fe5f9f2cSAl Viro struct mapped_device *md = disk->private_data; 3505c6bd75dSAlasdair G Kergon atomic_dec(&md->open_count); 3511da177e4SLinus Torvalds dm_put(md); 3521da177e4SLinus Torvalds return 0; 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds 3555c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md) 3565c6bd75dSAlasdair G Kergon { 3575c6bd75dSAlasdair G Kergon return atomic_read(&md->open_count); 3585c6bd75dSAlasdair G Kergon } 3595c6bd75dSAlasdair G Kergon 3605c6bd75dSAlasdair G Kergon /* 3615c6bd75dSAlasdair G Kergon * Guarantees nothing is using the device before it's deleted. 3625c6bd75dSAlasdair G Kergon */ 3635c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md) 3645c6bd75dSAlasdair G Kergon { 3655c6bd75dSAlasdair G Kergon int r = 0; 3665c6bd75dSAlasdair G Kergon 3675c6bd75dSAlasdair G Kergon spin_lock(&_minor_lock); 3685c6bd75dSAlasdair G Kergon 3695c6bd75dSAlasdair G Kergon if (dm_open_count(md)) 3705c6bd75dSAlasdair G Kergon r = -EBUSY; 3715c6bd75dSAlasdair G Kergon else 3725c6bd75dSAlasdair G Kergon set_bit(DMF_DELETING, &md->flags); 3735c6bd75dSAlasdair G Kergon 3745c6bd75dSAlasdair G Kergon spin_unlock(&_minor_lock); 3755c6bd75dSAlasdair G Kergon 3765c6bd75dSAlasdair G Kergon return r; 3775c6bd75dSAlasdair G Kergon } 3785c6bd75dSAlasdair G Kergon 3793ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 3803ac51e74SDarrick J. Wong { 3813ac51e74SDarrick J. Wong struct mapped_device *md = bdev->bd_disk->private_data; 3823ac51e74SDarrick J. Wong 3833ac51e74SDarrick J. Wong return dm_get_geometry(md, geo); 3843ac51e74SDarrick J. Wong } 3853ac51e74SDarrick J. Wong 386fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode, 387aa129a22SMilan Broz unsigned int cmd, unsigned long arg) 388aa129a22SMilan Broz { 389fe5f9f2cSAl Viro struct mapped_device *md = bdev->bd_disk->private_data; 390fe5f9f2cSAl Viro struct dm_table *map = dm_get_table(md); 391aa129a22SMilan Broz struct dm_target *tgt; 392aa129a22SMilan Broz int r = -ENOTTY; 393aa129a22SMilan Broz 394aa129a22SMilan Broz if (!map || !dm_table_get_size(map)) 395aa129a22SMilan Broz goto out; 396aa129a22SMilan Broz 397aa129a22SMilan Broz /* We only support devices that have a single target */ 398aa129a22SMilan Broz if (dm_table_get_num_targets(map) != 1) 399aa129a22SMilan Broz goto out; 400aa129a22SMilan Broz 401aa129a22SMilan Broz tgt = dm_table_get_target(map, 0); 402aa129a22SMilan Broz 403aa129a22SMilan Broz if (dm_suspended(md)) { 404aa129a22SMilan Broz r = -EAGAIN; 405aa129a22SMilan Broz goto out; 406aa129a22SMilan Broz } 407aa129a22SMilan Broz 408aa129a22SMilan Broz if (tgt->type->ioctl) 409647b3d00SAl Viro r = tgt->type->ioctl(tgt, cmd, arg); 410aa129a22SMilan Broz 411aa129a22SMilan Broz out: 412aa129a22SMilan Broz dm_table_put(map); 413aa129a22SMilan Broz 414aa129a22SMilan Broz return r; 415aa129a22SMilan Broz } 416aa129a22SMilan Broz 417028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md) 4181da177e4SLinus Torvalds { 4191da177e4SLinus Torvalds return mempool_alloc(md->io_pool, GFP_NOIO); 4201da177e4SLinus Torvalds } 4211da177e4SLinus Torvalds 422028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io) 4231da177e4SLinus Torvalds { 4241da177e4SLinus Torvalds mempool_free(io, md->io_pool); 4251da177e4SLinus Torvalds } 4261da177e4SLinus Torvalds 427028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio) 4281da177e4SLinus Torvalds { 4291da177e4SLinus Torvalds mempool_free(tio, md->tio_pool); 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds 43208885643SKiyoshi Ueda static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md, 43308885643SKiyoshi Ueda gfp_t gfp_mask) 434cec47e3dSKiyoshi Ueda { 43508885643SKiyoshi Ueda return mempool_alloc(md->tio_pool, gfp_mask); 436cec47e3dSKiyoshi Ueda } 437cec47e3dSKiyoshi Ueda 438cec47e3dSKiyoshi Ueda static void free_rq_tio(struct dm_rq_target_io *tio) 439cec47e3dSKiyoshi Ueda { 440cec47e3dSKiyoshi Ueda mempool_free(tio, tio->md->tio_pool); 441cec47e3dSKiyoshi Ueda } 442cec47e3dSKiyoshi Ueda 443cec47e3dSKiyoshi Ueda static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md) 444cec47e3dSKiyoshi Ueda { 445cec47e3dSKiyoshi Ueda return mempool_alloc(md->io_pool, GFP_ATOMIC); 446cec47e3dSKiyoshi Ueda } 447cec47e3dSKiyoshi Ueda 448cec47e3dSKiyoshi Ueda static void free_bio_info(struct dm_rq_clone_bio_info *info) 449cec47e3dSKiyoshi Ueda { 450cec47e3dSKiyoshi Ueda mempool_free(info, info->tio->md->io_pool); 451cec47e3dSKiyoshi Ueda } 452cec47e3dSKiyoshi Ueda 45390abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md) 45490abb8c4SKiyoshi Ueda { 45590abb8c4SKiyoshi Ueda return atomic_read(&md->pending[READ]) + 45690abb8c4SKiyoshi Ueda atomic_read(&md->pending[WRITE]); 45790abb8c4SKiyoshi Ueda } 45890abb8c4SKiyoshi Ueda 4593eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io) 4603eaf840eSJun'ichi "Nick" Nomura { 4613eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 462c9959059STejun Heo int cpu; 463316d315bSNikanth Karthikesan int rw = bio_data_dir(io->bio); 4643eaf840eSJun'ichi "Nick" Nomura 4653eaf840eSJun'ichi "Nick" Nomura io->start_time = jiffies; 4663eaf840eSJun'ichi "Nick" Nomura 467074a7acaSTejun Heo cpu = part_stat_lock(); 468074a7acaSTejun Heo part_round_stats(cpu, &dm_disk(md)->part0); 469074a7acaSTejun Heo part_stat_unlock(); 470316d315bSNikanth Karthikesan dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]); 4713eaf840eSJun'ichi "Nick" Nomura } 4723eaf840eSJun'ichi "Nick" Nomura 473d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io) 4743eaf840eSJun'ichi "Nick" Nomura { 4753eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 4763eaf840eSJun'ichi "Nick" Nomura struct bio *bio = io->bio; 4773eaf840eSJun'ichi "Nick" Nomura unsigned long duration = jiffies - io->start_time; 478c9959059STejun Heo int pending, cpu; 4793eaf840eSJun'ichi "Nick" Nomura int rw = bio_data_dir(bio); 4803eaf840eSJun'ichi "Nick" Nomura 481074a7acaSTejun Heo cpu = part_stat_lock(); 482074a7acaSTejun Heo part_round_stats(cpu, &dm_disk(md)->part0); 483074a7acaSTejun Heo part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration); 484074a7acaSTejun Heo part_stat_unlock(); 4853eaf840eSJun'ichi "Nick" Nomura 486af7e466aSMikulas Patocka /* 487af7e466aSMikulas Patocka * After this is decremented the bio must not be touched if it is 488af7e466aSMikulas Patocka * a barrier. 489af7e466aSMikulas Patocka */ 490316d315bSNikanth Karthikesan dm_disk(md)->part0.in_flight[rw] = pending = 491316d315bSNikanth Karthikesan atomic_dec_return(&md->pending[rw]); 492316d315bSNikanth Karthikesan pending += atomic_read(&md->pending[rw^0x1]); 4933eaf840eSJun'ichi "Nick" Nomura 494d221d2e7SMikulas Patocka /* nudge anyone waiting on suspend queue */ 495d221d2e7SMikulas Patocka if (!pending) 496d221d2e7SMikulas Patocka wake_up(&md->wait); 4973eaf840eSJun'ichi "Nick" Nomura } 4983eaf840eSJun'ichi "Nick" Nomura 4991da177e4SLinus Torvalds /* 5001da177e4SLinus Torvalds * Add the bio to the list of deferred io. 5011da177e4SLinus Torvalds */ 50292c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio) 5031da177e4SLinus Torvalds { 5042ca3310eSAlasdair G Kergon down_write(&md->io_lock); 5051da177e4SLinus Torvalds 506022c2611SMikulas Patocka spin_lock_irq(&md->deferred_lock); 5071da177e4SLinus Torvalds bio_list_add(&md->deferred, bio); 508022c2611SMikulas Patocka spin_unlock_irq(&md->deferred_lock); 5091da177e4SLinus Torvalds 51092c63902SMikulas Patocka if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) 51192c63902SMikulas Patocka queue_work(md->wq, &md->work); 51292c63902SMikulas Patocka 5132ca3310eSAlasdair G Kergon up_write(&md->io_lock); 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds /* 5171da177e4SLinus Torvalds * Everyone (including functions in this file), should use this 5181da177e4SLinus Torvalds * function to access the md->map field, and make sure they call 5191da177e4SLinus Torvalds * dm_table_put() when finished. 5201da177e4SLinus Torvalds */ 5211da177e4SLinus Torvalds struct dm_table *dm_get_table(struct mapped_device *md) 5221da177e4SLinus Torvalds { 5231da177e4SLinus Torvalds struct dm_table *t; 524523d9297SKiyoshi Ueda unsigned long flags; 5251da177e4SLinus Torvalds 526523d9297SKiyoshi Ueda read_lock_irqsave(&md->map_lock, flags); 5271da177e4SLinus Torvalds t = md->map; 5281da177e4SLinus Torvalds if (t) 5291da177e4SLinus Torvalds dm_table_get(t); 530523d9297SKiyoshi Ueda read_unlock_irqrestore(&md->map_lock, flags); 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds return t; 5331da177e4SLinus Torvalds } 5341da177e4SLinus Torvalds 5353ac51e74SDarrick J. Wong /* 5363ac51e74SDarrick J. Wong * Get the geometry associated with a dm device 5373ac51e74SDarrick J. Wong */ 5383ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) 5393ac51e74SDarrick J. Wong { 5403ac51e74SDarrick J. Wong *geo = md->geometry; 5413ac51e74SDarrick J. Wong 5423ac51e74SDarrick J. Wong return 0; 5433ac51e74SDarrick J. Wong } 5443ac51e74SDarrick J. Wong 5453ac51e74SDarrick J. Wong /* 5463ac51e74SDarrick J. Wong * Set the geometry of a device. 5473ac51e74SDarrick J. Wong */ 5483ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) 5493ac51e74SDarrick J. Wong { 5503ac51e74SDarrick J. Wong sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; 5513ac51e74SDarrick J. Wong 5523ac51e74SDarrick J. Wong if (geo->start > sz) { 5533ac51e74SDarrick J. Wong DMWARN("Start sector is beyond the geometry limits."); 5543ac51e74SDarrick J. Wong return -EINVAL; 5553ac51e74SDarrick J. Wong } 5563ac51e74SDarrick J. Wong 5573ac51e74SDarrick J. Wong md->geometry = *geo; 5583ac51e74SDarrick J. Wong 5593ac51e74SDarrick J. Wong return 0; 5603ac51e74SDarrick J. Wong } 5613ac51e74SDarrick J. Wong 5621da177e4SLinus Torvalds /*----------------------------------------------------------------- 5631da177e4SLinus Torvalds * CRUD START: 5641da177e4SLinus Torvalds * A more elegant soln is in the works that uses the queue 5651da177e4SLinus Torvalds * merge fn, unfortunately there are a couple of changes to 5661da177e4SLinus Torvalds * the block layer that I want to make for this. So in the 5671da177e4SLinus Torvalds * interests of getting something for people to use I give 5681da177e4SLinus Torvalds * you this clearly demarcated crap. 5691da177e4SLinus Torvalds *---------------------------------------------------------------*/ 5701da177e4SLinus Torvalds 5712e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md) 5722e93ccc1SKiyoshi Ueda { 5732e93ccc1SKiyoshi Ueda return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 5742e93ccc1SKiyoshi Ueda } 5752e93ccc1SKiyoshi Ueda 5761da177e4SLinus Torvalds /* 5771da177e4SLinus Torvalds * Decrements the number of outstanding ios that a bio has been 5781da177e4SLinus Torvalds * cloned into, completing the original io if necc. 5791da177e4SLinus Torvalds */ 580858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error) 5811da177e4SLinus Torvalds { 5822e93ccc1SKiyoshi Ueda unsigned long flags; 583b35f8caaSMilan Broz int io_error; 584b35f8caaSMilan Broz struct bio *bio; 585b35f8caaSMilan Broz struct mapped_device *md = io->md; 5862e93ccc1SKiyoshi Ueda 5872e93ccc1SKiyoshi Ueda /* Push-back supersedes any I/O errors */ 588f88fb981SKiyoshi Ueda if (unlikely(error)) { 589f88fb981SKiyoshi Ueda spin_lock_irqsave(&io->endio_lock, flags); 590f88fb981SKiyoshi Ueda if (!(io->error > 0 && __noflush_suspending(md))) 5911da177e4SLinus Torvalds io->error = error; 592f88fb981SKiyoshi Ueda spin_unlock_irqrestore(&io->endio_lock, flags); 593f88fb981SKiyoshi Ueda } 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds if (atomic_dec_and_test(&io->io_count)) { 5962e93ccc1SKiyoshi Ueda if (io->error == DM_ENDIO_REQUEUE) { 5972e93ccc1SKiyoshi Ueda /* 5982e93ccc1SKiyoshi Ueda * Target requested pushing back the I/O. 5992e93ccc1SKiyoshi Ueda */ 600022c2611SMikulas Patocka spin_lock_irqsave(&md->deferred_lock, flags); 6012761e95fSMikulas Patocka if (__noflush_suspending(md)) { 6021f98a13fSJens Axboe if (!bio_rw_flagged(io->bio, BIO_RW_BARRIER)) 6032761e95fSMikulas Patocka bio_list_add_head(&md->deferred, 6042761e95fSMikulas Patocka io->bio); 6052761e95fSMikulas Patocka } else 6062e93ccc1SKiyoshi Ueda /* noflush suspend was interrupted. */ 6072e93ccc1SKiyoshi Ueda io->error = -EIO; 608022c2611SMikulas Patocka spin_unlock_irqrestore(&md->deferred_lock, flags); 6092e93ccc1SKiyoshi Ueda } 6102e93ccc1SKiyoshi Ueda 611b35f8caaSMilan Broz io_error = io->error; 612b35f8caaSMilan Broz bio = io->bio; 6132056a782SJens Axboe 6141f98a13fSJens Axboe if (bio_rw_flagged(bio, BIO_RW_BARRIER)) { 615af7e466aSMikulas Patocka /* 616af7e466aSMikulas Patocka * There can be just one barrier request so we use 617af7e466aSMikulas Patocka * a per-device variable for error reporting. 618af7e466aSMikulas Patocka * Note that you can't touch the bio after end_io_acct 619af7e466aSMikulas Patocka */ 620fdb9572bSMikulas Patocka if (!md->barrier_error && io_error != -EOPNOTSUPP) 621af7e466aSMikulas Patocka md->barrier_error = io_error; 622af7e466aSMikulas Patocka end_io_acct(io); 623af7e466aSMikulas Patocka } else { 624af7e466aSMikulas Patocka end_io_acct(io); 625b35f8caaSMilan Broz 626b35f8caaSMilan Broz if (io_error != DM_ENDIO_REQUEUE) { 627b35f8caaSMilan Broz trace_block_bio_complete(md->queue, bio); 628b35f8caaSMilan Broz 629b35f8caaSMilan Broz bio_endio(bio, io_error); 6302e93ccc1SKiyoshi Ueda } 6311da177e4SLinus Torvalds } 632af7e466aSMikulas Patocka 633af7e466aSMikulas Patocka free_io(md, io); 634af7e466aSMikulas Patocka } 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds 6376712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error) 6381da177e4SLinus Torvalds { 6391da177e4SLinus Torvalds int r = 0; 640028867acSAlasdair G Kergon struct dm_target_io *tio = bio->bi_private; 641b35f8caaSMilan Broz struct dm_io *io = tio->io; 6429faf400fSStefan Bader struct mapped_device *md = tio->io->md; 6431da177e4SLinus Torvalds dm_endio_fn endio = tio->ti->type->end_io; 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds if (!bio_flagged(bio, BIO_UPTODATE) && !error) 6461da177e4SLinus Torvalds error = -EIO; 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds if (endio) { 6491da177e4SLinus Torvalds r = endio(tio->ti, bio, error, &tio->info); 6502e93ccc1SKiyoshi Ueda if (r < 0 || r == DM_ENDIO_REQUEUE) 6512e93ccc1SKiyoshi Ueda /* 6522e93ccc1SKiyoshi Ueda * error and requeue request are handled 6532e93ccc1SKiyoshi Ueda * in dec_pending(). 6542e93ccc1SKiyoshi Ueda */ 6551da177e4SLinus Torvalds error = r; 65645cbcd79SKiyoshi Ueda else if (r == DM_ENDIO_INCOMPLETE) 65745cbcd79SKiyoshi Ueda /* The target will handle the io */ 6586712ecf8SNeilBrown return; 65945cbcd79SKiyoshi Ueda else if (r) { 66045cbcd79SKiyoshi Ueda DMWARN("unimplemented target endio return value: %d", r); 66145cbcd79SKiyoshi Ueda BUG(); 66245cbcd79SKiyoshi Ueda } 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds 6659faf400fSStefan Bader /* 6669faf400fSStefan Bader * Store md for cleanup instead of tio which is about to get freed. 6679faf400fSStefan Bader */ 6689faf400fSStefan Bader bio->bi_private = md->bs; 6699faf400fSStefan Bader 6709faf400fSStefan Bader free_tio(md, tio); 671b35f8caaSMilan Broz bio_put(bio); 672b35f8caaSMilan Broz dec_pending(io, error); 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds 675cec47e3dSKiyoshi Ueda /* 676cec47e3dSKiyoshi Ueda * Partial completion handling for request-based dm 677cec47e3dSKiyoshi Ueda */ 678cec47e3dSKiyoshi Ueda static void end_clone_bio(struct bio *clone, int error) 679cec47e3dSKiyoshi Ueda { 680cec47e3dSKiyoshi Ueda struct dm_rq_clone_bio_info *info = clone->bi_private; 681cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = info->tio; 682cec47e3dSKiyoshi Ueda struct bio *bio = info->orig; 683cec47e3dSKiyoshi Ueda unsigned int nr_bytes = info->orig->bi_size; 684cec47e3dSKiyoshi Ueda 685cec47e3dSKiyoshi Ueda bio_put(clone); 686cec47e3dSKiyoshi Ueda 687cec47e3dSKiyoshi Ueda if (tio->error) 688cec47e3dSKiyoshi Ueda /* 689cec47e3dSKiyoshi Ueda * An error has already been detected on the request. 690cec47e3dSKiyoshi Ueda * Once error occurred, just let clone->end_io() handle 691cec47e3dSKiyoshi Ueda * the remainder. 692cec47e3dSKiyoshi Ueda */ 693cec47e3dSKiyoshi Ueda return; 694cec47e3dSKiyoshi Ueda else if (error) { 695cec47e3dSKiyoshi Ueda /* 696cec47e3dSKiyoshi Ueda * Don't notice the error to the upper layer yet. 697cec47e3dSKiyoshi Ueda * The error handling decision is made by the target driver, 698cec47e3dSKiyoshi Ueda * when the request is completed. 699cec47e3dSKiyoshi Ueda */ 700cec47e3dSKiyoshi Ueda tio->error = error; 701cec47e3dSKiyoshi Ueda return; 702cec47e3dSKiyoshi Ueda } 703cec47e3dSKiyoshi Ueda 704cec47e3dSKiyoshi Ueda /* 705cec47e3dSKiyoshi Ueda * I/O for the bio successfully completed. 706cec47e3dSKiyoshi Ueda * Notice the data completion to the upper layer. 707cec47e3dSKiyoshi Ueda */ 708cec47e3dSKiyoshi Ueda 709cec47e3dSKiyoshi Ueda /* 710cec47e3dSKiyoshi Ueda * bios are processed from the head of the list. 711cec47e3dSKiyoshi Ueda * So the completing bio should always be rq->bio. 712cec47e3dSKiyoshi Ueda * If it's not, something wrong is happening. 713cec47e3dSKiyoshi Ueda */ 714cec47e3dSKiyoshi Ueda if (tio->orig->bio != bio) 715cec47e3dSKiyoshi Ueda DMERR("bio completion is going in the middle of the request"); 716cec47e3dSKiyoshi Ueda 717cec47e3dSKiyoshi Ueda /* 718cec47e3dSKiyoshi Ueda * Update the original request. 719cec47e3dSKiyoshi Ueda * Do not use blk_end_request() here, because it may complete 720cec47e3dSKiyoshi Ueda * the original request before the clone, and break the ordering. 721cec47e3dSKiyoshi Ueda */ 722cec47e3dSKiyoshi Ueda blk_update_request(tio->orig, 0, nr_bytes); 723cec47e3dSKiyoshi Ueda } 724cec47e3dSKiyoshi Ueda 725cec47e3dSKiyoshi Ueda /* 726cec47e3dSKiyoshi Ueda * Don't touch any member of the md after calling this function because 727cec47e3dSKiyoshi Ueda * the md may be freed in dm_put() at the end of this function. 728cec47e3dSKiyoshi Ueda * Or do dm_get() before calling this function and dm_put() later. 729cec47e3dSKiyoshi Ueda */ 730b4324feeSKiyoshi Ueda static void rq_completed(struct mapped_device *md, int rw, int run_queue) 731cec47e3dSKiyoshi Ueda { 732b4324feeSKiyoshi Ueda atomic_dec(&md->pending[rw]); 733cec47e3dSKiyoshi Ueda 734cec47e3dSKiyoshi Ueda /* nudge anyone waiting on suspend queue */ 735b4324feeSKiyoshi Ueda if (!md_in_flight(md)) 736cec47e3dSKiyoshi Ueda wake_up(&md->wait); 737cec47e3dSKiyoshi Ueda 738cec47e3dSKiyoshi Ueda if (run_queue) 739b4324feeSKiyoshi Ueda blk_run_queue(md->queue); 740cec47e3dSKiyoshi Ueda 741cec47e3dSKiyoshi Ueda /* 742cec47e3dSKiyoshi Ueda * dm_put() must be at the end of this function. See the comment above 743cec47e3dSKiyoshi Ueda */ 744cec47e3dSKiyoshi Ueda dm_put(md); 745cec47e3dSKiyoshi Ueda } 746cec47e3dSKiyoshi Ueda 747a77e28c7SKiyoshi Ueda static void free_rq_clone(struct request *clone) 748a77e28c7SKiyoshi Ueda { 749a77e28c7SKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 750a77e28c7SKiyoshi Ueda 751a77e28c7SKiyoshi Ueda blk_rq_unprep_clone(clone); 752a77e28c7SKiyoshi Ueda free_rq_tio(tio); 753a77e28c7SKiyoshi Ueda } 754a77e28c7SKiyoshi Ueda 755*980691e5SKiyoshi Ueda /* 756*980691e5SKiyoshi Ueda * Complete the clone and the original request. 757*980691e5SKiyoshi Ueda * Must be called without queue lock. 758*980691e5SKiyoshi Ueda */ 759*980691e5SKiyoshi Ueda static void dm_end_request(struct request *clone, int error) 760*980691e5SKiyoshi Ueda { 761*980691e5SKiyoshi Ueda int rw = rq_data_dir(clone); 762*980691e5SKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 763*980691e5SKiyoshi Ueda struct mapped_device *md = tio->md; 764*980691e5SKiyoshi Ueda struct request *rq = tio->orig; 765*980691e5SKiyoshi Ueda 766*980691e5SKiyoshi Ueda if (blk_pc_request(rq)) { 767*980691e5SKiyoshi Ueda rq->errors = clone->errors; 768*980691e5SKiyoshi Ueda rq->resid_len = clone->resid_len; 769*980691e5SKiyoshi Ueda 770*980691e5SKiyoshi Ueda if (rq->sense) 771*980691e5SKiyoshi Ueda /* 772*980691e5SKiyoshi Ueda * We are using the sense buffer of the original 773*980691e5SKiyoshi Ueda * request. 774*980691e5SKiyoshi Ueda * So setting the length of the sense data is enough. 775*980691e5SKiyoshi Ueda */ 776*980691e5SKiyoshi Ueda rq->sense_len = clone->sense_len; 777*980691e5SKiyoshi Ueda } 778*980691e5SKiyoshi Ueda 779*980691e5SKiyoshi Ueda free_rq_clone(clone); 780*980691e5SKiyoshi Ueda 781*980691e5SKiyoshi Ueda blk_end_request_all(rq, error); 782*980691e5SKiyoshi Ueda 783*980691e5SKiyoshi Ueda rq_completed(md, rw, 1); 784*980691e5SKiyoshi Ueda } 785*980691e5SKiyoshi Ueda 786cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq) 787cec47e3dSKiyoshi Ueda { 788cec47e3dSKiyoshi Ueda struct request *clone = rq->special; 789cec47e3dSKiyoshi Ueda 790cec47e3dSKiyoshi Ueda rq->special = NULL; 791cec47e3dSKiyoshi Ueda rq->cmd_flags &= ~REQ_DONTPREP; 792cec47e3dSKiyoshi Ueda 793a77e28c7SKiyoshi Ueda free_rq_clone(clone); 794cec47e3dSKiyoshi Ueda } 795cec47e3dSKiyoshi Ueda 796cec47e3dSKiyoshi Ueda /* 797cec47e3dSKiyoshi Ueda * Requeue the original request of a clone. 798cec47e3dSKiyoshi Ueda */ 799cec47e3dSKiyoshi Ueda void dm_requeue_unmapped_request(struct request *clone) 800cec47e3dSKiyoshi Ueda { 801b4324feeSKiyoshi Ueda int rw = rq_data_dir(clone); 802cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 803cec47e3dSKiyoshi Ueda struct mapped_device *md = tio->md; 804cec47e3dSKiyoshi Ueda struct request *rq = tio->orig; 805cec47e3dSKiyoshi Ueda struct request_queue *q = rq->q; 806cec47e3dSKiyoshi Ueda unsigned long flags; 807cec47e3dSKiyoshi Ueda 808cec47e3dSKiyoshi Ueda dm_unprep_request(rq); 809cec47e3dSKiyoshi Ueda 810cec47e3dSKiyoshi Ueda spin_lock_irqsave(q->queue_lock, flags); 811cec47e3dSKiyoshi Ueda if (elv_queue_empty(q)) 812cec47e3dSKiyoshi Ueda blk_plug_device(q); 813cec47e3dSKiyoshi Ueda blk_requeue_request(q, rq); 814cec47e3dSKiyoshi Ueda spin_unlock_irqrestore(q->queue_lock, flags); 815cec47e3dSKiyoshi Ueda 816b4324feeSKiyoshi Ueda rq_completed(md, rw, 0); 817cec47e3dSKiyoshi Ueda } 818cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request); 819cec47e3dSKiyoshi Ueda 820cec47e3dSKiyoshi Ueda static void __stop_queue(struct request_queue *q) 821cec47e3dSKiyoshi Ueda { 822cec47e3dSKiyoshi Ueda blk_stop_queue(q); 823cec47e3dSKiyoshi Ueda } 824cec47e3dSKiyoshi Ueda 825cec47e3dSKiyoshi Ueda static void stop_queue(struct request_queue *q) 826cec47e3dSKiyoshi Ueda { 827cec47e3dSKiyoshi Ueda unsigned long flags; 828cec47e3dSKiyoshi Ueda 829cec47e3dSKiyoshi Ueda spin_lock_irqsave(q->queue_lock, flags); 830cec47e3dSKiyoshi Ueda __stop_queue(q); 831cec47e3dSKiyoshi Ueda spin_unlock_irqrestore(q->queue_lock, flags); 832cec47e3dSKiyoshi Ueda } 833cec47e3dSKiyoshi Ueda 834cec47e3dSKiyoshi Ueda static void __start_queue(struct request_queue *q) 835cec47e3dSKiyoshi Ueda { 836cec47e3dSKiyoshi Ueda if (blk_queue_stopped(q)) 837cec47e3dSKiyoshi Ueda blk_start_queue(q); 838cec47e3dSKiyoshi Ueda } 839cec47e3dSKiyoshi Ueda 840cec47e3dSKiyoshi Ueda static void start_queue(struct request_queue *q) 841cec47e3dSKiyoshi Ueda { 842cec47e3dSKiyoshi Ueda unsigned long flags; 843cec47e3dSKiyoshi Ueda 844cec47e3dSKiyoshi Ueda spin_lock_irqsave(q->queue_lock, flags); 845cec47e3dSKiyoshi Ueda __start_queue(q); 846cec47e3dSKiyoshi Ueda spin_unlock_irqrestore(q->queue_lock, flags); 847cec47e3dSKiyoshi Ueda } 848cec47e3dSKiyoshi Ueda 84911a68244SKiyoshi Ueda static void dm_done(struct request *clone, int error, bool mapped) 85011a68244SKiyoshi Ueda { 85111a68244SKiyoshi Ueda int r = error; 85211a68244SKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 85311a68244SKiyoshi Ueda dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io; 85411a68244SKiyoshi Ueda 85511a68244SKiyoshi Ueda if (mapped && rq_end_io) 85611a68244SKiyoshi Ueda r = rq_end_io(tio->ti, clone, error, &tio->info); 85711a68244SKiyoshi Ueda 85811a68244SKiyoshi Ueda if (r <= 0) 85911a68244SKiyoshi Ueda /* The target wants to complete the I/O */ 86011a68244SKiyoshi Ueda dm_end_request(clone, r); 86111a68244SKiyoshi Ueda else if (r == DM_ENDIO_INCOMPLETE) 86211a68244SKiyoshi Ueda /* The target will handle the I/O */ 86311a68244SKiyoshi Ueda return; 86411a68244SKiyoshi Ueda else if (r == DM_ENDIO_REQUEUE) 86511a68244SKiyoshi Ueda /* The target wants to requeue the I/O */ 86611a68244SKiyoshi Ueda dm_requeue_unmapped_request(clone); 86711a68244SKiyoshi Ueda else { 86811a68244SKiyoshi Ueda DMWARN("unimplemented target endio return value: %d", r); 86911a68244SKiyoshi Ueda BUG(); 87011a68244SKiyoshi Ueda } 87111a68244SKiyoshi Ueda } 87211a68244SKiyoshi Ueda 873cec47e3dSKiyoshi Ueda /* 874cec47e3dSKiyoshi Ueda * Request completion handler for request-based dm 875cec47e3dSKiyoshi Ueda */ 876cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq) 877cec47e3dSKiyoshi Ueda { 87811a68244SKiyoshi Ueda bool mapped = true; 879cec47e3dSKiyoshi Ueda struct request *clone = rq->completion_data; 880cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 881cec47e3dSKiyoshi Ueda 88211a68244SKiyoshi Ueda if (rq->cmd_flags & REQ_FAILED) 88311a68244SKiyoshi Ueda mapped = false; 884cec47e3dSKiyoshi Ueda 88511a68244SKiyoshi Ueda dm_done(clone, tio->error, mapped); 886cec47e3dSKiyoshi Ueda } 887cec47e3dSKiyoshi Ueda 888cec47e3dSKiyoshi Ueda /* 889cec47e3dSKiyoshi Ueda * Complete the clone and the original request with the error status 890cec47e3dSKiyoshi Ueda * through softirq context. 891cec47e3dSKiyoshi Ueda */ 892cec47e3dSKiyoshi Ueda static void dm_complete_request(struct request *clone, int error) 893cec47e3dSKiyoshi Ueda { 894cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 895cec47e3dSKiyoshi Ueda struct request *rq = tio->orig; 896cec47e3dSKiyoshi Ueda 897cec47e3dSKiyoshi Ueda tio->error = error; 898cec47e3dSKiyoshi Ueda rq->completion_data = clone; 899cec47e3dSKiyoshi Ueda blk_complete_request(rq); 900cec47e3dSKiyoshi Ueda } 901cec47e3dSKiyoshi Ueda 902cec47e3dSKiyoshi Ueda /* 903cec47e3dSKiyoshi Ueda * Complete the not-mapped clone and the original request with the error status 904cec47e3dSKiyoshi Ueda * through softirq context. 905cec47e3dSKiyoshi Ueda * Target's rq_end_io() function isn't called. 906cec47e3dSKiyoshi Ueda * This may be used when the target's map_rq() function fails. 907cec47e3dSKiyoshi Ueda */ 908cec47e3dSKiyoshi Ueda void dm_kill_unmapped_request(struct request *clone, int error) 909cec47e3dSKiyoshi Ueda { 910cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 911cec47e3dSKiyoshi Ueda struct request *rq = tio->orig; 912cec47e3dSKiyoshi Ueda 913cec47e3dSKiyoshi Ueda rq->cmd_flags |= REQ_FAILED; 914cec47e3dSKiyoshi Ueda dm_complete_request(clone, error); 915cec47e3dSKiyoshi Ueda } 916cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_kill_unmapped_request); 917cec47e3dSKiyoshi Ueda 918cec47e3dSKiyoshi Ueda /* 919cec47e3dSKiyoshi Ueda * Called with the queue lock held 920cec47e3dSKiyoshi Ueda */ 921cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error) 922cec47e3dSKiyoshi Ueda { 923cec47e3dSKiyoshi Ueda /* 924cec47e3dSKiyoshi Ueda * For just cleaning up the information of the queue in which 925cec47e3dSKiyoshi Ueda * the clone was dispatched. 926cec47e3dSKiyoshi Ueda * The clone is *NOT* freed actually here because it is alloced from 927cec47e3dSKiyoshi Ueda * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags. 928cec47e3dSKiyoshi Ueda */ 929cec47e3dSKiyoshi Ueda __blk_put_request(clone->q, clone); 930cec47e3dSKiyoshi Ueda 931cec47e3dSKiyoshi Ueda /* 932cec47e3dSKiyoshi Ueda * Actual request completion is done in a softirq context which doesn't 933cec47e3dSKiyoshi Ueda * hold the queue lock. Otherwise, deadlock could occur because: 934cec47e3dSKiyoshi Ueda * - another request may be submitted by the upper level driver 935cec47e3dSKiyoshi Ueda * of the stacking during the completion 936cec47e3dSKiyoshi Ueda * - the submission which requires queue lock may be done 937cec47e3dSKiyoshi Ueda * against this queue 938cec47e3dSKiyoshi Ueda */ 939cec47e3dSKiyoshi Ueda dm_complete_request(clone, error); 940cec47e3dSKiyoshi Ueda } 941cec47e3dSKiyoshi Ueda 9421da177e4SLinus Torvalds static sector_t max_io_len(struct mapped_device *md, 9431da177e4SLinus Torvalds sector_t sector, struct dm_target *ti) 9441da177e4SLinus Torvalds { 9451da177e4SLinus Torvalds sector_t offset = sector - ti->begin; 9461da177e4SLinus Torvalds sector_t len = ti->len - offset; 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds /* 9491da177e4SLinus Torvalds * Does the target need to split even further ? 9501da177e4SLinus Torvalds */ 9511da177e4SLinus Torvalds if (ti->split_io) { 9521da177e4SLinus Torvalds sector_t boundary; 9531da177e4SLinus Torvalds boundary = ((offset + ti->split_io) & ~(ti->split_io - 1)) 9541da177e4SLinus Torvalds - offset; 9551da177e4SLinus Torvalds if (len > boundary) 9561da177e4SLinus Torvalds len = boundary; 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds return len; 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds static void __map_bio(struct dm_target *ti, struct bio *clone, 963028867acSAlasdair G Kergon struct dm_target_io *tio) 9641da177e4SLinus Torvalds { 9651da177e4SLinus Torvalds int r; 9662056a782SJens Axboe sector_t sector; 9679faf400fSStefan Bader struct mapped_device *md; 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds clone->bi_end_io = clone_endio; 9701da177e4SLinus Torvalds clone->bi_private = tio; 9711da177e4SLinus Torvalds 9721da177e4SLinus Torvalds /* 9731da177e4SLinus Torvalds * Map the clone. If r == 0 we don't need to do 9741da177e4SLinus Torvalds * anything, the target has assumed ownership of 9751da177e4SLinus Torvalds * this io. 9761da177e4SLinus Torvalds */ 9771da177e4SLinus Torvalds atomic_inc(&tio->io->io_count); 9782056a782SJens Axboe sector = clone->bi_sector; 9791da177e4SLinus Torvalds r = ti->type->map(ti, clone, &tio->info); 98045cbcd79SKiyoshi Ueda if (r == DM_MAPIO_REMAPPED) { 9811da177e4SLinus Torvalds /* the bio has been remapped so dispatch it */ 9822056a782SJens Axboe 9835f3ea37cSArnaldo Carvalho de Melo trace_block_remap(bdev_get_queue(clone->bi_bdev), clone, 98422a7c31aSAlan D. Brunelle tio->io->bio->bi_bdev->bd_dev, sector); 9852056a782SJens Axboe 9861da177e4SLinus Torvalds generic_make_request(clone); 9872e93ccc1SKiyoshi Ueda } else if (r < 0 || r == DM_MAPIO_REQUEUE) { 9882e93ccc1SKiyoshi Ueda /* error the io and bail out, or requeue it if needed */ 9899faf400fSStefan Bader md = tio->io->md; 9909faf400fSStefan Bader dec_pending(tio->io, r); 9919faf400fSStefan Bader /* 9929faf400fSStefan Bader * Store bio_set for cleanup. 9939faf400fSStefan Bader */ 9949faf400fSStefan Bader clone->bi_private = md->bs; 9951da177e4SLinus Torvalds bio_put(clone); 9969faf400fSStefan Bader free_tio(md, tio); 99745cbcd79SKiyoshi Ueda } else if (r) { 99845cbcd79SKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 99945cbcd79SKiyoshi Ueda BUG(); 10001da177e4SLinus Torvalds } 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds struct clone_info { 10041da177e4SLinus Torvalds struct mapped_device *md; 10051da177e4SLinus Torvalds struct dm_table *map; 10061da177e4SLinus Torvalds struct bio *bio; 10071da177e4SLinus Torvalds struct dm_io *io; 10081da177e4SLinus Torvalds sector_t sector; 10091da177e4SLinus Torvalds sector_t sector_count; 10101da177e4SLinus Torvalds unsigned short idx; 10111da177e4SLinus Torvalds }; 10121da177e4SLinus Torvalds 10133676347aSPeter Osterlund static void dm_bio_destructor(struct bio *bio) 10143676347aSPeter Osterlund { 10159faf400fSStefan Bader struct bio_set *bs = bio->bi_private; 10169faf400fSStefan Bader 10179faf400fSStefan Bader bio_free(bio, bs); 10183676347aSPeter Osterlund } 10193676347aSPeter Osterlund 10201da177e4SLinus Torvalds /* 10211da177e4SLinus Torvalds * Creates a little bio that is just does part of a bvec. 10221da177e4SLinus Torvalds */ 10231da177e4SLinus Torvalds static struct bio *split_bvec(struct bio *bio, sector_t sector, 10241da177e4SLinus Torvalds unsigned short idx, unsigned int offset, 10259faf400fSStefan Bader unsigned int len, struct bio_set *bs) 10261da177e4SLinus Torvalds { 10271da177e4SLinus Torvalds struct bio *clone; 10281da177e4SLinus Torvalds struct bio_vec *bv = bio->bi_io_vec + idx; 10291da177e4SLinus Torvalds 10309faf400fSStefan Bader clone = bio_alloc_bioset(GFP_NOIO, 1, bs); 10313676347aSPeter Osterlund clone->bi_destructor = dm_bio_destructor; 10321da177e4SLinus Torvalds *clone->bi_io_vec = *bv; 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds clone->bi_sector = sector; 10351da177e4SLinus Torvalds clone->bi_bdev = bio->bi_bdev; 1036af7e466aSMikulas Patocka clone->bi_rw = bio->bi_rw & ~(1 << BIO_RW_BARRIER); 10371da177e4SLinus Torvalds clone->bi_vcnt = 1; 10381da177e4SLinus Torvalds clone->bi_size = to_bytes(len); 10391da177e4SLinus Torvalds clone->bi_io_vec->bv_offset = offset; 10401da177e4SLinus Torvalds clone->bi_io_vec->bv_len = clone->bi_size; 1041f3e1d26eSMartin K. Petersen clone->bi_flags |= 1 << BIO_CLONED; 10421da177e4SLinus Torvalds 10439c47008dSMartin K. Petersen if (bio_integrity(bio)) { 10447878cba9SMartin K. Petersen bio_integrity_clone(clone, bio, GFP_NOIO, bs); 10459c47008dSMartin K. Petersen bio_integrity_trim(clone, 10469c47008dSMartin K. Petersen bio_sector_offset(bio, idx, offset), len); 10479c47008dSMartin K. Petersen } 10489c47008dSMartin K. Petersen 10491da177e4SLinus Torvalds return clone; 10501da177e4SLinus Torvalds } 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds /* 10531da177e4SLinus Torvalds * Creates a bio that consists of range of complete bvecs. 10541da177e4SLinus Torvalds */ 10551da177e4SLinus Torvalds static struct bio *clone_bio(struct bio *bio, sector_t sector, 10561da177e4SLinus Torvalds unsigned short idx, unsigned short bv_count, 10579faf400fSStefan Bader unsigned int len, struct bio_set *bs) 10581da177e4SLinus Torvalds { 10591da177e4SLinus Torvalds struct bio *clone; 10601da177e4SLinus Torvalds 10619faf400fSStefan Bader clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs); 10629faf400fSStefan Bader __bio_clone(clone, bio); 1063af7e466aSMikulas Patocka clone->bi_rw &= ~(1 << BIO_RW_BARRIER); 10649faf400fSStefan Bader clone->bi_destructor = dm_bio_destructor; 10651da177e4SLinus Torvalds clone->bi_sector = sector; 10661da177e4SLinus Torvalds clone->bi_idx = idx; 10671da177e4SLinus Torvalds clone->bi_vcnt = idx + bv_count; 10681da177e4SLinus Torvalds clone->bi_size = to_bytes(len); 10691da177e4SLinus Torvalds clone->bi_flags &= ~(1 << BIO_SEG_VALID); 10701da177e4SLinus Torvalds 10719c47008dSMartin K. Petersen if (bio_integrity(bio)) { 10727878cba9SMartin K. Petersen bio_integrity_clone(clone, bio, GFP_NOIO, bs); 10739c47008dSMartin K. Petersen 10749c47008dSMartin K. Petersen if (idx != bio->bi_idx || clone->bi_size < bio->bi_size) 10759c47008dSMartin K. Petersen bio_integrity_trim(clone, 10769c47008dSMartin K. Petersen bio_sector_offset(bio, idx, 0), len); 10779c47008dSMartin K. Petersen } 10789c47008dSMartin K. Petersen 10791da177e4SLinus Torvalds return clone; 10801da177e4SLinus Torvalds } 10811da177e4SLinus Torvalds 10829015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci, 10839015df24SAlasdair G Kergon struct dm_target *ti) 1084f9ab94ceSMikulas Patocka { 10859015df24SAlasdair G Kergon struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO); 1086f9ab94ceSMikulas Patocka 1087f9ab94ceSMikulas Patocka tio->io = ci->io; 1088f9ab94ceSMikulas Patocka tio->ti = ti; 1089f9ab94ceSMikulas Patocka memset(&tio->info, 0, sizeof(tio->info)); 10909015df24SAlasdair G Kergon 10919015df24SAlasdair G Kergon return tio; 10929015df24SAlasdair G Kergon } 10939015df24SAlasdair G Kergon 10949015df24SAlasdair G Kergon static void __flush_target(struct clone_info *ci, struct dm_target *ti, 10959015df24SAlasdair G Kergon unsigned flush_nr) 10969015df24SAlasdair G Kergon { 10979015df24SAlasdair G Kergon struct dm_target_io *tio = alloc_tio(ci, ti); 10989015df24SAlasdair G Kergon struct bio *clone; 10999015df24SAlasdair G Kergon 1100f9ab94ceSMikulas Patocka tio->info.flush_request = flush_nr; 1101f9ab94ceSMikulas Patocka 1102f9ab94ceSMikulas Patocka clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs); 1103f9ab94ceSMikulas Patocka __bio_clone(clone, ci->bio); 1104f9ab94ceSMikulas Patocka clone->bi_destructor = dm_bio_destructor; 1105f9ab94ceSMikulas Patocka 1106f9ab94ceSMikulas Patocka __map_bio(ti, clone, tio); 1107f9ab94ceSMikulas Patocka } 1108f9ab94ceSMikulas Patocka 1109f9ab94ceSMikulas Patocka static int __clone_and_map_empty_barrier(struct clone_info *ci) 1110f9ab94ceSMikulas Patocka { 1111f9ab94ceSMikulas Patocka unsigned target_nr = 0, flush_nr; 1112f9ab94ceSMikulas Patocka struct dm_target *ti; 1113f9ab94ceSMikulas Patocka 1114f9ab94ceSMikulas Patocka while ((ti = dm_table_get_target(ci->map, target_nr++))) 1115f9ab94ceSMikulas Patocka for (flush_nr = 0; flush_nr < ti->num_flush_requests; 1116f9ab94ceSMikulas Patocka flush_nr++) 1117f9ab94ceSMikulas Patocka __flush_target(ci, ti, flush_nr); 1118f9ab94ceSMikulas Patocka 1119f9ab94ceSMikulas Patocka ci->sector_count = 0; 1120f9ab94ceSMikulas Patocka 1121f9ab94ceSMikulas Patocka return 0; 1122f9ab94ceSMikulas Patocka } 1123f9ab94ceSMikulas Patocka 1124512875bdSJun'ichi Nomura static int __clone_and_map(struct clone_info *ci) 11251da177e4SLinus Torvalds { 11261da177e4SLinus Torvalds struct bio *clone, *bio = ci->bio; 1127512875bdSJun'ichi Nomura struct dm_target *ti; 1128512875bdSJun'ichi Nomura sector_t len = 0, max; 1129028867acSAlasdair G Kergon struct dm_target_io *tio; 11301da177e4SLinus Torvalds 1131f9ab94ceSMikulas Patocka if (unlikely(bio_empty_barrier(bio))) 1132f9ab94ceSMikulas Patocka return __clone_and_map_empty_barrier(ci); 1133f9ab94ceSMikulas Patocka 1134512875bdSJun'ichi Nomura ti = dm_table_find_target(ci->map, ci->sector); 1135512875bdSJun'ichi Nomura if (!dm_target_is_valid(ti)) 1136512875bdSJun'ichi Nomura return -EIO; 1137512875bdSJun'ichi Nomura 1138512875bdSJun'ichi Nomura max = max_io_len(ci->md, ci->sector, ti); 1139512875bdSJun'ichi Nomura 11401da177e4SLinus Torvalds /* 11411da177e4SLinus Torvalds * Allocate a target io object. 11421da177e4SLinus Torvalds */ 11439015df24SAlasdair G Kergon tio = alloc_tio(ci, ti); 11441da177e4SLinus Torvalds 11451da177e4SLinus Torvalds if (ci->sector_count <= max) { 11461da177e4SLinus Torvalds /* 11471da177e4SLinus Torvalds * Optimise for the simple case where we can do all of 11481da177e4SLinus Torvalds * the remaining io with a single clone. 11491da177e4SLinus Torvalds */ 11501da177e4SLinus Torvalds clone = clone_bio(bio, ci->sector, ci->idx, 11519faf400fSStefan Bader bio->bi_vcnt - ci->idx, ci->sector_count, 11529faf400fSStefan Bader ci->md->bs); 11531da177e4SLinus Torvalds __map_bio(ti, clone, tio); 11541da177e4SLinus Torvalds ci->sector_count = 0; 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) { 11571da177e4SLinus Torvalds /* 11581da177e4SLinus Torvalds * There are some bvecs that don't span targets. 11591da177e4SLinus Torvalds * Do as many of these as possible. 11601da177e4SLinus Torvalds */ 11611da177e4SLinus Torvalds int i; 11621da177e4SLinus Torvalds sector_t remaining = max; 11631da177e4SLinus Torvalds sector_t bv_len; 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) { 11661da177e4SLinus Torvalds bv_len = to_sector(bio->bi_io_vec[i].bv_len); 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds if (bv_len > remaining) 11691da177e4SLinus Torvalds break; 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds remaining -= bv_len; 11721da177e4SLinus Torvalds len += bv_len; 11731da177e4SLinus Torvalds } 11741da177e4SLinus Torvalds 11759faf400fSStefan Bader clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len, 11769faf400fSStefan Bader ci->md->bs); 11771da177e4SLinus Torvalds __map_bio(ti, clone, tio); 11781da177e4SLinus Torvalds 11791da177e4SLinus Torvalds ci->sector += len; 11801da177e4SLinus Torvalds ci->sector_count -= len; 11811da177e4SLinus Torvalds ci->idx = i; 11821da177e4SLinus Torvalds 11831da177e4SLinus Torvalds } else { 11841da177e4SLinus Torvalds /* 1185d2044a94SAlasdair G Kergon * Handle a bvec that must be split between two or more targets. 11861da177e4SLinus Torvalds */ 11871da177e4SLinus Torvalds struct bio_vec *bv = bio->bi_io_vec + ci->idx; 1188d2044a94SAlasdair G Kergon sector_t remaining = to_sector(bv->bv_len); 1189d2044a94SAlasdair G Kergon unsigned int offset = 0; 11901da177e4SLinus Torvalds 1191d2044a94SAlasdair G Kergon do { 1192d2044a94SAlasdair G Kergon if (offset) { 11931da177e4SLinus Torvalds ti = dm_table_find_target(ci->map, ci->sector); 1194512875bdSJun'ichi Nomura if (!dm_target_is_valid(ti)) 1195512875bdSJun'ichi Nomura return -EIO; 1196512875bdSJun'ichi Nomura 1197d2044a94SAlasdair G Kergon max = max_io_len(ci->md, ci->sector, ti); 11981da177e4SLinus Torvalds 11999015df24SAlasdair G Kergon tio = alloc_tio(ci, ti); 1200d2044a94SAlasdair G Kergon } 1201d2044a94SAlasdair G Kergon 1202d2044a94SAlasdair G Kergon len = min(remaining, max); 1203d2044a94SAlasdair G Kergon 1204d2044a94SAlasdair G Kergon clone = split_bvec(bio, ci->sector, ci->idx, 12059faf400fSStefan Bader bv->bv_offset + offset, len, 12069faf400fSStefan Bader ci->md->bs); 1207d2044a94SAlasdair G Kergon 12081da177e4SLinus Torvalds __map_bio(ti, clone, tio); 12091da177e4SLinus Torvalds 12101da177e4SLinus Torvalds ci->sector += len; 12111da177e4SLinus Torvalds ci->sector_count -= len; 1212d2044a94SAlasdair G Kergon offset += to_bytes(len); 1213d2044a94SAlasdair G Kergon } while (remaining -= len); 1214d2044a94SAlasdair G Kergon 12151da177e4SLinus Torvalds ci->idx++; 12161da177e4SLinus Torvalds } 1217512875bdSJun'ichi Nomura 1218512875bdSJun'ichi Nomura return 0; 12191da177e4SLinus Torvalds } 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds /* 12228a53c28dSMikulas Patocka * Split the bio into several clones and submit it to targets. 12231da177e4SLinus Torvalds */ 1224f0b9a450SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md, struct bio *bio) 12251da177e4SLinus Torvalds { 12261da177e4SLinus Torvalds struct clone_info ci; 1227512875bdSJun'ichi Nomura int error = 0; 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds ci.map = dm_get_table(md); 1230f0b9a450SMikulas Patocka if (unlikely(!ci.map)) { 12311f98a13fSJens Axboe if (!bio_rw_flagged(bio, BIO_RW_BARRIER)) 1232f0b9a450SMikulas Patocka bio_io_error(bio); 1233af7e466aSMikulas Patocka else 12345aa2781dSMikulas Patocka if (!md->barrier_error) 1235af7e466aSMikulas Patocka md->barrier_error = -EIO; 1236f0b9a450SMikulas Patocka return; 1237f0b9a450SMikulas Patocka } 1238692d0eb9SMikulas Patocka 12391da177e4SLinus Torvalds ci.md = md; 12401da177e4SLinus Torvalds ci.bio = bio; 12411da177e4SLinus Torvalds ci.io = alloc_io(md); 12421da177e4SLinus Torvalds ci.io->error = 0; 12431da177e4SLinus Torvalds atomic_set(&ci.io->io_count, 1); 12441da177e4SLinus Torvalds ci.io->bio = bio; 12451da177e4SLinus Torvalds ci.io->md = md; 1246f88fb981SKiyoshi Ueda spin_lock_init(&ci.io->endio_lock); 12471da177e4SLinus Torvalds ci.sector = bio->bi_sector; 12481da177e4SLinus Torvalds ci.sector_count = bio_sectors(bio); 1249f9ab94ceSMikulas Patocka if (unlikely(bio_empty_barrier(bio))) 1250f9ab94ceSMikulas Patocka ci.sector_count = 1; 12511da177e4SLinus Torvalds ci.idx = bio->bi_idx; 12521da177e4SLinus Torvalds 12533eaf840eSJun'ichi "Nick" Nomura start_io_acct(ci.io); 1254512875bdSJun'ichi Nomura while (ci.sector_count && !error) 1255512875bdSJun'ichi Nomura error = __clone_and_map(&ci); 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds /* drop the extra reference count */ 1258512875bdSJun'ichi Nomura dec_pending(ci.io, error); 12591da177e4SLinus Torvalds dm_table_put(ci.map); 12601da177e4SLinus Torvalds } 12611da177e4SLinus Torvalds /*----------------------------------------------------------------- 12621da177e4SLinus Torvalds * CRUD END 12631da177e4SLinus Torvalds *---------------------------------------------------------------*/ 12641da177e4SLinus Torvalds 1265f6fccb12SMilan Broz static int dm_merge_bvec(struct request_queue *q, 1266f6fccb12SMilan Broz struct bvec_merge_data *bvm, 1267f6fccb12SMilan Broz struct bio_vec *biovec) 1268f6fccb12SMilan Broz { 1269f6fccb12SMilan Broz struct mapped_device *md = q->queuedata; 1270f6fccb12SMilan Broz struct dm_table *map = dm_get_table(md); 1271f6fccb12SMilan Broz struct dm_target *ti; 1272f6fccb12SMilan Broz sector_t max_sectors; 12735037108aSMikulas Patocka int max_size = 0; 1274f6fccb12SMilan Broz 1275f6fccb12SMilan Broz if (unlikely(!map)) 12765037108aSMikulas Patocka goto out; 1277f6fccb12SMilan Broz 1278f6fccb12SMilan Broz ti = dm_table_find_target(map, bvm->bi_sector); 1279b01cd5acSMikulas Patocka if (!dm_target_is_valid(ti)) 1280b01cd5acSMikulas Patocka goto out_table; 1281f6fccb12SMilan Broz 1282f6fccb12SMilan Broz /* 1283f6fccb12SMilan Broz * Find maximum amount of I/O that won't need splitting 1284f6fccb12SMilan Broz */ 1285f6fccb12SMilan Broz max_sectors = min(max_io_len(md, bvm->bi_sector, ti), 1286f6fccb12SMilan Broz (sector_t) BIO_MAX_SECTORS); 1287f6fccb12SMilan Broz max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size; 1288f6fccb12SMilan Broz if (max_size < 0) 1289f6fccb12SMilan Broz max_size = 0; 1290f6fccb12SMilan Broz 1291f6fccb12SMilan Broz /* 1292f6fccb12SMilan Broz * merge_bvec_fn() returns number of bytes 1293f6fccb12SMilan Broz * it can accept at this offset 1294f6fccb12SMilan Broz * max is precomputed maximal io size 1295f6fccb12SMilan Broz */ 1296f6fccb12SMilan Broz if (max_size && ti->type->merge) 1297f6fccb12SMilan Broz max_size = ti->type->merge(ti, bvm, biovec, max_size); 12988cbeb67aSMikulas Patocka /* 12998cbeb67aSMikulas Patocka * If the target doesn't support merge method and some of the devices 13008cbeb67aSMikulas Patocka * provided their merge_bvec method (we know this by looking at 13018cbeb67aSMikulas Patocka * queue_max_hw_sectors), then we can't allow bios with multiple vector 13028cbeb67aSMikulas Patocka * entries. So always set max_size to 0, and the code below allows 13038cbeb67aSMikulas Patocka * just one page. 13048cbeb67aSMikulas Patocka */ 13058cbeb67aSMikulas Patocka else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9) 13068cbeb67aSMikulas Patocka 13078cbeb67aSMikulas Patocka max_size = 0; 1308f6fccb12SMilan Broz 1309b01cd5acSMikulas Patocka out_table: 13105037108aSMikulas Patocka dm_table_put(map); 13115037108aSMikulas Patocka 13125037108aSMikulas Patocka out: 1313f6fccb12SMilan Broz /* 1314f6fccb12SMilan Broz * Always allow an entire first page 1315f6fccb12SMilan Broz */ 1316f6fccb12SMilan Broz if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT)) 1317f6fccb12SMilan Broz max_size = biovec->bv_len; 1318f6fccb12SMilan Broz 1319f6fccb12SMilan Broz return max_size; 1320f6fccb12SMilan Broz } 1321f6fccb12SMilan Broz 13221da177e4SLinus Torvalds /* 13231da177e4SLinus Torvalds * The request function that just remaps the bio built up by 13241da177e4SLinus Torvalds * dm_merge_bvec. 13251da177e4SLinus Torvalds */ 1326cec47e3dSKiyoshi Ueda static int _dm_request(struct request_queue *q, struct bio *bio) 13271da177e4SLinus Torvalds { 132812f03a49SKevin Corry int rw = bio_data_dir(bio); 13291da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 1330c9959059STejun Heo int cpu; 13311da177e4SLinus Torvalds 13322ca3310eSAlasdair G Kergon down_read(&md->io_lock); 13331da177e4SLinus Torvalds 1334074a7acaSTejun Heo cpu = part_stat_lock(); 1335074a7acaSTejun Heo part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]); 1336074a7acaSTejun Heo part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio)); 1337074a7acaSTejun Heo part_stat_unlock(); 133812f03a49SKevin Corry 13391da177e4SLinus Torvalds /* 13401eb787ecSAlasdair G Kergon * If we're suspended or the thread is processing barriers 13411eb787ecSAlasdair G Kergon * we have to queue this io for later. 13421da177e4SLinus Torvalds */ 1343af7e466aSMikulas Patocka if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) || 13441f98a13fSJens Axboe unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) { 13452ca3310eSAlasdair G Kergon up_read(&md->io_lock); 13461da177e4SLinus Torvalds 134754d9a1b4SAlasdair G Kergon if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) && 134854d9a1b4SAlasdair G Kergon bio_rw(bio) == READA) { 134954d9a1b4SAlasdair G Kergon bio_io_error(bio); 135054d9a1b4SAlasdair G Kergon return 0; 135154d9a1b4SAlasdair G Kergon } 13521da177e4SLinus Torvalds 135392c63902SMikulas Patocka queue_io(md, bio); 13541da177e4SLinus Torvalds 135592c63902SMikulas Patocka return 0; 13561da177e4SLinus Torvalds } 13571da177e4SLinus Torvalds 1358f0b9a450SMikulas Patocka __split_and_process_bio(md, bio); 13592ca3310eSAlasdair G Kergon up_read(&md->io_lock); 1360f0b9a450SMikulas Patocka return 0; 13611da177e4SLinus Torvalds } 13621da177e4SLinus Torvalds 1363cec47e3dSKiyoshi Ueda static int dm_make_request(struct request_queue *q, struct bio *bio) 1364cec47e3dSKiyoshi Ueda { 1365cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 1366cec47e3dSKiyoshi Ueda 13671f98a13fSJens Axboe if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) { 1368cec47e3dSKiyoshi Ueda bio_endio(bio, -EOPNOTSUPP); 1369cec47e3dSKiyoshi Ueda return 0; 1370cec47e3dSKiyoshi Ueda } 1371cec47e3dSKiyoshi Ueda 1372cec47e3dSKiyoshi Ueda return md->saved_make_request_fn(q, bio); /* call __make_request() */ 1373cec47e3dSKiyoshi Ueda } 1374cec47e3dSKiyoshi Ueda 1375cec47e3dSKiyoshi Ueda static int dm_request_based(struct mapped_device *md) 1376cec47e3dSKiyoshi Ueda { 1377cec47e3dSKiyoshi Ueda return blk_queue_stackable(md->queue); 1378cec47e3dSKiyoshi Ueda } 1379cec47e3dSKiyoshi Ueda 1380cec47e3dSKiyoshi Ueda static int dm_request(struct request_queue *q, struct bio *bio) 1381cec47e3dSKiyoshi Ueda { 1382cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 1383cec47e3dSKiyoshi Ueda 1384cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 1385cec47e3dSKiyoshi Ueda return dm_make_request(q, bio); 1386cec47e3dSKiyoshi Ueda 1387cec47e3dSKiyoshi Ueda return _dm_request(q, bio); 1388cec47e3dSKiyoshi Ueda } 1389cec47e3dSKiyoshi Ueda 1390cec47e3dSKiyoshi Ueda void dm_dispatch_request(struct request *rq) 1391cec47e3dSKiyoshi Ueda { 1392cec47e3dSKiyoshi Ueda int r; 1393cec47e3dSKiyoshi Ueda 1394cec47e3dSKiyoshi Ueda if (blk_queue_io_stat(rq->q)) 1395cec47e3dSKiyoshi Ueda rq->cmd_flags |= REQ_IO_STAT; 1396cec47e3dSKiyoshi Ueda 1397cec47e3dSKiyoshi Ueda rq->start_time = jiffies; 1398cec47e3dSKiyoshi Ueda r = blk_insert_cloned_request(rq->q, rq); 1399cec47e3dSKiyoshi Ueda if (r) 1400cec47e3dSKiyoshi Ueda dm_complete_request(rq, r); 1401cec47e3dSKiyoshi Ueda } 1402cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_dispatch_request); 1403cec47e3dSKiyoshi Ueda 1404cec47e3dSKiyoshi Ueda static void dm_rq_bio_destructor(struct bio *bio) 1405cec47e3dSKiyoshi Ueda { 1406cec47e3dSKiyoshi Ueda struct dm_rq_clone_bio_info *info = bio->bi_private; 1407cec47e3dSKiyoshi Ueda struct mapped_device *md = info->tio->md; 1408cec47e3dSKiyoshi Ueda 1409cec47e3dSKiyoshi Ueda free_bio_info(info); 1410cec47e3dSKiyoshi Ueda bio_free(bio, md->bs); 1411cec47e3dSKiyoshi Ueda } 1412cec47e3dSKiyoshi Ueda 1413cec47e3dSKiyoshi Ueda static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig, 1414cec47e3dSKiyoshi Ueda void *data) 1415cec47e3dSKiyoshi Ueda { 1416cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = data; 1417cec47e3dSKiyoshi Ueda struct mapped_device *md = tio->md; 1418cec47e3dSKiyoshi Ueda struct dm_rq_clone_bio_info *info = alloc_bio_info(md); 1419cec47e3dSKiyoshi Ueda 1420cec47e3dSKiyoshi Ueda if (!info) 1421cec47e3dSKiyoshi Ueda return -ENOMEM; 1422cec47e3dSKiyoshi Ueda 1423cec47e3dSKiyoshi Ueda info->orig = bio_orig; 1424cec47e3dSKiyoshi Ueda info->tio = tio; 1425cec47e3dSKiyoshi Ueda bio->bi_end_io = end_clone_bio; 1426cec47e3dSKiyoshi Ueda bio->bi_private = info; 1427cec47e3dSKiyoshi Ueda bio->bi_destructor = dm_rq_bio_destructor; 1428cec47e3dSKiyoshi Ueda 1429cec47e3dSKiyoshi Ueda return 0; 1430cec47e3dSKiyoshi Ueda } 1431cec47e3dSKiyoshi Ueda 1432cec47e3dSKiyoshi Ueda static int setup_clone(struct request *clone, struct request *rq, 1433cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio) 1434cec47e3dSKiyoshi Ueda { 1435cec47e3dSKiyoshi Ueda int r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC, 1436cec47e3dSKiyoshi Ueda dm_rq_bio_constructor, tio); 1437cec47e3dSKiyoshi Ueda 1438cec47e3dSKiyoshi Ueda if (r) 1439cec47e3dSKiyoshi Ueda return r; 1440cec47e3dSKiyoshi Ueda 1441cec47e3dSKiyoshi Ueda clone->cmd = rq->cmd; 1442cec47e3dSKiyoshi Ueda clone->cmd_len = rq->cmd_len; 1443cec47e3dSKiyoshi Ueda clone->sense = rq->sense; 1444cec47e3dSKiyoshi Ueda clone->buffer = rq->buffer; 1445cec47e3dSKiyoshi Ueda clone->end_io = end_clone_request; 1446cec47e3dSKiyoshi Ueda clone->end_io_data = tio; 1447cec47e3dSKiyoshi Ueda 1448cec47e3dSKiyoshi Ueda return 0; 1449cec47e3dSKiyoshi Ueda } 1450cec47e3dSKiyoshi Ueda 14516facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md, 14526facdaffSKiyoshi Ueda gfp_t gfp_mask) 14536facdaffSKiyoshi Ueda { 14546facdaffSKiyoshi Ueda struct request *clone; 14556facdaffSKiyoshi Ueda struct dm_rq_target_io *tio; 14566facdaffSKiyoshi Ueda 14576facdaffSKiyoshi Ueda tio = alloc_rq_tio(md, gfp_mask); 14586facdaffSKiyoshi Ueda if (!tio) 14596facdaffSKiyoshi Ueda return NULL; 14606facdaffSKiyoshi Ueda 14616facdaffSKiyoshi Ueda tio->md = md; 14626facdaffSKiyoshi Ueda tio->ti = NULL; 14636facdaffSKiyoshi Ueda tio->orig = rq; 14646facdaffSKiyoshi Ueda tio->error = 0; 14656facdaffSKiyoshi Ueda memset(&tio->info, 0, sizeof(tio->info)); 14666facdaffSKiyoshi Ueda 14676facdaffSKiyoshi Ueda clone = &tio->clone; 14686facdaffSKiyoshi Ueda if (setup_clone(clone, rq, tio)) { 14696facdaffSKiyoshi Ueda /* -ENOMEM */ 14706facdaffSKiyoshi Ueda free_rq_tio(tio); 14716facdaffSKiyoshi Ueda return NULL; 14726facdaffSKiyoshi Ueda } 14736facdaffSKiyoshi Ueda 14746facdaffSKiyoshi Ueda return clone; 14756facdaffSKiyoshi Ueda } 14766facdaffSKiyoshi Ueda 1477cec47e3dSKiyoshi Ueda /* 1478cec47e3dSKiyoshi Ueda * Called with the queue lock held. 1479cec47e3dSKiyoshi Ueda */ 1480cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq) 1481cec47e3dSKiyoshi Ueda { 1482cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 1483cec47e3dSKiyoshi Ueda struct request *clone; 1484cec47e3dSKiyoshi Ueda 1485cec47e3dSKiyoshi Ueda if (unlikely(rq->special)) { 1486cec47e3dSKiyoshi Ueda DMWARN("Already has something in rq->special."); 1487cec47e3dSKiyoshi Ueda return BLKPREP_KILL; 1488cec47e3dSKiyoshi Ueda } 1489cec47e3dSKiyoshi Ueda 14906facdaffSKiyoshi Ueda clone = clone_rq(rq, md, GFP_ATOMIC); 14916facdaffSKiyoshi Ueda if (!clone) 1492cec47e3dSKiyoshi Ueda return BLKPREP_DEFER; 1493cec47e3dSKiyoshi Ueda 1494cec47e3dSKiyoshi Ueda rq->special = clone; 1495cec47e3dSKiyoshi Ueda rq->cmd_flags |= REQ_DONTPREP; 1496cec47e3dSKiyoshi Ueda 1497cec47e3dSKiyoshi Ueda return BLKPREP_OK; 1498cec47e3dSKiyoshi Ueda } 1499cec47e3dSKiyoshi Ueda 1500598de409SKiyoshi Ueda static void map_request(struct dm_target *ti, struct request *clone, 1501cec47e3dSKiyoshi Ueda struct mapped_device *md) 1502cec47e3dSKiyoshi Ueda { 1503cec47e3dSKiyoshi Ueda int r; 1504cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 1505cec47e3dSKiyoshi Ueda 1506cec47e3dSKiyoshi Ueda /* 1507cec47e3dSKiyoshi Ueda * Hold the md reference here for the in-flight I/O. 1508cec47e3dSKiyoshi Ueda * We can't rely on the reference count by device opener, 1509cec47e3dSKiyoshi Ueda * because the device may be closed during the request completion 1510cec47e3dSKiyoshi Ueda * when all bios are completed. 1511cec47e3dSKiyoshi Ueda * See the comment in rq_completed() too. 1512cec47e3dSKiyoshi Ueda */ 1513cec47e3dSKiyoshi Ueda dm_get(md); 1514cec47e3dSKiyoshi Ueda 1515cec47e3dSKiyoshi Ueda tio->ti = ti; 1516cec47e3dSKiyoshi Ueda r = ti->type->map_rq(ti, clone, &tio->info); 1517cec47e3dSKiyoshi Ueda switch (r) { 1518cec47e3dSKiyoshi Ueda case DM_MAPIO_SUBMITTED: 1519cec47e3dSKiyoshi Ueda /* The target has taken the I/O to submit by itself later */ 1520cec47e3dSKiyoshi Ueda break; 1521cec47e3dSKiyoshi Ueda case DM_MAPIO_REMAPPED: 1522cec47e3dSKiyoshi Ueda /* The target has remapped the I/O so dispatch it */ 1523cec47e3dSKiyoshi Ueda dm_dispatch_request(clone); 1524cec47e3dSKiyoshi Ueda break; 1525cec47e3dSKiyoshi Ueda case DM_MAPIO_REQUEUE: 1526cec47e3dSKiyoshi Ueda /* The target wants to requeue the I/O */ 1527cec47e3dSKiyoshi Ueda dm_requeue_unmapped_request(clone); 1528cec47e3dSKiyoshi Ueda break; 1529cec47e3dSKiyoshi Ueda default: 1530cec47e3dSKiyoshi Ueda if (r > 0) { 1531cec47e3dSKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 1532cec47e3dSKiyoshi Ueda BUG(); 1533cec47e3dSKiyoshi Ueda } 1534cec47e3dSKiyoshi Ueda 1535cec47e3dSKiyoshi Ueda /* The target wants to complete the I/O */ 1536cec47e3dSKiyoshi Ueda dm_kill_unmapped_request(clone, r); 1537cec47e3dSKiyoshi Ueda break; 1538cec47e3dSKiyoshi Ueda } 1539cec47e3dSKiyoshi Ueda } 1540cec47e3dSKiyoshi Ueda 1541cec47e3dSKiyoshi Ueda /* 1542cec47e3dSKiyoshi Ueda * q->request_fn for request-based dm. 1543cec47e3dSKiyoshi Ueda * Called with the queue lock held. 1544cec47e3dSKiyoshi Ueda */ 1545cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q) 1546cec47e3dSKiyoshi Ueda { 1547cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 1548cec47e3dSKiyoshi Ueda struct dm_table *map = dm_get_table(md); 1549cec47e3dSKiyoshi Ueda struct dm_target *ti; 1550b4324feeSKiyoshi Ueda struct request *rq, *clone; 1551cec47e3dSKiyoshi Ueda 1552cec47e3dSKiyoshi Ueda /* 1553b4324feeSKiyoshi Ueda * For suspend, check blk_queue_stopped() and increment 1554b4324feeSKiyoshi Ueda * ->pending within a single queue_lock not to increment the 1555b4324feeSKiyoshi Ueda * number of in-flight I/Os after the queue is stopped in 1556b4324feeSKiyoshi Ueda * dm_suspend(). 1557cec47e3dSKiyoshi Ueda */ 1558cec47e3dSKiyoshi Ueda while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) { 1559cec47e3dSKiyoshi Ueda rq = blk_peek_request(q); 1560cec47e3dSKiyoshi Ueda if (!rq) 1561cec47e3dSKiyoshi Ueda goto plug_and_out; 1562cec47e3dSKiyoshi Ueda 1563cec47e3dSKiyoshi Ueda ti = dm_table_find_target(map, blk_rq_pos(rq)); 1564cec47e3dSKiyoshi Ueda if (ti->type->busy && ti->type->busy(ti)) 1565cec47e3dSKiyoshi Ueda goto plug_and_out; 1566cec47e3dSKiyoshi Ueda 1567cec47e3dSKiyoshi Ueda blk_start_request(rq); 1568b4324feeSKiyoshi Ueda clone = rq->special; 1569b4324feeSKiyoshi Ueda atomic_inc(&md->pending[rq_data_dir(clone)]); 1570b4324feeSKiyoshi Ueda 1571cec47e3dSKiyoshi Ueda spin_unlock(q->queue_lock); 1572b4324feeSKiyoshi Ueda map_request(ti, clone, md); 1573cec47e3dSKiyoshi Ueda spin_lock_irq(q->queue_lock); 1574cec47e3dSKiyoshi Ueda } 1575cec47e3dSKiyoshi Ueda 1576cec47e3dSKiyoshi Ueda goto out; 1577cec47e3dSKiyoshi Ueda 1578cec47e3dSKiyoshi Ueda plug_and_out: 1579cec47e3dSKiyoshi Ueda if (!elv_queue_empty(q)) 1580cec47e3dSKiyoshi Ueda /* Some requests still remain, retry later */ 1581cec47e3dSKiyoshi Ueda blk_plug_device(q); 1582cec47e3dSKiyoshi Ueda 1583cec47e3dSKiyoshi Ueda out: 1584cec47e3dSKiyoshi Ueda dm_table_put(map); 1585cec47e3dSKiyoshi Ueda 1586cec47e3dSKiyoshi Ueda return; 1587cec47e3dSKiyoshi Ueda } 1588cec47e3dSKiyoshi Ueda 1589cec47e3dSKiyoshi Ueda int dm_underlying_device_busy(struct request_queue *q) 1590cec47e3dSKiyoshi Ueda { 1591cec47e3dSKiyoshi Ueda return blk_lld_busy(q); 1592cec47e3dSKiyoshi Ueda } 1593cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_underlying_device_busy); 1594cec47e3dSKiyoshi Ueda 1595cec47e3dSKiyoshi Ueda static int dm_lld_busy(struct request_queue *q) 1596cec47e3dSKiyoshi Ueda { 1597cec47e3dSKiyoshi Ueda int r; 1598cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 1599cec47e3dSKiyoshi Ueda struct dm_table *map = dm_get_table(md); 1600cec47e3dSKiyoshi Ueda 1601cec47e3dSKiyoshi Ueda if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) 1602cec47e3dSKiyoshi Ueda r = 1; 1603cec47e3dSKiyoshi Ueda else 1604cec47e3dSKiyoshi Ueda r = dm_table_any_busy_target(map); 1605cec47e3dSKiyoshi Ueda 1606cec47e3dSKiyoshi Ueda dm_table_put(map); 1607cec47e3dSKiyoshi Ueda 1608cec47e3dSKiyoshi Ueda return r; 1609cec47e3dSKiyoshi Ueda } 1610cec47e3dSKiyoshi Ueda 1611165125e1SJens Axboe static void dm_unplug_all(struct request_queue *q) 16121da177e4SLinus Torvalds { 16131da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 16141da177e4SLinus Torvalds struct dm_table *map = dm_get_table(md); 16151da177e4SLinus Torvalds 16161da177e4SLinus Torvalds if (map) { 1617cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 1618cec47e3dSKiyoshi Ueda generic_unplug_device(q); 1619cec47e3dSKiyoshi Ueda 16201da177e4SLinus Torvalds dm_table_unplug_all(map); 16211da177e4SLinus Torvalds dm_table_put(map); 16221da177e4SLinus Torvalds } 16231da177e4SLinus Torvalds } 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits) 16261da177e4SLinus Torvalds { 16278a57dfc6SChandra Seetharaman int r = bdi_bits; 16288a57dfc6SChandra Seetharaman struct mapped_device *md = congested_data; 16298a57dfc6SChandra Seetharaman struct dm_table *map; 16301da177e4SLinus Torvalds 16311eb787ecSAlasdair G Kergon if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 16328a57dfc6SChandra Seetharaman map = dm_get_table(md); 16338a57dfc6SChandra Seetharaman if (map) { 1634cec47e3dSKiyoshi Ueda /* 1635cec47e3dSKiyoshi Ueda * Request-based dm cares about only own queue for 1636cec47e3dSKiyoshi Ueda * the query about congestion status of request_queue 1637cec47e3dSKiyoshi Ueda */ 1638cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 1639cec47e3dSKiyoshi Ueda r = md->queue->backing_dev_info.state & 1640cec47e3dSKiyoshi Ueda bdi_bits; 1641cec47e3dSKiyoshi Ueda else 16421da177e4SLinus Torvalds r = dm_table_any_congested(map, bdi_bits); 1643cec47e3dSKiyoshi Ueda 16441da177e4SLinus Torvalds dm_table_put(map); 16458a57dfc6SChandra Seetharaman } 16468a57dfc6SChandra Seetharaman } 16478a57dfc6SChandra Seetharaman 16481da177e4SLinus Torvalds return r; 16491da177e4SLinus Torvalds } 16501da177e4SLinus Torvalds 16511da177e4SLinus Torvalds /*----------------------------------------------------------------- 16521da177e4SLinus Torvalds * An IDR is used to keep track of allocated minor numbers. 16531da177e4SLinus Torvalds *---------------------------------------------------------------*/ 16541da177e4SLinus Torvalds static DEFINE_IDR(_minor_idr); 16551da177e4SLinus Torvalds 16562b06cfffSAlasdair G Kergon static void free_minor(int minor) 16571da177e4SLinus Torvalds { 1658f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 16591da177e4SLinus Torvalds idr_remove(&_minor_idr, minor); 1660f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 16611da177e4SLinus Torvalds } 16621da177e4SLinus Torvalds 16631da177e4SLinus Torvalds /* 16641da177e4SLinus Torvalds * See if the device with a specific minor # is free. 16651da177e4SLinus Torvalds */ 1666cf13ab8eSFrederik Deweerdt static int specific_minor(int minor) 16671da177e4SLinus Torvalds { 16681da177e4SLinus Torvalds int r, m; 16691da177e4SLinus Torvalds 16701da177e4SLinus Torvalds if (minor >= (1 << MINORBITS)) 16711da177e4SLinus Torvalds return -EINVAL; 16721da177e4SLinus Torvalds 167362f75c2fSJeff Mahoney r = idr_pre_get(&_minor_idr, GFP_KERNEL); 167462f75c2fSJeff Mahoney if (!r) 167562f75c2fSJeff Mahoney return -ENOMEM; 167662f75c2fSJeff Mahoney 1677f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds if (idr_find(&_minor_idr, minor)) { 16801da177e4SLinus Torvalds r = -EBUSY; 16811da177e4SLinus Torvalds goto out; 16821da177e4SLinus Torvalds } 16831da177e4SLinus Torvalds 1684ba61fdd1SJeff Mahoney r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m); 168562f75c2fSJeff Mahoney if (r) 16861da177e4SLinus Torvalds goto out; 16871da177e4SLinus Torvalds 16881da177e4SLinus Torvalds if (m != minor) { 16891da177e4SLinus Torvalds idr_remove(&_minor_idr, m); 16901da177e4SLinus Torvalds r = -EBUSY; 16911da177e4SLinus Torvalds goto out; 16921da177e4SLinus Torvalds } 16931da177e4SLinus Torvalds 16941da177e4SLinus Torvalds out: 1695f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 16961da177e4SLinus Torvalds return r; 16971da177e4SLinus Torvalds } 16981da177e4SLinus Torvalds 1699cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor) 17001da177e4SLinus Torvalds { 17012b06cfffSAlasdair G Kergon int r, m; 17021da177e4SLinus Torvalds 17031da177e4SLinus Torvalds r = idr_pre_get(&_minor_idr, GFP_KERNEL); 170462f75c2fSJeff Mahoney if (!r) 170562f75c2fSJeff Mahoney return -ENOMEM; 170662f75c2fSJeff Mahoney 1707f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 17081da177e4SLinus Torvalds 1709ba61fdd1SJeff Mahoney r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m); 1710cf13ab8eSFrederik Deweerdt if (r) 17111da177e4SLinus Torvalds goto out; 17121da177e4SLinus Torvalds 17131da177e4SLinus Torvalds if (m >= (1 << MINORBITS)) { 17141da177e4SLinus Torvalds idr_remove(&_minor_idr, m); 17151da177e4SLinus Torvalds r = -ENOSPC; 17161da177e4SLinus Torvalds goto out; 17171da177e4SLinus Torvalds } 17181da177e4SLinus Torvalds 17191da177e4SLinus Torvalds *minor = m; 17201da177e4SLinus Torvalds 17211da177e4SLinus Torvalds out: 1722f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 17231da177e4SLinus Torvalds return r; 17241da177e4SLinus Torvalds } 17251da177e4SLinus Torvalds 172683d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops; 17271da177e4SLinus Torvalds 172853d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work); 172953d5914fSMikulas Patocka 17301da177e4SLinus Torvalds /* 17311da177e4SLinus Torvalds * Allocate and initialise a blank device with a given minor. 17321da177e4SLinus Torvalds */ 17332b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor) 17341da177e4SLinus Torvalds { 17351da177e4SLinus Torvalds int r; 1736cf13ab8eSFrederik Deweerdt struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL); 1737ba61fdd1SJeff Mahoney void *old_md; 17381da177e4SLinus Torvalds 17391da177e4SLinus Torvalds if (!md) { 17401da177e4SLinus Torvalds DMWARN("unable to allocate device, out of memory."); 17411da177e4SLinus Torvalds return NULL; 17421da177e4SLinus Torvalds } 17431da177e4SLinus Torvalds 174410da4f79SJeff Mahoney if (!try_module_get(THIS_MODULE)) 17456ed7ade8SMilan Broz goto bad_module_get; 174610da4f79SJeff Mahoney 17471da177e4SLinus Torvalds /* get a minor number for the dev */ 17482b06cfffSAlasdair G Kergon if (minor == DM_ANY_MINOR) 1749cf13ab8eSFrederik Deweerdt r = next_free_minor(&minor); 17502b06cfffSAlasdair G Kergon else 1751cf13ab8eSFrederik Deweerdt r = specific_minor(minor); 17521da177e4SLinus Torvalds if (r < 0) 17536ed7ade8SMilan Broz goto bad_minor; 17541da177e4SLinus Torvalds 17552ca3310eSAlasdair G Kergon init_rwsem(&md->io_lock); 1756e61290a4SDaniel Walker mutex_init(&md->suspend_lock); 1757022c2611SMikulas Patocka spin_lock_init(&md->deferred_lock); 17581da177e4SLinus Torvalds rwlock_init(&md->map_lock); 17591da177e4SLinus Torvalds atomic_set(&md->holders, 1); 17605c6bd75dSAlasdair G Kergon atomic_set(&md->open_count, 0); 17611da177e4SLinus Torvalds atomic_set(&md->event_nr, 0); 17627a8c3d3bSMike Anderson atomic_set(&md->uevent_seq, 0); 17637a8c3d3bSMike Anderson INIT_LIST_HEAD(&md->uevent_list); 17647a8c3d3bSMike Anderson spin_lock_init(&md->uevent_lock); 17651da177e4SLinus Torvalds 1766e6ee8c0bSKiyoshi Ueda md->queue = blk_init_queue(dm_request_fn, NULL); 17671da177e4SLinus Torvalds if (!md->queue) 17686ed7ade8SMilan Broz goto bad_queue; 17691da177e4SLinus Torvalds 1770e6ee8c0bSKiyoshi Ueda /* 1771e6ee8c0bSKiyoshi Ueda * Request-based dm devices cannot be stacked on top of bio-based dm 1772e6ee8c0bSKiyoshi Ueda * devices. The type of this dm device has not been decided yet, 1773e6ee8c0bSKiyoshi Ueda * although we initialized the queue using blk_init_queue(). 1774e6ee8c0bSKiyoshi Ueda * The type is decided at the first table loading time. 1775e6ee8c0bSKiyoshi Ueda * To prevent problematic device stacking, clear the queue flag 1776e6ee8c0bSKiyoshi Ueda * for request stacking support until then. 1777e6ee8c0bSKiyoshi Ueda * 1778e6ee8c0bSKiyoshi Ueda * This queue is new, so no concurrency on the queue_flags. 1779e6ee8c0bSKiyoshi Ueda */ 1780e6ee8c0bSKiyoshi Ueda queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue); 1781e6ee8c0bSKiyoshi Ueda md->saved_make_request_fn = md->queue->make_request_fn; 17821da177e4SLinus Torvalds md->queue->queuedata = md; 17831da177e4SLinus Torvalds md->queue->backing_dev_info.congested_fn = dm_any_congested; 17841da177e4SLinus Torvalds md->queue->backing_dev_info.congested_data = md; 17851da177e4SLinus Torvalds blk_queue_make_request(md->queue, dm_request); 1786daef265fSJens Axboe blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY); 17871da177e4SLinus Torvalds md->queue->unplug_fn = dm_unplug_all; 1788f6fccb12SMilan Broz blk_queue_merge_bvec(md->queue, dm_merge_bvec); 1789e6ee8c0bSKiyoshi Ueda blk_queue_softirq_done(md->queue, dm_softirq_done); 1790e6ee8c0bSKiyoshi Ueda blk_queue_prep_rq(md->queue, dm_prep_fn); 1791e6ee8c0bSKiyoshi Ueda blk_queue_lld_busy(md->queue, dm_lld_busy); 17929faf400fSStefan Bader 17931da177e4SLinus Torvalds md->disk = alloc_disk(1); 17941da177e4SLinus Torvalds if (!md->disk) 17956ed7ade8SMilan Broz goto bad_disk; 17961da177e4SLinus Torvalds 1797316d315bSNikanth Karthikesan atomic_set(&md->pending[0], 0); 1798316d315bSNikanth Karthikesan atomic_set(&md->pending[1], 0); 1799f0b04115SJeff Mahoney init_waitqueue_head(&md->wait); 180053d5914fSMikulas Patocka INIT_WORK(&md->work, dm_wq_work); 1801f0b04115SJeff Mahoney init_waitqueue_head(&md->eventq); 1802f0b04115SJeff Mahoney 18031da177e4SLinus Torvalds md->disk->major = _major; 18041da177e4SLinus Torvalds md->disk->first_minor = minor; 18051da177e4SLinus Torvalds md->disk->fops = &dm_blk_dops; 18061da177e4SLinus Torvalds md->disk->queue = md->queue; 18071da177e4SLinus Torvalds md->disk->private_data = md; 18081da177e4SLinus Torvalds sprintf(md->disk->disk_name, "dm-%d", minor); 18091da177e4SLinus Torvalds add_disk(md->disk); 18107e51f257SMike Anderson format_dev_t(md->name, MKDEV(_major, minor)); 18111da177e4SLinus Torvalds 1812304f3f6aSMilan Broz md->wq = create_singlethread_workqueue("kdmflush"); 1813304f3f6aSMilan Broz if (!md->wq) 1814304f3f6aSMilan Broz goto bad_thread; 1815304f3f6aSMilan Broz 181632a926daSMikulas Patocka md->bdev = bdget_disk(md->disk, 0); 181732a926daSMikulas Patocka if (!md->bdev) 181832a926daSMikulas Patocka goto bad_bdev; 181932a926daSMikulas Patocka 1820ba61fdd1SJeff Mahoney /* Populate the mapping, nobody knows we exist yet */ 1821f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 1822ba61fdd1SJeff Mahoney old_md = idr_replace(&_minor_idr, md, minor); 1823f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1824ba61fdd1SJeff Mahoney 1825ba61fdd1SJeff Mahoney BUG_ON(old_md != MINOR_ALLOCED); 1826ba61fdd1SJeff Mahoney 18271da177e4SLinus Torvalds return md; 18281da177e4SLinus Torvalds 182932a926daSMikulas Patocka bad_bdev: 183032a926daSMikulas Patocka destroy_workqueue(md->wq); 1831304f3f6aSMilan Broz bad_thread: 183203022c54SZdenek Kabelac del_gendisk(md->disk); 1833304f3f6aSMilan Broz put_disk(md->disk); 18346ed7ade8SMilan Broz bad_disk: 18351312f40eSAl Viro blk_cleanup_queue(md->queue); 18366ed7ade8SMilan Broz bad_queue: 18371da177e4SLinus Torvalds free_minor(minor); 18386ed7ade8SMilan Broz bad_minor: 183910da4f79SJeff Mahoney module_put(THIS_MODULE); 18406ed7ade8SMilan Broz bad_module_get: 18411da177e4SLinus Torvalds kfree(md); 18421da177e4SLinus Torvalds return NULL; 18431da177e4SLinus Torvalds } 18441da177e4SLinus Torvalds 1845ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md); 1846ae9da83fSJun'ichi Nomura 18471da177e4SLinus Torvalds static void free_dev(struct mapped_device *md) 18481da177e4SLinus Torvalds { 1849f331c029STejun Heo int minor = MINOR(disk_devt(md->disk)); 185063d94e48SJun'ichi Nomura 1851ae9da83fSJun'ichi Nomura unlock_fs(md); 1852db8fef4fSMikulas Patocka bdput(md->bdev); 1853304f3f6aSMilan Broz destroy_workqueue(md->wq); 1854e6ee8c0bSKiyoshi Ueda if (md->tio_pool) 18551da177e4SLinus Torvalds mempool_destroy(md->tio_pool); 1856e6ee8c0bSKiyoshi Ueda if (md->io_pool) 18571da177e4SLinus Torvalds mempool_destroy(md->io_pool); 1858e6ee8c0bSKiyoshi Ueda if (md->bs) 18599faf400fSStefan Bader bioset_free(md->bs); 18609c47008dSMartin K. Petersen blk_integrity_unregister(md->disk); 18611da177e4SLinus Torvalds del_gendisk(md->disk); 186263d94e48SJun'ichi Nomura free_minor(minor); 1863fba9f90eSJeff Mahoney 1864fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 1865fba9f90eSJeff Mahoney md->disk->private_data = NULL; 1866fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 1867fba9f90eSJeff Mahoney 18681da177e4SLinus Torvalds put_disk(md->disk); 18691312f40eSAl Viro blk_cleanup_queue(md->queue); 187010da4f79SJeff Mahoney module_put(THIS_MODULE); 18711da177e4SLinus Torvalds kfree(md); 18721da177e4SLinus Torvalds } 18731da177e4SLinus Torvalds 1874e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t) 1875e6ee8c0bSKiyoshi Ueda { 1876e6ee8c0bSKiyoshi Ueda struct dm_md_mempools *p; 1877e6ee8c0bSKiyoshi Ueda 1878e6ee8c0bSKiyoshi Ueda if (md->io_pool && md->tio_pool && md->bs) 1879e6ee8c0bSKiyoshi Ueda /* the md already has necessary mempools */ 1880e6ee8c0bSKiyoshi Ueda goto out; 1881e6ee8c0bSKiyoshi Ueda 1882e6ee8c0bSKiyoshi Ueda p = dm_table_get_md_mempools(t); 1883e6ee8c0bSKiyoshi Ueda BUG_ON(!p || md->io_pool || md->tio_pool || md->bs); 1884e6ee8c0bSKiyoshi Ueda 1885e6ee8c0bSKiyoshi Ueda md->io_pool = p->io_pool; 1886e6ee8c0bSKiyoshi Ueda p->io_pool = NULL; 1887e6ee8c0bSKiyoshi Ueda md->tio_pool = p->tio_pool; 1888e6ee8c0bSKiyoshi Ueda p->tio_pool = NULL; 1889e6ee8c0bSKiyoshi Ueda md->bs = p->bs; 1890e6ee8c0bSKiyoshi Ueda p->bs = NULL; 1891e6ee8c0bSKiyoshi Ueda 1892e6ee8c0bSKiyoshi Ueda out: 1893e6ee8c0bSKiyoshi Ueda /* mempool bind completed, now no need any mempools in the table */ 1894e6ee8c0bSKiyoshi Ueda dm_table_free_md_mempools(t); 1895e6ee8c0bSKiyoshi Ueda } 1896e6ee8c0bSKiyoshi Ueda 18971da177e4SLinus Torvalds /* 18981da177e4SLinus Torvalds * Bind a table to the device. 18991da177e4SLinus Torvalds */ 19001da177e4SLinus Torvalds static void event_callback(void *context) 19011da177e4SLinus Torvalds { 19027a8c3d3bSMike Anderson unsigned long flags; 19037a8c3d3bSMike Anderson LIST_HEAD(uevents); 19041da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) context; 19051da177e4SLinus Torvalds 19067a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 19077a8c3d3bSMike Anderson list_splice_init(&md->uevent_list, &uevents); 19087a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 19097a8c3d3bSMike Anderson 1910ed9e1982STejun Heo dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); 19117a8c3d3bSMike Anderson 19121da177e4SLinus Torvalds atomic_inc(&md->event_nr); 19131da177e4SLinus Torvalds wake_up(&md->eventq); 19141da177e4SLinus Torvalds } 19151da177e4SLinus Torvalds 19164e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size) 19171da177e4SLinus Torvalds { 19184e90188bSAlasdair G Kergon set_capacity(md->disk, size); 19191da177e4SLinus Torvalds 1920db8fef4fSMikulas Patocka mutex_lock(&md->bdev->bd_inode->i_mutex); 1921db8fef4fSMikulas Patocka i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT); 1922db8fef4fSMikulas Patocka mutex_unlock(&md->bdev->bd_inode->i_mutex); 19231da177e4SLinus Torvalds } 19241da177e4SLinus Torvalds 1925754c5fc7SMike Snitzer static int __bind(struct mapped_device *md, struct dm_table *t, 1926754c5fc7SMike Snitzer struct queue_limits *limits) 19271da177e4SLinus Torvalds { 1928165125e1SJens Axboe struct request_queue *q = md->queue; 19291da177e4SLinus Torvalds sector_t size; 1930523d9297SKiyoshi Ueda unsigned long flags; 19311da177e4SLinus Torvalds 19321da177e4SLinus Torvalds size = dm_table_get_size(t); 19333ac51e74SDarrick J. Wong 19343ac51e74SDarrick J. Wong /* 19353ac51e74SDarrick J. Wong * Wipe any geometry if the size of the table changed. 19363ac51e74SDarrick J. Wong */ 19373ac51e74SDarrick J. Wong if (size != get_capacity(md->disk)) 19383ac51e74SDarrick J. Wong memset(&md->geometry, 0, sizeof(md->geometry)); 19393ac51e74SDarrick J. Wong 19404e90188bSAlasdair G Kergon __set_size(md, size); 19411da177e4SLinus Torvalds 1942d5816876SMikulas Patocka if (!size) { 1943d5816876SMikulas Patocka dm_table_destroy(t); 1944d5816876SMikulas Patocka return 0; 1945d5816876SMikulas Patocka } 1946d5816876SMikulas Patocka 1947cf222b37SAlasdair G Kergon dm_table_event_callback(t, event_callback, md); 19482ca3310eSAlasdair G Kergon 1949e6ee8c0bSKiyoshi Ueda /* 1950e6ee8c0bSKiyoshi Ueda * The queue hasn't been stopped yet, if the old table type wasn't 1951e6ee8c0bSKiyoshi Ueda * for request-based during suspension. So stop it to prevent 1952e6ee8c0bSKiyoshi Ueda * I/O mapping before resume. 1953e6ee8c0bSKiyoshi Ueda * This must be done before setting the queue restrictions, 1954e6ee8c0bSKiyoshi Ueda * because request-based dm may be run just after the setting. 1955e6ee8c0bSKiyoshi Ueda */ 1956e6ee8c0bSKiyoshi Ueda if (dm_table_request_based(t) && !blk_queue_stopped(q)) 1957e6ee8c0bSKiyoshi Ueda stop_queue(q); 1958e6ee8c0bSKiyoshi Ueda 1959e6ee8c0bSKiyoshi Ueda __bind_mempools(md, t); 1960e6ee8c0bSKiyoshi Ueda 1961523d9297SKiyoshi Ueda write_lock_irqsave(&md->map_lock, flags); 19622ca3310eSAlasdair G Kergon md->map = t; 1963754c5fc7SMike Snitzer dm_table_set_restrictions(t, q, limits); 1964523d9297SKiyoshi Ueda write_unlock_irqrestore(&md->map_lock, flags); 19652ca3310eSAlasdair G Kergon 19661da177e4SLinus Torvalds return 0; 19671da177e4SLinus Torvalds } 19681da177e4SLinus Torvalds 19691da177e4SLinus Torvalds static void __unbind(struct mapped_device *md) 19701da177e4SLinus Torvalds { 19711da177e4SLinus Torvalds struct dm_table *map = md->map; 1972523d9297SKiyoshi Ueda unsigned long flags; 19731da177e4SLinus Torvalds 19741da177e4SLinus Torvalds if (!map) 19751da177e4SLinus Torvalds return; 19761da177e4SLinus Torvalds 19771da177e4SLinus Torvalds dm_table_event_callback(map, NULL, NULL); 1978523d9297SKiyoshi Ueda write_lock_irqsave(&md->map_lock, flags); 19791da177e4SLinus Torvalds md->map = NULL; 1980523d9297SKiyoshi Ueda write_unlock_irqrestore(&md->map_lock, flags); 1981d5816876SMikulas Patocka dm_table_destroy(map); 19821da177e4SLinus Torvalds } 19831da177e4SLinus Torvalds 19841da177e4SLinus Torvalds /* 19851da177e4SLinus Torvalds * Constructor for a new device. 19861da177e4SLinus Torvalds */ 19872b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result) 19881da177e4SLinus Torvalds { 19891da177e4SLinus Torvalds struct mapped_device *md; 19901da177e4SLinus Torvalds 19912b06cfffSAlasdair G Kergon md = alloc_dev(minor); 19921da177e4SLinus Torvalds if (!md) 19931da177e4SLinus Torvalds return -ENXIO; 19941da177e4SLinus Torvalds 1995784aae73SMilan Broz dm_sysfs_init(md); 1996784aae73SMilan Broz 19971da177e4SLinus Torvalds *result = md; 19981da177e4SLinus Torvalds return 0; 19991da177e4SLinus Torvalds } 20001da177e4SLinus Torvalds 2001637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev) 20021da177e4SLinus Torvalds { 20031da177e4SLinus Torvalds struct mapped_device *md; 20041da177e4SLinus Torvalds unsigned minor = MINOR(dev); 20051da177e4SLinus Torvalds 20061da177e4SLinus Torvalds if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 20071da177e4SLinus Torvalds return NULL; 20081da177e4SLinus Torvalds 2009f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 20101da177e4SLinus Torvalds 20111da177e4SLinus Torvalds md = idr_find(&_minor_idr, minor); 2012fba9f90eSJeff Mahoney if (md && (md == MINOR_ALLOCED || 2013f331c029STejun Heo (MINOR(disk_devt(dm_disk(md))) != minor) || 2014fba9f90eSJeff Mahoney test_bit(DMF_FREEING, &md->flags))) { 2015637842cfSDavid Teigland md = NULL; 2016fba9f90eSJeff Mahoney goto out; 2017fba9f90eSJeff Mahoney } 20181da177e4SLinus Torvalds 2019fba9f90eSJeff Mahoney out: 2020f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 20211da177e4SLinus Torvalds 2022637842cfSDavid Teigland return md; 2023637842cfSDavid Teigland } 2024637842cfSDavid Teigland 2025d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev) 2026d229a958SDavid Teigland { 2027d229a958SDavid Teigland struct mapped_device *md = dm_find_md(dev); 2028d229a958SDavid Teigland 2029d229a958SDavid Teigland if (md) 2030d229a958SDavid Teigland dm_get(md); 2031d229a958SDavid Teigland 2032d229a958SDavid Teigland return md; 2033d229a958SDavid Teigland } 2034d229a958SDavid Teigland 20359ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md) 2036637842cfSDavid Teigland { 20379ade92a9SAlasdair G Kergon return md->interface_ptr; 20381da177e4SLinus Torvalds } 20391da177e4SLinus Torvalds 20401da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr) 20411da177e4SLinus Torvalds { 20421da177e4SLinus Torvalds md->interface_ptr = ptr; 20431da177e4SLinus Torvalds } 20441da177e4SLinus Torvalds 20451da177e4SLinus Torvalds void dm_get(struct mapped_device *md) 20461da177e4SLinus Torvalds { 20471da177e4SLinus Torvalds atomic_inc(&md->holders); 20481da177e4SLinus Torvalds } 20491da177e4SLinus Torvalds 205072d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md) 205172d94861SAlasdair G Kergon { 205272d94861SAlasdair G Kergon return md->name; 205372d94861SAlasdair G Kergon } 205472d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name); 205572d94861SAlasdair G Kergon 20561da177e4SLinus Torvalds void dm_put(struct mapped_device *md) 20571da177e4SLinus Torvalds { 20581134e5aeSMike Anderson struct dm_table *map; 20591da177e4SLinus Torvalds 2060fba9f90eSJeff Mahoney BUG_ON(test_bit(DMF_FREEING, &md->flags)); 2061fba9f90eSJeff Mahoney 2062f32c10b0SJeff Mahoney if (atomic_dec_and_lock(&md->holders, &_minor_lock)) { 20631134e5aeSMike Anderson map = dm_get_table(md); 2064f331c029STejun Heo idr_replace(&_minor_idr, MINOR_ALLOCED, 2065f331c029STejun Heo MINOR(disk_devt(dm_disk(md)))); 2066fba9f90eSJeff Mahoney set_bit(DMF_FREEING, &md->flags); 2067f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 2068cf222b37SAlasdair G Kergon if (!dm_suspended(md)) { 20691da177e4SLinus Torvalds dm_table_presuspend_targets(map); 20701da177e4SLinus Torvalds dm_table_postsuspend_targets(map); 20711da177e4SLinus Torvalds } 2072784aae73SMilan Broz dm_sysfs_exit(md); 20731134e5aeSMike Anderson dm_table_put(map); 2074a1b51e98SMikulas Patocka __unbind(md); 20751da177e4SLinus Torvalds free_dev(md); 20761da177e4SLinus Torvalds } 20771da177e4SLinus Torvalds } 207879eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put); 20791da177e4SLinus Torvalds 2080401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible) 208146125c1cSMilan Broz { 208246125c1cSMilan Broz int r = 0; 2083b44ebeb0SMikulas Patocka DECLARE_WAITQUEUE(wait, current); 2084b44ebeb0SMikulas Patocka 2085b44ebeb0SMikulas Patocka dm_unplug_all(md->queue); 2086b44ebeb0SMikulas Patocka 2087b44ebeb0SMikulas Patocka add_wait_queue(&md->wait, &wait); 208846125c1cSMilan Broz 208946125c1cSMilan Broz while (1) { 2090401600dfSMikulas Patocka set_current_state(interruptible); 209146125c1cSMilan Broz 209246125c1cSMilan Broz smp_mb(); 2093b4324feeSKiyoshi Ueda if (!md_in_flight(md)) 209446125c1cSMilan Broz break; 209546125c1cSMilan Broz 2096401600dfSMikulas Patocka if (interruptible == TASK_INTERRUPTIBLE && 2097401600dfSMikulas Patocka signal_pending(current)) { 209846125c1cSMilan Broz r = -EINTR; 209946125c1cSMilan Broz break; 210046125c1cSMilan Broz } 210146125c1cSMilan Broz 210246125c1cSMilan Broz io_schedule(); 210346125c1cSMilan Broz } 210446125c1cSMilan Broz set_current_state(TASK_RUNNING); 210546125c1cSMilan Broz 2106b44ebeb0SMikulas Patocka remove_wait_queue(&md->wait, &wait); 2107b44ebeb0SMikulas Patocka 210846125c1cSMilan Broz return r; 210946125c1cSMilan Broz } 211046125c1cSMilan Broz 2111531fe963SMikulas Patocka static void dm_flush(struct mapped_device *md) 2112af7e466aSMikulas Patocka { 2113af7e466aSMikulas Patocka dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); 211452b1fd5aSMikulas Patocka 211552b1fd5aSMikulas Patocka bio_init(&md->barrier_bio); 211652b1fd5aSMikulas Patocka md->barrier_bio.bi_bdev = md->bdev; 211752b1fd5aSMikulas Patocka md->barrier_bio.bi_rw = WRITE_BARRIER; 211852b1fd5aSMikulas Patocka __split_and_process_bio(md, &md->barrier_bio); 211952b1fd5aSMikulas Patocka 212052b1fd5aSMikulas Patocka dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); 2121af7e466aSMikulas Patocka } 2122af7e466aSMikulas Patocka 2123af7e466aSMikulas Patocka static void process_barrier(struct mapped_device *md, struct bio *bio) 2124af7e466aSMikulas Patocka { 21255aa2781dSMikulas Patocka md->barrier_error = 0; 21265aa2781dSMikulas Patocka 2127531fe963SMikulas Patocka dm_flush(md); 2128af7e466aSMikulas Patocka 21295aa2781dSMikulas Patocka if (!bio_empty_barrier(bio)) { 2130af7e466aSMikulas Patocka __split_and_process_bio(md, bio); 2131531fe963SMikulas Patocka dm_flush(md); 21325aa2781dSMikulas Patocka } 2133af7e466aSMikulas Patocka 2134af7e466aSMikulas Patocka if (md->barrier_error != DM_ENDIO_REQUEUE) 2135531fe963SMikulas Patocka bio_endio(bio, md->barrier_error); 21362761e95fSMikulas Patocka else { 21372761e95fSMikulas Patocka spin_lock_irq(&md->deferred_lock); 21382761e95fSMikulas Patocka bio_list_add_head(&md->deferred, bio); 21392761e95fSMikulas Patocka spin_unlock_irq(&md->deferred_lock); 21402761e95fSMikulas Patocka } 2141af7e466aSMikulas Patocka } 2142af7e466aSMikulas Patocka 21431da177e4SLinus Torvalds /* 21441da177e4SLinus Torvalds * Process the deferred bios 21451da177e4SLinus Torvalds */ 2146ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work) 21471da177e4SLinus Torvalds { 2148ef208587SMikulas Patocka struct mapped_device *md = container_of(work, struct mapped_device, 2149ef208587SMikulas Patocka work); 21506d6f10dfSMilan Broz struct bio *c; 21511da177e4SLinus Torvalds 2152ef208587SMikulas Patocka down_write(&md->io_lock); 2153ef208587SMikulas Patocka 21543b00b203SMikulas Patocka while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 2155022c2611SMikulas Patocka spin_lock_irq(&md->deferred_lock); 2156022c2611SMikulas Patocka c = bio_list_pop(&md->deferred); 2157022c2611SMikulas Patocka spin_unlock_irq(&md->deferred_lock); 2158022c2611SMikulas Patocka 2159df12ee99SAlasdair G Kergon if (!c) { 21601eb787ecSAlasdair G Kergon clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags); 2161df12ee99SAlasdair G Kergon break; 2162022c2611SMikulas Patocka } 216373d410c0SMilan Broz 21643b00b203SMikulas Patocka up_write(&md->io_lock); 21653b00b203SMikulas Patocka 2166e6ee8c0bSKiyoshi Ueda if (dm_request_based(md)) 2167e6ee8c0bSKiyoshi Ueda generic_make_request(c); 2168e6ee8c0bSKiyoshi Ueda else { 21691f98a13fSJens Axboe if (bio_rw_flagged(c, BIO_RW_BARRIER)) 2170af7e466aSMikulas Patocka process_barrier(md, c); 2171af7e466aSMikulas Patocka else 2172df12ee99SAlasdair G Kergon __split_and_process_bio(md, c); 2173e6ee8c0bSKiyoshi Ueda } 21743b00b203SMikulas Patocka 21753b00b203SMikulas Patocka down_write(&md->io_lock); 2176df12ee99SAlasdair G Kergon } 2177ef208587SMikulas Patocka 2178ef208587SMikulas Patocka up_write(&md->io_lock); 21791da177e4SLinus Torvalds } 21801da177e4SLinus Torvalds 21819a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md) 2182304f3f6aSMilan Broz { 21833b00b203SMikulas Patocka clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 21843b00b203SMikulas Patocka smp_mb__after_clear_bit(); 218553d5914fSMikulas Patocka queue_work(md->wq, &md->work); 2186304f3f6aSMilan Broz } 2187304f3f6aSMilan Broz 21881da177e4SLinus Torvalds /* 21891da177e4SLinus Torvalds * Swap in a new table (destroying old one). 21901da177e4SLinus Torvalds */ 21911da177e4SLinus Torvalds int dm_swap_table(struct mapped_device *md, struct dm_table *table) 21921da177e4SLinus Torvalds { 2193754c5fc7SMike Snitzer struct queue_limits limits; 219493c534aeSAlasdair G Kergon int r = -EINVAL; 21951da177e4SLinus Torvalds 2196e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 21971da177e4SLinus Torvalds 21981da177e4SLinus Torvalds /* device must be suspended */ 2199cf222b37SAlasdair G Kergon if (!dm_suspended(md)) 220093c534aeSAlasdair G Kergon goto out; 22011da177e4SLinus Torvalds 2202754c5fc7SMike Snitzer r = dm_calculate_queue_limits(table, &limits); 2203754c5fc7SMike Snitzer if (r) 2204754c5fc7SMike Snitzer goto out; 2205754c5fc7SMike Snitzer 2206e6ee8c0bSKiyoshi Ueda /* cannot change the device type, once a table is bound */ 2207e6ee8c0bSKiyoshi Ueda if (md->map && 2208e6ee8c0bSKiyoshi Ueda (dm_table_get_type(md->map) != dm_table_get_type(table))) { 2209e6ee8c0bSKiyoshi Ueda DMWARN("can't change the device type after a table is bound"); 2210e6ee8c0bSKiyoshi Ueda goto out; 2211e6ee8c0bSKiyoshi Ueda } 2212e6ee8c0bSKiyoshi Ueda 22131da177e4SLinus Torvalds __unbind(md); 2214754c5fc7SMike Snitzer r = __bind(md, table, &limits); 22151da177e4SLinus Torvalds 221693c534aeSAlasdair G Kergon out: 2217e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 221893c534aeSAlasdair G Kergon return r; 22191da177e4SLinus Torvalds } 22201da177e4SLinus Torvalds 22211da177e4SLinus Torvalds /* 22221da177e4SLinus Torvalds * Functions to lock and unlock any filesystem running on the 22231da177e4SLinus Torvalds * device. 22241da177e4SLinus Torvalds */ 22252ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md) 22261da177e4SLinus Torvalds { 2227e39e2e95SAlasdair G Kergon int r; 22281da177e4SLinus Torvalds 22291da177e4SLinus Torvalds WARN_ON(md->frozen_sb); 2230dfbe03f6SAlasdair G Kergon 2231db8fef4fSMikulas Patocka md->frozen_sb = freeze_bdev(md->bdev); 2232dfbe03f6SAlasdair G Kergon if (IS_ERR(md->frozen_sb)) { 2233cf222b37SAlasdair G Kergon r = PTR_ERR(md->frozen_sb); 2234e39e2e95SAlasdair G Kergon md->frozen_sb = NULL; 2235e39e2e95SAlasdair G Kergon return r; 2236dfbe03f6SAlasdair G Kergon } 2237dfbe03f6SAlasdair G Kergon 2238aa8d7c2fSAlasdair G Kergon set_bit(DMF_FROZEN, &md->flags); 2239aa8d7c2fSAlasdair G Kergon 22401da177e4SLinus Torvalds return 0; 22411da177e4SLinus Torvalds } 22421da177e4SLinus Torvalds 22432ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md) 22441da177e4SLinus Torvalds { 2245aa8d7c2fSAlasdair G Kergon if (!test_bit(DMF_FROZEN, &md->flags)) 2246aa8d7c2fSAlasdair G Kergon return; 2247aa8d7c2fSAlasdair G Kergon 2248db8fef4fSMikulas Patocka thaw_bdev(md->bdev, md->frozen_sb); 22491da177e4SLinus Torvalds md->frozen_sb = NULL; 2250aa8d7c2fSAlasdair G Kergon clear_bit(DMF_FROZEN, &md->flags); 22511da177e4SLinus Torvalds } 22521da177e4SLinus Torvalds 22531da177e4SLinus Torvalds /* 22541da177e4SLinus Torvalds * We need to be able to change a mapping table under a mounted 22551da177e4SLinus Torvalds * filesystem. For example we might want to move some data in 22561da177e4SLinus Torvalds * the background. Before the table can be swapped with 22571da177e4SLinus Torvalds * dm_bind_table, dm_suspend must be called to flush any in 22581da177e4SLinus Torvalds * flight bios and ensure that any further io gets deferred. 22591da177e4SLinus Torvalds */ 2260cec47e3dSKiyoshi Ueda /* 2261cec47e3dSKiyoshi Ueda * Suspend mechanism in request-based dm. 2262cec47e3dSKiyoshi Ueda * 22639f518b27SKiyoshi Ueda * 1. Flush all I/Os by lock_fs() if needed. 22649f518b27SKiyoshi Ueda * 2. Stop dispatching any I/O by stopping the request_queue. 22659f518b27SKiyoshi Ueda * 3. Wait for all in-flight I/Os to be completed or requeued. 2266cec47e3dSKiyoshi Ueda * 22679f518b27SKiyoshi Ueda * To abort suspend, start the request_queue. 2268cec47e3dSKiyoshi Ueda */ 2269a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags) 22701da177e4SLinus Torvalds { 22712ca3310eSAlasdair G Kergon struct dm_table *map = NULL; 227246125c1cSMilan Broz int r = 0; 2273a3d77d35SKiyoshi Ueda int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0; 22742e93ccc1SKiyoshi Ueda int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0; 22751da177e4SLinus Torvalds 2276e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 22772ca3310eSAlasdair G Kergon 227873d410c0SMilan Broz if (dm_suspended(md)) { 227973d410c0SMilan Broz r = -EINVAL; 2280d287483dSAlasdair G Kergon goto out_unlock; 228173d410c0SMilan Broz } 22821da177e4SLinus Torvalds 22831da177e4SLinus Torvalds map = dm_get_table(md); 2284cf222b37SAlasdair G Kergon 22852e93ccc1SKiyoshi Ueda /* 22862e93ccc1SKiyoshi Ueda * DMF_NOFLUSH_SUSPENDING must be set before presuspend. 22872e93ccc1SKiyoshi Ueda * This flag is cleared before dm_suspend returns. 22882e93ccc1SKiyoshi Ueda */ 22892e93ccc1SKiyoshi Ueda if (noflush) 22902e93ccc1SKiyoshi Ueda set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 22912e93ccc1SKiyoshi Ueda 2292436d4108SAlasdair G Kergon /* This does not get reverted if there's an error later. */ 22931da177e4SLinus Torvalds dm_table_presuspend_targets(map); 22941da177e4SLinus Torvalds 22952e93ccc1SKiyoshi Ueda /* 22969f518b27SKiyoshi Ueda * Flush I/O to the device. 22979f518b27SKiyoshi Ueda * Any I/O submitted after lock_fs() may not be flushed. 22989f518b27SKiyoshi Ueda * noflush takes precedence over do_lockfs. 22999f518b27SKiyoshi Ueda * (lock_fs() flushes I/Os and waits for them to complete.) 23002e93ccc1SKiyoshi Ueda */ 230132a926daSMikulas Patocka if (!noflush && do_lockfs) { 23022ca3310eSAlasdair G Kergon r = lock_fs(md); 23032ca3310eSAlasdair G Kergon if (r) 23042ca3310eSAlasdair G Kergon goto out; 2305aa8d7c2fSAlasdair G Kergon } 23061da177e4SLinus Torvalds 23071da177e4SLinus Torvalds /* 23083b00b203SMikulas Patocka * Here we must make sure that no processes are submitting requests 23093b00b203SMikulas Patocka * to target drivers i.e. no one may be executing 23103b00b203SMikulas Patocka * __split_and_process_bio. This is called from dm_request and 23113b00b203SMikulas Patocka * dm_wq_work. 23123b00b203SMikulas Patocka * 23133b00b203SMikulas Patocka * To get all processes out of __split_and_process_bio in dm_request, 23143b00b203SMikulas Patocka * we take the write lock. To prevent any process from reentering 23153b00b203SMikulas Patocka * __split_and_process_bio from dm_request, we set 23163b00b203SMikulas Patocka * DMF_QUEUE_IO_TO_THREAD. 23173b00b203SMikulas Patocka * 23183b00b203SMikulas Patocka * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND 23193b00b203SMikulas Patocka * and call flush_workqueue(md->wq). flush_workqueue will wait until 23203b00b203SMikulas Patocka * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any 23213b00b203SMikulas Patocka * further calls to __split_and_process_bio from dm_wq_work. 23221da177e4SLinus Torvalds */ 23232ca3310eSAlasdair G Kergon down_write(&md->io_lock); 23241eb787ecSAlasdair G Kergon set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 23251eb787ecSAlasdair G Kergon set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags); 23262ca3310eSAlasdair G Kergon up_write(&md->io_lock); 23271da177e4SLinus Torvalds 23283b00b203SMikulas Patocka flush_workqueue(md->wq); 23293b00b203SMikulas Patocka 2330cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 23319f518b27SKiyoshi Ueda stop_queue(md->queue); 2332cec47e3dSKiyoshi Ueda 23331da177e4SLinus Torvalds /* 23343b00b203SMikulas Patocka * At this point no more requests are entering target request routines. 23353b00b203SMikulas Patocka * We call dm_wait_for_completion to wait for all existing requests 23363b00b203SMikulas Patocka * to finish. 23371da177e4SLinus Torvalds */ 2338401600dfSMikulas Patocka r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE); 23391da177e4SLinus Torvalds 23402ca3310eSAlasdair G Kergon down_write(&md->io_lock); 23416d6f10dfSMilan Broz if (noflush) 2342022c2611SMikulas Patocka clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 234394d6351eSMilan Broz up_write(&md->io_lock); 23442e93ccc1SKiyoshi Ueda 23451da177e4SLinus Torvalds /* were we interrupted ? */ 234646125c1cSMilan Broz if (r < 0) { 23479a1fb464SMikulas Patocka dm_queue_flush(md); 234873d410c0SMilan Broz 2349cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 23509f518b27SKiyoshi Ueda start_queue(md->queue); 2351cec47e3dSKiyoshi Ueda 23522ca3310eSAlasdair G Kergon unlock_fs(md); 23532e93ccc1SKiyoshi Ueda goto out; /* pushback list is already flushed, so skip flush */ 23542ca3310eSAlasdair G Kergon } 23552ca3310eSAlasdair G Kergon 23563b00b203SMikulas Patocka /* 23573b00b203SMikulas Patocka * If dm_wait_for_completion returned 0, the device is completely 23583b00b203SMikulas Patocka * quiescent now. There is no request-processing activity. All new 23593b00b203SMikulas Patocka * requests are being added to md->deferred list. 23603b00b203SMikulas Patocka */ 23613b00b203SMikulas Patocka 23622ca3310eSAlasdair G Kergon dm_table_postsuspend_targets(map); 23631da177e4SLinus Torvalds 23641da177e4SLinus Torvalds set_bit(DMF_SUSPENDED, &md->flags); 23651da177e4SLinus Torvalds 23662ca3310eSAlasdair G Kergon out: 23671da177e4SLinus Torvalds dm_table_put(map); 2368d287483dSAlasdair G Kergon 2369d287483dSAlasdair G Kergon out_unlock: 2370e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 2371cf222b37SAlasdair G Kergon return r; 23721da177e4SLinus Torvalds } 23731da177e4SLinus Torvalds 23741da177e4SLinus Torvalds int dm_resume(struct mapped_device *md) 23751da177e4SLinus Torvalds { 2376cf222b37SAlasdair G Kergon int r = -EINVAL; 2377cf222b37SAlasdair G Kergon struct dm_table *map = NULL; 23781da177e4SLinus Torvalds 2379e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 23802ca3310eSAlasdair G Kergon if (!dm_suspended(md)) 2381cf222b37SAlasdair G Kergon goto out; 2382cf222b37SAlasdair G Kergon 2383cf222b37SAlasdair G Kergon map = dm_get_table(md); 23842ca3310eSAlasdair G Kergon if (!map || !dm_table_get_size(map)) 2385cf222b37SAlasdair G Kergon goto out; 23861da177e4SLinus Torvalds 23878757b776SMilan Broz r = dm_table_resume_targets(map); 23888757b776SMilan Broz if (r) 23898757b776SMilan Broz goto out; 23902ca3310eSAlasdair G Kergon 23919a1fb464SMikulas Patocka dm_queue_flush(md); 23922ca3310eSAlasdair G Kergon 2393cec47e3dSKiyoshi Ueda /* 2394cec47e3dSKiyoshi Ueda * Flushing deferred I/Os must be done after targets are resumed 2395cec47e3dSKiyoshi Ueda * so that mapping of targets can work correctly. 2396cec47e3dSKiyoshi Ueda * Request-based dm is queueing the deferred I/Os in its request_queue. 2397cec47e3dSKiyoshi Ueda */ 2398cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 2399cec47e3dSKiyoshi Ueda start_queue(md->queue); 2400cec47e3dSKiyoshi Ueda 24012ca3310eSAlasdair G Kergon unlock_fs(md); 24022ca3310eSAlasdair G Kergon 24032ca3310eSAlasdair G Kergon clear_bit(DMF_SUSPENDED, &md->flags); 24042ca3310eSAlasdair G Kergon 24051da177e4SLinus Torvalds dm_table_unplug_all(map); 2406cf222b37SAlasdair G Kergon r = 0; 2407cf222b37SAlasdair G Kergon out: 2408cf222b37SAlasdair G Kergon dm_table_put(map); 2409e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 24102ca3310eSAlasdair G Kergon 2411cf222b37SAlasdair G Kergon return r; 24121da177e4SLinus Torvalds } 24131da177e4SLinus Torvalds 24141da177e4SLinus Torvalds /*----------------------------------------------------------------- 24151da177e4SLinus Torvalds * Event notification. 24161da177e4SLinus Torvalds *---------------------------------------------------------------*/ 241760935eb2SMilan Broz void dm_kobject_uevent(struct mapped_device *md, enum kobject_action action, 241860935eb2SMilan Broz unsigned cookie) 241969267a30SAlasdair G Kergon { 242060935eb2SMilan Broz char udev_cookie[DM_COOKIE_LENGTH]; 242160935eb2SMilan Broz char *envp[] = { udev_cookie, NULL }; 242260935eb2SMilan Broz 242360935eb2SMilan Broz if (!cookie) 242460935eb2SMilan Broz kobject_uevent(&disk_to_dev(md->disk)->kobj, action); 242560935eb2SMilan Broz else { 242660935eb2SMilan Broz snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u", 242760935eb2SMilan Broz DM_COOKIE_ENV_VAR_NAME, cookie); 242860935eb2SMilan Broz kobject_uevent_env(&disk_to_dev(md->disk)->kobj, action, envp); 242960935eb2SMilan Broz } 243069267a30SAlasdair G Kergon } 243169267a30SAlasdair G Kergon 24327a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md) 24337a8c3d3bSMike Anderson { 24347a8c3d3bSMike Anderson return atomic_add_return(1, &md->uevent_seq); 24357a8c3d3bSMike Anderson } 24367a8c3d3bSMike Anderson 24371da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md) 24381da177e4SLinus Torvalds { 24391da177e4SLinus Torvalds return atomic_read(&md->event_nr); 24401da177e4SLinus Torvalds } 24411da177e4SLinus Torvalds 24421da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr) 24431da177e4SLinus Torvalds { 24441da177e4SLinus Torvalds return wait_event_interruptible(md->eventq, 24451da177e4SLinus Torvalds (event_nr != atomic_read(&md->event_nr))); 24461da177e4SLinus Torvalds } 24471da177e4SLinus Torvalds 24487a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist) 24497a8c3d3bSMike Anderson { 24507a8c3d3bSMike Anderson unsigned long flags; 24517a8c3d3bSMike Anderson 24527a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 24537a8c3d3bSMike Anderson list_add(elist, &md->uevent_list); 24547a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 24557a8c3d3bSMike Anderson } 24567a8c3d3bSMike Anderson 24571da177e4SLinus Torvalds /* 24581da177e4SLinus Torvalds * The gendisk is only valid as long as you have a reference 24591da177e4SLinus Torvalds * count on 'md'. 24601da177e4SLinus Torvalds */ 24611da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md) 24621da177e4SLinus Torvalds { 24631da177e4SLinus Torvalds return md->disk; 24641da177e4SLinus Torvalds } 24651da177e4SLinus Torvalds 2466784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md) 2467784aae73SMilan Broz { 2468784aae73SMilan Broz return &md->kobj; 2469784aae73SMilan Broz } 2470784aae73SMilan Broz 2471784aae73SMilan Broz /* 2472784aae73SMilan Broz * struct mapped_device should not be exported outside of dm.c 2473784aae73SMilan Broz * so use this check to verify that kobj is part of md structure 2474784aae73SMilan Broz */ 2475784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj) 2476784aae73SMilan Broz { 2477784aae73SMilan Broz struct mapped_device *md; 2478784aae73SMilan Broz 2479784aae73SMilan Broz md = container_of(kobj, struct mapped_device, kobj); 2480784aae73SMilan Broz if (&md->kobj != kobj) 2481784aae73SMilan Broz return NULL; 2482784aae73SMilan Broz 24834d89b7b4SMilan Broz if (test_bit(DMF_FREEING, &md->flags) || 24844d89b7b4SMilan Broz test_bit(DMF_DELETING, &md->flags)) 24854d89b7b4SMilan Broz return NULL; 24864d89b7b4SMilan Broz 2487784aae73SMilan Broz dm_get(md); 2488784aae73SMilan Broz return md; 2489784aae73SMilan Broz } 2490784aae73SMilan Broz 24911da177e4SLinus Torvalds int dm_suspended(struct mapped_device *md) 24921da177e4SLinus Torvalds { 24931da177e4SLinus Torvalds return test_bit(DMF_SUSPENDED, &md->flags); 24941da177e4SLinus Torvalds } 24951da177e4SLinus Torvalds 24962e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti) 24972e93ccc1SKiyoshi Ueda { 24982e93ccc1SKiyoshi Ueda struct mapped_device *md = dm_table_get_md(ti->table); 24992e93ccc1SKiyoshi Ueda int r = __noflush_suspending(md); 25002e93ccc1SKiyoshi Ueda 25012e93ccc1SKiyoshi Ueda dm_put(md); 25022e93ccc1SKiyoshi Ueda 25032e93ccc1SKiyoshi Ueda return r; 25042e93ccc1SKiyoshi Ueda } 25052e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending); 25062e93ccc1SKiyoshi Ueda 2507e6ee8c0bSKiyoshi Ueda struct dm_md_mempools *dm_alloc_md_mempools(unsigned type) 2508e6ee8c0bSKiyoshi Ueda { 2509e6ee8c0bSKiyoshi Ueda struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL); 2510e6ee8c0bSKiyoshi Ueda 2511e6ee8c0bSKiyoshi Ueda if (!pools) 2512e6ee8c0bSKiyoshi Ueda return NULL; 2513e6ee8c0bSKiyoshi Ueda 2514e6ee8c0bSKiyoshi Ueda pools->io_pool = (type == DM_TYPE_BIO_BASED) ? 2515e6ee8c0bSKiyoshi Ueda mempool_create_slab_pool(MIN_IOS, _io_cache) : 2516e6ee8c0bSKiyoshi Ueda mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache); 2517e6ee8c0bSKiyoshi Ueda if (!pools->io_pool) 2518e6ee8c0bSKiyoshi Ueda goto free_pools_and_out; 2519e6ee8c0bSKiyoshi Ueda 2520e6ee8c0bSKiyoshi Ueda pools->tio_pool = (type == DM_TYPE_BIO_BASED) ? 2521e6ee8c0bSKiyoshi Ueda mempool_create_slab_pool(MIN_IOS, _tio_cache) : 2522e6ee8c0bSKiyoshi Ueda mempool_create_slab_pool(MIN_IOS, _rq_tio_cache); 2523e6ee8c0bSKiyoshi Ueda if (!pools->tio_pool) 2524e6ee8c0bSKiyoshi Ueda goto free_io_pool_and_out; 2525e6ee8c0bSKiyoshi Ueda 2526e6ee8c0bSKiyoshi Ueda pools->bs = (type == DM_TYPE_BIO_BASED) ? 2527e6ee8c0bSKiyoshi Ueda bioset_create(16, 0) : bioset_create(MIN_IOS, 0); 2528e6ee8c0bSKiyoshi Ueda if (!pools->bs) 2529e6ee8c0bSKiyoshi Ueda goto free_tio_pool_and_out; 2530e6ee8c0bSKiyoshi Ueda 2531e6ee8c0bSKiyoshi Ueda return pools; 2532e6ee8c0bSKiyoshi Ueda 2533e6ee8c0bSKiyoshi Ueda free_tio_pool_and_out: 2534e6ee8c0bSKiyoshi Ueda mempool_destroy(pools->tio_pool); 2535e6ee8c0bSKiyoshi Ueda 2536e6ee8c0bSKiyoshi Ueda free_io_pool_and_out: 2537e6ee8c0bSKiyoshi Ueda mempool_destroy(pools->io_pool); 2538e6ee8c0bSKiyoshi Ueda 2539e6ee8c0bSKiyoshi Ueda free_pools_and_out: 2540e6ee8c0bSKiyoshi Ueda kfree(pools); 2541e6ee8c0bSKiyoshi Ueda 2542e6ee8c0bSKiyoshi Ueda return NULL; 2543e6ee8c0bSKiyoshi Ueda } 2544e6ee8c0bSKiyoshi Ueda 2545e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools) 2546e6ee8c0bSKiyoshi Ueda { 2547e6ee8c0bSKiyoshi Ueda if (!pools) 2548e6ee8c0bSKiyoshi Ueda return; 2549e6ee8c0bSKiyoshi Ueda 2550e6ee8c0bSKiyoshi Ueda if (pools->io_pool) 2551e6ee8c0bSKiyoshi Ueda mempool_destroy(pools->io_pool); 2552e6ee8c0bSKiyoshi Ueda 2553e6ee8c0bSKiyoshi Ueda if (pools->tio_pool) 2554e6ee8c0bSKiyoshi Ueda mempool_destroy(pools->tio_pool); 2555e6ee8c0bSKiyoshi Ueda 2556e6ee8c0bSKiyoshi Ueda if (pools->bs) 2557e6ee8c0bSKiyoshi Ueda bioset_free(pools->bs); 2558e6ee8c0bSKiyoshi Ueda 2559e6ee8c0bSKiyoshi Ueda kfree(pools); 2560e6ee8c0bSKiyoshi Ueda } 2561e6ee8c0bSKiyoshi Ueda 256283d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = { 25631da177e4SLinus Torvalds .open = dm_blk_open, 25641da177e4SLinus Torvalds .release = dm_blk_close, 2565aa129a22SMilan Broz .ioctl = dm_blk_ioctl, 25663ac51e74SDarrick J. Wong .getgeo = dm_blk_getgeo, 25671da177e4SLinus Torvalds .owner = THIS_MODULE 25681da177e4SLinus Torvalds }; 25691da177e4SLinus Torvalds 25701da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo); 25711da177e4SLinus Torvalds 25721da177e4SLinus Torvalds /* 25731da177e4SLinus Torvalds * module hooks 25741da177e4SLinus Torvalds */ 25751da177e4SLinus Torvalds module_init(dm_init); 25761da177e4SLinus Torvalds module_exit(dm_exit); 25771da177e4SLinus Torvalds 25781da177e4SLinus Torvalds module_param(major, uint, 0); 25791da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper"); 25801da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver"); 25811da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 25821da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 2583