xref: /openbmc/linux/include/linux/device-mapper.h (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
13bd94003SHeinz Mauelshagen /* SPDX-License-Identifier: GPL-2.0-only */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Copyright (C) 2001 Sistina Software (UK) Limited.
40da336e5SAlasdair G Kergon  * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * This file is released under the LGPL.
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #ifndef _LINUX_DEVICE_MAPPER_H
101da177e4SLinus Torvalds #define _LINUX_DEVICE_MAPPER_H
111da177e4SLinus Torvalds 
12416cd17bSHeinz Mauelshagen #include <linux/bio.h>
13f6fccb12SMilan Broz #include <linux/blkdev.h>
146bbc923dSHelen Koike #include <linux/dm-ioctl.h>
15fd2ed4d2SMikulas Patocka #include <linux/math64.h>
1671a16736SNamhyung Kim #include <linux/ratelimit.h>
17416cd17bSHeinz Mauelshagen 
18af4874e0SMike Snitzer struct dm_dev;
191da177e4SLinus Torvalds struct dm_target;
201da177e4SLinus Torvalds struct dm_table;
21d4100351SChristoph Hellwig struct dm_report_zones_args;
2217b2f66fSAlasdair G Kergon struct mapped_device;
23f6fccb12SMilan Broz struct bio_vec;
24e511c4a3SJane Chu enum dax_access_mode;
251da177e4SLinus Torvalds 
26e83068a5SMike Snitzer /*
27e83068a5SMike Snitzer  * Type of table, mapped_device's mempool and request_queue
28e83068a5SMike Snitzer  */
297e0d574fSBart Van Assche enum dm_queue_mode {
307e0d574fSBart Van Assche 	DM_TYPE_NONE		 = 0,
317e0d574fSBart Van Assche 	DM_TYPE_BIO_BASED	 = 1,
327e0d574fSBart Van Assche 	DM_TYPE_REQUEST_BASED	 = 2,
33953923c0SMike Snitzer 	DM_TYPE_DAX_BIO_BASED	 = 3,
347e0d574fSBart Van Assche };
35e83068a5SMike Snitzer 
3691ccbbacSTushar Sugandhi typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE, STATUSTYPE_IMA } status_type_t;
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds union map_info {
391da177e4SLinus Torvalds 	void *ptr;
401da177e4SLinus Torvalds };
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds /*
431da177e4SLinus Torvalds  * In the constructor the target parameter will already have the
441da177e4SLinus Torvalds  * table, type, begin and len fields filled in.
451da177e4SLinus Torvalds  */
461da177e4SLinus Torvalds typedef int (*dm_ctr_fn) (struct dm_target *target,
471da177e4SLinus Torvalds 			  unsigned int argc, char **argv);
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds /*
501da177e4SLinus Torvalds  * The destructor doesn't need to free the dm_target, just
511da177e4SLinus Torvalds  * anything hidden ti->private.
521da177e4SLinus Torvalds  */
531da177e4SLinus Torvalds typedef void (*dm_dtr_fn) (struct dm_target *ti);
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds /*
561da177e4SLinus Torvalds  * The map function must return:
571da177e4SLinus Torvalds  * < 0: error
581da177e4SLinus Torvalds  * = 0: The target will handle the io by resubmitting it later
5945cbcd79SKiyoshi Ueda  * = 1: simple remap complete
602e93ccc1SKiyoshi Ueda  * = 2: The target wants to push back the io
611da177e4SLinus Torvalds  */
627de3ee57SMikulas Patocka typedef int (*dm_map_fn) (struct dm_target *ti, struct bio *bio);
63e5863d9aSMike Snitzer typedef int (*dm_clone_and_map_request_fn) (struct dm_target *ti,
64e5863d9aSMike Snitzer 					    struct request *rq,
65e5863d9aSMike Snitzer 					    union map_info *map_context,
66e5863d9aSMike Snitzer 					    struct request **clone);
675de719e3SYufen Yu typedef void (*dm_release_clone_request_fn) (struct request *clone,
685de719e3SYufen Yu 					     union map_info *map_context);
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds /*
711da177e4SLinus Torvalds  * Returns:
721da177e4SLinus Torvalds  * < 0 : error (currently ignored)
731da177e4SLinus Torvalds  * 0   : ended successfully
741da177e4SLinus Torvalds  * 1   : for some reason the io has still not completed (eg,
751da177e4SLinus Torvalds  *       multipath target might want to requeue a failed io).
762e93ccc1SKiyoshi Ueda  * 2   : The target wants to push back the io
771da177e4SLinus Torvalds  */
781da177e4SLinus Torvalds typedef int (*dm_endio_fn) (struct dm_target *ti,
794e4cbee9SChristoph Hellwig 			    struct bio *bio, blk_status_t *error);
807d76345dSKiyoshi Ueda typedef int (*dm_request_endio_fn) (struct dm_target *ti,
812a842acaSChristoph Hellwig 				    struct request *clone, blk_status_t error,
827d76345dSKiyoshi Ueda 				    union map_info *map_context);
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds typedef void (*dm_presuspend_fn) (struct dm_target *ti);
85d67ee213SMike Snitzer typedef void (*dm_presuspend_undo_fn) (struct dm_target *ti);
861da177e4SLinus Torvalds typedef void (*dm_postsuspend_fn) (struct dm_target *ti);
878757b776SMilan Broz typedef int (*dm_preresume_fn) (struct dm_target *ti);
881da177e4SLinus Torvalds typedef void (*dm_resume_fn) (struct dm_target *ti);
891da177e4SLinus Torvalds 
90fd7c092eSMikulas Patocka typedef void (*dm_status_fn) (struct dm_target *ti, status_type_t status_type,
9186a3238cSHeinz Mauelshagen 			      unsigned int status_flags, char *result, unsigned int maxlen);
921da177e4SLinus Torvalds 
9386a3238cSHeinz Mauelshagen typedef int (*dm_message_fn) (struct dm_target *ti, unsigned int argc, char **argv,
9486a3238cSHeinz Mauelshagen 			      char *result, unsigned int maxlen);
951da177e4SLinus Torvalds 
965bd5e8d8SMike Snitzer typedef int (*dm_prepare_ioctl_fn) (struct dm_target *ti, struct block_device **bdev);
97aa129a22SMilan Broz 
98e3290b94SMike Snitzer #ifdef CONFIG_BLK_DEV_ZONED
99d4100351SChristoph Hellwig typedef int (*dm_report_zones_fn) (struct dm_target *ti,
100d4100351SChristoph Hellwig 				   struct dm_report_zones_args *args,
101d4100351SChristoph Hellwig 				   unsigned int nr_zones);
102e3290b94SMike Snitzer #else
103e3290b94SMike Snitzer /*
104e3290b94SMike Snitzer  * Define dm_report_zones_fn so that targets can assign to NULL if
105e3290b94SMike Snitzer  * CONFIG_BLK_DEV_ZONED disabled. Otherwise each target needs to do
106e3290b94SMike Snitzer  * awkward #ifdefs in their target_type, etc.
107e3290b94SMike Snitzer  */
108e3290b94SMike Snitzer typedef int (*dm_report_zones_fn) (struct dm_target *dummy);
109e3290b94SMike Snitzer #endif
110e76239a3SChristoph Hellwig 
111058ce5caSAlasdair G Kergon /*
112058ce5caSAlasdair G Kergon  * These iteration functions are typically used to check (and combine)
113058ce5caSAlasdair G Kergon  * properties of underlying devices.
114058ce5caSAlasdair G Kergon  * E.g. Does at least one underlying device support flush?
115058ce5caSAlasdair G Kergon  *      Does any underlying device not support WRITE_SAME?
116058ce5caSAlasdair G Kergon  *
117058ce5caSAlasdair G Kergon  * The callout function is called once for each contiguous section of
118058ce5caSAlasdair G Kergon  * an underlying device.  State can be maintained in *data.
119058ce5caSAlasdair G Kergon  * Return non-zero to stop iterating through any further devices.
120058ce5caSAlasdair G Kergon  */
121af4874e0SMike Snitzer typedef int (*iterate_devices_callout_fn) (struct dm_target *ti,
122af4874e0SMike Snitzer 					   struct dm_dev *dev,
1235dea271bSMike Snitzer 					   sector_t start, sector_t len,
124af4874e0SMike Snitzer 					   void *data);
125af4874e0SMike Snitzer 
126058ce5caSAlasdair G Kergon /*
127058ce5caSAlasdair G Kergon  * This function must iterate through each section of device used by the
128058ce5caSAlasdair G Kergon  * target until it encounters a non-zero return code, which it then returns.
129058ce5caSAlasdair G Kergon  * Returns zero if no callout returned non-zero.
130058ce5caSAlasdair G Kergon  */
131af4874e0SMike Snitzer typedef int (*dm_iterate_devices_fn) (struct dm_target *ti,
132af4874e0SMike Snitzer 				      iterate_devices_callout_fn fn,
133af4874e0SMike Snitzer 				      void *data);
134af4874e0SMike Snitzer 
13540bea431SMike Snitzer typedef void (*dm_io_hints_fn) (struct dm_target *ti,
13640bea431SMike Snitzer 				struct queue_limits *limits);
13740bea431SMike Snitzer 
1387d76345dSKiyoshi Ueda /*
1397d76345dSKiyoshi Ueda  * Returns:
1407d76345dSKiyoshi Ueda  *    0: The target can handle the next I/O immediately.
1417d76345dSKiyoshi Ueda  *    1: The target can't handle the next I/O immediately.
1427d76345dSKiyoshi Ueda  */
1437d76345dSKiyoshi Ueda typedef int (*dm_busy_fn) (struct dm_target *ti);
1447d76345dSKiyoshi Ueda 
145545ed20eSToshi Kani /*
146545ed20eSToshi Kani  * Returns:
147545ed20eSToshi Kani  *  < 0 : error
148545ed20eSToshi Kani  * >= 0 : the number of bytes accessible at the address
149545ed20eSToshi Kani  */
150817bf402SDan Williams typedef long (*dm_dax_direct_access_fn) (struct dm_target *ti, pgoff_t pgoff,
151e511c4a3SJane Chu 		long nr_pages, enum dax_access_mode node, void **kaddr,
152e511c4a3SJane Chu 		pfn_t *pfn);
153cdf6cdcdSVivek Goyal typedef int (*dm_dax_zero_page_range_fn)(struct dm_target *ti, pgoff_t pgoff,
154cdf6cdcdSVivek Goyal 		size_t nr_pages);
155545ed20eSToshi Kani 
156047218ecSJane Chu /*
157047218ecSJane Chu  * Returns:
158047218ecSJane Chu  * != 0 : number of bytes transferred
159047218ecSJane Chu  * 0    : recovery write failed
160047218ecSJane Chu  */
161047218ecSJane Chu typedef size_t (*dm_dax_recovery_write_fn)(struct dm_target *ti, pgoff_t pgoff,
162047218ecSJane Chu 		void *addr, size_t bytes, struct iov_iter *i);
163047218ecSJane Chu 
1641da177e4SLinus Torvalds void dm_error(const char *message);
1651da177e4SLinus Torvalds 
16682b1519bSMikulas Patocka struct dm_dev {
16782b1519bSMikulas Patocka 	struct block_device *bdev;
168817bf402SDan Williams 	struct dax_device *dax_dev;
169aeb5d727SAl Viro 	blk_mode_t mode;
17082b1519bSMikulas Patocka 	char name[16];
17182b1519bSMikulas Patocka };
17282b1519bSMikulas Patocka 
1734df2bf46SDingXiang /*
1744df2bf46SDingXiang  * Constructors should call these functions to ensure destination devices
1753cb40214SBryn Reeves  * are opened/closed correctly.
1761da177e4SLinus Torvalds  */
1771da177e4SLinus Torvalds int dm_get_device(struct dm_target *ti, const char *path, blk_mode_t mode,
1781da177e4SLinus Torvalds 		  struct dm_dev **result);
1798215d6ecSNikanth Karthikesan void dm_put_device(struct dm_target *ti, struct dm_dev *d);
1808215d6ecSNikanth Karthikesan 
1811da177e4SLinus Torvalds /*
1821da177e4SLinus Torvalds  * Information about a target type
1831da177e4SLinus Torvalds  */
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds struct target_type {
186ab4c1424SAndi Kleen 	uint64_t features;
1871da177e4SLinus Torvalds 	const char *name;
188ab4c1424SAndi Kleen 	struct module *module;
1891da177e4SLinus Torvalds 	unsigned int version[3];
1901da177e4SLinus Torvalds 	dm_ctr_fn ctr;
19186a3238cSHeinz Mauelshagen 	dm_dtr_fn dtr;
1921da177e4SLinus Torvalds 	dm_map_fn map;
1931da177e4SLinus Torvalds 	dm_clone_and_map_request_fn clone_and_map_rq;
1941da177e4SLinus Torvalds 	dm_release_clone_request_fn release_clone_rq;
195e5863d9aSMike Snitzer 	dm_endio_fn end_io;
196e5863d9aSMike Snitzer 	dm_request_endio_fn rq_end_io;
1971da177e4SLinus Torvalds 	dm_presuspend_fn presuspend;
1987d76345dSKiyoshi Ueda 	dm_presuspend_undo_fn presuspend_undo;
1991da177e4SLinus Torvalds 	dm_postsuspend_fn postsuspend;
200d67ee213SMike Snitzer 	dm_preresume_fn preresume;
2011da177e4SLinus Torvalds 	dm_resume_fn resume;
2028757b776SMilan Broz 	dm_status_fn status;
2031da177e4SLinus Torvalds 	dm_message_fn message;
2041da177e4SLinus Torvalds 	dm_prepare_ioctl_fn prepare_ioctl;
2051da177e4SLinus Torvalds 	dm_report_zones_fn report_zones;
206e56f81e0SChristoph Hellwig 	dm_busy_fn busy;
207e76239a3SChristoph Hellwig 	dm_iterate_devices_fn iterate_devices;
2087d76345dSKiyoshi Ueda 	dm_io_hints_fn io_hints;
209af4874e0SMike Snitzer 	dm_dax_direct_access_fn direct_access;
21040bea431SMike Snitzer 	dm_dax_zero_page_range_fn dax_zero_page_range;
211817bf402SDan Williams 	dm_dax_recovery_write_fn dax_recovery_write;
212cdf6cdcdSVivek Goyal 
213047218ecSJane Chu 	/* For internal device-mapper use. */
21445194e4fSCheng Renquan 	struct list_head list;
21545194e4fSCheng Renquan };
21645194e4fSCheng Renquan 
2171da177e4SLinus Torvalds /*
2181da177e4SLinus Torvalds  * Target features
2193791e2fcSAlasdair G Kergon  */
2203791e2fcSAlasdair G Kergon 
2213791e2fcSAlasdair G Kergon /*
2223791e2fcSAlasdair G Kergon  * Any table that contains an instance of this target must have only one.
2233791e2fcSAlasdair G Kergon  */
2243791e2fcSAlasdair G Kergon #define DM_TARGET_SINGLETON		0x00000001
2253791e2fcSAlasdair G Kergon #define dm_target_needs_singleton(type)	((type)->features & DM_TARGET_SINGLETON)
2263791e2fcSAlasdair G Kergon 
2273791e2fcSAlasdair G Kergon /*
2283791e2fcSAlasdair G Kergon  * Indicates that a target does not support read-only devices.
229cc6cbe14SAlasdair G Kergon  */
230cc6cbe14SAlasdair G Kergon #define DM_TARGET_ALWAYS_WRITEABLE	0x00000002
231cc6cbe14SAlasdair G Kergon #define dm_target_always_writeable(type) \
232cc6cbe14SAlasdair G Kergon 		((type)->features & DM_TARGET_ALWAYS_WRITEABLE)
233cc6cbe14SAlasdair G Kergon 
234cc6cbe14SAlasdair G Kergon /*
235cc6cbe14SAlasdair G Kergon  * Any device that contains a table with an instance of this target may never
23636a0456fSAlasdair G Kergon  * have tables containing any different target type.
23736a0456fSAlasdair G Kergon  */
23836a0456fSAlasdair G Kergon #define DM_TARGET_IMMUTABLE		0x00000004
23936a0456fSAlasdair G Kergon #define dm_target_is_immutable(type)	((type)->features & DM_TARGET_IMMUTABLE)
24036a0456fSAlasdair G Kergon 
24136a0456fSAlasdair G Kergon /*
24236a0456fSAlasdair G Kergon  * Indicates that a target may replace any target; even immutable targets.
243b0d8ed4dSAlasdair G Kergon  * .map, .map_rq, .clone_and_map_rq and .release_clone_rq are all defined.
244f083b09bSMike Snitzer  */
245f083b09bSMike Snitzer #define DM_TARGET_WILDCARD		0x00000008
246f083b09bSMike Snitzer #define dm_target_is_wildcard(type)	((type)->features & DM_TARGET_WILDCARD)
247f083b09bSMike Snitzer 
248f083b09bSMike Snitzer /*
249f083b09bSMike Snitzer  * A target implements own bio data integrity.
250f083b09bSMike Snitzer  */
2519b4b5a79SMilan Broz #define DM_TARGET_INTEGRITY		0x00000010
2529b4b5a79SMilan Broz #define dm_target_has_integrity(type)	((type)->features & DM_TARGET_INTEGRITY)
2539b4b5a79SMilan Broz 
2549b4b5a79SMilan Broz /*
2559b4b5a79SMilan Broz  * A target passes integrity data to the lower device.
256e2460f2aSMikulas Patocka  */
257e2460f2aSMikulas Patocka #define DM_TARGET_PASSES_INTEGRITY	0x00000020
258e2460f2aSMikulas Patocka #define dm_target_passes_integrity(type) ((type)->features & DM_TARGET_PASSES_INTEGRITY)
259e2460f2aSMikulas Patocka 
260e2460f2aSMikulas Patocka /*
261e2460f2aSMikulas Patocka  * Indicates support for zoned block devices:
262dd88d313SDamien Le Moal  * - DM_TARGET_ZONED_HM: the target also supports host-managed zoned
2632d669cebSShin'ichiro Kawasaki  *   block devices but does not support combining different zoned models.
2642d669cebSShin'ichiro Kawasaki  * - DM_TARGET_MIXED_ZONED_MODEL: the target supports combining multiple
2652d669cebSShin'ichiro Kawasaki  *   devices with different zoned models.
2662d669cebSShin'ichiro Kawasaki  */
2672d669cebSShin'ichiro Kawasaki #ifdef CONFIG_BLK_DEV_ZONED
268dd88d313SDamien Le Moal #define DM_TARGET_ZONED_HM		0x00000040
269e3290b94SMike Snitzer #define dm_target_supports_zoned_hm(type) ((type)->features & DM_TARGET_ZONED_HM)
270dd88d313SDamien Le Moal #else
271dd88d313SDamien Le Moal #define DM_TARGET_ZONED_HM		0x00000000
272e3290b94SMike Snitzer #define dm_target_supports_zoned_hm(type) (false)
273e3290b94SMike Snitzer #endif
274e3290b94SMike Snitzer 
275e3290b94SMike Snitzer /*
276dd88d313SDamien Le Moal  * A target handles REQ_NOWAIT
2776abc4946SKonstantin Khlebnikov  */
2786abc4946SKonstantin Khlebnikov #define DM_TARGET_NOWAIT		0x00000080
2796abc4946SKonstantin Khlebnikov #define dm_target_supports_nowait(type) ((type)->features & DM_TARGET_NOWAIT)
2806abc4946SKonstantin Khlebnikov 
2816abc4946SKonstantin Khlebnikov /*
2826abc4946SKonstantin Khlebnikov  * A target supports passing through inline crypto support.
283aa6ce87aSSatya Tangirala  */
284aa6ce87aSSatya Tangirala #define DM_TARGET_PASSES_CRYPTO		0x00000100
285aa6ce87aSSatya Tangirala #define dm_target_passes_crypto(type) ((type)->features & DM_TARGET_PASSES_CRYPTO)
286aa6ce87aSSatya Tangirala 
287aa6ce87aSSatya Tangirala #ifdef CONFIG_BLK_DEV_ZONED
288aa6ce87aSSatya Tangirala #define DM_TARGET_MIXED_ZONED_MODEL	0x00000200
2892d669cebSShin'ichiro Kawasaki #define dm_target_supports_mixed_zoned_model(type) \
2902d669cebSShin'ichiro Kawasaki 	((type)->features & DM_TARGET_MIXED_ZONED_MODEL)
2912d669cebSShin'ichiro Kawasaki #else
2922d669cebSShin'ichiro Kawasaki #define DM_TARGET_MIXED_ZONED_MODEL	0x00000000
2932d669cebSShin'ichiro Kawasaki #define dm_target_supports_mixed_zoned_model(type) (false)
2942d669cebSShin'ichiro Kawasaki #endif
2952d669cebSShin'ichiro Kawasaki 
2962d669cebSShin'ichiro Kawasaki struct dm_target {
2972d669cebSShin'ichiro Kawasaki 	struct dm_table *table;
2981da177e4SLinus Torvalds 	struct target_type *type;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	/* target limits */
3011da177e4SLinus Torvalds 	sector_t begin;
3021da177e4SLinus Torvalds 	sector_t len;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	/* If non-zero, maximum size of I/O submitted to a target. */
3051da177e4SLinus Torvalds 	uint32_t max_io_len;
306542f9038SMike Snitzer 
307542f9038SMike Snitzer 	/*
3081da177e4SLinus Torvalds 	 * A number of zero-length barrier bios that will be submitted
3091da177e4SLinus Torvalds 	 * to the target for the purpose of flushing cache.
31055a62eefSAlasdair G Kergon 	 *
311f9ab94ceSMikulas Patocka 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
312f9ab94ceSMikulas Patocka 	 * It is a responsibility of the target driver to remap these bios
31355a62eefSAlasdair G Kergon 	 * to the real underlying devices.
31455a62eefSAlasdair G Kergon 	 */
315f9ab94ceSMikulas Patocka 	unsigned int num_flush_bios;
316f9ab94ceSMikulas Patocka 
31786a3238cSHeinz Mauelshagen 	/*
318f9ab94ceSMikulas Patocka 	 * The number of discard bios that will be submitted to the target.
3195ae89a87SMike Snitzer 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
32055a62eefSAlasdair G Kergon 	 */
32155a62eefSAlasdair G Kergon 	unsigned int num_discard_bios;
3225ae89a87SMike Snitzer 
32386a3238cSHeinz Mauelshagen 	/*
3245ae89a87SMike Snitzer 	 * The number of secure erase bios that will be submitted to the target.
325d54eaa5aSMike Snitzer 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
32600716545SDenis Semakin 	 */
32700716545SDenis Semakin 	unsigned int num_secure_erase_bios;
32800716545SDenis Semakin 
32986a3238cSHeinz Mauelshagen 	/*
33000716545SDenis Semakin 	 * The number of WRITE ZEROES bios that will be submitted to the target.
33100716545SDenis Semakin 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
332ac62d620SChristoph Hellwig 	 */
333ac62d620SChristoph Hellwig 	unsigned int num_write_zeroes_bios;
334ac62d620SChristoph Hellwig 
33586a3238cSHeinz Mauelshagen 	/*
336ac62d620SChristoph Hellwig 	 * The minimum number of extra bytes allocated in each io for the
337ac62d620SChristoph Hellwig 	 * target to use.
33830187e1dSMike Snitzer 	 */
33930187e1dSMike Snitzer 	unsigned int per_io_data_size;
340c0820cf5SMikulas Patocka 
34186a3238cSHeinz Mauelshagen 	/* target specific data */
342c0820cf5SMikulas Patocka 	void *private;
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	/* Used to provide an error string from the ctr */
3451da177e4SLinus Torvalds 	char *error;
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	/*
3484c259327SMike Snitzer 	 * Set if this target needs to receive flushes regardless of
3494c259327SMike Snitzer 	 * whether or not its underlying devices have support.
3500e9c24edSJoe Thornber 	 */
3510e9c24edSJoe Thornber 	bool flush_supported:1;
3520e9c24edSJoe Thornber 
3530e9c24edSJoe Thornber 	/*
3540e9c24edSJoe Thornber 	 * Set if this target needs to receive discards regardless of
3550e9c24edSJoe Thornber 	 * whether or not its underlying devices have support.
3564c259327SMike Snitzer 	 */
3574c259327SMike Snitzer 	bool discards_supported:1;
3584c259327SMike Snitzer 
3590ac55489SAlasdair G Kergon 	/*
360a666e5c0SMikulas Patocka 	 * Set if this target requires that discards be split on
361a666e5c0SMikulas Patocka 	 * 'max_discard_sectors' boundaries.
362*13f6facfSMike Snitzer 	 */
363*13f6facfSMike Snitzer 	bool max_discard_granularity:1;
36406961c48SMike Snitzer 
36506961c48SMike Snitzer 	/*
36606961c48SMike Snitzer 	 * Set if this target requires that secure_erases be split on
36706961c48SMike Snitzer 	 * 'max_secure_erase_sectors' boundaries.
368*13f6facfSMike Snitzer 	 */
369*13f6facfSMike Snitzer 	bool max_secure_erase_granularity:1;
370*13f6facfSMike Snitzer 
371*13f6facfSMike Snitzer 	/*
372*13f6facfSMike Snitzer 	 * Set if this target requires that write_zeroes be split on
373*13f6facfSMike Snitzer 	 * 'max_write_zeroes_sectors' boundaries.
374*13f6facfSMike Snitzer 	 */
375*13f6facfSMike Snitzer 	bool max_write_zeroes_granularity:1;
376*13f6facfSMike Snitzer 
377*13f6facfSMike Snitzer 	/*
378*13f6facfSMike Snitzer 	 * Set if we need to limit the number of in-flight bios when swapping.
379*13f6facfSMike Snitzer 	 */
380a666e5c0SMikulas Patocka 	bool limit_swap_bios:1;
381a666e5c0SMikulas Patocka 
382a666e5c0SMikulas Patocka 	/*
383bb37d772SDamien Le Moal 	 * Set if this target implements a zoned device and needs emulation of
384bb37d772SDamien Le Moal 	 * zone append operations using regular writes.
385a8b9d116STom Rix 	 */
386bb37d772SDamien Le Moal 	bool emulate_zone_append:1;
387bb37d772SDamien Le Moal 
388bb37d772SDamien Le Moal 	/*
3890fbb4d93SMike Snitzer 	 * Set if the target will submit IO using dm_submit_bio_remap()
3900fbb4d93SMike Snitzer 	 * after returning DM_MAPIO_SUBMITTED from its map function.
3910fbb4d93SMike Snitzer 	 */
3920fbb4d93SMike Snitzer 	bool accounts_remapped_io:1;
3930fbb4d93SMike Snitzer 
3940fbb4d93SMike Snitzer 	/*
3959dd1cd32SMike Snitzer 	 * Set if the target will submit the DM bio without first calling
3969dd1cd32SMike Snitzer 	 * bio_set_dev(). NOTE: ideally a target should _not_ need this.
3979dd1cd32SMike Snitzer 	 */
3989dd1cd32SMike Snitzer 	bool needs_bio_set_dev:1;
3999dd1cd32SMike Snitzer };
4009dd1cd32SMike Snitzer 
4011da177e4SLinus Torvalds void *dm_per_bio_data(struct bio *bio, size_t data_size);
4021da177e4SLinus Torvalds struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size);
40364f52b0eSMike Snitzer unsigned int dm_bio_get_target_bio_nr(const struct bio *bio);
40464f52b0eSMike Snitzer 
40586a3238cSHeinz Mauelshagen u64 dm_start_time_ns_from_clone(struct bio *bio);
406ddbd658fSMikulas Patocka 
407087615bfSGabriel Krisman Bertazi int dm_register_target(struct target_type *t);
408087615bfSGabriel Krisman Bertazi void dm_unregister_target(struct target_type *t);
4091da177e4SLinus Torvalds 
41010d3bd09SMikulas Patocka /*
41117b2f66fSAlasdair G Kergon  * Target argument parsing.
412498f0103SMike Snitzer  */
413498f0103SMike Snitzer struct dm_arg_set {
414498f0103SMike Snitzer 	unsigned int argc;
415498f0103SMike Snitzer 	char **argv;
41686a3238cSHeinz Mauelshagen };
417498f0103SMike Snitzer 
418498f0103SMike Snitzer /*
419498f0103SMike Snitzer  * The minimum and maximum value of a numeric argument, together with
420498f0103SMike Snitzer  * the error message to use if the number is found to be outside that range.
421498f0103SMike Snitzer  */
422498f0103SMike Snitzer struct dm_arg {
423498f0103SMike Snitzer 	unsigned int min;
424498f0103SMike Snitzer 	unsigned int max;
42586a3238cSHeinz Mauelshagen 	char *error;
42686a3238cSHeinz Mauelshagen };
427498f0103SMike Snitzer 
428498f0103SMike Snitzer /*
429498f0103SMike Snitzer  * Validate the next argument, either returning it as *value or, if invalid,
430498f0103SMike Snitzer  * returning -EINVAL and setting *error.
431498f0103SMike Snitzer  */
432498f0103SMike Snitzer int dm_read_arg(const struct dm_arg *arg, struct dm_arg_set *arg_set,
433498f0103SMike Snitzer 		unsigned int *value, char **error);
4345916a22bSEric Biggers 
43586a3238cSHeinz Mauelshagen /*
436498f0103SMike Snitzer  * Process the next argument as the start of a group containing between
437498f0103SMike Snitzer  * arg->min and arg->max further arguments. Either return the size as
438498f0103SMike Snitzer  * *num_args or, if invalid, return -EINVAL and set *error.
439498f0103SMike Snitzer  */
440498f0103SMike Snitzer int dm_read_arg_group(const struct dm_arg *arg, struct dm_arg_set *arg_set,
441498f0103SMike Snitzer 		      unsigned int *num_args, char **error);
4425916a22bSEric Biggers 
44386a3238cSHeinz Mauelshagen /*
444498f0103SMike Snitzer  * Return the current argument and shift to the next.
445498f0103SMike Snitzer  */
446498f0103SMike Snitzer const char *dm_shift_arg(struct dm_arg_set *as);
447498f0103SMike Snitzer 
448498f0103SMike Snitzer /*
449498f0103SMike Snitzer  * Move through num_args arguments.
450498f0103SMike Snitzer  */
451498f0103SMike Snitzer void dm_consume_args(struct dm_arg_set *as, unsigned int num_args);
452498f0103SMike Snitzer 
45386a3238cSHeinz Mauelshagen /*
454498f0103SMike Snitzer  *----------------------------------------------------------------
455a4a82ce3SHeinz Mauelshagen  * Functions for creating and manipulating mapped devices.
456a4a82ce3SHeinz Mauelshagen  * Drop the reference with dm_put when you finish with the object.
45717b2f66fSAlasdair G Kergon  *----------------------------------------------------------------
45817b2f66fSAlasdair G Kergon  */
459a4a82ce3SHeinz Mauelshagen 
460a4a82ce3SHeinz Mauelshagen /*
46117b2f66fSAlasdair G Kergon  * DM_ANY_MINOR chooses the next available minor number.
46217b2f66fSAlasdair G Kergon  */
46317b2f66fSAlasdair G Kergon #define DM_ANY_MINOR (-1)
46417b2f66fSAlasdair G Kergon int dm_create(int minor, struct mapped_device **md);
46517b2f66fSAlasdair G Kergon 
46617b2f66fSAlasdair G Kergon /*
46717b2f66fSAlasdair G Kergon  * Reference counting for md.
46817b2f66fSAlasdair G Kergon  */
46917b2f66fSAlasdair G Kergon struct mapped_device *dm_get_md(dev_t dev);
47017b2f66fSAlasdair G Kergon void dm_get(struct mapped_device *md);
47117b2f66fSAlasdair G Kergon int dm_hold(struct mapped_device *md);
47217b2f66fSAlasdair G Kergon void dm_put(struct mapped_device *md);
47309ee96b2SMikulas Patocka 
47417b2f66fSAlasdair G Kergon /*
47517b2f66fSAlasdair G Kergon  * An arbitrary pointer may be stored alongside a mapped device.
47617b2f66fSAlasdair G Kergon  */
47717b2f66fSAlasdair G Kergon void dm_set_mdptr(struct mapped_device *md, void *ptr);
47817b2f66fSAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md);
47917b2f66fSAlasdair G Kergon 
48017b2f66fSAlasdair G Kergon /*
48117b2f66fSAlasdair G Kergon  * A device can still be used while suspended, but I/O is deferred.
48217b2f66fSAlasdair G Kergon  */
48317b2f66fSAlasdair G Kergon int dm_suspend(struct mapped_device *md, unsigned int suspend_flags);
48417b2f66fSAlasdair G Kergon int dm_resume(struct mapped_device *md);
48586a3238cSHeinz Mauelshagen 
48617b2f66fSAlasdair G Kergon /*
48717b2f66fSAlasdair G Kergon  * Event functions.
48817b2f66fSAlasdair G Kergon  */
48917b2f66fSAlasdair G Kergon uint32_t dm_get_event_nr(struct mapped_device *md);
49017b2f66fSAlasdair G Kergon int dm_wait_event(struct mapped_device *md, int event_nr);
49117b2f66fSAlasdair G Kergon uint32_t dm_next_uevent_seq(struct mapped_device *md);
49217b2f66fSAlasdair G Kergon void dm_uevent_add(struct mapped_device *md, struct list_head *elist);
4937a8c3d3bSMike Anderson 
4947a8c3d3bSMike Anderson /*
49517b2f66fSAlasdair G Kergon  * Info functions.
49617b2f66fSAlasdair G Kergon  */
49717b2f66fSAlasdair G Kergon const char *dm_device_name(struct mapped_device *md);
49817b2f66fSAlasdair G Kergon int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid);
49972d94861SAlasdair G Kergon struct gendisk *dm_disk(struct mapped_device *md);
50096a1f7dbSMike Anderson int dm_suspended(struct dm_target *ti);
50117b2f66fSAlasdair G Kergon int dm_post_suspending(struct dm_target *ti);
50264dbce58SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti);
5035df96f2bSMikulas Patocka void dm_accept_partial_bio(struct bio *bio, unsigned int n_sectors);
5042e93ccc1SKiyoshi Ueda void dm_submit_bio_remap(struct bio *clone, struct bio *tgt_clone);
50586a3238cSHeinz Mauelshagen union map_info *dm_get_rq_mapinfo(struct request *rq);
506b7f8dff0SMike Snitzer 
507cec47e3dSKiyoshi Ueda #ifdef CONFIG_BLK_DEV_ZONED
50817b2f66fSAlasdair G Kergon struct dm_report_zones_args {
509d4100351SChristoph Hellwig 	struct dm_target *tgt;
510d4100351SChristoph Hellwig 	sector_t next_sector;
511d4100351SChristoph Hellwig 
512d4100351SChristoph Hellwig 	void *orig_data;
513d4100351SChristoph Hellwig 	report_zones_cb orig_cb;
514d4100351SChristoph Hellwig 	unsigned int zone_idx;
515d4100351SChristoph Hellwig 
516d4100351SChristoph Hellwig 	/* must be filled by ->report_zones before calling dm_report_zones_cb */
517d4100351SChristoph Hellwig 	sector_t start;
518d4100351SChristoph Hellwig };
519d4100351SChristoph Hellwig int dm_report_zones(struct block_device *bdev, sector_t start, sector_t sector,
520d4100351SChristoph Hellwig 		    struct dm_report_zones_args *args, unsigned int nr_zones);
521912e8875SDamien Le Moal #endif /* CONFIG_BLK_DEV_ZONED */
522912e8875SDamien Le Moal 
523d4100351SChristoph Hellwig /*
524d4100351SChristoph Hellwig  * Device mapper functions to parse and create devices specified by the
5256bbc923dSHelen Koike  * parameter "dm-mod.create="
5266bbc923dSHelen Koike  */
5276bbc923dSHelen Koike int __init dm_early_create(struct dm_ioctl *dmi,
5286bbc923dSHelen Koike 			   struct dm_target_spec **spec_array,
5296bbc923dSHelen Koike 			   char **target_params_array);
5306bbc923dSHelen Koike 
5316bbc923dSHelen Koike /*
5326bbc923dSHelen Koike  * Geometry functions.
53317b2f66fSAlasdair G Kergon  */
53417b2f66fSAlasdair G Kergon int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo);
53517b2f66fSAlasdair G Kergon int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo);
53617b2f66fSAlasdair G Kergon 
53717b2f66fSAlasdair G Kergon /*
53817b2f66fSAlasdair G Kergon  *---------------------------------------------------------------
539a4a82ce3SHeinz Mauelshagen  * Functions for manipulating device-mapper tables.
540a4a82ce3SHeinz Mauelshagen  *---------------------------------------------------------------
54117b2f66fSAlasdair G Kergon  */
542a4a82ce3SHeinz Mauelshagen 
543a4a82ce3SHeinz Mauelshagen /*
54417b2f66fSAlasdair G Kergon  * First create an empty table.
54517b2f66fSAlasdair G Kergon  */
54617b2f66fSAlasdair G Kergon int dm_table_create(struct dm_table **result, blk_mode_t mode,
54717b2f66fSAlasdair G Kergon 		    unsigned int num_targets, struct mapped_device *md);
548aeb5d727SAl Viro 
54986a3238cSHeinz Mauelshagen /*
55017b2f66fSAlasdair G Kergon  * Then call this once for each target.
55117b2f66fSAlasdair G Kergon  */
55217b2f66fSAlasdair G Kergon int dm_table_add_target(struct dm_table *t, const char *type,
55317b2f66fSAlasdair G Kergon 			sector_t start, sector_t len, char *params);
55417b2f66fSAlasdair G Kergon 
55517b2f66fSAlasdair G Kergon /*
55617b2f66fSAlasdair G Kergon  * Target can use this to set the table's type.
55717b2f66fSAlasdair G Kergon  * Can only ever be called from a target's ctr.
558e83068a5SMike Snitzer  * Useful for "hybrid" target (supports both bio-based
559e83068a5SMike Snitzer  * and request-based).
560e83068a5SMike Snitzer  */
561e83068a5SMike Snitzer void dm_table_set_type(struct dm_table *t, enum dm_queue_mode type);
562e83068a5SMike Snitzer 
5637e0d574fSBart Van Assche /*
564e83068a5SMike Snitzer  * Finally call this to make the table ready for use.
565e83068a5SMike Snitzer  */
56617b2f66fSAlasdair G Kergon int dm_table_complete(struct dm_table *t);
56717b2f66fSAlasdair G Kergon 
56817b2f66fSAlasdair G Kergon /*
56917b2f66fSAlasdair G Kergon  * Destroy the table when finished.
57017b2f66fSAlasdair G Kergon  */
571f6e7baadSBrian Norris void dm_table_destroy(struct dm_table *t);
572f6e7baadSBrian Norris 
573f6e7baadSBrian Norris /*
574f6e7baadSBrian Norris  * Target may require that it is never sent I/O larger than len.
575f6e7baadSBrian Norris  */
576542f9038SMike Snitzer int __must_check dm_set_target_max_io_len(struct dm_target *ti, sector_t len);
577542f9038SMike Snitzer 
578542f9038SMike Snitzer /*
579542f9038SMike Snitzer  * Table reference counting.
580542f9038SMike Snitzer  */
58117b2f66fSAlasdair G Kergon struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx);
58217b2f66fSAlasdair G Kergon void dm_put_live_table(struct mapped_device *md, int srcu_idx);
58383d5e5b0SMikulas Patocka void dm_sync_table(struct mapped_device *md);
58483d5e5b0SMikulas Patocka 
58583d5e5b0SMikulas Patocka /*
58617b2f66fSAlasdair G Kergon  * Queries
58717b2f66fSAlasdair G Kergon  */
58817b2f66fSAlasdair G Kergon sector_t dm_table_get_size(struct dm_table *t);
58917b2f66fSAlasdair G Kergon blk_mode_t dm_table_get_mode(struct dm_table *t);
59017b2f66fSAlasdair G Kergon struct mapped_device *dm_table_get_md(struct dm_table *t);
591aeb5d727SAl Viro const char *dm_table_device_name(struct dm_table *t);
59217b2f66fSAlasdair G Kergon 
593f349b0a3SMichał Mirosław /*
59417b2f66fSAlasdair G Kergon  * Trigger an event.
59517b2f66fSAlasdair G Kergon  */
59617b2f66fSAlasdair G Kergon void dm_table_event(struct dm_table *t);
59717b2f66fSAlasdair G Kergon 
59817b2f66fSAlasdair G Kergon /*
59917b2f66fSAlasdair G Kergon  * Run the queue for request-based targets.
60017b2f66fSAlasdair G Kergon  */
6019974fa2cSMike Snitzer void dm_table_run_md_queue_async(struct dm_table *t);
6029974fa2cSMike Snitzer 
6039974fa2cSMike Snitzer /*
6049974fa2cSMike Snitzer  * The device must be suspended before calling this method.
6059974fa2cSMike Snitzer  * Returns the previous table, which the caller must destroy.
60617b2f66fSAlasdair G Kergon  */
607042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md,
60817b2f66fSAlasdair G Kergon 			       struct dm_table *t);
609042d2a9bSAlasdair G Kergon 
610042d2a9bSAlasdair G Kergon /*
61117b2f66fSAlasdair G Kergon  * Table blk_crypto_profile functions
61254160904SMikulas Patocka  */
613cb77cb5aSEric Biggers void dm_destroy_crypto_profile(struct blk_crypto_profile *profile);
614aa6ce87aSSatya Tangirala 
615cb77cb5aSEric Biggers /*
616aa6ce87aSSatya Tangirala  *---------------------------------------------------------------
617a4a82ce3SHeinz Mauelshagen  * Macros.
618a4a82ce3SHeinz Mauelshagen  *---------------------------------------------------------------
6190da336e5SAlasdair G Kergon  */
620a4a82ce3SHeinz Mauelshagen #define DM_NAME "device-mapper"
621a4a82ce3SHeinz Mauelshagen 
6220da336e5SAlasdair G Kergon #define DM_FMT(fmt) DM_NAME ": " DM_MSG_PREFIX ": " fmt "\n"
6230da336e5SAlasdair G Kergon 
624d2c3c8dcSJoe Perches #define DMCRIT(fmt, ...) pr_crit(DM_FMT(fmt), ##__VA_ARGS__)
62510d3bd09SMikulas Patocka 
626d2c3c8dcSJoe Perches #define DMERR(fmt, ...) pr_err(DM_FMT(fmt), ##__VA_ARGS__)
627d2c3c8dcSJoe Perches #define DMERR_LIMIT(fmt, ...) pr_err_ratelimited(DM_FMT(fmt), ##__VA_ARGS__)
628d2c3c8dcSJoe Perches #define DMWARN(fmt, ...) pr_warn(DM_FMT(fmt), ##__VA_ARGS__)
629733232f8SMike Snitzer #define DMWARN_LIMIT(fmt, ...) pr_warn_ratelimited(DM_FMT(fmt), ##__VA_ARGS__)
630d2c3c8dcSJoe Perches #define DMINFO(fmt, ...) pr_info(DM_FMT(fmt), ##__VA_ARGS__)
631733232f8SMike Snitzer #define DMINFO_LIMIT(fmt, ...) pr_info_ratelimited(DM_FMT(fmt), ##__VA_ARGS__)
632d2c3c8dcSJoe Perches 
633733232f8SMike Snitzer #define DMDEBUG(fmt, ...) pr_debug(DM_FMT(fmt), ##__VA_ARGS__)
6340da336e5SAlasdair G Kergon #define DMDEBUG_LIMIT(fmt, ...) pr_debug_ratelimited(DM_FMT(fmt), ##__VA_ARGS__)
63574244b59SHannes Reinecke 
636733232f8SMike Snitzer #define DMEMIT(x...) (sz += ((sz >= maxlen) ? 0 : scnprintf(result + sz, maxlen - sz, x)))
6370da336e5SAlasdair G Kergon 
63844bc08edSHeinz Mauelshagen #define DMEMIT_TARGET_NAME_VERSION(y) \
6390da336e5SAlasdair G Kergon 		DMEMIT("target_name=%s,target_version=%u.%u.%u", \
6408ec45662STushar Sugandhi 		       (y)->name, (y)->version[0], (y)->version[1], (y)->version[2])
6418ec45662STushar Sugandhi 
6428ec45662STushar Sugandhi /**
6438ec45662STushar Sugandhi  * module_dm() - Helper macro for DM targets that don't do anything
6443664ff82SYangtao Li  * special in their module_init and module_exit.
6453664ff82SYangtao Li  * Each module may only use this macro once, and calling it replaces
6463664ff82SYangtao Li  * module_init() and module_exit().
6473664ff82SYangtao Li  *
6483664ff82SYangtao Li  * @name: DM target's name
6493664ff82SYangtao Li  */
6503664ff82SYangtao Li #define module_dm(name) \
6513664ff82SYangtao Li static int __init dm_##name##_init(void) \
6523664ff82SYangtao Li { \
6533664ff82SYangtao Li 	return dm_register_target(&(name##_target)); \
6543664ff82SYangtao Li } \
6553664ff82SYangtao Li module_init(dm_##name##_init) \
6563664ff82SYangtao Li static void __exit dm_##name##_exit(void) \
6573664ff82SYangtao Li { \
6583664ff82SYangtao Li 	dm_unregister_target(&(name##_target)); \
6593664ff82SYangtao Li } \
6603664ff82SYangtao Li module_exit(dm_##name##_exit)
6613664ff82SYangtao Li 
6623664ff82SYangtao Li /*
6633664ff82SYangtao Li  * Definitions of return values from target end_io function.
6640da336e5SAlasdair G Kergon  */
6650da336e5SAlasdair G Kergon #define DM_ENDIO_DONE		0
6660da336e5SAlasdair G Kergon #define DM_ENDIO_INCOMPLETE	1
6677ed8578aSChristoph Hellwig #define DM_ENDIO_REQUEUE	2
6680da336e5SAlasdair G Kergon #define DM_ENDIO_DELAY_REQUEUE	3
6690da336e5SAlasdair G Kergon 
670ac514ffcSMike Snitzer /*
6710da336e5SAlasdair G Kergon  * Definitions of return values from target map function.
6720da336e5SAlasdair G Kergon  */
6730da336e5SAlasdair G Kergon #define DM_MAPIO_SUBMITTED	0
6740da336e5SAlasdair G Kergon #define DM_MAPIO_REMAPPED	1
6750da336e5SAlasdair G Kergon #define DM_MAPIO_REQUEUE	DM_ENDIO_REQUEUE
6760da336e5SAlasdair G Kergon #define DM_MAPIO_DELAY_REQUEUE	DM_ENDIO_DELAY_REQUEUE
6770da336e5SAlasdair G Kergon #define DM_MAPIO_KILL		4
678ac514ffcSMike Snitzer 
679412445acSChristoph Hellwig #define dm_sector_div64(x, y)( \
6800da336e5SAlasdair G Kergon { \
681fd2ed4d2SMikulas Patocka 	u64 _res; \
682fd2ed4d2SMikulas Patocka 	(x) = div64_u64_rem(x, y, &_res); \
683fd2ed4d2SMikulas Patocka 	_res; \
684fd2ed4d2SMikulas Patocka } \
685fd2ed4d2SMikulas Patocka )
686fd2ed4d2SMikulas Patocka 
687fd2ed4d2SMikulas Patocka /*
688fd2ed4d2SMikulas Patocka  * Ceiling(n / sz)
6890da336e5SAlasdair G Kergon  */
6900da336e5SAlasdair G Kergon #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz))
6910da336e5SAlasdair G Kergon 
6920da336e5SAlasdair G Kergon #define dm_sector_div_up(n, sz) ( \
6930da336e5SAlasdair G Kergon { \
6940da336e5SAlasdair G Kergon 	sector_t _r = ((n) + (sz) - 1); \
6950da336e5SAlasdair G Kergon 	sector_div(_r, (sz)); \
6960da336e5SAlasdair G Kergon 	_r; \
6970da336e5SAlasdair G Kergon } \
6980da336e5SAlasdair G Kergon )
6990da336e5SAlasdair G Kergon 
7000da336e5SAlasdair G Kergon /*
7010da336e5SAlasdair G Kergon  * ceiling(n / size) * size
7020da336e5SAlasdair G Kergon  */
7030da336e5SAlasdair G Kergon #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
7040da336e5SAlasdair G Kergon 
7050da336e5SAlasdair G Kergon /*
7060da336e5SAlasdair G Kergon  * Sector offset taken relative to the start of the target instead of
70756a67df7SMike Snitzer  * relative to the start of the device.
70856a67df7SMike Snitzer  */
70956a67df7SMike Snitzer #define dm_target_offset(ti, sector) ((sector) - (ti)->begin)
71056a67df7SMike Snitzer 
to_sector(unsigned long long n)71156a67df7SMike Snitzer static inline sector_t to_sector(unsigned long long n)
71256a67df7SMike Snitzer {
7130bdb50c5SNeilBrown 	return (n >> SECTOR_SHIFT);
7140da336e5SAlasdair G Kergon }
7150da336e5SAlasdair G Kergon 
to_bytes(sector_t n)7160da336e5SAlasdair G Kergon static inline unsigned long to_bytes(sector_t n)
7170da336e5SAlasdair G Kergon {
7180da336e5SAlasdair G Kergon 	return (n << SECTOR_SHIFT);
7190da336e5SAlasdair G Kergon }
7200da336e5SAlasdair G Kergon 
7210da336e5SAlasdair G Kergon #endif	/* _LINUX_DEVICE_MAPPER_H */
7220da336e5SAlasdair G Kergon