device-mapper.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. #include <linux/bio.h>
  10. #include <linux/blkdev.h>
  11. struct dm_target;
  12. struct dm_table;
  13. struct mapped_device;
  14. struct bio_vec;
  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. typedef int (*dm_merge_fn) (struct dm_target *ti, struct bvec_merge_data *bvm,
  63. struct bio_vec *biovec, int max_size);
  64. void dm_error(const char *message);
  65. /*
  66. * Combine device limits.
  67. */
  68. void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev);
  69. struct dm_dev {
  70. struct block_device *bdev;
  71. int mode;
  72. char name[16];
  73. };
  74. /*
  75. * Constructors should call these functions to ensure destination devices
  76. * are opened/closed correctly.
  77. * FIXME: too many arguments.
  78. */
  79. int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
  80. sector_t len, int mode, struct dm_dev **result);
  81. void dm_put_device(struct dm_target *ti, struct dm_dev *d);
  82. /*
  83. * Information about a target type
  84. */
  85. struct target_type {
  86. const char *name;
  87. struct module *module;
  88. unsigned version[3];
  89. dm_ctr_fn ctr;
  90. dm_dtr_fn dtr;
  91. dm_map_fn map;
  92. dm_endio_fn end_io;
  93. dm_flush_fn flush;
  94. dm_presuspend_fn presuspend;
  95. dm_postsuspend_fn postsuspend;
  96. dm_preresume_fn preresume;
  97. dm_resume_fn resume;
  98. dm_status_fn status;
  99. dm_message_fn message;
  100. dm_ioctl_fn ioctl;
  101. dm_merge_fn merge;
  102. };
  103. struct io_restrictions {
  104. unsigned long bounce_pfn;
  105. unsigned long seg_boundary_mask;
  106. unsigned max_hw_sectors;
  107. unsigned max_sectors;
  108. unsigned max_segment_size;
  109. unsigned short hardsect_size;
  110. unsigned short max_hw_segments;
  111. unsigned short max_phys_segments;
  112. unsigned char no_cluster; /* inverted so that 0 is default */
  113. };
  114. struct dm_target {
  115. struct dm_table *table;
  116. struct target_type *type;
  117. /* target limits */
  118. sector_t begin;
  119. sector_t len;
  120. /* FIXME: turn this into a mask, and merge with io_restrictions */
  121. /* Always a power of 2 */
  122. sector_t split_io;
  123. /*
  124. * These are automatically filled in by
  125. * dm_table_get_device.
  126. */
  127. struct io_restrictions limits;
  128. /* target specific data */
  129. void *private;
  130. /* Used to provide an error string from the ctr */
  131. char *error;
  132. };
  133. int dm_register_target(struct target_type *t);
  134. int dm_unregister_target(struct target_type *t);
  135. /*-----------------------------------------------------------------
  136. * Functions for creating and manipulating mapped devices.
  137. * Drop the reference with dm_put when you finish with the object.
  138. *---------------------------------------------------------------*/
  139. /*
  140. * DM_ANY_MINOR chooses the next available minor number.
  141. */
  142. #define DM_ANY_MINOR (-1)
  143. int dm_create(int minor, struct mapped_device **md);
  144. /*
  145. * Reference counting for md.
  146. */
  147. struct mapped_device *dm_get_md(dev_t dev);
  148. void dm_get(struct mapped_device *md);
  149. void dm_put(struct mapped_device *md);
  150. /*
  151. * An arbitrary pointer may be stored alongside a mapped device.
  152. */
  153. void dm_set_mdptr(struct mapped_device *md, void *ptr);
  154. void *dm_get_mdptr(struct mapped_device *md);
  155. /*
  156. * A device can still be used while suspended, but I/O is deferred.
  157. */
  158. int dm_suspend(struct mapped_device *md, unsigned suspend_flags);
  159. int dm_resume(struct mapped_device *md);
  160. /*
  161. * Event functions.
  162. */
  163. uint32_t dm_get_event_nr(struct mapped_device *md);
  164. int dm_wait_event(struct mapped_device *md, int event_nr);
  165. uint32_t dm_next_uevent_seq(struct mapped_device *md);
  166. void dm_uevent_add(struct mapped_device *md, struct list_head *elist);
  167. /*
  168. * Info functions.
  169. */
  170. const char *dm_device_name(struct mapped_device *md);
  171. int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid);
  172. struct gendisk *dm_disk(struct mapped_device *md);
  173. int dm_suspended(struct mapped_device *md);
  174. int dm_noflush_suspending(struct dm_target *ti);
  175. union map_info *dm_get_mapinfo(struct bio *bio);
  176. /*
  177. * Geometry functions.
  178. */
  179. int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo);
  180. int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo);
  181. /*-----------------------------------------------------------------
  182. * Functions for manipulating device-mapper tables.
  183. *---------------------------------------------------------------*/
  184. /*
  185. * First create an empty table.
  186. */
  187. int dm_table_create(struct dm_table **result, int mode,
  188. unsigned num_targets, struct mapped_device *md);
  189. /*
  190. * Then call this once for each target.
  191. */
  192. int dm_table_add_target(struct dm_table *t, const char *type,
  193. sector_t start, sector_t len, char *params);
  194. /*
  195. * Finally call this to make the table ready for use.
  196. */
  197. int dm_table_complete(struct dm_table *t);
  198. /*
  199. * Unplug all devices in a table.
  200. */
  201. void dm_table_unplug_all(struct dm_table *t);
  202. /*
  203. * Table reference counting.
  204. */
  205. struct dm_table *dm_get_table(struct mapped_device *md);
  206. void dm_table_get(struct dm_table *t);
  207. void dm_table_put(struct dm_table *t);
  208. /*
  209. * Queries
  210. */
  211. sector_t dm_table_get_size(struct dm_table *t);
  212. unsigned int dm_table_get_num_targets(struct dm_table *t);
  213. int dm_table_get_mode(struct dm_table *t);
  214. struct mapped_device *dm_table_get_md(struct dm_table *t);
  215. /*
  216. * Trigger an event.
  217. */
  218. void dm_table_event(struct dm_table *t);
  219. /*
  220. * The device must be suspended before calling this method.
  221. */
  222. int dm_swap_table(struct mapped_device *md, struct dm_table *t);
  223. /*
  224. * A wrapper around vmalloc.
  225. */
  226. void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size);
  227. /*-----------------------------------------------------------------
  228. * Macros.
  229. *---------------------------------------------------------------*/
  230. #define DM_NAME "device-mapper"
  231. #define DMERR(f, arg...) \
  232. printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
  233. #define DMERR_LIMIT(f, arg...) \
  234. do { \
  235. if (printk_ratelimit()) \
  236. printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " \
  237. f "\n", ## arg); \
  238. } while (0)
  239. #define DMWARN(f, arg...) \
  240. printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
  241. #define DMWARN_LIMIT(f, arg...) \
  242. do { \
  243. if (printk_ratelimit()) \
  244. printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " \
  245. f "\n", ## arg); \
  246. } while (0)
  247. #define DMINFO(f, arg...) \
  248. printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
  249. #define DMINFO_LIMIT(f, arg...) \
  250. do { \
  251. if (printk_ratelimit()) \
  252. printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f \
  253. "\n", ## arg); \
  254. } while (0)
  255. #ifdef CONFIG_DM_DEBUG
  256. # define DMDEBUG(f, arg...) \
  257. printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX " DEBUG: " f "\n", ## arg)
  258. # define DMDEBUG_LIMIT(f, arg...) \
  259. do { \
  260. if (printk_ratelimit()) \
  261. printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX ": " f \
  262. "\n", ## arg); \
  263. } while (0)
  264. #else
  265. # define DMDEBUG(f, arg...) do {} while (0)
  266. # define DMDEBUG_LIMIT(f, arg...) do {} while (0)
  267. #endif
  268. #define DMEMIT(x...) sz += ((sz >= maxlen) ? \
  269. 0 : scnprintf(result + sz, maxlen - sz, x))
  270. #define SECTOR_SHIFT 9
  271. /*
  272. * Definitions of return values from target end_io function.
  273. */
  274. #define DM_ENDIO_INCOMPLETE 1
  275. #define DM_ENDIO_REQUEUE 2
  276. /*
  277. * Definitions of return values from target map function.
  278. */
  279. #define DM_MAPIO_SUBMITTED 0
  280. #define DM_MAPIO_REMAPPED 1
  281. #define DM_MAPIO_REQUEUE DM_ENDIO_REQUEUE
  282. /*
  283. * Ceiling(n / sz)
  284. */
  285. #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz))
  286. #define dm_sector_div_up(n, sz) ( \
  287. { \
  288. sector_t _r = ((n) + (sz) - 1); \
  289. sector_div(_r, (sz)); \
  290. _r; \
  291. } \
  292. )
  293. /*
  294. * ceiling(n / size) * size
  295. */
  296. #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
  297. static inline sector_t to_sector(unsigned long n)
  298. {
  299. return (n >> SECTOR_SHIFT);
  300. }
  301. static inline unsigned long to_bytes(sector_t n)
  302. {
  303. return (n << SECTOR_SHIFT);
  304. }
  305. #endif /* _LINUX_DEVICE_MAPPER_H */