xref: /openbmc/linux/include/linux/device-mapper.h (revision 10999307c14eac281fbec3ada73bee7a05bd41dc)
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,
317e0d574fSBart Van Assche };
32e83068a5SMike Snitzer 
331da177e4SLinus Torvalds typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE } status_type_t;
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds union map_info {
361da177e4SLinus Torvalds 	void *ptr;
371da177e4SLinus Torvalds };
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds /*
401da177e4SLinus Torvalds  * In the constructor the target parameter will already have the
411da177e4SLinus Torvalds  * table, type, begin and len fields filled in.
421da177e4SLinus Torvalds  */
431da177e4SLinus Torvalds typedef int (*dm_ctr_fn) (struct dm_target *target,
441da177e4SLinus Torvalds 			  unsigned int argc, char **argv);
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds /*
471da177e4SLinus Torvalds  * The destructor doesn't need to free the dm_target, just
481da177e4SLinus Torvalds  * anything hidden ti->private.
491da177e4SLinus Torvalds  */
501da177e4SLinus Torvalds typedef void (*dm_dtr_fn) (struct dm_target *ti);
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds /*
531da177e4SLinus Torvalds  * The map function must return:
541da177e4SLinus Torvalds  * < 0: error
551da177e4SLinus Torvalds  * = 0: The target will handle the io by resubmitting it later
5645cbcd79SKiyoshi Ueda  * = 1: simple remap complete
572e93ccc1SKiyoshi Ueda  * = 2: The target wants to push back the io
581da177e4SLinus Torvalds  */
597de3ee57SMikulas Patocka typedef int (*dm_map_fn) (struct dm_target *ti, struct bio *bio);
60e5863d9aSMike Snitzer typedef int (*dm_clone_and_map_request_fn) (struct dm_target *ti,
61e5863d9aSMike Snitzer 					    struct request *rq,
62e5863d9aSMike Snitzer 					    union map_info *map_context,
63e5863d9aSMike Snitzer 					    struct request **clone);
64e5863d9aSMike Snitzer typedef void (*dm_release_clone_request_fn) (struct request *clone);
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds /*
671da177e4SLinus Torvalds  * Returns:
681da177e4SLinus Torvalds  * < 0 : error (currently ignored)
691da177e4SLinus Torvalds  * 0   : ended successfully
701da177e4SLinus Torvalds  * 1   : for some reason the io has still not completed (eg,
711da177e4SLinus Torvalds  *       multipath target might want to requeue a failed io).
722e93ccc1SKiyoshi Ueda  * 2   : The target wants to push back the io
731da177e4SLinus Torvalds  */
741da177e4SLinus Torvalds typedef int (*dm_endio_fn) (struct dm_target *ti,
754e4cbee9SChristoph Hellwig 			    struct bio *bio, blk_status_t *error);
767d76345dSKiyoshi Ueda typedef int (*dm_request_endio_fn) (struct dm_target *ti,
772a842acaSChristoph Hellwig 				    struct request *clone, blk_status_t error,
787d76345dSKiyoshi Ueda 				    union map_info *map_context);
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds typedef void (*dm_presuspend_fn) (struct dm_target *ti);
81d67ee213SMike Snitzer typedef void (*dm_presuspend_undo_fn) (struct dm_target *ti);
821da177e4SLinus Torvalds typedef void (*dm_postsuspend_fn) (struct dm_target *ti);
838757b776SMilan Broz typedef int (*dm_preresume_fn) (struct dm_target *ti);
841da177e4SLinus Torvalds typedef void (*dm_resume_fn) (struct dm_target *ti);
851da177e4SLinus Torvalds 
86fd7c092eSMikulas Patocka typedef void (*dm_status_fn) (struct dm_target *ti, status_type_t status_type,
871f4e0ff0SAlasdair G Kergon 			      unsigned status_flags, char *result, unsigned maxlen);
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds typedef int (*dm_message_fn) (struct dm_target *ti, unsigned argc, char **argv);
901da177e4SLinus Torvalds 
91e56f81e0SChristoph Hellwig typedef int (*dm_prepare_ioctl_fn) (struct dm_target *ti,
92e56f81e0SChristoph Hellwig 			    struct block_device **bdev, fmode_t *mode);
93aa129a22SMilan Broz 
94058ce5caSAlasdair G Kergon /*
95058ce5caSAlasdair G Kergon  * These iteration functions are typically used to check (and combine)
96058ce5caSAlasdair G Kergon  * properties of underlying devices.
97058ce5caSAlasdair G Kergon  * E.g. Does at least one underlying device support flush?
98058ce5caSAlasdair G Kergon  *      Does any underlying device not support WRITE_SAME?
99058ce5caSAlasdair G Kergon  *
100058ce5caSAlasdair G Kergon  * The callout function is called once for each contiguous section of
101058ce5caSAlasdair G Kergon  * an underlying device.  State can be maintained in *data.
102058ce5caSAlasdair G Kergon  * Return non-zero to stop iterating through any further devices.
103058ce5caSAlasdair G Kergon  */
104af4874e0SMike Snitzer typedef int (*iterate_devices_callout_fn) (struct dm_target *ti,
105af4874e0SMike Snitzer 					   struct dm_dev *dev,
1065dea271bSMike Snitzer 					   sector_t start, sector_t len,
107af4874e0SMike Snitzer 					   void *data);
108af4874e0SMike Snitzer 
109058ce5caSAlasdair G Kergon /*
110058ce5caSAlasdair G Kergon  * This function must iterate through each section of device used by the
111058ce5caSAlasdair G Kergon  * target until it encounters a non-zero return code, which it then returns.
112058ce5caSAlasdair G Kergon  * Returns zero if no callout returned non-zero.
113058ce5caSAlasdair G Kergon  */
114af4874e0SMike Snitzer typedef int (*dm_iterate_devices_fn) (struct dm_target *ti,
115af4874e0SMike Snitzer 				      iterate_devices_callout_fn fn,
116af4874e0SMike Snitzer 				      void *data);
117af4874e0SMike Snitzer 
11840bea431SMike Snitzer typedef void (*dm_io_hints_fn) (struct dm_target *ti,
11940bea431SMike Snitzer 				struct queue_limits *limits);
12040bea431SMike Snitzer 
1217d76345dSKiyoshi Ueda /*
1227d76345dSKiyoshi Ueda  * Returns:
1237d76345dSKiyoshi Ueda  *    0: The target can handle the next I/O immediately.
1247d76345dSKiyoshi Ueda  *    1: The target can't handle the next I/O immediately.
1257d76345dSKiyoshi Ueda  */
1267d76345dSKiyoshi Ueda typedef int (*dm_busy_fn) (struct dm_target *ti);
1277d76345dSKiyoshi Ueda 
128545ed20eSToshi Kani /*
129545ed20eSToshi Kani  * Returns:
130545ed20eSToshi Kani  *  < 0 : error
131545ed20eSToshi Kani  * >= 0 : the number of bytes accessible at the address
132545ed20eSToshi Kani  */
133817bf402SDan Williams typedef long (*dm_dax_direct_access_fn) (struct dm_target *ti, pgoff_t pgoff,
134817bf402SDan Williams 		long nr_pages, void **kaddr, pfn_t *pfn);
135f26c5719SDan Williams #define PAGE_SECTORS (PAGE_SIZE / 512)
136545ed20eSToshi Kani 
1371da177e4SLinus Torvalds void dm_error(const char *message);
1381da177e4SLinus Torvalds 
13982b1519bSMikulas Patocka struct dm_dev {
14082b1519bSMikulas Patocka 	struct block_device *bdev;
141817bf402SDan Williams 	struct dax_device *dax_dev;
142aeb5d727SAl Viro 	fmode_t mode;
14382b1519bSMikulas Patocka 	char name[16];
14482b1519bSMikulas Patocka };
14582b1519bSMikulas Patocka 
1464df2bf46SDingXiang dev_t dm_get_dev_t(const char *path);
1474df2bf46SDingXiang 
1483cb40214SBryn Reeves /*
1491da177e4SLinus Torvalds  * Constructors should call these functions to ensure destination devices
1501da177e4SLinus Torvalds  * are opened/closed correctly.
1511da177e4SLinus Torvalds  */
1528215d6ecSNikanth Karthikesan int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
1538215d6ecSNikanth Karthikesan 		  struct dm_dev **result);
1541da177e4SLinus Torvalds void dm_put_device(struct dm_target *ti, struct dm_dev *d);
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds /*
1571da177e4SLinus Torvalds  * Information about a target type
1581da177e4SLinus Torvalds  */
159ab4c1424SAndi Kleen 
1601da177e4SLinus Torvalds struct target_type {
161ab4c1424SAndi Kleen 	uint64_t features;
1621da177e4SLinus Torvalds 	const char *name;
1631da177e4SLinus Torvalds 	struct module *module;
1641da177e4SLinus Torvalds 	unsigned version[3];
1651da177e4SLinus Torvalds 	dm_ctr_fn ctr;
1661da177e4SLinus Torvalds 	dm_dtr_fn dtr;
1671da177e4SLinus Torvalds 	dm_map_fn map;
168e5863d9aSMike Snitzer 	dm_clone_and_map_request_fn clone_and_map_rq;
169e5863d9aSMike Snitzer 	dm_release_clone_request_fn release_clone_rq;
1701da177e4SLinus Torvalds 	dm_endio_fn end_io;
1717d76345dSKiyoshi Ueda 	dm_request_endio_fn rq_end_io;
1721da177e4SLinus Torvalds 	dm_presuspend_fn presuspend;
173d67ee213SMike Snitzer 	dm_presuspend_undo_fn presuspend_undo;
1741da177e4SLinus Torvalds 	dm_postsuspend_fn postsuspend;
1758757b776SMilan Broz 	dm_preresume_fn preresume;
1761da177e4SLinus Torvalds 	dm_resume_fn resume;
1771da177e4SLinus Torvalds 	dm_status_fn status;
1781da177e4SLinus Torvalds 	dm_message_fn message;
179e56f81e0SChristoph Hellwig 	dm_prepare_ioctl_fn prepare_ioctl;
1807d76345dSKiyoshi Ueda 	dm_busy_fn busy;
181af4874e0SMike Snitzer 	dm_iterate_devices_fn iterate_devices;
18240bea431SMike Snitzer 	dm_io_hints_fn io_hints;
183817bf402SDan Williams 	dm_dax_direct_access_fn direct_access;
18445194e4fSCheng Renquan 
18545194e4fSCheng Renquan 	/* For internal device-mapper use. */
18645194e4fSCheng Renquan 	struct list_head list;
1871da177e4SLinus Torvalds };
1881da177e4SLinus Torvalds 
1893791e2fcSAlasdair G Kergon /*
1903791e2fcSAlasdair G Kergon  * Target features
1913791e2fcSAlasdair G Kergon  */
1923791e2fcSAlasdair G Kergon 
1933791e2fcSAlasdair G Kergon /*
1943791e2fcSAlasdair G Kergon  * Any table that contains an instance of this target must have only one.
1953791e2fcSAlasdair G Kergon  */
1963791e2fcSAlasdair G Kergon #define DM_TARGET_SINGLETON		0x00000001
1973791e2fcSAlasdair G Kergon #define dm_target_needs_singleton(type)	((type)->features & DM_TARGET_SINGLETON)
1983791e2fcSAlasdair G Kergon 
199cc6cbe14SAlasdair G Kergon /*
200cc6cbe14SAlasdair G Kergon  * Indicates that a target does not support read-only devices.
201cc6cbe14SAlasdair G Kergon  */
202cc6cbe14SAlasdair G Kergon #define DM_TARGET_ALWAYS_WRITEABLE	0x00000002
203cc6cbe14SAlasdair G Kergon #define dm_target_always_writeable(type) \
204cc6cbe14SAlasdair G Kergon 		((type)->features & DM_TARGET_ALWAYS_WRITEABLE)
205cc6cbe14SAlasdair G Kergon 
20636a0456fSAlasdair G Kergon /*
20736a0456fSAlasdair G Kergon  * Any device that contains a table with an instance of this target may never
20836a0456fSAlasdair G Kergon  * have tables containing any different target type.
20936a0456fSAlasdair G Kergon  */
21036a0456fSAlasdair G Kergon #define DM_TARGET_IMMUTABLE		0x00000004
21136a0456fSAlasdair G Kergon #define dm_target_is_immutable(type)	((type)->features & DM_TARGET_IMMUTABLE)
21236a0456fSAlasdair G Kergon 
213b0d8ed4dSAlasdair G Kergon /*
214f083b09bSMike Snitzer  * Indicates that a target may replace any target; even immutable targets.
215f083b09bSMike Snitzer  * .map, .map_rq, .clone_and_map_rq and .release_clone_rq are all defined.
216f083b09bSMike Snitzer  */
217f083b09bSMike Snitzer #define DM_TARGET_WILDCARD		0x00000008
218f083b09bSMike Snitzer #define dm_target_is_wildcard(type)	((type)->features & DM_TARGET_WILDCARD)
219f083b09bSMike Snitzer 
220f083b09bSMike Snitzer /*
221b0d8ed4dSAlasdair G Kergon  * Some targets need to be sent the same WRITE bio severals times so
222b0d8ed4dSAlasdair G Kergon  * that they can send copies of it to different devices.  This function
223b0d8ed4dSAlasdair G Kergon  * examines any supplied bio and returns the number of copies of it the
224b0d8ed4dSAlasdair G Kergon  * target requires.
225b0d8ed4dSAlasdair G Kergon  */
226b0d8ed4dSAlasdair G Kergon typedef unsigned (*dm_num_write_bios_fn) (struct dm_target *ti, struct bio *bio);
227b0d8ed4dSAlasdair G Kergon 
2289b4b5a79SMilan Broz /*
2299b4b5a79SMilan Broz  * A target implements own bio data integrity.
2309b4b5a79SMilan Broz  */
2319b4b5a79SMilan Broz #define DM_TARGET_INTEGRITY		0x00000010
2329b4b5a79SMilan Broz #define dm_target_has_integrity(type)	((type)->features & DM_TARGET_INTEGRITY)
2339b4b5a79SMilan Broz 
234e2460f2aSMikulas Patocka /*
235e2460f2aSMikulas Patocka  * A target passes integrity data to the lower device.
236e2460f2aSMikulas Patocka  */
237e2460f2aSMikulas Patocka #define DM_TARGET_PASSES_INTEGRITY	0x00000020
238e2460f2aSMikulas Patocka #define dm_target_passes_integrity(type) ((type)->features & DM_TARGET_PASSES_INTEGRITY)
239e2460f2aSMikulas Patocka 
240dd88d313SDamien Le Moal /*
241dd88d313SDamien Le Moal  * Indicates that a target supports host-managed zoned block devices.
242dd88d313SDamien Le Moal  */
243dd88d313SDamien Le Moal #define DM_TARGET_ZONED_HM		0x00000040
244dd88d313SDamien Le Moal #define dm_target_supports_zoned_hm(type) ((type)->features & DM_TARGET_ZONED_HM)
245dd88d313SDamien Le Moal 
2461da177e4SLinus Torvalds struct dm_target {
2471da177e4SLinus Torvalds 	struct dm_table *table;
2481da177e4SLinus Torvalds 	struct target_type *type;
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	/* target limits */
2511da177e4SLinus Torvalds 	sector_t begin;
2521da177e4SLinus Torvalds 	sector_t len;
2531da177e4SLinus Torvalds 
254542f9038SMike Snitzer 	/* If non-zero, maximum size of I/O submitted to a target. */
255542f9038SMike Snitzer 	uint32_t max_io_len;
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	/*
25855a62eefSAlasdair G Kergon 	 * A number of zero-length barrier bios that will be submitted
259f9ab94ceSMikulas Patocka 	 * to the target for the purpose of flushing cache.
260f9ab94ceSMikulas Patocka 	 *
26155a62eefSAlasdair G Kergon 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
26255a62eefSAlasdair G Kergon 	 * It is a responsibility of the target driver to remap these bios
263f9ab94ceSMikulas Patocka 	 * to the real underlying devices.
264f9ab94ceSMikulas Patocka 	 */
26555a62eefSAlasdair G Kergon 	unsigned num_flush_bios;
266f9ab94ceSMikulas Patocka 
2675ae89a87SMike Snitzer 	/*
26855a62eefSAlasdair G Kergon 	 * The number of discard bios that will be submitted to the target.
26955a62eefSAlasdair G Kergon 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
2705ae89a87SMike Snitzer 	 */
27155a62eefSAlasdair G Kergon 	unsigned num_discard_bios;
2725ae89a87SMike Snitzer 
273d54eaa5aSMike Snitzer 	/*
27455a62eefSAlasdair G Kergon 	 * The number of WRITE SAME bios that will be submitted to the target.
27555a62eefSAlasdair G Kergon 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
276d54eaa5aSMike Snitzer 	 */
27755a62eefSAlasdair G Kergon 	unsigned num_write_same_bios;
278d54eaa5aSMike Snitzer 
279c0820cf5SMikulas Patocka 	/*
280ac62d620SChristoph Hellwig 	 * The number of WRITE ZEROES bios that will be submitted to the target.
281ac62d620SChristoph Hellwig 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
282ac62d620SChristoph Hellwig 	 */
283ac62d620SChristoph Hellwig 	unsigned num_write_zeroes_bios;
284ac62d620SChristoph Hellwig 
285ac62d620SChristoph Hellwig 	/*
28630187e1dSMike Snitzer 	 * The minimum number of extra bytes allocated in each io for the
28730187e1dSMike Snitzer 	 * target to use.
288c0820cf5SMikulas Patocka 	 */
28930187e1dSMike Snitzer 	unsigned per_io_data_size;
290c0820cf5SMikulas Patocka 
291b0d8ed4dSAlasdair G Kergon 	/*
292b0d8ed4dSAlasdair G Kergon 	 * If defined, this function is called to find out how many
293b0d8ed4dSAlasdair G Kergon 	 * duplicate bios should be sent to the target when writing
294b0d8ed4dSAlasdair G Kergon 	 * data.
295b0d8ed4dSAlasdair G Kergon 	 */
296b0d8ed4dSAlasdair G Kergon 	dm_num_write_bios_fn num_write_bios;
297b0d8ed4dSAlasdair G Kergon 
2981da177e4SLinus Torvalds 	/* target specific data */
2991da177e4SLinus Torvalds 	void *private;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	/* Used to provide an error string from the ctr */
3021da177e4SLinus Torvalds 	char *error;
3034c259327SMike Snitzer 
3044c259327SMike Snitzer 	/*
3050e9c24edSJoe Thornber 	 * Set if this target needs to receive flushes regardless of
3060e9c24edSJoe Thornber 	 * whether or not its underlying devices have support.
3070e9c24edSJoe Thornber 	 */
3080e9c24edSJoe Thornber 	bool flush_supported:1;
3090e9c24edSJoe Thornber 
3100e9c24edSJoe Thornber 	/*
3114c259327SMike Snitzer 	 * Set if this target needs to receive discards regardless of
3124c259327SMike Snitzer 	 * whether or not its underlying devices have support.
3134c259327SMike Snitzer 	 */
3140ac55489SAlasdair G Kergon 	bool discards_supported:1;
315983c7db3SMilan Broz 
316983c7db3SMilan Broz 	/*
31755a62eefSAlasdair G Kergon 	 * Set if the target required discard bios to be split
3187acf0277SMikulas Patocka 	 * on max_io_len boundary.
3197acf0277SMikulas Patocka 	 */
32055a62eefSAlasdair G Kergon 	bool split_discard_bios:1;
3211da177e4SLinus Torvalds };
3221da177e4SLinus Torvalds 
3239d357b07SNeilBrown /* Each target can link one of these into the table */
3249d357b07SNeilBrown struct dm_target_callbacks {
3259d357b07SNeilBrown 	struct list_head list;
3269d357b07SNeilBrown 	int (*congested_fn) (struct dm_target_callbacks *, int);
3279d357b07SNeilBrown };
3289d357b07SNeilBrown 
329c0820cf5SMikulas Patocka /*
330c0820cf5SMikulas Patocka  * For bio-based dm.
331c0820cf5SMikulas Patocka  * One of these is allocated for each bio.
332c0820cf5SMikulas Patocka  * This structure shouldn't be touched directly by target drivers.
333c0820cf5SMikulas Patocka  * It is here so that we can inline dm_per_bio_data and
334c0820cf5SMikulas Patocka  * dm_bio_from_per_bio_data
335c0820cf5SMikulas Patocka  */
336c0820cf5SMikulas Patocka struct dm_target_io {
337c0820cf5SMikulas Patocka 	struct dm_io *io;
338c0820cf5SMikulas Patocka 	struct dm_target *ti;
33955a62eefSAlasdair G Kergon 	unsigned target_bio_nr;
3401dd40c3eSMikulas Patocka 	unsigned *len_ptr;
341c0820cf5SMikulas Patocka 	struct bio clone;
342c0820cf5SMikulas Patocka };
343c0820cf5SMikulas Patocka 
344c0820cf5SMikulas Patocka static inline void *dm_per_bio_data(struct bio *bio, size_t data_size)
345c0820cf5SMikulas Patocka {
346c0820cf5SMikulas Patocka 	return (char *)bio - offsetof(struct dm_target_io, clone) - data_size;
347c0820cf5SMikulas Patocka }
348c0820cf5SMikulas Patocka 
349c0820cf5SMikulas Patocka static inline struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size)
350c0820cf5SMikulas Patocka {
351c0820cf5SMikulas Patocka 	return (struct bio *)((char *)data + data_size + offsetof(struct dm_target_io, clone));
352c0820cf5SMikulas Patocka }
353c0820cf5SMikulas Patocka 
35455a62eefSAlasdair G Kergon static inline unsigned dm_bio_get_target_bio_nr(const struct bio *bio)
355ddbd658fSMikulas Patocka {
35655a62eefSAlasdair G Kergon 	return container_of(bio, struct dm_target_io, clone)->target_bio_nr;
357ddbd658fSMikulas Patocka }
358ddbd658fSMikulas Patocka 
3591da177e4SLinus Torvalds int dm_register_target(struct target_type *t);
36010d3bd09SMikulas Patocka void dm_unregister_target(struct target_type *t);
36117b2f66fSAlasdair G Kergon 
362498f0103SMike Snitzer /*
363498f0103SMike Snitzer  * Target argument parsing.
364498f0103SMike Snitzer  */
365498f0103SMike Snitzer struct dm_arg_set {
366498f0103SMike Snitzer 	unsigned argc;
367498f0103SMike Snitzer 	char **argv;
368498f0103SMike Snitzer };
369498f0103SMike Snitzer 
370498f0103SMike Snitzer /*
371498f0103SMike Snitzer  * The minimum and maximum value of a numeric argument, together with
372498f0103SMike Snitzer  * the error message to use if the number is found to be outside that range.
373498f0103SMike Snitzer  */
374498f0103SMike Snitzer struct dm_arg {
375498f0103SMike Snitzer 	unsigned min;
376498f0103SMike Snitzer 	unsigned max;
377498f0103SMike Snitzer 	char *error;
378498f0103SMike Snitzer };
379498f0103SMike Snitzer 
380498f0103SMike Snitzer /*
381498f0103SMike Snitzer  * Validate the next argument, either returning it as *value or, if invalid,
382498f0103SMike Snitzer  * returning -EINVAL and setting *error.
383498f0103SMike Snitzer  */
384498f0103SMike Snitzer int dm_read_arg(struct dm_arg *arg, struct dm_arg_set *arg_set,
385498f0103SMike Snitzer 		unsigned *value, char **error);
386498f0103SMike Snitzer 
387498f0103SMike Snitzer /*
388498f0103SMike Snitzer  * Process the next argument as the start of a group containing between
389498f0103SMike Snitzer  * arg->min and arg->max further arguments. Either return the size as
390498f0103SMike Snitzer  * *num_args or, if invalid, return -EINVAL and set *error.
391498f0103SMike Snitzer  */
392498f0103SMike Snitzer int dm_read_arg_group(struct dm_arg *arg, struct dm_arg_set *arg_set,
393498f0103SMike Snitzer 		      unsigned *num_args, char **error);
394498f0103SMike Snitzer 
395498f0103SMike Snitzer /*
396498f0103SMike Snitzer  * Return the current argument and shift to the next.
397498f0103SMike Snitzer  */
398498f0103SMike Snitzer const char *dm_shift_arg(struct dm_arg_set *as);
399498f0103SMike Snitzer 
400498f0103SMike Snitzer /*
401498f0103SMike Snitzer  * Move through num_args arguments.
402498f0103SMike Snitzer  */
403498f0103SMike Snitzer void dm_consume_args(struct dm_arg_set *as, unsigned num_args);
404498f0103SMike Snitzer 
40517b2f66fSAlasdair G Kergon /*-----------------------------------------------------------------
40617b2f66fSAlasdair G Kergon  * Functions for creating and manipulating mapped devices.
40717b2f66fSAlasdair G Kergon  * Drop the reference with dm_put when you finish with the object.
40817b2f66fSAlasdair G Kergon  *---------------------------------------------------------------*/
40917b2f66fSAlasdair G Kergon 
41017b2f66fSAlasdair G Kergon /*
41117b2f66fSAlasdair G Kergon  * DM_ANY_MINOR chooses the next available minor number.
41217b2f66fSAlasdair G Kergon  */
41317b2f66fSAlasdair G Kergon #define DM_ANY_MINOR (-1)
41417b2f66fSAlasdair G Kergon int dm_create(int minor, struct mapped_device **md);
41517b2f66fSAlasdair G Kergon 
41617b2f66fSAlasdair G Kergon /*
41717b2f66fSAlasdair G Kergon  * Reference counting for md.
41817b2f66fSAlasdair G Kergon  */
41917b2f66fSAlasdair G Kergon struct mapped_device *dm_get_md(dev_t dev);
42017b2f66fSAlasdair G Kergon void dm_get(struct mapped_device *md);
42109ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md);
42217b2f66fSAlasdair G Kergon void dm_put(struct mapped_device *md);
42317b2f66fSAlasdair G Kergon 
42417b2f66fSAlasdair G Kergon /*
42517b2f66fSAlasdair G Kergon  * An arbitrary pointer may be stored alongside a mapped device.
42617b2f66fSAlasdair G Kergon  */
42717b2f66fSAlasdair G Kergon void dm_set_mdptr(struct mapped_device *md, void *ptr);
42817b2f66fSAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md);
42917b2f66fSAlasdair G Kergon 
43017b2f66fSAlasdair G Kergon /*
43117b2f66fSAlasdair G Kergon  * A device can still be used while suspended, but I/O is deferred.
43217b2f66fSAlasdair G Kergon  */
433a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags);
43417b2f66fSAlasdair G Kergon int dm_resume(struct mapped_device *md);
43517b2f66fSAlasdair G Kergon 
43617b2f66fSAlasdair G Kergon /*
43717b2f66fSAlasdair G Kergon  * Event functions.
43817b2f66fSAlasdair G Kergon  */
43917b2f66fSAlasdair G Kergon uint32_t dm_get_event_nr(struct mapped_device *md);
44017b2f66fSAlasdair G Kergon int dm_wait_event(struct mapped_device *md, int event_nr);
4417a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md);
4427a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist);
44317b2f66fSAlasdair G Kergon 
44417b2f66fSAlasdair G Kergon /*
44517b2f66fSAlasdair G Kergon  * Info functions.
44617b2f66fSAlasdair G Kergon  */
44772d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md);
44896a1f7dbSMike Anderson int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid);
44917b2f66fSAlasdair G Kergon struct gendisk *dm_disk(struct mapped_device *md);
45064dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti);
4512e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti);
4521dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors);
453*10999307SDamien Le Moal void dm_remap_zone_report(struct dm_target *ti, struct bio *bio,
454*10999307SDamien Le Moal 			  sector_t start);
455cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq);
45617b2f66fSAlasdair G Kergon 
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 /*
4829d357b07SNeilBrown  * Target_ctr should call this if it needs to add any callbacks.
4839d357b07SNeilBrown  */
4849d357b07SNeilBrown void dm_table_add_target_callbacks(struct dm_table *t, struct dm_target_callbacks *cb);
4859d357b07SNeilBrown 
4869d357b07SNeilBrown /*
487e83068a5SMike Snitzer  * Target can use this to set the table's type.
488e83068a5SMike Snitzer  * Can only ever be called from a target's ctr.
489e83068a5SMike Snitzer  * Useful for "hybrid" target (supports both bio-based
490e83068a5SMike Snitzer  * and request-based).
491e83068a5SMike Snitzer  */
4927e0d574fSBart Van Assche void dm_table_set_type(struct dm_table *t, enum dm_queue_mode type);
493e83068a5SMike Snitzer 
494e83068a5SMike Snitzer /*
49517b2f66fSAlasdair G Kergon  * Finally call this to make the table ready for use.
49617b2f66fSAlasdair G Kergon  */
49717b2f66fSAlasdair G Kergon int dm_table_complete(struct dm_table *t);
49817b2f66fSAlasdair G Kergon 
49917b2f66fSAlasdair G Kergon /*
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);
51817b2f66fSAlasdair G Kergon 
51917b2f66fSAlasdair G Kergon /*
52017b2f66fSAlasdair G Kergon  * Trigger an event.
52117b2f66fSAlasdair G Kergon  */
52217b2f66fSAlasdair G Kergon void dm_table_event(struct dm_table *t);
52317b2f66fSAlasdair G Kergon 
52417b2f66fSAlasdair G Kergon /*
5259974fa2cSMike Snitzer  * Run the queue for request-based targets.
5269974fa2cSMike Snitzer  */
5279974fa2cSMike Snitzer void dm_table_run_md_queue_async(struct dm_table *t);
5289974fa2cSMike Snitzer 
5299974fa2cSMike Snitzer /*
53017b2f66fSAlasdair G Kergon  * The device must be suspended before calling this method.
531042d2a9bSAlasdair G Kergon  * Returns the previous table, which the caller must destroy.
53217b2f66fSAlasdair G Kergon  */
533042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md,
534042d2a9bSAlasdair G Kergon 			       struct dm_table *t);
53517b2f66fSAlasdair G Kergon 
53654160904SMikulas Patocka /*
53754160904SMikulas Patocka  * A wrapper around vmalloc.
53854160904SMikulas Patocka  */
53954160904SMikulas Patocka void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size);
54054160904SMikulas Patocka 
5410da336e5SAlasdair G Kergon /*-----------------------------------------------------------------
5420da336e5SAlasdair G Kergon  * Macros.
5430da336e5SAlasdair G Kergon  *---------------------------------------------------------------*/
5440da336e5SAlasdair G Kergon #define DM_NAME "device-mapper"
5450da336e5SAlasdair G Kergon 
54671a16736SNamhyung Kim #ifdef CONFIG_PRINTK
54771a16736SNamhyung Kim extern struct ratelimit_state dm_ratelimit_state;
54871a16736SNamhyung Kim 
54971a16736SNamhyung Kim #define dm_ratelimit()	__ratelimit(&dm_ratelimit_state)
55071a16736SNamhyung Kim #else
55171a16736SNamhyung Kim #define dm_ratelimit()	0
55271a16736SNamhyung Kim #endif
55371a16736SNamhyung Kim 
554d2c3c8dcSJoe Perches #define DM_FMT(fmt) DM_NAME ": " DM_MSG_PREFIX ": " fmt "\n"
55510d3bd09SMikulas Patocka 
556d2c3c8dcSJoe Perches #define DMCRIT(fmt, ...) pr_crit(DM_FMT(fmt), ##__VA_ARGS__)
557d2c3c8dcSJoe Perches 
558d2c3c8dcSJoe Perches #define DMERR(fmt, ...) pr_err(DM_FMT(fmt), ##__VA_ARGS__)
559d2c3c8dcSJoe Perches #define DMERR_LIMIT(fmt, ...)						\
5600da336e5SAlasdair G Kergon do {									\
56171a16736SNamhyung Kim 	if (dm_ratelimit())						\
562d2c3c8dcSJoe Perches 		DMERR(fmt, ##__VA_ARGS__);				\
5630da336e5SAlasdair G Kergon } while (0)
5640da336e5SAlasdair G Kergon 
565d2c3c8dcSJoe Perches #define DMWARN(fmt, ...) pr_warn(DM_FMT(fmt), ##__VA_ARGS__)
566d2c3c8dcSJoe Perches #define DMWARN_LIMIT(fmt, ...)						\
5670da336e5SAlasdair G Kergon do {									\
56871a16736SNamhyung Kim 	if (dm_ratelimit())						\
569d2c3c8dcSJoe Perches 		DMWARN(fmt, ##__VA_ARGS__);				\
5700da336e5SAlasdair G Kergon } while (0)
5710da336e5SAlasdair G Kergon 
572d2c3c8dcSJoe Perches #define DMINFO(fmt, ...) pr_info(DM_FMT(fmt), ##__VA_ARGS__)
573d2c3c8dcSJoe Perches #define DMINFO_LIMIT(fmt, ...)						\
5740da336e5SAlasdair G Kergon do {									\
57571a16736SNamhyung Kim 	if (dm_ratelimit())						\
576d2c3c8dcSJoe Perches 		DMINFO(fmt, ##__VA_ARGS__);				\
5770da336e5SAlasdair G Kergon } while (0)
5780da336e5SAlasdair G Kergon 
5790da336e5SAlasdair G Kergon #ifdef CONFIG_DM_DEBUG
580d2c3c8dcSJoe Perches #define DMDEBUG(fmt, ...) printk(KERN_DEBUG DM_FMT(fmt), ##__VA_ARGS__)
581d2c3c8dcSJoe Perches #define DMDEBUG_LIMIT(fmt, ...)						\
5820da336e5SAlasdair G Kergon do {									\
58371a16736SNamhyung Kim 	if (dm_ratelimit())						\
584d2c3c8dcSJoe Perches 		DMDEBUG(fmt, ##__VA_ARGS__);				\
5850da336e5SAlasdair G Kergon } while (0)
5860da336e5SAlasdair G Kergon #else
587d2c3c8dcSJoe Perches #define DMDEBUG(fmt, ...) no_printk(fmt, ##__VA_ARGS__)
588d2c3c8dcSJoe Perches #define DMDEBUG_LIMIT(fmt, ...) no_printk(fmt, ##__VA_ARGS__)
5890da336e5SAlasdair G Kergon #endif
5900da336e5SAlasdair G Kergon 
5910da336e5SAlasdair G Kergon #define DMEMIT(x...) sz += ((sz >= maxlen) ? \
5920da336e5SAlasdair G Kergon 			  0 : scnprintf(result + sz, maxlen - sz, x))
5930da336e5SAlasdair G Kergon 
5940da336e5SAlasdair G Kergon #define SECTOR_SHIFT 9
5950da336e5SAlasdair G Kergon 
5960da336e5SAlasdair G Kergon /*
5970da336e5SAlasdair G Kergon  * Definitions of return values from target end_io function.
5980da336e5SAlasdair G Kergon  */
5997ed8578aSChristoph Hellwig #define DM_ENDIO_DONE		0
6000da336e5SAlasdair G Kergon #define DM_ENDIO_INCOMPLETE	1
6010da336e5SAlasdair G Kergon #define DM_ENDIO_REQUEUE	2
6020da336e5SAlasdair G Kergon 
6030da336e5SAlasdair G Kergon /*
6040da336e5SAlasdair G Kergon  * Definitions of return values from target map function.
6050da336e5SAlasdair G Kergon  */
6060da336e5SAlasdair G Kergon #define DM_MAPIO_SUBMITTED	0
6070da336e5SAlasdair G Kergon #define DM_MAPIO_REMAPPED	1
6080da336e5SAlasdair G Kergon #define DM_MAPIO_REQUEUE	DM_ENDIO_REQUEUE
609a8ac51e4SMike Snitzer #define DM_MAPIO_DELAY_REQUEUE	3
610412445acSChristoph Hellwig #define DM_MAPIO_KILL		4
6110da336e5SAlasdair G Kergon 
612fd2ed4d2SMikulas Patocka #define dm_sector_div64(x, y)( \
613fd2ed4d2SMikulas Patocka { \
614fd2ed4d2SMikulas Patocka 	u64 _res; \
615fd2ed4d2SMikulas Patocka 	(x) = div64_u64_rem(x, y, &_res); \
616fd2ed4d2SMikulas Patocka 	_res; \
617fd2ed4d2SMikulas Patocka } \
618fd2ed4d2SMikulas Patocka )
619fd2ed4d2SMikulas Patocka 
6200da336e5SAlasdair G Kergon /*
6210da336e5SAlasdair G Kergon  * Ceiling(n / sz)
6220da336e5SAlasdair G Kergon  */
6230da336e5SAlasdair G Kergon #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz))
6240da336e5SAlasdair G Kergon 
6250da336e5SAlasdair G Kergon #define dm_sector_div_up(n, sz) ( \
6260da336e5SAlasdair G Kergon { \
6270da336e5SAlasdair G Kergon 	sector_t _r = ((n) + (sz) - 1); \
6280da336e5SAlasdair G Kergon 	sector_div(_r, (sz)); \
6290da336e5SAlasdair G Kergon 	_r; \
6300da336e5SAlasdair G Kergon } \
6310da336e5SAlasdair G Kergon )
6320da336e5SAlasdair G Kergon 
6330da336e5SAlasdair G Kergon /*
6340da336e5SAlasdair G Kergon  * ceiling(n / size) * size
6350da336e5SAlasdair G Kergon  */
6360da336e5SAlasdair G Kergon #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
6370da336e5SAlasdair G Kergon 
638d63a5ce3SMikulas Patocka #define dm_array_too_big(fixed, obj, num) \
639d63a5ce3SMikulas Patocka 	((num) > (UINT_MAX - (fixed)) / (obj))
640d63a5ce3SMikulas Patocka 
64156a67df7SMike Snitzer /*
64256a67df7SMike Snitzer  * Sector offset taken relative to the start of the target instead of
64356a67df7SMike Snitzer  * relative to the start of the device.
64456a67df7SMike Snitzer  */
64556a67df7SMike Snitzer #define dm_target_offset(ti, sector) ((sector) - (ti)->begin)
64656a67df7SMike Snitzer 
6470da336e5SAlasdair G Kergon static inline sector_t to_sector(unsigned long n)
6480da336e5SAlasdair G Kergon {
6490da336e5SAlasdair G Kergon 	return (n >> SECTOR_SHIFT);
6500da336e5SAlasdair G Kergon }
6510da336e5SAlasdair G Kergon 
6520da336e5SAlasdair G Kergon static inline unsigned long to_bytes(sector_t n)
6530da336e5SAlasdair G Kergon {
6540da336e5SAlasdair G Kergon 	return (n << SECTOR_SHIFT);
6550da336e5SAlasdair G Kergon }
6560da336e5SAlasdair G Kergon 
6571da177e4SLinus Torvalds #endif	/* _LINUX_DEVICE_MAPPER_H */
658