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