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> 22ffcc3936SMike Snitzer #include <linux/wait.h> 232eb6e1e3SKeith Busch #include <linux/kthread.h> 240ce65797SMike Snitzer #include <linux/ktime.h> 25de3ec86dSMike Snitzer #include <linux/elevator.h> /* for rq_end_sector() */ 26bfebd1cdSMike Snitzer #include <linux/blk-mq.h> 2755782138SLi Zefan 2855782138SLi Zefan #include <trace/events/block.h> 291da177e4SLinus Torvalds 3072d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core" 3172d94861SAlasdair G Kergon 3271a16736SNamhyung Kim #ifdef CONFIG_PRINTK 3371a16736SNamhyung Kim /* 3471a16736SNamhyung Kim * ratelimit state to be used in DMXXX_LIMIT(). 3571a16736SNamhyung Kim */ 3671a16736SNamhyung Kim DEFINE_RATELIMIT_STATE(dm_ratelimit_state, 3771a16736SNamhyung Kim DEFAULT_RATELIMIT_INTERVAL, 3871a16736SNamhyung Kim DEFAULT_RATELIMIT_BURST); 3971a16736SNamhyung Kim EXPORT_SYMBOL(dm_ratelimit_state); 4071a16736SNamhyung Kim #endif 4171a16736SNamhyung Kim 4260935eb2SMilan Broz /* 4360935eb2SMilan Broz * Cookies are numeric values sent with CHANGE and REMOVE 4460935eb2SMilan Broz * uevents while resuming, removing or renaming the device. 4560935eb2SMilan Broz */ 4660935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE" 4760935eb2SMilan Broz #define DM_COOKIE_LENGTH 24 4860935eb2SMilan Broz 491da177e4SLinus Torvalds static const char *_name = DM_NAME; 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds static unsigned int major = 0; 521da177e4SLinus Torvalds static unsigned int _major = 0; 531da177e4SLinus Torvalds 54d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr); 55d15b774cSAlasdair G Kergon 56f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock); 572c140a24SMikulas Patocka 582c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w); 592c140a24SMikulas Patocka 602c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove); 612c140a24SMikulas Patocka 62acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue; 63acfe0ad7SMikulas Patocka 641da177e4SLinus Torvalds /* 658fbf26adSKiyoshi Ueda * For bio-based dm. 661da177e4SLinus Torvalds * One of these is allocated per bio. 671da177e4SLinus Torvalds */ 681da177e4SLinus Torvalds struct dm_io { 691da177e4SLinus Torvalds struct mapped_device *md; 701da177e4SLinus Torvalds int error; 711da177e4SLinus Torvalds atomic_t io_count; 726ae2fa67SRichard Kennedy struct bio *bio; 733eaf840eSJun'ichi "Nick" Nomura unsigned long start_time; 74f88fb981SKiyoshi Ueda spinlock_t endio_lock; 75fd2ed4d2SMikulas Patocka struct dm_stats_aux stats_aux; 761da177e4SLinus Torvalds }; 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds /* 798fbf26adSKiyoshi Ueda * For request-based dm. 808fbf26adSKiyoshi Ueda * One of these is allocated per request. 818fbf26adSKiyoshi Ueda */ 828fbf26adSKiyoshi Ueda struct dm_rq_target_io { 838fbf26adSKiyoshi Ueda struct mapped_device *md; 848fbf26adSKiyoshi Ueda struct dm_target *ti; 851ae49ea2SMike Snitzer struct request *orig, *clone; 862eb6e1e3SKeith Busch struct kthread_work work; 878fbf26adSKiyoshi Ueda int error; 888fbf26adSKiyoshi Ueda union map_info info; 898fbf26adSKiyoshi Ueda }; 908fbf26adSKiyoshi Ueda 918fbf26adSKiyoshi Ueda /* 9294818742SKent Overstreet * For request-based dm - the bio clones we allocate are embedded in these 9394818742SKent Overstreet * structs. 9494818742SKent Overstreet * 9594818742SKent Overstreet * We allocate these with bio_alloc_bioset, using the front_pad parameter when 9694818742SKent Overstreet * the bioset is created - this means the bio has to come at the end of the 9794818742SKent Overstreet * struct. 988fbf26adSKiyoshi Ueda */ 998fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info { 1008fbf26adSKiyoshi Ueda struct bio *orig; 101cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio; 10294818742SKent Overstreet struct bio clone; 1038fbf26adSKiyoshi Ueda }; 1048fbf26adSKiyoshi Ueda 105cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq) 106cec47e3dSKiyoshi Ueda { 107cec47e3dSKiyoshi Ueda if (rq && rq->end_io_data) 108cec47e3dSKiyoshi Ueda return &((struct dm_rq_target_io *)rq->end_io_data)->info; 109cec47e3dSKiyoshi Ueda return NULL; 110cec47e3dSKiyoshi Ueda } 111cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo); 112cec47e3dSKiyoshi Ueda 113ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1) 114ba61fdd1SJeff Mahoney 1151da177e4SLinus Torvalds /* 1161da177e4SLinus Torvalds * Bits for the md->flags field. 1171da177e4SLinus Torvalds */ 1181eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0 1191da177e4SLinus Torvalds #define DMF_SUSPENDED 1 120aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2 121fba9f90eSJeff Mahoney #define DMF_FREEING 3 1225c6bd75dSAlasdair G Kergon #define DMF_DELETING 4 1232e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5 124d5b9dd04SMikulas Patocka #define DMF_MERGE_IS_OPTIONAL 6 1252c140a24SMikulas Patocka #define DMF_DEFERRED_REMOVE 7 126ffcc3936SMike Snitzer #define DMF_SUSPENDED_INTERNALLY 8 1271da177e4SLinus Torvalds 128304f3f6aSMilan Broz /* 12983d5e5b0SMikulas Patocka * A dummy definition to make RCU happy. 13083d5e5b0SMikulas Patocka * struct dm_table should never be dereferenced in this file. 13183d5e5b0SMikulas Patocka */ 13283d5e5b0SMikulas Patocka struct dm_table { 13383d5e5b0SMikulas Patocka int undefined__; 13483d5e5b0SMikulas Patocka }; 13583d5e5b0SMikulas Patocka 13683d5e5b0SMikulas Patocka /* 137304f3f6aSMilan Broz * Work processed by per-device workqueue. 138304f3f6aSMilan Broz */ 1391da177e4SLinus Torvalds struct mapped_device { 14083d5e5b0SMikulas Patocka struct srcu_struct io_barrier; 141e61290a4SDaniel Walker struct mutex suspend_lock; 1421da177e4SLinus Torvalds atomic_t holders; 1435c6bd75dSAlasdair G Kergon atomic_t open_count; 1441da177e4SLinus Torvalds 1452a7faeb1SMikulas Patocka /* 1462a7faeb1SMikulas Patocka * The current mapping. 1472a7faeb1SMikulas Patocka * Use dm_get_live_table{_fast} or take suspend_lock for 1482a7faeb1SMikulas Patocka * dereference. 1492a7faeb1SMikulas Patocka */ 1506fa99520SPranith Kumar struct dm_table __rcu *map; 1512a7faeb1SMikulas Patocka 15286f1152bSBenjamin Marzinski struct list_head table_devices; 15386f1152bSBenjamin Marzinski struct mutex table_devices_lock; 15486f1152bSBenjamin Marzinski 1551da177e4SLinus Torvalds unsigned long flags; 1561da177e4SLinus Torvalds 157165125e1SJens Axboe struct request_queue *queue; 158a5664dadSMike Snitzer unsigned type; 1594a0b4ddfSMike Snitzer /* Protect queue and type against concurrent access. */ 160a5664dadSMike Snitzer struct mutex type_lock; 161a5664dadSMike Snitzer 16236a0456fSAlasdair G Kergon struct target_type *immutable_target_type; 16336a0456fSAlasdair G Kergon 1641da177e4SLinus Torvalds struct gendisk *disk; 1657e51f257SMike Anderson char name[16]; 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds void *interface_ptr; 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds /* 1701da177e4SLinus Torvalds * A list of ios that arrived while we were suspended. 1711da177e4SLinus Torvalds */ 172316d315bSNikanth Karthikesan atomic_t pending[2]; 1731da177e4SLinus Torvalds wait_queue_head_t wait; 17453d5914fSMikulas Patocka struct work_struct work; 1751da177e4SLinus Torvalds struct bio_list deferred; 176022c2611SMikulas Patocka spinlock_t deferred_lock; 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds /* 17929e4013dSTejun Heo * Processing queue (flush) 180304f3f6aSMilan Broz */ 181304f3f6aSMilan Broz struct workqueue_struct *wq; 182304f3f6aSMilan Broz 183304f3f6aSMilan Broz /* 1841da177e4SLinus Torvalds * io objects are allocated from here. 1851da177e4SLinus Torvalds */ 1861da177e4SLinus Torvalds mempool_t *io_pool; 1871ae49ea2SMike Snitzer mempool_t *rq_pool; 1881da177e4SLinus Torvalds 1899faf400fSStefan Bader struct bio_set *bs; 1909faf400fSStefan Bader 1911da177e4SLinus Torvalds /* 1921da177e4SLinus Torvalds * Event handling. 1931da177e4SLinus Torvalds */ 1941da177e4SLinus Torvalds atomic_t event_nr; 1951da177e4SLinus Torvalds wait_queue_head_t eventq; 1967a8c3d3bSMike Anderson atomic_t uevent_seq; 1977a8c3d3bSMike Anderson struct list_head uevent_list; 1987a8c3d3bSMike Anderson spinlock_t uevent_lock; /* Protect access to uevent_list */ 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds /* 2011da177e4SLinus Torvalds * freeze/thaw support require holding onto a super block 2021da177e4SLinus Torvalds */ 2031da177e4SLinus Torvalds struct super_block *frozen_sb; 204db8fef4fSMikulas Patocka struct block_device *bdev; 2053ac51e74SDarrick J. Wong 2063ac51e74SDarrick J. Wong /* forced geometry settings */ 2073ac51e74SDarrick J. Wong struct hd_geometry geometry; 208784aae73SMilan Broz 2092995fa78SMikulas Patocka /* kobject and completion */ 2102995fa78SMikulas Patocka struct dm_kobject_holder kobj_holder; 211be35f486SMikulas Patocka 212d87f4c14STejun Heo /* zero-length flush that will be cloned and submitted to targets */ 213d87f4c14STejun Heo struct bio flush_bio; 214fd2ed4d2SMikulas Patocka 21596b26c8cSMikulas Patocka /* the number of internal suspends */ 21696b26c8cSMikulas Patocka unsigned internal_suspend_count; 21796b26c8cSMikulas Patocka 218fd2ed4d2SMikulas Patocka struct dm_stats stats; 2192eb6e1e3SKeith Busch 2202eb6e1e3SKeith Busch struct kthread_worker kworker; 2212eb6e1e3SKeith Busch struct task_struct *kworker_task; 222de3ec86dSMike Snitzer 223de3ec86dSMike Snitzer /* for request-based merge heuristic in dm_request_fn() */ 2240ce65797SMike Snitzer unsigned seq_rq_merge_deadline_usecs; 225de3ec86dSMike Snitzer int last_rq_rw; 2260ce65797SMike Snitzer sector_t last_rq_pos; 2270ce65797SMike Snitzer ktime_t last_rq_start_time; 228bfebd1cdSMike Snitzer 229bfebd1cdSMike Snitzer /* for blk-mq request-based DM support */ 230bfebd1cdSMike Snitzer struct blk_mq_tag_set tag_set; 23117e149b8SMike Snitzer bool use_blk_mq; 2321da177e4SLinus Torvalds }; 2331da177e4SLinus Torvalds 23417e149b8SMike Snitzer #ifdef CONFIG_DM_MQ_DEFAULT 23517e149b8SMike Snitzer static bool use_blk_mq = true; 23617e149b8SMike Snitzer #else 23717e149b8SMike Snitzer static bool use_blk_mq = false; 23817e149b8SMike Snitzer #endif 23917e149b8SMike Snitzer 24017e149b8SMike Snitzer bool dm_use_blk_mq(struct mapped_device *md) 24117e149b8SMike Snitzer { 24217e149b8SMike Snitzer return md->use_blk_mq; 24317e149b8SMike Snitzer } 24417e149b8SMike Snitzer 245e6ee8c0bSKiyoshi Ueda /* 246e6ee8c0bSKiyoshi Ueda * For mempools pre-allocation at the table loading time. 247e6ee8c0bSKiyoshi Ueda */ 248e6ee8c0bSKiyoshi Ueda struct dm_md_mempools { 249e6ee8c0bSKiyoshi Ueda mempool_t *io_pool; 2501ae49ea2SMike Snitzer mempool_t *rq_pool; 251e6ee8c0bSKiyoshi Ueda struct bio_set *bs; 252e6ee8c0bSKiyoshi Ueda }; 253e6ee8c0bSKiyoshi Ueda 25486f1152bSBenjamin Marzinski struct table_device { 25586f1152bSBenjamin Marzinski struct list_head list; 25686f1152bSBenjamin Marzinski atomic_t count; 25786f1152bSBenjamin Marzinski struct dm_dev dm_dev; 25886f1152bSBenjamin Marzinski }; 25986f1152bSBenjamin Marzinski 2606cfa5857SMike Snitzer #define RESERVED_BIO_BASED_IOS 16 2616cfa5857SMike Snitzer #define RESERVED_REQUEST_BASED_IOS 256 262f4790826SMike Snitzer #define RESERVED_MAX_IOS 1024 263e18b890bSChristoph Lameter static struct kmem_cache *_io_cache; 2648fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache; 2651ae49ea2SMike Snitzer static struct kmem_cache *_rq_cache; 26694818742SKent Overstreet 267f4790826SMike Snitzer /* 268e8603136SMike Snitzer * Bio-based DM's mempools' reserved IOs set by the user. 269e8603136SMike Snitzer */ 270e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS; 271e8603136SMike Snitzer 272e8603136SMike Snitzer /* 273f4790826SMike Snitzer * Request-based DM's mempools' reserved IOs set by the user. 274f4790826SMike Snitzer */ 275f4790826SMike Snitzer static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS; 276f4790826SMike Snitzer 27709c2d531SMike Snitzer static unsigned __dm_get_module_param(unsigned *module_param, 278f4790826SMike Snitzer unsigned def, unsigned max) 279f4790826SMike Snitzer { 28009c2d531SMike Snitzer unsigned param = ACCESS_ONCE(*module_param); 28109c2d531SMike Snitzer unsigned modified_param = 0; 282f4790826SMike Snitzer 28309c2d531SMike Snitzer if (!param) 28409c2d531SMike Snitzer modified_param = def; 28509c2d531SMike Snitzer else if (param > max) 28609c2d531SMike Snitzer modified_param = max; 287f4790826SMike Snitzer 28809c2d531SMike Snitzer if (modified_param) { 28909c2d531SMike Snitzer (void)cmpxchg(module_param, param, modified_param); 29009c2d531SMike Snitzer param = modified_param; 291f4790826SMike Snitzer } 292f4790826SMike Snitzer 29309c2d531SMike Snitzer return param; 294f4790826SMike Snitzer } 295f4790826SMike Snitzer 296e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void) 297e8603136SMike Snitzer { 29809c2d531SMike Snitzer return __dm_get_module_param(&reserved_bio_based_ios, 299e8603136SMike Snitzer RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS); 300e8603136SMike Snitzer } 301e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios); 302e8603136SMike Snitzer 303f4790826SMike Snitzer unsigned dm_get_reserved_rq_based_ios(void) 304f4790826SMike Snitzer { 30509c2d531SMike Snitzer return __dm_get_module_param(&reserved_rq_based_ios, 306f4790826SMike Snitzer RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS); 307f4790826SMike Snitzer } 308f4790826SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios); 309f4790826SMike Snitzer 3101da177e4SLinus Torvalds static int __init local_init(void) 3111da177e4SLinus Torvalds { 31251157b4aSKiyoshi Ueda int r = -ENOMEM; 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds /* allocate a slab for the dm_ios */ 315028867acSAlasdair G Kergon _io_cache = KMEM_CACHE(dm_io, 0); 3161da177e4SLinus Torvalds if (!_io_cache) 31751157b4aSKiyoshi Ueda return r; 3181da177e4SLinus Torvalds 3198fbf26adSKiyoshi Ueda _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0); 3208fbf26adSKiyoshi Ueda if (!_rq_tio_cache) 321dba14160SMikulas Patocka goto out_free_io_cache; 3228fbf26adSKiyoshi Ueda 3231ae49ea2SMike Snitzer _rq_cache = kmem_cache_create("dm_clone_request", sizeof(struct request), 3241ae49ea2SMike Snitzer __alignof__(struct request), 0, NULL); 3251ae49ea2SMike Snitzer if (!_rq_cache) 3261ae49ea2SMike Snitzer goto out_free_rq_tio_cache; 3271ae49ea2SMike Snitzer 32851e5b2bdSMike Anderson r = dm_uevent_init(); 32951157b4aSKiyoshi Ueda if (r) 3301ae49ea2SMike Snitzer goto out_free_rq_cache; 33151e5b2bdSMike Anderson 332acfe0ad7SMikulas Patocka deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1); 333acfe0ad7SMikulas Patocka if (!deferred_remove_workqueue) { 334acfe0ad7SMikulas Patocka r = -ENOMEM; 335acfe0ad7SMikulas Patocka goto out_uevent_exit; 336acfe0ad7SMikulas Patocka } 337acfe0ad7SMikulas Patocka 3381da177e4SLinus Torvalds _major = major; 3391da177e4SLinus Torvalds r = register_blkdev(_major, _name); 34051157b4aSKiyoshi Ueda if (r < 0) 341acfe0ad7SMikulas Patocka goto out_free_workqueue; 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds if (!_major) 3441da177e4SLinus Torvalds _major = r; 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds return 0; 34751157b4aSKiyoshi Ueda 348acfe0ad7SMikulas Patocka out_free_workqueue: 349acfe0ad7SMikulas Patocka destroy_workqueue(deferred_remove_workqueue); 35051157b4aSKiyoshi Ueda out_uevent_exit: 35151157b4aSKiyoshi Ueda dm_uevent_exit(); 3521ae49ea2SMike Snitzer out_free_rq_cache: 3531ae49ea2SMike Snitzer kmem_cache_destroy(_rq_cache); 3548fbf26adSKiyoshi Ueda out_free_rq_tio_cache: 3558fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_tio_cache); 35651157b4aSKiyoshi Ueda out_free_io_cache: 35751157b4aSKiyoshi Ueda kmem_cache_destroy(_io_cache); 35851157b4aSKiyoshi Ueda 35951157b4aSKiyoshi Ueda return r; 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds static void local_exit(void) 3631da177e4SLinus Torvalds { 3642c140a24SMikulas Patocka flush_scheduled_work(); 365acfe0ad7SMikulas Patocka destroy_workqueue(deferred_remove_workqueue); 3662c140a24SMikulas Patocka 3671ae49ea2SMike Snitzer kmem_cache_destroy(_rq_cache); 3688fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_tio_cache); 3691da177e4SLinus Torvalds kmem_cache_destroy(_io_cache); 37000d59405SAkinobu Mita unregister_blkdev(_major, _name); 37151e5b2bdSMike Anderson dm_uevent_exit(); 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds _major = 0; 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds DMINFO("cleaned up"); 3761da177e4SLinus Torvalds } 3771da177e4SLinus Torvalds 378b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = { 3791da177e4SLinus Torvalds local_init, 3801da177e4SLinus Torvalds dm_target_init, 3811da177e4SLinus Torvalds dm_linear_init, 3821da177e4SLinus Torvalds dm_stripe_init, 383952b3557SMikulas Patocka dm_io_init, 384945fa4d2SMikulas Patocka dm_kcopyd_init, 3851da177e4SLinus Torvalds dm_interface_init, 386fd2ed4d2SMikulas Patocka dm_statistics_init, 3871da177e4SLinus Torvalds }; 3881da177e4SLinus Torvalds 389b9249e55SAlasdair G Kergon static void (*_exits[])(void) = { 3901da177e4SLinus Torvalds local_exit, 3911da177e4SLinus Torvalds dm_target_exit, 3921da177e4SLinus Torvalds dm_linear_exit, 3931da177e4SLinus Torvalds dm_stripe_exit, 394952b3557SMikulas Patocka dm_io_exit, 395945fa4d2SMikulas Patocka dm_kcopyd_exit, 3961da177e4SLinus Torvalds dm_interface_exit, 397fd2ed4d2SMikulas Patocka dm_statistics_exit, 3981da177e4SLinus Torvalds }; 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds static int __init dm_init(void) 4011da177e4SLinus Torvalds { 4021da177e4SLinus Torvalds const int count = ARRAY_SIZE(_inits); 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds int r, i; 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds for (i = 0; i < count; i++) { 4071da177e4SLinus Torvalds r = _inits[i](); 4081da177e4SLinus Torvalds if (r) 4091da177e4SLinus Torvalds goto bad; 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds return 0; 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds bad: 4151da177e4SLinus Torvalds while (i--) 4161da177e4SLinus Torvalds _exits[i](); 4171da177e4SLinus Torvalds 4181da177e4SLinus Torvalds return r; 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds static void __exit dm_exit(void) 4221da177e4SLinus Torvalds { 4231da177e4SLinus Torvalds int i = ARRAY_SIZE(_exits); 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds while (i--) 4261da177e4SLinus Torvalds _exits[i](); 427d15b774cSAlasdair G Kergon 428d15b774cSAlasdair G Kergon /* 429d15b774cSAlasdair G Kergon * Should be empty by this point. 430d15b774cSAlasdair G Kergon */ 431d15b774cSAlasdair G Kergon idr_destroy(&_minor_idr); 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds /* 4351da177e4SLinus Torvalds * Block device functions 4361da177e4SLinus Torvalds */ 437432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md) 438432a212cSMike Anderson { 439432a212cSMike Anderson return test_bit(DMF_DELETING, &md->flags); 440432a212cSMike Anderson } 441432a212cSMike Anderson 442fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode) 4431da177e4SLinus Torvalds { 4441da177e4SLinus Torvalds struct mapped_device *md; 4451da177e4SLinus Torvalds 446fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 447fba9f90eSJeff Mahoney 448fe5f9f2cSAl Viro md = bdev->bd_disk->private_data; 449fba9f90eSJeff Mahoney if (!md) 450fba9f90eSJeff Mahoney goto out; 451fba9f90eSJeff Mahoney 4525c6bd75dSAlasdair G Kergon if (test_bit(DMF_FREEING, &md->flags) || 453432a212cSMike Anderson dm_deleting_md(md)) { 454fba9f90eSJeff Mahoney md = NULL; 455fba9f90eSJeff Mahoney goto out; 456fba9f90eSJeff Mahoney } 457fba9f90eSJeff Mahoney 4581da177e4SLinus Torvalds dm_get(md); 4595c6bd75dSAlasdair G Kergon atomic_inc(&md->open_count); 460fba9f90eSJeff Mahoney out: 461fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 462fba9f90eSJeff Mahoney 463fba9f90eSJeff Mahoney return md ? 0 : -ENXIO; 4641da177e4SLinus Torvalds } 4651da177e4SLinus Torvalds 466db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode) 4671da177e4SLinus Torvalds { 46863a4f065SMike Snitzer struct mapped_device *md; 4696e9624b8SArnd Bergmann 4704a1aeb98SMilan Broz spin_lock(&_minor_lock); 4714a1aeb98SMilan Broz 47263a4f065SMike Snitzer md = disk->private_data; 47363a4f065SMike Snitzer if (WARN_ON(!md)) 47463a4f065SMike Snitzer goto out; 47563a4f065SMike Snitzer 4762c140a24SMikulas Patocka if (atomic_dec_and_test(&md->open_count) && 4772c140a24SMikulas Patocka (test_bit(DMF_DEFERRED_REMOVE, &md->flags))) 478acfe0ad7SMikulas Patocka queue_work(deferred_remove_workqueue, &deferred_remove_work); 4792c140a24SMikulas Patocka 4801da177e4SLinus Torvalds dm_put(md); 48163a4f065SMike Snitzer out: 4824a1aeb98SMilan Broz spin_unlock(&_minor_lock); 4831da177e4SLinus Torvalds } 4841da177e4SLinus Torvalds 4855c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md) 4865c6bd75dSAlasdair G Kergon { 4875c6bd75dSAlasdair G Kergon return atomic_read(&md->open_count); 4885c6bd75dSAlasdair G Kergon } 4895c6bd75dSAlasdair G Kergon 4905c6bd75dSAlasdair G Kergon /* 4915c6bd75dSAlasdair G Kergon * Guarantees nothing is using the device before it's deleted. 4925c6bd75dSAlasdair G Kergon */ 4932c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred) 4945c6bd75dSAlasdair G Kergon { 4955c6bd75dSAlasdair G Kergon int r = 0; 4965c6bd75dSAlasdair G Kergon 4975c6bd75dSAlasdair G Kergon spin_lock(&_minor_lock); 4985c6bd75dSAlasdair G Kergon 4992c140a24SMikulas Patocka if (dm_open_count(md)) { 5005c6bd75dSAlasdair G Kergon r = -EBUSY; 5012c140a24SMikulas Patocka if (mark_deferred) 5022c140a24SMikulas Patocka set_bit(DMF_DEFERRED_REMOVE, &md->flags); 5032c140a24SMikulas Patocka } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags)) 5042c140a24SMikulas Patocka r = -EEXIST; 5055c6bd75dSAlasdair G Kergon else 5065c6bd75dSAlasdair G Kergon set_bit(DMF_DELETING, &md->flags); 5075c6bd75dSAlasdair G Kergon 5085c6bd75dSAlasdair G Kergon spin_unlock(&_minor_lock); 5095c6bd75dSAlasdair G Kergon 5105c6bd75dSAlasdair G Kergon return r; 5115c6bd75dSAlasdair G Kergon } 5125c6bd75dSAlasdair G Kergon 5132c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md) 5142c140a24SMikulas Patocka { 5152c140a24SMikulas Patocka int r = 0; 5162c140a24SMikulas Patocka 5172c140a24SMikulas Patocka spin_lock(&_minor_lock); 5182c140a24SMikulas Patocka 5192c140a24SMikulas Patocka if (test_bit(DMF_DELETING, &md->flags)) 5202c140a24SMikulas Patocka r = -EBUSY; 5212c140a24SMikulas Patocka else 5222c140a24SMikulas Patocka clear_bit(DMF_DEFERRED_REMOVE, &md->flags); 5232c140a24SMikulas Patocka 5242c140a24SMikulas Patocka spin_unlock(&_minor_lock); 5252c140a24SMikulas Patocka 5262c140a24SMikulas Patocka return r; 5272c140a24SMikulas Patocka } 5282c140a24SMikulas Patocka 5292c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w) 5302c140a24SMikulas Patocka { 5312c140a24SMikulas Patocka dm_deferred_remove(); 5322c140a24SMikulas Patocka } 5332c140a24SMikulas Patocka 534fd2ed4d2SMikulas Patocka sector_t dm_get_size(struct mapped_device *md) 535fd2ed4d2SMikulas Patocka { 536fd2ed4d2SMikulas Patocka return get_capacity(md->disk); 537fd2ed4d2SMikulas Patocka } 538fd2ed4d2SMikulas Patocka 5399974fa2cSMike Snitzer struct request_queue *dm_get_md_queue(struct mapped_device *md) 5409974fa2cSMike Snitzer { 5419974fa2cSMike Snitzer return md->queue; 5429974fa2cSMike Snitzer } 5439974fa2cSMike Snitzer 544fd2ed4d2SMikulas Patocka struct dm_stats *dm_get_stats(struct mapped_device *md) 545fd2ed4d2SMikulas Patocka { 546fd2ed4d2SMikulas Patocka return &md->stats; 547fd2ed4d2SMikulas Patocka } 548fd2ed4d2SMikulas Patocka 5493ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 5503ac51e74SDarrick J. Wong { 5513ac51e74SDarrick J. Wong struct mapped_device *md = bdev->bd_disk->private_data; 5523ac51e74SDarrick J. Wong 5533ac51e74SDarrick J. Wong return dm_get_geometry(md, geo); 5543ac51e74SDarrick J. Wong } 5553ac51e74SDarrick J. Wong 556fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode, 557aa129a22SMilan Broz unsigned int cmd, unsigned long arg) 558aa129a22SMilan Broz { 559fe5f9f2cSAl Viro struct mapped_device *md = bdev->bd_disk->private_data; 56083d5e5b0SMikulas Patocka int srcu_idx; 5616c182cd8SHannes Reinecke struct dm_table *map; 562aa129a22SMilan Broz struct dm_target *tgt; 563aa129a22SMilan Broz int r = -ENOTTY; 564aa129a22SMilan Broz 5656c182cd8SHannes Reinecke retry: 56683d5e5b0SMikulas Patocka map = dm_get_live_table(md, &srcu_idx); 56783d5e5b0SMikulas Patocka 568aa129a22SMilan Broz if (!map || !dm_table_get_size(map)) 569aa129a22SMilan Broz goto out; 570aa129a22SMilan Broz 571aa129a22SMilan Broz /* We only support devices that have a single target */ 572aa129a22SMilan Broz if (dm_table_get_num_targets(map) != 1) 573aa129a22SMilan Broz goto out; 574aa129a22SMilan Broz 575aa129a22SMilan Broz tgt = dm_table_get_target(map, 0); 5764d341d82SMike Snitzer if (!tgt->type->ioctl) 5774d341d82SMike Snitzer goto out; 578aa129a22SMilan Broz 5794f186f8bSKiyoshi Ueda if (dm_suspended_md(md)) { 580aa129a22SMilan Broz r = -EAGAIN; 581aa129a22SMilan Broz goto out; 582aa129a22SMilan Broz } 583aa129a22SMilan Broz 584647b3d00SAl Viro r = tgt->type->ioctl(tgt, cmd, arg); 585aa129a22SMilan Broz 586aa129a22SMilan Broz out: 58783d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 588aa129a22SMilan Broz 5896c182cd8SHannes Reinecke if (r == -ENOTCONN) { 5906c182cd8SHannes Reinecke msleep(10); 5916c182cd8SHannes Reinecke goto retry; 5926c182cd8SHannes Reinecke } 5936c182cd8SHannes Reinecke 594aa129a22SMilan Broz return r; 595aa129a22SMilan Broz } 596aa129a22SMilan Broz 597028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md) 5981da177e4SLinus Torvalds { 5991da177e4SLinus Torvalds return mempool_alloc(md->io_pool, GFP_NOIO); 6001da177e4SLinus Torvalds } 6011da177e4SLinus Torvalds 602028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io) 6031da177e4SLinus Torvalds { 6041da177e4SLinus Torvalds mempool_free(io, md->io_pool); 6051da177e4SLinus Torvalds } 6061da177e4SLinus Torvalds 607028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio) 6081da177e4SLinus Torvalds { 609dba14160SMikulas Patocka bio_put(&tio->clone); 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds 61208885643SKiyoshi Ueda static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md, 61308885643SKiyoshi Ueda gfp_t gfp_mask) 614cec47e3dSKiyoshi Ueda { 6155f015204SJun'ichi Nomura return mempool_alloc(md->io_pool, gfp_mask); 616cec47e3dSKiyoshi Ueda } 617cec47e3dSKiyoshi Ueda 618cec47e3dSKiyoshi Ueda static void free_rq_tio(struct dm_rq_target_io *tio) 619cec47e3dSKiyoshi Ueda { 6205f015204SJun'ichi Nomura mempool_free(tio, tio->md->io_pool); 621cec47e3dSKiyoshi Ueda } 622cec47e3dSKiyoshi Ueda 6231ae49ea2SMike Snitzer static struct request *alloc_clone_request(struct mapped_device *md, 6241ae49ea2SMike Snitzer gfp_t gfp_mask) 6251ae49ea2SMike Snitzer { 6261ae49ea2SMike Snitzer return mempool_alloc(md->rq_pool, gfp_mask); 6271ae49ea2SMike Snitzer } 6281ae49ea2SMike Snitzer 6291ae49ea2SMike Snitzer static void free_clone_request(struct mapped_device *md, struct request *rq) 6301ae49ea2SMike Snitzer { 6311ae49ea2SMike Snitzer mempool_free(rq, md->rq_pool); 6321ae49ea2SMike Snitzer } 6331ae49ea2SMike Snitzer 63490abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md) 63590abb8c4SKiyoshi Ueda { 63690abb8c4SKiyoshi Ueda return atomic_read(&md->pending[READ]) + 63790abb8c4SKiyoshi Ueda atomic_read(&md->pending[WRITE]); 63890abb8c4SKiyoshi Ueda } 63990abb8c4SKiyoshi Ueda 6403eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io) 6413eaf840eSJun'ichi "Nick" Nomura { 6423eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 643fd2ed4d2SMikulas Patocka struct bio *bio = io->bio; 644c9959059STejun Heo int cpu; 645fd2ed4d2SMikulas Patocka int rw = bio_data_dir(bio); 6463eaf840eSJun'ichi "Nick" Nomura 6473eaf840eSJun'ichi "Nick" Nomura io->start_time = jiffies; 6483eaf840eSJun'ichi "Nick" Nomura 649074a7acaSTejun Heo cpu = part_stat_lock(); 650074a7acaSTejun Heo part_round_stats(cpu, &dm_disk(md)->part0); 651074a7acaSTejun Heo part_stat_unlock(); 6521e9bb880SShaohua Li atomic_set(&dm_disk(md)->part0.in_flight[rw], 6531e9bb880SShaohua Li atomic_inc_return(&md->pending[rw])); 654fd2ed4d2SMikulas Patocka 655fd2ed4d2SMikulas Patocka if (unlikely(dm_stats_used(&md->stats))) 6564f024f37SKent Overstreet dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector, 657fd2ed4d2SMikulas Patocka bio_sectors(bio), false, 0, &io->stats_aux); 6583eaf840eSJun'ichi "Nick" Nomura } 6593eaf840eSJun'ichi "Nick" Nomura 660d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io) 6613eaf840eSJun'ichi "Nick" Nomura { 6623eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 6633eaf840eSJun'ichi "Nick" Nomura struct bio *bio = io->bio; 6643eaf840eSJun'ichi "Nick" Nomura unsigned long duration = jiffies - io->start_time; 66518c0b223SGu Zheng int pending; 6663eaf840eSJun'ichi "Nick" Nomura int rw = bio_data_dir(bio); 6673eaf840eSJun'ichi "Nick" Nomura 66818c0b223SGu Zheng generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time); 6693eaf840eSJun'ichi "Nick" Nomura 670fd2ed4d2SMikulas Patocka if (unlikely(dm_stats_used(&md->stats))) 6714f024f37SKent Overstreet dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector, 672fd2ed4d2SMikulas Patocka bio_sectors(bio), true, duration, &io->stats_aux); 673fd2ed4d2SMikulas Patocka 674af7e466aSMikulas Patocka /* 675af7e466aSMikulas Patocka * After this is decremented the bio must not be touched if it is 676d87f4c14STejun Heo * a flush. 677af7e466aSMikulas Patocka */ 6781e9bb880SShaohua Li pending = atomic_dec_return(&md->pending[rw]); 6791e9bb880SShaohua Li atomic_set(&dm_disk(md)->part0.in_flight[rw], pending); 680316d315bSNikanth Karthikesan pending += atomic_read(&md->pending[rw^0x1]); 6813eaf840eSJun'ichi "Nick" Nomura 682d221d2e7SMikulas Patocka /* nudge anyone waiting on suspend queue */ 683d221d2e7SMikulas Patocka if (!pending) 684d221d2e7SMikulas Patocka wake_up(&md->wait); 6853eaf840eSJun'ichi "Nick" Nomura } 6863eaf840eSJun'ichi "Nick" Nomura 6871da177e4SLinus Torvalds /* 6881da177e4SLinus Torvalds * Add the bio to the list of deferred io. 6891da177e4SLinus Torvalds */ 69092c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio) 6911da177e4SLinus Torvalds { 69205447420SKiyoshi Ueda unsigned long flags; 6931da177e4SLinus Torvalds 69405447420SKiyoshi Ueda spin_lock_irqsave(&md->deferred_lock, flags); 6951da177e4SLinus Torvalds bio_list_add(&md->deferred, bio); 69605447420SKiyoshi Ueda spin_unlock_irqrestore(&md->deferred_lock, flags); 69792c63902SMikulas Patocka queue_work(md->wq, &md->work); 6981da177e4SLinus Torvalds } 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds /* 7011da177e4SLinus Torvalds * Everyone (including functions in this file), should use this 7021da177e4SLinus Torvalds * function to access the md->map field, and make sure they call 70383d5e5b0SMikulas Patocka * dm_put_live_table() when finished. 7041da177e4SLinus Torvalds */ 70583d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier) 7061da177e4SLinus Torvalds { 70783d5e5b0SMikulas Patocka *srcu_idx = srcu_read_lock(&md->io_barrier); 7081da177e4SLinus Torvalds 70983d5e5b0SMikulas Patocka return srcu_dereference(md->map, &md->io_barrier); 71083d5e5b0SMikulas Patocka } 7111da177e4SLinus Torvalds 71283d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier) 71383d5e5b0SMikulas Patocka { 71483d5e5b0SMikulas Patocka srcu_read_unlock(&md->io_barrier, srcu_idx); 71583d5e5b0SMikulas Patocka } 71683d5e5b0SMikulas Patocka 71783d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md) 71883d5e5b0SMikulas Patocka { 71983d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 72083d5e5b0SMikulas Patocka synchronize_rcu_expedited(); 72183d5e5b0SMikulas Patocka } 72283d5e5b0SMikulas Patocka 72383d5e5b0SMikulas Patocka /* 72483d5e5b0SMikulas Patocka * A fast alternative to dm_get_live_table/dm_put_live_table. 72583d5e5b0SMikulas Patocka * The caller must not block between these two functions. 72683d5e5b0SMikulas Patocka */ 72783d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU) 72883d5e5b0SMikulas Patocka { 72983d5e5b0SMikulas Patocka rcu_read_lock(); 73083d5e5b0SMikulas Patocka return rcu_dereference(md->map); 73183d5e5b0SMikulas Patocka } 73283d5e5b0SMikulas Patocka 73383d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU) 73483d5e5b0SMikulas Patocka { 73583d5e5b0SMikulas Patocka rcu_read_unlock(); 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds 7383ac51e74SDarrick J. Wong /* 73986f1152bSBenjamin Marzinski * Open a table device so we can use it as a map destination. 74086f1152bSBenjamin Marzinski */ 74186f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev, 74286f1152bSBenjamin Marzinski struct mapped_device *md) 74386f1152bSBenjamin Marzinski { 74486f1152bSBenjamin Marzinski static char *_claim_ptr = "I belong to device-mapper"; 74586f1152bSBenjamin Marzinski struct block_device *bdev; 74686f1152bSBenjamin Marzinski 74786f1152bSBenjamin Marzinski int r; 74886f1152bSBenjamin Marzinski 74986f1152bSBenjamin Marzinski BUG_ON(td->dm_dev.bdev); 75086f1152bSBenjamin Marzinski 75186f1152bSBenjamin Marzinski bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr); 75286f1152bSBenjamin Marzinski if (IS_ERR(bdev)) 75386f1152bSBenjamin Marzinski return PTR_ERR(bdev); 75486f1152bSBenjamin Marzinski 75586f1152bSBenjamin Marzinski r = bd_link_disk_holder(bdev, dm_disk(md)); 75686f1152bSBenjamin Marzinski if (r) { 75786f1152bSBenjamin Marzinski blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL); 75886f1152bSBenjamin Marzinski return r; 75986f1152bSBenjamin Marzinski } 76086f1152bSBenjamin Marzinski 76186f1152bSBenjamin Marzinski td->dm_dev.bdev = bdev; 76286f1152bSBenjamin Marzinski return 0; 76386f1152bSBenjamin Marzinski } 76486f1152bSBenjamin Marzinski 76586f1152bSBenjamin Marzinski /* 76686f1152bSBenjamin Marzinski * Close a table device that we've been using. 76786f1152bSBenjamin Marzinski */ 76886f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md) 76986f1152bSBenjamin Marzinski { 77086f1152bSBenjamin Marzinski if (!td->dm_dev.bdev) 77186f1152bSBenjamin Marzinski return; 77286f1152bSBenjamin Marzinski 77386f1152bSBenjamin Marzinski bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md)); 77486f1152bSBenjamin Marzinski blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL); 77586f1152bSBenjamin Marzinski td->dm_dev.bdev = NULL; 77686f1152bSBenjamin Marzinski } 77786f1152bSBenjamin Marzinski 77886f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev, 77986f1152bSBenjamin Marzinski fmode_t mode) { 78086f1152bSBenjamin Marzinski struct table_device *td; 78186f1152bSBenjamin Marzinski 78286f1152bSBenjamin Marzinski list_for_each_entry(td, l, list) 78386f1152bSBenjamin Marzinski if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode) 78486f1152bSBenjamin Marzinski return td; 78586f1152bSBenjamin Marzinski 78686f1152bSBenjamin Marzinski return NULL; 78786f1152bSBenjamin Marzinski } 78886f1152bSBenjamin Marzinski 78986f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode, 79086f1152bSBenjamin Marzinski struct dm_dev **result) { 79186f1152bSBenjamin Marzinski int r; 79286f1152bSBenjamin Marzinski struct table_device *td; 79386f1152bSBenjamin Marzinski 79486f1152bSBenjamin Marzinski mutex_lock(&md->table_devices_lock); 79586f1152bSBenjamin Marzinski td = find_table_device(&md->table_devices, dev, mode); 79686f1152bSBenjamin Marzinski if (!td) { 79786f1152bSBenjamin Marzinski td = kmalloc(sizeof(*td), GFP_KERNEL); 79886f1152bSBenjamin Marzinski if (!td) { 79986f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 80086f1152bSBenjamin Marzinski return -ENOMEM; 80186f1152bSBenjamin Marzinski } 80286f1152bSBenjamin Marzinski 80386f1152bSBenjamin Marzinski td->dm_dev.mode = mode; 80486f1152bSBenjamin Marzinski td->dm_dev.bdev = NULL; 80586f1152bSBenjamin Marzinski 80686f1152bSBenjamin Marzinski if ((r = open_table_device(td, dev, md))) { 80786f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 80886f1152bSBenjamin Marzinski kfree(td); 80986f1152bSBenjamin Marzinski return r; 81086f1152bSBenjamin Marzinski } 81186f1152bSBenjamin Marzinski 81286f1152bSBenjamin Marzinski format_dev_t(td->dm_dev.name, dev); 81386f1152bSBenjamin Marzinski 81486f1152bSBenjamin Marzinski atomic_set(&td->count, 0); 81586f1152bSBenjamin Marzinski list_add(&td->list, &md->table_devices); 81686f1152bSBenjamin Marzinski } 81786f1152bSBenjamin Marzinski atomic_inc(&td->count); 81886f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 81986f1152bSBenjamin Marzinski 82086f1152bSBenjamin Marzinski *result = &td->dm_dev; 82186f1152bSBenjamin Marzinski return 0; 82286f1152bSBenjamin Marzinski } 82386f1152bSBenjamin Marzinski EXPORT_SYMBOL_GPL(dm_get_table_device); 82486f1152bSBenjamin Marzinski 82586f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d) 82686f1152bSBenjamin Marzinski { 82786f1152bSBenjamin Marzinski struct table_device *td = container_of(d, struct table_device, dm_dev); 82886f1152bSBenjamin Marzinski 82986f1152bSBenjamin Marzinski mutex_lock(&md->table_devices_lock); 83086f1152bSBenjamin Marzinski if (atomic_dec_and_test(&td->count)) { 83186f1152bSBenjamin Marzinski close_table_device(td, md); 83286f1152bSBenjamin Marzinski list_del(&td->list); 83386f1152bSBenjamin Marzinski kfree(td); 83486f1152bSBenjamin Marzinski } 83586f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 83686f1152bSBenjamin Marzinski } 83786f1152bSBenjamin Marzinski EXPORT_SYMBOL(dm_put_table_device); 83886f1152bSBenjamin Marzinski 83986f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices) 84086f1152bSBenjamin Marzinski { 84186f1152bSBenjamin Marzinski struct list_head *tmp, *next; 84286f1152bSBenjamin Marzinski 84386f1152bSBenjamin Marzinski list_for_each_safe(tmp, next, devices) { 84486f1152bSBenjamin Marzinski struct table_device *td = list_entry(tmp, struct table_device, list); 84586f1152bSBenjamin Marzinski 84686f1152bSBenjamin Marzinski DMWARN("dm_destroy: %s still exists with %d references", 84786f1152bSBenjamin Marzinski td->dm_dev.name, atomic_read(&td->count)); 84886f1152bSBenjamin Marzinski kfree(td); 84986f1152bSBenjamin Marzinski } 85086f1152bSBenjamin Marzinski } 85186f1152bSBenjamin Marzinski 85286f1152bSBenjamin Marzinski /* 8533ac51e74SDarrick J. Wong * Get the geometry associated with a dm device 8543ac51e74SDarrick J. Wong */ 8553ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) 8563ac51e74SDarrick J. Wong { 8573ac51e74SDarrick J. Wong *geo = md->geometry; 8583ac51e74SDarrick J. Wong 8593ac51e74SDarrick J. Wong return 0; 8603ac51e74SDarrick J. Wong } 8613ac51e74SDarrick J. Wong 8623ac51e74SDarrick J. Wong /* 8633ac51e74SDarrick J. Wong * Set the geometry of a device. 8643ac51e74SDarrick J. Wong */ 8653ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) 8663ac51e74SDarrick J. Wong { 8673ac51e74SDarrick J. Wong sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; 8683ac51e74SDarrick J. Wong 8693ac51e74SDarrick J. Wong if (geo->start > sz) { 8703ac51e74SDarrick J. Wong DMWARN("Start sector is beyond the geometry limits."); 8713ac51e74SDarrick J. Wong return -EINVAL; 8723ac51e74SDarrick J. Wong } 8733ac51e74SDarrick J. Wong 8743ac51e74SDarrick J. Wong md->geometry = *geo; 8753ac51e74SDarrick J. Wong 8763ac51e74SDarrick J. Wong return 0; 8773ac51e74SDarrick J. Wong } 8783ac51e74SDarrick J. Wong 8791da177e4SLinus Torvalds /*----------------------------------------------------------------- 8801da177e4SLinus Torvalds * CRUD START: 8811da177e4SLinus Torvalds * A more elegant soln is in the works that uses the queue 8821da177e4SLinus Torvalds * merge fn, unfortunately there are a couple of changes to 8831da177e4SLinus Torvalds * the block layer that I want to make for this. So in the 8841da177e4SLinus Torvalds * interests of getting something for people to use I give 8851da177e4SLinus Torvalds * you this clearly demarcated crap. 8861da177e4SLinus Torvalds *---------------------------------------------------------------*/ 8871da177e4SLinus Torvalds 8882e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md) 8892e93ccc1SKiyoshi Ueda { 8902e93ccc1SKiyoshi Ueda return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 8912e93ccc1SKiyoshi Ueda } 8922e93ccc1SKiyoshi Ueda 8931da177e4SLinus Torvalds /* 8941da177e4SLinus Torvalds * Decrements the number of outstanding ios that a bio has been 8951da177e4SLinus Torvalds * cloned into, completing the original io if necc. 8961da177e4SLinus Torvalds */ 897858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error) 8981da177e4SLinus Torvalds { 8992e93ccc1SKiyoshi Ueda unsigned long flags; 900b35f8caaSMilan Broz int io_error; 901b35f8caaSMilan Broz struct bio *bio; 902b35f8caaSMilan Broz struct mapped_device *md = io->md; 9032e93ccc1SKiyoshi Ueda 9042e93ccc1SKiyoshi Ueda /* Push-back supersedes any I/O errors */ 905f88fb981SKiyoshi Ueda if (unlikely(error)) { 906f88fb981SKiyoshi Ueda spin_lock_irqsave(&io->endio_lock, flags); 907f88fb981SKiyoshi Ueda if (!(io->error > 0 && __noflush_suspending(md))) 9081da177e4SLinus Torvalds io->error = error; 909f88fb981SKiyoshi Ueda spin_unlock_irqrestore(&io->endio_lock, flags); 910f88fb981SKiyoshi Ueda } 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds if (atomic_dec_and_test(&io->io_count)) { 9132e93ccc1SKiyoshi Ueda if (io->error == DM_ENDIO_REQUEUE) { 9142e93ccc1SKiyoshi Ueda /* 9152e93ccc1SKiyoshi Ueda * Target requested pushing back the I/O. 9162e93ccc1SKiyoshi Ueda */ 917022c2611SMikulas Patocka spin_lock_irqsave(&md->deferred_lock, flags); 9186a8736d1STejun Heo if (__noflush_suspending(md)) 9196a8736d1STejun Heo bio_list_add_head(&md->deferred, io->bio); 9206a8736d1STejun Heo else 9212e93ccc1SKiyoshi Ueda /* noflush suspend was interrupted. */ 9222e93ccc1SKiyoshi Ueda io->error = -EIO; 923022c2611SMikulas Patocka spin_unlock_irqrestore(&md->deferred_lock, flags); 9242e93ccc1SKiyoshi Ueda } 9252e93ccc1SKiyoshi Ueda 926b35f8caaSMilan Broz io_error = io->error; 927b35f8caaSMilan Broz bio = io->bio; 928af7e466aSMikulas Patocka end_io_acct(io); 929a97f925aSMikulas Patocka free_io(md, io); 9301da177e4SLinus Torvalds 9316a8736d1STejun Heo if (io_error == DM_ENDIO_REQUEUE) 9326a8736d1STejun Heo return; 9336a8736d1STejun Heo 9344f024f37SKent Overstreet if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) { 9351da177e4SLinus Torvalds /* 9366a8736d1STejun Heo * Preflush done for flush with data, reissue 9376a8736d1STejun Heo * without REQ_FLUSH. 9381da177e4SLinus Torvalds */ 9396a8736d1STejun Heo bio->bi_rw &= ~REQ_FLUSH; 9406a8736d1STejun Heo queue_io(md, bio); 9415f3ea37cSArnaldo Carvalho de Melo } else { 942b372d360SMike Snitzer /* done with normal IO or empty flush */ 9430a82a8d1SLinus Torvalds trace_block_bio_complete(md->queue, bio, io_error); 944b35f8caaSMilan Broz bio_endio(bio, io_error); 9452e93ccc1SKiyoshi Ueda } 9461da177e4SLinus Torvalds } 947af7e466aSMikulas Patocka } 9481da177e4SLinus Torvalds 9497eee4ae2SMike Snitzer static void disable_write_same(struct mapped_device *md) 9507eee4ae2SMike Snitzer { 9517eee4ae2SMike Snitzer struct queue_limits *limits = dm_get_queue_limits(md); 9527eee4ae2SMike Snitzer 9537eee4ae2SMike Snitzer /* device doesn't really support WRITE SAME, disable it */ 9547eee4ae2SMike Snitzer limits->max_write_same_sectors = 0; 9557eee4ae2SMike Snitzer } 9567eee4ae2SMike Snitzer 9576712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error) 9581da177e4SLinus Torvalds { 9595164beceSzhendong chen int r = error; 960bfc6d41cSMikulas Patocka struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); 961b35f8caaSMilan Broz struct dm_io *io = tio->io; 9629faf400fSStefan Bader struct mapped_device *md = tio->io->md; 9631da177e4SLinus Torvalds dm_endio_fn endio = tio->ti->type->end_io; 9641da177e4SLinus Torvalds 9651da177e4SLinus Torvalds if (!bio_flagged(bio, BIO_UPTODATE) && !error) 9661da177e4SLinus Torvalds error = -EIO; 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds if (endio) { 9697de3ee57SMikulas Patocka r = endio(tio->ti, bio, error); 9702e93ccc1SKiyoshi Ueda if (r < 0 || r == DM_ENDIO_REQUEUE) 9712e93ccc1SKiyoshi Ueda /* 9722e93ccc1SKiyoshi Ueda * error and requeue request are handled 9732e93ccc1SKiyoshi Ueda * in dec_pending(). 9742e93ccc1SKiyoshi Ueda */ 9751da177e4SLinus Torvalds error = r; 97645cbcd79SKiyoshi Ueda else if (r == DM_ENDIO_INCOMPLETE) 97745cbcd79SKiyoshi Ueda /* The target will handle the io */ 9786712ecf8SNeilBrown return; 97945cbcd79SKiyoshi Ueda else if (r) { 98045cbcd79SKiyoshi Ueda DMWARN("unimplemented target endio return value: %d", r); 98145cbcd79SKiyoshi Ueda BUG(); 98245cbcd79SKiyoshi Ueda } 9831da177e4SLinus Torvalds } 9841da177e4SLinus Torvalds 9857eee4ae2SMike Snitzer if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) && 9867eee4ae2SMike Snitzer !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors)) 9877eee4ae2SMike Snitzer disable_write_same(md); 9887eee4ae2SMike Snitzer 9899faf400fSStefan Bader free_tio(md, tio); 990b35f8caaSMilan Broz dec_pending(io, error); 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds 993cec47e3dSKiyoshi Ueda /* 994cec47e3dSKiyoshi Ueda * Partial completion handling for request-based dm 995cec47e3dSKiyoshi Ueda */ 996cec47e3dSKiyoshi Ueda static void end_clone_bio(struct bio *clone, int error) 997cec47e3dSKiyoshi Ueda { 998bfc6d41cSMikulas Patocka struct dm_rq_clone_bio_info *info = 999bfc6d41cSMikulas Patocka container_of(clone, struct dm_rq_clone_bio_info, clone); 1000cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = info->tio; 1001cec47e3dSKiyoshi Ueda struct bio *bio = info->orig; 10024f024f37SKent Overstreet unsigned int nr_bytes = info->orig->bi_iter.bi_size; 1003cec47e3dSKiyoshi Ueda 1004cec47e3dSKiyoshi Ueda bio_put(clone); 1005cec47e3dSKiyoshi Ueda 1006cec47e3dSKiyoshi Ueda if (tio->error) 1007cec47e3dSKiyoshi Ueda /* 1008cec47e3dSKiyoshi Ueda * An error has already been detected on the request. 1009cec47e3dSKiyoshi Ueda * Once error occurred, just let clone->end_io() handle 1010cec47e3dSKiyoshi Ueda * the remainder. 1011cec47e3dSKiyoshi Ueda */ 1012cec47e3dSKiyoshi Ueda return; 1013cec47e3dSKiyoshi Ueda else if (error) { 1014cec47e3dSKiyoshi Ueda /* 1015cec47e3dSKiyoshi Ueda * Don't notice the error to the upper layer yet. 1016cec47e3dSKiyoshi Ueda * The error handling decision is made by the target driver, 1017cec47e3dSKiyoshi Ueda * when the request is completed. 1018cec47e3dSKiyoshi Ueda */ 1019cec47e3dSKiyoshi Ueda tio->error = error; 1020cec47e3dSKiyoshi Ueda return; 1021cec47e3dSKiyoshi Ueda } 1022cec47e3dSKiyoshi Ueda 1023cec47e3dSKiyoshi Ueda /* 1024cec47e3dSKiyoshi Ueda * I/O for the bio successfully completed. 1025cec47e3dSKiyoshi Ueda * Notice the data completion to the upper layer. 1026cec47e3dSKiyoshi Ueda */ 1027cec47e3dSKiyoshi Ueda 1028cec47e3dSKiyoshi Ueda /* 1029cec47e3dSKiyoshi Ueda * bios are processed from the head of the list. 1030cec47e3dSKiyoshi Ueda * So the completing bio should always be rq->bio. 1031cec47e3dSKiyoshi Ueda * If it's not, something wrong is happening. 1032cec47e3dSKiyoshi Ueda */ 1033cec47e3dSKiyoshi Ueda if (tio->orig->bio != bio) 1034cec47e3dSKiyoshi Ueda DMERR("bio completion is going in the middle of the request"); 1035cec47e3dSKiyoshi Ueda 1036cec47e3dSKiyoshi Ueda /* 1037cec47e3dSKiyoshi Ueda * Update the original request. 1038cec47e3dSKiyoshi Ueda * Do not use blk_end_request() here, because it may complete 1039cec47e3dSKiyoshi Ueda * the original request before the clone, and break the ordering. 1040cec47e3dSKiyoshi Ueda */ 1041cec47e3dSKiyoshi Ueda blk_update_request(tio->orig, 0, nr_bytes); 1042cec47e3dSKiyoshi Ueda } 1043cec47e3dSKiyoshi Ueda 1044bfebd1cdSMike Snitzer static struct dm_rq_target_io *tio_from_request(struct request *rq) 1045bfebd1cdSMike Snitzer { 1046bfebd1cdSMike Snitzer return (rq->q->mq_ops ? blk_mq_rq_to_pdu(rq) : rq->special); 1047bfebd1cdSMike Snitzer } 1048bfebd1cdSMike Snitzer 1049cec47e3dSKiyoshi Ueda /* 1050cec47e3dSKiyoshi Ueda * Don't touch any member of the md after calling this function because 1051cec47e3dSKiyoshi Ueda * the md may be freed in dm_put() at the end of this function. 1052cec47e3dSKiyoshi Ueda * Or do dm_get() before calling this function and dm_put() later. 1053cec47e3dSKiyoshi Ueda */ 1054466d89a6SKeith Busch static void rq_completed(struct mapped_device *md, int rw, bool run_queue) 1055cec47e3dSKiyoshi Ueda { 10569a0e609eSMike Snitzer int nr_requests_pending; 10579a0e609eSMike Snitzer 1058b4324feeSKiyoshi Ueda atomic_dec(&md->pending[rw]); 1059cec47e3dSKiyoshi Ueda 1060cec47e3dSKiyoshi Ueda /* nudge anyone waiting on suspend queue */ 10619a0e609eSMike Snitzer nr_requests_pending = md_in_flight(md); 10629a0e609eSMike Snitzer if (!nr_requests_pending) 1063cec47e3dSKiyoshi Ueda wake_up(&md->wait); 1064cec47e3dSKiyoshi Ueda 1065a8c32a5cSJens Axboe /* 1066a8c32a5cSJens Axboe * Run this off this callpath, as drivers could invoke end_io while 1067a8c32a5cSJens Axboe * inside their request_fn (and holding the queue lock). Calling 1068a8c32a5cSJens Axboe * back into ->request_fn() could deadlock attempting to grab the 1069a8c32a5cSJens Axboe * queue lock again. 1070a8c32a5cSJens Axboe */ 10719a0e609eSMike Snitzer if (run_queue) { 1072bfebd1cdSMike Snitzer if (md->queue->mq_ops) 1073bfebd1cdSMike Snitzer blk_mq_run_hw_queues(md->queue, true); 1074bfebd1cdSMike Snitzer else if (!nr_requests_pending || 10759a0e609eSMike Snitzer (nr_requests_pending >= md->queue->nr_congestion_on)) 1076a8c32a5cSJens Axboe blk_run_queue_async(md->queue); 10779a0e609eSMike Snitzer } 1078cec47e3dSKiyoshi Ueda 1079cec47e3dSKiyoshi Ueda /* 1080cec47e3dSKiyoshi Ueda * dm_put() must be at the end of this function. See the comment above 1081cec47e3dSKiyoshi Ueda */ 1082cec47e3dSKiyoshi Ueda dm_put(md); 1083cec47e3dSKiyoshi Ueda } 1084cec47e3dSKiyoshi Ueda 1085aa6df8ddSMike Snitzer static void free_rq_clone(struct request *clone, bool must_be_mapped) 1086a77e28c7SKiyoshi Ueda { 1087a77e28c7SKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 1088bfebd1cdSMike Snitzer struct mapped_device *md = tio->md; 1089a77e28c7SKiyoshi Ueda 1090aa6df8ddSMike Snitzer WARN_ON_ONCE(must_be_mapped && !clone->q); 1091aa6df8ddSMike Snitzer 1092a77e28c7SKiyoshi Ueda blk_rq_unprep_clone(clone); 1093bfebd1cdSMike Snitzer 1094aa6df8ddSMike Snitzer if (md->type == DM_TYPE_MQ_REQUEST_BASED) 1095aa6df8ddSMike Snitzer /* stacked on blk-mq queue(s) */ 1096e5863d9aSMike Snitzer tio->ti->type->release_clone_rq(clone); 109702233342SMike Snitzer else if (!md->queue->mq_ops) 109802233342SMike Snitzer /* request_fn queue stacked on request_fn queue(s) */ 1099bfebd1cdSMike Snitzer free_clone_request(md, clone); 1100aa6df8ddSMike Snitzer /* 1101aa6df8ddSMike Snitzer * NOTE: for the blk-mq queue stacked on request_fn queue(s) case: 1102aa6df8ddSMike Snitzer * no need to call free_clone_request() because we leverage blk-mq by 1103aa6df8ddSMike Snitzer * allocating the clone at the end of the blk-mq pdu (see: clone_rq) 1104aa6df8ddSMike Snitzer */ 1105bfebd1cdSMike Snitzer 1106bfebd1cdSMike Snitzer if (!md->queue->mq_ops) 1107a77e28c7SKiyoshi Ueda free_rq_tio(tio); 1108a77e28c7SKiyoshi Ueda } 1109a77e28c7SKiyoshi Ueda 1110980691e5SKiyoshi Ueda /* 1111980691e5SKiyoshi Ueda * Complete the clone and the original request. 1112466d89a6SKeith Busch * Must be called without clone's queue lock held, 1113466d89a6SKeith Busch * see end_clone_request() for more details. 1114980691e5SKiyoshi Ueda */ 1115980691e5SKiyoshi Ueda static void dm_end_request(struct request *clone, int error) 1116980691e5SKiyoshi Ueda { 1117980691e5SKiyoshi Ueda int rw = rq_data_dir(clone); 1118980691e5SKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 1119980691e5SKiyoshi Ueda struct mapped_device *md = tio->md; 1120980691e5SKiyoshi Ueda struct request *rq = tio->orig; 1121980691e5SKiyoshi Ueda 112229e4013dSTejun Heo if (rq->cmd_type == REQ_TYPE_BLOCK_PC) { 1123980691e5SKiyoshi Ueda rq->errors = clone->errors; 1124980691e5SKiyoshi Ueda rq->resid_len = clone->resid_len; 1125980691e5SKiyoshi Ueda 1126980691e5SKiyoshi Ueda if (rq->sense) 1127980691e5SKiyoshi Ueda /* 1128980691e5SKiyoshi Ueda * We are using the sense buffer of the original 1129980691e5SKiyoshi Ueda * request. 1130980691e5SKiyoshi Ueda * So setting the length of the sense data is enough. 1131980691e5SKiyoshi Ueda */ 1132980691e5SKiyoshi Ueda rq->sense_len = clone->sense_len; 1133980691e5SKiyoshi Ueda } 1134980691e5SKiyoshi Ueda 1135aa6df8ddSMike Snitzer free_rq_clone(clone, true); 1136bfebd1cdSMike Snitzer if (!rq->q->mq_ops) 1137980691e5SKiyoshi Ueda blk_end_request_all(rq, error); 1138bfebd1cdSMike Snitzer else 1139bfebd1cdSMike Snitzer blk_mq_end_request(rq, error); 114029e4013dSTejun Heo rq_completed(md, rw, true); 1141980691e5SKiyoshi Ueda } 1142980691e5SKiyoshi Ueda 1143cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq) 1144cec47e3dSKiyoshi Ueda { 1145bfebd1cdSMike Snitzer struct dm_rq_target_io *tio = tio_from_request(rq); 1146466d89a6SKeith Busch struct request *clone = tio->clone; 1147cec47e3dSKiyoshi Ueda 1148bfebd1cdSMike Snitzer if (!rq->q->mq_ops) { 1149cec47e3dSKiyoshi Ueda rq->special = NULL; 1150cec47e3dSKiyoshi Ueda rq->cmd_flags &= ~REQ_DONTPREP; 1151bfebd1cdSMike Snitzer } 1152cec47e3dSKiyoshi Ueda 1153e5863d9aSMike Snitzer if (clone) 1154aa6df8ddSMike Snitzer free_rq_clone(clone, false); 1155cec47e3dSKiyoshi Ueda } 1156cec47e3dSKiyoshi Ueda 1157cec47e3dSKiyoshi Ueda /* 1158cec47e3dSKiyoshi Ueda * Requeue the original request of a clone. 1159cec47e3dSKiyoshi Ueda */ 1160bfebd1cdSMike Snitzer static void old_requeue_request(struct request *rq) 1161cec47e3dSKiyoshi Ueda { 1162cec47e3dSKiyoshi Ueda struct request_queue *q = rq->q; 1163cec47e3dSKiyoshi Ueda unsigned long flags; 1164cec47e3dSKiyoshi Ueda 1165cec47e3dSKiyoshi Ueda spin_lock_irqsave(q->queue_lock, flags); 1166cec47e3dSKiyoshi Ueda blk_requeue_request(q, rq); 11674ae9944dSJunichi Nomura blk_run_queue_async(q); 1168cec47e3dSKiyoshi Ueda spin_unlock_irqrestore(q->queue_lock, flags); 1169bfebd1cdSMike Snitzer } 1170bfebd1cdSMike Snitzer 1171bfebd1cdSMike Snitzer static void dm_requeue_unmapped_original_request(struct mapped_device *md, 1172bfebd1cdSMike Snitzer struct request *rq) 1173bfebd1cdSMike Snitzer { 1174bfebd1cdSMike Snitzer int rw = rq_data_dir(rq); 1175bfebd1cdSMike Snitzer 1176bfebd1cdSMike Snitzer dm_unprep_request(rq); 1177bfebd1cdSMike Snitzer 1178bfebd1cdSMike Snitzer if (!rq->q->mq_ops) 1179bfebd1cdSMike Snitzer old_requeue_request(rq); 1180bfebd1cdSMike Snitzer else { 1181bfebd1cdSMike Snitzer blk_mq_requeue_request(rq); 1182bfebd1cdSMike Snitzer blk_mq_kick_requeue_list(rq->q); 1183bfebd1cdSMike Snitzer } 1184cec47e3dSKiyoshi Ueda 1185466d89a6SKeith Busch rq_completed(md, rw, false); 1186cec47e3dSKiyoshi Ueda } 1187466d89a6SKeith Busch 1188466d89a6SKeith Busch static void dm_requeue_unmapped_request(struct request *clone) 1189466d89a6SKeith Busch { 1190466d89a6SKeith Busch struct dm_rq_target_io *tio = clone->end_io_data; 1191466d89a6SKeith Busch 1192466d89a6SKeith Busch dm_requeue_unmapped_original_request(tio->md, tio->orig); 1193cec47e3dSKiyoshi Ueda } 1194cec47e3dSKiyoshi Ueda 1195bfebd1cdSMike Snitzer static void old_stop_queue(struct request_queue *q) 1196cec47e3dSKiyoshi Ueda { 1197bfebd1cdSMike Snitzer unsigned long flags; 1198bfebd1cdSMike Snitzer 1199bfebd1cdSMike Snitzer if (blk_queue_stopped(q)) 1200bfebd1cdSMike Snitzer return; 1201bfebd1cdSMike Snitzer 1202bfebd1cdSMike Snitzer spin_lock_irqsave(q->queue_lock, flags); 1203cec47e3dSKiyoshi Ueda blk_stop_queue(q); 1204bfebd1cdSMike Snitzer spin_unlock_irqrestore(q->queue_lock, flags); 1205cec47e3dSKiyoshi Ueda } 1206cec47e3dSKiyoshi Ueda 1207cec47e3dSKiyoshi Ueda static void stop_queue(struct request_queue *q) 1208cec47e3dSKiyoshi Ueda { 1209bfebd1cdSMike Snitzer if (!q->mq_ops) 1210bfebd1cdSMike Snitzer old_stop_queue(q); 1211bfebd1cdSMike Snitzer else 1212bfebd1cdSMike Snitzer blk_mq_stop_hw_queues(q); 1213bfebd1cdSMike Snitzer } 1214bfebd1cdSMike Snitzer 1215bfebd1cdSMike Snitzer static void old_start_queue(struct request_queue *q) 1216bfebd1cdSMike Snitzer { 1217cec47e3dSKiyoshi Ueda unsigned long flags; 1218cec47e3dSKiyoshi Ueda 1219cec47e3dSKiyoshi Ueda spin_lock_irqsave(q->queue_lock, flags); 1220cec47e3dSKiyoshi Ueda if (blk_queue_stopped(q)) 1221cec47e3dSKiyoshi Ueda blk_start_queue(q); 1222bfebd1cdSMike Snitzer spin_unlock_irqrestore(q->queue_lock, flags); 1223cec47e3dSKiyoshi Ueda } 1224cec47e3dSKiyoshi Ueda 1225cec47e3dSKiyoshi Ueda static void start_queue(struct request_queue *q) 1226cec47e3dSKiyoshi Ueda { 1227bfebd1cdSMike Snitzer if (!q->mq_ops) 1228bfebd1cdSMike Snitzer old_start_queue(q); 1229bfebd1cdSMike Snitzer else 1230bfebd1cdSMike Snitzer blk_mq_start_stopped_hw_queues(q, true); 1231cec47e3dSKiyoshi Ueda } 1232cec47e3dSKiyoshi Ueda 123311a68244SKiyoshi Ueda static void dm_done(struct request *clone, int error, bool mapped) 123411a68244SKiyoshi Ueda { 123511a68244SKiyoshi Ueda int r = error; 123611a68244SKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 1237ba1cbad9SMike Snitzer dm_request_endio_fn rq_end_io = NULL; 1238ba1cbad9SMike Snitzer 1239ba1cbad9SMike Snitzer if (tio->ti) { 1240ba1cbad9SMike Snitzer rq_end_io = tio->ti->type->rq_end_io; 124111a68244SKiyoshi Ueda 124211a68244SKiyoshi Ueda if (mapped && rq_end_io) 124311a68244SKiyoshi Ueda r = rq_end_io(tio->ti, clone, error, &tio->info); 1244ba1cbad9SMike Snitzer } 124511a68244SKiyoshi Ueda 12467eee4ae2SMike Snitzer if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) && 12477eee4ae2SMike Snitzer !clone->q->limits.max_write_same_sectors)) 12487eee4ae2SMike Snitzer disable_write_same(tio->md); 12497eee4ae2SMike Snitzer 125011a68244SKiyoshi Ueda if (r <= 0) 125111a68244SKiyoshi Ueda /* The target wants to complete the I/O */ 125211a68244SKiyoshi Ueda dm_end_request(clone, r); 125311a68244SKiyoshi Ueda else if (r == DM_ENDIO_INCOMPLETE) 125411a68244SKiyoshi Ueda /* The target will handle the I/O */ 125511a68244SKiyoshi Ueda return; 125611a68244SKiyoshi Ueda else if (r == DM_ENDIO_REQUEUE) 125711a68244SKiyoshi Ueda /* The target wants to requeue the I/O */ 125811a68244SKiyoshi Ueda dm_requeue_unmapped_request(clone); 125911a68244SKiyoshi Ueda else { 126011a68244SKiyoshi Ueda DMWARN("unimplemented target endio return value: %d", r); 126111a68244SKiyoshi Ueda BUG(); 126211a68244SKiyoshi Ueda } 126311a68244SKiyoshi Ueda } 126411a68244SKiyoshi Ueda 1265cec47e3dSKiyoshi Ueda /* 1266cec47e3dSKiyoshi Ueda * Request completion handler for request-based dm 1267cec47e3dSKiyoshi Ueda */ 1268cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq) 1269cec47e3dSKiyoshi Ueda { 127011a68244SKiyoshi Ueda bool mapped = true; 1271bfebd1cdSMike Snitzer struct dm_rq_target_io *tio = tio_from_request(rq); 1272466d89a6SKeith Busch struct request *clone = tio->clone; 1273bfebd1cdSMike Snitzer int rw; 1274cec47e3dSKiyoshi Ueda 1275e5863d9aSMike Snitzer if (!clone) { 1276bfebd1cdSMike Snitzer rw = rq_data_dir(rq); 1277bfebd1cdSMike Snitzer if (!rq->q->mq_ops) { 1278e5863d9aSMike Snitzer blk_end_request_all(rq, tio->error); 1279bfebd1cdSMike Snitzer rq_completed(tio->md, rw, false); 1280e5863d9aSMike Snitzer free_rq_tio(tio); 1281bfebd1cdSMike Snitzer } else { 1282bfebd1cdSMike Snitzer blk_mq_end_request(rq, tio->error); 1283bfebd1cdSMike Snitzer rq_completed(tio->md, rw, false); 1284bfebd1cdSMike Snitzer } 1285e5863d9aSMike Snitzer return; 1286e5863d9aSMike Snitzer } 1287cec47e3dSKiyoshi Ueda 128811a68244SKiyoshi Ueda if (rq->cmd_flags & REQ_FAILED) 128911a68244SKiyoshi Ueda mapped = false; 1290cec47e3dSKiyoshi Ueda 129111a68244SKiyoshi Ueda dm_done(clone, tio->error, mapped); 1292cec47e3dSKiyoshi Ueda } 1293cec47e3dSKiyoshi Ueda 1294cec47e3dSKiyoshi Ueda /* 1295cec47e3dSKiyoshi Ueda * Complete the clone and the original request with the error status 1296cec47e3dSKiyoshi Ueda * through softirq context. 1297cec47e3dSKiyoshi Ueda */ 1298466d89a6SKeith Busch static void dm_complete_request(struct request *rq, int error) 1299cec47e3dSKiyoshi Ueda { 1300bfebd1cdSMike Snitzer struct dm_rq_target_io *tio = tio_from_request(rq); 1301cec47e3dSKiyoshi Ueda 1302cec47e3dSKiyoshi Ueda tio->error = error; 1303cec47e3dSKiyoshi Ueda blk_complete_request(rq); 1304cec47e3dSKiyoshi Ueda } 1305cec47e3dSKiyoshi Ueda 1306cec47e3dSKiyoshi Ueda /* 1307cec47e3dSKiyoshi Ueda * Complete the not-mapped clone and the original request with the error status 1308cec47e3dSKiyoshi Ueda * through softirq context. 1309cec47e3dSKiyoshi Ueda * Target's rq_end_io() function isn't called. 1310e5863d9aSMike Snitzer * This may be used when the target's map_rq() or clone_and_map_rq() functions fail. 1311cec47e3dSKiyoshi Ueda */ 1312466d89a6SKeith Busch static void dm_kill_unmapped_request(struct request *rq, int error) 1313cec47e3dSKiyoshi Ueda { 1314cec47e3dSKiyoshi Ueda rq->cmd_flags |= REQ_FAILED; 1315466d89a6SKeith Busch dm_complete_request(rq, error); 1316cec47e3dSKiyoshi Ueda } 1317cec47e3dSKiyoshi Ueda 1318cec47e3dSKiyoshi Ueda /* 1319bfebd1cdSMike Snitzer * Called with the clone's queue lock held (for non-blk-mq) 1320cec47e3dSKiyoshi Ueda */ 1321cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error) 1322cec47e3dSKiyoshi Ueda { 1323466d89a6SKeith Busch struct dm_rq_target_io *tio = clone->end_io_data; 1324466d89a6SKeith Busch 1325e5863d9aSMike Snitzer if (!clone->q->mq_ops) { 1326cec47e3dSKiyoshi Ueda /* 1327cec47e3dSKiyoshi Ueda * For just cleaning up the information of the queue in which 1328cec47e3dSKiyoshi Ueda * the clone was dispatched. 1329e5863d9aSMike Snitzer * The clone is *NOT* freed actually here because it is alloced 1330e5863d9aSMike Snitzer * from dm own mempool (REQ_ALLOCED isn't set). 1331cec47e3dSKiyoshi Ueda */ 1332cec47e3dSKiyoshi Ueda __blk_put_request(clone->q, clone); 1333e5863d9aSMike Snitzer } 1334cec47e3dSKiyoshi Ueda 1335cec47e3dSKiyoshi Ueda /* 1336cec47e3dSKiyoshi Ueda * Actual request completion is done in a softirq context which doesn't 1337466d89a6SKeith Busch * hold the clone's queue lock. Otherwise, deadlock could occur because: 1338cec47e3dSKiyoshi Ueda * - another request may be submitted by the upper level driver 1339cec47e3dSKiyoshi Ueda * of the stacking during the completion 1340cec47e3dSKiyoshi Ueda * - the submission which requires queue lock may be done 1341466d89a6SKeith Busch * against this clone's queue 1342cec47e3dSKiyoshi Ueda */ 1343466d89a6SKeith Busch dm_complete_request(tio->orig, error); 1344cec47e3dSKiyoshi Ueda } 1345cec47e3dSKiyoshi Ueda 134656a67df7SMike Snitzer /* 134756a67df7SMike Snitzer * Return maximum size of I/O possible at the supplied sector up to the current 134856a67df7SMike Snitzer * target boundary. 134956a67df7SMike Snitzer */ 135056a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti) 13511da177e4SLinus Torvalds { 135256a67df7SMike Snitzer sector_t target_offset = dm_target_offset(ti, sector); 135356a67df7SMike Snitzer 135456a67df7SMike Snitzer return ti->len - target_offset; 135556a67df7SMike Snitzer } 135656a67df7SMike Snitzer 135756a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti) 135856a67df7SMike Snitzer { 135956a67df7SMike Snitzer sector_t len = max_io_len_target_boundary(sector, ti); 1360542f9038SMike Snitzer sector_t offset, max_len; 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds /* 13631da177e4SLinus Torvalds * Does the target need to split even further? 13641da177e4SLinus Torvalds */ 1365542f9038SMike Snitzer if (ti->max_io_len) { 1366542f9038SMike Snitzer offset = dm_target_offset(ti, sector); 1367542f9038SMike Snitzer if (unlikely(ti->max_io_len & (ti->max_io_len - 1))) 1368542f9038SMike Snitzer max_len = sector_div(offset, ti->max_io_len); 1369542f9038SMike Snitzer else 1370542f9038SMike Snitzer max_len = offset & (ti->max_io_len - 1); 1371542f9038SMike Snitzer max_len = ti->max_io_len - max_len; 1372542f9038SMike Snitzer 1373542f9038SMike Snitzer if (len > max_len) 1374542f9038SMike Snitzer len = max_len; 13751da177e4SLinus Torvalds } 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds return len; 13781da177e4SLinus Torvalds } 13791da177e4SLinus Torvalds 1380542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len) 1381542f9038SMike Snitzer { 1382542f9038SMike Snitzer if (len > UINT_MAX) { 1383542f9038SMike Snitzer DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)", 1384542f9038SMike Snitzer (unsigned long long)len, UINT_MAX); 1385542f9038SMike Snitzer ti->error = "Maximum size of target IO is too large"; 1386542f9038SMike Snitzer return -EINVAL; 1387542f9038SMike Snitzer } 1388542f9038SMike Snitzer 1389542f9038SMike Snitzer ti->max_io_len = (uint32_t) len; 1390542f9038SMike Snitzer 1391542f9038SMike Snitzer return 0; 1392542f9038SMike Snitzer } 1393542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len); 1394542f9038SMike Snitzer 13951dd40c3eSMikulas Patocka /* 13961dd40c3eSMikulas Patocka * A target may call dm_accept_partial_bio only from the map routine. It is 13971dd40c3eSMikulas Patocka * allowed for all bio types except REQ_FLUSH. 13981dd40c3eSMikulas Patocka * 13991dd40c3eSMikulas Patocka * dm_accept_partial_bio informs the dm that the target only wants to process 14001dd40c3eSMikulas Patocka * additional n_sectors sectors of the bio and the rest of the data should be 14011dd40c3eSMikulas Patocka * sent in a next bio. 14021dd40c3eSMikulas Patocka * 14031dd40c3eSMikulas Patocka * A diagram that explains the arithmetics: 14041dd40c3eSMikulas Patocka * +--------------------+---------------+-------+ 14051dd40c3eSMikulas Patocka * | 1 | 2 | 3 | 14061dd40c3eSMikulas Patocka * +--------------------+---------------+-------+ 14071dd40c3eSMikulas Patocka * 14081dd40c3eSMikulas Patocka * <-------------- *tio->len_ptr ---------------> 14091dd40c3eSMikulas Patocka * <------- bi_size -------> 14101dd40c3eSMikulas Patocka * <-- n_sectors --> 14111dd40c3eSMikulas Patocka * 14121dd40c3eSMikulas Patocka * Region 1 was already iterated over with bio_advance or similar function. 14131dd40c3eSMikulas Patocka * (it may be empty if the target doesn't use bio_advance) 14141dd40c3eSMikulas Patocka * Region 2 is the remaining bio size that the target wants to process. 14151dd40c3eSMikulas Patocka * (it may be empty if region 1 is non-empty, although there is no reason 14161dd40c3eSMikulas Patocka * to make it empty) 14171dd40c3eSMikulas Patocka * The target requires that region 3 is to be sent in the next bio. 14181dd40c3eSMikulas Patocka * 14191dd40c3eSMikulas Patocka * If the target wants to receive multiple copies of the bio (via num_*bios, etc), 14201dd40c3eSMikulas Patocka * the partially processed part (the sum of regions 1+2) must be the same for all 14211dd40c3eSMikulas Patocka * copies of the bio. 14221dd40c3eSMikulas Patocka */ 14231dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors) 14241dd40c3eSMikulas Patocka { 14251dd40c3eSMikulas Patocka struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); 14261dd40c3eSMikulas Patocka unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT; 14271dd40c3eSMikulas Patocka BUG_ON(bio->bi_rw & REQ_FLUSH); 14281dd40c3eSMikulas Patocka BUG_ON(bi_size > *tio->len_ptr); 14291dd40c3eSMikulas Patocka BUG_ON(n_sectors > bi_size); 14301dd40c3eSMikulas Patocka *tio->len_ptr -= bi_size - n_sectors; 14311dd40c3eSMikulas Patocka bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT; 14321dd40c3eSMikulas Patocka } 14331dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio); 14341dd40c3eSMikulas Patocka 1435bd2a49b8SAlasdair G Kergon static void __map_bio(struct dm_target_io *tio) 14361da177e4SLinus Torvalds { 14371da177e4SLinus Torvalds int r; 14382056a782SJens Axboe sector_t sector; 14399faf400fSStefan Bader struct mapped_device *md; 1440dba14160SMikulas Patocka struct bio *clone = &tio->clone; 1441bd2a49b8SAlasdair G Kergon struct dm_target *ti = tio->ti; 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds clone->bi_end_io = clone_endio; 14441da177e4SLinus Torvalds 14451da177e4SLinus Torvalds /* 14461da177e4SLinus Torvalds * Map the clone. If r == 0 we don't need to do 14471da177e4SLinus Torvalds * anything, the target has assumed ownership of 14481da177e4SLinus Torvalds * this io. 14491da177e4SLinus Torvalds */ 14501da177e4SLinus Torvalds atomic_inc(&tio->io->io_count); 14514f024f37SKent Overstreet sector = clone->bi_iter.bi_sector; 14527de3ee57SMikulas Patocka r = ti->type->map(ti, clone); 145345cbcd79SKiyoshi Ueda if (r == DM_MAPIO_REMAPPED) { 14541da177e4SLinus Torvalds /* the bio has been remapped so dispatch it */ 14552056a782SJens Axboe 1456d07335e5SMike Snitzer trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone, 145722a7c31aSAlan D. Brunelle tio->io->bio->bi_bdev->bd_dev, sector); 14582056a782SJens Axboe 14591da177e4SLinus Torvalds generic_make_request(clone); 14602e93ccc1SKiyoshi Ueda } else if (r < 0 || r == DM_MAPIO_REQUEUE) { 14612e93ccc1SKiyoshi Ueda /* error the io and bail out, or requeue it if needed */ 14629faf400fSStefan Bader md = tio->io->md; 14639faf400fSStefan Bader dec_pending(tio->io, r); 14649faf400fSStefan Bader free_tio(md, tio); 146545cbcd79SKiyoshi Ueda } else if (r) { 146645cbcd79SKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 146745cbcd79SKiyoshi Ueda BUG(); 14681da177e4SLinus Torvalds } 14691da177e4SLinus Torvalds } 14701da177e4SLinus Torvalds 14711da177e4SLinus Torvalds struct clone_info { 14721da177e4SLinus Torvalds struct mapped_device *md; 14731da177e4SLinus Torvalds struct dm_table *map; 14741da177e4SLinus Torvalds struct bio *bio; 14751da177e4SLinus Torvalds struct dm_io *io; 14761da177e4SLinus Torvalds sector_t sector; 1477e0d6609aSMikulas Patocka unsigned sector_count; 14781da177e4SLinus Torvalds }; 14791da177e4SLinus Torvalds 1480e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len) 1481bd2a49b8SAlasdair G Kergon { 14824f024f37SKent Overstreet bio->bi_iter.bi_sector = sector; 14834f024f37SKent Overstreet bio->bi_iter.bi_size = to_bytes(len); 14841da177e4SLinus Torvalds } 14851da177e4SLinus Torvalds 14861da177e4SLinus Torvalds /* 14871da177e4SLinus Torvalds * Creates a bio that consists of range of complete bvecs. 14881da177e4SLinus Torvalds */ 1489dba14160SMikulas Patocka static void clone_bio(struct dm_target_io *tio, struct bio *bio, 14901c3b13e6SKent Overstreet sector_t sector, unsigned len) 14911da177e4SLinus Torvalds { 1492dba14160SMikulas Patocka struct bio *clone = &tio->clone; 14931da177e4SLinus Torvalds 14941c3b13e6SKent Overstreet __bio_clone_fast(clone, bio); 14959c47008dSMartin K. Petersen 14961c3b13e6SKent Overstreet if (bio_integrity(bio)) 14971c3b13e6SKent Overstreet bio_integrity_clone(clone, bio, GFP_NOIO); 14981c3b13e6SKent Overstreet 14991c3b13e6SKent Overstreet bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector)); 15001c3b13e6SKent Overstreet clone->bi_iter.bi_size = to_bytes(len); 15011c3b13e6SKent Overstreet 15021c3b13e6SKent Overstreet if (bio_integrity(bio)) 15031c3b13e6SKent Overstreet bio_integrity_trim(clone, 0, len); 15041da177e4SLinus Torvalds } 15051da177e4SLinus Torvalds 15069015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci, 150799778273SJunichi Nomura struct dm_target *ti, 150855a62eefSAlasdair G Kergon unsigned target_bio_nr) 1509f9ab94ceSMikulas Patocka { 1510dba14160SMikulas Patocka struct dm_target_io *tio; 1511dba14160SMikulas Patocka struct bio *clone; 1512dba14160SMikulas Patocka 151399778273SJunichi Nomura clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs); 1514dba14160SMikulas Patocka tio = container_of(clone, struct dm_target_io, clone); 1515f9ab94ceSMikulas Patocka 1516f9ab94ceSMikulas Patocka tio->io = ci->io; 1517f9ab94ceSMikulas Patocka tio->ti = ti; 151855a62eefSAlasdair G Kergon tio->target_bio_nr = target_bio_nr; 15199015df24SAlasdair G Kergon 15209015df24SAlasdair G Kergon return tio; 15219015df24SAlasdair G Kergon } 15229015df24SAlasdair G Kergon 152314fe594dSAlasdair G Kergon static void __clone_and_map_simple_bio(struct clone_info *ci, 152414fe594dSAlasdair G Kergon struct dm_target *ti, 15251dd40c3eSMikulas Patocka unsigned target_bio_nr, unsigned *len) 15269015df24SAlasdair G Kergon { 152799778273SJunichi Nomura struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr); 1528dba14160SMikulas Patocka struct bio *clone = &tio->clone; 15299015df24SAlasdair G Kergon 15301dd40c3eSMikulas Patocka tio->len_ptr = len; 15311dd40c3eSMikulas Patocka 15321c3b13e6SKent Overstreet __bio_clone_fast(clone, ci->bio); 1533bd2a49b8SAlasdair G Kergon if (len) 15341dd40c3eSMikulas Patocka bio_setup_sector(clone, ci->sector, *len); 1535f9ab94ceSMikulas Patocka 1536bd2a49b8SAlasdair G Kergon __map_bio(tio); 1537f9ab94ceSMikulas Patocka } 1538f9ab94ceSMikulas Patocka 153914fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti, 15401dd40c3eSMikulas Patocka unsigned num_bios, unsigned *len) 154106a426ceSMike Snitzer { 154255a62eefSAlasdair G Kergon unsigned target_bio_nr; 154306a426ceSMike Snitzer 154455a62eefSAlasdair G Kergon for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++) 154514fe594dSAlasdair G Kergon __clone_and_map_simple_bio(ci, ti, target_bio_nr, len); 154606a426ceSMike Snitzer } 154706a426ceSMike Snitzer 154814fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci) 1549f9ab94ceSMikulas Patocka { 155006a426ceSMike Snitzer unsigned target_nr = 0; 1551f9ab94ceSMikulas Patocka struct dm_target *ti; 1552f9ab94ceSMikulas Patocka 1553b372d360SMike Snitzer BUG_ON(bio_has_data(ci->bio)); 1554f9ab94ceSMikulas Patocka while ((ti = dm_table_get_target(ci->map, target_nr++))) 15551dd40c3eSMikulas Patocka __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL); 1556f9ab94ceSMikulas Patocka 1557f9ab94ceSMikulas Patocka return 0; 1558f9ab94ceSMikulas Patocka } 1559f9ab94ceSMikulas Patocka 1560e4c93811SAlasdair G Kergon static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti, 15611dd40c3eSMikulas Patocka sector_t sector, unsigned *len) 15625ae89a87SMike Snitzer { 1563dba14160SMikulas Patocka struct bio *bio = ci->bio; 15645ae89a87SMike Snitzer struct dm_target_io *tio; 1565b0d8ed4dSAlasdair G Kergon unsigned target_bio_nr; 1566b0d8ed4dSAlasdair G Kergon unsigned num_target_bios = 1; 15675ae89a87SMike Snitzer 1568b0d8ed4dSAlasdair G Kergon /* 1569b0d8ed4dSAlasdair G Kergon * Does the target want to receive duplicate copies of the bio? 1570b0d8ed4dSAlasdair G Kergon */ 1571b0d8ed4dSAlasdair G Kergon if (bio_data_dir(bio) == WRITE && ti->num_write_bios) 1572b0d8ed4dSAlasdair G Kergon num_target_bios = ti->num_write_bios(ti, bio); 1573e4c93811SAlasdair G Kergon 1574b0d8ed4dSAlasdair G Kergon for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) { 157599778273SJunichi Nomura tio = alloc_tio(ci, ti, target_bio_nr); 15761dd40c3eSMikulas Patocka tio->len_ptr = len; 15771dd40c3eSMikulas Patocka clone_bio(tio, bio, sector, *len); 1578bd2a49b8SAlasdair G Kergon __map_bio(tio); 15795ae89a87SMike Snitzer } 1580b0d8ed4dSAlasdair G Kergon } 15815ae89a87SMike Snitzer 158255a62eefSAlasdair G Kergon typedef unsigned (*get_num_bios_fn)(struct dm_target *ti); 158323508a96SMike Snitzer 158455a62eefSAlasdair G Kergon static unsigned get_num_discard_bios(struct dm_target *ti) 158523508a96SMike Snitzer { 158655a62eefSAlasdair G Kergon return ti->num_discard_bios; 158723508a96SMike Snitzer } 158823508a96SMike Snitzer 158955a62eefSAlasdair G Kergon static unsigned get_num_write_same_bios(struct dm_target *ti) 159023508a96SMike Snitzer { 159155a62eefSAlasdair G Kergon return ti->num_write_same_bios; 159223508a96SMike Snitzer } 159323508a96SMike Snitzer 159423508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti); 159523508a96SMike Snitzer 159623508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti) 159723508a96SMike Snitzer { 159855a62eefSAlasdair G Kergon return ti->split_discard_bios; 159923508a96SMike Snitzer } 160023508a96SMike Snitzer 160114fe594dSAlasdair G Kergon static int __send_changing_extent_only(struct clone_info *ci, 160255a62eefSAlasdair G Kergon get_num_bios_fn get_num_bios, 160323508a96SMike Snitzer is_split_required_fn is_split_required) 16045ae89a87SMike Snitzer { 16055ae89a87SMike Snitzer struct dm_target *ti; 1606e0d6609aSMikulas Patocka unsigned len; 160755a62eefSAlasdair G Kergon unsigned num_bios; 16085ae89a87SMike Snitzer 1609a79245b3SMike Snitzer do { 16105ae89a87SMike Snitzer ti = dm_table_find_target(ci->map, ci->sector); 16115ae89a87SMike Snitzer if (!dm_target_is_valid(ti)) 16125ae89a87SMike Snitzer return -EIO; 16135ae89a87SMike Snitzer 16145ae89a87SMike Snitzer /* 161523508a96SMike Snitzer * Even though the device advertised support for this type of 161623508a96SMike Snitzer * request, that does not mean every target supports it, and 1617936688d7SMike Snitzer * reconfiguration might also have changed that since the 16185ae89a87SMike Snitzer * check was performed. 16195ae89a87SMike Snitzer */ 162055a62eefSAlasdair G Kergon num_bios = get_num_bios ? get_num_bios(ti) : 0; 162155a62eefSAlasdair G Kergon if (!num_bios) 16225ae89a87SMike Snitzer return -EOPNOTSUPP; 16235ae89a87SMike Snitzer 162423508a96SMike Snitzer if (is_split_required && !is_split_required(ti)) 1625e0d6609aSMikulas Patocka len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti)); 16267acf0277SMikulas Patocka else 1627e0d6609aSMikulas Patocka len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti)); 16285ae89a87SMike Snitzer 16291dd40c3eSMikulas Patocka __send_duplicate_bios(ci, ti, num_bios, &len); 16305ae89a87SMike Snitzer 1631a79245b3SMike Snitzer ci->sector += len; 1632a79245b3SMike Snitzer } while (ci->sector_count -= len); 16335ae89a87SMike Snitzer 16345ae89a87SMike Snitzer return 0; 16355ae89a87SMike Snitzer } 16365ae89a87SMike Snitzer 163714fe594dSAlasdair G Kergon static int __send_discard(struct clone_info *ci) 163823508a96SMike Snitzer { 163914fe594dSAlasdair G Kergon return __send_changing_extent_only(ci, get_num_discard_bios, 164023508a96SMike Snitzer is_split_required_for_discard); 164123508a96SMike Snitzer } 164223508a96SMike Snitzer 164314fe594dSAlasdair G Kergon static int __send_write_same(struct clone_info *ci) 164423508a96SMike Snitzer { 164514fe594dSAlasdair G Kergon return __send_changing_extent_only(ci, get_num_write_same_bios, NULL); 164623508a96SMike Snitzer } 164723508a96SMike Snitzer 1648e4c93811SAlasdair G Kergon /* 1649e4c93811SAlasdair G Kergon * Select the correct strategy for processing a non-flush bio. 1650e4c93811SAlasdair G Kergon */ 1651e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci) 1652e4c93811SAlasdair G Kergon { 1653e4c93811SAlasdair G Kergon struct bio *bio = ci->bio; 1654e4c93811SAlasdair G Kergon struct dm_target *ti; 16551c3b13e6SKent Overstreet unsigned len; 1656e4c93811SAlasdair G Kergon 1657e4c93811SAlasdair G Kergon if (unlikely(bio->bi_rw & REQ_DISCARD)) 1658e4c93811SAlasdair G Kergon return __send_discard(ci); 1659e4c93811SAlasdair G Kergon else if (unlikely(bio->bi_rw & REQ_WRITE_SAME)) 1660e4c93811SAlasdair G Kergon return __send_write_same(ci); 1661e4c93811SAlasdair G Kergon 1662e4c93811SAlasdair G Kergon ti = dm_table_find_target(ci->map, ci->sector); 1663e4c93811SAlasdair G Kergon if (!dm_target_is_valid(ti)) 1664e4c93811SAlasdair G Kergon return -EIO; 1665e4c93811SAlasdair G Kergon 16661c3b13e6SKent Overstreet len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count); 1667e4c93811SAlasdair G Kergon 16681dd40c3eSMikulas Patocka __clone_and_map_data_bio(ci, ti, ci->sector, &len); 1669e4c93811SAlasdair G Kergon 1670e4c93811SAlasdair G Kergon ci->sector += len; 1671e4c93811SAlasdair G Kergon ci->sector_count -= len; 1672e4c93811SAlasdair G Kergon 1673e4c93811SAlasdair G Kergon return 0; 1674e4c93811SAlasdair G Kergon } 1675e4c93811SAlasdair G Kergon 1676e4c93811SAlasdair G Kergon /* 167714fe594dSAlasdair G Kergon * Entry point to split a bio into clones and submit them to the targets. 16781da177e4SLinus Torvalds */ 167983d5e5b0SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md, 168083d5e5b0SMikulas Patocka struct dm_table *map, struct bio *bio) 16811da177e4SLinus Torvalds { 16821da177e4SLinus Torvalds struct clone_info ci; 1683512875bdSJun'ichi Nomura int error = 0; 16841da177e4SLinus Torvalds 168583d5e5b0SMikulas Patocka if (unlikely(!map)) { 1686f0b9a450SMikulas Patocka bio_io_error(bio); 1687f0b9a450SMikulas Patocka return; 1688f0b9a450SMikulas Patocka } 1689692d0eb9SMikulas Patocka 169083d5e5b0SMikulas Patocka ci.map = map; 16911da177e4SLinus Torvalds ci.md = md; 16921da177e4SLinus Torvalds ci.io = alloc_io(md); 16931da177e4SLinus Torvalds ci.io->error = 0; 16941da177e4SLinus Torvalds atomic_set(&ci.io->io_count, 1); 16951da177e4SLinus Torvalds ci.io->bio = bio; 16961da177e4SLinus Torvalds ci.io->md = md; 1697f88fb981SKiyoshi Ueda spin_lock_init(&ci.io->endio_lock); 16984f024f37SKent Overstreet ci.sector = bio->bi_iter.bi_sector; 16991da177e4SLinus Torvalds 17003eaf840eSJun'ichi "Nick" Nomura start_io_acct(ci.io); 1701bd2a49b8SAlasdair G Kergon 1702b372d360SMike Snitzer if (bio->bi_rw & REQ_FLUSH) { 1703b372d360SMike Snitzer ci.bio = &ci.md->flush_bio; 1704b372d360SMike Snitzer ci.sector_count = 0; 170514fe594dSAlasdair G Kergon error = __send_empty_flush(&ci); 1706b372d360SMike Snitzer /* dec_pending submits any data associated with flush */ 1707b372d360SMike Snitzer } else { 17086a8736d1STejun Heo ci.bio = bio; 1709f6fccb12SMilan Broz ci.sector_count = bio_sectors(bio); 1710512875bdSJun'ichi Nomura while (ci.sector_count && !error) 171114fe594dSAlasdair G Kergon error = __split_and_process_non_flush(&ci); 1712d87f4c14STejun Heo } 17131da177e4SLinus Torvalds 17141da177e4SLinus Torvalds /* drop the extra reference count */ 1715512875bdSJun'ichi Nomura dec_pending(ci.io, error); 17169e4e5f87SMilan Broz } 17179e4e5f87SMilan Broz /*----------------------------------------------------------------- 17181da177e4SLinus Torvalds * CRUD END 17191da177e4SLinus Torvalds *---------------------------------------------------------------*/ 17201da177e4SLinus Torvalds 17211da177e4SLinus Torvalds static int dm_merge_bvec(struct request_queue *q, 17221da177e4SLinus Torvalds struct bvec_merge_data *bvm, 1723f6fccb12SMilan Broz struct bio_vec *biovec) 1724f6fccb12SMilan Broz { 1725f6fccb12SMilan Broz struct mapped_device *md = q->queuedata; 172683d5e5b0SMikulas Patocka struct dm_table *map = dm_get_live_table_fast(md); 1727f6fccb12SMilan Broz struct dm_target *ti; 1728f6fccb12SMilan Broz sector_t max_sectors; 1729f6fccb12SMilan Broz int max_size = 0; 1730f6fccb12SMilan Broz 1731f6fccb12SMilan Broz if (unlikely(!map)) 1732f6fccb12SMilan Broz goto out; 1733f6fccb12SMilan Broz 1734f6fccb12SMilan Broz ti = dm_table_find_target(map, bvm->bi_sector); 1735f6fccb12SMilan Broz if (!dm_target_is_valid(ti)) 173683d5e5b0SMikulas Patocka goto out; 1737f6fccb12SMilan Broz 1738f6fccb12SMilan Broz /* 1739f6fccb12SMilan Broz * Find maximum amount of I/O that won't need splitting 1740f6fccb12SMilan Broz */ 174156a67df7SMike Snitzer max_sectors = min(max_io_len(bvm->bi_sector, ti), 1742148e51baSMike Snitzer (sector_t) queue_max_sectors(q)); 1743f6fccb12SMilan Broz max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size; 1744148e51baSMike Snitzer if (unlikely(max_size < 0)) /* this shouldn't _ever_ happen */ 1745f6fccb12SMilan Broz max_size = 0; 1746f6fccb12SMilan Broz 1747f6fccb12SMilan Broz /* 1748f6fccb12SMilan Broz * merge_bvec_fn() returns number of bytes 1749f6fccb12SMilan Broz * it can accept at this offset 1750f6fccb12SMilan Broz * max is precomputed maximal io size 1751f6fccb12SMilan Broz */ 1752f6fccb12SMilan Broz if (max_size && ti->type->merge) 1753f6fccb12SMilan Broz max_size = ti->type->merge(ti, bvm, biovec, max_size); 17548cbeb67aSMikulas Patocka /* 17558cbeb67aSMikulas Patocka * If the target doesn't support merge method and some of the devices 1756148e51baSMike Snitzer * provided their merge_bvec method (we know this by looking for the 1757148e51baSMike Snitzer * max_hw_sectors that dm_set_device_limits may set), then we can't 1758148e51baSMike Snitzer * allow bios with multiple vector entries. So always set max_size 1759148e51baSMike Snitzer * to 0, and the code below allows just one page. 17608cbeb67aSMikulas Patocka */ 17618cbeb67aSMikulas Patocka else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9) 17628cbeb67aSMikulas Patocka max_size = 0; 1763f6fccb12SMilan Broz 17645037108aSMikulas Patocka out: 176583d5e5b0SMikulas Patocka dm_put_live_table_fast(md); 1766f6fccb12SMilan Broz /* 1767f6fccb12SMilan Broz * Always allow an entire first page 1768f6fccb12SMilan Broz */ 1769f6fccb12SMilan Broz if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT)) 1770f6fccb12SMilan Broz max_size = biovec->bv_len; 1771f6fccb12SMilan Broz 1772f6fccb12SMilan Broz return max_size; 1773f6fccb12SMilan Broz } 1774f6fccb12SMilan Broz 17751da177e4SLinus Torvalds /* 17761da177e4SLinus Torvalds * The request function that just remaps the bio built up by 17771da177e4SLinus Torvalds * dm_merge_bvec. 17781da177e4SLinus Torvalds */ 1779ff36ab34SMike Snitzer static void dm_make_request(struct request_queue *q, struct bio *bio) 17801da177e4SLinus Torvalds { 178112f03a49SKevin Corry int rw = bio_data_dir(bio); 17821da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 178383d5e5b0SMikulas Patocka int srcu_idx; 178483d5e5b0SMikulas Patocka struct dm_table *map; 17851da177e4SLinus Torvalds 178683d5e5b0SMikulas Patocka map = dm_get_live_table(md, &srcu_idx); 17871da177e4SLinus Torvalds 178818c0b223SGu Zheng generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0); 178912f03a49SKevin Corry 17906a8736d1STejun Heo /* if we're suspended, we have to queue this io for later */ 17916a8736d1STejun Heo if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) { 179283d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 17931da177e4SLinus Torvalds 17946a8736d1STejun Heo if (bio_rw(bio) != READA) 179592c63902SMikulas Patocka queue_io(md, bio); 17966a8736d1STejun Heo else 17976a8736d1STejun Heo bio_io_error(bio); 17985a7bbad2SChristoph Hellwig return; 17991da177e4SLinus Torvalds } 18001da177e4SLinus Torvalds 180183d5e5b0SMikulas Patocka __split_and_process_bio(md, map, bio); 180283d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 18035a7bbad2SChristoph Hellwig return; 1804cec47e3dSKiyoshi Ueda } 1805cec47e3dSKiyoshi Ueda 1806fd2ed4d2SMikulas Patocka int dm_request_based(struct mapped_device *md) 1807cec47e3dSKiyoshi Ueda { 1808cec47e3dSKiyoshi Ueda return blk_queue_stackable(md->queue); 1809cec47e3dSKiyoshi Ueda } 1810cec47e3dSKiyoshi Ueda 1811466d89a6SKeith Busch static void dm_dispatch_clone_request(struct request *clone, struct request *rq) 1812cec47e3dSKiyoshi Ueda { 1813cec47e3dSKiyoshi Ueda int r; 1814cec47e3dSKiyoshi Ueda 1815466d89a6SKeith Busch if (blk_queue_io_stat(clone->q)) 1816466d89a6SKeith Busch clone->cmd_flags |= REQ_IO_STAT; 1817cec47e3dSKiyoshi Ueda 1818466d89a6SKeith Busch clone->start_time = jiffies; 1819466d89a6SKeith Busch r = blk_insert_cloned_request(clone->q, clone); 1820cec47e3dSKiyoshi Ueda if (r) 1821466d89a6SKeith Busch /* must complete clone in terms of original request */ 1822cec47e3dSKiyoshi Ueda dm_complete_request(rq, r); 1823cec47e3dSKiyoshi Ueda } 1824cec47e3dSKiyoshi Ueda 1825cec47e3dSKiyoshi Ueda static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig, 1826cec47e3dSKiyoshi Ueda void *data) 1827cec47e3dSKiyoshi Ueda { 1828cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio = data; 182994818742SKent Overstreet struct dm_rq_clone_bio_info *info = 183094818742SKent Overstreet container_of(bio, struct dm_rq_clone_bio_info, clone); 1831cec47e3dSKiyoshi Ueda 1832cec47e3dSKiyoshi Ueda info->orig = bio_orig; 1833cec47e3dSKiyoshi Ueda info->tio = tio; 1834cec47e3dSKiyoshi Ueda bio->bi_end_io = end_clone_bio; 1835cec47e3dSKiyoshi Ueda 1836cec47e3dSKiyoshi Ueda return 0; 1837cec47e3dSKiyoshi Ueda } 1838cec47e3dSKiyoshi Ueda 1839cec47e3dSKiyoshi Ueda static int setup_clone(struct request *clone, struct request *rq, 18401ae49ea2SMike Snitzer struct dm_rq_target_io *tio, gfp_t gfp_mask) 1841cec47e3dSKiyoshi Ueda { 1842d0bcb878SKiyoshi Ueda int r; 1843cec47e3dSKiyoshi Ueda 18441ae49ea2SMike Snitzer r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask, 1845d0bcb878SKiyoshi Ueda dm_rq_bio_constructor, tio); 1846cec47e3dSKiyoshi Ueda if (r) 1847cec47e3dSKiyoshi Ueda return r; 1848cec47e3dSKiyoshi Ueda 1849cec47e3dSKiyoshi Ueda clone->cmd = rq->cmd; 1850cec47e3dSKiyoshi Ueda clone->cmd_len = rq->cmd_len; 1851cec47e3dSKiyoshi Ueda clone->sense = rq->sense; 1852cec47e3dSKiyoshi Ueda clone->end_io = end_clone_request; 1853cec47e3dSKiyoshi Ueda clone->end_io_data = tio; 1854cec47e3dSKiyoshi Ueda 18551ae49ea2SMike Snitzer tio->clone = clone; 18561ae49ea2SMike Snitzer 1857cec47e3dSKiyoshi Ueda return 0; 1858cec47e3dSKiyoshi Ueda } 1859cec47e3dSKiyoshi Ueda 18606facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md, 18611ae49ea2SMike Snitzer struct dm_rq_target_io *tio, gfp_t gfp_mask) 18626facdaffSKiyoshi Ueda { 186302233342SMike Snitzer /* 186402233342SMike Snitzer * Do not allocate a clone if tio->clone was already set 186502233342SMike Snitzer * (see: dm_mq_queue_rq). 186602233342SMike Snitzer */ 186702233342SMike Snitzer bool alloc_clone = !tio->clone; 186802233342SMike Snitzer struct request *clone; 18691ae49ea2SMike Snitzer 187002233342SMike Snitzer if (alloc_clone) { 187102233342SMike Snitzer clone = alloc_clone_request(md, gfp_mask); 18721ae49ea2SMike Snitzer if (!clone) 18731ae49ea2SMike Snitzer return NULL; 187402233342SMike Snitzer } else 187502233342SMike Snitzer clone = tio->clone; 18761ae49ea2SMike Snitzer 18771ae49ea2SMike Snitzer blk_rq_init(NULL, clone); 18781ae49ea2SMike Snitzer if (setup_clone(clone, rq, tio, gfp_mask)) { 18791ae49ea2SMike Snitzer /* -ENOMEM */ 188002233342SMike Snitzer if (alloc_clone) 18811ae49ea2SMike Snitzer free_clone_request(md, clone); 18821ae49ea2SMike Snitzer return NULL; 18831ae49ea2SMike Snitzer } 18841ae49ea2SMike Snitzer 18851ae49ea2SMike Snitzer return clone; 18861ae49ea2SMike Snitzer } 18871ae49ea2SMike Snitzer 18882eb6e1e3SKeith Busch static void map_tio_request(struct kthread_work *work); 18892eb6e1e3SKeith Busch 1890bfebd1cdSMike Snitzer static void init_tio(struct dm_rq_target_io *tio, struct request *rq, 1891bfebd1cdSMike Snitzer struct mapped_device *md) 1892bfebd1cdSMike Snitzer { 1893bfebd1cdSMike Snitzer tio->md = md; 1894bfebd1cdSMike Snitzer tio->ti = NULL; 1895bfebd1cdSMike Snitzer tio->clone = NULL; 1896bfebd1cdSMike Snitzer tio->orig = rq; 1897bfebd1cdSMike Snitzer tio->error = 0; 1898bfebd1cdSMike Snitzer memset(&tio->info, 0, sizeof(tio->info)); 189902233342SMike Snitzer if (md->kworker_task) 1900bfebd1cdSMike Snitzer init_kthread_work(&tio->work, map_tio_request); 1901bfebd1cdSMike Snitzer } 1902bfebd1cdSMike Snitzer 1903466d89a6SKeith Busch static struct dm_rq_target_io *prep_tio(struct request *rq, 1904466d89a6SKeith Busch struct mapped_device *md, gfp_t gfp_mask) 19056facdaffSKiyoshi Ueda { 19066facdaffSKiyoshi Ueda struct dm_rq_target_io *tio; 1907e5863d9aSMike Snitzer int srcu_idx; 1908e5863d9aSMike Snitzer struct dm_table *table; 19096facdaffSKiyoshi Ueda 19106facdaffSKiyoshi Ueda tio = alloc_rq_tio(md, gfp_mask); 19116facdaffSKiyoshi Ueda if (!tio) 19126facdaffSKiyoshi Ueda return NULL; 19136facdaffSKiyoshi Ueda 1914bfebd1cdSMike Snitzer init_tio(tio, rq, md); 19156facdaffSKiyoshi Ueda 1916e5863d9aSMike Snitzer table = dm_get_live_table(md, &srcu_idx); 1917e5863d9aSMike Snitzer if (!dm_table_mq_request_based(table)) { 1918466d89a6SKeith Busch if (!clone_rq(rq, md, tio, gfp_mask)) { 1919e5863d9aSMike Snitzer dm_put_live_table(md, srcu_idx); 19206facdaffSKiyoshi Ueda free_rq_tio(tio); 19216facdaffSKiyoshi Ueda return NULL; 19226facdaffSKiyoshi Ueda } 1923e5863d9aSMike Snitzer } 1924e5863d9aSMike Snitzer dm_put_live_table(md, srcu_idx); 19256facdaffSKiyoshi Ueda 1926466d89a6SKeith Busch return tio; 19276facdaffSKiyoshi Ueda } 19286facdaffSKiyoshi Ueda 1929cec47e3dSKiyoshi Ueda /* 1930cec47e3dSKiyoshi Ueda * Called with the queue lock held. 1931cec47e3dSKiyoshi Ueda */ 1932cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq) 1933cec47e3dSKiyoshi Ueda { 1934cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 1935466d89a6SKeith Busch struct dm_rq_target_io *tio; 1936cec47e3dSKiyoshi Ueda 1937cec47e3dSKiyoshi Ueda if (unlikely(rq->special)) { 1938cec47e3dSKiyoshi Ueda DMWARN("Already has something in rq->special."); 1939cec47e3dSKiyoshi Ueda return BLKPREP_KILL; 1940cec47e3dSKiyoshi Ueda } 1941cec47e3dSKiyoshi Ueda 1942466d89a6SKeith Busch tio = prep_tio(rq, md, GFP_ATOMIC); 1943466d89a6SKeith Busch if (!tio) 1944cec47e3dSKiyoshi Ueda return BLKPREP_DEFER; 1945cec47e3dSKiyoshi Ueda 1946466d89a6SKeith Busch rq->special = tio; 1947cec47e3dSKiyoshi Ueda rq->cmd_flags |= REQ_DONTPREP; 1948cec47e3dSKiyoshi Ueda 1949cec47e3dSKiyoshi Ueda return BLKPREP_OK; 1950cec47e3dSKiyoshi Ueda } 1951cec47e3dSKiyoshi Ueda 19529eef87daSKiyoshi Ueda /* 19539eef87daSKiyoshi Ueda * Returns: 1954e5863d9aSMike Snitzer * 0 : the request has been processed 1955e5863d9aSMike Snitzer * DM_MAPIO_REQUEUE : the original request needs to be requeued 1956e5863d9aSMike Snitzer * < 0 : the request was completed due to failure 19579eef87daSKiyoshi Ueda */ 1958bfebd1cdSMike Snitzer static int map_request(struct dm_rq_target_io *tio, struct request *rq, 1959cec47e3dSKiyoshi Ueda struct mapped_device *md) 1960cec47e3dSKiyoshi Ueda { 1961e5863d9aSMike Snitzer int r; 1962bfebd1cdSMike Snitzer struct dm_target *ti = tio->ti; 1963e5863d9aSMike Snitzer struct request *clone = NULL; 1964cec47e3dSKiyoshi Ueda 1965e5863d9aSMike Snitzer if (tio->clone) { 1966e5863d9aSMike Snitzer clone = tio->clone; 1967cec47e3dSKiyoshi Ueda r = ti->type->map_rq(ti, clone, &tio->info); 1968e5863d9aSMike Snitzer } else { 1969e5863d9aSMike Snitzer r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone); 1970e5863d9aSMike Snitzer if (r < 0) { 1971e5863d9aSMike Snitzer /* The target wants to complete the I/O */ 1972e5863d9aSMike Snitzer dm_kill_unmapped_request(rq, r); 1973e5863d9aSMike Snitzer return r; 1974e5863d9aSMike Snitzer } 19753a140755SJunichi Nomura if (r != DM_MAPIO_REMAPPED) 19763a140755SJunichi Nomura return r; 197702233342SMike Snitzer if (setup_clone(clone, rq, tio, GFP_ATOMIC)) { 1978e5863d9aSMike Snitzer /* -ENOMEM */ 1979e5863d9aSMike Snitzer ti->type->release_clone_rq(clone); 1980e5863d9aSMike Snitzer return DM_MAPIO_REQUEUE; 1981e5863d9aSMike Snitzer } 1982e5863d9aSMike Snitzer } 1983e5863d9aSMike Snitzer 1984cec47e3dSKiyoshi Ueda switch (r) { 1985cec47e3dSKiyoshi Ueda case DM_MAPIO_SUBMITTED: 1986cec47e3dSKiyoshi Ueda /* The target has taken the I/O to submit by itself later */ 1987cec47e3dSKiyoshi Ueda break; 1988cec47e3dSKiyoshi Ueda case DM_MAPIO_REMAPPED: 1989cec47e3dSKiyoshi Ueda /* The target has remapped the I/O so dispatch it */ 19906db4ccd6SJun'ichi Nomura trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)), 1991466d89a6SKeith Busch blk_rq_pos(rq)); 1992466d89a6SKeith Busch dm_dispatch_clone_request(clone, rq); 1993cec47e3dSKiyoshi Ueda break; 1994cec47e3dSKiyoshi Ueda case DM_MAPIO_REQUEUE: 1995cec47e3dSKiyoshi Ueda /* The target wants to requeue the I/O */ 1996cec47e3dSKiyoshi Ueda dm_requeue_unmapped_request(clone); 1997cec47e3dSKiyoshi Ueda break; 1998cec47e3dSKiyoshi Ueda default: 1999cec47e3dSKiyoshi Ueda if (r > 0) { 2000cec47e3dSKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 2001cec47e3dSKiyoshi Ueda BUG(); 2002cec47e3dSKiyoshi Ueda } 2003cec47e3dSKiyoshi Ueda 2004cec47e3dSKiyoshi Ueda /* The target wants to complete the I/O */ 2005466d89a6SKeith Busch dm_kill_unmapped_request(rq, r); 2006e5863d9aSMike Snitzer return r; 2007cec47e3dSKiyoshi Ueda } 20089eef87daSKiyoshi Ueda 2009e5863d9aSMike Snitzer return 0; 2010cec47e3dSKiyoshi Ueda } 2011cec47e3dSKiyoshi Ueda 20122eb6e1e3SKeith Busch static void map_tio_request(struct kthread_work *work) 2013ba1cbad9SMike Snitzer { 20142eb6e1e3SKeith Busch struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work); 2015e5863d9aSMike Snitzer struct request *rq = tio->orig; 2016e5863d9aSMike Snitzer struct mapped_device *md = tio->md; 2017ba1cbad9SMike Snitzer 2018bfebd1cdSMike Snitzer if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) 2019e5863d9aSMike Snitzer dm_requeue_unmapped_original_request(md, rq); 20202eb6e1e3SKeith Busch } 20212eb6e1e3SKeith Busch 2022466d89a6SKeith Busch static void dm_start_request(struct mapped_device *md, struct request *orig) 2023ba1cbad9SMike Snitzer { 2024bfebd1cdSMike Snitzer if (!orig->q->mq_ops) 2025ba1cbad9SMike Snitzer blk_start_request(orig); 2026bfebd1cdSMike Snitzer else 2027bfebd1cdSMike Snitzer blk_mq_start_request(orig); 2028466d89a6SKeith Busch atomic_inc(&md->pending[rq_data_dir(orig)]); 2029ba1cbad9SMike Snitzer 20300ce65797SMike Snitzer if (md->seq_rq_merge_deadline_usecs) { 2031de3ec86dSMike Snitzer md->last_rq_pos = rq_end_sector(orig); 2032de3ec86dSMike Snitzer md->last_rq_rw = rq_data_dir(orig); 20330ce65797SMike Snitzer md->last_rq_start_time = ktime_get(); 20340ce65797SMike Snitzer } 2035de3ec86dSMike Snitzer 2036ba1cbad9SMike Snitzer /* 2037ba1cbad9SMike Snitzer * Hold the md reference here for the in-flight I/O. 2038ba1cbad9SMike Snitzer * We can't rely on the reference count by device opener, 2039ba1cbad9SMike Snitzer * because the device may be closed during the request completion 2040ba1cbad9SMike Snitzer * when all bios are completed. 2041ba1cbad9SMike Snitzer * See the comment in rq_completed() too. 2042ba1cbad9SMike Snitzer */ 2043ba1cbad9SMike Snitzer dm_get(md); 2044ba1cbad9SMike Snitzer } 2045ba1cbad9SMike Snitzer 20460ce65797SMike Snitzer #define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000 20470ce65797SMike Snitzer 20480ce65797SMike Snitzer ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf) 20490ce65797SMike Snitzer { 20500ce65797SMike Snitzer return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs); 20510ce65797SMike Snitzer } 20520ce65797SMike Snitzer 20530ce65797SMike Snitzer ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md, 20540ce65797SMike Snitzer const char *buf, size_t count) 20550ce65797SMike Snitzer { 20560ce65797SMike Snitzer unsigned deadline; 20570ce65797SMike Snitzer 205817e149b8SMike Snitzer if (!dm_request_based(md) || md->use_blk_mq) 20590ce65797SMike Snitzer return count; 20600ce65797SMike Snitzer 20610ce65797SMike Snitzer if (kstrtouint(buf, 10, &deadline)) 20620ce65797SMike Snitzer return -EINVAL; 20630ce65797SMike Snitzer 20640ce65797SMike Snitzer if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS) 20650ce65797SMike Snitzer deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS; 20660ce65797SMike Snitzer 20670ce65797SMike Snitzer md->seq_rq_merge_deadline_usecs = deadline; 20680ce65797SMike Snitzer 20690ce65797SMike Snitzer return count; 20700ce65797SMike Snitzer } 20710ce65797SMike Snitzer 20720ce65797SMike Snitzer static bool dm_request_peeked_before_merge_deadline(struct mapped_device *md) 20730ce65797SMike Snitzer { 20740ce65797SMike Snitzer ktime_t kt_deadline; 20750ce65797SMike Snitzer 20760ce65797SMike Snitzer if (!md->seq_rq_merge_deadline_usecs) 20770ce65797SMike Snitzer return false; 20780ce65797SMike Snitzer 20790ce65797SMike Snitzer kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC); 20800ce65797SMike Snitzer kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline); 20810ce65797SMike Snitzer 20820ce65797SMike Snitzer return !ktime_after(ktime_get(), kt_deadline); 20830ce65797SMike Snitzer } 20840ce65797SMike Snitzer 2085cec47e3dSKiyoshi Ueda /* 2086cec47e3dSKiyoshi Ueda * q->request_fn for request-based dm. 2087cec47e3dSKiyoshi Ueda * Called with the queue lock held. 2088cec47e3dSKiyoshi Ueda */ 2089cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q) 2090cec47e3dSKiyoshi Ueda { 2091cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 209283d5e5b0SMikulas Patocka int srcu_idx; 209383d5e5b0SMikulas Patocka struct dm_table *map = dm_get_live_table(md, &srcu_idx); 2094cec47e3dSKiyoshi Ueda struct dm_target *ti; 2095466d89a6SKeith Busch struct request *rq; 20962eb6e1e3SKeith Busch struct dm_rq_target_io *tio; 209729e4013dSTejun Heo sector_t pos; 2098cec47e3dSKiyoshi Ueda 2099cec47e3dSKiyoshi Ueda /* 2100b4324feeSKiyoshi Ueda * For suspend, check blk_queue_stopped() and increment 2101b4324feeSKiyoshi Ueda * ->pending within a single queue_lock not to increment the 2102b4324feeSKiyoshi Ueda * number of in-flight I/Os after the queue is stopped in 2103b4324feeSKiyoshi Ueda * dm_suspend(). 2104cec47e3dSKiyoshi Ueda */ 21057eaceaccSJens Axboe while (!blk_queue_stopped(q)) { 2106cec47e3dSKiyoshi Ueda rq = blk_peek_request(q); 2107cec47e3dSKiyoshi Ueda if (!rq) 21089d1deb83SMike Snitzer goto out; 2109cec47e3dSKiyoshi Ueda 211029e4013dSTejun Heo /* always use block 0 to find the target for flushes for now */ 211129e4013dSTejun Heo pos = 0; 211229e4013dSTejun Heo if (!(rq->cmd_flags & REQ_FLUSH)) 211329e4013dSTejun Heo pos = blk_rq_pos(rq); 2114d0bcb878SKiyoshi Ueda 211529e4013dSTejun Heo ti = dm_table_find_target(map, pos); 2116ba1cbad9SMike Snitzer if (!dm_target_is_valid(ti)) { 2117ba1cbad9SMike Snitzer /* 2118466d89a6SKeith Busch * Must perform setup, that rq_completed() requires, 2119ba1cbad9SMike Snitzer * before calling dm_kill_unmapped_request 2120ba1cbad9SMike Snitzer */ 2121ba1cbad9SMike Snitzer DMERR_LIMIT("request attempted access beyond the end of device"); 2122466d89a6SKeith Busch dm_start_request(md, rq); 2123466d89a6SKeith Busch dm_kill_unmapped_request(rq, -EIO); 2124ba1cbad9SMike Snitzer continue; 2125ba1cbad9SMike Snitzer } 212629e4013dSTejun Heo 21270ce65797SMike Snitzer if (dm_request_peeked_before_merge_deadline(md) && 21280ce65797SMike Snitzer md_in_flight(md) && rq->bio && rq->bio->bi_vcnt == 1 && 2129de3ec86dSMike Snitzer md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq)) 2130de3ec86dSMike Snitzer goto delay_and_out; 2131de3ec86dSMike Snitzer 2132cec47e3dSKiyoshi Ueda if (ti->type->busy && ti->type->busy(ti)) 21337eaceaccSJens Axboe goto delay_and_out; 2134cec47e3dSKiyoshi Ueda 2135466d89a6SKeith Busch dm_start_request(md, rq); 2136b4324feeSKiyoshi Ueda 2137bfebd1cdSMike Snitzer tio = tio_from_request(rq); 21382eb6e1e3SKeith Busch /* Establish tio->ti before queuing work (map_tio_request) */ 21392eb6e1e3SKeith Busch tio->ti = ti; 21402eb6e1e3SKeith Busch queue_kthread_work(&md->kworker, &tio->work); 2141052189a2SKiyoshi Ueda BUG_ON(!irqs_disabled()); 2142cec47e3dSKiyoshi Ueda } 2143cec47e3dSKiyoshi Ueda 2144cec47e3dSKiyoshi Ueda goto out; 2145cec47e3dSKiyoshi Ueda 21467eaceaccSJens Axboe delay_and_out: 2147d548b34bSMike Snitzer blk_delay_queue(q, HZ / 100); 2148cec47e3dSKiyoshi Ueda out: 214983d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 2150cec47e3dSKiyoshi Ueda } 2151cec47e3dSKiyoshi Ueda 21521da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits) 21531da177e4SLinus Torvalds { 21548a57dfc6SChandra Seetharaman int r = bdi_bits; 21558a57dfc6SChandra Seetharaman struct mapped_device *md = congested_data; 21568a57dfc6SChandra Seetharaman struct dm_table *map; 21571da177e4SLinus Torvalds 21581eb787ecSAlasdair G Kergon if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 215983d5e5b0SMikulas Patocka map = dm_get_live_table_fast(md); 21608a57dfc6SChandra Seetharaman if (map) { 2161cec47e3dSKiyoshi Ueda /* 2162cec47e3dSKiyoshi Ueda * Request-based dm cares about only own queue for 2163cec47e3dSKiyoshi Ueda * the query about congestion status of request_queue 2164cec47e3dSKiyoshi Ueda */ 2165cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 2166cec47e3dSKiyoshi Ueda r = md->queue->backing_dev_info.state & 2167cec47e3dSKiyoshi Ueda bdi_bits; 2168cec47e3dSKiyoshi Ueda else 21691da177e4SLinus Torvalds r = dm_table_any_congested(map, bdi_bits); 21708a57dfc6SChandra Seetharaman } 217183d5e5b0SMikulas Patocka dm_put_live_table_fast(md); 21728a57dfc6SChandra Seetharaman } 21738a57dfc6SChandra Seetharaman 21741da177e4SLinus Torvalds return r; 21751da177e4SLinus Torvalds } 21761da177e4SLinus Torvalds 21771da177e4SLinus Torvalds /*----------------------------------------------------------------- 21781da177e4SLinus Torvalds * An IDR is used to keep track of allocated minor numbers. 21791da177e4SLinus Torvalds *---------------------------------------------------------------*/ 21802b06cfffSAlasdair G Kergon static void free_minor(int minor) 21811da177e4SLinus Torvalds { 2182f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 21831da177e4SLinus Torvalds idr_remove(&_minor_idr, minor); 2184f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 21851da177e4SLinus Torvalds } 21861da177e4SLinus Torvalds 21871da177e4SLinus Torvalds /* 21881da177e4SLinus Torvalds * See if the device with a specific minor # is free. 21891da177e4SLinus Torvalds */ 2190cf13ab8eSFrederik Deweerdt static int specific_minor(int minor) 21911da177e4SLinus Torvalds { 2192c9d76be6STejun Heo int r; 21931da177e4SLinus Torvalds 21941da177e4SLinus Torvalds if (minor >= (1 << MINORBITS)) 21951da177e4SLinus Torvalds return -EINVAL; 21961da177e4SLinus Torvalds 2197c9d76be6STejun Heo idr_preload(GFP_KERNEL); 2198f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 21991da177e4SLinus Torvalds 2200c9d76be6STejun Heo r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT); 22011da177e4SLinus Torvalds 2202f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 2203c9d76be6STejun Heo idr_preload_end(); 2204c9d76be6STejun Heo if (r < 0) 2205c9d76be6STejun Heo return r == -ENOSPC ? -EBUSY : r; 2206c9d76be6STejun Heo return 0; 22071da177e4SLinus Torvalds } 22081da177e4SLinus Torvalds 2209cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor) 22101da177e4SLinus Torvalds { 2211c9d76be6STejun Heo int r; 22121da177e4SLinus Torvalds 2213c9d76be6STejun Heo idr_preload(GFP_KERNEL); 2214f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 22151da177e4SLinus Torvalds 2216c9d76be6STejun Heo r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT); 22171da177e4SLinus Torvalds 2218f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 2219c9d76be6STejun Heo idr_preload_end(); 2220c9d76be6STejun Heo if (r < 0) 22211da177e4SLinus Torvalds return r; 2222c9d76be6STejun Heo *minor = r; 2223c9d76be6STejun Heo return 0; 22241da177e4SLinus Torvalds } 22251da177e4SLinus Torvalds 222683d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops; 22271da177e4SLinus Torvalds 222853d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work); 222953d5914fSMikulas Patocka 22304a0b4ddfSMike Snitzer static void dm_init_md_queue(struct mapped_device *md) 22314a0b4ddfSMike Snitzer { 22324a0b4ddfSMike Snitzer /* 22334a0b4ddfSMike Snitzer * Request-based dm devices cannot be stacked on top of bio-based dm 2234bfebd1cdSMike Snitzer * devices. The type of this dm device may not have been decided yet. 22354a0b4ddfSMike Snitzer * The type is decided at the first table loading time. 22364a0b4ddfSMike Snitzer * To prevent problematic device stacking, clear the queue flag 22374a0b4ddfSMike Snitzer * for request stacking support until then. 22384a0b4ddfSMike Snitzer * 22394a0b4ddfSMike Snitzer * This queue is new, so no concurrency on the queue_flags. 22404a0b4ddfSMike Snitzer */ 22414a0b4ddfSMike Snitzer queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue); 2242bfebd1cdSMike Snitzer } 22434a0b4ddfSMike Snitzer 2244bfebd1cdSMike Snitzer static void dm_init_old_md_queue(struct mapped_device *md) 2245bfebd1cdSMike Snitzer { 224617e149b8SMike Snitzer md->use_blk_mq = false; 2247bfebd1cdSMike Snitzer dm_init_md_queue(md); 2248bfebd1cdSMike Snitzer 2249bfebd1cdSMike Snitzer /* 2250bfebd1cdSMike Snitzer * Initialize aspects of queue that aren't relevant for blk-mq 2251bfebd1cdSMike Snitzer */ 22524a0b4ddfSMike Snitzer md->queue->queuedata = md; 22534a0b4ddfSMike Snitzer md->queue->backing_dev_info.congested_fn = dm_any_congested; 22544a0b4ddfSMike Snitzer md->queue->backing_dev_info.congested_data = md; 2255ff36ab34SMike Snitzer 22564a0b4ddfSMike Snitzer blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY); 22574a0b4ddfSMike Snitzer } 22584a0b4ddfSMike Snitzer 22591da177e4SLinus Torvalds /* 22601da177e4SLinus Torvalds * Allocate and initialise a blank device with a given minor. 22611da177e4SLinus Torvalds */ 22622b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor) 22631da177e4SLinus Torvalds { 22641da177e4SLinus Torvalds int r; 2265cf13ab8eSFrederik Deweerdt struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL); 2266ba61fdd1SJeff Mahoney void *old_md; 22671da177e4SLinus Torvalds 22681da177e4SLinus Torvalds if (!md) { 22691da177e4SLinus Torvalds DMWARN("unable to allocate device, out of memory."); 22701da177e4SLinus Torvalds return NULL; 22711da177e4SLinus Torvalds } 22721da177e4SLinus Torvalds 227310da4f79SJeff Mahoney if (!try_module_get(THIS_MODULE)) 22746ed7ade8SMilan Broz goto bad_module_get; 227510da4f79SJeff Mahoney 22761da177e4SLinus Torvalds /* get a minor number for the dev */ 22772b06cfffSAlasdair G Kergon if (minor == DM_ANY_MINOR) 2278cf13ab8eSFrederik Deweerdt r = next_free_minor(&minor); 22792b06cfffSAlasdair G Kergon else 2280cf13ab8eSFrederik Deweerdt r = specific_minor(minor); 22811da177e4SLinus Torvalds if (r < 0) 22826ed7ade8SMilan Broz goto bad_minor; 22831da177e4SLinus Torvalds 228483d5e5b0SMikulas Patocka r = init_srcu_struct(&md->io_barrier); 228583d5e5b0SMikulas Patocka if (r < 0) 228683d5e5b0SMikulas Patocka goto bad_io_barrier; 228783d5e5b0SMikulas Patocka 228817e149b8SMike Snitzer md->use_blk_mq = use_blk_mq; 2289a5664dadSMike Snitzer md->type = DM_TYPE_NONE; 2290e61290a4SDaniel Walker mutex_init(&md->suspend_lock); 2291a5664dadSMike Snitzer mutex_init(&md->type_lock); 229286f1152bSBenjamin Marzinski mutex_init(&md->table_devices_lock); 2293022c2611SMikulas Patocka spin_lock_init(&md->deferred_lock); 22941da177e4SLinus Torvalds atomic_set(&md->holders, 1); 22955c6bd75dSAlasdair G Kergon atomic_set(&md->open_count, 0); 22961da177e4SLinus Torvalds atomic_set(&md->event_nr, 0); 22977a8c3d3bSMike Anderson atomic_set(&md->uevent_seq, 0); 22987a8c3d3bSMike Anderson INIT_LIST_HEAD(&md->uevent_list); 229986f1152bSBenjamin Marzinski INIT_LIST_HEAD(&md->table_devices); 23007a8c3d3bSMike Anderson spin_lock_init(&md->uevent_lock); 23011da177e4SLinus Torvalds 23024a0b4ddfSMike Snitzer md->queue = blk_alloc_queue(GFP_KERNEL); 23031da177e4SLinus Torvalds if (!md->queue) 23046ed7ade8SMilan Broz goto bad_queue; 23051da177e4SLinus Torvalds 23064a0b4ddfSMike Snitzer dm_init_md_queue(md); 23079faf400fSStefan Bader 23081da177e4SLinus Torvalds md->disk = alloc_disk(1); 23091da177e4SLinus Torvalds if (!md->disk) 23106ed7ade8SMilan Broz goto bad_disk; 23111da177e4SLinus Torvalds 2312316d315bSNikanth Karthikesan atomic_set(&md->pending[0], 0); 2313316d315bSNikanth Karthikesan atomic_set(&md->pending[1], 0); 2314f0b04115SJeff Mahoney init_waitqueue_head(&md->wait); 231553d5914fSMikulas Patocka INIT_WORK(&md->work, dm_wq_work); 2316f0b04115SJeff Mahoney init_waitqueue_head(&md->eventq); 23172995fa78SMikulas Patocka init_completion(&md->kobj_holder.completion); 23182eb6e1e3SKeith Busch md->kworker_task = NULL; 2319f0b04115SJeff Mahoney 23201da177e4SLinus Torvalds md->disk->major = _major; 23211da177e4SLinus Torvalds md->disk->first_minor = minor; 23221da177e4SLinus Torvalds md->disk->fops = &dm_blk_dops; 23231da177e4SLinus Torvalds md->disk->queue = md->queue; 23241da177e4SLinus Torvalds md->disk->private_data = md; 23251da177e4SLinus Torvalds sprintf(md->disk->disk_name, "dm-%d", minor); 23261da177e4SLinus Torvalds add_disk(md->disk); 23277e51f257SMike Anderson format_dev_t(md->name, MKDEV(_major, minor)); 23281da177e4SLinus Torvalds 2329670368a8STejun Heo md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0); 2330304f3f6aSMilan Broz if (!md->wq) 2331304f3f6aSMilan Broz goto bad_thread; 2332304f3f6aSMilan Broz 233332a926daSMikulas Patocka md->bdev = bdget_disk(md->disk, 0); 233432a926daSMikulas Patocka if (!md->bdev) 233532a926daSMikulas Patocka goto bad_bdev; 233632a926daSMikulas Patocka 23376a8736d1STejun Heo bio_init(&md->flush_bio); 23386a8736d1STejun Heo md->flush_bio.bi_bdev = md->bdev; 23396a8736d1STejun Heo md->flush_bio.bi_rw = WRITE_FLUSH; 23406a8736d1STejun Heo 2341fd2ed4d2SMikulas Patocka dm_stats_init(&md->stats); 2342fd2ed4d2SMikulas Patocka 2343ba61fdd1SJeff Mahoney /* Populate the mapping, nobody knows we exist yet */ 2344f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 2345ba61fdd1SJeff Mahoney old_md = idr_replace(&_minor_idr, md, minor); 2346f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 2347ba61fdd1SJeff Mahoney 2348ba61fdd1SJeff Mahoney BUG_ON(old_md != MINOR_ALLOCED); 2349ba61fdd1SJeff Mahoney 23501da177e4SLinus Torvalds return md; 23511da177e4SLinus Torvalds 235232a926daSMikulas Patocka bad_bdev: 235332a926daSMikulas Patocka destroy_workqueue(md->wq); 2354304f3f6aSMilan Broz bad_thread: 235503022c54SZdenek Kabelac del_gendisk(md->disk); 2356304f3f6aSMilan Broz put_disk(md->disk); 23576ed7ade8SMilan Broz bad_disk: 23581312f40eSAl Viro blk_cleanup_queue(md->queue); 23596ed7ade8SMilan Broz bad_queue: 236083d5e5b0SMikulas Patocka cleanup_srcu_struct(&md->io_barrier); 236183d5e5b0SMikulas Patocka bad_io_barrier: 23621da177e4SLinus Torvalds free_minor(minor); 23636ed7ade8SMilan Broz bad_minor: 236410da4f79SJeff Mahoney module_put(THIS_MODULE); 23656ed7ade8SMilan Broz bad_module_get: 23661da177e4SLinus Torvalds kfree(md); 23671da177e4SLinus Torvalds return NULL; 23681da177e4SLinus Torvalds } 23691da177e4SLinus Torvalds 2370ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md); 2371ae9da83fSJun'ichi Nomura 23721da177e4SLinus Torvalds static void free_dev(struct mapped_device *md) 23731da177e4SLinus Torvalds { 2374f331c029STejun Heo int minor = MINOR(disk_devt(md->disk)); 237563d94e48SJun'ichi Nomura 2376ae9da83fSJun'ichi Nomura unlock_fs(md); 2377304f3f6aSMilan Broz destroy_workqueue(md->wq); 23782eb6e1e3SKeith Busch 23792eb6e1e3SKeith Busch if (md->kworker_task) 23802eb6e1e3SKeith Busch kthread_stop(md->kworker_task); 2381e6ee8c0bSKiyoshi Ueda if (md->io_pool) 23821da177e4SLinus Torvalds mempool_destroy(md->io_pool); 23831ae49ea2SMike Snitzer if (md->rq_pool) 23841ae49ea2SMike Snitzer mempool_destroy(md->rq_pool); 2385e6ee8c0bSKiyoshi Ueda if (md->bs) 23869faf400fSStefan Bader bioset_free(md->bs); 238763a4f065SMike Snitzer 238883d5e5b0SMikulas Patocka cleanup_srcu_struct(&md->io_barrier); 238986f1152bSBenjamin Marzinski free_table_devices(&md->table_devices); 239063a4f065SMike Snitzer dm_stats_cleanup(&md->stats); 2391fba9f90eSJeff Mahoney 2392fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 2393fba9f90eSJeff Mahoney md->disk->private_data = NULL; 2394fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 239563a4f065SMike Snitzer if (blk_get_integrity(md->disk)) 239663a4f065SMike Snitzer blk_integrity_unregister(md->disk); 239763a4f065SMike Snitzer del_gendisk(md->disk); 23981da177e4SLinus Torvalds put_disk(md->disk); 23991312f40eSAl Viro blk_cleanup_queue(md->queue); 240017e149b8SMike Snitzer if (md->use_blk_mq) 2401bfebd1cdSMike Snitzer blk_mq_free_tag_set(&md->tag_set); 240263a4f065SMike Snitzer bdput(md->bdev); 240363a4f065SMike Snitzer free_minor(minor); 240463a4f065SMike Snitzer 240510da4f79SJeff Mahoney module_put(THIS_MODULE); 24061da177e4SLinus Torvalds kfree(md); 24071da177e4SLinus Torvalds } 24081da177e4SLinus Torvalds 2409e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t) 2410e6ee8c0bSKiyoshi Ueda { 2411c0820cf5SMikulas Patocka struct dm_md_mempools *p = dm_table_get_md_mempools(t); 2412e6ee8c0bSKiyoshi Ueda 241317e149b8SMike Snitzer if (md->bs) { 241416245bdcSJun'ichi Nomura /* The md already has necessary mempools. */ 241516245bdcSJun'ichi Nomura if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) { 2416c0820cf5SMikulas Patocka /* 241716245bdcSJun'ichi Nomura * Reload bioset because front_pad may have changed 241816245bdcSJun'ichi Nomura * because a different table was loaded. 2419c0820cf5SMikulas Patocka */ 2420c0820cf5SMikulas Patocka bioset_free(md->bs); 2421c0820cf5SMikulas Patocka md->bs = p->bs; 2422c0820cf5SMikulas Patocka p->bs = NULL; 2423466d89a6SKeith Busch } 242416245bdcSJun'ichi Nomura /* 242516245bdcSJun'ichi Nomura * There's no need to reload with request-based dm 242616245bdcSJun'ichi Nomura * because the size of front_pad doesn't change. 242716245bdcSJun'ichi Nomura * Note for future: If you are to reload bioset, 242816245bdcSJun'ichi Nomura * prep-ed requests in the queue may refer 242916245bdcSJun'ichi Nomura * to bio from the old bioset, so you must walk 243016245bdcSJun'ichi Nomura * through the queue to unprep. 243116245bdcSJun'ichi Nomura */ 2432e6ee8c0bSKiyoshi Ueda goto out; 2433c0820cf5SMikulas Patocka } 2434e6ee8c0bSKiyoshi Ueda 24351ae49ea2SMike Snitzer BUG_ON(!p || md->io_pool || md->rq_pool || md->bs); 2436e6ee8c0bSKiyoshi Ueda 2437e6ee8c0bSKiyoshi Ueda md->io_pool = p->io_pool; 2438e6ee8c0bSKiyoshi Ueda p->io_pool = NULL; 24391ae49ea2SMike Snitzer md->rq_pool = p->rq_pool; 24401ae49ea2SMike Snitzer p->rq_pool = NULL; 2441e6ee8c0bSKiyoshi Ueda md->bs = p->bs; 2442e6ee8c0bSKiyoshi Ueda p->bs = NULL; 2443e6ee8c0bSKiyoshi Ueda 2444e6ee8c0bSKiyoshi Ueda out: 244502233342SMike Snitzer /* mempool bind completed, no longer need any mempools in the table */ 2446e6ee8c0bSKiyoshi Ueda dm_table_free_md_mempools(t); 2447e6ee8c0bSKiyoshi Ueda } 2448e6ee8c0bSKiyoshi Ueda 24491da177e4SLinus Torvalds /* 24501da177e4SLinus Torvalds * Bind a table to the device. 24511da177e4SLinus Torvalds */ 24521da177e4SLinus Torvalds static void event_callback(void *context) 24531da177e4SLinus Torvalds { 24547a8c3d3bSMike Anderson unsigned long flags; 24557a8c3d3bSMike Anderson LIST_HEAD(uevents); 24561da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) context; 24571da177e4SLinus Torvalds 24587a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 24597a8c3d3bSMike Anderson list_splice_init(&md->uevent_list, &uevents); 24607a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 24617a8c3d3bSMike Anderson 2462ed9e1982STejun Heo dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); 24637a8c3d3bSMike Anderson 24641da177e4SLinus Torvalds atomic_inc(&md->event_nr); 24651da177e4SLinus Torvalds wake_up(&md->eventq); 24661da177e4SLinus Torvalds } 24671da177e4SLinus Torvalds 2468c217649bSMike Snitzer /* 2469c217649bSMike Snitzer * Protected by md->suspend_lock obtained by dm_swap_table(). 2470c217649bSMike Snitzer */ 24714e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size) 24721da177e4SLinus Torvalds { 24734e90188bSAlasdair G Kergon set_capacity(md->disk, size); 24741da177e4SLinus Torvalds 2475db8fef4fSMikulas Patocka i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT); 24761da177e4SLinus Torvalds } 24771da177e4SLinus Torvalds 2478042d2a9bSAlasdair G Kergon /* 2479d5b9dd04SMikulas Patocka * Return 1 if the queue has a compulsory merge_bvec_fn function. 2480d5b9dd04SMikulas Patocka * 2481d5b9dd04SMikulas Patocka * If this function returns 0, then the device is either a non-dm 2482d5b9dd04SMikulas Patocka * device without a merge_bvec_fn, or it is a dm device that is 2483d5b9dd04SMikulas Patocka * able to split any bios it receives that are too big. 2484d5b9dd04SMikulas Patocka */ 2485d5b9dd04SMikulas Patocka int dm_queue_merge_is_compulsory(struct request_queue *q) 2486d5b9dd04SMikulas Patocka { 2487d5b9dd04SMikulas Patocka struct mapped_device *dev_md; 2488d5b9dd04SMikulas Patocka 2489d5b9dd04SMikulas Patocka if (!q->merge_bvec_fn) 2490d5b9dd04SMikulas Patocka return 0; 2491d5b9dd04SMikulas Patocka 2492ff36ab34SMike Snitzer if (q->make_request_fn == dm_make_request) { 2493d5b9dd04SMikulas Patocka dev_md = q->queuedata; 2494d5b9dd04SMikulas Patocka if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags)) 2495d5b9dd04SMikulas Patocka return 0; 2496d5b9dd04SMikulas Patocka } 2497d5b9dd04SMikulas Patocka 2498d5b9dd04SMikulas Patocka return 1; 2499d5b9dd04SMikulas Patocka } 2500d5b9dd04SMikulas Patocka 2501d5b9dd04SMikulas Patocka static int dm_device_merge_is_compulsory(struct dm_target *ti, 2502d5b9dd04SMikulas Patocka struct dm_dev *dev, sector_t start, 2503d5b9dd04SMikulas Patocka sector_t len, void *data) 2504d5b9dd04SMikulas Patocka { 2505d5b9dd04SMikulas Patocka struct block_device *bdev = dev->bdev; 2506d5b9dd04SMikulas Patocka struct request_queue *q = bdev_get_queue(bdev); 2507d5b9dd04SMikulas Patocka 2508d5b9dd04SMikulas Patocka return dm_queue_merge_is_compulsory(q); 2509d5b9dd04SMikulas Patocka } 2510d5b9dd04SMikulas Patocka 2511d5b9dd04SMikulas Patocka /* 2512d5b9dd04SMikulas Patocka * Return 1 if it is acceptable to ignore merge_bvec_fn based 2513d5b9dd04SMikulas Patocka * on the properties of the underlying devices. 2514d5b9dd04SMikulas Patocka */ 2515d5b9dd04SMikulas Patocka static int dm_table_merge_is_optional(struct dm_table *table) 2516d5b9dd04SMikulas Patocka { 2517d5b9dd04SMikulas Patocka unsigned i = 0; 2518d5b9dd04SMikulas Patocka struct dm_target *ti; 2519d5b9dd04SMikulas Patocka 2520d5b9dd04SMikulas Patocka while (i < dm_table_get_num_targets(table)) { 2521d5b9dd04SMikulas Patocka ti = dm_table_get_target(table, i++); 2522d5b9dd04SMikulas Patocka 2523d5b9dd04SMikulas Patocka if (ti->type->iterate_devices && 2524d5b9dd04SMikulas Patocka ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL)) 2525d5b9dd04SMikulas Patocka return 0; 2526d5b9dd04SMikulas Patocka } 2527d5b9dd04SMikulas Patocka 2528d5b9dd04SMikulas Patocka return 1; 2529d5b9dd04SMikulas Patocka } 2530d5b9dd04SMikulas Patocka 2531d5b9dd04SMikulas Patocka /* 2532042d2a9bSAlasdair G Kergon * Returns old map, which caller must destroy. 2533042d2a9bSAlasdair G Kergon */ 2534042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t, 2535754c5fc7SMike Snitzer struct queue_limits *limits) 25361da177e4SLinus Torvalds { 2537042d2a9bSAlasdair G Kergon struct dm_table *old_map; 2538165125e1SJens Axboe struct request_queue *q = md->queue; 25391da177e4SLinus Torvalds sector_t size; 2540d5b9dd04SMikulas Patocka int merge_is_optional; 25411da177e4SLinus Torvalds 25421da177e4SLinus Torvalds size = dm_table_get_size(t); 25433ac51e74SDarrick J. Wong 25443ac51e74SDarrick J. Wong /* 25453ac51e74SDarrick J. Wong * Wipe any geometry if the size of the table changed. 25463ac51e74SDarrick J. Wong */ 2547fd2ed4d2SMikulas Patocka if (size != dm_get_size(md)) 25483ac51e74SDarrick J. Wong memset(&md->geometry, 0, sizeof(md->geometry)); 25493ac51e74SDarrick J. Wong 25504e90188bSAlasdair G Kergon __set_size(md, size); 25511da177e4SLinus Torvalds 2552cf222b37SAlasdair G Kergon dm_table_event_callback(t, event_callback, md); 25532ca3310eSAlasdair G Kergon 2554e6ee8c0bSKiyoshi Ueda /* 2555e6ee8c0bSKiyoshi Ueda * The queue hasn't been stopped yet, if the old table type wasn't 2556e6ee8c0bSKiyoshi Ueda * for request-based during suspension. So stop it to prevent 2557e6ee8c0bSKiyoshi Ueda * I/O mapping before resume. 2558e6ee8c0bSKiyoshi Ueda * This must be done before setting the queue restrictions, 2559e6ee8c0bSKiyoshi Ueda * because request-based dm may be run just after the setting. 2560e6ee8c0bSKiyoshi Ueda */ 2561bfebd1cdSMike Snitzer if (dm_table_request_based(t)) 2562e6ee8c0bSKiyoshi Ueda stop_queue(q); 2563e6ee8c0bSKiyoshi Ueda 2564e6ee8c0bSKiyoshi Ueda __bind_mempools(md, t); 2565e6ee8c0bSKiyoshi Ueda 2566d5b9dd04SMikulas Patocka merge_is_optional = dm_table_merge_is_optional(t); 2567d5b9dd04SMikulas Patocka 2568a12f5d48SEric Dumazet old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 256983d5e5b0SMikulas Patocka rcu_assign_pointer(md->map, t); 257036a0456fSAlasdair G Kergon md->immutable_target_type = dm_table_get_immutable_target_type(t); 257136a0456fSAlasdair G Kergon 2572754c5fc7SMike Snitzer dm_table_set_restrictions(t, q, limits); 2573d5b9dd04SMikulas Patocka if (merge_is_optional) 2574d5b9dd04SMikulas Patocka set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags); 2575d5b9dd04SMikulas Patocka else 2576d5b9dd04SMikulas Patocka clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags); 257741abc4e1SHannes Reinecke if (old_map) 257883d5e5b0SMikulas Patocka dm_sync_table(md); 25792ca3310eSAlasdair G Kergon 2580042d2a9bSAlasdair G Kergon return old_map; 25811da177e4SLinus Torvalds } 25821da177e4SLinus Torvalds 2583a7940155SAlasdair G Kergon /* 2584a7940155SAlasdair G Kergon * Returns unbound table for the caller to free. 2585a7940155SAlasdair G Kergon */ 2586a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md) 25871da177e4SLinus Torvalds { 2588a12f5d48SEric Dumazet struct dm_table *map = rcu_dereference_protected(md->map, 1); 25891da177e4SLinus Torvalds 25901da177e4SLinus Torvalds if (!map) 2591a7940155SAlasdair G Kergon return NULL; 25921da177e4SLinus Torvalds 25931da177e4SLinus Torvalds dm_table_event_callback(map, NULL, NULL); 25949cdb8520SMonam Agarwal RCU_INIT_POINTER(md->map, NULL); 259583d5e5b0SMikulas Patocka dm_sync_table(md); 2596a7940155SAlasdair G Kergon 2597a7940155SAlasdair G Kergon return map; 25981da177e4SLinus Torvalds } 25991da177e4SLinus Torvalds 26001da177e4SLinus Torvalds /* 26011da177e4SLinus Torvalds * Constructor for a new device. 26021da177e4SLinus Torvalds */ 26032b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result) 26041da177e4SLinus Torvalds { 26051da177e4SLinus Torvalds struct mapped_device *md; 26061da177e4SLinus Torvalds 26072b06cfffSAlasdair G Kergon md = alloc_dev(minor); 26081da177e4SLinus Torvalds if (!md) 26091da177e4SLinus Torvalds return -ENXIO; 26101da177e4SLinus Torvalds 2611784aae73SMilan Broz dm_sysfs_init(md); 2612784aae73SMilan Broz 26131da177e4SLinus Torvalds *result = md; 26141da177e4SLinus Torvalds return 0; 26151da177e4SLinus Torvalds } 26161da177e4SLinus Torvalds 2617a5664dadSMike Snitzer /* 2618a5664dadSMike Snitzer * Functions to manage md->type. 2619a5664dadSMike Snitzer * All are required to hold md->type_lock. 2620a5664dadSMike Snitzer */ 2621a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md) 2622a5664dadSMike Snitzer { 2623a5664dadSMike Snitzer mutex_lock(&md->type_lock); 2624a5664dadSMike Snitzer } 2625a5664dadSMike Snitzer 2626a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md) 2627a5664dadSMike Snitzer { 2628a5664dadSMike Snitzer mutex_unlock(&md->type_lock); 2629a5664dadSMike Snitzer } 2630a5664dadSMike Snitzer 2631a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type) 2632a5664dadSMike Snitzer { 263300c4fc3bSMike Snitzer BUG_ON(!mutex_is_locked(&md->type_lock)); 2634a5664dadSMike Snitzer md->type = type; 2635a5664dadSMike Snitzer } 2636a5664dadSMike Snitzer 2637a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md) 2638a5664dadSMike Snitzer { 263900c4fc3bSMike Snitzer BUG_ON(!mutex_is_locked(&md->type_lock)); 2640a5664dadSMike Snitzer return md->type; 2641a5664dadSMike Snitzer } 2642a5664dadSMike Snitzer 264336a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md) 264436a0456fSAlasdair G Kergon { 264536a0456fSAlasdair G Kergon return md->immutable_target_type; 264636a0456fSAlasdair G Kergon } 264736a0456fSAlasdair G Kergon 26484a0b4ddfSMike Snitzer /* 2649f84cb8a4SMike Snitzer * The queue_limits are only valid as long as you have a reference 2650f84cb8a4SMike Snitzer * count on 'md'. 2651f84cb8a4SMike Snitzer */ 2652f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md) 2653f84cb8a4SMike Snitzer { 2654f84cb8a4SMike Snitzer BUG_ON(!atomic_read(&md->holders)); 2655f84cb8a4SMike Snitzer return &md->queue->limits; 2656f84cb8a4SMike Snitzer } 2657f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits); 2658f84cb8a4SMike Snitzer 2659bfebd1cdSMike Snitzer static void init_rq_based_worker_thread(struct mapped_device *md) 2660bfebd1cdSMike Snitzer { 2661bfebd1cdSMike Snitzer /* Initialize the request-based DM worker thread */ 2662bfebd1cdSMike Snitzer init_kthread_worker(&md->kworker); 2663bfebd1cdSMike Snitzer md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker, 2664bfebd1cdSMike Snitzer "kdmwork-%s", dm_device_name(md)); 2665bfebd1cdSMike Snitzer } 2666bfebd1cdSMike Snitzer 2667f84cb8a4SMike Snitzer /* 26684a0b4ddfSMike Snitzer * Fully initialize a request-based queue (->elevator, ->request_fn, etc). 26694a0b4ddfSMike Snitzer */ 26704a0b4ddfSMike Snitzer static int dm_init_request_based_queue(struct mapped_device *md) 26714a0b4ddfSMike Snitzer { 26724a0b4ddfSMike Snitzer struct request_queue *q = NULL; 26734a0b4ddfSMike Snitzer 26744a0b4ddfSMike Snitzer /* Fully initialize the queue */ 26754a0b4ddfSMike Snitzer q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL); 26764a0b4ddfSMike Snitzer if (!q) 2677bfebd1cdSMike Snitzer return -EINVAL; 26784a0b4ddfSMike Snitzer 26790ce65797SMike Snitzer /* disable dm_request_fn's merge heuristic by default */ 26800ce65797SMike Snitzer md->seq_rq_merge_deadline_usecs = 0; 26810ce65797SMike Snitzer 26824a0b4ddfSMike Snitzer md->queue = q; 2683bfebd1cdSMike Snitzer dm_init_old_md_queue(md); 26844a0b4ddfSMike Snitzer blk_queue_softirq_done(md->queue, dm_softirq_done); 26854a0b4ddfSMike Snitzer blk_queue_prep_rq(md->queue, dm_prep_fn); 26864a0b4ddfSMike Snitzer 2687bfebd1cdSMike Snitzer init_rq_based_worker_thread(md); 26882eb6e1e3SKeith Busch 26894a0b4ddfSMike Snitzer elv_register_queue(md->queue); 26904a0b4ddfSMike Snitzer 2691bfebd1cdSMike Snitzer return 0; 2692bfebd1cdSMike Snitzer } 2693bfebd1cdSMike Snitzer 2694bfebd1cdSMike Snitzer static int dm_mq_init_request(void *data, struct request *rq, 2695bfebd1cdSMike Snitzer unsigned int hctx_idx, unsigned int request_idx, 2696bfebd1cdSMike Snitzer unsigned int numa_node) 2697bfebd1cdSMike Snitzer { 2698bfebd1cdSMike Snitzer struct mapped_device *md = data; 2699bfebd1cdSMike Snitzer struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq); 2700bfebd1cdSMike Snitzer 2701bfebd1cdSMike Snitzer /* 2702bfebd1cdSMike Snitzer * Must initialize md member of tio, otherwise it won't 2703bfebd1cdSMike Snitzer * be available in dm_mq_queue_rq. 2704bfebd1cdSMike Snitzer */ 2705bfebd1cdSMike Snitzer tio->md = md; 2706bfebd1cdSMike Snitzer 2707bfebd1cdSMike Snitzer return 0; 2708bfebd1cdSMike Snitzer } 2709bfebd1cdSMike Snitzer 2710bfebd1cdSMike Snitzer static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx, 2711bfebd1cdSMike Snitzer const struct blk_mq_queue_data *bd) 2712bfebd1cdSMike Snitzer { 2713bfebd1cdSMike Snitzer struct request *rq = bd->rq; 2714bfebd1cdSMike Snitzer struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq); 2715bfebd1cdSMike Snitzer struct mapped_device *md = tio->md; 2716bfebd1cdSMike Snitzer int srcu_idx; 2717bfebd1cdSMike Snitzer struct dm_table *map = dm_get_live_table(md, &srcu_idx); 2718bfebd1cdSMike Snitzer struct dm_target *ti; 2719bfebd1cdSMike Snitzer sector_t pos; 2720bfebd1cdSMike Snitzer 2721bfebd1cdSMike Snitzer /* always use block 0 to find the target for flushes for now */ 2722bfebd1cdSMike Snitzer pos = 0; 2723bfebd1cdSMike Snitzer if (!(rq->cmd_flags & REQ_FLUSH)) 2724bfebd1cdSMike Snitzer pos = blk_rq_pos(rq); 2725bfebd1cdSMike Snitzer 2726bfebd1cdSMike Snitzer ti = dm_table_find_target(map, pos); 2727bfebd1cdSMike Snitzer if (!dm_target_is_valid(ti)) { 2728bfebd1cdSMike Snitzer dm_put_live_table(md, srcu_idx); 2729bfebd1cdSMike Snitzer DMERR_LIMIT("request attempted access beyond the end of device"); 2730bfebd1cdSMike Snitzer /* 2731bfebd1cdSMike Snitzer * Must perform setup, that rq_completed() requires, 2732bfebd1cdSMike Snitzer * before returning BLK_MQ_RQ_QUEUE_ERROR 2733bfebd1cdSMike Snitzer */ 2734bfebd1cdSMike Snitzer dm_start_request(md, rq); 2735bfebd1cdSMike Snitzer return BLK_MQ_RQ_QUEUE_ERROR; 2736bfebd1cdSMike Snitzer } 2737bfebd1cdSMike Snitzer dm_put_live_table(md, srcu_idx); 2738bfebd1cdSMike Snitzer 2739bfebd1cdSMike Snitzer if (ti->type->busy && ti->type->busy(ti)) 2740bfebd1cdSMike Snitzer return BLK_MQ_RQ_QUEUE_BUSY; 2741bfebd1cdSMike Snitzer 2742bfebd1cdSMike Snitzer dm_start_request(md, rq); 2743bfebd1cdSMike Snitzer 2744bfebd1cdSMike Snitzer /* Init tio using md established in .init_request */ 2745bfebd1cdSMike Snitzer init_tio(tio, rq, md); 2746bfebd1cdSMike Snitzer 274702233342SMike Snitzer /* 274802233342SMike Snitzer * Establish tio->ti before queuing work (map_tio_request) 274902233342SMike Snitzer * or making direct call to map_request(). 275002233342SMike Snitzer */ 2751bfebd1cdSMike Snitzer tio->ti = ti; 275202233342SMike Snitzer 275302233342SMike Snitzer /* Clone the request if underlying devices aren't blk-mq */ 275402233342SMike Snitzer if (dm_table_get_type(map) == DM_TYPE_REQUEST_BASED) { 275502233342SMike Snitzer /* clone request is allocated at the end of the pdu */ 275602233342SMike Snitzer tio->clone = (void *)blk_mq_rq_to_pdu(rq) + sizeof(struct dm_rq_target_io); 2757*45714fbeSMike Snitzer (void) clone_rq(rq, md, tio, GFP_ATOMIC); 2758bfebd1cdSMike Snitzer queue_kthread_work(&md->kworker, &tio->work); 275902233342SMike Snitzer } else { 276002233342SMike Snitzer /* Direct call is fine since .queue_rq allows allocations */ 2761*45714fbeSMike Snitzer if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) { 2762*45714fbeSMike Snitzer /* Undo dm_start_request() before requeuing */ 2763*45714fbeSMike Snitzer rq_completed(md, rq_data_dir(rq), false); 2764*45714fbeSMike Snitzer return BLK_MQ_RQ_QUEUE_BUSY; 2765*45714fbeSMike Snitzer } 276602233342SMike Snitzer } 2767bfebd1cdSMike Snitzer 2768bfebd1cdSMike Snitzer return BLK_MQ_RQ_QUEUE_OK; 2769bfebd1cdSMike Snitzer } 2770bfebd1cdSMike Snitzer 2771bfebd1cdSMike Snitzer static struct blk_mq_ops dm_mq_ops = { 2772bfebd1cdSMike Snitzer .queue_rq = dm_mq_queue_rq, 2773bfebd1cdSMike Snitzer .map_queue = blk_mq_map_queue, 2774bfebd1cdSMike Snitzer .complete = dm_softirq_done, 2775bfebd1cdSMike Snitzer .init_request = dm_mq_init_request, 2776bfebd1cdSMike Snitzer }; 2777bfebd1cdSMike Snitzer 2778bfebd1cdSMike Snitzer static int dm_init_request_based_blk_mq_queue(struct mapped_device *md) 2779bfebd1cdSMike Snitzer { 278002233342SMike Snitzer unsigned md_type = dm_get_md_type(md); 2781bfebd1cdSMike Snitzer struct request_queue *q; 2782bfebd1cdSMike Snitzer int err; 2783bfebd1cdSMike Snitzer 2784bfebd1cdSMike Snitzer memset(&md->tag_set, 0, sizeof(md->tag_set)); 2785bfebd1cdSMike Snitzer md->tag_set.ops = &dm_mq_ops; 2786bfebd1cdSMike Snitzer md->tag_set.queue_depth = BLKDEV_MAX_RQ; 2787bfebd1cdSMike Snitzer md->tag_set.numa_node = NUMA_NO_NODE; 2788bfebd1cdSMike Snitzer md->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE; 2789bfebd1cdSMike Snitzer md->tag_set.nr_hw_queues = 1; 279002233342SMike Snitzer if (md_type == DM_TYPE_REQUEST_BASED) { 279102233342SMike Snitzer /* make the memory for non-blk-mq clone part of the pdu */ 279202233342SMike Snitzer md->tag_set.cmd_size = sizeof(struct dm_rq_target_io) + sizeof(struct request); 279302233342SMike Snitzer } else 2794bfebd1cdSMike Snitzer md->tag_set.cmd_size = sizeof(struct dm_rq_target_io); 2795bfebd1cdSMike Snitzer md->tag_set.driver_data = md; 2796bfebd1cdSMike Snitzer 2797bfebd1cdSMike Snitzer err = blk_mq_alloc_tag_set(&md->tag_set); 2798bfebd1cdSMike Snitzer if (err) 2799bfebd1cdSMike Snitzer return err; 2800bfebd1cdSMike Snitzer 2801bfebd1cdSMike Snitzer q = blk_mq_init_allocated_queue(&md->tag_set, md->queue); 2802bfebd1cdSMike Snitzer if (IS_ERR(q)) { 2803bfebd1cdSMike Snitzer err = PTR_ERR(q); 2804bfebd1cdSMike Snitzer goto out_tag_set; 2805bfebd1cdSMike Snitzer } 2806bfebd1cdSMike Snitzer md->queue = q; 2807bfebd1cdSMike Snitzer dm_init_md_queue(md); 2808bfebd1cdSMike Snitzer 2809bfebd1cdSMike Snitzer /* backfill 'mq' sysfs registration normally done in blk_register_queue */ 2810bfebd1cdSMike Snitzer blk_mq_register_disk(md->disk); 2811bfebd1cdSMike Snitzer 281202233342SMike Snitzer if (md_type == DM_TYPE_REQUEST_BASED) 2813bfebd1cdSMike Snitzer init_rq_based_worker_thread(md); 2814bfebd1cdSMike Snitzer 2815bfebd1cdSMike Snitzer return 0; 2816bfebd1cdSMike Snitzer 2817bfebd1cdSMike Snitzer out_tag_set: 2818bfebd1cdSMike Snitzer blk_mq_free_tag_set(&md->tag_set); 2819bfebd1cdSMike Snitzer return err; 28204a0b4ddfSMike Snitzer } 28214a0b4ddfSMike Snitzer 282217e149b8SMike Snitzer static unsigned filter_md_type(unsigned type, struct mapped_device *md) 282317e149b8SMike Snitzer { 282417e149b8SMike Snitzer if (type == DM_TYPE_BIO_BASED) 282517e149b8SMike Snitzer return type; 282617e149b8SMike Snitzer 282717e149b8SMike Snitzer return !md->use_blk_mq ? DM_TYPE_REQUEST_BASED : DM_TYPE_MQ_REQUEST_BASED; 282817e149b8SMike Snitzer } 282917e149b8SMike Snitzer 28304a0b4ddfSMike Snitzer /* 28314a0b4ddfSMike Snitzer * Setup the DM device's queue based on md's type 28324a0b4ddfSMike Snitzer */ 28334a0b4ddfSMike Snitzer int dm_setup_md_queue(struct mapped_device *md) 28344a0b4ddfSMike Snitzer { 2835bfebd1cdSMike Snitzer int r; 283617e149b8SMike Snitzer unsigned md_type = filter_md_type(dm_get_md_type(md), md); 2837bfebd1cdSMike Snitzer 2838bfebd1cdSMike Snitzer switch (md_type) { 2839bfebd1cdSMike Snitzer case DM_TYPE_REQUEST_BASED: 2840bfebd1cdSMike Snitzer r = dm_init_request_based_queue(md); 2841bfebd1cdSMike Snitzer if (r) { 28424a0b4ddfSMike Snitzer DMWARN("Cannot initialize queue for request-based mapped device"); 2843bfebd1cdSMike Snitzer return r; 28444a0b4ddfSMike Snitzer } 2845bfebd1cdSMike Snitzer break; 2846bfebd1cdSMike Snitzer case DM_TYPE_MQ_REQUEST_BASED: 2847bfebd1cdSMike Snitzer r = dm_init_request_based_blk_mq_queue(md); 2848bfebd1cdSMike Snitzer if (r) { 2849bfebd1cdSMike Snitzer DMWARN("Cannot initialize queue for request-based blk-mq mapped device"); 2850bfebd1cdSMike Snitzer return r; 2851bfebd1cdSMike Snitzer } 2852bfebd1cdSMike Snitzer break; 2853bfebd1cdSMike Snitzer case DM_TYPE_BIO_BASED: 2854bfebd1cdSMike Snitzer dm_init_old_md_queue(md); 2855ff36ab34SMike Snitzer blk_queue_make_request(md->queue, dm_make_request); 2856ff36ab34SMike Snitzer blk_queue_merge_bvec(md->queue, dm_merge_bvec); 2857bfebd1cdSMike Snitzer break; 2858ff36ab34SMike Snitzer } 28594a0b4ddfSMike Snitzer 28604a0b4ddfSMike Snitzer return 0; 28614a0b4ddfSMike Snitzer } 28624a0b4ddfSMike Snitzer 28632bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev) 28641da177e4SLinus Torvalds { 28651da177e4SLinus Torvalds struct mapped_device *md; 28661da177e4SLinus Torvalds unsigned minor = MINOR(dev); 28671da177e4SLinus Torvalds 28681da177e4SLinus Torvalds if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 28691da177e4SLinus Torvalds return NULL; 28701da177e4SLinus Torvalds 2871f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 28721da177e4SLinus Torvalds 28731da177e4SLinus Torvalds md = idr_find(&_minor_idr, minor); 28742bec1f4aSMikulas Patocka if (md) { 28752bec1f4aSMikulas Patocka if ((md == MINOR_ALLOCED || 2876f331c029STejun Heo (MINOR(disk_devt(dm_disk(md))) != minor) || 2877abdc568bSKiyoshi Ueda dm_deleting_md(md) || 2878fba9f90eSJeff Mahoney test_bit(DMF_FREEING, &md->flags))) { 2879637842cfSDavid Teigland md = NULL; 2880fba9f90eSJeff Mahoney goto out; 2881fba9f90eSJeff Mahoney } 28822bec1f4aSMikulas Patocka dm_get(md); 28832bec1f4aSMikulas Patocka } 28841da177e4SLinus Torvalds 2885fba9f90eSJeff Mahoney out: 2886f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 28871da177e4SLinus Torvalds 2888637842cfSDavid Teigland return md; 2889637842cfSDavid Teigland } 28903cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md); 2891d229a958SDavid Teigland 28929ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md) 2893637842cfSDavid Teigland { 28949ade92a9SAlasdair G Kergon return md->interface_ptr; 28951da177e4SLinus Torvalds } 28961da177e4SLinus Torvalds 28971da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr) 28981da177e4SLinus Torvalds { 28991da177e4SLinus Torvalds md->interface_ptr = ptr; 29001da177e4SLinus Torvalds } 29011da177e4SLinus Torvalds 29021da177e4SLinus Torvalds void dm_get(struct mapped_device *md) 29031da177e4SLinus Torvalds { 29041da177e4SLinus Torvalds atomic_inc(&md->holders); 29053f77316dSKiyoshi Ueda BUG_ON(test_bit(DMF_FREEING, &md->flags)); 29061da177e4SLinus Torvalds } 29071da177e4SLinus Torvalds 290809ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md) 290909ee96b2SMikulas Patocka { 291009ee96b2SMikulas Patocka spin_lock(&_minor_lock); 291109ee96b2SMikulas Patocka if (test_bit(DMF_FREEING, &md->flags)) { 291209ee96b2SMikulas Patocka spin_unlock(&_minor_lock); 291309ee96b2SMikulas Patocka return -EBUSY; 291409ee96b2SMikulas Patocka } 291509ee96b2SMikulas Patocka dm_get(md); 291609ee96b2SMikulas Patocka spin_unlock(&_minor_lock); 291709ee96b2SMikulas Patocka return 0; 291809ee96b2SMikulas Patocka } 291909ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold); 292009ee96b2SMikulas Patocka 292172d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md) 292272d94861SAlasdair G Kergon { 292372d94861SAlasdair G Kergon return md->name; 292472d94861SAlasdair G Kergon } 292572d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name); 292672d94861SAlasdair G Kergon 29273f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait) 29281da177e4SLinus Torvalds { 29291134e5aeSMike Anderson struct dm_table *map; 293083d5e5b0SMikulas Patocka int srcu_idx; 29311da177e4SLinus Torvalds 29323f77316dSKiyoshi Ueda might_sleep(); 2933fba9f90eSJeff Mahoney 293483d5e5b0SMikulas Patocka map = dm_get_live_table(md, &srcu_idx); 293563a4f065SMike Snitzer 293663a4f065SMike Snitzer spin_lock(&_minor_lock); 29373f77316dSKiyoshi Ueda idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md)))); 2938fba9f90eSJeff Mahoney set_bit(DMF_FREEING, &md->flags); 2939f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 29403f77316dSKiyoshi Ueda 294102233342SMike Snitzer if (dm_request_based(md) && md->kworker_task) 29422eb6e1e3SKeith Busch flush_kthread_worker(&md->kworker); 29432eb6e1e3SKeith Busch 2944ab7c7bb6SMikulas Patocka /* 2945ab7c7bb6SMikulas Patocka * Take suspend_lock so that presuspend and postsuspend methods 2946ab7c7bb6SMikulas Patocka * do not race with internal suspend. 2947ab7c7bb6SMikulas Patocka */ 2948ab7c7bb6SMikulas Patocka mutex_lock(&md->suspend_lock); 29494f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) { 29501da177e4SLinus Torvalds dm_table_presuspend_targets(map); 29511da177e4SLinus Torvalds dm_table_postsuspend_targets(map); 29521da177e4SLinus Torvalds } 2953ab7c7bb6SMikulas Patocka mutex_unlock(&md->suspend_lock); 29543f77316dSKiyoshi Ueda 295583d5e5b0SMikulas Patocka /* dm_put_live_table must be before msleep, otherwise deadlock is possible */ 295683d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 295783d5e5b0SMikulas Patocka 29583f77316dSKiyoshi Ueda /* 29593f77316dSKiyoshi Ueda * Rare, but there may be I/O requests still going to complete, 29603f77316dSKiyoshi Ueda * for example. Wait for all references to disappear. 29613f77316dSKiyoshi Ueda * No one should increment the reference count of the mapped_device, 29623f77316dSKiyoshi Ueda * after the mapped_device state becomes DMF_FREEING. 29633f77316dSKiyoshi Ueda */ 29643f77316dSKiyoshi Ueda if (wait) 29653f77316dSKiyoshi Ueda while (atomic_read(&md->holders)) 29663f77316dSKiyoshi Ueda msleep(1); 29673f77316dSKiyoshi Ueda else if (atomic_read(&md->holders)) 29683f77316dSKiyoshi Ueda DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)", 29693f77316dSKiyoshi Ueda dm_device_name(md), atomic_read(&md->holders)); 29703f77316dSKiyoshi Ueda 2971784aae73SMilan Broz dm_sysfs_exit(md); 2972a7940155SAlasdair G Kergon dm_table_destroy(__unbind(md)); 29731da177e4SLinus Torvalds free_dev(md); 29741da177e4SLinus Torvalds } 29753f77316dSKiyoshi Ueda 29763f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md) 29773f77316dSKiyoshi Ueda { 29783f77316dSKiyoshi Ueda __dm_destroy(md, true); 29793f77316dSKiyoshi Ueda } 29803f77316dSKiyoshi Ueda 29813f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md) 29823f77316dSKiyoshi Ueda { 29833f77316dSKiyoshi Ueda __dm_destroy(md, false); 29843f77316dSKiyoshi Ueda } 29853f77316dSKiyoshi Ueda 29863f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md) 29873f77316dSKiyoshi Ueda { 29883f77316dSKiyoshi Ueda atomic_dec(&md->holders); 29891da177e4SLinus Torvalds } 299079eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put); 29911da177e4SLinus Torvalds 2992401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible) 299346125c1cSMilan Broz { 299446125c1cSMilan Broz int r = 0; 2995b44ebeb0SMikulas Patocka DECLARE_WAITQUEUE(wait, current); 2996b44ebeb0SMikulas Patocka 2997b44ebeb0SMikulas Patocka add_wait_queue(&md->wait, &wait); 299846125c1cSMilan Broz 299946125c1cSMilan Broz while (1) { 3000401600dfSMikulas Patocka set_current_state(interruptible); 300146125c1cSMilan Broz 3002b4324feeSKiyoshi Ueda if (!md_in_flight(md)) 300346125c1cSMilan Broz break; 300446125c1cSMilan Broz 3005401600dfSMikulas Patocka if (interruptible == TASK_INTERRUPTIBLE && 3006401600dfSMikulas Patocka signal_pending(current)) { 300746125c1cSMilan Broz r = -EINTR; 300846125c1cSMilan Broz break; 300946125c1cSMilan Broz } 301046125c1cSMilan Broz 301146125c1cSMilan Broz io_schedule(); 301246125c1cSMilan Broz } 301346125c1cSMilan Broz set_current_state(TASK_RUNNING); 301446125c1cSMilan Broz 3015b44ebeb0SMikulas Patocka remove_wait_queue(&md->wait, &wait); 3016b44ebeb0SMikulas Patocka 301746125c1cSMilan Broz return r; 301846125c1cSMilan Broz } 301946125c1cSMilan Broz 30201da177e4SLinus Torvalds /* 30211da177e4SLinus Torvalds * Process the deferred bios 30221da177e4SLinus Torvalds */ 3023ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work) 30241da177e4SLinus Torvalds { 3025ef208587SMikulas Patocka struct mapped_device *md = container_of(work, struct mapped_device, 3026ef208587SMikulas Patocka work); 30276d6f10dfSMilan Broz struct bio *c; 302883d5e5b0SMikulas Patocka int srcu_idx; 302983d5e5b0SMikulas Patocka struct dm_table *map; 30301da177e4SLinus Torvalds 303183d5e5b0SMikulas Patocka map = dm_get_live_table(md, &srcu_idx); 3032ef208587SMikulas Patocka 30333b00b203SMikulas Patocka while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 3034022c2611SMikulas Patocka spin_lock_irq(&md->deferred_lock); 3035022c2611SMikulas Patocka c = bio_list_pop(&md->deferred); 3036022c2611SMikulas Patocka spin_unlock_irq(&md->deferred_lock); 3037022c2611SMikulas Patocka 30386a8736d1STejun Heo if (!c) 3039df12ee99SAlasdair G Kergon break; 304073d410c0SMilan Broz 3041e6ee8c0bSKiyoshi Ueda if (dm_request_based(md)) 3042e6ee8c0bSKiyoshi Ueda generic_make_request(c); 3043af7e466aSMikulas Patocka else 304483d5e5b0SMikulas Patocka __split_and_process_bio(md, map, c); 3045e6ee8c0bSKiyoshi Ueda } 30463b00b203SMikulas Patocka 304783d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 30481da177e4SLinus Torvalds } 30491da177e4SLinus Torvalds 30509a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md) 3051304f3f6aSMilan Broz { 30523b00b203SMikulas Patocka clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 30534e857c58SPeter Zijlstra smp_mb__after_atomic(); 305453d5914fSMikulas Patocka queue_work(md->wq, &md->work); 3055304f3f6aSMilan Broz } 3056304f3f6aSMilan Broz 30571da177e4SLinus Torvalds /* 3058042d2a9bSAlasdair G Kergon * Swap in a new table, returning the old one for the caller to destroy. 30591da177e4SLinus Torvalds */ 3060042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table) 30611da177e4SLinus Torvalds { 306287eb5b21SMike Christie struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL); 3063754c5fc7SMike Snitzer struct queue_limits limits; 3064042d2a9bSAlasdair G Kergon int r; 30651da177e4SLinus Torvalds 3066e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 30671da177e4SLinus Torvalds 30681da177e4SLinus Torvalds /* device must be suspended */ 30694f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) 307093c534aeSAlasdair G Kergon goto out; 30711da177e4SLinus Torvalds 30723ae70656SMike Snitzer /* 30733ae70656SMike Snitzer * If the new table has no data devices, retain the existing limits. 30743ae70656SMike Snitzer * This helps multipath with queue_if_no_path if all paths disappear, 30753ae70656SMike Snitzer * then new I/O is queued based on these limits, and then some paths 30763ae70656SMike Snitzer * reappear. 30773ae70656SMike Snitzer */ 30783ae70656SMike Snitzer if (dm_table_has_no_data_devices(table)) { 307983d5e5b0SMikulas Patocka live_map = dm_get_live_table_fast(md); 30803ae70656SMike Snitzer if (live_map) 30813ae70656SMike Snitzer limits = md->queue->limits; 308283d5e5b0SMikulas Patocka dm_put_live_table_fast(md); 30833ae70656SMike Snitzer } 30843ae70656SMike Snitzer 308587eb5b21SMike Christie if (!live_map) { 3086754c5fc7SMike Snitzer r = dm_calculate_queue_limits(table, &limits); 3087042d2a9bSAlasdair G Kergon if (r) { 3088042d2a9bSAlasdair G Kergon map = ERR_PTR(r); 3089754c5fc7SMike Snitzer goto out; 3090042d2a9bSAlasdair G Kergon } 309187eb5b21SMike Christie } 3092754c5fc7SMike Snitzer 3093042d2a9bSAlasdair G Kergon map = __bind(md, table, &limits); 30941da177e4SLinus Torvalds 309593c534aeSAlasdair G Kergon out: 3096e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 3097042d2a9bSAlasdair G Kergon return map; 30981da177e4SLinus Torvalds } 30991da177e4SLinus Torvalds 31001da177e4SLinus Torvalds /* 31011da177e4SLinus Torvalds * Functions to lock and unlock any filesystem running on the 31021da177e4SLinus Torvalds * device. 31031da177e4SLinus Torvalds */ 31042ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md) 31051da177e4SLinus Torvalds { 3106e39e2e95SAlasdair G Kergon int r; 31071da177e4SLinus Torvalds 31081da177e4SLinus Torvalds WARN_ON(md->frozen_sb); 3109dfbe03f6SAlasdair G Kergon 3110db8fef4fSMikulas Patocka md->frozen_sb = freeze_bdev(md->bdev); 3111dfbe03f6SAlasdair G Kergon if (IS_ERR(md->frozen_sb)) { 3112cf222b37SAlasdair G Kergon r = PTR_ERR(md->frozen_sb); 3113e39e2e95SAlasdair G Kergon md->frozen_sb = NULL; 3114e39e2e95SAlasdair G Kergon return r; 3115dfbe03f6SAlasdair G Kergon } 3116dfbe03f6SAlasdair G Kergon 3117aa8d7c2fSAlasdair G Kergon set_bit(DMF_FROZEN, &md->flags); 3118aa8d7c2fSAlasdair G Kergon 31191da177e4SLinus Torvalds return 0; 31201da177e4SLinus Torvalds } 31211da177e4SLinus Torvalds 31222ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md) 31231da177e4SLinus Torvalds { 3124aa8d7c2fSAlasdair G Kergon if (!test_bit(DMF_FROZEN, &md->flags)) 3125aa8d7c2fSAlasdair G Kergon return; 3126aa8d7c2fSAlasdair G Kergon 3127db8fef4fSMikulas Patocka thaw_bdev(md->bdev, md->frozen_sb); 31281da177e4SLinus Torvalds md->frozen_sb = NULL; 3129aa8d7c2fSAlasdair G Kergon clear_bit(DMF_FROZEN, &md->flags); 31301da177e4SLinus Torvalds } 31311da177e4SLinus Torvalds 31321da177e4SLinus Torvalds /* 3133ffcc3936SMike Snitzer * If __dm_suspend returns 0, the device is completely quiescent 3134ffcc3936SMike Snitzer * now. There is no request-processing activity. All new requests 3135ffcc3936SMike Snitzer * are being added to md->deferred list. 3136cec47e3dSKiyoshi Ueda * 3137ffcc3936SMike Snitzer * Caller must hold md->suspend_lock 3138cec47e3dSKiyoshi Ueda */ 3139ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map, 3140ffcc3936SMike Snitzer unsigned suspend_flags, int interruptible) 31411da177e4SLinus Torvalds { 3142ffcc3936SMike Snitzer bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG; 3143ffcc3936SMike Snitzer bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG; 3144ffcc3936SMike Snitzer int r; 3145cf222b37SAlasdair G Kergon 31462e93ccc1SKiyoshi Ueda /* 31472e93ccc1SKiyoshi Ueda * DMF_NOFLUSH_SUSPENDING must be set before presuspend. 31482e93ccc1SKiyoshi Ueda * This flag is cleared before dm_suspend returns. 31492e93ccc1SKiyoshi Ueda */ 31502e93ccc1SKiyoshi Ueda if (noflush) 31512e93ccc1SKiyoshi Ueda set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 31522e93ccc1SKiyoshi Ueda 3153d67ee213SMike Snitzer /* 3154d67ee213SMike Snitzer * This gets reverted if there's an error later and the targets 3155d67ee213SMike Snitzer * provide the .presuspend_undo hook. 3156d67ee213SMike Snitzer */ 31571da177e4SLinus Torvalds dm_table_presuspend_targets(map); 31581da177e4SLinus Torvalds 31592e93ccc1SKiyoshi Ueda /* 31609f518b27SKiyoshi Ueda * Flush I/O to the device. 31619f518b27SKiyoshi Ueda * Any I/O submitted after lock_fs() may not be flushed. 31629f518b27SKiyoshi Ueda * noflush takes precedence over do_lockfs. 31639f518b27SKiyoshi Ueda * (lock_fs() flushes I/Os and waits for them to complete.) 31642e93ccc1SKiyoshi Ueda */ 316532a926daSMikulas Patocka if (!noflush && do_lockfs) { 31662ca3310eSAlasdair G Kergon r = lock_fs(md); 3167d67ee213SMike Snitzer if (r) { 3168d67ee213SMike Snitzer dm_table_presuspend_undo_targets(map); 3169ffcc3936SMike Snitzer return r; 3170aa8d7c2fSAlasdair G Kergon } 3171d67ee213SMike Snitzer } 31721da177e4SLinus Torvalds 31731da177e4SLinus Torvalds /* 31743b00b203SMikulas Patocka * Here we must make sure that no processes are submitting requests 31753b00b203SMikulas Patocka * to target drivers i.e. no one may be executing 31763b00b203SMikulas Patocka * __split_and_process_bio. This is called from dm_request and 31773b00b203SMikulas Patocka * dm_wq_work. 31783b00b203SMikulas Patocka * 31793b00b203SMikulas Patocka * To get all processes out of __split_and_process_bio in dm_request, 31803b00b203SMikulas Patocka * we take the write lock. To prevent any process from reentering 31816a8736d1STejun Heo * __split_and_process_bio from dm_request and quiesce the thread 31826a8736d1STejun Heo * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call 31836a8736d1STejun Heo * flush_workqueue(md->wq). 31841da177e4SLinus Torvalds */ 31851eb787ecSAlasdair G Kergon set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 318641abc4e1SHannes Reinecke if (map) 318783d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 31881da177e4SLinus Torvalds 3189d0bcb878SKiyoshi Ueda /* 319029e4013dSTejun Heo * Stop md->queue before flushing md->wq in case request-based 319129e4013dSTejun Heo * dm defers requests to md->wq from md->queue. 3192d0bcb878SKiyoshi Ueda */ 31932eb6e1e3SKeith Busch if (dm_request_based(md)) { 31949f518b27SKiyoshi Ueda stop_queue(md->queue); 319502233342SMike Snitzer if (md->kworker_task) 31962eb6e1e3SKeith Busch flush_kthread_worker(&md->kworker); 31972eb6e1e3SKeith Busch } 3198cec47e3dSKiyoshi Ueda 3199d0bcb878SKiyoshi Ueda flush_workqueue(md->wq); 3200d0bcb878SKiyoshi Ueda 32011da177e4SLinus Torvalds /* 32023b00b203SMikulas Patocka * At this point no more requests are entering target request routines. 32033b00b203SMikulas Patocka * We call dm_wait_for_completion to wait for all existing requests 32043b00b203SMikulas Patocka * to finish. 32051da177e4SLinus Torvalds */ 3206ffcc3936SMike Snitzer r = dm_wait_for_completion(md, interruptible); 32071da177e4SLinus Torvalds 32086d6f10dfSMilan Broz if (noflush) 3209022c2611SMikulas Patocka clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 321041abc4e1SHannes Reinecke if (map) 321183d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 32122e93ccc1SKiyoshi Ueda 32131da177e4SLinus Torvalds /* were we interrupted ? */ 321446125c1cSMilan Broz if (r < 0) { 32159a1fb464SMikulas Patocka dm_queue_flush(md); 321673d410c0SMilan Broz 3217cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 32189f518b27SKiyoshi Ueda start_queue(md->queue); 3219cec47e3dSKiyoshi Ueda 32202ca3310eSAlasdair G Kergon unlock_fs(md); 3221d67ee213SMike Snitzer dm_table_presuspend_undo_targets(map); 3222ffcc3936SMike Snitzer /* pushback list is already flushed, so skip flush */ 3223ffcc3936SMike Snitzer } 3224ffcc3936SMike Snitzer 3225ffcc3936SMike Snitzer return r; 32262ca3310eSAlasdair G Kergon } 32272ca3310eSAlasdair G Kergon 32283b00b203SMikulas Patocka /* 3229ffcc3936SMike Snitzer * We need to be able to change a mapping table under a mounted 3230ffcc3936SMike Snitzer * filesystem. For example we might want to move some data in 3231ffcc3936SMike Snitzer * the background. Before the table can be swapped with 3232ffcc3936SMike Snitzer * dm_bind_table, dm_suspend must be called to flush any in 3233ffcc3936SMike Snitzer * flight bios and ensure that any further io gets deferred. 32343b00b203SMikulas Patocka */ 3235ffcc3936SMike Snitzer /* 3236ffcc3936SMike Snitzer * Suspend mechanism in request-based dm. 3237ffcc3936SMike Snitzer * 3238ffcc3936SMike Snitzer * 1. Flush all I/Os by lock_fs() if needed. 3239ffcc3936SMike Snitzer * 2. Stop dispatching any I/O by stopping the request_queue. 3240ffcc3936SMike Snitzer * 3. Wait for all in-flight I/Os to be completed or requeued. 3241ffcc3936SMike Snitzer * 3242ffcc3936SMike Snitzer * To abort suspend, start the request_queue. 3243ffcc3936SMike Snitzer */ 3244ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags) 3245ffcc3936SMike Snitzer { 3246ffcc3936SMike Snitzer struct dm_table *map = NULL; 3247ffcc3936SMike Snitzer int r = 0; 3248ffcc3936SMike Snitzer 3249ffcc3936SMike Snitzer retry: 3250ffcc3936SMike Snitzer mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); 3251ffcc3936SMike Snitzer 3252ffcc3936SMike Snitzer if (dm_suspended_md(md)) { 3253ffcc3936SMike Snitzer r = -EINVAL; 3254ffcc3936SMike Snitzer goto out_unlock; 3255ffcc3936SMike Snitzer } 3256ffcc3936SMike Snitzer 3257ffcc3936SMike Snitzer if (dm_suspended_internally_md(md)) { 3258ffcc3936SMike Snitzer /* already internally suspended, wait for internal resume */ 3259ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 3260ffcc3936SMike Snitzer r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); 3261ffcc3936SMike Snitzer if (r) 3262ffcc3936SMike Snitzer return r; 3263ffcc3936SMike Snitzer goto retry; 3264ffcc3936SMike Snitzer } 3265ffcc3936SMike Snitzer 3266a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 3267ffcc3936SMike Snitzer 3268ffcc3936SMike Snitzer r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE); 3269ffcc3936SMike Snitzer if (r) 3270ffcc3936SMike Snitzer goto out_unlock; 32713b00b203SMikulas Patocka 32721da177e4SLinus Torvalds set_bit(DMF_SUSPENDED, &md->flags); 32731da177e4SLinus Torvalds 32744d4471cbSKiyoshi Ueda dm_table_postsuspend_targets(map); 32754d4471cbSKiyoshi Ueda 3276d287483dSAlasdair G Kergon out_unlock: 3277e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 3278cf222b37SAlasdair G Kergon return r; 32791da177e4SLinus Torvalds } 32801da177e4SLinus Torvalds 3281ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map) 32821da177e4SLinus Torvalds { 3283ffcc3936SMike Snitzer if (map) { 3284ffcc3936SMike Snitzer int r = dm_table_resume_targets(map); 32858757b776SMilan Broz if (r) 3286ffcc3936SMike Snitzer return r; 3287ffcc3936SMike Snitzer } 32882ca3310eSAlasdair G Kergon 32899a1fb464SMikulas Patocka dm_queue_flush(md); 32902ca3310eSAlasdair G Kergon 3291cec47e3dSKiyoshi Ueda /* 3292cec47e3dSKiyoshi Ueda * Flushing deferred I/Os must be done after targets are resumed 3293cec47e3dSKiyoshi Ueda * so that mapping of targets can work correctly. 3294cec47e3dSKiyoshi Ueda * Request-based dm is queueing the deferred I/Os in its request_queue. 3295cec47e3dSKiyoshi Ueda */ 3296cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 3297cec47e3dSKiyoshi Ueda start_queue(md->queue); 3298cec47e3dSKiyoshi Ueda 32992ca3310eSAlasdair G Kergon unlock_fs(md); 33002ca3310eSAlasdair G Kergon 3301ffcc3936SMike Snitzer return 0; 3302ffcc3936SMike Snitzer } 3303ffcc3936SMike Snitzer 3304ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md) 3305ffcc3936SMike Snitzer { 3306ffcc3936SMike Snitzer int r = -EINVAL; 3307ffcc3936SMike Snitzer struct dm_table *map = NULL; 3308ffcc3936SMike Snitzer 3309ffcc3936SMike Snitzer retry: 3310ffcc3936SMike Snitzer mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); 3311ffcc3936SMike Snitzer 3312ffcc3936SMike Snitzer if (!dm_suspended_md(md)) 3313ffcc3936SMike Snitzer goto out; 3314ffcc3936SMike Snitzer 3315ffcc3936SMike Snitzer if (dm_suspended_internally_md(md)) { 3316ffcc3936SMike Snitzer /* already internally suspended, wait for internal resume */ 3317ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 3318ffcc3936SMike Snitzer r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); 3319ffcc3936SMike Snitzer if (r) 3320ffcc3936SMike Snitzer return r; 3321ffcc3936SMike Snitzer goto retry; 3322ffcc3936SMike Snitzer } 3323ffcc3936SMike Snitzer 3324a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 3325ffcc3936SMike Snitzer if (!map || !dm_table_get_size(map)) 3326ffcc3936SMike Snitzer goto out; 3327ffcc3936SMike Snitzer 3328ffcc3936SMike Snitzer r = __dm_resume(md, map); 3329ffcc3936SMike Snitzer if (r) 3330ffcc3936SMike Snitzer goto out; 3331ffcc3936SMike Snitzer 33322ca3310eSAlasdair G Kergon clear_bit(DMF_SUSPENDED, &md->flags); 33332ca3310eSAlasdair G Kergon 3334cf222b37SAlasdair G Kergon r = 0; 3335cf222b37SAlasdair G Kergon out: 3336e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 33372ca3310eSAlasdair G Kergon 3338cf222b37SAlasdair G Kergon return r; 33391da177e4SLinus Torvalds } 33401da177e4SLinus Torvalds 3341fd2ed4d2SMikulas Patocka /* 3342fd2ed4d2SMikulas Patocka * Internal suspend/resume works like userspace-driven suspend. It waits 3343fd2ed4d2SMikulas Patocka * until all bios finish and prevents issuing new bios to the target drivers. 3344fd2ed4d2SMikulas Patocka * It may be used only from the kernel. 3345fd2ed4d2SMikulas Patocka */ 3346fd2ed4d2SMikulas Patocka 3347ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags) 3348ffcc3936SMike Snitzer { 3349ffcc3936SMike Snitzer struct dm_table *map = NULL; 3350ffcc3936SMike Snitzer 335196b26c8cSMikulas Patocka if (md->internal_suspend_count++) 3352ffcc3936SMike Snitzer return; /* nested internal suspend */ 3353ffcc3936SMike Snitzer 3354ffcc3936SMike Snitzer if (dm_suspended_md(md)) { 3355ffcc3936SMike Snitzer set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 3356ffcc3936SMike Snitzer return; /* nest suspend */ 3357ffcc3936SMike Snitzer } 3358ffcc3936SMike Snitzer 3359a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 3360ffcc3936SMike Snitzer 3361ffcc3936SMike Snitzer /* 3362ffcc3936SMike Snitzer * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is 3363ffcc3936SMike Snitzer * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend 3364ffcc3936SMike Snitzer * would require changing .presuspend to return an error -- avoid this 3365ffcc3936SMike Snitzer * until there is a need for more elaborate variants of internal suspend. 3366ffcc3936SMike Snitzer */ 3367ffcc3936SMike Snitzer (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE); 3368ffcc3936SMike Snitzer 3369ffcc3936SMike Snitzer set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 3370ffcc3936SMike Snitzer 3371ffcc3936SMike Snitzer dm_table_postsuspend_targets(map); 3372ffcc3936SMike Snitzer } 3373ffcc3936SMike Snitzer 3374ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md) 3375ffcc3936SMike Snitzer { 337696b26c8cSMikulas Patocka BUG_ON(!md->internal_suspend_count); 337796b26c8cSMikulas Patocka 337896b26c8cSMikulas Patocka if (--md->internal_suspend_count) 3379ffcc3936SMike Snitzer return; /* resume from nested internal suspend */ 3380ffcc3936SMike Snitzer 3381ffcc3936SMike Snitzer if (dm_suspended_md(md)) 3382ffcc3936SMike Snitzer goto done; /* resume from nested suspend */ 3383ffcc3936SMike Snitzer 3384ffcc3936SMike Snitzer /* 3385ffcc3936SMike Snitzer * NOTE: existing callers don't need to call dm_table_resume_targets 3386ffcc3936SMike Snitzer * (which may fail -- so best to avoid it for now by passing NULL map) 3387ffcc3936SMike Snitzer */ 3388ffcc3936SMike Snitzer (void) __dm_resume(md, NULL); 3389ffcc3936SMike Snitzer 3390ffcc3936SMike Snitzer done: 3391ffcc3936SMike Snitzer clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 3392ffcc3936SMike Snitzer smp_mb__after_atomic(); 3393ffcc3936SMike Snitzer wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY); 3394ffcc3936SMike Snitzer } 3395ffcc3936SMike Snitzer 3396ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md) 3397fd2ed4d2SMikulas Patocka { 3398fd2ed4d2SMikulas Patocka mutex_lock(&md->suspend_lock); 3399ffcc3936SMike Snitzer __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG); 3400ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 3401ffcc3936SMike Snitzer } 3402ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush); 3403ffcc3936SMike Snitzer 3404ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md) 3405ffcc3936SMike Snitzer { 3406ffcc3936SMike Snitzer mutex_lock(&md->suspend_lock); 3407ffcc3936SMike Snitzer __dm_internal_resume(md); 3408ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 3409ffcc3936SMike Snitzer } 3410ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume); 3411ffcc3936SMike Snitzer 3412ffcc3936SMike Snitzer /* 3413ffcc3936SMike Snitzer * Fast variants of internal suspend/resume hold md->suspend_lock, 3414ffcc3936SMike Snitzer * which prevents interaction with userspace-driven suspend. 3415ffcc3936SMike Snitzer */ 3416ffcc3936SMike Snitzer 3417ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md) 3418ffcc3936SMike Snitzer { 3419ffcc3936SMike Snitzer mutex_lock(&md->suspend_lock); 3420ffcc3936SMike Snitzer if (dm_suspended_md(md) || dm_suspended_internally_md(md)) 3421fd2ed4d2SMikulas Patocka return; 3422fd2ed4d2SMikulas Patocka 3423fd2ed4d2SMikulas Patocka set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 3424fd2ed4d2SMikulas Patocka synchronize_srcu(&md->io_barrier); 3425fd2ed4d2SMikulas Patocka flush_workqueue(md->wq); 3426fd2ed4d2SMikulas Patocka dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); 3427fd2ed4d2SMikulas Patocka } 3428b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast); 3429fd2ed4d2SMikulas Patocka 3430ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md) 3431fd2ed4d2SMikulas Patocka { 3432ffcc3936SMike Snitzer if (dm_suspended_md(md) || dm_suspended_internally_md(md)) 3433fd2ed4d2SMikulas Patocka goto done; 3434fd2ed4d2SMikulas Patocka 3435fd2ed4d2SMikulas Patocka dm_queue_flush(md); 3436fd2ed4d2SMikulas Patocka 3437fd2ed4d2SMikulas Patocka done: 3438fd2ed4d2SMikulas Patocka mutex_unlock(&md->suspend_lock); 3439fd2ed4d2SMikulas Patocka } 3440b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast); 3441fd2ed4d2SMikulas Patocka 34421da177e4SLinus Torvalds /*----------------------------------------------------------------- 34431da177e4SLinus Torvalds * Event notification. 34441da177e4SLinus Torvalds *---------------------------------------------------------------*/ 34453abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action, 344660935eb2SMilan Broz unsigned cookie) 344769267a30SAlasdair G Kergon { 344860935eb2SMilan Broz char udev_cookie[DM_COOKIE_LENGTH]; 344960935eb2SMilan Broz char *envp[] = { udev_cookie, NULL }; 345060935eb2SMilan Broz 345160935eb2SMilan Broz if (!cookie) 34523abf85b5SPeter Rajnoha return kobject_uevent(&disk_to_dev(md->disk)->kobj, action); 345360935eb2SMilan Broz else { 345460935eb2SMilan Broz snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u", 345560935eb2SMilan Broz DM_COOKIE_ENV_VAR_NAME, cookie); 34563abf85b5SPeter Rajnoha return kobject_uevent_env(&disk_to_dev(md->disk)->kobj, 34573abf85b5SPeter Rajnoha action, envp); 345860935eb2SMilan Broz } 345969267a30SAlasdair G Kergon } 346069267a30SAlasdair G Kergon 34617a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md) 34627a8c3d3bSMike Anderson { 34637a8c3d3bSMike Anderson return atomic_add_return(1, &md->uevent_seq); 34647a8c3d3bSMike Anderson } 34657a8c3d3bSMike Anderson 34661da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md) 34671da177e4SLinus Torvalds { 34681da177e4SLinus Torvalds return atomic_read(&md->event_nr); 34691da177e4SLinus Torvalds } 34701da177e4SLinus Torvalds 34711da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr) 34721da177e4SLinus Torvalds { 34731da177e4SLinus Torvalds return wait_event_interruptible(md->eventq, 34741da177e4SLinus Torvalds (event_nr != atomic_read(&md->event_nr))); 34751da177e4SLinus Torvalds } 34761da177e4SLinus Torvalds 34777a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist) 34787a8c3d3bSMike Anderson { 34797a8c3d3bSMike Anderson unsigned long flags; 34807a8c3d3bSMike Anderson 34817a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 34827a8c3d3bSMike Anderson list_add(elist, &md->uevent_list); 34837a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 34847a8c3d3bSMike Anderson } 34857a8c3d3bSMike Anderson 34861da177e4SLinus Torvalds /* 34871da177e4SLinus Torvalds * The gendisk is only valid as long as you have a reference 34881da177e4SLinus Torvalds * count on 'md'. 34891da177e4SLinus Torvalds */ 34901da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md) 34911da177e4SLinus Torvalds { 34921da177e4SLinus Torvalds return md->disk; 34931da177e4SLinus Torvalds } 349465ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk); 34951da177e4SLinus Torvalds 3496784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md) 3497784aae73SMilan Broz { 34982995fa78SMikulas Patocka return &md->kobj_holder.kobj; 3499784aae73SMilan Broz } 3500784aae73SMilan Broz 3501784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj) 3502784aae73SMilan Broz { 3503784aae73SMilan Broz struct mapped_device *md; 3504784aae73SMilan Broz 35052995fa78SMikulas Patocka md = container_of(kobj, struct mapped_device, kobj_holder.kobj); 3506784aae73SMilan Broz 35074d89b7b4SMilan Broz if (test_bit(DMF_FREEING, &md->flags) || 3508432a212cSMike Anderson dm_deleting_md(md)) 35094d89b7b4SMilan Broz return NULL; 35104d89b7b4SMilan Broz 3511784aae73SMilan Broz dm_get(md); 3512784aae73SMilan Broz return md; 3513784aae73SMilan Broz } 3514784aae73SMilan Broz 35154f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md) 35161da177e4SLinus Torvalds { 35171da177e4SLinus Torvalds return test_bit(DMF_SUSPENDED, &md->flags); 35181da177e4SLinus Torvalds } 35191da177e4SLinus Torvalds 3520ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md) 3521ffcc3936SMike Snitzer { 3522ffcc3936SMike Snitzer return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 3523ffcc3936SMike Snitzer } 3524ffcc3936SMike Snitzer 35252c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md) 35262c140a24SMikulas Patocka { 35272c140a24SMikulas Patocka return test_bit(DMF_DEFERRED_REMOVE, &md->flags); 35282c140a24SMikulas Patocka } 35292c140a24SMikulas Patocka 353064dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti) 353164dbce58SKiyoshi Ueda { 3532ecdb2e25SKiyoshi Ueda return dm_suspended_md(dm_table_get_md(ti->table)); 353364dbce58SKiyoshi Ueda } 353464dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended); 353564dbce58SKiyoshi Ueda 35362e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti) 35372e93ccc1SKiyoshi Ueda { 3538ecdb2e25SKiyoshi Ueda return __noflush_suspending(dm_table_get_md(ti->table)); 35392e93ccc1SKiyoshi Ueda } 35402e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending); 35412e93ccc1SKiyoshi Ueda 354217e149b8SMike Snitzer struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type, 354317e149b8SMike Snitzer unsigned integrity, unsigned per_bio_data_size) 3544e6ee8c0bSKiyoshi Ueda { 35455f015204SJun'ichi Nomura struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL); 354617e149b8SMike Snitzer struct kmem_cache *cachep = NULL; 3547e5863d9aSMike Snitzer unsigned int pool_size = 0; 35485f015204SJun'ichi Nomura unsigned int front_pad; 3549e6ee8c0bSKiyoshi Ueda 3550e6ee8c0bSKiyoshi Ueda if (!pools) 3551e6ee8c0bSKiyoshi Ueda return NULL; 3552e6ee8c0bSKiyoshi Ueda 355317e149b8SMike Snitzer type = filter_md_type(type, md); 355417e149b8SMike Snitzer 3555e5863d9aSMike Snitzer switch (type) { 3556e5863d9aSMike Snitzer case DM_TYPE_BIO_BASED: 35575f015204SJun'ichi Nomura cachep = _io_cache; 3558e8603136SMike Snitzer pool_size = dm_get_reserved_bio_based_ios(); 35595f015204SJun'ichi Nomura front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone); 3560e5863d9aSMike Snitzer break; 3561e5863d9aSMike Snitzer case DM_TYPE_REQUEST_BASED: 356217e149b8SMike Snitzer cachep = _rq_tio_cache; 3563f4790826SMike Snitzer pool_size = dm_get_reserved_rq_based_ios(); 35641ae49ea2SMike Snitzer pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache); 35651ae49ea2SMike Snitzer if (!pools->rq_pool) 35661ae49ea2SMike Snitzer goto out; 3567e5863d9aSMike Snitzer /* fall through to setup remaining rq-based pools */ 3568e5863d9aSMike Snitzer case DM_TYPE_MQ_REQUEST_BASED: 3569e5863d9aSMike Snitzer if (!pool_size) 35706cfa5857SMike Snitzer pool_size = dm_get_reserved_rq_based_ios(); 35715f015204SJun'ichi Nomura front_pad = offsetof(struct dm_rq_clone_bio_info, clone); 35725f015204SJun'ichi Nomura /* per_bio_data_size is not used. See __bind_mempools(). */ 35735f015204SJun'ichi Nomura WARN_ON(per_bio_data_size != 0); 3574e5863d9aSMike Snitzer break; 3575e5863d9aSMike Snitzer default: 357617e149b8SMike Snitzer BUG(); 3577e5863d9aSMike Snitzer } 35785f015204SJun'ichi Nomura 357917e149b8SMike Snitzer if (cachep) { 35806cfa5857SMike Snitzer pools->io_pool = mempool_create_slab_pool(pool_size, cachep); 3581e6ee8c0bSKiyoshi Ueda if (!pools->io_pool) 35825f015204SJun'ichi Nomura goto out; 358317e149b8SMike Snitzer } 3584e6ee8c0bSKiyoshi Ueda 35853d8aab2dSJunichi Nomura pools->bs = bioset_create_nobvec(pool_size, front_pad); 3586e6ee8c0bSKiyoshi Ueda if (!pools->bs) 35875f015204SJun'ichi Nomura goto out; 3588e6ee8c0bSKiyoshi Ueda 3589a91a2785SMartin K. Petersen if (integrity && bioset_integrity_create(pools->bs, pool_size)) 35905f015204SJun'ichi Nomura goto out; 3591a91a2785SMartin K. Petersen 3592e6ee8c0bSKiyoshi Ueda return pools; 3593e6ee8c0bSKiyoshi Ueda 35945f015204SJun'ichi Nomura out: 35955f015204SJun'ichi Nomura dm_free_md_mempools(pools); 3596e6ee8c0bSKiyoshi Ueda 3597e6ee8c0bSKiyoshi Ueda return NULL; 3598e6ee8c0bSKiyoshi Ueda } 3599e6ee8c0bSKiyoshi Ueda 3600e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools) 3601e6ee8c0bSKiyoshi Ueda { 3602e6ee8c0bSKiyoshi Ueda if (!pools) 3603e6ee8c0bSKiyoshi Ueda return; 3604e6ee8c0bSKiyoshi Ueda 3605e6ee8c0bSKiyoshi Ueda if (pools->io_pool) 3606e6ee8c0bSKiyoshi Ueda mempool_destroy(pools->io_pool); 3607e6ee8c0bSKiyoshi Ueda 36081ae49ea2SMike Snitzer if (pools->rq_pool) 36091ae49ea2SMike Snitzer mempool_destroy(pools->rq_pool); 36101ae49ea2SMike Snitzer 3611e6ee8c0bSKiyoshi Ueda if (pools->bs) 3612e6ee8c0bSKiyoshi Ueda bioset_free(pools->bs); 3613e6ee8c0bSKiyoshi Ueda 3614e6ee8c0bSKiyoshi Ueda kfree(pools); 3615e6ee8c0bSKiyoshi Ueda } 3616e6ee8c0bSKiyoshi Ueda 361783d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = { 36181da177e4SLinus Torvalds .open = dm_blk_open, 36191da177e4SLinus Torvalds .release = dm_blk_close, 3620aa129a22SMilan Broz .ioctl = dm_blk_ioctl, 36213ac51e74SDarrick J. Wong .getgeo = dm_blk_getgeo, 36221da177e4SLinus Torvalds .owner = THIS_MODULE 36231da177e4SLinus Torvalds }; 36241da177e4SLinus Torvalds 36251da177e4SLinus Torvalds /* 36261da177e4SLinus Torvalds * module hooks 36271da177e4SLinus Torvalds */ 36281da177e4SLinus Torvalds module_init(dm_init); 36291da177e4SLinus Torvalds module_exit(dm_exit); 36301da177e4SLinus Torvalds 36311da177e4SLinus Torvalds module_param(major, uint, 0); 36321da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper"); 3633f4790826SMike Snitzer 3634e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR); 3635e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools"); 3636e8603136SMike Snitzer 3637f4790826SMike Snitzer module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR); 3638f4790826SMike Snitzer MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools"); 3639f4790826SMike Snitzer 364017e149b8SMike Snitzer module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR); 364117e149b8SMike Snitzer MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices"); 364217e149b8SMike Snitzer 36431da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver"); 36441da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 36451da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 3646