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" 1191ccbbacSTushar Sugandhi #include "dm-ima.h" 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds #include <linux/init.h> 141da177e4SLinus Torvalds #include <linux/module.h> 1548c9c27bSArjan van de Ven #include <linux/mutex.h> 166958c1c6SMikulas Patocka #include <linux/sched/mm.h> 17174cd4b1SIngo Molnar #include <linux/sched/signal.h> 181da177e4SLinus Torvalds #include <linux/blkpg.h> 191da177e4SLinus Torvalds #include <linux/bio.h> 201da177e4SLinus Torvalds #include <linux/mempool.h> 21f26c5719SDan Williams #include <linux/dax.h> 221da177e4SLinus Torvalds #include <linux/slab.h> 231da177e4SLinus Torvalds #include <linux/idr.h> 247e026c8cSDan Williams #include <linux/uio.h> 253ac51e74SDarrick J. Wong #include <linux/hdreg.h> 263f77316dSKiyoshi Ueda #include <linux/delay.h> 27ffcc3936SMike Snitzer #include <linux/wait.h> 2871cdb697SChristoph Hellwig #include <linux/pr.h> 29b0b4d7c6SElena Reshetova #include <linux/refcount.h> 30c6a564ffSChristoph Hellwig #include <linux/part_stat.h> 31a892c8d5SSatya Tangirala #include <linux/blk-crypto.h> 321e8d44bdSEric Biggers #include <linux/blk-crypto-profile.h> 3355782138SLi Zefan 3472d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core" 3572d94861SAlasdair G Kergon 3660935eb2SMilan Broz /* 3760935eb2SMilan Broz * Cookies are numeric values sent with CHANGE and REMOVE 3860935eb2SMilan Broz * uevents while resuming, removing or renaming the device. 3960935eb2SMilan Broz */ 4060935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE" 4160935eb2SMilan Broz #define DM_COOKIE_LENGTH 24 4260935eb2SMilan Broz 43b99fdcdcSMing Lei /* 44b99fdcdcSMing Lei * For REQ_POLLED fs bio, this flag is set if we link mapped underlying 45b99fdcdcSMing Lei * dm_io into one list, and reuse bio->bi_private as the list head. Before 46b99fdcdcSMing Lei * ending this fs bio, we will recover its ->bi_private. 47b99fdcdcSMing Lei */ 48b99fdcdcSMing Lei #define REQ_DM_POLL_LIST REQ_DRV 49b99fdcdcSMing Lei 501da177e4SLinus Torvalds static const char *_name = DM_NAME; 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds static unsigned int major = 0; 531da177e4SLinus Torvalds static unsigned int _major = 0; 541da177e4SLinus Torvalds 55d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr); 56d15b774cSAlasdair G Kergon 57f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock); 582c140a24SMikulas Patocka 592c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w); 602c140a24SMikulas Patocka 612c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove); 622c140a24SMikulas Patocka 63acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue; 64acfe0ad7SMikulas Patocka 6593e6442cSMikulas Patocka atomic_t dm_global_event_nr = ATOMIC_INIT(0); 6693e6442cSMikulas Patocka DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq); 6793e6442cSMikulas Patocka 6862e08243SMikulas Patocka void dm_issue_global_event(void) 6962e08243SMikulas Patocka { 7062e08243SMikulas Patocka atomic_inc(&dm_global_event_nr); 7162e08243SMikulas Patocka wake_up(&dm_global_eventq); 7262e08243SMikulas Patocka } 7362e08243SMikulas Patocka 74442761fdSMike Snitzer DEFINE_STATIC_KEY_FALSE(stats_enabled); 75442761fdSMike Snitzer DEFINE_STATIC_KEY_FALSE(swap_bios_enabled); 76442761fdSMike Snitzer DEFINE_STATIC_KEY_FALSE(zoned_enabled); 77442761fdSMike Snitzer 781da177e4SLinus Torvalds /* 7964f52b0eSMike Snitzer * One of these is allocated (on-stack) per original bio. 801da177e4SLinus Torvalds */ 8164f52b0eSMike Snitzer struct clone_info { 8264f52b0eSMike Snitzer struct dm_table *map; 8364f52b0eSMike Snitzer struct bio *bio; 8464f52b0eSMike Snitzer struct dm_io *io; 8564f52b0eSMike Snitzer sector_t sector; 8664f52b0eSMike Snitzer unsigned sector_count; 87b99fdcdcSMing Lei bool submit_as_polled; 8864f52b0eSMike Snitzer }; 8964f52b0eSMike Snitzer 9062f26317SJeffle Xu #define DM_TARGET_IO_BIO_OFFSET (offsetof(struct dm_target_io, clone)) 9162f26317SJeffle Xu #define DM_IO_BIO_OFFSET \ 9262f26317SJeffle Xu (offsetof(struct dm_target_io, clone) + offsetof(struct dm_io, tio)) 9362f26317SJeffle Xu 946c23f0bdSChristoph Hellwig static inline struct dm_target_io *clone_to_tio(struct bio *clone) 956c23f0bdSChristoph Hellwig { 966c23f0bdSChristoph Hellwig return container_of(clone, struct dm_target_io, clone); 976c23f0bdSChristoph Hellwig } 986c23f0bdSChristoph Hellwig 9964f52b0eSMike Snitzer void *dm_per_bio_data(struct bio *bio, size_t data_size) 10064f52b0eSMike Snitzer { 101655f3aadSMike Snitzer if (!dm_tio_flagged(clone_to_tio(bio), DM_TIO_INSIDE_DM_IO)) 10262f26317SJeffle Xu return (char *)bio - DM_TARGET_IO_BIO_OFFSET - data_size; 10362f26317SJeffle Xu return (char *)bio - DM_IO_BIO_OFFSET - data_size; 10464f52b0eSMike Snitzer } 10564f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_per_bio_data); 10664f52b0eSMike Snitzer 10764f52b0eSMike Snitzer struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size) 10864f52b0eSMike Snitzer { 10964f52b0eSMike Snitzer struct dm_io *io = (struct dm_io *)((char *)data + data_size); 11064f52b0eSMike Snitzer if (io->magic == DM_IO_MAGIC) 11162f26317SJeffle Xu return (struct bio *)((char *)io + DM_IO_BIO_OFFSET); 11264f52b0eSMike Snitzer BUG_ON(io->magic != DM_TIO_MAGIC); 11362f26317SJeffle Xu return (struct bio *)((char *)io + DM_TARGET_IO_BIO_OFFSET); 11464f52b0eSMike Snitzer } 11564f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_from_per_bio_data); 11664f52b0eSMike Snitzer 11764f52b0eSMike Snitzer unsigned dm_bio_get_target_bio_nr(const struct bio *bio) 11864f52b0eSMike Snitzer { 11964f52b0eSMike Snitzer return container_of(bio, struct dm_target_io, clone)->target_bio_nr; 12064f52b0eSMike Snitzer } 12164f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_get_target_bio_nr); 12264f52b0eSMike Snitzer 123ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1) 124ba61fdd1SJeff Mahoney 125115485e8SMike Snitzer #define DM_NUMA_NODE NUMA_NO_NODE 126115485e8SMike Snitzer static int dm_numa_node = DM_NUMA_NODE; 127faad87dfSMike Snitzer 128a666e5c0SMikulas Patocka #define DEFAULT_SWAP_BIOS (8 * 1048576 / PAGE_SIZE) 129a666e5c0SMikulas Patocka static int swap_bios = DEFAULT_SWAP_BIOS; 130a666e5c0SMikulas Patocka static int get_swap_bios(void) 131a666e5c0SMikulas Patocka { 132a666e5c0SMikulas Patocka int latch = READ_ONCE(swap_bios); 133a666e5c0SMikulas Patocka if (unlikely(latch <= 0)) 134a666e5c0SMikulas Patocka latch = DEFAULT_SWAP_BIOS; 135a666e5c0SMikulas Patocka return latch; 136a666e5c0SMikulas Patocka } 137a666e5c0SMikulas Patocka 138e6ee8c0bSKiyoshi Ueda /* 139e6ee8c0bSKiyoshi Ueda * For mempools pre-allocation at the table loading time. 140e6ee8c0bSKiyoshi Ueda */ 141e6ee8c0bSKiyoshi Ueda struct dm_md_mempools { 1426f1c819cSKent Overstreet struct bio_set bs; 1436f1c819cSKent Overstreet struct bio_set io_bs; 144e6ee8c0bSKiyoshi Ueda }; 145e6ee8c0bSKiyoshi Ueda 14686f1152bSBenjamin Marzinski struct table_device { 14786f1152bSBenjamin Marzinski struct list_head list; 148b0b4d7c6SElena Reshetova refcount_t count; 14986f1152bSBenjamin Marzinski struct dm_dev dm_dev; 15086f1152bSBenjamin Marzinski }; 15186f1152bSBenjamin Marzinski 152f4790826SMike Snitzer /* 153e8603136SMike Snitzer * Bio-based DM's mempools' reserved IOs set by the user. 154e8603136SMike Snitzer */ 1554cc96131SMike Snitzer #define RESERVED_BIO_BASED_IOS 16 156e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS; 157e8603136SMike Snitzer 158115485e8SMike Snitzer static int __dm_get_module_param_int(int *module_param, int min, int max) 159115485e8SMike Snitzer { 1606aa7de05SMark Rutland int param = READ_ONCE(*module_param); 161115485e8SMike Snitzer int modified_param = 0; 162115485e8SMike Snitzer bool modified = true; 163115485e8SMike Snitzer 164115485e8SMike Snitzer if (param < min) 165115485e8SMike Snitzer modified_param = min; 166115485e8SMike Snitzer else if (param > max) 167115485e8SMike Snitzer modified_param = max; 168115485e8SMike Snitzer else 169115485e8SMike Snitzer modified = false; 170115485e8SMike Snitzer 171115485e8SMike Snitzer if (modified) { 172115485e8SMike Snitzer (void)cmpxchg(module_param, param, modified_param); 173115485e8SMike Snitzer param = modified_param; 174115485e8SMike Snitzer } 175115485e8SMike Snitzer 176115485e8SMike Snitzer return param; 177115485e8SMike Snitzer } 178115485e8SMike Snitzer 1794cc96131SMike Snitzer unsigned __dm_get_module_param(unsigned *module_param, 180f4790826SMike Snitzer unsigned def, unsigned max) 181f4790826SMike Snitzer { 1826aa7de05SMark Rutland unsigned param = READ_ONCE(*module_param); 18309c2d531SMike Snitzer unsigned modified_param = 0; 184f4790826SMike Snitzer 18509c2d531SMike Snitzer if (!param) 18609c2d531SMike Snitzer modified_param = def; 18709c2d531SMike Snitzer else if (param > max) 18809c2d531SMike Snitzer modified_param = max; 189f4790826SMike Snitzer 19009c2d531SMike Snitzer if (modified_param) { 19109c2d531SMike Snitzer (void)cmpxchg(module_param, param, modified_param); 19209c2d531SMike Snitzer param = modified_param; 193f4790826SMike Snitzer } 194f4790826SMike Snitzer 19509c2d531SMike Snitzer return param; 196f4790826SMike Snitzer } 197f4790826SMike Snitzer 198e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void) 199e8603136SMike Snitzer { 20009c2d531SMike Snitzer return __dm_get_module_param(&reserved_bio_based_ios, 2014cc96131SMike Snitzer RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS); 202e8603136SMike Snitzer } 203e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios); 204e8603136SMike Snitzer 205115485e8SMike Snitzer static unsigned dm_get_numa_node(void) 206115485e8SMike Snitzer { 207115485e8SMike Snitzer return __dm_get_module_param_int(&dm_numa_node, 208115485e8SMike Snitzer DM_NUMA_NODE, num_online_nodes() - 1); 209115485e8SMike Snitzer } 210115485e8SMike Snitzer 2111da177e4SLinus Torvalds static int __init local_init(void) 2121da177e4SLinus Torvalds { 213e689fbabSMike Snitzer int r; 2141ae49ea2SMike Snitzer 21551e5b2bdSMike Anderson r = dm_uevent_init(); 21651157b4aSKiyoshi Ueda if (r) 217e689fbabSMike Snitzer return r; 21851e5b2bdSMike Anderson 219acfe0ad7SMikulas Patocka deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1); 220acfe0ad7SMikulas Patocka if (!deferred_remove_workqueue) { 221acfe0ad7SMikulas Patocka r = -ENOMEM; 222acfe0ad7SMikulas Patocka goto out_uevent_exit; 223acfe0ad7SMikulas Patocka } 224acfe0ad7SMikulas Patocka 2251da177e4SLinus Torvalds _major = major; 2261da177e4SLinus Torvalds r = register_blkdev(_major, _name); 22751157b4aSKiyoshi Ueda if (r < 0) 228acfe0ad7SMikulas Patocka goto out_free_workqueue; 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds if (!_major) 2311da177e4SLinus Torvalds _major = r; 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds return 0; 23451157b4aSKiyoshi Ueda 235acfe0ad7SMikulas Patocka out_free_workqueue: 236acfe0ad7SMikulas Patocka destroy_workqueue(deferred_remove_workqueue); 23751157b4aSKiyoshi Ueda out_uevent_exit: 23851157b4aSKiyoshi Ueda dm_uevent_exit(); 23951157b4aSKiyoshi Ueda 24051157b4aSKiyoshi Ueda return r; 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds static void local_exit(void) 2441da177e4SLinus Torvalds { 2452c140a24SMikulas Patocka flush_scheduled_work(); 246acfe0ad7SMikulas Patocka destroy_workqueue(deferred_remove_workqueue); 2472c140a24SMikulas Patocka 24800d59405SAkinobu Mita unregister_blkdev(_major, _name); 24951e5b2bdSMike Anderson dm_uevent_exit(); 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds _major = 0; 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds DMINFO("cleaned up"); 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds 256b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = { 2571da177e4SLinus Torvalds local_init, 2581da177e4SLinus Torvalds dm_target_init, 2591da177e4SLinus Torvalds dm_linear_init, 2601da177e4SLinus Torvalds dm_stripe_init, 261952b3557SMikulas Patocka dm_io_init, 262945fa4d2SMikulas Patocka dm_kcopyd_init, 2631da177e4SLinus Torvalds dm_interface_init, 264fd2ed4d2SMikulas Patocka dm_statistics_init, 2651da177e4SLinus Torvalds }; 2661da177e4SLinus Torvalds 267b9249e55SAlasdair G Kergon static void (*_exits[])(void) = { 2681da177e4SLinus Torvalds local_exit, 2691da177e4SLinus Torvalds dm_target_exit, 2701da177e4SLinus Torvalds dm_linear_exit, 2711da177e4SLinus Torvalds dm_stripe_exit, 272952b3557SMikulas Patocka dm_io_exit, 273945fa4d2SMikulas Patocka dm_kcopyd_exit, 2741da177e4SLinus Torvalds dm_interface_exit, 275fd2ed4d2SMikulas Patocka dm_statistics_exit, 2761da177e4SLinus Torvalds }; 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds static int __init dm_init(void) 2791da177e4SLinus Torvalds { 2801da177e4SLinus Torvalds const int count = ARRAY_SIZE(_inits); 2811da177e4SLinus Torvalds int r, i; 2821da177e4SLinus Torvalds 283f1cd6cb2STushar Sugandhi #if (IS_ENABLED(CONFIG_IMA) && !IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE)) 284f1cd6cb2STushar Sugandhi DMWARN("CONFIG_IMA_DISABLE_HTABLE is disabled." 285f1cd6cb2STushar Sugandhi " Duplicate IMA measurements will not be recorded in the IMA log."); 286f1cd6cb2STushar Sugandhi #endif 287f1cd6cb2STushar Sugandhi 2881da177e4SLinus Torvalds for (i = 0; i < count; i++) { 2891da177e4SLinus Torvalds r = _inits[i](); 2901da177e4SLinus Torvalds if (r) 2911da177e4SLinus Torvalds goto bad; 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds return 0; 2951da177e4SLinus Torvalds bad: 2961da177e4SLinus Torvalds while (i--) 2971da177e4SLinus Torvalds _exits[i](); 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds return r; 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds static void __exit dm_exit(void) 3031da177e4SLinus Torvalds { 3041da177e4SLinus Torvalds int i = ARRAY_SIZE(_exits); 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds while (i--) 3071da177e4SLinus Torvalds _exits[i](); 308d15b774cSAlasdair G Kergon 309d15b774cSAlasdair G Kergon /* 310d15b774cSAlasdair G Kergon * Should be empty by this point. 311d15b774cSAlasdair G Kergon */ 312d15b774cSAlasdair G Kergon idr_destroy(&_minor_idr); 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds /* 3161da177e4SLinus Torvalds * Block device functions 3171da177e4SLinus Torvalds */ 318432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md) 319432a212cSMike Anderson { 320432a212cSMike Anderson return test_bit(DMF_DELETING, &md->flags); 321432a212cSMike Anderson } 322432a212cSMike Anderson 323fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode) 3241da177e4SLinus Torvalds { 3251da177e4SLinus Torvalds struct mapped_device *md; 3261da177e4SLinus Torvalds 327fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 328fba9f90eSJeff Mahoney 329fe5f9f2cSAl Viro md = bdev->bd_disk->private_data; 330fba9f90eSJeff Mahoney if (!md) 331fba9f90eSJeff Mahoney goto out; 332fba9f90eSJeff Mahoney 3335c6bd75dSAlasdair G Kergon if (test_bit(DMF_FREEING, &md->flags) || 334432a212cSMike Anderson dm_deleting_md(md)) { 335fba9f90eSJeff Mahoney md = NULL; 336fba9f90eSJeff Mahoney goto out; 337fba9f90eSJeff Mahoney } 338fba9f90eSJeff Mahoney 3391da177e4SLinus Torvalds dm_get(md); 3405c6bd75dSAlasdair G Kergon atomic_inc(&md->open_count); 341fba9f90eSJeff Mahoney out: 342fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 343fba9f90eSJeff Mahoney 344fba9f90eSJeff Mahoney return md ? 0 : -ENXIO; 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds 347db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode) 3481da177e4SLinus Torvalds { 34963a4f065SMike Snitzer struct mapped_device *md; 3506e9624b8SArnd Bergmann 3514a1aeb98SMilan Broz spin_lock(&_minor_lock); 3524a1aeb98SMilan Broz 35363a4f065SMike Snitzer md = disk->private_data; 35463a4f065SMike Snitzer if (WARN_ON(!md)) 35563a4f065SMike Snitzer goto out; 35663a4f065SMike Snitzer 3572c140a24SMikulas Patocka if (atomic_dec_and_test(&md->open_count) && 3582c140a24SMikulas Patocka (test_bit(DMF_DEFERRED_REMOVE, &md->flags))) 359acfe0ad7SMikulas Patocka queue_work(deferred_remove_workqueue, &deferred_remove_work); 3602c140a24SMikulas Patocka 3611da177e4SLinus Torvalds dm_put(md); 36263a4f065SMike Snitzer out: 3634a1aeb98SMilan Broz spin_unlock(&_minor_lock); 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 3665c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md) 3675c6bd75dSAlasdair G Kergon { 3685c6bd75dSAlasdair G Kergon return atomic_read(&md->open_count); 3695c6bd75dSAlasdair G Kergon } 3705c6bd75dSAlasdair G Kergon 3715c6bd75dSAlasdair G Kergon /* 3725c6bd75dSAlasdair G Kergon * Guarantees nothing is using the device before it's deleted. 3735c6bd75dSAlasdair G Kergon */ 3742c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred) 3755c6bd75dSAlasdair G Kergon { 3765c6bd75dSAlasdair G Kergon int r = 0; 3775c6bd75dSAlasdair G Kergon 3785c6bd75dSAlasdair G Kergon spin_lock(&_minor_lock); 3795c6bd75dSAlasdair G Kergon 3802c140a24SMikulas Patocka if (dm_open_count(md)) { 3815c6bd75dSAlasdair G Kergon r = -EBUSY; 3822c140a24SMikulas Patocka if (mark_deferred) 3832c140a24SMikulas Patocka set_bit(DMF_DEFERRED_REMOVE, &md->flags); 3842c140a24SMikulas Patocka } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags)) 3852c140a24SMikulas Patocka r = -EEXIST; 3865c6bd75dSAlasdair G Kergon else 3875c6bd75dSAlasdair G Kergon set_bit(DMF_DELETING, &md->flags); 3885c6bd75dSAlasdair G Kergon 3895c6bd75dSAlasdair G Kergon spin_unlock(&_minor_lock); 3905c6bd75dSAlasdair G Kergon 3915c6bd75dSAlasdair G Kergon return r; 3925c6bd75dSAlasdair G Kergon } 3935c6bd75dSAlasdair G Kergon 3942c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md) 3952c140a24SMikulas Patocka { 3962c140a24SMikulas Patocka int r = 0; 3972c140a24SMikulas Patocka 3982c140a24SMikulas Patocka spin_lock(&_minor_lock); 3992c140a24SMikulas Patocka 4002c140a24SMikulas Patocka if (test_bit(DMF_DELETING, &md->flags)) 4012c140a24SMikulas Patocka r = -EBUSY; 4022c140a24SMikulas Patocka else 4032c140a24SMikulas Patocka clear_bit(DMF_DEFERRED_REMOVE, &md->flags); 4042c140a24SMikulas Patocka 4052c140a24SMikulas Patocka spin_unlock(&_minor_lock); 4062c140a24SMikulas Patocka 4072c140a24SMikulas Patocka return r; 4082c140a24SMikulas Patocka } 4092c140a24SMikulas Patocka 4102c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w) 4112c140a24SMikulas Patocka { 4122c140a24SMikulas Patocka dm_deferred_remove(); 4132c140a24SMikulas Patocka } 4142c140a24SMikulas Patocka 4153ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 4163ac51e74SDarrick J. Wong { 4173ac51e74SDarrick J. Wong struct mapped_device *md = bdev->bd_disk->private_data; 4183ac51e74SDarrick J. Wong 4193ac51e74SDarrick J. Wong return dm_get_geometry(md, geo); 4203ac51e74SDarrick J. Wong } 4213ac51e74SDarrick J. Wong 422971888c4SMike Snitzer static int dm_prepare_ioctl(struct mapped_device *md, int *srcu_idx, 4235bd5e8d8SMike Snitzer struct block_device **bdev) 424aa129a22SMilan Broz { 42566482026SMike Snitzer struct dm_target *tgt; 4266c182cd8SHannes Reinecke struct dm_table *map; 427971888c4SMike Snitzer int r; 428aa129a22SMilan Broz 4296c182cd8SHannes Reinecke retry: 430e56f81e0SChristoph Hellwig r = -ENOTTY; 431971888c4SMike Snitzer map = dm_get_live_table(md, srcu_idx); 432aa129a22SMilan Broz if (!map || !dm_table_get_size(map)) 433971888c4SMike Snitzer return r; 434aa129a22SMilan Broz 435aa129a22SMilan Broz /* We only support devices that have a single target */ 436aa129a22SMilan Broz if (dm_table_get_num_targets(map) != 1) 437971888c4SMike Snitzer return r; 438aa129a22SMilan Broz 43966482026SMike Snitzer tgt = dm_table_get_target(map, 0); 44066482026SMike Snitzer if (!tgt->type->prepare_ioctl) 441e56f81e0SChristoph Hellwig return r; 442aa129a22SMilan Broz 443971888c4SMike Snitzer if (dm_suspended_md(md)) 444971888c4SMike Snitzer return -EAGAIN; 445971888c4SMike Snitzer 4465bd5e8d8SMike Snitzer r = tgt->type->prepare_ioctl(tgt, bdev); 4475bbbfdf6SJunichi Nomura if (r == -ENOTCONN && !fatal_signal_pending(current)) { 448971888c4SMike Snitzer dm_put_live_table(md, *srcu_idx); 4496c182cd8SHannes Reinecke msleep(10); 4506c182cd8SHannes Reinecke goto retry; 4516c182cd8SHannes Reinecke } 452971888c4SMike Snitzer 453e56f81e0SChristoph Hellwig return r; 454e56f81e0SChristoph Hellwig } 4556c182cd8SHannes Reinecke 456971888c4SMike Snitzer static void dm_unprepare_ioctl(struct mapped_device *md, int srcu_idx) 457971888c4SMike Snitzer { 458971888c4SMike Snitzer dm_put_live_table(md, srcu_idx); 459971888c4SMike Snitzer } 460971888c4SMike Snitzer 461e56f81e0SChristoph Hellwig static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode, 462e56f81e0SChristoph Hellwig unsigned int cmd, unsigned long arg) 463e56f81e0SChristoph Hellwig { 464e56f81e0SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 465971888c4SMike Snitzer int r, srcu_idx; 466e56f81e0SChristoph Hellwig 4675bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 468e56f81e0SChristoph Hellwig if (r < 0) 469971888c4SMike Snitzer goto out; 470e56f81e0SChristoph Hellwig 471e56f81e0SChristoph Hellwig if (r > 0) { 472e56f81e0SChristoph Hellwig /* 473e980f623SChristoph Hellwig * Target determined this ioctl is being issued against a 474e980f623SChristoph Hellwig * subset of the parent bdev; require extra privileges. 475e56f81e0SChristoph Hellwig */ 476e980f623SChristoph Hellwig if (!capable(CAP_SYS_RAWIO)) { 4770378c625SMike Snitzer DMDEBUG_LIMIT( 478e980f623SChristoph Hellwig "%s: sending ioctl %x to DM device without required privilege.", 479e980f623SChristoph Hellwig current->comm, cmd); 480e980f623SChristoph Hellwig r = -ENOIOCTLCMD; 481e56f81e0SChristoph Hellwig goto out; 482e56f81e0SChristoph Hellwig } 483e980f623SChristoph Hellwig } 484e56f81e0SChristoph Hellwig 485a7cb3d2fSChristoph Hellwig if (!bdev->bd_disk->fops->ioctl) 486a7cb3d2fSChristoph Hellwig r = -ENOTTY; 487a7cb3d2fSChristoph Hellwig else 488a7cb3d2fSChristoph Hellwig r = bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg); 489e56f81e0SChristoph Hellwig out: 490971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 491aa129a22SMilan Broz return r; 492aa129a22SMilan Broz } 493aa129a22SMilan Broz 4947465d7acSMike Snitzer u64 dm_start_time_ns_from_clone(struct bio *bio) 4957465d7acSMike Snitzer { 4966c23f0bdSChristoph Hellwig return jiffies_to_nsecs(clone_to_tio(bio)->io->start_time); 4977465d7acSMike Snitzer } 4987465d7acSMike Snitzer EXPORT_SYMBOL_GPL(dm_start_time_ns_from_clone); 4997465d7acSMike Snitzer 5008d394bc4SMike Snitzer static bool bio_is_flush_with_data(struct bio *bio) 5017465d7acSMike Snitzer { 5028d394bc4SMike Snitzer return ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size); 5037465d7acSMike Snitzer } 5047465d7acSMike Snitzer 5058d394bc4SMike Snitzer static void dm_io_acct(bool end, struct mapped_device *md, struct bio *bio, 506d208b894SJiazi Li unsigned long start_time, struct dm_stats_aux *stats_aux) 5077465d7acSMike Snitzer { 5088d394bc4SMike Snitzer bool is_flush_with_data; 5098d394bc4SMike Snitzer unsigned int bi_size; 5107465d7acSMike Snitzer 5118d394bc4SMike Snitzer /* If REQ_PREFLUSH set save any payload but do not account it */ 5128d394bc4SMike Snitzer is_flush_with_data = bio_is_flush_with_data(bio); 5138d394bc4SMike Snitzer if (is_flush_with_data) { 5148d394bc4SMike Snitzer bi_size = bio->bi_iter.bi_size; 5158d394bc4SMike Snitzer bio->bi_iter.bi_size = 0; 5168d394bc4SMike Snitzer } 5178d394bc4SMike Snitzer 5188d394bc4SMike Snitzer if (!end) 5198d394bc4SMike Snitzer bio_start_io_acct_time(bio, start_time); 5208d394bc4SMike Snitzer else 521d208b894SJiazi Li bio_end_io_acct(bio, start_time); 5227465d7acSMike Snitzer 523442761fdSMike Snitzer if (static_branch_unlikely(&stats_enabled) && 524442761fdSMike Snitzer unlikely(dm_stats_used(&md->stats))) 5257465d7acSMike Snitzer dm_stats_account_io(&md->stats, bio_data_dir(bio), 5267465d7acSMike Snitzer bio->bi_iter.bi_sector, bio_sectors(bio), 5278d394bc4SMike Snitzer end, start_time, stats_aux); 5287465d7acSMike Snitzer 5298d394bc4SMike Snitzer /* Restore bio's payload so it does get accounted upon requeue */ 5308d394bc4SMike Snitzer if (is_flush_with_data) 5318d394bc4SMike Snitzer bio->bi_iter.bi_size = bi_size; 5328d394bc4SMike Snitzer } 5338d394bc4SMike Snitzer 5340fbb4d93SMike Snitzer static void __dm_start_io_acct(struct dm_io *io, struct bio *bio) 5358d394bc4SMike Snitzer { 5360fbb4d93SMike Snitzer dm_io_acct(false, io->md, bio, io->start_time, &io->stats_aux); 5378d394bc4SMike Snitzer } 5388d394bc4SMike Snitzer 5390fbb4d93SMike Snitzer static void dm_start_io_acct(struct dm_io *io, struct bio *clone) 5408d394bc4SMike Snitzer { 5410fbb4d93SMike Snitzer /* Must account IO to DM device in terms of orig_bio */ 5420fbb4d93SMike Snitzer struct bio *bio = io->orig_bio; 5430fbb4d93SMike Snitzer 5440fbb4d93SMike Snitzer /* 5450fbb4d93SMike Snitzer * Ensure IO accounting is only ever started once. 5460fbb4d93SMike Snitzer */ 5473b03f7c1SMike Snitzer if (dm_io_flagged(io, DM_IO_ACCOUNTED)) 5480fbb4d93SMike Snitzer return; 5493b03f7c1SMike Snitzer 5503b03f7c1SMike Snitzer /* Expect no possibility for race unless DM_TIO_IS_DUPLICATE_BIO. */ 5513b03f7c1SMike Snitzer if (!clone || likely(dm_tio_is_normal(clone_to_tio(clone)))) { 55282f6cdccSMike Snitzer dm_io_set_flag(io, DM_IO_ACCOUNTED); 55382f6cdccSMike Snitzer } else { 55482f6cdccSMike Snitzer unsigned long flags; 555655f3aadSMike Snitzer /* Can afford locking given DM_TIO_IS_DUPLICATE_BIO */ 5564d7bca13SMike Snitzer spin_lock_irqsave(&io->lock, flags); 55782f6cdccSMike Snitzer dm_io_set_flag(io, DM_IO_ACCOUNTED); 5584d7bca13SMike Snitzer spin_unlock_irqrestore(&io->lock, flags); 55982f6cdccSMike Snitzer } 5600fbb4d93SMike Snitzer 5610fbb4d93SMike Snitzer __dm_start_io_acct(io, bio); 5620fbb4d93SMike Snitzer } 5630fbb4d93SMike Snitzer 5640fbb4d93SMike Snitzer static void dm_end_io_acct(struct dm_io *io, struct bio *bio) 5650fbb4d93SMike Snitzer { 5660fbb4d93SMike Snitzer dm_io_acct(true, io->md, bio, io->start_time, &io->stats_aux); 5677465d7acSMike Snitzer } 568978e51baSMike Snitzer 569978e51baSMike Snitzer static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio) 5701da177e4SLinus Torvalds { 57164f52b0eSMike Snitzer struct dm_io *io; 57264f52b0eSMike Snitzer struct dm_target_io *tio; 57364f52b0eSMike Snitzer struct bio *clone; 57464f52b0eSMike Snitzer 575abfc426dSChristoph Hellwig clone = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO, &md->io_bs); 57664f52b0eSMike Snitzer 5776c23f0bdSChristoph Hellwig tio = clone_to_tio(clone); 578655f3aadSMike Snitzer tio->flags = 0; 579655f3aadSMike Snitzer dm_tio_set_flag(tio, DM_TIO_INSIDE_DM_IO); 58064f52b0eSMike Snitzer tio->io = NULL; 58164f52b0eSMike Snitzer 58264f52b0eSMike Snitzer io = container_of(tio, struct dm_io, tio); 58364f52b0eSMike Snitzer io->magic = DM_IO_MAGIC; 58484b98f4cSMike Snitzer io->status = BLK_STS_OK; 585978e51baSMike Snitzer atomic_set(&io->io_count, 1); 5869f6dc633SMike Snitzer this_cpu_inc(*md->pending_io); 5870fbb4d93SMike Snitzer io->orig_bio = NULL; 588978e51baSMike Snitzer io->md = md; 589b7f8dff0SMike Snitzer io->map_task = current; 5904d7bca13SMike Snitzer spin_lock_init(&io->lock); 591b879f915SMike Snitzer io->start_time = jiffies; 59282f6cdccSMike Snitzer io->flags = 0; 59364f52b0eSMike Snitzer 594442761fdSMike Snitzer if (static_branch_unlikely(&stats_enabled)) 5950cdb90f0SMike Snitzer dm_stats_record_start(&md->stats, &io->stats_aux); 59664f52b0eSMike Snitzer 59764f52b0eSMike Snitzer return io; 5981da177e4SLinus Torvalds } 5991da177e4SLinus Torvalds 6000119ab14SMike Snitzer static void free_io(struct dm_io *io) 6011da177e4SLinus Torvalds { 60264f52b0eSMike Snitzer bio_put(&io->tio.clone); 60364f52b0eSMike Snitzer } 60464f52b0eSMike Snitzer 6051d1068ceSChristoph Hellwig static struct bio *alloc_tio(struct clone_info *ci, struct dm_target *ti, 606dc8e2021SChristoph Hellwig unsigned target_bio_nr, unsigned *len, gfp_t gfp_mask) 60764f52b0eSMike Snitzer { 60864f52b0eSMike Snitzer struct dm_target_io *tio; 609018b05ebSMike Snitzer struct bio *clone; 61064f52b0eSMike Snitzer 61164f52b0eSMike Snitzer if (!ci->io->tio.io) { 61264f52b0eSMike Snitzer /* the dm_target_io embedded in ci->io is available */ 61364f52b0eSMike Snitzer tio = &ci->io->tio; 614018b05ebSMike Snitzer /* alloc_io() already initialized embedded clone */ 615018b05ebSMike Snitzer clone = &tio->clone; 61664f52b0eSMike Snitzer } else { 617018b05ebSMike Snitzer clone = bio_alloc_clone(ci->bio->bi_bdev, ci->bio, 618abfc426dSChristoph Hellwig gfp_mask, &ci->io->md->bs); 61964f52b0eSMike Snitzer if (!clone) 62064f52b0eSMike Snitzer return NULL; 62164f52b0eSMike Snitzer 622b99fdcdcSMing Lei /* REQ_DM_POLL_LIST shouldn't be inherited */ 623b99fdcdcSMing Lei clone->bi_opf &= ~REQ_DM_POLL_LIST; 624b99fdcdcSMing Lei 6256c23f0bdSChristoph Hellwig tio = clone_to_tio(clone); 626655f3aadSMike Snitzer tio->flags = 0; /* also clears DM_TIO_INSIDE_DM_IO */ 62764f52b0eSMike Snitzer } 62864f52b0eSMike Snitzer 62964f52b0eSMike Snitzer tio->magic = DM_TIO_MAGIC; 63064f52b0eSMike Snitzer tio->io = ci->io; 63164f52b0eSMike Snitzer tio->ti = ti; 63264f52b0eSMike Snitzer tio->target_bio_nr = target_bio_nr; 633dc8e2021SChristoph Hellwig tio->len_ptr = len; 634743598f0SMike Snitzer tio->old_sector = 0; 63564f52b0eSMike Snitzer 636018b05ebSMike Snitzer if (len) { 637018b05ebSMike Snitzer clone->bi_iter.bi_size = to_bytes(*len); 638018b05ebSMike Snitzer if (bio_integrity(clone)) 639018b05ebSMike Snitzer bio_integrity_trim(clone); 640018b05ebSMike Snitzer } 641018b05ebSMike Snitzer 642018b05ebSMike Snitzer return clone; 6431da177e4SLinus Torvalds } 6441da177e4SLinus Torvalds 6451d1068ceSChristoph Hellwig static void free_tio(struct bio *clone) 6461da177e4SLinus Torvalds { 647655f3aadSMike Snitzer if (dm_tio_flagged(clone_to_tio(clone), DM_TIO_INSIDE_DM_IO)) 64864f52b0eSMike Snitzer return; 6491d1068ceSChristoph Hellwig bio_put(clone); 6501da177e4SLinus Torvalds } 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds /* 6531da177e4SLinus Torvalds * Add the bio to the list of deferred io. 6541da177e4SLinus Torvalds */ 65592c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio) 6561da177e4SLinus Torvalds { 65705447420SKiyoshi Ueda unsigned long flags; 6581da177e4SLinus Torvalds 65905447420SKiyoshi Ueda spin_lock_irqsave(&md->deferred_lock, flags); 6601da177e4SLinus Torvalds bio_list_add(&md->deferred, bio); 66105447420SKiyoshi Ueda spin_unlock_irqrestore(&md->deferred_lock, flags); 66292c63902SMikulas Patocka queue_work(md->wq, &md->work); 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds /* 6661da177e4SLinus Torvalds * Everyone (including functions in this file), should use this 6671da177e4SLinus Torvalds * function to access the md->map field, and make sure they call 66883d5e5b0SMikulas Patocka * dm_put_live_table() when finished. 6691da177e4SLinus Torvalds */ 670563a225cSMike Snitzer struct dm_table *dm_get_live_table(struct mapped_device *md, 671563a225cSMike Snitzer int *srcu_idx) __acquires(md->io_barrier) 6721da177e4SLinus Torvalds { 67383d5e5b0SMikulas Patocka *srcu_idx = srcu_read_lock(&md->io_barrier); 6741da177e4SLinus Torvalds 67583d5e5b0SMikulas Patocka return srcu_dereference(md->map, &md->io_barrier); 67683d5e5b0SMikulas Patocka } 6771da177e4SLinus Torvalds 678563a225cSMike Snitzer void dm_put_live_table(struct mapped_device *md, 679563a225cSMike Snitzer int srcu_idx) __releases(md->io_barrier) 68083d5e5b0SMikulas Patocka { 68183d5e5b0SMikulas Patocka srcu_read_unlock(&md->io_barrier, srcu_idx); 68283d5e5b0SMikulas Patocka } 68383d5e5b0SMikulas Patocka 68483d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md) 68583d5e5b0SMikulas Patocka { 68683d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 68783d5e5b0SMikulas Patocka synchronize_rcu_expedited(); 68883d5e5b0SMikulas Patocka } 68983d5e5b0SMikulas Patocka 69083d5e5b0SMikulas Patocka /* 69183d5e5b0SMikulas Patocka * A fast alternative to dm_get_live_table/dm_put_live_table. 69283d5e5b0SMikulas Patocka * The caller must not block between these two functions. 69383d5e5b0SMikulas Patocka */ 69483d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU) 69583d5e5b0SMikulas Patocka { 69683d5e5b0SMikulas Patocka rcu_read_lock(); 69783d5e5b0SMikulas Patocka return rcu_dereference(md->map); 69883d5e5b0SMikulas Patocka } 69983d5e5b0SMikulas Patocka 70083d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU) 70183d5e5b0SMikulas Patocka { 70283d5e5b0SMikulas Patocka rcu_read_unlock(); 7031da177e4SLinus Torvalds } 7041da177e4SLinus Torvalds 705563a225cSMike Snitzer static inline struct dm_table *dm_get_live_table_bio(struct mapped_device *md, 706563a225cSMike Snitzer int *srcu_idx, struct bio *bio) 707563a225cSMike Snitzer { 708563a225cSMike Snitzer if (bio->bi_opf & REQ_NOWAIT) 709563a225cSMike Snitzer return dm_get_live_table_fast(md); 710563a225cSMike Snitzer else 711563a225cSMike Snitzer return dm_get_live_table(md, srcu_idx); 712563a225cSMike Snitzer } 713563a225cSMike Snitzer 714563a225cSMike Snitzer static inline void dm_put_live_table_bio(struct mapped_device *md, int srcu_idx, 715563a225cSMike Snitzer struct bio *bio) 716563a225cSMike Snitzer { 717563a225cSMike Snitzer if (bio->bi_opf & REQ_NOWAIT) 718563a225cSMike Snitzer dm_put_live_table_fast(md); 719563a225cSMike Snitzer else 720563a225cSMike Snitzer dm_put_live_table(md, srcu_idx); 721563a225cSMike Snitzer } 722563a225cSMike Snitzer 723971888c4SMike Snitzer static char *_dm_claim_ptr = "I belong to device-mapper"; 724971888c4SMike Snitzer 7253ac51e74SDarrick J. Wong /* 72686f1152bSBenjamin Marzinski * Open a table device so we can use it as a map destination. 72786f1152bSBenjamin Marzinski */ 72886f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev, 72986f1152bSBenjamin Marzinski struct mapped_device *md) 73086f1152bSBenjamin Marzinski { 73186f1152bSBenjamin Marzinski struct block_device *bdev; 732cd913c76SChristoph Hellwig u64 part_off; 73386f1152bSBenjamin Marzinski int r; 73486f1152bSBenjamin Marzinski 73586f1152bSBenjamin Marzinski BUG_ON(td->dm_dev.bdev); 73686f1152bSBenjamin Marzinski 737519049afSMike Snitzer bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr); 73886f1152bSBenjamin Marzinski if (IS_ERR(bdev)) 73986f1152bSBenjamin Marzinski return PTR_ERR(bdev); 74086f1152bSBenjamin Marzinski 74186f1152bSBenjamin Marzinski r = bd_link_disk_holder(bdev, dm_disk(md)); 74286f1152bSBenjamin Marzinski if (r) { 74386f1152bSBenjamin Marzinski blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL); 74486f1152bSBenjamin Marzinski return r; 74586f1152bSBenjamin Marzinski } 74686f1152bSBenjamin Marzinski 74786f1152bSBenjamin Marzinski td->dm_dev.bdev = bdev; 748cd913c76SChristoph Hellwig td->dm_dev.dax_dev = fs_dax_get_by_bdev(bdev, &part_off); 74986f1152bSBenjamin Marzinski return 0; 75086f1152bSBenjamin Marzinski } 75186f1152bSBenjamin Marzinski 75286f1152bSBenjamin Marzinski /* 75386f1152bSBenjamin Marzinski * Close a table device that we've been using. 75486f1152bSBenjamin Marzinski */ 75586f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md) 75686f1152bSBenjamin Marzinski { 75786f1152bSBenjamin Marzinski if (!td->dm_dev.bdev) 75886f1152bSBenjamin Marzinski return; 75986f1152bSBenjamin Marzinski 76086f1152bSBenjamin Marzinski bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md)); 76186f1152bSBenjamin Marzinski blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL); 762817bf402SDan Williams put_dax(td->dm_dev.dax_dev); 76386f1152bSBenjamin Marzinski td->dm_dev.bdev = NULL; 764817bf402SDan Williams td->dm_dev.dax_dev = NULL; 76586f1152bSBenjamin Marzinski } 76686f1152bSBenjamin Marzinski 76786f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev, 7688454fca4SSheetal Singala fmode_t mode) 7698454fca4SSheetal Singala { 77086f1152bSBenjamin Marzinski struct table_device *td; 77186f1152bSBenjamin Marzinski 77286f1152bSBenjamin Marzinski list_for_each_entry(td, l, list) 77386f1152bSBenjamin Marzinski if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode) 77486f1152bSBenjamin Marzinski return td; 77586f1152bSBenjamin Marzinski 77686f1152bSBenjamin Marzinski return NULL; 77786f1152bSBenjamin Marzinski } 77886f1152bSBenjamin Marzinski 77986f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode, 7808454fca4SSheetal Singala struct dm_dev **result) 7818454fca4SSheetal Singala { 78286f1152bSBenjamin Marzinski int r; 78386f1152bSBenjamin Marzinski struct table_device *td; 78486f1152bSBenjamin Marzinski 78586f1152bSBenjamin Marzinski mutex_lock(&md->table_devices_lock); 78686f1152bSBenjamin Marzinski td = find_table_device(&md->table_devices, dev, mode); 78786f1152bSBenjamin Marzinski if (!td) { 788115485e8SMike Snitzer td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id); 78986f1152bSBenjamin Marzinski if (!td) { 79086f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 79186f1152bSBenjamin Marzinski return -ENOMEM; 79286f1152bSBenjamin Marzinski } 79386f1152bSBenjamin Marzinski 79486f1152bSBenjamin Marzinski td->dm_dev.mode = mode; 79586f1152bSBenjamin Marzinski td->dm_dev.bdev = NULL; 79686f1152bSBenjamin Marzinski 79786f1152bSBenjamin Marzinski if ((r = open_table_device(td, dev, md))) { 79886f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 79986f1152bSBenjamin Marzinski kfree(td); 80086f1152bSBenjamin Marzinski return r; 80186f1152bSBenjamin Marzinski } 80286f1152bSBenjamin Marzinski 80386f1152bSBenjamin Marzinski format_dev_t(td->dm_dev.name, dev); 80486f1152bSBenjamin Marzinski 805b0b4d7c6SElena Reshetova refcount_set(&td->count, 1); 80686f1152bSBenjamin Marzinski list_add(&td->list, &md->table_devices); 807b0b4d7c6SElena Reshetova } else { 808b0b4d7c6SElena Reshetova refcount_inc(&td->count); 80986f1152bSBenjamin Marzinski } 81086f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 81186f1152bSBenjamin Marzinski 81286f1152bSBenjamin Marzinski *result = &td->dm_dev; 81386f1152bSBenjamin Marzinski return 0; 81486f1152bSBenjamin Marzinski } 81586f1152bSBenjamin Marzinski 81686f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d) 81786f1152bSBenjamin Marzinski { 81886f1152bSBenjamin Marzinski struct table_device *td = container_of(d, struct table_device, dm_dev); 81986f1152bSBenjamin Marzinski 82086f1152bSBenjamin Marzinski mutex_lock(&md->table_devices_lock); 821b0b4d7c6SElena Reshetova if (refcount_dec_and_test(&td->count)) { 82286f1152bSBenjamin Marzinski close_table_device(td, md); 82386f1152bSBenjamin Marzinski list_del(&td->list); 82486f1152bSBenjamin Marzinski kfree(td); 82586f1152bSBenjamin Marzinski } 82686f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 82786f1152bSBenjamin Marzinski } 82886f1152bSBenjamin Marzinski 82986f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices) 83086f1152bSBenjamin Marzinski { 83186f1152bSBenjamin Marzinski struct list_head *tmp, *next; 83286f1152bSBenjamin Marzinski 83386f1152bSBenjamin Marzinski list_for_each_safe(tmp, next, devices) { 83486f1152bSBenjamin Marzinski struct table_device *td = list_entry(tmp, struct table_device, list); 83586f1152bSBenjamin Marzinski 83686f1152bSBenjamin Marzinski DMWARN("dm_destroy: %s still exists with %d references", 837b0b4d7c6SElena Reshetova td->dm_dev.name, refcount_read(&td->count)); 83886f1152bSBenjamin Marzinski kfree(td); 83986f1152bSBenjamin Marzinski } 84086f1152bSBenjamin Marzinski } 84186f1152bSBenjamin Marzinski 84286f1152bSBenjamin Marzinski /* 8433ac51e74SDarrick J. Wong * Get the geometry associated with a dm device 8443ac51e74SDarrick J. Wong */ 8453ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) 8463ac51e74SDarrick J. Wong { 8473ac51e74SDarrick J. Wong *geo = md->geometry; 8483ac51e74SDarrick J. Wong 8493ac51e74SDarrick J. Wong return 0; 8503ac51e74SDarrick J. Wong } 8513ac51e74SDarrick J. Wong 8523ac51e74SDarrick J. Wong /* 8533ac51e74SDarrick J. Wong * Set the geometry of a device. 8543ac51e74SDarrick J. Wong */ 8553ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) 8563ac51e74SDarrick J. Wong { 8573ac51e74SDarrick J. Wong sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; 8583ac51e74SDarrick J. Wong 8593ac51e74SDarrick J. Wong if (geo->start > sz) { 8603ac51e74SDarrick J. Wong DMWARN("Start sector is beyond the geometry limits."); 8613ac51e74SDarrick J. Wong return -EINVAL; 8623ac51e74SDarrick J. Wong } 8633ac51e74SDarrick J. Wong 8643ac51e74SDarrick J. Wong md->geometry = *geo; 8653ac51e74SDarrick J. Wong 8663ac51e74SDarrick J. Wong return 0; 8673ac51e74SDarrick J. Wong } 8683ac51e74SDarrick J. Wong 8692e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md) 8702e93ccc1SKiyoshi Ueda { 8712e93ccc1SKiyoshi Ueda return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 8722e93ccc1SKiyoshi Ueda } 8732e93ccc1SKiyoshi Ueda 874e2736347SMike Snitzer static void dm_io_complete(struct dm_io *io) 8751da177e4SLinus Torvalds { 8764e4cbee9SChristoph Hellwig blk_status_t io_error; 877b35f8caaSMilan Broz struct mapped_device *md = io->md; 878e2736347SMike Snitzer struct bio *bio = io->orig_bio; 8792e93ccc1SKiyoshi Ueda 8804e4cbee9SChristoph Hellwig if (io->status == BLK_STS_DM_REQUEUE) { 881e2736347SMike Snitzer unsigned long flags; 8822e93ccc1SKiyoshi Ueda /* 8832e93ccc1SKiyoshi Ueda * Target requested pushing back the I/O. 8842e93ccc1SKiyoshi Ueda */ 885022c2611SMikulas Patocka spin_lock_irqsave(&md->deferred_lock, flags); 886bf14e2b2SDamien Le Moal if (__noflush_suspending(md) && 887bf14e2b2SDamien Le Moal !WARN_ON_ONCE(dm_is_zone_write(md, bio))) { 888745dc570SMike Snitzer /* NOTE early return due to BLK_STS_DM_REQUEUE below */ 889bf14e2b2SDamien Le Moal bio_list_add_head(&md->deferred, bio); 890bf14e2b2SDamien Le Moal } else { 891bf14e2b2SDamien Le Moal /* 892bf14e2b2SDamien Le Moal * noflush suspend was interrupted or this is 893bf14e2b2SDamien Le Moal * a write to a zoned target. 894bf14e2b2SDamien Le Moal */ 8954e4cbee9SChristoph Hellwig io->status = BLK_STS_IOERR; 896bf14e2b2SDamien Le Moal } 897022c2611SMikulas Patocka spin_unlock_irqrestore(&md->deferred_lock, flags); 8982e93ccc1SKiyoshi Ueda } 8992e93ccc1SKiyoshi Ueda 9004e4cbee9SChristoph Hellwig io_error = io->status; 90182f6cdccSMike Snitzer if (dm_io_flagged(io, DM_IO_ACCOUNTED)) 9020fbb4d93SMike Snitzer dm_end_io_acct(io, bio); 9030fbb4d93SMike Snitzer else if (!io_error) { 9040fbb4d93SMike Snitzer /* 9050fbb4d93SMike Snitzer * Must handle target that DM_MAPIO_SUBMITTED only to 9060fbb4d93SMike Snitzer * then bio_endio() rather than dm_submit_bio_remap() 9070fbb4d93SMike Snitzer */ 9080fbb4d93SMike Snitzer __dm_start_io_acct(io, bio); 9090fbb4d93SMike Snitzer dm_end_io_acct(io, bio); 9100fbb4d93SMike Snitzer } 9110119ab14SMike Snitzer free_io(io); 9129f6dc633SMike Snitzer smp_wmb(); 9139f6dc633SMike Snitzer this_cpu_dec(*md->pending_io); 9142056a782SJens Axboe 9159f6dc633SMike Snitzer /* nudge anyone waiting on suspend queue */ 9169f6dc633SMike Snitzer if (unlikely(wq_has_sleeper(&md->wait))) 9179f6dc633SMike Snitzer wake_up(&md->wait); 9181da177e4SLinus Torvalds 91952919840SMing Lei if (io_error == BLK_STS_DM_REQUEUE || io_error == BLK_STS_AGAIN) { 92052919840SMing Lei if (bio->bi_opf & REQ_POLLED) { 921b99fdcdcSMing Lei /* 92252919840SMing Lei * Upper layer won't help us poll split bio (io->orig_bio 92352919840SMing Lei * may only reflect a subset of the pre-split original) 92452919840SMing Lei * so clear REQ_POLLED in case of requeue. 925b99fdcdcSMing Lei */ 926cfc97abcSMike Snitzer bio_clear_polled(bio); 92752919840SMing Lei if (io_error == BLK_STS_AGAIN) { 92852919840SMing Lei /* io_uring doesn't handle BLK_STS_AGAIN (yet) */ 92952919840SMing Lei queue_io(md, bio); 93052919840SMing Lei } 93152919840SMing Lei } 9326a8736d1STejun Heo return; 933b99fdcdcSMing Lei } 9346a8736d1STejun Heo 9358d394bc4SMike Snitzer if (bio_is_flush_with_data(bio)) { 936af7e466aSMikulas Patocka /* 9376a8736d1STejun Heo * Preflush done for flush with data, reissue 93828a8f0d3SMike Christie * without REQ_PREFLUSH. 939af7e466aSMikulas Patocka */ 9401eff9d32SJens Axboe bio->bi_opf &= ~REQ_PREFLUSH; 9416a8736d1STejun Heo queue_io(md, bio); 942af7e466aSMikulas Patocka } else { 943b372d360SMike Snitzer /* done with normal IO or empty flush */ 9448dd601faSNeilBrown if (io_error) 9454e4cbee9SChristoph Hellwig bio->bi_status = io_error; 9464246a0b6SChristoph Hellwig bio_endio(bio); 9472e93ccc1SKiyoshi Ueda } 9481da177e4SLinus Torvalds } 949e2736347SMike Snitzer 950e2736347SMike Snitzer /* 951e2736347SMike Snitzer * Decrements the number of outstanding ios that a bio has been 952e2736347SMike Snitzer * cloned into, completing the original io if necc. 953e2736347SMike Snitzer */ 95484b98f4cSMike Snitzer static inline void __dm_io_dec_pending(struct dm_io *io) 955e2736347SMike Snitzer { 95684b98f4cSMike Snitzer if (atomic_dec_and_test(&io->io_count)) 95784b98f4cSMike Snitzer dm_io_complete(io); 95884b98f4cSMike Snitzer } 95984b98f4cSMike Snitzer 96084b98f4cSMike Snitzer static void dm_io_set_error(struct dm_io *io, blk_status_t error) 96184b98f4cSMike Snitzer { 962e2736347SMike Snitzer unsigned long flags; 96384b98f4cSMike Snitzer 96484b98f4cSMike Snitzer /* Push-back supersedes any I/O errors */ 9654d7bca13SMike Snitzer spin_lock_irqsave(&io->lock, flags); 966e2736347SMike Snitzer if (!(io->status == BLK_STS_DM_REQUEUE && 96784b98f4cSMike Snitzer __noflush_suspending(io->md))) { 968e2736347SMike Snitzer io->status = error; 96984b98f4cSMike Snitzer } 9704d7bca13SMike Snitzer spin_unlock_irqrestore(&io->lock, flags); 971e2736347SMike Snitzer } 972e2736347SMike Snitzer 97384b98f4cSMike Snitzer void dm_io_dec_pending(struct dm_io *io, blk_status_t error) 97484b98f4cSMike Snitzer { 97584b98f4cSMike Snitzer if (unlikely(error)) 97684b98f4cSMike Snitzer dm_io_set_error(io, error); 97784b98f4cSMike Snitzer 97884b98f4cSMike Snitzer __dm_io_dec_pending(io); 979af7e466aSMikulas Patocka } 9801da177e4SLinus Torvalds 981bcb44433SMike Snitzer void disable_discard(struct mapped_device *md) 982bcb44433SMike Snitzer { 983bcb44433SMike Snitzer struct queue_limits *limits = dm_get_queue_limits(md); 984bcb44433SMike Snitzer 985bcb44433SMike Snitzer /* device doesn't really support DISCARD, disable it */ 986bcb44433SMike Snitzer limits->max_discard_sectors = 0; 987bcb44433SMike Snitzer } 988bcb44433SMike Snitzer 989ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md) 990ac62d620SChristoph Hellwig { 991ac62d620SChristoph Hellwig struct queue_limits *limits = dm_get_queue_limits(md); 992ac62d620SChristoph Hellwig 993ac62d620SChristoph Hellwig /* device doesn't really support WRITE ZEROES, disable it */ 994ac62d620SChristoph Hellwig limits->max_write_zeroes_sectors = 0; 995ac62d620SChristoph Hellwig } 996ac62d620SChristoph Hellwig 997a666e5c0SMikulas Patocka static bool swap_bios_limit(struct dm_target *ti, struct bio *bio) 998a666e5c0SMikulas Patocka { 999a666e5c0SMikulas Patocka return unlikely((bio->bi_opf & REQ_SWAP) != 0) && unlikely(ti->limit_swap_bios); 1000a666e5c0SMikulas Patocka } 1001a666e5c0SMikulas Patocka 10024246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio) 10031da177e4SLinus Torvalds { 10044e4cbee9SChristoph Hellwig blk_status_t error = bio->bi_status; 1005309dca30SChristoph Hellwig struct request_queue *q = bio->bi_bdev->bd_disk->queue; 10066cbce280SMike Snitzer struct dm_target_io *tio = clone_to_tio(bio); 10076cbce280SMike Snitzer struct dm_target *ti = tio->ti; 10086cbce280SMike Snitzer dm_endio_fn endio = ti->type->end_io; 10096cbce280SMike Snitzer struct dm_io *io = tio->io; 10106cbce280SMike Snitzer struct mapped_device *md = io->md; 10111da177e4SLinus Torvalds 10129c37de29SMike Snitzer if (unlikely(error == BLK_STS_TARGET)) { 1013bcb44433SMike Snitzer if (bio_op(bio) == REQ_OP_DISCARD && 101470200574SChristoph Hellwig !bdev_max_discard_sectors(bio->bi_bdev)) 1015bcb44433SMike Snitzer disable_discard(md); 1016bcb44433SMike Snitzer else if (bio_op(bio) == REQ_OP_WRITE_ZEROES && 1017309dca30SChristoph Hellwig !q->limits.max_write_zeroes_sectors) 1018ac62d620SChristoph Hellwig disable_write_zeroes(md); 1019ac62d620SChristoph Hellwig } 10207eee4ae2SMike Snitzer 1021442761fdSMike Snitzer if (static_branch_unlikely(&zoned_enabled) && 1022442761fdSMike Snitzer unlikely(blk_queue_is_zoned(q))) 1023bb37d772SDamien Le Moal dm_zone_endio(io, bio); 1024415c79e1SJohannes Thumshirn 10251be56909SChristoph Hellwig if (endio) { 10266cbce280SMike Snitzer int r = endio(ti, bio, &error); 10271be56909SChristoph Hellwig switch (r) { 10281be56909SChristoph Hellwig case DM_ENDIO_REQUEUE: 1029442761fdSMike Snitzer if (static_branch_unlikely(&zoned_enabled)) { 1030bf14e2b2SDamien Le Moal /* 1031bf14e2b2SDamien Le Moal * Requeuing writes to a sequential zone of a zoned 1032bf14e2b2SDamien Le Moal * target will break the sequential write pattern: 1033bf14e2b2SDamien Le Moal * fail such IO. 1034bf14e2b2SDamien Le Moal */ 1035bf14e2b2SDamien Le Moal if (WARN_ON_ONCE(dm_is_zone_write(md, bio))) 1036bf14e2b2SDamien Le Moal error = BLK_STS_IOERR; 1037bf14e2b2SDamien Le Moal else 10384e4cbee9SChristoph Hellwig error = BLK_STS_DM_REQUEUE; 1039442761fdSMike Snitzer } else 1040442761fdSMike Snitzer error = BLK_STS_DM_REQUEUE; 1041df561f66SGustavo A. R. Silva fallthrough; 10421be56909SChristoph Hellwig case DM_ENDIO_DONE: 10431be56909SChristoph Hellwig break; 10441be56909SChristoph Hellwig case DM_ENDIO_INCOMPLETE: 10451be56909SChristoph Hellwig /* The target will handle the io */ 10461be56909SChristoph Hellwig return; 10471be56909SChristoph Hellwig default: 10481be56909SChristoph Hellwig DMWARN("unimplemented target endio return value: %d", r); 10491be56909SChristoph Hellwig BUG(); 10501be56909SChristoph Hellwig } 10511be56909SChristoph Hellwig } 10521be56909SChristoph Hellwig 1053442761fdSMike Snitzer if (static_branch_unlikely(&swap_bios_enabled) && 1054442761fdSMike Snitzer unlikely(swap_bios_limit(ti, bio))) 1055a666e5c0SMikulas Patocka up(&md->swap_bios_semaphore); 1056a666e5c0SMikulas Patocka 10571d1068ceSChristoph Hellwig free_tio(bio); 1058e2118b3cSDamien Le Moal dm_io_dec_pending(io, error); 10591da177e4SLinus Torvalds } 10601da177e4SLinus Torvalds 106178d8e58aSMike Snitzer /* 106256a67df7SMike Snitzer * Return maximum size of I/O possible at the supplied sector up to the current 106356a67df7SMike Snitzer * target boundary. 106456a67df7SMike Snitzer */ 10653720281dSMike Snitzer static inline sector_t max_io_len_target_boundary(struct dm_target *ti, 10663720281dSMike Snitzer sector_t target_offset) 10671da177e4SLinus Torvalds { 106856a67df7SMike Snitzer return ti->len - target_offset; 106956a67df7SMike Snitzer } 107056a67df7SMike Snitzer 10713720281dSMike Snitzer static sector_t max_io_len(struct dm_target *ti, sector_t sector) 107256a67df7SMike Snitzer { 10733720281dSMike Snitzer sector_t target_offset = dm_target_offset(ti, sector); 10743720281dSMike Snitzer sector_t len = max_io_len_target_boundary(ti, target_offset); 10755091cdecSMike Snitzer sector_t max_len; 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds /* 10783ee16db3SMike Snitzer * Does the target need to split IO even further? 10793ee16db3SMike Snitzer * - varied (per target) IO splitting is a tenet of DM; this 10803ee16db3SMike Snitzer * explains why stacked chunk_sectors based splitting via 10813ee16db3SMike Snitzer * blk_max_size_offset() isn't possible here. So pass in 10823ee16db3SMike Snitzer * ti->max_io_len to override stacked chunk_sectors. 10831da177e4SLinus Torvalds */ 10843ee16db3SMike Snitzer if (ti->max_io_len) { 108533bd6f06SMike Snitzer max_len = blk_max_size_offset(ti->table->md->queue, 10863ee16db3SMike Snitzer target_offset, ti->max_io_len); 1087542f9038SMike Snitzer if (len > max_len) 1088542f9038SMike Snitzer len = max_len; 10893ee16db3SMike Snitzer } 10901da177e4SLinus Torvalds 10911da177e4SLinus Torvalds return len; 10921da177e4SLinus Torvalds } 10931da177e4SLinus Torvalds 1094542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len) 1095542f9038SMike Snitzer { 1096542f9038SMike Snitzer if (len > UINT_MAX) { 1097542f9038SMike Snitzer DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)", 1098542f9038SMike Snitzer (unsigned long long)len, UINT_MAX); 1099542f9038SMike Snitzer ti->error = "Maximum size of target IO is too large"; 1100542f9038SMike Snitzer return -EINVAL; 1101542f9038SMike Snitzer } 1102542f9038SMike Snitzer 110375ae1936SMikulas Patocka ti->max_io_len = (uint32_t) len; 1104542f9038SMike Snitzer 1105542f9038SMike Snitzer return 0; 1106542f9038SMike Snitzer } 1107542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len); 1108542f9038SMike Snitzer 1109f26c5719SDan Williams static struct dm_target *dm_dax_get_live_target(struct mapped_device *md, 1110f26c5719SDan Williams sector_t sector, int *srcu_idx) 11113d97c829SMike Snitzer __acquires(md->io_barrier) 1112545ed20eSToshi Kani { 1113545ed20eSToshi Kani struct dm_table *map; 1114545ed20eSToshi Kani struct dm_target *ti; 1115545ed20eSToshi Kani 1116f26c5719SDan Williams map = dm_get_live_table(md, srcu_idx); 1117545ed20eSToshi Kani if (!map) 1118f26c5719SDan Williams return NULL; 1119545ed20eSToshi Kani 1120545ed20eSToshi Kani ti = dm_table_find_target(map, sector); 1121123d87d5SMikulas Patocka if (!ti) 1122f26c5719SDan Williams return NULL; 1123f26c5719SDan Williams 1124f26c5719SDan Williams return ti; 1125f26c5719SDan Williams } 1126f26c5719SDan Williams 1127f26c5719SDan Williams static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, 1128f26c5719SDan Williams long nr_pages, void **kaddr, pfn_t *pfn) 1129f26c5719SDan Williams { 1130f26c5719SDan Williams struct mapped_device *md = dax_get_private(dax_dev); 1131f26c5719SDan Williams sector_t sector = pgoff * PAGE_SECTORS; 1132f26c5719SDan Williams struct dm_target *ti; 1133f26c5719SDan Williams long len, ret = -EIO; 1134f26c5719SDan Williams int srcu_idx; 1135f26c5719SDan Williams 1136f26c5719SDan Williams ti = dm_dax_get_live_target(md, sector, &srcu_idx); 1137f26c5719SDan Williams 1138f26c5719SDan Williams if (!ti) 1139545ed20eSToshi Kani goto out; 1140f26c5719SDan Williams if (!ti->type->direct_access) 1141f26c5719SDan Williams goto out; 11423720281dSMike Snitzer len = max_io_len(ti, sector) / PAGE_SECTORS; 1143f26c5719SDan Williams if (len < 1) 1144f26c5719SDan Williams goto out; 1145f26c5719SDan Williams nr_pages = min(len, nr_pages); 1146817bf402SDan Williams ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn); 1147817bf402SDan Williams 1148545ed20eSToshi Kani out: 1149545ed20eSToshi Kani dm_put_live_table(md, srcu_idx); 1150f26c5719SDan Williams 1151f26c5719SDan Williams return ret; 1152545ed20eSToshi Kani } 1153545ed20eSToshi Kani 1154cdf6cdcdSVivek Goyal static int dm_dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff, 1155cdf6cdcdSVivek Goyal size_t nr_pages) 1156cdf6cdcdSVivek Goyal { 1157cdf6cdcdSVivek Goyal struct mapped_device *md = dax_get_private(dax_dev); 1158cdf6cdcdSVivek Goyal sector_t sector = pgoff * PAGE_SECTORS; 1159cdf6cdcdSVivek Goyal struct dm_target *ti; 1160cdf6cdcdSVivek Goyal int ret = -EIO; 1161cdf6cdcdSVivek Goyal int srcu_idx; 1162cdf6cdcdSVivek Goyal 1163cdf6cdcdSVivek Goyal ti = dm_dax_get_live_target(md, sector, &srcu_idx); 1164cdf6cdcdSVivek Goyal 1165cdf6cdcdSVivek Goyal if (!ti) 1166cdf6cdcdSVivek Goyal goto out; 1167cdf6cdcdSVivek Goyal if (WARN_ON(!ti->type->dax_zero_page_range)) { 1168cdf6cdcdSVivek Goyal /* 1169cdf6cdcdSVivek Goyal * ->zero_page_range() is mandatory dax operation. If we are 1170cdf6cdcdSVivek Goyal * here, something is wrong. 1171cdf6cdcdSVivek Goyal */ 1172cdf6cdcdSVivek Goyal goto out; 1173cdf6cdcdSVivek Goyal } 1174cdf6cdcdSVivek Goyal ret = ti->type->dax_zero_page_range(ti, pgoff, nr_pages); 1175cdf6cdcdSVivek Goyal out: 1176cdf6cdcdSVivek Goyal dm_put_live_table(md, srcu_idx); 1177cdf6cdcdSVivek Goyal 1178cdf6cdcdSVivek Goyal return ret; 1179cdf6cdcdSVivek Goyal } 1180cdf6cdcdSVivek Goyal 11811dd40c3eSMikulas Patocka /* 11821dd40c3eSMikulas Patocka * A target may call dm_accept_partial_bio only from the map routine. It is 11836842d264SDamien Le Moal * allowed for all bio types except REQ_PREFLUSH, REQ_OP_ZONE_* zone management 1184e6fc9f62SMike Snitzer * operations, REQ_OP_ZONE_APPEND (zone append writes) and any bio serviced by 1185e6fc9f62SMike Snitzer * __send_duplicate_bios(). 11861dd40c3eSMikulas Patocka * 11871dd40c3eSMikulas Patocka * dm_accept_partial_bio informs the dm that the target only wants to process 11881dd40c3eSMikulas Patocka * additional n_sectors sectors of the bio and the rest of the data should be 11891dd40c3eSMikulas Patocka * sent in a next bio. 11901dd40c3eSMikulas Patocka * 11911dd40c3eSMikulas Patocka * A diagram that explains the arithmetics: 11921dd40c3eSMikulas Patocka * +--------------------+---------------+-------+ 11931dd40c3eSMikulas Patocka * | 1 | 2 | 3 | 11941dd40c3eSMikulas Patocka * +--------------------+---------------+-------+ 11951dd40c3eSMikulas Patocka * 11961dd40c3eSMikulas Patocka * <-------------- *tio->len_ptr ---------------> 1197*bdb34759SMike Snitzer * <----- bio_sectors -----> 11981dd40c3eSMikulas Patocka * <-- n_sectors --> 11991dd40c3eSMikulas Patocka * 12001dd40c3eSMikulas Patocka * Region 1 was already iterated over with bio_advance or similar function. 12011dd40c3eSMikulas Patocka * (it may be empty if the target doesn't use bio_advance) 12021dd40c3eSMikulas Patocka * Region 2 is the remaining bio size that the target wants to process. 12031dd40c3eSMikulas Patocka * (it may be empty if region 1 is non-empty, although there is no reason 12041dd40c3eSMikulas Patocka * to make it empty) 12051dd40c3eSMikulas Patocka * The target requires that region 3 is to be sent in the next bio. 12061dd40c3eSMikulas Patocka * 12071dd40c3eSMikulas Patocka * If the target wants to receive multiple copies of the bio (via num_*bios, etc), 12081dd40c3eSMikulas Patocka * the partially processed part (the sum of regions 1+2) must be the same for all 12091dd40c3eSMikulas Patocka * copies of the bio. 12101dd40c3eSMikulas Patocka */ 12111dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors) 12121dd40c3eSMikulas Patocka { 12136c23f0bdSChristoph Hellwig struct dm_target_io *tio = clone_to_tio(bio); 1214*bdb34759SMike Snitzer unsigned bio_sectors = bio_sectors(bio); 12156842d264SDamien Le Moal 1216655f3aadSMike Snitzer BUG_ON(dm_tio_flagged(tio, DM_TIO_IS_DUPLICATE_BIO)); 12176842d264SDamien Le Moal BUG_ON(op_is_zone_mgmt(bio_op(bio))); 12186842d264SDamien Le Moal BUG_ON(bio_op(bio) == REQ_OP_ZONE_APPEND); 1219*bdb34759SMike Snitzer BUG_ON(bio_sectors > *tio->len_ptr); 1220*bdb34759SMike Snitzer BUG_ON(n_sectors > bio_sectors); 12216842d264SDamien Le Moal 1222*bdb34759SMike Snitzer *tio->len_ptr -= bio_sectors - n_sectors; 12231dd40c3eSMikulas Patocka bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT; 12241dd40c3eSMikulas Patocka } 12251dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio); 12261dd40c3eSMikulas Patocka 12270fbb4d93SMike Snitzer static inline void __dm_submit_bio_remap(struct bio *clone, 12280fbb4d93SMike Snitzer dev_t dev, sector_t old_sector) 12290fbb4d93SMike Snitzer { 12300fbb4d93SMike Snitzer trace_block_bio_remap(clone, dev, old_sector); 12310fbb4d93SMike Snitzer submit_bio_noacct(clone); 12320fbb4d93SMike Snitzer } 12330fbb4d93SMike Snitzer 12340fbb4d93SMike Snitzer /* 12350fbb4d93SMike Snitzer * @clone: clone bio that DM core passed to target's .map function 12360fbb4d93SMike Snitzer * @tgt_clone: clone of @clone bio that target needs submitted 12370fbb4d93SMike Snitzer * 12380fbb4d93SMike Snitzer * Targets should use this interface to submit bios they take 12390fbb4d93SMike Snitzer * ownership of when returning DM_MAPIO_SUBMITTED. 12400fbb4d93SMike Snitzer * 12410fbb4d93SMike Snitzer * Target should also enable ti->accounts_remapped_io 12420fbb4d93SMike Snitzer */ 1243b7f8dff0SMike Snitzer void dm_submit_bio_remap(struct bio *clone, struct bio *tgt_clone) 12440fbb4d93SMike Snitzer { 12450fbb4d93SMike Snitzer struct dm_target_io *tio = clone_to_tio(clone); 12460fbb4d93SMike Snitzer struct dm_io *io = tio->io; 12470fbb4d93SMike Snitzer 12480a8e9599SMike Snitzer WARN_ON_ONCE(!tio->ti->accounts_remapped_io); 12490a8e9599SMike Snitzer 12500fbb4d93SMike Snitzer /* establish bio that will get submitted */ 12510fbb4d93SMike Snitzer if (!tgt_clone) 12520fbb4d93SMike Snitzer tgt_clone = clone; 12530fbb4d93SMike Snitzer 12540fbb4d93SMike Snitzer /* 12550fbb4d93SMike Snitzer * Account io->origin_bio to DM dev on behalf of target 12560fbb4d93SMike Snitzer * that took ownership of IO with DM_MAPIO_SUBMITTED. 12570fbb4d93SMike Snitzer */ 1258b7f8dff0SMike Snitzer if (io->map_task == current) { 12590fbb4d93SMike Snitzer /* Still in target's map function */ 126082f6cdccSMike Snitzer dm_io_set_flag(io, DM_IO_START_ACCT); 12610fbb4d93SMike Snitzer } else { 12620fbb4d93SMike Snitzer /* 12630fbb4d93SMike Snitzer * Called by another thread, managed by DM target, 12640fbb4d93SMike Snitzer * wait for dm_split_and_process_bio() to store 12650fbb4d93SMike Snitzer * io->orig_bio 12660fbb4d93SMike Snitzer */ 12670fbb4d93SMike Snitzer while (unlikely(!smp_load_acquire(&io->orig_bio))) 12680fbb4d93SMike Snitzer msleep(1); 12690fbb4d93SMike Snitzer dm_start_io_acct(io, clone); 12700fbb4d93SMike Snitzer } 12710fbb4d93SMike Snitzer 12720fbb4d93SMike Snitzer __dm_submit_bio_remap(tgt_clone, disk_devt(io->md->disk), 12730fbb4d93SMike Snitzer tio->old_sector); 12740fbb4d93SMike Snitzer } 12750fbb4d93SMike Snitzer EXPORT_SYMBOL_GPL(dm_submit_bio_remap); 12760fbb4d93SMike Snitzer 1277a666e5c0SMikulas Patocka static noinline void __set_swap_bios_limit(struct mapped_device *md, int latch) 1278a666e5c0SMikulas Patocka { 1279a666e5c0SMikulas Patocka mutex_lock(&md->swap_bios_lock); 1280a666e5c0SMikulas Patocka while (latch < md->swap_bios) { 1281a666e5c0SMikulas Patocka cond_resched(); 1282a666e5c0SMikulas Patocka down(&md->swap_bios_semaphore); 1283a666e5c0SMikulas Patocka md->swap_bios--; 1284a666e5c0SMikulas Patocka } 1285a666e5c0SMikulas Patocka while (latch > md->swap_bios) { 1286a666e5c0SMikulas Patocka cond_resched(); 1287a666e5c0SMikulas Patocka up(&md->swap_bios_semaphore); 1288a666e5c0SMikulas Patocka md->swap_bios++; 1289a666e5c0SMikulas Patocka } 1290a666e5c0SMikulas Patocka mutex_unlock(&md->swap_bios_lock); 1291a666e5c0SMikulas Patocka } 1292a666e5c0SMikulas Patocka 12931561b396SChristoph Hellwig static void __map_bio(struct bio *clone) 12941da177e4SLinus Torvalds { 12951561b396SChristoph Hellwig struct dm_target_io *tio = clone_to_tio(clone); 1296bd2a49b8SAlasdair G Kergon struct dm_target *ti = tio->ti; 12976cbce280SMike Snitzer struct dm_io *io = tio->io; 12986cbce280SMike Snitzer struct mapped_device *md = io->md; 12996cbce280SMike Snitzer int r; 13001da177e4SLinus Torvalds 13011da177e4SLinus Torvalds clone->bi_end_io = clone_endio; 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds /* 13040fbb4d93SMike Snitzer * Map the clone. 13051da177e4SLinus Torvalds */ 1306e2118b3cSDamien Le Moal dm_io_inc_pending(io); 1307743598f0SMike Snitzer tio->old_sector = clone->bi_iter.bi_sector; 1308d67a5f4bSMikulas Patocka 1309442761fdSMike Snitzer if (static_branch_unlikely(&swap_bios_enabled) && 1310442761fdSMike Snitzer unlikely(swap_bios_limit(ti, clone))) { 1311a666e5c0SMikulas Patocka int latch = get_swap_bios(); 1312a666e5c0SMikulas Patocka if (unlikely(latch != md->swap_bios)) 1313a666e5c0SMikulas Patocka __set_swap_bios_limit(md, latch); 1314a666e5c0SMikulas Patocka down(&md->swap_bios_semaphore); 1315a666e5c0SMikulas Patocka } 1316a666e5c0SMikulas Patocka 1317442761fdSMike Snitzer if (static_branch_unlikely(&zoned_enabled)) { 1318bb37d772SDamien Le Moal /* 1319442761fdSMike Snitzer * Check if the IO needs a special mapping due to zone append 1320442761fdSMike Snitzer * emulation on zoned target. In this case, dm_zone_map_bio() 1321442761fdSMike Snitzer * calls the target map operation. 1322bb37d772SDamien Le Moal */ 13236cbce280SMike Snitzer if (unlikely(dm_emulate_zone_append(md))) 1324bb37d772SDamien Le Moal r = dm_zone_map_bio(tio); 1325bb37d772SDamien Le Moal else 13267de3ee57SMikulas Patocka r = ti->type->map(ti, clone); 1327442761fdSMike Snitzer } else 1328442761fdSMike Snitzer r = ti->type->map(ti, clone); 1329bb37d772SDamien Le Moal 1330846785e6SChristoph Hellwig switch (r) { 1331846785e6SChristoph Hellwig case DM_MAPIO_SUBMITTED: 13320fbb4d93SMike Snitzer /* target has assumed ownership of this io */ 13330fbb4d93SMike Snitzer if (!ti->accounts_remapped_io) 133482f6cdccSMike Snitzer dm_io_set_flag(io, DM_IO_START_ACCT); 1335846785e6SChristoph Hellwig break; 1336846785e6SChristoph Hellwig case DM_MAPIO_REMAPPED: 13370fbb4d93SMike Snitzer /* 13380fbb4d93SMike Snitzer * the bio has been remapped so dispatch it, but defer 13390fbb4d93SMike Snitzer * dm_start_io_acct() until after possible bio_split(). 13400fbb4d93SMike Snitzer */ 13416cbce280SMike Snitzer __dm_submit_bio_remap(clone, disk_devt(md->disk), 1342743598f0SMike Snitzer tio->old_sector); 134382f6cdccSMike Snitzer dm_io_set_flag(io, DM_IO_START_ACCT); 1344846785e6SChristoph Hellwig break; 1345846785e6SChristoph Hellwig case DM_MAPIO_KILL: 1346846785e6SChristoph Hellwig case DM_MAPIO_REQUEUE: 1347442761fdSMike Snitzer if (static_branch_unlikely(&swap_bios_enabled) && 1348442761fdSMike Snitzer unlikely(swap_bios_limit(ti, clone))) 13496cbce280SMike Snitzer up(&md->swap_bios_semaphore); 13501d1068ceSChristoph Hellwig free_tio(clone); 135190a2326eSMike Snitzer if (r == DM_MAPIO_KILL) 135290a2326eSMike Snitzer dm_io_dec_pending(io, BLK_STS_IOERR); 135390a2326eSMike Snitzer else 1354e2118b3cSDamien Le Moal dm_io_dec_pending(io, BLK_STS_DM_REQUEUE); 1355846785e6SChristoph Hellwig break; 1356846785e6SChristoph Hellwig default: 135745cbcd79SKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 135845cbcd79SKiyoshi Ueda BUG(); 13591da177e4SLinus Torvalds } 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds 1362318716ddSMike Snitzer static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci, 13637dd06a25SMike Snitzer struct dm_target *ti, unsigned num_bios) 1364f9ab94ceSMikulas Patocka { 13651d1068ceSChristoph Hellwig struct bio *bio; 1366318716ddSMike Snitzer int try; 1367dba14160SMikulas Patocka 1368318716ddSMike Snitzer for (try = 0; try < 2; try++) { 1369318716ddSMike Snitzer int bio_nr; 1370318716ddSMike Snitzer 1371318716ddSMike Snitzer if (try) 1372bc02cdbeSMike Snitzer mutex_lock(&ci->io->md->table_devices_lock); 1373318716ddSMike Snitzer for (bio_nr = 0; bio_nr < num_bios; bio_nr++) { 13747dd06a25SMike Snitzer bio = alloc_tio(ci, ti, bio_nr, NULL, 1375dc8e2021SChristoph Hellwig try ? GFP_NOIO : GFP_NOWAIT); 13761d1068ceSChristoph Hellwig if (!bio) 1377318716ddSMike Snitzer break; 1378318716ddSMike Snitzer 13791d1068ceSChristoph Hellwig bio_list_add(blist, bio); 1380318716ddSMike Snitzer } 1381318716ddSMike Snitzer if (try) 1382bc02cdbeSMike Snitzer mutex_unlock(&ci->io->md->table_devices_lock); 1383318716ddSMike Snitzer if (bio_nr == num_bios) 1384318716ddSMike Snitzer return; 1385318716ddSMike Snitzer 13866c23f0bdSChristoph Hellwig while ((bio = bio_list_pop(blist))) 13871d1068ceSChristoph Hellwig free_tio(bio); 1388318716ddSMike Snitzer } 1389318716ddSMike Snitzer } 1390f9ab94ceSMikulas Patocka 139114fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti, 13921dd40c3eSMikulas Patocka unsigned num_bios, unsigned *len) 139306a426ceSMike Snitzer { 1394318716ddSMike Snitzer struct bio_list blist = BIO_EMPTY_LIST; 13958eabf5d0SChristoph Hellwig struct bio *clone; 139606a426ceSMike Snitzer 1397891fced6SChristoph Hellwig switch (num_bios) { 1398891fced6SChristoph Hellwig case 0: 1399891fced6SChristoph Hellwig break; 1400891fced6SChristoph Hellwig case 1: 1401891fced6SChristoph Hellwig clone = alloc_tio(ci, ti, 0, len, GFP_NOIO); 1402891fced6SChristoph Hellwig __map_bio(clone); 1403891fced6SChristoph Hellwig break; 1404891fced6SChristoph Hellwig default: 14057dd06a25SMike Snitzer /* dm_accept_partial_bio() is not supported with shared tio->len_ptr */ 14067dd06a25SMike Snitzer alloc_multiple_bios(&blist, ci, ti, num_bios); 14078eabf5d0SChristoph Hellwig while ((clone = bio_list_pop(&blist))) { 1408655f3aadSMike Snitzer dm_tio_set_flag(clone_to_tio(clone), DM_TIO_IS_DUPLICATE_BIO); 14091561b396SChristoph Hellwig __map_bio(clone); 1410f9ab94ceSMikulas Patocka } 1411891fced6SChristoph Hellwig break; 1412318716ddSMike Snitzer } 141306a426ceSMike Snitzer } 141406a426ceSMike Snitzer 1415332f2b1eSMike Snitzer static void __send_empty_flush(struct clone_info *ci) 1416f9ab94ceSMikulas Patocka { 141706a426ceSMike Snitzer unsigned target_nr = 0; 1418f9ab94ceSMikulas Patocka struct dm_target *ti; 1419828678b8SMike Snitzer struct bio flush_bio; 1420828678b8SMike Snitzer 1421828678b8SMike Snitzer /* 1422828678b8SMike Snitzer * Use an on-stack bio for this, it's safe since we don't 1423828678b8SMike Snitzer * need to reference it after submit. It's just used as 1424828678b8SMike Snitzer * the basis for the clone(s). 1425828678b8SMike Snitzer */ 142649add496SChristoph Hellwig bio_init(&flush_bio, ci->io->md->disk->part0, NULL, 0, 142749add496SChristoph Hellwig REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC); 142847d95102SChristoph Hellwig 1429828678b8SMike Snitzer ci->bio = &flush_bio; 1430828678b8SMike Snitzer ci->sector_count = 0; 143192b914e2SShin'ichiro Kawasaki ci->io->tio.clone.bi_iter.bi_size = 0; 1432f9ab94ceSMikulas Patocka 1433f9ab94ceSMikulas Patocka while ((ti = dm_table_get_target(ci->map, target_nr++))) 14341dd40c3eSMikulas Patocka __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL); 1435828678b8SMike Snitzer 1436828678b8SMike Snitzer bio_uninit(ci->bio); 1437f9ab94ceSMikulas Patocka } 1438f9ab94ceSMikulas Patocka 1439e6fc9f62SMike Snitzer static void __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti, 144061697a6aSMike Snitzer unsigned num_bios) 14415ae89a87SMike Snitzer { 144251b86f9aSMichael Lass unsigned len; 14435ae89a87SMike Snitzer 14443720281dSMike Snitzer len = min_t(sector_t, ci->sector_count, 14453720281dSMike Snitzer max_io_len_target_boundary(ti, dm_target_offset(ti, ci->sector))); 144651b86f9aSMichael Lass 14477dd06a25SMike Snitzer __send_duplicate_bios(ci, ti, num_bios, &len); 14487dd06a25SMike Snitzer 1449a79245b3SMike Snitzer ci->sector += len; 14503d7f4562SMike Snitzer ci->sector_count -= len; 14515ae89a87SMike Snitzer } 14525ae89a87SMike Snitzer 1453568c73a3SMike Snitzer static bool is_abnormal_io(struct bio *bio) 1454568c73a3SMike Snitzer { 1455568c73a3SMike Snitzer bool r = false; 1456568c73a3SMike Snitzer 1457568c73a3SMike Snitzer switch (bio_op(bio)) { 1458568c73a3SMike Snitzer case REQ_OP_DISCARD: 1459568c73a3SMike Snitzer case REQ_OP_SECURE_ERASE: 1460568c73a3SMike Snitzer case REQ_OP_WRITE_ZEROES: 1461568c73a3SMike Snitzer r = true; 1462568c73a3SMike Snitzer break; 1463568c73a3SMike Snitzer } 1464568c73a3SMike Snitzer 1465568c73a3SMike Snitzer return r; 1466568c73a3SMike Snitzer } 1467568c73a3SMike Snitzer 14680519c71eSMike Snitzer static bool __process_abnormal_io(struct clone_info *ci, struct dm_target *ti, 146984b98f4cSMike Snitzer blk_status_t *status) 14700519c71eSMike Snitzer { 14719679b5a7SMike Snitzer unsigned num_bios = 0; 14720519c71eSMike Snitzer 1473e6fc9f62SMike Snitzer switch (bio_op(ci->bio)) { 14749679b5a7SMike Snitzer case REQ_OP_DISCARD: 14759679b5a7SMike Snitzer num_bios = ti->num_discard_bios; 14769679b5a7SMike Snitzer break; 14779679b5a7SMike Snitzer case REQ_OP_SECURE_ERASE: 14789679b5a7SMike Snitzer num_bios = ti->num_secure_erase_bios; 14799679b5a7SMike Snitzer break; 14809679b5a7SMike Snitzer case REQ_OP_WRITE_ZEROES: 14819679b5a7SMike Snitzer num_bios = ti->num_write_zeroes_bios; 14829679b5a7SMike Snitzer break; 14839679b5a7SMike Snitzer default: 14840519c71eSMike Snitzer return false; 14859679b5a7SMike Snitzer } 14860519c71eSMike Snitzer 1487e6fc9f62SMike Snitzer /* 1488e6fc9f62SMike Snitzer * Even though the device advertised support for this type of 1489e6fc9f62SMike Snitzer * request, that does not mean every target supports it, and 1490e6fc9f62SMike Snitzer * reconfiguration might also have changed that since the 1491e6fc9f62SMike Snitzer * check was performed. 1492e6fc9f62SMike Snitzer */ 149384b98f4cSMike Snitzer if (unlikely(!num_bios)) 149484b98f4cSMike Snitzer *status = BLK_STS_NOTSUPP; 1495e6fc9f62SMike Snitzer else { 1496e6fc9f62SMike Snitzer __send_changing_extent_only(ci, ti, num_bios); 149784b98f4cSMike Snitzer *status = BLK_STS_OK; 1498e6fc9f62SMike Snitzer } 14990519c71eSMike Snitzer return true; 15000519c71eSMike Snitzer } 15010519c71eSMike Snitzer 1502e4c93811SAlasdair G Kergon /* 1503b99fdcdcSMing Lei * Reuse ->bi_private as hlist head for storing all dm_io instances 1504b99fdcdcSMing Lei * associated with this bio, and this bio's bi_private needs to be 1505b99fdcdcSMing Lei * stored in dm_io->data before the reuse. 1506b99fdcdcSMing Lei * 1507b99fdcdcSMing Lei * bio->bi_private is owned by fs or upper layer, so block layer won't 1508b99fdcdcSMing Lei * touch it after splitting. Meantime it won't be changed by anyone after 1509b99fdcdcSMing Lei * bio is submitted. So this reuse is safe. 1510b99fdcdcSMing Lei */ 1511b99fdcdcSMing Lei static inline struct hlist_head *dm_get_bio_hlist_head(struct bio *bio) 1512b99fdcdcSMing Lei { 1513b99fdcdcSMing Lei return (struct hlist_head *)&bio->bi_private; 1514b99fdcdcSMing Lei } 1515b99fdcdcSMing Lei 1516b99fdcdcSMing Lei static void dm_queue_poll_io(struct bio *bio, struct dm_io *io) 1517b99fdcdcSMing Lei { 1518b99fdcdcSMing Lei struct hlist_head *head = dm_get_bio_hlist_head(bio); 1519b99fdcdcSMing Lei 1520b99fdcdcSMing Lei if (!(bio->bi_opf & REQ_DM_POLL_LIST)) { 1521b99fdcdcSMing Lei bio->bi_opf |= REQ_DM_POLL_LIST; 1522b99fdcdcSMing Lei /* 1523b99fdcdcSMing Lei * Save .bi_private into dm_io, so that we can reuse 1524b99fdcdcSMing Lei * .bi_private as hlist head for storing dm_io list 1525b99fdcdcSMing Lei */ 1526b99fdcdcSMing Lei io->data = bio->bi_private; 1527b99fdcdcSMing Lei 1528b99fdcdcSMing Lei INIT_HLIST_HEAD(head); 1529b99fdcdcSMing Lei 1530b99fdcdcSMing Lei /* tell block layer to poll for completion */ 1531b99fdcdcSMing Lei bio->bi_cookie = ~BLK_QC_T_NONE; 1532b99fdcdcSMing Lei } else { 1533b99fdcdcSMing Lei /* 1534b99fdcdcSMing Lei * bio recursed due to split, reuse original poll list, 1535b99fdcdcSMing Lei * and save bio->bi_private too. 1536b99fdcdcSMing Lei */ 1537b99fdcdcSMing Lei io->data = hlist_entry(head->first, struct dm_io, node)->data; 1538b99fdcdcSMing Lei } 1539b99fdcdcSMing Lei 1540b99fdcdcSMing Lei hlist_add_head(&io->node, head); 1541b99fdcdcSMing Lei } 1542b99fdcdcSMing Lei 1543b99fdcdcSMing Lei /* 1544e4c93811SAlasdair G Kergon * Select the correct strategy for processing a non-flush bio. 1545e4c93811SAlasdair G Kergon */ 154684b98f4cSMike Snitzer static blk_status_t __split_and_process_bio(struct clone_info *ci) 1547e4c93811SAlasdair G Kergon { 154866bdaa43SMike Snitzer struct bio *clone; 1549e4c93811SAlasdair G Kergon struct dm_target *ti; 15501c3b13e6SKent Overstreet unsigned len; 155184b98f4cSMike Snitzer blk_status_t error = BLK_STS_IOERR; 1552e4c93811SAlasdair G Kergon 1553e4c93811SAlasdair G Kergon ti = dm_table_find_target(ci->map, ci->sector); 155484b98f4cSMike Snitzer if (unlikely(!ti || __process_abnormal_io(ci, ti, &error))) 155584b98f4cSMike Snitzer return error; 15563d7f4562SMike Snitzer 1557b99fdcdcSMing Lei /* 1558b99fdcdcSMing Lei * Only support bio polling for normal IO, and the target io is 1559b99fdcdcSMing Lei * exactly inside the dm_io instance (verified in dm_poll_dm_io) 1560b99fdcdcSMing Lei */ 1561b99fdcdcSMing Lei ci->submit_as_polled = ci->bio->bi_opf & REQ_POLLED; 1562e4c93811SAlasdair G Kergon 1563e4c93811SAlasdair G Kergon len = min_t(sector_t, max_io_len(ti, ci->sector), ci->sector_count); 156466bdaa43SMike Snitzer clone = alloc_tio(ci, ti, 0, &len, GFP_NOIO); 156566bdaa43SMike Snitzer __map_bio(clone); 1566e4c93811SAlasdair G Kergon 1567e4c93811SAlasdair G Kergon ci->sector += len; 1568e4c93811SAlasdair G Kergon ci->sector_count -= len; 1569e4c93811SAlasdair G Kergon 157084b98f4cSMike Snitzer return BLK_STS_OK; 1571e4c93811SAlasdair G Kergon } 1572e4c93811SAlasdair G Kergon 1573978e51baSMike Snitzer static void init_clone_info(struct clone_info *ci, struct mapped_device *md, 1574978e51baSMike Snitzer struct dm_table *map, struct bio *bio) 1575978e51baSMike Snitzer { 1576978e51baSMike Snitzer ci->map = map; 1577978e51baSMike Snitzer ci->io = alloc_io(md, bio); 1578d41e077aSMike Snitzer ci->bio = bio; 1579b99fdcdcSMing Lei ci->submit_as_polled = false; 1580978e51baSMike Snitzer ci->sector = bio->bi_iter.bi_sector; 1581d41e077aSMike Snitzer ci->sector_count = bio_sectors(bio); 1582d41e077aSMike Snitzer 1583d41e077aSMike Snitzer /* Shouldn't happen but sector_count was being set to 0 so... */ 1584442761fdSMike Snitzer if (static_branch_unlikely(&zoned_enabled) && 1585442761fdSMike Snitzer WARN_ON_ONCE(op_is_zone_mgmt(bio_op(bio)) && ci->sector_count)) 1586d41e077aSMike Snitzer ci->sector_count = 0; 1587978e51baSMike Snitzer } 1588978e51baSMike Snitzer 1589e4c93811SAlasdair G Kergon /* 159014fe594dSAlasdair G Kergon * Entry point to split a bio into clones and submit them to the targets. 15911da177e4SLinus Torvalds */ 159296c9865cSMike Snitzer static void dm_split_and_process_bio(struct mapped_device *md, 159383d5e5b0SMikulas Patocka struct dm_table *map, struct bio *bio) 15941da177e4SLinus Torvalds { 15951da177e4SLinus Torvalds struct clone_info ci; 15964857abf6SMike Snitzer struct dm_io *io; 15970fbb4d93SMike Snitzer struct bio *orig_bio = NULL; 159884b98f4cSMike Snitzer blk_status_t error = BLK_STS_OK; 15991da177e4SLinus Torvalds 1600978e51baSMike Snitzer init_clone_info(&ci, md, map, bio); 16014857abf6SMike Snitzer io = ci.io; 1602bd2a49b8SAlasdair G Kergon 16031eff9d32SJens Axboe if (bio->bi_opf & REQ_PREFLUSH) { 1604332f2b1eSMike Snitzer __send_empty_flush(&ci); 1605e2736347SMike Snitzer /* dm_io_complete submits any data associated with flush */ 1606d41e077aSMike Snitzer goto out; 1607d41e077aSMike Snitzer } 1608d41e077aSMike Snitzer 160996c9865cSMike Snitzer error = __split_and_process_bio(&ci); 16104857abf6SMike Snitzer io->map_task = NULL; 1611d41e077aSMike Snitzer if (error || !ci.sector_count) 1612d41e077aSMike Snitzer goto out; 1613d41e077aSMike Snitzer 161418a25da8SNeilBrown /* 1615d41e077aSMike Snitzer * Remainder must be passed to submit_bio_noacct() so it gets handled 1616d41e077aSMike Snitzer * *after* bios already submitted have been completely processed. 16174857abf6SMike Snitzer * We take a clone of the original to store in io->orig_bio to be 1618e2736347SMike Snitzer * used by dm_end_io_acct() and for dm_io_complete() to use for 1619d41e077aSMike Snitzer * completion handling. 162018a25da8SNeilBrown */ 16210fbb4d93SMike Snitzer orig_bio = bio_split(bio, bio_sectors(bio) - ci.sector_count, 1622f21c601aSMike Snitzer GFP_NOIO, &md->queue->bio_split); 16230fbb4d93SMike Snitzer bio_chain(orig_bio, bio); 16240fbb4d93SMike Snitzer trace_block_split(orig_bio, bio->bi_iter.bi_sector); 16253e08773cSChristoph Hellwig submit_bio_noacct(bio); 1626d41e077aSMike Snitzer out: 16270fbb4d93SMike Snitzer if (!orig_bio) 16280fbb4d93SMike Snitzer orig_bio = bio; 16294857abf6SMike Snitzer smp_store_release(&io->orig_bio, orig_bio); 16304857abf6SMike Snitzer if (dm_io_flagged(io, DM_IO_START_ACCT)) 16314857abf6SMike Snitzer dm_start_io_acct(io, NULL); 16321da177e4SLinus Torvalds 1633b99fdcdcSMing Lei /* 1634b99fdcdcSMing Lei * Drop the extra reference count for non-POLLED bio, and hold one 1635b99fdcdcSMing Lei * reference for POLLED bio, which will be released in dm_poll_bio 1636b99fdcdcSMing Lei * 1637b99fdcdcSMing Lei * Add every dm_io instance into the hlist_head which is stored in 1638b99fdcdcSMing Lei * bio->bi_private, so that dm_poll_bio can poll them all. 1639b99fdcdcSMing Lei */ 1640b99fdcdcSMing Lei if (error || !ci.submit_as_polled) 164184b98f4cSMike Snitzer dm_io_dec_pending(ci.io, error); 1642b99fdcdcSMing Lei else 16434857abf6SMike Snitzer dm_queue_poll_io(bio, io); 16441da177e4SLinus Torvalds } 16451da177e4SLinus Torvalds 16463e08773cSChristoph Hellwig static void dm_submit_bio(struct bio *bio) 16471da177e4SLinus Torvalds { 1648309dca30SChristoph Hellwig struct mapped_device *md = bio->bi_bdev->bd_disk->private_data; 164983d5e5b0SMikulas Patocka int srcu_idx; 165083d5e5b0SMikulas Patocka struct dm_table *map; 16511da177e4SLinus Torvalds 1652563a225cSMike Snitzer map = dm_get_live_table_bio(md, &srcu_idx, bio); 16538cf7961dSChristoph Hellwig 1654fa247089SMike Snitzer /* If suspended, or map not yet available, queue this IO for later */ 1655fa247089SMike Snitzer if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) || 1656fa247089SMike Snitzer unlikely(!map)) { 16576abc4946SKonstantin Khlebnikov if (bio->bi_opf & REQ_NOWAIT) 16586abc4946SKonstantin Khlebnikov bio_wouldblock_error(bio); 1659b2abdb1bSMike Snitzer else if (bio->bi_opf & REQ_RAHEAD) 16606a8736d1STejun Heo bio_io_error(bio); 1661b2abdb1bSMike Snitzer else 1662b2abdb1bSMike Snitzer queue_io(md, bio); 1663b2abdb1bSMike Snitzer goto out; 16641da177e4SLinus Torvalds } 16651da177e4SLinus Torvalds 1666b2abdb1bSMike Snitzer /* 1667b2abdb1bSMike Snitzer * Use blk_queue_split() for abnormal IO (e.g. discard, writesame, etc) 1668b2abdb1bSMike Snitzer * otherwise associated queue_limits won't be imposed. 1669b2abdb1bSMike Snitzer */ 1670fe221db4SMike Snitzer if (unlikely(is_abnormal_io(bio))) 1671b2abdb1bSMike Snitzer blk_queue_split(&bio); 1672978e51baSMike Snitzer 167396c9865cSMike Snitzer dm_split_and_process_bio(md, map, bio); 1674b2abdb1bSMike Snitzer out: 1675563a225cSMike Snitzer dm_put_live_table_bio(md, srcu_idx, bio); 1676978e51baSMike Snitzer } 1677978e51baSMike Snitzer 1678b99fdcdcSMing Lei static bool dm_poll_dm_io(struct dm_io *io, struct io_comp_batch *iob, 1679b99fdcdcSMing Lei unsigned int flags) 1680b99fdcdcSMing Lei { 1681655f3aadSMike Snitzer WARN_ON_ONCE(!dm_tio_is_normal(&io->tio)); 1682b99fdcdcSMing Lei 1683b99fdcdcSMing Lei /* don't poll if the mapped io is done */ 1684b99fdcdcSMing Lei if (atomic_read(&io->io_count) > 1) 1685b99fdcdcSMing Lei bio_poll(&io->tio.clone, iob, flags); 1686b99fdcdcSMing Lei 1687b99fdcdcSMing Lei /* bio_poll holds the last reference */ 1688b99fdcdcSMing Lei return atomic_read(&io->io_count) == 1; 1689b99fdcdcSMing Lei } 1690b99fdcdcSMing Lei 1691b99fdcdcSMing Lei static int dm_poll_bio(struct bio *bio, struct io_comp_batch *iob, 1692b99fdcdcSMing Lei unsigned int flags) 1693b99fdcdcSMing Lei { 1694b99fdcdcSMing Lei struct hlist_head *head = dm_get_bio_hlist_head(bio); 1695b99fdcdcSMing Lei struct hlist_head tmp = HLIST_HEAD_INIT; 1696b99fdcdcSMing Lei struct hlist_node *next; 1697b99fdcdcSMing Lei struct dm_io *io; 1698b99fdcdcSMing Lei 1699b99fdcdcSMing Lei /* Only poll normal bio which was marked as REQ_DM_POLL_LIST */ 1700b99fdcdcSMing Lei if (!(bio->bi_opf & REQ_DM_POLL_LIST)) 1701b99fdcdcSMing Lei return 0; 1702b99fdcdcSMing Lei 1703b99fdcdcSMing Lei WARN_ON_ONCE(hlist_empty(head)); 1704b99fdcdcSMing Lei 1705b99fdcdcSMing Lei hlist_move_list(head, &tmp); 1706b99fdcdcSMing Lei 1707b99fdcdcSMing Lei /* 1708b99fdcdcSMing Lei * Restore .bi_private before possibly completing dm_io. 1709b99fdcdcSMing Lei * 1710b99fdcdcSMing Lei * bio_poll() is only possible once @bio has been completely 1711b99fdcdcSMing Lei * submitted via submit_bio_noacct()'s depth-first submission. 1712b99fdcdcSMing Lei * So there is no dm_queue_poll_io() race associated with 1713b99fdcdcSMing Lei * clearing REQ_DM_POLL_LIST here. 1714b99fdcdcSMing Lei */ 1715b99fdcdcSMing Lei bio->bi_opf &= ~REQ_DM_POLL_LIST; 1716b99fdcdcSMing Lei bio->bi_private = hlist_entry(tmp.first, struct dm_io, node)->data; 1717b99fdcdcSMing Lei 1718b99fdcdcSMing Lei hlist_for_each_entry_safe(io, next, &tmp, node) { 1719b99fdcdcSMing Lei if (dm_poll_dm_io(io, iob, flags)) { 1720b99fdcdcSMing Lei hlist_del_init(&io->node); 1721b99fdcdcSMing Lei /* 172284b98f4cSMike Snitzer * clone_endio() has already occurred, so no 172384b98f4cSMike Snitzer * error handling is needed here. 1724b99fdcdcSMing Lei */ 172584b98f4cSMike Snitzer __dm_io_dec_pending(io); 1726b99fdcdcSMing Lei } 1727b99fdcdcSMing Lei } 1728b99fdcdcSMing Lei 1729b99fdcdcSMing Lei /* Not done? */ 1730b99fdcdcSMing Lei if (!hlist_empty(&tmp)) { 1731b99fdcdcSMing Lei bio->bi_opf |= REQ_DM_POLL_LIST; 1732b99fdcdcSMing Lei /* Reset bio->bi_private to dm_io list head */ 1733b99fdcdcSMing Lei hlist_move_list(&tmp, head); 1734b99fdcdcSMing Lei return 0; 1735b99fdcdcSMing Lei } 1736b99fdcdcSMing Lei return 1; 1737b99fdcdcSMing Lei } 1738b99fdcdcSMing Lei 17391da177e4SLinus Torvalds /*----------------------------------------------------------------- 17401da177e4SLinus Torvalds * An IDR is used to keep track of allocated minor numbers. 17411da177e4SLinus Torvalds *---------------------------------------------------------------*/ 17422b06cfffSAlasdair G Kergon static void free_minor(int minor) 17431da177e4SLinus Torvalds { 1744f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 17451da177e4SLinus Torvalds idr_remove(&_minor_idr, minor); 1746f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 17471da177e4SLinus Torvalds } 17481da177e4SLinus Torvalds 17491da177e4SLinus Torvalds /* 17501da177e4SLinus Torvalds * See if the device with a specific minor # is free. 17511da177e4SLinus Torvalds */ 1752cf13ab8eSFrederik Deweerdt static int specific_minor(int minor) 17531da177e4SLinus Torvalds { 1754c9d76be6STejun Heo int r; 17551da177e4SLinus Torvalds 17561da177e4SLinus Torvalds if (minor >= (1 << MINORBITS)) 17571da177e4SLinus Torvalds return -EINVAL; 17581da177e4SLinus Torvalds 1759c9d76be6STejun Heo idr_preload(GFP_KERNEL); 1760f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 17611da177e4SLinus Torvalds 1762c9d76be6STejun Heo r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT); 17631da177e4SLinus Torvalds 1764f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1765c9d76be6STejun Heo idr_preload_end(); 1766c9d76be6STejun Heo if (r < 0) 1767c9d76be6STejun Heo return r == -ENOSPC ? -EBUSY : r; 1768c9d76be6STejun Heo return 0; 17691da177e4SLinus Torvalds } 17701da177e4SLinus Torvalds 1771cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor) 17721da177e4SLinus Torvalds { 1773c9d76be6STejun Heo int r; 17741da177e4SLinus Torvalds 1775c9d76be6STejun Heo idr_preload(GFP_KERNEL); 1776f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 17771da177e4SLinus Torvalds 1778c9d76be6STejun Heo r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT); 17791da177e4SLinus Torvalds 1780f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1781c9d76be6STejun Heo idr_preload_end(); 1782c9d76be6STejun Heo if (r < 0) 17831da177e4SLinus Torvalds return r; 1784c9d76be6STejun Heo *minor = r; 1785c9d76be6STejun Heo return 0; 17861da177e4SLinus Torvalds } 17871da177e4SLinus Torvalds 178883d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops; 1789681cc5e8SMike Snitzer static const struct block_device_operations dm_rq_blk_dops; 1790f26c5719SDan Williams static const struct dax_operations dm_dax_ops; 17911da177e4SLinus Torvalds 179253d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work); 179353d5914fSMikulas Patocka 1794aa6ce87aSSatya Tangirala #ifdef CONFIG_BLK_INLINE_ENCRYPTION 1795cb77cb5aSEric Biggers static void dm_queue_destroy_crypto_profile(struct request_queue *q) 1796aa6ce87aSSatya Tangirala { 1797cb77cb5aSEric Biggers dm_destroy_crypto_profile(q->crypto_profile); 1798aa6ce87aSSatya Tangirala } 1799aa6ce87aSSatya Tangirala 1800aa6ce87aSSatya Tangirala #else /* CONFIG_BLK_INLINE_ENCRYPTION */ 1801aa6ce87aSSatya Tangirala 1802cb77cb5aSEric Biggers static inline void dm_queue_destroy_crypto_profile(struct request_queue *q) 1803aa6ce87aSSatya Tangirala { 1804aa6ce87aSSatya Tangirala } 1805aa6ce87aSSatya Tangirala #endif /* !CONFIG_BLK_INLINE_ENCRYPTION */ 1806aa6ce87aSSatya Tangirala 18070f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md) 18080f20972fSMike Snitzer { 18090f20972fSMike Snitzer if (md->wq) 18100f20972fSMike Snitzer destroy_workqueue(md->wq); 18116f1c819cSKent Overstreet bioset_exit(&md->bs); 18126f1c819cSKent Overstreet bioset_exit(&md->io_bs); 18130f20972fSMike Snitzer 1814f26c5719SDan Williams if (md->dax_dev) { 1815fb08a190SChristoph Hellwig dax_remove_host(md->disk); 1816f26c5719SDan Williams kill_dax(md->dax_dev); 1817f26c5719SDan Williams put_dax(md->dax_dev); 1818f26c5719SDan Williams md->dax_dev = NULL; 1819f26c5719SDan Williams } 1820f26c5719SDan Williams 1821588b7f5dSKirill Tkhai dm_cleanup_zoned_dev(md); 18220f20972fSMike Snitzer if (md->disk) { 18230f20972fSMike Snitzer spin_lock(&_minor_lock); 18240f20972fSMike Snitzer md->disk->private_data = NULL; 18250f20972fSMike Snitzer spin_unlock(&_minor_lock); 182689f871afSChristoph Hellwig if (dm_get_md_type(md) != DM_TYPE_NONE) { 182789f871afSChristoph Hellwig dm_sysfs_exit(md); 18280f20972fSMike Snitzer del_gendisk(md->disk); 182989f871afSChristoph Hellwig } 1830cb77cb5aSEric Biggers dm_queue_destroy_crypto_profile(md->queue); 183174fe6ba9SChristoph Hellwig blk_cleanup_disk(md->disk); 183274a2b6ecSChristoph Hellwig } 18330f20972fSMike Snitzer 18349f6dc633SMike Snitzer if (md->pending_io) { 18359f6dc633SMike Snitzer free_percpu(md->pending_io); 18369f6dc633SMike Snitzer md->pending_io = NULL; 18379f6dc633SMike Snitzer } 18389f6dc633SMike Snitzer 1839d09960b0STahsin Erdogan cleanup_srcu_struct(&md->io_barrier); 1840d09960b0STahsin Erdogan 1841d5ffebddSMike Snitzer mutex_destroy(&md->suspend_lock); 1842d5ffebddSMike Snitzer mutex_destroy(&md->type_lock); 1843d5ffebddSMike Snitzer mutex_destroy(&md->table_devices_lock); 1844a666e5c0SMikulas Patocka mutex_destroy(&md->swap_bios_lock); 1845d5ffebddSMike Snitzer 18464cc96131SMike Snitzer dm_mq_cleanup_mapped_device(md); 18470f20972fSMike Snitzer } 18480f20972fSMike Snitzer 18491da177e4SLinus Torvalds /* 18501da177e4SLinus Torvalds * Allocate and initialise a blank device with a given minor. 18511da177e4SLinus Torvalds */ 18522b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor) 18531da177e4SLinus Torvalds { 1854115485e8SMike Snitzer int r, numa_node_id = dm_get_numa_node(); 1855115485e8SMike Snitzer struct mapped_device *md; 1856ba61fdd1SJeff Mahoney void *old_md; 18571da177e4SLinus Torvalds 1858856eb091SMikulas Patocka md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id); 18591da177e4SLinus Torvalds if (!md) { 18601da177e4SLinus Torvalds DMWARN("unable to allocate device, out of memory."); 18611da177e4SLinus Torvalds return NULL; 18621da177e4SLinus Torvalds } 18631da177e4SLinus Torvalds 186410da4f79SJeff Mahoney if (!try_module_get(THIS_MODULE)) 18656ed7ade8SMilan Broz goto bad_module_get; 186610da4f79SJeff Mahoney 18671da177e4SLinus Torvalds /* get a minor number for the dev */ 18682b06cfffSAlasdair G Kergon if (minor == DM_ANY_MINOR) 1869cf13ab8eSFrederik Deweerdt r = next_free_minor(&minor); 18702b06cfffSAlasdair G Kergon else 1871cf13ab8eSFrederik Deweerdt r = specific_minor(minor); 18721da177e4SLinus Torvalds if (r < 0) 18736ed7ade8SMilan Broz goto bad_minor; 18741da177e4SLinus Torvalds 187583d5e5b0SMikulas Patocka r = init_srcu_struct(&md->io_barrier); 187683d5e5b0SMikulas Patocka if (r < 0) 187783d5e5b0SMikulas Patocka goto bad_io_barrier; 187883d5e5b0SMikulas Patocka 1879115485e8SMike Snitzer md->numa_node_id = numa_node_id; 1880591ddcfcSMike Snitzer md->init_tio_pdu = false; 1881a5664dadSMike Snitzer md->type = DM_TYPE_NONE; 1882e61290a4SDaniel Walker mutex_init(&md->suspend_lock); 1883a5664dadSMike Snitzer mutex_init(&md->type_lock); 188486f1152bSBenjamin Marzinski mutex_init(&md->table_devices_lock); 1885022c2611SMikulas Patocka spin_lock_init(&md->deferred_lock); 18861da177e4SLinus Torvalds atomic_set(&md->holders, 1); 18875c6bd75dSAlasdair G Kergon atomic_set(&md->open_count, 0); 18881da177e4SLinus Torvalds atomic_set(&md->event_nr, 0); 18897a8c3d3bSMike Anderson atomic_set(&md->uevent_seq, 0); 18907a8c3d3bSMike Anderson INIT_LIST_HEAD(&md->uevent_list); 189186f1152bSBenjamin Marzinski INIT_LIST_HEAD(&md->table_devices); 18927a8c3d3bSMike Anderson spin_lock_init(&md->uevent_lock); 18931da177e4SLinus Torvalds 189447ace7e0SMike Snitzer /* 1895c62b37d9SChristoph Hellwig * default to bio-based until DM table is loaded and md->type 1896c62b37d9SChristoph Hellwig * established. If request-based table is loaded: blk-mq will 1897c62b37d9SChristoph Hellwig * override accordingly. 189847ace7e0SMike Snitzer */ 189974fe6ba9SChristoph Hellwig md->disk = blk_alloc_disk(md->numa_node_id); 19001da177e4SLinus Torvalds if (!md->disk) 19010f20972fSMike Snitzer goto bad; 190274fe6ba9SChristoph Hellwig md->queue = md->disk->queue; 19031da177e4SLinus Torvalds 1904f0b04115SJeff Mahoney init_waitqueue_head(&md->wait); 190553d5914fSMikulas Patocka INIT_WORK(&md->work, dm_wq_work); 1906f0b04115SJeff Mahoney init_waitqueue_head(&md->eventq); 19072995fa78SMikulas Patocka init_completion(&md->kobj_holder.completion); 1908f0b04115SJeff Mahoney 1909a666e5c0SMikulas Patocka md->swap_bios = get_swap_bios(); 1910a666e5c0SMikulas Patocka sema_init(&md->swap_bios_semaphore, md->swap_bios); 1911a666e5c0SMikulas Patocka mutex_init(&md->swap_bios_lock); 1912a666e5c0SMikulas Patocka 19131da177e4SLinus Torvalds md->disk->major = _major; 19141da177e4SLinus Torvalds md->disk->first_minor = minor; 191574fe6ba9SChristoph Hellwig md->disk->minors = 1; 19161ebe2e5fSChristoph Hellwig md->disk->flags |= GENHD_FL_NO_PART; 19171da177e4SLinus Torvalds md->disk->fops = &dm_blk_dops; 19181da177e4SLinus Torvalds md->disk->queue = md->queue; 19191da177e4SLinus Torvalds md->disk->private_data = md; 19201da177e4SLinus Torvalds sprintf(md->disk->disk_name, "dm-%d", minor); 1921f26c5719SDan Williams 19225d2a228bSChristoph Hellwig if (IS_ENABLED(CONFIG_FS_DAX)) { 192330c6828aSChristoph Hellwig md->dax_dev = alloc_dax(md, &dm_dax_ops); 1924d7519392SChristoph Hellwig if (IS_ERR(md->dax_dev)) { 1925d7519392SChristoph Hellwig md->dax_dev = NULL; 1926f26c5719SDan Williams goto bad; 1927976431b0SDan Williams } 19287ac5360cSChristoph Hellwig set_dax_nocache(md->dax_dev); 19297ac5360cSChristoph Hellwig set_dax_nomc(md->dax_dev); 1930fb08a190SChristoph Hellwig if (dax_add_host(md->dax_dev, md->disk)) 1931f26c5719SDan Williams goto bad; 1932f26c5719SDan Williams } 19331da177e4SLinus Torvalds 19347e51f257SMike Anderson format_dev_t(md->name, MKDEV(_major, minor)); 19351da177e4SLinus Torvalds 1936c7c879eeSMichał Mirosław md->wq = alloc_workqueue("kdmflush/%s", WQ_MEM_RECLAIM, 0, md->name); 1937304f3f6aSMilan Broz if (!md->wq) 19380f20972fSMike Snitzer goto bad; 1939304f3f6aSMilan Broz 19409f6dc633SMike Snitzer md->pending_io = alloc_percpu(unsigned long); 19419f6dc633SMike Snitzer if (!md->pending_io) 19429f6dc633SMike Snitzer goto bad; 19439f6dc633SMike Snitzer 1944fd2ed4d2SMikulas Patocka dm_stats_init(&md->stats); 1945fd2ed4d2SMikulas Patocka 1946ba61fdd1SJeff Mahoney /* Populate the mapping, nobody knows we exist yet */ 1947f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 1948ba61fdd1SJeff Mahoney old_md = idr_replace(&_minor_idr, md, minor); 1949f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1950ba61fdd1SJeff Mahoney 1951ba61fdd1SJeff Mahoney BUG_ON(old_md != MINOR_ALLOCED); 1952ba61fdd1SJeff Mahoney 19531da177e4SLinus Torvalds return md; 19541da177e4SLinus Torvalds 19550f20972fSMike Snitzer bad: 19560f20972fSMike Snitzer cleanup_mapped_device(md); 195783d5e5b0SMikulas Patocka bad_io_barrier: 19581da177e4SLinus Torvalds free_minor(minor); 19596ed7ade8SMilan Broz bad_minor: 196010da4f79SJeff Mahoney module_put(THIS_MODULE); 19616ed7ade8SMilan Broz bad_module_get: 1962856eb091SMikulas Patocka kvfree(md); 19631da177e4SLinus Torvalds return NULL; 19641da177e4SLinus Torvalds } 19651da177e4SLinus Torvalds 1966ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md); 1967ae9da83fSJun'ichi Nomura 19681da177e4SLinus Torvalds static void free_dev(struct mapped_device *md) 19691da177e4SLinus Torvalds { 1970f331c029STejun Heo int minor = MINOR(disk_devt(md->disk)); 197163d94e48SJun'ichi Nomura 1972ae9da83fSJun'ichi Nomura unlock_fs(md); 19732eb6e1e3SKeith Busch 19740f20972fSMike Snitzer cleanup_mapped_device(md); 19750f20972fSMike Snitzer 19760f20972fSMike Snitzer free_table_devices(&md->table_devices); 19770f20972fSMike Snitzer dm_stats_cleanup(&md->stats); 197863a4f065SMike Snitzer free_minor(minor); 197963a4f065SMike Snitzer 198010da4f79SJeff Mahoney module_put(THIS_MODULE); 1981856eb091SMikulas Patocka kvfree(md); 19821da177e4SLinus Torvalds } 19831da177e4SLinus Torvalds 19842a2a4c51SJens Axboe static int __bind_mempools(struct mapped_device *md, struct dm_table *t) 1985e6ee8c0bSKiyoshi Ueda { 1986c0820cf5SMikulas Patocka struct dm_md_mempools *p = dm_table_get_md_mempools(t); 19872a2a4c51SJens Axboe int ret = 0; 1988e6ee8c0bSKiyoshi Ueda 1989545ed20eSToshi Kani if (dm_table_bio_based(t)) { 1990c0820cf5SMikulas Patocka /* 199164f52b0eSMike Snitzer * The md may already have mempools that need changing. 199264f52b0eSMike Snitzer * If so, reload bioset because front_pad may have changed 199316245bdcSJun'ichi Nomura * because a different table was loaded. 1994c0820cf5SMikulas Patocka */ 19956f1c819cSKent Overstreet bioset_exit(&md->bs); 19966f1c819cSKent Overstreet bioset_exit(&md->io_bs); 19970776aa0eSMike Snitzer 19986f1c819cSKent Overstreet } else if (bioset_initialized(&md->bs)) { 1999cbc4e3c1SMike Snitzer /* 20004e6e36c3SMike Snitzer * There's no need to reload with request-based dm 20014e6e36c3SMike Snitzer * because the size of front_pad doesn't change. 20024e6e36c3SMike Snitzer * Note for future: If you are to reload bioset, 20034e6e36c3SMike Snitzer * prep-ed requests in the queue may refer 20044e6e36c3SMike Snitzer * to bio from the old bioset, so you must walk 20054e6e36c3SMike Snitzer * through the queue to unprep. 2006cbc4e3c1SMike Snitzer */ 2007cbc4e3c1SMike Snitzer goto out; 2008cbc4e3c1SMike Snitzer } 2009cbc4e3c1SMike Snitzer 20106f1c819cSKent Overstreet BUG_ON(!p || 20116f1c819cSKent Overstreet bioset_initialized(&md->bs) || 20126f1c819cSKent Overstreet bioset_initialized(&md->io_bs)); 2013e6ee8c0bSKiyoshi Ueda 20142a2a4c51SJens Axboe ret = bioset_init_from_src(&md->bs, &p->bs); 20152a2a4c51SJens Axboe if (ret) 20162a2a4c51SJens Axboe goto out; 20172a2a4c51SJens Axboe ret = bioset_init_from_src(&md->io_bs, &p->io_bs); 20182a2a4c51SJens Axboe if (ret) 20192a2a4c51SJens Axboe bioset_exit(&md->bs); 2020e6ee8c0bSKiyoshi Ueda out: 202102233342SMike Snitzer /* mempool bind completed, no longer need any mempools in the table */ 2022e6ee8c0bSKiyoshi Ueda dm_table_free_md_mempools(t); 20232a2a4c51SJens Axboe return ret; 2024e6ee8c0bSKiyoshi Ueda } 2025e6ee8c0bSKiyoshi Ueda 20261da177e4SLinus Torvalds /* 20271da177e4SLinus Torvalds * Bind a table to the device. 20281da177e4SLinus Torvalds */ 20291da177e4SLinus Torvalds static void event_callback(void *context) 20301da177e4SLinus Torvalds { 20317a8c3d3bSMike Anderson unsigned long flags; 20327a8c3d3bSMike Anderson LIST_HEAD(uevents); 20331da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) context; 20341da177e4SLinus Torvalds 20357a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 20367a8c3d3bSMike Anderson list_splice_init(&md->uevent_list, &uevents); 20377a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 20387a8c3d3bSMike Anderson 2039ed9e1982STejun Heo dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); 20407a8c3d3bSMike Anderson 20411da177e4SLinus Torvalds atomic_inc(&md->event_nr); 20421da177e4SLinus Torvalds wake_up(&md->eventq); 204362e08243SMikulas Patocka dm_issue_global_event(); 20441da177e4SLinus Torvalds } 20451da177e4SLinus Torvalds 2046c217649bSMike Snitzer /* 2047042d2a9bSAlasdair G Kergon * Returns old map, which caller must destroy. 2048042d2a9bSAlasdair G Kergon */ 2049042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t, 2050754c5fc7SMike Snitzer struct queue_limits *limits) 20511da177e4SLinus Torvalds { 2052042d2a9bSAlasdair G Kergon struct dm_table *old_map; 20531da177e4SLinus Torvalds sector_t size; 20542a2a4c51SJens Axboe int ret; 20551da177e4SLinus Torvalds 20565a8f1f80SBart Van Assche lockdep_assert_held(&md->suspend_lock); 20575a8f1f80SBart Van Assche 20581da177e4SLinus Torvalds size = dm_table_get_size(t); 20593ac51e74SDarrick J. Wong 20603ac51e74SDarrick J. Wong /* 20613ac51e74SDarrick J. Wong * Wipe any geometry if the size of the table changed. 20623ac51e74SDarrick J. Wong */ 2063fd2ed4d2SMikulas Patocka if (size != dm_get_size(md)) 20643ac51e74SDarrick J. Wong memset(&md->geometry, 0, sizeof(md->geometry)); 20653ac51e74SDarrick J. Wong 20665424a0b8SMikulas Patocka if (!get_capacity(md->disk)) 20675424a0b8SMikulas Patocka set_capacity(md->disk, size); 20685424a0b8SMikulas Patocka else 2069f64d9b2eSChristoph Hellwig set_capacity_and_notify(md->disk, size); 20701da177e4SLinus Torvalds 2071cf222b37SAlasdair G Kergon dm_table_event_callback(t, event_callback, md); 20722ca3310eSAlasdair G Kergon 2073f5b4aee1SMike Snitzer if (dm_table_request_based(t)) { 207416f12266SMike Snitzer /* 20759c37de29SMike Snitzer * Leverage the fact that request-based DM targets are 20769c37de29SMike Snitzer * immutable singletons - used to optimize dm_mq_queue_rq. 207716f12266SMike Snitzer */ 207816f12266SMike Snitzer md->immutable_target = dm_table_get_immutable_target(t); 207916f12266SMike Snitzer } 2080e6ee8c0bSKiyoshi Ueda 20812a2a4c51SJens Axboe ret = __bind_mempools(md, t); 20822a2a4c51SJens Axboe if (ret) { 20832a2a4c51SJens Axboe old_map = ERR_PTR(ret); 20842a2a4c51SJens Axboe goto out; 20852a2a4c51SJens Axboe } 2086e6ee8c0bSKiyoshi Ueda 2087f5b4aee1SMike Snitzer ret = dm_table_set_restrictions(t, md->queue, limits); 2088bb37d772SDamien Le Moal if (ret) { 2089bb37d772SDamien Le Moal old_map = ERR_PTR(ret); 2090bb37d772SDamien Le Moal goto out; 2091bb37d772SDamien Le Moal } 2092bb37d772SDamien Le Moal 2093a12f5d48SEric Dumazet old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 20941d3aa6f6SMike Snitzer rcu_assign_pointer(md->map, (void *)t); 209536a0456fSAlasdair G Kergon md->immutable_target_type = dm_table_get_immutable_target_type(t); 209636a0456fSAlasdair G Kergon 209741abc4e1SHannes Reinecke if (old_map) 209883d5e5b0SMikulas Patocka dm_sync_table(md); 20992a2a4c51SJens Axboe out: 2100042d2a9bSAlasdair G Kergon return old_map; 21011da177e4SLinus Torvalds } 21021da177e4SLinus Torvalds 2103a7940155SAlasdair G Kergon /* 2104a7940155SAlasdair G Kergon * Returns unbound table for the caller to free. 2105a7940155SAlasdair G Kergon */ 2106a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md) 21071da177e4SLinus Torvalds { 2108a12f5d48SEric Dumazet struct dm_table *map = rcu_dereference_protected(md->map, 1); 21091da177e4SLinus Torvalds 21101da177e4SLinus Torvalds if (!map) 2111a7940155SAlasdair G Kergon return NULL; 21121da177e4SLinus Torvalds 21131da177e4SLinus Torvalds dm_table_event_callback(map, NULL, NULL); 21149cdb8520SMonam Agarwal RCU_INIT_POINTER(md->map, NULL); 211583d5e5b0SMikulas Patocka dm_sync_table(md); 2116a7940155SAlasdair G Kergon 2117a7940155SAlasdair G Kergon return map; 21181da177e4SLinus Torvalds } 21191da177e4SLinus Torvalds 21201da177e4SLinus Torvalds /* 21211da177e4SLinus Torvalds * Constructor for a new device. 21221da177e4SLinus Torvalds */ 21232b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result) 21241da177e4SLinus Torvalds { 21251da177e4SLinus Torvalds struct mapped_device *md; 21261da177e4SLinus Torvalds 21272b06cfffSAlasdair G Kergon md = alloc_dev(minor); 21281da177e4SLinus Torvalds if (!md) 21291da177e4SLinus Torvalds return -ENXIO; 21301da177e4SLinus Torvalds 213191ccbbacSTushar Sugandhi dm_ima_reset_data(md); 213291ccbbacSTushar Sugandhi 21331da177e4SLinus Torvalds *result = md; 21341da177e4SLinus Torvalds return 0; 21351da177e4SLinus Torvalds } 21361da177e4SLinus Torvalds 2137a5664dadSMike Snitzer /* 2138a5664dadSMike Snitzer * Functions to manage md->type. 2139a5664dadSMike Snitzer * All are required to hold md->type_lock. 2140a5664dadSMike Snitzer */ 2141a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md) 2142a5664dadSMike Snitzer { 2143a5664dadSMike Snitzer mutex_lock(&md->type_lock); 2144a5664dadSMike Snitzer } 2145a5664dadSMike Snitzer 2146a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md) 2147a5664dadSMike Snitzer { 2148a5664dadSMike Snitzer mutex_unlock(&md->type_lock); 2149a5664dadSMike Snitzer } 2150a5664dadSMike Snitzer 21517e0d574fSBart Van Assche void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type) 2152a5664dadSMike Snitzer { 215300c4fc3bSMike Snitzer BUG_ON(!mutex_is_locked(&md->type_lock)); 2154a5664dadSMike Snitzer md->type = type; 2155a5664dadSMike Snitzer } 2156a5664dadSMike Snitzer 21577e0d574fSBart Van Assche enum dm_queue_mode dm_get_md_type(struct mapped_device *md) 2158a5664dadSMike Snitzer { 2159a5664dadSMike Snitzer return md->type; 2160a5664dadSMike Snitzer } 2161a5664dadSMike Snitzer 216236a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md) 216336a0456fSAlasdair G Kergon { 216436a0456fSAlasdair G Kergon return md->immutable_target_type; 216536a0456fSAlasdair G Kergon } 216636a0456fSAlasdair G Kergon 21674a0b4ddfSMike Snitzer /* 2168f84cb8a4SMike Snitzer * The queue_limits are only valid as long as you have a reference 2169f84cb8a4SMike Snitzer * count on 'md'. 2170f84cb8a4SMike Snitzer */ 2171f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md) 2172f84cb8a4SMike Snitzer { 2173f84cb8a4SMike Snitzer BUG_ON(!atomic_read(&md->holders)); 2174f84cb8a4SMike Snitzer return &md->queue->limits; 2175f84cb8a4SMike Snitzer } 2176f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits); 2177f84cb8a4SMike Snitzer 21784a0b4ddfSMike Snitzer /* 21794a0b4ddfSMike Snitzer * Setup the DM device's queue based on md's type 21804a0b4ddfSMike Snitzer */ 2181591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t) 21824a0b4ddfSMike Snitzer { 2183ba305859SChristoph Hellwig enum dm_queue_mode type = dm_table_get_type(t); 2184c100ec49SMike Snitzer struct queue_limits limits; 2185ba305859SChristoph Hellwig int r; 2186bfebd1cdSMike Snitzer 2187545ed20eSToshi Kani switch (type) { 2188bfebd1cdSMike Snitzer case DM_TYPE_REQUEST_BASED: 2189681cc5e8SMike Snitzer md->disk->fops = &dm_rq_blk_dops; 2190e83068a5SMike Snitzer r = dm_mq_init_request_queue(md, t); 2191bfebd1cdSMike Snitzer if (r) { 2192681cc5e8SMike Snitzer DMERR("Cannot initialize queue for request-based dm mapped device"); 2193bfebd1cdSMike Snitzer return r; 2194bfebd1cdSMike Snitzer } 2195bfebd1cdSMike Snitzer break; 2196bfebd1cdSMike Snitzer case DM_TYPE_BIO_BASED: 2197545ed20eSToshi Kani case DM_TYPE_DAX_BIO_BASED: 2198bfebd1cdSMike Snitzer break; 21997e0d574fSBart Van Assche case DM_TYPE_NONE: 22007e0d574fSBart Van Assche WARN_ON_ONCE(true); 22017e0d574fSBart Van Assche break; 2202ff36ab34SMike Snitzer } 22034a0b4ddfSMike Snitzer 2204c100ec49SMike Snitzer r = dm_calculate_queue_limits(t, &limits); 2205c100ec49SMike Snitzer if (r) { 2206c100ec49SMike Snitzer DMERR("Cannot calculate initial queue limits"); 2207c100ec49SMike Snitzer return r; 2208c100ec49SMike Snitzer } 2209bb37d772SDamien Le Moal r = dm_table_set_restrictions(t, md->queue, &limits); 2210bb37d772SDamien Le Moal if (r) 2211bb37d772SDamien Le Moal return r; 221289f871afSChristoph Hellwig 2213e7089f65SLuis Chamberlain r = add_disk(md->disk); 2214e7089f65SLuis Chamberlain if (r) 2215e7089f65SLuis Chamberlain return r; 221689f871afSChristoph Hellwig 221789f871afSChristoph Hellwig r = dm_sysfs_init(md); 221889f871afSChristoph Hellwig if (r) { 221989f871afSChristoph Hellwig del_gendisk(md->disk); 222089f871afSChristoph Hellwig return r; 222189f871afSChristoph Hellwig } 2222ba305859SChristoph Hellwig md->type = type; 22234a0b4ddfSMike Snitzer return 0; 22244a0b4ddfSMike Snitzer } 22254a0b4ddfSMike Snitzer 22262bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev) 22271da177e4SLinus Torvalds { 22281da177e4SLinus Torvalds struct mapped_device *md; 22291da177e4SLinus Torvalds unsigned minor = MINOR(dev); 22301da177e4SLinus Torvalds 22311da177e4SLinus Torvalds if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 22321da177e4SLinus Torvalds return NULL; 22331da177e4SLinus Torvalds 2234f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 22351da177e4SLinus Torvalds 22361da177e4SLinus Torvalds md = idr_find(&_minor_idr, minor); 223749de5769SMike Snitzer if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) || 223849de5769SMike Snitzer test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { 2239637842cfSDavid Teigland md = NULL; 2240fba9f90eSJeff Mahoney goto out; 2241fba9f90eSJeff Mahoney } 22422bec1f4aSMikulas Patocka dm_get(md); 2243fba9f90eSJeff Mahoney out: 2244f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 22451da177e4SLinus Torvalds 2246637842cfSDavid Teigland return md; 2247637842cfSDavid Teigland } 22483cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md); 2249d229a958SDavid Teigland 22509ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md) 2251637842cfSDavid Teigland { 22529ade92a9SAlasdair G Kergon return md->interface_ptr; 22531da177e4SLinus Torvalds } 22541da177e4SLinus Torvalds 22551da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr) 22561da177e4SLinus Torvalds { 22571da177e4SLinus Torvalds md->interface_ptr = ptr; 22581da177e4SLinus Torvalds } 22591da177e4SLinus Torvalds 22601da177e4SLinus Torvalds void dm_get(struct mapped_device *md) 22611da177e4SLinus Torvalds { 22621da177e4SLinus Torvalds atomic_inc(&md->holders); 22633f77316dSKiyoshi Ueda BUG_ON(test_bit(DMF_FREEING, &md->flags)); 22641da177e4SLinus Torvalds } 22651da177e4SLinus Torvalds 226609ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md) 226709ee96b2SMikulas Patocka { 226809ee96b2SMikulas Patocka spin_lock(&_minor_lock); 226909ee96b2SMikulas Patocka if (test_bit(DMF_FREEING, &md->flags)) { 227009ee96b2SMikulas Patocka spin_unlock(&_minor_lock); 227109ee96b2SMikulas Patocka return -EBUSY; 227209ee96b2SMikulas Patocka } 227309ee96b2SMikulas Patocka dm_get(md); 227409ee96b2SMikulas Patocka spin_unlock(&_minor_lock); 227509ee96b2SMikulas Patocka return 0; 227609ee96b2SMikulas Patocka } 227709ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold); 227809ee96b2SMikulas Patocka 227972d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md) 228072d94861SAlasdair G Kergon { 228172d94861SAlasdair G Kergon return md->name; 228272d94861SAlasdair G Kergon } 228372d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name); 228472d94861SAlasdair G Kergon 22853f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait) 22861da177e4SLinus Torvalds { 22871134e5aeSMike Anderson struct dm_table *map; 228883d5e5b0SMikulas Patocka int srcu_idx; 22891da177e4SLinus Torvalds 22903f77316dSKiyoshi Ueda might_sleep(); 2291fba9f90eSJeff Mahoney 229263a4f065SMike Snitzer spin_lock(&_minor_lock); 22933f77316dSKiyoshi Ueda idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md)))); 2294fba9f90eSJeff Mahoney set_bit(DMF_FREEING, &md->flags); 2295f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 22963f77316dSKiyoshi Ueda 22977a5428dcSChristoph Hellwig blk_mark_disk_dead(md->disk); 22983b785fbcSBart Van Assche 2299ab7c7bb6SMikulas Patocka /* 2300ab7c7bb6SMikulas Patocka * Take suspend_lock so that presuspend and postsuspend methods 2301ab7c7bb6SMikulas Patocka * do not race with internal suspend. 2302ab7c7bb6SMikulas Patocka */ 2303ab7c7bb6SMikulas Patocka mutex_lock(&md->suspend_lock); 23042a708cffSJunichi Nomura map = dm_get_live_table(md, &srcu_idx); 23054f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) { 23061da177e4SLinus Torvalds dm_table_presuspend_targets(map); 2307adc0daadSMikulas Patocka set_bit(DMF_SUSPENDED, &md->flags); 23085df96f2bSMikulas Patocka set_bit(DMF_POST_SUSPENDING, &md->flags); 23091da177e4SLinus Torvalds dm_table_postsuspend_targets(map); 23101da177e4SLinus Torvalds } 231183d5e5b0SMikulas Patocka /* dm_put_live_table must be before msleep, otherwise deadlock is possible */ 231283d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 23132a708cffSJunichi Nomura mutex_unlock(&md->suspend_lock); 231483d5e5b0SMikulas Patocka 23153f77316dSKiyoshi Ueda /* 23163f77316dSKiyoshi Ueda * Rare, but there may be I/O requests still going to complete, 23173f77316dSKiyoshi Ueda * for example. Wait for all references to disappear. 23183f77316dSKiyoshi Ueda * No one should increment the reference count of the mapped_device, 23193f77316dSKiyoshi Ueda * after the mapped_device state becomes DMF_FREEING. 23203f77316dSKiyoshi Ueda */ 23213f77316dSKiyoshi Ueda if (wait) 23223f77316dSKiyoshi Ueda while (atomic_read(&md->holders)) 23233f77316dSKiyoshi Ueda msleep(1); 23243f77316dSKiyoshi Ueda else if (atomic_read(&md->holders)) 23253f77316dSKiyoshi Ueda DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)", 23263f77316dSKiyoshi Ueda dm_device_name(md), atomic_read(&md->holders)); 23273f77316dSKiyoshi Ueda 2328a7940155SAlasdair G Kergon dm_table_destroy(__unbind(md)); 23291da177e4SLinus Torvalds free_dev(md); 23301da177e4SLinus Torvalds } 23313f77316dSKiyoshi Ueda 23323f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md) 23333f77316dSKiyoshi Ueda { 23343f77316dSKiyoshi Ueda __dm_destroy(md, true); 23353f77316dSKiyoshi Ueda } 23363f77316dSKiyoshi Ueda 23373f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md) 23383f77316dSKiyoshi Ueda { 23393f77316dSKiyoshi Ueda __dm_destroy(md, false); 23403f77316dSKiyoshi Ueda } 23413f77316dSKiyoshi Ueda 23423f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md) 23433f77316dSKiyoshi Ueda { 23443f77316dSKiyoshi Ueda atomic_dec(&md->holders); 23451da177e4SLinus Torvalds } 234679eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put); 23471da177e4SLinus Torvalds 23489f6dc633SMike Snitzer static bool dm_in_flight_bios(struct mapped_device *md) 234985067747SMing Lei { 235085067747SMing Lei int cpu; 23519f6dc633SMike Snitzer unsigned long sum = 0; 235285067747SMing Lei 23539f6dc633SMike Snitzer for_each_possible_cpu(cpu) 23549f6dc633SMike Snitzer sum += *per_cpu_ptr(md->pending_io, cpu); 235585067747SMing Lei 235685067747SMing Lei return sum != 0; 235785067747SMing Lei } 235885067747SMing Lei 23592f064a59SPeter Zijlstra static int dm_wait_for_bios_completion(struct mapped_device *md, unsigned int task_state) 236046125c1cSMilan Broz { 236146125c1cSMilan Broz int r = 0; 23629f4c3f87SBart Van Assche DEFINE_WAIT(wait); 236346125c1cSMilan Broz 236485067747SMing Lei while (true) { 23659f4c3f87SBart Van Assche prepare_to_wait(&md->wait, &wait, task_state); 236646125c1cSMilan Broz 23679f6dc633SMike Snitzer if (!dm_in_flight_bios(md)) 236846125c1cSMilan Broz break; 236946125c1cSMilan Broz 2370e3fabdfdSBart Van Assche if (signal_pending_state(task_state, current)) { 237146125c1cSMilan Broz r = -EINTR; 237246125c1cSMilan Broz break; 237346125c1cSMilan Broz } 237446125c1cSMilan Broz 237546125c1cSMilan Broz io_schedule(); 237646125c1cSMilan Broz } 23779f4c3f87SBart Van Assche finish_wait(&md->wait, &wait); 2378b44ebeb0SMikulas Patocka 23799f6dc633SMike Snitzer smp_rmb(); 23809f6dc633SMike Snitzer 238146125c1cSMilan Broz return r; 238246125c1cSMilan Broz } 238346125c1cSMilan Broz 23842f064a59SPeter Zijlstra static int dm_wait_for_completion(struct mapped_device *md, unsigned int task_state) 238585067747SMing Lei { 238685067747SMing Lei int r = 0; 238785067747SMing Lei 238885067747SMing Lei if (!queue_is_mq(md->queue)) 238985067747SMing Lei return dm_wait_for_bios_completion(md, task_state); 239085067747SMing Lei 239185067747SMing Lei while (true) { 239285067747SMing Lei if (!blk_mq_queue_inflight(md->queue)) 239385067747SMing Lei break; 239485067747SMing Lei 239585067747SMing Lei if (signal_pending_state(task_state, current)) { 239685067747SMing Lei r = -EINTR; 239785067747SMing Lei break; 239885067747SMing Lei } 239985067747SMing Lei 240085067747SMing Lei msleep(5); 240185067747SMing Lei } 240285067747SMing Lei 240385067747SMing Lei return r; 240485067747SMing Lei } 240585067747SMing Lei 24061da177e4SLinus Torvalds /* 24071da177e4SLinus Torvalds * Process the deferred bios 24081da177e4SLinus Torvalds */ 2409ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work) 24101da177e4SLinus Torvalds { 24110c2915b8SMike Snitzer struct mapped_device *md = container_of(work, struct mapped_device, work); 24120c2915b8SMike Snitzer struct bio *bio; 2413ef208587SMikulas Patocka 24143b00b203SMikulas Patocka while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 2415022c2611SMikulas Patocka spin_lock_irq(&md->deferred_lock); 24160c2915b8SMike Snitzer bio = bio_list_pop(&md->deferred); 2417022c2611SMikulas Patocka spin_unlock_irq(&md->deferred_lock); 2418022c2611SMikulas Patocka 24190c2915b8SMike Snitzer if (!bio) 2420df12ee99SAlasdair G Kergon break; 242173d410c0SMilan Broz 24220c2915b8SMike Snitzer submit_bio_noacct(bio); 2423e6ee8c0bSKiyoshi Ueda } 24241da177e4SLinus Torvalds } 24251da177e4SLinus Torvalds 24269a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md) 2427304f3f6aSMilan Broz { 24283b00b203SMikulas Patocka clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 24294e857c58SPeter Zijlstra smp_mb__after_atomic(); 243053d5914fSMikulas Patocka queue_work(md->wq, &md->work); 2431304f3f6aSMilan Broz } 2432304f3f6aSMilan Broz 24331da177e4SLinus Torvalds /* 2434042d2a9bSAlasdair G Kergon * Swap in a new table, returning the old one for the caller to destroy. 24351da177e4SLinus Torvalds */ 2436042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table) 24371da177e4SLinus Torvalds { 243887eb5b21SMike Christie struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL); 2439754c5fc7SMike Snitzer struct queue_limits limits; 2440042d2a9bSAlasdair G Kergon int r; 24411da177e4SLinus Torvalds 2442e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 24431da177e4SLinus Torvalds 24441da177e4SLinus Torvalds /* device must be suspended */ 24454f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) 244693c534aeSAlasdair G Kergon goto out; 24471da177e4SLinus Torvalds 24483ae70656SMike Snitzer /* 24493ae70656SMike Snitzer * If the new table has no data devices, retain the existing limits. 24503ae70656SMike Snitzer * This helps multipath with queue_if_no_path if all paths disappear, 24513ae70656SMike Snitzer * then new I/O is queued based on these limits, and then some paths 24523ae70656SMike Snitzer * reappear. 24533ae70656SMike Snitzer */ 24543ae70656SMike Snitzer if (dm_table_has_no_data_devices(table)) { 245583d5e5b0SMikulas Patocka live_map = dm_get_live_table_fast(md); 24563ae70656SMike Snitzer if (live_map) 24573ae70656SMike Snitzer limits = md->queue->limits; 245883d5e5b0SMikulas Patocka dm_put_live_table_fast(md); 24593ae70656SMike Snitzer } 24603ae70656SMike Snitzer 246187eb5b21SMike Christie if (!live_map) { 2462754c5fc7SMike Snitzer r = dm_calculate_queue_limits(table, &limits); 2463042d2a9bSAlasdair G Kergon if (r) { 2464042d2a9bSAlasdair G Kergon map = ERR_PTR(r); 2465754c5fc7SMike Snitzer goto out; 2466042d2a9bSAlasdair G Kergon } 246787eb5b21SMike Christie } 2468754c5fc7SMike Snitzer 2469042d2a9bSAlasdair G Kergon map = __bind(md, table, &limits); 247062e08243SMikulas Patocka dm_issue_global_event(); 24711da177e4SLinus Torvalds 247293c534aeSAlasdair G Kergon out: 2473e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 2474042d2a9bSAlasdair G Kergon return map; 24751da177e4SLinus Torvalds } 24761da177e4SLinus Torvalds 24771da177e4SLinus Torvalds /* 24781da177e4SLinus Torvalds * Functions to lock and unlock any filesystem running on the 24791da177e4SLinus Torvalds * device. 24801da177e4SLinus Torvalds */ 24812ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md) 24821da177e4SLinus Torvalds { 2483e39e2e95SAlasdair G Kergon int r; 24841da177e4SLinus Torvalds 2485040f04bdSChristoph Hellwig WARN_ON(test_bit(DMF_FROZEN, &md->flags)); 2486dfbe03f6SAlasdair G Kergon 2487977115c0SChristoph Hellwig r = freeze_bdev(md->disk->part0); 2488040f04bdSChristoph Hellwig if (!r) 2489aa8d7c2fSAlasdair G Kergon set_bit(DMF_FROZEN, &md->flags); 2490040f04bdSChristoph Hellwig return r; 24911da177e4SLinus Torvalds } 24921da177e4SLinus Torvalds 24932ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md) 24941da177e4SLinus Torvalds { 2495aa8d7c2fSAlasdair G Kergon if (!test_bit(DMF_FROZEN, &md->flags)) 2496aa8d7c2fSAlasdair G Kergon return; 2497977115c0SChristoph Hellwig thaw_bdev(md->disk->part0); 2498aa8d7c2fSAlasdair G Kergon clear_bit(DMF_FROZEN, &md->flags); 24991da177e4SLinus Torvalds } 25001da177e4SLinus Torvalds 25011da177e4SLinus Torvalds /* 2502b48633f8SBart Van Assche * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG 2503b48633f8SBart Van Assche * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE 2504b48633f8SBart Van Assche * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY 2505b48633f8SBart Van Assche * 2506ffcc3936SMike Snitzer * If __dm_suspend returns 0, the device is completely quiescent 2507ffcc3936SMike Snitzer * now. There is no request-processing activity. All new requests 2508ffcc3936SMike Snitzer * are being added to md->deferred list. 2509cec47e3dSKiyoshi Ueda */ 2510ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map, 25112f064a59SPeter Zijlstra unsigned suspend_flags, unsigned int task_state, 2512eaf9a736SMike Snitzer int dmf_suspended_flag) 25131da177e4SLinus Torvalds { 2514ffcc3936SMike Snitzer bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG; 2515ffcc3936SMike Snitzer bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG; 2516ffcc3936SMike Snitzer int r; 2517cf222b37SAlasdair G Kergon 25185a8f1f80SBart Van Assche lockdep_assert_held(&md->suspend_lock); 25195a8f1f80SBart Van Assche 25202e93ccc1SKiyoshi Ueda /* 25212e93ccc1SKiyoshi Ueda * DMF_NOFLUSH_SUSPENDING must be set before presuspend. 25222e93ccc1SKiyoshi Ueda * This flag is cleared before dm_suspend returns. 25232e93ccc1SKiyoshi Ueda */ 25242e93ccc1SKiyoshi Ueda if (noflush) 25252e93ccc1SKiyoshi Ueda set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 252686331f39SBart Van Assche else 2527ac75b09fSMike Snitzer DMDEBUG("%s: suspending with flush", dm_device_name(md)); 25282e93ccc1SKiyoshi Ueda 2529d67ee213SMike Snitzer /* 2530d67ee213SMike Snitzer * This gets reverted if there's an error later and the targets 2531d67ee213SMike Snitzer * provide the .presuspend_undo hook. 2532d67ee213SMike Snitzer */ 25331da177e4SLinus Torvalds dm_table_presuspend_targets(map); 25341da177e4SLinus Torvalds 25352e93ccc1SKiyoshi Ueda /* 25369f518b27SKiyoshi Ueda * Flush I/O to the device. 25379f518b27SKiyoshi Ueda * Any I/O submitted after lock_fs() may not be flushed. 25389f518b27SKiyoshi Ueda * noflush takes precedence over do_lockfs. 25399f518b27SKiyoshi Ueda * (lock_fs() flushes I/Os and waits for them to complete.) 25402e93ccc1SKiyoshi Ueda */ 254132a926daSMikulas Patocka if (!noflush && do_lockfs) { 25422ca3310eSAlasdair G Kergon r = lock_fs(md); 2543d67ee213SMike Snitzer if (r) { 2544d67ee213SMike Snitzer dm_table_presuspend_undo_targets(map); 2545ffcc3936SMike Snitzer return r; 2546aa8d7c2fSAlasdair G Kergon } 2547d67ee213SMike Snitzer } 25481da177e4SLinus Torvalds 25491da177e4SLinus Torvalds /* 25503b00b203SMikulas Patocka * Here we must make sure that no processes are submitting requests 25513b00b203SMikulas Patocka * to target drivers i.e. no one may be executing 255296c9865cSMike Snitzer * dm_split_and_process_bio from dm_submit_bio. 25533b00b203SMikulas Patocka * 255496c9865cSMike Snitzer * To get all processes out of dm_split_and_process_bio in dm_submit_bio, 25553b00b203SMikulas Patocka * we take the write lock. To prevent any process from reentering 255696c9865cSMike Snitzer * dm_split_and_process_bio from dm_submit_bio and quiesce the thread 25570cede372SMike Snitzer * (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND and call 25586a8736d1STejun Heo * flush_workqueue(md->wq). 25591da177e4SLinus Torvalds */ 25601eb787ecSAlasdair G Kergon set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 256141abc4e1SHannes Reinecke if (map) 256283d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 25631da177e4SLinus Torvalds 2564d0bcb878SKiyoshi Ueda /* 256529e4013dSTejun Heo * Stop md->queue before flushing md->wq in case request-based 256629e4013dSTejun Heo * dm defers requests to md->wq from md->queue. 2567d0bcb878SKiyoshi Ueda */ 25686a23e05cSJens Axboe if (dm_request_based(md)) 2569eca7ee6dSMike Snitzer dm_stop_queue(md->queue); 2570cec47e3dSKiyoshi Ueda 2571d0bcb878SKiyoshi Ueda flush_workqueue(md->wq); 2572d0bcb878SKiyoshi Ueda 25731da177e4SLinus Torvalds /* 25743b00b203SMikulas Patocka * At this point no more requests are entering target request routines. 25753b00b203SMikulas Patocka * We call dm_wait_for_completion to wait for all existing requests 25763b00b203SMikulas Patocka * to finish. 25771da177e4SLinus Torvalds */ 2578b48633f8SBart Van Assche r = dm_wait_for_completion(md, task_state); 2579eaf9a736SMike Snitzer if (!r) 2580eaf9a736SMike Snitzer set_bit(dmf_suspended_flag, &md->flags); 25811da177e4SLinus Torvalds 25826d6f10dfSMilan Broz if (noflush) 2583022c2611SMikulas Patocka clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 258441abc4e1SHannes Reinecke if (map) 258583d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 25862e93ccc1SKiyoshi Ueda 25871da177e4SLinus Torvalds /* were we interrupted ? */ 258846125c1cSMilan Broz if (r < 0) { 25899a1fb464SMikulas Patocka dm_queue_flush(md); 259073d410c0SMilan Broz 2591cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 2592eca7ee6dSMike Snitzer dm_start_queue(md->queue); 2593cec47e3dSKiyoshi Ueda 25942ca3310eSAlasdair G Kergon unlock_fs(md); 2595d67ee213SMike Snitzer dm_table_presuspend_undo_targets(map); 2596ffcc3936SMike Snitzer /* pushback list is already flushed, so skip flush */ 2597ffcc3936SMike Snitzer } 2598ffcc3936SMike Snitzer 2599ffcc3936SMike Snitzer return r; 26002ca3310eSAlasdair G Kergon } 26012ca3310eSAlasdair G Kergon 26023b00b203SMikulas Patocka /* 2603ffcc3936SMike Snitzer * We need to be able to change a mapping table under a mounted 2604ffcc3936SMike Snitzer * filesystem. For example we might want to move some data in 2605ffcc3936SMike Snitzer * the background. Before the table can be swapped with 2606ffcc3936SMike Snitzer * dm_bind_table, dm_suspend must be called to flush any in 2607ffcc3936SMike Snitzer * flight bios and ensure that any further io gets deferred. 26083b00b203SMikulas Patocka */ 2609ffcc3936SMike Snitzer /* 2610ffcc3936SMike Snitzer * Suspend mechanism in request-based dm. 2611ffcc3936SMike Snitzer * 2612ffcc3936SMike Snitzer * 1. Flush all I/Os by lock_fs() if needed. 2613ffcc3936SMike Snitzer * 2. Stop dispatching any I/O by stopping the request_queue. 2614ffcc3936SMike Snitzer * 3. Wait for all in-flight I/Os to be completed or requeued. 2615ffcc3936SMike Snitzer * 2616ffcc3936SMike Snitzer * To abort suspend, start the request_queue. 2617ffcc3936SMike Snitzer */ 2618ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags) 2619ffcc3936SMike Snitzer { 2620ffcc3936SMike Snitzer struct dm_table *map = NULL; 2621ffcc3936SMike Snitzer int r = 0; 2622ffcc3936SMike Snitzer 2623ffcc3936SMike Snitzer retry: 2624ffcc3936SMike Snitzer mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); 2625ffcc3936SMike Snitzer 2626ffcc3936SMike Snitzer if (dm_suspended_md(md)) { 2627ffcc3936SMike Snitzer r = -EINVAL; 2628ffcc3936SMike Snitzer goto out_unlock; 2629ffcc3936SMike Snitzer } 2630ffcc3936SMike Snitzer 2631ffcc3936SMike Snitzer if (dm_suspended_internally_md(md)) { 2632ffcc3936SMike Snitzer /* already internally suspended, wait for internal resume */ 2633ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2634ffcc3936SMike Snitzer r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); 2635ffcc3936SMike Snitzer if (r) 2636ffcc3936SMike Snitzer return r; 2637ffcc3936SMike Snitzer goto retry; 2638ffcc3936SMike Snitzer } 2639ffcc3936SMike Snitzer 2640a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2641ffcc3936SMike Snitzer 2642eaf9a736SMike Snitzer r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED); 2643ffcc3936SMike Snitzer if (r) 2644ffcc3936SMike Snitzer goto out_unlock; 26453b00b203SMikulas Patocka 26465df96f2bSMikulas Patocka set_bit(DMF_POST_SUSPENDING, &md->flags); 26474d4471cbSKiyoshi Ueda dm_table_postsuspend_targets(map); 26485df96f2bSMikulas Patocka clear_bit(DMF_POST_SUSPENDING, &md->flags); 26494d4471cbSKiyoshi Ueda 2650d287483dSAlasdair G Kergon out_unlock: 2651e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 2652cf222b37SAlasdair G Kergon return r; 26531da177e4SLinus Torvalds } 26541da177e4SLinus Torvalds 2655ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map) 26561da177e4SLinus Torvalds { 2657ffcc3936SMike Snitzer if (map) { 2658ffcc3936SMike Snitzer int r = dm_table_resume_targets(map); 26598757b776SMilan Broz if (r) 2660ffcc3936SMike Snitzer return r; 2661ffcc3936SMike Snitzer } 26622ca3310eSAlasdair G Kergon 26639a1fb464SMikulas Patocka dm_queue_flush(md); 26642ca3310eSAlasdair G Kergon 2665cec47e3dSKiyoshi Ueda /* 2666cec47e3dSKiyoshi Ueda * Flushing deferred I/Os must be done after targets are resumed 2667cec47e3dSKiyoshi Ueda * so that mapping of targets can work correctly. 2668cec47e3dSKiyoshi Ueda * Request-based dm is queueing the deferred I/Os in its request_queue. 2669cec47e3dSKiyoshi Ueda */ 2670cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 2671eca7ee6dSMike Snitzer dm_start_queue(md->queue); 2672cec47e3dSKiyoshi Ueda 26732ca3310eSAlasdair G Kergon unlock_fs(md); 26742ca3310eSAlasdair G Kergon 2675ffcc3936SMike Snitzer return 0; 2676ffcc3936SMike Snitzer } 2677ffcc3936SMike Snitzer 2678ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md) 2679ffcc3936SMike Snitzer { 26808dc23658SMinfei Huang int r; 2681ffcc3936SMike Snitzer struct dm_table *map = NULL; 2682ffcc3936SMike Snitzer 2683ffcc3936SMike Snitzer retry: 26848dc23658SMinfei Huang r = -EINVAL; 2685ffcc3936SMike Snitzer mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); 2686ffcc3936SMike Snitzer 2687ffcc3936SMike Snitzer if (!dm_suspended_md(md)) 2688ffcc3936SMike Snitzer goto out; 2689ffcc3936SMike Snitzer 2690ffcc3936SMike Snitzer if (dm_suspended_internally_md(md)) { 2691ffcc3936SMike Snitzer /* already internally suspended, wait for internal resume */ 2692ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2693ffcc3936SMike Snitzer r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); 2694ffcc3936SMike Snitzer if (r) 2695ffcc3936SMike Snitzer return r; 2696ffcc3936SMike Snitzer goto retry; 2697ffcc3936SMike Snitzer } 2698ffcc3936SMike Snitzer 2699a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2700ffcc3936SMike Snitzer if (!map || !dm_table_get_size(map)) 2701ffcc3936SMike Snitzer goto out; 2702ffcc3936SMike Snitzer 2703ffcc3936SMike Snitzer r = __dm_resume(md, map); 2704ffcc3936SMike Snitzer if (r) 2705ffcc3936SMike Snitzer goto out; 2706ffcc3936SMike Snitzer 27072ca3310eSAlasdair G Kergon clear_bit(DMF_SUSPENDED, &md->flags); 2708cf222b37SAlasdair G Kergon out: 2709e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 27102ca3310eSAlasdair G Kergon 2711cf222b37SAlasdair G Kergon return r; 27121da177e4SLinus Torvalds } 27131da177e4SLinus Torvalds 2714fd2ed4d2SMikulas Patocka /* 2715fd2ed4d2SMikulas Patocka * Internal suspend/resume works like userspace-driven suspend. It waits 2716fd2ed4d2SMikulas Patocka * until all bios finish and prevents issuing new bios to the target drivers. 2717fd2ed4d2SMikulas Patocka * It may be used only from the kernel. 2718fd2ed4d2SMikulas Patocka */ 2719fd2ed4d2SMikulas Patocka 2720ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags) 2721ffcc3936SMike Snitzer { 2722ffcc3936SMike Snitzer struct dm_table *map = NULL; 2723ffcc3936SMike Snitzer 27241ea0654eSBart Van Assche lockdep_assert_held(&md->suspend_lock); 27251ea0654eSBart Van Assche 272696b26c8cSMikulas Patocka if (md->internal_suspend_count++) 2727ffcc3936SMike Snitzer return; /* nested internal suspend */ 2728ffcc3936SMike Snitzer 2729ffcc3936SMike Snitzer if (dm_suspended_md(md)) { 2730ffcc3936SMike Snitzer set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2731ffcc3936SMike Snitzer return; /* nest suspend */ 2732ffcc3936SMike Snitzer } 2733ffcc3936SMike Snitzer 2734a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2735ffcc3936SMike Snitzer 2736ffcc3936SMike Snitzer /* 2737ffcc3936SMike Snitzer * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is 2738ffcc3936SMike Snitzer * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend 2739ffcc3936SMike Snitzer * would require changing .presuspend to return an error -- avoid this 2740ffcc3936SMike Snitzer * until there is a need for more elaborate variants of internal suspend. 2741ffcc3936SMike Snitzer */ 2742eaf9a736SMike Snitzer (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE, 2743eaf9a736SMike Snitzer DMF_SUSPENDED_INTERNALLY); 2744ffcc3936SMike Snitzer 27455df96f2bSMikulas Patocka set_bit(DMF_POST_SUSPENDING, &md->flags); 2746ffcc3936SMike Snitzer dm_table_postsuspend_targets(map); 27475df96f2bSMikulas Patocka clear_bit(DMF_POST_SUSPENDING, &md->flags); 2748ffcc3936SMike Snitzer } 2749ffcc3936SMike Snitzer 2750ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md) 2751ffcc3936SMike Snitzer { 275296b26c8cSMikulas Patocka BUG_ON(!md->internal_suspend_count); 275396b26c8cSMikulas Patocka 275496b26c8cSMikulas Patocka if (--md->internal_suspend_count) 2755ffcc3936SMike Snitzer return; /* resume from nested internal suspend */ 2756ffcc3936SMike Snitzer 2757ffcc3936SMike Snitzer if (dm_suspended_md(md)) 2758ffcc3936SMike Snitzer goto done; /* resume from nested suspend */ 2759ffcc3936SMike Snitzer 2760ffcc3936SMike Snitzer /* 2761ffcc3936SMike Snitzer * NOTE: existing callers don't need to call dm_table_resume_targets 2762ffcc3936SMike Snitzer * (which may fail -- so best to avoid it for now by passing NULL map) 2763ffcc3936SMike Snitzer */ 2764ffcc3936SMike Snitzer (void) __dm_resume(md, NULL); 2765ffcc3936SMike Snitzer 2766ffcc3936SMike Snitzer done: 2767ffcc3936SMike Snitzer clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2768ffcc3936SMike Snitzer smp_mb__after_atomic(); 2769ffcc3936SMike Snitzer wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY); 2770ffcc3936SMike Snitzer } 2771ffcc3936SMike Snitzer 2772ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md) 2773fd2ed4d2SMikulas Patocka { 2774fd2ed4d2SMikulas Patocka mutex_lock(&md->suspend_lock); 2775ffcc3936SMike Snitzer __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG); 2776ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2777ffcc3936SMike Snitzer } 2778ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush); 2779ffcc3936SMike Snitzer 2780ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md) 2781ffcc3936SMike Snitzer { 2782ffcc3936SMike Snitzer mutex_lock(&md->suspend_lock); 2783ffcc3936SMike Snitzer __dm_internal_resume(md); 2784ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2785ffcc3936SMike Snitzer } 2786ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume); 2787ffcc3936SMike Snitzer 2788ffcc3936SMike Snitzer /* 2789ffcc3936SMike Snitzer * Fast variants of internal suspend/resume hold md->suspend_lock, 2790ffcc3936SMike Snitzer * which prevents interaction with userspace-driven suspend. 2791ffcc3936SMike Snitzer */ 2792ffcc3936SMike Snitzer 2793ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md) 2794ffcc3936SMike Snitzer { 2795ffcc3936SMike Snitzer mutex_lock(&md->suspend_lock); 2796ffcc3936SMike Snitzer if (dm_suspended_md(md) || dm_suspended_internally_md(md)) 2797fd2ed4d2SMikulas Patocka return; 2798fd2ed4d2SMikulas Patocka 2799fd2ed4d2SMikulas Patocka set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 2800fd2ed4d2SMikulas Patocka synchronize_srcu(&md->io_barrier); 2801fd2ed4d2SMikulas Patocka flush_workqueue(md->wq); 2802fd2ed4d2SMikulas Patocka dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); 2803fd2ed4d2SMikulas Patocka } 2804b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast); 2805fd2ed4d2SMikulas Patocka 2806ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md) 2807fd2ed4d2SMikulas Patocka { 2808ffcc3936SMike Snitzer if (dm_suspended_md(md) || dm_suspended_internally_md(md)) 2809fd2ed4d2SMikulas Patocka goto done; 2810fd2ed4d2SMikulas Patocka 2811fd2ed4d2SMikulas Patocka dm_queue_flush(md); 2812fd2ed4d2SMikulas Patocka 2813fd2ed4d2SMikulas Patocka done: 2814fd2ed4d2SMikulas Patocka mutex_unlock(&md->suspend_lock); 2815fd2ed4d2SMikulas Patocka } 2816b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast); 2817fd2ed4d2SMikulas Patocka 28181da177e4SLinus Torvalds /*----------------------------------------------------------------- 28191da177e4SLinus Torvalds * Event notification. 28201da177e4SLinus Torvalds *---------------------------------------------------------------*/ 28213abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action, 282260935eb2SMilan Broz unsigned cookie) 282369267a30SAlasdair G Kergon { 28246958c1c6SMikulas Patocka int r; 28256958c1c6SMikulas Patocka unsigned noio_flag; 282660935eb2SMilan Broz char udev_cookie[DM_COOKIE_LENGTH]; 282760935eb2SMilan Broz char *envp[] = { udev_cookie, NULL }; 282860935eb2SMilan Broz 28296958c1c6SMikulas Patocka noio_flag = memalloc_noio_save(); 28306958c1c6SMikulas Patocka 283160935eb2SMilan Broz if (!cookie) 28326958c1c6SMikulas Patocka r = kobject_uevent(&disk_to_dev(md->disk)->kobj, action); 283360935eb2SMilan Broz else { 283460935eb2SMilan Broz snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u", 283560935eb2SMilan Broz DM_COOKIE_ENV_VAR_NAME, cookie); 28366958c1c6SMikulas Patocka r = kobject_uevent_env(&disk_to_dev(md->disk)->kobj, 28373abf85b5SPeter Rajnoha action, envp); 283860935eb2SMilan Broz } 28396958c1c6SMikulas Patocka 28406958c1c6SMikulas Patocka memalloc_noio_restore(noio_flag); 28416958c1c6SMikulas Patocka 28426958c1c6SMikulas Patocka return r; 284369267a30SAlasdair G Kergon } 284469267a30SAlasdair G Kergon 28457a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md) 28467a8c3d3bSMike Anderson { 28477a8c3d3bSMike Anderson return atomic_add_return(1, &md->uevent_seq); 28487a8c3d3bSMike Anderson } 28497a8c3d3bSMike Anderson 28501da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md) 28511da177e4SLinus Torvalds { 28521da177e4SLinus Torvalds return atomic_read(&md->event_nr); 28531da177e4SLinus Torvalds } 28541da177e4SLinus Torvalds 28551da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr) 28561da177e4SLinus Torvalds { 28571da177e4SLinus Torvalds return wait_event_interruptible(md->eventq, 28581da177e4SLinus Torvalds (event_nr != atomic_read(&md->event_nr))); 28591da177e4SLinus Torvalds } 28601da177e4SLinus Torvalds 28617a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist) 28627a8c3d3bSMike Anderson { 28637a8c3d3bSMike Anderson unsigned long flags; 28647a8c3d3bSMike Anderson 28657a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 28667a8c3d3bSMike Anderson list_add(elist, &md->uevent_list); 28677a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 28687a8c3d3bSMike Anderson } 28697a8c3d3bSMike Anderson 28701da177e4SLinus Torvalds /* 28711da177e4SLinus Torvalds * The gendisk is only valid as long as you have a reference 28721da177e4SLinus Torvalds * count on 'md'. 28731da177e4SLinus Torvalds */ 28741da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md) 28751da177e4SLinus Torvalds { 28761da177e4SLinus Torvalds return md->disk; 28771da177e4SLinus Torvalds } 287865ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk); 28791da177e4SLinus Torvalds 2880784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md) 2881784aae73SMilan Broz { 28822995fa78SMikulas Patocka return &md->kobj_holder.kobj; 2883784aae73SMilan Broz } 2884784aae73SMilan Broz 2885784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj) 2886784aae73SMilan Broz { 2887784aae73SMilan Broz struct mapped_device *md; 2888784aae73SMilan Broz 28892995fa78SMikulas Patocka md = container_of(kobj, struct mapped_device, kobj_holder.kobj); 2890784aae73SMilan Broz 2891b9a41d21SHou Tao spin_lock(&_minor_lock); 2892b9a41d21SHou Tao if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { 2893b9a41d21SHou Tao md = NULL; 2894b9a41d21SHou Tao goto out; 2895b9a41d21SHou Tao } 2896784aae73SMilan Broz dm_get(md); 2897b9a41d21SHou Tao out: 2898b9a41d21SHou Tao spin_unlock(&_minor_lock); 2899b9a41d21SHou Tao 2900784aae73SMilan Broz return md; 2901784aae73SMilan Broz } 2902784aae73SMilan Broz 29034f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md) 29041da177e4SLinus Torvalds { 29051da177e4SLinus Torvalds return test_bit(DMF_SUSPENDED, &md->flags); 29061da177e4SLinus Torvalds } 29071da177e4SLinus Torvalds 29085df96f2bSMikulas Patocka static int dm_post_suspending_md(struct mapped_device *md) 29095df96f2bSMikulas Patocka { 29105df96f2bSMikulas Patocka return test_bit(DMF_POST_SUSPENDING, &md->flags); 29115df96f2bSMikulas Patocka } 29125df96f2bSMikulas Patocka 2913ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md) 2914ffcc3936SMike Snitzer { 2915ffcc3936SMike Snitzer return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2916ffcc3936SMike Snitzer } 2917ffcc3936SMike Snitzer 29182c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md) 29192c140a24SMikulas Patocka { 29202c140a24SMikulas Patocka return test_bit(DMF_DEFERRED_REMOVE, &md->flags); 29212c140a24SMikulas Patocka } 29222c140a24SMikulas Patocka 292364dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti) 292464dbce58SKiyoshi Ueda { 292533bd6f06SMike Snitzer return dm_suspended_md(ti->table->md); 292664dbce58SKiyoshi Ueda } 292764dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended); 292864dbce58SKiyoshi Ueda 29295df96f2bSMikulas Patocka int dm_post_suspending(struct dm_target *ti) 29305df96f2bSMikulas Patocka { 293133bd6f06SMike Snitzer return dm_post_suspending_md(ti->table->md); 29325df96f2bSMikulas Patocka } 29335df96f2bSMikulas Patocka EXPORT_SYMBOL_GPL(dm_post_suspending); 29345df96f2bSMikulas Patocka 29352e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti) 29362e93ccc1SKiyoshi Ueda { 293733bd6f06SMike Snitzer return __noflush_suspending(ti->table->md); 29382e93ccc1SKiyoshi Ueda } 29392e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending); 29402e93ccc1SKiyoshi Ueda 29417e0d574fSBart Van Assche struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type, 2942cfc97abcSMike Snitzer unsigned per_io_data_size, unsigned min_pool_size, 2943cfc97abcSMike Snitzer bool integrity, bool poll) 2944e6ee8c0bSKiyoshi Ueda { 2945115485e8SMike Snitzer struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id); 294678d8e58aSMike Snitzer unsigned int pool_size = 0; 294764f52b0eSMike Snitzer unsigned int front_pad, io_front_pad; 29486f1c819cSKent Overstreet int ret; 2949e6ee8c0bSKiyoshi Ueda 2950e6ee8c0bSKiyoshi Ueda if (!pools) 29514e6e36c3SMike Snitzer return NULL; 2952e6ee8c0bSKiyoshi Ueda 295378d8e58aSMike Snitzer switch (type) { 295478d8e58aSMike Snitzer case DM_TYPE_BIO_BASED: 2955545ed20eSToshi Kani case DM_TYPE_DAX_BIO_BASED: 29560776aa0eSMike Snitzer pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size); 295762f26317SJeffle Xu front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + DM_TARGET_IO_BIO_OFFSET; 295862f26317SJeffle Xu io_front_pad = roundup(per_io_data_size, __alignof__(struct dm_io)) + DM_IO_BIO_OFFSET; 2959cfc97abcSMike Snitzer ret = bioset_init(&pools->io_bs, pool_size, io_front_pad, poll ? BIOSET_PERCPU_CACHE : 0); 29606f1c819cSKent Overstreet if (ret) 296164f52b0eSMike Snitzer goto out; 29626f1c819cSKent Overstreet if (integrity && bioset_integrity_create(&pools->io_bs, pool_size)) 2963eb8db831SChristoph Hellwig goto out; 296478d8e58aSMike Snitzer break; 296578d8e58aSMike Snitzer case DM_TYPE_REQUEST_BASED: 29660776aa0eSMike Snitzer pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size); 296778d8e58aSMike Snitzer front_pad = offsetof(struct dm_rq_clone_bio_info, clone); 2968591ddcfcSMike Snitzer /* per_io_data_size is used for blk-mq pdu at queue allocation */ 296978d8e58aSMike Snitzer break; 297078d8e58aSMike Snitzer default: 297178d8e58aSMike Snitzer BUG(); 297278d8e58aSMike Snitzer } 297378d8e58aSMike Snitzer 29746f1c819cSKent Overstreet ret = bioset_init(&pools->bs, pool_size, front_pad, 0); 29756f1c819cSKent Overstreet if (ret) 29765f015204SJun'ichi Nomura goto out; 2977e6ee8c0bSKiyoshi Ueda 29786f1c819cSKent Overstreet if (integrity && bioset_integrity_create(&pools->bs, pool_size)) 29795f015204SJun'ichi Nomura goto out; 2980a91a2785SMartin K. Petersen 2981e6ee8c0bSKiyoshi Ueda return pools; 298278d8e58aSMike Snitzer 29835f015204SJun'ichi Nomura out: 29845f015204SJun'ichi Nomura dm_free_md_mempools(pools); 2985e6ee8c0bSKiyoshi Ueda 29864e6e36c3SMike Snitzer return NULL; 2987e6ee8c0bSKiyoshi Ueda } 2988e6ee8c0bSKiyoshi Ueda 2989e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools) 2990e6ee8c0bSKiyoshi Ueda { 2991e6ee8c0bSKiyoshi Ueda if (!pools) 2992e6ee8c0bSKiyoshi Ueda return; 2993e6ee8c0bSKiyoshi Ueda 29946f1c819cSKent Overstreet bioset_exit(&pools->bs); 29956f1c819cSKent Overstreet bioset_exit(&pools->io_bs); 2996e6ee8c0bSKiyoshi Ueda 2997e6ee8c0bSKiyoshi Ueda kfree(pools); 2998e6ee8c0bSKiyoshi Ueda } 2999e6ee8c0bSKiyoshi Ueda 30009c72bad1SChristoph Hellwig struct dm_pr { 30019c72bad1SChristoph Hellwig u64 old_key; 30029c72bad1SChristoph Hellwig u64 new_key; 30039c72bad1SChristoph Hellwig u32 flags; 30049c72bad1SChristoph Hellwig bool fail_early; 30059c72bad1SChristoph Hellwig }; 30069c72bad1SChristoph Hellwig 30079c72bad1SChristoph Hellwig static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn, 30089c72bad1SChristoph Hellwig void *data) 30099c72bad1SChristoph Hellwig { 30109c72bad1SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 30119c72bad1SChristoph Hellwig struct dm_table *table; 30129c72bad1SChristoph Hellwig struct dm_target *ti; 30139c72bad1SChristoph Hellwig int ret = -ENOTTY, srcu_idx; 30149c72bad1SChristoph Hellwig 30159c72bad1SChristoph Hellwig table = dm_get_live_table(md, &srcu_idx); 30169c72bad1SChristoph Hellwig if (!table || !dm_table_get_size(table)) 30179c72bad1SChristoph Hellwig goto out; 30189c72bad1SChristoph Hellwig 30199c72bad1SChristoph Hellwig /* We only support devices that have a single target */ 30209c72bad1SChristoph Hellwig if (dm_table_get_num_targets(table) != 1) 30219c72bad1SChristoph Hellwig goto out; 30229c72bad1SChristoph Hellwig ti = dm_table_get_target(table, 0); 30239c72bad1SChristoph Hellwig 30249c72bad1SChristoph Hellwig ret = -EINVAL; 30259c72bad1SChristoph Hellwig if (!ti->type->iterate_devices) 30269c72bad1SChristoph Hellwig goto out; 30279c72bad1SChristoph Hellwig 30289c72bad1SChristoph Hellwig ret = ti->type->iterate_devices(ti, fn, data); 30299c72bad1SChristoph Hellwig out: 30309c72bad1SChristoph Hellwig dm_put_live_table(md, srcu_idx); 30319c72bad1SChristoph Hellwig return ret; 30329c72bad1SChristoph Hellwig } 30339c72bad1SChristoph Hellwig 30349c72bad1SChristoph Hellwig /* 30359c72bad1SChristoph Hellwig * For register / unregister we need to manually call out to every path. 30369c72bad1SChristoph Hellwig */ 30379c72bad1SChristoph Hellwig static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev, 30389c72bad1SChristoph Hellwig sector_t start, sector_t len, void *data) 30399c72bad1SChristoph Hellwig { 30409c72bad1SChristoph Hellwig struct dm_pr *pr = data; 30419c72bad1SChristoph Hellwig const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops; 30429c72bad1SChristoph Hellwig 30439c72bad1SChristoph Hellwig if (!ops || !ops->pr_register) 30449c72bad1SChristoph Hellwig return -EOPNOTSUPP; 30459c72bad1SChristoph Hellwig return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags); 30469c72bad1SChristoph Hellwig } 30479c72bad1SChristoph Hellwig 304871cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key, 304971cdb697SChristoph Hellwig u32 flags) 305071cdb697SChristoph Hellwig { 30519c72bad1SChristoph Hellwig struct dm_pr pr = { 30529c72bad1SChristoph Hellwig .old_key = old_key, 30539c72bad1SChristoph Hellwig .new_key = new_key, 30549c72bad1SChristoph Hellwig .flags = flags, 30559c72bad1SChristoph Hellwig .fail_early = true, 30569c72bad1SChristoph Hellwig }; 30579c72bad1SChristoph Hellwig int ret; 305871cdb697SChristoph Hellwig 30599c72bad1SChristoph Hellwig ret = dm_call_pr(bdev, __dm_pr_register, &pr); 30609c72bad1SChristoph Hellwig if (ret && new_key) { 30619c72bad1SChristoph Hellwig /* unregister all paths if we failed to register any path */ 30629c72bad1SChristoph Hellwig pr.old_key = new_key; 30639c72bad1SChristoph Hellwig pr.new_key = 0; 30649c72bad1SChristoph Hellwig pr.flags = 0; 30659c72bad1SChristoph Hellwig pr.fail_early = false; 30669c72bad1SChristoph Hellwig dm_call_pr(bdev, __dm_pr_register, &pr); 30679c72bad1SChristoph Hellwig } 306871cdb697SChristoph Hellwig 30699c72bad1SChristoph Hellwig return ret; 307071cdb697SChristoph Hellwig } 307171cdb697SChristoph Hellwig 307271cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type, 307371cdb697SChristoph Hellwig u32 flags) 307471cdb697SChristoph Hellwig { 307571cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 307671cdb697SChristoph Hellwig const struct pr_ops *ops; 3077971888c4SMike Snitzer int r, srcu_idx; 307871cdb697SChristoph Hellwig 30795bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 308071cdb697SChristoph Hellwig if (r < 0) 3081971888c4SMike Snitzer goto out; 308271cdb697SChristoph Hellwig 308371cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 308471cdb697SChristoph Hellwig if (ops && ops->pr_reserve) 308571cdb697SChristoph Hellwig r = ops->pr_reserve(bdev, key, type, flags); 308671cdb697SChristoph Hellwig else 308771cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3088971888c4SMike Snitzer out: 3089971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 309071cdb697SChristoph Hellwig return r; 309171cdb697SChristoph Hellwig } 309271cdb697SChristoph Hellwig 309371cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type) 309471cdb697SChristoph Hellwig { 309571cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 309671cdb697SChristoph Hellwig const struct pr_ops *ops; 3097971888c4SMike Snitzer int r, srcu_idx; 309871cdb697SChristoph Hellwig 30995bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 310071cdb697SChristoph Hellwig if (r < 0) 3101971888c4SMike Snitzer goto out; 310271cdb697SChristoph Hellwig 310371cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 310471cdb697SChristoph Hellwig if (ops && ops->pr_release) 310571cdb697SChristoph Hellwig r = ops->pr_release(bdev, key, type); 310671cdb697SChristoph Hellwig else 310771cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3108971888c4SMike Snitzer out: 3109971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 311071cdb697SChristoph Hellwig return r; 311171cdb697SChristoph Hellwig } 311271cdb697SChristoph Hellwig 311371cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key, 311471cdb697SChristoph Hellwig enum pr_type type, bool abort) 311571cdb697SChristoph Hellwig { 311671cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 311771cdb697SChristoph Hellwig const struct pr_ops *ops; 3118971888c4SMike Snitzer int r, srcu_idx; 311971cdb697SChristoph Hellwig 31205bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 312171cdb697SChristoph Hellwig if (r < 0) 3122971888c4SMike Snitzer goto out; 312371cdb697SChristoph Hellwig 312471cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 312571cdb697SChristoph Hellwig if (ops && ops->pr_preempt) 312671cdb697SChristoph Hellwig r = ops->pr_preempt(bdev, old_key, new_key, type, abort); 312771cdb697SChristoph Hellwig else 312871cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3129971888c4SMike Snitzer out: 3130971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 313171cdb697SChristoph Hellwig return r; 313271cdb697SChristoph Hellwig } 313371cdb697SChristoph Hellwig 313471cdb697SChristoph Hellwig static int dm_pr_clear(struct block_device *bdev, u64 key) 313571cdb697SChristoph Hellwig { 313671cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 313771cdb697SChristoph Hellwig const struct pr_ops *ops; 3138971888c4SMike Snitzer int r, srcu_idx; 313971cdb697SChristoph Hellwig 31405bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 314171cdb697SChristoph Hellwig if (r < 0) 3142971888c4SMike Snitzer goto out; 314371cdb697SChristoph Hellwig 314471cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 314571cdb697SChristoph Hellwig if (ops && ops->pr_clear) 314671cdb697SChristoph Hellwig r = ops->pr_clear(bdev, key); 314771cdb697SChristoph Hellwig else 314871cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3149971888c4SMike Snitzer out: 3150971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 315171cdb697SChristoph Hellwig return r; 315271cdb697SChristoph Hellwig } 315371cdb697SChristoph Hellwig 315471cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = { 315571cdb697SChristoph Hellwig .pr_register = dm_pr_register, 315671cdb697SChristoph Hellwig .pr_reserve = dm_pr_reserve, 315771cdb697SChristoph Hellwig .pr_release = dm_pr_release, 315871cdb697SChristoph Hellwig .pr_preempt = dm_pr_preempt, 315971cdb697SChristoph Hellwig .pr_clear = dm_pr_clear, 316071cdb697SChristoph Hellwig }; 316171cdb697SChristoph Hellwig 316283d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = { 3163c62b37d9SChristoph Hellwig .submit_bio = dm_submit_bio, 3164b99fdcdcSMing Lei .poll_bio = dm_poll_bio, 31651da177e4SLinus Torvalds .open = dm_blk_open, 31661da177e4SLinus Torvalds .release = dm_blk_close, 3167aa129a22SMilan Broz .ioctl = dm_blk_ioctl, 31683ac51e74SDarrick J. Wong .getgeo = dm_blk_getgeo, 3169e76239a3SChristoph Hellwig .report_zones = dm_blk_report_zones, 317071cdb697SChristoph Hellwig .pr_ops = &dm_pr_ops, 31711da177e4SLinus Torvalds .owner = THIS_MODULE 31721da177e4SLinus Torvalds }; 31731da177e4SLinus Torvalds 3174681cc5e8SMike Snitzer static const struct block_device_operations dm_rq_blk_dops = { 3175681cc5e8SMike Snitzer .open = dm_blk_open, 3176681cc5e8SMike Snitzer .release = dm_blk_close, 3177681cc5e8SMike Snitzer .ioctl = dm_blk_ioctl, 3178681cc5e8SMike Snitzer .getgeo = dm_blk_getgeo, 3179681cc5e8SMike Snitzer .pr_ops = &dm_pr_ops, 3180681cc5e8SMike Snitzer .owner = THIS_MODULE 3181681cc5e8SMike Snitzer }; 3182681cc5e8SMike Snitzer 3183f26c5719SDan Williams static const struct dax_operations dm_dax_ops = { 3184f26c5719SDan Williams .direct_access = dm_dax_direct_access, 3185cdf6cdcdSVivek Goyal .zero_page_range = dm_dax_zero_page_range, 3186f26c5719SDan Williams }; 3187f26c5719SDan Williams 31881da177e4SLinus Torvalds /* 31891da177e4SLinus Torvalds * module hooks 31901da177e4SLinus Torvalds */ 31911da177e4SLinus Torvalds module_init(dm_init); 31921da177e4SLinus Torvalds module_exit(dm_exit); 31931da177e4SLinus Torvalds 31941da177e4SLinus Torvalds module_param(major, uint, 0); 31951da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper"); 3196f4790826SMike Snitzer 3197e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR); 3198e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools"); 3199e8603136SMike Snitzer 3200115485e8SMike Snitzer module_param(dm_numa_node, int, S_IRUGO | S_IWUSR); 3201115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations"); 3202115485e8SMike Snitzer 3203a666e5c0SMikulas Patocka module_param(swap_bios, int, S_IRUGO | S_IWUSR); 3204a666e5c0SMikulas Patocka MODULE_PARM_DESC(swap_bios, "Maximum allowed inflight swap IOs"); 3205a666e5c0SMikulas Patocka 32061da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver"); 32071da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 32081da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 3209