Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (C) 2001 Sistina Software (UK) Limited. | |
0da336e5 | 3 | * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved. |
1da177e4 LT |
4 | * |
5 | * This file is released under the LGPL. | |
6 | */ | |
7 | ||
8 | #ifndef _LINUX_DEVICE_MAPPER_H | |
9 | #define _LINUX_DEVICE_MAPPER_H | |
10 | ||
416cd17b | 11 | #include <linux/bio.h> |
f6fccb12 | 12 | #include <linux/blkdev.h> |
416cd17b | 13 | |
1da177e4 LT |
14 | struct dm_target; |
15 | struct dm_table; | |
17b2f66f | 16 | struct mapped_device; |
f6fccb12 | 17 | struct bio_vec; |
1da177e4 LT |
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 | |
45cbcd79 | 43 | * = 1: simple remap complete |
2e93ccc1 | 44 | * = 2: The target wants to push back the io |
1da177e4 LT |
45 | */ |
46 | typedef int (*dm_map_fn) (struct dm_target *ti, struct bio *bio, | |
47 | union map_info *map_context); | |
7d76345d KU |
48 | typedef int (*dm_map_request_fn) (struct dm_target *ti, struct request *clone, |
49 | union map_info *map_context); | |
1da177e4 LT |
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). | |
2e93ccc1 | 57 | * 2 : The target wants to push back the io |
1da177e4 LT |
58 | */ |
59 | typedef int (*dm_endio_fn) (struct dm_target *ti, | |
60 | struct bio *bio, int error, | |
61 | union map_info *map_context); | |
7d76345d KU |
62 | typedef int (*dm_request_endio_fn) (struct dm_target *ti, |
63 | struct request *clone, int error, | |
64 | union map_info *map_context); | |
1da177e4 | 65 | |
999d8168 | 66 | typedef void (*dm_flush_fn) (struct dm_target *ti); |
1da177e4 LT |
67 | typedef void (*dm_presuspend_fn) (struct dm_target *ti); |
68 | typedef void (*dm_postsuspend_fn) (struct dm_target *ti); | |
8757b776 | 69 | typedef int (*dm_preresume_fn) (struct dm_target *ti); |
1da177e4 LT |
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 | ||
647b3d00 | 77 | typedef int (*dm_ioctl_fn) (struct dm_target *ti, unsigned int cmd, |
aa129a22 MB |
78 | unsigned long arg); |
79 | ||
f6fccb12 MB |
80 | typedef int (*dm_merge_fn) (struct dm_target *ti, struct bvec_merge_data *bvm, |
81 | struct bio_vec *biovec, int max_size); | |
82 | ||
7d76345d KU |
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 | ||
1da177e4 LT |
90 | void dm_error(const char *message); |
91 | ||
3cb40214 BR |
92 | /* |
93 | * Combine device limits. | |
94 | */ | |
95 | void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev); | |
96 | ||
82b1519b MP |
97 | struct dm_dev { |
98 | struct block_device *bdev; | |
aeb5d727 | 99 | fmode_t mode; |
82b1519b MP |
100 | char name[16]; |
101 | }; | |
102 | ||
1da177e4 LT |
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, | |
aeb5d727 | 109 | sector_t len, fmode_t mode, struct dm_dev **result); |
1da177e4 LT |
110 | void dm_put_device(struct dm_target *ti, struct dm_dev *d); |
111 | ||
112 | /* | |
113 | * Information about a target type | |
114 | */ | |
ab4c1424 AK |
115 | |
116 | /* | |
117 | * Target features | |
118 | */ | |
ab4c1424 | 119 | |
1da177e4 | 120 | struct target_type { |
ab4c1424 | 121 | uint64_t features; |
1da177e4 LT |
122 | const char *name; |
123 | struct module *module; | |
17b2f66f | 124 | unsigned version[3]; |
1da177e4 LT |
125 | dm_ctr_fn ctr; |
126 | dm_dtr_fn dtr; | |
127 | dm_map_fn map; | |
7d76345d | 128 | dm_map_request_fn map_rq; |
1da177e4 | 129 | dm_endio_fn end_io; |
7d76345d | 130 | dm_request_endio_fn rq_end_io; |
999d8168 | 131 | dm_flush_fn flush; |
1da177e4 LT |
132 | dm_presuspend_fn presuspend; |
133 | dm_postsuspend_fn postsuspend; | |
8757b776 | 134 | dm_preresume_fn preresume; |
1da177e4 LT |
135 | dm_resume_fn resume; |
136 | dm_status_fn status; | |
137 | dm_message_fn message; | |
aa129a22 | 138 | dm_ioctl_fn ioctl; |
f6fccb12 | 139 | dm_merge_fn merge; |
7d76345d | 140 | dm_busy_fn busy; |
45194e4f CR |
141 | |
142 | /* For internal device-mapper use. */ | |
143 | struct list_head list; | |
1da177e4 LT |
144 | }; |
145 | ||
146 | struct io_restrictions { | |
4f41b09f VA |
147 | unsigned long bounce_pfn; |
148 | unsigned long seg_boundary_mask; | |
149 | unsigned max_hw_sectors; | |
150 | unsigned max_sectors; | |
151 | unsigned max_segment_size; | |
e1defc4f | 152 | unsigned short logical_block_size; |
4f41b09f VA |
153 | unsigned short max_hw_segments; |
154 | unsigned short max_phys_segments; | |
155 | unsigned char no_cluster; /* inverted so that 0 is default */ | |
1da177e4 LT |
156 | }; |
157 | ||
158 | struct dm_target { | |
159 | struct dm_table *table; | |
160 | struct target_type *type; | |
161 | ||
162 | /* target limits */ | |
163 | sector_t begin; | |
164 | sector_t len; | |
165 | ||
166 | /* FIXME: turn this into a mask, and merge with io_restrictions */ | |
167 | /* Always a power of 2 */ | |
168 | sector_t split_io; | |
169 | ||
170 | /* | |
171 | * These are automatically filled in by | |
172 | * dm_table_get_device. | |
173 | */ | |
174 | struct io_restrictions limits; | |
175 | ||
176 | /* target specific data */ | |
177 | void *private; | |
178 | ||
179 | /* Used to provide an error string from the ctr */ | |
180 | char *error; | |
181 | }; | |
182 | ||
183 | int dm_register_target(struct target_type *t); | |
10d3bd09 | 184 | void dm_unregister_target(struct target_type *t); |
17b2f66f AK |
185 | |
186 | /*----------------------------------------------------------------- | |
187 | * Functions for creating and manipulating mapped devices. | |
188 | * Drop the reference with dm_put when you finish with the object. | |
189 | *---------------------------------------------------------------*/ | |
190 | ||
191 | /* | |
192 | * DM_ANY_MINOR chooses the next available minor number. | |
193 | */ | |
194 | #define DM_ANY_MINOR (-1) | |
195 | int dm_create(int minor, struct mapped_device **md); | |
196 | ||
197 | /* | |
198 | * Reference counting for md. | |
199 | */ | |
200 | struct mapped_device *dm_get_md(dev_t dev); | |
201 | void dm_get(struct mapped_device *md); | |
202 | void dm_put(struct mapped_device *md); | |
203 | ||
204 | /* | |
205 | * An arbitrary pointer may be stored alongside a mapped device. | |
206 | */ | |
207 | void dm_set_mdptr(struct mapped_device *md, void *ptr); | |
208 | void *dm_get_mdptr(struct mapped_device *md); | |
209 | ||
210 | /* | |
211 | * A device can still be used while suspended, but I/O is deferred. | |
212 | */ | |
a3d77d35 | 213 | int dm_suspend(struct mapped_device *md, unsigned suspend_flags); |
17b2f66f AK |
214 | int dm_resume(struct mapped_device *md); |
215 | ||
216 | /* | |
217 | * Event functions. | |
218 | */ | |
219 | uint32_t dm_get_event_nr(struct mapped_device *md); | |
220 | int dm_wait_event(struct mapped_device *md, int event_nr); | |
7a8c3d3b MA |
221 | uint32_t dm_next_uevent_seq(struct mapped_device *md); |
222 | void dm_uevent_add(struct mapped_device *md, struct list_head *elist); | |
17b2f66f AK |
223 | |
224 | /* | |
225 | * Info functions. | |
226 | */ | |
72d94861 | 227 | const char *dm_device_name(struct mapped_device *md); |
96a1f7db | 228 | int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid); |
17b2f66f AK |
229 | struct gendisk *dm_disk(struct mapped_device *md); |
230 | int dm_suspended(struct mapped_device *md); | |
2e93ccc1 | 231 | int dm_noflush_suspending(struct dm_target *ti); |
89343da0 | 232 | union map_info *dm_get_mapinfo(struct bio *bio); |
17b2f66f AK |
233 | |
234 | /* | |
235 | * Geometry functions. | |
236 | */ | |
237 | int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo); | |
238 | int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo); | |
239 | ||
240 | ||
241 | /*----------------------------------------------------------------- | |
242 | * Functions for manipulating device-mapper tables. | |
243 | *---------------------------------------------------------------*/ | |
244 | ||
245 | /* | |
246 | * First create an empty table. | |
247 | */ | |
aeb5d727 | 248 | int dm_table_create(struct dm_table **result, fmode_t mode, |
17b2f66f AK |
249 | unsigned num_targets, struct mapped_device *md); |
250 | ||
251 | /* | |
252 | * Then call this once for each target. | |
253 | */ | |
254 | int dm_table_add_target(struct dm_table *t, const char *type, | |
255 | sector_t start, sector_t len, char *params); | |
256 | ||
257 | /* | |
258 | * Finally call this to make the table ready for use. | |
259 | */ | |
260 | int dm_table_complete(struct dm_table *t); | |
261 | ||
ea0ec640 MP |
262 | /* |
263 | * Unplug all devices in a table. | |
264 | */ | |
265 | void dm_table_unplug_all(struct dm_table *t); | |
266 | ||
17b2f66f AK |
267 | /* |
268 | * Table reference counting. | |
269 | */ | |
270 | struct dm_table *dm_get_table(struct mapped_device *md); | |
271 | void dm_table_get(struct dm_table *t); | |
272 | void dm_table_put(struct dm_table *t); | |
273 | ||
274 | /* | |
275 | * Queries | |
276 | */ | |
277 | sector_t dm_table_get_size(struct dm_table *t); | |
278 | unsigned int dm_table_get_num_targets(struct dm_table *t); | |
aeb5d727 | 279 | fmode_t dm_table_get_mode(struct dm_table *t); |
17b2f66f AK |
280 | struct mapped_device *dm_table_get_md(struct dm_table *t); |
281 | ||
282 | /* | |
283 | * Trigger an event. | |
284 | */ | |
285 | void dm_table_event(struct dm_table *t); | |
286 | ||
287 | /* | |
288 | * The device must be suspended before calling this method. | |
289 | */ | |
290 | int dm_swap_table(struct mapped_device *md, struct dm_table *t); | |
291 | ||
54160904 MP |
292 | /* |
293 | * A wrapper around vmalloc. | |
294 | */ | |
295 | void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size); | |
296 | ||
0da336e5 AK |
297 | /*----------------------------------------------------------------- |
298 | * Macros. | |
299 | *---------------------------------------------------------------*/ | |
300 | #define DM_NAME "device-mapper" | |
301 | ||
10d3bd09 MP |
302 | #define DMCRIT(f, arg...) \ |
303 | printk(KERN_CRIT DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg) | |
304 | ||
0da336e5 AK |
305 | #define DMERR(f, arg...) \ |
306 | printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg) | |
307 | #define DMERR_LIMIT(f, arg...) \ | |
308 | do { \ | |
309 | if (printk_ratelimit()) \ | |
310 | printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " \ | |
311 | f "\n", ## arg); \ | |
312 | } while (0) | |
313 | ||
314 | #define DMWARN(f, arg...) \ | |
315 | printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg) | |
316 | #define DMWARN_LIMIT(f, arg...) \ | |
317 | do { \ | |
318 | if (printk_ratelimit()) \ | |
319 | printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " \ | |
320 | f "\n", ## arg); \ | |
321 | } while (0) | |
322 | ||
323 | #define DMINFO(f, arg...) \ | |
324 | printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg) | |
325 | #define DMINFO_LIMIT(f, arg...) \ | |
326 | do { \ | |
327 | if (printk_ratelimit()) \ | |
328 | printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f \ | |
329 | "\n", ## arg); \ | |
330 | } while (0) | |
331 | ||
332 | #ifdef CONFIG_DM_DEBUG | |
333 | # define DMDEBUG(f, arg...) \ | |
334 | printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX " DEBUG: " f "\n", ## arg) | |
335 | # define DMDEBUG_LIMIT(f, arg...) \ | |
336 | do { \ | |
337 | if (printk_ratelimit()) \ | |
338 | printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX ": " f \ | |
339 | "\n", ## arg); \ | |
340 | } while (0) | |
341 | #else | |
342 | # define DMDEBUG(f, arg...) do {} while (0) | |
343 | # define DMDEBUG_LIMIT(f, arg...) do {} while (0) | |
344 | #endif | |
345 | ||
346 | #define DMEMIT(x...) sz += ((sz >= maxlen) ? \ | |
347 | 0 : scnprintf(result + sz, maxlen - sz, x)) | |
348 | ||
349 | #define SECTOR_SHIFT 9 | |
350 | ||
351 | /* | |
352 | * Definitions of return values from target end_io function. | |
353 | */ | |
354 | #define DM_ENDIO_INCOMPLETE 1 | |
355 | #define DM_ENDIO_REQUEUE 2 | |
356 | ||
357 | /* | |
358 | * Definitions of return values from target map function. | |
359 | */ | |
360 | #define DM_MAPIO_SUBMITTED 0 | |
361 | #define DM_MAPIO_REMAPPED 1 | |
362 | #define DM_MAPIO_REQUEUE DM_ENDIO_REQUEUE | |
363 | ||
364 | /* | |
365 | * Ceiling(n / sz) | |
366 | */ | |
367 | #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz)) | |
368 | ||
369 | #define dm_sector_div_up(n, sz) ( \ | |
370 | { \ | |
371 | sector_t _r = ((n) + (sz) - 1); \ | |
372 | sector_div(_r, (sz)); \ | |
373 | _r; \ | |
374 | } \ | |
375 | ) | |
376 | ||
377 | /* | |
378 | * ceiling(n / size) * size | |
379 | */ | |
380 | #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz)) | |
381 | ||
d63a5ce3 MP |
382 | #define dm_array_too_big(fixed, obj, num) \ |
383 | ((num) > (UINT_MAX - (fixed)) / (obj)) | |
384 | ||
0da336e5 AK |
385 | static inline sector_t to_sector(unsigned long n) |
386 | { | |
387 | return (n >> SECTOR_SHIFT); | |
388 | } | |
389 | ||
390 | static inline unsigned long to_bytes(sector_t n) | |
391 | { | |
392 | return (n << SECTOR_SHIFT); | |
393 | } | |
394 | ||
17b2f66f | 395 | #endif /* _LINUX_DEVICE_MAPPER_H */ |