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> |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | struct dm_target; |
| 14 | struct dm_table; |
| 15 | struct dm_dev; |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 16 | struct mapped_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE } status_type_t; |
| 19 | |
| 20 | union map_info { |
| 21 | void *ptr; |
| 22 | unsigned long long ll; |
| 23 | }; |
| 24 | |
| 25 | /* |
| 26 | * In the constructor the target parameter will already have the |
| 27 | * table, type, begin and len fields filled in. |
| 28 | */ |
| 29 | typedef int (*dm_ctr_fn) (struct dm_target *target, |
| 30 | unsigned int argc, char **argv); |
| 31 | |
| 32 | /* |
| 33 | * The destructor doesn't need to free the dm_target, just |
| 34 | * anything hidden ti->private. |
| 35 | */ |
| 36 | typedef void (*dm_dtr_fn) (struct dm_target *ti); |
| 37 | |
| 38 | /* |
| 39 | * The map function must return: |
| 40 | * < 0: error |
| 41 | * = 0: The target will handle the io by resubmitting it later |
Kiyoshi Ueda | 45cbcd7 | 2006-12-08 02:41:05 -0800 | [diff] [blame] | 42 | * = 1: simple remap complete |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 43 | * = 2: The target wants to push back the io |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | */ |
| 45 | typedef int (*dm_map_fn) (struct dm_target *ti, struct bio *bio, |
| 46 | union map_info *map_context); |
| 47 | |
| 48 | /* |
| 49 | * Returns: |
| 50 | * < 0 : error (currently ignored) |
| 51 | * 0 : ended successfully |
| 52 | * 1 : for some reason the io has still not completed (eg, |
| 53 | * multipath target might want to requeue a failed io). |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 54 | * 2 : The target wants to push back the io |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | */ |
| 56 | typedef int (*dm_endio_fn) (struct dm_target *ti, |
| 57 | struct bio *bio, int error, |
| 58 | union map_info *map_context); |
| 59 | |
Bryn Reeves | 999d816 | 2006-10-03 01:15:43 -0700 | [diff] [blame] | 60 | typedef void (*dm_flush_fn) (struct dm_target *ti); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | typedef void (*dm_presuspend_fn) (struct dm_target *ti); |
| 62 | typedef void (*dm_postsuspend_fn) (struct dm_target *ti); |
Milan Broz | 8757b77 | 2006-10-03 01:15:36 -0700 | [diff] [blame] | 63 | typedef int (*dm_preresume_fn) (struct dm_target *ti); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | typedef void (*dm_resume_fn) (struct dm_target *ti); |
| 65 | |
| 66 | typedef int (*dm_status_fn) (struct dm_target *ti, status_type_t status_type, |
| 67 | char *result, unsigned int maxlen); |
| 68 | |
| 69 | typedef int (*dm_message_fn) (struct dm_target *ti, unsigned argc, char **argv); |
| 70 | |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 71 | typedef int (*dm_ioctl_fn) (struct dm_target *ti, struct inode *inode, |
| 72 | struct file *filp, unsigned int cmd, |
| 73 | unsigned long arg); |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | void dm_error(const char *message); |
| 76 | |
| 77 | /* |
Bryn Reeves | 3cb4021 | 2006-10-03 01:15:42 -0700 | [diff] [blame] | 78 | * Combine device limits. |
| 79 | */ |
| 80 | void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev); |
| 81 | |
| 82 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | * Constructors should call these functions to ensure destination devices |
| 84 | * are opened/closed correctly. |
| 85 | * FIXME: too many arguments. |
| 86 | */ |
| 87 | int dm_get_device(struct dm_target *ti, const char *path, sector_t start, |
| 88 | sector_t len, int mode, struct dm_dev **result); |
| 89 | void dm_put_device(struct dm_target *ti, struct dm_dev *d); |
| 90 | |
| 91 | /* |
| 92 | * Information about a target type |
| 93 | */ |
| 94 | struct target_type { |
| 95 | const char *name; |
| 96 | struct module *module; |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 97 | unsigned version[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | dm_ctr_fn ctr; |
| 99 | dm_dtr_fn dtr; |
| 100 | dm_map_fn map; |
| 101 | dm_endio_fn end_io; |
Bryn Reeves | 999d816 | 2006-10-03 01:15:43 -0700 | [diff] [blame] | 102 | dm_flush_fn flush; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | dm_presuspend_fn presuspend; |
| 104 | dm_postsuspend_fn postsuspend; |
Milan Broz | 8757b77 | 2006-10-03 01:15:36 -0700 | [diff] [blame] | 105 | dm_preresume_fn preresume; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | dm_resume_fn resume; |
| 107 | dm_status_fn status; |
| 108 | dm_message_fn message; |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 109 | dm_ioctl_fn ioctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | struct io_restrictions { |
Vasily Averin | 4f41b09 | 2008-02-08 02:10:01 +0000 | [diff] [blame] | 113 | unsigned long bounce_pfn; |
| 114 | unsigned long seg_boundary_mask; |
| 115 | unsigned max_hw_sectors; |
| 116 | unsigned max_sectors; |
| 117 | unsigned max_segment_size; |
| 118 | unsigned short hardsect_size; |
| 119 | unsigned short max_hw_segments; |
| 120 | unsigned short max_phys_segments; |
| 121 | unsigned char no_cluster; /* inverted so that 0 is default */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | struct dm_target { |
| 125 | struct dm_table *table; |
| 126 | struct target_type *type; |
| 127 | |
| 128 | /* target limits */ |
| 129 | sector_t begin; |
| 130 | sector_t len; |
| 131 | |
| 132 | /* FIXME: turn this into a mask, and merge with io_restrictions */ |
| 133 | /* Always a power of 2 */ |
| 134 | sector_t split_io; |
| 135 | |
| 136 | /* |
| 137 | * These are automatically filled in by |
| 138 | * dm_table_get_device. |
| 139 | */ |
| 140 | struct io_restrictions limits; |
| 141 | |
| 142 | /* target specific data */ |
| 143 | void *private; |
| 144 | |
| 145 | /* Used to provide an error string from the ctr */ |
| 146 | char *error; |
| 147 | }; |
| 148 | |
| 149 | int dm_register_target(struct target_type *t); |
| 150 | int dm_unregister_target(struct target_type *t); |
| 151 | |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 152 | |
| 153 | /*----------------------------------------------------------------- |
| 154 | * Functions for creating and manipulating mapped devices. |
| 155 | * Drop the reference with dm_put when you finish with the object. |
| 156 | *---------------------------------------------------------------*/ |
| 157 | |
| 158 | /* |
| 159 | * DM_ANY_MINOR chooses the next available minor number. |
| 160 | */ |
| 161 | #define DM_ANY_MINOR (-1) |
| 162 | int dm_create(int minor, struct mapped_device **md); |
| 163 | |
| 164 | /* |
| 165 | * Reference counting for md. |
| 166 | */ |
| 167 | struct mapped_device *dm_get_md(dev_t dev); |
| 168 | void dm_get(struct mapped_device *md); |
| 169 | void dm_put(struct mapped_device *md); |
| 170 | |
| 171 | /* |
| 172 | * An arbitrary pointer may be stored alongside a mapped device. |
| 173 | */ |
| 174 | void dm_set_mdptr(struct mapped_device *md, void *ptr); |
| 175 | void *dm_get_mdptr(struct mapped_device *md); |
| 176 | |
| 177 | /* |
| 178 | * A device can still be used while suspended, but I/O is deferred. |
| 179 | */ |
Kiyoshi Ueda | a3d77d3 | 2006-12-08 02:41:04 -0800 | [diff] [blame] | 180 | int dm_suspend(struct mapped_device *md, unsigned suspend_flags); |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 181 | int dm_resume(struct mapped_device *md); |
| 182 | |
| 183 | /* |
| 184 | * Event functions. |
| 185 | */ |
| 186 | uint32_t dm_get_event_nr(struct mapped_device *md); |
| 187 | int dm_wait_event(struct mapped_device *md, int event_nr); |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 188 | uint32_t dm_next_uevent_seq(struct mapped_device *md); |
| 189 | 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] | 190 | |
| 191 | /* |
| 192 | * Info functions. |
| 193 | */ |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 194 | const char *dm_device_name(struct mapped_device *md); |
Mike Anderson | 96a1f7d | 2007-10-19 22:47:59 +0100 | [diff] [blame] | 195 | 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] | 196 | struct gendisk *dm_disk(struct mapped_device *md); |
| 197 | int dm_suspended(struct mapped_device *md); |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 198 | int dm_noflush_suspending(struct dm_target *ti); |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * Geometry functions. |
| 202 | */ |
| 203 | int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo); |
| 204 | int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo); |
| 205 | |
| 206 | |
| 207 | /*----------------------------------------------------------------- |
| 208 | * Functions for manipulating device-mapper tables. |
| 209 | *---------------------------------------------------------------*/ |
| 210 | |
| 211 | /* |
| 212 | * First create an empty table. |
| 213 | */ |
| 214 | int dm_table_create(struct dm_table **result, int mode, |
| 215 | unsigned num_targets, struct mapped_device *md); |
| 216 | |
| 217 | /* |
| 218 | * Then call this once for each target. |
| 219 | */ |
| 220 | int dm_table_add_target(struct dm_table *t, const char *type, |
| 221 | sector_t start, sector_t len, char *params); |
| 222 | |
| 223 | /* |
| 224 | * Finally call this to make the table ready for use. |
| 225 | */ |
| 226 | int dm_table_complete(struct dm_table *t); |
| 227 | |
| 228 | /* |
| 229 | * Table reference counting. |
| 230 | */ |
| 231 | struct dm_table *dm_get_table(struct mapped_device *md); |
| 232 | void dm_table_get(struct dm_table *t); |
| 233 | void dm_table_put(struct dm_table *t); |
| 234 | |
| 235 | /* |
| 236 | * Queries |
| 237 | */ |
| 238 | sector_t dm_table_get_size(struct dm_table *t); |
| 239 | unsigned int dm_table_get_num_targets(struct dm_table *t); |
| 240 | int dm_table_get_mode(struct dm_table *t); |
| 241 | struct mapped_device *dm_table_get_md(struct dm_table *t); |
| 242 | |
| 243 | /* |
| 244 | * Trigger an event. |
| 245 | */ |
| 246 | void dm_table_event(struct dm_table *t); |
| 247 | |
| 248 | /* |
| 249 | * The device must be suspended before calling this method. |
| 250 | */ |
| 251 | int dm_swap_table(struct mapped_device *md, struct dm_table *t); |
| 252 | |
Alasdair G Kergon | 0da336e | 2008-04-24 21:43:52 +0100 | [diff] [blame] | 253 | /*----------------------------------------------------------------- |
| 254 | * Macros. |
| 255 | *---------------------------------------------------------------*/ |
| 256 | #define DM_NAME "device-mapper" |
| 257 | |
| 258 | #define DMERR(f, arg...) \ |
| 259 | printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg) |
| 260 | #define DMERR_LIMIT(f, arg...) \ |
| 261 | do { \ |
| 262 | if (printk_ratelimit()) \ |
| 263 | printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " \ |
| 264 | f "\n", ## arg); \ |
| 265 | } while (0) |
| 266 | |
| 267 | #define DMWARN(f, arg...) \ |
| 268 | printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg) |
| 269 | #define DMWARN_LIMIT(f, arg...) \ |
| 270 | do { \ |
| 271 | if (printk_ratelimit()) \ |
| 272 | printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " \ |
| 273 | f "\n", ## arg); \ |
| 274 | } while (0) |
| 275 | |
| 276 | #define DMINFO(f, arg...) \ |
| 277 | printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg) |
| 278 | #define DMINFO_LIMIT(f, arg...) \ |
| 279 | do { \ |
| 280 | if (printk_ratelimit()) \ |
| 281 | printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f \ |
| 282 | "\n", ## arg); \ |
| 283 | } while (0) |
| 284 | |
| 285 | #ifdef CONFIG_DM_DEBUG |
| 286 | # define DMDEBUG(f, arg...) \ |
| 287 | printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX " DEBUG: " f "\n", ## arg) |
| 288 | # define DMDEBUG_LIMIT(f, arg...) \ |
| 289 | do { \ |
| 290 | if (printk_ratelimit()) \ |
| 291 | printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX ": " f \ |
| 292 | "\n", ## arg); \ |
| 293 | } while (0) |
| 294 | #else |
| 295 | # define DMDEBUG(f, arg...) do {} while (0) |
| 296 | # define DMDEBUG_LIMIT(f, arg...) do {} while (0) |
| 297 | #endif |
| 298 | |
| 299 | #define DMEMIT(x...) sz += ((sz >= maxlen) ? \ |
| 300 | 0 : scnprintf(result + sz, maxlen - sz, x)) |
| 301 | |
| 302 | #define SECTOR_SHIFT 9 |
| 303 | |
| 304 | /* |
| 305 | * Definitions of return values from target end_io function. |
| 306 | */ |
| 307 | #define DM_ENDIO_INCOMPLETE 1 |
| 308 | #define DM_ENDIO_REQUEUE 2 |
| 309 | |
| 310 | /* |
| 311 | * Definitions of return values from target map function. |
| 312 | */ |
| 313 | #define DM_MAPIO_SUBMITTED 0 |
| 314 | #define DM_MAPIO_REMAPPED 1 |
| 315 | #define DM_MAPIO_REQUEUE DM_ENDIO_REQUEUE |
| 316 | |
| 317 | /* |
| 318 | * Ceiling(n / sz) |
| 319 | */ |
| 320 | #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz)) |
| 321 | |
| 322 | #define dm_sector_div_up(n, sz) ( \ |
| 323 | { \ |
| 324 | sector_t _r = ((n) + (sz) - 1); \ |
| 325 | sector_div(_r, (sz)); \ |
| 326 | _r; \ |
| 327 | } \ |
| 328 | ) |
| 329 | |
| 330 | /* |
| 331 | * ceiling(n / size) * size |
| 332 | */ |
| 333 | #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz)) |
| 334 | |
| 335 | static inline sector_t to_sector(unsigned long n) |
| 336 | { |
| 337 | return (n >> SECTOR_SHIFT); |
| 338 | } |
| 339 | |
| 340 | static inline unsigned long to_bytes(sector_t n) |
| 341 | { |
| 342 | return (n << SECTOR_SHIFT); |
| 343 | } |
| 344 | |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 345 | #endif /* _LINUX_DEVICE_MAPPER_H */ |