blob: 89ff2df402405a280622c792064df3954cbae2a9 [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
Al Viro647b3d02007-08-28 22:15:59 -040072typedef int (*dm_ioctl_fn) (struct dm_target *ti, unsigned int cmd,
Milan Brozaa129a22006-10-03 01:15:15 -070073 unsigned long arg);
74
Milan Brozf6fccb12008-07-21 12:00:37 +010075typedef int (*dm_merge_fn) (struct dm_target *ti, struct bvec_merge_data *bvm,
76 struct bio_vec *biovec, int max_size);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078void dm_error(const char *message);
79
80/*
Bryn Reeves3cb40212006-10-03 01:15:42 -070081 * Combine device limits.
82 */
83void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev);
84
Mikulas Patocka82b15192008-10-10 13:37:09 +010085struct dm_dev {
86 struct block_device *bdev;
Al Viroaeb5d722008-09-02 15:28:45 -040087 fmode_t mode;
Mikulas Patocka82b15192008-10-10 13:37:09 +010088 char name[16];
89};
90
Bryn Reeves3cb40212006-10-03 01:15:42 -070091/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * Constructors should call these functions to ensure destination devices
93 * are opened/closed correctly.
94 * FIXME: too many arguments.
95 */
96int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
Al Viroaeb5d722008-09-02 15:28:45 -040097 sector_t len, fmode_t mode, struct dm_dev **result);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098void dm_put_device(struct dm_target *ti, struct dm_dev *d);
99
100/*
101 * Information about a target type
102 */
103struct target_type {
104 const char *name;
105 struct module *module;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700106 unsigned version[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 dm_ctr_fn ctr;
108 dm_dtr_fn dtr;
109 dm_map_fn map;
110 dm_endio_fn end_io;
Bryn Reeves999d8162006-10-03 01:15:43 -0700111 dm_flush_fn flush;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 dm_presuspend_fn presuspend;
113 dm_postsuspend_fn postsuspend;
Milan Broz8757b772006-10-03 01:15:36 -0700114 dm_preresume_fn preresume;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 dm_resume_fn resume;
116 dm_status_fn status;
117 dm_message_fn message;
Milan Brozaa129a22006-10-03 01:15:15 -0700118 dm_ioctl_fn ioctl;
Milan Brozf6fccb12008-07-21 12:00:37 +0100119 dm_merge_fn merge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
122struct io_restrictions {
Vasily Averin4f41b092008-02-08 02:10:01 +0000123 unsigned long bounce_pfn;
124 unsigned long seg_boundary_mask;
125 unsigned max_hw_sectors;
126 unsigned max_sectors;
127 unsigned max_segment_size;
128 unsigned short hardsect_size;
129 unsigned short max_hw_segments;
130 unsigned short max_phys_segments;
131 unsigned char no_cluster; /* inverted so that 0 is default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132};
133
134struct dm_target {
135 struct dm_table *table;
136 struct target_type *type;
137
138 /* target limits */
139 sector_t begin;
140 sector_t len;
141
142 /* FIXME: turn this into a mask, and merge with io_restrictions */
143 /* Always a power of 2 */
144 sector_t split_io;
145
146 /*
147 * These are automatically filled in by
148 * dm_table_get_device.
149 */
150 struct io_restrictions limits;
151
152 /* target specific data */
153 void *private;
154
155 /* Used to provide an error string from the ctr */
156 char *error;
157};
158
159int dm_register_target(struct target_type *t);
Mikulas Patocka10d3bd02009-01-06 03:04:58 +0000160void dm_unregister_target(struct target_type *t);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700161
162/*-----------------------------------------------------------------
163 * Functions for creating and manipulating mapped devices.
164 * Drop the reference with dm_put when you finish with the object.
165 *---------------------------------------------------------------*/
166
167/*
168 * DM_ANY_MINOR chooses the next available minor number.
169 */
170#define DM_ANY_MINOR (-1)
171int dm_create(int minor, struct mapped_device **md);
172
173/*
174 * Reference counting for md.
175 */
176struct mapped_device *dm_get_md(dev_t dev);
177void dm_get(struct mapped_device *md);
178void dm_put(struct mapped_device *md);
179
180/*
181 * An arbitrary pointer may be stored alongside a mapped device.
182 */
183void dm_set_mdptr(struct mapped_device *md, void *ptr);
184void *dm_get_mdptr(struct mapped_device *md);
185
186/*
187 * A device can still be used while suspended, but I/O is deferred.
188 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800189int dm_suspend(struct mapped_device *md, unsigned suspend_flags);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700190int dm_resume(struct mapped_device *md);
191
192/*
193 * Event functions.
194 */
195uint32_t dm_get_event_nr(struct mapped_device *md);
196int dm_wait_event(struct mapped_device *md, int event_nr);
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100197uint32_t dm_next_uevent_seq(struct mapped_device *md);
198void dm_uevent_add(struct mapped_device *md, struct list_head *elist);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700199
200/*
201 * Info functions.
202 */
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700203const char *dm_device_name(struct mapped_device *md);
Mike Anderson96a1f7d2007-10-19 22:47:59 +0100204int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700205struct gendisk *dm_disk(struct mapped_device *md);
206int dm_suspended(struct mapped_device *md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800207int dm_noflush_suspending(struct dm_target *ti);
Mikulas Patocka89343da2008-10-10 13:37:10 +0100208union map_info *dm_get_mapinfo(struct bio *bio);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700209
210/*
211 * Geometry functions.
212 */
213int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo);
214int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo);
215
216
217/*-----------------------------------------------------------------
218 * Functions for manipulating device-mapper tables.
219 *---------------------------------------------------------------*/
220
221/*
222 * First create an empty table.
223 */
Al Viroaeb5d722008-09-02 15:28:45 -0400224int dm_table_create(struct dm_table **result, fmode_t mode,
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700225 unsigned num_targets, struct mapped_device *md);
226
227/*
228 * Then call this once for each target.
229 */
230int dm_table_add_target(struct dm_table *t, const char *type,
231 sector_t start, sector_t len, char *params);
232
233/*
234 * Finally call this to make the table ready for use.
235 */
236int dm_table_complete(struct dm_table *t);
237
238/*
Mikulas Patockaea0ec642008-10-10 13:37:11 +0100239 * Unplug all devices in a table.
240 */
241void dm_table_unplug_all(struct dm_table *t);
242
243/*
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700244 * Table reference counting.
245 */
246struct dm_table *dm_get_table(struct mapped_device *md);
247void dm_table_get(struct dm_table *t);
248void dm_table_put(struct dm_table *t);
249
250/*
251 * Queries
252 */
253sector_t dm_table_get_size(struct dm_table *t);
254unsigned int dm_table_get_num_targets(struct dm_table *t);
Al Viroaeb5d722008-09-02 15:28:45 -0400255fmode_t dm_table_get_mode(struct dm_table *t);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700256struct mapped_device *dm_table_get_md(struct dm_table *t);
257
258/*
259 * Trigger an event.
260 */
261void dm_table_event(struct dm_table *t);
262
263/*
264 * The device must be suspended before calling this method.
265 */
266int dm_swap_table(struct mapped_device *md, struct dm_table *t);
267
Mikulas Patocka54160902008-10-10 13:37:12 +0100268/*
269 * A wrapper around vmalloc.
270 */
271void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size);
272
Alasdair G Kergon0da336e2008-04-24 21:43:52 +0100273/*-----------------------------------------------------------------
274 * Macros.
275 *---------------------------------------------------------------*/
276#define DM_NAME "device-mapper"
277
Mikulas Patocka10d3bd02009-01-06 03:04:58 +0000278#define DMCRIT(f, arg...) \
279 printk(KERN_CRIT DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
280
Alasdair G Kergon0da336e2008-04-24 21:43:52 +0100281#define DMERR(f, arg...) \
282 printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
283#define DMERR_LIMIT(f, arg...) \
284 do { \
285 if (printk_ratelimit()) \
286 printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " \
287 f "\n", ## arg); \
288 } while (0)
289
290#define DMWARN(f, arg...) \
291 printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
292#define DMWARN_LIMIT(f, arg...) \
293 do { \
294 if (printk_ratelimit()) \
295 printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " \
296 f "\n", ## arg); \
297 } while (0)
298
299#define DMINFO(f, arg...) \
300 printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
301#define DMINFO_LIMIT(f, arg...) \
302 do { \
303 if (printk_ratelimit()) \
304 printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f \
305 "\n", ## arg); \
306 } while (0)
307
308#ifdef CONFIG_DM_DEBUG
309# define DMDEBUG(f, arg...) \
310 printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX " DEBUG: " f "\n", ## arg)
311# define DMDEBUG_LIMIT(f, arg...) \
312 do { \
313 if (printk_ratelimit()) \
314 printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX ": " f \
315 "\n", ## arg); \
316 } while (0)
317#else
318# define DMDEBUG(f, arg...) do {} while (0)
319# define DMDEBUG_LIMIT(f, arg...) do {} while (0)
320#endif
321
322#define DMEMIT(x...) sz += ((sz >= maxlen) ? \
323 0 : scnprintf(result + sz, maxlen - sz, x))
324
325#define SECTOR_SHIFT 9
326
327/*
328 * Definitions of return values from target end_io function.
329 */
330#define DM_ENDIO_INCOMPLETE 1
331#define DM_ENDIO_REQUEUE 2
332
333/*
334 * Definitions of return values from target map function.
335 */
336#define DM_MAPIO_SUBMITTED 0
337#define DM_MAPIO_REMAPPED 1
338#define DM_MAPIO_REQUEUE DM_ENDIO_REQUEUE
339
340/*
341 * Ceiling(n / sz)
342 */
343#define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz))
344
345#define dm_sector_div_up(n, sz) ( \
346{ \
347 sector_t _r = ((n) + (sz) - 1); \
348 sector_div(_r, (sz)); \
349 _r; \
350} \
351)
352
353/*
354 * ceiling(n / size) * size
355 */
356#define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
357
Mikulas Patockad63a5ce2008-10-21 17:44:57 +0100358#define dm_array_too_big(fixed, obj, num) \
359 ((num) > (UINT_MAX - (fixed)) / (obj))
360
Alasdair G Kergon0da336e2008-04-24 21:43:52 +0100361static inline sector_t to_sector(unsigned long n)
362{
363 return (n >> SECTOR_SHIFT);
364}
365
366static inline unsigned long to_bytes(sector_t n)
367{
368 return (n << SECTOR_SHIFT);
369}
370
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700371#endif /* _LINUX_DEVICE_MAPPER_H */