blob: 66ec05a5795558cef24450388e67ffd6bf0743f3 [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);
Kiyoshi Ueda7d763452009-01-06 03:05:07 +000048typedef int (*dm_map_request_fn) (struct dm_target *ti, struct request *clone,
49 union map_info *map_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
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).
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -080057 * 2 : The target wants to push back the io
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
59typedef int (*dm_endio_fn) (struct dm_target *ti,
60 struct bio *bio, int error,
61 union map_info *map_context);
Kiyoshi Ueda7d763452009-01-06 03:05:07 +000062typedef int (*dm_request_endio_fn) (struct dm_target *ti,
63 struct request *clone, int error,
64 union map_info *map_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Bryn Reeves999d8162006-10-03 01:15:43 -070066typedef void (*dm_flush_fn) (struct dm_target *ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067typedef void (*dm_presuspend_fn) (struct dm_target *ti);
68typedef void (*dm_postsuspend_fn) (struct dm_target *ti);
Milan Broz8757b772006-10-03 01:15:36 -070069typedef int (*dm_preresume_fn) (struct dm_target *ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070typedef void (*dm_resume_fn) (struct dm_target *ti);
71
72typedef int (*dm_status_fn) (struct dm_target *ti, status_type_t status_type,
73 char *result, unsigned int maxlen);
74
75typedef int (*dm_message_fn) (struct dm_target *ti, unsigned argc, char **argv);
76
Al Viro647b3d02007-08-28 22:15:59 -040077typedef int (*dm_ioctl_fn) (struct dm_target *ti, unsigned int cmd,
Milan Brozaa129a22006-10-03 01:15:15 -070078 unsigned long arg);
79
Milan Brozf6fccb12008-07-21 12:00:37 +010080typedef int (*dm_merge_fn) (struct dm_target *ti, struct bvec_merge_data *bvm,
81 struct bio_vec *biovec, int max_size);
82
Kiyoshi Ueda7d763452009-01-06 03:05:07 +000083/*
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 */
88typedef int (*dm_busy_fn) (struct dm_target *ti);
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090void dm_error(const char *message);
91
92/*
Bryn Reeves3cb40212006-10-03 01:15:42 -070093 * Combine device limits.
94 */
95void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev);
96
Mikulas Patocka82b15192008-10-10 13:37:09 +010097struct dm_dev {
98 struct block_device *bdev;
Al Viroaeb5d722008-09-02 15:28:45 -040099 fmode_t mode;
Mikulas Patocka82b15192008-10-10 13:37:09 +0100100 char name[16];
101};
102
Bryn Reeves3cb40212006-10-03 01:15:42 -0700103/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * Constructors should call these functions to ensure destination devices
105 * are opened/closed correctly.
106 * FIXME: too many arguments.
107 */
108int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
Al Viroaeb5d722008-09-02 15:28:45 -0400109 sector_t len, fmode_t mode, struct dm_dev **result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110void dm_put_device(struct dm_target *ti, struct dm_dev *d);
111
112/*
113 * Information about a target type
114 */
Andi Kleenab4c142482009-01-06 03:05:09 +0000115
116/*
117 * Target features
118 */
119#define DM_TARGET_SUPPORTS_BARRIERS 0x00000001
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121struct target_type {
Andi Kleenab4c142482009-01-06 03:05:09 +0000122 uint64_t features;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 const char *name;
124 struct module *module;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700125 unsigned version[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 dm_ctr_fn ctr;
127 dm_dtr_fn dtr;
128 dm_map_fn map;
Kiyoshi Ueda7d763452009-01-06 03:05:07 +0000129 dm_map_request_fn map_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 dm_endio_fn end_io;
Kiyoshi Ueda7d763452009-01-06 03:05:07 +0000131 dm_request_endio_fn rq_end_io;
Bryn Reeves999d8162006-10-03 01:15:43 -0700132 dm_flush_fn flush;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 dm_presuspend_fn presuspend;
134 dm_postsuspend_fn postsuspend;
Milan Broz8757b772006-10-03 01:15:36 -0700135 dm_preresume_fn preresume;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 dm_resume_fn resume;
137 dm_status_fn status;
138 dm_message_fn message;
Milan Brozaa129a22006-10-03 01:15:15 -0700139 dm_ioctl_fn ioctl;
Milan Brozf6fccb12008-07-21 12:00:37 +0100140 dm_merge_fn merge;
Kiyoshi Ueda7d763452009-01-06 03:05:07 +0000141 dm_busy_fn busy;
Cheng Renquan45194e42009-04-02 19:55:28 +0100142
143 /* For internal device-mapper use. */
144 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
147struct io_restrictions {
Vasily Averin4f41b092008-02-08 02:10:01 +0000148 unsigned long bounce_pfn;
149 unsigned long seg_boundary_mask;
150 unsigned max_hw_sectors;
151 unsigned max_sectors;
152 unsigned max_segment_size;
153 unsigned short hardsect_size;
154 unsigned short max_hw_segments;
155 unsigned short max_phys_segments;
156 unsigned char no_cluster; /* inverted so that 0 is default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157};
158
159struct dm_target {
160 struct dm_table *table;
161 struct target_type *type;
162
163 /* target limits */
164 sector_t begin;
165 sector_t len;
166
167 /* FIXME: turn this into a mask, and merge with io_restrictions */
168 /* Always a power of 2 */
169 sector_t split_io;
170
171 /*
172 * These are automatically filled in by
173 * dm_table_get_device.
174 */
175 struct io_restrictions limits;
176
177 /* target specific data */
178 void *private;
179
180 /* Used to provide an error string from the ctr */
181 char *error;
182};
183
184int dm_register_target(struct target_type *t);
Mikulas Patocka10d3bd02009-01-06 03:04:58 +0000185void dm_unregister_target(struct target_type *t);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700186
187/*-----------------------------------------------------------------
188 * Functions for creating and manipulating mapped devices.
189 * Drop the reference with dm_put when you finish with the object.
190 *---------------------------------------------------------------*/
191
192/*
193 * DM_ANY_MINOR chooses the next available minor number.
194 */
195#define DM_ANY_MINOR (-1)
196int dm_create(int minor, struct mapped_device **md);
197
198/*
199 * Reference counting for md.
200 */
201struct mapped_device *dm_get_md(dev_t dev);
202void dm_get(struct mapped_device *md);
203void dm_put(struct mapped_device *md);
204
205/*
206 * An arbitrary pointer may be stored alongside a mapped device.
207 */
208void dm_set_mdptr(struct mapped_device *md, void *ptr);
209void *dm_get_mdptr(struct mapped_device *md);
210
211/*
212 * A device can still be used while suspended, but I/O is deferred.
213 */
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800214int dm_suspend(struct mapped_device *md, unsigned suspend_flags);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700215int dm_resume(struct mapped_device *md);
216
217/*
218 * Event functions.
219 */
220uint32_t dm_get_event_nr(struct mapped_device *md);
221int dm_wait_event(struct mapped_device *md, int event_nr);
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100222uint32_t dm_next_uevent_seq(struct mapped_device *md);
223void dm_uevent_add(struct mapped_device *md, struct list_head *elist);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700224
225/*
226 * Info functions.
227 */
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700228const char *dm_device_name(struct mapped_device *md);
Mike Anderson96a1f7d2007-10-19 22:47:59 +0100229int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700230struct gendisk *dm_disk(struct mapped_device *md);
231int dm_suspended(struct mapped_device *md);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800232int dm_noflush_suspending(struct dm_target *ti);
Mikulas Patocka89343da2008-10-10 13:37:10 +0100233union map_info *dm_get_mapinfo(struct bio *bio);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700234
235/*
236 * Geometry functions.
237 */
238int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo);
239int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo);
240
241
242/*-----------------------------------------------------------------
243 * Functions for manipulating device-mapper tables.
244 *---------------------------------------------------------------*/
245
246/*
247 * First create an empty table.
248 */
Al Viroaeb5d722008-09-02 15:28:45 -0400249int dm_table_create(struct dm_table **result, fmode_t mode,
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700250 unsigned num_targets, struct mapped_device *md);
251
252/*
253 * Then call this once for each target.
254 */
255int dm_table_add_target(struct dm_table *t, const char *type,
256 sector_t start, sector_t len, char *params);
257
258/*
259 * Finally call this to make the table ready for use.
260 */
261int dm_table_complete(struct dm_table *t);
262
263/*
Mikulas Patockaea0ec642008-10-10 13:37:11 +0100264 * Unplug all devices in a table.
265 */
266void dm_table_unplug_all(struct dm_table *t);
267
268/*
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700269 * Table reference counting.
270 */
271struct dm_table *dm_get_table(struct mapped_device *md);
272void dm_table_get(struct dm_table *t);
273void dm_table_put(struct dm_table *t);
274
275/*
276 * Queries
277 */
278sector_t dm_table_get_size(struct dm_table *t);
279unsigned int dm_table_get_num_targets(struct dm_table *t);
Al Viroaeb5d722008-09-02 15:28:45 -0400280fmode_t dm_table_get_mode(struct dm_table *t);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700281struct mapped_device *dm_table_get_md(struct dm_table *t);
282
283/*
284 * Trigger an event.
285 */
286void dm_table_event(struct dm_table *t);
287
288/*
289 * The device must be suspended before calling this method.
290 */
291int dm_swap_table(struct mapped_device *md, struct dm_table *t);
292
Mikulas Patocka54160902008-10-10 13:37:12 +0100293/*
294 * A wrapper around vmalloc.
295 */
296void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size);
297
Alasdair G Kergon0da336e2008-04-24 21:43:52 +0100298/*-----------------------------------------------------------------
299 * Macros.
300 *---------------------------------------------------------------*/
301#define DM_NAME "device-mapper"
302
Mikulas Patocka10d3bd02009-01-06 03:04:58 +0000303#define DMCRIT(f, arg...) \
304 printk(KERN_CRIT DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
305
Alasdair G Kergon0da336e2008-04-24 21:43:52 +0100306#define DMERR(f, arg...) \
307 printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
308#define DMERR_LIMIT(f, arg...) \
309 do { \
310 if (printk_ratelimit()) \
311 printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " \
312 f "\n", ## arg); \
313 } while (0)
314
315#define DMWARN(f, arg...) \
316 printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
317#define DMWARN_LIMIT(f, arg...) \
318 do { \
319 if (printk_ratelimit()) \
320 printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " \
321 f "\n", ## arg); \
322 } while (0)
323
324#define DMINFO(f, arg...) \
325 printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
326#define DMINFO_LIMIT(f, arg...) \
327 do { \
328 if (printk_ratelimit()) \
329 printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f \
330 "\n", ## arg); \
331 } while (0)
332
333#ifdef CONFIG_DM_DEBUG
334# define DMDEBUG(f, arg...) \
335 printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX " DEBUG: " f "\n", ## arg)
336# define DMDEBUG_LIMIT(f, arg...) \
337 do { \
338 if (printk_ratelimit()) \
339 printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX ": " f \
340 "\n", ## arg); \
341 } while (0)
342#else
343# define DMDEBUG(f, arg...) do {} while (0)
344# define DMDEBUG_LIMIT(f, arg...) do {} while (0)
345#endif
346
347#define DMEMIT(x...) sz += ((sz >= maxlen) ? \
348 0 : scnprintf(result + sz, maxlen - sz, x))
349
350#define SECTOR_SHIFT 9
351
352/*
353 * Definitions of return values from target end_io function.
354 */
355#define DM_ENDIO_INCOMPLETE 1
356#define DM_ENDIO_REQUEUE 2
357
358/*
359 * Definitions of return values from target map function.
360 */
361#define DM_MAPIO_SUBMITTED 0
362#define DM_MAPIO_REMAPPED 1
363#define DM_MAPIO_REQUEUE DM_ENDIO_REQUEUE
364
365/*
366 * Ceiling(n / sz)
367 */
368#define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz))
369
370#define dm_sector_div_up(n, sz) ( \
371{ \
372 sector_t _r = ((n) + (sz) - 1); \
373 sector_div(_r, (sz)); \
374 _r; \
375} \
376)
377
378/*
379 * ceiling(n / size) * size
380 */
381#define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
382
Mikulas Patockad63a5ce2008-10-21 17:44:57 +0100383#define dm_array_too_big(fixed, obj, num) \
384 ((num) > (UINT_MAX - (fixed)) / (obj))
385
Alasdair G Kergon0da336e2008-04-24 21:43:52 +0100386static inline sector_t to_sector(unsigned long n)
387{
388 return (n >> SECTOR_SHIFT);
389}
390
391static inline unsigned long to_bytes(sector_t n)
392{
393 return (n << SECTOR_SHIFT);
394}
395
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700396#endif /* _LINUX_DEVICE_MAPPER_H */