device-mapper.h 12 KB

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