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/mempool.h> 181da177e4SLinus Torvalds #include <linux/slab.h> 191da177e4SLinus Torvalds #include <linux/idr.h> 203ac51e74SDarrick J. Wong #include <linux/hdreg.h> 213f77316dSKiyoshi Ueda #include <linux/delay.h> 2255782138SLi Zefan 2355782138SLi Zefan #include <trace/events/block.h> 241da177e4SLinus Torvalds 2572d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core" 2672d94861SAlasdair G Kergon 2771a16736SNamhyung Kim #ifdef CONFIG_PRINTK 2871a16736SNamhyung Kim /* 2971a16736SNamhyung Kim * ratelimit state to be used in DMXXX_LIMIT(). 3071a16736SNamhyung Kim */ 3171a16736SNamhyung Kim DEFINE_RATELIMIT_STATE(dm_ratelimit_state, 3271a16736SNamhyung Kim DEFAULT_RATELIMIT_INTERVAL, 3371a16736SNamhyung Kim DEFAULT_RATELIMIT_BURST); 3471a16736SNamhyung Kim EXPORT_SYMBOL(dm_ratelimit_state); 3571a16736SNamhyung Kim #endif 3671a16736SNamhyung Kim 3760935eb2SMilan Broz /* 3860935eb2SMilan Broz * Cookies are numeric values sent with CHANGE and REMOVE 3960935eb2SMilan Broz * uevents while resuming, removing or renaming the device. 4060935eb2SMilan Broz */ 4160935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE" 4260935eb2SMilan Broz #define DM_COOKIE_LENGTH 24 4360935eb2SMilan Broz 441da177e4SLinus Torvalds static const char *_name = DM_NAME; 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds static unsigned int major = 0; 471da177e4SLinus Torvalds static unsigned int _major = 0; 481da177e4SLinus Torvalds 49d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr); 50d15b774cSAlasdair G Kergon 51f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock); 521da177e4SLinus Torvalds /* 538fbf26adSKiyoshi Ueda * For bio-based dm. 541da177e4SLinus Torvalds * One of these is allocated per bio. 551da177e4SLinus Torvalds */ 561da177e4SLinus Torvalds struct dm_io { 571da177e4SLinus Torvalds struct mapped_device *md; 581da177e4SLinus Torvalds int error; 591da177e4SLinus Torvalds atomic_t io_count; 606ae2fa67SRichard Kennedy struct bio *bio; 613eaf840eSJun'ichi "Nick" Nomura unsigned long start_time; 62f88fb981SKiyoshi Ueda spinlock_t endio_lock; 631da177e4SLinus Torvalds }; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds /* 668fbf26adSKiyoshi Ueda * For bio-based dm. 671da177e4SLinus Torvalds * One of these is allocated per target within a bio. Hopefully 681da177e4SLinus Torvalds * this will be simplified out one day. 691da177e4SLinus Torvalds */ 70028867acSAlasdair G Kergon struct dm_target_io { 711da177e4SLinus Torvalds struct dm_io *io; 721da177e4SLinus Torvalds struct dm_target *ti; 731da177e4SLinus Torvalds union map_info info; 741da177e4SLinus Torvalds }; 751da177e4SLinus Torvalds 768fbf26adSKiyoshi Ueda /* 778fbf26adSKiyoshi Ueda * For request-based dm. 788fbf26adSKiyoshi Ueda * One of these is allocated per request. 798fbf26adSKiyoshi Ueda */ 808fbf26adSKiyoshi Ueda struct dm_rq_target_io { 818fbf26adSKiyoshi Ueda struct mapped_device *md; 828fbf26adSKiyoshi Ueda struct dm_target *ti; 838fbf26adSKiyoshi Ueda struct request *orig, clone; 848fbf26adSKiyoshi Ueda int error; 858fbf26adSKiyoshi Ueda union map_info info; 868fbf26adSKiyoshi Ueda }; 878fbf26adSKiyoshi Ueda 888fbf26adSKiyoshi Ueda /* 8994818742SKent Overstreet * For request-based dm - the bio clones we allocate are embedded in these 9094818742SKent Overstreet * structs. 9194818742SKent Overstreet * 9294818742SKent Overstreet * We allocate these with bio_alloc_bioset, using the front_pad parameter when 9394818742SKent Overstreet * the bioset is created - this means the bio has to come at the end of the 9494818742SKent Overstreet * struct. 958fbf26adSKiyoshi Ueda */ 968fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info { 978fbf26adSKiyoshi Ueda struct bio *orig; 98cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio; 9994818742SKent Overstreet struct bio clone; 1008fbf26adSKiyoshi Ueda }; 1018fbf26adSKiyoshi Ueda 1021da177e4SLinus Torvalds union map_info *dm_get_mapinfo(struct bio *bio) 1031da177e4SLinus Torvalds { 1041da177e4SLinus Torvalds if (bio && bio->bi_private) 105028867acSAlasdair G Kergon return &((struct dm_target_io *)bio->bi_private)->info; 1061da177e4SLinus Torvalds return NULL; 1071da177e4SLinus Torvalds } 1081da177e4SLinus Torvalds 109cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq) 110cec47e3dSKiyoshi Ueda { 111cec47e3dSKiyoshi Ueda if (rq && rq->end_io_data) 112cec47e3dSKiyoshi Ueda return &((struct dm_rq_target_io *)rq->end_io_data)->info; 113cec47e3dSKiyoshi Ueda return NULL; 114cec47e3dSKiyoshi Ueda } 115cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo); 116cec47e3dSKiyoshi Ueda 117ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1) 118ba61fdd1SJeff Mahoney 1191da177e4SLinus Torvalds /* 1201da177e4SLinus Torvalds * Bits for the md->flags field. 1211da177e4SLinus Torvalds */ 1221eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0 1231da177e4SLinus Torvalds #define DMF_SUSPENDED 1 124aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2 125fba9f90eSJeff Mahoney #define DMF_FREEING 3 1265c6bd75dSAlasdair G Kergon #define DMF_DELETING 4 1272e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5 128d5b9dd04SMikulas Patocka #define DMF_MERGE_IS_OPTIONAL 6 1291da177e4SLinus Torvalds 130304f3f6aSMilan Broz /* 131304f3f6aSMilan Broz * Work processed by per-device workqueue. 132304f3f6aSMilan Broz */ 1331da177e4SLinus Torvalds struct mapped_device { 1342ca3310eSAlasdair G Kergon struct rw_semaphore io_lock; 135e61290a4SDaniel Walker struct mutex suspend_lock; 1361da177e4SLinus Torvalds rwlock_t map_lock; 1371da177e4SLinus Torvalds atomic_t holders; 1385c6bd75dSAlasdair G Kergon atomic_t open_count; 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds unsigned long flags; 1411da177e4SLinus Torvalds 142165125e1SJens Axboe struct request_queue *queue; 143a5664dadSMike Snitzer unsigned type; 1444a0b4ddfSMike Snitzer /* Protect queue and type against concurrent access. */ 145a5664dadSMike Snitzer struct mutex type_lock; 146a5664dadSMike Snitzer 14736a0456fSAlasdair G Kergon struct target_type *immutable_target_type; 14836a0456fSAlasdair G Kergon 1491da177e4SLinus Torvalds struct gendisk *disk; 1507e51f257SMike Anderson char name[16]; 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds void *interface_ptr; 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds /* 1551da177e4SLinus Torvalds * A list of ios that arrived while we were suspended. 1561da177e4SLinus Torvalds */ 157316d315bSNikanth Karthikesan atomic_t pending[2]; 1581da177e4SLinus Torvalds wait_queue_head_t wait; 15953d5914fSMikulas Patocka struct work_struct work; 1601da177e4SLinus Torvalds struct bio_list deferred; 161022c2611SMikulas Patocka spinlock_t deferred_lock; 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds /* 16429e4013dSTejun Heo * Processing queue (flush) 165304f3f6aSMilan Broz */ 166304f3f6aSMilan Broz struct workqueue_struct *wq; 167304f3f6aSMilan Broz 168304f3f6aSMilan Broz /* 1691da177e4SLinus Torvalds * The current mapping. 1701da177e4SLinus Torvalds */ 1711da177e4SLinus Torvalds struct dm_table *map; 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds /* 1741da177e4SLinus Torvalds * io objects are allocated from here. 1751da177e4SLinus Torvalds */ 1761da177e4SLinus Torvalds mempool_t *io_pool; 1771da177e4SLinus Torvalds mempool_t *tio_pool; 1781da177e4SLinus Torvalds 1799faf400fSStefan Bader struct bio_set *bs; 1809faf400fSStefan Bader 1811da177e4SLinus Torvalds /* 1821da177e4SLinus Torvalds * Event handling. 1831da177e4SLinus Torvalds */ 1841da177e4SLinus Torvalds atomic_t event_nr; 1851da177e4SLinus Torvalds wait_queue_head_t eventq; 1867a8c3d3bSMike Anderson atomic_t uevent_seq; 1877a8c3d3bSMike Anderson struct list_head uevent_list; 1887a8c3d3bSMike Anderson spinlock_t uevent_lock; /* Protect access to uevent_list */ 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds /* 1911da177e4SLinus Torvalds * freeze/thaw support require holding onto a super block 1921da177e4SLinus Torvalds */ 1931da177e4SLinus Torvalds struct super_block *frozen_sb; 194db8fef4fSMikulas Patocka struct block_device *bdev; 1953ac51e74SDarrick J. Wong 1963ac51e74SDarrick J. Wong /* forced geometry settings */ 1973ac51e74SDarrick J. Wong struct hd_geometry geometry; 198784aae73SMilan Broz 199784aae73SMilan Broz /* sysfs handle */ 200784aae73SMilan Broz struct kobject kobj; 20152b1fd5aSMikulas Patocka 202d87f4c14STejun Heo /* zero-length flush that will be cloned and submitted to targets */ 203d87f4c14STejun Heo struct bio flush_bio; 2041da177e4SLinus Torvalds }; 2051da177e4SLinus Torvalds 206e6ee8c0bSKiyoshi Ueda /* 207e6ee8c0bSKiyoshi Ueda * For mempools pre-allocation at the table loading time. 208e6ee8c0bSKiyoshi Ueda */ 209e6ee8c0bSKiyoshi Ueda struct dm_md_mempools { 210e6ee8c0bSKiyoshi Ueda mempool_t *io_pool; 211e6ee8c0bSKiyoshi Ueda mempool_t *tio_pool; 212e6ee8c0bSKiyoshi Ueda struct bio_set *bs; 213e6ee8c0bSKiyoshi Ueda }; 214e6ee8c0bSKiyoshi Ueda 2151da177e4SLinus Torvalds #define MIN_IOS 256 216e18b890bSChristoph Lameter static struct kmem_cache *_io_cache; 217e18b890bSChristoph Lameter static struct kmem_cache *_tio_cache; 2188fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache; 21994818742SKent Overstreet 22094818742SKent Overstreet /* 22194818742SKent Overstreet * Unused now, and needs to be deleted. But since io_pool is overloaded and it's 22294818742SKent Overstreet * still used for _io_cache, I'm leaving this for a later cleanup 22394818742SKent Overstreet */ 2248fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_bio_info_cache; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds static int __init local_init(void) 2271da177e4SLinus Torvalds { 22851157b4aSKiyoshi Ueda int r = -ENOMEM; 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds /* allocate a slab for the dm_ios */ 231028867acSAlasdair G Kergon _io_cache = KMEM_CACHE(dm_io, 0); 2321da177e4SLinus Torvalds if (!_io_cache) 23351157b4aSKiyoshi Ueda return r; 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds /* allocate a slab for the target ios */ 236028867acSAlasdair G Kergon _tio_cache = KMEM_CACHE(dm_target_io, 0); 23751157b4aSKiyoshi Ueda if (!_tio_cache) 23851157b4aSKiyoshi Ueda goto out_free_io_cache; 2391da177e4SLinus Torvalds 2408fbf26adSKiyoshi Ueda _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0); 2418fbf26adSKiyoshi Ueda if (!_rq_tio_cache) 2428fbf26adSKiyoshi Ueda goto out_free_tio_cache; 2438fbf26adSKiyoshi Ueda 2448fbf26adSKiyoshi Ueda _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0); 2458fbf26adSKiyoshi Ueda if (!_rq_bio_info_cache) 2468fbf26adSKiyoshi Ueda goto out_free_rq_tio_cache; 2478fbf26adSKiyoshi Ueda 24851e5b2bdSMike Anderson r = dm_uevent_init(); 24951157b4aSKiyoshi Ueda if (r) 2508fbf26adSKiyoshi Ueda goto out_free_rq_bio_info_cache; 25151e5b2bdSMike Anderson 2521da177e4SLinus Torvalds _major = major; 2531da177e4SLinus Torvalds r = register_blkdev(_major, _name); 25451157b4aSKiyoshi Ueda if (r < 0) 25551157b4aSKiyoshi Ueda goto out_uevent_exit; 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds if (!_major) 2581da177e4SLinus Torvalds _major = r; 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds return 0; 26151157b4aSKiyoshi Ueda 26251157b4aSKiyoshi Ueda out_uevent_exit: 26351157b4aSKiyoshi Ueda dm_uevent_exit(); 2648fbf26adSKiyoshi Ueda out_free_rq_bio_info_cache: 2658fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_bio_info_cache); 2668fbf26adSKiyoshi Ueda out_free_rq_tio_cache: 2678fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_tio_cache); 26851157b4aSKiyoshi Ueda out_free_tio_cache: 26951157b4aSKiyoshi Ueda kmem_cache_destroy(_tio_cache); 27051157b4aSKiyoshi Ueda out_free_io_cache: 27151157b4aSKiyoshi Ueda kmem_cache_destroy(_io_cache); 27251157b4aSKiyoshi Ueda 27351157b4aSKiyoshi Ueda return r; 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds static void local_exit(void) 2771da177e4SLinus Torvalds { 2788fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_bio_info_cache); 2798fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_tio_cache); 2801da177e4SLinus Torvalds kmem_cache_destroy(_tio_cache); 2811da177e4SLinus Torvalds kmem_cache_destroy(_io_cache); 28200d59405SAkinobu Mita unregister_blkdev(_major, _name); 28351e5b2bdSMike Anderson dm_uevent_exit(); 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds _major = 0; 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds DMINFO("cleaned up"); 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds 290b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = { 2911da177e4SLinus Torvalds local_init, 2921da177e4SLinus Torvalds dm_target_init, 2931da177e4SLinus Torvalds dm_linear_init, 2941da177e4SLinus Torvalds dm_stripe_init, 295952b3557SMikulas Patocka dm_io_init, 296945fa4d2SMikulas Patocka dm_kcopyd_init, 2971da177e4SLinus Torvalds dm_interface_init, 2981da177e4SLinus Torvalds }; 2991da177e4SLinus Torvalds 300b9249e55SAlasdair G Kergon static void (*_exits[])(void) = { 3011da177e4SLinus Torvalds local_exit, 3021da177e4SLinus Torvalds dm_target_exit, 3031da177e4SLinus Torvalds dm_linear_exit, 3041da177e4SLinus Torvalds dm_stripe_exit, 305952b3557SMikulas Patocka dm_io_exit, 306945fa4d2SMikulas Patocka dm_kcopyd_exit, 3071da177e4SLinus Torvalds dm_interface_exit, 3081da177e4SLinus Torvalds }; 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds static int __init dm_init(void) 3111da177e4SLinus Torvalds { 3121da177e4SLinus Torvalds const int count = ARRAY_SIZE(_inits); 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds int r, i; 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds for (i = 0; i < count; i++) { 3171da177e4SLinus Torvalds r = _inits[i](); 3181da177e4SLinus Torvalds if (r) 3191da177e4SLinus Torvalds goto bad; 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds return 0; 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds bad: 3251da177e4SLinus Torvalds while (i--) 3261da177e4SLinus Torvalds _exits[i](); 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds return r; 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds static void __exit dm_exit(void) 3321da177e4SLinus Torvalds { 3331da177e4SLinus Torvalds int i = ARRAY_SIZE(_exits); 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds while (i--) 3361da177e4SLinus Torvalds _exits[i](); 337d15b774cSAlasdair G Kergon 338d15b774cSAlasdair G Kergon /* 339d15b774cSAlasdair G Kergon * Should be empty by this point. 340d15b774cSAlasdair G Kergon */ 341d15b774cSAlasdair G Kergon idr_remove_all(&_minor_idr); 342d15b774cSAlasdair G Kergon idr_destroy(&_minor_idr); 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds /* 3461da177e4SLinus Torvalds * Block device functions 3471da177e4SLinus Torvalds */ 348432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md) 349432a212cSMike Anderson { 350432a212cSMike Anderson return test_bit(DMF_DELETING, &md->flags); 351432a212cSMike Anderson } 352432a212cSMike Anderson 353fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode) 3541da177e4SLinus Torvalds { 3551da177e4SLinus Torvalds struct mapped_device *md; 3561da177e4SLinus Torvalds 357fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 358fba9f90eSJeff Mahoney 359fe5f9f2cSAl Viro md = bdev->bd_disk->private_data; 360fba9f90eSJeff Mahoney if (!md) 361fba9f90eSJeff Mahoney goto out; 362fba9f90eSJeff Mahoney 3635c6bd75dSAlasdair G Kergon if (test_bit(DMF_FREEING, &md->flags) || 364432a212cSMike Anderson dm_deleting_md(md)) { 365fba9f90eSJeff Mahoney md = NULL; 366fba9f90eSJeff Mahoney goto out; 367fba9f90eSJeff Mahoney } 368fba9f90eSJeff Mahoney 3691da177e4SLinus Torvalds dm_get(md); 3705c6bd75dSAlasdair G Kergon atomic_inc(&md->open_count); 371fba9f90eSJeff Mahoney 372fba9f90eSJeff Mahoney out: 373fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 374fba9f90eSJeff Mahoney 375fba9f90eSJeff Mahoney return md ? 0 : -ENXIO; 3761da177e4SLinus Torvalds } 3771da177e4SLinus Torvalds 378fe5f9f2cSAl Viro static int dm_blk_close(struct gendisk *disk, fmode_t mode) 3791da177e4SLinus Torvalds { 380fe5f9f2cSAl Viro struct mapped_device *md = disk->private_data; 3816e9624b8SArnd Bergmann 3824a1aeb98SMilan Broz spin_lock(&_minor_lock); 3834a1aeb98SMilan Broz 3845c6bd75dSAlasdair G Kergon atomic_dec(&md->open_count); 3851da177e4SLinus Torvalds dm_put(md); 3864a1aeb98SMilan Broz 3874a1aeb98SMilan Broz spin_unlock(&_minor_lock); 3886e9624b8SArnd Bergmann 3891da177e4SLinus Torvalds return 0; 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds 3925c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md) 3935c6bd75dSAlasdair G Kergon { 3945c6bd75dSAlasdair G Kergon return atomic_read(&md->open_count); 3955c6bd75dSAlasdair G Kergon } 3965c6bd75dSAlasdair G Kergon 3975c6bd75dSAlasdair G Kergon /* 3985c6bd75dSAlasdair G Kergon * Guarantees nothing is using the device before it's deleted. 3995c6bd75dSAlasdair G Kergon */ 4005c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md) 4015c6bd75dSAlasdair G Kergon { 4025c6bd75dSAlasdair G Kergon int r = 0; 4035c6bd75dSAlasdair G Kergon 4045c6bd75dSAlasdair G Kergon spin_lock(&_minor_lock); 4055c6bd75dSAlasdair G Kergon 4065c6bd75dSAlasdair G Kergon if (dm_open_count(md)) 4075c6bd75dSAlasdair G Kergon r = -EBUSY; 4085c6bd75dSAlasdair G Kergon else 4095c6bd75dSAlasdair G Kergon set_bit(DMF_DELETING, &md->flags); 4105c6bd75dSAlasdair G Kergon 4115c6bd75dSAlasdair G Kergon spin_unlock(&_minor_lock); 4125c6bd75dSAlasdair G Kergon 4135c6bd75dSAlasdair G Kergon return r; 4145c6bd75dSAlasdair G Kergon } 4155c6bd75dSAlasdair G Kergon 4163ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 4173ac51e74SDarrick J. Wong { 4183ac51e74SDarrick J. Wong struct mapped_device *md = bdev->bd_disk->private_data; 4193ac51e74SDarrick J. Wong 4203ac51e74SDarrick J. Wong return dm_get_geometry(md, geo); 4213ac51e74SDarrick J. Wong } 4223ac51e74SDarrick J. Wong 423fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode, 424aa129a22SMilan Broz unsigned int cmd, unsigned long arg) 425aa129a22SMilan Broz { 426fe5f9f2cSAl Viro struct mapped_device *md = bdev->bd_disk->private_data; 4277c666411SAlasdair G Kergon struct dm_table *map = dm_get_live_table(md); 428aa129a22SMilan Broz struct dm_target *tgt; 429aa129a22SMilan Broz int r = -ENOTTY; 430aa129a22SMilan Broz 431aa129a22SMilan Broz if (!map || !dm_table_get_size(map)) 432aa129a22SMilan Broz goto out; 433aa129a22SMilan Broz 434aa129a22SMilan Broz /* We only support devices that have a single target */ 435aa129a22SMilan Broz if (dm_table_get_num_targets(map) != 1) 436aa129a22SMilan Broz goto out; 437aa129a22SMilan Broz 438aa129a22SMilan Broz tgt = dm_table_get_target(map, 0); 439aa129a22SMilan Broz 4404f186f8bSKiyoshi Ueda if (dm_suspended_md(md)) { 441aa129a22SMilan Broz r = -EAGAIN; 442aa129a22SMilan Broz goto out; 443aa129a22SMilan Broz } 444aa129a22SMilan Broz 445aa129a22SMilan Broz if (tgt->type->ioctl) 446647b3d00SAl Viro r = tgt->type->ioctl(tgt, cmd, arg); 447aa129a22SMilan Broz 448aa129a22SMilan Broz out: 449aa129a22SMilan Broz dm_table_put(map); 450aa129a22SMilan Broz 451aa129a22SMilan Broz return r; 452aa129a22SMilan Broz } 453aa129a22SMilan Broz 454028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md) 4551da177e4SLinus Torvalds { 4561da177e4SLinus Torvalds return mempool_alloc(md->io_pool, GFP_NOIO); 4571da177e4SLinus Torvalds } 4581da177e4SLinus Torvalds 459028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io) 4601da177e4SLinus Torvalds { 4611da177e4SLinus Torvalds mempool_free(io, md->io_pool); 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds 464028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio) 4651da177e4SLinus Torvalds { 4661da177e4SLinus Torvalds mempool_free(tio, md->tio_pool); 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds 46908885643SKiyoshi Ueda static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md, 47008885643SKiyoshi Ueda gfp_t gfp_mask) 471cec47e3dSKiyoshi Ueda { 47208885643SKiyoshi Ueda return mempool_alloc(md->tio_pool, gfp_mask); 473cec47e3dSKiyoshi Ueda } 474cec47e3dSKiyoshi Ueda 475cec47e3dSKiyoshi Ueda static void free_rq_tio(struct dm_rq_target_io *tio) 476cec47e3dSKiyoshi Ueda { 477cec47e3dSKiyoshi Ueda mempool_free(tio, tio->md->tio_pool); 478cec47e3dSKiyoshi Ueda } 479cec47e3dSKiyoshi Ueda 48090abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md) 48190abb8c4SKiyoshi Ueda { 48290abb8c4SKiyoshi Ueda return atomic_read(&md->pending[READ]) + 48390abb8c4SKiyoshi Ueda atomic_read(&md->pending[WRITE]); 48490abb8c4SKiyoshi Ueda } 48590abb8c4SKiyoshi Ueda 4863eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io) 4873eaf840eSJun'ichi "Nick" Nomura { 4883eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 489c9959059STejun Heo int cpu; 490316d315bSNikanth Karthikesan int rw = bio_data_dir(io->bio); 4913eaf840eSJun'ichi "Nick" Nomura 4923eaf840eSJun'ichi "Nick" Nomura io->start_time = jiffies; 4933eaf840eSJun'ichi "Nick" Nomura 494074a7acaSTejun Heo cpu = part_stat_lock(); 495074a7acaSTejun Heo part_round_stats(cpu, &dm_disk(md)->part0); 496074a7acaSTejun Heo part_stat_unlock(); 4971e9bb880SShaohua Li atomic_set(&dm_disk(md)->part0.in_flight[rw], 4981e9bb880SShaohua Li atomic_inc_return(&md->pending[rw])); 4993eaf840eSJun'ichi "Nick" Nomura } 5003eaf840eSJun'ichi "Nick" Nomura 501d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io) 5023eaf840eSJun'ichi "Nick" Nomura { 5033eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 5043eaf840eSJun'ichi "Nick" Nomura struct bio *bio = io->bio; 5053eaf840eSJun'ichi "Nick" Nomura unsigned long duration = jiffies - io->start_time; 506c9959059STejun Heo int pending, cpu; 5073eaf840eSJun'ichi "Nick" Nomura int rw = bio_data_dir(bio); 5083eaf840eSJun'ichi "Nick" Nomura 509074a7acaSTejun Heo cpu = part_stat_lock(); 510074a7acaSTejun Heo part_round_stats(cpu, &dm_disk(md)->part0); 511074a7acaSTejun Heo part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration); 512074a7acaSTejun Heo part_stat_unlock(); 5133eaf840eSJun'ichi "Nick" Nomura 514af7e466aSMikulas Patocka /* 515af7e466aSMikulas Patocka * After this is decremented the bio must not be touched if it is 516d87f4c14STejun Heo * a flush. 517af7e466aSMikulas Patocka */ 5181e9bb880SShaohua Li pending = atomic_dec_return(&md->pending[rw]); 5191e9bb880SShaohua Li atomic_set(&dm_disk(md)->part0.in_flight[rw], pending); 520316d315bSNikanth Karthikesan pending += atomic_read(&md->pending[rw^0x1]); 5213eaf840eSJun'ichi "Nick" Nomura 522d221d2e7SMikulas Patocka /* nudge anyone waiting on suspend queue */ 523d221d2e7SMikulas Patocka if (!pending) 524d221d2e7SMikulas Patocka wake_up(&md->wait); 5253eaf840eSJun'ichi "Nick" Nomura } 5263eaf840eSJun'ichi "Nick" Nomura 5271da177e4SLinus Torvalds /* 5281da177e4SLinus Torvalds * Add the bio to the list of deferred io. 5291da177e4SLinus Torvalds */ 53092c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio) 5311da177e4SLinus Torvalds { 53205447420SKiyoshi Ueda unsigned long flags; 5331da177e4SLinus Torvalds 53405447420SKiyoshi Ueda spin_lock_irqsave(&md->deferred_lock, flags); 5351da177e4SLinus Torvalds bio_list_add(&md->deferred, bio); 53605447420SKiyoshi Ueda spin_unlock_irqrestore(&md->deferred_lock, flags); 53792c63902SMikulas Patocka queue_work(md->wq, &md->work); 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds /* 5411da177e4SLinus Torvalds * Everyone (including functions in this file), should use this 5421da177e4SLinus Torvalds * function to access the md->map field, and make sure they call 5431da177e4SLinus Torvalds * dm_table_put() when finished. 5441da177e4SLinus Torvalds */ 5457c666411SAlasdair G Kergon struct dm_table *dm_get_live_table(struct mapped_device *md) 5461da177e4SLinus Torvalds { 5471da177e4SLinus Torvalds struct dm_table *t; 548523d9297SKiyoshi Ueda unsigned long flags; 5491da177e4SLinus Torvalds 550523d9297SKiyoshi Ueda read_lock_irqsave(&md->map_lock, flags); 5511da177e4SLinus Torvalds t = md->map; 5521da177e4SLinus Torvalds if (t) 5531da177e4SLinus Torvalds dm_table_get(t); 554523d9297SKiyoshi Ueda read_unlock_irqrestore(&md->map_lock, flags); 5551da177e4SLinus Torvalds 5561da177e4SLinus Torvalds return t; 5571da177e4SLinus Torvalds } 5581da177e4SLinus Torvalds 5593ac51e74SDarrick J. Wong /* 5603ac51e74SDarrick J. Wong * Get the geometry associated with a dm device 5613ac51e74SDarrick J. Wong */ 5623ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) 5633ac51e74SDarrick J. Wong { 5643ac51e74SDarrick J. Wong *geo = md->geometry; 5653ac51e74SDarrick J. Wong 5663ac51e74SDarrick J. Wong return 0; 5673ac51e74SDarrick J. Wong } 5683ac51e74SDarrick J. Wong 5693ac51e74SDarrick J. Wong /* 5703ac51e74SDarrick J. Wong * Set the geometry of a device. 5713ac51e74SDarrick J. Wong */ 5723ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) 5733ac51e74SDarrick J. Wong { 5743ac51e74SDarrick J. Wong sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; 5753ac51e74SDarrick J. Wong 5763ac51e74SDarrick J. Wong if (geo->start > sz) { 5773ac51e74SDarrick J. Wong DMWARN("Start sector is beyond the geometry limits."); 5783ac51e74SDarrick J. Wong return -EINVAL; 5793ac51e74SDarrick J. Wong } 5803ac51e74SDarrick J. Wong 5813ac51e74SDarrick J. Wong md->geometry = *geo; 5823ac51e74SDarrick J. Wong 5833ac51e74SDarrick J. Wong return 0; 5843ac51e74SDarrick J. Wong } 5853ac51e74SDarrick J. Wong 5861da177e4SLinus Torvalds /*----------------------------------------------------------------- 5871da177e4SLinus Torvalds * CRUD START: 5881da177e4SLinus Torvalds * A more elegant soln is in the works that uses the queue 5891da177e4SLinus Torvalds * merge fn, unfortunately there are a couple of changes to 5901da177e4SLinus Torvalds * the block layer that I want to make for this. So in the 5911da177e4SLinus Torvalds * interests of getting something for people to use I give 5921da177e4SLinus Torvalds * you this clearly demarcated crap. 5931da177e4SLinus Torvalds *---------------------------------------------------------------*/ 5941da177e4SLinus Torvalds 5952e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md) 5962e93ccc1SKiyoshi Ueda { 5972e93ccc1SKiyoshi Ueda return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 5982e93ccc1SKiyoshi Ueda } 5992e93ccc1SKiyoshi Ueda 6001da177e4SLinus Torvalds /* 6011da177e4SLinus Torvalds * Decrements the number of outstanding ios that a bio has been 6021da177e4SLinus Torvalds * cloned into, completing the original io if necc. 6031da177e4SLinus Torvalds */ 604858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error) 6051da177e4SLinus Torvalds { 6062e93ccc1SKiyoshi Ueda unsigned long flags; 607b35f8caaSMilan Broz int io_error; 608b35f8caaSMilan Broz struct bio *bio; 609b35f8caaSMilan Broz struct mapped_device *md = io->md; 6102e93ccc1SKiyoshi Ueda 6112e93ccc1SKiyoshi Ueda /* Push-back supersedes any I/O errors */ 612f88fb981SKiyoshi Ueda if (unlikely(error)) { 613f88fb981SKiyoshi Ueda spin_lock_irqsave(&io->endio_lock, flags); 614f88fb981SKiyoshi Ueda if (!(io->error > 0 && __noflush_suspending(md))) 6151da177e4SLinus Torvalds io->error = error; 616f88fb981SKiyoshi Ueda spin_unlock_irqrestore(&io->endio_lock, flags); 617f88fb981SKiyoshi Ueda } 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds if (atomic_dec_and_test(&io->io_count)) { 6202e93ccc1SKiyoshi Ueda if (io->error == DM_ENDIO_REQUEUE) { 6212e93ccc1SKiyoshi Ueda /* 6222e93ccc1SKiyoshi Ueda * Target requested pushing back the I/O. 6232e93ccc1SKiyoshi Ueda */ 624022c2611SMikulas Patocka spin_lock_irqsave(&md->deferred_lock, flags); 6256a8736d1STejun Heo if (__noflush_suspending(md)) 6266a8736d1STejun Heo bio_list_add_head(&md->deferred, io->bio); 6276a8736d1STejun Heo else 6282e93ccc1SKiyoshi Ueda /* noflush suspend was interrupted. */ 6292e93ccc1SKiyoshi Ueda io->error = -EIO; 630022c2611SMikulas Patocka spin_unlock_irqrestore(&md->deferred_lock, flags); 6312e93ccc1SKiyoshi Ueda } 6322e93ccc1SKiyoshi Ueda 633b35f8caaSMilan Broz io_error = io->error; 634b35f8caaSMilan Broz bio = io->bio; 635af7e466aSMikulas Patocka end_io_acct(io); 636a97f925aSMikulas Patocka free_io(md, io); 6371da177e4SLinus Torvalds 6386a8736d1STejun Heo if (io_error == DM_ENDIO_REQUEUE) 6396a8736d1STejun Heo return; 6406a8736d1STejun Heo 641b372d360SMike Snitzer if ((bio->bi_rw & REQ_FLUSH) && bio->bi_size) { 6421da177e4SLinus Torvalds /* 6436a8736d1STejun Heo * Preflush done for flush with data, reissue 6446a8736d1STejun Heo * without REQ_FLUSH. 6451da177e4SLinus Torvalds */ 6466a8736d1STejun Heo bio->bi_rw &= ~REQ_FLUSH; 6476a8736d1STejun Heo queue_io(md, bio); 6485f3ea37cSArnaldo Carvalho de Melo } else { 649b372d360SMike Snitzer /* done with normal IO or empty flush */ 650b7908c10SJeff Moyer trace_block_bio_complete(md->queue, bio, io_error); 651b35f8caaSMilan Broz bio_endio(bio, io_error); 6522e93ccc1SKiyoshi Ueda } 6531da177e4SLinus Torvalds } 654af7e466aSMikulas Patocka } 6551da177e4SLinus Torvalds 6566712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error) 6571da177e4SLinus Torvalds { 6581da177e4SLinus Torvalds int r = 0; 659028867acSAlasdair G Kergon struct dm_target_io *tio = bio->bi_private; 660b35f8caaSMilan Broz struct dm_io *io = tio->io; 6619faf400fSStefan Bader struct mapped_device *md = tio->io->md; 6621da177e4SLinus Torvalds dm_endio_fn endio = tio->ti->type->end_io; 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds if (!bio_flagged(bio, BIO_UPTODATE) && !error) 6651da177e4SLinus Torvalds error = -EIO; 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds if (endio) { 6681da177e4SLinus Torvalds r = endio(tio->ti, bio, error, &tio->info); 6692e93ccc1SKiyoshi Ueda if (r < 0 || r == DM_ENDIO_REQUEUE) 6702e93ccc1SKiyoshi Ueda /* 6712e93ccc1SKiyoshi Ueda * error and requeue request are handled 6722e93ccc1SKiyoshi Ueda * in dec_pending(). 6732e93ccc1SKiyoshi Ueda */ 6741da177e4SLinus Torvalds error = r; 67545cbcd79SKiyoshi Ueda else if (r == DM_ENDIO_INCOMPLETE) 67645cbcd79SKiyoshi Ueda /* The target will handle the io */ 6776712ecf8SNeilBrown return; 67845cbcd79SKiyoshi Ueda else if (r) { 67945cbcd79SKiyoshi Ueda DMWARN("unimplemented target endio return value: %d", r); 68045cbcd79SKiyoshi Ueda BUG(); 68145cbcd79SKiyoshi Ueda } 6821da177e4SLinus Torvalds } 6831da177e4SLinus Torvalds 6849faf400fSStefan Bader free_tio(md, tio); 685b35f8caaSMilan Broz bio_put(bio); 686b35f8caaSMilan Broz dec_pending(io, error); 6871da177e4SLinus Torvalds } 6881da177e4SLinus Torvalds 689cec47e3dSKiyoshi Ueda /* 690cec47e3dSKiyoshi Ueda * Partial completion handling for request-based dm 691cec47e3dSKiyoshi Ueda */ 692cec47e3dSKiyoshi Ueda static void end_clone_bio(struct bio *clone, int error) 693cec47e3dSKiyoshi Ueda { 694cec47e3dSKiyoshi Ueda struct dm_rq_clone_bio_info *info = clone->bi_private; 695cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = info->tio; 696cec47e3dSKiyoshi Ueda struct bio *bio = info->orig; 697cec47e3dSKiyoshi Ueda unsigned int nr_bytes = info->orig->bi_size; 698cec47e3dSKiyoshi Ueda 699cec47e3dSKiyoshi Ueda bio_put(clone); 700cec47e3dSKiyoshi Ueda 701cec47e3dSKiyoshi Ueda if (tio->error) 702cec47e3dSKiyoshi Ueda /* 703cec47e3dSKiyoshi Ueda * An error has already been detected on the request. 704cec47e3dSKiyoshi Ueda * Once error occurred, just let clone->end_io() handle 705cec47e3dSKiyoshi Ueda * the remainder. 706cec47e3dSKiyoshi Ueda */ 707cec47e3dSKiyoshi Ueda return; 708cec47e3dSKiyoshi Ueda else if (error) { 709cec47e3dSKiyoshi Ueda /* 710cec47e3dSKiyoshi Ueda * Don't notice the error to the upper layer yet. 711cec47e3dSKiyoshi Ueda * The error handling decision is made by the target driver, 712cec47e3dSKiyoshi Ueda * when the request is completed. 713cec47e3dSKiyoshi Ueda */ 714cec47e3dSKiyoshi Ueda tio->error = error; 715cec47e3dSKiyoshi Ueda return; 716cec47e3dSKiyoshi Ueda } 717cec47e3dSKiyoshi Ueda 718cec47e3dSKiyoshi Ueda /* 719cec47e3dSKiyoshi Ueda * I/O for the bio successfully completed. 720cec47e3dSKiyoshi Ueda * Notice the data completion to the upper layer. 721cec47e3dSKiyoshi Ueda */ 722cec47e3dSKiyoshi Ueda 723cec47e3dSKiyoshi Ueda /* 724cec47e3dSKiyoshi Ueda * bios are processed from the head of the list. 725cec47e3dSKiyoshi Ueda * So the completing bio should always be rq->bio. 726cec47e3dSKiyoshi Ueda * If it's not, something wrong is happening. 727cec47e3dSKiyoshi Ueda */ 728cec47e3dSKiyoshi Ueda if (tio->orig->bio != bio) 729cec47e3dSKiyoshi Ueda DMERR("bio completion is going in the middle of the request"); 730cec47e3dSKiyoshi Ueda 731cec47e3dSKiyoshi Ueda /* 732cec47e3dSKiyoshi Ueda * Update the original request. 733cec47e3dSKiyoshi Ueda * Do not use blk_end_request() here, because it may complete 734cec47e3dSKiyoshi Ueda * the original request before the clone, and break the ordering. 735cec47e3dSKiyoshi Ueda */ 736cec47e3dSKiyoshi Ueda blk_update_request(tio->orig, 0, nr_bytes); 737cec47e3dSKiyoshi Ueda } 738cec47e3dSKiyoshi Ueda 739cec47e3dSKiyoshi Ueda /* 740cec47e3dSKiyoshi Ueda * Don't touch any member of the md after calling this function because 741cec47e3dSKiyoshi Ueda * the md may be freed in dm_put() at the end of this function. 742cec47e3dSKiyoshi Ueda * Or do dm_get() before calling this function and dm_put() later. 743cec47e3dSKiyoshi Ueda */ 744b4324feeSKiyoshi Ueda static void rq_completed(struct mapped_device *md, int rw, int run_queue) 745cec47e3dSKiyoshi Ueda { 746b4324feeSKiyoshi Ueda atomic_dec(&md->pending[rw]); 747cec47e3dSKiyoshi Ueda 748cec47e3dSKiyoshi Ueda /* nudge anyone waiting on suspend queue */ 749b4324feeSKiyoshi Ueda if (!md_in_flight(md)) 750cec47e3dSKiyoshi Ueda wake_up(&md->wait); 751cec47e3dSKiyoshi Ueda 752cec47e3dSKiyoshi Ueda if (run_queue) 753b4324feeSKiyoshi Ueda blk_run_queue(md->queue); 754cec47e3dSKiyoshi Ueda 755cec47e3dSKiyoshi Ueda /* 756cec47e3dSKiyoshi Ueda * dm_put() must be at the end of this function. See the comment above 757cec47e3dSKiyoshi Ueda */ 758cec47e3dSKiyoshi Ueda dm_put(md); 759cec47e3dSKiyoshi Ueda } 760cec47e3dSKiyoshi Ueda 761a77e28c7SKiyoshi Ueda static void free_rq_clone(struct request *clone) 762a77e28c7SKiyoshi Ueda { 763a77e28c7SKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 764a77e28c7SKiyoshi Ueda 765a77e28c7SKiyoshi Ueda blk_rq_unprep_clone(clone); 766a77e28c7SKiyoshi Ueda free_rq_tio(tio); 767a77e28c7SKiyoshi Ueda } 768a77e28c7SKiyoshi Ueda 769980691e5SKiyoshi Ueda /* 770980691e5SKiyoshi Ueda * Complete the clone and the original request. 771980691e5SKiyoshi Ueda * Must be called without queue lock. 772980691e5SKiyoshi Ueda */ 773980691e5SKiyoshi Ueda static void dm_end_request(struct request *clone, int error) 774980691e5SKiyoshi Ueda { 775980691e5SKiyoshi Ueda int rw = rq_data_dir(clone); 776980691e5SKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 777980691e5SKiyoshi Ueda struct mapped_device *md = tio->md; 778980691e5SKiyoshi Ueda struct request *rq = tio->orig; 779980691e5SKiyoshi Ueda 78029e4013dSTejun Heo if (rq->cmd_type == REQ_TYPE_BLOCK_PC) { 781980691e5SKiyoshi Ueda rq->errors = clone->errors; 782980691e5SKiyoshi Ueda rq->resid_len = clone->resid_len; 783980691e5SKiyoshi Ueda 784980691e5SKiyoshi Ueda if (rq->sense) 785980691e5SKiyoshi Ueda /* 786980691e5SKiyoshi Ueda * We are using the sense buffer of the original 787980691e5SKiyoshi Ueda * request. 788980691e5SKiyoshi Ueda * So setting the length of the sense data is enough. 789980691e5SKiyoshi Ueda */ 790980691e5SKiyoshi Ueda rq->sense_len = clone->sense_len; 791980691e5SKiyoshi Ueda } 792980691e5SKiyoshi Ueda 793980691e5SKiyoshi Ueda free_rq_clone(clone); 794980691e5SKiyoshi Ueda blk_end_request_all(rq, error); 79529e4013dSTejun Heo rq_completed(md, rw, true); 796980691e5SKiyoshi Ueda } 797980691e5SKiyoshi Ueda 798cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq) 799cec47e3dSKiyoshi Ueda { 800cec47e3dSKiyoshi Ueda struct request *clone = rq->special; 801cec47e3dSKiyoshi Ueda 802cec47e3dSKiyoshi Ueda rq->special = NULL; 803cec47e3dSKiyoshi Ueda rq->cmd_flags &= ~REQ_DONTPREP; 804cec47e3dSKiyoshi Ueda 805a77e28c7SKiyoshi Ueda free_rq_clone(clone); 806cec47e3dSKiyoshi Ueda } 807cec47e3dSKiyoshi Ueda 808cec47e3dSKiyoshi Ueda /* 809cec47e3dSKiyoshi Ueda * Requeue the original request of a clone. 810cec47e3dSKiyoshi Ueda */ 811cec47e3dSKiyoshi Ueda void dm_requeue_unmapped_request(struct request *clone) 812cec47e3dSKiyoshi Ueda { 813b4324feeSKiyoshi Ueda int rw = rq_data_dir(clone); 814cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 815cec47e3dSKiyoshi Ueda struct mapped_device *md = tio->md; 816cec47e3dSKiyoshi Ueda struct request *rq = tio->orig; 817cec47e3dSKiyoshi Ueda struct request_queue *q = rq->q; 818cec47e3dSKiyoshi Ueda unsigned long flags; 819cec47e3dSKiyoshi Ueda 820cec47e3dSKiyoshi Ueda dm_unprep_request(rq); 821cec47e3dSKiyoshi Ueda 822cec47e3dSKiyoshi Ueda spin_lock_irqsave(q->queue_lock, flags); 823cec47e3dSKiyoshi Ueda blk_requeue_request(q, rq); 824cec47e3dSKiyoshi Ueda spin_unlock_irqrestore(q->queue_lock, flags); 825cec47e3dSKiyoshi Ueda 826b4324feeSKiyoshi Ueda rq_completed(md, rw, 0); 827cec47e3dSKiyoshi Ueda } 828cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request); 829cec47e3dSKiyoshi Ueda 830cec47e3dSKiyoshi Ueda static void __stop_queue(struct request_queue *q) 831cec47e3dSKiyoshi Ueda { 832cec47e3dSKiyoshi Ueda blk_stop_queue(q); 833cec47e3dSKiyoshi Ueda } 834cec47e3dSKiyoshi Ueda 835cec47e3dSKiyoshi Ueda static void stop_queue(struct request_queue *q) 836cec47e3dSKiyoshi Ueda { 837cec47e3dSKiyoshi Ueda unsigned long flags; 838cec47e3dSKiyoshi Ueda 839cec47e3dSKiyoshi Ueda spin_lock_irqsave(q->queue_lock, flags); 840cec47e3dSKiyoshi Ueda __stop_queue(q); 841cec47e3dSKiyoshi Ueda spin_unlock_irqrestore(q->queue_lock, flags); 842cec47e3dSKiyoshi Ueda } 843cec47e3dSKiyoshi Ueda 844cec47e3dSKiyoshi Ueda static void __start_queue(struct request_queue *q) 845cec47e3dSKiyoshi Ueda { 846cec47e3dSKiyoshi Ueda if (blk_queue_stopped(q)) 847cec47e3dSKiyoshi Ueda blk_start_queue(q); 848cec47e3dSKiyoshi Ueda } 849cec47e3dSKiyoshi Ueda 850cec47e3dSKiyoshi Ueda static void start_queue(struct request_queue *q) 851cec47e3dSKiyoshi Ueda { 852cec47e3dSKiyoshi Ueda unsigned long flags; 853cec47e3dSKiyoshi Ueda 854cec47e3dSKiyoshi Ueda spin_lock_irqsave(q->queue_lock, flags); 855cec47e3dSKiyoshi Ueda __start_queue(q); 856cec47e3dSKiyoshi Ueda spin_unlock_irqrestore(q->queue_lock, flags); 857cec47e3dSKiyoshi Ueda } 858cec47e3dSKiyoshi Ueda 85911a68244SKiyoshi Ueda static void dm_done(struct request *clone, int error, bool mapped) 86011a68244SKiyoshi Ueda { 86111a68244SKiyoshi Ueda int r = error; 86211a68244SKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 86311a68244SKiyoshi Ueda dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io; 86411a68244SKiyoshi Ueda 86511a68244SKiyoshi Ueda if (mapped && rq_end_io) 86611a68244SKiyoshi Ueda r = rq_end_io(tio->ti, clone, error, &tio->info); 86711a68244SKiyoshi Ueda 86811a68244SKiyoshi Ueda if (r <= 0) 86911a68244SKiyoshi Ueda /* The target wants to complete the I/O */ 87011a68244SKiyoshi Ueda dm_end_request(clone, r); 87111a68244SKiyoshi Ueda else if (r == DM_ENDIO_INCOMPLETE) 87211a68244SKiyoshi Ueda /* The target will handle the I/O */ 87311a68244SKiyoshi Ueda return; 87411a68244SKiyoshi Ueda else if (r == DM_ENDIO_REQUEUE) 87511a68244SKiyoshi Ueda /* The target wants to requeue the I/O */ 87611a68244SKiyoshi Ueda dm_requeue_unmapped_request(clone); 87711a68244SKiyoshi Ueda else { 87811a68244SKiyoshi Ueda DMWARN("unimplemented target endio return value: %d", r); 87911a68244SKiyoshi Ueda BUG(); 88011a68244SKiyoshi Ueda } 88111a68244SKiyoshi Ueda } 88211a68244SKiyoshi Ueda 883cec47e3dSKiyoshi Ueda /* 884cec47e3dSKiyoshi Ueda * Request completion handler for request-based dm 885cec47e3dSKiyoshi Ueda */ 886cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq) 887cec47e3dSKiyoshi Ueda { 88811a68244SKiyoshi Ueda bool mapped = true; 889cec47e3dSKiyoshi Ueda struct request *clone = rq->completion_data; 890cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 891cec47e3dSKiyoshi Ueda 89211a68244SKiyoshi Ueda if (rq->cmd_flags & REQ_FAILED) 89311a68244SKiyoshi Ueda mapped = false; 894cec47e3dSKiyoshi Ueda 89511a68244SKiyoshi Ueda dm_done(clone, tio->error, mapped); 896cec47e3dSKiyoshi Ueda } 897cec47e3dSKiyoshi Ueda 898cec47e3dSKiyoshi Ueda /* 899cec47e3dSKiyoshi Ueda * Complete the clone and the original request with the error status 900cec47e3dSKiyoshi Ueda * through softirq context. 901cec47e3dSKiyoshi Ueda */ 902cec47e3dSKiyoshi Ueda static void dm_complete_request(struct request *clone, int error) 903cec47e3dSKiyoshi Ueda { 904cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 905cec47e3dSKiyoshi Ueda struct request *rq = tio->orig; 906cec47e3dSKiyoshi Ueda 907cec47e3dSKiyoshi Ueda tio->error = error; 908cec47e3dSKiyoshi Ueda rq->completion_data = clone; 909cec47e3dSKiyoshi Ueda blk_complete_request(rq); 910cec47e3dSKiyoshi Ueda } 911cec47e3dSKiyoshi Ueda 912cec47e3dSKiyoshi Ueda /* 913cec47e3dSKiyoshi Ueda * Complete the not-mapped clone and the original request with the error status 914cec47e3dSKiyoshi Ueda * through softirq context. 915cec47e3dSKiyoshi Ueda * Target's rq_end_io() function isn't called. 916cec47e3dSKiyoshi Ueda * This may be used when the target's map_rq() function fails. 917cec47e3dSKiyoshi Ueda */ 918cec47e3dSKiyoshi Ueda void dm_kill_unmapped_request(struct request *clone, int error) 919cec47e3dSKiyoshi Ueda { 920cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 921cec47e3dSKiyoshi Ueda struct request *rq = tio->orig; 922cec47e3dSKiyoshi Ueda 923cec47e3dSKiyoshi Ueda rq->cmd_flags |= REQ_FAILED; 924cec47e3dSKiyoshi Ueda dm_complete_request(clone, error); 925cec47e3dSKiyoshi Ueda } 926cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_kill_unmapped_request); 927cec47e3dSKiyoshi Ueda 928cec47e3dSKiyoshi Ueda /* 929cec47e3dSKiyoshi Ueda * Called with the queue lock held 930cec47e3dSKiyoshi Ueda */ 931cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error) 932cec47e3dSKiyoshi Ueda { 933cec47e3dSKiyoshi Ueda /* 934cec47e3dSKiyoshi Ueda * For just cleaning up the information of the queue in which 935cec47e3dSKiyoshi Ueda * the clone was dispatched. 936cec47e3dSKiyoshi Ueda * The clone is *NOT* freed actually here because it is alloced from 937cec47e3dSKiyoshi Ueda * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags. 938cec47e3dSKiyoshi Ueda */ 939cec47e3dSKiyoshi Ueda __blk_put_request(clone->q, clone); 940cec47e3dSKiyoshi Ueda 941cec47e3dSKiyoshi Ueda /* 942cec47e3dSKiyoshi Ueda * Actual request completion is done in a softirq context which doesn't 943cec47e3dSKiyoshi Ueda * hold the queue lock. Otherwise, deadlock could occur because: 944cec47e3dSKiyoshi Ueda * - another request may be submitted by the upper level driver 945cec47e3dSKiyoshi Ueda * of the stacking during the completion 946cec47e3dSKiyoshi Ueda * - the submission which requires queue lock may be done 947cec47e3dSKiyoshi Ueda * against this queue 948cec47e3dSKiyoshi Ueda */ 949cec47e3dSKiyoshi Ueda dm_complete_request(clone, error); 950cec47e3dSKiyoshi Ueda } 951cec47e3dSKiyoshi Ueda 95256a67df7SMike Snitzer /* 95356a67df7SMike Snitzer * Return maximum size of I/O possible at the supplied sector up to the current 95456a67df7SMike Snitzer * target boundary. 95556a67df7SMike Snitzer */ 95656a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti) 9571da177e4SLinus Torvalds { 95856a67df7SMike Snitzer sector_t target_offset = dm_target_offset(ti, sector); 95956a67df7SMike Snitzer 96056a67df7SMike Snitzer return ti->len - target_offset; 96156a67df7SMike Snitzer } 96256a67df7SMike Snitzer 96356a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti) 96456a67df7SMike Snitzer { 96556a67df7SMike Snitzer sector_t len = max_io_len_target_boundary(sector, ti); 966542f9038SMike Snitzer sector_t offset, max_len; 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds /* 9691da177e4SLinus Torvalds * Does the target need to split even further? 9701da177e4SLinus Torvalds */ 971542f9038SMike Snitzer if (ti->max_io_len) { 972542f9038SMike Snitzer offset = dm_target_offset(ti, sector); 973542f9038SMike Snitzer if (unlikely(ti->max_io_len & (ti->max_io_len - 1))) 974542f9038SMike Snitzer max_len = sector_div(offset, ti->max_io_len); 975542f9038SMike Snitzer else 976542f9038SMike Snitzer max_len = offset & (ti->max_io_len - 1); 977542f9038SMike Snitzer max_len = ti->max_io_len - max_len; 978542f9038SMike Snitzer 979542f9038SMike Snitzer if (len > max_len) 980542f9038SMike Snitzer len = max_len; 9811da177e4SLinus Torvalds } 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds return len; 9841da177e4SLinus Torvalds } 9851da177e4SLinus Torvalds 986542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len) 987542f9038SMike Snitzer { 988542f9038SMike Snitzer if (len > UINT_MAX) { 989542f9038SMike Snitzer DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)", 990542f9038SMike Snitzer (unsigned long long)len, UINT_MAX); 991542f9038SMike Snitzer ti->error = "Maximum size of target IO is too large"; 992542f9038SMike Snitzer return -EINVAL; 993542f9038SMike Snitzer } 994542f9038SMike Snitzer 995542f9038SMike Snitzer ti->max_io_len = (uint32_t) len; 996542f9038SMike Snitzer 997542f9038SMike Snitzer return 0; 998542f9038SMike Snitzer } 999542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len); 1000542f9038SMike Snitzer 10011da177e4SLinus Torvalds static void __map_bio(struct dm_target *ti, struct bio *clone, 1002028867acSAlasdair G Kergon struct dm_target_io *tio) 10031da177e4SLinus Torvalds { 10041da177e4SLinus Torvalds int r; 10052056a782SJens Axboe sector_t sector; 10069faf400fSStefan Bader struct mapped_device *md; 10071da177e4SLinus Torvalds 10081da177e4SLinus Torvalds clone->bi_end_io = clone_endio; 10091da177e4SLinus Torvalds clone->bi_private = tio; 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds /* 10121da177e4SLinus Torvalds * Map the clone. If r == 0 we don't need to do 10131da177e4SLinus Torvalds * anything, the target has assumed ownership of 10141da177e4SLinus Torvalds * this io. 10151da177e4SLinus Torvalds */ 10161da177e4SLinus Torvalds atomic_inc(&tio->io->io_count); 10172056a782SJens Axboe sector = clone->bi_sector; 10181da177e4SLinus Torvalds r = ti->type->map(ti, clone, &tio->info); 101945cbcd79SKiyoshi Ueda if (r == DM_MAPIO_REMAPPED) { 10201da177e4SLinus Torvalds /* the bio has been remapped so dispatch it */ 10212056a782SJens Axboe 1022d07335e5SMike Snitzer trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone, 102322a7c31aSAlan D. Brunelle tio->io->bio->bi_bdev->bd_dev, sector); 10242056a782SJens Axboe 10251da177e4SLinus Torvalds generic_make_request(clone); 10262e93ccc1SKiyoshi Ueda } else if (r < 0 || r == DM_MAPIO_REQUEUE) { 10272e93ccc1SKiyoshi Ueda /* error the io and bail out, or requeue it if needed */ 10289faf400fSStefan Bader md = tio->io->md; 10299faf400fSStefan Bader dec_pending(tio->io, r); 10301da177e4SLinus Torvalds bio_put(clone); 10319faf400fSStefan Bader free_tio(md, tio); 103245cbcd79SKiyoshi Ueda } else if (r) { 103345cbcd79SKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 103445cbcd79SKiyoshi Ueda BUG(); 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds struct clone_info { 10391da177e4SLinus Torvalds struct mapped_device *md; 10401da177e4SLinus Torvalds struct dm_table *map; 10411da177e4SLinus Torvalds struct bio *bio; 10421da177e4SLinus Torvalds struct dm_io *io; 10431da177e4SLinus Torvalds sector_t sector; 10441da177e4SLinus Torvalds sector_t sector_count; 10451da177e4SLinus Torvalds unsigned short idx; 10461da177e4SLinus Torvalds }; 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds /* 1049d87f4c14STejun Heo * Creates a little bio that just does part of a bvec. 10501da177e4SLinus Torvalds */ 10511da177e4SLinus Torvalds static struct bio *split_bvec(struct bio *bio, sector_t sector, 10521da177e4SLinus Torvalds unsigned short idx, unsigned int offset, 10539faf400fSStefan Bader unsigned int len, struct bio_set *bs) 10541da177e4SLinus Torvalds { 10551da177e4SLinus Torvalds struct bio *clone; 10561da177e4SLinus Torvalds struct bio_vec *bv = bio->bi_io_vec + idx; 10571da177e4SLinus Torvalds 10589faf400fSStefan Bader clone = bio_alloc_bioset(GFP_NOIO, 1, bs); 10591da177e4SLinus Torvalds *clone->bi_io_vec = *bv; 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds clone->bi_sector = sector; 10621da177e4SLinus Torvalds clone->bi_bdev = bio->bi_bdev; 1063d87f4c14STejun Heo clone->bi_rw = bio->bi_rw; 10641da177e4SLinus Torvalds clone->bi_vcnt = 1; 10651da177e4SLinus Torvalds clone->bi_size = to_bytes(len); 10661da177e4SLinus Torvalds clone->bi_io_vec->bv_offset = offset; 10671da177e4SLinus Torvalds clone->bi_io_vec->bv_len = clone->bi_size; 1068f3e1d26eSMartin K. Petersen clone->bi_flags |= 1 << BIO_CLONED; 10691da177e4SLinus Torvalds 10709c47008dSMartin K. Petersen if (bio_integrity(bio)) { 10711e2a410fSKent Overstreet bio_integrity_clone(clone, bio, GFP_NOIO); 10729c47008dSMartin K. Petersen bio_integrity_trim(clone, 10739c47008dSMartin K. Petersen bio_sector_offset(bio, idx, offset), len); 10749c47008dSMartin K. Petersen } 10759c47008dSMartin K. Petersen 10761da177e4SLinus Torvalds return clone; 10771da177e4SLinus Torvalds } 10781da177e4SLinus Torvalds 10791da177e4SLinus Torvalds /* 10801da177e4SLinus Torvalds * Creates a bio that consists of range of complete bvecs. 10811da177e4SLinus Torvalds */ 10821da177e4SLinus Torvalds static struct bio *clone_bio(struct bio *bio, sector_t sector, 10831da177e4SLinus Torvalds unsigned short idx, unsigned short bv_count, 10849faf400fSStefan Bader unsigned int len, struct bio_set *bs) 10851da177e4SLinus Torvalds { 10861da177e4SLinus Torvalds struct bio *clone; 10871da177e4SLinus Torvalds 10889faf400fSStefan Bader clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs); 10899faf400fSStefan Bader __bio_clone(clone, bio); 10901da177e4SLinus Torvalds clone->bi_sector = sector; 10911da177e4SLinus Torvalds clone->bi_idx = idx; 10921da177e4SLinus Torvalds clone->bi_vcnt = idx + bv_count; 10931da177e4SLinus Torvalds clone->bi_size = to_bytes(len); 10941da177e4SLinus Torvalds clone->bi_flags &= ~(1 << BIO_SEG_VALID); 10951da177e4SLinus Torvalds 10969c47008dSMartin K. Petersen if (bio_integrity(bio)) { 10971e2a410fSKent Overstreet bio_integrity_clone(clone, bio, GFP_NOIO); 10989c47008dSMartin K. Petersen 10999c47008dSMartin K. Petersen if (idx != bio->bi_idx || clone->bi_size < bio->bi_size) 11009c47008dSMartin K. Petersen bio_integrity_trim(clone, 11019c47008dSMartin K. Petersen bio_sector_offset(bio, idx, 0), len); 11029c47008dSMartin K. Petersen } 11039c47008dSMartin K. Petersen 11041da177e4SLinus Torvalds return clone; 11051da177e4SLinus Torvalds } 11061da177e4SLinus Torvalds 11079015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci, 11089015df24SAlasdair G Kergon struct dm_target *ti) 1109f9ab94ceSMikulas Patocka { 11109015df24SAlasdair G Kergon struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO); 1111f9ab94ceSMikulas Patocka 1112f9ab94ceSMikulas Patocka tio->io = ci->io; 1113f9ab94ceSMikulas Patocka tio->ti = ti; 1114f9ab94ceSMikulas Patocka memset(&tio->info, 0, sizeof(tio->info)); 11159015df24SAlasdair G Kergon 11169015df24SAlasdair G Kergon return tio; 11179015df24SAlasdair G Kergon } 11189015df24SAlasdair G Kergon 111906a426ceSMike Snitzer static void __issue_target_request(struct clone_info *ci, struct dm_target *ti, 1120a79245b3SMike Snitzer unsigned request_nr, sector_t len) 11219015df24SAlasdair G Kergon { 11229015df24SAlasdair G Kergon struct dm_target_io *tio = alloc_tio(ci, ti); 11239015df24SAlasdair G Kergon struct bio *clone; 11249015df24SAlasdair G Kergon 112557cba5d3SMike Snitzer tio->info.target_request_nr = request_nr; 1126f9ab94ceSMikulas Patocka 112706a426ceSMike Snitzer /* 112806a426ceSMike Snitzer * Discard requests require the bio's inline iovecs be initialized. 112906a426ceSMike Snitzer * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush 113006a426ceSMike Snitzer * and discard, so no need for concern about wasted bvec allocations. 113106a426ceSMike Snitzer */ 1132*bf800ef1SKent Overstreet clone = bio_clone_bioset(ci->bio, GFP_NOIO, ci->md->bs); 1133*bf800ef1SKent Overstreet 1134a79245b3SMike Snitzer if (len) { 1135a79245b3SMike Snitzer clone->bi_sector = ci->sector; 1136a79245b3SMike Snitzer clone->bi_size = to_bytes(len); 1137a79245b3SMike Snitzer } 1138f9ab94ceSMikulas Patocka 1139f9ab94ceSMikulas Patocka __map_bio(ti, clone, tio); 1140f9ab94ceSMikulas Patocka } 1141f9ab94ceSMikulas Patocka 114206a426ceSMike Snitzer static void __issue_target_requests(struct clone_info *ci, struct dm_target *ti, 1143a79245b3SMike Snitzer unsigned num_requests, sector_t len) 114406a426ceSMike Snitzer { 114506a426ceSMike Snitzer unsigned request_nr; 114606a426ceSMike Snitzer 114706a426ceSMike Snitzer for (request_nr = 0; request_nr < num_requests; request_nr++) 1148a79245b3SMike Snitzer __issue_target_request(ci, ti, request_nr, len); 114906a426ceSMike Snitzer } 115006a426ceSMike Snitzer 1151b372d360SMike Snitzer static int __clone_and_map_empty_flush(struct clone_info *ci) 1152f9ab94ceSMikulas Patocka { 115306a426ceSMike Snitzer unsigned target_nr = 0; 1154f9ab94ceSMikulas Patocka struct dm_target *ti; 1155f9ab94ceSMikulas Patocka 1156b372d360SMike Snitzer BUG_ON(bio_has_data(ci->bio)); 1157f9ab94ceSMikulas Patocka while ((ti = dm_table_get_target(ci->map, target_nr++))) 1158a79245b3SMike Snitzer __issue_target_requests(ci, ti, ti->num_flush_requests, 0); 1159f9ab94ceSMikulas Patocka 1160f9ab94ceSMikulas Patocka return 0; 1161f9ab94ceSMikulas Patocka } 1162f9ab94ceSMikulas Patocka 11635ae89a87SMike Snitzer /* 11645ae89a87SMike Snitzer * Perform all io with a single clone. 11655ae89a87SMike Snitzer */ 11665ae89a87SMike Snitzer static void __clone_and_map_simple(struct clone_info *ci, struct dm_target *ti) 11675ae89a87SMike Snitzer { 11685ae89a87SMike Snitzer struct bio *clone, *bio = ci->bio; 11695ae89a87SMike Snitzer struct dm_target_io *tio; 11705ae89a87SMike Snitzer 11715ae89a87SMike Snitzer tio = alloc_tio(ci, ti); 11725ae89a87SMike Snitzer clone = clone_bio(bio, ci->sector, ci->idx, 11735ae89a87SMike Snitzer bio->bi_vcnt - ci->idx, ci->sector_count, 11745ae89a87SMike Snitzer ci->md->bs); 11755ae89a87SMike Snitzer __map_bio(ti, clone, tio); 11765ae89a87SMike Snitzer ci->sector_count = 0; 11775ae89a87SMike Snitzer } 11785ae89a87SMike Snitzer 11795ae89a87SMike Snitzer static int __clone_and_map_discard(struct clone_info *ci) 11805ae89a87SMike Snitzer { 11815ae89a87SMike Snitzer struct dm_target *ti; 1182a79245b3SMike Snitzer sector_t len; 11835ae89a87SMike Snitzer 1184a79245b3SMike Snitzer do { 11855ae89a87SMike Snitzer ti = dm_table_find_target(ci->map, ci->sector); 11865ae89a87SMike Snitzer if (!dm_target_is_valid(ti)) 11875ae89a87SMike Snitzer return -EIO; 11885ae89a87SMike Snitzer 11895ae89a87SMike Snitzer /* 11905ae89a87SMike Snitzer * Even though the device advertised discard support, 1191936688d7SMike Snitzer * that does not mean every target supports it, and 1192936688d7SMike Snitzer * reconfiguration might also have changed that since the 11935ae89a87SMike Snitzer * check was performed. 11945ae89a87SMike Snitzer */ 11955ae89a87SMike Snitzer if (!ti->num_discard_requests) 11965ae89a87SMike Snitzer return -EOPNOTSUPP; 11975ae89a87SMike Snitzer 11987acf0277SMikulas Patocka if (!ti->split_discard_requests) 1199a79245b3SMike Snitzer len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti)); 12007acf0277SMikulas Patocka else 12017acf0277SMikulas Patocka len = min(ci->sector_count, max_io_len(ci->sector, ti)); 12025ae89a87SMike Snitzer 1203a79245b3SMike Snitzer __issue_target_requests(ci, ti, ti->num_discard_requests, len); 12045ae89a87SMike Snitzer 1205a79245b3SMike Snitzer ci->sector += len; 1206a79245b3SMike Snitzer } while (ci->sector_count -= len); 12075ae89a87SMike Snitzer 12085ae89a87SMike Snitzer return 0; 12095ae89a87SMike Snitzer } 12105ae89a87SMike Snitzer 1211512875bdSJun'ichi Nomura static int __clone_and_map(struct clone_info *ci) 12121da177e4SLinus Torvalds { 12131da177e4SLinus Torvalds struct bio *clone, *bio = ci->bio; 1214512875bdSJun'ichi Nomura struct dm_target *ti; 1215512875bdSJun'ichi Nomura sector_t len = 0, max; 1216028867acSAlasdair G Kergon struct dm_target_io *tio; 12171da177e4SLinus Torvalds 12185ae89a87SMike Snitzer if (unlikely(bio->bi_rw & REQ_DISCARD)) 12195ae89a87SMike Snitzer return __clone_and_map_discard(ci); 12205ae89a87SMike Snitzer 1221512875bdSJun'ichi Nomura ti = dm_table_find_target(ci->map, ci->sector); 1222512875bdSJun'ichi Nomura if (!dm_target_is_valid(ti)) 1223512875bdSJun'ichi Nomura return -EIO; 1224512875bdSJun'ichi Nomura 122556a67df7SMike Snitzer max = max_io_len(ci->sector, ti); 1226512875bdSJun'ichi Nomura 12271da177e4SLinus Torvalds if (ci->sector_count <= max) { 12281da177e4SLinus Torvalds /* 12291da177e4SLinus Torvalds * Optimise for the simple case where we can do all of 12301da177e4SLinus Torvalds * the remaining io with a single clone. 12311da177e4SLinus Torvalds */ 12325ae89a87SMike Snitzer __clone_and_map_simple(ci, ti); 12331da177e4SLinus Torvalds 12341da177e4SLinus Torvalds } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) { 12351da177e4SLinus Torvalds /* 12361da177e4SLinus Torvalds * There are some bvecs that don't span targets. 12371da177e4SLinus Torvalds * Do as many of these as possible. 12381da177e4SLinus Torvalds */ 12391da177e4SLinus Torvalds int i; 12401da177e4SLinus Torvalds sector_t remaining = max; 12411da177e4SLinus Torvalds sector_t bv_len; 12421da177e4SLinus Torvalds 12431da177e4SLinus Torvalds for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) { 12441da177e4SLinus Torvalds bv_len = to_sector(bio->bi_io_vec[i].bv_len); 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds if (bv_len > remaining) 12471da177e4SLinus Torvalds break; 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds remaining -= bv_len; 12501da177e4SLinus Torvalds len += bv_len; 12511da177e4SLinus Torvalds } 12521da177e4SLinus Torvalds 12535ae89a87SMike Snitzer tio = alloc_tio(ci, ti); 12549faf400fSStefan Bader clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len, 12559faf400fSStefan Bader ci->md->bs); 12561da177e4SLinus Torvalds __map_bio(ti, clone, tio); 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds ci->sector += len; 12591da177e4SLinus Torvalds ci->sector_count -= len; 12601da177e4SLinus Torvalds ci->idx = i; 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds } else { 12631da177e4SLinus Torvalds /* 1264d2044a94SAlasdair G Kergon * Handle a bvec that must be split between two or more targets. 12651da177e4SLinus Torvalds */ 12661da177e4SLinus Torvalds struct bio_vec *bv = bio->bi_io_vec + ci->idx; 1267d2044a94SAlasdair G Kergon sector_t remaining = to_sector(bv->bv_len); 1268d2044a94SAlasdair G Kergon unsigned int offset = 0; 12691da177e4SLinus Torvalds 1270d2044a94SAlasdair G Kergon do { 1271d2044a94SAlasdair G Kergon if (offset) { 12721da177e4SLinus Torvalds ti = dm_table_find_target(ci->map, ci->sector); 1273512875bdSJun'ichi Nomura if (!dm_target_is_valid(ti)) 1274512875bdSJun'ichi Nomura return -EIO; 1275512875bdSJun'ichi Nomura 127656a67df7SMike Snitzer max = max_io_len(ci->sector, ti); 1277d2044a94SAlasdair G Kergon } 1278d2044a94SAlasdair G Kergon 1279d2044a94SAlasdair G Kergon len = min(remaining, max); 1280d2044a94SAlasdair G Kergon 12815ae89a87SMike Snitzer tio = alloc_tio(ci, ti); 1282d2044a94SAlasdair G Kergon clone = split_bvec(bio, ci->sector, ci->idx, 12839faf400fSStefan Bader bv->bv_offset + offset, len, 12849faf400fSStefan Bader ci->md->bs); 1285d2044a94SAlasdair G Kergon 12861da177e4SLinus Torvalds __map_bio(ti, clone, tio); 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds ci->sector += len; 12891da177e4SLinus Torvalds ci->sector_count -= len; 1290d2044a94SAlasdair G Kergon offset += to_bytes(len); 1291d2044a94SAlasdair G Kergon } while (remaining -= len); 1292d2044a94SAlasdair G Kergon 12931da177e4SLinus Torvalds ci->idx++; 12941da177e4SLinus Torvalds } 1295512875bdSJun'ichi Nomura 1296512875bdSJun'ichi Nomura return 0; 12971da177e4SLinus Torvalds } 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds /* 13008a53c28dSMikulas Patocka * Split the bio into several clones and submit it to targets. 13011da177e4SLinus Torvalds */ 1302f0b9a450SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md, struct bio *bio) 13031da177e4SLinus Torvalds { 13041da177e4SLinus Torvalds struct clone_info ci; 1305512875bdSJun'ichi Nomura int error = 0; 13061da177e4SLinus Torvalds 13077c666411SAlasdair G Kergon ci.map = dm_get_live_table(md); 1308f0b9a450SMikulas Patocka if (unlikely(!ci.map)) { 1309f0b9a450SMikulas Patocka bio_io_error(bio); 1310f0b9a450SMikulas Patocka return; 1311f0b9a450SMikulas Patocka } 1312692d0eb9SMikulas Patocka 13131da177e4SLinus Torvalds ci.md = md; 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.idx = bio->bi_idx; 13221da177e4SLinus Torvalds 13233eaf840eSJun'ichi "Nick" Nomura start_io_acct(ci.io); 1324b372d360SMike Snitzer if (bio->bi_rw & REQ_FLUSH) { 1325b372d360SMike Snitzer ci.bio = &ci.md->flush_bio; 1326b372d360SMike Snitzer ci.sector_count = 0; 1327b372d360SMike Snitzer error = __clone_and_map_empty_flush(&ci); 1328b372d360SMike Snitzer /* dec_pending submits any data associated with flush */ 1329b372d360SMike Snitzer } else { 13306a8736d1STejun Heo ci.bio = bio; 1331f6fccb12SMilan Broz ci.sector_count = bio_sectors(bio); 1332512875bdSJun'ichi Nomura while (ci.sector_count && !error) 1333512875bdSJun'ichi Nomura error = __clone_and_map(&ci); 1334d87f4c14STejun Heo } 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds /* drop the extra reference count */ 1337512875bdSJun'ichi Nomura dec_pending(ci.io, error); 13381da177e4SLinus Torvalds dm_table_put(ci.map); 13399e4e5f87SMilan Broz } 13409e4e5f87SMilan Broz /*----------------------------------------------------------------- 13411da177e4SLinus Torvalds * CRUD END 13421da177e4SLinus Torvalds *---------------------------------------------------------------*/ 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds static int dm_merge_bvec(struct request_queue *q, 13451da177e4SLinus Torvalds struct bvec_merge_data *bvm, 1346f6fccb12SMilan Broz struct bio_vec *biovec) 1347f6fccb12SMilan Broz { 1348f6fccb12SMilan Broz struct mapped_device *md = q->queuedata; 13497c666411SAlasdair G Kergon struct dm_table *map = dm_get_live_table(md); 1350f6fccb12SMilan Broz struct dm_target *ti; 1351f6fccb12SMilan Broz sector_t max_sectors; 1352f6fccb12SMilan Broz int max_size = 0; 1353f6fccb12SMilan Broz 1354f6fccb12SMilan Broz if (unlikely(!map)) 1355f6fccb12SMilan Broz goto out; 1356f6fccb12SMilan Broz 1357f6fccb12SMilan Broz ti = dm_table_find_target(map, bvm->bi_sector); 1358f6fccb12SMilan Broz if (!dm_target_is_valid(ti)) 1359f6fccb12SMilan Broz goto out_table; 1360f6fccb12SMilan Broz 1361f6fccb12SMilan Broz /* 1362f6fccb12SMilan Broz * Find maximum amount of I/O that won't need splitting 1363f6fccb12SMilan Broz */ 136456a67df7SMike Snitzer max_sectors = min(max_io_len(bvm->bi_sector, ti), 1365f6fccb12SMilan Broz (sector_t) BIO_MAX_SECTORS); 1366f6fccb12SMilan Broz max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size; 1367f6fccb12SMilan Broz if (max_size < 0) 1368f6fccb12SMilan Broz max_size = 0; 1369f6fccb12SMilan Broz 1370f6fccb12SMilan Broz /* 1371f6fccb12SMilan Broz * merge_bvec_fn() returns number of bytes 1372f6fccb12SMilan Broz * it can accept at this offset 1373f6fccb12SMilan Broz * max is precomputed maximal io size 1374f6fccb12SMilan Broz */ 1375f6fccb12SMilan Broz if (max_size && ti->type->merge) 1376f6fccb12SMilan Broz max_size = ti->type->merge(ti, bvm, biovec, max_size); 13778cbeb67aSMikulas Patocka /* 13788cbeb67aSMikulas Patocka * If the target doesn't support merge method and some of the devices 13798cbeb67aSMikulas Patocka * provided their merge_bvec method (we know this by looking at 13808cbeb67aSMikulas Patocka * queue_max_hw_sectors), then we can't allow bios with multiple vector 13818cbeb67aSMikulas Patocka * entries. So always set max_size to 0, and the code below allows 13828cbeb67aSMikulas Patocka * just one page. 13838cbeb67aSMikulas Patocka */ 13848cbeb67aSMikulas Patocka else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9) 13858cbeb67aSMikulas Patocka 13868cbeb67aSMikulas Patocka max_size = 0; 1387f6fccb12SMilan Broz 1388b01cd5acSMikulas Patocka out_table: 13895037108aSMikulas Patocka dm_table_put(map); 13905037108aSMikulas Patocka 13915037108aSMikulas Patocka out: 1392f6fccb12SMilan Broz /* 1393f6fccb12SMilan Broz * Always allow an entire first page 1394f6fccb12SMilan Broz */ 1395f6fccb12SMilan Broz if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT)) 1396f6fccb12SMilan Broz max_size = biovec->bv_len; 1397f6fccb12SMilan Broz 1398f6fccb12SMilan Broz return max_size; 1399f6fccb12SMilan Broz } 1400f6fccb12SMilan Broz 14011da177e4SLinus Torvalds /* 14021da177e4SLinus Torvalds * The request function that just remaps the bio built up by 14031da177e4SLinus Torvalds * dm_merge_bvec. 14041da177e4SLinus Torvalds */ 14055a7bbad2SChristoph Hellwig static void _dm_request(struct request_queue *q, struct bio *bio) 14061da177e4SLinus Torvalds { 140712f03a49SKevin Corry int rw = bio_data_dir(bio); 14081da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 1409c9959059STejun Heo int cpu; 14101da177e4SLinus Torvalds 14112ca3310eSAlasdair G Kergon down_read(&md->io_lock); 14121da177e4SLinus Torvalds 1413074a7acaSTejun Heo cpu = part_stat_lock(); 1414074a7acaSTejun Heo part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]); 1415074a7acaSTejun Heo part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio)); 1416074a7acaSTejun Heo part_stat_unlock(); 141712f03a49SKevin Corry 14186a8736d1STejun Heo /* if we're suspended, we have to queue this io for later */ 14196a8736d1STejun Heo if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) { 14202ca3310eSAlasdair G Kergon up_read(&md->io_lock); 14211da177e4SLinus Torvalds 14226a8736d1STejun Heo if (bio_rw(bio) != READA) 142392c63902SMikulas Patocka queue_io(md, bio); 14246a8736d1STejun Heo else 14256a8736d1STejun Heo bio_io_error(bio); 14265a7bbad2SChristoph Hellwig return; 14271da177e4SLinus Torvalds } 14281da177e4SLinus Torvalds 1429f0b9a450SMikulas Patocka __split_and_process_bio(md, bio); 14302ca3310eSAlasdair G Kergon up_read(&md->io_lock); 14315a7bbad2SChristoph Hellwig return; 1432cec47e3dSKiyoshi Ueda } 1433cec47e3dSKiyoshi Ueda 1434cec47e3dSKiyoshi Ueda static int dm_request_based(struct mapped_device *md) 1435cec47e3dSKiyoshi Ueda { 1436cec47e3dSKiyoshi Ueda return blk_queue_stackable(md->queue); 1437cec47e3dSKiyoshi Ueda } 1438cec47e3dSKiyoshi Ueda 14395a7bbad2SChristoph Hellwig static void dm_request(struct request_queue *q, struct bio *bio) 1440cec47e3dSKiyoshi Ueda { 1441cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 1442cec47e3dSKiyoshi Ueda 1443cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 14445a7bbad2SChristoph Hellwig blk_queue_bio(q, bio); 14455a7bbad2SChristoph Hellwig else 14465a7bbad2SChristoph Hellwig _dm_request(q, bio); 1447cec47e3dSKiyoshi Ueda } 1448cec47e3dSKiyoshi Ueda 1449cec47e3dSKiyoshi Ueda void dm_dispatch_request(struct request *rq) 1450cec47e3dSKiyoshi Ueda { 1451cec47e3dSKiyoshi Ueda int r; 1452cec47e3dSKiyoshi Ueda 1453cec47e3dSKiyoshi Ueda if (blk_queue_io_stat(rq->q)) 1454cec47e3dSKiyoshi Ueda rq->cmd_flags |= REQ_IO_STAT; 1455cec47e3dSKiyoshi Ueda 1456cec47e3dSKiyoshi Ueda rq->start_time = jiffies; 1457cec47e3dSKiyoshi Ueda r = blk_insert_cloned_request(rq->q, rq); 1458cec47e3dSKiyoshi Ueda if (r) 1459cec47e3dSKiyoshi Ueda dm_complete_request(rq, r); 1460cec47e3dSKiyoshi Ueda } 1461cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_dispatch_request); 1462cec47e3dSKiyoshi Ueda 1463cec47e3dSKiyoshi Ueda static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig, 1464cec47e3dSKiyoshi Ueda void *data) 1465cec47e3dSKiyoshi Ueda { 1466cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = data; 146794818742SKent Overstreet struct dm_rq_clone_bio_info *info = 146894818742SKent Overstreet container_of(bio, struct dm_rq_clone_bio_info, clone); 1469cec47e3dSKiyoshi Ueda 1470cec47e3dSKiyoshi Ueda info->orig = bio_orig; 1471cec47e3dSKiyoshi Ueda info->tio = tio; 1472cec47e3dSKiyoshi Ueda bio->bi_end_io = end_clone_bio; 1473cec47e3dSKiyoshi Ueda bio->bi_private = info; 1474cec47e3dSKiyoshi Ueda 1475cec47e3dSKiyoshi Ueda return 0; 1476cec47e3dSKiyoshi Ueda } 1477cec47e3dSKiyoshi Ueda 1478cec47e3dSKiyoshi Ueda static int setup_clone(struct request *clone, struct request *rq, 1479cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio) 1480cec47e3dSKiyoshi Ueda { 1481d0bcb878SKiyoshi Ueda int r; 1482cec47e3dSKiyoshi Ueda 1483d0bcb878SKiyoshi Ueda r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC, 1484d0bcb878SKiyoshi Ueda dm_rq_bio_constructor, tio); 1485cec47e3dSKiyoshi Ueda if (r) 1486cec47e3dSKiyoshi Ueda return r; 1487cec47e3dSKiyoshi Ueda 1488cec47e3dSKiyoshi Ueda clone->cmd = rq->cmd; 1489cec47e3dSKiyoshi Ueda clone->cmd_len = rq->cmd_len; 1490cec47e3dSKiyoshi Ueda clone->sense = rq->sense; 1491cec47e3dSKiyoshi Ueda clone->buffer = rq->buffer; 1492cec47e3dSKiyoshi Ueda clone->end_io = end_clone_request; 1493cec47e3dSKiyoshi Ueda clone->end_io_data = tio; 1494cec47e3dSKiyoshi Ueda 1495cec47e3dSKiyoshi Ueda return 0; 1496cec47e3dSKiyoshi Ueda } 1497cec47e3dSKiyoshi Ueda 14986facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md, 14996facdaffSKiyoshi Ueda gfp_t gfp_mask) 15006facdaffSKiyoshi Ueda { 15016facdaffSKiyoshi Ueda struct request *clone; 15026facdaffSKiyoshi Ueda struct dm_rq_target_io *tio; 15036facdaffSKiyoshi Ueda 15046facdaffSKiyoshi Ueda tio = alloc_rq_tio(md, gfp_mask); 15056facdaffSKiyoshi Ueda if (!tio) 15066facdaffSKiyoshi Ueda return NULL; 15076facdaffSKiyoshi Ueda 15086facdaffSKiyoshi Ueda tio->md = md; 15096facdaffSKiyoshi Ueda tio->ti = NULL; 15106facdaffSKiyoshi Ueda tio->orig = rq; 15116facdaffSKiyoshi Ueda tio->error = 0; 15126facdaffSKiyoshi Ueda memset(&tio->info, 0, sizeof(tio->info)); 15136facdaffSKiyoshi Ueda 15146facdaffSKiyoshi Ueda clone = &tio->clone; 15156facdaffSKiyoshi Ueda if (setup_clone(clone, rq, tio)) { 15166facdaffSKiyoshi Ueda /* -ENOMEM */ 15176facdaffSKiyoshi Ueda free_rq_tio(tio); 15186facdaffSKiyoshi Ueda return NULL; 15196facdaffSKiyoshi Ueda } 15206facdaffSKiyoshi Ueda 15216facdaffSKiyoshi Ueda return clone; 15226facdaffSKiyoshi Ueda } 15236facdaffSKiyoshi Ueda 1524cec47e3dSKiyoshi Ueda /* 1525cec47e3dSKiyoshi Ueda * Called with the queue lock held. 1526cec47e3dSKiyoshi Ueda */ 1527cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq) 1528cec47e3dSKiyoshi Ueda { 1529cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 1530cec47e3dSKiyoshi Ueda struct request *clone; 1531cec47e3dSKiyoshi Ueda 1532cec47e3dSKiyoshi Ueda if (unlikely(rq->special)) { 1533cec47e3dSKiyoshi Ueda DMWARN("Already has something in rq->special."); 1534cec47e3dSKiyoshi Ueda return BLKPREP_KILL; 1535cec47e3dSKiyoshi Ueda } 1536cec47e3dSKiyoshi Ueda 15376facdaffSKiyoshi Ueda clone = clone_rq(rq, md, GFP_ATOMIC); 15386facdaffSKiyoshi Ueda if (!clone) 1539cec47e3dSKiyoshi Ueda return BLKPREP_DEFER; 1540cec47e3dSKiyoshi Ueda 1541cec47e3dSKiyoshi Ueda rq->special = clone; 1542cec47e3dSKiyoshi Ueda rq->cmd_flags |= REQ_DONTPREP; 1543cec47e3dSKiyoshi Ueda 1544cec47e3dSKiyoshi Ueda return BLKPREP_OK; 1545cec47e3dSKiyoshi Ueda } 1546cec47e3dSKiyoshi Ueda 15479eef87daSKiyoshi Ueda /* 15489eef87daSKiyoshi Ueda * Returns: 15499eef87daSKiyoshi Ueda * 0 : the request has been processed (not requeued) 15509eef87daSKiyoshi Ueda * !0 : the request has been requeued 15519eef87daSKiyoshi Ueda */ 15529eef87daSKiyoshi Ueda static int map_request(struct dm_target *ti, struct request *clone, 1553cec47e3dSKiyoshi Ueda struct mapped_device *md) 1554cec47e3dSKiyoshi Ueda { 15559eef87daSKiyoshi Ueda int r, requeued = 0; 1556cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 1557cec47e3dSKiyoshi Ueda 1558cec47e3dSKiyoshi Ueda /* 1559cec47e3dSKiyoshi Ueda * Hold the md reference here for the in-flight I/O. 1560cec47e3dSKiyoshi Ueda * We can't rely on the reference count by device opener, 1561cec47e3dSKiyoshi Ueda * because the device may be closed during the request completion 1562cec47e3dSKiyoshi Ueda * when all bios are completed. 1563cec47e3dSKiyoshi Ueda * See the comment in rq_completed() too. 1564cec47e3dSKiyoshi Ueda */ 1565cec47e3dSKiyoshi Ueda dm_get(md); 1566cec47e3dSKiyoshi Ueda 1567cec47e3dSKiyoshi Ueda tio->ti = ti; 1568cec47e3dSKiyoshi Ueda r = ti->type->map_rq(ti, clone, &tio->info); 1569cec47e3dSKiyoshi Ueda switch (r) { 1570cec47e3dSKiyoshi Ueda case DM_MAPIO_SUBMITTED: 1571cec47e3dSKiyoshi Ueda /* The target has taken the I/O to submit by itself later */ 1572cec47e3dSKiyoshi Ueda break; 1573cec47e3dSKiyoshi Ueda case DM_MAPIO_REMAPPED: 1574cec47e3dSKiyoshi Ueda /* The target has remapped the I/O so dispatch it */ 15756db4ccd6SJun'ichi Nomura trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)), 15766db4ccd6SJun'ichi Nomura blk_rq_pos(tio->orig)); 1577cec47e3dSKiyoshi Ueda dm_dispatch_request(clone); 1578cec47e3dSKiyoshi Ueda break; 1579cec47e3dSKiyoshi Ueda case DM_MAPIO_REQUEUE: 1580cec47e3dSKiyoshi Ueda /* The target wants to requeue the I/O */ 1581cec47e3dSKiyoshi Ueda dm_requeue_unmapped_request(clone); 15829eef87daSKiyoshi Ueda requeued = 1; 1583cec47e3dSKiyoshi Ueda break; 1584cec47e3dSKiyoshi Ueda default: 1585cec47e3dSKiyoshi Ueda if (r > 0) { 1586cec47e3dSKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 1587cec47e3dSKiyoshi Ueda BUG(); 1588cec47e3dSKiyoshi Ueda } 1589cec47e3dSKiyoshi Ueda 1590cec47e3dSKiyoshi Ueda /* The target wants to complete the I/O */ 1591cec47e3dSKiyoshi Ueda dm_kill_unmapped_request(clone, r); 1592cec47e3dSKiyoshi Ueda break; 1593cec47e3dSKiyoshi Ueda } 15949eef87daSKiyoshi Ueda 15959eef87daSKiyoshi Ueda return requeued; 1596cec47e3dSKiyoshi Ueda } 1597cec47e3dSKiyoshi Ueda 1598cec47e3dSKiyoshi Ueda /* 1599cec47e3dSKiyoshi Ueda * q->request_fn for request-based dm. 1600cec47e3dSKiyoshi Ueda * Called with the queue lock held. 1601cec47e3dSKiyoshi Ueda */ 1602cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q) 1603cec47e3dSKiyoshi Ueda { 1604cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 16057c666411SAlasdair G Kergon struct dm_table *map = dm_get_live_table(md); 1606cec47e3dSKiyoshi Ueda struct dm_target *ti; 1607b4324feeSKiyoshi Ueda struct request *rq, *clone; 160829e4013dSTejun Heo sector_t pos; 1609cec47e3dSKiyoshi Ueda 1610cec47e3dSKiyoshi Ueda /* 1611b4324feeSKiyoshi Ueda * For suspend, check blk_queue_stopped() and increment 1612b4324feeSKiyoshi Ueda * ->pending within a single queue_lock not to increment the 1613b4324feeSKiyoshi Ueda * number of in-flight I/Os after the queue is stopped in 1614b4324feeSKiyoshi Ueda * dm_suspend(). 1615cec47e3dSKiyoshi Ueda */ 16167eaceaccSJens Axboe while (!blk_queue_stopped(q)) { 1617cec47e3dSKiyoshi Ueda rq = blk_peek_request(q); 1618cec47e3dSKiyoshi Ueda if (!rq) 16197eaceaccSJens Axboe goto delay_and_out; 1620cec47e3dSKiyoshi Ueda 162129e4013dSTejun Heo /* always use block 0 to find the target for flushes for now */ 162229e4013dSTejun Heo pos = 0; 162329e4013dSTejun Heo if (!(rq->cmd_flags & REQ_FLUSH)) 162429e4013dSTejun Heo pos = blk_rq_pos(rq); 1625d0bcb878SKiyoshi Ueda 162629e4013dSTejun Heo ti = dm_table_find_target(map, pos); 162729e4013dSTejun Heo BUG_ON(!dm_target_is_valid(ti)); 162829e4013dSTejun Heo 1629cec47e3dSKiyoshi Ueda if (ti->type->busy && ti->type->busy(ti)) 16307eaceaccSJens Axboe goto delay_and_out; 1631cec47e3dSKiyoshi Ueda 1632cec47e3dSKiyoshi Ueda blk_start_request(rq); 1633b4324feeSKiyoshi Ueda clone = rq->special; 1634b4324feeSKiyoshi Ueda atomic_inc(&md->pending[rq_data_dir(clone)]); 1635b4324feeSKiyoshi Ueda 1636cec47e3dSKiyoshi Ueda spin_unlock(q->queue_lock); 16379eef87daSKiyoshi Ueda if (map_request(ti, clone, md)) 16389eef87daSKiyoshi Ueda goto requeued; 16399eef87daSKiyoshi Ueda 1640052189a2SKiyoshi Ueda BUG_ON(!irqs_disabled()); 1641052189a2SKiyoshi Ueda spin_lock(q->queue_lock); 1642cec47e3dSKiyoshi Ueda } 1643cec47e3dSKiyoshi Ueda 1644cec47e3dSKiyoshi Ueda goto out; 1645cec47e3dSKiyoshi Ueda 16469eef87daSKiyoshi Ueda requeued: 1647052189a2SKiyoshi Ueda BUG_ON(!irqs_disabled()); 1648052189a2SKiyoshi Ueda spin_lock(q->queue_lock); 16499eef87daSKiyoshi Ueda 16507eaceaccSJens Axboe delay_and_out: 16517eaceaccSJens Axboe blk_delay_queue(q, HZ / 10); 1652cec47e3dSKiyoshi Ueda out: 1653cec47e3dSKiyoshi Ueda dm_table_put(map); 1654cec47e3dSKiyoshi Ueda 1655cec47e3dSKiyoshi Ueda return; 1656cec47e3dSKiyoshi Ueda } 1657cec47e3dSKiyoshi Ueda 1658cec47e3dSKiyoshi Ueda int dm_underlying_device_busy(struct request_queue *q) 1659cec47e3dSKiyoshi Ueda { 1660cec47e3dSKiyoshi Ueda return blk_lld_busy(q); 1661cec47e3dSKiyoshi Ueda } 1662cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_underlying_device_busy); 1663cec47e3dSKiyoshi Ueda 1664cec47e3dSKiyoshi Ueda static int dm_lld_busy(struct request_queue *q) 1665cec47e3dSKiyoshi Ueda { 1666cec47e3dSKiyoshi Ueda int r; 1667cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 16687c666411SAlasdair G Kergon struct dm_table *map = dm_get_live_table(md); 1669cec47e3dSKiyoshi Ueda 1670cec47e3dSKiyoshi Ueda if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) 1671cec47e3dSKiyoshi Ueda r = 1; 1672cec47e3dSKiyoshi Ueda else 1673cec47e3dSKiyoshi Ueda r = dm_table_any_busy_target(map); 1674cec47e3dSKiyoshi Ueda 1675cec47e3dSKiyoshi Ueda dm_table_put(map); 1676cec47e3dSKiyoshi Ueda 1677cec47e3dSKiyoshi Ueda return r; 1678cec47e3dSKiyoshi Ueda } 1679cec47e3dSKiyoshi Ueda 16801da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits) 16811da177e4SLinus Torvalds { 16828a57dfc6SChandra Seetharaman int r = bdi_bits; 16838a57dfc6SChandra Seetharaman struct mapped_device *md = congested_data; 16848a57dfc6SChandra Seetharaman struct dm_table *map; 16851da177e4SLinus Torvalds 16861eb787ecSAlasdair G Kergon if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 16877c666411SAlasdair G Kergon map = dm_get_live_table(md); 16888a57dfc6SChandra Seetharaman if (map) { 1689cec47e3dSKiyoshi Ueda /* 1690cec47e3dSKiyoshi Ueda * Request-based dm cares about only own queue for 1691cec47e3dSKiyoshi Ueda * the query about congestion status of request_queue 1692cec47e3dSKiyoshi Ueda */ 1693cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 1694cec47e3dSKiyoshi Ueda r = md->queue->backing_dev_info.state & 1695cec47e3dSKiyoshi Ueda bdi_bits; 1696cec47e3dSKiyoshi Ueda else 16971da177e4SLinus Torvalds r = dm_table_any_congested(map, bdi_bits); 1698cec47e3dSKiyoshi Ueda 16991da177e4SLinus Torvalds dm_table_put(map); 17008a57dfc6SChandra Seetharaman } 17018a57dfc6SChandra Seetharaman } 17028a57dfc6SChandra Seetharaman 17031da177e4SLinus Torvalds return r; 17041da177e4SLinus Torvalds } 17051da177e4SLinus Torvalds 17061da177e4SLinus Torvalds /*----------------------------------------------------------------- 17071da177e4SLinus Torvalds * An IDR is used to keep track of allocated minor numbers. 17081da177e4SLinus Torvalds *---------------------------------------------------------------*/ 17092b06cfffSAlasdair G Kergon static void free_minor(int minor) 17101da177e4SLinus Torvalds { 1711f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 17121da177e4SLinus Torvalds idr_remove(&_minor_idr, minor); 1713f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 17141da177e4SLinus Torvalds } 17151da177e4SLinus Torvalds 17161da177e4SLinus Torvalds /* 17171da177e4SLinus Torvalds * See if the device with a specific minor # is free. 17181da177e4SLinus Torvalds */ 1719cf13ab8eSFrederik Deweerdt static int specific_minor(int minor) 17201da177e4SLinus Torvalds { 17211da177e4SLinus Torvalds int r, m; 17221da177e4SLinus Torvalds 17231da177e4SLinus Torvalds if (minor >= (1 << MINORBITS)) 17241da177e4SLinus Torvalds return -EINVAL; 17251da177e4SLinus Torvalds 172662f75c2fSJeff Mahoney r = idr_pre_get(&_minor_idr, GFP_KERNEL); 172762f75c2fSJeff Mahoney if (!r) 172862f75c2fSJeff Mahoney return -ENOMEM; 172962f75c2fSJeff Mahoney 1730f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 17311da177e4SLinus Torvalds 17321da177e4SLinus Torvalds if (idr_find(&_minor_idr, minor)) { 17331da177e4SLinus Torvalds r = -EBUSY; 17341da177e4SLinus Torvalds goto out; 17351da177e4SLinus Torvalds } 17361da177e4SLinus Torvalds 1737ba61fdd1SJeff Mahoney r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m); 173862f75c2fSJeff Mahoney if (r) 17391da177e4SLinus Torvalds goto out; 17401da177e4SLinus Torvalds 17411da177e4SLinus Torvalds if (m != minor) { 17421da177e4SLinus Torvalds idr_remove(&_minor_idr, m); 17431da177e4SLinus Torvalds r = -EBUSY; 17441da177e4SLinus Torvalds goto out; 17451da177e4SLinus Torvalds } 17461da177e4SLinus Torvalds 17471da177e4SLinus Torvalds out: 1748f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 17491da177e4SLinus Torvalds return r; 17501da177e4SLinus Torvalds } 17511da177e4SLinus Torvalds 1752cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor) 17531da177e4SLinus Torvalds { 17542b06cfffSAlasdair G Kergon int r, m; 17551da177e4SLinus Torvalds 17561da177e4SLinus Torvalds r = idr_pre_get(&_minor_idr, GFP_KERNEL); 175762f75c2fSJeff Mahoney if (!r) 175862f75c2fSJeff Mahoney return -ENOMEM; 175962f75c2fSJeff Mahoney 1760f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 17611da177e4SLinus Torvalds 1762ba61fdd1SJeff Mahoney r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m); 1763cf13ab8eSFrederik Deweerdt if (r) 17641da177e4SLinus Torvalds goto out; 17651da177e4SLinus Torvalds 17661da177e4SLinus Torvalds if (m >= (1 << MINORBITS)) { 17671da177e4SLinus Torvalds idr_remove(&_minor_idr, m); 17681da177e4SLinus Torvalds r = -ENOSPC; 17691da177e4SLinus Torvalds goto out; 17701da177e4SLinus Torvalds } 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds *minor = m; 17731da177e4SLinus Torvalds 17741da177e4SLinus Torvalds out: 1775f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 17761da177e4SLinus Torvalds return r; 17771da177e4SLinus Torvalds } 17781da177e4SLinus Torvalds 177983d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops; 17801da177e4SLinus Torvalds 178153d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work); 178253d5914fSMikulas Patocka 17834a0b4ddfSMike Snitzer static void dm_init_md_queue(struct mapped_device *md) 17844a0b4ddfSMike Snitzer { 17854a0b4ddfSMike Snitzer /* 17864a0b4ddfSMike Snitzer * Request-based dm devices cannot be stacked on top of bio-based dm 17874a0b4ddfSMike Snitzer * devices. The type of this dm device has not been decided yet. 17884a0b4ddfSMike Snitzer * The type is decided at the first table loading time. 17894a0b4ddfSMike Snitzer * To prevent problematic device stacking, clear the queue flag 17904a0b4ddfSMike Snitzer * for request stacking support until then. 17914a0b4ddfSMike Snitzer * 17924a0b4ddfSMike Snitzer * This queue is new, so no concurrency on the queue_flags. 17934a0b4ddfSMike Snitzer */ 17944a0b4ddfSMike Snitzer queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue); 17954a0b4ddfSMike Snitzer 17964a0b4ddfSMike Snitzer md->queue->queuedata = md; 17974a0b4ddfSMike Snitzer md->queue->backing_dev_info.congested_fn = dm_any_congested; 17984a0b4ddfSMike Snitzer md->queue->backing_dev_info.congested_data = md; 17994a0b4ddfSMike Snitzer blk_queue_make_request(md->queue, dm_request); 18004a0b4ddfSMike Snitzer blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY); 18014a0b4ddfSMike Snitzer blk_queue_merge_bvec(md->queue, dm_merge_bvec); 18024a0b4ddfSMike Snitzer } 18034a0b4ddfSMike Snitzer 18041da177e4SLinus Torvalds /* 18051da177e4SLinus Torvalds * Allocate and initialise a blank device with a given minor. 18061da177e4SLinus Torvalds */ 18072b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor) 18081da177e4SLinus Torvalds { 18091da177e4SLinus Torvalds int r; 1810cf13ab8eSFrederik Deweerdt struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL); 1811ba61fdd1SJeff Mahoney void *old_md; 18121da177e4SLinus Torvalds 18131da177e4SLinus Torvalds if (!md) { 18141da177e4SLinus Torvalds DMWARN("unable to allocate device, out of memory."); 18151da177e4SLinus Torvalds return NULL; 18161da177e4SLinus Torvalds } 18171da177e4SLinus Torvalds 181810da4f79SJeff Mahoney if (!try_module_get(THIS_MODULE)) 18196ed7ade8SMilan Broz goto bad_module_get; 182010da4f79SJeff Mahoney 18211da177e4SLinus Torvalds /* get a minor number for the dev */ 18222b06cfffSAlasdair G Kergon if (minor == DM_ANY_MINOR) 1823cf13ab8eSFrederik Deweerdt r = next_free_minor(&minor); 18242b06cfffSAlasdair G Kergon else 1825cf13ab8eSFrederik Deweerdt r = specific_minor(minor); 18261da177e4SLinus Torvalds if (r < 0) 18276ed7ade8SMilan Broz goto bad_minor; 18281da177e4SLinus Torvalds 1829a5664dadSMike Snitzer md->type = DM_TYPE_NONE; 18302ca3310eSAlasdair G Kergon init_rwsem(&md->io_lock); 1831e61290a4SDaniel Walker mutex_init(&md->suspend_lock); 1832a5664dadSMike Snitzer mutex_init(&md->type_lock); 1833022c2611SMikulas Patocka spin_lock_init(&md->deferred_lock); 18341da177e4SLinus Torvalds rwlock_init(&md->map_lock); 18351da177e4SLinus Torvalds atomic_set(&md->holders, 1); 18365c6bd75dSAlasdair G Kergon atomic_set(&md->open_count, 0); 18371da177e4SLinus Torvalds atomic_set(&md->event_nr, 0); 18387a8c3d3bSMike Anderson atomic_set(&md->uevent_seq, 0); 18397a8c3d3bSMike Anderson INIT_LIST_HEAD(&md->uevent_list); 18407a8c3d3bSMike Anderson spin_lock_init(&md->uevent_lock); 18411da177e4SLinus Torvalds 18424a0b4ddfSMike Snitzer md->queue = blk_alloc_queue(GFP_KERNEL); 18431da177e4SLinus Torvalds if (!md->queue) 18446ed7ade8SMilan Broz goto bad_queue; 18451da177e4SLinus Torvalds 18464a0b4ddfSMike Snitzer dm_init_md_queue(md); 18479faf400fSStefan Bader 18481da177e4SLinus Torvalds md->disk = alloc_disk(1); 18491da177e4SLinus Torvalds if (!md->disk) 18506ed7ade8SMilan Broz goto bad_disk; 18511da177e4SLinus Torvalds 1852316d315bSNikanth Karthikesan atomic_set(&md->pending[0], 0); 1853316d315bSNikanth Karthikesan atomic_set(&md->pending[1], 0); 1854f0b04115SJeff Mahoney init_waitqueue_head(&md->wait); 185553d5914fSMikulas Patocka INIT_WORK(&md->work, dm_wq_work); 1856f0b04115SJeff Mahoney init_waitqueue_head(&md->eventq); 1857f0b04115SJeff Mahoney 18581da177e4SLinus Torvalds md->disk->major = _major; 18591da177e4SLinus Torvalds md->disk->first_minor = minor; 18601da177e4SLinus Torvalds md->disk->fops = &dm_blk_dops; 18611da177e4SLinus Torvalds md->disk->queue = md->queue; 18621da177e4SLinus Torvalds md->disk->private_data = md; 18631da177e4SLinus Torvalds sprintf(md->disk->disk_name, "dm-%d", minor); 18641da177e4SLinus Torvalds add_disk(md->disk); 18657e51f257SMike Anderson format_dev_t(md->name, MKDEV(_major, minor)); 18661da177e4SLinus Torvalds 18679c4376deSTejun Heo md->wq = alloc_workqueue("kdmflush", 18689c4376deSTejun Heo WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0); 1869304f3f6aSMilan Broz if (!md->wq) 1870304f3f6aSMilan Broz goto bad_thread; 1871304f3f6aSMilan Broz 187232a926daSMikulas Patocka md->bdev = bdget_disk(md->disk, 0); 187332a926daSMikulas Patocka if (!md->bdev) 187432a926daSMikulas Patocka goto bad_bdev; 187532a926daSMikulas Patocka 18766a8736d1STejun Heo bio_init(&md->flush_bio); 18776a8736d1STejun Heo md->flush_bio.bi_bdev = md->bdev; 18786a8736d1STejun Heo md->flush_bio.bi_rw = WRITE_FLUSH; 18796a8736d1STejun Heo 1880ba61fdd1SJeff Mahoney /* Populate the mapping, nobody knows we exist yet */ 1881f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 1882ba61fdd1SJeff Mahoney old_md = idr_replace(&_minor_idr, md, minor); 1883f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1884ba61fdd1SJeff Mahoney 1885ba61fdd1SJeff Mahoney BUG_ON(old_md != MINOR_ALLOCED); 1886ba61fdd1SJeff Mahoney 18871da177e4SLinus Torvalds return md; 18881da177e4SLinus Torvalds 188932a926daSMikulas Patocka bad_bdev: 189032a926daSMikulas Patocka destroy_workqueue(md->wq); 1891304f3f6aSMilan Broz bad_thread: 189203022c54SZdenek Kabelac del_gendisk(md->disk); 1893304f3f6aSMilan Broz put_disk(md->disk); 18946ed7ade8SMilan Broz bad_disk: 18951312f40eSAl Viro blk_cleanup_queue(md->queue); 18966ed7ade8SMilan Broz bad_queue: 18971da177e4SLinus Torvalds free_minor(minor); 18986ed7ade8SMilan Broz bad_minor: 189910da4f79SJeff Mahoney module_put(THIS_MODULE); 19006ed7ade8SMilan Broz bad_module_get: 19011da177e4SLinus Torvalds kfree(md); 19021da177e4SLinus Torvalds return NULL; 19031da177e4SLinus Torvalds } 19041da177e4SLinus Torvalds 1905ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md); 1906ae9da83fSJun'ichi Nomura 19071da177e4SLinus Torvalds static void free_dev(struct mapped_device *md) 19081da177e4SLinus Torvalds { 1909f331c029STejun Heo int minor = MINOR(disk_devt(md->disk)); 191063d94e48SJun'ichi Nomura 1911ae9da83fSJun'ichi Nomura unlock_fs(md); 1912db8fef4fSMikulas Patocka bdput(md->bdev); 1913304f3f6aSMilan Broz destroy_workqueue(md->wq); 1914e6ee8c0bSKiyoshi Ueda if (md->tio_pool) 19151da177e4SLinus Torvalds mempool_destroy(md->tio_pool); 1916e6ee8c0bSKiyoshi Ueda if (md->io_pool) 19171da177e4SLinus Torvalds mempool_destroy(md->io_pool); 1918e6ee8c0bSKiyoshi Ueda if (md->bs) 19199faf400fSStefan Bader bioset_free(md->bs); 19209c47008dSMartin K. Petersen blk_integrity_unregister(md->disk); 19211da177e4SLinus Torvalds del_gendisk(md->disk); 192263d94e48SJun'ichi Nomura free_minor(minor); 1923fba9f90eSJeff Mahoney 1924fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 1925fba9f90eSJeff Mahoney md->disk->private_data = NULL; 1926fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 1927fba9f90eSJeff Mahoney 19281da177e4SLinus Torvalds put_disk(md->disk); 19291312f40eSAl Viro blk_cleanup_queue(md->queue); 193010da4f79SJeff Mahoney module_put(THIS_MODULE); 19311da177e4SLinus Torvalds kfree(md); 19321da177e4SLinus Torvalds } 19331da177e4SLinus Torvalds 1934e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t) 1935e6ee8c0bSKiyoshi Ueda { 1936e6ee8c0bSKiyoshi Ueda struct dm_md_mempools *p; 1937e6ee8c0bSKiyoshi Ueda 1938e6ee8c0bSKiyoshi Ueda if (md->io_pool && md->tio_pool && md->bs) 1939e6ee8c0bSKiyoshi Ueda /* the md already has necessary mempools */ 1940e6ee8c0bSKiyoshi Ueda goto out; 1941e6ee8c0bSKiyoshi Ueda 1942e6ee8c0bSKiyoshi Ueda p = dm_table_get_md_mempools(t); 1943e6ee8c0bSKiyoshi Ueda BUG_ON(!p || md->io_pool || md->tio_pool || md->bs); 1944e6ee8c0bSKiyoshi Ueda 1945e6ee8c0bSKiyoshi Ueda md->io_pool = p->io_pool; 1946e6ee8c0bSKiyoshi Ueda p->io_pool = NULL; 1947e6ee8c0bSKiyoshi Ueda md->tio_pool = p->tio_pool; 1948e6ee8c0bSKiyoshi Ueda p->tio_pool = NULL; 1949e6ee8c0bSKiyoshi Ueda md->bs = p->bs; 1950e6ee8c0bSKiyoshi Ueda p->bs = NULL; 1951e6ee8c0bSKiyoshi Ueda 1952e6ee8c0bSKiyoshi Ueda out: 1953e6ee8c0bSKiyoshi Ueda /* mempool bind completed, now no need any mempools in the table */ 1954e6ee8c0bSKiyoshi Ueda dm_table_free_md_mempools(t); 1955e6ee8c0bSKiyoshi Ueda } 1956e6ee8c0bSKiyoshi Ueda 19571da177e4SLinus Torvalds /* 19581da177e4SLinus Torvalds * Bind a table to the device. 19591da177e4SLinus Torvalds */ 19601da177e4SLinus Torvalds static void event_callback(void *context) 19611da177e4SLinus Torvalds { 19627a8c3d3bSMike Anderson unsigned long flags; 19637a8c3d3bSMike Anderson LIST_HEAD(uevents); 19641da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) context; 19651da177e4SLinus Torvalds 19667a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 19677a8c3d3bSMike Anderson list_splice_init(&md->uevent_list, &uevents); 19687a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 19697a8c3d3bSMike Anderson 1970ed9e1982STejun Heo dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); 19717a8c3d3bSMike Anderson 19721da177e4SLinus Torvalds atomic_inc(&md->event_nr); 19731da177e4SLinus Torvalds wake_up(&md->eventq); 19741da177e4SLinus Torvalds } 19751da177e4SLinus Torvalds 1976c217649bSMike Snitzer /* 1977c217649bSMike Snitzer * Protected by md->suspend_lock obtained by dm_swap_table(). 1978c217649bSMike Snitzer */ 19794e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size) 19801da177e4SLinus Torvalds { 19814e90188bSAlasdair G Kergon set_capacity(md->disk, size); 19821da177e4SLinus Torvalds 1983db8fef4fSMikulas Patocka i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT); 19841da177e4SLinus Torvalds } 19851da177e4SLinus Torvalds 1986042d2a9bSAlasdair G Kergon /* 1987d5b9dd04SMikulas Patocka * Return 1 if the queue has a compulsory merge_bvec_fn function. 1988d5b9dd04SMikulas Patocka * 1989d5b9dd04SMikulas Patocka * If this function returns 0, then the device is either a non-dm 1990d5b9dd04SMikulas Patocka * device without a merge_bvec_fn, or it is a dm device that is 1991d5b9dd04SMikulas Patocka * able to split any bios it receives that are too big. 1992d5b9dd04SMikulas Patocka */ 1993d5b9dd04SMikulas Patocka int dm_queue_merge_is_compulsory(struct request_queue *q) 1994d5b9dd04SMikulas Patocka { 1995d5b9dd04SMikulas Patocka struct mapped_device *dev_md; 1996d5b9dd04SMikulas Patocka 1997d5b9dd04SMikulas Patocka if (!q->merge_bvec_fn) 1998d5b9dd04SMikulas Patocka return 0; 1999d5b9dd04SMikulas Patocka 2000d5b9dd04SMikulas Patocka if (q->make_request_fn == dm_request) { 2001d5b9dd04SMikulas Patocka dev_md = q->queuedata; 2002d5b9dd04SMikulas Patocka if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags)) 2003d5b9dd04SMikulas Patocka return 0; 2004d5b9dd04SMikulas Patocka } 2005d5b9dd04SMikulas Patocka 2006d5b9dd04SMikulas Patocka return 1; 2007d5b9dd04SMikulas Patocka } 2008d5b9dd04SMikulas Patocka 2009d5b9dd04SMikulas Patocka static int dm_device_merge_is_compulsory(struct dm_target *ti, 2010d5b9dd04SMikulas Patocka struct dm_dev *dev, sector_t start, 2011d5b9dd04SMikulas Patocka sector_t len, void *data) 2012d5b9dd04SMikulas Patocka { 2013d5b9dd04SMikulas Patocka struct block_device *bdev = dev->bdev; 2014d5b9dd04SMikulas Patocka struct request_queue *q = bdev_get_queue(bdev); 2015d5b9dd04SMikulas Patocka 2016d5b9dd04SMikulas Patocka return dm_queue_merge_is_compulsory(q); 2017d5b9dd04SMikulas Patocka } 2018d5b9dd04SMikulas Patocka 2019d5b9dd04SMikulas Patocka /* 2020d5b9dd04SMikulas Patocka * Return 1 if it is acceptable to ignore merge_bvec_fn based 2021d5b9dd04SMikulas Patocka * on the properties of the underlying devices. 2022d5b9dd04SMikulas Patocka */ 2023d5b9dd04SMikulas Patocka static int dm_table_merge_is_optional(struct dm_table *table) 2024d5b9dd04SMikulas Patocka { 2025d5b9dd04SMikulas Patocka unsigned i = 0; 2026d5b9dd04SMikulas Patocka struct dm_target *ti; 2027d5b9dd04SMikulas Patocka 2028d5b9dd04SMikulas Patocka while (i < dm_table_get_num_targets(table)) { 2029d5b9dd04SMikulas Patocka ti = dm_table_get_target(table, i++); 2030d5b9dd04SMikulas Patocka 2031d5b9dd04SMikulas Patocka if (ti->type->iterate_devices && 2032d5b9dd04SMikulas Patocka ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL)) 2033d5b9dd04SMikulas Patocka return 0; 2034d5b9dd04SMikulas Patocka } 2035d5b9dd04SMikulas Patocka 2036d5b9dd04SMikulas Patocka return 1; 2037d5b9dd04SMikulas Patocka } 2038d5b9dd04SMikulas Patocka 2039d5b9dd04SMikulas Patocka /* 2040042d2a9bSAlasdair G Kergon * Returns old map, which caller must destroy. 2041042d2a9bSAlasdair G Kergon */ 2042042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t, 2043754c5fc7SMike Snitzer struct queue_limits *limits) 20441da177e4SLinus Torvalds { 2045042d2a9bSAlasdair G Kergon struct dm_table *old_map; 2046165125e1SJens Axboe struct request_queue *q = md->queue; 20471da177e4SLinus Torvalds sector_t size; 2048523d9297SKiyoshi Ueda unsigned long flags; 2049d5b9dd04SMikulas Patocka int merge_is_optional; 20501da177e4SLinus Torvalds 20511da177e4SLinus Torvalds size = dm_table_get_size(t); 20523ac51e74SDarrick J. Wong 20533ac51e74SDarrick J. Wong /* 20543ac51e74SDarrick J. Wong * Wipe any geometry if the size of the table changed. 20553ac51e74SDarrick J. Wong */ 20563ac51e74SDarrick J. Wong if (size != get_capacity(md->disk)) 20573ac51e74SDarrick J. Wong memset(&md->geometry, 0, sizeof(md->geometry)); 20583ac51e74SDarrick J. Wong 20594e90188bSAlasdair G Kergon __set_size(md, size); 20601da177e4SLinus Torvalds 2061cf222b37SAlasdair G Kergon dm_table_event_callback(t, event_callback, md); 20622ca3310eSAlasdair G Kergon 2063e6ee8c0bSKiyoshi Ueda /* 2064e6ee8c0bSKiyoshi Ueda * The queue hasn't been stopped yet, if the old table type wasn't 2065e6ee8c0bSKiyoshi Ueda * for request-based during suspension. So stop it to prevent 2066e6ee8c0bSKiyoshi Ueda * I/O mapping before resume. 2067e6ee8c0bSKiyoshi Ueda * This must be done before setting the queue restrictions, 2068e6ee8c0bSKiyoshi Ueda * because request-based dm may be run just after the setting. 2069e6ee8c0bSKiyoshi Ueda */ 2070e6ee8c0bSKiyoshi Ueda if (dm_table_request_based(t) && !blk_queue_stopped(q)) 2071e6ee8c0bSKiyoshi Ueda stop_queue(q); 2072e6ee8c0bSKiyoshi Ueda 2073e6ee8c0bSKiyoshi Ueda __bind_mempools(md, t); 2074e6ee8c0bSKiyoshi Ueda 2075d5b9dd04SMikulas Patocka merge_is_optional = dm_table_merge_is_optional(t); 2076d5b9dd04SMikulas Patocka 2077523d9297SKiyoshi Ueda write_lock_irqsave(&md->map_lock, flags); 2078042d2a9bSAlasdair G Kergon old_map = md->map; 20792ca3310eSAlasdair G Kergon md->map = t; 208036a0456fSAlasdair G Kergon md->immutable_target_type = dm_table_get_immutable_target_type(t); 208136a0456fSAlasdair G Kergon 2082754c5fc7SMike Snitzer dm_table_set_restrictions(t, q, limits); 2083d5b9dd04SMikulas Patocka if (merge_is_optional) 2084d5b9dd04SMikulas Patocka set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags); 2085d5b9dd04SMikulas Patocka else 2086d5b9dd04SMikulas Patocka clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags); 2087523d9297SKiyoshi Ueda write_unlock_irqrestore(&md->map_lock, flags); 20882ca3310eSAlasdair G Kergon 2089042d2a9bSAlasdair G Kergon return old_map; 20901da177e4SLinus Torvalds } 20911da177e4SLinus Torvalds 2092a7940155SAlasdair G Kergon /* 2093a7940155SAlasdair G Kergon * Returns unbound table for the caller to free. 2094a7940155SAlasdair G Kergon */ 2095a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md) 20961da177e4SLinus Torvalds { 20971da177e4SLinus Torvalds struct dm_table *map = md->map; 2098523d9297SKiyoshi Ueda unsigned long flags; 20991da177e4SLinus Torvalds 21001da177e4SLinus Torvalds if (!map) 2101a7940155SAlasdair G Kergon return NULL; 21021da177e4SLinus Torvalds 21031da177e4SLinus Torvalds dm_table_event_callback(map, NULL, NULL); 2104523d9297SKiyoshi Ueda write_lock_irqsave(&md->map_lock, flags); 21051da177e4SLinus Torvalds md->map = NULL; 2106523d9297SKiyoshi Ueda write_unlock_irqrestore(&md->map_lock, flags); 2107a7940155SAlasdair G Kergon 2108a7940155SAlasdair G Kergon return map; 21091da177e4SLinus Torvalds } 21101da177e4SLinus Torvalds 21111da177e4SLinus Torvalds /* 21121da177e4SLinus Torvalds * Constructor for a new device. 21131da177e4SLinus Torvalds */ 21142b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result) 21151da177e4SLinus Torvalds { 21161da177e4SLinus Torvalds struct mapped_device *md; 21171da177e4SLinus Torvalds 21182b06cfffSAlasdair G Kergon md = alloc_dev(minor); 21191da177e4SLinus Torvalds if (!md) 21201da177e4SLinus Torvalds return -ENXIO; 21211da177e4SLinus Torvalds 2122784aae73SMilan Broz dm_sysfs_init(md); 2123784aae73SMilan Broz 21241da177e4SLinus Torvalds *result = md; 21251da177e4SLinus Torvalds return 0; 21261da177e4SLinus Torvalds } 21271da177e4SLinus Torvalds 2128a5664dadSMike Snitzer /* 2129a5664dadSMike Snitzer * Functions to manage md->type. 2130a5664dadSMike Snitzer * All are required to hold md->type_lock. 2131a5664dadSMike Snitzer */ 2132a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md) 2133a5664dadSMike Snitzer { 2134a5664dadSMike Snitzer mutex_lock(&md->type_lock); 2135a5664dadSMike Snitzer } 2136a5664dadSMike Snitzer 2137a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md) 2138a5664dadSMike Snitzer { 2139a5664dadSMike Snitzer mutex_unlock(&md->type_lock); 2140a5664dadSMike Snitzer } 2141a5664dadSMike Snitzer 2142a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type) 2143a5664dadSMike Snitzer { 2144a5664dadSMike Snitzer md->type = type; 2145a5664dadSMike Snitzer } 2146a5664dadSMike Snitzer 2147a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md) 2148a5664dadSMike Snitzer { 2149a5664dadSMike Snitzer return md->type; 2150a5664dadSMike Snitzer } 2151a5664dadSMike Snitzer 215236a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md) 215336a0456fSAlasdair G Kergon { 215436a0456fSAlasdair G Kergon return md->immutable_target_type; 215536a0456fSAlasdair G Kergon } 215636a0456fSAlasdair G Kergon 21574a0b4ddfSMike Snitzer /* 21584a0b4ddfSMike Snitzer * Fully initialize a request-based queue (->elevator, ->request_fn, etc). 21594a0b4ddfSMike Snitzer */ 21604a0b4ddfSMike Snitzer static int dm_init_request_based_queue(struct mapped_device *md) 21614a0b4ddfSMike Snitzer { 21624a0b4ddfSMike Snitzer struct request_queue *q = NULL; 21634a0b4ddfSMike Snitzer 21644a0b4ddfSMike Snitzer if (md->queue->elevator) 21654a0b4ddfSMike Snitzer return 1; 21664a0b4ddfSMike Snitzer 21674a0b4ddfSMike Snitzer /* Fully initialize the queue */ 21684a0b4ddfSMike Snitzer q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL); 21694a0b4ddfSMike Snitzer if (!q) 21704a0b4ddfSMike Snitzer return 0; 21714a0b4ddfSMike Snitzer 21724a0b4ddfSMike Snitzer md->queue = q; 21734a0b4ddfSMike Snitzer dm_init_md_queue(md); 21744a0b4ddfSMike Snitzer blk_queue_softirq_done(md->queue, dm_softirq_done); 21754a0b4ddfSMike Snitzer blk_queue_prep_rq(md->queue, dm_prep_fn); 21764a0b4ddfSMike Snitzer blk_queue_lld_busy(md->queue, dm_lld_busy); 21774a0b4ddfSMike Snitzer 21784a0b4ddfSMike Snitzer elv_register_queue(md->queue); 21794a0b4ddfSMike Snitzer 21804a0b4ddfSMike Snitzer return 1; 21814a0b4ddfSMike Snitzer } 21824a0b4ddfSMike Snitzer 21834a0b4ddfSMike Snitzer /* 21844a0b4ddfSMike Snitzer * Setup the DM device's queue based on md's type 21854a0b4ddfSMike Snitzer */ 21864a0b4ddfSMike Snitzer int dm_setup_md_queue(struct mapped_device *md) 21874a0b4ddfSMike Snitzer { 21884a0b4ddfSMike Snitzer if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) && 21894a0b4ddfSMike Snitzer !dm_init_request_based_queue(md)) { 21904a0b4ddfSMike Snitzer DMWARN("Cannot initialize queue for request-based mapped device"); 21914a0b4ddfSMike Snitzer return -EINVAL; 21924a0b4ddfSMike Snitzer } 21934a0b4ddfSMike Snitzer 21944a0b4ddfSMike Snitzer return 0; 21954a0b4ddfSMike Snitzer } 21964a0b4ddfSMike Snitzer 2197637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev) 21981da177e4SLinus Torvalds { 21991da177e4SLinus Torvalds struct mapped_device *md; 22001da177e4SLinus Torvalds unsigned minor = MINOR(dev); 22011da177e4SLinus Torvalds 22021da177e4SLinus Torvalds if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 22031da177e4SLinus Torvalds return NULL; 22041da177e4SLinus Torvalds 2205f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 22061da177e4SLinus Torvalds 22071da177e4SLinus Torvalds md = idr_find(&_minor_idr, minor); 2208fba9f90eSJeff Mahoney if (md && (md == MINOR_ALLOCED || 2209f331c029STejun Heo (MINOR(disk_devt(dm_disk(md))) != minor) || 2210abdc568bSKiyoshi Ueda dm_deleting_md(md) || 2211fba9f90eSJeff Mahoney test_bit(DMF_FREEING, &md->flags))) { 2212637842cfSDavid Teigland md = NULL; 2213fba9f90eSJeff Mahoney goto out; 2214fba9f90eSJeff Mahoney } 22151da177e4SLinus Torvalds 2216fba9f90eSJeff Mahoney out: 2217f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 22181da177e4SLinus Torvalds 2219637842cfSDavid Teigland return md; 2220637842cfSDavid Teigland } 2221637842cfSDavid Teigland 2222d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev) 2223d229a958SDavid Teigland { 2224d229a958SDavid Teigland struct mapped_device *md = dm_find_md(dev); 2225d229a958SDavid Teigland 2226d229a958SDavid Teigland if (md) 2227d229a958SDavid Teigland dm_get(md); 2228d229a958SDavid Teigland 2229d229a958SDavid Teigland return md; 2230d229a958SDavid Teigland } 22313cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md); 2232d229a958SDavid Teigland 22339ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md) 2234637842cfSDavid Teigland { 22359ade92a9SAlasdair G Kergon return md->interface_ptr; 22361da177e4SLinus Torvalds } 22371da177e4SLinus Torvalds 22381da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr) 22391da177e4SLinus Torvalds { 22401da177e4SLinus Torvalds md->interface_ptr = ptr; 22411da177e4SLinus Torvalds } 22421da177e4SLinus Torvalds 22431da177e4SLinus Torvalds void dm_get(struct mapped_device *md) 22441da177e4SLinus Torvalds { 22451da177e4SLinus Torvalds atomic_inc(&md->holders); 22463f77316dSKiyoshi Ueda BUG_ON(test_bit(DMF_FREEING, &md->flags)); 22471da177e4SLinus Torvalds } 22481da177e4SLinus Torvalds 224972d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md) 225072d94861SAlasdair G Kergon { 225172d94861SAlasdair G Kergon return md->name; 225272d94861SAlasdair G Kergon } 225372d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name); 225472d94861SAlasdair G Kergon 22553f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait) 22561da177e4SLinus Torvalds { 22571134e5aeSMike Anderson struct dm_table *map; 22581da177e4SLinus Torvalds 22593f77316dSKiyoshi Ueda might_sleep(); 2260fba9f90eSJeff Mahoney 22613f77316dSKiyoshi Ueda spin_lock(&_minor_lock); 22627c666411SAlasdair G Kergon map = dm_get_live_table(md); 22633f77316dSKiyoshi Ueda idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md)))); 2264fba9f90eSJeff Mahoney set_bit(DMF_FREEING, &md->flags); 2265f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 22663f77316dSKiyoshi Ueda 22674f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) { 22681da177e4SLinus Torvalds dm_table_presuspend_targets(map); 22691da177e4SLinus Torvalds dm_table_postsuspend_targets(map); 22701da177e4SLinus Torvalds } 22713f77316dSKiyoshi Ueda 22723f77316dSKiyoshi Ueda /* 22733f77316dSKiyoshi Ueda * Rare, but there may be I/O requests still going to complete, 22743f77316dSKiyoshi Ueda * for example. Wait for all references to disappear. 22753f77316dSKiyoshi Ueda * No one should increment the reference count of the mapped_device, 22763f77316dSKiyoshi Ueda * after the mapped_device state becomes DMF_FREEING. 22773f77316dSKiyoshi Ueda */ 22783f77316dSKiyoshi Ueda if (wait) 22793f77316dSKiyoshi Ueda while (atomic_read(&md->holders)) 22803f77316dSKiyoshi Ueda msleep(1); 22813f77316dSKiyoshi Ueda else if (atomic_read(&md->holders)) 22823f77316dSKiyoshi Ueda DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)", 22833f77316dSKiyoshi Ueda dm_device_name(md), atomic_read(&md->holders)); 22843f77316dSKiyoshi Ueda 2285784aae73SMilan Broz dm_sysfs_exit(md); 22861134e5aeSMike Anderson dm_table_put(map); 2287a7940155SAlasdair G Kergon dm_table_destroy(__unbind(md)); 22881da177e4SLinus Torvalds free_dev(md); 22891da177e4SLinus Torvalds } 22903f77316dSKiyoshi Ueda 22913f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md) 22923f77316dSKiyoshi Ueda { 22933f77316dSKiyoshi Ueda __dm_destroy(md, true); 22943f77316dSKiyoshi Ueda } 22953f77316dSKiyoshi Ueda 22963f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md) 22973f77316dSKiyoshi Ueda { 22983f77316dSKiyoshi Ueda __dm_destroy(md, false); 22993f77316dSKiyoshi Ueda } 23003f77316dSKiyoshi Ueda 23013f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md) 23023f77316dSKiyoshi Ueda { 23033f77316dSKiyoshi Ueda atomic_dec(&md->holders); 23041da177e4SLinus Torvalds } 230579eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put); 23061da177e4SLinus Torvalds 2307401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible) 230846125c1cSMilan Broz { 230946125c1cSMilan Broz int r = 0; 2310b44ebeb0SMikulas Patocka DECLARE_WAITQUEUE(wait, current); 2311b44ebeb0SMikulas Patocka 2312b44ebeb0SMikulas Patocka add_wait_queue(&md->wait, &wait); 231346125c1cSMilan Broz 231446125c1cSMilan Broz while (1) { 2315401600dfSMikulas Patocka set_current_state(interruptible); 231646125c1cSMilan Broz 2317b4324feeSKiyoshi Ueda if (!md_in_flight(md)) 231846125c1cSMilan Broz break; 231946125c1cSMilan Broz 2320401600dfSMikulas Patocka if (interruptible == TASK_INTERRUPTIBLE && 2321401600dfSMikulas Patocka signal_pending(current)) { 232246125c1cSMilan Broz r = -EINTR; 232346125c1cSMilan Broz break; 232446125c1cSMilan Broz } 232546125c1cSMilan Broz 232646125c1cSMilan Broz io_schedule(); 232746125c1cSMilan Broz } 232846125c1cSMilan Broz set_current_state(TASK_RUNNING); 232946125c1cSMilan Broz 2330b44ebeb0SMikulas Patocka remove_wait_queue(&md->wait, &wait); 2331b44ebeb0SMikulas Patocka 233246125c1cSMilan Broz return r; 233346125c1cSMilan Broz } 233446125c1cSMilan Broz 23351da177e4SLinus Torvalds /* 23361da177e4SLinus Torvalds * Process the deferred bios 23371da177e4SLinus Torvalds */ 2338ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work) 23391da177e4SLinus Torvalds { 2340ef208587SMikulas Patocka struct mapped_device *md = container_of(work, struct mapped_device, 2341ef208587SMikulas Patocka work); 23426d6f10dfSMilan Broz struct bio *c; 23431da177e4SLinus Torvalds 23446a8736d1STejun Heo down_read(&md->io_lock); 2345ef208587SMikulas Patocka 23463b00b203SMikulas Patocka while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 2347022c2611SMikulas Patocka spin_lock_irq(&md->deferred_lock); 2348022c2611SMikulas Patocka c = bio_list_pop(&md->deferred); 2349022c2611SMikulas Patocka spin_unlock_irq(&md->deferred_lock); 2350022c2611SMikulas Patocka 23516a8736d1STejun Heo if (!c) 2352df12ee99SAlasdair G Kergon break; 235373d410c0SMilan Broz 23546a8736d1STejun Heo up_read(&md->io_lock); 23553b00b203SMikulas Patocka 2356e6ee8c0bSKiyoshi Ueda if (dm_request_based(md)) 2357e6ee8c0bSKiyoshi Ueda generic_make_request(c); 2358af7e466aSMikulas Patocka else 2359df12ee99SAlasdair G Kergon __split_and_process_bio(md, c); 23606a8736d1STejun Heo 23616a8736d1STejun Heo down_read(&md->io_lock); 2362e6ee8c0bSKiyoshi Ueda } 23633b00b203SMikulas Patocka 23646a8736d1STejun Heo up_read(&md->io_lock); 23651da177e4SLinus Torvalds } 23661da177e4SLinus Torvalds 23679a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md) 2368304f3f6aSMilan Broz { 23693b00b203SMikulas Patocka clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 23703b00b203SMikulas Patocka smp_mb__after_clear_bit(); 237153d5914fSMikulas Patocka queue_work(md->wq, &md->work); 2372304f3f6aSMilan Broz } 2373304f3f6aSMilan Broz 23741da177e4SLinus Torvalds /* 2375042d2a9bSAlasdair G Kergon * Swap in a new table, returning the old one for the caller to destroy. 23761da177e4SLinus Torvalds */ 2377042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table) 23781da177e4SLinus Torvalds { 2379042d2a9bSAlasdair G Kergon struct dm_table *map = ERR_PTR(-EINVAL); 2380754c5fc7SMike Snitzer struct queue_limits limits; 2381042d2a9bSAlasdair G Kergon int r; 23821da177e4SLinus Torvalds 2383e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 23841da177e4SLinus Torvalds 23851da177e4SLinus Torvalds /* device must be suspended */ 23864f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) 238793c534aeSAlasdair G Kergon goto out; 23881da177e4SLinus Torvalds 2389754c5fc7SMike Snitzer r = dm_calculate_queue_limits(table, &limits); 2390042d2a9bSAlasdair G Kergon if (r) { 2391042d2a9bSAlasdair G Kergon map = ERR_PTR(r); 2392754c5fc7SMike Snitzer goto out; 2393042d2a9bSAlasdair G Kergon } 2394754c5fc7SMike Snitzer 2395042d2a9bSAlasdair G Kergon map = __bind(md, table, &limits); 23961da177e4SLinus Torvalds 239793c534aeSAlasdair G Kergon out: 2398e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 2399042d2a9bSAlasdair G Kergon return map; 24001da177e4SLinus Torvalds } 24011da177e4SLinus Torvalds 24021da177e4SLinus Torvalds /* 24031da177e4SLinus Torvalds * Functions to lock and unlock any filesystem running on the 24041da177e4SLinus Torvalds * device. 24051da177e4SLinus Torvalds */ 24062ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md) 24071da177e4SLinus Torvalds { 2408e39e2e95SAlasdair G Kergon int r; 24091da177e4SLinus Torvalds 24101da177e4SLinus Torvalds WARN_ON(md->frozen_sb); 2411dfbe03f6SAlasdair G Kergon 2412db8fef4fSMikulas Patocka md->frozen_sb = freeze_bdev(md->bdev); 2413dfbe03f6SAlasdair G Kergon if (IS_ERR(md->frozen_sb)) { 2414cf222b37SAlasdair G Kergon r = PTR_ERR(md->frozen_sb); 2415e39e2e95SAlasdair G Kergon md->frozen_sb = NULL; 2416e39e2e95SAlasdair G Kergon return r; 2417dfbe03f6SAlasdair G Kergon } 2418dfbe03f6SAlasdair G Kergon 2419aa8d7c2fSAlasdair G Kergon set_bit(DMF_FROZEN, &md->flags); 2420aa8d7c2fSAlasdair G Kergon 24211da177e4SLinus Torvalds return 0; 24221da177e4SLinus Torvalds } 24231da177e4SLinus Torvalds 24242ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md) 24251da177e4SLinus Torvalds { 2426aa8d7c2fSAlasdair G Kergon if (!test_bit(DMF_FROZEN, &md->flags)) 2427aa8d7c2fSAlasdair G Kergon return; 2428aa8d7c2fSAlasdair G Kergon 2429db8fef4fSMikulas Patocka thaw_bdev(md->bdev, md->frozen_sb); 24301da177e4SLinus Torvalds md->frozen_sb = NULL; 2431aa8d7c2fSAlasdair G Kergon clear_bit(DMF_FROZEN, &md->flags); 24321da177e4SLinus Torvalds } 24331da177e4SLinus Torvalds 24341da177e4SLinus Torvalds /* 24351da177e4SLinus Torvalds * We need to be able to change a mapping table under a mounted 24361da177e4SLinus Torvalds * filesystem. For example we might want to move some data in 24371da177e4SLinus Torvalds * the background. Before the table can be swapped with 24381da177e4SLinus Torvalds * dm_bind_table, dm_suspend must be called to flush any in 24391da177e4SLinus Torvalds * flight bios and ensure that any further io gets deferred. 24401da177e4SLinus Torvalds */ 2441cec47e3dSKiyoshi Ueda /* 2442cec47e3dSKiyoshi Ueda * Suspend mechanism in request-based dm. 2443cec47e3dSKiyoshi Ueda * 24449f518b27SKiyoshi Ueda * 1. Flush all I/Os by lock_fs() if needed. 24459f518b27SKiyoshi Ueda * 2. Stop dispatching any I/O by stopping the request_queue. 24469f518b27SKiyoshi Ueda * 3. Wait for all in-flight I/Os to be completed or requeued. 2447cec47e3dSKiyoshi Ueda * 24489f518b27SKiyoshi Ueda * To abort suspend, start the request_queue. 2449cec47e3dSKiyoshi Ueda */ 2450a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags) 24511da177e4SLinus Torvalds { 24522ca3310eSAlasdair G Kergon struct dm_table *map = NULL; 245346125c1cSMilan Broz int r = 0; 2454a3d77d35SKiyoshi Ueda int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0; 24552e93ccc1SKiyoshi Ueda int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0; 24561da177e4SLinus Torvalds 2457e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 24582ca3310eSAlasdair G Kergon 24594f186f8bSKiyoshi Ueda if (dm_suspended_md(md)) { 246073d410c0SMilan Broz r = -EINVAL; 2461d287483dSAlasdair G Kergon goto out_unlock; 246273d410c0SMilan Broz } 24631da177e4SLinus Torvalds 24647c666411SAlasdair G Kergon map = dm_get_live_table(md); 2465cf222b37SAlasdair G Kergon 24662e93ccc1SKiyoshi Ueda /* 24672e93ccc1SKiyoshi Ueda * DMF_NOFLUSH_SUSPENDING must be set before presuspend. 24682e93ccc1SKiyoshi Ueda * This flag is cleared before dm_suspend returns. 24692e93ccc1SKiyoshi Ueda */ 24702e93ccc1SKiyoshi Ueda if (noflush) 24712e93ccc1SKiyoshi Ueda set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 24722e93ccc1SKiyoshi Ueda 2473436d4108SAlasdair G Kergon /* This does not get reverted if there's an error later. */ 24741da177e4SLinus Torvalds dm_table_presuspend_targets(map); 24751da177e4SLinus Torvalds 24762e93ccc1SKiyoshi Ueda /* 24779f518b27SKiyoshi Ueda * Flush I/O to the device. 24789f518b27SKiyoshi Ueda * Any I/O submitted after lock_fs() may not be flushed. 24799f518b27SKiyoshi Ueda * noflush takes precedence over do_lockfs. 24809f518b27SKiyoshi Ueda * (lock_fs() flushes I/Os and waits for them to complete.) 24812e93ccc1SKiyoshi Ueda */ 248232a926daSMikulas Patocka if (!noflush && do_lockfs) { 24832ca3310eSAlasdair G Kergon r = lock_fs(md); 24842ca3310eSAlasdair G Kergon if (r) 24852ca3310eSAlasdair G Kergon goto out; 2486aa8d7c2fSAlasdair G Kergon } 24871da177e4SLinus Torvalds 24881da177e4SLinus Torvalds /* 24893b00b203SMikulas Patocka * Here we must make sure that no processes are submitting requests 24903b00b203SMikulas Patocka * to target drivers i.e. no one may be executing 24913b00b203SMikulas Patocka * __split_and_process_bio. This is called from dm_request and 24923b00b203SMikulas Patocka * dm_wq_work. 24933b00b203SMikulas Patocka * 24943b00b203SMikulas Patocka * To get all processes out of __split_and_process_bio in dm_request, 24953b00b203SMikulas Patocka * we take the write lock. To prevent any process from reentering 24966a8736d1STejun Heo * __split_and_process_bio from dm_request and quiesce the thread 24976a8736d1STejun Heo * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call 24986a8736d1STejun Heo * flush_workqueue(md->wq). 24991da177e4SLinus Torvalds */ 25002ca3310eSAlasdair G Kergon down_write(&md->io_lock); 25011eb787ecSAlasdair G Kergon set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 25022ca3310eSAlasdair G Kergon up_write(&md->io_lock); 25031da177e4SLinus Torvalds 2504d0bcb878SKiyoshi Ueda /* 250529e4013dSTejun Heo * Stop md->queue before flushing md->wq in case request-based 250629e4013dSTejun Heo * dm defers requests to md->wq from md->queue. 2507d0bcb878SKiyoshi Ueda */ 2508cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 25099f518b27SKiyoshi Ueda stop_queue(md->queue); 2510cec47e3dSKiyoshi Ueda 2511d0bcb878SKiyoshi Ueda flush_workqueue(md->wq); 2512d0bcb878SKiyoshi Ueda 25131da177e4SLinus Torvalds /* 25143b00b203SMikulas Patocka * At this point no more requests are entering target request routines. 25153b00b203SMikulas Patocka * We call dm_wait_for_completion to wait for all existing requests 25163b00b203SMikulas Patocka * to finish. 25171da177e4SLinus Torvalds */ 2518401600dfSMikulas Patocka r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE); 25191da177e4SLinus Torvalds 25202ca3310eSAlasdair G Kergon down_write(&md->io_lock); 25216d6f10dfSMilan Broz if (noflush) 2522022c2611SMikulas Patocka clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 252394d6351eSMilan Broz up_write(&md->io_lock); 25242e93ccc1SKiyoshi Ueda 25251da177e4SLinus Torvalds /* were we interrupted ? */ 252646125c1cSMilan Broz if (r < 0) { 25279a1fb464SMikulas Patocka dm_queue_flush(md); 252873d410c0SMilan Broz 2529cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 25309f518b27SKiyoshi Ueda start_queue(md->queue); 2531cec47e3dSKiyoshi Ueda 25322ca3310eSAlasdair G Kergon unlock_fs(md); 25332e93ccc1SKiyoshi Ueda goto out; /* pushback list is already flushed, so skip flush */ 25342ca3310eSAlasdair G Kergon } 25352ca3310eSAlasdair G Kergon 25363b00b203SMikulas Patocka /* 25373b00b203SMikulas Patocka * If dm_wait_for_completion returned 0, the device is completely 25383b00b203SMikulas Patocka * quiescent now. There is no request-processing activity. All new 25393b00b203SMikulas Patocka * requests are being added to md->deferred list. 25403b00b203SMikulas Patocka */ 25413b00b203SMikulas Patocka 25421da177e4SLinus Torvalds set_bit(DMF_SUSPENDED, &md->flags); 25431da177e4SLinus Torvalds 25444d4471cbSKiyoshi Ueda dm_table_postsuspend_targets(map); 25454d4471cbSKiyoshi Ueda 25462ca3310eSAlasdair G Kergon out: 25471da177e4SLinus Torvalds dm_table_put(map); 2548d287483dSAlasdair G Kergon 2549d287483dSAlasdair G Kergon out_unlock: 2550e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 2551cf222b37SAlasdair G Kergon return r; 25521da177e4SLinus Torvalds } 25531da177e4SLinus Torvalds 25541da177e4SLinus Torvalds int dm_resume(struct mapped_device *md) 25551da177e4SLinus Torvalds { 2556cf222b37SAlasdair G Kergon int r = -EINVAL; 2557cf222b37SAlasdair G Kergon struct dm_table *map = NULL; 25581da177e4SLinus Torvalds 2559e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 25604f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) 2561cf222b37SAlasdair G Kergon goto out; 2562cf222b37SAlasdair G Kergon 25637c666411SAlasdair G Kergon map = dm_get_live_table(md); 25642ca3310eSAlasdair G Kergon if (!map || !dm_table_get_size(map)) 2565cf222b37SAlasdair G Kergon goto out; 25661da177e4SLinus Torvalds 25678757b776SMilan Broz r = dm_table_resume_targets(map); 25688757b776SMilan Broz if (r) 25698757b776SMilan Broz goto out; 25702ca3310eSAlasdair G Kergon 25719a1fb464SMikulas Patocka dm_queue_flush(md); 25722ca3310eSAlasdair G Kergon 2573cec47e3dSKiyoshi Ueda /* 2574cec47e3dSKiyoshi Ueda * Flushing deferred I/Os must be done after targets are resumed 2575cec47e3dSKiyoshi Ueda * so that mapping of targets can work correctly. 2576cec47e3dSKiyoshi Ueda * Request-based dm is queueing the deferred I/Os in its request_queue. 2577cec47e3dSKiyoshi Ueda */ 2578cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 2579cec47e3dSKiyoshi Ueda start_queue(md->queue); 2580cec47e3dSKiyoshi Ueda 25812ca3310eSAlasdair G Kergon unlock_fs(md); 25822ca3310eSAlasdair G Kergon 25832ca3310eSAlasdair G Kergon clear_bit(DMF_SUSPENDED, &md->flags); 25842ca3310eSAlasdair G Kergon 2585cf222b37SAlasdair G Kergon r = 0; 2586cf222b37SAlasdair G Kergon out: 2587cf222b37SAlasdair G Kergon dm_table_put(map); 2588e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 25892ca3310eSAlasdair G Kergon 2590cf222b37SAlasdair G Kergon return r; 25911da177e4SLinus Torvalds } 25921da177e4SLinus Torvalds 25931da177e4SLinus Torvalds /*----------------------------------------------------------------- 25941da177e4SLinus Torvalds * Event notification. 25951da177e4SLinus Torvalds *---------------------------------------------------------------*/ 25963abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action, 259760935eb2SMilan Broz unsigned cookie) 259869267a30SAlasdair G Kergon { 259960935eb2SMilan Broz char udev_cookie[DM_COOKIE_LENGTH]; 260060935eb2SMilan Broz char *envp[] = { udev_cookie, NULL }; 260160935eb2SMilan Broz 260260935eb2SMilan Broz if (!cookie) 26033abf85b5SPeter Rajnoha return kobject_uevent(&disk_to_dev(md->disk)->kobj, action); 260460935eb2SMilan Broz else { 260560935eb2SMilan Broz snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u", 260660935eb2SMilan Broz DM_COOKIE_ENV_VAR_NAME, cookie); 26073abf85b5SPeter Rajnoha return kobject_uevent_env(&disk_to_dev(md->disk)->kobj, 26083abf85b5SPeter Rajnoha action, envp); 260960935eb2SMilan Broz } 261069267a30SAlasdair G Kergon } 261169267a30SAlasdair G Kergon 26127a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md) 26137a8c3d3bSMike Anderson { 26147a8c3d3bSMike Anderson return atomic_add_return(1, &md->uevent_seq); 26157a8c3d3bSMike Anderson } 26167a8c3d3bSMike Anderson 26171da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md) 26181da177e4SLinus Torvalds { 26191da177e4SLinus Torvalds return atomic_read(&md->event_nr); 26201da177e4SLinus Torvalds } 26211da177e4SLinus Torvalds 26221da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr) 26231da177e4SLinus Torvalds { 26241da177e4SLinus Torvalds return wait_event_interruptible(md->eventq, 26251da177e4SLinus Torvalds (event_nr != atomic_read(&md->event_nr))); 26261da177e4SLinus Torvalds } 26271da177e4SLinus Torvalds 26287a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist) 26297a8c3d3bSMike Anderson { 26307a8c3d3bSMike Anderson unsigned long flags; 26317a8c3d3bSMike Anderson 26327a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 26337a8c3d3bSMike Anderson list_add(elist, &md->uevent_list); 26347a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 26357a8c3d3bSMike Anderson } 26367a8c3d3bSMike Anderson 26371da177e4SLinus Torvalds /* 26381da177e4SLinus Torvalds * The gendisk is only valid as long as you have a reference 26391da177e4SLinus Torvalds * count on 'md'. 26401da177e4SLinus Torvalds */ 26411da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md) 26421da177e4SLinus Torvalds { 26431da177e4SLinus Torvalds return md->disk; 26441da177e4SLinus Torvalds } 26451da177e4SLinus Torvalds 2646784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md) 2647784aae73SMilan Broz { 2648784aae73SMilan Broz return &md->kobj; 2649784aae73SMilan Broz } 2650784aae73SMilan Broz 2651784aae73SMilan Broz /* 2652784aae73SMilan Broz * struct mapped_device should not be exported outside of dm.c 2653784aae73SMilan Broz * so use this check to verify that kobj is part of md structure 2654784aae73SMilan Broz */ 2655784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj) 2656784aae73SMilan Broz { 2657784aae73SMilan Broz struct mapped_device *md; 2658784aae73SMilan Broz 2659784aae73SMilan Broz md = container_of(kobj, struct mapped_device, kobj); 2660784aae73SMilan Broz if (&md->kobj != kobj) 2661784aae73SMilan Broz return NULL; 2662784aae73SMilan Broz 26634d89b7b4SMilan Broz if (test_bit(DMF_FREEING, &md->flags) || 2664432a212cSMike Anderson dm_deleting_md(md)) 26654d89b7b4SMilan Broz return NULL; 26664d89b7b4SMilan Broz 2667784aae73SMilan Broz dm_get(md); 2668784aae73SMilan Broz return md; 2669784aae73SMilan Broz } 2670784aae73SMilan Broz 26714f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md) 26721da177e4SLinus Torvalds { 26731da177e4SLinus Torvalds return test_bit(DMF_SUSPENDED, &md->flags); 26741da177e4SLinus Torvalds } 26751da177e4SLinus Torvalds 267664dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti) 267764dbce58SKiyoshi Ueda { 2678ecdb2e25SKiyoshi Ueda return dm_suspended_md(dm_table_get_md(ti->table)); 267964dbce58SKiyoshi Ueda } 268064dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended); 268164dbce58SKiyoshi Ueda 26822e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti) 26832e93ccc1SKiyoshi Ueda { 2684ecdb2e25SKiyoshi Ueda return __noflush_suspending(dm_table_get_md(ti->table)); 26852e93ccc1SKiyoshi Ueda } 26862e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending); 26872e93ccc1SKiyoshi Ueda 2688a91a2785SMartin K. Petersen struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity) 2689e6ee8c0bSKiyoshi Ueda { 2690e6ee8c0bSKiyoshi Ueda struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL); 2691a91a2785SMartin K. Petersen unsigned int pool_size = (type == DM_TYPE_BIO_BASED) ? 16 : MIN_IOS; 2692e6ee8c0bSKiyoshi Ueda 2693e6ee8c0bSKiyoshi Ueda if (!pools) 2694e6ee8c0bSKiyoshi Ueda return NULL; 2695e6ee8c0bSKiyoshi Ueda 2696e6ee8c0bSKiyoshi Ueda pools->io_pool = (type == DM_TYPE_BIO_BASED) ? 2697e6ee8c0bSKiyoshi Ueda mempool_create_slab_pool(MIN_IOS, _io_cache) : 2698e6ee8c0bSKiyoshi Ueda mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache); 2699e6ee8c0bSKiyoshi Ueda if (!pools->io_pool) 2700e6ee8c0bSKiyoshi Ueda goto free_pools_and_out; 2701e6ee8c0bSKiyoshi Ueda 2702e6ee8c0bSKiyoshi Ueda pools->tio_pool = (type == DM_TYPE_BIO_BASED) ? 2703e6ee8c0bSKiyoshi Ueda mempool_create_slab_pool(MIN_IOS, _tio_cache) : 2704e6ee8c0bSKiyoshi Ueda mempool_create_slab_pool(MIN_IOS, _rq_tio_cache); 2705e6ee8c0bSKiyoshi Ueda if (!pools->tio_pool) 2706e6ee8c0bSKiyoshi Ueda goto free_io_pool_and_out; 2707e6ee8c0bSKiyoshi Ueda 270894818742SKent Overstreet pools->bs = (type == DM_TYPE_BIO_BASED) ? 270994818742SKent Overstreet bioset_create(pool_size, 0) : 271094818742SKent Overstreet bioset_create(pool_size, 271194818742SKent Overstreet offsetof(struct dm_rq_clone_bio_info, clone)); 2712e6ee8c0bSKiyoshi Ueda if (!pools->bs) 2713e6ee8c0bSKiyoshi Ueda goto free_tio_pool_and_out; 2714e6ee8c0bSKiyoshi Ueda 2715a91a2785SMartin K. Petersen if (integrity && bioset_integrity_create(pools->bs, pool_size)) 2716a91a2785SMartin K. Petersen goto free_bioset_and_out; 2717a91a2785SMartin K. Petersen 2718e6ee8c0bSKiyoshi Ueda return pools; 2719e6ee8c0bSKiyoshi Ueda 2720a91a2785SMartin K. Petersen free_bioset_and_out: 2721a91a2785SMartin K. Petersen bioset_free(pools->bs); 2722a91a2785SMartin K. Petersen 2723e6ee8c0bSKiyoshi Ueda free_tio_pool_and_out: 2724e6ee8c0bSKiyoshi Ueda mempool_destroy(pools->tio_pool); 2725e6ee8c0bSKiyoshi Ueda 2726e6ee8c0bSKiyoshi Ueda free_io_pool_and_out: 2727e6ee8c0bSKiyoshi Ueda mempool_destroy(pools->io_pool); 2728e6ee8c0bSKiyoshi Ueda 2729e6ee8c0bSKiyoshi Ueda free_pools_and_out: 2730e6ee8c0bSKiyoshi Ueda kfree(pools); 2731e6ee8c0bSKiyoshi Ueda 2732e6ee8c0bSKiyoshi Ueda return NULL; 2733e6ee8c0bSKiyoshi Ueda } 2734e6ee8c0bSKiyoshi Ueda 2735e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools) 2736e6ee8c0bSKiyoshi Ueda { 2737e6ee8c0bSKiyoshi Ueda if (!pools) 2738e6ee8c0bSKiyoshi Ueda return; 2739e6ee8c0bSKiyoshi Ueda 2740e6ee8c0bSKiyoshi Ueda if (pools->io_pool) 2741e6ee8c0bSKiyoshi Ueda mempool_destroy(pools->io_pool); 2742e6ee8c0bSKiyoshi Ueda 2743e6ee8c0bSKiyoshi Ueda if (pools->tio_pool) 2744e6ee8c0bSKiyoshi Ueda mempool_destroy(pools->tio_pool); 2745e6ee8c0bSKiyoshi Ueda 2746e6ee8c0bSKiyoshi Ueda if (pools->bs) 2747e6ee8c0bSKiyoshi Ueda bioset_free(pools->bs); 2748e6ee8c0bSKiyoshi Ueda 2749e6ee8c0bSKiyoshi Ueda kfree(pools); 2750e6ee8c0bSKiyoshi Ueda } 2751e6ee8c0bSKiyoshi Ueda 275283d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = { 27531da177e4SLinus Torvalds .open = dm_blk_open, 27541da177e4SLinus Torvalds .release = dm_blk_close, 2755aa129a22SMilan Broz .ioctl = dm_blk_ioctl, 27563ac51e74SDarrick J. Wong .getgeo = dm_blk_getgeo, 27571da177e4SLinus Torvalds .owner = THIS_MODULE 27581da177e4SLinus Torvalds }; 27591da177e4SLinus Torvalds 27601da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo); 27611da177e4SLinus Torvalds 27621da177e4SLinus Torvalds /* 27631da177e4SLinus Torvalds * module hooks 27641da177e4SLinus Torvalds */ 27651da177e4SLinus Torvalds module_init(dm_init); 27661da177e4SLinus Torvalds module_exit(dm_exit); 27671da177e4SLinus Torvalds 27681da177e4SLinus Torvalds module_param(major, uint, 0); 27691da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper"); 27701da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver"); 27711da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 27721da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 2773