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; 58184b98f4cSMike Snitzer io->status = BLK_STS_OK; 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 */ 902cfc97abcSMike Snitzer bio_clear_polled(bio); 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 */ 93684b98f4cSMike Snitzer static inline void __dm_io_dec_pending(struct dm_io *io) 937e2736347SMike Snitzer { 93884b98f4cSMike Snitzer if (atomic_dec_and_test(&io->io_count)) 93984b98f4cSMike Snitzer dm_io_complete(io); 94084b98f4cSMike Snitzer } 94184b98f4cSMike Snitzer 94284b98f4cSMike Snitzer static void dm_io_set_error(struct dm_io *io, blk_status_t error) 94384b98f4cSMike Snitzer { 944e2736347SMike Snitzer unsigned long flags; 94584b98f4cSMike Snitzer 94684b98f4cSMike Snitzer /* Push-back supersedes any I/O errors */ 9474d7bca13SMike Snitzer spin_lock_irqsave(&io->lock, flags); 948e2736347SMike Snitzer if (!(io->status == BLK_STS_DM_REQUEUE && 94984b98f4cSMike Snitzer __noflush_suspending(io->md))) { 950e2736347SMike Snitzer io->status = error; 95184b98f4cSMike Snitzer } 9524d7bca13SMike Snitzer spin_unlock_irqrestore(&io->lock, flags); 953e2736347SMike Snitzer } 954e2736347SMike Snitzer 95584b98f4cSMike Snitzer void dm_io_dec_pending(struct dm_io *io, blk_status_t error) 95684b98f4cSMike Snitzer { 95784b98f4cSMike Snitzer if (unlikely(error)) 95884b98f4cSMike Snitzer dm_io_set_error(io, error); 95984b98f4cSMike Snitzer 96084b98f4cSMike Snitzer __dm_io_dec_pending(io); 961af7e466aSMikulas Patocka } 9621da177e4SLinus Torvalds 963bcb44433SMike Snitzer void disable_discard(struct mapped_device *md) 964bcb44433SMike Snitzer { 965bcb44433SMike Snitzer struct queue_limits *limits = dm_get_queue_limits(md); 966bcb44433SMike Snitzer 967bcb44433SMike Snitzer /* device doesn't really support DISCARD, disable it */ 968bcb44433SMike Snitzer limits->max_discard_sectors = 0; 969bcb44433SMike Snitzer } 970bcb44433SMike Snitzer 971ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md) 972ac62d620SChristoph Hellwig { 973ac62d620SChristoph Hellwig struct queue_limits *limits = dm_get_queue_limits(md); 974ac62d620SChristoph Hellwig 975ac62d620SChristoph Hellwig /* device doesn't really support WRITE ZEROES, disable it */ 976ac62d620SChristoph Hellwig limits->max_write_zeroes_sectors = 0; 977ac62d620SChristoph Hellwig } 978ac62d620SChristoph Hellwig 979a666e5c0SMikulas Patocka static bool swap_bios_limit(struct dm_target *ti, struct bio *bio) 980a666e5c0SMikulas Patocka { 981a666e5c0SMikulas Patocka return unlikely((bio->bi_opf & REQ_SWAP) != 0) && unlikely(ti->limit_swap_bios); 982a666e5c0SMikulas Patocka } 983a666e5c0SMikulas Patocka 9844246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio) 9851da177e4SLinus Torvalds { 9864e4cbee9SChristoph Hellwig blk_status_t error = bio->bi_status; 9876c23f0bdSChristoph Hellwig struct dm_target_io *tio = clone_to_tio(bio); 988b35f8caaSMilan Broz struct dm_io *io = tio->io; 9899faf400fSStefan Bader struct mapped_device *md = tio->io->md; 9901da177e4SLinus Torvalds dm_endio_fn endio = tio->ti->type->end_io; 991309dca30SChristoph Hellwig struct request_queue *q = bio->bi_bdev->bd_disk->queue; 9921da177e4SLinus Torvalds 9939c37de29SMike Snitzer if (unlikely(error == BLK_STS_TARGET)) { 994bcb44433SMike Snitzer if (bio_op(bio) == REQ_OP_DISCARD && 99570200574SChristoph Hellwig !bdev_max_discard_sectors(bio->bi_bdev)) 996bcb44433SMike Snitzer disable_discard(md); 997bcb44433SMike Snitzer else if (bio_op(bio) == REQ_OP_WRITE_ZEROES && 998309dca30SChristoph Hellwig !q->limits.max_write_zeroes_sectors) 999ac62d620SChristoph Hellwig disable_write_zeroes(md); 1000ac62d620SChristoph Hellwig } 10017eee4ae2SMike Snitzer 1002bb37d772SDamien Le Moal if (blk_queue_is_zoned(q)) 1003bb37d772SDamien Le Moal dm_zone_endio(io, bio); 1004415c79e1SJohannes Thumshirn 10051be56909SChristoph Hellwig if (endio) { 10064e4cbee9SChristoph Hellwig int r = endio(tio->ti, bio, &error); 10071be56909SChristoph Hellwig switch (r) { 10081be56909SChristoph Hellwig case DM_ENDIO_REQUEUE: 1009bf14e2b2SDamien Le Moal /* 1010bf14e2b2SDamien Le Moal * Requeuing writes to a sequential zone of a zoned 1011bf14e2b2SDamien Le Moal * target will break the sequential write pattern: 1012bf14e2b2SDamien Le Moal * fail such IO. 1013bf14e2b2SDamien Le Moal */ 1014bf14e2b2SDamien Le Moal if (WARN_ON_ONCE(dm_is_zone_write(md, bio))) 1015bf14e2b2SDamien Le Moal error = BLK_STS_IOERR; 1016bf14e2b2SDamien Le Moal else 10174e4cbee9SChristoph Hellwig error = BLK_STS_DM_REQUEUE; 1018df561f66SGustavo A. R. Silva fallthrough; 10191be56909SChristoph Hellwig case DM_ENDIO_DONE: 10201be56909SChristoph Hellwig break; 10211be56909SChristoph Hellwig case DM_ENDIO_INCOMPLETE: 10221be56909SChristoph Hellwig /* The target will handle the io */ 10231be56909SChristoph Hellwig return; 10241be56909SChristoph Hellwig default: 10251be56909SChristoph Hellwig DMWARN("unimplemented target endio return value: %d", r); 10261be56909SChristoph Hellwig BUG(); 10271be56909SChristoph Hellwig } 10281be56909SChristoph Hellwig } 10291be56909SChristoph Hellwig 1030a666e5c0SMikulas Patocka if (unlikely(swap_bios_limit(tio->ti, bio))) { 1031a666e5c0SMikulas Patocka struct mapped_device *md = io->md; 1032a666e5c0SMikulas Patocka up(&md->swap_bios_semaphore); 1033a666e5c0SMikulas Patocka } 1034a666e5c0SMikulas Patocka 10351d1068ceSChristoph Hellwig free_tio(bio); 1036e2118b3cSDamien Le Moal dm_io_dec_pending(io, error); 10371da177e4SLinus Torvalds } 10381da177e4SLinus Torvalds 103978d8e58aSMike Snitzer /* 104056a67df7SMike Snitzer * Return maximum size of I/O possible at the supplied sector up to the current 104156a67df7SMike Snitzer * target boundary. 104256a67df7SMike Snitzer */ 10433720281dSMike Snitzer static inline sector_t max_io_len_target_boundary(struct dm_target *ti, 10443720281dSMike Snitzer sector_t target_offset) 10451da177e4SLinus Torvalds { 104656a67df7SMike Snitzer return ti->len - target_offset; 104756a67df7SMike Snitzer } 104856a67df7SMike Snitzer 10493720281dSMike Snitzer static sector_t max_io_len(struct dm_target *ti, sector_t sector) 105056a67df7SMike Snitzer { 10513720281dSMike Snitzer sector_t target_offset = dm_target_offset(ti, sector); 10523720281dSMike Snitzer sector_t len = max_io_len_target_boundary(ti, target_offset); 10535091cdecSMike Snitzer sector_t max_len; 10541da177e4SLinus Torvalds 10551da177e4SLinus Torvalds /* 10563ee16db3SMike Snitzer * Does the target need to split IO even further? 10573ee16db3SMike Snitzer * - varied (per target) IO splitting is a tenet of DM; this 10583ee16db3SMike Snitzer * explains why stacked chunk_sectors based splitting via 10593ee16db3SMike Snitzer * blk_max_size_offset() isn't possible here. So pass in 10603ee16db3SMike Snitzer * ti->max_io_len to override stacked chunk_sectors. 10611da177e4SLinus Torvalds */ 10623ee16db3SMike Snitzer if (ti->max_io_len) { 106333bd6f06SMike Snitzer max_len = blk_max_size_offset(ti->table->md->queue, 10643ee16db3SMike Snitzer target_offset, ti->max_io_len); 1065542f9038SMike Snitzer if (len > max_len) 1066542f9038SMike Snitzer len = max_len; 10673ee16db3SMike Snitzer } 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds return len; 10701da177e4SLinus Torvalds } 10711da177e4SLinus Torvalds 1072542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len) 1073542f9038SMike Snitzer { 1074542f9038SMike Snitzer if (len > UINT_MAX) { 1075542f9038SMike Snitzer DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)", 1076542f9038SMike Snitzer (unsigned long long)len, UINT_MAX); 1077542f9038SMike Snitzer ti->error = "Maximum size of target IO is too large"; 1078542f9038SMike Snitzer return -EINVAL; 1079542f9038SMike Snitzer } 1080542f9038SMike Snitzer 108175ae1936SMikulas Patocka ti->max_io_len = (uint32_t) len; 1082542f9038SMike Snitzer 1083542f9038SMike Snitzer return 0; 1084542f9038SMike Snitzer } 1085542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len); 1086542f9038SMike Snitzer 1087f26c5719SDan Williams static struct dm_target *dm_dax_get_live_target(struct mapped_device *md, 1088f26c5719SDan Williams sector_t sector, int *srcu_idx) 10893d97c829SMike Snitzer __acquires(md->io_barrier) 1090545ed20eSToshi Kani { 1091545ed20eSToshi Kani struct dm_table *map; 1092545ed20eSToshi Kani struct dm_target *ti; 1093545ed20eSToshi Kani 1094f26c5719SDan Williams map = dm_get_live_table(md, srcu_idx); 1095545ed20eSToshi Kani if (!map) 1096f26c5719SDan Williams return NULL; 1097545ed20eSToshi Kani 1098545ed20eSToshi Kani ti = dm_table_find_target(map, sector); 1099123d87d5SMikulas Patocka if (!ti) 1100f26c5719SDan Williams return NULL; 1101f26c5719SDan Williams 1102f26c5719SDan Williams return ti; 1103f26c5719SDan Williams } 1104f26c5719SDan Williams 1105f26c5719SDan Williams static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, 1106f26c5719SDan Williams long nr_pages, void **kaddr, pfn_t *pfn) 1107f26c5719SDan Williams { 1108f26c5719SDan Williams struct mapped_device *md = dax_get_private(dax_dev); 1109f26c5719SDan Williams sector_t sector = pgoff * PAGE_SECTORS; 1110f26c5719SDan Williams struct dm_target *ti; 1111f26c5719SDan Williams long len, ret = -EIO; 1112f26c5719SDan Williams int srcu_idx; 1113f26c5719SDan Williams 1114f26c5719SDan Williams ti = dm_dax_get_live_target(md, sector, &srcu_idx); 1115f26c5719SDan Williams 1116f26c5719SDan Williams if (!ti) 1117545ed20eSToshi Kani goto out; 1118f26c5719SDan Williams if (!ti->type->direct_access) 1119f26c5719SDan Williams goto out; 11203720281dSMike Snitzer len = max_io_len(ti, sector) / PAGE_SECTORS; 1121f26c5719SDan Williams if (len < 1) 1122f26c5719SDan Williams goto out; 1123f26c5719SDan Williams nr_pages = min(len, nr_pages); 1124817bf402SDan Williams ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn); 1125817bf402SDan Williams 1126545ed20eSToshi Kani out: 1127545ed20eSToshi Kani dm_put_live_table(md, srcu_idx); 1128f26c5719SDan Williams 1129f26c5719SDan Williams return ret; 1130545ed20eSToshi Kani } 1131545ed20eSToshi Kani 1132cdf6cdcdSVivek Goyal static int dm_dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff, 1133cdf6cdcdSVivek Goyal size_t nr_pages) 1134cdf6cdcdSVivek Goyal { 1135cdf6cdcdSVivek Goyal struct mapped_device *md = dax_get_private(dax_dev); 1136cdf6cdcdSVivek Goyal sector_t sector = pgoff * PAGE_SECTORS; 1137cdf6cdcdSVivek Goyal struct dm_target *ti; 1138cdf6cdcdSVivek Goyal int ret = -EIO; 1139cdf6cdcdSVivek Goyal int srcu_idx; 1140cdf6cdcdSVivek Goyal 1141cdf6cdcdSVivek Goyal ti = dm_dax_get_live_target(md, sector, &srcu_idx); 1142cdf6cdcdSVivek Goyal 1143cdf6cdcdSVivek Goyal if (!ti) 1144cdf6cdcdSVivek Goyal goto out; 1145cdf6cdcdSVivek Goyal if (WARN_ON(!ti->type->dax_zero_page_range)) { 1146cdf6cdcdSVivek Goyal /* 1147cdf6cdcdSVivek Goyal * ->zero_page_range() is mandatory dax operation. If we are 1148cdf6cdcdSVivek Goyal * here, something is wrong. 1149cdf6cdcdSVivek Goyal */ 1150cdf6cdcdSVivek Goyal goto out; 1151cdf6cdcdSVivek Goyal } 1152cdf6cdcdSVivek Goyal ret = ti->type->dax_zero_page_range(ti, pgoff, nr_pages); 1153cdf6cdcdSVivek Goyal out: 1154cdf6cdcdSVivek Goyal dm_put_live_table(md, srcu_idx); 1155cdf6cdcdSVivek Goyal 1156cdf6cdcdSVivek Goyal return ret; 1157cdf6cdcdSVivek Goyal } 1158cdf6cdcdSVivek Goyal 11591dd40c3eSMikulas Patocka /* 11601dd40c3eSMikulas Patocka * A target may call dm_accept_partial_bio only from the map routine. It is 11616842d264SDamien Le Moal * allowed for all bio types except REQ_PREFLUSH, REQ_OP_ZONE_* zone management 1162e6fc9f62SMike Snitzer * operations, REQ_OP_ZONE_APPEND (zone append writes) and any bio serviced by 1163e6fc9f62SMike Snitzer * __send_duplicate_bios(). 11641dd40c3eSMikulas Patocka * 11651dd40c3eSMikulas Patocka * dm_accept_partial_bio informs the dm that the target only wants to process 11661dd40c3eSMikulas Patocka * additional n_sectors sectors of the bio and the rest of the data should be 11671dd40c3eSMikulas Patocka * sent in a next bio. 11681dd40c3eSMikulas Patocka * 11691dd40c3eSMikulas Patocka * A diagram that explains the arithmetics: 11701dd40c3eSMikulas Patocka * +--------------------+---------------+-------+ 11711dd40c3eSMikulas Patocka * | 1 | 2 | 3 | 11721dd40c3eSMikulas Patocka * +--------------------+---------------+-------+ 11731dd40c3eSMikulas Patocka * 11741dd40c3eSMikulas Patocka * <-------------- *tio->len_ptr ---------------> 11751dd40c3eSMikulas Patocka * <------- bi_size -------> 11761dd40c3eSMikulas Patocka * <-- n_sectors --> 11771dd40c3eSMikulas Patocka * 11781dd40c3eSMikulas Patocka * Region 1 was already iterated over with bio_advance or similar function. 11791dd40c3eSMikulas Patocka * (it may be empty if the target doesn't use bio_advance) 11801dd40c3eSMikulas Patocka * Region 2 is the remaining bio size that the target wants to process. 11811dd40c3eSMikulas Patocka * (it may be empty if region 1 is non-empty, although there is no reason 11821dd40c3eSMikulas Patocka * to make it empty) 11831dd40c3eSMikulas Patocka * The target requires that region 3 is to be sent in the next bio. 11841dd40c3eSMikulas Patocka * 11851dd40c3eSMikulas Patocka * If the target wants to receive multiple copies of the bio (via num_*bios, etc), 11861dd40c3eSMikulas Patocka * the partially processed part (the sum of regions 1+2) must be the same for all 11871dd40c3eSMikulas Patocka * copies of the bio. 11881dd40c3eSMikulas Patocka */ 11891dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors) 11901dd40c3eSMikulas Patocka { 11916c23f0bdSChristoph Hellwig struct dm_target_io *tio = clone_to_tio(bio); 11921dd40c3eSMikulas Patocka unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT; 11936842d264SDamien Le Moal 1194655f3aadSMike Snitzer BUG_ON(dm_tio_flagged(tio, DM_TIO_IS_DUPLICATE_BIO)); 11956842d264SDamien Le Moal BUG_ON(op_is_zone_mgmt(bio_op(bio))); 11966842d264SDamien Le Moal BUG_ON(bio_op(bio) == REQ_OP_ZONE_APPEND); 11971dd40c3eSMikulas Patocka BUG_ON(bi_size > *tio->len_ptr); 11981dd40c3eSMikulas Patocka BUG_ON(n_sectors > bi_size); 11996842d264SDamien Le Moal 12001dd40c3eSMikulas Patocka *tio->len_ptr -= bi_size - n_sectors; 12011dd40c3eSMikulas Patocka bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT; 12021dd40c3eSMikulas Patocka } 12031dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio); 12041dd40c3eSMikulas Patocka 12050fbb4d93SMike Snitzer static inline void __dm_submit_bio_remap(struct bio *clone, 12060fbb4d93SMike Snitzer dev_t dev, sector_t old_sector) 12070fbb4d93SMike Snitzer { 12080fbb4d93SMike Snitzer trace_block_bio_remap(clone, dev, old_sector); 12090fbb4d93SMike Snitzer submit_bio_noacct(clone); 12100fbb4d93SMike Snitzer } 12110fbb4d93SMike Snitzer 12120fbb4d93SMike Snitzer /* 12130fbb4d93SMike Snitzer * @clone: clone bio that DM core passed to target's .map function 12140fbb4d93SMike Snitzer * @tgt_clone: clone of @clone bio that target needs submitted 12150fbb4d93SMike Snitzer * 12160fbb4d93SMike Snitzer * Targets should use this interface to submit bios they take 12170fbb4d93SMike Snitzer * ownership of when returning DM_MAPIO_SUBMITTED. 12180fbb4d93SMike Snitzer * 12190fbb4d93SMike Snitzer * Target should also enable ti->accounts_remapped_io 12200fbb4d93SMike Snitzer */ 1221b7f8dff0SMike Snitzer void dm_submit_bio_remap(struct bio *clone, struct bio *tgt_clone) 12220fbb4d93SMike Snitzer { 12230fbb4d93SMike Snitzer struct dm_target_io *tio = clone_to_tio(clone); 12240fbb4d93SMike Snitzer struct dm_io *io = tio->io; 12250fbb4d93SMike Snitzer 12260a8e9599SMike Snitzer WARN_ON_ONCE(!tio->ti->accounts_remapped_io); 12270a8e9599SMike Snitzer 12280fbb4d93SMike Snitzer /* establish bio that will get submitted */ 12290fbb4d93SMike Snitzer if (!tgt_clone) 12300fbb4d93SMike Snitzer tgt_clone = clone; 12310fbb4d93SMike Snitzer 12320fbb4d93SMike Snitzer /* 12330fbb4d93SMike Snitzer * Account io->origin_bio to DM dev on behalf of target 12340fbb4d93SMike Snitzer * that took ownership of IO with DM_MAPIO_SUBMITTED. 12350fbb4d93SMike Snitzer */ 1236b7f8dff0SMike Snitzer if (io->map_task == current) { 12370fbb4d93SMike Snitzer /* Still in target's map function */ 123882f6cdccSMike Snitzer dm_io_set_flag(io, DM_IO_START_ACCT); 12390fbb4d93SMike Snitzer } else { 12400fbb4d93SMike Snitzer /* 12410fbb4d93SMike Snitzer * Called by another thread, managed by DM target, 12420fbb4d93SMike Snitzer * wait for dm_split_and_process_bio() to store 12430fbb4d93SMike Snitzer * io->orig_bio 12440fbb4d93SMike Snitzer */ 12450fbb4d93SMike Snitzer while (unlikely(!smp_load_acquire(&io->orig_bio))) 12460fbb4d93SMike Snitzer msleep(1); 12470fbb4d93SMike Snitzer dm_start_io_acct(io, clone); 12480fbb4d93SMike Snitzer } 12490fbb4d93SMike Snitzer 12500fbb4d93SMike Snitzer __dm_submit_bio_remap(tgt_clone, disk_devt(io->md->disk), 12510fbb4d93SMike Snitzer tio->old_sector); 12520fbb4d93SMike Snitzer } 12530fbb4d93SMike Snitzer EXPORT_SYMBOL_GPL(dm_submit_bio_remap); 12540fbb4d93SMike Snitzer 1255a666e5c0SMikulas Patocka static noinline void __set_swap_bios_limit(struct mapped_device *md, int latch) 1256a666e5c0SMikulas Patocka { 1257a666e5c0SMikulas Patocka mutex_lock(&md->swap_bios_lock); 1258a666e5c0SMikulas Patocka while (latch < md->swap_bios) { 1259a666e5c0SMikulas Patocka cond_resched(); 1260a666e5c0SMikulas Patocka down(&md->swap_bios_semaphore); 1261a666e5c0SMikulas Patocka md->swap_bios--; 1262a666e5c0SMikulas Patocka } 1263a666e5c0SMikulas Patocka while (latch > md->swap_bios) { 1264a666e5c0SMikulas Patocka cond_resched(); 1265a666e5c0SMikulas Patocka up(&md->swap_bios_semaphore); 1266a666e5c0SMikulas Patocka md->swap_bios++; 1267a666e5c0SMikulas Patocka } 1268a666e5c0SMikulas Patocka mutex_unlock(&md->swap_bios_lock); 1269a666e5c0SMikulas Patocka } 1270a666e5c0SMikulas Patocka 12711561b396SChristoph Hellwig static void __map_bio(struct bio *clone) 12721da177e4SLinus Torvalds { 12731561b396SChristoph Hellwig struct dm_target_io *tio = clone_to_tio(clone); 12741da177e4SLinus Torvalds int r; 127564f52b0eSMike Snitzer struct dm_io *io = tio->io; 1276bd2a49b8SAlasdair G Kergon struct dm_target *ti = tio->ti; 12771da177e4SLinus Torvalds 12781da177e4SLinus Torvalds clone->bi_end_io = clone_endio; 12791da177e4SLinus Torvalds 12801da177e4SLinus Torvalds /* 12810fbb4d93SMike Snitzer * Map the clone. 12821da177e4SLinus Torvalds */ 1283e2118b3cSDamien Le Moal dm_io_inc_pending(io); 1284743598f0SMike Snitzer tio->old_sector = clone->bi_iter.bi_sector; 1285d67a5f4bSMikulas Patocka 1286a666e5c0SMikulas Patocka if (unlikely(swap_bios_limit(ti, clone))) { 1287a666e5c0SMikulas Patocka struct mapped_device *md = io->md; 1288a666e5c0SMikulas Patocka int latch = get_swap_bios(); 1289a666e5c0SMikulas Patocka if (unlikely(latch != md->swap_bios)) 1290a666e5c0SMikulas Patocka __set_swap_bios_limit(md, latch); 1291a666e5c0SMikulas Patocka down(&md->swap_bios_semaphore); 1292a666e5c0SMikulas Patocka } 1293a666e5c0SMikulas Patocka 1294bb37d772SDamien Le Moal /* 1295bb37d772SDamien Le Moal * Check if the IO needs a special mapping due to zone append emulation 1296bb37d772SDamien Le Moal * on zoned target. In this case, dm_zone_map_bio() calls the target 1297bb37d772SDamien Le Moal * map operation. 1298bb37d772SDamien Le Moal */ 1299bb37d772SDamien Le Moal if (dm_emulate_zone_append(io->md)) 1300bb37d772SDamien Le Moal r = dm_zone_map_bio(tio); 1301bb37d772SDamien Le Moal else 13027de3ee57SMikulas Patocka r = ti->type->map(ti, clone); 1303bb37d772SDamien Le Moal 1304846785e6SChristoph Hellwig switch (r) { 1305846785e6SChristoph Hellwig case DM_MAPIO_SUBMITTED: 13060fbb4d93SMike Snitzer /* target has assumed ownership of this io */ 13070fbb4d93SMike Snitzer if (!ti->accounts_remapped_io) 130882f6cdccSMike Snitzer dm_io_set_flag(io, DM_IO_START_ACCT); 1309846785e6SChristoph Hellwig break; 1310846785e6SChristoph Hellwig case DM_MAPIO_REMAPPED: 13110fbb4d93SMike Snitzer /* 13120fbb4d93SMike Snitzer * the bio has been remapped so dispatch it, but defer 13130fbb4d93SMike Snitzer * dm_start_io_acct() until after possible bio_split(). 13140fbb4d93SMike Snitzer */ 13150fbb4d93SMike Snitzer __dm_submit_bio_remap(clone, disk_devt(io->md->disk), 1316743598f0SMike Snitzer tio->old_sector); 131782f6cdccSMike Snitzer dm_io_set_flag(io, DM_IO_START_ACCT); 1318846785e6SChristoph Hellwig break; 1319846785e6SChristoph Hellwig case DM_MAPIO_KILL: 1320846785e6SChristoph Hellwig case DM_MAPIO_REQUEUE: 132190a2326eSMike Snitzer if (unlikely(swap_bios_limit(ti, clone))) 132290a2326eSMike Snitzer up(&io->md->swap_bios_semaphore); 13231d1068ceSChristoph Hellwig free_tio(clone); 132490a2326eSMike Snitzer if (r == DM_MAPIO_KILL) 132590a2326eSMike Snitzer dm_io_dec_pending(io, BLK_STS_IOERR); 132690a2326eSMike Snitzer else 1327e2118b3cSDamien Le Moal dm_io_dec_pending(io, BLK_STS_DM_REQUEUE); 1328846785e6SChristoph Hellwig break; 1329846785e6SChristoph Hellwig default: 133045cbcd79SKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 133145cbcd79SKiyoshi Ueda BUG(); 13321da177e4SLinus Torvalds } 13331da177e4SLinus Torvalds } 13341da177e4SLinus Torvalds 1335318716ddSMike Snitzer static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci, 13367dd06a25SMike Snitzer struct dm_target *ti, unsigned num_bios) 1337f9ab94ceSMikulas Patocka { 13381d1068ceSChristoph Hellwig struct bio *bio; 1339318716ddSMike Snitzer int try; 1340dba14160SMikulas Patocka 1341318716ddSMike Snitzer for (try = 0; try < 2; try++) { 1342318716ddSMike Snitzer int bio_nr; 1343318716ddSMike Snitzer 1344318716ddSMike Snitzer if (try) 1345bc02cdbeSMike Snitzer mutex_lock(&ci->io->md->table_devices_lock); 1346318716ddSMike Snitzer for (bio_nr = 0; bio_nr < num_bios; bio_nr++) { 13477dd06a25SMike Snitzer bio = alloc_tio(ci, ti, bio_nr, NULL, 1348dc8e2021SChristoph Hellwig try ? GFP_NOIO : GFP_NOWAIT); 13491d1068ceSChristoph Hellwig if (!bio) 1350318716ddSMike Snitzer break; 1351318716ddSMike Snitzer 13521d1068ceSChristoph Hellwig bio_list_add(blist, bio); 1353318716ddSMike Snitzer } 1354318716ddSMike Snitzer if (try) 1355bc02cdbeSMike Snitzer mutex_unlock(&ci->io->md->table_devices_lock); 1356318716ddSMike Snitzer if (bio_nr == num_bios) 1357318716ddSMike Snitzer return; 1358318716ddSMike Snitzer 13596c23f0bdSChristoph Hellwig while ((bio = bio_list_pop(blist))) 13601d1068ceSChristoph Hellwig free_tio(bio); 1361318716ddSMike Snitzer } 1362318716ddSMike Snitzer } 1363f9ab94ceSMikulas Patocka 136414fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti, 13651dd40c3eSMikulas Patocka unsigned num_bios, unsigned *len) 136606a426ceSMike Snitzer { 1367318716ddSMike Snitzer struct bio_list blist = BIO_EMPTY_LIST; 13688eabf5d0SChristoph Hellwig struct bio *clone; 136906a426ceSMike Snitzer 1370891fced6SChristoph Hellwig switch (num_bios) { 1371891fced6SChristoph Hellwig case 0: 1372891fced6SChristoph Hellwig break; 1373891fced6SChristoph Hellwig case 1: 1374891fced6SChristoph Hellwig clone = alloc_tio(ci, ti, 0, len, GFP_NOIO); 1375891fced6SChristoph Hellwig __map_bio(clone); 1376891fced6SChristoph Hellwig break; 1377891fced6SChristoph Hellwig default: 13787dd06a25SMike Snitzer /* dm_accept_partial_bio() is not supported with shared tio->len_ptr */ 13797dd06a25SMike Snitzer alloc_multiple_bios(&blist, ci, ti, num_bios); 13808eabf5d0SChristoph Hellwig while ((clone = bio_list_pop(&blist))) { 1381655f3aadSMike Snitzer dm_tio_set_flag(clone_to_tio(clone), DM_TIO_IS_DUPLICATE_BIO); 13821561b396SChristoph Hellwig __map_bio(clone); 1383f9ab94ceSMikulas Patocka } 1384891fced6SChristoph Hellwig break; 1385318716ddSMike Snitzer } 138606a426ceSMike Snitzer } 138706a426ceSMike Snitzer 1388332f2b1eSMike Snitzer static void __send_empty_flush(struct clone_info *ci) 1389f9ab94ceSMikulas Patocka { 139006a426ceSMike Snitzer unsigned target_nr = 0; 1391f9ab94ceSMikulas Patocka struct dm_target *ti; 1392828678b8SMike Snitzer struct bio flush_bio; 1393828678b8SMike Snitzer 1394828678b8SMike Snitzer /* 1395828678b8SMike Snitzer * Use an on-stack bio for this, it's safe since we don't 1396828678b8SMike Snitzer * need to reference it after submit. It's just used as 1397828678b8SMike Snitzer * the basis for the clone(s). 1398828678b8SMike Snitzer */ 139949add496SChristoph Hellwig bio_init(&flush_bio, ci->io->md->disk->part0, NULL, 0, 140049add496SChristoph Hellwig REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC); 140147d95102SChristoph Hellwig 1402828678b8SMike Snitzer ci->bio = &flush_bio; 1403828678b8SMike Snitzer ci->sector_count = 0; 140492b914e2SShin'ichiro Kawasaki ci->io->tio.clone.bi_iter.bi_size = 0; 1405f9ab94ceSMikulas Patocka 1406f9ab94ceSMikulas Patocka while ((ti = dm_table_get_target(ci->map, target_nr++))) 14071dd40c3eSMikulas Patocka __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL); 1408828678b8SMike Snitzer 1409828678b8SMike Snitzer bio_uninit(ci->bio); 1410f9ab94ceSMikulas Patocka } 1411f9ab94ceSMikulas Patocka 1412e6fc9f62SMike Snitzer static void __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti, 141361697a6aSMike Snitzer unsigned num_bios) 14145ae89a87SMike Snitzer { 141551b86f9aSMichael Lass unsigned len; 14165ae89a87SMike Snitzer 14173720281dSMike Snitzer len = min_t(sector_t, ci->sector_count, 14183720281dSMike Snitzer max_io_len_target_boundary(ti, dm_target_offset(ti, ci->sector))); 141951b86f9aSMichael Lass 14207dd06a25SMike Snitzer __send_duplicate_bios(ci, ti, num_bios, &len); 14217dd06a25SMike Snitzer 1422a79245b3SMike Snitzer ci->sector += len; 14233d7f4562SMike Snitzer ci->sector_count -= len; 14245ae89a87SMike Snitzer } 14255ae89a87SMike Snitzer 1426568c73a3SMike Snitzer static bool is_abnormal_io(struct bio *bio) 1427568c73a3SMike Snitzer { 1428568c73a3SMike Snitzer bool r = false; 1429568c73a3SMike Snitzer 1430568c73a3SMike Snitzer switch (bio_op(bio)) { 1431568c73a3SMike Snitzer case REQ_OP_DISCARD: 1432568c73a3SMike Snitzer case REQ_OP_SECURE_ERASE: 1433568c73a3SMike Snitzer case REQ_OP_WRITE_ZEROES: 1434568c73a3SMike Snitzer r = true; 1435568c73a3SMike Snitzer break; 1436568c73a3SMike Snitzer } 1437568c73a3SMike Snitzer 1438568c73a3SMike Snitzer return r; 1439568c73a3SMike Snitzer } 1440568c73a3SMike Snitzer 14410519c71eSMike Snitzer static bool __process_abnormal_io(struct clone_info *ci, struct dm_target *ti, 144284b98f4cSMike Snitzer blk_status_t *status) 14430519c71eSMike Snitzer { 14449679b5a7SMike Snitzer unsigned num_bios = 0; 14450519c71eSMike Snitzer 1446e6fc9f62SMike Snitzer switch (bio_op(ci->bio)) { 14479679b5a7SMike Snitzer case REQ_OP_DISCARD: 14489679b5a7SMike Snitzer num_bios = ti->num_discard_bios; 14499679b5a7SMike Snitzer break; 14509679b5a7SMike Snitzer case REQ_OP_SECURE_ERASE: 14519679b5a7SMike Snitzer num_bios = ti->num_secure_erase_bios; 14529679b5a7SMike Snitzer break; 14539679b5a7SMike Snitzer case REQ_OP_WRITE_ZEROES: 14549679b5a7SMike Snitzer num_bios = ti->num_write_zeroes_bios; 14559679b5a7SMike Snitzer break; 14569679b5a7SMike Snitzer default: 14570519c71eSMike Snitzer return false; 14589679b5a7SMike Snitzer } 14590519c71eSMike Snitzer 1460e6fc9f62SMike Snitzer /* 1461e6fc9f62SMike Snitzer * Even though the device advertised support for this type of 1462e6fc9f62SMike Snitzer * request, that does not mean every target supports it, and 1463e6fc9f62SMike Snitzer * reconfiguration might also have changed that since the 1464e6fc9f62SMike Snitzer * check was performed. 1465e6fc9f62SMike Snitzer */ 146684b98f4cSMike Snitzer if (unlikely(!num_bios)) 146784b98f4cSMike Snitzer *status = BLK_STS_NOTSUPP; 1468e6fc9f62SMike Snitzer else { 1469e6fc9f62SMike Snitzer __send_changing_extent_only(ci, ti, num_bios); 147084b98f4cSMike Snitzer *status = BLK_STS_OK; 1471e6fc9f62SMike Snitzer } 14720519c71eSMike Snitzer return true; 14730519c71eSMike Snitzer } 14740519c71eSMike Snitzer 1475e4c93811SAlasdair G Kergon /* 1476b99fdcdcSMing Lei * Reuse ->bi_private as hlist head for storing all dm_io instances 1477b99fdcdcSMing Lei * associated with this bio, and this bio's bi_private needs to be 1478b99fdcdcSMing Lei * stored in dm_io->data before the reuse. 1479b99fdcdcSMing Lei * 1480b99fdcdcSMing Lei * bio->bi_private is owned by fs or upper layer, so block layer won't 1481b99fdcdcSMing Lei * touch it after splitting. Meantime it won't be changed by anyone after 1482b99fdcdcSMing Lei * bio is submitted. So this reuse is safe. 1483b99fdcdcSMing Lei */ 1484b99fdcdcSMing Lei static inline struct hlist_head *dm_get_bio_hlist_head(struct bio *bio) 1485b99fdcdcSMing Lei { 1486b99fdcdcSMing Lei return (struct hlist_head *)&bio->bi_private; 1487b99fdcdcSMing Lei } 1488b99fdcdcSMing Lei 1489b99fdcdcSMing Lei static void dm_queue_poll_io(struct bio *bio, struct dm_io *io) 1490b99fdcdcSMing Lei { 1491b99fdcdcSMing Lei struct hlist_head *head = dm_get_bio_hlist_head(bio); 1492b99fdcdcSMing Lei 1493b99fdcdcSMing Lei if (!(bio->bi_opf & REQ_DM_POLL_LIST)) { 1494b99fdcdcSMing Lei bio->bi_opf |= REQ_DM_POLL_LIST; 1495b99fdcdcSMing Lei /* 1496b99fdcdcSMing Lei * Save .bi_private into dm_io, so that we can reuse 1497b99fdcdcSMing Lei * .bi_private as hlist head for storing dm_io list 1498b99fdcdcSMing Lei */ 1499b99fdcdcSMing Lei io->data = bio->bi_private; 1500b99fdcdcSMing Lei 1501b99fdcdcSMing Lei INIT_HLIST_HEAD(head); 1502b99fdcdcSMing Lei 1503b99fdcdcSMing Lei /* tell block layer to poll for completion */ 1504b99fdcdcSMing Lei bio->bi_cookie = ~BLK_QC_T_NONE; 1505b99fdcdcSMing Lei } else { 1506b99fdcdcSMing Lei /* 1507b99fdcdcSMing Lei * bio recursed due to split, reuse original poll list, 1508b99fdcdcSMing Lei * and save bio->bi_private too. 1509b99fdcdcSMing Lei */ 1510b99fdcdcSMing Lei io->data = hlist_entry(head->first, struct dm_io, node)->data; 1511b99fdcdcSMing Lei } 1512b99fdcdcSMing Lei 1513b99fdcdcSMing Lei hlist_add_head(&io->node, head); 1514b99fdcdcSMing Lei } 1515b99fdcdcSMing Lei 1516b99fdcdcSMing Lei /* 1517e4c93811SAlasdair G Kergon * Select the correct strategy for processing a non-flush bio. 1518e4c93811SAlasdair G Kergon */ 151984b98f4cSMike Snitzer static blk_status_t __split_and_process_bio(struct clone_info *ci) 1520e4c93811SAlasdair G Kergon { 152166bdaa43SMike Snitzer struct bio *clone; 1522e4c93811SAlasdair G Kergon struct dm_target *ti; 15231c3b13e6SKent Overstreet unsigned len; 152484b98f4cSMike Snitzer blk_status_t error = BLK_STS_IOERR; 1525e4c93811SAlasdair G Kergon 1526e4c93811SAlasdair G Kergon ti = dm_table_find_target(ci->map, ci->sector); 152784b98f4cSMike Snitzer if (unlikely(!ti || __process_abnormal_io(ci, ti, &error))) 152884b98f4cSMike Snitzer return error; 15293d7f4562SMike Snitzer 1530b99fdcdcSMing Lei /* 1531b99fdcdcSMing Lei * Only support bio polling for normal IO, and the target io is 1532b99fdcdcSMing Lei * exactly inside the dm_io instance (verified in dm_poll_dm_io) 1533b99fdcdcSMing Lei */ 1534b99fdcdcSMing Lei ci->submit_as_polled = ci->bio->bi_opf & REQ_POLLED; 1535e4c93811SAlasdair G Kergon 1536e4c93811SAlasdair G Kergon len = min_t(sector_t, max_io_len(ti, ci->sector), ci->sector_count); 153766bdaa43SMike Snitzer clone = alloc_tio(ci, ti, 0, &len, GFP_NOIO); 153866bdaa43SMike Snitzer __map_bio(clone); 1539e4c93811SAlasdair G Kergon 1540e4c93811SAlasdair G Kergon ci->sector += len; 1541e4c93811SAlasdair G Kergon ci->sector_count -= len; 1542e4c93811SAlasdair G Kergon 154384b98f4cSMike Snitzer return BLK_STS_OK; 1544e4c93811SAlasdair G Kergon } 1545e4c93811SAlasdair G Kergon 1546978e51baSMike Snitzer static void init_clone_info(struct clone_info *ci, struct mapped_device *md, 1547978e51baSMike Snitzer struct dm_table *map, struct bio *bio) 1548978e51baSMike Snitzer { 1549978e51baSMike Snitzer ci->map = map; 1550978e51baSMike Snitzer ci->io = alloc_io(md, bio); 1551d41e077aSMike Snitzer ci->bio = bio; 1552b99fdcdcSMing Lei ci->submit_as_polled = false; 1553978e51baSMike Snitzer ci->sector = bio->bi_iter.bi_sector; 1554d41e077aSMike Snitzer ci->sector_count = bio_sectors(bio); 1555d41e077aSMike Snitzer 1556d41e077aSMike Snitzer /* Shouldn't happen but sector_count was being set to 0 so... */ 1557d41e077aSMike Snitzer if (WARN_ON_ONCE(op_is_zone_mgmt(bio_op(bio)) && ci->sector_count)) 1558d41e077aSMike Snitzer ci->sector_count = 0; 1559978e51baSMike Snitzer } 1560978e51baSMike Snitzer 1561e4c93811SAlasdair G Kergon /* 156214fe594dSAlasdair G Kergon * Entry point to split a bio into clones and submit them to the targets. 15631da177e4SLinus Torvalds */ 156496c9865cSMike Snitzer static void dm_split_and_process_bio(struct mapped_device *md, 156583d5e5b0SMikulas Patocka struct dm_table *map, struct bio *bio) 15661da177e4SLinus Torvalds { 15671da177e4SLinus Torvalds struct clone_info ci; 1568*4857abf6SMike Snitzer struct dm_io *io; 15690fbb4d93SMike Snitzer struct bio *orig_bio = NULL; 157084b98f4cSMike Snitzer blk_status_t error = BLK_STS_OK; 15711da177e4SLinus Torvalds 1572978e51baSMike Snitzer init_clone_info(&ci, md, map, bio); 1573*4857abf6SMike Snitzer io = ci.io; 1574bd2a49b8SAlasdair G Kergon 15751eff9d32SJens Axboe if (bio->bi_opf & REQ_PREFLUSH) { 1576332f2b1eSMike Snitzer __send_empty_flush(&ci); 1577e2736347SMike Snitzer /* dm_io_complete submits any data associated with flush */ 1578d41e077aSMike Snitzer goto out; 1579d41e077aSMike Snitzer } 1580d41e077aSMike Snitzer 158196c9865cSMike Snitzer error = __split_and_process_bio(&ci); 1582*4857abf6SMike Snitzer io->map_task = NULL; 1583d41e077aSMike Snitzer if (error || !ci.sector_count) 1584d41e077aSMike Snitzer goto out; 1585d41e077aSMike Snitzer 158618a25da8SNeilBrown /* 1587d41e077aSMike Snitzer * Remainder must be passed to submit_bio_noacct() so it gets handled 1588d41e077aSMike Snitzer * *after* bios already submitted have been completely processed. 1589*4857abf6SMike Snitzer * We take a clone of the original to store in io->orig_bio to be 1590e2736347SMike Snitzer * used by dm_end_io_acct() and for dm_io_complete() to use for 1591d41e077aSMike Snitzer * completion handling. 159218a25da8SNeilBrown */ 15930fbb4d93SMike Snitzer orig_bio = bio_split(bio, bio_sectors(bio) - ci.sector_count, 1594f21c601aSMike Snitzer GFP_NOIO, &md->queue->bio_split); 15950fbb4d93SMike Snitzer bio_chain(orig_bio, bio); 15960fbb4d93SMike Snitzer trace_block_split(orig_bio, bio->bi_iter.bi_sector); 15973e08773cSChristoph Hellwig submit_bio_noacct(bio); 1598d41e077aSMike Snitzer out: 15990fbb4d93SMike Snitzer if (!orig_bio) 16000fbb4d93SMike Snitzer orig_bio = bio; 1601*4857abf6SMike Snitzer smp_store_release(&io->orig_bio, orig_bio); 1602*4857abf6SMike Snitzer if (dm_io_flagged(io, DM_IO_START_ACCT)) 1603*4857abf6SMike Snitzer dm_start_io_acct(io, NULL); 16041da177e4SLinus Torvalds 1605b99fdcdcSMing Lei /* 1606b99fdcdcSMing Lei * Drop the extra reference count for non-POLLED bio, and hold one 1607b99fdcdcSMing Lei * reference for POLLED bio, which will be released in dm_poll_bio 1608b99fdcdcSMing Lei * 1609b99fdcdcSMing Lei * Add every dm_io instance into the hlist_head which is stored in 1610b99fdcdcSMing Lei * bio->bi_private, so that dm_poll_bio can poll them all. 1611b99fdcdcSMing Lei */ 1612b99fdcdcSMing Lei if (error || !ci.submit_as_polled) 161384b98f4cSMike Snitzer dm_io_dec_pending(ci.io, error); 1614b99fdcdcSMing Lei else 1615*4857abf6SMike Snitzer dm_queue_poll_io(bio, io); 16161da177e4SLinus Torvalds } 16171da177e4SLinus Torvalds 16183e08773cSChristoph Hellwig static void dm_submit_bio(struct bio *bio) 16191da177e4SLinus Torvalds { 1620309dca30SChristoph Hellwig struct mapped_device *md = bio->bi_bdev->bd_disk->private_data; 162183d5e5b0SMikulas Patocka int srcu_idx; 162283d5e5b0SMikulas Patocka struct dm_table *map; 16231da177e4SLinus Torvalds 162483d5e5b0SMikulas Patocka map = dm_get_live_table(md, &srcu_idx); 16256a8736d1STejun Heo 1626fa247089SMike Snitzer /* If suspended, or map not yet available, queue this IO for later */ 1627fa247089SMike Snitzer if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) || 1628fa247089SMike Snitzer unlikely(!map)) { 16296abc4946SKonstantin Khlebnikov if (bio->bi_opf & REQ_NOWAIT) 16306abc4946SKonstantin Khlebnikov bio_wouldblock_error(bio); 1631b2abdb1bSMike Snitzer else if (bio->bi_opf & REQ_RAHEAD) 16326a8736d1STejun Heo bio_io_error(bio); 1633b2abdb1bSMike Snitzer else 1634b2abdb1bSMike Snitzer queue_io(md, bio); 1635b2abdb1bSMike Snitzer goto out; 16361da177e4SLinus Torvalds } 16371da177e4SLinus Torvalds 1638b2abdb1bSMike Snitzer /* 1639b2abdb1bSMike Snitzer * Use blk_queue_split() for abnormal IO (e.g. discard, writesame, etc) 1640b2abdb1bSMike Snitzer * otherwise associated queue_limits won't be imposed. 1641b2abdb1bSMike Snitzer */ 1642b2abdb1bSMike Snitzer if (is_abnormal_io(bio)) 1643b2abdb1bSMike Snitzer blk_queue_split(&bio); 1644978e51baSMike Snitzer 164596c9865cSMike Snitzer dm_split_and_process_bio(md, map, bio); 1646b2abdb1bSMike Snitzer out: 164783d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 1648978e51baSMike Snitzer } 1649978e51baSMike Snitzer 1650b99fdcdcSMing Lei static bool dm_poll_dm_io(struct dm_io *io, struct io_comp_batch *iob, 1651b99fdcdcSMing Lei unsigned int flags) 1652b99fdcdcSMing Lei { 1653655f3aadSMike Snitzer WARN_ON_ONCE(!dm_tio_is_normal(&io->tio)); 1654b99fdcdcSMing Lei 1655b99fdcdcSMing Lei /* don't poll if the mapped io is done */ 1656b99fdcdcSMing Lei if (atomic_read(&io->io_count) > 1) 1657b99fdcdcSMing Lei bio_poll(&io->tio.clone, iob, flags); 1658b99fdcdcSMing Lei 1659b99fdcdcSMing Lei /* bio_poll holds the last reference */ 1660b99fdcdcSMing Lei return atomic_read(&io->io_count) == 1; 1661b99fdcdcSMing Lei } 1662b99fdcdcSMing Lei 1663b99fdcdcSMing Lei static int dm_poll_bio(struct bio *bio, struct io_comp_batch *iob, 1664b99fdcdcSMing Lei unsigned int flags) 1665b99fdcdcSMing Lei { 1666b99fdcdcSMing Lei struct hlist_head *head = dm_get_bio_hlist_head(bio); 1667b99fdcdcSMing Lei struct hlist_head tmp = HLIST_HEAD_INIT; 1668b99fdcdcSMing Lei struct hlist_node *next; 1669b99fdcdcSMing Lei struct dm_io *io; 1670b99fdcdcSMing Lei 1671b99fdcdcSMing Lei /* Only poll normal bio which was marked as REQ_DM_POLL_LIST */ 1672b99fdcdcSMing Lei if (!(bio->bi_opf & REQ_DM_POLL_LIST)) 1673b99fdcdcSMing Lei return 0; 1674b99fdcdcSMing Lei 1675b99fdcdcSMing Lei WARN_ON_ONCE(hlist_empty(head)); 1676b99fdcdcSMing Lei 1677b99fdcdcSMing Lei hlist_move_list(head, &tmp); 1678b99fdcdcSMing Lei 1679b99fdcdcSMing Lei /* 1680b99fdcdcSMing Lei * Restore .bi_private before possibly completing dm_io. 1681b99fdcdcSMing Lei * 1682b99fdcdcSMing Lei * bio_poll() is only possible once @bio has been completely 1683b99fdcdcSMing Lei * submitted via submit_bio_noacct()'s depth-first submission. 1684b99fdcdcSMing Lei * So there is no dm_queue_poll_io() race associated with 1685b99fdcdcSMing Lei * clearing REQ_DM_POLL_LIST here. 1686b99fdcdcSMing Lei */ 1687b99fdcdcSMing Lei bio->bi_opf &= ~REQ_DM_POLL_LIST; 1688b99fdcdcSMing Lei bio->bi_private = hlist_entry(tmp.first, struct dm_io, node)->data; 1689b99fdcdcSMing Lei 1690b99fdcdcSMing Lei hlist_for_each_entry_safe(io, next, &tmp, node) { 1691b99fdcdcSMing Lei if (dm_poll_dm_io(io, iob, flags)) { 1692b99fdcdcSMing Lei hlist_del_init(&io->node); 1693b99fdcdcSMing Lei /* 169484b98f4cSMike Snitzer * clone_endio() has already occurred, so no 169584b98f4cSMike Snitzer * error handling is needed here. 1696b99fdcdcSMing Lei */ 169784b98f4cSMike Snitzer __dm_io_dec_pending(io); 1698b99fdcdcSMing Lei } 1699b99fdcdcSMing Lei } 1700b99fdcdcSMing Lei 1701b99fdcdcSMing Lei /* Not done? */ 1702b99fdcdcSMing Lei if (!hlist_empty(&tmp)) { 1703b99fdcdcSMing Lei bio->bi_opf |= REQ_DM_POLL_LIST; 1704b99fdcdcSMing Lei /* Reset bio->bi_private to dm_io list head */ 1705b99fdcdcSMing Lei hlist_move_list(&tmp, head); 1706b99fdcdcSMing Lei return 0; 1707b99fdcdcSMing Lei } 1708b99fdcdcSMing Lei return 1; 1709b99fdcdcSMing Lei } 1710b99fdcdcSMing Lei 17111da177e4SLinus Torvalds /*----------------------------------------------------------------- 17121da177e4SLinus Torvalds * An IDR is used to keep track of allocated minor numbers. 17131da177e4SLinus Torvalds *---------------------------------------------------------------*/ 17142b06cfffSAlasdair G Kergon static void free_minor(int minor) 17151da177e4SLinus Torvalds { 1716f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 17171da177e4SLinus Torvalds idr_remove(&_minor_idr, minor); 1718f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 17191da177e4SLinus Torvalds } 17201da177e4SLinus Torvalds 17211da177e4SLinus Torvalds /* 17221da177e4SLinus Torvalds * See if the device with a specific minor # is free. 17231da177e4SLinus Torvalds */ 1724cf13ab8eSFrederik Deweerdt static int specific_minor(int minor) 17251da177e4SLinus Torvalds { 1726c9d76be6STejun Heo int r; 17271da177e4SLinus Torvalds 17281da177e4SLinus Torvalds if (minor >= (1 << MINORBITS)) 17291da177e4SLinus Torvalds return -EINVAL; 17301da177e4SLinus Torvalds 1731c9d76be6STejun Heo idr_preload(GFP_KERNEL); 1732f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 17331da177e4SLinus Torvalds 1734c9d76be6STejun Heo r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT); 17351da177e4SLinus Torvalds 1736f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1737c9d76be6STejun Heo idr_preload_end(); 1738c9d76be6STejun Heo if (r < 0) 1739c9d76be6STejun Heo return r == -ENOSPC ? -EBUSY : r; 1740c9d76be6STejun Heo return 0; 17411da177e4SLinus Torvalds } 17421da177e4SLinus Torvalds 1743cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor) 17441da177e4SLinus Torvalds { 1745c9d76be6STejun Heo int r; 17461da177e4SLinus Torvalds 1747c9d76be6STejun Heo idr_preload(GFP_KERNEL); 1748f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 17491da177e4SLinus Torvalds 1750c9d76be6STejun Heo r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT); 17511da177e4SLinus Torvalds 1752f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1753c9d76be6STejun Heo idr_preload_end(); 1754c9d76be6STejun Heo if (r < 0) 17551da177e4SLinus Torvalds return r; 1756c9d76be6STejun Heo *minor = r; 1757c9d76be6STejun Heo return 0; 17581da177e4SLinus Torvalds } 17591da177e4SLinus Torvalds 176083d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops; 1761681cc5e8SMike Snitzer static const struct block_device_operations dm_rq_blk_dops; 1762f26c5719SDan Williams static const struct dax_operations dm_dax_ops; 17631da177e4SLinus Torvalds 176453d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work); 176553d5914fSMikulas Patocka 1766aa6ce87aSSatya Tangirala #ifdef CONFIG_BLK_INLINE_ENCRYPTION 1767cb77cb5aSEric Biggers static void dm_queue_destroy_crypto_profile(struct request_queue *q) 1768aa6ce87aSSatya Tangirala { 1769cb77cb5aSEric Biggers dm_destroy_crypto_profile(q->crypto_profile); 1770aa6ce87aSSatya Tangirala } 1771aa6ce87aSSatya Tangirala 1772aa6ce87aSSatya Tangirala #else /* CONFIG_BLK_INLINE_ENCRYPTION */ 1773aa6ce87aSSatya Tangirala 1774cb77cb5aSEric Biggers static inline void dm_queue_destroy_crypto_profile(struct request_queue *q) 1775aa6ce87aSSatya Tangirala { 1776aa6ce87aSSatya Tangirala } 1777aa6ce87aSSatya Tangirala #endif /* !CONFIG_BLK_INLINE_ENCRYPTION */ 1778aa6ce87aSSatya Tangirala 17790f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md) 17800f20972fSMike Snitzer { 17810f20972fSMike Snitzer if (md->wq) 17820f20972fSMike Snitzer destroy_workqueue(md->wq); 17836f1c819cSKent Overstreet bioset_exit(&md->bs); 17846f1c819cSKent Overstreet bioset_exit(&md->io_bs); 17850f20972fSMike Snitzer 1786f26c5719SDan Williams if (md->dax_dev) { 1787fb08a190SChristoph Hellwig dax_remove_host(md->disk); 1788f26c5719SDan Williams kill_dax(md->dax_dev); 1789f26c5719SDan Williams put_dax(md->dax_dev); 1790f26c5719SDan Williams md->dax_dev = NULL; 1791f26c5719SDan Williams } 1792f26c5719SDan Williams 1793588b7f5dSKirill Tkhai dm_cleanup_zoned_dev(md); 17940f20972fSMike Snitzer if (md->disk) { 17950f20972fSMike Snitzer spin_lock(&_minor_lock); 17960f20972fSMike Snitzer md->disk->private_data = NULL; 17970f20972fSMike Snitzer spin_unlock(&_minor_lock); 179889f871afSChristoph Hellwig if (dm_get_md_type(md) != DM_TYPE_NONE) { 179989f871afSChristoph Hellwig dm_sysfs_exit(md); 18000f20972fSMike Snitzer del_gendisk(md->disk); 180189f871afSChristoph Hellwig } 1802cb77cb5aSEric Biggers dm_queue_destroy_crypto_profile(md->queue); 180374fe6ba9SChristoph Hellwig blk_cleanup_disk(md->disk); 180474a2b6ecSChristoph Hellwig } 18050f20972fSMike Snitzer 18069f6dc633SMike Snitzer if (md->pending_io) { 18079f6dc633SMike Snitzer free_percpu(md->pending_io); 18089f6dc633SMike Snitzer md->pending_io = NULL; 18099f6dc633SMike Snitzer } 18109f6dc633SMike Snitzer 1811d09960b0STahsin Erdogan cleanup_srcu_struct(&md->io_barrier); 1812d09960b0STahsin Erdogan 1813d5ffebddSMike Snitzer mutex_destroy(&md->suspend_lock); 1814d5ffebddSMike Snitzer mutex_destroy(&md->type_lock); 1815d5ffebddSMike Snitzer mutex_destroy(&md->table_devices_lock); 1816a666e5c0SMikulas Patocka mutex_destroy(&md->swap_bios_lock); 1817d5ffebddSMike Snitzer 18184cc96131SMike Snitzer dm_mq_cleanup_mapped_device(md); 18190f20972fSMike Snitzer } 18200f20972fSMike Snitzer 18211da177e4SLinus Torvalds /* 18221da177e4SLinus Torvalds * Allocate and initialise a blank device with a given minor. 18231da177e4SLinus Torvalds */ 18242b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor) 18251da177e4SLinus Torvalds { 1826115485e8SMike Snitzer int r, numa_node_id = dm_get_numa_node(); 1827115485e8SMike Snitzer struct mapped_device *md; 1828ba61fdd1SJeff Mahoney void *old_md; 18291da177e4SLinus Torvalds 1830856eb091SMikulas Patocka md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id); 18311da177e4SLinus Torvalds if (!md) { 18321da177e4SLinus Torvalds DMWARN("unable to allocate device, out of memory."); 18331da177e4SLinus Torvalds return NULL; 18341da177e4SLinus Torvalds } 18351da177e4SLinus Torvalds 183610da4f79SJeff Mahoney if (!try_module_get(THIS_MODULE)) 18376ed7ade8SMilan Broz goto bad_module_get; 183810da4f79SJeff Mahoney 18391da177e4SLinus Torvalds /* get a minor number for the dev */ 18402b06cfffSAlasdair G Kergon if (minor == DM_ANY_MINOR) 1841cf13ab8eSFrederik Deweerdt r = next_free_minor(&minor); 18422b06cfffSAlasdair G Kergon else 1843cf13ab8eSFrederik Deweerdt r = specific_minor(minor); 18441da177e4SLinus Torvalds if (r < 0) 18456ed7ade8SMilan Broz goto bad_minor; 18461da177e4SLinus Torvalds 184783d5e5b0SMikulas Patocka r = init_srcu_struct(&md->io_barrier); 184883d5e5b0SMikulas Patocka if (r < 0) 184983d5e5b0SMikulas Patocka goto bad_io_barrier; 185083d5e5b0SMikulas Patocka 1851115485e8SMike Snitzer md->numa_node_id = numa_node_id; 1852591ddcfcSMike Snitzer md->init_tio_pdu = false; 1853a5664dadSMike Snitzer md->type = DM_TYPE_NONE; 1854e61290a4SDaniel Walker mutex_init(&md->suspend_lock); 1855a5664dadSMike Snitzer mutex_init(&md->type_lock); 185686f1152bSBenjamin Marzinski mutex_init(&md->table_devices_lock); 1857022c2611SMikulas Patocka spin_lock_init(&md->deferred_lock); 18581da177e4SLinus Torvalds atomic_set(&md->holders, 1); 18595c6bd75dSAlasdair G Kergon atomic_set(&md->open_count, 0); 18601da177e4SLinus Torvalds atomic_set(&md->event_nr, 0); 18617a8c3d3bSMike Anderson atomic_set(&md->uevent_seq, 0); 18627a8c3d3bSMike Anderson INIT_LIST_HEAD(&md->uevent_list); 186386f1152bSBenjamin Marzinski INIT_LIST_HEAD(&md->table_devices); 18647a8c3d3bSMike Anderson spin_lock_init(&md->uevent_lock); 18651da177e4SLinus Torvalds 186647ace7e0SMike Snitzer /* 1867c62b37d9SChristoph Hellwig * default to bio-based until DM table is loaded and md->type 1868c62b37d9SChristoph Hellwig * established. If request-based table is loaded: blk-mq will 1869c62b37d9SChristoph Hellwig * override accordingly. 187047ace7e0SMike Snitzer */ 187174fe6ba9SChristoph Hellwig md->disk = blk_alloc_disk(md->numa_node_id); 18721da177e4SLinus Torvalds if (!md->disk) 18730f20972fSMike Snitzer goto bad; 187474fe6ba9SChristoph Hellwig md->queue = md->disk->queue; 18751da177e4SLinus Torvalds 1876f0b04115SJeff Mahoney init_waitqueue_head(&md->wait); 187753d5914fSMikulas Patocka INIT_WORK(&md->work, dm_wq_work); 1878f0b04115SJeff Mahoney init_waitqueue_head(&md->eventq); 18792995fa78SMikulas Patocka init_completion(&md->kobj_holder.completion); 1880f0b04115SJeff Mahoney 1881a666e5c0SMikulas Patocka md->swap_bios = get_swap_bios(); 1882a666e5c0SMikulas Patocka sema_init(&md->swap_bios_semaphore, md->swap_bios); 1883a666e5c0SMikulas Patocka mutex_init(&md->swap_bios_lock); 1884a666e5c0SMikulas Patocka 18851da177e4SLinus Torvalds md->disk->major = _major; 18861da177e4SLinus Torvalds md->disk->first_minor = minor; 188774fe6ba9SChristoph Hellwig md->disk->minors = 1; 18881ebe2e5fSChristoph Hellwig md->disk->flags |= GENHD_FL_NO_PART; 18891da177e4SLinus Torvalds md->disk->fops = &dm_blk_dops; 18901da177e4SLinus Torvalds md->disk->queue = md->queue; 18911da177e4SLinus Torvalds md->disk->private_data = md; 18921da177e4SLinus Torvalds sprintf(md->disk->disk_name, "dm-%d", minor); 1893f26c5719SDan Williams 18945d2a228bSChristoph Hellwig if (IS_ENABLED(CONFIG_FS_DAX)) { 189530c6828aSChristoph Hellwig md->dax_dev = alloc_dax(md, &dm_dax_ops); 1896d7519392SChristoph Hellwig if (IS_ERR(md->dax_dev)) { 1897d7519392SChristoph Hellwig md->dax_dev = NULL; 1898f26c5719SDan Williams goto bad; 1899976431b0SDan Williams } 19007ac5360cSChristoph Hellwig set_dax_nocache(md->dax_dev); 19017ac5360cSChristoph Hellwig set_dax_nomc(md->dax_dev); 1902fb08a190SChristoph Hellwig if (dax_add_host(md->dax_dev, md->disk)) 1903f26c5719SDan Williams goto bad; 1904f26c5719SDan Williams } 19051da177e4SLinus Torvalds 19067e51f257SMike Anderson format_dev_t(md->name, MKDEV(_major, minor)); 19071da177e4SLinus Torvalds 1908c7c879eeSMichał Mirosław md->wq = alloc_workqueue("kdmflush/%s", WQ_MEM_RECLAIM, 0, md->name); 1909304f3f6aSMilan Broz if (!md->wq) 19100f20972fSMike Snitzer goto bad; 1911304f3f6aSMilan Broz 19129f6dc633SMike Snitzer md->pending_io = alloc_percpu(unsigned long); 19139f6dc633SMike Snitzer if (!md->pending_io) 19149f6dc633SMike Snitzer goto bad; 19159f6dc633SMike Snitzer 1916fd2ed4d2SMikulas Patocka dm_stats_init(&md->stats); 1917fd2ed4d2SMikulas Patocka 1918ba61fdd1SJeff Mahoney /* Populate the mapping, nobody knows we exist yet */ 1919f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 1920ba61fdd1SJeff Mahoney old_md = idr_replace(&_minor_idr, md, minor); 1921f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1922ba61fdd1SJeff Mahoney 1923ba61fdd1SJeff Mahoney BUG_ON(old_md != MINOR_ALLOCED); 1924ba61fdd1SJeff Mahoney 19251da177e4SLinus Torvalds return md; 19261da177e4SLinus Torvalds 19270f20972fSMike Snitzer bad: 19280f20972fSMike Snitzer cleanup_mapped_device(md); 192983d5e5b0SMikulas Patocka bad_io_barrier: 19301da177e4SLinus Torvalds free_minor(minor); 19316ed7ade8SMilan Broz bad_minor: 193210da4f79SJeff Mahoney module_put(THIS_MODULE); 19336ed7ade8SMilan Broz bad_module_get: 1934856eb091SMikulas Patocka kvfree(md); 19351da177e4SLinus Torvalds return NULL; 19361da177e4SLinus Torvalds } 19371da177e4SLinus Torvalds 1938ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md); 1939ae9da83fSJun'ichi Nomura 19401da177e4SLinus Torvalds static void free_dev(struct mapped_device *md) 19411da177e4SLinus Torvalds { 1942f331c029STejun Heo int minor = MINOR(disk_devt(md->disk)); 194363d94e48SJun'ichi Nomura 1944ae9da83fSJun'ichi Nomura unlock_fs(md); 19452eb6e1e3SKeith Busch 19460f20972fSMike Snitzer cleanup_mapped_device(md); 19470f20972fSMike Snitzer 19480f20972fSMike Snitzer free_table_devices(&md->table_devices); 19490f20972fSMike Snitzer dm_stats_cleanup(&md->stats); 195063a4f065SMike Snitzer free_minor(minor); 195163a4f065SMike Snitzer 195210da4f79SJeff Mahoney module_put(THIS_MODULE); 1953856eb091SMikulas Patocka kvfree(md); 19541da177e4SLinus Torvalds } 19551da177e4SLinus Torvalds 19562a2a4c51SJens Axboe static int __bind_mempools(struct mapped_device *md, struct dm_table *t) 1957e6ee8c0bSKiyoshi Ueda { 1958c0820cf5SMikulas Patocka struct dm_md_mempools *p = dm_table_get_md_mempools(t); 19592a2a4c51SJens Axboe int ret = 0; 1960e6ee8c0bSKiyoshi Ueda 1961545ed20eSToshi Kani if (dm_table_bio_based(t)) { 1962c0820cf5SMikulas Patocka /* 196364f52b0eSMike Snitzer * The md may already have mempools that need changing. 196464f52b0eSMike Snitzer * If so, reload bioset because front_pad may have changed 196516245bdcSJun'ichi Nomura * because a different table was loaded. 1966c0820cf5SMikulas Patocka */ 19676f1c819cSKent Overstreet bioset_exit(&md->bs); 19686f1c819cSKent Overstreet bioset_exit(&md->io_bs); 19690776aa0eSMike Snitzer 19706f1c819cSKent Overstreet } else if (bioset_initialized(&md->bs)) { 1971cbc4e3c1SMike Snitzer /* 19724e6e36c3SMike Snitzer * There's no need to reload with request-based dm 19734e6e36c3SMike Snitzer * because the size of front_pad doesn't change. 19744e6e36c3SMike Snitzer * Note for future: If you are to reload bioset, 19754e6e36c3SMike Snitzer * prep-ed requests in the queue may refer 19764e6e36c3SMike Snitzer * to bio from the old bioset, so you must walk 19774e6e36c3SMike Snitzer * through the queue to unprep. 1978cbc4e3c1SMike Snitzer */ 1979cbc4e3c1SMike Snitzer goto out; 1980cbc4e3c1SMike Snitzer } 1981cbc4e3c1SMike Snitzer 19826f1c819cSKent Overstreet BUG_ON(!p || 19836f1c819cSKent Overstreet bioset_initialized(&md->bs) || 19846f1c819cSKent Overstreet bioset_initialized(&md->io_bs)); 1985e6ee8c0bSKiyoshi Ueda 19862a2a4c51SJens Axboe ret = bioset_init_from_src(&md->bs, &p->bs); 19872a2a4c51SJens Axboe if (ret) 19882a2a4c51SJens Axboe goto out; 19892a2a4c51SJens Axboe ret = bioset_init_from_src(&md->io_bs, &p->io_bs); 19902a2a4c51SJens Axboe if (ret) 19912a2a4c51SJens Axboe bioset_exit(&md->bs); 1992e6ee8c0bSKiyoshi Ueda out: 199302233342SMike Snitzer /* mempool bind completed, no longer need any mempools in the table */ 1994e6ee8c0bSKiyoshi Ueda dm_table_free_md_mempools(t); 19952a2a4c51SJens Axboe return ret; 1996e6ee8c0bSKiyoshi Ueda } 1997e6ee8c0bSKiyoshi Ueda 19981da177e4SLinus Torvalds /* 19991da177e4SLinus Torvalds * Bind a table to the device. 20001da177e4SLinus Torvalds */ 20011da177e4SLinus Torvalds static void event_callback(void *context) 20021da177e4SLinus Torvalds { 20037a8c3d3bSMike Anderson unsigned long flags; 20047a8c3d3bSMike Anderson LIST_HEAD(uevents); 20051da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) context; 20061da177e4SLinus Torvalds 20077a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 20087a8c3d3bSMike Anderson list_splice_init(&md->uevent_list, &uevents); 20097a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 20107a8c3d3bSMike Anderson 2011ed9e1982STejun Heo dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); 20127a8c3d3bSMike Anderson 20131da177e4SLinus Torvalds atomic_inc(&md->event_nr); 20141da177e4SLinus Torvalds wake_up(&md->eventq); 201562e08243SMikulas Patocka dm_issue_global_event(); 20161da177e4SLinus Torvalds } 20171da177e4SLinus Torvalds 2018c217649bSMike Snitzer /* 2019042d2a9bSAlasdair G Kergon * Returns old map, which caller must destroy. 2020042d2a9bSAlasdair G Kergon */ 2021042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t, 2022754c5fc7SMike Snitzer struct queue_limits *limits) 20231da177e4SLinus Torvalds { 2024042d2a9bSAlasdair G Kergon struct dm_table *old_map; 20251da177e4SLinus Torvalds sector_t size; 20262a2a4c51SJens Axboe int ret; 20271da177e4SLinus Torvalds 20285a8f1f80SBart Van Assche lockdep_assert_held(&md->suspend_lock); 20295a8f1f80SBart Van Assche 20301da177e4SLinus Torvalds size = dm_table_get_size(t); 20313ac51e74SDarrick J. Wong 20323ac51e74SDarrick J. Wong /* 20333ac51e74SDarrick J. Wong * Wipe any geometry if the size of the table changed. 20343ac51e74SDarrick J. Wong */ 2035fd2ed4d2SMikulas Patocka if (size != dm_get_size(md)) 20363ac51e74SDarrick J. Wong memset(&md->geometry, 0, sizeof(md->geometry)); 20373ac51e74SDarrick J. Wong 20385424a0b8SMikulas Patocka if (!get_capacity(md->disk)) 20395424a0b8SMikulas Patocka set_capacity(md->disk, size); 20405424a0b8SMikulas Patocka else 2041f64d9b2eSChristoph Hellwig set_capacity_and_notify(md->disk, size); 20421da177e4SLinus Torvalds 2043cf222b37SAlasdair G Kergon dm_table_event_callback(t, event_callback, md); 20442ca3310eSAlasdair G Kergon 2045f5b4aee1SMike Snitzer if (dm_table_request_based(t)) { 204616f12266SMike Snitzer /* 20479c37de29SMike Snitzer * Leverage the fact that request-based DM targets are 20489c37de29SMike Snitzer * immutable singletons - used to optimize dm_mq_queue_rq. 204916f12266SMike Snitzer */ 205016f12266SMike Snitzer md->immutable_target = dm_table_get_immutable_target(t); 205116f12266SMike Snitzer } 2052e6ee8c0bSKiyoshi Ueda 20532a2a4c51SJens Axboe ret = __bind_mempools(md, t); 20542a2a4c51SJens Axboe if (ret) { 20552a2a4c51SJens Axboe old_map = ERR_PTR(ret); 20562a2a4c51SJens Axboe goto out; 20572a2a4c51SJens Axboe } 2058e6ee8c0bSKiyoshi Ueda 2059f5b4aee1SMike Snitzer ret = dm_table_set_restrictions(t, md->queue, limits); 2060bb37d772SDamien Le Moal if (ret) { 2061bb37d772SDamien Le Moal old_map = ERR_PTR(ret); 2062bb37d772SDamien Le Moal goto out; 2063bb37d772SDamien Le Moal } 2064bb37d772SDamien Le Moal 2065a12f5d48SEric Dumazet old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 20661d3aa6f6SMike Snitzer rcu_assign_pointer(md->map, (void *)t); 206736a0456fSAlasdair G Kergon md->immutable_target_type = dm_table_get_immutable_target_type(t); 206836a0456fSAlasdair G Kergon 206941abc4e1SHannes Reinecke if (old_map) 207083d5e5b0SMikulas Patocka dm_sync_table(md); 20712a2a4c51SJens Axboe out: 2072042d2a9bSAlasdair G Kergon return old_map; 20731da177e4SLinus Torvalds } 20741da177e4SLinus Torvalds 2075a7940155SAlasdair G Kergon /* 2076a7940155SAlasdair G Kergon * Returns unbound table for the caller to free. 2077a7940155SAlasdair G Kergon */ 2078a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md) 20791da177e4SLinus Torvalds { 2080a12f5d48SEric Dumazet struct dm_table *map = rcu_dereference_protected(md->map, 1); 20811da177e4SLinus Torvalds 20821da177e4SLinus Torvalds if (!map) 2083a7940155SAlasdair G Kergon return NULL; 20841da177e4SLinus Torvalds 20851da177e4SLinus Torvalds dm_table_event_callback(map, NULL, NULL); 20869cdb8520SMonam Agarwal RCU_INIT_POINTER(md->map, NULL); 208783d5e5b0SMikulas Patocka dm_sync_table(md); 2088a7940155SAlasdair G Kergon 2089a7940155SAlasdair G Kergon return map; 20901da177e4SLinus Torvalds } 20911da177e4SLinus Torvalds 20921da177e4SLinus Torvalds /* 20931da177e4SLinus Torvalds * Constructor for a new device. 20941da177e4SLinus Torvalds */ 20952b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result) 20961da177e4SLinus Torvalds { 20971da177e4SLinus Torvalds struct mapped_device *md; 20981da177e4SLinus Torvalds 20992b06cfffSAlasdair G Kergon md = alloc_dev(minor); 21001da177e4SLinus Torvalds if (!md) 21011da177e4SLinus Torvalds return -ENXIO; 21021da177e4SLinus Torvalds 210391ccbbacSTushar Sugandhi dm_ima_reset_data(md); 210491ccbbacSTushar Sugandhi 21051da177e4SLinus Torvalds *result = md; 21061da177e4SLinus Torvalds return 0; 21071da177e4SLinus Torvalds } 21081da177e4SLinus Torvalds 2109a5664dadSMike Snitzer /* 2110a5664dadSMike Snitzer * Functions to manage md->type. 2111a5664dadSMike Snitzer * All are required to hold md->type_lock. 2112a5664dadSMike Snitzer */ 2113a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md) 2114a5664dadSMike Snitzer { 2115a5664dadSMike Snitzer mutex_lock(&md->type_lock); 2116a5664dadSMike Snitzer } 2117a5664dadSMike Snitzer 2118a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md) 2119a5664dadSMike Snitzer { 2120a5664dadSMike Snitzer mutex_unlock(&md->type_lock); 2121a5664dadSMike Snitzer } 2122a5664dadSMike Snitzer 21237e0d574fSBart Van Assche void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type) 2124a5664dadSMike Snitzer { 212500c4fc3bSMike Snitzer BUG_ON(!mutex_is_locked(&md->type_lock)); 2126a5664dadSMike Snitzer md->type = type; 2127a5664dadSMike Snitzer } 2128a5664dadSMike Snitzer 21297e0d574fSBart Van Assche enum dm_queue_mode dm_get_md_type(struct mapped_device *md) 2130a5664dadSMike Snitzer { 2131a5664dadSMike Snitzer return md->type; 2132a5664dadSMike Snitzer } 2133a5664dadSMike Snitzer 213436a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md) 213536a0456fSAlasdair G Kergon { 213636a0456fSAlasdair G Kergon return md->immutable_target_type; 213736a0456fSAlasdair G Kergon } 213836a0456fSAlasdair G Kergon 21394a0b4ddfSMike Snitzer /* 2140f84cb8a4SMike Snitzer * The queue_limits are only valid as long as you have a reference 2141f84cb8a4SMike Snitzer * count on 'md'. 2142f84cb8a4SMike Snitzer */ 2143f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md) 2144f84cb8a4SMike Snitzer { 2145f84cb8a4SMike Snitzer BUG_ON(!atomic_read(&md->holders)); 2146f84cb8a4SMike Snitzer return &md->queue->limits; 2147f84cb8a4SMike Snitzer } 2148f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits); 2149f84cb8a4SMike Snitzer 21504a0b4ddfSMike Snitzer /* 21514a0b4ddfSMike Snitzer * Setup the DM device's queue based on md's type 21524a0b4ddfSMike Snitzer */ 2153591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t) 21544a0b4ddfSMike Snitzer { 2155ba305859SChristoph Hellwig enum dm_queue_mode type = dm_table_get_type(t); 2156c100ec49SMike Snitzer struct queue_limits limits; 2157ba305859SChristoph Hellwig int r; 2158bfebd1cdSMike Snitzer 2159545ed20eSToshi Kani switch (type) { 2160bfebd1cdSMike Snitzer case DM_TYPE_REQUEST_BASED: 2161681cc5e8SMike Snitzer md->disk->fops = &dm_rq_blk_dops; 2162e83068a5SMike Snitzer r = dm_mq_init_request_queue(md, t); 2163bfebd1cdSMike Snitzer if (r) { 2164681cc5e8SMike Snitzer DMERR("Cannot initialize queue for request-based dm mapped device"); 2165bfebd1cdSMike Snitzer return r; 2166bfebd1cdSMike Snitzer } 2167bfebd1cdSMike Snitzer break; 2168bfebd1cdSMike Snitzer case DM_TYPE_BIO_BASED: 2169545ed20eSToshi Kani case DM_TYPE_DAX_BIO_BASED: 2170bfebd1cdSMike Snitzer break; 21717e0d574fSBart Van Assche case DM_TYPE_NONE: 21727e0d574fSBart Van Assche WARN_ON_ONCE(true); 21737e0d574fSBart Van Assche break; 2174ff36ab34SMike Snitzer } 21754a0b4ddfSMike Snitzer 2176c100ec49SMike Snitzer r = dm_calculate_queue_limits(t, &limits); 2177c100ec49SMike Snitzer if (r) { 2178c100ec49SMike Snitzer DMERR("Cannot calculate initial queue limits"); 2179c100ec49SMike Snitzer return r; 2180c100ec49SMike Snitzer } 2181bb37d772SDamien Le Moal r = dm_table_set_restrictions(t, md->queue, &limits); 2182bb37d772SDamien Le Moal if (r) 2183bb37d772SDamien Le Moal return r; 218489f871afSChristoph Hellwig 2185e7089f65SLuis Chamberlain r = add_disk(md->disk); 2186e7089f65SLuis Chamberlain if (r) 2187e7089f65SLuis Chamberlain return r; 218889f871afSChristoph Hellwig 218989f871afSChristoph Hellwig r = dm_sysfs_init(md); 219089f871afSChristoph Hellwig if (r) { 219189f871afSChristoph Hellwig del_gendisk(md->disk); 219289f871afSChristoph Hellwig return r; 219389f871afSChristoph Hellwig } 2194ba305859SChristoph Hellwig md->type = type; 21954a0b4ddfSMike Snitzer return 0; 21964a0b4ddfSMike Snitzer } 21974a0b4ddfSMike Snitzer 21982bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev) 21991da177e4SLinus Torvalds { 22001da177e4SLinus Torvalds struct mapped_device *md; 22011da177e4SLinus Torvalds unsigned minor = MINOR(dev); 22021da177e4SLinus Torvalds 22031da177e4SLinus Torvalds if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 22041da177e4SLinus Torvalds return NULL; 22051da177e4SLinus Torvalds 2206f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 22071da177e4SLinus Torvalds 22081da177e4SLinus Torvalds md = idr_find(&_minor_idr, minor); 220949de5769SMike Snitzer if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) || 221049de5769SMike Snitzer test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { 2211637842cfSDavid Teigland md = NULL; 2212fba9f90eSJeff Mahoney goto out; 2213fba9f90eSJeff Mahoney } 22142bec1f4aSMikulas Patocka dm_get(md); 2215fba9f90eSJeff Mahoney out: 2216f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 22171da177e4SLinus Torvalds 2218637842cfSDavid Teigland return md; 2219637842cfSDavid Teigland } 22203cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md); 2221d229a958SDavid Teigland 22229ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md) 2223637842cfSDavid Teigland { 22249ade92a9SAlasdair G Kergon return md->interface_ptr; 22251da177e4SLinus Torvalds } 22261da177e4SLinus Torvalds 22271da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr) 22281da177e4SLinus Torvalds { 22291da177e4SLinus Torvalds md->interface_ptr = ptr; 22301da177e4SLinus Torvalds } 22311da177e4SLinus Torvalds 22321da177e4SLinus Torvalds void dm_get(struct mapped_device *md) 22331da177e4SLinus Torvalds { 22341da177e4SLinus Torvalds atomic_inc(&md->holders); 22353f77316dSKiyoshi Ueda BUG_ON(test_bit(DMF_FREEING, &md->flags)); 22361da177e4SLinus Torvalds } 22371da177e4SLinus Torvalds 223809ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md) 223909ee96b2SMikulas Patocka { 224009ee96b2SMikulas Patocka spin_lock(&_minor_lock); 224109ee96b2SMikulas Patocka if (test_bit(DMF_FREEING, &md->flags)) { 224209ee96b2SMikulas Patocka spin_unlock(&_minor_lock); 224309ee96b2SMikulas Patocka return -EBUSY; 224409ee96b2SMikulas Patocka } 224509ee96b2SMikulas Patocka dm_get(md); 224609ee96b2SMikulas Patocka spin_unlock(&_minor_lock); 224709ee96b2SMikulas Patocka return 0; 224809ee96b2SMikulas Patocka } 224909ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold); 225009ee96b2SMikulas Patocka 225172d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md) 225272d94861SAlasdair G Kergon { 225372d94861SAlasdair G Kergon return md->name; 225472d94861SAlasdair G Kergon } 225572d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name); 225672d94861SAlasdair G Kergon 22573f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait) 22581da177e4SLinus Torvalds { 22591134e5aeSMike Anderson struct dm_table *map; 226083d5e5b0SMikulas Patocka int srcu_idx; 22611da177e4SLinus Torvalds 22623f77316dSKiyoshi Ueda might_sleep(); 2263fba9f90eSJeff Mahoney 226463a4f065SMike Snitzer spin_lock(&_minor_lock); 22653f77316dSKiyoshi Ueda idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md)))); 2266fba9f90eSJeff Mahoney set_bit(DMF_FREEING, &md->flags); 2267f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 22683f77316dSKiyoshi Ueda 22697a5428dcSChristoph Hellwig blk_mark_disk_dead(md->disk); 22703b785fbcSBart Van Assche 2271ab7c7bb6SMikulas Patocka /* 2272ab7c7bb6SMikulas Patocka * Take suspend_lock so that presuspend and postsuspend methods 2273ab7c7bb6SMikulas Patocka * do not race with internal suspend. 2274ab7c7bb6SMikulas Patocka */ 2275ab7c7bb6SMikulas Patocka mutex_lock(&md->suspend_lock); 22762a708cffSJunichi Nomura map = dm_get_live_table(md, &srcu_idx); 22774f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) { 22781da177e4SLinus Torvalds dm_table_presuspend_targets(map); 2279adc0daadSMikulas Patocka set_bit(DMF_SUSPENDED, &md->flags); 22805df96f2bSMikulas Patocka set_bit(DMF_POST_SUSPENDING, &md->flags); 22811da177e4SLinus Torvalds dm_table_postsuspend_targets(map); 22821da177e4SLinus Torvalds } 228383d5e5b0SMikulas Patocka /* dm_put_live_table must be before msleep, otherwise deadlock is possible */ 228483d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 22852a708cffSJunichi Nomura mutex_unlock(&md->suspend_lock); 228683d5e5b0SMikulas Patocka 22873f77316dSKiyoshi Ueda /* 22883f77316dSKiyoshi Ueda * Rare, but there may be I/O requests still going to complete, 22893f77316dSKiyoshi Ueda * for example. Wait for all references to disappear. 22903f77316dSKiyoshi Ueda * No one should increment the reference count of the mapped_device, 22913f77316dSKiyoshi Ueda * after the mapped_device state becomes DMF_FREEING. 22923f77316dSKiyoshi Ueda */ 22933f77316dSKiyoshi Ueda if (wait) 22943f77316dSKiyoshi Ueda while (atomic_read(&md->holders)) 22953f77316dSKiyoshi Ueda msleep(1); 22963f77316dSKiyoshi Ueda else if (atomic_read(&md->holders)) 22973f77316dSKiyoshi Ueda DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)", 22983f77316dSKiyoshi Ueda dm_device_name(md), atomic_read(&md->holders)); 22993f77316dSKiyoshi Ueda 2300a7940155SAlasdair G Kergon dm_table_destroy(__unbind(md)); 23011da177e4SLinus Torvalds free_dev(md); 23021da177e4SLinus Torvalds } 23033f77316dSKiyoshi Ueda 23043f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md) 23053f77316dSKiyoshi Ueda { 23063f77316dSKiyoshi Ueda __dm_destroy(md, true); 23073f77316dSKiyoshi Ueda } 23083f77316dSKiyoshi Ueda 23093f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md) 23103f77316dSKiyoshi Ueda { 23113f77316dSKiyoshi Ueda __dm_destroy(md, false); 23123f77316dSKiyoshi Ueda } 23133f77316dSKiyoshi Ueda 23143f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md) 23153f77316dSKiyoshi Ueda { 23163f77316dSKiyoshi Ueda atomic_dec(&md->holders); 23171da177e4SLinus Torvalds } 231879eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put); 23191da177e4SLinus Torvalds 23209f6dc633SMike Snitzer static bool dm_in_flight_bios(struct mapped_device *md) 232185067747SMing Lei { 232285067747SMing Lei int cpu; 23239f6dc633SMike Snitzer unsigned long sum = 0; 232485067747SMing Lei 23259f6dc633SMike Snitzer for_each_possible_cpu(cpu) 23269f6dc633SMike Snitzer sum += *per_cpu_ptr(md->pending_io, cpu); 232785067747SMing Lei 232885067747SMing Lei return sum != 0; 232985067747SMing Lei } 233085067747SMing Lei 23312f064a59SPeter Zijlstra static int dm_wait_for_bios_completion(struct mapped_device *md, unsigned int task_state) 233246125c1cSMilan Broz { 233346125c1cSMilan Broz int r = 0; 23349f4c3f87SBart Van Assche DEFINE_WAIT(wait); 233546125c1cSMilan Broz 233685067747SMing Lei while (true) { 23379f4c3f87SBart Van Assche prepare_to_wait(&md->wait, &wait, task_state); 233846125c1cSMilan Broz 23399f6dc633SMike Snitzer if (!dm_in_flight_bios(md)) 234046125c1cSMilan Broz break; 234146125c1cSMilan Broz 2342e3fabdfdSBart Van Assche if (signal_pending_state(task_state, current)) { 234346125c1cSMilan Broz r = -EINTR; 234446125c1cSMilan Broz break; 234546125c1cSMilan Broz } 234646125c1cSMilan Broz 234746125c1cSMilan Broz io_schedule(); 234846125c1cSMilan Broz } 23499f4c3f87SBart Van Assche finish_wait(&md->wait, &wait); 2350b44ebeb0SMikulas Patocka 23519f6dc633SMike Snitzer smp_rmb(); 23529f6dc633SMike Snitzer 235346125c1cSMilan Broz return r; 235446125c1cSMilan Broz } 235546125c1cSMilan Broz 23562f064a59SPeter Zijlstra static int dm_wait_for_completion(struct mapped_device *md, unsigned int task_state) 235785067747SMing Lei { 235885067747SMing Lei int r = 0; 235985067747SMing Lei 236085067747SMing Lei if (!queue_is_mq(md->queue)) 236185067747SMing Lei return dm_wait_for_bios_completion(md, task_state); 236285067747SMing Lei 236385067747SMing Lei while (true) { 236485067747SMing Lei if (!blk_mq_queue_inflight(md->queue)) 236585067747SMing Lei break; 236685067747SMing Lei 236785067747SMing Lei if (signal_pending_state(task_state, current)) { 236885067747SMing Lei r = -EINTR; 236985067747SMing Lei break; 237085067747SMing Lei } 237185067747SMing Lei 237285067747SMing Lei msleep(5); 237385067747SMing Lei } 237485067747SMing Lei 237585067747SMing Lei return r; 237685067747SMing Lei } 237785067747SMing Lei 23781da177e4SLinus Torvalds /* 23791da177e4SLinus Torvalds * Process the deferred bios 23801da177e4SLinus Torvalds */ 2381ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work) 23821da177e4SLinus Torvalds { 23830c2915b8SMike Snitzer struct mapped_device *md = container_of(work, struct mapped_device, work); 23840c2915b8SMike Snitzer struct bio *bio; 2385ef208587SMikulas Patocka 23863b00b203SMikulas Patocka while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 2387022c2611SMikulas Patocka spin_lock_irq(&md->deferred_lock); 23880c2915b8SMike Snitzer bio = bio_list_pop(&md->deferred); 2389022c2611SMikulas Patocka spin_unlock_irq(&md->deferred_lock); 2390022c2611SMikulas Patocka 23910c2915b8SMike Snitzer if (!bio) 2392df12ee99SAlasdair G Kergon break; 239373d410c0SMilan Broz 23940c2915b8SMike Snitzer submit_bio_noacct(bio); 2395e6ee8c0bSKiyoshi Ueda } 23961da177e4SLinus Torvalds } 23971da177e4SLinus Torvalds 23989a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md) 2399304f3f6aSMilan Broz { 24003b00b203SMikulas Patocka clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 24014e857c58SPeter Zijlstra smp_mb__after_atomic(); 240253d5914fSMikulas Patocka queue_work(md->wq, &md->work); 2403304f3f6aSMilan Broz } 2404304f3f6aSMilan Broz 24051da177e4SLinus Torvalds /* 2406042d2a9bSAlasdair G Kergon * Swap in a new table, returning the old one for the caller to destroy. 24071da177e4SLinus Torvalds */ 2408042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table) 24091da177e4SLinus Torvalds { 241087eb5b21SMike Christie struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL); 2411754c5fc7SMike Snitzer struct queue_limits limits; 2412042d2a9bSAlasdair G Kergon int r; 24131da177e4SLinus Torvalds 2414e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 24151da177e4SLinus Torvalds 24161da177e4SLinus Torvalds /* device must be suspended */ 24174f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) 241893c534aeSAlasdair G Kergon goto out; 24191da177e4SLinus Torvalds 24203ae70656SMike Snitzer /* 24213ae70656SMike Snitzer * If the new table has no data devices, retain the existing limits. 24223ae70656SMike Snitzer * This helps multipath with queue_if_no_path if all paths disappear, 24233ae70656SMike Snitzer * then new I/O is queued based on these limits, and then some paths 24243ae70656SMike Snitzer * reappear. 24253ae70656SMike Snitzer */ 24263ae70656SMike Snitzer if (dm_table_has_no_data_devices(table)) { 242783d5e5b0SMikulas Patocka live_map = dm_get_live_table_fast(md); 24283ae70656SMike Snitzer if (live_map) 24293ae70656SMike Snitzer limits = md->queue->limits; 243083d5e5b0SMikulas Patocka dm_put_live_table_fast(md); 24313ae70656SMike Snitzer } 24323ae70656SMike Snitzer 243387eb5b21SMike Christie if (!live_map) { 2434754c5fc7SMike Snitzer r = dm_calculate_queue_limits(table, &limits); 2435042d2a9bSAlasdair G Kergon if (r) { 2436042d2a9bSAlasdair G Kergon map = ERR_PTR(r); 2437754c5fc7SMike Snitzer goto out; 2438042d2a9bSAlasdair G Kergon } 243987eb5b21SMike Christie } 2440754c5fc7SMike Snitzer 2441042d2a9bSAlasdair G Kergon map = __bind(md, table, &limits); 244262e08243SMikulas Patocka dm_issue_global_event(); 24431da177e4SLinus Torvalds 244493c534aeSAlasdair G Kergon out: 2445e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 2446042d2a9bSAlasdair G Kergon return map; 24471da177e4SLinus Torvalds } 24481da177e4SLinus Torvalds 24491da177e4SLinus Torvalds /* 24501da177e4SLinus Torvalds * Functions to lock and unlock any filesystem running on the 24511da177e4SLinus Torvalds * device. 24521da177e4SLinus Torvalds */ 24532ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md) 24541da177e4SLinus Torvalds { 2455e39e2e95SAlasdair G Kergon int r; 24561da177e4SLinus Torvalds 2457040f04bdSChristoph Hellwig WARN_ON(test_bit(DMF_FROZEN, &md->flags)); 2458dfbe03f6SAlasdair G Kergon 2459977115c0SChristoph Hellwig r = freeze_bdev(md->disk->part0); 2460040f04bdSChristoph Hellwig if (!r) 2461aa8d7c2fSAlasdair G Kergon set_bit(DMF_FROZEN, &md->flags); 2462040f04bdSChristoph Hellwig return r; 24631da177e4SLinus Torvalds } 24641da177e4SLinus Torvalds 24652ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md) 24661da177e4SLinus Torvalds { 2467aa8d7c2fSAlasdair G Kergon if (!test_bit(DMF_FROZEN, &md->flags)) 2468aa8d7c2fSAlasdair G Kergon return; 2469977115c0SChristoph Hellwig thaw_bdev(md->disk->part0); 2470aa8d7c2fSAlasdair G Kergon clear_bit(DMF_FROZEN, &md->flags); 24711da177e4SLinus Torvalds } 24721da177e4SLinus Torvalds 24731da177e4SLinus Torvalds /* 2474b48633f8SBart Van Assche * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG 2475b48633f8SBart Van Assche * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE 2476b48633f8SBart Van Assche * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY 2477b48633f8SBart Van Assche * 2478ffcc3936SMike Snitzer * If __dm_suspend returns 0, the device is completely quiescent 2479ffcc3936SMike Snitzer * now. There is no request-processing activity. All new requests 2480ffcc3936SMike Snitzer * are being added to md->deferred list. 2481cec47e3dSKiyoshi Ueda */ 2482ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map, 24832f064a59SPeter Zijlstra unsigned suspend_flags, unsigned int task_state, 2484eaf9a736SMike Snitzer int dmf_suspended_flag) 24851da177e4SLinus Torvalds { 2486ffcc3936SMike Snitzer bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG; 2487ffcc3936SMike Snitzer bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG; 2488ffcc3936SMike Snitzer int r; 2489cf222b37SAlasdair G Kergon 24905a8f1f80SBart Van Assche lockdep_assert_held(&md->suspend_lock); 24915a8f1f80SBart Van Assche 24922e93ccc1SKiyoshi Ueda /* 24932e93ccc1SKiyoshi Ueda * DMF_NOFLUSH_SUSPENDING must be set before presuspend. 24942e93ccc1SKiyoshi Ueda * This flag is cleared before dm_suspend returns. 24952e93ccc1SKiyoshi Ueda */ 24962e93ccc1SKiyoshi Ueda if (noflush) 24972e93ccc1SKiyoshi Ueda set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 249886331f39SBart Van Assche else 2499ac75b09fSMike Snitzer DMDEBUG("%s: suspending with flush", dm_device_name(md)); 25002e93ccc1SKiyoshi Ueda 2501d67ee213SMike Snitzer /* 2502d67ee213SMike Snitzer * This gets reverted if there's an error later and the targets 2503d67ee213SMike Snitzer * provide the .presuspend_undo hook. 2504d67ee213SMike Snitzer */ 25051da177e4SLinus Torvalds dm_table_presuspend_targets(map); 25061da177e4SLinus Torvalds 25072e93ccc1SKiyoshi Ueda /* 25089f518b27SKiyoshi Ueda * Flush I/O to the device. 25099f518b27SKiyoshi Ueda * Any I/O submitted after lock_fs() may not be flushed. 25109f518b27SKiyoshi Ueda * noflush takes precedence over do_lockfs. 25119f518b27SKiyoshi Ueda * (lock_fs() flushes I/Os and waits for them to complete.) 25122e93ccc1SKiyoshi Ueda */ 251332a926daSMikulas Patocka if (!noflush && do_lockfs) { 25142ca3310eSAlasdair G Kergon r = lock_fs(md); 2515d67ee213SMike Snitzer if (r) { 2516d67ee213SMike Snitzer dm_table_presuspend_undo_targets(map); 2517ffcc3936SMike Snitzer return r; 2518aa8d7c2fSAlasdair G Kergon } 2519d67ee213SMike Snitzer } 25201da177e4SLinus Torvalds 25211da177e4SLinus Torvalds /* 25223b00b203SMikulas Patocka * Here we must make sure that no processes are submitting requests 25233b00b203SMikulas Patocka * to target drivers i.e. no one may be executing 252496c9865cSMike Snitzer * dm_split_and_process_bio from dm_submit_bio. 25253b00b203SMikulas Patocka * 252696c9865cSMike Snitzer * To get all processes out of dm_split_and_process_bio in dm_submit_bio, 25273b00b203SMikulas Patocka * we take the write lock. To prevent any process from reentering 252896c9865cSMike Snitzer * dm_split_and_process_bio from dm_submit_bio and quiesce the thread 25290cede372SMike Snitzer * (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND and call 25306a8736d1STejun Heo * flush_workqueue(md->wq). 25311da177e4SLinus Torvalds */ 25321eb787ecSAlasdair G Kergon set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 253341abc4e1SHannes Reinecke if (map) 253483d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 25351da177e4SLinus Torvalds 2536d0bcb878SKiyoshi Ueda /* 253729e4013dSTejun Heo * Stop md->queue before flushing md->wq in case request-based 253829e4013dSTejun Heo * dm defers requests to md->wq from md->queue. 2539d0bcb878SKiyoshi Ueda */ 25406a23e05cSJens Axboe if (dm_request_based(md)) 2541eca7ee6dSMike Snitzer dm_stop_queue(md->queue); 2542cec47e3dSKiyoshi Ueda 2543d0bcb878SKiyoshi Ueda flush_workqueue(md->wq); 2544d0bcb878SKiyoshi Ueda 25451da177e4SLinus Torvalds /* 25463b00b203SMikulas Patocka * At this point no more requests are entering target request routines. 25473b00b203SMikulas Patocka * We call dm_wait_for_completion to wait for all existing requests 25483b00b203SMikulas Patocka * to finish. 25491da177e4SLinus Torvalds */ 2550b48633f8SBart Van Assche r = dm_wait_for_completion(md, task_state); 2551eaf9a736SMike Snitzer if (!r) 2552eaf9a736SMike Snitzer set_bit(dmf_suspended_flag, &md->flags); 25531da177e4SLinus Torvalds 25546d6f10dfSMilan Broz if (noflush) 2555022c2611SMikulas Patocka clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 255641abc4e1SHannes Reinecke if (map) 255783d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 25582e93ccc1SKiyoshi Ueda 25591da177e4SLinus Torvalds /* were we interrupted ? */ 256046125c1cSMilan Broz if (r < 0) { 25619a1fb464SMikulas Patocka dm_queue_flush(md); 256273d410c0SMilan Broz 2563cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 2564eca7ee6dSMike Snitzer dm_start_queue(md->queue); 2565cec47e3dSKiyoshi Ueda 25662ca3310eSAlasdair G Kergon unlock_fs(md); 2567d67ee213SMike Snitzer dm_table_presuspend_undo_targets(map); 2568ffcc3936SMike Snitzer /* pushback list is already flushed, so skip flush */ 2569ffcc3936SMike Snitzer } 2570ffcc3936SMike Snitzer 2571ffcc3936SMike Snitzer return r; 25722ca3310eSAlasdair G Kergon } 25732ca3310eSAlasdair G Kergon 25743b00b203SMikulas Patocka /* 2575ffcc3936SMike Snitzer * We need to be able to change a mapping table under a mounted 2576ffcc3936SMike Snitzer * filesystem. For example we might want to move some data in 2577ffcc3936SMike Snitzer * the background. Before the table can be swapped with 2578ffcc3936SMike Snitzer * dm_bind_table, dm_suspend must be called to flush any in 2579ffcc3936SMike Snitzer * flight bios and ensure that any further io gets deferred. 25803b00b203SMikulas Patocka */ 2581ffcc3936SMike Snitzer /* 2582ffcc3936SMike Snitzer * Suspend mechanism in request-based dm. 2583ffcc3936SMike Snitzer * 2584ffcc3936SMike Snitzer * 1. Flush all I/Os by lock_fs() if needed. 2585ffcc3936SMike Snitzer * 2. Stop dispatching any I/O by stopping the request_queue. 2586ffcc3936SMike Snitzer * 3. Wait for all in-flight I/Os to be completed or requeued. 2587ffcc3936SMike Snitzer * 2588ffcc3936SMike Snitzer * To abort suspend, start the request_queue. 2589ffcc3936SMike Snitzer */ 2590ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags) 2591ffcc3936SMike Snitzer { 2592ffcc3936SMike Snitzer struct dm_table *map = NULL; 2593ffcc3936SMike Snitzer int r = 0; 2594ffcc3936SMike Snitzer 2595ffcc3936SMike Snitzer retry: 2596ffcc3936SMike Snitzer mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); 2597ffcc3936SMike Snitzer 2598ffcc3936SMike Snitzer if (dm_suspended_md(md)) { 2599ffcc3936SMike Snitzer r = -EINVAL; 2600ffcc3936SMike Snitzer goto out_unlock; 2601ffcc3936SMike Snitzer } 2602ffcc3936SMike Snitzer 2603ffcc3936SMike Snitzer if (dm_suspended_internally_md(md)) { 2604ffcc3936SMike Snitzer /* already internally suspended, wait for internal resume */ 2605ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2606ffcc3936SMike Snitzer r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); 2607ffcc3936SMike Snitzer if (r) 2608ffcc3936SMike Snitzer return r; 2609ffcc3936SMike Snitzer goto retry; 2610ffcc3936SMike Snitzer } 2611ffcc3936SMike Snitzer 2612a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2613ffcc3936SMike Snitzer 2614eaf9a736SMike Snitzer r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED); 2615ffcc3936SMike Snitzer if (r) 2616ffcc3936SMike Snitzer goto out_unlock; 26173b00b203SMikulas Patocka 26185df96f2bSMikulas Patocka set_bit(DMF_POST_SUSPENDING, &md->flags); 26194d4471cbSKiyoshi Ueda dm_table_postsuspend_targets(map); 26205df96f2bSMikulas Patocka clear_bit(DMF_POST_SUSPENDING, &md->flags); 26214d4471cbSKiyoshi Ueda 2622d287483dSAlasdair G Kergon out_unlock: 2623e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 2624cf222b37SAlasdair G Kergon return r; 26251da177e4SLinus Torvalds } 26261da177e4SLinus Torvalds 2627ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map) 26281da177e4SLinus Torvalds { 2629ffcc3936SMike Snitzer if (map) { 2630ffcc3936SMike Snitzer int r = dm_table_resume_targets(map); 26318757b776SMilan Broz if (r) 2632ffcc3936SMike Snitzer return r; 2633ffcc3936SMike Snitzer } 26342ca3310eSAlasdair G Kergon 26359a1fb464SMikulas Patocka dm_queue_flush(md); 26362ca3310eSAlasdair G Kergon 2637cec47e3dSKiyoshi Ueda /* 2638cec47e3dSKiyoshi Ueda * Flushing deferred I/Os must be done after targets are resumed 2639cec47e3dSKiyoshi Ueda * so that mapping of targets can work correctly. 2640cec47e3dSKiyoshi Ueda * Request-based dm is queueing the deferred I/Os in its request_queue. 2641cec47e3dSKiyoshi Ueda */ 2642cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 2643eca7ee6dSMike Snitzer dm_start_queue(md->queue); 2644cec47e3dSKiyoshi Ueda 26452ca3310eSAlasdair G Kergon unlock_fs(md); 26462ca3310eSAlasdair G Kergon 2647ffcc3936SMike Snitzer return 0; 2648ffcc3936SMike Snitzer } 2649ffcc3936SMike Snitzer 2650ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md) 2651ffcc3936SMike Snitzer { 26528dc23658SMinfei Huang int r; 2653ffcc3936SMike Snitzer struct dm_table *map = NULL; 2654ffcc3936SMike Snitzer 2655ffcc3936SMike Snitzer retry: 26568dc23658SMinfei Huang r = -EINVAL; 2657ffcc3936SMike Snitzer mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); 2658ffcc3936SMike Snitzer 2659ffcc3936SMike Snitzer if (!dm_suspended_md(md)) 2660ffcc3936SMike Snitzer goto out; 2661ffcc3936SMike Snitzer 2662ffcc3936SMike Snitzer if (dm_suspended_internally_md(md)) { 2663ffcc3936SMike Snitzer /* already internally suspended, wait for internal resume */ 2664ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2665ffcc3936SMike Snitzer r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); 2666ffcc3936SMike Snitzer if (r) 2667ffcc3936SMike Snitzer return r; 2668ffcc3936SMike Snitzer goto retry; 2669ffcc3936SMike Snitzer } 2670ffcc3936SMike Snitzer 2671a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2672ffcc3936SMike Snitzer if (!map || !dm_table_get_size(map)) 2673ffcc3936SMike Snitzer goto out; 2674ffcc3936SMike Snitzer 2675ffcc3936SMike Snitzer r = __dm_resume(md, map); 2676ffcc3936SMike Snitzer if (r) 2677ffcc3936SMike Snitzer goto out; 2678ffcc3936SMike Snitzer 26792ca3310eSAlasdair G Kergon clear_bit(DMF_SUSPENDED, &md->flags); 2680cf222b37SAlasdair G Kergon out: 2681e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 26822ca3310eSAlasdair G Kergon 2683cf222b37SAlasdair G Kergon return r; 26841da177e4SLinus Torvalds } 26851da177e4SLinus Torvalds 2686fd2ed4d2SMikulas Patocka /* 2687fd2ed4d2SMikulas Patocka * Internal suspend/resume works like userspace-driven suspend. It waits 2688fd2ed4d2SMikulas Patocka * until all bios finish and prevents issuing new bios to the target drivers. 2689fd2ed4d2SMikulas Patocka * It may be used only from the kernel. 2690fd2ed4d2SMikulas Patocka */ 2691fd2ed4d2SMikulas Patocka 2692ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags) 2693ffcc3936SMike Snitzer { 2694ffcc3936SMike Snitzer struct dm_table *map = NULL; 2695ffcc3936SMike Snitzer 26961ea0654eSBart Van Assche lockdep_assert_held(&md->suspend_lock); 26971ea0654eSBart Van Assche 269896b26c8cSMikulas Patocka if (md->internal_suspend_count++) 2699ffcc3936SMike Snitzer return; /* nested internal suspend */ 2700ffcc3936SMike Snitzer 2701ffcc3936SMike Snitzer if (dm_suspended_md(md)) { 2702ffcc3936SMike Snitzer set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2703ffcc3936SMike Snitzer return; /* nest suspend */ 2704ffcc3936SMike Snitzer } 2705ffcc3936SMike Snitzer 2706a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2707ffcc3936SMike Snitzer 2708ffcc3936SMike Snitzer /* 2709ffcc3936SMike Snitzer * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is 2710ffcc3936SMike Snitzer * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend 2711ffcc3936SMike Snitzer * would require changing .presuspend to return an error -- avoid this 2712ffcc3936SMike Snitzer * until there is a need for more elaborate variants of internal suspend. 2713ffcc3936SMike Snitzer */ 2714eaf9a736SMike Snitzer (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE, 2715eaf9a736SMike Snitzer DMF_SUSPENDED_INTERNALLY); 2716ffcc3936SMike Snitzer 27175df96f2bSMikulas Patocka set_bit(DMF_POST_SUSPENDING, &md->flags); 2718ffcc3936SMike Snitzer dm_table_postsuspend_targets(map); 27195df96f2bSMikulas Patocka clear_bit(DMF_POST_SUSPENDING, &md->flags); 2720ffcc3936SMike Snitzer } 2721ffcc3936SMike Snitzer 2722ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md) 2723ffcc3936SMike Snitzer { 272496b26c8cSMikulas Patocka BUG_ON(!md->internal_suspend_count); 272596b26c8cSMikulas Patocka 272696b26c8cSMikulas Patocka if (--md->internal_suspend_count) 2727ffcc3936SMike Snitzer return; /* resume from nested internal suspend */ 2728ffcc3936SMike Snitzer 2729ffcc3936SMike Snitzer if (dm_suspended_md(md)) 2730ffcc3936SMike Snitzer goto done; /* resume from nested suspend */ 2731ffcc3936SMike Snitzer 2732ffcc3936SMike Snitzer /* 2733ffcc3936SMike Snitzer * NOTE: existing callers don't need to call dm_table_resume_targets 2734ffcc3936SMike Snitzer * (which may fail -- so best to avoid it for now by passing NULL map) 2735ffcc3936SMike Snitzer */ 2736ffcc3936SMike Snitzer (void) __dm_resume(md, NULL); 2737ffcc3936SMike Snitzer 2738ffcc3936SMike Snitzer done: 2739ffcc3936SMike Snitzer clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2740ffcc3936SMike Snitzer smp_mb__after_atomic(); 2741ffcc3936SMike Snitzer wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY); 2742ffcc3936SMike Snitzer } 2743ffcc3936SMike Snitzer 2744ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md) 2745fd2ed4d2SMikulas Patocka { 2746fd2ed4d2SMikulas Patocka mutex_lock(&md->suspend_lock); 2747ffcc3936SMike Snitzer __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG); 2748ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2749ffcc3936SMike Snitzer } 2750ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush); 2751ffcc3936SMike Snitzer 2752ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md) 2753ffcc3936SMike Snitzer { 2754ffcc3936SMike Snitzer mutex_lock(&md->suspend_lock); 2755ffcc3936SMike Snitzer __dm_internal_resume(md); 2756ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2757ffcc3936SMike Snitzer } 2758ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume); 2759ffcc3936SMike Snitzer 2760ffcc3936SMike Snitzer /* 2761ffcc3936SMike Snitzer * Fast variants of internal suspend/resume hold md->suspend_lock, 2762ffcc3936SMike Snitzer * which prevents interaction with userspace-driven suspend. 2763ffcc3936SMike Snitzer */ 2764ffcc3936SMike Snitzer 2765ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md) 2766ffcc3936SMike Snitzer { 2767ffcc3936SMike Snitzer mutex_lock(&md->suspend_lock); 2768ffcc3936SMike Snitzer if (dm_suspended_md(md) || dm_suspended_internally_md(md)) 2769fd2ed4d2SMikulas Patocka return; 2770fd2ed4d2SMikulas Patocka 2771fd2ed4d2SMikulas Patocka set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 2772fd2ed4d2SMikulas Patocka synchronize_srcu(&md->io_barrier); 2773fd2ed4d2SMikulas Patocka flush_workqueue(md->wq); 2774fd2ed4d2SMikulas Patocka dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); 2775fd2ed4d2SMikulas Patocka } 2776b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast); 2777fd2ed4d2SMikulas Patocka 2778ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md) 2779fd2ed4d2SMikulas Patocka { 2780ffcc3936SMike Snitzer if (dm_suspended_md(md) || dm_suspended_internally_md(md)) 2781fd2ed4d2SMikulas Patocka goto done; 2782fd2ed4d2SMikulas Patocka 2783fd2ed4d2SMikulas Patocka dm_queue_flush(md); 2784fd2ed4d2SMikulas Patocka 2785fd2ed4d2SMikulas Patocka done: 2786fd2ed4d2SMikulas Patocka mutex_unlock(&md->suspend_lock); 2787fd2ed4d2SMikulas Patocka } 2788b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast); 2789fd2ed4d2SMikulas Patocka 27901da177e4SLinus Torvalds /*----------------------------------------------------------------- 27911da177e4SLinus Torvalds * Event notification. 27921da177e4SLinus Torvalds *---------------------------------------------------------------*/ 27933abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action, 279460935eb2SMilan Broz unsigned cookie) 279569267a30SAlasdair G Kergon { 27966958c1c6SMikulas Patocka int r; 27976958c1c6SMikulas Patocka unsigned noio_flag; 279860935eb2SMilan Broz char udev_cookie[DM_COOKIE_LENGTH]; 279960935eb2SMilan Broz char *envp[] = { udev_cookie, NULL }; 280060935eb2SMilan Broz 28016958c1c6SMikulas Patocka noio_flag = memalloc_noio_save(); 28026958c1c6SMikulas Patocka 280360935eb2SMilan Broz if (!cookie) 28046958c1c6SMikulas Patocka r = kobject_uevent(&disk_to_dev(md->disk)->kobj, action); 280560935eb2SMilan Broz else { 280660935eb2SMilan Broz snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u", 280760935eb2SMilan Broz DM_COOKIE_ENV_VAR_NAME, cookie); 28086958c1c6SMikulas Patocka r = kobject_uevent_env(&disk_to_dev(md->disk)->kobj, 28093abf85b5SPeter Rajnoha action, envp); 281060935eb2SMilan Broz } 28116958c1c6SMikulas Patocka 28126958c1c6SMikulas Patocka memalloc_noio_restore(noio_flag); 28136958c1c6SMikulas Patocka 28146958c1c6SMikulas Patocka return r; 281569267a30SAlasdair G Kergon } 281669267a30SAlasdair G Kergon 28177a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md) 28187a8c3d3bSMike Anderson { 28197a8c3d3bSMike Anderson return atomic_add_return(1, &md->uevent_seq); 28207a8c3d3bSMike Anderson } 28217a8c3d3bSMike Anderson 28221da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md) 28231da177e4SLinus Torvalds { 28241da177e4SLinus Torvalds return atomic_read(&md->event_nr); 28251da177e4SLinus Torvalds } 28261da177e4SLinus Torvalds 28271da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr) 28281da177e4SLinus Torvalds { 28291da177e4SLinus Torvalds return wait_event_interruptible(md->eventq, 28301da177e4SLinus Torvalds (event_nr != atomic_read(&md->event_nr))); 28311da177e4SLinus Torvalds } 28321da177e4SLinus Torvalds 28337a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist) 28347a8c3d3bSMike Anderson { 28357a8c3d3bSMike Anderson unsigned long flags; 28367a8c3d3bSMike Anderson 28377a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 28387a8c3d3bSMike Anderson list_add(elist, &md->uevent_list); 28397a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 28407a8c3d3bSMike Anderson } 28417a8c3d3bSMike Anderson 28421da177e4SLinus Torvalds /* 28431da177e4SLinus Torvalds * The gendisk is only valid as long as you have a reference 28441da177e4SLinus Torvalds * count on 'md'. 28451da177e4SLinus Torvalds */ 28461da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md) 28471da177e4SLinus Torvalds { 28481da177e4SLinus Torvalds return md->disk; 28491da177e4SLinus Torvalds } 285065ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk); 28511da177e4SLinus Torvalds 2852784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md) 2853784aae73SMilan Broz { 28542995fa78SMikulas Patocka return &md->kobj_holder.kobj; 2855784aae73SMilan Broz } 2856784aae73SMilan Broz 2857784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj) 2858784aae73SMilan Broz { 2859784aae73SMilan Broz struct mapped_device *md; 2860784aae73SMilan Broz 28612995fa78SMikulas Patocka md = container_of(kobj, struct mapped_device, kobj_holder.kobj); 2862784aae73SMilan Broz 2863b9a41d21SHou Tao spin_lock(&_minor_lock); 2864b9a41d21SHou Tao if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { 2865b9a41d21SHou Tao md = NULL; 2866b9a41d21SHou Tao goto out; 2867b9a41d21SHou Tao } 2868784aae73SMilan Broz dm_get(md); 2869b9a41d21SHou Tao out: 2870b9a41d21SHou Tao spin_unlock(&_minor_lock); 2871b9a41d21SHou Tao 2872784aae73SMilan Broz return md; 2873784aae73SMilan Broz } 2874784aae73SMilan Broz 28754f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md) 28761da177e4SLinus Torvalds { 28771da177e4SLinus Torvalds return test_bit(DMF_SUSPENDED, &md->flags); 28781da177e4SLinus Torvalds } 28791da177e4SLinus Torvalds 28805df96f2bSMikulas Patocka static int dm_post_suspending_md(struct mapped_device *md) 28815df96f2bSMikulas Patocka { 28825df96f2bSMikulas Patocka return test_bit(DMF_POST_SUSPENDING, &md->flags); 28835df96f2bSMikulas Patocka } 28845df96f2bSMikulas Patocka 2885ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md) 2886ffcc3936SMike Snitzer { 2887ffcc3936SMike Snitzer return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2888ffcc3936SMike Snitzer } 2889ffcc3936SMike Snitzer 28902c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md) 28912c140a24SMikulas Patocka { 28922c140a24SMikulas Patocka return test_bit(DMF_DEFERRED_REMOVE, &md->flags); 28932c140a24SMikulas Patocka } 28942c140a24SMikulas Patocka 289564dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti) 289664dbce58SKiyoshi Ueda { 289733bd6f06SMike Snitzer return dm_suspended_md(ti->table->md); 289864dbce58SKiyoshi Ueda } 289964dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended); 290064dbce58SKiyoshi Ueda 29015df96f2bSMikulas Patocka int dm_post_suspending(struct dm_target *ti) 29025df96f2bSMikulas Patocka { 290333bd6f06SMike Snitzer return dm_post_suspending_md(ti->table->md); 29045df96f2bSMikulas Patocka } 29055df96f2bSMikulas Patocka EXPORT_SYMBOL_GPL(dm_post_suspending); 29065df96f2bSMikulas Patocka 29072e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti) 29082e93ccc1SKiyoshi Ueda { 290933bd6f06SMike Snitzer return __noflush_suspending(ti->table->md); 29102e93ccc1SKiyoshi Ueda } 29112e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending); 29122e93ccc1SKiyoshi Ueda 29137e0d574fSBart Van Assche struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type, 2914cfc97abcSMike Snitzer unsigned per_io_data_size, unsigned min_pool_size, 2915cfc97abcSMike Snitzer bool integrity, bool poll) 2916e6ee8c0bSKiyoshi Ueda { 2917115485e8SMike Snitzer struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id); 291878d8e58aSMike Snitzer unsigned int pool_size = 0; 291964f52b0eSMike Snitzer unsigned int front_pad, io_front_pad; 29206f1c819cSKent Overstreet int ret; 2921e6ee8c0bSKiyoshi Ueda 2922e6ee8c0bSKiyoshi Ueda if (!pools) 29234e6e36c3SMike Snitzer return NULL; 2924e6ee8c0bSKiyoshi Ueda 292578d8e58aSMike Snitzer switch (type) { 292678d8e58aSMike Snitzer case DM_TYPE_BIO_BASED: 2927545ed20eSToshi Kani case DM_TYPE_DAX_BIO_BASED: 29280776aa0eSMike Snitzer pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size); 292962f26317SJeffle Xu front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + DM_TARGET_IO_BIO_OFFSET; 293062f26317SJeffle Xu io_front_pad = roundup(per_io_data_size, __alignof__(struct dm_io)) + DM_IO_BIO_OFFSET; 2931cfc97abcSMike Snitzer ret = bioset_init(&pools->io_bs, pool_size, io_front_pad, poll ? BIOSET_PERCPU_CACHE : 0); 29326f1c819cSKent Overstreet if (ret) 293364f52b0eSMike Snitzer goto out; 29346f1c819cSKent Overstreet if (integrity && bioset_integrity_create(&pools->io_bs, pool_size)) 2935eb8db831SChristoph Hellwig goto out; 293678d8e58aSMike Snitzer break; 293778d8e58aSMike Snitzer case DM_TYPE_REQUEST_BASED: 29380776aa0eSMike Snitzer pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size); 293978d8e58aSMike Snitzer front_pad = offsetof(struct dm_rq_clone_bio_info, clone); 2940591ddcfcSMike Snitzer /* per_io_data_size is used for blk-mq pdu at queue allocation */ 294178d8e58aSMike Snitzer break; 294278d8e58aSMike Snitzer default: 294378d8e58aSMike Snitzer BUG(); 294478d8e58aSMike Snitzer } 294578d8e58aSMike Snitzer 29466f1c819cSKent Overstreet ret = bioset_init(&pools->bs, pool_size, front_pad, 0); 29476f1c819cSKent Overstreet if (ret) 29485f015204SJun'ichi Nomura goto out; 2949e6ee8c0bSKiyoshi Ueda 29506f1c819cSKent Overstreet if (integrity && bioset_integrity_create(&pools->bs, pool_size)) 29515f015204SJun'ichi Nomura goto out; 2952a91a2785SMartin K. Petersen 2953e6ee8c0bSKiyoshi Ueda return pools; 295478d8e58aSMike Snitzer 29555f015204SJun'ichi Nomura out: 29565f015204SJun'ichi Nomura dm_free_md_mempools(pools); 2957e6ee8c0bSKiyoshi Ueda 29584e6e36c3SMike Snitzer return NULL; 2959e6ee8c0bSKiyoshi Ueda } 2960e6ee8c0bSKiyoshi Ueda 2961e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools) 2962e6ee8c0bSKiyoshi Ueda { 2963e6ee8c0bSKiyoshi Ueda if (!pools) 2964e6ee8c0bSKiyoshi Ueda return; 2965e6ee8c0bSKiyoshi Ueda 29666f1c819cSKent Overstreet bioset_exit(&pools->bs); 29676f1c819cSKent Overstreet bioset_exit(&pools->io_bs); 2968e6ee8c0bSKiyoshi Ueda 2969e6ee8c0bSKiyoshi Ueda kfree(pools); 2970e6ee8c0bSKiyoshi Ueda } 2971e6ee8c0bSKiyoshi Ueda 29729c72bad1SChristoph Hellwig struct dm_pr { 29739c72bad1SChristoph Hellwig u64 old_key; 29749c72bad1SChristoph Hellwig u64 new_key; 29759c72bad1SChristoph Hellwig u32 flags; 29769c72bad1SChristoph Hellwig bool fail_early; 29779c72bad1SChristoph Hellwig }; 29789c72bad1SChristoph Hellwig 29799c72bad1SChristoph Hellwig static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn, 29809c72bad1SChristoph Hellwig void *data) 29819c72bad1SChristoph Hellwig { 29829c72bad1SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 29839c72bad1SChristoph Hellwig struct dm_table *table; 29849c72bad1SChristoph Hellwig struct dm_target *ti; 29859c72bad1SChristoph Hellwig int ret = -ENOTTY, srcu_idx; 29869c72bad1SChristoph Hellwig 29879c72bad1SChristoph Hellwig table = dm_get_live_table(md, &srcu_idx); 29889c72bad1SChristoph Hellwig if (!table || !dm_table_get_size(table)) 29899c72bad1SChristoph Hellwig goto out; 29909c72bad1SChristoph Hellwig 29919c72bad1SChristoph Hellwig /* We only support devices that have a single target */ 29929c72bad1SChristoph Hellwig if (dm_table_get_num_targets(table) != 1) 29939c72bad1SChristoph Hellwig goto out; 29949c72bad1SChristoph Hellwig ti = dm_table_get_target(table, 0); 29959c72bad1SChristoph Hellwig 29969c72bad1SChristoph Hellwig ret = -EINVAL; 29979c72bad1SChristoph Hellwig if (!ti->type->iterate_devices) 29989c72bad1SChristoph Hellwig goto out; 29999c72bad1SChristoph Hellwig 30009c72bad1SChristoph Hellwig ret = ti->type->iterate_devices(ti, fn, data); 30019c72bad1SChristoph Hellwig out: 30029c72bad1SChristoph Hellwig dm_put_live_table(md, srcu_idx); 30039c72bad1SChristoph Hellwig return ret; 30049c72bad1SChristoph Hellwig } 30059c72bad1SChristoph Hellwig 30069c72bad1SChristoph Hellwig /* 30079c72bad1SChristoph Hellwig * For register / unregister we need to manually call out to every path. 30089c72bad1SChristoph Hellwig */ 30099c72bad1SChristoph Hellwig static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev, 30109c72bad1SChristoph Hellwig sector_t start, sector_t len, void *data) 30119c72bad1SChristoph Hellwig { 30129c72bad1SChristoph Hellwig struct dm_pr *pr = data; 30139c72bad1SChristoph Hellwig const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops; 30149c72bad1SChristoph Hellwig 30159c72bad1SChristoph Hellwig if (!ops || !ops->pr_register) 30169c72bad1SChristoph Hellwig return -EOPNOTSUPP; 30179c72bad1SChristoph Hellwig return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags); 30189c72bad1SChristoph Hellwig } 30199c72bad1SChristoph Hellwig 302071cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key, 302171cdb697SChristoph Hellwig u32 flags) 302271cdb697SChristoph Hellwig { 30239c72bad1SChristoph Hellwig struct dm_pr pr = { 30249c72bad1SChristoph Hellwig .old_key = old_key, 30259c72bad1SChristoph Hellwig .new_key = new_key, 30269c72bad1SChristoph Hellwig .flags = flags, 30279c72bad1SChristoph Hellwig .fail_early = true, 30289c72bad1SChristoph Hellwig }; 30299c72bad1SChristoph Hellwig int ret; 303071cdb697SChristoph Hellwig 30319c72bad1SChristoph Hellwig ret = dm_call_pr(bdev, __dm_pr_register, &pr); 30329c72bad1SChristoph Hellwig if (ret && new_key) { 30339c72bad1SChristoph Hellwig /* unregister all paths if we failed to register any path */ 30349c72bad1SChristoph Hellwig pr.old_key = new_key; 30359c72bad1SChristoph Hellwig pr.new_key = 0; 30369c72bad1SChristoph Hellwig pr.flags = 0; 30379c72bad1SChristoph Hellwig pr.fail_early = false; 30389c72bad1SChristoph Hellwig dm_call_pr(bdev, __dm_pr_register, &pr); 30399c72bad1SChristoph Hellwig } 304071cdb697SChristoph Hellwig 30419c72bad1SChristoph Hellwig return ret; 304271cdb697SChristoph Hellwig } 304371cdb697SChristoph Hellwig 304471cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type, 304571cdb697SChristoph Hellwig u32 flags) 304671cdb697SChristoph Hellwig { 304771cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 304871cdb697SChristoph Hellwig const struct pr_ops *ops; 3049971888c4SMike Snitzer int r, srcu_idx; 305071cdb697SChristoph Hellwig 30515bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 305271cdb697SChristoph Hellwig if (r < 0) 3053971888c4SMike Snitzer goto out; 305471cdb697SChristoph Hellwig 305571cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 305671cdb697SChristoph Hellwig if (ops && ops->pr_reserve) 305771cdb697SChristoph Hellwig r = ops->pr_reserve(bdev, key, type, flags); 305871cdb697SChristoph Hellwig else 305971cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3060971888c4SMike Snitzer out: 3061971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 306271cdb697SChristoph Hellwig return r; 306371cdb697SChristoph Hellwig } 306471cdb697SChristoph Hellwig 306571cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type) 306671cdb697SChristoph Hellwig { 306771cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 306871cdb697SChristoph Hellwig const struct pr_ops *ops; 3069971888c4SMike Snitzer int r, srcu_idx; 307071cdb697SChristoph Hellwig 30715bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 307271cdb697SChristoph Hellwig if (r < 0) 3073971888c4SMike Snitzer goto out; 307471cdb697SChristoph Hellwig 307571cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 307671cdb697SChristoph Hellwig if (ops && ops->pr_release) 307771cdb697SChristoph Hellwig r = ops->pr_release(bdev, key, type); 307871cdb697SChristoph Hellwig else 307971cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3080971888c4SMike Snitzer out: 3081971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 308271cdb697SChristoph Hellwig return r; 308371cdb697SChristoph Hellwig } 308471cdb697SChristoph Hellwig 308571cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key, 308671cdb697SChristoph Hellwig enum pr_type type, bool abort) 308771cdb697SChristoph Hellwig { 308871cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 308971cdb697SChristoph Hellwig const struct pr_ops *ops; 3090971888c4SMike Snitzer int r, srcu_idx; 309171cdb697SChristoph Hellwig 30925bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 309371cdb697SChristoph Hellwig if (r < 0) 3094971888c4SMike Snitzer goto out; 309571cdb697SChristoph Hellwig 309671cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 309771cdb697SChristoph Hellwig if (ops && ops->pr_preempt) 309871cdb697SChristoph Hellwig r = ops->pr_preempt(bdev, old_key, new_key, type, abort); 309971cdb697SChristoph Hellwig else 310071cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3101971888c4SMike Snitzer out: 3102971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 310371cdb697SChristoph Hellwig return r; 310471cdb697SChristoph Hellwig } 310571cdb697SChristoph Hellwig 310671cdb697SChristoph Hellwig static int dm_pr_clear(struct block_device *bdev, u64 key) 310771cdb697SChristoph Hellwig { 310871cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 310971cdb697SChristoph Hellwig const struct pr_ops *ops; 3110971888c4SMike Snitzer int r, srcu_idx; 311171cdb697SChristoph Hellwig 31125bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 311371cdb697SChristoph Hellwig if (r < 0) 3114971888c4SMike Snitzer goto out; 311571cdb697SChristoph Hellwig 311671cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 311771cdb697SChristoph Hellwig if (ops && ops->pr_clear) 311871cdb697SChristoph Hellwig r = ops->pr_clear(bdev, key); 311971cdb697SChristoph Hellwig else 312071cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3121971888c4SMike Snitzer out: 3122971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 312371cdb697SChristoph Hellwig return r; 312471cdb697SChristoph Hellwig } 312571cdb697SChristoph Hellwig 312671cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = { 312771cdb697SChristoph Hellwig .pr_register = dm_pr_register, 312871cdb697SChristoph Hellwig .pr_reserve = dm_pr_reserve, 312971cdb697SChristoph Hellwig .pr_release = dm_pr_release, 313071cdb697SChristoph Hellwig .pr_preempt = dm_pr_preempt, 313171cdb697SChristoph Hellwig .pr_clear = dm_pr_clear, 313271cdb697SChristoph Hellwig }; 313371cdb697SChristoph Hellwig 313483d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = { 3135c62b37d9SChristoph Hellwig .submit_bio = dm_submit_bio, 3136b99fdcdcSMing Lei .poll_bio = dm_poll_bio, 31371da177e4SLinus Torvalds .open = dm_blk_open, 31381da177e4SLinus Torvalds .release = dm_blk_close, 3139aa129a22SMilan Broz .ioctl = dm_blk_ioctl, 31403ac51e74SDarrick J. Wong .getgeo = dm_blk_getgeo, 3141e76239a3SChristoph Hellwig .report_zones = dm_blk_report_zones, 314271cdb697SChristoph Hellwig .pr_ops = &dm_pr_ops, 31431da177e4SLinus Torvalds .owner = THIS_MODULE 31441da177e4SLinus Torvalds }; 31451da177e4SLinus Torvalds 3146681cc5e8SMike Snitzer static const struct block_device_operations dm_rq_blk_dops = { 3147681cc5e8SMike Snitzer .open = dm_blk_open, 3148681cc5e8SMike Snitzer .release = dm_blk_close, 3149681cc5e8SMike Snitzer .ioctl = dm_blk_ioctl, 3150681cc5e8SMike Snitzer .getgeo = dm_blk_getgeo, 3151681cc5e8SMike Snitzer .pr_ops = &dm_pr_ops, 3152681cc5e8SMike Snitzer .owner = THIS_MODULE 3153681cc5e8SMike Snitzer }; 3154681cc5e8SMike Snitzer 3155f26c5719SDan Williams static const struct dax_operations dm_dax_ops = { 3156f26c5719SDan Williams .direct_access = dm_dax_direct_access, 3157cdf6cdcdSVivek Goyal .zero_page_range = dm_dax_zero_page_range, 3158f26c5719SDan Williams }; 3159f26c5719SDan Williams 31601da177e4SLinus Torvalds /* 31611da177e4SLinus Torvalds * module hooks 31621da177e4SLinus Torvalds */ 31631da177e4SLinus Torvalds module_init(dm_init); 31641da177e4SLinus Torvalds module_exit(dm_exit); 31651da177e4SLinus Torvalds 31661da177e4SLinus Torvalds module_param(major, uint, 0); 31671da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper"); 3168f4790826SMike Snitzer 3169e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR); 3170e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools"); 3171e8603136SMike Snitzer 3172115485e8SMike Snitzer module_param(dm_numa_node, int, S_IRUGO | S_IWUSR); 3173115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations"); 3174115485e8SMike Snitzer 3175a666e5c0SMikulas Patocka module_param(swap_bios, int, S_IRUGO | S_IWUSR); 3176a666e5c0SMikulas Patocka MODULE_PARM_DESC(swap_bios, "Maximum allowed inflight swap IOs"); 3177a666e5c0SMikulas Patocka 31781da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver"); 31791da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 31801da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 3181