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; 89e262f347SMikulas Patocka struct dm_stats_aux stats_aux; 90e262f347SMikulas Patocka unsigned long duration_jiffies; 91e262f347SMikulas Patocka unsigned n_sectors; 928fbf26adSKiyoshi Ueda }; 938fbf26adSKiyoshi Ueda 948fbf26adSKiyoshi Ueda /* 9594818742SKent Overstreet * For request-based dm - the bio clones we allocate are embedded in these 9694818742SKent Overstreet * structs. 9794818742SKent Overstreet * 9894818742SKent Overstreet * We allocate these with bio_alloc_bioset, using the front_pad parameter when 9994818742SKent Overstreet * the bioset is created - this means the bio has to come at the end of the 10094818742SKent Overstreet * struct. 1018fbf26adSKiyoshi Ueda */ 1028fbf26adSKiyoshi Ueda struct dm_rq_clone_bio_info { 1038fbf26adSKiyoshi Ueda struct bio *orig; 104cec47e3dSKiyoshi Ueda struct dm_rq_target_io *tio; 10594818742SKent Overstreet struct bio clone; 1068fbf26adSKiyoshi Ueda }; 1078fbf26adSKiyoshi Ueda 108cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq) 109cec47e3dSKiyoshi Ueda { 110cec47e3dSKiyoshi Ueda if (rq && rq->end_io_data) 111cec47e3dSKiyoshi Ueda return &((struct dm_rq_target_io *)rq->end_io_data)->info; 112cec47e3dSKiyoshi Ueda return NULL; 113cec47e3dSKiyoshi Ueda } 114cec47e3dSKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo); 115cec47e3dSKiyoshi Ueda 116ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1) 117ba61fdd1SJeff Mahoney 1181da177e4SLinus Torvalds /* 1191da177e4SLinus Torvalds * Bits for the md->flags field. 1201da177e4SLinus Torvalds */ 1211eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0 1221da177e4SLinus Torvalds #define DMF_SUSPENDED 1 123aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2 124fba9f90eSJeff Mahoney #define DMF_FREEING 3 1255c6bd75dSAlasdair G Kergon #define DMF_DELETING 4 1262e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5 127d5b9dd04SMikulas Patocka #define DMF_MERGE_IS_OPTIONAL 6 1282c140a24SMikulas Patocka #define DMF_DEFERRED_REMOVE 7 129ffcc3936SMike Snitzer #define DMF_SUSPENDED_INTERNALLY 8 1301da177e4SLinus Torvalds 131304f3f6aSMilan Broz /* 13283d5e5b0SMikulas Patocka * A dummy definition to make RCU happy. 13383d5e5b0SMikulas Patocka * struct dm_table should never be dereferenced in this file. 13483d5e5b0SMikulas Patocka */ 13583d5e5b0SMikulas Patocka struct dm_table { 13683d5e5b0SMikulas Patocka int undefined__; 13783d5e5b0SMikulas Patocka }; 13883d5e5b0SMikulas Patocka 13983d5e5b0SMikulas Patocka /* 140304f3f6aSMilan Broz * Work processed by per-device workqueue. 141304f3f6aSMilan Broz */ 1421da177e4SLinus Torvalds struct mapped_device { 14383d5e5b0SMikulas Patocka struct srcu_struct io_barrier; 144e61290a4SDaniel Walker struct mutex suspend_lock; 1451da177e4SLinus Torvalds atomic_t holders; 1465c6bd75dSAlasdair G Kergon atomic_t open_count; 1471da177e4SLinus Torvalds 1482a7faeb1SMikulas Patocka /* 1492a7faeb1SMikulas Patocka * The current mapping. 1502a7faeb1SMikulas Patocka * Use dm_get_live_table{_fast} or take suspend_lock for 1512a7faeb1SMikulas Patocka * dereference. 1522a7faeb1SMikulas Patocka */ 1536fa99520SPranith Kumar struct dm_table __rcu *map; 1542a7faeb1SMikulas Patocka 15586f1152bSBenjamin Marzinski struct list_head table_devices; 15686f1152bSBenjamin Marzinski struct mutex table_devices_lock; 15786f1152bSBenjamin Marzinski 1581da177e4SLinus Torvalds unsigned long flags; 1591da177e4SLinus Torvalds 160165125e1SJens Axboe struct request_queue *queue; 161a5664dadSMike Snitzer unsigned type; 1624a0b4ddfSMike Snitzer /* Protect queue and type against concurrent access. */ 163a5664dadSMike Snitzer struct mutex type_lock; 164a5664dadSMike Snitzer 16536a0456fSAlasdair G Kergon struct target_type *immutable_target_type; 16636a0456fSAlasdair G Kergon 1671da177e4SLinus Torvalds struct gendisk *disk; 1687e51f257SMike Anderson char name[16]; 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds void *interface_ptr; 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds /* 1731da177e4SLinus Torvalds * A list of ios that arrived while we were suspended. 1741da177e4SLinus Torvalds */ 175316d315bSNikanth Karthikesan atomic_t pending[2]; 1761da177e4SLinus Torvalds wait_queue_head_t wait; 17753d5914fSMikulas Patocka struct work_struct work; 1781da177e4SLinus Torvalds struct bio_list deferred; 179022c2611SMikulas Patocka spinlock_t deferred_lock; 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds /* 18229e4013dSTejun Heo * Processing queue (flush) 183304f3f6aSMilan Broz */ 184304f3f6aSMilan Broz struct workqueue_struct *wq; 185304f3f6aSMilan Broz 186304f3f6aSMilan Broz /* 1871da177e4SLinus Torvalds * io objects are allocated from here. 1881da177e4SLinus Torvalds */ 1891da177e4SLinus Torvalds mempool_t *io_pool; 1901ae49ea2SMike Snitzer mempool_t *rq_pool; 1911da177e4SLinus Torvalds 1929faf400fSStefan Bader struct bio_set *bs; 1939faf400fSStefan Bader 1941da177e4SLinus Torvalds /* 1951da177e4SLinus Torvalds * Event handling. 1961da177e4SLinus Torvalds */ 1971da177e4SLinus Torvalds atomic_t event_nr; 1981da177e4SLinus Torvalds wait_queue_head_t eventq; 1997a8c3d3bSMike Anderson atomic_t uevent_seq; 2007a8c3d3bSMike Anderson struct list_head uevent_list; 2017a8c3d3bSMike Anderson spinlock_t uevent_lock; /* Protect access to uevent_list */ 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds /* 2041da177e4SLinus Torvalds * freeze/thaw support require holding onto a super block 2051da177e4SLinus Torvalds */ 2061da177e4SLinus Torvalds struct super_block *frozen_sb; 207db8fef4fSMikulas Patocka struct block_device *bdev; 2083ac51e74SDarrick J. Wong 2093ac51e74SDarrick J. Wong /* forced geometry settings */ 2103ac51e74SDarrick J. Wong struct hd_geometry geometry; 211784aae73SMilan Broz 2122995fa78SMikulas Patocka /* kobject and completion */ 2132995fa78SMikulas Patocka struct dm_kobject_holder kobj_holder; 214be35f486SMikulas Patocka 215d87f4c14STejun Heo /* zero-length flush that will be cloned and submitted to targets */ 216d87f4c14STejun Heo struct bio flush_bio; 217fd2ed4d2SMikulas Patocka 21896b26c8cSMikulas Patocka /* the number of internal suspends */ 21996b26c8cSMikulas Patocka unsigned internal_suspend_count; 22096b26c8cSMikulas Patocka 221fd2ed4d2SMikulas Patocka struct dm_stats stats; 2222eb6e1e3SKeith Busch 2232eb6e1e3SKeith Busch struct kthread_worker kworker; 2242eb6e1e3SKeith Busch struct task_struct *kworker_task; 225de3ec86dSMike Snitzer 226de3ec86dSMike Snitzer /* for request-based merge heuristic in dm_request_fn() */ 2270ce65797SMike Snitzer unsigned seq_rq_merge_deadline_usecs; 228de3ec86dSMike Snitzer int last_rq_rw; 2290ce65797SMike Snitzer sector_t last_rq_pos; 2300ce65797SMike Snitzer ktime_t last_rq_start_time; 231bfebd1cdSMike Snitzer 232bfebd1cdSMike Snitzer /* for blk-mq request-based DM support */ 233bfebd1cdSMike Snitzer struct blk_mq_tag_set tag_set; 23417e149b8SMike Snitzer bool use_blk_mq; 2351da177e4SLinus Torvalds }; 2361da177e4SLinus Torvalds 23717e149b8SMike Snitzer #ifdef CONFIG_DM_MQ_DEFAULT 23817e149b8SMike Snitzer static bool use_blk_mq = true; 23917e149b8SMike Snitzer #else 24017e149b8SMike Snitzer static bool use_blk_mq = false; 24117e149b8SMike Snitzer #endif 24217e149b8SMike Snitzer 24317e149b8SMike Snitzer bool dm_use_blk_mq(struct mapped_device *md) 24417e149b8SMike Snitzer { 24517e149b8SMike Snitzer return md->use_blk_mq; 24617e149b8SMike Snitzer } 24717e149b8SMike Snitzer 248e6ee8c0bSKiyoshi Ueda /* 249e6ee8c0bSKiyoshi Ueda * For mempools pre-allocation at the table loading time. 250e6ee8c0bSKiyoshi Ueda */ 251e6ee8c0bSKiyoshi Ueda struct dm_md_mempools { 252e6ee8c0bSKiyoshi Ueda mempool_t *io_pool; 2531ae49ea2SMike Snitzer mempool_t *rq_pool; 254e6ee8c0bSKiyoshi Ueda struct bio_set *bs; 255e6ee8c0bSKiyoshi Ueda }; 256e6ee8c0bSKiyoshi Ueda 25786f1152bSBenjamin Marzinski struct table_device { 25886f1152bSBenjamin Marzinski struct list_head list; 25986f1152bSBenjamin Marzinski atomic_t count; 26086f1152bSBenjamin Marzinski struct dm_dev dm_dev; 26186f1152bSBenjamin Marzinski }; 26286f1152bSBenjamin Marzinski 2636cfa5857SMike Snitzer #define RESERVED_BIO_BASED_IOS 16 2646cfa5857SMike Snitzer #define RESERVED_REQUEST_BASED_IOS 256 265f4790826SMike Snitzer #define RESERVED_MAX_IOS 1024 266e18b890bSChristoph Lameter static struct kmem_cache *_io_cache; 2678fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache; 2681ae49ea2SMike Snitzer static struct kmem_cache *_rq_cache; 26994818742SKent Overstreet 270f4790826SMike Snitzer /* 271e8603136SMike Snitzer * Bio-based DM's mempools' reserved IOs set by the user. 272e8603136SMike Snitzer */ 273e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS; 274e8603136SMike Snitzer 275e8603136SMike Snitzer /* 276f4790826SMike Snitzer * Request-based DM's mempools' reserved IOs set by the user. 277f4790826SMike Snitzer */ 278f4790826SMike Snitzer static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS; 279f4790826SMike Snitzer 28009c2d531SMike Snitzer static unsigned __dm_get_module_param(unsigned *module_param, 281f4790826SMike Snitzer unsigned def, unsigned max) 282f4790826SMike Snitzer { 28309c2d531SMike Snitzer unsigned param = ACCESS_ONCE(*module_param); 28409c2d531SMike Snitzer unsigned modified_param = 0; 285f4790826SMike Snitzer 28609c2d531SMike Snitzer if (!param) 28709c2d531SMike Snitzer modified_param = def; 28809c2d531SMike Snitzer else if (param > max) 28909c2d531SMike Snitzer modified_param = max; 290f4790826SMike Snitzer 29109c2d531SMike Snitzer if (modified_param) { 29209c2d531SMike Snitzer (void)cmpxchg(module_param, param, modified_param); 29309c2d531SMike Snitzer param = modified_param; 294f4790826SMike Snitzer } 295f4790826SMike Snitzer 29609c2d531SMike Snitzer return param; 297f4790826SMike Snitzer } 298f4790826SMike Snitzer 299e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void) 300e8603136SMike Snitzer { 30109c2d531SMike Snitzer return __dm_get_module_param(&reserved_bio_based_ios, 302e8603136SMike Snitzer RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS); 303e8603136SMike Snitzer } 304e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios); 305e8603136SMike Snitzer 306f4790826SMike Snitzer unsigned dm_get_reserved_rq_based_ios(void) 307f4790826SMike Snitzer { 30809c2d531SMike Snitzer return __dm_get_module_param(&reserved_rq_based_ios, 309f4790826SMike Snitzer RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS); 310f4790826SMike Snitzer } 311f4790826SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios); 312f4790826SMike Snitzer 3131da177e4SLinus Torvalds static int __init local_init(void) 3141da177e4SLinus Torvalds { 31551157b4aSKiyoshi Ueda int r = -ENOMEM; 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds /* allocate a slab for the dm_ios */ 318028867acSAlasdair G Kergon _io_cache = KMEM_CACHE(dm_io, 0); 3191da177e4SLinus Torvalds if (!_io_cache) 32051157b4aSKiyoshi Ueda return r; 3211da177e4SLinus Torvalds 3228fbf26adSKiyoshi Ueda _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0); 3238fbf26adSKiyoshi Ueda if (!_rq_tio_cache) 324dba14160SMikulas Patocka goto out_free_io_cache; 3258fbf26adSKiyoshi Ueda 3261ae49ea2SMike Snitzer _rq_cache = kmem_cache_create("dm_clone_request", sizeof(struct request), 3271ae49ea2SMike Snitzer __alignof__(struct request), 0, NULL); 3281ae49ea2SMike Snitzer if (!_rq_cache) 3291ae49ea2SMike Snitzer goto out_free_rq_tio_cache; 3301ae49ea2SMike Snitzer 33151e5b2bdSMike Anderson r = dm_uevent_init(); 33251157b4aSKiyoshi Ueda if (r) 3331ae49ea2SMike Snitzer goto out_free_rq_cache; 33451e5b2bdSMike Anderson 335acfe0ad7SMikulas Patocka deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1); 336acfe0ad7SMikulas Patocka if (!deferred_remove_workqueue) { 337acfe0ad7SMikulas Patocka r = -ENOMEM; 338acfe0ad7SMikulas Patocka goto out_uevent_exit; 339acfe0ad7SMikulas Patocka } 340acfe0ad7SMikulas Patocka 3411da177e4SLinus Torvalds _major = major; 3421da177e4SLinus Torvalds r = register_blkdev(_major, _name); 34351157b4aSKiyoshi Ueda if (r < 0) 344acfe0ad7SMikulas Patocka goto out_free_workqueue; 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds if (!_major) 3471da177e4SLinus Torvalds _major = r; 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds return 0; 35051157b4aSKiyoshi Ueda 351acfe0ad7SMikulas Patocka out_free_workqueue: 352acfe0ad7SMikulas Patocka destroy_workqueue(deferred_remove_workqueue); 35351157b4aSKiyoshi Ueda out_uevent_exit: 35451157b4aSKiyoshi Ueda dm_uevent_exit(); 3551ae49ea2SMike Snitzer out_free_rq_cache: 3561ae49ea2SMike Snitzer kmem_cache_destroy(_rq_cache); 3578fbf26adSKiyoshi Ueda out_free_rq_tio_cache: 3588fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_tio_cache); 35951157b4aSKiyoshi Ueda out_free_io_cache: 36051157b4aSKiyoshi Ueda kmem_cache_destroy(_io_cache); 36151157b4aSKiyoshi Ueda 36251157b4aSKiyoshi Ueda return r; 3631da177e4SLinus Torvalds } 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds static void local_exit(void) 3661da177e4SLinus Torvalds { 3672c140a24SMikulas Patocka flush_scheduled_work(); 368acfe0ad7SMikulas Patocka destroy_workqueue(deferred_remove_workqueue); 3692c140a24SMikulas Patocka 3701ae49ea2SMike Snitzer kmem_cache_destroy(_rq_cache); 3718fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_tio_cache); 3721da177e4SLinus Torvalds kmem_cache_destroy(_io_cache); 37300d59405SAkinobu Mita unregister_blkdev(_major, _name); 37451e5b2bdSMike Anderson dm_uevent_exit(); 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds _major = 0; 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds DMINFO("cleaned up"); 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds 381b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = { 3821da177e4SLinus Torvalds local_init, 3831da177e4SLinus Torvalds dm_target_init, 3841da177e4SLinus Torvalds dm_linear_init, 3851da177e4SLinus Torvalds dm_stripe_init, 386952b3557SMikulas Patocka dm_io_init, 387945fa4d2SMikulas Patocka dm_kcopyd_init, 3881da177e4SLinus Torvalds dm_interface_init, 389fd2ed4d2SMikulas Patocka dm_statistics_init, 3901da177e4SLinus Torvalds }; 3911da177e4SLinus Torvalds 392b9249e55SAlasdair G Kergon static void (*_exits[])(void) = { 3931da177e4SLinus Torvalds local_exit, 3941da177e4SLinus Torvalds dm_target_exit, 3951da177e4SLinus Torvalds dm_linear_exit, 3961da177e4SLinus Torvalds dm_stripe_exit, 397952b3557SMikulas Patocka dm_io_exit, 398945fa4d2SMikulas Patocka dm_kcopyd_exit, 3991da177e4SLinus Torvalds dm_interface_exit, 400fd2ed4d2SMikulas Patocka dm_statistics_exit, 4011da177e4SLinus Torvalds }; 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds static int __init dm_init(void) 4041da177e4SLinus Torvalds { 4051da177e4SLinus Torvalds const int count = ARRAY_SIZE(_inits); 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds int r, i; 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds for (i = 0; i < count; i++) { 4101da177e4SLinus Torvalds r = _inits[i](); 4111da177e4SLinus Torvalds if (r) 4121da177e4SLinus Torvalds goto bad; 4131da177e4SLinus Torvalds } 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds return 0; 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds bad: 4181da177e4SLinus Torvalds while (i--) 4191da177e4SLinus Torvalds _exits[i](); 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds return r; 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds static void __exit dm_exit(void) 4251da177e4SLinus Torvalds { 4261da177e4SLinus Torvalds int i = ARRAY_SIZE(_exits); 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds while (i--) 4291da177e4SLinus Torvalds _exits[i](); 430d15b774cSAlasdair G Kergon 431d15b774cSAlasdair G Kergon /* 432d15b774cSAlasdair G Kergon * Should be empty by this point. 433d15b774cSAlasdair G Kergon */ 434d15b774cSAlasdair G Kergon idr_destroy(&_minor_idr); 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds /* 4381da177e4SLinus Torvalds * Block device functions 4391da177e4SLinus Torvalds */ 440432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md) 441432a212cSMike Anderson { 442432a212cSMike Anderson return test_bit(DMF_DELETING, &md->flags); 443432a212cSMike Anderson } 444432a212cSMike Anderson 445fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode) 4461da177e4SLinus Torvalds { 4471da177e4SLinus Torvalds struct mapped_device *md; 4481da177e4SLinus Torvalds 449fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 450fba9f90eSJeff Mahoney 451fe5f9f2cSAl Viro md = bdev->bd_disk->private_data; 452fba9f90eSJeff Mahoney if (!md) 453fba9f90eSJeff Mahoney goto out; 454fba9f90eSJeff Mahoney 4555c6bd75dSAlasdair G Kergon if (test_bit(DMF_FREEING, &md->flags) || 456432a212cSMike Anderson dm_deleting_md(md)) { 457fba9f90eSJeff Mahoney md = NULL; 458fba9f90eSJeff Mahoney goto out; 459fba9f90eSJeff Mahoney } 460fba9f90eSJeff Mahoney 4611da177e4SLinus Torvalds dm_get(md); 4625c6bd75dSAlasdair G Kergon atomic_inc(&md->open_count); 463fba9f90eSJeff Mahoney out: 464fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 465fba9f90eSJeff Mahoney 466fba9f90eSJeff Mahoney return md ? 0 : -ENXIO; 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds 469db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode) 4701da177e4SLinus Torvalds { 47163a4f065SMike Snitzer struct mapped_device *md; 4726e9624b8SArnd Bergmann 4734a1aeb98SMilan Broz spin_lock(&_minor_lock); 4744a1aeb98SMilan Broz 47563a4f065SMike Snitzer md = disk->private_data; 47663a4f065SMike Snitzer if (WARN_ON(!md)) 47763a4f065SMike Snitzer goto out; 47863a4f065SMike Snitzer 4792c140a24SMikulas Patocka if (atomic_dec_and_test(&md->open_count) && 4802c140a24SMikulas Patocka (test_bit(DMF_DEFERRED_REMOVE, &md->flags))) 481acfe0ad7SMikulas Patocka queue_work(deferred_remove_workqueue, &deferred_remove_work); 4822c140a24SMikulas Patocka 4831da177e4SLinus Torvalds dm_put(md); 48463a4f065SMike Snitzer out: 4854a1aeb98SMilan Broz spin_unlock(&_minor_lock); 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds 4885c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md) 4895c6bd75dSAlasdair G Kergon { 4905c6bd75dSAlasdair G Kergon return atomic_read(&md->open_count); 4915c6bd75dSAlasdair G Kergon } 4925c6bd75dSAlasdair G Kergon 4935c6bd75dSAlasdair G Kergon /* 4945c6bd75dSAlasdair G Kergon * Guarantees nothing is using the device before it's deleted. 4955c6bd75dSAlasdair G Kergon */ 4962c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred) 4975c6bd75dSAlasdair G Kergon { 4985c6bd75dSAlasdair G Kergon int r = 0; 4995c6bd75dSAlasdair G Kergon 5005c6bd75dSAlasdair G Kergon spin_lock(&_minor_lock); 5015c6bd75dSAlasdair G Kergon 5022c140a24SMikulas Patocka if (dm_open_count(md)) { 5035c6bd75dSAlasdair G Kergon r = -EBUSY; 5042c140a24SMikulas Patocka if (mark_deferred) 5052c140a24SMikulas Patocka set_bit(DMF_DEFERRED_REMOVE, &md->flags); 5062c140a24SMikulas Patocka } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags)) 5072c140a24SMikulas Patocka r = -EEXIST; 5085c6bd75dSAlasdair G Kergon else 5095c6bd75dSAlasdair G Kergon set_bit(DMF_DELETING, &md->flags); 5105c6bd75dSAlasdair G Kergon 5115c6bd75dSAlasdair G Kergon spin_unlock(&_minor_lock); 5125c6bd75dSAlasdair G Kergon 5135c6bd75dSAlasdair G Kergon return r; 5145c6bd75dSAlasdair G Kergon } 5155c6bd75dSAlasdair G Kergon 5162c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md) 5172c140a24SMikulas Patocka { 5182c140a24SMikulas Patocka int r = 0; 5192c140a24SMikulas Patocka 5202c140a24SMikulas Patocka spin_lock(&_minor_lock); 5212c140a24SMikulas Patocka 5222c140a24SMikulas Patocka if (test_bit(DMF_DELETING, &md->flags)) 5232c140a24SMikulas Patocka r = -EBUSY; 5242c140a24SMikulas Patocka else 5252c140a24SMikulas Patocka clear_bit(DMF_DEFERRED_REMOVE, &md->flags); 5262c140a24SMikulas Patocka 5272c140a24SMikulas Patocka spin_unlock(&_minor_lock); 5282c140a24SMikulas Patocka 5292c140a24SMikulas Patocka return r; 5302c140a24SMikulas Patocka } 5312c140a24SMikulas Patocka 5322c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w) 5332c140a24SMikulas Patocka { 5342c140a24SMikulas Patocka dm_deferred_remove(); 5352c140a24SMikulas Patocka } 5362c140a24SMikulas Patocka 537fd2ed4d2SMikulas Patocka sector_t dm_get_size(struct mapped_device *md) 538fd2ed4d2SMikulas Patocka { 539fd2ed4d2SMikulas Patocka return get_capacity(md->disk); 540fd2ed4d2SMikulas Patocka } 541fd2ed4d2SMikulas Patocka 5429974fa2cSMike Snitzer struct request_queue *dm_get_md_queue(struct mapped_device *md) 5439974fa2cSMike Snitzer { 5449974fa2cSMike Snitzer return md->queue; 5459974fa2cSMike Snitzer } 5469974fa2cSMike Snitzer 547fd2ed4d2SMikulas Patocka struct dm_stats *dm_get_stats(struct mapped_device *md) 548fd2ed4d2SMikulas Patocka { 549fd2ed4d2SMikulas Patocka return &md->stats; 550fd2ed4d2SMikulas Patocka } 551fd2ed4d2SMikulas Patocka 5523ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 5533ac51e74SDarrick J. Wong { 5543ac51e74SDarrick J. Wong struct mapped_device *md = bdev->bd_disk->private_data; 5553ac51e74SDarrick J. Wong 5563ac51e74SDarrick J. Wong return dm_get_geometry(md, geo); 5573ac51e74SDarrick J. Wong } 5583ac51e74SDarrick J. Wong 559fe5f9f2cSAl Viro static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode, 560aa129a22SMilan Broz unsigned int cmd, unsigned long arg) 561aa129a22SMilan Broz { 562fe5f9f2cSAl Viro struct mapped_device *md = bdev->bd_disk->private_data; 56383d5e5b0SMikulas Patocka int srcu_idx; 5646c182cd8SHannes Reinecke struct dm_table *map; 565aa129a22SMilan Broz struct dm_target *tgt; 566aa129a22SMilan Broz int r = -ENOTTY; 567aa129a22SMilan Broz 5686c182cd8SHannes Reinecke retry: 56983d5e5b0SMikulas Patocka map = dm_get_live_table(md, &srcu_idx); 57083d5e5b0SMikulas Patocka 571aa129a22SMilan Broz if (!map || !dm_table_get_size(map)) 572aa129a22SMilan Broz goto out; 573aa129a22SMilan Broz 574aa129a22SMilan Broz /* We only support devices that have a single target */ 575aa129a22SMilan Broz if (dm_table_get_num_targets(map) != 1) 576aa129a22SMilan Broz goto out; 577aa129a22SMilan Broz 578aa129a22SMilan Broz tgt = dm_table_get_target(map, 0); 5794d341d82SMike Snitzer if (!tgt->type->ioctl) 5804d341d82SMike Snitzer goto out; 581aa129a22SMilan Broz 5824f186f8bSKiyoshi Ueda if (dm_suspended_md(md)) { 583aa129a22SMilan Broz r = -EAGAIN; 584aa129a22SMilan Broz goto out; 585aa129a22SMilan Broz } 586aa129a22SMilan Broz 587647b3d00SAl Viro r = tgt->type->ioctl(tgt, cmd, arg); 588aa129a22SMilan Broz 589aa129a22SMilan Broz out: 59083d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 591aa129a22SMilan Broz 5926c182cd8SHannes Reinecke if (r == -ENOTCONN) { 5936c182cd8SHannes Reinecke msleep(10); 5946c182cd8SHannes Reinecke goto retry; 5956c182cd8SHannes Reinecke } 5966c182cd8SHannes Reinecke 597aa129a22SMilan Broz return r; 598aa129a22SMilan Broz } 599aa129a22SMilan Broz 600028867acSAlasdair G Kergon static struct dm_io *alloc_io(struct mapped_device *md) 6011da177e4SLinus Torvalds { 6021da177e4SLinus Torvalds return mempool_alloc(md->io_pool, GFP_NOIO); 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds 605028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io) 6061da177e4SLinus Torvalds { 6071da177e4SLinus Torvalds mempool_free(io, md->io_pool); 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds 610028867acSAlasdair G Kergon static void free_tio(struct mapped_device *md, struct dm_target_io *tio) 6111da177e4SLinus Torvalds { 612dba14160SMikulas Patocka bio_put(&tio->clone); 6131da177e4SLinus Torvalds } 6141da177e4SLinus Torvalds 61508885643SKiyoshi Ueda static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md, 61608885643SKiyoshi Ueda gfp_t gfp_mask) 617cec47e3dSKiyoshi Ueda { 6185f015204SJun'ichi Nomura return mempool_alloc(md->io_pool, gfp_mask); 619cec47e3dSKiyoshi Ueda } 620cec47e3dSKiyoshi Ueda 621cec47e3dSKiyoshi Ueda static void free_rq_tio(struct dm_rq_target_io *tio) 622cec47e3dSKiyoshi Ueda { 6235f015204SJun'ichi Nomura mempool_free(tio, tio->md->io_pool); 624cec47e3dSKiyoshi Ueda } 625cec47e3dSKiyoshi Ueda 6261ae49ea2SMike Snitzer static struct request *alloc_clone_request(struct mapped_device *md, 6271ae49ea2SMike Snitzer gfp_t gfp_mask) 6281ae49ea2SMike Snitzer { 6291ae49ea2SMike Snitzer return mempool_alloc(md->rq_pool, gfp_mask); 6301ae49ea2SMike Snitzer } 6311ae49ea2SMike Snitzer 6321ae49ea2SMike Snitzer static void free_clone_request(struct mapped_device *md, struct request *rq) 6331ae49ea2SMike Snitzer { 6341ae49ea2SMike Snitzer mempool_free(rq, md->rq_pool); 6351ae49ea2SMike Snitzer } 6361ae49ea2SMike Snitzer 63790abb8c4SKiyoshi Ueda static int md_in_flight(struct mapped_device *md) 63890abb8c4SKiyoshi Ueda { 63990abb8c4SKiyoshi Ueda return atomic_read(&md->pending[READ]) + 64090abb8c4SKiyoshi Ueda atomic_read(&md->pending[WRITE]); 64190abb8c4SKiyoshi Ueda } 64290abb8c4SKiyoshi Ueda 6433eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io) 6443eaf840eSJun'ichi "Nick" Nomura { 6453eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 646fd2ed4d2SMikulas Patocka struct bio *bio = io->bio; 647c9959059STejun Heo int cpu; 648fd2ed4d2SMikulas Patocka int rw = bio_data_dir(bio); 6493eaf840eSJun'ichi "Nick" Nomura 6503eaf840eSJun'ichi "Nick" Nomura io->start_time = jiffies; 6513eaf840eSJun'ichi "Nick" Nomura 652074a7acaSTejun Heo cpu = part_stat_lock(); 653074a7acaSTejun Heo part_round_stats(cpu, &dm_disk(md)->part0); 654074a7acaSTejun Heo part_stat_unlock(); 6551e9bb880SShaohua Li atomic_set(&dm_disk(md)->part0.in_flight[rw], 6561e9bb880SShaohua Li atomic_inc_return(&md->pending[rw])); 657fd2ed4d2SMikulas Patocka 658fd2ed4d2SMikulas Patocka if (unlikely(dm_stats_used(&md->stats))) 6594f024f37SKent Overstreet dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector, 660fd2ed4d2SMikulas Patocka bio_sectors(bio), false, 0, &io->stats_aux); 6613eaf840eSJun'ichi "Nick" Nomura } 6623eaf840eSJun'ichi "Nick" Nomura 663d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io) 6643eaf840eSJun'ichi "Nick" Nomura { 6653eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 6663eaf840eSJun'ichi "Nick" Nomura struct bio *bio = io->bio; 6673eaf840eSJun'ichi "Nick" Nomura unsigned long duration = jiffies - io->start_time; 66818c0b223SGu Zheng int pending; 6693eaf840eSJun'ichi "Nick" Nomura int rw = bio_data_dir(bio); 6703eaf840eSJun'ichi "Nick" Nomura 67118c0b223SGu Zheng generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time); 6723eaf840eSJun'ichi "Nick" Nomura 673fd2ed4d2SMikulas Patocka if (unlikely(dm_stats_used(&md->stats))) 6744f024f37SKent Overstreet dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector, 675fd2ed4d2SMikulas Patocka bio_sectors(bio), true, duration, &io->stats_aux); 676fd2ed4d2SMikulas Patocka 677af7e466aSMikulas Patocka /* 678af7e466aSMikulas Patocka * After this is decremented the bio must not be touched if it is 679d87f4c14STejun Heo * a flush. 680af7e466aSMikulas Patocka */ 6811e9bb880SShaohua Li pending = atomic_dec_return(&md->pending[rw]); 6821e9bb880SShaohua Li atomic_set(&dm_disk(md)->part0.in_flight[rw], pending); 683316d315bSNikanth Karthikesan pending += atomic_read(&md->pending[rw^0x1]); 6843eaf840eSJun'ichi "Nick" Nomura 685d221d2e7SMikulas Patocka /* nudge anyone waiting on suspend queue */ 686d221d2e7SMikulas Patocka if (!pending) 687d221d2e7SMikulas Patocka wake_up(&md->wait); 6883eaf840eSJun'ichi "Nick" Nomura } 6893eaf840eSJun'ichi "Nick" Nomura 6901da177e4SLinus Torvalds /* 6911da177e4SLinus Torvalds * Add the bio to the list of deferred io. 6921da177e4SLinus Torvalds */ 69392c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio) 6941da177e4SLinus Torvalds { 69505447420SKiyoshi Ueda unsigned long flags; 6961da177e4SLinus Torvalds 69705447420SKiyoshi Ueda spin_lock_irqsave(&md->deferred_lock, flags); 6981da177e4SLinus Torvalds bio_list_add(&md->deferred, bio); 69905447420SKiyoshi Ueda spin_unlock_irqrestore(&md->deferred_lock, flags); 70092c63902SMikulas Patocka queue_work(md->wq, &md->work); 7011da177e4SLinus Torvalds } 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds /* 7041da177e4SLinus Torvalds * Everyone (including functions in this file), should use this 7051da177e4SLinus Torvalds * function to access the md->map field, and make sure they call 70683d5e5b0SMikulas Patocka * dm_put_live_table() when finished. 7071da177e4SLinus Torvalds */ 70883d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier) 7091da177e4SLinus Torvalds { 71083d5e5b0SMikulas Patocka *srcu_idx = srcu_read_lock(&md->io_barrier); 7111da177e4SLinus Torvalds 71283d5e5b0SMikulas Patocka return srcu_dereference(md->map, &md->io_barrier); 71383d5e5b0SMikulas Patocka } 7141da177e4SLinus Torvalds 71583d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier) 71683d5e5b0SMikulas Patocka { 71783d5e5b0SMikulas Patocka srcu_read_unlock(&md->io_barrier, srcu_idx); 71883d5e5b0SMikulas Patocka } 71983d5e5b0SMikulas Patocka 72083d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md) 72183d5e5b0SMikulas Patocka { 72283d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 72383d5e5b0SMikulas Patocka synchronize_rcu_expedited(); 72483d5e5b0SMikulas Patocka } 72583d5e5b0SMikulas Patocka 72683d5e5b0SMikulas Patocka /* 72783d5e5b0SMikulas Patocka * A fast alternative to dm_get_live_table/dm_put_live_table. 72883d5e5b0SMikulas Patocka * The caller must not block between these two functions. 72983d5e5b0SMikulas Patocka */ 73083d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU) 73183d5e5b0SMikulas Patocka { 73283d5e5b0SMikulas Patocka rcu_read_lock(); 73383d5e5b0SMikulas Patocka return rcu_dereference(md->map); 73483d5e5b0SMikulas Patocka } 73583d5e5b0SMikulas Patocka 73683d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU) 73783d5e5b0SMikulas Patocka { 73883d5e5b0SMikulas Patocka rcu_read_unlock(); 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 7413ac51e74SDarrick J. Wong /* 74286f1152bSBenjamin Marzinski * Open a table device so we can use it as a map destination. 74386f1152bSBenjamin Marzinski */ 74486f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev, 74586f1152bSBenjamin Marzinski struct mapped_device *md) 74686f1152bSBenjamin Marzinski { 74786f1152bSBenjamin Marzinski static char *_claim_ptr = "I belong to device-mapper"; 74886f1152bSBenjamin Marzinski struct block_device *bdev; 74986f1152bSBenjamin Marzinski 75086f1152bSBenjamin Marzinski int r; 75186f1152bSBenjamin Marzinski 75286f1152bSBenjamin Marzinski BUG_ON(td->dm_dev.bdev); 75386f1152bSBenjamin Marzinski 75486f1152bSBenjamin Marzinski bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr); 75586f1152bSBenjamin Marzinski if (IS_ERR(bdev)) 75686f1152bSBenjamin Marzinski return PTR_ERR(bdev); 75786f1152bSBenjamin Marzinski 75886f1152bSBenjamin Marzinski r = bd_link_disk_holder(bdev, dm_disk(md)); 75986f1152bSBenjamin Marzinski if (r) { 76086f1152bSBenjamin Marzinski blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL); 76186f1152bSBenjamin Marzinski return r; 76286f1152bSBenjamin Marzinski } 76386f1152bSBenjamin Marzinski 76486f1152bSBenjamin Marzinski td->dm_dev.bdev = bdev; 76586f1152bSBenjamin Marzinski return 0; 76686f1152bSBenjamin Marzinski } 76786f1152bSBenjamin Marzinski 76886f1152bSBenjamin Marzinski /* 76986f1152bSBenjamin Marzinski * Close a table device that we've been using. 77086f1152bSBenjamin Marzinski */ 77186f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md) 77286f1152bSBenjamin Marzinski { 77386f1152bSBenjamin Marzinski if (!td->dm_dev.bdev) 77486f1152bSBenjamin Marzinski return; 77586f1152bSBenjamin Marzinski 77686f1152bSBenjamin Marzinski bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md)); 77786f1152bSBenjamin Marzinski blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL); 77886f1152bSBenjamin Marzinski td->dm_dev.bdev = NULL; 77986f1152bSBenjamin Marzinski } 78086f1152bSBenjamin Marzinski 78186f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev, 78286f1152bSBenjamin Marzinski fmode_t mode) { 78386f1152bSBenjamin Marzinski struct table_device *td; 78486f1152bSBenjamin Marzinski 78586f1152bSBenjamin Marzinski list_for_each_entry(td, l, list) 78686f1152bSBenjamin Marzinski if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode) 78786f1152bSBenjamin Marzinski return td; 78886f1152bSBenjamin Marzinski 78986f1152bSBenjamin Marzinski return NULL; 79086f1152bSBenjamin Marzinski } 79186f1152bSBenjamin Marzinski 79286f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode, 79386f1152bSBenjamin Marzinski struct dm_dev **result) { 79486f1152bSBenjamin Marzinski int r; 79586f1152bSBenjamin Marzinski struct table_device *td; 79686f1152bSBenjamin Marzinski 79786f1152bSBenjamin Marzinski mutex_lock(&md->table_devices_lock); 79886f1152bSBenjamin Marzinski td = find_table_device(&md->table_devices, dev, mode); 79986f1152bSBenjamin Marzinski if (!td) { 80086f1152bSBenjamin Marzinski td = kmalloc(sizeof(*td), GFP_KERNEL); 80186f1152bSBenjamin Marzinski if (!td) { 80286f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 80386f1152bSBenjamin Marzinski return -ENOMEM; 80486f1152bSBenjamin Marzinski } 80586f1152bSBenjamin Marzinski 80686f1152bSBenjamin Marzinski td->dm_dev.mode = mode; 80786f1152bSBenjamin Marzinski td->dm_dev.bdev = NULL; 80886f1152bSBenjamin Marzinski 80986f1152bSBenjamin Marzinski if ((r = open_table_device(td, dev, md))) { 81086f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 81186f1152bSBenjamin Marzinski kfree(td); 81286f1152bSBenjamin Marzinski return r; 81386f1152bSBenjamin Marzinski } 81486f1152bSBenjamin Marzinski 81586f1152bSBenjamin Marzinski format_dev_t(td->dm_dev.name, dev); 81686f1152bSBenjamin Marzinski 81786f1152bSBenjamin Marzinski atomic_set(&td->count, 0); 81886f1152bSBenjamin Marzinski list_add(&td->list, &md->table_devices); 81986f1152bSBenjamin Marzinski } 82086f1152bSBenjamin Marzinski atomic_inc(&td->count); 82186f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 82286f1152bSBenjamin Marzinski 82386f1152bSBenjamin Marzinski *result = &td->dm_dev; 82486f1152bSBenjamin Marzinski return 0; 82586f1152bSBenjamin Marzinski } 82686f1152bSBenjamin Marzinski EXPORT_SYMBOL_GPL(dm_get_table_device); 82786f1152bSBenjamin Marzinski 82886f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d) 82986f1152bSBenjamin Marzinski { 83086f1152bSBenjamin Marzinski struct table_device *td = container_of(d, struct table_device, dm_dev); 83186f1152bSBenjamin Marzinski 83286f1152bSBenjamin Marzinski mutex_lock(&md->table_devices_lock); 83386f1152bSBenjamin Marzinski if (atomic_dec_and_test(&td->count)) { 83486f1152bSBenjamin Marzinski close_table_device(td, md); 83586f1152bSBenjamin Marzinski list_del(&td->list); 83686f1152bSBenjamin Marzinski kfree(td); 83786f1152bSBenjamin Marzinski } 83886f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 83986f1152bSBenjamin Marzinski } 84086f1152bSBenjamin Marzinski EXPORT_SYMBOL(dm_put_table_device); 84186f1152bSBenjamin Marzinski 84286f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices) 84386f1152bSBenjamin Marzinski { 84486f1152bSBenjamin Marzinski struct list_head *tmp, *next; 84586f1152bSBenjamin Marzinski 84686f1152bSBenjamin Marzinski list_for_each_safe(tmp, next, devices) { 84786f1152bSBenjamin Marzinski struct table_device *td = list_entry(tmp, struct table_device, list); 84886f1152bSBenjamin Marzinski 84986f1152bSBenjamin Marzinski DMWARN("dm_destroy: %s still exists with %d references", 85086f1152bSBenjamin Marzinski td->dm_dev.name, atomic_read(&td->count)); 85186f1152bSBenjamin Marzinski kfree(td); 85286f1152bSBenjamin Marzinski } 85386f1152bSBenjamin Marzinski } 85486f1152bSBenjamin Marzinski 85586f1152bSBenjamin Marzinski /* 8563ac51e74SDarrick J. Wong * Get the geometry associated with a dm device 8573ac51e74SDarrick J. Wong */ 8583ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) 8593ac51e74SDarrick J. Wong { 8603ac51e74SDarrick J. Wong *geo = md->geometry; 8613ac51e74SDarrick J. Wong 8623ac51e74SDarrick J. Wong return 0; 8633ac51e74SDarrick J. Wong } 8643ac51e74SDarrick J. Wong 8653ac51e74SDarrick J. Wong /* 8663ac51e74SDarrick J. Wong * Set the geometry of a device. 8673ac51e74SDarrick J. Wong */ 8683ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) 8693ac51e74SDarrick J. Wong { 8703ac51e74SDarrick J. Wong sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; 8713ac51e74SDarrick J. Wong 8723ac51e74SDarrick J. Wong if (geo->start > sz) { 8733ac51e74SDarrick J. Wong DMWARN("Start sector is beyond the geometry limits."); 8743ac51e74SDarrick J. Wong return -EINVAL; 8753ac51e74SDarrick J. Wong } 8763ac51e74SDarrick J. Wong 8773ac51e74SDarrick J. Wong md->geometry = *geo; 8783ac51e74SDarrick J. Wong 8793ac51e74SDarrick J. Wong return 0; 8803ac51e74SDarrick J. Wong } 8813ac51e74SDarrick J. Wong 8821da177e4SLinus Torvalds /*----------------------------------------------------------------- 8831da177e4SLinus Torvalds * CRUD START: 8841da177e4SLinus Torvalds * A more elegant soln is in the works that uses the queue 8851da177e4SLinus Torvalds * merge fn, unfortunately there are a couple of changes to 8861da177e4SLinus Torvalds * the block layer that I want to make for this. So in the 8871da177e4SLinus Torvalds * interests of getting something for people to use I give 8881da177e4SLinus Torvalds * you this clearly demarcated crap. 8891da177e4SLinus Torvalds *---------------------------------------------------------------*/ 8901da177e4SLinus Torvalds 8912e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md) 8922e93ccc1SKiyoshi Ueda { 8932e93ccc1SKiyoshi Ueda return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 8942e93ccc1SKiyoshi Ueda } 8952e93ccc1SKiyoshi Ueda 8961da177e4SLinus Torvalds /* 8971da177e4SLinus Torvalds * Decrements the number of outstanding ios that a bio has been 8981da177e4SLinus Torvalds * cloned into, completing the original io if necc. 8991da177e4SLinus Torvalds */ 900858119e1SArjan van de Ven static void dec_pending(struct dm_io *io, int error) 9011da177e4SLinus Torvalds { 9022e93ccc1SKiyoshi Ueda unsigned long flags; 903b35f8caaSMilan Broz int io_error; 904b35f8caaSMilan Broz struct bio *bio; 905b35f8caaSMilan Broz struct mapped_device *md = io->md; 9062e93ccc1SKiyoshi Ueda 9072e93ccc1SKiyoshi Ueda /* Push-back supersedes any I/O errors */ 908f88fb981SKiyoshi Ueda if (unlikely(error)) { 909f88fb981SKiyoshi Ueda spin_lock_irqsave(&io->endio_lock, flags); 910f88fb981SKiyoshi Ueda if (!(io->error > 0 && __noflush_suspending(md))) 9111da177e4SLinus Torvalds io->error = error; 912f88fb981SKiyoshi Ueda spin_unlock_irqrestore(&io->endio_lock, flags); 913f88fb981SKiyoshi Ueda } 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds if (atomic_dec_and_test(&io->io_count)) { 9162e93ccc1SKiyoshi Ueda if (io->error == DM_ENDIO_REQUEUE) { 9172e93ccc1SKiyoshi Ueda /* 9182e93ccc1SKiyoshi Ueda * Target requested pushing back the I/O. 9192e93ccc1SKiyoshi Ueda */ 920022c2611SMikulas Patocka spin_lock_irqsave(&md->deferred_lock, flags); 9216a8736d1STejun Heo if (__noflush_suspending(md)) 9226a8736d1STejun Heo bio_list_add_head(&md->deferred, io->bio); 9236a8736d1STejun Heo else 9242e93ccc1SKiyoshi Ueda /* noflush suspend was interrupted. */ 9252e93ccc1SKiyoshi Ueda io->error = -EIO; 926022c2611SMikulas Patocka spin_unlock_irqrestore(&md->deferred_lock, flags); 9272e93ccc1SKiyoshi Ueda } 9282e93ccc1SKiyoshi Ueda 929b35f8caaSMilan Broz io_error = io->error; 930b35f8caaSMilan Broz bio = io->bio; 931af7e466aSMikulas Patocka end_io_acct(io); 932a97f925aSMikulas Patocka free_io(md, io); 9331da177e4SLinus Torvalds 9346a8736d1STejun Heo if (io_error == DM_ENDIO_REQUEUE) 9356a8736d1STejun Heo return; 9366a8736d1STejun Heo 9374f024f37SKent Overstreet if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) { 9381da177e4SLinus Torvalds /* 9396a8736d1STejun Heo * Preflush done for flush with data, reissue 9406a8736d1STejun Heo * without REQ_FLUSH. 9411da177e4SLinus Torvalds */ 9426a8736d1STejun Heo bio->bi_rw &= ~REQ_FLUSH; 9436a8736d1STejun Heo queue_io(md, bio); 9445f3ea37cSArnaldo Carvalho de Melo } else { 945b372d360SMike Snitzer /* done with normal IO or empty flush */ 9460a82a8d1SLinus Torvalds trace_block_bio_complete(md->queue, bio, io_error); 947b35f8caaSMilan Broz bio_endio(bio, io_error); 9482e93ccc1SKiyoshi Ueda } 9491da177e4SLinus Torvalds } 950af7e466aSMikulas Patocka } 9511da177e4SLinus Torvalds 9527eee4ae2SMike Snitzer static void disable_write_same(struct mapped_device *md) 9537eee4ae2SMike Snitzer { 9547eee4ae2SMike Snitzer struct queue_limits *limits = dm_get_queue_limits(md); 9557eee4ae2SMike Snitzer 9567eee4ae2SMike Snitzer /* device doesn't really support WRITE SAME, disable it */ 9577eee4ae2SMike Snitzer limits->max_write_same_sectors = 0; 9587eee4ae2SMike Snitzer } 9597eee4ae2SMike Snitzer 9606712ecf8SNeilBrown static void clone_endio(struct bio *bio, int error) 9611da177e4SLinus Torvalds { 9625164beceSzhendong chen int r = error; 963bfc6d41cSMikulas Patocka struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); 964b35f8caaSMilan Broz struct dm_io *io = tio->io; 9659faf400fSStefan Bader struct mapped_device *md = tio->io->md; 9661da177e4SLinus Torvalds dm_endio_fn endio = tio->ti->type->end_io; 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds if (!bio_flagged(bio, BIO_UPTODATE) && !error) 9691da177e4SLinus Torvalds error = -EIO; 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds if (endio) { 9727de3ee57SMikulas Patocka r = endio(tio->ti, bio, error); 9732e93ccc1SKiyoshi Ueda if (r < 0 || r == DM_ENDIO_REQUEUE) 9742e93ccc1SKiyoshi Ueda /* 9752e93ccc1SKiyoshi Ueda * error and requeue request are handled 9762e93ccc1SKiyoshi Ueda * in dec_pending(). 9772e93ccc1SKiyoshi Ueda */ 9781da177e4SLinus Torvalds error = r; 97945cbcd79SKiyoshi Ueda else if (r == DM_ENDIO_INCOMPLETE) 98045cbcd79SKiyoshi Ueda /* The target will handle the io */ 9816712ecf8SNeilBrown return; 98245cbcd79SKiyoshi Ueda else if (r) { 98345cbcd79SKiyoshi Ueda DMWARN("unimplemented target endio return value: %d", r); 98445cbcd79SKiyoshi Ueda BUG(); 98545cbcd79SKiyoshi Ueda } 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds 9887eee4ae2SMike Snitzer if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) && 9897eee4ae2SMike Snitzer !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors)) 9907eee4ae2SMike Snitzer disable_write_same(md); 9917eee4ae2SMike Snitzer 9929faf400fSStefan Bader free_tio(md, tio); 993b35f8caaSMilan Broz dec_pending(io, error); 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds 996*78d8e58aSMike Snitzer /* 997*78d8e58aSMike Snitzer * Partial completion handling for request-based dm 998*78d8e58aSMike Snitzer */ 999*78d8e58aSMike Snitzer static void end_clone_bio(struct bio *clone, int error) 1000*78d8e58aSMike Snitzer { 1001*78d8e58aSMike Snitzer struct dm_rq_clone_bio_info *info = 1002*78d8e58aSMike Snitzer container_of(clone, struct dm_rq_clone_bio_info, clone); 1003*78d8e58aSMike Snitzer struct dm_rq_target_io *tio = info->tio; 1004*78d8e58aSMike Snitzer struct bio *bio = info->orig; 1005*78d8e58aSMike Snitzer unsigned int nr_bytes = info->orig->bi_iter.bi_size; 1006*78d8e58aSMike Snitzer 1007*78d8e58aSMike Snitzer bio_put(clone); 1008*78d8e58aSMike Snitzer 1009*78d8e58aSMike Snitzer if (tio->error) 1010*78d8e58aSMike Snitzer /* 1011*78d8e58aSMike Snitzer * An error has already been detected on the request. 1012*78d8e58aSMike Snitzer * Once error occurred, just let clone->end_io() handle 1013*78d8e58aSMike Snitzer * the remainder. 1014*78d8e58aSMike Snitzer */ 1015*78d8e58aSMike Snitzer return; 1016*78d8e58aSMike Snitzer else if (error) { 1017*78d8e58aSMike Snitzer /* 1018*78d8e58aSMike Snitzer * Don't notice the error to the upper layer yet. 1019*78d8e58aSMike Snitzer * The error handling decision is made by the target driver, 1020*78d8e58aSMike Snitzer * when the request is completed. 1021*78d8e58aSMike Snitzer */ 1022*78d8e58aSMike Snitzer tio->error = error; 1023*78d8e58aSMike Snitzer return; 1024*78d8e58aSMike Snitzer } 1025*78d8e58aSMike Snitzer 1026*78d8e58aSMike Snitzer /* 1027*78d8e58aSMike Snitzer * I/O for the bio successfully completed. 1028*78d8e58aSMike Snitzer * Notice the data completion to the upper layer. 1029*78d8e58aSMike Snitzer */ 1030*78d8e58aSMike Snitzer 1031*78d8e58aSMike Snitzer /* 1032*78d8e58aSMike Snitzer * bios are processed from the head of the list. 1033*78d8e58aSMike Snitzer * So the completing bio should always be rq->bio. 1034*78d8e58aSMike Snitzer * If it's not, something wrong is happening. 1035*78d8e58aSMike Snitzer */ 1036*78d8e58aSMike Snitzer if (tio->orig->bio != bio) 1037*78d8e58aSMike Snitzer DMERR("bio completion is going in the middle of the request"); 1038*78d8e58aSMike Snitzer 1039*78d8e58aSMike Snitzer /* 1040*78d8e58aSMike Snitzer * Update the original request. 1041*78d8e58aSMike Snitzer * Do not use blk_end_request() here, because it may complete 1042*78d8e58aSMike Snitzer * the original request before the clone, and break the ordering. 1043*78d8e58aSMike Snitzer */ 1044*78d8e58aSMike Snitzer blk_update_request(tio->orig, 0, nr_bytes); 1045*78d8e58aSMike Snitzer } 1046*78d8e58aSMike Snitzer 1047bfebd1cdSMike Snitzer static struct dm_rq_target_io *tio_from_request(struct request *rq) 1048bfebd1cdSMike Snitzer { 1049bfebd1cdSMike Snitzer return (rq->q->mq_ops ? blk_mq_rq_to_pdu(rq) : rq->special); 1050bfebd1cdSMike Snitzer } 1051bfebd1cdSMike Snitzer 1052e262f347SMikulas Patocka static void rq_end_stats(struct mapped_device *md, struct request *orig) 1053e262f347SMikulas Patocka { 1054e262f347SMikulas Patocka if (unlikely(dm_stats_used(&md->stats))) { 1055e262f347SMikulas Patocka struct dm_rq_target_io *tio = tio_from_request(orig); 1056e262f347SMikulas Patocka tio->duration_jiffies = jiffies - tio->duration_jiffies; 1057e262f347SMikulas Patocka dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig), 1058e262f347SMikulas Patocka tio->n_sectors, true, tio->duration_jiffies, 1059e262f347SMikulas Patocka &tio->stats_aux); 1060e262f347SMikulas Patocka } 1061e262f347SMikulas Patocka } 1062e262f347SMikulas Patocka 1063cec47e3dSKiyoshi Ueda /* 1064cec47e3dSKiyoshi Ueda * Don't touch any member of the md after calling this function because 1065cec47e3dSKiyoshi Ueda * the md may be freed in dm_put() at the end of this function. 1066cec47e3dSKiyoshi Ueda * Or do dm_get() before calling this function and dm_put() later. 1067cec47e3dSKiyoshi Ueda */ 1068466d89a6SKeith Busch static void rq_completed(struct mapped_device *md, int rw, bool run_queue) 1069cec47e3dSKiyoshi Ueda { 10709a0e609eSMike Snitzer int nr_requests_pending; 10719a0e609eSMike Snitzer 1072b4324feeSKiyoshi Ueda atomic_dec(&md->pending[rw]); 1073cec47e3dSKiyoshi Ueda 1074cec47e3dSKiyoshi Ueda /* nudge anyone waiting on suspend queue */ 10759a0e609eSMike Snitzer nr_requests_pending = md_in_flight(md); 10769a0e609eSMike Snitzer if (!nr_requests_pending) 1077cec47e3dSKiyoshi Ueda wake_up(&md->wait); 1078cec47e3dSKiyoshi Ueda 1079a8c32a5cSJens Axboe /* 1080a8c32a5cSJens Axboe * Run this off this callpath, as drivers could invoke end_io while 1081a8c32a5cSJens Axboe * inside their request_fn (and holding the queue lock). Calling 1082a8c32a5cSJens Axboe * back into ->request_fn() could deadlock attempting to grab the 1083a8c32a5cSJens Axboe * queue lock again. 1084a8c32a5cSJens Axboe */ 10859a0e609eSMike Snitzer if (run_queue) { 1086bfebd1cdSMike Snitzer if (md->queue->mq_ops) 1087bfebd1cdSMike Snitzer blk_mq_run_hw_queues(md->queue, true); 1088bfebd1cdSMike Snitzer else if (!nr_requests_pending || 10899a0e609eSMike Snitzer (nr_requests_pending >= md->queue->nr_congestion_on)) 1090a8c32a5cSJens Axboe blk_run_queue_async(md->queue); 10919a0e609eSMike Snitzer } 1092cec47e3dSKiyoshi Ueda 1093cec47e3dSKiyoshi Ueda /* 1094cec47e3dSKiyoshi Ueda * dm_put() must be at the end of this function. See the comment above 1095cec47e3dSKiyoshi Ueda */ 1096cec47e3dSKiyoshi Ueda dm_put(md); 1097cec47e3dSKiyoshi Ueda } 1098cec47e3dSKiyoshi Ueda 1099e5d8de32SMike Snitzer static void free_rq_clone(struct request *clone) 1100a77e28c7SKiyoshi Ueda { 1101a77e28c7SKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 1102bfebd1cdSMike Snitzer struct mapped_device *md = tio->md; 1103a77e28c7SKiyoshi Ueda 1104*78d8e58aSMike Snitzer blk_rq_unprep_clone(clone); 1105*78d8e58aSMike Snitzer 1106aa6df8ddSMike Snitzer if (md->type == DM_TYPE_MQ_REQUEST_BASED) 1107aa6df8ddSMike Snitzer /* stacked on blk-mq queue(s) */ 1108e5863d9aSMike Snitzer tio->ti->type->release_clone_rq(clone); 110902233342SMike Snitzer else if (!md->queue->mq_ops) 111002233342SMike Snitzer /* request_fn queue stacked on request_fn queue(s) */ 1111bfebd1cdSMike Snitzer free_clone_request(md, clone); 1112aa6df8ddSMike Snitzer /* 1113aa6df8ddSMike Snitzer * NOTE: for the blk-mq queue stacked on request_fn queue(s) case: 1114aa6df8ddSMike Snitzer * no need to call free_clone_request() because we leverage blk-mq by 1115aa6df8ddSMike Snitzer * allocating the clone at the end of the blk-mq pdu (see: clone_rq) 1116aa6df8ddSMike Snitzer */ 1117bfebd1cdSMike Snitzer 1118bfebd1cdSMike Snitzer if (!md->queue->mq_ops) 1119a77e28c7SKiyoshi Ueda free_rq_tio(tio); 1120a77e28c7SKiyoshi Ueda } 1121a77e28c7SKiyoshi Ueda 1122980691e5SKiyoshi Ueda /* 1123980691e5SKiyoshi Ueda * Complete the clone and the original request. 1124466d89a6SKeith Busch * Must be called without clone's queue lock held, 1125466d89a6SKeith Busch * see end_clone_request() for more details. 1126980691e5SKiyoshi Ueda */ 1127980691e5SKiyoshi Ueda static void dm_end_request(struct request *clone, int error) 1128980691e5SKiyoshi Ueda { 1129980691e5SKiyoshi Ueda int rw = rq_data_dir(clone); 1130980691e5SKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 1131980691e5SKiyoshi Ueda struct mapped_device *md = tio->md; 1132980691e5SKiyoshi Ueda struct request *rq = tio->orig; 1133980691e5SKiyoshi Ueda 113429e4013dSTejun Heo if (rq->cmd_type == REQ_TYPE_BLOCK_PC) { 1135980691e5SKiyoshi Ueda rq->errors = clone->errors; 1136980691e5SKiyoshi Ueda rq->resid_len = clone->resid_len; 1137980691e5SKiyoshi Ueda 1138980691e5SKiyoshi Ueda if (rq->sense) 1139980691e5SKiyoshi Ueda /* 1140980691e5SKiyoshi Ueda * We are using the sense buffer of the original 1141980691e5SKiyoshi Ueda * request. 1142980691e5SKiyoshi Ueda * So setting the length of the sense data is enough. 1143980691e5SKiyoshi Ueda */ 1144980691e5SKiyoshi Ueda rq->sense_len = clone->sense_len; 1145980691e5SKiyoshi Ueda } 1146980691e5SKiyoshi Ueda 1147e5d8de32SMike Snitzer free_rq_clone(clone); 1148e262f347SMikulas Patocka rq_end_stats(md, rq); 1149bfebd1cdSMike Snitzer if (!rq->q->mq_ops) 1150980691e5SKiyoshi Ueda blk_end_request_all(rq, error); 1151bfebd1cdSMike Snitzer else 1152bfebd1cdSMike Snitzer blk_mq_end_request(rq, error); 115329e4013dSTejun Heo rq_completed(md, rw, true); 1154980691e5SKiyoshi Ueda } 1155980691e5SKiyoshi Ueda 1156cec47e3dSKiyoshi Ueda static void dm_unprep_request(struct request *rq) 1157cec47e3dSKiyoshi Ueda { 1158bfebd1cdSMike Snitzer struct dm_rq_target_io *tio = tio_from_request(rq); 1159466d89a6SKeith Busch struct request *clone = tio->clone; 1160cec47e3dSKiyoshi Ueda 1161bfebd1cdSMike Snitzer if (!rq->q->mq_ops) { 1162cec47e3dSKiyoshi Ueda rq->special = NULL; 1163cec47e3dSKiyoshi Ueda rq->cmd_flags &= ~REQ_DONTPREP; 1164bfebd1cdSMike Snitzer } 1165cec47e3dSKiyoshi Ueda 1166e5863d9aSMike Snitzer if (clone) 1167e5d8de32SMike Snitzer free_rq_clone(clone); 1168cec47e3dSKiyoshi Ueda } 1169cec47e3dSKiyoshi Ueda 1170cec47e3dSKiyoshi Ueda /* 1171cec47e3dSKiyoshi Ueda * Requeue the original request of a clone. 1172cec47e3dSKiyoshi Ueda */ 1173bfebd1cdSMike Snitzer static void old_requeue_request(struct request *rq) 1174cec47e3dSKiyoshi Ueda { 1175cec47e3dSKiyoshi Ueda struct request_queue *q = rq->q; 1176cec47e3dSKiyoshi Ueda unsigned long flags; 1177cec47e3dSKiyoshi Ueda 1178cec47e3dSKiyoshi Ueda spin_lock_irqsave(q->queue_lock, flags); 1179cec47e3dSKiyoshi Ueda blk_requeue_request(q, rq); 11804ae9944dSJunichi Nomura blk_run_queue_async(q); 1181cec47e3dSKiyoshi Ueda spin_unlock_irqrestore(q->queue_lock, flags); 1182bfebd1cdSMike Snitzer } 1183bfebd1cdSMike Snitzer 11842d76fff1SMike Snitzer static void dm_requeue_original_request(struct mapped_device *md, 1185bfebd1cdSMike Snitzer struct request *rq) 1186bfebd1cdSMike Snitzer { 1187bfebd1cdSMike Snitzer int rw = rq_data_dir(rq); 1188bfebd1cdSMike Snitzer 1189bfebd1cdSMike Snitzer dm_unprep_request(rq); 1190bfebd1cdSMike Snitzer 1191e262f347SMikulas Patocka rq_end_stats(md, rq); 1192bfebd1cdSMike Snitzer if (!rq->q->mq_ops) 1193bfebd1cdSMike Snitzer old_requeue_request(rq); 1194bfebd1cdSMike Snitzer else { 1195bfebd1cdSMike Snitzer blk_mq_requeue_request(rq); 1196bfebd1cdSMike Snitzer blk_mq_kick_requeue_list(rq->q); 1197bfebd1cdSMike Snitzer } 1198cec47e3dSKiyoshi Ueda 1199466d89a6SKeith Busch rq_completed(md, rw, false); 1200cec47e3dSKiyoshi Ueda } 1201466d89a6SKeith Busch 1202bfebd1cdSMike Snitzer static void old_stop_queue(struct request_queue *q) 1203cec47e3dSKiyoshi Ueda { 1204bfebd1cdSMike Snitzer unsigned long flags; 1205bfebd1cdSMike Snitzer 1206bfebd1cdSMike Snitzer if (blk_queue_stopped(q)) 1207bfebd1cdSMike Snitzer return; 1208bfebd1cdSMike Snitzer 1209bfebd1cdSMike Snitzer spin_lock_irqsave(q->queue_lock, flags); 1210cec47e3dSKiyoshi Ueda blk_stop_queue(q); 1211bfebd1cdSMike Snitzer spin_unlock_irqrestore(q->queue_lock, flags); 1212cec47e3dSKiyoshi Ueda } 1213cec47e3dSKiyoshi Ueda 1214cec47e3dSKiyoshi Ueda static void stop_queue(struct request_queue *q) 1215cec47e3dSKiyoshi Ueda { 1216bfebd1cdSMike Snitzer if (!q->mq_ops) 1217bfebd1cdSMike Snitzer old_stop_queue(q); 1218bfebd1cdSMike Snitzer else 1219bfebd1cdSMike Snitzer blk_mq_stop_hw_queues(q); 1220bfebd1cdSMike Snitzer } 1221bfebd1cdSMike Snitzer 1222bfebd1cdSMike Snitzer static void old_start_queue(struct request_queue *q) 1223bfebd1cdSMike Snitzer { 1224cec47e3dSKiyoshi Ueda unsigned long flags; 1225cec47e3dSKiyoshi Ueda 1226cec47e3dSKiyoshi Ueda spin_lock_irqsave(q->queue_lock, flags); 1227cec47e3dSKiyoshi Ueda if (blk_queue_stopped(q)) 1228cec47e3dSKiyoshi Ueda blk_start_queue(q); 1229bfebd1cdSMike Snitzer spin_unlock_irqrestore(q->queue_lock, flags); 1230cec47e3dSKiyoshi Ueda } 1231cec47e3dSKiyoshi Ueda 1232cec47e3dSKiyoshi Ueda static void start_queue(struct request_queue *q) 1233cec47e3dSKiyoshi Ueda { 1234bfebd1cdSMike Snitzer if (!q->mq_ops) 1235bfebd1cdSMike Snitzer old_start_queue(q); 1236bfebd1cdSMike Snitzer else 1237bfebd1cdSMike Snitzer blk_mq_start_stopped_hw_queues(q, true); 1238cec47e3dSKiyoshi Ueda } 1239cec47e3dSKiyoshi Ueda 124011a68244SKiyoshi Ueda static void dm_done(struct request *clone, int error, bool mapped) 124111a68244SKiyoshi Ueda { 124211a68244SKiyoshi Ueda int r = error; 124311a68244SKiyoshi Ueda struct dm_rq_target_io *tio = clone->end_io_data; 1244ba1cbad9SMike Snitzer dm_request_endio_fn rq_end_io = NULL; 1245ba1cbad9SMike Snitzer 1246ba1cbad9SMike Snitzer if (tio->ti) { 1247ba1cbad9SMike Snitzer rq_end_io = tio->ti->type->rq_end_io; 124811a68244SKiyoshi Ueda 124911a68244SKiyoshi Ueda if (mapped && rq_end_io) 125011a68244SKiyoshi Ueda r = rq_end_io(tio->ti, clone, error, &tio->info); 1251ba1cbad9SMike Snitzer } 125211a68244SKiyoshi Ueda 12537eee4ae2SMike Snitzer if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) && 12547eee4ae2SMike Snitzer !clone->q->limits.max_write_same_sectors)) 12557eee4ae2SMike Snitzer disable_write_same(tio->md); 12567eee4ae2SMike Snitzer 125711a68244SKiyoshi Ueda if (r <= 0) 125811a68244SKiyoshi Ueda /* The target wants to complete the I/O */ 125911a68244SKiyoshi Ueda dm_end_request(clone, r); 126011a68244SKiyoshi Ueda else if (r == DM_ENDIO_INCOMPLETE) 126111a68244SKiyoshi Ueda /* The target will handle the I/O */ 126211a68244SKiyoshi Ueda return; 126311a68244SKiyoshi Ueda else if (r == DM_ENDIO_REQUEUE) 126411a68244SKiyoshi Ueda /* The target wants to requeue the I/O */ 12652d76fff1SMike Snitzer dm_requeue_original_request(tio->md, tio->orig); 126611a68244SKiyoshi Ueda else { 126711a68244SKiyoshi Ueda DMWARN("unimplemented target endio return value: %d", r); 126811a68244SKiyoshi Ueda BUG(); 126911a68244SKiyoshi Ueda } 127011a68244SKiyoshi Ueda } 127111a68244SKiyoshi Ueda 1272cec47e3dSKiyoshi Ueda /* 1273cec47e3dSKiyoshi Ueda * Request completion handler for request-based dm 1274cec47e3dSKiyoshi Ueda */ 1275cec47e3dSKiyoshi Ueda static void dm_softirq_done(struct request *rq) 1276cec47e3dSKiyoshi Ueda { 127711a68244SKiyoshi Ueda bool mapped = true; 1278bfebd1cdSMike Snitzer struct dm_rq_target_io *tio = tio_from_request(rq); 1279466d89a6SKeith Busch struct request *clone = tio->clone; 1280bfebd1cdSMike Snitzer int rw; 1281cec47e3dSKiyoshi Ueda 1282e5863d9aSMike Snitzer if (!clone) { 1283e262f347SMikulas Patocka rq_end_stats(tio->md, rq); 1284bfebd1cdSMike Snitzer rw = rq_data_dir(rq); 1285bfebd1cdSMike Snitzer if (!rq->q->mq_ops) { 1286e5863d9aSMike Snitzer blk_end_request_all(rq, tio->error); 1287bfebd1cdSMike Snitzer rq_completed(tio->md, rw, false); 1288e5863d9aSMike Snitzer free_rq_tio(tio); 1289bfebd1cdSMike Snitzer } else { 1290bfebd1cdSMike Snitzer blk_mq_end_request(rq, tio->error); 1291bfebd1cdSMike Snitzer rq_completed(tio->md, rw, false); 1292bfebd1cdSMike Snitzer } 1293e5863d9aSMike Snitzer return; 1294e5863d9aSMike Snitzer } 1295cec47e3dSKiyoshi Ueda 129611a68244SKiyoshi Ueda if (rq->cmd_flags & REQ_FAILED) 129711a68244SKiyoshi Ueda mapped = false; 1298cec47e3dSKiyoshi Ueda 129911a68244SKiyoshi Ueda dm_done(clone, tio->error, mapped); 1300cec47e3dSKiyoshi Ueda } 1301cec47e3dSKiyoshi Ueda 1302cec47e3dSKiyoshi Ueda /* 1303cec47e3dSKiyoshi Ueda * Complete the clone and the original request with the error status 1304cec47e3dSKiyoshi Ueda * through softirq context. 1305cec47e3dSKiyoshi Ueda */ 1306466d89a6SKeith Busch static void dm_complete_request(struct request *rq, int error) 1307cec47e3dSKiyoshi Ueda { 1308bfebd1cdSMike Snitzer struct dm_rq_target_io *tio = tio_from_request(rq); 1309cec47e3dSKiyoshi Ueda 1310cec47e3dSKiyoshi Ueda tio->error = error; 1311cec47e3dSKiyoshi Ueda blk_complete_request(rq); 1312cec47e3dSKiyoshi Ueda } 1313cec47e3dSKiyoshi Ueda 1314cec47e3dSKiyoshi Ueda /* 1315cec47e3dSKiyoshi Ueda * Complete the not-mapped clone and the original request with the error status 1316cec47e3dSKiyoshi Ueda * through softirq context. 1317cec47e3dSKiyoshi Ueda * Target's rq_end_io() function isn't called. 1318e5863d9aSMike Snitzer * This may be used when the target's map_rq() or clone_and_map_rq() functions fail. 1319cec47e3dSKiyoshi Ueda */ 1320466d89a6SKeith Busch static void dm_kill_unmapped_request(struct request *rq, int error) 1321cec47e3dSKiyoshi Ueda { 1322cec47e3dSKiyoshi Ueda rq->cmd_flags |= REQ_FAILED; 1323466d89a6SKeith Busch dm_complete_request(rq, error); 1324cec47e3dSKiyoshi Ueda } 1325cec47e3dSKiyoshi Ueda 1326cec47e3dSKiyoshi Ueda /* 1327bfebd1cdSMike Snitzer * Called with the clone's queue lock held (for non-blk-mq) 1328cec47e3dSKiyoshi Ueda */ 1329cec47e3dSKiyoshi Ueda static void end_clone_request(struct request *clone, int error) 1330cec47e3dSKiyoshi Ueda { 1331466d89a6SKeith Busch struct dm_rq_target_io *tio = clone->end_io_data; 1332466d89a6SKeith Busch 1333e5863d9aSMike Snitzer if (!clone->q->mq_ops) { 1334cec47e3dSKiyoshi Ueda /* 1335cec47e3dSKiyoshi Ueda * For just cleaning up the information of the queue in which 1336cec47e3dSKiyoshi Ueda * the clone was dispatched. 1337e5863d9aSMike Snitzer * The clone is *NOT* freed actually here because it is alloced 1338e5863d9aSMike Snitzer * from dm own mempool (REQ_ALLOCED isn't set). 1339cec47e3dSKiyoshi Ueda */ 1340cec47e3dSKiyoshi Ueda __blk_put_request(clone->q, clone); 1341e5863d9aSMike Snitzer } 1342cec47e3dSKiyoshi Ueda 1343cec47e3dSKiyoshi Ueda /* 1344cec47e3dSKiyoshi Ueda * Actual request completion is done in a softirq context which doesn't 1345466d89a6SKeith Busch * hold the clone's queue lock. Otherwise, deadlock could occur because: 1346cec47e3dSKiyoshi Ueda * - another request may be submitted by the upper level driver 1347cec47e3dSKiyoshi Ueda * of the stacking during the completion 1348cec47e3dSKiyoshi Ueda * - the submission which requires queue lock may be done 1349466d89a6SKeith Busch * against this clone's queue 1350cec47e3dSKiyoshi Ueda */ 1351466d89a6SKeith Busch dm_complete_request(tio->orig, error); 1352cec47e3dSKiyoshi Ueda } 1353cec47e3dSKiyoshi Ueda 135456a67df7SMike Snitzer /* 135556a67df7SMike Snitzer * Return maximum size of I/O possible at the supplied sector up to the current 135656a67df7SMike Snitzer * target boundary. 135756a67df7SMike Snitzer */ 135856a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti) 13591da177e4SLinus Torvalds { 136056a67df7SMike Snitzer sector_t target_offset = dm_target_offset(ti, sector); 136156a67df7SMike Snitzer 136256a67df7SMike Snitzer return ti->len - target_offset; 136356a67df7SMike Snitzer } 136456a67df7SMike Snitzer 136556a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti) 136656a67df7SMike Snitzer { 136756a67df7SMike Snitzer sector_t len = max_io_len_target_boundary(sector, ti); 1368542f9038SMike Snitzer sector_t offset, max_len; 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds /* 13711da177e4SLinus Torvalds * Does the target need to split even further? 13721da177e4SLinus Torvalds */ 1373542f9038SMike Snitzer if (ti->max_io_len) { 1374542f9038SMike Snitzer offset = dm_target_offset(ti, sector); 1375542f9038SMike Snitzer if (unlikely(ti->max_io_len & (ti->max_io_len - 1))) 1376542f9038SMike Snitzer max_len = sector_div(offset, ti->max_io_len); 1377542f9038SMike Snitzer else 1378542f9038SMike Snitzer max_len = offset & (ti->max_io_len - 1); 1379542f9038SMike Snitzer max_len = ti->max_io_len - max_len; 1380542f9038SMike Snitzer 1381542f9038SMike Snitzer if (len > max_len) 1382542f9038SMike Snitzer len = max_len; 13831da177e4SLinus Torvalds } 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds return len; 13861da177e4SLinus Torvalds } 13871da177e4SLinus Torvalds 1388542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len) 1389542f9038SMike Snitzer { 1390542f9038SMike Snitzer if (len > UINT_MAX) { 1391542f9038SMike Snitzer DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)", 1392542f9038SMike Snitzer (unsigned long long)len, UINT_MAX); 1393542f9038SMike Snitzer ti->error = "Maximum size of target IO is too large"; 1394542f9038SMike Snitzer return -EINVAL; 1395542f9038SMike Snitzer } 1396542f9038SMike Snitzer 1397542f9038SMike Snitzer ti->max_io_len = (uint32_t) len; 1398542f9038SMike Snitzer 1399542f9038SMike Snitzer return 0; 1400542f9038SMike Snitzer } 1401542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len); 1402542f9038SMike Snitzer 14031dd40c3eSMikulas Patocka /* 14041dd40c3eSMikulas Patocka * A target may call dm_accept_partial_bio only from the map routine. It is 14051dd40c3eSMikulas Patocka * allowed for all bio types except REQ_FLUSH. 14061dd40c3eSMikulas Patocka * 14071dd40c3eSMikulas Patocka * dm_accept_partial_bio informs the dm that the target only wants to process 14081dd40c3eSMikulas Patocka * additional n_sectors sectors of the bio and the rest of the data should be 14091dd40c3eSMikulas Patocka * sent in a next bio. 14101dd40c3eSMikulas Patocka * 14111dd40c3eSMikulas Patocka * A diagram that explains the arithmetics: 14121dd40c3eSMikulas Patocka * +--------------------+---------------+-------+ 14131dd40c3eSMikulas Patocka * | 1 | 2 | 3 | 14141dd40c3eSMikulas Patocka * +--------------------+---------------+-------+ 14151dd40c3eSMikulas Patocka * 14161dd40c3eSMikulas Patocka * <-------------- *tio->len_ptr ---------------> 14171dd40c3eSMikulas Patocka * <------- bi_size -------> 14181dd40c3eSMikulas Patocka * <-- n_sectors --> 14191dd40c3eSMikulas Patocka * 14201dd40c3eSMikulas Patocka * Region 1 was already iterated over with bio_advance or similar function. 14211dd40c3eSMikulas Patocka * (it may be empty if the target doesn't use bio_advance) 14221dd40c3eSMikulas Patocka * Region 2 is the remaining bio size that the target wants to process. 14231dd40c3eSMikulas Patocka * (it may be empty if region 1 is non-empty, although there is no reason 14241dd40c3eSMikulas Patocka * to make it empty) 14251dd40c3eSMikulas Patocka * The target requires that region 3 is to be sent in the next bio. 14261dd40c3eSMikulas Patocka * 14271dd40c3eSMikulas Patocka * If the target wants to receive multiple copies of the bio (via num_*bios, etc), 14281dd40c3eSMikulas Patocka * the partially processed part (the sum of regions 1+2) must be the same for all 14291dd40c3eSMikulas Patocka * copies of the bio. 14301dd40c3eSMikulas Patocka */ 14311dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors) 14321dd40c3eSMikulas Patocka { 14331dd40c3eSMikulas Patocka struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); 14341dd40c3eSMikulas Patocka unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT; 14351dd40c3eSMikulas Patocka BUG_ON(bio->bi_rw & REQ_FLUSH); 14361dd40c3eSMikulas Patocka BUG_ON(bi_size > *tio->len_ptr); 14371dd40c3eSMikulas Patocka BUG_ON(n_sectors > bi_size); 14381dd40c3eSMikulas Patocka *tio->len_ptr -= bi_size - n_sectors; 14391dd40c3eSMikulas Patocka bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT; 14401dd40c3eSMikulas Patocka } 14411dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio); 14421dd40c3eSMikulas Patocka 1443bd2a49b8SAlasdair G Kergon static void __map_bio(struct dm_target_io *tio) 14441da177e4SLinus Torvalds { 14451da177e4SLinus Torvalds int r; 14462056a782SJens Axboe sector_t sector; 14479faf400fSStefan Bader struct mapped_device *md; 1448dba14160SMikulas Patocka struct bio *clone = &tio->clone; 1449bd2a49b8SAlasdair G Kergon struct dm_target *ti = tio->ti; 14501da177e4SLinus Torvalds 14511da177e4SLinus Torvalds clone->bi_end_io = clone_endio; 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds /* 14541da177e4SLinus Torvalds * Map the clone. If r == 0 we don't need to do 14551da177e4SLinus Torvalds * anything, the target has assumed ownership of 14561da177e4SLinus Torvalds * this io. 14571da177e4SLinus Torvalds */ 14581da177e4SLinus Torvalds atomic_inc(&tio->io->io_count); 14594f024f37SKent Overstreet sector = clone->bi_iter.bi_sector; 14607de3ee57SMikulas Patocka r = ti->type->map(ti, clone); 146145cbcd79SKiyoshi Ueda if (r == DM_MAPIO_REMAPPED) { 14621da177e4SLinus Torvalds /* the bio has been remapped so dispatch it */ 14632056a782SJens Axboe 1464d07335e5SMike Snitzer trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone, 146522a7c31aSAlan D. Brunelle tio->io->bio->bi_bdev->bd_dev, sector); 14662056a782SJens Axboe 14671da177e4SLinus Torvalds generic_make_request(clone); 14682e93ccc1SKiyoshi Ueda } else if (r < 0 || r == DM_MAPIO_REQUEUE) { 14692e93ccc1SKiyoshi Ueda /* error the io and bail out, or requeue it if needed */ 14709faf400fSStefan Bader md = tio->io->md; 14719faf400fSStefan Bader dec_pending(tio->io, r); 14729faf400fSStefan Bader free_tio(md, tio); 147345cbcd79SKiyoshi Ueda } else if (r) { 147445cbcd79SKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 147545cbcd79SKiyoshi Ueda BUG(); 14761da177e4SLinus Torvalds } 14771da177e4SLinus Torvalds } 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds struct clone_info { 14801da177e4SLinus Torvalds struct mapped_device *md; 14811da177e4SLinus Torvalds struct dm_table *map; 14821da177e4SLinus Torvalds struct bio *bio; 14831da177e4SLinus Torvalds struct dm_io *io; 14841da177e4SLinus Torvalds sector_t sector; 1485e0d6609aSMikulas Patocka unsigned sector_count; 14861da177e4SLinus Torvalds }; 14871da177e4SLinus Torvalds 1488e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len) 1489bd2a49b8SAlasdair G Kergon { 14904f024f37SKent Overstreet bio->bi_iter.bi_sector = sector; 14914f024f37SKent Overstreet bio->bi_iter.bi_size = to_bytes(len); 14921da177e4SLinus Torvalds } 14931da177e4SLinus Torvalds 14941da177e4SLinus Torvalds /* 14951da177e4SLinus Torvalds * Creates a bio that consists of range of complete bvecs. 14961da177e4SLinus Torvalds */ 1497dba14160SMikulas Patocka static void clone_bio(struct dm_target_io *tio, struct bio *bio, 14981c3b13e6SKent Overstreet sector_t sector, unsigned len) 14991da177e4SLinus Torvalds { 1500dba14160SMikulas Patocka struct bio *clone = &tio->clone; 15011da177e4SLinus Torvalds 15021c3b13e6SKent Overstreet __bio_clone_fast(clone, bio); 15039c47008dSMartin K. Petersen 15041c3b13e6SKent Overstreet if (bio_integrity(bio)) 15051c3b13e6SKent Overstreet bio_integrity_clone(clone, bio, GFP_NOIO); 15061c3b13e6SKent Overstreet 15071c3b13e6SKent Overstreet bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector)); 15081c3b13e6SKent Overstreet clone->bi_iter.bi_size = to_bytes(len); 15091c3b13e6SKent Overstreet 15101c3b13e6SKent Overstreet if (bio_integrity(bio)) 15111c3b13e6SKent Overstreet bio_integrity_trim(clone, 0, len); 15121da177e4SLinus Torvalds } 15131da177e4SLinus Torvalds 15149015df24SAlasdair G Kergon static struct dm_target_io *alloc_tio(struct clone_info *ci, 151599778273SJunichi Nomura struct dm_target *ti, 151655a62eefSAlasdair G Kergon unsigned target_bio_nr) 1517f9ab94ceSMikulas Patocka { 1518dba14160SMikulas Patocka struct dm_target_io *tio; 1519dba14160SMikulas Patocka struct bio *clone; 1520dba14160SMikulas Patocka 152199778273SJunichi Nomura clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs); 1522dba14160SMikulas Patocka tio = container_of(clone, struct dm_target_io, clone); 1523f9ab94ceSMikulas Patocka 1524f9ab94ceSMikulas Patocka tio->io = ci->io; 1525f9ab94ceSMikulas Patocka tio->ti = ti; 152655a62eefSAlasdair G Kergon tio->target_bio_nr = target_bio_nr; 15279015df24SAlasdair G Kergon 15289015df24SAlasdair G Kergon return tio; 15299015df24SAlasdair G Kergon } 15309015df24SAlasdair G Kergon 153114fe594dSAlasdair G Kergon static void __clone_and_map_simple_bio(struct clone_info *ci, 153214fe594dSAlasdair G Kergon struct dm_target *ti, 15331dd40c3eSMikulas Patocka unsigned target_bio_nr, unsigned *len) 15349015df24SAlasdair G Kergon { 153599778273SJunichi Nomura struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr); 1536dba14160SMikulas Patocka struct bio *clone = &tio->clone; 15379015df24SAlasdair G Kergon 15381dd40c3eSMikulas Patocka tio->len_ptr = len; 15391dd40c3eSMikulas Patocka 15401c3b13e6SKent Overstreet __bio_clone_fast(clone, ci->bio); 1541bd2a49b8SAlasdair G Kergon if (len) 15421dd40c3eSMikulas Patocka bio_setup_sector(clone, ci->sector, *len); 1543f9ab94ceSMikulas Patocka 1544bd2a49b8SAlasdair G Kergon __map_bio(tio); 1545f9ab94ceSMikulas Patocka } 1546f9ab94ceSMikulas Patocka 154714fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti, 15481dd40c3eSMikulas Patocka unsigned num_bios, unsigned *len) 154906a426ceSMike Snitzer { 155055a62eefSAlasdair G Kergon unsigned target_bio_nr; 155106a426ceSMike Snitzer 155255a62eefSAlasdair G Kergon for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++) 155314fe594dSAlasdair G Kergon __clone_and_map_simple_bio(ci, ti, target_bio_nr, len); 155406a426ceSMike Snitzer } 155506a426ceSMike Snitzer 155614fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci) 1557f9ab94ceSMikulas Patocka { 155806a426ceSMike Snitzer unsigned target_nr = 0; 1559f9ab94ceSMikulas Patocka struct dm_target *ti; 1560f9ab94ceSMikulas Patocka 1561b372d360SMike Snitzer BUG_ON(bio_has_data(ci->bio)); 1562f9ab94ceSMikulas Patocka while ((ti = dm_table_get_target(ci->map, target_nr++))) 15631dd40c3eSMikulas Patocka __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL); 1564f9ab94ceSMikulas Patocka 1565f9ab94ceSMikulas Patocka return 0; 1566f9ab94ceSMikulas Patocka } 1567f9ab94ceSMikulas Patocka 1568e4c93811SAlasdair G Kergon static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti, 15691dd40c3eSMikulas Patocka sector_t sector, unsigned *len) 15705ae89a87SMike Snitzer { 1571dba14160SMikulas Patocka struct bio *bio = ci->bio; 15725ae89a87SMike Snitzer struct dm_target_io *tio; 1573b0d8ed4dSAlasdair G Kergon unsigned target_bio_nr; 1574b0d8ed4dSAlasdair G Kergon unsigned num_target_bios = 1; 15755ae89a87SMike Snitzer 1576b0d8ed4dSAlasdair G Kergon /* 1577b0d8ed4dSAlasdair G Kergon * Does the target want to receive duplicate copies of the bio? 1578b0d8ed4dSAlasdair G Kergon */ 1579b0d8ed4dSAlasdair G Kergon if (bio_data_dir(bio) == WRITE && ti->num_write_bios) 1580b0d8ed4dSAlasdair G Kergon num_target_bios = ti->num_write_bios(ti, bio); 1581e4c93811SAlasdair G Kergon 1582b0d8ed4dSAlasdair G Kergon for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) { 158399778273SJunichi Nomura tio = alloc_tio(ci, ti, target_bio_nr); 15841dd40c3eSMikulas Patocka tio->len_ptr = len; 15851dd40c3eSMikulas Patocka clone_bio(tio, bio, sector, *len); 1586bd2a49b8SAlasdair G Kergon __map_bio(tio); 15875ae89a87SMike Snitzer } 1588b0d8ed4dSAlasdair G Kergon } 15895ae89a87SMike Snitzer 159055a62eefSAlasdair G Kergon typedef unsigned (*get_num_bios_fn)(struct dm_target *ti); 159123508a96SMike Snitzer 159255a62eefSAlasdair G Kergon static unsigned get_num_discard_bios(struct dm_target *ti) 159323508a96SMike Snitzer { 159455a62eefSAlasdair G Kergon return ti->num_discard_bios; 159523508a96SMike Snitzer } 159623508a96SMike Snitzer 159755a62eefSAlasdair G Kergon static unsigned get_num_write_same_bios(struct dm_target *ti) 159823508a96SMike Snitzer { 159955a62eefSAlasdair G Kergon return ti->num_write_same_bios; 160023508a96SMike Snitzer } 160123508a96SMike Snitzer 160223508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti); 160323508a96SMike Snitzer 160423508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti) 160523508a96SMike Snitzer { 160655a62eefSAlasdair G Kergon return ti->split_discard_bios; 160723508a96SMike Snitzer } 160823508a96SMike Snitzer 160914fe594dSAlasdair G Kergon static int __send_changing_extent_only(struct clone_info *ci, 161055a62eefSAlasdair G Kergon get_num_bios_fn get_num_bios, 161123508a96SMike Snitzer is_split_required_fn is_split_required) 16125ae89a87SMike Snitzer { 16135ae89a87SMike Snitzer struct dm_target *ti; 1614e0d6609aSMikulas Patocka unsigned len; 161555a62eefSAlasdair G Kergon unsigned num_bios; 16165ae89a87SMike Snitzer 1617a79245b3SMike Snitzer do { 16185ae89a87SMike Snitzer ti = dm_table_find_target(ci->map, ci->sector); 16195ae89a87SMike Snitzer if (!dm_target_is_valid(ti)) 16205ae89a87SMike Snitzer return -EIO; 16215ae89a87SMike Snitzer 16225ae89a87SMike Snitzer /* 162323508a96SMike Snitzer * Even though the device advertised support for this type of 162423508a96SMike Snitzer * request, that does not mean every target supports it, and 1625936688d7SMike Snitzer * reconfiguration might also have changed that since the 16265ae89a87SMike Snitzer * check was performed. 16275ae89a87SMike Snitzer */ 162855a62eefSAlasdair G Kergon num_bios = get_num_bios ? get_num_bios(ti) : 0; 162955a62eefSAlasdair G Kergon if (!num_bios) 16305ae89a87SMike Snitzer return -EOPNOTSUPP; 16315ae89a87SMike Snitzer 163223508a96SMike Snitzer if (is_split_required && !is_split_required(ti)) 1633e0d6609aSMikulas Patocka len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti)); 16347acf0277SMikulas Patocka else 1635e0d6609aSMikulas Patocka len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti)); 16365ae89a87SMike Snitzer 16371dd40c3eSMikulas Patocka __send_duplicate_bios(ci, ti, num_bios, &len); 16385ae89a87SMike Snitzer 1639a79245b3SMike Snitzer ci->sector += len; 1640a79245b3SMike Snitzer } while (ci->sector_count -= len); 16415ae89a87SMike Snitzer 16425ae89a87SMike Snitzer return 0; 16435ae89a87SMike Snitzer } 16445ae89a87SMike Snitzer 164514fe594dSAlasdair G Kergon static int __send_discard(struct clone_info *ci) 164623508a96SMike Snitzer { 164714fe594dSAlasdair G Kergon return __send_changing_extent_only(ci, get_num_discard_bios, 164823508a96SMike Snitzer is_split_required_for_discard); 164923508a96SMike Snitzer } 165023508a96SMike Snitzer 165114fe594dSAlasdair G Kergon static int __send_write_same(struct clone_info *ci) 165223508a96SMike Snitzer { 165314fe594dSAlasdair G Kergon return __send_changing_extent_only(ci, get_num_write_same_bios, NULL); 165423508a96SMike Snitzer } 165523508a96SMike Snitzer 1656e4c93811SAlasdair G Kergon /* 1657e4c93811SAlasdair G Kergon * Select the correct strategy for processing a non-flush bio. 1658e4c93811SAlasdair G Kergon */ 1659e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci) 1660e4c93811SAlasdair G Kergon { 1661e4c93811SAlasdair G Kergon struct bio *bio = ci->bio; 1662e4c93811SAlasdair G Kergon struct dm_target *ti; 16631c3b13e6SKent Overstreet unsigned len; 1664e4c93811SAlasdair G Kergon 1665e4c93811SAlasdair G Kergon if (unlikely(bio->bi_rw & REQ_DISCARD)) 1666e4c93811SAlasdair G Kergon return __send_discard(ci); 1667e4c93811SAlasdair G Kergon else if (unlikely(bio->bi_rw & REQ_WRITE_SAME)) 1668e4c93811SAlasdair G Kergon return __send_write_same(ci); 1669e4c93811SAlasdair G Kergon 1670e4c93811SAlasdair G Kergon ti = dm_table_find_target(ci->map, ci->sector); 1671e4c93811SAlasdair G Kergon if (!dm_target_is_valid(ti)) 1672e4c93811SAlasdair G Kergon return -EIO; 1673e4c93811SAlasdair G Kergon 16741c3b13e6SKent Overstreet len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count); 1675e4c93811SAlasdair G Kergon 16761dd40c3eSMikulas Patocka __clone_and_map_data_bio(ci, ti, ci->sector, &len); 1677e4c93811SAlasdair G Kergon 1678e4c93811SAlasdair G Kergon ci->sector += len; 1679e4c93811SAlasdair G Kergon ci->sector_count -= len; 1680e4c93811SAlasdair G Kergon 1681e4c93811SAlasdair G Kergon return 0; 1682e4c93811SAlasdair G Kergon } 1683e4c93811SAlasdair G Kergon 1684e4c93811SAlasdair G Kergon /* 168514fe594dSAlasdair G Kergon * Entry point to split a bio into clones and submit them to the targets. 16861da177e4SLinus Torvalds */ 168783d5e5b0SMikulas Patocka static void __split_and_process_bio(struct mapped_device *md, 168883d5e5b0SMikulas Patocka struct dm_table *map, struct bio *bio) 16891da177e4SLinus Torvalds { 16901da177e4SLinus Torvalds struct clone_info ci; 1691512875bdSJun'ichi Nomura int error = 0; 16921da177e4SLinus Torvalds 169383d5e5b0SMikulas Patocka if (unlikely(!map)) { 1694f0b9a450SMikulas Patocka bio_io_error(bio); 1695f0b9a450SMikulas Patocka return; 1696f0b9a450SMikulas Patocka } 1697692d0eb9SMikulas Patocka 169883d5e5b0SMikulas Patocka ci.map = map; 16991da177e4SLinus Torvalds ci.md = md; 17001da177e4SLinus Torvalds ci.io = alloc_io(md); 17011da177e4SLinus Torvalds ci.io->error = 0; 17021da177e4SLinus Torvalds atomic_set(&ci.io->io_count, 1); 17031da177e4SLinus Torvalds ci.io->bio = bio; 17041da177e4SLinus Torvalds ci.io->md = md; 1705f88fb981SKiyoshi Ueda spin_lock_init(&ci.io->endio_lock); 17064f024f37SKent Overstreet ci.sector = bio->bi_iter.bi_sector; 17071da177e4SLinus Torvalds 17083eaf840eSJun'ichi "Nick" Nomura start_io_acct(ci.io); 1709bd2a49b8SAlasdair G Kergon 1710b372d360SMike Snitzer if (bio->bi_rw & REQ_FLUSH) { 1711b372d360SMike Snitzer ci.bio = &ci.md->flush_bio; 1712b372d360SMike Snitzer ci.sector_count = 0; 171314fe594dSAlasdair G Kergon error = __send_empty_flush(&ci); 1714b372d360SMike Snitzer /* dec_pending submits any data associated with flush */ 1715b372d360SMike Snitzer } else { 17166a8736d1STejun Heo ci.bio = bio; 1717f6fccb12SMilan Broz ci.sector_count = bio_sectors(bio); 1718512875bdSJun'ichi Nomura while (ci.sector_count && !error) 171914fe594dSAlasdair G Kergon error = __split_and_process_non_flush(&ci); 1720d87f4c14STejun Heo } 17211da177e4SLinus Torvalds 17221da177e4SLinus Torvalds /* drop the extra reference count */ 1723512875bdSJun'ichi Nomura dec_pending(ci.io, error); 17249e4e5f87SMilan Broz } 17259e4e5f87SMilan Broz /*----------------------------------------------------------------- 17261da177e4SLinus Torvalds * CRUD END 17271da177e4SLinus Torvalds *---------------------------------------------------------------*/ 17281da177e4SLinus Torvalds 17291da177e4SLinus Torvalds static int dm_merge_bvec(struct request_queue *q, 17301da177e4SLinus Torvalds struct bvec_merge_data *bvm, 1731f6fccb12SMilan Broz struct bio_vec *biovec) 1732f6fccb12SMilan Broz { 1733f6fccb12SMilan Broz struct mapped_device *md = q->queuedata; 173483d5e5b0SMikulas Patocka struct dm_table *map = dm_get_live_table_fast(md); 1735f6fccb12SMilan Broz struct dm_target *ti; 17361c220c69SJoe Thornber sector_t max_sectors, max_size = 0; 1737f6fccb12SMilan Broz 1738f6fccb12SMilan Broz if (unlikely(!map)) 1739f6fccb12SMilan Broz goto out; 1740f6fccb12SMilan Broz 1741f6fccb12SMilan Broz ti = dm_table_find_target(map, bvm->bi_sector); 1742f6fccb12SMilan Broz if (!dm_target_is_valid(ti)) 174383d5e5b0SMikulas Patocka goto out; 1744f6fccb12SMilan Broz 1745f6fccb12SMilan Broz /* 1746f6fccb12SMilan Broz * Find maximum amount of I/O that won't need splitting 1747f6fccb12SMilan Broz */ 174856a67df7SMike Snitzer max_sectors = min(max_io_len(bvm->bi_sector, ti), 1749148e51baSMike Snitzer (sector_t) queue_max_sectors(q)); 1750f6fccb12SMilan Broz max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size; 17511c220c69SJoe Thornber 17521c220c69SJoe Thornber /* 17531c220c69SJoe Thornber * FIXME: this stop-gap fix _must_ be cleaned up (by passing a sector_t 17541c220c69SJoe Thornber * to the targets' merge function since it holds sectors not bytes). 17551c220c69SJoe Thornber * Just doing this as an interim fix for stable@ because the more 17561c220c69SJoe Thornber * comprehensive cleanup of switching to sector_t will impact every 17571c220c69SJoe Thornber * DM target that implements a ->merge hook. 17581c220c69SJoe Thornber */ 17591c220c69SJoe Thornber if (max_size > INT_MAX) 17601c220c69SJoe Thornber max_size = INT_MAX; 1761f6fccb12SMilan Broz 1762f6fccb12SMilan Broz /* 1763f6fccb12SMilan Broz * merge_bvec_fn() returns number of bytes 1764f6fccb12SMilan Broz * it can accept at this offset 1765f6fccb12SMilan Broz * max is precomputed maximal io size 1766f6fccb12SMilan Broz */ 1767f6fccb12SMilan Broz if (max_size && ti->type->merge) 17681c220c69SJoe Thornber max_size = ti->type->merge(ti, bvm, biovec, (int) max_size); 17698cbeb67aSMikulas Patocka /* 17708cbeb67aSMikulas Patocka * If the target doesn't support merge method and some of the devices 1771148e51baSMike Snitzer * provided their merge_bvec method (we know this by looking for the 1772148e51baSMike Snitzer * max_hw_sectors that dm_set_device_limits may set), then we can't 1773148e51baSMike Snitzer * allow bios with multiple vector entries. So always set max_size 1774148e51baSMike Snitzer * to 0, and the code below allows just one page. 17758cbeb67aSMikulas Patocka */ 17768cbeb67aSMikulas Patocka else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9) 17778cbeb67aSMikulas Patocka max_size = 0; 1778f6fccb12SMilan Broz 17795037108aSMikulas Patocka out: 178083d5e5b0SMikulas Patocka dm_put_live_table_fast(md); 1781f6fccb12SMilan Broz /* 1782f6fccb12SMilan Broz * Always allow an entire first page 1783f6fccb12SMilan Broz */ 1784f6fccb12SMilan Broz if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT)) 1785f6fccb12SMilan Broz max_size = biovec->bv_len; 1786f6fccb12SMilan Broz 1787f6fccb12SMilan Broz return max_size; 1788f6fccb12SMilan Broz } 1789f6fccb12SMilan Broz 17901da177e4SLinus Torvalds /* 17911da177e4SLinus Torvalds * The request function that just remaps the bio built up by 17921da177e4SLinus Torvalds * dm_merge_bvec. 17931da177e4SLinus Torvalds */ 1794ff36ab34SMike Snitzer static void dm_make_request(struct request_queue *q, struct bio *bio) 17951da177e4SLinus Torvalds { 179612f03a49SKevin Corry int rw = bio_data_dir(bio); 17971da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 179883d5e5b0SMikulas Patocka int srcu_idx; 179983d5e5b0SMikulas Patocka struct dm_table *map; 18001da177e4SLinus Torvalds 180183d5e5b0SMikulas Patocka map = dm_get_live_table(md, &srcu_idx); 18021da177e4SLinus Torvalds 180318c0b223SGu Zheng generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0); 180412f03a49SKevin Corry 18056a8736d1STejun Heo /* if we're suspended, we have to queue this io for later */ 18066a8736d1STejun Heo if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) { 180783d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 18081da177e4SLinus Torvalds 18096a8736d1STejun Heo if (bio_rw(bio) != READA) 181092c63902SMikulas Patocka queue_io(md, bio); 18116a8736d1STejun Heo else 18126a8736d1STejun Heo bio_io_error(bio); 18135a7bbad2SChristoph Hellwig return; 18141da177e4SLinus Torvalds } 18151da177e4SLinus Torvalds 181683d5e5b0SMikulas Patocka __split_and_process_bio(md, map, bio); 181783d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 18185a7bbad2SChristoph Hellwig return; 1819cec47e3dSKiyoshi Ueda } 1820cec47e3dSKiyoshi Ueda 1821fd2ed4d2SMikulas Patocka int dm_request_based(struct mapped_device *md) 1822cec47e3dSKiyoshi Ueda { 1823cec47e3dSKiyoshi Ueda return blk_queue_stackable(md->queue); 1824cec47e3dSKiyoshi Ueda } 1825cec47e3dSKiyoshi Ueda 1826466d89a6SKeith Busch static void dm_dispatch_clone_request(struct request *clone, struct request *rq) 1827cec47e3dSKiyoshi Ueda { 1828cec47e3dSKiyoshi Ueda int r; 1829cec47e3dSKiyoshi Ueda 1830466d89a6SKeith Busch if (blk_queue_io_stat(clone->q)) 1831466d89a6SKeith Busch clone->cmd_flags |= REQ_IO_STAT; 1832cec47e3dSKiyoshi Ueda 1833466d89a6SKeith Busch clone->start_time = jiffies; 1834466d89a6SKeith Busch r = blk_insert_cloned_request(clone->q, clone); 1835cec47e3dSKiyoshi Ueda if (r) 1836466d89a6SKeith Busch /* must complete clone in terms of original request */ 1837cec47e3dSKiyoshi Ueda dm_complete_request(rq, r); 1838cec47e3dSKiyoshi Ueda } 1839cec47e3dSKiyoshi Ueda 1840*78d8e58aSMike Snitzer static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig, 1841*78d8e58aSMike Snitzer void *data) 1842cec47e3dSKiyoshi Ueda { 1843*78d8e58aSMike Snitzer struct dm_rq_target_io *tio = data; 1844*78d8e58aSMike Snitzer struct dm_rq_clone_bio_info *info = 1845*78d8e58aSMike Snitzer container_of(bio, struct dm_rq_clone_bio_info, clone); 1846*78d8e58aSMike Snitzer 1847*78d8e58aSMike Snitzer info->orig = bio_orig; 1848*78d8e58aSMike Snitzer info->tio = tio; 1849*78d8e58aSMike Snitzer bio->bi_end_io = end_clone_bio; 1850*78d8e58aSMike Snitzer 1851*78d8e58aSMike Snitzer return 0; 1852*78d8e58aSMike Snitzer } 1853*78d8e58aSMike Snitzer 1854*78d8e58aSMike Snitzer static int setup_clone(struct request *clone, struct request *rq, 1855*78d8e58aSMike Snitzer struct dm_rq_target_io *tio, gfp_t gfp_mask) 1856*78d8e58aSMike Snitzer { 1857*78d8e58aSMike Snitzer int r; 1858*78d8e58aSMike Snitzer 1859*78d8e58aSMike Snitzer r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask, 1860*78d8e58aSMike Snitzer dm_rq_bio_constructor, tio); 1861*78d8e58aSMike Snitzer if (r) 1862*78d8e58aSMike Snitzer return r; 1863*78d8e58aSMike Snitzer 1864*78d8e58aSMike Snitzer clone->cmd = rq->cmd; 1865*78d8e58aSMike Snitzer clone->cmd_len = rq->cmd_len; 1866*78d8e58aSMike Snitzer clone->sense = rq->sense; 1867cec47e3dSKiyoshi Ueda clone->end_io = end_clone_request; 1868cec47e3dSKiyoshi Ueda clone->end_io_data = tio; 1869*78d8e58aSMike Snitzer 18701ae49ea2SMike Snitzer tio->clone = clone; 1871*78d8e58aSMike Snitzer 1872*78d8e58aSMike Snitzer return 0; 1873cec47e3dSKiyoshi Ueda } 1874cec47e3dSKiyoshi Ueda 18756facdaffSKiyoshi Ueda static struct request *clone_rq(struct request *rq, struct mapped_device *md, 18761ae49ea2SMike Snitzer struct dm_rq_target_io *tio, gfp_t gfp_mask) 18776facdaffSKiyoshi Ueda { 187802233342SMike Snitzer /* 187902233342SMike Snitzer * Do not allocate a clone if tio->clone was already set 188002233342SMike Snitzer * (see: dm_mq_queue_rq). 188102233342SMike Snitzer */ 188202233342SMike Snitzer bool alloc_clone = !tio->clone; 188302233342SMike Snitzer struct request *clone; 18841ae49ea2SMike Snitzer 188502233342SMike Snitzer if (alloc_clone) { 188602233342SMike Snitzer clone = alloc_clone_request(md, gfp_mask); 18871ae49ea2SMike Snitzer if (!clone) 18881ae49ea2SMike Snitzer return NULL; 188902233342SMike Snitzer } else 189002233342SMike Snitzer clone = tio->clone; 18911ae49ea2SMike Snitzer 18921ae49ea2SMike Snitzer blk_rq_init(NULL, clone); 1893*78d8e58aSMike Snitzer if (setup_clone(clone, rq, tio, gfp_mask)) { 1894*78d8e58aSMike Snitzer /* -ENOMEM */ 1895*78d8e58aSMike Snitzer if (alloc_clone) 1896*78d8e58aSMike Snitzer free_clone_request(md, clone); 1897*78d8e58aSMike Snitzer return NULL; 1898*78d8e58aSMike Snitzer } 18991ae49ea2SMike Snitzer 19001ae49ea2SMike Snitzer return clone; 19011ae49ea2SMike Snitzer } 19021ae49ea2SMike Snitzer 19032eb6e1e3SKeith Busch static void map_tio_request(struct kthread_work *work); 19042eb6e1e3SKeith Busch 1905bfebd1cdSMike Snitzer static void init_tio(struct dm_rq_target_io *tio, struct request *rq, 1906bfebd1cdSMike Snitzer struct mapped_device *md) 1907bfebd1cdSMike Snitzer { 1908bfebd1cdSMike Snitzer tio->md = md; 1909bfebd1cdSMike Snitzer tio->ti = NULL; 1910bfebd1cdSMike Snitzer tio->clone = NULL; 1911bfebd1cdSMike Snitzer tio->orig = rq; 1912bfebd1cdSMike Snitzer tio->error = 0; 1913bfebd1cdSMike Snitzer memset(&tio->info, 0, sizeof(tio->info)); 191402233342SMike Snitzer if (md->kworker_task) 1915bfebd1cdSMike Snitzer init_kthread_work(&tio->work, map_tio_request); 1916bfebd1cdSMike Snitzer } 1917bfebd1cdSMike Snitzer 1918466d89a6SKeith Busch static struct dm_rq_target_io *prep_tio(struct request *rq, 1919466d89a6SKeith Busch struct mapped_device *md, gfp_t gfp_mask) 19206facdaffSKiyoshi Ueda { 19216facdaffSKiyoshi Ueda struct dm_rq_target_io *tio; 1922e5863d9aSMike Snitzer int srcu_idx; 1923e5863d9aSMike Snitzer struct dm_table *table; 19246facdaffSKiyoshi Ueda 19256facdaffSKiyoshi Ueda tio = alloc_rq_tio(md, gfp_mask); 19266facdaffSKiyoshi Ueda if (!tio) 19276facdaffSKiyoshi Ueda return NULL; 19286facdaffSKiyoshi Ueda 1929bfebd1cdSMike Snitzer init_tio(tio, rq, md); 19306facdaffSKiyoshi Ueda 1931e5863d9aSMike Snitzer table = dm_get_live_table(md, &srcu_idx); 1932e5863d9aSMike Snitzer if (!dm_table_mq_request_based(table)) { 1933466d89a6SKeith Busch if (!clone_rq(rq, md, tio, gfp_mask)) { 1934e5863d9aSMike Snitzer dm_put_live_table(md, srcu_idx); 19356facdaffSKiyoshi Ueda free_rq_tio(tio); 19366facdaffSKiyoshi Ueda return NULL; 19376facdaffSKiyoshi Ueda } 1938e5863d9aSMike Snitzer } 1939e5863d9aSMike Snitzer dm_put_live_table(md, srcu_idx); 19406facdaffSKiyoshi Ueda 1941466d89a6SKeith Busch return tio; 19426facdaffSKiyoshi Ueda } 19436facdaffSKiyoshi Ueda 1944cec47e3dSKiyoshi Ueda /* 1945cec47e3dSKiyoshi Ueda * Called with the queue lock held. 1946cec47e3dSKiyoshi Ueda */ 1947cec47e3dSKiyoshi Ueda static int dm_prep_fn(struct request_queue *q, struct request *rq) 1948cec47e3dSKiyoshi Ueda { 1949cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 1950466d89a6SKeith Busch struct dm_rq_target_io *tio; 1951cec47e3dSKiyoshi Ueda 1952cec47e3dSKiyoshi Ueda if (unlikely(rq->special)) { 1953cec47e3dSKiyoshi Ueda DMWARN("Already has something in rq->special."); 1954cec47e3dSKiyoshi Ueda return BLKPREP_KILL; 1955cec47e3dSKiyoshi Ueda } 1956cec47e3dSKiyoshi Ueda 1957466d89a6SKeith Busch tio = prep_tio(rq, md, GFP_ATOMIC); 1958466d89a6SKeith Busch if (!tio) 1959cec47e3dSKiyoshi Ueda return BLKPREP_DEFER; 1960cec47e3dSKiyoshi Ueda 1961466d89a6SKeith Busch rq->special = tio; 1962cec47e3dSKiyoshi Ueda rq->cmd_flags |= REQ_DONTPREP; 1963cec47e3dSKiyoshi Ueda 1964cec47e3dSKiyoshi Ueda return BLKPREP_OK; 1965cec47e3dSKiyoshi Ueda } 1966cec47e3dSKiyoshi Ueda 19679eef87daSKiyoshi Ueda /* 19689eef87daSKiyoshi Ueda * Returns: 1969e5863d9aSMike Snitzer * 0 : the request has been processed 1970e5863d9aSMike Snitzer * DM_MAPIO_REQUEUE : the original request needs to be requeued 1971e5863d9aSMike Snitzer * < 0 : the request was completed due to failure 19729eef87daSKiyoshi Ueda */ 1973bfebd1cdSMike Snitzer static int map_request(struct dm_rq_target_io *tio, struct request *rq, 1974cec47e3dSKiyoshi Ueda struct mapped_device *md) 1975cec47e3dSKiyoshi Ueda { 1976e5863d9aSMike Snitzer int r; 1977bfebd1cdSMike Snitzer struct dm_target *ti = tio->ti; 1978e5863d9aSMike Snitzer struct request *clone = NULL; 1979cec47e3dSKiyoshi Ueda 1980e5863d9aSMike Snitzer if (tio->clone) { 1981e5863d9aSMike Snitzer clone = tio->clone; 1982cec47e3dSKiyoshi Ueda r = ti->type->map_rq(ti, clone, &tio->info); 1983e5863d9aSMike Snitzer } else { 1984e5863d9aSMike Snitzer r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone); 1985e5863d9aSMike Snitzer if (r < 0) { 1986e5863d9aSMike Snitzer /* The target wants to complete the I/O */ 1987e5863d9aSMike Snitzer dm_kill_unmapped_request(rq, r); 1988e5863d9aSMike Snitzer return r; 1989e5863d9aSMike Snitzer } 19903a140755SJunichi Nomura if (r != DM_MAPIO_REMAPPED) 19913a140755SJunichi Nomura return r; 1992*78d8e58aSMike Snitzer if (setup_clone(clone, rq, tio, GFP_ATOMIC)) { 1993*78d8e58aSMike Snitzer /* -ENOMEM */ 1994*78d8e58aSMike Snitzer ti->type->release_clone_rq(clone); 1995*78d8e58aSMike Snitzer return DM_MAPIO_REQUEUE; 1996*78d8e58aSMike Snitzer } 1997e5863d9aSMike Snitzer } 1998e5863d9aSMike Snitzer 1999cec47e3dSKiyoshi Ueda switch (r) { 2000cec47e3dSKiyoshi Ueda case DM_MAPIO_SUBMITTED: 2001cec47e3dSKiyoshi Ueda /* The target has taken the I/O to submit by itself later */ 2002cec47e3dSKiyoshi Ueda break; 2003cec47e3dSKiyoshi Ueda case DM_MAPIO_REMAPPED: 2004cec47e3dSKiyoshi Ueda /* The target has remapped the I/O so dispatch it */ 20056db4ccd6SJun'ichi Nomura trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)), 2006466d89a6SKeith Busch blk_rq_pos(rq)); 2007466d89a6SKeith Busch dm_dispatch_clone_request(clone, rq); 2008cec47e3dSKiyoshi Ueda break; 2009cec47e3dSKiyoshi Ueda case DM_MAPIO_REQUEUE: 2010cec47e3dSKiyoshi Ueda /* The target wants to requeue the I/O */ 20112d76fff1SMike Snitzer dm_requeue_original_request(md, tio->orig); 2012cec47e3dSKiyoshi Ueda break; 2013cec47e3dSKiyoshi Ueda default: 2014cec47e3dSKiyoshi Ueda if (r > 0) { 2015cec47e3dSKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 2016cec47e3dSKiyoshi Ueda BUG(); 2017cec47e3dSKiyoshi Ueda } 2018cec47e3dSKiyoshi Ueda 2019cec47e3dSKiyoshi Ueda /* The target wants to complete the I/O */ 2020466d89a6SKeith Busch dm_kill_unmapped_request(rq, r); 2021e5863d9aSMike Snitzer return r; 2022cec47e3dSKiyoshi Ueda } 20239eef87daSKiyoshi Ueda 2024e5863d9aSMike Snitzer return 0; 2025cec47e3dSKiyoshi Ueda } 2026cec47e3dSKiyoshi Ueda 20272eb6e1e3SKeith Busch static void map_tio_request(struct kthread_work *work) 2028ba1cbad9SMike Snitzer { 20292eb6e1e3SKeith Busch struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work); 2030e5863d9aSMike Snitzer struct request *rq = tio->orig; 2031e5863d9aSMike Snitzer struct mapped_device *md = tio->md; 2032ba1cbad9SMike Snitzer 2033bfebd1cdSMike Snitzer if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) 20342d76fff1SMike Snitzer dm_requeue_original_request(md, rq); 20352eb6e1e3SKeith Busch } 20362eb6e1e3SKeith Busch 2037466d89a6SKeith Busch static void dm_start_request(struct mapped_device *md, struct request *orig) 2038ba1cbad9SMike Snitzer { 2039bfebd1cdSMike Snitzer if (!orig->q->mq_ops) 2040ba1cbad9SMike Snitzer blk_start_request(orig); 2041bfebd1cdSMike Snitzer else 2042bfebd1cdSMike Snitzer blk_mq_start_request(orig); 2043466d89a6SKeith Busch atomic_inc(&md->pending[rq_data_dir(orig)]); 2044ba1cbad9SMike Snitzer 20450ce65797SMike Snitzer if (md->seq_rq_merge_deadline_usecs) { 2046de3ec86dSMike Snitzer md->last_rq_pos = rq_end_sector(orig); 2047de3ec86dSMike Snitzer md->last_rq_rw = rq_data_dir(orig); 20480ce65797SMike Snitzer md->last_rq_start_time = ktime_get(); 20490ce65797SMike Snitzer } 2050de3ec86dSMike Snitzer 2051e262f347SMikulas Patocka if (unlikely(dm_stats_used(&md->stats))) { 2052e262f347SMikulas Patocka struct dm_rq_target_io *tio = tio_from_request(orig); 2053e262f347SMikulas Patocka tio->duration_jiffies = jiffies; 2054e262f347SMikulas Patocka tio->n_sectors = blk_rq_sectors(orig); 2055e262f347SMikulas Patocka dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig), 2056e262f347SMikulas Patocka tio->n_sectors, false, 0, &tio->stats_aux); 2057e262f347SMikulas Patocka } 2058e262f347SMikulas Patocka 2059ba1cbad9SMike Snitzer /* 2060ba1cbad9SMike Snitzer * Hold the md reference here for the in-flight I/O. 2061ba1cbad9SMike Snitzer * We can't rely on the reference count by device opener, 2062ba1cbad9SMike Snitzer * because the device may be closed during the request completion 2063ba1cbad9SMike Snitzer * when all bios are completed. 2064ba1cbad9SMike Snitzer * See the comment in rq_completed() too. 2065ba1cbad9SMike Snitzer */ 2066ba1cbad9SMike Snitzer dm_get(md); 2067ba1cbad9SMike Snitzer } 2068ba1cbad9SMike Snitzer 20690ce65797SMike Snitzer #define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000 20700ce65797SMike Snitzer 20710ce65797SMike Snitzer ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf) 20720ce65797SMike Snitzer { 20730ce65797SMike Snitzer return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs); 20740ce65797SMike Snitzer } 20750ce65797SMike Snitzer 20760ce65797SMike Snitzer ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md, 20770ce65797SMike Snitzer const char *buf, size_t count) 20780ce65797SMike Snitzer { 20790ce65797SMike Snitzer unsigned deadline; 20800ce65797SMike Snitzer 208117e149b8SMike Snitzer if (!dm_request_based(md) || md->use_blk_mq) 20820ce65797SMike Snitzer return count; 20830ce65797SMike Snitzer 20840ce65797SMike Snitzer if (kstrtouint(buf, 10, &deadline)) 20850ce65797SMike Snitzer return -EINVAL; 20860ce65797SMike Snitzer 20870ce65797SMike Snitzer if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS) 20880ce65797SMike Snitzer deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS; 20890ce65797SMike Snitzer 20900ce65797SMike Snitzer md->seq_rq_merge_deadline_usecs = deadline; 20910ce65797SMike Snitzer 20920ce65797SMike Snitzer return count; 20930ce65797SMike Snitzer } 20940ce65797SMike Snitzer 20950ce65797SMike Snitzer static bool dm_request_peeked_before_merge_deadline(struct mapped_device *md) 20960ce65797SMike Snitzer { 20970ce65797SMike Snitzer ktime_t kt_deadline; 20980ce65797SMike Snitzer 20990ce65797SMike Snitzer if (!md->seq_rq_merge_deadline_usecs) 21000ce65797SMike Snitzer return false; 21010ce65797SMike Snitzer 21020ce65797SMike Snitzer kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC); 21030ce65797SMike Snitzer kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline); 21040ce65797SMike Snitzer 21050ce65797SMike Snitzer return !ktime_after(ktime_get(), kt_deadline); 21060ce65797SMike Snitzer } 21070ce65797SMike Snitzer 2108cec47e3dSKiyoshi Ueda /* 2109cec47e3dSKiyoshi Ueda * q->request_fn for request-based dm. 2110cec47e3dSKiyoshi Ueda * Called with the queue lock held. 2111cec47e3dSKiyoshi Ueda */ 2112cec47e3dSKiyoshi Ueda static void dm_request_fn(struct request_queue *q) 2113cec47e3dSKiyoshi Ueda { 2114cec47e3dSKiyoshi Ueda struct mapped_device *md = q->queuedata; 211583d5e5b0SMikulas Patocka int srcu_idx; 211683d5e5b0SMikulas Patocka struct dm_table *map = dm_get_live_table(md, &srcu_idx); 2117cec47e3dSKiyoshi Ueda struct dm_target *ti; 2118466d89a6SKeith Busch struct request *rq; 21192eb6e1e3SKeith Busch struct dm_rq_target_io *tio; 212029e4013dSTejun Heo sector_t pos; 2121cec47e3dSKiyoshi Ueda 2122cec47e3dSKiyoshi Ueda /* 2123b4324feeSKiyoshi Ueda * For suspend, check blk_queue_stopped() and increment 2124b4324feeSKiyoshi Ueda * ->pending within a single queue_lock not to increment the 2125b4324feeSKiyoshi Ueda * number of in-flight I/Os after the queue is stopped in 2126b4324feeSKiyoshi Ueda * dm_suspend(). 2127cec47e3dSKiyoshi Ueda */ 21287eaceaccSJens Axboe while (!blk_queue_stopped(q)) { 2129cec47e3dSKiyoshi Ueda rq = blk_peek_request(q); 2130cec47e3dSKiyoshi Ueda if (!rq) 21319d1deb83SMike Snitzer goto out; 2132cec47e3dSKiyoshi Ueda 213329e4013dSTejun Heo /* always use block 0 to find the target for flushes for now */ 213429e4013dSTejun Heo pos = 0; 213529e4013dSTejun Heo if (!(rq->cmd_flags & REQ_FLUSH)) 213629e4013dSTejun Heo pos = blk_rq_pos(rq); 2137d0bcb878SKiyoshi Ueda 213829e4013dSTejun Heo ti = dm_table_find_target(map, pos); 2139ba1cbad9SMike Snitzer if (!dm_target_is_valid(ti)) { 2140ba1cbad9SMike Snitzer /* 2141466d89a6SKeith Busch * Must perform setup, that rq_completed() requires, 2142ba1cbad9SMike Snitzer * before calling dm_kill_unmapped_request 2143ba1cbad9SMike Snitzer */ 2144ba1cbad9SMike Snitzer DMERR_LIMIT("request attempted access beyond the end of device"); 2145466d89a6SKeith Busch dm_start_request(md, rq); 2146466d89a6SKeith Busch dm_kill_unmapped_request(rq, -EIO); 2147ba1cbad9SMike Snitzer continue; 2148ba1cbad9SMike Snitzer } 214929e4013dSTejun Heo 21500ce65797SMike Snitzer if (dm_request_peeked_before_merge_deadline(md) && 21510ce65797SMike Snitzer md_in_flight(md) && rq->bio && rq->bio->bi_vcnt == 1 && 2152de3ec86dSMike Snitzer md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq)) 2153de3ec86dSMike Snitzer goto delay_and_out; 2154de3ec86dSMike Snitzer 2155cec47e3dSKiyoshi Ueda if (ti->type->busy && ti->type->busy(ti)) 21567eaceaccSJens Axboe goto delay_and_out; 2157cec47e3dSKiyoshi Ueda 2158466d89a6SKeith Busch dm_start_request(md, rq); 2159b4324feeSKiyoshi Ueda 2160bfebd1cdSMike Snitzer tio = tio_from_request(rq); 21612eb6e1e3SKeith Busch /* Establish tio->ti before queuing work (map_tio_request) */ 21622eb6e1e3SKeith Busch tio->ti = ti; 21632eb6e1e3SKeith Busch queue_kthread_work(&md->kworker, &tio->work); 2164052189a2SKiyoshi Ueda BUG_ON(!irqs_disabled()); 2165cec47e3dSKiyoshi Ueda } 2166cec47e3dSKiyoshi Ueda 2167cec47e3dSKiyoshi Ueda goto out; 2168cec47e3dSKiyoshi Ueda 21697eaceaccSJens Axboe delay_and_out: 2170d548b34bSMike Snitzer blk_delay_queue(q, HZ / 100); 2171cec47e3dSKiyoshi Ueda out: 217283d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 2173cec47e3dSKiyoshi Ueda } 2174cec47e3dSKiyoshi Ueda 21751da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits) 21761da177e4SLinus Torvalds { 21778a57dfc6SChandra Seetharaman int r = bdi_bits; 21788a57dfc6SChandra Seetharaman struct mapped_device *md = congested_data; 21798a57dfc6SChandra Seetharaman struct dm_table *map; 21801da177e4SLinus Torvalds 21811eb787ecSAlasdair G Kergon if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 218283d5e5b0SMikulas Patocka map = dm_get_live_table_fast(md); 21838a57dfc6SChandra Seetharaman if (map) { 2184cec47e3dSKiyoshi Ueda /* 2185cec47e3dSKiyoshi Ueda * Request-based dm cares about only own queue for 2186cec47e3dSKiyoshi Ueda * the query about congestion status of request_queue 2187cec47e3dSKiyoshi Ueda */ 2188cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 2189cec47e3dSKiyoshi Ueda r = md->queue->backing_dev_info.state & 2190cec47e3dSKiyoshi Ueda bdi_bits; 2191cec47e3dSKiyoshi Ueda else 21921da177e4SLinus Torvalds r = dm_table_any_congested(map, bdi_bits); 21938a57dfc6SChandra Seetharaman } 219483d5e5b0SMikulas Patocka dm_put_live_table_fast(md); 21958a57dfc6SChandra Seetharaman } 21968a57dfc6SChandra Seetharaman 21971da177e4SLinus Torvalds return r; 21981da177e4SLinus Torvalds } 21991da177e4SLinus Torvalds 22001da177e4SLinus Torvalds /*----------------------------------------------------------------- 22011da177e4SLinus Torvalds * An IDR is used to keep track of allocated minor numbers. 22021da177e4SLinus Torvalds *---------------------------------------------------------------*/ 22032b06cfffSAlasdair G Kergon static void free_minor(int minor) 22041da177e4SLinus Torvalds { 2205f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 22061da177e4SLinus Torvalds idr_remove(&_minor_idr, minor); 2207f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 22081da177e4SLinus Torvalds } 22091da177e4SLinus Torvalds 22101da177e4SLinus Torvalds /* 22111da177e4SLinus Torvalds * See if the device with a specific minor # is free. 22121da177e4SLinus Torvalds */ 2213cf13ab8eSFrederik Deweerdt static int specific_minor(int minor) 22141da177e4SLinus Torvalds { 2215c9d76be6STejun Heo int r; 22161da177e4SLinus Torvalds 22171da177e4SLinus Torvalds if (minor >= (1 << MINORBITS)) 22181da177e4SLinus Torvalds return -EINVAL; 22191da177e4SLinus Torvalds 2220c9d76be6STejun Heo idr_preload(GFP_KERNEL); 2221f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 22221da177e4SLinus Torvalds 2223c9d76be6STejun Heo r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT); 22241da177e4SLinus Torvalds 2225f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 2226c9d76be6STejun Heo idr_preload_end(); 2227c9d76be6STejun Heo if (r < 0) 2228c9d76be6STejun Heo return r == -ENOSPC ? -EBUSY : r; 2229c9d76be6STejun Heo return 0; 22301da177e4SLinus Torvalds } 22311da177e4SLinus Torvalds 2232cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor) 22331da177e4SLinus Torvalds { 2234c9d76be6STejun Heo int r; 22351da177e4SLinus Torvalds 2236c9d76be6STejun Heo idr_preload(GFP_KERNEL); 2237f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 22381da177e4SLinus Torvalds 2239c9d76be6STejun Heo r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT); 22401da177e4SLinus Torvalds 2241f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 2242c9d76be6STejun Heo idr_preload_end(); 2243c9d76be6STejun Heo if (r < 0) 22441da177e4SLinus Torvalds return r; 2245c9d76be6STejun Heo *minor = r; 2246c9d76be6STejun Heo return 0; 22471da177e4SLinus Torvalds } 22481da177e4SLinus Torvalds 224983d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops; 22501da177e4SLinus Torvalds 225153d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work); 225253d5914fSMikulas Patocka 22534a0b4ddfSMike Snitzer static void dm_init_md_queue(struct mapped_device *md) 22544a0b4ddfSMike Snitzer { 22554a0b4ddfSMike Snitzer /* 22564a0b4ddfSMike Snitzer * Request-based dm devices cannot be stacked on top of bio-based dm 2257bfebd1cdSMike Snitzer * devices. The type of this dm device may not have been decided yet. 22584a0b4ddfSMike Snitzer * The type is decided at the first table loading time. 22594a0b4ddfSMike Snitzer * To prevent problematic device stacking, clear the queue flag 22604a0b4ddfSMike Snitzer * for request stacking support until then. 22614a0b4ddfSMike Snitzer * 22624a0b4ddfSMike Snitzer * This queue is new, so no concurrency on the queue_flags. 22634a0b4ddfSMike Snitzer */ 22644a0b4ddfSMike Snitzer queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue); 2265bfebd1cdSMike Snitzer } 22664a0b4ddfSMike Snitzer 2267bfebd1cdSMike Snitzer static void dm_init_old_md_queue(struct mapped_device *md) 2268bfebd1cdSMike Snitzer { 226917e149b8SMike Snitzer md->use_blk_mq = false; 2270bfebd1cdSMike Snitzer dm_init_md_queue(md); 2271bfebd1cdSMike Snitzer 2272bfebd1cdSMike Snitzer /* 2273bfebd1cdSMike Snitzer * Initialize aspects of queue that aren't relevant for blk-mq 2274bfebd1cdSMike Snitzer */ 22754a0b4ddfSMike Snitzer md->queue->queuedata = md; 22764a0b4ddfSMike Snitzer md->queue->backing_dev_info.congested_fn = dm_any_congested; 22774a0b4ddfSMike Snitzer md->queue->backing_dev_info.congested_data = md; 2278ff36ab34SMike Snitzer 22794a0b4ddfSMike Snitzer blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY); 22804a0b4ddfSMike Snitzer } 22814a0b4ddfSMike Snitzer 22820f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md) 22830f20972fSMike Snitzer { 22840f20972fSMike Snitzer cleanup_srcu_struct(&md->io_barrier); 22850f20972fSMike Snitzer 22860f20972fSMike Snitzer if (md->wq) 22870f20972fSMike Snitzer destroy_workqueue(md->wq); 22880f20972fSMike Snitzer if (md->kworker_task) 22890f20972fSMike Snitzer kthread_stop(md->kworker_task); 22900f20972fSMike Snitzer if (md->io_pool) 22910f20972fSMike Snitzer mempool_destroy(md->io_pool); 22920f20972fSMike Snitzer if (md->rq_pool) 22930f20972fSMike Snitzer mempool_destroy(md->rq_pool); 22940f20972fSMike Snitzer if (md->bs) 22950f20972fSMike Snitzer bioset_free(md->bs); 22960f20972fSMike Snitzer 22970f20972fSMike Snitzer if (md->disk) { 22980f20972fSMike Snitzer spin_lock(&_minor_lock); 22990f20972fSMike Snitzer md->disk->private_data = NULL; 23000f20972fSMike Snitzer spin_unlock(&_minor_lock); 23010f20972fSMike Snitzer if (blk_get_integrity(md->disk)) 23020f20972fSMike Snitzer blk_integrity_unregister(md->disk); 23030f20972fSMike Snitzer del_gendisk(md->disk); 23040f20972fSMike Snitzer put_disk(md->disk); 23050f20972fSMike Snitzer } 23060f20972fSMike Snitzer 23070f20972fSMike Snitzer if (md->queue) 23080f20972fSMike Snitzer blk_cleanup_queue(md->queue); 23090f20972fSMike Snitzer 23100f20972fSMike Snitzer if (md->bdev) { 23110f20972fSMike Snitzer bdput(md->bdev); 23120f20972fSMike Snitzer md->bdev = NULL; 23130f20972fSMike Snitzer } 23140f20972fSMike Snitzer } 23150f20972fSMike Snitzer 23161da177e4SLinus Torvalds /* 23171da177e4SLinus Torvalds * Allocate and initialise a blank device with a given minor. 23181da177e4SLinus Torvalds */ 23192b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor) 23201da177e4SLinus Torvalds { 23211da177e4SLinus Torvalds int r; 2322cf13ab8eSFrederik Deweerdt struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL); 2323ba61fdd1SJeff Mahoney void *old_md; 23241da177e4SLinus Torvalds 23251da177e4SLinus Torvalds if (!md) { 23261da177e4SLinus Torvalds DMWARN("unable to allocate device, out of memory."); 23271da177e4SLinus Torvalds return NULL; 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds 233010da4f79SJeff Mahoney if (!try_module_get(THIS_MODULE)) 23316ed7ade8SMilan Broz goto bad_module_get; 233210da4f79SJeff Mahoney 23331da177e4SLinus Torvalds /* get a minor number for the dev */ 23342b06cfffSAlasdair G Kergon if (minor == DM_ANY_MINOR) 2335cf13ab8eSFrederik Deweerdt r = next_free_minor(&minor); 23362b06cfffSAlasdair G Kergon else 2337cf13ab8eSFrederik Deweerdt r = specific_minor(minor); 23381da177e4SLinus Torvalds if (r < 0) 23396ed7ade8SMilan Broz goto bad_minor; 23401da177e4SLinus Torvalds 234183d5e5b0SMikulas Patocka r = init_srcu_struct(&md->io_barrier); 234283d5e5b0SMikulas Patocka if (r < 0) 234383d5e5b0SMikulas Patocka goto bad_io_barrier; 234483d5e5b0SMikulas Patocka 234517e149b8SMike Snitzer md->use_blk_mq = use_blk_mq; 2346a5664dadSMike Snitzer md->type = DM_TYPE_NONE; 2347e61290a4SDaniel Walker mutex_init(&md->suspend_lock); 2348a5664dadSMike Snitzer mutex_init(&md->type_lock); 234986f1152bSBenjamin Marzinski mutex_init(&md->table_devices_lock); 2350022c2611SMikulas Patocka spin_lock_init(&md->deferred_lock); 23511da177e4SLinus Torvalds atomic_set(&md->holders, 1); 23525c6bd75dSAlasdair G Kergon atomic_set(&md->open_count, 0); 23531da177e4SLinus Torvalds atomic_set(&md->event_nr, 0); 23547a8c3d3bSMike Anderson atomic_set(&md->uevent_seq, 0); 23557a8c3d3bSMike Anderson INIT_LIST_HEAD(&md->uevent_list); 235686f1152bSBenjamin Marzinski INIT_LIST_HEAD(&md->table_devices); 23577a8c3d3bSMike Anderson spin_lock_init(&md->uevent_lock); 23581da177e4SLinus Torvalds 23594a0b4ddfSMike Snitzer md->queue = blk_alloc_queue(GFP_KERNEL); 23601da177e4SLinus Torvalds if (!md->queue) 23610f20972fSMike Snitzer goto bad; 23621da177e4SLinus Torvalds 23634a0b4ddfSMike Snitzer dm_init_md_queue(md); 23649faf400fSStefan Bader 23651da177e4SLinus Torvalds md->disk = alloc_disk(1); 23661da177e4SLinus Torvalds if (!md->disk) 23670f20972fSMike Snitzer goto bad; 23681da177e4SLinus Torvalds 2369316d315bSNikanth Karthikesan atomic_set(&md->pending[0], 0); 2370316d315bSNikanth Karthikesan atomic_set(&md->pending[1], 0); 2371f0b04115SJeff Mahoney init_waitqueue_head(&md->wait); 237253d5914fSMikulas Patocka INIT_WORK(&md->work, dm_wq_work); 2373f0b04115SJeff Mahoney init_waitqueue_head(&md->eventq); 23742995fa78SMikulas Patocka init_completion(&md->kobj_holder.completion); 23752eb6e1e3SKeith Busch md->kworker_task = NULL; 2376f0b04115SJeff Mahoney 23771da177e4SLinus Torvalds md->disk->major = _major; 23781da177e4SLinus Torvalds md->disk->first_minor = minor; 23791da177e4SLinus Torvalds md->disk->fops = &dm_blk_dops; 23801da177e4SLinus Torvalds md->disk->queue = md->queue; 23811da177e4SLinus Torvalds md->disk->private_data = md; 23821da177e4SLinus Torvalds sprintf(md->disk->disk_name, "dm-%d", minor); 23831da177e4SLinus Torvalds add_disk(md->disk); 23847e51f257SMike Anderson format_dev_t(md->name, MKDEV(_major, minor)); 23851da177e4SLinus Torvalds 2386670368a8STejun Heo md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0); 2387304f3f6aSMilan Broz if (!md->wq) 23880f20972fSMike Snitzer goto bad; 2389304f3f6aSMilan Broz 239032a926daSMikulas Patocka md->bdev = bdget_disk(md->disk, 0); 239132a926daSMikulas Patocka if (!md->bdev) 23920f20972fSMike Snitzer goto bad; 239332a926daSMikulas Patocka 23946a8736d1STejun Heo bio_init(&md->flush_bio); 23956a8736d1STejun Heo md->flush_bio.bi_bdev = md->bdev; 23966a8736d1STejun Heo md->flush_bio.bi_rw = WRITE_FLUSH; 23976a8736d1STejun Heo 2398fd2ed4d2SMikulas Patocka dm_stats_init(&md->stats); 2399fd2ed4d2SMikulas Patocka 2400ba61fdd1SJeff Mahoney /* Populate the mapping, nobody knows we exist yet */ 2401f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 2402ba61fdd1SJeff Mahoney old_md = idr_replace(&_minor_idr, md, minor); 2403f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 2404ba61fdd1SJeff Mahoney 2405ba61fdd1SJeff Mahoney BUG_ON(old_md != MINOR_ALLOCED); 2406ba61fdd1SJeff Mahoney 24071da177e4SLinus Torvalds return md; 24081da177e4SLinus Torvalds 24090f20972fSMike Snitzer bad: 24100f20972fSMike Snitzer cleanup_mapped_device(md); 241183d5e5b0SMikulas Patocka bad_io_barrier: 24121da177e4SLinus Torvalds free_minor(minor); 24136ed7ade8SMilan Broz bad_minor: 241410da4f79SJeff Mahoney module_put(THIS_MODULE); 24156ed7ade8SMilan Broz bad_module_get: 24161da177e4SLinus Torvalds kfree(md); 24171da177e4SLinus Torvalds return NULL; 24181da177e4SLinus Torvalds } 24191da177e4SLinus Torvalds 2420ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md); 2421ae9da83fSJun'ichi Nomura 24221da177e4SLinus Torvalds static void free_dev(struct mapped_device *md) 24231da177e4SLinus Torvalds { 2424f331c029STejun Heo int minor = MINOR(disk_devt(md->disk)); 242563d94e48SJun'ichi Nomura 2426ae9da83fSJun'ichi Nomura unlock_fs(md); 24272eb6e1e3SKeith Busch 24280f20972fSMike Snitzer cleanup_mapped_device(md); 242917e149b8SMike Snitzer if (md->use_blk_mq) 2430bfebd1cdSMike Snitzer blk_mq_free_tag_set(&md->tag_set); 24310f20972fSMike Snitzer 24320f20972fSMike Snitzer free_table_devices(&md->table_devices); 24330f20972fSMike Snitzer dm_stats_cleanup(&md->stats); 243463a4f065SMike Snitzer free_minor(minor); 243563a4f065SMike Snitzer 243610da4f79SJeff Mahoney module_put(THIS_MODULE); 24371da177e4SLinus Torvalds kfree(md); 24381da177e4SLinus Torvalds } 24391da177e4SLinus Torvalds 2440e6ee8c0bSKiyoshi Ueda static void __bind_mempools(struct mapped_device *md, struct dm_table *t) 2441e6ee8c0bSKiyoshi Ueda { 2442c0820cf5SMikulas Patocka struct dm_md_mempools *p = dm_table_get_md_mempools(t); 2443e6ee8c0bSKiyoshi Ueda 24444e6e36c3SMike Snitzer if (md->bs) { 24454e6e36c3SMike Snitzer /* The md already has necessary mempools. */ 24464e6e36c3SMike Snitzer if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) { 2447c0820cf5SMikulas Patocka /* 244816245bdcSJun'ichi Nomura * Reload bioset because front_pad may have changed 244916245bdcSJun'ichi Nomura * because a different table was loaded. 2450c0820cf5SMikulas Patocka */ 2451c0820cf5SMikulas Patocka bioset_free(md->bs); 2452c0820cf5SMikulas Patocka md->bs = p->bs; 2453c0820cf5SMikulas Patocka p->bs = NULL; 2454c0820cf5SMikulas Patocka } 2455cbc4e3c1SMike Snitzer /* 24564e6e36c3SMike Snitzer * There's no need to reload with request-based dm 24574e6e36c3SMike Snitzer * because the size of front_pad doesn't change. 24584e6e36c3SMike Snitzer * Note for future: If you are to reload bioset, 24594e6e36c3SMike Snitzer * prep-ed requests in the queue may refer 24604e6e36c3SMike Snitzer * to bio from the old bioset, so you must walk 24614e6e36c3SMike Snitzer * through the queue to unprep. 2462cbc4e3c1SMike Snitzer */ 2463cbc4e3c1SMike Snitzer goto out; 2464cbc4e3c1SMike Snitzer } 2465cbc4e3c1SMike Snitzer 2466*78d8e58aSMike Snitzer BUG_ON(!p || md->io_pool || md->rq_pool || md->bs); 2467*78d8e58aSMike Snitzer 2468e6ee8c0bSKiyoshi Ueda md->io_pool = p->io_pool; 2469e6ee8c0bSKiyoshi Ueda p->io_pool = NULL; 24701ae49ea2SMike Snitzer md->rq_pool = p->rq_pool; 24711ae49ea2SMike Snitzer p->rq_pool = NULL; 2472e6ee8c0bSKiyoshi Ueda md->bs = p->bs; 2473e6ee8c0bSKiyoshi Ueda p->bs = NULL; 24744e6e36c3SMike Snitzer 2475e6ee8c0bSKiyoshi Ueda out: 247602233342SMike Snitzer /* mempool bind completed, no longer need any mempools in the table */ 2477e6ee8c0bSKiyoshi Ueda dm_table_free_md_mempools(t); 2478e6ee8c0bSKiyoshi Ueda } 2479e6ee8c0bSKiyoshi Ueda 24801da177e4SLinus Torvalds /* 24811da177e4SLinus Torvalds * Bind a table to the device. 24821da177e4SLinus Torvalds */ 24831da177e4SLinus Torvalds static void event_callback(void *context) 24841da177e4SLinus Torvalds { 24857a8c3d3bSMike Anderson unsigned long flags; 24867a8c3d3bSMike Anderson LIST_HEAD(uevents); 24871da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) context; 24881da177e4SLinus Torvalds 24897a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 24907a8c3d3bSMike Anderson list_splice_init(&md->uevent_list, &uevents); 24917a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 24927a8c3d3bSMike Anderson 2493ed9e1982STejun Heo dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); 24947a8c3d3bSMike Anderson 24951da177e4SLinus Torvalds atomic_inc(&md->event_nr); 24961da177e4SLinus Torvalds wake_up(&md->eventq); 24971da177e4SLinus Torvalds } 24981da177e4SLinus Torvalds 2499c217649bSMike Snitzer /* 2500c217649bSMike Snitzer * Protected by md->suspend_lock obtained by dm_swap_table(). 2501c217649bSMike Snitzer */ 25024e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size) 25031da177e4SLinus Torvalds { 25044e90188bSAlasdair G Kergon set_capacity(md->disk, size); 25051da177e4SLinus Torvalds 2506db8fef4fSMikulas Patocka i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT); 25071da177e4SLinus Torvalds } 25081da177e4SLinus Torvalds 2509042d2a9bSAlasdair G Kergon /* 2510d5b9dd04SMikulas Patocka * Return 1 if the queue has a compulsory merge_bvec_fn function. 2511d5b9dd04SMikulas Patocka * 2512d5b9dd04SMikulas Patocka * If this function returns 0, then the device is either a non-dm 2513d5b9dd04SMikulas Patocka * device without a merge_bvec_fn, or it is a dm device that is 2514d5b9dd04SMikulas Patocka * able to split any bios it receives that are too big. 2515d5b9dd04SMikulas Patocka */ 2516d5b9dd04SMikulas Patocka int dm_queue_merge_is_compulsory(struct request_queue *q) 2517d5b9dd04SMikulas Patocka { 2518d5b9dd04SMikulas Patocka struct mapped_device *dev_md; 2519d5b9dd04SMikulas Patocka 2520d5b9dd04SMikulas Patocka if (!q->merge_bvec_fn) 2521d5b9dd04SMikulas Patocka return 0; 2522d5b9dd04SMikulas Patocka 2523ff36ab34SMike Snitzer if (q->make_request_fn == dm_make_request) { 2524d5b9dd04SMikulas Patocka dev_md = q->queuedata; 2525d5b9dd04SMikulas Patocka if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags)) 2526d5b9dd04SMikulas Patocka return 0; 2527d5b9dd04SMikulas Patocka } 2528d5b9dd04SMikulas Patocka 2529d5b9dd04SMikulas Patocka return 1; 2530d5b9dd04SMikulas Patocka } 2531d5b9dd04SMikulas Patocka 2532d5b9dd04SMikulas Patocka static int dm_device_merge_is_compulsory(struct dm_target *ti, 2533d5b9dd04SMikulas Patocka struct dm_dev *dev, sector_t start, 2534d5b9dd04SMikulas Patocka sector_t len, void *data) 2535d5b9dd04SMikulas Patocka { 2536d5b9dd04SMikulas Patocka struct block_device *bdev = dev->bdev; 2537d5b9dd04SMikulas Patocka struct request_queue *q = bdev_get_queue(bdev); 2538d5b9dd04SMikulas Patocka 2539d5b9dd04SMikulas Patocka return dm_queue_merge_is_compulsory(q); 2540d5b9dd04SMikulas Patocka } 2541d5b9dd04SMikulas Patocka 2542d5b9dd04SMikulas Patocka /* 2543d5b9dd04SMikulas Patocka * Return 1 if it is acceptable to ignore merge_bvec_fn based 2544d5b9dd04SMikulas Patocka * on the properties of the underlying devices. 2545d5b9dd04SMikulas Patocka */ 2546d5b9dd04SMikulas Patocka static int dm_table_merge_is_optional(struct dm_table *table) 2547d5b9dd04SMikulas Patocka { 2548d5b9dd04SMikulas Patocka unsigned i = 0; 2549d5b9dd04SMikulas Patocka struct dm_target *ti; 2550d5b9dd04SMikulas Patocka 2551d5b9dd04SMikulas Patocka while (i < dm_table_get_num_targets(table)) { 2552d5b9dd04SMikulas Patocka ti = dm_table_get_target(table, i++); 2553d5b9dd04SMikulas Patocka 2554d5b9dd04SMikulas Patocka if (ti->type->iterate_devices && 2555d5b9dd04SMikulas Patocka ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL)) 2556d5b9dd04SMikulas Patocka return 0; 2557d5b9dd04SMikulas Patocka } 2558d5b9dd04SMikulas Patocka 2559d5b9dd04SMikulas Patocka return 1; 2560d5b9dd04SMikulas Patocka } 2561d5b9dd04SMikulas Patocka 2562d5b9dd04SMikulas Patocka /* 2563042d2a9bSAlasdair G Kergon * Returns old map, which caller must destroy. 2564042d2a9bSAlasdair G Kergon */ 2565042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t, 2566754c5fc7SMike Snitzer struct queue_limits *limits) 25671da177e4SLinus Torvalds { 2568042d2a9bSAlasdair G Kergon struct dm_table *old_map; 2569165125e1SJens Axboe struct request_queue *q = md->queue; 25701da177e4SLinus Torvalds sector_t size; 2571d5b9dd04SMikulas Patocka int merge_is_optional; 25721da177e4SLinus Torvalds 25731da177e4SLinus Torvalds size = dm_table_get_size(t); 25743ac51e74SDarrick J. Wong 25753ac51e74SDarrick J. Wong /* 25763ac51e74SDarrick J. Wong * Wipe any geometry if the size of the table changed. 25773ac51e74SDarrick J. Wong */ 2578fd2ed4d2SMikulas Patocka if (size != dm_get_size(md)) 25793ac51e74SDarrick J. Wong memset(&md->geometry, 0, sizeof(md->geometry)); 25803ac51e74SDarrick J. Wong 25814e90188bSAlasdair G Kergon __set_size(md, size); 25821da177e4SLinus Torvalds 2583cf222b37SAlasdair G Kergon dm_table_event_callback(t, event_callback, md); 25842ca3310eSAlasdair G Kergon 2585e6ee8c0bSKiyoshi Ueda /* 2586e6ee8c0bSKiyoshi Ueda * The queue hasn't been stopped yet, if the old table type wasn't 2587e6ee8c0bSKiyoshi Ueda * for request-based during suspension. So stop it to prevent 2588e6ee8c0bSKiyoshi Ueda * I/O mapping before resume. 2589e6ee8c0bSKiyoshi Ueda * This must be done before setting the queue restrictions, 2590e6ee8c0bSKiyoshi Ueda * because request-based dm may be run just after the setting. 2591e6ee8c0bSKiyoshi Ueda */ 2592bfebd1cdSMike Snitzer if (dm_table_request_based(t)) 2593e6ee8c0bSKiyoshi Ueda stop_queue(q); 2594e6ee8c0bSKiyoshi Ueda 2595e6ee8c0bSKiyoshi Ueda __bind_mempools(md, t); 2596e6ee8c0bSKiyoshi Ueda 2597d5b9dd04SMikulas Patocka merge_is_optional = dm_table_merge_is_optional(t); 2598d5b9dd04SMikulas Patocka 2599a12f5d48SEric Dumazet old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 260083d5e5b0SMikulas Patocka rcu_assign_pointer(md->map, t); 260136a0456fSAlasdair G Kergon md->immutable_target_type = dm_table_get_immutable_target_type(t); 260236a0456fSAlasdair G Kergon 2603754c5fc7SMike Snitzer dm_table_set_restrictions(t, q, limits); 2604d5b9dd04SMikulas Patocka if (merge_is_optional) 2605d5b9dd04SMikulas Patocka set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags); 2606d5b9dd04SMikulas Patocka else 2607d5b9dd04SMikulas Patocka clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags); 260841abc4e1SHannes Reinecke if (old_map) 260983d5e5b0SMikulas Patocka dm_sync_table(md); 26102ca3310eSAlasdair G Kergon 2611042d2a9bSAlasdair G Kergon return old_map; 26121da177e4SLinus Torvalds } 26131da177e4SLinus Torvalds 2614a7940155SAlasdair G Kergon /* 2615a7940155SAlasdair G Kergon * Returns unbound table for the caller to free. 2616a7940155SAlasdair G Kergon */ 2617a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md) 26181da177e4SLinus Torvalds { 2619a12f5d48SEric Dumazet struct dm_table *map = rcu_dereference_protected(md->map, 1); 26201da177e4SLinus Torvalds 26211da177e4SLinus Torvalds if (!map) 2622a7940155SAlasdair G Kergon return NULL; 26231da177e4SLinus Torvalds 26241da177e4SLinus Torvalds dm_table_event_callback(map, NULL, NULL); 26259cdb8520SMonam Agarwal RCU_INIT_POINTER(md->map, NULL); 262683d5e5b0SMikulas Patocka dm_sync_table(md); 2627a7940155SAlasdair G Kergon 2628a7940155SAlasdair G Kergon return map; 26291da177e4SLinus Torvalds } 26301da177e4SLinus Torvalds 26311da177e4SLinus Torvalds /* 26321da177e4SLinus Torvalds * Constructor for a new device. 26331da177e4SLinus Torvalds */ 26342b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result) 26351da177e4SLinus Torvalds { 26361da177e4SLinus Torvalds struct mapped_device *md; 26371da177e4SLinus Torvalds 26382b06cfffSAlasdair G Kergon md = alloc_dev(minor); 26391da177e4SLinus Torvalds if (!md) 26401da177e4SLinus Torvalds return -ENXIO; 26411da177e4SLinus Torvalds 2642784aae73SMilan Broz dm_sysfs_init(md); 2643784aae73SMilan Broz 26441da177e4SLinus Torvalds *result = md; 26451da177e4SLinus Torvalds return 0; 26461da177e4SLinus Torvalds } 26471da177e4SLinus Torvalds 2648a5664dadSMike Snitzer /* 2649a5664dadSMike Snitzer * Functions to manage md->type. 2650a5664dadSMike Snitzer * All are required to hold md->type_lock. 2651a5664dadSMike Snitzer */ 2652a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md) 2653a5664dadSMike Snitzer { 2654a5664dadSMike Snitzer mutex_lock(&md->type_lock); 2655a5664dadSMike Snitzer } 2656a5664dadSMike Snitzer 2657a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md) 2658a5664dadSMike Snitzer { 2659a5664dadSMike Snitzer mutex_unlock(&md->type_lock); 2660a5664dadSMike Snitzer } 2661a5664dadSMike Snitzer 2662a5664dadSMike Snitzer void dm_set_md_type(struct mapped_device *md, unsigned type) 2663a5664dadSMike Snitzer { 266400c4fc3bSMike Snitzer BUG_ON(!mutex_is_locked(&md->type_lock)); 2665a5664dadSMike Snitzer md->type = type; 2666a5664dadSMike Snitzer } 2667a5664dadSMike Snitzer 2668a5664dadSMike Snitzer unsigned dm_get_md_type(struct mapped_device *md) 2669a5664dadSMike Snitzer { 267000c4fc3bSMike Snitzer BUG_ON(!mutex_is_locked(&md->type_lock)); 2671a5664dadSMike Snitzer return md->type; 2672a5664dadSMike Snitzer } 2673a5664dadSMike Snitzer 267436a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md) 267536a0456fSAlasdair G Kergon { 267636a0456fSAlasdair G Kergon return md->immutable_target_type; 267736a0456fSAlasdair G Kergon } 267836a0456fSAlasdair G Kergon 26794a0b4ddfSMike Snitzer /* 2680f84cb8a4SMike Snitzer * The queue_limits are only valid as long as you have a reference 2681f84cb8a4SMike Snitzer * count on 'md'. 2682f84cb8a4SMike Snitzer */ 2683f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md) 2684f84cb8a4SMike Snitzer { 2685f84cb8a4SMike Snitzer BUG_ON(!atomic_read(&md->holders)); 2686f84cb8a4SMike Snitzer return &md->queue->limits; 2687f84cb8a4SMike Snitzer } 2688f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits); 2689f84cb8a4SMike Snitzer 2690bfebd1cdSMike Snitzer static void init_rq_based_worker_thread(struct mapped_device *md) 2691bfebd1cdSMike Snitzer { 2692bfebd1cdSMike Snitzer /* Initialize the request-based DM worker thread */ 2693bfebd1cdSMike Snitzer init_kthread_worker(&md->kworker); 2694bfebd1cdSMike Snitzer md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker, 2695bfebd1cdSMike Snitzer "kdmwork-%s", dm_device_name(md)); 2696bfebd1cdSMike Snitzer } 2697bfebd1cdSMike Snitzer 2698f84cb8a4SMike Snitzer /* 26994a0b4ddfSMike Snitzer * Fully initialize a request-based queue (->elevator, ->request_fn, etc). 27004a0b4ddfSMike Snitzer */ 27014a0b4ddfSMike Snitzer static int dm_init_request_based_queue(struct mapped_device *md) 27024a0b4ddfSMike Snitzer { 27034a0b4ddfSMike Snitzer struct request_queue *q = NULL; 27044a0b4ddfSMike Snitzer 27054a0b4ddfSMike Snitzer /* Fully initialize the queue */ 27064a0b4ddfSMike Snitzer q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL); 27074a0b4ddfSMike Snitzer if (!q) 2708bfebd1cdSMike Snitzer return -EINVAL; 27094a0b4ddfSMike Snitzer 27100ce65797SMike Snitzer /* disable dm_request_fn's merge heuristic by default */ 27110ce65797SMike Snitzer md->seq_rq_merge_deadline_usecs = 0; 27120ce65797SMike Snitzer 27134a0b4ddfSMike Snitzer md->queue = q; 2714bfebd1cdSMike Snitzer dm_init_old_md_queue(md); 27154a0b4ddfSMike Snitzer blk_queue_softirq_done(md->queue, dm_softirq_done); 27164a0b4ddfSMike Snitzer blk_queue_prep_rq(md->queue, dm_prep_fn); 27174a0b4ddfSMike Snitzer 2718bfebd1cdSMike Snitzer init_rq_based_worker_thread(md); 27192eb6e1e3SKeith Busch 27204a0b4ddfSMike Snitzer elv_register_queue(md->queue); 27214a0b4ddfSMike Snitzer 2722bfebd1cdSMike Snitzer return 0; 2723bfebd1cdSMike Snitzer } 2724bfebd1cdSMike Snitzer 2725bfebd1cdSMike Snitzer static int dm_mq_init_request(void *data, struct request *rq, 2726bfebd1cdSMike Snitzer unsigned int hctx_idx, unsigned int request_idx, 2727bfebd1cdSMike Snitzer unsigned int numa_node) 2728bfebd1cdSMike Snitzer { 2729bfebd1cdSMike Snitzer struct mapped_device *md = data; 2730bfebd1cdSMike Snitzer struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq); 2731bfebd1cdSMike Snitzer 2732bfebd1cdSMike Snitzer /* 2733bfebd1cdSMike Snitzer * Must initialize md member of tio, otherwise it won't 2734bfebd1cdSMike Snitzer * be available in dm_mq_queue_rq. 2735bfebd1cdSMike Snitzer */ 2736bfebd1cdSMike Snitzer tio->md = md; 2737bfebd1cdSMike Snitzer 2738bfebd1cdSMike Snitzer return 0; 2739bfebd1cdSMike Snitzer } 2740bfebd1cdSMike Snitzer 2741bfebd1cdSMike Snitzer static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx, 2742bfebd1cdSMike Snitzer const struct blk_mq_queue_data *bd) 2743bfebd1cdSMike Snitzer { 2744bfebd1cdSMike Snitzer struct request *rq = bd->rq; 2745bfebd1cdSMike Snitzer struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq); 2746bfebd1cdSMike Snitzer struct mapped_device *md = tio->md; 2747bfebd1cdSMike Snitzer int srcu_idx; 2748bfebd1cdSMike Snitzer struct dm_table *map = dm_get_live_table(md, &srcu_idx); 2749bfebd1cdSMike Snitzer struct dm_target *ti; 2750bfebd1cdSMike Snitzer sector_t pos; 2751bfebd1cdSMike Snitzer 2752bfebd1cdSMike Snitzer /* always use block 0 to find the target for flushes for now */ 2753bfebd1cdSMike Snitzer pos = 0; 2754bfebd1cdSMike Snitzer if (!(rq->cmd_flags & REQ_FLUSH)) 2755bfebd1cdSMike Snitzer pos = blk_rq_pos(rq); 2756bfebd1cdSMike Snitzer 2757bfebd1cdSMike Snitzer ti = dm_table_find_target(map, pos); 2758bfebd1cdSMike Snitzer if (!dm_target_is_valid(ti)) { 2759bfebd1cdSMike Snitzer dm_put_live_table(md, srcu_idx); 2760bfebd1cdSMike Snitzer DMERR_LIMIT("request attempted access beyond the end of device"); 2761bfebd1cdSMike Snitzer /* 2762bfebd1cdSMike Snitzer * Must perform setup, that rq_completed() requires, 2763bfebd1cdSMike Snitzer * before returning BLK_MQ_RQ_QUEUE_ERROR 2764bfebd1cdSMike Snitzer */ 2765bfebd1cdSMike Snitzer dm_start_request(md, rq); 2766bfebd1cdSMike Snitzer return BLK_MQ_RQ_QUEUE_ERROR; 2767bfebd1cdSMike Snitzer } 2768bfebd1cdSMike Snitzer dm_put_live_table(md, srcu_idx); 2769bfebd1cdSMike Snitzer 2770bfebd1cdSMike Snitzer if (ti->type->busy && ti->type->busy(ti)) 2771bfebd1cdSMike Snitzer return BLK_MQ_RQ_QUEUE_BUSY; 2772bfebd1cdSMike Snitzer 2773bfebd1cdSMike Snitzer dm_start_request(md, rq); 2774bfebd1cdSMike Snitzer 2775bfebd1cdSMike Snitzer /* Init tio using md established in .init_request */ 2776bfebd1cdSMike Snitzer init_tio(tio, rq, md); 2777bfebd1cdSMike Snitzer 277802233342SMike Snitzer /* 277902233342SMike Snitzer * Establish tio->ti before queuing work (map_tio_request) 278002233342SMike Snitzer * or making direct call to map_request(). 278102233342SMike Snitzer */ 2782bfebd1cdSMike Snitzer tio->ti = ti; 278302233342SMike Snitzer 278402233342SMike Snitzer /* Clone the request if underlying devices aren't blk-mq */ 278502233342SMike Snitzer if (dm_table_get_type(map) == DM_TYPE_REQUEST_BASED) { 278602233342SMike Snitzer /* clone request is allocated at the end of the pdu */ 278702233342SMike Snitzer tio->clone = (void *)blk_mq_rq_to_pdu(rq) + sizeof(struct dm_rq_target_io); 278845714fbeSMike Snitzer (void) clone_rq(rq, md, tio, GFP_ATOMIC); 2789bfebd1cdSMike Snitzer queue_kthread_work(&md->kworker, &tio->work); 279002233342SMike Snitzer } else { 279102233342SMike Snitzer /* Direct call is fine since .queue_rq allows allocations */ 279245714fbeSMike Snitzer if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) { 279345714fbeSMike Snitzer /* Undo dm_start_request() before requeuing */ 2794e262f347SMikulas Patocka rq_end_stats(md, rq); 279545714fbeSMike Snitzer rq_completed(md, rq_data_dir(rq), false); 279645714fbeSMike Snitzer return BLK_MQ_RQ_QUEUE_BUSY; 279745714fbeSMike Snitzer } 279802233342SMike Snitzer } 2799bfebd1cdSMike Snitzer 2800bfebd1cdSMike Snitzer return BLK_MQ_RQ_QUEUE_OK; 2801bfebd1cdSMike Snitzer } 2802bfebd1cdSMike Snitzer 2803bfebd1cdSMike Snitzer static struct blk_mq_ops dm_mq_ops = { 2804bfebd1cdSMike Snitzer .queue_rq = dm_mq_queue_rq, 2805bfebd1cdSMike Snitzer .map_queue = blk_mq_map_queue, 2806bfebd1cdSMike Snitzer .complete = dm_softirq_done, 2807bfebd1cdSMike Snitzer .init_request = dm_mq_init_request, 2808bfebd1cdSMike Snitzer }; 2809bfebd1cdSMike Snitzer 2810bfebd1cdSMike Snitzer static int dm_init_request_based_blk_mq_queue(struct mapped_device *md) 2811bfebd1cdSMike Snitzer { 281202233342SMike Snitzer unsigned md_type = dm_get_md_type(md); 2813bfebd1cdSMike Snitzer struct request_queue *q; 2814bfebd1cdSMike Snitzer int err; 2815bfebd1cdSMike Snitzer 2816bfebd1cdSMike Snitzer memset(&md->tag_set, 0, sizeof(md->tag_set)); 2817bfebd1cdSMike Snitzer md->tag_set.ops = &dm_mq_ops; 2818bfebd1cdSMike Snitzer md->tag_set.queue_depth = BLKDEV_MAX_RQ; 2819bfebd1cdSMike Snitzer md->tag_set.numa_node = NUMA_NO_NODE; 2820bfebd1cdSMike Snitzer md->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE; 2821bfebd1cdSMike Snitzer md->tag_set.nr_hw_queues = 1; 282202233342SMike Snitzer if (md_type == DM_TYPE_REQUEST_BASED) { 282302233342SMike Snitzer /* make the memory for non-blk-mq clone part of the pdu */ 282402233342SMike Snitzer md->tag_set.cmd_size = sizeof(struct dm_rq_target_io) + sizeof(struct request); 282502233342SMike Snitzer } else 2826bfebd1cdSMike Snitzer md->tag_set.cmd_size = sizeof(struct dm_rq_target_io); 2827bfebd1cdSMike Snitzer md->tag_set.driver_data = md; 2828bfebd1cdSMike Snitzer 2829bfebd1cdSMike Snitzer err = blk_mq_alloc_tag_set(&md->tag_set); 2830bfebd1cdSMike Snitzer if (err) 2831bfebd1cdSMike Snitzer return err; 2832bfebd1cdSMike Snitzer 2833bfebd1cdSMike Snitzer q = blk_mq_init_allocated_queue(&md->tag_set, md->queue); 2834bfebd1cdSMike Snitzer if (IS_ERR(q)) { 2835bfebd1cdSMike Snitzer err = PTR_ERR(q); 2836bfebd1cdSMike Snitzer goto out_tag_set; 2837bfebd1cdSMike Snitzer } 2838bfebd1cdSMike Snitzer md->queue = q; 2839bfebd1cdSMike Snitzer dm_init_md_queue(md); 2840bfebd1cdSMike Snitzer 2841bfebd1cdSMike Snitzer /* backfill 'mq' sysfs registration normally done in blk_register_queue */ 2842bfebd1cdSMike Snitzer blk_mq_register_disk(md->disk); 2843bfebd1cdSMike Snitzer 284402233342SMike Snitzer if (md_type == DM_TYPE_REQUEST_BASED) 2845bfebd1cdSMike Snitzer init_rq_based_worker_thread(md); 2846bfebd1cdSMike Snitzer 2847bfebd1cdSMike Snitzer return 0; 2848bfebd1cdSMike Snitzer 2849bfebd1cdSMike Snitzer out_tag_set: 2850bfebd1cdSMike Snitzer blk_mq_free_tag_set(&md->tag_set); 2851bfebd1cdSMike Snitzer return err; 28524a0b4ddfSMike Snitzer } 28534a0b4ddfSMike Snitzer 28544e6e36c3SMike Snitzer static unsigned filter_md_type(unsigned type, struct mapped_device *md) 28554e6e36c3SMike Snitzer { 28564e6e36c3SMike Snitzer if (type == DM_TYPE_BIO_BASED) 28574e6e36c3SMike Snitzer return type; 28584e6e36c3SMike Snitzer 28594e6e36c3SMike Snitzer return !md->use_blk_mq ? DM_TYPE_REQUEST_BASED : DM_TYPE_MQ_REQUEST_BASED; 28604e6e36c3SMike Snitzer } 28614e6e36c3SMike Snitzer 28624a0b4ddfSMike Snitzer /* 28634a0b4ddfSMike Snitzer * Setup the DM device's queue based on md's type 28644a0b4ddfSMike Snitzer */ 28654a0b4ddfSMike Snitzer int dm_setup_md_queue(struct mapped_device *md) 28664a0b4ddfSMike Snitzer { 2867bfebd1cdSMike Snitzer int r; 286817e149b8SMike Snitzer unsigned md_type = filter_md_type(dm_get_md_type(md), md); 2869bfebd1cdSMike Snitzer 2870bfebd1cdSMike Snitzer switch (md_type) { 2871bfebd1cdSMike Snitzer case DM_TYPE_REQUEST_BASED: 2872bfebd1cdSMike Snitzer r = dm_init_request_based_queue(md); 2873bfebd1cdSMike Snitzer if (r) { 28744a0b4ddfSMike Snitzer DMWARN("Cannot initialize queue for request-based mapped device"); 2875bfebd1cdSMike Snitzer return r; 28764a0b4ddfSMike Snitzer } 2877bfebd1cdSMike Snitzer break; 2878bfebd1cdSMike Snitzer case DM_TYPE_MQ_REQUEST_BASED: 2879bfebd1cdSMike Snitzer r = dm_init_request_based_blk_mq_queue(md); 2880bfebd1cdSMike Snitzer if (r) { 2881bfebd1cdSMike Snitzer DMWARN("Cannot initialize queue for request-based blk-mq mapped device"); 2882bfebd1cdSMike Snitzer return r; 2883bfebd1cdSMike Snitzer } 2884bfebd1cdSMike Snitzer break; 2885bfebd1cdSMike Snitzer case DM_TYPE_BIO_BASED: 2886bfebd1cdSMike Snitzer dm_init_old_md_queue(md); 2887ff36ab34SMike Snitzer blk_queue_make_request(md->queue, dm_make_request); 2888ff36ab34SMike Snitzer blk_queue_merge_bvec(md->queue, dm_merge_bvec); 2889bfebd1cdSMike Snitzer break; 2890ff36ab34SMike Snitzer } 28914a0b4ddfSMike Snitzer 28924a0b4ddfSMike Snitzer return 0; 28934a0b4ddfSMike Snitzer } 28944a0b4ddfSMike Snitzer 28952bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev) 28961da177e4SLinus Torvalds { 28971da177e4SLinus Torvalds struct mapped_device *md; 28981da177e4SLinus Torvalds unsigned minor = MINOR(dev); 28991da177e4SLinus Torvalds 29001da177e4SLinus Torvalds if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 29011da177e4SLinus Torvalds return NULL; 29021da177e4SLinus Torvalds 2903f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 29041da177e4SLinus Torvalds 29051da177e4SLinus Torvalds md = idr_find(&_minor_idr, minor); 29062bec1f4aSMikulas Patocka if (md) { 29072bec1f4aSMikulas Patocka if ((md == MINOR_ALLOCED || 2908f331c029STejun Heo (MINOR(disk_devt(dm_disk(md))) != minor) || 2909abdc568bSKiyoshi Ueda dm_deleting_md(md) || 2910fba9f90eSJeff Mahoney test_bit(DMF_FREEING, &md->flags))) { 2911637842cfSDavid Teigland md = NULL; 2912fba9f90eSJeff Mahoney goto out; 2913fba9f90eSJeff Mahoney } 29142bec1f4aSMikulas Patocka dm_get(md); 29152bec1f4aSMikulas Patocka } 29161da177e4SLinus Torvalds 2917fba9f90eSJeff Mahoney out: 2918f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 29191da177e4SLinus Torvalds 2920637842cfSDavid Teigland return md; 2921637842cfSDavid Teigland } 29223cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md); 2923d229a958SDavid Teigland 29249ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md) 2925637842cfSDavid Teigland { 29269ade92a9SAlasdair G Kergon return md->interface_ptr; 29271da177e4SLinus Torvalds } 29281da177e4SLinus Torvalds 29291da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr) 29301da177e4SLinus Torvalds { 29311da177e4SLinus Torvalds md->interface_ptr = ptr; 29321da177e4SLinus Torvalds } 29331da177e4SLinus Torvalds 29341da177e4SLinus Torvalds void dm_get(struct mapped_device *md) 29351da177e4SLinus Torvalds { 29361da177e4SLinus Torvalds atomic_inc(&md->holders); 29373f77316dSKiyoshi Ueda BUG_ON(test_bit(DMF_FREEING, &md->flags)); 29381da177e4SLinus Torvalds } 29391da177e4SLinus Torvalds 294009ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md) 294109ee96b2SMikulas Patocka { 294209ee96b2SMikulas Patocka spin_lock(&_minor_lock); 294309ee96b2SMikulas Patocka if (test_bit(DMF_FREEING, &md->flags)) { 294409ee96b2SMikulas Patocka spin_unlock(&_minor_lock); 294509ee96b2SMikulas Patocka return -EBUSY; 294609ee96b2SMikulas Patocka } 294709ee96b2SMikulas Patocka dm_get(md); 294809ee96b2SMikulas Patocka spin_unlock(&_minor_lock); 294909ee96b2SMikulas Patocka return 0; 295009ee96b2SMikulas Patocka } 295109ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold); 295209ee96b2SMikulas Patocka 295372d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md) 295472d94861SAlasdair G Kergon { 295572d94861SAlasdair G Kergon return md->name; 295672d94861SAlasdair G Kergon } 295772d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name); 295872d94861SAlasdair G Kergon 29593f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait) 29601da177e4SLinus Torvalds { 29611134e5aeSMike Anderson struct dm_table *map; 296283d5e5b0SMikulas Patocka int srcu_idx; 29631da177e4SLinus Torvalds 29643f77316dSKiyoshi Ueda might_sleep(); 2965fba9f90eSJeff Mahoney 296683d5e5b0SMikulas Patocka map = dm_get_live_table(md, &srcu_idx); 296763a4f065SMike Snitzer 296863a4f065SMike Snitzer spin_lock(&_minor_lock); 29693f77316dSKiyoshi Ueda idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md)))); 2970fba9f90eSJeff Mahoney set_bit(DMF_FREEING, &md->flags); 2971f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 29723f77316dSKiyoshi Ueda 297302233342SMike Snitzer if (dm_request_based(md) && md->kworker_task) 29742eb6e1e3SKeith Busch flush_kthread_worker(&md->kworker); 29752eb6e1e3SKeith Busch 2976ab7c7bb6SMikulas Patocka /* 2977ab7c7bb6SMikulas Patocka * Take suspend_lock so that presuspend and postsuspend methods 2978ab7c7bb6SMikulas Patocka * do not race with internal suspend. 2979ab7c7bb6SMikulas Patocka */ 2980ab7c7bb6SMikulas Patocka mutex_lock(&md->suspend_lock); 29814f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) { 29821da177e4SLinus Torvalds dm_table_presuspend_targets(map); 29831da177e4SLinus Torvalds dm_table_postsuspend_targets(map); 29841da177e4SLinus Torvalds } 2985ab7c7bb6SMikulas Patocka mutex_unlock(&md->suspend_lock); 29863f77316dSKiyoshi Ueda 298783d5e5b0SMikulas Patocka /* dm_put_live_table must be before msleep, otherwise deadlock is possible */ 298883d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 298983d5e5b0SMikulas Patocka 29903f77316dSKiyoshi Ueda /* 29913f77316dSKiyoshi Ueda * Rare, but there may be I/O requests still going to complete, 29923f77316dSKiyoshi Ueda * for example. Wait for all references to disappear. 29933f77316dSKiyoshi Ueda * No one should increment the reference count of the mapped_device, 29943f77316dSKiyoshi Ueda * after the mapped_device state becomes DMF_FREEING. 29953f77316dSKiyoshi Ueda */ 29963f77316dSKiyoshi Ueda if (wait) 29973f77316dSKiyoshi Ueda while (atomic_read(&md->holders)) 29983f77316dSKiyoshi Ueda msleep(1); 29993f77316dSKiyoshi Ueda else if (atomic_read(&md->holders)) 30003f77316dSKiyoshi Ueda DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)", 30013f77316dSKiyoshi Ueda dm_device_name(md), atomic_read(&md->holders)); 30023f77316dSKiyoshi Ueda 3003784aae73SMilan Broz dm_sysfs_exit(md); 3004a7940155SAlasdair G Kergon dm_table_destroy(__unbind(md)); 30051da177e4SLinus Torvalds free_dev(md); 30061da177e4SLinus Torvalds } 30073f77316dSKiyoshi Ueda 30083f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md) 30093f77316dSKiyoshi Ueda { 30103f77316dSKiyoshi Ueda __dm_destroy(md, true); 30113f77316dSKiyoshi Ueda } 30123f77316dSKiyoshi Ueda 30133f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md) 30143f77316dSKiyoshi Ueda { 30153f77316dSKiyoshi Ueda __dm_destroy(md, false); 30163f77316dSKiyoshi Ueda } 30173f77316dSKiyoshi Ueda 30183f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md) 30193f77316dSKiyoshi Ueda { 30203f77316dSKiyoshi Ueda atomic_dec(&md->holders); 30211da177e4SLinus Torvalds } 302279eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put); 30231da177e4SLinus Torvalds 3024401600dfSMikulas Patocka static int dm_wait_for_completion(struct mapped_device *md, int interruptible) 302546125c1cSMilan Broz { 302646125c1cSMilan Broz int r = 0; 3027b44ebeb0SMikulas Patocka DECLARE_WAITQUEUE(wait, current); 3028b44ebeb0SMikulas Patocka 3029b44ebeb0SMikulas Patocka add_wait_queue(&md->wait, &wait); 303046125c1cSMilan Broz 303146125c1cSMilan Broz while (1) { 3032401600dfSMikulas Patocka set_current_state(interruptible); 303346125c1cSMilan Broz 3034b4324feeSKiyoshi Ueda if (!md_in_flight(md)) 303546125c1cSMilan Broz break; 303646125c1cSMilan Broz 3037401600dfSMikulas Patocka if (interruptible == TASK_INTERRUPTIBLE && 3038401600dfSMikulas Patocka signal_pending(current)) { 303946125c1cSMilan Broz r = -EINTR; 304046125c1cSMilan Broz break; 304146125c1cSMilan Broz } 304246125c1cSMilan Broz 304346125c1cSMilan Broz io_schedule(); 304446125c1cSMilan Broz } 304546125c1cSMilan Broz set_current_state(TASK_RUNNING); 304646125c1cSMilan Broz 3047b44ebeb0SMikulas Patocka remove_wait_queue(&md->wait, &wait); 3048b44ebeb0SMikulas Patocka 304946125c1cSMilan Broz return r; 305046125c1cSMilan Broz } 305146125c1cSMilan Broz 30521da177e4SLinus Torvalds /* 30531da177e4SLinus Torvalds * Process the deferred bios 30541da177e4SLinus Torvalds */ 3055ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work) 30561da177e4SLinus Torvalds { 3057ef208587SMikulas Patocka struct mapped_device *md = container_of(work, struct mapped_device, 3058ef208587SMikulas Patocka work); 30596d6f10dfSMilan Broz struct bio *c; 306083d5e5b0SMikulas Patocka int srcu_idx; 306183d5e5b0SMikulas Patocka struct dm_table *map; 30621da177e4SLinus Torvalds 306383d5e5b0SMikulas Patocka map = dm_get_live_table(md, &srcu_idx); 3064ef208587SMikulas Patocka 30653b00b203SMikulas Patocka while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 3066022c2611SMikulas Patocka spin_lock_irq(&md->deferred_lock); 3067022c2611SMikulas Patocka c = bio_list_pop(&md->deferred); 3068022c2611SMikulas Patocka spin_unlock_irq(&md->deferred_lock); 3069022c2611SMikulas Patocka 30706a8736d1STejun Heo if (!c) 3071df12ee99SAlasdair G Kergon break; 307273d410c0SMilan Broz 3073e6ee8c0bSKiyoshi Ueda if (dm_request_based(md)) 3074e6ee8c0bSKiyoshi Ueda generic_make_request(c); 3075af7e466aSMikulas Patocka else 307683d5e5b0SMikulas Patocka __split_and_process_bio(md, map, c); 3077e6ee8c0bSKiyoshi Ueda } 30783b00b203SMikulas Patocka 307983d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 30801da177e4SLinus Torvalds } 30811da177e4SLinus Torvalds 30829a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md) 3083304f3f6aSMilan Broz { 30843b00b203SMikulas Patocka clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 30854e857c58SPeter Zijlstra smp_mb__after_atomic(); 308653d5914fSMikulas Patocka queue_work(md->wq, &md->work); 3087304f3f6aSMilan Broz } 3088304f3f6aSMilan Broz 30891da177e4SLinus Torvalds /* 3090042d2a9bSAlasdair G Kergon * Swap in a new table, returning the old one for the caller to destroy. 30911da177e4SLinus Torvalds */ 3092042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table) 30931da177e4SLinus Torvalds { 309487eb5b21SMike Christie struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL); 3095754c5fc7SMike Snitzer struct queue_limits limits; 3096042d2a9bSAlasdair G Kergon int r; 30971da177e4SLinus Torvalds 3098e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 30991da177e4SLinus Torvalds 31001da177e4SLinus Torvalds /* device must be suspended */ 31014f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) 310293c534aeSAlasdair G Kergon goto out; 31031da177e4SLinus Torvalds 31043ae70656SMike Snitzer /* 31053ae70656SMike Snitzer * If the new table has no data devices, retain the existing limits. 31063ae70656SMike Snitzer * This helps multipath with queue_if_no_path if all paths disappear, 31073ae70656SMike Snitzer * then new I/O is queued based on these limits, and then some paths 31083ae70656SMike Snitzer * reappear. 31093ae70656SMike Snitzer */ 31103ae70656SMike Snitzer if (dm_table_has_no_data_devices(table)) { 311183d5e5b0SMikulas Patocka live_map = dm_get_live_table_fast(md); 31123ae70656SMike Snitzer if (live_map) 31133ae70656SMike Snitzer limits = md->queue->limits; 311483d5e5b0SMikulas Patocka dm_put_live_table_fast(md); 31153ae70656SMike Snitzer } 31163ae70656SMike Snitzer 311787eb5b21SMike Christie if (!live_map) { 3118754c5fc7SMike Snitzer r = dm_calculate_queue_limits(table, &limits); 3119042d2a9bSAlasdair G Kergon if (r) { 3120042d2a9bSAlasdair G Kergon map = ERR_PTR(r); 3121754c5fc7SMike Snitzer goto out; 3122042d2a9bSAlasdair G Kergon } 312387eb5b21SMike Christie } 3124754c5fc7SMike Snitzer 3125042d2a9bSAlasdair G Kergon map = __bind(md, table, &limits); 31261da177e4SLinus Torvalds 312793c534aeSAlasdair G Kergon out: 3128e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 3129042d2a9bSAlasdair G Kergon return map; 31301da177e4SLinus Torvalds } 31311da177e4SLinus Torvalds 31321da177e4SLinus Torvalds /* 31331da177e4SLinus Torvalds * Functions to lock and unlock any filesystem running on the 31341da177e4SLinus Torvalds * device. 31351da177e4SLinus Torvalds */ 31362ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md) 31371da177e4SLinus Torvalds { 3138e39e2e95SAlasdair G Kergon int r; 31391da177e4SLinus Torvalds 31401da177e4SLinus Torvalds WARN_ON(md->frozen_sb); 3141dfbe03f6SAlasdair G Kergon 3142db8fef4fSMikulas Patocka md->frozen_sb = freeze_bdev(md->bdev); 3143dfbe03f6SAlasdair G Kergon if (IS_ERR(md->frozen_sb)) { 3144cf222b37SAlasdair G Kergon r = PTR_ERR(md->frozen_sb); 3145e39e2e95SAlasdair G Kergon md->frozen_sb = NULL; 3146e39e2e95SAlasdair G Kergon return r; 3147dfbe03f6SAlasdair G Kergon } 3148dfbe03f6SAlasdair G Kergon 3149aa8d7c2fSAlasdair G Kergon set_bit(DMF_FROZEN, &md->flags); 3150aa8d7c2fSAlasdair G Kergon 31511da177e4SLinus Torvalds return 0; 31521da177e4SLinus Torvalds } 31531da177e4SLinus Torvalds 31542ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md) 31551da177e4SLinus Torvalds { 3156aa8d7c2fSAlasdair G Kergon if (!test_bit(DMF_FROZEN, &md->flags)) 3157aa8d7c2fSAlasdair G Kergon return; 3158aa8d7c2fSAlasdair G Kergon 3159db8fef4fSMikulas Patocka thaw_bdev(md->bdev, md->frozen_sb); 31601da177e4SLinus Torvalds md->frozen_sb = NULL; 3161aa8d7c2fSAlasdair G Kergon clear_bit(DMF_FROZEN, &md->flags); 31621da177e4SLinus Torvalds } 31631da177e4SLinus Torvalds 31641da177e4SLinus Torvalds /* 3165ffcc3936SMike Snitzer * If __dm_suspend returns 0, the device is completely quiescent 3166ffcc3936SMike Snitzer * now. There is no request-processing activity. All new requests 3167ffcc3936SMike Snitzer * are being added to md->deferred list. 3168cec47e3dSKiyoshi Ueda * 3169ffcc3936SMike Snitzer * Caller must hold md->suspend_lock 3170cec47e3dSKiyoshi Ueda */ 3171ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map, 3172ffcc3936SMike Snitzer unsigned suspend_flags, int interruptible) 31731da177e4SLinus Torvalds { 3174ffcc3936SMike Snitzer bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG; 3175ffcc3936SMike Snitzer bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG; 3176ffcc3936SMike Snitzer int r; 3177cf222b37SAlasdair G Kergon 31782e93ccc1SKiyoshi Ueda /* 31792e93ccc1SKiyoshi Ueda * DMF_NOFLUSH_SUSPENDING must be set before presuspend. 31802e93ccc1SKiyoshi Ueda * This flag is cleared before dm_suspend returns. 31812e93ccc1SKiyoshi Ueda */ 31822e93ccc1SKiyoshi Ueda if (noflush) 31832e93ccc1SKiyoshi Ueda set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 31842e93ccc1SKiyoshi Ueda 3185d67ee213SMike Snitzer /* 3186d67ee213SMike Snitzer * This gets reverted if there's an error later and the targets 3187d67ee213SMike Snitzer * provide the .presuspend_undo hook. 3188d67ee213SMike Snitzer */ 31891da177e4SLinus Torvalds dm_table_presuspend_targets(map); 31901da177e4SLinus Torvalds 31912e93ccc1SKiyoshi Ueda /* 31929f518b27SKiyoshi Ueda * Flush I/O to the device. 31939f518b27SKiyoshi Ueda * Any I/O submitted after lock_fs() may not be flushed. 31949f518b27SKiyoshi Ueda * noflush takes precedence over do_lockfs. 31959f518b27SKiyoshi Ueda * (lock_fs() flushes I/Os and waits for them to complete.) 31962e93ccc1SKiyoshi Ueda */ 319732a926daSMikulas Patocka if (!noflush && do_lockfs) { 31982ca3310eSAlasdair G Kergon r = lock_fs(md); 3199d67ee213SMike Snitzer if (r) { 3200d67ee213SMike Snitzer dm_table_presuspend_undo_targets(map); 3201ffcc3936SMike Snitzer return r; 3202aa8d7c2fSAlasdair G Kergon } 3203d67ee213SMike Snitzer } 32041da177e4SLinus Torvalds 32051da177e4SLinus Torvalds /* 32063b00b203SMikulas Patocka * Here we must make sure that no processes are submitting requests 32073b00b203SMikulas Patocka * to target drivers i.e. no one may be executing 32083b00b203SMikulas Patocka * __split_and_process_bio. This is called from dm_request and 32093b00b203SMikulas Patocka * dm_wq_work. 32103b00b203SMikulas Patocka * 32113b00b203SMikulas Patocka * To get all processes out of __split_and_process_bio in dm_request, 32123b00b203SMikulas Patocka * we take the write lock. To prevent any process from reentering 32136a8736d1STejun Heo * __split_and_process_bio from dm_request and quiesce the thread 32146a8736d1STejun Heo * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call 32156a8736d1STejun Heo * flush_workqueue(md->wq). 32161da177e4SLinus Torvalds */ 32171eb787ecSAlasdair G Kergon set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 321841abc4e1SHannes Reinecke if (map) 321983d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 32201da177e4SLinus Torvalds 3221d0bcb878SKiyoshi Ueda /* 322229e4013dSTejun Heo * Stop md->queue before flushing md->wq in case request-based 322329e4013dSTejun Heo * dm defers requests to md->wq from md->queue. 3224d0bcb878SKiyoshi Ueda */ 32252eb6e1e3SKeith Busch if (dm_request_based(md)) { 32269f518b27SKiyoshi Ueda stop_queue(md->queue); 322702233342SMike Snitzer if (md->kworker_task) 32282eb6e1e3SKeith Busch flush_kthread_worker(&md->kworker); 32292eb6e1e3SKeith Busch } 3230cec47e3dSKiyoshi Ueda 3231d0bcb878SKiyoshi Ueda flush_workqueue(md->wq); 3232d0bcb878SKiyoshi Ueda 32331da177e4SLinus Torvalds /* 32343b00b203SMikulas Patocka * At this point no more requests are entering target request routines. 32353b00b203SMikulas Patocka * We call dm_wait_for_completion to wait for all existing requests 32363b00b203SMikulas Patocka * to finish. 32371da177e4SLinus Torvalds */ 3238ffcc3936SMike Snitzer r = dm_wait_for_completion(md, interruptible); 32391da177e4SLinus Torvalds 32406d6f10dfSMilan Broz if (noflush) 3241022c2611SMikulas Patocka clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 324241abc4e1SHannes Reinecke if (map) 324383d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 32442e93ccc1SKiyoshi Ueda 32451da177e4SLinus Torvalds /* were we interrupted ? */ 324646125c1cSMilan Broz if (r < 0) { 32479a1fb464SMikulas Patocka dm_queue_flush(md); 324873d410c0SMilan Broz 3249cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 32509f518b27SKiyoshi Ueda start_queue(md->queue); 3251cec47e3dSKiyoshi Ueda 32522ca3310eSAlasdair G Kergon unlock_fs(md); 3253d67ee213SMike Snitzer dm_table_presuspend_undo_targets(map); 3254ffcc3936SMike Snitzer /* pushback list is already flushed, so skip flush */ 3255ffcc3936SMike Snitzer } 3256ffcc3936SMike Snitzer 3257ffcc3936SMike Snitzer return r; 32582ca3310eSAlasdair G Kergon } 32592ca3310eSAlasdair G Kergon 32603b00b203SMikulas Patocka /* 3261ffcc3936SMike Snitzer * We need to be able to change a mapping table under a mounted 3262ffcc3936SMike Snitzer * filesystem. For example we might want to move some data in 3263ffcc3936SMike Snitzer * the background. Before the table can be swapped with 3264ffcc3936SMike Snitzer * dm_bind_table, dm_suspend must be called to flush any in 3265ffcc3936SMike Snitzer * flight bios and ensure that any further io gets deferred. 32663b00b203SMikulas Patocka */ 3267ffcc3936SMike Snitzer /* 3268ffcc3936SMike Snitzer * Suspend mechanism in request-based dm. 3269ffcc3936SMike Snitzer * 3270ffcc3936SMike Snitzer * 1. Flush all I/Os by lock_fs() if needed. 3271ffcc3936SMike Snitzer * 2. Stop dispatching any I/O by stopping the request_queue. 3272ffcc3936SMike Snitzer * 3. Wait for all in-flight I/Os to be completed or requeued. 3273ffcc3936SMike Snitzer * 3274ffcc3936SMike Snitzer * To abort suspend, start the request_queue. 3275ffcc3936SMike Snitzer */ 3276ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags) 3277ffcc3936SMike Snitzer { 3278ffcc3936SMike Snitzer struct dm_table *map = NULL; 3279ffcc3936SMike Snitzer int r = 0; 3280ffcc3936SMike Snitzer 3281ffcc3936SMike Snitzer retry: 3282ffcc3936SMike Snitzer mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); 3283ffcc3936SMike Snitzer 3284ffcc3936SMike Snitzer if (dm_suspended_md(md)) { 3285ffcc3936SMike Snitzer r = -EINVAL; 3286ffcc3936SMike Snitzer goto out_unlock; 3287ffcc3936SMike Snitzer } 3288ffcc3936SMike Snitzer 3289ffcc3936SMike Snitzer if (dm_suspended_internally_md(md)) { 3290ffcc3936SMike Snitzer /* already internally suspended, wait for internal resume */ 3291ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 3292ffcc3936SMike Snitzer r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); 3293ffcc3936SMike Snitzer if (r) 3294ffcc3936SMike Snitzer return r; 3295ffcc3936SMike Snitzer goto retry; 3296ffcc3936SMike Snitzer } 3297ffcc3936SMike Snitzer 3298a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 3299ffcc3936SMike Snitzer 3300ffcc3936SMike Snitzer r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE); 3301ffcc3936SMike Snitzer if (r) 3302ffcc3936SMike Snitzer goto out_unlock; 33033b00b203SMikulas Patocka 33041da177e4SLinus Torvalds set_bit(DMF_SUSPENDED, &md->flags); 33051da177e4SLinus Torvalds 33064d4471cbSKiyoshi Ueda dm_table_postsuspend_targets(map); 33074d4471cbSKiyoshi Ueda 3308d287483dSAlasdair G Kergon out_unlock: 3309e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 3310cf222b37SAlasdair G Kergon return r; 33111da177e4SLinus Torvalds } 33121da177e4SLinus Torvalds 3313ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map) 33141da177e4SLinus Torvalds { 3315ffcc3936SMike Snitzer if (map) { 3316ffcc3936SMike Snitzer int r = dm_table_resume_targets(map); 33178757b776SMilan Broz if (r) 3318ffcc3936SMike Snitzer return r; 3319ffcc3936SMike Snitzer } 33202ca3310eSAlasdair G Kergon 33219a1fb464SMikulas Patocka dm_queue_flush(md); 33222ca3310eSAlasdair G Kergon 3323cec47e3dSKiyoshi Ueda /* 3324cec47e3dSKiyoshi Ueda * Flushing deferred I/Os must be done after targets are resumed 3325cec47e3dSKiyoshi Ueda * so that mapping of targets can work correctly. 3326cec47e3dSKiyoshi Ueda * Request-based dm is queueing the deferred I/Os in its request_queue. 3327cec47e3dSKiyoshi Ueda */ 3328cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 3329cec47e3dSKiyoshi Ueda start_queue(md->queue); 3330cec47e3dSKiyoshi Ueda 33312ca3310eSAlasdair G Kergon unlock_fs(md); 33322ca3310eSAlasdair G Kergon 3333ffcc3936SMike Snitzer return 0; 3334ffcc3936SMike Snitzer } 3335ffcc3936SMike Snitzer 3336ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md) 3337ffcc3936SMike Snitzer { 3338ffcc3936SMike Snitzer int r = -EINVAL; 3339ffcc3936SMike Snitzer struct dm_table *map = NULL; 3340ffcc3936SMike Snitzer 3341ffcc3936SMike Snitzer retry: 3342ffcc3936SMike Snitzer mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); 3343ffcc3936SMike Snitzer 3344ffcc3936SMike Snitzer if (!dm_suspended_md(md)) 3345ffcc3936SMike Snitzer goto out; 3346ffcc3936SMike Snitzer 3347ffcc3936SMike Snitzer if (dm_suspended_internally_md(md)) { 3348ffcc3936SMike Snitzer /* already internally suspended, wait for internal resume */ 3349ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 3350ffcc3936SMike Snitzer r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); 3351ffcc3936SMike Snitzer if (r) 3352ffcc3936SMike Snitzer return r; 3353ffcc3936SMike Snitzer goto retry; 3354ffcc3936SMike Snitzer } 3355ffcc3936SMike Snitzer 3356a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 3357ffcc3936SMike Snitzer if (!map || !dm_table_get_size(map)) 3358ffcc3936SMike Snitzer goto out; 3359ffcc3936SMike Snitzer 3360ffcc3936SMike Snitzer r = __dm_resume(md, map); 3361ffcc3936SMike Snitzer if (r) 3362ffcc3936SMike Snitzer goto out; 3363ffcc3936SMike Snitzer 33642ca3310eSAlasdair G Kergon clear_bit(DMF_SUSPENDED, &md->flags); 33652ca3310eSAlasdair G Kergon 3366cf222b37SAlasdair G Kergon r = 0; 3367cf222b37SAlasdair G Kergon out: 3368e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 33692ca3310eSAlasdair G Kergon 3370cf222b37SAlasdair G Kergon return r; 33711da177e4SLinus Torvalds } 33721da177e4SLinus Torvalds 3373fd2ed4d2SMikulas Patocka /* 3374fd2ed4d2SMikulas Patocka * Internal suspend/resume works like userspace-driven suspend. It waits 3375fd2ed4d2SMikulas Patocka * until all bios finish and prevents issuing new bios to the target drivers. 3376fd2ed4d2SMikulas Patocka * It may be used only from the kernel. 3377fd2ed4d2SMikulas Patocka */ 3378fd2ed4d2SMikulas Patocka 3379ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags) 3380ffcc3936SMike Snitzer { 3381ffcc3936SMike Snitzer struct dm_table *map = NULL; 3382ffcc3936SMike Snitzer 338396b26c8cSMikulas Patocka if (md->internal_suspend_count++) 3384ffcc3936SMike Snitzer return; /* nested internal suspend */ 3385ffcc3936SMike Snitzer 3386ffcc3936SMike Snitzer if (dm_suspended_md(md)) { 3387ffcc3936SMike Snitzer set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 3388ffcc3936SMike Snitzer return; /* nest suspend */ 3389ffcc3936SMike Snitzer } 3390ffcc3936SMike Snitzer 3391a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 3392ffcc3936SMike Snitzer 3393ffcc3936SMike Snitzer /* 3394ffcc3936SMike Snitzer * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is 3395ffcc3936SMike Snitzer * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend 3396ffcc3936SMike Snitzer * would require changing .presuspend to return an error -- avoid this 3397ffcc3936SMike Snitzer * until there is a need for more elaborate variants of internal suspend. 3398ffcc3936SMike Snitzer */ 3399ffcc3936SMike Snitzer (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE); 3400ffcc3936SMike Snitzer 3401ffcc3936SMike Snitzer set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 3402ffcc3936SMike Snitzer 3403ffcc3936SMike Snitzer dm_table_postsuspend_targets(map); 3404ffcc3936SMike Snitzer } 3405ffcc3936SMike Snitzer 3406ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md) 3407ffcc3936SMike Snitzer { 340896b26c8cSMikulas Patocka BUG_ON(!md->internal_suspend_count); 340996b26c8cSMikulas Patocka 341096b26c8cSMikulas Patocka if (--md->internal_suspend_count) 3411ffcc3936SMike Snitzer return; /* resume from nested internal suspend */ 3412ffcc3936SMike Snitzer 3413ffcc3936SMike Snitzer if (dm_suspended_md(md)) 3414ffcc3936SMike Snitzer goto done; /* resume from nested suspend */ 3415ffcc3936SMike Snitzer 3416ffcc3936SMike Snitzer /* 3417ffcc3936SMike Snitzer * NOTE: existing callers don't need to call dm_table_resume_targets 3418ffcc3936SMike Snitzer * (which may fail -- so best to avoid it for now by passing NULL map) 3419ffcc3936SMike Snitzer */ 3420ffcc3936SMike Snitzer (void) __dm_resume(md, NULL); 3421ffcc3936SMike Snitzer 3422ffcc3936SMike Snitzer done: 3423ffcc3936SMike Snitzer clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 3424ffcc3936SMike Snitzer smp_mb__after_atomic(); 3425ffcc3936SMike Snitzer wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY); 3426ffcc3936SMike Snitzer } 3427ffcc3936SMike Snitzer 3428ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md) 3429fd2ed4d2SMikulas Patocka { 3430fd2ed4d2SMikulas Patocka mutex_lock(&md->suspend_lock); 3431ffcc3936SMike Snitzer __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG); 3432ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 3433ffcc3936SMike Snitzer } 3434ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush); 3435ffcc3936SMike Snitzer 3436ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md) 3437ffcc3936SMike Snitzer { 3438ffcc3936SMike Snitzer mutex_lock(&md->suspend_lock); 3439ffcc3936SMike Snitzer __dm_internal_resume(md); 3440ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 3441ffcc3936SMike Snitzer } 3442ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume); 3443ffcc3936SMike Snitzer 3444ffcc3936SMike Snitzer /* 3445ffcc3936SMike Snitzer * Fast variants of internal suspend/resume hold md->suspend_lock, 3446ffcc3936SMike Snitzer * which prevents interaction with userspace-driven suspend. 3447ffcc3936SMike Snitzer */ 3448ffcc3936SMike Snitzer 3449ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md) 3450ffcc3936SMike Snitzer { 3451ffcc3936SMike Snitzer mutex_lock(&md->suspend_lock); 3452ffcc3936SMike Snitzer if (dm_suspended_md(md) || dm_suspended_internally_md(md)) 3453fd2ed4d2SMikulas Patocka return; 3454fd2ed4d2SMikulas Patocka 3455fd2ed4d2SMikulas Patocka set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 3456fd2ed4d2SMikulas Patocka synchronize_srcu(&md->io_barrier); 3457fd2ed4d2SMikulas Patocka flush_workqueue(md->wq); 3458fd2ed4d2SMikulas Patocka dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); 3459fd2ed4d2SMikulas Patocka } 3460b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast); 3461fd2ed4d2SMikulas Patocka 3462ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md) 3463fd2ed4d2SMikulas Patocka { 3464ffcc3936SMike Snitzer if (dm_suspended_md(md) || dm_suspended_internally_md(md)) 3465fd2ed4d2SMikulas Patocka goto done; 3466fd2ed4d2SMikulas Patocka 3467fd2ed4d2SMikulas Patocka dm_queue_flush(md); 3468fd2ed4d2SMikulas Patocka 3469fd2ed4d2SMikulas Patocka done: 3470fd2ed4d2SMikulas Patocka mutex_unlock(&md->suspend_lock); 3471fd2ed4d2SMikulas Patocka } 3472b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast); 3473fd2ed4d2SMikulas Patocka 34741da177e4SLinus Torvalds /*----------------------------------------------------------------- 34751da177e4SLinus Torvalds * Event notification. 34761da177e4SLinus Torvalds *---------------------------------------------------------------*/ 34773abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action, 347860935eb2SMilan Broz unsigned cookie) 347969267a30SAlasdair G Kergon { 348060935eb2SMilan Broz char udev_cookie[DM_COOKIE_LENGTH]; 348160935eb2SMilan Broz char *envp[] = { udev_cookie, NULL }; 348260935eb2SMilan Broz 348360935eb2SMilan Broz if (!cookie) 34843abf85b5SPeter Rajnoha return kobject_uevent(&disk_to_dev(md->disk)->kobj, action); 348560935eb2SMilan Broz else { 348660935eb2SMilan Broz snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u", 348760935eb2SMilan Broz DM_COOKIE_ENV_VAR_NAME, cookie); 34883abf85b5SPeter Rajnoha return kobject_uevent_env(&disk_to_dev(md->disk)->kobj, 34893abf85b5SPeter Rajnoha action, envp); 349060935eb2SMilan Broz } 349169267a30SAlasdair G Kergon } 349269267a30SAlasdair G Kergon 34937a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md) 34947a8c3d3bSMike Anderson { 34957a8c3d3bSMike Anderson return atomic_add_return(1, &md->uevent_seq); 34967a8c3d3bSMike Anderson } 34977a8c3d3bSMike Anderson 34981da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md) 34991da177e4SLinus Torvalds { 35001da177e4SLinus Torvalds return atomic_read(&md->event_nr); 35011da177e4SLinus Torvalds } 35021da177e4SLinus Torvalds 35031da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr) 35041da177e4SLinus Torvalds { 35051da177e4SLinus Torvalds return wait_event_interruptible(md->eventq, 35061da177e4SLinus Torvalds (event_nr != atomic_read(&md->event_nr))); 35071da177e4SLinus Torvalds } 35081da177e4SLinus Torvalds 35097a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist) 35107a8c3d3bSMike Anderson { 35117a8c3d3bSMike Anderson unsigned long flags; 35127a8c3d3bSMike Anderson 35137a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 35147a8c3d3bSMike Anderson list_add(elist, &md->uevent_list); 35157a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 35167a8c3d3bSMike Anderson } 35177a8c3d3bSMike Anderson 35181da177e4SLinus Torvalds /* 35191da177e4SLinus Torvalds * The gendisk is only valid as long as you have a reference 35201da177e4SLinus Torvalds * count on 'md'. 35211da177e4SLinus Torvalds */ 35221da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md) 35231da177e4SLinus Torvalds { 35241da177e4SLinus Torvalds return md->disk; 35251da177e4SLinus Torvalds } 352665ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk); 35271da177e4SLinus Torvalds 3528784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md) 3529784aae73SMilan Broz { 35302995fa78SMikulas Patocka return &md->kobj_holder.kobj; 3531784aae73SMilan Broz } 3532784aae73SMilan Broz 3533784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj) 3534784aae73SMilan Broz { 3535784aae73SMilan Broz struct mapped_device *md; 3536784aae73SMilan Broz 35372995fa78SMikulas Patocka md = container_of(kobj, struct mapped_device, kobj_holder.kobj); 3538784aae73SMilan Broz 35394d89b7b4SMilan Broz if (test_bit(DMF_FREEING, &md->flags) || 3540432a212cSMike Anderson dm_deleting_md(md)) 35414d89b7b4SMilan Broz return NULL; 35424d89b7b4SMilan Broz 3543784aae73SMilan Broz dm_get(md); 3544784aae73SMilan Broz return md; 3545784aae73SMilan Broz } 3546784aae73SMilan Broz 35474f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md) 35481da177e4SLinus Torvalds { 35491da177e4SLinus Torvalds return test_bit(DMF_SUSPENDED, &md->flags); 35501da177e4SLinus Torvalds } 35511da177e4SLinus Torvalds 3552ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md) 3553ffcc3936SMike Snitzer { 3554ffcc3936SMike Snitzer return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 3555ffcc3936SMike Snitzer } 3556ffcc3936SMike Snitzer 35572c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md) 35582c140a24SMikulas Patocka { 35592c140a24SMikulas Patocka return test_bit(DMF_DEFERRED_REMOVE, &md->flags); 35602c140a24SMikulas Patocka } 35612c140a24SMikulas Patocka 356264dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti) 356364dbce58SKiyoshi Ueda { 3564ecdb2e25SKiyoshi Ueda return dm_suspended_md(dm_table_get_md(ti->table)); 356564dbce58SKiyoshi Ueda } 356664dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended); 356764dbce58SKiyoshi Ueda 35682e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti) 35692e93ccc1SKiyoshi Ueda { 3570ecdb2e25SKiyoshi Ueda return __noflush_suspending(dm_table_get_md(ti->table)); 35712e93ccc1SKiyoshi Ueda } 35722e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending); 35732e93ccc1SKiyoshi Ueda 3574*78d8e58aSMike Snitzer struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type, 3575*78d8e58aSMike Snitzer unsigned integrity, unsigned per_bio_data_size) 3576e6ee8c0bSKiyoshi Ueda { 3577*78d8e58aSMike Snitzer struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL); 3578*78d8e58aSMike Snitzer struct kmem_cache *cachep = NULL; 3579*78d8e58aSMike Snitzer unsigned int pool_size = 0; 35805f015204SJun'ichi Nomura unsigned int front_pad; 3581e6ee8c0bSKiyoshi Ueda 3582e6ee8c0bSKiyoshi Ueda if (!pools) 35834e6e36c3SMike Snitzer return NULL; 3584e6ee8c0bSKiyoshi Ueda 3585*78d8e58aSMike Snitzer type = filter_md_type(type, md); 358617e149b8SMike Snitzer 3587*78d8e58aSMike Snitzer switch (type) { 3588*78d8e58aSMike Snitzer case DM_TYPE_BIO_BASED: 3589*78d8e58aSMike Snitzer cachep = _io_cache; 3590*78d8e58aSMike Snitzer pool_size = dm_get_reserved_bio_based_ios(); 3591*78d8e58aSMike Snitzer front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone); 3592*78d8e58aSMike Snitzer break; 3593*78d8e58aSMike Snitzer case DM_TYPE_REQUEST_BASED: 3594*78d8e58aSMike Snitzer cachep = _rq_tio_cache; 3595*78d8e58aSMike Snitzer pool_size = dm_get_reserved_rq_based_ios(); 3596*78d8e58aSMike Snitzer pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache); 3597*78d8e58aSMike Snitzer if (!pools->rq_pool) 3598*78d8e58aSMike Snitzer goto out; 3599*78d8e58aSMike Snitzer /* fall through to setup remaining rq-based pools */ 3600*78d8e58aSMike Snitzer case DM_TYPE_MQ_REQUEST_BASED: 3601*78d8e58aSMike Snitzer if (!pool_size) 3602*78d8e58aSMike Snitzer pool_size = dm_get_reserved_rq_based_ios(); 3603*78d8e58aSMike Snitzer front_pad = offsetof(struct dm_rq_clone_bio_info, clone); 3604*78d8e58aSMike Snitzer /* per_bio_data_size is not used. See __bind_mempools(). */ 3605*78d8e58aSMike Snitzer WARN_ON(per_bio_data_size != 0); 3606*78d8e58aSMike Snitzer break; 3607*78d8e58aSMike Snitzer default: 3608*78d8e58aSMike Snitzer BUG(); 3609*78d8e58aSMike Snitzer } 3610*78d8e58aSMike Snitzer 3611*78d8e58aSMike Snitzer if (cachep) { 3612*78d8e58aSMike Snitzer pools->io_pool = mempool_create_slab_pool(pool_size, cachep); 3613e6ee8c0bSKiyoshi Ueda if (!pools->io_pool) 36145f015204SJun'ichi Nomura goto out; 3615*78d8e58aSMike Snitzer } 3616e6ee8c0bSKiyoshi Ueda 36173d8aab2dSJunichi Nomura pools->bs = bioset_create_nobvec(pool_size, front_pad); 3618e6ee8c0bSKiyoshi Ueda if (!pools->bs) 36195f015204SJun'ichi Nomura goto out; 3620e6ee8c0bSKiyoshi Ueda 3621a91a2785SMartin K. Petersen if (integrity && bioset_integrity_create(pools->bs, pool_size)) 36225f015204SJun'ichi Nomura goto out; 3623a91a2785SMartin K. Petersen 3624e6ee8c0bSKiyoshi Ueda return pools; 3625*78d8e58aSMike Snitzer 36265f015204SJun'ichi Nomura out: 36275f015204SJun'ichi Nomura dm_free_md_mempools(pools); 3628e6ee8c0bSKiyoshi Ueda 36294e6e36c3SMike Snitzer return NULL; 3630e6ee8c0bSKiyoshi Ueda } 3631e6ee8c0bSKiyoshi Ueda 3632e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools) 3633e6ee8c0bSKiyoshi Ueda { 3634e6ee8c0bSKiyoshi Ueda if (!pools) 3635e6ee8c0bSKiyoshi Ueda return; 3636e6ee8c0bSKiyoshi Ueda 3637e6ee8c0bSKiyoshi Ueda if (pools->io_pool) 3638e6ee8c0bSKiyoshi Ueda mempool_destroy(pools->io_pool); 3639e6ee8c0bSKiyoshi Ueda 36401ae49ea2SMike Snitzer if (pools->rq_pool) 36411ae49ea2SMike Snitzer mempool_destroy(pools->rq_pool); 36421ae49ea2SMike Snitzer 3643e6ee8c0bSKiyoshi Ueda if (pools->bs) 3644e6ee8c0bSKiyoshi Ueda bioset_free(pools->bs); 3645e6ee8c0bSKiyoshi Ueda 3646e6ee8c0bSKiyoshi Ueda kfree(pools); 3647e6ee8c0bSKiyoshi Ueda } 3648e6ee8c0bSKiyoshi Ueda 364983d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = { 36501da177e4SLinus Torvalds .open = dm_blk_open, 36511da177e4SLinus Torvalds .release = dm_blk_close, 3652aa129a22SMilan Broz .ioctl = dm_blk_ioctl, 36533ac51e74SDarrick J. Wong .getgeo = dm_blk_getgeo, 36541da177e4SLinus Torvalds .owner = THIS_MODULE 36551da177e4SLinus Torvalds }; 36561da177e4SLinus Torvalds 36571da177e4SLinus Torvalds /* 36581da177e4SLinus Torvalds * module hooks 36591da177e4SLinus Torvalds */ 36601da177e4SLinus Torvalds module_init(dm_init); 36611da177e4SLinus Torvalds module_exit(dm_exit); 36621da177e4SLinus Torvalds 36631da177e4SLinus Torvalds module_param(major, uint, 0); 36641da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper"); 3665f4790826SMike Snitzer 3666e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR); 3667e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools"); 3668e8603136SMike Snitzer 3669f4790826SMike Snitzer module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR); 3670f4790826SMike Snitzer MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools"); 3671f4790826SMike Snitzer 367217e149b8SMike Snitzer module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR); 367317e149b8SMike Snitzer MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices"); 367417e149b8SMike Snitzer 36751da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver"); 36761da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 36771da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 3678