xref: /openbmc/linux/include/linux/device-mapper.h (revision 6abc49468eeaad2dd481779906aaa29605139087)
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,
32953923c0SMike Snitzer 	DM_TYPE_NVME_BIO_BASED	 = 4,
337e0d574fSBart Van Assche };
34e83068a5SMike Snitzer 
351da177e4SLinus Torvalds typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE } 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 
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);
100e76239a3SChristoph Hellwig 
101058ce5caSAlasdair G Kergon /*
102058ce5caSAlasdair G Kergon  * These iteration functions are typically used to check (and combine)
103058ce5caSAlasdair G Kergon  * properties of underlying devices.
104058ce5caSAlasdair G Kergon  * E.g. Does at least one underlying device support flush?
105058ce5caSAlasdair G Kergon  *      Does any underlying device not support WRITE_SAME?
106058ce5caSAlasdair G Kergon  *
107058ce5caSAlasdair G Kergon  * The callout function is called once for each contiguous section of
108058ce5caSAlasdair G Kergon  * an underlying device.  State can be maintained in *data.
109058ce5caSAlasdair G Kergon  * Return non-zero to stop iterating through any further devices.
110058ce5caSAlasdair G Kergon  */
111af4874e0SMike Snitzer typedef int (*iterate_devices_callout_fn) (struct dm_target *ti,
112af4874e0SMike Snitzer 					   struct dm_dev *dev,
1135dea271bSMike Snitzer 					   sector_t start, sector_t len,
114af4874e0SMike Snitzer 					   void *data);
115af4874e0SMike Snitzer 
116058ce5caSAlasdair G Kergon /*
117058ce5caSAlasdair G Kergon  * This function must iterate through each section of device used by the
118058ce5caSAlasdair G Kergon  * target until it encounters a non-zero return code, which it then returns.
119058ce5caSAlasdair G Kergon  * Returns zero if no callout returned non-zero.
120058ce5caSAlasdair G Kergon  */
121af4874e0SMike Snitzer typedef int (*dm_iterate_devices_fn) (struct dm_target *ti,
122af4874e0SMike Snitzer 				      iterate_devices_callout_fn fn,
123af4874e0SMike Snitzer 				      void *data);
124af4874e0SMike Snitzer 
12540bea431SMike Snitzer typedef void (*dm_io_hints_fn) (struct dm_target *ti,
12640bea431SMike Snitzer 				struct queue_limits *limits);
12740bea431SMike Snitzer 
1287d76345dSKiyoshi Ueda /*
1297d76345dSKiyoshi Ueda  * Returns:
1307d76345dSKiyoshi Ueda  *    0: The target can handle the next I/O immediately.
1317d76345dSKiyoshi Ueda  *    1: The target can't handle the next I/O immediately.
1327d76345dSKiyoshi Ueda  */
1337d76345dSKiyoshi Ueda typedef int (*dm_busy_fn) (struct dm_target *ti);
1347d76345dSKiyoshi Ueda 
135545ed20eSToshi Kani /*
136545ed20eSToshi Kani  * Returns:
137545ed20eSToshi Kani  *  < 0 : error
138545ed20eSToshi Kani  * >= 0 : the number of bytes accessible at the address
139545ed20eSToshi Kani  */
140817bf402SDan Williams typedef long (*dm_dax_direct_access_fn) (struct dm_target *ti, pgoff_t pgoff,
141817bf402SDan Williams 		long nr_pages, void **kaddr, pfn_t *pfn);
142b3a9a0c3SDan Williams typedef size_t (*dm_dax_copy_iter_fn)(struct dm_target *ti, pgoff_t pgoff,
1437e026c8cSDan Williams 		void *addr, size_t bytes, struct iov_iter *i);
144cdf6cdcdSVivek Goyal typedef int (*dm_dax_zero_page_range_fn)(struct dm_target *ti, pgoff_t pgoff,
145cdf6cdcdSVivek Goyal 		size_t nr_pages);
146f26c5719SDan Williams #define PAGE_SECTORS (PAGE_SIZE / 512)
147545ed20eSToshi Kani 
1481da177e4SLinus Torvalds void dm_error(const char *message);
1491da177e4SLinus Torvalds 
15082b1519bSMikulas Patocka struct dm_dev {
15182b1519bSMikulas Patocka 	struct block_device *bdev;
152817bf402SDan Williams 	struct dax_device *dax_dev;
153aeb5d727SAl Viro 	fmode_t mode;
15482b1519bSMikulas Patocka 	char name[16];
15582b1519bSMikulas Patocka };
15682b1519bSMikulas Patocka 
1574df2bf46SDingXiang dev_t dm_get_dev_t(const char *path);
1584df2bf46SDingXiang 
1593cb40214SBryn Reeves /*
1601da177e4SLinus Torvalds  * Constructors should call these functions to ensure destination devices
1611da177e4SLinus Torvalds  * are opened/closed correctly.
1621da177e4SLinus Torvalds  */
1638215d6ecSNikanth Karthikesan int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
1648215d6ecSNikanth Karthikesan 		  struct dm_dev **result);
1651da177e4SLinus Torvalds void dm_put_device(struct dm_target *ti, struct dm_dev *d);
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds /*
1681da177e4SLinus Torvalds  * Information about a target type
1691da177e4SLinus Torvalds  */
170ab4c1424SAndi Kleen 
1711da177e4SLinus Torvalds struct target_type {
172ab4c1424SAndi Kleen 	uint64_t features;
1731da177e4SLinus Torvalds 	const char *name;
1741da177e4SLinus Torvalds 	struct module *module;
1751da177e4SLinus Torvalds 	unsigned version[3];
1761da177e4SLinus Torvalds 	dm_ctr_fn ctr;
1771da177e4SLinus Torvalds 	dm_dtr_fn dtr;
1781da177e4SLinus Torvalds 	dm_map_fn map;
179e5863d9aSMike Snitzer 	dm_clone_and_map_request_fn clone_and_map_rq;
180e5863d9aSMike Snitzer 	dm_release_clone_request_fn release_clone_rq;
1811da177e4SLinus Torvalds 	dm_endio_fn end_io;
1827d76345dSKiyoshi Ueda 	dm_request_endio_fn rq_end_io;
1831da177e4SLinus Torvalds 	dm_presuspend_fn presuspend;
184d67ee213SMike Snitzer 	dm_presuspend_undo_fn presuspend_undo;
1851da177e4SLinus Torvalds 	dm_postsuspend_fn postsuspend;
1868757b776SMilan Broz 	dm_preresume_fn preresume;
1871da177e4SLinus Torvalds 	dm_resume_fn resume;
1881da177e4SLinus Torvalds 	dm_status_fn status;
1891da177e4SLinus Torvalds 	dm_message_fn message;
190e56f81e0SChristoph Hellwig 	dm_prepare_ioctl_fn prepare_ioctl;
191e76239a3SChristoph Hellwig #ifdef CONFIG_BLK_DEV_ZONED
192e76239a3SChristoph Hellwig 	dm_report_zones_fn report_zones;
193e76239a3SChristoph Hellwig #endif
1947d76345dSKiyoshi Ueda 	dm_busy_fn busy;
195af4874e0SMike Snitzer 	dm_iterate_devices_fn iterate_devices;
19640bea431SMike Snitzer 	dm_io_hints_fn io_hints;
197817bf402SDan Williams 	dm_dax_direct_access_fn direct_access;
198b3a9a0c3SDan Williams 	dm_dax_copy_iter_fn dax_copy_from_iter;
199b3a9a0c3SDan Williams 	dm_dax_copy_iter_fn dax_copy_to_iter;
200cdf6cdcdSVivek Goyal 	dm_dax_zero_page_range_fn dax_zero_page_range;
20145194e4fSCheng Renquan 
20245194e4fSCheng Renquan 	/* For internal device-mapper use. */
20345194e4fSCheng Renquan 	struct list_head list;
2041da177e4SLinus Torvalds };
2051da177e4SLinus Torvalds 
2063791e2fcSAlasdair G Kergon /*
2073791e2fcSAlasdair G Kergon  * Target features
2083791e2fcSAlasdair G Kergon  */
2093791e2fcSAlasdair G Kergon 
2103791e2fcSAlasdair G Kergon /*
2113791e2fcSAlasdair G Kergon  * Any table that contains an instance of this target must have only one.
2123791e2fcSAlasdair G Kergon  */
2133791e2fcSAlasdair G Kergon #define DM_TARGET_SINGLETON		0x00000001
2143791e2fcSAlasdair G Kergon #define dm_target_needs_singleton(type)	((type)->features & DM_TARGET_SINGLETON)
2153791e2fcSAlasdair G Kergon 
216cc6cbe14SAlasdair G Kergon /*
217cc6cbe14SAlasdair G Kergon  * Indicates that a target does not support read-only devices.
218cc6cbe14SAlasdair G Kergon  */
219cc6cbe14SAlasdair G Kergon #define DM_TARGET_ALWAYS_WRITEABLE	0x00000002
220cc6cbe14SAlasdair G Kergon #define dm_target_always_writeable(type) \
221cc6cbe14SAlasdair G Kergon 		((type)->features & DM_TARGET_ALWAYS_WRITEABLE)
222cc6cbe14SAlasdair G Kergon 
22336a0456fSAlasdair G Kergon /*
22436a0456fSAlasdair G Kergon  * Any device that contains a table with an instance of this target may never
22536a0456fSAlasdair G Kergon  * have tables containing any different target type.
22636a0456fSAlasdair G Kergon  */
22736a0456fSAlasdair G Kergon #define DM_TARGET_IMMUTABLE		0x00000004
22836a0456fSAlasdair G Kergon #define dm_target_is_immutable(type)	((type)->features & DM_TARGET_IMMUTABLE)
22936a0456fSAlasdair G Kergon 
230b0d8ed4dSAlasdair G Kergon /*
231f083b09bSMike Snitzer  * Indicates that a target may replace any target; even immutable targets.
232f083b09bSMike Snitzer  * .map, .map_rq, .clone_and_map_rq and .release_clone_rq are all defined.
233f083b09bSMike Snitzer  */
234f083b09bSMike Snitzer #define DM_TARGET_WILDCARD		0x00000008
235f083b09bSMike Snitzer #define dm_target_is_wildcard(type)	((type)->features & DM_TARGET_WILDCARD)
236f083b09bSMike Snitzer 
237f083b09bSMike Snitzer /*
2389b4b5a79SMilan Broz  * A target implements own bio data integrity.
2399b4b5a79SMilan Broz  */
2409b4b5a79SMilan Broz #define DM_TARGET_INTEGRITY		0x00000010
2419b4b5a79SMilan Broz #define dm_target_has_integrity(type)	((type)->features & DM_TARGET_INTEGRITY)
2429b4b5a79SMilan Broz 
243e2460f2aSMikulas Patocka /*
244e2460f2aSMikulas Patocka  * A target passes integrity data to the lower device.
245e2460f2aSMikulas Patocka  */
246e2460f2aSMikulas Patocka #define DM_TARGET_PASSES_INTEGRITY	0x00000020
247e2460f2aSMikulas Patocka #define dm_target_passes_integrity(type) ((type)->features & DM_TARGET_PASSES_INTEGRITY)
248e2460f2aSMikulas Patocka 
249dd88d313SDamien Le Moal /*
250dd88d313SDamien Le Moal  * Indicates that a target supports host-managed zoned block devices.
251dd88d313SDamien Le Moal  */
252dd88d313SDamien Le Moal #define DM_TARGET_ZONED_HM		0x00000040
253dd88d313SDamien Le Moal #define dm_target_supports_zoned_hm(type) ((type)->features & DM_TARGET_ZONED_HM)
254dd88d313SDamien Le Moal 
255*6abc4946SKonstantin Khlebnikov /*
256*6abc4946SKonstantin Khlebnikov  * A target handles REQ_NOWAIT
257*6abc4946SKonstantin Khlebnikov  */
258*6abc4946SKonstantin Khlebnikov #define DM_TARGET_NOWAIT		0x00000080
259*6abc4946SKonstantin Khlebnikov #define dm_target_supports_nowait(type) ((type)->features & DM_TARGET_NOWAIT)
260*6abc4946SKonstantin Khlebnikov 
2611da177e4SLinus Torvalds struct dm_target {
2621da177e4SLinus Torvalds 	struct dm_table *table;
2631da177e4SLinus Torvalds 	struct target_type *type;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	/* target limits */
2661da177e4SLinus Torvalds 	sector_t begin;
2671da177e4SLinus Torvalds 	sector_t len;
2681da177e4SLinus Torvalds 
269542f9038SMike Snitzer 	/* If non-zero, maximum size of I/O submitted to a target. */
270542f9038SMike Snitzer 	uint32_t max_io_len;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	/*
27355a62eefSAlasdair G Kergon 	 * A number of zero-length barrier bios that will be submitted
274f9ab94ceSMikulas Patocka 	 * to the target for the purpose of flushing cache.
275f9ab94ceSMikulas Patocka 	 *
27655a62eefSAlasdair G Kergon 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
27755a62eefSAlasdair G Kergon 	 * It is a responsibility of the target driver to remap these bios
278f9ab94ceSMikulas Patocka 	 * to the real underlying devices.
279f9ab94ceSMikulas Patocka 	 */
28055a62eefSAlasdair G Kergon 	unsigned num_flush_bios;
281f9ab94ceSMikulas Patocka 
2825ae89a87SMike Snitzer 	/*
28355a62eefSAlasdair G Kergon 	 * The number of discard bios that will be submitted to the target.
28455a62eefSAlasdair G Kergon 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
2855ae89a87SMike Snitzer 	 */
28655a62eefSAlasdair G Kergon 	unsigned num_discard_bios;
2875ae89a87SMike Snitzer 
288d54eaa5aSMike Snitzer 	/*
28900716545SDenis Semakin 	 * The number of secure erase bios that will be submitted to the target.
29000716545SDenis Semakin 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
29100716545SDenis Semakin 	 */
29200716545SDenis Semakin 	unsigned num_secure_erase_bios;
29300716545SDenis Semakin 
29400716545SDenis Semakin 	/*
29555a62eefSAlasdair G Kergon 	 * The number of WRITE SAME bios that will be submitted to the target.
29655a62eefSAlasdair G Kergon 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
297d54eaa5aSMike Snitzer 	 */
29855a62eefSAlasdair G Kergon 	unsigned num_write_same_bios;
299d54eaa5aSMike Snitzer 
300c0820cf5SMikulas Patocka 	/*
301ac62d620SChristoph Hellwig 	 * The number of WRITE ZEROES bios that will be submitted to the target.
302ac62d620SChristoph Hellwig 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
303ac62d620SChristoph Hellwig 	 */
304ac62d620SChristoph Hellwig 	unsigned num_write_zeroes_bios;
305ac62d620SChristoph Hellwig 
306ac62d620SChristoph Hellwig 	/*
30730187e1dSMike Snitzer 	 * The minimum number of extra bytes allocated in each io for the
30830187e1dSMike Snitzer 	 * target to use.
309c0820cf5SMikulas Patocka 	 */
31030187e1dSMike Snitzer 	unsigned per_io_data_size;
311c0820cf5SMikulas Patocka 
3121da177e4SLinus Torvalds 	/* target specific data */
3131da177e4SLinus Torvalds 	void *private;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	/* Used to provide an error string from the ctr */
3161da177e4SLinus Torvalds 	char *error;
3174c259327SMike Snitzer 
3184c259327SMike Snitzer 	/*
3190e9c24edSJoe Thornber 	 * Set if this target needs to receive flushes regardless of
3200e9c24edSJoe Thornber 	 * whether or not its underlying devices have support.
3210e9c24edSJoe Thornber 	 */
3220e9c24edSJoe Thornber 	bool flush_supported:1;
3230e9c24edSJoe Thornber 
3240e9c24edSJoe Thornber 	/*
3254c259327SMike Snitzer 	 * Set if this target needs to receive discards regardless of
3264c259327SMike Snitzer 	 * whether or not its underlying devices have support.
3274c259327SMike Snitzer 	 */
3280ac55489SAlasdair G Kergon 	bool discards_supported:1;
3291da177e4SLinus Torvalds };
3301da177e4SLinus Torvalds 
33164f52b0eSMike Snitzer void *dm_per_bio_data(struct bio *bio, size_t data_size);
33264f52b0eSMike Snitzer struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size);
33364f52b0eSMike Snitzer unsigned dm_bio_get_target_bio_nr(const struct bio *bio);
334ddbd658fSMikulas Patocka 
335087615bfSGabriel Krisman Bertazi u64 dm_start_time_ns_from_clone(struct bio *bio);
336087615bfSGabriel Krisman Bertazi 
3371da177e4SLinus Torvalds int dm_register_target(struct target_type *t);
33810d3bd09SMikulas Patocka void dm_unregister_target(struct target_type *t);
33917b2f66fSAlasdair G Kergon 
340498f0103SMike Snitzer /*
341498f0103SMike Snitzer  * Target argument parsing.
342498f0103SMike Snitzer  */
343498f0103SMike Snitzer struct dm_arg_set {
344498f0103SMike Snitzer 	unsigned argc;
345498f0103SMike Snitzer 	char **argv;
346498f0103SMike Snitzer };
347498f0103SMike Snitzer 
348498f0103SMike Snitzer /*
349498f0103SMike Snitzer  * The minimum and maximum value of a numeric argument, together with
350498f0103SMike Snitzer  * the error message to use if the number is found to be outside that range.
351498f0103SMike Snitzer  */
352498f0103SMike Snitzer struct dm_arg {
353498f0103SMike Snitzer 	unsigned min;
354498f0103SMike Snitzer 	unsigned max;
355498f0103SMike Snitzer 	char *error;
356498f0103SMike Snitzer };
357498f0103SMike Snitzer 
358498f0103SMike Snitzer /*
359498f0103SMike Snitzer  * Validate the next argument, either returning it as *value or, if invalid,
360498f0103SMike Snitzer  * returning -EINVAL and setting *error.
361498f0103SMike Snitzer  */
3625916a22bSEric Biggers int dm_read_arg(const struct dm_arg *arg, struct dm_arg_set *arg_set,
363498f0103SMike Snitzer 		unsigned *value, char **error);
364498f0103SMike Snitzer 
365498f0103SMike Snitzer /*
366498f0103SMike Snitzer  * Process the next argument as the start of a group containing between
367498f0103SMike Snitzer  * arg->min and arg->max further arguments. Either return the size as
368498f0103SMike Snitzer  * *num_args or, if invalid, return -EINVAL and set *error.
369498f0103SMike Snitzer  */
3705916a22bSEric Biggers int dm_read_arg_group(const struct dm_arg *arg, struct dm_arg_set *arg_set,
371498f0103SMike Snitzer 		      unsigned *num_args, char **error);
372498f0103SMike Snitzer 
373498f0103SMike Snitzer /*
374498f0103SMike Snitzer  * Return the current argument and shift to the next.
375498f0103SMike Snitzer  */
376498f0103SMike Snitzer const char *dm_shift_arg(struct dm_arg_set *as);
377498f0103SMike Snitzer 
378498f0103SMike Snitzer /*
379498f0103SMike Snitzer  * Move through num_args arguments.
380498f0103SMike Snitzer  */
381498f0103SMike Snitzer void dm_consume_args(struct dm_arg_set *as, unsigned num_args);
382498f0103SMike Snitzer 
38317b2f66fSAlasdair G Kergon /*-----------------------------------------------------------------
38417b2f66fSAlasdair G Kergon  * Functions for creating and manipulating mapped devices.
38517b2f66fSAlasdair G Kergon  * Drop the reference with dm_put when you finish with the object.
38617b2f66fSAlasdair G Kergon  *---------------------------------------------------------------*/
38717b2f66fSAlasdair G Kergon 
38817b2f66fSAlasdair G Kergon /*
38917b2f66fSAlasdair G Kergon  * DM_ANY_MINOR chooses the next available minor number.
39017b2f66fSAlasdair G Kergon  */
39117b2f66fSAlasdair G Kergon #define DM_ANY_MINOR (-1)
39217b2f66fSAlasdair G Kergon int dm_create(int minor, struct mapped_device **md);
39317b2f66fSAlasdair G Kergon 
39417b2f66fSAlasdair G Kergon /*
39517b2f66fSAlasdair G Kergon  * Reference counting for md.
39617b2f66fSAlasdair G Kergon  */
39717b2f66fSAlasdair G Kergon struct mapped_device *dm_get_md(dev_t dev);
39817b2f66fSAlasdair G Kergon void dm_get(struct mapped_device *md);
39909ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md);
40017b2f66fSAlasdair G Kergon void dm_put(struct mapped_device *md);
40117b2f66fSAlasdair G Kergon 
40217b2f66fSAlasdair G Kergon /*
40317b2f66fSAlasdair G Kergon  * An arbitrary pointer may be stored alongside a mapped device.
40417b2f66fSAlasdair G Kergon  */
40517b2f66fSAlasdair G Kergon void dm_set_mdptr(struct mapped_device *md, void *ptr);
40617b2f66fSAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md);
40717b2f66fSAlasdair G Kergon 
40817b2f66fSAlasdair G Kergon /*
40917b2f66fSAlasdair G Kergon  * A device can still be used while suspended, but I/O is deferred.
41017b2f66fSAlasdair G Kergon  */
411a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags);
41217b2f66fSAlasdair G Kergon int dm_resume(struct mapped_device *md);
41317b2f66fSAlasdair G Kergon 
41417b2f66fSAlasdair G Kergon /*
41517b2f66fSAlasdair G Kergon  * Event functions.
41617b2f66fSAlasdair G Kergon  */
41717b2f66fSAlasdair G Kergon uint32_t dm_get_event_nr(struct mapped_device *md);
41817b2f66fSAlasdair G Kergon int dm_wait_event(struct mapped_device *md, int event_nr);
4197a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md);
4207a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist);
42117b2f66fSAlasdair G Kergon 
42217b2f66fSAlasdair G Kergon /*
42317b2f66fSAlasdair G Kergon  * Info functions.
42417b2f66fSAlasdair G Kergon  */
42572d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md);
42696a1f7dbSMike Anderson int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid);
42717b2f66fSAlasdair G Kergon struct gendisk *dm_disk(struct mapped_device *md);
42864dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti);
4295df96f2bSMikulas Patocka int dm_post_suspending(struct dm_target *ti);
4302e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti);
4311dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors);
432cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq);
43317b2f66fSAlasdair G Kergon 
434d4100351SChristoph Hellwig #ifdef CONFIG_BLK_DEV_ZONED
435d4100351SChristoph Hellwig struct dm_report_zones_args {
436d4100351SChristoph Hellwig 	struct dm_target *tgt;
437d4100351SChristoph Hellwig 	sector_t next_sector;
438d4100351SChristoph Hellwig 
439d4100351SChristoph Hellwig 	void *orig_data;
440d4100351SChristoph Hellwig 	report_zones_cb orig_cb;
441d4100351SChristoph Hellwig 	unsigned int zone_idx;
442d4100351SChristoph Hellwig 
443d4100351SChristoph Hellwig 	/* must be filled by ->report_zones before calling dm_report_zones_cb */
444d4100351SChristoph Hellwig 	sector_t start;
445d4100351SChristoph Hellwig };
446d4100351SChristoph Hellwig int dm_report_zones_cb(struct blk_zone *zone, unsigned int idx, void *data);
447d4100351SChristoph Hellwig #endif /* CONFIG_BLK_DEV_ZONED */
448d4100351SChristoph Hellwig 
4496bbc923dSHelen Koike /*
4506bbc923dSHelen Koike  * Device mapper functions to parse and create devices specified by the
4516bbc923dSHelen Koike  * parameter "dm-mod.create="
4526bbc923dSHelen Koike  */
4536bbc923dSHelen Koike int __init dm_early_create(struct dm_ioctl *dmi,
4546bbc923dSHelen Koike 			   struct dm_target_spec **spec_array,
4556bbc923dSHelen Koike 			   char **target_params_array);
4566bbc923dSHelen Koike 
457f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md);
458f84cb8a4SMike Snitzer 
45917b2f66fSAlasdair G Kergon /*
46017b2f66fSAlasdair G Kergon  * Geometry functions.
46117b2f66fSAlasdair G Kergon  */
46217b2f66fSAlasdair G Kergon int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo);
46317b2f66fSAlasdair G Kergon int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo);
46417b2f66fSAlasdair G Kergon 
46517b2f66fSAlasdair G Kergon /*-----------------------------------------------------------------
46617b2f66fSAlasdair G Kergon  * Functions for manipulating device-mapper tables.
46717b2f66fSAlasdair G Kergon  *---------------------------------------------------------------*/
46817b2f66fSAlasdair G Kergon 
46917b2f66fSAlasdair G Kergon /*
47017b2f66fSAlasdair G Kergon  * First create an empty table.
47117b2f66fSAlasdair G Kergon  */
472aeb5d727SAl Viro int dm_table_create(struct dm_table **result, fmode_t mode,
47317b2f66fSAlasdair G Kergon 		    unsigned num_targets, struct mapped_device *md);
47417b2f66fSAlasdair G Kergon 
47517b2f66fSAlasdair G Kergon /*
47617b2f66fSAlasdair G Kergon  * Then call this once for each target.
47717b2f66fSAlasdair G Kergon  */
47817b2f66fSAlasdair G Kergon int dm_table_add_target(struct dm_table *t, const char *type,
47917b2f66fSAlasdair G Kergon 			sector_t start, sector_t len, char *params);
48017b2f66fSAlasdair G Kergon 
48117b2f66fSAlasdair G Kergon /*
482e83068a5SMike Snitzer  * Target can use this to set the table's type.
483e83068a5SMike Snitzer  * Can only ever be called from a target's ctr.
484e83068a5SMike Snitzer  * Useful for "hybrid" target (supports both bio-based
485e83068a5SMike Snitzer  * and request-based).
486e83068a5SMike Snitzer  */
4877e0d574fSBart Van Assche void dm_table_set_type(struct dm_table *t, enum dm_queue_mode type);
488e83068a5SMike Snitzer 
489e83068a5SMike Snitzer /*
49017b2f66fSAlasdair G Kergon  * Finally call this to make the table ready for use.
49117b2f66fSAlasdair G Kergon  */
49217b2f66fSAlasdair G Kergon int dm_table_complete(struct dm_table *t);
49317b2f66fSAlasdair G Kergon 
49417b2f66fSAlasdair G Kergon /*
495f6e7baadSBrian Norris  * Destroy the table when finished.
496f6e7baadSBrian Norris  */
497f6e7baadSBrian Norris void dm_table_destroy(struct dm_table *t);
498f6e7baadSBrian Norris 
499f6e7baadSBrian Norris /*
500542f9038SMike Snitzer  * Target may require that it is never sent I/O larger than len.
501542f9038SMike Snitzer  */
502542f9038SMike Snitzer int __must_check dm_set_target_max_io_len(struct dm_target *ti, sector_t len);
503542f9038SMike Snitzer 
504542f9038SMike Snitzer /*
50517b2f66fSAlasdair G Kergon  * Table reference counting.
50617b2f66fSAlasdair G Kergon  */
50783d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx);
50883d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx);
50983d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md);
51017b2f66fSAlasdair G Kergon 
51117b2f66fSAlasdair G Kergon /*
51217b2f66fSAlasdair G Kergon  * Queries
51317b2f66fSAlasdair G Kergon  */
51417b2f66fSAlasdair G Kergon sector_t dm_table_get_size(struct dm_table *t);
51517b2f66fSAlasdair G Kergon unsigned int dm_table_get_num_targets(struct dm_table *t);
516aeb5d727SAl Viro fmode_t dm_table_get_mode(struct dm_table *t);
51717b2f66fSAlasdair G Kergon struct mapped_device *dm_table_get_md(struct dm_table *t);
518f349b0a3SMichał Mirosław const char *dm_table_device_name(struct dm_table *t);
51917b2f66fSAlasdair G Kergon 
52017b2f66fSAlasdair G Kergon /*
52117b2f66fSAlasdair G Kergon  * Trigger an event.
52217b2f66fSAlasdair G Kergon  */
52317b2f66fSAlasdair G Kergon void dm_table_event(struct dm_table *t);
52417b2f66fSAlasdair G Kergon 
52517b2f66fSAlasdair G Kergon /*
5269974fa2cSMike Snitzer  * Run the queue for request-based targets.
5279974fa2cSMike Snitzer  */
5289974fa2cSMike Snitzer void dm_table_run_md_queue_async(struct dm_table *t);
5299974fa2cSMike Snitzer 
5309974fa2cSMike Snitzer /*
53117b2f66fSAlasdair G Kergon  * The device must be suspended before calling this method.
532042d2a9bSAlasdair G Kergon  * Returns the previous table, which the caller must destroy.
53317b2f66fSAlasdair G Kergon  */
534042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md,
535042d2a9bSAlasdair G Kergon 			       struct dm_table *t);
53617b2f66fSAlasdair G Kergon 
53754160904SMikulas Patocka /*
53854160904SMikulas Patocka  * A wrapper around vmalloc.
53954160904SMikulas Patocka  */
54054160904SMikulas Patocka void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size);
54154160904SMikulas Patocka 
5420da336e5SAlasdair G Kergon /*-----------------------------------------------------------------
5430da336e5SAlasdair G Kergon  * Macros.
5440da336e5SAlasdair G Kergon  *---------------------------------------------------------------*/
5450da336e5SAlasdair G Kergon #define DM_NAME "device-mapper"
5460da336e5SAlasdair G Kergon 
547d2c3c8dcSJoe Perches #define DM_FMT(fmt) DM_NAME ": " DM_MSG_PREFIX ": " fmt "\n"
54810d3bd09SMikulas Patocka 
549d2c3c8dcSJoe Perches #define DMCRIT(fmt, ...) pr_crit(DM_FMT(fmt), ##__VA_ARGS__)
550d2c3c8dcSJoe Perches 
551d2c3c8dcSJoe Perches #define DMERR(fmt, ...) pr_err(DM_FMT(fmt), ##__VA_ARGS__)
552733232f8SMike Snitzer #define DMERR_LIMIT(fmt, ...) pr_err_ratelimited(DM_FMT(fmt), ##__VA_ARGS__)
553d2c3c8dcSJoe Perches #define DMWARN(fmt, ...) pr_warn(DM_FMT(fmt), ##__VA_ARGS__)
554733232f8SMike Snitzer #define DMWARN_LIMIT(fmt, ...) pr_warn_ratelimited(DM_FMT(fmt), ##__VA_ARGS__)
555d2c3c8dcSJoe Perches #define DMINFO(fmt, ...) pr_info(DM_FMT(fmt), ##__VA_ARGS__)
556733232f8SMike Snitzer #define DMINFO_LIMIT(fmt, ...) pr_info_ratelimited(DM_FMT(fmt), ##__VA_ARGS__)
5570da336e5SAlasdair G Kergon 
55874244b59SHannes Reinecke #define DMDEBUG(fmt, ...) pr_debug(DM_FMT(fmt), ##__VA_ARGS__)
559733232f8SMike Snitzer #define DMDEBUG_LIMIT(fmt, ...) pr_debug_ratelimited(DM_FMT(fmt), ##__VA_ARGS__)
5600da336e5SAlasdair G Kergon 
5610da336e5SAlasdair G Kergon #define DMEMIT(x...) sz += ((sz >= maxlen) ? \
5620da336e5SAlasdair G Kergon 			  0 : scnprintf(result + sz, maxlen - sz, x))
5630da336e5SAlasdair G Kergon 
5640da336e5SAlasdair G Kergon /*
5650da336e5SAlasdair G Kergon  * Definitions of return values from target end_io function.
5660da336e5SAlasdair G Kergon  */
5677ed8578aSChristoph Hellwig #define DM_ENDIO_DONE		0
5680da336e5SAlasdair G Kergon #define DM_ENDIO_INCOMPLETE	1
5690da336e5SAlasdair G Kergon #define DM_ENDIO_REQUEUE	2
570ac514ffcSMike Snitzer #define DM_ENDIO_DELAY_REQUEUE	3
5710da336e5SAlasdair G Kergon 
5720da336e5SAlasdair G Kergon /*
5730da336e5SAlasdair G Kergon  * Definitions of return values from target map function.
5740da336e5SAlasdair G Kergon  */
5750da336e5SAlasdair G Kergon #define DM_MAPIO_SUBMITTED	0
5760da336e5SAlasdair G Kergon #define DM_MAPIO_REMAPPED	1
5770da336e5SAlasdair G Kergon #define DM_MAPIO_REQUEUE	DM_ENDIO_REQUEUE
578ac514ffcSMike Snitzer #define DM_MAPIO_DELAY_REQUEUE	DM_ENDIO_DELAY_REQUEUE
579412445acSChristoph Hellwig #define DM_MAPIO_KILL		4
5800da336e5SAlasdair G Kergon 
581fd2ed4d2SMikulas Patocka #define dm_sector_div64(x, y)( \
582fd2ed4d2SMikulas Patocka { \
583fd2ed4d2SMikulas Patocka 	u64 _res; \
584fd2ed4d2SMikulas Patocka 	(x) = div64_u64_rem(x, y, &_res); \
585fd2ed4d2SMikulas Patocka 	_res; \
586fd2ed4d2SMikulas Patocka } \
587fd2ed4d2SMikulas Patocka )
588fd2ed4d2SMikulas Patocka 
5890da336e5SAlasdair G Kergon /*
5900da336e5SAlasdair G Kergon  * Ceiling(n / sz)
5910da336e5SAlasdair G Kergon  */
5920da336e5SAlasdair G Kergon #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz))
5930da336e5SAlasdair G Kergon 
5940da336e5SAlasdair G Kergon #define dm_sector_div_up(n, sz) ( \
5950da336e5SAlasdair G Kergon { \
5960da336e5SAlasdair G Kergon 	sector_t _r = ((n) + (sz) - 1); \
5970da336e5SAlasdair G Kergon 	sector_div(_r, (sz)); \
5980da336e5SAlasdair G Kergon 	_r; \
5990da336e5SAlasdair G Kergon } \
6000da336e5SAlasdair G Kergon )
6010da336e5SAlasdair G Kergon 
6020da336e5SAlasdair G Kergon /*
6030da336e5SAlasdair G Kergon  * ceiling(n / size) * size
6040da336e5SAlasdair G Kergon  */
6050da336e5SAlasdair G Kergon #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
6060da336e5SAlasdair G Kergon 
60756a67df7SMike Snitzer /*
60856a67df7SMike Snitzer  * Sector offset taken relative to the start of the target instead of
60956a67df7SMike Snitzer  * relative to the start of the device.
61056a67df7SMike Snitzer  */
61156a67df7SMike Snitzer #define dm_target_offset(ti, sector) ((sector) - (ti)->begin)
61256a67df7SMike Snitzer 
6130bdb50c5SNeilBrown static inline sector_t to_sector(unsigned long long n)
6140da336e5SAlasdair G Kergon {
6150da336e5SAlasdair G Kergon 	return (n >> SECTOR_SHIFT);
6160da336e5SAlasdair G Kergon }
6170da336e5SAlasdair G Kergon 
6180da336e5SAlasdair G Kergon static inline unsigned long to_bytes(sector_t n)
6190da336e5SAlasdair G Kergon {
6200da336e5SAlasdair G Kergon 	return (n << SECTOR_SHIFT);
6210da336e5SAlasdair G Kergon }
6220da336e5SAlasdair G Kergon 
6231da177e4SLinus Torvalds #endif	/* _LINUX_DEVICE_MAPPER_H */
624