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 /* 146d0bcb878SKiyoshi Ueda * Protect barrier_error from concurrent endio processing 147d0bcb878SKiyoshi Ueda * in request-based dm. 148d0bcb878SKiyoshi Ueda */ 149d0bcb878SKiyoshi Ueda spinlock_t barrier_error_lock; 150d0bcb878SKiyoshi Ueda 151d0bcb878SKiyoshi Ueda /* 152304f3f6aSMilan Broz * Processing queue (flush/barriers) 153304f3f6aSMilan Broz */ 154304f3f6aSMilan Broz struct workqueue_struct *wq; 155d0bcb878SKiyoshi Ueda struct work_struct barrier_work; 156d0bcb878SKiyoshi Ueda 157d0bcb878SKiyoshi Ueda /* A pointer to the currently processing pre/post flush request */ 158d0bcb878SKiyoshi Ueda struct request *flush_request; 159304f3f6aSMilan Broz 160304f3f6aSMilan Broz /* 1611da177e4SLinus Torvalds * The current mapping. 1621da177e4SLinus Torvalds */ 1631da177e4SLinus Torvalds struct dm_table *map; 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds /* 1661da177e4SLinus Torvalds * io objects are allocated from here. 1671da177e4SLinus Torvalds */ 1681da177e4SLinus Torvalds mempool_t *io_pool; 1691da177e4SLinus Torvalds mempool_t *tio_pool; 1701da177e4SLinus Torvalds 1719faf400fSStefan Bader struct bio_set *bs; 1729faf400fSStefan Bader 1731da177e4SLinus Torvalds /* 1741da177e4SLinus Torvalds * Event handling. 1751da177e4SLinus Torvalds */ 1761da177e4SLinus Torvalds atomic_t event_nr; 1771da177e4SLinus Torvalds wait_queue_head_t eventq; 1787a8c3d3bSMike Anderson atomic_t uevent_seq; 1797a8c3d3bSMike Anderson struct list_head uevent_list; 1807a8c3d3bSMike Anderson spinlock_t uevent_lock; /* Protect access to uevent_list */ 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds /* 1831da177e4SLinus Torvalds * freeze/thaw support require holding onto a super block 1841da177e4SLinus Torvalds */ 1851da177e4SLinus Torvalds struct super_block *frozen_sb; 186db8fef4fSMikulas Patocka struct block_device *bdev; 1873ac51e74SDarrick J. Wong 1883ac51e74SDarrick J. Wong /* forced geometry settings */ 1893ac51e74SDarrick J. Wong struct hd_geometry geometry; 190784aae73SMilan Broz 191cec47e3dSKiyoshi Ueda /* For saving the address of __make_request for request based dm */ 192cec47e3dSKiyoshi Ueda make_request_fn *saved_make_request_fn; 193cec47e3dSKiyoshi Ueda 194784aae73SMilan Broz /* sysfs handle */ 195784aae73SMilan Broz struct kobject kobj; 19652b1fd5aSMikulas Patocka 19752b1fd5aSMikulas Patocka /* zero-length barrier that will be cloned and submitted to targets */ 19852b1fd5aSMikulas Patocka struct bio barrier_bio; 1991da177e4SLinus Torvalds }; 2001da177e4SLinus Torvalds 201e6ee8c0bSKiyoshi Ueda /* 202e6ee8c0bSKiyoshi Ueda * For mempools pre-allocation at the table loading time. 203e6ee8c0bSKiyoshi Ueda */ 204e6ee8c0bSKiyoshi Ueda struct dm_md_mempools { 205e6ee8c0bSKiyoshi Ueda mempool_t *io_pool; 206e6ee8c0bSKiyoshi Ueda mempool_t *tio_pool; 207e6ee8c0bSKiyoshi Ueda struct bio_set *bs; 208e6ee8c0bSKiyoshi Ueda }; 209e6ee8c0bSKiyoshi Ueda 2101da177e4SLinus Torvalds #define MIN_IOS 256 211e18b890bSChristoph Lameter static struct kmem_cache *_io_cache; 212e18b890bSChristoph Lameter static struct kmem_cache *_tio_cache; 2138fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache; 2148fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_bio_info_cache; 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds static int __init local_init(void) 2171da177e4SLinus Torvalds { 21851157b4aSKiyoshi Ueda int r = -ENOMEM; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds /* allocate a slab for the dm_ios */ 221028867acSAlasdair G Kergon _io_cache = KMEM_CACHE(dm_io, 0); 2221da177e4SLinus Torvalds if (!_io_cache) 22351157b4aSKiyoshi Ueda return r; 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds /* allocate a slab for the target ios */ 226028867acSAlasdair G Kergon _tio_cache = KMEM_CACHE(dm_target_io, 0); 22751157b4aSKiyoshi Ueda if (!_tio_cache) 22851157b4aSKiyoshi Ueda goto out_free_io_cache; 2291da177e4SLinus Torvalds 2308fbf26adSKiyoshi Ueda _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0); 2318fbf26adSKiyoshi Ueda if (!_rq_tio_cache) 2328fbf26adSKiyoshi Ueda goto out_free_tio_cache; 2338fbf26adSKiyoshi Ueda 2348fbf26adSKiyoshi Ueda _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0); 2358fbf26adSKiyoshi Ueda if (!_rq_bio_info_cache) 2368fbf26adSKiyoshi Ueda goto out_free_rq_tio_cache; 2378fbf26adSKiyoshi Ueda 23851e5b2bdSMike Anderson r = dm_uevent_init(); 23951157b4aSKiyoshi Ueda if (r) 2408fbf26adSKiyoshi Ueda goto out_free_rq_bio_info_cache; 24151e5b2bdSMike Anderson 2421da177e4SLinus Torvalds _major = major; 2431da177e4SLinus Torvalds r = register_blkdev(_major, _name); 24451157b4aSKiyoshi Ueda if (r < 0) 24551157b4aSKiyoshi Ueda goto out_uevent_exit; 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds if (!_major) 2481da177e4SLinus Torvalds _major = r; 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds return 0; 25151157b4aSKiyoshi Ueda 25251157b4aSKiyoshi Ueda out_uevent_exit: 25351157b4aSKiyoshi Ueda dm_uevent_exit(); 2548fbf26adSKiyoshi Ueda out_free_rq_bio_info_cache: 2558fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_bio_info_cache); 2568fbf26adSKiyoshi Ueda out_free_rq_tio_cache: 2578fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_tio_cache); 25851157b4aSKiyoshi Ueda out_free_tio_cache: 25951157b4aSKiyoshi Ueda kmem_cache_destroy(_tio_cache); 26051157b4aSKiyoshi Ueda out_free_io_cache: 26151157b4aSKiyoshi Ueda kmem_cache_destroy(_io_cache); 26251157b4aSKiyoshi Ueda 26351157b4aSKiyoshi Ueda return r; 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds static void local_exit(void) 2671da177e4SLinus Torvalds { 2688fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_bio_info_cache); 2698fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_tio_cache); 2701da177e4SLinus Torvalds kmem_cache_destroy(_tio_cache); 2711da177e4SLinus Torvalds kmem_cache_destroy(_io_cache); 27200d59405SAkinobu Mita unregister_blkdev(_major, _name); 27351e5b2bdSMike Anderson dm_uevent_exit(); 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds _major = 0; 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds DMINFO("cleaned up"); 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds 280b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = { 2811da177e4SLinus Torvalds local_init, 2821da177e4SLinus Torvalds dm_target_init, 2831da177e4SLinus Torvalds dm_linear_init, 2841da177e4SLinus Torvalds dm_stripe_init, 285952b3557SMikulas Patocka dm_io_init, 286945fa4d2SMikulas Patocka dm_kcopyd_init, 2871da177e4SLinus Torvalds dm_interface_init, 2881da177e4SLinus Torvalds }; 2891da177e4SLinus Torvalds 290b9249e55SAlasdair G Kergon static void (*_exits[])(void) = { 2911da177e4SLinus Torvalds local_exit, 2921da177e4SLinus Torvalds dm_target_exit, 2931da177e4SLinus Torvalds dm_linear_exit, 2941da177e4SLinus Torvalds dm_stripe_exit, 295952b3557SMikulas Patocka dm_io_exit, 296945fa4d2SMikulas Patocka dm_kcopyd_exit, 2971da177e4SLinus Torvalds dm_interface_exit, 2981da177e4SLinus Torvalds }; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds static int __init dm_init(void) 3011da177e4SLinus Torvalds { 3021da177e4SLinus Torvalds const int count = ARRAY_SIZE(_inits); 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds int r, i; 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds for (i = 0; i < count; i++) { 3071da177e4SLinus Torvalds r = _inits[i](); 3081da177e4SLinus Torvalds if (r) 3091da177e4SLinus Torvalds goto bad; 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds return 0; 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds bad: 3151da177e4SLinus Torvalds while (i--) 3161da177e4SLinus Torvalds _exits[i](); 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds return r; 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds static void __exit dm_exit(void) 3221da177e4SLinus Torvalds { 3231da177e4SLinus Torvalds int i = ARRAY_SIZE(_exits); 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds while (i--) 3261da177e4SLinus Torvalds _exits[i](); 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds /* 3301da177e4SLinus Torvalds * Block device functions 3311da177e4SLinus Torvalds */ 332*432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md) 333*432a212cSMike Anderson { 334*432a212cSMike Anderson return test_bit(DMF_DELETING, &md->flags); 335*432a212cSMike Anderson } 336*432a212cSMike Anderson 337fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode) 3381da177e4SLinus Torvalds { 3391da177e4SLinus Torvalds struct mapped_device *md; 3401da177e4SLinus Torvalds 341fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 342fba9f90eSJeff Mahoney 343fe5f9f2cSAl Viro md = bdev->bd_disk->private_data; 344fba9f90eSJeff Mahoney if (!md) 345fba9f90eSJeff Mahoney goto out; 346fba9f90eSJeff Mahoney 3475c6bd75dSAlasdair G Kergon if (test_bit(DMF_FREEING, &md->flags) || 348*432a212cSMike Anderson dm_deleting_md(md)) { 349fba9f90eSJeff Mahoney md = NULL; 350fba9f90eSJeff Mahoney goto out; 351fba9f90eSJeff Mahoney } 352fba9f90eSJeff Mahoney 3531da177e4SLinus Torvalds dm_get(md); 3545c6bd75dSAlasdair G Kergon atomic_inc(&md->open_count); 355fba9f90eSJeff Mahoney 356fba9f90eSJeff Mahoney out: 357fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 358fba9f90eSJeff Mahoney 359fba9f90eSJeff Mahoney return md ? 0 : -ENXIO; 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds 362fe5f9f2cSAl Viro static int dm_blk_close(struct gendisk *disk, fmode_t mode) 3631da177e4SLinus Torvalds { 364fe5f9f2cSAl Viro struct mapped_device *md = disk->private_data; 3655c6bd75dSAlasdair G Kergon atomic_dec(&md->open_count); 3661da177e4SLinus Torvalds dm_put(md); 3671da177e4SLinus Torvalds return 0; 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds 3705c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md) 3715c6bd75dSAlasdair G Kergon { 3725c6bd75dSAlasdair G Kergon return atomic_read(&md->open_count); 3735c6bd75dSAlasdair G Kergon } 3745c6bd75dSAlasdair G Kergon 3755c6bd75dSAlasdair G Kergon /* 3765c6bd75dSAlasdair G Kergon * Guarantees nothing is using the device before it's deleted. 3775c6bd75dSAlasdair G Kergon */ 3785c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md) 3795c6bd75dSAlasdair G Kergon { 3805c6bd75dSAlasdair G Kergon int r = 0; 3815c6bd75dSAlasdair G Kergon 3825c6bd75dSAlasdair G Kergon spin_lock(&_minor_lock); 3835c6bd75dSAlasdair G Kergon 3845c6bd75dSAlasdair G Kergon if (dm_open_count(md)) 3855c6bd75dSAlasdair G Kergon r = -EBUSY; 3865c6bd75dSAlasdair G Kergon else 3875c6bd75dSAlasdair G Kergon set_bit(DMF_DELETING, &md->flags); 3885c6bd75dSAlasdair G Kergon 3895c6bd75dSAlasdair G Kergon spin_unlock(&_minor_lock); 3905c6bd75dSAlasdair G Kergon 3915c6bd75dSAlasdair G Kergon return r; 3925c6bd75dSAlasdair G Kergon } 3935c6bd75dSAlasdair G Kergon 3943ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 3953ac51e74SDarrick J. Wong { 3963ac51e74SDarrick J. Wong struct mapped_device *md = bdev->bd_disk->private_data; 3973ac51e74SDarrick J. Wong 3983ac51e74SDarrick J. Wong return dm_get_geometry(md, geo); 3993ac51e74SDarrick J. Wong } 4003ac51e74SDarrick J. Wong 401fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode, 402aa129a22SMilan Broz unsigned int cmd, unsigned long arg) 403aa129a22SMilan Broz { 404fe5f9f2cSAl Viro struct mapped_device *md = bdev->bd_disk->private_data; 4057c666411SAlasdair G Kergon struct dm_table *map = dm_get_live_table(md); 406aa129a22SMilan Broz struct dm_target *tgt; 407aa129a22SMilan Broz int r = -ENOTTY; 408aa129a22SMilan Broz 409aa129a22SMilan Broz if (!map || !dm_table_get_size(map)) 410aa129a22SMilan Broz goto out; 411aa129a22SMilan Broz 412aa129a22SMilan Broz /* We only support devices that have a single target */ 413aa129a22SMilan Broz if (dm_table_get_num_targets(map) != 1) 414aa129a22SMilan Broz goto out; 415aa129a22SMilan Broz 416aa129a22SMilan Broz tgt = dm_table_get_target(map, 0); 417aa129a22SMilan Broz 418aa129a22SMilan Broz if (dm_suspended(md)) { 419aa129a22SMilan Broz r = -EAGAIN; 420aa129a22SMilan Broz goto out; 421aa129a22SMilan Broz } 422aa129a22SMilan Broz 423aa129a22SMilan Broz if (tgt->type->ioctl) 424647b3d00SAl Viro r = tgt->type->ioctl(tgt, cmd, arg); 425aa129a22SMilan Broz 426aa129a22SMilan Broz out: 427aa129a22SMilan Broz dm_table_put(map); 428aa129a22SMilan Broz 429aa129a22SMilan Broz return r; 430aa129a22SMilan Broz } 431aa129a22SMilan Broz 432028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md) 4331da177e4SLinus Torvalds { 4341da177e4SLinus Torvalds return mempool_alloc(md->io_pool, GFP_NOIO); 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 437028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io) 4381da177e4SLinus Torvalds { 4391da177e4SLinus Torvalds mempool_free(io, md->io_pool); 4401da177e4SLinus Torvalds } 4411da177e4SLinus Torvalds 442028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio) 4431da177e4SLinus Torvalds { 4441da177e4SLinus Torvalds mempool_free(tio, md->tio_pool); 4451da177e4SLinus Torvalds } 4461da177e4SLinus Torvalds 44708885643SKiyoshi Ueda static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md, 44808885643SKiyoshi Ueda gfp_t gfp_mask) 449cec47e3dSKiyoshi Ueda { 45008885643SKiyoshi Ueda return mempool_alloc(md->tio_pool, gfp_mask); 451cec47e3dSKiyoshi Ueda } 452cec47e3dSKiyoshi Ueda 453cec47e3dSKiyoshi Ueda static void free_rq_tio(struct dm_rq_target_io *tio) 454cec47e3dSKiyoshi Ueda { 455cec47e3dSKiyoshi Ueda mempool_free(tio, tio->md->tio_pool); 456cec47e3dSKiyoshi Ueda } 457cec47e3dSKiyoshi Ueda 458cec47e3dSKiyoshi Ueda static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md) 459cec47e3dSKiyoshi Ueda { 460cec47e3dSKiyoshi Ueda return mempool_alloc(md->io_pool, GFP_ATOMIC); 461cec47e3dSKiyoshi Ueda } 462cec47e3dSKiyoshi Ueda 463cec47e3dSKiyoshi Ueda static void free_bio_info(struct dm_rq_clone_bio_info *info) 464cec47e3dSKiyoshi Ueda { 465cec47e3dSKiyoshi Ueda mempool_free(info, info->tio->md->io_pool); 466cec47e3dSKiyoshi Ueda } 467cec47e3dSKiyoshi Ueda 46890abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md) 46990abb8c4SKiyoshi Ueda { 47090abb8c4SKiyoshi Ueda return atomic_read(&md->pending[READ]) + 47190abb8c4SKiyoshi Ueda atomic_read(&md->pending[WRITE]); 47290abb8c4SKiyoshi Ueda } 47390abb8c4SKiyoshi Ueda 4743eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io) 4753eaf840eSJun'ichi "Nick" Nomura { 4763eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 477c9959059STejun Heo int cpu; 478316d315bSNikanth Karthikesan int rw = bio_data_dir(io->bio); 4793eaf840eSJun'ichi "Nick" Nomura 4803eaf840eSJun'ichi "Nick" Nomura io->start_time = jiffies; 4813eaf840eSJun'ichi "Nick" Nomura 482074a7acaSTejun Heo cpu = part_stat_lock(); 483074a7acaSTejun Heo part_round_stats(cpu, &dm_disk(md)->part0); 484074a7acaSTejun Heo part_stat_unlock(); 485316d315bSNikanth Karthikesan dm_disk(md)->part0.in_flight[rw] = atomic_inc_return(&md->pending[rw]); 4863eaf840eSJun'ichi "Nick" Nomura } 4873eaf840eSJun'ichi "Nick" Nomura 488d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io) 4893eaf840eSJun'ichi "Nick" Nomura { 4903eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 4913eaf840eSJun'ichi "Nick" Nomura struct bio *bio = io->bio; 4923eaf840eSJun'ichi "Nick" Nomura unsigned long duration = jiffies - io->start_time; 493c9959059STejun Heo int pending, cpu; 4943eaf840eSJun'ichi "Nick" Nomura int rw = bio_data_dir(bio); 4953eaf840eSJun'ichi "Nick" Nomura 496074a7acaSTejun Heo cpu = part_stat_lock(); 497074a7acaSTejun Heo part_round_stats(cpu, &dm_disk(md)->part0); 498074a7acaSTejun Heo part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration); 499074a7acaSTejun Heo part_stat_unlock(); 5003eaf840eSJun'ichi "Nick" Nomura 501af7e466aSMikulas Patocka /* 502af7e466aSMikulas Patocka * After this is decremented the bio must not be touched if it is 503af7e466aSMikulas Patocka * a barrier. 504af7e466aSMikulas Patocka */ 505316d315bSNikanth Karthikesan dm_disk(md)->part0.in_flight[rw] = pending = 506316d315bSNikanth Karthikesan atomic_dec_return(&md->pending[rw]); 507316d315bSNikanth Karthikesan pending += atomic_read(&md->pending[rw^0x1]); 5083eaf840eSJun'ichi "Nick" Nomura 509d221d2e7SMikulas Patocka /* nudge anyone waiting on suspend queue */ 510d221d2e7SMikulas Patocka if (!pending) 511d221d2e7SMikulas Patocka wake_up(&md->wait); 5123eaf840eSJun'ichi "Nick" Nomura } 5133eaf840eSJun'ichi "Nick" Nomura 5141da177e4SLinus Torvalds /* 5151da177e4SLinus Torvalds * Add the bio to the list of deferred io. 5161da177e4SLinus Torvalds */ 51792c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio) 5181da177e4SLinus Torvalds { 5192ca3310eSAlasdair G Kergon down_write(&md->io_lock); 5201da177e4SLinus Torvalds 521022c2611SMikulas Patocka spin_lock_irq(&md->deferred_lock); 5221da177e4SLinus Torvalds bio_list_add(&md->deferred, bio); 523022c2611SMikulas Patocka spin_unlock_irq(&md->deferred_lock); 5241da177e4SLinus Torvalds 52592c63902SMikulas Patocka if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) 52692c63902SMikulas Patocka queue_work(md->wq, &md->work); 52792c63902SMikulas Patocka 5282ca3310eSAlasdair G Kergon up_write(&md->io_lock); 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds 5311da177e4SLinus Torvalds /* 5321da177e4SLinus Torvalds * Everyone (including functions in this file), should use this 5331da177e4SLinus Torvalds * function to access the md->map field, and make sure they call 5341da177e4SLinus Torvalds * dm_table_put() when finished. 5351da177e4SLinus Torvalds */ 5367c666411SAlasdair G Kergon struct dm_table *dm_get_live_table(struct mapped_device *md) 5371da177e4SLinus Torvalds { 5381da177e4SLinus Torvalds struct dm_table *t; 539523d9297SKiyoshi Ueda unsigned long flags; 5401da177e4SLinus Torvalds 541523d9297SKiyoshi Ueda read_lock_irqsave(&md->map_lock, flags); 5421da177e4SLinus Torvalds t = md->map; 5431da177e4SLinus Torvalds if (t) 5441da177e4SLinus Torvalds dm_table_get(t); 545523d9297SKiyoshi Ueda read_unlock_irqrestore(&md->map_lock, flags); 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds return t; 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds 5503ac51e74SDarrick J. Wong /* 5513ac51e74SDarrick J. Wong * Get the geometry associated with a dm device 5523ac51e74SDarrick J. Wong */ 5533ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) 5543ac51e74SDarrick J. Wong { 5553ac51e74SDarrick J. Wong *geo = md->geometry; 5563ac51e74SDarrick J. Wong 5573ac51e74SDarrick J. Wong return 0; 5583ac51e74SDarrick J. Wong } 5593ac51e74SDarrick J. Wong 5603ac51e74SDarrick J. Wong /* 5613ac51e74SDarrick J. Wong * Set the geometry of a device. 5623ac51e74SDarrick J. Wong */ 5633ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) 5643ac51e74SDarrick J. Wong { 5653ac51e74SDarrick J. Wong sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; 5663ac51e74SDarrick J. Wong 5673ac51e74SDarrick J. Wong if (geo->start > sz) { 5683ac51e74SDarrick J. Wong DMWARN("Start sector is beyond the geometry limits."); 5693ac51e74SDarrick J. Wong return -EINVAL; 5703ac51e74SDarrick J. Wong } 5713ac51e74SDarrick J. Wong 5723ac51e74SDarrick J. Wong md->geometry = *geo; 5733ac51e74SDarrick J. Wong 5743ac51e74SDarrick J. Wong return 0; 5753ac51e74SDarrick J. Wong } 5763ac51e74SDarrick J. Wong 5771da177e4SLinus Torvalds /*----------------------------------------------------------------- 5781da177e4SLinus Torvalds * CRUD START: 5791da177e4SLinus Torvalds * A more elegant soln is in the works that uses the queue 5801da177e4SLinus Torvalds * merge fn, unfortunately there are a couple of changes to 5811da177e4SLinus Torvalds * the block layer that I want to make for this. So in the 5821da177e4SLinus Torvalds * interests of getting something for people to use I give 5831da177e4SLinus Torvalds * you this clearly demarcated crap. 5841da177e4SLinus Torvalds *---------------------------------------------------------------*/ 5851da177e4SLinus Torvalds 5862e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md) 5872e93ccc1SKiyoshi Ueda { 5882e93ccc1SKiyoshi Ueda return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 5892e93ccc1SKiyoshi Ueda } 5902e93ccc1SKiyoshi Ueda 5911da177e4SLinus Torvalds /* 5921da177e4SLinus Torvalds * Decrements the number of outstanding ios that a bio has been 5931da177e4SLinus Torvalds * cloned into, completing the original io if necc. 5941da177e4SLinus Torvalds */ 595858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error) 5961da177e4SLinus Torvalds { 5972e93ccc1SKiyoshi Ueda unsigned long flags; 598b35f8caaSMilan Broz int io_error; 599b35f8caaSMilan Broz struct bio *bio; 600b35f8caaSMilan Broz struct mapped_device *md = io->md; 6012e93ccc1SKiyoshi Ueda 6022e93ccc1SKiyoshi Ueda /* Push-back supersedes any I/O errors */ 603f88fb981SKiyoshi Ueda if (unlikely(error)) { 604f88fb981SKiyoshi Ueda spin_lock_irqsave(&io->endio_lock, flags); 605f88fb981SKiyoshi Ueda if (!(io->error > 0 && __noflush_suspending(md))) 6061da177e4SLinus Torvalds io->error = error; 607f88fb981SKiyoshi Ueda spin_unlock_irqrestore(&io->endio_lock, flags); 608f88fb981SKiyoshi Ueda } 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds if (atomic_dec_and_test(&io->io_count)) { 6112e93ccc1SKiyoshi Ueda if (io->error == DM_ENDIO_REQUEUE) { 6122e93ccc1SKiyoshi Ueda /* 6132e93ccc1SKiyoshi Ueda * Target requested pushing back the I/O. 6142e93ccc1SKiyoshi Ueda */ 615022c2611SMikulas Patocka spin_lock_irqsave(&md->deferred_lock, flags); 6162761e95fSMikulas Patocka if (__noflush_suspending(md)) { 6171f98a13fSJens Axboe if (!bio_rw_flagged(io->bio, BIO_RW_BARRIER)) 6182761e95fSMikulas Patocka bio_list_add_head(&md->deferred, 6192761e95fSMikulas Patocka io->bio); 6202761e95fSMikulas Patocka } else 6212e93ccc1SKiyoshi Ueda /* noflush suspend was interrupted. */ 6222e93ccc1SKiyoshi Ueda io->error = -EIO; 623022c2611SMikulas Patocka spin_unlock_irqrestore(&md->deferred_lock, flags); 6242e93ccc1SKiyoshi Ueda } 6252e93ccc1SKiyoshi Ueda 626b35f8caaSMilan Broz io_error = io->error; 627b35f8caaSMilan Broz bio = io->bio; 6282056a782SJens Axboe 6291f98a13fSJens Axboe if (bio_rw_flagged(bio, BIO_RW_BARRIER)) { 630af7e466aSMikulas Patocka /* 631af7e466aSMikulas Patocka * There can be just one barrier request so we use 632af7e466aSMikulas Patocka * a per-device variable for error reporting. 633af7e466aSMikulas Patocka * Note that you can't touch the bio after end_io_acct 634af7e466aSMikulas Patocka */ 635fdb9572bSMikulas Patocka if (!md->barrier_error && io_error != -EOPNOTSUPP) 636af7e466aSMikulas Patocka md->barrier_error = io_error; 637af7e466aSMikulas Patocka end_io_acct(io); 638af7e466aSMikulas Patocka } else { 639af7e466aSMikulas Patocka end_io_acct(io); 640b35f8caaSMilan Broz 641b35f8caaSMilan Broz if (io_error != DM_ENDIO_REQUEUE) { 642b35f8caaSMilan Broz trace_block_bio_complete(md->queue, bio); 643b35f8caaSMilan Broz 644b35f8caaSMilan Broz bio_endio(bio, io_error); 6452e93ccc1SKiyoshi Ueda } 6461da177e4SLinus Torvalds } 647af7e466aSMikulas Patocka 648af7e466aSMikulas Patocka free_io(md, io); 649af7e466aSMikulas Patocka } 6501da177e4SLinus Torvalds } 6511da177e4SLinus Torvalds 6526712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error) 6531da177e4SLinus Torvalds { 6541da177e4SLinus Torvalds int r = 0; 655028867acSAlasdair G Kergon struct dm_target_io *tio = bio->bi_private; 656b35f8caaSMilan Broz struct dm_io *io = tio->io; 6579faf400fSStefan Bader struct mapped_device *md = tio->io->md; 6581da177e4SLinus Torvalds dm_endio_fn endio = tio->ti->type->end_io; 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds if (!bio_flagged(bio, BIO_UPTODATE) && !error) 6611da177e4SLinus Torvalds error = -EIO; 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds if (endio) { 6641da177e4SLinus Torvalds r = endio(tio->ti, bio, error, &tio->info); 6652e93ccc1SKiyoshi Ueda if (r < 0 || r == DM_ENDIO_REQUEUE) 6662e93ccc1SKiyoshi Ueda /* 6672e93ccc1SKiyoshi Ueda * error and requeue request are handled 6682e93ccc1SKiyoshi Ueda * in dec_pending(). 6692e93ccc1SKiyoshi Ueda */ 6701da177e4SLinus Torvalds error = r; 67145cbcd79SKiyoshi Ueda else if (r == DM_ENDIO_INCOMPLETE) 67245cbcd79SKiyoshi Ueda /* The target will handle the io */ 6736712ecf8SNeilBrown return; 67445cbcd79SKiyoshi Ueda else if (r) { 67545cbcd79SKiyoshi Ueda DMWARN("unimplemented target endio return value: %d", r); 67645cbcd79SKiyoshi Ueda BUG(); 67745cbcd79SKiyoshi Ueda } 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds 6809faf400fSStefan Bader /* 6819faf400fSStefan Bader * Store md for cleanup instead of tio which is about to get freed. 6829faf400fSStefan Bader */ 6839faf400fSStefan Bader bio->bi_private = md->bs; 6849faf400fSStefan Bader 6859faf400fSStefan Bader free_tio(md, tio); 686b35f8caaSMilan Broz bio_put(bio); 687b35f8caaSMilan Broz dec_pending(io, error); 6881da177e4SLinus Torvalds } 6891da177e4SLinus Torvalds 690cec47e3dSKiyoshi Ueda /* 691cec47e3dSKiyoshi Ueda * Partial completion handling for request-based dm 692cec47e3dSKiyoshi Ueda */ 693cec47e3dSKiyoshi Ueda static void end_clone_bio(struct bio *clone, int error) 694cec47e3dSKiyoshi Ueda { 695cec47e3dSKiyoshi Ueda struct dm_rq_clone_bio_info *info = clone->bi_private; 696cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = info->tio; 697cec47e3dSKiyoshi Ueda struct bio *bio = info->orig; 698cec47e3dSKiyoshi Ueda unsigned int nr_bytes = info->orig->bi_size; 699cec47e3dSKiyoshi Ueda 700cec47e3dSKiyoshi Ueda bio_put(clone); 701cec47e3dSKiyoshi Ueda 702cec47e3dSKiyoshi Ueda if (tio->error) 703cec47e3dSKiyoshi Ueda /* 704cec47e3dSKiyoshi Ueda * An error has already been detected on the request. 705cec47e3dSKiyoshi Ueda * Once error occurred, just let clone->end_io() handle 706cec47e3dSKiyoshi Ueda * the remainder. 707cec47e3dSKiyoshi Ueda */ 708cec47e3dSKiyoshi Ueda return; 709cec47e3dSKiyoshi Ueda else if (error) { 710cec47e3dSKiyoshi Ueda /* 711cec47e3dSKiyoshi Ueda * Don't notice the error to the upper layer yet. 712cec47e3dSKiyoshi Ueda * The error handling decision is made by the target driver, 713cec47e3dSKiyoshi Ueda * when the request is completed. 714cec47e3dSKiyoshi Ueda */ 715cec47e3dSKiyoshi Ueda tio->error = error; 716cec47e3dSKiyoshi Ueda return; 717cec47e3dSKiyoshi Ueda } 718cec47e3dSKiyoshi Ueda 719cec47e3dSKiyoshi Ueda /* 720cec47e3dSKiyoshi Ueda * I/O for the bio successfully completed. 721cec47e3dSKiyoshi Ueda * Notice the data completion to the upper layer. 722cec47e3dSKiyoshi Ueda */ 723cec47e3dSKiyoshi Ueda 724cec47e3dSKiyoshi Ueda /* 725cec47e3dSKiyoshi Ueda * bios are processed from the head of the list. 726cec47e3dSKiyoshi Ueda * So the completing bio should always be rq->bio. 727cec47e3dSKiyoshi Ueda * If it's not, something wrong is happening. 728cec47e3dSKiyoshi Ueda */ 729cec47e3dSKiyoshi Ueda if (tio->orig->bio != bio) 730cec47e3dSKiyoshi Ueda DMERR("bio completion is going in the middle of the request"); 731cec47e3dSKiyoshi Ueda 732cec47e3dSKiyoshi Ueda /* 733cec47e3dSKiyoshi Ueda * Update the original request. 734cec47e3dSKiyoshi Ueda * Do not use blk_end_request() here, because it may complete 735cec47e3dSKiyoshi Ueda * the original request before the clone, and break the ordering. 736cec47e3dSKiyoshi Ueda */ 737cec47e3dSKiyoshi Ueda blk_update_request(tio->orig, 0, nr_bytes); 738cec47e3dSKiyoshi Ueda } 739cec47e3dSKiyoshi Ueda 740d0bcb878SKiyoshi Ueda static void store_barrier_error(struct mapped_device *md, int error) 741d0bcb878SKiyoshi Ueda { 742d0bcb878SKiyoshi Ueda unsigned long flags; 743d0bcb878SKiyoshi Ueda 744d0bcb878SKiyoshi Ueda spin_lock_irqsave(&md->barrier_error_lock, flags); 745d0bcb878SKiyoshi Ueda /* 746d0bcb878SKiyoshi Ueda * Basically, the first error is taken, but: 747d0bcb878SKiyoshi Ueda * -EOPNOTSUPP supersedes any I/O error. 748d0bcb878SKiyoshi Ueda * Requeue request supersedes any I/O error but -EOPNOTSUPP. 749d0bcb878SKiyoshi Ueda */ 750d0bcb878SKiyoshi Ueda if (!md->barrier_error || error == -EOPNOTSUPP || 751d0bcb878SKiyoshi Ueda (md->barrier_error != -EOPNOTSUPP && 752d0bcb878SKiyoshi Ueda error == DM_ENDIO_REQUEUE)) 753d0bcb878SKiyoshi Ueda md->barrier_error = error; 754d0bcb878SKiyoshi Ueda spin_unlock_irqrestore(&md->barrier_error_lock, flags); 755d0bcb878SKiyoshi Ueda } 756d0bcb878SKiyoshi Ueda 757cec47e3dSKiyoshi Ueda /* 758cec47e3dSKiyoshi Ueda * Don't touch any member of the md after calling this function because 759cec47e3dSKiyoshi Ueda * the md may be freed in dm_put() at the end of this function. 760cec47e3dSKiyoshi Ueda * Or do dm_get() before calling this function and dm_put() later. 761cec47e3dSKiyoshi Ueda */ 762b4324feeSKiyoshi Ueda static void rq_completed(struct mapped_device *md, int rw, int run_queue) 763cec47e3dSKiyoshi Ueda { 764b4324feeSKiyoshi Ueda atomic_dec(&md->pending[rw]); 765cec47e3dSKiyoshi Ueda 766cec47e3dSKiyoshi Ueda /* nudge anyone waiting on suspend queue */ 767b4324feeSKiyoshi Ueda if (!md_in_flight(md)) 768cec47e3dSKiyoshi Ueda wake_up(&md->wait); 769cec47e3dSKiyoshi Ueda 770cec47e3dSKiyoshi Ueda if (run_queue) 771b4324feeSKiyoshi Ueda blk_run_queue(md->queue); 772cec47e3dSKiyoshi Ueda 773cec47e3dSKiyoshi Ueda /* 774cec47e3dSKiyoshi Ueda * dm_put() must be at the end of this function. See the comment above 775cec47e3dSKiyoshi Ueda */ 776cec47e3dSKiyoshi Ueda dm_put(md); 777cec47e3dSKiyoshi Ueda } 778cec47e3dSKiyoshi Ueda 779a77e28c7SKiyoshi Ueda static void free_rq_clone(struct request *clone) 780a77e28c7SKiyoshi Ueda { 781a77e28c7SKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 782a77e28c7SKiyoshi Ueda 783a77e28c7SKiyoshi Ueda blk_rq_unprep_clone(clone); 784a77e28c7SKiyoshi Ueda free_rq_tio(tio); 785a77e28c7SKiyoshi Ueda } 786a77e28c7SKiyoshi Ueda 787980691e5SKiyoshi Ueda /* 788980691e5SKiyoshi Ueda * Complete the clone and the original request. 789980691e5SKiyoshi Ueda * Must be called without queue lock. 790980691e5SKiyoshi Ueda */ 791980691e5SKiyoshi Ueda static void dm_end_request(struct request *clone, int error) 792980691e5SKiyoshi Ueda { 793980691e5SKiyoshi Ueda int rw = rq_data_dir(clone); 794d0bcb878SKiyoshi Ueda int run_queue = 1; 795d0bcb878SKiyoshi Ueda bool is_barrier = blk_barrier_rq(clone); 796980691e5SKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 797980691e5SKiyoshi Ueda struct mapped_device *md = tio->md; 798980691e5SKiyoshi Ueda struct request *rq = tio->orig; 799980691e5SKiyoshi Ueda 800d0bcb878SKiyoshi Ueda if (blk_pc_request(rq) && !is_barrier) { 801980691e5SKiyoshi Ueda rq->errors = clone->errors; 802980691e5SKiyoshi Ueda rq->resid_len = clone->resid_len; 803980691e5SKiyoshi Ueda 804980691e5SKiyoshi Ueda if (rq->sense) 805980691e5SKiyoshi Ueda /* 806980691e5SKiyoshi Ueda * We are using the sense buffer of the original 807980691e5SKiyoshi Ueda * request. 808980691e5SKiyoshi Ueda * So setting the length of the sense data is enough. 809980691e5SKiyoshi Ueda */ 810980691e5SKiyoshi Ueda rq->sense_len = clone->sense_len; 811980691e5SKiyoshi Ueda } 812980691e5SKiyoshi Ueda 813980691e5SKiyoshi Ueda free_rq_clone(clone); 814980691e5SKiyoshi Ueda 815d0bcb878SKiyoshi Ueda if (unlikely(is_barrier)) { 816d0bcb878SKiyoshi Ueda if (unlikely(error)) 817d0bcb878SKiyoshi Ueda store_barrier_error(md, error); 818d0bcb878SKiyoshi Ueda run_queue = 0; 819d0bcb878SKiyoshi Ueda } else 820980691e5SKiyoshi Ueda blk_end_request_all(rq, error); 821980691e5SKiyoshi Ueda 822d0bcb878SKiyoshi Ueda rq_completed(md, rw, run_queue); 823980691e5SKiyoshi Ueda } 824980691e5SKiyoshi Ueda 825cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq) 826cec47e3dSKiyoshi Ueda { 827cec47e3dSKiyoshi Ueda struct request *clone = rq->special; 828cec47e3dSKiyoshi Ueda 829cec47e3dSKiyoshi Ueda rq->special = NULL; 830cec47e3dSKiyoshi Ueda rq->cmd_flags &= ~REQ_DONTPREP; 831cec47e3dSKiyoshi Ueda 832a77e28c7SKiyoshi Ueda free_rq_clone(clone); 833cec47e3dSKiyoshi Ueda } 834cec47e3dSKiyoshi Ueda 835cec47e3dSKiyoshi Ueda /* 836cec47e3dSKiyoshi Ueda * Requeue the original request of a clone. 837cec47e3dSKiyoshi Ueda */ 838cec47e3dSKiyoshi Ueda void dm_requeue_unmapped_request(struct request *clone) 839cec47e3dSKiyoshi Ueda { 840b4324feeSKiyoshi Ueda int rw = rq_data_dir(clone); 841cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 842cec47e3dSKiyoshi Ueda struct mapped_device *md = tio->md; 843cec47e3dSKiyoshi Ueda struct request *rq = tio->orig; 844cec47e3dSKiyoshi Ueda struct request_queue *q = rq->q; 845cec47e3dSKiyoshi Ueda unsigned long flags; 846cec47e3dSKiyoshi Ueda 847d0bcb878SKiyoshi Ueda if (unlikely(blk_barrier_rq(clone))) { 848d0bcb878SKiyoshi Ueda /* 849d0bcb878SKiyoshi Ueda * Barrier clones share an original request. 850d0bcb878SKiyoshi Ueda * Leave it to dm_end_request(), which handles this special 851d0bcb878SKiyoshi Ueda * case. 852d0bcb878SKiyoshi Ueda */ 853d0bcb878SKiyoshi Ueda dm_end_request(clone, DM_ENDIO_REQUEUE); 854d0bcb878SKiyoshi Ueda return; 855d0bcb878SKiyoshi Ueda } 856d0bcb878SKiyoshi Ueda 857cec47e3dSKiyoshi Ueda dm_unprep_request(rq); 858cec47e3dSKiyoshi Ueda 859cec47e3dSKiyoshi Ueda spin_lock_irqsave(q->queue_lock, flags); 860cec47e3dSKiyoshi Ueda if (elv_queue_empty(q)) 861cec47e3dSKiyoshi Ueda blk_plug_device(q); 862cec47e3dSKiyoshi Ueda blk_requeue_request(q, rq); 863cec47e3dSKiyoshi Ueda spin_unlock_irqrestore(q->queue_lock, flags); 864cec47e3dSKiyoshi Ueda 865b4324feeSKiyoshi Ueda rq_completed(md, rw, 0); 866cec47e3dSKiyoshi Ueda } 867cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request); 868cec47e3dSKiyoshi Ueda 869cec47e3dSKiyoshi Ueda static void __stop_queue(struct request_queue *q) 870cec47e3dSKiyoshi Ueda { 871cec47e3dSKiyoshi Ueda blk_stop_queue(q); 872cec47e3dSKiyoshi Ueda } 873cec47e3dSKiyoshi Ueda 874cec47e3dSKiyoshi Ueda static void stop_queue(struct request_queue *q) 875cec47e3dSKiyoshi Ueda { 876cec47e3dSKiyoshi Ueda unsigned long flags; 877cec47e3dSKiyoshi Ueda 878cec47e3dSKiyoshi Ueda spin_lock_irqsave(q->queue_lock, flags); 879cec47e3dSKiyoshi Ueda __stop_queue(q); 880cec47e3dSKiyoshi Ueda spin_unlock_irqrestore(q->queue_lock, flags); 881cec47e3dSKiyoshi Ueda } 882cec47e3dSKiyoshi Ueda 883cec47e3dSKiyoshi Ueda static void __start_queue(struct request_queue *q) 884cec47e3dSKiyoshi Ueda { 885cec47e3dSKiyoshi Ueda if (blk_queue_stopped(q)) 886cec47e3dSKiyoshi Ueda blk_start_queue(q); 887cec47e3dSKiyoshi Ueda } 888cec47e3dSKiyoshi Ueda 889cec47e3dSKiyoshi Ueda static void start_queue(struct request_queue *q) 890cec47e3dSKiyoshi Ueda { 891cec47e3dSKiyoshi Ueda unsigned long flags; 892cec47e3dSKiyoshi Ueda 893cec47e3dSKiyoshi Ueda spin_lock_irqsave(q->queue_lock, flags); 894cec47e3dSKiyoshi Ueda __start_queue(q); 895cec47e3dSKiyoshi Ueda spin_unlock_irqrestore(q->queue_lock, flags); 896cec47e3dSKiyoshi Ueda } 897cec47e3dSKiyoshi Ueda 89811a68244SKiyoshi Ueda static void dm_done(struct request *clone, int error, bool mapped) 89911a68244SKiyoshi Ueda { 90011a68244SKiyoshi Ueda int r = error; 90111a68244SKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 90211a68244SKiyoshi Ueda dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io; 90311a68244SKiyoshi Ueda 90411a68244SKiyoshi Ueda if (mapped && rq_end_io) 90511a68244SKiyoshi Ueda r = rq_end_io(tio->ti, clone, error, &tio->info); 90611a68244SKiyoshi Ueda 90711a68244SKiyoshi Ueda if (r <= 0) 90811a68244SKiyoshi Ueda /* The target wants to complete the I/O */ 90911a68244SKiyoshi Ueda dm_end_request(clone, r); 91011a68244SKiyoshi Ueda else if (r == DM_ENDIO_INCOMPLETE) 91111a68244SKiyoshi Ueda /* The target will handle the I/O */ 91211a68244SKiyoshi Ueda return; 91311a68244SKiyoshi Ueda else if (r == DM_ENDIO_REQUEUE) 91411a68244SKiyoshi Ueda /* The target wants to requeue the I/O */ 91511a68244SKiyoshi Ueda dm_requeue_unmapped_request(clone); 91611a68244SKiyoshi Ueda else { 91711a68244SKiyoshi Ueda DMWARN("unimplemented target endio return value: %d", r); 91811a68244SKiyoshi Ueda BUG(); 91911a68244SKiyoshi Ueda } 92011a68244SKiyoshi Ueda } 92111a68244SKiyoshi Ueda 922cec47e3dSKiyoshi Ueda /* 923cec47e3dSKiyoshi Ueda * Request completion handler for request-based dm 924cec47e3dSKiyoshi Ueda */ 925cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq) 926cec47e3dSKiyoshi Ueda { 92711a68244SKiyoshi Ueda bool mapped = true; 928cec47e3dSKiyoshi Ueda struct request *clone = rq->completion_data; 929cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 930cec47e3dSKiyoshi Ueda 93111a68244SKiyoshi Ueda if (rq->cmd_flags & REQ_FAILED) 93211a68244SKiyoshi Ueda mapped = false; 933cec47e3dSKiyoshi Ueda 93411a68244SKiyoshi Ueda dm_done(clone, tio->error, mapped); 935cec47e3dSKiyoshi Ueda } 936cec47e3dSKiyoshi Ueda 937cec47e3dSKiyoshi Ueda /* 938cec47e3dSKiyoshi Ueda * Complete the clone and the original request with the error status 939cec47e3dSKiyoshi Ueda * through softirq context. 940cec47e3dSKiyoshi Ueda */ 941cec47e3dSKiyoshi Ueda static void dm_complete_request(struct request *clone, int error) 942cec47e3dSKiyoshi Ueda { 943cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 944cec47e3dSKiyoshi Ueda struct request *rq = tio->orig; 945cec47e3dSKiyoshi Ueda 946d0bcb878SKiyoshi Ueda if (unlikely(blk_barrier_rq(clone))) { 947d0bcb878SKiyoshi Ueda /* 948d0bcb878SKiyoshi Ueda * Barrier clones share an original request. So can't use 949d0bcb878SKiyoshi Ueda * softirq_done with the original. 950d0bcb878SKiyoshi Ueda * Pass the clone to dm_done() directly in this special case. 951d0bcb878SKiyoshi Ueda * It is safe (even if clone->q->queue_lock is held here) 952d0bcb878SKiyoshi Ueda * because there is no I/O dispatching during the completion 953d0bcb878SKiyoshi Ueda * of barrier clone. 954d0bcb878SKiyoshi Ueda */ 955d0bcb878SKiyoshi Ueda dm_done(clone, error, true); 956d0bcb878SKiyoshi Ueda return; 957d0bcb878SKiyoshi Ueda } 958d0bcb878SKiyoshi Ueda 959cec47e3dSKiyoshi Ueda tio->error = error; 960cec47e3dSKiyoshi Ueda rq->completion_data = clone; 961cec47e3dSKiyoshi Ueda blk_complete_request(rq); 962cec47e3dSKiyoshi Ueda } 963cec47e3dSKiyoshi Ueda 964cec47e3dSKiyoshi Ueda /* 965cec47e3dSKiyoshi Ueda * Complete the not-mapped clone and the original request with the error status 966cec47e3dSKiyoshi Ueda * through softirq context. 967cec47e3dSKiyoshi Ueda * Target's rq_end_io() function isn't called. 968cec47e3dSKiyoshi Ueda * This may be used when the target's map_rq() function fails. 969cec47e3dSKiyoshi Ueda */ 970cec47e3dSKiyoshi Ueda void dm_kill_unmapped_request(struct request *clone, int error) 971cec47e3dSKiyoshi Ueda { 972cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 973cec47e3dSKiyoshi Ueda struct request *rq = tio->orig; 974cec47e3dSKiyoshi Ueda 975d0bcb878SKiyoshi Ueda if (unlikely(blk_barrier_rq(clone))) { 976d0bcb878SKiyoshi Ueda /* 977d0bcb878SKiyoshi Ueda * Barrier clones share an original request. 978d0bcb878SKiyoshi Ueda * Leave it to dm_end_request(), which handles this special 979d0bcb878SKiyoshi Ueda * case. 980d0bcb878SKiyoshi Ueda */ 981d0bcb878SKiyoshi Ueda BUG_ON(error > 0); 982d0bcb878SKiyoshi Ueda dm_end_request(clone, error); 983d0bcb878SKiyoshi Ueda return; 984d0bcb878SKiyoshi Ueda } 985d0bcb878SKiyoshi Ueda 986cec47e3dSKiyoshi Ueda rq->cmd_flags |= REQ_FAILED; 987cec47e3dSKiyoshi Ueda dm_complete_request(clone, error); 988cec47e3dSKiyoshi Ueda } 989cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_kill_unmapped_request); 990cec47e3dSKiyoshi Ueda 991cec47e3dSKiyoshi Ueda /* 992cec47e3dSKiyoshi Ueda * Called with the queue lock held 993cec47e3dSKiyoshi Ueda */ 994cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error) 995cec47e3dSKiyoshi Ueda { 996cec47e3dSKiyoshi Ueda /* 997cec47e3dSKiyoshi Ueda * For just cleaning up the information of the queue in which 998cec47e3dSKiyoshi Ueda * the clone was dispatched. 999cec47e3dSKiyoshi Ueda * The clone is *NOT* freed actually here because it is alloced from 1000cec47e3dSKiyoshi Ueda * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags. 1001cec47e3dSKiyoshi Ueda */ 1002cec47e3dSKiyoshi Ueda __blk_put_request(clone->q, clone); 1003cec47e3dSKiyoshi Ueda 1004cec47e3dSKiyoshi Ueda /* 1005cec47e3dSKiyoshi Ueda * Actual request completion is done in a softirq context which doesn't 1006cec47e3dSKiyoshi Ueda * hold the queue lock. Otherwise, deadlock could occur because: 1007cec47e3dSKiyoshi Ueda * - another request may be submitted by the upper level driver 1008cec47e3dSKiyoshi Ueda * of the stacking during the completion 1009cec47e3dSKiyoshi Ueda * - the submission which requires queue lock may be done 1010cec47e3dSKiyoshi Ueda * against this queue 1011cec47e3dSKiyoshi Ueda */ 1012cec47e3dSKiyoshi Ueda dm_complete_request(clone, error); 1013cec47e3dSKiyoshi Ueda } 1014cec47e3dSKiyoshi Ueda 10151da177e4SLinus Torvalds static sector_t max_io_len(struct mapped_device *md, 10161da177e4SLinus Torvalds sector_t sector, struct dm_target *ti) 10171da177e4SLinus Torvalds { 10181da177e4SLinus Torvalds sector_t offset = sector - ti->begin; 10191da177e4SLinus Torvalds sector_t len = ti->len - offset; 10201da177e4SLinus Torvalds 10211da177e4SLinus Torvalds /* 10221da177e4SLinus Torvalds * Does the target need to split even further ? 10231da177e4SLinus Torvalds */ 10241da177e4SLinus Torvalds if (ti->split_io) { 10251da177e4SLinus Torvalds sector_t boundary; 10261da177e4SLinus Torvalds boundary = ((offset + ti->split_io) & ~(ti->split_io - 1)) 10271da177e4SLinus Torvalds - offset; 10281da177e4SLinus Torvalds if (len > boundary) 10291da177e4SLinus Torvalds len = boundary; 10301da177e4SLinus Torvalds } 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds return len; 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds static void __map_bio(struct dm_target *ti, struct bio *clone, 1036028867acSAlasdair G Kergon struct dm_target_io *tio) 10371da177e4SLinus Torvalds { 10381da177e4SLinus Torvalds int r; 10392056a782SJens Axboe sector_t sector; 10409faf400fSStefan Bader struct mapped_device *md; 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds clone->bi_end_io = clone_endio; 10431da177e4SLinus Torvalds clone->bi_private = tio; 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds /* 10461da177e4SLinus Torvalds * Map the clone. If r == 0 we don't need to do 10471da177e4SLinus Torvalds * anything, the target has assumed ownership of 10481da177e4SLinus Torvalds * this io. 10491da177e4SLinus Torvalds */ 10501da177e4SLinus Torvalds atomic_inc(&tio->io->io_count); 10512056a782SJens Axboe sector = clone->bi_sector; 10521da177e4SLinus Torvalds r = ti->type->map(ti, clone, &tio->info); 105345cbcd79SKiyoshi Ueda if (r == DM_MAPIO_REMAPPED) { 10541da177e4SLinus Torvalds /* the bio has been remapped so dispatch it */ 10552056a782SJens Axboe 10565f3ea37cSArnaldo Carvalho de Melo trace_block_remap(bdev_get_queue(clone->bi_bdev), clone, 105722a7c31aSAlan D. Brunelle tio->io->bio->bi_bdev->bd_dev, sector); 10582056a782SJens Axboe 10591da177e4SLinus Torvalds generic_make_request(clone); 10602e93ccc1SKiyoshi Ueda } else if (r < 0 || r == DM_MAPIO_REQUEUE) { 10612e93ccc1SKiyoshi Ueda /* error the io and bail out, or requeue it if needed */ 10629faf400fSStefan Bader md = tio->io->md; 10639faf400fSStefan Bader dec_pending(tio->io, r); 10649faf400fSStefan Bader /* 10659faf400fSStefan Bader * Store bio_set for cleanup. 10669faf400fSStefan Bader */ 10679faf400fSStefan Bader clone->bi_private = md->bs; 10681da177e4SLinus Torvalds bio_put(clone); 10699faf400fSStefan Bader free_tio(md, tio); 107045cbcd79SKiyoshi Ueda } else if (r) { 107145cbcd79SKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 107245cbcd79SKiyoshi Ueda BUG(); 10731da177e4SLinus Torvalds } 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds 10761da177e4SLinus Torvalds struct clone_info { 10771da177e4SLinus Torvalds struct mapped_device *md; 10781da177e4SLinus Torvalds struct dm_table *map; 10791da177e4SLinus Torvalds struct bio *bio; 10801da177e4SLinus Torvalds struct dm_io *io; 10811da177e4SLinus Torvalds sector_t sector; 10821da177e4SLinus Torvalds sector_t sector_count; 10831da177e4SLinus Torvalds unsigned short idx; 10841da177e4SLinus Torvalds }; 10851da177e4SLinus Torvalds 10863676347aSPeter Osterlund static void dm_bio_destructor(struct bio *bio) 10873676347aSPeter Osterlund { 10889faf400fSStefan Bader struct bio_set *bs = bio->bi_private; 10899faf400fSStefan Bader 10909faf400fSStefan Bader bio_free(bio, bs); 10913676347aSPeter Osterlund } 10923676347aSPeter Osterlund 10931da177e4SLinus Torvalds /* 10941da177e4SLinus Torvalds * Creates a little bio that is just does part of a bvec. 10951da177e4SLinus Torvalds */ 10961da177e4SLinus Torvalds static struct bio *split_bvec(struct bio *bio, sector_t sector, 10971da177e4SLinus Torvalds unsigned short idx, unsigned int offset, 10989faf400fSStefan Bader unsigned int len, struct bio_set *bs) 10991da177e4SLinus Torvalds { 11001da177e4SLinus Torvalds struct bio *clone; 11011da177e4SLinus Torvalds struct bio_vec *bv = bio->bi_io_vec + idx; 11021da177e4SLinus Torvalds 11039faf400fSStefan Bader clone = bio_alloc_bioset(GFP_NOIO, 1, bs); 11043676347aSPeter Osterlund clone->bi_destructor = dm_bio_destructor; 11051da177e4SLinus Torvalds *clone->bi_io_vec = *bv; 11061da177e4SLinus Torvalds 11071da177e4SLinus Torvalds clone->bi_sector = sector; 11081da177e4SLinus Torvalds clone->bi_bdev = bio->bi_bdev; 1109af7e466aSMikulas Patocka clone->bi_rw = bio->bi_rw & ~(1 << BIO_RW_BARRIER); 11101da177e4SLinus Torvalds clone->bi_vcnt = 1; 11111da177e4SLinus Torvalds clone->bi_size = to_bytes(len); 11121da177e4SLinus Torvalds clone->bi_io_vec->bv_offset = offset; 11131da177e4SLinus Torvalds clone->bi_io_vec->bv_len = clone->bi_size; 1114f3e1d26eSMartin K. Petersen clone->bi_flags |= 1 << BIO_CLONED; 11151da177e4SLinus Torvalds 11169c47008dSMartin K. Petersen if (bio_integrity(bio)) { 11177878cba9SMartin K. Petersen bio_integrity_clone(clone, bio, GFP_NOIO, bs); 11189c47008dSMartin K. Petersen bio_integrity_trim(clone, 11199c47008dSMartin K. Petersen bio_sector_offset(bio, idx, offset), len); 11209c47008dSMartin K. Petersen } 11219c47008dSMartin K. Petersen 11221da177e4SLinus Torvalds return clone; 11231da177e4SLinus Torvalds } 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds /* 11261da177e4SLinus Torvalds * Creates a bio that consists of range of complete bvecs. 11271da177e4SLinus Torvalds */ 11281da177e4SLinus Torvalds static struct bio *clone_bio(struct bio *bio, sector_t sector, 11291da177e4SLinus Torvalds unsigned short idx, unsigned short bv_count, 11309faf400fSStefan Bader unsigned int len, struct bio_set *bs) 11311da177e4SLinus Torvalds { 11321da177e4SLinus Torvalds struct bio *clone; 11331da177e4SLinus Torvalds 11349faf400fSStefan Bader clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs); 11359faf400fSStefan Bader __bio_clone(clone, bio); 1136af7e466aSMikulas Patocka clone->bi_rw &= ~(1 << BIO_RW_BARRIER); 11379faf400fSStefan Bader clone->bi_destructor = dm_bio_destructor; 11381da177e4SLinus Torvalds clone->bi_sector = sector; 11391da177e4SLinus Torvalds clone->bi_idx = idx; 11401da177e4SLinus Torvalds clone->bi_vcnt = idx + bv_count; 11411da177e4SLinus Torvalds clone->bi_size = to_bytes(len); 11421da177e4SLinus Torvalds clone->bi_flags &= ~(1 << BIO_SEG_VALID); 11431da177e4SLinus Torvalds 11449c47008dSMartin K. Petersen if (bio_integrity(bio)) { 11457878cba9SMartin K. Petersen bio_integrity_clone(clone, bio, GFP_NOIO, bs); 11469c47008dSMartin K. Petersen 11479c47008dSMartin K. Petersen if (idx != bio->bi_idx || clone->bi_size < bio->bi_size) 11489c47008dSMartin K. Petersen bio_integrity_trim(clone, 11499c47008dSMartin K. Petersen bio_sector_offset(bio, idx, 0), len); 11509c47008dSMartin K. Petersen } 11519c47008dSMartin K. Petersen 11521da177e4SLinus Torvalds return clone; 11531da177e4SLinus Torvalds } 11541da177e4SLinus Torvalds 11559015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci, 11569015df24SAlasdair G Kergon struct dm_target *ti) 1157f9ab94ceSMikulas Patocka { 11589015df24SAlasdair G Kergon struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO); 1159f9ab94ceSMikulas Patocka 1160f9ab94ceSMikulas Patocka tio->io = ci->io; 1161f9ab94ceSMikulas Patocka tio->ti = ti; 1162f9ab94ceSMikulas Patocka memset(&tio->info, 0, sizeof(tio->info)); 11639015df24SAlasdair G Kergon 11649015df24SAlasdair G Kergon return tio; 11659015df24SAlasdair G Kergon } 11669015df24SAlasdair G Kergon 11679015df24SAlasdair G Kergon static void __flush_target(struct clone_info *ci, struct dm_target *ti, 11689015df24SAlasdair G Kergon unsigned flush_nr) 11699015df24SAlasdair G Kergon { 11709015df24SAlasdair G Kergon struct dm_target_io *tio = alloc_tio(ci, ti); 11719015df24SAlasdair G Kergon struct bio *clone; 11729015df24SAlasdair G Kergon 1173f9ab94ceSMikulas Patocka tio->info.flush_request = flush_nr; 1174f9ab94ceSMikulas Patocka 1175f9ab94ceSMikulas Patocka clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs); 1176f9ab94ceSMikulas Patocka __bio_clone(clone, ci->bio); 1177f9ab94ceSMikulas Patocka clone->bi_destructor = dm_bio_destructor; 1178f9ab94ceSMikulas Patocka 1179f9ab94ceSMikulas Patocka __map_bio(ti, clone, tio); 1180f9ab94ceSMikulas Patocka } 1181f9ab94ceSMikulas Patocka 1182f9ab94ceSMikulas Patocka static int __clone_and_map_empty_barrier(struct clone_info *ci) 1183f9ab94ceSMikulas Patocka { 1184f9ab94ceSMikulas Patocka unsigned target_nr = 0, flush_nr; 1185f9ab94ceSMikulas Patocka struct dm_target *ti; 1186f9ab94ceSMikulas Patocka 1187f9ab94ceSMikulas Patocka while ((ti = dm_table_get_target(ci->map, target_nr++))) 1188f9ab94ceSMikulas Patocka for (flush_nr = 0; flush_nr < ti->num_flush_requests; 1189f9ab94ceSMikulas Patocka flush_nr++) 1190f9ab94ceSMikulas Patocka __flush_target(ci, ti, flush_nr); 1191f9ab94ceSMikulas Patocka 1192f9ab94ceSMikulas Patocka ci->sector_count = 0; 1193f9ab94ceSMikulas Patocka 1194f9ab94ceSMikulas Patocka return 0; 1195f9ab94ceSMikulas Patocka } 1196f9ab94ceSMikulas Patocka 1197512875bdSJun'ichi Nomura static int __clone_and_map(struct clone_info *ci) 11981da177e4SLinus Torvalds { 11991da177e4SLinus Torvalds struct bio *clone, *bio = ci->bio; 1200512875bdSJun'ichi Nomura struct dm_target *ti; 1201512875bdSJun'ichi Nomura sector_t len = 0, max; 1202028867acSAlasdair G Kergon struct dm_target_io *tio; 12031da177e4SLinus Torvalds 1204f9ab94ceSMikulas Patocka if (unlikely(bio_empty_barrier(bio))) 1205f9ab94ceSMikulas Patocka return __clone_and_map_empty_barrier(ci); 1206f9ab94ceSMikulas Patocka 1207512875bdSJun'ichi Nomura ti = dm_table_find_target(ci->map, ci->sector); 1208512875bdSJun'ichi Nomura if (!dm_target_is_valid(ti)) 1209512875bdSJun'ichi Nomura return -EIO; 1210512875bdSJun'ichi Nomura 1211512875bdSJun'ichi Nomura max = max_io_len(ci->md, ci->sector, ti); 1212512875bdSJun'ichi Nomura 12131da177e4SLinus Torvalds /* 12141da177e4SLinus Torvalds * Allocate a target io object. 12151da177e4SLinus Torvalds */ 12169015df24SAlasdair G Kergon tio = alloc_tio(ci, ti); 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds if (ci->sector_count <= max) { 12191da177e4SLinus Torvalds /* 12201da177e4SLinus Torvalds * Optimise for the simple case where we can do all of 12211da177e4SLinus Torvalds * the remaining io with a single clone. 12221da177e4SLinus Torvalds */ 12231da177e4SLinus Torvalds clone = clone_bio(bio, ci->sector, ci->idx, 12249faf400fSStefan Bader bio->bi_vcnt - ci->idx, ci->sector_count, 12259faf400fSStefan Bader ci->md->bs); 12261da177e4SLinus Torvalds __map_bio(ti, clone, tio); 12271da177e4SLinus Torvalds ci->sector_count = 0; 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) { 12301da177e4SLinus Torvalds /* 12311da177e4SLinus Torvalds * There are some bvecs that don't span targets. 12321da177e4SLinus Torvalds * Do as many of these as possible. 12331da177e4SLinus Torvalds */ 12341da177e4SLinus Torvalds int i; 12351da177e4SLinus Torvalds sector_t remaining = max; 12361da177e4SLinus Torvalds sector_t bv_len; 12371da177e4SLinus Torvalds 12381da177e4SLinus Torvalds for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) { 12391da177e4SLinus Torvalds bv_len = to_sector(bio->bi_io_vec[i].bv_len); 12401da177e4SLinus Torvalds 12411da177e4SLinus Torvalds if (bv_len > remaining) 12421da177e4SLinus Torvalds break; 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds remaining -= bv_len; 12451da177e4SLinus Torvalds len += bv_len; 12461da177e4SLinus Torvalds } 12471da177e4SLinus Torvalds 12489faf400fSStefan Bader clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len, 12499faf400fSStefan Bader ci->md->bs); 12501da177e4SLinus Torvalds __map_bio(ti, clone, tio); 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds ci->sector += len; 12531da177e4SLinus Torvalds ci->sector_count -= len; 12541da177e4SLinus Torvalds ci->idx = i; 12551da177e4SLinus Torvalds 12561da177e4SLinus Torvalds } else { 12571da177e4SLinus Torvalds /* 1258d2044a94SAlasdair G Kergon * Handle a bvec that must be split between two or more targets. 12591da177e4SLinus Torvalds */ 12601da177e4SLinus Torvalds struct bio_vec *bv = bio->bi_io_vec + ci->idx; 1261d2044a94SAlasdair G Kergon sector_t remaining = to_sector(bv->bv_len); 1262d2044a94SAlasdair G Kergon unsigned int offset = 0; 12631da177e4SLinus Torvalds 1264d2044a94SAlasdair G Kergon do { 1265d2044a94SAlasdair G Kergon if (offset) { 12661da177e4SLinus Torvalds ti = dm_table_find_target(ci->map, ci->sector); 1267512875bdSJun'ichi Nomura if (!dm_target_is_valid(ti)) 1268512875bdSJun'ichi Nomura return -EIO; 1269512875bdSJun'ichi Nomura 1270d2044a94SAlasdair G Kergon max = max_io_len(ci->md, ci->sector, ti); 12711da177e4SLinus Torvalds 12729015df24SAlasdair G Kergon tio = alloc_tio(ci, ti); 1273d2044a94SAlasdair G Kergon } 1274d2044a94SAlasdair G Kergon 1275d2044a94SAlasdair G Kergon len = min(remaining, max); 1276d2044a94SAlasdair G Kergon 1277d2044a94SAlasdair G Kergon clone = split_bvec(bio, ci->sector, ci->idx, 12789faf400fSStefan Bader bv->bv_offset + offset, len, 12799faf400fSStefan Bader ci->md->bs); 1280d2044a94SAlasdair G Kergon 12811da177e4SLinus Torvalds __map_bio(ti, clone, tio); 12821da177e4SLinus Torvalds 12831da177e4SLinus Torvalds ci->sector += len; 12841da177e4SLinus Torvalds ci->sector_count -= len; 1285d2044a94SAlasdair G Kergon offset += to_bytes(len); 1286d2044a94SAlasdair G Kergon } while (remaining -= len); 1287d2044a94SAlasdair G Kergon 12881da177e4SLinus Torvalds ci->idx++; 12891da177e4SLinus Torvalds } 1290512875bdSJun'ichi Nomura 1291512875bdSJun'ichi Nomura return 0; 12921da177e4SLinus Torvalds } 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds /* 12958a53c28dSMikulas Patocka * Split the bio into several clones and submit it to targets. 12961da177e4SLinus Torvalds */ 1297f0b9a450SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md, struct bio *bio) 12981da177e4SLinus Torvalds { 12991da177e4SLinus Torvalds struct clone_info ci; 1300512875bdSJun'ichi Nomura int error = 0; 13011da177e4SLinus Torvalds 13027c666411SAlasdair G Kergon ci.map = dm_get_live_table(md); 1303f0b9a450SMikulas Patocka if (unlikely(!ci.map)) { 13041f98a13fSJens Axboe if (!bio_rw_flagged(bio, BIO_RW_BARRIER)) 1305f0b9a450SMikulas Patocka bio_io_error(bio); 1306af7e466aSMikulas Patocka else 13075aa2781dSMikulas Patocka if (!md->barrier_error) 1308af7e466aSMikulas Patocka md->barrier_error = -EIO; 1309f0b9a450SMikulas Patocka return; 1310f0b9a450SMikulas Patocka } 1311692d0eb9SMikulas Patocka 13121da177e4SLinus Torvalds ci.md = md; 13131da177e4SLinus Torvalds ci.bio = bio; 13141da177e4SLinus Torvalds ci.io = alloc_io(md); 13151da177e4SLinus Torvalds ci.io->error = 0; 13161da177e4SLinus Torvalds atomic_set(&ci.io->io_count, 1); 13171da177e4SLinus Torvalds ci.io->bio = bio; 13181da177e4SLinus Torvalds ci.io->md = md; 1319f88fb981SKiyoshi Ueda spin_lock_init(&ci.io->endio_lock); 13201da177e4SLinus Torvalds ci.sector = bio->bi_sector; 13211da177e4SLinus Torvalds ci.sector_count = bio_sectors(bio); 1322f9ab94ceSMikulas Patocka if (unlikely(bio_empty_barrier(bio))) 1323f9ab94ceSMikulas Patocka ci.sector_count = 1; 13241da177e4SLinus Torvalds ci.idx = bio->bi_idx; 13251da177e4SLinus Torvalds 13263eaf840eSJun'ichi "Nick" Nomura start_io_acct(ci.io); 1327512875bdSJun'ichi Nomura while (ci.sector_count && !error) 1328512875bdSJun'ichi Nomura error = __clone_and_map(&ci); 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds /* drop the extra reference count */ 1331512875bdSJun'ichi Nomura dec_pending(ci.io, error); 13321da177e4SLinus Torvalds dm_table_put(ci.map); 13331da177e4SLinus Torvalds } 13341da177e4SLinus Torvalds /*----------------------------------------------------------------- 13351da177e4SLinus Torvalds * CRUD END 13361da177e4SLinus Torvalds *---------------------------------------------------------------*/ 13371da177e4SLinus Torvalds 1338f6fccb12SMilan Broz static int dm_merge_bvec(struct request_queue *q, 1339f6fccb12SMilan Broz struct bvec_merge_data *bvm, 1340f6fccb12SMilan Broz struct bio_vec *biovec) 1341f6fccb12SMilan Broz { 1342f6fccb12SMilan Broz struct mapped_device *md = q->queuedata; 13437c666411SAlasdair G Kergon struct dm_table *map = dm_get_live_table(md); 1344f6fccb12SMilan Broz struct dm_target *ti; 1345f6fccb12SMilan Broz sector_t max_sectors; 13465037108aSMikulas Patocka int max_size = 0; 1347f6fccb12SMilan Broz 1348f6fccb12SMilan Broz if (unlikely(!map)) 13495037108aSMikulas Patocka goto out; 1350f6fccb12SMilan Broz 1351f6fccb12SMilan Broz ti = dm_table_find_target(map, bvm->bi_sector); 1352b01cd5acSMikulas Patocka if (!dm_target_is_valid(ti)) 1353b01cd5acSMikulas Patocka goto out_table; 1354f6fccb12SMilan Broz 1355f6fccb12SMilan Broz /* 1356f6fccb12SMilan Broz * Find maximum amount of I/O that won't need splitting 1357f6fccb12SMilan Broz */ 1358f6fccb12SMilan Broz max_sectors = min(max_io_len(md, bvm->bi_sector, ti), 1359f6fccb12SMilan Broz (sector_t) BIO_MAX_SECTORS); 1360f6fccb12SMilan Broz max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size; 1361f6fccb12SMilan Broz if (max_size < 0) 1362f6fccb12SMilan Broz max_size = 0; 1363f6fccb12SMilan Broz 1364f6fccb12SMilan Broz /* 1365f6fccb12SMilan Broz * merge_bvec_fn() returns number of bytes 1366f6fccb12SMilan Broz * it can accept at this offset 1367f6fccb12SMilan Broz * max is precomputed maximal io size 1368f6fccb12SMilan Broz */ 1369f6fccb12SMilan Broz if (max_size && ti->type->merge) 1370f6fccb12SMilan Broz max_size = ti->type->merge(ti, bvm, biovec, max_size); 13718cbeb67aSMikulas Patocka /* 13728cbeb67aSMikulas Patocka * If the target doesn't support merge method and some of the devices 13738cbeb67aSMikulas Patocka * provided their merge_bvec method (we know this by looking at 13748cbeb67aSMikulas Patocka * queue_max_hw_sectors), then we can't allow bios with multiple vector 13758cbeb67aSMikulas Patocka * entries. So always set max_size to 0, and the code below allows 13768cbeb67aSMikulas Patocka * just one page. 13778cbeb67aSMikulas Patocka */ 13788cbeb67aSMikulas Patocka else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9) 13798cbeb67aSMikulas Patocka 13808cbeb67aSMikulas Patocka max_size = 0; 1381f6fccb12SMilan Broz 1382b01cd5acSMikulas Patocka out_table: 13835037108aSMikulas Patocka dm_table_put(map); 13845037108aSMikulas Patocka 13855037108aSMikulas Patocka out: 1386f6fccb12SMilan Broz /* 1387f6fccb12SMilan Broz * Always allow an entire first page 1388f6fccb12SMilan Broz */ 1389f6fccb12SMilan Broz if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT)) 1390f6fccb12SMilan Broz max_size = biovec->bv_len; 1391f6fccb12SMilan Broz 1392f6fccb12SMilan Broz return max_size; 1393f6fccb12SMilan Broz } 1394f6fccb12SMilan Broz 13951da177e4SLinus Torvalds /* 13961da177e4SLinus Torvalds * The request function that just remaps the bio built up by 13971da177e4SLinus Torvalds * dm_merge_bvec. 13981da177e4SLinus Torvalds */ 1399cec47e3dSKiyoshi Ueda static int _dm_request(struct request_queue *q, struct bio *bio) 14001da177e4SLinus Torvalds { 140112f03a49SKevin Corry int rw = bio_data_dir(bio); 14021da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 1403c9959059STejun Heo int cpu; 14041da177e4SLinus Torvalds 14052ca3310eSAlasdair G Kergon down_read(&md->io_lock); 14061da177e4SLinus Torvalds 1407074a7acaSTejun Heo cpu = part_stat_lock(); 1408074a7acaSTejun Heo part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]); 1409074a7acaSTejun Heo part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio)); 1410074a7acaSTejun Heo part_stat_unlock(); 141112f03a49SKevin Corry 14121da177e4SLinus Torvalds /* 14131eb787ecSAlasdair G Kergon * If we're suspended or the thread is processing barriers 14141eb787ecSAlasdair G Kergon * we have to queue this io for later. 14151da177e4SLinus Torvalds */ 1416af7e466aSMikulas Patocka if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) || 14171f98a13fSJens Axboe unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) { 14182ca3310eSAlasdair G Kergon up_read(&md->io_lock); 14191da177e4SLinus Torvalds 142054d9a1b4SAlasdair G Kergon if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) && 142154d9a1b4SAlasdair G Kergon bio_rw(bio) == READA) { 142254d9a1b4SAlasdair G Kergon bio_io_error(bio); 142354d9a1b4SAlasdair G Kergon return 0; 142454d9a1b4SAlasdair G Kergon } 14251da177e4SLinus Torvalds 142692c63902SMikulas Patocka queue_io(md, bio); 14271da177e4SLinus Torvalds 142892c63902SMikulas Patocka return 0; 14291da177e4SLinus Torvalds } 14301da177e4SLinus Torvalds 1431f0b9a450SMikulas Patocka __split_and_process_bio(md, bio); 14322ca3310eSAlasdair G Kergon up_read(&md->io_lock); 1433f0b9a450SMikulas Patocka return 0; 14341da177e4SLinus Torvalds } 14351da177e4SLinus Torvalds 1436cec47e3dSKiyoshi Ueda static int dm_make_request(struct request_queue *q, struct bio *bio) 1437cec47e3dSKiyoshi Ueda { 1438cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 1439cec47e3dSKiyoshi Ueda 1440cec47e3dSKiyoshi Ueda return md->saved_make_request_fn(q, bio); /* call __make_request() */ 1441cec47e3dSKiyoshi Ueda } 1442cec47e3dSKiyoshi Ueda 1443cec47e3dSKiyoshi Ueda static int dm_request_based(struct mapped_device *md) 1444cec47e3dSKiyoshi Ueda { 1445cec47e3dSKiyoshi Ueda return blk_queue_stackable(md->queue); 1446cec47e3dSKiyoshi Ueda } 1447cec47e3dSKiyoshi Ueda 1448cec47e3dSKiyoshi Ueda static int dm_request(struct request_queue *q, struct bio *bio) 1449cec47e3dSKiyoshi Ueda { 1450cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 1451cec47e3dSKiyoshi Ueda 1452cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 1453cec47e3dSKiyoshi Ueda return dm_make_request(q, bio); 1454cec47e3dSKiyoshi Ueda 1455cec47e3dSKiyoshi Ueda return _dm_request(q, bio); 1456cec47e3dSKiyoshi Ueda } 1457cec47e3dSKiyoshi Ueda 1458d0bcb878SKiyoshi Ueda /* 1459d0bcb878SKiyoshi Ueda * Mark this request as flush request, so that dm_request_fn() can 1460d0bcb878SKiyoshi Ueda * recognize. 1461d0bcb878SKiyoshi Ueda */ 1462d0bcb878SKiyoshi Ueda static void dm_rq_prepare_flush(struct request_queue *q, struct request *rq) 1463d0bcb878SKiyoshi Ueda { 1464d0bcb878SKiyoshi Ueda rq->cmd_type = REQ_TYPE_LINUX_BLOCK; 1465d0bcb878SKiyoshi Ueda rq->cmd[0] = REQ_LB_OP_FLUSH; 1466d0bcb878SKiyoshi Ueda } 1467d0bcb878SKiyoshi Ueda 1468d0bcb878SKiyoshi Ueda static bool dm_rq_is_flush_request(struct request *rq) 1469d0bcb878SKiyoshi Ueda { 1470d0bcb878SKiyoshi Ueda if (rq->cmd_type == REQ_TYPE_LINUX_BLOCK && 1471d0bcb878SKiyoshi Ueda rq->cmd[0] == REQ_LB_OP_FLUSH) 1472d0bcb878SKiyoshi Ueda return true; 1473d0bcb878SKiyoshi Ueda else 1474d0bcb878SKiyoshi Ueda return false; 1475d0bcb878SKiyoshi Ueda } 1476d0bcb878SKiyoshi Ueda 1477cec47e3dSKiyoshi Ueda void dm_dispatch_request(struct request *rq) 1478cec47e3dSKiyoshi Ueda { 1479cec47e3dSKiyoshi Ueda int r; 1480cec47e3dSKiyoshi Ueda 1481cec47e3dSKiyoshi Ueda if (blk_queue_io_stat(rq->q)) 1482cec47e3dSKiyoshi Ueda rq->cmd_flags |= REQ_IO_STAT; 1483cec47e3dSKiyoshi Ueda 1484cec47e3dSKiyoshi Ueda rq->start_time = jiffies; 1485cec47e3dSKiyoshi Ueda r = blk_insert_cloned_request(rq->q, rq); 1486cec47e3dSKiyoshi Ueda if (r) 1487cec47e3dSKiyoshi Ueda dm_complete_request(rq, r); 1488cec47e3dSKiyoshi Ueda } 1489cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_dispatch_request); 1490cec47e3dSKiyoshi Ueda 1491cec47e3dSKiyoshi Ueda static void dm_rq_bio_destructor(struct bio *bio) 1492cec47e3dSKiyoshi Ueda { 1493cec47e3dSKiyoshi Ueda struct dm_rq_clone_bio_info *info = bio->bi_private; 1494cec47e3dSKiyoshi Ueda struct mapped_device *md = info->tio->md; 1495cec47e3dSKiyoshi Ueda 1496cec47e3dSKiyoshi Ueda free_bio_info(info); 1497cec47e3dSKiyoshi Ueda bio_free(bio, md->bs); 1498cec47e3dSKiyoshi Ueda } 1499cec47e3dSKiyoshi Ueda 1500cec47e3dSKiyoshi Ueda static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig, 1501cec47e3dSKiyoshi Ueda void *data) 1502cec47e3dSKiyoshi Ueda { 1503cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = data; 1504cec47e3dSKiyoshi Ueda struct mapped_device *md = tio->md; 1505cec47e3dSKiyoshi Ueda struct dm_rq_clone_bio_info *info = alloc_bio_info(md); 1506cec47e3dSKiyoshi Ueda 1507cec47e3dSKiyoshi Ueda if (!info) 1508cec47e3dSKiyoshi Ueda return -ENOMEM; 1509cec47e3dSKiyoshi Ueda 1510cec47e3dSKiyoshi Ueda info->orig = bio_orig; 1511cec47e3dSKiyoshi Ueda info->tio = tio; 1512cec47e3dSKiyoshi Ueda bio->bi_end_io = end_clone_bio; 1513cec47e3dSKiyoshi Ueda bio->bi_private = info; 1514cec47e3dSKiyoshi Ueda bio->bi_destructor = dm_rq_bio_destructor; 1515cec47e3dSKiyoshi Ueda 1516cec47e3dSKiyoshi Ueda return 0; 1517cec47e3dSKiyoshi Ueda } 1518cec47e3dSKiyoshi Ueda 1519cec47e3dSKiyoshi Ueda static int setup_clone(struct request *clone, struct request *rq, 1520cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio) 1521cec47e3dSKiyoshi Ueda { 1522d0bcb878SKiyoshi Ueda int r; 1523cec47e3dSKiyoshi Ueda 1524d0bcb878SKiyoshi Ueda if (dm_rq_is_flush_request(rq)) { 1525d0bcb878SKiyoshi Ueda blk_rq_init(NULL, clone); 1526d0bcb878SKiyoshi Ueda clone->cmd_type = REQ_TYPE_FS; 1527d0bcb878SKiyoshi Ueda clone->cmd_flags |= (REQ_HARDBARRIER | WRITE); 1528d0bcb878SKiyoshi Ueda } else { 1529d0bcb878SKiyoshi Ueda r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC, 1530d0bcb878SKiyoshi Ueda dm_rq_bio_constructor, tio); 1531cec47e3dSKiyoshi Ueda if (r) 1532cec47e3dSKiyoshi Ueda return r; 1533cec47e3dSKiyoshi Ueda 1534cec47e3dSKiyoshi Ueda clone->cmd = rq->cmd; 1535cec47e3dSKiyoshi Ueda clone->cmd_len = rq->cmd_len; 1536cec47e3dSKiyoshi Ueda clone->sense = rq->sense; 1537cec47e3dSKiyoshi Ueda clone->buffer = rq->buffer; 1538d0bcb878SKiyoshi Ueda } 1539d0bcb878SKiyoshi Ueda 1540cec47e3dSKiyoshi Ueda clone->end_io = end_clone_request; 1541cec47e3dSKiyoshi Ueda clone->end_io_data = tio; 1542cec47e3dSKiyoshi Ueda 1543cec47e3dSKiyoshi Ueda return 0; 1544cec47e3dSKiyoshi Ueda } 1545cec47e3dSKiyoshi Ueda 15466facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md, 15476facdaffSKiyoshi Ueda gfp_t gfp_mask) 15486facdaffSKiyoshi Ueda { 15496facdaffSKiyoshi Ueda struct request *clone; 15506facdaffSKiyoshi Ueda struct dm_rq_target_io *tio; 15516facdaffSKiyoshi Ueda 15526facdaffSKiyoshi Ueda tio = alloc_rq_tio(md, gfp_mask); 15536facdaffSKiyoshi Ueda if (!tio) 15546facdaffSKiyoshi Ueda return NULL; 15556facdaffSKiyoshi Ueda 15566facdaffSKiyoshi Ueda tio->md = md; 15576facdaffSKiyoshi Ueda tio->ti = NULL; 15586facdaffSKiyoshi Ueda tio->orig = rq; 15596facdaffSKiyoshi Ueda tio->error = 0; 15606facdaffSKiyoshi Ueda memset(&tio->info, 0, sizeof(tio->info)); 15616facdaffSKiyoshi Ueda 15626facdaffSKiyoshi Ueda clone = &tio->clone; 15636facdaffSKiyoshi Ueda if (setup_clone(clone, rq, tio)) { 15646facdaffSKiyoshi Ueda /* -ENOMEM */ 15656facdaffSKiyoshi Ueda free_rq_tio(tio); 15666facdaffSKiyoshi Ueda return NULL; 15676facdaffSKiyoshi Ueda } 15686facdaffSKiyoshi Ueda 15696facdaffSKiyoshi Ueda return clone; 15706facdaffSKiyoshi Ueda } 15716facdaffSKiyoshi Ueda 1572cec47e3dSKiyoshi Ueda /* 1573cec47e3dSKiyoshi Ueda * Called with the queue lock held. 1574cec47e3dSKiyoshi Ueda */ 1575cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq) 1576cec47e3dSKiyoshi Ueda { 1577cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 1578cec47e3dSKiyoshi Ueda struct request *clone; 1579cec47e3dSKiyoshi Ueda 1580d0bcb878SKiyoshi Ueda if (unlikely(dm_rq_is_flush_request(rq))) 1581d0bcb878SKiyoshi Ueda return BLKPREP_OK; 1582d0bcb878SKiyoshi Ueda 1583cec47e3dSKiyoshi Ueda if (unlikely(rq->special)) { 1584cec47e3dSKiyoshi Ueda DMWARN("Already has something in rq->special."); 1585cec47e3dSKiyoshi Ueda return BLKPREP_KILL; 1586cec47e3dSKiyoshi Ueda } 1587cec47e3dSKiyoshi Ueda 15886facdaffSKiyoshi Ueda clone = clone_rq(rq, md, GFP_ATOMIC); 15896facdaffSKiyoshi Ueda if (!clone) 1590cec47e3dSKiyoshi Ueda return BLKPREP_DEFER; 1591cec47e3dSKiyoshi Ueda 1592cec47e3dSKiyoshi Ueda rq->special = clone; 1593cec47e3dSKiyoshi Ueda rq->cmd_flags |= REQ_DONTPREP; 1594cec47e3dSKiyoshi Ueda 1595cec47e3dSKiyoshi Ueda return BLKPREP_OK; 1596cec47e3dSKiyoshi Ueda } 1597cec47e3dSKiyoshi Ueda 1598598de409SKiyoshi Ueda static void map_request(struct dm_target *ti, struct request *clone, 1599cec47e3dSKiyoshi Ueda struct mapped_device *md) 1600cec47e3dSKiyoshi Ueda { 1601cec47e3dSKiyoshi Ueda int r; 1602cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 1603cec47e3dSKiyoshi Ueda 1604cec47e3dSKiyoshi Ueda /* 1605cec47e3dSKiyoshi Ueda * Hold the md reference here for the in-flight I/O. 1606cec47e3dSKiyoshi Ueda * We can't rely on the reference count by device opener, 1607cec47e3dSKiyoshi Ueda * because the device may be closed during the request completion 1608cec47e3dSKiyoshi Ueda * when all bios are completed. 1609cec47e3dSKiyoshi Ueda * See the comment in rq_completed() too. 1610cec47e3dSKiyoshi Ueda */ 1611cec47e3dSKiyoshi Ueda dm_get(md); 1612cec47e3dSKiyoshi Ueda 1613cec47e3dSKiyoshi Ueda tio->ti = ti; 1614cec47e3dSKiyoshi Ueda r = ti->type->map_rq(ti, clone, &tio->info); 1615cec47e3dSKiyoshi Ueda switch (r) { 1616cec47e3dSKiyoshi Ueda case DM_MAPIO_SUBMITTED: 1617cec47e3dSKiyoshi Ueda /* The target has taken the I/O to submit by itself later */ 1618cec47e3dSKiyoshi Ueda break; 1619cec47e3dSKiyoshi Ueda case DM_MAPIO_REMAPPED: 1620cec47e3dSKiyoshi Ueda /* The target has remapped the I/O so dispatch it */ 1621cec47e3dSKiyoshi Ueda dm_dispatch_request(clone); 1622cec47e3dSKiyoshi Ueda break; 1623cec47e3dSKiyoshi Ueda case DM_MAPIO_REQUEUE: 1624cec47e3dSKiyoshi Ueda /* The target wants to requeue the I/O */ 1625cec47e3dSKiyoshi Ueda dm_requeue_unmapped_request(clone); 1626cec47e3dSKiyoshi Ueda break; 1627cec47e3dSKiyoshi Ueda default: 1628cec47e3dSKiyoshi Ueda if (r > 0) { 1629cec47e3dSKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 1630cec47e3dSKiyoshi Ueda BUG(); 1631cec47e3dSKiyoshi Ueda } 1632cec47e3dSKiyoshi Ueda 1633cec47e3dSKiyoshi Ueda /* The target wants to complete the I/O */ 1634cec47e3dSKiyoshi Ueda dm_kill_unmapped_request(clone, r); 1635cec47e3dSKiyoshi Ueda break; 1636cec47e3dSKiyoshi Ueda } 1637cec47e3dSKiyoshi Ueda } 1638cec47e3dSKiyoshi Ueda 1639cec47e3dSKiyoshi Ueda /* 1640cec47e3dSKiyoshi Ueda * q->request_fn for request-based dm. 1641cec47e3dSKiyoshi Ueda * Called with the queue lock held. 1642cec47e3dSKiyoshi Ueda */ 1643cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q) 1644cec47e3dSKiyoshi Ueda { 1645cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 16467c666411SAlasdair G Kergon struct dm_table *map = dm_get_live_table(md); 1647cec47e3dSKiyoshi Ueda struct dm_target *ti; 1648b4324feeSKiyoshi Ueda struct request *rq, *clone; 1649cec47e3dSKiyoshi Ueda 1650cec47e3dSKiyoshi Ueda /* 1651b4324feeSKiyoshi Ueda * For suspend, check blk_queue_stopped() and increment 1652b4324feeSKiyoshi Ueda * ->pending within a single queue_lock not to increment the 1653b4324feeSKiyoshi Ueda * number of in-flight I/Os after the queue is stopped in 1654b4324feeSKiyoshi Ueda * dm_suspend(). 1655cec47e3dSKiyoshi Ueda */ 1656cec47e3dSKiyoshi Ueda while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) { 1657cec47e3dSKiyoshi Ueda rq = blk_peek_request(q); 1658cec47e3dSKiyoshi Ueda if (!rq) 1659cec47e3dSKiyoshi Ueda goto plug_and_out; 1660cec47e3dSKiyoshi Ueda 1661d0bcb878SKiyoshi Ueda if (unlikely(dm_rq_is_flush_request(rq))) { 1662d0bcb878SKiyoshi Ueda BUG_ON(md->flush_request); 1663d0bcb878SKiyoshi Ueda md->flush_request = rq; 1664d0bcb878SKiyoshi Ueda blk_start_request(rq); 1665d0bcb878SKiyoshi Ueda queue_work(md->wq, &md->barrier_work); 1666d0bcb878SKiyoshi Ueda goto out; 1667d0bcb878SKiyoshi Ueda } 1668d0bcb878SKiyoshi Ueda 1669cec47e3dSKiyoshi Ueda ti = dm_table_find_target(map, blk_rq_pos(rq)); 1670cec47e3dSKiyoshi Ueda if (ti->type->busy && ti->type->busy(ti)) 1671cec47e3dSKiyoshi Ueda goto plug_and_out; 1672cec47e3dSKiyoshi Ueda 1673cec47e3dSKiyoshi Ueda blk_start_request(rq); 1674b4324feeSKiyoshi Ueda clone = rq->special; 1675b4324feeSKiyoshi Ueda atomic_inc(&md->pending[rq_data_dir(clone)]); 1676b4324feeSKiyoshi Ueda 1677cec47e3dSKiyoshi Ueda spin_unlock(q->queue_lock); 1678b4324feeSKiyoshi Ueda map_request(ti, clone, md); 1679cec47e3dSKiyoshi Ueda spin_lock_irq(q->queue_lock); 1680cec47e3dSKiyoshi Ueda } 1681cec47e3dSKiyoshi Ueda 1682cec47e3dSKiyoshi Ueda goto out; 1683cec47e3dSKiyoshi Ueda 1684cec47e3dSKiyoshi Ueda plug_and_out: 1685cec47e3dSKiyoshi Ueda if (!elv_queue_empty(q)) 1686cec47e3dSKiyoshi Ueda /* Some requests still remain, retry later */ 1687cec47e3dSKiyoshi Ueda blk_plug_device(q); 1688cec47e3dSKiyoshi Ueda 1689cec47e3dSKiyoshi Ueda out: 1690cec47e3dSKiyoshi Ueda dm_table_put(map); 1691cec47e3dSKiyoshi Ueda 1692cec47e3dSKiyoshi Ueda return; 1693cec47e3dSKiyoshi Ueda } 1694cec47e3dSKiyoshi Ueda 1695cec47e3dSKiyoshi Ueda int dm_underlying_device_busy(struct request_queue *q) 1696cec47e3dSKiyoshi Ueda { 1697cec47e3dSKiyoshi Ueda return blk_lld_busy(q); 1698cec47e3dSKiyoshi Ueda } 1699cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_underlying_device_busy); 1700cec47e3dSKiyoshi Ueda 1701cec47e3dSKiyoshi Ueda static int dm_lld_busy(struct request_queue *q) 1702cec47e3dSKiyoshi Ueda { 1703cec47e3dSKiyoshi Ueda int r; 1704cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 17057c666411SAlasdair G Kergon struct dm_table *map = dm_get_live_table(md); 1706cec47e3dSKiyoshi Ueda 1707cec47e3dSKiyoshi Ueda if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) 1708cec47e3dSKiyoshi Ueda r = 1; 1709cec47e3dSKiyoshi Ueda else 1710cec47e3dSKiyoshi Ueda r = dm_table_any_busy_target(map); 1711cec47e3dSKiyoshi Ueda 1712cec47e3dSKiyoshi Ueda dm_table_put(map); 1713cec47e3dSKiyoshi Ueda 1714cec47e3dSKiyoshi Ueda return r; 1715cec47e3dSKiyoshi Ueda } 1716cec47e3dSKiyoshi Ueda 1717165125e1SJens Axboe static void dm_unplug_all(struct request_queue *q) 17181da177e4SLinus Torvalds { 17191da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 17207c666411SAlasdair G Kergon struct dm_table *map = dm_get_live_table(md); 17211da177e4SLinus Torvalds 17221da177e4SLinus Torvalds if (map) { 1723cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 1724cec47e3dSKiyoshi Ueda generic_unplug_device(q); 1725cec47e3dSKiyoshi Ueda 17261da177e4SLinus Torvalds dm_table_unplug_all(map); 17271da177e4SLinus Torvalds dm_table_put(map); 17281da177e4SLinus Torvalds } 17291da177e4SLinus Torvalds } 17301da177e4SLinus Torvalds 17311da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits) 17321da177e4SLinus Torvalds { 17338a57dfc6SChandra Seetharaman int r = bdi_bits; 17348a57dfc6SChandra Seetharaman struct mapped_device *md = congested_data; 17358a57dfc6SChandra Seetharaman struct dm_table *map; 17361da177e4SLinus Torvalds 17371eb787ecSAlasdair G Kergon if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 17387c666411SAlasdair G Kergon map = dm_get_live_table(md); 17398a57dfc6SChandra Seetharaman if (map) { 1740cec47e3dSKiyoshi Ueda /* 1741cec47e3dSKiyoshi Ueda * Request-based dm cares about only own queue for 1742cec47e3dSKiyoshi Ueda * the query about congestion status of request_queue 1743cec47e3dSKiyoshi Ueda */ 1744cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 1745cec47e3dSKiyoshi Ueda r = md->queue->backing_dev_info.state & 1746cec47e3dSKiyoshi Ueda bdi_bits; 1747cec47e3dSKiyoshi Ueda else 17481da177e4SLinus Torvalds r = dm_table_any_congested(map, bdi_bits); 1749cec47e3dSKiyoshi Ueda 17501da177e4SLinus Torvalds dm_table_put(map); 17518a57dfc6SChandra Seetharaman } 17528a57dfc6SChandra Seetharaman } 17538a57dfc6SChandra Seetharaman 17541da177e4SLinus Torvalds return r; 17551da177e4SLinus Torvalds } 17561da177e4SLinus Torvalds 17571da177e4SLinus Torvalds /*----------------------------------------------------------------- 17581da177e4SLinus Torvalds * An IDR is used to keep track of allocated minor numbers. 17591da177e4SLinus Torvalds *---------------------------------------------------------------*/ 17601da177e4SLinus Torvalds static DEFINE_IDR(_minor_idr); 17611da177e4SLinus Torvalds 17622b06cfffSAlasdair G Kergon static void free_minor(int minor) 17631da177e4SLinus Torvalds { 1764f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 17651da177e4SLinus Torvalds idr_remove(&_minor_idr, minor); 1766f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 17671da177e4SLinus Torvalds } 17681da177e4SLinus Torvalds 17691da177e4SLinus Torvalds /* 17701da177e4SLinus Torvalds * See if the device with a specific minor # is free. 17711da177e4SLinus Torvalds */ 1772cf13ab8eSFrederik Deweerdt static int specific_minor(int minor) 17731da177e4SLinus Torvalds { 17741da177e4SLinus Torvalds int r, m; 17751da177e4SLinus Torvalds 17761da177e4SLinus Torvalds if (minor >= (1 << MINORBITS)) 17771da177e4SLinus Torvalds return -EINVAL; 17781da177e4SLinus Torvalds 177962f75c2fSJeff Mahoney r = idr_pre_get(&_minor_idr, GFP_KERNEL); 178062f75c2fSJeff Mahoney if (!r) 178162f75c2fSJeff Mahoney return -ENOMEM; 178262f75c2fSJeff Mahoney 1783f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds if (idr_find(&_minor_idr, minor)) { 17861da177e4SLinus Torvalds r = -EBUSY; 17871da177e4SLinus Torvalds goto out; 17881da177e4SLinus Torvalds } 17891da177e4SLinus Torvalds 1790ba61fdd1SJeff Mahoney r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m); 179162f75c2fSJeff Mahoney if (r) 17921da177e4SLinus Torvalds goto out; 17931da177e4SLinus Torvalds 17941da177e4SLinus Torvalds if (m != minor) { 17951da177e4SLinus Torvalds idr_remove(&_minor_idr, m); 17961da177e4SLinus Torvalds r = -EBUSY; 17971da177e4SLinus Torvalds goto out; 17981da177e4SLinus Torvalds } 17991da177e4SLinus Torvalds 18001da177e4SLinus Torvalds out: 1801f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 18021da177e4SLinus Torvalds return r; 18031da177e4SLinus Torvalds } 18041da177e4SLinus Torvalds 1805cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor) 18061da177e4SLinus Torvalds { 18072b06cfffSAlasdair G Kergon int r, m; 18081da177e4SLinus Torvalds 18091da177e4SLinus Torvalds r = idr_pre_get(&_minor_idr, GFP_KERNEL); 181062f75c2fSJeff Mahoney if (!r) 181162f75c2fSJeff Mahoney return -ENOMEM; 181262f75c2fSJeff Mahoney 1813f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 18141da177e4SLinus Torvalds 1815ba61fdd1SJeff Mahoney r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m); 1816cf13ab8eSFrederik Deweerdt if (r) 18171da177e4SLinus Torvalds goto out; 18181da177e4SLinus Torvalds 18191da177e4SLinus Torvalds if (m >= (1 << MINORBITS)) { 18201da177e4SLinus Torvalds idr_remove(&_minor_idr, m); 18211da177e4SLinus Torvalds r = -ENOSPC; 18221da177e4SLinus Torvalds goto out; 18231da177e4SLinus Torvalds } 18241da177e4SLinus Torvalds 18251da177e4SLinus Torvalds *minor = m; 18261da177e4SLinus Torvalds 18271da177e4SLinus Torvalds out: 1828f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 18291da177e4SLinus Torvalds return r; 18301da177e4SLinus Torvalds } 18311da177e4SLinus Torvalds 183283d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops; 18331da177e4SLinus Torvalds 183453d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work); 1835d0bcb878SKiyoshi Ueda static void dm_rq_barrier_work(struct work_struct *work); 183653d5914fSMikulas Patocka 18371da177e4SLinus Torvalds /* 18381da177e4SLinus Torvalds * Allocate and initialise a blank device with a given minor. 18391da177e4SLinus Torvalds */ 18402b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor) 18411da177e4SLinus Torvalds { 18421da177e4SLinus Torvalds int r; 1843cf13ab8eSFrederik Deweerdt struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL); 1844ba61fdd1SJeff Mahoney void *old_md; 18451da177e4SLinus Torvalds 18461da177e4SLinus Torvalds if (!md) { 18471da177e4SLinus Torvalds DMWARN("unable to allocate device, out of memory."); 18481da177e4SLinus Torvalds return NULL; 18491da177e4SLinus Torvalds } 18501da177e4SLinus Torvalds 185110da4f79SJeff Mahoney if (!try_module_get(THIS_MODULE)) 18526ed7ade8SMilan Broz goto bad_module_get; 185310da4f79SJeff Mahoney 18541da177e4SLinus Torvalds /* get a minor number for the dev */ 18552b06cfffSAlasdair G Kergon if (minor == DM_ANY_MINOR) 1856cf13ab8eSFrederik Deweerdt r = next_free_minor(&minor); 18572b06cfffSAlasdair G Kergon else 1858cf13ab8eSFrederik Deweerdt r = specific_minor(minor); 18591da177e4SLinus Torvalds if (r < 0) 18606ed7ade8SMilan Broz goto bad_minor; 18611da177e4SLinus Torvalds 18622ca3310eSAlasdair G Kergon init_rwsem(&md->io_lock); 1863e61290a4SDaniel Walker mutex_init(&md->suspend_lock); 1864022c2611SMikulas Patocka spin_lock_init(&md->deferred_lock); 1865d0bcb878SKiyoshi Ueda spin_lock_init(&md->barrier_error_lock); 18661da177e4SLinus Torvalds rwlock_init(&md->map_lock); 18671da177e4SLinus Torvalds atomic_set(&md->holders, 1); 18685c6bd75dSAlasdair G Kergon atomic_set(&md->open_count, 0); 18691da177e4SLinus Torvalds atomic_set(&md->event_nr, 0); 18707a8c3d3bSMike Anderson atomic_set(&md->uevent_seq, 0); 18717a8c3d3bSMike Anderson INIT_LIST_HEAD(&md->uevent_list); 18727a8c3d3bSMike Anderson spin_lock_init(&md->uevent_lock); 18731da177e4SLinus Torvalds 1874e6ee8c0bSKiyoshi Ueda md->queue = blk_init_queue(dm_request_fn, NULL); 18751da177e4SLinus Torvalds if (!md->queue) 18766ed7ade8SMilan Broz goto bad_queue; 18771da177e4SLinus Torvalds 1878e6ee8c0bSKiyoshi Ueda /* 1879e6ee8c0bSKiyoshi Ueda * Request-based dm devices cannot be stacked on top of bio-based dm 1880e6ee8c0bSKiyoshi Ueda * devices. The type of this dm device has not been decided yet, 1881e6ee8c0bSKiyoshi Ueda * although we initialized the queue using blk_init_queue(). 1882e6ee8c0bSKiyoshi Ueda * The type is decided at the first table loading time. 1883e6ee8c0bSKiyoshi Ueda * To prevent problematic device stacking, clear the queue flag 1884e6ee8c0bSKiyoshi Ueda * for request stacking support until then. 1885e6ee8c0bSKiyoshi Ueda * 1886e6ee8c0bSKiyoshi Ueda * This queue is new, so no concurrency on the queue_flags. 1887e6ee8c0bSKiyoshi Ueda */ 1888e6ee8c0bSKiyoshi Ueda queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue); 1889e6ee8c0bSKiyoshi Ueda md->saved_make_request_fn = md->queue->make_request_fn; 18901da177e4SLinus Torvalds md->queue->queuedata = md; 18911da177e4SLinus Torvalds md->queue->backing_dev_info.congested_fn = dm_any_congested; 18921da177e4SLinus Torvalds md->queue->backing_dev_info.congested_data = md; 18931da177e4SLinus Torvalds blk_queue_make_request(md->queue, dm_request); 1894daef265fSJens Axboe blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY); 18951da177e4SLinus Torvalds md->queue->unplug_fn = dm_unplug_all; 1896f6fccb12SMilan Broz blk_queue_merge_bvec(md->queue, dm_merge_bvec); 1897e6ee8c0bSKiyoshi Ueda blk_queue_softirq_done(md->queue, dm_softirq_done); 1898e6ee8c0bSKiyoshi Ueda blk_queue_prep_rq(md->queue, dm_prep_fn); 1899e6ee8c0bSKiyoshi Ueda blk_queue_lld_busy(md->queue, dm_lld_busy); 1900d0bcb878SKiyoshi Ueda blk_queue_ordered(md->queue, QUEUE_ORDERED_DRAIN_FLUSH, 1901d0bcb878SKiyoshi Ueda dm_rq_prepare_flush); 19029faf400fSStefan Bader 19031da177e4SLinus Torvalds md->disk = alloc_disk(1); 19041da177e4SLinus Torvalds if (!md->disk) 19056ed7ade8SMilan Broz goto bad_disk; 19061da177e4SLinus Torvalds 1907316d315bSNikanth Karthikesan atomic_set(&md->pending[0], 0); 1908316d315bSNikanth Karthikesan atomic_set(&md->pending[1], 0); 1909f0b04115SJeff Mahoney init_waitqueue_head(&md->wait); 191053d5914fSMikulas Patocka INIT_WORK(&md->work, dm_wq_work); 1911d0bcb878SKiyoshi Ueda INIT_WORK(&md->barrier_work, dm_rq_barrier_work); 1912f0b04115SJeff Mahoney init_waitqueue_head(&md->eventq); 1913f0b04115SJeff Mahoney 19141da177e4SLinus Torvalds md->disk->major = _major; 19151da177e4SLinus Torvalds md->disk->first_minor = minor; 19161da177e4SLinus Torvalds md->disk->fops = &dm_blk_dops; 19171da177e4SLinus Torvalds md->disk->queue = md->queue; 19181da177e4SLinus Torvalds md->disk->private_data = md; 19191da177e4SLinus Torvalds sprintf(md->disk->disk_name, "dm-%d", minor); 19201da177e4SLinus Torvalds add_disk(md->disk); 19217e51f257SMike Anderson format_dev_t(md->name, MKDEV(_major, minor)); 19221da177e4SLinus Torvalds 1923304f3f6aSMilan Broz md->wq = create_singlethread_workqueue("kdmflush"); 1924304f3f6aSMilan Broz if (!md->wq) 1925304f3f6aSMilan Broz goto bad_thread; 1926304f3f6aSMilan Broz 192732a926daSMikulas Patocka md->bdev = bdget_disk(md->disk, 0); 192832a926daSMikulas Patocka if (!md->bdev) 192932a926daSMikulas Patocka goto bad_bdev; 193032a926daSMikulas Patocka 1931ba61fdd1SJeff Mahoney /* Populate the mapping, nobody knows we exist yet */ 1932f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 1933ba61fdd1SJeff Mahoney old_md = idr_replace(&_minor_idr, md, minor); 1934f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1935ba61fdd1SJeff Mahoney 1936ba61fdd1SJeff Mahoney BUG_ON(old_md != MINOR_ALLOCED); 1937ba61fdd1SJeff Mahoney 19381da177e4SLinus Torvalds return md; 19391da177e4SLinus Torvalds 194032a926daSMikulas Patocka bad_bdev: 194132a926daSMikulas Patocka destroy_workqueue(md->wq); 1942304f3f6aSMilan Broz bad_thread: 194303022c54SZdenek Kabelac del_gendisk(md->disk); 1944304f3f6aSMilan Broz put_disk(md->disk); 19456ed7ade8SMilan Broz bad_disk: 19461312f40eSAl Viro blk_cleanup_queue(md->queue); 19476ed7ade8SMilan Broz bad_queue: 19481da177e4SLinus Torvalds free_minor(minor); 19496ed7ade8SMilan Broz bad_minor: 195010da4f79SJeff Mahoney module_put(THIS_MODULE); 19516ed7ade8SMilan Broz bad_module_get: 19521da177e4SLinus Torvalds kfree(md); 19531da177e4SLinus Torvalds return NULL; 19541da177e4SLinus Torvalds } 19551da177e4SLinus Torvalds 1956ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md); 1957ae9da83fSJun'ichi Nomura 19581da177e4SLinus Torvalds static void free_dev(struct mapped_device *md) 19591da177e4SLinus Torvalds { 1960f331c029STejun Heo int minor = MINOR(disk_devt(md->disk)); 196163d94e48SJun'ichi Nomura 1962ae9da83fSJun'ichi Nomura unlock_fs(md); 1963db8fef4fSMikulas Patocka bdput(md->bdev); 1964304f3f6aSMilan Broz destroy_workqueue(md->wq); 1965e6ee8c0bSKiyoshi Ueda if (md->tio_pool) 19661da177e4SLinus Torvalds mempool_destroy(md->tio_pool); 1967e6ee8c0bSKiyoshi Ueda if (md->io_pool) 19681da177e4SLinus Torvalds mempool_destroy(md->io_pool); 1969e6ee8c0bSKiyoshi Ueda if (md->bs) 19709faf400fSStefan Bader bioset_free(md->bs); 19719c47008dSMartin K. Petersen blk_integrity_unregister(md->disk); 19721da177e4SLinus Torvalds del_gendisk(md->disk); 197363d94e48SJun'ichi Nomura free_minor(minor); 1974fba9f90eSJeff Mahoney 1975fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 1976fba9f90eSJeff Mahoney md->disk->private_data = NULL; 1977fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 1978fba9f90eSJeff Mahoney 19791da177e4SLinus Torvalds put_disk(md->disk); 19801312f40eSAl Viro blk_cleanup_queue(md->queue); 198110da4f79SJeff Mahoney module_put(THIS_MODULE); 19821da177e4SLinus Torvalds kfree(md); 19831da177e4SLinus Torvalds } 19841da177e4SLinus Torvalds 1985e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t) 1986e6ee8c0bSKiyoshi Ueda { 1987e6ee8c0bSKiyoshi Ueda struct dm_md_mempools *p; 1988e6ee8c0bSKiyoshi Ueda 1989e6ee8c0bSKiyoshi Ueda if (md->io_pool && md->tio_pool && md->bs) 1990e6ee8c0bSKiyoshi Ueda /* the md already has necessary mempools */ 1991e6ee8c0bSKiyoshi Ueda goto out; 1992e6ee8c0bSKiyoshi Ueda 1993e6ee8c0bSKiyoshi Ueda p = dm_table_get_md_mempools(t); 1994e6ee8c0bSKiyoshi Ueda BUG_ON(!p || md->io_pool || md->tio_pool || md->bs); 1995e6ee8c0bSKiyoshi Ueda 1996e6ee8c0bSKiyoshi Ueda md->io_pool = p->io_pool; 1997e6ee8c0bSKiyoshi Ueda p->io_pool = NULL; 1998e6ee8c0bSKiyoshi Ueda md->tio_pool = p->tio_pool; 1999e6ee8c0bSKiyoshi Ueda p->tio_pool = NULL; 2000e6ee8c0bSKiyoshi Ueda md->bs = p->bs; 2001e6ee8c0bSKiyoshi Ueda p->bs = NULL; 2002e6ee8c0bSKiyoshi Ueda 2003e6ee8c0bSKiyoshi Ueda out: 2004e6ee8c0bSKiyoshi Ueda /* mempool bind completed, now no need any mempools in the table */ 2005e6ee8c0bSKiyoshi Ueda dm_table_free_md_mempools(t); 2006e6ee8c0bSKiyoshi Ueda } 2007e6ee8c0bSKiyoshi Ueda 20081da177e4SLinus Torvalds /* 20091da177e4SLinus Torvalds * Bind a table to the device. 20101da177e4SLinus Torvalds */ 20111da177e4SLinus Torvalds static void event_callback(void *context) 20121da177e4SLinus Torvalds { 20137a8c3d3bSMike Anderson unsigned long flags; 20147a8c3d3bSMike Anderson LIST_HEAD(uevents); 20151da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) context; 20161da177e4SLinus Torvalds 20177a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 20187a8c3d3bSMike Anderson list_splice_init(&md->uevent_list, &uevents); 20197a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 20207a8c3d3bSMike Anderson 2021ed9e1982STejun Heo dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); 20227a8c3d3bSMike Anderson 20231da177e4SLinus Torvalds atomic_inc(&md->event_nr); 20241da177e4SLinus Torvalds wake_up(&md->eventq); 20251da177e4SLinus Torvalds } 20261da177e4SLinus Torvalds 20274e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size) 20281da177e4SLinus Torvalds { 20294e90188bSAlasdair G Kergon set_capacity(md->disk, size); 20301da177e4SLinus Torvalds 2031db8fef4fSMikulas Patocka mutex_lock(&md->bdev->bd_inode->i_mutex); 2032db8fef4fSMikulas Patocka i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT); 2033db8fef4fSMikulas Patocka mutex_unlock(&md->bdev->bd_inode->i_mutex); 20341da177e4SLinus Torvalds } 20351da177e4SLinus Torvalds 2036754c5fc7SMike Snitzer static int __bind(struct mapped_device *md, struct dm_table *t, 2037754c5fc7SMike Snitzer struct queue_limits *limits) 20381da177e4SLinus Torvalds { 2039165125e1SJens Axboe struct request_queue *q = md->queue; 20401da177e4SLinus Torvalds sector_t size; 2041523d9297SKiyoshi Ueda unsigned long flags; 20421da177e4SLinus Torvalds 20431da177e4SLinus Torvalds size = dm_table_get_size(t); 20443ac51e74SDarrick J. Wong 20453ac51e74SDarrick J. Wong /* 20463ac51e74SDarrick J. Wong * Wipe any geometry if the size of the table changed. 20473ac51e74SDarrick J. Wong */ 20483ac51e74SDarrick J. Wong if (size != get_capacity(md->disk)) 20493ac51e74SDarrick J. Wong memset(&md->geometry, 0, sizeof(md->geometry)); 20503ac51e74SDarrick J. Wong 20514e90188bSAlasdair G Kergon __set_size(md, size); 20521da177e4SLinus Torvalds 2053d5816876SMikulas Patocka if (!size) { 2054d5816876SMikulas Patocka dm_table_destroy(t); 2055d5816876SMikulas Patocka return 0; 2056d5816876SMikulas Patocka } 2057d5816876SMikulas Patocka 2058cf222b37SAlasdair G Kergon dm_table_event_callback(t, event_callback, md); 20592ca3310eSAlasdair G Kergon 2060e6ee8c0bSKiyoshi Ueda /* 2061e6ee8c0bSKiyoshi Ueda * The queue hasn't been stopped yet, if the old table type wasn't 2062e6ee8c0bSKiyoshi Ueda * for request-based during suspension. So stop it to prevent 2063e6ee8c0bSKiyoshi Ueda * I/O mapping before resume. 2064e6ee8c0bSKiyoshi Ueda * This must be done before setting the queue restrictions, 2065e6ee8c0bSKiyoshi Ueda * because request-based dm may be run just after the setting. 2066e6ee8c0bSKiyoshi Ueda */ 2067e6ee8c0bSKiyoshi Ueda if (dm_table_request_based(t) && !blk_queue_stopped(q)) 2068e6ee8c0bSKiyoshi Ueda stop_queue(q); 2069e6ee8c0bSKiyoshi Ueda 2070e6ee8c0bSKiyoshi Ueda __bind_mempools(md, t); 2071e6ee8c0bSKiyoshi Ueda 2072523d9297SKiyoshi Ueda write_lock_irqsave(&md->map_lock, flags); 20732ca3310eSAlasdair G Kergon md->map = t; 2074754c5fc7SMike Snitzer dm_table_set_restrictions(t, q, limits); 2075523d9297SKiyoshi Ueda write_unlock_irqrestore(&md->map_lock, flags); 20762ca3310eSAlasdair G Kergon 20771da177e4SLinus Torvalds return 0; 20781da177e4SLinus Torvalds } 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds static void __unbind(struct mapped_device *md) 20811da177e4SLinus Torvalds { 20821da177e4SLinus Torvalds struct dm_table *map = md->map; 2083523d9297SKiyoshi Ueda unsigned long flags; 20841da177e4SLinus Torvalds 20851da177e4SLinus Torvalds if (!map) 20861da177e4SLinus Torvalds return; 20871da177e4SLinus Torvalds 20881da177e4SLinus Torvalds dm_table_event_callback(map, NULL, NULL); 2089523d9297SKiyoshi Ueda write_lock_irqsave(&md->map_lock, flags); 20901da177e4SLinus Torvalds md->map = NULL; 2091523d9297SKiyoshi Ueda write_unlock_irqrestore(&md->map_lock, flags); 2092d5816876SMikulas Patocka dm_table_destroy(map); 20931da177e4SLinus Torvalds } 20941da177e4SLinus Torvalds 20951da177e4SLinus Torvalds /* 20961da177e4SLinus Torvalds * Constructor for a new device. 20971da177e4SLinus Torvalds */ 20982b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result) 20991da177e4SLinus Torvalds { 21001da177e4SLinus Torvalds struct mapped_device *md; 21011da177e4SLinus Torvalds 21022b06cfffSAlasdair G Kergon md = alloc_dev(minor); 21031da177e4SLinus Torvalds if (!md) 21041da177e4SLinus Torvalds return -ENXIO; 21051da177e4SLinus Torvalds 2106784aae73SMilan Broz dm_sysfs_init(md); 2107784aae73SMilan Broz 21081da177e4SLinus Torvalds *result = md; 21091da177e4SLinus Torvalds return 0; 21101da177e4SLinus Torvalds } 21111da177e4SLinus Torvalds 2112637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev) 21131da177e4SLinus Torvalds { 21141da177e4SLinus Torvalds struct mapped_device *md; 21151da177e4SLinus Torvalds unsigned minor = MINOR(dev); 21161da177e4SLinus Torvalds 21171da177e4SLinus Torvalds if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 21181da177e4SLinus Torvalds return NULL; 21191da177e4SLinus Torvalds 2120f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 21211da177e4SLinus Torvalds 21221da177e4SLinus Torvalds md = idr_find(&_minor_idr, minor); 2123fba9f90eSJeff Mahoney if (md && (md == MINOR_ALLOCED || 2124f331c029STejun Heo (MINOR(disk_devt(dm_disk(md))) != minor) || 2125fba9f90eSJeff Mahoney test_bit(DMF_FREEING, &md->flags))) { 2126637842cfSDavid Teigland md = NULL; 2127fba9f90eSJeff Mahoney goto out; 2128fba9f90eSJeff Mahoney } 21291da177e4SLinus Torvalds 2130fba9f90eSJeff Mahoney out: 2131f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 21321da177e4SLinus Torvalds 2133637842cfSDavid Teigland return md; 2134637842cfSDavid Teigland } 2135637842cfSDavid Teigland 2136d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev) 2137d229a958SDavid Teigland { 2138d229a958SDavid Teigland struct mapped_device *md = dm_find_md(dev); 2139d229a958SDavid Teigland 2140d229a958SDavid Teigland if (md) 2141d229a958SDavid Teigland dm_get(md); 2142d229a958SDavid Teigland 2143d229a958SDavid Teigland return md; 2144d229a958SDavid Teigland } 2145d229a958SDavid Teigland 21469ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md) 2147637842cfSDavid Teigland { 21489ade92a9SAlasdair G Kergon return md->interface_ptr; 21491da177e4SLinus Torvalds } 21501da177e4SLinus Torvalds 21511da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr) 21521da177e4SLinus Torvalds { 21531da177e4SLinus Torvalds md->interface_ptr = ptr; 21541da177e4SLinus Torvalds } 21551da177e4SLinus Torvalds 21561da177e4SLinus Torvalds void dm_get(struct mapped_device *md) 21571da177e4SLinus Torvalds { 21581da177e4SLinus Torvalds atomic_inc(&md->holders); 21591da177e4SLinus Torvalds } 21601da177e4SLinus Torvalds 216172d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md) 216272d94861SAlasdair G Kergon { 216372d94861SAlasdair G Kergon return md->name; 216472d94861SAlasdair G Kergon } 216572d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name); 216672d94861SAlasdair G Kergon 21671da177e4SLinus Torvalds void dm_put(struct mapped_device *md) 21681da177e4SLinus Torvalds { 21691134e5aeSMike Anderson struct dm_table *map; 21701da177e4SLinus Torvalds 2171fba9f90eSJeff Mahoney BUG_ON(test_bit(DMF_FREEING, &md->flags)); 2172fba9f90eSJeff Mahoney 2173f32c10b0SJeff Mahoney if (atomic_dec_and_lock(&md->holders, &_minor_lock)) { 21747c666411SAlasdair G Kergon map = dm_get_live_table(md); 2175f331c029STejun Heo idr_replace(&_minor_idr, MINOR_ALLOCED, 2176f331c029STejun Heo MINOR(disk_devt(dm_disk(md)))); 2177fba9f90eSJeff Mahoney set_bit(DMF_FREEING, &md->flags); 2178f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 2179cf222b37SAlasdair G Kergon if (!dm_suspended(md)) { 21801da177e4SLinus Torvalds dm_table_presuspend_targets(map); 21811da177e4SLinus Torvalds dm_table_postsuspend_targets(map); 21821da177e4SLinus Torvalds } 2183784aae73SMilan Broz dm_sysfs_exit(md); 21841134e5aeSMike Anderson dm_table_put(map); 2185a1b51e98SMikulas Patocka __unbind(md); 21861da177e4SLinus Torvalds free_dev(md); 21871da177e4SLinus Torvalds } 21881da177e4SLinus Torvalds } 218979eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put); 21901da177e4SLinus Torvalds 2191401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible) 219246125c1cSMilan Broz { 219346125c1cSMilan Broz int r = 0; 2194b44ebeb0SMikulas Patocka DECLARE_WAITQUEUE(wait, current); 2195b44ebeb0SMikulas Patocka 2196b44ebeb0SMikulas Patocka dm_unplug_all(md->queue); 2197b44ebeb0SMikulas Patocka 2198b44ebeb0SMikulas Patocka add_wait_queue(&md->wait, &wait); 219946125c1cSMilan Broz 220046125c1cSMilan Broz while (1) { 2201401600dfSMikulas Patocka set_current_state(interruptible); 220246125c1cSMilan Broz 220346125c1cSMilan Broz smp_mb(); 2204b4324feeSKiyoshi Ueda if (!md_in_flight(md)) 220546125c1cSMilan Broz break; 220646125c1cSMilan Broz 2207401600dfSMikulas Patocka if (interruptible == TASK_INTERRUPTIBLE && 2208401600dfSMikulas Patocka signal_pending(current)) { 220946125c1cSMilan Broz r = -EINTR; 221046125c1cSMilan Broz break; 221146125c1cSMilan Broz } 221246125c1cSMilan Broz 221346125c1cSMilan Broz io_schedule(); 221446125c1cSMilan Broz } 221546125c1cSMilan Broz set_current_state(TASK_RUNNING); 221646125c1cSMilan Broz 2217b44ebeb0SMikulas Patocka remove_wait_queue(&md->wait, &wait); 2218b44ebeb0SMikulas Patocka 221946125c1cSMilan Broz return r; 222046125c1cSMilan Broz } 222146125c1cSMilan Broz 2222531fe963SMikulas Patocka static void dm_flush(struct mapped_device *md) 2223af7e466aSMikulas Patocka { 2224af7e466aSMikulas Patocka dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); 222552b1fd5aSMikulas Patocka 222652b1fd5aSMikulas Patocka bio_init(&md->barrier_bio); 222752b1fd5aSMikulas Patocka md->barrier_bio.bi_bdev = md->bdev; 222852b1fd5aSMikulas Patocka md->barrier_bio.bi_rw = WRITE_BARRIER; 222952b1fd5aSMikulas Patocka __split_and_process_bio(md, &md->barrier_bio); 223052b1fd5aSMikulas Patocka 223152b1fd5aSMikulas Patocka dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); 2232af7e466aSMikulas Patocka } 2233af7e466aSMikulas Patocka 2234af7e466aSMikulas Patocka static void process_barrier(struct mapped_device *md, struct bio *bio) 2235af7e466aSMikulas Patocka { 22365aa2781dSMikulas Patocka md->barrier_error = 0; 22375aa2781dSMikulas Patocka 2238531fe963SMikulas Patocka dm_flush(md); 2239af7e466aSMikulas Patocka 22405aa2781dSMikulas Patocka if (!bio_empty_barrier(bio)) { 2241af7e466aSMikulas Patocka __split_and_process_bio(md, bio); 2242531fe963SMikulas Patocka dm_flush(md); 22435aa2781dSMikulas Patocka } 2244af7e466aSMikulas Patocka 2245af7e466aSMikulas Patocka if (md->barrier_error != DM_ENDIO_REQUEUE) 2246531fe963SMikulas Patocka bio_endio(bio, md->barrier_error); 22472761e95fSMikulas Patocka else { 22482761e95fSMikulas Patocka spin_lock_irq(&md->deferred_lock); 22492761e95fSMikulas Patocka bio_list_add_head(&md->deferred, bio); 22502761e95fSMikulas Patocka spin_unlock_irq(&md->deferred_lock); 22512761e95fSMikulas Patocka } 2252af7e466aSMikulas Patocka } 2253af7e466aSMikulas Patocka 22541da177e4SLinus Torvalds /* 22551da177e4SLinus Torvalds * Process the deferred bios 22561da177e4SLinus Torvalds */ 2257ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work) 22581da177e4SLinus Torvalds { 2259ef208587SMikulas Patocka struct mapped_device *md = container_of(work, struct mapped_device, 2260ef208587SMikulas Patocka work); 22616d6f10dfSMilan Broz struct bio *c; 22621da177e4SLinus Torvalds 2263ef208587SMikulas Patocka down_write(&md->io_lock); 2264ef208587SMikulas Patocka 22653b00b203SMikulas Patocka while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 2266022c2611SMikulas Patocka spin_lock_irq(&md->deferred_lock); 2267022c2611SMikulas Patocka c = bio_list_pop(&md->deferred); 2268022c2611SMikulas Patocka spin_unlock_irq(&md->deferred_lock); 2269022c2611SMikulas Patocka 2270df12ee99SAlasdair G Kergon if (!c) { 22711eb787ecSAlasdair G Kergon clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags); 2272df12ee99SAlasdair G Kergon break; 2273022c2611SMikulas Patocka } 227473d410c0SMilan Broz 22753b00b203SMikulas Patocka up_write(&md->io_lock); 22763b00b203SMikulas Patocka 2277e6ee8c0bSKiyoshi Ueda if (dm_request_based(md)) 2278e6ee8c0bSKiyoshi Ueda generic_make_request(c); 2279e6ee8c0bSKiyoshi Ueda else { 22801f98a13fSJens Axboe if (bio_rw_flagged(c, BIO_RW_BARRIER)) 2281af7e466aSMikulas Patocka process_barrier(md, c); 2282af7e466aSMikulas Patocka else 2283df12ee99SAlasdair G Kergon __split_and_process_bio(md, c); 2284e6ee8c0bSKiyoshi Ueda } 22853b00b203SMikulas Patocka 22863b00b203SMikulas Patocka down_write(&md->io_lock); 2287df12ee99SAlasdair G Kergon } 2288ef208587SMikulas Patocka 2289ef208587SMikulas Patocka up_write(&md->io_lock); 22901da177e4SLinus Torvalds } 22911da177e4SLinus Torvalds 22929a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md) 2293304f3f6aSMilan Broz { 22943b00b203SMikulas Patocka clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 22953b00b203SMikulas Patocka smp_mb__after_clear_bit(); 229653d5914fSMikulas Patocka queue_work(md->wq, &md->work); 2297304f3f6aSMilan Broz } 2298304f3f6aSMilan Broz 2299d0bcb878SKiyoshi Ueda static void dm_rq_set_flush_nr(struct request *clone, unsigned flush_nr) 2300d0bcb878SKiyoshi Ueda { 2301d0bcb878SKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 2302d0bcb878SKiyoshi Ueda 2303d0bcb878SKiyoshi Ueda tio->info.flush_request = flush_nr; 2304d0bcb878SKiyoshi Ueda } 2305d0bcb878SKiyoshi Ueda 2306d0bcb878SKiyoshi Ueda /* Issue barrier requests to targets and wait for their completion. */ 2307d0bcb878SKiyoshi Ueda static int dm_rq_barrier(struct mapped_device *md) 2308d0bcb878SKiyoshi Ueda { 2309d0bcb878SKiyoshi Ueda int i, j; 23107c666411SAlasdair G Kergon struct dm_table *map = dm_get_live_table(md); 2311d0bcb878SKiyoshi Ueda unsigned num_targets = dm_table_get_num_targets(map); 2312d0bcb878SKiyoshi Ueda struct dm_target *ti; 2313d0bcb878SKiyoshi Ueda struct request *clone; 2314d0bcb878SKiyoshi Ueda 2315d0bcb878SKiyoshi Ueda md->barrier_error = 0; 2316d0bcb878SKiyoshi Ueda 2317d0bcb878SKiyoshi Ueda for (i = 0; i < num_targets; i++) { 2318d0bcb878SKiyoshi Ueda ti = dm_table_get_target(map, i); 2319d0bcb878SKiyoshi Ueda for (j = 0; j < ti->num_flush_requests; j++) { 2320d0bcb878SKiyoshi Ueda clone = clone_rq(md->flush_request, md, GFP_NOIO); 2321d0bcb878SKiyoshi Ueda dm_rq_set_flush_nr(clone, j); 2322d0bcb878SKiyoshi Ueda atomic_inc(&md->pending[rq_data_dir(clone)]); 2323d0bcb878SKiyoshi Ueda map_request(ti, clone, md); 2324d0bcb878SKiyoshi Ueda } 2325d0bcb878SKiyoshi Ueda } 2326d0bcb878SKiyoshi Ueda 2327d0bcb878SKiyoshi Ueda dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); 2328d0bcb878SKiyoshi Ueda dm_table_put(map); 2329d0bcb878SKiyoshi Ueda 2330d0bcb878SKiyoshi Ueda return md->barrier_error; 2331d0bcb878SKiyoshi Ueda } 2332d0bcb878SKiyoshi Ueda 2333d0bcb878SKiyoshi Ueda static void dm_rq_barrier_work(struct work_struct *work) 2334d0bcb878SKiyoshi Ueda { 2335d0bcb878SKiyoshi Ueda int error; 2336d0bcb878SKiyoshi Ueda struct mapped_device *md = container_of(work, struct mapped_device, 2337d0bcb878SKiyoshi Ueda barrier_work); 2338d0bcb878SKiyoshi Ueda struct request_queue *q = md->queue; 2339d0bcb878SKiyoshi Ueda struct request *rq; 2340d0bcb878SKiyoshi Ueda unsigned long flags; 2341d0bcb878SKiyoshi Ueda 2342d0bcb878SKiyoshi Ueda /* 2343d0bcb878SKiyoshi Ueda * Hold the md reference here and leave it at the last part so that 2344d0bcb878SKiyoshi Ueda * the md can't be deleted by device opener when the barrier request 2345d0bcb878SKiyoshi Ueda * completes. 2346d0bcb878SKiyoshi Ueda */ 2347d0bcb878SKiyoshi Ueda dm_get(md); 2348d0bcb878SKiyoshi Ueda 2349d0bcb878SKiyoshi Ueda error = dm_rq_barrier(md); 2350d0bcb878SKiyoshi Ueda 2351d0bcb878SKiyoshi Ueda rq = md->flush_request; 2352d0bcb878SKiyoshi Ueda md->flush_request = NULL; 2353d0bcb878SKiyoshi Ueda 2354d0bcb878SKiyoshi Ueda if (error == DM_ENDIO_REQUEUE) { 2355d0bcb878SKiyoshi Ueda spin_lock_irqsave(q->queue_lock, flags); 2356d0bcb878SKiyoshi Ueda blk_requeue_request(q, rq); 2357d0bcb878SKiyoshi Ueda spin_unlock_irqrestore(q->queue_lock, flags); 2358d0bcb878SKiyoshi Ueda } else 2359d0bcb878SKiyoshi Ueda blk_end_request_all(rq, error); 2360d0bcb878SKiyoshi Ueda 2361d0bcb878SKiyoshi Ueda blk_run_queue(q); 2362d0bcb878SKiyoshi Ueda 2363d0bcb878SKiyoshi Ueda dm_put(md); 2364d0bcb878SKiyoshi Ueda } 2365d0bcb878SKiyoshi Ueda 23661da177e4SLinus Torvalds /* 23671da177e4SLinus Torvalds * Swap in a new table (destroying old one). 23681da177e4SLinus Torvalds */ 23691da177e4SLinus Torvalds int dm_swap_table(struct mapped_device *md, struct dm_table *table) 23701da177e4SLinus Torvalds { 2371754c5fc7SMike Snitzer struct queue_limits limits; 237293c534aeSAlasdair G Kergon int r = -EINVAL; 23731da177e4SLinus Torvalds 2374e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 23751da177e4SLinus Torvalds 23761da177e4SLinus Torvalds /* device must be suspended */ 2377cf222b37SAlasdair G Kergon if (!dm_suspended(md)) 237893c534aeSAlasdair G Kergon goto out; 23791da177e4SLinus Torvalds 2380754c5fc7SMike Snitzer r = dm_calculate_queue_limits(table, &limits); 2381754c5fc7SMike Snitzer if (r) 2382754c5fc7SMike Snitzer goto out; 2383754c5fc7SMike Snitzer 2384e6ee8c0bSKiyoshi Ueda /* cannot change the device type, once a table is bound */ 2385e6ee8c0bSKiyoshi Ueda if (md->map && 2386e6ee8c0bSKiyoshi Ueda (dm_table_get_type(md->map) != dm_table_get_type(table))) { 2387e6ee8c0bSKiyoshi Ueda DMWARN("can't change the device type after a table is bound"); 2388e6ee8c0bSKiyoshi Ueda goto out; 2389e6ee8c0bSKiyoshi Ueda } 2390e6ee8c0bSKiyoshi Ueda 23911da177e4SLinus Torvalds __unbind(md); 2392754c5fc7SMike Snitzer r = __bind(md, table, &limits); 23931da177e4SLinus Torvalds 239493c534aeSAlasdair G Kergon out: 2395e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 239693c534aeSAlasdair G Kergon return r; 23971da177e4SLinus Torvalds } 23981da177e4SLinus Torvalds 23991da177e4SLinus Torvalds /* 24001da177e4SLinus Torvalds * Functions to lock and unlock any filesystem running on the 24011da177e4SLinus Torvalds * device. 24021da177e4SLinus Torvalds */ 24032ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md) 24041da177e4SLinus Torvalds { 2405e39e2e95SAlasdair G Kergon int r; 24061da177e4SLinus Torvalds 24071da177e4SLinus Torvalds WARN_ON(md->frozen_sb); 2408dfbe03f6SAlasdair G Kergon 2409db8fef4fSMikulas Patocka md->frozen_sb = freeze_bdev(md->bdev); 2410dfbe03f6SAlasdair G Kergon if (IS_ERR(md->frozen_sb)) { 2411cf222b37SAlasdair G Kergon r = PTR_ERR(md->frozen_sb); 2412e39e2e95SAlasdair G Kergon md->frozen_sb = NULL; 2413e39e2e95SAlasdair G Kergon return r; 2414dfbe03f6SAlasdair G Kergon } 2415dfbe03f6SAlasdair G Kergon 2416aa8d7c2fSAlasdair G Kergon set_bit(DMF_FROZEN, &md->flags); 2417aa8d7c2fSAlasdair G Kergon 24181da177e4SLinus Torvalds return 0; 24191da177e4SLinus Torvalds } 24201da177e4SLinus Torvalds 24212ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md) 24221da177e4SLinus Torvalds { 2423aa8d7c2fSAlasdair G Kergon if (!test_bit(DMF_FROZEN, &md->flags)) 2424aa8d7c2fSAlasdair G Kergon return; 2425aa8d7c2fSAlasdair G Kergon 2426db8fef4fSMikulas Patocka thaw_bdev(md->bdev, md->frozen_sb); 24271da177e4SLinus Torvalds md->frozen_sb = NULL; 2428aa8d7c2fSAlasdair G Kergon clear_bit(DMF_FROZEN, &md->flags); 24291da177e4SLinus Torvalds } 24301da177e4SLinus Torvalds 24311da177e4SLinus Torvalds /* 24321da177e4SLinus Torvalds * We need to be able to change a mapping table under a mounted 24331da177e4SLinus Torvalds * filesystem. For example we might want to move some data in 24341da177e4SLinus Torvalds * the background. Before the table can be swapped with 24351da177e4SLinus Torvalds * dm_bind_table, dm_suspend must be called to flush any in 24361da177e4SLinus Torvalds * flight bios and ensure that any further io gets deferred. 24371da177e4SLinus Torvalds */ 2438cec47e3dSKiyoshi Ueda /* 2439cec47e3dSKiyoshi Ueda * Suspend mechanism in request-based dm. 2440cec47e3dSKiyoshi Ueda * 24419f518b27SKiyoshi Ueda * 1. Flush all I/Os by lock_fs() if needed. 24429f518b27SKiyoshi Ueda * 2. Stop dispatching any I/O by stopping the request_queue. 24439f518b27SKiyoshi Ueda * 3. Wait for all in-flight I/Os to be completed or requeued. 2444cec47e3dSKiyoshi Ueda * 24459f518b27SKiyoshi Ueda * To abort suspend, start the request_queue. 2446cec47e3dSKiyoshi Ueda */ 2447a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags) 24481da177e4SLinus Torvalds { 24492ca3310eSAlasdair G Kergon struct dm_table *map = NULL; 245046125c1cSMilan Broz int r = 0; 2451a3d77d35SKiyoshi Ueda int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0; 24522e93ccc1SKiyoshi Ueda int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0; 24531da177e4SLinus Torvalds 2454e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 24552ca3310eSAlasdair G Kergon 245673d410c0SMilan Broz if (dm_suspended(md)) { 245773d410c0SMilan Broz r = -EINVAL; 2458d287483dSAlasdair G Kergon goto out_unlock; 245973d410c0SMilan Broz } 24601da177e4SLinus Torvalds 24617c666411SAlasdair G Kergon map = dm_get_live_table(md); 2462cf222b37SAlasdair G Kergon 24632e93ccc1SKiyoshi Ueda /* 24642e93ccc1SKiyoshi Ueda * DMF_NOFLUSH_SUSPENDING must be set before presuspend. 24652e93ccc1SKiyoshi Ueda * This flag is cleared before dm_suspend returns. 24662e93ccc1SKiyoshi Ueda */ 24672e93ccc1SKiyoshi Ueda if (noflush) 24682e93ccc1SKiyoshi Ueda set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 24692e93ccc1SKiyoshi Ueda 2470436d4108SAlasdair G Kergon /* This does not get reverted if there's an error later. */ 24711da177e4SLinus Torvalds dm_table_presuspend_targets(map); 24721da177e4SLinus Torvalds 24732e93ccc1SKiyoshi Ueda /* 24749f518b27SKiyoshi Ueda * Flush I/O to the device. 24759f518b27SKiyoshi Ueda * Any I/O submitted after lock_fs() may not be flushed. 24769f518b27SKiyoshi Ueda * noflush takes precedence over do_lockfs. 24779f518b27SKiyoshi Ueda * (lock_fs() flushes I/Os and waits for them to complete.) 24782e93ccc1SKiyoshi Ueda */ 247932a926daSMikulas Patocka if (!noflush && do_lockfs) { 24802ca3310eSAlasdair G Kergon r = lock_fs(md); 24812ca3310eSAlasdair G Kergon if (r) 24822ca3310eSAlasdair G Kergon goto out; 2483aa8d7c2fSAlasdair G Kergon } 24841da177e4SLinus Torvalds 24851da177e4SLinus Torvalds /* 24863b00b203SMikulas Patocka * Here we must make sure that no processes are submitting requests 24873b00b203SMikulas Patocka * to target drivers i.e. no one may be executing 24883b00b203SMikulas Patocka * __split_and_process_bio. This is called from dm_request and 24893b00b203SMikulas Patocka * dm_wq_work. 24903b00b203SMikulas Patocka * 24913b00b203SMikulas Patocka * To get all processes out of __split_and_process_bio in dm_request, 24923b00b203SMikulas Patocka * we take the write lock. To prevent any process from reentering 24933b00b203SMikulas Patocka * __split_and_process_bio from dm_request, we set 24943b00b203SMikulas Patocka * DMF_QUEUE_IO_TO_THREAD. 24953b00b203SMikulas Patocka * 24963b00b203SMikulas Patocka * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND 24973b00b203SMikulas Patocka * and call flush_workqueue(md->wq). flush_workqueue will wait until 24983b00b203SMikulas Patocka * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any 24993b00b203SMikulas Patocka * further calls to __split_and_process_bio from dm_wq_work. 25001da177e4SLinus Torvalds */ 25012ca3310eSAlasdair G Kergon down_write(&md->io_lock); 25021eb787ecSAlasdair G Kergon set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 25031eb787ecSAlasdair G Kergon set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags); 25042ca3310eSAlasdair G Kergon up_write(&md->io_lock); 25051da177e4SLinus Torvalds 2506d0bcb878SKiyoshi Ueda /* 2507d0bcb878SKiyoshi Ueda * Request-based dm uses md->wq for barrier (dm_rq_barrier_work) which 2508d0bcb878SKiyoshi Ueda * can be kicked until md->queue is stopped. So stop md->queue before 2509d0bcb878SKiyoshi Ueda * flushing md->wq. 2510d0bcb878SKiyoshi Ueda */ 2511cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 25129f518b27SKiyoshi Ueda stop_queue(md->queue); 2513cec47e3dSKiyoshi Ueda 2514d0bcb878SKiyoshi Ueda flush_workqueue(md->wq); 2515d0bcb878SKiyoshi Ueda 25161da177e4SLinus Torvalds /* 25173b00b203SMikulas Patocka * At this point no more requests are entering target request routines. 25183b00b203SMikulas Patocka * We call dm_wait_for_completion to wait for all existing requests 25193b00b203SMikulas Patocka * to finish. 25201da177e4SLinus Torvalds */ 2521401600dfSMikulas Patocka r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE); 25221da177e4SLinus Torvalds 25232ca3310eSAlasdair G Kergon down_write(&md->io_lock); 25246d6f10dfSMilan Broz if (noflush) 2525022c2611SMikulas Patocka clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 252694d6351eSMilan Broz up_write(&md->io_lock); 25272e93ccc1SKiyoshi Ueda 25281da177e4SLinus Torvalds /* were we interrupted ? */ 252946125c1cSMilan Broz if (r < 0) { 25309a1fb464SMikulas Patocka dm_queue_flush(md); 253173d410c0SMilan Broz 2532cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 25339f518b27SKiyoshi Ueda start_queue(md->queue); 2534cec47e3dSKiyoshi Ueda 25352ca3310eSAlasdair G Kergon unlock_fs(md); 25362e93ccc1SKiyoshi Ueda goto out; /* pushback list is already flushed, so skip flush */ 25372ca3310eSAlasdair G Kergon } 25382ca3310eSAlasdair G Kergon 25393b00b203SMikulas Patocka /* 25403b00b203SMikulas Patocka * If dm_wait_for_completion returned 0, the device is completely 25413b00b203SMikulas Patocka * quiescent now. There is no request-processing activity. All new 25423b00b203SMikulas Patocka * requests are being added to md->deferred list. 25433b00b203SMikulas Patocka */ 25443b00b203SMikulas Patocka 25452ca3310eSAlasdair G Kergon dm_table_postsuspend_targets(map); 25461da177e4SLinus Torvalds 25471da177e4SLinus Torvalds set_bit(DMF_SUSPENDED, &md->flags); 25481da177e4SLinus Torvalds 25492ca3310eSAlasdair G Kergon out: 25501da177e4SLinus Torvalds dm_table_put(map); 2551d287483dSAlasdair G Kergon 2552d287483dSAlasdair G Kergon out_unlock: 2553e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 2554cf222b37SAlasdair G Kergon return r; 25551da177e4SLinus Torvalds } 25561da177e4SLinus Torvalds 25571da177e4SLinus Torvalds int dm_resume(struct mapped_device *md) 25581da177e4SLinus Torvalds { 2559cf222b37SAlasdair G Kergon int r = -EINVAL; 2560cf222b37SAlasdair G Kergon struct dm_table *map = NULL; 25611da177e4SLinus Torvalds 2562e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 25632ca3310eSAlasdair G Kergon if (!dm_suspended(md)) 2564cf222b37SAlasdair G Kergon goto out; 2565cf222b37SAlasdair G Kergon 25667c666411SAlasdair G Kergon map = dm_get_live_table(md); 25672ca3310eSAlasdair G Kergon if (!map || !dm_table_get_size(map)) 2568cf222b37SAlasdair G Kergon goto out; 25691da177e4SLinus Torvalds 25708757b776SMilan Broz r = dm_table_resume_targets(map); 25718757b776SMilan Broz if (r) 25728757b776SMilan Broz goto out; 25732ca3310eSAlasdair G Kergon 25749a1fb464SMikulas Patocka dm_queue_flush(md); 25752ca3310eSAlasdair G Kergon 2576cec47e3dSKiyoshi Ueda /* 2577cec47e3dSKiyoshi Ueda * Flushing deferred I/Os must be done after targets are resumed 2578cec47e3dSKiyoshi Ueda * so that mapping of targets can work correctly. 2579cec47e3dSKiyoshi Ueda * Request-based dm is queueing the deferred I/Os in its request_queue. 2580cec47e3dSKiyoshi Ueda */ 2581cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 2582cec47e3dSKiyoshi Ueda start_queue(md->queue); 2583cec47e3dSKiyoshi Ueda 25842ca3310eSAlasdair G Kergon unlock_fs(md); 25852ca3310eSAlasdair G Kergon 25862ca3310eSAlasdair G Kergon clear_bit(DMF_SUSPENDED, &md->flags); 25872ca3310eSAlasdair G Kergon 25881da177e4SLinus Torvalds dm_table_unplug_all(map); 2589cf222b37SAlasdair G Kergon r = 0; 2590cf222b37SAlasdair G Kergon out: 2591cf222b37SAlasdair G Kergon dm_table_put(map); 2592e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 25932ca3310eSAlasdair G Kergon 2594cf222b37SAlasdair G Kergon return r; 25951da177e4SLinus Torvalds } 25961da177e4SLinus Torvalds 25971da177e4SLinus Torvalds /*----------------------------------------------------------------- 25981da177e4SLinus Torvalds * Event notification. 25991da177e4SLinus Torvalds *---------------------------------------------------------------*/ 260060935eb2SMilan Broz void dm_kobject_uevent(struct mapped_device *md, enum kobject_action action, 260160935eb2SMilan Broz unsigned cookie) 260269267a30SAlasdair G Kergon { 260360935eb2SMilan Broz char udev_cookie[DM_COOKIE_LENGTH]; 260460935eb2SMilan Broz char *envp[] = { udev_cookie, NULL }; 260560935eb2SMilan Broz 260660935eb2SMilan Broz if (!cookie) 260760935eb2SMilan Broz kobject_uevent(&disk_to_dev(md->disk)->kobj, action); 260860935eb2SMilan Broz else { 260960935eb2SMilan Broz snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u", 261060935eb2SMilan Broz DM_COOKIE_ENV_VAR_NAME, cookie); 261160935eb2SMilan Broz kobject_uevent_env(&disk_to_dev(md->disk)->kobj, action, envp); 261260935eb2SMilan Broz } 261369267a30SAlasdair G Kergon } 261469267a30SAlasdair G Kergon 26157a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md) 26167a8c3d3bSMike Anderson { 26177a8c3d3bSMike Anderson return atomic_add_return(1, &md->uevent_seq); 26187a8c3d3bSMike Anderson } 26197a8c3d3bSMike Anderson 26201da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md) 26211da177e4SLinus Torvalds { 26221da177e4SLinus Torvalds return atomic_read(&md->event_nr); 26231da177e4SLinus Torvalds } 26241da177e4SLinus Torvalds 26251da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr) 26261da177e4SLinus Torvalds { 26271da177e4SLinus Torvalds return wait_event_interruptible(md->eventq, 26281da177e4SLinus Torvalds (event_nr != atomic_read(&md->event_nr))); 26291da177e4SLinus Torvalds } 26301da177e4SLinus Torvalds 26317a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist) 26327a8c3d3bSMike Anderson { 26337a8c3d3bSMike Anderson unsigned long flags; 26347a8c3d3bSMike Anderson 26357a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 26367a8c3d3bSMike Anderson list_add(elist, &md->uevent_list); 26377a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 26387a8c3d3bSMike Anderson } 26397a8c3d3bSMike Anderson 26401da177e4SLinus Torvalds /* 26411da177e4SLinus Torvalds * The gendisk is only valid as long as you have a reference 26421da177e4SLinus Torvalds * count on 'md'. 26431da177e4SLinus Torvalds */ 26441da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md) 26451da177e4SLinus Torvalds { 26461da177e4SLinus Torvalds return md->disk; 26471da177e4SLinus Torvalds } 26481da177e4SLinus Torvalds 2649784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md) 2650784aae73SMilan Broz { 2651784aae73SMilan Broz return &md->kobj; 2652784aae73SMilan Broz } 2653784aae73SMilan Broz 2654784aae73SMilan Broz /* 2655784aae73SMilan Broz * struct mapped_device should not be exported outside of dm.c 2656784aae73SMilan Broz * so use this check to verify that kobj is part of md structure 2657784aae73SMilan Broz */ 2658784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj) 2659784aae73SMilan Broz { 2660784aae73SMilan Broz struct mapped_device *md; 2661784aae73SMilan Broz 2662784aae73SMilan Broz md = container_of(kobj, struct mapped_device, kobj); 2663784aae73SMilan Broz if (&md->kobj != kobj) 2664784aae73SMilan Broz return NULL; 2665784aae73SMilan Broz 26664d89b7b4SMilan Broz if (test_bit(DMF_FREEING, &md->flags) || 2667*432a212cSMike Anderson dm_deleting_md(md)) 26684d89b7b4SMilan Broz return NULL; 26694d89b7b4SMilan Broz 2670784aae73SMilan Broz dm_get(md); 2671784aae73SMilan Broz return md; 2672784aae73SMilan Broz } 2673784aae73SMilan Broz 26741da177e4SLinus Torvalds int dm_suspended(struct mapped_device *md) 26751da177e4SLinus Torvalds { 26761da177e4SLinus Torvalds return test_bit(DMF_SUSPENDED, &md->flags); 26771da177e4SLinus Torvalds } 26781da177e4SLinus Torvalds 26792e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti) 26802e93ccc1SKiyoshi Ueda { 26812e93ccc1SKiyoshi Ueda struct mapped_device *md = dm_table_get_md(ti->table); 26822e93ccc1SKiyoshi Ueda int r = __noflush_suspending(md); 26832e93ccc1SKiyoshi Ueda 26842e93ccc1SKiyoshi Ueda dm_put(md); 26852e93ccc1SKiyoshi Ueda 26862e93ccc1SKiyoshi Ueda return r; 26872e93ccc1SKiyoshi Ueda } 26882e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending); 26892e93ccc1SKiyoshi Ueda 2690e6ee8c0bSKiyoshi Ueda struct dm_md_mempools *dm_alloc_md_mempools(unsigned type) 2691e6ee8c0bSKiyoshi Ueda { 2692e6ee8c0bSKiyoshi Ueda struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL); 2693e6ee8c0bSKiyoshi Ueda 2694e6ee8c0bSKiyoshi Ueda if (!pools) 2695e6ee8c0bSKiyoshi Ueda return NULL; 2696e6ee8c0bSKiyoshi Ueda 2697e6ee8c0bSKiyoshi Ueda pools->io_pool = (type == DM_TYPE_BIO_BASED) ? 2698e6ee8c0bSKiyoshi Ueda mempool_create_slab_pool(MIN_IOS, _io_cache) : 2699e6ee8c0bSKiyoshi Ueda mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache); 2700e6ee8c0bSKiyoshi Ueda if (!pools->io_pool) 2701e6ee8c0bSKiyoshi Ueda goto free_pools_and_out; 2702e6ee8c0bSKiyoshi Ueda 2703e6ee8c0bSKiyoshi Ueda pools->tio_pool = (type == DM_TYPE_BIO_BASED) ? 2704e6ee8c0bSKiyoshi Ueda mempool_create_slab_pool(MIN_IOS, _tio_cache) : 2705e6ee8c0bSKiyoshi Ueda mempool_create_slab_pool(MIN_IOS, _rq_tio_cache); 2706e6ee8c0bSKiyoshi Ueda if (!pools->tio_pool) 2707e6ee8c0bSKiyoshi Ueda goto free_io_pool_and_out; 2708e6ee8c0bSKiyoshi Ueda 2709e6ee8c0bSKiyoshi Ueda pools->bs = (type == DM_TYPE_BIO_BASED) ? 2710e6ee8c0bSKiyoshi Ueda bioset_create(16, 0) : bioset_create(MIN_IOS, 0); 2711e6ee8c0bSKiyoshi Ueda if (!pools->bs) 2712e6ee8c0bSKiyoshi Ueda goto free_tio_pool_and_out; 2713e6ee8c0bSKiyoshi Ueda 2714e6ee8c0bSKiyoshi Ueda return pools; 2715e6ee8c0bSKiyoshi Ueda 2716e6ee8c0bSKiyoshi Ueda free_tio_pool_and_out: 2717e6ee8c0bSKiyoshi Ueda mempool_destroy(pools->tio_pool); 2718e6ee8c0bSKiyoshi Ueda 2719e6ee8c0bSKiyoshi Ueda free_io_pool_and_out: 2720e6ee8c0bSKiyoshi Ueda mempool_destroy(pools->io_pool); 2721e6ee8c0bSKiyoshi Ueda 2722e6ee8c0bSKiyoshi Ueda free_pools_and_out: 2723e6ee8c0bSKiyoshi Ueda kfree(pools); 2724e6ee8c0bSKiyoshi Ueda 2725e6ee8c0bSKiyoshi Ueda return NULL; 2726e6ee8c0bSKiyoshi Ueda } 2727e6ee8c0bSKiyoshi Ueda 2728e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools) 2729e6ee8c0bSKiyoshi Ueda { 2730e6ee8c0bSKiyoshi Ueda if (!pools) 2731e6ee8c0bSKiyoshi Ueda return; 2732e6ee8c0bSKiyoshi Ueda 2733e6ee8c0bSKiyoshi Ueda if (pools->io_pool) 2734e6ee8c0bSKiyoshi Ueda mempool_destroy(pools->io_pool); 2735e6ee8c0bSKiyoshi Ueda 2736e6ee8c0bSKiyoshi Ueda if (pools->tio_pool) 2737e6ee8c0bSKiyoshi Ueda mempool_destroy(pools->tio_pool); 2738e6ee8c0bSKiyoshi Ueda 2739e6ee8c0bSKiyoshi Ueda if (pools->bs) 2740e6ee8c0bSKiyoshi Ueda bioset_free(pools->bs); 2741e6ee8c0bSKiyoshi Ueda 2742e6ee8c0bSKiyoshi Ueda kfree(pools); 2743e6ee8c0bSKiyoshi Ueda } 2744e6ee8c0bSKiyoshi Ueda 274583d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = { 27461da177e4SLinus Torvalds .open = dm_blk_open, 27471da177e4SLinus Torvalds .release = dm_blk_close, 2748aa129a22SMilan Broz .ioctl = dm_blk_ioctl, 27493ac51e74SDarrick J. Wong .getgeo = dm_blk_getgeo, 27501da177e4SLinus Torvalds .owner = THIS_MODULE 27511da177e4SLinus Torvalds }; 27521da177e4SLinus Torvalds 27531da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo); 27541da177e4SLinus Torvalds 27551da177e4SLinus Torvalds /* 27561da177e4SLinus Torvalds * module hooks 27571da177e4SLinus Torvalds */ 27581da177e4SLinus Torvalds module_init(dm_init); 27591da177e4SLinus Torvalds module_exit(dm_exit); 27601da177e4SLinus Torvalds 27611da177e4SLinus Torvalds module_param(major, uint, 0); 27621da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper"); 27631da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver"); 27641da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 27651da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 2766