xref: /linux-6.15/include/linux/device-mapper.h (revision 2d6ffcca)
1 /*
2  * Copyright (C) 2001 Sistina Software (UK) Limited.
3  * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
4  *
5  * This file is released under the LGPL.
6  */
7 
8 #ifndef _LINUX_DEVICE_MAPPER_H
9 #define _LINUX_DEVICE_MAPPER_H
10 
11 #include <linux/bio.h>
12 #include <linux/blkdev.h>
13 
14 struct dm_target;
15 struct dm_table;
16 struct dm_dev;
17 struct mapped_device;
18 struct bio_vec;
19 
20 typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE } status_type_t;
21 
22 union map_info {
23 	void *ptr;
24 	unsigned long long ll;
25 };
26 
27 /*
28  * In the constructor the target parameter will already have the
29  * table, type, begin and len fields filled in.
30  */
31 typedef int (*dm_ctr_fn) (struct dm_target *target,
32 			  unsigned int argc, char **argv);
33 
34 /*
35  * The destructor doesn't need to free the dm_target, just
36  * anything hidden ti->private.
37  */
38 typedef void (*dm_dtr_fn) (struct dm_target *ti);
39 
40 /*
41  * The map function must return:
42  * < 0: error
43  * = 0: The target will handle the io by resubmitting it later
44  * = 1: simple remap complete
45  * = 2: The target wants to push back the io
46  */
47 typedef int (*dm_map_fn) (struct dm_target *ti, struct bio *bio,
48 			  union map_info *map_context);
49 
50 /*
51  * Returns:
52  * < 0 : error (currently ignored)
53  * 0   : ended successfully
54  * 1   : for some reason the io has still not completed (eg,
55  *       multipath target might want to requeue a failed io).
56  * 2   : The target wants to push back the io
57  */
58 typedef int (*dm_endio_fn) (struct dm_target *ti,
59 			    struct bio *bio, int error,
60 			    union map_info *map_context);
61 
62 typedef void (*dm_flush_fn) (struct dm_target *ti);
63 typedef void (*dm_presuspend_fn) (struct dm_target *ti);
64 typedef void (*dm_postsuspend_fn) (struct dm_target *ti);
65 typedef int (*dm_preresume_fn) (struct dm_target *ti);
66 typedef void (*dm_resume_fn) (struct dm_target *ti);
67 
68 typedef int (*dm_status_fn) (struct dm_target *ti, status_type_t status_type,
69 			     char *result, unsigned int maxlen);
70 
71 typedef int (*dm_message_fn) (struct dm_target *ti, unsigned argc, char **argv);
72 
73 typedef int (*dm_ioctl_fn) (struct dm_target *ti, struct inode *inode,
74 			    struct file *filp, unsigned int cmd,
75 			    unsigned long arg);
76 
77 typedef int (*dm_merge_fn) (struct dm_target *ti, struct bvec_merge_data *bvm,
78 			    struct bio_vec *biovec, int max_size);
79 
80 void dm_error(const char *message);
81 
82 /*
83  * Combine device limits.
84  */
85 void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev);
86 
87 /*
88  * Constructors should call these functions to ensure destination devices
89  * are opened/closed correctly.
90  * FIXME: too many arguments.
91  */
92 int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
93 		  sector_t len, int mode, struct dm_dev **result);
94 void dm_put_device(struct dm_target *ti, struct dm_dev *d);
95 
96 /*
97  * Information about a target type
98  */
99 struct target_type {
100 	const char *name;
101 	struct module *module;
102 	unsigned version[3];
103 	dm_ctr_fn ctr;
104 	dm_dtr_fn dtr;
105 	dm_map_fn map;
106 	dm_endio_fn end_io;
107 	dm_flush_fn flush;
108 	dm_presuspend_fn presuspend;
109 	dm_postsuspend_fn postsuspend;
110 	dm_preresume_fn preresume;
111 	dm_resume_fn resume;
112 	dm_status_fn status;
113 	dm_message_fn message;
114 	dm_ioctl_fn ioctl;
115 	dm_merge_fn merge;
116 };
117 
118 struct io_restrictions {
119 	unsigned long bounce_pfn;
120 	unsigned long seg_boundary_mask;
121 	unsigned max_hw_sectors;
122 	unsigned max_sectors;
123 	unsigned max_segment_size;
124 	unsigned short hardsect_size;
125 	unsigned short max_hw_segments;
126 	unsigned short max_phys_segments;
127 	unsigned char no_cluster; /* inverted so that 0 is default */
128 };
129 
130 struct dm_target {
131 	struct dm_table *table;
132 	struct target_type *type;
133 
134 	/* target limits */
135 	sector_t begin;
136 	sector_t len;
137 
138 	/* FIXME: turn this into a mask, and merge with io_restrictions */
139 	/* Always a power of 2 */
140 	sector_t split_io;
141 
142 	/*
143 	 * These are automatically filled in by
144 	 * dm_table_get_device.
145 	 */
146 	struct io_restrictions limits;
147 
148 	/* target specific data */
149 	void *private;
150 
151 	/* Used to provide an error string from the ctr */
152 	char *error;
153 };
154 
155 int dm_register_target(struct target_type *t);
156 int dm_unregister_target(struct target_type *t);
157 
158 
159 /*-----------------------------------------------------------------
160  * Functions for creating and manipulating mapped devices.
161  * Drop the reference with dm_put when you finish with the object.
162  *---------------------------------------------------------------*/
163 
164 /*
165  * DM_ANY_MINOR chooses the next available minor number.
166  */
167 #define DM_ANY_MINOR (-1)
168 int dm_create(int minor, struct mapped_device **md);
169 
170 /*
171  * Reference counting for md.
172  */
173 struct mapped_device *dm_get_md(dev_t dev);
174 void dm_get(struct mapped_device *md);
175 void dm_put(struct mapped_device *md);
176 
177 /*
178  * An arbitrary pointer may be stored alongside a mapped device.
179  */
180 void dm_set_mdptr(struct mapped_device *md, void *ptr);
181 void *dm_get_mdptr(struct mapped_device *md);
182 
183 /*
184  * A device can still be used while suspended, but I/O is deferred.
185  */
186 int dm_suspend(struct mapped_device *md, unsigned suspend_flags);
187 int dm_resume(struct mapped_device *md);
188 
189 /*
190  * Event functions.
191  */
192 uint32_t dm_get_event_nr(struct mapped_device *md);
193 int dm_wait_event(struct mapped_device *md, int event_nr);
194 uint32_t dm_next_uevent_seq(struct mapped_device *md);
195 void dm_uevent_add(struct mapped_device *md, struct list_head *elist);
196 
197 /*
198  * Info functions.
199  */
200 const char *dm_device_name(struct mapped_device *md);
201 int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid);
202 struct gendisk *dm_disk(struct mapped_device *md);
203 int dm_suspended(struct mapped_device *md);
204 int dm_noflush_suspending(struct dm_target *ti);
205 
206 /*
207  * Geometry functions.
208  */
209 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo);
210 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo);
211 
212 
213 /*-----------------------------------------------------------------
214  * Functions for manipulating device-mapper tables.
215  *---------------------------------------------------------------*/
216 
217 /*
218  * First create an empty table.
219  */
220 int dm_table_create(struct dm_table **result, int mode,
221 		    unsigned num_targets, struct mapped_device *md);
222 
223 /*
224  * Then call this once for each target.
225  */
226 int dm_table_add_target(struct dm_table *t, const char *type,
227 			sector_t start, sector_t len, char *params);
228 
229 /*
230  * Finally call this to make the table ready for use.
231  */
232 int dm_table_complete(struct dm_table *t);
233 
234 /*
235  * Table reference counting.
236  */
237 struct dm_table *dm_get_table(struct mapped_device *md);
238 void dm_table_get(struct dm_table *t);
239 void dm_table_put(struct dm_table *t);
240 
241 /*
242  * Queries
243  */
244 sector_t dm_table_get_size(struct dm_table *t);
245 unsigned int dm_table_get_num_targets(struct dm_table *t);
246 int dm_table_get_mode(struct dm_table *t);
247 struct mapped_device *dm_table_get_md(struct dm_table *t);
248 
249 /*
250  * Trigger an event.
251  */
252 void dm_table_event(struct dm_table *t);
253 
254 /*
255  * The device must be suspended before calling this method.
256  */
257 int dm_swap_table(struct mapped_device *md, struct dm_table *t);
258 
259 /*-----------------------------------------------------------------
260  * Macros.
261  *---------------------------------------------------------------*/
262 #define DM_NAME "device-mapper"
263 
264 #define DMERR(f, arg...) \
265 	printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
266 #define DMERR_LIMIT(f, arg...) \
267 	do { \
268 		if (printk_ratelimit())	\
269 			printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " \
270 			       f "\n", ## arg); \
271 	} while (0)
272 
273 #define DMWARN(f, arg...) \
274 	printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
275 #define DMWARN_LIMIT(f, arg...) \
276 	do { \
277 		if (printk_ratelimit())	\
278 			printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " \
279 			       f "\n", ## arg); \
280 	} while (0)
281 
282 #define DMINFO(f, arg...) \
283 	printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
284 #define DMINFO_LIMIT(f, arg...) \
285 	do { \
286 		if (printk_ratelimit())	\
287 			printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f \
288 			       "\n", ## arg); \
289 	} while (0)
290 
291 #ifdef CONFIG_DM_DEBUG
292 #  define DMDEBUG(f, arg...) \
293 	printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX " DEBUG: " f "\n", ## arg)
294 #  define DMDEBUG_LIMIT(f, arg...) \
295 	do { \
296 		if (printk_ratelimit())	\
297 			printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX ": " f \
298 			       "\n", ## arg); \
299 	} while (0)
300 #else
301 #  define DMDEBUG(f, arg...) do {} while (0)
302 #  define DMDEBUG_LIMIT(f, arg...) do {} while (0)
303 #endif
304 
305 #define DMEMIT(x...) sz += ((sz >= maxlen) ? \
306 			  0 : scnprintf(result + sz, maxlen - sz, x))
307 
308 #define SECTOR_SHIFT 9
309 
310 /*
311  * Definitions of return values from target end_io function.
312  */
313 #define DM_ENDIO_INCOMPLETE	1
314 #define DM_ENDIO_REQUEUE	2
315 
316 /*
317  * Definitions of return values from target map function.
318  */
319 #define DM_MAPIO_SUBMITTED	0
320 #define DM_MAPIO_REMAPPED	1
321 #define DM_MAPIO_REQUEUE	DM_ENDIO_REQUEUE
322 
323 /*
324  * Ceiling(n / sz)
325  */
326 #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz))
327 
328 #define dm_sector_div_up(n, sz) ( \
329 { \
330 	sector_t _r = ((n) + (sz) - 1); \
331 	sector_div(_r, (sz)); \
332 	_r; \
333 } \
334 )
335 
336 /*
337  * ceiling(n / size) * size
338  */
339 #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
340 
341 static inline sector_t to_sector(unsigned long n)
342 {
343 	return (n >> SECTOR_SHIFT);
344 }
345 
346 static inline unsigned long to_bytes(sector_t n)
347 {
348 	return (n << SECTOR_SHIFT);
349 }
350 
351 #endif	/* _LINUX_DEVICE_MAPPER_H */
352