xref: /openbmc/linux/include/linux/device-mapper.h (revision e5863d9ad754926e7d3f38b43ac8bd48ef73b097)
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 
221da177e4SLinus Torvalds typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE } status_type_t;
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds union map_info {
251da177e4SLinus Torvalds 	void *ptr;
261da177e4SLinus Torvalds };
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds /*
291da177e4SLinus Torvalds  * In the constructor the target parameter will already have the
301da177e4SLinus Torvalds  * table, type, begin and len fields filled in.
311da177e4SLinus Torvalds  */
321da177e4SLinus Torvalds typedef int (*dm_ctr_fn) (struct dm_target *target,
331da177e4SLinus Torvalds 			  unsigned int argc, char **argv);
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds /*
361da177e4SLinus Torvalds  * The destructor doesn't need to free the dm_target, just
371da177e4SLinus Torvalds  * anything hidden ti->private.
381da177e4SLinus Torvalds  */
391da177e4SLinus Torvalds typedef void (*dm_dtr_fn) (struct dm_target *ti);
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds /*
421da177e4SLinus Torvalds  * The map function must return:
431da177e4SLinus Torvalds  * < 0: error
441da177e4SLinus Torvalds  * = 0: The target will handle the io by resubmitting it later
4545cbcd79SKiyoshi Ueda  * = 1: simple remap complete
462e93ccc1SKiyoshi Ueda  * = 2: The target wants to push back the io
471da177e4SLinus Torvalds  */
487de3ee57SMikulas Patocka typedef int (*dm_map_fn) (struct dm_target *ti, struct bio *bio);
497d76345dSKiyoshi Ueda typedef int (*dm_map_request_fn) (struct dm_target *ti, struct request *clone,
507d76345dSKiyoshi Ueda 				  union map_info *map_context);
51*e5863d9aSMike Snitzer typedef int (*dm_clone_and_map_request_fn) (struct dm_target *ti,
52*e5863d9aSMike Snitzer 					    struct request *rq,
53*e5863d9aSMike Snitzer 					    union map_info *map_context,
54*e5863d9aSMike Snitzer 					    struct request **clone);
55*e5863d9aSMike Snitzer typedef void (*dm_release_clone_request_fn) (struct request *clone);
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds /*
581da177e4SLinus Torvalds  * Returns:
591da177e4SLinus Torvalds  * < 0 : error (currently ignored)
601da177e4SLinus Torvalds  * 0   : ended successfully
611da177e4SLinus Torvalds  * 1   : for some reason the io has still not completed (eg,
621da177e4SLinus Torvalds  *       multipath target might want to requeue a failed io).
632e93ccc1SKiyoshi Ueda  * 2   : The target wants to push back the io
641da177e4SLinus Torvalds  */
651da177e4SLinus Torvalds typedef int (*dm_endio_fn) (struct dm_target *ti,
667de3ee57SMikulas Patocka 			    struct bio *bio, int error);
677d76345dSKiyoshi Ueda typedef int (*dm_request_endio_fn) (struct dm_target *ti,
687d76345dSKiyoshi Ueda 				    struct request *clone, int error,
697d76345dSKiyoshi Ueda 				    union map_info *map_context);
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds typedef void (*dm_presuspend_fn) (struct dm_target *ti);
72d67ee213SMike Snitzer typedef void (*dm_presuspend_undo_fn) (struct dm_target *ti);
731da177e4SLinus Torvalds typedef void (*dm_postsuspend_fn) (struct dm_target *ti);
748757b776SMilan Broz typedef int (*dm_preresume_fn) (struct dm_target *ti);
751da177e4SLinus Torvalds typedef void (*dm_resume_fn) (struct dm_target *ti);
761da177e4SLinus Torvalds 
77fd7c092eSMikulas Patocka typedef void (*dm_status_fn) (struct dm_target *ti, status_type_t status_type,
781f4e0ff0SAlasdair G Kergon 			      unsigned status_flags, char *result, unsigned maxlen);
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds typedef int (*dm_message_fn) (struct dm_target *ti, unsigned argc, char **argv);
811da177e4SLinus Torvalds 
82647b3d00SAl Viro typedef int (*dm_ioctl_fn) (struct dm_target *ti, unsigned int cmd,
83aa129a22SMilan Broz 			    unsigned long arg);
84aa129a22SMilan Broz 
85f6fccb12SMilan Broz typedef int (*dm_merge_fn) (struct dm_target *ti, struct bvec_merge_data *bvm,
86f6fccb12SMilan Broz 			    struct bio_vec *biovec, int max_size);
87f6fccb12SMilan Broz 
88058ce5caSAlasdair G Kergon /*
89058ce5caSAlasdair G Kergon  * These iteration functions are typically used to check (and combine)
90058ce5caSAlasdair G Kergon  * properties of underlying devices.
91058ce5caSAlasdair G Kergon  * E.g. Does at least one underlying device support flush?
92058ce5caSAlasdair G Kergon  *      Does any underlying device not support WRITE_SAME?
93058ce5caSAlasdair G Kergon  *
94058ce5caSAlasdair G Kergon  * The callout function is called once for each contiguous section of
95058ce5caSAlasdair G Kergon  * an underlying device.  State can be maintained in *data.
96058ce5caSAlasdair G Kergon  * Return non-zero to stop iterating through any further devices.
97058ce5caSAlasdair G Kergon  */
98af4874e0SMike Snitzer typedef int (*iterate_devices_callout_fn) (struct dm_target *ti,
99af4874e0SMike Snitzer 					   struct dm_dev *dev,
1005dea271bSMike Snitzer 					   sector_t start, sector_t len,
101af4874e0SMike Snitzer 					   void *data);
102af4874e0SMike Snitzer 
103058ce5caSAlasdair G Kergon /*
104058ce5caSAlasdair G Kergon  * This function must iterate through each section of device used by the
105058ce5caSAlasdair G Kergon  * target until it encounters a non-zero return code, which it then returns.
106058ce5caSAlasdair G Kergon  * Returns zero if no callout returned non-zero.
107058ce5caSAlasdair G Kergon  */
108af4874e0SMike Snitzer typedef int (*dm_iterate_devices_fn) (struct dm_target *ti,
109af4874e0SMike Snitzer 				      iterate_devices_callout_fn fn,
110af4874e0SMike Snitzer 				      void *data);
111af4874e0SMike Snitzer 
11240bea431SMike Snitzer typedef void (*dm_io_hints_fn) (struct dm_target *ti,
11340bea431SMike Snitzer 				struct queue_limits *limits);
11440bea431SMike Snitzer 
1157d76345dSKiyoshi Ueda /*
1167d76345dSKiyoshi Ueda  * Returns:
1177d76345dSKiyoshi Ueda  *    0: The target can handle the next I/O immediately.
1187d76345dSKiyoshi Ueda  *    1: The target can't handle the next I/O immediately.
1197d76345dSKiyoshi Ueda  */
1207d76345dSKiyoshi Ueda typedef int (*dm_busy_fn) (struct dm_target *ti);
1217d76345dSKiyoshi Ueda 
1221da177e4SLinus Torvalds void dm_error(const char *message);
1231da177e4SLinus Torvalds 
12482b1519bSMikulas Patocka struct dm_dev {
12582b1519bSMikulas Patocka 	struct block_device *bdev;
126aeb5d727SAl Viro 	fmode_t mode;
12782b1519bSMikulas Patocka 	char name[16];
12882b1519bSMikulas Patocka };
12982b1519bSMikulas Patocka 
1303cb40214SBryn Reeves /*
1311da177e4SLinus Torvalds  * Constructors should call these functions to ensure destination devices
1321da177e4SLinus Torvalds  * are opened/closed correctly.
1331da177e4SLinus Torvalds  */
1348215d6ecSNikanth Karthikesan int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
1358215d6ecSNikanth Karthikesan 		  struct dm_dev **result);
1361da177e4SLinus Torvalds void dm_put_device(struct dm_target *ti, struct dm_dev *d);
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds /*
1391da177e4SLinus Torvalds  * Information about a target type
1401da177e4SLinus Torvalds  */
141ab4c1424SAndi Kleen 
1421da177e4SLinus Torvalds struct target_type {
143ab4c1424SAndi Kleen 	uint64_t features;
1441da177e4SLinus Torvalds 	const char *name;
1451da177e4SLinus Torvalds 	struct module *module;
1461da177e4SLinus Torvalds 	unsigned version[3];
1471da177e4SLinus Torvalds 	dm_ctr_fn ctr;
1481da177e4SLinus Torvalds 	dm_dtr_fn dtr;
1491da177e4SLinus Torvalds 	dm_map_fn map;
1507d76345dSKiyoshi Ueda 	dm_map_request_fn map_rq;
151*e5863d9aSMike Snitzer 	dm_clone_and_map_request_fn clone_and_map_rq;
152*e5863d9aSMike Snitzer 	dm_release_clone_request_fn release_clone_rq;
1531da177e4SLinus Torvalds 	dm_endio_fn end_io;
1547d76345dSKiyoshi Ueda 	dm_request_endio_fn rq_end_io;
1551da177e4SLinus Torvalds 	dm_presuspend_fn presuspend;
156d67ee213SMike Snitzer 	dm_presuspend_undo_fn presuspend_undo;
1571da177e4SLinus Torvalds 	dm_postsuspend_fn postsuspend;
1588757b776SMilan Broz 	dm_preresume_fn preresume;
1591da177e4SLinus Torvalds 	dm_resume_fn resume;
1601da177e4SLinus Torvalds 	dm_status_fn status;
1611da177e4SLinus Torvalds 	dm_message_fn message;
162aa129a22SMilan Broz 	dm_ioctl_fn ioctl;
163f6fccb12SMilan Broz 	dm_merge_fn merge;
1647d76345dSKiyoshi Ueda 	dm_busy_fn busy;
165af4874e0SMike Snitzer 	dm_iterate_devices_fn iterate_devices;
16640bea431SMike Snitzer 	dm_io_hints_fn io_hints;
16745194e4fSCheng Renquan 
16845194e4fSCheng Renquan 	/* For internal device-mapper use. */
16945194e4fSCheng Renquan 	struct list_head list;
1701da177e4SLinus Torvalds };
1711da177e4SLinus Torvalds 
1723791e2fcSAlasdair G Kergon /*
1733791e2fcSAlasdair G Kergon  * Target features
1743791e2fcSAlasdair G Kergon  */
1753791e2fcSAlasdair G Kergon 
1763791e2fcSAlasdair G Kergon /*
1773791e2fcSAlasdair G Kergon  * Any table that contains an instance of this target must have only one.
1783791e2fcSAlasdair G Kergon  */
1793791e2fcSAlasdair G Kergon #define DM_TARGET_SINGLETON		0x00000001
1803791e2fcSAlasdair G Kergon #define dm_target_needs_singleton(type)	((type)->features & DM_TARGET_SINGLETON)
1813791e2fcSAlasdair G Kergon 
182cc6cbe14SAlasdair G Kergon /*
183cc6cbe14SAlasdair G Kergon  * Indicates that a target does not support read-only devices.
184cc6cbe14SAlasdair G Kergon  */
185cc6cbe14SAlasdair G Kergon #define DM_TARGET_ALWAYS_WRITEABLE	0x00000002
186cc6cbe14SAlasdair G Kergon #define dm_target_always_writeable(type) \
187cc6cbe14SAlasdair G Kergon 		((type)->features & DM_TARGET_ALWAYS_WRITEABLE)
188cc6cbe14SAlasdair G Kergon 
18936a0456fSAlasdair G Kergon /*
19036a0456fSAlasdair G Kergon  * Any device that contains a table with an instance of this target may never
19136a0456fSAlasdair G Kergon  * have tables containing any different target type.
19236a0456fSAlasdair G Kergon  */
19336a0456fSAlasdair G Kergon #define DM_TARGET_IMMUTABLE		0x00000004
19436a0456fSAlasdair G Kergon #define dm_target_is_immutable(type)	((type)->features & DM_TARGET_IMMUTABLE)
19536a0456fSAlasdair G Kergon 
196b0d8ed4dSAlasdair G Kergon /*
197b0d8ed4dSAlasdair G Kergon  * Some targets need to be sent the same WRITE bio severals times so
198b0d8ed4dSAlasdair G Kergon  * that they can send copies of it to different devices.  This function
199b0d8ed4dSAlasdair G Kergon  * examines any supplied bio and returns the number of copies of it the
200b0d8ed4dSAlasdair G Kergon  * target requires.
201b0d8ed4dSAlasdair G Kergon  */
202b0d8ed4dSAlasdair G Kergon typedef unsigned (*dm_num_write_bios_fn) (struct dm_target *ti, struct bio *bio);
203b0d8ed4dSAlasdair G Kergon 
2041da177e4SLinus Torvalds struct dm_target {
2051da177e4SLinus Torvalds 	struct dm_table *table;
2061da177e4SLinus Torvalds 	struct target_type *type;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	/* target limits */
2091da177e4SLinus Torvalds 	sector_t begin;
2101da177e4SLinus Torvalds 	sector_t len;
2111da177e4SLinus Torvalds 
212542f9038SMike Snitzer 	/* If non-zero, maximum size of I/O submitted to a target. */
213542f9038SMike Snitzer 	uint32_t max_io_len;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	/*
21655a62eefSAlasdair G Kergon 	 * A number of zero-length barrier bios that will be submitted
217f9ab94ceSMikulas Patocka 	 * to the target for the purpose of flushing cache.
218f9ab94ceSMikulas Patocka 	 *
21955a62eefSAlasdair G Kergon 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
22055a62eefSAlasdair G Kergon 	 * It is a responsibility of the target driver to remap these bios
221f9ab94ceSMikulas Patocka 	 * to the real underlying devices.
222f9ab94ceSMikulas Patocka 	 */
22355a62eefSAlasdair G Kergon 	unsigned num_flush_bios;
224f9ab94ceSMikulas Patocka 
2255ae89a87SMike Snitzer 	/*
22655a62eefSAlasdair G Kergon 	 * The number of discard bios that will be submitted to the target.
22755a62eefSAlasdair G Kergon 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
2285ae89a87SMike Snitzer 	 */
22955a62eefSAlasdair G Kergon 	unsigned num_discard_bios;
2305ae89a87SMike Snitzer 
231d54eaa5aSMike Snitzer 	/*
23255a62eefSAlasdair G Kergon 	 * The number of WRITE SAME bios that will be submitted to the target.
23355a62eefSAlasdair G Kergon 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
234d54eaa5aSMike Snitzer 	 */
23555a62eefSAlasdair G Kergon 	unsigned num_write_same_bios;
236d54eaa5aSMike Snitzer 
237c0820cf5SMikulas Patocka 	/*
238c0820cf5SMikulas Patocka 	 * The minimum number of extra bytes allocated in each bio for the
239c0820cf5SMikulas Patocka 	 * target to use.  dm_per_bio_data returns the data location.
240c0820cf5SMikulas Patocka 	 */
241c0820cf5SMikulas Patocka 	unsigned per_bio_data_size;
242c0820cf5SMikulas Patocka 
243b0d8ed4dSAlasdair G Kergon 	/*
244b0d8ed4dSAlasdair G Kergon 	 * If defined, this function is called to find out how many
245b0d8ed4dSAlasdair G Kergon 	 * duplicate bios should be sent to the target when writing
246b0d8ed4dSAlasdair G Kergon 	 * data.
247b0d8ed4dSAlasdair G Kergon 	 */
248b0d8ed4dSAlasdair G Kergon 	dm_num_write_bios_fn num_write_bios;
249b0d8ed4dSAlasdair G Kergon 
2501da177e4SLinus Torvalds 	/* target specific data */
2511da177e4SLinus Torvalds 	void *private;
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	/* Used to provide an error string from the ctr */
2541da177e4SLinus Torvalds 	char *error;
2554c259327SMike Snitzer 
2564c259327SMike Snitzer 	/*
2570e9c24edSJoe Thornber 	 * Set if this target needs to receive flushes regardless of
2580e9c24edSJoe Thornber 	 * whether or not its underlying devices have support.
2590e9c24edSJoe Thornber 	 */
2600e9c24edSJoe Thornber 	bool flush_supported:1;
2610e9c24edSJoe Thornber 
2620e9c24edSJoe Thornber 	/*
2634c259327SMike Snitzer 	 * Set if this target needs to receive discards regardless of
2644c259327SMike Snitzer 	 * whether or not its underlying devices have support.
2654c259327SMike Snitzer 	 */
2660ac55489SAlasdair G Kergon 	bool discards_supported:1;
267983c7db3SMilan Broz 
268983c7db3SMilan Broz 	/*
26955a62eefSAlasdair G Kergon 	 * Set if the target required discard bios to be split
2707acf0277SMikulas Patocka 	 * on max_io_len boundary.
2717acf0277SMikulas Patocka 	 */
27255a62eefSAlasdair G Kergon 	bool split_discard_bios:1;
2737acf0277SMikulas Patocka 
2747acf0277SMikulas Patocka 	/*
275983c7db3SMilan Broz 	 * Set if this target does not return zeroes on discarded blocks.
276983c7db3SMilan Broz 	 */
2770ac55489SAlasdair G Kergon 	bool discard_zeroes_data_unsupported:1;
2781da177e4SLinus Torvalds };
2791da177e4SLinus Torvalds 
2809d357b07SNeilBrown /* Each target can link one of these into the table */
2819d357b07SNeilBrown struct dm_target_callbacks {
2829d357b07SNeilBrown 	struct list_head list;
2839d357b07SNeilBrown 	int (*congested_fn) (struct dm_target_callbacks *, int);
2849d357b07SNeilBrown };
2859d357b07SNeilBrown 
286c0820cf5SMikulas Patocka /*
287c0820cf5SMikulas Patocka  * For bio-based dm.
288c0820cf5SMikulas Patocka  * One of these is allocated for each bio.
289c0820cf5SMikulas Patocka  * This structure shouldn't be touched directly by target drivers.
290c0820cf5SMikulas Patocka  * It is here so that we can inline dm_per_bio_data and
291c0820cf5SMikulas Patocka  * dm_bio_from_per_bio_data
292c0820cf5SMikulas Patocka  */
293c0820cf5SMikulas Patocka struct dm_target_io {
294c0820cf5SMikulas Patocka 	struct dm_io *io;
295c0820cf5SMikulas Patocka 	struct dm_target *ti;
29655a62eefSAlasdair G Kergon 	unsigned target_bio_nr;
2971dd40c3eSMikulas Patocka 	unsigned *len_ptr;
298c0820cf5SMikulas Patocka 	struct bio clone;
299c0820cf5SMikulas Patocka };
300c0820cf5SMikulas Patocka 
301c0820cf5SMikulas Patocka static inline void *dm_per_bio_data(struct bio *bio, size_t data_size)
302c0820cf5SMikulas Patocka {
303c0820cf5SMikulas Patocka 	return (char *)bio - offsetof(struct dm_target_io, clone) - data_size;
304c0820cf5SMikulas Patocka }
305c0820cf5SMikulas Patocka 
306c0820cf5SMikulas Patocka static inline struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size)
307c0820cf5SMikulas Patocka {
308c0820cf5SMikulas Patocka 	return (struct bio *)((char *)data + data_size + offsetof(struct dm_target_io, clone));
309c0820cf5SMikulas Patocka }
310c0820cf5SMikulas Patocka 
31155a62eefSAlasdair G Kergon static inline unsigned dm_bio_get_target_bio_nr(const struct bio *bio)
312ddbd658fSMikulas Patocka {
31355a62eefSAlasdair G Kergon 	return container_of(bio, struct dm_target_io, clone)->target_bio_nr;
314ddbd658fSMikulas Patocka }
315ddbd658fSMikulas Patocka 
3161da177e4SLinus Torvalds int dm_register_target(struct target_type *t);
31710d3bd09SMikulas Patocka void dm_unregister_target(struct target_type *t);
31817b2f66fSAlasdair G Kergon 
319498f0103SMike Snitzer /*
320498f0103SMike Snitzer  * Target argument parsing.
321498f0103SMike Snitzer  */
322498f0103SMike Snitzer struct dm_arg_set {
323498f0103SMike Snitzer 	unsigned argc;
324498f0103SMike Snitzer 	char **argv;
325498f0103SMike Snitzer };
326498f0103SMike Snitzer 
327498f0103SMike Snitzer /*
328498f0103SMike Snitzer  * The minimum and maximum value of a numeric argument, together with
329498f0103SMike Snitzer  * the error message to use if the number is found to be outside that range.
330498f0103SMike Snitzer  */
331498f0103SMike Snitzer struct dm_arg {
332498f0103SMike Snitzer 	unsigned min;
333498f0103SMike Snitzer 	unsigned max;
334498f0103SMike Snitzer 	char *error;
335498f0103SMike Snitzer };
336498f0103SMike Snitzer 
337498f0103SMike Snitzer /*
338498f0103SMike Snitzer  * Validate the next argument, either returning it as *value or, if invalid,
339498f0103SMike Snitzer  * returning -EINVAL and setting *error.
340498f0103SMike Snitzer  */
341498f0103SMike Snitzer int dm_read_arg(struct dm_arg *arg, struct dm_arg_set *arg_set,
342498f0103SMike Snitzer 		unsigned *value, char **error);
343498f0103SMike Snitzer 
344498f0103SMike Snitzer /*
345498f0103SMike Snitzer  * Process the next argument as the start of a group containing between
346498f0103SMike Snitzer  * arg->min and arg->max further arguments. Either return the size as
347498f0103SMike Snitzer  * *num_args or, if invalid, return -EINVAL and set *error.
348498f0103SMike Snitzer  */
349498f0103SMike Snitzer int dm_read_arg_group(struct dm_arg *arg, struct dm_arg_set *arg_set,
350498f0103SMike Snitzer 		      unsigned *num_args, char **error);
351498f0103SMike Snitzer 
352498f0103SMike Snitzer /*
353498f0103SMike Snitzer  * Return the current argument and shift to the next.
354498f0103SMike Snitzer  */
355498f0103SMike Snitzer const char *dm_shift_arg(struct dm_arg_set *as);
356498f0103SMike Snitzer 
357498f0103SMike Snitzer /*
358498f0103SMike Snitzer  * Move through num_args arguments.
359498f0103SMike Snitzer  */
360498f0103SMike Snitzer void dm_consume_args(struct dm_arg_set *as, unsigned num_args);
361498f0103SMike Snitzer 
36217b2f66fSAlasdair G Kergon /*-----------------------------------------------------------------
36317b2f66fSAlasdair G Kergon  * Functions for creating and manipulating mapped devices.
36417b2f66fSAlasdair G Kergon  * Drop the reference with dm_put when you finish with the object.
36517b2f66fSAlasdair G Kergon  *---------------------------------------------------------------*/
36617b2f66fSAlasdair G Kergon 
36717b2f66fSAlasdair G Kergon /*
36817b2f66fSAlasdair G Kergon  * DM_ANY_MINOR chooses the next available minor number.
36917b2f66fSAlasdair G Kergon  */
37017b2f66fSAlasdair G Kergon #define DM_ANY_MINOR (-1)
37117b2f66fSAlasdair G Kergon int dm_create(int minor, struct mapped_device **md);
37217b2f66fSAlasdair G Kergon 
37317b2f66fSAlasdair G Kergon /*
37417b2f66fSAlasdair G Kergon  * Reference counting for md.
37517b2f66fSAlasdair G Kergon  */
37617b2f66fSAlasdair G Kergon struct mapped_device *dm_get_md(dev_t dev);
37717b2f66fSAlasdair G Kergon void dm_get(struct mapped_device *md);
37817b2f66fSAlasdair G Kergon void dm_put(struct mapped_device *md);
37917b2f66fSAlasdair G Kergon 
38017b2f66fSAlasdair G Kergon /*
38117b2f66fSAlasdair G Kergon  * An arbitrary pointer may be stored alongside a mapped device.
38217b2f66fSAlasdair G Kergon  */
38317b2f66fSAlasdair G Kergon void dm_set_mdptr(struct mapped_device *md, void *ptr);
38417b2f66fSAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md);
38517b2f66fSAlasdair G Kergon 
38617b2f66fSAlasdair G Kergon /*
38717b2f66fSAlasdair G Kergon  * A device can still be used while suspended, but I/O is deferred.
38817b2f66fSAlasdair G Kergon  */
389a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags);
39017b2f66fSAlasdair G Kergon int dm_resume(struct mapped_device *md);
39117b2f66fSAlasdair G Kergon 
39217b2f66fSAlasdair G Kergon /*
39317b2f66fSAlasdair G Kergon  * Event functions.
39417b2f66fSAlasdair G Kergon  */
39517b2f66fSAlasdair G Kergon uint32_t dm_get_event_nr(struct mapped_device *md);
39617b2f66fSAlasdair G Kergon int dm_wait_event(struct mapped_device *md, int event_nr);
3977a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md);
3987a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist);
39917b2f66fSAlasdair G Kergon 
40017b2f66fSAlasdair G Kergon /*
40117b2f66fSAlasdair G Kergon  * Info functions.
40217b2f66fSAlasdair G Kergon  */
40372d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md);
40496a1f7dbSMike Anderson int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid);
40517b2f66fSAlasdair G Kergon struct gendisk *dm_disk(struct mapped_device *md);
40664dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti);
4072e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti);
4081dd40c3eSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors);
409cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq);
41017b2f66fSAlasdair G Kergon 
411f84cb8a4SMike Snitzer struct queue_limits *dm_get_queue_limits(struct mapped_device *md);
412f84cb8a4SMike Snitzer 
41317b2f66fSAlasdair G Kergon /*
41417b2f66fSAlasdair G Kergon  * Geometry functions.
41517b2f66fSAlasdair G Kergon  */
41617b2f66fSAlasdair G Kergon int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo);
41717b2f66fSAlasdair G Kergon int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo);
41817b2f66fSAlasdair G Kergon 
41917b2f66fSAlasdair G Kergon /*-----------------------------------------------------------------
42017b2f66fSAlasdair G Kergon  * Functions for manipulating device-mapper tables.
42117b2f66fSAlasdair G Kergon  *---------------------------------------------------------------*/
42217b2f66fSAlasdair G Kergon 
42317b2f66fSAlasdair G Kergon /*
42417b2f66fSAlasdair G Kergon  * First create an empty table.
42517b2f66fSAlasdair G Kergon  */
426aeb5d727SAl Viro int dm_table_create(struct dm_table **result, fmode_t mode,
42717b2f66fSAlasdair G Kergon 		    unsigned num_targets, struct mapped_device *md);
42817b2f66fSAlasdair G Kergon 
42917b2f66fSAlasdair G Kergon /*
43017b2f66fSAlasdair G Kergon  * Then call this once for each target.
43117b2f66fSAlasdair G Kergon  */
43217b2f66fSAlasdair G Kergon int dm_table_add_target(struct dm_table *t, const char *type,
43317b2f66fSAlasdair G Kergon 			sector_t start, sector_t len, char *params);
43417b2f66fSAlasdair G Kergon 
43517b2f66fSAlasdair G Kergon /*
4369d357b07SNeilBrown  * Target_ctr should call this if it needs to add any callbacks.
4379d357b07SNeilBrown  */
4389d357b07SNeilBrown void dm_table_add_target_callbacks(struct dm_table *t, struct dm_target_callbacks *cb);
4399d357b07SNeilBrown 
4409d357b07SNeilBrown /*
44117b2f66fSAlasdair G Kergon  * Finally call this to make the table ready for use.
44217b2f66fSAlasdair G Kergon  */
44317b2f66fSAlasdair G Kergon int dm_table_complete(struct dm_table *t);
44417b2f66fSAlasdair G Kergon 
44517b2f66fSAlasdair G Kergon /*
446542f9038SMike Snitzer  * Target may require that it is never sent I/O larger than len.
447542f9038SMike Snitzer  */
448542f9038SMike Snitzer int __must_check dm_set_target_max_io_len(struct dm_target *ti, sector_t len);
449542f9038SMike Snitzer 
450542f9038SMike Snitzer /*
45117b2f66fSAlasdair G Kergon  * Table reference counting.
45217b2f66fSAlasdair G Kergon  */
45383d5e5b0SMikulas Patocka struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx);
45483d5e5b0SMikulas Patocka void dm_put_live_table(struct mapped_device *md, int srcu_idx);
45583d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md);
45617b2f66fSAlasdair G Kergon 
45717b2f66fSAlasdair G Kergon /*
45817b2f66fSAlasdair G Kergon  * Queries
45917b2f66fSAlasdair G Kergon  */
46017b2f66fSAlasdair G Kergon sector_t dm_table_get_size(struct dm_table *t);
46117b2f66fSAlasdair G Kergon unsigned int dm_table_get_num_targets(struct dm_table *t);
462aeb5d727SAl Viro fmode_t dm_table_get_mode(struct dm_table *t);
46317b2f66fSAlasdair G Kergon struct mapped_device *dm_table_get_md(struct dm_table *t);
46417b2f66fSAlasdair G Kergon 
46517b2f66fSAlasdair G Kergon /*
46617b2f66fSAlasdair G Kergon  * Trigger an event.
46717b2f66fSAlasdair G Kergon  */
46817b2f66fSAlasdair G Kergon void dm_table_event(struct dm_table *t);
46917b2f66fSAlasdair G Kergon 
47017b2f66fSAlasdair G Kergon /*
4719974fa2cSMike Snitzer  * Run the queue for request-based targets.
4729974fa2cSMike Snitzer  */
4739974fa2cSMike Snitzer void dm_table_run_md_queue_async(struct dm_table *t);
4749974fa2cSMike Snitzer 
4759974fa2cSMike Snitzer /*
47617b2f66fSAlasdair G Kergon  * The device must be suspended before calling this method.
477042d2a9bSAlasdair G Kergon  * Returns the previous table, which the caller must destroy.
47817b2f66fSAlasdair G Kergon  */
479042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md,
480042d2a9bSAlasdair G Kergon 			       struct dm_table *t);
48117b2f66fSAlasdair G Kergon 
48254160904SMikulas Patocka /*
48354160904SMikulas Patocka  * A wrapper around vmalloc.
48454160904SMikulas Patocka  */
48554160904SMikulas Patocka void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size);
48654160904SMikulas Patocka 
4870da336e5SAlasdair G Kergon /*-----------------------------------------------------------------
4880da336e5SAlasdair G Kergon  * Macros.
4890da336e5SAlasdair G Kergon  *---------------------------------------------------------------*/
4900da336e5SAlasdair G Kergon #define DM_NAME "device-mapper"
4910da336e5SAlasdair G Kergon 
49271a16736SNamhyung Kim #ifdef CONFIG_PRINTK
49371a16736SNamhyung Kim extern struct ratelimit_state dm_ratelimit_state;
49471a16736SNamhyung Kim 
49571a16736SNamhyung Kim #define dm_ratelimit()	__ratelimit(&dm_ratelimit_state)
49671a16736SNamhyung Kim #else
49771a16736SNamhyung Kim #define dm_ratelimit()	0
49871a16736SNamhyung Kim #endif
49971a16736SNamhyung Kim 
50010d3bd09SMikulas Patocka #define DMCRIT(f, arg...) \
50110d3bd09SMikulas Patocka 	printk(KERN_CRIT DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
50210d3bd09SMikulas Patocka 
5030da336e5SAlasdair G Kergon #define DMERR(f, arg...) \
5040da336e5SAlasdair G Kergon 	printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
5050da336e5SAlasdair G Kergon #define DMERR_LIMIT(f, arg...) \
5060da336e5SAlasdair G Kergon 	do { \
50771a16736SNamhyung Kim 		if (dm_ratelimit())	\
5080da336e5SAlasdair G Kergon 			printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " \
5090da336e5SAlasdair G Kergon 			       f "\n", ## arg); \
5100da336e5SAlasdair G Kergon 	} while (0)
5110da336e5SAlasdair G Kergon 
5120da336e5SAlasdair G Kergon #define DMWARN(f, arg...) \
5130da336e5SAlasdair G Kergon 	printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
5140da336e5SAlasdair G Kergon #define DMWARN_LIMIT(f, arg...) \
5150da336e5SAlasdair G Kergon 	do { \
51671a16736SNamhyung Kim 		if (dm_ratelimit())	\
5170da336e5SAlasdair G Kergon 			printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " \
5180da336e5SAlasdair G Kergon 			       f "\n", ## arg); \
5190da336e5SAlasdair G Kergon 	} while (0)
5200da336e5SAlasdair G Kergon 
5210da336e5SAlasdair G Kergon #define DMINFO(f, arg...) \
5220da336e5SAlasdair G Kergon 	printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
5230da336e5SAlasdair G Kergon #define DMINFO_LIMIT(f, arg...) \
5240da336e5SAlasdair G Kergon 	do { \
52571a16736SNamhyung Kim 		if (dm_ratelimit())	\
5260da336e5SAlasdair G Kergon 			printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f \
5270da336e5SAlasdair G Kergon 			       "\n", ## arg); \
5280da336e5SAlasdair G Kergon 	} while (0)
5290da336e5SAlasdair G Kergon 
5300da336e5SAlasdair G Kergon #ifdef CONFIG_DM_DEBUG
5310da336e5SAlasdair G Kergon #  define DMDEBUG(f, arg...) \
5320da336e5SAlasdair G Kergon 	printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX " DEBUG: " f "\n", ## arg)
5330da336e5SAlasdair G Kergon #  define DMDEBUG_LIMIT(f, arg...) \
5340da336e5SAlasdair G Kergon 	do { \
53571a16736SNamhyung Kim 		if (dm_ratelimit())	\
5360da336e5SAlasdair G Kergon 			printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX ": " f \
5370da336e5SAlasdair G Kergon 			       "\n", ## arg); \
5380da336e5SAlasdair G Kergon 	} while (0)
5390da336e5SAlasdair G Kergon #else
5400da336e5SAlasdair G Kergon #  define DMDEBUG(f, arg...) do {} while (0)
5410da336e5SAlasdair G Kergon #  define DMDEBUG_LIMIT(f, arg...) do {} while (0)
5420da336e5SAlasdair G Kergon #endif
5430da336e5SAlasdair G Kergon 
5440da336e5SAlasdair G Kergon #define DMEMIT(x...) sz += ((sz >= maxlen) ? \
5450da336e5SAlasdair G Kergon 			  0 : scnprintf(result + sz, maxlen - sz, x))
5460da336e5SAlasdair G Kergon 
5470da336e5SAlasdair G Kergon #define SECTOR_SHIFT 9
5480da336e5SAlasdair G Kergon 
5490da336e5SAlasdair G Kergon /*
5500da336e5SAlasdair G Kergon  * Definitions of return values from target end_io function.
5510da336e5SAlasdair G Kergon  */
5520da336e5SAlasdair G Kergon #define DM_ENDIO_INCOMPLETE	1
5530da336e5SAlasdair G Kergon #define DM_ENDIO_REQUEUE	2
5540da336e5SAlasdair G Kergon 
5550da336e5SAlasdair G Kergon /*
5560da336e5SAlasdair G Kergon  * Definitions of return values from target map function.
5570da336e5SAlasdair G Kergon  */
5580da336e5SAlasdair G Kergon #define DM_MAPIO_SUBMITTED	0
5590da336e5SAlasdair G Kergon #define DM_MAPIO_REMAPPED	1
5600da336e5SAlasdair G Kergon #define DM_MAPIO_REQUEUE	DM_ENDIO_REQUEUE
5610da336e5SAlasdair G Kergon 
562fd2ed4d2SMikulas Patocka #define dm_sector_div64(x, y)( \
563fd2ed4d2SMikulas Patocka { \
564fd2ed4d2SMikulas Patocka 	u64 _res; \
565fd2ed4d2SMikulas Patocka 	(x) = div64_u64_rem(x, y, &_res); \
566fd2ed4d2SMikulas Patocka 	_res; \
567fd2ed4d2SMikulas Patocka } \
568fd2ed4d2SMikulas Patocka )
569fd2ed4d2SMikulas Patocka 
5700da336e5SAlasdair G Kergon /*
5710da336e5SAlasdair G Kergon  * Ceiling(n / sz)
5720da336e5SAlasdair G Kergon  */
5730da336e5SAlasdair G Kergon #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz))
5740da336e5SAlasdair G Kergon 
5750da336e5SAlasdair G Kergon #define dm_sector_div_up(n, sz) ( \
5760da336e5SAlasdair G Kergon { \
5770da336e5SAlasdair G Kergon 	sector_t _r = ((n) + (sz) - 1); \
5780da336e5SAlasdair G Kergon 	sector_div(_r, (sz)); \
5790da336e5SAlasdair G Kergon 	_r; \
5800da336e5SAlasdair G Kergon } \
5810da336e5SAlasdair G Kergon )
5820da336e5SAlasdair G Kergon 
5830da336e5SAlasdair G Kergon /*
5840da336e5SAlasdair G Kergon  * ceiling(n / size) * size
5850da336e5SAlasdair G Kergon  */
5860da336e5SAlasdair G Kergon #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
5870da336e5SAlasdair G Kergon 
588d63a5ce3SMikulas Patocka #define dm_array_too_big(fixed, obj, num) \
589d63a5ce3SMikulas Patocka 	((num) > (UINT_MAX - (fixed)) / (obj))
590d63a5ce3SMikulas Patocka 
59156a67df7SMike Snitzer /*
59256a67df7SMike Snitzer  * Sector offset taken relative to the start of the target instead of
59356a67df7SMike Snitzer  * relative to the start of the device.
59456a67df7SMike Snitzer  */
59556a67df7SMike Snitzer #define dm_target_offset(ti, sector) ((sector) - (ti)->begin)
59656a67df7SMike Snitzer 
5970da336e5SAlasdair G Kergon static inline sector_t to_sector(unsigned long n)
5980da336e5SAlasdair G Kergon {
5990da336e5SAlasdair G Kergon 	return (n >> SECTOR_SHIFT);
6000da336e5SAlasdair G Kergon }
6010da336e5SAlasdair G Kergon 
6020da336e5SAlasdair G Kergon static inline unsigned long to_bytes(sector_t n)
6030da336e5SAlasdair G Kergon {
6040da336e5SAlasdair G Kergon 	return (n << SECTOR_SHIFT);
6050da336e5SAlasdair G Kergon }
6060da336e5SAlasdair G Kergon 
607cec47e3dSKiyoshi Ueda /*-----------------------------------------------------------------
608cec47e3dSKiyoshi Ueda  * Helper for block layer and dm core operations
609cec47e3dSKiyoshi Ueda  *---------------------------------------------------------------*/
610cec47e3dSKiyoshi Ueda int dm_underlying_device_busy(struct request_queue *q);
611cec47e3dSKiyoshi Ueda 
6121da177e4SLinus Torvalds #endif	/* _LINUX_DEVICE_MAPPER_H */
613