device-mapper.h 10 KB

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