xref: /openbmc/linux/include/linux/device-mapper.h (revision e2460f2a4bc740fae9e23f14d653cf53e90b3f9a)
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  */
25e83068a5SMike Snitzer #define DM_TYPE_NONE			0
26e83068a5SMike Snitzer #define DM_TYPE_BIO_BASED		1
27e83068a5SMike Snitzer #define DM_TYPE_REQUEST_BASED		2
28e83068a5SMike Snitzer #define DM_TYPE_MQ_REQUEST_BASED	3
29545ed20eSToshi Kani #define DM_TYPE_DAX_BIO_BASED		4
30e83068a5SMike Snitzer 
311da177e4SLinus Torvalds typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE } status_type_t;
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds union map_info {
341da177e4SLinus Torvalds 	void *ptr;
351da177e4SLinus Torvalds };
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds /*
381da177e4SLinus Torvalds  * In the constructor the target parameter will already have the
391da177e4SLinus Torvalds  * table, type, begin and len fields filled in.
401da177e4SLinus Torvalds  */
411da177e4SLinus Torvalds typedef int (*dm_ctr_fn) (struct dm_target *target,
421da177e4SLinus Torvalds 			  unsigned int argc, char **argv);
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds /*
451da177e4SLinus Torvalds  * The destructor doesn't need to free the dm_target, just
461da177e4SLinus Torvalds  * anything hidden ti->private.
471da177e4SLinus Torvalds  */
481da177e4SLinus Torvalds typedef void (*dm_dtr_fn) (struct dm_target *ti);
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds /*
511da177e4SLinus Torvalds  * The map function must return:
521da177e4SLinus Torvalds  * < 0: error
531da177e4SLinus Torvalds  * = 0: The target will handle the io by resubmitting it later
5445cbcd79SKiyoshi Ueda  * = 1: simple remap complete
552e93ccc1SKiyoshi Ueda  * = 2: The target wants to push back the io
561da177e4SLinus Torvalds  */
577de3ee57SMikulas Patocka typedef int (*dm_map_fn) (struct dm_target *ti, struct bio *bio);
58e5863d9aSMike Snitzer typedef int (*dm_clone_and_map_request_fn) (struct dm_target *ti,
59e5863d9aSMike Snitzer 					    struct request *rq,
60e5863d9aSMike Snitzer 					    union map_info *map_context,
61e5863d9aSMike Snitzer 					    struct request **clone);
62e5863d9aSMike Snitzer typedef void (*dm_release_clone_request_fn) (struct request *clone);
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /*
651da177e4SLinus Torvalds  * Returns:
661da177e4SLinus Torvalds  * < 0 : error (currently ignored)
671da177e4SLinus Torvalds  * 0   : ended successfully
681da177e4SLinus Torvalds  * 1   : for some reason the io has still not completed (eg,
691da177e4SLinus Torvalds  *       multipath target might want to requeue a failed io).
702e93ccc1SKiyoshi Ueda  * 2   : The target wants to push back the io
711da177e4SLinus Torvalds  */
721da177e4SLinus Torvalds typedef int (*dm_endio_fn) (struct dm_target *ti,
737de3ee57SMikulas Patocka 			    struct bio *bio, int error);
747d76345dSKiyoshi Ueda typedef int (*dm_request_endio_fn) (struct dm_target *ti,
757d76345dSKiyoshi Ueda 				    struct request *clone, int error,
767d76345dSKiyoshi Ueda 				    union map_info *map_context);
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds typedef void (*dm_presuspend_fn) (struct dm_target *ti);
79d67ee213SMike Snitzer typedef void (*dm_presuspend_undo_fn) (struct dm_target *ti);
801da177e4SLinus Torvalds typedef void (*dm_postsuspend_fn) (struct dm_target *ti);
818757b776SMilan Broz typedef int (*dm_preresume_fn) (struct dm_target *ti);
821da177e4SLinus Torvalds typedef void (*dm_resume_fn) (struct dm_target *ti);
831da177e4SLinus Torvalds 
84fd7c092eSMikulas Patocka typedef void (*dm_status_fn) (struct dm_target *ti, status_type_t status_type,
851f4e0ff0SAlasdair G Kergon 			      unsigned status_flags, char *result, unsigned maxlen);
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds typedef int (*dm_message_fn) (struct dm_target *ti, unsigned argc, char **argv);
881da177e4SLinus Torvalds 
89e56f81e0SChristoph Hellwig typedef int (*dm_prepare_ioctl_fn) (struct dm_target *ti,
90e56f81e0SChristoph Hellwig 			    struct block_device **bdev, fmode_t *mode);
91aa129a22SMilan Broz 
92058ce5caSAlasdair G Kergon /*
93058ce5caSAlasdair G Kergon  * These iteration functions are typically used to check (and combine)
94058ce5caSAlasdair G Kergon  * properties of underlying devices.
95058ce5caSAlasdair G Kergon  * E.g. Does at least one underlying device support flush?
96058ce5caSAlasdair G Kergon  *      Does any underlying device not support WRITE_SAME?
97058ce5caSAlasdair G Kergon  *
98058ce5caSAlasdair G Kergon  * The callout function is called once for each contiguous section of
99058ce5caSAlasdair G Kergon  * an underlying device.  State can be maintained in *data.
100058ce5caSAlasdair G Kergon  * Return non-zero to stop iterating through any further devices.
101058ce5caSAlasdair G Kergon  */
102af4874e0SMike Snitzer typedef int (*iterate_devices_callout_fn) (struct dm_target *ti,
103af4874e0SMike Snitzer 					   struct dm_dev *dev,
1045dea271bSMike Snitzer 					   sector_t start, sector_t len,
105af4874e0SMike Snitzer 					   void *data);
106af4874e0SMike Snitzer 
107058ce5caSAlasdair G Kergon /*
108058ce5caSAlasdair G Kergon  * This function must iterate through each section of device used by the
109058ce5caSAlasdair G Kergon  * target until it encounters a non-zero return code, which it then returns.
110058ce5caSAlasdair G Kergon  * Returns zero if no callout returned non-zero.
111058ce5caSAlasdair G Kergon  */
112af4874e0SMike Snitzer typedef int (*dm_iterate_devices_fn) (struct dm_target *ti,
113af4874e0SMike Snitzer 				      iterate_devices_callout_fn fn,
114af4874e0SMike Snitzer 				      void *data);
115af4874e0SMike Snitzer 
11640bea431SMike Snitzer typedef void (*dm_io_hints_fn) (struct dm_target *ti,
11740bea431SMike Snitzer 				struct queue_limits *limits);
11840bea431SMike Snitzer 
1197d76345dSKiyoshi Ueda /*
1207d76345dSKiyoshi Ueda  * Returns:
1217d76345dSKiyoshi Ueda  *    0: The target can handle the next I/O immediately.
1227d76345dSKiyoshi Ueda  *    1: The target can't handle the next I/O immediately.
1237d76345dSKiyoshi Ueda  */
1247d76345dSKiyoshi Ueda typedef int (*dm_busy_fn) (struct dm_target *ti);
1257d76345dSKiyoshi Ueda 
126545ed20eSToshi Kani /*
127545ed20eSToshi Kani  * Returns:
128545ed20eSToshi Kani  *  < 0 : error
129545ed20eSToshi Kani  * >= 0 : the number of bytes accessible at the address
130545ed20eSToshi Kani  */
131545ed20eSToshi Kani typedef long (*dm_direct_access_fn) (struct dm_target *ti, sector_t sector,
132f0c98ebcSLinus Torvalds 				     void **kaddr, pfn_t *pfn, long size);
133545ed20eSToshi Kani 
1341da177e4SLinus Torvalds void dm_error(const char *message);
1351da177e4SLinus Torvalds 
13682b1519bSMikulas Patocka struct dm_dev {
13782b1519bSMikulas Patocka 	struct block_device *bdev;
138aeb5d727SAl Viro 	fmode_t mode;
13982b1519bSMikulas Patocka 	char name[16];
14082b1519bSMikulas Patocka };
14182b1519bSMikulas Patocka 
1424df2bf46SDingXiang dev_t dm_get_dev_t(const char *path);
1434df2bf46SDingXiang 
1443cb40214SBryn Reeves /*
1451da177e4SLinus Torvalds  * Constructors should call these functions to ensure destination devices
1461da177e4SLinus Torvalds  * are opened/closed correctly.
1471da177e4SLinus Torvalds  */
1488215d6ecSNikanth Karthikesan int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
1498215d6ecSNikanth Karthikesan 		  struct dm_dev **result);
1501da177e4SLinus Torvalds void dm_put_device(struct dm_target *ti, struct dm_dev *d);
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds /*
1531da177e4SLinus Torvalds  * Information about a target type
1541da177e4SLinus Torvalds  */
155ab4c1424SAndi Kleen 
1561da177e4SLinus Torvalds struct target_type {
157ab4c1424SAndi Kleen 	uint64_t features;
1581da177e4SLinus Torvalds 	const char *name;
1591da177e4SLinus Torvalds 	struct module *module;
1601da177e4SLinus Torvalds 	unsigned version[3];
1611da177e4SLinus Torvalds 	dm_ctr_fn ctr;
1621da177e4SLinus Torvalds 	dm_dtr_fn dtr;
1631da177e4SLinus Torvalds 	dm_map_fn map;
164e5863d9aSMike Snitzer 	dm_clone_and_map_request_fn clone_and_map_rq;
165e5863d9aSMike Snitzer 	dm_release_clone_request_fn release_clone_rq;
1661da177e4SLinus Torvalds 	dm_endio_fn end_io;
1677d76345dSKiyoshi Ueda 	dm_request_endio_fn rq_end_io;
1681da177e4SLinus Torvalds 	dm_presuspend_fn presuspend;
169d67ee213SMike Snitzer 	dm_presuspend_undo_fn presuspend_undo;
1701da177e4SLinus Torvalds 	dm_postsuspend_fn postsuspend;
1718757b776SMilan Broz 	dm_preresume_fn preresume;
1721da177e4SLinus Torvalds 	dm_resume_fn resume;
1731da177e4SLinus Torvalds 	dm_status_fn status;
1741da177e4SLinus Torvalds 	dm_message_fn message;
175e56f81e0SChristoph Hellwig 	dm_prepare_ioctl_fn prepare_ioctl;
1767d76345dSKiyoshi Ueda 	dm_busy_fn busy;
177af4874e0SMike Snitzer 	dm_iterate_devices_fn iterate_devices;
17840bea431SMike Snitzer 	dm_io_hints_fn io_hints;
179545ed20eSToshi Kani 	dm_direct_access_fn direct_access;
18045194e4fSCheng Renquan 
18145194e4fSCheng Renquan 	/* For internal device-mapper use. */
18245194e4fSCheng Renquan 	struct list_head list;
1831da177e4SLinus Torvalds };
1841da177e4SLinus Torvalds 
1853791e2fcSAlasdair G Kergon /*
1863791e2fcSAlasdair G Kergon  * Target features
1873791e2fcSAlasdair G Kergon  */
1883791e2fcSAlasdair G Kergon 
1893791e2fcSAlasdair G Kergon /*
1903791e2fcSAlasdair G Kergon  * Any table that contains an instance of this target must have only one.
1913791e2fcSAlasdair G Kergon  */
1923791e2fcSAlasdair G Kergon #define DM_TARGET_SINGLETON		0x00000001
1933791e2fcSAlasdair G Kergon #define dm_target_needs_singleton(type)	((type)->features & DM_TARGET_SINGLETON)
1943791e2fcSAlasdair G Kergon 
195cc6cbe14SAlasdair G Kergon /*
196cc6cbe14SAlasdair G Kergon  * Indicates that a target does not support read-only devices.
197cc6cbe14SAlasdair G Kergon  */
198cc6cbe14SAlasdair G Kergon #define DM_TARGET_ALWAYS_WRITEABLE	0x00000002
199cc6cbe14SAlasdair G Kergon #define dm_target_always_writeable(type) \
200cc6cbe14SAlasdair G Kergon 		((type)->features & DM_TARGET_ALWAYS_WRITEABLE)
201cc6cbe14SAlasdair G Kergon 
20236a0456fSAlasdair G Kergon /*
20336a0456fSAlasdair G Kergon  * Any device that contains a table with an instance of this target may never
20436a0456fSAlasdair G Kergon  * have tables containing any different target type.
20536a0456fSAlasdair G Kergon  */
20636a0456fSAlasdair G Kergon #define DM_TARGET_IMMUTABLE		0x00000004
20736a0456fSAlasdair G Kergon #define dm_target_is_immutable(type)	((type)->features & DM_TARGET_IMMUTABLE)
20836a0456fSAlasdair G Kergon 
209b0d8ed4dSAlasdair G Kergon /*
210f083b09bSMike Snitzer  * Indicates that a target may replace any target; even immutable targets.
211f083b09bSMike Snitzer  * .map, .map_rq, .clone_and_map_rq and .release_clone_rq are all defined.
212f083b09bSMike Snitzer  */
213f083b09bSMike Snitzer #define DM_TARGET_WILDCARD		0x00000008
214f083b09bSMike Snitzer #define dm_target_is_wildcard(type)	((type)->features & DM_TARGET_WILDCARD)
215f083b09bSMike Snitzer 
216f083b09bSMike Snitzer /*
217b0d8ed4dSAlasdair G Kergon  * Some targets need to be sent the same WRITE bio severals times so
218b0d8ed4dSAlasdair G Kergon  * that they can send copies of it to different devices.  This function
219b0d8ed4dSAlasdair G Kergon  * examines any supplied bio and returns the number of copies of it the
220b0d8ed4dSAlasdair G Kergon  * target requires.
221b0d8ed4dSAlasdair G Kergon  */
222b0d8ed4dSAlasdair G Kergon typedef unsigned (*dm_num_write_bios_fn) (struct dm_target *ti, struct bio *bio);
223b0d8ed4dSAlasdair G Kergon 
2249b4b5a79SMilan Broz /*
2259b4b5a79SMilan Broz  * A target implements own bio data integrity.
2269b4b5a79SMilan Broz  */
2279b4b5a79SMilan Broz #define DM_TARGET_INTEGRITY		0x00000010
2289b4b5a79SMilan Broz #define dm_target_has_integrity(type)	((type)->features & DM_TARGET_INTEGRITY)
2299b4b5a79SMilan Broz 
230*e2460f2aSMikulas Patocka /*
231*e2460f2aSMikulas Patocka  * A target passes integrity data to the lower device.
232*e2460f2aSMikulas Patocka  */
233*e2460f2aSMikulas Patocka #define DM_TARGET_PASSES_INTEGRITY	0x00000020
234*e2460f2aSMikulas Patocka #define dm_target_passes_integrity(type) ((type)->features & DM_TARGET_PASSES_INTEGRITY)
235*e2460f2aSMikulas Patocka 
2361da177e4SLinus Torvalds struct dm_target {
2371da177e4SLinus Torvalds 	struct dm_table *table;
2381da177e4SLinus Torvalds 	struct target_type *type;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	/* target limits */
2411da177e4SLinus Torvalds 	sector_t begin;
2421da177e4SLinus Torvalds 	sector_t len;
2431da177e4SLinus Torvalds 
244542f9038SMike Snitzer 	/* If non-zero, maximum size of I/O submitted to a target. */
245542f9038SMike Snitzer 	uint32_t max_io_len;
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	/*
24855a62eefSAlasdair G Kergon 	 * A number of zero-length barrier bios that will be submitted
249f9ab94ceSMikulas Patocka 	 * to the target for the purpose of flushing cache.
250f9ab94ceSMikulas Patocka 	 *
25155a62eefSAlasdair G Kergon 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
25255a62eefSAlasdair G Kergon 	 * It is a responsibility of the target driver to remap these bios
253f9ab94ceSMikulas Patocka 	 * to the real underlying devices.
254f9ab94ceSMikulas Patocka 	 */
25555a62eefSAlasdair G Kergon 	unsigned num_flush_bios;
256f9ab94ceSMikulas Patocka 
2575ae89a87SMike Snitzer 	/*
25855a62eefSAlasdair G Kergon 	 * The number of discard bios that will be submitted to the target.
25955a62eefSAlasdair G Kergon 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
2605ae89a87SMike Snitzer 	 */
26155a62eefSAlasdair G Kergon 	unsigned num_discard_bios;
2625ae89a87SMike Snitzer 
263d54eaa5aSMike Snitzer 	/*
26455a62eefSAlasdair G Kergon 	 * The number of WRITE SAME bios that will be submitted to the target.
26555a62eefSAlasdair G Kergon 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
266d54eaa5aSMike Snitzer 	 */
26755a62eefSAlasdair G Kergon 	unsigned num_write_same_bios;
268d54eaa5aSMike Snitzer 
269c0820cf5SMikulas Patocka 	/*
27030187e1dSMike Snitzer 	 * The minimum number of extra bytes allocated in each io for the
27130187e1dSMike Snitzer 	 * target to use.
272c0820cf5SMikulas Patocka 	 */
27330187e1dSMike Snitzer 	unsigned per_io_data_size;
274c0820cf5SMikulas Patocka 
275b0d8ed4dSAlasdair G Kergon 	/*
276b0d8ed4dSAlasdair G Kergon 	 * If defined, this function is called to find out how many
277b0d8ed4dSAlasdair G Kergon 	 * duplicate bios should be sent to the target when writing
278b0d8ed4dSAlasdair G Kergon 	 * data.
279b0d8ed4dSAlasdair G Kergon 	 */
280b0d8ed4dSAlasdair G Kergon 	dm_num_write_bios_fn num_write_bios;
281b0d8ed4dSAlasdair G Kergon 
2821da177e4SLinus Torvalds 	/* target specific data */
2831da177e4SLinus Torvalds 	void *private;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	/* Used to provide an error string from the ctr */
2861da177e4SLinus Torvalds 	char *error;
2874c259327SMike Snitzer 
2884c259327SMike Snitzer 	/*
2890e9c24edSJoe Thornber 	 * Set if this target needs to receive flushes regardless of
2900e9c24edSJoe Thornber 	 * whether or not its underlying devices have support.
2910e9c24edSJoe Thornber 	 */
2920e9c24edSJoe Thornber 	bool flush_supported:1;
2930e9c24edSJoe Thornber 
2940e9c24edSJoe Thornber 	/*
2954c259327SMike Snitzer 	 * Set if this target needs to receive discards regardless of
2964c259327SMike Snitzer 	 * whether or not its underlying devices have support.
2974c259327SMike Snitzer 	 */
2980ac55489SAlasdair G Kergon 	bool discards_supported:1;
299983c7db3SMilan Broz 
300983c7db3SMilan Broz 	/*
30155a62eefSAlasdair G Kergon 	 * Set if the target required discard bios to be split
3027acf0277SMikulas Patocka 	 * on max_io_len boundary.
3037acf0277SMikulas Patocka 	 */
30455a62eefSAlasdair G Kergon 	bool split_discard_bios:1;
3057acf0277SMikulas Patocka 
3067acf0277SMikulas Patocka 	/*
307983c7db3SMilan Broz 	 * Set if this target does not return zeroes on discarded blocks.
308983c7db3SMilan Broz 	 */
3090ac55489SAlasdair G Kergon 	bool discard_zeroes_data_unsupported:1;
3101da177e4SLinus Torvalds };
3111da177e4SLinus Torvalds 
3129d357b07SNeilBrown /* Each target can link one of these into the table */
3139d357b07SNeilBrown struct dm_target_callbacks {
3149d357b07SNeilBrown 	struct list_head list;
3159d357b07SNeilBrown 	int (*congested_fn) (struct dm_target_callbacks *, int);
3169d357b07SNeilBrown };
3179d357b07SNeilBrown 
318c0820cf5SMikulas Patocka /*
319c0820cf5SMikulas Patocka  * For bio-based dm.
320c0820cf5SMikulas Patocka  * One of these is allocated for each bio.
321c0820cf5SMikulas Patocka  * This structure shouldn't be touched directly by target drivers.
322c0820cf5SMikulas Patocka  * It is here so that we can inline dm_per_bio_data and
323c0820cf5SMikulas Patocka  * dm_bio_from_per_bio_data
324c0820cf5SMikulas Patocka  */
325c0820cf5SMikulas Patocka struct dm_target_io {
326c0820cf5SMikulas Patocka 	struct dm_io *io;
327c0820cf5SMikulas Patocka 	struct dm_target *ti;
32855a62eefSAlasdair G Kergon 	unsigned target_bio_nr;
3291dd40c3eSMikulas Patocka 	unsigned *len_ptr;
330c0820cf5SMikulas Patocka 	struct bio clone;
331c0820cf5SMikulas Patocka };
332c0820cf5SMikulas Patocka 
333c0820cf5SMikulas Patocka static inline void *dm_per_bio_data(struct bio *bio, size_t data_size)
334c0820cf5SMikulas Patocka {
335c0820cf5SMikulas Patocka 	return (char *)bio - offsetof(struct dm_target_io, clone) - data_size;
336c0820cf5SMikulas Patocka }
337c0820cf5SMikulas Patocka 
338c0820cf5SMikulas Patocka static inline struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size)
339c0820cf5SMikulas Patocka {
340c0820cf5SMikulas Patocka 	return (struct bio *)((char *)data + data_size + offsetof(struct dm_target_io, clone));
341c0820cf5SMikulas Patocka }
342c0820cf5SMikulas Patocka 
34355a62eefSAlasdair G Kergon static inline unsigned dm_bio_get_target_bio_nr(const struct bio *bio)
344ddbd658fSMikulas Patocka {
34555a62eefSAlasdair G Kergon 	return container_of(bio, struct dm_target_io, clone)->target_bio_nr;
346ddbd658fSMikulas Patocka }
347ddbd658fSMikulas Patocka 
3481da177e4SLinus Torvalds int dm_register_target(struct target_type *t);
34910d3bd09SMikulas Patocka void dm_unregister_target(struct target_type *t);
35017b2f66fSAlasdair G Kergon 
351498f0103SMike Snitzer /*
352498f0103SMike Snitzer  * Target argument parsing.
353498f0103SMike Snitzer  */
354498f0103SMike Snitzer struct dm_arg_set {
355498f0103SMike Snitzer 	unsigned argc;
356498f0103SMike Snitzer 	char **argv;
357498f0103SMike Snitzer };
358498f0103SMike Snitzer 
359498f0103SMike Snitzer /*
360498f0103SMike Snitzer  * The minimum and maximum value of a numeric argument, together with
361498f0103SMike Snitzer  * the error message to use if the number is found to be outside that range.
362498f0103SMike Snitzer  */
363498f0103SMike Snitzer struct dm_arg {
364498f0103SMike Snitzer 	unsigned min;
365498f0103SMike Snitzer 	unsigned max;
366498f0103SMike Snitzer 	char *error;
367498f0103SMike Snitzer };
368498f0103SMike Snitzer 
369498f0103SMike Snitzer /*
370498f0103SMike Snitzer  * Validate the next argument, either returning it as *value or, if invalid,
371498f0103SMike Snitzer  * returning -EINVAL and setting *error.
372498f0103SMike Snitzer  */
373498f0103SMike Snitzer int dm_read_arg(struct dm_arg *arg, struct dm_arg_set *arg_set,
374498f0103SMike Snitzer 		unsigned *value, char **error);
375498f0103SMike Snitzer 
376498f0103SMike Snitzer /*
377498f0103SMike Snitzer  * Process the next argument as the start of a group containing between
378498f0103SMike Snitzer  * arg->min and arg->max further arguments. Either return the size as
379498f0103SMike Snitzer  * *num_args or, if invalid, return -EINVAL and set *error.
380498f0103SMike Snitzer  */
381498f0103SMike Snitzer int dm_read_arg_group(struct dm_arg *arg, struct dm_arg_set *arg_set,
382498f0103SMike Snitzer 		      unsigned *num_args, char **error);
383498f0103SMike Snitzer 
384498f0103SMike Snitzer /*
385498f0103SMike Snitzer  * Return the current argument and shift to the next.
386498f0103SMike Snitzer  */
387498f0103SMike Snitzer const char *dm_shift_arg(struct dm_arg_set *as);
388498f0103SMike Snitzer 
389498f0103SMike Snitzer /*
390498f0103SMike Snitzer  * Move through num_args arguments.
391498f0103SMike Snitzer  */
392498f0103SMike Snitzer void dm_consume_args(struct dm_arg_set *as, unsigned num_args);
393498f0103SMike Snitzer 
39417b2f66fSAlasdair G Kergon /*-----------------------------------------------------------------
39517b2f66fSAlasdair G Kergon  * Functions for creating and manipulating mapped devices.
39617b2f66fSAlasdair G Kergon  * Drop the reference with dm_put when you finish with the object.
39717b2f66fSAlasdair G Kergon  *---------------------------------------------------------------*/
39817b2f66fSAlasdair G Kergon 
39917b2f66fSAlasdair G Kergon /*
40017b2f66fSAlasdair G Kergon  * DM_ANY_MINOR chooses the next available minor number.
40117b2f66fSAlasdair G Kergon  */
40217b2f66fSAlasdair G Kergon #define DM_ANY_MINOR (-1)
40317b2f66fSAlasdair G Kergon int dm_create(int minor, struct mapped_device **md);
40417b2f66fSAlasdair G Kergon 
40517b2f66fSAlasdair G Kergon /*
40617b2f66fSAlasdair G Kergon  * Reference counting for md.
40717b2f66fSAlasdair G Kergon  */
40817b2f66fSAlasdair G Kergon struct mapped_device *dm_get_md(dev_t dev);
40917b2f66fSAlasdair G Kergon void dm_get(struct mapped_device *md);
41009ee96b2SMikulas Patocka int dm_hold(struct mapped_device *md);
41117b2f66fSAlasdair G Kergon void dm_put(struct mapped_device *md);
41217b2f66fSAlasdair G Kergon 
41317b2f66fSAlasdair G Kergon /*
41417b2f66fSAlasdair G Kergon  * An arbitrary pointer may be stored alongside a mapped device.
41517b2f66fSAlasdair G Kergon  */
41617b2f66fSAlasdair G Kergon void dm_set_mdptr(struct mapped_device *md, void *ptr);
41717b2f66fSAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md);
41817b2f66fSAlasdair G Kergon 
41917b2f66fSAlasdair G Kergon /*
42017b2f66fSAlasdair G Kergon  * A device can still be used while suspended, but I/O is deferred.
42117b2f66fSAlasdair G Kergon  */
422a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags);
42317b2f66fSAlasdair G Kergon int dm_resume(struct mapped_device *md);
42417b2f66fSAlasdair G Kergon 
42517b2f66fSAlasdair G Kergon /*
42617b2f66fSAlasdair G Kergon  * Event functions.
42717b2f66fSAlasdair G Kergon  */
42817b2f66fSAlasdair G Kergon uint32_t dm_get_event_nr(struct mapped_device *md);
42917b2f66fSAlasdair G Kergon int dm_wait_event(struct mapped_device *md, int event_nr);
4307a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md);
4317a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist);
43217b2f66fSAlasdair G Kergon 
43317b2f66fSAlasdair G Kergon /*
43417b2f66fSAlasdair G Kergon  * Info functions.
43517b2f66fSAlasdair G Kergon  */
43672d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md);
43796a1f7dbSMike Anderson int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid);
43817b2f66fSAlasdair G Kergon struct gendisk *dm_disk(struct mapped_device *md);
43964dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti);
4402e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti);
4411dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors);
442cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq);
44317b2f66fSAlasdair G Kergon 
444f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md);
445f84cb8a4SMike Snitzer 
44617b2f66fSAlasdair G Kergon /*
44717b2f66fSAlasdair G Kergon  * Geometry functions.
44817b2f66fSAlasdair G Kergon  */
44917b2f66fSAlasdair G Kergon int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo);
45017b2f66fSAlasdair G Kergon int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo);
45117b2f66fSAlasdair G Kergon 
45217b2f66fSAlasdair G Kergon /*-----------------------------------------------------------------
45317b2f66fSAlasdair G Kergon  * Functions for manipulating device-mapper tables.
45417b2f66fSAlasdair G Kergon  *---------------------------------------------------------------*/
45517b2f66fSAlasdair G Kergon 
45617b2f66fSAlasdair G Kergon /*
45717b2f66fSAlasdair G Kergon  * First create an empty table.
45817b2f66fSAlasdair G Kergon  */
459aeb5d727SAl Viro int dm_table_create(struct dm_table **result, fmode_t mode,
46017b2f66fSAlasdair G Kergon 		    unsigned num_targets, struct mapped_device *md);
46117b2f66fSAlasdair G Kergon 
46217b2f66fSAlasdair G Kergon /*
46317b2f66fSAlasdair G Kergon  * Then call this once for each target.
46417b2f66fSAlasdair G Kergon  */
46517b2f66fSAlasdair G Kergon int dm_table_add_target(struct dm_table *t, const char *type,
46617b2f66fSAlasdair G Kergon 			sector_t start, sector_t len, char *params);
46717b2f66fSAlasdair G Kergon 
46817b2f66fSAlasdair G Kergon /*
4699d357b07SNeilBrown  * Target_ctr should call this if it needs to add any callbacks.
4709d357b07SNeilBrown  */
4719d357b07SNeilBrown void dm_table_add_target_callbacks(struct dm_table *t, struct dm_target_callbacks *cb);
4729d357b07SNeilBrown 
4739d357b07SNeilBrown /*
474e83068a5SMike Snitzer  * Target can use this to set the table's type.
475e83068a5SMike Snitzer  * Can only ever be called from a target's ctr.
476e83068a5SMike Snitzer  * Useful for "hybrid" target (supports both bio-based
477e83068a5SMike Snitzer  * and request-based).
478e83068a5SMike Snitzer  */
479e83068a5SMike Snitzer void dm_table_set_type(struct dm_table *t, unsigned type);
480e83068a5SMike Snitzer 
481e83068a5SMike Snitzer /*
48217b2f66fSAlasdair G Kergon  * Finally call this to make the table ready for use.
48317b2f66fSAlasdair G Kergon  */
48417b2f66fSAlasdair G Kergon int dm_table_complete(struct dm_table *t);
48517b2f66fSAlasdair G Kergon 
48617b2f66fSAlasdair G Kergon /*
487542f9038SMike Snitzer  * Target may require that it is never sent I/O larger than len.
488542f9038SMike Snitzer  */
489542f9038SMike Snitzer int __must_check dm_set_target_max_io_len(struct dm_target *ti, sector_t len);
490542f9038SMike Snitzer 
491542f9038SMike Snitzer /*
49217b2f66fSAlasdair G Kergon  * Table reference counting.
49317b2f66fSAlasdair G Kergon  */
49483d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx);
49583d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx);
49683d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md);
49717b2f66fSAlasdair G Kergon 
49817b2f66fSAlasdair G Kergon /*
49917b2f66fSAlasdair G Kergon  * Queries
50017b2f66fSAlasdair G Kergon  */
50117b2f66fSAlasdair G Kergon sector_t dm_table_get_size(struct dm_table *t);
50217b2f66fSAlasdair G Kergon unsigned int dm_table_get_num_targets(struct dm_table *t);
503aeb5d727SAl Viro fmode_t dm_table_get_mode(struct dm_table *t);
50417b2f66fSAlasdair G Kergon struct mapped_device *dm_table_get_md(struct dm_table *t);
50517b2f66fSAlasdair G Kergon 
50617b2f66fSAlasdair G Kergon /*
50717b2f66fSAlasdair G Kergon  * Trigger an event.
50817b2f66fSAlasdair G Kergon  */
50917b2f66fSAlasdair G Kergon void dm_table_event(struct dm_table *t);
51017b2f66fSAlasdair G Kergon 
51117b2f66fSAlasdair G Kergon /*
5129974fa2cSMike Snitzer  * Run the queue for request-based targets.
5139974fa2cSMike Snitzer  */
5149974fa2cSMike Snitzer void dm_table_run_md_queue_async(struct dm_table *t);
5159974fa2cSMike Snitzer 
5169974fa2cSMike Snitzer /*
51717b2f66fSAlasdair G Kergon  * The device must be suspended before calling this method.
518042d2a9bSAlasdair G Kergon  * Returns the previous table, which the caller must destroy.
51917b2f66fSAlasdair G Kergon  */
520042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md,
521042d2a9bSAlasdair G Kergon 			       struct dm_table *t);
52217b2f66fSAlasdair G Kergon 
52354160904SMikulas Patocka /*
52454160904SMikulas Patocka  * A wrapper around vmalloc.
52554160904SMikulas Patocka  */
52654160904SMikulas Patocka void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size);
52754160904SMikulas Patocka 
5280da336e5SAlasdair G Kergon /*-----------------------------------------------------------------
5290da336e5SAlasdair G Kergon  * Macros.
5300da336e5SAlasdair G Kergon  *---------------------------------------------------------------*/
5310da336e5SAlasdair G Kergon #define DM_NAME "device-mapper"
5320da336e5SAlasdair G Kergon 
53371a16736SNamhyung Kim #ifdef CONFIG_PRINTK
53471a16736SNamhyung Kim extern struct ratelimit_state dm_ratelimit_state;
53571a16736SNamhyung Kim 
53671a16736SNamhyung Kim #define dm_ratelimit()	__ratelimit(&dm_ratelimit_state)
53771a16736SNamhyung Kim #else
53871a16736SNamhyung Kim #define dm_ratelimit()	0
53971a16736SNamhyung Kim #endif
54071a16736SNamhyung Kim 
54110d3bd09SMikulas Patocka #define DMCRIT(f, arg...) \
54210d3bd09SMikulas Patocka 	printk(KERN_CRIT DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
54310d3bd09SMikulas Patocka 
5440da336e5SAlasdair G Kergon #define DMERR(f, arg...) \
5450da336e5SAlasdair G Kergon 	printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
5460da336e5SAlasdair G Kergon #define DMERR_LIMIT(f, arg...) \
5470da336e5SAlasdair G Kergon 	do { \
54871a16736SNamhyung Kim 		if (dm_ratelimit())	\
5490da336e5SAlasdair G Kergon 			printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " \
5500da336e5SAlasdair G Kergon 			       f "\n", ## arg); \
5510da336e5SAlasdair G Kergon 	} while (0)
5520da336e5SAlasdair G Kergon 
5530da336e5SAlasdair G Kergon #define DMWARN(f, arg...) \
5540da336e5SAlasdair G Kergon 	printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
5550da336e5SAlasdair G Kergon #define DMWARN_LIMIT(f, arg...) \
5560da336e5SAlasdair G Kergon 	do { \
55771a16736SNamhyung Kim 		if (dm_ratelimit())	\
5580da336e5SAlasdair G Kergon 			printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " \
5590da336e5SAlasdair G Kergon 			       f "\n", ## arg); \
5600da336e5SAlasdair G Kergon 	} while (0)
5610da336e5SAlasdair G Kergon 
5620da336e5SAlasdair G Kergon #define DMINFO(f, arg...) \
5630da336e5SAlasdair G Kergon 	printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
5640da336e5SAlasdair G Kergon #define DMINFO_LIMIT(f, arg...) \
5650da336e5SAlasdair G Kergon 	do { \
56671a16736SNamhyung Kim 		if (dm_ratelimit())	\
5670da336e5SAlasdair G Kergon 			printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f \
5680da336e5SAlasdair G Kergon 			       "\n", ## arg); \
5690da336e5SAlasdair G Kergon 	} while (0)
5700da336e5SAlasdair G Kergon 
5710da336e5SAlasdair G Kergon #ifdef CONFIG_DM_DEBUG
5720da336e5SAlasdair G Kergon #  define DMDEBUG(f, arg...) \
5730da336e5SAlasdair G Kergon 	printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX " DEBUG: " f "\n", ## arg)
5740da336e5SAlasdair G Kergon #  define DMDEBUG_LIMIT(f, arg...) \
5750da336e5SAlasdair G Kergon 	do { \
57671a16736SNamhyung Kim 		if (dm_ratelimit())	\
5770da336e5SAlasdair G Kergon 			printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX ": " f \
5780da336e5SAlasdair G Kergon 			       "\n", ## arg); \
5790da336e5SAlasdair G Kergon 	} while (0)
5800da336e5SAlasdair G Kergon #else
5810da336e5SAlasdair G Kergon #  define DMDEBUG(f, arg...) do {} while (0)
5820da336e5SAlasdair G Kergon #  define DMDEBUG_LIMIT(f, arg...) do {} while (0)
5830da336e5SAlasdair G Kergon #endif
5840da336e5SAlasdair G Kergon 
5850da336e5SAlasdair G Kergon #define DMEMIT(x...) sz += ((sz >= maxlen) ? \
5860da336e5SAlasdair G Kergon 			  0 : scnprintf(result + sz, maxlen - sz, x))
5870da336e5SAlasdair G Kergon 
5880da336e5SAlasdair G Kergon #define SECTOR_SHIFT 9
5890da336e5SAlasdair G Kergon 
5900da336e5SAlasdair G Kergon /*
5910da336e5SAlasdair G Kergon  * Definitions of return values from target end_io function.
5920da336e5SAlasdair G Kergon  */
5930da336e5SAlasdair G Kergon #define DM_ENDIO_INCOMPLETE	1
5940da336e5SAlasdair G Kergon #define DM_ENDIO_REQUEUE	2
5950da336e5SAlasdair G Kergon 
5960da336e5SAlasdair G Kergon /*
5970da336e5SAlasdair G Kergon  * Definitions of return values from target map function.
5980da336e5SAlasdair G Kergon  */
5990da336e5SAlasdair G Kergon #define DM_MAPIO_SUBMITTED	0
6000da336e5SAlasdair G Kergon #define DM_MAPIO_REMAPPED	1
6010da336e5SAlasdair G Kergon #define DM_MAPIO_REQUEUE	DM_ENDIO_REQUEUE
602a8ac51e4SMike Snitzer #define DM_MAPIO_DELAY_REQUEUE	3
6030da336e5SAlasdair G Kergon 
604fd2ed4d2SMikulas Patocka #define dm_sector_div64(x, y)( \
605fd2ed4d2SMikulas Patocka { \
606fd2ed4d2SMikulas Patocka 	u64 _res; \
607fd2ed4d2SMikulas Patocka 	(x) = div64_u64_rem(x, y, &_res); \
608fd2ed4d2SMikulas Patocka 	_res; \
609fd2ed4d2SMikulas Patocka } \
610fd2ed4d2SMikulas Patocka )
611fd2ed4d2SMikulas Patocka 
6120da336e5SAlasdair G Kergon /*
6130da336e5SAlasdair G Kergon  * Ceiling(n / sz)
6140da336e5SAlasdair G Kergon  */
6150da336e5SAlasdair G Kergon #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz))
6160da336e5SAlasdair G Kergon 
6170da336e5SAlasdair G Kergon #define dm_sector_div_up(n, sz) ( \
6180da336e5SAlasdair G Kergon { \
6190da336e5SAlasdair G Kergon 	sector_t _r = ((n) + (sz) - 1); \
6200da336e5SAlasdair G Kergon 	sector_div(_r, (sz)); \
6210da336e5SAlasdair G Kergon 	_r; \
6220da336e5SAlasdair G Kergon } \
6230da336e5SAlasdair G Kergon )
6240da336e5SAlasdair G Kergon 
6250da336e5SAlasdair G Kergon /*
6260da336e5SAlasdair G Kergon  * ceiling(n / size) * size
6270da336e5SAlasdair G Kergon  */
6280da336e5SAlasdair G Kergon #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
6290da336e5SAlasdair G Kergon 
630d63a5ce3SMikulas Patocka #define dm_array_too_big(fixed, obj, num) \
631d63a5ce3SMikulas Patocka 	((num) > (UINT_MAX - (fixed)) / (obj))
632d63a5ce3SMikulas Patocka 
63356a67df7SMike Snitzer /*
63456a67df7SMike Snitzer  * Sector offset taken relative to the start of the target instead of
63556a67df7SMike Snitzer  * relative to the start of the device.
63656a67df7SMike Snitzer  */
63756a67df7SMike Snitzer #define dm_target_offset(ti, sector) ((sector) - (ti)->begin)
63856a67df7SMike Snitzer 
6390da336e5SAlasdair G Kergon static inline sector_t to_sector(unsigned long n)
6400da336e5SAlasdair G Kergon {
6410da336e5SAlasdair G Kergon 	return (n >> SECTOR_SHIFT);
6420da336e5SAlasdair G Kergon }
6430da336e5SAlasdair G Kergon 
6440da336e5SAlasdair G Kergon static inline unsigned long to_bytes(sector_t n)
6450da336e5SAlasdair G Kergon {
6460da336e5SAlasdair G Kergon 	return (n << SECTOR_SHIFT);
6470da336e5SAlasdair G Kergon }
6480da336e5SAlasdair G Kergon 
6491da177e4SLinus Torvalds #endif	/* _LINUX_DEVICE_MAPPER_H */
650