blob: e462c7f3b3917d5aa352c03403a418c56a06c86b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001 Sistina Software (UK) Limited.
Alasdair G Kergon0da336e2008-04-24 21:43:52 +01003 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file is released under the LGPL.
6 */
7
8#ifndef _LINUX_DEVICE_MAPPER_H
9#define _LINUX_DEVICE_MAPPER_H
10
Heinz Mauelshagen416cd172008-04-24 21:43:35 +010011#include <linux/bio.h>
Milan Brozf6fccb12008-07-21 12:00:37 +010012#include <linux/blkdev.h>
Heinz Mauelshagen416cd172008-04-24 21:43:35 +010013
Linus Torvalds1da177e2005-04-16 15:20:36 -070014struct dm_target;
15struct dm_table;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070016struct mapped_device;
Milan Brozf6fccb12008-07-21 12:00:37 +010017struct bio_vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE } status_type_t;
20
21union 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 */
30typedef 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 */
37typedef 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
Kiyoshi Ueda45cbcd72006-12-08 02:41:05 -080043 * = 1: simple remap complete
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -080044 * = 2: The target wants to push back the io
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 */
46typedef int (*dm_map_fn) (struct dm_target *ti, struct bio *bio,
47 union map_info *map_context);
48
49/*
50 * Returns:
51 * < 0 : error (currently ignored)
52 * 0 : ended successfully
53 * 1 : for some reason the io has still not completed (eg,
54 * multipath target might want to requeue a failed io).
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -080055 * 2 : The target wants to push back the io
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 */
57typedef int (*dm_endio_fn) (struct dm_target *ti,
58 struct bio *bio, int error,
59 union map_info *map_context);
60
Bryn Reeves999d8162006-10-03 01:15:43 -070061typedef void (*dm_flush_fn) (struct dm_target *ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062typedef void (*dm_presuspend_fn) (struct dm_target *ti);
63typedef void (*dm_postsuspend_fn) (struct dm_target *ti);
Milan Broz8757b772006-10-03 01:15:36 -070064typedef int (*dm_preresume_fn) (struct dm_target *ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065typedef void (*dm_resume_fn) (struct dm_target *ti);
66
67typedef int (*dm_status_fn) (struct dm_target *ti, status_type_t status_type,
68 char *result, unsigned int maxlen);
69
70typedef int (*dm_message_fn) (struct dm_target *ti, unsigned argc, char **argv);
71
Milan Brozaa129a22006-10-03 01:15:15 -070072typedef int (*dm_ioctl_fn) (struct dm_target *ti, struct inode *inode,
73 struct file *filp, unsigned int cmd,
74 unsigned long arg);
75
Milan Brozf6fccb12008-07-21 12:00:37 +010076typedef int (*dm_merge_fn) (struct dm_target *ti, struct bvec_merge_data *bvm,
77 struct bio_vec *biovec, int max_size);
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079void dm_error(const char *message);
80
81/*
Bryn Reeves3cb40212006-10-03 01:15:42 -070082 * Combine device limits.
83 */
84void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev);
85
Mikulas Patocka82b15192008-10-10 13:37:09 +010086struct dm_dev {
87 struct block_device *bdev;
88 int mode;
89 char name[16];
90};
91
Bryn Reeves3cb40212006-10-03 01:15:42 -070092/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * Constructors should call these functions to ensure destination devices
94 * are opened/closed correctly.
95 * FIXME: too many arguments.
96 */
97int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
98 sector_t len, int mode, struct dm_dev **result);
99void dm_put_device(struct dm_target *ti, struct dm_dev *d);
100
101/*
102 * Information about a target type
103 */
104struct target_type {
105 const char *name;
106 struct module *module;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700107 unsigned version[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 dm_ctr_fn ctr;
109 dm_dtr_fn dtr;
110 dm_map_fn map;
111 dm_endio_fn end_io;
Bryn Reeves999d8162006-10-03 01:15:43 -0700112 dm_flush_fn flush;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 dm_presuspend_fn presuspend;
114 dm_postsuspend_fn postsuspend;
Milan Broz8757b772006-10-03 01:15:36 -0700115 dm_preresume_fn preresume;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 dm_resume_fn resume;
117 dm_status_fn status;
118 dm_message_fn message;
Milan Brozaa129a22006-10-03 01:15:15 -0700119 dm_ioctl_fn ioctl;
Milan Brozf6fccb12008-07-21 12:00:37 +0100120 dm_merge_fn merge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
123struct io_restrictions {
Vasily Averin4f41b092008-02-08 02:10:01 +0000124 unsigned long bounce_pfn;
125 unsigned long seg_boundary_mask;
126 unsigned max_hw_sectors;
127 unsigned max_sectors;
128 unsigned max_segment_size;
129 unsigned short hardsect_size;
130 unsigned short max_hw_segments;
131 unsigned short max_phys_segments;
132 unsigned char no_cluster; /* inverted so that 0 is default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
135struct dm_target {
136 struct dm_table *table;
137 struct target_type *type;
138
139 /* target limits */
140 sector_t begin;
141 sector_t len;
142
143 /* FIXME: turn this into a mask, and merge with io_restrictions */
144 /* Always a power of 2 */
145 sector_t split_io;
146
147 /*
148 * These are automatically filled in by
149 * dm_table_get_device.
150 */
151 struct io_restrictions limits;
152
153 /* target specific data */
154 void *private;
155
156 /* Used to provide an error string from the ctr */
157 char *error;
158};
159
160int dm_register_target(struct target_type *t);
161int dm_unregister_target(struct target_type *t);
162
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700163
164/*-----------------------------------------------------------------
165 * Functions for creating and manipulating mapped devices.
166 * Drop the reference with dm_put when you finish with the object.
167 *---------------------------------------------------------------*/
168
169/*
170 * DM_ANY_MINOR chooses the next available minor number.
171 */
172#define DM_ANY_MINOR (-1)
173int dm_create(int minor, struct mapped_device **md);
174
175/*
176 * Reference counting for md.
177 */
178struct mapped_device *dm_get_md(dev_t dev);
179void dm_get(struct mapped_device *md);
180void dm_put(struct mapped_device *md);
181
182/*
183 * An arbitrary pointer may be stored alongside a mapped device.
184 */
185void dm_set_mdptr(struct mapped_device *md, void *ptr);
186void *dm_get_mdptr(struct mapped_device *md);
187
188/*
189 * A device can still be used while suspended, but I/O is deferred.
190 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800191int dm_suspend(struct mapped_device *md, unsigned suspend_flags);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700192int dm_resume(struct mapped_device *md);
193
194/*
195 * Event functions.
196 */
197uint32_t dm_get_event_nr(struct mapped_device *md);
198int dm_wait_event(struct mapped_device *md, int event_nr);
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100199uint32_t dm_next_uevent_seq(struct mapped_device *md);
200void dm_uevent_add(struct mapped_device *md, struct list_head *elist);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700201
202/*
203 * Info functions.
204 */
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700205const char *dm_device_name(struct mapped_device *md);
Mike Anderson96a1f7d2007-10-19 22:47:59 +0100206int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700207struct gendisk *dm_disk(struct mapped_device *md);
208int dm_suspended(struct mapped_device *md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800209int dm_noflush_suspending(struct dm_target *ti);
Mikulas Patocka89343da2008-10-10 13:37:10 +0100210union map_info *dm_get_mapinfo(struct bio *bio);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700211
212/*
213 * Geometry functions.
214 */
215int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo);
216int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo);
217
218
219/*-----------------------------------------------------------------
220 * Functions for manipulating device-mapper tables.
221 *---------------------------------------------------------------*/
222
223/*
224 * First create an empty table.
225 */
226int dm_table_create(struct dm_table **result, int mode,
227 unsigned num_targets, struct mapped_device *md);
228
229/*
230 * Then call this once for each target.
231 */
232int dm_table_add_target(struct dm_table *t, const char *type,
233 sector_t start, sector_t len, char *params);
234
235/*
236 * Finally call this to make the table ready for use.
237 */
238int dm_table_complete(struct dm_table *t);
239
240/*
Mikulas Patockaea0ec642008-10-10 13:37:11 +0100241 * Unplug all devices in a table.
242 */
243void dm_table_unplug_all(struct dm_table *t);
244
245/*
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700246 * Table reference counting.
247 */
248struct dm_table *dm_get_table(struct mapped_device *md);
249void dm_table_get(struct dm_table *t);
250void dm_table_put(struct dm_table *t);
251
252/*
253 * Queries
254 */
255sector_t dm_table_get_size(struct dm_table *t);
256unsigned int dm_table_get_num_targets(struct dm_table *t);
257int dm_table_get_mode(struct dm_table *t);
258struct mapped_device *dm_table_get_md(struct dm_table *t);
259
260/*
261 * Trigger an event.
262 */
263void dm_table_event(struct dm_table *t);
264
265/*
266 * The device must be suspended before calling this method.
267 */
268int dm_swap_table(struct mapped_device *md, struct dm_table *t);
269
Alasdair G Kergon0da336e2008-04-24 21:43:52 +0100270/*-----------------------------------------------------------------
271 * Macros.
272 *---------------------------------------------------------------*/
273#define DM_NAME "device-mapper"
274
275#define DMERR(f, arg...) \
276 printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
277#define DMERR_LIMIT(f, arg...) \
278 do { \
279 if (printk_ratelimit()) \
280 printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " \
281 f "\n", ## arg); \
282 } while (0)
283
284#define DMWARN(f, arg...) \
285 printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
286#define DMWARN_LIMIT(f, arg...) \
287 do { \
288 if (printk_ratelimit()) \
289 printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " \
290 f "\n", ## arg); \
291 } while (0)
292
293#define DMINFO(f, arg...) \
294 printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
295#define DMINFO_LIMIT(f, arg...) \
296 do { \
297 if (printk_ratelimit()) \
298 printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f \
299 "\n", ## arg); \
300 } while (0)
301
302#ifdef CONFIG_DM_DEBUG
303# define DMDEBUG(f, arg...) \
304 printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX " DEBUG: " f "\n", ## arg)
305# define DMDEBUG_LIMIT(f, arg...) \
306 do { \
307 if (printk_ratelimit()) \
308 printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX ": " f \
309 "\n", ## arg); \
310 } while (0)
311#else
312# define DMDEBUG(f, arg...) do {} while (0)
313# define DMDEBUG_LIMIT(f, arg...) do {} while (0)
314#endif
315
316#define DMEMIT(x...) sz += ((sz >= maxlen) ? \
317 0 : scnprintf(result + sz, maxlen - sz, x))
318
319#define SECTOR_SHIFT 9
320
321/*
322 * Definitions of return values from target end_io function.
323 */
324#define DM_ENDIO_INCOMPLETE 1
325#define DM_ENDIO_REQUEUE 2
326
327/*
328 * Definitions of return values from target map function.
329 */
330#define DM_MAPIO_SUBMITTED 0
331#define DM_MAPIO_REMAPPED 1
332#define DM_MAPIO_REQUEUE DM_ENDIO_REQUEUE
333
334/*
335 * Ceiling(n / sz)
336 */
337#define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz))
338
339#define dm_sector_div_up(n, sz) ( \
340{ \
341 sector_t _r = ((n) + (sz) - 1); \
342 sector_div(_r, (sz)); \
343 _r; \
344} \
345)
346
347/*
348 * ceiling(n / size) * size
349 */
350#define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
351
352static inline sector_t to_sector(unsigned long n)
353{
354 return (n >> SECTOR_SHIFT);
355}
356
357static inline unsigned long to_bytes(sector_t n)
358{
359 return (n << SECTOR_SHIFT);
360}
361
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700362#endif /* _LINUX_DEVICE_MAPPER_H */