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 741da177e4SLinus Torvalds /* 7564f52b0eSMike Snitzer * One of these is allocated (on-stack) per original bio. 761da177e4SLinus Torvalds */ 7764f52b0eSMike Snitzer struct clone_info { 7864f52b0eSMike Snitzer struct dm_table *map; 7964f52b0eSMike Snitzer struct bio *bio; 8064f52b0eSMike Snitzer struct dm_io *io; 8164f52b0eSMike Snitzer sector_t sector; 8264f52b0eSMike Snitzer unsigned sector_count; 83b99fdcdcSMing Lei bool submit_as_polled; 8464f52b0eSMike Snitzer }; 8564f52b0eSMike Snitzer 8662f26317SJeffle Xu #define DM_TARGET_IO_BIO_OFFSET (offsetof(struct dm_target_io, clone)) 8762f26317SJeffle Xu #define DM_IO_BIO_OFFSET \ 8862f26317SJeffle Xu (offsetof(struct dm_target_io, clone) + offsetof(struct dm_io, tio)) 8962f26317SJeffle Xu 906c23f0bdSChristoph Hellwig static inline struct dm_target_io *clone_to_tio(struct bio *clone) 916c23f0bdSChristoph Hellwig { 926c23f0bdSChristoph Hellwig return container_of(clone, struct dm_target_io, clone); 936c23f0bdSChristoph Hellwig } 946c23f0bdSChristoph Hellwig 9564f52b0eSMike Snitzer void *dm_per_bio_data(struct bio *bio, size_t data_size) 9664f52b0eSMike Snitzer { 97655f3aadSMike Snitzer if (!dm_tio_flagged(clone_to_tio(bio), DM_TIO_INSIDE_DM_IO)) 9862f26317SJeffle Xu return (char *)bio - DM_TARGET_IO_BIO_OFFSET - data_size; 9962f26317SJeffle Xu return (char *)bio - DM_IO_BIO_OFFSET - data_size; 10064f52b0eSMike Snitzer } 10164f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_per_bio_data); 10264f52b0eSMike Snitzer 10364f52b0eSMike Snitzer struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size) 10464f52b0eSMike Snitzer { 10564f52b0eSMike Snitzer struct dm_io *io = (struct dm_io *)((char *)data + data_size); 10664f52b0eSMike Snitzer if (io->magic == DM_IO_MAGIC) 10762f26317SJeffle Xu return (struct bio *)((char *)io + DM_IO_BIO_OFFSET); 10864f52b0eSMike Snitzer BUG_ON(io->magic != DM_TIO_MAGIC); 10962f26317SJeffle Xu return (struct bio *)((char *)io + DM_TARGET_IO_BIO_OFFSET); 11064f52b0eSMike Snitzer } 11164f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_from_per_bio_data); 11264f52b0eSMike Snitzer 11364f52b0eSMike Snitzer unsigned dm_bio_get_target_bio_nr(const struct bio *bio) 11464f52b0eSMike Snitzer { 11564f52b0eSMike Snitzer return container_of(bio, struct dm_target_io, clone)->target_bio_nr; 11664f52b0eSMike Snitzer } 11764f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_get_target_bio_nr); 11864f52b0eSMike Snitzer 119ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1) 120ba61fdd1SJeff Mahoney 121115485e8SMike Snitzer #define DM_NUMA_NODE NUMA_NO_NODE 122115485e8SMike Snitzer static int dm_numa_node = DM_NUMA_NODE; 123faad87dfSMike Snitzer 124a666e5c0SMikulas Patocka #define DEFAULT_SWAP_BIOS (8 * 1048576 / PAGE_SIZE) 125a666e5c0SMikulas Patocka static int swap_bios = DEFAULT_SWAP_BIOS; 126a666e5c0SMikulas Patocka static int get_swap_bios(void) 127a666e5c0SMikulas Patocka { 128a666e5c0SMikulas Patocka int latch = READ_ONCE(swap_bios); 129a666e5c0SMikulas Patocka if (unlikely(latch <= 0)) 130a666e5c0SMikulas Patocka latch = DEFAULT_SWAP_BIOS; 131a666e5c0SMikulas Patocka return latch; 132a666e5c0SMikulas Patocka } 133a666e5c0SMikulas Patocka 134e6ee8c0bSKiyoshi Ueda /* 135e6ee8c0bSKiyoshi Ueda * For mempools pre-allocation at the table loading time. 136e6ee8c0bSKiyoshi Ueda */ 137e6ee8c0bSKiyoshi Ueda struct dm_md_mempools { 1386f1c819cSKent Overstreet struct bio_set bs; 1396f1c819cSKent Overstreet struct bio_set io_bs; 140e6ee8c0bSKiyoshi Ueda }; 141e6ee8c0bSKiyoshi Ueda 14286f1152bSBenjamin Marzinski struct table_device { 14386f1152bSBenjamin Marzinski struct list_head list; 144b0b4d7c6SElena Reshetova refcount_t count; 14586f1152bSBenjamin Marzinski struct dm_dev dm_dev; 14686f1152bSBenjamin Marzinski }; 14786f1152bSBenjamin Marzinski 148f4790826SMike Snitzer /* 149e8603136SMike Snitzer * Bio-based DM's mempools' reserved IOs set by the user. 150e8603136SMike Snitzer */ 1514cc96131SMike Snitzer #define RESERVED_BIO_BASED_IOS 16 152e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS; 153e8603136SMike Snitzer 154115485e8SMike Snitzer static int __dm_get_module_param_int(int *module_param, int min, int max) 155115485e8SMike Snitzer { 1566aa7de05SMark Rutland int param = READ_ONCE(*module_param); 157115485e8SMike Snitzer int modified_param = 0; 158115485e8SMike Snitzer bool modified = true; 159115485e8SMike Snitzer 160115485e8SMike Snitzer if (param < min) 161115485e8SMike Snitzer modified_param = min; 162115485e8SMike Snitzer else if (param > max) 163115485e8SMike Snitzer modified_param = max; 164115485e8SMike Snitzer else 165115485e8SMike Snitzer modified = false; 166115485e8SMike Snitzer 167115485e8SMike Snitzer if (modified) { 168115485e8SMike Snitzer (void)cmpxchg(module_param, param, modified_param); 169115485e8SMike Snitzer param = modified_param; 170115485e8SMike Snitzer } 171115485e8SMike Snitzer 172115485e8SMike Snitzer return param; 173115485e8SMike Snitzer } 174115485e8SMike Snitzer 1754cc96131SMike Snitzer unsigned __dm_get_module_param(unsigned *module_param, 176f4790826SMike Snitzer unsigned def, unsigned max) 177f4790826SMike Snitzer { 1786aa7de05SMark Rutland unsigned param = READ_ONCE(*module_param); 17909c2d531SMike Snitzer unsigned modified_param = 0; 180f4790826SMike Snitzer 18109c2d531SMike Snitzer if (!param) 18209c2d531SMike Snitzer modified_param = def; 18309c2d531SMike Snitzer else if (param > max) 18409c2d531SMike Snitzer modified_param = max; 185f4790826SMike Snitzer 18609c2d531SMike Snitzer if (modified_param) { 18709c2d531SMike Snitzer (void)cmpxchg(module_param, param, modified_param); 18809c2d531SMike Snitzer param = modified_param; 189f4790826SMike Snitzer } 190f4790826SMike Snitzer 19109c2d531SMike Snitzer return param; 192f4790826SMike Snitzer } 193f4790826SMike Snitzer 194e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void) 195e8603136SMike Snitzer { 19609c2d531SMike Snitzer return __dm_get_module_param(&reserved_bio_based_ios, 1974cc96131SMike Snitzer RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS); 198e8603136SMike Snitzer } 199e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios); 200e8603136SMike Snitzer 201115485e8SMike Snitzer static unsigned dm_get_numa_node(void) 202115485e8SMike Snitzer { 203115485e8SMike Snitzer return __dm_get_module_param_int(&dm_numa_node, 204115485e8SMike Snitzer DM_NUMA_NODE, num_online_nodes() - 1); 205115485e8SMike Snitzer } 206115485e8SMike Snitzer 2071da177e4SLinus Torvalds static int __init local_init(void) 2081da177e4SLinus Torvalds { 209e689fbabSMike Snitzer int r; 2101ae49ea2SMike Snitzer 21151e5b2bdSMike Anderson r = dm_uevent_init(); 21251157b4aSKiyoshi Ueda if (r) 213e689fbabSMike Snitzer return r; 21451e5b2bdSMike Anderson 215acfe0ad7SMikulas Patocka deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1); 216acfe0ad7SMikulas Patocka if (!deferred_remove_workqueue) { 217acfe0ad7SMikulas Patocka r = -ENOMEM; 218acfe0ad7SMikulas Patocka goto out_uevent_exit; 219acfe0ad7SMikulas Patocka } 220acfe0ad7SMikulas Patocka 2211da177e4SLinus Torvalds _major = major; 2221da177e4SLinus Torvalds r = register_blkdev(_major, _name); 22351157b4aSKiyoshi Ueda if (r < 0) 224acfe0ad7SMikulas Patocka goto out_free_workqueue; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds if (!_major) 2271da177e4SLinus Torvalds _major = r; 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds return 0; 23051157b4aSKiyoshi Ueda 231acfe0ad7SMikulas Patocka out_free_workqueue: 232acfe0ad7SMikulas Patocka destroy_workqueue(deferred_remove_workqueue); 23351157b4aSKiyoshi Ueda out_uevent_exit: 23451157b4aSKiyoshi Ueda dm_uevent_exit(); 23551157b4aSKiyoshi Ueda 23651157b4aSKiyoshi Ueda return r; 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds static void local_exit(void) 2401da177e4SLinus Torvalds { 2412c140a24SMikulas Patocka flush_scheduled_work(); 242acfe0ad7SMikulas Patocka destroy_workqueue(deferred_remove_workqueue); 2432c140a24SMikulas Patocka 24400d59405SAkinobu Mita unregister_blkdev(_major, _name); 24551e5b2bdSMike Anderson dm_uevent_exit(); 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds _major = 0; 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds DMINFO("cleaned up"); 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds 252b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = { 2531da177e4SLinus Torvalds local_init, 2541da177e4SLinus Torvalds dm_target_init, 2551da177e4SLinus Torvalds dm_linear_init, 2561da177e4SLinus Torvalds dm_stripe_init, 257952b3557SMikulas Patocka dm_io_init, 258945fa4d2SMikulas Patocka dm_kcopyd_init, 2591da177e4SLinus Torvalds dm_interface_init, 260fd2ed4d2SMikulas Patocka dm_statistics_init, 2611da177e4SLinus Torvalds }; 2621da177e4SLinus Torvalds 263b9249e55SAlasdair G Kergon static void (*_exits[])(void) = { 2641da177e4SLinus Torvalds local_exit, 2651da177e4SLinus Torvalds dm_target_exit, 2661da177e4SLinus Torvalds dm_linear_exit, 2671da177e4SLinus Torvalds dm_stripe_exit, 268952b3557SMikulas Patocka dm_io_exit, 269945fa4d2SMikulas Patocka dm_kcopyd_exit, 2701da177e4SLinus Torvalds dm_interface_exit, 271fd2ed4d2SMikulas Patocka dm_statistics_exit, 2721da177e4SLinus Torvalds }; 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds static int __init dm_init(void) 2751da177e4SLinus Torvalds { 2761da177e4SLinus Torvalds const int count = ARRAY_SIZE(_inits); 2771da177e4SLinus Torvalds int r, i; 2781da177e4SLinus Torvalds 279f1cd6cb2STushar Sugandhi #if (IS_ENABLED(CONFIG_IMA) && !IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE)) 280f1cd6cb2STushar Sugandhi DMWARN("CONFIG_IMA_DISABLE_HTABLE is disabled." 281f1cd6cb2STushar Sugandhi " Duplicate IMA measurements will not be recorded in the IMA log."); 282f1cd6cb2STushar Sugandhi #endif 283f1cd6cb2STushar Sugandhi 2841da177e4SLinus Torvalds for (i = 0; i < count; i++) { 2851da177e4SLinus Torvalds r = _inits[i](); 2861da177e4SLinus Torvalds if (r) 2871da177e4SLinus Torvalds goto bad; 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds return 0; 2911da177e4SLinus Torvalds bad: 2921da177e4SLinus Torvalds while (i--) 2931da177e4SLinus Torvalds _exits[i](); 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds return r; 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds static void __exit dm_exit(void) 2991da177e4SLinus Torvalds { 3001da177e4SLinus Torvalds int i = ARRAY_SIZE(_exits); 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds while (i--) 3031da177e4SLinus Torvalds _exits[i](); 304d15b774cSAlasdair G Kergon 305d15b774cSAlasdair G Kergon /* 306d15b774cSAlasdair G Kergon * Should be empty by this point. 307d15b774cSAlasdair G Kergon */ 308d15b774cSAlasdair G Kergon idr_destroy(&_minor_idr); 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds /* 3121da177e4SLinus Torvalds * Block device functions 3131da177e4SLinus Torvalds */ 314432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md) 315432a212cSMike Anderson { 316432a212cSMike Anderson return test_bit(DMF_DELETING, &md->flags); 317432a212cSMike Anderson } 318432a212cSMike Anderson 319fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode) 3201da177e4SLinus Torvalds { 3211da177e4SLinus Torvalds struct mapped_device *md; 3221da177e4SLinus Torvalds 323fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 324fba9f90eSJeff Mahoney 325fe5f9f2cSAl Viro md = bdev->bd_disk->private_data; 326fba9f90eSJeff Mahoney if (!md) 327fba9f90eSJeff Mahoney goto out; 328fba9f90eSJeff Mahoney 3295c6bd75dSAlasdair G Kergon if (test_bit(DMF_FREEING, &md->flags) || 330432a212cSMike Anderson dm_deleting_md(md)) { 331fba9f90eSJeff Mahoney md = NULL; 332fba9f90eSJeff Mahoney goto out; 333fba9f90eSJeff Mahoney } 334fba9f90eSJeff Mahoney 3351da177e4SLinus Torvalds dm_get(md); 3365c6bd75dSAlasdair G Kergon atomic_inc(&md->open_count); 337fba9f90eSJeff Mahoney out: 338fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 339fba9f90eSJeff Mahoney 340fba9f90eSJeff Mahoney return md ? 0 : -ENXIO; 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds 343db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode) 3441da177e4SLinus Torvalds { 34563a4f065SMike Snitzer struct mapped_device *md; 3466e9624b8SArnd Bergmann 3474a1aeb98SMilan Broz spin_lock(&_minor_lock); 3484a1aeb98SMilan Broz 34963a4f065SMike Snitzer md = disk->private_data; 35063a4f065SMike Snitzer if (WARN_ON(!md)) 35163a4f065SMike Snitzer goto out; 35263a4f065SMike Snitzer 3532c140a24SMikulas Patocka if (atomic_dec_and_test(&md->open_count) && 3542c140a24SMikulas Patocka (test_bit(DMF_DEFERRED_REMOVE, &md->flags))) 355acfe0ad7SMikulas Patocka queue_work(deferred_remove_workqueue, &deferred_remove_work); 3562c140a24SMikulas Patocka 3571da177e4SLinus Torvalds dm_put(md); 35863a4f065SMike Snitzer out: 3594a1aeb98SMilan Broz spin_unlock(&_minor_lock); 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds 3625c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md) 3635c6bd75dSAlasdair G Kergon { 3645c6bd75dSAlasdair G Kergon return atomic_read(&md->open_count); 3655c6bd75dSAlasdair G Kergon } 3665c6bd75dSAlasdair G Kergon 3675c6bd75dSAlasdair G Kergon /* 3685c6bd75dSAlasdair G Kergon * Guarantees nothing is using the device before it's deleted. 3695c6bd75dSAlasdair G Kergon */ 3702c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred) 3715c6bd75dSAlasdair G Kergon { 3725c6bd75dSAlasdair G Kergon int r = 0; 3735c6bd75dSAlasdair G Kergon 3745c6bd75dSAlasdair G Kergon spin_lock(&_minor_lock); 3755c6bd75dSAlasdair G Kergon 3762c140a24SMikulas Patocka if (dm_open_count(md)) { 3775c6bd75dSAlasdair G Kergon r = -EBUSY; 3782c140a24SMikulas Patocka if (mark_deferred) 3792c140a24SMikulas Patocka set_bit(DMF_DEFERRED_REMOVE, &md->flags); 3802c140a24SMikulas Patocka } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags)) 3812c140a24SMikulas Patocka r = -EEXIST; 3825c6bd75dSAlasdair G Kergon else 3835c6bd75dSAlasdair G Kergon set_bit(DMF_DELETING, &md->flags); 3845c6bd75dSAlasdair G Kergon 3855c6bd75dSAlasdair G Kergon spin_unlock(&_minor_lock); 3865c6bd75dSAlasdair G Kergon 3875c6bd75dSAlasdair G Kergon return r; 3885c6bd75dSAlasdair G Kergon } 3895c6bd75dSAlasdair G Kergon 3902c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md) 3912c140a24SMikulas Patocka { 3922c140a24SMikulas Patocka int r = 0; 3932c140a24SMikulas Patocka 3942c140a24SMikulas Patocka spin_lock(&_minor_lock); 3952c140a24SMikulas Patocka 3962c140a24SMikulas Patocka if (test_bit(DMF_DELETING, &md->flags)) 3972c140a24SMikulas Patocka r = -EBUSY; 3982c140a24SMikulas Patocka else 3992c140a24SMikulas Patocka clear_bit(DMF_DEFERRED_REMOVE, &md->flags); 4002c140a24SMikulas Patocka 4012c140a24SMikulas Patocka spin_unlock(&_minor_lock); 4022c140a24SMikulas Patocka 4032c140a24SMikulas Patocka return r; 4042c140a24SMikulas Patocka } 4052c140a24SMikulas Patocka 4062c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w) 4072c140a24SMikulas Patocka { 4082c140a24SMikulas Patocka dm_deferred_remove(); 4092c140a24SMikulas Patocka } 4102c140a24SMikulas Patocka 4113ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 4123ac51e74SDarrick J. Wong { 4133ac51e74SDarrick J. Wong struct mapped_device *md = bdev->bd_disk->private_data; 4143ac51e74SDarrick J. Wong 4153ac51e74SDarrick J. Wong return dm_get_geometry(md, geo); 4163ac51e74SDarrick J. Wong } 4173ac51e74SDarrick J. Wong 418971888c4SMike Snitzer static int dm_prepare_ioctl(struct mapped_device *md, int *srcu_idx, 4195bd5e8d8SMike Snitzer struct block_device **bdev) 420aa129a22SMilan Broz { 42166482026SMike Snitzer struct dm_target *tgt; 4226c182cd8SHannes Reinecke struct dm_table *map; 423971888c4SMike Snitzer int r; 424aa129a22SMilan Broz 4256c182cd8SHannes Reinecke retry: 426e56f81e0SChristoph Hellwig r = -ENOTTY; 427971888c4SMike Snitzer map = dm_get_live_table(md, srcu_idx); 428aa129a22SMilan Broz if (!map || !dm_table_get_size(map)) 429971888c4SMike Snitzer return r; 430aa129a22SMilan Broz 431aa129a22SMilan Broz /* We only support devices that have a single target */ 432aa129a22SMilan Broz if (dm_table_get_num_targets(map) != 1) 433971888c4SMike Snitzer return r; 434aa129a22SMilan Broz 43566482026SMike Snitzer tgt = dm_table_get_target(map, 0); 43666482026SMike Snitzer if (!tgt->type->prepare_ioctl) 437e56f81e0SChristoph Hellwig return r; 438aa129a22SMilan Broz 439971888c4SMike Snitzer if (dm_suspended_md(md)) 440971888c4SMike Snitzer return -EAGAIN; 441971888c4SMike Snitzer 4425bd5e8d8SMike Snitzer r = tgt->type->prepare_ioctl(tgt, bdev); 4435bbbfdf6SJunichi Nomura if (r == -ENOTCONN && !fatal_signal_pending(current)) { 444971888c4SMike Snitzer dm_put_live_table(md, *srcu_idx); 4456c182cd8SHannes Reinecke msleep(10); 4466c182cd8SHannes Reinecke goto retry; 4476c182cd8SHannes Reinecke } 448971888c4SMike Snitzer 449e56f81e0SChristoph Hellwig return r; 450e56f81e0SChristoph Hellwig } 4516c182cd8SHannes Reinecke 452971888c4SMike Snitzer static void dm_unprepare_ioctl(struct mapped_device *md, int srcu_idx) 453971888c4SMike Snitzer { 454971888c4SMike Snitzer dm_put_live_table(md, srcu_idx); 455971888c4SMike Snitzer } 456971888c4SMike Snitzer 457e56f81e0SChristoph Hellwig static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode, 458e56f81e0SChristoph Hellwig unsigned int cmd, unsigned long arg) 459e56f81e0SChristoph Hellwig { 460e56f81e0SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 461971888c4SMike Snitzer int r, srcu_idx; 462e56f81e0SChristoph Hellwig 4635bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 464e56f81e0SChristoph Hellwig if (r < 0) 465971888c4SMike Snitzer goto out; 466e56f81e0SChristoph Hellwig 467e56f81e0SChristoph Hellwig if (r > 0) { 468e56f81e0SChristoph Hellwig /* 469e980f623SChristoph Hellwig * Target determined this ioctl is being issued against a 470e980f623SChristoph Hellwig * subset of the parent bdev; require extra privileges. 471e56f81e0SChristoph Hellwig */ 472e980f623SChristoph Hellwig if (!capable(CAP_SYS_RAWIO)) { 4730378c625SMike Snitzer DMDEBUG_LIMIT( 474e980f623SChristoph Hellwig "%s: sending ioctl %x to DM device without required privilege.", 475e980f623SChristoph Hellwig current->comm, cmd); 476e980f623SChristoph Hellwig r = -ENOIOCTLCMD; 477e56f81e0SChristoph Hellwig goto out; 478e56f81e0SChristoph Hellwig } 479e980f623SChristoph Hellwig } 480e56f81e0SChristoph Hellwig 481a7cb3d2fSChristoph Hellwig if (!bdev->bd_disk->fops->ioctl) 482a7cb3d2fSChristoph Hellwig r = -ENOTTY; 483a7cb3d2fSChristoph Hellwig else 484a7cb3d2fSChristoph Hellwig r = bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg); 485e56f81e0SChristoph Hellwig out: 486971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 487aa129a22SMilan Broz return r; 488aa129a22SMilan Broz } 489aa129a22SMilan Broz 4907465d7acSMike Snitzer u64 dm_start_time_ns_from_clone(struct bio *bio) 4917465d7acSMike Snitzer { 4926c23f0bdSChristoph Hellwig return jiffies_to_nsecs(clone_to_tio(bio)->io->start_time); 4937465d7acSMike Snitzer } 4947465d7acSMike Snitzer EXPORT_SYMBOL_GPL(dm_start_time_ns_from_clone); 4957465d7acSMike Snitzer 4968d394bc4SMike Snitzer static bool bio_is_flush_with_data(struct bio *bio) 4977465d7acSMike Snitzer { 4988d394bc4SMike Snitzer return ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size); 4997465d7acSMike Snitzer } 5007465d7acSMike Snitzer 5018d394bc4SMike Snitzer static void dm_io_acct(bool end, struct mapped_device *md, struct bio *bio, 502d208b894SJiazi Li unsigned long start_time, struct dm_stats_aux *stats_aux) 5037465d7acSMike Snitzer { 5048d394bc4SMike Snitzer bool is_flush_with_data; 5058d394bc4SMike Snitzer unsigned int bi_size; 5067465d7acSMike Snitzer 5078d394bc4SMike Snitzer /* If REQ_PREFLUSH set save any payload but do not account it */ 5088d394bc4SMike Snitzer is_flush_with_data = bio_is_flush_with_data(bio); 5098d394bc4SMike Snitzer if (is_flush_with_data) { 5108d394bc4SMike Snitzer bi_size = bio->bi_iter.bi_size; 5118d394bc4SMike Snitzer bio->bi_iter.bi_size = 0; 5128d394bc4SMike Snitzer } 5138d394bc4SMike Snitzer 5148d394bc4SMike Snitzer if (!end) 5158d394bc4SMike Snitzer bio_start_io_acct_time(bio, start_time); 5168d394bc4SMike Snitzer else 517d208b894SJiazi Li bio_end_io_acct(bio, start_time); 5187465d7acSMike Snitzer 5197465d7acSMike Snitzer if (unlikely(dm_stats_used(&md->stats))) 5207465d7acSMike Snitzer dm_stats_account_io(&md->stats, bio_data_dir(bio), 5217465d7acSMike Snitzer bio->bi_iter.bi_sector, bio_sectors(bio), 5228d394bc4SMike Snitzer end, start_time, stats_aux); 5237465d7acSMike Snitzer 5248d394bc4SMike Snitzer /* Restore bio's payload so it does get accounted upon requeue */ 5258d394bc4SMike Snitzer if (is_flush_with_data) 5268d394bc4SMike Snitzer bio->bi_iter.bi_size = bi_size; 5278d394bc4SMike Snitzer } 5288d394bc4SMike Snitzer 5290fbb4d93SMike Snitzer static void __dm_start_io_acct(struct dm_io *io, struct bio *bio) 5308d394bc4SMike Snitzer { 5310fbb4d93SMike Snitzer dm_io_acct(false, io->md, bio, io->start_time, &io->stats_aux); 5328d394bc4SMike Snitzer } 5338d394bc4SMike Snitzer 5340fbb4d93SMike Snitzer static void dm_start_io_acct(struct dm_io *io, struct bio *clone) 5358d394bc4SMike Snitzer { 5360fbb4d93SMike Snitzer /* Must account IO to DM device in terms of orig_bio */ 5370fbb4d93SMike Snitzer struct bio *bio = io->orig_bio; 5380fbb4d93SMike Snitzer 5390fbb4d93SMike Snitzer /* 5400fbb4d93SMike Snitzer * Ensure IO accounting is only ever started once. 541655f3aadSMike Snitzer * Expect no possibility for race unless DM_TIO_IS_DUPLICATE_BIO. 5420fbb4d93SMike Snitzer */ 543655f3aadSMike Snitzer if (!clone || 544655f3aadSMike Snitzer likely(!dm_tio_flagged(clone_to_tio(clone), DM_TIO_IS_DUPLICATE_BIO))) { 54582f6cdccSMike Snitzer if (WARN_ON_ONCE(dm_io_flagged(io, DM_IO_ACCOUNTED))) 5460fbb4d93SMike Snitzer return; 54782f6cdccSMike Snitzer dm_io_set_flag(io, DM_IO_ACCOUNTED); 54882f6cdccSMike Snitzer } else { 54982f6cdccSMike Snitzer unsigned long flags; 55082f6cdccSMike Snitzer if (dm_io_flagged(io, DM_IO_ACCOUNTED)) 5510fbb4d93SMike Snitzer return; 552655f3aadSMike Snitzer /* Can afford locking given DM_TIO_IS_DUPLICATE_BIO */ 5534d7bca13SMike Snitzer spin_lock_irqsave(&io->lock, flags); 55482f6cdccSMike Snitzer dm_io_set_flag(io, DM_IO_ACCOUNTED); 5554d7bca13SMike Snitzer spin_unlock_irqrestore(&io->lock, flags); 55682f6cdccSMike Snitzer } 5570fbb4d93SMike Snitzer 5580fbb4d93SMike Snitzer __dm_start_io_acct(io, bio); 5590fbb4d93SMike Snitzer } 5600fbb4d93SMike Snitzer 5610fbb4d93SMike Snitzer static void dm_end_io_acct(struct dm_io *io, struct bio *bio) 5620fbb4d93SMike Snitzer { 5630fbb4d93SMike Snitzer dm_io_acct(true, io->md, bio, io->start_time, &io->stats_aux); 5647465d7acSMike Snitzer } 565978e51baSMike Snitzer 566978e51baSMike Snitzer static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio) 5671da177e4SLinus Torvalds { 56864f52b0eSMike Snitzer struct dm_io *io; 56964f52b0eSMike Snitzer struct dm_target_io *tio; 57064f52b0eSMike Snitzer struct bio *clone; 57164f52b0eSMike Snitzer 572abfc426dSChristoph Hellwig clone = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO, &md->io_bs); 57364f52b0eSMike Snitzer 5746c23f0bdSChristoph Hellwig tio = clone_to_tio(clone); 575655f3aadSMike Snitzer tio->flags = 0; 576655f3aadSMike Snitzer dm_tio_set_flag(tio, DM_TIO_INSIDE_DM_IO); 57764f52b0eSMike Snitzer tio->io = NULL; 57864f52b0eSMike Snitzer 57964f52b0eSMike Snitzer io = container_of(tio, struct dm_io, tio); 58064f52b0eSMike Snitzer io->magic = DM_IO_MAGIC; 581978e51baSMike Snitzer io->status = 0; 582978e51baSMike Snitzer atomic_set(&io->io_count, 1); 5839f6dc633SMike Snitzer this_cpu_inc(*md->pending_io); 5840fbb4d93SMike Snitzer io->orig_bio = NULL; 585978e51baSMike Snitzer io->md = md; 586b7f8dff0SMike Snitzer io->map_task = current; 5874d7bca13SMike Snitzer spin_lock_init(&io->lock); 588b879f915SMike Snitzer io->start_time = jiffies; 58982f6cdccSMike Snitzer io->flags = 0; 59064f52b0eSMike Snitzer 5910cdb90f0SMike Snitzer dm_stats_record_start(&md->stats, &io->stats_aux); 59264f52b0eSMike Snitzer 59364f52b0eSMike Snitzer return io; 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds 5960119ab14SMike Snitzer static void free_io(struct dm_io *io) 5971da177e4SLinus Torvalds { 59864f52b0eSMike Snitzer bio_put(&io->tio.clone); 59964f52b0eSMike Snitzer } 60064f52b0eSMike Snitzer 6011d1068ceSChristoph Hellwig static struct bio *alloc_tio(struct clone_info *ci, struct dm_target *ti, 602dc8e2021SChristoph Hellwig unsigned target_bio_nr, unsigned *len, gfp_t gfp_mask) 60364f52b0eSMike Snitzer { 60464f52b0eSMike Snitzer struct dm_target_io *tio; 605018b05ebSMike Snitzer struct bio *clone; 60664f52b0eSMike Snitzer 60764f52b0eSMike Snitzer if (!ci->io->tio.io) { 60864f52b0eSMike Snitzer /* the dm_target_io embedded in ci->io is available */ 60964f52b0eSMike Snitzer tio = &ci->io->tio; 610018b05ebSMike Snitzer /* alloc_io() already initialized embedded clone */ 611018b05ebSMike Snitzer clone = &tio->clone; 61264f52b0eSMike Snitzer } else { 613018b05ebSMike Snitzer clone = bio_alloc_clone(ci->bio->bi_bdev, ci->bio, 614abfc426dSChristoph Hellwig gfp_mask, &ci->io->md->bs); 61564f52b0eSMike Snitzer if (!clone) 61664f52b0eSMike Snitzer return NULL; 61764f52b0eSMike Snitzer 618b99fdcdcSMing Lei /* REQ_DM_POLL_LIST shouldn't be inherited */ 619b99fdcdcSMing Lei clone->bi_opf &= ~REQ_DM_POLL_LIST; 620b99fdcdcSMing Lei 6216c23f0bdSChristoph Hellwig tio = clone_to_tio(clone); 622655f3aadSMike Snitzer tio->flags = 0; /* also clears DM_TIO_INSIDE_DM_IO */ 62364f52b0eSMike Snitzer } 62464f52b0eSMike Snitzer 62564f52b0eSMike Snitzer tio->magic = DM_TIO_MAGIC; 62664f52b0eSMike Snitzer tio->io = ci->io; 62764f52b0eSMike Snitzer tio->ti = ti; 62864f52b0eSMike Snitzer tio->target_bio_nr = target_bio_nr; 629dc8e2021SChristoph Hellwig tio->len_ptr = len; 630743598f0SMike Snitzer tio->old_sector = 0; 63164f52b0eSMike Snitzer 632018b05ebSMike Snitzer if (len) { 633018b05ebSMike Snitzer clone->bi_iter.bi_size = to_bytes(*len); 634018b05ebSMike Snitzer if (bio_integrity(clone)) 635018b05ebSMike Snitzer bio_integrity_trim(clone); 636018b05ebSMike Snitzer } 637018b05ebSMike Snitzer 638018b05ebSMike Snitzer return clone; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 6411d1068ceSChristoph Hellwig static void free_tio(struct bio *clone) 6421da177e4SLinus Torvalds { 643655f3aadSMike Snitzer if (dm_tio_flagged(clone_to_tio(clone), DM_TIO_INSIDE_DM_IO)) 64464f52b0eSMike Snitzer return; 6451d1068ceSChristoph Hellwig bio_put(clone); 6461da177e4SLinus Torvalds } 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds /* 6491da177e4SLinus Torvalds * Add the bio to the list of deferred io. 6501da177e4SLinus Torvalds */ 65192c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio) 6521da177e4SLinus Torvalds { 65305447420SKiyoshi Ueda unsigned long flags; 6541da177e4SLinus Torvalds 65505447420SKiyoshi Ueda spin_lock_irqsave(&md->deferred_lock, flags); 6561da177e4SLinus Torvalds bio_list_add(&md->deferred, bio); 65705447420SKiyoshi Ueda spin_unlock_irqrestore(&md->deferred_lock, flags); 65892c63902SMikulas Patocka queue_work(md->wq, &md->work); 6591da177e4SLinus Torvalds } 6601da177e4SLinus Torvalds 6611da177e4SLinus Torvalds /* 6621da177e4SLinus Torvalds * Everyone (including functions in this file), should use this 6631da177e4SLinus Torvalds * function to access the md->map field, and make sure they call 66483d5e5b0SMikulas Patocka * dm_put_live_table() when finished. 6651da177e4SLinus Torvalds */ 66683d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier) 6671da177e4SLinus Torvalds { 66883d5e5b0SMikulas Patocka *srcu_idx = srcu_read_lock(&md->io_barrier); 6691da177e4SLinus Torvalds 67083d5e5b0SMikulas Patocka return srcu_dereference(md->map, &md->io_barrier); 67183d5e5b0SMikulas Patocka } 6721da177e4SLinus Torvalds 67383d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier) 67483d5e5b0SMikulas Patocka { 67583d5e5b0SMikulas Patocka srcu_read_unlock(&md->io_barrier, srcu_idx); 67683d5e5b0SMikulas Patocka } 67783d5e5b0SMikulas Patocka 67883d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md) 67983d5e5b0SMikulas Patocka { 68083d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 68183d5e5b0SMikulas Patocka synchronize_rcu_expedited(); 68283d5e5b0SMikulas Patocka } 68383d5e5b0SMikulas Patocka 68483d5e5b0SMikulas Patocka /* 68583d5e5b0SMikulas Patocka * A fast alternative to dm_get_live_table/dm_put_live_table. 68683d5e5b0SMikulas Patocka * The caller must not block between these two functions. 68783d5e5b0SMikulas Patocka */ 68883d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU) 68983d5e5b0SMikulas Patocka { 69083d5e5b0SMikulas Patocka rcu_read_lock(); 69183d5e5b0SMikulas Patocka return rcu_dereference(md->map); 69283d5e5b0SMikulas Patocka } 69383d5e5b0SMikulas Patocka 69483d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU) 69583d5e5b0SMikulas Patocka { 69683d5e5b0SMikulas Patocka rcu_read_unlock(); 6971da177e4SLinus Torvalds } 6981da177e4SLinus Torvalds 699971888c4SMike Snitzer static char *_dm_claim_ptr = "I belong to device-mapper"; 700971888c4SMike Snitzer 7013ac51e74SDarrick J. Wong /* 70286f1152bSBenjamin Marzinski * Open a table device so we can use it as a map destination. 70386f1152bSBenjamin Marzinski */ 70486f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev, 70586f1152bSBenjamin Marzinski struct mapped_device *md) 70686f1152bSBenjamin Marzinski { 70786f1152bSBenjamin Marzinski struct block_device *bdev; 708cd913c76SChristoph Hellwig u64 part_off; 70986f1152bSBenjamin Marzinski int r; 71086f1152bSBenjamin Marzinski 71186f1152bSBenjamin Marzinski BUG_ON(td->dm_dev.bdev); 71286f1152bSBenjamin Marzinski 713519049afSMike Snitzer bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr); 71486f1152bSBenjamin Marzinski if (IS_ERR(bdev)) 71586f1152bSBenjamin Marzinski return PTR_ERR(bdev); 71686f1152bSBenjamin Marzinski 71786f1152bSBenjamin Marzinski r = bd_link_disk_holder(bdev, dm_disk(md)); 71886f1152bSBenjamin Marzinski if (r) { 71986f1152bSBenjamin Marzinski blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL); 72086f1152bSBenjamin Marzinski return r; 72186f1152bSBenjamin Marzinski } 72286f1152bSBenjamin Marzinski 72386f1152bSBenjamin Marzinski td->dm_dev.bdev = bdev; 724cd913c76SChristoph Hellwig td->dm_dev.dax_dev = fs_dax_get_by_bdev(bdev, &part_off); 72586f1152bSBenjamin Marzinski return 0; 72686f1152bSBenjamin Marzinski } 72786f1152bSBenjamin Marzinski 72886f1152bSBenjamin Marzinski /* 72986f1152bSBenjamin Marzinski * Close a table device that we've been using. 73086f1152bSBenjamin Marzinski */ 73186f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md) 73286f1152bSBenjamin Marzinski { 73386f1152bSBenjamin Marzinski if (!td->dm_dev.bdev) 73486f1152bSBenjamin Marzinski return; 73586f1152bSBenjamin Marzinski 73686f1152bSBenjamin Marzinski bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md)); 73786f1152bSBenjamin Marzinski blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL); 738817bf402SDan Williams put_dax(td->dm_dev.dax_dev); 73986f1152bSBenjamin Marzinski td->dm_dev.bdev = NULL; 740817bf402SDan Williams td->dm_dev.dax_dev = NULL; 74186f1152bSBenjamin Marzinski } 74286f1152bSBenjamin Marzinski 74386f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev, 7448454fca4SSheetal Singala fmode_t mode) 7458454fca4SSheetal Singala { 74686f1152bSBenjamin Marzinski struct table_device *td; 74786f1152bSBenjamin Marzinski 74886f1152bSBenjamin Marzinski list_for_each_entry(td, l, list) 74986f1152bSBenjamin Marzinski if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode) 75086f1152bSBenjamin Marzinski return td; 75186f1152bSBenjamin Marzinski 75286f1152bSBenjamin Marzinski return NULL; 75386f1152bSBenjamin Marzinski } 75486f1152bSBenjamin Marzinski 75586f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode, 7568454fca4SSheetal Singala struct dm_dev **result) 7578454fca4SSheetal Singala { 75886f1152bSBenjamin Marzinski int r; 75986f1152bSBenjamin Marzinski struct table_device *td; 76086f1152bSBenjamin Marzinski 76186f1152bSBenjamin Marzinski mutex_lock(&md->table_devices_lock); 76286f1152bSBenjamin Marzinski td = find_table_device(&md->table_devices, dev, mode); 76386f1152bSBenjamin Marzinski if (!td) { 764115485e8SMike Snitzer td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id); 76586f1152bSBenjamin Marzinski if (!td) { 76686f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 76786f1152bSBenjamin Marzinski return -ENOMEM; 76886f1152bSBenjamin Marzinski } 76986f1152bSBenjamin Marzinski 77086f1152bSBenjamin Marzinski td->dm_dev.mode = mode; 77186f1152bSBenjamin Marzinski td->dm_dev.bdev = NULL; 77286f1152bSBenjamin Marzinski 77386f1152bSBenjamin Marzinski if ((r = open_table_device(td, dev, md))) { 77486f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 77586f1152bSBenjamin Marzinski kfree(td); 77686f1152bSBenjamin Marzinski return r; 77786f1152bSBenjamin Marzinski } 77886f1152bSBenjamin Marzinski 77986f1152bSBenjamin Marzinski format_dev_t(td->dm_dev.name, dev); 78086f1152bSBenjamin Marzinski 781b0b4d7c6SElena Reshetova refcount_set(&td->count, 1); 78286f1152bSBenjamin Marzinski list_add(&td->list, &md->table_devices); 783b0b4d7c6SElena Reshetova } else { 784b0b4d7c6SElena Reshetova refcount_inc(&td->count); 78586f1152bSBenjamin Marzinski } 78686f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 78786f1152bSBenjamin Marzinski 78886f1152bSBenjamin Marzinski *result = &td->dm_dev; 78986f1152bSBenjamin Marzinski return 0; 79086f1152bSBenjamin Marzinski } 79186f1152bSBenjamin Marzinski 79286f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d) 79386f1152bSBenjamin Marzinski { 79486f1152bSBenjamin Marzinski struct table_device *td = container_of(d, struct table_device, dm_dev); 79586f1152bSBenjamin Marzinski 79686f1152bSBenjamin Marzinski mutex_lock(&md->table_devices_lock); 797b0b4d7c6SElena Reshetova if (refcount_dec_and_test(&td->count)) { 79886f1152bSBenjamin Marzinski close_table_device(td, md); 79986f1152bSBenjamin Marzinski list_del(&td->list); 80086f1152bSBenjamin Marzinski kfree(td); 80186f1152bSBenjamin Marzinski } 80286f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 80386f1152bSBenjamin Marzinski } 80486f1152bSBenjamin Marzinski 80586f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices) 80686f1152bSBenjamin Marzinski { 80786f1152bSBenjamin Marzinski struct list_head *tmp, *next; 80886f1152bSBenjamin Marzinski 80986f1152bSBenjamin Marzinski list_for_each_safe(tmp, next, devices) { 81086f1152bSBenjamin Marzinski struct table_device *td = list_entry(tmp, struct table_device, list); 81186f1152bSBenjamin Marzinski 81286f1152bSBenjamin Marzinski DMWARN("dm_destroy: %s still exists with %d references", 813b0b4d7c6SElena Reshetova td->dm_dev.name, refcount_read(&td->count)); 81486f1152bSBenjamin Marzinski kfree(td); 81586f1152bSBenjamin Marzinski } 81686f1152bSBenjamin Marzinski } 81786f1152bSBenjamin Marzinski 81886f1152bSBenjamin Marzinski /* 8193ac51e74SDarrick J. Wong * Get the geometry associated with a dm device 8203ac51e74SDarrick J. Wong */ 8213ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) 8223ac51e74SDarrick J. Wong { 8233ac51e74SDarrick J. Wong *geo = md->geometry; 8243ac51e74SDarrick J. Wong 8253ac51e74SDarrick J. Wong return 0; 8263ac51e74SDarrick J. Wong } 8273ac51e74SDarrick J. Wong 8283ac51e74SDarrick J. Wong /* 8293ac51e74SDarrick J. Wong * Set the geometry of a device. 8303ac51e74SDarrick J. Wong */ 8313ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) 8323ac51e74SDarrick J. Wong { 8333ac51e74SDarrick J. Wong sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; 8343ac51e74SDarrick J. Wong 8353ac51e74SDarrick J. Wong if (geo->start > sz) { 8363ac51e74SDarrick J. Wong DMWARN("Start sector is beyond the geometry limits."); 8373ac51e74SDarrick J. Wong return -EINVAL; 8383ac51e74SDarrick J. Wong } 8393ac51e74SDarrick J. Wong 8403ac51e74SDarrick J. Wong md->geometry = *geo; 8413ac51e74SDarrick J. Wong 8423ac51e74SDarrick J. Wong return 0; 8433ac51e74SDarrick J. Wong } 8443ac51e74SDarrick J. Wong 8452e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md) 8462e93ccc1SKiyoshi Ueda { 8472e93ccc1SKiyoshi Ueda return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 8482e93ccc1SKiyoshi Ueda } 8492e93ccc1SKiyoshi Ueda 850e2736347SMike Snitzer static void dm_io_complete(struct dm_io *io) 8511da177e4SLinus Torvalds { 8524e4cbee9SChristoph Hellwig blk_status_t io_error; 853b35f8caaSMilan Broz struct mapped_device *md = io->md; 854e2736347SMike Snitzer struct bio *bio = io->orig_bio; 8552e93ccc1SKiyoshi Ueda 8564e4cbee9SChristoph Hellwig if (io->status == BLK_STS_DM_REQUEUE) { 857e2736347SMike Snitzer unsigned long flags; 8582e93ccc1SKiyoshi Ueda /* 8592e93ccc1SKiyoshi Ueda * Target requested pushing back the I/O. 8602e93ccc1SKiyoshi Ueda */ 861022c2611SMikulas Patocka spin_lock_irqsave(&md->deferred_lock, flags); 862bf14e2b2SDamien Le Moal if (__noflush_suspending(md) && 863bf14e2b2SDamien Le Moal !WARN_ON_ONCE(dm_is_zone_write(md, bio))) { 864745dc570SMike Snitzer /* NOTE early return due to BLK_STS_DM_REQUEUE below */ 865bf14e2b2SDamien Le Moal bio_list_add_head(&md->deferred, bio); 866bf14e2b2SDamien Le Moal } else { 867bf14e2b2SDamien Le Moal /* 868bf14e2b2SDamien Le Moal * noflush suspend was interrupted or this is 869bf14e2b2SDamien Le Moal * a write to a zoned target. 870bf14e2b2SDamien Le Moal */ 8714e4cbee9SChristoph Hellwig io->status = BLK_STS_IOERR; 872bf14e2b2SDamien Le Moal } 873022c2611SMikulas Patocka spin_unlock_irqrestore(&md->deferred_lock, flags); 8742e93ccc1SKiyoshi Ueda } 8752e93ccc1SKiyoshi Ueda 8764e4cbee9SChristoph Hellwig io_error = io->status; 87782f6cdccSMike Snitzer if (dm_io_flagged(io, DM_IO_ACCOUNTED)) 8780fbb4d93SMike Snitzer dm_end_io_acct(io, bio); 8790fbb4d93SMike Snitzer else if (!io_error) { 8800fbb4d93SMike Snitzer /* 8810fbb4d93SMike Snitzer * Must handle target that DM_MAPIO_SUBMITTED only to 8820fbb4d93SMike Snitzer * then bio_endio() rather than dm_submit_bio_remap() 8830fbb4d93SMike Snitzer */ 8840fbb4d93SMike Snitzer __dm_start_io_acct(io, bio); 8850fbb4d93SMike Snitzer dm_end_io_acct(io, bio); 8860fbb4d93SMike Snitzer } 8870119ab14SMike Snitzer free_io(io); 8889f6dc633SMike Snitzer smp_wmb(); 8899f6dc633SMike Snitzer this_cpu_dec(*md->pending_io); 8902056a782SJens Axboe 8919f6dc633SMike Snitzer /* nudge anyone waiting on suspend queue */ 8929f6dc633SMike Snitzer if (unlikely(wq_has_sleeper(&md->wait))) 8939f6dc633SMike Snitzer wake_up(&md->wait); 8941da177e4SLinus Torvalds 89552919840SMing Lei if (io_error == BLK_STS_DM_REQUEUE || io_error == BLK_STS_AGAIN) { 89652919840SMing Lei if (bio->bi_opf & REQ_POLLED) { 897b99fdcdcSMing Lei /* 89852919840SMing Lei * Upper layer won't help us poll split bio (io->orig_bio 89952919840SMing Lei * may only reflect a subset of the pre-split original) 90052919840SMing Lei * so clear REQ_POLLED in case of requeue. 901b99fdcdcSMing Lei */ 902b99fdcdcSMing Lei bio->bi_opf &= ~REQ_POLLED; 90352919840SMing Lei if (io_error == BLK_STS_AGAIN) { 90452919840SMing Lei /* io_uring doesn't handle BLK_STS_AGAIN (yet) */ 90552919840SMing Lei queue_io(md, bio); 90652919840SMing Lei } 90752919840SMing Lei } 9086a8736d1STejun Heo return; 909b99fdcdcSMing Lei } 9106a8736d1STejun Heo 9118d394bc4SMike Snitzer if (bio_is_flush_with_data(bio)) { 912af7e466aSMikulas Patocka /* 9136a8736d1STejun Heo * Preflush done for flush with data, reissue 91428a8f0d3SMike Christie * without REQ_PREFLUSH. 915af7e466aSMikulas Patocka */ 9161eff9d32SJens Axboe bio->bi_opf &= ~REQ_PREFLUSH; 9176a8736d1STejun Heo queue_io(md, bio); 918af7e466aSMikulas Patocka } else { 919b372d360SMike Snitzer /* done with normal IO or empty flush */ 9208dd601faSNeilBrown if (io_error) 9214e4cbee9SChristoph Hellwig bio->bi_status = io_error; 9224246a0b6SChristoph Hellwig bio_endio(bio); 9232e93ccc1SKiyoshi Ueda } 9241da177e4SLinus Torvalds } 925e2736347SMike Snitzer 926655f3aadSMike Snitzer static inline bool dm_tio_is_normal(struct dm_target_io *tio) 927655f3aadSMike Snitzer { 928655f3aadSMike Snitzer return (dm_tio_flagged(tio, DM_TIO_INSIDE_DM_IO) && 929655f3aadSMike Snitzer !dm_tio_flagged(tio, DM_TIO_IS_DUPLICATE_BIO)); 930655f3aadSMike Snitzer } 931655f3aadSMike Snitzer 932e2736347SMike Snitzer /* 933e2736347SMike Snitzer * Decrements the number of outstanding ios that a bio has been 934e2736347SMike Snitzer * cloned into, completing the original io if necc. 935e2736347SMike Snitzer */ 936e2736347SMike Snitzer void dm_io_dec_pending(struct dm_io *io, blk_status_t error) 937e2736347SMike Snitzer { 938e2736347SMike Snitzer /* Push-back supersedes any I/O errors */ 939e2736347SMike Snitzer if (unlikely(error)) { 940e2736347SMike Snitzer unsigned long flags; 9414d7bca13SMike Snitzer spin_lock_irqsave(&io->lock, flags); 942e2736347SMike Snitzer if (!(io->status == BLK_STS_DM_REQUEUE && 943e2736347SMike Snitzer __noflush_suspending(io->md))) 944e2736347SMike Snitzer io->status = error; 9454d7bca13SMike Snitzer spin_unlock_irqrestore(&io->lock, flags); 946e2736347SMike Snitzer } 947e2736347SMike Snitzer 948e2736347SMike Snitzer if (atomic_dec_and_test(&io->io_count)) 949e2736347SMike Snitzer dm_io_complete(io); 950af7e466aSMikulas Patocka } 9511da177e4SLinus Torvalds 952bcb44433SMike Snitzer void disable_discard(struct mapped_device *md) 953bcb44433SMike Snitzer { 954bcb44433SMike Snitzer struct queue_limits *limits = dm_get_queue_limits(md); 955bcb44433SMike Snitzer 956bcb44433SMike Snitzer /* device doesn't really support DISCARD, disable it */ 957bcb44433SMike Snitzer limits->max_discard_sectors = 0; 958bcb44433SMike Snitzer } 959bcb44433SMike Snitzer 960ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md) 961ac62d620SChristoph Hellwig { 962ac62d620SChristoph Hellwig struct queue_limits *limits = dm_get_queue_limits(md); 963ac62d620SChristoph Hellwig 964ac62d620SChristoph Hellwig /* device doesn't really support WRITE ZEROES, disable it */ 965ac62d620SChristoph Hellwig limits->max_write_zeroes_sectors = 0; 966ac62d620SChristoph Hellwig } 967ac62d620SChristoph Hellwig 968a666e5c0SMikulas Patocka static bool swap_bios_limit(struct dm_target *ti, struct bio *bio) 969a666e5c0SMikulas Patocka { 970a666e5c0SMikulas Patocka return unlikely((bio->bi_opf & REQ_SWAP) != 0) && unlikely(ti->limit_swap_bios); 971a666e5c0SMikulas Patocka } 972a666e5c0SMikulas Patocka 9734246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio) 9741da177e4SLinus Torvalds { 9754e4cbee9SChristoph Hellwig blk_status_t error = bio->bi_status; 9766c23f0bdSChristoph Hellwig struct dm_target_io *tio = clone_to_tio(bio); 977b35f8caaSMilan Broz struct dm_io *io = tio->io; 9789faf400fSStefan Bader struct mapped_device *md = tio->io->md; 9791da177e4SLinus Torvalds dm_endio_fn endio = tio->ti->type->end_io; 980309dca30SChristoph Hellwig struct request_queue *q = bio->bi_bdev->bd_disk->queue; 9811da177e4SLinus Torvalds 9829c37de29SMike Snitzer if (unlikely(error == BLK_STS_TARGET)) { 983bcb44433SMike Snitzer if (bio_op(bio) == REQ_OP_DISCARD && 984*70200574SChristoph Hellwig !bdev_max_discard_sectors(bio->bi_bdev)) 985bcb44433SMike Snitzer disable_discard(md); 986bcb44433SMike Snitzer else if (bio_op(bio) == REQ_OP_WRITE_ZEROES && 987309dca30SChristoph Hellwig !q->limits.max_write_zeroes_sectors) 988ac62d620SChristoph Hellwig disable_write_zeroes(md); 989ac62d620SChristoph Hellwig } 9907eee4ae2SMike Snitzer 991bb37d772SDamien Le Moal if (blk_queue_is_zoned(q)) 992bb37d772SDamien Le Moal dm_zone_endio(io, bio); 993415c79e1SJohannes Thumshirn 9941be56909SChristoph Hellwig if (endio) { 9954e4cbee9SChristoph Hellwig int r = endio(tio->ti, bio, &error); 9961be56909SChristoph Hellwig switch (r) { 9971be56909SChristoph Hellwig case DM_ENDIO_REQUEUE: 998bf14e2b2SDamien Le Moal /* 999bf14e2b2SDamien Le Moal * Requeuing writes to a sequential zone of a zoned 1000bf14e2b2SDamien Le Moal * target will break the sequential write pattern: 1001bf14e2b2SDamien Le Moal * fail such IO. 1002bf14e2b2SDamien Le Moal */ 1003bf14e2b2SDamien Le Moal if (WARN_ON_ONCE(dm_is_zone_write(md, bio))) 1004bf14e2b2SDamien Le Moal error = BLK_STS_IOERR; 1005bf14e2b2SDamien Le Moal else 10064e4cbee9SChristoph Hellwig error = BLK_STS_DM_REQUEUE; 1007df561f66SGustavo A. R. Silva fallthrough; 10081be56909SChristoph Hellwig case DM_ENDIO_DONE: 10091be56909SChristoph Hellwig break; 10101be56909SChristoph Hellwig case DM_ENDIO_INCOMPLETE: 10111be56909SChristoph Hellwig /* The target will handle the io */ 10121be56909SChristoph Hellwig return; 10131be56909SChristoph Hellwig default: 10141be56909SChristoph Hellwig DMWARN("unimplemented target endio return value: %d", r); 10151be56909SChristoph Hellwig BUG(); 10161be56909SChristoph Hellwig } 10171be56909SChristoph Hellwig } 10181be56909SChristoph Hellwig 1019a666e5c0SMikulas Patocka if (unlikely(swap_bios_limit(tio->ti, bio))) { 1020a666e5c0SMikulas Patocka struct mapped_device *md = io->md; 1021a666e5c0SMikulas Patocka up(&md->swap_bios_semaphore); 1022a666e5c0SMikulas Patocka } 1023a666e5c0SMikulas Patocka 10241d1068ceSChristoph Hellwig free_tio(bio); 1025e2118b3cSDamien Le Moal dm_io_dec_pending(io, error); 10261da177e4SLinus Torvalds } 10271da177e4SLinus Torvalds 102878d8e58aSMike Snitzer /* 102956a67df7SMike Snitzer * Return maximum size of I/O possible at the supplied sector up to the current 103056a67df7SMike Snitzer * target boundary. 103156a67df7SMike Snitzer */ 10323720281dSMike Snitzer static inline sector_t max_io_len_target_boundary(struct dm_target *ti, 10333720281dSMike Snitzer sector_t target_offset) 10341da177e4SLinus Torvalds { 103556a67df7SMike Snitzer return ti->len - target_offset; 103656a67df7SMike Snitzer } 103756a67df7SMike Snitzer 10383720281dSMike Snitzer static sector_t max_io_len(struct dm_target *ti, sector_t sector) 103956a67df7SMike Snitzer { 10403720281dSMike Snitzer sector_t target_offset = dm_target_offset(ti, sector); 10413720281dSMike Snitzer sector_t len = max_io_len_target_boundary(ti, target_offset); 10425091cdecSMike Snitzer sector_t max_len; 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds /* 10453ee16db3SMike Snitzer * Does the target need to split IO even further? 10463ee16db3SMike Snitzer * - varied (per target) IO splitting is a tenet of DM; this 10473ee16db3SMike Snitzer * explains why stacked chunk_sectors based splitting via 10483ee16db3SMike Snitzer * blk_max_size_offset() isn't possible here. So pass in 10493ee16db3SMike Snitzer * ti->max_io_len to override stacked chunk_sectors. 10501da177e4SLinus Torvalds */ 10513ee16db3SMike Snitzer if (ti->max_io_len) { 105233bd6f06SMike Snitzer max_len = blk_max_size_offset(ti->table->md->queue, 10533ee16db3SMike Snitzer target_offset, ti->max_io_len); 1054542f9038SMike Snitzer if (len > max_len) 1055542f9038SMike Snitzer len = max_len; 10563ee16db3SMike Snitzer } 10571da177e4SLinus Torvalds 10581da177e4SLinus Torvalds return len; 10591da177e4SLinus Torvalds } 10601da177e4SLinus Torvalds 1061542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len) 1062542f9038SMike Snitzer { 1063542f9038SMike Snitzer if (len > UINT_MAX) { 1064542f9038SMike Snitzer DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)", 1065542f9038SMike Snitzer (unsigned long long)len, UINT_MAX); 1066542f9038SMike Snitzer ti->error = "Maximum size of target IO is too large"; 1067542f9038SMike Snitzer return -EINVAL; 1068542f9038SMike Snitzer } 1069542f9038SMike Snitzer 107075ae1936SMikulas Patocka ti->max_io_len = (uint32_t) len; 1071542f9038SMike Snitzer 1072542f9038SMike Snitzer return 0; 1073542f9038SMike Snitzer } 1074542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len); 1075542f9038SMike Snitzer 1076f26c5719SDan Williams static struct dm_target *dm_dax_get_live_target(struct mapped_device *md, 1077f26c5719SDan Williams sector_t sector, int *srcu_idx) 10783d97c829SMike Snitzer __acquires(md->io_barrier) 1079545ed20eSToshi Kani { 1080545ed20eSToshi Kani struct dm_table *map; 1081545ed20eSToshi Kani struct dm_target *ti; 1082545ed20eSToshi Kani 1083f26c5719SDan Williams map = dm_get_live_table(md, srcu_idx); 1084545ed20eSToshi Kani if (!map) 1085f26c5719SDan Williams return NULL; 1086545ed20eSToshi Kani 1087545ed20eSToshi Kani ti = dm_table_find_target(map, sector); 1088123d87d5SMikulas Patocka if (!ti) 1089f26c5719SDan Williams return NULL; 1090f26c5719SDan Williams 1091f26c5719SDan Williams return ti; 1092f26c5719SDan Williams } 1093f26c5719SDan Williams 1094f26c5719SDan Williams static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, 1095f26c5719SDan Williams long nr_pages, void **kaddr, pfn_t *pfn) 1096f26c5719SDan Williams { 1097f26c5719SDan Williams struct mapped_device *md = dax_get_private(dax_dev); 1098f26c5719SDan Williams sector_t sector = pgoff * PAGE_SECTORS; 1099f26c5719SDan Williams struct dm_target *ti; 1100f26c5719SDan Williams long len, ret = -EIO; 1101f26c5719SDan Williams int srcu_idx; 1102f26c5719SDan Williams 1103f26c5719SDan Williams ti = dm_dax_get_live_target(md, sector, &srcu_idx); 1104f26c5719SDan Williams 1105f26c5719SDan Williams if (!ti) 1106545ed20eSToshi Kani goto out; 1107f26c5719SDan Williams if (!ti->type->direct_access) 1108f26c5719SDan Williams goto out; 11093720281dSMike Snitzer len = max_io_len(ti, sector) / PAGE_SECTORS; 1110f26c5719SDan Williams if (len < 1) 1111f26c5719SDan Williams goto out; 1112f26c5719SDan Williams nr_pages = min(len, nr_pages); 1113817bf402SDan Williams ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn); 1114817bf402SDan Williams 1115545ed20eSToshi Kani out: 1116545ed20eSToshi Kani dm_put_live_table(md, srcu_idx); 1117f26c5719SDan Williams 1118f26c5719SDan Williams return ret; 1119545ed20eSToshi Kani } 1120545ed20eSToshi Kani 1121cdf6cdcdSVivek Goyal static int dm_dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff, 1122cdf6cdcdSVivek Goyal size_t nr_pages) 1123cdf6cdcdSVivek Goyal { 1124cdf6cdcdSVivek Goyal struct mapped_device *md = dax_get_private(dax_dev); 1125cdf6cdcdSVivek Goyal sector_t sector = pgoff * PAGE_SECTORS; 1126cdf6cdcdSVivek Goyal struct dm_target *ti; 1127cdf6cdcdSVivek Goyal int ret = -EIO; 1128cdf6cdcdSVivek Goyal int srcu_idx; 1129cdf6cdcdSVivek Goyal 1130cdf6cdcdSVivek Goyal ti = dm_dax_get_live_target(md, sector, &srcu_idx); 1131cdf6cdcdSVivek Goyal 1132cdf6cdcdSVivek Goyal if (!ti) 1133cdf6cdcdSVivek Goyal goto out; 1134cdf6cdcdSVivek Goyal if (WARN_ON(!ti->type->dax_zero_page_range)) { 1135cdf6cdcdSVivek Goyal /* 1136cdf6cdcdSVivek Goyal * ->zero_page_range() is mandatory dax operation. If we are 1137cdf6cdcdSVivek Goyal * here, something is wrong. 1138cdf6cdcdSVivek Goyal */ 1139cdf6cdcdSVivek Goyal goto out; 1140cdf6cdcdSVivek Goyal } 1141cdf6cdcdSVivek Goyal ret = ti->type->dax_zero_page_range(ti, pgoff, nr_pages); 1142cdf6cdcdSVivek Goyal out: 1143cdf6cdcdSVivek Goyal dm_put_live_table(md, srcu_idx); 1144cdf6cdcdSVivek Goyal 1145cdf6cdcdSVivek Goyal return ret; 1146cdf6cdcdSVivek Goyal } 1147cdf6cdcdSVivek Goyal 11481dd40c3eSMikulas Patocka /* 11491dd40c3eSMikulas Patocka * A target may call dm_accept_partial_bio only from the map routine. It is 11506842d264SDamien Le Moal * allowed for all bio types except REQ_PREFLUSH, REQ_OP_ZONE_* zone management 1151e6fc9f62SMike Snitzer * operations, REQ_OP_ZONE_APPEND (zone append writes) and any bio serviced by 1152e6fc9f62SMike Snitzer * __send_duplicate_bios(). 11531dd40c3eSMikulas Patocka * 11541dd40c3eSMikulas Patocka * dm_accept_partial_bio informs the dm that the target only wants to process 11551dd40c3eSMikulas Patocka * additional n_sectors sectors of the bio and the rest of the data should be 11561dd40c3eSMikulas Patocka * sent in a next bio. 11571dd40c3eSMikulas Patocka * 11581dd40c3eSMikulas Patocka * A diagram that explains the arithmetics: 11591dd40c3eSMikulas Patocka * +--------------------+---------------+-------+ 11601dd40c3eSMikulas Patocka * | 1 | 2 | 3 | 11611dd40c3eSMikulas Patocka * +--------------------+---------------+-------+ 11621dd40c3eSMikulas Patocka * 11631dd40c3eSMikulas Patocka * <-------------- *tio->len_ptr ---------------> 11641dd40c3eSMikulas Patocka * <------- bi_size -------> 11651dd40c3eSMikulas Patocka * <-- n_sectors --> 11661dd40c3eSMikulas Patocka * 11671dd40c3eSMikulas Patocka * Region 1 was already iterated over with bio_advance or similar function. 11681dd40c3eSMikulas Patocka * (it may be empty if the target doesn't use bio_advance) 11691dd40c3eSMikulas Patocka * Region 2 is the remaining bio size that the target wants to process. 11701dd40c3eSMikulas Patocka * (it may be empty if region 1 is non-empty, although there is no reason 11711dd40c3eSMikulas Patocka * to make it empty) 11721dd40c3eSMikulas Patocka * The target requires that region 3 is to be sent in the next bio. 11731dd40c3eSMikulas Patocka * 11741dd40c3eSMikulas Patocka * If the target wants to receive multiple copies of the bio (via num_*bios, etc), 11751dd40c3eSMikulas Patocka * the partially processed part (the sum of regions 1+2) must be the same for all 11761dd40c3eSMikulas Patocka * copies of the bio. 11771dd40c3eSMikulas Patocka */ 11781dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors) 11791dd40c3eSMikulas Patocka { 11806c23f0bdSChristoph Hellwig struct dm_target_io *tio = clone_to_tio(bio); 11811dd40c3eSMikulas Patocka unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT; 11826842d264SDamien Le Moal 1183655f3aadSMike Snitzer BUG_ON(dm_tio_flagged(tio, DM_TIO_IS_DUPLICATE_BIO)); 11846842d264SDamien Le Moal BUG_ON(op_is_zone_mgmt(bio_op(bio))); 11856842d264SDamien Le Moal BUG_ON(bio_op(bio) == REQ_OP_ZONE_APPEND); 11861dd40c3eSMikulas Patocka BUG_ON(bi_size > *tio->len_ptr); 11871dd40c3eSMikulas Patocka BUG_ON(n_sectors > bi_size); 11886842d264SDamien Le Moal 11891dd40c3eSMikulas Patocka *tio->len_ptr -= bi_size - n_sectors; 11901dd40c3eSMikulas Patocka bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT; 11911dd40c3eSMikulas Patocka } 11921dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio); 11931dd40c3eSMikulas Patocka 11940fbb4d93SMike Snitzer static inline void __dm_submit_bio_remap(struct bio *clone, 11950fbb4d93SMike Snitzer dev_t dev, sector_t old_sector) 11960fbb4d93SMike Snitzer { 11970fbb4d93SMike Snitzer trace_block_bio_remap(clone, dev, old_sector); 11980fbb4d93SMike Snitzer submit_bio_noacct(clone); 11990fbb4d93SMike Snitzer } 12000fbb4d93SMike Snitzer 12010fbb4d93SMike Snitzer /* 12020fbb4d93SMike Snitzer * @clone: clone bio that DM core passed to target's .map function 12030fbb4d93SMike Snitzer * @tgt_clone: clone of @clone bio that target needs submitted 12040fbb4d93SMike Snitzer * 12050fbb4d93SMike Snitzer * Targets should use this interface to submit bios they take 12060fbb4d93SMike Snitzer * ownership of when returning DM_MAPIO_SUBMITTED. 12070fbb4d93SMike Snitzer * 12080fbb4d93SMike Snitzer * Target should also enable ti->accounts_remapped_io 12090fbb4d93SMike Snitzer */ 1210b7f8dff0SMike Snitzer void dm_submit_bio_remap(struct bio *clone, struct bio *tgt_clone) 12110fbb4d93SMike Snitzer { 12120fbb4d93SMike Snitzer struct dm_target_io *tio = clone_to_tio(clone); 12130fbb4d93SMike Snitzer struct dm_io *io = tio->io; 12140fbb4d93SMike Snitzer 12150a8e9599SMike Snitzer WARN_ON_ONCE(!tio->ti->accounts_remapped_io); 12160a8e9599SMike Snitzer 12170fbb4d93SMike Snitzer /* establish bio that will get submitted */ 12180fbb4d93SMike Snitzer if (!tgt_clone) 12190fbb4d93SMike Snitzer tgt_clone = clone; 12200fbb4d93SMike Snitzer 12210fbb4d93SMike Snitzer /* 12220fbb4d93SMike Snitzer * Account io->origin_bio to DM dev on behalf of target 12230fbb4d93SMike Snitzer * that took ownership of IO with DM_MAPIO_SUBMITTED. 12240fbb4d93SMike Snitzer */ 1225b7f8dff0SMike Snitzer if (io->map_task == current) { 12260fbb4d93SMike Snitzer /* Still in target's map function */ 122782f6cdccSMike Snitzer dm_io_set_flag(io, DM_IO_START_ACCT); 12280fbb4d93SMike Snitzer } else { 12290fbb4d93SMike Snitzer /* 12300fbb4d93SMike Snitzer * Called by another thread, managed by DM target, 12310fbb4d93SMike Snitzer * wait for dm_split_and_process_bio() to store 12320fbb4d93SMike Snitzer * io->orig_bio 12330fbb4d93SMike Snitzer */ 12340fbb4d93SMike Snitzer while (unlikely(!smp_load_acquire(&io->orig_bio))) 12350fbb4d93SMike Snitzer msleep(1); 12360fbb4d93SMike Snitzer dm_start_io_acct(io, clone); 12370fbb4d93SMike Snitzer } 12380fbb4d93SMike Snitzer 12390fbb4d93SMike Snitzer __dm_submit_bio_remap(tgt_clone, disk_devt(io->md->disk), 12400fbb4d93SMike Snitzer tio->old_sector); 12410fbb4d93SMike Snitzer } 12420fbb4d93SMike Snitzer EXPORT_SYMBOL_GPL(dm_submit_bio_remap); 12430fbb4d93SMike Snitzer 1244a666e5c0SMikulas Patocka static noinline void __set_swap_bios_limit(struct mapped_device *md, int latch) 1245a666e5c0SMikulas Patocka { 1246a666e5c0SMikulas Patocka mutex_lock(&md->swap_bios_lock); 1247a666e5c0SMikulas Patocka while (latch < md->swap_bios) { 1248a666e5c0SMikulas Patocka cond_resched(); 1249a666e5c0SMikulas Patocka down(&md->swap_bios_semaphore); 1250a666e5c0SMikulas Patocka md->swap_bios--; 1251a666e5c0SMikulas Patocka } 1252a666e5c0SMikulas Patocka while (latch > md->swap_bios) { 1253a666e5c0SMikulas Patocka cond_resched(); 1254a666e5c0SMikulas Patocka up(&md->swap_bios_semaphore); 1255a666e5c0SMikulas Patocka md->swap_bios++; 1256a666e5c0SMikulas Patocka } 1257a666e5c0SMikulas Patocka mutex_unlock(&md->swap_bios_lock); 1258a666e5c0SMikulas Patocka } 1259a666e5c0SMikulas Patocka 12601561b396SChristoph Hellwig static void __map_bio(struct bio *clone) 12611da177e4SLinus Torvalds { 12621561b396SChristoph Hellwig struct dm_target_io *tio = clone_to_tio(clone); 12631da177e4SLinus Torvalds int r; 126464f52b0eSMike Snitzer struct dm_io *io = tio->io; 1265bd2a49b8SAlasdair G Kergon struct dm_target *ti = tio->ti; 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds clone->bi_end_io = clone_endio; 12681da177e4SLinus Torvalds 12691da177e4SLinus Torvalds /* 12700fbb4d93SMike Snitzer * Map the clone. 12711da177e4SLinus Torvalds */ 1272e2118b3cSDamien Le Moal dm_io_inc_pending(io); 1273743598f0SMike Snitzer tio->old_sector = clone->bi_iter.bi_sector; 1274d67a5f4bSMikulas Patocka 1275a666e5c0SMikulas Patocka if (unlikely(swap_bios_limit(ti, clone))) { 1276a666e5c0SMikulas Patocka struct mapped_device *md = io->md; 1277a666e5c0SMikulas Patocka int latch = get_swap_bios(); 1278a666e5c0SMikulas Patocka if (unlikely(latch != md->swap_bios)) 1279a666e5c0SMikulas Patocka __set_swap_bios_limit(md, latch); 1280a666e5c0SMikulas Patocka down(&md->swap_bios_semaphore); 1281a666e5c0SMikulas Patocka } 1282a666e5c0SMikulas Patocka 1283bb37d772SDamien Le Moal /* 1284bb37d772SDamien Le Moal * Check if the IO needs a special mapping due to zone append emulation 1285bb37d772SDamien Le Moal * on zoned target. In this case, dm_zone_map_bio() calls the target 1286bb37d772SDamien Le Moal * map operation. 1287bb37d772SDamien Le Moal */ 1288bb37d772SDamien Le Moal if (dm_emulate_zone_append(io->md)) 1289bb37d772SDamien Le Moal r = dm_zone_map_bio(tio); 1290bb37d772SDamien Le Moal else 12917de3ee57SMikulas Patocka r = ti->type->map(ti, clone); 1292bb37d772SDamien Le Moal 1293846785e6SChristoph Hellwig switch (r) { 1294846785e6SChristoph Hellwig case DM_MAPIO_SUBMITTED: 12950fbb4d93SMike Snitzer /* target has assumed ownership of this io */ 12960fbb4d93SMike Snitzer if (!ti->accounts_remapped_io) 129782f6cdccSMike Snitzer dm_io_set_flag(io, DM_IO_START_ACCT); 1298846785e6SChristoph Hellwig break; 1299846785e6SChristoph Hellwig case DM_MAPIO_REMAPPED: 13000fbb4d93SMike Snitzer /* 13010fbb4d93SMike Snitzer * the bio has been remapped so dispatch it, but defer 13020fbb4d93SMike Snitzer * dm_start_io_acct() until after possible bio_split(). 13030fbb4d93SMike Snitzer */ 13040fbb4d93SMike Snitzer __dm_submit_bio_remap(clone, disk_devt(io->md->disk), 1305743598f0SMike Snitzer tio->old_sector); 130682f6cdccSMike Snitzer dm_io_set_flag(io, DM_IO_START_ACCT); 1307846785e6SChristoph Hellwig break; 1308846785e6SChristoph Hellwig case DM_MAPIO_KILL: 1309846785e6SChristoph Hellwig case DM_MAPIO_REQUEUE: 131090a2326eSMike Snitzer if (unlikely(swap_bios_limit(ti, clone))) 131190a2326eSMike Snitzer up(&io->md->swap_bios_semaphore); 13121d1068ceSChristoph Hellwig free_tio(clone); 131390a2326eSMike Snitzer if (r == DM_MAPIO_KILL) 131490a2326eSMike Snitzer dm_io_dec_pending(io, BLK_STS_IOERR); 131590a2326eSMike Snitzer else 1316e2118b3cSDamien Le Moal dm_io_dec_pending(io, BLK_STS_DM_REQUEUE); 1317846785e6SChristoph Hellwig break; 1318846785e6SChristoph Hellwig default: 131945cbcd79SKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 132045cbcd79SKiyoshi Ueda BUG(); 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds } 13231da177e4SLinus Torvalds 1324318716ddSMike Snitzer static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci, 13257dd06a25SMike Snitzer struct dm_target *ti, unsigned num_bios) 1326f9ab94ceSMikulas Patocka { 13271d1068ceSChristoph Hellwig struct bio *bio; 1328318716ddSMike Snitzer int try; 1329dba14160SMikulas Patocka 1330318716ddSMike Snitzer for (try = 0; try < 2; try++) { 1331318716ddSMike Snitzer int bio_nr; 1332318716ddSMike Snitzer 1333318716ddSMike Snitzer if (try) 1334bc02cdbeSMike Snitzer mutex_lock(&ci->io->md->table_devices_lock); 1335318716ddSMike Snitzer for (bio_nr = 0; bio_nr < num_bios; bio_nr++) { 13367dd06a25SMike Snitzer bio = alloc_tio(ci, ti, bio_nr, NULL, 1337dc8e2021SChristoph Hellwig try ? GFP_NOIO : GFP_NOWAIT); 13381d1068ceSChristoph Hellwig if (!bio) 1339318716ddSMike Snitzer break; 1340318716ddSMike Snitzer 13411d1068ceSChristoph Hellwig bio_list_add(blist, bio); 1342318716ddSMike Snitzer } 1343318716ddSMike Snitzer if (try) 1344bc02cdbeSMike Snitzer mutex_unlock(&ci->io->md->table_devices_lock); 1345318716ddSMike Snitzer if (bio_nr == num_bios) 1346318716ddSMike Snitzer return; 1347318716ddSMike Snitzer 13486c23f0bdSChristoph Hellwig while ((bio = bio_list_pop(blist))) 13491d1068ceSChristoph Hellwig free_tio(bio); 1350318716ddSMike Snitzer } 1351318716ddSMike Snitzer } 1352f9ab94ceSMikulas Patocka 135314fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti, 13541dd40c3eSMikulas Patocka unsigned num_bios, unsigned *len) 135506a426ceSMike Snitzer { 1356318716ddSMike Snitzer struct bio_list blist = BIO_EMPTY_LIST; 13578eabf5d0SChristoph Hellwig struct bio *clone; 135806a426ceSMike Snitzer 1359891fced6SChristoph Hellwig switch (num_bios) { 1360891fced6SChristoph Hellwig case 0: 1361891fced6SChristoph Hellwig break; 1362891fced6SChristoph Hellwig case 1: 1363891fced6SChristoph Hellwig clone = alloc_tio(ci, ti, 0, len, GFP_NOIO); 1364891fced6SChristoph Hellwig __map_bio(clone); 1365891fced6SChristoph Hellwig break; 1366891fced6SChristoph Hellwig default: 13677dd06a25SMike Snitzer /* dm_accept_partial_bio() is not supported with shared tio->len_ptr */ 13687dd06a25SMike Snitzer alloc_multiple_bios(&blist, ci, ti, num_bios); 13698eabf5d0SChristoph Hellwig while ((clone = bio_list_pop(&blist))) { 1370655f3aadSMike Snitzer dm_tio_set_flag(clone_to_tio(clone), DM_TIO_IS_DUPLICATE_BIO); 13711561b396SChristoph Hellwig __map_bio(clone); 1372f9ab94ceSMikulas Patocka } 1373891fced6SChristoph Hellwig break; 1374318716ddSMike Snitzer } 137506a426ceSMike Snitzer } 137606a426ceSMike Snitzer 1377332f2b1eSMike Snitzer static void __send_empty_flush(struct clone_info *ci) 1378f9ab94ceSMikulas Patocka { 137906a426ceSMike Snitzer unsigned target_nr = 0; 1380f9ab94ceSMikulas Patocka struct dm_target *ti; 1381828678b8SMike Snitzer struct bio flush_bio; 1382828678b8SMike Snitzer 1383828678b8SMike Snitzer /* 1384828678b8SMike Snitzer * Use an on-stack bio for this, it's safe since we don't 1385828678b8SMike Snitzer * need to reference it after submit. It's just used as 1386828678b8SMike Snitzer * the basis for the clone(s). 1387828678b8SMike Snitzer */ 138849add496SChristoph Hellwig bio_init(&flush_bio, ci->io->md->disk->part0, NULL, 0, 138949add496SChristoph Hellwig REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC); 139047d95102SChristoph Hellwig 1391828678b8SMike Snitzer ci->bio = &flush_bio; 1392828678b8SMike Snitzer ci->sector_count = 0; 139392b914e2SShin'ichiro Kawasaki ci->io->tio.clone.bi_iter.bi_size = 0; 1394f9ab94ceSMikulas Patocka 1395f9ab94ceSMikulas Patocka while ((ti = dm_table_get_target(ci->map, target_nr++))) 13961dd40c3eSMikulas Patocka __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL); 1397828678b8SMike Snitzer 1398828678b8SMike Snitzer bio_uninit(ci->bio); 1399f9ab94ceSMikulas Patocka } 1400f9ab94ceSMikulas Patocka 1401e6fc9f62SMike Snitzer static void __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti, 140261697a6aSMike Snitzer unsigned num_bios) 14035ae89a87SMike Snitzer { 140451b86f9aSMichael Lass unsigned len; 14055ae89a87SMike Snitzer 14063720281dSMike Snitzer len = min_t(sector_t, ci->sector_count, 14073720281dSMike Snitzer max_io_len_target_boundary(ti, dm_target_offset(ti, ci->sector))); 140851b86f9aSMichael Lass 14097dd06a25SMike Snitzer __send_duplicate_bios(ci, ti, num_bios, &len); 14107dd06a25SMike Snitzer 1411a79245b3SMike Snitzer ci->sector += len; 14123d7f4562SMike Snitzer ci->sector_count -= len; 14135ae89a87SMike Snitzer } 14145ae89a87SMike Snitzer 1415568c73a3SMike Snitzer static bool is_abnormal_io(struct bio *bio) 1416568c73a3SMike Snitzer { 1417568c73a3SMike Snitzer bool r = false; 1418568c73a3SMike Snitzer 1419568c73a3SMike Snitzer switch (bio_op(bio)) { 1420568c73a3SMike Snitzer case REQ_OP_DISCARD: 1421568c73a3SMike Snitzer case REQ_OP_SECURE_ERASE: 1422568c73a3SMike Snitzer case REQ_OP_WRITE_ZEROES: 1423568c73a3SMike Snitzer r = true; 1424568c73a3SMike Snitzer break; 1425568c73a3SMike Snitzer } 1426568c73a3SMike Snitzer 1427568c73a3SMike Snitzer return r; 1428568c73a3SMike Snitzer } 1429568c73a3SMike Snitzer 14300519c71eSMike Snitzer static bool __process_abnormal_io(struct clone_info *ci, struct dm_target *ti, 14310519c71eSMike Snitzer int *result) 14320519c71eSMike Snitzer { 14339679b5a7SMike Snitzer unsigned num_bios = 0; 14340519c71eSMike Snitzer 1435e6fc9f62SMike Snitzer switch (bio_op(ci->bio)) { 14369679b5a7SMike Snitzer case REQ_OP_DISCARD: 14379679b5a7SMike Snitzer num_bios = ti->num_discard_bios; 14389679b5a7SMike Snitzer break; 14399679b5a7SMike Snitzer case REQ_OP_SECURE_ERASE: 14409679b5a7SMike Snitzer num_bios = ti->num_secure_erase_bios; 14419679b5a7SMike Snitzer break; 14429679b5a7SMike Snitzer case REQ_OP_WRITE_ZEROES: 14439679b5a7SMike Snitzer num_bios = ti->num_write_zeroes_bios; 14449679b5a7SMike Snitzer break; 14459679b5a7SMike Snitzer default: 14460519c71eSMike Snitzer return false; 14479679b5a7SMike Snitzer } 14480519c71eSMike Snitzer 1449e6fc9f62SMike Snitzer /* 1450e6fc9f62SMike Snitzer * Even though the device advertised support for this type of 1451e6fc9f62SMike Snitzer * request, that does not mean every target supports it, and 1452e6fc9f62SMike Snitzer * reconfiguration might also have changed that since the 1453e6fc9f62SMike Snitzer * check was performed. 1454e6fc9f62SMike Snitzer */ 1455e6fc9f62SMike Snitzer if (!num_bios) 1456e6fc9f62SMike Snitzer *result = -EOPNOTSUPP; 1457e6fc9f62SMike Snitzer else { 1458e6fc9f62SMike Snitzer __send_changing_extent_only(ci, ti, num_bios); 1459e6fc9f62SMike Snitzer *result = 0; 1460e6fc9f62SMike Snitzer } 14610519c71eSMike Snitzer return true; 14620519c71eSMike Snitzer } 14630519c71eSMike Snitzer 1464e4c93811SAlasdair G Kergon /* 1465b99fdcdcSMing Lei * Reuse ->bi_private as hlist head for storing all dm_io instances 1466b99fdcdcSMing Lei * associated with this bio, and this bio's bi_private needs to be 1467b99fdcdcSMing Lei * stored in dm_io->data before the reuse. 1468b99fdcdcSMing Lei * 1469b99fdcdcSMing Lei * bio->bi_private is owned by fs or upper layer, so block layer won't 1470b99fdcdcSMing Lei * touch it after splitting. Meantime it won't be changed by anyone after 1471b99fdcdcSMing Lei * bio is submitted. So this reuse is safe. 1472b99fdcdcSMing Lei */ 1473b99fdcdcSMing Lei static inline struct hlist_head *dm_get_bio_hlist_head(struct bio *bio) 1474b99fdcdcSMing Lei { 1475b99fdcdcSMing Lei return (struct hlist_head *)&bio->bi_private; 1476b99fdcdcSMing Lei } 1477b99fdcdcSMing Lei 1478b99fdcdcSMing Lei static void dm_queue_poll_io(struct bio *bio, struct dm_io *io) 1479b99fdcdcSMing Lei { 1480b99fdcdcSMing Lei struct hlist_head *head = dm_get_bio_hlist_head(bio); 1481b99fdcdcSMing Lei 1482b99fdcdcSMing Lei if (!(bio->bi_opf & REQ_DM_POLL_LIST)) { 1483b99fdcdcSMing Lei bio->bi_opf |= REQ_DM_POLL_LIST; 1484b99fdcdcSMing Lei /* 1485b99fdcdcSMing Lei * Save .bi_private into dm_io, so that we can reuse 1486b99fdcdcSMing Lei * .bi_private as hlist head for storing dm_io list 1487b99fdcdcSMing Lei */ 1488b99fdcdcSMing Lei io->data = bio->bi_private; 1489b99fdcdcSMing Lei 1490b99fdcdcSMing Lei INIT_HLIST_HEAD(head); 1491b99fdcdcSMing Lei 1492b99fdcdcSMing Lei /* tell block layer to poll for completion */ 1493b99fdcdcSMing Lei bio->bi_cookie = ~BLK_QC_T_NONE; 1494b99fdcdcSMing Lei } else { 1495b99fdcdcSMing Lei /* 1496b99fdcdcSMing Lei * bio recursed due to split, reuse original poll list, 1497b99fdcdcSMing Lei * and save bio->bi_private too. 1498b99fdcdcSMing Lei */ 1499b99fdcdcSMing Lei io->data = hlist_entry(head->first, struct dm_io, node)->data; 1500b99fdcdcSMing Lei } 1501b99fdcdcSMing Lei 1502b99fdcdcSMing Lei hlist_add_head(&io->node, head); 1503b99fdcdcSMing Lei } 1504b99fdcdcSMing Lei 1505b99fdcdcSMing Lei /* 1506e4c93811SAlasdair G Kergon * Select the correct strategy for processing a non-flush bio. 1507e4c93811SAlasdair G Kergon */ 150896c9865cSMike Snitzer static int __split_and_process_bio(struct clone_info *ci) 1509e4c93811SAlasdair G Kergon { 151066bdaa43SMike Snitzer struct bio *clone; 1511e4c93811SAlasdair G Kergon struct dm_target *ti; 15121c3b13e6SKent Overstreet unsigned len; 1513c80914e8SMike Snitzer int r; 1514e4c93811SAlasdair G Kergon 1515e4c93811SAlasdair G Kergon ti = dm_table_find_target(ci->map, ci->sector); 1516123d87d5SMikulas Patocka if (!ti) 1517e4c93811SAlasdair G Kergon return -EIO; 1518e4c93811SAlasdair G Kergon 1519568c73a3SMike Snitzer if (__process_abnormal_io(ci, ti, &r)) 15200519c71eSMike Snitzer return r; 15213d7f4562SMike Snitzer 1522b99fdcdcSMing Lei /* 1523b99fdcdcSMing Lei * Only support bio polling for normal IO, and the target io is 1524b99fdcdcSMing Lei * exactly inside the dm_io instance (verified in dm_poll_dm_io) 1525b99fdcdcSMing Lei */ 1526b99fdcdcSMing Lei ci->submit_as_polled = ci->bio->bi_opf & REQ_POLLED; 1527e4c93811SAlasdair G Kergon 1528e4c93811SAlasdair G Kergon len = min_t(sector_t, max_io_len(ti, ci->sector), ci->sector_count); 152966bdaa43SMike Snitzer clone = alloc_tio(ci, ti, 0, &len, GFP_NOIO); 153066bdaa43SMike Snitzer __map_bio(clone); 1531e4c93811SAlasdair G Kergon 1532e4c93811SAlasdair G Kergon ci->sector += len; 1533e4c93811SAlasdair G Kergon ci->sector_count -= len; 1534e4c93811SAlasdair G Kergon 1535e4c93811SAlasdair G Kergon return 0; 1536e4c93811SAlasdair G Kergon } 1537e4c93811SAlasdair G Kergon 1538978e51baSMike Snitzer static void init_clone_info(struct clone_info *ci, struct mapped_device *md, 1539978e51baSMike Snitzer struct dm_table *map, struct bio *bio) 1540978e51baSMike Snitzer { 1541978e51baSMike Snitzer ci->map = map; 1542978e51baSMike Snitzer ci->io = alloc_io(md, bio); 1543d41e077aSMike Snitzer ci->bio = bio; 1544b99fdcdcSMing Lei ci->submit_as_polled = false; 1545978e51baSMike Snitzer ci->sector = bio->bi_iter.bi_sector; 1546d41e077aSMike Snitzer ci->sector_count = bio_sectors(bio); 1547d41e077aSMike Snitzer 1548d41e077aSMike Snitzer /* Shouldn't happen but sector_count was being set to 0 so... */ 1549d41e077aSMike Snitzer if (WARN_ON_ONCE(op_is_zone_mgmt(bio_op(bio)) && ci->sector_count)) 1550d41e077aSMike Snitzer ci->sector_count = 0; 1551978e51baSMike Snitzer } 1552978e51baSMike Snitzer 1553e4c93811SAlasdair G Kergon /* 155414fe594dSAlasdair G Kergon * Entry point to split a bio into clones and submit them to the targets. 15551da177e4SLinus Torvalds */ 155696c9865cSMike Snitzer static void dm_split_and_process_bio(struct mapped_device *md, 155783d5e5b0SMikulas Patocka struct dm_table *map, struct bio *bio) 15581da177e4SLinus Torvalds { 15591da177e4SLinus Torvalds struct clone_info ci; 15600fbb4d93SMike Snitzer struct bio *orig_bio = NULL; 1561512875bdSJun'ichi Nomura int error = 0; 15621da177e4SLinus Torvalds 1563978e51baSMike Snitzer init_clone_info(&ci, md, map, bio); 1564bd2a49b8SAlasdair G Kergon 15651eff9d32SJens Axboe if (bio->bi_opf & REQ_PREFLUSH) { 1566332f2b1eSMike Snitzer __send_empty_flush(&ci); 1567e2736347SMike Snitzer /* dm_io_complete submits any data associated with flush */ 1568d41e077aSMike Snitzer goto out; 1569d41e077aSMike Snitzer } 1570d41e077aSMike Snitzer 157196c9865cSMike Snitzer error = __split_and_process_bio(&ci); 1572b7f8dff0SMike Snitzer ci.io->map_task = NULL; 1573d41e077aSMike Snitzer if (error || !ci.sector_count) 1574d41e077aSMike Snitzer goto out; 1575d41e077aSMike Snitzer 157618a25da8SNeilBrown /* 1577d41e077aSMike Snitzer * Remainder must be passed to submit_bio_noacct() so it gets handled 1578d41e077aSMike Snitzer * *after* bios already submitted have been completely processed. 1579d41e077aSMike Snitzer * We take a clone of the original to store in ci.io->orig_bio to be 1580e2736347SMike Snitzer * used by dm_end_io_acct() and for dm_io_complete() to use for 1581d41e077aSMike Snitzer * completion handling. 158218a25da8SNeilBrown */ 15830fbb4d93SMike Snitzer orig_bio = bio_split(bio, bio_sectors(bio) - ci.sector_count, 1584f21c601aSMike Snitzer GFP_NOIO, &md->queue->bio_split); 15850fbb4d93SMike Snitzer bio_chain(orig_bio, bio); 15860fbb4d93SMike Snitzer trace_block_split(orig_bio, bio->bi_iter.bi_sector); 15873e08773cSChristoph Hellwig submit_bio_noacct(bio); 1588d41e077aSMike Snitzer out: 15890fbb4d93SMike Snitzer if (!orig_bio) 15900fbb4d93SMike Snitzer orig_bio = bio; 15910fbb4d93SMike Snitzer smp_store_release(&ci.io->orig_bio, orig_bio); 159282f6cdccSMike Snitzer if (dm_io_flagged(ci.io, DM_IO_START_ACCT)) 15930fbb4d93SMike Snitzer dm_start_io_acct(ci.io, NULL); 15941da177e4SLinus Torvalds 1595b99fdcdcSMing Lei /* 1596b99fdcdcSMing Lei * Drop the extra reference count for non-POLLED bio, and hold one 1597b99fdcdcSMing Lei * reference for POLLED bio, which will be released in dm_poll_bio 1598b99fdcdcSMing Lei * 1599b99fdcdcSMing Lei * Add every dm_io instance into the hlist_head which is stored in 1600b99fdcdcSMing Lei * bio->bi_private, so that dm_poll_bio can poll them all. 1601b99fdcdcSMing Lei */ 1602b99fdcdcSMing Lei if (error || !ci.submit_as_polled) 1603e2118b3cSDamien Le Moal dm_io_dec_pending(ci.io, errno_to_blk_status(error)); 1604b99fdcdcSMing Lei else 1605b99fdcdcSMing Lei dm_queue_poll_io(bio, ci.io); 16061da177e4SLinus Torvalds } 16071da177e4SLinus Torvalds 16083e08773cSChristoph Hellwig static void dm_submit_bio(struct bio *bio) 16091da177e4SLinus Torvalds { 1610309dca30SChristoph Hellwig struct mapped_device *md = bio->bi_bdev->bd_disk->private_data; 161183d5e5b0SMikulas Patocka int srcu_idx; 161283d5e5b0SMikulas Patocka struct dm_table *map; 16131da177e4SLinus Torvalds 161483d5e5b0SMikulas Patocka map = dm_get_live_table(md, &srcu_idx); 16156a8736d1STejun Heo 1616fa247089SMike Snitzer /* If suspended, or map not yet available, queue this IO for later */ 1617fa247089SMike Snitzer if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) || 1618fa247089SMike Snitzer unlikely(!map)) { 16196abc4946SKonstantin Khlebnikov if (bio->bi_opf & REQ_NOWAIT) 16206abc4946SKonstantin Khlebnikov bio_wouldblock_error(bio); 1621b2abdb1bSMike Snitzer else if (bio->bi_opf & REQ_RAHEAD) 16226a8736d1STejun Heo bio_io_error(bio); 1623b2abdb1bSMike Snitzer else 1624b2abdb1bSMike Snitzer queue_io(md, bio); 1625b2abdb1bSMike Snitzer goto out; 16261da177e4SLinus Torvalds } 16271da177e4SLinus Torvalds 1628b2abdb1bSMike Snitzer /* 1629b2abdb1bSMike Snitzer * Use blk_queue_split() for abnormal IO (e.g. discard, writesame, etc) 1630b2abdb1bSMike Snitzer * otherwise associated queue_limits won't be imposed. 1631b2abdb1bSMike Snitzer */ 1632b2abdb1bSMike Snitzer if (is_abnormal_io(bio)) 1633b2abdb1bSMike Snitzer blk_queue_split(&bio); 1634978e51baSMike Snitzer 163596c9865cSMike Snitzer dm_split_and_process_bio(md, map, bio); 1636b2abdb1bSMike Snitzer out: 163783d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 1638978e51baSMike Snitzer } 1639978e51baSMike Snitzer 1640b99fdcdcSMing Lei static bool dm_poll_dm_io(struct dm_io *io, struct io_comp_batch *iob, 1641b99fdcdcSMing Lei unsigned int flags) 1642b99fdcdcSMing Lei { 1643655f3aadSMike Snitzer WARN_ON_ONCE(!dm_tio_is_normal(&io->tio)); 1644b99fdcdcSMing Lei 1645b99fdcdcSMing Lei /* don't poll if the mapped io is done */ 1646b99fdcdcSMing Lei if (atomic_read(&io->io_count) > 1) 1647b99fdcdcSMing Lei bio_poll(&io->tio.clone, iob, flags); 1648b99fdcdcSMing Lei 1649b99fdcdcSMing Lei /* bio_poll holds the last reference */ 1650b99fdcdcSMing Lei return atomic_read(&io->io_count) == 1; 1651b99fdcdcSMing Lei } 1652b99fdcdcSMing Lei 1653b99fdcdcSMing Lei static int dm_poll_bio(struct bio *bio, struct io_comp_batch *iob, 1654b99fdcdcSMing Lei unsigned int flags) 1655b99fdcdcSMing Lei { 1656b99fdcdcSMing Lei struct hlist_head *head = dm_get_bio_hlist_head(bio); 1657b99fdcdcSMing Lei struct hlist_head tmp = HLIST_HEAD_INIT; 1658b99fdcdcSMing Lei struct hlist_node *next; 1659b99fdcdcSMing Lei struct dm_io *io; 1660b99fdcdcSMing Lei 1661b99fdcdcSMing Lei /* Only poll normal bio which was marked as REQ_DM_POLL_LIST */ 1662b99fdcdcSMing Lei if (!(bio->bi_opf & REQ_DM_POLL_LIST)) 1663b99fdcdcSMing Lei return 0; 1664b99fdcdcSMing Lei 1665b99fdcdcSMing Lei WARN_ON_ONCE(hlist_empty(head)); 1666b99fdcdcSMing Lei 1667b99fdcdcSMing Lei hlist_move_list(head, &tmp); 1668b99fdcdcSMing Lei 1669b99fdcdcSMing Lei /* 1670b99fdcdcSMing Lei * Restore .bi_private before possibly completing dm_io. 1671b99fdcdcSMing Lei * 1672b99fdcdcSMing Lei * bio_poll() is only possible once @bio has been completely 1673b99fdcdcSMing Lei * submitted via submit_bio_noacct()'s depth-first submission. 1674b99fdcdcSMing Lei * So there is no dm_queue_poll_io() race associated with 1675b99fdcdcSMing Lei * clearing REQ_DM_POLL_LIST here. 1676b99fdcdcSMing Lei */ 1677b99fdcdcSMing Lei bio->bi_opf &= ~REQ_DM_POLL_LIST; 1678b99fdcdcSMing Lei bio->bi_private = hlist_entry(tmp.first, struct dm_io, node)->data; 1679b99fdcdcSMing Lei 1680b99fdcdcSMing Lei hlist_for_each_entry_safe(io, next, &tmp, node) { 1681b99fdcdcSMing Lei if (dm_poll_dm_io(io, iob, flags)) { 1682b99fdcdcSMing Lei hlist_del_init(&io->node); 1683b99fdcdcSMing Lei /* 1684b99fdcdcSMing Lei * clone_endio() has already occurred, so passing 1685b99fdcdcSMing Lei * error as 0 here doesn't override io->status 1686b99fdcdcSMing Lei */ 1687b99fdcdcSMing Lei dm_io_dec_pending(io, 0); 1688b99fdcdcSMing Lei } 1689b99fdcdcSMing Lei } 1690b99fdcdcSMing Lei 1691b99fdcdcSMing Lei /* Not done? */ 1692b99fdcdcSMing Lei if (!hlist_empty(&tmp)) { 1693b99fdcdcSMing Lei bio->bi_opf |= REQ_DM_POLL_LIST; 1694b99fdcdcSMing Lei /* Reset bio->bi_private to dm_io list head */ 1695b99fdcdcSMing Lei hlist_move_list(&tmp, head); 1696b99fdcdcSMing Lei return 0; 1697b99fdcdcSMing Lei } 1698b99fdcdcSMing Lei return 1; 1699b99fdcdcSMing Lei } 1700b99fdcdcSMing Lei 17011da177e4SLinus Torvalds /*----------------------------------------------------------------- 17021da177e4SLinus Torvalds * An IDR is used to keep track of allocated minor numbers. 17031da177e4SLinus Torvalds *---------------------------------------------------------------*/ 17042b06cfffSAlasdair G Kergon static void free_minor(int minor) 17051da177e4SLinus Torvalds { 1706f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 17071da177e4SLinus Torvalds idr_remove(&_minor_idr, minor); 1708f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 17091da177e4SLinus Torvalds } 17101da177e4SLinus Torvalds 17111da177e4SLinus Torvalds /* 17121da177e4SLinus Torvalds * See if the device with a specific minor # is free. 17131da177e4SLinus Torvalds */ 1714cf13ab8eSFrederik Deweerdt static int specific_minor(int minor) 17151da177e4SLinus Torvalds { 1716c9d76be6STejun Heo int r; 17171da177e4SLinus Torvalds 17181da177e4SLinus Torvalds if (minor >= (1 << MINORBITS)) 17191da177e4SLinus Torvalds return -EINVAL; 17201da177e4SLinus Torvalds 1721c9d76be6STejun Heo idr_preload(GFP_KERNEL); 1722f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 17231da177e4SLinus Torvalds 1724c9d76be6STejun Heo r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT); 17251da177e4SLinus Torvalds 1726f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1727c9d76be6STejun Heo idr_preload_end(); 1728c9d76be6STejun Heo if (r < 0) 1729c9d76be6STejun Heo return r == -ENOSPC ? -EBUSY : r; 1730c9d76be6STejun Heo return 0; 17311da177e4SLinus Torvalds } 17321da177e4SLinus Torvalds 1733cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor) 17341da177e4SLinus Torvalds { 1735c9d76be6STejun Heo int r; 17361da177e4SLinus Torvalds 1737c9d76be6STejun Heo idr_preload(GFP_KERNEL); 1738f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 17391da177e4SLinus Torvalds 1740c9d76be6STejun Heo r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT); 17411da177e4SLinus Torvalds 1742f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1743c9d76be6STejun Heo idr_preload_end(); 1744c9d76be6STejun Heo if (r < 0) 17451da177e4SLinus Torvalds return r; 1746c9d76be6STejun Heo *minor = r; 1747c9d76be6STejun Heo return 0; 17481da177e4SLinus Torvalds } 17491da177e4SLinus Torvalds 175083d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops; 1751681cc5e8SMike Snitzer static const struct block_device_operations dm_rq_blk_dops; 1752f26c5719SDan Williams static const struct dax_operations dm_dax_ops; 17531da177e4SLinus Torvalds 175453d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work); 175553d5914fSMikulas Patocka 1756aa6ce87aSSatya Tangirala #ifdef CONFIG_BLK_INLINE_ENCRYPTION 1757cb77cb5aSEric Biggers static void dm_queue_destroy_crypto_profile(struct request_queue *q) 1758aa6ce87aSSatya Tangirala { 1759cb77cb5aSEric Biggers dm_destroy_crypto_profile(q->crypto_profile); 1760aa6ce87aSSatya Tangirala } 1761aa6ce87aSSatya Tangirala 1762aa6ce87aSSatya Tangirala #else /* CONFIG_BLK_INLINE_ENCRYPTION */ 1763aa6ce87aSSatya Tangirala 1764cb77cb5aSEric Biggers static inline void dm_queue_destroy_crypto_profile(struct request_queue *q) 1765aa6ce87aSSatya Tangirala { 1766aa6ce87aSSatya Tangirala } 1767aa6ce87aSSatya Tangirala #endif /* !CONFIG_BLK_INLINE_ENCRYPTION */ 1768aa6ce87aSSatya Tangirala 17690f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md) 17700f20972fSMike Snitzer { 17710f20972fSMike Snitzer if (md->wq) 17720f20972fSMike Snitzer destroy_workqueue(md->wq); 17736f1c819cSKent Overstreet bioset_exit(&md->bs); 17746f1c819cSKent Overstreet bioset_exit(&md->io_bs); 17750f20972fSMike Snitzer 1776f26c5719SDan Williams if (md->dax_dev) { 1777fb08a190SChristoph Hellwig dax_remove_host(md->disk); 1778f26c5719SDan Williams kill_dax(md->dax_dev); 1779f26c5719SDan Williams put_dax(md->dax_dev); 1780f26c5719SDan Williams md->dax_dev = NULL; 1781f26c5719SDan Williams } 1782f26c5719SDan Williams 1783588b7f5dSKirill Tkhai dm_cleanup_zoned_dev(md); 17840f20972fSMike Snitzer if (md->disk) { 17850f20972fSMike Snitzer spin_lock(&_minor_lock); 17860f20972fSMike Snitzer md->disk->private_data = NULL; 17870f20972fSMike Snitzer spin_unlock(&_minor_lock); 178889f871afSChristoph Hellwig if (dm_get_md_type(md) != DM_TYPE_NONE) { 178989f871afSChristoph Hellwig dm_sysfs_exit(md); 17900f20972fSMike Snitzer del_gendisk(md->disk); 179189f871afSChristoph Hellwig } 1792cb77cb5aSEric Biggers dm_queue_destroy_crypto_profile(md->queue); 179374fe6ba9SChristoph Hellwig blk_cleanup_disk(md->disk); 179474a2b6ecSChristoph Hellwig } 17950f20972fSMike Snitzer 17969f6dc633SMike Snitzer if (md->pending_io) { 17979f6dc633SMike Snitzer free_percpu(md->pending_io); 17989f6dc633SMike Snitzer md->pending_io = NULL; 17999f6dc633SMike Snitzer } 18009f6dc633SMike Snitzer 1801d09960b0STahsin Erdogan cleanup_srcu_struct(&md->io_barrier); 1802d09960b0STahsin Erdogan 1803d5ffebddSMike Snitzer mutex_destroy(&md->suspend_lock); 1804d5ffebddSMike Snitzer mutex_destroy(&md->type_lock); 1805d5ffebddSMike Snitzer mutex_destroy(&md->table_devices_lock); 1806a666e5c0SMikulas Patocka mutex_destroy(&md->swap_bios_lock); 1807d5ffebddSMike Snitzer 18084cc96131SMike Snitzer dm_mq_cleanup_mapped_device(md); 18090f20972fSMike Snitzer } 18100f20972fSMike Snitzer 18111da177e4SLinus Torvalds /* 18121da177e4SLinus Torvalds * Allocate and initialise a blank device with a given minor. 18131da177e4SLinus Torvalds */ 18142b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor) 18151da177e4SLinus Torvalds { 1816115485e8SMike Snitzer int r, numa_node_id = dm_get_numa_node(); 1817115485e8SMike Snitzer struct mapped_device *md; 1818ba61fdd1SJeff Mahoney void *old_md; 18191da177e4SLinus Torvalds 1820856eb091SMikulas Patocka md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id); 18211da177e4SLinus Torvalds if (!md) { 18221da177e4SLinus Torvalds DMWARN("unable to allocate device, out of memory."); 18231da177e4SLinus Torvalds return NULL; 18241da177e4SLinus Torvalds } 18251da177e4SLinus Torvalds 182610da4f79SJeff Mahoney if (!try_module_get(THIS_MODULE)) 18276ed7ade8SMilan Broz goto bad_module_get; 182810da4f79SJeff Mahoney 18291da177e4SLinus Torvalds /* get a minor number for the dev */ 18302b06cfffSAlasdair G Kergon if (minor == DM_ANY_MINOR) 1831cf13ab8eSFrederik Deweerdt r = next_free_minor(&minor); 18322b06cfffSAlasdair G Kergon else 1833cf13ab8eSFrederik Deweerdt r = specific_minor(minor); 18341da177e4SLinus Torvalds if (r < 0) 18356ed7ade8SMilan Broz goto bad_minor; 18361da177e4SLinus Torvalds 183783d5e5b0SMikulas Patocka r = init_srcu_struct(&md->io_barrier); 183883d5e5b0SMikulas Patocka if (r < 0) 183983d5e5b0SMikulas Patocka goto bad_io_barrier; 184083d5e5b0SMikulas Patocka 1841115485e8SMike Snitzer md->numa_node_id = numa_node_id; 1842591ddcfcSMike Snitzer md->init_tio_pdu = false; 1843a5664dadSMike Snitzer md->type = DM_TYPE_NONE; 1844e61290a4SDaniel Walker mutex_init(&md->suspend_lock); 1845a5664dadSMike Snitzer mutex_init(&md->type_lock); 184686f1152bSBenjamin Marzinski mutex_init(&md->table_devices_lock); 1847022c2611SMikulas Patocka spin_lock_init(&md->deferred_lock); 18481da177e4SLinus Torvalds atomic_set(&md->holders, 1); 18495c6bd75dSAlasdair G Kergon atomic_set(&md->open_count, 0); 18501da177e4SLinus Torvalds atomic_set(&md->event_nr, 0); 18517a8c3d3bSMike Anderson atomic_set(&md->uevent_seq, 0); 18527a8c3d3bSMike Anderson INIT_LIST_HEAD(&md->uevent_list); 185386f1152bSBenjamin Marzinski INIT_LIST_HEAD(&md->table_devices); 18547a8c3d3bSMike Anderson spin_lock_init(&md->uevent_lock); 18551da177e4SLinus Torvalds 185647ace7e0SMike Snitzer /* 1857c62b37d9SChristoph Hellwig * default to bio-based until DM table is loaded and md->type 1858c62b37d9SChristoph Hellwig * established. If request-based table is loaded: blk-mq will 1859c62b37d9SChristoph Hellwig * override accordingly. 186047ace7e0SMike Snitzer */ 186174fe6ba9SChristoph Hellwig md->disk = blk_alloc_disk(md->numa_node_id); 18621da177e4SLinus Torvalds if (!md->disk) 18630f20972fSMike Snitzer goto bad; 186474fe6ba9SChristoph Hellwig md->queue = md->disk->queue; 18651da177e4SLinus Torvalds 1866f0b04115SJeff Mahoney init_waitqueue_head(&md->wait); 186753d5914fSMikulas Patocka INIT_WORK(&md->work, dm_wq_work); 1868f0b04115SJeff Mahoney init_waitqueue_head(&md->eventq); 18692995fa78SMikulas Patocka init_completion(&md->kobj_holder.completion); 1870f0b04115SJeff Mahoney 1871a666e5c0SMikulas Patocka md->swap_bios = get_swap_bios(); 1872a666e5c0SMikulas Patocka sema_init(&md->swap_bios_semaphore, md->swap_bios); 1873a666e5c0SMikulas Patocka mutex_init(&md->swap_bios_lock); 1874a666e5c0SMikulas Patocka 18751da177e4SLinus Torvalds md->disk->major = _major; 18761da177e4SLinus Torvalds md->disk->first_minor = minor; 187774fe6ba9SChristoph Hellwig md->disk->minors = 1; 18781ebe2e5fSChristoph Hellwig md->disk->flags |= GENHD_FL_NO_PART; 18791da177e4SLinus Torvalds md->disk->fops = &dm_blk_dops; 18801da177e4SLinus Torvalds md->disk->queue = md->queue; 18811da177e4SLinus Torvalds md->disk->private_data = md; 18821da177e4SLinus Torvalds sprintf(md->disk->disk_name, "dm-%d", minor); 1883f26c5719SDan Williams 18845d2a228bSChristoph Hellwig if (IS_ENABLED(CONFIG_FS_DAX)) { 188530c6828aSChristoph Hellwig md->dax_dev = alloc_dax(md, &dm_dax_ops); 1886d7519392SChristoph Hellwig if (IS_ERR(md->dax_dev)) { 1887d7519392SChristoph Hellwig md->dax_dev = NULL; 1888f26c5719SDan Williams goto bad; 1889976431b0SDan Williams } 18907ac5360cSChristoph Hellwig set_dax_nocache(md->dax_dev); 18917ac5360cSChristoph Hellwig set_dax_nomc(md->dax_dev); 1892fb08a190SChristoph Hellwig if (dax_add_host(md->dax_dev, md->disk)) 1893f26c5719SDan Williams goto bad; 1894f26c5719SDan Williams } 18951da177e4SLinus Torvalds 18967e51f257SMike Anderson format_dev_t(md->name, MKDEV(_major, minor)); 18971da177e4SLinus Torvalds 1898c7c879eeSMichał Mirosław md->wq = alloc_workqueue("kdmflush/%s", WQ_MEM_RECLAIM, 0, md->name); 1899304f3f6aSMilan Broz if (!md->wq) 19000f20972fSMike Snitzer goto bad; 1901304f3f6aSMilan Broz 19029f6dc633SMike Snitzer md->pending_io = alloc_percpu(unsigned long); 19039f6dc633SMike Snitzer if (!md->pending_io) 19049f6dc633SMike Snitzer goto bad; 19059f6dc633SMike Snitzer 1906fd2ed4d2SMikulas Patocka dm_stats_init(&md->stats); 1907fd2ed4d2SMikulas Patocka 1908ba61fdd1SJeff Mahoney /* Populate the mapping, nobody knows we exist yet */ 1909f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 1910ba61fdd1SJeff Mahoney old_md = idr_replace(&_minor_idr, md, minor); 1911f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1912ba61fdd1SJeff Mahoney 1913ba61fdd1SJeff Mahoney BUG_ON(old_md != MINOR_ALLOCED); 1914ba61fdd1SJeff Mahoney 19151da177e4SLinus Torvalds return md; 19161da177e4SLinus Torvalds 19170f20972fSMike Snitzer bad: 19180f20972fSMike Snitzer cleanup_mapped_device(md); 191983d5e5b0SMikulas Patocka bad_io_barrier: 19201da177e4SLinus Torvalds free_minor(minor); 19216ed7ade8SMilan Broz bad_minor: 192210da4f79SJeff Mahoney module_put(THIS_MODULE); 19236ed7ade8SMilan Broz bad_module_get: 1924856eb091SMikulas Patocka kvfree(md); 19251da177e4SLinus Torvalds return NULL; 19261da177e4SLinus Torvalds } 19271da177e4SLinus Torvalds 1928ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md); 1929ae9da83fSJun'ichi Nomura 19301da177e4SLinus Torvalds static void free_dev(struct mapped_device *md) 19311da177e4SLinus Torvalds { 1932f331c029STejun Heo int minor = MINOR(disk_devt(md->disk)); 193363d94e48SJun'ichi Nomura 1934ae9da83fSJun'ichi Nomura unlock_fs(md); 19352eb6e1e3SKeith Busch 19360f20972fSMike Snitzer cleanup_mapped_device(md); 19370f20972fSMike Snitzer 19380f20972fSMike Snitzer free_table_devices(&md->table_devices); 19390f20972fSMike Snitzer dm_stats_cleanup(&md->stats); 194063a4f065SMike Snitzer free_minor(minor); 194163a4f065SMike Snitzer 194210da4f79SJeff Mahoney module_put(THIS_MODULE); 1943856eb091SMikulas Patocka kvfree(md); 19441da177e4SLinus Torvalds } 19451da177e4SLinus Torvalds 19462a2a4c51SJens Axboe static int __bind_mempools(struct mapped_device *md, struct dm_table *t) 1947e6ee8c0bSKiyoshi Ueda { 1948c0820cf5SMikulas Patocka struct dm_md_mempools *p = dm_table_get_md_mempools(t); 19492a2a4c51SJens Axboe int ret = 0; 1950e6ee8c0bSKiyoshi Ueda 1951545ed20eSToshi Kani if (dm_table_bio_based(t)) { 1952c0820cf5SMikulas Patocka /* 195364f52b0eSMike Snitzer * The md may already have mempools that need changing. 195464f52b0eSMike Snitzer * If so, reload bioset because front_pad may have changed 195516245bdcSJun'ichi Nomura * because a different table was loaded. 1956c0820cf5SMikulas Patocka */ 19576f1c819cSKent Overstreet bioset_exit(&md->bs); 19586f1c819cSKent Overstreet bioset_exit(&md->io_bs); 19590776aa0eSMike Snitzer 19606f1c819cSKent Overstreet } else if (bioset_initialized(&md->bs)) { 1961cbc4e3c1SMike Snitzer /* 19624e6e36c3SMike Snitzer * There's no need to reload with request-based dm 19634e6e36c3SMike Snitzer * because the size of front_pad doesn't change. 19644e6e36c3SMike Snitzer * Note for future: If you are to reload bioset, 19654e6e36c3SMike Snitzer * prep-ed requests in the queue may refer 19664e6e36c3SMike Snitzer * to bio from the old bioset, so you must walk 19674e6e36c3SMike Snitzer * through the queue to unprep. 1968cbc4e3c1SMike Snitzer */ 1969cbc4e3c1SMike Snitzer goto out; 1970cbc4e3c1SMike Snitzer } 1971cbc4e3c1SMike Snitzer 19726f1c819cSKent Overstreet BUG_ON(!p || 19736f1c819cSKent Overstreet bioset_initialized(&md->bs) || 19746f1c819cSKent Overstreet bioset_initialized(&md->io_bs)); 1975e6ee8c0bSKiyoshi Ueda 19762a2a4c51SJens Axboe ret = bioset_init_from_src(&md->bs, &p->bs); 19772a2a4c51SJens Axboe if (ret) 19782a2a4c51SJens Axboe goto out; 19792a2a4c51SJens Axboe ret = bioset_init_from_src(&md->io_bs, &p->io_bs); 19802a2a4c51SJens Axboe if (ret) 19812a2a4c51SJens Axboe bioset_exit(&md->bs); 1982e6ee8c0bSKiyoshi Ueda out: 198302233342SMike Snitzer /* mempool bind completed, no longer need any mempools in the table */ 1984e6ee8c0bSKiyoshi Ueda dm_table_free_md_mempools(t); 19852a2a4c51SJens Axboe return ret; 1986e6ee8c0bSKiyoshi Ueda } 1987e6ee8c0bSKiyoshi Ueda 19881da177e4SLinus Torvalds /* 19891da177e4SLinus Torvalds * Bind a table to the device. 19901da177e4SLinus Torvalds */ 19911da177e4SLinus Torvalds static void event_callback(void *context) 19921da177e4SLinus Torvalds { 19937a8c3d3bSMike Anderson unsigned long flags; 19947a8c3d3bSMike Anderson LIST_HEAD(uevents); 19951da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) context; 19961da177e4SLinus Torvalds 19977a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 19987a8c3d3bSMike Anderson list_splice_init(&md->uevent_list, &uevents); 19997a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 20007a8c3d3bSMike Anderson 2001ed9e1982STejun Heo dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); 20027a8c3d3bSMike Anderson 20031da177e4SLinus Torvalds atomic_inc(&md->event_nr); 20041da177e4SLinus Torvalds wake_up(&md->eventq); 200562e08243SMikulas Patocka dm_issue_global_event(); 20061da177e4SLinus Torvalds } 20071da177e4SLinus Torvalds 2008c217649bSMike Snitzer /* 2009042d2a9bSAlasdair G Kergon * Returns old map, which caller must destroy. 2010042d2a9bSAlasdair G Kergon */ 2011042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t, 2012754c5fc7SMike Snitzer struct queue_limits *limits) 20131da177e4SLinus Torvalds { 2014042d2a9bSAlasdair G Kergon struct dm_table *old_map; 20151da177e4SLinus Torvalds sector_t size; 20162a2a4c51SJens Axboe int ret; 20171da177e4SLinus Torvalds 20185a8f1f80SBart Van Assche lockdep_assert_held(&md->suspend_lock); 20195a8f1f80SBart Van Assche 20201da177e4SLinus Torvalds size = dm_table_get_size(t); 20213ac51e74SDarrick J. Wong 20223ac51e74SDarrick J. Wong /* 20233ac51e74SDarrick J. Wong * Wipe any geometry if the size of the table changed. 20243ac51e74SDarrick J. Wong */ 2025fd2ed4d2SMikulas Patocka if (size != dm_get_size(md)) 20263ac51e74SDarrick J. Wong memset(&md->geometry, 0, sizeof(md->geometry)); 20273ac51e74SDarrick J. Wong 20285424a0b8SMikulas Patocka if (!get_capacity(md->disk)) 20295424a0b8SMikulas Patocka set_capacity(md->disk, size); 20305424a0b8SMikulas Patocka else 2031f64d9b2eSChristoph Hellwig set_capacity_and_notify(md->disk, size); 20321da177e4SLinus Torvalds 2033cf222b37SAlasdair G Kergon dm_table_event_callback(t, event_callback, md); 20342ca3310eSAlasdair G Kergon 2035f5b4aee1SMike Snitzer if (dm_table_request_based(t)) { 203616f12266SMike Snitzer /* 20379c37de29SMike Snitzer * Leverage the fact that request-based DM targets are 20389c37de29SMike Snitzer * immutable singletons - used to optimize dm_mq_queue_rq. 203916f12266SMike Snitzer */ 204016f12266SMike Snitzer md->immutable_target = dm_table_get_immutable_target(t); 204116f12266SMike Snitzer } 2042e6ee8c0bSKiyoshi Ueda 20432a2a4c51SJens Axboe ret = __bind_mempools(md, t); 20442a2a4c51SJens Axboe if (ret) { 20452a2a4c51SJens Axboe old_map = ERR_PTR(ret); 20462a2a4c51SJens Axboe goto out; 20472a2a4c51SJens Axboe } 2048e6ee8c0bSKiyoshi Ueda 2049f5b4aee1SMike Snitzer ret = dm_table_set_restrictions(t, md->queue, limits); 2050bb37d772SDamien Le Moal if (ret) { 2051bb37d772SDamien Le Moal old_map = ERR_PTR(ret); 2052bb37d772SDamien Le Moal goto out; 2053bb37d772SDamien Le Moal } 2054bb37d772SDamien Le Moal 2055a12f5d48SEric Dumazet old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 20561d3aa6f6SMike Snitzer rcu_assign_pointer(md->map, (void *)t); 205736a0456fSAlasdair G Kergon md->immutable_target_type = dm_table_get_immutable_target_type(t); 205836a0456fSAlasdair G Kergon 205941abc4e1SHannes Reinecke if (old_map) 206083d5e5b0SMikulas Patocka dm_sync_table(md); 20612a2a4c51SJens Axboe out: 2062042d2a9bSAlasdair G Kergon return old_map; 20631da177e4SLinus Torvalds } 20641da177e4SLinus Torvalds 2065a7940155SAlasdair G Kergon /* 2066a7940155SAlasdair G Kergon * Returns unbound table for the caller to free. 2067a7940155SAlasdair G Kergon */ 2068a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md) 20691da177e4SLinus Torvalds { 2070a12f5d48SEric Dumazet struct dm_table *map = rcu_dereference_protected(md->map, 1); 20711da177e4SLinus Torvalds 20721da177e4SLinus Torvalds if (!map) 2073a7940155SAlasdair G Kergon return NULL; 20741da177e4SLinus Torvalds 20751da177e4SLinus Torvalds dm_table_event_callback(map, NULL, NULL); 20769cdb8520SMonam Agarwal RCU_INIT_POINTER(md->map, NULL); 207783d5e5b0SMikulas Patocka dm_sync_table(md); 2078a7940155SAlasdair G Kergon 2079a7940155SAlasdair G Kergon return map; 20801da177e4SLinus Torvalds } 20811da177e4SLinus Torvalds 20821da177e4SLinus Torvalds /* 20831da177e4SLinus Torvalds * Constructor for a new device. 20841da177e4SLinus Torvalds */ 20852b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result) 20861da177e4SLinus Torvalds { 20871da177e4SLinus Torvalds struct mapped_device *md; 20881da177e4SLinus Torvalds 20892b06cfffSAlasdair G Kergon md = alloc_dev(minor); 20901da177e4SLinus Torvalds if (!md) 20911da177e4SLinus Torvalds return -ENXIO; 20921da177e4SLinus Torvalds 209391ccbbacSTushar Sugandhi dm_ima_reset_data(md); 209491ccbbacSTushar Sugandhi 20951da177e4SLinus Torvalds *result = md; 20961da177e4SLinus Torvalds return 0; 20971da177e4SLinus Torvalds } 20981da177e4SLinus Torvalds 2099a5664dadSMike Snitzer /* 2100a5664dadSMike Snitzer * Functions to manage md->type. 2101a5664dadSMike Snitzer * All are required to hold md->type_lock. 2102a5664dadSMike Snitzer */ 2103a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md) 2104a5664dadSMike Snitzer { 2105a5664dadSMike Snitzer mutex_lock(&md->type_lock); 2106a5664dadSMike Snitzer } 2107a5664dadSMike Snitzer 2108a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md) 2109a5664dadSMike Snitzer { 2110a5664dadSMike Snitzer mutex_unlock(&md->type_lock); 2111a5664dadSMike Snitzer } 2112a5664dadSMike Snitzer 21137e0d574fSBart Van Assche void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type) 2114a5664dadSMike Snitzer { 211500c4fc3bSMike Snitzer BUG_ON(!mutex_is_locked(&md->type_lock)); 2116a5664dadSMike Snitzer md->type = type; 2117a5664dadSMike Snitzer } 2118a5664dadSMike Snitzer 21197e0d574fSBart Van Assche enum dm_queue_mode dm_get_md_type(struct mapped_device *md) 2120a5664dadSMike Snitzer { 2121a5664dadSMike Snitzer return md->type; 2122a5664dadSMike Snitzer } 2123a5664dadSMike Snitzer 212436a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md) 212536a0456fSAlasdair G Kergon { 212636a0456fSAlasdair G Kergon return md->immutable_target_type; 212736a0456fSAlasdair G Kergon } 212836a0456fSAlasdair G Kergon 21294a0b4ddfSMike Snitzer /* 2130f84cb8a4SMike Snitzer * The queue_limits are only valid as long as you have a reference 2131f84cb8a4SMike Snitzer * count on 'md'. 2132f84cb8a4SMike Snitzer */ 2133f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md) 2134f84cb8a4SMike Snitzer { 2135f84cb8a4SMike Snitzer BUG_ON(!atomic_read(&md->holders)); 2136f84cb8a4SMike Snitzer return &md->queue->limits; 2137f84cb8a4SMike Snitzer } 2138f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits); 2139f84cb8a4SMike Snitzer 21404a0b4ddfSMike Snitzer /* 21414a0b4ddfSMike Snitzer * Setup the DM device's queue based on md's type 21424a0b4ddfSMike Snitzer */ 2143591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t) 21444a0b4ddfSMike Snitzer { 2145ba305859SChristoph Hellwig enum dm_queue_mode type = dm_table_get_type(t); 2146c100ec49SMike Snitzer struct queue_limits limits; 2147ba305859SChristoph Hellwig int r; 2148bfebd1cdSMike Snitzer 2149545ed20eSToshi Kani switch (type) { 2150bfebd1cdSMike Snitzer case DM_TYPE_REQUEST_BASED: 2151681cc5e8SMike Snitzer md->disk->fops = &dm_rq_blk_dops; 2152e83068a5SMike Snitzer r = dm_mq_init_request_queue(md, t); 2153bfebd1cdSMike Snitzer if (r) { 2154681cc5e8SMike Snitzer DMERR("Cannot initialize queue for request-based dm mapped device"); 2155bfebd1cdSMike Snitzer return r; 2156bfebd1cdSMike Snitzer } 2157bfebd1cdSMike Snitzer break; 2158bfebd1cdSMike Snitzer case DM_TYPE_BIO_BASED: 2159545ed20eSToshi Kani case DM_TYPE_DAX_BIO_BASED: 2160bfebd1cdSMike Snitzer break; 21617e0d574fSBart Van Assche case DM_TYPE_NONE: 21627e0d574fSBart Van Assche WARN_ON_ONCE(true); 21637e0d574fSBart Van Assche break; 2164ff36ab34SMike Snitzer } 21654a0b4ddfSMike Snitzer 2166c100ec49SMike Snitzer r = dm_calculate_queue_limits(t, &limits); 2167c100ec49SMike Snitzer if (r) { 2168c100ec49SMike Snitzer DMERR("Cannot calculate initial queue limits"); 2169c100ec49SMike Snitzer return r; 2170c100ec49SMike Snitzer } 2171bb37d772SDamien Le Moal r = dm_table_set_restrictions(t, md->queue, &limits); 2172bb37d772SDamien Le Moal if (r) 2173bb37d772SDamien Le Moal return r; 217489f871afSChristoph Hellwig 2175e7089f65SLuis Chamberlain r = add_disk(md->disk); 2176e7089f65SLuis Chamberlain if (r) 2177e7089f65SLuis Chamberlain return r; 217889f871afSChristoph Hellwig 217989f871afSChristoph Hellwig r = dm_sysfs_init(md); 218089f871afSChristoph Hellwig if (r) { 218189f871afSChristoph Hellwig del_gendisk(md->disk); 218289f871afSChristoph Hellwig return r; 218389f871afSChristoph Hellwig } 2184ba305859SChristoph Hellwig md->type = type; 21854a0b4ddfSMike Snitzer return 0; 21864a0b4ddfSMike Snitzer } 21874a0b4ddfSMike Snitzer 21882bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev) 21891da177e4SLinus Torvalds { 21901da177e4SLinus Torvalds struct mapped_device *md; 21911da177e4SLinus Torvalds unsigned minor = MINOR(dev); 21921da177e4SLinus Torvalds 21931da177e4SLinus Torvalds if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 21941da177e4SLinus Torvalds return NULL; 21951da177e4SLinus Torvalds 2196f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 21971da177e4SLinus Torvalds 21981da177e4SLinus Torvalds md = idr_find(&_minor_idr, minor); 219949de5769SMike Snitzer if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) || 220049de5769SMike Snitzer test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { 2201637842cfSDavid Teigland md = NULL; 2202fba9f90eSJeff Mahoney goto out; 2203fba9f90eSJeff Mahoney } 22042bec1f4aSMikulas Patocka dm_get(md); 2205fba9f90eSJeff Mahoney out: 2206f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 22071da177e4SLinus Torvalds 2208637842cfSDavid Teigland return md; 2209637842cfSDavid Teigland } 22103cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md); 2211d229a958SDavid Teigland 22129ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md) 2213637842cfSDavid Teigland { 22149ade92a9SAlasdair G Kergon return md->interface_ptr; 22151da177e4SLinus Torvalds } 22161da177e4SLinus Torvalds 22171da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr) 22181da177e4SLinus Torvalds { 22191da177e4SLinus Torvalds md->interface_ptr = ptr; 22201da177e4SLinus Torvalds } 22211da177e4SLinus Torvalds 22221da177e4SLinus Torvalds void dm_get(struct mapped_device *md) 22231da177e4SLinus Torvalds { 22241da177e4SLinus Torvalds atomic_inc(&md->holders); 22253f77316dSKiyoshi Ueda BUG_ON(test_bit(DMF_FREEING, &md->flags)); 22261da177e4SLinus Torvalds } 22271da177e4SLinus Torvalds 222809ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md) 222909ee96b2SMikulas Patocka { 223009ee96b2SMikulas Patocka spin_lock(&_minor_lock); 223109ee96b2SMikulas Patocka if (test_bit(DMF_FREEING, &md->flags)) { 223209ee96b2SMikulas Patocka spin_unlock(&_minor_lock); 223309ee96b2SMikulas Patocka return -EBUSY; 223409ee96b2SMikulas Patocka } 223509ee96b2SMikulas Patocka dm_get(md); 223609ee96b2SMikulas Patocka spin_unlock(&_minor_lock); 223709ee96b2SMikulas Patocka return 0; 223809ee96b2SMikulas Patocka } 223909ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold); 224009ee96b2SMikulas Patocka 224172d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md) 224272d94861SAlasdair G Kergon { 224372d94861SAlasdair G Kergon return md->name; 224472d94861SAlasdair G Kergon } 224572d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name); 224672d94861SAlasdair G Kergon 22473f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait) 22481da177e4SLinus Torvalds { 22491134e5aeSMike Anderson struct dm_table *map; 225083d5e5b0SMikulas Patocka int srcu_idx; 22511da177e4SLinus Torvalds 22523f77316dSKiyoshi Ueda might_sleep(); 2253fba9f90eSJeff Mahoney 225463a4f065SMike Snitzer spin_lock(&_minor_lock); 22553f77316dSKiyoshi Ueda idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md)))); 2256fba9f90eSJeff Mahoney set_bit(DMF_FREEING, &md->flags); 2257f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 22583f77316dSKiyoshi Ueda 22597a5428dcSChristoph Hellwig blk_mark_disk_dead(md->disk); 22603b785fbcSBart Van Assche 2261ab7c7bb6SMikulas Patocka /* 2262ab7c7bb6SMikulas Patocka * Take suspend_lock so that presuspend and postsuspend methods 2263ab7c7bb6SMikulas Patocka * do not race with internal suspend. 2264ab7c7bb6SMikulas Patocka */ 2265ab7c7bb6SMikulas Patocka mutex_lock(&md->suspend_lock); 22662a708cffSJunichi Nomura map = dm_get_live_table(md, &srcu_idx); 22674f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) { 22681da177e4SLinus Torvalds dm_table_presuspend_targets(map); 2269adc0daadSMikulas Patocka set_bit(DMF_SUSPENDED, &md->flags); 22705df96f2bSMikulas Patocka set_bit(DMF_POST_SUSPENDING, &md->flags); 22711da177e4SLinus Torvalds dm_table_postsuspend_targets(map); 22721da177e4SLinus Torvalds } 227383d5e5b0SMikulas Patocka /* dm_put_live_table must be before msleep, otherwise deadlock is possible */ 227483d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 22752a708cffSJunichi Nomura mutex_unlock(&md->suspend_lock); 227683d5e5b0SMikulas Patocka 22773f77316dSKiyoshi Ueda /* 22783f77316dSKiyoshi Ueda * Rare, but there may be I/O requests still going to complete, 22793f77316dSKiyoshi Ueda * for example. Wait for all references to disappear. 22803f77316dSKiyoshi Ueda * No one should increment the reference count of the mapped_device, 22813f77316dSKiyoshi Ueda * after the mapped_device state becomes DMF_FREEING. 22823f77316dSKiyoshi Ueda */ 22833f77316dSKiyoshi Ueda if (wait) 22843f77316dSKiyoshi Ueda while (atomic_read(&md->holders)) 22853f77316dSKiyoshi Ueda msleep(1); 22863f77316dSKiyoshi Ueda else if (atomic_read(&md->holders)) 22873f77316dSKiyoshi Ueda DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)", 22883f77316dSKiyoshi Ueda dm_device_name(md), atomic_read(&md->holders)); 22893f77316dSKiyoshi Ueda 2290a7940155SAlasdair G Kergon dm_table_destroy(__unbind(md)); 22911da177e4SLinus Torvalds free_dev(md); 22921da177e4SLinus Torvalds } 22933f77316dSKiyoshi Ueda 22943f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md) 22953f77316dSKiyoshi Ueda { 22963f77316dSKiyoshi Ueda __dm_destroy(md, true); 22973f77316dSKiyoshi Ueda } 22983f77316dSKiyoshi Ueda 22993f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md) 23003f77316dSKiyoshi Ueda { 23013f77316dSKiyoshi Ueda __dm_destroy(md, false); 23023f77316dSKiyoshi Ueda } 23033f77316dSKiyoshi Ueda 23043f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md) 23053f77316dSKiyoshi Ueda { 23063f77316dSKiyoshi Ueda atomic_dec(&md->holders); 23071da177e4SLinus Torvalds } 230879eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put); 23091da177e4SLinus Torvalds 23109f6dc633SMike Snitzer static bool dm_in_flight_bios(struct mapped_device *md) 231185067747SMing Lei { 231285067747SMing Lei int cpu; 23139f6dc633SMike Snitzer unsigned long sum = 0; 231485067747SMing Lei 23159f6dc633SMike Snitzer for_each_possible_cpu(cpu) 23169f6dc633SMike Snitzer sum += *per_cpu_ptr(md->pending_io, cpu); 231785067747SMing Lei 231885067747SMing Lei return sum != 0; 231985067747SMing Lei } 232085067747SMing Lei 23212f064a59SPeter Zijlstra static int dm_wait_for_bios_completion(struct mapped_device *md, unsigned int task_state) 232246125c1cSMilan Broz { 232346125c1cSMilan Broz int r = 0; 23249f4c3f87SBart Van Assche DEFINE_WAIT(wait); 232546125c1cSMilan Broz 232685067747SMing Lei while (true) { 23279f4c3f87SBart Van Assche prepare_to_wait(&md->wait, &wait, task_state); 232846125c1cSMilan Broz 23299f6dc633SMike Snitzer if (!dm_in_flight_bios(md)) 233046125c1cSMilan Broz break; 233146125c1cSMilan Broz 2332e3fabdfdSBart Van Assche if (signal_pending_state(task_state, current)) { 233346125c1cSMilan Broz r = -EINTR; 233446125c1cSMilan Broz break; 233546125c1cSMilan Broz } 233646125c1cSMilan Broz 233746125c1cSMilan Broz io_schedule(); 233846125c1cSMilan Broz } 23399f4c3f87SBart Van Assche finish_wait(&md->wait, &wait); 2340b44ebeb0SMikulas Patocka 23419f6dc633SMike Snitzer smp_rmb(); 23429f6dc633SMike Snitzer 234346125c1cSMilan Broz return r; 234446125c1cSMilan Broz } 234546125c1cSMilan Broz 23462f064a59SPeter Zijlstra static int dm_wait_for_completion(struct mapped_device *md, unsigned int task_state) 234785067747SMing Lei { 234885067747SMing Lei int r = 0; 234985067747SMing Lei 235085067747SMing Lei if (!queue_is_mq(md->queue)) 235185067747SMing Lei return dm_wait_for_bios_completion(md, task_state); 235285067747SMing Lei 235385067747SMing Lei while (true) { 235485067747SMing Lei if (!blk_mq_queue_inflight(md->queue)) 235585067747SMing Lei break; 235685067747SMing Lei 235785067747SMing Lei if (signal_pending_state(task_state, current)) { 235885067747SMing Lei r = -EINTR; 235985067747SMing Lei break; 236085067747SMing Lei } 236185067747SMing Lei 236285067747SMing Lei msleep(5); 236385067747SMing Lei } 236485067747SMing Lei 236585067747SMing Lei return r; 236685067747SMing Lei } 236785067747SMing Lei 23681da177e4SLinus Torvalds /* 23691da177e4SLinus Torvalds * Process the deferred bios 23701da177e4SLinus Torvalds */ 2371ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work) 23721da177e4SLinus Torvalds { 23730c2915b8SMike Snitzer struct mapped_device *md = container_of(work, struct mapped_device, work); 23740c2915b8SMike Snitzer struct bio *bio; 2375ef208587SMikulas Patocka 23763b00b203SMikulas Patocka while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 2377022c2611SMikulas Patocka spin_lock_irq(&md->deferred_lock); 23780c2915b8SMike Snitzer bio = bio_list_pop(&md->deferred); 2379022c2611SMikulas Patocka spin_unlock_irq(&md->deferred_lock); 2380022c2611SMikulas Patocka 23810c2915b8SMike Snitzer if (!bio) 2382df12ee99SAlasdair G Kergon break; 238373d410c0SMilan Broz 23840c2915b8SMike Snitzer submit_bio_noacct(bio); 2385e6ee8c0bSKiyoshi Ueda } 23861da177e4SLinus Torvalds } 23871da177e4SLinus Torvalds 23889a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md) 2389304f3f6aSMilan Broz { 23903b00b203SMikulas Patocka clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 23914e857c58SPeter Zijlstra smp_mb__after_atomic(); 239253d5914fSMikulas Patocka queue_work(md->wq, &md->work); 2393304f3f6aSMilan Broz } 2394304f3f6aSMilan Broz 23951da177e4SLinus Torvalds /* 2396042d2a9bSAlasdair G Kergon * Swap in a new table, returning the old one for the caller to destroy. 23971da177e4SLinus Torvalds */ 2398042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table) 23991da177e4SLinus Torvalds { 240087eb5b21SMike Christie struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL); 2401754c5fc7SMike Snitzer struct queue_limits limits; 2402042d2a9bSAlasdair G Kergon int r; 24031da177e4SLinus Torvalds 2404e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 24051da177e4SLinus Torvalds 24061da177e4SLinus Torvalds /* device must be suspended */ 24074f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) 240893c534aeSAlasdair G Kergon goto out; 24091da177e4SLinus Torvalds 24103ae70656SMike Snitzer /* 24113ae70656SMike Snitzer * If the new table has no data devices, retain the existing limits. 24123ae70656SMike Snitzer * This helps multipath with queue_if_no_path if all paths disappear, 24133ae70656SMike Snitzer * then new I/O is queued based on these limits, and then some paths 24143ae70656SMike Snitzer * reappear. 24153ae70656SMike Snitzer */ 24163ae70656SMike Snitzer if (dm_table_has_no_data_devices(table)) { 241783d5e5b0SMikulas Patocka live_map = dm_get_live_table_fast(md); 24183ae70656SMike Snitzer if (live_map) 24193ae70656SMike Snitzer limits = md->queue->limits; 242083d5e5b0SMikulas Patocka dm_put_live_table_fast(md); 24213ae70656SMike Snitzer } 24223ae70656SMike Snitzer 242387eb5b21SMike Christie if (!live_map) { 2424754c5fc7SMike Snitzer r = dm_calculate_queue_limits(table, &limits); 2425042d2a9bSAlasdair G Kergon if (r) { 2426042d2a9bSAlasdair G Kergon map = ERR_PTR(r); 2427754c5fc7SMike Snitzer goto out; 2428042d2a9bSAlasdair G Kergon } 242987eb5b21SMike Christie } 2430754c5fc7SMike Snitzer 2431042d2a9bSAlasdair G Kergon map = __bind(md, table, &limits); 243262e08243SMikulas Patocka dm_issue_global_event(); 24331da177e4SLinus Torvalds 243493c534aeSAlasdair G Kergon out: 2435e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 2436042d2a9bSAlasdair G Kergon return map; 24371da177e4SLinus Torvalds } 24381da177e4SLinus Torvalds 24391da177e4SLinus Torvalds /* 24401da177e4SLinus Torvalds * Functions to lock and unlock any filesystem running on the 24411da177e4SLinus Torvalds * device. 24421da177e4SLinus Torvalds */ 24432ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md) 24441da177e4SLinus Torvalds { 2445e39e2e95SAlasdair G Kergon int r; 24461da177e4SLinus Torvalds 2447040f04bdSChristoph Hellwig WARN_ON(test_bit(DMF_FROZEN, &md->flags)); 2448dfbe03f6SAlasdair G Kergon 2449977115c0SChristoph Hellwig r = freeze_bdev(md->disk->part0); 2450040f04bdSChristoph Hellwig if (!r) 2451aa8d7c2fSAlasdair G Kergon set_bit(DMF_FROZEN, &md->flags); 2452040f04bdSChristoph Hellwig return r; 24531da177e4SLinus Torvalds } 24541da177e4SLinus Torvalds 24552ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md) 24561da177e4SLinus Torvalds { 2457aa8d7c2fSAlasdair G Kergon if (!test_bit(DMF_FROZEN, &md->flags)) 2458aa8d7c2fSAlasdair G Kergon return; 2459977115c0SChristoph Hellwig thaw_bdev(md->disk->part0); 2460aa8d7c2fSAlasdair G Kergon clear_bit(DMF_FROZEN, &md->flags); 24611da177e4SLinus Torvalds } 24621da177e4SLinus Torvalds 24631da177e4SLinus Torvalds /* 2464b48633f8SBart Van Assche * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG 2465b48633f8SBart Van Assche * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE 2466b48633f8SBart Van Assche * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY 2467b48633f8SBart Van Assche * 2468ffcc3936SMike Snitzer * If __dm_suspend returns 0, the device is completely quiescent 2469ffcc3936SMike Snitzer * now. There is no request-processing activity. All new requests 2470ffcc3936SMike Snitzer * are being added to md->deferred list. 2471cec47e3dSKiyoshi Ueda */ 2472ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map, 24732f064a59SPeter Zijlstra unsigned suspend_flags, unsigned int task_state, 2474eaf9a736SMike Snitzer int dmf_suspended_flag) 24751da177e4SLinus Torvalds { 2476ffcc3936SMike Snitzer bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG; 2477ffcc3936SMike Snitzer bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG; 2478ffcc3936SMike Snitzer int r; 2479cf222b37SAlasdair G Kergon 24805a8f1f80SBart Van Assche lockdep_assert_held(&md->suspend_lock); 24815a8f1f80SBart Van Assche 24822e93ccc1SKiyoshi Ueda /* 24832e93ccc1SKiyoshi Ueda * DMF_NOFLUSH_SUSPENDING must be set before presuspend. 24842e93ccc1SKiyoshi Ueda * This flag is cleared before dm_suspend returns. 24852e93ccc1SKiyoshi Ueda */ 24862e93ccc1SKiyoshi Ueda if (noflush) 24872e93ccc1SKiyoshi Ueda set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 248886331f39SBart Van Assche else 2489ac75b09fSMike Snitzer DMDEBUG("%s: suspending with flush", dm_device_name(md)); 24902e93ccc1SKiyoshi Ueda 2491d67ee213SMike Snitzer /* 2492d67ee213SMike Snitzer * This gets reverted if there's an error later and the targets 2493d67ee213SMike Snitzer * provide the .presuspend_undo hook. 2494d67ee213SMike Snitzer */ 24951da177e4SLinus Torvalds dm_table_presuspend_targets(map); 24961da177e4SLinus Torvalds 24972e93ccc1SKiyoshi Ueda /* 24989f518b27SKiyoshi Ueda * Flush I/O to the device. 24999f518b27SKiyoshi Ueda * Any I/O submitted after lock_fs() may not be flushed. 25009f518b27SKiyoshi Ueda * noflush takes precedence over do_lockfs. 25019f518b27SKiyoshi Ueda * (lock_fs() flushes I/Os and waits for them to complete.) 25022e93ccc1SKiyoshi Ueda */ 250332a926daSMikulas Patocka if (!noflush && do_lockfs) { 25042ca3310eSAlasdair G Kergon r = lock_fs(md); 2505d67ee213SMike Snitzer if (r) { 2506d67ee213SMike Snitzer dm_table_presuspend_undo_targets(map); 2507ffcc3936SMike Snitzer return r; 2508aa8d7c2fSAlasdair G Kergon } 2509d67ee213SMike Snitzer } 25101da177e4SLinus Torvalds 25111da177e4SLinus Torvalds /* 25123b00b203SMikulas Patocka * Here we must make sure that no processes are submitting requests 25133b00b203SMikulas Patocka * to target drivers i.e. no one may be executing 251496c9865cSMike Snitzer * dm_split_and_process_bio from dm_submit_bio. 25153b00b203SMikulas Patocka * 251696c9865cSMike Snitzer * To get all processes out of dm_split_and_process_bio in dm_submit_bio, 25173b00b203SMikulas Patocka * we take the write lock. To prevent any process from reentering 251896c9865cSMike Snitzer * dm_split_and_process_bio from dm_submit_bio and quiesce the thread 25190cede372SMike Snitzer * (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND and call 25206a8736d1STejun Heo * flush_workqueue(md->wq). 25211da177e4SLinus Torvalds */ 25221eb787ecSAlasdair G Kergon set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 252341abc4e1SHannes Reinecke if (map) 252483d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 25251da177e4SLinus Torvalds 2526d0bcb878SKiyoshi Ueda /* 252729e4013dSTejun Heo * Stop md->queue before flushing md->wq in case request-based 252829e4013dSTejun Heo * dm defers requests to md->wq from md->queue. 2529d0bcb878SKiyoshi Ueda */ 25306a23e05cSJens Axboe if (dm_request_based(md)) 2531eca7ee6dSMike Snitzer dm_stop_queue(md->queue); 2532cec47e3dSKiyoshi Ueda 2533d0bcb878SKiyoshi Ueda flush_workqueue(md->wq); 2534d0bcb878SKiyoshi Ueda 25351da177e4SLinus Torvalds /* 25363b00b203SMikulas Patocka * At this point no more requests are entering target request routines. 25373b00b203SMikulas Patocka * We call dm_wait_for_completion to wait for all existing requests 25383b00b203SMikulas Patocka * to finish. 25391da177e4SLinus Torvalds */ 2540b48633f8SBart Van Assche r = dm_wait_for_completion(md, task_state); 2541eaf9a736SMike Snitzer if (!r) 2542eaf9a736SMike Snitzer set_bit(dmf_suspended_flag, &md->flags); 25431da177e4SLinus Torvalds 25446d6f10dfSMilan Broz if (noflush) 2545022c2611SMikulas Patocka clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 254641abc4e1SHannes Reinecke if (map) 254783d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 25482e93ccc1SKiyoshi Ueda 25491da177e4SLinus Torvalds /* were we interrupted ? */ 255046125c1cSMilan Broz if (r < 0) { 25519a1fb464SMikulas Patocka dm_queue_flush(md); 255273d410c0SMilan Broz 2553cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 2554eca7ee6dSMike Snitzer dm_start_queue(md->queue); 2555cec47e3dSKiyoshi Ueda 25562ca3310eSAlasdair G Kergon unlock_fs(md); 2557d67ee213SMike Snitzer dm_table_presuspend_undo_targets(map); 2558ffcc3936SMike Snitzer /* pushback list is already flushed, so skip flush */ 2559ffcc3936SMike Snitzer } 2560ffcc3936SMike Snitzer 2561ffcc3936SMike Snitzer return r; 25622ca3310eSAlasdair G Kergon } 25632ca3310eSAlasdair G Kergon 25643b00b203SMikulas Patocka /* 2565ffcc3936SMike Snitzer * We need to be able to change a mapping table under a mounted 2566ffcc3936SMike Snitzer * filesystem. For example we might want to move some data in 2567ffcc3936SMike Snitzer * the background. Before the table can be swapped with 2568ffcc3936SMike Snitzer * dm_bind_table, dm_suspend must be called to flush any in 2569ffcc3936SMike Snitzer * flight bios and ensure that any further io gets deferred. 25703b00b203SMikulas Patocka */ 2571ffcc3936SMike Snitzer /* 2572ffcc3936SMike Snitzer * Suspend mechanism in request-based dm. 2573ffcc3936SMike Snitzer * 2574ffcc3936SMike Snitzer * 1. Flush all I/Os by lock_fs() if needed. 2575ffcc3936SMike Snitzer * 2. Stop dispatching any I/O by stopping the request_queue. 2576ffcc3936SMike Snitzer * 3. Wait for all in-flight I/Os to be completed or requeued. 2577ffcc3936SMike Snitzer * 2578ffcc3936SMike Snitzer * To abort suspend, start the request_queue. 2579ffcc3936SMike Snitzer */ 2580ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags) 2581ffcc3936SMike Snitzer { 2582ffcc3936SMike Snitzer struct dm_table *map = NULL; 2583ffcc3936SMike Snitzer int r = 0; 2584ffcc3936SMike Snitzer 2585ffcc3936SMike Snitzer retry: 2586ffcc3936SMike Snitzer mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); 2587ffcc3936SMike Snitzer 2588ffcc3936SMike Snitzer if (dm_suspended_md(md)) { 2589ffcc3936SMike Snitzer r = -EINVAL; 2590ffcc3936SMike Snitzer goto out_unlock; 2591ffcc3936SMike Snitzer } 2592ffcc3936SMike Snitzer 2593ffcc3936SMike Snitzer if (dm_suspended_internally_md(md)) { 2594ffcc3936SMike Snitzer /* already internally suspended, wait for internal resume */ 2595ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2596ffcc3936SMike Snitzer r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); 2597ffcc3936SMike Snitzer if (r) 2598ffcc3936SMike Snitzer return r; 2599ffcc3936SMike Snitzer goto retry; 2600ffcc3936SMike Snitzer } 2601ffcc3936SMike Snitzer 2602a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2603ffcc3936SMike Snitzer 2604eaf9a736SMike Snitzer r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED); 2605ffcc3936SMike Snitzer if (r) 2606ffcc3936SMike Snitzer goto out_unlock; 26073b00b203SMikulas Patocka 26085df96f2bSMikulas Patocka set_bit(DMF_POST_SUSPENDING, &md->flags); 26094d4471cbSKiyoshi Ueda dm_table_postsuspend_targets(map); 26105df96f2bSMikulas Patocka clear_bit(DMF_POST_SUSPENDING, &md->flags); 26114d4471cbSKiyoshi Ueda 2612d287483dSAlasdair G Kergon out_unlock: 2613e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 2614cf222b37SAlasdair G Kergon return r; 26151da177e4SLinus Torvalds } 26161da177e4SLinus Torvalds 2617ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map) 26181da177e4SLinus Torvalds { 2619ffcc3936SMike Snitzer if (map) { 2620ffcc3936SMike Snitzer int r = dm_table_resume_targets(map); 26218757b776SMilan Broz if (r) 2622ffcc3936SMike Snitzer return r; 2623ffcc3936SMike Snitzer } 26242ca3310eSAlasdair G Kergon 26259a1fb464SMikulas Patocka dm_queue_flush(md); 26262ca3310eSAlasdair G Kergon 2627cec47e3dSKiyoshi Ueda /* 2628cec47e3dSKiyoshi Ueda * Flushing deferred I/Os must be done after targets are resumed 2629cec47e3dSKiyoshi Ueda * so that mapping of targets can work correctly. 2630cec47e3dSKiyoshi Ueda * Request-based dm is queueing the deferred I/Os in its request_queue. 2631cec47e3dSKiyoshi Ueda */ 2632cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 2633eca7ee6dSMike Snitzer dm_start_queue(md->queue); 2634cec47e3dSKiyoshi Ueda 26352ca3310eSAlasdair G Kergon unlock_fs(md); 26362ca3310eSAlasdair G Kergon 2637ffcc3936SMike Snitzer return 0; 2638ffcc3936SMike Snitzer } 2639ffcc3936SMike Snitzer 2640ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md) 2641ffcc3936SMike Snitzer { 26428dc23658SMinfei Huang int r; 2643ffcc3936SMike Snitzer struct dm_table *map = NULL; 2644ffcc3936SMike Snitzer 2645ffcc3936SMike Snitzer retry: 26468dc23658SMinfei Huang r = -EINVAL; 2647ffcc3936SMike Snitzer mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); 2648ffcc3936SMike Snitzer 2649ffcc3936SMike Snitzer if (!dm_suspended_md(md)) 2650ffcc3936SMike Snitzer goto out; 2651ffcc3936SMike Snitzer 2652ffcc3936SMike Snitzer if (dm_suspended_internally_md(md)) { 2653ffcc3936SMike Snitzer /* already internally suspended, wait for internal resume */ 2654ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2655ffcc3936SMike Snitzer r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); 2656ffcc3936SMike Snitzer if (r) 2657ffcc3936SMike Snitzer return r; 2658ffcc3936SMike Snitzer goto retry; 2659ffcc3936SMike Snitzer } 2660ffcc3936SMike Snitzer 2661a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2662ffcc3936SMike Snitzer if (!map || !dm_table_get_size(map)) 2663ffcc3936SMike Snitzer goto out; 2664ffcc3936SMike Snitzer 2665ffcc3936SMike Snitzer r = __dm_resume(md, map); 2666ffcc3936SMike Snitzer if (r) 2667ffcc3936SMike Snitzer goto out; 2668ffcc3936SMike Snitzer 26692ca3310eSAlasdair G Kergon clear_bit(DMF_SUSPENDED, &md->flags); 2670cf222b37SAlasdair G Kergon out: 2671e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 26722ca3310eSAlasdair G Kergon 2673cf222b37SAlasdair G Kergon return r; 26741da177e4SLinus Torvalds } 26751da177e4SLinus Torvalds 2676fd2ed4d2SMikulas Patocka /* 2677fd2ed4d2SMikulas Patocka * Internal suspend/resume works like userspace-driven suspend. It waits 2678fd2ed4d2SMikulas Patocka * until all bios finish and prevents issuing new bios to the target drivers. 2679fd2ed4d2SMikulas Patocka * It may be used only from the kernel. 2680fd2ed4d2SMikulas Patocka */ 2681fd2ed4d2SMikulas Patocka 2682ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags) 2683ffcc3936SMike Snitzer { 2684ffcc3936SMike Snitzer struct dm_table *map = NULL; 2685ffcc3936SMike Snitzer 26861ea0654eSBart Van Assche lockdep_assert_held(&md->suspend_lock); 26871ea0654eSBart Van Assche 268896b26c8cSMikulas Patocka if (md->internal_suspend_count++) 2689ffcc3936SMike Snitzer return; /* nested internal suspend */ 2690ffcc3936SMike Snitzer 2691ffcc3936SMike Snitzer if (dm_suspended_md(md)) { 2692ffcc3936SMike Snitzer set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2693ffcc3936SMike Snitzer return; /* nest suspend */ 2694ffcc3936SMike Snitzer } 2695ffcc3936SMike Snitzer 2696a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2697ffcc3936SMike Snitzer 2698ffcc3936SMike Snitzer /* 2699ffcc3936SMike Snitzer * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is 2700ffcc3936SMike Snitzer * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend 2701ffcc3936SMike Snitzer * would require changing .presuspend to return an error -- avoid this 2702ffcc3936SMike Snitzer * until there is a need for more elaborate variants of internal suspend. 2703ffcc3936SMike Snitzer */ 2704eaf9a736SMike Snitzer (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE, 2705eaf9a736SMike Snitzer DMF_SUSPENDED_INTERNALLY); 2706ffcc3936SMike Snitzer 27075df96f2bSMikulas Patocka set_bit(DMF_POST_SUSPENDING, &md->flags); 2708ffcc3936SMike Snitzer dm_table_postsuspend_targets(map); 27095df96f2bSMikulas Patocka clear_bit(DMF_POST_SUSPENDING, &md->flags); 2710ffcc3936SMike Snitzer } 2711ffcc3936SMike Snitzer 2712ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md) 2713ffcc3936SMike Snitzer { 271496b26c8cSMikulas Patocka BUG_ON(!md->internal_suspend_count); 271596b26c8cSMikulas Patocka 271696b26c8cSMikulas Patocka if (--md->internal_suspend_count) 2717ffcc3936SMike Snitzer return; /* resume from nested internal suspend */ 2718ffcc3936SMike Snitzer 2719ffcc3936SMike Snitzer if (dm_suspended_md(md)) 2720ffcc3936SMike Snitzer goto done; /* resume from nested suspend */ 2721ffcc3936SMike Snitzer 2722ffcc3936SMike Snitzer /* 2723ffcc3936SMike Snitzer * NOTE: existing callers don't need to call dm_table_resume_targets 2724ffcc3936SMike Snitzer * (which may fail -- so best to avoid it for now by passing NULL map) 2725ffcc3936SMike Snitzer */ 2726ffcc3936SMike Snitzer (void) __dm_resume(md, NULL); 2727ffcc3936SMike Snitzer 2728ffcc3936SMike Snitzer done: 2729ffcc3936SMike Snitzer clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2730ffcc3936SMike Snitzer smp_mb__after_atomic(); 2731ffcc3936SMike Snitzer wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY); 2732ffcc3936SMike Snitzer } 2733ffcc3936SMike Snitzer 2734ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md) 2735fd2ed4d2SMikulas Patocka { 2736fd2ed4d2SMikulas Patocka mutex_lock(&md->suspend_lock); 2737ffcc3936SMike Snitzer __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG); 2738ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2739ffcc3936SMike Snitzer } 2740ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush); 2741ffcc3936SMike Snitzer 2742ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md) 2743ffcc3936SMike Snitzer { 2744ffcc3936SMike Snitzer mutex_lock(&md->suspend_lock); 2745ffcc3936SMike Snitzer __dm_internal_resume(md); 2746ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2747ffcc3936SMike Snitzer } 2748ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume); 2749ffcc3936SMike Snitzer 2750ffcc3936SMike Snitzer /* 2751ffcc3936SMike Snitzer * Fast variants of internal suspend/resume hold md->suspend_lock, 2752ffcc3936SMike Snitzer * which prevents interaction with userspace-driven suspend. 2753ffcc3936SMike Snitzer */ 2754ffcc3936SMike Snitzer 2755ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md) 2756ffcc3936SMike Snitzer { 2757ffcc3936SMike Snitzer mutex_lock(&md->suspend_lock); 2758ffcc3936SMike Snitzer if (dm_suspended_md(md) || dm_suspended_internally_md(md)) 2759fd2ed4d2SMikulas Patocka return; 2760fd2ed4d2SMikulas Patocka 2761fd2ed4d2SMikulas Patocka set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 2762fd2ed4d2SMikulas Patocka synchronize_srcu(&md->io_barrier); 2763fd2ed4d2SMikulas Patocka flush_workqueue(md->wq); 2764fd2ed4d2SMikulas Patocka dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); 2765fd2ed4d2SMikulas Patocka } 2766b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast); 2767fd2ed4d2SMikulas Patocka 2768ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md) 2769fd2ed4d2SMikulas Patocka { 2770ffcc3936SMike Snitzer if (dm_suspended_md(md) || dm_suspended_internally_md(md)) 2771fd2ed4d2SMikulas Patocka goto done; 2772fd2ed4d2SMikulas Patocka 2773fd2ed4d2SMikulas Patocka dm_queue_flush(md); 2774fd2ed4d2SMikulas Patocka 2775fd2ed4d2SMikulas Patocka done: 2776fd2ed4d2SMikulas Patocka mutex_unlock(&md->suspend_lock); 2777fd2ed4d2SMikulas Patocka } 2778b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast); 2779fd2ed4d2SMikulas Patocka 27801da177e4SLinus Torvalds /*----------------------------------------------------------------- 27811da177e4SLinus Torvalds * Event notification. 27821da177e4SLinus Torvalds *---------------------------------------------------------------*/ 27833abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action, 278460935eb2SMilan Broz unsigned cookie) 278569267a30SAlasdair G Kergon { 27866958c1c6SMikulas Patocka int r; 27876958c1c6SMikulas Patocka unsigned noio_flag; 278860935eb2SMilan Broz char udev_cookie[DM_COOKIE_LENGTH]; 278960935eb2SMilan Broz char *envp[] = { udev_cookie, NULL }; 279060935eb2SMilan Broz 27916958c1c6SMikulas Patocka noio_flag = memalloc_noio_save(); 27926958c1c6SMikulas Patocka 279360935eb2SMilan Broz if (!cookie) 27946958c1c6SMikulas Patocka r = kobject_uevent(&disk_to_dev(md->disk)->kobj, action); 279560935eb2SMilan Broz else { 279660935eb2SMilan Broz snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u", 279760935eb2SMilan Broz DM_COOKIE_ENV_VAR_NAME, cookie); 27986958c1c6SMikulas Patocka r = kobject_uevent_env(&disk_to_dev(md->disk)->kobj, 27993abf85b5SPeter Rajnoha action, envp); 280060935eb2SMilan Broz } 28016958c1c6SMikulas Patocka 28026958c1c6SMikulas Patocka memalloc_noio_restore(noio_flag); 28036958c1c6SMikulas Patocka 28046958c1c6SMikulas Patocka return r; 280569267a30SAlasdair G Kergon } 280669267a30SAlasdair G Kergon 28077a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md) 28087a8c3d3bSMike Anderson { 28097a8c3d3bSMike Anderson return atomic_add_return(1, &md->uevent_seq); 28107a8c3d3bSMike Anderson } 28117a8c3d3bSMike Anderson 28121da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md) 28131da177e4SLinus Torvalds { 28141da177e4SLinus Torvalds return atomic_read(&md->event_nr); 28151da177e4SLinus Torvalds } 28161da177e4SLinus Torvalds 28171da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr) 28181da177e4SLinus Torvalds { 28191da177e4SLinus Torvalds return wait_event_interruptible(md->eventq, 28201da177e4SLinus Torvalds (event_nr != atomic_read(&md->event_nr))); 28211da177e4SLinus Torvalds } 28221da177e4SLinus Torvalds 28237a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist) 28247a8c3d3bSMike Anderson { 28257a8c3d3bSMike Anderson unsigned long flags; 28267a8c3d3bSMike Anderson 28277a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 28287a8c3d3bSMike Anderson list_add(elist, &md->uevent_list); 28297a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 28307a8c3d3bSMike Anderson } 28317a8c3d3bSMike Anderson 28321da177e4SLinus Torvalds /* 28331da177e4SLinus Torvalds * The gendisk is only valid as long as you have a reference 28341da177e4SLinus Torvalds * count on 'md'. 28351da177e4SLinus Torvalds */ 28361da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md) 28371da177e4SLinus Torvalds { 28381da177e4SLinus Torvalds return md->disk; 28391da177e4SLinus Torvalds } 284065ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk); 28411da177e4SLinus Torvalds 2842784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md) 2843784aae73SMilan Broz { 28442995fa78SMikulas Patocka return &md->kobj_holder.kobj; 2845784aae73SMilan Broz } 2846784aae73SMilan Broz 2847784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj) 2848784aae73SMilan Broz { 2849784aae73SMilan Broz struct mapped_device *md; 2850784aae73SMilan Broz 28512995fa78SMikulas Patocka md = container_of(kobj, struct mapped_device, kobj_holder.kobj); 2852784aae73SMilan Broz 2853b9a41d21SHou Tao spin_lock(&_minor_lock); 2854b9a41d21SHou Tao if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { 2855b9a41d21SHou Tao md = NULL; 2856b9a41d21SHou Tao goto out; 2857b9a41d21SHou Tao } 2858784aae73SMilan Broz dm_get(md); 2859b9a41d21SHou Tao out: 2860b9a41d21SHou Tao spin_unlock(&_minor_lock); 2861b9a41d21SHou Tao 2862784aae73SMilan Broz return md; 2863784aae73SMilan Broz } 2864784aae73SMilan Broz 28654f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md) 28661da177e4SLinus Torvalds { 28671da177e4SLinus Torvalds return test_bit(DMF_SUSPENDED, &md->flags); 28681da177e4SLinus Torvalds } 28691da177e4SLinus Torvalds 28705df96f2bSMikulas Patocka static int dm_post_suspending_md(struct mapped_device *md) 28715df96f2bSMikulas Patocka { 28725df96f2bSMikulas Patocka return test_bit(DMF_POST_SUSPENDING, &md->flags); 28735df96f2bSMikulas Patocka } 28745df96f2bSMikulas Patocka 2875ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md) 2876ffcc3936SMike Snitzer { 2877ffcc3936SMike Snitzer return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2878ffcc3936SMike Snitzer } 2879ffcc3936SMike Snitzer 28802c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md) 28812c140a24SMikulas Patocka { 28822c140a24SMikulas Patocka return test_bit(DMF_DEFERRED_REMOVE, &md->flags); 28832c140a24SMikulas Patocka } 28842c140a24SMikulas Patocka 288564dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti) 288664dbce58SKiyoshi Ueda { 288733bd6f06SMike Snitzer return dm_suspended_md(ti->table->md); 288864dbce58SKiyoshi Ueda } 288964dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended); 289064dbce58SKiyoshi Ueda 28915df96f2bSMikulas Patocka int dm_post_suspending(struct dm_target *ti) 28925df96f2bSMikulas Patocka { 289333bd6f06SMike Snitzer return dm_post_suspending_md(ti->table->md); 28945df96f2bSMikulas Patocka } 28955df96f2bSMikulas Patocka EXPORT_SYMBOL_GPL(dm_post_suspending); 28965df96f2bSMikulas Patocka 28972e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti) 28982e93ccc1SKiyoshi Ueda { 289933bd6f06SMike Snitzer return __noflush_suspending(ti->table->md); 29002e93ccc1SKiyoshi Ueda } 29012e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending); 29022e93ccc1SKiyoshi Ueda 29037e0d574fSBart Van Assche struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type, 29040776aa0eSMike Snitzer unsigned integrity, unsigned per_io_data_size, 29050776aa0eSMike Snitzer unsigned min_pool_size) 2906e6ee8c0bSKiyoshi Ueda { 2907115485e8SMike Snitzer struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id); 290878d8e58aSMike Snitzer unsigned int pool_size = 0; 290964f52b0eSMike Snitzer unsigned int front_pad, io_front_pad; 29106f1c819cSKent Overstreet int ret; 2911e6ee8c0bSKiyoshi Ueda 2912e6ee8c0bSKiyoshi Ueda if (!pools) 29134e6e36c3SMike Snitzer return NULL; 2914e6ee8c0bSKiyoshi Ueda 291578d8e58aSMike Snitzer switch (type) { 291678d8e58aSMike Snitzer case DM_TYPE_BIO_BASED: 2917545ed20eSToshi Kani case DM_TYPE_DAX_BIO_BASED: 29180776aa0eSMike Snitzer pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size); 291962f26317SJeffle Xu front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + DM_TARGET_IO_BIO_OFFSET; 292062f26317SJeffle Xu io_front_pad = roundup(per_io_data_size, __alignof__(struct dm_io)) + DM_IO_BIO_OFFSET; 29216f1c819cSKent Overstreet ret = bioset_init(&pools->io_bs, pool_size, io_front_pad, 0); 29226f1c819cSKent Overstreet if (ret) 292364f52b0eSMike Snitzer goto out; 29246f1c819cSKent Overstreet if (integrity && bioset_integrity_create(&pools->io_bs, pool_size)) 2925eb8db831SChristoph Hellwig goto out; 292678d8e58aSMike Snitzer break; 292778d8e58aSMike Snitzer case DM_TYPE_REQUEST_BASED: 29280776aa0eSMike Snitzer pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size); 292978d8e58aSMike Snitzer front_pad = offsetof(struct dm_rq_clone_bio_info, clone); 2930591ddcfcSMike Snitzer /* per_io_data_size is used for blk-mq pdu at queue allocation */ 293178d8e58aSMike Snitzer break; 293278d8e58aSMike Snitzer default: 293378d8e58aSMike Snitzer BUG(); 293478d8e58aSMike Snitzer } 293578d8e58aSMike Snitzer 29366f1c819cSKent Overstreet ret = bioset_init(&pools->bs, pool_size, front_pad, 0); 29376f1c819cSKent Overstreet if (ret) 29385f015204SJun'ichi Nomura goto out; 2939e6ee8c0bSKiyoshi Ueda 29406f1c819cSKent Overstreet if (integrity && bioset_integrity_create(&pools->bs, pool_size)) 29415f015204SJun'ichi Nomura goto out; 2942a91a2785SMartin K. Petersen 2943e6ee8c0bSKiyoshi Ueda return pools; 294478d8e58aSMike Snitzer 29455f015204SJun'ichi Nomura out: 29465f015204SJun'ichi Nomura dm_free_md_mempools(pools); 2947e6ee8c0bSKiyoshi Ueda 29484e6e36c3SMike Snitzer return NULL; 2949e6ee8c0bSKiyoshi Ueda } 2950e6ee8c0bSKiyoshi Ueda 2951e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools) 2952e6ee8c0bSKiyoshi Ueda { 2953e6ee8c0bSKiyoshi Ueda if (!pools) 2954e6ee8c0bSKiyoshi Ueda return; 2955e6ee8c0bSKiyoshi Ueda 29566f1c819cSKent Overstreet bioset_exit(&pools->bs); 29576f1c819cSKent Overstreet bioset_exit(&pools->io_bs); 2958e6ee8c0bSKiyoshi Ueda 2959e6ee8c0bSKiyoshi Ueda kfree(pools); 2960e6ee8c0bSKiyoshi Ueda } 2961e6ee8c0bSKiyoshi Ueda 29629c72bad1SChristoph Hellwig struct dm_pr { 29639c72bad1SChristoph Hellwig u64 old_key; 29649c72bad1SChristoph Hellwig u64 new_key; 29659c72bad1SChristoph Hellwig u32 flags; 29669c72bad1SChristoph Hellwig bool fail_early; 29679c72bad1SChristoph Hellwig }; 29689c72bad1SChristoph Hellwig 29699c72bad1SChristoph Hellwig static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn, 29709c72bad1SChristoph Hellwig void *data) 29719c72bad1SChristoph Hellwig { 29729c72bad1SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 29739c72bad1SChristoph Hellwig struct dm_table *table; 29749c72bad1SChristoph Hellwig struct dm_target *ti; 29759c72bad1SChristoph Hellwig int ret = -ENOTTY, srcu_idx; 29769c72bad1SChristoph Hellwig 29779c72bad1SChristoph Hellwig table = dm_get_live_table(md, &srcu_idx); 29789c72bad1SChristoph Hellwig if (!table || !dm_table_get_size(table)) 29799c72bad1SChristoph Hellwig goto out; 29809c72bad1SChristoph Hellwig 29819c72bad1SChristoph Hellwig /* We only support devices that have a single target */ 29829c72bad1SChristoph Hellwig if (dm_table_get_num_targets(table) != 1) 29839c72bad1SChristoph Hellwig goto out; 29849c72bad1SChristoph Hellwig ti = dm_table_get_target(table, 0); 29859c72bad1SChristoph Hellwig 29869c72bad1SChristoph Hellwig ret = -EINVAL; 29879c72bad1SChristoph Hellwig if (!ti->type->iterate_devices) 29889c72bad1SChristoph Hellwig goto out; 29899c72bad1SChristoph Hellwig 29909c72bad1SChristoph Hellwig ret = ti->type->iterate_devices(ti, fn, data); 29919c72bad1SChristoph Hellwig out: 29929c72bad1SChristoph Hellwig dm_put_live_table(md, srcu_idx); 29939c72bad1SChristoph Hellwig return ret; 29949c72bad1SChristoph Hellwig } 29959c72bad1SChristoph Hellwig 29969c72bad1SChristoph Hellwig /* 29979c72bad1SChristoph Hellwig * For register / unregister we need to manually call out to every path. 29989c72bad1SChristoph Hellwig */ 29999c72bad1SChristoph Hellwig static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev, 30009c72bad1SChristoph Hellwig sector_t start, sector_t len, void *data) 30019c72bad1SChristoph Hellwig { 30029c72bad1SChristoph Hellwig struct dm_pr *pr = data; 30039c72bad1SChristoph Hellwig const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops; 30049c72bad1SChristoph Hellwig 30059c72bad1SChristoph Hellwig if (!ops || !ops->pr_register) 30069c72bad1SChristoph Hellwig return -EOPNOTSUPP; 30079c72bad1SChristoph Hellwig return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags); 30089c72bad1SChristoph Hellwig } 30099c72bad1SChristoph Hellwig 301071cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key, 301171cdb697SChristoph Hellwig u32 flags) 301271cdb697SChristoph Hellwig { 30139c72bad1SChristoph Hellwig struct dm_pr pr = { 30149c72bad1SChristoph Hellwig .old_key = old_key, 30159c72bad1SChristoph Hellwig .new_key = new_key, 30169c72bad1SChristoph Hellwig .flags = flags, 30179c72bad1SChristoph Hellwig .fail_early = true, 30189c72bad1SChristoph Hellwig }; 30199c72bad1SChristoph Hellwig int ret; 302071cdb697SChristoph Hellwig 30219c72bad1SChristoph Hellwig ret = dm_call_pr(bdev, __dm_pr_register, &pr); 30229c72bad1SChristoph Hellwig if (ret && new_key) { 30239c72bad1SChristoph Hellwig /* unregister all paths if we failed to register any path */ 30249c72bad1SChristoph Hellwig pr.old_key = new_key; 30259c72bad1SChristoph Hellwig pr.new_key = 0; 30269c72bad1SChristoph Hellwig pr.flags = 0; 30279c72bad1SChristoph Hellwig pr.fail_early = false; 30289c72bad1SChristoph Hellwig dm_call_pr(bdev, __dm_pr_register, &pr); 30299c72bad1SChristoph Hellwig } 303071cdb697SChristoph Hellwig 30319c72bad1SChristoph Hellwig return ret; 303271cdb697SChristoph Hellwig } 303371cdb697SChristoph Hellwig 303471cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type, 303571cdb697SChristoph Hellwig u32 flags) 303671cdb697SChristoph Hellwig { 303771cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 303871cdb697SChristoph Hellwig const struct pr_ops *ops; 3039971888c4SMike Snitzer int r, srcu_idx; 304071cdb697SChristoph Hellwig 30415bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 304271cdb697SChristoph Hellwig if (r < 0) 3043971888c4SMike Snitzer goto out; 304471cdb697SChristoph Hellwig 304571cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 304671cdb697SChristoph Hellwig if (ops && ops->pr_reserve) 304771cdb697SChristoph Hellwig r = ops->pr_reserve(bdev, key, type, flags); 304871cdb697SChristoph Hellwig else 304971cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3050971888c4SMike Snitzer out: 3051971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 305271cdb697SChristoph Hellwig return r; 305371cdb697SChristoph Hellwig } 305471cdb697SChristoph Hellwig 305571cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type) 305671cdb697SChristoph Hellwig { 305771cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 305871cdb697SChristoph Hellwig const struct pr_ops *ops; 3059971888c4SMike Snitzer int r, srcu_idx; 306071cdb697SChristoph Hellwig 30615bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 306271cdb697SChristoph Hellwig if (r < 0) 3063971888c4SMike Snitzer goto out; 306471cdb697SChristoph Hellwig 306571cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 306671cdb697SChristoph Hellwig if (ops && ops->pr_release) 306771cdb697SChristoph Hellwig r = ops->pr_release(bdev, key, type); 306871cdb697SChristoph Hellwig else 306971cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3070971888c4SMike Snitzer out: 3071971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 307271cdb697SChristoph Hellwig return r; 307371cdb697SChristoph Hellwig } 307471cdb697SChristoph Hellwig 307571cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key, 307671cdb697SChristoph Hellwig enum pr_type type, bool abort) 307771cdb697SChristoph Hellwig { 307871cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 307971cdb697SChristoph Hellwig const struct pr_ops *ops; 3080971888c4SMike Snitzer int r, srcu_idx; 308171cdb697SChristoph Hellwig 30825bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 308371cdb697SChristoph Hellwig if (r < 0) 3084971888c4SMike Snitzer goto out; 308571cdb697SChristoph Hellwig 308671cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 308771cdb697SChristoph Hellwig if (ops && ops->pr_preempt) 308871cdb697SChristoph Hellwig r = ops->pr_preempt(bdev, old_key, new_key, type, abort); 308971cdb697SChristoph Hellwig else 309071cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3091971888c4SMike Snitzer out: 3092971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 309371cdb697SChristoph Hellwig return r; 309471cdb697SChristoph Hellwig } 309571cdb697SChristoph Hellwig 309671cdb697SChristoph Hellwig static int dm_pr_clear(struct block_device *bdev, u64 key) 309771cdb697SChristoph Hellwig { 309871cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 309971cdb697SChristoph Hellwig const struct pr_ops *ops; 3100971888c4SMike Snitzer int r, srcu_idx; 310171cdb697SChristoph Hellwig 31025bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 310371cdb697SChristoph Hellwig if (r < 0) 3104971888c4SMike Snitzer goto out; 310571cdb697SChristoph Hellwig 310671cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 310771cdb697SChristoph Hellwig if (ops && ops->pr_clear) 310871cdb697SChristoph Hellwig r = ops->pr_clear(bdev, key); 310971cdb697SChristoph Hellwig else 311071cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3111971888c4SMike Snitzer out: 3112971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 311371cdb697SChristoph Hellwig return r; 311471cdb697SChristoph Hellwig } 311571cdb697SChristoph Hellwig 311671cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = { 311771cdb697SChristoph Hellwig .pr_register = dm_pr_register, 311871cdb697SChristoph Hellwig .pr_reserve = dm_pr_reserve, 311971cdb697SChristoph Hellwig .pr_release = dm_pr_release, 312071cdb697SChristoph Hellwig .pr_preempt = dm_pr_preempt, 312171cdb697SChristoph Hellwig .pr_clear = dm_pr_clear, 312271cdb697SChristoph Hellwig }; 312371cdb697SChristoph Hellwig 312483d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = { 3125c62b37d9SChristoph Hellwig .submit_bio = dm_submit_bio, 3126b99fdcdcSMing Lei .poll_bio = dm_poll_bio, 31271da177e4SLinus Torvalds .open = dm_blk_open, 31281da177e4SLinus Torvalds .release = dm_blk_close, 3129aa129a22SMilan Broz .ioctl = dm_blk_ioctl, 31303ac51e74SDarrick J. Wong .getgeo = dm_blk_getgeo, 3131e76239a3SChristoph Hellwig .report_zones = dm_blk_report_zones, 313271cdb697SChristoph Hellwig .pr_ops = &dm_pr_ops, 31331da177e4SLinus Torvalds .owner = THIS_MODULE 31341da177e4SLinus Torvalds }; 31351da177e4SLinus Torvalds 3136681cc5e8SMike Snitzer static const struct block_device_operations dm_rq_blk_dops = { 3137681cc5e8SMike Snitzer .open = dm_blk_open, 3138681cc5e8SMike Snitzer .release = dm_blk_close, 3139681cc5e8SMike Snitzer .ioctl = dm_blk_ioctl, 3140681cc5e8SMike Snitzer .getgeo = dm_blk_getgeo, 3141681cc5e8SMike Snitzer .pr_ops = &dm_pr_ops, 3142681cc5e8SMike Snitzer .owner = THIS_MODULE 3143681cc5e8SMike Snitzer }; 3144681cc5e8SMike Snitzer 3145f26c5719SDan Williams static const struct dax_operations dm_dax_ops = { 3146f26c5719SDan Williams .direct_access = dm_dax_direct_access, 3147cdf6cdcdSVivek Goyal .zero_page_range = dm_dax_zero_page_range, 3148f26c5719SDan Williams }; 3149f26c5719SDan Williams 31501da177e4SLinus Torvalds /* 31511da177e4SLinus Torvalds * module hooks 31521da177e4SLinus Torvalds */ 31531da177e4SLinus Torvalds module_init(dm_init); 31541da177e4SLinus Torvalds module_exit(dm_exit); 31551da177e4SLinus Torvalds 31561da177e4SLinus Torvalds module_param(major, uint, 0); 31571da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper"); 3158f4790826SMike Snitzer 3159e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR); 3160e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools"); 3161e8603136SMike Snitzer 3162115485e8SMike Snitzer module_param(dm_numa_node, int, S_IRUGO | S_IWUSR); 3163115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations"); 3164115485e8SMike Snitzer 3165a666e5c0SMikulas Patocka module_param(swap_bios, int, S_IRUGO | S_IWUSR); 3166a666e5c0SMikulas Patocka MODULE_PARM_DESC(swap_bios, "Maximum allowed inflight swap IOs"); 3167a666e5c0SMikulas Patocka 31681da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver"); 31691da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 31701da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 3171