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 84cc96131SMike Snitzer #include "dm-core.h" 94cc96131SMike Snitzer #include "dm-rq.h" 1051e5b2bdSMike Anderson #include "dm-uevent.h" 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #include <linux/init.h> 131da177e4SLinus Torvalds #include <linux/module.h> 1448c9c27bSArjan van de Ven #include <linux/mutex.h> 15174cd4b1SIngo Molnar #include <linux/sched/signal.h> 161da177e4SLinus Torvalds #include <linux/blkpg.h> 171da177e4SLinus Torvalds #include <linux/bio.h> 181da177e4SLinus Torvalds #include <linux/mempool.h> 19f26c5719SDan Williams #include <linux/dax.h> 201da177e4SLinus Torvalds #include <linux/slab.h> 211da177e4SLinus Torvalds #include <linux/idr.h> 227e026c8cSDan Williams #include <linux/uio.h> 233ac51e74SDarrick J. Wong #include <linux/hdreg.h> 243f77316dSKiyoshi Ueda #include <linux/delay.h> 25ffcc3936SMike Snitzer #include <linux/wait.h> 2671cdb697SChristoph Hellwig #include <linux/pr.h> 27b0b4d7c6SElena Reshetova #include <linux/refcount.h> 2855782138SLi Zefan 2972d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core" 3072d94861SAlasdair G Kergon 3160935eb2SMilan Broz /* 3260935eb2SMilan Broz * Cookies are numeric values sent with CHANGE and REMOVE 3360935eb2SMilan Broz * uevents while resuming, removing or renaming the device. 3460935eb2SMilan Broz */ 3560935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE" 3660935eb2SMilan Broz #define DM_COOKIE_LENGTH 24 3760935eb2SMilan Broz 381da177e4SLinus Torvalds static const char *_name = DM_NAME; 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds static unsigned int major = 0; 411da177e4SLinus Torvalds static unsigned int _major = 0; 421da177e4SLinus Torvalds 43d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr); 44d15b774cSAlasdair G Kergon 45f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock); 462c140a24SMikulas Patocka 472c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w); 482c140a24SMikulas Patocka 492c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove); 502c140a24SMikulas Patocka 51acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue; 52acfe0ad7SMikulas Patocka 5393e6442cSMikulas Patocka atomic_t dm_global_event_nr = ATOMIC_INIT(0); 5493e6442cSMikulas Patocka DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq); 5593e6442cSMikulas Patocka 5662e08243SMikulas Patocka void dm_issue_global_event(void) 5762e08243SMikulas Patocka { 5862e08243SMikulas Patocka atomic_inc(&dm_global_event_nr); 5962e08243SMikulas Patocka wake_up(&dm_global_eventq); 6062e08243SMikulas Patocka } 6162e08243SMikulas Patocka 621da177e4SLinus Torvalds /* 6364f52b0eSMike Snitzer * One of these is allocated (on-stack) per original bio. 641da177e4SLinus Torvalds */ 6564f52b0eSMike Snitzer struct clone_info { 6664f52b0eSMike Snitzer struct dm_table *map; 6764f52b0eSMike Snitzer struct bio *bio; 6864f52b0eSMike Snitzer struct dm_io *io; 6964f52b0eSMike Snitzer sector_t sector; 7064f52b0eSMike Snitzer unsigned sector_count; 7164f52b0eSMike Snitzer }; 7264f52b0eSMike Snitzer 7364f52b0eSMike Snitzer /* 7464f52b0eSMike Snitzer * One of these is allocated per clone bio. 7564f52b0eSMike Snitzer */ 7664f52b0eSMike Snitzer #define DM_TIO_MAGIC 7282014 7764f52b0eSMike Snitzer struct dm_target_io { 7864f52b0eSMike Snitzer unsigned magic; 7964f52b0eSMike Snitzer struct dm_io *io; 8064f52b0eSMike Snitzer struct dm_target *ti; 8164f52b0eSMike Snitzer unsigned target_bio_nr; 8264f52b0eSMike Snitzer unsigned *len_ptr; 8364f52b0eSMike Snitzer bool inside_dm_io; 8464f52b0eSMike Snitzer struct bio clone; 8564f52b0eSMike Snitzer }; 8664f52b0eSMike Snitzer 8764f52b0eSMike Snitzer /* 8864f52b0eSMike Snitzer * One of these is allocated per original bio. 8964f52b0eSMike Snitzer * It contains the first clone used for that original. 9064f52b0eSMike Snitzer */ 9164f52b0eSMike Snitzer #define DM_IO_MAGIC 5191977 921da177e4SLinus Torvalds struct dm_io { 9364f52b0eSMike Snitzer unsigned magic; 941da177e4SLinus Torvalds struct mapped_device *md; 954e4cbee9SChristoph Hellwig blk_status_t status; 961da177e4SLinus Torvalds atomic_t io_count; 97745dc570SMike Snitzer struct bio *orig_bio; 983eaf840eSJun'ichi "Nick" Nomura unsigned long start_time; 99f88fb981SKiyoshi Ueda spinlock_t endio_lock; 100fd2ed4d2SMikulas Patocka struct dm_stats_aux stats_aux; 10164f52b0eSMike Snitzer /* last member of dm_target_io is 'struct bio' */ 10264f52b0eSMike Snitzer struct dm_target_io tio; 1031da177e4SLinus Torvalds }; 1041da177e4SLinus Torvalds 10564f52b0eSMike Snitzer void *dm_per_bio_data(struct bio *bio, size_t data_size) 10664f52b0eSMike Snitzer { 10764f52b0eSMike Snitzer struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); 10864f52b0eSMike Snitzer if (!tio->inside_dm_io) 10964f52b0eSMike Snitzer return (char *)bio - offsetof(struct dm_target_io, clone) - data_size; 11064f52b0eSMike Snitzer return (char *)bio - offsetof(struct dm_target_io, clone) - offsetof(struct dm_io, tio) - data_size; 11164f52b0eSMike Snitzer } 11264f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_per_bio_data); 11364f52b0eSMike Snitzer 11464f52b0eSMike Snitzer struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size) 11564f52b0eSMike Snitzer { 11664f52b0eSMike Snitzer struct dm_io *io = (struct dm_io *)((char *)data + data_size); 11764f52b0eSMike Snitzer if (io->magic == DM_IO_MAGIC) 11864f52b0eSMike Snitzer return (struct bio *)((char *)io + offsetof(struct dm_io, tio) + offsetof(struct dm_target_io, clone)); 11964f52b0eSMike Snitzer BUG_ON(io->magic != DM_TIO_MAGIC); 12064f52b0eSMike Snitzer return (struct bio *)((char *)io + offsetof(struct dm_target_io, clone)); 12164f52b0eSMike Snitzer } 12264f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_from_per_bio_data); 12364f52b0eSMike Snitzer 12464f52b0eSMike Snitzer unsigned dm_bio_get_target_bio_nr(const struct bio *bio) 12564f52b0eSMike Snitzer { 12664f52b0eSMike Snitzer return container_of(bio, struct dm_target_io, clone)->target_bio_nr; 12764f52b0eSMike Snitzer } 12864f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_get_target_bio_nr); 12964f52b0eSMike Snitzer 130ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1) 131ba61fdd1SJeff Mahoney 1321da177e4SLinus Torvalds /* 1331da177e4SLinus Torvalds * Bits for the md->flags field. 1341da177e4SLinus Torvalds */ 1351eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0 1361da177e4SLinus Torvalds #define DMF_SUSPENDED 1 137aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2 138fba9f90eSJeff Mahoney #define DMF_FREEING 3 1395c6bd75dSAlasdair G Kergon #define DMF_DELETING 4 1402e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5 1418ae12666SKent Overstreet #define DMF_DEFERRED_REMOVE 6 1428ae12666SKent Overstreet #define DMF_SUSPENDED_INTERNALLY 7 1431da177e4SLinus Torvalds 144115485e8SMike Snitzer #define DM_NUMA_NODE NUMA_NO_NODE 145115485e8SMike Snitzer static int dm_numa_node = DM_NUMA_NODE; 146faad87dfSMike Snitzer 147e6ee8c0bSKiyoshi Ueda /* 148e6ee8c0bSKiyoshi Ueda * For mempools pre-allocation at the table loading time. 149e6ee8c0bSKiyoshi Ueda */ 150e6ee8c0bSKiyoshi Ueda struct dm_md_mempools { 1516f1c819cSKent Overstreet struct bio_set bs; 1526f1c819cSKent Overstreet struct bio_set io_bs; 153e6ee8c0bSKiyoshi Ueda }; 154e6ee8c0bSKiyoshi Ueda 15586f1152bSBenjamin Marzinski struct table_device { 15686f1152bSBenjamin Marzinski struct list_head list; 157b0b4d7c6SElena Reshetova refcount_t count; 15886f1152bSBenjamin Marzinski struct dm_dev dm_dev; 15986f1152bSBenjamin Marzinski }; 16086f1152bSBenjamin Marzinski 1618fbf26adSKiyoshi Ueda static struct kmem_cache *_rq_tio_cache; 1621ae49ea2SMike Snitzer static struct kmem_cache *_rq_cache; 16394818742SKent Overstreet 164f4790826SMike Snitzer /* 165e8603136SMike Snitzer * Bio-based DM's mempools' reserved IOs set by the user. 166e8603136SMike Snitzer */ 1674cc96131SMike Snitzer #define RESERVED_BIO_BASED_IOS 16 168e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS; 169e8603136SMike Snitzer 170115485e8SMike Snitzer static int __dm_get_module_param_int(int *module_param, int min, int max) 171115485e8SMike Snitzer { 1726aa7de05SMark Rutland int param = READ_ONCE(*module_param); 173115485e8SMike Snitzer int modified_param = 0; 174115485e8SMike Snitzer bool modified = true; 175115485e8SMike Snitzer 176115485e8SMike Snitzer if (param < min) 177115485e8SMike Snitzer modified_param = min; 178115485e8SMike Snitzer else if (param > max) 179115485e8SMike Snitzer modified_param = max; 180115485e8SMike Snitzer else 181115485e8SMike Snitzer modified = false; 182115485e8SMike Snitzer 183115485e8SMike Snitzer if (modified) { 184115485e8SMike Snitzer (void)cmpxchg(module_param, param, modified_param); 185115485e8SMike Snitzer param = modified_param; 186115485e8SMike Snitzer } 187115485e8SMike Snitzer 188115485e8SMike Snitzer return param; 189115485e8SMike Snitzer } 190115485e8SMike Snitzer 1914cc96131SMike Snitzer unsigned __dm_get_module_param(unsigned *module_param, 192f4790826SMike Snitzer unsigned def, unsigned max) 193f4790826SMike Snitzer { 1946aa7de05SMark Rutland unsigned param = READ_ONCE(*module_param); 19509c2d531SMike Snitzer unsigned modified_param = 0; 196f4790826SMike Snitzer 19709c2d531SMike Snitzer if (!param) 19809c2d531SMike Snitzer modified_param = def; 19909c2d531SMike Snitzer else if (param > max) 20009c2d531SMike Snitzer modified_param = max; 201f4790826SMike Snitzer 20209c2d531SMike Snitzer if (modified_param) { 20309c2d531SMike Snitzer (void)cmpxchg(module_param, param, modified_param); 20409c2d531SMike Snitzer param = modified_param; 205f4790826SMike Snitzer } 206f4790826SMike Snitzer 20709c2d531SMike Snitzer return param; 208f4790826SMike Snitzer } 209f4790826SMike Snitzer 210e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void) 211e8603136SMike Snitzer { 21209c2d531SMike Snitzer return __dm_get_module_param(&reserved_bio_based_ios, 2134cc96131SMike Snitzer RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS); 214e8603136SMike Snitzer } 215e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios); 216e8603136SMike Snitzer 217115485e8SMike Snitzer static unsigned dm_get_numa_node(void) 218115485e8SMike Snitzer { 219115485e8SMike Snitzer return __dm_get_module_param_int(&dm_numa_node, 220115485e8SMike Snitzer DM_NUMA_NODE, num_online_nodes() - 1); 221115485e8SMike Snitzer } 222115485e8SMike Snitzer 2231da177e4SLinus Torvalds static int __init local_init(void) 2241da177e4SLinus Torvalds { 22551157b4aSKiyoshi Ueda int r = -ENOMEM; 2261da177e4SLinus Torvalds 2278fbf26adSKiyoshi Ueda _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0); 2288fbf26adSKiyoshi Ueda if (!_rq_tio_cache) 229dde1e1ecSMike Snitzer return r; 2308fbf26adSKiyoshi Ueda 231eca7ee6dSMike Snitzer _rq_cache = kmem_cache_create("dm_old_clone_request", sizeof(struct request), 2321ae49ea2SMike Snitzer __alignof__(struct request), 0, NULL); 2331ae49ea2SMike Snitzer if (!_rq_cache) 2341ae49ea2SMike Snitzer goto out_free_rq_tio_cache; 2351ae49ea2SMike Snitzer 23651e5b2bdSMike Anderson r = dm_uevent_init(); 23751157b4aSKiyoshi Ueda if (r) 2381ae49ea2SMike Snitzer goto out_free_rq_cache; 23951e5b2bdSMike Anderson 240acfe0ad7SMikulas Patocka deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1); 241acfe0ad7SMikulas Patocka if (!deferred_remove_workqueue) { 242acfe0ad7SMikulas Patocka r = -ENOMEM; 243acfe0ad7SMikulas Patocka goto out_uevent_exit; 244acfe0ad7SMikulas Patocka } 245acfe0ad7SMikulas Patocka 2461da177e4SLinus Torvalds _major = major; 2471da177e4SLinus Torvalds r = register_blkdev(_major, _name); 24851157b4aSKiyoshi Ueda if (r < 0) 249acfe0ad7SMikulas Patocka goto out_free_workqueue; 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds if (!_major) 2521da177e4SLinus Torvalds _major = r; 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds return 0; 25551157b4aSKiyoshi Ueda 256acfe0ad7SMikulas Patocka out_free_workqueue: 257acfe0ad7SMikulas Patocka destroy_workqueue(deferred_remove_workqueue); 25851157b4aSKiyoshi Ueda out_uevent_exit: 25951157b4aSKiyoshi Ueda dm_uevent_exit(); 2601ae49ea2SMike Snitzer out_free_rq_cache: 2611ae49ea2SMike Snitzer kmem_cache_destroy(_rq_cache); 2628fbf26adSKiyoshi Ueda out_free_rq_tio_cache: 2638fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_tio_cache); 26451157b4aSKiyoshi Ueda 26551157b4aSKiyoshi Ueda return r; 2661da177e4SLinus Torvalds } 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds static void local_exit(void) 2691da177e4SLinus Torvalds { 2702c140a24SMikulas Patocka flush_scheduled_work(); 271acfe0ad7SMikulas Patocka destroy_workqueue(deferred_remove_workqueue); 2722c140a24SMikulas Patocka 2731ae49ea2SMike Snitzer kmem_cache_destroy(_rq_cache); 2748fbf26adSKiyoshi Ueda kmem_cache_destroy(_rq_tio_cache); 27500d59405SAkinobu Mita unregister_blkdev(_major, _name); 27651e5b2bdSMike Anderson dm_uevent_exit(); 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds _major = 0; 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds DMINFO("cleaned up"); 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds 283b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = { 2841da177e4SLinus Torvalds local_init, 2851da177e4SLinus Torvalds dm_target_init, 2861da177e4SLinus Torvalds dm_linear_init, 2871da177e4SLinus Torvalds dm_stripe_init, 288952b3557SMikulas Patocka dm_io_init, 289945fa4d2SMikulas Patocka dm_kcopyd_init, 2901da177e4SLinus Torvalds dm_interface_init, 291fd2ed4d2SMikulas Patocka dm_statistics_init, 2921da177e4SLinus Torvalds }; 2931da177e4SLinus Torvalds 294b9249e55SAlasdair G Kergon static void (*_exits[])(void) = { 2951da177e4SLinus Torvalds local_exit, 2961da177e4SLinus Torvalds dm_target_exit, 2971da177e4SLinus Torvalds dm_linear_exit, 2981da177e4SLinus Torvalds dm_stripe_exit, 299952b3557SMikulas Patocka dm_io_exit, 300945fa4d2SMikulas Patocka dm_kcopyd_exit, 3011da177e4SLinus Torvalds dm_interface_exit, 302fd2ed4d2SMikulas Patocka dm_statistics_exit, 3031da177e4SLinus Torvalds }; 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds static int __init dm_init(void) 3061da177e4SLinus Torvalds { 3071da177e4SLinus Torvalds const int count = ARRAY_SIZE(_inits); 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds int r, i; 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds for (i = 0; i < count; i++) { 3121da177e4SLinus Torvalds r = _inits[i](); 3131da177e4SLinus Torvalds if (r) 3141da177e4SLinus Torvalds goto bad; 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds return 0; 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds bad: 3201da177e4SLinus Torvalds while (i--) 3211da177e4SLinus Torvalds _exits[i](); 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds return r; 3241da177e4SLinus Torvalds } 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds static void __exit dm_exit(void) 3271da177e4SLinus Torvalds { 3281da177e4SLinus Torvalds int i = ARRAY_SIZE(_exits); 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds while (i--) 3311da177e4SLinus Torvalds _exits[i](); 332d15b774cSAlasdair G Kergon 333d15b774cSAlasdair G Kergon /* 334d15b774cSAlasdair G Kergon * Should be empty by this point. 335d15b774cSAlasdair G Kergon */ 336d15b774cSAlasdair G Kergon idr_destroy(&_minor_idr); 3371da177e4SLinus Torvalds } 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds /* 3401da177e4SLinus Torvalds * Block device functions 3411da177e4SLinus Torvalds */ 342432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md) 343432a212cSMike Anderson { 344432a212cSMike Anderson return test_bit(DMF_DELETING, &md->flags); 345432a212cSMike Anderson } 346432a212cSMike Anderson 347fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode) 3481da177e4SLinus Torvalds { 3491da177e4SLinus Torvalds struct mapped_device *md; 3501da177e4SLinus Torvalds 351fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 352fba9f90eSJeff Mahoney 353fe5f9f2cSAl Viro md = bdev->bd_disk->private_data; 354fba9f90eSJeff Mahoney if (!md) 355fba9f90eSJeff Mahoney goto out; 356fba9f90eSJeff Mahoney 3575c6bd75dSAlasdair G Kergon if (test_bit(DMF_FREEING, &md->flags) || 358432a212cSMike Anderson dm_deleting_md(md)) { 359fba9f90eSJeff Mahoney md = NULL; 360fba9f90eSJeff Mahoney goto out; 361fba9f90eSJeff Mahoney } 362fba9f90eSJeff Mahoney 3631da177e4SLinus Torvalds dm_get(md); 3645c6bd75dSAlasdair G Kergon atomic_inc(&md->open_count); 365fba9f90eSJeff Mahoney out: 366fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 367fba9f90eSJeff Mahoney 368fba9f90eSJeff Mahoney return md ? 0 : -ENXIO; 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds 371db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode) 3721da177e4SLinus Torvalds { 37363a4f065SMike Snitzer struct mapped_device *md; 3746e9624b8SArnd Bergmann 3754a1aeb98SMilan Broz spin_lock(&_minor_lock); 3764a1aeb98SMilan Broz 37763a4f065SMike Snitzer md = disk->private_data; 37863a4f065SMike Snitzer if (WARN_ON(!md)) 37963a4f065SMike Snitzer goto out; 38063a4f065SMike Snitzer 3812c140a24SMikulas Patocka if (atomic_dec_and_test(&md->open_count) && 3822c140a24SMikulas Patocka (test_bit(DMF_DEFERRED_REMOVE, &md->flags))) 383acfe0ad7SMikulas Patocka queue_work(deferred_remove_workqueue, &deferred_remove_work); 3842c140a24SMikulas Patocka 3851da177e4SLinus Torvalds dm_put(md); 38663a4f065SMike Snitzer out: 3874a1aeb98SMilan Broz spin_unlock(&_minor_lock); 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds 3905c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md) 3915c6bd75dSAlasdair G Kergon { 3925c6bd75dSAlasdair G Kergon return atomic_read(&md->open_count); 3935c6bd75dSAlasdair G Kergon } 3945c6bd75dSAlasdair G Kergon 3955c6bd75dSAlasdair G Kergon /* 3965c6bd75dSAlasdair G Kergon * Guarantees nothing is using the device before it's deleted. 3975c6bd75dSAlasdair G Kergon */ 3982c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred) 3995c6bd75dSAlasdair G Kergon { 4005c6bd75dSAlasdair G Kergon int r = 0; 4015c6bd75dSAlasdair G Kergon 4025c6bd75dSAlasdair G Kergon spin_lock(&_minor_lock); 4035c6bd75dSAlasdair G Kergon 4042c140a24SMikulas Patocka if (dm_open_count(md)) { 4055c6bd75dSAlasdair G Kergon r = -EBUSY; 4062c140a24SMikulas Patocka if (mark_deferred) 4072c140a24SMikulas Patocka set_bit(DMF_DEFERRED_REMOVE, &md->flags); 4082c140a24SMikulas Patocka } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags)) 4092c140a24SMikulas Patocka r = -EEXIST; 4105c6bd75dSAlasdair G Kergon else 4115c6bd75dSAlasdair G Kergon set_bit(DMF_DELETING, &md->flags); 4125c6bd75dSAlasdair G Kergon 4135c6bd75dSAlasdair G Kergon spin_unlock(&_minor_lock); 4145c6bd75dSAlasdair G Kergon 4155c6bd75dSAlasdair G Kergon return r; 4165c6bd75dSAlasdair G Kergon } 4175c6bd75dSAlasdair G Kergon 4182c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md) 4192c140a24SMikulas Patocka { 4202c140a24SMikulas Patocka int r = 0; 4212c140a24SMikulas Patocka 4222c140a24SMikulas Patocka spin_lock(&_minor_lock); 4232c140a24SMikulas Patocka 4242c140a24SMikulas Patocka if (test_bit(DMF_DELETING, &md->flags)) 4252c140a24SMikulas Patocka r = -EBUSY; 4262c140a24SMikulas Patocka else 4272c140a24SMikulas Patocka clear_bit(DMF_DEFERRED_REMOVE, &md->flags); 4282c140a24SMikulas Patocka 4292c140a24SMikulas Patocka spin_unlock(&_minor_lock); 4302c140a24SMikulas Patocka 4312c140a24SMikulas Patocka return r; 4322c140a24SMikulas Patocka } 4332c140a24SMikulas Patocka 4342c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w) 4352c140a24SMikulas Patocka { 4362c140a24SMikulas Patocka dm_deferred_remove(); 4372c140a24SMikulas Patocka } 4382c140a24SMikulas Patocka 439fd2ed4d2SMikulas Patocka sector_t dm_get_size(struct mapped_device *md) 440fd2ed4d2SMikulas Patocka { 441fd2ed4d2SMikulas Patocka return get_capacity(md->disk); 442fd2ed4d2SMikulas Patocka } 443fd2ed4d2SMikulas Patocka 4449974fa2cSMike Snitzer struct request_queue *dm_get_md_queue(struct mapped_device *md) 4459974fa2cSMike Snitzer { 4469974fa2cSMike Snitzer return md->queue; 4479974fa2cSMike Snitzer } 4489974fa2cSMike Snitzer 449fd2ed4d2SMikulas Patocka struct dm_stats *dm_get_stats(struct mapped_device *md) 450fd2ed4d2SMikulas Patocka { 451fd2ed4d2SMikulas Patocka return &md->stats; 452fd2ed4d2SMikulas Patocka } 453fd2ed4d2SMikulas Patocka 4543ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 4553ac51e74SDarrick J. Wong { 4563ac51e74SDarrick J. Wong struct mapped_device *md = bdev->bd_disk->private_data; 4573ac51e74SDarrick J. Wong 4583ac51e74SDarrick J. Wong return dm_get_geometry(md, geo); 4593ac51e74SDarrick J. Wong } 4603ac51e74SDarrick J. Wong 461e76239a3SChristoph Hellwig static int dm_blk_report_zones(struct gendisk *disk, sector_t sector, 462e76239a3SChristoph Hellwig struct blk_zone *zones, unsigned int *nr_zones, 463e76239a3SChristoph Hellwig gfp_t gfp_mask) 464e76239a3SChristoph Hellwig { 465e76239a3SChristoph Hellwig #ifdef CONFIG_BLK_DEV_ZONED 466e76239a3SChristoph Hellwig struct mapped_device *md = disk->private_data; 467e76239a3SChristoph Hellwig struct dm_target *tgt; 468e76239a3SChristoph Hellwig struct dm_table *map; 469e76239a3SChristoph Hellwig int srcu_idx, ret; 470e76239a3SChristoph Hellwig 471e76239a3SChristoph Hellwig if (dm_suspended_md(md)) 472e76239a3SChristoph Hellwig return -EAGAIN; 473e76239a3SChristoph Hellwig 474e76239a3SChristoph Hellwig map = dm_get_live_table(md, &srcu_idx); 475e76239a3SChristoph Hellwig if (!map) 476e76239a3SChristoph Hellwig return -EIO; 477e76239a3SChristoph Hellwig 478e76239a3SChristoph Hellwig tgt = dm_table_find_target(map, sector); 479e76239a3SChristoph Hellwig if (!dm_target_is_valid(tgt)) { 480e76239a3SChristoph Hellwig ret = -EIO; 481e76239a3SChristoph Hellwig goto out; 482e76239a3SChristoph Hellwig } 483e76239a3SChristoph Hellwig 484e76239a3SChristoph Hellwig /* 485e76239a3SChristoph Hellwig * If we are executing this, we already know that the block device 486e76239a3SChristoph Hellwig * is a zoned device and so each target should have support for that 487e76239a3SChristoph Hellwig * type of drive. A missing report_zones method means that the target 488e76239a3SChristoph Hellwig * driver has a problem. 489e76239a3SChristoph Hellwig */ 490e76239a3SChristoph Hellwig if (WARN_ON(!tgt->type->report_zones)) { 491e76239a3SChristoph Hellwig ret = -EIO; 492e76239a3SChristoph Hellwig goto out; 493e76239a3SChristoph Hellwig } 494e76239a3SChristoph Hellwig 495e76239a3SChristoph Hellwig /* 496e76239a3SChristoph Hellwig * blkdev_report_zones() will loop and call this again to cover all the 497e76239a3SChristoph Hellwig * zones of the target, eventually moving on to the next target. 498e76239a3SChristoph Hellwig * So there is no need to loop here trying to fill the entire array 499e76239a3SChristoph Hellwig * of zones. 500e76239a3SChristoph Hellwig */ 501e76239a3SChristoph Hellwig ret = tgt->type->report_zones(tgt, sector, zones, 502e76239a3SChristoph Hellwig nr_zones, gfp_mask); 503e76239a3SChristoph Hellwig 504e76239a3SChristoph Hellwig out: 505e76239a3SChristoph Hellwig dm_put_live_table(md, srcu_idx); 506e76239a3SChristoph Hellwig return ret; 507e76239a3SChristoph Hellwig #else 508e76239a3SChristoph Hellwig return -ENOTSUPP; 509e76239a3SChristoph Hellwig #endif 510e76239a3SChristoph Hellwig } 511e76239a3SChristoph Hellwig 512971888c4SMike Snitzer static int dm_prepare_ioctl(struct mapped_device *md, int *srcu_idx, 5135bd5e8d8SMike Snitzer struct block_device **bdev) 514971888c4SMike Snitzer __acquires(md->io_barrier) 515aa129a22SMilan Broz { 51666482026SMike Snitzer struct dm_target *tgt; 5176c182cd8SHannes Reinecke struct dm_table *map; 518971888c4SMike Snitzer int r; 519aa129a22SMilan Broz 5206c182cd8SHannes Reinecke retry: 521e56f81e0SChristoph Hellwig r = -ENOTTY; 522971888c4SMike Snitzer map = dm_get_live_table(md, srcu_idx); 523aa129a22SMilan Broz if (!map || !dm_table_get_size(map)) 524971888c4SMike Snitzer return r; 525aa129a22SMilan Broz 526aa129a22SMilan Broz /* We only support devices that have a single target */ 527aa129a22SMilan Broz if (dm_table_get_num_targets(map) != 1) 528971888c4SMike Snitzer return r; 529aa129a22SMilan Broz 53066482026SMike Snitzer tgt = dm_table_get_target(map, 0); 53166482026SMike Snitzer if (!tgt->type->prepare_ioctl) 532e56f81e0SChristoph Hellwig return r; 533aa129a22SMilan Broz 534971888c4SMike Snitzer if (dm_suspended_md(md)) 535971888c4SMike Snitzer return -EAGAIN; 536971888c4SMike Snitzer 5375bd5e8d8SMike Snitzer r = tgt->type->prepare_ioctl(tgt, bdev); 5385bbbfdf6SJunichi Nomura if (r == -ENOTCONN && !fatal_signal_pending(current)) { 539971888c4SMike Snitzer dm_put_live_table(md, *srcu_idx); 5406c182cd8SHannes Reinecke msleep(10); 5416c182cd8SHannes Reinecke goto retry; 5426c182cd8SHannes Reinecke } 543971888c4SMike Snitzer 544e56f81e0SChristoph Hellwig return r; 545e56f81e0SChristoph Hellwig } 5466c182cd8SHannes Reinecke 547971888c4SMike Snitzer static void dm_unprepare_ioctl(struct mapped_device *md, int srcu_idx) 548971888c4SMike Snitzer __releases(md->io_barrier) 549971888c4SMike Snitzer { 550971888c4SMike Snitzer dm_put_live_table(md, srcu_idx); 551971888c4SMike Snitzer } 552971888c4SMike Snitzer 553e56f81e0SChristoph Hellwig static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode, 554e56f81e0SChristoph Hellwig unsigned int cmd, unsigned long arg) 555e56f81e0SChristoph Hellwig { 556e56f81e0SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 557971888c4SMike Snitzer int r, srcu_idx; 558e56f81e0SChristoph Hellwig 5595bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 560e56f81e0SChristoph Hellwig if (r < 0) 561971888c4SMike Snitzer goto out; 562e56f81e0SChristoph Hellwig 563e56f81e0SChristoph Hellwig if (r > 0) { 564e56f81e0SChristoph Hellwig /* 565e980f623SChristoph Hellwig * Target determined this ioctl is being issued against a 566e980f623SChristoph Hellwig * subset of the parent bdev; require extra privileges. 567e56f81e0SChristoph Hellwig */ 568e980f623SChristoph Hellwig if (!capable(CAP_SYS_RAWIO)) { 569e980f623SChristoph Hellwig DMWARN_LIMIT( 570e980f623SChristoph Hellwig "%s: sending ioctl %x to DM device without required privilege.", 571e980f623SChristoph Hellwig current->comm, cmd); 572e980f623SChristoph Hellwig r = -ENOIOCTLCMD; 573e56f81e0SChristoph Hellwig goto out; 574e56f81e0SChristoph Hellwig } 575e980f623SChristoph Hellwig } 576e56f81e0SChristoph Hellwig 57766482026SMike Snitzer r = __blkdev_driver_ioctl(bdev, mode, cmd, arg); 578e56f81e0SChristoph Hellwig out: 579971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 580aa129a22SMilan Broz return r; 581aa129a22SMilan Broz } 582aa129a22SMilan Broz 583978e51baSMike Snitzer static void start_io_acct(struct dm_io *io); 584978e51baSMike Snitzer 585978e51baSMike Snitzer static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio) 5861da177e4SLinus Torvalds { 58764f52b0eSMike Snitzer struct dm_io *io; 58864f52b0eSMike Snitzer struct dm_target_io *tio; 58964f52b0eSMike Snitzer struct bio *clone; 59064f52b0eSMike Snitzer 5916f1c819cSKent Overstreet clone = bio_alloc_bioset(GFP_NOIO, 0, &md->io_bs); 59264f52b0eSMike Snitzer if (!clone) 59364f52b0eSMike Snitzer return NULL; 59464f52b0eSMike Snitzer 59564f52b0eSMike Snitzer tio = container_of(clone, struct dm_target_io, clone); 59664f52b0eSMike Snitzer tio->inside_dm_io = true; 59764f52b0eSMike Snitzer tio->io = NULL; 59864f52b0eSMike Snitzer 59964f52b0eSMike Snitzer io = container_of(tio, struct dm_io, tio); 60064f52b0eSMike Snitzer io->magic = DM_IO_MAGIC; 601978e51baSMike Snitzer io->status = 0; 602978e51baSMike Snitzer atomic_set(&io->io_count, 1); 603978e51baSMike Snitzer io->orig_bio = bio; 604978e51baSMike Snitzer io->md = md; 605978e51baSMike Snitzer spin_lock_init(&io->endio_lock); 606978e51baSMike Snitzer 607978e51baSMike Snitzer start_io_acct(io); 60864f52b0eSMike Snitzer 60964f52b0eSMike Snitzer return io; 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds 612028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io) 6131da177e4SLinus Torvalds { 61464f52b0eSMike Snitzer bio_put(&io->tio.clone); 61564f52b0eSMike Snitzer } 61664f52b0eSMike Snitzer 61764f52b0eSMike Snitzer static struct dm_target_io *alloc_tio(struct clone_info *ci, struct dm_target *ti, 61864f52b0eSMike Snitzer unsigned target_bio_nr, gfp_t gfp_mask) 61964f52b0eSMike Snitzer { 62064f52b0eSMike Snitzer struct dm_target_io *tio; 62164f52b0eSMike Snitzer 62264f52b0eSMike Snitzer if (!ci->io->tio.io) { 62364f52b0eSMike Snitzer /* the dm_target_io embedded in ci->io is available */ 62464f52b0eSMike Snitzer tio = &ci->io->tio; 62564f52b0eSMike Snitzer } else { 6266f1c819cSKent Overstreet struct bio *clone = bio_alloc_bioset(gfp_mask, 0, &ci->io->md->bs); 62764f52b0eSMike Snitzer if (!clone) 62864f52b0eSMike Snitzer return NULL; 62964f52b0eSMike Snitzer 63064f52b0eSMike Snitzer tio = container_of(clone, struct dm_target_io, clone); 63164f52b0eSMike Snitzer tio->inside_dm_io = false; 63264f52b0eSMike Snitzer } 63364f52b0eSMike Snitzer 63464f52b0eSMike Snitzer tio->magic = DM_TIO_MAGIC; 63564f52b0eSMike Snitzer tio->io = ci->io; 63664f52b0eSMike Snitzer tio->ti = ti; 63764f52b0eSMike Snitzer tio->target_bio_nr = target_bio_nr; 63864f52b0eSMike Snitzer 63964f52b0eSMike Snitzer return tio; 6401da177e4SLinus Torvalds } 6411da177e4SLinus Torvalds 642cfae7529SMike Snitzer static void free_tio(struct dm_target_io *tio) 6431da177e4SLinus Torvalds { 64464f52b0eSMike Snitzer if (tio->inside_dm_io) 64564f52b0eSMike Snitzer return; 646dba14160SMikulas Patocka bio_put(&tio->clone); 6471da177e4SLinus Torvalds } 6481da177e4SLinus Torvalds 649c4576aedSMike Snitzer static bool md_in_flight_bios(struct mapped_device *md) 65090abb8c4SKiyoshi Ueda { 6516f757231SMikulas Patocka int cpu; 6526f757231SMikulas Patocka struct hd_struct *part = &dm_disk(md)->part0; 653b7934ba4SJens Axboe long sum = 0; 6546f757231SMikulas Patocka 6556f757231SMikulas Patocka for_each_possible_cpu(cpu) { 656b7934ba4SJens Axboe sum += part_stat_local_read_cpu(part, in_flight[0], cpu); 657b7934ba4SJens Axboe sum += part_stat_local_read_cpu(part, in_flight[1], cpu); 6586f757231SMikulas Patocka } 6596f757231SMikulas Patocka 660b7934ba4SJens Axboe return sum != 0; 66190abb8c4SKiyoshi Ueda } 66290abb8c4SKiyoshi Ueda 663c4576aedSMike Snitzer static bool md_in_flight(struct mapped_device *md) 664c4576aedSMike Snitzer { 665c4576aedSMike Snitzer if (queue_is_mq(md->queue)) 6663c94d83cSJens Axboe return blk_mq_queue_inflight(md->queue); 667c4576aedSMike Snitzer else 668c4576aedSMike Snitzer return md_in_flight_bios(md); 669cec47e3dSKiyoshi Ueda } 670cec47e3dSKiyoshi Ueda 6713eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io) 6723eaf840eSJun'ichi "Nick" Nomura { 6733eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 674745dc570SMike Snitzer struct bio *bio = io->orig_bio; 6753eaf840eSJun'ichi "Nick" Nomura 6763eaf840eSJun'ichi "Nick" Nomura io->start_time = jiffies; 6773eaf840eSJun'ichi "Nick" Nomura 678ddcf35d3SMichael Callahan generic_start_io_acct(md->queue, bio_op(bio), bio_sectors(bio), 679ddcf35d3SMichael Callahan &dm_disk(md)->part0); 680f3986374SMike Snitzer 681fd2ed4d2SMikulas Patocka if (unlikely(dm_stats_used(&md->stats))) 682528ec5abSMike Christie dm_stats_account_io(&md->stats, bio_data_dir(bio), 683528ec5abSMike Christie bio->bi_iter.bi_sector, bio_sectors(bio), 684528ec5abSMike Christie false, 0, &io->stats_aux); 6853eaf840eSJun'ichi "Nick" Nomura } 6863eaf840eSJun'ichi "Nick" Nomura 687d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io) 6883eaf840eSJun'ichi "Nick" Nomura { 6893eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 690745dc570SMike Snitzer struct bio *bio = io->orig_bio; 6913eaf840eSJun'ichi "Nick" Nomura unsigned long duration = jiffies - io->start_time; 6923eaf840eSJun'ichi "Nick" Nomura 693ddcf35d3SMichael Callahan generic_end_io_acct(md->queue, bio_op(bio), &dm_disk(md)->part0, 694ddcf35d3SMichael Callahan io->start_time); 6953eaf840eSJun'ichi "Nick" Nomura 696fd2ed4d2SMikulas Patocka if (unlikely(dm_stats_used(&md->stats))) 697528ec5abSMike Christie dm_stats_account_io(&md->stats, bio_data_dir(bio), 698528ec5abSMike Christie bio->bi_iter.bi_sector, bio_sectors(bio), 699528ec5abSMike Christie true, duration, &io->stats_aux); 700fd2ed4d2SMikulas Patocka 701d221d2e7SMikulas Patocka /* nudge anyone waiting on suspend queue */ 702*645efa84SMikulas Patocka if (unlikely(wq_has_sleeper(&md->wait))) 703d221d2e7SMikulas Patocka wake_up(&md->wait); 7043eaf840eSJun'ichi "Nick" Nomura } 7053eaf840eSJun'ichi "Nick" Nomura 7061da177e4SLinus Torvalds /* 7071da177e4SLinus Torvalds * Add the bio to the list of deferred io. 7081da177e4SLinus Torvalds */ 70992c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio) 7101da177e4SLinus Torvalds { 71105447420SKiyoshi Ueda unsigned long flags; 7121da177e4SLinus Torvalds 71305447420SKiyoshi Ueda spin_lock_irqsave(&md->deferred_lock, flags); 7141da177e4SLinus Torvalds bio_list_add(&md->deferred, bio); 71505447420SKiyoshi Ueda spin_unlock_irqrestore(&md->deferred_lock, flags); 71692c63902SMikulas Patocka queue_work(md->wq, &md->work); 7171da177e4SLinus Torvalds } 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds /* 7201da177e4SLinus Torvalds * Everyone (including functions in this file), should use this 7211da177e4SLinus Torvalds * function to access the md->map field, and make sure they call 72283d5e5b0SMikulas Patocka * dm_put_live_table() when finished. 7231da177e4SLinus Torvalds */ 72483d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier) 7251da177e4SLinus Torvalds { 72683d5e5b0SMikulas Patocka *srcu_idx = srcu_read_lock(&md->io_barrier); 7271da177e4SLinus Torvalds 72883d5e5b0SMikulas Patocka return srcu_dereference(md->map, &md->io_barrier); 72983d5e5b0SMikulas Patocka } 7301da177e4SLinus Torvalds 73183d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier) 73283d5e5b0SMikulas Patocka { 73383d5e5b0SMikulas Patocka srcu_read_unlock(&md->io_barrier, srcu_idx); 73483d5e5b0SMikulas Patocka } 73583d5e5b0SMikulas Patocka 73683d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md) 73783d5e5b0SMikulas Patocka { 73883d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 73983d5e5b0SMikulas Patocka synchronize_rcu_expedited(); 74083d5e5b0SMikulas Patocka } 74183d5e5b0SMikulas Patocka 74283d5e5b0SMikulas Patocka /* 74383d5e5b0SMikulas Patocka * A fast alternative to dm_get_live_table/dm_put_live_table. 74483d5e5b0SMikulas Patocka * The caller must not block between these two functions. 74583d5e5b0SMikulas Patocka */ 74683d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU) 74783d5e5b0SMikulas Patocka { 74883d5e5b0SMikulas Patocka rcu_read_lock(); 74983d5e5b0SMikulas Patocka return rcu_dereference(md->map); 75083d5e5b0SMikulas Patocka } 75183d5e5b0SMikulas Patocka 75283d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU) 75383d5e5b0SMikulas Patocka { 75483d5e5b0SMikulas Patocka rcu_read_unlock(); 7551da177e4SLinus Torvalds } 7561da177e4SLinus Torvalds 757971888c4SMike Snitzer static char *_dm_claim_ptr = "I belong to device-mapper"; 758971888c4SMike Snitzer 7593ac51e74SDarrick J. Wong /* 76086f1152bSBenjamin Marzinski * Open a table device so we can use it as a map destination. 76186f1152bSBenjamin Marzinski */ 76286f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev, 76386f1152bSBenjamin Marzinski struct mapped_device *md) 76486f1152bSBenjamin Marzinski { 76586f1152bSBenjamin Marzinski struct block_device *bdev; 76686f1152bSBenjamin Marzinski 76786f1152bSBenjamin Marzinski int r; 76886f1152bSBenjamin Marzinski 76986f1152bSBenjamin Marzinski BUG_ON(td->dm_dev.bdev); 77086f1152bSBenjamin Marzinski 771519049afSMike Snitzer bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr); 77286f1152bSBenjamin Marzinski if (IS_ERR(bdev)) 77386f1152bSBenjamin Marzinski return PTR_ERR(bdev); 77486f1152bSBenjamin Marzinski 77586f1152bSBenjamin Marzinski r = bd_link_disk_holder(bdev, dm_disk(md)); 77686f1152bSBenjamin Marzinski if (r) { 77786f1152bSBenjamin Marzinski blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL); 77886f1152bSBenjamin Marzinski return r; 77986f1152bSBenjamin Marzinski } 78086f1152bSBenjamin Marzinski 78186f1152bSBenjamin Marzinski td->dm_dev.bdev = bdev; 782817bf402SDan Williams td->dm_dev.dax_dev = dax_get_by_host(bdev->bd_disk->disk_name); 78386f1152bSBenjamin Marzinski return 0; 78486f1152bSBenjamin Marzinski } 78586f1152bSBenjamin Marzinski 78686f1152bSBenjamin Marzinski /* 78786f1152bSBenjamin Marzinski * Close a table device that we've been using. 78886f1152bSBenjamin Marzinski */ 78986f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md) 79086f1152bSBenjamin Marzinski { 79186f1152bSBenjamin Marzinski if (!td->dm_dev.bdev) 79286f1152bSBenjamin Marzinski return; 79386f1152bSBenjamin Marzinski 79486f1152bSBenjamin Marzinski bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md)); 79586f1152bSBenjamin Marzinski blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL); 796817bf402SDan Williams put_dax(td->dm_dev.dax_dev); 79786f1152bSBenjamin Marzinski td->dm_dev.bdev = NULL; 798817bf402SDan Williams td->dm_dev.dax_dev = NULL; 79986f1152bSBenjamin Marzinski } 80086f1152bSBenjamin Marzinski 80186f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev, 80286f1152bSBenjamin Marzinski fmode_t mode) { 80386f1152bSBenjamin Marzinski struct table_device *td; 80486f1152bSBenjamin Marzinski 80586f1152bSBenjamin Marzinski list_for_each_entry(td, l, list) 80686f1152bSBenjamin Marzinski if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode) 80786f1152bSBenjamin Marzinski return td; 80886f1152bSBenjamin Marzinski 80986f1152bSBenjamin Marzinski return NULL; 81086f1152bSBenjamin Marzinski } 81186f1152bSBenjamin Marzinski 81286f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode, 81386f1152bSBenjamin Marzinski struct dm_dev **result) { 81486f1152bSBenjamin Marzinski int r; 81586f1152bSBenjamin Marzinski struct table_device *td; 81686f1152bSBenjamin Marzinski 81786f1152bSBenjamin Marzinski mutex_lock(&md->table_devices_lock); 81886f1152bSBenjamin Marzinski td = find_table_device(&md->table_devices, dev, mode); 81986f1152bSBenjamin Marzinski if (!td) { 820115485e8SMike Snitzer td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id); 82186f1152bSBenjamin Marzinski if (!td) { 82286f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 82386f1152bSBenjamin Marzinski return -ENOMEM; 82486f1152bSBenjamin Marzinski } 82586f1152bSBenjamin Marzinski 82686f1152bSBenjamin Marzinski td->dm_dev.mode = mode; 82786f1152bSBenjamin Marzinski td->dm_dev.bdev = NULL; 82886f1152bSBenjamin Marzinski 82986f1152bSBenjamin Marzinski if ((r = open_table_device(td, dev, md))) { 83086f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 83186f1152bSBenjamin Marzinski kfree(td); 83286f1152bSBenjamin Marzinski return r; 83386f1152bSBenjamin Marzinski } 83486f1152bSBenjamin Marzinski 83586f1152bSBenjamin Marzinski format_dev_t(td->dm_dev.name, dev); 83686f1152bSBenjamin Marzinski 837b0b4d7c6SElena Reshetova refcount_set(&td->count, 1); 83886f1152bSBenjamin Marzinski list_add(&td->list, &md->table_devices); 839b0b4d7c6SElena Reshetova } else { 840b0b4d7c6SElena Reshetova refcount_inc(&td->count); 84186f1152bSBenjamin Marzinski } 84286f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 84386f1152bSBenjamin Marzinski 84486f1152bSBenjamin Marzinski *result = &td->dm_dev; 84586f1152bSBenjamin Marzinski return 0; 84686f1152bSBenjamin Marzinski } 84786f1152bSBenjamin Marzinski EXPORT_SYMBOL_GPL(dm_get_table_device); 84886f1152bSBenjamin Marzinski 84986f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d) 85086f1152bSBenjamin Marzinski { 85186f1152bSBenjamin Marzinski struct table_device *td = container_of(d, struct table_device, dm_dev); 85286f1152bSBenjamin Marzinski 85386f1152bSBenjamin Marzinski mutex_lock(&md->table_devices_lock); 854b0b4d7c6SElena Reshetova if (refcount_dec_and_test(&td->count)) { 85586f1152bSBenjamin Marzinski close_table_device(td, md); 85686f1152bSBenjamin Marzinski list_del(&td->list); 85786f1152bSBenjamin Marzinski kfree(td); 85886f1152bSBenjamin Marzinski } 85986f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 86086f1152bSBenjamin Marzinski } 86186f1152bSBenjamin Marzinski EXPORT_SYMBOL(dm_put_table_device); 86286f1152bSBenjamin Marzinski 86386f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices) 86486f1152bSBenjamin Marzinski { 86586f1152bSBenjamin Marzinski struct list_head *tmp, *next; 86686f1152bSBenjamin Marzinski 86786f1152bSBenjamin Marzinski list_for_each_safe(tmp, next, devices) { 86886f1152bSBenjamin Marzinski struct table_device *td = list_entry(tmp, struct table_device, list); 86986f1152bSBenjamin Marzinski 87086f1152bSBenjamin Marzinski DMWARN("dm_destroy: %s still exists with %d references", 871b0b4d7c6SElena Reshetova td->dm_dev.name, refcount_read(&td->count)); 87286f1152bSBenjamin Marzinski kfree(td); 87386f1152bSBenjamin Marzinski } 87486f1152bSBenjamin Marzinski } 87586f1152bSBenjamin Marzinski 87686f1152bSBenjamin Marzinski /* 8773ac51e74SDarrick J. Wong * Get the geometry associated with a dm device 8783ac51e74SDarrick J. Wong */ 8793ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) 8803ac51e74SDarrick J. Wong { 8813ac51e74SDarrick J. Wong *geo = md->geometry; 8823ac51e74SDarrick J. Wong 8833ac51e74SDarrick J. Wong return 0; 8843ac51e74SDarrick J. Wong } 8853ac51e74SDarrick J. Wong 8863ac51e74SDarrick J. Wong /* 8873ac51e74SDarrick J. Wong * Set the geometry of a device. 8883ac51e74SDarrick J. Wong */ 8893ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) 8903ac51e74SDarrick J. Wong { 8913ac51e74SDarrick J. Wong sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; 8923ac51e74SDarrick J. Wong 8933ac51e74SDarrick J. Wong if (geo->start > sz) { 8943ac51e74SDarrick J. Wong DMWARN("Start sector is beyond the geometry limits."); 8953ac51e74SDarrick J. Wong return -EINVAL; 8963ac51e74SDarrick J. Wong } 8973ac51e74SDarrick J. Wong 8983ac51e74SDarrick J. Wong md->geometry = *geo; 8993ac51e74SDarrick J. Wong 9003ac51e74SDarrick J. Wong return 0; 9013ac51e74SDarrick J. Wong } 9023ac51e74SDarrick J. Wong 9032e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md) 9042e93ccc1SKiyoshi Ueda { 9052e93ccc1SKiyoshi Ueda return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 9062e93ccc1SKiyoshi Ueda } 9072e93ccc1SKiyoshi Ueda 9081da177e4SLinus Torvalds /* 9091da177e4SLinus Torvalds * Decrements the number of outstanding ios that a bio has been 9101da177e4SLinus Torvalds * cloned into, completing the original io if necc. 9111da177e4SLinus Torvalds */ 9124e4cbee9SChristoph Hellwig static void dec_pending(struct dm_io *io, blk_status_t error) 9131da177e4SLinus Torvalds { 9142e93ccc1SKiyoshi Ueda unsigned long flags; 9154e4cbee9SChristoph Hellwig blk_status_t io_error; 916b35f8caaSMilan Broz struct bio *bio; 917b35f8caaSMilan Broz struct mapped_device *md = io->md; 9182e93ccc1SKiyoshi Ueda 9192e93ccc1SKiyoshi Ueda /* Push-back supersedes any I/O errors */ 920f88fb981SKiyoshi Ueda if (unlikely(error)) { 921f88fb981SKiyoshi Ueda spin_lock_irqsave(&io->endio_lock, flags); 922745dc570SMike Snitzer if (!(io->status == BLK_STS_DM_REQUEUE && __noflush_suspending(md))) 9234e4cbee9SChristoph Hellwig io->status = error; 924f88fb981SKiyoshi Ueda spin_unlock_irqrestore(&io->endio_lock, flags); 925f88fb981SKiyoshi Ueda } 9261da177e4SLinus Torvalds 9271da177e4SLinus Torvalds if (atomic_dec_and_test(&io->io_count)) { 9284e4cbee9SChristoph Hellwig if (io->status == BLK_STS_DM_REQUEUE) { 9292e93ccc1SKiyoshi Ueda /* 9302e93ccc1SKiyoshi Ueda * Target requested pushing back the I/O. 9312e93ccc1SKiyoshi Ueda */ 932022c2611SMikulas Patocka spin_lock_irqsave(&md->deferred_lock, flags); 9336a8736d1STejun Heo if (__noflush_suspending(md)) 934745dc570SMike Snitzer /* NOTE early return due to BLK_STS_DM_REQUEUE below */ 935745dc570SMike Snitzer bio_list_add_head(&md->deferred, io->orig_bio); 9366a8736d1STejun Heo else 9372e93ccc1SKiyoshi Ueda /* noflush suspend was interrupted. */ 9384e4cbee9SChristoph Hellwig io->status = BLK_STS_IOERR; 939022c2611SMikulas Patocka spin_unlock_irqrestore(&md->deferred_lock, flags); 9402e93ccc1SKiyoshi Ueda } 9412e93ccc1SKiyoshi Ueda 9424e4cbee9SChristoph Hellwig io_error = io->status; 943745dc570SMike Snitzer bio = io->orig_bio; 944af7e466aSMikulas Patocka end_io_acct(io); 945a97f925aSMikulas Patocka free_io(md, io); 9461da177e4SLinus Torvalds 9474e4cbee9SChristoph Hellwig if (io_error == BLK_STS_DM_REQUEUE) 9486a8736d1STejun Heo return; 9496a8736d1STejun Heo 9501eff9d32SJens Axboe if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) { 9511da177e4SLinus Torvalds /* 9526a8736d1STejun Heo * Preflush done for flush with data, reissue 95328a8f0d3SMike Christie * without REQ_PREFLUSH. 9541da177e4SLinus Torvalds */ 9551eff9d32SJens Axboe bio->bi_opf &= ~REQ_PREFLUSH; 9566a8736d1STejun Heo queue_io(md, bio); 957b35f8caaSMilan Broz } else { 958b372d360SMike Snitzer /* done with normal IO or empty flush */ 9598dd601faSNeilBrown if (io_error) 9604e4cbee9SChristoph Hellwig bio->bi_status = io_error; 9614246a0b6SChristoph Hellwig bio_endio(bio); 9622e93ccc1SKiyoshi Ueda } 9631da177e4SLinus Torvalds } 964af7e466aSMikulas Patocka } 9651da177e4SLinus Torvalds 9664cc96131SMike Snitzer void disable_write_same(struct mapped_device *md) 9677eee4ae2SMike Snitzer { 9687eee4ae2SMike Snitzer struct queue_limits *limits = dm_get_queue_limits(md); 9697eee4ae2SMike Snitzer 9707eee4ae2SMike Snitzer /* device doesn't really support WRITE SAME, disable it */ 9717eee4ae2SMike Snitzer limits->max_write_same_sectors = 0; 9727eee4ae2SMike Snitzer } 9737eee4ae2SMike Snitzer 974ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md) 975ac62d620SChristoph Hellwig { 976ac62d620SChristoph Hellwig struct queue_limits *limits = dm_get_queue_limits(md); 977ac62d620SChristoph Hellwig 978ac62d620SChristoph Hellwig /* device doesn't really support WRITE ZEROES, disable it */ 979ac62d620SChristoph Hellwig limits->max_write_zeroes_sectors = 0; 980ac62d620SChristoph Hellwig } 981ac62d620SChristoph Hellwig 9824246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio) 9831da177e4SLinus Torvalds { 9844e4cbee9SChristoph Hellwig blk_status_t error = bio->bi_status; 985bfc6d41cSMikulas Patocka struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); 986b35f8caaSMilan Broz struct dm_io *io = tio->io; 9879faf400fSStefan Bader struct mapped_device *md = tio->io->md; 9881da177e4SLinus Torvalds dm_endio_fn endio = tio->ti->type->end_io; 9891da177e4SLinus Torvalds 990978e51baSMike Snitzer if (unlikely(error == BLK_STS_TARGET) && md->type != DM_TYPE_NVME_BIO_BASED) { 991ac62d620SChristoph Hellwig if (bio_op(bio) == REQ_OP_WRITE_SAME && 99274d46992SChristoph Hellwig !bio->bi_disk->queue->limits.max_write_same_sectors) 9937eee4ae2SMike Snitzer disable_write_same(md); 994ac62d620SChristoph Hellwig if (bio_op(bio) == REQ_OP_WRITE_ZEROES && 99574d46992SChristoph Hellwig !bio->bi_disk->queue->limits.max_write_zeroes_sectors) 996ac62d620SChristoph Hellwig disable_write_zeroes(md); 997ac62d620SChristoph Hellwig } 9987eee4ae2SMike Snitzer 9991be56909SChristoph Hellwig if (endio) { 10004e4cbee9SChristoph Hellwig int r = endio(tio->ti, bio, &error); 10011be56909SChristoph Hellwig switch (r) { 10021be56909SChristoph Hellwig case DM_ENDIO_REQUEUE: 10034e4cbee9SChristoph Hellwig error = BLK_STS_DM_REQUEUE; 10041be56909SChristoph Hellwig /*FALLTHRU*/ 10051be56909SChristoph Hellwig case DM_ENDIO_DONE: 10061be56909SChristoph Hellwig break; 10071be56909SChristoph Hellwig case DM_ENDIO_INCOMPLETE: 10081be56909SChristoph Hellwig /* The target will handle the io */ 10091be56909SChristoph Hellwig return; 10101be56909SChristoph Hellwig default: 10111be56909SChristoph Hellwig DMWARN("unimplemented target endio return value: %d", r); 10121be56909SChristoph Hellwig BUG(); 10131be56909SChristoph Hellwig } 10141be56909SChristoph Hellwig } 10151be56909SChristoph Hellwig 1016cfae7529SMike Snitzer free_tio(tio); 1017b35f8caaSMilan Broz dec_pending(io, error); 10181da177e4SLinus Torvalds } 10191da177e4SLinus Torvalds 102078d8e58aSMike Snitzer /* 102156a67df7SMike Snitzer * Return maximum size of I/O possible at the supplied sector up to the current 102256a67df7SMike Snitzer * target boundary. 102356a67df7SMike Snitzer */ 102456a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti) 10251da177e4SLinus Torvalds { 102656a67df7SMike Snitzer sector_t target_offset = dm_target_offset(ti, sector); 102756a67df7SMike Snitzer 102856a67df7SMike Snitzer return ti->len - target_offset; 102956a67df7SMike Snitzer } 103056a67df7SMike Snitzer 103156a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti) 103256a67df7SMike Snitzer { 103356a67df7SMike Snitzer sector_t len = max_io_len_target_boundary(sector, ti); 1034542f9038SMike Snitzer sector_t offset, max_len; 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds /* 10371da177e4SLinus Torvalds * Does the target need to split even further? 10381da177e4SLinus Torvalds */ 1039542f9038SMike Snitzer if (ti->max_io_len) { 1040542f9038SMike Snitzer offset = dm_target_offset(ti, sector); 1041542f9038SMike Snitzer if (unlikely(ti->max_io_len & (ti->max_io_len - 1))) 1042542f9038SMike Snitzer max_len = sector_div(offset, ti->max_io_len); 1043542f9038SMike Snitzer else 1044542f9038SMike Snitzer max_len = offset & (ti->max_io_len - 1); 1045542f9038SMike Snitzer max_len = ti->max_io_len - max_len; 1046542f9038SMike Snitzer 1047542f9038SMike Snitzer if (len > max_len) 1048542f9038SMike Snitzer len = max_len; 10491da177e4SLinus Torvalds } 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds return len; 10521da177e4SLinus Torvalds } 10531da177e4SLinus Torvalds 1054542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len) 1055542f9038SMike Snitzer { 1056542f9038SMike Snitzer if (len > UINT_MAX) { 1057542f9038SMike Snitzer DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)", 1058542f9038SMike Snitzer (unsigned long long)len, UINT_MAX); 1059542f9038SMike Snitzer ti->error = "Maximum size of target IO is too large"; 1060542f9038SMike Snitzer return -EINVAL; 1061542f9038SMike Snitzer } 1062542f9038SMike Snitzer 10638f50e358SMing Lei /* 10648f50e358SMing Lei * BIO based queue uses its own splitting. When multipage bvecs 10658f50e358SMing Lei * is switched on, size of the incoming bio may be too big to 10668f50e358SMing Lei * be handled in some targets, such as crypt. 10678f50e358SMing Lei * 10688f50e358SMing Lei * When these targets are ready for the big bio, we can remove 10698f50e358SMing Lei * the limit. 10708f50e358SMing Lei */ 10718f50e358SMing Lei ti->max_io_len = min_t(uint32_t, len, BIO_MAX_PAGES * PAGE_SIZE); 1072542f9038SMike Snitzer 1073542f9038SMike Snitzer return 0; 1074542f9038SMike Snitzer } 1075542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len); 1076542f9038SMike Snitzer 1077f26c5719SDan Williams static struct dm_target *dm_dax_get_live_target(struct mapped_device *md, 1078f26c5719SDan Williams sector_t sector, int *srcu_idx) 10793d97c829SMike Snitzer __acquires(md->io_barrier) 1080545ed20eSToshi Kani { 1081545ed20eSToshi Kani struct dm_table *map; 1082545ed20eSToshi Kani struct dm_target *ti; 1083545ed20eSToshi Kani 1084f26c5719SDan Williams map = dm_get_live_table(md, srcu_idx); 1085545ed20eSToshi Kani if (!map) 1086f26c5719SDan Williams return NULL; 1087545ed20eSToshi Kani 1088545ed20eSToshi Kani ti = dm_table_find_target(map, sector); 1089545ed20eSToshi Kani if (!dm_target_is_valid(ti)) 1090f26c5719SDan Williams return NULL; 1091f26c5719SDan Williams 1092f26c5719SDan Williams return ti; 1093f26c5719SDan Williams } 1094f26c5719SDan Williams 1095f26c5719SDan Williams static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, 1096f26c5719SDan Williams long nr_pages, void **kaddr, pfn_t *pfn) 1097f26c5719SDan Williams { 1098f26c5719SDan Williams struct mapped_device *md = dax_get_private(dax_dev); 1099f26c5719SDan Williams sector_t sector = pgoff * PAGE_SECTORS; 1100f26c5719SDan Williams struct dm_target *ti; 1101f26c5719SDan Williams long len, ret = -EIO; 1102f26c5719SDan Williams int srcu_idx; 1103f26c5719SDan Williams 1104f26c5719SDan Williams ti = dm_dax_get_live_target(md, sector, &srcu_idx); 1105f26c5719SDan Williams 1106f26c5719SDan Williams if (!ti) 1107545ed20eSToshi Kani goto out; 1108f26c5719SDan Williams if (!ti->type->direct_access) 1109f26c5719SDan Williams goto out; 1110f26c5719SDan Williams len = max_io_len(sector, ti) / PAGE_SECTORS; 1111f26c5719SDan Williams if (len < 1) 1112f26c5719SDan Williams goto out; 1113f26c5719SDan Williams nr_pages = min(len, nr_pages); 1114817bf402SDan Williams ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn); 1115817bf402SDan Williams 1116545ed20eSToshi Kani out: 1117545ed20eSToshi Kani dm_put_live_table(md, srcu_idx); 1118f26c5719SDan Williams 1119f26c5719SDan Williams return ret; 1120545ed20eSToshi Kani } 1121545ed20eSToshi Kani 11227e026c8cSDan Williams static size_t dm_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, 11237e026c8cSDan Williams void *addr, size_t bytes, struct iov_iter *i) 11247e026c8cSDan Williams { 11257e026c8cSDan Williams struct mapped_device *md = dax_get_private(dax_dev); 11267e026c8cSDan Williams sector_t sector = pgoff * PAGE_SECTORS; 11277e026c8cSDan Williams struct dm_target *ti; 11287e026c8cSDan Williams long ret = 0; 11297e026c8cSDan Williams int srcu_idx; 11307e026c8cSDan Williams 11317e026c8cSDan Williams ti = dm_dax_get_live_target(md, sector, &srcu_idx); 11327e026c8cSDan Williams 11337e026c8cSDan Williams if (!ti) 11347e026c8cSDan Williams goto out; 11357e026c8cSDan Williams if (!ti->type->dax_copy_from_iter) { 11367e026c8cSDan Williams ret = copy_from_iter(addr, bytes, i); 11377e026c8cSDan Williams goto out; 11387e026c8cSDan Williams } 11397e026c8cSDan Williams ret = ti->type->dax_copy_from_iter(ti, pgoff, addr, bytes, i); 11407e026c8cSDan Williams out: 11417e026c8cSDan Williams dm_put_live_table(md, srcu_idx); 11427e026c8cSDan Williams 11437e026c8cSDan Williams return ret; 11447e026c8cSDan Williams } 11457e026c8cSDan Williams 1146b3a9a0c3SDan Williams static size_t dm_dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, 1147b3a9a0c3SDan Williams void *addr, size_t bytes, struct iov_iter *i) 1148b3a9a0c3SDan Williams { 1149b3a9a0c3SDan Williams struct mapped_device *md = dax_get_private(dax_dev); 1150b3a9a0c3SDan Williams sector_t sector = pgoff * PAGE_SECTORS; 1151b3a9a0c3SDan Williams struct dm_target *ti; 1152b3a9a0c3SDan Williams long ret = 0; 1153b3a9a0c3SDan Williams int srcu_idx; 1154b3a9a0c3SDan Williams 1155b3a9a0c3SDan Williams ti = dm_dax_get_live_target(md, sector, &srcu_idx); 1156b3a9a0c3SDan Williams 1157b3a9a0c3SDan Williams if (!ti) 1158b3a9a0c3SDan Williams goto out; 1159b3a9a0c3SDan Williams if (!ti->type->dax_copy_to_iter) { 1160b3a9a0c3SDan Williams ret = copy_to_iter(addr, bytes, i); 1161b3a9a0c3SDan Williams goto out; 1162b3a9a0c3SDan Williams } 1163b3a9a0c3SDan Williams ret = ti->type->dax_copy_to_iter(ti, pgoff, addr, bytes, i); 1164b3a9a0c3SDan Williams out: 1165b3a9a0c3SDan Williams dm_put_live_table(md, srcu_idx); 1166b3a9a0c3SDan Williams 1167b3a9a0c3SDan Williams return ret; 1168b3a9a0c3SDan Williams } 1169b3a9a0c3SDan Williams 11701dd40c3eSMikulas Patocka /* 11711dd40c3eSMikulas Patocka * A target may call dm_accept_partial_bio only from the map routine. It is 1172c06b3e58SNeilBrown * allowed for all bio types except REQ_PREFLUSH and REQ_OP_ZONE_RESET. 11731dd40c3eSMikulas Patocka * 11741dd40c3eSMikulas Patocka * dm_accept_partial_bio informs the dm that the target only wants to process 11751dd40c3eSMikulas Patocka * additional n_sectors sectors of the bio and the rest of the data should be 11761dd40c3eSMikulas Patocka * sent in a next bio. 11771dd40c3eSMikulas Patocka * 11781dd40c3eSMikulas Patocka * A diagram that explains the arithmetics: 11791dd40c3eSMikulas Patocka * +--------------------+---------------+-------+ 11801dd40c3eSMikulas Patocka * | 1 | 2 | 3 | 11811dd40c3eSMikulas Patocka * +--------------------+---------------+-------+ 11821dd40c3eSMikulas Patocka * 11831dd40c3eSMikulas Patocka * <-------------- *tio->len_ptr ---------------> 11841dd40c3eSMikulas Patocka * <------- bi_size -------> 11851dd40c3eSMikulas Patocka * <-- n_sectors --> 11861dd40c3eSMikulas Patocka * 11871dd40c3eSMikulas Patocka * Region 1 was already iterated over with bio_advance or similar function. 11881dd40c3eSMikulas Patocka * (it may be empty if the target doesn't use bio_advance) 11891dd40c3eSMikulas Patocka * Region 2 is the remaining bio size that the target wants to process. 11901dd40c3eSMikulas Patocka * (it may be empty if region 1 is non-empty, although there is no reason 11911dd40c3eSMikulas Patocka * to make it empty) 11921dd40c3eSMikulas Patocka * The target requires that region 3 is to be sent in the next bio. 11931dd40c3eSMikulas Patocka * 11941dd40c3eSMikulas Patocka * If the target wants to receive multiple copies of the bio (via num_*bios, etc), 11951dd40c3eSMikulas Patocka * the partially processed part (the sum of regions 1+2) must be the same for all 11961dd40c3eSMikulas Patocka * copies of the bio. 11971dd40c3eSMikulas Patocka */ 11981dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors) 11991dd40c3eSMikulas Patocka { 12001dd40c3eSMikulas Patocka struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); 12011dd40c3eSMikulas Patocka unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT; 12021eff9d32SJens Axboe BUG_ON(bio->bi_opf & REQ_PREFLUSH); 12031dd40c3eSMikulas Patocka BUG_ON(bi_size > *tio->len_ptr); 12041dd40c3eSMikulas Patocka BUG_ON(n_sectors > bi_size); 12051dd40c3eSMikulas Patocka *tio->len_ptr -= bi_size - n_sectors; 12061dd40c3eSMikulas Patocka bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT; 12071dd40c3eSMikulas Patocka } 12081dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio); 12091dd40c3eSMikulas Patocka 1210d67a5f4bSMikulas Patocka /* 1211e76239a3SChristoph Hellwig * The zone descriptors obtained with a zone report indicate 1212e76239a3SChristoph Hellwig * zone positions within the underlying device of the target. The zone 1213e76239a3SChristoph Hellwig * descriptors must be remapped to match their position within the dm device. 1214e76239a3SChristoph Hellwig * The caller target should obtain the zones information using 1215e76239a3SChristoph Hellwig * blkdev_report_zones() to ensure that remapping for partition offset is 1216e76239a3SChristoph Hellwig * already handled. 121710999307SDamien Le Moal */ 1218e76239a3SChristoph Hellwig void dm_remap_zone_report(struct dm_target *ti, sector_t start, 1219e76239a3SChristoph Hellwig struct blk_zone *zones, unsigned int *nr_zones) 122010999307SDamien Le Moal { 122110999307SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED 122210999307SDamien Le Moal struct blk_zone *zone; 1223e76239a3SChristoph Hellwig unsigned int nrz = *nr_zones; 1224e76239a3SChristoph Hellwig int i; 122510999307SDamien Le Moal 122610999307SDamien Le Moal /* 1227e76239a3SChristoph Hellwig * Remap the start sector and write pointer position of the zones in 1228e76239a3SChristoph Hellwig * the array. Since we may have obtained from the target underlying 1229e76239a3SChristoph Hellwig * device more zones that the target size, also adjust the number 1230e76239a3SChristoph Hellwig * of zones. 12319864cd5dSDamien Le Moal */ 1232e76239a3SChristoph Hellwig for (i = 0; i < nrz; i++) { 1233e76239a3SChristoph Hellwig zone = zones + i; 123410999307SDamien Le Moal if (zone->start >= start + ti->len) { 1235e76239a3SChristoph Hellwig memset(zone, 0, sizeof(struct blk_zone) * (nrz - i)); 123610999307SDamien Le Moal break; 123710999307SDamien Le Moal } 1238e76239a3SChristoph Hellwig 123910999307SDamien Le Moal zone->start = zone->start + ti->begin - start; 1240e76239a3SChristoph Hellwig if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL) 1241e76239a3SChristoph Hellwig continue; 1242e76239a3SChristoph Hellwig 124310999307SDamien Le Moal if (zone->cond == BLK_ZONE_COND_FULL) 124410999307SDamien Le Moal zone->wp = zone->start + zone->len; 124510999307SDamien Le Moal else if (zone->cond == BLK_ZONE_COND_EMPTY) 124610999307SDamien Le Moal zone->wp = zone->start; 124710999307SDamien Le Moal else 1248e76239a3SChristoph Hellwig zone->wp = zone->wp + ti->begin - start; 124910999307SDamien Le Moal } 125010999307SDamien Le Moal 1251e76239a3SChristoph Hellwig *nr_zones = i; 125210999307SDamien Le Moal #else /* !CONFIG_BLK_DEV_ZONED */ 1253e76239a3SChristoph Hellwig *nr_zones = 0; 125410999307SDamien Le Moal #endif 125510999307SDamien Le Moal } 125610999307SDamien Le Moal EXPORT_SYMBOL_GPL(dm_remap_zone_report); 125710999307SDamien Le Moal 1258978e51baSMike Snitzer static blk_qc_t __map_bio(struct dm_target_io *tio) 12591da177e4SLinus Torvalds { 12601da177e4SLinus Torvalds int r; 12612056a782SJens Axboe sector_t sector; 1262dba14160SMikulas Patocka struct bio *clone = &tio->clone; 126364f52b0eSMike Snitzer struct dm_io *io = tio->io; 1264978e51baSMike Snitzer struct mapped_device *md = io->md; 1265bd2a49b8SAlasdair G Kergon struct dm_target *ti = tio->ti; 1266978e51baSMike Snitzer blk_qc_t ret = BLK_QC_T_NONE; 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds clone->bi_end_io = clone_endio; 12691da177e4SLinus Torvalds 12701da177e4SLinus Torvalds /* 12711da177e4SLinus Torvalds * Map the clone. If r == 0 we don't need to do 12721da177e4SLinus Torvalds * anything, the target has assumed ownership of 12731da177e4SLinus Torvalds * this io. 12741da177e4SLinus Torvalds */ 127564f52b0eSMike Snitzer atomic_inc(&io->io_count); 12764f024f37SKent Overstreet sector = clone->bi_iter.bi_sector; 1277d67a5f4bSMikulas Patocka 12787de3ee57SMikulas Patocka r = ti->type->map(ti, clone); 1279846785e6SChristoph Hellwig switch (r) { 1280846785e6SChristoph Hellwig case DM_MAPIO_SUBMITTED: 1281846785e6SChristoph Hellwig break; 1282846785e6SChristoph Hellwig case DM_MAPIO_REMAPPED: 12831da177e4SLinus Torvalds /* the bio has been remapped so dispatch it */ 128474d46992SChristoph Hellwig trace_block_bio_remap(clone->bi_disk->queue, clone, 128564f52b0eSMike Snitzer bio_dev(io->orig_bio), sector); 1286978e51baSMike Snitzer if (md->type == DM_TYPE_NVME_BIO_BASED) 1287978e51baSMike Snitzer ret = direct_make_request(clone); 1288978e51baSMike Snitzer else 1289978e51baSMike Snitzer ret = generic_make_request(clone); 1290846785e6SChristoph Hellwig break; 1291846785e6SChristoph Hellwig case DM_MAPIO_KILL: 12924e4cbee9SChristoph Hellwig free_tio(tio); 129364f52b0eSMike Snitzer dec_pending(io, BLK_STS_IOERR); 12944e4cbee9SChristoph Hellwig break; 1295846785e6SChristoph Hellwig case DM_MAPIO_REQUEUE: 1296cfae7529SMike Snitzer free_tio(tio); 129764f52b0eSMike Snitzer dec_pending(io, BLK_STS_DM_REQUEUE); 1298846785e6SChristoph Hellwig break; 1299846785e6SChristoph Hellwig default: 130045cbcd79SKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 130145cbcd79SKiyoshi Ueda BUG(); 13021da177e4SLinus Torvalds } 13031da177e4SLinus Torvalds 1304978e51baSMike Snitzer return ret; 13051da177e4SLinus Torvalds } 13061da177e4SLinus Torvalds 1307e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len) 1308bd2a49b8SAlasdair G Kergon { 13094f024f37SKent Overstreet bio->bi_iter.bi_sector = sector; 13104f024f37SKent Overstreet bio->bi_iter.bi_size = to_bytes(len); 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds /* 13141da177e4SLinus Torvalds * Creates a bio that consists of range of complete bvecs. 13151da177e4SLinus Torvalds */ 1316c80914e8SMike Snitzer static int clone_bio(struct dm_target_io *tio, struct bio *bio, 13171c3b13e6SKent Overstreet sector_t sector, unsigned len) 13181da177e4SLinus Torvalds { 1319dba14160SMikulas Patocka struct bio *clone = &tio->clone; 13201da177e4SLinus Torvalds 13211c3b13e6SKent Overstreet __bio_clone_fast(clone, bio); 13229c47008dSMartin K. Petersen 132357c36519SMike Snitzer if (bio_integrity(bio)) { 1324e2460f2aSMikulas Patocka int r; 1325e2460f2aSMikulas Patocka 1326e2460f2aSMikulas Patocka if (unlikely(!dm_target_has_integrity(tio->ti->type) && 1327e2460f2aSMikulas Patocka !dm_target_passes_integrity(tio->ti->type))) { 1328e2460f2aSMikulas Patocka DMWARN("%s: the target %s doesn't support integrity data.", 1329e2460f2aSMikulas Patocka dm_device_name(tio->io->md), 1330e2460f2aSMikulas Patocka tio->ti->type->name); 1331e2460f2aSMikulas Patocka return -EIO; 1332e2460f2aSMikulas Patocka } 1333e2460f2aSMikulas Patocka 1334e2460f2aSMikulas Patocka r = bio_integrity_clone(clone, bio, GFP_NOIO); 1335c80914e8SMike Snitzer if (r < 0) 1336c80914e8SMike Snitzer return r; 1337c80914e8SMike Snitzer } 13381c3b13e6SKent Overstreet 133957c36519SMike Snitzer bio_trim(clone, sector - clone->bi_iter.bi_sector, len); 1340c80914e8SMike Snitzer 1341c80914e8SMike Snitzer return 0; 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds 1344318716ddSMike Snitzer static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci, 1345318716ddSMike Snitzer struct dm_target *ti, unsigned num_bios) 1346f9ab94ceSMikulas Patocka { 1347dba14160SMikulas Patocka struct dm_target_io *tio; 1348318716ddSMike Snitzer int try; 1349dba14160SMikulas Patocka 1350318716ddSMike Snitzer if (!num_bios) 1351318716ddSMike Snitzer return; 1352f9ab94ceSMikulas Patocka 1353318716ddSMike Snitzer if (num_bios == 1) { 1354318716ddSMike Snitzer tio = alloc_tio(ci, ti, 0, GFP_NOIO); 1355318716ddSMike Snitzer bio_list_add(blist, &tio->clone); 1356318716ddSMike Snitzer return; 13579015df24SAlasdair G Kergon } 13589015df24SAlasdair G Kergon 1359318716ddSMike Snitzer for (try = 0; try < 2; try++) { 1360318716ddSMike Snitzer int bio_nr; 1361318716ddSMike Snitzer struct bio *bio; 1362318716ddSMike Snitzer 1363318716ddSMike Snitzer if (try) 1364bc02cdbeSMike Snitzer mutex_lock(&ci->io->md->table_devices_lock); 1365318716ddSMike Snitzer for (bio_nr = 0; bio_nr < num_bios; bio_nr++) { 1366318716ddSMike Snitzer tio = alloc_tio(ci, ti, bio_nr, try ? GFP_NOIO : GFP_NOWAIT); 1367318716ddSMike Snitzer if (!tio) 1368318716ddSMike Snitzer break; 1369318716ddSMike Snitzer 1370318716ddSMike Snitzer bio_list_add(blist, &tio->clone); 1371318716ddSMike Snitzer } 1372318716ddSMike Snitzer if (try) 1373bc02cdbeSMike Snitzer mutex_unlock(&ci->io->md->table_devices_lock); 1374318716ddSMike Snitzer if (bio_nr == num_bios) 1375318716ddSMike Snitzer return; 1376318716ddSMike Snitzer 1377318716ddSMike Snitzer while ((bio = bio_list_pop(blist))) { 1378318716ddSMike Snitzer tio = container_of(bio, struct dm_target_io, clone); 1379318716ddSMike Snitzer free_tio(tio); 1380318716ddSMike Snitzer } 1381318716ddSMike Snitzer } 1382318716ddSMike Snitzer } 1383318716ddSMike Snitzer 1384978e51baSMike Snitzer static blk_qc_t __clone_and_map_simple_bio(struct clone_info *ci, 1385318716ddSMike Snitzer struct dm_target_io *tio, unsigned *len) 13869015df24SAlasdair G Kergon { 1387dba14160SMikulas Patocka struct bio *clone = &tio->clone; 13889015df24SAlasdair G Kergon 13891dd40c3eSMikulas Patocka tio->len_ptr = len; 13901dd40c3eSMikulas Patocka 13911c3b13e6SKent Overstreet __bio_clone_fast(clone, ci->bio); 1392bd2a49b8SAlasdair G Kergon if (len) 13931dd40c3eSMikulas Patocka bio_setup_sector(clone, ci->sector, *len); 1394f9ab94ceSMikulas Patocka 1395978e51baSMike Snitzer return __map_bio(tio); 1396f9ab94ceSMikulas Patocka } 1397f9ab94ceSMikulas Patocka 139814fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti, 13991dd40c3eSMikulas Patocka unsigned num_bios, unsigned *len) 140006a426ceSMike Snitzer { 1401318716ddSMike Snitzer struct bio_list blist = BIO_EMPTY_LIST; 1402318716ddSMike Snitzer struct bio *bio; 1403318716ddSMike Snitzer struct dm_target_io *tio; 140406a426ceSMike Snitzer 1405318716ddSMike Snitzer alloc_multiple_bios(&blist, ci, ti, num_bios); 1406318716ddSMike Snitzer 1407318716ddSMike Snitzer while ((bio = bio_list_pop(&blist))) { 1408318716ddSMike Snitzer tio = container_of(bio, struct dm_target_io, clone); 1409978e51baSMike Snitzer (void) __clone_and_map_simple_bio(ci, tio, len); 1410318716ddSMike Snitzer } 141106a426ceSMike Snitzer } 141206a426ceSMike Snitzer 141314fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci) 1414f9ab94ceSMikulas Patocka { 141506a426ceSMike Snitzer unsigned target_nr = 0; 1416f9ab94ceSMikulas Patocka struct dm_target *ti; 1417f9ab94ceSMikulas Patocka 1418892ad71fSDennis Zhou /* 1419dbe3ece1SJens Axboe * Empty flush uses a statically initialized bio, as the base for 1420dbe3ece1SJens Axboe * cloning. However, blkg association requires that a bdev is 1421dbe3ece1SJens Axboe * associated with a gendisk, which doesn't happen until the bdev is 1422dbe3ece1SJens Axboe * opened. So, blkg association is done at issue time of the flush 1423dbe3ece1SJens Axboe * rather than when the device is created in alloc_dev(). 1424892ad71fSDennis Zhou */ 1425892ad71fSDennis Zhou bio_set_dev(ci->bio, ci->io->md->bdev); 1426892ad71fSDennis Zhou 1427b372d360SMike Snitzer BUG_ON(bio_has_data(ci->bio)); 1428f9ab94ceSMikulas Patocka while ((ti = dm_table_get_target(ci->map, target_nr++))) 14291dd40c3eSMikulas Patocka __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL); 1430f9ab94ceSMikulas Patocka 1431892ad71fSDennis Zhou bio_disassociate_blkg(ci->bio); 1432892ad71fSDennis Zhou 1433f9ab94ceSMikulas Patocka return 0; 1434f9ab94ceSMikulas Patocka } 1435f9ab94ceSMikulas Patocka 1436c80914e8SMike Snitzer static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti, 14371dd40c3eSMikulas Patocka sector_t sector, unsigned *len) 14385ae89a87SMike Snitzer { 1439dba14160SMikulas Patocka struct bio *bio = ci->bio; 14405ae89a87SMike Snitzer struct dm_target_io *tio; 1441f31c21e4SNeilBrown int r; 14425ae89a87SMike Snitzer 1443318716ddSMike Snitzer tio = alloc_tio(ci, ti, 0, GFP_NOIO); 14441dd40c3eSMikulas Patocka tio->len_ptr = len; 1445c80914e8SMike Snitzer r = clone_bio(tio, bio, sector, *len); 1446072623deSMikulas Patocka if (r < 0) { 1447cfae7529SMike Snitzer free_tio(tio); 1448c80914e8SMike Snitzer return r; 1449b0d8ed4dSAlasdair G Kergon } 1450978e51baSMike Snitzer (void) __map_bio(tio); 145155a62eefSAlasdair G Kergon 1452f31c21e4SNeilBrown return 0; 145323508a96SMike Snitzer } 145455a62eefSAlasdair G Kergon 145523508a96SMike Snitzer typedef unsigned (*get_num_bios_fn)(struct dm_target *ti); 145655a62eefSAlasdair G Kergon 145723508a96SMike Snitzer static unsigned get_num_discard_bios(struct dm_target *ti) 145823508a96SMike Snitzer { 145923508a96SMike Snitzer return ti->num_discard_bios; 146023508a96SMike Snitzer } 146123508a96SMike Snitzer 146200716545SDenis Semakin static unsigned get_num_secure_erase_bios(struct dm_target *ti) 146300716545SDenis Semakin { 146400716545SDenis Semakin return ti->num_secure_erase_bios; 146500716545SDenis Semakin } 146600716545SDenis Semakin 146723508a96SMike Snitzer static unsigned get_num_write_same_bios(struct dm_target *ti) 146823508a96SMike Snitzer { 146923508a96SMike Snitzer return ti->num_write_same_bios; 147023508a96SMike Snitzer } 147123508a96SMike Snitzer 1472ac62d620SChristoph Hellwig static unsigned get_num_write_zeroes_bios(struct dm_target *ti) 1473ac62d620SChristoph Hellwig { 1474ac62d620SChristoph Hellwig return ti->num_write_zeroes_bios; 1475ac62d620SChristoph Hellwig } 1476ac62d620SChristoph Hellwig 147723508a96SMike Snitzer typedef bool (*is_split_required_fn)(struct dm_target *ti); 147823508a96SMike Snitzer 147923508a96SMike Snitzer static bool is_split_required_for_discard(struct dm_target *ti) 148023508a96SMike Snitzer { 148155a62eefSAlasdair G Kergon return ti->split_discard_bios; 148223508a96SMike Snitzer } 148323508a96SMike Snitzer 14843d7f4562SMike Snitzer static int __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti, 148553b47168SMike Snitzer unsigned num_bios, bool is_split_required) 14865ae89a87SMike Snitzer { 1487e0d6609aSMikulas Patocka unsigned len; 14885ae89a87SMike Snitzer 14895ae89a87SMike Snitzer /* 149023508a96SMike Snitzer * Even though the device advertised support for this type of 149123508a96SMike Snitzer * request, that does not mean every target supports it, and 1492936688d7SMike Snitzer * reconfiguration might also have changed that since the 14935ae89a87SMike Snitzer * check was performed. 14945ae89a87SMike Snitzer */ 149555a62eefSAlasdair G Kergon if (!num_bios) 14965ae89a87SMike Snitzer return -EOPNOTSUPP; 14975ae89a87SMike Snitzer 149853b47168SMike Snitzer if (!is_split_required) 1499e0d6609aSMikulas Patocka len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti)); 15007acf0277SMikulas Patocka else 1501e0d6609aSMikulas Patocka len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti)); 15025ae89a87SMike Snitzer 15031dd40c3eSMikulas Patocka __send_duplicate_bios(ci, ti, num_bios, &len); 15045ae89a87SMike Snitzer 1505a79245b3SMike Snitzer ci->sector += len; 15063d7f4562SMike Snitzer ci->sector_count -= len; 15075ae89a87SMike Snitzer 15085ae89a87SMike Snitzer return 0; 15095ae89a87SMike Snitzer } 15105ae89a87SMike Snitzer 15113d7f4562SMike Snitzer static int __send_discard(struct clone_info *ci, struct dm_target *ti) 151223508a96SMike Snitzer { 151353b47168SMike Snitzer return __send_changing_extent_only(ci, ti, get_num_discard_bios(ti), 151453b47168SMike Snitzer is_split_required_for_discard(ti)); 151523508a96SMike Snitzer } 151623508a96SMike Snitzer 151700716545SDenis Semakin static int __send_secure_erase(struct clone_info *ci, struct dm_target *ti) 151800716545SDenis Semakin { 151953b47168SMike Snitzer return __send_changing_extent_only(ci, ti, get_num_secure_erase_bios(ti), false); 152000716545SDenis Semakin } 152100716545SDenis Semakin 15223d7f4562SMike Snitzer static int __send_write_same(struct clone_info *ci, struct dm_target *ti) 152323508a96SMike Snitzer { 152453b47168SMike Snitzer return __send_changing_extent_only(ci, ti, get_num_write_same_bios(ti), false); 152523508a96SMike Snitzer } 152623508a96SMike Snitzer 15273d7f4562SMike Snitzer static int __send_write_zeroes(struct clone_info *ci, struct dm_target *ti) 1528ac62d620SChristoph Hellwig { 152953b47168SMike Snitzer return __send_changing_extent_only(ci, ti, get_num_write_zeroes_bios(ti), false); 1530ac62d620SChristoph Hellwig } 1531ac62d620SChristoph Hellwig 15320519c71eSMike Snitzer static bool __process_abnormal_io(struct clone_info *ci, struct dm_target *ti, 15330519c71eSMike Snitzer int *result) 15340519c71eSMike Snitzer { 15350519c71eSMike Snitzer struct bio *bio = ci->bio; 15360519c71eSMike Snitzer 15370519c71eSMike Snitzer if (bio_op(bio) == REQ_OP_DISCARD) 15380519c71eSMike Snitzer *result = __send_discard(ci, ti); 153900716545SDenis Semakin else if (bio_op(bio) == REQ_OP_SECURE_ERASE) 154000716545SDenis Semakin *result = __send_secure_erase(ci, ti); 15410519c71eSMike Snitzer else if (bio_op(bio) == REQ_OP_WRITE_SAME) 15420519c71eSMike Snitzer *result = __send_write_same(ci, ti); 15430519c71eSMike Snitzer else if (bio_op(bio) == REQ_OP_WRITE_ZEROES) 15440519c71eSMike Snitzer *result = __send_write_zeroes(ci, ti); 15450519c71eSMike Snitzer else 15460519c71eSMike Snitzer return false; 15470519c71eSMike Snitzer 15480519c71eSMike Snitzer return true; 15490519c71eSMike Snitzer } 15500519c71eSMike Snitzer 1551e4c93811SAlasdair G Kergon /* 1552e4c93811SAlasdair G Kergon * Select the correct strategy for processing a non-flush bio. 1553e4c93811SAlasdair G Kergon */ 1554e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci) 1555e4c93811SAlasdair G Kergon { 1556e4c93811SAlasdair G Kergon struct dm_target *ti; 15571c3b13e6SKent Overstreet unsigned len; 1558c80914e8SMike Snitzer int r; 1559e4c93811SAlasdair G Kergon 1560e4c93811SAlasdair G Kergon ti = dm_table_find_target(ci->map, ci->sector); 1561e4c93811SAlasdair G Kergon if (!dm_target_is_valid(ti)) 1562e4c93811SAlasdair G Kergon return -EIO; 1563e4c93811SAlasdair G Kergon 15640519c71eSMike Snitzer if (unlikely(__process_abnormal_io(ci, ti, &r))) 15650519c71eSMike Snitzer return r; 15663d7f4562SMike Snitzer 1567e76239a3SChristoph Hellwig len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count); 1568e4c93811SAlasdair G Kergon 1569c80914e8SMike Snitzer r = __clone_and_map_data_bio(ci, ti, ci->sector, &len); 1570c80914e8SMike Snitzer if (r < 0) 1571c80914e8SMike Snitzer return r; 1572e4c93811SAlasdair G Kergon 1573e4c93811SAlasdair G Kergon ci->sector += len; 1574e4c93811SAlasdair G Kergon ci->sector_count -= len; 1575e4c93811SAlasdair G Kergon 1576e4c93811SAlasdair G Kergon return 0; 1577e4c93811SAlasdair G Kergon } 1578e4c93811SAlasdair G Kergon 1579978e51baSMike Snitzer static void init_clone_info(struct clone_info *ci, struct mapped_device *md, 1580978e51baSMike Snitzer struct dm_table *map, struct bio *bio) 1581978e51baSMike Snitzer { 1582978e51baSMike Snitzer ci->map = map; 1583978e51baSMike Snitzer ci->io = alloc_io(md, bio); 1584978e51baSMike Snitzer ci->sector = bio->bi_iter.bi_sector; 1585978e51baSMike Snitzer } 1586978e51baSMike Snitzer 1587a1e1cb72SMike Snitzer #define __dm_part_stat_sub(part, field, subnd) \ 1588a1e1cb72SMike Snitzer (part_stat_get(part, field) -= (subnd)) 1589a1e1cb72SMike Snitzer 1590e4c93811SAlasdair G Kergon /* 159114fe594dSAlasdair G Kergon * Entry point to split a bio into clones and submit them to the targets. 15921da177e4SLinus Torvalds */ 1593978e51baSMike Snitzer static blk_qc_t __split_and_process_bio(struct mapped_device *md, 159483d5e5b0SMikulas Patocka struct dm_table *map, struct bio *bio) 15951da177e4SLinus Torvalds { 15961da177e4SLinus Torvalds struct clone_info ci; 1597978e51baSMike Snitzer blk_qc_t ret = BLK_QC_T_NONE; 1598512875bdSJun'ichi Nomura int error = 0; 15991da177e4SLinus Torvalds 160083d5e5b0SMikulas Patocka if (unlikely(!map)) { 1601f0b9a450SMikulas Patocka bio_io_error(bio); 1602978e51baSMike Snitzer return ret; 1603f0b9a450SMikulas Patocka } 1604692d0eb9SMikulas Patocka 160589f5fa47SMike Snitzer blk_queue_split(md->queue, &bio); 160689f5fa47SMike Snitzer 1607978e51baSMike Snitzer init_clone_info(&ci, md, map, bio); 1608bd2a49b8SAlasdair G Kergon 16091eff9d32SJens Axboe if (bio->bi_opf & REQ_PREFLUSH) { 1610dbe3ece1SJens Axboe struct bio flush_bio; 1611dbe3ece1SJens Axboe 1612dbe3ece1SJens Axboe /* 1613dbe3ece1SJens Axboe * Use an on-stack bio for this, it's safe since we don't 1614dbe3ece1SJens Axboe * need to reference it after submit. It's just used as 1615dbe3ece1SJens Axboe * the basis for the clone(s). 1616dbe3ece1SJens Axboe */ 1617dbe3ece1SJens Axboe bio_init(&flush_bio, NULL, 0); 1618dbe3ece1SJens Axboe flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC; 1619dbe3ece1SJens Axboe ci.bio = &flush_bio; 1620b372d360SMike Snitzer ci.sector_count = 0; 162114fe594dSAlasdair G Kergon error = __send_empty_flush(&ci); 1622b372d360SMike Snitzer /* dec_pending submits any data associated with flush */ 1623a4aa5e56SDamien Le Moal } else if (bio_op(bio) == REQ_OP_ZONE_RESET) { 1624a4aa5e56SDamien Le Moal ci.bio = bio; 1625a4aa5e56SDamien Le Moal ci.sector_count = 0; 1626a4aa5e56SDamien Le Moal error = __split_and_process_non_flush(&ci); 1627b372d360SMike Snitzer } else { 16286a8736d1STejun Heo ci.bio = bio; 16291da177e4SLinus Torvalds ci.sector_count = bio_sectors(bio); 163018a25da8SNeilBrown while (ci.sector_count && !error) { 163114fe594dSAlasdair G Kergon error = __split_and_process_non_flush(&ci); 163218a25da8SNeilBrown if (current->bio_list && ci.sector_count && !error) { 163318a25da8SNeilBrown /* 163418a25da8SNeilBrown * Remainder must be passed to generic_make_request() 163518a25da8SNeilBrown * so that it gets handled *after* bios already submitted 163618a25da8SNeilBrown * have been completely processed. 163718a25da8SNeilBrown * We take a clone of the original to store in 1638745dc570SMike Snitzer * ci.io->orig_bio to be used by end_io_acct() and 163918a25da8SNeilBrown * for dec_pending to use for completion handling. 164018a25da8SNeilBrown */ 1641f21c601aSMike Snitzer struct bio *b = bio_split(bio, bio_sectors(bio) - ci.sector_count, 1642f21c601aSMike Snitzer GFP_NOIO, &md->queue->bio_split); 1643745dc570SMike Snitzer ci.io->orig_bio = b; 1644a1e1cb72SMike Snitzer 1645a1e1cb72SMike Snitzer /* 1646a1e1cb72SMike Snitzer * Adjust IO stats for each split, otherwise upon queue 1647a1e1cb72SMike Snitzer * reentry there will be redundant IO accounting. 1648a1e1cb72SMike Snitzer * NOTE: this is a stop-gap fix, a proper fix involves 1649a1e1cb72SMike Snitzer * significant refactoring of DM core's bio splitting 1650a1e1cb72SMike Snitzer * (by eliminating DM's splitting and just using bio_split) 1651a1e1cb72SMike Snitzer */ 1652a1e1cb72SMike Snitzer part_stat_lock(); 1653a1e1cb72SMike Snitzer __dm_part_stat_sub(&dm_disk(md)->part0, 1654a1e1cb72SMike Snitzer sectors[op_stat_group(bio_op(bio))], ci.sector_count); 1655a1e1cb72SMike Snitzer part_stat_unlock(); 1656a1e1cb72SMike Snitzer 165718a25da8SNeilBrown bio_chain(b, bio); 1658075c18c3SMike Snitzer trace_block_split(md->queue, b, bio->bi_iter.bi_sector); 1659978e51baSMike Snitzer ret = generic_make_request(bio); 166018a25da8SNeilBrown break; 166118a25da8SNeilBrown } 166218a25da8SNeilBrown } 1663d87f4c14STejun Heo } 16641da177e4SLinus Torvalds 16651da177e4SLinus Torvalds /* drop the extra reference count */ 166654385bf7SBart Van Assche dec_pending(ci.io, errno_to_blk_status(error)); 1667978e51baSMike Snitzer return ret; 16681da177e4SLinus Torvalds } 16691da177e4SLinus Torvalds 16701da177e4SLinus Torvalds /* 1671978e51baSMike Snitzer * Optimized variant of __split_and_process_bio that leverages the 1672978e51baSMike Snitzer * fact that targets that use it do _not_ have a need to split bios. 16731da177e4SLinus Torvalds */ 1674978e51baSMike Snitzer static blk_qc_t __process_bio(struct mapped_device *md, 1675978e51baSMike Snitzer struct dm_table *map, struct bio *bio) 16761da177e4SLinus Torvalds { 1677978e51baSMike Snitzer struct clone_info ci; 1678978e51baSMike Snitzer blk_qc_t ret = BLK_QC_T_NONE; 1679978e51baSMike Snitzer int error = 0; 1680978e51baSMike Snitzer 1681978e51baSMike Snitzer if (unlikely(!map)) { 1682978e51baSMike Snitzer bio_io_error(bio); 1683978e51baSMike Snitzer return ret; 1684978e51baSMike Snitzer } 1685978e51baSMike Snitzer 1686978e51baSMike Snitzer init_clone_info(&ci, md, map, bio); 1687978e51baSMike Snitzer 1688978e51baSMike Snitzer if (bio->bi_opf & REQ_PREFLUSH) { 1689dbe3ece1SJens Axboe struct bio flush_bio; 1690dbe3ece1SJens Axboe 1691dbe3ece1SJens Axboe /* 1692dbe3ece1SJens Axboe * Use an on-stack bio for this, it's safe since we don't 1693dbe3ece1SJens Axboe * need to reference it after submit. It's just used as 1694dbe3ece1SJens Axboe * the basis for the clone(s). 1695dbe3ece1SJens Axboe */ 1696dbe3ece1SJens Axboe bio_init(&flush_bio, NULL, 0); 1697dbe3ece1SJens Axboe flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC; 1698dbe3ece1SJens Axboe ci.bio = &flush_bio; 1699978e51baSMike Snitzer ci.sector_count = 0; 1700978e51baSMike Snitzer error = __send_empty_flush(&ci); 1701978e51baSMike Snitzer /* dec_pending submits any data associated with flush */ 1702978e51baSMike Snitzer } else { 1703978e51baSMike Snitzer struct dm_target *ti = md->immutable_target; 1704978e51baSMike Snitzer struct dm_target_io *tio; 1705978e51baSMike Snitzer 1706978e51baSMike Snitzer /* 1707978e51baSMike Snitzer * Defend against IO still getting in during teardown 1708978e51baSMike Snitzer * - as was seen for a time with nvme-fcloop 1709978e51baSMike Snitzer */ 1710bab5d988SIgor Stoppa if (WARN_ON_ONCE(!ti || !dm_target_is_valid(ti))) { 1711978e51baSMike Snitzer error = -EIO; 1712978e51baSMike Snitzer goto out; 1713978e51baSMike Snitzer } 1714978e51baSMike Snitzer 1715978e51baSMike Snitzer ci.bio = bio; 1716978e51baSMike Snitzer ci.sector_count = bio_sectors(bio); 17170519c71eSMike Snitzer if (unlikely(__process_abnormal_io(&ci, ti, &error))) 17180519c71eSMike Snitzer goto out; 17190519c71eSMike Snitzer 17200519c71eSMike Snitzer tio = alloc_tio(&ci, ti, 0, GFP_NOIO); 1721978e51baSMike Snitzer ret = __clone_and_map_simple_bio(&ci, tio, NULL); 1722978e51baSMike Snitzer } 1723978e51baSMike Snitzer out: 1724978e51baSMike Snitzer /* drop the extra reference count */ 1725978e51baSMike Snitzer dec_pending(ci.io, errno_to_blk_status(error)); 1726978e51baSMike Snitzer return ret; 1727978e51baSMike Snitzer } 1728978e51baSMike Snitzer 17296548c7c5SMike Snitzer static blk_qc_t dm_process_bio(struct mapped_device *md, 17306548c7c5SMike Snitzer struct dm_table *map, struct bio *bio) 17316548c7c5SMike Snitzer { 17326548c7c5SMike Snitzer if (dm_get_md_type(md) == DM_TYPE_NVME_BIO_BASED) 17336548c7c5SMike Snitzer return __process_bio(md, map, bio); 17346548c7c5SMike Snitzer else 17356548c7c5SMike Snitzer return __split_and_process_bio(md, map, bio); 17366548c7c5SMike Snitzer } 17376548c7c5SMike Snitzer 173824113d48SMikulas Patocka static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio) 17391da177e4SLinus Torvalds { 17401da177e4SLinus Torvalds struct mapped_device *md = q->queuedata; 1741978e51baSMike Snitzer blk_qc_t ret = BLK_QC_T_NONE; 174283d5e5b0SMikulas Patocka int srcu_idx; 174383d5e5b0SMikulas Patocka struct dm_table *map; 17441da177e4SLinus Torvalds 174583d5e5b0SMikulas Patocka map = dm_get_live_table(md, &srcu_idx); 17461da177e4SLinus Torvalds 17476a8736d1STejun Heo /* if we're suspended, we have to queue this io for later */ 17486a8736d1STejun Heo if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) { 174983d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 17501da177e4SLinus Torvalds 17511eff9d32SJens Axboe if (!(bio->bi_opf & REQ_RAHEAD)) 175292c63902SMikulas Patocka queue_io(md, bio); 17536a8736d1STejun Heo else 17546a8736d1STejun Heo bio_io_error(bio); 1755978e51baSMike Snitzer return ret; 17561da177e4SLinus Torvalds } 17571da177e4SLinus Torvalds 17586548c7c5SMike Snitzer ret = dm_process_bio(md, map, bio); 1759978e51baSMike Snitzer 176083d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 1761978e51baSMike Snitzer return ret; 1762978e51baSMike Snitzer } 1763978e51baSMike Snitzer 17641da177e4SLinus Torvalds static int dm_any_congested(void *congested_data, int bdi_bits) 17651da177e4SLinus Torvalds { 17668a57dfc6SChandra Seetharaman int r = bdi_bits; 17678a57dfc6SChandra Seetharaman struct mapped_device *md = congested_data; 17688a57dfc6SChandra Seetharaman struct dm_table *map; 17691da177e4SLinus Torvalds 17701eb787ecSAlasdair G Kergon if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 1771e522c039SMike Snitzer if (dm_request_based(md)) { 1772cec47e3dSKiyoshi Ueda /* 1773e522c039SMike Snitzer * With request-based DM we only need to check the 1774e522c039SMike Snitzer * top-level queue for congestion. 1775cec47e3dSKiyoshi Ueda */ 1776dc3b17ccSJan Kara r = md->queue->backing_dev_info->wb.state & bdi_bits; 1777e522c039SMike Snitzer } else { 1778e522c039SMike Snitzer map = dm_get_live_table_fast(md); 1779e522c039SMike Snitzer if (map) 17801da177e4SLinus Torvalds r = dm_table_any_congested(map, bdi_bits); 178183d5e5b0SMikulas Patocka dm_put_live_table_fast(md); 17828a57dfc6SChandra Seetharaman } 1783e522c039SMike Snitzer } 17848a57dfc6SChandra Seetharaman 17851da177e4SLinus Torvalds return r; 17861da177e4SLinus Torvalds } 17871da177e4SLinus Torvalds 17881da177e4SLinus Torvalds /*----------------------------------------------------------------- 17891da177e4SLinus Torvalds * An IDR is used to keep track of allocated minor numbers. 17901da177e4SLinus Torvalds *---------------------------------------------------------------*/ 17912b06cfffSAlasdair G Kergon static void free_minor(int minor) 17921da177e4SLinus Torvalds { 1793f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 17941da177e4SLinus Torvalds idr_remove(&_minor_idr, minor); 1795f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 17961da177e4SLinus Torvalds } 17971da177e4SLinus Torvalds 17981da177e4SLinus Torvalds /* 17991da177e4SLinus Torvalds * See if the device with a specific minor # is free. 18001da177e4SLinus Torvalds */ 1801cf13ab8eSFrederik Deweerdt static int specific_minor(int minor) 18021da177e4SLinus Torvalds { 1803c9d76be6STejun Heo int r; 18041da177e4SLinus Torvalds 18051da177e4SLinus Torvalds if (minor >= (1 << MINORBITS)) 18061da177e4SLinus Torvalds return -EINVAL; 18071da177e4SLinus Torvalds 1808c9d76be6STejun Heo idr_preload(GFP_KERNEL); 1809f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 18101da177e4SLinus Torvalds 1811c9d76be6STejun Heo r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT); 18121da177e4SLinus Torvalds 1813f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1814c9d76be6STejun Heo idr_preload_end(); 1815c9d76be6STejun Heo if (r < 0) 1816c9d76be6STejun Heo return r == -ENOSPC ? -EBUSY : r; 1817c9d76be6STejun Heo return 0; 18181da177e4SLinus Torvalds } 18191da177e4SLinus Torvalds 1820cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor) 18211da177e4SLinus Torvalds { 1822c9d76be6STejun Heo int r; 18231da177e4SLinus Torvalds 1824c9d76be6STejun Heo idr_preload(GFP_KERNEL); 1825f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 18261da177e4SLinus Torvalds 1827c9d76be6STejun Heo r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT); 18281da177e4SLinus Torvalds 1829f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1830c9d76be6STejun Heo idr_preload_end(); 1831c9d76be6STejun Heo if (r < 0) 18321da177e4SLinus Torvalds return r; 1833c9d76be6STejun Heo *minor = r; 1834c9d76be6STejun Heo return 0; 18351da177e4SLinus Torvalds } 18361da177e4SLinus Torvalds 183783d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops; 1838f26c5719SDan Williams static const struct dax_operations dm_dax_ops; 18391da177e4SLinus Torvalds 184053d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work); 184153d5914fSMikulas Patocka 1842c12c9a3cSMike Snitzer static void dm_init_normal_md_queue(struct mapped_device *md) 1843bfebd1cdSMike Snitzer { 1844bfebd1cdSMike Snitzer /* 1845bfebd1cdSMike Snitzer * Initialize aspects of queue that aren't relevant for blk-mq 1846bfebd1cdSMike Snitzer */ 1847dc3b17ccSJan Kara md->queue->backing_dev_info->congested_fn = dm_any_congested; 18484a0b4ddfSMike Snitzer } 18494a0b4ddfSMike Snitzer 18500f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md) 18510f20972fSMike Snitzer { 18520f20972fSMike Snitzer if (md->wq) 18530f20972fSMike Snitzer destroy_workqueue(md->wq); 18546f1c819cSKent Overstreet bioset_exit(&md->bs); 18556f1c819cSKent Overstreet bioset_exit(&md->io_bs); 18560f20972fSMike Snitzer 1857f26c5719SDan Williams if (md->dax_dev) { 1858f26c5719SDan Williams kill_dax(md->dax_dev); 1859f26c5719SDan Williams put_dax(md->dax_dev); 1860f26c5719SDan Williams md->dax_dev = NULL; 1861f26c5719SDan Williams } 1862f26c5719SDan Williams 18630f20972fSMike Snitzer if (md->disk) { 18640f20972fSMike Snitzer spin_lock(&_minor_lock); 18650f20972fSMike Snitzer md->disk->private_data = NULL; 18660f20972fSMike Snitzer spin_unlock(&_minor_lock); 18670f20972fSMike Snitzer del_gendisk(md->disk); 18680f20972fSMike Snitzer put_disk(md->disk); 18690f20972fSMike Snitzer } 18700f20972fSMike Snitzer 18710f20972fSMike Snitzer if (md->queue) 18720f20972fSMike Snitzer blk_cleanup_queue(md->queue); 18730f20972fSMike Snitzer 1874d09960b0STahsin Erdogan cleanup_srcu_struct(&md->io_barrier); 1875d09960b0STahsin Erdogan 18760f20972fSMike Snitzer if (md->bdev) { 18770f20972fSMike Snitzer bdput(md->bdev); 18780f20972fSMike Snitzer md->bdev = NULL; 18790f20972fSMike Snitzer } 18804cc96131SMike Snitzer 1881d5ffebddSMike Snitzer mutex_destroy(&md->suspend_lock); 1882d5ffebddSMike Snitzer mutex_destroy(&md->type_lock); 1883d5ffebddSMike Snitzer mutex_destroy(&md->table_devices_lock); 1884d5ffebddSMike Snitzer 18854cc96131SMike Snitzer dm_mq_cleanup_mapped_device(md); 18860f20972fSMike Snitzer } 18870f20972fSMike Snitzer 18881da177e4SLinus Torvalds /* 18891da177e4SLinus Torvalds * Allocate and initialise a blank device with a given minor. 18901da177e4SLinus Torvalds */ 18912b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor) 18921da177e4SLinus Torvalds { 1893115485e8SMike Snitzer int r, numa_node_id = dm_get_numa_node(); 1894976431b0SDan Williams struct dax_device *dax_dev = NULL; 1895115485e8SMike Snitzer struct mapped_device *md; 1896ba61fdd1SJeff Mahoney void *old_md; 18971da177e4SLinus Torvalds 1898856eb091SMikulas Patocka md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id); 18991da177e4SLinus Torvalds if (!md) { 19001da177e4SLinus Torvalds DMWARN("unable to allocate device, out of memory."); 19011da177e4SLinus Torvalds return NULL; 19021da177e4SLinus Torvalds } 19031da177e4SLinus Torvalds 190410da4f79SJeff Mahoney if (!try_module_get(THIS_MODULE)) 19056ed7ade8SMilan Broz goto bad_module_get; 190610da4f79SJeff Mahoney 19071da177e4SLinus Torvalds /* get a minor number for the dev */ 19082b06cfffSAlasdair G Kergon if (minor == DM_ANY_MINOR) 1909cf13ab8eSFrederik Deweerdt r = next_free_minor(&minor); 19102b06cfffSAlasdair G Kergon else 1911cf13ab8eSFrederik Deweerdt r = specific_minor(minor); 19121da177e4SLinus Torvalds if (r < 0) 19136ed7ade8SMilan Broz goto bad_minor; 19141da177e4SLinus Torvalds 191583d5e5b0SMikulas Patocka r = init_srcu_struct(&md->io_barrier); 191683d5e5b0SMikulas Patocka if (r < 0) 191783d5e5b0SMikulas Patocka goto bad_io_barrier; 191883d5e5b0SMikulas Patocka 1919115485e8SMike Snitzer md->numa_node_id = numa_node_id; 1920591ddcfcSMike Snitzer md->init_tio_pdu = false; 1921a5664dadSMike Snitzer md->type = DM_TYPE_NONE; 1922e61290a4SDaniel Walker mutex_init(&md->suspend_lock); 1923a5664dadSMike Snitzer mutex_init(&md->type_lock); 192486f1152bSBenjamin Marzinski mutex_init(&md->table_devices_lock); 1925022c2611SMikulas Patocka spin_lock_init(&md->deferred_lock); 19261da177e4SLinus Torvalds atomic_set(&md->holders, 1); 19275c6bd75dSAlasdair G Kergon atomic_set(&md->open_count, 0); 19281da177e4SLinus Torvalds atomic_set(&md->event_nr, 0); 19297a8c3d3bSMike Anderson atomic_set(&md->uevent_seq, 0); 19307a8c3d3bSMike Anderson INIT_LIST_HEAD(&md->uevent_list); 193186f1152bSBenjamin Marzinski INIT_LIST_HEAD(&md->table_devices); 19327a8c3d3bSMike Anderson spin_lock_init(&md->uevent_lock); 19331da177e4SLinus Torvalds 19346d469642SChristoph Hellwig md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id); 19351da177e4SLinus Torvalds if (!md->queue) 19360f20972fSMike Snitzer goto bad; 1937c12c9a3cSMike Snitzer md->queue->queuedata = md; 1938c12c9a3cSMike Snitzer md->queue->backing_dev_info->congested_data = md; 19391da177e4SLinus Torvalds 1940c12c9a3cSMike Snitzer md->disk = alloc_disk_node(1, md->numa_node_id); 19411da177e4SLinus Torvalds if (!md->disk) 19420f20972fSMike Snitzer goto bad; 19431da177e4SLinus Torvalds 1944f0b04115SJeff Mahoney init_waitqueue_head(&md->wait); 194553d5914fSMikulas Patocka INIT_WORK(&md->work, dm_wq_work); 1946f0b04115SJeff Mahoney init_waitqueue_head(&md->eventq); 19472995fa78SMikulas Patocka init_completion(&md->kobj_holder.completion); 1948f0b04115SJeff Mahoney 19491da177e4SLinus Torvalds md->disk->major = _major; 19501da177e4SLinus Torvalds md->disk->first_minor = minor; 19511da177e4SLinus Torvalds md->disk->fops = &dm_blk_dops; 19521da177e4SLinus Torvalds md->disk->queue = md->queue; 19531da177e4SLinus Torvalds md->disk->private_data = md; 19541da177e4SLinus Torvalds sprintf(md->disk->disk_name, "dm-%d", minor); 1955f26c5719SDan Williams 1956976431b0SDan Williams if (IS_ENABLED(CONFIG_DAX_DRIVER)) { 1957f26c5719SDan Williams dax_dev = alloc_dax(md, md->disk->disk_name, &dm_dax_ops); 1958f26c5719SDan Williams if (!dax_dev) 1959f26c5719SDan Williams goto bad; 1960976431b0SDan Williams } 1961f26c5719SDan Williams md->dax_dev = dax_dev; 1962f26c5719SDan Williams 1963c100ec49SMike Snitzer add_disk_no_queue_reg(md->disk); 19647e51f257SMike Anderson format_dev_t(md->name, MKDEV(_major, minor)); 19651da177e4SLinus Torvalds 1966670368a8STejun Heo md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0); 1967304f3f6aSMilan Broz if (!md->wq) 19680f20972fSMike Snitzer goto bad; 1969304f3f6aSMilan Broz 197032a926daSMikulas Patocka md->bdev = bdget_disk(md->disk, 0); 197132a926daSMikulas Patocka if (!md->bdev) 19720f20972fSMike Snitzer goto bad; 197332a926daSMikulas Patocka 1974fd2ed4d2SMikulas Patocka dm_stats_init(&md->stats); 1975fd2ed4d2SMikulas Patocka 1976ba61fdd1SJeff Mahoney /* Populate the mapping, nobody knows we exist yet */ 1977f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 1978ba61fdd1SJeff Mahoney old_md = idr_replace(&_minor_idr, md, minor); 1979f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1980ba61fdd1SJeff Mahoney 1981ba61fdd1SJeff Mahoney BUG_ON(old_md != MINOR_ALLOCED); 1982ba61fdd1SJeff Mahoney 19831da177e4SLinus Torvalds return md; 19841da177e4SLinus Torvalds 19850f20972fSMike Snitzer bad: 19860f20972fSMike Snitzer cleanup_mapped_device(md); 198783d5e5b0SMikulas Patocka bad_io_barrier: 19881da177e4SLinus Torvalds free_minor(minor); 19896ed7ade8SMilan Broz bad_minor: 199010da4f79SJeff Mahoney module_put(THIS_MODULE); 19916ed7ade8SMilan Broz bad_module_get: 1992856eb091SMikulas Patocka kvfree(md); 19931da177e4SLinus Torvalds return NULL; 19941da177e4SLinus Torvalds } 19951da177e4SLinus Torvalds 1996ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md); 1997ae9da83fSJun'ichi Nomura 19981da177e4SLinus Torvalds static void free_dev(struct mapped_device *md) 19991da177e4SLinus Torvalds { 2000f331c029STejun Heo int minor = MINOR(disk_devt(md->disk)); 200163d94e48SJun'ichi Nomura 2002ae9da83fSJun'ichi Nomura unlock_fs(md); 20032eb6e1e3SKeith Busch 20040f20972fSMike Snitzer cleanup_mapped_device(md); 20050f20972fSMike Snitzer 20060f20972fSMike Snitzer free_table_devices(&md->table_devices); 20070f20972fSMike Snitzer dm_stats_cleanup(&md->stats); 200863a4f065SMike Snitzer free_minor(minor); 200963a4f065SMike Snitzer 201010da4f79SJeff Mahoney module_put(THIS_MODULE); 2011856eb091SMikulas Patocka kvfree(md); 20121da177e4SLinus Torvalds } 20131da177e4SLinus Torvalds 20142a2a4c51SJens Axboe static int __bind_mempools(struct mapped_device *md, struct dm_table *t) 2015e6ee8c0bSKiyoshi Ueda { 2016c0820cf5SMikulas Patocka struct dm_md_mempools *p = dm_table_get_md_mempools(t); 20172a2a4c51SJens Axboe int ret = 0; 2018e6ee8c0bSKiyoshi Ueda 2019545ed20eSToshi Kani if (dm_table_bio_based(t)) { 2020c0820cf5SMikulas Patocka /* 202164f52b0eSMike Snitzer * The md may already have mempools that need changing. 202264f52b0eSMike Snitzer * If so, reload bioset because front_pad may have changed 202316245bdcSJun'ichi Nomura * because a different table was loaded. 2024c0820cf5SMikulas Patocka */ 20256f1c819cSKent Overstreet bioset_exit(&md->bs); 20266f1c819cSKent Overstreet bioset_exit(&md->io_bs); 20270776aa0eSMike Snitzer 20286f1c819cSKent Overstreet } else if (bioset_initialized(&md->bs)) { 2029cbc4e3c1SMike Snitzer /* 20304e6e36c3SMike Snitzer * There's no need to reload with request-based dm 20314e6e36c3SMike Snitzer * because the size of front_pad doesn't change. 20324e6e36c3SMike Snitzer * Note for future: If you are to reload bioset, 20334e6e36c3SMike Snitzer * prep-ed requests in the queue may refer 20344e6e36c3SMike Snitzer * to bio from the old bioset, so you must walk 20354e6e36c3SMike Snitzer * through the queue to unprep. 2036cbc4e3c1SMike Snitzer */ 2037cbc4e3c1SMike Snitzer goto out; 2038cbc4e3c1SMike Snitzer } 2039cbc4e3c1SMike Snitzer 20406f1c819cSKent Overstreet BUG_ON(!p || 20416f1c819cSKent Overstreet bioset_initialized(&md->bs) || 20426f1c819cSKent Overstreet bioset_initialized(&md->io_bs)); 2043e6ee8c0bSKiyoshi Ueda 20442a2a4c51SJens Axboe ret = bioset_init_from_src(&md->bs, &p->bs); 20452a2a4c51SJens Axboe if (ret) 20462a2a4c51SJens Axboe goto out; 20472a2a4c51SJens Axboe ret = bioset_init_from_src(&md->io_bs, &p->io_bs); 20482a2a4c51SJens Axboe if (ret) 20492a2a4c51SJens Axboe bioset_exit(&md->bs); 2050e6ee8c0bSKiyoshi Ueda out: 205102233342SMike Snitzer /* mempool bind completed, no longer need any mempools in the table */ 2052e6ee8c0bSKiyoshi Ueda dm_table_free_md_mempools(t); 20532a2a4c51SJens Axboe return ret; 2054e6ee8c0bSKiyoshi Ueda } 2055e6ee8c0bSKiyoshi Ueda 20561da177e4SLinus Torvalds /* 20571da177e4SLinus Torvalds * Bind a table to the device. 20581da177e4SLinus Torvalds */ 20591da177e4SLinus Torvalds static void event_callback(void *context) 20601da177e4SLinus Torvalds { 20617a8c3d3bSMike Anderson unsigned long flags; 20627a8c3d3bSMike Anderson LIST_HEAD(uevents); 20631da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) context; 20641da177e4SLinus Torvalds 20657a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 20667a8c3d3bSMike Anderson list_splice_init(&md->uevent_list, &uevents); 20677a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 20687a8c3d3bSMike Anderson 2069ed9e1982STejun Heo dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); 20707a8c3d3bSMike Anderson 20711da177e4SLinus Torvalds atomic_inc(&md->event_nr); 20721da177e4SLinus Torvalds wake_up(&md->eventq); 207362e08243SMikulas Patocka dm_issue_global_event(); 20741da177e4SLinus Torvalds } 20751da177e4SLinus Torvalds 2076c217649bSMike Snitzer /* 2077c217649bSMike Snitzer * Protected by md->suspend_lock obtained by dm_swap_table(). 2078c217649bSMike Snitzer */ 20794e90188bSAlasdair G Kergon static void __set_size(struct mapped_device *md, sector_t size) 20801da177e4SLinus Torvalds { 20811ea0654eSBart Van Assche lockdep_assert_held(&md->suspend_lock); 20821ea0654eSBart Van Assche 20834e90188bSAlasdair G Kergon set_capacity(md->disk, size); 20841da177e4SLinus Torvalds 2085db8fef4fSMikulas Patocka i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT); 20861da177e4SLinus Torvalds } 20871da177e4SLinus Torvalds 2088042d2a9bSAlasdair G Kergon /* 2089042d2a9bSAlasdair G Kergon * Returns old map, which caller must destroy. 2090042d2a9bSAlasdair G Kergon */ 2091042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t, 2092754c5fc7SMike Snitzer struct queue_limits *limits) 20931da177e4SLinus Torvalds { 2094042d2a9bSAlasdair G Kergon struct dm_table *old_map; 2095165125e1SJens Axboe struct request_queue *q = md->queue; 2096978e51baSMike Snitzer bool request_based = dm_table_request_based(t); 20971da177e4SLinus Torvalds sector_t size; 20982a2a4c51SJens Axboe int ret; 20991da177e4SLinus Torvalds 21005a8f1f80SBart Van Assche lockdep_assert_held(&md->suspend_lock); 21015a8f1f80SBart Van Assche 21021da177e4SLinus Torvalds size = dm_table_get_size(t); 21033ac51e74SDarrick J. Wong 21043ac51e74SDarrick J. Wong /* 21053ac51e74SDarrick J. Wong * Wipe any geometry if the size of the table changed. 21063ac51e74SDarrick J. Wong */ 2107fd2ed4d2SMikulas Patocka if (size != dm_get_size(md)) 21083ac51e74SDarrick J. Wong memset(&md->geometry, 0, sizeof(md->geometry)); 21093ac51e74SDarrick J. Wong 21104e90188bSAlasdair G Kergon __set_size(md, size); 21111da177e4SLinus Torvalds 2112cf222b37SAlasdair G Kergon dm_table_event_callback(t, event_callback, md); 21132ca3310eSAlasdair G Kergon 2114e6ee8c0bSKiyoshi Ueda /* 2115e6ee8c0bSKiyoshi Ueda * The queue hasn't been stopped yet, if the old table type wasn't 2116e6ee8c0bSKiyoshi Ueda * for request-based during suspension. So stop it to prevent 2117e6ee8c0bSKiyoshi Ueda * I/O mapping before resume. 2118e6ee8c0bSKiyoshi Ueda * This must be done before setting the queue restrictions, 2119e6ee8c0bSKiyoshi Ueda * because request-based dm may be run just after the setting. 2120e6ee8c0bSKiyoshi Ueda */ 2121978e51baSMike Snitzer if (request_based) 2122eca7ee6dSMike Snitzer dm_stop_queue(q); 2123978e51baSMike Snitzer 2124978e51baSMike Snitzer if (request_based || md->type == DM_TYPE_NVME_BIO_BASED) { 212516f12266SMike Snitzer /* 2126978e51baSMike Snitzer * Leverage the fact that request-based DM targets and 2127978e51baSMike Snitzer * NVMe bio based targets are immutable singletons 2128978e51baSMike Snitzer * - used to optimize both dm_request_fn and dm_mq_queue_rq; 2129978e51baSMike Snitzer * and __process_bio. 213016f12266SMike Snitzer */ 213116f12266SMike Snitzer md->immutable_target = dm_table_get_immutable_target(t); 213216f12266SMike Snitzer } 2133e6ee8c0bSKiyoshi Ueda 21342a2a4c51SJens Axboe ret = __bind_mempools(md, t); 21352a2a4c51SJens Axboe if (ret) { 21362a2a4c51SJens Axboe old_map = ERR_PTR(ret); 21372a2a4c51SJens Axboe goto out; 21382a2a4c51SJens Axboe } 2139e6ee8c0bSKiyoshi Ueda 2140a12f5d48SEric Dumazet old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 21411d3aa6f6SMike Snitzer rcu_assign_pointer(md->map, (void *)t); 214236a0456fSAlasdair G Kergon md->immutable_target_type = dm_table_get_immutable_target_type(t); 214336a0456fSAlasdair G Kergon 2144754c5fc7SMike Snitzer dm_table_set_restrictions(t, q, limits); 214541abc4e1SHannes Reinecke if (old_map) 214683d5e5b0SMikulas Patocka dm_sync_table(md); 21472ca3310eSAlasdair G Kergon 21482a2a4c51SJens Axboe out: 2149042d2a9bSAlasdair G Kergon return old_map; 21501da177e4SLinus Torvalds } 21511da177e4SLinus Torvalds 2152a7940155SAlasdair G Kergon /* 2153a7940155SAlasdair G Kergon * Returns unbound table for the caller to free. 2154a7940155SAlasdair G Kergon */ 2155a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md) 21561da177e4SLinus Torvalds { 2157a12f5d48SEric Dumazet struct dm_table *map = rcu_dereference_protected(md->map, 1); 21581da177e4SLinus Torvalds 21591da177e4SLinus Torvalds if (!map) 2160a7940155SAlasdair G Kergon return NULL; 21611da177e4SLinus Torvalds 21621da177e4SLinus Torvalds dm_table_event_callback(map, NULL, NULL); 21639cdb8520SMonam Agarwal RCU_INIT_POINTER(md->map, NULL); 216483d5e5b0SMikulas Patocka dm_sync_table(md); 2165a7940155SAlasdair G Kergon 2166a7940155SAlasdair G Kergon return map; 21671da177e4SLinus Torvalds } 21681da177e4SLinus Torvalds 21691da177e4SLinus Torvalds /* 21701da177e4SLinus Torvalds * Constructor for a new device. 21711da177e4SLinus Torvalds */ 21722b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result) 21731da177e4SLinus Torvalds { 2174c12c9a3cSMike Snitzer int r; 21751da177e4SLinus Torvalds struct mapped_device *md; 21761da177e4SLinus Torvalds 21772b06cfffSAlasdair G Kergon md = alloc_dev(minor); 21781da177e4SLinus Torvalds if (!md) 21791da177e4SLinus Torvalds return -ENXIO; 21801da177e4SLinus Torvalds 2181c12c9a3cSMike Snitzer r = dm_sysfs_init(md); 2182c12c9a3cSMike Snitzer if (r) { 2183c12c9a3cSMike Snitzer free_dev(md); 2184c12c9a3cSMike Snitzer return r; 2185c12c9a3cSMike Snitzer } 2186784aae73SMilan Broz 21871da177e4SLinus Torvalds *result = md; 21881da177e4SLinus Torvalds return 0; 21891da177e4SLinus Torvalds } 21901da177e4SLinus Torvalds 2191a5664dadSMike Snitzer /* 2192a5664dadSMike Snitzer * Functions to manage md->type. 2193a5664dadSMike Snitzer * All are required to hold md->type_lock. 2194a5664dadSMike Snitzer */ 2195a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md) 2196a5664dadSMike Snitzer { 2197a5664dadSMike Snitzer mutex_lock(&md->type_lock); 2198a5664dadSMike Snitzer } 2199a5664dadSMike Snitzer 2200a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md) 2201a5664dadSMike Snitzer { 2202a5664dadSMike Snitzer mutex_unlock(&md->type_lock); 2203a5664dadSMike Snitzer } 2204a5664dadSMike Snitzer 22057e0d574fSBart Van Assche void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type) 2206a5664dadSMike Snitzer { 220700c4fc3bSMike Snitzer BUG_ON(!mutex_is_locked(&md->type_lock)); 2208a5664dadSMike Snitzer md->type = type; 2209a5664dadSMike Snitzer } 2210a5664dadSMike Snitzer 22117e0d574fSBart Van Assche enum dm_queue_mode dm_get_md_type(struct mapped_device *md) 2212a5664dadSMike Snitzer { 2213a5664dadSMike Snitzer return md->type; 2214a5664dadSMike Snitzer } 2215a5664dadSMike Snitzer 221636a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md) 221736a0456fSAlasdair G Kergon { 221836a0456fSAlasdair G Kergon return md->immutable_target_type; 221936a0456fSAlasdair G Kergon } 222036a0456fSAlasdair G Kergon 22214a0b4ddfSMike Snitzer /* 2222f84cb8a4SMike Snitzer * The queue_limits are only valid as long as you have a reference 2223f84cb8a4SMike Snitzer * count on 'md'. 2224f84cb8a4SMike Snitzer */ 2225f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md) 2226f84cb8a4SMike Snitzer { 2227f84cb8a4SMike Snitzer BUG_ON(!atomic_read(&md->holders)); 2228f84cb8a4SMike Snitzer return &md->queue->limits; 2229f84cb8a4SMike Snitzer } 2230f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits); 2231f84cb8a4SMike Snitzer 22324a0b4ddfSMike Snitzer /* 22334a0b4ddfSMike Snitzer * Setup the DM device's queue based on md's type 22344a0b4ddfSMike Snitzer */ 2235591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t) 22364a0b4ddfSMike Snitzer { 2237bfebd1cdSMike Snitzer int r; 2238c100ec49SMike Snitzer struct queue_limits limits; 22397e0d574fSBart Van Assche enum dm_queue_mode type = dm_get_md_type(md); 2240bfebd1cdSMike Snitzer 2241545ed20eSToshi Kani switch (type) { 2242bfebd1cdSMike Snitzer case DM_TYPE_REQUEST_BASED: 2243e83068a5SMike Snitzer r = dm_mq_init_request_queue(md, t); 2244bfebd1cdSMike Snitzer if (r) { 2245eca7ee6dSMike Snitzer DMERR("Cannot initialize queue for request-based dm-mq mapped device"); 2246bfebd1cdSMike Snitzer return r; 2247bfebd1cdSMike Snitzer } 2248bfebd1cdSMike Snitzer break; 2249bfebd1cdSMike Snitzer case DM_TYPE_BIO_BASED: 2250545ed20eSToshi Kani case DM_TYPE_DAX_BIO_BASED: 2251978e51baSMike Snitzer case DM_TYPE_NVME_BIO_BASED: 2252978e51baSMike Snitzer dm_init_normal_md_queue(md); 225324113d48SMikulas Patocka blk_queue_make_request(md->queue, dm_make_request); 2254bfebd1cdSMike Snitzer break; 22557e0d574fSBart Van Assche case DM_TYPE_NONE: 22567e0d574fSBart Van Assche WARN_ON_ONCE(true); 22577e0d574fSBart Van Assche break; 2258ff36ab34SMike Snitzer } 22594a0b4ddfSMike Snitzer 2260c100ec49SMike Snitzer r = dm_calculate_queue_limits(t, &limits); 2261c100ec49SMike Snitzer if (r) { 2262c100ec49SMike Snitzer DMERR("Cannot calculate initial queue limits"); 2263c100ec49SMike Snitzer return r; 2264c100ec49SMike Snitzer } 2265c100ec49SMike Snitzer dm_table_set_restrictions(t, md->queue, &limits); 2266c100ec49SMike Snitzer blk_register_queue(md->disk); 2267c100ec49SMike Snitzer 22684a0b4ddfSMike Snitzer return 0; 22694a0b4ddfSMike Snitzer } 22704a0b4ddfSMike Snitzer 22712bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev) 22721da177e4SLinus Torvalds { 22731da177e4SLinus Torvalds struct mapped_device *md; 22741da177e4SLinus Torvalds unsigned minor = MINOR(dev); 22751da177e4SLinus Torvalds 22761da177e4SLinus Torvalds if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 22771da177e4SLinus Torvalds return NULL; 22781da177e4SLinus Torvalds 2279f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 22801da177e4SLinus Torvalds 22811da177e4SLinus Torvalds md = idr_find(&_minor_idr, minor); 228249de5769SMike Snitzer if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) || 228349de5769SMike Snitzer test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { 2284637842cfSDavid Teigland md = NULL; 2285fba9f90eSJeff Mahoney goto out; 2286fba9f90eSJeff Mahoney } 22872bec1f4aSMikulas Patocka dm_get(md); 2288fba9f90eSJeff Mahoney out: 2289f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 22901da177e4SLinus Torvalds 2291637842cfSDavid Teigland return md; 2292637842cfSDavid Teigland } 22933cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md); 2294d229a958SDavid Teigland 22959ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md) 2296637842cfSDavid Teigland { 22979ade92a9SAlasdair G Kergon return md->interface_ptr; 22981da177e4SLinus Torvalds } 22991da177e4SLinus Torvalds 23001da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr) 23011da177e4SLinus Torvalds { 23021da177e4SLinus Torvalds md->interface_ptr = ptr; 23031da177e4SLinus Torvalds } 23041da177e4SLinus Torvalds 23051da177e4SLinus Torvalds void dm_get(struct mapped_device *md) 23061da177e4SLinus Torvalds { 23071da177e4SLinus Torvalds atomic_inc(&md->holders); 23083f77316dSKiyoshi Ueda BUG_ON(test_bit(DMF_FREEING, &md->flags)); 23091da177e4SLinus Torvalds } 23101da177e4SLinus Torvalds 231109ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md) 231209ee96b2SMikulas Patocka { 231309ee96b2SMikulas Patocka spin_lock(&_minor_lock); 231409ee96b2SMikulas Patocka if (test_bit(DMF_FREEING, &md->flags)) { 231509ee96b2SMikulas Patocka spin_unlock(&_minor_lock); 231609ee96b2SMikulas Patocka return -EBUSY; 231709ee96b2SMikulas Patocka } 231809ee96b2SMikulas Patocka dm_get(md); 231909ee96b2SMikulas Patocka spin_unlock(&_minor_lock); 232009ee96b2SMikulas Patocka return 0; 232109ee96b2SMikulas Patocka } 232209ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold); 232309ee96b2SMikulas Patocka 232472d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md) 232572d94861SAlasdair G Kergon { 232672d94861SAlasdair G Kergon return md->name; 232772d94861SAlasdair G Kergon } 232872d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name); 232972d94861SAlasdair G Kergon 23303f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait) 23311da177e4SLinus Torvalds { 23321134e5aeSMike Anderson struct dm_table *map; 233383d5e5b0SMikulas Patocka int srcu_idx; 23341da177e4SLinus Torvalds 23353f77316dSKiyoshi Ueda might_sleep(); 2336fba9f90eSJeff Mahoney 233763a4f065SMike Snitzer spin_lock(&_minor_lock); 23383f77316dSKiyoshi Ueda idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md)))); 2339fba9f90eSJeff Mahoney set_bit(DMF_FREEING, &md->flags); 2340f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 23413f77316dSKiyoshi Ueda 2342c12c9a3cSMike Snitzer blk_set_queue_dying(md->queue); 23433b785fbcSBart Van Assche 2344ab7c7bb6SMikulas Patocka /* 2345ab7c7bb6SMikulas Patocka * Take suspend_lock so that presuspend and postsuspend methods 2346ab7c7bb6SMikulas Patocka * do not race with internal suspend. 2347ab7c7bb6SMikulas Patocka */ 2348ab7c7bb6SMikulas Patocka mutex_lock(&md->suspend_lock); 23492a708cffSJunichi Nomura map = dm_get_live_table(md, &srcu_idx); 23504f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) { 23511da177e4SLinus Torvalds dm_table_presuspend_targets(map); 23521da177e4SLinus Torvalds dm_table_postsuspend_targets(map); 23531da177e4SLinus Torvalds } 235483d5e5b0SMikulas Patocka /* dm_put_live_table must be before msleep, otherwise deadlock is possible */ 235583d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 23562a708cffSJunichi Nomura mutex_unlock(&md->suspend_lock); 235783d5e5b0SMikulas Patocka 23583f77316dSKiyoshi Ueda /* 23593f77316dSKiyoshi Ueda * Rare, but there may be I/O requests still going to complete, 23603f77316dSKiyoshi Ueda * for example. Wait for all references to disappear. 23613f77316dSKiyoshi Ueda * No one should increment the reference count of the mapped_device, 23623f77316dSKiyoshi Ueda * after the mapped_device state becomes DMF_FREEING. 23633f77316dSKiyoshi Ueda */ 23643f77316dSKiyoshi Ueda if (wait) 23653f77316dSKiyoshi Ueda while (atomic_read(&md->holders)) 23663f77316dSKiyoshi Ueda msleep(1); 23673f77316dSKiyoshi Ueda else if (atomic_read(&md->holders)) 23683f77316dSKiyoshi Ueda DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)", 23693f77316dSKiyoshi Ueda dm_device_name(md), atomic_read(&md->holders)); 23703f77316dSKiyoshi Ueda 2371784aae73SMilan Broz dm_sysfs_exit(md); 2372a7940155SAlasdair G Kergon dm_table_destroy(__unbind(md)); 23731da177e4SLinus Torvalds free_dev(md); 23741da177e4SLinus Torvalds } 23753f77316dSKiyoshi Ueda 23763f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md) 23773f77316dSKiyoshi Ueda { 23783f77316dSKiyoshi Ueda __dm_destroy(md, true); 23793f77316dSKiyoshi Ueda } 23803f77316dSKiyoshi Ueda 23813f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md) 23823f77316dSKiyoshi Ueda { 23833f77316dSKiyoshi Ueda __dm_destroy(md, false); 23843f77316dSKiyoshi Ueda } 23853f77316dSKiyoshi Ueda 23863f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md) 23873f77316dSKiyoshi Ueda { 23883f77316dSKiyoshi Ueda atomic_dec(&md->holders); 23891da177e4SLinus Torvalds } 239079eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put); 23911da177e4SLinus Torvalds 2392b48633f8SBart Van Assche static int dm_wait_for_completion(struct mapped_device *md, long task_state) 239346125c1cSMilan Broz { 239446125c1cSMilan Broz int r = 0; 23959f4c3f87SBart Van Assche DEFINE_WAIT(wait); 239646125c1cSMilan Broz 239746125c1cSMilan Broz while (1) { 23989f4c3f87SBart Van Assche prepare_to_wait(&md->wait, &wait, task_state); 239946125c1cSMilan Broz 2400b4324feeSKiyoshi Ueda if (!md_in_flight(md)) 240146125c1cSMilan Broz break; 240246125c1cSMilan Broz 2403e3fabdfdSBart Van Assche if (signal_pending_state(task_state, current)) { 240446125c1cSMilan Broz r = -EINTR; 240546125c1cSMilan Broz break; 240646125c1cSMilan Broz } 240746125c1cSMilan Broz 240846125c1cSMilan Broz io_schedule(); 240946125c1cSMilan Broz } 24109f4c3f87SBart Van Assche finish_wait(&md->wait, &wait); 2411b44ebeb0SMikulas Patocka 241246125c1cSMilan Broz return r; 241346125c1cSMilan Broz } 241446125c1cSMilan Broz 24151da177e4SLinus Torvalds /* 24161da177e4SLinus Torvalds * Process the deferred bios 24171da177e4SLinus Torvalds */ 2418ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work) 24191da177e4SLinus Torvalds { 2420ef208587SMikulas Patocka struct mapped_device *md = container_of(work, struct mapped_device, 2421ef208587SMikulas Patocka work); 24226d6f10dfSMilan Broz struct bio *c; 242383d5e5b0SMikulas Patocka int srcu_idx; 242483d5e5b0SMikulas Patocka struct dm_table *map; 24251da177e4SLinus Torvalds 242683d5e5b0SMikulas Patocka map = dm_get_live_table(md, &srcu_idx); 2427ef208587SMikulas Patocka 24283b00b203SMikulas Patocka while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 2429022c2611SMikulas Patocka spin_lock_irq(&md->deferred_lock); 2430022c2611SMikulas Patocka c = bio_list_pop(&md->deferred); 2431022c2611SMikulas Patocka spin_unlock_irq(&md->deferred_lock); 2432022c2611SMikulas Patocka 24336a8736d1STejun Heo if (!c) 2434df12ee99SAlasdair G Kergon break; 243573d410c0SMilan Broz 2436e6ee8c0bSKiyoshi Ueda if (dm_request_based(md)) 24376548c7c5SMike Snitzer (void) generic_make_request(c); 2438af7e466aSMikulas Patocka else 24396548c7c5SMike Snitzer (void) dm_process_bio(md, map, c); 2440e6ee8c0bSKiyoshi Ueda } 24413b00b203SMikulas Patocka 244283d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 24431da177e4SLinus Torvalds } 24441da177e4SLinus Torvalds 24459a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md) 2446304f3f6aSMilan Broz { 24473b00b203SMikulas Patocka clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 24484e857c58SPeter Zijlstra smp_mb__after_atomic(); 244953d5914fSMikulas Patocka queue_work(md->wq, &md->work); 2450304f3f6aSMilan Broz } 2451304f3f6aSMilan Broz 24521da177e4SLinus Torvalds /* 2453042d2a9bSAlasdair G Kergon * Swap in a new table, returning the old one for the caller to destroy. 24541da177e4SLinus Torvalds */ 2455042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table) 24561da177e4SLinus Torvalds { 245787eb5b21SMike Christie struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL); 2458754c5fc7SMike Snitzer struct queue_limits limits; 2459042d2a9bSAlasdair G Kergon int r; 24601da177e4SLinus Torvalds 2461e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 24621da177e4SLinus Torvalds 24631da177e4SLinus Torvalds /* device must be suspended */ 24644f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) 246593c534aeSAlasdair G Kergon goto out; 24661da177e4SLinus Torvalds 24673ae70656SMike Snitzer /* 24683ae70656SMike Snitzer * If the new table has no data devices, retain the existing limits. 24693ae70656SMike Snitzer * This helps multipath with queue_if_no_path if all paths disappear, 24703ae70656SMike Snitzer * then new I/O is queued based on these limits, and then some paths 24713ae70656SMike Snitzer * reappear. 24723ae70656SMike Snitzer */ 24733ae70656SMike Snitzer if (dm_table_has_no_data_devices(table)) { 247483d5e5b0SMikulas Patocka live_map = dm_get_live_table_fast(md); 24753ae70656SMike Snitzer if (live_map) 24763ae70656SMike Snitzer limits = md->queue->limits; 247783d5e5b0SMikulas Patocka dm_put_live_table_fast(md); 24783ae70656SMike Snitzer } 24793ae70656SMike Snitzer 248087eb5b21SMike Christie if (!live_map) { 2481754c5fc7SMike Snitzer r = dm_calculate_queue_limits(table, &limits); 2482042d2a9bSAlasdair G Kergon if (r) { 2483042d2a9bSAlasdair G Kergon map = ERR_PTR(r); 2484754c5fc7SMike Snitzer goto out; 2485042d2a9bSAlasdair G Kergon } 248687eb5b21SMike Christie } 2487754c5fc7SMike Snitzer 2488042d2a9bSAlasdair G Kergon map = __bind(md, table, &limits); 248962e08243SMikulas Patocka dm_issue_global_event(); 24901da177e4SLinus Torvalds 249193c534aeSAlasdair G Kergon out: 2492e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 2493042d2a9bSAlasdair G Kergon return map; 24941da177e4SLinus Torvalds } 24951da177e4SLinus Torvalds 24961da177e4SLinus Torvalds /* 24971da177e4SLinus Torvalds * Functions to lock and unlock any filesystem running on the 24981da177e4SLinus Torvalds * device. 24991da177e4SLinus Torvalds */ 25002ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md) 25011da177e4SLinus Torvalds { 2502e39e2e95SAlasdair G Kergon int r; 25031da177e4SLinus Torvalds 25041da177e4SLinus Torvalds WARN_ON(md->frozen_sb); 2505dfbe03f6SAlasdair G Kergon 2506db8fef4fSMikulas Patocka md->frozen_sb = freeze_bdev(md->bdev); 2507dfbe03f6SAlasdair G Kergon if (IS_ERR(md->frozen_sb)) { 2508cf222b37SAlasdair G Kergon r = PTR_ERR(md->frozen_sb); 2509e39e2e95SAlasdair G Kergon md->frozen_sb = NULL; 2510e39e2e95SAlasdair G Kergon return r; 2511dfbe03f6SAlasdair G Kergon } 2512dfbe03f6SAlasdair G Kergon 2513aa8d7c2fSAlasdair G Kergon set_bit(DMF_FROZEN, &md->flags); 2514aa8d7c2fSAlasdair G Kergon 25151da177e4SLinus Torvalds return 0; 25161da177e4SLinus Torvalds } 25171da177e4SLinus Torvalds 25182ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md) 25191da177e4SLinus Torvalds { 2520aa8d7c2fSAlasdair G Kergon if (!test_bit(DMF_FROZEN, &md->flags)) 2521aa8d7c2fSAlasdair G Kergon return; 2522aa8d7c2fSAlasdair G Kergon 2523db8fef4fSMikulas Patocka thaw_bdev(md->bdev, md->frozen_sb); 25241da177e4SLinus Torvalds md->frozen_sb = NULL; 2525aa8d7c2fSAlasdair G Kergon clear_bit(DMF_FROZEN, &md->flags); 25261da177e4SLinus Torvalds } 25271da177e4SLinus Torvalds 25281da177e4SLinus Torvalds /* 2529b48633f8SBart Van Assche * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG 2530b48633f8SBart Van Assche * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE 2531b48633f8SBart Van Assche * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY 2532b48633f8SBart Van Assche * 2533ffcc3936SMike Snitzer * If __dm_suspend returns 0, the device is completely quiescent 2534ffcc3936SMike Snitzer * now. There is no request-processing activity. All new requests 2535ffcc3936SMike Snitzer * are being added to md->deferred list. 2536cec47e3dSKiyoshi Ueda */ 2537ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map, 2538b48633f8SBart Van Assche unsigned suspend_flags, long task_state, 2539eaf9a736SMike Snitzer int dmf_suspended_flag) 25401da177e4SLinus Torvalds { 2541ffcc3936SMike Snitzer bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG; 2542ffcc3936SMike Snitzer bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG; 2543ffcc3936SMike Snitzer int r; 2544cf222b37SAlasdair G Kergon 25455a8f1f80SBart Van Assche lockdep_assert_held(&md->suspend_lock); 25465a8f1f80SBart Van Assche 25472e93ccc1SKiyoshi Ueda /* 25482e93ccc1SKiyoshi Ueda * DMF_NOFLUSH_SUSPENDING must be set before presuspend. 25492e93ccc1SKiyoshi Ueda * This flag is cleared before dm_suspend returns. 25502e93ccc1SKiyoshi Ueda */ 25512e93ccc1SKiyoshi Ueda if (noflush) 25522e93ccc1SKiyoshi Ueda set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 255386331f39SBart Van Assche else 255486331f39SBart Van Assche pr_debug("%s: suspending with flush\n", dm_device_name(md)); 25552e93ccc1SKiyoshi Ueda 2556d67ee213SMike Snitzer /* 2557d67ee213SMike Snitzer * This gets reverted if there's an error later and the targets 2558d67ee213SMike Snitzer * provide the .presuspend_undo hook. 2559d67ee213SMike Snitzer */ 25601da177e4SLinus Torvalds dm_table_presuspend_targets(map); 25611da177e4SLinus Torvalds 25622e93ccc1SKiyoshi Ueda /* 25639f518b27SKiyoshi Ueda * Flush I/O to the device. 25649f518b27SKiyoshi Ueda * Any I/O submitted after lock_fs() may not be flushed. 25659f518b27SKiyoshi Ueda * noflush takes precedence over do_lockfs. 25669f518b27SKiyoshi Ueda * (lock_fs() flushes I/Os and waits for them to complete.) 25672e93ccc1SKiyoshi Ueda */ 256832a926daSMikulas Patocka if (!noflush && do_lockfs) { 25692ca3310eSAlasdair G Kergon r = lock_fs(md); 2570d67ee213SMike Snitzer if (r) { 2571d67ee213SMike Snitzer dm_table_presuspend_undo_targets(map); 2572ffcc3936SMike Snitzer return r; 2573aa8d7c2fSAlasdair G Kergon } 2574d67ee213SMike Snitzer } 25751da177e4SLinus Torvalds 25761da177e4SLinus Torvalds /* 25773b00b203SMikulas Patocka * Here we must make sure that no processes are submitting requests 25783b00b203SMikulas Patocka * to target drivers i.e. no one may be executing 25793b00b203SMikulas Patocka * __split_and_process_bio. This is called from dm_request and 25803b00b203SMikulas Patocka * dm_wq_work. 25813b00b203SMikulas Patocka * 25823b00b203SMikulas Patocka * To get all processes out of __split_and_process_bio in dm_request, 25833b00b203SMikulas Patocka * we take the write lock. To prevent any process from reentering 25846a8736d1STejun Heo * __split_and_process_bio from dm_request and quiesce the thread 25856a8736d1STejun Heo * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call 25866a8736d1STejun Heo * flush_workqueue(md->wq). 25871da177e4SLinus Torvalds */ 25881eb787ecSAlasdair G Kergon set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 258941abc4e1SHannes Reinecke if (map) 259083d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 25911da177e4SLinus Torvalds 2592d0bcb878SKiyoshi Ueda /* 259329e4013dSTejun Heo * Stop md->queue before flushing md->wq in case request-based 259429e4013dSTejun Heo * dm defers requests to md->wq from md->queue. 2595d0bcb878SKiyoshi Ueda */ 25966a23e05cSJens Axboe if (dm_request_based(md)) 2597eca7ee6dSMike Snitzer dm_stop_queue(md->queue); 2598cec47e3dSKiyoshi Ueda 2599d0bcb878SKiyoshi Ueda flush_workqueue(md->wq); 2600d0bcb878SKiyoshi Ueda 26011da177e4SLinus Torvalds /* 26023b00b203SMikulas Patocka * At this point no more requests are entering target request routines. 26033b00b203SMikulas Patocka * We call dm_wait_for_completion to wait for all existing requests 26043b00b203SMikulas Patocka * to finish. 26051da177e4SLinus Torvalds */ 2606b48633f8SBart Van Assche r = dm_wait_for_completion(md, task_state); 2607eaf9a736SMike Snitzer if (!r) 2608eaf9a736SMike Snitzer set_bit(dmf_suspended_flag, &md->flags); 26091da177e4SLinus Torvalds 26106d6f10dfSMilan Broz if (noflush) 2611022c2611SMikulas Patocka clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 261241abc4e1SHannes Reinecke if (map) 261383d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 26142e93ccc1SKiyoshi Ueda 26151da177e4SLinus Torvalds /* were we interrupted ? */ 261646125c1cSMilan Broz if (r < 0) { 26179a1fb464SMikulas Patocka dm_queue_flush(md); 261873d410c0SMilan Broz 2619cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 2620eca7ee6dSMike Snitzer dm_start_queue(md->queue); 2621cec47e3dSKiyoshi Ueda 26222ca3310eSAlasdair G Kergon unlock_fs(md); 2623d67ee213SMike Snitzer dm_table_presuspend_undo_targets(map); 2624ffcc3936SMike Snitzer /* pushback list is already flushed, so skip flush */ 2625ffcc3936SMike Snitzer } 2626ffcc3936SMike Snitzer 2627ffcc3936SMike Snitzer return r; 26282ca3310eSAlasdair G Kergon } 26292ca3310eSAlasdair G Kergon 26303b00b203SMikulas Patocka /* 2631ffcc3936SMike Snitzer * We need to be able to change a mapping table under a mounted 2632ffcc3936SMike Snitzer * filesystem. For example we might want to move some data in 2633ffcc3936SMike Snitzer * the background. Before the table can be swapped with 2634ffcc3936SMike Snitzer * dm_bind_table, dm_suspend must be called to flush any in 2635ffcc3936SMike Snitzer * flight bios and ensure that any further io gets deferred. 26363b00b203SMikulas Patocka */ 2637ffcc3936SMike Snitzer /* 2638ffcc3936SMike Snitzer * Suspend mechanism in request-based dm. 2639ffcc3936SMike Snitzer * 2640ffcc3936SMike Snitzer * 1. Flush all I/Os by lock_fs() if needed. 2641ffcc3936SMike Snitzer * 2. Stop dispatching any I/O by stopping the request_queue. 2642ffcc3936SMike Snitzer * 3. Wait for all in-flight I/Os to be completed or requeued. 2643ffcc3936SMike Snitzer * 2644ffcc3936SMike Snitzer * To abort suspend, start the request_queue. 2645ffcc3936SMike Snitzer */ 2646ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags) 2647ffcc3936SMike Snitzer { 2648ffcc3936SMike Snitzer struct dm_table *map = NULL; 2649ffcc3936SMike Snitzer int r = 0; 2650ffcc3936SMike Snitzer 2651ffcc3936SMike Snitzer retry: 2652ffcc3936SMike Snitzer mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); 2653ffcc3936SMike Snitzer 2654ffcc3936SMike Snitzer if (dm_suspended_md(md)) { 2655ffcc3936SMike Snitzer r = -EINVAL; 2656ffcc3936SMike Snitzer goto out_unlock; 2657ffcc3936SMike Snitzer } 2658ffcc3936SMike Snitzer 2659ffcc3936SMike Snitzer if (dm_suspended_internally_md(md)) { 2660ffcc3936SMike Snitzer /* already internally suspended, wait for internal resume */ 2661ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2662ffcc3936SMike Snitzer r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); 2663ffcc3936SMike Snitzer if (r) 2664ffcc3936SMike Snitzer return r; 2665ffcc3936SMike Snitzer goto retry; 2666ffcc3936SMike Snitzer } 2667ffcc3936SMike Snitzer 2668a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2669ffcc3936SMike Snitzer 2670eaf9a736SMike Snitzer r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED); 2671ffcc3936SMike Snitzer if (r) 2672ffcc3936SMike Snitzer goto out_unlock; 26733b00b203SMikulas Patocka 26744d4471cbSKiyoshi Ueda dm_table_postsuspend_targets(map); 26754d4471cbSKiyoshi Ueda 2676d287483dSAlasdair G Kergon out_unlock: 2677e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 2678cf222b37SAlasdair G Kergon return r; 26791da177e4SLinus Torvalds } 26801da177e4SLinus Torvalds 2681ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map) 26821da177e4SLinus Torvalds { 2683ffcc3936SMike Snitzer if (map) { 2684ffcc3936SMike Snitzer int r = dm_table_resume_targets(map); 26858757b776SMilan Broz if (r) 2686ffcc3936SMike Snitzer return r; 2687ffcc3936SMike Snitzer } 26882ca3310eSAlasdair G Kergon 26899a1fb464SMikulas Patocka dm_queue_flush(md); 26902ca3310eSAlasdair G Kergon 2691cec47e3dSKiyoshi Ueda /* 2692cec47e3dSKiyoshi Ueda * Flushing deferred I/Os must be done after targets are resumed 2693cec47e3dSKiyoshi Ueda * so that mapping of targets can work correctly. 2694cec47e3dSKiyoshi Ueda * Request-based dm is queueing the deferred I/Os in its request_queue. 2695cec47e3dSKiyoshi Ueda */ 2696cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 2697eca7ee6dSMike Snitzer dm_start_queue(md->queue); 2698cec47e3dSKiyoshi Ueda 26992ca3310eSAlasdair G Kergon unlock_fs(md); 27002ca3310eSAlasdair G Kergon 2701ffcc3936SMike Snitzer return 0; 2702ffcc3936SMike Snitzer } 2703ffcc3936SMike Snitzer 2704ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md) 2705ffcc3936SMike Snitzer { 27068dc23658SMinfei Huang int r; 2707ffcc3936SMike Snitzer struct dm_table *map = NULL; 2708ffcc3936SMike Snitzer 2709ffcc3936SMike Snitzer retry: 27108dc23658SMinfei Huang r = -EINVAL; 2711ffcc3936SMike Snitzer mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); 2712ffcc3936SMike Snitzer 2713ffcc3936SMike Snitzer if (!dm_suspended_md(md)) 2714ffcc3936SMike Snitzer goto out; 2715ffcc3936SMike Snitzer 2716ffcc3936SMike Snitzer if (dm_suspended_internally_md(md)) { 2717ffcc3936SMike Snitzer /* already internally suspended, wait for internal resume */ 2718ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2719ffcc3936SMike Snitzer r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); 2720ffcc3936SMike Snitzer if (r) 2721ffcc3936SMike Snitzer return r; 2722ffcc3936SMike Snitzer goto retry; 2723ffcc3936SMike Snitzer } 2724ffcc3936SMike Snitzer 2725a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2726ffcc3936SMike Snitzer if (!map || !dm_table_get_size(map)) 2727ffcc3936SMike Snitzer goto out; 2728ffcc3936SMike Snitzer 2729ffcc3936SMike Snitzer r = __dm_resume(md, map); 2730ffcc3936SMike Snitzer if (r) 2731ffcc3936SMike Snitzer goto out; 2732ffcc3936SMike Snitzer 27332ca3310eSAlasdair G Kergon clear_bit(DMF_SUSPENDED, &md->flags); 2734cf222b37SAlasdair G Kergon out: 2735e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 27362ca3310eSAlasdair G Kergon 2737cf222b37SAlasdair G Kergon return r; 27381da177e4SLinus Torvalds } 27391da177e4SLinus Torvalds 2740fd2ed4d2SMikulas Patocka /* 2741fd2ed4d2SMikulas Patocka * Internal suspend/resume works like userspace-driven suspend. It waits 2742fd2ed4d2SMikulas Patocka * until all bios finish and prevents issuing new bios to the target drivers. 2743fd2ed4d2SMikulas Patocka * It may be used only from the kernel. 2744fd2ed4d2SMikulas Patocka */ 2745fd2ed4d2SMikulas Patocka 2746ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags) 2747ffcc3936SMike Snitzer { 2748ffcc3936SMike Snitzer struct dm_table *map = NULL; 2749ffcc3936SMike Snitzer 27501ea0654eSBart Van Assche lockdep_assert_held(&md->suspend_lock); 27511ea0654eSBart Van Assche 275296b26c8cSMikulas Patocka if (md->internal_suspend_count++) 2753ffcc3936SMike Snitzer return; /* nested internal suspend */ 2754ffcc3936SMike Snitzer 2755ffcc3936SMike Snitzer if (dm_suspended_md(md)) { 2756ffcc3936SMike Snitzer set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2757ffcc3936SMike Snitzer return; /* nest suspend */ 2758ffcc3936SMike Snitzer } 2759ffcc3936SMike Snitzer 2760a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2761ffcc3936SMike Snitzer 2762ffcc3936SMike Snitzer /* 2763ffcc3936SMike Snitzer * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is 2764ffcc3936SMike Snitzer * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend 2765ffcc3936SMike Snitzer * would require changing .presuspend to return an error -- avoid this 2766ffcc3936SMike Snitzer * until there is a need for more elaborate variants of internal suspend. 2767ffcc3936SMike Snitzer */ 2768eaf9a736SMike Snitzer (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE, 2769eaf9a736SMike Snitzer DMF_SUSPENDED_INTERNALLY); 2770ffcc3936SMike Snitzer 2771ffcc3936SMike Snitzer dm_table_postsuspend_targets(map); 2772ffcc3936SMike Snitzer } 2773ffcc3936SMike Snitzer 2774ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md) 2775ffcc3936SMike Snitzer { 277696b26c8cSMikulas Patocka BUG_ON(!md->internal_suspend_count); 277796b26c8cSMikulas Patocka 277896b26c8cSMikulas Patocka if (--md->internal_suspend_count) 2779ffcc3936SMike Snitzer return; /* resume from nested internal suspend */ 2780ffcc3936SMike Snitzer 2781ffcc3936SMike Snitzer if (dm_suspended_md(md)) 2782ffcc3936SMike Snitzer goto done; /* resume from nested suspend */ 2783ffcc3936SMike Snitzer 2784ffcc3936SMike Snitzer /* 2785ffcc3936SMike Snitzer * NOTE: existing callers don't need to call dm_table_resume_targets 2786ffcc3936SMike Snitzer * (which may fail -- so best to avoid it for now by passing NULL map) 2787ffcc3936SMike Snitzer */ 2788ffcc3936SMike Snitzer (void) __dm_resume(md, NULL); 2789ffcc3936SMike Snitzer 2790ffcc3936SMike Snitzer done: 2791ffcc3936SMike Snitzer clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2792ffcc3936SMike Snitzer smp_mb__after_atomic(); 2793ffcc3936SMike Snitzer wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY); 2794ffcc3936SMike Snitzer } 2795ffcc3936SMike Snitzer 2796ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md) 2797fd2ed4d2SMikulas Patocka { 2798fd2ed4d2SMikulas Patocka mutex_lock(&md->suspend_lock); 2799ffcc3936SMike Snitzer __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG); 2800ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2801ffcc3936SMike Snitzer } 2802ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush); 2803ffcc3936SMike Snitzer 2804ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md) 2805ffcc3936SMike Snitzer { 2806ffcc3936SMike Snitzer mutex_lock(&md->suspend_lock); 2807ffcc3936SMike Snitzer __dm_internal_resume(md); 2808ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2809ffcc3936SMike Snitzer } 2810ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume); 2811ffcc3936SMike Snitzer 2812ffcc3936SMike Snitzer /* 2813ffcc3936SMike Snitzer * Fast variants of internal suspend/resume hold md->suspend_lock, 2814ffcc3936SMike Snitzer * which prevents interaction with userspace-driven suspend. 2815ffcc3936SMike Snitzer */ 2816ffcc3936SMike Snitzer 2817ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md) 2818ffcc3936SMike Snitzer { 2819ffcc3936SMike Snitzer mutex_lock(&md->suspend_lock); 2820ffcc3936SMike Snitzer if (dm_suspended_md(md) || dm_suspended_internally_md(md)) 2821fd2ed4d2SMikulas Patocka return; 2822fd2ed4d2SMikulas Patocka 2823fd2ed4d2SMikulas Patocka set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 2824fd2ed4d2SMikulas Patocka synchronize_srcu(&md->io_barrier); 2825fd2ed4d2SMikulas Patocka flush_workqueue(md->wq); 2826fd2ed4d2SMikulas Patocka dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); 2827fd2ed4d2SMikulas Patocka } 2828b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast); 2829fd2ed4d2SMikulas Patocka 2830ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md) 2831fd2ed4d2SMikulas Patocka { 2832ffcc3936SMike Snitzer if (dm_suspended_md(md) || dm_suspended_internally_md(md)) 2833fd2ed4d2SMikulas Patocka goto done; 2834fd2ed4d2SMikulas Patocka 2835fd2ed4d2SMikulas Patocka dm_queue_flush(md); 2836fd2ed4d2SMikulas Patocka 2837fd2ed4d2SMikulas Patocka done: 2838fd2ed4d2SMikulas Patocka mutex_unlock(&md->suspend_lock); 2839fd2ed4d2SMikulas Patocka } 2840b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast); 2841fd2ed4d2SMikulas Patocka 28421da177e4SLinus Torvalds /*----------------------------------------------------------------- 28431da177e4SLinus Torvalds * Event notification. 28441da177e4SLinus Torvalds *---------------------------------------------------------------*/ 28453abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action, 284660935eb2SMilan Broz unsigned cookie) 284769267a30SAlasdair G Kergon { 284860935eb2SMilan Broz char udev_cookie[DM_COOKIE_LENGTH]; 284960935eb2SMilan Broz char *envp[] = { udev_cookie, NULL }; 285060935eb2SMilan Broz 285160935eb2SMilan Broz if (!cookie) 28523abf85b5SPeter Rajnoha return kobject_uevent(&disk_to_dev(md->disk)->kobj, action); 285360935eb2SMilan Broz else { 285460935eb2SMilan Broz snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u", 285560935eb2SMilan Broz DM_COOKIE_ENV_VAR_NAME, cookie); 28563abf85b5SPeter Rajnoha return kobject_uevent_env(&disk_to_dev(md->disk)->kobj, 28573abf85b5SPeter Rajnoha action, envp); 285860935eb2SMilan Broz } 285969267a30SAlasdair G Kergon } 286069267a30SAlasdair G Kergon 28617a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md) 28627a8c3d3bSMike Anderson { 28637a8c3d3bSMike Anderson return atomic_add_return(1, &md->uevent_seq); 28647a8c3d3bSMike Anderson } 28657a8c3d3bSMike Anderson 28661da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md) 28671da177e4SLinus Torvalds { 28681da177e4SLinus Torvalds return atomic_read(&md->event_nr); 28691da177e4SLinus Torvalds } 28701da177e4SLinus Torvalds 28711da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr) 28721da177e4SLinus Torvalds { 28731da177e4SLinus Torvalds return wait_event_interruptible(md->eventq, 28741da177e4SLinus Torvalds (event_nr != atomic_read(&md->event_nr))); 28751da177e4SLinus Torvalds } 28761da177e4SLinus Torvalds 28777a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist) 28787a8c3d3bSMike Anderson { 28797a8c3d3bSMike Anderson unsigned long flags; 28807a8c3d3bSMike Anderson 28817a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 28827a8c3d3bSMike Anderson list_add(elist, &md->uevent_list); 28837a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 28847a8c3d3bSMike Anderson } 28857a8c3d3bSMike Anderson 28861da177e4SLinus Torvalds /* 28871da177e4SLinus Torvalds * The gendisk is only valid as long as you have a reference 28881da177e4SLinus Torvalds * count on 'md'. 28891da177e4SLinus Torvalds */ 28901da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md) 28911da177e4SLinus Torvalds { 28921da177e4SLinus Torvalds return md->disk; 28931da177e4SLinus Torvalds } 289465ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk); 28951da177e4SLinus Torvalds 2896784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md) 2897784aae73SMilan Broz { 28982995fa78SMikulas Patocka return &md->kobj_holder.kobj; 2899784aae73SMilan Broz } 2900784aae73SMilan Broz 2901784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj) 2902784aae73SMilan Broz { 2903784aae73SMilan Broz struct mapped_device *md; 2904784aae73SMilan Broz 29052995fa78SMikulas Patocka md = container_of(kobj, struct mapped_device, kobj_holder.kobj); 2906784aae73SMilan Broz 2907b9a41d21SHou Tao spin_lock(&_minor_lock); 2908b9a41d21SHou Tao if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { 2909b9a41d21SHou Tao md = NULL; 2910b9a41d21SHou Tao goto out; 2911b9a41d21SHou Tao } 2912784aae73SMilan Broz dm_get(md); 2913b9a41d21SHou Tao out: 2914b9a41d21SHou Tao spin_unlock(&_minor_lock); 2915b9a41d21SHou Tao 2916784aae73SMilan Broz return md; 2917784aae73SMilan Broz } 2918784aae73SMilan Broz 29194f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md) 29201da177e4SLinus Torvalds { 29211da177e4SLinus Torvalds return test_bit(DMF_SUSPENDED, &md->flags); 29221da177e4SLinus Torvalds } 29231da177e4SLinus Torvalds 2924ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md) 2925ffcc3936SMike Snitzer { 2926ffcc3936SMike Snitzer return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2927ffcc3936SMike Snitzer } 2928ffcc3936SMike Snitzer 29292c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md) 29302c140a24SMikulas Patocka { 29312c140a24SMikulas Patocka return test_bit(DMF_DEFERRED_REMOVE, &md->flags); 29322c140a24SMikulas Patocka } 29332c140a24SMikulas Patocka 293464dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti) 293564dbce58SKiyoshi Ueda { 2936ecdb2e25SKiyoshi Ueda return dm_suspended_md(dm_table_get_md(ti->table)); 293764dbce58SKiyoshi Ueda } 293864dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended); 293964dbce58SKiyoshi Ueda 29402e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti) 29412e93ccc1SKiyoshi Ueda { 2942ecdb2e25SKiyoshi Ueda return __noflush_suspending(dm_table_get_md(ti->table)); 29432e93ccc1SKiyoshi Ueda } 29442e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending); 29452e93ccc1SKiyoshi Ueda 29467e0d574fSBart Van Assche struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type, 29470776aa0eSMike Snitzer unsigned integrity, unsigned per_io_data_size, 29480776aa0eSMike Snitzer unsigned min_pool_size) 2949e6ee8c0bSKiyoshi Ueda { 2950115485e8SMike Snitzer struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id); 295178d8e58aSMike Snitzer unsigned int pool_size = 0; 295264f52b0eSMike Snitzer unsigned int front_pad, io_front_pad; 29536f1c819cSKent Overstreet int ret; 2954e6ee8c0bSKiyoshi Ueda 2955e6ee8c0bSKiyoshi Ueda if (!pools) 29564e6e36c3SMike Snitzer return NULL; 2957e6ee8c0bSKiyoshi Ueda 295878d8e58aSMike Snitzer switch (type) { 295978d8e58aSMike Snitzer case DM_TYPE_BIO_BASED: 2960545ed20eSToshi Kani case DM_TYPE_DAX_BIO_BASED: 296122c11858SMike Snitzer case DM_TYPE_NVME_BIO_BASED: 29620776aa0eSMike Snitzer pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size); 296330187e1dSMike Snitzer front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone); 296464f52b0eSMike Snitzer io_front_pad = roundup(front_pad, __alignof__(struct dm_io)) + offsetof(struct dm_io, tio); 29656f1c819cSKent Overstreet ret = bioset_init(&pools->io_bs, pool_size, io_front_pad, 0); 29666f1c819cSKent Overstreet if (ret) 296764f52b0eSMike Snitzer goto out; 29686f1c819cSKent Overstreet if (integrity && bioset_integrity_create(&pools->io_bs, pool_size)) 2969eb8db831SChristoph Hellwig goto out; 297078d8e58aSMike Snitzer break; 297178d8e58aSMike Snitzer case DM_TYPE_REQUEST_BASED: 29720776aa0eSMike Snitzer pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size); 297378d8e58aSMike Snitzer front_pad = offsetof(struct dm_rq_clone_bio_info, clone); 2974591ddcfcSMike Snitzer /* per_io_data_size is used for blk-mq pdu at queue allocation */ 297578d8e58aSMike Snitzer break; 297678d8e58aSMike Snitzer default: 297778d8e58aSMike Snitzer BUG(); 297878d8e58aSMike Snitzer } 297978d8e58aSMike Snitzer 29806f1c819cSKent Overstreet ret = bioset_init(&pools->bs, pool_size, front_pad, 0); 29816f1c819cSKent Overstreet if (ret) 29825f015204SJun'ichi Nomura goto out; 2983e6ee8c0bSKiyoshi Ueda 29846f1c819cSKent Overstreet if (integrity && bioset_integrity_create(&pools->bs, pool_size)) 29855f015204SJun'ichi Nomura goto out; 2986a91a2785SMartin K. Petersen 2987e6ee8c0bSKiyoshi Ueda return pools; 298878d8e58aSMike Snitzer 29895f015204SJun'ichi Nomura out: 29905f015204SJun'ichi Nomura dm_free_md_mempools(pools); 2991e6ee8c0bSKiyoshi Ueda 29924e6e36c3SMike Snitzer return NULL; 2993e6ee8c0bSKiyoshi Ueda } 2994e6ee8c0bSKiyoshi Ueda 2995e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools) 2996e6ee8c0bSKiyoshi Ueda { 2997e6ee8c0bSKiyoshi Ueda if (!pools) 2998e6ee8c0bSKiyoshi Ueda return; 2999e6ee8c0bSKiyoshi Ueda 30006f1c819cSKent Overstreet bioset_exit(&pools->bs); 30016f1c819cSKent Overstreet bioset_exit(&pools->io_bs); 3002e6ee8c0bSKiyoshi Ueda 3003e6ee8c0bSKiyoshi Ueda kfree(pools); 3004e6ee8c0bSKiyoshi Ueda } 3005e6ee8c0bSKiyoshi Ueda 30069c72bad1SChristoph Hellwig struct dm_pr { 30079c72bad1SChristoph Hellwig u64 old_key; 30089c72bad1SChristoph Hellwig u64 new_key; 30099c72bad1SChristoph Hellwig u32 flags; 30109c72bad1SChristoph Hellwig bool fail_early; 30119c72bad1SChristoph Hellwig }; 30129c72bad1SChristoph Hellwig 30139c72bad1SChristoph Hellwig static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn, 30149c72bad1SChristoph Hellwig void *data) 30159c72bad1SChristoph Hellwig { 30169c72bad1SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 30179c72bad1SChristoph Hellwig struct dm_table *table; 30189c72bad1SChristoph Hellwig struct dm_target *ti; 30199c72bad1SChristoph Hellwig int ret = -ENOTTY, srcu_idx; 30209c72bad1SChristoph Hellwig 30219c72bad1SChristoph Hellwig table = dm_get_live_table(md, &srcu_idx); 30229c72bad1SChristoph Hellwig if (!table || !dm_table_get_size(table)) 30239c72bad1SChristoph Hellwig goto out; 30249c72bad1SChristoph Hellwig 30259c72bad1SChristoph Hellwig /* We only support devices that have a single target */ 30269c72bad1SChristoph Hellwig if (dm_table_get_num_targets(table) != 1) 30279c72bad1SChristoph Hellwig goto out; 30289c72bad1SChristoph Hellwig ti = dm_table_get_target(table, 0); 30299c72bad1SChristoph Hellwig 30309c72bad1SChristoph Hellwig ret = -EINVAL; 30319c72bad1SChristoph Hellwig if (!ti->type->iterate_devices) 30329c72bad1SChristoph Hellwig goto out; 30339c72bad1SChristoph Hellwig 30349c72bad1SChristoph Hellwig ret = ti->type->iterate_devices(ti, fn, data); 30359c72bad1SChristoph Hellwig out: 30369c72bad1SChristoph Hellwig dm_put_live_table(md, srcu_idx); 30379c72bad1SChristoph Hellwig return ret; 30389c72bad1SChristoph Hellwig } 30399c72bad1SChristoph Hellwig 30409c72bad1SChristoph Hellwig /* 30419c72bad1SChristoph Hellwig * For register / unregister we need to manually call out to every path. 30429c72bad1SChristoph Hellwig */ 30439c72bad1SChristoph Hellwig static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev, 30449c72bad1SChristoph Hellwig sector_t start, sector_t len, void *data) 30459c72bad1SChristoph Hellwig { 30469c72bad1SChristoph Hellwig struct dm_pr *pr = data; 30479c72bad1SChristoph Hellwig const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops; 30489c72bad1SChristoph Hellwig 30499c72bad1SChristoph Hellwig if (!ops || !ops->pr_register) 30509c72bad1SChristoph Hellwig return -EOPNOTSUPP; 30519c72bad1SChristoph Hellwig return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags); 30529c72bad1SChristoph Hellwig } 30539c72bad1SChristoph Hellwig 305471cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key, 305571cdb697SChristoph Hellwig u32 flags) 305671cdb697SChristoph Hellwig { 30579c72bad1SChristoph Hellwig struct dm_pr pr = { 30589c72bad1SChristoph Hellwig .old_key = old_key, 30599c72bad1SChristoph Hellwig .new_key = new_key, 30609c72bad1SChristoph Hellwig .flags = flags, 30619c72bad1SChristoph Hellwig .fail_early = true, 30629c72bad1SChristoph Hellwig }; 30639c72bad1SChristoph Hellwig int ret; 306471cdb697SChristoph Hellwig 30659c72bad1SChristoph Hellwig ret = dm_call_pr(bdev, __dm_pr_register, &pr); 30669c72bad1SChristoph Hellwig if (ret && new_key) { 30679c72bad1SChristoph Hellwig /* unregister all paths if we failed to register any path */ 30689c72bad1SChristoph Hellwig pr.old_key = new_key; 30699c72bad1SChristoph Hellwig pr.new_key = 0; 30709c72bad1SChristoph Hellwig pr.flags = 0; 30719c72bad1SChristoph Hellwig pr.fail_early = false; 30729c72bad1SChristoph Hellwig dm_call_pr(bdev, __dm_pr_register, &pr); 30739c72bad1SChristoph Hellwig } 307471cdb697SChristoph Hellwig 30759c72bad1SChristoph Hellwig return ret; 307671cdb697SChristoph Hellwig } 307771cdb697SChristoph Hellwig 307871cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type, 307971cdb697SChristoph Hellwig u32 flags) 308071cdb697SChristoph Hellwig { 308171cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 308271cdb697SChristoph Hellwig const struct pr_ops *ops; 3083971888c4SMike Snitzer int r, srcu_idx; 308471cdb697SChristoph Hellwig 30855bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 308671cdb697SChristoph Hellwig if (r < 0) 3087971888c4SMike Snitzer goto out; 308871cdb697SChristoph Hellwig 308971cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 309071cdb697SChristoph Hellwig if (ops && ops->pr_reserve) 309171cdb697SChristoph Hellwig r = ops->pr_reserve(bdev, key, type, flags); 309271cdb697SChristoph Hellwig else 309371cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3094971888c4SMike Snitzer out: 3095971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 309671cdb697SChristoph Hellwig return r; 309771cdb697SChristoph Hellwig } 309871cdb697SChristoph Hellwig 309971cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type) 310071cdb697SChristoph Hellwig { 310171cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 310271cdb697SChristoph Hellwig const struct pr_ops *ops; 3103971888c4SMike Snitzer int r, srcu_idx; 310471cdb697SChristoph Hellwig 31055bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 310671cdb697SChristoph Hellwig if (r < 0) 3107971888c4SMike Snitzer goto out; 310871cdb697SChristoph Hellwig 310971cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 311071cdb697SChristoph Hellwig if (ops && ops->pr_release) 311171cdb697SChristoph Hellwig r = ops->pr_release(bdev, key, type); 311271cdb697SChristoph Hellwig else 311371cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3114971888c4SMike Snitzer out: 3115971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 311671cdb697SChristoph Hellwig return r; 311771cdb697SChristoph Hellwig } 311871cdb697SChristoph Hellwig 311971cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key, 312071cdb697SChristoph Hellwig enum pr_type type, bool abort) 312171cdb697SChristoph Hellwig { 312271cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 312371cdb697SChristoph Hellwig const struct pr_ops *ops; 3124971888c4SMike Snitzer int r, srcu_idx; 312571cdb697SChristoph Hellwig 31265bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 312771cdb697SChristoph Hellwig if (r < 0) 3128971888c4SMike Snitzer goto out; 312971cdb697SChristoph Hellwig 313071cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 313171cdb697SChristoph Hellwig if (ops && ops->pr_preempt) 313271cdb697SChristoph Hellwig r = ops->pr_preempt(bdev, old_key, new_key, type, abort); 313371cdb697SChristoph Hellwig else 313471cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3135971888c4SMike Snitzer out: 3136971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 313771cdb697SChristoph Hellwig return r; 313871cdb697SChristoph Hellwig } 313971cdb697SChristoph Hellwig 314071cdb697SChristoph Hellwig static int dm_pr_clear(struct block_device *bdev, u64 key) 314171cdb697SChristoph Hellwig { 314271cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 314371cdb697SChristoph Hellwig const struct pr_ops *ops; 3144971888c4SMike Snitzer int r, srcu_idx; 314571cdb697SChristoph Hellwig 31465bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 314771cdb697SChristoph Hellwig if (r < 0) 3148971888c4SMike Snitzer goto out; 314971cdb697SChristoph Hellwig 315071cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 315171cdb697SChristoph Hellwig if (ops && ops->pr_clear) 315271cdb697SChristoph Hellwig r = ops->pr_clear(bdev, key); 315371cdb697SChristoph Hellwig else 315471cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3155971888c4SMike Snitzer out: 3156971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 315771cdb697SChristoph Hellwig return r; 315871cdb697SChristoph Hellwig } 315971cdb697SChristoph Hellwig 316071cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = { 316171cdb697SChristoph Hellwig .pr_register = dm_pr_register, 316271cdb697SChristoph Hellwig .pr_reserve = dm_pr_reserve, 316371cdb697SChristoph Hellwig .pr_release = dm_pr_release, 316471cdb697SChristoph Hellwig .pr_preempt = dm_pr_preempt, 316571cdb697SChristoph Hellwig .pr_clear = dm_pr_clear, 316671cdb697SChristoph Hellwig }; 316771cdb697SChristoph Hellwig 316883d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = { 31691da177e4SLinus Torvalds .open = dm_blk_open, 31701da177e4SLinus Torvalds .release = dm_blk_close, 3171aa129a22SMilan Broz .ioctl = dm_blk_ioctl, 31723ac51e74SDarrick J. Wong .getgeo = dm_blk_getgeo, 3173e76239a3SChristoph Hellwig .report_zones = dm_blk_report_zones, 317471cdb697SChristoph Hellwig .pr_ops = &dm_pr_ops, 31751da177e4SLinus Torvalds .owner = THIS_MODULE 31761da177e4SLinus Torvalds }; 31771da177e4SLinus Torvalds 3178f26c5719SDan Williams static const struct dax_operations dm_dax_ops = { 3179f26c5719SDan Williams .direct_access = dm_dax_direct_access, 31807e026c8cSDan Williams .copy_from_iter = dm_dax_copy_from_iter, 3181b3a9a0c3SDan Williams .copy_to_iter = dm_dax_copy_to_iter, 3182f26c5719SDan Williams }; 3183f26c5719SDan Williams 31841da177e4SLinus Torvalds /* 31851da177e4SLinus Torvalds * module hooks 31861da177e4SLinus Torvalds */ 31871da177e4SLinus Torvalds module_init(dm_init); 31881da177e4SLinus Torvalds module_exit(dm_exit); 31891da177e4SLinus Torvalds 31901da177e4SLinus Torvalds module_param(major, uint, 0); 31911da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper"); 3192f4790826SMike Snitzer 3193e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR); 3194e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools"); 3195e8603136SMike Snitzer 3196115485e8SMike Snitzer module_param(dm_numa_node, int, S_IRUGO | S_IWUSR); 3197115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations"); 3198115485e8SMike Snitzer 31991da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver"); 32001da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 32011da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 3202