device-mapper.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. #include <linux/ratelimit.h>
  12. struct dm_dev;
  13. struct dm_target;
  14. struct dm_table;
  15. struct mapped_device;
  16. struct bio_vec;
  17. typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE } status_type_t;
  18. union map_info {
  19. void *ptr;
  20. unsigned long long ll;
  21. unsigned target_request_nr;
  22. };
  23. /*
  24. * In the constructor the target parameter will already have the
  25. * table, type, begin and len fields filled in.
  26. */
  27. typedef int (*dm_ctr_fn) (struct dm_target *target,
  28. unsigned int argc, char **argv);
  29. /*
  30. * The destructor doesn't need to free the dm_target, just
  31. * anything hidden ti->private.
  32. */
  33. typedef void (*dm_dtr_fn) (struct dm_target *ti);
  34. /*
  35. * The map function must return:
  36. * < 0: error
  37. * = 0: The target will handle the io by resubmitting it later
  38. * = 1: simple remap complete
  39. * = 2: The target wants to push back the io
  40. */
  41. typedef int (*dm_map_fn) (struct dm_target *ti, struct bio *bio,
  42. union map_info *map_context);
  43. typedef int (*dm_map_request_fn) (struct dm_target *ti, struct request *clone,
  44. union map_info *map_context);
  45. /*
  46. * Returns:
  47. * < 0 : error (currently ignored)
  48. * 0 : ended successfully
  49. * 1 : for some reason the io has still not completed (eg,
  50. * multipath target might want to requeue a failed io).
  51. * 2 : The target wants to push back the io
  52. */
  53. typedef int (*dm_endio_fn) (struct dm_target *ti,
  54. struct bio *bio, int error,
  55. union map_info *map_context);
  56. typedef int (*dm_request_endio_fn) (struct dm_target *ti,
  57. struct request *clone, int error,
  58. union map_info *map_context);
  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. unsigned status_flags, char *result, unsigned 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. struct target_type {
  107. uint64_t features;
  108. const char *name;
  109. struct module *module;
  110. unsigned version[3];
  111. dm_ctr_fn ctr;
  112. dm_dtr_fn dtr;
  113. dm_map_fn map;
  114. dm_map_request_fn map_rq;
  115. dm_endio_fn end_io;
  116. dm_request_endio_fn rq_end_io;
  117. dm_presuspend_fn presuspend;
  118. dm_postsuspend_fn postsuspend;
  119. dm_preresume_fn preresume;
  120. dm_resume_fn resume;
  121. dm_status_fn status;
  122. dm_message_fn message;
  123. dm_ioctl_fn ioctl;
  124. dm_merge_fn merge;
  125. dm_busy_fn busy;
  126. dm_iterate_devices_fn iterate_devices;
  127. dm_io_hints_fn io_hints;
  128. /* For internal device-mapper use. */
  129. struct list_head list;
  130. };
  131. /*
  132. * Target features
  133. */
  134. /*
  135. * Any table that contains an instance of this target must have only one.
  136. */
  137. #define DM_TARGET_SINGLETON 0x00000001
  138. #define dm_target_needs_singleton(type) ((type)->features & DM_TARGET_SINGLETON)
  139. /*
  140. * Indicates that a target does not support read-only devices.
  141. */
  142. #define DM_TARGET_ALWAYS_WRITEABLE 0x00000002
  143. #define dm_target_always_writeable(type) \
  144. ((type)->features & DM_TARGET_ALWAYS_WRITEABLE)
  145. /*
  146. * Any device that contains a table with an instance of this target may never
  147. * have tables containing any different target type.
  148. */
  149. #define DM_TARGET_IMMUTABLE 0x00000004
  150. #define dm_target_is_immutable(type) ((type)->features & DM_TARGET_IMMUTABLE)
  151. struct dm_target {
  152. struct dm_table *table;
  153. struct target_type *type;
  154. /* target limits */
  155. sector_t begin;
  156. sector_t len;
  157. /* If non-zero, maximum size of I/O submitted to a target. */
  158. uint32_t max_io_len;
  159. /*
  160. * A number of zero-length barrier requests that will be submitted
  161. * to the target for the purpose of flushing cache.
  162. *
  163. * The request number will be placed in union map_info->target_request_nr.
  164. * It is a responsibility of the target driver to remap these requests
  165. * to the real underlying devices.
  166. */
  167. unsigned num_flush_requests;
  168. /*
  169. * The number of discard requests that will be submitted to the
  170. * target. map_info->request_nr is used just like num_flush_requests.
  171. */
  172. unsigned num_discard_requests;
  173. /* target specific data */
  174. void *private;
  175. /* Used to provide an error string from the ctr */
  176. char *error;
  177. /*
  178. * Set if this target needs to receive flushes regardless of
  179. * whether or not its underlying devices have support.
  180. */
  181. bool flush_supported:1;
  182. /*
  183. * Set if this target needs to receive discards regardless of
  184. * whether or not its underlying devices have support.
  185. */
  186. bool discards_supported:1;
  187. /*
  188. * Set if the target required discard request to be split
  189. * on max_io_len boundary.
  190. */
  191. bool split_discard_requests:1;
  192. /*
  193. * Set if this target does not return zeroes on discarded blocks.
  194. */
  195. bool discard_zeroes_data_unsupported:1;
  196. };
  197. /* Each target can link one of these into the table */
  198. struct dm_target_callbacks {
  199. struct list_head list;
  200. int (*congested_fn) (struct dm_target_callbacks *, int);
  201. };
  202. int dm_register_target(struct target_type *t);
  203. void dm_unregister_target(struct target_type *t);
  204. /*
  205. * Target argument parsing.
  206. */
  207. struct dm_arg_set {
  208. unsigned argc;
  209. char **argv;
  210. };
  211. /*
  212. * The minimum and maximum value of a numeric argument, together with
  213. * the error message to use if the number is found to be outside that range.
  214. */
  215. struct dm_arg {
  216. unsigned min;
  217. unsigned max;
  218. char *error;
  219. };
  220. /*
  221. * Validate the next argument, either returning it as *value or, if invalid,
  222. * returning -EINVAL and setting *error.
  223. */
  224. int dm_read_arg(struct dm_arg *arg, struct dm_arg_set *arg_set,
  225. unsigned *value, char **error);
  226. /*
  227. * Process the next argument as the start of a group containing between
  228. * arg->min and arg->max further arguments. Either return the size as
  229. * *num_args or, if invalid, return -EINVAL and set *error.
  230. */
  231. int dm_read_arg_group(struct dm_arg *arg, struct dm_arg_set *arg_set,
  232. unsigned *num_args, char **error);
  233. /*
  234. * Return the current argument and shift to the next.
  235. */
  236. const char *dm_shift_arg(struct dm_arg_set *as);
  237. /*
  238. * Move through num_args arguments.
  239. */
  240. void dm_consume_args(struct dm_arg_set *as, unsigned num_args);
  241. /*-----------------------------------------------------------------
  242. * Functions for creating and manipulating mapped devices.
  243. * Drop the reference with dm_put when you finish with the object.
  244. *---------------------------------------------------------------*/
  245. /*
  246. * DM_ANY_MINOR chooses the next available minor number.
  247. */
  248. #define DM_ANY_MINOR (-1)
  249. int dm_create(int minor, struct mapped_device **md);
  250. /*
  251. * Reference counting for md.
  252. */
  253. struct mapped_device *dm_get_md(dev_t dev);
  254. void dm_get(struct mapped_device *md);
  255. void dm_put(struct mapped_device *md);
  256. /*
  257. * An arbitrary pointer may be stored alongside a mapped device.
  258. */
  259. void dm_set_mdptr(struct mapped_device *md, void *ptr);
  260. void *dm_get_mdptr(struct mapped_device *md);
  261. /*
  262. * A device can still be used while suspended, but I/O is deferred.
  263. */
  264. int dm_suspend(struct mapped_device *md, unsigned suspend_flags);
  265. int dm_resume(struct mapped_device *md);
  266. /*
  267. * Event functions.
  268. */
  269. uint32_t dm_get_event_nr(struct mapped_device *md);
  270. int dm_wait_event(struct mapped_device *md, int event_nr);
  271. uint32_t dm_next_uevent_seq(struct mapped_device *md);
  272. void dm_uevent_add(struct mapped_device *md, struct list_head *elist);
  273. /*
  274. * Info functions.
  275. */
  276. const char *dm_device_name(struct mapped_device *md);
  277. int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid);
  278. struct gendisk *dm_disk(struct mapped_device *md);
  279. int dm_suspended(struct dm_target *ti);
  280. int dm_noflush_suspending(struct dm_target *ti);
  281. union map_info *dm_get_mapinfo(struct bio *bio);
  282. union map_info *dm_get_rq_mapinfo(struct request *rq);
  283. /*
  284. * Geometry functions.
  285. */
  286. int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo);
  287. int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo);
  288. /*-----------------------------------------------------------------
  289. * Functions for manipulating device-mapper tables.
  290. *---------------------------------------------------------------*/
  291. /*
  292. * First create an empty table.
  293. */
  294. int dm_table_create(struct dm_table **result, fmode_t mode,
  295. unsigned num_targets, struct mapped_device *md);
  296. /*
  297. * Then call this once for each target.
  298. */
  299. int dm_table_add_target(struct dm_table *t, const char *type,
  300. sector_t start, sector_t len, char *params);
  301. /*
  302. * Target_ctr should call this if it needs to add any callbacks.
  303. */
  304. void dm_table_add_target_callbacks(struct dm_table *t, struct dm_target_callbacks *cb);
  305. /*
  306. * Finally call this to make the table ready for use.
  307. */
  308. int dm_table_complete(struct dm_table *t);
  309. /*
  310. * Target may require that it is never sent I/O larger than len.
  311. */
  312. int __must_check dm_set_target_max_io_len(struct dm_target *ti, sector_t len);
  313. /*
  314. * Table reference counting.
  315. */
  316. struct dm_table *dm_get_live_table(struct mapped_device *md);
  317. void dm_table_get(struct dm_table *t);
  318. void dm_table_put(struct dm_table *t);
  319. /*
  320. * Queries
  321. */
  322. sector_t dm_table_get_size(struct dm_table *t);
  323. unsigned int dm_table_get_num_targets(struct dm_table *t);
  324. fmode_t dm_table_get_mode(struct dm_table *t);
  325. struct mapped_device *dm_table_get_md(struct dm_table *t);
  326. /*
  327. * Trigger an event.
  328. */
  329. void dm_table_event(struct dm_table *t);
  330. /*
  331. * The device must be suspended before calling this method.
  332. * Returns the previous table, which the caller must destroy.
  333. */
  334. struct dm_table *dm_swap_table(struct mapped_device *md,
  335. struct dm_table *t);
  336. /*
  337. * A wrapper around vmalloc.
  338. */
  339. void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size);
  340. /*-----------------------------------------------------------------
  341. * Macros.
  342. *---------------------------------------------------------------*/
  343. #define DM_NAME "device-mapper"
  344. #ifdef CONFIG_PRINTK
  345. extern struct ratelimit_state dm_ratelimit_state;
  346. #define dm_ratelimit() __ratelimit(&dm_ratelimit_state)
  347. #else
  348. #define dm_ratelimit() 0
  349. #endif
  350. #define DMCRIT(f, arg...) \
  351. printk(KERN_CRIT DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
  352. #define DMERR(f, arg...) \
  353. printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
  354. #define DMERR_LIMIT(f, arg...) \
  355. do { \
  356. if (dm_ratelimit()) \
  357. printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " \
  358. f "\n", ## arg); \
  359. } while (0)
  360. #define DMWARN(f, arg...) \
  361. printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
  362. #define DMWARN_LIMIT(f, arg...) \
  363. do { \
  364. if (dm_ratelimit()) \
  365. printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " \
  366. f "\n", ## arg); \
  367. } while (0)
  368. #define DMINFO(f, arg...) \
  369. printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
  370. #define DMINFO_LIMIT(f, arg...) \
  371. do { \
  372. if (dm_ratelimit()) \
  373. printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f \
  374. "\n", ## arg); \
  375. } while (0)
  376. #ifdef CONFIG_DM_DEBUG
  377. # define DMDEBUG(f, arg...) \
  378. printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX " DEBUG: " f "\n", ## arg)
  379. # define DMDEBUG_LIMIT(f, arg...) \
  380. do { \
  381. if (dm_ratelimit()) \
  382. printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX ": " f \
  383. "\n", ## arg); \
  384. } while (0)
  385. #else
  386. # define DMDEBUG(f, arg...) do {} while (0)
  387. # define DMDEBUG_LIMIT(f, arg...) do {} while (0)
  388. #endif
  389. #define DMEMIT(x...) sz += ((sz >= maxlen) ? \
  390. 0 : scnprintf(result + sz, maxlen - sz, x))
  391. #define SECTOR_SHIFT 9
  392. /*
  393. * Definitions of return values from target end_io function.
  394. */
  395. #define DM_ENDIO_INCOMPLETE 1
  396. #define DM_ENDIO_REQUEUE 2
  397. /*
  398. * Definitions of return values from target map function.
  399. */
  400. #define DM_MAPIO_SUBMITTED 0
  401. #define DM_MAPIO_REMAPPED 1
  402. #define DM_MAPIO_REQUEUE DM_ENDIO_REQUEUE
  403. /*
  404. * Ceiling(n / sz)
  405. */
  406. #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz))
  407. #define dm_sector_div_up(n, sz) ( \
  408. { \
  409. sector_t _r = ((n) + (sz) - 1); \
  410. sector_div(_r, (sz)); \
  411. _r; \
  412. } \
  413. )
  414. /*
  415. * ceiling(n / size) * size
  416. */
  417. #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
  418. #define dm_array_too_big(fixed, obj, num) \
  419. ((num) > (UINT_MAX - (fixed)) / (obj))
  420. /*
  421. * Sector offset taken relative to the start of the target instead of
  422. * relative to the start of the device.
  423. */
  424. #define dm_target_offset(ti, sector) ((sector) - (ti)->begin)
  425. static inline sector_t to_sector(unsigned long n)
  426. {
  427. return (n >> SECTOR_SHIFT);
  428. }
  429. static inline unsigned long to_bytes(sector_t n)
  430. {
  431. return (n << SECTOR_SHIFT);
  432. }
  433. /*-----------------------------------------------------------------
  434. * Helper for block layer and dm core operations
  435. *---------------------------------------------------------------*/
  436. void dm_dispatch_request(struct request *rq);
  437. void dm_requeue_unmapped_request(struct request *rq);
  438. void dm_kill_unmapped_request(struct request *rq, int error);
  439. int dm_underlying_device_busy(struct request_queue *q);
  440. #endif /* _LINUX_DEVICE_MAPPER_H */