device-mapper.h 8.8 KB

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