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 request-based dm. 678fbf26adSKiyoshi Ueda * One of these is allocated per request. 688fbf26adSKiyoshi Ueda */ 698fbf26adSKiyoshi Ueda struct dm_rq_target_io { 708fbf26adSKiyoshi Ueda struct mapped_device *md; 718fbf26adSKiyoshi Ueda struct dm_target *ti; 728fbf26adSKiyoshi Ueda struct request *orig, clone; 738fbf26adSKiyoshi Ueda int error; 748fbf26adSKiyoshi Ueda union map_info info; 758fbf26adSKiyoshi Ueda }; 768fbf26adSKiyoshi Ueda 778fbf26adSKiyoshi Ueda /* 7894818742SKent Overstreet * For request-based dm - the bio clones we allocate are embedded in these 7994818742SKent Overstreet * structs. 8094818742SKent Overstreet * 8194818742SKent Overstreet * We allocate these with bio_alloc_bioset, using the front_pad parameter when 8294818742SKent Overstreet * the bioset is created - this means the bio has to come at the end of the 8394818742SKent Overstreet * struct. 848fbf26adSKiyoshi Ueda */ 858fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info { 868fbf26adSKiyoshi Ueda struct bio *orig; 87cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio; 8894818742SKent Overstreet struct bio clone; 898fbf26adSKiyoshi Ueda }; 908fbf26adSKiyoshi Ueda 911da177e4SLinus Torvalds union map_info *dm_get_mapinfo(struct bio *bio) 921da177e4SLinus Torvalds { 931da177e4SLinus Torvalds if (bio && bio->bi_private) 94028867acSAlasdair G Kergon return &((struct dm_target_io *)bio->bi_private)->info; 951da177e4SLinus Torvalds return NULL; 961da177e4SLinus Torvalds } 971da177e4SLinus Torvalds 98cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq) 99cec47e3dSKiyoshi Ueda { 100cec47e3dSKiyoshi Ueda if (rq && rq->end_io_data) 101cec47e3dSKiyoshi Ueda return &((struct dm_rq_target_io *)rq->end_io_data)->info; 102cec47e3dSKiyoshi Ueda return NULL; 103cec47e3dSKiyoshi Ueda } 104cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo); 105cec47e3dSKiyoshi Ueda 106ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1) 107ba61fdd1SJeff Mahoney 1081da177e4SLinus Torvalds /* 1091da177e4SLinus Torvalds * Bits for the md->flags field. 1101da177e4SLinus Torvalds */ 1111eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0 1121da177e4SLinus Torvalds #define DMF_SUSPENDED 1 113aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2 114fba9f90eSJeff Mahoney #define DMF_FREEING 3 1155c6bd75dSAlasdair G Kergon #define DMF_DELETING 4 1162e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5 117d5b9dd04SMikulas Patocka #define DMF_MERGE_IS_OPTIONAL 6 1181da177e4SLinus Torvalds 119304f3f6aSMilan Broz /* 120304f3f6aSMilan Broz * Work processed by per-device workqueue. 121304f3f6aSMilan Broz */ 1221da177e4SLinus Torvalds struct mapped_device { 1232ca3310eSAlasdair G Kergon struct rw_semaphore io_lock; 124e61290a4SDaniel Walker struct mutex suspend_lock; 1251da177e4SLinus Torvalds rwlock_t map_lock; 1261da177e4SLinus Torvalds atomic_t holders; 1275c6bd75dSAlasdair G Kergon atomic_t open_count; 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds unsigned long flags; 1301da177e4SLinus Torvalds 131165125e1SJens Axboe struct request_queue *queue; 132a5664dadSMike Snitzer unsigned type; 1334a0b4ddfSMike Snitzer /* Protect queue and type against concurrent access. */ 134a5664dadSMike Snitzer struct mutex type_lock; 135a5664dadSMike Snitzer 13636a0456fSAlasdair G Kergon struct target_type *immutable_target_type; 13736a0456fSAlasdair G Kergon 1381da177e4SLinus Torvalds struct gendisk *disk; 1397e51f257SMike Anderson char name[16]; 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds void *interface_ptr; 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds /* 1441da177e4SLinus Torvalds * A list of ios that arrived while we were suspended. 1451da177e4SLinus Torvalds */ 146316d315bSNikanth Karthikesan atomic_t pending[2]; 1471da177e4SLinus Torvalds wait_queue_head_t wait; 14853d5914fSMikulas Patocka struct work_struct work; 1491da177e4SLinus Torvalds struct bio_list deferred; 150022c2611SMikulas Patocka spinlock_t deferred_lock; 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds /* 15329e4013dSTejun Heo * Processing queue (flush) 154304f3f6aSMilan Broz */ 155304f3f6aSMilan Broz struct workqueue_struct *wq; 156304f3f6aSMilan Broz 157304f3f6aSMilan Broz /* 1581da177e4SLinus Torvalds * The current mapping. 1591da177e4SLinus Torvalds */ 1601da177e4SLinus Torvalds struct dm_table *map; 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds /* 1631da177e4SLinus Torvalds * io objects are allocated from here. 1641da177e4SLinus Torvalds */ 1651da177e4SLinus Torvalds mempool_t *io_pool; 1661da177e4SLinus Torvalds mempool_t *tio_pool; 1671da177e4SLinus Torvalds 1689faf400fSStefan Bader struct bio_set *bs; 1699faf400fSStefan Bader 1701da177e4SLinus Torvalds /* 1711da177e4SLinus Torvalds * Event handling. 1721da177e4SLinus Torvalds */ 1731da177e4SLinus Torvalds atomic_t event_nr; 1741da177e4SLinus Torvalds wait_queue_head_t eventq; 1757a8c3d3bSMike Anderson atomic_t uevent_seq; 1767a8c3d3bSMike Anderson struct list_head uevent_list; 1777a8c3d3bSMike Anderson spinlock_t uevent_lock; /* Protect access to uevent_list */ 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds /* 1801da177e4SLinus Torvalds * freeze/thaw support require holding onto a super block 1811da177e4SLinus Torvalds */ 1821da177e4SLinus Torvalds struct super_block *frozen_sb; 183db8fef4fSMikulas Patocka struct block_device *bdev; 1843ac51e74SDarrick J. Wong 1853ac51e74SDarrick J. Wong /* forced geometry settings */ 1863ac51e74SDarrick J. Wong struct hd_geometry geometry; 187784aae73SMilan Broz 188784aae73SMilan Broz /* sysfs handle */ 189784aae73SMilan Broz struct kobject kobj; 19052b1fd5aSMikulas Patocka 191d87f4c14STejun Heo /* zero-length flush that will be cloned and submitted to targets */ 192d87f4c14STejun Heo struct bio flush_bio; 1931da177e4SLinus Torvalds }; 1941da177e4SLinus Torvalds 195e6ee8c0bSKiyoshi Ueda /* 196e6ee8c0bSKiyoshi Ueda * For mempools pre-allocation at the table loading time. 197e6ee8c0bSKiyoshi Ueda */ 198e6ee8c0bSKiyoshi Ueda struct dm_md_mempools { 199e6ee8c0bSKiyoshi Ueda mempool_t *io_pool; 200e6ee8c0bSKiyoshi Ueda mempool_t *tio_pool; 201e6ee8c0bSKiyoshi Ueda struct bio_set *bs; 202e6ee8c0bSKiyoshi Ueda }; 203e6ee8c0bSKiyoshi Ueda 2041da177e4SLinus Torvalds #define MIN_IOS 256 205e18b890bSChristoph Lameter static struct kmem_cache *_io_cache; 2068fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache; 20794818742SKent Overstreet 20894818742SKent Overstreet /* 20994818742SKent Overstreet * Unused now, and needs to be deleted. But since io_pool is overloaded and it's 21094818742SKent Overstreet * still used for _io_cache, I'm leaving this for a later cleanup 21194818742SKent Overstreet */ 2128fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_bio_info_cache; 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds static int __init local_init(void) 2151da177e4SLinus Torvalds { 21651157b4aSKiyoshi Ueda int r = -ENOMEM; 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds /* allocate a slab for the dm_ios */ 219028867acSAlasdair G Kergon _io_cache = KMEM_CACHE(dm_io, 0); 2201da177e4SLinus Torvalds if (!_io_cache) 22151157b4aSKiyoshi Ueda return r; 2221da177e4SLinus Torvalds 2238fbf26adSKiyoshi Ueda _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0); 2248fbf26adSKiyoshi Ueda if (!_rq_tio_cache) 225dba14160SMikulas Patocka goto out_free_io_cache; 2268fbf26adSKiyoshi Ueda 2278fbf26adSKiyoshi Ueda _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0); 2288fbf26adSKiyoshi Ueda if (!_rq_bio_info_cache) 2298fbf26adSKiyoshi Ueda goto out_free_rq_tio_cache; 2308fbf26adSKiyoshi Ueda 23151e5b2bdSMike Anderson r = dm_uevent_init(); 23251157b4aSKiyoshi Ueda if (r) 2338fbf26adSKiyoshi Ueda goto out_free_rq_bio_info_cache; 23451e5b2bdSMike Anderson 2351da177e4SLinus Torvalds _major = major; 2361da177e4SLinus Torvalds r = register_blkdev(_major, _name); 23751157b4aSKiyoshi Ueda if (r < 0) 23851157b4aSKiyoshi Ueda goto out_uevent_exit; 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds if (!_major) 2411da177e4SLinus Torvalds _major = r; 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds return 0; 24451157b4aSKiyoshi Ueda 24551157b4aSKiyoshi Ueda out_uevent_exit: 24651157b4aSKiyoshi Ueda dm_uevent_exit(); 2478fbf26adSKiyoshi Ueda out_free_rq_bio_info_cache: 2488fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_bio_info_cache); 2498fbf26adSKiyoshi Ueda out_free_rq_tio_cache: 2508fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_tio_cache); 25151157b4aSKiyoshi Ueda out_free_io_cache: 25251157b4aSKiyoshi Ueda kmem_cache_destroy(_io_cache); 25351157b4aSKiyoshi Ueda 25451157b4aSKiyoshi Ueda return r; 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds static void local_exit(void) 2581da177e4SLinus Torvalds { 2598fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_bio_info_cache); 2608fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_tio_cache); 2611da177e4SLinus Torvalds kmem_cache_destroy(_io_cache); 26200d59405SAkinobu Mita unregister_blkdev(_major, _name); 26351e5b2bdSMike Anderson dm_uevent_exit(); 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds _major = 0; 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds DMINFO("cleaned up"); 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds 270b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = { 2711da177e4SLinus Torvalds local_init, 2721da177e4SLinus Torvalds dm_target_init, 2731da177e4SLinus Torvalds dm_linear_init, 2741da177e4SLinus Torvalds dm_stripe_init, 275952b3557SMikulas Patocka dm_io_init, 276945fa4d2SMikulas Patocka dm_kcopyd_init, 2771da177e4SLinus Torvalds dm_interface_init, 2781da177e4SLinus Torvalds }; 2791da177e4SLinus Torvalds 280b9249e55SAlasdair G Kergon static void (*_exits[])(void) = { 2811da177e4SLinus Torvalds local_exit, 2821da177e4SLinus Torvalds dm_target_exit, 2831da177e4SLinus Torvalds dm_linear_exit, 2841da177e4SLinus Torvalds dm_stripe_exit, 285952b3557SMikulas Patocka dm_io_exit, 286945fa4d2SMikulas Patocka dm_kcopyd_exit, 2871da177e4SLinus Torvalds dm_interface_exit, 2881da177e4SLinus Torvalds }; 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds static int __init dm_init(void) 2911da177e4SLinus Torvalds { 2921da177e4SLinus Torvalds const int count = ARRAY_SIZE(_inits); 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds int r, i; 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds for (i = 0; i < count; i++) { 2971da177e4SLinus Torvalds r = _inits[i](); 2981da177e4SLinus Torvalds if (r) 2991da177e4SLinus Torvalds goto bad; 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds return 0; 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds bad: 3051da177e4SLinus Torvalds while (i--) 3061da177e4SLinus Torvalds _exits[i](); 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds return r; 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds static void __exit dm_exit(void) 3121da177e4SLinus Torvalds { 3131da177e4SLinus Torvalds int i = ARRAY_SIZE(_exits); 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds while (i--) 3161da177e4SLinus Torvalds _exits[i](); 317d15b774cSAlasdair G Kergon 318d15b774cSAlasdair G Kergon /* 319d15b774cSAlasdair G Kergon * Should be empty by this point. 320d15b774cSAlasdair G Kergon */ 321d15b774cSAlasdair G Kergon idr_destroy(&_minor_idr); 3221da177e4SLinus Torvalds } 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds /* 3251da177e4SLinus Torvalds * Block device functions 3261da177e4SLinus Torvalds */ 327432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md) 328432a212cSMike Anderson { 329432a212cSMike Anderson return test_bit(DMF_DELETING, &md->flags); 330432a212cSMike Anderson } 331432a212cSMike Anderson 332fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode) 3331da177e4SLinus Torvalds { 3341da177e4SLinus Torvalds struct mapped_device *md; 3351da177e4SLinus Torvalds 336fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 337fba9f90eSJeff Mahoney 338fe5f9f2cSAl Viro md = bdev->bd_disk->private_data; 339fba9f90eSJeff Mahoney if (!md) 340fba9f90eSJeff Mahoney goto out; 341fba9f90eSJeff Mahoney 3425c6bd75dSAlasdair G Kergon if (test_bit(DMF_FREEING, &md->flags) || 343432a212cSMike Anderson dm_deleting_md(md)) { 344fba9f90eSJeff Mahoney md = NULL; 345fba9f90eSJeff Mahoney goto out; 346fba9f90eSJeff Mahoney } 347fba9f90eSJeff Mahoney 3481da177e4SLinus Torvalds dm_get(md); 3495c6bd75dSAlasdair G Kergon atomic_inc(&md->open_count); 350fba9f90eSJeff Mahoney 351fba9f90eSJeff Mahoney out: 352fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 353fba9f90eSJeff Mahoney 354fba9f90eSJeff Mahoney return md ? 0 : -ENXIO; 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds 357fe5f9f2cSAl Viro static int dm_blk_close(struct gendisk *disk, fmode_t mode) 3581da177e4SLinus Torvalds { 359fe5f9f2cSAl Viro struct mapped_device *md = disk->private_data; 3606e9624b8SArnd Bergmann 3614a1aeb98SMilan Broz spin_lock(&_minor_lock); 3624a1aeb98SMilan Broz 3635c6bd75dSAlasdair G Kergon atomic_dec(&md->open_count); 3641da177e4SLinus Torvalds dm_put(md); 3654a1aeb98SMilan Broz 3664a1aeb98SMilan Broz spin_unlock(&_minor_lock); 3676e9624b8SArnd Bergmann 3681da177e4SLinus Torvalds return 0; 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds 3715c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md) 3725c6bd75dSAlasdair G Kergon { 3735c6bd75dSAlasdair G Kergon return atomic_read(&md->open_count); 3745c6bd75dSAlasdair G Kergon } 3755c6bd75dSAlasdair G Kergon 3765c6bd75dSAlasdair G Kergon /* 3775c6bd75dSAlasdair G Kergon * Guarantees nothing is using the device before it's deleted. 3785c6bd75dSAlasdair G Kergon */ 3795c6bd75dSAlasdair G Kergon int dm_lock_for_deletion(struct mapped_device *md) 3805c6bd75dSAlasdair G Kergon { 3815c6bd75dSAlasdair G Kergon int r = 0; 3825c6bd75dSAlasdair G Kergon 3835c6bd75dSAlasdair G Kergon spin_lock(&_minor_lock); 3845c6bd75dSAlasdair G Kergon 3855c6bd75dSAlasdair G Kergon if (dm_open_count(md)) 3865c6bd75dSAlasdair G Kergon r = -EBUSY; 3875c6bd75dSAlasdair G Kergon else 3885c6bd75dSAlasdair G Kergon set_bit(DMF_DELETING, &md->flags); 3895c6bd75dSAlasdair G Kergon 3905c6bd75dSAlasdair G Kergon spin_unlock(&_minor_lock); 3915c6bd75dSAlasdair G Kergon 3925c6bd75dSAlasdair G Kergon return r; 3935c6bd75dSAlasdair G Kergon } 3945c6bd75dSAlasdair G Kergon 3953ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 3963ac51e74SDarrick J. Wong { 3973ac51e74SDarrick J. Wong struct mapped_device *md = bdev->bd_disk->private_data; 3983ac51e74SDarrick J. Wong 3993ac51e74SDarrick J. Wong return dm_get_geometry(md, geo); 4003ac51e74SDarrick J. Wong } 4013ac51e74SDarrick J. Wong 402fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode, 403aa129a22SMilan Broz unsigned int cmd, unsigned long arg) 404aa129a22SMilan Broz { 405fe5f9f2cSAl Viro struct mapped_device *md = bdev->bd_disk->private_data; 4067c666411SAlasdair G Kergon struct dm_table *map = dm_get_live_table(md); 407aa129a22SMilan Broz struct dm_target *tgt; 408aa129a22SMilan Broz int r = -ENOTTY; 409aa129a22SMilan Broz 410aa129a22SMilan Broz if (!map || !dm_table_get_size(map)) 411aa129a22SMilan Broz goto out; 412aa129a22SMilan Broz 413aa129a22SMilan Broz /* We only support devices that have a single target */ 414aa129a22SMilan Broz if (dm_table_get_num_targets(map) != 1) 415aa129a22SMilan Broz goto out; 416aa129a22SMilan Broz 417aa129a22SMilan Broz tgt = dm_table_get_target(map, 0); 418aa129a22SMilan Broz 4194f186f8bSKiyoshi Ueda if (dm_suspended_md(md)) { 420aa129a22SMilan Broz r = -EAGAIN; 421aa129a22SMilan Broz goto out; 422aa129a22SMilan Broz } 423aa129a22SMilan Broz 424aa129a22SMilan Broz if (tgt->type->ioctl) 425647b3d00SAl Viro r = tgt->type->ioctl(tgt, cmd, arg); 426aa129a22SMilan Broz 427aa129a22SMilan Broz out: 428aa129a22SMilan Broz dm_table_put(map); 429aa129a22SMilan Broz 430aa129a22SMilan Broz return r; 431aa129a22SMilan Broz } 432aa129a22SMilan Broz 433028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md) 4341da177e4SLinus Torvalds { 4351da177e4SLinus Torvalds return mempool_alloc(md->io_pool, GFP_NOIO); 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds 438028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io) 4391da177e4SLinus Torvalds { 4401da177e4SLinus Torvalds mempool_free(io, md->io_pool); 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds 443028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio) 4441da177e4SLinus Torvalds { 445dba14160SMikulas Patocka bio_put(&tio->clone); 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds 44808885643SKiyoshi Ueda static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md, 44908885643SKiyoshi Ueda gfp_t gfp_mask) 450cec47e3dSKiyoshi Ueda { 45108885643SKiyoshi Ueda return mempool_alloc(md->tio_pool, gfp_mask); 452cec47e3dSKiyoshi Ueda } 453cec47e3dSKiyoshi Ueda 454cec47e3dSKiyoshi Ueda static void free_rq_tio(struct dm_rq_target_io *tio) 455cec47e3dSKiyoshi Ueda { 456cec47e3dSKiyoshi Ueda mempool_free(tio, tio->md->tio_pool); 457cec47e3dSKiyoshi Ueda } 458cec47e3dSKiyoshi Ueda 45990abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md) 46090abb8c4SKiyoshi Ueda { 46190abb8c4SKiyoshi Ueda return atomic_read(&md->pending[READ]) + 46290abb8c4SKiyoshi Ueda atomic_read(&md->pending[WRITE]); 46390abb8c4SKiyoshi Ueda } 46490abb8c4SKiyoshi Ueda 4653eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io) 4663eaf840eSJun'ichi "Nick" Nomura { 4673eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 468c9959059STejun Heo int cpu; 469316d315bSNikanth Karthikesan int rw = bio_data_dir(io->bio); 4703eaf840eSJun'ichi "Nick" Nomura 4713eaf840eSJun'ichi "Nick" Nomura io->start_time = jiffies; 4723eaf840eSJun'ichi "Nick" Nomura 473074a7acaSTejun Heo cpu = part_stat_lock(); 474074a7acaSTejun Heo part_round_stats(cpu, &dm_disk(md)->part0); 475074a7acaSTejun Heo part_stat_unlock(); 4761e9bb880SShaohua Li atomic_set(&dm_disk(md)->part0.in_flight[rw], 4771e9bb880SShaohua Li atomic_inc_return(&md->pending[rw])); 4783eaf840eSJun'ichi "Nick" Nomura } 4793eaf840eSJun'ichi "Nick" Nomura 480d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io) 4813eaf840eSJun'ichi "Nick" Nomura { 4823eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 4833eaf840eSJun'ichi "Nick" Nomura struct bio *bio = io->bio; 4843eaf840eSJun'ichi "Nick" Nomura unsigned long duration = jiffies - io->start_time; 485c9959059STejun Heo int pending, cpu; 4863eaf840eSJun'ichi "Nick" Nomura int rw = bio_data_dir(bio); 4873eaf840eSJun'ichi "Nick" Nomura 488074a7acaSTejun Heo cpu = part_stat_lock(); 489074a7acaSTejun Heo part_round_stats(cpu, &dm_disk(md)->part0); 490074a7acaSTejun Heo part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration); 491074a7acaSTejun Heo part_stat_unlock(); 4923eaf840eSJun'ichi "Nick" Nomura 493af7e466aSMikulas Patocka /* 494af7e466aSMikulas Patocka * After this is decremented the bio must not be touched if it is 495d87f4c14STejun Heo * a flush. 496af7e466aSMikulas Patocka */ 4971e9bb880SShaohua Li pending = atomic_dec_return(&md->pending[rw]); 4981e9bb880SShaohua Li atomic_set(&dm_disk(md)->part0.in_flight[rw], pending); 499316d315bSNikanth Karthikesan pending += atomic_read(&md->pending[rw^0x1]); 5003eaf840eSJun'ichi "Nick" Nomura 501d221d2e7SMikulas Patocka /* nudge anyone waiting on suspend queue */ 502d221d2e7SMikulas Patocka if (!pending) 503d221d2e7SMikulas Patocka wake_up(&md->wait); 5043eaf840eSJun'ichi "Nick" Nomura } 5053eaf840eSJun'ichi "Nick" Nomura 5061da177e4SLinus Torvalds /* 5071da177e4SLinus Torvalds * Add the bio to the list of deferred io. 5081da177e4SLinus Torvalds */ 50992c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio) 5101da177e4SLinus Torvalds { 51105447420SKiyoshi Ueda unsigned long flags; 5121da177e4SLinus Torvalds 51305447420SKiyoshi Ueda spin_lock_irqsave(&md->deferred_lock, flags); 5141da177e4SLinus Torvalds bio_list_add(&md->deferred, bio); 51505447420SKiyoshi Ueda spin_unlock_irqrestore(&md->deferred_lock, flags); 51692c63902SMikulas Patocka queue_work(md->wq, &md->work); 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds /* 5201da177e4SLinus Torvalds * Everyone (including functions in this file), should use this 5211da177e4SLinus Torvalds * function to access the md->map field, and make sure they call 5221da177e4SLinus Torvalds * dm_table_put() when finished. 5231da177e4SLinus Torvalds */ 5247c666411SAlasdair G Kergon struct dm_table *dm_get_live_table(struct mapped_device *md) 5251da177e4SLinus Torvalds { 5261da177e4SLinus Torvalds struct dm_table *t; 527523d9297SKiyoshi Ueda unsigned long flags; 5281da177e4SLinus Torvalds 529523d9297SKiyoshi Ueda read_lock_irqsave(&md->map_lock, flags); 5301da177e4SLinus Torvalds t = md->map; 5311da177e4SLinus Torvalds if (t) 5321da177e4SLinus Torvalds dm_table_get(t); 533523d9297SKiyoshi Ueda read_unlock_irqrestore(&md->map_lock, flags); 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds return t; 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds 5383ac51e74SDarrick J. Wong /* 5393ac51e74SDarrick J. Wong * Get the geometry associated with a dm device 5403ac51e74SDarrick J. Wong */ 5413ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) 5423ac51e74SDarrick J. Wong { 5433ac51e74SDarrick J. Wong *geo = md->geometry; 5443ac51e74SDarrick J. Wong 5453ac51e74SDarrick J. Wong return 0; 5463ac51e74SDarrick J. Wong } 5473ac51e74SDarrick J. Wong 5483ac51e74SDarrick J. Wong /* 5493ac51e74SDarrick J. Wong * Set the geometry of a device. 5503ac51e74SDarrick J. Wong */ 5513ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) 5523ac51e74SDarrick J. Wong { 5533ac51e74SDarrick J. Wong sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; 5543ac51e74SDarrick J. Wong 5553ac51e74SDarrick J. Wong if (geo->start > sz) { 5563ac51e74SDarrick J. Wong DMWARN("Start sector is beyond the geometry limits."); 5573ac51e74SDarrick J. Wong return -EINVAL; 5583ac51e74SDarrick J. Wong } 5593ac51e74SDarrick J. Wong 5603ac51e74SDarrick J. Wong md->geometry = *geo; 5613ac51e74SDarrick J. Wong 5623ac51e74SDarrick J. Wong return 0; 5633ac51e74SDarrick J. Wong } 5643ac51e74SDarrick J. Wong 5651da177e4SLinus Torvalds /*----------------------------------------------------------------- 5661da177e4SLinus Torvalds * CRUD START: 5671da177e4SLinus Torvalds * A more elegant soln is in the works that uses the queue 5681da177e4SLinus Torvalds * merge fn, unfortunately there are a couple of changes to 5691da177e4SLinus Torvalds * the block layer that I want to make for this. So in the 5701da177e4SLinus Torvalds * interests of getting something for people to use I give 5711da177e4SLinus Torvalds * you this clearly demarcated crap. 5721da177e4SLinus Torvalds *---------------------------------------------------------------*/ 5731da177e4SLinus Torvalds 5742e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md) 5752e93ccc1SKiyoshi Ueda { 5762e93ccc1SKiyoshi Ueda return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 5772e93ccc1SKiyoshi Ueda } 5782e93ccc1SKiyoshi Ueda 5791da177e4SLinus Torvalds /* 5801da177e4SLinus Torvalds * Decrements the number of outstanding ios that a bio has been 5811da177e4SLinus Torvalds * cloned into, completing the original io if necc. 5821da177e4SLinus Torvalds */ 583858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error) 5841da177e4SLinus Torvalds { 5852e93ccc1SKiyoshi Ueda unsigned long flags; 586b35f8caaSMilan Broz int io_error; 587b35f8caaSMilan Broz struct bio *bio; 588b35f8caaSMilan Broz struct mapped_device *md = io->md; 5892e93ccc1SKiyoshi Ueda 5902e93ccc1SKiyoshi Ueda /* Push-back supersedes any I/O errors */ 591f88fb981SKiyoshi Ueda if (unlikely(error)) { 592f88fb981SKiyoshi Ueda spin_lock_irqsave(&io->endio_lock, flags); 593f88fb981SKiyoshi Ueda if (!(io->error > 0 && __noflush_suspending(md))) 5941da177e4SLinus Torvalds io->error = error; 595f88fb981SKiyoshi Ueda spin_unlock_irqrestore(&io->endio_lock, flags); 596f88fb981SKiyoshi Ueda } 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds if (atomic_dec_and_test(&io->io_count)) { 5992e93ccc1SKiyoshi Ueda if (io->error == DM_ENDIO_REQUEUE) { 6002e93ccc1SKiyoshi Ueda /* 6012e93ccc1SKiyoshi Ueda * Target requested pushing back the I/O. 6022e93ccc1SKiyoshi Ueda */ 603022c2611SMikulas Patocka spin_lock_irqsave(&md->deferred_lock, flags); 6046a8736d1STejun Heo if (__noflush_suspending(md)) 6056a8736d1STejun Heo bio_list_add_head(&md->deferred, io->bio); 6066a8736d1STejun Heo else 6072e93ccc1SKiyoshi Ueda /* noflush suspend was interrupted. */ 6082e93ccc1SKiyoshi Ueda io->error = -EIO; 609022c2611SMikulas Patocka spin_unlock_irqrestore(&md->deferred_lock, flags); 6102e93ccc1SKiyoshi Ueda } 6112e93ccc1SKiyoshi Ueda 612b35f8caaSMilan Broz io_error = io->error; 613b35f8caaSMilan Broz bio = io->bio; 614af7e466aSMikulas Patocka end_io_acct(io); 615a97f925aSMikulas Patocka free_io(md, io); 6161da177e4SLinus Torvalds 6176a8736d1STejun Heo if (io_error == DM_ENDIO_REQUEUE) 6186a8736d1STejun Heo return; 6196a8736d1STejun Heo 620b372d360SMike Snitzer if ((bio->bi_rw & REQ_FLUSH) && bio->bi_size) { 6211da177e4SLinus Torvalds /* 6226a8736d1STejun Heo * Preflush done for flush with data, reissue 6236a8736d1STejun Heo * without REQ_FLUSH. 6241da177e4SLinus Torvalds */ 6256a8736d1STejun Heo bio->bi_rw &= ~REQ_FLUSH; 6266a8736d1STejun Heo queue_io(md, bio); 6275f3ea37cSArnaldo Carvalho de Melo } else { 628b372d360SMike Snitzer /* done with normal IO or empty flush */ 629b7908c10SJeff Moyer trace_block_bio_complete(md->queue, bio, io_error); 630b35f8caaSMilan Broz bio_endio(bio, io_error); 6312e93ccc1SKiyoshi Ueda } 6321da177e4SLinus Torvalds } 633af7e466aSMikulas Patocka } 6341da177e4SLinus Torvalds 6356712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error) 6361da177e4SLinus Torvalds { 6371da177e4SLinus Torvalds int r = 0; 638028867acSAlasdair G Kergon struct dm_target_io *tio = bio->bi_private; 639b35f8caaSMilan Broz struct dm_io *io = tio->io; 6409faf400fSStefan Bader struct mapped_device *md = tio->io->md; 6411da177e4SLinus Torvalds dm_endio_fn endio = tio->ti->type->end_io; 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds if (!bio_flagged(bio, BIO_UPTODATE) && !error) 6441da177e4SLinus Torvalds error = -EIO; 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds if (endio) { 6477de3ee57SMikulas Patocka r = endio(tio->ti, bio, error); 6482e93ccc1SKiyoshi Ueda if (r < 0 || r == DM_ENDIO_REQUEUE) 6492e93ccc1SKiyoshi Ueda /* 6502e93ccc1SKiyoshi Ueda * error and requeue request are handled 6512e93ccc1SKiyoshi Ueda * in dec_pending(). 6522e93ccc1SKiyoshi Ueda */ 6531da177e4SLinus Torvalds error = r; 65445cbcd79SKiyoshi Ueda else if (r == DM_ENDIO_INCOMPLETE) 65545cbcd79SKiyoshi Ueda /* The target will handle the io */ 6566712ecf8SNeilBrown return; 65745cbcd79SKiyoshi Ueda else if (r) { 65845cbcd79SKiyoshi Ueda DMWARN("unimplemented target endio return value: %d", r); 65945cbcd79SKiyoshi Ueda BUG(); 66045cbcd79SKiyoshi Ueda } 6611da177e4SLinus Torvalds } 6621da177e4SLinus Torvalds 6639faf400fSStefan Bader free_tio(md, tio); 664b35f8caaSMilan Broz dec_pending(io, error); 6651da177e4SLinus Torvalds } 6661da177e4SLinus Torvalds 667cec47e3dSKiyoshi Ueda /* 668cec47e3dSKiyoshi Ueda * Partial completion handling for request-based dm 669cec47e3dSKiyoshi Ueda */ 670cec47e3dSKiyoshi Ueda static void end_clone_bio(struct bio *clone, int error) 671cec47e3dSKiyoshi Ueda { 672cec47e3dSKiyoshi Ueda struct dm_rq_clone_bio_info *info = clone->bi_private; 673cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = info->tio; 674cec47e3dSKiyoshi Ueda struct bio *bio = info->orig; 675cec47e3dSKiyoshi Ueda unsigned int nr_bytes = info->orig->bi_size; 676cec47e3dSKiyoshi Ueda 677cec47e3dSKiyoshi Ueda bio_put(clone); 678cec47e3dSKiyoshi Ueda 679cec47e3dSKiyoshi Ueda if (tio->error) 680cec47e3dSKiyoshi Ueda /* 681cec47e3dSKiyoshi Ueda * An error has already been detected on the request. 682cec47e3dSKiyoshi Ueda * Once error occurred, just let clone->end_io() handle 683cec47e3dSKiyoshi Ueda * the remainder. 684cec47e3dSKiyoshi Ueda */ 685cec47e3dSKiyoshi Ueda return; 686cec47e3dSKiyoshi Ueda else if (error) { 687cec47e3dSKiyoshi Ueda /* 688cec47e3dSKiyoshi Ueda * Don't notice the error to the upper layer yet. 689cec47e3dSKiyoshi Ueda * The error handling decision is made by the target driver, 690cec47e3dSKiyoshi Ueda * when the request is completed. 691cec47e3dSKiyoshi Ueda */ 692cec47e3dSKiyoshi Ueda tio->error = error; 693cec47e3dSKiyoshi Ueda return; 694cec47e3dSKiyoshi Ueda } 695cec47e3dSKiyoshi Ueda 696cec47e3dSKiyoshi Ueda /* 697cec47e3dSKiyoshi Ueda * I/O for the bio successfully completed. 698cec47e3dSKiyoshi Ueda * Notice the data completion to the upper layer. 699cec47e3dSKiyoshi Ueda */ 700cec47e3dSKiyoshi Ueda 701cec47e3dSKiyoshi Ueda /* 702cec47e3dSKiyoshi Ueda * bios are processed from the head of the list. 703cec47e3dSKiyoshi Ueda * So the completing bio should always be rq->bio. 704cec47e3dSKiyoshi Ueda * If it's not, something wrong is happening. 705cec47e3dSKiyoshi Ueda */ 706cec47e3dSKiyoshi Ueda if (tio->orig->bio != bio) 707cec47e3dSKiyoshi Ueda DMERR("bio completion is going in the middle of the request"); 708cec47e3dSKiyoshi Ueda 709cec47e3dSKiyoshi Ueda /* 710cec47e3dSKiyoshi Ueda * Update the original request. 711cec47e3dSKiyoshi Ueda * Do not use blk_end_request() here, because it may complete 712cec47e3dSKiyoshi Ueda * the original request before the clone, and break the ordering. 713cec47e3dSKiyoshi Ueda */ 714cec47e3dSKiyoshi Ueda blk_update_request(tio->orig, 0, nr_bytes); 715cec47e3dSKiyoshi Ueda } 716cec47e3dSKiyoshi Ueda 717cec47e3dSKiyoshi Ueda /* 718cec47e3dSKiyoshi Ueda * Don't touch any member of the md after calling this function because 719cec47e3dSKiyoshi Ueda * the md may be freed in dm_put() at the end of this function. 720cec47e3dSKiyoshi Ueda * Or do dm_get() before calling this function and dm_put() later. 721cec47e3dSKiyoshi Ueda */ 722b4324feeSKiyoshi Ueda static void rq_completed(struct mapped_device *md, int rw, int run_queue) 723cec47e3dSKiyoshi Ueda { 724b4324feeSKiyoshi Ueda atomic_dec(&md->pending[rw]); 725cec47e3dSKiyoshi Ueda 726cec47e3dSKiyoshi Ueda /* nudge anyone waiting on suspend queue */ 727b4324feeSKiyoshi Ueda if (!md_in_flight(md)) 728cec47e3dSKiyoshi Ueda wake_up(&md->wait); 729cec47e3dSKiyoshi Ueda 730a8c32a5cSJens Axboe /* 731a8c32a5cSJens Axboe * Run this off this callpath, as drivers could invoke end_io while 732a8c32a5cSJens Axboe * inside their request_fn (and holding the queue lock). Calling 733a8c32a5cSJens Axboe * back into ->request_fn() could deadlock attempting to grab the 734a8c32a5cSJens Axboe * queue lock again. 735a8c32a5cSJens Axboe */ 736cec47e3dSKiyoshi Ueda if (run_queue) 737a8c32a5cSJens Axboe blk_run_queue_async(md->queue); 738cec47e3dSKiyoshi Ueda 739cec47e3dSKiyoshi Ueda /* 740cec47e3dSKiyoshi Ueda * dm_put() must be at the end of this function. See the comment above 741cec47e3dSKiyoshi Ueda */ 742cec47e3dSKiyoshi Ueda dm_put(md); 743cec47e3dSKiyoshi Ueda } 744cec47e3dSKiyoshi Ueda 745a77e28c7SKiyoshi Ueda static void free_rq_clone(struct request *clone) 746a77e28c7SKiyoshi Ueda { 747a77e28c7SKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 748a77e28c7SKiyoshi Ueda 749a77e28c7SKiyoshi Ueda blk_rq_unprep_clone(clone); 750a77e28c7SKiyoshi Ueda free_rq_tio(tio); 751a77e28c7SKiyoshi Ueda } 752a77e28c7SKiyoshi Ueda 753980691e5SKiyoshi Ueda /* 754980691e5SKiyoshi Ueda * Complete the clone and the original request. 755980691e5SKiyoshi Ueda * Must be called without queue lock. 756980691e5SKiyoshi Ueda */ 757980691e5SKiyoshi Ueda static void dm_end_request(struct request *clone, int error) 758980691e5SKiyoshi Ueda { 759980691e5SKiyoshi Ueda int rw = rq_data_dir(clone); 760980691e5SKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 761980691e5SKiyoshi Ueda struct mapped_device *md = tio->md; 762980691e5SKiyoshi Ueda struct request *rq = tio->orig; 763980691e5SKiyoshi Ueda 76429e4013dSTejun Heo if (rq->cmd_type == REQ_TYPE_BLOCK_PC) { 765980691e5SKiyoshi Ueda rq->errors = clone->errors; 766980691e5SKiyoshi Ueda rq->resid_len = clone->resid_len; 767980691e5SKiyoshi Ueda 768980691e5SKiyoshi Ueda if (rq->sense) 769980691e5SKiyoshi Ueda /* 770980691e5SKiyoshi Ueda * We are using the sense buffer of the original 771980691e5SKiyoshi Ueda * request. 772980691e5SKiyoshi Ueda * So setting the length of the sense data is enough. 773980691e5SKiyoshi Ueda */ 774980691e5SKiyoshi Ueda rq->sense_len = clone->sense_len; 775980691e5SKiyoshi Ueda } 776980691e5SKiyoshi Ueda 777980691e5SKiyoshi Ueda free_rq_clone(clone); 778980691e5SKiyoshi Ueda blk_end_request_all(rq, error); 77929e4013dSTejun Heo rq_completed(md, rw, true); 780980691e5SKiyoshi Ueda } 781980691e5SKiyoshi Ueda 782cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq) 783cec47e3dSKiyoshi Ueda { 784cec47e3dSKiyoshi Ueda struct request *clone = rq->special; 785cec47e3dSKiyoshi Ueda 786cec47e3dSKiyoshi Ueda rq->special = NULL; 787cec47e3dSKiyoshi Ueda rq->cmd_flags &= ~REQ_DONTPREP; 788cec47e3dSKiyoshi Ueda 789a77e28c7SKiyoshi Ueda free_rq_clone(clone); 790cec47e3dSKiyoshi Ueda } 791cec47e3dSKiyoshi Ueda 792cec47e3dSKiyoshi Ueda /* 793cec47e3dSKiyoshi Ueda * Requeue the original request of a clone. 794cec47e3dSKiyoshi Ueda */ 795cec47e3dSKiyoshi Ueda void dm_requeue_unmapped_request(struct request *clone) 796cec47e3dSKiyoshi Ueda { 797b4324feeSKiyoshi Ueda int rw = rq_data_dir(clone); 798cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 799cec47e3dSKiyoshi Ueda struct mapped_device *md = tio->md; 800cec47e3dSKiyoshi Ueda struct request *rq = tio->orig; 801cec47e3dSKiyoshi Ueda struct request_queue *q = rq->q; 802cec47e3dSKiyoshi Ueda unsigned long flags; 803cec47e3dSKiyoshi Ueda 804cec47e3dSKiyoshi Ueda dm_unprep_request(rq); 805cec47e3dSKiyoshi Ueda 806cec47e3dSKiyoshi Ueda spin_lock_irqsave(q->queue_lock, flags); 807cec47e3dSKiyoshi Ueda blk_requeue_request(q, rq); 808cec47e3dSKiyoshi Ueda spin_unlock_irqrestore(q->queue_lock, flags); 809cec47e3dSKiyoshi Ueda 810b4324feeSKiyoshi Ueda rq_completed(md, rw, 0); 811cec47e3dSKiyoshi Ueda } 812cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request); 813cec47e3dSKiyoshi Ueda 814cec47e3dSKiyoshi Ueda static void __stop_queue(struct request_queue *q) 815cec47e3dSKiyoshi Ueda { 816cec47e3dSKiyoshi Ueda blk_stop_queue(q); 817cec47e3dSKiyoshi Ueda } 818cec47e3dSKiyoshi Ueda 819cec47e3dSKiyoshi Ueda static void stop_queue(struct request_queue *q) 820cec47e3dSKiyoshi Ueda { 821cec47e3dSKiyoshi Ueda unsigned long flags; 822cec47e3dSKiyoshi Ueda 823cec47e3dSKiyoshi Ueda spin_lock_irqsave(q->queue_lock, flags); 824cec47e3dSKiyoshi Ueda __stop_queue(q); 825cec47e3dSKiyoshi Ueda spin_unlock_irqrestore(q->queue_lock, flags); 826cec47e3dSKiyoshi Ueda } 827cec47e3dSKiyoshi Ueda 828cec47e3dSKiyoshi Ueda static void __start_queue(struct request_queue *q) 829cec47e3dSKiyoshi Ueda { 830cec47e3dSKiyoshi Ueda if (blk_queue_stopped(q)) 831cec47e3dSKiyoshi Ueda blk_start_queue(q); 832cec47e3dSKiyoshi Ueda } 833cec47e3dSKiyoshi Ueda 834cec47e3dSKiyoshi Ueda static void start_queue(struct request_queue *q) 835cec47e3dSKiyoshi Ueda { 836cec47e3dSKiyoshi Ueda unsigned long flags; 837cec47e3dSKiyoshi Ueda 838cec47e3dSKiyoshi Ueda spin_lock_irqsave(q->queue_lock, flags); 839cec47e3dSKiyoshi Ueda __start_queue(q); 840cec47e3dSKiyoshi Ueda spin_unlock_irqrestore(q->queue_lock, flags); 841cec47e3dSKiyoshi Ueda } 842cec47e3dSKiyoshi Ueda 84311a68244SKiyoshi Ueda static void dm_done(struct request *clone, int error, bool mapped) 84411a68244SKiyoshi Ueda { 84511a68244SKiyoshi Ueda int r = error; 84611a68244SKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 847ba1cbad9SMike Snitzer dm_request_endio_fn rq_end_io = NULL; 848ba1cbad9SMike Snitzer 849ba1cbad9SMike Snitzer if (tio->ti) { 850ba1cbad9SMike Snitzer rq_end_io = tio->ti->type->rq_end_io; 85111a68244SKiyoshi Ueda 85211a68244SKiyoshi Ueda if (mapped && rq_end_io) 85311a68244SKiyoshi Ueda r = rq_end_io(tio->ti, clone, error, &tio->info); 854ba1cbad9SMike Snitzer } 85511a68244SKiyoshi Ueda 85611a68244SKiyoshi Ueda if (r <= 0) 85711a68244SKiyoshi Ueda /* The target wants to complete the I/O */ 85811a68244SKiyoshi Ueda dm_end_request(clone, r); 85911a68244SKiyoshi Ueda else if (r == DM_ENDIO_INCOMPLETE) 86011a68244SKiyoshi Ueda /* The target will handle the I/O */ 86111a68244SKiyoshi Ueda return; 86211a68244SKiyoshi Ueda else if (r == DM_ENDIO_REQUEUE) 86311a68244SKiyoshi Ueda /* The target wants to requeue the I/O */ 86411a68244SKiyoshi Ueda dm_requeue_unmapped_request(clone); 86511a68244SKiyoshi Ueda else { 86611a68244SKiyoshi Ueda DMWARN("unimplemented target endio return value: %d", r); 86711a68244SKiyoshi Ueda BUG(); 86811a68244SKiyoshi Ueda } 86911a68244SKiyoshi Ueda } 87011a68244SKiyoshi Ueda 871cec47e3dSKiyoshi Ueda /* 872cec47e3dSKiyoshi Ueda * Request completion handler for request-based dm 873cec47e3dSKiyoshi Ueda */ 874cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq) 875cec47e3dSKiyoshi Ueda { 87611a68244SKiyoshi Ueda bool mapped = true; 877cec47e3dSKiyoshi Ueda struct request *clone = rq->completion_data; 878cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 879cec47e3dSKiyoshi Ueda 88011a68244SKiyoshi Ueda if (rq->cmd_flags & REQ_FAILED) 88111a68244SKiyoshi Ueda mapped = false; 882cec47e3dSKiyoshi Ueda 88311a68244SKiyoshi Ueda dm_done(clone, tio->error, mapped); 884cec47e3dSKiyoshi Ueda } 885cec47e3dSKiyoshi Ueda 886cec47e3dSKiyoshi Ueda /* 887cec47e3dSKiyoshi Ueda * Complete the clone and the original request with the error status 888cec47e3dSKiyoshi Ueda * through softirq context. 889cec47e3dSKiyoshi Ueda */ 890cec47e3dSKiyoshi Ueda static void dm_complete_request(struct request *clone, int error) 891cec47e3dSKiyoshi Ueda { 892cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 893cec47e3dSKiyoshi Ueda struct request *rq = tio->orig; 894cec47e3dSKiyoshi Ueda 895cec47e3dSKiyoshi Ueda tio->error = error; 896cec47e3dSKiyoshi Ueda rq->completion_data = clone; 897cec47e3dSKiyoshi Ueda blk_complete_request(rq); 898cec47e3dSKiyoshi Ueda } 899cec47e3dSKiyoshi Ueda 900cec47e3dSKiyoshi Ueda /* 901cec47e3dSKiyoshi Ueda * Complete the not-mapped clone and the original request with the error status 902cec47e3dSKiyoshi Ueda * through softirq context. 903cec47e3dSKiyoshi Ueda * Target's rq_end_io() function isn't called. 904cec47e3dSKiyoshi Ueda * This may be used when the target's map_rq() function fails. 905cec47e3dSKiyoshi Ueda */ 906cec47e3dSKiyoshi Ueda void dm_kill_unmapped_request(struct request *clone, int error) 907cec47e3dSKiyoshi Ueda { 908cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 909cec47e3dSKiyoshi Ueda struct request *rq = tio->orig; 910cec47e3dSKiyoshi Ueda 911cec47e3dSKiyoshi Ueda rq->cmd_flags |= REQ_FAILED; 912cec47e3dSKiyoshi Ueda dm_complete_request(clone, error); 913cec47e3dSKiyoshi Ueda } 914cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_kill_unmapped_request); 915cec47e3dSKiyoshi Ueda 916cec47e3dSKiyoshi Ueda /* 917cec47e3dSKiyoshi Ueda * Called with the queue lock held 918cec47e3dSKiyoshi Ueda */ 919cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error) 920cec47e3dSKiyoshi Ueda { 921cec47e3dSKiyoshi Ueda /* 922cec47e3dSKiyoshi Ueda * For just cleaning up the information of the queue in which 923cec47e3dSKiyoshi Ueda * the clone was dispatched. 924cec47e3dSKiyoshi Ueda * The clone is *NOT* freed actually here because it is alloced from 925cec47e3dSKiyoshi Ueda * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags. 926cec47e3dSKiyoshi Ueda */ 927cec47e3dSKiyoshi Ueda __blk_put_request(clone->q, clone); 928cec47e3dSKiyoshi Ueda 929cec47e3dSKiyoshi Ueda /* 930cec47e3dSKiyoshi Ueda * Actual request completion is done in a softirq context which doesn't 931cec47e3dSKiyoshi Ueda * hold the queue lock. Otherwise, deadlock could occur because: 932cec47e3dSKiyoshi Ueda * - another request may be submitted by the upper level driver 933cec47e3dSKiyoshi Ueda * of the stacking during the completion 934cec47e3dSKiyoshi Ueda * - the submission which requires queue lock may be done 935cec47e3dSKiyoshi Ueda * against this queue 936cec47e3dSKiyoshi Ueda */ 937cec47e3dSKiyoshi Ueda dm_complete_request(clone, error); 938cec47e3dSKiyoshi Ueda } 939cec47e3dSKiyoshi Ueda 94056a67df7SMike Snitzer /* 94156a67df7SMike Snitzer * Return maximum size of I/O possible at the supplied sector up to the current 94256a67df7SMike Snitzer * target boundary. 94356a67df7SMike Snitzer */ 94456a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti) 9451da177e4SLinus Torvalds { 94656a67df7SMike Snitzer sector_t target_offset = dm_target_offset(ti, sector); 94756a67df7SMike Snitzer 94856a67df7SMike Snitzer return ti->len - target_offset; 94956a67df7SMike Snitzer } 95056a67df7SMike Snitzer 95156a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti) 95256a67df7SMike Snitzer { 95356a67df7SMike Snitzer sector_t len = max_io_len_target_boundary(sector, ti); 954542f9038SMike Snitzer sector_t offset, max_len; 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds /* 9571da177e4SLinus Torvalds * Does the target need to split even further? 9581da177e4SLinus Torvalds */ 959542f9038SMike Snitzer if (ti->max_io_len) { 960542f9038SMike Snitzer offset = dm_target_offset(ti, sector); 961542f9038SMike Snitzer if (unlikely(ti->max_io_len & (ti->max_io_len - 1))) 962542f9038SMike Snitzer max_len = sector_div(offset, ti->max_io_len); 963542f9038SMike Snitzer else 964542f9038SMike Snitzer max_len = offset & (ti->max_io_len - 1); 965542f9038SMike Snitzer max_len = ti->max_io_len - max_len; 966542f9038SMike Snitzer 967542f9038SMike Snitzer if (len > max_len) 968542f9038SMike Snitzer len = max_len; 9691da177e4SLinus Torvalds } 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds return len; 9721da177e4SLinus Torvalds } 9731da177e4SLinus Torvalds 974542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len) 975542f9038SMike Snitzer { 976542f9038SMike Snitzer if (len > UINT_MAX) { 977542f9038SMike Snitzer DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)", 978542f9038SMike Snitzer (unsigned long long)len, UINT_MAX); 979542f9038SMike Snitzer ti->error = "Maximum size of target IO is too large"; 980542f9038SMike Snitzer return -EINVAL; 981542f9038SMike Snitzer } 982542f9038SMike Snitzer 983542f9038SMike Snitzer ti->max_io_len = (uint32_t) len; 984542f9038SMike Snitzer 985542f9038SMike Snitzer return 0; 986542f9038SMike Snitzer } 987542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len); 988542f9038SMike Snitzer 989dba14160SMikulas Patocka static void __map_bio(struct dm_target *ti, struct dm_target_io *tio) 9901da177e4SLinus Torvalds { 9911da177e4SLinus Torvalds int r; 9922056a782SJens Axboe sector_t sector; 9939faf400fSStefan Bader struct mapped_device *md; 994dba14160SMikulas Patocka struct bio *clone = &tio->clone; 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds clone->bi_end_io = clone_endio; 9971da177e4SLinus Torvalds clone->bi_private = tio; 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds /* 10001da177e4SLinus Torvalds * Map the clone. If r == 0 we don't need to do 10011da177e4SLinus Torvalds * anything, the target has assumed ownership of 10021da177e4SLinus Torvalds * this io. 10031da177e4SLinus Torvalds */ 10041da177e4SLinus Torvalds atomic_inc(&tio->io->io_count); 10052056a782SJens Axboe sector = clone->bi_sector; 10067de3ee57SMikulas Patocka r = ti->type->map(ti, clone); 100745cbcd79SKiyoshi Ueda if (r == DM_MAPIO_REMAPPED) { 10081da177e4SLinus Torvalds /* the bio has been remapped so dispatch it */ 10092056a782SJens Axboe 1010d07335e5SMike Snitzer trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone, 101122a7c31aSAlan D. Brunelle tio->io->bio->bi_bdev->bd_dev, sector); 10122056a782SJens Axboe 10131da177e4SLinus Torvalds generic_make_request(clone); 10142e93ccc1SKiyoshi Ueda } else if (r < 0 || r == DM_MAPIO_REQUEUE) { 10152e93ccc1SKiyoshi Ueda /* error the io and bail out, or requeue it if needed */ 10169faf400fSStefan Bader md = tio->io->md; 10179faf400fSStefan Bader dec_pending(tio->io, r); 10189faf400fSStefan Bader free_tio(md, tio); 101945cbcd79SKiyoshi Ueda } else if (r) { 102045cbcd79SKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 102145cbcd79SKiyoshi Ueda BUG(); 10221da177e4SLinus Torvalds } 10231da177e4SLinus Torvalds } 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds struct clone_info { 10261da177e4SLinus Torvalds struct mapped_device *md; 10271da177e4SLinus Torvalds struct dm_table *map; 10281da177e4SLinus Torvalds struct bio *bio; 10291da177e4SLinus Torvalds struct dm_io *io; 10301da177e4SLinus Torvalds sector_t sector; 10311da177e4SLinus Torvalds sector_t sector_count; 10321da177e4SLinus Torvalds unsigned short idx; 10331da177e4SLinus Torvalds }; 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds /* 1036d87f4c14STejun Heo * Creates a little bio that just does part of a bvec. 10371da177e4SLinus Torvalds */ 1038dba14160SMikulas Patocka static void split_bvec(struct dm_target_io *tio, struct bio *bio, 1039dba14160SMikulas Patocka sector_t sector, unsigned short idx, unsigned int offset, 10409faf400fSStefan Bader unsigned int len, struct bio_set *bs) 10411da177e4SLinus Torvalds { 1042dba14160SMikulas Patocka struct bio *clone = &tio->clone; 10431da177e4SLinus Torvalds struct bio_vec *bv = bio->bi_io_vec + idx; 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds *clone->bi_io_vec = *bv; 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds clone->bi_sector = sector; 10481da177e4SLinus Torvalds clone->bi_bdev = bio->bi_bdev; 1049d87f4c14STejun Heo clone->bi_rw = bio->bi_rw; 10501da177e4SLinus Torvalds clone->bi_vcnt = 1; 10511da177e4SLinus Torvalds clone->bi_size = to_bytes(len); 10521da177e4SLinus Torvalds clone->bi_io_vec->bv_offset = offset; 10531da177e4SLinus Torvalds clone->bi_io_vec->bv_len = clone->bi_size; 1054f3e1d26eSMartin K. Petersen clone->bi_flags |= 1 << BIO_CLONED; 10551da177e4SLinus Torvalds 10569c47008dSMartin K. Petersen if (bio_integrity(bio)) { 10571e2a410fSKent Overstreet bio_integrity_clone(clone, bio, GFP_NOIO); 10589c47008dSMartin K. Petersen bio_integrity_trim(clone, 10599c47008dSMartin K. Petersen bio_sector_offset(bio, idx, offset), len); 10609c47008dSMartin K. Petersen } 10611da177e4SLinus Torvalds } 10621da177e4SLinus Torvalds 10631da177e4SLinus Torvalds /* 10641da177e4SLinus Torvalds * Creates a bio that consists of range of complete bvecs. 10651da177e4SLinus Torvalds */ 1066dba14160SMikulas Patocka static void clone_bio(struct dm_target_io *tio, struct bio *bio, 1067dba14160SMikulas Patocka sector_t sector, unsigned short idx, 1068dba14160SMikulas Patocka unsigned short bv_count, unsigned int len, 1069dba14160SMikulas Patocka struct bio_set *bs) 10701da177e4SLinus Torvalds { 1071dba14160SMikulas Patocka struct bio *clone = &tio->clone; 10721da177e4SLinus Torvalds 10739faf400fSStefan Bader __bio_clone(clone, bio); 10741da177e4SLinus Torvalds clone->bi_sector = sector; 10751da177e4SLinus Torvalds clone->bi_idx = idx; 10761da177e4SLinus Torvalds clone->bi_vcnt = idx + bv_count; 10771da177e4SLinus Torvalds clone->bi_size = to_bytes(len); 10781da177e4SLinus Torvalds clone->bi_flags &= ~(1 << BIO_SEG_VALID); 10791da177e4SLinus Torvalds 10809c47008dSMartin K. Petersen if (bio_integrity(bio)) { 10811e2a410fSKent Overstreet bio_integrity_clone(clone, bio, GFP_NOIO); 10829c47008dSMartin K. Petersen 10839c47008dSMartin K. Petersen if (idx != bio->bi_idx || clone->bi_size < bio->bi_size) 10849c47008dSMartin K. Petersen bio_integrity_trim(clone, 10859c47008dSMartin K. Petersen bio_sector_offset(bio, idx, 0), len); 10869c47008dSMartin K. Petersen } 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds 10899015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci, 1090dba14160SMikulas Patocka struct dm_target *ti, int nr_iovecs) 1091f9ab94ceSMikulas Patocka { 1092dba14160SMikulas Patocka struct dm_target_io *tio; 1093dba14160SMikulas Patocka struct bio *clone; 1094dba14160SMikulas Patocka 1095dba14160SMikulas Patocka clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, ci->md->bs); 1096dba14160SMikulas Patocka tio = container_of(clone, struct dm_target_io, clone); 1097f9ab94ceSMikulas Patocka 1098f9ab94ceSMikulas Patocka tio->io = ci->io; 1099f9ab94ceSMikulas Patocka tio->ti = ti; 1100f9ab94ceSMikulas Patocka memset(&tio->info, 0, sizeof(tio->info)); 1101ddbd658fSMikulas Patocka tio->target_request_nr = 0; 11029015df24SAlasdair G Kergon 11039015df24SAlasdair G Kergon return tio; 11049015df24SAlasdair G Kergon } 11059015df24SAlasdair G Kergon 110606a426ceSMike Snitzer static void __issue_target_request(struct clone_info *ci, struct dm_target *ti, 1107a79245b3SMike Snitzer unsigned request_nr, sector_t len) 11089015df24SAlasdair G Kergon { 1109dba14160SMikulas Patocka struct dm_target_io *tio = alloc_tio(ci, ti, ci->bio->bi_max_vecs); 1110dba14160SMikulas Patocka struct bio *clone = &tio->clone; 11119015df24SAlasdair G Kergon 1112ddbd658fSMikulas Patocka tio->target_request_nr = request_nr; 1113f9ab94ceSMikulas Patocka 111406a426ceSMike Snitzer /* 111506a426ceSMike Snitzer * Discard requests require the bio's inline iovecs be initialized. 111606a426ceSMike Snitzer * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush 111706a426ceSMike Snitzer * and discard, so no need for concern about wasted bvec allocations. 111806a426ceSMike Snitzer */ 1119bf800ef1SKent Overstreet 1120dba14160SMikulas Patocka __bio_clone(clone, ci->bio); 1121a79245b3SMike Snitzer if (len) { 1122a79245b3SMike Snitzer clone->bi_sector = ci->sector; 1123a79245b3SMike Snitzer clone->bi_size = to_bytes(len); 1124a79245b3SMike Snitzer } 1125f9ab94ceSMikulas Patocka 1126dba14160SMikulas Patocka __map_bio(ti, tio); 1127f9ab94ceSMikulas Patocka } 1128f9ab94ceSMikulas Patocka 112906a426ceSMike Snitzer static void __issue_target_requests(struct clone_info *ci, struct dm_target *ti, 1130a79245b3SMike Snitzer unsigned num_requests, sector_t len) 113106a426ceSMike Snitzer { 113206a426ceSMike Snitzer unsigned request_nr; 113306a426ceSMike Snitzer 113406a426ceSMike Snitzer for (request_nr = 0; request_nr < num_requests; request_nr++) 1135a79245b3SMike Snitzer __issue_target_request(ci, ti, request_nr, len); 113606a426ceSMike Snitzer } 113706a426ceSMike Snitzer 1138b372d360SMike Snitzer static int __clone_and_map_empty_flush(struct clone_info *ci) 1139f9ab94ceSMikulas Patocka { 114006a426ceSMike Snitzer unsigned target_nr = 0; 1141f9ab94ceSMikulas Patocka struct dm_target *ti; 1142f9ab94ceSMikulas Patocka 1143b372d360SMike Snitzer BUG_ON(bio_has_data(ci->bio)); 1144f9ab94ceSMikulas Patocka while ((ti = dm_table_get_target(ci->map, target_nr++))) 1145a79245b3SMike Snitzer __issue_target_requests(ci, ti, ti->num_flush_requests, 0); 1146f9ab94ceSMikulas Patocka 1147f9ab94ceSMikulas Patocka return 0; 1148f9ab94ceSMikulas Patocka } 1149f9ab94ceSMikulas Patocka 11505ae89a87SMike Snitzer /* 11515ae89a87SMike Snitzer * Perform all io with a single clone. 11525ae89a87SMike Snitzer */ 11535ae89a87SMike Snitzer static void __clone_and_map_simple(struct clone_info *ci, struct dm_target *ti) 11545ae89a87SMike Snitzer { 1155dba14160SMikulas Patocka struct bio *bio = ci->bio; 11565ae89a87SMike Snitzer struct dm_target_io *tio; 11575ae89a87SMike Snitzer 1158dba14160SMikulas Patocka tio = alloc_tio(ci, ti, bio->bi_max_vecs); 1159dba14160SMikulas Patocka clone_bio(tio, bio, ci->sector, ci->idx, bio->bi_vcnt - ci->idx, 1160dba14160SMikulas Patocka ci->sector_count, ci->md->bs); 1161dba14160SMikulas Patocka __map_bio(ti, tio); 11625ae89a87SMike Snitzer ci->sector_count = 0; 11635ae89a87SMike Snitzer } 11645ae89a87SMike Snitzer 116523508a96SMike Snitzer typedef unsigned (*get_num_requests_fn)(struct dm_target *ti); 116623508a96SMike Snitzer 116723508a96SMike Snitzer static unsigned get_num_discard_requests(struct dm_target *ti) 116823508a96SMike Snitzer { 116923508a96SMike Snitzer return ti->num_discard_requests; 117023508a96SMike Snitzer } 117123508a96SMike Snitzer 117223508a96SMike Snitzer static unsigned get_num_write_same_requests(struct dm_target *ti) 117323508a96SMike Snitzer { 117423508a96SMike Snitzer return ti->num_write_same_requests; 117523508a96SMike Snitzer } 117623508a96SMike Snitzer 117723508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti); 117823508a96SMike Snitzer 117923508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti) 118023508a96SMike Snitzer { 118123508a96SMike Snitzer return ti->split_discard_requests; 118223508a96SMike Snitzer } 118323508a96SMike Snitzer 118423508a96SMike Snitzer static int __clone_and_map_changing_extent_only(struct clone_info *ci, 118523508a96SMike Snitzer get_num_requests_fn get_num_requests, 118623508a96SMike Snitzer is_split_required_fn is_split_required) 11875ae89a87SMike Snitzer { 11885ae89a87SMike Snitzer struct dm_target *ti; 1189a79245b3SMike Snitzer sector_t len; 1190fe7af2d3SAlasdair G Kergon unsigned num_requests; 11915ae89a87SMike Snitzer 1192a79245b3SMike Snitzer do { 11935ae89a87SMike Snitzer ti = dm_table_find_target(ci->map, ci->sector); 11945ae89a87SMike Snitzer if (!dm_target_is_valid(ti)) 11955ae89a87SMike Snitzer return -EIO; 11965ae89a87SMike Snitzer 11975ae89a87SMike Snitzer /* 119823508a96SMike Snitzer * Even though the device advertised support for this type of 119923508a96SMike Snitzer * request, that does not mean every target supports it, and 1200936688d7SMike Snitzer * reconfiguration might also have changed that since the 12015ae89a87SMike Snitzer * check was performed. 12025ae89a87SMike Snitzer */ 1203fe7af2d3SAlasdair G Kergon num_requests = get_num_requests ? get_num_requests(ti) : 0; 1204fe7af2d3SAlasdair G Kergon if (!num_requests) 12055ae89a87SMike Snitzer return -EOPNOTSUPP; 12065ae89a87SMike Snitzer 120723508a96SMike Snitzer if (is_split_required && !is_split_required(ti)) 1208a79245b3SMike Snitzer len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti)); 12097acf0277SMikulas Patocka else 12107acf0277SMikulas Patocka len = min(ci->sector_count, max_io_len(ci->sector, ti)); 12115ae89a87SMike Snitzer 1212fe7af2d3SAlasdair G Kergon __issue_target_requests(ci, ti, num_requests, len); 12135ae89a87SMike Snitzer 1214a79245b3SMike Snitzer ci->sector += len; 1215a79245b3SMike Snitzer } while (ci->sector_count -= len); 12165ae89a87SMike Snitzer 12175ae89a87SMike Snitzer return 0; 12185ae89a87SMike Snitzer } 12195ae89a87SMike Snitzer 122023508a96SMike Snitzer static int __clone_and_map_discard(struct clone_info *ci) 122123508a96SMike Snitzer { 122223508a96SMike Snitzer return __clone_and_map_changing_extent_only(ci, get_num_discard_requests, 122323508a96SMike Snitzer is_split_required_for_discard); 122423508a96SMike Snitzer } 122523508a96SMike Snitzer 122623508a96SMike Snitzer static int __clone_and_map_write_same(struct clone_info *ci) 122723508a96SMike Snitzer { 122823508a96SMike Snitzer return __clone_and_map_changing_extent_only(ci, get_num_write_same_requests, NULL); 122923508a96SMike Snitzer } 123023508a96SMike Snitzer 1231512875bdSJun'ichi Nomura static int __clone_and_map(struct clone_info *ci) 12321da177e4SLinus Torvalds { 1233dba14160SMikulas Patocka struct bio *bio = ci->bio; 1234512875bdSJun'ichi Nomura struct dm_target *ti; 1235512875bdSJun'ichi Nomura sector_t len = 0, max; 1236028867acSAlasdair G Kergon struct dm_target_io *tio; 12371da177e4SLinus Torvalds 12385ae89a87SMike Snitzer if (unlikely(bio->bi_rw & REQ_DISCARD)) 12395ae89a87SMike Snitzer return __clone_and_map_discard(ci); 124023508a96SMike Snitzer else if (unlikely(bio->bi_rw & REQ_WRITE_SAME)) 124123508a96SMike Snitzer return __clone_and_map_write_same(ci); 12425ae89a87SMike Snitzer 1243512875bdSJun'ichi Nomura ti = dm_table_find_target(ci->map, ci->sector); 1244512875bdSJun'ichi Nomura if (!dm_target_is_valid(ti)) 1245512875bdSJun'ichi Nomura return -EIO; 1246512875bdSJun'ichi Nomura 124756a67df7SMike Snitzer max = max_io_len(ci->sector, ti); 1248512875bdSJun'ichi Nomura 12491da177e4SLinus Torvalds if (ci->sector_count <= max) { 12501da177e4SLinus Torvalds /* 12511da177e4SLinus Torvalds * Optimise for the simple case where we can do all of 12521da177e4SLinus Torvalds * the remaining io with a single clone. 12531da177e4SLinus Torvalds */ 12545ae89a87SMike Snitzer __clone_and_map_simple(ci, ti); 12551da177e4SLinus Torvalds 12561da177e4SLinus Torvalds } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) { 12571da177e4SLinus Torvalds /* 12581da177e4SLinus Torvalds * There are some bvecs that don't span targets. 12591da177e4SLinus Torvalds * Do as many of these as possible. 12601da177e4SLinus Torvalds */ 12611da177e4SLinus Torvalds int i; 12621da177e4SLinus Torvalds sector_t remaining = max; 12631da177e4SLinus Torvalds sector_t bv_len; 12641da177e4SLinus Torvalds 12651da177e4SLinus Torvalds for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) { 12661da177e4SLinus Torvalds bv_len = to_sector(bio->bi_io_vec[i].bv_len); 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds if (bv_len > remaining) 12691da177e4SLinus Torvalds break; 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds remaining -= bv_len; 12721da177e4SLinus Torvalds len += bv_len; 12731da177e4SLinus Torvalds } 12741da177e4SLinus Torvalds 1275dba14160SMikulas Patocka tio = alloc_tio(ci, ti, bio->bi_max_vecs); 1276dba14160SMikulas Patocka clone_bio(tio, bio, ci->sector, ci->idx, i - ci->idx, len, 12779faf400fSStefan Bader ci->md->bs); 1278dba14160SMikulas Patocka __map_bio(ti, tio); 12791da177e4SLinus Torvalds 12801da177e4SLinus Torvalds ci->sector += len; 12811da177e4SLinus Torvalds ci->sector_count -= len; 12821da177e4SLinus Torvalds ci->idx = i; 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds } else { 12851da177e4SLinus Torvalds /* 1286d2044a94SAlasdair G Kergon * Handle a bvec that must be split between two or more targets. 12871da177e4SLinus Torvalds */ 12881da177e4SLinus Torvalds struct bio_vec *bv = bio->bi_io_vec + ci->idx; 1289d2044a94SAlasdair G Kergon sector_t remaining = to_sector(bv->bv_len); 1290d2044a94SAlasdair G Kergon unsigned int offset = 0; 12911da177e4SLinus Torvalds 1292d2044a94SAlasdair G Kergon do { 1293d2044a94SAlasdair G Kergon if (offset) { 12941da177e4SLinus Torvalds ti = dm_table_find_target(ci->map, ci->sector); 1295512875bdSJun'ichi Nomura if (!dm_target_is_valid(ti)) 1296512875bdSJun'ichi Nomura return -EIO; 1297512875bdSJun'ichi Nomura 129856a67df7SMike Snitzer max = max_io_len(ci->sector, ti); 1299d2044a94SAlasdair G Kergon } 1300d2044a94SAlasdair G Kergon 1301d2044a94SAlasdair G Kergon len = min(remaining, max); 1302d2044a94SAlasdair G Kergon 1303dba14160SMikulas Patocka tio = alloc_tio(ci, ti, 1); 1304dba14160SMikulas Patocka split_bvec(tio, bio, ci->sector, ci->idx, 1305dba14160SMikulas Patocka bv->bv_offset + offset, len, ci->md->bs); 1306d2044a94SAlasdair G Kergon 1307dba14160SMikulas Patocka __map_bio(ti, tio); 13081da177e4SLinus Torvalds 13091da177e4SLinus Torvalds ci->sector += len; 13101da177e4SLinus Torvalds ci->sector_count -= len; 1311d2044a94SAlasdair G Kergon offset += to_bytes(len); 1312d2044a94SAlasdair G Kergon } while (remaining -= len); 1313d2044a94SAlasdair G Kergon 13141da177e4SLinus Torvalds ci->idx++; 13151da177e4SLinus Torvalds } 1316512875bdSJun'ichi Nomura 1317512875bdSJun'ichi Nomura return 0; 13181da177e4SLinus Torvalds } 13191da177e4SLinus Torvalds 13201da177e4SLinus Torvalds /* 13218a53c28dSMikulas Patocka * Split the bio into several clones and submit it to targets. 13221da177e4SLinus Torvalds */ 1323f0b9a450SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md, struct bio *bio) 13241da177e4SLinus Torvalds { 13251da177e4SLinus Torvalds struct clone_info ci; 1326512875bdSJun'ichi Nomura int error = 0; 13271da177e4SLinus Torvalds 13287c666411SAlasdair G Kergon ci.map = dm_get_live_table(md); 1329f0b9a450SMikulas Patocka if (unlikely(!ci.map)) { 1330f0b9a450SMikulas Patocka bio_io_error(bio); 1331f0b9a450SMikulas Patocka return; 1332f0b9a450SMikulas Patocka } 1333692d0eb9SMikulas Patocka 13341da177e4SLinus Torvalds ci.md = md; 13351da177e4SLinus Torvalds ci.io = alloc_io(md); 13361da177e4SLinus Torvalds ci.io->error = 0; 13371da177e4SLinus Torvalds atomic_set(&ci.io->io_count, 1); 13381da177e4SLinus Torvalds ci.io->bio = bio; 13391da177e4SLinus Torvalds ci.io->md = md; 1340f88fb981SKiyoshi Ueda spin_lock_init(&ci.io->endio_lock); 13411da177e4SLinus Torvalds ci.sector = bio->bi_sector; 13421da177e4SLinus Torvalds ci.idx = bio->bi_idx; 13431da177e4SLinus Torvalds 13443eaf840eSJun'ichi "Nick" Nomura start_io_acct(ci.io); 1345b372d360SMike Snitzer if (bio->bi_rw & REQ_FLUSH) { 1346b372d360SMike Snitzer ci.bio = &ci.md->flush_bio; 1347b372d360SMike Snitzer ci.sector_count = 0; 1348b372d360SMike Snitzer error = __clone_and_map_empty_flush(&ci); 1349b372d360SMike Snitzer /* dec_pending submits any data associated with flush */ 1350b372d360SMike Snitzer } else { 13516a8736d1STejun Heo ci.bio = bio; 1352f6fccb12SMilan Broz ci.sector_count = bio_sectors(bio); 1353512875bdSJun'ichi Nomura while (ci.sector_count && !error) 1354512875bdSJun'ichi Nomura error = __clone_and_map(&ci); 1355d87f4c14STejun Heo } 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds /* drop the extra reference count */ 1358512875bdSJun'ichi Nomura dec_pending(ci.io, error); 13591da177e4SLinus Torvalds dm_table_put(ci.map); 13609e4e5f87SMilan Broz } 13619e4e5f87SMilan Broz /*----------------------------------------------------------------- 13621da177e4SLinus Torvalds * CRUD END 13631da177e4SLinus Torvalds *---------------------------------------------------------------*/ 13641da177e4SLinus Torvalds 13651da177e4SLinus Torvalds static int dm_merge_bvec(struct request_queue *q, 13661da177e4SLinus Torvalds struct bvec_merge_data *bvm, 1367f6fccb12SMilan Broz struct bio_vec *biovec) 1368f6fccb12SMilan Broz { 1369f6fccb12SMilan Broz struct mapped_device *md = q->queuedata; 13707c666411SAlasdair G Kergon struct dm_table *map = dm_get_live_table(md); 1371f6fccb12SMilan Broz struct dm_target *ti; 1372f6fccb12SMilan Broz sector_t max_sectors; 1373f6fccb12SMilan Broz int max_size = 0; 1374f6fccb12SMilan Broz 1375f6fccb12SMilan Broz if (unlikely(!map)) 1376f6fccb12SMilan Broz goto out; 1377f6fccb12SMilan Broz 1378f6fccb12SMilan Broz ti = dm_table_find_target(map, bvm->bi_sector); 1379f6fccb12SMilan Broz if (!dm_target_is_valid(ti)) 1380f6fccb12SMilan Broz goto out_table; 1381f6fccb12SMilan Broz 1382f6fccb12SMilan Broz /* 1383f6fccb12SMilan Broz * Find maximum amount of I/O that won't need splitting 1384f6fccb12SMilan Broz */ 138556a67df7SMike Snitzer max_sectors = min(max_io_len(bvm->bi_sector, ti), 1386f6fccb12SMilan Broz (sector_t) BIO_MAX_SECTORS); 1387f6fccb12SMilan Broz max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size; 1388f6fccb12SMilan Broz if (max_size < 0) 1389f6fccb12SMilan Broz max_size = 0; 1390f6fccb12SMilan Broz 1391f6fccb12SMilan Broz /* 1392f6fccb12SMilan Broz * merge_bvec_fn() returns number of bytes 1393f6fccb12SMilan Broz * it can accept at this offset 1394f6fccb12SMilan Broz * max is precomputed maximal io size 1395f6fccb12SMilan Broz */ 1396f6fccb12SMilan Broz if (max_size && ti->type->merge) 1397f6fccb12SMilan Broz max_size = ti->type->merge(ti, bvm, biovec, max_size); 13988cbeb67aSMikulas Patocka /* 13998cbeb67aSMikulas Patocka * If the target doesn't support merge method and some of the devices 14008cbeb67aSMikulas Patocka * provided their merge_bvec method (we know this by looking at 14018cbeb67aSMikulas Patocka * queue_max_hw_sectors), then we can't allow bios with multiple vector 14028cbeb67aSMikulas Patocka * entries. So always set max_size to 0, and the code below allows 14038cbeb67aSMikulas Patocka * just one page. 14048cbeb67aSMikulas Patocka */ 14058cbeb67aSMikulas Patocka else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9) 14068cbeb67aSMikulas Patocka 14078cbeb67aSMikulas Patocka max_size = 0; 1408f6fccb12SMilan Broz 1409b01cd5acSMikulas Patocka out_table: 14105037108aSMikulas Patocka dm_table_put(map); 14115037108aSMikulas Patocka 14125037108aSMikulas Patocka out: 1413f6fccb12SMilan Broz /* 1414f6fccb12SMilan Broz * Always allow an entire first page 1415f6fccb12SMilan Broz */ 1416f6fccb12SMilan Broz if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT)) 1417f6fccb12SMilan Broz max_size = biovec->bv_len; 1418f6fccb12SMilan Broz 1419f6fccb12SMilan Broz return max_size; 1420f6fccb12SMilan Broz } 1421f6fccb12SMilan Broz 14221da177e4SLinus Torvalds /* 14231da177e4SLinus Torvalds * The request function that just remaps the bio built up by 14241da177e4SLinus Torvalds * dm_merge_bvec. 14251da177e4SLinus Torvalds */ 14265a7bbad2SChristoph Hellwig static void _dm_request(struct request_queue *q, struct bio *bio) 14271da177e4SLinus Torvalds { 142812f03a49SKevin Corry int rw = bio_data_dir(bio); 14291da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 1430c9959059STejun Heo int cpu; 14311da177e4SLinus Torvalds 14322ca3310eSAlasdair G Kergon down_read(&md->io_lock); 14331da177e4SLinus Torvalds 1434074a7acaSTejun Heo cpu = part_stat_lock(); 1435074a7acaSTejun Heo part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]); 1436074a7acaSTejun Heo part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio)); 1437074a7acaSTejun Heo part_stat_unlock(); 143812f03a49SKevin Corry 14396a8736d1STejun Heo /* if we're suspended, we have to queue this io for later */ 14406a8736d1STejun Heo if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) { 14412ca3310eSAlasdair G Kergon up_read(&md->io_lock); 14421da177e4SLinus Torvalds 14436a8736d1STejun Heo if (bio_rw(bio) != READA) 144492c63902SMikulas Patocka queue_io(md, bio); 14456a8736d1STejun Heo else 14466a8736d1STejun Heo bio_io_error(bio); 14475a7bbad2SChristoph Hellwig return; 14481da177e4SLinus Torvalds } 14491da177e4SLinus Torvalds 1450f0b9a450SMikulas Patocka __split_and_process_bio(md, bio); 14512ca3310eSAlasdair G Kergon up_read(&md->io_lock); 14525a7bbad2SChristoph Hellwig return; 1453cec47e3dSKiyoshi Ueda } 1454cec47e3dSKiyoshi Ueda 1455cec47e3dSKiyoshi Ueda static int dm_request_based(struct mapped_device *md) 1456cec47e3dSKiyoshi Ueda { 1457cec47e3dSKiyoshi Ueda return blk_queue_stackable(md->queue); 1458cec47e3dSKiyoshi Ueda } 1459cec47e3dSKiyoshi Ueda 14605a7bbad2SChristoph Hellwig static void dm_request(struct request_queue *q, struct bio *bio) 1461cec47e3dSKiyoshi Ueda { 1462cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 1463cec47e3dSKiyoshi Ueda 1464cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 14655a7bbad2SChristoph Hellwig blk_queue_bio(q, bio); 14665a7bbad2SChristoph Hellwig else 14675a7bbad2SChristoph Hellwig _dm_request(q, bio); 1468cec47e3dSKiyoshi Ueda } 1469cec47e3dSKiyoshi Ueda 1470cec47e3dSKiyoshi Ueda void dm_dispatch_request(struct request *rq) 1471cec47e3dSKiyoshi Ueda { 1472cec47e3dSKiyoshi Ueda int r; 1473cec47e3dSKiyoshi Ueda 1474cec47e3dSKiyoshi Ueda if (blk_queue_io_stat(rq->q)) 1475cec47e3dSKiyoshi Ueda rq->cmd_flags |= REQ_IO_STAT; 1476cec47e3dSKiyoshi Ueda 1477cec47e3dSKiyoshi Ueda rq->start_time = jiffies; 1478cec47e3dSKiyoshi Ueda r = blk_insert_cloned_request(rq->q, rq); 1479cec47e3dSKiyoshi Ueda if (r) 1480cec47e3dSKiyoshi Ueda dm_complete_request(rq, r); 1481cec47e3dSKiyoshi Ueda } 1482cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_dispatch_request); 1483cec47e3dSKiyoshi Ueda 1484cec47e3dSKiyoshi Ueda static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig, 1485cec47e3dSKiyoshi Ueda void *data) 1486cec47e3dSKiyoshi Ueda { 1487cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = data; 148894818742SKent Overstreet struct dm_rq_clone_bio_info *info = 148994818742SKent Overstreet container_of(bio, struct dm_rq_clone_bio_info, clone); 1490cec47e3dSKiyoshi Ueda 1491cec47e3dSKiyoshi Ueda info->orig = bio_orig; 1492cec47e3dSKiyoshi Ueda info->tio = tio; 1493cec47e3dSKiyoshi Ueda bio->bi_end_io = end_clone_bio; 1494cec47e3dSKiyoshi Ueda bio->bi_private = info; 1495cec47e3dSKiyoshi Ueda 1496cec47e3dSKiyoshi Ueda return 0; 1497cec47e3dSKiyoshi Ueda } 1498cec47e3dSKiyoshi Ueda 1499cec47e3dSKiyoshi Ueda static int setup_clone(struct request *clone, struct request *rq, 1500cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio) 1501cec47e3dSKiyoshi Ueda { 1502d0bcb878SKiyoshi Ueda int r; 1503cec47e3dSKiyoshi Ueda 1504d0bcb878SKiyoshi Ueda r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC, 1505d0bcb878SKiyoshi Ueda dm_rq_bio_constructor, tio); 1506cec47e3dSKiyoshi Ueda if (r) 1507cec47e3dSKiyoshi Ueda return r; 1508cec47e3dSKiyoshi Ueda 1509cec47e3dSKiyoshi Ueda clone->cmd = rq->cmd; 1510cec47e3dSKiyoshi Ueda clone->cmd_len = rq->cmd_len; 1511cec47e3dSKiyoshi Ueda clone->sense = rq->sense; 1512cec47e3dSKiyoshi Ueda clone->buffer = rq->buffer; 1513cec47e3dSKiyoshi Ueda clone->end_io = end_clone_request; 1514cec47e3dSKiyoshi Ueda clone->end_io_data = tio; 1515cec47e3dSKiyoshi Ueda 1516cec47e3dSKiyoshi Ueda return 0; 1517cec47e3dSKiyoshi Ueda } 1518cec47e3dSKiyoshi Ueda 15196facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md, 15206facdaffSKiyoshi Ueda gfp_t gfp_mask) 15216facdaffSKiyoshi Ueda { 15226facdaffSKiyoshi Ueda struct request *clone; 15236facdaffSKiyoshi Ueda struct dm_rq_target_io *tio; 15246facdaffSKiyoshi Ueda 15256facdaffSKiyoshi Ueda tio = alloc_rq_tio(md, gfp_mask); 15266facdaffSKiyoshi Ueda if (!tio) 15276facdaffSKiyoshi Ueda return NULL; 15286facdaffSKiyoshi Ueda 15296facdaffSKiyoshi Ueda tio->md = md; 15306facdaffSKiyoshi Ueda tio->ti = NULL; 15316facdaffSKiyoshi Ueda tio->orig = rq; 15326facdaffSKiyoshi Ueda tio->error = 0; 15336facdaffSKiyoshi Ueda memset(&tio->info, 0, sizeof(tio->info)); 15346facdaffSKiyoshi Ueda 15356facdaffSKiyoshi Ueda clone = &tio->clone; 15366facdaffSKiyoshi Ueda if (setup_clone(clone, rq, tio)) { 15376facdaffSKiyoshi Ueda /* -ENOMEM */ 15386facdaffSKiyoshi Ueda free_rq_tio(tio); 15396facdaffSKiyoshi Ueda return NULL; 15406facdaffSKiyoshi Ueda } 15416facdaffSKiyoshi Ueda 15426facdaffSKiyoshi Ueda return clone; 15436facdaffSKiyoshi Ueda } 15446facdaffSKiyoshi Ueda 1545cec47e3dSKiyoshi Ueda /* 1546cec47e3dSKiyoshi Ueda * Called with the queue lock held. 1547cec47e3dSKiyoshi Ueda */ 1548cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq) 1549cec47e3dSKiyoshi Ueda { 1550cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 1551cec47e3dSKiyoshi Ueda struct request *clone; 1552cec47e3dSKiyoshi Ueda 1553cec47e3dSKiyoshi Ueda if (unlikely(rq->special)) { 1554cec47e3dSKiyoshi Ueda DMWARN("Already has something in rq->special."); 1555cec47e3dSKiyoshi Ueda return BLKPREP_KILL; 1556cec47e3dSKiyoshi Ueda } 1557cec47e3dSKiyoshi Ueda 15586facdaffSKiyoshi Ueda clone = clone_rq(rq, md, GFP_ATOMIC); 15596facdaffSKiyoshi Ueda if (!clone) 1560cec47e3dSKiyoshi Ueda return BLKPREP_DEFER; 1561cec47e3dSKiyoshi Ueda 1562cec47e3dSKiyoshi Ueda rq->special = clone; 1563cec47e3dSKiyoshi Ueda rq->cmd_flags |= REQ_DONTPREP; 1564cec47e3dSKiyoshi Ueda 1565cec47e3dSKiyoshi Ueda return BLKPREP_OK; 1566cec47e3dSKiyoshi Ueda } 1567cec47e3dSKiyoshi Ueda 15689eef87daSKiyoshi Ueda /* 15699eef87daSKiyoshi Ueda * Returns: 15709eef87daSKiyoshi Ueda * 0 : the request has been processed (not requeued) 15719eef87daSKiyoshi Ueda * !0 : the request has been requeued 15729eef87daSKiyoshi Ueda */ 15739eef87daSKiyoshi Ueda static int map_request(struct dm_target *ti, struct request *clone, 1574cec47e3dSKiyoshi Ueda struct mapped_device *md) 1575cec47e3dSKiyoshi Ueda { 15769eef87daSKiyoshi Ueda int r, requeued = 0; 1577cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 1578cec47e3dSKiyoshi Ueda 1579cec47e3dSKiyoshi Ueda tio->ti = ti; 1580cec47e3dSKiyoshi Ueda r = ti->type->map_rq(ti, clone, &tio->info); 1581cec47e3dSKiyoshi Ueda switch (r) { 1582cec47e3dSKiyoshi Ueda case DM_MAPIO_SUBMITTED: 1583cec47e3dSKiyoshi Ueda /* The target has taken the I/O to submit by itself later */ 1584cec47e3dSKiyoshi Ueda break; 1585cec47e3dSKiyoshi Ueda case DM_MAPIO_REMAPPED: 1586cec47e3dSKiyoshi Ueda /* The target has remapped the I/O so dispatch it */ 15876db4ccd6SJun'ichi Nomura trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)), 15886db4ccd6SJun'ichi Nomura blk_rq_pos(tio->orig)); 1589cec47e3dSKiyoshi Ueda dm_dispatch_request(clone); 1590cec47e3dSKiyoshi Ueda break; 1591cec47e3dSKiyoshi Ueda case DM_MAPIO_REQUEUE: 1592cec47e3dSKiyoshi Ueda /* The target wants to requeue the I/O */ 1593cec47e3dSKiyoshi Ueda dm_requeue_unmapped_request(clone); 15949eef87daSKiyoshi Ueda requeued = 1; 1595cec47e3dSKiyoshi Ueda break; 1596cec47e3dSKiyoshi Ueda default: 1597cec47e3dSKiyoshi Ueda if (r > 0) { 1598cec47e3dSKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 1599cec47e3dSKiyoshi Ueda BUG(); 1600cec47e3dSKiyoshi Ueda } 1601cec47e3dSKiyoshi Ueda 1602cec47e3dSKiyoshi Ueda /* The target wants to complete the I/O */ 1603cec47e3dSKiyoshi Ueda dm_kill_unmapped_request(clone, r); 1604cec47e3dSKiyoshi Ueda break; 1605cec47e3dSKiyoshi Ueda } 16069eef87daSKiyoshi Ueda 16079eef87daSKiyoshi Ueda return requeued; 1608cec47e3dSKiyoshi Ueda } 1609cec47e3dSKiyoshi Ueda 1610ba1cbad9SMike Snitzer static struct request *dm_start_request(struct mapped_device *md, struct request *orig) 1611ba1cbad9SMike Snitzer { 1612ba1cbad9SMike Snitzer struct request *clone; 1613ba1cbad9SMike Snitzer 1614ba1cbad9SMike Snitzer blk_start_request(orig); 1615ba1cbad9SMike Snitzer clone = orig->special; 1616ba1cbad9SMike Snitzer atomic_inc(&md->pending[rq_data_dir(clone)]); 1617ba1cbad9SMike Snitzer 1618ba1cbad9SMike Snitzer /* 1619ba1cbad9SMike Snitzer * Hold the md reference here for the in-flight I/O. 1620ba1cbad9SMike Snitzer * We can't rely on the reference count by device opener, 1621ba1cbad9SMike Snitzer * because the device may be closed during the request completion 1622ba1cbad9SMike Snitzer * when all bios are completed. 1623ba1cbad9SMike Snitzer * See the comment in rq_completed() too. 1624ba1cbad9SMike Snitzer */ 1625ba1cbad9SMike Snitzer dm_get(md); 1626ba1cbad9SMike Snitzer 1627ba1cbad9SMike Snitzer return clone; 1628ba1cbad9SMike Snitzer } 1629ba1cbad9SMike Snitzer 1630cec47e3dSKiyoshi Ueda /* 1631cec47e3dSKiyoshi Ueda * q->request_fn for request-based dm. 1632cec47e3dSKiyoshi Ueda * Called with the queue lock held. 1633cec47e3dSKiyoshi Ueda */ 1634cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q) 1635cec47e3dSKiyoshi Ueda { 1636cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 16377c666411SAlasdair G Kergon struct dm_table *map = dm_get_live_table(md); 1638cec47e3dSKiyoshi Ueda struct dm_target *ti; 1639b4324feeSKiyoshi Ueda struct request *rq, *clone; 164029e4013dSTejun Heo sector_t pos; 1641cec47e3dSKiyoshi Ueda 1642cec47e3dSKiyoshi Ueda /* 1643b4324feeSKiyoshi Ueda * For suspend, check blk_queue_stopped() and increment 1644b4324feeSKiyoshi Ueda * ->pending within a single queue_lock not to increment the 1645b4324feeSKiyoshi Ueda * number of in-flight I/Os after the queue is stopped in 1646b4324feeSKiyoshi Ueda * dm_suspend(). 1647cec47e3dSKiyoshi Ueda */ 16487eaceaccSJens Axboe while (!blk_queue_stopped(q)) { 1649cec47e3dSKiyoshi Ueda rq = blk_peek_request(q); 1650cec47e3dSKiyoshi Ueda if (!rq) 16517eaceaccSJens Axboe goto delay_and_out; 1652cec47e3dSKiyoshi Ueda 165329e4013dSTejun Heo /* always use block 0 to find the target for flushes for now */ 165429e4013dSTejun Heo pos = 0; 165529e4013dSTejun Heo if (!(rq->cmd_flags & REQ_FLUSH)) 165629e4013dSTejun Heo pos = blk_rq_pos(rq); 1657d0bcb878SKiyoshi Ueda 165829e4013dSTejun Heo ti = dm_table_find_target(map, pos); 1659ba1cbad9SMike Snitzer if (!dm_target_is_valid(ti)) { 1660ba1cbad9SMike Snitzer /* 1661ba1cbad9SMike Snitzer * Must perform setup, that dm_done() requires, 1662ba1cbad9SMike Snitzer * before calling dm_kill_unmapped_request 1663ba1cbad9SMike Snitzer */ 1664ba1cbad9SMike Snitzer DMERR_LIMIT("request attempted access beyond the end of device"); 1665ba1cbad9SMike Snitzer clone = dm_start_request(md, rq); 1666ba1cbad9SMike Snitzer dm_kill_unmapped_request(clone, -EIO); 1667ba1cbad9SMike Snitzer continue; 1668ba1cbad9SMike Snitzer } 166929e4013dSTejun Heo 1670cec47e3dSKiyoshi Ueda if (ti->type->busy && ti->type->busy(ti)) 16717eaceaccSJens Axboe goto delay_and_out; 1672cec47e3dSKiyoshi Ueda 1673ba1cbad9SMike Snitzer clone = dm_start_request(md, rq); 1674b4324feeSKiyoshi Ueda 1675cec47e3dSKiyoshi Ueda spin_unlock(q->queue_lock); 16769eef87daSKiyoshi Ueda if (map_request(ti, clone, md)) 16779eef87daSKiyoshi Ueda goto requeued; 16789eef87daSKiyoshi Ueda 1679052189a2SKiyoshi Ueda BUG_ON(!irqs_disabled()); 1680052189a2SKiyoshi Ueda spin_lock(q->queue_lock); 1681cec47e3dSKiyoshi Ueda } 1682cec47e3dSKiyoshi Ueda 1683cec47e3dSKiyoshi Ueda goto out; 1684cec47e3dSKiyoshi Ueda 16859eef87daSKiyoshi Ueda requeued: 1686052189a2SKiyoshi Ueda BUG_ON(!irqs_disabled()); 1687052189a2SKiyoshi Ueda spin_lock(q->queue_lock); 16889eef87daSKiyoshi Ueda 16897eaceaccSJens Axboe delay_and_out: 16907eaceaccSJens Axboe blk_delay_queue(q, HZ / 10); 1691cec47e3dSKiyoshi Ueda out: 1692cec47e3dSKiyoshi Ueda dm_table_put(map); 1693cec47e3dSKiyoshi Ueda } 1694cec47e3dSKiyoshi Ueda 1695cec47e3dSKiyoshi Ueda int dm_underlying_device_busy(struct request_queue *q) 1696cec47e3dSKiyoshi Ueda { 1697cec47e3dSKiyoshi Ueda return blk_lld_busy(q); 1698cec47e3dSKiyoshi Ueda } 1699cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_underlying_device_busy); 1700cec47e3dSKiyoshi Ueda 1701cec47e3dSKiyoshi Ueda static int dm_lld_busy(struct request_queue *q) 1702cec47e3dSKiyoshi Ueda { 1703cec47e3dSKiyoshi Ueda int r; 1704cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 17057c666411SAlasdair G Kergon struct dm_table *map = dm_get_live_table(md); 1706cec47e3dSKiyoshi Ueda 1707cec47e3dSKiyoshi Ueda if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) 1708cec47e3dSKiyoshi Ueda r = 1; 1709cec47e3dSKiyoshi Ueda else 1710cec47e3dSKiyoshi Ueda r = dm_table_any_busy_target(map); 1711cec47e3dSKiyoshi Ueda 1712cec47e3dSKiyoshi Ueda dm_table_put(map); 1713cec47e3dSKiyoshi Ueda 1714cec47e3dSKiyoshi Ueda return r; 1715cec47e3dSKiyoshi Ueda } 1716cec47e3dSKiyoshi Ueda 17171da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits) 17181da177e4SLinus Torvalds { 17198a57dfc6SChandra Seetharaman int r = bdi_bits; 17208a57dfc6SChandra Seetharaman struct mapped_device *md = congested_data; 17218a57dfc6SChandra Seetharaman struct dm_table *map; 17221da177e4SLinus Torvalds 17231eb787ecSAlasdair G Kergon if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 17247c666411SAlasdair G Kergon map = dm_get_live_table(md); 17258a57dfc6SChandra Seetharaman if (map) { 1726cec47e3dSKiyoshi Ueda /* 1727cec47e3dSKiyoshi Ueda * Request-based dm cares about only own queue for 1728cec47e3dSKiyoshi Ueda * the query about congestion status of request_queue 1729cec47e3dSKiyoshi Ueda */ 1730cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 1731cec47e3dSKiyoshi Ueda r = md->queue->backing_dev_info.state & 1732cec47e3dSKiyoshi Ueda bdi_bits; 1733cec47e3dSKiyoshi Ueda else 17341da177e4SLinus Torvalds r = dm_table_any_congested(map, bdi_bits); 1735cec47e3dSKiyoshi Ueda 17361da177e4SLinus Torvalds dm_table_put(map); 17378a57dfc6SChandra Seetharaman } 17388a57dfc6SChandra Seetharaman } 17398a57dfc6SChandra Seetharaman 17401da177e4SLinus Torvalds return r; 17411da177e4SLinus Torvalds } 17421da177e4SLinus Torvalds 17431da177e4SLinus Torvalds /*----------------------------------------------------------------- 17441da177e4SLinus Torvalds * An IDR is used to keep track of allocated minor numbers. 17451da177e4SLinus Torvalds *---------------------------------------------------------------*/ 17462b06cfffSAlasdair G Kergon static void free_minor(int minor) 17471da177e4SLinus Torvalds { 1748f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 17491da177e4SLinus Torvalds idr_remove(&_minor_idr, minor); 1750f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 17511da177e4SLinus Torvalds } 17521da177e4SLinus Torvalds 17531da177e4SLinus Torvalds /* 17541da177e4SLinus Torvalds * See if the device with a specific minor # is free. 17551da177e4SLinus Torvalds */ 1756cf13ab8eSFrederik Deweerdt static int specific_minor(int minor) 17571da177e4SLinus Torvalds { 1758*c9d76be6STejun Heo int r; 17591da177e4SLinus Torvalds 17601da177e4SLinus Torvalds if (minor >= (1 << MINORBITS)) 17611da177e4SLinus Torvalds return -EINVAL; 17621da177e4SLinus Torvalds 1763*c9d76be6STejun Heo idr_preload(GFP_KERNEL); 1764f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 17651da177e4SLinus Torvalds 1766*c9d76be6STejun Heo r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT); 17671da177e4SLinus Torvalds 1768f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1769*c9d76be6STejun Heo idr_preload_end(); 1770*c9d76be6STejun Heo if (r < 0) 1771*c9d76be6STejun Heo return r == -ENOSPC ? -EBUSY : r; 1772*c9d76be6STejun Heo return 0; 17731da177e4SLinus Torvalds } 17741da177e4SLinus Torvalds 1775cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor) 17761da177e4SLinus Torvalds { 1777*c9d76be6STejun Heo int r; 17781da177e4SLinus Torvalds 1779*c9d76be6STejun Heo idr_preload(GFP_KERNEL); 1780f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 17811da177e4SLinus Torvalds 1782*c9d76be6STejun Heo r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT); 17831da177e4SLinus Torvalds 1784f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1785*c9d76be6STejun Heo idr_preload_end(); 1786*c9d76be6STejun Heo if (r < 0) 17871da177e4SLinus Torvalds return r; 1788*c9d76be6STejun Heo *minor = r; 1789*c9d76be6STejun Heo return 0; 17901da177e4SLinus Torvalds } 17911da177e4SLinus Torvalds 179283d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops; 17931da177e4SLinus Torvalds 179453d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work); 179553d5914fSMikulas Patocka 17964a0b4ddfSMike Snitzer static void dm_init_md_queue(struct mapped_device *md) 17974a0b4ddfSMike Snitzer { 17984a0b4ddfSMike Snitzer /* 17994a0b4ddfSMike Snitzer * Request-based dm devices cannot be stacked on top of bio-based dm 18004a0b4ddfSMike Snitzer * devices. The type of this dm device has not been decided yet. 18014a0b4ddfSMike Snitzer * The type is decided at the first table loading time. 18024a0b4ddfSMike Snitzer * To prevent problematic device stacking, clear the queue flag 18034a0b4ddfSMike Snitzer * for request stacking support until then. 18044a0b4ddfSMike Snitzer * 18054a0b4ddfSMike Snitzer * This queue is new, so no concurrency on the queue_flags. 18064a0b4ddfSMike Snitzer */ 18074a0b4ddfSMike Snitzer queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue); 18084a0b4ddfSMike Snitzer 18094a0b4ddfSMike Snitzer md->queue->queuedata = md; 18104a0b4ddfSMike Snitzer md->queue->backing_dev_info.congested_fn = dm_any_congested; 18114a0b4ddfSMike Snitzer md->queue->backing_dev_info.congested_data = md; 18124a0b4ddfSMike Snitzer blk_queue_make_request(md->queue, dm_request); 18134a0b4ddfSMike Snitzer blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY); 18144a0b4ddfSMike Snitzer blk_queue_merge_bvec(md->queue, dm_merge_bvec); 18154a0b4ddfSMike Snitzer } 18164a0b4ddfSMike Snitzer 18171da177e4SLinus Torvalds /* 18181da177e4SLinus Torvalds * Allocate and initialise a blank device with a given minor. 18191da177e4SLinus Torvalds */ 18202b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor) 18211da177e4SLinus Torvalds { 18221da177e4SLinus Torvalds int r; 1823cf13ab8eSFrederik Deweerdt struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL); 1824ba61fdd1SJeff Mahoney void *old_md; 18251da177e4SLinus Torvalds 18261da177e4SLinus Torvalds if (!md) { 18271da177e4SLinus Torvalds DMWARN("unable to allocate device, out of memory."); 18281da177e4SLinus Torvalds return NULL; 18291da177e4SLinus Torvalds } 18301da177e4SLinus Torvalds 183110da4f79SJeff Mahoney if (!try_module_get(THIS_MODULE)) 18326ed7ade8SMilan Broz goto bad_module_get; 183310da4f79SJeff Mahoney 18341da177e4SLinus Torvalds /* get a minor number for the dev */ 18352b06cfffSAlasdair G Kergon if (minor == DM_ANY_MINOR) 1836cf13ab8eSFrederik Deweerdt r = next_free_minor(&minor); 18372b06cfffSAlasdair G Kergon else 1838cf13ab8eSFrederik Deweerdt r = specific_minor(minor); 18391da177e4SLinus Torvalds if (r < 0) 18406ed7ade8SMilan Broz goto bad_minor; 18411da177e4SLinus Torvalds 1842a5664dadSMike Snitzer md->type = DM_TYPE_NONE; 18432ca3310eSAlasdair G Kergon init_rwsem(&md->io_lock); 1844e61290a4SDaniel Walker mutex_init(&md->suspend_lock); 1845a5664dadSMike Snitzer mutex_init(&md->type_lock); 1846022c2611SMikulas Patocka spin_lock_init(&md->deferred_lock); 18471da177e4SLinus Torvalds rwlock_init(&md->map_lock); 18481da177e4SLinus Torvalds atomic_set(&md->holders, 1); 18495c6bd75dSAlasdair G Kergon atomic_set(&md->open_count, 0); 18501da177e4SLinus Torvalds atomic_set(&md->event_nr, 0); 18517a8c3d3bSMike Anderson atomic_set(&md->uevent_seq, 0); 18527a8c3d3bSMike Anderson INIT_LIST_HEAD(&md->uevent_list); 18537a8c3d3bSMike Anderson spin_lock_init(&md->uevent_lock); 18541da177e4SLinus Torvalds 18554a0b4ddfSMike Snitzer md->queue = blk_alloc_queue(GFP_KERNEL); 18561da177e4SLinus Torvalds if (!md->queue) 18576ed7ade8SMilan Broz goto bad_queue; 18581da177e4SLinus Torvalds 18594a0b4ddfSMike Snitzer dm_init_md_queue(md); 18609faf400fSStefan Bader 18611da177e4SLinus Torvalds md->disk = alloc_disk(1); 18621da177e4SLinus Torvalds if (!md->disk) 18636ed7ade8SMilan Broz goto bad_disk; 18641da177e4SLinus Torvalds 1865316d315bSNikanth Karthikesan atomic_set(&md->pending[0], 0); 1866316d315bSNikanth Karthikesan atomic_set(&md->pending[1], 0); 1867f0b04115SJeff Mahoney init_waitqueue_head(&md->wait); 186853d5914fSMikulas Patocka INIT_WORK(&md->work, dm_wq_work); 1869f0b04115SJeff Mahoney init_waitqueue_head(&md->eventq); 1870f0b04115SJeff Mahoney 18711da177e4SLinus Torvalds md->disk->major = _major; 18721da177e4SLinus Torvalds md->disk->first_minor = minor; 18731da177e4SLinus Torvalds md->disk->fops = &dm_blk_dops; 18741da177e4SLinus Torvalds md->disk->queue = md->queue; 18751da177e4SLinus Torvalds md->disk->private_data = md; 18761da177e4SLinus Torvalds sprintf(md->disk->disk_name, "dm-%d", minor); 18771da177e4SLinus Torvalds add_disk(md->disk); 18787e51f257SMike Anderson format_dev_t(md->name, MKDEV(_major, minor)); 18791da177e4SLinus Torvalds 18809c4376deSTejun Heo md->wq = alloc_workqueue("kdmflush", 18819c4376deSTejun Heo WQ_NON_REENTRANT | WQ_MEM_RECLAIM, 0); 1882304f3f6aSMilan Broz if (!md->wq) 1883304f3f6aSMilan Broz goto bad_thread; 1884304f3f6aSMilan Broz 188532a926daSMikulas Patocka md->bdev = bdget_disk(md->disk, 0); 188632a926daSMikulas Patocka if (!md->bdev) 188732a926daSMikulas Patocka goto bad_bdev; 188832a926daSMikulas Patocka 18896a8736d1STejun Heo bio_init(&md->flush_bio); 18906a8736d1STejun Heo md->flush_bio.bi_bdev = md->bdev; 18916a8736d1STejun Heo md->flush_bio.bi_rw = WRITE_FLUSH; 18926a8736d1STejun Heo 1893ba61fdd1SJeff Mahoney /* Populate the mapping, nobody knows we exist yet */ 1894f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 1895ba61fdd1SJeff Mahoney old_md = idr_replace(&_minor_idr, md, minor); 1896f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1897ba61fdd1SJeff Mahoney 1898ba61fdd1SJeff Mahoney BUG_ON(old_md != MINOR_ALLOCED); 1899ba61fdd1SJeff Mahoney 19001da177e4SLinus Torvalds return md; 19011da177e4SLinus Torvalds 190232a926daSMikulas Patocka bad_bdev: 190332a926daSMikulas Patocka destroy_workqueue(md->wq); 1904304f3f6aSMilan Broz bad_thread: 190503022c54SZdenek Kabelac del_gendisk(md->disk); 1906304f3f6aSMilan Broz put_disk(md->disk); 19076ed7ade8SMilan Broz bad_disk: 19081312f40eSAl Viro blk_cleanup_queue(md->queue); 19096ed7ade8SMilan Broz bad_queue: 19101da177e4SLinus Torvalds free_minor(minor); 19116ed7ade8SMilan Broz bad_minor: 191210da4f79SJeff Mahoney module_put(THIS_MODULE); 19136ed7ade8SMilan Broz bad_module_get: 19141da177e4SLinus Torvalds kfree(md); 19151da177e4SLinus Torvalds return NULL; 19161da177e4SLinus Torvalds } 19171da177e4SLinus Torvalds 1918ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md); 1919ae9da83fSJun'ichi Nomura 19201da177e4SLinus Torvalds static void free_dev(struct mapped_device *md) 19211da177e4SLinus Torvalds { 1922f331c029STejun Heo int minor = MINOR(disk_devt(md->disk)); 192363d94e48SJun'ichi Nomura 1924ae9da83fSJun'ichi Nomura unlock_fs(md); 1925db8fef4fSMikulas Patocka bdput(md->bdev); 1926304f3f6aSMilan Broz destroy_workqueue(md->wq); 1927e6ee8c0bSKiyoshi Ueda if (md->tio_pool) 19281da177e4SLinus Torvalds mempool_destroy(md->tio_pool); 1929e6ee8c0bSKiyoshi Ueda if (md->io_pool) 19301da177e4SLinus Torvalds mempool_destroy(md->io_pool); 1931e6ee8c0bSKiyoshi Ueda if (md->bs) 19329faf400fSStefan Bader bioset_free(md->bs); 19339c47008dSMartin K. Petersen blk_integrity_unregister(md->disk); 19341da177e4SLinus Torvalds del_gendisk(md->disk); 193563d94e48SJun'ichi Nomura free_minor(minor); 1936fba9f90eSJeff Mahoney 1937fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 1938fba9f90eSJeff Mahoney md->disk->private_data = NULL; 1939fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 1940fba9f90eSJeff Mahoney 19411da177e4SLinus Torvalds put_disk(md->disk); 19421312f40eSAl Viro blk_cleanup_queue(md->queue); 194310da4f79SJeff Mahoney module_put(THIS_MODULE); 19441da177e4SLinus Torvalds kfree(md); 19451da177e4SLinus Torvalds } 19461da177e4SLinus Torvalds 1947e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t) 1948e6ee8c0bSKiyoshi Ueda { 1949c0820cf5SMikulas Patocka struct dm_md_mempools *p = dm_table_get_md_mempools(t); 1950e6ee8c0bSKiyoshi Ueda 1951c0820cf5SMikulas Patocka if (md->io_pool && (md->tio_pool || dm_table_get_type(t) == DM_TYPE_BIO_BASED) && md->bs) { 1952c0820cf5SMikulas Patocka /* 1953c0820cf5SMikulas Patocka * The md already has necessary mempools. Reload just the 1954c0820cf5SMikulas Patocka * bioset because front_pad may have changed because 1955c0820cf5SMikulas Patocka * a different table was loaded. 1956c0820cf5SMikulas Patocka */ 1957c0820cf5SMikulas Patocka bioset_free(md->bs); 1958c0820cf5SMikulas Patocka md->bs = p->bs; 1959c0820cf5SMikulas Patocka p->bs = NULL; 1960e6ee8c0bSKiyoshi Ueda goto out; 1961c0820cf5SMikulas Patocka } 1962e6ee8c0bSKiyoshi Ueda 1963e6ee8c0bSKiyoshi Ueda BUG_ON(!p || md->io_pool || md->tio_pool || md->bs); 1964e6ee8c0bSKiyoshi Ueda 1965e6ee8c0bSKiyoshi Ueda md->io_pool = p->io_pool; 1966e6ee8c0bSKiyoshi Ueda p->io_pool = NULL; 1967e6ee8c0bSKiyoshi Ueda md->tio_pool = p->tio_pool; 1968e6ee8c0bSKiyoshi Ueda p->tio_pool = NULL; 1969e6ee8c0bSKiyoshi Ueda md->bs = p->bs; 1970e6ee8c0bSKiyoshi Ueda p->bs = NULL; 1971e6ee8c0bSKiyoshi Ueda 1972e6ee8c0bSKiyoshi Ueda out: 1973e6ee8c0bSKiyoshi Ueda /* mempool bind completed, now no need any mempools in the table */ 1974e6ee8c0bSKiyoshi Ueda dm_table_free_md_mempools(t); 1975e6ee8c0bSKiyoshi Ueda } 1976e6ee8c0bSKiyoshi Ueda 19771da177e4SLinus Torvalds /* 19781da177e4SLinus Torvalds * Bind a table to the device. 19791da177e4SLinus Torvalds */ 19801da177e4SLinus Torvalds static void event_callback(void *context) 19811da177e4SLinus Torvalds { 19827a8c3d3bSMike Anderson unsigned long flags; 19837a8c3d3bSMike Anderson LIST_HEAD(uevents); 19841da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) context; 19851da177e4SLinus Torvalds 19867a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 19877a8c3d3bSMike Anderson list_splice_init(&md->uevent_list, &uevents); 19887a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 19897a8c3d3bSMike Anderson 1990ed9e1982STejun Heo dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); 19917a8c3d3bSMike Anderson 19921da177e4SLinus Torvalds atomic_inc(&md->event_nr); 19931da177e4SLinus Torvalds wake_up(&md->eventq); 19941da177e4SLinus Torvalds } 19951da177e4SLinus Torvalds 1996c217649bSMike Snitzer /* 1997c217649bSMike Snitzer * Protected by md->suspend_lock obtained by dm_swap_table(). 1998c217649bSMike Snitzer */ 19994e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size) 20001da177e4SLinus Torvalds { 20014e90188bSAlasdair G Kergon set_capacity(md->disk, size); 20021da177e4SLinus Torvalds 2003db8fef4fSMikulas Patocka i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT); 20041da177e4SLinus Torvalds } 20051da177e4SLinus Torvalds 2006042d2a9bSAlasdair G Kergon /* 2007d5b9dd04SMikulas Patocka * Return 1 if the queue has a compulsory merge_bvec_fn function. 2008d5b9dd04SMikulas Patocka * 2009d5b9dd04SMikulas Patocka * If this function returns 0, then the device is either a non-dm 2010d5b9dd04SMikulas Patocka * device without a merge_bvec_fn, or it is a dm device that is 2011d5b9dd04SMikulas Patocka * able to split any bios it receives that are too big. 2012d5b9dd04SMikulas Patocka */ 2013d5b9dd04SMikulas Patocka int dm_queue_merge_is_compulsory(struct request_queue *q) 2014d5b9dd04SMikulas Patocka { 2015d5b9dd04SMikulas Patocka struct mapped_device *dev_md; 2016d5b9dd04SMikulas Patocka 2017d5b9dd04SMikulas Patocka if (!q->merge_bvec_fn) 2018d5b9dd04SMikulas Patocka return 0; 2019d5b9dd04SMikulas Patocka 2020d5b9dd04SMikulas Patocka if (q->make_request_fn == dm_request) { 2021d5b9dd04SMikulas Patocka dev_md = q->queuedata; 2022d5b9dd04SMikulas Patocka if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags)) 2023d5b9dd04SMikulas Patocka return 0; 2024d5b9dd04SMikulas Patocka } 2025d5b9dd04SMikulas Patocka 2026d5b9dd04SMikulas Patocka return 1; 2027d5b9dd04SMikulas Patocka } 2028d5b9dd04SMikulas Patocka 2029d5b9dd04SMikulas Patocka static int dm_device_merge_is_compulsory(struct dm_target *ti, 2030d5b9dd04SMikulas Patocka struct dm_dev *dev, sector_t start, 2031d5b9dd04SMikulas Patocka sector_t len, void *data) 2032d5b9dd04SMikulas Patocka { 2033d5b9dd04SMikulas Patocka struct block_device *bdev = dev->bdev; 2034d5b9dd04SMikulas Patocka struct request_queue *q = bdev_get_queue(bdev); 2035d5b9dd04SMikulas Patocka 2036d5b9dd04SMikulas Patocka return dm_queue_merge_is_compulsory(q); 2037d5b9dd04SMikulas Patocka } 2038d5b9dd04SMikulas Patocka 2039d5b9dd04SMikulas Patocka /* 2040d5b9dd04SMikulas Patocka * Return 1 if it is acceptable to ignore merge_bvec_fn based 2041d5b9dd04SMikulas Patocka * on the properties of the underlying devices. 2042d5b9dd04SMikulas Patocka */ 2043d5b9dd04SMikulas Patocka static int dm_table_merge_is_optional(struct dm_table *table) 2044d5b9dd04SMikulas Patocka { 2045d5b9dd04SMikulas Patocka unsigned i = 0; 2046d5b9dd04SMikulas Patocka struct dm_target *ti; 2047d5b9dd04SMikulas Patocka 2048d5b9dd04SMikulas Patocka while (i < dm_table_get_num_targets(table)) { 2049d5b9dd04SMikulas Patocka ti = dm_table_get_target(table, i++); 2050d5b9dd04SMikulas Patocka 2051d5b9dd04SMikulas Patocka if (ti->type->iterate_devices && 2052d5b9dd04SMikulas Patocka ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL)) 2053d5b9dd04SMikulas Patocka return 0; 2054d5b9dd04SMikulas Patocka } 2055d5b9dd04SMikulas Patocka 2056d5b9dd04SMikulas Patocka return 1; 2057d5b9dd04SMikulas Patocka } 2058d5b9dd04SMikulas Patocka 2059d5b9dd04SMikulas Patocka /* 2060042d2a9bSAlasdair G Kergon * Returns old map, which caller must destroy. 2061042d2a9bSAlasdair G Kergon */ 2062042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t, 2063754c5fc7SMike Snitzer struct queue_limits *limits) 20641da177e4SLinus Torvalds { 2065042d2a9bSAlasdair G Kergon struct dm_table *old_map; 2066165125e1SJens Axboe struct request_queue *q = md->queue; 20671da177e4SLinus Torvalds sector_t size; 2068523d9297SKiyoshi Ueda unsigned long flags; 2069d5b9dd04SMikulas Patocka int merge_is_optional; 20701da177e4SLinus Torvalds 20711da177e4SLinus Torvalds size = dm_table_get_size(t); 20723ac51e74SDarrick J. Wong 20733ac51e74SDarrick J. Wong /* 20743ac51e74SDarrick J. Wong * Wipe any geometry if the size of the table changed. 20753ac51e74SDarrick J. Wong */ 20763ac51e74SDarrick J. Wong if (size != get_capacity(md->disk)) 20773ac51e74SDarrick J. Wong memset(&md->geometry, 0, sizeof(md->geometry)); 20783ac51e74SDarrick J. Wong 20794e90188bSAlasdair G Kergon __set_size(md, size); 20801da177e4SLinus Torvalds 2081cf222b37SAlasdair G Kergon dm_table_event_callback(t, event_callback, md); 20822ca3310eSAlasdair G Kergon 2083e6ee8c0bSKiyoshi Ueda /* 2084e6ee8c0bSKiyoshi Ueda * The queue hasn't been stopped yet, if the old table type wasn't 2085e6ee8c0bSKiyoshi Ueda * for request-based during suspension. So stop it to prevent 2086e6ee8c0bSKiyoshi Ueda * I/O mapping before resume. 2087e6ee8c0bSKiyoshi Ueda * This must be done before setting the queue restrictions, 2088e6ee8c0bSKiyoshi Ueda * because request-based dm may be run just after the setting. 2089e6ee8c0bSKiyoshi Ueda */ 2090e6ee8c0bSKiyoshi Ueda if (dm_table_request_based(t) && !blk_queue_stopped(q)) 2091e6ee8c0bSKiyoshi Ueda stop_queue(q); 2092e6ee8c0bSKiyoshi Ueda 2093e6ee8c0bSKiyoshi Ueda __bind_mempools(md, t); 2094e6ee8c0bSKiyoshi Ueda 2095d5b9dd04SMikulas Patocka merge_is_optional = dm_table_merge_is_optional(t); 2096d5b9dd04SMikulas Patocka 2097523d9297SKiyoshi Ueda write_lock_irqsave(&md->map_lock, flags); 2098042d2a9bSAlasdair G Kergon old_map = md->map; 20992ca3310eSAlasdair G Kergon md->map = t; 210036a0456fSAlasdair G Kergon md->immutable_target_type = dm_table_get_immutable_target_type(t); 210136a0456fSAlasdair G Kergon 2102754c5fc7SMike Snitzer dm_table_set_restrictions(t, q, limits); 2103d5b9dd04SMikulas Patocka if (merge_is_optional) 2104d5b9dd04SMikulas Patocka set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags); 2105d5b9dd04SMikulas Patocka else 2106d5b9dd04SMikulas Patocka clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags); 2107523d9297SKiyoshi Ueda write_unlock_irqrestore(&md->map_lock, flags); 21082ca3310eSAlasdair G Kergon 2109042d2a9bSAlasdair G Kergon return old_map; 21101da177e4SLinus Torvalds } 21111da177e4SLinus Torvalds 2112a7940155SAlasdair G Kergon /* 2113a7940155SAlasdair G Kergon * Returns unbound table for the caller to free. 2114a7940155SAlasdair G Kergon */ 2115a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md) 21161da177e4SLinus Torvalds { 21171da177e4SLinus Torvalds struct dm_table *map = md->map; 2118523d9297SKiyoshi Ueda unsigned long flags; 21191da177e4SLinus Torvalds 21201da177e4SLinus Torvalds if (!map) 2121a7940155SAlasdair G Kergon return NULL; 21221da177e4SLinus Torvalds 21231da177e4SLinus Torvalds dm_table_event_callback(map, NULL, NULL); 2124523d9297SKiyoshi Ueda write_lock_irqsave(&md->map_lock, flags); 21251da177e4SLinus Torvalds md->map = NULL; 2126523d9297SKiyoshi Ueda write_unlock_irqrestore(&md->map_lock, flags); 2127a7940155SAlasdair G Kergon 2128a7940155SAlasdair G Kergon return map; 21291da177e4SLinus Torvalds } 21301da177e4SLinus Torvalds 21311da177e4SLinus Torvalds /* 21321da177e4SLinus Torvalds * Constructor for a new device. 21331da177e4SLinus Torvalds */ 21342b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result) 21351da177e4SLinus Torvalds { 21361da177e4SLinus Torvalds struct mapped_device *md; 21371da177e4SLinus Torvalds 21382b06cfffSAlasdair G Kergon md = alloc_dev(minor); 21391da177e4SLinus Torvalds if (!md) 21401da177e4SLinus Torvalds return -ENXIO; 21411da177e4SLinus Torvalds 2142784aae73SMilan Broz dm_sysfs_init(md); 2143784aae73SMilan Broz 21441da177e4SLinus Torvalds *result = md; 21451da177e4SLinus Torvalds return 0; 21461da177e4SLinus Torvalds } 21471da177e4SLinus Torvalds 2148a5664dadSMike Snitzer /* 2149a5664dadSMike Snitzer * Functions to manage md->type. 2150a5664dadSMike Snitzer * All are required to hold md->type_lock. 2151a5664dadSMike Snitzer */ 2152a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md) 2153a5664dadSMike Snitzer { 2154a5664dadSMike Snitzer mutex_lock(&md->type_lock); 2155a5664dadSMike Snitzer } 2156a5664dadSMike Snitzer 2157a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md) 2158a5664dadSMike Snitzer { 2159a5664dadSMike Snitzer mutex_unlock(&md->type_lock); 2160a5664dadSMike Snitzer } 2161a5664dadSMike Snitzer 2162a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type) 2163a5664dadSMike Snitzer { 2164a5664dadSMike Snitzer md->type = type; 2165a5664dadSMike Snitzer } 2166a5664dadSMike Snitzer 2167a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md) 2168a5664dadSMike Snitzer { 2169a5664dadSMike Snitzer return md->type; 2170a5664dadSMike Snitzer } 2171a5664dadSMike Snitzer 217236a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md) 217336a0456fSAlasdair G Kergon { 217436a0456fSAlasdair G Kergon return md->immutable_target_type; 217536a0456fSAlasdair G Kergon } 217636a0456fSAlasdair G Kergon 21774a0b4ddfSMike Snitzer /* 21784a0b4ddfSMike Snitzer * Fully initialize a request-based queue (->elevator, ->request_fn, etc). 21794a0b4ddfSMike Snitzer */ 21804a0b4ddfSMike Snitzer static int dm_init_request_based_queue(struct mapped_device *md) 21814a0b4ddfSMike Snitzer { 21824a0b4ddfSMike Snitzer struct request_queue *q = NULL; 21834a0b4ddfSMike Snitzer 21844a0b4ddfSMike Snitzer if (md->queue->elevator) 21854a0b4ddfSMike Snitzer return 1; 21864a0b4ddfSMike Snitzer 21874a0b4ddfSMike Snitzer /* Fully initialize the queue */ 21884a0b4ddfSMike Snitzer q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL); 21894a0b4ddfSMike Snitzer if (!q) 21904a0b4ddfSMike Snitzer return 0; 21914a0b4ddfSMike Snitzer 21924a0b4ddfSMike Snitzer md->queue = q; 21934a0b4ddfSMike Snitzer dm_init_md_queue(md); 21944a0b4ddfSMike Snitzer blk_queue_softirq_done(md->queue, dm_softirq_done); 21954a0b4ddfSMike Snitzer blk_queue_prep_rq(md->queue, dm_prep_fn); 21964a0b4ddfSMike Snitzer blk_queue_lld_busy(md->queue, dm_lld_busy); 21974a0b4ddfSMike Snitzer 21984a0b4ddfSMike Snitzer elv_register_queue(md->queue); 21994a0b4ddfSMike Snitzer 22004a0b4ddfSMike Snitzer return 1; 22014a0b4ddfSMike Snitzer } 22024a0b4ddfSMike Snitzer 22034a0b4ddfSMike Snitzer /* 22044a0b4ddfSMike Snitzer * Setup the DM device's queue based on md's type 22054a0b4ddfSMike Snitzer */ 22064a0b4ddfSMike Snitzer int dm_setup_md_queue(struct mapped_device *md) 22074a0b4ddfSMike Snitzer { 22084a0b4ddfSMike Snitzer if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) && 22094a0b4ddfSMike Snitzer !dm_init_request_based_queue(md)) { 22104a0b4ddfSMike Snitzer DMWARN("Cannot initialize queue for request-based mapped device"); 22114a0b4ddfSMike Snitzer return -EINVAL; 22124a0b4ddfSMike Snitzer } 22134a0b4ddfSMike Snitzer 22144a0b4ddfSMike Snitzer return 0; 22154a0b4ddfSMike Snitzer } 22164a0b4ddfSMike Snitzer 2217637842cfSDavid Teigland static struct mapped_device *dm_find_md(dev_t dev) 22181da177e4SLinus Torvalds { 22191da177e4SLinus Torvalds struct mapped_device *md; 22201da177e4SLinus Torvalds unsigned minor = MINOR(dev); 22211da177e4SLinus Torvalds 22221da177e4SLinus Torvalds if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 22231da177e4SLinus Torvalds return NULL; 22241da177e4SLinus Torvalds 2225f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 22261da177e4SLinus Torvalds 22271da177e4SLinus Torvalds md = idr_find(&_minor_idr, minor); 2228fba9f90eSJeff Mahoney if (md && (md == MINOR_ALLOCED || 2229f331c029STejun Heo (MINOR(disk_devt(dm_disk(md))) != minor) || 2230abdc568bSKiyoshi Ueda dm_deleting_md(md) || 2231fba9f90eSJeff Mahoney test_bit(DMF_FREEING, &md->flags))) { 2232637842cfSDavid Teigland md = NULL; 2233fba9f90eSJeff Mahoney goto out; 2234fba9f90eSJeff Mahoney } 22351da177e4SLinus Torvalds 2236fba9f90eSJeff Mahoney out: 2237f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 22381da177e4SLinus Torvalds 2239637842cfSDavid Teigland return md; 2240637842cfSDavid Teigland } 2241637842cfSDavid Teigland 2242d229a958SDavid Teigland struct mapped_device *dm_get_md(dev_t dev) 2243d229a958SDavid Teigland { 2244d229a958SDavid Teigland struct mapped_device *md = dm_find_md(dev); 2245d229a958SDavid Teigland 2246d229a958SDavid Teigland if (md) 2247d229a958SDavid Teigland dm_get(md); 2248d229a958SDavid Teigland 2249d229a958SDavid Teigland return md; 2250d229a958SDavid Teigland } 22513cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md); 2252d229a958SDavid Teigland 22539ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md) 2254637842cfSDavid Teigland { 22559ade92a9SAlasdair G Kergon return md->interface_ptr; 22561da177e4SLinus Torvalds } 22571da177e4SLinus Torvalds 22581da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr) 22591da177e4SLinus Torvalds { 22601da177e4SLinus Torvalds md->interface_ptr = ptr; 22611da177e4SLinus Torvalds } 22621da177e4SLinus Torvalds 22631da177e4SLinus Torvalds void dm_get(struct mapped_device *md) 22641da177e4SLinus Torvalds { 22651da177e4SLinus Torvalds atomic_inc(&md->holders); 22663f77316dSKiyoshi Ueda BUG_ON(test_bit(DMF_FREEING, &md->flags)); 22671da177e4SLinus Torvalds } 22681da177e4SLinus Torvalds 226972d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md) 227072d94861SAlasdair G Kergon { 227172d94861SAlasdair G Kergon return md->name; 227272d94861SAlasdair G Kergon } 227372d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name); 227472d94861SAlasdair G Kergon 22753f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait) 22761da177e4SLinus Torvalds { 22771134e5aeSMike Anderson struct dm_table *map; 22781da177e4SLinus Torvalds 22793f77316dSKiyoshi Ueda might_sleep(); 2280fba9f90eSJeff Mahoney 22813f77316dSKiyoshi Ueda spin_lock(&_minor_lock); 22827c666411SAlasdair G Kergon map = dm_get_live_table(md); 22833f77316dSKiyoshi Ueda idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md)))); 2284fba9f90eSJeff Mahoney set_bit(DMF_FREEING, &md->flags); 2285f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 22863f77316dSKiyoshi Ueda 22874f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) { 22881da177e4SLinus Torvalds dm_table_presuspend_targets(map); 22891da177e4SLinus Torvalds dm_table_postsuspend_targets(map); 22901da177e4SLinus Torvalds } 22913f77316dSKiyoshi Ueda 22923f77316dSKiyoshi Ueda /* 22933f77316dSKiyoshi Ueda * Rare, but there may be I/O requests still going to complete, 22943f77316dSKiyoshi Ueda * for example. Wait for all references to disappear. 22953f77316dSKiyoshi Ueda * No one should increment the reference count of the mapped_device, 22963f77316dSKiyoshi Ueda * after the mapped_device state becomes DMF_FREEING. 22973f77316dSKiyoshi Ueda */ 22983f77316dSKiyoshi Ueda if (wait) 22993f77316dSKiyoshi Ueda while (atomic_read(&md->holders)) 23003f77316dSKiyoshi Ueda msleep(1); 23013f77316dSKiyoshi Ueda else if (atomic_read(&md->holders)) 23023f77316dSKiyoshi Ueda DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)", 23033f77316dSKiyoshi Ueda dm_device_name(md), atomic_read(&md->holders)); 23043f77316dSKiyoshi Ueda 2305784aae73SMilan Broz dm_sysfs_exit(md); 23061134e5aeSMike Anderson dm_table_put(map); 2307a7940155SAlasdair G Kergon dm_table_destroy(__unbind(md)); 23081da177e4SLinus Torvalds free_dev(md); 23091da177e4SLinus Torvalds } 23103f77316dSKiyoshi Ueda 23113f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md) 23123f77316dSKiyoshi Ueda { 23133f77316dSKiyoshi Ueda __dm_destroy(md, true); 23143f77316dSKiyoshi Ueda } 23153f77316dSKiyoshi Ueda 23163f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md) 23173f77316dSKiyoshi Ueda { 23183f77316dSKiyoshi Ueda __dm_destroy(md, false); 23193f77316dSKiyoshi Ueda } 23203f77316dSKiyoshi Ueda 23213f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md) 23223f77316dSKiyoshi Ueda { 23233f77316dSKiyoshi Ueda atomic_dec(&md->holders); 23241da177e4SLinus Torvalds } 232579eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put); 23261da177e4SLinus Torvalds 2327401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible) 232846125c1cSMilan Broz { 232946125c1cSMilan Broz int r = 0; 2330b44ebeb0SMikulas Patocka DECLARE_WAITQUEUE(wait, current); 2331b44ebeb0SMikulas Patocka 2332b44ebeb0SMikulas Patocka add_wait_queue(&md->wait, &wait); 233346125c1cSMilan Broz 233446125c1cSMilan Broz while (1) { 2335401600dfSMikulas Patocka set_current_state(interruptible); 233646125c1cSMilan Broz 2337b4324feeSKiyoshi Ueda if (!md_in_flight(md)) 233846125c1cSMilan Broz break; 233946125c1cSMilan Broz 2340401600dfSMikulas Patocka if (interruptible == TASK_INTERRUPTIBLE && 2341401600dfSMikulas Patocka signal_pending(current)) { 234246125c1cSMilan Broz r = -EINTR; 234346125c1cSMilan Broz break; 234446125c1cSMilan Broz } 234546125c1cSMilan Broz 234646125c1cSMilan Broz io_schedule(); 234746125c1cSMilan Broz } 234846125c1cSMilan Broz set_current_state(TASK_RUNNING); 234946125c1cSMilan Broz 2350b44ebeb0SMikulas Patocka remove_wait_queue(&md->wait, &wait); 2351b44ebeb0SMikulas Patocka 235246125c1cSMilan Broz return r; 235346125c1cSMilan Broz } 235446125c1cSMilan Broz 23551da177e4SLinus Torvalds /* 23561da177e4SLinus Torvalds * Process the deferred bios 23571da177e4SLinus Torvalds */ 2358ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work) 23591da177e4SLinus Torvalds { 2360ef208587SMikulas Patocka struct mapped_device *md = container_of(work, struct mapped_device, 2361ef208587SMikulas Patocka work); 23626d6f10dfSMilan Broz struct bio *c; 23631da177e4SLinus Torvalds 23646a8736d1STejun Heo down_read(&md->io_lock); 2365ef208587SMikulas Patocka 23663b00b203SMikulas Patocka while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 2367022c2611SMikulas Patocka spin_lock_irq(&md->deferred_lock); 2368022c2611SMikulas Patocka c = bio_list_pop(&md->deferred); 2369022c2611SMikulas Patocka spin_unlock_irq(&md->deferred_lock); 2370022c2611SMikulas Patocka 23716a8736d1STejun Heo if (!c) 2372df12ee99SAlasdair G Kergon break; 237373d410c0SMilan Broz 23746a8736d1STejun Heo up_read(&md->io_lock); 23753b00b203SMikulas Patocka 2376e6ee8c0bSKiyoshi Ueda if (dm_request_based(md)) 2377e6ee8c0bSKiyoshi Ueda generic_make_request(c); 2378af7e466aSMikulas Patocka else 2379df12ee99SAlasdair G Kergon __split_and_process_bio(md, c); 23806a8736d1STejun Heo 23816a8736d1STejun Heo down_read(&md->io_lock); 2382e6ee8c0bSKiyoshi Ueda } 23833b00b203SMikulas Patocka 23846a8736d1STejun Heo up_read(&md->io_lock); 23851da177e4SLinus Torvalds } 23861da177e4SLinus Torvalds 23879a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md) 2388304f3f6aSMilan Broz { 23893b00b203SMikulas Patocka clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 23903b00b203SMikulas Patocka smp_mb__after_clear_bit(); 239153d5914fSMikulas Patocka queue_work(md->wq, &md->work); 2392304f3f6aSMilan Broz } 2393304f3f6aSMilan Broz 23941da177e4SLinus Torvalds /* 2395042d2a9bSAlasdair G Kergon * Swap in a new table, returning the old one for the caller to destroy. 23961da177e4SLinus Torvalds */ 2397042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table) 23981da177e4SLinus Torvalds { 23993ae70656SMike Snitzer struct dm_table *live_map, *map = ERR_PTR(-EINVAL); 2400754c5fc7SMike Snitzer struct queue_limits limits; 2401042d2a9bSAlasdair G Kergon int r; 24021da177e4SLinus Torvalds 2403e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 24041da177e4SLinus Torvalds 24051da177e4SLinus Torvalds /* device must be suspended */ 24064f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) 240793c534aeSAlasdair G Kergon goto out; 24081da177e4SLinus Torvalds 24093ae70656SMike Snitzer /* 24103ae70656SMike Snitzer * If the new table has no data devices, retain the existing limits. 24113ae70656SMike Snitzer * This helps multipath with queue_if_no_path if all paths disappear, 24123ae70656SMike Snitzer * then new I/O is queued based on these limits, and then some paths 24133ae70656SMike Snitzer * reappear. 24143ae70656SMike Snitzer */ 24153ae70656SMike Snitzer if (dm_table_has_no_data_devices(table)) { 24163ae70656SMike Snitzer live_map = dm_get_live_table(md); 24173ae70656SMike Snitzer if (live_map) 24183ae70656SMike Snitzer limits = md->queue->limits; 24193ae70656SMike Snitzer dm_table_put(live_map); 24203ae70656SMike Snitzer } 24213ae70656SMike Snitzer 2422754c5fc7SMike Snitzer r = dm_calculate_queue_limits(table, &limits); 2423042d2a9bSAlasdair G Kergon if (r) { 2424042d2a9bSAlasdair G Kergon map = ERR_PTR(r); 2425754c5fc7SMike Snitzer goto out; 2426042d2a9bSAlasdair G Kergon } 2427754c5fc7SMike Snitzer 2428042d2a9bSAlasdair G Kergon map = __bind(md, table, &limits); 24291da177e4SLinus Torvalds 243093c534aeSAlasdair G Kergon out: 2431e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 2432042d2a9bSAlasdair G Kergon return map; 24331da177e4SLinus Torvalds } 24341da177e4SLinus Torvalds 24351da177e4SLinus Torvalds /* 24361da177e4SLinus Torvalds * Functions to lock and unlock any filesystem running on the 24371da177e4SLinus Torvalds * device. 24381da177e4SLinus Torvalds */ 24392ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md) 24401da177e4SLinus Torvalds { 2441e39e2e95SAlasdair G Kergon int r; 24421da177e4SLinus Torvalds 24431da177e4SLinus Torvalds WARN_ON(md->frozen_sb); 2444dfbe03f6SAlasdair G Kergon 2445db8fef4fSMikulas Patocka md->frozen_sb = freeze_bdev(md->bdev); 2446dfbe03f6SAlasdair G Kergon if (IS_ERR(md->frozen_sb)) { 2447cf222b37SAlasdair G Kergon r = PTR_ERR(md->frozen_sb); 2448e39e2e95SAlasdair G Kergon md->frozen_sb = NULL; 2449e39e2e95SAlasdair G Kergon return r; 2450dfbe03f6SAlasdair G Kergon } 2451dfbe03f6SAlasdair G Kergon 2452aa8d7c2fSAlasdair G Kergon set_bit(DMF_FROZEN, &md->flags); 2453aa8d7c2fSAlasdair G Kergon 24541da177e4SLinus Torvalds return 0; 24551da177e4SLinus Torvalds } 24561da177e4SLinus Torvalds 24572ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md) 24581da177e4SLinus Torvalds { 2459aa8d7c2fSAlasdair G Kergon if (!test_bit(DMF_FROZEN, &md->flags)) 2460aa8d7c2fSAlasdair G Kergon return; 2461aa8d7c2fSAlasdair G Kergon 2462db8fef4fSMikulas Patocka thaw_bdev(md->bdev, md->frozen_sb); 24631da177e4SLinus Torvalds md->frozen_sb = NULL; 2464aa8d7c2fSAlasdair G Kergon clear_bit(DMF_FROZEN, &md->flags); 24651da177e4SLinus Torvalds } 24661da177e4SLinus Torvalds 24671da177e4SLinus Torvalds /* 24681da177e4SLinus Torvalds * We need to be able to change a mapping table under a mounted 24691da177e4SLinus Torvalds * filesystem. For example we might want to move some data in 24701da177e4SLinus Torvalds * the background. Before the table can be swapped with 24711da177e4SLinus Torvalds * dm_bind_table, dm_suspend must be called to flush any in 24721da177e4SLinus Torvalds * flight bios and ensure that any further io gets deferred. 24731da177e4SLinus Torvalds */ 2474cec47e3dSKiyoshi Ueda /* 2475cec47e3dSKiyoshi Ueda * Suspend mechanism in request-based dm. 2476cec47e3dSKiyoshi Ueda * 24779f518b27SKiyoshi Ueda * 1. Flush all I/Os by lock_fs() if needed. 24789f518b27SKiyoshi Ueda * 2. Stop dispatching any I/O by stopping the request_queue. 24799f518b27SKiyoshi Ueda * 3. Wait for all in-flight I/Os to be completed or requeued. 2480cec47e3dSKiyoshi Ueda * 24819f518b27SKiyoshi Ueda * To abort suspend, start the request_queue. 2482cec47e3dSKiyoshi Ueda */ 2483a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags) 24841da177e4SLinus Torvalds { 24852ca3310eSAlasdair G Kergon struct dm_table *map = NULL; 248646125c1cSMilan Broz int r = 0; 2487a3d77d35SKiyoshi Ueda int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0; 24882e93ccc1SKiyoshi Ueda int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0; 24891da177e4SLinus Torvalds 2490e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 24912ca3310eSAlasdair G Kergon 24924f186f8bSKiyoshi Ueda if (dm_suspended_md(md)) { 249373d410c0SMilan Broz r = -EINVAL; 2494d287483dSAlasdair G Kergon goto out_unlock; 249573d410c0SMilan Broz } 24961da177e4SLinus Torvalds 24977c666411SAlasdair G Kergon map = dm_get_live_table(md); 2498cf222b37SAlasdair G Kergon 24992e93ccc1SKiyoshi Ueda /* 25002e93ccc1SKiyoshi Ueda * DMF_NOFLUSH_SUSPENDING must be set before presuspend. 25012e93ccc1SKiyoshi Ueda * This flag is cleared before dm_suspend returns. 25022e93ccc1SKiyoshi Ueda */ 25032e93ccc1SKiyoshi Ueda if (noflush) 25042e93ccc1SKiyoshi Ueda set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 25052e93ccc1SKiyoshi Ueda 2506436d4108SAlasdair G Kergon /* This does not get reverted if there's an error later. */ 25071da177e4SLinus Torvalds dm_table_presuspend_targets(map); 25081da177e4SLinus Torvalds 25092e93ccc1SKiyoshi Ueda /* 25109f518b27SKiyoshi Ueda * Flush I/O to the device. 25119f518b27SKiyoshi Ueda * Any I/O submitted after lock_fs() may not be flushed. 25129f518b27SKiyoshi Ueda * noflush takes precedence over do_lockfs. 25139f518b27SKiyoshi Ueda * (lock_fs() flushes I/Os and waits for them to complete.) 25142e93ccc1SKiyoshi Ueda */ 251532a926daSMikulas Patocka if (!noflush && do_lockfs) { 25162ca3310eSAlasdair G Kergon r = lock_fs(md); 25172ca3310eSAlasdair G Kergon if (r) 25182ca3310eSAlasdair G Kergon goto out; 2519aa8d7c2fSAlasdair G Kergon } 25201da177e4SLinus Torvalds 25211da177e4SLinus Torvalds /* 25223b00b203SMikulas Patocka * Here we must make sure that no processes are submitting requests 25233b00b203SMikulas Patocka * to target drivers i.e. no one may be executing 25243b00b203SMikulas Patocka * __split_and_process_bio. This is called from dm_request and 25253b00b203SMikulas Patocka * dm_wq_work. 25263b00b203SMikulas Patocka * 25273b00b203SMikulas Patocka * To get all processes out of __split_and_process_bio in dm_request, 25283b00b203SMikulas Patocka * we take the write lock. To prevent any process from reentering 25296a8736d1STejun Heo * __split_and_process_bio from dm_request and quiesce the thread 25306a8736d1STejun Heo * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call 25316a8736d1STejun Heo * flush_workqueue(md->wq). 25321da177e4SLinus Torvalds */ 25332ca3310eSAlasdair G Kergon down_write(&md->io_lock); 25341eb787ecSAlasdair G Kergon set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 25352ca3310eSAlasdair G Kergon up_write(&md->io_lock); 25361da177e4SLinus Torvalds 2537d0bcb878SKiyoshi Ueda /* 253829e4013dSTejun Heo * Stop md->queue before flushing md->wq in case request-based 253929e4013dSTejun Heo * dm defers requests to md->wq from md->queue. 2540d0bcb878SKiyoshi Ueda */ 2541cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 25429f518b27SKiyoshi Ueda stop_queue(md->queue); 2543cec47e3dSKiyoshi Ueda 2544d0bcb878SKiyoshi Ueda flush_workqueue(md->wq); 2545d0bcb878SKiyoshi Ueda 25461da177e4SLinus Torvalds /* 25473b00b203SMikulas Patocka * At this point no more requests are entering target request routines. 25483b00b203SMikulas Patocka * We call dm_wait_for_completion to wait for all existing requests 25493b00b203SMikulas Patocka * to finish. 25501da177e4SLinus Torvalds */ 2551401600dfSMikulas Patocka r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE); 25521da177e4SLinus Torvalds 25532ca3310eSAlasdair G Kergon down_write(&md->io_lock); 25546d6f10dfSMilan Broz if (noflush) 2555022c2611SMikulas Patocka clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 255694d6351eSMilan Broz up_write(&md->io_lock); 25572e93ccc1SKiyoshi Ueda 25581da177e4SLinus Torvalds /* were we interrupted ? */ 255946125c1cSMilan Broz if (r < 0) { 25609a1fb464SMikulas Patocka dm_queue_flush(md); 256173d410c0SMilan Broz 2562cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 25639f518b27SKiyoshi Ueda start_queue(md->queue); 2564cec47e3dSKiyoshi Ueda 25652ca3310eSAlasdair G Kergon unlock_fs(md); 25662e93ccc1SKiyoshi Ueda goto out; /* pushback list is already flushed, so skip flush */ 25672ca3310eSAlasdair G Kergon } 25682ca3310eSAlasdair G Kergon 25693b00b203SMikulas Patocka /* 25703b00b203SMikulas Patocka * If dm_wait_for_completion returned 0, the device is completely 25713b00b203SMikulas Patocka * quiescent now. There is no request-processing activity. All new 25723b00b203SMikulas Patocka * requests are being added to md->deferred list. 25733b00b203SMikulas Patocka */ 25743b00b203SMikulas Patocka 25751da177e4SLinus Torvalds set_bit(DMF_SUSPENDED, &md->flags); 25761da177e4SLinus Torvalds 25774d4471cbSKiyoshi Ueda dm_table_postsuspend_targets(map); 25784d4471cbSKiyoshi Ueda 25792ca3310eSAlasdair G Kergon out: 25801da177e4SLinus Torvalds dm_table_put(map); 2581d287483dSAlasdair G Kergon 2582d287483dSAlasdair G Kergon out_unlock: 2583e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 2584cf222b37SAlasdair G Kergon return r; 25851da177e4SLinus Torvalds } 25861da177e4SLinus Torvalds 25871da177e4SLinus Torvalds int dm_resume(struct mapped_device *md) 25881da177e4SLinus Torvalds { 2589cf222b37SAlasdair G Kergon int r = -EINVAL; 2590cf222b37SAlasdair G Kergon struct dm_table *map = NULL; 25911da177e4SLinus Torvalds 2592e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 25934f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) 2594cf222b37SAlasdair G Kergon goto out; 2595cf222b37SAlasdair G Kergon 25967c666411SAlasdair G Kergon map = dm_get_live_table(md); 25972ca3310eSAlasdair G Kergon if (!map || !dm_table_get_size(map)) 2598cf222b37SAlasdair G Kergon goto out; 25991da177e4SLinus Torvalds 26008757b776SMilan Broz r = dm_table_resume_targets(map); 26018757b776SMilan Broz if (r) 26028757b776SMilan Broz goto out; 26032ca3310eSAlasdair G Kergon 26049a1fb464SMikulas Patocka dm_queue_flush(md); 26052ca3310eSAlasdair G Kergon 2606cec47e3dSKiyoshi Ueda /* 2607cec47e3dSKiyoshi Ueda * Flushing deferred I/Os must be done after targets are resumed 2608cec47e3dSKiyoshi Ueda * so that mapping of targets can work correctly. 2609cec47e3dSKiyoshi Ueda * Request-based dm is queueing the deferred I/Os in its request_queue. 2610cec47e3dSKiyoshi Ueda */ 2611cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 2612cec47e3dSKiyoshi Ueda start_queue(md->queue); 2613cec47e3dSKiyoshi Ueda 26142ca3310eSAlasdair G Kergon unlock_fs(md); 26152ca3310eSAlasdair G Kergon 26162ca3310eSAlasdair G Kergon clear_bit(DMF_SUSPENDED, &md->flags); 26172ca3310eSAlasdair G Kergon 2618cf222b37SAlasdair G Kergon r = 0; 2619cf222b37SAlasdair G Kergon out: 2620cf222b37SAlasdair G Kergon dm_table_put(map); 2621e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 26222ca3310eSAlasdair G Kergon 2623cf222b37SAlasdair G Kergon return r; 26241da177e4SLinus Torvalds } 26251da177e4SLinus Torvalds 26261da177e4SLinus Torvalds /*----------------------------------------------------------------- 26271da177e4SLinus Torvalds * Event notification. 26281da177e4SLinus Torvalds *---------------------------------------------------------------*/ 26293abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action, 263060935eb2SMilan Broz unsigned cookie) 263169267a30SAlasdair G Kergon { 263260935eb2SMilan Broz char udev_cookie[DM_COOKIE_LENGTH]; 263360935eb2SMilan Broz char *envp[] = { udev_cookie, NULL }; 263460935eb2SMilan Broz 263560935eb2SMilan Broz if (!cookie) 26363abf85b5SPeter Rajnoha return kobject_uevent(&disk_to_dev(md->disk)->kobj, action); 263760935eb2SMilan Broz else { 263860935eb2SMilan Broz snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u", 263960935eb2SMilan Broz DM_COOKIE_ENV_VAR_NAME, cookie); 26403abf85b5SPeter Rajnoha return kobject_uevent_env(&disk_to_dev(md->disk)->kobj, 26413abf85b5SPeter Rajnoha action, envp); 264260935eb2SMilan Broz } 264369267a30SAlasdair G Kergon } 264469267a30SAlasdair G Kergon 26457a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md) 26467a8c3d3bSMike Anderson { 26477a8c3d3bSMike Anderson return atomic_add_return(1, &md->uevent_seq); 26487a8c3d3bSMike Anderson } 26497a8c3d3bSMike Anderson 26501da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md) 26511da177e4SLinus Torvalds { 26521da177e4SLinus Torvalds return atomic_read(&md->event_nr); 26531da177e4SLinus Torvalds } 26541da177e4SLinus Torvalds 26551da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr) 26561da177e4SLinus Torvalds { 26571da177e4SLinus Torvalds return wait_event_interruptible(md->eventq, 26581da177e4SLinus Torvalds (event_nr != atomic_read(&md->event_nr))); 26591da177e4SLinus Torvalds } 26601da177e4SLinus Torvalds 26617a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist) 26627a8c3d3bSMike Anderson { 26637a8c3d3bSMike Anderson unsigned long flags; 26647a8c3d3bSMike Anderson 26657a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 26667a8c3d3bSMike Anderson list_add(elist, &md->uevent_list); 26677a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 26687a8c3d3bSMike Anderson } 26697a8c3d3bSMike Anderson 26701da177e4SLinus Torvalds /* 26711da177e4SLinus Torvalds * The gendisk is only valid as long as you have a reference 26721da177e4SLinus Torvalds * count on 'md'. 26731da177e4SLinus Torvalds */ 26741da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md) 26751da177e4SLinus Torvalds { 26761da177e4SLinus Torvalds return md->disk; 26771da177e4SLinus Torvalds } 26781da177e4SLinus Torvalds 2679784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md) 2680784aae73SMilan Broz { 2681784aae73SMilan Broz return &md->kobj; 2682784aae73SMilan Broz } 2683784aae73SMilan Broz 2684784aae73SMilan Broz /* 2685784aae73SMilan Broz * struct mapped_device should not be exported outside of dm.c 2686784aae73SMilan Broz * so use this check to verify that kobj is part of md structure 2687784aae73SMilan Broz */ 2688784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj) 2689784aae73SMilan Broz { 2690784aae73SMilan Broz struct mapped_device *md; 2691784aae73SMilan Broz 2692784aae73SMilan Broz md = container_of(kobj, struct mapped_device, kobj); 2693784aae73SMilan Broz if (&md->kobj != kobj) 2694784aae73SMilan Broz return NULL; 2695784aae73SMilan Broz 26964d89b7b4SMilan Broz if (test_bit(DMF_FREEING, &md->flags) || 2697432a212cSMike Anderson dm_deleting_md(md)) 26984d89b7b4SMilan Broz return NULL; 26994d89b7b4SMilan Broz 2700784aae73SMilan Broz dm_get(md); 2701784aae73SMilan Broz return md; 2702784aae73SMilan Broz } 2703784aae73SMilan Broz 27044f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md) 27051da177e4SLinus Torvalds { 27061da177e4SLinus Torvalds return test_bit(DMF_SUSPENDED, &md->flags); 27071da177e4SLinus Torvalds } 27081da177e4SLinus Torvalds 270964dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti) 271064dbce58SKiyoshi Ueda { 2711ecdb2e25SKiyoshi Ueda return dm_suspended_md(dm_table_get_md(ti->table)); 271264dbce58SKiyoshi Ueda } 271364dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended); 271464dbce58SKiyoshi Ueda 27152e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti) 27162e93ccc1SKiyoshi Ueda { 2717ecdb2e25SKiyoshi Ueda return __noflush_suspending(dm_table_get_md(ti->table)); 27182e93ccc1SKiyoshi Ueda } 27192e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending); 27202e93ccc1SKiyoshi Ueda 2721c0820cf5SMikulas Patocka struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity, unsigned per_bio_data_size) 2722e6ee8c0bSKiyoshi Ueda { 2723e6ee8c0bSKiyoshi Ueda struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL); 2724a91a2785SMartin K. Petersen unsigned int pool_size = (type == DM_TYPE_BIO_BASED) ? 16 : MIN_IOS; 2725e6ee8c0bSKiyoshi Ueda 2726e6ee8c0bSKiyoshi Ueda if (!pools) 2727e6ee8c0bSKiyoshi Ueda return NULL; 2728e6ee8c0bSKiyoshi Ueda 2729c0820cf5SMikulas Patocka per_bio_data_size = roundup(per_bio_data_size, __alignof__(struct dm_target_io)); 2730c0820cf5SMikulas Patocka 2731e6ee8c0bSKiyoshi Ueda pools->io_pool = (type == DM_TYPE_BIO_BASED) ? 2732e6ee8c0bSKiyoshi Ueda mempool_create_slab_pool(MIN_IOS, _io_cache) : 2733e6ee8c0bSKiyoshi Ueda mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache); 2734e6ee8c0bSKiyoshi Ueda if (!pools->io_pool) 2735e6ee8c0bSKiyoshi Ueda goto free_pools_and_out; 2736e6ee8c0bSKiyoshi Ueda 2737dba14160SMikulas Patocka pools->tio_pool = NULL; 2738dba14160SMikulas Patocka if (type == DM_TYPE_REQUEST_BASED) { 2739dba14160SMikulas Patocka pools->tio_pool = mempool_create_slab_pool(MIN_IOS, _rq_tio_cache); 2740e6ee8c0bSKiyoshi Ueda if (!pools->tio_pool) 2741e6ee8c0bSKiyoshi Ueda goto free_io_pool_and_out; 2742dba14160SMikulas Patocka } 2743e6ee8c0bSKiyoshi Ueda 274494818742SKent Overstreet pools->bs = (type == DM_TYPE_BIO_BASED) ? 2745dba14160SMikulas Patocka bioset_create(pool_size, 2746c0820cf5SMikulas Patocka per_bio_data_size + offsetof(struct dm_target_io, clone)) : 274794818742SKent Overstreet bioset_create(pool_size, 274894818742SKent Overstreet offsetof(struct dm_rq_clone_bio_info, clone)); 2749e6ee8c0bSKiyoshi Ueda if (!pools->bs) 2750e6ee8c0bSKiyoshi Ueda goto free_tio_pool_and_out; 2751e6ee8c0bSKiyoshi Ueda 2752a91a2785SMartin K. Petersen if (integrity && bioset_integrity_create(pools->bs, pool_size)) 2753a91a2785SMartin K. Petersen goto free_bioset_and_out; 2754a91a2785SMartin K. Petersen 2755e6ee8c0bSKiyoshi Ueda return pools; 2756e6ee8c0bSKiyoshi Ueda 2757a91a2785SMartin K. Petersen free_bioset_and_out: 2758a91a2785SMartin K. Petersen bioset_free(pools->bs); 2759a91a2785SMartin K. Petersen 2760e6ee8c0bSKiyoshi Ueda free_tio_pool_and_out: 2761dba14160SMikulas Patocka if (pools->tio_pool) 2762e6ee8c0bSKiyoshi Ueda mempool_destroy(pools->tio_pool); 2763e6ee8c0bSKiyoshi Ueda 2764e6ee8c0bSKiyoshi Ueda free_io_pool_and_out: 2765e6ee8c0bSKiyoshi Ueda mempool_destroy(pools->io_pool); 2766e6ee8c0bSKiyoshi Ueda 2767e6ee8c0bSKiyoshi Ueda free_pools_and_out: 2768e6ee8c0bSKiyoshi Ueda kfree(pools); 2769e6ee8c0bSKiyoshi Ueda 2770e6ee8c0bSKiyoshi Ueda return NULL; 2771e6ee8c0bSKiyoshi Ueda } 2772e6ee8c0bSKiyoshi Ueda 2773e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools) 2774e6ee8c0bSKiyoshi Ueda { 2775e6ee8c0bSKiyoshi Ueda if (!pools) 2776e6ee8c0bSKiyoshi Ueda return; 2777e6ee8c0bSKiyoshi Ueda 2778e6ee8c0bSKiyoshi Ueda if (pools->io_pool) 2779e6ee8c0bSKiyoshi Ueda mempool_destroy(pools->io_pool); 2780e6ee8c0bSKiyoshi Ueda 2781e6ee8c0bSKiyoshi Ueda if (pools->tio_pool) 2782e6ee8c0bSKiyoshi Ueda mempool_destroy(pools->tio_pool); 2783e6ee8c0bSKiyoshi Ueda 2784e6ee8c0bSKiyoshi Ueda if (pools->bs) 2785e6ee8c0bSKiyoshi Ueda bioset_free(pools->bs); 2786e6ee8c0bSKiyoshi Ueda 2787e6ee8c0bSKiyoshi Ueda kfree(pools); 2788e6ee8c0bSKiyoshi Ueda } 2789e6ee8c0bSKiyoshi Ueda 279083d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = { 27911da177e4SLinus Torvalds .open = dm_blk_open, 27921da177e4SLinus Torvalds .release = dm_blk_close, 2793aa129a22SMilan Broz .ioctl = dm_blk_ioctl, 27943ac51e74SDarrick J. Wong .getgeo = dm_blk_getgeo, 27951da177e4SLinus Torvalds .owner = THIS_MODULE 27961da177e4SLinus Torvalds }; 27971da177e4SLinus Torvalds 27981da177e4SLinus Torvalds EXPORT_SYMBOL(dm_get_mapinfo); 27991da177e4SLinus Torvalds 28001da177e4SLinus Torvalds /* 28011da177e4SLinus Torvalds * module hooks 28021da177e4SLinus Torvalds */ 28031da177e4SLinus Torvalds module_init(dm_init); 28041da177e4SLinus Torvalds module_exit(dm_exit); 28051da177e4SLinus Torvalds 28061da177e4SLinus Torvalds module_param(major, uint, 0); 28071da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper"); 28081da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver"); 28091da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 28101da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 2811