Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2001 Sistina Software (UK) Limited. |
Alasdair G Kergon | 0da336e | 2008-04-24 21:43:52 +0100 | [diff] [blame] | 3 | * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is released under the LGPL. |
| 6 | */ |
| 7 | |
| 8 | #ifndef _LINUX_DEVICE_MAPPER_H |
| 9 | #define _LINUX_DEVICE_MAPPER_H |
| 10 | |
Heinz Mauelshagen | 416cd17 | 2008-04-24 21:43:35 +0100 | [diff] [blame] | 11 | #include <linux/bio.h> |
Milan Broz | f6fccb1 | 2008-07-21 12:00:37 +0100 | [diff] [blame] | 12 | #include <linux/blkdev.h> |
Heinz Mauelshagen | 416cd17 | 2008-04-24 21:43:35 +0100 | [diff] [blame] | 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | struct dm_target; |
| 15 | struct dm_table; |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 16 | struct mapped_device; |
Milan Broz | f6fccb1 | 2008-07-21 12:00:37 +0100 | [diff] [blame] | 17 | struct bio_vec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Kiyoshi Ueda | 45cbcd7 | 2006-12-08 02:41:05 -0800 | [diff] [blame] | 43 | * = 1: simple remap complete |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 44 | * = 2: The target wants to push back the io |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | */ |
| 46 | typedef 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 Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 55 | * 2 : The target wants to push back the io |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | */ |
| 57 | typedef int (*dm_endio_fn) (struct dm_target *ti, |
| 58 | struct bio *bio, int error, |
| 59 | union map_info *map_context); |
| 60 | |
Bryn Reeves | 999d816 | 2006-10-03 01:15:43 -0700 | [diff] [blame] | 61 | typedef void (*dm_flush_fn) (struct dm_target *ti); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | typedef void (*dm_presuspend_fn) (struct dm_target *ti); |
| 63 | typedef void (*dm_postsuspend_fn) (struct dm_target *ti); |
Milan Broz | 8757b77 | 2006-10-03 01:15:36 -0700 | [diff] [blame] | 64 | typedef int (*dm_preresume_fn) (struct dm_target *ti); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | typedef void (*dm_resume_fn) (struct dm_target *ti); |
| 66 | |
| 67 | typedef int (*dm_status_fn) (struct dm_target *ti, status_type_t status_type, |
| 68 | char *result, unsigned int maxlen); |
| 69 | |
| 70 | typedef int (*dm_message_fn) (struct dm_target *ti, unsigned argc, char **argv); |
| 71 | |
Al Viro | 647b3d0 | 2007-08-28 22:15:59 -0400 | [diff] [blame] | 72 | typedef int (*dm_ioctl_fn) (struct dm_target *ti, unsigned int cmd, |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 73 | unsigned long arg); |
| 74 | |
Milan Broz | f6fccb1 | 2008-07-21 12:00:37 +0100 | [diff] [blame] | 75 | typedef int (*dm_merge_fn) (struct dm_target *ti, struct bvec_merge_data *bvm, |
| 76 | struct bio_vec *biovec, int max_size); |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | void dm_error(const char *message); |
| 79 | |
| 80 | /* |
Bryn Reeves | 3cb4021 | 2006-10-03 01:15:42 -0700 | [diff] [blame] | 81 | * Combine device limits. |
| 82 | */ |
| 83 | void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev); |
| 84 | |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 85 | struct dm_dev { |
| 86 | struct block_device *bdev; |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 87 | fmode_t mode; |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 88 | char name[16]; |
| 89 | }; |
| 90 | |
Bryn Reeves | 3cb4021 | 2006-10-03 01:15:42 -0700 | [diff] [blame] | 91 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | * Constructors should call these functions to ensure destination devices |
| 93 | * are opened/closed correctly. |
| 94 | * FIXME: too many arguments. |
| 95 | */ |
| 96 | int dm_get_device(struct dm_target *ti, const char *path, sector_t start, |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 97 | sector_t len, fmode_t mode, struct dm_dev **result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | void dm_put_device(struct dm_target *ti, struct dm_dev *d); |
| 99 | |
| 100 | /* |
| 101 | * Information about a target type |
| 102 | */ |
| 103 | struct target_type { |
| 104 | const char *name; |
| 105 | struct module *module; |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 106 | unsigned version[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | dm_ctr_fn ctr; |
| 108 | dm_dtr_fn dtr; |
| 109 | dm_map_fn map; |
| 110 | dm_endio_fn end_io; |
Bryn Reeves | 999d816 | 2006-10-03 01:15:43 -0700 | [diff] [blame] | 111 | dm_flush_fn flush; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | dm_presuspend_fn presuspend; |
| 113 | dm_postsuspend_fn postsuspend; |
Milan Broz | 8757b77 | 2006-10-03 01:15:36 -0700 | [diff] [blame] | 114 | dm_preresume_fn preresume; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | dm_resume_fn resume; |
| 116 | dm_status_fn status; |
| 117 | dm_message_fn message; |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 118 | dm_ioctl_fn ioctl; |
Milan Broz | f6fccb1 | 2008-07-21 12:00:37 +0100 | [diff] [blame] | 119 | dm_merge_fn merge; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | struct io_restrictions { |
Vasily Averin | 4f41b09 | 2008-02-08 02:10:01 +0000 | [diff] [blame] | 123 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | struct 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 | |
| 159 | int dm_register_target(struct target_type *t); |
| 160 | int dm_unregister_target(struct target_type *t); |
| 161 | |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 162 | |
| 163 | /*----------------------------------------------------------------- |
| 164 | * Functions for creating and manipulating mapped devices. |
| 165 | * Drop the reference with dm_put when you finish with the object. |
| 166 | *---------------------------------------------------------------*/ |
| 167 | |
| 168 | /* |
| 169 | * DM_ANY_MINOR chooses the next available minor number. |
| 170 | */ |
| 171 | #define DM_ANY_MINOR (-1) |
| 172 | int dm_create(int minor, struct mapped_device **md); |
| 173 | |
| 174 | /* |
| 175 | * Reference counting for md. |
| 176 | */ |
| 177 | struct mapped_device *dm_get_md(dev_t dev); |
| 178 | void dm_get(struct mapped_device *md); |
| 179 | void dm_put(struct mapped_device *md); |
| 180 | |
| 181 | /* |
| 182 | * An arbitrary pointer may be stored alongside a mapped device. |
| 183 | */ |
| 184 | void dm_set_mdptr(struct mapped_device *md, void *ptr); |
| 185 | void *dm_get_mdptr(struct mapped_device *md); |
| 186 | |
| 187 | /* |
| 188 | * A device can still be used while suspended, but I/O is deferred. |
| 189 | */ |
Kiyoshi Ueda | a3d77d3 | 2006-12-08 02:41:04 -0800 | [diff] [blame] | 190 | int dm_suspend(struct mapped_device *md, unsigned suspend_flags); |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 191 | int dm_resume(struct mapped_device *md); |
| 192 | |
| 193 | /* |
| 194 | * Event functions. |
| 195 | */ |
| 196 | uint32_t dm_get_event_nr(struct mapped_device *md); |
| 197 | int dm_wait_event(struct mapped_device *md, int event_nr); |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 198 | uint32_t dm_next_uevent_seq(struct mapped_device *md); |
| 199 | void dm_uevent_add(struct mapped_device *md, struct list_head *elist); |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 200 | |
| 201 | /* |
| 202 | * Info functions. |
| 203 | */ |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 204 | const char *dm_device_name(struct mapped_device *md); |
Mike Anderson | 96a1f7d | 2007-10-19 22:47:59 +0100 | [diff] [blame] | 205 | int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid); |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 206 | struct gendisk *dm_disk(struct mapped_device *md); |
| 207 | int dm_suspended(struct mapped_device *md); |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 208 | int dm_noflush_suspending(struct dm_target *ti); |
Mikulas Patocka | 89343da | 2008-10-10 13:37:10 +0100 | [diff] [blame] | 209 | union map_info *dm_get_mapinfo(struct bio *bio); |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 210 | |
| 211 | /* |
| 212 | * Geometry functions. |
| 213 | */ |
| 214 | int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo); |
| 215 | int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo); |
| 216 | |
| 217 | |
| 218 | /*----------------------------------------------------------------- |
| 219 | * Functions for manipulating device-mapper tables. |
| 220 | *---------------------------------------------------------------*/ |
| 221 | |
| 222 | /* |
| 223 | * First create an empty table. |
| 224 | */ |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 225 | int dm_table_create(struct dm_table **result, fmode_t mode, |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 226 | unsigned num_targets, struct mapped_device *md); |
| 227 | |
| 228 | /* |
| 229 | * Then call this once for each target. |
| 230 | */ |
| 231 | int dm_table_add_target(struct dm_table *t, const char *type, |
| 232 | sector_t start, sector_t len, char *params); |
| 233 | |
| 234 | /* |
| 235 | * Finally call this to make the table ready for use. |
| 236 | */ |
| 237 | int dm_table_complete(struct dm_table *t); |
| 238 | |
| 239 | /* |
Mikulas Patocka | ea0ec64 | 2008-10-10 13:37:11 +0100 | [diff] [blame] | 240 | * Unplug all devices in a table. |
| 241 | */ |
| 242 | void dm_table_unplug_all(struct dm_table *t); |
| 243 | |
| 244 | /* |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 245 | * Table reference counting. |
| 246 | */ |
| 247 | struct dm_table *dm_get_table(struct mapped_device *md); |
| 248 | void dm_table_get(struct dm_table *t); |
| 249 | void dm_table_put(struct dm_table *t); |
| 250 | |
| 251 | /* |
| 252 | * Queries |
| 253 | */ |
| 254 | sector_t dm_table_get_size(struct dm_table *t); |
| 255 | unsigned int dm_table_get_num_targets(struct dm_table *t); |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 256 | fmode_t dm_table_get_mode(struct dm_table *t); |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 257 | struct mapped_device *dm_table_get_md(struct dm_table *t); |
| 258 | |
| 259 | /* |
| 260 | * Trigger an event. |
| 261 | */ |
| 262 | void dm_table_event(struct dm_table *t); |
| 263 | |
| 264 | /* |
| 265 | * The device must be suspended before calling this method. |
| 266 | */ |
| 267 | int dm_swap_table(struct mapped_device *md, struct dm_table *t); |
| 268 | |
Mikulas Patocka | 5416090 | 2008-10-10 13:37:12 +0100 | [diff] [blame] | 269 | /* |
| 270 | * A wrapper around vmalloc. |
| 271 | */ |
| 272 | void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size); |
| 273 | |
Alasdair G Kergon | 0da336e | 2008-04-24 21:43:52 +0100 | [diff] [blame] | 274 | /*----------------------------------------------------------------- |
| 275 | * Macros. |
| 276 | *---------------------------------------------------------------*/ |
| 277 | #define DM_NAME "device-mapper" |
| 278 | |
| 279 | #define DMERR(f, arg...) \ |
| 280 | printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg) |
| 281 | #define DMERR_LIMIT(f, arg...) \ |
| 282 | do { \ |
| 283 | if (printk_ratelimit()) \ |
| 284 | printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " \ |
| 285 | f "\n", ## arg); \ |
| 286 | } while (0) |
| 287 | |
| 288 | #define DMWARN(f, arg...) \ |
| 289 | printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg) |
| 290 | #define DMWARN_LIMIT(f, arg...) \ |
| 291 | do { \ |
| 292 | if (printk_ratelimit()) \ |
| 293 | printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " \ |
| 294 | f "\n", ## arg); \ |
| 295 | } while (0) |
| 296 | |
| 297 | #define DMINFO(f, arg...) \ |
| 298 | printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg) |
| 299 | #define DMINFO_LIMIT(f, arg...) \ |
| 300 | do { \ |
| 301 | if (printk_ratelimit()) \ |
| 302 | printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f \ |
| 303 | "\n", ## arg); \ |
| 304 | } while (0) |
| 305 | |
| 306 | #ifdef CONFIG_DM_DEBUG |
| 307 | # define DMDEBUG(f, arg...) \ |
| 308 | printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX " DEBUG: " f "\n", ## arg) |
| 309 | # define DMDEBUG_LIMIT(f, arg...) \ |
| 310 | do { \ |
| 311 | if (printk_ratelimit()) \ |
| 312 | printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX ": " f \ |
| 313 | "\n", ## arg); \ |
| 314 | } while (0) |
| 315 | #else |
| 316 | # define DMDEBUG(f, arg...) do {} while (0) |
| 317 | # define DMDEBUG_LIMIT(f, arg...) do {} while (0) |
| 318 | #endif |
| 319 | |
| 320 | #define DMEMIT(x...) sz += ((sz >= maxlen) ? \ |
| 321 | 0 : scnprintf(result + sz, maxlen - sz, x)) |
| 322 | |
| 323 | #define SECTOR_SHIFT 9 |
| 324 | |
| 325 | /* |
| 326 | * Definitions of return values from target end_io function. |
| 327 | */ |
| 328 | #define DM_ENDIO_INCOMPLETE 1 |
| 329 | #define DM_ENDIO_REQUEUE 2 |
| 330 | |
| 331 | /* |
| 332 | * Definitions of return values from target map function. |
| 333 | */ |
| 334 | #define DM_MAPIO_SUBMITTED 0 |
| 335 | #define DM_MAPIO_REMAPPED 1 |
| 336 | #define DM_MAPIO_REQUEUE DM_ENDIO_REQUEUE |
| 337 | |
| 338 | /* |
| 339 | * Ceiling(n / sz) |
| 340 | */ |
| 341 | #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz)) |
| 342 | |
| 343 | #define dm_sector_div_up(n, sz) ( \ |
| 344 | { \ |
| 345 | sector_t _r = ((n) + (sz) - 1); \ |
| 346 | sector_div(_r, (sz)); \ |
| 347 | _r; \ |
| 348 | } \ |
| 349 | ) |
| 350 | |
| 351 | /* |
| 352 | * ceiling(n / size) * size |
| 353 | */ |
| 354 | #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz)) |
| 355 | |
Mikulas Patocka | d63a5ce | 2008-10-21 17:44:57 +0100 | [diff] [blame] | 356 | #define dm_array_too_big(fixed, obj, num) \ |
| 357 | ((num) > (UINT_MAX - (fixed)) / (obj)) |
| 358 | |
Alasdair G Kergon | 0da336e | 2008-04-24 21:43:52 +0100 | [diff] [blame] | 359 | static inline sector_t to_sector(unsigned long n) |
| 360 | { |
| 361 | return (n >> SECTOR_SHIFT); |
| 362 | } |
| 363 | |
| 364 | static inline unsigned long to_bytes(sector_t n) |
| 365 | { |
| 366 | return (n << SECTOR_SHIFT); |
| 367 | } |
| 368 | |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 369 | #endif /* _LINUX_DEVICE_MAPPER_H */ |