11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Copyright (C) 2001 Sistina Software (UK) Limited. 30da336e5SAlasdair G Kergon * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This file is released under the LGPL. 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds #ifndef _LINUX_DEVICE_MAPPER_H 91da177e4SLinus Torvalds #define _LINUX_DEVICE_MAPPER_H 101da177e4SLinus Torvalds 11416cd17bSHeinz Mauelshagen #include <linux/bio.h> 12f6fccb12SMilan Broz #include <linux/blkdev.h> 136bbc923dSHelen Koike #include <linux/dm-ioctl.h> 14fd2ed4d2SMikulas Patocka #include <linux/math64.h> 1571a16736SNamhyung Kim #include <linux/ratelimit.h> 16416cd17bSHeinz Mauelshagen 17af4874e0SMike Snitzer struct dm_dev; 181da177e4SLinus Torvalds struct dm_target; 191da177e4SLinus Torvalds struct dm_table; 20d4100351SChristoph Hellwig struct dm_report_zones_args; 2117b2f66fSAlasdair G Kergon struct mapped_device; 22f6fccb12SMilan Broz struct bio_vec; 23e511c4a3SJane Chu enum dax_access_mode; 241da177e4SLinus Torvalds 25e83068a5SMike Snitzer /* 26e83068a5SMike Snitzer * Type of table, mapped_device's mempool and request_queue 27e83068a5SMike Snitzer */ 287e0d574fSBart Van Assche enum dm_queue_mode { 297e0d574fSBart Van Assche DM_TYPE_NONE = 0, 307e0d574fSBart Van Assche DM_TYPE_BIO_BASED = 1, 317e0d574fSBart Van Assche DM_TYPE_REQUEST_BASED = 2, 32953923c0SMike Snitzer DM_TYPE_DAX_BIO_BASED = 3, 337e0d574fSBart Van Assche }; 34e83068a5SMike Snitzer 3591ccbbacSTushar Sugandhi typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE, STATUSTYPE_IMA } status_type_t; 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds union map_info { 381da177e4SLinus Torvalds void *ptr; 391da177e4SLinus Torvalds }; 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds /* 421da177e4SLinus Torvalds * In the constructor the target parameter will already have the 431da177e4SLinus Torvalds * table, type, begin and len fields filled in. 441da177e4SLinus Torvalds */ 451da177e4SLinus Torvalds typedef int (*dm_ctr_fn) (struct dm_target *target, 461da177e4SLinus Torvalds unsigned int argc, char **argv); 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds /* 491da177e4SLinus Torvalds * The destructor doesn't need to free the dm_target, just 501da177e4SLinus Torvalds * anything hidden ti->private. 511da177e4SLinus Torvalds */ 521da177e4SLinus Torvalds typedef void (*dm_dtr_fn) (struct dm_target *ti); 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds /* 551da177e4SLinus Torvalds * The map function must return: 561da177e4SLinus Torvalds * < 0: error 571da177e4SLinus Torvalds * = 0: The target will handle the io by resubmitting it later 5845cbcd79SKiyoshi Ueda * = 1: simple remap complete 592e93ccc1SKiyoshi Ueda * = 2: The target wants to push back the io 601da177e4SLinus Torvalds */ 617de3ee57SMikulas Patocka typedef int (*dm_map_fn) (struct dm_target *ti, struct bio *bio); 62e5863d9aSMike Snitzer typedef int (*dm_clone_and_map_request_fn) (struct dm_target *ti, 63e5863d9aSMike Snitzer struct request *rq, 64e5863d9aSMike Snitzer union map_info *map_context, 65e5863d9aSMike Snitzer struct request **clone); 665de719e3SYufen Yu typedef void (*dm_release_clone_request_fn) (struct request *clone, 675de719e3SYufen Yu union map_info *map_context); 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds /* 701da177e4SLinus Torvalds * Returns: 711da177e4SLinus Torvalds * < 0 : error (currently ignored) 721da177e4SLinus Torvalds * 0 : ended successfully 731da177e4SLinus Torvalds * 1 : for some reason the io has still not completed (eg, 741da177e4SLinus Torvalds * multipath target might want to requeue a failed io). 752e93ccc1SKiyoshi Ueda * 2 : The target wants to push back the io 761da177e4SLinus Torvalds */ 771da177e4SLinus Torvalds typedef int (*dm_endio_fn) (struct dm_target *ti, 784e4cbee9SChristoph Hellwig struct bio *bio, blk_status_t *error); 797d76345dSKiyoshi Ueda typedef int (*dm_request_endio_fn) (struct dm_target *ti, 802a842acaSChristoph Hellwig struct request *clone, blk_status_t error, 817d76345dSKiyoshi Ueda union map_info *map_context); 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds typedef void (*dm_presuspend_fn) (struct dm_target *ti); 84d67ee213SMike Snitzer typedef void (*dm_presuspend_undo_fn) (struct dm_target *ti); 851da177e4SLinus Torvalds typedef void (*dm_postsuspend_fn) (struct dm_target *ti); 868757b776SMilan Broz typedef int (*dm_preresume_fn) (struct dm_target *ti); 871da177e4SLinus Torvalds typedef void (*dm_resume_fn) (struct dm_target *ti); 881da177e4SLinus Torvalds 89fd7c092eSMikulas Patocka typedef void (*dm_status_fn) (struct dm_target *ti, status_type_t status_type, 901f4e0ff0SAlasdair G Kergon unsigned status_flags, char *result, unsigned maxlen); 911da177e4SLinus Torvalds 921eb5fa84SMike Snitzer typedef int (*dm_message_fn) (struct dm_target *ti, unsigned argc, char **argv, 931eb5fa84SMike Snitzer char *result, unsigned maxlen); 941da177e4SLinus Torvalds 955bd5e8d8SMike Snitzer typedef int (*dm_prepare_ioctl_fn) (struct dm_target *ti, struct block_device **bdev); 96aa129a22SMilan Broz 97e3290b94SMike Snitzer #ifdef CONFIG_BLK_DEV_ZONED 98d4100351SChristoph Hellwig typedef int (*dm_report_zones_fn) (struct dm_target *ti, 99d4100351SChristoph Hellwig struct dm_report_zones_args *args, 100d4100351SChristoph Hellwig unsigned int nr_zones); 101e3290b94SMike Snitzer #else 102e3290b94SMike Snitzer /* 103e3290b94SMike Snitzer * Define dm_report_zones_fn so that targets can assign to NULL if 104e3290b94SMike Snitzer * CONFIG_BLK_DEV_ZONED disabled. Otherwise each target needs to do 105e3290b94SMike Snitzer * awkward #ifdefs in their target_type, etc. 106e3290b94SMike Snitzer */ 107e3290b94SMike Snitzer typedef int (*dm_report_zones_fn) (struct dm_target *dummy); 108e3290b94SMike Snitzer #endif 109e76239a3SChristoph Hellwig 110058ce5caSAlasdair G Kergon /* 111058ce5caSAlasdair G Kergon * These iteration functions are typically used to check (and combine) 112058ce5caSAlasdair G Kergon * properties of underlying devices. 113058ce5caSAlasdair G Kergon * E.g. Does at least one underlying device support flush? 114058ce5caSAlasdair G Kergon * Does any underlying device not support WRITE_SAME? 115058ce5caSAlasdair G Kergon * 116058ce5caSAlasdair G Kergon * The callout function is called once for each contiguous section of 117058ce5caSAlasdair G Kergon * an underlying device. State can be maintained in *data. 118058ce5caSAlasdair G Kergon * Return non-zero to stop iterating through any further devices. 119058ce5caSAlasdair G Kergon */ 120af4874e0SMike Snitzer typedef int (*iterate_devices_callout_fn) (struct dm_target *ti, 121af4874e0SMike Snitzer struct dm_dev *dev, 1225dea271bSMike Snitzer sector_t start, sector_t len, 123af4874e0SMike Snitzer void *data); 124af4874e0SMike Snitzer 125058ce5caSAlasdair G Kergon /* 126058ce5caSAlasdair G Kergon * This function must iterate through each section of device used by the 127058ce5caSAlasdair G Kergon * target until it encounters a non-zero return code, which it then returns. 128058ce5caSAlasdair G Kergon * Returns zero if no callout returned non-zero. 129058ce5caSAlasdair G Kergon */ 130af4874e0SMike Snitzer typedef int (*dm_iterate_devices_fn) (struct dm_target *ti, 131af4874e0SMike Snitzer iterate_devices_callout_fn fn, 132af4874e0SMike Snitzer void *data); 133af4874e0SMike Snitzer 13440bea431SMike Snitzer typedef void (*dm_io_hints_fn) (struct dm_target *ti, 13540bea431SMike Snitzer struct queue_limits *limits); 13640bea431SMike Snitzer 1377d76345dSKiyoshi Ueda /* 1387d76345dSKiyoshi Ueda * Returns: 1397d76345dSKiyoshi Ueda * 0: The target can handle the next I/O immediately. 1407d76345dSKiyoshi Ueda * 1: The target can't handle the next I/O immediately. 1417d76345dSKiyoshi Ueda */ 1427d76345dSKiyoshi Ueda typedef int (*dm_busy_fn) (struct dm_target *ti); 1437d76345dSKiyoshi Ueda 144545ed20eSToshi Kani /* 145545ed20eSToshi Kani * Returns: 146545ed20eSToshi Kani * < 0 : error 147545ed20eSToshi Kani * >= 0 : the number of bytes accessible at the address 148545ed20eSToshi Kani */ 149817bf402SDan Williams typedef long (*dm_dax_direct_access_fn) (struct dm_target *ti, pgoff_t pgoff, 150e511c4a3SJane Chu long nr_pages, enum dax_access_mode node, void **kaddr, 151e511c4a3SJane Chu pfn_t *pfn); 152cdf6cdcdSVivek Goyal typedef int (*dm_dax_zero_page_range_fn)(struct dm_target *ti, pgoff_t pgoff, 153cdf6cdcdSVivek Goyal size_t nr_pages); 154545ed20eSToshi Kani 155047218ecSJane Chu /* 156047218ecSJane Chu * Returns: 157047218ecSJane Chu * != 0 : number of bytes transferred 158047218ecSJane Chu * 0 : recovery write failed 159047218ecSJane Chu */ 160047218ecSJane Chu typedef size_t (*dm_dax_recovery_write_fn)(struct dm_target *ti, pgoff_t pgoff, 161047218ecSJane Chu void *addr, size_t bytes, struct iov_iter *i); 162047218ecSJane Chu 1631da177e4SLinus Torvalds void dm_error(const char *message); 1641da177e4SLinus Torvalds 16582b1519bSMikulas Patocka struct dm_dev { 16682b1519bSMikulas Patocka struct block_device *bdev; 167817bf402SDan Williams struct dax_device *dax_dev; 168aeb5d727SAl Viro fmode_t mode; 16982b1519bSMikulas Patocka char name[16]; 17082b1519bSMikulas Patocka }; 17182b1519bSMikulas Patocka 1724df2bf46SDingXiang dev_t dm_get_dev_t(const char *path); 1734df2bf46SDingXiang 1743cb40214SBryn Reeves /* 1751da177e4SLinus Torvalds * Constructors should call these functions to ensure destination devices 1761da177e4SLinus Torvalds * are opened/closed correctly. 1771da177e4SLinus Torvalds */ 1788215d6ecSNikanth Karthikesan int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode, 1798215d6ecSNikanth Karthikesan struct dm_dev **result); 1801da177e4SLinus Torvalds void dm_put_device(struct dm_target *ti, struct dm_dev *d); 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds /* 1831da177e4SLinus Torvalds * Information about a target type 1841da177e4SLinus Torvalds */ 185ab4c1424SAndi Kleen 1861da177e4SLinus Torvalds struct target_type { 187ab4c1424SAndi Kleen uint64_t features; 1881da177e4SLinus Torvalds const char *name; 1891da177e4SLinus Torvalds struct module *module; 1901da177e4SLinus Torvalds unsigned version[3]; 1911da177e4SLinus Torvalds dm_ctr_fn ctr; 1921da177e4SLinus Torvalds dm_dtr_fn dtr; 1931da177e4SLinus Torvalds dm_map_fn map; 194e5863d9aSMike Snitzer dm_clone_and_map_request_fn clone_and_map_rq; 195e5863d9aSMike Snitzer dm_release_clone_request_fn release_clone_rq; 1961da177e4SLinus Torvalds dm_endio_fn end_io; 1977d76345dSKiyoshi Ueda dm_request_endio_fn rq_end_io; 1981da177e4SLinus Torvalds dm_presuspend_fn presuspend; 199d67ee213SMike Snitzer dm_presuspend_undo_fn presuspend_undo; 2001da177e4SLinus Torvalds dm_postsuspend_fn postsuspend; 2018757b776SMilan Broz dm_preresume_fn preresume; 2021da177e4SLinus Torvalds dm_resume_fn resume; 2031da177e4SLinus Torvalds dm_status_fn status; 2041da177e4SLinus Torvalds dm_message_fn message; 205e56f81e0SChristoph Hellwig dm_prepare_ioctl_fn prepare_ioctl; 206e76239a3SChristoph Hellwig dm_report_zones_fn report_zones; 2077d76345dSKiyoshi Ueda dm_busy_fn busy; 208af4874e0SMike Snitzer dm_iterate_devices_fn iterate_devices; 20940bea431SMike Snitzer dm_io_hints_fn io_hints; 210817bf402SDan Williams dm_dax_direct_access_fn direct_access; 211cdf6cdcdSVivek Goyal dm_dax_zero_page_range_fn dax_zero_page_range; 212047218ecSJane Chu dm_dax_recovery_write_fn dax_recovery_write; 21345194e4fSCheng Renquan 21445194e4fSCheng Renquan /* For internal device-mapper use. */ 21545194e4fSCheng Renquan struct list_head list; 2161da177e4SLinus Torvalds }; 2171da177e4SLinus Torvalds 2183791e2fcSAlasdair G Kergon /* 2193791e2fcSAlasdair G Kergon * Target features 2203791e2fcSAlasdair G Kergon */ 2213791e2fcSAlasdair G Kergon 2223791e2fcSAlasdair G Kergon /* 2233791e2fcSAlasdair G Kergon * Any table that contains an instance of this target must have only one. 2243791e2fcSAlasdair G Kergon */ 2253791e2fcSAlasdair G Kergon #define DM_TARGET_SINGLETON 0x00000001 2263791e2fcSAlasdair G Kergon #define dm_target_needs_singleton(type) ((type)->features & DM_TARGET_SINGLETON) 2273791e2fcSAlasdair G Kergon 228cc6cbe14SAlasdair G Kergon /* 229cc6cbe14SAlasdair G Kergon * Indicates that a target does not support read-only devices. 230cc6cbe14SAlasdair G Kergon */ 231cc6cbe14SAlasdair G Kergon #define DM_TARGET_ALWAYS_WRITEABLE 0x00000002 232cc6cbe14SAlasdair G Kergon #define dm_target_always_writeable(type) \ 233cc6cbe14SAlasdair G Kergon ((type)->features & DM_TARGET_ALWAYS_WRITEABLE) 234cc6cbe14SAlasdair G Kergon 23536a0456fSAlasdair G Kergon /* 23636a0456fSAlasdair G Kergon * Any device that contains a table with an instance of this target may never 23736a0456fSAlasdair G Kergon * have tables containing any different target type. 23836a0456fSAlasdair G Kergon */ 23936a0456fSAlasdair G Kergon #define DM_TARGET_IMMUTABLE 0x00000004 24036a0456fSAlasdair G Kergon #define dm_target_is_immutable(type) ((type)->features & DM_TARGET_IMMUTABLE) 24136a0456fSAlasdair G Kergon 242b0d8ed4dSAlasdair G Kergon /* 243f083b09bSMike Snitzer * Indicates that a target may replace any target; even immutable targets. 244f083b09bSMike Snitzer * .map, .map_rq, .clone_and_map_rq and .release_clone_rq are all defined. 245f083b09bSMike Snitzer */ 246f083b09bSMike Snitzer #define DM_TARGET_WILDCARD 0x00000008 247f083b09bSMike Snitzer #define dm_target_is_wildcard(type) ((type)->features & DM_TARGET_WILDCARD) 248f083b09bSMike Snitzer 249f083b09bSMike Snitzer /* 2509b4b5a79SMilan Broz * A target implements own bio data integrity. 2519b4b5a79SMilan Broz */ 2529b4b5a79SMilan Broz #define DM_TARGET_INTEGRITY 0x00000010 2539b4b5a79SMilan Broz #define dm_target_has_integrity(type) ((type)->features & DM_TARGET_INTEGRITY) 2549b4b5a79SMilan Broz 255e2460f2aSMikulas Patocka /* 256e2460f2aSMikulas Patocka * A target passes integrity data to the lower device. 257e2460f2aSMikulas Patocka */ 258e2460f2aSMikulas Patocka #define DM_TARGET_PASSES_INTEGRITY 0x00000020 259e2460f2aSMikulas Patocka #define dm_target_passes_integrity(type) ((type)->features & DM_TARGET_PASSES_INTEGRITY) 260e2460f2aSMikulas Patocka 261dd88d313SDamien Le Moal /* 2622d669cebSShin'ichiro Kawasaki * Indicates support for zoned block devices: 2632d669cebSShin'ichiro Kawasaki * - DM_TARGET_ZONED_HM: the target also supports host-managed zoned 2642d669cebSShin'ichiro Kawasaki * block devices but does not support combining different zoned models. 2652d669cebSShin'ichiro Kawasaki * - DM_TARGET_MIXED_ZONED_MODEL: the target supports combining multiple 2662d669cebSShin'ichiro Kawasaki * devices with different zoned models. 267dd88d313SDamien Le Moal */ 268e3290b94SMike Snitzer #ifdef CONFIG_BLK_DEV_ZONED 269dd88d313SDamien Le Moal #define DM_TARGET_ZONED_HM 0x00000040 270dd88d313SDamien Le Moal #define dm_target_supports_zoned_hm(type) ((type)->features & DM_TARGET_ZONED_HM) 271e3290b94SMike Snitzer #else 272e3290b94SMike Snitzer #define DM_TARGET_ZONED_HM 0x00000000 273e3290b94SMike Snitzer #define dm_target_supports_zoned_hm(type) (false) 274e3290b94SMike Snitzer #endif 275dd88d313SDamien Le Moal 2766abc4946SKonstantin Khlebnikov /* 2776abc4946SKonstantin Khlebnikov * A target handles REQ_NOWAIT 2786abc4946SKonstantin Khlebnikov */ 2796abc4946SKonstantin Khlebnikov #define DM_TARGET_NOWAIT 0x00000080 2806abc4946SKonstantin Khlebnikov #define dm_target_supports_nowait(type) ((type)->features & DM_TARGET_NOWAIT) 2816abc4946SKonstantin Khlebnikov 282aa6ce87aSSatya Tangirala /* 283aa6ce87aSSatya Tangirala * A target supports passing through inline crypto support. 284aa6ce87aSSatya Tangirala */ 285aa6ce87aSSatya Tangirala #define DM_TARGET_PASSES_CRYPTO 0x00000100 286aa6ce87aSSatya Tangirala #define dm_target_passes_crypto(type) ((type)->features & DM_TARGET_PASSES_CRYPTO) 287aa6ce87aSSatya Tangirala 2882d669cebSShin'ichiro Kawasaki #ifdef CONFIG_BLK_DEV_ZONED 2892d669cebSShin'ichiro Kawasaki #define DM_TARGET_MIXED_ZONED_MODEL 0x00000200 2902d669cebSShin'ichiro Kawasaki #define dm_target_supports_mixed_zoned_model(type) \ 2912d669cebSShin'ichiro Kawasaki ((type)->features & DM_TARGET_MIXED_ZONED_MODEL) 2922d669cebSShin'ichiro Kawasaki #else 2932d669cebSShin'ichiro Kawasaki #define DM_TARGET_MIXED_ZONED_MODEL 0x00000000 2942d669cebSShin'ichiro Kawasaki #define dm_target_supports_mixed_zoned_model(type) (false) 2952d669cebSShin'ichiro Kawasaki #endif 2962d669cebSShin'ichiro Kawasaki 2971da177e4SLinus Torvalds struct dm_target { 2981da177e4SLinus Torvalds struct dm_table *table; 2991da177e4SLinus Torvalds struct target_type *type; 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds /* target limits */ 3021da177e4SLinus Torvalds sector_t begin; 3031da177e4SLinus Torvalds sector_t len; 3041da177e4SLinus Torvalds 305542f9038SMike Snitzer /* If non-zero, maximum size of I/O submitted to a target. */ 306542f9038SMike Snitzer uint32_t max_io_len; 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds /* 30955a62eefSAlasdair G Kergon * A number of zero-length barrier bios that will be submitted 310f9ab94ceSMikulas Patocka * to the target for the purpose of flushing cache. 311f9ab94ceSMikulas Patocka * 31255a62eefSAlasdair G Kergon * The bio number can be accessed with dm_bio_get_target_bio_nr. 31355a62eefSAlasdair G Kergon * It is a responsibility of the target driver to remap these bios 314f9ab94ceSMikulas Patocka * to the real underlying devices. 315f9ab94ceSMikulas Patocka */ 31655a62eefSAlasdair G Kergon unsigned num_flush_bios; 317f9ab94ceSMikulas Patocka 3185ae89a87SMike Snitzer /* 31955a62eefSAlasdair G Kergon * The number of discard bios that will be submitted to the target. 32055a62eefSAlasdair G Kergon * The bio number can be accessed with dm_bio_get_target_bio_nr. 3215ae89a87SMike Snitzer */ 32255a62eefSAlasdair G Kergon unsigned num_discard_bios; 3235ae89a87SMike Snitzer 324d54eaa5aSMike Snitzer /* 32500716545SDenis Semakin * The number of secure erase bios that will be submitted to the target. 32600716545SDenis Semakin * The bio number can be accessed with dm_bio_get_target_bio_nr. 32700716545SDenis Semakin */ 32800716545SDenis Semakin unsigned num_secure_erase_bios; 32900716545SDenis Semakin 33000716545SDenis Semakin /* 331ac62d620SChristoph Hellwig * The number of WRITE ZEROES bios that will be submitted to the target. 332ac62d620SChristoph Hellwig * The bio number can be accessed with dm_bio_get_target_bio_nr. 333ac62d620SChristoph Hellwig */ 334ac62d620SChristoph Hellwig unsigned num_write_zeroes_bios; 335ac62d620SChristoph Hellwig 336ac62d620SChristoph Hellwig /* 33730187e1dSMike Snitzer * The minimum number of extra bytes allocated in each io for the 33830187e1dSMike Snitzer * target to use. 339c0820cf5SMikulas Patocka */ 34030187e1dSMike Snitzer unsigned per_io_data_size; 341c0820cf5SMikulas Patocka 3421da177e4SLinus Torvalds /* target specific data */ 3431da177e4SLinus Torvalds void *private; 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds /* Used to provide an error string from the ctr */ 3461da177e4SLinus Torvalds char *error; 3474c259327SMike Snitzer 3484c259327SMike Snitzer /* 3490e9c24edSJoe Thornber * Set if this target needs to receive flushes regardless of 3500e9c24edSJoe Thornber * whether or not its underlying devices have support. 3510e9c24edSJoe Thornber */ 3520e9c24edSJoe Thornber bool flush_supported:1; 3530e9c24edSJoe Thornber 3540e9c24edSJoe Thornber /* 3554c259327SMike Snitzer * Set if this target needs to receive discards regardless of 3564c259327SMike Snitzer * whether or not its underlying devices have support. 3574c259327SMike Snitzer */ 3580ac55489SAlasdair G Kergon bool discards_supported:1; 359a666e5c0SMikulas Patocka 360a666e5c0SMikulas Patocka /* 361a666e5c0SMikulas Patocka * Set if we need to limit the number of in-flight bios when swapping. 362a666e5c0SMikulas Patocka */ 363a666e5c0SMikulas Patocka bool limit_swap_bios:1; 364bb37d772SDamien Le Moal 365bb37d772SDamien Le Moal /* 366a8b9d116STom Rix * Set if this target implements a zoned device and needs emulation of 367bb37d772SDamien Le Moal * zone append operations using regular writes. 368bb37d772SDamien Le Moal */ 369bb37d772SDamien Le Moal bool emulate_zone_append:1; 3700fbb4d93SMike Snitzer 3710fbb4d93SMike Snitzer /* 3720fbb4d93SMike Snitzer * Set if the target will submit IO using dm_submit_bio_remap() 3730fbb4d93SMike Snitzer * after returning DM_MAPIO_SUBMITTED from its map function. 3740fbb4d93SMike Snitzer */ 3750fbb4d93SMike Snitzer bool accounts_remapped_io:1; 376*9dd1cd32SMike Snitzer 377*9dd1cd32SMike Snitzer /* 378*9dd1cd32SMike Snitzer * Set if the target will submit the DM bio without first calling 379*9dd1cd32SMike Snitzer * bio_set_dev(). NOTE: ideally a target should _not_ need this. 380*9dd1cd32SMike Snitzer */ 381*9dd1cd32SMike Snitzer bool needs_bio_set_dev:1; 3821da177e4SLinus Torvalds }; 3831da177e4SLinus Torvalds 38464f52b0eSMike Snitzer void *dm_per_bio_data(struct bio *bio, size_t data_size); 38564f52b0eSMike Snitzer struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size); 38664f52b0eSMike Snitzer unsigned dm_bio_get_target_bio_nr(const struct bio *bio); 387ddbd658fSMikulas Patocka 388087615bfSGabriel Krisman Bertazi u64 dm_start_time_ns_from_clone(struct bio *bio); 389087615bfSGabriel Krisman Bertazi 3901da177e4SLinus Torvalds int dm_register_target(struct target_type *t); 39110d3bd09SMikulas Patocka void dm_unregister_target(struct target_type *t); 39217b2f66fSAlasdair G Kergon 393498f0103SMike Snitzer /* 394498f0103SMike Snitzer * Target argument parsing. 395498f0103SMike Snitzer */ 396498f0103SMike Snitzer struct dm_arg_set { 397498f0103SMike Snitzer unsigned argc; 398498f0103SMike Snitzer char **argv; 399498f0103SMike Snitzer }; 400498f0103SMike Snitzer 401498f0103SMike Snitzer /* 402498f0103SMike Snitzer * The minimum and maximum value of a numeric argument, together with 403498f0103SMike Snitzer * the error message to use if the number is found to be outside that range. 404498f0103SMike Snitzer */ 405498f0103SMike Snitzer struct dm_arg { 406498f0103SMike Snitzer unsigned min; 407498f0103SMike Snitzer unsigned max; 408498f0103SMike Snitzer char *error; 409498f0103SMike Snitzer }; 410498f0103SMike Snitzer 411498f0103SMike Snitzer /* 412498f0103SMike Snitzer * Validate the next argument, either returning it as *value or, if invalid, 413498f0103SMike Snitzer * returning -EINVAL and setting *error. 414498f0103SMike Snitzer */ 4155916a22bSEric Biggers int dm_read_arg(const struct dm_arg *arg, struct dm_arg_set *arg_set, 416498f0103SMike Snitzer unsigned *value, char **error); 417498f0103SMike Snitzer 418498f0103SMike Snitzer /* 419498f0103SMike Snitzer * Process the next argument as the start of a group containing between 420498f0103SMike Snitzer * arg->min and arg->max further arguments. Either return the size as 421498f0103SMike Snitzer * *num_args or, if invalid, return -EINVAL and set *error. 422498f0103SMike Snitzer */ 4235916a22bSEric Biggers int dm_read_arg_group(const struct dm_arg *arg, struct dm_arg_set *arg_set, 424498f0103SMike Snitzer unsigned *num_args, char **error); 425498f0103SMike Snitzer 426498f0103SMike Snitzer /* 427498f0103SMike Snitzer * Return the current argument and shift to the next. 428498f0103SMike Snitzer */ 429498f0103SMike Snitzer const char *dm_shift_arg(struct dm_arg_set *as); 430498f0103SMike Snitzer 431498f0103SMike Snitzer /* 432498f0103SMike Snitzer * Move through num_args arguments. 433498f0103SMike Snitzer */ 434498f0103SMike Snitzer void dm_consume_args(struct dm_arg_set *as, unsigned num_args); 435498f0103SMike Snitzer 43617b2f66fSAlasdair G Kergon /*----------------------------------------------------------------- 43717b2f66fSAlasdair G Kergon * Functions for creating and manipulating mapped devices. 43817b2f66fSAlasdair G Kergon * Drop the reference with dm_put when you finish with the object. 43917b2f66fSAlasdair G Kergon *---------------------------------------------------------------*/ 44017b2f66fSAlasdair G Kergon 44117b2f66fSAlasdair G Kergon /* 44217b2f66fSAlasdair G Kergon * DM_ANY_MINOR chooses the next available minor number. 44317b2f66fSAlasdair G Kergon */ 44417b2f66fSAlasdair G Kergon #define DM_ANY_MINOR (-1) 44517b2f66fSAlasdair G Kergon int dm_create(int minor, struct mapped_device **md); 44617b2f66fSAlasdair G Kergon 44717b2f66fSAlasdair G Kergon /* 44817b2f66fSAlasdair G Kergon * Reference counting for md. 44917b2f66fSAlasdair G Kergon */ 45017b2f66fSAlasdair G Kergon struct mapped_device *dm_get_md(dev_t dev); 45117b2f66fSAlasdair G Kergon void dm_get(struct mapped_device *md); 45209ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md); 45317b2f66fSAlasdair G Kergon void dm_put(struct mapped_device *md); 45417b2f66fSAlasdair G Kergon 45517b2f66fSAlasdair G Kergon /* 45617b2f66fSAlasdair G Kergon * An arbitrary pointer may be stored alongside a mapped device. 45717b2f66fSAlasdair G Kergon */ 45817b2f66fSAlasdair G Kergon void dm_set_mdptr(struct mapped_device *md, void *ptr); 45917b2f66fSAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md); 46017b2f66fSAlasdair G Kergon 46117b2f66fSAlasdair G Kergon /* 46217b2f66fSAlasdair G Kergon * A device can still be used while suspended, but I/O is deferred. 46317b2f66fSAlasdair G Kergon */ 464a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags); 46517b2f66fSAlasdair G Kergon int dm_resume(struct mapped_device *md); 46617b2f66fSAlasdair G Kergon 46717b2f66fSAlasdair G Kergon /* 46817b2f66fSAlasdair G Kergon * Event functions. 46917b2f66fSAlasdair G Kergon */ 47017b2f66fSAlasdair G Kergon uint32_t dm_get_event_nr(struct mapped_device *md); 47117b2f66fSAlasdair G Kergon int dm_wait_event(struct mapped_device *md, int event_nr); 4727a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md); 4737a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist); 47417b2f66fSAlasdair G Kergon 47517b2f66fSAlasdair G Kergon /* 47617b2f66fSAlasdair G Kergon * Info functions. 47717b2f66fSAlasdair G Kergon */ 47872d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md); 47996a1f7dbSMike Anderson int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid); 48017b2f66fSAlasdair G Kergon struct gendisk *dm_disk(struct mapped_device *md); 48164dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti); 4825df96f2bSMikulas Patocka int dm_post_suspending(struct dm_target *ti); 4832e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti); 4841dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors); 485b7f8dff0SMike Snitzer void dm_submit_bio_remap(struct bio *clone, struct bio *tgt_clone); 486cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq); 48717b2f66fSAlasdair G Kergon 488d4100351SChristoph Hellwig #ifdef CONFIG_BLK_DEV_ZONED 489d4100351SChristoph Hellwig struct dm_report_zones_args { 490d4100351SChristoph Hellwig struct dm_target *tgt; 491d4100351SChristoph Hellwig sector_t next_sector; 492d4100351SChristoph Hellwig 493d4100351SChristoph Hellwig void *orig_data; 494d4100351SChristoph Hellwig report_zones_cb orig_cb; 495d4100351SChristoph Hellwig unsigned int zone_idx; 496d4100351SChristoph Hellwig 497d4100351SChristoph Hellwig /* must be filled by ->report_zones before calling dm_report_zones_cb */ 498d4100351SChristoph Hellwig sector_t start; 499d4100351SChristoph Hellwig }; 500912e8875SDamien Le Moal int dm_report_zones(struct block_device *bdev, sector_t start, sector_t sector, 501912e8875SDamien Le Moal struct dm_report_zones_args *args, unsigned int nr_zones); 502d4100351SChristoph Hellwig #endif /* CONFIG_BLK_DEV_ZONED */ 503d4100351SChristoph Hellwig 5046bbc923dSHelen Koike /* 5056bbc923dSHelen Koike * Device mapper functions to parse and create devices specified by the 5066bbc923dSHelen Koike * parameter "dm-mod.create=" 5076bbc923dSHelen Koike */ 5086bbc923dSHelen Koike int __init dm_early_create(struct dm_ioctl *dmi, 5096bbc923dSHelen Koike struct dm_target_spec **spec_array, 5106bbc923dSHelen Koike char **target_params_array); 5116bbc923dSHelen Koike 512f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md); 513f84cb8a4SMike Snitzer 51417b2f66fSAlasdair G Kergon /* 51517b2f66fSAlasdair G Kergon * Geometry functions. 51617b2f66fSAlasdair G Kergon */ 51717b2f66fSAlasdair G Kergon int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo); 51817b2f66fSAlasdair G Kergon int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo); 51917b2f66fSAlasdair G Kergon 52017b2f66fSAlasdair G Kergon /*----------------------------------------------------------------- 52117b2f66fSAlasdair G Kergon * Functions for manipulating device-mapper tables. 52217b2f66fSAlasdair G Kergon *---------------------------------------------------------------*/ 52317b2f66fSAlasdair G Kergon 52417b2f66fSAlasdair G Kergon /* 52517b2f66fSAlasdair G Kergon * First create an empty table. 52617b2f66fSAlasdair G Kergon */ 527aeb5d727SAl Viro int dm_table_create(struct dm_table **result, fmode_t mode, 52817b2f66fSAlasdair G Kergon unsigned num_targets, struct mapped_device *md); 52917b2f66fSAlasdair G Kergon 53017b2f66fSAlasdair G Kergon /* 53117b2f66fSAlasdair G Kergon * Then call this once for each target. 53217b2f66fSAlasdair G Kergon */ 53317b2f66fSAlasdair G Kergon int dm_table_add_target(struct dm_table *t, const char *type, 53417b2f66fSAlasdair G Kergon sector_t start, sector_t len, char *params); 53517b2f66fSAlasdair G Kergon 53617b2f66fSAlasdair G Kergon /* 537e83068a5SMike Snitzer * Target can use this to set the table's type. 538e83068a5SMike Snitzer * Can only ever be called from a target's ctr. 539e83068a5SMike Snitzer * Useful for "hybrid" target (supports both bio-based 540e83068a5SMike Snitzer * and request-based). 541e83068a5SMike Snitzer */ 5427e0d574fSBart Van Assche void dm_table_set_type(struct dm_table *t, enum dm_queue_mode type); 543e83068a5SMike Snitzer 544e83068a5SMike Snitzer /* 54517b2f66fSAlasdair G Kergon * Finally call this to make the table ready for use. 54617b2f66fSAlasdair G Kergon */ 54717b2f66fSAlasdair G Kergon int dm_table_complete(struct dm_table *t); 54817b2f66fSAlasdair G Kergon 54917b2f66fSAlasdair G Kergon /* 550f6e7baadSBrian Norris * Destroy the table when finished. 551f6e7baadSBrian Norris */ 552f6e7baadSBrian Norris void dm_table_destroy(struct dm_table *t); 553f6e7baadSBrian Norris 554f6e7baadSBrian Norris /* 555542f9038SMike Snitzer * Target may require that it is never sent I/O larger than len. 556542f9038SMike Snitzer */ 557542f9038SMike Snitzer int __must_check dm_set_target_max_io_len(struct dm_target *ti, sector_t len); 558542f9038SMike Snitzer 559542f9038SMike Snitzer /* 56017b2f66fSAlasdair G Kergon * Table reference counting. 56117b2f66fSAlasdair G Kergon */ 56283d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx); 56383d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx); 56483d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md); 56517b2f66fSAlasdair G Kergon 56617b2f66fSAlasdair G Kergon /* 56717b2f66fSAlasdair G Kergon * Queries 56817b2f66fSAlasdair G Kergon */ 56917b2f66fSAlasdair G Kergon sector_t dm_table_get_size(struct dm_table *t); 570aeb5d727SAl Viro fmode_t dm_table_get_mode(struct dm_table *t); 57117b2f66fSAlasdair G Kergon struct mapped_device *dm_table_get_md(struct dm_table *t); 572f349b0a3SMichał Mirosław const char *dm_table_device_name(struct dm_table *t); 57317b2f66fSAlasdair G Kergon 57417b2f66fSAlasdair G Kergon /* 57517b2f66fSAlasdair G Kergon * Trigger an event. 57617b2f66fSAlasdair G Kergon */ 57717b2f66fSAlasdair G Kergon void dm_table_event(struct dm_table *t); 57817b2f66fSAlasdair G Kergon 57917b2f66fSAlasdair G Kergon /* 5809974fa2cSMike Snitzer * Run the queue for request-based targets. 5819974fa2cSMike Snitzer */ 5829974fa2cSMike Snitzer void dm_table_run_md_queue_async(struct dm_table *t); 5839974fa2cSMike Snitzer 5849974fa2cSMike Snitzer /* 58517b2f66fSAlasdair G Kergon * The device must be suspended before calling this method. 586042d2a9bSAlasdair G Kergon * Returns the previous table, which the caller must destroy. 58717b2f66fSAlasdair G Kergon */ 588042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md, 589042d2a9bSAlasdair G Kergon struct dm_table *t); 59017b2f66fSAlasdair G Kergon 59154160904SMikulas Patocka /* 592cb77cb5aSEric Biggers * Table blk_crypto_profile functions 593aa6ce87aSSatya Tangirala */ 594cb77cb5aSEric Biggers void dm_destroy_crypto_profile(struct blk_crypto_profile *profile); 595aa6ce87aSSatya Tangirala 5960da336e5SAlasdair G Kergon /*----------------------------------------------------------------- 5970da336e5SAlasdair G Kergon * Macros. 5980da336e5SAlasdair G Kergon *---------------------------------------------------------------*/ 5990da336e5SAlasdair G Kergon #define DM_NAME "device-mapper" 6000da336e5SAlasdair G Kergon 601d2c3c8dcSJoe Perches #define DM_FMT(fmt) DM_NAME ": " DM_MSG_PREFIX ": " fmt "\n" 60210d3bd09SMikulas Patocka 603d2c3c8dcSJoe Perches #define DMCRIT(fmt, ...) pr_crit(DM_FMT(fmt), ##__VA_ARGS__) 604d2c3c8dcSJoe Perches 605d2c3c8dcSJoe Perches #define DMERR(fmt, ...) pr_err(DM_FMT(fmt), ##__VA_ARGS__) 606733232f8SMike Snitzer #define DMERR_LIMIT(fmt, ...) pr_err_ratelimited(DM_FMT(fmt), ##__VA_ARGS__) 607d2c3c8dcSJoe Perches #define DMWARN(fmt, ...) pr_warn(DM_FMT(fmt), ##__VA_ARGS__) 608733232f8SMike Snitzer #define DMWARN_LIMIT(fmt, ...) pr_warn_ratelimited(DM_FMT(fmt), ##__VA_ARGS__) 609d2c3c8dcSJoe Perches #define DMINFO(fmt, ...) pr_info(DM_FMT(fmt), ##__VA_ARGS__) 610733232f8SMike Snitzer #define DMINFO_LIMIT(fmt, ...) pr_info_ratelimited(DM_FMT(fmt), ##__VA_ARGS__) 6110da336e5SAlasdair G Kergon 61274244b59SHannes Reinecke #define DMDEBUG(fmt, ...) pr_debug(DM_FMT(fmt), ##__VA_ARGS__) 613733232f8SMike Snitzer #define DMDEBUG_LIMIT(fmt, ...) pr_debug_ratelimited(DM_FMT(fmt), ##__VA_ARGS__) 6140da336e5SAlasdair G Kergon 6150da336e5SAlasdair G Kergon #define DMEMIT(x...) sz += ((sz >= maxlen) ? \ 6160da336e5SAlasdair G Kergon 0 : scnprintf(result + sz, maxlen - sz, x)) 6170da336e5SAlasdair G Kergon 6188ec45662STushar Sugandhi #define DMEMIT_TARGET_NAME_VERSION(y) \ 6198ec45662STushar Sugandhi DMEMIT("target_name=%s,target_version=%u.%u.%u", \ 6208ec45662STushar Sugandhi (y)->name, (y)->version[0], (y)->version[1], (y)->version[2]) 6218ec45662STushar Sugandhi 6220da336e5SAlasdair G Kergon /* 6230da336e5SAlasdair G Kergon * Definitions of return values from target end_io function. 6240da336e5SAlasdair G Kergon */ 6257ed8578aSChristoph Hellwig #define DM_ENDIO_DONE 0 6260da336e5SAlasdair G Kergon #define DM_ENDIO_INCOMPLETE 1 6270da336e5SAlasdair G Kergon #define DM_ENDIO_REQUEUE 2 628ac514ffcSMike Snitzer #define DM_ENDIO_DELAY_REQUEUE 3 6290da336e5SAlasdair G Kergon 6300da336e5SAlasdair G Kergon /* 6310da336e5SAlasdair G Kergon * Definitions of return values from target map function. 6320da336e5SAlasdair G Kergon */ 6330da336e5SAlasdair G Kergon #define DM_MAPIO_SUBMITTED 0 6340da336e5SAlasdair G Kergon #define DM_MAPIO_REMAPPED 1 6350da336e5SAlasdair G Kergon #define DM_MAPIO_REQUEUE DM_ENDIO_REQUEUE 636ac514ffcSMike Snitzer #define DM_MAPIO_DELAY_REQUEUE DM_ENDIO_DELAY_REQUEUE 637412445acSChristoph Hellwig #define DM_MAPIO_KILL 4 6380da336e5SAlasdair G Kergon 639fd2ed4d2SMikulas Patocka #define dm_sector_div64(x, y)( \ 640fd2ed4d2SMikulas Patocka { \ 641fd2ed4d2SMikulas Patocka u64 _res; \ 642fd2ed4d2SMikulas Patocka (x) = div64_u64_rem(x, y, &_res); \ 643fd2ed4d2SMikulas Patocka _res; \ 644fd2ed4d2SMikulas Patocka } \ 645fd2ed4d2SMikulas Patocka ) 646fd2ed4d2SMikulas Patocka 6470da336e5SAlasdair G Kergon /* 6480da336e5SAlasdair G Kergon * Ceiling(n / sz) 6490da336e5SAlasdair G Kergon */ 6500da336e5SAlasdair G Kergon #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz)) 6510da336e5SAlasdair G Kergon 6520da336e5SAlasdair G Kergon #define dm_sector_div_up(n, sz) ( \ 6530da336e5SAlasdair G Kergon { \ 6540da336e5SAlasdair G Kergon sector_t _r = ((n) + (sz) - 1); \ 6550da336e5SAlasdair G Kergon sector_div(_r, (sz)); \ 6560da336e5SAlasdair G Kergon _r; \ 6570da336e5SAlasdair G Kergon } \ 6580da336e5SAlasdair G Kergon ) 6590da336e5SAlasdair G Kergon 6600da336e5SAlasdair G Kergon /* 6610da336e5SAlasdair G Kergon * ceiling(n / size) * size 6620da336e5SAlasdair G Kergon */ 6630da336e5SAlasdair G Kergon #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz)) 6640da336e5SAlasdair G Kergon 66556a67df7SMike Snitzer /* 66656a67df7SMike Snitzer * Sector offset taken relative to the start of the target instead of 66756a67df7SMike Snitzer * relative to the start of the device. 66856a67df7SMike Snitzer */ 66956a67df7SMike Snitzer #define dm_target_offset(ti, sector) ((sector) - (ti)->begin) 67056a67df7SMike Snitzer 6710bdb50c5SNeilBrown static inline sector_t to_sector(unsigned long long n) 6720da336e5SAlasdair G Kergon { 6730da336e5SAlasdair G Kergon return (n >> SECTOR_SHIFT); 6740da336e5SAlasdair G Kergon } 6750da336e5SAlasdair G Kergon 6760da336e5SAlasdair G Kergon static inline unsigned long to_bytes(sector_t n) 6770da336e5SAlasdair G Kergon { 6780da336e5SAlasdair G Kergon return (n << SECTOR_SHIFT); 6790da336e5SAlasdair G Kergon } 6800da336e5SAlasdair G Kergon 6811da177e4SLinus Torvalds #endif /* _LINUX_DEVICE_MAPPER_H */ 682