xref: /openbmc/linux/include/linux/device-mapper.h (revision b7f8dff09827c96032c34a945ee7757e394b5952)
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;
231da177e4SLinus Torvalds 
24e83068a5SMike Snitzer /*
25e83068a5SMike Snitzer  * Type of table, mapped_device's mempool and request_queue
26e83068a5SMike Snitzer  */
277e0d574fSBart Van Assche enum dm_queue_mode {
287e0d574fSBart Van Assche 	DM_TYPE_NONE		 = 0,
297e0d574fSBart Van Assche 	DM_TYPE_BIO_BASED	 = 1,
307e0d574fSBart Van Assche 	DM_TYPE_REQUEST_BASED	 = 2,
31953923c0SMike Snitzer 	DM_TYPE_DAX_BIO_BASED	 = 3,
327e0d574fSBart Van Assche };
33e83068a5SMike Snitzer 
3491ccbbacSTushar Sugandhi typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE, STATUSTYPE_IMA } status_type_t;
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds union map_info {
371da177e4SLinus Torvalds 	void *ptr;
381da177e4SLinus Torvalds };
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds /*
411da177e4SLinus Torvalds  * In the constructor the target parameter will already have the
421da177e4SLinus Torvalds  * table, type, begin and len fields filled in.
431da177e4SLinus Torvalds  */
441da177e4SLinus Torvalds typedef int (*dm_ctr_fn) (struct dm_target *target,
451da177e4SLinus Torvalds 			  unsigned int argc, char **argv);
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds /*
481da177e4SLinus Torvalds  * The destructor doesn't need to free the dm_target, just
491da177e4SLinus Torvalds  * anything hidden ti->private.
501da177e4SLinus Torvalds  */
511da177e4SLinus Torvalds typedef void (*dm_dtr_fn) (struct dm_target *ti);
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds /*
541da177e4SLinus Torvalds  * The map function must return:
551da177e4SLinus Torvalds  * < 0: error
561da177e4SLinus Torvalds  * = 0: The target will handle the io by resubmitting it later
5745cbcd79SKiyoshi Ueda  * = 1: simple remap complete
582e93ccc1SKiyoshi Ueda  * = 2: The target wants to push back the io
591da177e4SLinus Torvalds  */
607de3ee57SMikulas Patocka typedef int (*dm_map_fn) (struct dm_target *ti, struct bio *bio);
61e5863d9aSMike Snitzer typedef int (*dm_clone_and_map_request_fn) (struct dm_target *ti,
62e5863d9aSMike Snitzer 					    struct request *rq,
63e5863d9aSMike Snitzer 					    union map_info *map_context,
64e5863d9aSMike Snitzer 					    struct request **clone);
655de719e3SYufen Yu typedef void (*dm_release_clone_request_fn) (struct request *clone,
665de719e3SYufen Yu 					     union map_info *map_context);
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /*
691da177e4SLinus Torvalds  * Returns:
701da177e4SLinus Torvalds  * < 0 : error (currently ignored)
711da177e4SLinus Torvalds  * 0   : ended successfully
721da177e4SLinus Torvalds  * 1   : for some reason the io has still not completed (eg,
731da177e4SLinus Torvalds  *       multipath target might want to requeue a failed io).
742e93ccc1SKiyoshi Ueda  * 2   : The target wants to push back the io
751da177e4SLinus Torvalds  */
761da177e4SLinus Torvalds typedef int (*dm_endio_fn) (struct dm_target *ti,
774e4cbee9SChristoph Hellwig 			    struct bio *bio, blk_status_t *error);
787d76345dSKiyoshi Ueda typedef int (*dm_request_endio_fn) (struct dm_target *ti,
792a842acaSChristoph Hellwig 				    struct request *clone, blk_status_t error,
807d76345dSKiyoshi Ueda 				    union map_info *map_context);
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds typedef void (*dm_presuspend_fn) (struct dm_target *ti);
83d67ee213SMike Snitzer typedef void (*dm_presuspend_undo_fn) (struct dm_target *ti);
841da177e4SLinus Torvalds typedef void (*dm_postsuspend_fn) (struct dm_target *ti);
858757b776SMilan Broz typedef int (*dm_preresume_fn) (struct dm_target *ti);
861da177e4SLinus Torvalds typedef void (*dm_resume_fn) (struct dm_target *ti);
871da177e4SLinus Torvalds 
88fd7c092eSMikulas Patocka typedef void (*dm_status_fn) (struct dm_target *ti, status_type_t status_type,
891f4e0ff0SAlasdair G Kergon 			      unsigned status_flags, char *result, unsigned maxlen);
901da177e4SLinus Torvalds 
911eb5fa84SMike Snitzer typedef int (*dm_message_fn) (struct dm_target *ti, unsigned argc, char **argv,
921eb5fa84SMike Snitzer 			      char *result, unsigned maxlen);
931da177e4SLinus Torvalds 
945bd5e8d8SMike Snitzer typedef int (*dm_prepare_ioctl_fn) (struct dm_target *ti, struct block_device **bdev);
95aa129a22SMilan Broz 
96e3290b94SMike Snitzer #ifdef CONFIG_BLK_DEV_ZONED
97d4100351SChristoph Hellwig typedef int (*dm_report_zones_fn) (struct dm_target *ti,
98d4100351SChristoph Hellwig 				   struct dm_report_zones_args *args,
99d4100351SChristoph Hellwig 				   unsigned int nr_zones);
100e3290b94SMike Snitzer #else
101e3290b94SMike Snitzer /*
102e3290b94SMike Snitzer  * Define dm_report_zones_fn so that targets can assign to NULL if
103e3290b94SMike Snitzer  * CONFIG_BLK_DEV_ZONED disabled. Otherwise each target needs to do
104e3290b94SMike Snitzer  * awkward #ifdefs in their target_type, etc.
105e3290b94SMike Snitzer  */
106e3290b94SMike Snitzer typedef int (*dm_report_zones_fn) (struct dm_target *dummy);
107e3290b94SMike Snitzer #endif
108e76239a3SChristoph Hellwig 
109058ce5caSAlasdair G Kergon /*
110058ce5caSAlasdair G Kergon  * These iteration functions are typically used to check (and combine)
111058ce5caSAlasdair G Kergon  * properties of underlying devices.
112058ce5caSAlasdair G Kergon  * E.g. Does at least one underlying device support flush?
113058ce5caSAlasdair G Kergon  *      Does any underlying device not support WRITE_SAME?
114058ce5caSAlasdair G Kergon  *
115058ce5caSAlasdair G Kergon  * The callout function is called once for each contiguous section of
116058ce5caSAlasdair G Kergon  * an underlying device.  State can be maintained in *data.
117058ce5caSAlasdair G Kergon  * Return non-zero to stop iterating through any further devices.
118058ce5caSAlasdair G Kergon  */
119af4874e0SMike Snitzer typedef int (*iterate_devices_callout_fn) (struct dm_target *ti,
120af4874e0SMike Snitzer 					   struct dm_dev *dev,
1215dea271bSMike Snitzer 					   sector_t start, sector_t len,
122af4874e0SMike Snitzer 					   void *data);
123af4874e0SMike Snitzer 
124058ce5caSAlasdair G Kergon /*
125058ce5caSAlasdair G Kergon  * This function must iterate through each section of device used by the
126058ce5caSAlasdair G Kergon  * target until it encounters a non-zero return code, which it then returns.
127058ce5caSAlasdair G Kergon  * Returns zero if no callout returned non-zero.
128058ce5caSAlasdair G Kergon  */
129af4874e0SMike Snitzer typedef int (*dm_iterate_devices_fn) (struct dm_target *ti,
130af4874e0SMike Snitzer 				      iterate_devices_callout_fn fn,
131af4874e0SMike Snitzer 				      void *data);
132af4874e0SMike Snitzer 
13340bea431SMike Snitzer typedef void (*dm_io_hints_fn) (struct dm_target *ti,
13440bea431SMike Snitzer 				struct queue_limits *limits);
13540bea431SMike Snitzer 
1367d76345dSKiyoshi Ueda /*
1377d76345dSKiyoshi Ueda  * Returns:
1387d76345dSKiyoshi Ueda  *    0: The target can handle the next I/O immediately.
1397d76345dSKiyoshi Ueda  *    1: The target can't handle the next I/O immediately.
1407d76345dSKiyoshi Ueda  */
1417d76345dSKiyoshi Ueda typedef int (*dm_busy_fn) (struct dm_target *ti);
1427d76345dSKiyoshi Ueda 
143545ed20eSToshi Kani /*
144545ed20eSToshi Kani  * Returns:
145545ed20eSToshi Kani  *  < 0 : error
146545ed20eSToshi Kani  * >= 0 : the number of bytes accessible at the address
147545ed20eSToshi Kani  */
148817bf402SDan Williams typedef long (*dm_dax_direct_access_fn) (struct dm_target *ti, pgoff_t pgoff,
149817bf402SDan Williams 		long nr_pages, void **kaddr, pfn_t *pfn);
150cdf6cdcdSVivek Goyal typedef int (*dm_dax_zero_page_range_fn)(struct dm_target *ti, pgoff_t pgoff,
151cdf6cdcdSVivek Goyal 		size_t nr_pages);
152545ed20eSToshi Kani 
1531da177e4SLinus Torvalds void dm_error(const char *message);
1541da177e4SLinus Torvalds 
15582b1519bSMikulas Patocka struct dm_dev {
15682b1519bSMikulas Patocka 	struct block_device *bdev;
157817bf402SDan Williams 	struct dax_device *dax_dev;
158aeb5d727SAl Viro 	fmode_t mode;
15982b1519bSMikulas Patocka 	char name[16];
16082b1519bSMikulas Patocka };
16182b1519bSMikulas Patocka 
1624df2bf46SDingXiang dev_t dm_get_dev_t(const char *path);
1634df2bf46SDingXiang 
1643cb40214SBryn Reeves /*
1651da177e4SLinus Torvalds  * Constructors should call these functions to ensure destination devices
1661da177e4SLinus Torvalds  * are opened/closed correctly.
1671da177e4SLinus Torvalds  */
1688215d6ecSNikanth Karthikesan int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
1698215d6ecSNikanth Karthikesan 		  struct dm_dev **result);
1701da177e4SLinus Torvalds void dm_put_device(struct dm_target *ti, struct dm_dev *d);
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds /*
1731da177e4SLinus Torvalds  * Information about a target type
1741da177e4SLinus Torvalds  */
175ab4c1424SAndi Kleen 
1761da177e4SLinus Torvalds struct target_type {
177ab4c1424SAndi Kleen 	uint64_t features;
1781da177e4SLinus Torvalds 	const char *name;
1791da177e4SLinus Torvalds 	struct module *module;
1801da177e4SLinus Torvalds 	unsigned version[3];
1811da177e4SLinus Torvalds 	dm_ctr_fn ctr;
1821da177e4SLinus Torvalds 	dm_dtr_fn dtr;
1831da177e4SLinus Torvalds 	dm_map_fn map;
184e5863d9aSMike Snitzer 	dm_clone_and_map_request_fn clone_and_map_rq;
185e5863d9aSMike Snitzer 	dm_release_clone_request_fn release_clone_rq;
1861da177e4SLinus Torvalds 	dm_endio_fn end_io;
1877d76345dSKiyoshi Ueda 	dm_request_endio_fn rq_end_io;
1881da177e4SLinus Torvalds 	dm_presuspend_fn presuspend;
189d67ee213SMike Snitzer 	dm_presuspend_undo_fn presuspend_undo;
1901da177e4SLinus Torvalds 	dm_postsuspend_fn postsuspend;
1918757b776SMilan Broz 	dm_preresume_fn preresume;
1921da177e4SLinus Torvalds 	dm_resume_fn resume;
1931da177e4SLinus Torvalds 	dm_status_fn status;
1941da177e4SLinus Torvalds 	dm_message_fn message;
195e56f81e0SChristoph Hellwig 	dm_prepare_ioctl_fn prepare_ioctl;
196e76239a3SChristoph Hellwig 	dm_report_zones_fn report_zones;
1977d76345dSKiyoshi Ueda 	dm_busy_fn busy;
198af4874e0SMike Snitzer 	dm_iterate_devices_fn iterate_devices;
19940bea431SMike Snitzer 	dm_io_hints_fn io_hints;
200817bf402SDan Williams 	dm_dax_direct_access_fn direct_access;
201cdf6cdcdSVivek Goyal 	dm_dax_zero_page_range_fn dax_zero_page_range;
20245194e4fSCheng Renquan 
20345194e4fSCheng Renquan 	/* For internal device-mapper use. */
20445194e4fSCheng Renquan 	struct list_head list;
2051da177e4SLinus Torvalds };
2061da177e4SLinus Torvalds 
2073791e2fcSAlasdair G Kergon /*
2083791e2fcSAlasdair G Kergon  * Target features
2093791e2fcSAlasdair G Kergon  */
2103791e2fcSAlasdair G Kergon 
2113791e2fcSAlasdair G Kergon /*
2123791e2fcSAlasdair G Kergon  * Any table that contains an instance of this target must have only one.
2133791e2fcSAlasdair G Kergon  */
2143791e2fcSAlasdair G Kergon #define DM_TARGET_SINGLETON		0x00000001
2153791e2fcSAlasdair G Kergon #define dm_target_needs_singleton(type)	((type)->features & DM_TARGET_SINGLETON)
2163791e2fcSAlasdair G Kergon 
217cc6cbe14SAlasdair G Kergon /*
218cc6cbe14SAlasdair G Kergon  * Indicates that a target does not support read-only devices.
219cc6cbe14SAlasdair G Kergon  */
220cc6cbe14SAlasdair G Kergon #define DM_TARGET_ALWAYS_WRITEABLE	0x00000002
221cc6cbe14SAlasdair G Kergon #define dm_target_always_writeable(type) \
222cc6cbe14SAlasdair G Kergon 		((type)->features & DM_TARGET_ALWAYS_WRITEABLE)
223cc6cbe14SAlasdair G Kergon 
22436a0456fSAlasdair G Kergon /*
22536a0456fSAlasdair G Kergon  * Any device that contains a table with an instance of this target may never
22636a0456fSAlasdair G Kergon  * have tables containing any different target type.
22736a0456fSAlasdair G Kergon  */
22836a0456fSAlasdair G Kergon #define DM_TARGET_IMMUTABLE		0x00000004
22936a0456fSAlasdair G Kergon #define dm_target_is_immutable(type)	((type)->features & DM_TARGET_IMMUTABLE)
23036a0456fSAlasdair G Kergon 
231b0d8ed4dSAlasdair G Kergon /*
232f083b09bSMike Snitzer  * Indicates that a target may replace any target; even immutable targets.
233f083b09bSMike Snitzer  * .map, .map_rq, .clone_and_map_rq and .release_clone_rq are all defined.
234f083b09bSMike Snitzer  */
235f083b09bSMike Snitzer #define DM_TARGET_WILDCARD		0x00000008
236f083b09bSMike Snitzer #define dm_target_is_wildcard(type)	((type)->features & DM_TARGET_WILDCARD)
237f083b09bSMike Snitzer 
238f083b09bSMike Snitzer /*
2399b4b5a79SMilan Broz  * A target implements own bio data integrity.
2409b4b5a79SMilan Broz  */
2419b4b5a79SMilan Broz #define DM_TARGET_INTEGRITY		0x00000010
2429b4b5a79SMilan Broz #define dm_target_has_integrity(type)	((type)->features & DM_TARGET_INTEGRITY)
2439b4b5a79SMilan Broz 
244e2460f2aSMikulas Patocka /*
245e2460f2aSMikulas Patocka  * A target passes integrity data to the lower device.
246e2460f2aSMikulas Patocka  */
247e2460f2aSMikulas Patocka #define DM_TARGET_PASSES_INTEGRITY	0x00000020
248e2460f2aSMikulas Patocka #define dm_target_passes_integrity(type) ((type)->features & DM_TARGET_PASSES_INTEGRITY)
249e2460f2aSMikulas Patocka 
250dd88d313SDamien Le Moal /*
2512d669cebSShin'ichiro Kawasaki  * Indicates support for zoned block devices:
2522d669cebSShin'ichiro Kawasaki  * - DM_TARGET_ZONED_HM: the target also supports host-managed zoned
2532d669cebSShin'ichiro Kawasaki  *   block devices but does not support combining different zoned models.
2542d669cebSShin'ichiro Kawasaki  * - DM_TARGET_MIXED_ZONED_MODEL: the target supports combining multiple
2552d669cebSShin'ichiro Kawasaki  *   devices with different zoned models.
256dd88d313SDamien Le Moal  */
257e3290b94SMike Snitzer #ifdef CONFIG_BLK_DEV_ZONED
258dd88d313SDamien Le Moal #define DM_TARGET_ZONED_HM		0x00000040
259dd88d313SDamien Le Moal #define dm_target_supports_zoned_hm(type) ((type)->features & DM_TARGET_ZONED_HM)
260e3290b94SMike Snitzer #else
261e3290b94SMike Snitzer #define DM_TARGET_ZONED_HM		0x00000000
262e3290b94SMike Snitzer #define dm_target_supports_zoned_hm(type) (false)
263e3290b94SMike Snitzer #endif
264dd88d313SDamien Le Moal 
2656abc4946SKonstantin Khlebnikov /*
2666abc4946SKonstantin Khlebnikov  * A target handles REQ_NOWAIT
2676abc4946SKonstantin Khlebnikov  */
2686abc4946SKonstantin Khlebnikov #define DM_TARGET_NOWAIT		0x00000080
2696abc4946SKonstantin Khlebnikov #define dm_target_supports_nowait(type) ((type)->features & DM_TARGET_NOWAIT)
2706abc4946SKonstantin Khlebnikov 
271aa6ce87aSSatya Tangirala /*
272aa6ce87aSSatya Tangirala  * A target supports passing through inline crypto support.
273aa6ce87aSSatya Tangirala  */
274aa6ce87aSSatya Tangirala #define DM_TARGET_PASSES_CRYPTO		0x00000100
275aa6ce87aSSatya Tangirala #define dm_target_passes_crypto(type) ((type)->features & DM_TARGET_PASSES_CRYPTO)
276aa6ce87aSSatya Tangirala 
2772d669cebSShin'ichiro Kawasaki #ifdef CONFIG_BLK_DEV_ZONED
2782d669cebSShin'ichiro Kawasaki #define DM_TARGET_MIXED_ZONED_MODEL	0x00000200
2792d669cebSShin'ichiro Kawasaki #define dm_target_supports_mixed_zoned_model(type) \
2802d669cebSShin'ichiro Kawasaki 	((type)->features & DM_TARGET_MIXED_ZONED_MODEL)
2812d669cebSShin'ichiro Kawasaki #else
2822d669cebSShin'ichiro Kawasaki #define DM_TARGET_MIXED_ZONED_MODEL	0x00000000
2832d669cebSShin'ichiro Kawasaki #define dm_target_supports_mixed_zoned_model(type) (false)
2842d669cebSShin'ichiro Kawasaki #endif
2852d669cebSShin'ichiro Kawasaki 
2861da177e4SLinus Torvalds struct dm_target {
2871da177e4SLinus Torvalds 	struct dm_table *table;
2881da177e4SLinus Torvalds 	struct target_type *type;
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	/* target limits */
2911da177e4SLinus Torvalds 	sector_t begin;
2921da177e4SLinus Torvalds 	sector_t len;
2931da177e4SLinus Torvalds 
294542f9038SMike Snitzer 	/* If non-zero, maximum size of I/O submitted to a target. */
295542f9038SMike Snitzer 	uint32_t max_io_len;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	/*
29855a62eefSAlasdair G Kergon 	 * A number of zero-length barrier bios that will be submitted
299f9ab94ceSMikulas Patocka 	 * to the target for the purpose of flushing cache.
300f9ab94ceSMikulas Patocka 	 *
30155a62eefSAlasdair G Kergon 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
30255a62eefSAlasdair G Kergon 	 * It is a responsibility of the target driver to remap these bios
303f9ab94ceSMikulas Patocka 	 * to the real underlying devices.
304f9ab94ceSMikulas Patocka 	 */
30555a62eefSAlasdair G Kergon 	unsigned num_flush_bios;
306f9ab94ceSMikulas Patocka 
3075ae89a87SMike Snitzer 	/*
30855a62eefSAlasdair G Kergon 	 * The number of discard bios that will be submitted to the target.
30955a62eefSAlasdair G Kergon 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
3105ae89a87SMike Snitzer 	 */
31155a62eefSAlasdair G Kergon 	unsigned num_discard_bios;
3125ae89a87SMike Snitzer 
313d54eaa5aSMike Snitzer 	/*
31400716545SDenis Semakin 	 * The number of secure erase bios that will be submitted to the target.
31500716545SDenis Semakin 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
31600716545SDenis Semakin 	 */
31700716545SDenis Semakin 	unsigned num_secure_erase_bios;
31800716545SDenis Semakin 
31900716545SDenis Semakin 	/*
32055a62eefSAlasdair G Kergon 	 * The number of WRITE SAME bios that will be submitted to the target.
32155a62eefSAlasdair G Kergon 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
322d54eaa5aSMike Snitzer 	 */
32355a62eefSAlasdair G Kergon 	unsigned num_write_same_bios;
324d54eaa5aSMike Snitzer 
325c0820cf5SMikulas Patocka 	/*
326ac62d620SChristoph Hellwig 	 * The number of WRITE ZEROES bios that will be submitted to the target.
327ac62d620SChristoph Hellwig 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
328ac62d620SChristoph Hellwig 	 */
329ac62d620SChristoph Hellwig 	unsigned num_write_zeroes_bios;
330ac62d620SChristoph Hellwig 
331ac62d620SChristoph Hellwig 	/*
33230187e1dSMike Snitzer 	 * The minimum number of extra bytes allocated in each io for the
33330187e1dSMike Snitzer 	 * target to use.
334c0820cf5SMikulas Patocka 	 */
33530187e1dSMike Snitzer 	unsigned per_io_data_size;
336c0820cf5SMikulas Patocka 
3371da177e4SLinus Torvalds 	/* target specific data */
3381da177e4SLinus Torvalds 	void *private;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	/* Used to provide an error string from the ctr */
3411da177e4SLinus Torvalds 	char *error;
3424c259327SMike Snitzer 
3434c259327SMike Snitzer 	/*
3440e9c24edSJoe Thornber 	 * Set if this target needs to receive flushes regardless of
3450e9c24edSJoe Thornber 	 * whether or not its underlying devices have support.
3460e9c24edSJoe Thornber 	 */
3470e9c24edSJoe Thornber 	bool flush_supported:1;
3480e9c24edSJoe Thornber 
3490e9c24edSJoe Thornber 	/*
3504c259327SMike Snitzer 	 * Set if this target needs to receive discards regardless of
3514c259327SMike Snitzer 	 * whether or not its underlying devices have support.
3524c259327SMike Snitzer 	 */
3530ac55489SAlasdair G Kergon 	bool discards_supported:1;
354a666e5c0SMikulas Patocka 
355a666e5c0SMikulas Patocka 	/*
356a666e5c0SMikulas Patocka 	 * Set if we need to limit the number of in-flight bios when swapping.
357a666e5c0SMikulas Patocka 	 */
358a666e5c0SMikulas Patocka 	bool limit_swap_bios:1;
359bb37d772SDamien Le Moal 
360bb37d772SDamien Le Moal 	/*
361a8b9d116STom Rix 	 * Set if this target implements a zoned device and needs emulation of
362bb37d772SDamien Le Moal 	 * zone append operations using regular writes.
363bb37d772SDamien Le Moal 	 */
364bb37d772SDamien Le Moal 	bool emulate_zone_append:1;
3650fbb4d93SMike Snitzer 
3660fbb4d93SMike Snitzer 	/*
3670fbb4d93SMike Snitzer 	 * Set if the target will submit IO using dm_submit_bio_remap()
3680fbb4d93SMike Snitzer 	 * after returning DM_MAPIO_SUBMITTED from its map function.
3690fbb4d93SMike Snitzer 	 */
3700fbb4d93SMike Snitzer 	bool accounts_remapped_io:1;
3711da177e4SLinus Torvalds };
3721da177e4SLinus Torvalds 
37364f52b0eSMike Snitzer void *dm_per_bio_data(struct bio *bio, size_t data_size);
37464f52b0eSMike Snitzer struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size);
37564f52b0eSMike Snitzer unsigned dm_bio_get_target_bio_nr(const struct bio *bio);
376ddbd658fSMikulas Patocka 
377087615bfSGabriel Krisman Bertazi u64 dm_start_time_ns_from_clone(struct bio *bio);
378087615bfSGabriel Krisman Bertazi 
3791da177e4SLinus Torvalds int dm_register_target(struct target_type *t);
38010d3bd09SMikulas Patocka void dm_unregister_target(struct target_type *t);
38117b2f66fSAlasdair G Kergon 
382498f0103SMike Snitzer /*
383498f0103SMike Snitzer  * Target argument parsing.
384498f0103SMike Snitzer  */
385498f0103SMike Snitzer struct dm_arg_set {
386498f0103SMike Snitzer 	unsigned argc;
387498f0103SMike Snitzer 	char **argv;
388498f0103SMike Snitzer };
389498f0103SMike Snitzer 
390498f0103SMike Snitzer /*
391498f0103SMike Snitzer  * The minimum and maximum value of a numeric argument, together with
392498f0103SMike Snitzer  * the error message to use if the number is found to be outside that range.
393498f0103SMike Snitzer  */
394498f0103SMike Snitzer struct dm_arg {
395498f0103SMike Snitzer 	unsigned min;
396498f0103SMike Snitzer 	unsigned max;
397498f0103SMike Snitzer 	char *error;
398498f0103SMike Snitzer };
399498f0103SMike Snitzer 
400498f0103SMike Snitzer /*
401498f0103SMike Snitzer  * Validate the next argument, either returning it as *value or, if invalid,
402498f0103SMike Snitzer  * returning -EINVAL and setting *error.
403498f0103SMike Snitzer  */
4045916a22bSEric Biggers int dm_read_arg(const struct dm_arg *arg, struct dm_arg_set *arg_set,
405498f0103SMike Snitzer 		unsigned *value, char **error);
406498f0103SMike Snitzer 
407498f0103SMike Snitzer /*
408498f0103SMike Snitzer  * Process the next argument as the start of a group containing between
409498f0103SMike Snitzer  * arg->min and arg->max further arguments. Either return the size as
410498f0103SMike Snitzer  * *num_args or, if invalid, return -EINVAL and set *error.
411498f0103SMike Snitzer  */
4125916a22bSEric Biggers int dm_read_arg_group(const struct dm_arg *arg, struct dm_arg_set *arg_set,
413498f0103SMike Snitzer 		      unsigned *num_args, char **error);
414498f0103SMike Snitzer 
415498f0103SMike Snitzer /*
416498f0103SMike Snitzer  * Return the current argument and shift to the next.
417498f0103SMike Snitzer  */
418498f0103SMike Snitzer const char *dm_shift_arg(struct dm_arg_set *as);
419498f0103SMike Snitzer 
420498f0103SMike Snitzer /*
421498f0103SMike Snitzer  * Move through num_args arguments.
422498f0103SMike Snitzer  */
423498f0103SMike Snitzer void dm_consume_args(struct dm_arg_set *as, unsigned num_args);
424498f0103SMike Snitzer 
42517b2f66fSAlasdair G Kergon /*-----------------------------------------------------------------
42617b2f66fSAlasdair G Kergon  * Functions for creating and manipulating mapped devices.
42717b2f66fSAlasdair G Kergon  * Drop the reference with dm_put when you finish with the object.
42817b2f66fSAlasdair G Kergon  *---------------------------------------------------------------*/
42917b2f66fSAlasdair G Kergon 
43017b2f66fSAlasdair G Kergon /*
43117b2f66fSAlasdair G Kergon  * DM_ANY_MINOR chooses the next available minor number.
43217b2f66fSAlasdair G Kergon  */
43317b2f66fSAlasdair G Kergon #define DM_ANY_MINOR (-1)
43417b2f66fSAlasdair G Kergon int dm_create(int minor, struct mapped_device **md);
43517b2f66fSAlasdair G Kergon 
43617b2f66fSAlasdair G Kergon /*
43717b2f66fSAlasdair G Kergon  * Reference counting for md.
43817b2f66fSAlasdair G Kergon  */
43917b2f66fSAlasdair G Kergon struct mapped_device *dm_get_md(dev_t dev);
44017b2f66fSAlasdair G Kergon void dm_get(struct mapped_device *md);
44109ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md);
44217b2f66fSAlasdair G Kergon void dm_put(struct mapped_device *md);
44317b2f66fSAlasdair G Kergon 
44417b2f66fSAlasdair G Kergon /*
44517b2f66fSAlasdair G Kergon  * An arbitrary pointer may be stored alongside a mapped device.
44617b2f66fSAlasdair G Kergon  */
44717b2f66fSAlasdair G Kergon void dm_set_mdptr(struct mapped_device *md, void *ptr);
44817b2f66fSAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md);
44917b2f66fSAlasdair G Kergon 
45017b2f66fSAlasdair G Kergon /*
45117b2f66fSAlasdair G Kergon  * A device can still be used while suspended, but I/O is deferred.
45217b2f66fSAlasdair G Kergon  */
453a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags);
45417b2f66fSAlasdair G Kergon int dm_resume(struct mapped_device *md);
45517b2f66fSAlasdair G Kergon 
45617b2f66fSAlasdair G Kergon /*
45717b2f66fSAlasdair G Kergon  * Event functions.
45817b2f66fSAlasdair G Kergon  */
45917b2f66fSAlasdair G Kergon uint32_t dm_get_event_nr(struct mapped_device *md);
46017b2f66fSAlasdair G Kergon int dm_wait_event(struct mapped_device *md, int event_nr);
4617a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md);
4627a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist);
46317b2f66fSAlasdair G Kergon 
46417b2f66fSAlasdair G Kergon /*
46517b2f66fSAlasdair G Kergon  * Info functions.
46617b2f66fSAlasdair G Kergon  */
46772d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md);
46896a1f7dbSMike Anderson int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid);
46917b2f66fSAlasdair G Kergon struct gendisk *dm_disk(struct mapped_device *md);
47064dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti);
4715df96f2bSMikulas Patocka int dm_post_suspending(struct dm_target *ti);
4722e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti);
4731dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors);
474*b7f8dff0SMike Snitzer void dm_submit_bio_remap(struct bio *clone, struct bio *tgt_clone);
475cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq);
47617b2f66fSAlasdair G Kergon 
477d4100351SChristoph Hellwig #ifdef CONFIG_BLK_DEV_ZONED
478d4100351SChristoph Hellwig struct dm_report_zones_args {
479d4100351SChristoph Hellwig 	struct dm_target *tgt;
480d4100351SChristoph Hellwig 	sector_t next_sector;
481d4100351SChristoph Hellwig 
482d4100351SChristoph Hellwig 	void *orig_data;
483d4100351SChristoph Hellwig 	report_zones_cb orig_cb;
484d4100351SChristoph Hellwig 	unsigned int zone_idx;
485d4100351SChristoph Hellwig 
486d4100351SChristoph Hellwig 	/* must be filled by ->report_zones before calling dm_report_zones_cb */
487d4100351SChristoph Hellwig 	sector_t start;
488d4100351SChristoph Hellwig };
489912e8875SDamien Le Moal int dm_report_zones(struct block_device *bdev, sector_t start, sector_t sector,
490912e8875SDamien Le Moal 		    struct dm_report_zones_args *args, unsigned int nr_zones);
491d4100351SChristoph Hellwig #endif /* CONFIG_BLK_DEV_ZONED */
492d4100351SChristoph Hellwig 
4936bbc923dSHelen Koike /*
4946bbc923dSHelen Koike  * Device mapper functions to parse and create devices specified by the
4956bbc923dSHelen Koike  * parameter "dm-mod.create="
4966bbc923dSHelen Koike  */
4976bbc923dSHelen Koike int __init dm_early_create(struct dm_ioctl *dmi,
4986bbc923dSHelen Koike 			   struct dm_target_spec **spec_array,
4996bbc923dSHelen Koike 			   char **target_params_array);
5006bbc923dSHelen Koike 
501f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md);
502f84cb8a4SMike Snitzer 
50317b2f66fSAlasdair G Kergon /*
50417b2f66fSAlasdair G Kergon  * Geometry functions.
50517b2f66fSAlasdair G Kergon  */
50617b2f66fSAlasdair G Kergon int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo);
50717b2f66fSAlasdair G Kergon int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo);
50817b2f66fSAlasdair G Kergon 
50917b2f66fSAlasdair G Kergon /*-----------------------------------------------------------------
51017b2f66fSAlasdair G Kergon  * Functions for manipulating device-mapper tables.
51117b2f66fSAlasdair G Kergon  *---------------------------------------------------------------*/
51217b2f66fSAlasdair G Kergon 
51317b2f66fSAlasdair G Kergon /*
51417b2f66fSAlasdair G Kergon  * First create an empty table.
51517b2f66fSAlasdair G Kergon  */
516aeb5d727SAl Viro int dm_table_create(struct dm_table **result, fmode_t mode,
51717b2f66fSAlasdair G Kergon 		    unsigned num_targets, struct mapped_device *md);
51817b2f66fSAlasdair G Kergon 
51917b2f66fSAlasdair G Kergon /*
52017b2f66fSAlasdair G Kergon  * Then call this once for each target.
52117b2f66fSAlasdair G Kergon  */
52217b2f66fSAlasdair G Kergon int dm_table_add_target(struct dm_table *t, const char *type,
52317b2f66fSAlasdair G Kergon 			sector_t start, sector_t len, char *params);
52417b2f66fSAlasdair G Kergon 
52517b2f66fSAlasdair G Kergon /*
526e83068a5SMike Snitzer  * Target can use this to set the table's type.
527e83068a5SMike Snitzer  * Can only ever be called from a target's ctr.
528e83068a5SMike Snitzer  * Useful for "hybrid" target (supports both bio-based
529e83068a5SMike Snitzer  * and request-based).
530e83068a5SMike Snitzer  */
5317e0d574fSBart Van Assche void dm_table_set_type(struct dm_table *t, enum dm_queue_mode type);
532e83068a5SMike Snitzer 
533e83068a5SMike Snitzer /*
53417b2f66fSAlasdair G Kergon  * Finally call this to make the table ready for use.
53517b2f66fSAlasdair G Kergon  */
53617b2f66fSAlasdair G Kergon int dm_table_complete(struct dm_table *t);
53717b2f66fSAlasdair G Kergon 
53817b2f66fSAlasdair G Kergon /*
539f6e7baadSBrian Norris  * Destroy the table when finished.
540f6e7baadSBrian Norris  */
541f6e7baadSBrian Norris void dm_table_destroy(struct dm_table *t);
542f6e7baadSBrian Norris 
543f6e7baadSBrian Norris /*
544542f9038SMike Snitzer  * Target may require that it is never sent I/O larger than len.
545542f9038SMike Snitzer  */
546542f9038SMike Snitzer int __must_check dm_set_target_max_io_len(struct dm_target *ti, sector_t len);
547542f9038SMike Snitzer 
548542f9038SMike Snitzer /*
54917b2f66fSAlasdair G Kergon  * Table reference counting.
55017b2f66fSAlasdair G Kergon  */
55183d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx);
55283d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx);
55383d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md);
55417b2f66fSAlasdair G Kergon 
55517b2f66fSAlasdair G Kergon /*
55617b2f66fSAlasdair G Kergon  * Queries
55717b2f66fSAlasdair G Kergon  */
55817b2f66fSAlasdair G Kergon sector_t dm_table_get_size(struct dm_table *t);
55917b2f66fSAlasdair G Kergon unsigned int dm_table_get_num_targets(struct dm_table *t);
560aeb5d727SAl Viro fmode_t dm_table_get_mode(struct dm_table *t);
56117b2f66fSAlasdair G Kergon struct mapped_device *dm_table_get_md(struct dm_table *t);
562f349b0a3SMichał Mirosław const char *dm_table_device_name(struct dm_table *t);
56317b2f66fSAlasdair G Kergon 
56417b2f66fSAlasdair G Kergon /*
56517b2f66fSAlasdair G Kergon  * Trigger an event.
56617b2f66fSAlasdair G Kergon  */
56717b2f66fSAlasdair G Kergon void dm_table_event(struct dm_table *t);
56817b2f66fSAlasdair G Kergon 
56917b2f66fSAlasdair G Kergon /*
5709974fa2cSMike Snitzer  * Run the queue for request-based targets.
5719974fa2cSMike Snitzer  */
5729974fa2cSMike Snitzer void dm_table_run_md_queue_async(struct dm_table *t);
5739974fa2cSMike Snitzer 
5749974fa2cSMike Snitzer /*
57517b2f66fSAlasdair G Kergon  * The device must be suspended before calling this method.
576042d2a9bSAlasdair G Kergon  * Returns the previous table, which the caller must destroy.
57717b2f66fSAlasdair G Kergon  */
578042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md,
579042d2a9bSAlasdair G Kergon 			       struct dm_table *t);
58017b2f66fSAlasdair G Kergon 
58154160904SMikulas Patocka /*
582cb77cb5aSEric Biggers  * Table blk_crypto_profile functions
583aa6ce87aSSatya Tangirala  */
584cb77cb5aSEric Biggers void dm_destroy_crypto_profile(struct blk_crypto_profile *profile);
585aa6ce87aSSatya Tangirala 
5860da336e5SAlasdair G Kergon /*-----------------------------------------------------------------
5870da336e5SAlasdair G Kergon  * Macros.
5880da336e5SAlasdair G Kergon  *---------------------------------------------------------------*/
5890da336e5SAlasdair G Kergon #define DM_NAME "device-mapper"
5900da336e5SAlasdair G Kergon 
591d2c3c8dcSJoe Perches #define DM_FMT(fmt) DM_NAME ": " DM_MSG_PREFIX ": " fmt "\n"
59210d3bd09SMikulas Patocka 
593d2c3c8dcSJoe Perches #define DMCRIT(fmt, ...) pr_crit(DM_FMT(fmt), ##__VA_ARGS__)
594d2c3c8dcSJoe Perches 
595d2c3c8dcSJoe Perches #define DMERR(fmt, ...) pr_err(DM_FMT(fmt), ##__VA_ARGS__)
596733232f8SMike Snitzer #define DMERR_LIMIT(fmt, ...) pr_err_ratelimited(DM_FMT(fmt), ##__VA_ARGS__)
597d2c3c8dcSJoe Perches #define DMWARN(fmt, ...) pr_warn(DM_FMT(fmt), ##__VA_ARGS__)
598733232f8SMike Snitzer #define DMWARN_LIMIT(fmt, ...) pr_warn_ratelimited(DM_FMT(fmt), ##__VA_ARGS__)
599d2c3c8dcSJoe Perches #define DMINFO(fmt, ...) pr_info(DM_FMT(fmt), ##__VA_ARGS__)
600733232f8SMike Snitzer #define DMINFO_LIMIT(fmt, ...) pr_info_ratelimited(DM_FMT(fmt), ##__VA_ARGS__)
6010da336e5SAlasdair G Kergon 
60274244b59SHannes Reinecke #define DMDEBUG(fmt, ...) pr_debug(DM_FMT(fmt), ##__VA_ARGS__)
603733232f8SMike Snitzer #define DMDEBUG_LIMIT(fmt, ...) pr_debug_ratelimited(DM_FMT(fmt), ##__VA_ARGS__)
6040da336e5SAlasdair G Kergon 
6050da336e5SAlasdair G Kergon #define DMEMIT(x...) sz += ((sz >= maxlen) ? \
6060da336e5SAlasdair G Kergon 			  0 : scnprintf(result + sz, maxlen - sz, x))
6070da336e5SAlasdair G Kergon 
6088ec45662STushar Sugandhi #define DMEMIT_TARGET_NAME_VERSION(y) \
6098ec45662STushar Sugandhi 		DMEMIT("target_name=%s,target_version=%u.%u.%u", \
6108ec45662STushar Sugandhi 		       (y)->name, (y)->version[0], (y)->version[1], (y)->version[2])
6118ec45662STushar Sugandhi 
6120da336e5SAlasdair G Kergon /*
6130da336e5SAlasdair G Kergon  * Definitions of return values from target end_io function.
6140da336e5SAlasdair G Kergon  */
6157ed8578aSChristoph Hellwig #define DM_ENDIO_DONE		0
6160da336e5SAlasdair G Kergon #define DM_ENDIO_INCOMPLETE	1
6170da336e5SAlasdair G Kergon #define DM_ENDIO_REQUEUE	2
618ac514ffcSMike Snitzer #define DM_ENDIO_DELAY_REQUEUE	3
6190da336e5SAlasdair G Kergon 
6200da336e5SAlasdair G Kergon /*
6210da336e5SAlasdair G Kergon  * Definitions of return values from target map function.
6220da336e5SAlasdair G Kergon  */
6230da336e5SAlasdair G Kergon #define DM_MAPIO_SUBMITTED	0
6240da336e5SAlasdair G Kergon #define DM_MAPIO_REMAPPED	1
6250da336e5SAlasdair G Kergon #define DM_MAPIO_REQUEUE	DM_ENDIO_REQUEUE
626ac514ffcSMike Snitzer #define DM_MAPIO_DELAY_REQUEUE	DM_ENDIO_DELAY_REQUEUE
627412445acSChristoph Hellwig #define DM_MAPIO_KILL		4
6280da336e5SAlasdair G Kergon 
629fd2ed4d2SMikulas Patocka #define dm_sector_div64(x, y)( \
630fd2ed4d2SMikulas Patocka { \
631fd2ed4d2SMikulas Patocka 	u64 _res; \
632fd2ed4d2SMikulas Patocka 	(x) = div64_u64_rem(x, y, &_res); \
633fd2ed4d2SMikulas Patocka 	_res; \
634fd2ed4d2SMikulas Patocka } \
635fd2ed4d2SMikulas Patocka )
636fd2ed4d2SMikulas Patocka 
6370da336e5SAlasdair G Kergon /*
6380da336e5SAlasdair G Kergon  * Ceiling(n / sz)
6390da336e5SAlasdair G Kergon  */
6400da336e5SAlasdair G Kergon #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz))
6410da336e5SAlasdair G Kergon 
6420da336e5SAlasdair G Kergon #define dm_sector_div_up(n, sz) ( \
6430da336e5SAlasdair G Kergon { \
6440da336e5SAlasdair G Kergon 	sector_t _r = ((n) + (sz) - 1); \
6450da336e5SAlasdair G Kergon 	sector_div(_r, (sz)); \
6460da336e5SAlasdair G Kergon 	_r; \
6470da336e5SAlasdair G Kergon } \
6480da336e5SAlasdair G Kergon )
6490da336e5SAlasdair G Kergon 
6500da336e5SAlasdair G Kergon /*
6510da336e5SAlasdair G Kergon  * ceiling(n / size) * size
6520da336e5SAlasdair G Kergon  */
6530da336e5SAlasdair G Kergon #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
6540da336e5SAlasdair G Kergon 
65556a67df7SMike Snitzer /*
65656a67df7SMike Snitzer  * Sector offset taken relative to the start of the target instead of
65756a67df7SMike Snitzer  * relative to the start of the device.
65856a67df7SMike Snitzer  */
65956a67df7SMike Snitzer #define dm_target_offset(ti, sector) ((sector) - (ti)->begin)
66056a67df7SMike Snitzer 
6610bdb50c5SNeilBrown static inline sector_t to_sector(unsigned long long n)
6620da336e5SAlasdair G Kergon {
6630da336e5SAlasdair G Kergon 	return (n >> SECTOR_SHIFT);
6640da336e5SAlasdair G Kergon }
6650da336e5SAlasdair G Kergon 
6660da336e5SAlasdair G Kergon static inline unsigned long to_bytes(sector_t n)
6670da336e5SAlasdair G Kergon {
6680da336e5SAlasdair G Kergon 	return (n << SECTOR_SHIFT);
6690da336e5SAlasdair G Kergon }
6700da336e5SAlasdair G Kergon 
6711da177e4SLinus Torvalds #endif	/* _LINUX_DEVICE_MAPPER_H */
672