xref: /openbmc/linux/include/linux/device-mapper.h (revision 0ac55489d9e3898987b2ae305844cf2af86e6b8d)
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>
1371a16736SNamhyung Kim #include <linux/ratelimit.h>
14416cd17bSHeinz Mauelshagen 
15af4874e0SMike Snitzer struct dm_dev;
161da177e4SLinus Torvalds struct dm_target;
171da177e4SLinus Torvalds struct dm_table;
1817b2f66fSAlasdair G Kergon struct mapped_device;
19f6fccb12SMilan Broz struct bio_vec;
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE } status_type_t;
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds union map_info {
241da177e4SLinus Torvalds 	void *ptr;
251da177e4SLinus Torvalds 	unsigned long long ll;
2657cba5d3SMike Snitzer 	unsigned target_request_nr;
271da177e4SLinus Torvalds };
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds /*
301da177e4SLinus Torvalds  * In the constructor the target parameter will already have the
311da177e4SLinus Torvalds  * table, type, begin and len fields filled in.
321da177e4SLinus Torvalds  */
331da177e4SLinus Torvalds typedef int (*dm_ctr_fn) (struct dm_target *target,
341da177e4SLinus Torvalds 			  unsigned int argc, char **argv);
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds /*
371da177e4SLinus Torvalds  * The destructor doesn't need to free the dm_target, just
381da177e4SLinus Torvalds  * anything hidden ti->private.
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds typedef void (*dm_dtr_fn) (struct dm_target *ti);
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds /*
431da177e4SLinus Torvalds  * The map function must return:
441da177e4SLinus Torvalds  * < 0: error
451da177e4SLinus Torvalds  * = 0: The target will handle the io by resubmitting it later
4645cbcd79SKiyoshi Ueda  * = 1: simple remap complete
472e93ccc1SKiyoshi Ueda  * = 2: The target wants to push back the io
481da177e4SLinus Torvalds  */
491da177e4SLinus Torvalds typedef int (*dm_map_fn) (struct dm_target *ti, struct bio *bio,
501da177e4SLinus Torvalds 			  union map_info *map_context);
517d76345dSKiyoshi Ueda typedef int (*dm_map_request_fn) (struct dm_target *ti, struct request *clone,
527d76345dSKiyoshi Ueda 				  union map_info *map_context);
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds /*
551da177e4SLinus Torvalds  * Returns:
561da177e4SLinus Torvalds  * < 0 : error (currently ignored)
571da177e4SLinus Torvalds  * 0   : ended successfully
581da177e4SLinus Torvalds  * 1   : for some reason the io has still not completed (eg,
591da177e4SLinus Torvalds  *       multipath target might want to requeue a failed io).
602e93ccc1SKiyoshi Ueda  * 2   : The target wants to push back the io
611da177e4SLinus Torvalds  */
621da177e4SLinus Torvalds typedef int (*dm_endio_fn) (struct dm_target *ti,
631da177e4SLinus Torvalds 			    struct bio *bio, int error,
641da177e4SLinus Torvalds 			    union map_info *map_context);
657d76345dSKiyoshi Ueda typedef int (*dm_request_endio_fn) (struct dm_target *ti,
667d76345dSKiyoshi Ueda 				    struct request *clone, int error,
677d76345dSKiyoshi Ueda 				    union map_info *map_context);
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds typedef void (*dm_presuspend_fn) (struct dm_target *ti);
701da177e4SLinus Torvalds typedef void (*dm_postsuspend_fn) (struct dm_target *ti);
718757b776SMilan Broz typedef int (*dm_preresume_fn) (struct dm_target *ti);
721da177e4SLinus Torvalds typedef void (*dm_resume_fn) (struct dm_target *ti);
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds typedef int (*dm_status_fn) (struct dm_target *ti, status_type_t status_type,
751da177e4SLinus Torvalds 			     char *result, unsigned int maxlen);
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds typedef int (*dm_message_fn) (struct dm_target *ti, unsigned argc, char **argv);
781da177e4SLinus Torvalds 
79647b3d00SAl Viro typedef int (*dm_ioctl_fn) (struct dm_target *ti, unsigned int cmd,
80aa129a22SMilan Broz 			    unsigned long arg);
81aa129a22SMilan Broz 
82f6fccb12SMilan Broz typedef int (*dm_merge_fn) (struct dm_target *ti, struct bvec_merge_data *bvm,
83f6fccb12SMilan Broz 			    struct bio_vec *biovec, int max_size);
84f6fccb12SMilan Broz 
85af4874e0SMike Snitzer typedef int (*iterate_devices_callout_fn) (struct dm_target *ti,
86af4874e0SMike Snitzer 					   struct dm_dev *dev,
875dea271bSMike Snitzer 					   sector_t start, sector_t len,
88af4874e0SMike Snitzer 					   void *data);
89af4874e0SMike Snitzer 
90af4874e0SMike Snitzer typedef int (*dm_iterate_devices_fn) (struct dm_target *ti,
91af4874e0SMike Snitzer 				      iterate_devices_callout_fn fn,
92af4874e0SMike Snitzer 				      void *data);
93af4874e0SMike Snitzer 
9440bea431SMike Snitzer typedef void (*dm_io_hints_fn) (struct dm_target *ti,
9540bea431SMike Snitzer 				struct queue_limits *limits);
9640bea431SMike Snitzer 
977d76345dSKiyoshi Ueda /*
987d76345dSKiyoshi Ueda  * Returns:
997d76345dSKiyoshi Ueda  *    0: The target can handle the next I/O immediately.
1007d76345dSKiyoshi Ueda  *    1: The target can't handle the next I/O immediately.
1017d76345dSKiyoshi Ueda  */
1027d76345dSKiyoshi Ueda typedef int (*dm_busy_fn) (struct dm_target *ti);
1037d76345dSKiyoshi Ueda 
1041da177e4SLinus Torvalds void dm_error(const char *message);
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds /*
1073cb40214SBryn Reeves  * Combine device limits.
1083cb40214SBryn Reeves  */
109754c5fc7SMike Snitzer int dm_set_device_limits(struct dm_target *ti, struct dm_dev *dev,
1105dea271bSMike Snitzer 			 sector_t start, sector_t len, void *data);
1113cb40214SBryn Reeves 
11282b1519bSMikulas Patocka struct dm_dev {
11382b1519bSMikulas Patocka 	struct block_device *bdev;
114aeb5d727SAl Viro 	fmode_t mode;
11582b1519bSMikulas Patocka 	char name[16];
11682b1519bSMikulas Patocka };
11782b1519bSMikulas Patocka 
1183cb40214SBryn Reeves /*
1191da177e4SLinus Torvalds  * Constructors should call these functions to ensure destination devices
1201da177e4SLinus Torvalds  * are opened/closed correctly.
1211da177e4SLinus Torvalds  */
1228215d6ecSNikanth Karthikesan int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
1238215d6ecSNikanth Karthikesan 						 struct dm_dev **result);
1241da177e4SLinus Torvalds void dm_put_device(struct dm_target *ti, struct dm_dev *d);
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds /*
1271da177e4SLinus Torvalds  * Information about a target type
1281da177e4SLinus Torvalds  */
129ab4c1424SAndi Kleen 
1301da177e4SLinus Torvalds struct target_type {
131ab4c1424SAndi Kleen 	uint64_t features;
1321da177e4SLinus Torvalds 	const char *name;
1331da177e4SLinus Torvalds 	struct module *module;
1341da177e4SLinus Torvalds 	unsigned version[3];
1351da177e4SLinus Torvalds 	dm_ctr_fn ctr;
1361da177e4SLinus Torvalds 	dm_dtr_fn dtr;
1371da177e4SLinus Torvalds 	dm_map_fn map;
1387d76345dSKiyoshi Ueda 	dm_map_request_fn map_rq;
1391da177e4SLinus Torvalds 	dm_endio_fn end_io;
1407d76345dSKiyoshi Ueda 	dm_request_endio_fn rq_end_io;
1411da177e4SLinus Torvalds 	dm_presuspend_fn presuspend;
1421da177e4SLinus Torvalds 	dm_postsuspend_fn postsuspend;
1438757b776SMilan Broz 	dm_preresume_fn preresume;
1441da177e4SLinus Torvalds 	dm_resume_fn resume;
1451da177e4SLinus Torvalds 	dm_status_fn status;
1461da177e4SLinus Torvalds 	dm_message_fn message;
147aa129a22SMilan Broz 	dm_ioctl_fn ioctl;
148f6fccb12SMilan Broz 	dm_merge_fn merge;
1497d76345dSKiyoshi Ueda 	dm_busy_fn busy;
150af4874e0SMike Snitzer 	dm_iterate_devices_fn iterate_devices;
15140bea431SMike Snitzer 	dm_io_hints_fn io_hints;
15245194e4fSCheng Renquan 
15345194e4fSCheng Renquan 	/* For internal device-mapper use. */
15445194e4fSCheng Renquan 	struct list_head list;
1551da177e4SLinus Torvalds };
1561da177e4SLinus Torvalds 
1573791e2fcSAlasdair G Kergon /*
1583791e2fcSAlasdair G Kergon  * Target features
1593791e2fcSAlasdair G Kergon  */
1603791e2fcSAlasdair G Kergon 
1613791e2fcSAlasdair G Kergon /*
1623791e2fcSAlasdair G Kergon  * Any table that contains an instance of this target must have only one.
1633791e2fcSAlasdair G Kergon  */
1643791e2fcSAlasdair G Kergon #define DM_TARGET_SINGLETON		0x00000001
1653791e2fcSAlasdair G Kergon #define dm_target_needs_singleton(type)	((type)->features & DM_TARGET_SINGLETON)
1663791e2fcSAlasdair G Kergon 
167cc6cbe14SAlasdair G Kergon /*
168cc6cbe14SAlasdair G Kergon  * Indicates that a target does not support read-only devices.
169cc6cbe14SAlasdair G Kergon  */
170cc6cbe14SAlasdair G Kergon #define DM_TARGET_ALWAYS_WRITEABLE	0x00000002
171cc6cbe14SAlasdair G Kergon #define dm_target_always_writeable(type) \
172cc6cbe14SAlasdair G Kergon 		((type)->features & DM_TARGET_ALWAYS_WRITEABLE)
173cc6cbe14SAlasdair G Kergon 
17436a0456fSAlasdair G Kergon /*
17536a0456fSAlasdair G Kergon  * Any device that contains a table with an instance of this target may never
17636a0456fSAlasdair G Kergon  * have tables containing any different target type.
17736a0456fSAlasdair G Kergon  */
17836a0456fSAlasdair G Kergon #define DM_TARGET_IMMUTABLE		0x00000004
17936a0456fSAlasdair G Kergon #define dm_target_is_immutable(type)	((type)->features & DM_TARGET_IMMUTABLE)
18036a0456fSAlasdair G Kergon 
1811da177e4SLinus Torvalds struct dm_target {
1821da177e4SLinus Torvalds 	struct dm_table *table;
1831da177e4SLinus Torvalds 	struct target_type *type;
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	/* target limits */
1861da177e4SLinus Torvalds 	sector_t begin;
1871da177e4SLinus Torvalds 	sector_t len;
1881da177e4SLinus Torvalds 
189542f9038SMike Snitzer 	/* If non-zero, maximum size of I/O submitted to a target. */
190542f9038SMike Snitzer 	uint32_t max_io_len;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	/*
193f9ab94ceSMikulas Patocka 	 * A number of zero-length barrier requests that will be submitted
194f9ab94ceSMikulas Patocka 	 * to the target for the purpose of flushing cache.
195f9ab94ceSMikulas Patocka 	 *
19657cba5d3SMike Snitzer 	 * The request number will be placed in union map_info->target_request_nr.
197f9ab94ceSMikulas Patocka 	 * It is a responsibility of the target driver to remap these requests
198f9ab94ceSMikulas Patocka 	 * to the real underlying devices.
199f9ab94ceSMikulas Patocka 	 */
200f9ab94ceSMikulas Patocka 	unsigned num_flush_requests;
201f9ab94ceSMikulas Patocka 
2025ae89a87SMike Snitzer 	/*
2035ae89a87SMike Snitzer 	 * The number of discard requests that will be submitted to the
2045ae89a87SMike Snitzer 	 * target.  map_info->request_nr is used just like num_flush_requests.
2055ae89a87SMike Snitzer 	 */
2065ae89a87SMike Snitzer 	unsigned num_discard_requests;
2075ae89a87SMike Snitzer 
2081da177e4SLinus Torvalds 	/* target specific data */
2091da177e4SLinus Torvalds 	void *private;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	/* Used to provide an error string from the ctr */
2121da177e4SLinus Torvalds 	char *error;
2134c259327SMike Snitzer 
2144c259327SMike Snitzer 	/*
2150e9c24edSJoe Thornber 	 * Set if this target needs to receive flushes regardless of
2160e9c24edSJoe Thornber 	 * whether or not its underlying devices have support.
2170e9c24edSJoe Thornber 	 */
2180e9c24edSJoe Thornber 	bool flush_supported:1;
2190e9c24edSJoe Thornber 
2200e9c24edSJoe Thornber 	/*
2214c259327SMike Snitzer 	 * Set if this target needs to receive discards regardless of
2224c259327SMike Snitzer 	 * whether or not its underlying devices have support.
2234c259327SMike Snitzer 	 */
224*0ac55489SAlasdair G Kergon 	bool discards_supported:1;
225983c7db3SMilan Broz 
226983c7db3SMilan Broz 	/*
2277acf0277SMikulas Patocka 	 * Set if the target required discard request to be split
2287acf0277SMikulas Patocka 	 * on max_io_len boundary.
2297acf0277SMikulas Patocka 	 */
230*0ac55489SAlasdair G Kergon 	bool split_discard_requests:1;
2317acf0277SMikulas Patocka 
2327acf0277SMikulas Patocka 	/*
233983c7db3SMilan Broz 	 * Set if this target does not return zeroes on discarded blocks.
234983c7db3SMilan Broz 	 */
235*0ac55489SAlasdair G Kergon 	bool discard_zeroes_data_unsupported:1;
2361da177e4SLinus Torvalds };
2371da177e4SLinus Torvalds 
2389d357b07SNeilBrown /* Each target can link one of these into the table */
2399d357b07SNeilBrown struct dm_target_callbacks {
2409d357b07SNeilBrown 	struct list_head list;
2419d357b07SNeilBrown 	int (*congested_fn) (struct dm_target_callbacks *, int);
2429d357b07SNeilBrown };
2439d357b07SNeilBrown 
2441da177e4SLinus Torvalds int dm_register_target(struct target_type *t);
24510d3bd09SMikulas Patocka void dm_unregister_target(struct target_type *t);
24617b2f66fSAlasdair G Kergon 
247498f0103SMike Snitzer /*
248498f0103SMike Snitzer  * Target argument parsing.
249498f0103SMike Snitzer  */
250498f0103SMike Snitzer struct dm_arg_set {
251498f0103SMike Snitzer 	unsigned argc;
252498f0103SMike Snitzer 	char **argv;
253498f0103SMike Snitzer };
254498f0103SMike Snitzer 
255498f0103SMike Snitzer /*
256498f0103SMike Snitzer  * The minimum and maximum value of a numeric argument, together with
257498f0103SMike Snitzer  * the error message to use if the number is found to be outside that range.
258498f0103SMike Snitzer  */
259498f0103SMike Snitzer struct dm_arg {
260498f0103SMike Snitzer 	unsigned min;
261498f0103SMike Snitzer 	unsigned max;
262498f0103SMike Snitzer 	char *error;
263498f0103SMike Snitzer };
264498f0103SMike Snitzer 
265498f0103SMike Snitzer /*
266498f0103SMike Snitzer  * Validate the next argument, either returning it as *value or, if invalid,
267498f0103SMike Snitzer  * returning -EINVAL and setting *error.
268498f0103SMike Snitzer  */
269498f0103SMike Snitzer int dm_read_arg(struct dm_arg *arg, struct dm_arg_set *arg_set,
270498f0103SMike Snitzer 		unsigned *value, char **error);
271498f0103SMike Snitzer 
272498f0103SMike Snitzer /*
273498f0103SMike Snitzer  * Process the next argument as the start of a group containing between
274498f0103SMike Snitzer  * arg->min and arg->max further arguments. Either return the size as
275498f0103SMike Snitzer  * *num_args or, if invalid, return -EINVAL and set *error.
276498f0103SMike Snitzer  */
277498f0103SMike Snitzer int dm_read_arg_group(struct dm_arg *arg, struct dm_arg_set *arg_set,
278498f0103SMike Snitzer 		      unsigned *num_args, char **error);
279498f0103SMike Snitzer 
280498f0103SMike Snitzer /*
281498f0103SMike Snitzer  * Return the current argument and shift to the next.
282498f0103SMike Snitzer  */
283498f0103SMike Snitzer const char *dm_shift_arg(struct dm_arg_set *as);
284498f0103SMike Snitzer 
285498f0103SMike Snitzer /*
286498f0103SMike Snitzer  * Move through num_args arguments.
287498f0103SMike Snitzer  */
288498f0103SMike Snitzer void dm_consume_args(struct dm_arg_set *as, unsigned num_args);
289498f0103SMike Snitzer 
29017b2f66fSAlasdair G Kergon /*-----------------------------------------------------------------
29117b2f66fSAlasdair G Kergon  * Functions for creating and manipulating mapped devices.
29217b2f66fSAlasdair G Kergon  * Drop the reference with dm_put when you finish with the object.
29317b2f66fSAlasdair G Kergon  *---------------------------------------------------------------*/
29417b2f66fSAlasdair G Kergon 
29517b2f66fSAlasdair G Kergon /*
29617b2f66fSAlasdair G Kergon  * DM_ANY_MINOR chooses the next available minor number.
29717b2f66fSAlasdair G Kergon  */
29817b2f66fSAlasdair G Kergon #define DM_ANY_MINOR (-1)
29917b2f66fSAlasdair G Kergon int dm_create(int minor, struct mapped_device **md);
30017b2f66fSAlasdair G Kergon 
30117b2f66fSAlasdair G Kergon /*
30217b2f66fSAlasdair G Kergon  * Reference counting for md.
30317b2f66fSAlasdair G Kergon  */
30417b2f66fSAlasdair G Kergon struct mapped_device *dm_get_md(dev_t dev);
30517b2f66fSAlasdair G Kergon void dm_get(struct mapped_device *md);
30617b2f66fSAlasdair G Kergon void dm_put(struct mapped_device *md);
30717b2f66fSAlasdair G Kergon 
30817b2f66fSAlasdair G Kergon /*
30917b2f66fSAlasdair G Kergon  * An arbitrary pointer may be stored alongside a mapped device.
31017b2f66fSAlasdair G Kergon  */
31117b2f66fSAlasdair G Kergon void dm_set_mdptr(struct mapped_device *md, void *ptr);
31217b2f66fSAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md);
31317b2f66fSAlasdair G Kergon 
31417b2f66fSAlasdair G Kergon /*
31517b2f66fSAlasdair G Kergon  * A device can still be used while suspended, but I/O is deferred.
31617b2f66fSAlasdair G Kergon  */
317a3d77d35SKiyoshi Ueda int dm_suspend(struct mapped_device *md, unsigned suspend_flags);
31817b2f66fSAlasdair G Kergon int dm_resume(struct mapped_device *md);
31917b2f66fSAlasdair G Kergon 
32017b2f66fSAlasdair G Kergon /*
32117b2f66fSAlasdair G Kergon  * Event functions.
32217b2f66fSAlasdair G Kergon  */
32317b2f66fSAlasdair G Kergon uint32_t dm_get_event_nr(struct mapped_device *md);
32417b2f66fSAlasdair G Kergon int dm_wait_event(struct mapped_device *md, int event_nr);
3257a8c3d3bSMike Anderson uint32_t dm_next_uevent_seq(struct mapped_device *md);
3267a8c3d3bSMike Anderson void dm_uevent_add(struct mapped_device *md, struct list_head *elist);
32717b2f66fSAlasdair G Kergon 
32817b2f66fSAlasdair G Kergon /*
32917b2f66fSAlasdair G Kergon  * Info functions.
33017b2f66fSAlasdair G Kergon  */
33172d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md);
33296a1f7dbSMike Anderson int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid);
33317b2f66fSAlasdair G Kergon struct gendisk *dm_disk(struct mapped_device *md);
33464dbce58SKiyoshi Ueda int dm_suspended(struct dm_target *ti);
3352e93ccc1SKiyoshi Ueda int dm_noflush_suspending(struct dm_target *ti);
33689343da0SMikulas Patocka union map_info *dm_get_mapinfo(struct bio *bio);
337cec47e3dSKiyoshi Ueda union map_info *dm_get_rq_mapinfo(struct request *rq);
33817b2f66fSAlasdair G Kergon 
33917b2f66fSAlasdair G Kergon /*
34017b2f66fSAlasdair G Kergon  * Geometry functions.
34117b2f66fSAlasdair G Kergon  */
34217b2f66fSAlasdair G Kergon int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo);
34317b2f66fSAlasdair G Kergon int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo);
34417b2f66fSAlasdair G Kergon 
34517b2f66fSAlasdair G Kergon 
34617b2f66fSAlasdair G Kergon /*-----------------------------------------------------------------
34717b2f66fSAlasdair G Kergon  * Functions for manipulating device-mapper tables.
34817b2f66fSAlasdair G Kergon  *---------------------------------------------------------------*/
34917b2f66fSAlasdair G Kergon 
35017b2f66fSAlasdair G Kergon /*
35117b2f66fSAlasdair G Kergon  * First create an empty table.
35217b2f66fSAlasdair G Kergon  */
353aeb5d727SAl Viro int dm_table_create(struct dm_table **result, fmode_t mode,
35417b2f66fSAlasdair G Kergon 		    unsigned num_targets, struct mapped_device *md);
35517b2f66fSAlasdair G Kergon 
35617b2f66fSAlasdair G Kergon /*
35717b2f66fSAlasdair G Kergon  * Then call this once for each target.
35817b2f66fSAlasdair G Kergon  */
35917b2f66fSAlasdair G Kergon int dm_table_add_target(struct dm_table *t, const char *type,
36017b2f66fSAlasdair G Kergon 			sector_t start, sector_t len, char *params);
36117b2f66fSAlasdair G Kergon 
36217b2f66fSAlasdair G Kergon /*
3639d357b07SNeilBrown  * Target_ctr should call this if it needs to add any callbacks.
3649d357b07SNeilBrown  */
3659d357b07SNeilBrown void dm_table_add_target_callbacks(struct dm_table *t, struct dm_target_callbacks *cb);
3669d357b07SNeilBrown 
3679d357b07SNeilBrown /*
36817b2f66fSAlasdair G Kergon  * Finally call this to make the table ready for use.
36917b2f66fSAlasdair G Kergon  */
37017b2f66fSAlasdair G Kergon int dm_table_complete(struct dm_table *t);
37117b2f66fSAlasdair G Kergon 
37217b2f66fSAlasdair G Kergon /*
373542f9038SMike Snitzer  * Target may require that it is never sent I/O larger than len.
374542f9038SMike Snitzer  */
375542f9038SMike Snitzer int __must_check dm_set_target_max_io_len(struct dm_target *ti, sector_t len);
376542f9038SMike Snitzer 
377542f9038SMike Snitzer /*
37817b2f66fSAlasdair G Kergon  * Table reference counting.
37917b2f66fSAlasdair G Kergon  */
3807c666411SAlasdair G Kergon struct dm_table *dm_get_live_table(struct mapped_device *md);
38117b2f66fSAlasdair G Kergon void dm_table_get(struct dm_table *t);
38217b2f66fSAlasdair G Kergon void dm_table_put(struct dm_table *t);
38317b2f66fSAlasdair G Kergon 
38417b2f66fSAlasdair G Kergon /*
38517b2f66fSAlasdair G Kergon  * Queries
38617b2f66fSAlasdair G Kergon  */
38717b2f66fSAlasdair G Kergon sector_t dm_table_get_size(struct dm_table *t);
38817b2f66fSAlasdair G Kergon unsigned int dm_table_get_num_targets(struct dm_table *t);
389aeb5d727SAl Viro fmode_t dm_table_get_mode(struct dm_table *t);
39017b2f66fSAlasdair G Kergon struct mapped_device *dm_table_get_md(struct dm_table *t);
39117b2f66fSAlasdair G Kergon 
39217b2f66fSAlasdair G Kergon /*
39317b2f66fSAlasdair G Kergon  * Trigger an event.
39417b2f66fSAlasdair G Kergon  */
39517b2f66fSAlasdair G Kergon void dm_table_event(struct dm_table *t);
39617b2f66fSAlasdair G Kergon 
39717b2f66fSAlasdair G Kergon /*
39817b2f66fSAlasdair G Kergon  * The device must be suspended before calling this method.
399042d2a9bSAlasdair G Kergon  * Returns the previous table, which the caller must destroy.
40017b2f66fSAlasdair G Kergon  */
401042d2a9bSAlasdair G Kergon struct dm_table *dm_swap_table(struct mapped_device *md,
402042d2a9bSAlasdair G Kergon 			       struct dm_table *t);
40317b2f66fSAlasdair G Kergon 
40454160904SMikulas Patocka /*
40554160904SMikulas Patocka  * A wrapper around vmalloc.
40654160904SMikulas Patocka  */
40754160904SMikulas Patocka void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size);
40854160904SMikulas Patocka 
4090da336e5SAlasdair G Kergon /*-----------------------------------------------------------------
4100da336e5SAlasdair G Kergon  * Macros.
4110da336e5SAlasdair G Kergon  *---------------------------------------------------------------*/
4120da336e5SAlasdair G Kergon #define DM_NAME "device-mapper"
4130da336e5SAlasdair G Kergon 
41471a16736SNamhyung Kim #ifdef CONFIG_PRINTK
41571a16736SNamhyung Kim extern struct ratelimit_state dm_ratelimit_state;
41671a16736SNamhyung Kim 
41771a16736SNamhyung Kim #define dm_ratelimit()	__ratelimit(&dm_ratelimit_state)
41871a16736SNamhyung Kim #else
41971a16736SNamhyung Kim #define dm_ratelimit()	0
42071a16736SNamhyung Kim #endif
42171a16736SNamhyung Kim 
42210d3bd09SMikulas Patocka #define DMCRIT(f, arg...) \
42310d3bd09SMikulas Patocka 	printk(KERN_CRIT DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
42410d3bd09SMikulas Patocka 
4250da336e5SAlasdair G Kergon #define DMERR(f, arg...) \
4260da336e5SAlasdair G Kergon 	printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
4270da336e5SAlasdair G Kergon #define DMERR_LIMIT(f, arg...) \
4280da336e5SAlasdair G Kergon 	do { \
42971a16736SNamhyung Kim 		if (dm_ratelimit())	\
4300da336e5SAlasdair G Kergon 			printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " \
4310da336e5SAlasdair G Kergon 			       f "\n", ## arg); \
4320da336e5SAlasdair G Kergon 	} while (0)
4330da336e5SAlasdair G Kergon 
4340da336e5SAlasdair G Kergon #define DMWARN(f, arg...) \
4350da336e5SAlasdair G Kergon 	printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
4360da336e5SAlasdair G Kergon #define DMWARN_LIMIT(f, arg...) \
4370da336e5SAlasdair G Kergon 	do { \
43871a16736SNamhyung Kim 		if (dm_ratelimit())	\
4390da336e5SAlasdair G Kergon 			printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " \
4400da336e5SAlasdair G Kergon 			       f "\n", ## arg); \
4410da336e5SAlasdair G Kergon 	} while (0)
4420da336e5SAlasdair G Kergon 
4430da336e5SAlasdair G Kergon #define DMINFO(f, arg...) \
4440da336e5SAlasdair G Kergon 	printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
4450da336e5SAlasdair G Kergon #define DMINFO_LIMIT(f, arg...) \
4460da336e5SAlasdair G Kergon 	do { \
44771a16736SNamhyung Kim 		if (dm_ratelimit())	\
4480da336e5SAlasdair G Kergon 			printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f \
4490da336e5SAlasdair G Kergon 			       "\n", ## arg); \
4500da336e5SAlasdair G Kergon 	} while (0)
4510da336e5SAlasdair G Kergon 
4520da336e5SAlasdair G Kergon #ifdef CONFIG_DM_DEBUG
4530da336e5SAlasdair G Kergon #  define DMDEBUG(f, arg...) \
4540da336e5SAlasdair G Kergon 	printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX " DEBUG: " f "\n", ## arg)
4550da336e5SAlasdair G Kergon #  define DMDEBUG_LIMIT(f, arg...) \
4560da336e5SAlasdair G Kergon 	do { \
45771a16736SNamhyung Kim 		if (dm_ratelimit())	\
4580da336e5SAlasdair G Kergon 			printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX ": " f \
4590da336e5SAlasdair G Kergon 			       "\n", ## arg); \
4600da336e5SAlasdair G Kergon 	} while (0)
4610da336e5SAlasdair G Kergon #else
4620da336e5SAlasdair G Kergon #  define DMDEBUG(f, arg...) do {} while (0)
4630da336e5SAlasdair G Kergon #  define DMDEBUG_LIMIT(f, arg...) do {} while (0)
4640da336e5SAlasdair G Kergon #endif
4650da336e5SAlasdair G Kergon 
4660da336e5SAlasdair G Kergon #define DMEMIT(x...) sz += ((sz >= maxlen) ? \
4670da336e5SAlasdair G Kergon 			  0 : scnprintf(result + sz, maxlen - sz, x))
4680da336e5SAlasdair G Kergon 
4690da336e5SAlasdair G Kergon #define SECTOR_SHIFT 9
4700da336e5SAlasdair G Kergon 
4710da336e5SAlasdair G Kergon /*
4720da336e5SAlasdair G Kergon  * Definitions of return values from target end_io function.
4730da336e5SAlasdair G Kergon  */
4740da336e5SAlasdair G Kergon #define DM_ENDIO_INCOMPLETE	1
4750da336e5SAlasdair G Kergon #define DM_ENDIO_REQUEUE	2
4760da336e5SAlasdair G Kergon 
4770da336e5SAlasdair G Kergon /*
4780da336e5SAlasdair G Kergon  * Definitions of return values from target map function.
4790da336e5SAlasdair G Kergon  */
4800da336e5SAlasdair G Kergon #define DM_MAPIO_SUBMITTED	0
4810da336e5SAlasdair G Kergon #define DM_MAPIO_REMAPPED	1
4820da336e5SAlasdair G Kergon #define DM_MAPIO_REQUEUE	DM_ENDIO_REQUEUE
4830da336e5SAlasdair G Kergon 
4840da336e5SAlasdair G Kergon /*
4850da336e5SAlasdair G Kergon  * Ceiling(n / sz)
4860da336e5SAlasdair G Kergon  */
4870da336e5SAlasdair G Kergon #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz))
4880da336e5SAlasdair G Kergon 
4890da336e5SAlasdair G Kergon #define dm_sector_div_up(n, sz) ( \
4900da336e5SAlasdair G Kergon { \
4910da336e5SAlasdair G Kergon 	sector_t _r = ((n) + (sz) - 1); \
4920da336e5SAlasdair G Kergon 	sector_div(_r, (sz)); \
4930da336e5SAlasdair G Kergon 	_r; \
4940da336e5SAlasdair G Kergon } \
4950da336e5SAlasdair G Kergon )
4960da336e5SAlasdair G Kergon 
4970da336e5SAlasdair G Kergon /*
4980da336e5SAlasdair G Kergon  * ceiling(n / size) * size
4990da336e5SAlasdair G Kergon  */
5000da336e5SAlasdair G Kergon #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
5010da336e5SAlasdair G Kergon 
502d63a5ce3SMikulas Patocka #define dm_array_too_big(fixed, obj, num) \
503d63a5ce3SMikulas Patocka 	((num) > (UINT_MAX - (fixed)) / (obj))
504d63a5ce3SMikulas Patocka 
50556a67df7SMike Snitzer /*
50656a67df7SMike Snitzer  * Sector offset taken relative to the start of the target instead of
50756a67df7SMike Snitzer  * relative to the start of the device.
50856a67df7SMike Snitzer  */
50956a67df7SMike Snitzer #define dm_target_offset(ti, sector) ((sector) - (ti)->begin)
51056a67df7SMike Snitzer 
5110da336e5SAlasdair G Kergon static inline sector_t to_sector(unsigned long n)
5120da336e5SAlasdair G Kergon {
5130da336e5SAlasdair G Kergon 	return (n >> SECTOR_SHIFT);
5140da336e5SAlasdair G Kergon }
5150da336e5SAlasdair G Kergon 
5160da336e5SAlasdair G Kergon static inline unsigned long to_bytes(sector_t n)
5170da336e5SAlasdair G Kergon {
5180da336e5SAlasdair G Kergon 	return (n << SECTOR_SHIFT);
5190da336e5SAlasdair G Kergon }
5200da336e5SAlasdair G Kergon 
521cec47e3dSKiyoshi Ueda /*-----------------------------------------------------------------
522cec47e3dSKiyoshi Ueda  * Helper for block layer and dm core operations
523cec47e3dSKiyoshi Ueda  *---------------------------------------------------------------*/
524cec47e3dSKiyoshi Ueda void dm_dispatch_request(struct request *rq);
525cec47e3dSKiyoshi Ueda void dm_requeue_unmapped_request(struct request *rq);
526cec47e3dSKiyoshi Ueda void dm_kill_unmapped_request(struct request *rq, int error);
527cec47e3dSKiyoshi Ueda int dm_underlying_device_busy(struct request_queue *q);
528cec47e3dSKiyoshi Ueda 
5291da177e4SLinus Torvalds #endif	/* _LINUX_DEVICE_MAPPER_H */
530