11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Copyright (C) 2001, 2002 Sistina Software (UK) Limited. 3784aae73SMilan Broz * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This file is released under the GPL. 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 84cc96131SMike Snitzer #include "dm-core.h" 94cc96131SMike Snitzer #include "dm-rq.h" 1051e5b2bdSMike Anderson #include "dm-uevent.h" 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #include <linux/init.h> 131da177e4SLinus Torvalds #include <linux/module.h> 1448c9c27bSArjan van de Ven #include <linux/mutex.h> 156958c1c6SMikulas Patocka #include <linux/sched/mm.h> 16174cd4b1SIngo Molnar #include <linux/sched/signal.h> 171da177e4SLinus Torvalds #include <linux/blkpg.h> 181da177e4SLinus Torvalds #include <linux/bio.h> 191da177e4SLinus Torvalds #include <linux/mempool.h> 20f26c5719SDan Williams #include <linux/dax.h> 211da177e4SLinus Torvalds #include <linux/slab.h> 221da177e4SLinus Torvalds #include <linux/idr.h> 237e026c8cSDan Williams #include <linux/uio.h> 243ac51e74SDarrick J. Wong #include <linux/hdreg.h> 253f77316dSKiyoshi Ueda #include <linux/delay.h> 26ffcc3936SMike Snitzer #include <linux/wait.h> 2771cdb697SChristoph Hellwig #include <linux/pr.h> 28b0b4d7c6SElena Reshetova #include <linux/refcount.h> 29c6a564ffSChristoph Hellwig #include <linux/part_stat.h> 30a892c8d5SSatya Tangirala #include <linux/blk-crypto.h> 3155782138SLi Zefan 3272d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core" 3372d94861SAlasdair G Kergon 3460935eb2SMilan Broz /* 3560935eb2SMilan Broz * Cookies are numeric values sent with CHANGE and REMOVE 3660935eb2SMilan Broz * uevents while resuming, removing or renaming the device. 3760935eb2SMilan Broz */ 3860935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE" 3960935eb2SMilan Broz #define DM_COOKIE_LENGTH 24 4060935eb2SMilan Broz 411da177e4SLinus Torvalds static const char *_name = DM_NAME; 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds static unsigned int major = 0; 441da177e4SLinus Torvalds static unsigned int _major = 0; 451da177e4SLinus Torvalds 46d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr); 47d15b774cSAlasdair G Kergon 48f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock); 492c140a24SMikulas Patocka 502c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w); 512c140a24SMikulas Patocka 522c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove); 532c140a24SMikulas Patocka 54acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue; 55acfe0ad7SMikulas Patocka 5693e6442cSMikulas Patocka atomic_t dm_global_event_nr = ATOMIC_INIT(0); 5793e6442cSMikulas Patocka DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq); 5893e6442cSMikulas Patocka 5962e08243SMikulas Patocka void dm_issue_global_event(void) 6062e08243SMikulas Patocka { 6162e08243SMikulas Patocka atomic_inc(&dm_global_event_nr); 6262e08243SMikulas Patocka wake_up(&dm_global_eventq); 6362e08243SMikulas Patocka } 6462e08243SMikulas Patocka 651da177e4SLinus Torvalds /* 6664f52b0eSMike Snitzer * One of these is allocated (on-stack) per original bio. 671da177e4SLinus Torvalds */ 6864f52b0eSMike Snitzer struct clone_info { 6964f52b0eSMike Snitzer struct dm_table *map; 7064f52b0eSMike Snitzer struct bio *bio; 7164f52b0eSMike Snitzer struct dm_io *io; 7264f52b0eSMike Snitzer sector_t sector; 7364f52b0eSMike Snitzer unsigned sector_count; 7464f52b0eSMike Snitzer }; 7564f52b0eSMike Snitzer 7664f52b0eSMike Snitzer /* 7764f52b0eSMike Snitzer * One of these is allocated per clone bio. 7864f52b0eSMike Snitzer */ 7964f52b0eSMike Snitzer #define DM_TIO_MAGIC 7282014 8064f52b0eSMike Snitzer struct dm_target_io { 8164f52b0eSMike Snitzer unsigned magic; 8264f52b0eSMike Snitzer struct dm_io *io; 8364f52b0eSMike Snitzer struct dm_target *ti; 8464f52b0eSMike Snitzer unsigned target_bio_nr; 8564f52b0eSMike Snitzer unsigned *len_ptr; 8664f52b0eSMike Snitzer bool inside_dm_io; 8764f52b0eSMike Snitzer struct bio clone; 8864f52b0eSMike Snitzer }; 8964f52b0eSMike Snitzer 9064f52b0eSMike Snitzer /* 9164f52b0eSMike Snitzer * One of these is allocated per original bio. 9264f52b0eSMike Snitzer * It contains the first clone used for that original. 9364f52b0eSMike Snitzer */ 9464f52b0eSMike Snitzer #define DM_IO_MAGIC 5191977 951da177e4SLinus Torvalds struct dm_io { 9664f52b0eSMike Snitzer unsigned magic; 971da177e4SLinus Torvalds struct mapped_device *md; 984e4cbee9SChristoph Hellwig blk_status_t status; 991da177e4SLinus Torvalds atomic_t io_count; 100745dc570SMike Snitzer struct bio *orig_bio; 1013eaf840eSJun'ichi "Nick" Nomura unsigned long start_time; 102f88fb981SKiyoshi Ueda spinlock_t endio_lock; 103fd2ed4d2SMikulas Patocka struct dm_stats_aux stats_aux; 10464f52b0eSMike Snitzer /* last member of dm_target_io is 'struct bio' */ 10564f52b0eSMike Snitzer struct dm_target_io tio; 1061da177e4SLinus Torvalds }; 1071da177e4SLinus Torvalds 10864f52b0eSMike Snitzer void *dm_per_bio_data(struct bio *bio, size_t data_size) 10964f52b0eSMike Snitzer { 11064f52b0eSMike Snitzer struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); 11164f52b0eSMike Snitzer if (!tio->inside_dm_io) 11264f52b0eSMike Snitzer return (char *)bio - offsetof(struct dm_target_io, clone) - data_size; 11364f52b0eSMike Snitzer return (char *)bio - offsetof(struct dm_target_io, clone) - offsetof(struct dm_io, tio) - data_size; 11464f52b0eSMike Snitzer } 11564f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_per_bio_data); 11664f52b0eSMike Snitzer 11764f52b0eSMike Snitzer struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size) 11864f52b0eSMike Snitzer { 11964f52b0eSMike Snitzer struct dm_io *io = (struct dm_io *)((char *)data + data_size); 12064f52b0eSMike Snitzer if (io->magic == DM_IO_MAGIC) 12164f52b0eSMike Snitzer return (struct bio *)((char *)io + offsetof(struct dm_io, tio) + offsetof(struct dm_target_io, clone)); 12264f52b0eSMike Snitzer BUG_ON(io->magic != DM_TIO_MAGIC); 12364f52b0eSMike Snitzer return (struct bio *)((char *)io + offsetof(struct dm_target_io, clone)); 12464f52b0eSMike Snitzer } 12564f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_from_per_bio_data); 12664f52b0eSMike Snitzer 12764f52b0eSMike Snitzer unsigned dm_bio_get_target_bio_nr(const struct bio *bio) 12864f52b0eSMike Snitzer { 12964f52b0eSMike Snitzer return container_of(bio, struct dm_target_io, clone)->target_bio_nr; 13064f52b0eSMike Snitzer } 13164f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_get_target_bio_nr); 13264f52b0eSMike Snitzer 133ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1) 134ba61fdd1SJeff Mahoney 1351da177e4SLinus Torvalds /* 1361da177e4SLinus Torvalds * Bits for the md->flags field. 1371da177e4SLinus Torvalds */ 1381eb787ecSAlasdair G Kergon #define DMF_BLOCK_IO_FOR_SUSPEND 0 1391da177e4SLinus Torvalds #define DMF_SUSPENDED 1 140aa8d7c2fSAlasdair G Kergon #define DMF_FROZEN 2 141fba9f90eSJeff Mahoney #define DMF_FREEING 3 1425c6bd75dSAlasdair G Kergon #define DMF_DELETING 4 1432e93ccc1SKiyoshi Ueda #define DMF_NOFLUSH_SUSPENDING 5 1448ae12666SKent Overstreet #define DMF_DEFERRED_REMOVE 6 1458ae12666SKent Overstreet #define DMF_SUSPENDED_INTERNALLY 7 1465df96f2bSMikulas Patocka #define DMF_POST_SUSPENDING 8 1471da177e4SLinus Torvalds 148115485e8SMike Snitzer #define DM_NUMA_NODE NUMA_NO_NODE 149115485e8SMike Snitzer static int dm_numa_node = DM_NUMA_NODE; 150faad87dfSMike Snitzer 151e6ee8c0bSKiyoshi Ueda /* 152e6ee8c0bSKiyoshi Ueda * For mempools pre-allocation at the table loading time. 153e6ee8c0bSKiyoshi Ueda */ 154e6ee8c0bSKiyoshi Ueda struct dm_md_mempools { 1556f1c819cSKent Overstreet struct bio_set bs; 1566f1c819cSKent Overstreet struct bio_set io_bs; 157e6ee8c0bSKiyoshi Ueda }; 158e6ee8c0bSKiyoshi Ueda 15986f1152bSBenjamin Marzinski struct table_device { 16086f1152bSBenjamin Marzinski struct list_head list; 161b0b4d7c6SElena Reshetova refcount_t count; 16286f1152bSBenjamin Marzinski struct dm_dev dm_dev; 16386f1152bSBenjamin Marzinski }; 16486f1152bSBenjamin Marzinski 165f4790826SMike Snitzer /* 166e8603136SMike Snitzer * Bio-based DM's mempools' reserved IOs set by the user. 167e8603136SMike Snitzer */ 1684cc96131SMike Snitzer #define RESERVED_BIO_BASED_IOS 16 169e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS; 170e8603136SMike Snitzer 171115485e8SMike Snitzer static int __dm_get_module_param_int(int *module_param, int min, int max) 172115485e8SMike Snitzer { 1736aa7de05SMark Rutland int param = READ_ONCE(*module_param); 174115485e8SMike Snitzer int modified_param = 0; 175115485e8SMike Snitzer bool modified = true; 176115485e8SMike Snitzer 177115485e8SMike Snitzer if (param < min) 178115485e8SMike Snitzer modified_param = min; 179115485e8SMike Snitzer else if (param > max) 180115485e8SMike Snitzer modified_param = max; 181115485e8SMike Snitzer else 182115485e8SMike Snitzer modified = false; 183115485e8SMike Snitzer 184115485e8SMike Snitzer if (modified) { 185115485e8SMike Snitzer (void)cmpxchg(module_param, param, modified_param); 186115485e8SMike Snitzer param = modified_param; 187115485e8SMike Snitzer } 188115485e8SMike Snitzer 189115485e8SMike Snitzer return param; 190115485e8SMike Snitzer } 191115485e8SMike Snitzer 1924cc96131SMike Snitzer unsigned __dm_get_module_param(unsigned *module_param, 193f4790826SMike Snitzer unsigned def, unsigned max) 194f4790826SMike Snitzer { 1956aa7de05SMark Rutland unsigned param = READ_ONCE(*module_param); 19609c2d531SMike Snitzer unsigned modified_param = 0; 197f4790826SMike Snitzer 19809c2d531SMike Snitzer if (!param) 19909c2d531SMike Snitzer modified_param = def; 20009c2d531SMike Snitzer else if (param > max) 20109c2d531SMike Snitzer modified_param = max; 202f4790826SMike Snitzer 20309c2d531SMike Snitzer if (modified_param) { 20409c2d531SMike Snitzer (void)cmpxchg(module_param, param, modified_param); 20509c2d531SMike Snitzer param = modified_param; 206f4790826SMike Snitzer } 207f4790826SMike Snitzer 20809c2d531SMike Snitzer return param; 209f4790826SMike Snitzer } 210f4790826SMike Snitzer 211e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void) 212e8603136SMike Snitzer { 21309c2d531SMike Snitzer return __dm_get_module_param(&reserved_bio_based_ios, 2144cc96131SMike Snitzer RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS); 215e8603136SMike Snitzer } 216e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios); 217e8603136SMike Snitzer 218115485e8SMike Snitzer static unsigned dm_get_numa_node(void) 219115485e8SMike Snitzer { 220115485e8SMike Snitzer return __dm_get_module_param_int(&dm_numa_node, 221115485e8SMike Snitzer DM_NUMA_NODE, num_online_nodes() - 1); 222115485e8SMike Snitzer } 223115485e8SMike Snitzer 2241da177e4SLinus Torvalds static int __init local_init(void) 2251da177e4SLinus Torvalds { 226e689fbabSMike Snitzer int r; 2271ae49ea2SMike Snitzer 22851e5b2bdSMike Anderson r = dm_uevent_init(); 22951157b4aSKiyoshi Ueda if (r) 230e689fbabSMike Snitzer return r; 23151e5b2bdSMike Anderson 232acfe0ad7SMikulas Patocka deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1); 233acfe0ad7SMikulas Patocka if (!deferred_remove_workqueue) { 234acfe0ad7SMikulas Patocka r = -ENOMEM; 235acfe0ad7SMikulas Patocka goto out_uevent_exit; 236acfe0ad7SMikulas Patocka } 237acfe0ad7SMikulas Patocka 2381da177e4SLinus Torvalds _major = major; 2391da177e4SLinus Torvalds r = register_blkdev(_major, _name); 24051157b4aSKiyoshi Ueda if (r < 0) 241acfe0ad7SMikulas Patocka goto out_free_workqueue; 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds if (!_major) 2441da177e4SLinus Torvalds _major = r; 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds return 0; 24751157b4aSKiyoshi Ueda 248acfe0ad7SMikulas Patocka out_free_workqueue: 249acfe0ad7SMikulas Patocka destroy_workqueue(deferred_remove_workqueue); 25051157b4aSKiyoshi Ueda out_uevent_exit: 25151157b4aSKiyoshi Ueda dm_uevent_exit(); 25251157b4aSKiyoshi Ueda 25351157b4aSKiyoshi Ueda return r; 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds static void local_exit(void) 2571da177e4SLinus Torvalds { 2582c140a24SMikulas Patocka flush_scheduled_work(); 259acfe0ad7SMikulas Patocka destroy_workqueue(deferred_remove_workqueue); 2602c140a24SMikulas Patocka 26100d59405SAkinobu Mita unregister_blkdev(_major, _name); 26251e5b2bdSMike Anderson dm_uevent_exit(); 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds _major = 0; 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds DMINFO("cleaned up"); 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds 269b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = { 2701da177e4SLinus Torvalds local_init, 2711da177e4SLinus Torvalds dm_target_init, 2721da177e4SLinus Torvalds dm_linear_init, 2731da177e4SLinus Torvalds dm_stripe_init, 274952b3557SMikulas Patocka dm_io_init, 275945fa4d2SMikulas Patocka dm_kcopyd_init, 2761da177e4SLinus Torvalds dm_interface_init, 277fd2ed4d2SMikulas Patocka dm_statistics_init, 2781da177e4SLinus Torvalds }; 2791da177e4SLinus Torvalds 280b9249e55SAlasdair G Kergon static void (*_exits[])(void) = { 2811da177e4SLinus Torvalds local_exit, 2821da177e4SLinus Torvalds dm_target_exit, 2831da177e4SLinus Torvalds dm_linear_exit, 2841da177e4SLinus Torvalds dm_stripe_exit, 285952b3557SMikulas Patocka dm_io_exit, 286945fa4d2SMikulas Patocka dm_kcopyd_exit, 2871da177e4SLinus Torvalds dm_interface_exit, 288fd2ed4d2SMikulas Patocka dm_statistics_exit, 2891da177e4SLinus Torvalds }; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds static int __init dm_init(void) 2921da177e4SLinus Torvalds { 2931da177e4SLinus Torvalds const int count = ARRAY_SIZE(_inits); 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds int r, i; 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds for (i = 0; i < count; i++) { 2981da177e4SLinus Torvalds r = _inits[i](); 2991da177e4SLinus Torvalds if (r) 3001da177e4SLinus Torvalds goto bad; 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds return 0; 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds bad: 3061da177e4SLinus Torvalds while (i--) 3071da177e4SLinus Torvalds _exits[i](); 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds return r; 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds static void __exit dm_exit(void) 3131da177e4SLinus Torvalds { 3141da177e4SLinus Torvalds int i = ARRAY_SIZE(_exits); 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds while (i--) 3171da177e4SLinus Torvalds _exits[i](); 318d15b774cSAlasdair G Kergon 319d15b774cSAlasdair G Kergon /* 320d15b774cSAlasdair G Kergon * Should be empty by this point. 321d15b774cSAlasdair G Kergon */ 322d15b774cSAlasdair G Kergon idr_destroy(&_minor_idr); 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds /* 3261da177e4SLinus Torvalds * Block device functions 3271da177e4SLinus Torvalds */ 328432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md) 329432a212cSMike Anderson { 330432a212cSMike Anderson return test_bit(DMF_DELETING, &md->flags); 331432a212cSMike Anderson } 332432a212cSMike Anderson 333fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode) 3341da177e4SLinus Torvalds { 3351da177e4SLinus Torvalds struct mapped_device *md; 3361da177e4SLinus Torvalds 337fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 338fba9f90eSJeff Mahoney 339fe5f9f2cSAl Viro md = bdev->bd_disk->private_data; 340fba9f90eSJeff Mahoney if (!md) 341fba9f90eSJeff Mahoney goto out; 342fba9f90eSJeff Mahoney 3435c6bd75dSAlasdair G Kergon if (test_bit(DMF_FREEING, &md->flags) || 344432a212cSMike Anderson dm_deleting_md(md)) { 345fba9f90eSJeff Mahoney md = NULL; 346fba9f90eSJeff Mahoney goto out; 347fba9f90eSJeff Mahoney } 348fba9f90eSJeff Mahoney 3491da177e4SLinus Torvalds dm_get(md); 3505c6bd75dSAlasdair G Kergon atomic_inc(&md->open_count); 351fba9f90eSJeff Mahoney out: 352fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 353fba9f90eSJeff Mahoney 354fba9f90eSJeff Mahoney return md ? 0 : -ENXIO; 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds 357db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode) 3581da177e4SLinus Torvalds { 35963a4f065SMike Snitzer struct mapped_device *md; 3606e9624b8SArnd Bergmann 3614a1aeb98SMilan Broz spin_lock(&_minor_lock); 3624a1aeb98SMilan Broz 36363a4f065SMike Snitzer md = disk->private_data; 36463a4f065SMike Snitzer if (WARN_ON(!md)) 36563a4f065SMike Snitzer goto out; 36663a4f065SMike Snitzer 3672c140a24SMikulas Patocka if (atomic_dec_and_test(&md->open_count) && 3682c140a24SMikulas Patocka (test_bit(DMF_DEFERRED_REMOVE, &md->flags))) 369acfe0ad7SMikulas Patocka queue_work(deferred_remove_workqueue, &deferred_remove_work); 3702c140a24SMikulas Patocka 3711da177e4SLinus Torvalds dm_put(md); 37263a4f065SMike Snitzer out: 3734a1aeb98SMilan Broz spin_unlock(&_minor_lock); 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds 3765c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md) 3775c6bd75dSAlasdair G Kergon { 3785c6bd75dSAlasdair G Kergon return atomic_read(&md->open_count); 3795c6bd75dSAlasdair G Kergon } 3805c6bd75dSAlasdair G Kergon 3815c6bd75dSAlasdair G Kergon /* 3825c6bd75dSAlasdair G Kergon * Guarantees nothing is using the device before it's deleted. 3835c6bd75dSAlasdair G Kergon */ 3842c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred) 3855c6bd75dSAlasdair G Kergon { 3865c6bd75dSAlasdair G Kergon int r = 0; 3875c6bd75dSAlasdair G Kergon 3885c6bd75dSAlasdair G Kergon spin_lock(&_minor_lock); 3895c6bd75dSAlasdair G Kergon 3902c140a24SMikulas Patocka if (dm_open_count(md)) { 3915c6bd75dSAlasdair G Kergon r = -EBUSY; 3922c140a24SMikulas Patocka if (mark_deferred) 3932c140a24SMikulas Patocka set_bit(DMF_DEFERRED_REMOVE, &md->flags); 3942c140a24SMikulas Patocka } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags)) 3952c140a24SMikulas Patocka r = -EEXIST; 3965c6bd75dSAlasdair G Kergon else 3975c6bd75dSAlasdair G Kergon set_bit(DMF_DELETING, &md->flags); 3985c6bd75dSAlasdair G Kergon 3995c6bd75dSAlasdair G Kergon spin_unlock(&_minor_lock); 4005c6bd75dSAlasdair G Kergon 4015c6bd75dSAlasdair G Kergon return r; 4025c6bd75dSAlasdair G Kergon } 4035c6bd75dSAlasdair G Kergon 4042c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md) 4052c140a24SMikulas Patocka { 4062c140a24SMikulas Patocka int r = 0; 4072c140a24SMikulas Patocka 4082c140a24SMikulas Patocka spin_lock(&_minor_lock); 4092c140a24SMikulas Patocka 4102c140a24SMikulas Patocka if (test_bit(DMF_DELETING, &md->flags)) 4112c140a24SMikulas Patocka r = -EBUSY; 4122c140a24SMikulas Patocka else 4132c140a24SMikulas Patocka clear_bit(DMF_DEFERRED_REMOVE, &md->flags); 4142c140a24SMikulas Patocka 4152c140a24SMikulas Patocka spin_unlock(&_minor_lock); 4162c140a24SMikulas Patocka 4172c140a24SMikulas Patocka return r; 4182c140a24SMikulas Patocka } 4192c140a24SMikulas Patocka 4202c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w) 4212c140a24SMikulas Patocka { 4222c140a24SMikulas Patocka dm_deferred_remove(); 4232c140a24SMikulas Patocka } 4242c140a24SMikulas Patocka 425fd2ed4d2SMikulas Patocka sector_t dm_get_size(struct mapped_device *md) 426fd2ed4d2SMikulas Patocka { 427fd2ed4d2SMikulas Patocka return get_capacity(md->disk); 428fd2ed4d2SMikulas Patocka } 429fd2ed4d2SMikulas Patocka 4309974fa2cSMike Snitzer struct request_queue *dm_get_md_queue(struct mapped_device *md) 4319974fa2cSMike Snitzer { 4329974fa2cSMike Snitzer return md->queue; 4339974fa2cSMike Snitzer } 4349974fa2cSMike Snitzer 435fd2ed4d2SMikulas Patocka struct dm_stats *dm_get_stats(struct mapped_device *md) 436fd2ed4d2SMikulas Patocka { 437fd2ed4d2SMikulas Patocka return &md->stats; 438fd2ed4d2SMikulas Patocka } 439fd2ed4d2SMikulas Patocka 4403ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 4413ac51e74SDarrick J. Wong { 4423ac51e74SDarrick J. Wong struct mapped_device *md = bdev->bd_disk->private_data; 4433ac51e74SDarrick J. Wong 4443ac51e74SDarrick J. Wong return dm_get_geometry(md, geo); 4453ac51e74SDarrick J. Wong } 4463ac51e74SDarrick J. Wong 447e76239a3SChristoph Hellwig #ifdef CONFIG_BLK_DEV_ZONED 448d4100351SChristoph Hellwig int dm_report_zones_cb(struct blk_zone *zone, unsigned int idx, void *data) 449d4100351SChristoph Hellwig { 450d4100351SChristoph Hellwig struct dm_report_zones_args *args = data; 451d4100351SChristoph Hellwig sector_t sector_diff = args->tgt->begin - args->start; 452d4100351SChristoph Hellwig 453d4100351SChristoph Hellwig /* 454d4100351SChristoph Hellwig * Ignore zones beyond the target range. 455d4100351SChristoph Hellwig */ 456d4100351SChristoph Hellwig if (zone->start >= args->start + args->tgt->len) 457d4100351SChristoph Hellwig return 0; 458d4100351SChristoph Hellwig 459d4100351SChristoph Hellwig /* 460d4100351SChristoph Hellwig * Remap the start sector and write pointer position of the zone 461d4100351SChristoph Hellwig * to match its position in the target range. 462d4100351SChristoph Hellwig */ 463d4100351SChristoph Hellwig zone->start += sector_diff; 464d4100351SChristoph Hellwig if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL) { 465d4100351SChristoph Hellwig if (zone->cond == BLK_ZONE_COND_FULL) 466d4100351SChristoph Hellwig zone->wp = zone->start + zone->len; 467d4100351SChristoph Hellwig else if (zone->cond == BLK_ZONE_COND_EMPTY) 468d4100351SChristoph Hellwig zone->wp = zone->start; 469d4100351SChristoph Hellwig else 470d4100351SChristoph Hellwig zone->wp += sector_diff; 471d4100351SChristoph Hellwig } 472d4100351SChristoph Hellwig 473d4100351SChristoph Hellwig args->next_sector = zone->start + zone->len; 474d4100351SChristoph Hellwig return args->orig_cb(zone, args->zone_idx++, args->orig_data); 475d4100351SChristoph Hellwig } 476d4100351SChristoph Hellwig EXPORT_SYMBOL_GPL(dm_report_zones_cb); 477d4100351SChristoph Hellwig 478d4100351SChristoph Hellwig static int dm_blk_report_zones(struct gendisk *disk, sector_t sector, 479d4100351SChristoph Hellwig unsigned int nr_zones, report_zones_cb cb, void *data) 480d4100351SChristoph Hellwig { 481e76239a3SChristoph Hellwig struct mapped_device *md = disk->private_data; 482e76239a3SChristoph Hellwig struct dm_table *map; 483e76239a3SChristoph Hellwig int srcu_idx, ret; 484d4100351SChristoph Hellwig struct dm_report_zones_args args = { 485d4100351SChristoph Hellwig .next_sector = sector, 486d4100351SChristoph Hellwig .orig_data = data, 487d4100351SChristoph Hellwig .orig_cb = cb, 488d4100351SChristoph Hellwig }; 489e76239a3SChristoph Hellwig 490e76239a3SChristoph Hellwig if (dm_suspended_md(md)) 491e76239a3SChristoph Hellwig return -EAGAIN; 492e76239a3SChristoph Hellwig 493e76239a3SChristoph Hellwig map = dm_get_live_table(md, &srcu_idx); 494e76239a3SChristoph Hellwig if (!map) 495e76239a3SChristoph Hellwig return -EIO; 496e76239a3SChristoph Hellwig 497d4100351SChristoph Hellwig do { 498d4100351SChristoph Hellwig struct dm_target *tgt; 499d4100351SChristoph Hellwig 500d4100351SChristoph Hellwig tgt = dm_table_find_target(map, args.next_sector); 501d4100351SChristoph Hellwig if (WARN_ON_ONCE(!tgt->type->report_zones)) { 502e76239a3SChristoph Hellwig ret = -EIO; 503e76239a3SChristoph Hellwig goto out; 504e76239a3SChristoph Hellwig } 505e76239a3SChristoph Hellwig 506d4100351SChristoph Hellwig args.tgt = tgt; 507a9cb9f41SJohannes Thumshirn ret = tgt->type->report_zones(tgt, &args, 508a9cb9f41SJohannes Thumshirn nr_zones - args.zone_idx); 509d4100351SChristoph Hellwig if (ret < 0) 510e76239a3SChristoph Hellwig goto out; 511d4100351SChristoph Hellwig } while (args.zone_idx < nr_zones && 512d4100351SChristoph Hellwig args.next_sector < get_capacity(disk)); 513e76239a3SChristoph Hellwig 514d4100351SChristoph Hellwig ret = args.zone_idx; 515e76239a3SChristoph Hellwig out: 516e76239a3SChristoph Hellwig dm_put_live_table(md, srcu_idx); 517e76239a3SChristoph Hellwig return ret; 518e76239a3SChristoph Hellwig } 519d4100351SChristoph Hellwig #else 520d4100351SChristoph Hellwig #define dm_blk_report_zones NULL 521d4100351SChristoph Hellwig #endif /* CONFIG_BLK_DEV_ZONED */ 522e76239a3SChristoph Hellwig 523971888c4SMike Snitzer static int dm_prepare_ioctl(struct mapped_device *md, int *srcu_idx, 5245bd5e8d8SMike Snitzer struct block_device **bdev) 525971888c4SMike Snitzer __acquires(md->io_barrier) 526aa129a22SMilan Broz { 52766482026SMike Snitzer struct dm_target *tgt; 5286c182cd8SHannes Reinecke struct dm_table *map; 529971888c4SMike Snitzer int r; 530aa129a22SMilan Broz 5316c182cd8SHannes Reinecke retry: 532e56f81e0SChristoph Hellwig r = -ENOTTY; 533971888c4SMike Snitzer map = dm_get_live_table(md, srcu_idx); 534aa129a22SMilan Broz if (!map || !dm_table_get_size(map)) 535971888c4SMike Snitzer return r; 536aa129a22SMilan Broz 537aa129a22SMilan Broz /* We only support devices that have a single target */ 538aa129a22SMilan Broz if (dm_table_get_num_targets(map) != 1) 539971888c4SMike Snitzer return r; 540aa129a22SMilan Broz 54166482026SMike Snitzer tgt = dm_table_get_target(map, 0); 54266482026SMike Snitzer if (!tgt->type->prepare_ioctl) 543e56f81e0SChristoph Hellwig return r; 544aa129a22SMilan Broz 545971888c4SMike Snitzer if (dm_suspended_md(md)) 546971888c4SMike Snitzer return -EAGAIN; 547971888c4SMike Snitzer 5485bd5e8d8SMike Snitzer r = tgt->type->prepare_ioctl(tgt, bdev); 5495bbbfdf6SJunichi Nomura if (r == -ENOTCONN && !fatal_signal_pending(current)) { 550971888c4SMike Snitzer dm_put_live_table(md, *srcu_idx); 5516c182cd8SHannes Reinecke msleep(10); 5526c182cd8SHannes Reinecke goto retry; 5536c182cd8SHannes Reinecke } 554971888c4SMike Snitzer 555e56f81e0SChristoph Hellwig return r; 556e56f81e0SChristoph Hellwig } 5576c182cd8SHannes Reinecke 558971888c4SMike Snitzer static void dm_unprepare_ioctl(struct mapped_device *md, int srcu_idx) 559971888c4SMike Snitzer __releases(md->io_barrier) 560971888c4SMike Snitzer { 561971888c4SMike Snitzer dm_put_live_table(md, srcu_idx); 562971888c4SMike Snitzer } 563971888c4SMike Snitzer 564e56f81e0SChristoph Hellwig static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode, 565e56f81e0SChristoph Hellwig unsigned int cmd, unsigned long arg) 566e56f81e0SChristoph Hellwig { 567e56f81e0SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 568971888c4SMike Snitzer int r, srcu_idx; 569e56f81e0SChristoph Hellwig 5705bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 571e56f81e0SChristoph Hellwig if (r < 0) 572971888c4SMike Snitzer goto out; 573e56f81e0SChristoph Hellwig 574e56f81e0SChristoph Hellwig if (r > 0) { 575e56f81e0SChristoph Hellwig /* 576e980f623SChristoph Hellwig * Target determined this ioctl is being issued against a 577e980f623SChristoph Hellwig * subset of the parent bdev; require extra privileges. 578e56f81e0SChristoph Hellwig */ 579e980f623SChristoph Hellwig if (!capable(CAP_SYS_RAWIO)) { 580e980f623SChristoph Hellwig DMWARN_LIMIT( 581e980f623SChristoph Hellwig "%s: sending ioctl %x to DM device without required privilege.", 582e980f623SChristoph Hellwig current->comm, cmd); 583e980f623SChristoph Hellwig r = -ENOIOCTLCMD; 584e56f81e0SChristoph Hellwig goto out; 585e56f81e0SChristoph Hellwig } 586e980f623SChristoph Hellwig } 587e56f81e0SChristoph Hellwig 58866482026SMike Snitzer r = __blkdev_driver_ioctl(bdev, mode, cmd, arg); 589e56f81e0SChristoph Hellwig out: 590971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 591aa129a22SMilan Broz return r; 592aa129a22SMilan Broz } 593aa129a22SMilan Broz 594978e51baSMike Snitzer static void start_io_acct(struct dm_io *io); 595978e51baSMike Snitzer 596978e51baSMike Snitzer static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio) 5971da177e4SLinus Torvalds { 59864f52b0eSMike Snitzer struct dm_io *io; 59964f52b0eSMike Snitzer struct dm_target_io *tio; 60064f52b0eSMike Snitzer struct bio *clone; 60164f52b0eSMike Snitzer 6026f1c819cSKent Overstreet clone = bio_alloc_bioset(GFP_NOIO, 0, &md->io_bs); 60364f52b0eSMike Snitzer if (!clone) 60464f52b0eSMike Snitzer return NULL; 60564f52b0eSMike Snitzer 60664f52b0eSMike Snitzer tio = container_of(clone, struct dm_target_io, clone); 60764f52b0eSMike Snitzer tio->inside_dm_io = true; 60864f52b0eSMike Snitzer tio->io = NULL; 60964f52b0eSMike Snitzer 61064f52b0eSMike Snitzer io = container_of(tio, struct dm_io, tio); 61164f52b0eSMike Snitzer io->magic = DM_IO_MAGIC; 612978e51baSMike Snitzer io->status = 0; 613978e51baSMike Snitzer atomic_set(&io->io_count, 1); 614978e51baSMike Snitzer io->orig_bio = bio; 615978e51baSMike Snitzer io->md = md; 616978e51baSMike Snitzer spin_lock_init(&io->endio_lock); 617978e51baSMike Snitzer 618978e51baSMike Snitzer start_io_acct(io); 61964f52b0eSMike Snitzer 62064f52b0eSMike Snitzer return io; 6211da177e4SLinus Torvalds } 6221da177e4SLinus Torvalds 623028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io) 6241da177e4SLinus Torvalds { 62564f52b0eSMike Snitzer bio_put(&io->tio.clone); 62664f52b0eSMike Snitzer } 62764f52b0eSMike Snitzer 62864f52b0eSMike Snitzer static struct dm_target_io *alloc_tio(struct clone_info *ci, struct dm_target *ti, 62964f52b0eSMike Snitzer unsigned target_bio_nr, gfp_t gfp_mask) 63064f52b0eSMike Snitzer { 63164f52b0eSMike Snitzer struct dm_target_io *tio; 63264f52b0eSMike Snitzer 63364f52b0eSMike Snitzer if (!ci->io->tio.io) { 63464f52b0eSMike Snitzer /* the dm_target_io embedded in ci->io is available */ 63564f52b0eSMike Snitzer tio = &ci->io->tio; 63664f52b0eSMike Snitzer } else { 6376f1c819cSKent Overstreet struct bio *clone = bio_alloc_bioset(gfp_mask, 0, &ci->io->md->bs); 63864f52b0eSMike Snitzer if (!clone) 63964f52b0eSMike Snitzer return NULL; 64064f52b0eSMike Snitzer 64164f52b0eSMike Snitzer tio = container_of(clone, struct dm_target_io, clone); 64264f52b0eSMike Snitzer tio->inside_dm_io = false; 64364f52b0eSMike Snitzer } 64464f52b0eSMike Snitzer 64564f52b0eSMike Snitzer tio->magic = DM_TIO_MAGIC; 64664f52b0eSMike Snitzer tio->io = ci->io; 64764f52b0eSMike Snitzer tio->ti = ti; 64864f52b0eSMike Snitzer tio->target_bio_nr = target_bio_nr; 64964f52b0eSMike Snitzer 65064f52b0eSMike Snitzer return tio; 6511da177e4SLinus Torvalds } 6521da177e4SLinus Torvalds 653cfae7529SMike Snitzer static void free_tio(struct dm_target_io *tio) 6541da177e4SLinus Torvalds { 65564f52b0eSMike Snitzer if (tio->inside_dm_io) 65664f52b0eSMike Snitzer return; 657dba14160SMikulas Patocka bio_put(&tio->clone); 6581da177e4SLinus Torvalds } 6591da177e4SLinus Torvalds 660087615bfSGabriel Krisman Bertazi u64 dm_start_time_ns_from_clone(struct bio *bio) 661087615bfSGabriel Krisman Bertazi { 662087615bfSGabriel Krisman Bertazi struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); 663087615bfSGabriel Krisman Bertazi struct dm_io *io = tio->io; 664087615bfSGabriel Krisman Bertazi 665087615bfSGabriel Krisman Bertazi return jiffies_to_nsecs(io->start_time); 666087615bfSGabriel Krisman Bertazi } 667087615bfSGabriel Krisman Bertazi EXPORT_SYMBOL_GPL(dm_start_time_ns_from_clone); 668087615bfSGabriel Krisman Bertazi 6693eaf840eSJun'ichi "Nick" Nomura static void start_io_acct(struct dm_io *io) 6703eaf840eSJun'ichi "Nick" Nomura { 6713eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 672745dc570SMike Snitzer struct bio *bio = io->orig_bio; 6733eaf840eSJun'ichi "Nick" Nomura 67486240d5bSChristoph Hellwig io->start_time = bio_start_io_acct(bio); 675fd2ed4d2SMikulas Patocka if (unlikely(dm_stats_used(&md->stats))) 676528ec5abSMike Christie dm_stats_account_io(&md->stats, bio_data_dir(bio), 677528ec5abSMike Christie bio->bi_iter.bi_sector, bio_sectors(bio), 678528ec5abSMike Christie false, 0, &io->stats_aux); 6793eaf840eSJun'ichi "Nick" Nomura } 6803eaf840eSJun'ichi "Nick" Nomura 681d221d2e7SMikulas Patocka static void end_io_acct(struct dm_io *io) 6823eaf840eSJun'ichi "Nick" Nomura { 6833eaf840eSJun'ichi "Nick" Nomura struct mapped_device *md = io->md; 684745dc570SMike Snitzer struct bio *bio = io->orig_bio; 6853eaf840eSJun'ichi "Nick" Nomura unsigned long duration = jiffies - io->start_time; 6863eaf840eSJun'ichi "Nick" Nomura 68786240d5bSChristoph Hellwig bio_end_io_acct(bio, io->start_time); 6883eaf840eSJun'ichi "Nick" Nomura 689fd2ed4d2SMikulas Patocka if (unlikely(dm_stats_used(&md->stats))) 690528ec5abSMike Christie dm_stats_account_io(&md->stats, bio_data_dir(bio), 691528ec5abSMike Christie bio->bi_iter.bi_sector, bio_sectors(bio), 692528ec5abSMike Christie true, duration, &io->stats_aux); 693fd2ed4d2SMikulas Patocka 694d221d2e7SMikulas Patocka /* nudge anyone waiting on suspend queue */ 695645efa84SMikulas Patocka if (unlikely(wq_has_sleeper(&md->wait))) 696d221d2e7SMikulas Patocka wake_up(&md->wait); 6973eaf840eSJun'ichi "Nick" Nomura } 6983eaf840eSJun'ichi "Nick" Nomura 6991da177e4SLinus Torvalds /* 7001da177e4SLinus Torvalds * Add the bio to the list of deferred io. 7011da177e4SLinus Torvalds */ 70292c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio) 7031da177e4SLinus Torvalds { 70405447420SKiyoshi Ueda unsigned long flags; 7051da177e4SLinus Torvalds 70605447420SKiyoshi Ueda spin_lock_irqsave(&md->deferred_lock, flags); 7071da177e4SLinus Torvalds bio_list_add(&md->deferred, bio); 70805447420SKiyoshi Ueda spin_unlock_irqrestore(&md->deferred_lock, flags); 70992c63902SMikulas Patocka queue_work(md->wq, &md->work); 7101da177e4SLinus Torvalds } 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds /* 7131da177e4SLinus Torvalds * Everyone (including functions in this file), should use this 7141da177e4SLinus Torvalds * function to access the md->map field, and make sure they call 71583d5e5b0SMikulas Patocka * dm_put_live_table() when finished. 7161da177e4SLinus Torvalds */ 71783d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier) 7181da177e4SLinus Torvalds { 71983d5e5b0SMikulas Patocka *srcu_idx = srcu_read_lock(&md->io_barrier); 7201da177e4SLinus Torvalds 72183d5e5b0SMikulas Patocka return srcu_dereference(md->map, &md->io_barrier); 72283d5e5b0SMikulas Patocka } 7231da177e4SLinus Torvalds 72483d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier) 72583d5e5b0SMikulas Patocka { 72683d5e5b0SMikulas Patocka srcu_read_unlock(&md->io_barrier, srcu_idx); 72783d5e5b0SMikulas Patocka } 72883d5e5b0SMikulas Patocka 72983d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md) 73083d5e5b0SMikulas Patocka { 73183d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 73283d5e5b0SMikulas Patocka synchronize_rcu_expedited(); 73383d5e5b0SMikulas Patocka } 73483d5e5b0SMikulas Patocka 73583d5e5b0SMikulas Patocka /* 73683d5e5b0SMikulas Patocka * A fast alternative to dm_get_live_table/dm_put_live_table. 73783d5e5b0SMikulas Patocka * The caller must not block between these two functions. 73883d5e5b0SMikulas Patocka */ 73983d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU) 74083d5e5b0SMikulas Patocka { 74183d5e5b0SMikulas Patocka rcu_read_lock(); 74283d5e5b0SMikulas Patocka return rcu_dereference(md->map); 74383d5e5b0SMikulas Patocka } 74483d5e5b0SMikulas Patocka 74583d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU) 74683d5e5b0SMikulas Patocka { 74783d5e5b0SMikulas Patocka rcu_read_unlock(); 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds 750971888c4SMike Snitzer static char *_dm_claim_ptr = "I belong to device-mapper"; 751971888c4SMike Snitzer 7523ac51e74SDarrick J. Wong /* 75386f1152bSBenjamin Marzinski * Open a table device so we can use it as a map destination. 75486f1152bSBenjamin Marzinski */ 75586f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev, 75686f1152bSBenjamin Marzinski struct mapped_device *md) 75786f1152bSBenjamin Marzinski { 75886f1152bSBenjamin Marzinski struct block_device *bdev; 75986f1152bSBenjamin Marzinski 76086f1152bSBenjamin Marzinski int r; 76186f1152bSBenjamin Marzinski 76286f1152bSBenjamin Marzinski BUG_ON(td->dm_dev.bdev); 76386f1152bSBenjamin Marzinski 764519049afSMike Snitzer bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr); 76586f1152bSBenjamin Marzinski if (IS_ERR(bdev)) 76686f1152bSBenjamin Marzinski return PTR_ERR(bdev); 76786f1152bSBenjamin Marzinski 76886f1152bSBenjamin Marzinski r = bd_link_disk_holder(bdev, dm_disk(md)); 76986f1152bSBenjamin Marzinski if (r) { 77086f1152bSBenjamin Marzinski blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL); 77186f1152bSBenjamin Marzinski return r; 77286f1152bSBenjamin Marzinski } 77386f1152bSBenjamin Marzinski 77486f1152bSBenjamin Marzinski td->dm_dev.bdev = bdev; 775817bf402SDan Williams td->dm_dev.dax_dev = dax_get_by_host(bdev->bd_disk->disk_name); 77686f1152bSBenjamin Marzinski return 0; 77786f1152bSBenjamin Marzinski } 77886f1152bSBenjamin Marzinski 77986f1152bSBenjamin Marzinski /* 78086f1152bSBenjamin Marzinski * Close a table device that we've been using. 78186f1152bSBenjamin Marzinski */ 78286f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md) 78386f1152bSBenjamin Marzinski { 78486f1152bSBenjamin Marzinski if (!td->dm_dev.bdev) 78586f1152bSBenjamin Marzinski return; 78686f1152bSBenjamin Marzinski 78786f1152bSBenjamin Marzinski bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md)); 78886f1152bSBenjamin Marzinski blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL); 789817bf402SDan Williams put_dax(td->dm_dev.dax_dev); 79086f1152bSBenjamin Marzinski td->dm_dev.bdev = NULL; 791817bf402SDan Williams td->dm_dev.dax_dev = NULL; 79286f1152bSBenjamin Marzinski } 79386f1152bSBenjamin Marzinski 79486f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev, 7958454fca4SSheetal Singala fmode_t mode) 7968454fca4SSheetal Singala { 79786f1152bSBenjamin Marzinski struct table_device *td; 79886f1152bSBenjamin Marzinski 79986f1152bSBenjamin Marzinski list_for_each_entry(td, l, list) 80086f1152bSBenjamin Marzinski if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode) 80186f1152bSBenjamin Marzinski return td; 80286f1152bSBenjamin Marzinski 80386f1152bSBenjamin Marzinski return NULL; 80486f1152bSBenjamin Marzinski } 80586f1152bSBenjamin Marzinski 80686f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode, 8078454fca4SSheetal Singala struct dm_dev **result) 8088454fca4SSheetal Singala { 80986f1152bSBenjamin Marzinski int r; 81086f1152bSBenjamin Marzinski struct table_device *td; 81186f1152bSBenjamin Marzinski 81286f1152bSBenjamin Marzinski mutex_lock(&md->table_devices_lock); 81386f1152bSBenjamin Marzinski td = find_table_device(&md->table_devices, dev, mode); 81486f1152bSBenjamin Marzinski if (!td) { 815115485e8SMike Snitzer td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id); 81686f1152bSBenjamin Marzinski if (!td) { 81786f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 81886f1152bSBenjamin Marzinski return -ENOMEM; 81986f1152bSBenjamin Marzinski } 82086f1152bSBenjamin Marzinski 82186f1152bSBenjamin Marzinski td->dm_dev.mode = mode; 82286f1152bSBenjamin Marzinski td->dm_dev.bdev = NULL; 82386f1152bSBenjamin Marzinski 82486f1152bSBenjamin Marzinski if ((r = open_table_device(td, dev, md))) { 82586f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 82686f1152bSBenjamin Marzinski kfree(td); 82786f1152bSBenjamin Marzinski return r; 82886f1152bSBenjamin Marzinski } 82986f1152bSBenjamin Marzinski 83086f1152bSBenjamin Marzinski format_dev_t(td->dm_dev.name, dev); 83186f1152bSBenjamin Marzinski 832b0b4d7c6SElena Reshetova refcount_set(&td->count, 1); 83386f1152bSBenjamin Marzinski list_add(&td->list, &md->table_devices); 834b0b4d7c6SElena Reshetova } else { 835b0b4d7c6SElena Reshetova refcount_inc(&td->count); 83686f1152bSBenjamin Marzinski } 83786f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 83886f1152bSBenjamin Marzinski 83986f1152bSBenjamin Marzinski *result = &td->dm_dev; 84086f1152bSBenjamin Marzinski return 0; 84186f1152bSBenjamin Marzinski } 84286f1152bSBenjamin Marzinski EXPORT_SYMBOL_GPL(dm_get_table_device); 84386f1152bSBenjamin Marzinski 84486f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d) 84586f1152bSBenjamin Marzinski { 84686f1152bSBenjamin Marzinski struct table_device *td = container_of(d, struct table_device, dm_dev); 84786f1152bSBenjamin Marzinski 84886f1152bSBenjamin Marzinski mutex_lock(&md->table_devices_lock); 849b0b4d7c6SElena Reshetova if (refcount_dec_and_test(&td->count)) { 85086f1152bSBenjamin Marzinski close_table_device(td, md); 85186f1152bSBenjamin Marzinski list_del(&td->list); 85286f1152bSBenjamin Marzinski kfree(td); 85386f1152bSBenjamin Marzinski } 85486f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 85586f1152bSBenjamin Marzinski } 85686f1152bSBenjamin Marzinski EXPORT_SYMBOL(dm_put_table_device); 85786f1152bSBenjamin Marzinski 85886f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices) 85986f1152bSBenjamin Marzinski { 86086f1152bSBenjamin Marzinski struct list_head *tmp, *next; 86186f1152bSBenjamin Marzinski 86286f1152bSBenjamin Marzinski list_for_each_safe(tmp, next, devices) { 86386f1152bSBenjamin Marzinski struct table_device *td = list_entry(tmp, struct table_device, list); 86486f1152bSBenjamin Marzinski 86586f1152bSBenjamin Marzinski DMWARN("dm_destroy: %s still exists with %d references", 866b0b4d7c6SElena Reshetova td->dm_dev.name, refcount_read(&td->count)); 86786f1152bSBenjamin Marzinski kfree(td); 86886f1152bSBenjamin Marzinski } 86986f1152bSBenjamin Marzinski } 87086f1152bSBenjamin Marzinski 87186f1152bSBenjamin Marzinski /* 8723ac51e74SDarrick J. Wong * Get the geometry associated with a dm device 8733ac51e74SDarrick J. Wong */ 8743ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) 8753ac51e74SDarrick J. Wong { 8763ac51e74SDarrick J. Wong *geo = md->geometry; 8773ac51e74SDarrick J. Wong 8783ac51e74SDarrick J. Wong return 0; 8793ac51e74SDarrick J. Wong } 8803ac51e74SDarrick J. Wong 8813ac51e74SDarrick J. Wong /* 8823ac51e74SDarrick J. Wong * Set the geometry of a device. 8833ac51e74SDarrick J. Wong */ 8843ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) 8853ac51e74SDarrick J. Wong { 8863ac51e74SDarrick J. Wong sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; 8873ac51e74SDarrick J. Wong 8883ac51e74SDarrick J. Wong if (geo->start > sz) { 8893ac51e74SDarrick J. Wong DMWARN("Start sector is beyond the geometry limits."); 8903ac51e74SDarrick J. Wong return -EINVAL; 8913ac51e74SDarrick J. Wong } 8923ac51e74SDarrick J. Wong 8933ac51e74SDarrick J. Wong md->geometry = *geo; 8943ac51e74SDarrick J. Wong 8953ac51e74SDarrick J. Wong return 0; 8963ac51e74SDarrick J. Wong } 8973ac51e74SDarrick J. Wong 8982e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md) 8992e93ccc1SKiyoshi Ueda { 9002e93ccc1SKiyoshi Ueda return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 9012e93ccc1SKiyoshi Ueda } 9022e93ccc1SKiyoshi Ueda 9031da177e4SLinus Torvalds /* 9041da177e4SLinus Torvalds * Decrements the number of outstanding ios that a bio has been 9051da177e4SLinus Torvalds * cloned into, completing the original io if necc. 9061da177e4SLinus Torvalds */ 9074e4cbee9SChristoph Hellwig static void dec_pending(struct dm_io *io, blk_status_t error) 9081da177e4SLinus Torvalds { 9092e93ccc1SKiyoshi Ueda unsigned long flags; 9104e4cbee9SChristoph Hellwig blk_status_t io_error; 911b35f8caaSMilan Broz struct bio *bio; 912b35f8caaSMilan Broz struct mapped_device *md = io->md; 9132e93ccc1SKiyoshi Ueda 9142e93ccc1SKiyoshi Ueda /* Push-back supersedes any I/O errors */ 915f88fb981SKiyoshi Ueda if (unlikely(error)) { 916f88fb981SKiyoshi Ueda spin_lock_irqsave(&io->endio_lock, flags); 917745dc570SMike Snitzer if (!(io->status == BLK_STS_DM_REQUEUE && __noflush_suspending(md))) 9184e4cbee9SChristoph Hellwig io->status = error; 919f88fb981SKiyoshi Ueda spin_unlock_irqrestore(&io->endio_lock, flags); 920f88fb981SKiyoshi Ueda } 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds if (atomic_dec_and_test(&io->io_count)) { 9234e4cbee9SChristoph Hellwig if (io->status == BLK_STS_DM_REQUEUE) { 9242e93ccc1SKiyoshi Ueda /* 9252e93ccc1SKiyoshi Ueda * Target requested pushing back the I/O. 9262e93ccc1SKiyoshi Ueda */ 927022c2611SMikulas Patocka spin_lock_irqsave(&md->deferred_lock, flags); 9286a8736d1STejun Heo if (__noflush_suspending(md)) 929745dc570SMike Snitzer /* NOTE early return due to BLK_STS_DM_REQUEUE below */ 930745dc570SMike Snitzer bio_list_add_head(&md->deferred, io->orig_bio); 9316a8736d1STejun Heo else 9322e93ccc1SKiyoshi Ueda /* noflush suspend was interrupted. */ 9334e4cbee9SChristoph Hellwig io->status = BLK_STS_IOERR; 934022c2611SMikulas Patocka spin_unlock_irqrestore(&md->deferred_lock, flags); 9352e93ccc1SKiyoshi Ueda } 9362e93ccc1SKiyoshi Ueda 9374e4cbee9SChristoph Hellwig io_error = io->status; 938745dc570SMike Snitzer bio = io->orig_bio; 939af7e466aSMikulas Patocka end_io_acct(io); 940a97f925aSMikulas Patocka free_io(md, io); 9411da177e4SLinus Torvalds 9424e4cbee9SChristoph Hellwig if (io_error == BLK_STS_DM_REQUEUE) 9436a8736d1STejun Heo return; 9446a8736d1STejun Heo 9451eff9d32SJens Axboe if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) { 9461da177e4SLinus Torvalds /* 9476a8736d1STejun Heo * Preflush done for flush with data, reissue 94828a8f0d3SMike Christie * without REQ_PREFLUSH. 9491da177e4SLinus Torvalds */ 9501eff9d32SJens Axboe bio->bi_opf &= ~REQ_PREFLUSH; 9516a8736d1STejun Heo queue_io(md, bio); 952b35f8caaSMilan Broz } else { 953b372d360SMike Snitzer /* done with normal IO or empty flush */ 9548dd601faSNeilBrown if (io_error) 9554e4cbee9SChristoph Hellwig bio->bi_status = io_error; 9564246a0b6SChristoph Hellwig bio_endio(bio); 9572e93ccc1SKiyoshi Ueda } 9581da177e4SLinus Torvalds } 959af7e466aSMikulas Patocka } 9601da177e4SLinus Torvalds 961bcb44433SMike Snitzer void disable_discard(struct mapped_device *md) 962bcb44433SMike Snitzer { 963bcb44433SMike Snitzer struct queue_limits *limits = dm_get_queue_limits(md); 964bcb44433SMike Snitzer 965bcb44433SMike Snitzer /* device doesn't really support DISCARD, disable it */ 966bcb44433SMike Snitzer limits->max_discard_sectors = 0; 967bcb44433SMike Snitzer blk_queue_flag_clear(QUEUE_FLAG_DISCARD, md->queue); 968bcb44433SMike Snitzer } 969bcb44433SMike Snitzer 9704cc96131SMike Snitzer void disable_write_same(struct mapped_device *md) 9717eee4ae2SMike Snitzer { 9727eee4ae2SMike Snitzer struct queue_limits *limits = dm_get_queue_limits(md); 9737eee4ae2SMike Snitzer 9747eee4ae2SMike Snitzer /* device doesn't really support WRITE SAME, disable it */ 9757eee4ae2SMike Snitzer limits->max_write_same_sectors = 0; 9767eee4ae2SMike Snitzer } 9777eee4ae2SMike Snitzer 978ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md) 979ac62d620SChristoph Hellwig { 980ac62d620SChristoph Hellwig struct queue_limits *limits = dm_get_queue_limits(md); 981ac62d620SChristoph Hellwig 982ac62d620SChristoph Hellwig /* device doesn't really support WRITE ZEROES, disable it */ 983ac62d620SChristoph Hellwig limits->max_write_zeroes_sectors = 0; 984ac62d620SChristoph Hellwig } 985ac62d620SChristoph Hellwig 9864246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio) 9871da177e4SLinus Torvalds { 9884e4cbee9SChristoph Hellwig blk_status_t error = bio->bi_status; 989bfc6d41cSMikulas Patocka struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); 990b35f8caaSMilan Broz struct dm_io *io = tio->io; 9919faf400fSStefan Bader struct mapped_device *md = tio->io->md; 9921da177e4SLinus Torvalds dm_endio_fn endio = tio->ti->type->end_io; 993415c79e1SJohannes Thumshirn struct bio *orig_bio = io->orig_bio; 9941da177e4SLinus Torvalds 995978e51baSMike Snitzer if (unlikely(error == BLK_STS_TARGET) && md->type != DM_TYPE_NVME_BIO_BASED) { 996bcb44433SMike Snitzer if (bio_op(bio) == REQ_OP_DISCARD && 997bcb44433SMike Snitzer !bio->bi_disk->queue->limits.max_discard_sectors) 998bcb44433SMike Snitzer disable_discard(md); 999bcb44433SMike Snitzer else if (bio_op(bio) == REQ_OP_WRITE_SAME && 100074d46992SChristoph Hellwig !bio->bi_disk->queue->limits.max_write_same_sectors) 10017eee4ae2SMike Snitzer disable_write_same(md); 1002bcb44433SMike Snitzer else if (bio_op(bio) == REQ_OP_WRITE_ZEROES && 100374d46992SChristoph Hellwig !bio->bi_disk->queue->limits.max_write_zeroes_sectors) 1004ac62d620SChristoph Hellwig disable_write_zeroes(md); 1005ac62d620SChristoph Hellwig } 10067eee4ae2SMike Snitzer 1007415c79e1SJohannes Thumshirn /* 1008415c79e1SJohannes Thumshirn * For zone-append bios get offset in zone of the written 1009415c79e1SJohannes Thumshirn * sector and add that to the original bio sector pos. 1010415c79e1SJohannes Thumshirn */ 1011415c79e1SJohannes Thumshirn if (bio_op(orig_bio) == REQ_OP_ZONE_APPEND) { 1012415c79e1SJohannes Thumshirn sector_t written_sector = bio->bi_iter.bi_sector; 1013415c79e1SJohannes Thumshirn struct request_queue *q = orig_bio->bi_disk->queue; 1014415c79e1SJohannes Thumshirn u64 mask = (u64)blk_queue_zone_sectors(q) - 1; 1015415c79e1SJohannes Thumshirn 1016415c79e1SJohannes Thumshirn orig_bio->bi_iter.bi_sector += written_sector & mask; 1017415c79e1SJohannes Thumshirn } 1018415c79e1SJohannes Thumshirn 10191be56909SChristoph Hellwig if (endio) { 10204e4cbee9SChristoph Hellwig int r = endio(tio->ti, bio, &error); 10211be56909SChristoph Hellwig switch (r) { 10221be56909SChristoph Hellwig case DM_ENDIO_REQUEUE: 10234e4cbee9SChristoph Hellwig error = BLK_STS_DM_REQUEUE; 1024df561f66SGustavo A. R. Silva fallthrough; 10251be56909SChristoph Hellwig case DM_ENDIO_DONE: 10261be56909SChristoph Hellwig break; 10271be56909SChristoph Hellwig case DM_ENDIO_INCOMPLETE: 10281be56909SChristoph Hellwig /* The target will handle the io */ 10291be56909SChristoph Hellwig return; 10301be56909SChristoph Hellwig default: 10311be56909SChristoph Hellwig DMWARN("unimplemented target endio return value: %d", r); 10321be56909SChristoph Hellwig BUG(); 10331be56909SChristoph Hellwig } 10341be56909SChristoph Hellwig } 10351be56909SChristoph Hellwig 1036cfae7529SMike Snitzer free_tio(tio); 1037b35f8caaSMilan Broz dec_pending(io, error); 10381da177e4SLinus Torvalds } 10391da177e4SLinus Torvalds 104078d8e58aSMike Snitzer /* 104156a67df7SMike Snitzer * Return maximum size of I/O possible at the supplied sector up to the current 104256a67df7SMike Snitzer * target boundary. 104356a67df7SMike Snitzer */ 104456a67df7SMike Snitzer static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti) 10451da177e4SLinus Torvalds { 104656a67df7SMike Snitzer sector_t target_offset = dm_target_offset(ti, sector); 104756a67df7SMike Snitzer 104856a67df7SMike Snitzer return ti->len - target_offset; 104956a67df7SMike Snitzer } 105056a67df7SMike Snitzer 105156a67df7SMike Snitzer static sector_t max_io_len(sector_t sector, struct dm_target *ti) 105256a67df7SMike Snitzer { 105356a67df7SMike Snitzer sector_t len = max_io_len_target_boundary(sector, ti); 1054542f9038SMike Snitzer sector_t offset, max_len; 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds /* 10571da177e4SLinus Torvalds * Does the target need to split even further? 10581da177e4SLinus Torvalds */ 1059542f9038SMike Snitzer if (ti->max_io_len) { 1060542f9038SMike Snitzer offset = dm_target_offset(ti, sector); 1061542f9038SMike Snitzer if (unlikely(ti->max_io_len & (ti->max_io_len - 1))) 1062542f9038SMike Snitzer max_len = sector_div(offset, ti->max_io_len); 1063542f9038SMike Snitzer else 1064542f9038SMike Snitzer max_len = offset & (ti->max_io_len - 1); 1065542f9038SMike Snitzer max_len = ti->max_io_len - max_len; 1066542f9038SMike Snitzer 1067542f9038SMike Snitzer if (len > max_len) 1068542f9038SMike Snitzer len = max_len; 10691da177e4SLinus Torvalds } 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds return len; 10721da177e4SLinus Torvalds } 10731da177e4SLinus Torvalds 1074542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len) 1075542f9038SMike Snitzer { 1076542f9038SMike Snitzer if (len > UINT_MAX) { 1077542f9038SMike Snitzer DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)", 1078542f9038SMike Snitzer (unsigned long long)len, UINT_MAX); 1079542f9038SMike Snitzer ti->error = "Maximum size of target IO is too large"; 1080542f9038SMike Snitzer return -EINVAL; 1081542f9038SMike Snitzer } 1082542f9038SMike Snitzer 108375ae1936SMikulas Patocka ti->max_io_len = (uint32_t) len; 1084542f9038SMike Snitzer 1085542f9038SMike Snitzer return 0; 1086542f9038SMike Snitzer } 1087542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len); 1088542f9038SMike Snitzer 1089f26c5719SDan Williams static struct dm_target *dm_dax_get_live_target(struct mapped_device *md, 1090f26c5719SDan Williams sector_t sector, int *srcu_idx) 10913d97c829SMike Snitzer __acquires(md->io_barrier) 1092545ed20eSToshi Kani { 1093545ed20eSToshi Kani struct dm_table *map; 1094545ed20eSToshi Kani struct dm_target *ti; 1095545ed20eSToshi Kani 1096f26c5719SDan Williams map = dm_get_live_table(md, srcu_idx); 1097545ed20eSToshi Kani if (!map) 1098f26c5719SDan Williams return NULL; 1099545ed20eSToshi Kani 1100545ed20eSToshi Kani ti = dm_table_find_target(map, sector); 1101123d87d5SMikulas Patocka if (!ti) 1102f26c5719SDan Williams return NULL; 1103f26c5719SDan Williams 1104f26c5719SDan Williams return ti; 1105f26c5719SDan Williams } 1106f26c5719SDan Williams 1107f26c5719SDan Williams static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, 1108f26c5719SDan Williams long nr_pages, void **kaddr, pfn_t *pfn) 1109f26c5719SDan Williams { 1110f26c5719SDan Williams struct mapped_device *md = dax_get_private(dax_dev); 1111f26c5719SDan Williams sector_t sector = pgoff * PAGE_SECTORS; 1112f26c5719SDan Williams struct dm_target *ti; 1113f26c5719SDan Williams long len, ret = -EIO; 1114f26c5719SDan Williams int srcu_idx; 1115f26c5719SDan Williams 1116f26c5719SDan Williams ti = dm_dax_get_live_target(md, sector, &srcu_idx); 1117f26c5719SDan Williams 1118f26c5719SDan Williams if (!ti) 1119545ed20eSToshi Kani goto out; 1120f26c5719SDan Williams if (!ti->type->direct_access) 1121f26c5719SDan Williams goto out; 1122f26c5719SDan Williams len = max_io_len(sector, ti) / PAGE_SECTORS; 1123f26c5719SDan Williams if (len < 1) 1124f26c5719SDan Williams goto out; 1125f26c5719SDan Williams nr_pages = min(len, nr_pages); 1126817bf402SDan Williams ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn); 1127817bf402SDan Williams 1128545ed20eSToshi Kani out: 1129545ed20eSToshi Kani dm_put_live_table(md, srcu_idx); 1130f26c5719SDan Williams 1131f26c5719SDan Williams return ret; 1132545ed20eSToshi Kani } 1133545ed20eSToshi Kani 11347bf7eac8SDan Williams static bool dm_dax_supported(struct dax_device *dax_dev, struct block_device *bdev, 11357bf7eac8SDan Williams int blocksize, sector_t start, sector_t len) 11367bf7eac8SDan Williams { 11377bf7eac8SDan Williams struct mapped_device *md = dax_get_private(dax_dev); 11387bf7eac8SDan Williams struct dm_table *map; 11397bf7eac8SDan Williams int srcu_idx; 11407bf7eac8SDan Williams bool ret; 11417bf7eac8SDan Williams 11427bf7eac8SDan Williams map = dm_get_live_table(md, &srcu_idx); 11437bf7eac8SDan Williams if (!map) 11447bf7eac8SDan Williams return false; 11457bf7eac8SDan Williams 11462e9ee095SPankaj Gupta ret = dm_table_supports_dax(map, device_supports_dax, &blocksize); 11477bf7eac8SDan Williams 11487bf7eac8SDan Williams dm_put_live_table(md, srcu_idx); 11497bf7eac8SDan Williams 11507bf7eac8SDan Williams return ret; 11517bf7eac8SDan Williams } 11527bf7eac8SDan Williams 11537e026c8cSDan Williams static size_t dm_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, 11547e026c8cSDan Williams void *addr, size_t bytes, struct iov_iter *i) 11557e026c8cSDan Williams { 11567e026c8cSDan Williams struct mapped_device *md = dax_get_private(dax_dev); 11577e026c8cSDan Williams sector_t sector = pgoff * PAGE_SECTORS; 11587e026c8cSDan Williams struct dm_target *ti; 11597e026c8cSDan Williams long ret = 0; 11607e026c8cSDan Williams int srcu_idx; 11617e026c8cSDan Williams 11627e026c8cSDan Williams ti = dm_dax_get_live_target(md, sector, &srcu_idx); 11637e026c8cSDan Williams 11647e026c8cSDan Williams if (!ti) 11657e026c8cSDan Williams goto out; 11667e026c8cSDan Williams if (!ti->type->dax_copy_from_iter) { 11677e026c8cSDan Williams ret = copy_from_iter(addr, bytes, i); 11687e026c8cSDan Williams goto out; 11697e026c8cSDan Williams } 11707e026c8cSDan Williams ret = ti->type->dax_copy_from_iter(ti, pgoff, addr, bytes, i); 11717e026c8cSDan Williams out: 11727e026c8cSDan Williams dm_put_live_table(md, srcu_idx); 11737e026c8cSDan Williams 11747e026c8cSDan Williams return ret; 11757e026c8cSDan Williams } 11767e026c8cSDan Williams 1177b3a9a0c3SDan Williams static size_t dm_dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, 1178b3a9a0c3SDan Williams void *addr, size_t bytes, struct iov_iter *i) 1179b3a9a0c3SDan Williams { 1180b3a9a0c3SDan Williams struct mapped_device *md = dax_get_private(dax_dev); 1181b3a9a0c3SDan Williams sector_t sector = pgoff * PAGE_SECTORS; 1182b3a9a0c3SDan Williams struct dm_target *ti; 1183b3a9a0c3SDan Williams long ret = 0; 1184b3a9a0c3SDan Williams int srcu_idx; 1185b3a9a0c3SDan Williams 1186b3a9a0c3SDan Williams ti = dm_dax_get_live_target(md, sector, &srcu_idx); 1187b3a9a0c3SDan Williams 1188b3a9a0c3SDan Williams if (!ti) 1189b3a9a0c3SDan Williams goto out; 1190b3a9a0c3SDan Williams if (!ti->type->dax_copy_to_iter) { 1191b3a9a0c3SDan Williams ret = copy_to_iter(addr, bytes, i); 1192b3a9a0c3SDan Williams goto out; 1193b3a9a0c3SDan Williams } 1194b3a9a0c3SDan Williams ret = ti->type->dax_copy_to_iter(ti, pgoff, addr, bytes, i); 1195b3a9a0c3SDan Williams out: 1196b3a9a0c3SDan Williams dm_put_live_table(md, srcu_idx); 1197b3a9a0c3SDan Williams 1198b3a9a0c3SDan Williams return ret; 1199b3a9a0c3SDan Williams } 1200b3a9a0c3SDan Williams 1201cdf6cdcdSVivek Goyal static int dm_dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff, 1202cdf6cdcdSVivek Goyal size_t nr_pages) 1203cdf6cdcdSVivek Goyal { 1204cdf6cdcdSVivek Goyal struct mapped_device *md = dax_get_private(dax_dev); 1205cdf6cdcdSVivek Goyal sector_t sector = pgoff * PAGE_SECTORS; 1206cdf6cdcdSVivek Goyal struct dm_target *ti; 1207cdf6cdcdSVivek Goyal int ret = -EIO; 1208cdf6cdcdSVivek Goyal int srcu_idx; 1209cdf6cdcdSVivek Goyal 1210cdf6cdcdSVivek Goyal ti = dm_dax_get_live_target(md, sector, &srcu_idx); 1211cdf6cdcdSVivek Goyal 1212cdf6cdcdSVivek Goyal if (!ti) 1213cdf6cdcdSVivek Goyal goto out; 1214cdf6cdcdSVivek Goyal if (WARN_ON(!ti->type->dax_zero_page_range)) { 1215cdf6cdcdSVivek Goyal /* 1216cdf6cdcdSVivek Goyal * ->zero_page_range() is mandatory dax operation. If we are 1217cdf6cdcdSVivek Goyal * here, something is wrong. 1218cdf6cdcdSVivek Goyal */ 1219cdf6cdcdSVivek Goyal dm_put_live_table(md, srcu_idx); 1220cdf6cdcdSVivek Goyal goto out; 1221cdf6cdcdSVivek Goyal } 1222cdf6cdcdSVivek Goyal ret = ti->type->dax_zero_page_range(ti, pgoff, nr_pages); 1223cdf6cdcdSVivek Goyal 1224cdf6cdcdSVivek Goyal out: 1225cdf6cdcdSVivek Goyal dm_put_live_table(md, srcu_idx); 1226cdf6cdcdSVivek Goyal 1227cdf6cdcdSVivek Goyal return ret; 1228cdf6cdcdSVivek Goyal } 1229cdf6cdcdSVivek Goyal 12301dd40c3eSMikulas Patocka /* 12311dd40c3eSMikulas Patocka * A target may call dm_accept_partial_bio only from the map routine. It is 12322e2d6f7eSAjay Joshi * allowed for all bio types except REQ_PREFLUSH, REQ_OP_ZONE_RESET, 12332e2d6f7eSAjay Joshi * REQ_OP_ZONE_OPEN, REQ_OP_ZONE_CLOSE and REQ_OP_ZONE_FINISH. 12341dd40c3eSMikulas Patocka * 12351dd40c3eSMikulas Patocka * dm_accept_partial_bio informs the dm that the target only wants to process 12361dd40c3eSMikulas Patocka * additional n_sectors sectors of the bio and the rest of the data should be 12371dd40c3eSMikulas Patocka * sent in a next bio. 12381dd40c3eSMikulas Patocka * 12391dd40c3eSMikulas Patocka * A diagram that explains the arithmetics: 12401dd40c3eSMikulas Patocka * +--------------------+---------------+-------+ 12411dd40c3eSMikulas Patocka * | 1 | 2 | 3 | 12421dd40c3eSMikulas Patocka * +--------------------+---------------+-------+ 12431dd40c3eSMikulas Patocka * 12441dd40c3eSMikulas Patocka * <-------------- *tio->len_ptr ---------------> 12451dd40c3eSMikulas Patocka * <------- bi_size -------> 12461dd40c3eSMikulas Patocka * <-- n_sectors --> 12471dd40c3eSMikulas Patocka * 12481dd40c3eSMikulas Patocka * Region 1 was already iterated over with bio_advance or similar function. 12491dd40c3eSMikulas Patocka * (it may be empty if the target doesn't use bio_advance) 12501dd40c3eSMikulas Patocka * Region 2 is the remaining bio size that the target wants to process. 12511dd40c3eSMikulas Patocka * (it may be empty if region 1 is non-empty, although there is no reason 12521dd40c3eSMikulas Patocka * to make it empty) 12531dd40c3eSMikulas Patocka * The target requires that region 3 is to be sent in the next bio. 12541dd40c3eSMikulas Patocka * 12551dd40c3eSMikulas Patocka * If the target wants to receive multiple copies of the bio (via num_*bios, etc), 12561dd40c3eSMikulas Patocka * the partially processed part (the sum of regions 1+2) must be the same for all 12571dd40c3eSMikulas Patocka * copies of the bio. 12581dd40c3eSMikulas Patocka */ 12591dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors) 12601dd40c3eSMikulas Patocka { 12611dd40c3eSMikulas Patocka struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone); 12621dd40c3eSMikulas Patocka unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT; 12631eff9d32SJens Axboe BUG_ON(bio->bi_opf & REQ_PREFLUSH); 12641dd40c3eSMikulas Patocka BUG_ON(bi_size > *tio->len_ptr); 12651dd40c3eSMikulas Patocka BUG_ON(n_sectors > bi_size); 12661dd40c3eSMikulas Patocka *tio->len_ptr -= bi_size - n_sectors; 12671dd40c3eSMikulas Patocka bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT; 12681dd40c3eSMikulas Patocka } 12691dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio); 12701dd40c3eSMikulas Patocka 1271978e51baSMike Snitzer static blk_qc_t __map_bio(struct dm_target_io *tio) 12721da177e4SLinus Torvalds { 12731da177e4SLinus Torvalds int r; 12742056a782SJens Axboe sector_t sector; 1275dba14160SMikulas Patocka struct bio *clone = &tio->clone; 127664f52b0eSMike Snitzer struct dm_io *io = tio->io; 1277bd2a49b8SAlasdair G Kergon struct dm_target *ti = tio->ti; 1278978e51baSMike Snitzer blk_qc_t ret = BLK_QC_T_NONE; 12791da177e4SLinus Torvalds 12801da177e4SLinus Torvalds clone->bi_end_io = clone_endio; 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds /* 12831da177e4SLinus Torvalds * Map the clone. If r == 0 we don't need to do 12841da177e4SLinus Torvalds * anything, the target has assumed ownership of 12851da177e4SLinus Torvalds * this io. 12861da177e4SLinus Torvalds */ 128764f52b0eSMike Snitzer atomic_inc(&io->io_count); 12884f024f37SKent Overstreet sector = clone->bi_iter.bi_sector; 1289d67a5f4bSMikulas Patocka 12907de3ee57SMikulas Patocka r = ti->type->map(ti, clone); 1291846785e6SChristoph Hellwig switch (r) { 1292846785e6SChristoph Hellwig case DM_MAPIO_SUBMITTED: 1293846785e6SChristoph Hellwig break; 1294846785e6SChristoph Hellwig case DM_MAPIO_REMAPPED: 12951da177e4SLinus Torvalds /* the bio has been remapped so dispatch it */ 129674d46992SChristoph Hellwig trace_block_bio_remap(clone->bi_disk->queue, clone, 129764f52b0eSMike Snitzer bio_dev(io->orig_bio), sector); 1298ed00aabdSChristoph Hellwig ret = submit_bio_noacct(clone); 1299846785e6SChristoph Hellwig break; 1300846785e6SChristoph Hellwig case DM_MAPIO_KILL: 13014e4cbee9SChristoph Hellwig free_tio(tio); 130264f52b0eSMike Snitzer dec_pending(io, BLK_STS_IOERR); 13034e4cbee9SChristoph Hellwig break; 1304846785e6SChristoph Hellwig case DM_MAPIO_REQUEUE: 1305cfae7529SMike Snitzer free_tio(tio); 130664f52b0eSMike Snitzer dec_pending(io, BLK_STS_DM_REQUEUE); 1307846785e6SChristoph Hellwig break; 1308846785e6SChristoph Hellwig default: 130945cbcd79SKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 131045cbcd79SKiyoshi Ueda BUG(); 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds 1313978e51baSMike Snitzer return ret; 13141da177e4SLinus Torvalds } 13151da177e4SLinus Torvalds 1316e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len) 1317bd2a49b8SAlasdair G Kergon { 13184f024f37SKent Overstreet bio->bi_iter.bi_sector = sector; 13194f024f37SKent Overstreet bio->bi_iter.bi_size = to_bytes(len); 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds /* 13231da177e4SLinus Torvalds * Creates a bio that consists of range of complete bvecs. 13241da177e4SLinus Torvalds */ 1325c80914e8SMike Snitzer static int clone_bio(struct dm_target_io *tio, struct bio *bio, 13261c3b13e6SKent Overstreet sector_t sector, unsigned len) 13271da177e4SLinus Torvalds { 1328dba14160SMikulas Patocka struct bio *clone = &tio->clone; 1329*07560151SEric Biggers int r; 13301da177e4SLinus Torvalds 13311c3b13e6SKent Overstreet __bio_clone_fast(clone, bio); 13329c47008dSMartin K. Petersen 1333*07560151SEric Biggers r = bio_crypt_clone(clone, bio, GFP_NOIO); 1334*07560151SEric Biggers if (r < 0) 1335*07560151SEric Biggers return r; 1336a892c8d5SSatya Tangirala 133757c36519SMike Snitzer if (bio_integrity(bio)) { 1338e2460f2aSMikulas Patocka if (unlikely(!dm_target_has_integrity(tio->ti->type) && 1339e2460f2aSMikulas Patocka !dm_target_passes_integrity(tio->ti->type))) { 1340e2460f2aSMikulas Patocka DMWARN("%s: the target %s doesn't support integrity data.", 1341e2460f2aSMikulas Patocka dm_device_name(tio->io->md), 1342e2460f2aSMikulas Patocka tio->ti->type->name); 1343e2460f2aSMikulas Patocka return -EIO; 1344e2460f2aSMikulas Patocka } 1345e2460f2aSMikulas Patocka 1346e2460f2aSMikulas Patocka r = bio_integrity_clone(clone, bio, GFP_NOIO); 1347c80914e8SMike Snitzer if (r < 0) 1348c80914e8SMike Snitzer return r; 1349c80914e8SMike Snitzer } 13501c3b13e6SKent Overstreet 1351fa8db494SMike Snitzer bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector)); 1352fa8db494SMike Snitzer clone->bi_iter.bi_size = to_bytes(len); 1353fa8db494SMike Snitzer 1354fa8db494SMike Snitzer if (bio_integrity(bio)) 1355fa8db494SMike Snitzer bio_integrity_trim(clone); 1356c80914e8SMike Snitzer 1357c80914e8SMike Snitzer return 0; 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds 1360318716ddSMike Snitzer static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci, 1361318716ddSMike Snitzer struct dm_target *ti, unsigned num_bios) 1362f9ab94ceSMikulas Patocka { 1363dba14160SMikulas Patocka struct dm_target_io *tio; 1364318716ddSMike Snitzer int try; 1365dba14160SMikulas Patocka 1366318716ddSMike Snitzer if (!num_bios) 1367318716ddSMike Snitzer return; 1368f9ab94ceSMikulas Patocka 1369318716ddSMike Snitzer if (num_bios == 1) { 1370318716ddSMike Snitzer tio = alloc_tio(ci, ti, 0, GFP_NOIO); 1371318716ddSMike Snitzer bio_list_add(blist, &tio->clone); 1372318716ddSMike Snitzer return; 13739015df24SAlasdair G Kergon } 13749015df24SAlasdair G Kergon 1375318716ddSMike Snitzer for (try = 0; try < 2; try++) { 1376318716ddSMike Snitzer int bio_nr; 1377318716ddSMike Snitzer struct bio *bio; 1378318716ddSMike Snitzer 1379318716ddSMike Snitzer if (try) 1380bc02cdbeSMike Snitzer mutex_lock(&ci->io->md->table_devices_lock); 1381318716ddSMike Snitzer for (bio_nr = 0; bio_nr < num_bios; bio_nr++) { 1382318716ddSMike Snitzer tio = alloc_tio(ci, ti, bio_nr, try ? GFP_NOIO : GFP_NOWAIT); 1383318716ddSMike Snitzer if (!tio) 1384318716ddSMike Snitzer break; 1385318716ddSMike Snitzer 1386318716ddSMike Snitzer bio_list_add(blist, &tio->clone); 1387318716ddSMike Snitzer } 1388318716ddSMike Snitzer if (try) 1389bc02cdbeSMike Snitzer mutex_unlock(&ci->io->md->table_devices_lock); 1390318716ddSMike Snitzer if (bio_nr == num_bios) 1391318716ddSMike Snitzer return; 1392318716ddSMike Snitzer 1393318716ddSMike Snitzer while ((bio = bio_list_pop(blist))) { 1394318716ddSMike Snitzer tio = container_of(bio, struct dm_target_io, clone); 1395318716ddSMike Snitzer free_tio(tio); 1396318716ddSMike Snitzer } 1397318716ddSMike Snitzer } 1398318716ddSMike Snitzer } 1399318716ddSMike Snitzer 1400978e51baSMike Snitzer static blk_qc_t __clone_and_map_simple_bio(struct clone_info *ci, 1401318716ddSMike Snitzer struct dm_target_io *tio, unsigned *len) 14029015df24SAlasdair G Kergon { 1403dba14160SMikulas Patocka struct bio *clone = &tio->clone; 14049015df24SAlasdair G Kergon 14051dd40c3eSMikulas Patocka tio->len_ptr = len; 14061dd40c3eSMikulas Patocka 14071c3b13e6SKent Overstreet __bio_clone_fast(clone, ci->bio); 1408bd2a49b8SAlasdair G Kergon if (len) 14091dd40c3eSMikulas Patocka bio_setup_sector(clone, ci->sector, *len); 1410f9ab94ceSMikulas Patocka 1411978e51baSMike Snitzer return __map_bio(tio); 1412f9ab94ceSMikulas Patocka } 1413f9ab94ceSMikulas Patocka 141414fe594dSAlasdair G Kergon static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti, 14151dd40c3eSMikulas Patocka unsigned num_bios, unsigned *len) 141606a426ceSMike Snitzer { 1417318716ddSMike Snitzer struct bio_list blist = BIO_EMPTY_LIST; 1418318716ddSMike Snitzer struct bio *bio; 1419318716ddSMike Snitzer struct dm_target_io *tio; 142006a426ceSMike Snitzer 1421318716ddSMike Snitzer alloc_multiple_bios(&blist, ci, ti, num_bios); 1422318716ddSMike Snitzer 1423318716ddSMike Snitzer while ((bio = bio_list_pop(&blist))) { 1424318716ddSMike Snitzer tio = container_of(bio, struct dm_target_io, clone); 1425978e51baSMike Snitzer (void) __clone_and_map_simple_bio(ci, tio, len); 1426318716ddSMike Snitzer } 142706a426ceSMike Snitzer } 142806a426ceSMike Snitzer 142914fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci) 1430f9ab94ceSMikulas Patocka { 143106a426ceSMike Snitzer unsigned target_nr = 0; 1432f9ab94ceSMikulas Patocka struct dm_target *ti; 1433f9ab94ceSMikulas Patocka 1434892ad71fSDennis Zhou /* 1435dbe3ece1SJens Axboe * Empty flush uses a statically initialized bio, as the base for 1436dbe3ece1SJens Axboe * cloning. However, blkg association requires that a bdev is 1437dbe3ece1SJens Axboe * associated with a gendisk, which doesn't happen until the bdev is 1438dbe3ece1SJens Axboe * opened. So, blkg association is done at issue time of the flush 1439dbe3ece1SJens Axboe * rather than when the device is created in alloc_dev(). 1440892ad71fSDennis Zhou */ 1441892ad71fSDennis Zhou bio_set_dev(ci->bio, ci->io->md->bdev); 1442892ad71fSDennis Zhou 1443b372d360SMike Snitzer BUG_ON(bio_has_data(ci->bio)); 1444f9ab94ceSMikulas Patocka while ((ti = dm_table_get_target(ci->map, target_nr++))) 14451dd40c3eSMikulas Patocka __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL); 1446f9ab94ceSMikulas Patocka return 0; 1447f9ab94ceSMikulas Patocka } 1448f9ab94ceSMikulas Patocka 1449c80914e8SMike Snitzer static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti, 14501dd40c3eSMikulas Patocka sector_t sector, unsigned *len) 14515ae89a87SMike Snitzer { 1452dba14160SMikulas Patocka struct bio *bio = ci->bio; 14535ae89a87SMike Snitzer struct dm_target_io *tio; 1454f31c21e4SNeilBrown int r; 14555ae89a87SMike Snitzer 1456318716ddSMike Snitzer tio = alloc_tio(ci, ti, 0, GFP_NOIO); 14571dd40c3eSMikulas Patocka tio->len_ptr = len; 1458c80914e8SMike Snitzer r = clone_bio(tio, bio, sector, *len); 1459072623deSMikulas Patocka if (r < 0) { 1460cfae7529SMike Snitzer free_tio(tio); 1461c80914e8SMike Snitzer return r; 1462b0d8ed4dSAlasdair G Kergon } 1463978e51baSMike Snitzer (void) __map_bio(tio); 146455a62eefSAlasdair G Kergon 1465f31c21e4SNeilBrown return 0; 146623508a96SMike Snitzer } 146755a62eefSAlasdair G Kergon 146823508a96SMike Snitzer typedef unsigned (*get_num_bios_fn)(struct dm_target *ti); 146955a62eefSAlasdair G Kergon 147023508a96SMike Snitzer static unsigned get_num_discard_bios(struct dm_target *ti) 147123508a96SMike Snitzer { 147223508a96SMike Snitzer return ti->num_discard_bios; 147323508a96SMike Snitzer } 147423508a96SMike Snitzer 147500716545SDenis Semakin static unsigned get_num_secure_erase_bios(struct dm_target *ti) 147600716545SDenis Semakin { 147700716545SDenis Semakin return ti->num_secure_erase_bios; 147800716545SDenis Semakin } 147900716545SDenis Semakin 148023508a96SMike Snitzer static unsigned get_num_write_same_bios(struct dm_target *ti) 148123508a96SMike Snitzer { 148223508a96SMike Snitzer return ti->num_write_same_bios; 148323508a96SMike Snitzer } 148423508a96SMike Snitzer 1485ac62d620SChristoph Hellwig static unsigned get_num_write_zeroes_bios(struct dm_target *ti) 1486ac62d620SChristoph Hellwig { 1487ac62d620SChristoph Hellwig return ti->num_write_zeroes_bios; 1488ac62d620SChristoph Hellwig } 1489ac62d620SChristoph Hellwig 14903d7f4562SMike Snitzer static int __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti, 149161697a6aSMike Snitzer unsigned num_bios) 14925ae89a87SMike Snitzer { 149351b86f9aSMichael Lass unsigned len; 14945ae89a87SMike Snitzer 14955ae89a87SMike Snitzer /* 149623508a96SMike Snitzer * Even though the device advertised support for this type of 149723508a96SMike Snitzer * request, that does not mean every target supports it, and 1498936688d7SMike Snitzer * reconfiguration might also have changed that since the 14995ae89a87SMike Snitzer * check was performed. 15005ae89a87SMike Snitzer */ 150155a62eefSAlasdair G Kergon if (!num_bios) 15025ae89a87SMike Snitzer return -EOPNOTSUPP; 15035ae89a87SMike Snitzer 150451b86f9aSMichael Lass len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti)); 150551b86f9aSMichael Lass 15061dd40c3eSMikulas Patocka __send_duplicate_bios(ci, ti, num_bios, &len); 15075ae89a87SMike Snitzer 1508a79245b3SMike Snitzer ci->sector += len; 15093d7f4562SMike Snitzer ci->sector_count -= len; 15105ae89a87SMike Snitzer 15115ae89a87SMike Snitzer return 0; 15125ae89a87SMike Snitzer } 15135ae89a87SMike Snitzer 15143d7f4562SMike Snitzer static int __send_discard(struct clone_info *ci, struct dm_target *ti) 151523508a96SMike Snitzer { 151661697a6aSMike Snitzer return __send_changing_extent_only(ci, ti, get_num_discard_bios(ti)); 151723508a96SMike Snitzer } 151823508a96SMike Snitzer 151900716545SDenis Semakin static int __send_secure_erase(struct clone_info *ci, struct dm_target *ti) 152000716545SDenis Semakin { 152161697a6aSMike Snitzer return __send_changing_extent_only(ci, ti, get_num_secure_erase_bios(ti)); 152200716545SDenis Semakin } 152300716545SDenis Semakin 15243d7f4562SMike Snitzer static int __send_write_same(struct clone_info *ci, struct dm_target *ti) 152523508a96SMike Snitzer { 152661697a6aSMike Snitzer return __send_changing_extent_only(ci, ti, get_num_write_same_bios(ti)); 152723508a96SMike Snitzer } 152823508a96SMike Snitzer 15293d7f4562SMike Snitzer static int __send_write_zeroes(struct clone_info *ci, struct dm_target *ti) 1530ac62d620SChristoph Hellwig { 153161697a6aSMike Snitzer return __send_changing_extent_only(ci, ti, get_num_write_zeroes_bios(ti)); 1532ac62d620SChristoph Hellwig } 1533ac62d620SChristoph Hellwig 1534568c73a3SMike Snitzer static bool is_abnormal_io(struct bio *bio) 1535568c73a3SMike Snitzer { 1536568c73a3SMike Snitzer bool r = false; 1537568c73a3SMike Snitzer 1538568c73a3SMike Snitzer switch (bio_op(bio)) { 1539568c73a3SMike Snitzer case REQ_OP_DISCARD: 1540568c73a3SMike Snitzer case REQ_OP_SECURE_ERASE: 1541568c73a3SMike Snitzer case REQ_OP_WRITE_SAME: 1542568c73a3SMike Snitzer case REQ_OP_WRITE_ZEROES: 1543568c73a3SMike Snitzer r = true; 1544568c73a3SMike Snitzer break; 1545568c73a3SMike Snitzer } 1546568c73a3SMike Snitzer 1547568c73a3SMike Snitzer return r; 1548568c73a3SMike Snitzer } 1549568c73a3SMike Snitzer 15500519c71eSMike Snitzer static bool __process_abnormal_io(struct clone_info *ci, struct dm_target *ti, 15510519c71eSMike Snitzer int *result) 15520519c71eSMike Snitzer { 15530519c71eSMike Snitzer struct bio *bio = ci->bio; 15540519c71eSMike Snitzer 15550519c71eSMike Snitzer if (bio_op(bio) == REQ_OP_DISCARD) 15560519c71eSMike Snitzer *result = __send_discard(ci, ti); 155700716545SDenis Semakin else if (bio_op(bio) == REQ_OP_SECURE_ERASE) 155800716545SDenis Semakin *result = __send_secure_erase(ci, ti); 15590519c71eSMike Snitzer else if (bio_op(bio) == REQ_OP_WRITE_SAME) 15600519c71eSMike Snitzer *result = __send_write_same(ci, ti); 15610519c71eSMike Snitzer else if (bio_op(bio) == REQ_OP_WRITE_ZEROES) 15620519c71eSMike Snitzer *result = __send_write_zeroes(ci, ti); 15630519c71eSMike Snitzer else 15640519c71eSMike Snitzer return false; 15650519c71eSMike Snitzer 15660519c71eSMike Snitzer return true; 15670519c71eSMike Snitzer } 15680519c71eSMike Snitzer 1569e4c93811SAlasdair G Kergon /* 1570e4c93811SAlasdair G Kergon * Select the correct strategy for processing a non-flush bio. 1571e4c93811SAlasdair G Kergon */ 1572e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci) 1573e4c93811SAlasdair G Kergon { 1574e4c93811SAlasdair G Kergon struct dm_target *ti; 15751c3b13e6SKent Overstreet unsigned len; 1576c80914e8SMike Snitzer int r; 1577e4c93811SAlasdair G Kergon 1578e4c93811SAlasdair G Kergon ti = dm_table_find_target(ci->map, ci->sector); 1579123d87d5SMikulas Patocka if (!ti) 1580e4c93811SAlasdair G Kergon return -EIO; 1581e4c93811SAlasdair G Kergon 1582568c73a3SMike Snitzer if (__process_abnormal_io(ci, ti, &r)) 15830519c71eSMike Snitzer return r; 15843d7f4562SMike Snitzer 1585e76239a3SChristoph Hellwig len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count); 1586e4c93811SAlasdair G Kergon 1587c80914e8SMike Snitzer r = __clone_and_map_data_bio(ci, ti, ci->sector, &len); 1588c80914e8SMike Snitzer if (r < 0) 1589c80914e8SMike Snitzer return r; 1590e4c93811SAlasdair G Kergon 1591e4c93811SAlasdair G Kergon ci->sector += len; 1592e4c93811SAlasdair G Kergon ci->sector_count -= len; 1593e4c93811SAlasdair G Kergon 1594e4c93811SAlasdair G Kergon return 0; 1595e4c93811SAlasdair G Kergon } 1596e4c93811SAlasdair G Kergon 1597978e51baSMike Snitzer static void init_clone_info(struct clone_info *ci, struct mapped_device *md, 1598978e51baSMike Snitzer struct dm_table *map, struct bio *bio) 1599978e51baSMike Snitzer { 1600978e51baSMike Snitzer ci->map = map; 1601978e51baSMike Snitzer ci->io = alloc_io(md, bio); 1602978e51baSMike Snitzer ci->sector = bio->bi_iter.bi_sector; 1603978e51baSMike Snitzer } 1604978e51baSMike Snitzer 1605a1e1cb72SMike Snitzer #define __dm_part_stat_sub(part, field, subnd) \ 1606a1e1cb72SMike Snitzer (part_stat_get(part, field) -= (subnd)) 1607a1e1cb72SMike Snitzer 1608e4c93811SAlasdair G Kergon /* 160914fe594dSAlasdair G Kergon * Entry point to split a bio into clones and submit them to the targets. 16101da177e4SLinus Torvalds */ 1611978e51baSMike Snitzer static blk_qc_t __split_and_process_bio(struct mapped_device *md, 161283d5e5b0SMikulas Patocka struct dm_table *map, struct bio *bio) 16131da177e4SLinus Torvalds { 16141da177e4SLinus Torvalds struct clone_info ci; 1615978e51baSMike Snitzer blk_qc_t ret = BLK_QC_T_NONE; 1616512875bdSJun'ichi Nomura int error = 0; 16171da177e4SLinus Torvalds 1618978e51baSMike Snitzer init_clone_info(&ci, md, map, bio); 1619bd2a49b8SAlasdair G Kergon 16201eff9d32SJens Axboe if (bio->bi_opf & REQ_PREFLUSH) { 1621dbe3ece1SJens Axboe struct bio flush_bio; 1622dbe3ece1SJens Axboe 1623dbe3ece1SJens Axboe /* 1624dbe3ece1SJens Axboe * Use an on-stack bio for this, it's safe since we don't 1625dbe3ece1SJens Axboe * need to reference it after submit. It's just used as 1626dbe3ece1SJens Axboe * the basis for the clone(s). 1627dbe3ece1SJens Axboe */ 1628dbe3ece1SJens Axboe bio_init(&flush_bio, NULL, 0); 1629dbe3ece1SJens Axboe flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC; 1630dbe3ece1SJens Axboe ci.bio = &flush_bio; 1631b372d360SMike Snitzer ci.sector_count = 0; 163214fe594dSAlasdair G Kergon error = __send_empty_flush(&ci); 1633382761dcSChristoph Hellwig bio_uninit(ci.bio); 1634b372d360SMike Snitzer /* dec_pending submits any data associated with flush */ 16352e2d6f7eSAjay Joshi } else if (op_is_zone_mgmt(bio_op(bio))) { 1636a4aa5e56SDamien Le Moal ci.bio = bio; 1637a4aa5e56SDamien Le Moal ci.sector_count = 0; 1638a4aa5e56SDamien Le Moal error = __split_and_process_non_flush(&ci); 1639b372d360SMike Snitzer } else { 16406a8736d1STejun Heo ci.bio = bio; 16411da177e4SLinus Torvalds ci.sector_count = bio_sectors(bio); 164218a25da8SNeilBrown while (ci.sector_count && !error) { 164314fe594dSAlasdair G Kergon error = __split_and_process_non_flush(&ci); 164418a25da8SNeilBrown if (current->bio_list && ci.sector_count && !error) { 164518a25da8SNeilBrown /* 1646ed00aabdSChristoph Hellwig * Remainder must be passed to submit_bio_noacct() 164718a25da8SNeilBrown * so that it gets handled *after* bios already submitted 164818a25da8SNeilBrown * have been completely processed. 164918a25da8SNeilBrown * We take a clone of the original to store in 1650745dc570SMike Snitzer * ci.io->orig_bio to be used by end_io_acct() and 165118a25da8SNeilBrown * for dec_pending to use for completion handling. 165218a25da8SNeilBrown */ 1653f21c601aSMike Snitzer struct bio *b = bio_split(bio, bio_sectors(bio) - ci.sector_count, 1654f21c601aSMike Snitzer GFP_NOIO, &md->queue->bio_split); 1655745dc570SMike Snitzer ci.io->orig_bio = b; 1656a1e1cb72SMike Snitzer 1657a1e1cb72SMike Snitzer /* 1658a1e1cb72SMike Snitzer * Adjust IO stats for each split, otherwise upon queue 1659a1e1cb72SMike Snitzer * reentry there will be redundant IO accounting. 1660a1e1cb72SMike Snitzer * NOTE: this is a stop-gap fix, a proper fix involves 1661a1e1cb72SMike Snitzer * significant refactoring of DM core's bio splitting 1662a1e1cb72SMike Snitzer * (by eliminating DM's splitting and just using bio_split) 1663a1e1cb72SMike Snitzer */ 1664a1e1cb72SMike Snitzer part_stat_lock(); 1665a1e1cb72SMike Snitzer __dm_part_stat_sub(&dm_disk(md)->part0, 1666a1e1cb72SMike Snitzer sectors[op_stat_group(bio_op(bio))], ci.sector_count); 1667a1e1cb72SMike Snitzer part_stat_unlock(); 1668a1e1cb72SMike Snitzer 166918a25da8SNeilBrown bio_chain(b, bio); 1670075c18c3SMike Snitzer trace_block_split(md->queue, b, bio->bi_iter.bi_sector); 1671ed00aabdSChristoph Hellwig ret = submit_bio_noacct(bio); 167218a25da8SNeilBrown break; 167318a25da8SNeilBrown } 167418a25da8SNeilBrown } 1675d87f4c14STejun Heo } 16761da177e4SLinus Torvalds 16771da177e4SLinus Torvalds /* drop the extra reference count */ 167854385bf7SBart Van Assche dec_pending(ci.io, errno_to_blk_status(error)); 1679978e51baSMike Snitzer return ret; 16801da177e4SLinus Torvalds } 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds /* 1683978e51baSMike Snitzer * Optimized variant of __split_and_process_bio that leverages the 1684978e51baSMike Snitzer * fact that targets that use it do _not_ have a need to split bios. 16851da177e4SLinus Torvalds */ 1686568c73a3SMike Snitzer static blk_qc_t __process_bio(struct mapped_device *md, struct dm_table *map, 1687568c73a3SMike Snitzer struct bio *bio, struct dm_target *ti) 16881da177e4SLinus Torvalds { 1689978e51baSMike Snitzer struct clone_info ci; 1690978e51baSMike Snitzer blk_qc_t ret = BLK_QC_T_NONE; 1691978e51baSMike Snitzer int error = 0; 1692978e51baSMike Snitzer 1693978e51baSMike Snitzer init_clone_info(&ci, md, map, bio); 1694978e51baSMike Snitzer 1695978e51baSMike Snitzer if (bio->bi_opf & REQ_PREFLUSH) { 1696dbe3ece1SJens Axboe struct bio flush_bio; 1697dbe3ece1SJens Axboe 1698dbe3ece1SJens Axboe /* 1699dbe3ece1SJens Axboe * Use an on-stack bio for this, it's safe since we don't 1700dbe3ece1SJens Axboe * need to reference it after submit. It's just used as 1701dbe3ece1SJens Axboe * the basis for the clone(s). 1702dbe3ece1SJens Axboe */ 1703dbe3ece1SJens Axboe bio_init(&flush_bio, NULL, 0); 1704dbe3ece1SJens Axboe flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC; 1705dbe3ece1SJens Axboe ci.bio = &flush_bio; 1706978e51baSMike Snitzer ci.sector_count = 0; 1707978e51baSMike Snitzer error = __send_empty_flush(&ci); 1708382761dcSChristoph Hellwig bio_uninit(ci.bio); 1709978e51baSMike Snitzer /* dec_pending submits any data associated with flush */ 1710978e51baSMike Snitzer } else { 1711978e51baSMike Snitzer struct dm_target_io *tio; 1712978e51baSMike Snitzer 1713978e51baSMike Snitzer ci.bio = bio; 1714978e51baSMike Snitzer ci.sector_count = bio_sectors(bio); 1715568c73a3SMike Snitzer if (__process_abnormal_io(&ci, ti, &error)) 17160519c71eSMike Snitzer goto out; 17170519c71eSMike Snitzer 17180519c71eSMike Snitzer tio = alloc_tio(&ci, ti, 0, GFP_NOIO); 1719978e51baSMike Snitzer ret = __clone_and_map_simple_bio(&ci, tio, NULL); 1720978e51baSMike Snitzer } 1721978e51baSMike Snitzer out: 1722978e51baSMike Snitzer /* drop the extra reference count */ 1723978e51baSMike Snitzer dec_pending(ci.io, errno_to_blk_status(error)); 1724978e51baSMike Snitzer return ret; 1725978e51baSMike Snitzer } 1726978e51baSMike Snitzer 1727568c73a3SMike Snitzer static void dm_queue_split(struct mapped_device *md, struct dm_target *ti, struct bio **bio) 1728568c73a3SMike Snitzer { 1729568c73a3SMike Snitzer unsigned len, sector_count; 1730568c73a3SMike Snitzer 1731568c73a3SMike Snitzer sector_count = bio_sectors(*bio); 1732568c73a3SMike Snitzer len = min_t(sector_t, max_io_len((*bio)->bi_iter.bi_sector, ti), sector_count); 1733568c73a3SMike Snitzer 1734568c73a3SMike Snitzer if (sector_count > len) { 1735568c73a3SMike Snitzer struct bio *split = bio_split(*bio, len, GFP_NOIO, &md->queue->bio_split); 1736568c73a3SMike Snitzer 1737568c73a3SMike Snitzer bio_chain(split, *bio); 1738568c73a3SMike Snitzer trace_block_split(md->queue, split, (*bio)->bi_iter.bi_sector); 1739ed00aabdSChristoph Hellwig submit_bio_noacct(*bio); 1740568c73a3SMike Snitzer *bio = split; 1741568c73a3SMike Snitzer } 1742568c73a3SMike Snitzer } 1743568c73a3SMike Snitzer 17446548c7c5SMike Snitzer static blk_qc_t dm_process_bio(struct mapped_device *md, 17456548c7c5SMike Snitzer struct dm_table *map, struct bio *bio) 17466548c7c5SMike Snitzer { 1747568c73a3SMike Snitzer blk_qc_t ret = BLK_QC_T_NONE; 1748568c73a3SMike Snitzer struct dm_target *ti = md->immutable_target; 1749568c73a3SMike Snitzer 1750568c73a3SMike Snitzer if (unlikely(!map)) { 1751568c73a3SMike Snitzer bio_io_error(bio); 1752568c73a3SMike Snitzer return ret; 1753568c73a3SMike Snitzer } 1754568c73a3SMike Snitzer 1755568c73a3SMike Snitzer if (!ti) { 1756568c73a3SMike Snitzer ti = dm_table_find_target(map, bio->bi_iter.bi_sector); 1757123d87d5SMikulas Patocka if (unlikely(!ti)) { 1758568c73a3SMike Snitzer bio_io_error(bio); 1759568c73a3SMike Snitzer return ret; 1760568c73a3SMike Snitzer } 1761568c73a3SMike Snitzer } 1762568c73a3SMike Snitzer 1763568c73a3SMike Snitzer /* 1764c62b37d9SChristoph Hellwig * If in ->queue_bio we need to use blk_queue_split(), otherwise 1765568c73a3SMike Snitzer * queue_limits for abnormal requests (e.g. discard, writesame, etc) 1766568c73a3SMike Snitzer * won't be imposed. 1767568c73a3SMike Snitzer */ 1768568c73a3SMike Snitzer if (current->bio_list) { 1769120c9257SMike Snitzer if (is_abnormal_io(bio)) 1770f695ca38SChristoph Hellwig blk_queue_split(&bio); 1771120c9257SMike Snitzer else 1772568c73a3SMike Snitzer dm_queue_split(md, ti, &bio); 1773568c73a3SMike Snitzer } 1774568c73a3SMike Snitzer 17756548c7c5SMike Snitzer if (dm_get_md_type(md) == DM_TYPE_NVME_BIO_BASED) 1776568c73a3SMike Snitzer return __process_bio(md, map, bio, ti); 17776548c7c5SMike Snitzer else 17786548c7c5SMike Snitzer return __split_and_process_bio(md, map, bio); 17796548c7c5SMike Snitzer } 17806548c7c5SMike Snitzer 1781c62b37d9SChristoph Hellwig static blk_qc_t dm_submit_bio(struct bio *bio) 17821da177e4SLinus Torvalds { 1783c4a59c4eSChristoph Hellwig struct mapped_device *md = bio->bi_disk->private_data; 1784978e51baSMike Snitzer blk_qc_t ret = BLK_QC_T_NONE; 178583d5e5b0SMikulas Patocka int srcu_idx; 178683d5e5b0SMikulas Patocka struct dm_table *map; 17871da177e4SLinus Torvalds 1788ac7c5675SChristoph Hellwig if (dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) { 1789ac7c5675SChristoph Hellwig /* 1790ac7c5675SChristoph Hellwig * We are called with a live reference on q_usage_counter, but 1791ac7c5675SChristoph Hellwig * that one will be released as soon as we return. Grab an 1792c62b37d9SChristoph Hellwig * extra one as blk_mq_submit_bio expects to be able to consume 1793c62b37d9SChristoph Hellwig * a reference (which lives until the request is freed in case a 1794c62b37d9SChristoph Hellwig * request is allocated). 1795ac7c5675SChristoph Hellwig */ 1796c62b37d9SChristoph Hellwig percpu_ref_get(&bio->bi_disk->queue->q_usage_counter); 1797c62b37d9SChristoph Hellwig return blk_mq_submit_bio(bio); 1798ac7c5675SChristoph Hellwig } 17998cf7961dSChristoph Hellwig 180083d5e5b0SMikulas Patocka map = dm_get_live_table(md, &srcu_idx); 18011da177e4SLinus Torvalds 18026a8736d1STejun Heo /* if we're suspended, we have to queue this io for later */ 18036a8736d1STejun Heo if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) { 180483d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 18051da177e4SLinus Torvalds 18066abc4946SKonstantin Khlebnikov if (bio->bi_opf & REQ_NOWAIT) 18076abc4946SKonstantin Khlebnikov bio_wouldblock_error(bio); 18086abc4946SKonstantin Khlebnikov else if (!(bio->bi_opf & REQ_RAHEAD)) 180992c63902SMikulas Patocka queue_io(md, bio); 18106a8736d1STejun Heo else 18116a8736d1STejun Heo bio_io_error(bio); 1812978e51baSMike Snitzer return ret; 18131da177e4SLinus Torvalds } 18141da177e4SLinus Torvalds 18156548c7c5SMike Snitzer ret = dm_process_bio(md, map, bio); 1816978e51baSMike Snitzer 181783d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 1818978e51baSMike Snitzer return ret; 1819978e51baSMike Snitzer } 1820978e51baSMike Snitzer 18211da177e4SLinus Torvalds /*----------------------------------------------------------------- 18221da177e4SLinus Torvalds * An IDR is used to keep track of allocated minor numbers. 18231da177e4SLinus Torvalds *---------------------------------------------------------------*/ 18242b06cfffSAlasdair G Kergon static void free_minor(int minor) 18251da177e4SLinus Torvalds { 1826f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 18271da177e4SLinus Torvalds idr_remove(&_minor_idr, minor); 1828f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 18291da177e4SLinus Torvalds } 18301da177e4SLinus Torvalds 18311da177e4SLinus Torvalds /* 18321da177e4SLinus Torvalds * See if the device with a specific minor # is free. 18331da177e4SLinus Torvalds */ 1834cf13ab8eSFrederik Deweerdt static int specific_minor(int minor) 18351da177e4SLinus Torvalds { 1836c9d76be6STejun Heo int r; 18371da177e4SLinus Torvalds 18381da177e4SLinus Torvalds if (minor >= (1 << MINORBITS)) 18391da177e4SLinus Torvalds return -EINVAL; 18401da177e4SLinus Torvalds 1841c9d76be6STejun Heo idr_preload(GFP_KERNEL); 1842f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 18431da177e4SLinus Torvalds 1844c9d76be6STejun Heo r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT); 18451da177e4SLinus Torvalds 1846f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1847c9d76be6STejun Heo idr_preload_end(); 1848c9d76be6STejun Heo if (r < 0) 1849c9d76be6STejun Heo return r == -ENOSPC ? -EBUSY : r; 1850c9d76be6STejun Heo return 0; 18511da177e4SLinus Torvalds } 18521da177e4SLinus Torvalds 1853cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor) 18541da177e4SLinus Torvalds { 1855c9d76be6STejun Heo int r; 18561da177e4SLinus Torvalds 1857c9d76be6STejun Heo idr_preload(GFP_KERNEL); 1858f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 18591da177e4SLinus Torvalds 1860c9d76be6STejun Heo r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT); 18611da177e4SLinus Torvalds 1862f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1863c9d76be6STejun Heo idr_preload_end(); 1864c9d76be6STejun Heo if (r < 0) 18651da177e4SLinus Torvalds return r; 1866c9d76be6STejun Heo *minor = r; 1867c9d76be6STejun Heo return 0; 18681da177e4SLinus Torvalds } 18691da177e4SLinus Torvalds 187083d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops; 1871f26c5719SDan Williams static const struct dax_operations dm_dax_ops; 18721da177e4SLinus Torvalds 187353d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work); 187453d5914fSMikulas Patocka 18750f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md) 18760f20972fSMike Snitzer { 18770f20972fSMike Snitzer if (md->wq) 18780f20972fSMike Snitzer destroy_workqueue(md->wq); 18796f1c819cSKent Overstreet bioset_exit(&md->bs); 18806f1c819cSKent Overstreet bioset_exit(&md->io_bs); 18810f20972fSMike Snitzer 1882f26c5719SDan Williams if (md->dax_dev) { 1883f26c5719SDan Williams kill_dax(md->dax_dev); 1884f26c5719SDan Williams put_dax(md->dax_dev); 1885f26c5719SDan Williams md->dax_dev = NULL; 1886f26c5719SDan Williams } 1887f26c5719SDan Williams 18880f20972fSMike Snitzer if (md->disk) { 18890f20972fSMike Snitzer spin_lock(&_minor_lock); 18900f20972fSMike Snitzer md->disk->private_data = NULL; 18910f20972fSMike Snitzer spin_unlock(&_minor_lock); 18920f20972fSMike Snitzer del_gendisk(md->disk); 18930f20972fSMike Snitzer put_disk(md->disk); 18940f20972fSMike Snitzer } 18950f20972fSMike Snitzer 18960f20972fSMike Snitzer if (md->queue) 18970f20972fSMike Snitzer blk_cleanup_queue(md->queue); 18980f20972fSMike Snitzer 1899d09960b0STahsin Erdogan cleanup_srcu_struct(&md->io_barrier); 1900d09960b0STahsin Erdogan 19010f20972fSMike Snitzer if (md->bdev) { 19020f20972fSMike Snitzer bdput(md->bdev); 19030f20972fSMike Snitzer md->bdev = NULL; 19040f20972fSMike Snitzer } 19054cc96131SMike Snitzer 1906d5ffebddSMike Snitzer mutex_destroy(&md->suspend_lock); 1907d5ffebddSMike Snitzer mutex_destroy(&md->type_lock); 1908d5ffebddSMike Snitzer mutex_destroy(&md->table_devices_lock); 1909d5ffebddSMike Snitzer 19104cc96131SMike Snitzer dm_mq_cleanup_mapped_device(md); 19110f20972fSMike Snitzer } 19120f20972fSMike Snitzer 19131da177e4SLinus Torvalds /* 19141da177e4SLinus Torvalds * Allocate and initialise a blank device with a given minor. 19151da177e4SLinus Torvalds */ 19162b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor) 19171da177e4SLinus Torvalds { 1918115485e8SMike Snitzer int r, numa_node_id = dm_get_numa_node(); 1919115485e8SMike Snitzer struct mapped_device *md; 1920ba61fdd1SJeff Mahoney void *old_md; 19211da177e4SLinus Torvalds 1922856eb091SMikulas Patocka md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id); 19231da177e4SLinus Torvalds if (!md) { 19241da177e4SLinus Torvalds DMWARN("unable to allocate device, out of memory."); 19251da177e4SLinus Torvalds return NULL; 19261da177e4SLinus Torvalds } 19271da177e4SLinus Torvalds 192810da4f79SJeff Mahoney if (!try_module_get(THIS_MODULE)) 19296ed7ade8SMilan Broz goto bad_module_get; 193010da4f79SJeff Mahoney 19311da177e4SLinus Torvalds /* get a minor number for the dev */ 19322b06cfffSAlasdair G Kergon if (minor == DM_ANY_MINOR) 1933cf13ab8eSFrederik Deweerdt r = next_free_minor(&minor); 19342b06cfffSAlasdair G Kergon else 1935cf13ab8eSFrederik Deweerdt r = specific_minor(minor); 19361da177e4SLinus Torvalds if (r < 0) 19376ed7ade8SMilan Broz goto bad_minor; 19381da177e4SLinus Torvalds 193983d5e5b0SMikulas Patocka r = init_srcu_struct(&md->io_barrier); 194083d5e5b0SMikulas Patocka if (r < 0) 194183d5e5b0SMikulas Patocka goto bad_io_barrier; 194283d5e5b0SMikulas Patocka 1943115485e8SMike Snitzer md->numa_node_id = numa_node_id; 1944591ddcfcSMike Snitzer md->init_tio_pdu = false; 1945a5664dadSMike Snitzer md->type = DM_TYPE_NONE; 1946e61290a4SDaniel Walker mutex_init(&md->suspend_lock); 1947a5664dadSMike Snitzer mutex_init(&md->type_lock); 194886f1152bSBenjamin Marzinski mutex_init(&md->table_devices_lock); 1949022c2611SMikulas Patocka spin_lock_init(&md->deferred_lock); 19501da177e4SLinus Torvalds atomic_set(&md->holders, 1); 19515c6bd75dSAlasdair G Kergon atomic_set(&md->open_count, 0); 19521da177e4SLinus Torvalds atomic_set(&md->event_nr, 0); 19537a8c3d3bSMike Anderson atomic_set(&md->uevent_seq, 0); 19547a8c3d3bSMike Anderson INIT_LIST_HEAD(&md->uevent_list); 195586f1152bSBenjamin Marzinski INIT_LIST_HEAD(&md->table_devices); 19567a8c3d3bSMike Anderson spin_lock_init(&md->uevent_lock); 19571da177e4SLinus Torvalds 195847ace7e0SMike Snitzer /* 1959c62b37d9SChristoph Hellwig * default to bio-based until DM table is loaded and md->type 1960c62b37d9SChristoph Hellwig * established. If request-based table is loaded: blk-mq will 1961c62b37d9SChristoph Hellwig * override accordingly. 196247ace7e0SMike Snitzer */ 1963c62b37d9SChristoph Hellwig md->queue = blk_alloc_queue(numa_node_id); 19643d745ea5SChristoph Hellwig if (!md->queue) 19653d745ea5SChristoph Hellwig goto bad; 19661da177e4SLinus Torvalds 1967c12c9a3cSMike Snitzer md->disk = alloc_disk_node(1, md->numa_node_id); 19681da177e4SLinus Torvalds if (!md->disk) 19690f20972fSMike Snitzer goto bad; 19701da177e4SLinus Torvalds 1971f0b04115SJeff Mahoney init_waitqueue_head(&md->wait); 197253d5914fSMikulas Patocka INIT_WORK(&md->work, dm_wq_work); 1973f0b04115SJeff Mahoney init_waitqueue_head(&md->eventq); 19742995fa78SMikulas Patocka init_completion(&md->kobj_holder.completion); 1975f0b04115SJeff Mahoney 19761da177e4SLinus Torvalds md->disk->major = _major; 19771da177e4SLinus Torvalds md->disk->first_minor = minor; 19781da177e4SLinus Torvalds md->disk->fops = &dm_blk_dops; 19791da177e4SLinus Torvalds md->disk->queue = md->queue; 19801da177e4SLinus Torvalds md->disk->private_data = md; 19811da177e4SLinus Torvalds sprintf(md->disk->disk_name, "dm-%d", minor); 1982f26c5719SDan Williams 1983976431b0SDan Williams if (IS_ENABLED(CONFIG_DAX_DRIVER)) { 1984fefc1d97SPankaj Gupta md->dax_dev = alloc_dax(md, md->disk->disk_name, 1985fefc1d97SPankaj Gupta &dm_dax_ops, 0); 19864e4ced93SVivek Goyal if (IS_ERR(md->dax_dev)) 1987f26c5719SDan Williams goto bad; 1988976431b0SDan Williams } 1989f26c5719SDan Williams 1990c100ec49SMike Snitzer add_disk_no_queue_reg(md->disk); 19917e51f257SMike Anderson format_dev_t(md->name, MKDEV(_major, minor)); 19921da177e4SLinus Torvalds 1993670368a8STejun Heo md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0); 1994304f3f6aSMilan Broz if (!md->wq) 19950f20972fSMike Snitzer goto bad; 1996304f3f6aSMilan Broz 199732a926daSMikulas Patocka md->bdev = bdget_disk(md->disk, 0); 199832a926daSMikulas Patocka if (!md->bdev) 19990f20972fSMike Snitzer goto bad; 200032a926daSMikulas Patocka 2001fd2ed4d2SMikulas Patocka dm_stats_init(&md->stats); 2002fd2ed4d2SMikulas Patocka 2003ba61fdd1SJeff Mahoney /* Populate the mapping, nobody knows we exist yet */ 2004f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 2005ba61fdd1SJeff Mahoney old_md = idr_replace(&_minor_idr, md, minor); 2006f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 2007ba61fdd1SJeff Mahoney 2008ba61fdd1SJeff Mahoney BUG_ON(old_md != MINOR_ALLOCED); 2009ba61fdd1SJeff Mahoney 20101da177e4SLinus Torvalds return md; 20111da177e4SLinus Torvalds 20120f20972fSMike Snitzer bad: 20130f20972fSMike Snitzer cleanup_mapped_device(md); 201483d5e5b0SMikulas Patocka bad_io_barrier: 20151da177e4SLinus Torvalds free_minor(minor); 20166ed7ade8SMilan Broz bad_minor: 201710da4f79SJeff Mahoney module_put(THIS_MODULE); 20186ed7ade8SMilan Broz bad_module_get: 2019856eb091SMikulas Patocka kvfree(md); 20201da177e4SLinus Torvalds return NULL; 20211da177e4SLinus Torvalds } 20221da177e4SLinus Torvalds 2023ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md); 2024ae9da83fSJun'ichi Nomura 20251da177e4SLinus Torvalds static void free_dev(struct mapped_device *md) 20261da177e4SLinus Torvalds { 2027f331c029STejun Heo int minor = MINOR(disk_devt(md->disk)); 202863d94e48SJun'ichi Nomura 2029ae9da83fSJun'ichi Nomura unlock_fs(md); 20302eb6e1e3SKeith Busch 20310f20972fSMike Snitzer cleanup_mapped_device(md); 20320f20972fSMike Snitzer 20330f20972fSMike Snitzer free_table_devices(&md->table_devices); 20340f20972fSMike Snitzer dm_stats_cleanup(&md->stats); 203563a4f065SMike Snitzer free_minor(minor); 203663a4f065SMike Snitzer 203710da4f79SJeff Mahoney module_put(THIS_MODULE); 2038856eb091SMikulas Patocka kvfree(md); 20391da177e4SLinus Torvalds } 20401da177e4SLinus Torvalds 20412a2a4c51SJens Axboe static int __bind_mempools(struct mapped_device *md, struct dm_table *t) 2042e6ee8c0bSKiyoshi Ueda { 2043c0820cf5SMikulas Patocka struct dm_md_mempools *p = dm_table_get_md_mempools(t); 20442a2a4c51SJens Axboe int ret = 0; 2045e6ee8c0bSKiyoshi Ueda 2046545ed20eSToshi Kani if (dm_table_bio_based(t)) { 2047c0820cf5SMikulas Patocka /* 204864f52b0eSMike Snitzer * The md may already have mempools that need changing. 204964f52b0eSMike Snitzer * If so, reload bioset because front_pad may have changed 205016245bdcSJun'ichi Nomura * because a different table was loaded. 2051c0820cf5SMikulas Patocka */ 20526f1c819cSKent Overstreet bioset_exit(&md->bs); 20536f1c819cSKent Overstreet bioset_exit(&md->io_bs); 20540776aa0eSMike Snitzer 20556f1c819cSKent Overstreet } else if (bioset_initialized(&md->bs)) { 2056cbc4e3c1SMike Snitzer /* 20574e6e36c3SMike Snitzer * There's no need to reload with request-based dm 20584e6e36c3SMike Snitzer * because the size of front_pad doesn't change. 20594e6e36c3SMike Snitzer * Note for future: If you are to reload bioset, 20604e6e36c3SMike Snitzer * prep-ed requests in the queue may refer 20614e6e36c3SMike Snitzer * to bio from the old bioset, so you must walk 20624e6e36c3SMike Snitzer * through the queue to unprep. 2063cbc4e3c1SMike Snitzer */ 2064cbc4e3c1SMike Snitzer goto out; 2065cbc4e3c1SMike Snitzer } 2066cbc4e3c1SMike Snitzer 20676f1c819cSKent Overstreet BUG_ON(!p || 20686f1c819cSKent Overstreet bioset_initialized(&md->bs) || 20696f1c819cSKent Overstreet bioset_initialized(&md->io_bs)); 2070e6ee8c0bSKiyoshi Ueda 20712a2a4c51SJens Axboe ret = bioset_init_from_src(&md->bs, &p->bs); 20722a2a4c51SJens Axboe if (ret) 20732a2a4c51SJens Axboe goto out; 20742a2a4c51SJens Axboe ret = bioset_init_from_src(&md->io_bs, &p->io_bs); 20752a2a4c51SJens Axboe if (ret) 20762a2a4c51SJens Axboe bioset_exit(&md->bs); 2077e6ee8c0bSKiyoshi Ueda out: 207802233342SMike Snitzer /* mempool bind completed, no longer need any mempools in the table */ 2079e6ee8c0bSKiyoshi Ueda dm_table_free_md_mempools(t); 20802a2a4c51SJens Axboe return ret; 2081e6ee8c0bSKiyoshi Ueda } 2082e6ee8c0bSKiyoshi Ueda 20831da177e4SLinus Torvalds /* 20841da177e4SLinus Torvalds * Bind a table to the device. 20851da177e4SLinus Torvalds */ 20861da177e4SLinus Torvalds static void event_callback(void *context) 20871da177e4SLinus Torvalds { 20887a8c3d3bSMike Anderson unsigned long flags; 20897a8c3d3bSMike Anderson LIST_HEAD(uevents); 20901da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) context; 20911da177e4SLinus Torvalds 20927a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 20937a8c3d3bSMike Anderson list_splice_init(&md->uevent_list, &uevents); 20947a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 20957a8c3d3bSMike Anderson 2096ed9e1982STejun Heo dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); 20977a8c3d3bSMike Anderson 20981da177e4SLinus Torvalds atomic_inc(&md->event_nr); 20991da177e4SLinus Torvalds wake_up(&md->eventq); 210062e08243SMikulas Patocka dm_issue_global_event(); 21011da177e4SLinus Torvalds } 21021da177e4SLinus Torvalds 2103c217649bSMike Snitzer /* 2104042d2a9bSAlasdair G Kergon * Returns old map, which caller must destroy. 2105042d2a9bSAlasdair G Kergon */ 2106042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t, 2107754c5fc7SMike Snitzer struct queue_limits *limits) 21081da177e4SLinus Torvalds { 2109042d2a9bSAlasdair G Kergon struct dm_table *old_map; 2110165125e1SJens Axboe struct request_queue *q = md->queue; 2111978e51baSMike Snitzer bool request_based = dm_table_request_based(t); 21121da177e4SLinus Torvalds sector_t size; 21132a2a4c51SJens Axboe int ret; 21141da177e4SLinus Torvalds 21155a8f1f80SBart Van Assche lockdep_assert_held(&md->suspend_lock); 21165a8f1f80SBart Van Assche 21171da177e4SLinus Torvalds size = dm_table_get_size(t); 21183ac51e74SDarrick J. Wong 21193ac51e74SDarrick J. Wong /* 21203ac51e74SDarrick J. Wong * Wipe any geometry if the size of the table changed. 21213ac51e74SDarrick J. Wong */ 2122fd2ed4d2SMikulas Patocka if (size != dm_get_size(md)) 21233ac51e74SDarrick J. Wong memset(&md->geometry, 0, sizeof(md->geometry)); 21243ac51e74SDarrick J. Wong 2125c2b4bb8cSChristoph Hellwig set_capacity(md->disk, size); 2126c2b4bb8cSChristoph Hellwig bd_set_nr_sectors(md->bdev, size); 21271da177e4SLinus Torvalds 2128cf222b37SAlasdair G Kergon dm_table_event_callback(t, event_callback, md); 21292ca3310eSAlasdair G Kergon 2130e6ee8c0bSKiyoshi Ueda /* 2131e6ee8c0bSKiyoshi Ueda * The queue hasn't been stopped yet, if the old table type wasn't 2132e6ee8c0bSKiyoshi Ueda * for request-based during suspension. So stop it to prevent 2133e6ee8c0bSKiyoshi Ueda * I/O mapping before resume. 2134e6ee8c0bSKiyoshi Ueda * This must be done before setting the queue restrictions, 2135e6ee8c0bSKiyoshi Ueda * because request-based dm may be run just after the setting. 2136e6ee8c0bSKiyoshi Ueda */ 2137978e51baSMike Snitzer if (request_based) 2138eca7ee6dSMike Snitzer dm_stop_queue(q); 2139978e51baSMike Snitzer 2140978e51baSMike Snitzer if (request_based || md->type == DM_TYPE_NVME_BIO_BASED) { 214116f12266SMike Snitzer /* 2142978e51baSMike Snitzer * Leverage the fact that request-based DM targets and 2143978e51baSMike Snitzer * NVMe bio based targets are immutable singletons 2144978e51baSMike Snitzer * - used to optimize both dm_request_fn and dm_mq_queue_rq; 2145978e51baSMike Snitzer * and __process_bio. 214616f12266SMike Snitzer */ 214716f12266SMike Snitzer md->immutable_target = dm_table_get_immutable_target(t); 214816f12266SMike Snitzer } 2149e6ee8c0bSKiyoshi Ueda 21502a2a4c51SJens Axboe ret = __bind_mempools(md, t); 21512a2a4c51SJens Axboe if (ret) { 21522a2a4c51SJens Axboe old_map = ERR_PTR(ret); 21532a2a4c51SJens Axboe goto out; 21542a2a4c51SJens Axboe } 2155e6ee8c0bSKiyoshi Ueda 2156a12f5d48SEric Dumazet old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 21571d3aa6f6SMike Snitzer rcu_assign_pointer(md->map, (void *)t); 215836a0456fSAlasdair G Kergon md->immutable_target_type = dm_table_get_immutable_target_type(t); 215936a0456fSAlasdair G Kergon 2160754c5fc7SMike Snitzer dm_table_set_restrictions(t, q, limits); 216141abc4e1SHannes Reinecke if (old_map) 216283d5e5b0SMikulas Patocka dm_sync_table(md); 21632ca3310eSAlasdair G Kergon 21642a2a4c51SJens Axboe out: 2165042d2a9bSAlasdair G Kergon return old_map; 21661da177e4SLinus Torvalds } 21671da177e4SLinus Torvalds 2168a7940155SAlasdair G Kergon /* 2169a7940155SAlasdair G Kergon * Returns unbound table for the caller to free. 2170a7940155SAlasdair G Kergon */ 2171a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md) 21721da177e4SLinus Torvalds { 2173a12f5d48SEric Dumazet struct dm_table *map = rcu_dereference_protected(md->map, 1); 21741da177e4SLinus Torvalds 21751da177e4SLinus Torvalds if (!map) 2176a7940155SAlasdair G Kergon return NULL; 21771da177e4SLinus Torvalds 21781da177e4SLinus Torvalds dm_table_event_callback(map, NULL, NULL); 21799cdb8520SMonam Agarwal RCU_INIT_POINTER(md->map, NULL); 218083d5e5b0SMikulas Patocka dm_sync_table(md); 2181a7940155SAlasdair G Kergon 2182a7940155SAlasdair G Kergon return map; 21831da177e4SLinus Torvalds } 21841da177e4SLinus Torvalds 21851da177e4SLinus Torvalds /* 21861da177e4SLinus Torvalds * Constructor for a new device. 21871da177e4SLinus Torvalds */ 21882b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result) 21891da177e4SLinus Torvalds { 2190c12c9a3cSMike Snitzer int r; 21911da177e4SLinus Torvalds struct mapped_device *md; 21921da177e4SLinus Torvalds 21932b06cfffSAlasdair G Kergon md = alloc_dev(minor); 21941da177e4SLinus Torvalds if (!md) 21951da177e4SLinus Torvalds return -ENXIO; 21961da177e4SLinus Torvalds 2197c12c9a3cSMike Snitzer r = dm_sysfs_init(md); 2198c12c9a3cSMike Snitzer if (r) { 2199c12c9a3cSMike Snitzer free_dev(md); 2200c12c9a3cSMike Snitzer return r; 2201c12c9a3cSMike Snitzer } 2202784aae73SMilan Broz 22031da177e4SLinus Torvalds *result = md; 22041da177e4SLinus Torvalds return 0; 22051da177e4SLinus Torvalds } 22061da177e4SLinus Torvalds 2207a5664dadSMike Snitzer /* 2208a5664dadSMike Snitzer * Functions to manage md->type. 2209a5664dadSMike Snitzer * All are required to hold md->type_lock. 2210a5664dadSMike Snitzer */ 2211a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md) 2212a5664dadSMike Snitzer { 2213a5664dadSMike Snitzer mutex_lock(&md->type_lock); 2214a5664dadSMike Snitzer } 2215a5664dadSMike Snitzer 2216a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md) 2217a5664dadSMike Snitzer { 2218a5664dadSMike Snitzer mutex_unlock(&md->type_lock); 2219a5664dadSMike Snitzer } 2220a5664dadSMike Snitzer 22217e0d574fSBart Van Assche void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type) 2222a5664dadSMike Snitzer { 222300c4fc3bSMike Snitzer BUG_ON(!mutex_is_locked(&md->type_lock)); 2224a5664dadSMike Snitzer md->type = type; 2225a5664dadSMike Snitzer } 2226a5664dadSMike Snitzer 22277e0d574fSBart Van Assche enum dm_queue_mode dm_get_md_type(struct mapped_device *md) 2228a5664dadSMike Snitzer { 2229a5664dadSMike Snitzer return md->type; 2230a5664dadSMike Snitzer } 2231a5664dadSMike Snitzer 223236a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md) 223336a0456fSAlasdair G Kergon { 223436a0456fSAlasdair G Kergon return md->immutable_target_type; 223536a0456fSAlasdair G Kergon } 223636a0456fSAlasdair G Kergon 22374a0b4ddfSMike Snitzer /* 2238f84cb8a4SMike Snitzer * The queue_limits are only valid as long as you have a reference 2239f84cb8a4SMike Snitzer * count on 'md'. 2240f84cb8a4SMike Snitzer */ 2241f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md) 2242f84cb8a4SMike Snitzer { 2243f84cb8a4SMike Snitzer BUG_ON(!atomic_read(&md->holders)); 2244f84cb8a4SMike Snitzer return &md->queue->limits; 2245f84cb8a4SMike Snitzer } 2246f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits); 2247f84cb8a4SMike Snitzer 22484a0b4ddfSMike Snitzer /* 22494a0b4ddfSMike Snitzer * Setup the DM device's queue based on md's type 22504a0b4ddfSMike Snitzer */ 2251591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t) 22524a0b4ddfSMike Snitzer { 2253bfebd1cdSMike Snitzer int r; 2254c100ec49SMike Snitzer struct queue_limits limits; 22557e0d574fSBart Van Assche enum dm_queue_mode type = dm_get_md_type(md); 2256bfebd1cdSMike Snitzer 2257545ed20eSToshi Kani switch (type) { 2258bfebd1cdSMike Snitzer case DM_TYPE_REQUEST_BASED: 2259e83068a5SMike Snitzer r = dm_mq_init_request_queue(md, t); 2260bfebd1cdSMike Snitzer if (r) { 2261eca7ee6dSMike Snitzer DMERR("Cannot initialize queue for request-based dm-mq mapped device"); 2262bfebd1cdSMike Snitzer return r; 2263bfebd1cdSMike Snitzer } 2264bfebd1cdSMike Snitzer break; 2265bfebd1cdSMike Snitzer case DM_TYPE_BIO_BASED: 2266545ed20eSToshi Kani case DM_TYPE_DAX_BIO_BASED: 2267978e51baSMike Snitzer case DM_TYPE_NVME_BIO_BASED: 2268bfebd1cdSMike Snitzer break; 22697e0d574fSBart Van Assche case DM_TYPE_NONE: 22707e0d574fSBart Van Assche WARN_ON_ONCE(true); 22717e0d574fSBart Van Assche break; 2272ff36ab34SMike Snitzer } 22734a0b4ddfSMike Snitzer 2274c100ec49SMike Snitzer r = dm_calculate_queue_limits(t, &limits); 2275c100ec49SMike Snitzer if (r) { 2276c100ec49SMike Snitzer DMERR("Cannot calculate initial queue limits"); 2277c100ec49SMike Snitzer return r; 2278c100ec49SMike Snitzer } 2279c100ec49SMike Snitzer dm_table_set_restrictions(t, md->queue, &limits); 2280c100ec49SMike Snitzer blk_register_queue(md->disk); 2281c100ec49SMike Snitzer 22824a0b4ddfSMike Snitzer return 0; 22834a0b4ddfSMike Snitzer } 22844a0b4ddfSMike Snitzer 22852bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev) 22861da177e4SLinus Torvalds { 22871da177e4SLinus Torvalds struct mapped_device *md; 22881da177e4SLinus Torvalds unsigned minor = MINOR(dev); 22891da177e4SLinus Torvalds 22901da177e4SLinus Torvalds if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 22911da177e4SLinus Torvalds return NULL; 22921da177e4SLinus Torvalds 2293f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 22941da177e4SLinus Torvalds 22951da177e4SLinus Torvalds md = idr_find(&_minor_idr, minor); 229649de5769SMike Snitzer if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) || 229749de5769SMike Snitzer test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { 2298637842cfSDavid Teigland md = NULL; 2299fba9f90eSJeff Mahoney goto out; 2300fba9f90eSJeff Mahoney } 23012bec1f4aSMikulas Patocka dm_get(md); 2302fba9f90eSJeff Mahoney out: 2303f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 23041da177e4SLinus Torvalds 2305637842cfSDavid Teigland return md; 2306637842cfSDavid Teigland } 23073cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md); 2308d229a958SDavid Teigland 23099ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md) 2310637842cfSDavid Teigland { 23119ade92a9SAlasdair G Kergon return md->interface_ptr; 23121da177e4SLinus Torvalds } 23131da177e4SLinus Torvalds 23141da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr) 23151da177e4SLinus Torvalds { 23161da177e4SLinus Torvalds md->interface_ptr = ptr; 23171da177e4SLinus Torvalds } 23181da177e4SLinus Torvalds 23191da177e4SLinus Torvalds void dm_get(struct mapped_device *md) 23201da177e4SLinus Torvalds { 23211da177e4SLinus Torvalds atomic_inc(&md->holders); 23223f77316dSKiyoshi Ueda BUG_ON(test_bit(DMF_FREEING, &md->flags)); 23231da177e4SLinus Torvalds } 23241da177e4SLinus Torvalds 232509ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md) 232609ee96b2SMikulas Patocka { 232709ee96b2SMikulas Patocka spin_lock(&_minor_lock); 232809ee96b2SMikulas Patocka if (test_bit(DMF_FREEING, &md->flags)) { 232909ee96b2SMikulas Patocka spin_unlock(&_minor_lock); 233009ee96b2SMikulas Patocka return -EBUSY; 233109ee96b2SMikulas Patocka } 233209ee96b2SMikulas Patocka dm_get(md); 233309ee96b2SMikulas Patocka spin_unlock(&_minor_lock); 233409ee96b2SMikulas Patocka return 0; 233509ee96b2SMikulas Patocka } 233609ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold); 233709ee96b2SMikulas Patocka 233872d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md) 233972d94861SAlasdair G Kergon { 234072d94861SAlasdair G Kergon return md->name; 234172d94861SAlasdair G Kergon } 234272d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name); 234372d94861SAlasdair G Kergon 23443f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait) 23451da177e4SLinus Torvalds { 23461134e5aeSMike Anderson struct dm_table *map; 234783d5e5b0SMikulas Patocka int srcu_idx; 23481da177e4SLinus Torvalds 23493f77316dSKiyoshi Ueda might_sleep(); 2350fba9f90eSJeff Mahoney 235163a4f065SMike Snitzer spin_lock(&_minor_lock); 23523f77316dSKiyoshi Ueda idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md)))); 2353fba9f90eSJeff Mahoney set_bit(DMF_FREEING, &md->flags); 2354f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 23553f77316dSKiyoshi Ueda 2356c12c9a3cSMike Snitzer blk_set_queue_dying(md->queue); 23573b785fbcSBart Van Assche 2358ab7c7bb6SMikulas Patocka /* 2359ab7c7bb6SMikulas Patocka * Take suspend_lock so that presuspend and postsuspend methods 2360ab7c7bb6SMikulas Patocka * do not race with internal suspend. 2361ab7c7bb6SMikulas Patocka */ 2362ab7c7bb6SMikulas Patocka mutex_lock(&md->suspend_lock); 23632a708cffSJunichi Nomura map = dm_get_live_table(md, &srcu_idx); 23644f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) { 23651da177e4SLinus Torvalds dm_table_presuspend_targets(map); 2366adc0daadSMikulas Patocka set_bit(DMF_SUSPENDED, &md->flags); 23675df96f2bSMikulas Patocka set_bit(DMF_POST_SUSPENDING, &md->flags); 23681da177e4SLinus Torvalds dm_table_postsuspend_targets(map); 23691da177e4SLinus Torvalds } 237083d5e5b0SMikulas Patocka /* dm_put_live_table must be before msleep, otherwise deadlock is possible */ 237183d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 23722a708cffSJunichi Nomura mutex_unlock(&md->suspend_lock); 237383d5e5b0SMikulas Patocka 23743f77316dSKiyoshi Ueda /* 23753f77316dSKiyoshi Ueda * Rare, but there may be I/O requests still going to complete, 23763f77316dSKiyoshi Ueda * for example. Wait for all references to disappear. 23773f77316dSKiyoshi Ueda * No one should increment the reference count of the mapped_device, 23783f77316dSKiyoshi Ueda * after the mapped_device state becomes DMF_FREEING. 23793f77316dSKiyoshi Ueda */ 23803f77316dSKiyoshi Ueda if (wait) 23813f77316dSKiyoshi Ueda while (atomic_read(&md->holders)) 23823f77316dSKiyoshi Ueda msleep(1); 23833f77316dSKiyoshi Ueda else if (atomic_read(&md->holders)) 23843f77316dSKiyoshi Ueda DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)", 23853f77316dSKiyoshi Ueda dm_device_name(md), atomic_read(&md->holders)); 23863f77316dSKiyoshi Ueda 2387784aae73SMilan Broz dm_sysfs_exit(md); 2388a7940155SAlasdair G Kergon dm_table_destroy(__unbind(md)); 23891da177e4SLinus Torvalds free_dev(md); 23901da177e4SLinus Torvalds } 23913f77316dSKiyoshi Ueda 23923f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md) 23933f77316dSKiyoshi Ueda { 23943f77316dSKiyoshi Ueda __dm_destroy(md, true); 23953f77316dSKiyoshi Ueda } 23963f77316dSKiyoshi Ueda 23973f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md) 23983f77316dSKiyoshi Ueda { 23993f77316dSKiyoshi Ueda __dm_destroy(md, false); 24003f77316dSKiyoshi Ueda } 24013f77316dSKiyoshi Ueda 24023f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md) 24033f77316dSKiyoshi Ueda { 24043f77316dSKiyoshi Ueda atomic_dec(&md->holders); 24051da177e4SLinus Torvalds } 240679eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put); 24071da177e4SLinus Torvalds 240885067747SMing Lei static bool md_in_flight_bios(struct mapped_device *md) 240985067747SMing Lei { 241085067747SMing Lei int cpu; 241185067747SMing Lei struct hd_struct *part = &dm_disk(md)->part0; 241285067747SMing Lei long sum = 0; 241385067747SMing Lei 241485067747SMing Lei for_each_possible_cpu(cpu) { 241585067747SMing Lei sum += part_stat_local_read_cpu(part, in_flight[0], cpu); 241685067747SMing Lei sum += part_stat_local_read_cpu(part, in_flight[1], cpu); 241785067747SMing Lei } 241885067747SMing Lei 241985067747SMing Lei return sum != 0; 242085067747SMing Lei } 242185067747SMing Lei 242285067747SMing Lei static int dm_wait_for_bios_completion(struct mapped_device *md, long task_state) 242346125c1cSMilan Broz { 242446125c1cSMilan Broz int r = 0; 24259f4c3f87SBart Van Assche DEFINE_WAIT(wait); 242646125c1cSMilan Broz 242785067747SMing Lei while (true) { 24289f4c3f87SBart Van Assche prepare_to_wait(&md->wait, &wait, task_state); 242946125c1cSMilan Broz 243085067747SMing Lei if (!md_in_flight_bios(md)) 243146125c1cSMilan Broz break; 243246125c1cSMilan Broz 2433e3fabdfdSBart Van Assche if (signal_pending_state(task_state, current)) { 243446125c1cSMilan Broz r = -EINTR; 243546125c1cSMilan Broz break; 243646125c1cSMilan Broz } 243746125c1cSMilan Broz 243846125c1cSMilan Broz io_schedule(); 243946125c1cSMilan Broz } 24409f4c3f87SBart Van Assche finish_wait(&md->wait, &wait); 2441b44ebeb0SMikulas Patocka 244246125c1cSMilan Broz return r; 244346125c1cSMilan Broz } 244446125c1cSMilan Broz 244585067747SMing Lei static int dm_wait_for_completion(struct mapped_device *md, long task_state) 244685067747SMing Lei { 244785067747SMing Lei int r = 0; 244885067747SMing Lei 244985067747SMing Lei if (!queue_is_mq(md->queue)) 245085067747SMing Lei return dm_wait_for_bios_completion(md, task_state); 245185067747SMing Lei 245285067747SMing Lei while (true) { 245385067747SMing Lei if (!blk_mq_queue_inflight(md->queue)) 245485067747SMing Lei break; 245585067747SMing Lei 245685067747SMing Lei if (signal_pending_state(task_state, current)) { 245785067747SMing Lei r = -EINTR; 245885067747SMing Lei break; 245985067747SMing Lei } 246085067747SMing Lei 246185067747SMing Lei msleep(5); 246285067747SMing Lei } 246385067747SMing Lei 246485067747SMing Lei return r; 246585067747SMing Lei } 246685067747SMing Lei 24671da177e4SLinus Torvalds /* 24681da177e4SLinus Torvalds * Process the deferred bios 24691da177e4SLinus Torvalds */ 2470ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work) 24711da177e4SLinus Torvalds { 2472ef208587SMikulas Patocka struct mapped_device *md = container_of(work, struct mapped_device, 2473ef208587SMikulas Patocka work); 24746d6f10dfSMilan Broz struct bio *c; 247583d5e5b0SMikulas Patocka int srcu_idx; 247683d5e5b0SMikulas Patocka struct dm_table *map; 24771da177e4SLinus Torvalds 247883d5e5b0SMikulas Patocka map = dm_get_live_table(md, &srcu_idx); 2479ef208587SMikulas Patocka 24803b00b203SMikulas Patocka while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 2481022c2611SMikulas Patocka spin_lock_irq(&md->deferred_lock); 2482022c2611SMikulas Patocka c = bio_list_pop(&md->deferred); 2483022c2611SMikulas Patocka spin_unlock_irq(&md->deferred_lock); 2484022c2611SMikulas Patocka 24856a8736d1STejun Heo if (!c) 2486df12ee99SAlasdair G Kergon break; 248773d410c0SMilan Broz 2488e6ee8c0bSKiyoshi Ueda if (dm_request_based(md)) 2489ed00aabdSChristoph Hellwig (void) submit_bio_noacct(c); 2490af7e466aSMikulas Patocka else 24916548c7c5SMike Snitzer (void) dm_process_bio(md, map, c); 2492e6ee8c0bSKiyoshi Ueda } 24933b00b203SMikulas Patocka 249483d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 24951da177e4SLinus Torvalds } 24961da177e4SLinus Torvalds 24979a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md) 2498304f3f6aSMilan Broz { 24993b00b203SMikulas Patocka clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 25004e857c58SPeter Zijlstra smp_mb__after_atomic(); 250153d5914fSMikulas Patocka queue_work(md->wq, &md->work); 2502304f3f6aSMilan Broz } 2503304f3f6aSMilan Broz 25041da177e4SLinus Torvalds /* 2505042d2a9bSAlasdair G Kergon * Swap in a new table, returning the old one for the caller to destroy. 25061da177e4SLinus Torvalds */ 2507042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table) 25081da177e4SLinus Torvalds { 250987eb5b21SMike Christie struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL); 2510754c5fc7SMike Snitzer struct queue_limits limits; 2511042d2a9bSAlasdair G Kergon int r; 25121da177e4SLinus Torvalds 2513e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 25141da177e4SLinus Torvalds 25151da177e4SLinus Torvalds /* device must be suspended */ 25164f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) 251793c534aeSAlasdair G Kergon goto out; 25181da177e4SLinus Torvalds 25193ae70656SMike Snitzer /* 25203ae70656SMike Snitzer * If the new table has no data devices, retain the existing limits. 25213ae70656SMike Snitzer * This helps multipath with queue_if_no_path if all paths disappear, 25223ae70656SMike Snitzer * then new I/O is queued based on these limits, and then some paths 25233ae70656SMike Snitzer * reappear. 25243ae70656SMike Snitzer */ 25253ae70656SMike Snitzer if (dm_table_has_no_data_devices(table)) { 252683d5e5b0SMikulas Patocka live_map = dm_get_live_table_fast(md); 25273ae70656SMike Snitzer if (live_map) 25283ae70656SMike Snitzer limits = md->queue->limits; 252983d5e5b0SMikulas Patocka dm_put_live_table_fast(md); 25303ae70656SMike Snitzer } 25313ae70656SMike Snitzer 253287eb5b21SMike Christie if (!live_map) { 2533754c5fc7SMike Snitzer r = dm_calculate_queue_limits(table, &limits); 2534042d2a9bSAlasdair G Kergon if (r) { 2535042d2a9bSAlasdair G Kergon map = ERR_PTR(r); 2536754c5fc7SMike Snitzer goto out; 2537042d2a9bSAlasdair G Kergon } 253887eb5b21SMike Christie } 2539754c5fc7SMike Snitzer 2540042d2a9bSAlasdair G Kergon map = __bind(md, table, &limits); 254162e08243SMikulas Patocka dm_issue_global_event(); 25421da177e4SLinus Torvalds 254393c534aeSAlasdair G Kergon out: 2544e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 2545042d2a9bSAlasdair G Kergon return map; 25461da177e4SLinus Torvalds } 25471da177e4SLinus Torvalds 25481da177e4SLinus Torvalds /* 25491da177e4SLinus Torvalds * Functions to lock and unlock any filesystem running on the 25501da177e4SLinus Torvalds * device. 25511da177e4SLinus Torvalds */ 25522ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md) 25531da177e4SLinus Torvalds { 2554e39e2e95SAlasdair G Kergon int r; 25551da177e4SLinus Torvalds 25561da177e4SLinus Torvalds WARN_ON(md->frozen_sb); 2557dfbe03f6SAlasdair G Kergon 2558db8fef4fSMikulas Patocka md->frozen_sb = freeze_bdev(md->bdev); 2559dfbe03f6SAlasdair G Kergon if (IS_ERR(md->frozen_sb)) { 2560cf222b37SAlasdair G Kergon r = PTR_ERR(md->frozen_sb); 2561e39e2e95SAlasdair G Kergon md->frozen_sb = NULL; 2562e39e2e95SAlasdair G Kergon return r; 2563dfbe03f6SAlasdair G Kergon } 2564dfbe03f6SAlasdair G Kergon 2565aa8d7c2fSAlasdair G Kergon set_bit(DMF_FROZEN, &md->flags); 2566aa8d7c2fSAlasdair G Kergon 25671da177e4SLinus Torvalds return 0; 25681da177e4SLinus Torvalds } 25691da177e4SLinus Torvalds 25702ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md) 25711da177e4SLinus Torvalds { 2572aa8d7c2fSAlasdair G Kergon if (!test_bit(DMF_FROZEN, &md->flags)) 2573aa8d7c2fSAlasdair G Kergon return; 2574aa8d7c2fSAlasdair G Kergon 2575db8fef4fSMikulas Patocka thaw_bdev(md->bdev, md->frozen_sb); 25761da177e4SLinus Torvalds md->frozen_sb = NULL; 2577aa8d7c2fSAlasdair G Kergon clear_bit(DMF_FROZEN, &md->flags); 25781da177e4SLinus Torvalds } 25791da177e4SLinus Torvalds 25801da177e4SLinus Torvalds /* 2581b48633f8SBart Van Assche * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG 2582b48633f8SBart Van Assche * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE 2583b48633f8SBart Van Assche * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY 2584b48633f8SBart Van Assche * 2585ffcc3936SMike Snitzer * If __dm_suspend returns 0, the device is completely quiescent 2586ffcc3936SMike Snitzer * now. There is no request-processing activity. All new requests 2587ffcc3936SMike Snitzer * are being added to md->deferred list. 2588cec47e3dSKiyoshi Ueda */ 2589ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map, 2590b48633f8SBart Van Assche unsigned suspend_flags, long task_state, 2591eaf9a736SMike Snitzer int dmf_suspended_flag) 25921da177e4SLinus Torvalds { 2593ffcc3936SMike Snitzer bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG; 2594ffcc3936SMike Snitzer bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG; 2595ffcc3936SMike Snitzer int r; 2596cf222b37SAlasdair G Kergon 25975a8f1f80SBart Van Assche lockdep_assert_held(&md->suspend_lock); 25985a8f1f80SBart Van Assche 25992e93ccc1SKiyoshi Ueda /* 26002e93ccc1SKiyoshi Ueda * DMF_NOFLUSH_SUSPENDING must be set before presuspend. 26012e93ccc1SKiyoshi Ueda * This flag is cleared before dm_suspend returns. 26022e93ccc1SKiyoshi Ueda */ 26032e93ccc1SKiyoshi Ueda if (noflush) 26042e93ccc1SKiyoshi Ueda set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 260586331f39SBart Van Assche else 2606ac75b09fSMike Snitzer DMDEBUG("%s: suspending with flush", dm_device_name(md)); 26072e93ccc1SKiyoshi Ueda 2608d67ee213SMike Snitzer /* 2609d67ee213SMike Snitzer * This gets reverted if there's an error later and the targets 2610d67ee213SMike Snitzer * provide the .presuspend_undo hook. 2611d67ee213SMike Snitzer */ 26121da177e4SLinus Torvalds dm_table_presuspend_targets(map); 26131da177e4SLinus Torvalds 26142e93ccc1SKiyoshi Ueda /* 26159f518b27SKiyoshi Ueda * Flush I/O to the device. 26169f518b27SKiyoshi Ueda * Any I/O submitted after lock_fs() may not be flushed. 26179f518b27SKiyoshi Ueda * noflush takes precedence over do_lockfs. 26189f518b27SKiyoshi Ueda * (lock_fs() flushes I/Os and waits for them to complete.) 26192e93ccc1SKiyoshi Ueda */ 262032a926daSMikulas Patocka if (!noflush && do_lockfs) { 26212ca3310eSAlasdair G Kergon r = lock_fs(md); 2622d67ee213SMike Snitzer if (r) { 2623d67ee213SMike Snitzer dm_table_presuspend_undo_targets(map); 2624ffcc3936SMike Snitzer return r; 2625aa8d7c2fSAlasdair G Kergon } 2626d67ee213SMike Snitzer } 26271da177e4SLinus Torvalds 26281da177e4SLinus Torvalds /* 26293b00b203SMikulas Patocka * Here we must make sure that no processes are submitting requests 26303b00b203SMikulas Patocka * to target drivers i.e. no one may be executing 26313b00b203SMikulas Patocka * __split_and_process_bio. This is called from dm_request and 26323b00b203SMikulas Patocka * dm_wq_work. 26333b00b203SMikulas Patocka * 26343b00b203SMikulas Patocka * To get all processes out of __split_and_process_bio in dm_request, 26353b00b203SMikulas Patocka * we take the write lock. To prevent any process from reentering 26366a8736d1STejun Heo * __split_and_process_bio from dm_request and quiesce the thread 26376a8736d1STejun Heo * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call 26386a8736d1STejun Heo * flush_workqueue(md->wq). 26391da177e4SLinus Torvalds */ 26401eb787ecSAlasdair G Kergon set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 264141abc4e1SHannes Reinecke if (map) 264283d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 26431da177e4SLinus Torvalds 2644d0bcb878SKiyoshi Ueda /* 264529e4013dSTejun Heo * Stop md->queue before flushing md->wq in case request-based 264629e4013dSTejun Heo * dm defers requests to md->wq from md->queue. 2647d0bcb878SKiyoshi Ueda */ 26486a23e05cSJens Axboe if (dm_request_based(md)) 2649eca7ee6dSMike Snitzer dm_stop_queue(md->queue); 2650cec47e3dSKiyoshi Ueda 2651d0bcb878SKiyoshi Ueda flush_workqueue(md->wq); 2652d0bcb878SKiyoshi Ueda 26531da177e4SLinus Torvalds /* 26543b00b203SMikulas Patocka * At this point no more requests are entering target request routines. 26553b00b203SMikulas Patocka * We call dm_wait_for_completion to wait for all existing requests 26563b00b203SMikulas Patocka * to finish. 26571da177e4SLinus Torvalds */ 2658b48633f8SBart Van Assche r = dm_wait_for_completion(md, task_state); 2659eaf9a736SMike Snitzer if (!r) 2660eaf9a736SMike Snitzer set_bit(dmf_suspended_flag, &md->flags); 26611da177e4SLinus Torvalds 26626d6f10dfSMilan Broz if (noflush) 2663022c2611SMikulas Patocka clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 266441abc4e1SHannes Reinecke if (map) 266583d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 26662e93ccc1SKiyoshi Ueda 26671da177e4SLinus Torvalds /* were we interrupted ? */ 266846125c1cSMilan Broz if (r < 0) { 26699a1fb464SMikulas Patocka dm_queue_flush(md); 267073d410c0SMilan Broz 2671cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 2672eca7ee6dSMike Snitzer dm_start_queue(md->queue); 2673cec47e3dSKiyoshi Ueda 26742ca3310eSAlasdair G Kergon unlock_fs(md); 2675d67ee213SMike Snitzer dm_table_presuspend_undo_targets(map); 2676ffcc3936SMike Snitzer /* pushback list is already flushed, so skip flush */ 2677ffcc3936SMike Snitzer } 2678ffcc3936SMike Snitzer 2679ffcc3936SMike Snitzer return r; 26802ca3310eSAlasdair G Kergon } 26812ca3310eSAlasdair G Kergon 26823b00b203SMikulas Patocka /* 2683ffcc3936SMike Snitzer * We need to be able to change a mapping table under a mounted 2684ffcc3936SMike Snitzer * filesystem. For example we might want to move some data in 2685ffcc3936SMike Snitzer * the background. Before the table can be swapped with 2686ffcc3936SMike Snitzer * dm_bind_table, dm_suspend must be called to flush any in 2687ffcc3936SMike Snitzer * flight bios and ensure that any further io gets deferred. 26883b00b203SMikulas Patocka */ 2689ffcc3936SMike Snitzer /* 2690ffcc3936SMike Snitzer * Suspend mechanism in request-based dm. 2691ffcc3936SMike Snitzer * 2692ffcc3936SMike Snitzer * 1. Flush all I/Os by lock_fs() if needed. 2693ffcc3936SMike Snitzer * 2. Stop dispatching any I/O by stopping the request_queue. 2694ffcc3936SMike Snitzer * 3. Wait for all in-flight I/Os to be completed or requeued. 2695ffcc3936SMike Snitzer * 2696ffcc3936SMike Snitzer * To abort suspend, start the request_queue. 2697ffcc3936SMike Snitzer */ 2698ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags) 2699ffcc3936SMike Snitzer { 2700ffcc3936SMike Snitzer struct dm_table *map = NULL; 2701ffcc3936SMike Snitzer int r = 0; 2702ffcc3936SMike Snitzer 2703ffcc3936SMike Snitzer retry: 2704ffcc3936SMike Snitzer mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); 2705ffcc3936SMike Snitzer 2706ffcc3936SMike Snitzer if (dm_suspended_md(md)) { 2707ffcc3936SMike Snitzer r = -EINVAL; 2708ffcc3936SMike Snitzer goto out_unlock; 2709ffcc3936SMike Snitzer } 2710ffcc3936SMike Snitzer 2711ffcc3936SMike Snitzer if (dm_suspended_internally_md(md)) { 2712ffcc3936SMike Snitzer /* already internally suspended, wait for internal resume */ 2713ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2714ffcc3936SMike Snitzer r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); 2715ffcc3936SMike Snitzer if (r) 2716ffcc3936SMike Snitzer return r; 2717ffcc3936SMike Snitzer goto retry; 2718ffcc3936SMike Snitzer } 2719ffcc3936SMike Snitzer 2720a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2721ffcc3936SMike Snitzer 2722eaf9a736SMike Snitzer r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED); 2723ffcc3936SMike Snitzer if (r) 2724ffcc3936SMike Snitzer goto out_unlock; 27253b00b203SMikulas Patocka 27265df96f2bSMikulas Patocka set_bit(DMF_POST_SUSPENDING, &md->flags); 27274d4471cbSKiyoshi Ueda dm_table_postsuspend_targets(map); 27285df96f2bSMikulas Patocka clear_bit(DMF_POST_SUSPENDING, &md->flags); 27294d4471cbSKiyoshi Ueda 2730d287483dSAlasdair G Kergon out_unlock: 2731e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 2732cf222b37SAlasdair G Kergon return r; 27331da177e4SLinus Torvalds } 27341da177e4SLinus Torvalds 2735ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map) 27361da177e4SLinus Torvalds { 2737ffcc3936SMike Snitzer if (map) { 2738ffcc3936SMike Snitzer int r = dm_table_resume_targets(map); 27398757b776SMilan Broz if (r) 2740ffcc3936SMike Snitzer return r; 2741ffcc3936SMike Snitzer } 27422ca3310eSAlasdair G Kergon 27439a1fb464SMikulas Patocka dm_queue_flush(md); 27442ca3310eSAlasdair G Kergon 2745cec47e3dSKiyoshi Ueda /* 2746cec47e3dSKiyoshi Ueda * Flushing deferred I/Os must be done after targets are resumed 2747cec47e3dSKiyoshi Ueda * so that mapping of targets can work correctly. 2748cec47e3dSKiyoshi Ueda * Request-based dm is queueing the deferred I/Os in its request_queue. 2749cec47e3dSKiyoshi Ueda */ 2750cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 2751eca7ee6dSMike Snitzer dm_start_queue(md->queue); 2752cec47e3dSKiyoshi Ueda 27532ca3310eSAlasdair G Kergon unlock_fs(md); 27542ca3310eSAlasdair G Kergon 2755ffcc3936SMike Snitzer return 0; 2756ffcc3936SMike Snitzer } 2757ffcc3936SMike Snitzer 2758ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md) 2759ffcc3936SMike Snitzer { 27608dc23658SMinfei Huang int r; 2761ffcc3936SMike Snitzer struct dm_table *map = NULL; 2762ffcc3936SMike Snitzer 2763ffcc3936SMike Snitzer retry: 27648dc23658SMinfei Huang r = -EINVAL; 2765ffcc3936SMike Snitzer mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); 2766ffcc3936SMike Snitzer 2767ffcc3936SMike Snitzer if (!dm_suspended_md(md)) 2768ffcc3936SMike Snitzer goto out; 2769ffcc3936SMike Snitzer 2770ffcc3936SMike Snitzer if (dm_suspended_internally_md(md)) { 2771ffcc3936SMike Snitzer /* already internally suspended, wait for internal resume */ 2772ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2773ffcc3936SMike Snitzer r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); 2774ffcc3936SMike Snitzer if (r) 2775ffcc3936SMike Snitzer return r; 2776ffcc3936SMike Snitzer goto retry; 2777ffcc3936SMike Snitzer } 2778ffcc3936SMike Snitzer 2779a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2780ffcc3936SMike Snitzer if (!map || !dm_table_get_size(map)) 2781ffcc3936SMike Snitzer goto out; 2782ffcc3936SMike Snitzer 2783ffcc3936SMike Snitzer r = __dm_resume(md, map); 2784ffcc3936SMike Snitzer if (r) 2785ffcc3936SMike Snitzer goto out; 2786ffcc3936SMike Snitzer 27872ca3310eSAlasdair G Kergon clear_bit(DMF_SUSPENDED, &md->flags); 2788cf222b37SAlasdair G Kergon out: 2789e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 27902ca3310eSAlasdair G Kergon 2791cf222b37SAlasdair G Kergon return r; 27921da177e4SLinus Torvalds } 27931da177e4SLinus Torvalds 2794fd2ed4d2SMikulas Patocka /* 2795fd2ed4d2SMikulas Patocka * Internal suspend/resume works like userspace-driven suspend. It waits 2796fd2ed4d2SMikulas Patocka * until all bios finish and prevents issuing new bios to the target drivers. 2797fd2ed4d2SMikulas Patocka * It may be used only from the kernel. 2798fd2ed4d2SMikulas Patocka */ 2799fd2ed4d2SMikulas Patocka 2800ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags) 2801ffcc3936SMike Snitzer { 2802ffcc3936SMike Snitzer struct dm_table *map = NULL; 2803ffcc3936SMike Snitzer 28041ea0654eSBart Van Assche lockdep_assert_held(&md->suspend_lock); 28051ea0654eSBart Van Assche 280696b26c8cSMikulas Patocka if (md->internal_suspend_count++) 2807ffcc3936SMike Snitzer return; /* nested internal suspend */ 2808ffcc3936SMike Snitzer 2809ffcc3936SMike Snitzer if (dm_suspended_md(md)) { 2810ffcc3936SMike Snitzer set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2811ffcc3936SMike Snitzer return; /* nest suspend */ 2812ffcc3936SMike Snitzer } 2813ffcc3936SMike Snitzer 2814a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2815ffcc3936SMike Snitzer 2816ffcc3936SMike Snitzer /* 2817ffcc3936SMike Snitzer * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is 2818ffcc3936SMike Snitzer * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend 2819ffcc3936SMike Snitzer * would require changing .presuspend to return an error -- avoid this 2820ffcc3936SMike Snitzer * until there is a need for more elaborate variants of internal suspend. 2821ffcc3936SMike Snitzer */ 2822eaf9a736SMike Snitzer (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE, 2823eaf9a736SMike Snitzer DMF_SUSPENDED_INTERNALLY); 2824ffcc3936SMike Snitzer 28255df96f2bSMikulas Patocka set_bit(DMF_POST_SUSPENDING, &md->flags); 2826ffcc3936SMike Snitzer dm_table_postsuspend_targets(map); 28275df96f2bSMikulas Patocka clear_bit(DMF_POST_SUSPENDING, &md->flags); 2828ffcc3936SMike Snitzer } 2829ffcc3936SMike Snitzer 2830ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md) 2831ffcc3936SMike Snitzer { 283296b26c8cSMikulas Patocka BUG_ON(!md->internal_suspend_count); 283396b26c8cSMikulas Patocka 283496b26c8cSMikulas Patocka if (--md->internal_suspend_count) 2835ffcc3936SMike Snitzer return; /* resume from nested internal suspend */ 2836ffcc3936SMike Snitzer 2837ffcc3936SMike Snitzer if (dm_suspended_md(md)) 2838ffcc3936SMike Snitzer goto done; /* resume from nested suspend */ 2839ffcc3936SMike Snitzer 2840ffcc3936SMike Snitzer /* 2841ffcc3936SMike Snitzer * NOTE: existing callers don't need to call dm_table_resume_targets 2842ffcc3936SMike Snitzer * (which may fail -- so best to avoid it for now by passing NULL map) 2843ffcc3936SMike Snitzer */ 2844ffcc3936SMike Snitzer (void) __dm_resume(md, NULL); 2845ffcc3936SMike Snitzer 2846ffcc3936SMike Snitzer done: 2847ffcc3936SMike Snitzer clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2848ffcc3936SMike Snitzer smp_mb__after_atomic(); 2849ffcc3936SMike Snitzer wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY); 2850ffcc3936SMike Snitzer } 2851ffcc3936SMike Snitzer 2852ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md) 2853fd2ed4d2SMikulas Patocka { 2854fd2ed4d2SMikulas Patocka mutex_lock(&md->suspend_lock); 2855ffcc3936SMike Snitzer __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG); 2856ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2857ffcc3936SMike Snitzer } 2858ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush); 2859ffcc3936SMike Snitzer 2860ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md) 2861ffcc3936SMike Snitzer { 2862ffcc3936SMike Snitzer mutex_lock(&md->suspend_lock); 2863ffcc3936SMike Snitzer __dm_internal_resume(md); 2864ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2865ffcc3936SMike Snitzer } 2866ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume); 2867ffcc3936SMike Snitzer 2868ffcc3936SMike Snitzer /* 2869ffcc3936SMike Snitzer * Fast variants of internal suspend/resume hold md->suspend_lock, 2870ffcc3936SMike Snitzer * which prevents interaction with userspace-driven suspend. 2871ffcc3936SMike Snitzer */ 2872ffcc3936SMike Snitzer 2873ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md) 2874ffcc3936SMike Snitzer { 2875ffcc3936SMike Snitzer mutex_lock(&md->suspend_lock); 2876ffcc3936SMike Snitzer if (dm_suspended_md(md) || dm_suspended_internally_md(md)) 2877fd2ed4d2SMikulas Patocka return; 2878fd2ed4d2SMikulas Patocka 2879fd2ed4d2SMikulas Patocka set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 2880fd2ed4d2SMikulas Patocka synchronize_srcu(&md->io_barrier); 2881fd2ed4d2SMikulas Patocka flush_workqueue(md->wq); 2882fd2ed4d2SMikulas Patocka dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); 2883fd2ed4d2SMikulas Patocka } 2884b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast); 2885fd2ed4d2SMikulas Patocka 2886ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md) 2887fd2ed4d2SMikulas Patocka { 2888ffcc3936SMike Snitzer if (dm_suspended_md(md) || dm_suspended_internally_md(md)) 2889fd2ed4d2SMikulas Patocka goto done; 2890fd2ed4d2SMikulas Patocka 2891fd2ed4d2SMikulas Patocka dm_queue_flush(md); 2892fd2ed4d2SMikulas Patocka 2893fd2ed4d2SMikulas Patocka done: 2894fd2ed4d2SMikulas Patocka mutex_unlock(&md->suspend_lock); 2895fd2ed4d2SMikulas Patocka } 2896b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast); 2897fd2ed4d2SMikulas Patocka 28981da177e4SLinus Torvalds /*----------------------------------------------------------------- 28991da177e4SLinus Torvalds * Event notification. 29001da177e4SLinus Torvalds *---------------------------------------------------------------*/ 29013abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action, 290260935eb2SMilan Broz unsigned cookie) 290369267a30SAlasdair G Kergon { 29046958c1c6SMikulas Patocka int r; 29056958c1c6SMikulas Patocka unsigned noio_flag; 290660935eb2SMilan Broz char udev_cookie[DM_COOKIE_LENGTH]; 290760935eb2SMilan Broz char *envp[] = { udev_cookie, NULL }; 290860935eb2SMilan Broz 29096958c1c6SMikulas Patocka noio_flag = memalloc_noio_save(); 29106958c1c6SMikulas Patocka 291160935eb2SMilan Broz if (!cookie) 29126958c1c6SMikulas Patocka r = kobject_uevent(&disk_to_dev(md->disk)->kobj, action); 291360935eb2SMilan Broz else { 291460935eb2SMilan Broz snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u", 291560935eb2SMilan Broz DM_COOKIE_ENV_VAR_NAME, cookie); 29166958c1c6SMikulas Patocka r = kobject_uevent_env(&disk_to_dev(md->disk)->kobj, 29173abf85b5SPeter Rajnoha action, envp); 291860935eb2SMilan Broz } 29196958c1c6SMikulas Patocka 29206958c1c6SMikulas Patocka memalloc_noio_restore(noio_flag); 29216958c1c6SMikulas Patocka 29226958c1c6SMikulas Patocka return r; 292369267a30SAlasdair G Kergon } 292469267a30SAlasdair G Kergon 29257a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md) 29267a8c3d3bSMike Anderson { 29277a8c3d3bSMike Anderson return atomic_add_return(1, &md->uevent_seq); 29287a8c3d3bSMike Anderson } 29297a8c3d3bSMike Anderson 29301da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md) 29311da177e4SLinus Torvalds { 29321da177e4SLinus Torvalds return atomic_read(&md->event_nr); 29331da177e4SLinus Torvalds } 29341da177e4SLinus Torvalds 29351da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr) 29361da177e4SLinus Torvalds { 29371da177e4SLinus Torvalds return wait_event_interruptible(md->eventq, 29381da177e4SLinus Torvalds (event_nr != atomic_read(&md->event_nr))); 29391da177e4SLinus Torvalds } 29401da177e4SLinus Torvalds 29417a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist) 29427a8c3d3bSMike Anderson { 29437a8c3d3bSMike Anderson unsigned long flags; 29447a8c3d3bSMike Anderson 29457a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 29467a8c3d3bSMike Anderson list_add(elist, &md->uevent_list); 29477a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 29487a8c3d3bSMike Anderson } 29497a8c3d3bSMike Anderson 29501da177e4SLinus Torvalds /* 29511da177e4SLinus Torvalds * The gendisk is only valid as long as you have a reference 29521da177e4SLinus Torvalds * count on 'md'. 29531da177e4SLinus Torvalds */ 29541da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md) 29551da177e4SLinus Torvalds { 29561da177e4SLinus Torvalds return md->disk; 29571da177e4SLinus Torvalds } 295865ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk); 29591da177e4SLinus Torvalds 2960784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md) 2961784aae73SMilan Broz { 29622995fa78SMikulas Patocka return &md->kobj_holder.kobj; 2963784aae73SMilan Broz } 2964784aae73SMilan Broz 2965784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj) 2966784aae73SMilan Broz { 2967784aae73SMilan Broz struct mapped_device *md; 2968784aae73SMilan Broz 29692995fa78SMikulas Patocka md = container_of(kobj, struct mapped_device, kobj_holder.kobj); 2970784aae73SMilan Broz 2971b9a41d21SHou Tao spin_lock(&_minor_lock); 2972b9a41d21SHou Tao if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { 2973b9a41d21SHou Tao md = NULL; 2974b9a41d21SHou Tao goto out; 2975b9a41d21SHou Tao } 2976784aae73SMilan Broz dm_get(md); 2977b9a41d21SHou Tao out: 2978b9a41d21SHou Tao spin_unlock(&_minor_lock); 2979b9a41d21SHou Tao 2980784aae73SMilan Broz return md; 2981784aae73SMilan Broz } 2982784aae73SMilan Broz 29834f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md) 29841da177e4SLinus Torvalds { 29851da177e4SLinus Torvalds return test_bit(DMF_SUSPENDED, &md->flags); 29861da177e4SLinus Torvalds } 29871da177e4SLinus Torvalds 29885df96f2bSMikulas Patocka static int dm_post_suspending_md(struct mapped_device *md) 29895df96f2bSMikulas Patocka { 29905df96f2bSMikulas Patocka return test_bit(DMF_POST_SUSPENDING, &md->flags); 29915df96f2bSMikulas Patocka } 29925df96f2bSMikulas Patocka 2993ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md) 2994ffcc3936SMike Snitzer { 2995ffcc3936SMike Snitzer return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2996ffcc3936SMike Snitzer } 2997ffcc3936SMike Snitzer 29982c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md) 29992c140a24SMikulas Patocka { 30002c140a24SMikulas Patocka return test_bit(DMF_DEFERRED_REMOVE, &md->flags); 30012c140a24SMikulas Patocka } 30022c140a24SMikulas Patocka 300364dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti) 300464dbce58SKiyoshi Ueda { 3005ecdb2e25SKiyoshi Ueda return dm_suspended_md(dm_table_get_md(ti->table)); 300664dbce58SKiyoshi Ueda } 300764dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended); 300864dbce58SKiyoshi Ueda 30095df96f2bSMikulas Patocka int dm_post_suspending(struct dm_target *ti) 30105df96f2bSMikulas Patocka { 30115df96f2bSMikulas Patocka return dm_post_suspending_md(dm_table_get_md(ti->table)); 30125df96f2bSMikulas Patocka } 30135df96f2bSMikulas Patocka EXPORT_SYMBOL_GPL(dm_post_suspending); 30145df96f2bSMikulas Patocka 30152e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti) 30162e93ccc1SKiyoshi Ueda { 3017ecdb2e25SKiyoshi Ueda return __noflush_suspending(dm_table_get_md(ti->table)); 30182e93ccc1SKiyoshi Ueda } 30192e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending); 30202e93ccc1SKiyoshi Ueda 30217e0d574fSBart Van Assche struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type, 30220776aa0eSMike Snitzer unsigned integrity, unsigned per_io_data_size, 30230776aa0eSMike Snitzer unsigned min_pool_size) 3024e6ee8c0bSKiyoshi Ueda { 3025115485e8SMike Snitzer struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id); 302678d8e58aSMike Snitzer unsigned int pool_size = 0; 302764f52b0eSMike Snitzer unsigned int front_pad, io_front_pad; 30286f1c819cSKent Overstreet int ret; 3029e6ee8c0bSKiyoshi Ueda 3030e6ee8c0bSKiyoshi Ueda if (!pools) 30314e6e36c3SMike Snitzer return NULL; 3032e6ee8c0bSKiyoshi Ueda 303378d8e58aSMike Snitzer switch (type) { 303478d8e58aSMike Snitzer case DM_TYPE_BIO_BASED: 3035545ed20eSToshi Kani case DM_TYPE_DAX_BIO_BASED: 303622c11858SMike Snitzer case DM_TYPE_NVME_BIO_BASED: 30370776aa0eSMike Snitzer pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size); 303830187e1dSMike Snitzer front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone); 303964f52b0eSMike Snitzer io_front_pad = roundup(front_pad, __alignof__(struct dm_io)) + offsetof(struct dm_io, tio); 30406f1c819cSKent Overstreet ret = bioset_init(&pools->io_bs, pool_size, io_front_pad, 0); 30416f1c819cSKent Overstreet if (ret) 304264f52b0eSMike Snitzer goto out; 30436f1c819cSKent Overstreet if (integrity && bioset_integrity_create(&pools->io_bs, pool_size)) 3044eb8db831SChristoph Hellwig goto out; 304578d8e58aSMike Snitzer break; 304678d8e58aSMike Snitzer case DM_TYPE_REQUEST_BASED: 30470776aa0eSMike Snitzer pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size); 304878d8e58aSMike Snitzer front_pad = offsetof(struct dm_rq_clone_bio_info, clone); 3049591ddcfcSMike Snitzer /* per_io_data_size is used for blk-mq pdu at queue allocation */ 305078d8e58aSMike Snitzer break; 305178d8e58aSMike Snitzer default: 305278d8e58aSMike Snitzer BUG(); 305378d8e58aSMike Snitzer } 305478d8e58aSMike Snitzer 30556f1c819cSKent Overstreet ret = bioset_init(&pools->bs, pool_size, front_pad, 0); 30566f1c819cSKent Overstreet if (ret) 30575f015204SJun'ichi Nomura goto out; 3058e6ee8c0bSKiyoshi Ueda 30596f1c819cSKent Overstreet if (integrity && bioset_integrity_create(&pools->bs, pool_size)) 30605f015204SJun'ichi Nomura goto out; 3061a91a2785SMartin K. Petersen 3062e6ee8c0bSKiyoshi Ueda return pools; 306378d8e58aSMike Snitzer 30645f015204SJun'ichi Nomura out: 30655f015204SJun'ichi Nomura dm_free_md_mempools(pools); 3066e6ee8c0bSKiyoshi Ueda 30674e6e36c3SMike Snitzer return NULL; 3068e6ee8c0bSKiyoshi Ueda } 3069e6ee8c0bSKiyoshi Ueda 3070e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools) 3071e6ee8c0bSKiyoshi Ueda { 3072e6ee8c0bSKiyoshi Ueda if (!pools) 3073e6ee8c0bSKiyoshi Ueda return; 3074e6ee8c0bSKiyoshi Ueda 30756f1c819cSKent Overstreet bioset_exit(&pools->bs); 30766f1c819cSKent Overstreet bioset_exit(&pools->io_bs); 3077e6ee8c0bSKiyoshi Ueda 3078e6ee8c0bSKiyoshi Ueda kfree(pools); 3079e6ee8c0bSKiyoshi Ueda } 3080e6ee8c0bSKiyoshi Ueda 30819c72bad1SChristoph Hellwig struct dm_pr { 30829c72bad1SChristoph Hellwig u64 old_key; 30839c72bad1SChristoph Hellwig u64 new_key; 30849c72bad1SChristoph Hellwig u32 flags; 30859c72bad1SChristoph Hellwig bool fail_early; 30869c72bad1SChristoph Hellwig }; 30879c72bad1SChristoph Hellwig 30889c72bad1SChristoph Hellwig static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn, 30899c72bad1SChristoph Hellwig void *data) 30909c72bad1SChristoph Hellwig { 30919c72bad1SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 30929c72bad1SChristoph Hellwig struct dm_table *table; 30939c72bad1SChristoph Hellwig struct dm_target *ti; 30949c72bad1SChristoph Hellwig int ret = -ENOTTY, srcu_idx; 30959c72bad1SChristoph Hellwig 30969c72bad1SChristoph Hellwig table = dm_get_live_table(md, &srcu_idx); 30979c72bad1SChristoph Hellwig if (!table || !dm_table_get_size(table)) 30989c72bad1SChristoph Hellwig goto out; 30999c72bad1SChristoph Hellwig 31009c72bad1SChristoph Hellwig /* We only support devices that have a single target */ 31019c72bad1SChristoph Hellwig if (dm_table_get_num_targets(table) != 1) 31029c72bad1SChristoph Hellwig goto out; 31039c72bad1SChristoph Hellwig ti = dm_table_get_target(table, 0); 31049c72bad1SChristoph Hellwig 31059c72bad1SChristoph Hellwig ret = -EINVAL; 31069c72bad1SChristoph Hellwig if (!ti->type->iterate_devices) 31079c72bad1SChristoph Hellwig goto out; 31089c72bad1SChristoph Hellwig 31099c72bad1SChristoph Hellwig ret = ti->type->iterate_devices(ti, fn, data); 31109c72bad1SChristoph Hellwig out: 31119c72bad1SChristoph Hellwig dm_put_live_table(md, srcu_idx); 31129c72bad1SChristoph Hellwig return ret; 31139c72bad1SChristoph Hellwig } 31149c72bad1SChristoph Hellwig 31159c72bad1SChristoph Hellwig /* 31169c72bad1SChristoph Hellwig * For register / unregister we need to manually call out to every path. 31179c72bad1SChristoph Hellwig */ 31189c72bad1SChristoph Hellwig static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev, 31199c72bad1SChristoph Hellwig sector_t start, sector_t len, void *data) 31209c72bad1SChristoph Hellwig { 31219c72bad1SChristoph Hellwig struct dm_pr *pr = data; 31229c72bad1SChristoph Hellwig const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops; 31239c72bad1SChristoph Hellwig 31249c72bad1SChristoph Hellwig if (!ops || !ops->pr_register) 31259c72bad1SChristoph Hellwig return -EOPNOTSUPP; 31269c72bad1SChristoph Hellwig return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags); 31279c72bad1SChristoph Hellwig } 31289c72bad1SChristoph Hellwig 312971cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key, 313071cdb697SChristoph Hellwig u32 flags) 313171cdb697SChristoph Hellwig { 31329c72bad1SChristoph Hellwig struct dm_pr pr = { 31339c72bad1SChristoph Hellwig .old_key = old_key, 31349c72bad1SChristoph Hellwig .new_key = new_key, 31359c72bad1SChristoph Hellwig .flags = flags, 31369c72bad1SChristoph Hellwig .fail_early = true, 31379c72bad1SChristoph Hellwig }; 31389c72bad1SChristoph Hellwig int ret; 313971cdb697SChristoph Hellwig 31409c72bad1SChristoph Hellwig ret = dm_call_pr(bdev, __dm_pr_register, &pr); 31419c72bad1SChristoph Hellwig if (ret && new_key) { 31429c72bad1SChristoph Hellwig /* unregister all paths if we failed to register any path */ 31439c72bad1SChristoph Hellwig pr.old_key = new_key; 31449c72bad1SChristoph Hellwig pr.new_key = 0; 31459c72bad1SChristoph Hellwig pr.flags = 0; 31469c72bad1SChristoph Hellwig pr.fail_early = false; 31479c72bad1SChristoph Hellwig dm_call_pr(bdev, __dm_pr_register, &pr); 31489c72bad1SChristoph Hellwig } 314971cdb697SChristoph Hellwig 31509c72bad1SChristoph Hellwig return ret; 315171cdb697SChristoph Hellwig } 315271cdb697SChristoph Hellwig 315371cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type, 315471cdb697SChristoph Hellwig u32 flags) 315571cdb697SChristoph Hellwig { 315671cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 315771cdb697SChristoph Hellwig const struct pr_ops *ops; 3158971888c4SMike Snitzer int r, srcu_idx; 315971cdb697SChristoph Hellwig 31605bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 316171cdb697SChristoph Hellwig if (r < 0) 3162971888c4SMike Snitzer goto out; 316371cdb697SChristoph Hellwig 316471cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 316571cdb697SChristoph Hellwig if (ops && ops->pr_reserve) 316671cdb697SChristoph Hellwig r = ops->pr_reserve(bdev, key, type, flags); 316771cdb697SChristoph Hellwig else 316871cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3169971888c4SMike Snitzer out: 3170971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 317171cdb697SChristoph Hellwig return r; 317271cdb697SChristoph Hellwig } 317371cdb697SChristoph Hellwig 317471cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type) 317571cdb697SChristoph Hellwig { 317671cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 317771cdb697SChristoph Hellwig const struct pr_ops *ops; 3178971888c4SMike Snitzer int r, srcu_idx; 317971cdb697SChristoph Hellwig 31805bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 318171cdb697SChristoph Hellwig if (r < 0) 3182971888c4SMike Snitzer goto out; 318371cdb697SChristoph Hellwig 318471cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 318571cdb697SChristoph Hellwig if (ops && ops->pr_release) 318671cdb697SChristoph Hellwig r = ops->pr_release(bdev, key, type); 318771cdb697SChristoph Hellwig else 318871cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3189971888c4SMike Snitzer out: 3190971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 319171cdb697SChristoph Hellwig return r; 319271cdb697SChristoph Hellwig } 319371cdb697SChristoph Hellwig 319471cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key, 319571cdb697SChristoph Hellwig enum pr_type type, bool abort) 319671cdb697SChristoph Hellwig { 319771cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 319871cdb697SChristoph Hellwig const struct pr_ops *ops; 3199971888c4SMike Snitzer int r, srcu_idx; 320071cdb697SChristoph Hellwig 32015bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 320271cdb697SChristoph Hellwig if (r < 0) 3203971888c4SMike Snitzer goto out; 320471cdb697SChristoph Hellwig 320571cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 320671cdb697SChristoph Hellwig if (ops && ops->pr_preempt) 320771cdb697SChristoph Hellwig r = ops->pr_preempt(bdev, old_key, new_key, type, abort); 320871cdb697SChristoph Hellwig else 320971cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3210971888c4SMike Snitzer out: 3211971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 321271cdb697SChristoph Hellwig return r; 321371cdb697SChristoph Hellwig } 321471cdb697SChristoph Hellwig 321571cdb697SChristoph Hellwig static int dm_pr_clear(struct block_device *bdev, u64 key) 321671cdb697SChristoph Hellwig { 321771cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 321871cdb697SChristoph Hellwig const struct pr_ops *ops; 3219971888c4SMike Snitzer int r, srcu_idx; 322071cdb697SChristoph Hellwig 32215bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 322271cdb697SChristoph Hellwig if (r < 0) 3223971888c4SMike Snitzer goto out; 322471cdb697SChristoph Hellwig 322571cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 322671cdb697SChristoph Hellwig if (ops && ops->pr_clear) 322771cdb697SChristoph Hellwig r = ops->pr_clear(bdev, key); 322871cdb697SChristoph Hellwig else 322971cdb697SChristoph Hellwig r = -EOPNOTSUPP; 3230971888c4SMike Snitzer out: 3231971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 323271cdb697SChristoph Hellwig return r; 323371cdb697SChristoph Hellwig } 323471cdb697SChristoph Hellwig 323571cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = { 323671cdb697SChristoph Hellwig .pr_register = dm_pr_register, 323771cdb697SChristoph Hellwig .pr_reserve = dm_pr_reserve, 323871cdb697SChristoph Hellwig .pr_release = dm_pr_release, 323971cdb697SChristoph Hellwig .pr_preempt = dm_pr_preempt, 324071cdb697SChristoph Hellwig .pr_clear = dm_pr_clear, 324171cdb697SChristoph Hellwig }; 324271cdb697SChristoph Hellwig 324383d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = { 3244c62b37d9SChristoph Hellwig .submit_bio = dm_submit_bio, 32451da177e4SLinus Torvalds .open = dm_blk_open, 32461da177e4SLinus Torvalds .release = dm_blk_close, 3247aa129a22SMilan Broz .ioctl = dm_blk_ioctl, 32483ac51e74SDarrick J. Wong .getgeo = dm_blk_getgeo, 3249e76239a3SChristoph Hellwig .report_zones = dm_blk_report_zones, 325071cdb697SChristoph Hellwig .pr_ops = &dm_pr_ops, 32511da177e4SLinus Torvalds .owner = THIS_MODULE 32521da177e4SLinus Torvalds }; 32531da177e4SLinus Torvalds 3254f26c5719SDan Williams static const struct dax_operations dm_dax_ops = { 3255f26c5719SDan Williams .direct_access = dm_dax_direct_access, 32567bf7eac8SDan Williams .dax_supported = dm_dax_supported, 32577e026c8cSDan Williams .copy_from_iter = dm_dax_copy_from_iter, 3258b3a9a0c3SDan Williams .copy_to_iter = dm_dax_copy_to_iter, 3259cdf6cdcdSVivek Goyal .zero_page_range = dm_dax_zero_page_range, 3260f26c5719SDan Williams }; 3261f26c5719SDan Williams 32621da177e4SLinus Torvalds /* 32631da177e4SLinus Torvalds * module hooks 32641da177e4SLinus Torvalds */ 32651da177e4SLinus Torvalds module_init(dm_init); 32661da177e4SLinus Torvalds module_exit(dm_exit); 32671da177e4SLinus Torvalds 32681da177e4SLinus Torvalds module_param(major, uint, 0); 32691da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper"); 3270f4790826SMike Snitzer 3271e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR); 3272e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools"); 3273e8603136SMike Snitzer 3274115485e8SMike Snitzer module_param(dm_numa_node, int, S_IRUGO | S_IWUSR); 3275115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations"); 3276115485e8SMike Snitzer 32771da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver"); 32781da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 32791da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 3280