11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Copyright (C) 2001, 2002 Sistina Software (UK) Limited. 3784aae73SMilan Broz * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This file is released under the GPL. 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 84cc96131SMike Snitzer #include "dm-core.h" 94cc96131SMike Snitzer #include "dm-rq.h" 1051e5b2bdSMike Anderson #include "dm-uevent.h" 1191ccbbacSTushar Sugandhi #include "dm-ima.h" 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds #include <linux/init.h> 141da177e4SLinus Torvalds #include <linux/module.h> 1548c9c27bSArjan van de Ven #include <linux/mutex.h> 166958c1c6SMikulas Patocka #include <linux/sched/mm.h> 17174cd4b1SIngo Molnar #include <linux/sched/signal.h> 181da177e4SLinus Torvalds #include <linux/blkpg.h> 191da177e4SLinus Torvalds #include <linux/bio.h> 201da177e4SLinus Torvalds #include <linux/mempool.h> 21f26c5719SDan Williams #include <linux/dax.h> 221da177e4SLinus Torvalds #include <linux/slab.h> 231da177e4SLinus Torvalds #include <linux/idr.h> 247e026c8cSDan Williams #include <linux/uio.h> 253ac51e74SDarrick J. Wong #include <linux/hdreg.h> 263f77316dSKiyoshi Ueda #include <linux/delay.h> 27ffcc3936SMike Snitzer #include <linux/wait.h> 2871cdb697SChristoph Hellwig #include <linux/pr.h> 29b0b4d7c6SElena Reshetova #include <linux/refcount.h> 30c6a564ffSChristoph Hellwig #include <linux/part_stat.h> 31a892c8d5SSatya Tangirala #include <linux/blk-crypto.h> 321e8d44bdSEric Biggers #include <linux/blk-crypto-profile.h> 3355782138SLi Zefan 3472d94861SAlasdair G Kergon #define DM_MSG_PREFIX "core" 3572d94861SAlasdair G Kergon 3660935eb2SMilan Broz /* 3760935eb2SMilan Broz * Cookies are numeric values sent with CHANGE and REMOVE 3860935eb2SMilan Broz * uevents while resuming, removing or renaming the device. 3960935eb2SMilan Broz */ 4060935eb2SMilan Broz #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE" 4160935eb2SMilan Broz #define DM_COOKIE_LENGTH 24 4260935eb2SMilan Broz 431da177e4SLinus Torvalds static const char *_name = DM_NAME; 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds static unsigned int major = 0; 461da177e4SLinus Torvalds static unsigned int _major = 0; 471da177e4SLinus Torvalds 48d15b774cSAlasdair G Kergon static DEFINE_IDR(_minor_idr); 49d15b774cSAlasdair G Kergon 50f32c10b0SJeff Mahoney static DEFINE_SPINLOCK(_minor_lock); 512c140a24SMikulas Patocka 522c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w); 532c140a24SMikulas Patocka 542c140a24SMikulas Patocka static DECLARE_WORK(deferred_remove_work, do_deferred_remove); 552c140a24SMikulas Patocka 56acfe0ad7SMikulas Patocka static struct workqueue_struct *deferred_remove_workqueue; 57acfe0ad7SMikulas Patocka 5893e6442cSMikulas Patocka atomic_t dm_global_event_nr = ATOMIC_INIT(0); 5993e6442cSMikulas Patocka DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq); 6093e6442cSMikulas Patocka 6162e08243SMikulas Patocka void dm_issue_global_event(void) 6262e08243SMikulas Patocka { 6362e08243SMikulas Patocka atomic_inc(&dm_global_event_nr); 6462e08243SMikulas Patocka wake_up(&dm_global_eventq); 6562e08243SMikulas Patocka } 6662e08243SMikulas Patocka 671da177e4SLinus Torvalds /* 6864f52b0eSMike Snitzer * One of these is allocated (on-stack) per original bio. 691da177e4SLinus Torvalds */ 7064f52b0eSMike Snitzer struct clone_info { 7164f52b0eSMike Snitzer struct dm_table *map; 7264f52b0eSMike Snitzer struct bio *bio; 7364f52b0eSMike Snitzer struct dm_io *io; 7464f52b0eSMike Snitzer sector_t sector; 7564f52b0eSMike Snitzer unsigned sector_count; 7664f52b0eSMike Snitzer }; 7764f52b0eSMike Snitzer 7862f26317SJeffle Xu #define DM_TARGET_IO_BIO_OFFSET (offsetof(struct dm_target_io, clone)) 7962f26317SJeffle Xu #define DM_IO_BIO_OFFSET \ 8062f26317SJeffle Xu (offsetof(struct dm_target_io, clone) + offsetof(struct dm_io, tio)) 8162f26317SJeffle Xu 826c23f0bdSChristoph Hellwig static inline struct dm_target_io *clone_to_tio(struct bio *clone) 836c23f0bdSChristoph Hellwig { 846c23f0bdSChristoph Hellwig return container_of(clone, struct dm_target_io, clone); 856c23f0bdSChristoph Hellwig } 866c23f0bdSChristoph Hellwig 8764f52b0eSMike Snitzer void *dm_per_bio_data(struct bio *bio, size_t data_size) 8864f52b0eSMike Snitzer { 896c23f0bdSChristoph Hellwig if (!clone_to_tio(bio)->inside_dm_io) 9062f26317SJeffle Xu return (char *)bio - DM_TARGET_IO_BIO_OFFSET - data_size; 9162f26317SJeffle Xu return (char *)bio - DM_IO_BIO_OFFSET - data_size; 9264f52b0eSMike Snitzer } 9364f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_per_bio_data); 9464f52b0eSMike Snitzer 9564f52b0eSMike Snitzer struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size) 9664f52b0eSMike Snitzer { 9764f52b0eSMike Snitzer struct dm_io *io = (struct dm_io *)((char *)data + data_size); 9864f52b0eSMike Snitzer if (io->magic == DM_IO_MAGIC) 9962f26317SJeffle Xu return (struct bio *)((char *)io + DM_IO_BIO_OFFSET); 10064f52b0eSMike Snitzer BUG_ON(io->magic != DM_TIO_MAGIC); 10162f26317SJeffle Xu return (struct bio *)((char *)io + DM_TARGET_IO_BIO_OFFSET); 10264f52b0eSMike Snitzer } 10364f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_from_per_bio_data); 10464f52b0eSMike Snitzer 10564f52b0eSMike Snitzer unsigned dm_bio_get_target_bio_nr(const struct bio *bio) 10664f52b0eSMike Snitzer { 10764f52b0eSMike Snitzer return container_of(bio, struct dm_target_io, clone)->target_bio_nr; 10864f52b0eSMike Snitzer } 10964f52b0eSMike Snitzer EXPORT_SYMBOL_GPL(dm_bio_get_target_bio_nr); 11064f52b0eSMike Snitzer 111ba61fdd1SJeff Mahoney #define MINOR_ALLOCED ((void *)-1) 112ba61fdd1SJeff Mahoney 113115485e8SMike Snitzer #define DM_NUMA_NODE NUMA_NO_NODE 114115485e8SMike Snitzer static int dm_numa_node = DM_NUMA_NODE; 115faad87dfSMike Snitzer 116a666e5c0SMikulas Patocka #define DEFAULT_SWAP_BIOS (8 * 1048576 / PAGE_SIZE) 117a666e5c0SMikulas Patocka static int swap_bios = DEFAULT_SWAP_BIOS; 118a666e5c0SMikulas Patocka static int get_swap_bios(void) 119a666e5c0SMikulas Patocka { 120a666e5c0SMikulas Patocka int latch = READ_ONCE(swap_bios); 121a666e5c0SMikulas Patocka if (unlikely(latch <= 0)) 122a666e5c0SMikulas Patocka latch = DEFAULT_SWAP_BIOS; 123a666e5c0SMikulas Patocka return latch; 124a666e5c0SMikulas Patocka } 125a666e5c0SMikulas Patocka 126e6ee8c0bSKiyoshi Ueda /* 127e6ee8c0bSKiyoshi Ueda * For mempools pre-allocation at the table loading time. 128e6ee8c0bSKiyoshi Ueda */ 129e6ee8c0bSKiyoshi Ueda struct dm_md_mempools { 1306f1c819cSKent Overstreet struct bio_set bs; 1316f1c819cSKent Overstreet struct bio_set io_bs; 132e6ee8c0bSKiyoshi Ueda }; 133e6ee8c0bSKiyoshi Ueda 13486f1152bSBenjamin Marzinski struct table_device { 13586f1152bSBenjamin Marzinski struct list_head list; 136b0b4d7c6SElena Reshetova refcount_t count; 13786f1152bSBenjamin Marzinski struct dm_dev dm_dev; 13886f1152bSBenjamin Marzinski }; 13986f1152bSBenjamin Marzinski 140f4790826SMike Snitzer /* 141e8603136SMike Snitzer * Bio-based DM's mempools' reserved IOs set by the user. 142e8603136SMike Snitzer */ 1434cc96131SMike Snitzer #define RESERVED_BIO_BASED_IOS 16 144e8603136SMike Snitzer static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS; 145e8603136SMike Snitzer 146115485e8SMike Snitzer static int __dm_get_module_param_int(int *module_param, int min, int max) 147115485e8SMike Snitzer { 1486aa7de05SMark Rutland int param = READ_ONCE(*module_param); 149115485e8SMike Snitzer int modified_param = 0; 150115485e8SMike Snitzer bool modified = true; 151115485e8SMike Snitzer 152115485e8SMike Snitzer if (param < min) 153115485e8SMike Snitzer modified_param = min; 154115485e8SMike Snitzer else if (param > max) 155115485e8SMike Snitzer modified_param = max; 156115485e8SMike Snitzer else 157115485e8SMike Snitzer modified = false; 158115485e8SMike Snitzer 159115485e8SMike Snitzer if (modified) { 160115485e8SMike Snitzer (void)cmpxchg(module_param, param, modified_param); 161115485e8SMike Snitzer param = modified_param; 162115485e8SMike Snitzer } 163115485e8SMike Snitzer 164115485e8SMike Snitzer return param; 165115485e8SMike Snitzer } 166115485e8SMike Snitzer 1674cc96131SMike Snitzer unsigned __dm_get_module_param(unsigned *module_param, 168f4790826SMike Snitzer unsigned def, unsigned max) 169f4790826SMike Snitzer { 1706aa7de05SMark Rutland unsigned param = READ_ONCE(*module_param); 17109c2d531SMike Snitzer unsigned modified_param = 0; 172f4790826SMike Snitzer 17309c2d531SMike Snitzer if (!param) 17409c2d531SMike Snitzer modified_param = def; 17509c2d531SMike Snitzer else if (param > max) 17609c2d531SMike Snitzer modified_param = max; 177f4790826SMike Snitzer 17809c2d531SMike Snitzer if (modified_param) { 17909c2d531SMike Snitzer (void)cmpxchg(module_param, param, modified_param); 18009c2d531SMike Snitzer param = modified_param; 181f4790826SMike Snitzer } 182f4790826SMike Snitzer 18309c2d531SMike Snitzer return param; 184f4790826SMike Snitzer } 185f4790826SMike Snitzer 186e8603136SMike Snitzer unsigned dm_get_reserved_bio_based_ios(void) 187e8603136SMike Snitzer { 18809c2d531SMike Snitzer return __dm_get_module_param(&reserved_bio_based_ios, 1894cc96131SMike Snitzer RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS); 190e8603136SMike Snitzer } 191e8603136SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios); 192e8603136SMike Snitzer 193115485e8SMike Snitzer static unsigned dm_get_numa_node(void) 194115485e8SMike Snitzer { 195115485e8SMike Snitzer return __dm_get_module_param_int(&dm_numa_node, 196115485e8SMike Snitzer DM_NUMA_NODE, num_online_nodes() - 1); 197115485e8SMike Snitzer } 198115485e8SMike Snitzer 1991da177e4SLinus Torvalds static int __init local_init(void) 2001da177e4SLinus Torvalds { 201e689fbabSMike Snitzer int r; 2021ae49ea2SMike Snitzer 20351e5b2bdSMike Anderson r = dm_uevent_init(); 20451157b4aSKiyoshi Ueda if (r) 205e689fbabSMike Snitzer return r; 20651e5b2bdSMike Anderson 207acfe0ad7SMikulas Patocka deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1); 208acfe0ad7SMikulas Patocka if (!deferred_remove_workqueue) { 209acfe0ad7SMikulas Patocka r = -ENOMEM; 210acfe0ad7SMikulas Patocka goto out_uevent_exit; 211acfe0ad7SMikulas Patocka } 212acfe0ad7SMikulas Patocka 2131da177e4SLinus Torvalds _major = major; 2141da177e4SLinus Torvalds r = register_blkdev(_major, _name); 21551157b4aSKiyoshi Ueda if (r < 0) 216acfe0ad7SMikulas Patocka goto out_free_workqueue; 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds if (!_major) 2191da177e4SLinus Torvalds _major = r; 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds return 0; 22251157b4aSKiyoshi Ueda 223acfe0ad7SMikulas Patocka out_free_workqueue: 224acfe0ad7SMikulas Patocka destroy_workqueue(deferred_remove_workqueue); 22551157b4aSKiyoshi Ueda out_uevent_exit: 22651157b4aSKiyoshi Ueda dm_uevent_exit(); 22751157b4aSKiyoshi Ueda 22851157b4aSKiyoshi Ueda return r; 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds static void local_exit(void) 2321da177e4SLinus Torvalds { 2332c140a24SMikulas Patocka flush_scheduled_work(); 234acfe0ad7SMikulas Patocka destroy_workqueue(deferred_remove_workqueue); 2352c140a24SMikulas Patocka 23600d59405SAkinobu Mita unregister_blkdev(_major, _name); 23751e5b2bdSMike Anderson dm_uevent_exit(); 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds _major = 0; 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds DMINFO("cleaned up"); 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds 244b9249e55SAlasdair G Kergon static int (*_inits[])(void) __initdata = { 2451da177e4SLinus Torvalds local_init, 2461da177e4SLinus Torvalds dm_target_init, 2471da177e4SLinus Torvalds dm_linear_init, 2481da177e4SLinus Torvalds dm_stripe_init, 249952b3557SMikulas Patocka dm_io_init, 250945fa4d2SMikulas Patocka dm_kcopyd_init, 2511da177e4SLinus Torvalds dm_interface_init, 252fd2ed4d2SMikulas Patocka dm_statistics_init, 2531da177e4SLinus Torvalds }; 2541da177e4SLinus Torvalds 255b9249e55SAlasdair G Kergon static void (*_exits[])(void) = { 2561da177e4SLinus Torvalds local_exit, 2571da177e4SLinus Torvalds dm_target_exit, 2581da177e4SLinus Torvalds dm_linear_exit, 2591da177e4SLinus Torvalds dm_stripe_exit, 260952b3557SMikulas Patocka dm_io_exit, 261945fa4d2SMikulas Patocka dm_kcopyd_exit, 2621da177e4SLinus Torvalds dm_interface_exit, 263fd2ed4d2SMikulas Patocka dm_statistics_exit, 2641da177e4SLinus Torvalds }; 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds static int __init dm_init(void) 2671da177e4SLinus Torvalds { 2681da177e4SLinus Torvalds const int count = ARRAY_SIZE(_inits); 2691da177e4SLinus Torvalds int r, i; 2701da177e4SLinus Torvalds 271f1cd6cb2STushar Sugandhi #if (IS_ENABLED(CONFIG_IMA) && !IS_ENABLED(CONFIG_IMA_DISABLE_HTABLE)) 272f1cd6cb2STushar Sugandhi DMWARN("CONFIG_IMA_DISABLE_HTABLE is disabled." 273f1cd6cb2STushar Sugandhi " Duplicate IMA measurements will not be recorded in the IMA log."); 274f1cd6cb2STushar Sugandhi #endif 275f1cd6cb2STushar Sugandhi 2761da177e4SLinus Torvalds for (i = 0; i < count; i++) { 2771da177e4SLinus Torvalds r = _inits[i](); 2781da177e4SLinus Torvalds if (r) 2791da177e4SLinus Torvalds goto bad; 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds return 0; 2831da177e4SLinus Torvalds bad: 2841da177e4SLinus Torvalds while (i--) 2851da177e4SLinus Torvalds _exits[i](); 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds return r; 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds static void __exit dm_exit(void) 2911da177e4SLinus Torvalds { 2921da177e4SLinus Torvalds int i = ARRAY_SIZE(_exits); 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds while (i--) 2951da177e4SLinus Torvalds _exits[i](); 296d15b774cSAlasdair G Kergon 297d15b774cSAlasdair G Kergon /* 298d15b774cSAlasdair G Kergon * Should be empty by this point. 299d15b774cSAlasdair G Kergon */ 300d15b774cSAlasdair G Kergon idr_destroy(&_minor_idr); 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds /* 3041da177e4SLinus Torvalds * Block device functions 3051da177e4SLinus Torvalds */ 306432a212cSMike Anderson int dm_deleting_md(struct mapped_device *md) 307432a212cSMike Anderson { 308432a212cSMike Anderson return test_bit(DMF_DELETING, &md->flags); 309432a212cSMike Anderson } 310432a212cSMike Anderson 311fe5f9f2cSAl Viro static int dm_blk_open(struct block_device *bdev, fmode_t mode) 3121da177e4SLinus Torvalds { 3131da177e4SLinus Torvalds struct mapped_device *md; 3141da177e4SLinus Torvalds 315fba9f90eSJeff Mahoney spin_lock(&_minor_lock); 316fba9f90eSJeff Mahoney 317fe5f9f2cSAl Viro md = bdev->bd_disk->private_data; 318fba9f90eSJeff Mahoney if (!md) 319fba9f90eSJeff Mahoney goto out; 320fba9f90eSJeff Mahoney 3215c6bd75dSAlasdair G Kergon if (test_bit(DMF_FREEING, &md->flags) || 322432a212cSMike Anderson dm_deleting_md(md)) { 323fba9f90eSJeff Mahoney md = NULL; 324fba9f90eSJeff Mahoney goto out; 325fba9f90eSJeff Mahoney } 326fba9f90eSJeff Mahoney 3271da177e4SLinus Torvalds dm_get(md); 3285c6bd75dSAlasdair G Kergon atomic_inc(&md->open_count); 329fba9f90eSJeff Mahoney out: 330fba9f90eSJeff Mahoney spin_unlock(&_minor_lock); 331fba9f90eSJeff Mahoney 332fba9f90eSJeff Mahoney return md ? 0 : -ENXIO; 3331da177e4SLinus Torvalds } 3341da177e4SLinus Torvalds 335db2a144bSAl Viro static void dm_blk_close(struct gendisk *disk, fmode_t mode) 3361da177e4SLinus Torvalds { 33763a4f065SMike Snitzer struct mapped_device *md; 3386e9624b8SArnd Bergmann 3394a1aeb98SMilan Broz spin_lock(&_minor_lock); 3404a1aeb98SMilan Broz 34163a4f065SMike Snitzer md = disk->private_data; 34263a4f065SMike Snitzer if (WARN_ON(!md)) 34363a4f065SMike Snitzer goto out; 34463a4f065SMike Snitzer 3452c140a24SMikulas Patocka if (atomic_dec_and_test(&md->open_count) && 3462c140a24SMikulas Patocka (test_bit(DMF_DEFERRED_REMOVE, &md->flags))) 347acfe0ad7SMikulas Patocka queue_work(deferred_remove_workqueue, &deferred_remove_work); 3482c140a24SMikulas Patocka 3491da177e4SLinus Torvalds dm_put(md); 35063a4f065SMike Snitzer out: 3514a1aeb98SMilan Broz spin_unlock(&_minor_lock); 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds 3545c6bd75dSAlasdair G Kergon int dm_open_count(struct mapped_device *md) 3555c6bd75dSAlasdair G Kergon { 3565c6bd75dSAlasdair G Kergon return atomic_read(&md->open_count); 3575c6bd75dSAlasdair G Kergon } 3585c6bd75dSAlasdair G Kergon 3595c6bd75dSAlasdair G Kergon /* 3605c6bd75dSAlasdair G Kergon * Guarantees nothing is using the device before it's deleted. 3615c6bd75dSAlasdair G Kergon */ 3622c140a24SMikulas Patocka int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred) 3635c6bd75dSAlasdair G Kergon { 3645c6bd75dSAlasdair G Kergon int r = 0; 3655c6bd75dSAlasdair G Kergon 3665c6bd75dSAlasdair G Kergon spin_lock(&_minor_lock); 3675c6bd75dSAlasdair G Kergon 3682c140a24SMikulas Patocka if (dm_open_count(md)) { 3695c6bd75dSAlasdair G Kergon r = -EBUSY; 3702c140a24SMikulas Patocka if (mark_deferred) 3712c140a24SMikulas Patocka set_bit(DMF_DEFERRED_REMOVE, &md->flags); 3722c140a24SMikulas Patocka } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags)) 3732c140a24SMikulas Patocka r = -EEXIST; 3745c6bd75dSAlasdair G Kergon else 3755c6bd75dSAlasdair G Kergon set_bit(DMF_DELETING, &md->flags); 3765c6bd75dSAlasdair G Kergon 3775c6bd75dSAlasdair G Kergon spin_unlock(&_minor_lock); 3785c6bd75dSAlasdair G Kergon 3795c6bd75dSAlasdair G Kergon return r; 3805c6bd75dSAlasdair G Kergon } 3815c6bd75dSAlasdair G Kergon 3822c140a24SMikulas Patocka int dm_cancel_deferred_remove(struct mapped_device *md) 3832c140a24SMikulas Patocka { 3842c140a24SMikulas Patocka int r = 0; 3852c140a24SMikulas Patocka 3862c140a24SMikulas Patocka spin_lock(&_minor_lock); 3872c140a24SMikulas Patocka 3882c140a24SMikulas Patocka if (test_bit(DMF_DELETING, &md->flags)) 3892c140a24SMikulas Patocka r = -EBUSY; 3902c140a24SMikulas Patocka else 3912c140a24SMikulas Patocka clear_bit(DMF_DEFERRED_REMOVE, &md->flags); 3922c140a24SMikulas Patocka 3932c140a24SMikulas Patocka spin_unlock(&_minor_lock); 3942c140a24SMikulas Patocka 3952c140a24SMikulas Patocka return r; 3962c140a24SMikulas Patocka } 3972c140a24SMikulas Patocka 3982c140a24SMikulas Patocka static void do_deferred_remove(struct work_struct *w) 3992c140a24SMikulas Patocka { 4002c140a24SMikulas Patocka dm_deferred_remove(); 4012c140a24SMikulas Patocka } 4022c140a24SMikulas Patocka 4033ac51e74SDarrick J. Wong static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) 4043ac51e74SDarrick J. Wong { 4053ac51e74SDarrick J. Wong struct mapped_device *md = bdev->bd_disk->private_data; 4063ac51e74SDarrick J. Wong 4073ac51e74SDarrick J. Wong return dm_get_geometry(md, geo); 4083ac51e74SDarrick J. Wong } 4093ac51e74SDarrick J. Wong 410971888c4SMike Snitzer static int dm_prepare_ioctl(struct mapped_device *md, int *srcu_idx, 4115bd5e8d8SMike Snitzer struct block_device **bdev) 412aa129a22SMilan Broz { 41366482026SMike Snitzer struct dm_target *tgt; 4146c182cd8SHannes Reinecke struct dm_table *map; 415971888c4SMike Snitzer int r; 416aa129a22SMilan Broz 4176c182cd8SHannes Reinecke retry: 418e56f81e0SChristoph Hellwig r = -ENOTTY; 419971888c4SMike Snitzer map = dm_get_live_table(md, srcu_idx); 420aa129a22SMilan Broz if (!map || !dm_table_get_size(map)) 421971888c4SMike Snitzer return r; 422aa129a22SMilan Broz 423aa129a22SMilan Broz /* We only support devices that have a single target */ 424aa129a22SMilan Broz if (dm_table_get_num_targets(map) != 1) 425971888c4SMike Snitzer return r; 426aa129a22SMilan Broz 42766482026SMike Snitzer tgt = dm_table_get_target(map, 0); 42866482026SMike Snitzer if (!tgt->type->prepare_ioctl) 429e56f81e0SChristoph Hellwig return r; 430aa129a22SMilan Broz 431971888c4SMike Snitzer if (dm_suspended_md(md)) 432971888c4SMike Snitzer return -EAGAIN; 433971888c4SMike Snitzer 4345bd5e8d8SMike Snitzer r = tgt->type->prepare_ioctl(tgt, bdev); 4355bbbfdf6SJunichi Nomura if (r == -ENOTCONN && !fatal_signal_pending(current)) { 436971888c4SMike Snitzer dm_put_live_table(md, *srcu_idx); 4376c182cd8SHannes Reinecke msleep(10); 4386c182cd8SHannes Reinecke goto retry; 4396c182cd8SHannes Reinecke } 440971888c4SMike Snitzer 441e56f81e0SChristoph Hellwig return r; 442e56f81e0SChristoph Hellwig } 4436c182cd8SHannes Reinecke 444971888c4SMike Snitzer static void dm_unprepare_ioctl(struct mapped_device *md, int srcu_idx) 445971888c4SMike Snitzer { 446971888c4SMike Snitzer dm_put_live_table(md, srcu_idx); 447971888c4SMike Snitzer } 448971888c4SMike Snitzer 449e56f81e0SChristoph Hellwig static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode, 450e56f81e0SChristoph Hellwig unsigned int cmd, unsigned long arg) 451e56f81e0SChristoph Hellwig { 452e56f81e0SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 453971888c4SMike Snitzer int r, srcu_idx; 454e56f81e0SChristoph Hellwig 4555bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 456e56f81e0SChristoph Hellwig if (r < 0) 457971888c4SMike Snitzer goto out; 458e56f81e0SChristoph Hellwig 459e56f81e0SChristoph Hellwig if (r > 0) { 460e56f81e0SChristoph Hellwig /* 461e980f623SChristoph Hellwig * Target determined this ioctl is being issued against a 462e980f623SChristoph Hellwig * subset of the parent bdev; require extra privileges. 463e56f81e0SChristoph Hellwig */ 464e980f623SChristoph Hellwig if (!capable(CAP_SYS_RAWIO)) { 4650378c625SMike Snitzer DMDEBUG_LIMIT( 466e980f623SChristoph Hellwig "%s: sending ioctl %x to DM device without required privilege.", 467e980f623SChristoph Hellwig current->comm, cmd); 468e980f623SChristoph Hellwig r = -ENOIOCTLCMD; 469e56f81e0SChristoph Hellwig goto out; 470e56f81e0SChristoph Hellwig } 471e980f623SChristoph Hellwig } 472e56f81e0SChristoph Hellwig 473a7cb3d2fSChristoph Hellwig if (!bdev->bd_disk->fops->ioctl) 474a7cb3d2fSChristoph Hellwig r = -ENOTTY; 475a7cb3d2fSChristoph Hellwig else 476a7cb3d2fSChristoph Hellwig r = bdev->bd_disk->fops->ioctl(bdev, mode, cmd, arg); 477e56f81e0SChristoph Hellwig out: 478971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 479aa129a22SMilan Broz return r; 480aa129a22SMilan Broz } 481aa129a22SMilan Broz 4827465d7acSMike Snitzer u64 dm_start_time_ns_from_clone(struct bio *bio) 4837465d7acSMike Snitzer { 4846c23f0bdSChristoph Hellwig return jiffies_to_nsecs(clone_to_tio(bio)->io->start_time); 4857465d7acSMike Snitzer } 4867465d7acSMike Snitzer EXPORT_SYMBOL_GPL(dm_start_time_ns_from_clone); 4877465d7acSMike Snitzer 4887465d7acSMike Snitzer static void start_io_acct(struct dm_io *io) 4897465d7acSMike Snitzer { 4907465d7acSMike Snitzer struct mapped_device *md = io->md; 4917465d7acSMike Snitzer struct bio *bio = io->orig_bio; 4927465d7acSMike Snitzer 493b879f915SMike Snitzer bio_start_io_acct_time(bio, io->start_time); 4947465d7acSMike Snitzer if (unlikely(dm_stats_used(&md->stats))) 4957465d7acSMike Snitzer dm_stats_account_io(&md->stats, bio_data_dir(bio), 4967465d7acSMike Snitzer bio->bi_iter.bi_sector, bio_sectors(bio), 4977465d7acSMike Snitzer false, 0, &io->stats_aux); 4987465d7acSMike Snitzer } 4997465d7acSMike Snitzer 500d208b894SJiazi Li static void end_io_acct(struct mapped_device *md, struct bio *bio, 501d208b894SJiazi Li unsigned long start_time, struct dm_stats_aux *stats_aux) 5027465d7acSMike Snitzer { 503d208b894SJiazi Li unsigned long duration = jiffies - start_time; 5047465d7acSMike Snitzer 505d208b894SJiazi Li bio_end_io_acct(bio, start_time); 5067465d7acSMike Snitzer 5077465d7acSMike Snitzer if (unlikely(dm_stats_used(&md->stats))) 5087465d7acSMike Snitzer dm_stats_account_io(&md->stats, bio_data_dir(bio), 5097465d7acSMike Snitzer bio->bi_iter.bi_sector, bio_sectors(bio), 510d208b894SJiazi Li true, duration, stats_aux); 5117465d7acSMike Snitzer 5127465d7acSMike Snitzer /* nudge anyone waiting on suspend queue */ 5137465d7acSMike Snitzer if (unlikely(wq_has_sleeper(&md->wait))) 5147465d7acSMike Snitzer wake_up(&md->wait); 5157465d7acSMike Snitzer } 516978e51baSMike Snitzer 517978e51baSMike Snitzer static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio) 5181da177e4SLinus Torvalds { 51964f52b0eSMike Snitzer struct dm_io *io; 52064f52b0eSMike Snitzer struct dm_target_io *tio; 52164f52b0eSMike Snitzer struct bio *clone; 52264f52b0eSMike Snitzer 523609be106SChristoph Hellwig clone = bio_alloc_bioset(NULL, 0, 0, GFP_NOIO, &md->io_bs); 52464f52b0eSMike Snitzer 5256c23f0bdSChristoph Hellwig tio = clone_to_tio(clone); 52664f52b0eSMike Snitzer tio->inside_dm_io = true; 52764f52b0eSMike Snitzer tio->io = NULL; 52864f52b0eSMike Snitzer 52964f52b0eSMike Snitzer io = container_of(tio, struct dm_io, tio); 53064f52b0eSMike Snitzer io->magic = DM_IO_MAGIC; 531978e51baSMike Snitzer io->status = 0; 532978e51baSMike Snitzer atomic_set(&io->io_count, 1); 533978e51baSMike Snitzer io->orig_bio = bio; 534978e51baSMike Snitzer io->md = md; 535978e51baSMike Snitzer spin_lock_init(&io->endio_lock); 536978e51baSMike Snitzer 537b879f915SMike Snitzer io->start_time = jiffies; 53864f52b0eSMike Snitzer 53964f52b0eSMike Snitzer return io; 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds 542028867acSAlasdair G Kergon static void free_io(struct mapped_device *md, struct dm_io *io) 5431da177e4SLinus Torvalds { 54464f52b0eSMike Snitzer bio_put(&io->tio.clone); 54564f52b0eSMike Snitzer } 54664f52b0eSMike Snitzer 54764f52b0eSMike Snitzer static struct dm_target_io *alloc_tio(struct clone_info *ci, struct dm_target *ti, 548dc8e2021SChristoph Hellwig unsigned target_bio_nr, unsigned *len, gfp_t gfp_mask) 54964f52b0eSMike Snitzer { 55064f52b0eSMike Snitzer struct dm_target_io *tio; 55164f52b0eSMike Snitzer 55264f52b0eSMike Snitzer if (!ci->io->tio.io) { 55364f52b0eSMike Snitzer /* the dm_target_io embedded in ci->io is available */ 55464f52b0eSMike Snitzer tio = &ci->io->tio; 55564f52b0eSMike Snitzer } else { 556609be106SChristoph Hellwig struct bio *clone = bio_alloc_bioset(NULL, 0, 0, gfp_mask, 557609be106SChristoph Hellwig &ci->io->md->bs); 55864f52b0eSMike Snitzer if (!clone) 55964f52b0eSMike Snitzer return NULL; 56064f52b0eSMike Snitzer 5616c23f0bdSChristoph Hellwig tio = clone_to_tio(clone); 56264f52b0eSMike Snitzer tio->inside_dm_io = false; 56364f52b0eSMike Snitzer } 564dc8e2021SChristoph Hellwig __bio_clone_fast(&tio->clone, ci->bio); 56564f52b0eSMike Snitzer 56664f52b0eSMike Snitzer tio->magic = DM_TIO_MAGIC; 56764f52b0eSMike Snitzer tio->io = ci->io; 56864f52b0eSMike Snitzer tio->ti = ti; 56964f52b0eSMike Snitzer tio->target_bio_nr = target_bio_nr; 570dc8e2021SChristoph Hellwig tio->len_ptr = len; 57164f52b0eSMike Snitzer 57264f52b0eSMike Snitzer return tio; 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds 575cfae7529SMike Snitzer static void free_tio(struct dm_target_io *tio) 5761da177e4SLinus Torvalds { 57764f52b0eSMike Snitzer if (tio->inside_dm_io) 57864f52b0eSMike Snitzer return; 579dba14160SMikulas Patocka bio_put(&tio->clone); 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds /* 5831da177e4SLinus Torvalds * Add the bio to the list of deferred io. 5841da177e4SLinus Torvalds */ 58592c63902SMikulas Patocka static void queue_io(struct mapped_device *md, struct bio *bio) 5861da177e4SLinus Torvalds { 58705447420SKiyoshi Ueda unsigned long flags; 5881da177e4SLinus Torvalds 58905447420SKiyoshi Ueda spin_lock_irqsave(&md->deferred_lock, flags); 5901da177e4SLinus Torvalds bio_list_add(&md->deferred, bio); 59105447420SKiyoshi Ueda spin_unlock_irqrestore(&md->deferred_lock, flags); 59292c63902SMikulas Patocka queue_work(md->wq, &md->work); 5931da177e4SLinus Torvalds } 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds /* 5961da177e4SLinus Torvalds * Everyone (including functions in this file), should use this 5971da177e4SLinus Torvalds * function to access the md->map field, and make sure they call 59883d5e5b0SMikulas Patocka * dm_put_live_table() when finished. 5991da177e4SLinus Torvalds */ 60083d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier) 6011da177e4SLinus Torvalds { 60283d5e5b0SMikulas Patocka *srcu_idx = srcu_read_lock(&md->io_barrier); 6031da177e4SLinus Torvalds 60483d5e5b0SMikulas Patocka return srcu_dereference(md->map, &md->io_barrier); 60583d5e5b0SMikulas Patocka } 6061da177e4SLinus Torvalds 60783d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier) 60883d5e5b0SMikulas Patocka { 60983d5e5b0SMikulas Patocka srcu_read_unlock(&md->io_barrier, srcu_idx); 61083d5e5b0SMikulas Patocka } 61183d5e5b0SMikulas Patocka 61283d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md) 61383d5e5b0SMikulas Patocka { 61483d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 61583d5e5b0SMikulas Patocka synchronize_rcu_expedited(); 61683d5e5b0SMikulas Patocka } 61783d5e5b0SMikulas Patocka 61883d5e5b0SMikulas Patocka /* 61983d5e5b0SMikulas Patocka * A fast alternative to dm_get_live_table/dm_put_live_table. 62083d5e5b0SMikulas Patocka * The caller must not block between these two functions. 62183d5e5b0SMikulas Patocka */ 62283d5e5b0SMikulas Patocka static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU) 62383d5e5b0SMikulas Patocka { 62483d5e5b0SMikulas Patocka rcu_read_lock(); 62583d5e5b0SMikulas Patocka return rcu_dereference(md->map); 62683d5e5b0SMikulas Patocka } 62783d5e5b0SMikulas Patocka 62883d5e5b0SMikulas Patocka static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU) 62983d5e5b0SMikulas Patocka { 63083d5e5b0SMikulas Patocka rcu_read_unlock(); 6311da177e4SLinus Torvalds } 6321da177e4SLinus Torvalds 633971888c4SMike Snitzer static char *_dm_claim_ptr = "I belong to device-mapper"; 634971888c4SMike Snitzer 6353ac51e74SDarrick J. Wong /* 63686f1152bSBenjamin Marzinski * Open a table device so we can use it as a map destination. 63786f1152bSBenjamin Marzinski */ 63886f1152bSBenjamin Marzinski static int open_table_device(struct table_device *td, dev_t dev, 63986f1152bSBenjamin Marzinski struct mapped_device *md) 64086f1152bSBenjamin Marzinski { 64186f1152bSBenjamin Marzinski struct block_device *bdev; 642cd913c76SChristoph Hellwig u64 part_off; 64386f1152bSBenjamin Marzinski int r; 64486f1152bSBenjamin Marzinski 64586f1152bSBenjamin Marzinski BUG_ON(td->dm_dev.bdev); 64686f1152bSBenjamin Marzinski 647519049afSMike Snitzer bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr); 64886f1152bSBenjamin Marzinski if (IS_ERR(bdev)) 64986f1152bSBenjamin Marzinski return PTR_ERR(bdev); 65086f1152bSBenjamin Marzinski 65186f1152bSBenjamin Marzinski r = bd_link_disk_holder(bdev, dm_disk(md)); 65286f1152bSBenjamin Marzinski if (r) { 65386f1152bSBenjamin Marzinski blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL); 65486f1152bSBenjamin Marzinski return r; 65586f1152bSBenjamin Marzinski } 65686f1152bSBenjamin Marzinski 65786f1152bSBenjamin Marzinski td->dm_dev.bdev = bdev; 658cd913c76SChristoph Hellwig td->dm_dev.dax_dev = fs_dax_get_by_bdev(bdev, &part_off); 65986f1152bSBenjamin Marzinski return 0; 66086f1152bSBenjamin Marzinski } 66186f1152bSBenjamin Marzinski 66286f1152bSBenjamin Marzinski /* 66386f1152bSBenjamin Marzinski * Close a table device that we've been using. 66486f1152bSBenjamin Marzinski */ 66586f1152bSBenjamin Marzinski static void close_table_device(struct table_device *td, struct mapped_device *md) 66686f1152bSBenjamin Marzinski { 66786f1152bSBenjamin Marzinski if (!td->dm_dev.bdev) 66886f1152bSBenjamin Marzinski return; 66986f1152bSBenjamin Marzinski 67086f1152bSBenjamin Marzinski bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md)); 67186f1152bSBenjamin Marzinski blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL); 672817bf402SDan Williams put_dax(td->dm_dev.dax_dev); 67386f1152bSBenjamin Marzinski td->dm_dev.bdev = NULL; 674817bf402SDan Williams td->dm_dev.dax_dev = NULL; 67586f1152bSBenjamin Marzinski } 67686f1152bSBenjamin Marzinski 67786f1152bSBenjamin Marzinski static struct table_device *find_table_device(struct list_head *l, dev_t dev, 6788454fca4SSheetal Singala fmode_t mode) 6798454fca4SSheetal Singala { 68086f1152bSBenjamin Marzinski struct table_device *td; 68186f1152bSBenjamin Marzinski 68286f1152bSBenjamin Marzinski list_for_each_entry(td, l, list) 68386f1152bSBenjamin Marzinski if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode) 68486f1152bSBenjamin Marzinski return td; 68586f1152bSBenjamin Marzinski 68686f1152bSBenjamin Marzinski return NULL; 68786f1152bSBenjamin Marzinski } 68886f1152bSBenjamin Marzinski 68986f1152bSBenjamin Marzinski int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode, 6908454fca4SSheetal Singala struct dm_dev **result) 6918454fca4SSheetal Singala { 69286f1152bSBenjamin Marzinski int r; 69386f1152bSBenjamin Marzinski struct table_device *td; 69486f1152bSBenjamin Marzinski 69586f1152bSBenjamin Marzinski mutex_lock(&md->table_devices_lock); 69686f1152bSBenjamin Marzinski td = find_table_device(&md->table_devices, dev, mode); 69786f1152bSBenjamin Marzinski if (!td) { 698115485e8SMike Snitzer td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id); 69986f1152bSBenjamin Marzinski if (!td) { 70086f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 70186f1152bSBenjamin Marzinski return -ENOMEM; 70286f1152bSBenjamin Marzinski } 70386f1152bSBenjamin Marzinski 70486f1152bSBenjamin Marzinski td->dm_dev.mode = mode; 70586f1152bSBenjamin Marzinski td->dm_dev.bdev = NULL; 70686f1152bSBenjamin Marzinski 70786f1152bSBenjamin Marzinski if ((r = open_table_device(td, dev, md))) { 70886f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 70986f1152bSBenjamin Marzinski kfree(td); 71086f1152bSBenjamin Marzinski return r; 71186f1152bSBenjamin Marzinski } 71286f1152bSBenjamin Marzinski 71386f1152bSBenjamin Marzinski format_dev_t(td->dm_dev.name, dev); 71486f1152bSBenjamin Marzinski 715b0b4d7c6SElena Reshetova refcount_set(&td->count, 1); 71686f1152bSBenjamin Marzinski list_add(&td->list, &md->table_devices); 717b0b4d7c6SElena Reshetova } else { 718b0b4d7c6SElena Reshetova refcount_inc(&td->count); 71986f1152bSBenjamin Marzinski } 72086f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 72186f1152bSBenjamin Marzinski 72286f1152bSBenjamin Marzinski *result = &td->dm_dev; 72386f1152bSBenjamin Marzinski return 0; 72486f1152bSBenjamin Marzinski } 72586f1152bSBenjamin Marzinski 72686f1152bSBenjamin Marzinski void dm_put_table_device(struct mapped_device *md, struct dm_dev *d) 72786f1152bSBenjamin Marzinski { 72886f1152bSBenjamin Marzinski struct table_device *td = container_of(d, struct table_device, dm_dev); 72986f1152bSBenjamin Marzinski 73086f1152bSBenjamin Marzinski mutex_lock(&md->table_devices_lock); 731b0b4d7c6SElena Reshetova if (refcount_dec_and_test(&td->count)) { 73286f1152bSBenjamin Marzinski close_table_device(td, md); 73386f1152bSBenjamin Marzinski list_del(&td->list); 73486f1152bSBenjamin Marzinski kfree(td); 73586f1152bSBenjamin Marzinski } 73686f1152bSBenjamin Marzinski mutex_unlock(&md->table_devices_lock); 73786f1152bSBenjamin Marzinski } 73886f1152bSBenjamin Marzinski 73986f1152bSBenjamin Marzinski static void free_table_devices(struct list_head *devices) 74086f1152bSBenjamin Marzinski { 74186f1152bSBenjamin Marzinski struct list_head *tmp, *next; 74286f1152bSBenjamin Marzinski 74386f1152bSBenjamin Marzinski list_for_each_safe(tmp, next, devices) { 74486f1152bSBenjamin Marzinski struct table_device *td = list_entry(tmp, struct table_device, list); 74586f1152bSBenjamin Marzinski 74686f1152bSBenjamin Marzinski DMWARN("dm_destroy: %s still exists with %d references", 747b0b4d7c6SElena Reshetova td->dm_dev.name, refcount_read(&td->count)); 74886f1152bSBenjamin Marzinski kfree(td); 74986f1152bSBenjamin Marzinski } 75086f1152bSBenjamin Marzinski } 75186f1152bSBenjamin Marzinski 75286f1152bSBenjamin Marzinski /* 7533ac51e74SDarrick J. Wong * Get the geometry associated with a dm device 7543ac51e74SDarrick J. Wong */ 7553ac51e74SDarrick J. Wong int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo) 7563ac51e74SDarrick J. Wong { 7573ac51e74SDarrick J. Wong *geo = md->geometry; 7583ac51e74SDarrick J. Wong 7593ac51e74SDarrick J. Wong return 0; 7603ac51e74SDarrick J. Wong } 7613ac51e74SDarrick J. Wong 7623ac51e74SDarrick J. Wong /* 7633ac51e74SDarrick J. Wong * Set the geometry of a device. 7643ac51e74SDarrick J. Wong */ 7653ac51e74SDarrick J. Wong int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo) 7663ac51e74SDarrick J. Wong { 7673ac51e74SDarrick J. Wong sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors; 7683ac51e74SDarrick J. Wong 7693ac51e74SDarrick J. Wong if (geo->start > sz) { 7703ac51e74SDarrick J. Wong DMWARN("Start sector is beyond the geometry limits."); 7713ac51e74SDarrick J. Wong return -EINVAL; 7723ac51e74SDarrick J. Wong } 7733ac51e74SDarrick J. Wong 7743ac51e74SDarrick J. Wong md->geometry = *geo; 7753ac51e74SDarrick J. Wong 7763ac51e74SDarrick J. Wong return 0; 7773ac51e74SDarrick J. Wong } 7783ac51e74SDarrick J. Wong 7792e93ccc1SKiyoshi Ueda static int __noflush_suspending(struct mapped_device *md) 7802e93ccc1SKiyoshi Ueda { 7812e93ccc1SKiyoshi Ueda return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 7822e93ccc1SKiyoshi Ueda } 7832e93ccc1SKiyoshi Ueda 7841da177e4SLinus Torvalds /* 7851da177e4SLinus Torvalds * Decrements the number of outstanding ios that a bio has been 7861da177e4SLinus Torvalds * cloned into, completing the original io if necc. 7871da177e4SLinus Torvalds */ 788e2118b3cSDamien Le Moal void dm_io_dec_pending(struct dm_io *io, blk_status_t error) 7891da177e4SLinus Torvalds { 7902e93ccc1SKiyoshi Ueda unsigned long flags; 7914e4cbee9SChristoph Hellwig blk_status_t io_error; 792b35f8caaSMilan Broz struct bio *bio; 793b35f8caaSMilan Broz struct mapped_device *md = io->md; 794d208b894SJiazi Li unsigned long start_time = 0; 795d208b894SJiazi Li struct dm_stats_aux stats_aux; 7962e93ccc1SKiyoshi Ueda 7972e93ccc1SKiyoshi Ueda /* Push-back supersedes any I/O errors */ 798f88fb981SKiyoshi Ueda if (unlikely(error)) { 799f88fb981SKiyoshi Ueda spin_lock_irqsave(&io->endio_lock, flags); 800745dc570SMike Snitzer if (!(io->status == BLK_STS_DM_REQUEUE && __noflush_suspending(md))) 8014e4cbee9SChristoph Hellwig io->status = error; 802f88fb981SKiyoshi Ueda spin_unlock_irqrestore(&io->endio_lock, flags); 803f88fb981SKiyoshi Ueda } 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds if (atomic_dec_and_test(&io->io_count)) { 806bf14e2b2SDamien Le Moal bio = io->orig_bio; 8074e4cbee9SChristoph Hellwig if (io->status == BLK_STS_DM_REQUEUE) { 8082e93ccc1SKiyoshi Ueda /* 8092e93ccc1SKiyoshi Ueda * Target requested pushing back the I/O. 8102e93ccc1SKiyoshi Ueda */ 811022c2611SMikulas Patocka spin_lock_irqsave(&md->deferred_lock, flags); 812bf14e2b2SDamien Le Moal if (__noflush_suspending(md) && 813bf14e2b2SDamien Le Moal !WARN_ON_ONCE(dm_is_zone_write(md, bio))) { 814745dc570SMike Snitzer /* NOTE early return due to BLK_STS_DM_REQUEUE below */ 815bf14e2b2SDamien Le Moal bio_list_add_head(&md->deferred, bio); 816bf14e2b2SDamien Le Moal } else { 817bf14e2b2SDamien Le Moal /* 818bf14e2b2SDamien Le Moal * noflush suspend was interrupted or this is 819bf14e2b2SDamien Le Moal * a write to a zoned target. 820bf14e2b2SDamien Le Moal */ 8214e4cbee9SChristoph Hellwig io->status = BLK_STS_IOERR; 822bf14e2b2SDamien Le Moal } 823022c2611SMikulas Patocka spin_unlock_irqrestore(&md->deferred_lock, flags); 8242e93ccc1SKiyoshi Ueda } 8252e93ccc1SKiyoshi Ueda 8264e4cbee9SChristoph Hellwig io_error = io->status; 827d208b894SJiazi Li start_time = io->start_time; 828d208b894SJiazi Li stats_aux = io->stats_aux; 829a97f925aSMikulas Patocka free_io(md, io); 830d208b894SJiazi Li end_io_acct(md, bio, start_time, &stats_aux); 8311da177e4SLinus Torvalds 8324e4cbee9SChristoph Hellwig if (io_error == BLK_STS_DM_REQUEUE) 8336a8736d1STejun Heo return; 8346a8736d1STejun Heo 8351eff9d32SJens Axboe if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) { 8361da177e4SLinus Torvalds /* 8376a8736d1STejun Heo * Preflush done for flush with data, reissue 83828a8f0d3SMike Christie * without REQ_PREFLUSH. 839af7e466aSMikulas Patocka */ 8401eff9d32SJens Axboe bio->bi_opf &= ~REQ_PREFLUSH; 8416a8736d1STejun Heo queue_io(md, bio); 842af7e466aSMikulas Patocka } else { 843b372d360SMike Snitzer /* done with normal IO or empty flush */ 8448dd601faSNeilBrown if (io_error) 8454e4cbee9SChristoph Hellwig bio->bi_status = io_error; 8464246a0b6SChristoph Hellwig bio_endio(bio); 8472e93ccc1SKiyoshi Ueda } 8481da177e4SLinus Torvalds } 849af7e466aSMikulas Patocka } 8501da177e4SLinus Torvalds 851bcb44433SMike Snitzer void disable_discard(struct mapped_device *md) 852bcb44433SMike Snitzer { 853bcb44433SMike Snitzer struct queue_limits *limits = dm_get_queue_limits(md); 854bcb44433SMike Snitzer 855bcb44433SMike Snitzer /* device doesn't really support DISCARD, disable it */ 856bcb44433SMike Snitzer limits->max_discard_sectors = 0; 857bcb44433SMike Snitzer blk_queue_flag_clear(QUEUE_FLAG_DISCARD, md->queue); 858bcb44433SMike Snitzer } 859bcb44433SMike Snitzer 8604cc96131SMike Snitzer void disable_write_same(struct mapped_device *md) 8617eee4ae2SMike Snitzer { 8627eee4ae2SMike Snitzer struct queue_limits *limits = dm_get_queue_limits(md); 8637eee4ae2SMike Snitzer 8647eee4ae2SMike Snitzer /* device doesn't really support WRITE SAME, disable it */ 8657eee4ae2SMike Snitzer limits->max_write_same_sectors = 0; 8667eee4ae2SMike Snitzer } 8677eee4ae2SMike Snitzer 868ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md) 869ac62d620SChristoph Hellwig { 870ac62d620SChristoph Hellwig struct queue_limits *limits = dm_get_queue_limits(md); 871ac62d620SChristoph Hellwig 872ac62d620SChristoph Hellwig /* device doesn't really support WRITE ZEROES, disable it */ 873ac62d620SChristoph Hellwig limits->max_write_zeroes_sectors = 0; 874ac62d620SChristoph Hellwig } 875ac62d620SChristoph Hellwig 876a666e5c0SMikulas Patocka static bool swap_bios_limit(struct dm_target *ti, struct bio *bio) 877a666e5c0SMikulas Patocka { 878a666e5c0SMikulas Patocka return unlikely((bio->bi_opf & REQ_SWAP) != 0) && unlikely(ti->limit_swap_bios); 879a666e5c0SMikulas Patocka } 880a666e5c0SMikulas Patocka 8814246a0b6SChristoph Hellwig static void clone_endio(struct bio *bio) 8821da177e4SLinus Torvalds { 8834e4cbee9SChristoph Hellwig blk_status_t error = bio->bi_status; 8846c23f0bdSChristoph Hellwig struct dm_target_io *tio = clone_to_tio(bio); 885b35f8caaSMilan Broz struct dm_io *io = tio->io; 8869faf400fSStefan Bader struct mapped_device *md = tio->io->md; 8871da177e4SLinus Torvalds dm_endio_fn endio = tio->ti->type->end_io; 888309dca30SChristoph Hellwig struct request_queue *q = bio->bi_bdev->bd_disk->queue; 8891da177e4SLinus Torvalds 8909c37de29SMike Snitzer if (unlikely(error == BLK_STS_TARGET)) { 891bcb44433SMike Snitzer if (bio_op(bio) == REQ_OP_DISCARD && 892309dca30SChristoph Hellwig !q->limits.max_discard_sectors) 893bcb44433SMike Snitzer disable_discard(md); 894bcb44433SMike Snitzer else if (bio_op(bio) == REQ_OP_WRITE_SAME && 895309dca30SChristoph Hellwig !q->limits.max_write_same_sectors) 8967eee4ae2SMike Snitzer disable_write_same(md); 897bcb44433SMike Snitzer else if (bio_op(bio) == REQ_OP_WRITE_ZEROES && 898309dca30SChristoph Hellwig !q->limits.max_write_zeroes_sectors) 899ac62d620SChristoph Hellwig disable_write_zeroes(md); 900ac62d620SChristoph Hellwig } 9017eee4ae2SMike Snitzer 902bb37d772SDamien Le Moal if (blk_queue_is_zoned(q)) 903bb37d772SDamien Le Moal dm_zone_endio(io, bio); 904415c79e1SJohannes Thumshirn 9051be56909SChristoph Hellwig if (endio) { 9064e4cbee9SChristoph Hellwig int r = endio(tio->ti, bio, &error); 9071be56909SChristoph Hellwig switch (r) { 9081be56909SChristoph Hellwig case DM_ENDIO_REQUEUE: 909bf14e2b2SDamien Le Moal /* 910bf14e2b2SDamien Le Moal * Requeuing writes to a sequential zone of a zoned 911bf14e2b2SDamien Le Moal * target will break the sequential write pattern: 912bf14e2b2SDamien Le Moal * fail such IO. 913bf14e2b2SDamien Le Moal */ 914bf14e2b2SDamien Le Moal if (WARN_ON_ONCE(dm_is_zone_write(md, bio))) 915bf14e2b2SDamien Le Moal error = BLK_STS_IOERR; 916bf14e2b2SDamien Le Moal else 9174e4cbee9SChristoph Hellwig error = BLK_STS_DM_REQUEUE; 918df561f66SGustavo A. R. Silva fallthrough; 9191be56909SChristoph Hellwig case DM_ENDIO_DONE: 9201be56909SChristoph Hellwig break; 9211be56909SChristoph Hellwig case DM_ENDIO_INCOMPLETE: 9221be56909SChristoph Hellwig /* The target will handle the io */ 9231be56909SChristoph Hellwig return; 9241be56909SChristoph Hellwig default: 9251be56909SChristoph Hellwig DMWARN("unimplemented target endio return value: %d", r); 9261be56909SChristoph Hellwig BUG(); 9271be56909SChristoph Hellwig } 9281be56909SChristoph Hellwig } 9291be56909SChristoph Hellwig 930a666e5c0SMikulas Patocka if (unlikely(swap_bios_limit(tio->ti, bio))) { 931a666e5c0SMikulas Patocka struct mapped_device *md = io->md; 932a666e5c0SMikulas Patocka up(&md->swap_bios_semaphore); 933a666e5c0SMikulas Patocka } 934a666e5c0SMikulas Patocka 935cfae7529SMike Snitzer free_tio(tio); 936e2118b3cSDamien Le Moal dm_io_dec_pending(io, error); 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds 93978d8e58aSMike Snitzer /* 94056a67df7SMike Snitzer * Return maximum size of I/O possible at the supplied sector up to the current 94156a67df7SMike Snitzer * target boundary. 94256a67df7SMike Snitzer */ 9433720281dSMike Snitzer static inline sector_t max_io_len_target_boundary(struct dm_target *ti, 9443720281dSMike Snitzer sector_t target_offset) 9451da177e4SLinus Torvalds { 94656a67df7SMike Snitzer return ti->len - target_offset; 94756a67df7SMike Snitzer } 94856a67df7SMike Snitzer 9493720281dSMike Snitzer static sector_t max_io_len(struct dm_target *ti, sector_t sector) 95056a67df7SMike Snitzer { 9513720281dSMike Snitzer sector_t target_offset = dm_target_offset(ti, sector); 9523720281dSMike Snitzer sector_t len = max_io_len_target_boundary(ti, target_offset); 9535091cdecSMike Snitzer sector_t max_len; 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds /* 9563ee16db3SMike Snitzer * Does the target need to split IO even further? 9573ee16db3SMike Snitzer * - varied (per target) IO splitting is a tenet of DM; this 9583ee16db3SMike Snitzer * explains why stacked chunk_sectors based splitting via 9593ee16db3SMike Snitzer * blk_max_size_offset() isn't possible here. So pass in 9603ee16db3SMike Snitzer * ti->max_io_len to override stacked chunk_sectors. 9611da177e4SLinus Torvalds */ 9623ee16db3SMike Snitzer if (ti->max_io_len) { 96333bd6f06SMike Snitzer max_len = blk_max_size_offset(ti->table->md->queue, 9643ee16db3SMike Snitzer target_offset, ti->max_io_len); 965542f9038SMike Snitzer if (len > max_len) 966542f9038SMike Snitzer len = max_len; 9673ee16db3SMike Snitzer } 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds return len; 9701da177e4SLinus Torvalds } 9711da177e4SLinus Torvalds 972542f9038SMike Snitzer int dm_set_target_max_io_len(struct dm_target *ti, sector_t len) 973542f9038SMike Snitzer { 974542f9038SMike Snitzer if (len > UINT_MAX) { 975542f9038SMike Snitzer DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)", 976542f9038SMike Snitzer (unsigned long long)len, UINT_MAX); 977542f9038SMike Snitzer ti->error = "Maximum size of target IO is too large"; 978542f9038SMike Snitzer return -EINVAL; 979542f9038SMike Snitzer } 980542f9038SMike Snitzer 98175ae1936SMikulas Patocka ti->max_io_len = (uint32_t) len; 982542f9038SMike Snitzer 983542f9038SMike Snitzer return 0; 984542f9038SMike Snitzer } 985542f9038SMike Snitzer EXPORT_SYMBOL_GPL(dm_set_target_max_io_len); 986542f9038SMike Snitzer 987f26c5719SDan Williams static struct dm_target *dm_dax_get_live_target(struct mapped_device *md, 988f26c5719SDan Williams sector_t sector, int *srcu_idx) 9893d97c829SMike Snitzer __acquires(md->io_barrier) 990545ed20eSToshi Kani { 991545ed20eSToshi Kani struct dm_table *map; 992545ed20eSToshi Kani struct dm_target *ti; 993545ed20eSToshi Kani 994f26c5719SDan Williams map = dm_get_live_table(md, srcu_idx); 995545ed20eSToshi Kani if (!map) 996f26c5719SDan Williams return NULL; 997545ed20eSToshi Kani 998545ed20eSToshi Kani ti = dm_table_find_target(map, sector); 999123d87d5SMikulas Patocka if (!ti) 1000f26c5719SDan Williams return NULL; 1001f26c5719SDan Williams 1002f26c5719SDan Williams return ti; 1003f26c5719SDan Williams } 1004f26c5719SDan Williams 1005f26c5719SDan Williams static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, 1006f26c5719SDan Williams long nr_pages, void **kaddr, pfn_t *pfn) 1007f26c5719SDan Williams { 1008f26c5719SDan Williams struct mapped_device *md = dax_get_private(dax_dev); 1009f26c5719SDan Williams sector_t sector = pgoff * PAGE_SECTORS; 1010f26c5719SDan Williams struct dm_target *ti; 1011f26c5719SDan Williams long len, ret = -EIO; 1012f26c5719SDan Williams int srcu_idx; 1013f26c5719SDan Williams 1014f26c5719SDan Williams ti = dm_dax_get_live_target(md, sector, &srcu_idx); 1015f26c5719SDan Williams 1016f26c5719SDan Williams if (!ti) 1017545ed20eSToshi Kani goto out; 1018f26c5719SDan Williams if (!ti->type->direct_access) 1019f26c5719SDan Williams goto out; 10203720281dSMike Snitzer len = max_io_len(ti, sector) / PAGE_SECTORS; 1021f26c5719SDan Williams if (len < 1) 1022f26c5719SDan Williams goto out; 1023f26c5719SDan Williams nr_pages = min(len, nr_pages); 1024817bf402SDan Williams ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn); 1025817bf402SDan Williams 1026545ed20eSToshi Kani out: 1027545ed20eSToshi Kani dm_put_live_table(md, srcu_idx); 1028f26c5719SDan Williams 1029f26c5719SDan Williams return ret; 1030545ed20eSToshi Kani } 1031545ed20eSToshi Kani 1032cdf6cdcdSVivek Goyal static int dm_dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff, 1033cdf6cdcdSVivek Goyal size_t nr_pages) 1034cdf6cdcdSVivek Goyal { 1035cdf6cdcdSVivek Goyal struct mapped_device *md = dax_get_private(dax_dev); 1036cdf6cdcdSVivek Goyal sector_t sector = pgoff * PAGE_SECTORS; 1037cdf6cdcdSVivek Goyal struct dm_target *ti; 1038cdf6cdcdSVivek Goyal int ret = -EIO; 1039cdf6cdcdSVivek Goyal int srcu_idx; 1040cdf6cdcdSVivek Goyal 1041cdf6cdcdSVivek Goyal ti = dm_dax_get_live_target(md, sector, &srcu_idx); 1042cdf6cdcdSVivek Goyal 1043cdf6cdcdSVivek Goyal if (!ti) 1044cdf6cdcdSVivek Goyal goto out; 1045cdf6cdcdSVivek Goyal if (WARN_ON(!ti->type->dax_zero_page_range)) { 1046cdf6cdcdSVivek Goyal /* 1047cdf6cdcdSVivek Goyal * ->zero_page_range() is mandatory dax operation. If we are 1048cdf6cdcdSVivek Goyal * here, something is wrong. 1049cdf6cdcdSVivek Goyal */ 1050cdf6cdcdSVivek Goyal goto out; 1051cdf6cdcdSVivek Goyal } 1052cdf6cdcdSVivek Goyal ret = ti->type->dax_zero_page_range(ti, pgoff, nr_pages); 1053cdf6cdcdSVivek Goyal out: 1054cdf6cdcdSVivek Goyal dm_put_live_table(md, srcu_idx); 1055cdf6cdcdSVivek Goyal 1056cdf6cdcdSVivek Goyal return ret; 1057cdf6cdcdSVivek Goyal } 1058cdf6cdcdSVivek Goyal 10591dd40c3eSMikulas Patocka /* 10601dd40c3eSMikulas Patocka * A target may call dm_accept_partial_bio only from the map routine. It is 10616842d264SDamien Le Moal * allowed for all bio types except REQ_PREFLUSH, REQ_OP_ZONE_* zone management 10626842d264SDamien Le Moal * operations and REQ_OP_ZONE_APPEND (zone append writes). 10631dd40c3eSMikulas Patocka * 10641dd40c3eSMikulas Patocka * dm_accept_partial_bio informs the dm that the target only wants to process 10651dd40c3eSMikulas Patocka * additional n_sectors sectors of the bio and the rest of the data should be 10661dd40c3eSMikulas Patocka * sent in a next bio. 10671dd40c3eSMikulas Patocka * 10681dd40c3eSMikulas Patocka * A diagram that explains the arithmetics: 10691dd40c3eSMikulas Patocka * +--------------------+---------------+-------+ 10701dd40c3eSMikulas Patocka * | 1 | 2 | 3 | 10711dd40c3eSMikulas Patocka * +--------------------+---------------+-------+ 10721dd40c3eSMikulas Patocka * 10731dd40c3eSMikulas Patocka * <-------------- *tio->len_ptr ---------------> 10741dd40c3eSMikulas Patocka * <------- bi_size -------> 10751dd40c3eSMikulas Patocka * <-- n_sectors --> 10761dd40c3eSMikulas Patocka * 10771dd40c3eSMikulas Patocka * Region 1 was already iterated over with bio_advance or similar function. 10781dd40c3eSMikulas Patocka * (it may be empty if the target doesn't use bio_advance) 10791dd40c3eSMikulas Patocka * Region 2 is the remaining bio size that the target wants to process. 10801dd40c3eSMikulas Patocka * (it may be empty if region 1 is non-empty, although there is no reason 10811dd40c3eSMikulas Patocka * to make it empty) 10821dd40c3eSMikulas Patocka * The target requires that region 3 is to be sent in the next bio. 10831dd40c3eSMikulas Patocka * 10841dd40c3eSMikulas Patocka * If the target wants to receive multiple copies of the bio (via num_*bios, etc), 10851dd40c3eSMikulas Patocka * the partially processed part (the sum of regions 1+2) must be the same for all 10861dd40c3eSMikulas Patocka * copies of the bio. 10871dd40c3eSMikulas Patocka */ 10881dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors) 10891dd40c3eSMikulas Patocka { 10906c23f0bdSChristoph Hellwig struct dm_target_io *tio = clone_to_tio(bio); 10911dd40c3eSMikulas Patocka unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT; 10926842d264SDamien Le Moal 10931eff9d32SJens Axboe BUG_ON(bio->bi_opf & REQ_PREFLUSH); 10946842d264SDamien Le Moal BUG_ON(op_is_zone_mgmt(bio_op(bio))); 10956842d264SDamien Le Moal BUG_ON(bio_op(bio) == REQ_OP_ZONE_APPEND); 10961dd40c3eSMikulas Patocka BUG_ON(bi_size > *tio->len_ptr); 10971dd40c3eSMikulas Patocka BUG_ON(n_sectors > bi_size); 10986842d264SDamien Le Moal 10991dd40c3eSMikulas Patocka *tio->len_ptr -= bi_size - n_sectors; 11001dd40c3eSMikulas Patocka bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT; 11011dd40c3eSMikulas Patocka } 11021dd40c3eSMikulas Patocka EXPORT_SYMBOL_GPL(dm_accept_partial_bio); 11031dd40c3eSMikulas Patocka 1104a666e5c0SMikulas Patocka static noinline void __set_swap_bios_limit(struct mapped_device *md, int latch) 1105a666e5c0SMikulas Patocka { 1106a666e5c0SMikulas Patocka mutex_lock(&md->swap_bios_lock); 1107a666e5c0SMikulas Patocka while (latch < md->swap_bios) { 1108a666e5c0SMikulas Patocka cond_resched(); 1109a666e5c0SMikulas Patocka down(&md->swap_bios_semaphore); 1110a666e5c0SMikulas Patocka md->swap_bios--; 1111a666e5c0SMikulas Patocka } 1112a666e5c0SMikulas Patocka while (latch > md->swap_bios) { 1113a666e5c0SMikulas Patocka cond_resched(); 1114a666e5c0SMikulas Patocka up(&md->swap_bios_semaphore); 1115a666e5c0SMikulas Patocka md->swap_bios++; 1116a666e5c0SMikulas Patocka } 1117a666e5c0SMikulas Patocka mutex_unlock(&md->swap_bios_lock); 1118a666e5c0SMikulas Patocka } 1119a666e5c0SMikulas Patocka 1120*1561b396SChristoph Hellwig static void __map_bio(struct bio *clone) 11211da177e4SLinus Torvalds { 1122*1561b396SChristoph Hellwig struct dm_target_io *tio = clone_to_tio(clone); 11231da177e4SLinus Torvalds int r; 11242056a782SJens Axboe sector_t sector; 112564f52b0eSMike Snitzer struct dm_io *io = tio->io; 1126bd2a49b8SAlasdair G Kergon struct dm_target *ti = tio->ti; 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds clone->bi_end_io = clone_endio; 11291da177e4SLinus Torvalds 11301da177e4SLinus Torvalds /* 11311da177e4SLinus Torvalds * Map the clone. If r == 0 we don't need to do 11321da177e4SLinus Torvalds * anything, the target has assumed ownership of 11331da177e4SLinus Torvalds * this io. 11341da177e4SLinus Torvalds */ 1135e2118b3cSDamien Le Moal dm_io_inc_pending(io); 11364f024f37SKent Overstreet sector = clone->bi_iter.bi_sector; 1137d67a5f4bSMikulas Patocka 1138a666e5c0SMikulas Patocka if (unlikely(swap_bios_limit(ti, clone))) { 1139a666e5c0SMikulas Patocka struct mapped_device *md = io->md; 1140a666e5c0SMikulas Patocka int latch = get_swap_bios(); 1141a666e5c0SMikulas Patocka if (unlikely(latch != md->swap_bios)) 1142a666e5c0SMikulas Patocka __set_swap_bios_limit(md, latch); 1143a666e5c0SMikulas Patocka down(&md->swap_bios_semaphore); 1144a666e5c0SMikulas Patocka } 1145a666e5c0SMikulas Patocka 1146bb37d772SDamien Le Moal /* 1147bb37d772SDamien Le Moal * Check if the IO needs a special mapping due to zone append emulation 1148bb37d772SDamien Le Moal * on zoned target. In this case, dm_zone_map_bio() calls the target 1149bb37d772SDamien Le Moal * map operation. 1150bb37d772SDamien Le Moal */ 1151bb37d772SDamien Le Moal if (dm_emulate_zone_append(io->md)) 1152bb37d772SDamien Le Moal r = dm_zone_map_bio(tio); 1153bb37d772SDamien Le Moal else 11547de3ee57SMikulas Patocka r = ti->type->map(ti, clone); 1155bb37d772SDamien Le Moal 1156846785e6SChristoph Hellwig switch (r) { 1157846785e6SChristoph Hellwig case DM_MAPIO_SUBMITTED: 1158846785e6SChristoph Hellwig break; 1159846785e6SChristoph Hellwig case DM_MAPIO_REMAPPED: 11601da177e4SLinus Torvalds /* the bio has been remapped so dispatch it */ 11611c02fca6SChristoph Hellwig trace_block_bio_remap(clone, bio_dev(io->orig_bio), sector); 11623e08773cSChristoph Hellwig submit_bio_noacct(clone); 1163846785e6SChristoph Hellwig break; 1164846785e6SChristoph Hellwig case DM_MAPIO_KILL: 1165a666e5c0SMikulas Patocka if (unlikely(swap_bios_limit(ti, clone))) { 1166a666e5c0SMikulas Patocka struct mapped_device *md = io->md; 1167a666e5c0SMikulas Patocka up(&md->swap_bios_semaphore); 1168a666e5c0SMikulas Patocka } 11694e4cbee9SChristoph Hellwig free_tio(tio); 1170e2118b3cSDamien Le Moal dm_io_dec_pending(io, BLK_STS_IOERR); 11714e4cbee9SChristoph Hellwig break; 1172846785e6SChristoph Hellwig case DM_MAPIO_REQUEUE: 1173a666e5c0SMikulas Patocka if (unlikely(swap_bios_limit(ti, clone))) { 1174a666e5c0SMikulas Patocka struct mapped_device *md = io->md; 1175a666e5c0SMikulas Patocka up(&md->swap_bios_semaphore); 1176a666e5c0SMikulas Patocka } 1177cfae7529SMike Snitzer free_tio(tio); 1178e2118b3cSDamien Le Moal dm_io_dec_pending(io, BLK_STS_DM_REQUEUE); 1179846785e6SChristoph Hellwig break; 1180846785e6SChristoph Hellwig default: 118145cbcd79SKiyoshi Ueda DMWARN("unimplemented target map return value: %d", r); 118245cbcd79SKiyoshi Ueda BUG(); 11831da177e4SLinus Torvalds } 11841da177e4SLinus Torvalds } 11851da177e4SLinus Torvalds 1186e0d6609aSMikulas Patocka static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len) 1187bd2a49b8SAlasdair G Kergon { 11884f024f37SKent Overstreet bio->bi_iter.bi_sector = sector; 11894f024f37SKent Overstreet bio->bi_iter.bi_size = to_bytes(len); 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds 11921da177e4SLinus Torvalds /* 11931da177e4SLinus Torvalds * Creates a bio that consists of range of complete bvecs. 11941da177e4SLinus Torvalds */ 1195b1bee792SChristoph Hellwig static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti, 1196b1bee792SChristoph Hellwig sector_t sector, unsigned *len) 11971da177e4SLinus Torvalds { 1198b1bee792SChristoph Hellwig struct bio *bio = ci->bio, *clone; 1199b1bee792SChristoph Hellwig struct dm_target_io *tio; 120007560151SEric Biggers int r; 12011da177e4SLinus Torvalds 1202dc8e2021SChristoph Hellwig tio = alloc_tio(ci, ti, 0, len, GFP_NOIO); 1203b1bee792SChristoph Hellwig clone = &tio->clone; 12049c47008dSMartin K. Petersen 120507560151SEric Biggers r = bio_crypt_clone(clone, bio, GFP_NOIO); 120607560151SEric Biggers if (r < 0) 1207b1bee792SChristoph Hellwig goto free_tio; 1208a892c8d5SSatya Tangirala 120957c36519SMike Snitzer if (bio_integrity(bio)) { 1210e2460f2aSMikulas Patocka if (unlikely(!dm_target_has_integrity(tio->ti->type) && 1211e2460f2aSMikulas Patocka !dm_target_passes_integrity(tio->ti->type))) { 1212e2460f2aSMikulas Patocka DMWARN("%s: the target %s doesn't support integrity data.", 1213e2460f2aSMikulas Patocka dm_device_name(tio->io->md), 1214e2460f2aSMikulas Patocka tio->ti->type->name); 1215b1bee792SChristoph Hellwig r = -EIO; 1216b1bee792SChristoph Hellwig goto free_tio; 1217e2460f2aSMikulas Patocka } 1218e2460f2aSMikulas Patocka 1219e2460f2aSMikulas Patocka r = bio_integrity_clone(clone, bio, GFP_NOIO); 1220c80914e8SMike Snitzer if (r < 0) 1221b1bee792SChristoph Hellwig goto free_tio; 1222c80914e8SMike Snitzer } 12231c3b13e6SKent Overstreet 1224fa8db494SMike Snitzer bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector)); 1225b1bee792SChristoph Hellwig clone->bi_iter.bi_size = to_bytes(*len); 1226fa8db494SMike Snitzer 1227fa8db494SMike Snitzer if (bio_integrity(bio)) 1228fa8db494SMike Snitzer bio_integrity_trim(clone); 1229c80914e8SMike Snitzer 1230*1561b396SChristoph Hellwig __map_bio(clone); 1231c80914e8SMike Snitzer return 0; 1232b1bee792SChristoph Hellwig free_tio: 1233b1bee792SChristoph Hellwig free_tio(tio); 1234b1bee792SChristoph Hellwig return r; 12351da177e4SLinus Torvalds } 12361da177e4SLinus Torvalds 1237318716ddSMike Snitzer static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci, 1238dc8e2021SChristoph Hellwig struct dm_target *ti, unsigned num_bios, 1239dc8e2021SChristoph Hellwig unsigned *len) 1240f9ab94ceSMikulas Patocka { 1241dba14160SMikulas Patocka struct dm_target_io *tio; 1242318716ddSMike Snitzer int try; 1243dba14160SMikulas Patocka 1244318716ddSMike Snitzer if (!num_bios) 1245318716ddSMike Snitzer return; 1246f9ab94ceSMikulas Patocka 1247318716ddSMike Snitzer if (num_bios == 1) { 1248dc8e2021SChristoph Hellwig tio = alloc_tio(ci, ti, 0, len, GFP_NOIO); 1249318716ddSMike Snitzer bio_list_add(blist, &tio->clone); 1250318716ddSMike Snitzer return; 12519015df24SAlasdair G Kergon } 12529015df24SAlasdair G Kergon 1253318716ddSMike Snitzer for (try = 0; try < 2; try++) { 1254318716ddSMike Snitzer int bio_nr; 1255318716ddSMike Snitzer struct bio *bio; 1256318716ddSMike Snitzer 1257318716ddSMike Snitzer if (try) 1258bc02cdbeSMike Snitzer mutex_lock(&ci->io->md->table_devices_lock); 1259318716ddSMike Snitzer for (bio_nr = 0; bio_nr < num_bios; bio_nr++) { 1260dc8e2021SChristoph Hellwig tio = alloc_tio(ci, ti, bio_nr, len, 1261dc8e2021SChristoph Hellwig try ? GFP_NOIO : GFP_NOWAIT); 1262318716ddSMike Snitzer if (!tio) 1263318716ddSMike Snitzer break; 1264318716ddSMike Snitzer 1265318716ddSMike Snitzer bio_list_add(blist, &tio->clone); 1266318716ddSMike Snitzer } 1267318716ddSMike Snitzer if (try) 1268bc02cdbeSMike Snitzer mutex_unlock(&ci->io->md->table_devices_lock); 1269318716ddSMike Snitzer if (bio_nr == num_bios) 1270318716ddSMike Snitzer return; 1271318716ddSMike Snitzer 12726c23f0bdSChristoph Hellwig while ((bio = bio_list_pop(blist))) 12736c23f0bdSChristoph Hellwig free_tio(clone_to_tio(bio)); 1274318716ddSMike Snitzer } 1275318716ddSMike Snitzer } 1276318716ddSMike Snitzer 12778eabf5d0SChristoph Hellwig static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti, 12788eabf5d0SChristoph Hellwig unsigned num_bios, unsigned *len) 12799015df24SAlasdair G Kergon { 12808eabf5d0SChristoph Hellwig struct bio_list blist = BIO_EMPTY_LIST; 12818eabf5d0SChristoph Hellwig struct bio *clone; 12828eabf5d0SChristoph Hellwig 1283dc8e2021SChristoph Hellwig alloc_multiple_bios(&blist, ci, ti, num_bios, len); 12848eabf5d0SChristoph Hellwig 12858eabf5d0SChristoph Hellwig while ((clone = bio_list_pop(&blist))) { 1286bd2a49b8SAlasdair G Kergon if (len) 12871dd40c3eSMikulas Patocka bio_setup_sector(clone, ci->sector, *len); 1288*1561b396SChristoph Hellwig __map_bio(clone); 1289f9ab94ceSMikulas Patocka } 129006a426ceSMike Snitzer } 129106a426ceSMike Snitzer 129214fe594dSAlasdair G Kergon static int __send_empty_flush(struct clone_info *ci) 1293f9ab94ceSMikulas Patocka { 129406a426ceSMike Snitzer unsigned target_nr = 0; 1295f9ab94ceSMikulas Patocka struct dm_target *ti; 1296828678b8SMike Snitzer struct bio flush_bio; 1297828678b8SMike Snitzer 1298828678b8SMike Snitzer /* 1299828678b8SMike Snitzer * Use an on-stack bio for this, it's safe since we don't 1300828678b8SMike Snitzer * need to reference it after submit. It's just used as 1301828678b8SMike Snitzer * the basis for the clone(s). 1302828678b8SMike Snitzer */ 130349add496SChristoph Hellwig bio_init(&flush_bio, ci->io->md->disk->part0, NULL, 0, 130449add496SChristoph Hellwig REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC); 130547d95102SChristoph Hellwig 1306828678b8SMike Snitzer ci->bio = &flush_bio; 1307828678b8SMike Snitzer ci->sector_count = 0; 1308f9ab94ceSMikulas Patocka 1309b372d360SMike Snitzer BUG_ON(bio_has_data(ci->bio)); 1310f9ab94ceSMikulas Patocka while ((ti = dm_table_get_target(ci->map, target_nr++))) 13111dd40c3eSMikulas Patocka __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL); 1312828678b8SMike Snitzer 1313828678b8SMike Snitzer bio_uninit(ci->bio); 1314f9ab94ceSMikulas Patocka return 0; 1315f9ab94ceSMikulas Patocka } 1316f9ab94ceSMikulas Patocka 13173d7f4562SMike Snitzer static int __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti, 131861697a6aSMike Snitzer unsigned num_bios) 13195ae89a87SMike Snitzer { 132051b86f9aSMichael Lass unsigned len; 13215ae89a87SMike Snitzer 13225ae89a87SMike Snitzer /* 132323508a96SMike Snitzer * Even though the device advertised support for this type of 132423508a96SMike Snitzer * request, that does not mean every target supports it, and 1325936688d7SMike Snitzer * reconfiguration might also have changed that since the 13265ae89a87SMike Snitzer * check was performed. 13275ae89a87SMike Snitzer */ 132855a62eefSAlasdair G Kergon if (!num_bios) 13295ae89a87SMike Snitzer return -EOPNOTSUPP; 13305ae89a87SMike Snitzer 13313720281dSMike Snitzer len = min_t(sector_t, ci->sector_count, 13323720281dSMike Snitzer max_io_len_target_boundary(ti, dm_target_offset(ti, ci->sector))); 133351b86f9aSMichael Lass 13341dd40c3eSMikulas Patocka __send_duplicate_bios(ci, ti, num_bios, &len); 13355ae89a87SMike Snitzer 1336a79245b3SMike Snitzer ci->sector += len; 13373d7f4562SMike Snitzer ci->sector_count -= len; 13385ae89a87SMike Snitzer 13395ae89a87SMike Snitzer return 0; 13405ae89a87SMike Snitzer } 13415ae89a87SMike Snitzer 1342568c73a3SMike Snitzer static bool is_abnormal_io(struct bio *bio) 1343568c73a3SMike Snitzer { 1344568c73a3SMike Snitzer bool r = false; 1345568c73a3SMike Snitzer 1346568c73a3SMike Snitzer switch (bio_op(bio)) { 1347568c73a3SMike Snitzer case REQ_OP_DISCARD: 1348568c73a3SMike Snitzer case REQ_OP_SECURE_ERASE: 1349568c73a3SMike Snitzer case REQ_OP_WRITE_SAME: 1350568c73a3SMike Snitzer case REQ_OP_WRITE_ZEROES: 1351568c73a3SMike Snitzer r = true; 1352568c73a3SMike Snitzer break; 1353568c73a3SMike Snitzer } 1354568c73a3SMike Snitzer 1355568c73a3SMike Snitzer return r; 1356568c73a3SMike Snitzer } 1357568c73a3SMike Snitzer 13580519c71eSMike Snitzer static bool __process_abnormal_io(struct clone_info *ci, struct dm_target *ti, 13590519c71eSMike Snitzer int *result) 13600519c71eSMike Snitzer { 13610519c71eSMike Snitzer struct bio *bio = ci->bio; 13629679b5a7SMike Snitzer unsigned num_bios = 0; 13630519c71eSMike Snitzer 13649679b5a7SMike Snitzer switch (bio_op(bio)) { 13659679b5a7SMike Snitzer case REQ_OP_DISCARD: 13669679b5a7SMike Snitzer num_bios = ti->num_discard_bios; 13679679b5a7SMike Snitzer break; 13689679b5a7SMike Snitzer case REQ_OP_SECURE_ERASE: 13699679b5a7SMike Snitzer num_bios = ti->num_secure_erase_bios; 13709679b5a7SMike Snitzer break; 13719679b5a7SMike Snitzer case REQ_OP_WRITE_SAME: 13729679b5a7SMike Snitzer num_bios = ti->num_write_same_bios; 13739679b5a7SMike Snitzer break; 13749679b5a7SMike Snitzer case REQ_OP_WRITE_ZEROES: 13759679b5a7SMike Snitzer num_bios = ti->num_write_zeroes_bios; 13769679b5a7SMike Snitzer break; 13779679b5a7SMike Snitzer default: 13780519c71eSMike Snitzer return false; 13799679b5a7SMike Snitzer } 13800519c71eSMike Snitzer 13819679b5a7SMike Snitzer *result = __send_changing_extent_only(ci, ti, num_bios); 13820519c71eSMike Snitzer return true; 13830519c71eSMike Snitzer } 13840519c71eSMike Snitzer 1385e4c93811SAlasdair G Kergon /* 1386e4c93811SAlasdair G Kergon * Select the correct strategy for processing a non-flush bio. 1387e4c93811SAlasdair G Kergon */ 1388e4c93811SAlasdair G Kergon static int __split_and_process_non_flush(struct clone_info *ci) 1389e4c93811SAlasdair G Kergon { 1390e4c93811SAlasdair G Kergon struct dm_target *ti; 13911c3b13e6SKent Overstreet unsigned len; 1392c80914e8SMike Snitzer int r; 1393e4c93811SAlasdair G Kergon 1394e4c93811SAlasdair G Kergon ti = dm_table_find_target(ci->map, ci->sector); 1395123d87d5SMikulas Patocka if (!ti) 1396e4c93811SAlasdair G Kergon return -EIO; 1397e4c93811SAlasdair G Kergon 1398568c73a3SMike Snitzer if (__process_abnormal_io(ci, ti, &r)) 13990519c71eSMike Snitzer return r; 14003d7f4562SMike Snitzer 14013720281dSMike Snitzer len = min_t(sector_t, max_io_len(ti, ci->sector), ci->sector_count); 1402e4c93811SAlasdair G Kergon 1403c80914e8SMike Snitzer r = __clone_and_map_data_bio(ci, ti, ci->sector, &len); 1404c80914e8SMike Snitzer if (r < 0) 1405c80914e8SMike Snitzer return r; 1406e4c93811SAlasdair G Kergon 1407e4c93811SAlasdair G Kergon ci->sector += len; 1408e4c93811SAlasdair G Kergon ci->sector_count -= len; 1409e4c93811SAlasdair G Kergon 1410e4c93811SAlasdair G Kergon return 0; 1411e4c93811SAlasdair G Kergon } 1412e4c93811SAlasdair G Kergon 1413978e51baSMike Snitzer static void init_clone_info(struct clone_info *ci, struct mapped_device *md, 1414978e51baSMike Snitzer struct dm_table *map, struct bio *bio) 1415978e51baSMike Snitzer { 1416978e51baSMike Snitzer ci->map = map; 1417978e51baSMike Snitzer ci->io = alloc_io(md, bio); 1418978e51baSMike Snitzer ci->sector = bio->bi_iter.bi_sector; 1419978e51baSMike Snitzer } 1420978e51baSMike Snitzer 1421e4c93811SAlasdair G Kergon /* 142214fe594dSAlasdair G Kergon * Entry point to split a bio into clones and submit them to the targets. 14231da177e4SLinus Torvalds */ 14243e08773cSChristoph Hellwig static void __split_and_process_bio(struct mapped_device *md, 142583d5e5b0SMikulas Patocka struct dm_table *map, struct bio *bio) 14261da177e4SLinus Torvalds { 14271da177e4SLinus Torvalds struct clone_info ci; 1428512875bdSJun'ichi Nomura int error = 0; 14291da177e4SLinus Torvalds 1430978e51baSMike Snitzer init_clone_info(&ci, md, map, bio); 1431bd2a49b8SAlasdair G Kergon 14321eff9d32SJens Axboe if (bio->bi_opf & REQ_PREFLUSH) { 143314fe594dSAlasdair G Kergon error = __send_empty_flush(&ci); 1434e2118b3cSDamien Le Moal /* dm_io_dec_pending submits any data associated with flush */ 14352e2d6f7eSAjay Joshi } else if (op_is_zone_mgmt(bio_op(bio))) { 1436a4aa5e56SDamien Le Moal ci.bio = bio; 1437a4aa5e56SDamien Le Moal ci.sector_count = 0; 1438a4aa5e56SDamien Le Moal error = __split_and_process_non_flush(&ci); 1439b372d360SMike Snitzer } else { 14406a8736d1STejun Heo ci.bio = bio; 14411da177e4SLinus Torvalds ci.sector_count = bio_sectors(bio); 144214fe594dSAlasdair G Kergon error = __split_and_process_non_flush(&ci); 1443985eabdcSJeffle Xu if (ci.sector_count && !error) { 144418a25da8SNeilBrown /* 1445ed00aabdSChristoph Hellwig * Remainder must be passed to submit_bio_noacct() 144618a25da8SNeilBrown * so that it gets handled *after* bios already submitted 144718a25da8SNeilBrown * have been completely processed. 144818a25da8SNeilBrown * We take a clone of the original to store in 1449745dc570SMike Snitzer * ci.io->orig_bio to be used by end_io_acct() and 145018a25da8SNeilBrown * for dec_pending to use for completion handling. 145118a25da8SNeilBrown */ 1452f21c601aSMike Snitzer struct bio *b = bio_split(bio, bio_sectors(bio) - ci.sector_count, 1453f21c601aSMike Snitzer GFP_NOIO, &md->queue->bio_split); 1454745dc570SMike Snitzer ci.io->orig_bio = b; 1455a1e1cb72SMike Snitzer 145618a25da8SNeilBrown bio_chain(b, bio); 1457eb6f7f7cSChristoph Hellwig trace_block_split(b, bio->bi_iter.bi_sector); 14583e08773cSChristoph Hellwig submit_bio_noacct(bio); 145918a25da8SNeilBrown } 1460d87f4c14STejun Heo } 1461b879f915SMike Snitzer start_io_acct(ci.io); 14621da177e4SLinus Torvalds 14631da177e4SLinus Torvalds /* drop the extra reference count */ 1464e2118b3cSDamien Le Moal dm_io_dec_pending(ci.io, errno_to_blk_status(error)); 14651da177e4SLinus Torvalds } 14661da177e4SLinus Torvalds 14673e08773cSChristoph Hellwig static void dm_submit_bio(struct bio *bio) 14681da177e4SLinus Torvalds { 1469309dca30SChristoph Hellwig struct mapped_device *md = bio->bi_bdev->bd_disk->private_data; 147083d5e5b0SMikulas Patocka int srcu_idx; 147183d5e5b0SMikulas Patocka struct dm_table *map; 14721da177e4SLinus Torvalds 147383d5e5b0SMikulas Patocka map = dm_get_live_table(md, &srcu_idx); 1474b2abdb1bSMike Snitzer if (unlikely(!map)) { 1475b2abdb1bSMike Snitzer DMERR_LIMIT("%s: mapping table unavailable, erroring io", 1476b2abdb1bSMike Snitzer dm_device_name(md)); 14776a8736d1STejun Heo bio_io_error(bio); 1478b2abdb1bSMike Snitzer goto out; 14791da177e4SLinus Torvalds } 148092c63902SMikulas Patocka 1481b2abdb1bSMike Snitzer /* If suspended, queue this IO for later */ 14821da177e4SLinus Torvalds if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) { 14836abc4946SKonstantin Khlebnikov if (bio->bi_opf & REQ_NOWAIT) 14846abc4946SKonstantin Khlebnikov bio_wouldblock_error(bio); 1485b2abdb1bSMike Snitzer else if (bio->bi_opf & REQ_RAHEAD) 14861da177e4SLinus Torvalds bio_io_error(bio); 1487b2abdb1bSMike Snitzer else 1488b2abdb1bSMike Snitzer queue_io(md, bio); 1489b2abdb1bSMike Snitzer goto out; 14901da177e4SLinus Torvalds } 14911da177e4SLinus Torvalds 1492b2abdb1bSMike Snitzer /* 1493b2abdb1bSMike Snitzer * Use blk_queue_split() for abnormal IO (e.g. discard, writesame, etc) 1494b2abdb1bSMike Snitzer * otherwise associated queue_limits won't be imposed. 1495b2abdb1bSMike Snitzer */ 1496b2abdb1bSMike Snitzer if (is_abnormal_io(bio)) 1497b2abdb1bSMike Snitzer blk_queue_split(&bio); 1498978e51baSMike Snitzer 14993e08773cSChristoph Hellwig __split_and_process_bio(md, map, bio); 1500b2abdb1bSMike Snitzer out: 150183d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 1502978e51baSMike Snitzer } 1503978e51baSMike Snitzer 15041da177e4SLinus Torvalds /*----------------------------------------------------------------- 15051da177e4SLinus Torvalds * An IDR is used to keep track of allocated minor numbers. 15061da177e4SLinus Torvalds *---------------------------------------------------------------*/ 15072b06cfffSAlasdair G Kergon static void free_minor(int minor) 15081da177e4SLinus Torvalds { 1509f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 15101da177e4SLinus Torvalds idr_remove(&_minor_idr, minor); 1511f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 15121da177e4SLinus Torvalds } 15131da177e4SLinus Torvalds 15141da177e4SLinus Torvalds /* 15151da177e4SLinus Torvalds * See if the device with a specific minor # is free. 15161da177e4SLinus Torvalds */ 1517cf13ab8eSFrederik Deweerdt static int specific_minor(int minor) 15181da177e4SLinus Torvalds { 1519c9d76be6STejun Heo int r; 15201da177e4SLinus Torvalds 15211da177e4SLinus Torvalds if (minor >= (1 << MINORBITS)) 15221da177e4SLinus Torvalds return -EINVAL; 15231da177e4SLinus Torvalds 1524c9d76be6STejun Heo idr_preload(GFP_KERNEL); 1525f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 15261da177e4SLinus Torvalds 1527c9d76be6STejun Heo r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT); 15281da177e4SLinus Torvalds 1529f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1530c9d76be6STejun Heo idr_preload_end(); 1531c9d76be6STejun Heo if (r < 0) 1532c9d76be6STejun Heo return r == -ENOSPC ? -EBUSY : r; 1533c9d76be6STejun Heo return 0; 15341da177e4SLinus Torvalds } 15351da177e4SLinus Torvalds 1536cf13ab8eSFrederik Deweerdt static int next_free_minor(int *minor) 15371da177e4SLinus Torvalds { 1538c9d76be6STejun Heo int r; 15391da177e4SLinus Torvalds 1540c9d76be6STejun Heo idr_preload(GFP_KERNEL); 1541f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 15421da177e4SLinus Torvalds 1543c9d76be6STejun Heo r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT); 15441da177e4SLinus Torvalds 1545f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1546c9d76be6STejun Heo idr_preload_end(); 1547c9d76be6STejun Heo if (r < 0) 15481da177e4SLinus Torvalds return r; 1549c9d76be6STejun Heo *minor = r; 1550c9d76be6STejun Heo return 0; 15511da177e4SLinus Torvalds } 15521da177e4SLinus Torvalds 155383d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops; 1554681cc5e8SMike Snitzer static const struct block_device_operations dm_rq_blk_dops; 1555f26c5719SDan Williams static const struct dax_operations dm_dax_ops; 15561da177e4SLinus Torvalds 155753d5914fSMikulas Patocka static void dm_wq_work(struct work_struct *work); 155853d5914fSMikulas Patocka 1559aa6ce87aSSatya Tangirala #ifdef CONFIG_BLK_INLINE_ENCRYPTION 1560cb77cb5aSEric Biggers static void dm_queue_destroy_crypto_profile(struct request_queue *q) 1561aa6ce87aSSatya Tangirala { 1562cb77cb5aSEric Biggers dm_destroy_crypto_profile(q->crypto_profile); 1563aa6ce87aSSatya Tangirala } 1564aa6ce87aSSatya Tangirala 1565aa6ce87aSSatya Tangirala #else /* CONFIG_BLK_INLINE_ENCRYPTION */ 1566aa6ce87aSSatya Tangirala 1567cb77cb5aSEric Biggers static inline void dm_queue_destroy_crypto_profile(struct request_queue *q) 1568aa6ce87aSSatya Tangirala { 1569aa6ce87aSSatya Tangirala } 1570aa6ce87aSSatya Tangirala #endif /* !CONFIG_BLK_INLINE_ENCRYPTION */ 1571aa6ce87aSSatya Tangirala 15720f20972fSMike Snitzer static void cleanup_mapped_device(struct mapped_device *md) 15730f20972fSMike Snitzer { 15740f20972fSMike Snitzer if (md->wq) 15750f20972fSMike Snitzer destroy_workqueue(md->wq); 15766f1c819cSKent Overstreet bioset_exit(&md->bs); 15776f1c819cSKent Overstreet bioset_exit(&md->io_bs); 15780f20972fSMike Snitzer 1579f26c5719SDan Williams if (md->dax_dev) { 1580fb08a190SChristoph Hellwig dax_remove_host(md->disk); 1581f26c5719SDan Williams kill_dax(md->dax_dev); 1582f26c5719SDan Williams put_dax(md->dax_dev); 1583f26c5719SDan Williams md->dax_dev = NULL; 1584f26c5719SDan Williams } 1585f26c5719SDan Williams 15860f20972fSMike Snitzer if (md->disk) { 15870f20972fSMike Snitzer spin_lock(&_minor_lock); 15880f20972fSMike Snitzer md->disk->private_data = NULL; 15890f20972fSMike Snitzer spin_unlock(&_minor_lock); 159089f871afSChristoph Hellwig if (dm_get_md_type(md) != DM_TYPE_NONE) { 159189f871afSChristoph Hellwig dm_sysfs_exit(md); 15920f20972fSMike Snitzer del_gendisk(md->disk); 159389f871afSChristoph Hellwig } 1594cb77cb5aSEric Biggers dm_queue_destroy_crypto_profile(md->queue); 159574fe6ba9SChristoph Hellwig blk_cleanup_disk(md->disk); 159674a2b6ecSChristoph Hellwig } 15970f20972fSMike Snitzer 1598d09960b0STahsin Erdogan cleanup_srcu_struct(&md->io_barrier); 1599d09960b0STahsin Erdogan 1600d5ffebddSMike Snitzer mutex_destroy(&md->suspend_lock); 1601d5ffebddSMike Snitzer mutex_destroy(&md->type_lock); 1602d5ffebddSMike Snitzer mutex_destroy(&md->table_devices_lock); 1603a666e5c0SMikulas Patocka mutex_destroy(&md->swap_bios_lock); 1604d5ffebddSMike Snitzer 16054cc96131SMike Snitzer dm_mq_cleanup_mapped_device(md); 1606bb37d772SDamien Le Moal dm_cleanup_zoned_dev(md); 16070f20972fSMike Snitzer } 16080f20972fSMike Snitzer 16091da177e4SLinus Torvalds /* 16101da177e4SLinus Torvalds * Allocate and initialise a blank device with a given minor. 16111da177e4SLinus Torvalds */ 16122b06cfffSAlasdair G Kergon static struct mapped_device *alloc_dev(int minor) 16131da177e4SLinus Torvalds { 1614115485e8SMike Snitzer int r, numa_node_id = dm_get_numa_node(); 1615115485e8SMike Snitzer struct mapped_device *md; 1616ba61fdd1SJeff Mahoney void *old_md; 16171da177e4SLinus Torvalds 1618856eb091SMikulas Patocka md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id); 16191da177e4SLinus Torvalds if (!md) { 16201da177e4SLinus Torvalds DMWARN("unable to allocate device, out of memory."); 16211da177e4SLinus Torvalds return NULL; 16221da177e4SLinus Torvalds } 16231da177e4SLinus Torvalds 162410da4f79SJeff Mahoney if (!try_module_get(THIS_MODULE)) 16256ed7ade8SMilan Broz goto bad_module_get; 162610da4f79SJeff Mahoney 16271da177e4SLinus Torvalds /* get a minor number for the dev */ 16282b06cfffSAlasdair G Kergon if (minor == DM_ANY_MINOR) 1629cf13ab8eSFrederik Deweerdt r = next_free_minor(&minor); 16302b06cfffSAlasdair G Kergon else 1631cf13ab8eSFrederik Deweerdt r = specific_minor(minor); 16321da177e4SLinus Torvalds if (r < 0) 16336ed7ade8SMilan Broz goto bad_minor; 16341da177e4SLinus Torvalds 163583d5e5b0SMikulas Patocka r = init_srcu_struct(&md->io_barrier); 163683d5e5b0SMikulas Patocka if (r < 0) 163783d5e5b0SMikulas Patocka goto bad_io_barrier; 163883d5e5b0SMikulas Patocka 1639115485e8SMike Snitzer md->numa_node_id = numa_node_id; 1640591ddcfcSMike Snitzer md->init_tio_pdu = false; 1641a5664dadSMike Snitzer md->type = DM_TYPE_NONE; 1642e61290a4SDaniel Walker mutex_init(&md->suspend_lock); 1643a5664dadSMike Snitzer mutex_init(&md->type_lock); 164486f1152bSBenjamin Marzinski mutex_init(&md->table_devices_lock); 1645022c2611SMikulas Patocka spin_lock_init(&md->deferred_lock); 16461da177e4SLinus Torvalds atomic_set(&md->holders, 1); 16475c6bd75dSAlasdair G Kergon atomic_set(&md->open_count, 0); 16481da177e4SLinus Torvalds atomic_set(&md->event_nr, 0); 16497a8c3d3bSMike Anderson atomic_set(&md->uevent_seq, 0); 16507a8c3d3bSMike Anderson INIT_LIST_HEAD(&md->uevent_list); 165186f1152bSBenjamin Marzinski INIT_LIST_HEAD(&md->table_devices); 16527a8c3d3bSMike Anderson spin_lock_init(&md->uevent_lock); 16531da177e4SLinus Torvalds 165447ace7e0SMike Snitzer /* 1655c62b37d9SChristoph Hellwig * default to bio-based until DM table is loaded and md->type 1656c62b37d9SChristoph Hellwig * established. If request-based table is loaded: blk-mq will 1657c62b37d9SChristoph Hellwig * override accordingly. 165847ace7e0SMike Snitzer */ 165974fe6ba9SChristoph Hellwig md->disk = blk_alloc_disk(md->numa_node_id); 16601da177e4SLinus Torvalds if (!md->disk) 16610f20972fSMike Snitzer goto bad; 166274fe6ba9SChristoph Hellwig md->queue = md->disk->queue; 16631da177e4SLinus Torvalds 1664f0b04115SJeff Mahoney init_waitqueue_head(&md->wait); 166553d5914fSMikulas Patocka INIT_WORK(&md->work, dm_wq_work); 1666f0b04115SJeff Mahoney init_waitqueue_head(&md->eventq); 16672995fa78SMikulas Patocka init_completion(&md->kobj_holder.completion); 1668f0b04115SJeff Mahoney 1669a666e5c0SMikulas Patocka md->swap_bios = get_swap_bios(); 1670a666e5c0SMikulas Patocka sema_init(&md->swap_bios_semaphore, md->swap_bios); 1671a666e5c0SMikulas Patocka mutex_init(&md->swap_bios_lock); 1672a666e5c0SMikulas Patocka 16731da177e4SLinus Torvalds md->disk->major = _major; 16741da177e4SLinus Torvalds md->disk->first_minor = minor; 167574fe6ba9SChristoph Hellwig md->disk->minors = 1; 16761ebe2e5fSChristoph Hellwig md->disk->flags |= GENHD_FL_NO_PART; 16771da177e4SLinus Torvalds md->disk->fops = &dm_blk_dops; 16781da177e4SLinus Torvalds md->disk->queue = md->queue; 16791da177e4SLinus Torvalds md->disk->private_data = md; 16801da177e4SLinus Torvalds sprintf(md->disk->disk_name, "dm-%d", minor); 1681f26c5719SDan Williams 16825d2a228bSChristoph Hellwig if (IS_ENABLED(CONFIG_FS_DAX)) { 168330c6828aSChristoph Hellwig md->dax_dev = alloc_dax(md, &dm_dax_ops); 1684d7519392SChristoph Hellwig if (IS_ERR(md->dax_dev)) { 1685d7519392SChristoph Hellwig md->dax_dev = NULL; 1686f26c5719SDan Williams goto bad; 1687976431b0SDan Williams } 16887ac5360cSChristoph Hellwig set_dax_nocache(md->dax_dev); 16897ac5360cSChristoph Hellwig set_dax_nomc(md->dax_dev); 1690fb08a190SChristoph Hellwig if (dax_add_host(md->dax_dev, md->disk)) 1691f26c5719SDan Williams goto bad; 1692f26c5719SDan Williams } 16931da177e4SLinus Torvalds 16947e51f257SMike Anderson format_dev_t(md->name, MKDEV(_major, minor)); 16951da177e4SLinus Torvalds 1696c7c879eeSMichał Mirosław md->wq = alloc_workqueue("kdmflush/%s", WQ_MEM_RECLAIM, 0, md->name); 1697304f3f6aSMilan Broz if (!md->wq) 16980f20972fSMike Snitzer goto bad; 1699304f3f6aSMilan Broz 1700fd2ed4d2SMikulas Patocka dm_stats_init(&md->stats); 1701fd2ed4d2SMikulas Patocka 1702ba61fdd1SJeff Mahoney /* Populate the mapping, nobody knows we exist yet */ 1703f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 1704ba61fdd1SJeff Mahoney old_md = idr_replace(&_minor_idr, md, minor); 1705f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 1706ba61fdd1SJeff Mahoney 1707ba61fdd1SJeff Mahoney BUG_ON(old_md != MINOR_ALLOCED); 1708ba61fdd1SJeff Mahoney 17091da177e4SLinus Torvalds return md; 17101da177e4SLinus Torvalds 17110f20972fSMike Snitzer bad: 17120f20972fSMike Snitzer cleanup_mapped_device(md); 171383d5e5b0SMikulas Patocka bad_io_barrier: 17141da177e4SLinus Torvalds free_minor(minor); 17156ed7ade8SMilan Broz bad_minor: 171610da4f79SJeff Mahoney module_put(THIS_MODULE); 17176ed7ade8SMilan Broz bad_module_get: 1718856eb091SMikulas Patocka kvfree(md); 17191da177e4SLinus Torvalds return NULL; 17201da177e4SLinus Torvalds } 17211da177e4SLinus Torvalds 1722ae9da83fSJun'ichi Nomura static void unlock_fs(struct mapped_device *md); 1723ae9da83fSJun'ichi Nomura 17241da177e4SLinus Torvalds static void free_dev(struct mapped_device *md) 17251da177e4SLinus Torvalds { 1726f331c029STejun Heo int minor = MINOR(disk_devt(md->disk)); 172763d94e48SJun'ichi Nomura 1728ae9da83fSJun'ichi Nomura unlock_fs(md); 17292eb6e1e3SKeith Busch 17300f20972fSMike Snitzer cleanup_mapped_device(md); 17310f20972fSMike Snitzer 17320f20972fSMike Snitzer free_table_devices(&md->table_devices); 17330f20972fSMike Snitzer dm_stats_cleanup(&md->stats); 173463a4f065SMike Snitzer free_minor(minor); 173563a4f065SMike Snitzer 173610da4f79SJeff Mahoney module_put(THIS_MODULE); 1737856eb091SMikulas Patocka kvfree(md); 17381da177e4SLinus Torvalds } 17391da177e4SLinus Torvalds 17402a2a4c51SJens Axboe static int __bind_mempools(struct mapped_device *md, struct dm_table *t) 1741e6ee8c0bSKiyoshi Ueda { 1742c0820cf5SMikulas Patocka struct dm_md_mempools *p = dm_table_get_md_mempools(t); 17432a2a4c51SJens Axboe int ret = 0; 1744e6ee8c0bSKiyoshi Ueda 1745545ed20eSToshi Kani if (dm_table_bio_based(t)) { 1746c0820cf5SMikulas Patocka /* 174764f52b0eSMike Snitzer * The md may already have mempools that need changing. 174864f52b0eSMike Snitzer * If so, reload bioset because front_pad may have changed 174916245bdcSJun'ichi Nomura * because a different table was loaded. 1750c0820cf5SMikulas Patocka */ 17516f1c819cSKent Overstreet bioset_exit(&md->bs); 17526f1c819cSKent Overstreet bioset_exit(&md->io_bs); 17530776aa0eSMike Snitzer 17546f1c819cSKent Overstreet } else if (bioset_initialized(&md->bs)) { 1755cbc4e3c1SMike Snitzer /* 17564e6e36c3SMike Snitzer * There's no need to reload with request-based dm 17574e6e36c3SMike Snitzer * because the size of front_pad doesn't change. 17584e6e36c3SMike Snitzer * Note for future: If you are to reload bioset, 17594e6e36c3SMike Snitzer * prep-ed requests in the queue may refer 17604e6e36c3SMike Snitzer * to bio from the old bioset, so you must walk 17614e6e36c3SMike Snitzer * through the queue to unprep. 1762cbc4e3c1SMike Snitzer */ 1763cbc4e3c1SMike Snitzer goto out; 1764cbc4e3c1SMike Snitzer } 1765cbc4e3c1SMike Snitzer 17666f1c819cSKent Overstreet BUG_ON(!p || 17676f1c819cSKent Overstreet bioset_initialized(&md->bs) || 17686f1c819cSKent Overstreet bioset_initialized(&md->io_bs)); 1769e6ee8c0bSKiyoshi Ueda 17702a2a4c51SJens Axboe ret = bioset_init_from_src(&md->bs, &p->bs); 17712a2a4c51SJens Axboe if (ret) 17722a2a4c51SJens Axboe goto out; 17732a2a4c51SJens Axboe ret = bioset_init_from_src(&md->io_bs, &p->io_bs); 17742a2a4c51SJens Axboe if (ret) 17752a2a4c51SJens Axboe bioset_exit(&md->bs); 1776e6ee8c0bSKiyoshi Ueda out: 177702233342SMike Snitzer /* mempool bind completed, no longer need any mempools in the table */ 1778e6ee8c0bSKiyoshi Ueda dm_table_free_md_mempools(t); 17792a2a4c51SJens Axboe return ret; 1780e6ee8c0bSKiyoshi Ueda } 1781e6ee8c0bSKiyoshi Ueda 17821da177e4SLinus Torvalds /* 17831da177e4SLinus Torvalds * Bind a table to the device. 17841da177e4SLinus Torvalds */ 17851da177e4SLinus Torvalds static void event_callback(void *context) 17861da177e4SLinus Torvalds { 17877a8c3d3bSMike Anderson unsigned long flags; 17887a8c3d3bSMike Anderson LIST_HEAD(uevents); 17891da177e4SLinus Torvalds struct mapped_device *md = (struct mapped_device *) context; 17901da177e4SLinus Torvalds 17917a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 17927a8c3d3bSMike Anderson list_splice_init(&md->uevent_list, &uevents); 17937a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 17947a8c3d3bSMike Anderson 1795ed9e1982STejun Heo dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj); 17967a8c3d3bSMike Anderson 17971da177e4SLinus Torvalds atomic_inc(&md->event_nr); 17981da177e4SLinus Torvalds wake_up(&md->eventq); 179962e08243SMikulas Patocka dm_issue_global_event(); 18001da177e4SLinus Torvalds } 18011da177e4SLinus Torvalds 1802c217649bSMike Snitzer /* 1803042d2a9bSAlasdair G Kergon * Returns old map, which caller must destroy. 1804042d2a9bSAlasdair G Kergon */ 1805042d2a9bSAlasdair G Kergon static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t, 1806754c5fc7SMike Snitzer struct queue_limits *limits) 18071da177e4SLinus Torvalds { 1808042d2a9bSAlasdair G Kergon struct dm_table *old_map; 1809165125e1SJens Axboe struct request_queue *q = md->queue; 1810978e51baSMike Snitzer bool request_based = dm_table_request_based(t); 18111da177e4SLinus Torvalds sector_t size; 18122a2a4c51SJens Axboe int ret; 18131da177e4SLinus Torvalds 18145a8f1f80SBart Van Assche lockdep_assert_held(&md->suspend_lock); 18155a8f1f80SBart Van Assche 18161da177e4SLinus Torvalds size = dm_table_get_size(t); 18173ac51e74SDarrick J. Wong 18183ac51e74SDarrick J. Wong /* 18193ac51e74SDarrick J. Wong * Wipe any geometry if the size of the table changed. 18203ac51e74SDarrick J. Wong */ 1821fd2ed4d2SMikulas Patocka if (size != dm_get_size(md)) 18223ac51e74SDarrick J. Wong memset(&md->geometry, 0, sizeof(md->geometry)); 18233ac51e74SDarrick J. Wong 18245424a0b8SMikulas Patocka if (!get_capacity(md->disk)) 18255424a0b8SMikulas Patocka set_capacity(md->disk, size); 18265424a0b8SMikulas Patocka else 1827f64d9b2eSChristoph Hellwig set_capacity_and_notify(md->disk, size); 18281da177e4SLinus Torvalds 1829cf222b37SAlasdair G Kergon dm_table_event_callback(t, event_callback, md); 18302ca3310eSAlasdair G Kergon 18319c37de29SMike Snitzer if (request_based) { 183216f12266SMike Snitzer /* 18339c37de29SMike Snitzer * Leverage the fact that request-based DM targets are 18349c37de29SMike Snitzer * immutable singletons - used to optimize dm_mq_queue_rq. 183516f12266SMike Snitzer */ 183616f12266SMike Snitzer md->immutable_target = dm_table_get_immutable_target(t); 183716f12266SMike Snitzer } 1838e6ee8c0bSKiyoshi Ueda 18392a2a4c51SJens Axboe ret = __bind_mempools(md, t); 18402a2a4c51SJens Axboe if (ret) { 18412a2a4c51SJens Axboe old_map = ERR_PTR(ret); 18422a2a4c51SJens Axboe goto out; 18432a2a4c51SJens Axboe } 1844e6ee8c0bSKiyoshi Ueda 1845bb37d772SDamien Le Moal ret = dm_table_set_restrictions(t, q, limits); 1846bb37d772SDamien Le Moal if (ret) { 1847bb37d772SDamien Le Moal old_map = ERR_PTR(ret); 1848bb37d772SDamien Le Moal goto out; 1849bb37d772SDamien Le Moal } 1850bb37d772SDamien Le Moal 1851a12f5d48SEric Dumazet old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 18521d3aa6f6SMike Snitzer rcu_assign_pointer(md->map, (void *)t); 185336a0456fSAlasdair G Kergon md->immutable_target_type = dm_table_get_immutable_target_type(t); 185436a0456fSAlasdair G Kergon 185541abc4e1SHannes Reinecke if (old_map) 185683d5e5b0SMikulas Patocka dm_sync_table(md); 18572ca3310eSAlasdair G Kergon 18582a2a4c51SJens Axboe out: 1859042d2a9bSAlasdair G Kergon return old_map; 18601da177e4SLinus Torvalds } 18611da177e4SLinus Torvalds 1862a7940155SAlasdair G Kergon /* 1863a7940155SAlasdair G Kergon * Returns unbound table for the caller to free. 1864a7940155SAlasdair G Kergon */ 1865a7940155SAlasdair G Kergon static struct dm_table *__unbind(struct mapped_device *md) 18661da177e4SLinus Torvalds { 1867a12f5d48SEric Dumazet struct dm_table *map = rcu_dereference_protected(md->map, 1); 18681da177e4SLinus Torvalds 18691da177e4SLinus Torvalds if (!map) 1870a7940155SAlasdair G Kergon return NULL; 18711da177e4SLinus Torvalds 18721da177e4SLinus Torvalds dm_table_event_callback(map, NULL, NULL); 18739cdb8520SMonam Agarwal RCU_INIT_POINTER(md->map, NULL); 187483d5e5b0SMikulas Patocka dm_sync_table(md); 1875a7940155SAlasdair G Kergon 1876a7940155SAlasdair G Kergon return map; 18771da177e4SLinus Torvalds } 18781da177e4SLinus Torvalds 18791da177e4SLinus Torvalds /* 18801da177e4SLinus Torvalds * Constructor for a new device. 18811da177e4SLinus Torvalds */ 18822b06cfffSAlasdair G Kergon int dm_create(int minor, struct mapped_device **result) 18831da177e4SLinus Torvalds { 18841da177e4SLinus Torvalds struct mapped_device *md; 18851da177e4SLinus Torvalds 18862b06cfffSAlasdair G Kergon md = alloc_dev(minor); 18871da177e4SLinus Torvalds if (!md) 18881da177e4SLinus Torvalds return -ENXIO; 18891da177e4SLinus Torvalds 189091ccbbacSTushar Sugandhi dm_ima_reset_data(md); 189191ccbbacSTushar Sugandhi 18921da177e4SLinus Torvalds *result = md; 18931da177e4SLinus Torvalds return 0; 18941da177e4SLinus Torvalds } 18951da177e4SLinus Torvalds 1896a5664dadSMike Snitzer /* 1897a5664dadSMike Snitzer * Functions to manage md->type. 1898a5664dadSMike Snitzer * All are required to hold md->type_lock. 1899a5664dadSMike Snitzer */ 1900a5664dadSMike Snitzer void dm_lock_md_type(struct mapped_device *md) 1901a5664dadSMike Snitzer { 1902a5664dadSMike Snitzer mutex_lock(&md->type_lock); 1903a5664dadSMike Snitzer } 1904a5664dadSMike Snitzer 1905a5664dadSMike Snitzer void dm_unlock_md_type(struct mapped_device *md) 1906a5664dadSMike Snitzer { 1907a5664dadSMike Snitzer mutex_unlock(&md->type_lock); 1908a5664dadSMike Snitzer } 1909a5664dadSMike Snitzer 19107e0d574fSBart Van Assche void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type) 1911a5664dadSMike Snitzer { 191200c4fc3bSMike Snitzer BUG_ON(!mutex_is_locked(&md->type_lock)); 1913a5664dadSMike Snitzer md->type = type; 1914a5664dadSMike Snitzer } 1915a5664dadSMike Snitzer 19167e0d574fSBart Van Assche enum dm_queue_mode dm_get_md_type(struct mapped_device *md) 1917a5664dadSMike Snitzer { 1918a5664dadSMike Snitzer return md->type; 1919a5664dadSMike Snitzer } 1920a5664dadSMike Snitzer 192136a0456fSAlasdair G Kergon struct target_type *dm_get_immutable_target_type(struct mapped_device *md) 192236a0456fSAlasdair G Kergon { 192336a0456fSAlasdair G Kergon return md->immutable_target_type; 192436a0456fSAlasdair G Kergon } 192536a0456fSAlasdair G Kergon 19264a0b4ddfSMike Snitzer /* 1927f84cb8a4SMike Snitzer * The queue_limits are only valid as long as you have a reference 1928f84cb8a4SMike Snitzer * count on 'md'. 1929f84cb8a4SMike Snitzer */ 1930f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md) 1931f84cb8a4SMike Snitzer { 1932f84cb8a4SMike Snitzer BUG_ON(!atomic_read(&md->holders)); 1933f84cb8a4SMike Snitzer return &md->queue->limits; 1934f84cb8a4SMike Snitzer } 1935f84cb8a4SMike Snitzer EXPORT_SYMBOL_GPL(dm_get_queue_limits); 1936f84cb8a4SMike Snitzer 19374a0b4ddfSMike Snitzer /* 19384a0b4ddfSMike Snitzer * Setup the DM device's queue based on md's type 19394a0b4ddfSMike Snitzer */ 1940591ddcfcSMike Snitzer int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t) 19414a0b4ddfSMike Snitzer { 1942ba305859SChristoph Hellwig enum dm_queue_mode type = dm_table_get_type(t); 1943c100ec49SMike Snitzer struct queue_limits limits; 1944ba305859SChristoph Hellwig int r; 1945bfebd1cdSMike Snitzer 1946545ed20eSToshi Kani switch (type) { 1947bfebd1cdSMike Snitzer case DM_TYPE_REQUEST_BASED: 1948681cc5e8SMike Snitzer md->disk->fops = &dm_rq_blk_dops; 1949e83068a5SMike Snitzer r = dm_mq_init_request_queue(md, t); 1950bfebd1cdSMike Snitzer if (r) { 1951681cc5e8SMike Snitzer DMERR("Cannot initialize queue for request-based dm mapped device"); 1952bfebd1cdSMike Snitzer return r; 1953bfebd1cdSMike Snitzer } 1954bfebd1cdSMike Snitzer break; 1955bfebd1cdSMike Snitzer case DM_TYPE_BIO_BASED: 1956545ed20eSToshi Kani case DM_TYPE_DAX_BIO_BASED: 1957bfebd1cdSMike Snitzer break; 19587e0d574fSBart Van Assche case DM_TYPE_NONE: 19597e0d574fSBart Van Assche WARN_ON_ONCE(true); 19607e0d574fSBart Van Assche break; 1961ff36ab34SMike Snitzer } 19624a0b4ddfSMike Snitzer 1963c100ec49SMike Snitzer r = dm_calculate_queue_limits(t, &limits); 1964c100ec49SMike Snitzer if (r) { 1965c100ec49SMike Snitzer DMERR("Cannot calculate initial queue limits"); 1966c100ec49SMike Snitzer return r; 1967c100ec49SMike Snitzer } 1968bb37d772SDamien Le Moal r = dm_table_set_restrictions(t, md->queue, &limits); 1969bb37d772SDamien Le Moal if (r) 1970bb37d772SDamien Le Moal return r; 197189f871afSChristoph Hellwig 1972e7089f65SLuis Chamberlain r = add_disk(md->disk); 1973e7089f65SLuis Chamberlain if (r) 1974e7089f65SLuis Chamberlain return r; 197589f871afSChristoph Hellwig 197689f871afSChristoph Hellwig r = dm_sysfs_init(md); 197789f871afSChristoph Hellwig if (r) { 197889f871afSChristoph Hellwig del_gendisk(md->disk); 197989f871afSChristoph Hellwig return r; 198089f871afSChristoph Hellwig } 1981ba305859SChristoph Hellwig md->type = type; 19824a0b4ddfSMike Snitzer return 0; 19834a0b4ddfSMike Snitzer } 19844a0b4ddfSMike Snitzer 19852bec1f4aSMikulas Patocka struct mapped_device *dm_get_md(dev_t dev) 19861da177e4SLinus Torvalds { 19871da177e4SLinus Torvalds struct mapped_device *md; 19881da177e4SLinus Torvalds unsigned minor = MINOR(dev); 19891da177e4SLinus Torvalds 19901da177e4SLinus Torvalds if (MAJOR(dev) != _major || minor >= (1 << MINORBITS)) 19911da177e4SLinus Torvalds return NULL; 19921da177e4SLinus Torvalds 1993f32c10b0SJeff Mahoney spin_lock(&_minor_lock); 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds md = idr_find(&_minor_idr, minor); 199649de5769SMike Snitzer if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) || 199749de5769SMike Snitzer test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { 1998637842cfSDavid Teigland md = NULL; 1999fba9f90eSJeff Mahoney goto out; 2000fba9f90eSJeff Mahoney } 20012bec1f4aSMikulas Patocka dm_get(md); 2002fba9f90eSJeff Mahoney out: 2003f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 20041da177e4SLinus Torvalds 2005637842cfSDavid Teigland return md; 2006637842cfSDavid Teigland } 20073cf2e4baSAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_get_md); 2008d229a958SDavid Teigland 20099ade92a9SAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md) 2010637842cfSDavid Teigland { 20119ade92a9SAlasdair G Kergon return md->interface_ptr; 20121da177e4SLinus Torvalds } 20131da177e4SLinus Torvalds 20141da177e4SLinus Torvalds void dm_set_mdptr(struct mapped_device *md, void *ptr) 20151da177e4SLinus Torvalds { 20161da177e4SLinus Torvalds md->interface_ptr = ptr; 20171da177e4SLinus Torvalds } 20181da177e4SLinus Torvalds 20191da177e4SLinus Torvalds void dm_get(struct mapped_device *md) 20201da177e4SLinus Torvalds { 20211da177e4SLinus Torvalds atomic_inc(&md->holders); 20223f77316dSKiyoshi Ueda BUG_ON(test_bit(DMF_FREEING, &md->flags)); 20231da177e4SLinus Torvalds } 20241da177e4SLinus Torvalds 202509ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md) 202609ee96b2SMikulas Patocka { 202709ee96b2SMikulas Patocka spin_lock(&_minor_lock); 202809ee96b2SMikulas Patocka if (test_bit(DMF_FREEING, &md->flags)) { 202909ee96b2SMikulas Patocka spin_unlock(&_minor_lock); 203009ee96b2SMikulas Patocka return -EBUSY; 203109ee96b2SMikulas Patocka } 203209ee96b2SMikulas Patocka dm_get(md); 203309ee96b2SMikulas Patocka spin_unlock(&_minor_lock); 203409ee96b2SMikulas Patocka return 0; 203509ee96b2SMikulas Patocka } 203609ee96b2SMikulas Patocka EXPORT_SYMBOL_GPL(dm_hold); 203709ee96b2SMikulas Patocka 203872d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md) 203972d94861SAlasdair G Kergon { 204072d94861SAlasdair G Kergon return md->name; 204172d94861SAlasdair G Kergon } 204272d94861SAlasdair G Kergon EXPORT_SYMBOL_GPL(dm_device_name); 204372d94861SAlasdair G Kergon 20443f77316dSKiyoshi Ueda static void __dm_destroy(struct mapped_device *md, bool wait) 20451da177e4SLinus Torvalds { 20461134e5aeSMike Anderson struct dm_table *map; 204783d5e5b0SMikulas Patocka int srcu_idx; 20481da177e4SLinus Torvalds 20493f77316dSKiyoshi Ueda might_sleep(); 2050fba9f90eSJeff Mahoney 205163a4f065SMike Snitzer spin_lock(&_minor_lock); 20523f77316dSKiyoshi Ueda idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md)))); 2053fba9f90eSJeff Mahoney set_bit(DMF_FREEING, &md->flags); 2054f32c10b0SJeff Mahoney spin_unlock(&_minor_lock); 20553f77316dSKiyoshi Ueda 2056c12c9a3cSMike Snitzer blk_set_queue_dying(md->queue); 20573b785fbcSBart Van Assche 2058ab7c7bb6SMikulas Patocka /* 2059ab7c7bb6SMikulas Patocka * Take suspend_lock so that presuspend and postsuspend methods 2060ab7c7bb6SMikulas Patocka * do not race with internal suspend. 2061ab7c7bb6SMikulas Patocka */ 2062ab7c7bb6SMikulas Patocka mutex_lock(&md->suspend_lock); 20632a708cffSJunichi Nomura map = dm_get_live_table(md, &srcu_idx); 20644f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) { 20651da177e4SLinus Torvalds dm_table_presuspend_targets(map); 2066adc0daadSMikulas Patocka set_bit(DMF_SUSPENDED, &md->flags); 20675df96f2bSMikulas Patocka set_bit(DMF_POST_SUSPENDING, &md->flags); 20681da177e4SLinus Torvalds dm_table_postsuspend_targets(map); 20691da177e4SLinus Torvalds } 207083d5e5b0SMikulas Patocka /* dm_put_live_table must be before msleep, otherwise deadlock is possible */ 207183d5e5b0SMikulas Patocka dm_put_live_table(md, srcu_idx); 20722a708cffSJunichi Nomura mutex_unlock(&md->suspend_lock); 207383d5e5b0SMikulas Patocka 20743f77316dSKiyoshi Ueda /* 20753f77316dSKiyoshi Ueda * Rare, but there may be I/O requests still going to complete, 20763f77316dSKiyoshi Ueda * for example. Wait for all references to disappear. 20773f77316dSKiyoshi Ueda * No one should increment the reference count of the mapped_device, 20783f77316dSKiyoshi Ueda * after the mapped_device state becomes DMF_FREEING. 20793f77316dSKiyoshi Ueda */ 20803f77316dSKiyoshi Ueda if (wait) 20813f77316dSKiyoshi Ueda while (atomic_read(&md->holders)) 20823f77316dSKiyoshi Ueda msleep(1); 20833f77316dSKiyoshi Ueda else if (atomic_read(&md->holders)) 20843f77316dSKiyoshi Ueda DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)", 20853f77316dSKiyoshi Ueda dm_device_name(md), atomic_read(&md->holders)); 20863f77316dSKiyoshi Ueda 2087a7940155SAlasdair G Kergon dm_table_destroy(__unbind(md)); 20881da177e4SLinus Torvalds free_dev(md); 20891da177e4SLinus Torvalds } 20903f77316dSKiyoshi Ueda 20913f77316dSKiyoshi Ueda void dm_destroy(struct mapped_device *md) 20923f77316dSKiyoshi Ueda { 20933f77316dSKiyoshi Ueda __dm_destroy(md, true); 20943f77316dSKiyoshi Ueda } 20953f77316dSKiyoshi Ueda 20963f77316dSKiyoshi Ueda void dm_destroy_immediate(struct mapped_device *md) 20973f77316dSKiyoshi Ueda { 20983f77316dSKiyoshi Ueda __dm_destroy(md, false); 20993f77316dSKiyoshi Ueda } 21003f77316dSKiyoshi Ueda 21013f77316dSKiyoshi Ueda void dm_put(struct mapped_device *md) 21023f77316dSKiyoshi Ueda { 21033f77316dSKiyoshi Ueda atomic_dec(&md->holders); 21041da177e4SLinus Torvalds } 210579eb885cSEdward Goggin EXPORT_SYMBOL_GPL(dm_put); 21061da177e4SLinus Torvalds 210785067747SMing Lei static bool md_in_flight_bios(struct mapped_device *md) 210885067747SMing Lei { 210985067747SMing Lei int cpu; 21108446fe92SChristoph Hellwig struct block_device *part = dm_disk(md)->part0; 211185067747SMing Lei long sum = 0; 211285067747SMing Lei 211385067747SMing Lei for_each_possible_cpu(cpu) { 211485067747SMing Lei sum += part_stat_local_read_cpu(part, in_flight[0], cpu); 211585067747SMing Lei sum += part_stat_local_read_cpu(part, in_flight[1], cpu); 211685067747SMing Lei } 211785067747SMing Lei 211885067747SMing Lei return sum != 0; 211985067747SMing Lei } 212085067747SMing Lei 21212f064a59SPeter Zijlstra static int dm_wait_for_bios_completion(struct mapped_device *md, unsigned int task_state) 212246125c1cSMilan Broz { 212346125c1cSMilan Broz int r = 0; 21249f4c3f87SBart Van Assche DEFINE_WAIT(wait); 212546125c1cSMilan Broz 212685067747SMing Lei while (true) { 21279f4c3f87SBart Van Assche prepare_to_wait(&md->wait, &wait, task_state); 212846125c1cSMilan Broz 212985067747SMing Lei if (!md_in_flight_bios(md)) 213046125c1cSMilan Broz break; 213146125c1cSMilan Broz 2132e3fabdfdSBart Van Assche if (signal_pending_state(task_state, current)) { 213346125c1cSMilan Broz r = -EINTR; 213446125c1cSMilan Broz break; 213546125c1cSMilan Broz } 213646125c1cSMilan Broz 213746125c1cSMilan Broz io_schedule(); 213846125c1cSMilan Broz } 21399f4c3f87SBart Van Assche finish_wait(&md->wait, &wait); 2140b44ebeb0SMikulas Patocka 214146125c1cSMilan Broz return r; 214246125c1cSMilan Broz } 214346125c1cSMilan Broz 21442f064a59SPeter Zijlstra static int dm_wait_for_completion(struct mapped_device *md, unsigned int task_state) 214585067747SMing Lei { 214685067747SMing Lei int r = 0; 214785067747SMing Lei 214885067747SMing Lei if (!queue_is_mq(md->queue)) 214985067747SMing Lei return dm_wait_for_bios_completion(md, task_state); 215085067747SMing Lei 215185067747SMing Lei while (true) { 215285067747SMing Lei if (!blk_mq_queue_inflight(md->queue)) 215385067747SMing Lei break; 215485067747SMing Lei 215585067747SMing Lei if (signal_pending_state(task_state, current)) { 215685067747SMing Lei r = -EINTR; 215785067747SMing Lei break; 215885067747SMing Lei } 215985067747SMing Lei 216085067747SMing Lei msleep(5); 216185067747SMing Lei } 216285067747SMing Lei 216385067747SMing Lei return r; 216485067747SMing Lei } 216585067747SMing Lei 21661da177e4SLinus Torvalds /* 21671da177e4SLinus Torvalds * Process the deferred bios 21681da177e4SLinus Torvalds */ 2169ef208587SMikulas Patocka static void dm_wq_work(struct work_struct *work) 21701da177e4SLinus Torvalds { 21710c2915b8SMike Snitzer struct mapped_device *md = container_of(work, struct mapped_device, work); 21720c2915b8SMike Snitzer struct bio *bio; 2173ef208587SMikulas Patocka 21743b00b203SMikulas Patocka while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) { 2175022c2611SMikulas Patocka spin_lock_irq(&md->deferred_lock); 21760c2915b8SMike Snitzer bio = bio_list_pop(&md->deferred); 2177022c2611SMikulas Patocka spin_unlock_irq(&md->deferred_lock); 2178022c2611SMikulas Patocka 21790c2915b8SMike Snitzer if (!bio) 2180df12ee99SAlasdair G Kergon break; 218173d410c0SMilan Broz 21820c2915b8SMike Snitzer submit_bio_noacct(bio); 2183e6ee8c0bSKiyoshi Ueda } 21841da177e4SLinus Torvalds } 21851da177e4SLinus Torvalds 21869a1fb464SMikulas Patocka static void dm_queue_flush(struct mapped_device *md) 2187304f3f6aSMilan Broz { 21883b00b203SMikulas Patocka clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 21894e857c58SPeter Zijlstra smp_mb__after_atomic(); 219053d5914fSMikulas Patocka queue_work(md->wq, &md->work); 2191304f3f6aSMilan Broz } 2192304f3f6aSMilan Broz 21931da177e4SLinus Torvalds /* 2194042d2a9bSAlasdair G Kergon * Swap in a new table, returning the old one for the caller to destroy. 21951da177e4SLinus Torvalds */ 2196042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table) 21971da177e4SLinus Torvalds { 219887eb5b21SMike Christie struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL); 2199754c5fc7SMike Snitzer struct queue_limits limits; 2200042d2a9bSAlasdair G Kergon int r; 22011da177e4SLinus Torvalds 2202e61290a4SDaniel Walker mutex_lock(&md->suspend_lock); 22031da177e4SLinus Torvalds 22041da177e4SLinus Torvalds /* device must be suspended */ 22054f186f8bSKiyoshi Ueda if (!dm_suspended_md(md)) 220693c534aeSAlasdair G Kergon goto out; 22071da177e4SLinus Torvalds 22083ae70656SMike Snitzer /* 22093ae70656SMike Snitzer * If the new table has no data devices, retain the existing limits. 22103ae70656SMike Snitzer * This helps multipath with queue_if_no_path if all paths disappear, 22113ae70656SMike Snitzer * then new I/O is queued based on these limits, and then some paths 22123ae70656SMike Snitzer * reappear. 22133ae70656SMike Snitzer */ 22143ae70656SMike Snitzer if (dm_table_has_no_data_devices(table)) { 221583d5e5b0SMikulas Patocka live_map = dm_get_live_table_fast(md); 22163ae70656SMike Snitzer if (live_map) 22173ae70656SMike Snitzer limits = md->queue->limits; 221883d5e5b0SMikulas Patocka dm_put_live_table_fast(md); 22193ae70656SMike Snitzer } 22203ae70656SMike Snitzer 222187eb5b21SMike Christie if (!live_map) { 2222754c5fc7SMike Snitzer r = dm_calculate_queue_limits(table, &limits); 2223042d2a9bSAlasdair G Kergon if (r) { 2224042d2a9bSAlasdair G Kergon map = ERR_PTR(r); 2225754c5fc7SMike Snitzer goto out; 2226042d2a9bSAlasdair G Kergon } 222787eb5b21SMike Christie } 2228754c5fc7SMike Snitzer 2229042d2a9bSAlasdair G Kergon map = __bind(md, table, &limits); 223062e08243SMikulas Patocka dm_issue_global_event(); 22311da177e4SLinus Torvalds 223293c534aeSAlasdair G Kergon out: 2233e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 2234042d2a9bSAlasdair G Kergon return map; 22351da177e4SLinus Torvalds } 22361da177e4SLinus Torvalds 22371da177e4SLinus Torvalds /* 22381da177e4SLinus Torvalds * Functions to lock and unlock any filesystem running on the 22391da177e4SLinus Torvalds * device. 22401da177e4SLinus Torvalds */ 22412ca3310eSAlasdair G Kergon static int lock_fs(struct mapped_device *md) 22421da177e4SLinus Torvalds { 2243e39e2e95SAlasdair G Kergon int r; 22441da177e4SLinus Torvalds 2245040f04bdSChristoph Hellwig WARN_ON(test_bit(DMF_FROZEN, &md->flags)); 2246dfbe03f6SAlasdair G Kergon 2247977115c0SChristoph Hellwig r = freeze_bdev(md->disk->part0); 2248040f04bdSChristoph Hellwig if (!r) 2249aa8d7c2fSAlasdair G Kergon set_bit(DMF_FROZEN, &md->flags); 2250040f04bdSChristoph Hellwig return r; 22511da177e4SLinus Torvalds } 22521da177e4SLinus Torvalds 22532ca3310eSAlasdair G Kergon static void unlock_fs(struct mapped_device *md) 22541da177e4SLinus Torvalds { 2255aa8d7c2fSAlasdair G Kergon if (!test_bit(DMF_FROZEN, &md->flags)) 2256aa8d7c2fSAlasdair G Kergon return; 2257977115c0SChristoph Hellwig thaw_bdev(md->disk->part0); 2258aa8d7c2fSAlasdair G Kergon clear_bit(DMF_FROZEN, &md->flags); 22591da177e4SLinus Torvalds } 22601da177e4SLinus Torvalds 22611da177e4SLinus Torvalds /* 2262b48633f8SBart Van Assche * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG 2263b48633f8SBart Van Assche * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE 2264b48633f8SBart Van Assche * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY 2265b48633f8SBart Van Assche * 2266ffcc3936SMike Snitzer * If __dm_suspend returns 0, the device is completely quiescent 2267ffcc3936SMike Snitzer * now. There is no request-processing activity. All new requests 2268ffcc3936SMike Snitzer * are being added to md->deferred list. 2269cec47e3dSKiyoshi Ueda */ 2270ffcc3936SMike Snitzer static int __dm_suspend(struct mapped_device *md, struct dm_table *map, 22712f064a59SPeter Zijlstra unsigned suspend_flags, unsigned int task_state, 2272eaf9a736SMike Snitzer int dmf_suspended_flag) 22731da177e4SLinus Torvalds { 2274ffcc3936SMike Snitzer bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG; 2275ffcc3936SMike Snitzer bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG; 2276ffcc3936SMike Snitzer int r; 2277cf222b37SAlasdair G Kergon 22785a8f1f80SBart Van Assche lockdep_assert_held(&md->suspend_lock); 22795a8f1f80SBart Van Assche 22802e93ccc1SKiyoshi Ueda /* 22812e93ccc1SKiyoshi Ueda * DMF_NOFLUSH_SUSPENDING must be set before presuspend. 22822e93ccc1SKiyoshi Ueda * This flag is cleared before dm_suspend returns. 22832e93ccc1SKiyoshi Ueda */ 22842e93ccc1SKiyoshi Ueda if (noflush) 22852e93ccc1SKiyoshi Ueda set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 228686331f39SBart Van Assche else 2287ac75b09fSMike Snitzer DMDEBUG("%s: suspending with flush", dm_device_name(md)); 22882e93ccc1SKiyoshi Ueda 2289d67ee213SMike Snitzer /* 2290d67ee213SMike Snitzer * This gets reverted if there's an error later and the targets 2291d67ee213SMike Snitzer * provide the .presuspend_undo hook. 2292d67ee213SMike Snitzer */ 22931da177e4SLinus Torvalds dm_table_presuspend_targets(map); 22941da177e4SLinus Torvalds 22952e93ccc1SKiyoshi Ueda /* 22969f518b27SKiyoshi Ueda * Flush I/O to the device. 22979f518b27SKiyoshi Ueda * Any I/O submitted after lock_fs() may not be flushed. 22989f518b27SKiyoshi Ueda * noflush takes precedence over do_lockfs. 22999f518b27SKiyoshi Ueda * (lock_fs() flushes I/Os and waits for them to complete.) 23002e93ccc1SKiyoshi Ueda */ 230132a926daSMikulas Patocka if (!noflush && do_lockfs) { 23022ca3310eSAlasdair G Kergon r = lock_fs(md); 2303d67ee213SMike Snitzer if (r) { 2304d67ee213SMike Snitzer dm_table_presuspend_undo_targets(map); 2305ffcc3936SMike Snitzer return r; 2306aa8d7c2fSAlasdair G Kergon } 2307d67ee213SMike Snitzer } 23081da177e4SLinus Torvalds 23091da177e4SLinus Torvalds /* 23103b00b203SMikulas Patocka * Here we must make sure that no processes are submitting requests 23113b00b203SMikulas Patocka * to target drivers i.e. no one may be executing 23120cede372SMike Snitzer * __split_and_process_bio from dm_submit_bio. 23133b00b203SMikulas Patocka * 23140cede372SMike Snitzer * To get all processes out of __split_and_process_bio in dm_submit_bio, 23153b00b203SMikulas Patocka * we take the write lock. To prevent any process from reentering 23160cede372SMike Snitzer * __split_and_process_bio from dm_submit_bio and quiesce the thread 23170cede372SMike Snitzer * (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND and call 23186a8736d1STejun Heo * flush_workqueue(md->wq). 23191da177e4SLinus Torvalds */ 23201eb787ecSAlasdair G Kergon set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 232141abc4e1SHannes Reinecke if (map) 232283d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 23231da177e4SLinus Torvalds 2324d0bcb878SKiyoshi Ueda /* 232529e4013dSTejun Heo * Stop md->queue before flushing md->wq in case request-based 232629e4013dSTejun Heo * dm defers requests to md->wq from md->queue. 2327d0bcb878SKiyoshi Ueda */ 23286a23e05cSJens Axboe if (dm_request_based(md)) 2329eca7ee6dSMike Snitzer dm_stop_queue(md->queue); 2330cec47e3dSKiyoshi Ueda 2331d0bcb878SKiyoshi Ueda flush_workqueue(md->wq); 2332d0bcb878SKiyoshi Ueda 23331da177e4SLinus Torvalds /* 23343b00b203SMikulas Patocka * At this point no more requests are entering target request routines. 23353b00b203SMikulas Patocka * We call dm_wait_for_completion to wait for all existing requests 23363b00b203SMikulas Patocka * to finish. 23371da177e4SLinus Torvalds */ 2338b48633f8SBart Van Assche r = dm_wait_for_completion(md, task_state); 2339eaf9a736SMike Snitzer if (!r) 2340eaf9a736SMike Snitzer set_bit(dmf_suspended_flag, &md->flags); 23411da177e4SLinus Torvalds 23426d6f10dfSMilan Broz if (noflush) 2343022c2611SMikulas Patocka clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags); 234441abc4e1SHannes Reinecke if (map) 234583d5e5b0SMikulas Patocka synchronize_srcu(&md->io_barrier); 23462e93ccc1SKiyoshi Ueda 23471da177e4SLinus Torvalds /* were we interrupted ? */ 234846125c1cSMilan Broz if (r < 0) { 23499a1fb464SMikulas Patocka dm_queue_flush(md); 235073d410c0SMilan Broz 2351cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 2352eca7ee6dSMike Snitzer dm_start_queue(md->queue); 2353cec47e3dSKiyoshi Ueda 23542ca3310eSAlasdair G Kergon unlock_fs(md); 2355d67ee213SMike Snitzer dm_table_presuspend_undo_targets(map); 2356ffcc3936SMike Snitzer /* pushback list is already flushed, so skip flush */ 2357ffcc3936SMike Snitzer } 2358ffcc3936SMike Snitzer 2359ffcc3936SMike Snitzer return r; 23602ca3310eSAlasdair G Kergon } 23612ca3310eSAlasdair G Kergon 23623b00b203SMikulas Patocka /* 2363ffcc3936SMike Snitzer * We need to be able to change a mapping table under a mounted 2364ffcc3936SMike Snitzer * filesystem. For example we might want to move some data in 2365ffcc3936SMike Snitzer * the background. Before the table can be swapped with 2366ffcc3936SMike Snitzer * dm_bind_table, dm_suspend must be called to flush any in 2367ffcc3936SMike Snitzer * flight bios and ensure that any further io gets deferred. 23683b00b203SMikulas Patocka */ 2369ffcc3936SMike Snitzer /* 2370ffcc3936SMike Snitzer * Suspend mechanism in request-based dm. 2371ffcc3936SMike Snitzer * 2372ffcc3936SMike Snitzer * 1. Flush all I/Os by lock_fs() if needed. 2373ffcc3936SMike Snitzer * 2. Stop dispatching any I/O by stopping the request_queue. 2374ffcc3936SMike Snitzer * 3. Wait for all in-flight I/Os to be completed or requeued. 2375ffcc3936SMike Snitzer * 2376ffcc3936SMike Snitzer * To abort suspend, start the request_queue. 2377ffcc3936SMike Snitzer */ 2378ffcc3936SMike Snitzer int dm_suspend(struct mapped_device *md, unsigned suspend_flags) 2379ffcc3936SMike Snitzer { 2380ffcc3936SMike Snitzer struct dm_table *map = NULL; 2381ffcc3936SMike Snitzer int r = 0; 2382ffcc3936SMike Snitzer 2383ffcc3936SMike Snitzer retry: 2384ffcc3936SMike Snitzer mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); 2385ffcc3936SMike Snitzer 2386ffcc3936SMike Snitzer if (dm_suspended_md(md)) { 2387ffcc3936SMike Snitzer r = -EINVAL; 2388ffcc3936SMike Snitzer goto out_unlock; 2389ffcc3936SMike Snitzer } 2390ffcc3936SMike Snitzer 2391ffcc3936SMike Snitzer if (dm_suspended_internally_md(md)) { 2392ffcc3936SMike Snitzer /* already internally suspended, wait for internal resume */ 2393ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2394ffcc3936SMike Snitzer r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); 2395ffcc3936SMike Snitzer if (r) 2396ffcc3936SMike Snitzer return r; 2397ffcc3936SMike Snitzer goto retry; 2398ffcc3936SMike Snitzer } 2399ffcc3936SMike Snitzer 2400a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2401ffcc3936SMike Snitzer 2402eaf9a736SMike Snitzer r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED); 2403ffcc3936SMike Snitzer if (r) 2404ffcc3936SMike Snitzer goto out_unlock; 24053b00b203SMikulas Patocka 24065df96f2bSMikulas Patocka set_bit(DMF_POST_SUSPENDING, &md->flags); 24074d4471cbSKiyoshi Ueda dm_table_postsuspend_targets(map); 24085df96f2bSMikulas Patocka clear_bit(DMF_POST_SUSPENDING, &md->flags); 24094d4471cbSKiyoshi Ueda 2410d287483dSAlasdair G Kergon out_unlock: 2411e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 2412cf222b37SAlasdair G Kergon return r; 24131da177e4SLinus Torvalds } 24141da177e4SLinus Torvalds 2415ffcc3936SMike Snitzer static int __dm_resume(struct mapped_device *md, struct dm_table *map) 24161da177e4SLinus Torvalds { 2417ffcc3936SMike Snitzer if (map) { 2418ffcc3936SMike Snitzer int r = dm_table_resume_targets(map); 24198757b776SMilan Broz if (r) 2420ffcc3936SMike Snitzer return r; 2421ffcc3936SMike Snitzer } 24222ca3310eSAlasdair G Kergon 24239a1fb464SMikulas Patocka dm_queue_flush(md); 24242ca3310eSAlasdair G Kergon 2425cec47e3dSKiyoshi Ueda /* 2426cec47e3dSKiyoshi Ueda * Flushing deferred I/Os must be done after targets are resumed 2427cec47e3dSKiyoshi Ueda * so that mapping of targets can work correctly. 2428cec47e3dSKiyoshi Ueda * Request-based dm is queueing the deferred I/Os in its request_queue. 2429cec47e3dSKiyoshi Ueda */ 2430cec47e3dSKiyoshi Ueda if (dm_request_based(md)) 2431eca7ee6dSMike Snitzer dm_start_queue(md->queue); 2432cec47e3dSKiyoshi Ueda 24332ca3310eSAlasdair G Kergon unlock_fs(md); 24342ca3310eSAlasdair G Kergon 2435ffcc3936SMike Snitzer return 0; 2436ffcc3936SMike Snitzer } 2437ffcc3936SMike Snitzer 2438ffcc3936SMike Snitzer int dm_resume(struct mapped_device *md) 2439ffcc3936SMike Snitzer { 24408dc23658SMinfei Huang int r; 2441ffcc3936SMike Snitzer struct dm_table *map = NULL; 2442ffcc3936SMike Snitzer 2443ffcc3936SMike Snitzer retry: 24448dc23658SMinfei Huang r = -EINVAL; 2445ffcc3936SMike Snitzer mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING); 2446ffcc3936SMike Snitzer 2447ffcc3936SMike Snitzer if (!dm_suspended_md(md)) 2448ffcc3936SMike Snitzer goto out; 2449ffcc3936SMike Snitzer 2450ffcc3936SMike Snitzer if (dm_suspended_internally_md(md)) { 2451ffcc3936SMike Snitzer /* already internally suspended, wait for internal resume */ 2452ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2453ffcc3936SMike Snitzer r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE); 2454ffcc3936SMike Snitzer if (r) 2455ffcc3936SMike Snitzer return r; 2456ffcc3936SMike Snitzer goto retry; 2457ffcc3936SMike Snitzer } 2458ffcc3936SMike Snitzer 2459a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2460ffcc3936SMike Snitzer if (!map || !dm_table_get_size(map)) 2461ffcc3936SMike Snitzer goto out; 2462ffcc3936SMike Snitzer 2463ffcc3936SMike Snitzer r = __dm_resume(md, map); 2464ffcc3936SMike Snitzer if (r) 2465ffcc3936SMike Snitzer goto out; 2466ffcc3936SMike Snitzer 24672ca3310eSAlasdair G Kergon clear_bit(DMF_SUSPENDED, &md->flags); 2468cf222b37SAlasdair G Kergon out: 2469e61290a4SDaniel Walker mutex_unlock(&md->suspend_lock); 24702ca3310eSAlasdair G Kergon 2471cf222b37SAlasdair G Kergon return r; 24721da177e4SLinus Torvalds } 24731da177e4SLinus Torvalds 2474fd2ed4d2SMikulas Patocka /* 2475fd2ed4d2SMikulas Patocka * Internal suspend/resume works like userspace-driven suspend. It waits 2476fd2ed4d2SMikulas Patocka * until all bios finish and prevents issuing new bios to the target drivers. 2477fd2ed4d2SMikulas Patocka * It may be used only from the kernel. 2478fd2ed4d2SMikulas Patocka */ 2479fd2ed4d2SMikulas Patocka 2480ffcc3936SMike Snitzer static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags) 2481ffcc3936SMike Snitzer { 2482ffcc3936SMike Snitzer struct dm_table *map = NULL; 2483ffcc3936SMike Snitzer 24841ea0654eSBart Van Assche lockdep_assert_held(&md->suspend_lock); 24851ea0654eSBart Van Assche 248696b26c8cSMikulas Patocka if (md->internal_suspend_count++) 2487ffcc3936SMike Snitzer return; /* nested internal suspend */ 2488ffcc3936SMike Snitzer 2489ffcc3936SMike Snitzer if (dm_suspended_md(md)) { 2490ffcc3936SMike Snitzer set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2491ffcc3936SMike Snitzer return; /* nest suspend */ 2492ffcc3936SMike Snitzer } 2493ffcc3936SMike Snitzer 2494a12f5d48SEric Dumazet map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock)); 2495ffcc3936SMike Snitzer 2496ffcc3936SMike Snitzer /* 2497ffcc3936SMike Snitzer * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is 2498ffcc3936SMike Snitzer * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend 2499ffcc3936SMike Snitzer * would require changing .presuspend to return an error -- avoid this 2500ffcc3936SMike Snitzer * until there is a need for more elaborate variants of internal suspend. 2501ffcc3936SMike Snitzer */ 2502eaf9a736SMike Snitzer (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE, 2503eaf9a736SMike Snitzer DMF_SUSPENDED_INTERNALLY); 2504ffcc3936SMike Snitzer 25055df96f2bSMikulas Patocka set_bit(DMF_POST_SUSPENDING, &md->flags); 2506ffcc3936SMike Snitzer dm_table_postsuspend_targets(map); 25075df96f2bSMikulas Patocka clear_bit(DMF_POST_SUSPENDING, &md->flags); 2508ffcc3936SMike Snitzer } 2509ffcc3936SMike Snitzer 2510ffcc3936SMike Snitzer static void __dm_internal_resume(struct mapped_device *md) 2511ffcc3936SMike Snitzer { 251296b26c8cSMikulas Patocka BUG_ON(!md->internal_suspend_count); 251396b26c8cSMikulas Patocka 251496b26c8cSMikulas Patocka if (--md->internal_suspend_count) 2515ffcc3936SMike Snitzer return; /* resume from nested internal suspend */ 2516ffcc3936SMike Snitzer 2517ffcc3936SMike Snitzer if (dm_suspended_md(md)) 2518ffcc3936SMike Snitzer goto done; /* resume from nested suspend */ 2519ffcc3936SMike Snitzer 2520ffcc3936SMike Snitzer /* 2521ffcc3936SMike Snitzer * NOTE: existing callers don't need to call dm_table_resume_targets 2522ffcc3936SMike Snitzer * (which may fail -- so best to avoid it for now by passing NULL map) 2523ffcc3936SMike Snitzer */ 2524ffcc3936SMike Snitzer (void) __dm_resume(md, NULL); 2525ffcc3936SMike Snitzer 2526ffcc3936SMike Snitzer done: 2527ffcc3936SMike Snitzer clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2528ffcc3936SMike Snitzer smp_mb__after_atomic(); 2529ffcc3936SMike Snitzer wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY); 2530ffcc3936SMike Snitzer } 2531ffcc3936SMike Snitzer 2532ffcc3936SMike Snitzer void dm_internal_suspend_noflush(struct mapped_device *md) 2533fd2ed4d2SMikulas Patocka { 2534fd2ed4d2SMikulas Patocka mutex_lock(&md->suspend_lock); 2535ffcc3936SMike Snitzer __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG); 2536ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2537ffcc3936SMike Snitzer } 2538ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush); 2539ffcc3936SMike Snitzer 2540ffcc3936SMike Snitzer void dm_internal_resume(struct mapped_device *md) 2541ffcc3936SMike Snitzer { 2542ffcc3936SMike Snitzer mutex_lock(&md->suspend_lock); 2543ffcc3936SMike Snitzer __dm_internal_resume(md); 2544ffcc3936SMike Snitzer mutex_unlock(&md->suspend_lock); 2545ffcc3936SMike Snitzer } 2546ffcc3936SMike Snitzer EXPORT_SYMBOL_GPL(dm_internal_resume); 2547ffcc3936SMike Snitzer 2548ffcc3936SMike Snitzer /* 2549ffcc3936SMike Snitzer * Fast variants of internal suspend/resume hold md->suspend_lock, 2550ffcc3936SMike Snitzer * which prevents interaction with userspace-driven suspend. 2551ffcc3936SMike Snitzer */ 2552ffcc3936SMike Snitzer 2553ffcc3936SMike Snitzer void dm_internal_suspend_fast(struct mapped_device *md) 2554ffcc3936SMike Snitzer { 2555ffcc3936SMike Snitzer mutex_lock(&md->suspend_lock); 2556ffcc3936SMike Snitzer if (dm_suspended_md(md) || dm_suspended_internally_md(md)) 2557fd2ed4d2SMikulas Patocka return; 2558fd2ed4d2SMikulas Patocka 2559fd2ed4d2SMikulas Patocka set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags); 2560fd2ed4d2SMikulas Patocka synchronize_srcu(&md->io_barrier); 2561fd2ed4d2SMikulas Patocka flush_workqueue(md->wq); 2562fd2ed4d2SMikulas Patocka dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE); 2563fd2ed4d2SMikulas Patocka } 2564b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_suspend_fast); 2565fd2ed4d2SMikulas Patocka 2566ffcc3936SMike Snitzer void dm_internal_resume_fast(struct mapped_device *md) 2567fd2ed4d2SMikulas Patocka { 2568ffcc3936SMike Snitzer if (dm_suspended_md(md) || dm_suspended_internally_md(md)) 2569fd2ed4d2SMikulas Patocka goto done; 2570fd2ed4d2SMikulas Patocka 2571fd2ed4d2SMikulas Patocka dm_queue_flush(md); 2572fd2ed4d2SMikulas Patocka 2573fd2ed4d2SMikulas Patocka done: 2574fd2ed4d2SMikulas Patocka mutex_unlock(&md->suspend_lock); 2575fd2ed4d2SMikulas Patocka } 2576b735fedeSMikulas Patocka EXPORT_SYMBOL_GPL(dm_internal_resume_fast); 2577fd2ed4d2SMikulas Patocka 25781da177e4SLinus Torvalds /*----------------------------------------------------------------- 25791da177e4SLinus Torvalds * Event notification. 25801da177e4SLinus Torvalds *---------------------------------------------------------------*/ 25813abf85b5SPeter Rajnoha int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action, 258260935eb2SMilan Broz unsigned cookie) 258369267a30SAlasdair G Kergon { 25846958c1c6SMikulas Patocka int r; 25856958c1c6SMikulas Patocka unsigned noio_flag; 258660935eb2SMilan Broz char udev_cookie[DM_COOKIE_LENGTH]; 258760935eb2SMilan Broz char *envp[] = { udev_cookie, NULL }; 258860935eb2SMilan Broz 25896958c1c6SMikulas Patocka noio_flag = memalloc_noio_save(); 25906958c1c6SMikulas Patocka 259160935eb2SMilan Broz if (!cookie) 25926958c1c6SMikulas Patocka r = kobject_uevent(&disk_to_dev(md->disk)->kobj, action); 259360935eb2SMilan Broz else { 259460935eb2SMilan Broz snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u", 259560935eb2SMilan Broz DM_COOKIE_ENV_VAR_NAME, cookie); 25966958c1c6SMikulas Patocka r = kobject_uevent_env(&disk_to_dev(md->disk)->kobj, 25973abf85b5SPeter Rajnoha action, envp); 259860935eb2SMilan Broz } 25996958c1c6SMikulas Patocka 26006958c1c6SMikulas Patocka memalloc_noio_restore(noio_flag); 26016958c1c6SMikulas Patocka 26026958c1c6SMikulas Patocka return r; 260369267a30SAlasdair G Kergon } 260469267a30SAlasdair G Kergon 26057a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md) 26067a8c3d3bSMike Anderson { 26077a8c3d3bSMike Anderson return atomic_add_return(1, &md->uevent_seq); 26087a8c3d3bSMike Anderson } 26097a8c3d3bSMike Anderson 26101da177e4SLinus Torvalds uint32_t dm_get_event_nr(struct mapped_device *md) 26111da177e4SLinus Torvalds { 26121da177e4SLinus Torvalds return atomic_read(&md->event_nr); 26131da177e4SLinus Torvalds } 26141da177e4SLinus Torvalds 26151da177e4SLinus Torvalds int dm_wait_event(struct mapped_device *md, int event_nr) 26161da177e4SLinus Torvalds { 26171da177e4SLinus Torvalds return wait_event_interruptible(md->eventq, 26181da177e4SLinus Torvalds (event_nr != atomic_read(&md->event_nr))); 26191da177e4SLinus Torvalds } 26201da177e4SLinus Torvalds 26217a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist) 26227a8c3d3bSMike Anderson { 26237a8c3d3bSMike Anderson unsigned long flags; 26247a8c3d3bSMike Anderson 26257a8c3d3bSMike Anderson spin_lock_irqsave(&md->uevent_lock, flags); 26267a8c3d3bSMike Anderson list_add(elist, &md->uevent_list); 26277a8c3d3bSMike Anderson spin_unlock_irqrestore(&md->uevent_lock, flags); 26287a8c3d3bSMike Anderson } 26297a8c3d3bSMike Anderson 26301da177e4SLinus Torvalds /* 26311da177e4SLinus Torvalds * The gendisk is only valid as long as you have a reference 26321da177e4SLinus Torvalds * count on 'md'. 26331da177e4SLinus Torvalds */ 26341da177e4SLinus Torvalds struct gendisk *dm_disk(struct mapped_device *md) 26351da177e4SLinus Torvalds { 26361da177e4SLinus Torvalds return md->disk; 26371da177e4SLinus Torvalds } 263865ff5b7dSSami Tolvanen EXPORT_SYMBOL_GPL(dm_disk); 26391da177e4SLinus Torvalds 2640784aae73SMilan Broz struct kobject *dm_kobject(struct mapped_device *md) 2641784aae73SMilan Broz { 26422995fa78SMikulas Patocka return &md->kobj_holder.kobj; 2643784aae73SMilan Broz } 2644784aae73SMilan Broz 2645784aae73SMilan Broz struct mapped_device *dm_get_from_kobject(struct kobject *kobj) 2646784aae73SMilan Broz { 2647784aae73SMilan Broz struct mapped_device *md; 2648784aae73SMilan Broz 26492995fa78SMikulas Patocka md = container_of(kobj, struct mapped_device, kobj_holder.kobj); 2650784aae73SMilan Broz 2651b9a41d21SHou Tao spin_lock(&_minor_lock); 2652b9a41d21SHou Tao if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { 2653b9a41d21SHou Tao md = NULL; 2654b9a41d21SHou Tao goto out; 2655b9a41d21SHou Tao } 2656784aae73SMilan Broz dm_get(md); 2657b9a41d21SHou Tao out: 2658b9a41d21SHou Tao spin_unlock(&_minor_lock); 2659b9a41d21SHou Tao 2660784aae73SMilan Broz return md; 2661784aae73SMilan Broz } 2662784aae73SMilan Broz 26634f186f8bSKiyoshi Ueda int dm_suspended_md(struct mapped_device *md) 26641da177e4SLinus Torvalds { 26651da177e4SLinus Torvalds return test_bit(DMF_SUSPENDED, &md->flags); 26661da177e4SLinus Torvalds } 26671da177e4SLinus Torvalds 26685df96f2bSMikulas Patocka static int dm_post_suspending_md(struct mapped_device *md) 26695df96f2bSMikulas Patocka { 26705df96f2bSMikulas Patocka return test_bit(DMF_POST_SUSPENDING, &md->flags); 26715df96f2bSMikulas Patocka } 26725df96f2bSMikulas Patocka 2673ffcc3936SMike Snitzer int dm_suspended_internally_md(struct mapped_device *md) 2674ffcc3936SMike Snitzer { 2675ffcc3936SMike Snitzer return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags); 2676ffcc3936SMike Snitzer } 2677ffcc3936SMike Snitzer 26782c140a24SMikulas Patocka int dm_test_deferred_remove_flag(struct mapped_device *md) 26792c140a24SMikulas Patocka { 26802c140a24SMikulas Patocka return test_bit(DMF_DEFERRED_REMOVE, &md->flags); 26812c140a24SMikulas Patocka } 26822c140a24SMikulas Patocka 268364dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti) 268464dbce58SKiyoshi Ueda { 268533bd6f06SMike Snitzer return dm_suspended_md(ti->table->md); 268664dbce58SKiyoshi Ueda } 268764dbce58SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_suspended); 268864dbce58SKiyoshi Ueda 26895df96f2bSMikulas Patocka int dm_post_suspending(struct dm_target *ti) 26905df96f2bSMikulas Patocka { 269133bd6f06SMike Snitzer return dm_post_suspending_md(ti->table->md); 26925df96f2bSMikulas Patocka } 26935df96f2bSMikulas Patocka EXPORT_SYMBOL_GPL(dm_post_suspending); 26945df96f2bSMikulas Patocka 26952e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti) 26962e93ccc1SKiyoshi Ueda { 269733bd6f06SMike Snitzer return __noflush_suspending(ti->table->md); 26982e93ccc1SKiyoshi Ueda } 26992e93ccc1SKiyoshi Ueda EXPORT_SYMBOL_GPL(dm_noflush_suspending); 27002e93ccc1SKiyoshi Ueda 27017e0d574fSBart Van Assche struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type, 27020776aa0eSMike Snitzer unsigned integrity, unsigned per_io_data_size, 27030776aa0eSMike Snitzer unsigned min_pool_size) 2704e6ee8c0bSKiyoshi Ueda { 2705115485e8SMike Snitzer struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id); 270678d8e58aSMike Snitzer unsigned int pool_size = 0; 270764f52b0eSMike Snitzer unsigned int front_pad, io_front_pad; 27086f1c819cSKent Overstreet int ret; 2709e6ee8c0bSKiyoshi Ueda 2710e6ee8c0bSKiyoshi Ueda if (!pools) 27114e6e36c3SMike Snitzer return NULL; 2712e6ee8c0bSKiyoshi Ueda 271378d8e58aSMike Snitzer switch (type) { 271478d8e58aSMike Snitzer case DM_TYPE_BIO_BASED: 2715545ed20eSToshi Kani case DM_TYPE_DAX_BIO_BASED: 27160776aa0eSMike Snitzer pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size); 271762f26317SJeffle Xu front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + DM_TARGET_IO_BIO_OFFSET; 271862f26317SJeffle Xu io_front_pad = roundup(per_io_data_size, __alignof__(struct dm_io)) + DM_IO_BIO_OFFSET; 27196f1c819cSKent Overstreet ret = bioset_init(&pools->io_bs, pool_size, io_front_pad, 0); 27206f1c819cSKent Overstreet if (ret) 272164f52b0eSMike Snitzer goto out; 27226f1c819cSKent Overstreet if (integrity && bioset_integrity_create(&pools->io_bs, pool_size)) 2723eb8db831SChristoph Hellwig goto out; 272478d8e58aSMike Snitzer break; 272578d8e58aSMike Snitzer case DM_TYPE_REQUEST_BASED: 27260776aa0eSMike Snitzer pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size); 272778d8e58aSMike Snitzer front_pad = offsetof(struct dm_rq_clone_bio_info, clone); 2728591ddcfcSMike Snitzer /* per_io_data_size is used for blk-mq pdu at queue allocation */ 272978d8e58aSMike Snitzer break; 273078d8e58aSMike Snitzer default: 273178d8e58aSMike Snitzer BUG(); 273278d8e58aSMike Snitzer } 273378d8e58aSMike Snitzer 27346f1c819cSKent Overstreet ret = bioset_init(&pools->bs, pool_size, front_pad, 0); 27356f1c819cSKent Overstreet if (ret) 27365f015204SJun'ichi Nomura goto out; 2737e6ee8c0bSKiyoshi Ueda 27386f1c819cSKent Overstreet if (integrity && bioset_integrity_create(&pools->bs, pool_size)) 27395f015204SJun'ichi Nomura goto out; 2740a91a2785SMartin K. Petersen 2741e6ee8c0bSKiyoshi Ueda return pools; 274278d8e58aSMike Snitzer 27435f015204SJun'ichi Nomura out: 27445f015204SJun'ichi Nomura dm_free_md_mempools(pools); 2745e6ee8c0bSKiyoshi Ueda 27464e6e36c3SMike Snitzer return NULL; 2747e6ee8c0bSKiyoshi Ueda } 2748e6ee8c0bSKiyoshi Ueda 2749e6ee8c0bSKiyoshi Ueda void dm_free_md_mempools(struct dm_md_mempools *pools) 2750e6ee8c0bSKiyoshi Ueda { 2751e6ee8c0bSKiyoshi Ueda if (!pools) 2752e6ee8c0bSKiyoshi Ueda return; 2753e6ee8c0bSKiyoshi Ueda 27546f1c819cSKent Overstreet bioset_exit(&pools->bs); 27556f1c819cSKent Overstreet bioset_exit(&pools->io_bs); 2756e6ee8c0bSKiyoshi Ueda 2757e6ee8c0bSKiyoshi Ueda kfree(pools); 2758e6ee8c0bSKiyoshi Ueda } 2759e6ee8c0bSKiyoshi Ueda 27609c72bad1SChristoph Hellwig struct dm_pr { 27619c72bad1SChristoph Hellwig u64 old_key; 27629c72bad1SChristoph Hellwig u64 new_key; 27639c72bad1SChristoph Hellwig u32 flags; 27649c72bad1SChristoph Hellwig bool fail_early; 27659c72bad1SChristoph Hellwig }; 27669c72bad1SChristoph Hellwig 27679c72bad1SChristoph Hellwig static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn, 27689c72bad1SChristoph Hellwig void *data) 27699c72bad1SChristoph Hellwig { 27709c72bad1SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 27719c72bad1SChristoph Hellwig struct dm_table *table; 27729c72bad1SChristoph Hellwig struct dm_target *ti; 27739c72bad1SChristoph Hellwig int ret = -ENOTTY, srcu_idx; 27749c72bad1SChristoph Hellwig 27759c72bad1SChristoph Hellwig table = dm_get_live_table(md, &srcu_idx); 27769c72bad1SChristoph Hellwig if (!table || !dm_table_get_size(table)) 27779c72bad1SChristoph Hellwig goto out; 27789c72bad1SChristoph Hellwig 27799c72bad1SChristoph Hellwig /* We only support devices that have a single target */ 27809c72bad1SChristoph Hellwig if (dm_table_get_num_targets(table) != 1) 27819c72bad1SChristoph Hellwig goto out; 27829c72bad1SChristoph Hellwig ti = dm_table_get_target(table, 0); 27839c72bad1SChristoph Hellwig 27849c72bad1SChristoph Hellwig ret = -EINVAL; 27859c72bad1SChristoph Hellwig if (!ti->type->iterate_devices) 27869c72bad1SChristoph Hellwig goto out; 27879c72bad1SChristoph Hellwig 27889c72bad1SChristoph Hellwig ret = ti->type->iterate_devices(ti, fn, data); 27899c72bad1SChristoph Hellwig out: 27909c72bad1SChristoph Hellwig dm_put_live_table(md, srcu_idx); 27919c72bad1SChristoph Hellwig return ret; 27929c72bad1SChristoph Hellwig } 27939c72bad1SChristoph Hellwig 27949c72bad1SChristoph Hellwig /* 27959c72bad1SChristoph Hellwig * For register / unregister we need to manually call out to every path. 27969c72bad1SChristoph Hellwig */ 27979c72bad1SChristoph Hellwig static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev, 27989c72bad1SChristoph Hellwig sector_t start, sector_t len, void *data) 27999c72bad1SChristoph Hellwig { 28009c72bad1SChristoph Hellwig struct dm_pr *pr = data; 28019c72bad1SChristoph Hellwig const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops; 28029c72bad1SChristoph Hellwig 28039c72bad1SChristoph Hellwig if (!ops || !ops->pr_register) 28049c72bad1SChristoph Hellwig return -EOPNOTSUPP; 28059c72bad1SChristoph Hellwig return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags); 28069c72bad1SChristoph Hellwig } 28079c72bad1SChristoph Hellwig 280871cdb697SChristoph Hellwig static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key, 280971cdb697SChristoph Hellwig u32 flags) 281071cdb697SChristoph Hellwig { 28119c72bad1SChristoph Hellwig struct dm_pr pr = { 28129c72bad1SChristoph Hellwig .old_key = old_key, 28139c72bad1SChristoph Hellwig .new_key = new_key, 28149c72bad1SChristoph Hellwig .flags = flags, 28159c72bad1SChristoph Hellwig .fail_early = true, 28169c72bad1SChristoph Hellwig }; 28179c72bad1SChristoph Hellwig int ret; 281871cdb697SChristoph Hellwig 28199c72bad1SChristoph Hellwig ret = dm_call_pr(bdev, __dm_pr_register, &pr); 28209c72bad1SChristoph Hellwig if (ret && new_key) { 28219c72bad1SChristoph Hellwig /* unregister all paths if we failed to register any path */ 28229c72bad1SChristoph Hellwig pr.old_key = new_key; 28239c72bad1SChristoph Hellwig pr.new_key = 0; 28249c72bad1SChristoph Hellwig pr.flags = 0; 28259c72bad1SChristoph Hellwig pr.fail_early = false; 28269c72bad1SChristoph Hellwig dm_call_pr(bdev, __dm_pr_register, &pr); 28279c72bad1SChristoph Hellwig } 282871cdb697SChristoph Hellwig 28299c72bad1SChristoph Hellwig return ret; 283071cdb697SChristoph Hellwig } 283171cdb697SChristoph Hellwig 283271cdb697SChristoph Hellwig static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type, 283371cdb697SChristoph Hellwig u32 flags) 283471cdb697SChristoph Hellwig { 283571cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 283671cdb697SChristoph Hellwig const struct pr_ops *ops; 2837971888c4SMike Snitzer int r, srcu_idx; 283871cdb697SChristoph Hellwig 28395bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 284071cdb697SChristoph Hellwig if (r < 0) 2841971888c4SMike Snitzer goto out; 284271cdb697SChristoph Hellwig 284371cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 284471cdb697SChristoph Hellwig if (ops && ops->pr_reserve) 284571cdb697SChristoph Hellwig r = ops->pr_reserve(bdev, key, type, flags); 284671cdb697SChristoph Hellwig else 284771cdb697SChristoph Hellwig r = -EOPNOTSUPP; 2848971888c4SMike Snitzer out: 2849971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 285071cdb697SChristoph Hellwig return r; 285171cdb697SChristoph Hellwig } 285271cdb697SChristoph Hellwig 285371cdb697SChristoph Hellwig static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type) 285471cdb697SChristoph Hellwig { 285571cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 285671cdb697SChristoph Hellwig const struct pr_ops *ops; 2857971888c4SMike Snitzer int r, srcu_idx; 285871cdb697SChristoph Hellwig 28595bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 286071cdb697SChristoph Hellwig if (r < 0) 2861971888c4SMike Snitzer goto out; 286271cdb697SChristoph Hellwig 286371cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 286471cdb697SChristoph Hellwig if (ops && ops->pr_release) 286571cdb697SChristoph Hellwig r = ops->pr_release(bdev, key, type); 286671cdb697SChristoph Hellwig else 286771cdb697SChristoph Hellwig r = -EOPNOTSUPP; 2868971888c4SMike Snitzer out: 2869971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 287071cdb697SChristoph Hellwig return r; 287171cdb697SChristoph Hellwig } 287271cdb697SChristoph Hellwig 287371cdb697SChristoph Hellwig static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key, 287471cdb697SChristoph Hellwig enum pr_type type, bool abort) 287571cdb697SChristoph Hellwig { 287671cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 287771cdb697SChristoph Hellwig const struct pr_ops *ops; 2878971888c4SMike Snitzer int r, srcu_idx; 287971cdb697SChristoph Hellwig 28805bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 288171cdb697SChristoph Hellwig if (r < 0) 2882971888c4SMike Snitzer goto out; 288371cdb697SChristoph Hellwig 288471cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 288571cdb697SChristoph Hellwig if (ops && ops->pr_preempt) 288671cdb697SChristoph Hellwig r = ops->pr_preempt(bdev, old_key, new_key, type, abort); 288771cdb697SChristoph Hellwig else 288871cdb697SChristoph Hellwig r = -EOPNOTSUPP; 2889971888c4SMike Snitzer out: 2890971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 289171cdb697SChristoph Hellwig return r; 289271cdb697SChristoph Hellwig } 289371cdb697SChristoph Hellwig 289471cdb697SChristoph Hellwig static int dm_pr_clear(struct block_device *bdev, u64 key) 289571cdb697SChristoph Hellwig { 289671cdb697SChristoph Hellwig struct mapped_device *md = bdev->bd_disk->private_data; 289771cdb697SChristoph Hellwig const struct pr_ops *ops; 2898971888c4SMike Snitzer int r, srcu_idx; 289971cdb697SChristoph Hellwig 29005bd5e8d8SMike Snitzer r = dm_prepare_ioctl(md, &srcu_idx, &bdev); 290171cdb697SChristoph Hellwig if (r < 0) 2902971888c4SMike Snitzer goto out; 290371cdb697SChristoph Hellwig 290471cdb697SChristoph Hellwig ops = bdev->bd_disk->fops->pr_ops; 290571cdb697SChristoph Hellwig if (ops && ops->pr_clear) 290671cdb697SChristoph Hellwig r = ops->pr_clear(bdev, key); 290771cdb697SChristoph Hellwig else 290871cdb697SChristoph Hellwig r = -EOPNOTSUPP; 2909971888c4SMike Snitzer out: 2910971888c4SMike Snitzer dm_unprepare_ioctl(md, srcu_idx); 291171cdb697SChristoph Hellwig return r; 291271cdb697SChristoph Hellwig } 291371cdb697SChristoph Hellwig 291471cdb697SChristoph Hellwig static const struct pr_ops dm_pr_ops = { 291571cdb697SChristoph Hellwig .pr_register = dm_pr_register, 291671cdb697SChristoph Hellwig .pr_reserve = dm_pr_reserve, 291771cdb697SChristoph Hellwig .pr_release = dm_pr_release, 291871cdb697SChristoph Hellwig .pr_preempt = dm_pr_preempt, 291971cdb697SChristoph Hellwig .pr_clear = dm_pr_clear, 292071cdb697SChristoph Hellwig }; 292171cdb697SChristoph Hellwig 292283d5cde4SAlexey Dobriyan static const struct block_device_operations dm_blk_dops = { 2923c62b37d9SChristoph Hellwig .submit_bio = dm_submit_bio, 29241da177e4SLinus Torvalds .open = dm_blk_open, 29251da177e4SLinus Torvalds .release = dm_blk_close, 2926aa129a22SMilan Broz .ioctl = dm_blk_ioctl, 29273ac51e74SDarrick J. Wong .getgeo = dm_blk_getgeo, 2928e76239a3SChristoph Hellwig .report_zones = dm_blk_report_zones, 292971cdb697SChristoph Hellwig .pr_ops = &dm_pr_ops, 29301da177e4SLinus Torvalds .owner = THIS_MODULE 29311da177e4SLinus Torvalds }; 29321da177e4SLinus Torvalds 2933681cc5e8SMike Snitzer static const struct block_device_operations dm_rq_blk_dops = { 2934681cc5e8SMike Snitzer .open = dm_blk_open, 2935681cc5e8SMike Snitzer .release = dm_blk_close, 2936681cc5e8SMike Snitzer .ioctl = dm_blk_ioctl, 2937681cc5e8SMike Snitzer .getgeo = dm_blk_getgeo, 2938681cc5e8SMike Snitzer .pr_ops = &dm_pr_ops, 2939681cc5e8SMike Snitzer .owner = THIS_MODULE 2940681cc5e8SMike Snitzer }; 2941681cc5e8SMike Snitzer 2942f26c5719SDan Williams static const struct dax_operations dm_dax_ops = { 2943f26c5719SDan Williams .direct_access = dm_dax_direct_access, 2944cdf6cdcdSVivek Goyal .zero_page_range = dm_dax_zero_page_range, 2945f26c5719SDan Williams }; 2946f26c5719SDan Williams 29471da177e4SLinus Torvalds /* 29481da177e4SLinus Torvalds * module hooks 29491da177e4SLinus Torvalds */ 29501da177e4SLinus Torvalds module_init(dm_init); 29511da177e4SLinus Torvalds module_exit(dm_exit); 29521da177e4SLinus Torvalds 29531da177e4SLinus Torvalds module_param(major, uint, 0); 29541da177e4SLinus Torvalds MODULE_PARM_DESC(major, "The major number of the device mapper"); 2955f4790826SMike Snitzer 2956e8603136SMike Snitzer module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR); 2957e8603136SMike Snitzer MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools"); 2958e8603136SMike Snitzer 2959115485e8SMike Snitzer module_param(dm_numa_node, int, S_IRUGO | S_IWUSR); 2960115485e8SMike Snitzer MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations"); 2961115485e8SMike Snitzer 2962a666e5c0SMikulas Patocka module_param(swap_bios, int, S_IRUGO | S_IWUSR); 2963a666e5c0SMikulas Patocka MODULE_PARM_DESC(swap_bios, "Maximum allowed inflight swap IOs"); 2964a666e5c0SMikulas Patocka 29651da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " driver"); 29661da177e4SLinus Torvalds MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); 29671da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 2968