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> 32aa6ce87aSSatya Tangirala #include <linux/keyslot-manager.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 431da177e4SLinus Torvalds static const char *_name = DM_NAME; 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds static unsigned int major = 0; 461da177e4SLinus Torvalds static unsigned int _major = 0; 471da177e4SLinus Torvalds 48d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr); 49d15b774cSAlasdair G Kergon 50f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock); 512c140a24SMikulas Patocka 522c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w); 532c140a24SMikulas Patocka 542c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove); 552c140a24SMikulas Patocka 56acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue; 57acfe0ad7SMikulas Patocka 5893e6442cSMikulas Patocka atomic_t dm_global_event_nr = ATOMIC_INIT(0); 5993e6442cSMikulas Patocka DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq); 6093e6442cSMikulas Patocka 6162e08243SMikulas Patocka void dm_issue_global_event(void) 6262e08243SMikulas Patocka { 6362e08243SMikulas Patocka atomic_inc(&dm_global_event_nr); 6462e08243SMikulas Patocka wake_up(&dm_global_eventq); 6562e08243SMikulas Patocka } 6662e08243SMikulas Patocka 671da177e4SLinus Torvalds /* 6864f52b0eSMike Snitzer * One of these is allocated (on-stack) per original bio. 691da177e4SLinus Torvalds */ 7064f52b0eSMike Snitzer struct clone_info { 7164f52b0eSMike Snitzer struct dm_table *map; 7264f52b0eSMike Snitzer struct bio *bio; 7364f52b0eSMike Snitzer struct dm_io *io; 7464f52b0eSMike Snitzer sector_t sector; 7564f52b0eSMike Snitzer unsigned sector_count; 7664f52b0eSMike Snitzer }; 7764f52b0eSMike Snitzer 7862f26317SJeffle Xu #define DM_TARGET_IO_BIO_OFFSET (offsetof(struct dm_target_io, clone)) 7962f26317SJeffle Xu #define DM_IO_BIO_OFFSET \ 8062f26317SJeffle Xu (offsetof(struct dm_target_io, clone) + offsetof(struct dm_io, tio)) 8162f26317SJeffle Xu 8264f52b0eSMike Snitzer void *dm_per_bio_data(struct bio *bio, size_t data_size) 8364f52b0eSMike Snitzer { 8464f52b0eSMike Snitzer struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); 8564f52b0eSMike Snitzer if (!tio->inside_dm_io) 8662f26317SJeffle Xu return (char *)bio - DM_TARGET_IO_BIO_OFFSET - data_size; 8762f26317SJeffle Xu return (char *)bio - DM_IO_BIO_OFFSET - data_size; 8864f52b0eSMike Snitzer } 8964f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_per_bio_data); 9064f52b0eSMike Snitzer 9164f52b0eSMike Snitzer struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size) 9264f52b0eSMike Snitzer { 9364f52b0eSMike Snitzer struct dm_io *io = (struct dm_io *)((char *)data + data_size); 9464f52b0eSMike Snitzer if (io->magic == DM_IO_MAGIC) 9562f26317SJeffle Xu return (struct bio *)((char *)io + DM_IO_BIO_OFFSET); 9664f52b0eSMike Snitzer BUG_ON(io->magic != DM_TIO_MAGIC); 9762f26317SJeffle Xu return (struct bio *)((char *)io + DM_TARGET_IO_BIO_OFFSET); 9864f52b0eSMike Snitzer } 9964f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_from_per_bio_data); 10064f52b0eSMike Snitzer 10164f52b0eSMike Snitzer unsigned dm_bio_get_target_bio_nr(const struct bio *bio) 10264f52b0eSMike Snitzer { 10364f52b0eSMike Snitzer return container_of(bio, struct dm_target_io, clone)->target_bio_nr; 10464f52b0eSMike Snitzer } 10564f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_get_target_bio_nr); 10664f52b0eSMike Snitzer 107ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1) 108ba61fdd1SJeff Mahoney 109115485e8SMike Snitzer #define DM_NUMA_NODE NUMA_NO_NODE 110115485e8SMike Snitzer static int dm_numa_node = DM_NUMA_NODE; 111faad87dfSMike Snitzer 112a666e5c0SMikulas Patocka #define DEFAULT_SWAP_BIOS (8 * 1048576 / PAGE_SIZE) 113a666e5c0SMikulas Patocka static int swap_bios = DEFAULT_SWAP_BIOS; 114a666e5c0SMikulas Patocka static int get_swap_bios(void) 115a666e5c0SMikulas Patocka { 116a666e5c0SMikulas Patocka int latch = READ_ONCE(swap_bios); 117a666e5c0SMikulas Patocka if (unlikely(latch <= 0)) 118a666e5c0SMikulas Patocka latch = DEFAULT_SWAP_BIOS; 119a666e5c0SMikulas Patocka return latch; 120a666e5c0SMikulas Patocka } 121a666e5c0SMikulas Patocka 122e6ee8c0bSKiyoshi Ueda /* 123e6ee8c0bSKiyoshi Ueda * For mempools pre-allocation at the table loading time. 124e6ee8c0bSKiyoshi Ueda */ 125e6ee8c0bSKiyoshi Ueda struct dm_md_mempools { 1266f1c819cSKent Overstreet struct bio_set bs; 1276f1c819cSKent Overstreet struct bio_set io_bs; 128e6ee8c0bSKiyoshi Ueda }; 129e6ee8c0bSKiyoshi Ueda 13086f1152bSBenjamin Marzinski struct table_device { 13186f1152bSBenjamin Marzinski struct list_head list; 132b0b4d7c6SElena Reshetova refcount_t count; 13386f1152bSBenjamin Marzinski struct dm_dev dm_dev; 13486f1152bSBenjamin Marzinski }; 13586f1152bSBenjamin Marzinski 136f4790826SMike Snitzer /* 137e8603136SMike Snitzer * Bio-based DM's mempools' reserved IOs set by the user. 138e8603136SMike Snitzer */ 1394cc96131SMike Snitzer #define RESERVED_BIO_BASED_IOS 16 140e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS; 141e8603136SMike Snitzer 142115485e8SMike Snitzer static int __dm_get_module_param_int(int *module_param, int min, int max) 143115485e8SMike Snitzer { 1446aa7de05SMark Rutland int param = READ_ONCE(*module_param); 145115485e8SMike Snitzer int modified_param = 0; 146115485e8SMike Snitzer bool modified = true; 147115485e8SMike Snitzer 148115485e8SMike Snitzer if (param < min) 149115485e8SMike Snitzer modified_param = min; 150115485e8SMike Snitzer else if (param > max) 151115485e8SMike Snitzer modified_param = max; 152115485e8SMike Snitzer else 153115485e8SMike Snitzer modified = false; 154115485e8SMike Snitzer 155115485e8SMike Snitzer if (modified) { 156115485e8SMike Snitzer (void)cmpxchg(module_param, param, modified_param); 157115485e8SMike Snitzer param = modified_param; 158115485e8SMike Snitzer } 159115485e8SMike Snitzer 160115485e8SMike Snitzer return param; 161115485e8SMike Snitzer } 162115485e8SMike Snitzer 1634cc96131SMike Snitzer unsigned __dm_get_module_param(unsigned *module_param, 164f4790826SMike Snitzer unsigned def, unsigned max) 165f4790826SMike Snitzer { 1666aa7de05SMark Rutland unsigned param = READ_ONCE(*module_param); 16709c2d531SMike Snitzer unsigned modified_param = 0; 168f4790826SMike Snitzer 16909c2d531SMike Snitzer if (!param) 17009c2d531SMike Snitzer modified_param = def; 17109c2d531SMike Snitzer else if (param > max) 17209c2d531SMike Snitzer modified_param = max; 173f4790826SMike Snitzer 17409c2d531SMike Snitzer if (modified_param) { 17509c2d531SMike Snitzer (void)cmpxchg(module_param, param, modified_param); 17609c2d531SMike Snitzer param = modified_param; 177f4790826SMike Snitzer } 178f4790826SMike Snitzer 17909c2d531SMike Snitzer return param; 180f4790826SMike Snitzer } 181f4790826SMike Snitzer 182e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void) 183e8603136SMike Snitzer { 18409c2d531SMike Snitzer return __dm_get_module_param(&reserved_bio_based_ios, 1854cc96131SMike Snitzer RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS); 186e8603136SMike Snitzer } 187e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios); 188e8603136SMike Snitzer 189115485e8SMike Snitzer static unsigned dm_get_numa_node(void) 190115485e8SMike Snitzer { 191115485e8SMike Snitzer return __dm_get_module_param_int(&dm_numa_node, 192115485e8SMike Snitzer DM_NUMA_NODE, num_online_nodes() - 1); 193115485e8SMike Snitzer } 194115485e8SMike Snitzer 1951da177e4SLinus Torvalds static int __init local_init(void) 1961da177e4SLinus Torvalds { 197e689fbabSMike Snitzer int r; 1981ae49ea2SMike Snitzer 19951e5b2bdSMike Anderson r = dm_uevent_init(); 20051157b4aSKiyoshi Ueda if (r) 201e689fbabSMike Snitzer return r; 20251e5b2bdSMike Anderson 203acfe0ad7SMikulas Patocka deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1); 204acfe0ad7SMikulas Patocka if (!deferred_remove_workqueue) { 205acfe0ad7SMikulas Patocka r = -ENOMEM; 206acfe0ad7SMikulas Patocka goto out_uevent_exit; 207acfe0ad7SMikulas Patocka } 208acfe0ad7SMikulas Patocka 2091da177e4SLinus Torvalds _major = major; 2101da177e4SLinus Torvalds r = register_blkdev(_major, _name); 21151157b4aSKiyoshi Ueda if (r < 0) 212acfe0ad7SMikulas Patocka goto out_free_workqueue; 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds if (!_major) 2151da177e4SLinus Torvalds _major = r; 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds return 0; 21851157b4aSKiyoshi Ueda 219acfe0ad7SMikulas Patocka out_free_workqueue: 220acfe0ad7SMikulas Patocka destroy_workqueue(deferred_remove_workqueue); 22151157b4aSKiyoshi Ueda out_uevent_exit: 22251157b4aSKiyoshi Ueda dm_uevent_exit(); 22351157b4aSKiyoshi Ueda 22451157b4aSKiyoshi Ueda return r; 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds static void local_exit(void) 2281da177e4SLinus Torvalds { 2292c140a24SMikulas Patocka flush_scheduled_work(); 230acfe0ad7SMikulas Patocka destroy_workqueue(deferred_remove_workqueue); 2312c140a24SMikulas Patocka 23200d59405SAkinobu Mita unregister_blkdev(_major, _name); 23351e5b2bdSMike Anderson dm_uevent_exit(); 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds _major = 0; 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds DMINFO("cleaned up"); 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds 240b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = { 2411da177e4SLinus Torvalds local_init, 2421da177e4SLinus Torvalds dm_target_init, 2431da177e4SLinus Torvalds dm_linear_init, 2441da177e4SLinus Torvalds dm_stripe_init, 245952b3557SMikulas Patocka dm_io_init, 246945fa4d2SMikulas Patocka dm_kcopyd_init, 2471da177e4SLinus Torvalds dm_interface_init, 248fd2ed4d2SMikulas Patocka dm_statistics_init, 2491da177e4SLinus Torvalds }; 2501da177e4SLinus Torvalds 251b9249e55SAlasdair G Kergon static void (*_exits[])(void) = { 2521da177e4SLinus Torvalds local_exit, 2531da177e4SLinus Torvalds dm_target_exit, 2541da177e4SLinus Torvalds dm_linear_exit, 2551da177e4SLinus Torvalds dm_stripe_exit, 256952b3557SMikulas Patocka dm_io_exit, 257945fa4d2SMikulas Patocka dm_kcopyd_exit, 2581da177e4SLinus Torvalds dm_interface_exit, 259fd2ed4d2SMikulas Patocka dm_statistics_exit, 2601da177e4SLinus Torvalds }; 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds static int __init dm_init(void) 2631da177e4SLinus Torvalds { 2641da177e4SLinus Torvalds const int count = ARRAY_SIZE(_inits); 2651da177e4SLinus Torvalds int r, i; 2661da177e4SLinus Torvalds 267*f1cd6cb2STushar Sugandhi #if (IS_ENABLED(CONFIG_IMA) && !IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE)) 268*f1cd6cb2STushar Sugandhi DMWARN("CONFIG_IMA_DISABLE_HTABLE is disabled." 269*f1cd6cb2STushar Sugandhi " Duplicate IMA measurements will not be recorded in the IMA log."); 270*f1cd6cb2STushar Sugandhi #endif 271*f1cd6cb2STushar Sugandhi 2721da177e4SLinus Torvalds for (i = 0; i < count; i++) { 2731da177e4SLinus Torvalds r = _inits[i](); 2741da177e4SLinus Torvalds if (r) 2751da177e4SLinus Torvalds goto bad; 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds return 0; 2791da177e4SLinus Torvalds bad: 2801da177e4SLinus Torvalds while (i--) 2811da177e4SLinus Torvalds _exits[i](); 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds return r; 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds static void __exit dm_exit(void) 2871da177e4SLinus Torvalds { 2881da177e4SLinus Torvalds int i = ARRAY_SIZE(_exits); 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds while (i--) 2911da177e4SLinus Torvalds _exits[i](); 292d15b774cSAlasdair G Kergon 293d15b774cSAlasdair G Kergon /* 294d15b774cSAlasdair G Kergon * Should be empty by this point. 295d15b774cSAlasdair G Kergon */ 296d15b774cSAlasdair G Kergon idr_destroy(&_minor_idr); 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds /* 3001da177e4SLinus Torvalds * Block device functions 3011da177e4SLinus Torvalds */ 302432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md) 303432a212cSMike Anderson { 304432a212cSMike Anderson return test_bit(DMF_DELETING, &md->flags); 305432a212cSMike Anderson } 306432a212cSMike Anderson 307fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode) 3081da177e4SLinus Torvalds { 3091da177e4SLinus Torvalds struct mapped_device *md; 3101da177e4SLinus Torvalds 311fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 312fba9f90eSJeff Mahoney 313fe5f9f2cSAl Viro md = bdev->bd_disk->private_data; 314fba9f90eSJeff Mahoney if (!md) 315fba9f90eSJeff Mahoney goto out; 316fba9f90eSJeff Mahoney 3175c6bd75dSAlasdair G Kergon if (test_bit(DMF_FREEING, &md->flags) || 318432a212cSMike Anderson dm_deleting_md(md)) { 319fba9f90eSJeff Mahoney md = NULL; 320fba9f90eSJeff Mahoney goto out; 321fba9f90eSJeff Mahoney } 322fba9f90eSJeff Mahoney 3231da177e4SLinus Torvalds dm_get(md); 3245c6bd75dSAlasdair G Kergon atomic_inc(&md->open_count); 325fba9f90eSJeff Mahoney out: 326fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 327fba9f90eSJeff Mahoney 328fba9f90eSJeff Mahoney return md ? 0 : -ENXIO; 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 331db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode) 3321da177e4SLinus Torvalds { 33363a4f065SMike Snitzer struct mapped_device *md; 3346e9624b8SArnd Bergmann 3354a1aeb98SMilan Broz spin_lock(&_minor_lock); 3364a1aeb98SMilan Broz 33763a4f065SMike Snitzer md = disk->private_data; 33863a4f065SMike Snitzer if (WARN_ON(!md)) 33963a4f065SMike Snitzer goto out; 34063a4f065SMike Snitzer 3412c140a24SMikulas Patocka if (atomic_dec_and_test(&md->open_count) && 3422c140a24SMikulas Patocka (test_bit(DMF_DEFERRED_REMOVE, &md->flags))) 343acfe0ad7SMikulas Patocka queue_work(deferred_remove_workqueue, &deferred_remove_work); 3442c140a24SMikulas Patocka 3451da177e4SLinus Torvalds dm_put(md); 34663a4f065SMike Snitzer out: 3474a1aeb98SMilan Broz spin_unlock(&_minor_lock); 3481da177e4SLinus Torvalds } 3491da177e4SLinus Torvalds 3505c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md) 3515c6bd75dSAlasdair G Kergon { 3525c6bd75dSAlasdair G Kergon return atomic_read(&md->open_count); 3535c6bd75dSAlasdair G Kergon } 3545c6bd75dSAlasdair G Kergon 3555c6bd75dSAlasdair G Kergon /* 3565c6bd75dSAlasdair G Kergon * Guarantees nothing is using the device before it's deleted. 3575c6bd75dSAlasdair G Kergon */ 3582c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred) 3595c6bd75dSAlasdair G Kergon { 3605c6bd75dSAlasdair G Kergon int r = 0; 3615c6bd75dSAlasdair G Kergon 3625c6bd75dSAlasdair G Kergon spin_lock(&_minor_lock); 3635c6bd75dSAlasdair G Kergon 3642c140a24SMikulas Patocka if (dm_open_count(md)) { 3655c6bd75dSAlasdair G Kergon r = -EBUSY; 3662c140a24SMikulas Patocka if (mark_deferred) 3672c140a24SMikulas Patocka set_bit(DMF_DEFERRED_REMOVE, &md->flags); 3682c140a24SMikulas Patocka } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags)) 3692c140a24SMikulas Patocka r = -EEXIST; 3705c6bd75dSAlasdair G Kergon else 3715c6bd75dSAlasdair G Kergon set_bit(DMF_DELETING, &md->flags); 3725c6bd75dSAlasdair G Kergon 3735c6bd75dSAlasdair G Kergon spin_unlock(&_minor_lock); 3745c6bd75dSAlasdair G Kergon 3755c6bd75dSAlasdair G Kergon return r; 3765c6bd75dSAlasdair G Kergon } 3775c6bd75dSAlasdair G Kergon 3782c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md) 3792c140a24SMikulas Patocka { 3802c140a24SMikulas Patocka int r = 0; 3812c140a24SMikulas Patocka 3822c140a24SMikulas Patocka spin_lock(&_minor_lock); 3832c140a24SMikulas Patocka 3842c140a24SMikulas Patocka if (test_bit(DMF_DELETING, &md->flags)) 3852c140a24SMikulas Patocka r = -EBUSY; 3862c140a24SMikulas Patocka else 3872c140a24SMikulas Patocka clear_bit(DMF_DEFERRED_REMOVE, &md->flags); 3882c140a24SMikulas Patocka 3892c140a24SMikulas Patocka spin_unlock(&_minor_lock); 3902c140a24SMikulas Patocka 3912c140a24SMikulas Patocka return r; 3922c140a24SMikulas Patocka } 3932c140a24SMikulas Patocka 3942c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w) 3952c140a24SMikulas Patocka { 3962c140a24SMikulas Patocka dm_deferred_remove(); 3972c140a24SMikulas Patocka } 3982c140a24SMikulas Patocka 3993ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 4003ac51e74SDarrick J. Wong { 4013ac51e74SDarrick J. Wong struct mapped_device *md = bdev->bd_disk->private_data; 4023ac51e74SDarrick J. Wong 4033ac51e74SDarrick J. Wong return dm_get_geometry(md, geo); 4043ac51e74SDarrick J. Wong } 4053ac51e74SDarrick J. Wong 406971888c4SMike Snitzer static int dm_prepare_ioctl(struct mapped_device *md, int *srcu_idx, 4075bd5e8d8SMike Snitzer struct block_device **bdev) 408aa129a22SMilan Broz { 40966482026SMike Snitzer struct dm_target *tgt; 4106c182cd8SHannes Reinecke struct dm_table *map; 411971888c4SMike Snitzer int r; 412aa129a22SMilan Broz 4136c182cd8SHannes Reinecke retry: 414e56f81e0SChristoph Hellwig r = -ENOTTY; 415971888c4SMike Snitzer map = dm_get_live_table(md, srcu_idx); 416aa129a22SMilan Broz if (!map || !dm_table_get_size(map)) 417971888c4SMike Snitzer return r; 418aa129a22SMilan Broz 419aa129a22SMilan Broz /* We only support devices that have a single target */ 420aa129a22SMilan Broz if (dm_table_get_num_targets(map) != 1) 421971888c4SMike Snitzer return r; 422aa129a22SMilan Broz 42366482026SMike Snitzer tgt = dm_table_get_target(map, 0); 42466482026SMike Snitzer if (!tgt->type->prepare_ioctl) 425e56f81e0SChristoph Hellwig return r; 426aa129a22SMilan Broz 427971888c4SMike Snitzer if (dm_suspended_md(md)) 428971888c4SMike Snitzer return -EAGAIN; 429971888c4SMike Snitzer 4305bd5e8d8SMike Snitzer r = tgt->type->prepare_ioctl(tgt, bdev); 4315bbbfdf6SJunichi Nomura if (r == -ENOTCONN && !fatal_signal_pending(current)) { 432971888c4SMike Snitzer dm_put_live_table(md, *srcu_idx); 4336c182cd8SHannes Reinecke msleep(10); 4346c182cd8SHannes Reinecke goto retry; 4356c182cd8SHannes Reinecke } 436971888c4SMike Snitzer 437e56f81e0SChristoph Hellwig return r; 438e56f81e0SChristoph Hellwig } 4396c182cd8SHannes Reinecke 440971888c4SMike Snitzer static void dm_unprepare_ioctl(struct mapped_device *md, int srcu_idx) 441971888c4SMike Snitzer { 442971888c4SMike Snitzer dm_put_live_table(md, srcu_idx); 443971888c4SMike Snitzer } 444971888c4SMike Snitzer 445e56f81e0SChristoph Hellwig static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode, 446e56f81e0SChristoph Hellwig unsigned int cmd, unsigned long arg) 447e56f81e0SChristoph Hellwig { 448e56f81e0SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 449971888c4SMike Snitzer int r, srcu_idx; 450e56f81e0SChristoph Hellwig 4515bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 452e56f81e0SChristoph Hellwig if (r < 0) 453971888c4SMike Snitzer goto out; 454e56f81e0SChristoph Hellwig 455e56f81e0SChristoph Hellwig if (r > 0) { 456e56f81e0SChristoph Hellwig /* 457e980f623SChristoph Hellwig * Target determined this ioctl is being issued against a 458e980f623SChristoph Hellwig * subset of the parent bdev; require extra privileges. 459e56f81e0SChristoph Hellwig */ 460e980f623SChristoph Hellwig if (!capable(CAP_SYS_RAWIO)) { 4610378c625SMike Snitzer DMDEBUG_LIMIT( 462e980f623SChristoph Hellwig "%s: sending ioctl %x to DM device without required privilege.", 463e980f623SChristoph Hellwig current->comm, cmd); 464e980f623SChristoph Hellwig r = -ENOIOCTLCMD; 465e56f81e0SChristoph Hellwig goto out; 466e56f81e0SChristoph Hellwig } 467e980f623SChristoph Hellwig } 468e56f81e0SChristoph Hellwig 469a7cb3d2fSChristoph Hellwig if (!bdev->bd_disk->fops->ioctl) 470a7cb3d2fSChristoph Hellwig r = -ENOTTY; 471a7cb3d2fSChristoph Hellwig else 472a7cb3d2fSChristoph Hellwig r = bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg); 473e56f81e0SChristoph Hellwig out: 474971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 475aa129a22SMilan Broz return r; 476aa129a22SMilan Broz } 477aa129a22SMilan Broz 4787465d7acSMike Snitzer u64 dm_start_time_ns_from_clone(struct bio *bio) 4797465d7acSMike Snitzer { 4807465d7acSMike Snitzer struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); 4817465d7acSMike Snitzer struct dm_io *io = tio->io; 4827465d7acSMike Snitzer 4837465d7acSMike Snitzer return jiffies_to_nsecs(io->start_time); 4847465d7acSMike Snitzer } 4857465d7acSMike Snitzer EXPORT_SYMBOL_GPL(dm_start_time_ns_from_clone); 4867465d7acSMike Snitzer 4877465d7acSMike Snitzer static void start_io_acct(struct dm_io *io) 4887465d7acSMike Snitzer { 4897465d7acSMike Snitzer struct mapped_device *md = io->md; 4907465d7acSMike Snitzer struct bio *bio = io->orig_bio; 4917465d7acSMike Snitzer 4927465d7acSMike Snitzer io->start_time = bio_start_io_acct(bio); 4937465d7acSMike Snitzer if (unlikely(dm_stats_used(&md->stats))) 4947465d7acSMike Snitzer dm_stats_account_io(&md->stats, bio_data_dir(bio), 4957465d7acSMike Snitzer bio->bi_iter.bi_sector, bio_sectors(bio), 4967465d7acSMike Snitzer false, 0, &io->stats_aux); 4977465d7acSMike Snitzer } 4987465d7acSMike Snitzer 4997465d7acSMike Snitzer static void end_io_acct(struct dm_io *io) 5007465d7acSMike Snitzer { 5017465d7acSMike Snitzer struct mapped_device *md = io->md; 5027465d7acSMike Snitzer struct bio *bio = io->orig_bio; 5037465d7acSMike Snitzer unsigned long duration = jiffies - io->start_time; 5047465d7acSMike Snitzer 5057465d7acSMike Snitzer bio_end_io_acct(bio, io->start_time); 5067465d7acSMike Snitzer 5077465d7acSMike Snitzer if (unlikely(dm_stats_used(&md->stats))) 5087465d7acSMike Snitzer dm_stats_account_io(&md->stats, bio_data_dir(bio), 5097465d7acSMike Snitzer bio->bi_iter.bi_sector, bio_sectors(bio), 5107465d7acSMike Snitzer true, duration, &io->stats_aux); 5117465d7acSMike Snitzer 5127465d7acSMike Snitzer /* nudge anyone waiting on suspend queue */ 5137465d7acSMike Snitzer if (unlikely(wq_has_sleeper(&md->wait))) 5147465d7acSMike Snitzer wake_up(&md->wait); 5157465d7acSMike Snitzer } 516978e51baSMike Snitzer 517978e51baSMike Snitzer static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio) 5181da177e4SLinus Torvalds { 51964f52b0eSMike Snitzer struct dm_io *io; 52064f52b0eSMike Snitzer struct dm_target_io *tio; 52164f52b0eSMike Snitzer struct bio *clone; 52264f52b0eSMike Snitzer 5236f1c819cSKent Overstreet clone = bio_alloc_bioset(GFP_NOIO, 0, &md->io_bs); 52464f52b0eSMike Snitzer if (!clone) 52564f52b0eSMike Snitzer return NULL; 52664f52b0eSMike Snitzer 52764f52b0eSMike Snitzer tio = container_of(clone, struct dm_target_io, clone); 52864f52b0eSMike Snitzer tio->inside_dm_io = true; 52964f52b0eSMike Snitzer tio->io = NULL; 53064f52b0eSMike Snitzer 53164f52b0eSMike Snitzer io = container_of(tio, struct dm_io, tio); 53264f52b0eSMike Snitzer io->magic = DM_IO_MAGIC; 533978e51baSMike Snitzer io->status = 0; 534978e51baSMike Snitzer atomic_set(&io->io_count, 1); 535978e51baSMike Snitzer io->orig_bio = bio; 536978e51baSMike Snitzer io->md = md; 537978e51baSMike Snitzer spin_lock_init(&io->endio_lock); 538978e51baSMike Snitzer 539978e51baSMike Snitzer start_io_acct(io); 54064f52b0eSMike Snitzer 54164f52b0eSMike Snitzer return io; 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds 544028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io) 5451da177e4SLinus Torvalds { 54664f52b0eSMike Snitzer bio_put(&io->tio.clone); 54764f52b0eSMike Snitzer } 54864f52b0eSMike Snitzer 54964f52b0eSMike Snitzer static struct dm_target_io *alloc_tio(struct clone_info *ci, struct dm_target *ti, 55064f52b0eSMike Snitzer unsigned target_bio_nr, gfp_t gfp_mask) 55164f52b0eSMike Snitzer { 55264f52b0eSMike Snitzer struct dm_target_io *tio; 55364f52b0eSMike Snitzer 55464f52b0eSMike Snitzer if (!ci->io->tio.io) { 55564f52b0eSMike Snitzer /* the dm_target_io embedded in ci->io is available */ 55664f52b0eSMike Snitzer tio = &ci->io->tio; 55764f52b0eSMike Snitzer } else { 5586f1c819cSKent Overstreet struct bio *clone = bio_alloc_bioset(gfp_mask, 0, &ci->io->md->bs); 55964f52b0eSMike Snitzer if (!clone) 56064f52b0eSMike Snitzer return NULL; 56164f52b0eSMike Snitzer 56264f52b0eSMike Snitzer tio = container_of(clone, struct dm_target_io, clone); 56364f52b0eSMike Snitzer tio->inside_dm_io = false; 56464f52b0eSMike Snitzer } 56564f52b0eSMike Snitzer 56664f52b0eSMike Snitzer tio->magic = DM_TIO_MAGIC; 56764f52b0eSMike Snitzer tio->io = ci->io; 56864f52b0eSMike Snitzer tio->ti = ti; 56964f52b0eSMike Snitzer tio->target_bio_nr = target_bio_nr; 57064f52b0eSMike Snitzer 57164f52b0eSMike Snitzer return tio; 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds 574cfae7529SMike Snitzer static void free_tio(struct dm_target_io *tio) 5751da177e4SLinus Torvalds { 57664f52b0eSMike Snitzer if (tio->inside_dm_io) 57764f52b0eSMike Snitzer return; 578dba14160SMikulas Patocka bio_put(&tio->clone); 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds /* 5821da177e4SLinus Torvalds * Add the bio to the list of deferred io. 5831da177e4SLinus Torvalds */ 58492c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio) 5851da177e4SLinus Torvalds { 58605447420SKiyoshi Ueda unsigned long flags; 5871da177e4SLinus Torvalds 58805447420SKiyoshi Ueda spin_lock_irqsave(&md->deferred_lock, flags); 5891da177e4SLinus Torvalds bio_list_add(&md->deferred, bio); 59005447420SKiyoshi Ueda spin_unlock_irqrestore(&md->deferred_lock, flags); 59192c63902SMikulas Patocka queue_work(md->wq, &md->work); 5921da177e4SLinus Torvalds } 5931da177e4SLinus Torvalds 5941da177e4SLinus Torvalds /* 5951da177e4SLinus Torvalds * Everyone (including functions in this file), should use this 5961da177e4SLinus Torvalds * function to access the md->map field, and make sure they call 59783d5e5b0SMikulas Patocka * dm_put_live_table() when finished. 5981da177e4SLinus Torvalds */ 59983d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier) 6001da177e4SLinus Torvalds { 60183d5e5b0SMikulas Patocka *srcu_idx = srcu_read_lock(&md->io_barrier); 6021da177e4SLinus Torvalds 60383d5e5b0SMikulas Patocka return srcu_dereference(md->map, &md->io_barrier); 60483d5e5b0SMikulas Patocka } 6051da177e4SLinus Torvalds 60683d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier) 60783d5e5b0SMikulas Patocka { 60883d5e5b0SMikulas Patocka srcu_read_unlock(&md->io_barrier, srcu_idx); 60983d5e5b0SMikulas Patocka } 61083d5e5b0SMikulas Patocka 61183d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md) 61283d5e5b0SMikulas Patocka { 61383d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 61483d5e5b0SMikulas Patocka synchronize_rcu_expedited(); 61583d5e5b0SMikulas Patocka } 61683d5e5b0SMikulas Patocka 61783d5e5b0SMikulas Patocka /* 61883d5e5b0SMikulas Patocka * A fast alternative to dm_get_live_table/dm_put_live_table. 61983d5e5b0SMikulas Patocka * The caller must not block between these two functions. 62083d5e5b0SMikulas Patocka */ 62183d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU) 62283d5e5b0SMikulas Patocka { 62383d5e5b0SMikulas Patocka rcu_read_lock(); 62483d5e5b0SMikulas Patocka return rcu_dereference(md->map); 62583d5e5b0SMikulas Patocka } 62683d5e5b0SMikulas Patocka 62783d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU) 62883d5e5b0SMikulas Patocka { 62983d5e5b0SMikulas Patocka rcu_read_unlock(); 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds 632971888c4SMike Snitzer static char *_dm_claim_ptr = "I belong to device-mapper"; 633971888c4SMike Snitzer 6343ac51e74SDarrick J. Wong /* 63586f1152bSBenjamin Marzinski * Open a table device so we can use it as a map destination. 63686f1152bSBenjamin Marzinski */ 63786f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev, 63886f1152bSBenjamin Marzinski struct mapped_device *md) 63986f1152bSBenjamin Marzinski { 64086f1152bSBenjamin Marzinski struct block_device *bdev; 64186f1152bSBenjamin Marzinski 64286f1152bSBenjamin Marzinski int r; 64386f1152bSBenjamin Marzinski 64486f1152bSBenjamin Marzinski BUG_ON(td->dm_dev.bdev); 64586f1152bSBenjamin Marzinski 646519049afSMike Snitzer bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr); 64786f1152bSBenjamin Marzinski if (IS_ERR(bdev)) 64886f1152bSBenjamin Marzinski return PTR_ERR(bdev); 64986f1152bSBenjamin Marzinski 65086f1152bSBenjamin Marzinski r = bd_link_disk_holder(bdev, dm_disk(md)); 65186f1152bSBenjamin Marzinski if (r) { 65286f1152bSBenjamin Marzinski blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL); 65386f1152bSBenjamin Marzinski return r; 65486f1152bSBenjamin Marzinski } 65586f1152bSBenjamin Marzinski 65686f1152bSBenjamin Marzinski td->dm_dev.bdev = bdev; 657817bf402SDan Williams td->dm_dev.dax_dev = dax_get_by_host(bdev->bd_disk->disk_name); 65886f1152bSBenjamin Marzinski return 0; 65986f1152bSBenjamin Marzinski } 66086f1152bSBenjamin Marzinski 66186f1152bSBenjamin Marzinski /* 66286f1152bSBenjamin Marzinski * Close a table device that we've been using. 66386f1152bSBenjamin Marzinski */ 66486f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md) 66586f1152bSBenjamin Marzinski { 66686f1152bSBenjamin Marzinski if (!td->dm_dev.bdev) 66786f1152bSBenjamin Marzinski return; 66886f1152bSBenjamin Marzinski 66986f1152bSBenjamin Marzinski bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md)); 67086f1152bSBenjamin Marzinski blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL); 671817bf402SDan Williams put_dax(td->dm_dev.dax_dev); 67286f1152bSBenjamin Marzinski td->dm_dev.bdev = NULL; 673817bf402SDan Williams td->dm_dev.dax_dev = NULL; 67486f1152bSBenjamin Marzinski } 67586f1152bSBenjamin Marzinski 67686f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev, 6778454fca4SSheetal Singala fmode_t mode) 6788454fca4SSheetal Singala { 67986f1152bSBenjamin Marzinski struct table_device *td; 68086f1152bSBenjamin Marzinski 68186f1152bSBenjamin Marzinski list_for_each_entry(td, l, list) 68286f1152bSBenjamin Marzinski if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode) 68386f1152bSBenjamin Marzinski return td; 68486f1152bSBenjamin Marzinski 68586f1152bSBenjamin Marzinski return NULL; 68686f1152bSBenjamin Marzinski } 68786f1152bSBenjamin Marzinski 68886f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode, 6898454fca4SSheetal Singala struct dm_dev **result) 6908454fca4SSheetal Singala { 69186f1152bSBenjamin Marzinski int r; 69286f1152bSBenjamin Marzinski struct table_device *td; 69386f1152bSBenjamin Marzinski 69486f1152bSBenjamin Marzinski mutex_lock(&md->table_devices_lock); 69586f1152bSBenjamin Marzinski td = find_table_device(&md->table_devices, dev, mode); 69686f1152bSBenjamin Marzinski if (!td) { 697115485e8SMike Snitzer td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id); 69886f1152bSBenjamin Marzinski if (!td) { 69986f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 70086f1152bSBenjamin Marzinski return -ENOMEM; 70186f1152bSBenjamin Marzinski } 70286f1152bSBenjamin Marzinski 70386f1152bSBenjamin Marzinski td->dm_dev.mode = mode; 70486f1152bSBenjamin Marzinski td->dm_dev.bdev = NULL; 70586f1152bSBenjamin Marzinski 70686f1152bSBenjamin Marzinski if ((r = open_table_device(td, dev, md))) { 70786f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 70886f1152bSBenjamin Marzinski kfree(td); 70986f1152bSBenjamin Marzinski return r; 71086f1152bSBenjamin Marzinski } 71186f1152bSBenjamin Marzinski 71286f1152bSBenjamin Marzinski format_dev_t(td->dm_dev.name, dev); 71386f1152bSBenjamin Marzinski 714b0b4d7c6SElena Reshetova refcount_set(&td->count, 1); 71586f1152bSBenjamin Marzinski list_add(&td->list, &md->table_devices); 716b0b4d7c6SElena Reshetova } else { 717b0b4d7c6SElena Reshetova refcount_inc(&td->count); 71886f1152bSBenjamin Marzinski } 71986f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 72086f1152bSBenjamin Marzinski 72186f1152bSBenjamin Marzinski *result = &td->dm_dev; 72286f1152bSBenjamin Marzinski return 0; 72386f1152bSBenjamin Marzinski } 72486f1152bSBenjamin Marzinski 72586f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d) 72686f1152bSBenjamin Marzinski { 72786f1152bSBenjamin Marzinski struct table_device *td = container_of(d, struct table_device, dm_dev); 72886f1152bSBenjamin Marzinski 72986f1152bSBenjamin Marzinski mutex_lock(&md->table_devices_lock); 730b0b4d7c6SElena Reshetova if (refcount_dec_and_test(&td->count)) { 73186f1152bSBenjamin Marzinski close_table_device(td, md); 73286f1152bSBenjamin Marzinski list_del(&td->list); 73386f1152bSBenjamin Marzinski kfree(td); 73486f1152bSBenjamin Marzinski } 73586f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 73686f1152bSBenjamin Marzinski } 73786f1152bSBenjamin Marzinski 73886f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices) 73986f1152bSBenjamin Marzinski { 74086f1152bSBenjamin Marzinski struct list_head *tmp, *next; 74186f1152bSBenjamin Marzinski 74286f1152bSBenjamin Marzinski list_for_each_safe(tmp, next, devices) { 74386f1152bSBenjamin Marzinski struct table_device *td = list_entry(tmp, struct table_device, list); 74486f1152bSBenjamin Marzinski 74586f1152bSBenjamin Marzinski DMWARN("dm_destroy: %s still exists with %d references", 746b0b4d7c6SElena Reshetova td->dm_dev.name, refcount_read(&td->count)); 74786f1152bSBenjamin Marzinski kfree(td); 74886f1152bSBenjamin Marzinski } 74986f1152bSBenjamin Marzinski } 75086f1152bSBenjamin Marzinski 75186f1152bSBenjamin Marzinski /* 7523ac51e74SDarrick J. Wong * Get the geometry associated with a dm device 7533ac51e74SDarrick J. Wong */ 7543ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) 7553ac51e74SDarrick J. Wong { 7563ac51e74SDarrick J. Wong *geo = md->geometry; 7573ac51e74SDarrick J. Wong 7583ac51e74SDarrick J. Wong return 0; 7593ac51e74SDarrick J. Wong } 7603ac51e74SDarrick J. Wong 7613ac51e74SDarrick J. Wong /* 7623ac51e74SDarrick J. Wong * Set the geometry of a device. 7633ac51e74SDarrick J. Wong */ 7643ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) 7653ac51e74SDarrick J. Wong { 7663ac51e74SDarrick J. Wong sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; 7673ac51e74SDarrick J. Wong 7683ac51e74SDarrick J. Wong if (geo->start > sz) { 7693ac51e74SDarrick J. Wong DMWARN("Start sector is beyond the geometry limits."); 7703ac51e74SDarrick J. Wong return -EINVAL; 7713ac51e74SDarrick J. Wong } 7723ac51e74SDarrick J. Wong 7733ac51e74SDarrick J. Wong md->geometry = *geo; 7743ac51e74SDarrick J. Wong 7753ac51e74SDarrick J. Wong return 0; 7763ac51e74SDarrick J. Wong } 7773ac51e74SDarrick J. Wong 7782e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md) 7792e93ccc1SKiyoshi Ueda { 7802e93ccc1SKiyoshi Ueda return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 7812e93ccc1SKiyoshi Ueda } 7822e93ccc1SKiyoshi Ueda 7831da177e4SLinus Torvalds /* 7841da177e4SLinus Torvalds * Decrements the number of outstanding ios that a bio has been 7851da177e4SLinus Torvalds * cloned into, completing the original io if necc. 7861da177e4SLinus Torvalds */ 787e2118b3cSDamien Le Moal void dm_io_dec_pending(struct dm_io *io, blk_status_t error) 7881da177e4SLinus Torvalds { 7892e93ccc1SKiyoshi Ueda unsigned long flags; 7904e4cbee9SChristoph Hellwig blk_status_t io_error; 791b35f8caaSMilan Broz struct bio *bio; 792b35f8caaSMilan Broz struct mapped_device *md = io->md; 7932e93ccc1SKiyoshi Ueda 7942e93ccc1SKiyoshi Ueda /* Push-back supersedes any I/O errors */ 795f88fb981SKiyoshi Ueda if (unlikely(error)) { 796f88fb981SKiyoshi Ueda spin_lock_irqsave(&io->endio_lock, flags); 797745dc570SMike Snitzer if (!(io->status == BLK_STS_DM_REQUEUE && __noflush_suspending(md))) 7984e4cbee9SChristoph Hellwig io->status = error; 799f88fb981SKiyoshi Ueda spin_unlock_irqrestore(&io->endio_lock, flags); 800f88fb981SKiyoshi Ueda } 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds if (atomic_dec_and_test(&io->io_count)) { 803bf14e2b2SDamien Le Moal bio = io->orig_bio; 8044e4cbee9SChristoph Hellwig if (io->status == BLK_STS_DM_REQUEUE) { 8052e93ccc1SKiyoshi Ueda /* 8062e93ccc1SKiyoshi Ueda * Target requested pushing back the I/O. 8072e93ccc1SKiyoshi Ueda */ 808022c2611SMikulas Patocka spin_lock_irqsave(&md->deferred_lock, flags); 809bf14e2b2SDamien Le Moal if (__noflush_suspending(md) && 810bf14e2b2SDamien Le Moal !WARN_ON_ONCE(dm_is_zone_write(md, bio))) { 811745dc570SMike Snitzer /* NOTE early return due to BLK_STS_DM_REQUEUE below */ 812bf14e2b2SDamien Le Moal bio_list_add_head(&md->deferred, bio); 813bf14e2b2SDamien Le Moal } else { 814bf14e2b2SDamien Le Moal /* 815bf14e2b2SDamien Le Moal * noflush suspend was interrupted or this is 816bf14e2b2SDamien Le Moal * a write to a zoned target. 817bf14e2b2SDamien Le Moal */ 8184e4cbee9SChristoph Hellwig io->status = BLK_STS_IOERR; 819bf14e2b2SDamien Le Moal } 820022c2611SMikulas Patocka spin_unlock_irqrestore(&md->deferred_lock, flags); 8212e93ccc1SKiyoshi Ueda } 8222e93ccc1SKiyoshi Ueda 8234e4cbee9SChristoph Hellwig io_error = io->status; 824af7e466aSMikulas Patocka end_io_acct(io); 825a97f925aSMikulas Patocka free_io(md, io); 8261da177e4SLinus Torvalds 8274e4cbee9SChristoph Hellwig if (io_error == BLK_STS_DM_REQUEUE) 8286a8736d1STejun Heo return; 8296a8736d1STejun Heo 8301eff9d32SJens Axboe if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) { 8311da177e4SLinus Torvalds /* 8326a8736d1STejun Heo * Preflush done for flush with data, reissue 83328a8f0d3SMike Christie * without REQ_PREFLUSH. 8341da177e4SLinus Torvalds */ 8351eff9d32SJens Axboe bio->bi_opf &= ~REQ_PREFLUSH; 8366a8736d1STejun Heo queue_io(md, bio); 837b35f8caaSMilan Broz } else { 838b372d360SMike Snitzer /* done with normal IO or empty flush */ 8398dd601faSNeilBrown if (io_error) 8404e4cbee9SChristoph Hellwig bio->bi_status = io_error; 8414246a0b6SChristoph Hellwig bio_endio(bio); 8422e93ccc1SKiyoshi Ueda } 8431da177e4SLinus Torvalds } 844af7e466aSMikulas Patocka } 8451da177e4SLinus Torvalds 846bcb44433SMike Snitzer void disable_discard(struct mapped_device *md) 847bcb44433SMike Snitzer { 848bcb44433SMike Snitzer struct queue_limits *limits = dm_get_queue_limits(md); 849bcb44433SMike Snitzer 850bcb44433SMike Snitzer /* device doesn't really support DISCARD, disable it */ 851bcb44433SMike Snitzer limits->max_discard_sectors = 0; 852bcb44433SMike Snitzer blk_queue_flag_clear(QUEUE_FLAG_DISCARD, md->queue); 853bcb44433SMike Snitzer } 854bcb44433SMike Snitzer 8554cc96131SMike Snitzer void disable_write_same(struct mapped_device *md) 8567eee4ae2SMike Snitzer { 8577eee4ae2SMike Snitzer struct queue_limits *limits = dm_get_queue_limits(md); 8587eee4ae2SMike Snitzer 8597eee4ae2SMike Snitzer /* device doesn't really support WRITE SAME, disable it */ 8607eee4ae2SMike Snitzer limits->max_write_same_sectors = 0; 8617eee4ae2SMike Snitzer } 8627eee4ae2SMike Snitzer 863ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md) 864ac62d620SChristoph Hellwig { 865ac62d620SChristoph Hellwig struct queue_limits *limits = dm_get_queue_limits(md); 866ac62d620SChristoph Hellwig 867ac62d620SChristoph Hellwig /* device doesn't really support WRITE ZEROES, disable it */ 868ac62d620SChristoph Hellwig limits->max_write_zeroes_sectors = 0; 869ac62d620SChristoph Hellwig } 870ac62d620SChristoph Hellwig 871a666e5c0SMikulas Patocka static bool swap_bios_limit(struct dm_target *ti, struct bio *bio) 872a666e5c0SMikulas Patocka { 873a666e5c0SMikulas Patocka return unlikely((bio->bi_opf & REQ_SWAP) != 0) && unlikely(ti->limit_swap_bios); 874a666e5c0SMikulas Patocka } 875a666e5c0SMikulas Patocka 8764246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio) 8771da177e4SLinus Torvalds { 8784e4cbee9SChristoph Hellwig blk_status_t error = bio->bi_status; 879bfc6d41cSMikulas Patocka struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); 880b35f8caaSMilan Broz struct dm_io *io = tio->io; 8819faf400fSStefan Bader struct mapped_device *md = tio->io->md; 8821da177e4SLinus Torvalds dm_endio_fn endio = tio->ti->type->end_io; 883309dca30SChristoph Hellwig struct request_queue *q = bio->bi_bdev->bd_disk->queue; 8841da177e4SLinus Torvalds 8859c37de29SMike Snitzer if (unlikely(error == BLK_STS_TARGET)) { 886bcb44433SMike Snitzer if (bio_op(bio) == REQ_OP_DISCARD && 887309dca30SChristoph Hellwig !q->limits.max_discard_sectors) 888bcb44433SMike Snitzer disable_discard(md); 889bcb44433SMike Snitzer else if (bio_op(bio) == REQ_OP_WRITE_SAME && 890309dca30SChristoph Hellwig !q->limits.max_write_same_sectors) 8917eee4ae2SMike Snitzer disable_write_same(md); 892bcb44433SMike Snitzer else if (bio_op(bio) == REQ_OP_WRITE_ZEROES && 893309dca30SChristoph Hellwig !q->limits.max_write_zeroes_sectors) 894ac62d620SChristoph Hellwig disable_write_zeroes(md); 895ac62d620SChristoph Hellwig } 8967eee4ae2SMike Snitzer 897bb37d772SDamien Le Moal if (blk_queue_is_zoned(q)) 898bb37d772SDamien Le Moal dm_zone_endio(io, bio); 899415c79e1SJohannes Thumshirn 9001be56909SChristoph Hellwig if (endio) { 9014e4cbee9SChristoph Hellwig int r = endio(tio->ti, bio, &error); 9021be56909SChristoph Hellwig switch (r) { 9031be56909SChristoph Hellwig case DM_ENDIO_REQUEUE: 904bf14e2b2SDamien Le Moal /* 905bf14e2b2SDamien Le Moal * Requeuing writes to a sequential zone of a zoned 906bf14e2b2SDamien Le Moal * target will break the sequential write pattern: 907bf14e2b2SDamien Le Moal * fail such IO. 908bf14e2b2SDamien Le Moal */ 909bf14e2b2SDamien Le Moal if (WARN_ON_ONCE(dm_is_zone_write(md, bio))) 910bf14e2b2SDamien Le Moal error = BLK_STS_IOERR; 911bf14e2b2SDamien Le Moal else 9124e4cbee9SChristoph Hellwig error = BLK_STS_DM_REQUEUE; 913df561f66SGustavo A. R. Silva fallthrough; 9141be56909SChristoph Hellwig case DM_ENDIO_DONE: 9151be56909SChristoph Hellwig break; 9161be56909SChristoph Hellwig case DM_ENDIO_INCOMPLETE: 9171be56909SChristoph Hellwig /* The target will handle the io */ 9181be56909SChristoph Hellwig return; 9191be56909SChristoph Hellwig default: 9201be56909SChristoph Hellwig DMWARN("unimplemented target endio return value: %d", r); 9211be56909SChristoph Hellwig BUG(); 9221be56909SChristoph Hellwig } 9231be56909SChristoph Hellwig } 9241be56909SChristoph Hellwig 925a666e5c0SMikulas Patocka if (unlikely(swap_bios_limit(tio->ti, bio))) { 926a666e5c0SMikulas Patocka struct mapped_device *md = io->md; 927a666e5c0SMikulas Patocka up(&md->swap_bios_semaphore); 928a666e5c0SMikulas Patocka } 929a666e5c0SMikulas Patocka 930cfae7529SMike Snitzer free_tio(tio); 931e2118b3cSDamien Le Moal dm_io_dec_pending(io, error); 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds 93478d8e58aSMike Snitzer /* 93556a67df7SMike Snitzer * Return maximum size of I/O possible at the supplied sector up to the current 93656a67df7SMike Snitzer * target boundary. 93756a67df7SMike Snitzer */ 9383720281dSMike Snitzer static inline sector_t max_io_len_target_boundary(struct dm_target *ti, 9393720281dSMike Snitzer sector_t target_offset) 9401da177e4SLinus Torvalds { 94156a67df7SMike Snitzer return ti->len - target_offset; 94256a67df7SMike Snitzer } 94356a67df7SMike Snitzer 9443720281dSMike Snitzer static sector_t max_io_len(struct dm_target *ti, sector_t sector) 94556a67df7SMike Snitzer { 9463720281dSMike Snitzer sector_t target_offset = dm_target_offset(ti, sector); 9473720281dSMike Snitzer sector_t len = max_io_len_target_boundary(ti, target_offset); 9485091cdecSMike Snitzer sector_t max_len; 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds /* 9513ee16db3SMike Snitzer * Does the target need to split IO even further? 9523ee16db3SMike Snitzer * - varied (per target) IO splitting is a tenet of DM; this 9533ee16db3SMike Snitzer * explains why stacked chunk_sectors based splitting via 9543ee16db3SMike Snitzer * blk_max_size_offset() isn't possible here. So pass in 9553ee16db3SMike Snitzer * ti->max_io_len to override stacked chunk_sectors. 9561da177e4SLinus Torvalds */ 9573ee16db3SMike Snitzer if (ti->max_io_len) { 95833bd6f06SMike Snitzer max_len = blk_max_size_offset(ti->table->md->queue, 9593ee16db3SMike Snitzer target_offset, ti->max_io_len); 960542f9038SMike Snitzer if (len > max_len) 961542f9038SMike Snitzer len = max_len; 9623ee16db3SMike Snitzer } 9631da177e4SLinus Torvalds 9641da177e4SLinus Torvalds return len; 9651da177e4SLinus Torvalds } 9661da177e4SLinus Torvalds 967542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len) 968542f9038SMike Snitzer { 969542f9038SMike Snitzer if (len > UINT_MAX) { 970542f9038SMike Snitzer DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)", 971542f9038SMike Snitzer (unsigned long long)len, UINT_MAX); 972542f9038SMike Snitzer ti->error = "Maximum size of target IO is too large"; 973542f9038SMike Snitzer return -EINVAL; 974542f9038SMike Snitzer } 975542f9038SMike Snitzer 97675ae1936SMikulas Patocka ti->max_io_len = (uint32_t) len; 977542f9038SMike Snitzer 978542f9038SMike Snitzer return 0; 979542f9038SMike Snitzer } 980542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len); 981542f9038SMike Snitzer 982f26c5719SDan Williams static struct dm_target *dm_dax_get_live_target(struct mapped_device *md, 983f26c5719SDan Williams sector_t sector, int *srcu_idx) 9843d97c829SMike Snitzer __acquires(md->io_barrier) 985545ed20eSToshi Kani { 986545ed20eSToshi Kani struct dm_table *map; 987545ed20eSToshi Kani struct dm_target *ti; 988545ed20eSToshi Kani 989f26c5719SDan Williams map = dm_get_live_table(md, srcu_idx); 990545ed20eSToshi Kani if (!map) 991f26c5719SDan Williams return NULL; 992545ed20eSToshi Kani 993545ed20eSToshi Kani ti = dm_table_find_target(map, sector); 994123d87d5SMikulas Patocka if (!ti) 995f26c5719SDan Williams return NULL; 996f26c5719SDan Williams 997f26c5719SDan Williams return ti; 998f26c5719SDan Williams } 999f26c5719SDan Williams 1000f26c5719SDan Williams static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, 1001f26c5719SDan Williams long nr_pages, void **kaddr, pfn_t *pfn) 1002f26c5719SDan Williams { 1003f26c5719SDan Williams struct mapped_device *md = dax_get_private(dax_dev); 1004f26c5719SDan Williams sector_t sector = pgoff * PAGE_SECTORS; 1005f26c5719SDan Williams struct dm_target *ti; 1006f26c5719SDan Williams long len, ret = -EIO; 1007f26c5719SDan Williams int srcu_idx; 1008f26c5719SDan Williams 1009f26c5719SDan Williams ti = dm_dax_get_live_target(md, sector, &srcu_idx); 1010f26c5719SDan Williams 1011f26c5719SDan Williams if (!ti) 1012545ed20eSToshi Kani goto out; 1013f26c5719SDan Williams if (!ti->type->direct_access) 1014f26c5719SDan Williams goto out; 10153720281dSMike Snitzer len = max_io_len(ti, sector) / PAGE_SECTORS; 1016f26c5719SDan Williams if (len < 1) 1017f26c5719SDan Williams goto out; 1018f26c5719SDan Williams nr_pages = min(len, nr_pages); 1019817bf402SDan Williams ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn); 1020817bf402SDan Williams 1021545ed20eSToshi Kani out: 1022545ed20eSToshi Kani dm_put_live_table(md, srcu_idx); 1023f26c5719SDan Williams 1024f26c5719SDan Williams return ret; 1025545ed20eSToshi Kani } 1026545ed20eSToshi Kani 10277bf7eac8SDan Williams static bool dm_dax_supported(struct dax_device *dax_dev, struct block_device *bdev, 10287bf7eac8SDan Williams int blocksize, sector_t start, sector_t len) 10297bf7eac8SDan Williams { 10307bf7eac8SDan Williams struct mapped_device *md = dax_get_private(dax_dev); 10317bf7eac8SDan Williams struct dm_table *map; 103202186d88SDan Williams bool ret = false; 10337bf7eac8SDan Williams int srcu_idx; 10347bf7eac8SDan Williams 10357bf7eac8SDan Williams map = dm_get_live_table(md, &srcu_idx); 10367bf7eac8SDan Williams if (!map) 103702186d88SDan Williams goto out; 10387bf7eac8SDan Williams 10395b0fab50SJeffle Xu ret = dm_table_supports_dax(map, device_not_dax_capable, &blocksize); 10407bf7eac8SDan Williams 104102186d88SDan Williams out: 10427bf7eac8SDan Williams dm_put_live_table(md, srcu_idx); 10437bf7eac8SDan Williams 10447bf7eac8SDan Williams return ret; 10457bf7eac8SDan Williams } 10467bf7eac8SDan Williams 10477e026c8cSDan Williams static size_t dm_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, 10487e026c8cSDan Williams void *addr, size_t bytes, struct iov_iter *i) 10497e026c8cSDan Williams { 10507e026c8cSDan Williams struct mapped_device *md = dax_get_private(dax_dev); 10517e026c8cSDan Williams sector_t sector = pgoff * PAGE_SECTORS; 10527e026c8cSDan Williams struct dm_target *ti; 10537e026c8cSDan Williams long ret = 0; 10547e026c8cSDan Williams int srcu_idx; 10557e026c8cSDan Williams 10567e026c8cSDan Williams ti = dm_dax_get_live_target(md, sector, &srcu_idx); 10577e026c8cSDan Williams 10587e026c8cSDan Williams if (!ti) 10597e026c8cSDan Williams goto out; 10607e026c8cSDan Williams if (!ti->type->dax_copy_from_iter) { 10617e026c8cSDan Williams ret = copy_from_iter(addr, bytes, i); 10627e026c8cSDan Williams goto out; 10637e026c8cSDan Williams } 10647e026c8cSDan Williams ret = ti->type->dax_copy_from_iter(ti, pgoff, addr, bytes, i); 10657e026c8cSDan Williams out: 10667e026c8cSDan Williams dm_put_live_table(md, srcu_idx); 10677e026c8cSDan Williams 10687e026c8cSDan Williams return ret; 10697e026c8cSDan Williams } 10707e026c8cSDan Williams 1071b3a9a0c3SDan Williams static size_t dm_dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, 1072b3a9a0c3SDan Williams void *addr, size_t bytes, struct iov_iter *i) 1073b3a9a0c3SDan Williams { 1074b3a9a0c3SDan Williams struct mapped_device *md = dax_get_private(dax_dev); 1075b3a9a0c3SDan Williams sector_t sector = pgoff * PAGE_SECTORS; 1076b3a9a0c3SDan Williams struct dm_target *ti; 1077b3a9a0c3SDan Williams long ret = 0; 1078b3a9a0c3SDan Williams int srcu_idx; 1079b3a9a0c3SDan Williams 1080b3a9a0c3SDan Williams ti = dm_dax_get_live_target(md, sector, &srcu_idx); 1081b3a9a0c3SDan Williams 1082b3a9a0c3SDan Williams if (!ti) 1083b3a9a0c3SDan Williams goto out; 1084b3a9a0c3SDan Williams if (!ti->type->dax_copy_to_iter) { 1085b3a9a0c3SDan Williams ret = copy_to_iter(addr, bytes, i); 1086b3a9a0c3SDan Williams goto out; 1087b3a9a0c3SDan Williams } 1088b3a9a0c3SDan Williams ret = ti->type->dax_copy_to_iter(ti, pgoff, addr, bytes, i); 1089b3a9a0c3SDan Williams out: 1090b3a9a0c3SDan Williams dm_put_live_table(md, srcu_idx); 1091b3a9a0c3SDan Williams 1092b3a9a0c3SDan Williams return ret; 1093b3a9a0c3SDan Williams } 1094b3a9a0c3SDan Williams 1095cdf6cdcdSVivek Goyal static int dm_dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff, 1096cdf6cdcdSVivek Goyal size_t nr_pages) 1097cdf6cdcdSVivek Goyal { 1098cdf6cdcdSVivek Goyal struct mapped_device *md = dax_get_private(dax_dev); 1099cdf6cdcdSVivek Goyal sector_t sector = pgoff * PAGE_SECTORS; 1100cdf6cdcdSVivek Goyal struct dm_target *ti; 1101cdf6cdcdSVivek Goyal int ret = -EIO; 1102cdf6cdcdSVivek Goyal int srcu_idx; 1103cdf6cdcdSVivek Goyal 1104cdf6cdcdSVivek Goyal ti = dm_dax_get_live_target(md, sector, &srcu_idx); 1105cdf6cdcdSVivek Goyal 1106cdf6cdcdSVivek Goyal if (!ti) 1107cdf6cdcdSVivek Goyal goto out; 1108cdf6cdcdSVivek Goyal if (WARN_ON(!ti->type->dax_zero_page_range)) { 1109cdf6cdcdSVivek Goyal /* 1110cdf6cdcdSVivek Goyal * ->zero_page_range() is mandatory dax operation. If we are 1111cdf6cdcdSVivek Goyal * here, something is wrong. 1112cdf6cdcdSVivek Goyal */ 1113cdf6cdcdSVivek Goyal goto out; 1114cdf6cdcdSVivek Goyal } 1115cdf6cdcdSVivek Goyal ret = ti->type->dax_zero_page_range(ti, pgoff, nr_pages); 1116cdf6cdcdSVivek Goyal out: 1117cdf6cdcdSVivek Goyal dm_put_live_table(md, srcu_idx); 1118cdf6cdcdSVivek Goyal 1119cdf6cdcdSVivek Goyal return ret; 1120cdf6cdcdSVivek Goyal } 1121cdf6cdcdSVivek Goyal 11221dd40c3eSMikulas Patocka /* 11231dd40c3eSMikulas Patocka * A target may call dm_accept_partial_bio only from the map routine. It is 11246842d264SDamien Le Moal * allowed for all bio types except REQ_PREFLUSH, REQ_OP_ZONE_* zone management 11256842d264SDamien Le Moal * operations and REQ_OP_ZONE_APPEND (zone append writes). 11261dd40c3eSMikulas Patocka * 11271dd40c3eSMikulas Patocka * dm_accept_partial_bio informs the dm that the target only wants to process 11281dd40c3eSMikulas Patocka * additional n_sectors sectors of the bio and the rest of the data should be 11291dd40c3eSMikulas Patocka * sent in a next bio. 11301dd40c3eSMikulas Patocka * 11311dd40c3eSMikulas Patocka * A diagram that explains the arithmetics: 11321dd40c3eSMikulas Patocka * +--------------------+---------------+-------+ 11331dd40c3eSMikulas Patocka * | 1 | 2 | 3 | 11341dd40c3eSMikulas Patocka * +--------------------+---------------+-------+ 11351dd40c3eSMikulas Patocka * 11361dd40c3eSMikulas Patocka * <-------------- *tio->len_ptr ---------------> 11371dd40c3eSMikulas Patocka * <------- bi_size -------> 11381dd40c3eSMikulas Patocka * <-- n_sectors --> 11391dd40c3eSMikulas Patocka * 11401dd40c3eSMikulas Patocka * Region 1 was already iterated over with bio_advance or similar function. 11411dd40c3eSMikulas Patocka * (it may be empty if the target doesn't use bio_advance) 11421dd40c3eSMikulas Patocka * Region 2 is the remaining bio size that the target wants to process. 11431dd40c3eSMikulas Patocka * (it may be empty if region 1 is non-empty, although there is no reason 11441dd40c3eSMikulas Patocka * to make it empty) 11451dd40c3eSMikulas Patocka * The target requires that region 3 is to be sent in the next bio. 11461dd40c3eSMikulas Patocka * 11471dd40c3eSMikulas Patocka * If the target wants to receive multiple copies of the bio (via num_*bios, etc), 11481dd40c3eSMikulas Patocka * the partially processed part (the sum of regions 1+2) must be the same for all 11491dd40c3eSMikulas Patocka * copies of the bio. 11501dd40c3eSMikulas Patocka */ 11511dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors) 11521dd40c3eSMikulas Patocka { 11531dd40c3eSMikulas Patocka struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); 11541dd40c3eSMikulas Patocka unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT; 11556842d264SDamien Le Moal 11561eff9d32SJens Axboe BUG_ON(bio->bi_opf & REQ_PREFLUSH); 11576842d264SDamien Le Moal BUG_ON(op_is_zone_mgmt(bio_op(bio))); 11586842d264SDamien Le Moal BUG_ON(bio_op(bio) == REQ_OP_ZONE_APPEND); 11591dd40c3eSMikulas Patocka BUG_ON(bi_size > *tio->len_ptr); 11601dd40c3eSMikulas Patocka BUG_ON(n_sectors > bi_size); 11616842d264SDamien Le Moal 11621dd40c3eSMikulas Patocka *tio->len_ptr -= bi_size - n_sectors; 11631dd40c3eSMikulas Patocka bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT; 11641dd40c3eSMikulas Patocka } 11651dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio); 11661dd40c3eSMikulas Patocka 1167a666e5c0SMikulas Patocka static noinline void __set_swap_bios_limit(struct mapped_device *md, int latch) 1168a666e5c0SMikulas Patocka { 1169a666e5c0SMikulas Patocka mutex_lock(&md->swap_bios_lock); 1170a666e5c0SMikulas Patocka while (latch < md->swap_bios) { 1171a666e5c0SMikulas Patocka cond_resched(); 1172a666e5c0SMikulas Patocka down(&md->swap_bios_semaphore); 1173a666e5c0SMikulas Patocka md->swap_bios--; 1174a666e5c0SMikulas Patocka } 1175a666e5c0SMikulas Patocka while (latch > md->swap_bios) { 1176a666e5c0SMikulas Patocka cond_resched(); 1177a666e5c0SMikulas Patocka up(&md->swap_bios_semaphore); 1178a666e5c0SMikulas Patocka md->swap_bios++; 1179a666e5c0SMikulas Patocka } 1180a666e5c0SMikulas Patocka mutex_unlock(&md->swap_bios_lock); 1181a666e5c0SMikulas Patocka } 1182a666e5c0SMikulas Patocka 1183978e51baSMike Snitzer static blk_qc_t __map_bio(struct dm_target_io *tio) 11841da177e4SLinus Torvalds { 11851da177e4SLinus Torvalds int r; 11862056a782SJens Axboe sector_t sector; 1187dba14160SMikulas Patocka struct bio *clone = &tio->clone; 118864f52b0eSMike Snitzer struct dm_io *io = tio->io; 1189bd2a49b8SAlasdair G Kergon struct dm_target *ti = tio->ti; 1190978e51baSMike Snitzer blk_qc_t ret = BLK_QC_T_NONE; 11911da177e4SLinus Torvalds 11921da177e4SLinus Torvalds clone->bi_end_io = clone_endio; 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds /* 11951da177e4SLinus Torvalds * Map the clone. If r == 0 we don't need to do 11961da177e4SLinus Torvalds * anything, the target has assumed ownership of 11971da177e4SLinus Torvalds * this io. 11981da177e4SLinus Torvalds */ 1199e2118b3cSDamien Le Moal dm_io_inc_pending(io); 12004f024f37SKent Overstreet sector = clone->bi_iter.bi_sector; 1201d67a5f4bSMikulas Patocka 1202a666e5c0SMikulas Patocka if (unlikely(swap_bios_limit(ti, clone))) { 1203a666e5c0SMikulas Patocka struct mapped_device *md = io->md; 1204a666e5c0SMikulas Patocka int latch = get_swap_bios(); 1205a666e5c0SMikulas Patocka if (unlikely(latch != md->swap_bios)) 1206a666e5c0SMikulas Patocka __set_swap_bios_limit(md, latch); 1207a666e5c0SMikulas Patocka down(&md->swap_bios_semaphore); 1208a666e5c0SMikulas Patocka } 1209a666e5c0SMikulas Patocka 1210bb37d772SDamien Le Moal /* 1211bb37d772SDamien Le Moal * Check if the IO needs a special mapping due to zone append emulation 1212bb37d772SDamien Le Moal * on zoned target. In this case, dm_zone_map_bio() calls the target 1213bb37d772SDamien Le Moal * map operation. 1214bb37d772SDamien Le Moal */ 1215bb37d772SDamien Le Moal if (dm_emulate_zone_append(io->md)) 1216bb37d772SDamien Le Moal r = dm_zone_map_bio(tio); 1217bb37d772SDamien Le Moal else 12187de3ee57SMikulas Patocka r = ti->type->map(ti, clone); 1219bb37d772SDamien Le Moal 1220846785e6SChristoph Hellwig switch (r) { 1221846785e6SChristoph Hellwig case DM_MAPIO_SUBMITTED: 1222846785e6SChristoph Hellwig break; 1223846785e6SChristoph Hellwig case DM_MAPIO_REMAPPED: 12241da177e4SLinus Torvalds /* the bio has been remapped so dispatch it */ 12251c02fca6SChristoph Hellwig trace_block_bio_remap(clone, bio_dev(io->orig_bio), sector); 1226ed00aabdSChristoph Hellwig ret = submit_bio_noacct(clone); 1227846785e6SChristoph Hellwig break; 1228846785e6SChristoph Hellwig case DM_MAPIO_KILL: 1229a666e5c0SMikulas Patocka if (unlikely(swap_bios_limit(ti, clone))) { 1230a666e5c0SMikulas Patocka struct mapped_device *md = io->md; 1231a666e5c0SMikulas Patocka up(&md->swap_bios_semaphore); 1232a666e5c0SMikulas Patocka } 12334e4cbee9SChristoph Hellwig free_tio(tio); 1234e2118b3cSDamien Le Moal dm_io_dec_pending(io, BLK_STS_IOERR); 12354e4cbee9SChristoph Hellwig break; 1236846785e6SChristoph Hellwig case DM_MAPIO_REQUEUE: 1237a666e5c0SMikulas Patocka if (unlikely(swap_bios_limit(ti, clone))) { 1238a666e5c0SMikulas Patocka struct mapped_device *md = io->md; 1239a666e5c0SMikulas Patocka up(&md->swap_bios_semaphore); 1240a666e5c0SMikulas Patocka } 1241cfae7529SMike Snitzer free_tio(tio); 1242e2118b3cSDamien Le Moal dm_io_dec_pending(io, BLK_STS_DM_REQUEUE); 1243846785e6SChristoph Hellwig break; 1244846785e6SChristoph Hellwig default: 124545cbcd79SKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 124645cbcd79SKiyoshi Ueda BUG(); 12471da177e4SLinus Torvalds } 12481da177e4SLinus Torvalds 1249978e51baSMike Snitzer return ret; 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds 1252e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len) 1253bd2a49b8SAlasdair G Kergon { 12544f024f37SKent Overstreet bio->bi_iter.bi_sector = sector; 12554f024f37SKent Overstreet bio->bi_iter.bi_size = to_bytes(len); 12561da177e4SLinus Torvalds } 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds /* 12591da177e4SLinus Torvalds * Creates a bio that consists of range of complete bvecs. 12601da177e4SLinus Torvalds */ 1261c80914e8SMike Snitzer static int clone_bio(struct dm_target_io *tio, struct bio *bio, 12621c3b13e6SKent Overstreet sector_t sector, unsigned len) 12631da177e4SLinus Torvalds { 1264dba14160SMikulas Patocka struct bio *clone = &tio->clone; 126507560151SEric Biggers int r; 12661da177e4SLinus Torvalds 12671c3b13e6SKent Overstreet __bio_clone_fast(clone, bio); 12689c47008dSMartin K. Petersen 126907560151SEric Biggers r = bio_crypt_clone(clone, bio, GFP_NOIO); 127007560151SEric Biggers if (r < 0) 127107560151SEric Biggers return r; 1272a892c8d5SSatya Tangirala 127357c36519SMike Snitzer if (bio_integrity(bio)) { 1274e2460f2aSMikulas Patocka if (unlikely(!dm_target_has_integrity(tio->ti->type) && 1275e2460f2aSMikulas Patocka !dm_target_passes_integrity(tio->ti->type))) { 1276e2460f2aSMikulas Patocka DMWARN("%s: the target %s doesn't support integrity data.", 1277e2460f2aSMikulas Patocka dm_device_name(tio->io->md), 1278e2460f2aSMikulas Patocka tio->ti->type->name); 1279e2460f2aSMikulas Patocka return -EIO; 1280e2460f2aSMikulas Patocka } 1281e2460f2aSMikulas Patocka 1282e2460f2aSMikulas Patocka r = bio_integrity_clone(clone, bio, GFP_NOIO); 1283c80914e8SMike Snitzer if (r < 0) 1284c80914e8SMike Snitzer return r; 1285c80914e8SMike Snitzer } 12861c3b13e6SKent Overstreet 1287fa8db494SMike Snitzer bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector)); 1288fa8db494SMike Snitzer clone->bi_iter.bi_size = to_bytes(len); 1289fa8db494SMike Snitzer 1290fa8db494SMike Snitzer if (bio_integrity(bio)) 1291fa8db494SMike Snitzer bio_integrity_trim(clone); 1292c80914e8SMike Snitzer 1293c80914e8SMike Snitzer return 0; 12941da177e4SLinus Torvalds } 12951da177e4SLinus Torvalds 1296318716ddSMike Snitzer static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci, 1297318716ddSMike Snitzer struct dm_target *ti, unsigned num_bios) 1298f9ab94ceSMikulas Patocka { 1299dba14160SMikulas Patocka struct dm_target_io *tio; 1300318716ddSMike Snitzer int try; 1301dba14160SMikulas Patocka 1302318716ddSMike Snitzer if (!num_bios) 1303318716ddSMike Snitzer return; 1304f9ab94ceSMikulas Patocka 1305318716ddSMike Snitzer if (num_bios == 1) { 1306318716ddSMike Snitzer tio = alloc_tio(ci, ti, 0, GFP_NOIO); 1307318716ddSMike Snitzer bio_list_add(blist, &tio->clone); 1308318716ddSMike Snitzer return; 13099015df24SAlasdair G Kergon } 13109015df24SAlasdair G Kergon 1311318716ddSMike Snitzer for (try = 0; try < 2; try++) { 1312318716ddSMike Snitzer int bio_nr; 1313318716ddSMike Snitzer struct bio *bio; 1314318716ddSMike Snitzer 1315318716ddSMike Snitzer if (try) 1316bc02cdbeSMike Snitzer mutex_lock(&ci->io->md->table_devices_lock); 1317318716ddSMike Snitzer for (bio_nr = 0; bio_nr < num_bios; bio_nr++) { 1318318716ddSMike Snitzer tio = alloc_tio(ci, ti, bio_nr, try ? GFP_NOIO : GFP_NOWAIT); 1319318716ddSMike Snitzer if (!tio) 1320318716ddSMike Snitzer break; 1321318716ddSMike Snitzer 1322318716ddSMike Snitzer bio_list_add(blist, &tio->clone); 1323318716ddSMike Snitzer } 1324318716ddSMike Snitzer if (try) 1325bc02cdbeSMike Snitzer mutex_unlock(&ci->io->md->table_devices_lock); 1326318716ddSMike Snitzer if (bio_nr == num_bios) 1327318716ddSMike Snitzer return; 1328318716ddSMike Snitzer 1329318716ddSMike Snitzer while ((bio = bio_list_pop(blist))) { 1330318716ddSMike Snitzer tio = container_of(bio, struct dm_target_io, clone); 1331318716ddSMike Snitzer free_tio(tio); 1332318716ddSMike Snitzer } 1333318716ddSMike Snitzer } 1334318716ddSMike Snitzer } 1335318716ddSMike Snitzer 1336978e51baSMike Snitzer static blk_qc_t __clone_and_map_simple_bio(struct clone_info *ci, 1337318716ddSMike Snitzer struct dm_target_io *tio, unsigned *len) 13389015df24SAlasdair G Kergon { 1339dba14160SMikulas Patocka struct bio *clone = &tio->clone; 13409015df24SAlasdair G Kergon 13411dd40c3eSMikulas Patocka tio->len_ptr = len; 13421dd40c3eSMikulas Patocka 13431c3b13e6SKent Overstreet __bio_clone_fast(clone, ci->bio); 1344bd2a49b8SAlasdair G Kergon if (len) 13451dd40c3eSMikulas Patocka bio_setup_sector(clone, ci->sector, *len); 1346f9ab94ceSMikulas Patocka 1347978e51baSMike Snitzer return __map_bio(tio); 1348f9ab94ceSMikulas Patocka } 1349f9ab94ceSMikulas Patocka 135014fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti, 13511dd40c3eSMikulas Patocka unsigned num_bios, unsigned *len) 135206a426ceSMike Snitzer { 1353318716ddSMike Snitzer struct bio_list blist = BIO_EMPTY_LIST; 1354318716ddSMike Snitzer struct bio *bio; 1355318716ddSMike Snitzer struct dm_target_io *tio; 135606a426ceSMike Snitzer 1357318716ddSMike Snitzer alloc_multiple_bios(&blist, ci, ti, num_bios); 1358318716ddSMike Snitzer 1359318716ddSMike Snitzer while ((bio = bio_list_pop(&blist))) { 1360318716ddSMike Snitzer tio = container_of(bio, struct dm_target_io, clone); 1361978e51baSMike Snitzer (void) __clone_and_map_simple_bio(ci, tio, len); 1362318716ddSMike Snitzer } 136306a426ceSMike Snitzer } 136406a426ceSMike Snitzer 136514fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci) 1366f9ab94ceSMikulas Patocka { 136706a426ceSMike Snitzer unsigned target_nr = 0; 1368f9ab94ceSMikulas Patocka struct dm_target *ti; 1369828678b8SMike Snitzer struct bio flush_bio; 1370828678b8SMike Snitzer 1371828678b8SMike Snitzer /* 1372828678b8SMike Snitzer * Use an on-stack bio for this, it's safe since we don't 1373828678b8SMike Snitzer * need to reference it after submit. It's just used as 1374828678b8SMike Snitzer * the basis for the clone(s). 1375828678b8SMike Snitzer */ 1376828678b8SMike Snitzer bio_init(&flush_bio, NULL, 0); 1377828678b8SMike Snitzer flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC; 1378309dca30SChristoph Hellwig bio_set_dev(&flush_bio, ci->io->md->disk->part0); 137947d95102SChristoph Hellwig 1380828678b8SMike Snitzer ci->bio = &flush_bio; 1381828678b8SMike Snitzer ci->sector_count = 0; 1382f9ab94ceSMikulas Patocka 1383b372d360SMike Snitzer BUG_ON(bio_has_data(ci->bio)); 1384f9ab94ceSMikulas Patocka while ((ti = dm_table_get_target(ci->map, target_nr++))) 13851dd40c3eSMikulas Patocka __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL); 1386828678b8SMike Snitzer 1387828678b8SMike Snitzer bio_uninit(ci->bio); 1388f9ab94ceSMikulas Patocka return 0; 1389f9ab94ceSMikulas Patocka } 1390f9ab94ceSMikulas Patocka 1391c80914e8SMike Snitzer static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti, 13921dd40c3eSMikulas Patocka sector_t sector, unsigned *len) 13935ae89a87SMike Snitzer { 1394dba14160SMikulas Patocka struct bio *bio = ci->bio; 13955ae89a87SMike Snitzer struct dm_target_io *tio; 1396f31c21e4SNeilBrown int r; 13975ae89a87SMike Snitzer 1398318716ddSMike Snitzer tio = alloc_tio(ci, ti, 0, GFP_NOIO); 13991dd40c3eSMikulas Patocka tio->len_ptr = len; 1400c80914e8SMike Snitzer r = clone_bio(tio, bio, sector, *len); 1401072623deSMikulas Patocka if (r < 0) { 1402cfae7529SMike Snitzer free_tio(tio); 1403c80914e8SMike Snitzer return r; 1404b0d8ed4dSAlasdair G Kergon } 1405978e51baSMike Snitzer (void) __map_bio(tio); 140655a62eefSAlasdair G Kergon 1407f31c21e4SNeilBrown return 0; 140823508a96SMike Snitzer } 140955a62eefSAlasdair G Kergon 14103d7f4562SMike Snitzer static int __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti, 141161697a6aSMike Snitzer unsigned num_bios) 14125ae89a87SMike Snitzer { 141351b86f9aSMichael Lass unsigned len; 14145ae89a87SMike Snitzer 14155ae89a87SMike Snitzer /* 141623508a96SMike Snitzer * Even though the device advertised support for this type of 141723508a96SMike Snitzer * request, that does not mean every target supports it, and 1418936688d7SMike Snitzer * reconfiguration might also have changed that since the 14195ae89a87SMike Snitzer * check was performed. 14205ae89a87SMike Snitzer */ 142155a62eefSAlasdair G Kergon if (!num_bios) 14225ae89a87SMike Snitzer return -EOPNOTSUPP; 14235ae89a87SMike Snitzer 14243720281dSMike Snitzer len = min_t(sector_t, ci->sector_count, 14253720281dSMike Snitzer max_io_len_target_boundary(ti, dm_target_offset(ti, ci->sector))); 142651b86f9aSMichael Lass 14271dd40c3eSMikulas Patocka __send_duplicate_bios(ci, ti, num_bios, &len); 14285ae89a87SMike Snitzer 1429a79245b3SMike Snitzer ci->sector += len; 14303d7f4562SMike Snitzer ci->sector_count -= len; 14315ae89a87SMike Snitzer 14325ae89a87SMike Snitzer return 0; 14335ae89a87SMike Snitzer } 14345ae89a87SMike Snitzer 1435568c73a3SMike Snitzer static bool is_abnormal_io(struct bio *bio) 1436568c73a3SMike Snitzer { 1437568c73a3SMike Snitzer bool r = false; 1438568c73a3SMike Snitzer 1439568c73a3SMike Snitzer switch (bio_op(bio)) { 1440568c73a3SMike Snitzer case REQ_OP_DISCARD: 1441568c73a3SMike Snitzer case REQ_OP_SECURE_ERASE: 1442568c73a3SMike Snitzer case REQ_OP_WRITE_SAME: 1443568c73a3SMike Snitzer case REQ_OP_WRITE_ZEROES: 1444568c73a3SMike Snitzer r = true; 1445568c73a3SMike Snitzer break; 1446568c73a3SMike Snitzer } 1447568c73a3SMike Snitzer 1448568c73a3SMike Snitzer return r; 1449568c73a3SMike Snitzer } 1450568c73a3SMike Snitzer 14510519c71eSMike Snitzer static bool __process_abnormal_io(struct clone_info *ci, struct dm_target *ti, 14520519c71eSMike Snitzer int *result) 14530519c71eSMike Snitzer { 14540519c71eSMike Snitzer struct bio *bio = ci->bio; 14559679b5a7SMike Snitzer unsigned num_bios = 0; 14560519c71eSMike Snitzer 14579679b5a7SMike Snitzer switch (bio_op(bio)) { 14589679b5a7SMike Snitzer case REQ_OP_DISCARD: 14599679b5a7SMike Snitzer num_bios = ti->num_discard_bios; 14609679b5a7SMike Snitzer break; 14619679b5a7SMike Snitzer case REQ_OP_SECURE_ERASE: 14629679b5a7SMike Snitzer num_bios = ti->num_secure_erase_bios; 14639679b5a7SMike Snitzer break; 14649679b5a7SMike Snitzer case REQ_OP_WRITE_SAME: 14659679b5a7SMike Snitzer num_bios = ti->num_write_same_bios; 14669679b5a7SMike Snitzer break; 14679679b5a7SMike Snitzer case REQ_OP_WRITE_ZEROES: 14689679b5a7SMike Snitzer num_bios = ti->num_write_zeroes_bios; 14699679b5a7SMike Snitzer break; 14709679b5a7SMike Snitzer default: 14710519c71eSMike Snitzer return false; 14729679b5a7SMike Snitzer } 14730519c71eSMike Snitzer 14749679b5a7SMike Snitzer *result = __send_changing_extent_only(ci, ti, num_bios); 14750519c71eSMike Snitzer return true; 14760519c71eSMike Snitzer } 14770519c71eSMike Snitzer 1478e4c93811SAlasdair G Kergon /* 1479e4c93811SAlasdair G Kergon * Select the correct strategy for processing a non-flush bio. 1480e4c93811SAlasdair G Kergon */ 1481e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci) 1482e4c93811SAlasdair G Kergon { 1483e4c93811SAlasdair G Kergon struct dm_target *ti; 14841c3b13e6SKent Overstreet unsigned len; 1485c80914e8SMike Snitzer int r; 1486e4c93811SAlasdair G Kergon 1487e4c93811SAlasdair G Kergon ti = dm_table_find_target(ci->map, ci->sector); 1488123d87d5SMikulas Patocka if (!ti) 1489e4c93811SAlasdair G Kergon return -EIO; 1490e4c93811SAlasdair G Kergon 1491568c73a3SMike Snitzer if (__process_abnormal_io(ci, ti, &r)) 14920519c71eSMike Snitzer return r; 14933d7f4562SMike Snitzer 14943720281dSMike Snitzer len = min_t(sector_t, max_io_len(ti, ci->sector), ci->sector_count); 1495e4c93811SAlasdair G Kergon 1496c80914e8SMike Snitzer r = __clone_and_map_data_bio(ci, ti, ci->sector, &len); 1497c80914e8SMike Snitzer if (r < 0) 1498c80914e8SMike Snitzer return r; 1499e4c93811SAlasdair G Kergon 1500e4c93811SAlasdair G Kergon ci->sector += len; 1501e4c93811SAlasdair G Kergon ci->sector_count -= len; 1502e4c93811SAlasdair G Kergon 1503e4c93811SAlasdair G Kergon return 0; 1504e4c93811SAlasdair G Kergon } 1505e4c93811SAlasdair G Kergon 1506978e51baSMike Snitzer static void init_clone_info(struct clone_info *ci, struct mapped_device *md, 1507978e51baSMike Snitzer struct dm_table *map, struct bio *bio) 1508978e51baSMike Snitzer { 1509978e51baSMike Snitzer ci->map = map; 1510978e51baSMike Snitzer ci->io = alloc_io(md, bio); 1511978e51baSMike Snitzer ci->sector = bio->bi_iter.bi_sector; 1512978e51baSMike Snitzer } 1513978e51baSMike Snitzer 1514a1e1cb72SMike Snitzer #define __dm_part_stat_sub(part, field, subnd) \ 1515a1e1cb72SMike Snitzer (part_stat_get(part, field) -= (subnd)) 1516a1e1cb72SMike Snitzer 1517e4c93811SAlasdair G Kergon /* 151814fe594dSAlasdair G Kergon * Entry point to split a bio into clones and submit them to the targets. 15191da177e4SLinus Torvalds */ 1520978e51baSMike Snitzer static blk_qc_t __split_and_process_bio(struct mapped_device *md, 152183d5e5b0SMikulas Patocka struct dm_table *map, struct bio *bio) 15221da177e4SLinus Torvalds { 15231da177e4SLinus Torvalds struct clone_info ci; 1524978e51baSMike Snitzer blk_qc_t ret = BLK_QC_T_NONE; 1525512875bdSJun'ichi Nomura int error = 0; 15261da177e4SLinus Torvalds 1527978e51baSMike Snitzer init_clone_info(&ci, md, map, bio); 1528bd2a49b8SAlasdair G Kergon 15291eff9d32SJens Axboe if (bio->bi_opf & REQ_PREFLUSH) { 153014fe594dSAlasdair G Kergon error = __send_empty_flush(&ci); 1531e2118b3cSDamien Le Moal /* dm_io_dec_pending submits any data associated with flush */ 15322e2d6f7eSAjay Joshi } else if (op_is_zone_mgmt(bio_op(bio))) { 1533a4aa5e56SDamien Le Moal ci.bio = bio; 1534a4aa5e56SDamien Le Moal ci.sector_count = 0; 1535a4aa5e56SDamien Le Moal error = __split_and_process_non_flush(&ci); 1536b372d360SMike Snitzer } else { 15376a8736d1STejun Heo ci.bio = bio; 15381da177e4SLinus Torvalds ci.sector_count = bio_sectors(bio); 153914fe594dSAlasdair G Kergon error = __split_and_process_non_flush(&ci); 1540985eabdcSJeffle Xu if (ci.sector_count && !error) { 154118a25da8SNeilBrown /* 1542ed00aabdSChristoph Hellwig * Remainder must be passed to submit_bio_noacct() 154318a25da8SNeilBrown * so that it gets handled *after* bios already submitted 154418a25da8SNeilBrown * have been completely processed. 154518a25da8SNeilBrown * We take a clone of the original to store in 1546745dc570SMike Snitzer * ci.io->orig_bio to be used by end_io_acct() and 154718a25da8SNeilBrown * for dec_pending to use for completion handling. 154818a25da8SNeilBrown */ 1549f21c601aSMike Snitzer struct bio *b = bio_split(bio, bio_sectors(bio) - ci.sector_count, 1550f21c601aSMike Snitzer GFP_NOIO, &md->queue->bio_split); 1551745dc570SMike Snitzer ci.io->orig_bio = b; 1552a1e1cb72SMike Snitzer 1553a1e1cb72SMike Snitzer /* 1554a1e1cb72SMike Snitzer * Adjust IO stats for each split, otherwise upon queue 1555a1e1cb72SMike Snitzer * reentry there will be redundant IO accounting. 1556a1e1cb72SMike Snitzer * NOTE: this is a stop-gap fix, a proper fix involves 1557a1e1cb72SMike Snitzer * significant refactoring of DM core's bio splitting 1558a1e1cb72SMike Snitzer * (by eliminating DM's splitting and just using bio_split) 1559a1e1cb72SMike Snitzer */ 1560a1e1cb72SMike Snitzer part_stat_lock(); 15618446fe92SChristoph Hellwig __dm_part_stat_sub(dm_disk(md)->part0, 1562a1e1cb72SMike Snitzer sectors[op_stat_group(bio_op(bio))], ci.sector_count); 1563a1e1cb72SMike Snitzer part_stat_unlock(); 1564a1e1cb72SMike Snitzer 156518a25da8SNeilBrown bio_chain(b, bio); 1566eb6f7f7cSChristoph Hellwig trace_block_split(b, bio->bi_iter.bi_sector); 1567ed00aabdSChristoph Hellwig ret = submit_bio_noacct(bio); 156818a25da8SNeilBrown } 1569d87f4c14STejun Heo } 15701da177e4SLinus Torvalds 15711da177e4SLinus Torvalds /* drop the extra reference count */ 1572e2118b3cSDamien Le Moal dm_io_dec_pending(ci.io, errno_to_blk_status(error)); 1573978e51baSMike Snitzer return ret; 15741da177e4SLinus Torvalds } 15751da177e4SLinus Torvalds 1576c62b37d9SChristoph Hellwig static blk_qc_t dm_submit_bio(struct bio *bio) 15771da177e4SLinus Torvalds { 1578309dca30SChristoph Hellwig struct mapped_device *md = bio->bi_bdev->bd_disk->private_data; 1579978e51baSMike Snitzer blk_qc_t ret = BLK_QC_T_NONE; 158083d5e5b0SMikulas Patocka int srcu_idx; 158183d5e5b0SMikulas Patocka struct dm_table *map; 15821da177e4SLinus Torvalds 158383d5e5b0SMikulas Patocka map = dm_get_live_table(md, &srcu_idx); 1584b2abdb1bSMike Snitzer if (unlikely(!map)) { 1585b2abdb1bSMike Snitzer DMERR_LIMIT("%s: mapping table unavailable, erroring io", 1586b2abdb1bSMike Snitzer dm_device_name(md)); 15876a8736d1STejun Heo bio_io_error(bio); 1588b2abdb1bSMike Snitzer goto out; 15891da177e4SLinus Torvalds } 159092c63902SMikulas Patocka 1591b2abdb1bSMike Snitzer /* If suspended, queue this IO for later */ 15921da177e4SLinus Torvalds if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) { 15936abc4946SKonstantin Khlebnikov if (bio->bi_opf & REQ_NOWAIT) 15946abc4946SKonstantin Khlebnikov bio_wouldblock_error(bio); 1595b2abdb1bSMike Snitzer else if (bio->bi_opf & REQ_RAHEAD) 15961da177e4SLinus Torvalds bio_io_error(bio); 1597b2abdb1bSMike Snitzer else 1598b2abdb1bSMike Snitzer queue_io(md, bio); 1599b2abdb1bSMike Snitzer goto out; 16001da177e4SLinus Torvalds } 16011da177e4SLinus Torvalds 1602b2abdb1bSMike Snitzer /* 1603b2abdb1bSMike Snitzer * Use blk_queue_split() for abnormal IO (e.g. discard, writesame, etc) 1604b2abdb1bSMike Snitzer * otherwise associated queue_limits won't be imposed. 1605b2abdb1bSMike Snitzer */ 1606b2abdb1bSMike Snitzer if (is_abnormal_io(bio)) 1607b2abdb1bSMike Snitzer blk_queue_split(&bio); 1608978e51baSMike Snitzer 1609b2abdb1bSMike Snitzer ret = __split_and_process_bio(md, map, bio); 1610b2abdb1bSMike Snitzer out: 161183d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 1612978e51baSMike Snitzer return ret; 1613978e51baSMike Snitzer } 1614978e51baSMike Snitzer 16151da177e4SLinus Torvalds /*----------------------------------------------------------------- 16161da177e4SLinus Torvalds * An IDR is used to keep track of allocated minor numbers. 16171da177e4SLinus Torvalds *---------------------------------------------------------------*/ 16182b06cfffSAlasdair G Kergon static void free_minor(int minor) 16191da177e4SLinus Torvalds { 1620f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 16211da177e4SLinus Torvalds idr_remove(&_minor_idr, minor); 1622f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 16231da177e4SLinus Torvalds } 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds /* 16261da177e4SLinus Torvalds * See if the device with a specific minor # is free. 16271da177e4SLinus Torvalds */ 1628cf13ab8eSFrederik Deweerdt static int specific_minor(int minor) 16291da177e4SLinus Torvalds { 1630c9d76be6STejun Heo int r; 16311da177e4SLinus Torvalds 16321da177e4SLinus Torvalds if (minor >= (1 << MINORBITS)) 16331da177e4SLinus Torvalds return -EINVAL; 16341da177e4SLinus Torvalds 1635c9d76be6STejun Heo idr_preload(GFP_KERNEL); 1636f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 16371da177e4SLinus Torvalds 1638c9d76be6STejun Heo r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT); 16391da177e4SLinus Torvalds 1640f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1641c9d76be6STejun Heo idr_preload_end(); 1642c9d76be6STejun Heo if (r < 0) 1643c9d76be6STejun Heo return r == -ENOSPC ? -EBUSY : r; 1644c9d76be6STejun Heo return 0; 16451da177e4SLinus Torvalds } 16461da177e4SLinus Torvalds 1647cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor) 16481da177e4SLinus Torvalds { 1649c9d76be6STejun Heo int r; 16501da177e4SLinus Torvalds 1651c9d76be6STejun Heo idr_preload(GFP_KERNEL); 1652f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 16531da177e4SLinus Torvalds 1654c9d76be6STejun Heo r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT); 16551da177e4SLinus Torvalds 1656f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1657c9d76be6STejun Heo idr_preload_end(); 1658c9d76be6STejun Heo if (r < 0) 16591da177e4SLinus Torvalds return r; 1660c9d76be6STejun Heo *minor = r; 1661c9d76be6STejun Heo return 0; 16621da177e4SLinus Torvalds } 16631da177e4SLinus Torvalds 166483d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops; 1665681cc5e8SMike Snitzer static const struct block_device_operations dm_rq_blk_dops; 1666f26c5719SDan Williams static const struct dax_operations dm_dax_ops; 16671da177e4SLinus Torvalds 166853d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work); 166953d5914fSMikulas Patocka 1670aa6ce87aSSatya Tangirala #ifdef CONFIG_BLK_INLINE_ENCRYPTION 1671aa6ce87aSSatya Tangirala static void dm_queue_destroy_keyslot_manager(struct request_queue *q) 1672aa6ce87aSSatya Tangirala { 1673aa6ce87aSSatya Tangirala dm_destroy_keyslot_manager(q->ksm); 1674aa6ce87aSSatya Tangirala } 1675aa6ce87aSSatya Tangirala 1676aa6ce87aSSatya Tangirala #else /* CONFIG_BLK_INLINE_ENCRYPTION */ 1677aa6ce87aSSatya Tangirala 1678aa6ce87aSSatya Tangirala static inline void dm_queue_destroy_keyslot_manager(struct request_queue *q) 1679aa6ce87aSSatya Tangirala { 1680aa6ce87aSSatya Tangirala } 1681aa6ce87aSSatya Tangirala #endif /* !CONFIG_BLK_INLINE_ENCRYPTION */ 1682aa6ce87aSSatya Tangirala 16830f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md) 16840f20972fSMike Snitzer { 16850f20972fSMike Snitzer if (md->wq) 16860f20972fSMike Snitzer destroy_workqueue(md->wq); 16876f1c819cSKent Overstreet bioset_exit(&md->bs); 16886f1c819cSKent Overstreet bioset_exit(&md->io_bs); 16890f20972fSMike Snitzer 1690f26c5719SDan Williams if (md->dax_dev) { 1691f26c5719SDan Williams kill_dax(md->dax_dev); 1692f26c5719SDan Williams put_dax(md->dax_dev); 1693f26c5719SDan Williams md->dax_dev = NULL; 1694f26c5719SDan Williams } 1695f26c5719SDan Williams 16960f20972fSMike Snitzer if (md->disk) { 16970f20972fSMike Snitzer spin_lock(&_minor_lock); 16980f20972fSMike Snitzer md->disk->private_data = NULL; 16990f20972fSMike Snitzer spin_unlock(&_minor_lock); 170089f871afSChristoph Hellwig if (dm_get_md_type(md) != DM_TYPE_NONE) { 170189f871afSChristoph Hellwig dm_sysfs_exit(md); 17020f20972fSMike Snitzer del_gendisk(md->disk); 170389f871afSChristoph Hellwig } 1704aa6ce87aSSatya Tangirala dm_queue_destroy_keyslot_manager(md->queue); 170574fe6ba9SChristoph Hellwig blk_cleanup_disk(md->disk); 170674a2b6ecSChristoph Hellwig } 17070f20972fSMike Snitzer 1708d09960b0STahsin Erdogan cleanup_srcu_struct(&md->io_barrier); 1709d09960b0STahsin Erdogan 1710d5ffebddSMike Snitzer mutex_destroy(&md->suspend_lock); 1711d5ffebddSMike Snitzer mutex_destroy(&md->type_lock); 1712d5ffebddSMike Snitzer mutex_destroy(&md->table_devices_lock); 1713a666e5c0SMikulas Patocka mutex_destroy(&md->swap_bios_lock); 1714d5ffebddSMike Snitzer 17154cc96131SMike Snitzer dm_mq_cleanup_mapped_device(md); 1716bb37d772SDamien Le Moal dm_cleanup_zoned_dev(md); 17170f20972fSMike Snitzer } 17180f20972fSMike Snitzer 17191da177e4SLinus Torvalds /* 17201da177e4SLinus Torvalds * Allocate and initialise a blank device with a given minor. 17211da177e4SLinus Torvalds */ 17222b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor) 17231da177e4SLinus Torvalds { 1724115485e8SMike Snitzer int r, numa_node_id = dm_get_numa_node(); 1725115485e8SMike Snitzer struct mapped_device *md; 1726ba61fdd1SJeff Mahoney void *old_md; 17271da177e4SLinus Torvalds 1728856eb091SMikulas Patocka md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id); 17291da177e4SLinus Torvalds if (!md) { 17301da177e4SLinus Torvalds DMWARN("unable to allocate device, out of memory."); 17311da177e4SLinus Torvalds return NULL; 17321da177e4SLinus Torvalds } 17331da177e4SLinus Torvalds 173410da4f79SJeff Mahoney if (!try_module_get(THIS_MODULE)) 17356ed7ade8SMilan Broz goto bad_module_get; 173610da4f79SJeff Mahoney 17371da177e4SLinus Torvalds /* get a minor number for the dev */ 17382b06cfffSAlasdair G Kergon if (minor == DM_ANY_MINOR) 1739cf13ab8eSFrederik Deweerdt r = next_free_minor(&minor); 17402b06cfffSAlasdair G Kergon else 1741cf13ab8eSFrederik Deweerdt r = specific_minor(minor); 17421da177e4SLinus Torvalds if (r < 0) 17436ed7ade8SMilan Broz goto bad_minor; 17441da177e4SLinus Torvalds 174583d5e5b0SMikulas Patocka r = init_srcu_struct(&md->io_barrier); 174683d5e5b0SMikulas Patocka if (r < 0) 174783d5e5b0SMikulas Patocka goto bad_io_barrier; 174883d5e5b0SMikulas Patocka 1749115485e8SMike Snitzer md->numa_node_id = numa_node_id; 1750591ddcfcSMike Snitzer md->init_tio_pdu = false; 1751a5664dadSMike Snitzer md->type = DM_TYPE_NONE; 1752e61290a4SDaniel Walker mutex_init(&md->suspend_lock); 1753a5664dadSMike Snitzer mutex_init(&md->type_lock); 175486f1152bSBenjamin Marzinski mutex_init(&md->table_devices_lock); 1755022c2611SMikulas Patocka spin_lock_init(&md->deferred_lock); 17561da177e4SLinus Torvalds atomic_set(&md->holders, 1); 17575c6bd75dSAlasdair G Kergon atomic_set(&md->open_count, 0); 17581da177e4SLinus Torvalds atomic_set(&md->event_nr, 0); 17597a8c3d3bSMike Anderson atomic_set(&md->uevent_seq, 0); 17607a8c3d3bSMike Anderson INIT_LIST_HEAD(&md->uevent_list); 176186f1152bSBenjamin Marzinski INIT_LIST_HEAD(&md->table_devices); 17627a8c3d3bSMike Anderson spin_lock_init(&md->uevent_lock); 17631da177e4SLinus Torvalds 176447ace7e0SMike Snitzer /* 1765c62b37d9SChristoph Hellwig * default to bio-based until DM table is loaded and md->type 1766c62b37d9SChristoph Hellwig * established. If request-based table is loaded: blk-mq will 1767c62b37d9SChristoph Hellwig * override accordingly. 176847ace7e0SMike Snitzer */ 176974fe6ba9SChristoph Hellwig md->disk = blk_alloc_disk(md->numa_node_id); 17701da177e4SLinus Torvalds if (!md->disk) 17710f20972fSMike Snitzer goto bad; 177274fe6ba9SChristoph Hellwig md->queue = md->disk->queue; 17731da177e4SLinus Torvalds 1774f0b04115SJeff Mahoney init_waitqueue_head(&md->wait); 177553d5914fSMikulas Patocka INIT_WORK(&md->work, dm_wq_work); 1776f0b04115SJeff Mahoney init_waitqueue_head(&md->eventq); 17772995fa78SMikulas Patocka init_completion(&md->kobj_holder.completion); 1778f0b04115SJeff Mahoney 1779a666e5c0SMikulas Patocka md->swap_bios = get_swap_bios(); 1780a666e5c0SMikulas Patocka sema_init(&md->swap_bios_semaphore, md->swap_bios); 1781a666e5c0SMikulas Patocka mutex_init(&md->swap_bios_lock); 1782a666e5c0SMikulas Patocka 17831da177e4SLinus Torvalds md->disk->major = _major; 17841da177e4SLinus Torvalds md->disk->first_minor = minor; 178574fe6ba9SChristoph Hellwig md->disk->minors = 1; 17861da177e4SLinus Torvalds md->disk->fops = &dm_blk_dops; 17871da177e4SLinus Torvalds md->disk->queue = md->queue; 17881da177e4SLinus Torvalds md->disk->private_data = md; 17891da177e4SLinus Torvalds sprintf(md->disk->disk_name, "dm-%d", minor); 1790f26c5719SDan Williams 1791976431b0SDan Williams if (IS_ENABLED(CONFIG_DAX_DRIVER)) { 1792fefc1d97SPankaj Gupta md->dax_dev = alloc_dax(md, md->disk->disk_name, 1793fefc1d97SPankaj Gupta &dm_dax_ops, 0); 17944e4ced93SVivek Goyal if (IS_ERR(md->dax_dev)) 1795f26c5719SDan Williams goto bad; 1796976431b0SDan Williams } 1797f26c5719SDan Williams 17987e51f257SMike Anderson format_dev_t(md->name, MKDEV(_major, minor)); 17991da177e4SLinus Torvalds 1800670368a8STejun Heo md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0); 1801304f3f6aSMilan Broz if (!md->wq) 18020f20972fSMike Snitzer goto bad; 1803304f3f6aSMilan Broz 1804fd2ed4d2SMikulas Patocka dm_stats_init(&md->stats); 1805fd2ed4d2SMikulas Patocka 1806ba61fdd1SJeff Mahoney /* Populate the mapping, nobody knows we exist yet */ 1807f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 1808ba61fdd1SJeff Mahoney old_md = idr_replace(&_minor_idr, md, minor); 1809f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1810ba61fdd1SJeff Mahoney 1811ba61fdd1SJeff Mahoney BUG_ON(old_md != MINOR_ALLOCED); 1812ba61fdd1SJeff Mahoney 18131da177e4SLinus Torvalds return md; 18141da177e4SLinus Torvalds 18150f20972fSMike Snitzer bad: 18160f20972fSMike Snitzer cleanup_mapped_device(md); 181783d5e5b0SMikulas Patocka bad_io_barrier: 18181da177e4SLinus Torvalds free_minor(minor); 18196ed7ade8SMilan Broz bad_minor: 182010da4f79SJeff Mahoney module_put(THIS_MODULE); 18216ed7ade8SMilan Broz bad_module_get: 1822856eb091SMikulas Patocka kvfree(md); 18231da177e4SLinus Torvalds return NULL; 18241da177e4SLinus Torvalds } 18251da177e4SLinus Torvalds 1826ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md); 1827ae9da83fSJun'ichi Nomura 18281da177e4SLinus Torvalds static void free_dev(struct mapped_device *md) 18291da177e4SLinus Torvalds { 1830f331c029STejun Heo int minor = MINOR(disk_devt(md->disk)); 183163d94e48SJun'ichi Nomura 1832ae9da83fSJun'ichi Nomura unlock_fs(md); 18332eb6e1e3SKeith Busch 18340f20972fSMike Snitzer cleanup_mapped_device(md); 18350f20972fSMike Snitzer 18360f20972fSMike Snitzer free_table_devices(&md->table_devices); 18370f20972fSMike Snitzer dm_stats_cleanup(&md->stats); 183863a4f065SMike Snitzer free_minor(minor); 183963a4f065SMike Snitzer 184010da4f79SJeff Mahoney module_put(THIS_MODULE); 1841856eb091SMikulas Patocka kvfree(md); 18421da177e4SLinus Torvalds } 18431da177e4SLinus Torvalds 18442a2a4c51SJens Axboe static int __bind_mempools(struct mapped_device *md, struct dm_table *t) 1845e6ee8c0bSKiyoshi Ueda { 1846c0820cf5SMikulas Patocka struct dm_md_mempools *p = dm_table_get_md_mempools(t); 18472a2a4c51SJens Axboe int ret = 0; 1848e6ee8c0bSKiyoshi Ueda 1849545ed20eSToshi Kani if (dm_table_bio_based(t)) { 1850c0820cf5SMikulas Patocka /* 185164f52b0eSMike Snitzer * The md may already have mempools that need changing. 185264f52b0eSMike Snitzer * If so, reload bioset because front_pad may have changed 185316245bdcSJun'ichi Nomura * because a different table was loaded. 1854c0820cf5SMikulas Patocka */ 18556f1c819cSKent Overstreet bioset_exit(&md->bs); 18566f1c819cSKent Overstreet bioset_exit(&md->io_bs); 18570776aa0eSMike Snitzer 18586f1c819cSKent Overstreet } else if (bioset_initialized(&md->bs)) { 1859cbc4e3c1SMike Snitzer /* 18604e6e36c3SMike Snitzer * There's no need to reload with request-based dm 18614e6e36c3SMike Snitzer * because the size of front_pad doesn't change. 18624e6e36c3SMike Snitzer * Note for future: If you are to reload bioset, 18634e6e36c3SMike Snitzer * prep-ed requests in the queue may refer 18644e6e36c3SMike Snitzer * to bio from the old bioset, so you must walk 18654e6e36c3SMike Snitzer * through the queue to unprep. 1866cbc4e3c1SMike Snitzer */ 1867cbc4e3c1SMike Snitzer goto out; 1868cbc4e3c1SMike Snitzer } 1869cbc4e3c1SMike Snitzer 18706f1c819cSKent Overstreet BUG_ON(!p || 18716f1c819cSKent Overstreet bioset_initialized(&md->bs) || 18726f1c819cSKent Overstreet bioset_initialized(&md->io_bs)); 1873e6ee8c0bSKiyoshi Ueda 18742a2a4c51SJens Axboe ret = bioset_init_from_src(&md->bs, &p->bs); 18752a2a4c51SJens Axboe if (ret) 18762a2a4c51SJens Axboe goto out; 18772a2a4c51SJens Axboe ret = bioset_init_from_src(&md->io_bs, &p->io_bs); 18782a2a4c51SJens Axboe if (ret) 18792a2a4c51SJens Axboe bioset_exit(&md->bs); 1880e6ee8c0bSKiyoshi Ueda out: 188102233342SMike Snitzer /* mempool bind completed, no longer need any mempools in the table */ 1882e6ee8c0bSKiyoshi Ueda dm_table_free_md_mempools(t); 18832a2a4c51SJens Axboe return ret; 1884e6ee8c0bSKiyoshi Ueda } 1885e6ee8c0bSKiyoshi Ueda 18861da177e4SLinus Torvalds /* 18871da177e4SLinus Torvalds * Bind a table to the device. 18881da177e4SLinus Torvalds */ 18891da177e4SLinus Torvalds static void event_callback(void *context) 18901da177e4SLinus Torvalds { 18917a8c3d3bSMike Anderson unsigned long flags; 18927a8c3d3bSMike Anderson LIST_HEAD(uevents); 18931da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) context; 18941da177e4SLinus Torvalds 18957a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 18967a8c3d3bSMike Anderson list_splice_init(&md->uevent_list, &uevents); 18977a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 18987a8c3d3bSMike Anderson 1899ed9e1982STejun Heo dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); 19007a8c3d3bSMike Anderson 19011da177e4SLinus Torvalds atomic_inc(&md->event_nr); 19021da177e4SLinus Torvalds wake_up(&md->eventq); 190362e08243SMikulas Patocka dm_issue_global_event(); 19041da177e4SLinus Torvalds } 19051da177e4SLinus Torvalds 1906c217649bSMike Snitzer /* 1907042d2a9bSAlasdair G Kergon * Returns old map, which caller must destroy. 1908042d2a9bSAlasdair G Kergon */ 1909042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t, 1910754c5fc7SMike Snitzer struct queue_limits *limits) 19111da177e4SLinus Torvalds { 1912042d2a9bSAlasdair G Kergon struct dm_table *old_map; 1913165125e1SJens Axboe struct request_queue *q = md->queue; 1914978e51baSMike Snitzer bool request_based = dm_table_request_based(t); 19151da177e4SLinus Torvalds sector_t size; 19162a2a4c51SJens Axboe int ret; 19171da177e4SLinus Torvalds 19185a8f1f80SBart Van Assche lockdep_assert_held(&md->suspend_lock); 19195a8f1f80SBart Van Assche 19201da177e4SLinus Torvalds size = dm_table_get_size(t); 19213ac51e74SDarrick J. Wong 19223ac51e74SDarrick J. Wong /* 19233ac51e74SDarrick J. Wong * Wipe any geometry if the size of the table changed. 19243ac51e74SDarrick J. Wong */ 1925fd2ed4d2SMikulas Patocka if (size != dm_get_size(md)) 19263ac51e74SDarrick J. Wong memset(&md->geometry, 0, sizeof(md->geometry)); 19273ac51e74SDarrick J. Wong 19285424a0b8SMikulas Patocka if (!get_capacity(md->disk)) 19295424a0b8SMikulas Patocka set_capacity(md->disk, size); 19305424a0b8SMikulas Patocka else 1931f64d9b2eSChristoph Hellwig set_capacity_and_notify(md->disk, size); 19321da177e4SLinus Torvalds 1933cf222b37SAlasdair G Kergon dm_table_event_callback(t, event_callback, md); 19342ca3310eSAlasdair G Kergon 1935e6ee8c0bSKiyoshi Ueda /* 1936e6ee8c0bSKiyoshi Ueda * The queue hasn't been stopped yet, if the old table type wasn't 1937e6ee8c0bSKiyoshi Ueda * for request-based during suspension. So stop it to prevent 1938e6ee8c0bSKiyoshi Ueda * I/O mapping before resume. 1939e6ee8c0bSKiyoshi Ueda * This must be done before setting the queue restrictions, 1940e6ee8c0bSKiyoshi Ueda * because request-based dm may be run just after the setting. 1941e6ee8c0bSKiyoshi Ueda */ 1942978e51baSMike Snitzer if (request_based) 1943eca7ee6dSMike Snitzer dm_stop_queue(q); 1944978e51baSMike Snitzer 19459c37de29SMike Snitzer if (request_based) { 194616f12266SMike Snitzer /* 19479c37de29SMike Snitzer * Leverage the fact that request-based DM targets are 19489c37de29SMike Snitzer * immutable singletons - used to optimize dm_mq_queue_rq. 194916f12266SMike Snitzer */ 195016f12266SMike Snitzer md->immutable_target = dm_table_get_immutable_target(t); 195116f12266SMike Snitzer } 1952e6ee8c0bSKiyoshi Ueda 19532a2a4c51SJens Axboe ret = __bind_mempools(md, t); 19542a2a4c51SJens Axboe if (ret) { 19552a2a4c51SJens Axboe old_map = ERR_PTR(ret); 19562a2a4c51SJens Axboe goto out; 19572a2a4c51SJens Axboe } 1958e6ee8c0bSKiyoshi Ueda 1959bb37d772SDamien Le Moal ret = dm_table_set_restrictions(t, q, limits); 1960bb37d772SDamien Le Moal if (ret) { 1961bb37d772SDamien Le Moal old_map = ERR_PTR(ret); 1962bb37d772SDamien Le Moal goto out; 1963bb37d772SDamien Le Moal } 1964bb37d772SDamien Le Moal 1965a12f5d48SEric Dumazet old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 19661d3aa6f6SMike Snitzer rcu_assign_pointer(md->map, (void *)t); 196736a0456fSAlasdair G Kergon md->immutable_target_type = dm_table_get_immutable_target_type(t); 196836a0456fSAlasdair G Kergon 196941abc4e1SHannes Reinecke if (old_map) 197083d5e5b0SMikulas Patocka dm_sync_table(md); 19712ca3310eSAlasdair G Kergon 19722a2a4c51SJens Axboe out: 1973042d2a9bSAlasdair G Kergon return old_map; 19741da177e4SLinus Torvalds } 19751da177e4SLinus Torvalds 1976a7940155SAlasdair G Kergon /* 1977a7940155SAlasdair G Kergon * Returns unbound table for the caller to free. 1978a7940155SAlasdair G Kergon */ 1979a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md) 19801da177e4SLinus Torvalds { 1981a12f5d48SEric Dumazet struct dm_table *map = rcu_dereference_protected(md->map, 1); 19821da177e4SLinus Torvalds 19831da177e4SLinus Torvalds if (!map) 1984a7940155SAlasdair G Kergon return NULL; 19851da177e4SLinus Torvalds 19861da177e4SLinus Torvalds dm_table_event_callback(map, NULL, NULL); 19879cdb8520SMonam Agarwal RCU_INIT_POINTER(md->map, NULL); 198883d5e5b0SMikulas Patocka dm_sync_table(md); 1989a7940155SAlasdair G Kergon 1990a7940155SAlasdair G Kergon return map; 19911da177e4SLinus Torvalds } 19921da177e4SLinus Torvalds 19931da177e4SLinus Torvalds /* 19941da177e4SLinus Torvalds * Constructor for a new device. 19951da177e4SLinus Torvalds */ 19962b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result) 19971da177e4SLinus Torvalds { 19981da177e4SLinus Torvalds struct mapped_device *md; 19991da177e4SLinus Torvalds 20002b06cfffSAlasdair G Kergon md = alloc_dev(minor); 20011da177e4SLinus Torvalds if (!md) 20021da177e4SLinus Torvalds return -ENXIO; 20031da177e4SLinus Torvalds 200491ccbbacSTushar Sugandhi dm_ima_reset_data(md); 200591ccbbacSTushar Sugandhi 20061da177e4SLinus Torvalds *result = md; 20071da177e4SLinus Torvalds return 0; 20081da177e4SLinus Torvalds } 20091da177e4SLinus Torvalds 2010a5664dadSMike Snitzer /* 2011a5664dadSMike Snitzer * Functions to manage md->type. 2012a5664dadSMike Snitzer * All are required to hold md->type_lock. 2013a5664dadSMike Snitzer */ 2014a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md) 2015a5664dadSMike Snitzer { 2016a5664dadSMike Snitzer mutex_lock(&md->type_lock); 2017a5664dadSMike Snitzer } 2018a5664dadSMike Snitzer 2019a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md) 2020a5664dadSMike Snitzer { 2021a5664dadSMike Snitzer mutex_unlock(&md->type_lock); 2022a5664dadSMike Snitzer } 2023a5664dadSMike Snitzer 20247e0d574fSBart Van Assche void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type) 2025a5664dadSMike Snitzer { 202600c4fc3bSMike Snitzer BUG_ON(!mutex_is_locked(&md->type_lock)); 2027a5664dadSMike Snitzer md->type = type; 2028a5664dadSMike Snitzer } 2029a5664dadSMike Snitzer 20307e0d574fSBart Van Assche enum dm_queue_mode dm_get_md_type(struct mapped_device *md) 2031a5664dadSMike Snitzer { 2032a5664dadSMike Snitzer return md->type; 2033a5664dadSMike Snitzer } 2034a5664dadSMike Snitzer 203536a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md) 203636a0456fSAlasdair G Kergon { 203736a0456fSAlasdair G Kergon return md->immutable_target_type; 203836a0456fSAlasdair G Kergon } 203936a0456fSAlasdair G Kergon 20404a0b4ddfSMike Snitzer /* 2041f84cb8a4SMike Snitzer * The queue_limits are only valid as long as you have a reference 2042f84cb8a4SMike Snitzer * count on 'md'. 2043f84cb8a4SMike Snitzer */ 2044f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md) 2045f84cb8a4SMike Snitzer { 2046f84cb8a4SMike Snitzer BUG_ON(!atomic_read(&md->holders)); 2047f84cb8a4SMike Snitzer return &md->queue->limits; 2048f84cb8a4SMike Snitzer } 2049f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits); 2050f84cb8a4SMike Snitzer 20514a0b4ddfSMike Snitzer /* 20524a0b4ddfSMike Snitzer * Setup the DM device's queue based on md's type 20534a0b4ddfSMike Snitzer */ 2054591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t) 20554a0b4ddfSMike Snitzer { 2056ba305859SChristoph Hellwig enum dm_queue_mode type = dm_table_get_type(t); 2057c100ec49SMike Snitzer struct queue_limits limits; 2058ba305859SChristoph Hellwig int r; 2059bfebd1cdSMike Snitzer 2060545ed20eSToshi Kani switch (type) { 2061bfebd1cdSMike Snitzer case DM_TYPE_REQUEST_BASED: 2062681cc5e8SMike Snitzer md->disk->fops = &dm_rq_blk_dops; 2063e83068a5SMike Snitzer r = dm_mq_init_request_queue(md, t); 2064bfebd1cdSMike Snitzer if (r) { 2065681cc5e8SMike Snitzer DMERR("Cannot initialize queue for request-based dm mapped device"); 2066bfebd1cdSMike Snitzer return r; 2067bfebd1cdSMike Snitzer } 2068bfebd1cdSMike Snitzer break; 2069bfebd1cdSMike Snitzer case DM_TYPE_BIO_BASED: 2070545ed20eSToshi Kani case DM_TYPE_DAX_BIO_BASED: 2071bfebd1cdSMike Snitzer break; 20727e0d574fSBart Van Assche case DM_TYPE_NONE: 20737e0d574fSBart Van Assche WARN_ON_ONCE(true); 20747e0d574fSBart Van Assche break; 2075ff36ab34SMike Snitzer } 20764a0b4ddfSMike Snitzer 2077c100ec49SMike Snitzer r = dm_calculate_queue_limits(t, &limits); 2078c100ec49SMike Snitzer if (r) { 2079c100ec49SMike Snitzer DMERR("Cannot calculate initial queue limits"); 2080c100ec49SMike Snitzer return r; 2081c100ec49SMike Snitzer } 2082bb37d772SDamien Le Moal r = dm_table_set_restrictions(t, md->queue, &limits); 2083bb37d772SDamien Le Moal if (r) 2084bb37d772SDamien Le Moal return r; 208589f871afSChristoph Hellwig 208689f871afSChristoph Hellwig add_disk(md->disk); 208789f871afSChristoph Hellwig 208889f871afSChristoph Hellwig r = dm_sysfs_init(md); 208989f871afSChristoph Hellwig if (r) { 209089f871afSChristoph Hellwig del_gendisk(md->disk); 209189f871afSChristoph Hellwig return r; 209289f871afSChristoph Hellwig } 2093ba305859SChristoph Hellwig md->type = type; 20944a0b4ddfSMike Snitzer return 0; 20954a0b4ddfSMike Snitzer } 20964a0b4ddfSMike Snitzer 20972bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev) 20981da177e4SLinus Torvalds { 20991da177e4SLinus Torvalds struct mapped_device *md; 21001da177e4SLinus Torvalds unsigned minor = MINOR(dev); 21011da177e4SLinus Torvalds 21021da177e4SLinus Torvalds if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 21031da177e4SLinus Torvalds return NULL; 21041da177e4SLinus Torvalds 2105f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 21061da177e4SLinus Torvalds 21071da177e4SLinus Torvalds md = idr_find(&_minor_idr, minor); 210849de5769SMike Snitzer if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) || 210949de5769SMike Snitzer test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { 2110637842cfSDavid Teigland md = NULL; 2111fba9f90eSJeff Mahoney goto out; 2112fba9f90eSJeff Mahoney } 21132bec1f4aSMikulas Patocka dm_get(md); 2114fba9f90eSJeff Mahoney out: 2115f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 21161da177e4SLinus Torvalds 2117637842cfSDavid Teigland return md; 2118637842cfSDavid Teigland } 21193cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md); 2120d229a958SDavid Teigland 21219ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md) 2122637842cfSDavid Teigland { 21239ade92a9SAlasdair G Kergon return md->interface_ptr; 21241da177e4SLinus Torvalds } 21251da177e4SLinus Torvalds 21261da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr) 21271da177e4SLinus Torvalds { 21281da177e4SLinus Torvalds md->interface_ptr = ptr; 21291da177e4SLinus Torvalds } 21301da177e4SLinus Torvalds 21311da177e4SLinus Torvalds void dm_get(struct mapped_device *md) 21321da177e4SLinus Torvalds { 21331da177e4SLinus Torvalds atomic_inc(&md->holders); 21343f77316dSKiyoshi Ueda BUG_ON(test_bit(DMF_FREEING, &md->flags)); 21351da177e4SLinus Torvalds } 21361da177e4SLinus Torvalds 213709ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md) 213809ee96b2SMikulas Patocka { 213909ee96b2SMikulas Patocka spin_lock(&_minor_lock); 214009ee96b2SMikulas Patocka if (test_bit(DMF_FREEING, &md->flags)) { 214109ee96b2SMikulas Patocka spin_unlock(&_minor_lock); 214209ee96b2SMikulas Patocka return -EBUSY; 214309ee96b2SMikulas Patocka } 214409ee96b2SMikulas Patocka dm_get(md); 214509ee96b2SMikulas Patocka spin_unlock(&_minor_lock); 214609ee96b2SMikulas Patocka return 0; 214709ee96b2SMikulas Patocka } 214809ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold); 214909ee96b2SMikulas Patocka 215072d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md) 215172d94861SAlasdair G Kergon { 215272d94861SAlasdair G Kergon return md->name; 215372d94861SAlasdair G Kergon } 215472d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name); 215572d94861SAlasdair G Kergon 21563f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait) 21571da177e4SLinus Torvalds { 21581134e5aeSMike Anderson struct dm_table *map; 215983d5e5b0SMikulas Patocka int srcu_idx; 21601da177e4SLinus Torvalds 21613f77316dSKiyoshi Ueda might_sleep(); 2162fba9f90eSJeff Mahoney 216363a4f065SMike Snitzer spin_lock(&_minor_lock); 21643f77316dSKiyoshi Ueda idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md)))); 2165fba9f90eSJeff Mahoney set_bit(DMF_FREEING, &md->flags); 2166f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 21673f77316dSKiyoshi Ueda 2168c12c9a3cSMike Snitzer blk_set_queue_dying(md->queue); 21693b785fbcSBart Van Assche 2170ab7c7bb6SMikulas Patocka /* 2171ab7c7bb6SMikulas Patocka * Take suspend_lock so that presuspend and postsuspend methods 2172ab7c7bb6SMikulas Patocka * do not race with internal suspend. 2173ab7c7bb6SMikulas Patocka */ 2174ab7c7bb6SMikulas Patocka mutex_lock(&md->suspend_lock); 21752a708cffSJunichi Nomura map = dm_get_live_table(md, &srcu_idx); 21764f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) { 21771da177e4SLinus Torvalds dm_table_presuspend_targets(map); 2178adc0daadSMikulas Patocka set_bit(DMF_SUSPENDED, &md->flags); 21795df96f2bSMikulas Patocka set_bit(DMF_POST_SUSPENDING, &md->flags); 21801da177e4SLinus Torvalds dm_table_postsuspend_targets(map); 21811da177e4SLinus Torvalds } 218283d5e5b0SMikulas Patocka /* dm_put_live_table must be before msleep, otherwise deadlock is possible */ 218383d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 21842a708cffSJunichi Nomura mutex_unlock(&md->suspend_lock); 218583d5e5b0SMikulas Patocka 21863f77316dSKiyoshi Ueda /* 21873f77316dSKiyoshi Ueda * Rare, but there may be I/O requests still going to complete, 21883f77316dSKiyoshi Ueda * for example. Wait for all references to disappear. 21893f77316dSKiyoshi Ueda * No one should increment the reference count of the mapped_device, 21903f77316dSKiyoshi Ueda * after the mapped_device state becomes DMF_FREEING. 21913f77316dSKiyoshi Ueda */ 21923f77316dSKiyoshi Ueda if (wait) 21933f77316dSKiyoshi Ueda while (atomic_read(&md->holders)) 21943f77316dSKiyoshi Ueda msleep(1); 21953f77316dSKiyoshi Ueda else if (atomic_read(&md->holders)) 21963f77316dSKiyoshi Ueda DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)", 21973f77316dSKiyoshi Ueda dm_device_name(md), atomic_read(&md->holders)); 21983f77316dSKiyoshi Ueda 2199a7940155SAlasdair G Kergon dm_table_destroy(__unbind(md)); 22001da177e4SLinus Torvalds free_dev(md); 22011da177e4SLinus Torvalds } 22023f77316dSKiyoshi Ueda 22033f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md) 22043f77316dSKiyoshi Ueda { 22053f77316dSKiyoshi Ueda __dm_destroy(md, true); 22063f77316dSKiyoshi Ueda } 22073f77316dSKiyoshi Ueda 22083f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md) 22093f77316dSKiyoshi Ueda { 22103f77316dSKiyoshi Ueda __dm_destroy(md, false); 22113f77316dSKiyoshi Ueda } 22123f77316dSKiyoshi Ueda 22133f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md) 22143f77316dSKiyoshi Ueda { 22153f77316dSKiyoshi Ueda atomic_dec(&md->holders); 22161da177e4SLinus Torvalds } 221779eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put); 22181da177e4SLinus Torvalds 221985067747SMing Lei static bool md_in_flight_bios(struct mapped_device *md) 222085067747SMing Lei { 222185067747SMing Lei int cpu; 22228446fe92SChristoph Hellwig struct block_device *part = dm_disk(md)->part0; 222385067747SMing Lei long sum = 0; 222485067747SMing Lei 222585067747SMing Lei for_each_possible_cpu(cpu) { 222685067747SMing Lei sum += part_stat_local_read_cpu(part, in_flight[0], cpu); 222785067747SMing Lei sum += part_stat_local_read_cpu(part, in_flight[1], cpu); 222885067747SMing Lei } 222985067747SMing Lei 223085067747SMing Lei return sum != 0; 223185067747SMing Lei } 223285067747SMing Lei 22332f064a59SPeter Zijlstra static int dm_wait_for_bios_completion(struct mapped_device *md, unsigned int task_state) 223446125c1cSMilan Broz { 223546125c1cSMilan Broz int r = 0; 22369f4c3f87SBart Van Assche DEFINE_WAIT(wait); 223746125c1cSMilan Broz 223885067747SMing Lei while (true) { 22399f4c3f87SBart Van Assche prepare_to_wait(&md->wait, &wait, task_state); 224046125c1cSMilan Broz 224185067747SMing Lei if (!md_in_flight_bios(md)) 224246125c1cSMilan Broz break; 224346125c1cSMilan Broz 2244e3fabdfdSBart Van Assche if (signal_pending_state(task_state, current)) { 224546125c1cSMilan Broz r = -EINTR; 224646125c1cSMilan Broz break; 224746125c1cSMilan Broz } 224846125c1cSMilan Broz 224946125c1cSMilan Broz io_schedule(); 225046125c1cSMilan Broz } 22519f4c3f87SBart Van Assche finish_wait(&md->wait, &wait); 2252b44ebeb0SMikulas Patocka 225346125c1cSMilan Broz return r; 225446125c1cSMilan Broz } 225546125c1cSMilan Broz 22562f064a59SPeter Zijlstra static int dm_wait_for_completion(struct mapped_device *md, unsigned int task_state) 225785067747SMing Lei { 225885067747SMing Lei int r = 0; 225985067747SMing Lei 226085067747SMing Lei if (!queue_is_mq(md->queue)) 226185067747SMing Lei return dm_wait_for_bios_completion(md, task_state); 226285067747SMing Lei 226385067747SMing Lei while (true) { 226485067747SMing Lei if (!blk_mq_queue_inflight(md->queue)) 226585067747SMing Lei break; 226685067747SMing Lei 226785067747SMing Lei if (signal_pending_state(task_state, current)) { 226885067747SMing Lei r = -EINTR; 226985067747SMing Lei break; 227085067747SMing Lei } 227185067747SMing Lei 227285067747SMing Lei msleep(5); 227385067747SMing Lei } 227485067747SMing Lei 227585067747SMing Lei return r; 227685067747SMing Lei } 227785067747SMing Lei 22781da177e4SLinus Torvalds /* 22791da177e4SLinus Torvalds * Process the deferred bios 22801da177e4SLinus Torvalds */ 2281ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work) 22821da177e4SLinus Torvalds { 22830c2915b8SMike Snitzer struct mapped_device *md = container_of(work, struct mapped_device, work); 22840c2915b8SMike Snitzer struct bio *bio; 2285ef208587SMikulas Patocka 22863b00b203SMikulas Patocka while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 2287022c2611SMikulas Patocka spin_lock_irq(&md->deferred_lock); 22880c2915b8SMike Snitzer bio = bio_list_pop(&md->deferred); 2289022c2611SMikulas Patocka spin_unlock_irq(&md->deferred_lock); 2290022c2611SMikulas Patocka 22910c2915b8SMike Snitzer if (!bio) 2292df12ee99SAlasdair G Kergon break; 229373d410c0SMilan Broz 22940c2915b8SMike Snitzer submit_bio_noacct(bio); 2295e6ee8c0bSKiyoshi Ueda } 22961da177e4SLinus Torvalds } 22971da177e4SLinus Torvalds 22989a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md) 2299304f3f6aSMilan Broz { 23003b00b203SMikulas Patocka clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 23014e857c58SPeter Zijlstra smp_mb__after_atomic(); 230253d5914fSMikulas Patocka queue_work(md->wq, &md->work); 2303304f3f6aSMilan Broz } 2304304f3f6aSMilan Broz 23051da177e4SLinus Torvalds /* 2306042d2a9bSAlasdair G Kergon * Swap in a new table, returning the old one for the caller to destroy. 23071da177e4SLinus Torvalds */ 2308042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table) 23091da177e4SLinus Torvalds { 231087eb5b21SMike Christie struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL); 2311754c5fc7SMike Snitzer struct queue_limits limits; 2312042d2a9bSAlasdair G Kergon int r; 23131da177e4SLinus Torvalds 2314e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 23151da177e4SLinus Torvalds 23161da177e4SLinus Torvalds /* device must be suspended */ 23174f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) 231893c534aeSAlasdair G Kergon goto out; 23191da177e4SLinus Torvalds 23203ae70656SMike Snitzer /* 23213ae70656SMike Snitzer * If the new table has no data devices, retain the existing limits. 23223ae70656SMike Snitzer * This helps multipath with queue_if_no_path if all paths disappear, 23233ae70656SMike Snitzer * then new I/O is queued based on these limits, and then some paths 23243ae70656SMike Snitzer * reappear. 23253ae70656SMike Snitzer */ 23263ae70656SMike Snitzer if (dm_table_has_no_data_devices(table)) { 232783d5e5b0SMikulas Patocka live_map = dm_get_live_table_fast(md); 23283ae70656SMike Snitzer if (live_map) 23293ae70656SMike Snitzer limits = md->queue->limits; 233083d5e5b0SMikulas Patocka dm_put_live_table_fast(md); 23313ae70656SMike Snitzer } 23323ae70656SMike Snitzer 233387eb5b21SMike Christie if (!live_map) { 2334754c5fc7SMike Snitzer r = dm_calculate_queue_limits(table, &limits); 2335042d2a9bSAlasdair G Kergon if (r) { 2336042d2a9bSAlasdair G Kergon map = ERR_PTR(r); 2337754c5fc7SMike Snitzer goto out; 2338042d2a9bSAlasdair G Kergon } 233987eb5b21SMike Christie } 2340754c5fc7SMike Snitzer 2341042d2a9bSAlasdair G Kergon map = __bind(md, table, &limits); 234262e08243SMikulas Patocka dm_issue_global_event(); 23431da177e4SLinus Torvalds 234493c534aeSAlasdair G Kergon out: 2345e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 2346042d2a9bSAlasdair G Kergon return map; 23471da177e4SLinus Torvalds } 23481da177e4SLinus Torvalds 23491da177e4SLinus Torvalds /* 23501da177e4SLinus Torvalds * Functions to lock and unlock any filesystem running on the 23511da177e4SLinus Torvalds * device. 23521da177e4SLinus Torvalds */ 23532ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md) 23541da177e4SLinus Torvalds { 2355e39e2e95SAlasdair G Kergon int r; 23561da177e4SLinus Torvalds 2357040f04bdSChristoph Hellwig WARN_ON(test_bit(DMF_FROZEN, &md->flags)); 2358dfbe03f6SAlasdair G Kergon 2359977115c0SChristoph Hellwig r = freeze_bdev(md->disk->part0); 2360040f04bdSChristoph Hellwig if (!r) 2361aa8d7c2fSAlasdair G Kergon set_bit(DMF_FROZEN, &md->flags); 2362040f04bdSChristoph Hellwig return r; 23631da177e4SLinus Torvalds } 23641da177e4SLinus Torvalds 23652ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md) 23661da177e4SLinus Torvalds { 2367aa8d7c2fSAlasdair G Kergon if (!test_bit(DMF_FROZEN, &md->flags)) 2368aa8d7c2fSAlasdair G Kergon return; 2369977115c0SChristoph Hellwig thaw_bdev(md->disk->part0); 2370aa8d7c2fSAlasdair G Kergon clear_bit(DMF_FROZEN, &md->flags); 23711da177e4SLinus Torvalds } 23721da177e4SLinus Torvalds 23731da177e4SLinus Torvalds /* 2374b48633f8SBart Van Assche * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG 2375b48633f8SBart Van Assche * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE 2376b48633f8SBart Van Assche * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY 2377b48633f8SBart Van Assche * 2378ffcc3936SMike Snitzer * If __dm_suspend returns 0, the device is completely quiescent 2379ffcc3936SMike Snitzer * now. There is no request-processing activity. All new requests 2380ffcc3936SMike Snitzer * are being added to md->deferred list. 2381cec47e3dSKiyoshi Ueda */ 2382ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map, 23832f064a59SPeter Zijlstra unsigned suspend_flags, unsigned int task_state, 2384eaf9a736SMike Snitzer int dmf_suspended_flag) 23851da177e4SLinus Torvalds { 2386ffcc3936SMike Snitzer bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG; 2387ffcc3936SMike Snitzer bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG; 2388ffcc3936SMike Snitzer int r; 2389cf222b37SAlasdair G Kergon 23905a8f1f80SBart Van Assche lockdep_assert_held(&md->suspend_lock); 23915a8f1f80SBart Van Assche 23922e93ccc1SKiyoshi Ueda /* 23932e93ccc1SKiyoshi Ueda * DMF_NOFLUSH_SUSPENDING must be set before presuspend. 23942e93ccc1SKiyoshi Ueda * This flag is cleared before dm_suspend returns. 23952e93ccc1SKiyoshi Ueda */ 23962e93ccc1SKiyoshi Ueda if (noflush) 23972e93ccc1SKiyoshi Ueda set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 239886331f39SBart Van Assche else 2399ac75b09fSMike Snitzer DMDEBUG("%s: suspending with flush", dm_device_name(md)); 24002e93ccc1SKiyoshi Ueda 2401d67ee213SMike Snitzer /* 2402d67ee213SMike Snitzer * This gets reverted if there's an error later and the targets 2403d67ee213SMike Snitzer * provide the .presuspend_undo hook. 2404d67ee213SMike Snitzer */ 24051da177e4SLinus Torvalds dm_table_presuspend_targets(map); 24061da177e4SLinus Torvalds 24072e93ccc1SKiyoshi Ueda /* 24089f518b27SKiyoshi Ueda * Flush I/O to the device. 24099f518b27SKiyoshi Ueda * Any I/O submitted after lock_fs() may not be flushed. 24109f518b27SKiyoshi Ueda * noflush takes precedence over do_lockfs. 24119f518b27SKiyoshi Ueda * (lock_fs() flushes I/Os and waits for them to complete.) 24122e93ccc1SKiyoshi Ueda */ 241332a926daSMikulas Patocka if (!noflush && do_lockfs) { 24142ca3310eSAlasdair G Kergon r = lock_fs(md); 2415d67ee213SMike Snitzer if (r) { 2416d67ee213SMike Snitzer dm_table_presuspend_undo_targets(map); 2417ffcc3936SMike Snitzer return r; 2418aa8d7c2fSAlasdair G Kergon } 2419d67ee213SMike Snitzer } 24201da177e4SLinus Torvalds 24211da177e4SLinus Torvalds /* 24223b00b203SMikulas Patocka * Here we must make sure that no processes are submitting requests 24233b00b203SMikulas Patocka * to target drivers i.e. no one may be executing 24240cede372SMike Snitzer * __split_and_process_bio from dm_submit_bio. 24253b00b203SMikulas Patocka * 24260cede372SMike Snitzer * To get all processes out of __split_and_process_bio in dm_submit_bio, 24273b00b203SMikulas Patocka * we take the write lock. To prevent any process from reentering 24280cede372SMike Snitzer * __split_and_process_bio from dm_submit_bio and quiesce the thread 24290cede372SMike Snitzer * (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND and call 24306a8736d1STejun Heo * flush_workqueue(md->wq). 24311da177e4SLinus Torvalds */ 24321eb787ecSAlasdair G Kergon set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 243341abc4e1SHannes Reinecke if (map) 243483d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 24351da177e4SLinus Torvalds 2436d0bcb878SKiyoshi Ueda /* 243729e4013dSTejun Heo * Stop md->queue before flushing md->wq in case request-based 243829e4013dSTejun Heo * dm defers requests to md->wq from md->queue. 2439d0bcb878SKiyoshi Ueda */ 24406a23e05cSJens Axboe if (dm_request_based(md)) 2441eca7ee6dSMike Snitzer dm_stop_queue(md->queue); 2442cec47e3dSKiyoshi Ueda 2443d0bcb878SKiyoshi Ueda flush_workqueue(md->wq); 2444d0bcb878SKiyoshi Ueda 24451da177e4SLinus Torvalds /* 24463b00b203SMikulas Patocka * At this point no more requests are entering target request routines. 24473b00b203SMikulas Patocka * We call dm_wait_for_completion to wait for all existing requests 24483b00b203SMikulas Patocka * to finish. 24491da177e4SLinus Torvalds */ 2450b48633f8SBart Van Assche r = dm_wait_for_completion(md, task_state); 2451eaf9a736SMike Snitzer if (!r) 2452eaf9a736SMike Snitzer set_bit(dmf_suspended_flag, &md->flags); 24531da177e4SLinus Torvalds 24546d6f10dfSMilan Broz if (noflush) 2455022c2611SMikulas Patocka clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 245641abc4e1SHannes Reinecke if (map) 245783d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 24582e93ccc1SKiyoshi Ueda 24591da177e4SLinus Torvalds /* were we interrupted ? */ 246046125c1cSMilan Broz if (r < 0) { 24619a1fb464SMikulas Patocka dm_queue_flush(md); 246273d410c0SMilan Broz 2463cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 2464eca7ee6dSMike Snitzer dm_start_queue(md->queue); 2465cec47e3dSKiyoshi Ueda 24662ca3310eSAlasdair G Kergon unlock_fs(md); 2467d67ee213SMike Snitzer dm_table_presuspend_undo_targets(map); 2468ffcc3936SMike Snitzer /* pushback list is already flushed, so skip flush */ 2469ffcc3936SMike Snitzer } 2470ffcc3936SMike Snitzer 2471ffcc3936SMike Snitzer return r; 24722ca3310eSAlasdair G Kergon } 24732ca3310eSAlasdair G Kergon 24743b00b203SMikulas Patocka /* 2475ffcc3936SMike Snitzer * We need to be able to change a mapping table under a mounted 2476ffcc3936SMike Snitzer * filesystem. For example we might want to move some data in 2477ffcc3936SMike Snitzer * the background. Before the table can be swapped with 2478ffcc3936SMike Snitzer * dm_bind_table, dm_suspend must be called to flush any in 2479ffcc3936SMike Snitzer * flight bios and ensure that any further io gets deferred. 24803b00b203SMikulas Patocka */ 2481ffcc3936SMike Snitzer /* 2482ffcc3936SMike Snitzer * Suspend mechanism in request-based dm. 2483ffcc3936SMike Snitzer * 2484ffcc3936SMike Snitzer * 1. Flush all I/Os by lock_fs() if needed. 2485ffcc3936SMike Snitzer * 2. Stop dispatching any I/O by stopping the request_queue. 2486ffcc3936SMike Snitzer * 3. Wait for all in-flight I/Os to be completed or requeued. 2487ffcc3936SMike Snitzer * 2488ffcc3936SMike Snitzer * To abort suspend, start the request_queue. 2489ffcc3936SMike Snitzer */ 2490ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags) 2491ffcc3936SMike Snitzer { 2492ffcc3936SMike Snitzer struct dm_table *map = NULL; 2493ffcc3936SMike Snitzer int r = 0; 2494ffcc3936SMike Snitzer 2495ffcc3936SMike Snitzer retry: 2496ffcc3936SMike Snitzer mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); 2497ffcc3936SMike Snitzer 2498ffcc3936SMike Snitzer if (dm_suspended_md(md)) { 2499ffcc3936SMike Snitzer r = -EINVAL; 2500ffcc3936SMike Snitzer goto out_unlock; 2501ffcc3936SMike Snitzer } 2502ffcc3936SMike Snitzer 2503ffcc3936SMike Snitzer if (dm_suspended_internally_md(md)) { 2504ffcc3936SMike Snitzer /* already internally suspended, wait for internal resume */ 2505ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2506ffcc3936SMike Snitzer r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); 2507ffcc3936SMike Snitzer if (r) 2508ffcc3936SMike Snitzer return r; 2509ffcc3936SMike Snitzer goto retry; 2510ffcc3936SMike Snitzer } 2511ffcc3936SMike Snitzer 2512a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2513ffcc3936SMike Snitzer 2514eaf9a736SMike Snitzer r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED); 2515ffcc3936SMike Snitzer if (r) 2516ffcc3936SMike Snitzer goto out_unlock; 25173b00b203SMikulas Patocka 25185df96f2bSMikulas Patocka set_bit(DMF_POST_SUSPENDING, &md->flags); 25194d4471cbSKiyoshi Ueda dm_table_postsuspend_targets(map); 25205df96f2bSMikulas Patocka clear_bit(DMF_POST_SUSPENDING, &md->flags); 25214d4471cbSKiyoshi Ueda 2522d287483dSAlasdair G Kergon out_unlock: 2523e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 2524cf222b37SAlasdair G Kergon return r; 25251da177e4SLinus Torvalds } 25261da177e4SLinus Torvalds 2527ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map) 25281da177e4SLinus Torvalds { 2529ffcc3936SMike Snitzer if (map) { 2530ffcc3936SMike Snitzer int r = dm_table_resume_targets(map); 25318757b776SMilan Broz if (r) 2532ffcc3936SMike Snitzer return r; 2533ffcc3936SMike Snitzer } 25342ca3310eSAlasdair G Kergon 25359a1fb464SMikulas Patocka dm_queue_flush(md); 25362ca3310eSAlasdair G Kergon 2537cec47e3dSKiyoshi Ueda /* 2538cec47e3dSKiyoshi Ueda * Flushing deferred I/Os must be done after targets are resumed 2539cec47e3dSKiyoshi Ueda * so that mapping of targets can work correctly. 2540cec47e3dSKiyoshi Ueda * Request-based dm is queueing the deferred I/Os in its request_queue. 2541cec47e3dSKiyoshi Ueda */ 2542cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 2543eca7ee6dSMike Snitzer dm_start_queue(md->queue); 2544cec47e3dSKiyoshi Ueda 25452ca3310eSAlasdair G Kergon unlock_fs(md); 25462ca3310eSAlasdair G Kergon 2547ffcc3936SMike Snitzer return 0; 2548ffcc3936SMike Snitzer } 2549ffcc3936SMike Snitzer 2550ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md) 2551ffcc3936SMike Snitzer { 25528dc23658SMinfei Huang int r; 2553ffcc3936SMike Snitzer struct dm_table *map = NULL; 2554ffcc3936SMike Snitzer 2555ffcc3936SMike Snitzer retry: 25568dc23658SMinfei Huang r = -EINVAL; 2557ffcc3936SMike Snitzer mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); 2558ffcc3936SMike Snitzer 2559ffcc3936SMike Snitzer if (!dm_suspended_md(md)) 2560ffcc3936SMike Snitzer goto out; 2561ffcc3936SMike Snitzer 2562ffcc3936SMike Snitzer if (dm_suspended_internally_md(md)) { 2563ffcc3936SMike Snitzer /* already internally suspended, wait for internal resume */ 2564ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2565ffcc3936SMike Snitzer r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); 2566ffcc3936SMike Snitzer if (r) 2567ffcc3936SMike Snitzer return r; 2568ffcc3936SMike Snitzer goto retry; 2569ffcc3936SMike Snitzer } 2570ffcc3936SMike Snitzer 2571a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2572ffcc3936SMike Snitzer if (!map || !dm_table_get_size(map)) 2573ffcc3936SMike Snitzer goto out; 2574ffcc3936SMike Snitzer 2575ffcc3936SMike Snitzer r = __dm_resume(md, map); 2576ffcc3936SMike Snitzer if (r) 2577ffcc3936SMike Snitzer goto out; 2578ffcc3936SMike Snitzer 25792ca3310eSAlasdair G Kergon clear_bit(DMF_SUSPENDED, &md->flags); 2580cf222b37SAlasdair G Kergon out: 2581e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 25822ca3310eSAlasdair G Kergon 2583cf222b37SAlasdair G Kergon return r; 25841da177e4SLinus Torvalds } 25851da177e4SLinus Torvalds 2586fd2ed4d2SMikulas Patocka /* 2587fd2ed4d2SMikulas Patocka * Internal suspend/resume works like userspace-driven suspend. It waits 2588fd2ed4d2SMikulas Patocka * until all bios finish and prevents issuing new bios to the target drivers. 2589fd2ed4d2SMikulas Patocka * It may be used only from the kernel. 2590fd2ed4d2SMikulas Patocka */ 2591fd2ed4d2SMikulas Patocka 2592ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags) 2593ffcc3936SMike Snitzer { 2594ffcc3936SMike Snitzer struct dm_table *map = NULL; 2595ffcc3936SMike Snitzer 25961ea0654eSBart Van Assche lockdep_assert_held(&md->suspend_lock); 25971ea0654eSBart Van Assche 259896b26c8cSMikulas Patocka if (md->internal_suspend_count++) 2599ffcc3936SMike Snitzer return; /* nested internal suspend */ 2600ffcc3936SMike Snitzer 2601ffcc3936SMike Snitzer if (dm_suspended_md(md)) { 2602ffcc3936SMike Snitzer set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2603ffcc3936SMike Snitzer return; /* nest suspend */ 2604ffcc3936SMike Snitzer } 2605ffcc3936SMike Snitzer 2606a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2607ffcc3936SMike Snitzer 2608ffcc3936SMike Snitzer /* 2609ffcc3936SMike Snitzer * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is 2610ffcc3936SMike Snitzer * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend 2611ffcc3936SMike Snitzer * would require changing .presuspend to return an error -- avoid this 2612ffcc3936SMike Snitzer * until there is a need for more elaborate variants of internal suspend. 2613ffcc3936SMike Snitzer */ 2614eaf9a736SMike Snitzer (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE, 2615eaf9a736SMike Snitzer DMF_SUSPENDED_INTERNALLY); 2616ffcc3936SMike Snitzer 26175df96f2bSMikulas Patocka set_bit(DMF_POST_SUSPENDING, &md->flags); 2618ffcc3936SMike Snitzer dm_table_postsuspend_targets(map); 26195df96f2bSMikulas Patocka clear_bit(DMF_POST_SUSPENDING, &md->flags); 2620ffcc3936SMike Snitzer } 2621ffcc3936SMike Snitzer 2622ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md) 2623ffcc3936SMike Snitzer { 262496b26c8cSMikulas Patocka BUG_ON(!md->internal_suspend_count); 262596b26c8cSMikulas Patocka 262696b26c8cSMikulas Patocka if (--md->internal_suspend_count) 2627ffcc3936SMike Snitzer return; /* resume from nested internal suspend */ 2628ffcc3936SMike Snitzer 2629ffcc3936SMike Snitzer if (dm_suspended_md(md)) 2630ffcc3936SMike Snitzer goto done; /* resume from nested suspend */ 2631ffcc3936SMike Snitzer 2632ffcc3936SMike Snitzer /* 2633ffcc3936SMike Snitzer * NOTE: existing callers don't need to call dm_table_resume_targets 2634ffcc3936SMike Snitzer * (which may fail -- so best to avoid it for now by passing NULL map) 2635ffcc3936SMike Snitzer */ 2636ffcc3936SMike Snitzer (void) __dm_resume(md, NULL); 2637ffcc3936SMike Snitzer 2638ffcc3936SMike Snitzer done: 2639ffcc3936SMike Snitzer clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2640ffcc3936SMike Snitzer smp_mb__after_atomic(); 2641ffcc3936SMike Snitzer wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY); 2642ffcc3936SMike Snitzer } 2643ffcc3936SMike Snitzer 2644ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md) 2645fd2ed4d2SMikulas Patocka { 2646fd2ed4d2SMikulas Patocka mutex_lock(&md->suspend_lock); 2647ffcc3936SMike Snitzer __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG); 2648ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2649ffcc3936SMike Snitzer } 2650ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush); 2651ffcc3936SMike Snitzer 2652ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md) 2653ffcc3936SMike Snitzer { 2654ffcc3936SMike Snitzer mutex_lock(&md->suspend_lock); 2655ffcc3936SMike Snitzer __dm_internal_resume(md); 2656ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2657ffcc3936SMike Snitzer } 2658ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume); 2659ffcc3936SMike Snitzer 2660ffcc3936SMike Snitzer /* 2661ffcc3936SMike Snitzer * Fast variants of internal suspend/resume hold md->suspend_lock, 2662ffcc3936SMike Snitzer * which prevents interaction with userspace-driven suspend. 2663ffcc3936SMike Snitzer */ 2664ffcc3936SMike Snitzer 2665ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md) 2666ffcc3936SMike Snitzer { 2667ffcc3936SMike Snitzer mutex_lock(&md->suspend_lock); 2668ffcc3936SMike Snitzer if (dm_suspended_md(md) || dm_suspended_internally_md(md)) 2669fd2ed4d2SMikulas Patocka return; 2670fd2ed4d2SMikulas Patocka 2671fd2ed4d2SMikulas Patocka set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 2672fd2ed4d2SMikulas Patocka synchronize_srcu(&md->io_barrier); 2673fd2ed4d2SMikulas Patocka flush_workqueue(md->wq); 2674fd2ed4d2SMikulas Patocka dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); 2675fd2ed4d2SMikulas Patocka } 2676b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast); 2677fd2ed4d2SMikulas Patocka 2678ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md) 2679fd2ed4d2SMikulas Patocka { 2680ffcc3936SMike Snitzer if (dm_suspended_md(md) || dm_suspended_internally_md(md)) 2681fd2ed4d2SMikulas Patocka goto done; 2682fd2ed4d2SMikulas Patocka 2683fd2ed4d2SMikulas Patocka dm_queue_flush(md); 2684fd2ed4d2SMikulas Patocka 2685fd2ed4d2SMikulas Patocka done: 2686fd2ed4d2SMikulas Patocka mutex_unlock(&md->suspend_lock); 2687fd2ed4d2SMikulas Patocka } 2688b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast); 2689fd2ed4d2SMikulas Patocka 26901da177e4SLinus Torvalds /*----------------------------------------------------------------- 26911da177e4SLinus Torvalds * Event notification. 26921da177e4SLinus Torvalds *---------------------------------------------------------------*/ 26933abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action, 269460935eb2SMilan Broz unsigned cookie) 269569267a30SAlasdair G Kergon { 26966958c1c6SMikulas Patocka int r; 26976958c1c6SMikulas Patocka unsigned noio_flag; 269860935eb2SMilan Broz char udev_cookie[DM_COOKIE_LENGTH]; 269960935eb2SMilan Broz char *envp[] = { udev_cookie, NULL }; 270060935eb2SMilan Broz 27016958c1c6SMikulas Patocka noio_flag = memalloc_noio_save(); 27026958c1c6SMikulas Patocka 270360935eb2SMilan Broz if (!cookie) 27046958c1c6SMikulas Patocka r = kobject_uevent(&disk_to_dev(md->disk)->kobj, action); 270560935eb2SMilan Broz else { 270660935eb2SMilan Broz snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u", 270760935eb2SMilan Broz DM_COOKIE_ENV_VAR_NAME, cookie); 27086958c1c6SMikulas Patocka r = kobject_uevent_env(&disk_to_dev(md->disk)->kobj, 27093abf85b5SPeter Rajnoha action, envp); 271060935eb2SMilan Broz } 27116958c1c6SMikulas Patocka 27126958c1c6SMikulas Patocka memalloc_noio_restore(noio_flag); 27136958c1c6SMikulas Patocka 27146958c1c6SMikulas Patocka return r; 271569267a30SAlasdair G Kergon } 271669267a30SAlasdair G Kergon 27177a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md) 27187a8c3d3bSMike Anderson { 27197a8c3d3bSMike Anderson return atomic_add_return(1, &md->uevent_seq); 27207a8c3d3bSMike Anderson } 27217a8c3d3bSMike Anderson 27221da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md) 27231da177e4SLinus Torvalds { 27241da177e4SLinus Torvalds return atomic_read(&md->event_nr); 27251da177e4SLinus Torvalds } 27261da177e4SLinus Torvalds 27271da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr) 27281da177e4SLinus Torvalds { 27291da177e4SLinus Torvalds return wait_event_interruptible(md->eventq, 27301da177e4SLinus Torvalds (event_nr != atomic_read(&md->event_nr))); 27311da177e4SLinus Torvalds } 27321da177e4SLinus Torvalds 27337a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist) 27347a8c3d3bSMike Anderson { 27357a8c3d3bSMike Anderson unsigned long flags; 27367a8c3d3bSMike Anderson 27377a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 27387a8c3d3bSMike Anderson list_add(elist, &md->uevent_list); 27397a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 27407a8c3d3bSMike Anderson } 27417a8c3d3bSMike Anderson 27421da177e4SLinus Torvalds /* 27431da177e4SLinus Torvalds * The gendisk is only valid as long as you have a reference 27441da177e4SLinus Torvalds * count on 'md'. 27451da177e4SLinus Torvalds */ 27461da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md) 27471da177e4SLinus Torvalds { 27481da177e4SLinus Torvalds return md->disk; 27491da177e4SLinus Torvalds } 275065ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk); 27511da177e4SLinus Torvalds 2752784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md) 2753784aae73SMilan Broz { 27542995fa78SMikulas Patocka return &md->kobj_holder.kobj; 2755784aae73SMilan Broz } 2756784aae73SMilan Broz 2757784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj) 2758784aae73SMilan Broz { 2759784aae73SMilan Broz struct mapped_device *md; 2760784aae73SMilan Broz 27612995fa78SMikulas Patocka md = container_of(kobj, struct mapped_device, kobj_holder.kobj); 2762784aae73SMilan Broz 2763b9a41d21SHou Tao spin_lock(&_minor_lock); 2764b9a41d21SHou Tao if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { 2765b9a41d21SHou Tao md = NULL; 2766b9a41d21SHou Tao goto out; 2767b9a41d21SHou Tao } 2768784aae73SMilan Broz dm_get(md); 2769b9a41d21SHou Tao out: 2770b9a41d21SHou Tao spin_unlock(&_minor_lock); 2771b9a41d21SHou Tao 2772784aae73SMilan Broz return md; 2773784aae73SMilan Broz } 2774784aae73SMilan Broz 27754f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md) 27761da177e4SLinus Torvalds { 27771da177e4SLinus Torvalds return test_bit(DMF_SUSPENDED, &md->flags); 27781da177e4SLinus Torvalds } 27791da177e4SLinus Torvalds 27805df96f2bSMikulas Patocka static int dm_post_suspending_md(struct mapped_device *md) 27815df96f2bSMikulas Patocka { 27825df96f2bSMikulas Patocka return test_bit(DMF_POST_SUSPENDING, &md->flags); 27835df96f2bSMikulas Patocka } 27845df96f2bSMikulas Patocka 2785ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md) 2786ffcc3936SMike Snitzer { 2787ffcc3936SMike Snitzer return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2788ffcc3936SMike Snitzer } 2789ffcc3936SMike Snitzer 27902c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md) 27912c140a24SMikulas Patocka { 27922c140a24SMikulas Patocka return test_bit(DMF_DEFERRED_REMOVE, &md->flags); 27932c140a24SMikulas Patocka } 27942c140a24SMikulas Patocka 279564dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti) 279664dbce58SKiyoshi Ueda { 279733bd6f06SMike Snitzer return dm_suspended_md(ti->table->md); 279864dbce58SKiyoshi Ueda } 279964dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended); 280064dbce58SKiyoshi Ueda 28015df96f2bSMikulas Patocka int dm_post_suspending(struct dm_target *ti) 28025df96f2bSMikulas Patocka { 280333bd6f06SMike Snitzer return dm_post_suspending_md(ti->table->md); 28045df96f2bSMikulas Patocka } 28055df96f2bSMikulas Patocka EXPORT_SYMBOL_GPL(dm_post_suspending); 28065df96f2bSMikulas Patocka 28072e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti) 28082e93ccc1SKiyoshi Ueda { 280933bd6f06SMike Snitzer return __noflush_suspending(ti->table->md); 28102e93ccc1SKiyoshi Ueda } 28112e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending); 28122e93ccc1SKiyoshi Ueda 28137e0d574fSBart Van Assche struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type, 28140776aa0eSMike Snitzer unsigned integrity, unsigned per_io_data_size, 28150776aa0eSMike Snitzer unsigned min_pool_size) 2816e6ee8c0bSKiyoshi Ueda { 2817115485e8SMike Snitzer struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id); 281878d8e58aSMike Snitzer unsigned int pool_size = 0; 281964f52b0eSMike Snitzer unsigned int front_pad, io_front_pad; 28206f1c819cSKent Overstreet int ret; 2821e6ee8c0bSKiyoshi Ueda 2822e6ee8c0bSKiyoshi Ueda if (!pools) 28234e6e36c3SMike Snitzer return NULL; 2824e6ee8c0bSKiyoshi Ueda 282578d8e58aSMike Snitzer switch (type) { 282678d8e58aSMike Snitzer case DM_TYPE_BIO_BASED: 2827545ed20eSToshi Kani case DM_TYPE_DAX_BIO_BASED: 28280776aa0eSMike Snitzer pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size); 282962f26317SJeffle Xu front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + DM_TARGET_IO_BIO_OFFSET; 283062f26317SJeffle Xu io_front_pad = roundup(per_io_data_size, __alignof__(struct dm_io)) + DM_IO_BIO_OFFSET; 28316f1c819cSKent Overstreet ret = bioset_init(&pools->io_bs, pool_size, io_front_pad, 0); 28326f1c819cSKent Overstreet if (ret) 283364f52b0eSMike Snitzer goto out; 28346f1c819cSKent Overstreet if (integrity && bioset_integrity_create(&pools->io_bs, pool_size)) 2835eb8db831SChristoph Hellwig goto out; 283678d8e58aSMike Snitzer break; 283778d8e58aSMike Snitzer case DM_TYPE_REQUEST_BASED: 28380776aa0eSMike Snitzer pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size); 283978d8e58aSMike Snitzer front_pad = offsetof(struct dm_rq_clone_bio_info, clone); 2840591ddcfcSMike Snitzer /* per_io_data_size is used for blk-mq pdu at queue allocation */ 284178d8e58aSMike Snitzer break; 284278d8e58aSMike Snitzer default: 284378d8e58aSMike Snitzer BUG(); 284478d8e58aSMike Snitzer } 284578d8e58aSMike Snitzer 28466f1c819cSKent Overstreet ret = bioset_init(&pools->bs, pool_size, front_pad, 0); 28476f1c819cSKent Overstreet if (ret) 28485f015204SJun'ichi Nomura goto out; 2849e6ee8c0bSKiyoshi Ueda 28506f1c819cSKent Overstreet if (integrity && bioset_integrity_create(&pools->bs, pool_size)) 28515f015204SJun'ichi Nomura goto out; 2852a91a2785SMartin K. Petersen 2853e6ee8c0bSKiyoshi Ueda return pools; 285478d8e58aSMike Snitzer 28555f015204SJun'ichi Nomura out: 28565f015204SJun'ichi Nomura dm_free_md_mempools(pools); 2857e6ee8c0bSKiyoshi Ueda 28584e6e36c3SMike Snitzer return NULL; 2859e6ee8c0bSKiyoshi Ueda } 2860e6ee8c0bSKiyoshi Ueda 2861e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools) 2862e6ee8c0bSKiyoshi Ueda { 2863e6ee8c0bSKiyoshi Ueda if (!pools) 2864e6ee8c0bSKiyoshi Ueda return; 2865e6ee8c0bSKiyoshi Ueda 28666f1c819cSKent Overstreet bioset_exit(&pools->bs); 28676f1c819cSKent Overstreet bioset_exit(&pools->io_bs); 2868e6ee8c0bSKiyoshi Ueda 2869e6ee8c0bSKiyoshi Ueda kfree(pools); 2870e6ee8c0bSKiyoshi Ueda } 2871e6ee8c0bSKiyoshi Ueda 28729c72bad1SChristoph Hellwig struct dm_pr { 28739c72bad1SChristoph Hellwig u64 old_key; 28749c72bad1SChristoph Hellwig u64 new_key; 28759c72bad1SChristoph Hellwig u32 flags; 28769c72bad1SChristoph Hellwig bool fail_early; 28779c72bad1SChristoph Hellwig }; 28789c72bad1SChristoph Hellwig 28799c72bad1SChristoph Hellwig static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn, 28809c72bad1SChristoph Hellwig void *data) 28819c72bad1SChristoph Hellwig { 28829c72bad1SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 28839c72bad1SChristoph Hellwig struct dm_table *table; 28849c72bad1SChristoph Hellwig struct dm_target *ti; 28859c72bad1SChristoph Hellwig int ret = -ENOTTY, srcu_idx; 28869c72bad1SChristoph Hellwig 28879c72bad1SChristoph Hellwig table = dm_get_live_table(md, &srcu_idx); 28889c72bad1SChristoph Hellwig if (!table || !dm_table_get_size(table)) 28899c72bad1SChristoph Hellwig goto out; 28909c72bad1SChristoph Hellwig 28919c72bad1SChristoph Hellwig /* We only support devices that have a single target */ 28929c72bad1SChristoph Hellwig if (dm_table_get_num_targets(table) != 1) 28939c72bad1SChristoph Hellwig goto out; 28949c72bad1SChristoph Hellwig ti = dm_table_get_target(table, 0); 28959c72bad1SChristoph Hellwig 28969c72bad1SChristoph Hellwig ret = -EINVAL; 28979c72bad1SChristoph Hellwig if (!ti->type->iterate_devices) 28989c72bad1SChristoph Hellwig goto out; 28999c72bad1SChristoph Hellwig 29009c72bad1SChristoph Hellwig ret = ti->type->iterate_devices(ti, fn, data); 29019c72bad1SChristoph Hellwig out: 29029c72bad1SChristoph Hellwig dm_put_live_table(md, srcu_idx); 29039c72bad1SChristoph Hellwig return ret; 29049c72bad1SChristoph Hellwig } 29059c72bad1SChristoph Hellwig 29069c72bad1SChristoph Hellwig /* 29079c72bad1SChristoph Hellwig * For register / unregister we need to manually call out to every path. 29089c72bad1SChristoph Hellwig */ 29099c72bad1SChristoph Hellwig static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev, 29109c72bad1SChristoph Hellwig sector_t start, sector_t len, void *data) 29119c72bad1SChristoph Hellwig { 29129c72bad1SChristoph Hellwig struct dm_pr *pr = data; 29139c72bad1SChristoph Hellwig const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops; 29149c72bad1SChristoph Hellwig 29159c72bad1SChristoph Hellwig if (!ops || !ops->pr_register) 29169c72bad1SChristoph Hellwig return -EOPNOTSUPP; 29179c72bad1SChristoph Hellwig return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags); 29189c72bad1SChristoph Hellwig } 29199c72bad1SChristoph Hellwig 292071cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key, 292171cdb697SChristoph Hellwig u32 flags) 292271cdb697SChristoph Hellwig { 29239c72bad1SChristoph Hellwig struct dm_pr pr = { 29249c72bad1SChristoph Hellwig .old_key = old_key, 29259c72bad1SChristoph Hellwig .new_key = new_key, 29269c72bad1SChristoph Hellwig .flags = flags, 29279c72bad1SChristoph Hellwig .fail_early = true, 29289c72bad1SChristoph Hellwig }; 29299c72bad1SChristoph Hellwig int ret; 293071cdb697SChristoph Hellwig 29319c72bad1SChristoph Hellwig ret = dm_call_pr(bdev, __dm_pr_register, &pr); 29329c72bad1SChristoph Hellwig if (ret && new_key) { 29339c72bad1SChristoph Hellwig /* unregister all paths if we failed to register any path */ 29349c72bad1SChristoph Hellwig pr.old_key = new_key; 29359c72bad1SChristoph Hellwig pr.new_key = 0; 29369c72bad1SChristoph Hellwig pr.flags = 0; 29379c72bad1SChristoph Hellwig pr.fail_early = false; 29389c72bad1SChristoph Hellwig dm_call_pr(bdev, __dm_pr_register, &pr); 29399c72bad1SChristoph Hellwig } 294071cdb697SChristoph Hellwig 29419c72bad1SChristoph Hellwig return ret; 294271cdb697SChristoph Hellwig } 294371cdb697SChristoph Hellwig 294471cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type, 294571cdb697SChristoph Hellwig u32 flags) 294671cdb697SChristoph Hellwig { 294771cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 294871cdb697SChristoph Hellwig const struct pr_ops *ops; 2949971888c4SMike Snitzer int r, srcu_idx; 295071cdb697SChristoph Hellwig 29515bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 295271cdb697SChristoph Hellwig if (r < 0) 2953971888c4SMike Snitzer goto out; 295471cdb697SChristoph Hellwig 295571cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 295671cdb697SChristoph Hellwig if (ops && ops->pr_reserve) 295771cdb697SChristoph Hellwig r = ops->pr_reserve(bdev, key, type, flags); 295871cdb697SChristoph Hellwig else 295971cdb697SChristoph Hellwig r = -EOPNOTSUPP; 2960971888c4SMike Snitzer out: 2961971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 296271cdb697SChristoph Hellwig return r; 296371cdb697SChristoph Hellwig } 296471cdb697SChristoph Hellwig 296571cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type) 296671cdb697SChristoph Hellwig { 296771cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 296871cdb697SChristoph Hellwig const struct pr_ops *ops; 2969971888c4SMike Snitzer int r, srcu_idx; 297071cdb697SChristoph Hellwig 29715bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 297271cdb697SChristoph Hellwig if (r < 0) 2973971888c4SMike Snitzer goto out; 297471cdb697SChristoph Hellwig 297571cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 297671cdb697SChristoph Hellwig if (ops && ops->pr_release) 297771cdb697SChristoph Hellwig r = ops->pr_release(bdev, key, type); 297871cdb697SChristoph Hellwig else 297971cdb697SChristoph Hellwig r = -EOPNOTSUPP; 2980971888c4SMike Snitzer out: 2981971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 298271cdb697SChristoph Hellwig return r; 298371cdb697SChristoph Hellwig } 298471cdb697SChristoph Hellwig 298571cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key, 298671cdb697SChristoph Hellwig enum pr_type type, bool abort) 298771cdb697SChristoph Hellwig { 298871cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 298971cdb697SChristoph Hellwig const struct pr_ops *ops; 2990971888c4SMike Snitzer int r, srcu_idx; 299171cdb697SChristoph Hellwig 29925bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 299371cdb697SChristoph Hellwig if (r < 0) 2994971888c4SMike Snitzer goto out; 299571cdb697SChristoph Hellwig 299671cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 299771cdb697SChristoph Hellwig if (ops && ops->pr_preempt) 299871cdb697SChristoph Hellwig r = ops->pr_preempt(bdev, old_key, new_key, type, abort); 299971cdb697SChristoph Hellwig else 300071cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3001971888c4SMike Snitzer out: 3002971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 300371cdb697SChristoph Hellwig return r; 300471cdb697SChristoph Hellwig } 300571cdb697SChristoph Hellwig 300671cdb697SChristoph Hellwig static int dm_pr_clear(struct block_device *bdev, u64 key) 300771cdb697SChristoph Hellwig { 300871cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 300971cdb697SChristoph Hellwig const struct pr_ops *ops; 3010971888c4SMike Snitzer int r, srcu_idx; 301171cdb697SChristoph Hellwig 30125bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 301371cdb697SChristoph Hellwig if (r < 0) 3014971888c4SMike Snitzer goto out; 301571cdb697SChristoph Hellwig 301671cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 301771cdb697SChristoph Hellwig if (ops && ops->pr_clear) 301871cdb697SChristoph Hellwig r = ops->pr_clear(bdev, key); 301971cdb697SChristoph Hellwig else 302071cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3021971888c4SMike Snitzer out: 3022971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 302371cdb697SChristoph Hellwig return r; 302471cdb697SChristoph Hellwig } 302571cdb697SChristoph Hellwig 302671cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = { 302771cdb697SChristoph Hellwig .pr_register = dm_pr_register, 302871cdb697SChristoph Hellwig .pr_reserve = dm_pr_reserve, 302971cdb697SChristoph Hellwig .pr_release = dm_pr_release, 303071cdb697SChristoph Hellwig .pr_preempt = dm_pr_preempt, 303171cdb697SChristoph Hellwig .pr_clear = dm_pr_clear, 303271cdb697SChristoph Hellwig }; 303371cdb697SChristoph Hellwig 303483d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = { 3035c62b37d9SChristoph Hellwig .submit_bio = dm_submit_bio, 30361da177e4SLinus Torvalds .open = dm_blk_open, 30371da177e4SLinus Torvalds .release = dm_blk_close, 3038aa129a22SMilan Broz .ioctl = dm_blk_ioctl, 30393ac51e74SDarrick J. Wong .getgeo = dm_blk_getgeo, 3040e76239a3SChristoph Hellwig .report_zones = dm_blk_report_zones, 304171cdb697SChristoph Hellwig .pr_ops = &dm_pr_ops, 30421da177e4SLinus Torvalds .owner = THIS_MODULE 30431da177e4SLinus Torvalds }; 30441da177e4SLinus Torvalds 3045681cc5e8SMike Snitzer static const struct block_device_operations dm_rq_blk_dops = { 3046681cc5e8SMike Snitzer .open = dm_blk_open, 3047681cc5e8SMike Snitzer .release = dm_blk_close, 3048681cc5e8SMike Snitzer .ioctl = dm_blk_ioctl, 3049681cc5e8SMike Snitzer .getgeo = dm_blk_getgeo, 3050681cc5e8SMike Snitzer .pr_ops = &dm_pr_ops, 3051681cc5e8SMike Snitzer .owner = THIS_MODULE 3052681cc5e8SMike Snitzer }; 3053681cc5e8SMike Snitzer 3054f26c5719SDan Williams static const struct dax_operations dm_dax_ops = { 3055f26c5719SDan Williams .direct_access = dm_dax_direct_access, 30567bf7eac8SDan Williams .dax_supported = dm_dax_supported, 30577e026c8cSDan Williams .copy_from_iter = dm_dax_copy_from_iter, 3058b3a9a0c3SDan Williams .copy_to_iter = dm_dax_copy_to_iter, 3059cdf6cdcdSVivek Goyal .zero_page_range = dm_dax_zero_page_range, 3060f26c5719SDan Williams }; 3061f26c5719SDan Williams 30621da177e4SLinus Torvalds /* 30631da177e4SLinus Torvalds * module hooks 30641da177e4SLinus Torvalds */ 30651da177e4SLinus Torvalds module_init(dm_init); 30661da177e4SLinus Torvalds module_exit(dm_exit); 30671da177e4SLinus Torvalds 30681da177e4SLinus Torvalds module_param(major, uint, 0); 30691da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper"); 3070f4790826SMike Snitzer 3071e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR); 3072e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools"); 3073e8603136SMike Snitzer 3074115485e8SMike Snitzer module_param(dm_numa_node, int, S_IRUGO | S_IWUSR); 3075115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations"); 3076115485e8SMike Snitzer 3077a666e5c0SMikulas Patocka module_param(swap_bios, int, S_IRUGO | S_IWUSR); 3078a666e5c0SMikulas Patocka MODULE_PARM_DESC(swap_bios, "Maximum allowed inflight swap IOs"); 3079a666e5c0SMikulas Patocka 30801da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver"); 30811da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 30821da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 3083