xref: /openbmc/linux/include/linux/device-mapper.h (revision 00716545c894fc464e00612809d9cb836b180c99)
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>
13fd2ed4d2SMikulas Patocka #include <linux/math64.h>
1471a16736SNamhyung Kim #include <linux/ratelimit.h>
15416cd17bSHeinz Mauelshagen 
16af4874e0SMike Snitzer struct dm_dev;
171da177e4SLinus Torvalds struct dm_target;
181da177e4SLinus Torvalds struct dm_table;
1917b2f66fSAlasdair G Kergon struct mapped_device;
20f6fccb12SMilan Broz struct bio_vec;
211da177e4SLinus Torvalds 
22e83068a5SMike Snitzer /*
23e83068a5SMike Snitzer  * Type of table, mapped_device's mempool and request_queue
24e83068a5SMike Snitzer  */
257e0d574fSBart Van Assche enum dm_queue_mode {
267e0d574fSBart Van Assche 	DM_TYPE_NONE		 = 0,
277e0d574fSBart Van Assche 	DM_TYPE_BIO_BASED	 = 1,
287e0d574fSBart Van Assche 	DM_TYPE_REQUEST_BASED	 = 2,
297e0d574fSBart Van Assche 	DM_TYPE_MQ_REQUEST_BASED = 3,
307e0d574fSBart Van Assche 	DM_TYPE_DAX_BIO_BASED	 = 4,
3122c11858SMike Snitzer 	DM_TYPE_NVME_BIO_BASED	 = 5,
327e0d574fSBart Van Assche };
33e83068a5SMike Snitzer 
341da177e4SLinus Torvalds typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE } 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);
65e5863d9aSMike Snitzer typedef void (*dm_release_clone_request_fn) (struct request *clone);
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /*
681da177e4SLinus Torvalds  * Returns:
691da177e4SLinus Torvalds  * < 0 : error (currently ignored)
701da177e4SLinus Torvalds  * 0   : ended successfully
711da177e4SLinus Torvalds  * 1   : for some reason the io has still not completed (eg,
721da177e4SLinus Torvalds  *       multipath target might want to requeue a failed io).
732e93ccc1SKiyoshi Ueda  * 2   : The target wants to push back the io
741da177e4SLinus Torvalds  */
751da177e4SLinus Torvalds typedef int (*dm_endio_fn) (struct dm_target *ti,
764e4cbee9SChristoph Hellwig 			    struct bio *bio, blk_status_t *error);
777d76345dSKiyoshi Ueda typedef int (*dm_request_endio_fn) (struct dm_target *ti,
782a842acaSChristoph Hellwig 				    struct request *clone, blk_status_t error,
797d76345dSKiyoshi Ueda 				    union map_info *map_context);
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds typedef void (*dm_presuspend_fn) (struct dm_target *ti);
82d67ee213SMike Snitzer typedef void (*dm_presuspend_undo_fn) (struct dm_target *ti);
831da177e4SLinus Torvalds typedef void (*dm_postsuspend_fn) (struct dm_target *ti);
848757b776SMilan Broz typedef int (*dm_preresume_fn) (struct dm_target *ti);
851da177e4SLinus Torvalds typedef void (*dm_resume_fn) (struct dm_target *ti);
861da177e4SLinus Torvalds 
87fd7c092eSMikulas Patocka typedef void (*dm_status_fn) (struct dm_target *ti, status_type_t status_type,
881f4e0ff0SAlasdair G Kergon 			      unsigned status_flags, char *result, unsigned maxlen);
891da177e4SLinus Torvalds 
901eb5fa84SMike Snitzer typedef int (*dm_message_fn) (struct dm_target *ti, unsigned argc, char **argv,
911eb5fa84SMike Snitzer 			      char *result, unsigned maxlen);
921da177e4SLinus Torvalds 
93e56f81e0SChristoph Hellwig typedef int (*dm_prepare_ioctl_fn) (struct dm_target *ti,
94e56f81e0SChristoph Hellwig 			    struct block_device **bdev, fmode_t *mode);
95aa129a22SMilan Broz 
96058ce5caSAlasdair G Kergon /*
97058ce5caSAlasdair G Kergon  * These iteration functions are typically used to check (and combine)
98058ce5caSAlasdair G Kergon  * properties of underlying devices.
99058ce5caSAlasdair G Kergon  * E.g. Does at least one underlying device support flush?
100058ce5caSAlasdair G Kergon  *      Does any underlying device not support WRITE_SAME?
101058ce5caSAlasdair G Kergon  *
102058ce5caSAlasdair G Kergon  * The callout function is called once for each contiguous section of
103058ce5caSAlasdair G Kergon  * an underlying device.  State can be maintained in *data.
104058ce5caSAlasdair G Kergon  * Return non-zero to stop iterating through any further devices.
105058ce5caSAlasdair G Kergon  */
106af4874e0SMike Snitzer typedef int (*iterate_devices_callout_fn) (struct dm_target *ti,
107af4874e0SMike Snitzer 					   struct dm_dev *dev,
1085dea271bSMike Snitzer 					   sector_t start, sector_t len,
109af4874e0SMike Snitzer 					   void *data);
110af4874e0SMike Snitzer 
111058ce5caSAlasdair G Kergon /*
112058ce5caSAlasdair G Kergon  * This function must iterate through each section of device used by the
113058ce5caSAlasdair G Kergon  * target until it encounters a non-zero return code, which it then returns.
114058ce5caSAlasdair G Kergon  * Returns zero if no callout returned non-zero.
115058ce5caSAlasdair G Kergon  */
116af4874e0SMike Snitzer typedef int (*dm_iterate_devices_fn) (struct dm_target *ti,
117af4874e0SMike Snitzer 				      iterate_devices_callout_fn fn,
118af4874e0SMike Snitzer 				      void *data);
119af4874e0SMike Snitzer 
12040bea431SMike Snitzer typedef void (*dm_io_hints_fn) (struct dm_target *ti,
12140bea431SMike Snitzer 				struct queue_limits *limits);
12240bea431SMike Snitzer 
1237d76345dSKiyoshi Ueda /*
1247d76345dSKiyoshi Ueda  * Returns:
1257d76345dSKiyoshi Ueda  *    0: The target can handle the next I/O immediately.
1267d76345dSKiyoshi Ueda  *    1: The target can't handle the next I/O immediately.
1277d76345dSKiyoshi Ueda  */
1287d76345dSKiyoshi Ueda typedef int (*dm_busy_fn) (struct dm_target *ti);
1297d76345dSKiyoshi Ueda 
130545ed20eSToshi Kani /*
131545ed20eSToshi Kani  * Returns:
132545ed20eSToshi Kani  *  < 0 : error
133545ed20eSToshi Kani  * >= 0 : the number of bytes accessible at the address
134545ed20eSToshi Kani  */
135817bf402SDan Williams typedef long (*dm_dax_direct_access_fn) (struct dm_target *ti, pgoff_t pgoff,
136817bf402SDan Williams 		long nr_pages, void **kaddr, pfn_t *pfn);
1377e026c8cSDan Williams typedef size_t (*dm_dax_copy_from_iter_fn)(struct dm_target *ti, pgoff_t pgoff,
1387e026c8cSDan Williams 		void *addr, size_t bytes, struct iov_iter *i);
139f26c5719SDan Williams #define PAGE_SECTORS (PAGE_SIZE / 512)
140545ed20eSToshi Kani 
1411da177e4SLinus Torvalds void dm_error(const char *message);
1421da177e4SLinus Torvalds 
14382b1519bSMikulas Patocka struct dm_dev {
14482b1519bSMikulas Patocka 	struct block_device *bdev;
145817bf402SDan Williams 	struct dax_device *dax_dev;
146aeb5d727SAl Viro 	fmode_t mode;
14782b1519bSMikulas Patocka 	char name[16];
14882b1519bSMikulas Patocka };
14982b1519bSMikulas Patocka 
1504df2bf46SDingXiang dev_t dm_get_dev_t(const char *path);
1514df2bf46SDingXiang 
1523cb40214SBryn Reeves /*
1531da177e4SLinus Torvalds  * Constructors should call these functions to ensure destination devices
1541da177e4SLinus Torvalds  * are opened/closed correctly.
1551da177e4SLinus Torvalds  */
1568215d6ecSNikanth Karthikesan int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
1578215d6ecSNikanth Karthikesan 		  struct dm_dev **result);
1581da177e4SLinus Torvalds void dm_put_device(struct dm_target *ti, struct dm_dev *d);
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds /*
1611da177e4SLinus Torvalds  * Information about a target type
1621da177e4SLinus Torvalds  */
163ab4c1424SAndi Kleen 
1641da177e4SLinus Torvalds struct target_type {
165ab4c1424SAndi Kleen 	uint64_t features;
1661da177e4SLinus Torvalds 	const char *name;
1671da177e4SLinus Torvalds 	struct module *module;
1681da177e4SLinus Torvalds 	unsigned version[3];
1691da177e4SLinus Torvalds 	dm_ctr_fn ctr;
1701da177e4SLinus Torvalds 	dm_dtr_fn dtr;
1711da177e4SLinus Torvalds 	dm_map_fn map;
172e5863d9aSMike Snitzer 	dm_clone_and_map_request_fn clone_and_map_rq;
173e5863d9aSMike Snitzer 	dm_release_clone_request_fn release_clone_rq;
1741da177e4SLinus Torvalds 	dm_endio_fn end_io;
1757d76345dSKiyoshi Ueda 	dm_request_endio_fn rq_end_io;
1761da177e4SLinus Torvalds 	dm_presuspend_fn presuspend;
177d67ee213SMike Snitzer 	dm_presuspend_undo_fn presuspend_undo;
1781da177e4SLinus Torvalds 	dm_postsuspend_fn postsuspend;
1798757b776SMilan Broz 	dm_preresume_fn preresume;
1801da177e4SLinus Torvalds 	dm_resume_fn resume;
1811da177e4SLinus Torvalds 	dm_status_fn status;
1821da177e4SLinus Torvalds 	dm_message_fn message;
183e56f81e0SChristoph Hellwig 	dm_prepare_ioctl_fn prepare_ioctl;
1847d76345dSKiyoshi Ueda 	dm_busy_fn busy;
185af4874e0SMike Snitzer 	dm_iterate_devices_fn iterate_devices;
18640bea431SMike Snitzer 	dm_io_hints_fn io_hints;
187817bf402SDan Williams 	dm_dax_direct_access_fn direct_access;
1887e026c8cSDan Williams 	dm_dax_copy_from_iter_fn dax_copy_from_iter;
18945194e4fSCheng Renquan 
19045194e4fSCheng Renquan 	/* For internal device-mapper use. */
19145194e4fSCheng Renquan 	struct list_head list;
1921da177e4SLinus Torvalds };
1931da177e4SLinus Torvalds 
1943791e2fcSAlasdair G Kergon /*
1953791e2fcSAlasdair G Kergon  * Target features
1963791e2fcSAlasdair G Kergon  */
1973791e2fcSAlasdair G Kergon 
1983791e2fcSAlasdair G Kergon /*
1993791e2fcSAlasdair G Kergon  * Any table that contains an instance of this target must have only one.
2003791e2fcSAlasdair G Kergon  */
2013791e2fcSAlasdair G Kergon #define DM_TARGET_SINGLETON		0x00000001
2023791e2fcSAlasdair G Kergon #define dm_target_needs_singleton(type)	((type)->features & DM_TARGET_SINGLETON)
2033791e2fcSAlasdair G Kergon 
204cc6cbe14SAlasdair G Kergon /*
205cc6cbe14SAlasdair G Kergon  * Indicates that a target does not support read-only devices.
206cc6cbe14SAlasdair G Kergon  */
207cc6cbe14SAlasdair G Kergon #define DM_TARGET_ALWAYS_WRITEABLE	0x00000002
208cc6cbe14SAlasdair G Kergon #define dm_target_always_writeable(type) \
209cc6cbe14SAlasdair G Kergon 		((type)->features & DM_TARGET_ALWAYS_WRITEABLE)
210cc6cbe14SAlasdair G Kergon 
21136a0456fSAlasdair G Kergon /*
21236a0456fSAlasdair G Kergon  * Any device that contains a table with an instance of this target may never
21336a0456fSAlasdair G Kergon  * have tables containing any different target type.
21436a0456fSAlasdair G Kergon  */
21536a0456fSAlasdair G Kergon #define DM_TARGET_IMMUTABLE		0x00000004
21636a0456fSAlasdair G Kergon #define dm_target_is_immutable(type)	((type)->features & DM_TARGET_IMMUTABLE)
21736a0456fSAlasdair G Kergon 
218b0d8ed4dSAlasdair G Kergon /*
219f083b09bSMike Snitzer  * Indicates that a target may replace any target; even immutable targets.
220f083b09bSMike Snitzer  * .map, .map_rq, .clone_and_map_rq and .release_clone_rq are all defined.
221f083b09bSMike Snitzer  */
222f083b09bSMike Snitzer #define DM_TARGET_WILDCARD		0x00000008
223f083b09bSMike Snitzer #define dm_target_is_wildcard(type)	((type)->features & DM_TARGET_WILDCARD)
224f083b09bSMike Snitzer 
225f083b09bSMike Snitzer /*
2269b4b5a79SMilan Broz  * A target implements own bio data integrity.
2279b4b5a79SMilan Broz  */
2289b4b5a79SMilan Broz #define DM_TARGET_INTEGRITY		0x00000010
2299b4b5a79SMilan Broz #define dm_target_has_integrity(type)	((type)->features & DM_TARGET_INTEGRITY)
2309b4b5a79SMilan Broz 
231e2460f2aSMikulas Patocka /*
232e2460f2aSMikulas Patocka  * A target passes integrity data to the lower device.
233e2460f2aSMikulas Patocka  */
234e2460f2aSMikulas Patocka #define DM_TARGET_PASSES_INTEGRITY	0x00000020
235e2460f2aSMikulas Patocka #define dm_target_passes_integrity(type) ((type)->features & DM_TARGET_PASSES_INTEGRITY)
236e2460f2aSMikulas Patocka 
237dd88d313SDamien Le Moal /*
238dd88d313SDamien Le Moal  * Indicates that a target supports host-managed zoned block devices.
239dd88d313SDamien Le Moal  */
240dd88d313SDamien Le Moal #define DM_TARGET_ZONED_HM		0x00000040
241dd88d313SDamien Le Moal #define dm_target_supports_zoned_hm(type) ((type)->features & DM_TARGET_ZONED_HM)
242dd88d313SDamien Le Moal 
2431da177e4SLinus Torvalds struct dm_target {
2441da177e4SLinus Torvalds 	struct dm_table *table;
2451da177e4SLinus Torvalds 	struct target_type *type;
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	/* target limits */
2481da177e4SLinus Torvalds 	sector_t begin;
2491da177e4SLinus Torvalds 	sector_t len;
2501da177e4SLinus Torvalds 
251542f9038SMike Snitzer 	/* If non-zero, maximum size of I/O submitted to a target. */
252542f9038SMike Snitzer 	uint32_t max_io_len;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	/*
25555a62eefSAlasdair G Kergon 	 * A number of zero-length barrier bios that will be submitted
256f9ab94ceSMikulas Patocka 	 * to the target for the purpose of flushing cache.
257f9ab94ceSMikulas Patocka 	 *
25855a62eefSAlasdair G Kergon 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
25955a62eefSAlasdair G Kergon 	 * It is a responsibility of the target driver to remap these bios
260f9ab94ceSMikulas Patocka 	 * to the real underlying devices.
261f9ab94ceSMikulas Patocka 	 */
26255a62eefSAlasdair G Kergon 	unsigned num_flush_bios;
263f9ab94ceSMikulas Patocka 
2645ae89a87SMike Snitzer 	/*
26555a62eefSAlasdair G Kergon 	 * The number of discard bios that will be submitted to the target.
26655a62eefSAlasdair G Kergon 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
2675ae89a87SMike Snitzer 	 */
26855a62eefSAlasdair G Kergon 	unsigned num_discard_bios;
2695ae89a87SMike Snitzer 
270d54eaa5aSMike Snitzer 	/*
271*00716545SDenis Semakin 	 * The number of secure erase bios that will be submitted to the target.
272*00716545SDenis Semakin 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
273*00716545SDenis Semakin 	 */
274*00716545SDenis Semakin 	unsigned num_secure_erase_bios;
275*00716545SDenis Semakin 
276*00716545SDenis Semakin 	/*
27755a62eefSAlasdair G Kergon 	 * The number of WRITE SAME bios that will be submitted to the target.
27855a62eefSAlasdair G Kergon 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
279d54eaa5aSMike Snitzer 	 */
28055a62eefSAlasdair G Kergon 	unsigned num_write_same_bios;
281d54eaa5aSMike Snitzer 
282c0820cf5SMikulas Patocka 	/*
283ac62d620SChristoph Hellwig 	 * The number of WRITE ZEROES bios that will be submitted to the target.
284ac62d620SChristoph Hellwig 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
285ac62d620SChristoph Hellwig 	 */
286ac62d620SChristoph Hellwig 	unsigned num_write_zeroes_bios;
287ac62d620SChristoph Hellwig 
288ac62d620SChristoph Hellwig 	/*
28930187e1dSMike Snitzer 	 * The minimum number of extra bytes allocated in each io for the
29030187e1dSMike Snitzer 	 * target to use.
291c0820cf5SMikulas Patocka 	 */
29230187e1dSMike Snitzer 	unsigned per_io_data_size;
293c0820cf5SMikulas Patocka 
2941da177e4SLinus Torvalds 	/* target specific data */
2951da177e4SLinus Torvalds 	void *private;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	/* Used to provide an error string from the ctr */
2981da177e4SLinus Torvalds 	char *error;
2994c259327SMike Snitzer 
3004c259327SMike Snitzer 	/*
3010e9c24edSJoe Thornber 	 * Set if this target needs to receive flushes regardless of
3020e9c24edSJoe Thornber 	 * whether or not its underlying devices have support.
3030e9c24edSJoe Thornber 	 */
3040e9c24edSJoe Thornber 	bool flush_supported:1;
3050e9c24edSJoe Thornber 
3060e9c24edSJoe Thornber 	/*
3074c259327SMike Snitzer 	 * Set if this target needs to receive discards regardless of
3084c259327SMike Snitzer 	 * whether or not its underlying devices have support.
3094c259327SMike Snitzer 	 */
3100ac55489SAlasdair G Kergon 	bool discards_supported:1;
311983c7db3SMilan Broz 
312983c7db3SMilan Broz 	/*
31355a62eefSAlasdair G Kergon 	 * Set if the target required discard bios to be split
3147acf0277SMikulas Patocka 	 * on max_io_len boundary.
3157acf0277SMikulas Patocka 	 */
31655a62eefSAlasdair G Kergon 	bool split_discard_bios:1;
3171da177e4SLinus Torvalds };
3181da177e4SLinus Torvalds 
3199d357b07SNeilBrown /* Each target can link one of these into the table */
3209d357b07SNeilBrown struct dm_target_callbacks {
3219d357b07SNeilBrown 	struct list_head list;
3229d357b07SNeilBrown 	int (*congested_fn) (struct dm_target_callbacks *, int);
3239d357b07SNeilBrown };
3249d357b07SNeilBrown 
32564f52b0eSMike Snitzer void *dm_per_bio_data(struct bio *bio, size_t data_size);
32664f52b0eSMike Snitzer struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size);
32764f52b0eSMike Snitzer unsigned dm_bio_get_target_bio_nr(const struct bio *bio);
328ddbd658fSMikulas Patocka 
3291da177e4SLinus Torvalds int dm_register_target(struct target_type *t);
33010d3bd09SMikulas Patocka void dm_unregister_target(struct target_type *t);
33117b2f66fSAlasdair G Kergon 
332498f0103SMike Snitzer /*
333498f0103SMike Snitzer  * Target argument parsing.
334498f0103SMike Snitzer  */
335498f0103SMike Snitzer struct dm_arg_set {
336498f0103SMike Snitzer 	unsigned argc;
337498f0103SMike Snitzer 	char **argv;
338498f0103SMike Snitzer };
339498f0103SMike Snitzer 
340498f0103SMike Snitzer /*
341498f0103SMike Snitzer  * The minimum and maximum value of a numeric argument, together with
342498f0103SMike Snitzer  * the error message to use if the number is found to be outside that range.
343498f0103SMike Snitzer  */
344498f0103SMike Snitzer struct dm_arg {
345498f0103SMike Snitzer 	unsigned min;
346498f0103SMike Snitzer 	unsigned max;
347498f0103SMike Snitzer 	char *error;
348498f0103SMike Snitzer };
349498f0103SMike Snitzer 
350498f0103SMike Snitzer /*
351498f0103SMike Snitzer  * Validate the next argument, either returning it as *value or, if invalid,
352498f0103SMike Snitzer  * returning -EINVAL and setting *error.
353498f0103SMike Snitzer  */
3545916a22bSEric Biggers int dm_read_arg(const struct dm_arg *arg, struct dm_arg_set *arg_set,
355498f0103SMike Snitzer 		unsigned *value, char **error);
356498f0103SMike Snitzer 
357498f0103SMike Snitzer /*
358498f0103SMike Snitzer  * Process the next argument as the start of a group containing between
359498f0103SMike Snitzer  * arg->min and arg->max further arguments. Either return the size as
360498f0103SMike Snitzer  * *num_args or, if invalid, return -EINVAL and set *error.
361498f0103SMike Snitzer  */
3625916a22bSEric Biggers int dm_read_arg_group(const struct dm_arg *arg, struct dm_arg_set *arg_set,
363498f0103SMike Snitzer 		      unsigned *num_args, char **error);
364498f0103SMike Snitzer 
365498f0103SMike Snitzer /*
366498f0103SMike Snitzer  * Return the current argument and shift to the next.
367498f0103SMike Snitzer  */
368498f0103SMike Snitzer const char *dm_shift_arg(struct dm_arg_set *as);
369498f0103SMike Snitzer 
370498f0103SMike Snitzer /*
371498f0103SMike Snitzer  * Move through num_args arguments.
372498f0103SMike Snitzer  */
373498f0103SMike Snitzer void dm_consume_args(struct dm_arg_set *as, unsigned num_args);
374498f0103SMike Snitzer 
37517b2f66fSAlasdair G Kergon /*-----------------------------------------------------------------
37617b2f66fSAlasdair G Kergon  * Functions for creating and manipulating mapped devices.
37717b2f66fSAlasdair G Kergon  * Drop the reference with dm_put when you finish with the object.
37817b2f66fSAlasdair G Kergon  *---------------------------------------------------------------*/
37917b2f66fSAlasdair G Kergon 
38017b2f66fSAlasdair G Kergon /*
38117b2f66fSAlasdair G Kergon  * DM_ANY_MINOR chooses the next available minor number.
38217b2f66fSAlasdair G Kergon  */
38317b2f66fSAlasdair G Kergon #define DM_ANY_MINOR (-1)
38417b2f66fSAlasdair G Kergon int dm_create(int minor, struct mapped_device **md);
38517b2f66fSAlasdair G Kergon 
38617b2f66fSAlasdair G Kergon /*
38717b2f66fSAlasdair G Kergon  * Reference counting for md.
38817b2f66fSAlasdair G Kergon  */
38917b2f66fSAlasdair G Kergon struct mapped_device *dm_get_md(dev_t dev);
39017b2f66fSAlasdair G Kergon void dm_get(struct mapped_device *md);
39109ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md);
39217b2f66fSAlasdair G Kergon void dm_put(struct mapped_device *md);
39317b2f66fSAlasdair G Kergon 
39417b2f66fSAlasdair G Kergon /*
39517b2f66fSAlasdair G Kergon  * An arbitrary pointer may be stored alongside a mapped device.
39617b2f66fSAlasdair G Kergon  */
39717b2f66fSAlasdair G Kergon void dm_set_mdptr(struct mapped_device *md, void *ptr);
39817b2f66fSAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md);
39917b2f66fSAlasdair G Kergon 
40017b2f66fSAlasdair G Kergon /*
40117b2f66fSAlasdair G Kergon  * A device can still be used while suspended, but I/O is deferred.
40217b2f66fSAlasdair G Kergon  */
403a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags);
40417b2f66fSAlasdair G Kergon int dm_resume(struct mapped_device *md);
40517b2f66fSAlasdair G Kergon 
40617b2f66fSAlasdair G Kergon /*
40717b2f66fSAlasdair G Kergon  * Event functions.
40817b2f66fSAlasdair G Kergon  */
40917b2f66fSAlasdair G Kergon uint32_t dm_get_event_nr(struct mapped_device *md);
41017b2f66fSAlasdair G Kergon int dm_wait_event(struct mapped_device *md, int event_nr);
4117a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md);
4127a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist);
41317b2f66fSAlasdair G Kergon 
41417b2f66fSAlasdair G Kergon /*
41517b2f66fSAlasdair G Kergon  * Info functions.
41617b2f66fSAlasdair G Kergon  */
41772d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md);
41896a1f7dbSMike Anderson int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid);
41917b2f66fSAlasdair G Kergon struct gendisk *dm_disk(struct mapped_device *md);
42064dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti);
4212e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti);
4221dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors);
42310999307SDamien Le Moal void dm_remap_zone_report(struct dm_target *ti, struct bio *bio,
42410999307SDamien Le Moal 			  sector_t start);
425cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq);
42617b2f66fSAlasdair G Kergon 
427f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md);
428f84cb8a4SMike Snitzer 
42917b2f66fSAlasdair G Kergon /*
43017b2f66fSAlasdair G Kergon  * Geometry functions.
43117b2f66fSAlasdair G Kergon  */
43217b2f66fSAlasdair G Kergon int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo);
43317b2f66fSAlasdair G Kergon int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo);
43417b2f66fSAlasdair G Kergon 
43517b2f66fSAlasdair G Kergon /*-----------------------------------------------------------------
43617b2f66fSAlasdair G Kergon  * Functions for manipulating device-mapper tables.
43717b2f66fSAlasdair G Kergon  *---------------------------------------------------------------*/
43817b2f66fSAlasdair G Kergon 
43917b2f66fSAlasdair G Kergon /*
44017b2f66fSAlasdair G Kergon  * First create an empty table.
44117b2f66fSAlasdair G Kergon  */
442aeb5d727SAl Viro int dm_table_create(struct dm_table **result, fmode_t mode,
44317b2f66fSAlasdair G Kergon 		    unsigned num_targets, struct mapped_device *md);
44417b2f66fSAlasdair G Kergon 
44517b2f66fSAlasdair G Kergon /*
44617b2f66fSAlasdair G Kergon  * Then call this once for each target.
44717b2f66fSAlasdair G Kergon  */
44817b2f66fSAlasdair G Kergon int dm_table_add_target(struct dm_table *t, const char *type,
44917b2f66fSAlasdair G Kergon 			sector_t start, sector_t len, char *params);
45017b2f66fSAlasdair G Kergon 
45117b2f66fSAlasdair G Kergon /*
4529d357b07SNeilBrown  * Target_ctr should call this if it needs to add any callbacks.
4539d357b07SNeilBrown  */
4549d357b07SNeilBrown void dm_table_add_target_callbacks(struct dm_table *t, struct dm_target_callbacks *cb);
4559d357b07SNeilBrown 
4569d357b07SNeilBrown /*
457e83068a5SMike Snitzer  * Target can use this to set the table's type.
458e83068a5SMike Snitzer  * Can only ever be called from a target's ctr.
459e83068a5SMike Snitzer  * Useful for "hybrid" target (supports both bio-based
460e83068a5SMike Snitzer  * and request-based).
461e83068a5SMike Snitzer  */
4627e0d574fSBart Van Assche void dm_table_set_type(struct dm_table *t, enum dm_queue_mode type);
463e83068a5SMike Snitzer 
464e83068a5SMike Snitzer /*
46517b2f66fSAlasdair G Kergon  * Finally call this to make the table ready for use.
46617b2f66fSAlasdair G Kergon  */
46717b2f66fSAlasdair G Kergon int dm_table_complete(struct dm_table *t);
46817b2f66fSAlasdair G Kergon 
46917b2f66fSAlasdair G Kergon /*
470f6e7baadSBrian Norris  * Destroy the table when finished.
471f6e7baadSBrian Norris  */
472f6e7baadSBrian Norris void dm_table_destroy(struct dm_table *t);
473f6e7baadSBrian Norris 
474f6e7baadSBrian Norris /*
475542f9038SMike Snitzer  * Target may require that it is never sent I/O larger than len.
476542f9038SMike Snitzer  */
477542f9038SMike Snitzer int __must_check dm_set_target_max_io_len(struct dm_target *ti, sector_t len);
478542f9038SMike Snitzer 
479542f9038SMike Snitzer /*
48017b2f66fSAlasdair G Kergon  * Table reference counting.
48117b2f66fSAlasdair G Kergon  */
48283d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx);
48383d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx);
48483d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md);
48517b2f66fSAlasdair G Kergon 
48617b2f66fSAlasdair G Kergon /*
48717b2f66fSAlasdair G Kergon  * Queries
48817b2f66fSAlasdair G Kergon  */
48917b2f66fSAlasdair G Kergon sector_t dm_table_get_size(struct dm_table *t);
49017b2f66fSAlasdair G Kergon unsigned int dm_table_get_num_targets(struct dm_table *t);
491aeb5d727SAl Viro fmode_t dm_table_get_mode(struct dm_table *t);
49217b2f66fSAlasdair G Kergon struct mapped_device *dm_table_get_md(struct dm_table *t);
49317b2f66fSAlasdair G Kergon 
49417b2f66fSAlasdair G Kergon /*
49517b2f66fSAlasdair G Kergon  * Trigger an event.
49617b2f66fSAlasdair G Kergon  */
49717b2f66fSAlasdair G Kergon void dm_table_event(struct dm_table *t);
49817b2f66fSAlasdair G Kergon 
49917b2f66fSAlasdair G Kergon /*
5009974fa2cSMike Snitzer  * Run the queue for request-based targets.
5019974fa2cSMike Snitzer  */
5029974fa2cSMike Snitzer void dm_table_run_md_queue_async(struct dm_table *t);
5039974fa2cSMike Snitzer 
5049974fa2cSMike Snitzer /*
50517b2f66fSAlasdair G Kergon  * The device must be suspended before calling this method.
506042d2a9bSAlasdair G Kergon  * Returns the previous table, which the caller must destroy.
50717b2f66fSAlasdair G Kergon  */
508042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md,
509042d2a9bSAlasdair G Kergon 			       struct dm_table *t);
51017b2f66fSAlasdair G Kergon 
51154160904SMikulas Patocka /*
51254160904SMikulas Patocka  * A wrapper around vmalloc.
51354160904SMikulas Patocka  */
51454160904SMikulas Patocka void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size);
51554160904SMikulas Patocka 
5160da336e5SAlasdair G Kergon /*-----------------------------------------------------------------
5170da336e5SAlasdair G Kergon  * Macros.
5180da336e5SAlasdair G Kergon  *---------------------------------------------------------------*/
5190da336e5SAlasdair G Kergon #define DM_NAME "device-mapper"
5200da336e5SAlasdair G Kergon 
52160440789SBart Van Assche #define DM_RATELIMIT(pr_func, fmt, ...)					\
52260440789SBart Van Assche do {									\
52360440789SBart Van Assche 	static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,	\
52460440789SBart Van Assche 				      DEFAULT_RATELIMIT_BURST);		\
52560440789SBart Van Assche 									\
52660440789SBart Van Assche 	if (__ratelimit(&rs))						\
52760440789SBart Van Assche 		pr_func(DM_FMT(fmt), ##__VA_ARGS__);			\
52860440789SBart Van Assche } while (0)
52971a16736SNamhyung Kim 
530d2c3c8dcSJoe Perches #define DM_FMT(fmt) DM_NAME ": " DM_MSG_PREFIX ": " fmt "\n"
53110d3bd09SMikulas Patocka 
532d2c3c8dcSJoe Perches #define DMCRIT(fmt, ...) pr_crit(DM_FMT(fmt), ##__VA_ARGS__)
533d2c3c8dcSJoe Perches 
534d2c3c8dcSJoe Perches #define DMERR(fmt, ...) pr_err(DM_FMT(fmt), ##__VA_ARGS__)
53560440789SBart Van Assche #define DMERR_LIMIT(fmt, ...) DM_RATELIMIT(pr_err, fmt, ##__VA_ARGS__)
536d2c3c8dcSJoe Perches #define DMWARN(fmt, ...) pr_warn(DM_FMT(fmt), ##__VA_ARGS__)
53760440789SBart Van Assche #define DMWARN_LIMIT(fmt, ...) DM_RATELIMIT(pr_warn, fmt, ##__VA_ARGS__)
538d2c3c8dcSJoe Perches #define DMINFO(fmt, ...) pr_info(DM_FMT(fmt), ##__VA_ARGS__)
53960440789SBart Van Assche #define DMINFO_LIMIT(fmt, ...) DM_RATELIMIT(pr_info, fmt, ##__VA_ARGS__)
5400da336e5SAlasdair G Kergon 
5410da336e5SAlasdair G Kergon #ifdef CONFIG_DM_DEBUG
542d2c3c8dcSJoe Perches #define DMDEBUG(fmt, ...) printk(KERN_DEBUG DM_FMT(fmt), ##__VA_ARGS__)
54360440789SBart Van Assche #define DMDEBUG_LIMIT(fmt, ...) DM_RATELIMIT(pr_debug, fmt, ##__VA_ARGS__)
5440da336e5SAlasdair G Kergon #else
545d2c3c8dcSJoe Perches #define DMDEBUG(fmt, ...) no_printk(fmt, ##__VA_ARGS__)
546d2c3c8dcSJoe Perches #define DMDEBUG_LIMIT(fmt, ...) no_printk(fmt, ##__VA_ARGS__)
5470da336e5SAlasdair G Kergon #endif
5480da336e5SAlasdair G Kergon 
5490da336e5SAlasdair G Kergon #define DMEMIT(x...) sz += ((sz >= maxlen) ? \
5500da336e5SAlasdair G Kergon 			  0 : scnprintf(result + sz, maxlen - sz, x))
5510da336e5SAlasdair G Kergon 
5520da336e5SAlasdair G Kergon #define SECTOR_SHIFT 9
5530da336e5SAlasdair G Kergon 
5540da336e5SAlasdair G Kergon /*
5550da336e5SAlasdair G Kergon  * Definitions of return values from target end_io function.
5560da336e5SAlasdair G Kergon  */
5577ed8578aSChristoph Hellwig #define DM_ENDIO_DONE		0
5580da336e5SAlasdair G Kergon #define DM_ENDIO_INCOMPLETE	1
5590da336e5SAlasdair G Kergon #define DM_ENDIO_REQUEUE	2
560ac514ffcSMike Snitzer #define DM_ENDIO_DELAY_REQUEUE	3
5610da336e5SAlasdair G Kergon 
5620da336e5SAlasdair G Kergon /*
5630da336e5SAlasdair G Kergon  * Definitions of return values from target map function.
5640da336e5SAlasdair G Kergon  */
5650da336e5SAlasdair G Kergon #define DM_MAPIO_SUBMITTED	0
5660da336e5SAlasdair G Kergon #define DM_MAPIO_REMAPPED	1
5670da336e5SAlasdair G Kergon #define DM_MAPIO_REQUEUE	DM_ENDIO_REQUEUE
568ac514ffcSMike Snitzer #define DM_MAPIO_DELAY_REQUEUE	DM_ENDIO_DELAY_REQUEUE
569412445acSChristoph Hellwig #define DM_MAPIO_KILL		4
5700da336e5SAlasdair G Kergon 
571fd2ed4d2SMikulas Patocka #define dm_sector_div64(x, y)( \
572fd2ed4d2SMikulas Patocka { \
573fd2ed4d2SMikulas Patocka 	u64 _res; \
574fd2ed4d2SMikulas Patocka 	(x) = div64_u64_rem(x, y, &_res); \
575fd2ed4d2SMikulas Patocka 	_res; \
576fd2ed4d2SMikulas Patocka } \
577fd2ed4d2SMikulas Patocka )
578fd2ed4d2SMikulas Patocka 
5790da336e5SAlasdair G Kergon /*
5800da336e5SAlasdair G Kergon  * Ceiling(n / sz)
5810da336e5SAlasdair G Kergon  */
5820da336e5SAlasdair G Kergon #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz))
5830da336e5SAlasdair G Kergon 
5840da336e5SAlasdair G Kergon #define dm_sector_div_up(n, sz) ( \
5850da336e5SAlasdair G Kergon { \
5860da336e5SAlasdair G Kergon 	sector_t _r = ((n) + (sz) - 1); \
5870da336e5SAlasdair G Kergon 	sector_div(_r, (sz)); \
5880da336e5SAlasdair G Kergon 	_r; \
5890da336e5SAlasdair G Kergon } \
5900da336e5SAlasdair G Kergon )
5910da336e5SAlasdair G Kergon 
5920da336e5SAlasdair G Kergon /*
5930da336e5SAlasdair G Kergon  * ceiling(n / size) * size
5940da336e5SAlasdair G Kergon  */
5950da336e5SAlasdair G Kergon #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
5960da336e5SAlasdair G Kergon 
597d63a5ce3SMikulas Patocka #define dm_array_too_big(fixed, obj, num) \
598d63a5ce3SMikulas Patocka 	((num) > (UINT_MAX - (fixed)) / (obj))
599d63a5ce3SMikulas Patocka 
60056a67df7SMike Snitzer /*
60156a67df7SMike Snitzer  * Sector offset taken relative to the start of the target instead of
60256a67df7SMike Snitzer  * relative to the start of the device.
60356a67df7SMike Snitzer  */
60456a67df7SMike Snitzer #define dm_target_offset(ti, sector) ((sector) - (ti)->begin)
60556a67df7SMike Snitzer 
6060da336e5SAlasdair G Kergon static inline sector_t to_sector(unsigned long n)
6070da336e5SAlasdair G Kergon {
6080da336e5SAlasdair G Kergon 	return (n >> SECTOR_SHIFT);
6090da336e5SAlasdair G Kergon }
6100da336e5SAlasdair G Kergon 
6110da336e5SAlasdair G Kergon static inline unsigned long to_bytes(sector_t n)
6120da336e5SAlasdair G Kergon {
6130da336e5SAlasdair G Kergon 	return (n << SECTOR_SHIFT);
6140da336e5SAlasdair G Kergon }
6150da336e5SAlasdair G Kergon 
6161da177e4SLinus Torvalds #endif	/* _LINUX_DEVICE_MAPPER_H */
617